FFmpeg  4.3.7
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 xui(width, name, var, range_min, range_max, subs, ...) \
52  xuia(width, #name, var, range_min, range_max, subs, __VA_ARGS__)
53 #define sis(width, name, subs, ...) \
54  xsi(width, name, current->name, subs, __VA_ARGS__)
55 
56 #define marker_bit() \
57  bit("marker_bit", 1)
58 #define bit(string, value) do { \
59  av_unused uint32_t bit = value; \
60  xuia(1, string, bit, value, value, 0, ); \
61  } while (0)
62 
63 
64 #define READ
65 #define READWRITE read
66 #define RWContext GetBitContext
67 
68 #define xuia(width, string, var, range_min, range_max, subs, ...) do { \
69  uint32_t value; \
70  CHECK(ff_cbs_read_unsigned(ctx, rw, width, string, \
71  SUBSCRIPTS(subs, __VA_ARGS__), \
72  &value, range_min, range_max)); \
73  var = value; \
74  } while (0)
75 
76 #define xsi(width, name, var, subs, ...) do { \
77  int32_t value; \
78  CHECK(ff_cbs_read_signed(ctx, rw, width, #name, \
79  SUBSCRIPTS(subs, __VA_ARGS__), &value, \
80  MIN_INT_BITS(width), \
81  MAX_INT_BITS(width))); \
82  var = value; \
83  } while (0)
84 
85 #define nextbits(width, compare, var) \
86  (get_bits_left(rw) >= width && \
87  (var = show_bits(rw, width)) == (compare))
88 
89 #define infer(name, value) do { \
90  current->name = value; \
91  } while (0)
92 
94 
95 #undef READ
96 #undef READWRITE
97 #undef RWContext
98 #undef xuia
99 #undef xsi
100 #undef nextbits
101 #undef infer
102 
103 
104 #define WRITE
105 #define READWRITE write
106 #define RWContext PutBitContext
107 
108 #define xuia(width, string, var, range_min, range_max, subs, ...) do { \
109  CHECK(ff_cbs_write_unsigned(ctx, rw, width, string, \
110  SUBSCRIPTS(subs, __VA_ARGS__), \
111  var, range_min, range_max)); \
112  } while (0)
113 
114 #define xsi(width, name, var, subs, ...) do { \
115  CHECK(ff_cbs_write_signed(ctx, rw, width, #name, \
116  SUBSCRIPTS(subs, __VA_ARGS__), var, \
117  MIN_INT_BITS(width), \
118  MAX_INT_BITS(width))); \
119  } while (0)
120 
121 #define nextbits(width, compare, var) (var)
122 
123 #define infer(name, value) do { \
124  if (current->name != (value)) { \
125  av_log(ctx->log_ctx, AV_LOG_WARNING, "Warning: " \
126  "%s does not match inferred value: " \
127  "%"PRId64", but should be %"PRId64".\n", \
128  #name, (int64_t)current->name, (int64_t)(value)); \
129  } \
130  } while (0)
131 
133 
134 #undef WRITE
135 #undef READWRITE
136 #undef RWContext
137 #undef xuia
138 #undef xsi
139 #undef nextbits
140 #undef infer
141 
142 
143 static void cbs_mpeg2_free_picture_header(void *opaque, uint8_t *content)
144 {
145  MPEG2RawPictureHeader *picture = (MPEG2RawPictureHeader*)content;
147  av_freep(&content);
148 }
149 
150 static void cbs_mpeg2_free_user_data(void *opaque, uint8_t *content)
151 {
152  MPEG2RawUserData *user = (MPEG2RawUserData*)content;
154  av_freep(&content);
155 }
156 
157 static void cbs_mpeg2_free_slice(void *opaque, uint8_t *content)
158 {
159  MPEG2RawSlice *slice = (MPEG2RawSlice*)content;
161  av_buffer_unref(&slice->data_ref);
162  av_freep(&content);
163 }
164 
167  int header)
168 {
169  const uint8_t *start, *end;
170  CodedBitstreamUnitType unit_type;
171  uint32_t start_code = -1;
172  size_t unit_size;
173  int err, i, final = 0;
174 
175  start = avpriv_find_start_code(frag->data, frag->data + frag->data_size,
176  &start_code);
177  if (start_code >> 8 != 0x000001) {
178  // No start code found.
179  return AVERROR_INVALIDDATA;
180  }
181 
182  for (i = 0;; i++) {
183  unit_type = start_code & 0xff;
184 
185  if (start == frag->data + frag->data_size) {
186  // The last four bytes form a start code which constitutes
187  // a unit of its own. In this situation avpriv_find_start_code
188  // won't modify start_code at all so modify start_code so that
189  // the next unit will be treated as the last unit.
190  start_code = 0;
191  }
192 
193  end = avpriv_find_start_code(start--, frag->data + frag->data_size,
194  &start_code);
195 
196  // start points to the byte containing the start_code_identifier
197  // (may be the last byte of fragment->data); end points to the byte
198  // following the byte containing the start code identifier (or to
199  // the end of fragment->data).
200  if (start_code >> 8 == 0x000001) {
201  // Unit runs from start to the beginning of the start code
202  // pointed to by end (including any padding zeroes).
203  unit_size = (end - 4) - start;
204  } else {
205  // We didn't find a start code, so this is the final unit.
206  unit_size = end - start;
207  final = 1;
208  }
209 
210  err = ff_cbs_insert_unit_data(ctx, frag, i, unit_type, (uint8_t*)start,
211  unit_size, frag->data_ref);
212  if (err < 0)
213  return err;
214 
215  if (final)
216  break;
217 
218  start = end;
219  }
220 
221  return 0;
222 }
223 
225  CodedBitstreamUnit *unit)
226 {
227  GetBitContext gbc;
228  int err;
229 
230  err = init_get_bits(&gbc, unit->data, 8 * unit->data_size);
231  if (err < 0)
232  return err;
233 
234  if (MPEG2_START_IS_SLICE(unit->type)) {
235  MPEG2RawSlice *slice;
236  int pos, len;
237 
238  err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(*slice),
240  if (err < 0)
241  return err;
242  slice = unit->content;
243 
244  err = cbs_mpeg2_read_slice_header(ctx, &gbc, &slice->header);
245  if (err < 0)
246  return err;
247 
248  if (!get_bits_left(&gbc))
249  return AVERROR_INVALIDDATA;
250 
251  pos = get_bits_count(&gbc);
252  len = unit->data_size;
253 
254  slice->data_size = len - pos / 8;
255  slice->data_ref = av_buffer_ref(unit->data_ref);
256  if (!slice->data_ref)
257  return AVERROR(ENOMEM);
258  slice->data = unit->data + pos / 8;
259 
260  slice->data_bit_start = pos % 8;
261 
262  } else {
263  switch (unit->type) {
264 #define START(start_code, type, read_func, free_func) \
265  case start_code: \
266  { \
267  type *header; \
268  err = ff_cbs_alloc_unit_content(ctx, unit, \
269  sizeof(*header), free_func); \
270  if (err < 0) \
271  return err; \
272  header = unit->content; \
273  err = cbs_mpeg2_read_ ## read_func(ctx, &gbc, header); \
274  if (err < 0) \
275  return err; \
276  } \
277  break;
289  sequence_end, NULL);
290 #undef START
291  default:
292  return AVERROR(ENOSYS);
293  }
294  }
295 
296  return 0;
297 }
298 
300  CodedBitstreamUnit *unit,
301  PutBitContext *pbc)
302 {
303  int err;
304 
305  switch (unit->type) {
306 #define START(start_code, type, func) \
307  case start_code: \
308  err = cbs_mpeg2_write_ ## func(ctx, pbc, unit->content); \
309  break;
317 #undef START
318  default:
319  av_log(ctx->log_ctx, AV_LOG_ERROR, "Write unimplemented for start "
320  "code %02"PRIx32".\n", unit->type);
321  return AVERROR_PATCHWELCOME;
322  }
323 
324  return err;
325 }
326 
328  CodedBitstreamUnit *unit,
329  PutBitContext *pbc)
330 {
331  MPEG2RawSlice *slice = unit->content;
332  int err;
333 
334  err = cbs_mpeg2_write_slice_header(ctx, pbc, &slice->header);
335  if (err < 0)
336  return err;
337 
338  if (slice->data) {
339  size_t rest = slice->data_size - (slice->data_bit_start + 7) / 8;
340  uint8_t *pos = slice->data + slice->data_bit_start / 8;
341 
342  av_assert0(slice->data_bit_start >= 0 &&
343  slice->data_size > slice->data_bit_start / 8);
344 
345  if (slice->data_size * 8 + 8 > put_bits_left(pbc))
346  return AVERROR(ENOSPC);
347 
348  // First copy the remaining bits of the first byte
349  if (slice->data_bit_start % 8)
350  put_bits(pbc, 8 - slice->data_bit_start % 8,
351  *pos++ & MAX_UINT_BITS(8 - slice->data_bit_start % 8));
352 
353  if (put_bits_count(pbc) % 8 == 0) {
354  // If the writer is aligned at this point,
355  // memcpy can be used to improve performance.
356  // This is the normal case.
357  flush_put_bits(pbc);
358  memcpy(put_bits_ptr(pbc), pos, rest);
359  skip_put_bytes(pbc, rest);
360  } else {
361  // If not, we have to copy manually:
362  for (; rest > 3; rest -= 4, pos += 4)
363  put_bits32(pbc, AV_RB32(pos));
364 
365  for (; rest; rest--, pos++)
366  put_bits(pbc, 8, *pos);
367 
368  // Align with zeros
369  put_bits(pbc, 8 - put_bits_count(pbc) % 8, 0);
370  }
371  }
372 
373  return 0;
374 }
375 
377  CodedBitstreamUnit *unit,
378  PutBitContext *pbc)
379 {
380  if (MPEG2_START_IS_SLICE(unit->type))
381  return cbs_mpeg2_write_slice (ctx, unit, pbc);
382  else
383  return cbs_mpeg2_write_header(ctx, unit, pbc);
384 }
385 
388 {
389  uint8_t *data;
390  size_t size, dp;
391  int i;
392 
393  size = 0;
394  for (i = 0; i < frag->nb_units; i++)
395  size += 3 + frag->units[i].data_size;
396 
398  if (!frag->data_ref)
399  return AVERROR(ENOMEM);
400  data = frag->data_ref->data;
401 
402  dp = 0;
403  for (i = 0; i < frag->nb_units; i++) {
404  CodedBitstreamUnit *unit = &frag->units[i];
405 
406  data[dp++] = 0;
407  data[dp++] = 0;
408  data[dp++] = 1;
409 
410  memcpy(data + dp, unit->data, unit->data_size);
411  dp += unit->data_size;
412  }
413 
414  av_assert0(dp == size);
415 
416  memset(data + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
417  frag->data = data;
418  frag->data_size = size;
419 
420  return 0;
421 }
422 
425 
426  .priv_data_size = sizeof(CodedBitstreamMPEG2Context),
427 
428  .split_fragment = &cbs_mpeg2_split_fragment,
429  .read_unit = &cbs_mpeg2_read_unit,
430  .write_unit = &cbs_mpeg2_write_unit,
431  .assemble_fragment = &cbs_mpeg2_assemble_fragment,
432 };
MPEG2RawExtraInformation extra_information_slice
Definition: cbs_mpeg2.h:203
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
static void cbs_mpeg2_free_slice(void *opaque, uint8_t *content)
Definition: cbs_mpeg2.c:157
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
static void cbs_mpeg2_free_picture_header(void *opaque, uint8_t *content)
Definition: cbs_mpeg2.c:143
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
int size
uint8_t * data
Definition: cbs_mpeg2.h:209
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:644
static int cbs_mpeg2_read_unit(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit)
Definition: cbs_mpeg2.c:224
#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:386
uint8_t
uint32_t CodedBitstreamUnitType
The codec-specific type of a bitstream unit.
Definition: cbs.h:43
#define MAX_UINT_BITS(length)
Definition: cbs_internal.h:98
#define AV_RB32
Definition: intreadwrite.h:130
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:92
const char data[16]
Definition: mxf.c:91
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:219
Coded bitstream unit structure.
Definition: cbs.h:64
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:759
#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
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:849
static int FUNC() extension_data(CodedBitstreamContext *ctx, RWContext *rw, H265RawExtensionData *current)
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
#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
#define AVERROR(e)
Definition: error.h:43
unsigned int pos
Definition: spdifenc.c:412
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:67
static int cbs_mpeg2_split_fragment(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, int header)
Definition: cbs_mpeg2.c:165
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 cbs_mpeg2_free_user_data(void *opaque, uint8_t *content)
Definition: cbs_mpeg2.c:150
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)
static int FUNC() user_data(CodedBitstreamContext *ctx, RWContext *rw, MPEG2RawUserData *current)
static int FUNC() sequence_end(CodedBitstreamContext *ctx, RWContext *rw, MPEG2RawSequenceEnd *current)
AVFormatContext * ctx
Definition: movenc.c:48
preferred ID for MPEG-1/2 video decoding
Definition: codec_id.h:51
AVBufferRef * extra_information_ref
Definition: cbs_mpeg2.h:119
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:423
int data_bit_start
Definition: cbs_mpeg2.h:211
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
static int cbs_mpeg2_write_unit(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit, PutBitContext *pbc)
Definition: cbs_mpeg2.c:376
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
static int cbs_mpeg2_write_slice(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit, PutBitContext *pbc)
Definition: cbs_mpeg2.c:327
MPEG2RawSliceHeader header
Definition: cbs_mpeg2.h:207
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
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)
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:215
int len
AVBufferRef * data_ref
Definition: cbs_mpeg2.h:212
size_t data_size
Definition: cbs_mpeg2.h:210
#define av_freep(p)
static const uint8_t start_code[]
MPEG2RawExtraInformation extra_information_picture
Definition: cbs_mpeg2.h:135
static int cbs_mpeg2_write_header(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit, PutBitContext *pbc)
Definition: cbs_mpeg2.c:299
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