FFmpeg
cbs_mpeg2.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 "libavutil/avassert.h"
20 
21 #include "cbs.h"
22 #include "cbs_internal.h"
23 #include "cbs_mpeg2.h"
24 #include "internal.h"
25 
26 
27 #define HEADER(name) do { \
28  ff_cbs_trace_header(ctx, name); \
29  } while (0)
30 
31 #define CHECK(call) do { \
32  err = (call); \
33  if (err < 0) \
34  return err; \
35  } while (0)
36 
37 #define FUNC_NAME(rw, codec, name) cbs_ ## codec ## _ ## rw ## _ ## name
38 #define FUNC_MPEG2(rw, name) FUNC_NAME(rw, mpeg2, name)
39 #define FUNC(name) FUNC_MPEG2(READWRITE, name)
40 
41 #define SUBSCRIPTS(subs, ...) (subs > 0 ? ((int[subs + 1]){ subs, __VA_ARGS__ }) : NULL)
42 
43 #define ui(width, name) \
44  xui(width, name, current->name, 0, MAX_UINT_BITS(width), 0)
45 #define uir(width, name) \
46  xui(width, name, current->name, 1, MAX_UINT_BITS(width), 0)
47 #define uis(width, name, subs, ...) \
48  xui(width, name, current->name, 0, MAX_UINT_BITS(width), subs, __VA_ARGS__)
49 #define uirs(width, name, subs, ...) \
50  xui(width, name, current->name, 1, MAX_UINT_BITS(width), subs, __VA_ARGS__)
51 #define sis(width, name, subs, ...) \
52  xsi(width, name, current->name, subs, __VA_ARGS__)
53 
54 
55 #define READ
56 #define READWRITE read
57 #define RWContext GetBitContext
58 
59 #define xui(width, name, var, range_min, range_max, subs, ...) do { \
60  uint32_t value = 0; \
61  CHECK(ff_cbs_read_unsigned(ctx, rw, width, #name, \
62  SUBSCRIPTS(subs, __VA_ARGS__), \
63  &value, range_min, range_max)); \
64  var = value; \
65  } while (0)
66 
67 #define xsi(width, name, var, subs, ...) do { \
68  int32_t value; \
69  CHECK(ff_cbs_read_signed(ctx, rw, width, #name, \
70  SUBSCRIPTS(subs, __VA_ARGS__), &value, \
71  MIN_INT_BITS(width), \
72  MAX_INT_BITS(width))); \
73  var = value; \
74  } while (0)
75 
76 #define marker_bit() do { \
77  av_unused uint32_t one; \
78  CHECK(ff_cbs_read_unsigned(ctx, rw, 1, "marker_bit", NULL, &one, 1, 1)); \
79  } while (0)
80 
81 #define nextbits(width, compare, var) \
82  (get_bits_left(rw) >= width && \
83  (var = show_bits(rw, width)) == (compare))
84 
86 
87 #undef READ
88 #undef READWRITE
89 #undef RWContext
90 #undef xui
91 #undef xsi
92 #undef marker_bit
93 #undef nextbits
94 
95 
96 #define WRITE
97 #define READWRITE write
98 #define RWContext PutBitContext
99 
100 #define xui(width, name, var, range_min, range_max, subs, ...) do { \
101  CHECK(ff_cbs_write_unsigned(ctx, rw, width, #name, \
102  SUBSCRIPTS(subs, __VA_ARGS__), \
103  var, range_min, range_max)); \
104  } while (0)
105 
106 #define xsi(width, name, var, subs, ...) do { \
107  CHECK(ff_cbs_write_signed(ctx, rw, width, #name, \
108  SUBSCRIPTS(subs, __VA_ARGS__), var, \
109  MIN_INT_BITS(width), \
110  MAX_INT_BITS(width))); \
111  } while (0)
112 
113 #define marker_bit() do { \
114  CHECK(ff_cbs_write_unsigned(ctx, rw, 1, "marker_bit", NULL, 1, 1, 1)); \
115  } while (0)
116 
117 #define nextbits(width, compare, var) (var)
118 
120 
121 #undef WRITE
122 #undef READWRITE
123 #undef RWContext
124 #undef xui
125 #undef xsi
126 #undef marker_bit
127 #undef nextbits
128 
129 
130 static void cbs_mpeg2_free_user_data(void *unit, uint8_t *content)
131 {
132  MPEG2RawUserData *user = (MPEG2RawUserData*)content;
134  av_freep(&content);
135 }
136 
137 static void cbs_mpeg2_free_slice(void *unit, uint8_t *content)
138 {
139  MPEG2RawSlice *slice = (MPEG2RawSlice*)content;
141  av_buffer_unref(&slice->data_ref);
142  av_freep(&content);
143 }
144 
147  int header)
148 {
149  const uint8_t *start, *end;
150  uint8_t *unit_data;
151  uint32_t start_code = -1, next_start_code = -1;
152  size_t unit_size;
153  int err, i, unit_type;
154 
155  start = avpriv_find_start_code(frag->data, frag->data + frag->data_size,
156  &start_code);
157  for (i = 0;; i++) {
158  end = avpriv_find_start_code(start, frag->data + frag->data_size,
159  &next_start_code);
160 
161  unit_type = start_code & 0xff;
162 
163  // The start and end pointers point at to the byte following the
164  // start_code_identifier in the start code that they found.
165  if (end == frag->data + frag->data_size) {
166  // We didn't find a start code, so this is the final unit.
167  unit_size = end - (start - 1);
168  } else {
169  // Unit runs from start to the beginning of the start code
170  // pointed to by end (including any padding zeroes).
171  unit_size = (end - 4) - (start - 1);
172  }
173 
174  unit_data = (uint8_t *)start - 1;
175 
176  err = ff_cbs_insert_unit_data(ctx, frag, i, unit_type,
177  unit_data, unit_size, frag->data_ref);
178  if (err < 0)
179  return err;
180 
181  if (end == frag->data + frag->data_size)
182  break;
183 
184  start_code = next_start_code;
185  start = end;
186  }
187 
188  return 0;
189 }
190 
192  CodedBitstreamUnit *unit)
193 {
194  GetBitContext gbc;
195  int err;
196 
197  err = init_get_bits(&gbc, unit->data, 8 * unit->data_size);
198  if (err < 0)
199  return err;
200 
201  if (MPEG2_START_IS_SLICE(unit->type)) {
202  MPEG2RawSlice *slice;
203  int pos, len;
204 
205  err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(*slice),
207  if (err < 0)
208  return err;
209  slice = unit->content;
210 
211  err = cbs_mpeg2_read_slice_header(ctx, &gbc, &slice->header);
212  if (err < 0)
213  return err;
214 
215  pos = get_bits_count(&gbc);
216  len = unit->data_size;
217 
218  slice->data_size = len - pos / 8;
219  slice->data_ref = av_buffer_ref(unit->data_ref);
220  if (!slice->data_ref)
221  return AVERROR(ENOMEM);
222  slice->data = unit->data + pos / 8;
223 
224  slice->data_bit_start = pos % 8;
225 
226  } else {
227  switch (unit->type) {
228 #define START(start_code, type, read_func, free_func) \
229  case start_code: \
230  { \
231  type *header; \
232  err = ff_cbs_alloc_unit_content(ctx, unit, \
233  sizeof(*header), free_func); \
234  if (err < 0) \
235  return err; \
236  header = unit->content; \
237  err = cbs_mpeg2_read_ ## read_func(ctx, &gbc, header); \
238  if (err < 0) \
239  return err; \
240  } \
241  break;
252 #undef START
253  default:
254  return AVERROR(ENOSYS);
255  }
256  }
257 
258  return 0;
259 }
260 
262  CodedBitstreamUnit *unit,
263  PutBitContext *pbc)
264 {
265  int err;
266 
267  switch (unit->type) {
268 #define START(start_code, type, func) \
269  case start_code: \
270  err = cbs_mpeg2_write_ ## func(ctx, pbc, unit->content); \
271  break;
278 #undef START
279  default:
280  av_log(ctx->log_ctx, AV_LOG_ERROR, "Write unimplemented for start "
281  "code %02"PRIx32".\n", unit->type);
282  return AVERROR_PATCHWELCOME;
283  }
284 
285  return err;
286 }
287 
289  CodedBitstreamUnit *unit,
290  PutBitContext *pbc)
291 {
292  MPEG2RawSlice *slice = unit->content;
293  int err;
294 
295  err = cbs_mpeg2_write_slice_header(ctx, pbc, &slice->header);
296  if (err < 0)
297  return err;
298 
299  if (slice->data) {
300  size_t rest = slice->data_size - (slice->data_bit_start + 7) / 8;
301  uint8_t *pos = slice->data + slice->data_bit_start / 8;
302 
303  av_assert0(slice->data_bit_start >= 0 &&
304  8 * slice->data_size > slice->data_bit_start);
305 
306  if (slice->data_size * 8 + 8 > put_bits_left(pbc))
307  return AVERROR(ENOSPC);
308 
309  // First copy the remaining bits of the first byte
310  if (slice->data_bit_start % 8)
311  put_bits(pbc, 8 - slice->data_bit_start % 8,
312  *pos++ & MAX_UINT_BITS(8 - slice->data_bit_start % 8));
313 
314  if (put_bits_count(pbc) % 8 == 0) {
315  // If the writer is aligned at this point,
316  // memcpy can be used to improve performance.
317  // This is the normal case.
318  flush_put_bits(pbc);
319  memcpy(put_bits_ptr(pbc), pos, rest);
320  skip_put_bytes(pbc, rest);
321  } else {
322  // If not, we have to copy manually:
323  for (; rest > 3; rest -= 4, pos += 4)
324  put_bits32(pbc, AV_RB32(pos));
325 
326  for (; rest; rest--, pos++)
327  put_bits(pbc, 8, *pos);
328 
329  // Align with zeros
330  put_bits(pbc, 8 - put_bits_count(pbc) % 8, 0);
331  }
332  }
333 
334  return 0;
335 }
336 
338  CodedBitstreamUnit *unit)
339 {
341  PutBitContext pbc;
342  int err;
343 
344  if (!priv->write_buffer) {
345  // Initial write buffer size is 1MB.
346  priv->write_buffer_size = 1024 * 1024;
347 
348  reallocate_and_try_again:
349  err = av_reallocp(&priv->write_buffer, priv->write_buffer_size);
350  if (err < 0) {
351  av_log(ctx->log_ctx, AV_LOG_ERROR, "Unable to allocate a "
352  "sufficiently large write buffer (last attempt "
353  "%"SIZE_SPECIFIER" bytes).\n", priv->write_buffer_size);
354  return err;
355  }
356  }
357 
358  init_put_bits(&pbc, priv->write_buffer, priv->write_buffer_size);
359 
360  if (MPEG2_START_IS_SLICE(unit->type))
361  err = cbs_mpeg2_write_slice(ctx, unit, &pbc);
362  else
363  err = cbs_mpeg2_write_header(ctx, unit, &pbc);
364 
365  if (err == AVERROR(ENOSPC)) {
366  // Overflow.
367  priv->write_buffer_size *= 2;
368  goto reallocate_and_try_again;
369  }
370  if (err < 0) {
371  // Write failed for some other reason.
372  return err;
373  }
374 
375  if (put_bits_count(&pbc) % 8)
376  unit->data_bit_padding = 8 - put_bits_count(&pbc) % 8;
377  else
378  unit->data_bit_padding = 0;
379 
380  unit->data_size = (put_bits_count(&pbc) + 7) / 8;
381  flush_put_bits(&pbc);
382 
383  err = ff_cbs_alloc_unit_data(ctx, unit, unit->data_size);
384  if (err < 0)
385  return err;
386 
387  memcpy(unit->data, priv->write_buffer, unit->data_size);
388 
389  return 0;
390 }
391 
394 {
395  uint8_t *data;
396  size_t size, dp;
397  int i;
398 
399  size = 0;
400  for (i = 0; i < frag->nb_units; i++)
401  size += 3 + frag->units[i].data_size;
402 
404  if (!frag->data_ref)
405  return AVERROR(ENOMEM);
406  data = frag->data_ref->data;
407 
408  dp = 0;
409  for (i = 0; i < frag->nb_units; i++) {
410  CodedBitstreamUnit *unit = &frag->units[i];
411 
412  data[dp++] = 0;
413  data[dp++] = 0;
414  data[dp++] = 1;
415 
416  memcpy(data + dp, unit->data, unit->data_size);
417  dp += unit->data_size;
418  }
419 
420  av_assert0(dp == size);
421 
422  memset(data + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
423  frag->data = data;
424  frag->data_size = size;
425 
426  return 0;
427 }
428 
430 {
432 
433  av_freep(&priv->write_buffer);
434 }
435 
438 
439  .priv_data_size = sizeof(CodedBitstreamMPEG2Context),
440 
441  .split_fragment = &cbs_mpeg2_split_fragment,
442  .read_unit = &cbs_mpeg2_read_unit,
443  .write_unit = &cbs_mpeg2_write_unit,
444  .assemble_fragment = &cbs_mpeg2_assemble_fragment,
445 
446  .close = &cbs_mpeg2_close,
447 };
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:147
void av_buffer_unref(AVBufferRef **buf)
Free a given reference and automatically free the buffer if there are no more references to it...
Definition: buffer.c:125
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:100
uint8_t * data
Definition: cbs_mpeg2.h:207
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition: j2kenc.c:208
CodedBitstreamUnitType type
Codec-specific type of this unit.
Definition: cbs.h:68
int ff_cbs_alloc_unit_content(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit, size_t size, void(*free)(void *opaque, uint8_t *data))
Definition: cbs.c:587
static int cbs_mpeg2_write_unit(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit)
Definition: cbs_mpeg2.c:337
static void cbs_mpeg2_free_user_data(void *unit, uint8_t *content)
Definition: cbs_mpeg2.c:130
static int cbs_mpeg2_read_unit(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit)
Definition: cbs_mpeg2.c:191
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
static int cbs_mpeg2_assemble_fragment(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag)
Definition: cbs_mpeg2.c:392
static int FUNC() extension_data(CodedBitstreamContext *ctx, RWContext *rw, H265RawPSExtensionData *current)
uint8_t
#define MAX_UINT_BITS(length)
Definition: cbs_internal.h:96
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:90
AVBufferRef * extra_information_ref
Definition: cbs_mpeg2.h:201
size_t data_bit_padding
The number of bits which should be ignored in the final byte.
Definition: cbs.h:86
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_RB32
Definition: bytestream.h:87
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:219
int ff_cbs_alloc_unit_data(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit, size_t size)
Allocate a new internal data buffer of the given size in the unit.
Definition: cbs.c:608
Coded bitstream unit structure.
Definition: cbs.h:64
ptrdiff_t size
Definition: opengl_enc.c:100
void * content
Pointer to the decomposed form of this unit.
Definition: cbs.h:101
static const uint8_t header[24]
Definition: sdr2.c:67
CodedBitstreamUnit * units
Pointer to an array of units of length nb_units_allocated.
Definition: cbs.h:162
uint8_t * data
Pointer to the directly-parsable bitstream form of this unit.
Definition: cbs.h:75
int ff_cbs_insert_unit_data(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, int position, CodedBitstreamUnitType type, uint8_t *data, size_t data_size, AVBufferRef *data_buf)
Insert a new unit into a fragment with the given data bitstream.
Definition: cbs.c:702
#define av_log(a,...)
const uint8_t * avpriv_find_start_code(const uint8_t *p, const uint8_t *end, uint32_t *state)
size_t data_size
The number of bytes in the bitstream.
Definition: cbs.h:129
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:260
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
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:324
static int put_bits_left(PutBitContext *s)
Definition: put_bits.h:93
AVS_Value void * user_data
Definition: avisynth_c.h:775
AVBufferRef * user_data_ref
Definition: cbs_mpeg2.h:81
simple assert() macros that are a bit more flexible than ISO C assert().
static int put_bits_count(PutBitContext *s)
Definition: put_bits.h:85
static int cbs_mpeg2_split_fragment(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, int header)
Definition: cbs_mpeg2.c:145
static int FUNC() picture_header(CodedBitstreamContext *ctx, RWContext *rw, MPEG2RawPictureHeader *current)
void * log_ctx
Logging context to be passed to all av_log() calls associated with this context.
Definition: cbs.h:173
static void skip_put_bytes(PutBitContext *s, int n)
Skip the given number of bytes.
Definition: put_bits.h:333
#define START(start_code, type, read_func, free_func)
AVFormatContext * ctx
Definition: movenc.c:48
preferred ID for MPEG-1/2 video decoding
Definition: avcodec.h:220
uint8_t * data
Pointer to the bitstream form of this fragment.
Definition: cbs.h:122
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
const CodedBitstreamType ff_cbs_type_mpeg2
Definition: cbs_mpeg2.c:436
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:163
int data_bit_start
Definition: cbs_mpeg2.h:209
static int FUNC() sequence_header(CodedBitstreamContext *ctx, RWContext *rw, MPEG2RawSequenceHeader *current)
AVBufferRef * av_buffer_alloc(int size)
Allocate an AVBuffer of the given size using av_malloc().
Definition: buffer.c:67
Coded bitstream fragment structure, combining one or more units.
Definition: cbs.h:116
#define MPEG2_START_IS_SLICE(type)
Definition: cbs_mpeg2.h:40
uint8_t * data
The data buffer.
Definition: buffer.h:89
Context structure for coded bitstream operations.
Definition: cbs.h:168
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:659
enum AVCodecID codec_id
Definition: cbs_internal.h:29
#define SIZE_SPECIFIER
Definition: internal.h:262
static int cbs_mpeg2_write_slice(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit, PutBitContext *pbc)
Definition: cbs_mpeg2.c:288
MPEG2RawSliceHeader header
Definition: cbs_mpeg2.h:205
void * priv_data
Internal codec-specific data.
Definition: cbs.h:189
common internal api header.
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition: put_bits.h:101
static void cbs_mpeg2_close(CodedBitstreamContext *ctx)
Definition: cbs_mpeg2.c:429
AVBufferRef * data_ref
A reference to the buffer containing data.
Definition: cbs.h:92
AVBufferRef * av_buffer_ref(AVBufferRef *buf)
Create a new reference to an AVBuffer.
Definition: buffer.c:93
static int FUNC() group_of_pictures_header(CodedBitstreamContext *ctx, RWContext *rw, MPEG2RawGroupOfPicturesHeader *current)
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
Definition: put_bits.h:48
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:790
int len
static void cbs_mpeg2_free_slice(void *unit, uint8_t *content)
Definition: cbs_mpeg2.c:137
AVBufferRef * data_ref
Definition: cbs_mpeg2.h:210
size_t data_size
Definition: cbs_mpeg2.h:208
#define av_freep(p)
void INT64 start
Definition: avisynth_c.h:766
static const uint8_t start_code[]
static int cbs_mpeg2_write_header(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit, PutBitContext *pbc)
Definition: cbs_mpeg2.c:261
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
AVBufferRef * data_ref
A reference to the buffer containing data.
Definition: cbs.h:139
size_t data_size
The number of bytes in the bitstream (including any padding bits in the final byte).
Definition: cbs.h:80