FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
cbs.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 
19 #include <string.h>
20 
21 #include "config.h"
22 
23 #include "libavutil/avassert.h"
24 #include "libavutil/common.h"
25 
26 #include "cbs.h"
27 #include "cbs_internal.h"
28 
29 
31 #if CONFIG_CBS_H264
33 #endif
34 #if CONFIG_CBS_H265
36 #endif
37 #if CONFIG_CBS_MPEG2
39 #endif
40 };
41 
43  enum AVCodecID codec_id, void *log_ctx)
44 {
46  const CodedBitstreamType *type;
47  int i;
48 
49  type = NULL;
50  for (i = 0; i < FF_ARRAY_ELEMS(cbs_type_table); i++) {
51  if (cbs_type_table[i]->codec_id == codec_id) {
52  type = cbs_type_table[i];
53  break;
54  }
55  }
56  if (!type)
57  return AVERROR(EINVAL);
58 
59  ctx = av_mallocz(sizeof(*ctx));
60  if (!ctx)
61  return AVERROR(ENOMEM);
62 
63  ctx->log_ctx = log_ctx;
64  ctx->codec = type;
65 
67  if (!ctx->priv_data) {
68  av_freep(&ctx);
69  return AVERROR(ENOMEM);
70  }
71 
73 
74  ctx->trace_enable = 0;
76 
77  *ctx_ptr = ctx;
78  return 0;
79 }
80 
82 {
83  CodedBitstreamContext *ctx = *ctx_ptr;
84 
85  if (!ctx)
86  return;
87 
88  if (ctx->codec && ctx->codec->close)
89  ctx->codec->close(ctx);
90 
91  av_freep(&ctx->priv_data);
92  av_freep(ctx_ptr);
93 }
94 
96  CodedBitstreamUnit *unit)
97 {
98  if (ctx->codec->free_unit && unit->content && !unit->content_external)
99  ctx->codec->free_unit(unit);
100 
101  av_freep(&unit->data);
102  unit->data_size = 0;
103  unit->data_bit_padding = 0;
104 }
105 
108 {
109  int i;
110 
111  for (i = 0; i < frag->nb_units; i++)
112  cbs_unit_uninit(ctx, &frag->units[i]);
113  av_freep(&frag->units);
114  frag->nb_units = 0;
115 
116  av_freep(&frag->data);
117  frag->data_size = 0;
118  frag->data_bit_padding = 0;
119 }
120 
123 {
124  int err, i, j;
125 
126  for (i = 0; i < frag->nb_units; i++) {
127  if (ctx->decompose_unit_types) {
128  for (j = 0; j < ctx->nb_decompose_unit_types; j++) {
129  if (ctx->decompose_unit_types[j] == frag->units[i].type)
130  break;
131  }
132  if (j >= ctx->nb_decompose_unit_types)
133  continue;
134  }
135 
136  err = ctx->codec->read_unit(ctx, &frag->units[i]);
137  if (err == AVERROR(ENOSYS)) {
139  "Decomposition unimplemented for unit %d "
140  "(type %"PRIu32").\n", i, frag->units[i].type);
141  } else if (err < 0) {
142  av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to read unit %d "
143  "(type %"PRIu32").\n", i, frag->units[i].type);
144  return err;
145  }
146  }
147 
148  return 0;
149 }
150 
153  const AVCodecParameters *par)
154 {
155  int err;
156 
157  memset(frag, 0, sizeof(*frag));
158 
159  frag->data = par->extradata;
160  frag->data_size = par->extradata_size;
161 
162  err = ctx->codec->split_fragment(ctx, frag, 1);
163  if (err < 0)
164  return err;
165 
166  frag->data = NULL;
167  frag->data_size = 0;
168 
169  return cbs_read_fragment_content(ctx, frag);
170 }
171 
174  const AVPacket *pkt)
175 {
176  int err;
177 
178  memset(frag, 0, sizeof(*frag));
179 
180  frag->data = pkt->data;
181  frag->data_size = pkt->size;
182 
183  err = ctx->codec->split_fragment(ctx, frag, 0);
184  if (err < 0)
185  return err;
186 
187  frag->data = NULL;
188  frag->data_size = 0;
189 
190  return cbs_read_fragment_content(ctx, frag);
191 }
192 
195  const uint8_t *data, size_t size)
196 {
197  int err;
198 
199  memset(frag, 0, sizeof(*frag));
200 
201  // (We won't write to this during split.)
202  frag->data = (uint8_t*)data;
203  frag->data_size = size;
204 
205  err = ctx->codec->split_fragment(ctx, frag, 0);
206  if (err < 0)
207  return err;
208 
209  frag->data = NULL;
210  frag->data_size = 0;
211 
212  return cbs_read_fragment_content(ctx, frag);
213 }
214 
215 
218 {
219  int err, i;
220 
221  for (i = 0; i < frag->nb_units; i++) {
222  if (!frag->units[i].content)
223  continue;
224 
225  err = ctx->codec->write_unit(ctx, &frag->units[i]);
226  if (err < 0) {
227  av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to write unit %d "
228  "(type %"PRIu32").\n", i, frag->units[i].type);
229  return err;
230  }
231  }
232 
233  err = ctx->codec->assemble_fragment(ctx, frag);
234  if (err < 0) {
235  av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to assemble fragment.\n");
236  return err;
237  }
238 
239  return 0;
240 }
241 
243  AVCodecParameters *par,
245 {
246  int err;
247 
248  err = ff_cbs_write_fragment_data(ctx, frag);
249  if (err < 0)
250  return err;
251 
252  av_freep(&par->extradata);
253 
254  par->extradata = av_malloc(frag->data_size +
256  if (!par->extradata)
257  return AVERROR(ENOMEM);
258 
259  memcpy(par->extradata, frag->data, frag->data_size);
260  memset(par->extradata + frag->data_size, 0,
262  par->extradata_size = frag->data_size;
263 
264  return 0;
265 }
266 
268  AVPacket *pkt,
270 {
271  int err;
272 
273  err = ff_cbs_write_fragment_data(ctx, frag);
274  if (err < 0)
275  return err;
276 
277  err = av_new_packet(pkt, frag->data_size);
278  if (err < 0)
279  return err;
280 
281  memcpy(pkt->data, frag->data, frag->data_size);
282  pkt->size = frag->data_size;
283 
284  return 0;
285 }
286 
287 
289  const char *name)
290 {
291  if (!ctx->trace_enable)
292  return;
293 
294  av_log(ctx->log_ctx, ctx->trace_level, "%s\n", name);
295 }
296 
298  const char *name, const char *bits,
299  int64_t value)
300 {
301  size_t name_len, bits_len;
302  int pad;
303 
304  if (!ctx->trace_enable)
305  return;
306 
307  av_assert0(value >= INT_MIN && value <= UINT32_MAX);
308 
309  name_len = strlen(name);
310  bits_len = strlen(bits);
311 
312  if (name_len + bits_len > 60)
313  pad = bits_len + 2;
314  else
315  pad = 61 - name_len;
316 
317  av_log(ctx->log_ctx, ctx->trace_level, "%-10d %s%*s = %"PRId64"\n",
318  position, name, pad, bits, value);
319 }
320 
322  int width, const char *name, uint32_t *write_to,
323  uint32_t range_min, uint32_t range_max)
324 {
325  uint32_t value;
326  int position;
327 
328  av_assert0(width > 0 && width <= 32);
329 
330  if (get_bits_left(gbc) < width) {
331  av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid value at "
332  "%s: bitstream ended.\n", name);
333  return AVERROR_INVALIDDATA;
334  }
335 
336  if (ctx->trace_enable)
337  position = get_bits_count(gbc);
338 
339  value = get_bits_long(gbc, width);
340 
341  if (ctx->trace_enable) {
342  char bits[33];
343  int i;
344  for (i = 0; i < width; i++)
345  bits[i] = value >> (width - i - 1) & 1 ? '1' : '0';
346  bits[i] = 0;
347 
348  ff_cbs_trace_syntax_element(ctx, position, name, bits, value);
349  }
350 
351  if (value < range_min || value > range_max) {
352  av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
353  "%"PRIu32", but must be in [%"PRIu32",%"PRIu32"].\n",
354  name, value, range_min, range_max);
355  return AVERROR_INVALIDDATA;
356  }
357 
358  *write_to = value;
359  return 0;
360 }
361 
363  int width, const char *name, uint32_t value,
364  uint32_t range_min, uint32_t range_max)
365 {
366  av_assert0(width > 0 && width <= 32);
367 
368  if (value < range_min || value > range_max) {
369  av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
370  "%"PRIu32", but must be in [%"PRIu32",%"PRIu32"].\n",
371  name, value, range_min, range_max);
372  return AVERROR_INVALIDDATA;
373  }
374 
375  if (put_bits_left(pbc) < width)
376  return AVERROR(ENOSPC);
377 
378  if (ctx->trace_enable) {
379  char bits[33];
380  int i;
381  for (i = 0; i < width; i++)
382  bits[i] = value >> (width - i - 1) & 1 ? '1' : '0';
383  bits[i] = 0;
384 
385  ff_cbs_trace_syntax_element(ctx, put_bits_count(pbc), name, bits, value);
386  }
387 
388  if (width < 32)
389  put_bits(pbc, width, value);
390  else
391  put_bits32(pbc, value);
392 
393  return 0;
394 }
395 
396 
399  int position)
400 {
401  CodedBitstreamUnit *units;
402 
403  units = av_malloc_array(frag->nb_units + 1, sizeof(*units));
404  if (!units)
405  return AVERROR(ENOMEM);
406 
407  if (position > 0)
408  memcpy(units, frag->units, position * sizeof(*units));
409  if (position < frag->nb_units)
410  memcpy(units + position + 1, frag->units + position,
411  (frag->nb_units - position) * sizeof(*units));
412 
413  memset(units + position, 0, sizeof(*units));
414 
415  av_freep(&frag->units);
416  frag->units = units;
417  ++frag->nb_units;
418 
419  return 0;
420 }
421 
424  int position,
426  void *content)
427 {
428  int err;
429 
430  if (position == -1)
431  position = frag->nb_units;
432  av_assert0(position >= 0 && position <= frag->nb_units);
433 
434  err = cbs_insert_unit(ctx, frag, position);
435  if (err < 0)
436  return err;
437 
438  frag->units[position].type = type;
439  frag->units[position].content = content;
440  frag->units[position].content_external = 1;
441 
442  return 0;
443 }
444 
447  int position,
449  uint8_t *data, size_t data_size)
450 {
451  int err;
452 
453  if (position == -1)
454  position = frag->nb_units;
455  av_assert0(position >= 0 && position <= frag->nb_units);
456 
457  err = cbs_insert_unit(ctx, frag, position);
458  if (err < 0)
459  return err;
460 
461  frag->units[position].type = type;
462  frag->units[position].data = data;
463  frag->units[position].data_size = data_size;
464 
465  return 0;
466 }
467 
470  int position)
471 {
472  if (position < 0 || position >= frag->nb_units)
473  return AVERROR(EINVAL);
474 
475  cbs_unit_uninit(ctx, &frag->units[position]);
476 
477  --frag->nb_units;
478 
479  if (frag->nb_units == 0) {
480  av_freep(&frag->units);
481 
482  } else {
483  memmove(frag->units + position,
484  frag->units + position + 1,
485  (frag->nb_units - position) * sizeof(*frag->units));
486 
487  // Don't bother reallocating the unit array.
488  }
489 
490  return 0;
491 }
static void av_unused put_bits32(PutBitContext *s, uint32_t value)
Write exactly 32 bits into a bitstream.
Definition: put_bits.h:250
#define NULL
Definition: coverity.c:32
int nb_units
Number of units in this fragment.
Definition: cbs.h:133
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition: j2kenc.c:207
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
int ff_cbs_write_packet(CodedBitstreamContext *ctx, AVPacket *pkt, CodedBitstreamFragment *frag)
Write the bitstream of a fragment to a packet.
Definition: cbs.c:267
static void cbs_unit_uninit(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit)
Definition: cbs.c:95
int ff_cbs_init(CodedBitstreamContext **ctx_ptr, enum AVCodecID codec_id, void *log_ctx)
Create and initialise a new context for the given codec.
Definition: cbs.c:42
CodedBitstreamUnitType type
Codec-specific type of this unit.
Definition: cbs.h:65
int size
Definition: avcodec.h:1401
void ff_cbs_trace_header(CodedBitstreamContext *ctx, const char *name)
Definition: cbs.c:288
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:236
static AVPacket pkt
This struct describes the properties of an encoded stream.
Definition: avcodec.h:3804
int ff_cbs_insert_unit_data(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, int position, CodedBitstreamUnitType type, uint8_t *data, size_t data_size)
Insert a new unit into a fragment with the given data bitstream.
Definition: cbs.c:445
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
int trace_enable
Enable trace output during read/write operations.
Definition: cbs.h:183
uint8_t
#define av_malloc(s)
#define AV_LOG_TRACE
Extremely verbose debugging, useful for libav* development.
Definition: log.h:202
uint32_t CodedBitstreamUnitType
The codec-specific type of a bitstream unit.
Definition: cbs.h:41
size_t data_bit_padding
The number of bits which should be ignored in the final byte.
Definition: cbs.h:83
static const CodedBitstreamType * cbs_type_table[]
Definition: cbs.c:30
int ff_cbs_write_unsigned(CodedBitstreamContext *ctx, PutBitContext *pbc, int width, const char *name, uint32_t value, uint32_t range_min, uint32_t range_max)
Definition: cbs.c:362
void(* close)(CodedBitstreamContext *ctx)
Definition: cbs_internal.h:60
static int cbs_read_fragment_content(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag)
Definition: cbs.c:121
int ff_cbs_read_packet(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, const AVPacket *pkt)
Read the data bitstream from a packet into a fragment, then split into units and decompose.
Definition: cbs.c:172
uint8_t * data
Definition: avcodec.h:1400
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:199
Coded bitstream unit structure.
Definition: cbs.h:61
ptrdiff_t size
Definition: opengl_enc.c:101
void * content
Pointer to the decomposed form of this unit.
Definition: cbs.h:92
CodedBitstreamUnit * units
Pointer to an array of units of length nb_units.
Definition: cbs.h:139
uint8_t * data
Pointer to the directly-parsable bitstream form of this unit.
Definition: cbs.h:72
int ff_cbs_insert_unit_content(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, int position, CodedBitstreamUnitType type, void *content)
Insert a new unit into a fragment with the given content.
Definition: cbs.c:422
#define av_log(a,...)
size_t data_size
The number of bytes in the bitstream.
Definition: cbs.h:121
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:86
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:587
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: avcodec.h:215
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
void ff_cbs_fragment_uninit(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag)
Free all allocated memory in a fragment.
Definition: cbs.c:106
static int put_bits_left(PutBitContext *s)
Definition: put_bits.h:93
#define AVERROR(e)
Definition: error.h:43
int(* split_fragment)(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, int header)
Definition: cbs_internal.h:38
uint16_t width
Definition: gdv.c:47
simple assert() macros that are a bit more flexible than ISO C assert().
int(* read_unit)(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit)
Definition: cbs_internal.h:44
size_t data_bit_padding
The number of bits which should be ignored in the final byte.
Definition: cbs.h:125
static int put_bits_count(PutBitContext *s)
Definition: put_bits.h:85
int extradata_size
Size of the extradata content in bytes.
Definition: avcodec.h:3830
const CodedBitstreamType ff_cbs_type_mpeg2
Definition: cbs_mpeg2.c:402
void * log_ctx
Logging context to be passed to all av_log() calls associated with this context.
Definition: cbs.h:150
const CodedBitstreamType ff_cbs_type_h264
Definition: cbs_h2645.c:1393
int content_external
Whether the content was supplied externally.
Definition: cbs.h:98
int(* assemble_fragment)(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag)
Definition: cbs_internal.h:53
int ff_cbs_write_extradata(CodedBitstreamContext *ctx, AVCodecParameters *par, CodedBitstreamFragment *frag)
Write the bitstream of a fragment to the extradata in codec parameters.
Definition: cbs.c:242
void(* free_unit)(CodedBitstreamUnit *unit)
Definition: cbs_internal.h:57
GLsizei GLboolean const GLfloat * value
Definition: opengl_enc.c:109
AVFormatContext * ctx
Definition: movenc.c:48
int ff_cbs_delete_unit(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, int position)
Delete a unit from a fragment and free all memory it uses.
Definition: cbs.c:468
enum AVCodecID codec_id
Definition: vaapi_decode.c:236
uint8_t * data
Pointer to the bitstream form of this fragment.
Definition: cbs.h:114
#define FF_ARRAY_ELEMS(a)
int ff_cbs_write_fragment_data(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag)
Write the content of the fragment to its own internal buffer.
Definition: cbs.c:216
int nb_decompose_unit_types
Length of the decompose_unit_types array.
Definition: cbs.h:178
Coded bitstream fragment structure, combining one or more units.
Definition: cbs.h:108
int trace_level
Log level to use for trace output.
Definition: cbs.h:189
GLint GLenum type
Definition: opengl_enc.c:105
Context structure for coded bitstream operations.
Definition: cbs.h:145
void ff_cbs_close(CodedBitstreamContext **ctx_ptr)
Close a context and free all internal state.
Definition: cbs.c:81
static unsigned int get_bits_long(GetBitContext *s, int n)
Read 0-32 bits.
Definition: get_bits.h:346
int ff_cbs_read_extradata(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, const AVCodecParameters *par)
Read the extradata bitstream found in codec parameters into a fragment, then split into units and dec...
Definition: cbs.c:151
void ff_cbs_trace_syntax_element(CodedBitstreamContext *ctx, int position, const char *name, const char *bits, int64_t value)
Definition: cbs.c:297
void * priv_data
Internal codec-specific data.
Definition: cbs.h:166
common internal and external API header
static int cbs_insert_unit(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, int position)
Definition: cbs.c:397
CodedBitstreamUnitType * decompose_unit_types
Array of unit types which should be decomposed when reading.
Definition: cbs.h:174
const CodedBitstreamType ff_cbs_type_h265
Definition: cbs_h2645.c:1407
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:770
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition: avcodec.h:3826
#define av_freep(p)
const struct CodedBitstreamType * codec
Internal codec-specific hooks.
Definition: cbs.h:155
#define av_malloc_array(a, b)
int ff_cbs_read(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, const uint8_t *data, size_t size)
Read a bitstream from a memory region into a fragment, then split into units and decompose.
Definition: cbs.c:193
This structure stores compressed data.
Definition: avcodec.h:1377
size_t data_size
The number of bytes in the bitstream (including any padding bits in the final byte).
Definition: cbs.h:77
const char * name
Definition: opengl_enc.c:103
int(* write_unit)(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit)
Definition: cbs_internal.h:48
int ff_cbs_read_unsigned(CodedBitstreamContext *ctx, GetBitContext *gbc, int width, const char *name, uint32_t *write_to, uint32_t range_min, uint32_t range_max)
Definition: cbs.c:321