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"
24 #include "startcode.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 uir(width, name) \
44 xui(width, name, current->name, 1, MAX_UINT_BITS(width), 0, )
45 #define uis(width, name, subs, ...) \
46 xui(width, name, current->name, 0, MAX_UINT_BITS(width), subs, __VA_ARGS__)
47 #define uirs(width, name, subs, ...) \
48 xui(width, name, current->name, 1, MAX_UINT_BITS(width), subs, __VA_ARGS__)
49 #define xui(width, name, var, range_min, range_max, subs, ...) \
50 xuia(width, #name, var, range_min, range_max, subs, __VA_ARGS__)
51 #define sis(width, name, subs, ...) \
52 xsi(width, name, current->name, subs, __VA_ARGS__)
54 #define marker_bit() \
56 #define bit(string, value) do { \
57 av_unused uint32_t bit = value; \
58 xuia(1, string, bit, value, value, 0, ); \
63 #define READWRITE read
64 #define RWContext GetBitContext
66 #define ui(width, name) do { \
68 CHECK(ff_cbs_read_simple_unsigned(ctx, rw, width, #name, \
70 current->name = value; \
72 #define xuia(width, string, var, range_min, range_max, subs, ...) do { \
74 CHECK(ff_cbs_read_unsigned(ctx, rw, width, string, \
75 SUBSCRIPTS(subs, __VA_ARGS__), \
76 &value, range_min, range_max)); \
80 #define xsi(width, name, var, subs, ...) do { \
82 CHECK(ff_cbs_read_signed(ctx, rw, width, #name, \
83 SUBSCRIPTS(subs, __VA_ARGS__), &value, \
84 MIN_INT_BITS(width), \
85 MAX_INT_BITS(width))); \
89 #define nextbits(width, compare, var) \
90 (get_bits_left(rw) >= width && \
91 (var = show_bits(rw, width)) == (compare))
93 #define infer(name, value) do { \
94 current->name = value; \
97 #include "cbs_mpeg2_syntax_template.c"
110 #define READWRITE write
111 #define RWContext PutBitContext
113 #define ui(width, name) do { \
114 CHECK(ff_cbs_write_simple_unsigned(ctx, rw, width, #name, \
118 #define xuia(width, string, var, range_min, range_max, subs, ...) do { \
119 CHECK(ff_cbs_write_unsigned(ctx, rw, width, string, \
120 SUBSCRIPTS(subs, __VA_ARGS__), \
121 var, range_min, range_max)); \
124 #define xsi(width, name, var, subs, ...) do { \
125 CHECK(ff_cbs_write_signed(ctx, rw, width, #name, \
126 SUBSCRIPTS(subs, __VA_ARGS__), var, \
127 MIN_INT_BITS(width), \
128 MAX_INT_BITS(width))); \
131 #define nextbits(width, compare, var) (var)
133 #define infer(name, value) do { \
134 if (current->name != (value)) { \
135 av_log(ctx->log_ctx, AV_LOG_WARNING, "Warning: " \
136 "%s does not match inferred value: " \
137 "%"PRId64", but should be %"PRId64".\n", \
138 #name, (int64_t)current->name, (int64_t)(value)); \
142 #include "cbs_mpeg2_syntax_template.c"
154 static int cbs_mpeg2_split_fragment(CodedBitstreamContext
*ctx
,
155 CodedBitstreamFragment
*frag
,
158 const uint8_t *start
;
159 uint32_t start_code
= -1;
162 start
= avpriv_find_start_code(frag
->data
, frag
->data
+ frag
->data_size
,
164 if (start_code
>> 8 != 0x000001) {
165 // No start code found.
166 return AVERROR_INVALIDDATA
;
170 CodedBitstreamUnitType unit_type
= start_code
& 0xff;
174 // Reset start_code to ensure that avpriv_find_start_code()
175 // really reads a new start code and does not reuse the old
176 // start code in any way (as e.g. happens when there is a
177 // Sequence End unit at the very end of a packet).
178 start_code
= UINT32_MAX
;
179 end
= avpriv_find_start_code(start
--, frag
->data
+ frag
->data_size
,
182 // start points to the byte containing the start_code_identifier
183 // (may be the last byte of fragment->data); end points to the byte
184 // following the byte containing the start code identifier (or to
185 // the end of fragment->data).
186 if (start_code
>> 8 == 0x000001) {
187 // Unit runs from start to the beginning of the start code
188 // pointed to by end (including any padding zeroes).
189 unit_size
= (end
- 4) - start
;
191 // We didn't find a start code, so this is the final unit.
192 unit_size
= end
- start
;
195 err
= ff_cbs_append_unit_data(frag
, unit_type
, (uint8_t*)start
,
196 unit_size
, frag
->data_ref
);
202 // Do we have a further unit to add to the fragment?
203 } while ((start_code
>> 8) == 0x000001);
208 static int cbs_mpeg2_read_unit(CodedBitstreamContext
*ctx
,
209 CodedBitstreamUnit
*unit
)
214 err
= init_get_bits(&gbc
, unit
->data
, 8 * unit
->data_size
);
218 err
= ff_cbs_alloc_unit_content(ctx
, unit
);
222 if (MPEG2_START_IS_SLICE(unit
->type
)) {
223 MPEG2RawSlice
*slice
= unit
->content
;
226 err
= cbs_mpeg2_read_slice_header(ctx
, &gbc
, &slice
->header
);
230 if (!get_bits_left(&gbc
))
231 return AVERROR_INVALIDDATA
;
233 pos
= get_bits_count(&gbc
);
234 len
= unit
->data_size
;
236 slice
->data_size
= len
- pos
/ 8;
237 slice
->data_ref
= av_buffer_ref(unit
->data_ref
);
238 if (!slice
->data_ref
)
239 return AVERROR(ENOMEM
);
240 slice
->data
= unit
->data
+ pos
/ 8;
242 slice
->data_bit_start
= pos
% 8;
245 switch (unit
->type
) {
246 #define START(start_code, type, read_func, free_func) \
249 type *header = unit->content; \
250 err = cbs_mpeg2_read_ ## read_func(ctx, &gbc, header); \
255 START(MPEG2_START_PICTURE
, MPEG2RawPictureHeader
,
256 picture_header
, &cbs_mpeg2_free_picture_header
);
257 START(MPEG2_START_USER_DATA
, MPEG2RawUserData
,
258 user_data
, &cbs_mpeg2_free_user_data
);
259 START(MPEG2_START_SEQUENCE_HEADER
, MPEG2RawSequenceHeader
,
260 sequence_header
, NULL
);
261 START(MPEG2_START_EXTENSION
, MPEG2RawExtensionData
,
262 extension_data
, NULL
);
263 START(MPEG2_START_GROUP
, MPEG2RawGroupOfPicturesHeader
,
264 group_of_pictures_header
, NULL
);
265 START(MPEG2_START_SEQUENCE_END
, MPEG2RawSequenceEnd
,
269 return AVERROR(ENOSYS
);
276 static int cbs_mpeg2_write_header(CodedBitstreamContext
*ctx
,
277 CodedBitstreamUnit
*unit
,
282 switch (unit
->type
) {
283 #define START(start_code, type, func) \
285 err = cbs_mpeg2_write_ ## func(ctx, pbc, unit->content); \
287 START(MPEG2_START_PICTURE
, MPEG2RawPictureHeader
, picture_header
);
288 START(MPEG2_START_USER_DATA
, MPEG2RawUserData
, user_data
);
289 START(MPEG2_START_SEQUENCE_HEADER
, MPEG2RawSequenceHeader
, sequence_header
);
290 START(MPEG2_START_EXTENSION
, MPEG2RawExtensionData
, extension_data
);
291 START(MPEG2_START_GROUP
, MPEG2RawGroupOfPicturesHeader
,
292 group_of_pictures_header
);
293 START(MPEG2_START_SEQUENCE_END
, MPEG2RawSequenceEnd
, sequence_end
);
296 av_log(ctx
->log_ctx
, AV_LOG_ERROR
, "Write unimplemented for start "
297 "code %02"PRIx32
".\n", unit
->type
);
298 return AVERROR_PATCHWELCOME
;
304 static int cbs_mpeg2_write_slice(CodedBitstreamContext
*ctx
,
305 CodedBitstreamUnit
*unit
,
308 MPEG2RawSlice
*slice
= unit
->content
;
311 err
= cbs_mpeg2_write_slice_header(ctx
, pbc
, &slice
->header
);
316 size_t rest
= slice
->data_size
- (slice
->data_bit_start
+ 7) / 8;
317 uint8_t *pos
= slice
->data
+ slice
->data_bit_start
/ 8;
319 av_assert0(slice
->data_bit_start
>= 0 &&
320 slice
->data_size
> slice
->data_bit_start
/ 8);
322 if (slice
->data_size
* 8 + 8 > put_bits_left(pbc
))
323 return AVERROR(ENOSPC
);
325 // First copy the remaining bits of the first byte
326 if (slice
->data_bit_start
% 8)
327 put_bits(pbc
, 8 - slice
->data_bit_start
% 8,
328 *pos
++ & MAX_UINT_BITS(8 - slice
->data_bit_start
% 8));
330 if (put_bits_count(pbc
) % 8 == 0) {
331 // If the writer is aligned at this point,
332 // memcpy can be used to improve performance.
333 // This is the normal case.
335 memcpy(put_bits_ptr(pbc
), pos
, rest
);
336 skip_put_bytes(pbc
, rest
);
338 // If not, we have to copy manually:
339 for (; rest
> 3; rest
-= 4, pos
+= 4)
340 put_bits32(pbc
, AV_RB32(pos
));
342 for (; rest
; rest
--, pos
++)
343 put_bits(pbc
, 8, *pos
);
346 put_bits(pbc
, 8 - put_bits_count(pbc
) % 8, 0);
353 static int cbs_mpeg2_write_unit(CodedBitstreamContext
*ctx
,
354 CodedBitstreamUnit
*unit
,
357 if (MPEG2_START_IS_SLICE(unit
->type
))
358 return cbs_mpeg2_write_slice (ctx
, unit
, pbc
);
360 return cbs_mpeg2_write_header(ctx
, unit
, pbc
);
363 static int cbs_mpeg2_assemble_fragment(CodedBitstreamContext
*ctx
,
364 CodedBitstreamFragment
*frag
)
371 for (i
= 0; i
< frag
->nb_units
; i
++)
372 size
+= 3 + frag
->units
[i
].data_size
;
374 frag
->data_ref
= av_buffer_alloc(size
+ AV_INPUT_BUFFER_PADDING_SIZE
);
376 return AVERROR(ENOMEM
);
377 data
= frag
->data_ref
->data
;
380 for (i
= 0; i
< frag
->nb_units
; i
++) {
381 CodedBitstreamUnit
*unit
= &frag
->units
[i
];
387 memcpy(data
+ dp
, unit
->data
, unit
->data_size
);
388 dp
+= unit
->data_size
;
391 av_assert0(dp
== size
);
393 memset(data
+ size
, 0, AV_INPUT_BUFFER_PADDING_SIZE
);
395 frag
->data_size
= size
;
400 static CodedBitstreamUnitTypeDescriptor cbs_mpeg2_unit_types
[] = {
401 CBS_UNIT_TYPE_INTERNAL_REF(MPEG2_START_PICTURE
, MPEG2RawPictureHeader
,
402 extra_information_picture
.extra_information
),
405 .nb_unit_types
= CBS_UNIT_TYPE_RANGE
,
406 .unit_type
.range
.start
= 0x01,
407 .unit_type
.range
.end
= 0xaf,
409 .content_type
= CBS_CONTENT_TYPE_INTERNAL_REFS
,
410 .content_size
= sizeof(MPEG2RawSlice
),
411 .type
.ref
= { .nb_offsets
= 2,
412 .offsets
= { offsetof(MPEG2RawSlice
, header
.extra_information_slice
.extra_information
),
413 offsetof(MPEG2RawSlice
, data
) } },
416 CBS_UNIT_TYPE_INTERNAL_REF(MPEG2_START_USER_DATA
, MPEG2RawUserData
,
419 CBS_UNIT_TYPE_POD(MPEG2_START_SEQUENCE_HEADER
, MPEG2RawSequenceHeader
),
420 CBS_UNIT_TYPE_POD(MPEG2_START_EXTENSION
, MPEG2RawExtensionData
),
421 CBS_UNIT_TYPE_POD(MPEG2_START_SEQUENCE_END
, MPEG2RawSequenceEnd
),
422 CBS_UNIT_TYPE_POD(MPEG2_START_GROUP
, MPEG2RawGroupOfPicturesHeader
),
424 CBS_UNIT_TYPE_END_OF_LIST
427 const CodedBitstreamType ff_cbs_type_mpeg2
= {
428 .codec_id
= AV_CODEC_ID_MPEG2VIDEO
,
430 .priv_data_size
= sizeof(CodedBitstreamMPEG2Context
),
432 .unit_types
= cbs_mpeg2_unit_types
,
434 .split_fragment
= &cbs_mpeg2_split_fragment
,
435 .read_unit
= &cbs_mpeg2_read_unit
,
436 .write_unit
= &cbs_mpeg2_write_unit
,
437 .assemble_fragment
= &cbs_mpeg2_assemble_fragment
,