FFmpeg
av1_parse.h
Go to the documentation of this file.
1 /*
2  * AV1 common parsing code
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #ifndef AVCODEC_AV1_PARSE_H
22 #define AVCODEC_AV1_PARSE_H
23 
24 #include <stdint.h>
25 
26 #include "av1.h"
27 #include "avcodec.h"
28 #include "get_bits.h"
29 
30 typedef struct AV1OBU {
31  /** Size of payload */
32  int size;
33  const uint8_t *data;
34 
35  /**
36  * Size, in bits, of just the data, excluding the trailing_one_bit and
37  * any trailing padding.
38  */
39  int size_bits;
40 
41  /** Size of entire OBU, including header */
42  int raw_size;
43  const uint8_t *raw_data;
44 
45  /** GetBitContext initialized to the start of the payload */
47 
48  int type;
49 
52 } AV1OBU;
53 
54 /** An input packet split into OBUs */
55 typedef struct AV1Packet {
57  int nb_obus;
60 } AV1Packet;
61 
62 /**
63  * Extract an OBU from a raw bitstream.
64  *
65  * @note This function does not copy or store any bitstream data. All
66  * the pointers in the AV1OBU structure will be valid as long
67  * as the input buffer also is.
68  */
69 int ff_av1_extract_obu(AV1OBU *obu, const uint8_t *buf, int length,
70  void *logctx);
71 
72 /**
73  * Split an input packet into OBUs.
74  *
75  * @note This function does not copy or store any bitstream data. All
76  * the pointers in the AV1Packet structure will be valid as
77  * long as the input buffer also is.
78  */
79 int ff_av1_packet_split(AV1Packet *pkt, const uint8_t *buf, int length,
80  void *logctx);
81 
82 /**
83  * Free all the allocated memory in the packet.
84  */
86 
87 static inline int64_t leb128(GetBitContext *gb) {
88  int64_t ret = 0;
89  int i;
90 
91  for (i = 0; i < 8; i++) {
92  int byte = get_bits(gb, 8);
93  ret |= (int64_t)(byte & 0x7f) << (i * 7);
94  if (!(byte & 0x80))
95  break;
96  }
97  return ret;
98 }
99 
100 static inline int parse_obu_header(const uint8_t *buf, int buf_size,
101  int64_t *obu_size, int *start_pos, int *type,
102  int *temporal_id, int *spatial_id)
103 {
104  GetBitContext gb;
105  int ret, extension_flag, has_size_flag;
106  int64_t size;
107 
108  ret = init_get_bits8(&gb, buf, FFMIN(buf_size, 2 + 8)); // OBU header fields + max leb128 length
109  if (ret < 0)
110  return ret;
111 
112  if (get_bits1(&gb) != 0) // obu_forbidden_bit
113  return AVERROR_INVALIDDATA;
114 
115  *type = get_bits(&gb, 4);
116  extension_flag = get_bits1(&gb);
117  has_size_flag = get_bits1(&gb);
118  skip_bits1(&gb); // obu_reserved_1bit
119 
120  if (extension_flag) {
121  *temporal_id = get_bits(&gb, 3);
122  *spatial_id = get_bits(&gb, 2);
123  skip_bits(&gb, 3); // extension_header_reserved_3bits
124  } else {
125  *temporal_id = *spatial_id = 0;
126  }
127 
128  *obu_size = has_size_flag ? leb128(&gb)
129  : buf_size - 1 - extension_flag;
130 
131  if (get_bits_left(&gb) < 0)
132  return AVERROR_INVALIDDATA;
133 
134  *start_pos = get_bits_count(&gb) / 8;
135 
136  size = *obu_size + *start_pos;
137 
138  if (size > buf_size)
139  return AVERROR_INVALIDDATA;
140 
141  return size;
142 }
143 
144 static inline int get_obu_bit_length(const uint8_t *buf, int size, int type)
145 {
146  int v;
147 
148  /* There are no trailing bits on these */
149  if (type == AV1_OBU_TILE_GROUP ||
150  type == AV1_OBU_TILE_LIST ||
151  type == AV1_OBU_FRAME) {
152  if (size > INT_MAX / 8)
153  return AVERROR(ERANGE);
154  else
155  return size * 8;
156  }
157 
158  while (size > 0 && buf[size - 1] == 0)
159  size--;
160 
161  if (!size)
162  return 0;
163 
164  v = buf[size - 1];
165 
166  if (size > INT_MAX / 8)
167  return AVERROR(ERANGE);
168  size *= 8;
169 
170  /* Remove the trailing_one_bit and following trailing zeros */
171  if (v)
172  size -= ff_ctz(v) + 1;
173 
174  return size;
175 }
176 
177 #endif /* AVCODEC_AV1_PARSE_H */
get_bits_left
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:849
AVERROR
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
AV1OBU::size
int size
Size of payload.
Definition: av1_parse.h:32
ff_ctz
#define ff_ctz
Definition: intmath.h:106
leb128
static int64_t leb128(GetBitContext *gb)
Definition: av1_parse.h:87
get_bits_count
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:219
AV1OBU
Definition: av1_parse.h:30
ff_av1_extract_obu
int ff_av1_extract_obu(AV1OBU *obu, const uint8_t *buf, int length, void *logctx)
Extract an OBU from a raw bitstream.
Definition: av1_parse.c:29
AV1OBU::data
const uint8_t * data
Definition: av1_parse.h:33
skip_bits
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:467
get_bits
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:379
AV1Packet
An input packet split into OBUs.
Definition: av1_parse.h:55
GetBitContext
Definition: get_bits.h:61
type
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
Definition: writing_filters.txt:86
init_get_bits8
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:677
parse_obu_header
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
get_bits.h
AV1OBU::gb
GetBitContext gb
GetBitContext initialized to the start of the payload.
Definition: av1_parse.h:46
get_bits1
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:498
AV1_OBU_TILE_GROUP
@ AV1_OBU_TILE_GROUP
Definition: av1.h:33
AV1Packet::nb_obus
int nb_obus
Definition: av1_parse.h:57
AV1_OBU_FRAME
@ AV1_OBU_FRAME
Definition: av1.h:35
get_obu_bit_length
static int get_obu_bit_length(const uint8_t *buf, int size, int type)
Definition: av1_parse.h:144
size
int size
Definition: twinvq_data.h:11134
av1.h
FFMIN
#define FFMIN(a, b)
Definition: common.h:96
AV1OBU::raw_size
int raw_size
Size of entire OBU, including header.
Definition: av1_parse.h:42
skip_bits1
static void skip_bits1(GetBitContext *s)
Definition: get_bits.h:538
AV1OBU::size_bits
int size_bits
Size, in bits, of just the data, excluding the trailing_one_bit and any trailing padding.
Definition: av1_parse.h:39
AV1Packet::obus_allocated_size
unsigned obus_allocated_size
Definition: av1_parse.h:59
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
AV1OBU::spatial_id
int spatial_id
Definition: av1_parse.h:51
AV1OBU::raw_data
const uint8_t * raw_data
Definition: av1_parse.h:43
uint8_t
uint8_t
Definition: audio_convert.c:194
AV1OBU::temporal_id
int temporal_id
Definition: av1_parse.h:50
avcodec.h
ret
ret
Definition: filter_design.txt:187
AV1_OBU_TILE_LIST
@ AV1_OBU_TILE_LIST
Definition: av1.h:37
ff_av1_packet_split
int ff_av1_packet_split(AV1Packet *pkt, const uint8_t *buf, int length, void *logctx)
Split an input packet into OBUs.
Definition: av1_parse.c:56
pkt
static AVPacket pkt
Definition: demuxing_decoding.c:54
AV1OBU::type
int type
Definition: av1_parse.h:48
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
AV1Packet::obus
AV1OBU * obus
Definition: av1_parse.h:56
ff_av1_packet_uninit
void ff_av1_packet_uninit(AV1Packet *pkt)
Free all the allocated memory in the packet.
Definition: av1_parse.c:106
AV1Packet::obus_allocated
int obus_allocated
Definition: av1_parse.h:58