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;
59 } AV1Packet;
60 
61 /**
62  * Extract an OBU from a raw bitstream.
63  *
64  * @note This function does not copy or store any bitstream data. All
65  * the pointers in the AV1OBU structure will be valid as long
66  * as the input buffer also is.
67  */
68 int ff_av1_extract_obu(AV1OBU *obu, const uint8_t *buf, int length,
69  void *logctx);
70 
71 /**
72  * Split an input packet into OBUs.
73  *
74  * @note This function does not copy or store any bitstream data. All
75  * the pointers in the AV1Packet structure will be valid as
76  * long as the input buffer also is.
77  */
79  void *logctx);
80 
81 /**
82  * Free all the allocated memory in the packet.
83  */
85 
86 static inline int64_t leb128(GetBitContext *gb) {
87  int64_t ret = 0;
88  int i;
89 
90  for (i = 0; i < 8; i++) {
91  int byte = get_bits(gb, 8);
92  ret |= (int64_t)(byte & 0x7f) << (i * 7);
93  if (!(byte & 0x80))
94  break;
95  }
96  return ret;
97 }
98 
99 static inline int parse_obu_header(const uint8_t *buf, int buf_size,
100  int64_t *obu_size, int *start_pos, int *type,
101  int *temporal_id, int *spatial_id)
102 {
104  int ret, extension_flag, has_size_flag;
105  int64_t size;
106 
107  ret = init_get_bits8(&gb, buf, FFMIN(buf_size, 2 + 8)); // OBU header fields + max leb128 length
108  if (ret < 0)
109  return ret;
110 
111  if (get_bits1(&gb) != 0) // obu_forbidden_bit
112  return AVERROR_INVALIDDATA;
113 
114  *type = get_bits(&gb, 4);
115  extension_flag = get_bits1(&gb);
116  has_size_flag = get_bits1(&gb);
117  skip_bits1(&gb); // obu_reserved_1bit
118 
119  if (extension_flag) {
120  *temporal_id = get_bits(&gb, 3);
121  *spatial_id = get_bits(&gb, 2);
122  skip_bits(&gb, 3); // extension_header_reserved_3bits
123  } else {
124  *temporal_id = *spatial_id = 0;
125  }
126 
127  *obu_size = has_size_flag ? leb128(&gb)
128  : buf_size - 1 - extension_flag;
129 
130  if (get_bits_left(&gb) < 0)
131  return AVERROR_INVALIDDATA;
132 
133  *start_pos = get_bits_count(&gb) / 8;
134 
135  size = *obu_size + *start_pos;
136 
137  if (size > buf_size)
138  return AVERROR_INVALIDDATA;
139 
140  return size;
141 }
142 
143 static inline int get_obu_bit_length(const uint8_t *buf, int size, int type)
144 {
145  int v;
146 
147  /* There are no trailing bits on these */
148  if (type == AV1_OBU_TILE_GROUP || type == AV1_OBU_FRAME) {
149  if (size > INT_MAX / 8)
150  return AVERROR(ERANGE);
151  else
152  return size * 8;
153  }
154 
155  while (size > 0 && buf[size - 1] == 0)
156  size--;
157 
158  if (!size)
159  return 0;
160 
161  v = buf[size - 1];
162 
163  if (size > INT_MAX / 8)
164  return AVERROR(ERANGE);
165  size *= 8;
166 
167  /* Remove the trailing_one_bit and following trailing zeros */
168  if (v)
169  size -= ff_ctz(v) + 1;
170 
171  return size;
172 }
173 
174 #endif /* AVCODEC_AV1_PARSE_H */
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
#define ff_ctz
Definition: intmath.h:106
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:379
const uint8_t * raw_data
Definition: av1_parse.h:43
int spatial_id
Definition: av1_parse.h:51
GLint GLenum type
Definition: opengl_enc.c:104
static AVPacket pkt
const uint8_t * data
Definition: av1_parse.h:33
uint8_t
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
int size
Size of payload.
Definition: av1_parse.h:32
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:219
bitstream reader API header.
ptrdiff_t size
Definition: opengl_enc.c:100
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:849
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:259
GetBitContext gb
GetBitContext initialized to the start of the payload.
Definition: av1_parse.h:46
GLsizei GLsizei * length
Definition: opengl_enc.c:114
#define FFMIN(a, b)
Definition: common.h:96
int temporal_id
Definition: av1_parse.h:50
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:99
int nb_obus
Definition: av1_parse.h:57
AV1OBU * obus
Definition: av1_parse.h:56
Libavcodec external API header.
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_WB32 unsigned int_TMPL AV_WB24 unsigned int_TMPL AV_WB16 unsigned int_TMPL byte
Definition: bytestream.h:87
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:677
void * buf
Definition: avisynth_c.h:766
AV1 common definitions.
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:498
static void skip_bits1(GetBitContext *s)
Definition: get_bits.h:538
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:467
int type
Definition: av1_parse.h:48
int obus_allocated
Definition: av1_parse.h:58
int size_bits
Size, in bits, of just the data, excluding the trailing_one_bit and any trailing padding.
Definition: av1_parse.h:39
An input packet split into OBUs.
Definition: av1_parse.h:55
int raw_size
Size of entire OBU, including header.
Definition: av1_parse.h:42
static int64_t leb128(GetBitContext *gb)
Definition: av1_parse.h:86
static int get_obu_bit_length(const uint8_t *buf, int size, int type)
Definition: av1_parse.h:143
void ff_av1_packet_uninit(AV1Packet *pkt)
Free all the allocated memory in the packet.
Definition: av1_parse.c:103
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