]> git.sesse.net Git - ffmpeg/blob - libavformat/av1dec.c
avformat/hlsenc: free the original malloc pointer to avoid error when system function...
[ffmpeg] / libavformat / av1dec.c
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.h"
23
24 #include "libavutil/common.h"
25 #include "libavutil/opt.h"
26 #include "libavcodec/av1_parse.h"
27 #include "avformat.h"
28 #include "avio_internal.h"
29 #include "internal.h"
30
31 typedef struct AnnexBContext {
32     const AVClass *class;
33     AVBSFContext *bsf;
34     uint32_t temporal_unit_size;
35     uint32_t frame_unit_size;
36     AVRational framerate;
37 } AnnexBContext;
38
39 static int leb(AVIOContext *pb, uint32_t *len) {
40     int more, i = 0;
41     uint8_t byte;
42     *len = 0;
43     do {
44         unsigned bits;
45         byte = avio_r8(pb);
46         more = byte & 0x80;
47         bits = byte & 0x7f;
48         if (i <= 3 || (i == 4 && bits < (1 << 4)))
49             *len |= bits << (i * 7);
50         else if (bits)
51             return AVERROR_INVALIDDATA;
52         if (++i == 8 && more)
53             return AVERROR_INVALIDDATA;
54         if (pb->eof_reached || pb->error)
55             return pb->error ? pb->error : AVERROR(EIO);
56     } while (more);
57     return i;
58 }
59
60 static int read_obu(const uint8_t *buf, int size, int64_t *obu_size, int *type)
61 {
62     int start_pos, temporal_id, spatial_id;
63     int len;
64
65     len = parse_obu_header(buf, size, obu_size, &start_pos,
66                            type, &temporal_id, &spatial_id);
67     if (len < 0)
68         return len;
69
70     return 0;
71 }
72
73 static int annexb_probe(const AVProbeData *p)
74 {
75     AVIOContext pb;
76     int64_t obu_size;
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;
80
81     ffio_init_context(&pb, p->buf, p->buf_size, 0,
82                       NULL, NULL, NULL, NULL);
83
84     ret = leb(&pb, &temporal_unit_size);
85     if (ret < 0)
86         return 0;
87     cnt += ret;
88     ret = leb(&pb, &frame_unit_size);
89     if (ret < 0 || ((int64_t)frame_unit_size + ret) > temporal_unit_size)
90         return 0;
91     cnt += ret;
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)
95         return 0;
96     cnt += ret;
97
98     temporal_unit_size -= obu_unit_size + ret;
99     frame_unit_size -= obu_unit_size + ret;
100
101     avio_skip(&pb, obu_unit_size);
102     if (pb.eof_reached || pb.error)
103         return 0;
104
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)
108         return 0;
109     cnt += obu_unit_size;
110
111     do {
112         ret = leb(&pb, &obu_unit_size);
113         if (ret < 0 || ((int64_t)obu_unit_size + ret) > frame_unit_size)
114             return 0;
115         cnt += ret;
116
117         avio_skip(&pb, obu_unit_size);
118         if (pb.eof_reached || pb.error)
119             return 0;
120
121         ret = read_obu(p->buf + cnt, FFMIN(p->buf_size - cnt, obu_unit_size), &obu_size, &type);
122         if (ret < 0)
123             return 0;
124         cnt += obu_unit_size;
125
126         if (type == AV1_OBU_SEQUENCE_HEADER)
127             seq = 1;
128         if (type == AV1_OBU_FRAME || type == AV1_OBU_FRAME_HEADER) {
129             if (frame_header || !seq)
130                 return 0;
131             frame_header = 1;
132             break;
133         }
134         if (type == AV1_OBU_TILE_GROUP && !frame_header)
135             return 0;
136
137         temporal_unit_size -= obu_unit_size + ret;
138         frame_unit_size -= obu_unit_size + ret;
139     } while (!frame_header && frame_unit_size);
140
141     return frame_header ? AVPROBE_SCORE_EXTENSION + 1 : 0;
142 }
143
144 static int annexb_read_header(AVFormatContext *s)
145 {
146     AnnexBContext *c = s->priv_data;
147     const AVBitStreamFilter *filter = av_bsf_get_by_name("av1_frame_merge");
148     AVStream *st;
149     int ret;
150
151     if (!filter) {
152         av_log(c, AV_LOG_ERROR, "av1_frame_merge bitstream filter "
153                "not found. This is a bug, please report it.\n");
154         return AVERROR_BUG;
155     }
156
157     st = avformat_new_stream(s, NULL);
158     if (!st)
159         return AVERROR(ENOMEM);
160
161     st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
162     st->codecpar->codec_id = AV_CODEC_ID_AV1;
163     st->need_parsing = AVSTREAM_PARSE_HEADERS;
164
165     st->internal->avctx->framerate = c->framerate;
166     // taken from rawvideo demuxers
167     avpriv_set_pts_info(st, 64, 1, 1200000);
168
169     ret = av_bsf_alloc(filter, &c->bsf);
170     if (ret < 0)
171         return ret;
172
173     ret = avcodec_parameters_copy(c->bsf->par_in, st->codecpar);
174     if (ret < 0) {
175         av_bsf_free(&c->bsf);
176         return ret;
177     }
178
179     ret = av_bsf_init(c->bsf);
180     if (ret < 0)
181         av_bsf_free(&c->bsf);
182
183     return ret;
184 }
185
186 static int annexb_read_packet(AVFormatContext *s, AVPacket *pkt)
187 {
188     AnnexBContext *c = s->priv_data;
189     uint32_t obu_unit_size;
190     int ret, len;
191
192 retry:
193     if (avio_feof(s->pb)) {
194         if (c->temporal_unit_size || c->frame_unit_size)
195             return AVERROR(EIO);
196         av_bsf_send_packet(c->bsf, NULL);
197         goto end;
198     }
199
200     if (!c->temporal_unit_size) {
201         len = leb(s->pb, &c->temporal_unit_size);
202         if (len < 0) return AVERROR_INVALIDDATA;
203     }
204
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;
210     }
211
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;
215
216     ret = av_get_packet(s->pb, pkt, obu_unit_size);
217     if (ret < 0)
218         return ret;
219     if (ret != obu_unit_size)
220         return AVERROR(EIO);
221
222     c->temporal_unit_size -= obu_unit_size + len;
223     c->frame_unit_size -= obu_unit_size + len;
224
225     ret = av_bsf_send_packet(c->bsf, pkt);
226     if (ret < 0) {
227         av_log(s, AV_LOG_ERROR, "Failed to send packet to "
228                                 "av1_frame_merge filter\n");
229         return ret;
230     }
231
232 end:
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");
237
238     if (ret == AVERROR(EAGAIN))
239         goto retry;
240
241     return ret;
242 }
243
244 static int annexb_read_close(AVFormatContext *s)
245 {
246     AnnexBContext *c = s->priv_data;
247
248     av_bsf_free(&c->bsf);
249     return 0;
250 }
251
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},
256     { NULL },
257 };
258
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,
264 };
265
266 AVInputFormat ff_av1_demuxer = {
267     .name           = "av1",
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,
274     .extensions     = "obu",
275     .flags          = AVFMT_GENERIC_INDEX,
276     .priv_class     = &annexb_demuxer_class,
277 };