3 * Copyright (c) 2019 James Almer <jamrial@gmail.com>
5 * This file is part of FFmpeg.
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.
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.
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
24 #include "libavutil/common.h"
25 #include "libavutil/fifo.h"
26 #include "libavutil/opt.h"
27 #include "libavcodec/av1_parse.h"
29 #include "avio_internal.h"
32 typedef struct AnnexBContext {
35 uint32_t temporal_unit_size;
36 uint32_t frame_unit_size;
40 static int leb(AVIOContext *pb, uint32_t *len) {
49 if (i <= 3 || (i == 4 && bits < (1 << 4)))
50 *len |= bits << (i * 7);
52 return AVERROR_INVALIDDATA;
54 return AVERROR_INVALIDDATA;
55 if (pb->eof_reached || pb->error)
56 return pb->error ? pb->error : AVERROR(EIO);
61 static int read_obu(const uint8_t *buf, int size, int64_t *obu_size, int *type)
63 int start_pos, temporal_id, spatial_id;
66 len = parse_obu_header(buf, size, obu_size, &start_pos,
67 type, &temporal_id, &spatial_id);
74 //return < 0 if we need more data
75 static int get_score(int type, int *seq)
78 case AV1_OBU_SEQUENCE_HEADER:
82 case AV1_OBU_FRAME_HEADER:
83 return *seq ? AVPROBE_SCORE_EXTENSION + 1 : 0;
84 case AV1_OBU_METADATA:
93 static int annexb_probe(const AVProbeData *p)
97 uint32_t temporal_unit_size, frame_unit_size, obu_unit_size;
99 int ret, type, cnt = 0;
101 ffio_init_context(&pb, p->buf, p->buf_size, 0,
102 NULL, NULL, NULL, NULL);
104 ret = leb(&pb, &temporal_unit_size);
108 ret = leb(&pb, &frame_unit_size);
109 if (ret < 0 || ((int64_t)frame_unit_size + ret) > temporal_unit_size)
112 temporal_unit_size -= ret;
113 ret = leb(&pb, &obu_unit_size);
114 if (ret < 0 || ((int64_t)obu_unit_size + ret) >= frame_unit_size)
118 temporal_unit_size -= obu_unit_size + ret;
119 frame_unit_size -= obu_unit_size + ret;
121 avio_skip(&pb, obu_unit_size);
122 if (pb.eof_reached || pb.error)
125 // Check that the first OBU is a Temporal Delimiter.
126 ret = read_obu(p->buf + cnt, FFMIN(p->buf_size - cnt, obu_unit_size), &obu_size, &type);
127 if (ret < 0 || type != AV1_OBU_TEMPORAL_DELIMITER || obu_size > 0)
129 cnt += obu_unit_size;
132 ret = leb(&pb, &obu_unit_size);
133 if (ret < 0 || ((int64_t)obu_unit_size + ret) > frame_unit_size)
137 avio_skip(&pb, obu_unit_size);
138 if (pb.eof_reached || pb.error)
141 ret = read_obu(p->buf + cnt, FFMIN(p->buf_size - cnt, obu_unit_size), &obu_size, &type);
144 cnt += obu_unit_size;
146 ret = get_score(type, &seq);
150 temporal_unit_size -= obu_unit_size + ret;
151 frame_unit_size -= obu_unit_size + ret;
152 } while (frame_unit_size);
157 static int read_header(AVFormatContext *s, const AVRational *framerate, AVBSFContext **bsf, void *logctx)
159 const AVBitStreamFilter *filter = av_bsf_get_by_name("av1_frame_merge");
164 av_log(logctx, AV_LOG_ERROR, "av1_frame_merge bitstream filter "
165 "not found. This is a bug, please report it.\n");
169 st = avformat_new_stream(s, NULL);
171 return AVERROR(ENOMEM);
173 st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
174 st->codecpar->codec_id = AV_CODEC_ID_AV1;
175 st->need_parsing = AVSTREAM_PARSE_HEADERS;
177 st->internal->avctx->framerate = *framerate;
178 // taken from rawvideo demuxers
179 avpriv_set_pts_info(st, 64, 1, 1200000);
181 ret = av_bsf_alloc(filter, bsf);
185 ret = avcodec_parameters_copy((*bsf)->par_in, st->codecpar);
191 ret = av_bsf_init(*bsf);
199 static int annexb_read_header(AVFormatContext *s)
201 AnnexBContext *c = s->priv_data;
202 return read_header(s, &c->framerate, &c->bsf, c);
205 static int annexb_read_packet(AVFormatContext *s, AVPacket *pkt)
207 AnnexBContext *c = s->priv_data;
208 uint32_t obu_unit_size;
212 if (avio_feof(s->pb)) {
213 if (c->temporal_unit_size || c->frame_unit_size)
218 if (!c->temporal_unit_size) {
219 len = leb(s->pb, &c->temporal_unit_size);
220 if (len < 0) return AVERROR_INVALIDDATA;
223 if (!c->frame_unit_size) {
224 len = leb(s->pb, &c->frame_unit_size);
225 if (len < 0 || ((int64_t)c->frame_unit_size + len) > c->temporal_unit_size)
226 return AVERROR_INVALIDDATA;
227 c->temporal_unit_size -= len;
230 len = leb(s->pb, &obu_unit_size);
231 if (len < 0 || ((int64_t)obu_unit_size + len) > c->frame_unit_size)
232 return AVERROR_INVALIDDATA;
234 ret = av_get_packet(s->pb, pkt, obu_unit_size);
237 if (ret != obu_unit_size)
240 c->temporal_unit_size -= obu_unit_size + len;
241 c->frame_unit_size -= obu_unit_size + len;
244 ret = av_bsf_send_packet(c->bsf, pkt);
246 av_log(s, AV_LOG_ERROR, "Failed to send packet to "
247 "av1_frame_merge filter\n");
251 ret = av_bsf_receive_packet(c->bsf, pkt);
252 if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
253 av_log(s, AV_LOG_ERROR, "av1_frame_merge filter failed to "
254 "send output packet\n");
256 if (ret == AVERROR(EAGAIN))
262 static int annexb_read_close(AVFormatContext *s)
264 AnnexBContext *c = s->priv_data;
266 av_bsf_free(&c->bsf);
270 typedef struct ObuContext {
271 const AVClass *class;
273 AVRational framerate;
277 //For low overhead obu, we can't foresee the obu size before we parsed the header.
278 //So, we can't use parse_obu_header here, since it will check size <= buf_size
279 //see c27c7b49dc for more details
280 static int read_obu_with_size(const uint8_t *buf, int buf_size, int64_t *obu_size, int *type)
283 int ret, extension_flag, start_pos;
286 ret = init_get_bits8(&gb, buf, FFMIN(buf_size, MAX_OBU_HEADER_SIZE));
290 if (get_bits1(&gb) != 0) // obu_forbidden_bit
291 return AVERROR_INVALIDDATA;
293 *type = get_bits(&gb, 4);
294 extension_flag = get_bits1(&gb);
295 if (!get_bits1(&gb)) // has_size_flag
296 return AVERROR_INVALIDDATA;
297 skip_bits1(&gb); // obu_reserved_1bit
299 if (extension_flag) {
300 get_bits(&gb, 3); // temporal_id
301 get_bits(&gb, 2); // spatial_id
302 skip_bits(&gb, 3); // extension_header_reserved_3bits
305 *obu_size = leb128(&gb);
306 if (*obu_size > INT_MAX)
307 return AVERROR_INVALIDDATA;
309 if (get_bits_left(&gb) < 0)
310 return AVERROR_INVALIDDATA;
312 start_pos = get_bits_count(&gb) / 8;
314 size = *obu_size + start_pos;
316 return AVERROR_INVALIDDATA;
320 static int obu_probe(const AVProbeData *p)
326 // Check that the first OBU is a Temporal Delimiter.
327 cnt = read_obu_with_size(p->buf, p->buf_size, &obu_size, &type);
328 if (cnt < 0 || type != AV1_OBU_TEMPORAL_DELIMITER || obu_size != 0)
332 ret = read_obu_with_size(p->buf + cnt, p->buf_size - cnt, &obu_size, &type);
333 if (ret < 0 || obu_size <= 0)
337 ret = get_score(type, &seq);
344 static int obu_read_header(AVFormatContext *s)
346 ObuContext *c = s->priv_data;
347 c->fifo = av_fifo_alloc(MAX_OBU_HEADER_SIZE);
349 return AVERROR(ENOMEM);
350 return read_header(s, &c->framerate, &c->bsf, c);
353 static int obu_prefetch(AVFormatContext *s, uint8_t* dest)
355 ObuContext *c = s->priv_data;
356 int size = av_fifo_space(c->fifo);
357 av_fifo_generic_write(c->fifo, s->pb, size,
358 (int (*)(void*, void*, int))avio_read);
359 size = av_fifo_size(c->fifo);
361 av_fifo_generic_peek(c->fifo, dest, size, NULL);
366 static int obu_read_data(AVFormatContext *s, AVPacket *pkt, int len)
369 ObuContext *c = s->priv_data;
370 int ret = av_new_packet(pkt, len);
372 av_log(c, AV_LOG_ERROR, "Failed to allocate packet for obu\n");
375 size = FFMIN(av_fifo_size(c->fifo), len);
376 av_fifo_generic_read(c->fifo, pkt->data, size, NULL);
379 ret = avio_read(s->pb, pkt->data + size, left);
381 av_log(c, AV_LOG_ERROR, "Failed to read %d frome file\n", left);
388 static int obu_get_packet(AVFormatContext *s, AVPacket *pkt)
390 ObuContext *c = s->priv_data;
393 uint8_t header[MAX_OBU_HEADER_SIZE];
395 ret = obu_prefetch(s, header);
399 ret = read_obu_with_size(header, ret, &obu_size, &type);
401 av_log(c, AV_LOG_ERROR, "Failed to read obu\n");
404 return obu_read_data(s, pkt, ret);
407 static int obu_read_packet(AVFormatContext *s, AVPacket *pkt)
409 ObuContext *c = s->priv_data;
413 ret = obu_get_packet(s, pkt);
416 ret = av_bsf_send_packet(c->bsf, pkt);
418 av_log(s, AV_LOG_ERROR, "Failed to send packet to "
419 "av1_frame_merge filter\n");
422 ret = av_bsf_receive_packet(c->bsf, pkt);
423 if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
424 av_log(s, AV_LOG_ERROR, "av1_frame_merge filter failed to "
425 "send output packet\n");
426 if (ret != AVERROR(EAGAIN))
433 static int obu_read_close(AVFormatContext *s)
435 ObuContext *c = s->priv_data;
437 av_fifo_freep(&c->fifo);
438 av_bsf_free(&c->bsf);
442 #define DEC AV_OPT_FLAG_DECODING_PARAM
444 #define OFFSET(x) offsetof(AnnexBContext, x)
445 static const AVOption annexb_options[] = {
446 { "framerate", "", OFFSET(framerate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, INT_MAX, DEC},
451 #define OFFSET(x) offsetof(ObuContext, x)
452 static const AVOption obu_options[] = {
453 { "framerate", "", OFFSET(framerate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, INT_MAX, DEC},
458 static const AVClass annexb_demuxer_class = {
459 .class_name = "AV1 Annex B demuxer",
460 .item_name = av_default_item_name,
461 .option = annexb_options,
462 .version = LIBAVUTIL_VERSION_INT,
465 AVInputFormat ff_av1_demuxer = {
467 .long_name = NULL_IF_CONFIG_SMALL("AV1 Annex B"),
468 .priv_data_size = sizeof(AnnexBContext),
469 .read_probe = annexb_probe,
470 .read_header = annexb_read_header,
471 .read_packet = annexb_read_packet,
472 .read_close = annexb_read_close,
474 .flags = AVFMT_GENERIC_INDEX,
475 .priv_class = &annexb_demuxer_class,
478 static const AVClass obu_demuxer_class = {
479 .class_name = "AV1 low overhead OBU demuxer",
480 .item_name = av_default_item_name,
481 .option = obu_options,
482 .version = LIBAVUTIL_VERSION_INT,
485 AVInputFormat ff_obu_demuxer = {
487 .long_name = NULL_IF_CONFIG_SMALL("AV1 low overhead OBU"),
488 .priv_data_size = sizeof(ObuContext),
489 .read_probe = obu_probe,
490 .read_header = obu_read_header,
491 .read_packet = obu_read_packet,
492 .read_close = obu_read_close,
494 .flags = AVFMT_GENERIC_INDEX,
495 .priv_class = &obu_demuxer_class,