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