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)
45 #define uis(width, name, subs, ...) \
46 xui(width, name, current->name, subs, __VA_ARGS__)
50 #define READWRITE read
51 #define RWContext GetBitContext
53 #define xui(width, name, var, subs, ...) do { \
55 CHECK(ff_cbs_read_unsigned(ctx, rw, width, #name, \
56 SUBSCRIPTS(subs, __VA_ARGS__), \
57 &value, 0, (1 << width) - 1)); \
61 #define marker_bit() do { \
62 av_unused uint32_t one; \
63 CHECK(ff_cbs_read_unsigned(ctx, rw, 1, "marker_bit", NULL, &one, 1, 1)); \
66 #define nextbits(width, compare, var) \
67 (get_bits_left(rw) >= width && \
68 (var = show_bits(rw, width)) == (compare))
70 #include "cbs_mpeg2_syntax_template.c"
81 #define READWRITE write
82 #define RWContext PutBitContext
84 #define xui(width, name, var, subs, ...) do { \
85 CHECK(ff_cbs_write_unsigned(ctx, rw, width, #name, \
86 SUBSCRIPTS(subs, __VA_ARGS__), \
87 var, 0, (1 << width) - 1)); \
90 #define marker_bit() do { \
91 CHECK(ff_cbs_write_unsigned(ctx, rw, 1, "marker_bit", NULL, 1, 1, 1)); \
94 #define nextbits(width, compare, var) (var)
96 #include "cbs_mpeg2_syntax_template.c"
106 static void cbs_mpeg2_free_user_data(void *unit, uint8_t *content)
108 MPEG2RawUserData *user = (MPEG2RawUserData*)content;
109 av_buffer_unref(&user->user_data_ref);
113 static void cbs_mpeg2_free_slice(void *unit, uint8_t *content)
115 MPEG2RawSlice *slice = (MPEG2RawSlice*)content;
116 av_buffer_unref(&slice->header.extra_information_ref);
117 av_buffer_unref(&slice->data_ref);
121 static int cbs_mpeg2_split_fragment(CodedBitstreamContext *ctx,
122 CodedBitstreamFragment *frag,
125 const uint8_t *start, *end;
127 uint32_t start_code = -1, next_start_code = -1;
129 int err, i, unit_type;
131 start = avpriv_find_start_code(frag->data, frag->data + frag->data_size,
134 end = avpriv_find_start_code(start, frag->data + frag->data_size,
137 unit_type = start_code & 0xff;
139 // The start and end pointers point at to the byte following the
140 // start_code_identifier in the start code that they found.
141 if (end == frag->data + frag->data_size) {
142 // We didn't find a start code, so this is the final unit.
143 unit_size = end - (start - 1);
145 // Unit runs from start to the beginning of the start code
146 // pointed to by end (including any padding zeroes).
147 unit_size = (end - 4) - (start - 1);
150 unit_data = (uint8_t *)start - 1;
152 err = ff_cbs_insert_unit_data(ctx, frag, i, unit_type,
153 unit_data, unit_size, frag->data_ref);
157 if (end == frag->data + frag->data_size)
160 start_code = next_start_code;
167 static int cbs_mpeg2_read_unit(CodedBitstreamContext *ctx,
168 CodedBitstreamUnit *unit)
173 err = init_get_bits(&gbc, unit->data, 8 * unit->data_size);
177 if (MPEG2_START_IS_SLICE(unit->type)) {
178 MPEG2RawSlice *slice;
181 err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(*slice),
182 &cbs_mpeg2_free_slice);
185 slice = unit->content;
187 err = cbs_mpeg2_read_slice_header(ctx, &gbc, &slice->header);
191 pos = get_bits_count(&gbc);
192 len = unit->data_size;
194 slice->data_size = len - pos / 8;
195 slice->data_ref = av_buffer_ref(unit->data_ref);
196 if (!slice->data_ref)
197 return AVERROR(ENOMEM);
198 slice->data = unit->data + pos / 8;
200 slice->data_bit_start = pos % 8;
203 switch (unit->type) {
204 #define START(start_code, type, read_func, free_func) \
208 err = ff_cbs_alloc_unit_content(ctx, unit, \
209 sizeof(*header), free_func); \
212 header = unit->content; \
213 err = cbs_mpeg2_read_ ## read_func(ctx, &gbc, header); \
218 START(0x00, MPEG2RawPictureHeader, picture_header, NULL);
219 START(0xb2, MPEG2RawUserData, user_data,
220 &cbs_mpeg2_free_user_data);
221 START(0xb3, MPEG2RawSequenceHeader, sequence_header, NULL);
222 START(0xb5, MPEG2RawExtensionData, extension_data, NULL);
223 START(0xb8, MPEG2RawGroupOfPicturesHeader,
224 group_of_pictures_header, NULL);
227 av_log(ctx->log_ctx, AV_LOG_ERROR, "Unknown start code %02"PRIx32".\n",
229 return AVERROR_INVALIDDATA;
236 static int cbs_mpeg2_write_header(CodedBitstreamContext *ctx,
237 CodedBitstreamUnit *unit,
242 switch (unit->type) {
243 #define START(start_code, type, func) \
245 err = cbs_mpeg2_write_ ## func(ctx, pbc, unit->content); \
247 START(0x00, MPEG2RawPictureHeader, picture_header);
248 START(0xb2, MPEG2RawUserData, user_data);
249 START(0xb3, MPEG2RawSequenceHeader, sequence_header);
250 START(0xb5, MPEG2RawExtensionData, extension_data);
251 START(0xb8, MPEG2RawGroupOfPicturesHeader, group_of_pictures_header);
254 av_log(ctx->log_ctx, AV_LOG_ERROR, "Write unimplemented for start "
255 "code %02"PRIx32".\n", unit->type);
256 return AVERROR_PATCHWELCOME;
262 static int cbs_mpeg2_write_slice(CodedBitstreamContext *ctx,
263 CodedBitstreamUnit *unit,
266 MPEG2RawSlice *slice = unit->content;
269 err = cbs_mpeg2_write_slice_header(ctx, pbc, &slice->header);
274 size_t rest = slice->data_size - (slice->data_bit_start + 7) / 8;
275 uint8_t *pos = slice->data + slice->data_bit_start / 8;
277 av_assert0(slice->data_bit_start >= 0 &&
278 8 * slice->data_size > slice->data_bit_start);
280 if (slice->data_size * 8 + 8 > put_bits_left(pbc))
281 return AVERROR(ENOSPC);
283 // First copy the remaining bits of the first byte
284 if (slice->data_bit_start % 8)
285 put_bits(pbc, 8 - slice->data_bit_start % 8,
286 *pos++ & MAX_UINT_BITS(8 - slice->data_bit_start % 8));
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 is the normal case.
293 memcpy(put_bits_ptr(pbc), pos, rest);
294 skip_put_bytes(pbc, rest);
296 // If not, we have to copy manually:
297 for (; rest > 3; rest -= 4, pos += 4)
298 put_bits32(pbc, AV_RB32(pos));
300 for (; rest; rest--, pos++)
301 put_bits(pbc, 8, *pos);
304 put_bits(pbc, 8 - put_bits_count(pbc) % 8, 0);
311 static int cbs_mpeg2_write_unit(CodedBitstreamContext *ctx,
312 CodedBitstreamUnit *unit)
314 CodedBitstreamMPEG2Context *priv = ctx->priv_data;
318 if (!priv->write_buffer) {
319 // Initial write buffer size is 1MB.
320 priv->write_buffer_size = 1024 * 1024;
322 reallocate_and_try_again:
323 err = av_reallocp(&priv->write_buffer, priv->write_buffer_size);
325 av_log(ctx->log_ctx, AV_LOG_ERROR, "Unable to allocate a "
326 "sufficiently large write buffer (last attempt "
327 "%"SIZE_SPECIFIER" bytes).\n", priv->write_buffer_size);
332 init_put_bits(&pbc, priv->write_buffer, priv->write_buffer_size);
334 if (unit->type >= 0x01 && unit->type <= 0xaf)
335 err = cbs_mpeg2_write_slice(ctx, unit, &pbc);
337 err = cbs_mpeg2_write_header(ctx, unit, &pbc);
339 if (err == AVERROR(ENOSPC)) {
341 priv->write_buffer_size *= 2;
342 goto reallocate_and_try_again;
345 // Write failed for some other reason.
349 if (put_bits_count(&pbc) % 8)
350 unit->data_bit_padding = 8 - put_bits_count(&pbc) % 8;
352 unit->data_bit_padding = 0;
354 unit->data_size = (put_bits_count(&pbc) + 7) / 8;
355 flush_put_bits(&pbc);
357 err = ff_cbs_alloc_unit_data(ctx, unit, unit->data_size);
361 memcpy(unit->data, priv->write_buffer, unit->data_size);
366 static int cbs_mpeg2_assemble_fragment(CodedBitstreamContext *ctx,
367 CodedBitstreamFragment *frag)
374 for (i = 0; i < frag->nb_units; i++)
375 size += 3 + frag->units[i].data_size;
377 frag->data_ref = av_buffer_alloc(size + AV_INPUT_BUFFER_PADDING_SIZE);
379 return AVERROR(ENOMEM);
380 data = frag->data_ref->data;
383 for (i = 0; i < frag->nb_units; i++) {
384 CodedBitstreamUnit *unit = &frag->units[i];
390 memcpy(data + dp, unit->data, unit->data_size);
391 dp += unit->data_size;
394 av_assert0(dp == size);
396 memset(data + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
398 frag->data_size = size;
403 static void cbs_mpeg2_close(CodedBitstreamContext *ctx)
405 CodedBitstreamMPEG2Context *priv = ctx->priv_data;
407 av_freep(&priv->write_buffer);
410 const CodedBitstreamType ff_cbs_type_mpeg2 = {
411 .codec_id = AV_CODEC_ID_MPEG2VIDEO,
413 .priv_data_size = sizeof(CodedBitstreamMPEG2Context),
415 .split_fragment = &cbs_mpeg2_split_fragment,
416 .read_unit = &cbs_mpeg2_read_unit,
417 .write_unit = &cbs_mpeg2_write_unit,
418 .assemble_fragment = &cbs_mpeg2_assemble_fragment,
420 .close = &cbs_mpeg2_close,