FFmpeg
Loading...
Searching...
No Matches
cbs_h2645.c
Go to the documentation of this file.
1/*
2 * This file is part of FFmpeg.
3 *
4 * FFmpeg is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * FFmpeg is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with FFmpeg; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
20#include "libavutil/avassert.h"
21#include "libavutil/mem.h"
22
23#include "bytestream.h"
24#include "cbs.h"
25#include "cbs_internal.h"
26#include "cbs_h2645.h"
27#include "h264.h"
28#include "h2645_parse.h"
29#include "vvc.h"
30
31#include "hevc/hevc.h"
32
34 int cur_pos)
35{
36 int bits_left = payload_size * 8 - cur_pos;
37 return (bits_left > 0 &&
39}
40
42 const char *name, const int *subscripts,
43 uint32_t *write_to,
44 uint32_t range_min, uint32_t range_max)
45{
46 uint32_t leading_bits, value;
47 int max_length, leading_zeroes;
48
50
51 max_length = FFMIN(get_bits_left(gbc), 32);
52
53 leading_bits = max_length ? show_bits_long(gbc, max_length) : 0;
54 if (leading_bits == 0) {
55 if (max_length >= 32) {
56 av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid ue-golomb code at "
57 "%s: more than 31 zeroes.\n", name);
59 } else {
60 av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid ue-golomb code at "
61 "%s: bitstream ended.\n", name);
63 }
64 }
65
66 leading_zeroes = max_length - 1 - av_log2(leading_bits);
67 skip_bits_long(gbc, leading_zeroes);
68
69 if (get_bits_left(gbc) < leading_zeroes + 1) {
70 av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid ue-golomb code at "
71 "%s: bitstream ended.\n", name);
73 }
74
75 value = get_bits_long(gbc, leading_zeroes + 1) - 1;
76
78
79 if (value < range_min || value > range_max) {
80 av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
81 "%"PRIu32", but must be in [%"PRIu32",%"PRIu32"].\n",
82 name, value, range_min, range_max);
84 }
85
86 *write_to = value;
87 return 0;
88}
89
91 const char *name, const int *subscripts,
92 int32_t *write_to,
93 int32_t range_min, int32_t range_max)
94{
95 uint32_t leading_bits, unsigned_value;
96 int max_length, leading_zeroes;
98
100
101 max_length = FFMIN(get_bits_left(gbc), 32);
102
103 leading_bits = max_length ? show_bits_long(gbc, max_length) : 0;
104 if (leading_bits == 0) {
105 if (max_length >= 32) {
106 av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid se-golomb code at "
107 "%s: more than 31 zeroes.\n", name);
108 return AVERROR_INVALIDDATA;
109 } else {
110 av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid se-golomb code at "
111 "%s: bitstream ended.\n", name);
112 return AVERROR_INVALIDDATA;
113 }
114 }
115
116 leading_zeroes = max_length - 1 - av_log2(leading_bits);
117 skip_bits_long(gbc, leading_zeroes);
118
119 if (get_bits_left(gbc) < leading_zeroes + 1) {
120 av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid se-golomb code at "
121 "%s: bitstream ended.\n", name);
122 return AVERROR_INVALIDDATA;
123 }
124
125 unsigned_value = get_bits_long(gbc, leading_zeroes + 1);
126
127 if (unsigned_value & 1)
128 value = -(int32_t)(unsigned_value / 2);
129 else
130 value = unsigned_value / 2;
131
133
134 if (value < range_min || value > range_max) {
135 av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
136 "%"PRId32", but must be in [%"PRId32",%"PRId32"].\n",
137 name, value, range_min, range_max);
138 return AVERROR_INVALIDDATA;
139 }
140
141 *write_to = value;
142 return 0;
143}
144
146 const char *name, const int *subscripts,
147 uint32_t value,
148 uint32_t range_min, uint32_t range_max)
149{
150 int len;
151
153
154 if (value < range_min || value > range_max) {
155 av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
156 "%"PRIu32", but must be in [%"PRIu32",%"PRIu32"].\n",
157 name, value, range_min, range_max);
158 return AVERROR_INVALIDDATA;
159 }
160 av_assert0(value != UINT32_MAX);
161
162 len = av_log2(value + 1);
163 if (put_bits_left(pbc) < 2 * len + 1)
164 return AVERROR(ENOSPC);
165
166 put_bits(pbc, len, 0);
167 if (len + 1 < 32)
168 put_bits(pbc, len + 1, value + 1);
169 else
170 put_bits32(pbc, value + 1);
171
173
174 return 0;
175}
176
178 const char *name, const int *subscripts,
180 int32_t range_min, int32_t range_max)
181{
182 int len;
183 uint32_t uvalue;
184
186
187 if (value < range_min || value > range_max) {
188 av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
189 "%"PRId32", but must be in [%"PRId32",%"PRId32"].\n",
190 name, value, range_min, range_max);
191 return AVERROR_INVALIDDATA;
192 }
193 av_assert0(value != INT32_MIN);
194
195 if (value == 0)
196 uvalue = 0;
197 else if (value > 0)
198 uvalue = 2 * (uint32_t)value - 1;
199 else
200 uvalue = 2 * (uint32_t)-value;
201
202 len = av_log2(uvalue + 1);
203 if (put_bits_left(pbc) < 2 * len + 1)
204 return AVERROR(ENOSPC);
205
206 put_bits(pbc, len, 0);
207 if (len + 1 < 32)
208 put_bits(pbc, len + 1, uvalue + 1);
209 else
210 put_bits32(pbc, uvalue + 1);
211
213
214 return 0;
215}
216
218{
219 int bits_left = get_bits_left(gbc);
220 if (bits_left > 8)
221 return 1;
222 if (bits_left == 0)
223 return 0;
225 return 1;
226 return 0;
227}
228
231 const H2645Packet *packet)
232{
233 int err, i;
234
235 for (i = 0; i < packet->nb_nals; i++) {
236 const H2645NAL *nal = &packet->nals[i];
238 size_t size = nal->size;
239 enum AVCodecID codec_id = ctx->codec->codec_id;
240
241 if (codec_id == AV_CODEC_ID_HEVC && nal->nuh_layer_id > 0 &&
242 (nal->type < HEVC_NAL_VPS || nal->type > HEVC_NAL_PPS))
243 continue;
244
245 // Remove trailing zeroes.
246 while (size > 0 && nal->data[size - 1] == 0)
247 --size;
248 if (size == 0) {
249 av_log(ctx->log_ctx, AV_LOG_VERBOSE, "Discarding empty 0 NAL unit\n");
250 continue;
251 }
252
253 ref = (nal->data == nal->raw_data) ? frag->data_ref
254 : packet->rbsp.rbsp_buffer_ref;
255
256 err = ff_cbs_append_unit_data(frag, nal->type,
257 (uint8_t*)nal->data, size, ref);
258 if (err < 0)
259 return err;
260 }
261
262 return 0;
263}
264
266 PutBitContext *pbc, const uint8_t *data,
267 size_t data_size, int data_bit_start)
268{
269 size_t rest = data_size - (data_bit_start + 7) / 8;
270 const uint8_t *pos = data + data_bit_start / 8;
271
272 av_assert0(data_bit_start >= 0 &&
273 data_size > data_bit_start / 8);
274
275 if (data_size * 8 + 8 > put_bits_left(pbc))
276 return AVERROR(ENOSPC);
277
278 if (!rest)
279 goto rbsp_stop_one_bit;
280
281 // First copy the remaining bits of the first byte
282 // The above check ensures that we do not accidentally
283 // copy beyond the rbsp_stop_one_bit.
284 if (data_bit_start % 8)
285 put_bits(pbc, 8 - data_bit_start % 8,
286 *pos++ & MAX_UINT_BITS(8 - data_bit_start % 8));
287
288 if (put_bits_count(pbc) % 8 == 0) {
289 // If the writer is aligned at this point,
290 // memcpy can be used to improve performance.
291 // This happens normally for CABAC.
292 flush_put_bits(pbc);
293 memcpy(put_bits_ptr(pbc), pos, rest);
294 skip_put_bytes(pbc, rest);
295 } else {
296 // If not, we have to copy manually.
297 // rbsp_stop_one_bit forces us to special-case
298 // the last byte.
299 uint8_t temp;
300 int i;
301
302 for (; rest > 4; rest -= 4, pos += 4)
303 put_bits32(pbc, AV_RB32(pos));
304
305 for (; rest > 1; rest--, pos++)
306 put_bits(pbc, 8, *pos);
307
308 rbsp_stop_one_bit:
309 temp = rest ? *pos : *pos & MAX_UINT_BITS(8 - data_bit_start % 8);
310
312 i = ff_ctz(*pos);
313 temp = temp >> i;
314 i = rest ? (8 - i) : (8 - i - data_bit_start % 8);
315 put_bits(pbc, i, temp);
316 if (put_bits_count(pbc) % 8)
317 put_bits(pbc, 8 - put_bits_count(pbc) % 8, 0);
318 }
319
320 return 0;
321}
322
325{
326 uint8_t *data;
327 size_t max_size, dp, sp;
328 int err, i, zero_run;
329
330 for (i = 0; i < frag->nb_units; i++) {
331 // Data should already all have been written when we get here.
332 av_assert0(frag->units[i].data);
333 }
334
335 max_size = 0;
336 for (i = 0; i < frag->nb_units; i++) {
337 // Start code + content with worst-case emulation prevention.
338 max_size += 4 + frag->units[i].data_size * 3 / 2;
339 }
340
342 if (!data)
343 return AVERROR(ENOMEM);
344
345 dp = 0;
346 for (i = 0; i < frag->nb_units; i++) {
347 CodedBitstreamUnit *unit = &frag->units[i];
348
349 if (unit->data_bit_padding > 0) {
350 if (i < frag->nb_units - 1)
351 av_log(ctx->log_ctx, AV_LOG_WARNING, "Probably invalid "
352 "unaligned padding on non-final NAL unit.\n");
353 else
355 }
356
357 if (ff_h2645_unit_requires_zero_byte(ctx->codec->codec_id, unit->type, i)) {
358 // zero_byte
359 data[dp++] = 0;
360 }
361 // start_code_prefix_one_3bytes
362 data[dp++] = 0;
363 data[dp++] = 0;
364 data[dp++] = 1;
365
366 zero_run = 0;
367 for (sp = 0; sp < unit->data_size; sp++) {
368 if (zero_run < 2) {
369 if (unit->data[sp] == 0)
370 ++zero_run;
371 else
372 zero_run = 0;
373 } else {
374 if ((unit->data[sp] & ~3) == 0) {
375 // emulation_prevention_three_byte
376 data[dp++] = 3;
377 }
378 zero_run = unit->data[sp] == 0;
379 }
380 data[dp++] = unit->data[sp];
381 }
382 }
383
384 av_assert0(dp <= max_size);
386 if (err)
387 return err;
388 memset(data + dp, 0, AV_INPUT_BUFFER_PADDING_SIZE);
389
391 NULL, NULL, 0);
392 if (!frag->data_ref) {
393 av_freep(&data);
394 return AVERROR(ENOMEM);
395 }
396
397 frag->data = data;
398 frag->data_size = dp;
399
400 return 0;
401}
int32_t
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition avassert.h:42
#define bits_left
Definition bitstream.h:116
int ff_cbs_write_ue_golomb(CodedBitstreamContext *ctx, PutBitContext *pbc, const char *name, const int *subscripts, uint32_t value, uint32_t range_min, uint32_t range_max)
Definition cbs_h2645.c:145
int ff_cbs_h2645_fragment_add_nals(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, const H2645Packet *packet)
Definition cbs_h2645.c:229
int ff_cbs_read_ue_golomb(CodedBitstreamContext *ctx, GetBitContext *gbc, const char *name, const int *subscripts, uint32_t *write_to, uint32_t range_min, uint32_t range_max)
Definition cbs_h2645.c:41
int ff_cbs_h2645_payload_extension_present(GetBitContext *gbc, uint32_t payload_size, int cur_pos)
payload_extension_present() - true if we are before the last 1-bit in the payload structure,...
Definition cbs_h2645.c:33
int ff_cbs_h2645_write_slice_data(CodedBitstreamContext *ctx, PutBitContext *pbc, const uint8_t *data, size_t data_size, int data_bit_start)
Definition cbs_h2645.c:265
int ff_cbs_h2645_read_more_rbsp_data(GetBitContext *gbc)
Definition cbs_h2645.c:217
int ff_cbs_read_se_golomb(CodedBitstreamContext *ctx, GetBitContext *gbc, const char *name, const int *subscripts, int32_t *write_to, int32_t range_min, int32_t range_max)
Definition cbs_h2645.c:90
int ff_cbs_h2645_assemble_fragment(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag)
Definition cbs_h2645.c:323
int ff_cbs_write_se_golomb(CodedBitstreamContext *ctx, PutBitContext *pbc, const char *name, const int *subscripts, int32_t value, int32_t range_min, int32_t range_max)
Definition cbs_h2645.c:177
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define CBS_TRACE_READ_END()
#define CBS_TRACE_READ_START()
#define MAX_UINT_BITS(length)
#define CBS_TRACE_WRITE_END()
#define CBS_TRACE_WRITE_START()
static int FUNC nal(CodedBitstreamContext *ctx, RWContext *rw, LCEVCRawNAL *current, int nal_unit_type)
#define NULL
Definition coverity.c:32
double value
Definition eval.c:102
static unsigned int show_bits_long(GetBitContext *s, int n)
Show 0-32 bits.
Definition get_bits.h:498
static unsigned int get_bits_long(GetBitContext *s, int n)
Read 0-32 bits.
Definition get_bits.h:424
static int get_bits_left(GetBitContext *gb)
Definition get_bits.h:688
static void skip_bits_long(GetBitContext *s, int n)
Skips the specified number of bits.
Definition get_bits.h:280
static unsigned int show_bits(GetBitContext *s, int n)
Show 1-25 bits.
Definition get_bits.h:373
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition codec_id.h:47
@ AV_CODEC_ID_HEVC
Definition codec_id.h:223
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding.
Definition defs.h:40
AVBufferRef * av_buffer_create(uint8_t *data, size_t size, void(*free)(void *opaque, uint8_t *data), void *opaque, int flags)
Create an AVBuffer from an existing array.
Definition buffer.c:55
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition log.h:216
#define AV_LOG_VERBOSE
Detailed information.
Definition log.h:226
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
#define ff_ctz
Definition intmath.h:105
int av_reallocp(void *ptr, size_t size)
Allocate, reallocate, or free a block of memory through a pointer to a pointer.
Definition mem.c:188
H.264 common definitions.
#define av_log2
Definition intmath.h:84
#define AV_RB32(p)
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition j2kenc.c:154
int ff_h2645_unit_requires_zero_byte(enum AVCodecID codec_id, unsigned int type, int nal_unit_index)
@ HEVC_NAL_PPS
Definition hevc.h:63
@ HEVC_NAL_VPS
Definition hevc.h:61
Macro definitions for various function/variable attributes.
#define FFMIN(a, b)
Definition macros.h:49
Memory handling functions.
const char data[16]
Definition mxf.c:149
#define av_realloc(p, s)
Definition ops_static.c:54
static int put_bits_count(PutBitContext *s)
Definition put_bits.h:90
static uint8_t * put_bits_ptr(PutBitContext *s)
Return the pointer to the byte where the bitstream writer will put the next bit.
Definition put_bits.h:402
static int put_bits_left(PutBitContext *s)
Definition put_bits.h:135
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition put_bits.h:153
static void skip_put_bytes(PutBitContext *s, int n)
Skip the given number of bytes.
Definition put_bits.h:411
static av_unused void put_bits32(PutBitContext *s, uint32_t value)
Write exactly 32 bits into a bitstream.
Definition put_bits.h:301
const char * name
Definition qsvenc.c:142
unsigned int pos
Definition spdifenc.c:431
A reference to a data buffer.
Definition buffer.h:82
Context structure for coded bitstream operations.
Definition cbs.h:226
Coded bitstream fragment structure, combining one or more units.
Definition cbs.h:129
CodedBitstreamUnit * units
Pointer to an array of units of length nb_units_allocated.
Definition cbs.h:175
int nb_units
Number of units in this fragment.
Definition cbs.h:160
size_t data_bit_padding
The number of bits which should be ignored in the final byte.
Definition cbs.h:146
size_t data_size
The number of bytes in the bitstream.
Definition cbs.h:142
uint8_t * data
Pointer to the bitstream form of this fragment.
Definition cbs.h:135
AVBufferRef * data_ref
A reference to the buffer containing data.
Definition cbs.h:152
Coded bitstream unit structure.
Definition cbs.h:77
uint8_t * data
Pointer to the directly-parsable bitstream form of this unit.
Definition cbs.h:88
size_t data_bit_padding
The number of bits which should be ignored in the final byte.
Definition cbs.h:99
CodedBitstreamUnitType type
Codec-specific type of this unit.
Definition cbs.h:81
size_t data_size
The number of bytes in the bitstream (including any padding bits in the final byte).
Definition cbs.h:93
H2645NAL * nals
Definition h2645_parse.h:83
H2645RBSP rbsp
Definition h2645_parse.h:84
AVBufferRef * rbsp_buffer_ref
Definition h2645_parse.h:76
#define av_freep(p)
#define av_log(a,...)
static int ref[MAX_W *MAX_W]
static AVFormatContext * ctx
Definition movenc.c:49
int size
enum AVCodecID codec_id
else temp
Definition vf_mcdeint.c:275
int len