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 //return < 0 if we need more data
33 static int get_score(int type, int *seq)
36 case AV1_OBU_SEQUENCE_HEADER:
40 case AV1_OBU_FRAME_HEADER:
41 return *seq ? AVPROBE_SCORE_EXTENSION + 1 : 0;
42 case AV1_OBU_METADATA:
51 static int read_header(AVFormatContext *s, const AVRational *framerate, AVBSFContext **bsf, void *logctx)
53 const AVBitStreamFilter *filter = av_bsf_get_by_name("av1_frame_merge");
58 av_log(logctx, AV_LOG_ERROR, "av1_frame_merge bitstream filter "
59 "not found. This is a bug, please report it.\n");
63 st = avformat_new_stream(s, NULL);
65 return AVERROR(ENOMEM);
67 st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
68 st->codecpar->codec_id = AV_CODEC_ID_AV1;
69 st->need_parsing = AVSTREAM_PARSE_HEADERS;
71 st->internal->avctx->framerate = *framerate;
72 // taken from rawvideo demuxers
73 avpriv_set_pts_info(st, 64, 1, 1200000);
75 ret = av_bsf_alloc(filter, bsf);
79 ret = avcodec_parameters_copy((*bsf)->par_in, st->codecpar);
85 ret = av_bsf_init(*bsf);
93 #define DEC AV_OPT_FLAG_DECODING_PARAM
95 #if CONFIG_AV1_DEMUXER
96 typedef struct AnnexBContext {
99 uint32_t temporal_unit_size;
100 uint32_t frame_unit_size;
101 AVRational framerate;
104 static int leb(AVIOContext *pb, uint32_t *len) {
113 if (i <= 3 || (i == 4 && bits < (1 << 4)))
114 *len |= bits << (i * 7);
116 return AVERROR_INVALIDDATA;
117 if (++i == 8 && more)
118 return AVERROR_INVALIDDATA;
119 if (pb->eof_reached || pb->error)
120 return pb->error ? pb->error : AVERROR(EIO);
125 static int read_obu(const uint8_t *buf, int size, int64_t *obu_size, int *type)
127 int start_pos, temporal_id, spatial_id;
130 len = parse_obu_header(buf, size, obu_size, &start_pos,
131 type, &temporal_id, &spatial_id);
138 static int annexb_probe(const AVProbeData *p)
142 uint32_t temporal_unit_size, frame_unit_size, obu_unit_size;
144 int ret, type, cnt = 0;
146 ffio_init_context(&pb, p->buf, p->buf_size, 0,
147 NULL, NULL, NULL, NULL);
149 ret = leb(&pb, &temporal_unit_size);
153 ret = leb(&pb, &frame_unit_size);
154 if (ret < 0 || ((int64_t)frame_unit_size + ret) > temporal_unit_size)
157 ret = leb(&pb, &obu_unit_size);
158 if (ret < 0 || ((int64_t)obu_unit_size + ret) >= frame_unit_size)
162 frame_unit_size -= obu_unit_size + ret;
164 avio_skip(&pb, obu_unit_size);
165 if (pb.eof_reached || pb.error)
168 // Check that the first OBU is a Temporal Delimiter.
169 ret = read_obu(p->buf + cnt, FFMIN(p->buf_size - cnt, obu_unit_size), &obu_size, &type);
170 if (ret < 0 || type != AV1_OBU_TEMPORAL_DELIMITER || obu_size > 0)
172 cnt += obu_unit_size;
175 ret = leb(&pb, &obu_unit_size);
176 if (ret < 0 || ((int64_t)obu_unit_size + ret) > frame_unit_size)
180 avio_skip(&pb, obu_unit_size);
181 if (pb.eof_reached || pb.error)
184 ret = read_obu(p->buf + cnt, FFMIN(p->buf_size - cnt, obu_unit_size), &obu_size, &type);
187 cnt += obu_unit_size;
189 ret = get_score(type, &seq);
193 frame_unit_size -= obu_unit_size + ret;
194 } while (frame_unit_size);
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 #define OFFSET(x) offsetof(AnnexBContext, x)
271 static const AVOption annexb_options[] = {
272 { "framerate", "", OFFSET(framerate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, INT_MAX, DEC},
277 static const AVClass annexb_demuxer_class = {
278 .class_name = "AV1 Annex B demuxer",
279 .item_name = av_default_item_name,
280 .option = annexb_options,
281 .version = LIBAVUTIL_VERSION_INT,
284 const AVInputFormat ff_av1_demuxer = {
286 .long_name = NULL_IF_CONFIG_SMALL("AV1 Annex B"),
287 .priv_data_size = sizeof(AnnexBContext),
288 .read_probe = annexb_probe,
289 .read_header = annexb_read_header,
290 .read_packet = annexb_read_packet,
291 .read_close = annexb_read_close,
293 .flags = AVFMT_GENERIC_INDEX,
294 .priv_class = &annexb_demuxer_class,
298 #if CONFIG_OBU_DEMUXER
299 typedef struct ObuContext {
300 const AVClass *class;
302 AVRational framerate;
306 //For low overhead obu, we can't foresee the obu size before we parsed the header.
307 //So, we can't use parse_obu_header here, since it will check size <= buf_size
308 //see c27c7b49dc for more details
309 static int read_obu_with_size(const uint8_t *buf, int buf_size, int64_t *obu_size, int *type)
312 int ret, extension_flag, start_pos;
315 ret = init_get_bits8(&gb, buf, FFMIN(buf_size, MAX_OBU_HEADER_SIZE));
319 if (get_bits1(&gb) != 0) // obu_forbidden_bit
320 return AVERROR_INVALIDDATA;
322 *type = get_bits(&gb, 4);
323 extension_flag = get_bits1(&gb);
324 if (!get_bits1(&gb)) // has_size_flag
325 return AVERROR_INVALIDDATA;
326 skip_bits1(&gb); // obu_reserved_1bit
328 if (extension_flag) {
329 get_bits(&gb, 3); // temporal_id
330 get_bits(&gb, 2); // spatial_id
331 skip_bits(&gb, 3); // extension_header_reserved_3bits
334 *obu_size = leb128(&gb);
335 if (*obu_size > INT_MAX)
336 return AVERROR_INVALIDDATA;
338 if (get_bits_left(&gb) < 0)
339 return AVERROR_INVALIDDATA;
341 start_pos = get_bits_count(&gb) / 8;
343 size = *obu_size + start_pos;
345 return AVERROR_INVALIDDATA;
349 static int obu_probe(const AVProbeData *p)
355 // Check that the first OBU is a Temporal Delimiter.
356 cnt = read_obu_with_size(p->buf, p->buf_size, &obu_size, &type);
357 if (cnt < 0 || type != AV1_OBU_TEMPORAL_DELIMITER || obu_size != 0)
361 ret = read_obu_with_size(p->buf + cnt, p->buf_size - cnt, &obu_size, &type);
362 if (ret < 0 || obu_size <= 0)
364 cnt += FFMIN(ret, p->buf_size - cnt);
366 ret = get_score(type, &seq);
373 static int obu_read_header(AVFormatContext *s)
375 ObuContext *c = s->priv_data;
376 c->fifo = av_fifo_alloc(MAX_OBU_HEADER_SIZE);
378 return AVERROR(ENOMEM);
379 return read_header(s, &c->framerate, &c->bsf, c);
382 static int obu_get_packet(AVFormatContext *s, AVPacket *pkt)
384 ObuContext *c = s->priv_data;
385 uint8_t header[MAX_OBU_HEADER_SIZE + AV_INPUT_BUFFER_PADDING_SIZE];
387 int size = av_fifo_space(c->fifo);
390 av_fifo_generic_write(c->fifo, s->pb, size,
391 (int (*)(void*, void*, int))avio_read);
392 size = av_fifo_size(c->fifo);
396 av_fifo_generic_peek(c->fifo, header, size, NULL);
398 len = read_obu_with_size(header, size, &obu_size, &type);
400 av_log(c, AV_LOG_ERROR, "Failed to read obu\n");
404 ret = av_new_packet(pkt, len);
406 av_log(c, AV_LOG_ERROR, "Failed to allocate packet for obu\n");
409 size = FFMIN(size, len);
410 av_fifo_generic_read(c->fifo, pkt->data, size, NULL);
413 ret = avio_read(s->pb, pkt->data + size, len);
415 av_log(c, AV_LOG_ERROR, "Failed to read %d frome file\n", len);
416 return ret < 0 ? ret : AVERROR_INVALIDDATA;
422 static int obu_read_packet(AVFormatContext *s, AVPacket *pkt)
424 ObuContext *c = s->priv_data;
428 ret = obu_get_packet(s, pkt);
431 ret = av_bsf_send_packet(c->bsf, pkt);
433 av_log(s, AV_LOG_ERROR, "Failed to send packet to "
434 "av1_frame_merge filter\n");
437 ret = av_bsf_receive_packet(c->bsf, pkt);
438 if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
439 av_log(s, AV_LOG_ERROR, "av1_frame_merge filter failed to "
440 "send output packet\n");
441 if (ret != AVERROR(EAGAIN))
448 static int obu_read_close(AVFormatContext *s)
450 ObuContext *c = s->priv_data;
452 av_fifo_freep(&c->fifo);
453 av_bsf_free(&c->bsf);
457 #define OFFSET(x) offsetof(ObuContext, x)
458 static const AVOption obu_options[] = {
459 { "framerate", "", OFFSET(framerate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, INT_MAX, DEC},
464 static const AVClass obu_demuxer_class = {
465 .class_name = "AV1 low overhead OBU demuxer",
466 .item_name = av_default_item_name,
467 .option = obu_options,
468 .version = LIBAVUTIL_VERSION_INT,
471 const AVInputFormat ff_obu_demuxer = {
473 .long_name = NULL_IF_CONFIG_SMALL("AV1 low overhead OBU"),
474 .priv_data_size = sizeof(ObuContext),
475 .read_probe = obu_probe,
476 .read_header = obu_read_header,
477 .read_packet = obu_read_packet,
478 .read_close = obu_read_close,
480 .flags = AVFMT_GENERIC_INDEX,
481 .priv_class = &obu_demuxer_class,