FFmpeg
Loading...
Searching...
No Matches
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 <limits.h>
25#include <stdint.h>
26
27#include "libavutil/error.h"
28#include "libavutil/intmath.h"
29#include "libavutil/macros.h"
30
31#include "av1.h"
32#include "get_bits.h"
33#include "leb.h"
34
35// OBU header fields + max leb128 length
36#define MAX_OBU_HEADER_SIZE (2 + 8)
37
38typedef struct AV1OBU {
39 /** Size of payload */
40 int size;
41 const uint8_t *data;
42
43 /**
44 * Size, in bits, of just the data, excluding the trailing_one_bit and
45 * any trailing padding.
46 */
48
49 /** Size of entire OBU, including header */
51 const uint8_t *raw_data;
52
53 int type;
54
57} AV1OBU;
58
59/** An input packet split into OBUs */
66
67/**
68 * Extract an OBU from a raw bitstream.
69 *
70 * @note This function does not copy or store any bitstream data. All
71 * the pointers in the AV1OBU structure will be valid as long
72 * as the input buffer also is.
73 */
74int ff_av1_extract_obu(AV1OBU *obu, const uint8_t *buf, int length,
75 void *logctx);
76
77/**
78 * Split an input packet into OBUs.
79 *
80 * @note This function does not copy or store any bitstream data. All
81 * the pointers in the AV1Packet structure will be valid as
82 * long as the input buffer also is.
83 */
84int ff_av1_packet_split(AV1Packet *pkt, const uint8_t *buf, int length,
85 void *logctx);
86
87/**
88 * Free all the allocated memory in the packet.
89 */
91
92static inline int parse_obu_header(const uint8_t *buf, int buf_size,
93 int64_t *obu_size, int *start_pos, int *type,
94 int *temporal_id, int *spatial_id)
95{
97 int ret, extension_flag, has_size_flag;
99
100 ret = init_get_bits8(&gb, buf, FFMIN(buf_size, MAX_OBU_HEADER_SIZE));
101 if (ret < 0)
102 return ret;
103
104 if (get_bits1(&gb) != 0) // obu_forbidden_bit
105 return AVERROR_INVALIDDATA;
106
107 *type = get_bits(&gb, 4);
108 extension_flag = get_bits1(&gb);
109 has_size_flag = get_bits1(&gb);
110 skip_bits1(&gb); // obu_reserved_1bit
111
112 if (extension_flag) {
113 *temporal_id = get_bits(&gb, 3);
114 *spatial_id = get_bits(&gb, 2);
115 skip_bits(&gb, 3); // extension_header_reserved_3bits
116 } else {
117 *temporal_id = *spatial_id = 0;
118 }
119
120 *obu_size = has_size_flag ? get_leb128(&gb)
121 : buf_size - 1 - extension_flag;
122
123 if (get_bits_left(&gb) < 0)
124 return AVERROR_INVALIDDATA;
125
126 *start_pos = get_bits_count(&gb) / 8;
127
128 size = *obu_size + *start_pos;
129
130 if (size > buf_size)
131 return AVERROR_INVALIDDATA;
132
133 return size;
134}
135
136static inline int get_obu_bit_length(const uint8_t *buf, int size, int type)
137{
138 int v;
139
140 /* There are no trailing bits on these */
141 if (type == AV1_OBU_TILE_GROUP ||
143 type == AV1_OBU_FRAME) {
144 if (size > INT_MAX / 8)
145 return AVERROR(ERANGE);
146 else
147 return size * 8;
148 }
149
150 while (size > 0 && buf[size - 1] == 0)
151 size--;
152
153 if (!size)
154 return 0;
155
156 v = buf[size - 1];
157
158 if (size > INT_MAX / 8)
159 return AVERROR(ERANGE);
160 size *= 8;
161
162 /* Remove the trailing_one_bit and following trailing zeros */
163 if (v)
164 size -= ff_ctz(v) + 1;
165
166 return size;
167}
168
169AVRational ff_av1_framerate(int64_t ticks_per_frame, int64_t units_per_tick,
170 int64_t time_scale);
171
172#endif /* AVCODEC_AV1_PARSE_H */
void ff_av1_packet_uninit(AV1Packet *pkt)
Free all the allocated memory in the packet.
Definition av1_parse.c:104
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:92
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
AVRational ff_av1_framerate(int64_t ticks_per_frame, int64_t units_per_tick, int64_t time_scale)
Definition av1_parse.c:110
#define MAX_OBU_HEADER_SIZE
Definition av1_parse.h:36
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:136
long long int64_t
Definition coverity.c:34
static AVPacket * pkt
error code definitions
bitstream reader API header.
static int get_bits_left(GetBitContext *gb)
Definition get_bits.h:688
static unsigned int get_bits1(GetBitContext *s)
Definition get_bits.h:391
static void skip_bits(GetBitContext *s, int n)
Definition get_bits.h:383
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition get_bits.h:544
static int get_bits_count(const GetBitContext *s)
Definition get_bits.h:254
static void skip_bits1(GetBitContext *s)
Definition get_bits.h:416
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition get_bits.h:337
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR(e)
Definition error.h:45
#define ff_ctz
Definition intmath.h:105
cl_device_type type
leb128 handling implementations
static int64_t get_leb128(GetBitContext *gb)
Read a unsigned integer coded as a variable number of up to eight little-endian bytes,...
Definition leb.h:57
AV1 common definitions.
@ AV1_OBU_TILE_LIST
Definition av1.h:37
@ AV1_OBU_TILE_GROUP
Definition av1.h:33
@ AV1_OBU_FRAME
Definition av1.h:35
Utility Preprocessor macros.
#define FFMIN(a, b)
Definition macros.h:49
int size_bits
Size, in bits, of just the data, excluding the trailing_one_bit and any trailing padding.
Definition av1_parse.h:47
int spatial_id
Definition av1_parse.h:56
int temporal_id
Definition av1_parse.h:55
const uint8_t * data
Definition av1_parse.h:41
int type
Definition av1_parse.h:53
int raw_size
Size of entire OBU, including header.
Definition av1_parse.h:50
int size
Size of payload.
Definition av1_parse.h:40
const uint8_t * raw_data
Definition av1_parse.h:51
An input packet split into OBUs.
Definition av1_parse.h:60
int nb_obus
Definition av1_parse.h:62
AV1OBU * obus
Definition av1_parse.h:61
int obus_allocated
Definition av1_parse.h:63
unsigned obus_allocated_size
Definition av1_parse.h:64
Rational number (pair of numerator and denominator).
Definition rational.h:58
int size