/src/ffmpeg/libavformat/av1dec.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * AV1 Annex B demuxer |
3 | | * Copyright (c) 2019 James Almer <jamrial@gmail.com> |
4 | | * |
5 | | * This file is part of FFmpeg. |
6 | | * |
7 | | * FFmpeg is free software; you can redistribute it and/or |
8 | | * modify it under the terms of the GNU Lesser General Public |
9 | | * License as published by the Free Software Foundation; either |
10 | | * version 2.1 of the License, or (at your option) any later version. |
11 | | * |
12 | | * FFmpeg is distributed in the hope that it will be useful, |
13 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
15 | | * Lesser General Public License for more details. |
16 | | * |
17 | | * You should have received a copy of the GNU Lesser General Public |
18 | | * License along with FFmpeg; if not, write to the Free Software |
19 | | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
20 | | */ |
21 | | |
22 | | #include "config_components.h" |
23 | | |
24 | | #include "libavutil/common.h" |
25 | | #include "libavutil/opt.h" |
26 | | #include "libavcodec/av1_parse.h" |
27 | | #include "libavcodec/bsf.h" |
28 | | #include "avformat.h" |
29 | | #include "avio_internal.h" |
30 | | #include "demux.h" |
31 | | #include "internal.h" |
32 | | |
33 | | typedef struct AV1DemuxContext { |
34 | | const AVClass *class; |
35 | | AVBSFContext *bsf; |
36 | | AVRational framerate; |
37 | | uint32_t temporal_unit_size; |
38 | | uint32_t frame_unit_size; |
39 | | } AV1DemuxContext; |
40 | | |
41 | | //return < 0 if we need more data |
42 | | static int get_score(int type, int *seq) |
43 | 7.09k | { |
44 | 7.09k | switch (type) { |
45 | 3.79k | case AV1_OBU_SEQUENCE_HEADER: |
46 | 3.79k | *seq = 1; |
47 | 3.79k | return -1; |
48 | 472 | case AV1_OBU_FRAME: |
49 | 745 | case AV1_OBU_FRAME_HEADER: |
50 | 745 | return *seq ? AVPROBE_SCORE_EXTENSION + 1 : 0; |
51 | 1.05k | case AV1_OBU_METADATA: |
52 | 2.12k | case AV1_OBU_PADDING: |
53 | 2.12k | return -1; |
54 | 434 | default: |
55 | 434 | break; |
56 | 7.09k | } |
57 | 434 | return 0; |
58 | 7.09k | } |
59 | | |
60 | | static int av1_read_header(AVFormatContext *s) |
61 | 19.5k | { |
62 | 19.5k | AV1DemuxContext *const c = s->priv_data; |
63 | 19.5k | const AVBitStreamFilter *filter = av_bsf_get_by_name("av1_frame_merge"); |
64 | 19.5k | AVStream *st; |
65 | 19.5k | FFStream *sti; |
66 | 19.5k | int ret; |
67 | | |
68 | 19.5k | if (!filter) { |
69 | 0 | av_log(s, AV_LOG_ERROR, "av1_frame_merge bitstream filter " |
70 | 0 | "not found. This is a bug, please report it.\n"); |
71 | 0 | return AVERROR_BUG; |
72 | 0 | } |
73 | | |
74 | 19.5k | st = avformat_new_stream(s, NULL); |
75 | 19.5k | if (!st) |
76 | 0 | return AVERROR(ENOMEM); |
77 | 19.5k | sti = ffstream(st); |
78 | | |
79 | 19.5k | st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO; |
80 | 19.5k | st->codecpar->codec_id = AV_CODEC_ID_AV1; |
81 | 19.5k | sti->need_parsing = AVSTREAM_PARSE_HEADERS; |
82 | | |
83 | 19.5k | st->avg_frame_rate = c->framerate; |
84 | | // taken from rawvideo demuxers |
85 | 19.5k | avpriv_set_pts_info(st, 64, 1, 1200000); |
86 | | |
87 | 19.5k | ret = av_bsf_alloc(filter, &c->bsf); |
88 | 19.5k | if (ret < 0) |
89 | 0 | return ret; |
90 | | |
91 | 19.5k | ret = avcodec_parameters_copy(c->bsf->par_in, st->codecpar); |
92 | 19.5k | if (ret < 0) |
93 | 0 | return ret; |
94 | | |
95 | 19.5k | ret = av_bsf_init(c->bsf); |
96 | 19.5k | if (ret < 0) |
97 | 0 | return ret; |
98 | | |
99 | 19.5k | return 0; |
100 | 19.5k | } |
101 | | |
102 | | static int av1_read_close(AVFormatContext *s) |
103 | 19.5k | { |
104 | 19.5k | AV1DemuxContext *const c = s->priv_data; |
105 | | |
106 | 19.5k | av_bsf_free(&c->bsf); |
107 | 19.5k | return 0; |
108 | 19.5k | } |
109 | | |
110 | | #define DEC AV_OPT_FLAG_DECODING_PARAM |
111 | | #define OFFSET(x) offsetof(AV1DemuxContext, x) |
112 | | static const AVOption av1_options[] = { |
113 | | { "framerate", "", OFFSET(framerate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, INT_MAX, DEC}, |
114 | | { NULL }, |
115 | | }; |
116 | | #undef OFFSET |
117 | | |
118 | | static const AVClass av1_demuxer_class = { |
119 | | .class_name = "AV1 Annex B/low overhead OBU demuxer", |
120 | | .item_name = av_default_item_name, |
121 | | .option = av1_options, |
122 | | .version = LIBAVUTIL_VERSION_INT, |
123 | | }; |
124 | | |
125 | | #if CONFIG_AV1_DEMUXER |
126 | | |
127 | 1.26M | static int leb(AVIOContext *pb, uint32_t *len, int eof) { |
128 | 1.26M | int more, i = 0; |
129 | 1.26M | *len = 0; |
130 | 1.55M | do { |
131 | 1.55M | unsigned bits; |
132 | 1.55M | int byte = avio_r8(pb); |
133 | 1.55M | if (pb->error) |
134 | 0 | return pb->error; |
135 | 1.55M | if (pb->eof_reached) |
136 | 15.3k | return (eof && !i) ? AVERROR_EOF : AVERROR_INVALIDDATA; |
137 | 1.53M | more = byte & 0x80; |
138 | 1.53M | bits = byte & 0x7f; |
139 | 1.53M | if (i <= 3 || (i == 4 && bits < (1 << 4))) |
140 | 1.51M | *len |= bits << (i * 7); |
141 | 25.4k | else if (bits) |
142 | 22.3k | return AVERROR_INVALIDDATA; |
143 | 1.51M | if (++i == 8 && more) |
144 | 538 | return AVERROR_INVALIDDATA; |
145 | 1.51M | } while (more); |
146 | 1.22M | return i; |
147 | 1.26M | } |
148 | | |
149 | | static int read_obu(const uint8_t *buf, int size, int64_t *obu_size, int *type) |
150 | 49.1k | { |
151 | 49.1k | int start_pos, temporal_id, spatial_id; |
152 | 49.1k | int len; |
153 | | |
154 | 49.1k | len = parse_obu_header(buf, size, obu_size, &start_pos, |
155 | 49.1k | type, &temporal_id, &spatial_id); |
156 | 49.1k | if (len < 0) |
157 | 24.4k | return len; |
158 | | |
159 | 24.6k | return 0; |
160 | 49.1k | } |
161 | | |
162 | | static int annexb_probe(const AVProbeData *p) |
163 | 358k | { |
164 | 358k | FFIOContext ctx; |
165 | 358k | AVIOContext *const pb = &ctx.pub; |
166 | 358k | int64_t obu_size; |
167 | 358k | uint32_t temporal_unit_size, frame_unit_size, obu_unit_size; |
168 | 358k | int seq = 0; |
169 | 358k | int ret, type, cnt = 0; |
170 | | |
171 | 358k | ffio_init_read_context(&ctx, p->buf, p->buf_size); |
172 | | |
173 | 358k | ret = leb(pb, &temporal_unit_size, 1); |
174 | 358k | if (ret < 0) |
175 | 18.3k | return 0; |
176 | 340k | cnt += ret; |
177 | 340k | ret = leb(pb, &frame_unit_size, 0); |
178 | 340k | if (ret < 0 || ((int64_t)frame_unit_size + ret) > temporal_unit_size) |
179 | 211k | return 0; |
180 | 128k | cnt += ret; |
181 | 128k | ret = leb(pb, &obu_unit_size, 0); |
182 | 128k | if (ret < 0 || ((int64_t)obu_unit_size + ret) >= frame_unit_size) |
183 | 75.1k | return 0; |
184 | 53.0k | cnt += ret; |
185 | | |
186 | 53.0k | frame_unit_size -= obu_unit_size + ret; |
187 | | |
188 | 53.0k | avio_skip(pb, obu_unit_size); |
189 | 53.0k | if (pb->eof_reached || pb->error) |
190 | 8.27k | return 0; |
191 | | |
192 | | // Check that the first OBU is a Temporal Delimiter. |
193 | 44.7k | ret = read_obu(p->buf + cnt, FFMIN(p->buf_size - cnt, obu_unit_size), &obu_size, &type); |
194 | 44.7k | if (ret < 0 || type != AV1_OBU_TEMPORAL_DELIMITER || obu_size > 0) |
195 | 42.8k | return 0; |
196 | 1.93k | cnt += obu_unit_size; |
197 | | |
198 | 5.30k | do { |
199 | 5.30k | ret = leb(pb, &obu_unit_size, 0); |
200 | 5.30k | if (ret < 0 || ((int64_t)obu_unit_size + ret) > frame_unit_size) |
201 | 735 | return 0; |
202 | 4.57k | cnt += ret; |
203 | | |
204 | 4.57k | avio_skip(pb, obu_unit_size); |
205 | 4.57k | if (pb->eof_reached || pb->error) |
206 | 247 | return 0; |
207 | | |
208 | 4.32k | ret = read_obu(p->buf + cnt, FFMIN(p->buf_size - cnt, obu_unit_size), &obu_size, &type); |
209 | 4.32k | if (ret < 0) |
210 | 407 | return 0; |
211 | 3.91k | cnt += obu_unit_size; |
212 | | |
213 | 3.91k | ret = get_score(type, &seq); |
214 | 3.91k | if (ret >= 0) |
215 | 542 | return ret; |
216 | | |
217 | 3.37k | frame_unit_size -= obu_unit_size + ret; |
218 | 3.37k | } while (frame_unit_size); |
219 | | |
220 | 0 | return 0; |
221 | 1.93k | } |
222 | | |
223 | | static int annexb_read_packet(AVFormatContext *s, AVPacket *pkt) |
224 | 139k | { |
225 | 139k | AV1DemuxContext *const c = s->priv_data; |
226 | 139k | uint32_t obu_unit_size; |
227 | 139k | int ret, len; |
228 | | |
229 | 424k | retry: |
230 | 424k | if (avio_feof(s->pb)) { |
231 | 4.84k | if (c->temporal_unit_size || c->frame_unit_size) |
232 | 1.91k | return AVERROR_INVALIDDATA; |
233 | 2.93k | goto end; |
234 | 4.84k | } |
235 | | |
236 | 419k | if (!c->temporal_unit_size) { |
237 | 10.0k | len = leb(s->pb, &c->temporal_unit_size, 1); |
238 | 10.0k | if (len == AVERROR_EOF) goto end; |
239 | 7.13k | else if (len < 0) return len; |
240 | 10.0k | } |
241 | | |
242 | 416k | if (!c->frame_unit_size) { |
243 | 7.21k | len = leb(s->pb, &c->frame_unit_size, 0); |
244 | 7.21k | if (len < 0) |
245 | 389 | return len; |
246 | 6.82k | if (((int64_t)c->frame_unit_size + len) > c->temporal_unit_size) |
247 | 2.32k | return AVERROR_INVALIDDATA; |
248 | 4.49k | c->temporal_unit_size -= len; |
249 | 4.49k | } |
250 | | |
251 | 413k | len = leb(s->pb, &obu_unit_size, 0); |
252 | 413k | if (len < 0) |
253 | 3.50k | return len; |
254 | 410k | if (((int64_t)obu_unit_size + len) > c->frame_unit_size) |
255 | 423 | return AVERROR_INVALIDDATA; |
256 | | |
257 | 409k | ret = av_get_packet(s->pb, pkt, obu_unit_size); |
258 | 409k | if (ret < 0) |
259 | 581 | return ret; |
260 | 409k | if (ret != obu_unit_size) |
261 | 779 | return AVERROR_INVALIDDATA; |
262 | | |
263 | 408k | c->temporal_unit_size -= obu_unit_size + len; |
264 | 408k | c->frame_unit_size -= obu_unit_size + len; |
265 | | |
266 | 414k | end: |
267 | 414k | ret = av_bsf_send_packet(c->bsf, pkt); |
268 | 414k | if (ret < 0) { |
269 | 75 | av_log(s, AV_LOG_ERROR, "Failed to send packet to " |
270 | 75 | "av1_frame_merge filter\n"); |
271 | 75 | return ret; |
272 | 75 | } |
273 | | |
274 | 414k | ret = av_bsf_receive_packet(c->bsf, pkt); |
275 | 414k | if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF) |
276 | 2.93k | av_log(s, AV_LOG_ERROR, "av1_frame_merge filter failed to " |
277 | 2.93k | "send output packet\n"); |
278 | | |
279 | 414k | if (ret == AVERROR(EAGAIN)) |
280 | 284k | goto retry; |
281 | | |
282 | 129k | return ret; |
283 | 414k | } |
284 | | |
285 | | const FFInputFormat ff_av1_demuxer = { |
286 | | .p.name = "av1", |
287 | | .p.long_name = NULL_IF_CONFIG_SMALL("AV1 Annex B"), |
288 | | .p.extensions = "obu", |
289 | | .p.flags = AVFMT_GENERIC_INDEX | AVFMT_NOTIMESTAMPS, |
290 | | .p.priv_class = &av1_demuxer_class, |
291 | | .priv_data_size = sizeof(AV1DemuxContext), |
292 | | .flags_internal = FF_INFMT_FLAG_INIT_CLEANUP, |
293 | | .read_probe = annexb_probe, |
294 | | .read_header = av1_read_header, |
295 | | .read_packet = annexb_read_packet, |
296 | | .read_close = av1_read_close, |
297 | | }; |
298 | | #endif |
299 | | |
300 | | #if CONFIG_OBU_DEMUXER |
301 | | //For low overhead obu, we can't foresee the obu size before we parsed the header. |
302 | | //So, we can't use parse_obu_header here, since it will check size <= buf_size |
303 | | //see c27c7b49dc for more details |
304 | | static int read_obu_with_size(const uint8_t *buf, int buf_size, int64_t *obu_size, int *type) |
305 | 1.57M | { |
306 | 1.57M | GetBitContext gb; |
307 | 1.57M | int ret, extension_flag, start_pos; |
308 | 1.57M | int64_t size; |
309 | | |
310 | 1.57M | ret = init_get_bits8(&gb, buf, FFMIN(buf_size, MAX_OBU_HEADER_SIZE)); |
311 | 1.57M | if (ret < 0) |
312 | 0 | return ret; |
313 | | |
314 | 1.57M | if (get_bits1(&gb) != 0) // obu_forbidden_bit |
315 | 75.4k | return AVERROR_INVALIDDATA; |
316 | | |
317 | 1.50M | *type = get_bits(&gb, 4); |
318 | 1.50M | extension_flag = get_bits1(&gb); |
319 | 1.50M | if (!get_bits1(&gb)) // has_size_flag |
320 | 206k | return AVERROR_INVALIDDATA; |
321 | 1.29M | skip_bits1(&gb); // obu_reserved_1bit |
322 | | |
323 | 1.29M | if (extension_flag) { |
324 | 172k | get_bits(&gb, 3); // temporal_id |
325 | 172k | get_bits(&gb, 2); // spatial_id |
326 | 172k | skip_bits(&gb, 3); // extension_header_reserved_3bits |
327 | 172k | } |
328 | | |
329 | 1.29M | *obu_size = get_leb128(&gb); |
330 | 1.29M | if (*obu_size > INT_MAX) |
331 | 826 | return AVERROR_INVALIDDATA; |
332 | | |
333 | 1.29M | if (get_bits_left(&gb) < 0) |
334 | 968 | return AVERROR_INVALIDDATA; |
335 | | |
336 | 1.29M | start_pos = get_bits_count(&gb) / 8; |
337 | | |
338 | 1.29M | size = *obu_size + start_pos; |
339 | 1.29M | if (size > INT_MAX) |
340 | 62 | return AVERROR_INVALIDDATA; |
341 | 1.29M | return size; |
342 | 1.29M | } |
343 | | |
344 | | static int obu_probe(const AVProbeData *p) |
345 | 358k | { |
346 | 358k | int64_t obu_size; |
347 | 358k | int seq = 0; |
348 | 358k | int ret, type, cnt; |
349 | | |
350 | | // Check that the first OBU is a Temporal Delimiter. |
351 | 358k | cnt = read_obu_with_size(p->buf, p->buf_size, &obu_size, &type); |
352 | 358k | if (cnt < 0 || type != AV1_OBU_TEMPORAL_DELIMITER || obu_size != 0) |
353 | 356k | return 0; |
354 | | |
355 | 4.19k | while (1) { |
356 | 4.19k | ret = read_obu_with_size(p->buf + cnt, p->buf_size - cnt, &obu_size, &type); |
357 | 4.19k | if (ret < 0 || obu_size <= 0) |
358 | 1.01k | return 0; |
359 | 3.18k | cnt += FFMIN(ret, p->buf_size - cnt); |
360 | | |
361 | 3.18k | ret = get_score(type, &seq); |
362 | 3.18k | if (ret >= 0) |
363 | 637 | return ret; |
364 | 3.18k | } |
365 | 0 | return 0; |
366 | 1.64k | } |
367 | | |
368 | | static int obu_get_packet(AVFormatContext *s, AVPacket *pkt) |
369 | 1.23M | { |
370 | 1.23M | AV1DemuxContext *const c = s->priv_data; |
371 | 1.23M | uint8_t header[MAX_OBU_HEADER_SIZE + AV_INPUT_BUFFER_PADDING_SIZE]; |
372 | 1.23M | int64_t obu_size; |
373 | 1.23M | int size; |
374 | 1.23M | int ret, len, type; |
375 | | |
376 | 1.23M | if ((ret = ffio_ensure_seekback(s->pb, MAX_OBU_HEADER_SIZE)) < 0) |
377 | 0 | return ret; |
378 | 1.23M | size = avio_read(s->pb, header, MAX_OBU_HEADER_SIZE); |
379 | 1.23M | if (size < 0) |
380 | 16.6k | return size; |
381 | | |
382 | 1.21M | memset(header + size, 0, AV_INPUT_BUFFER_PADDING_SIZE); |
383 | 1.21M | len = read_obu_with_size(header, size, &obu_size, &type); |
384 | 1.21M | if (len < 0) { |
385 | 1.60k | av_log(c, AV_LOG_ERROR, "Failed to read obu\n"); |
386 | 1.60k | return len; |
387 | 1.60k | } |
388 | 1.21M | avio_seek(s->pb, -size, SEEK_CUR); |
389 | | |
390 | 1.21M | ret = av_get_packet(s->pb, pkt, len); |
391 | 1.21M | if (ret != len) { |
392 | 989 | av_log(c, AV_LOG_ERROR, "Failed to get packet for obu\n"); |
393 | 989 | return ret < 0 ? ret : AVERROR_INVALIDDATA; |
394 | 989 | } |
395 | 1.21M | return 0; |
396 | 1.21M | } |
397 | | |
398 | | static int obu_read_packet(AVFormatContext *s, AVPacket *pkt) |
399 | 152k | { |
400 | 152k | AV1DemuxContext *const c = s->priv_data; |
401 | 152k | int ret; |
402 | | |
403 | 152k | if (s->io_repositioned) { |
404 | 0 | av_bsf_flush(c->bsf); |
405 | 0 | s->io_repositioned = 0; |
406 | 0 | } |
407 | 1.23M | while (1) { |
408 | 1.23M | ret = obu_get_packet(s, pkt); |
409 | | /* In case of AVERROR_EOF we need to flush the BSF. Conveniently |
410 | | * obu_get_packet() returns a blank pkt in this case which |
411 | | * can be used to signal that the BSF should be flushed. */ |
412 | 1.23M | if (ret < 0 && ret != AVERROR_EOF) |
413 | 2.56k | return ret; |
414 | 1.23M | ret = av_bsf_send_packet(c->bsf, pkt); |
415 | 1.23M | if (ret < 0) { |
416 | 0 | av_log(s, AV_LOG_ERROR, "Failed to send packet to " |
417 | 0 | "av1_frame_merge filter\n"); |
418 | 0 | return ret; |
419 | 0 | } |
420 | 1.23M | ret = av_bsf_receive_packet(c->bsf, pkt); |
421 | 1.23M | if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF) |
422 | 3.54k | av_log(s, AV_LOG_ERROR, "av1_frame_merge filter failed to " |
423 | 3.54k | "send output packet\n"); |
424 | 1.23M | if (ret != AVERROR(EAGAIN)) |
425 | 150k | break; |
426 | 1.23M | } |
427 | | |
428 | 150k | return ret; |
429 | 152k | } |
430 | | |
431 | | const FFInputFormat ff_obu_demuxer = { |
432 | | .p.name = "obu", |
433 | | .p.long_name = NULL_IF_CONFIG_SMALL("AV1 low overhead OBU"), |
434 | | .p.extensions = "obu", |
435 | | .p.flags = AVFMT_GENERIC_INDEX | AVFMT_NO_BYTE_SEEK | AVFMT_NOTIMESTAMPS, |
436 | | .p.priv_class = &av1_demuxer_class, |
437 | | .priv_data_size = sizeof(AV1DemuxContext), |
438 | | .flags_internal = FF_INFMT_FLAG_INIT_CLEANUP, |
439 | | .read_probe = obu_probe, |
440 | | .read_header = av1_read_header, |
441 | | .read_packet = obu_read_packet, |
442 | | .read_close = av1_read_close, |
443 | | }; |
444 | | #endif |