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/opt.h"
26 #include "libavcodec/av1_parse.h"
28 #include "avio_internal.h"
31 typedef struct AnnexBContext {
34 uint32_t temporal_unit_size;
35 uint32_t frame_unit_size;
39 static int leb(AVIOContext *pb, uint32_t *len) {
48 if (i <= 3 || (i == 4 && bits < (1 << 4)))
49 *len |= bits << (i * 7);
51 return AVERROR_INVALIDDATA;
53 return AVERROR_INVALIDDATA;
54 if (pb->eof_reached || pb->error)
55 return pb->error ? pb->error : AVERROR(EIO);
60 static int read_obu(const uint8_t *buf, int size, int64_t *obu_size, int *type)
62 int start_pos, temporal_id, spatial_id;
65 len = parse_obu_header(buf, size, obu_size, &start_pos,
66 type, &temporal_id, &spatial_id);
73 static int annexb_probe(const AVProbeData *p)
77 uint32_t temporal_unit_size, frame_unit_size, obu_unit_size;
79 int ret, type, cnt = 0;
81 ffio_init_context(&pb, p->buf, p->buf_size, 0,
82 NULL, NULL, NULL, NULL);
84 ret = leb(&pb, &temporal_unit_size);
88 ret = leb(&pb, &frame_unit_size);
89 if (ret < 0 || ((int64_t)frame_unit_size + ret) > temporal_unit_size)
92 temporal_unit_size -= ret;
93 ret = leb(&pb, &obu_unit_size);
94 if (ret < 0 || ((int64_t)obu_unit_size + ret) >= frame_unit_size)
98 temporal_unit_size -= obu_unit_size + ret;
99 frame_unit_size -= obu_unit_size + ret;
101 avio_skip(&pb, obu_unit_size);
102 if (pb.eof_reached || pb.error)
105 // Check that the first OBU is a Temporal Delimiter.
106 ret = read_obu(p->buf + cnt, FFMIN(p->buf_size - cnt, obu_unit_size), &obu_size, &type);
107 if (ret < 0 || type != AV1_OBU_TEMPORAL_DELIMITER || obu_size > 0)
109 cnt += obu_unit_size;
112 ret = leb(&pb, &obu_unit_size);
113 if (ret < 0 || ((int64_t)obu_unit_size + ret) > frame_unit_size)
117 avio_skip(&pb, obu_unit_size);
118 if (pb.eof_reached || pb.error)
121 ret = read_obu(p->buf + cnt, FFMIN(p->buf_size - cnt, obu_unit_size), &obu_size, &type);
124 cnt += obu_unit_size;
127 case AV1_OBU_SEQUENCE_HEADER:
131 case AV1_OBU_FRAME_HEADER:
132 return seq ? AVPROBE_SCORE_EXTENSION + 1 : 0;
133 case AV1_OBU_TILE_GROUP:
134 case AV1_OBU_TEMPORAL_DELIMITER:
140 temporal_unit_size -= obu_unit_size + ret;
141 frame_unit_size -= obu_unit_size + ret;
142 } while (frame_unit_size);
147 static int annexb_read_header(AVFormatContext *s)
149 AnnexBContext *c = s->priv_data;
150 const AVBitStreamFilter *filter = av_bsf_get_by_name("av1_frame_merge");
155 av_log(c, AV_LOG_ERROR, "av1_frame_merge bitstream filter "
156 "not found. This is a bug, please report it.\n");
160 st = avformat_new_stream(s, NULL);
162 return AVERROR(ENOMEM);
164 st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
165 st->codecpar->codec_id = AV_CODEC_ID_AV1;
166 st->need_parsing = AVSTREAM_PARSE_HEADERS;
168 st->internal->avctx->framerate = c->framerate;
169 // taken from rawvideo demuxers
170 avpriv_set_pts_info(st, 64, 1, 1200000);
172 ret = av_bsf_alloc(filter, &c->bsf);
176 ret = avcodec_parameters_copy(c->bsf->par_in, st->codecpar);
178 av_bsf_free(&c->bsf);
182 ret = av_bsf_init(c->bsf);
184 av_bsf_free(&c->bsf);
189 static int annexb_read_packet(AVFormatContext *s, AVPacket *pkt)
191 AnnexBContext *c = s->priv_data;
192 uint32_t obu_unit_size;
196 if (avio_feof(s->pb)) {
197 if (c->temporal_unit_size || c->frame_unit_size)
202 if (!c->temporal_unit_size) {
203 len = leb(s->pb, &c->temporal_unit_size);
204 if (len < 0) return AVERROR_INVALIDDATA;
207 if (!c->frame_unit_size) {
208 len = leb(s->pb, &c->frame_unit_size);
209 if (len < 0 || ((int64_t)c->frame_unit_size + len) > c->temporal_unit_size)
210 return AVERROR_INVALIDDATA;
211 c->temporal_unit_size -= len;
214 len = leb(s->pb, &obu_unit_size);
215 if (len < 0 || ((int64_t)obu_unit_size + len) > c->frame_unit_size)
216 return AVERROR_INVALIDDATA;
218 ret = av_get_packet(s->pb, pkt, obu_unit_size);
221 if (ret != obu_unit_size)
224 c->temporal_unit_size -= obu_unit_size + len;
225 c->frame_unit_size -= obu_unit_size + len;
228 ret = av_bsf_send_packet(c->bsf, pkt);
230 av_log(s, AV_LOG_ERROR, "Failed to send packet to "
231 "av1_frame_merge filter\n");
235 ret = av_bsf_receive_packet(c->bsf, pkt);
236 if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
237 av_log(s, AV_LOG_ERROR, "av1_frame_merge filter failed to "
238 "send output packet\n");
240 if (ret == AVERROR(EAGAIN))
246 static int annexb_read_close(AVFormatContext *s)
248 AnnexBContext *c = s->priv_data;
250 av_bsf_free(&c->bsf);
254 #define OFFSET(x) offsetof(AnnexBContext, x)
255 #define DEC AV_OPT_FLAG_DECODING_PARAM
256 static const AVOption annexb_options[] = {
257 { "framerate", "", OFFSET(framerate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, INT_MAX, DEC},
261 static const AVClass annexb_demuxer_class = {
262 .class_name = "AV1 Annex B demuxer",
263 .item_name = av_default_item_name,
264 .option = annexb_options,
265 .version = LIBAVUTIL_VERSION_INT,
268 AVInputFormat ff_av1_demuxer = {
270 .long_name = NULL_IF_CONFIG_SMALL("AV1 Annex B"),
271 .priv_data_size = sizeof(AnnexBContext),
272 .read_probe = annexb_probe,
273 .read_header = annexb_read_header,
274 .read_packet = annexb_read_packet,
275 .read_close = annexb_read_close,
277 .flags = AVFMT_GENERIC_INDEX,
278 .priv_class = &annexb_demuxer_class,