FFmpeg  4.3.9
av1dec.c
Go to the documentation of this file.
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;
35  uint32_t frame_unit_size;
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) {
128  seq = 1;
129  break;
130  case AV1_OBU_FRAME:
132  return seq ? AVPROBE_SCORE_EXTENSION + 1 : 0;
133  case AV1_OBU_TILE_GROUP:
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 
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 
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 
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 
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 
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 
269  .name = "av1",
270  .long_name = NULL_IF_CONFIG_SMALL("AV1 Annex B"),
271  .priv_data_size = sizeof(AnnexBContext),
276  .extensions = "obu",
278  .priv_class = &annexb_demuxer_class,
279 };
#define DEC
Definition: av1dec.c:255
#define NULL
Definition: coverity.c:32
const AVBitStreamFilter * av_bsf_get_by_name(const char *name)
AVRational framerate
Definition: avcodec.h:2073
Bytestream IO Context.
Definition: avio.h:161
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
int size
AVOption.
Definition: opt.h:246
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition: utils.c:4948
AVInputFormat ff_av1_demuxer
Definition: av1dec.c:268
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:60
The bitstream filter state.
Definition: bsf.h:49
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:235
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:329
static AVPacket pkt
Format I/O context.
Definition: avformat.h:1351
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:72
static void filter(int16_t *output, ptrdiff_t out_stride, int16_t *low, ptrdiff_t low_stride, int16_t *high, ptrdiff_t high_stride, int len, int clip)
Definition: cfhd.c:196
uint8_t
AVOptions.
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:92
enum AVStreamParseType need_parsing
Definition: avformat.h:1094
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:4526
static int annexb_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: av1dec.c:189
#define AVERROR_EOF
End of file.
Definition: error.h:55
static av_cold int read_close(AVFormatContext *ctx)
Definition: libcdio.c:145
int av_get_packet(AVIOContext *s, AVPacket *pkt, int size)
Allocate and read the payload of a packet and initialize its fields with default values.
Definition: utils.c:307
#define av_log(a,...)
int av_bsf_alloc(const AVBitStreamFilter *filter, AVBSFContext **pctx)
Allocate a context for a given bitstream filter.
Definition: bsf.c:91
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
#define AVERROR(e)
Definition: error.h:43
static int annexb_read_close(AVFormatContext *s)
Definition: av1dec.c:246
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:188
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:56
uint8_t bits
Definition: vp3data.h:202
int av_bsf_init(AVBSFContext *ctx)
Prepare the filter for use, after all the parameters and options have been set.
Definition: bsf.c:144
Only parse headers, do not repack.
Definition: avformat.h:796
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:616
int buf_size
Size of buf except extra allocated bytes.
Definition: avformat.h:444
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:443
#define FFMIN(a, b)
Definition: common.h:96
static int parse_obu_header(const uint8_t *buf, int buf_size, int64_t *obu_size, int *start_pos, int *type, int *temporal_id, int *spatial_id)
Definition: av1_parse.h:100
#define s(width, name)
Definition: cbs_vp9.c:257
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:545
Stream structure.
Definition: avformat.h:876
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_reading.c:42
int av_bsf_send_packet(AVBSFContext *ctx, AVPacket *pkt)
Submit a packet for filtering.
Definition: bsf.c:197
AVStreamInternal * internal
An opaque field for libavformat internal usage.
Definition: avformat.h:1231
static int annexb_read_header(AVFormatContext *s)
Definition: av1dec.c:147
AVIOContext * pb
I/O context.
Definition: avformat.h:1393
long long int64_t
Definition: coverity.c:34
int av_bsf_receive_packet(AVBSFContext *ctx, AVPacket *pkt)
Retrieve a filtered packet.
Definition: bsf.c:223
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Definition: error.h:50
Describe the class of an AVClass context structure.
Definition: log.h:67
#define AVFMT_GENERIC_INDEX
Use generic index building code.
Definition: avformat.h:463
Rational number (pair of numerator and denominator).
Definition: rational.h:58
static const AVClass annexb_demuxer_class
Definition: av1dec.c:261
cl_device_type type
offset must point to AVRational
Definition: opt.h:236
static int leb(AVIOContext *pb, uint32_t *len)
Definition: av1dec.c:39
#define AVPROBE_SCORE_EXTENSION
score for file extension
Definition: avformat.h:451
int error
contains the error code or 0 if no error happened
Definition: avio.h:245
This structure contains the data a format has to probe a file.
Definition: avformat.h:441
uint32_t frame_unit_size
Definition: av1dec.c:35
AVRational framerate
Definition: av1dec.c:36
#define flags(name, subs,...)
Definition: cbs_av1.c:576
static int read_obu(const uint8_t *buf, int size, int64_t *obu_size, int *type)
Definition: av1dec.c:60
static int read_probe(const AVProbeData *pd)
Definition: jvdec.c:55
Main libavformat public API header.
#define OFFSET(x)
Definition: av1dec.c:254
common internal and external API header
int ffio_init_context(AVIOContext *s, unsigned char *buffer, int buffer_size, int write_flag, void *opaque, int(*read_packet)(void *opaque, uint8_t *buf, int buf_size), int(*write_packet)(void *opaque, uint8_t *buf, int buf_size), int64_t(*seek)(void *opaque, int64_t offset, int whence))
Definition: aviobuf.c:76
static double c[64]
uint32_t temporal_unit_size
Definition: av1dec.c:34
int eof_reached
true if was unable to read due to error or eof
Definition: avio.h:239
int len
void * priv_data
Format private data.
Definition: avformat.h:1379
static const AVOption annexb_options[]
Definition: av1dec.c:256
AVCodecContext * avctx
The codec context used by avformat_find_stream_info, the parser, etc.
Definition: internal.h:169
AVBSFContext * bsf
Definition: av1dec.c:33
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:650
void av_bsf_free(AVBSFContext **pctx)
Free a bitstream filter context and everything associated with it; write NULL into the supplied point...
Definition: bsf.c:40
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1023
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition: aviobuf.c:356
static int annexb_probe(const AVProbeData *p)
Definition: av1dec.c:73
int avcodec_parameters_copy(AVCodecParameters *dst, const AVCodecParameters *src)
Copy the contents of src to dst.
Definition: utils.c:2118
This structure stores compressed data.
Definition: packet.h:332
AVCodecParameters * par_in
Parameters of the input stream.
Definition: bsf.h:77