FFmpeg: libavformat/av1dec.c Source File
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_components.h"
23
32
41
42 //return < 0 if we need more data
44 {
47 *seq = 1;
48 return -1;
54 return -1;
55 default:
56 break;
57 }
58 return 0;
59 }
60
62 {
68
71 "not found. This is a bug, please report it.\n");
73 }
74
76 if (!st)
79
83
85 // taken from rawvideo demuxers
87
91
95
99
100 return 0;
101 }
102
104 {
106
108 return 0;
109 }
110
111 #define DEC AV_OPT_FLAG_DECODING_PARAM
112 #define OFFSET(x) offsetof(AV1DemuxContext, x)
116 };
117 #undef OFFSET
118
120 .
class_name =
"AV1 Annex B/low overhead OBU demuxer",
124 };
125
126 #if CONFIG_AV1_DEMUXER
127
131 do {
138 more = byte & 0x80;
140 if (
i <= 3 || (
i == 4 &&
bits < (1 << 4)))
144 if (++
i == 8 && more)
146 } while (more);
148 }
149
150 static int read_obu(
const uint8_t *buf,
int size,
int64_t *obu_size,
int *
type)
151 {
152 int start_pos, temporal_id, spatial_id;
154
156 type, &temporal_id, &spatial_id);
159
160 return 0;
161 }
162
164 {
168 uint32_t temporal_unit_size, frame_unit_size, obu_unit_size;
169 int seq = 0;
171
173
174 ret = leb(pb, &temporal_unit_size, 1);
176 return 0;
178 ret = leb(pb, &frame_unit_size, 0);
179 if (
ret < 0 || ((
int64_t)frame_unit_size +
ret) > temporal_unit_size)
180 return 0;
182 ret = leb(pb, &obu_unit_size, 0);
183 if (
ret < 0 || ((
int64_t)obu_unit_size +
ret) >= frame_unit_size)
184 return 0;
186
187 frame_unit_size -= obu_unit_size +
ret;
188
191 return 0;
192
193 // Check that the first OBU is a Temporal Delimiter.
194 ret = read_obu(
p->buf + cnt,
FFMIN(
p->buf_size - cnt, obu_unit_size), &obu_size, &
type);
195 if (ret < 0 || type != AV1_OBU_TEMPORAL_DELIMITER || obu_size > 0)
196 return 0;
197 cnt += obu_unit_size;
198
199 do {
200 ret = leb(pb, &obu_unit_size, 0);
201 if (
ret < 0 || ((
int64_t)obu_unit_size +
ret) > frame_unit_size)
202 return 0;
204
207 return 0;
208
209 ret = read_obu(
p->buf + cnt,
FFMIN(
p->buf_size - cnt, obu_unit_size), &obu_size, &
type);
211 return 0;
212 cnt += obu_unit_size;
213
217
218 frame_unit_size -= obu_unit_size +
ret;
219 } while (frame_unit_size);
220
221 return 0;
222 }
223
225 {
227 uint32_t obu_unit_size;
230
231 retry:
233 if (
c->temporal_unit_size ||
c->frame_unit_size)
235 goto end;
236 }
237
238 if (!
c->temporal_unit_size) {
240 len = leb(
s->pb, &
c->temporal_unit_size, 1);
242 else if (
len < 0)
return len;
243 }
244
245 if (!
c->frame_unit_size) {
246 len = leb(
s->pb, &
c->frame_unit_size, 0);
249 if (((
int64_t)
c->frame_unit_size +
len) >
c->temporal_unit_size)
251 c->temporal_unit_size -=
len;
252 }
253
254 len = leb(
s->pb, &obu_unit_size, 0);
257 if (((
int64_t)obu_unit_size +
len) >
c->frame_unit_size)
259
263 if (
ret != obu_unit_size)
265
266 c->temporal_unit_size -= obu_unit_size +
len;
267 c->frame_unit_size -= obu_unit_size +
len;
268
269 end:
273 "av1_frame_merge filter\n");
275 }
276
280 goto retry;
283 "send output packet\n");
285 }
286
288
289 return 0;
290 }
291
295 .p.extensions = "obu",
304 };
305 #endif
306
307 #if CONFIG_OBU_DEMUXER
308 //For low overhead obu, we can't foresee the obu size before we parsed the header.
309 //So, we can't use parse_obu_header here, since it will check size <= buf_size
310 //see c27c7b49dc for more details
311 static int read_obu_with_size(
const uint8_t *buf,
int buf_size,
int64_t *obu_size,
int *
type)
312 {
314 int ret, extension_flag, start_pos;
316
320
321 if (
get_bits1(&gb) != 0)
// obu_forbidden_bit
323
329
330 if (extension_flag) {
333 skip_bits(&gb, 3);
// extension_header_reserved_3bits
334 }
335
337 if (*obu_size > INT_MAX)
339
342
344
345 size = *obu_size + start_pos;
349 }
350
352 {
354 int seq = 0;
356
357 // Check that the first OBU is a Temporal Delimiter.
358 cnt = read_obu_with_size(
p->buf,
p->buf_size, &obu_size, &
type);
360 return 0;
361
362 while (1) {
363 ret = read_obu_with_size(
p->buf + cnt,
p->buf_size - cnt, &obu_size, &
type);
364 if (
ret < 0 || obu_size <= 0)
365 return 0;
367
371 }
372 return 0;
373 }
374
376 {
382
388
394 }
396
401 }
402 return 0;
403 }
404
406 {
409
410 if (
s->io_repositioned) {
412 s->io_repositioned = 0;
413 }
414 while (1) {
416 /* In case of AVERROR_EOF we need to flush the BSF. Conveniently
417 * obu_get_packet() returns a blank pkt in this case which
418 * can be used to signal that the BSF should be flushed. */
424 "av1_frame_merge filter\n");
426 }
430 "send output packet\n");
432 break;
433 }
434
436 }
437
441 .p.extensions = "obu",
450 };
451 #endif
static int get_bits_left(GetBitContext *gb)
Filter the word "frame" indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
enum AVMediaType codec_type
General type of the encoded data.
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
#define AVERROR_EOF
End of file.
@ AV_OPT_TYPE_VIDEO_RATE
Underlying C type is AVRational.
static int get_bits_count(const GetBitContext *s)
AVRational avg_frame_rate
Average framerate.
int error
contains the error code or 0 if no error happened
void(* filter)(uint8_t *src, int stride, int qscale)
void ffio_init_read_context(FFIOContext *s, const uint8_t *buffer, int buffer_size)
Wrap a buffer in an AVIOContext for reading.
@ AV1_OBU_TEMPORAL_DELIMITER
void av_bsf_free(AVBSFContext **pctx)
Free a bitstream filter context and everything associated with it; write NULL into the supplied point...
The bitstream filter state.
static void skip_bits(GetBitContext *s, int n)
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
static av_cold int read_close(AVFormatContext *ctx)
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf type
static int annexb_probe(const AVProbeData *p)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
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)
void av_bsf_flush(AVBSFContext *ctx)
Reset the internal bitstream filter state.
int av_bsf_alloc(const AVBitStreamFilter *filter, AVBSFContext **pctx)
Allocate a context for a given bitstream filter.
#define FF_INFMT_FLAG_INIT_CLEANUP
For an FFInputFormat with this flag set read_close() needs to be called by the caller upon read_heade...
enum AVStreamParseType need_parsing
AVCodecParameters * codecpar
Codec parameters associated with this stream.
#define LIBAVUTIL_VERSION_INT
int av_bsf_init(AVBSFContext *ctx)
Prepare the filter for use, after all the parameters and options have been set.
Describe the class of an AVClass context structure.
int av_bsf_receive_packet(AVBSFContext *ctx, AVPacket *pkt)
Retrieve a filtered packet.
Rational number (pair of numerator and denominator).
const char * av_default_item_name(void *ptr)
Return the context name.
static unsigned int get_bits1(GetBitContext *s)
This structure contains the data a format has to probe a file.
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
int av_bsf_send_packet(AVBSFContext *ctx, AVPacket *pkt)
Submit a packet for filtering.
@ AV1_OBU_SEQUENCE_HEADER
static const uint8_t header[24]
int avio_r8(AVIOContext *s)
int ffio_ensure_seekback(AVIOContext *s, int64_t buf_size)
Ensures that the requested seekback buffer size will be available.
static void skip_bits1(GetBitContext *s)
static int read_header(FFV1Context *f, RangeCoder *c)
#define i(width, name, range_min, range_max)
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
#define AV_INPUT_BUFFER_PADDING_SIZE
uint32_t temporal_unit_size
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
int eof_reached
true if was unable to read due to error or eof
IDirect3DDxgiInterfaceAccess _COM_Outptr_ void ** p
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
static int read_probe(const AVProbeData *p)
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
This structure stores compressed data.
int64_t pos
byte position in stream, -1 if unknown
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
int avcodec_parameters_copy(AVCodecParameters *dst, const AVCodecParameters *src)
Copy the contents of src to dst.
#define MAX_OBU_HEADER_SIZE
static int64_t get_leb128(GetBitContext *gb)
Read a unsigned integer coded as a variable number of up to eight little-endian bytes,...
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
const AVBitStreamFilter * av_bsf_get_by_name(const char *name)
Generated on Tue Nov 18 2025 19:22:01 for FFmpeg by
doxygen
1.8.17