2 * This file is part of FFmpeg.
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.
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.
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
19 #include "libavutil/avassert.h"
22 #include "cbs_internal.h"
23 #include "cbs_mpeg2.h"
27 #define HEADER(name) do { \
28 ff_cbs_trace_header(ctx, name); \
31 #define CHECK(call) do { \
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)
41 #define SUBSCRIPTS(subs, ...) (subs > 0 ? ((int[subs + 1]){ subs, __VA_ARGS__ }) : NULL)
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__)
56 #define marker_bit() \
58 #define bit(string, value) do { \
59 av_unused uint32_t bit = value; \
60 xuia(1, string, bit, value, value, 0); \
65 #define READWRITE read
66 #define RWContext GetBitContext
68 #define xuia(width, string, var, range_min, range_max, subs, ...) do { \
70 CHECK(ff_cbs_read_unsigned(ctx, rw, width, string, \
71 SUBSCRIPTS(subs, __VA_ARGS__), \
72 &value, range_min, range_max)); \
76 #define xsi(width, name, var, subs, ...) do { \
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))); \
85 #define nextbits(width, compare, var) \
86 (get_bits_left(rw) >= width && \
87 (var = show_bits(rw, width)) == (compare))
89 #define infer(name, value) do { \
90 current->name = value; \
93 #include "cbs_mpeg2_syntax_template.c"
105 #define READWRITE write
106 #define RWContext PutBitContext
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)); \
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))); \
121 #define nextbits(width, compare, var) (var)
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)); \
132 #include "cbs_mpeg2_syntax_template.c"
143 static void cbs_mpeg2_free_picture_header(void *opaque, uint8_t *content)
145 MPEG2RawPictureHeader *picture = (MPEG2RawPictureHeader*)content;
146 av_buffer_unref(&picture->extra_information_picture.extra_information_ref);
150 static void cbs_mpeg2_free_user_data(void *opaque, uint8_t *content)
152 MPEG2RawUserData *user = (MPEG2RawUserData*)content;
153 av_buffer_unref(&user->user_data_ref);
157 static void cbs_mpeg2_free_slice(void *opaque, uint8_t *content)
159 MPEG2RawSlice *slice = (MPEG2RawSlice*)content;
160 av_buffer_unref(&slice->header.extra_information_slice.extra_information_ref);
161 av_buffer_unref(&slice->data_ref);
165 static int cbs_mpeg2_split_fragment(CodedBitstreamContext *ctx,
166 CodedBitstreamFragment *frag,
169 const uint8_t *start, *end;
170 CodedBitstreamUnitType unit_type;
171 uint32_t start_code = -1;
175 start = avpriv_find_start_code(frag->data, frag->data + frag->data_size,
178 unit_type = start_code & 0xff;
180 end = avpriv_find_start_code(start--, frag->data + frag->data_size,
183 // start points to the byte containing the start_code_identifier
184 // (or to the last byte of fragment->data); end points to the byte
185 // following the byte containing the start code identifier (or to
186 // the end of fragment->data).
187 if (end == frag->data + frag->data_size) {
188 // We didn't find a start code, so this is the final unit.
189 unit_size = end - start;
191 // Unit runs from start to the beginning of the start code
192 // pointed to by end (including any padding zeroes).
193 unit_size = (end - 4) - start;
196 err = ff_cbs_insert_unit_data(ctx, frag, i, unit_type, (uint8_t*)start,
197 unit_size, frag->data_ref);
201 if (end == frag->data + frag->data_size)
210 static int cbs_mpeg2_read_unit(CodedBitstreamContext *ctx,
211 CodedBitstreamUnit *unit)
216 err = init_get_bits(&gbc, unit->data, 8 * unit->data_size);
220 if (MPEG2_START_IS_SLICE(unit->type)) {
221 MPEG2RawSlice *slice;
224 err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(*slice),
225 &cbs_mpeg2_free_slice);
228 slice = unit->content;
230 err = cbs_mpeg2_read_slice_header(ctx, &gbc, &slice->header);
234 pos = get_bits_count(&gbc);
235 len = unit->data_size;
237 slice->data_size = len - pos / 8;
238 slice->data_ref = av_buffer_ref(unit->data_ref);
239 if (!slice->data_ref)
240 return AVERROR(ENOMEM);
241 slice->data = unit->data + pos / 8;
243 slice->data_bit_start = pos % 8;
246 switch (unit->type) {
247 #define START(start_code, type, read_func, free_func) \
251 err = ff_cbs_alloc_unit_content(ctx, unit, \
252 sizeof(*header), free_func); \
255 header = unit->content; \
256 err = cbs_mpeg2_read_ ## read_func(ctx, &gbc, header); \
261 START(MPEG2_START_PICTURE, MPEG2RawPictureHeader,
262 picture_header, &cbs_mpeg2_free_picture_header);
263 START(MPEG2_START_USER_DATA, MPEG2RawUserData,
264 user_data, &cbs_mpeg2_free_user_data);
265 START(MPEG2_START_SEQUENCE_HEADER, MPEG2RawSequenceHeader,
266 sequence_header, NULL);
267 START(MPEG2_START_EXTENSION, MPEG2RawExtensionData,
268 extension_data, NULL);
269 START(MPEG2_START_GROUP, MPEG2RawGroupOfPicturesHeader,
270 group_of_pictures_header, NULL);
271 START(MPEG2_START_SEQUENCE_END, MPEG2RawSequenceEnd,
275 return AVERROR(ENOSYS);
282 static int cbs_mpeg2_write_header(CodedBitstreamContext *ctx,
283 CodedBitstreamUnit *unit,
288 switch (unit->type) {
289 #define START(start_code, type, func) \
291 err = cbs_mpeg2_write_ ## func(ctx, pbc, unit->content); \
293 START(MPEG2_START_PICTURE, MPEG2RawPictureHeader, picture_header);
294 START(MPEG2_START_USER_DATA, MPEG2RawUserData, user_data);
295 START(MPEG2_START_SEQUENCE_HEADER, MPEG2RawSequenceHeader, sequence_header);
296 START(MPEG2_START_EXTENSION, MPEG2RawExtensionData, extension_data);
297 START(MPEG2_START_GROUP, MPEG2RawGroupOfPicturesHeader,
298 group_of_pictures_header);
299 START(MPEG2_START_SEQUENCE_END, MPEG2RawSequenceEnd, sequence_end);
302 av_log(ctx->log_ctx, AV_LOG_ERROR, "Write unimplemented for start "
303 "code %02"PRIx32".\n", unit->type);
304 return AVERROR_PATCHWELCOME;
310 static int cbs_mpeg2_write_slice(CodedBitstreamContext *ctx,
311 CodedBitstreamUnit *unit,
314 MPEG2RawSlice *slice = unit->content;
317 err = cbs_mpeg2_write_slice_header(ctx, pbc, &slice->header);
322 size_t rest = slice->data_size - (slice->data_bit_start + 7) / 8;
323 uint8_t *pos = slice->data + slice->data_bit_start / 8;
325 av_assert0(slice->data_bit_start >= 0 &&
326 8 * slice->data_size > slice->data_bit_start);
328 if (slice->data_size * 8 + 8 > put_bits_left(pbc))
329 return AVERROR(ENOSPC);
331 // First copy the remaining bits of the first byte
332 if (slice->data_bit_start % 8)
333 put_bits(pbc, 8 - slice->data_bit_start % 8,
334 *pos++ & MAX_UINT_BITS(8 - slice->data_bit_start % 8));
336 if (put_bits_count(pbc) % 8 == 0) {
337 // If the writer is aligned at this point,
338 // memcpy can be used to improve performance.
339 // This is the normal case.
341 memcpy(put_bits_ptr(pbc), pos, rest);
342 skip_put_bytes(pbc, rest);
344 // If not, we have to copy manually:
345 for (; rest > 3; rest -= 4, pos += 4)
346 put_bits32(pbc, AV_RB32(pos));
348 for (; rest; rest--, pos++)
349 put_bits(pbc, 8, *pos);
352 put_bits(pbc, 8 - put_bits_count(pbc) % 8, 0);
359 static int cbs_mpeg2_write_unit(CodedBitstreamContext *ctx,
360 CodedBitstreamUnit *unit)
362 CodedBitstreamMPEG2Context *priv = ctx->priv_data;
366 if (!priv->write_buffer) {
367 // Initial write buffer size is 1MB.
368 priv->write_buffer_size = 1024 * 1024;
370 reallocate_and_try_again:
371 err = av_reallocp(&priv->write_buffer, priv->write_buffer_size);
373 av_log(ctx->log_ctx, AV_LOG_ERROR, "Unable to allocate a "
374 "sufficiently large write buffer (last attempt "
375 "%"SIZE_SPECIFIER" bytes).\n", priv->write_buffer_size);
380 init_put_bits(&pbc, priv->write_buffer, priv->write_buffer_size);
382 if (MPEG2_START_IS_SLICE(unit->type))
383 err = cbs_mpeg2_write_slice(ctx, unit, &pbc);
385 err = cbs_mpeg2_write_header(ctx, unit, &pbc);
387 if (err == AVERROR(ENOSPC)) {
389 priv->write_buffer_size *= 2;
390 goto reallocate_and_try_again;
393 // Write failed for some other reason.
397 if (put_bits_count(&pbc) % 8)
398 unit->data_bit_padding = 8 - put_bits_count(&pbc) % 8;
400 unit->data_bit_padding = 0;
402 unit->data_size = (put_bits_count(&pbc) + 7) / 8;
403 flush_put_bits(&pbc);
405 err = ff_cbs_alloc_unit_data(ctx, unit, unit->data_size);
409 memcpy(unit->data, priv->write_buffer, unit->data_size);
414 static int cbs_mpeg2_assemble_fragment(CodedBitstreamContext *ctx,
415 CodedBitstreamFragment *frag)
422 for (i = 0; i < frag->nb_units; i++)
423 size += 3 + frag->units[i].data_size;
425 frag->data_ref = av_buffer_alloc(size + AV_INPUT_BUFFER_PADDING_SIZE);
427 return AVERROR(ENOMEM);
428 data = frag->data_ref->data;
431 for (i = 0; i < frag->nb_units; i++) {
432 CodedBitstreamUnit *unit = &frag->units[i];
438 memcpy(data + dp, unit->data, unit->data_size);
439 dp += unit->data_size;
442 av_assert0(dp == size);
444 memset(data + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
446 frag->data_size = size;
451 static void cbs_mpeg2_close(CodedBitstreamContext *ctx)
453 CodedBitstreamMPEG2Context *priv = ctx->priv_data;
455 av_freep(&priv->write_buffer);
458 const CodedBitstreamType ff_cbs_type_mpeg2 = {
459 .codec_id = AV_CODEC_ID_MPEG2VIDEO,
461 .priv_data_size = sizeof(CodedBitstreamMPEG2Context),
463 .split_fragment = &cbs_mpeg2_split_fragment,
464 .read_unit = &cbs_mpeg2_read_unit,
465 .write_unit = &cbs_mpeg2_write_unit,
466 .assemble_fragment = &cbs_mpeg2_assemble_fragment,
468 .close = &cbs_mpeg2_close,