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;
78 int seq = 0, frame_header = 0;
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;
126 if (type == AV1_OBU_SEQUENCE_HEADER)
128 if (type == AV1_OBU_FRAME || type == AV1_OBU_FRAME_HEADER) {
129 if (frame_header || !seq)
134 if (type == AV1_OBU_TILE_GROUP && !frame_header)
137 temporal_unit_size -= obu_unit_size + ret;
138 frame_unit_size -= obu_unit_size + ret;
139 } while (!frame_header && frame_unit_size);
141 return frame_header ? AVPROBE_SCORE_EXTENSION + 1 : 0;
144 static int annexb_read_header(AVFormatContext *s)
146 AnnexBContext *c = s->priv_data;
147 const AVBitStreamFilter *filter = av_bsf_get_by_name("av1_frame_merge");
152 av_log(c, AV_LOG_ERROR, "av1_frame_merge bitstream filter "
153 "not found. This is a bug, please report it.\n");
157 st = avformat_new_stream(s, NULL);
159 return AVERROR(ENOMEM);
161 st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
162 st->codecpar->codec_id = AV_CODEC_ID_AV1;
163 st->need_parsing = AVSTREAM_PARSE_HEADERS;
165 st->internal->avctx->framerate = c->framerate;
166 // taken from rawvideo demuxers
167 avpriv_set_pts_info(st, 64, 1, 1200000);
169 ret = av_bsf_alloc(filter, &c->bsf);
173 ret = avcodec_parameters_copy(c->bsf->par_in, st->codecpar);
175 av_bsf_free(&c->bsf);
179 ret = av_bsf_init(c->bsf);
181 av_bsf_free(&c->bsf);
186 static int annexb_read_packet(AVFormatContext *s, AVPacket *pkt)
188 AnnexBContext *c = s->priv_data;
189 uint32_t obu_unit_size;
193 if (avio_feof(s->pb)) {
194 if (c->temporal_unit_size || c->frame_unit_size)
196 av_bsf_send_packet(c->bsf, NULL);
200 if (!c->temporal_unit_size) {
201 len = leb(s->pb, &c->temporal_unit_size);
202 if (len < 0) return AVERROR_INVALIDDATA;
205 if (!c->frame_unit_size) {
206 len = leb(s->pb, &c->frame_unit_size);
207 if (len < 0 || ((int64_t)c->frame_unit_size + len) > c->temporal_unit_size)
208 return AVERROR_INVALIDDATA;
209 c->temporal_unit_size -= len;
212 len = leb(s->pb, &obu_unit_size);
213 if (len < 0 || ((int64_t)obu_unit_size + len) > c->frame_unit_size)
214 return AVERROR_INVALIDDATA;
216 ret = av_get_packet(s->pb, pkt, obu_unit_size);
219 if (ret != obu_unit_size)
222 c->temporal_unit_size -= obu_unit_size + len;
223 c->frame_unit_size -= obu_unit_size + len;
225 ret = av_bsf_send_packet(c->bsf, pkt);
227 av_log(s, AV_LOG_ERROR, "Failed to send packet to "
228 "av1_frame_merge filter\n");
233 ret = av_bsf_receive_packet(c->bsf, pkt);
234 if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
235 av_log(s, AV_LOG_ERROR, "av1_frame_merge filter failed to "
236 "send output packet\n");
238 if (ret == AVERROR(EAGAIN))
244 static int annexb_read_close(AVFormatContext *s)
246 AnnexBContext *c = s->priv_data;
248 av_bsf_free(&c->bsf);
252 #define OFFSET(x) offsetof(AnnexBContext, x)
253 #define DEC AV_OPT_FLAG_DECODING_PARAM
254 static const AVOption annexb_options[] = {
255 { "framerate", "", OFFSET(framerate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, INT_MAX, DEC},
259 static const AVClass annexb_demuxer_class = {
260 .class_name = "AV1 Annex B demuxer",
261 .item_name = av_default_item_name,
262 .option = annexb_options,
263 .version = LIBAVUTIL_VERSION_INT,
266 AVInputFormat ff_av1_demuxer = {
268 .long_name = NULL_IF_CONFIG_SMALL("AV1 Annex B"),
269 .priv_data_size = sizeof(AnnexBContext),
270 .read_probe = annexb_probe,
271 .read_header = annexb_read_header,
272 .read_packet = annexb_read_packet,
273 .read_close = annexb_read_close,
275 .flags = AVFMT_GENERIC_INDEX,
276 .priv_class = &annexb_demuxer_class,