]> git.sesse.net Git - ffmpeg/blob - libavformat/av1dec.c
hwcontext_vulkan: remove plane size alignment checks when host importing
[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;
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         switch (type) {
127         case AV1_OBU_SEQUENCE_HEADER:
128             seq = 1;
129             break;
130         case AV1_OBU_FRAME:
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:
135             return 0;
136         default:
137             break;
138         }
139
140         temporal_unit_size -= obu_unit_size + ret;
141         frame_unit_size -= obu_unit_size + ret;
142     } while (frame_unit_size);
143
144     return 0;
145 }
146
147 static int annexb_read_header(AVFormatContext *s)
148 {
149     AnnexBContext *c = s->priv_data;
150     const AVBitStreamFilter *filter = av_bsf_get_by_name("av1_frame_merge");
151     AVStream *st;
152     int ret;
153
154     if (!filter) {
155         av_log(c, AV_LOG_ERROR, "av1_frame_merge bitstream filter "
156                "not found. This is a bug, please report it.\n");
157         return AVERROR_BUG;
158     }
159
160     st = avformat_new_stream(s, NULL);
161     if (!st)
162         return AVERROR(ENOMEM);
163
164     st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
165     st->codecpar->codec_id = AV_CODEC_ID_AV1;
166     st->need_parsing = AVSTREAM_PARSE_HEADERS;
167
168     st->internal->avctx->framerate = c->framerate;
169     // taken from rawvideo demuxers
170     avpriv_set_pts_info(st, 64, 1, 1200000);
171
172     ret = av_bsf_alloc(filter, &c->bsf);
173     if (ret < 0)
174         return ret;
175
176     ret = avcodec_parameters_copy(c->bsf->par_in, st->codecpar);
177     if (ret < 0) {
178         av_bsf_free(&c->bsf);
179         return ret;
180     }
181
182     ret = av_bsf_init(c->bsf);
183     if (ret < 0)
184         av_bsf_free(&c->bsf);
185
186     return ret;
187 }
188
189 static int annexb_read_packet(AVFormatContext *s, AVPacket *pkt)
190 {
191     AnnexBContext *c = s->priv_data;
192     uint32_t obu_unit_size;
193     int ret, len;
194
195 retry:
196     if (avio_feof(s->pb)) {
197         if (c->temporal_unit_size || c->frame_unit_size)
198             return AVERROR(EIO);
199         goto end;
200     }
201
202     if (!c->temporal_unit_size) {
203         len = leb(s->pb, &c->temporal_unit_size);
204         if (len < 0) return AVERROR_INVALIDDATA;
205     }
206
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;
212     }
213
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;
217
218     ret = av_get_packet(s->pb, pkt, obu_unit_size);
219     if (ret < 0)
220         return ret;
221     if (ret != obu_unit_size)
222         return AVERROR(EIO);
223
224     c->temporal_unit_size -= obu_unit_size + len;
225     c->frame_unit_size -= obu_unit_size + len;
226
227 end:
228     ret = av_bsf_send_packet(c->bsf, pkt);
229     if (ret < 0) {
230         av_log(s, AV_LOG_ERROR, "Failed to send packet to "
231                                 "av1_frame_merge filter\n");
232         return ret;
233     }
234
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");
239
240     if (ret == AVERROR(EAGAIN))
241         goto retry;
242
243     return ret;
244 }
245
246 static int annexb_read_close(AVFormatContext *s)
247 {
248     AnnexBContext *c = s->priv_data;
249
250     av_bsf_free(&c->bsf);
251     return 0;
252 }
253
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},
258     { NULL },
259 };
260
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,
266 };
267
268 AVInputFormat ff_av1_demuxer = {
269     .name           = "av1",
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,
276     .extensions     = "obu",
277     .flags          = AVFMT_GENERIC_INDEX,
278     .priv_class     = &annexb_demuxer_class,
279 };