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 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 int cbs_mpeg2_split_fragment(CodedBitstreamContext
*ctx
,
144 CodedBitstreamFragment
*frag
,
147 const uint8_t *start
;
148 uint32_t start_code
= -1;
151 start
= avpriv_find_start_code(frag
->data
, frag
->data
+ frag
->data_size
,
153 if (start_code
>> 8 != 0x000001) {
154 // No start code found.
155 return AVERROR_INVALIDDATA
;
159 CodedBitstreamUnitType unit_type
= start_code
& 0xff;
163 // Reset start_code to ensure that avpriv_find_start_code()
164 // really reads a new start code and does not reuse the old
165 // start code in any way (as e.g. happens when there is a
166 // Sequence End unit at the very end of a packet).
167 start_code
= UINT32_MAX
;
168 end
= avpriv_find_start_code(start
--, frag
->data
+ frag
->data_size
,
171 // start points to the byte containing the start_code_identifier
172 // (may be the last byte of fragment->data); end points to the byte
173 // following the byte containing the start code identifier (or to
174 // the end of fragment->data).
175 if (start_code
>> 8 == 0x000001) {
176 // Unit runs from start to the beginning of the start code
177 // pointed to by end (including any padding zeroes).
178 unit_size
= (end
- 4) - start
;
180 // We didn't find a start code, so this is the final unit.
181 unit_size
= end
- start
;
184 err
= ff_cbs_append_unit_data(frag
, unit_type
, (uint8_t*)start
,
185 unit_size
, frag
->data_ref
);
191 // Do we have a further unit to add to the fragment?
192 } while ((start_code
>> 8) == 0x000001);
197 static int cbs_mpeg2_read_unit(CodedBitstreamContext
*ctx
,
198 CodedBitstreamUnit
*unit
)
203 err
= init_get_bits(&gbc
, unit
->data
, 8 * unit
->data_size
);
207 err
= ff_cbs_alloc_unit_content2(ctx
, unit
);
211 if (MPEG2_START_IS_SLICE(unit
->type
)) {
212 MPEG2RawSlice
*slice
= unit
->content
;
215 err
= cbs_mpeg2_read_slice_header(ctx
, &gbc
, &slice
->header
);
219 if (!get_bits_left(&gbc
))
220 return AVERROR_INVALIDDATA
;
222 pos
= get_bits_count(&gbc
);
223 len
= unit
->data_size
;
225 slice
->data_size
= len
- pos
/ 8;
226 slice
->data_ref
= av_buffer_ref(unit
->data_ref
);
227 if (!slice
->data_ref
)
228 return AVERROR(ENOMEM
);
229 slice
->data
= unit
->data
+ pos
/ 8;
231 slice
->data_bit_start
= pos
% 8;
234 switch (unit
->type
) {
235 #define START(start_code, type, read_func, free_func) \
238 type *header = unit->content; \
239 err = cbs_mpeg2_read_ ## read_func(ctx, &gbc, header); \
244 START(MPEG2_START_PICTURE
, MPEG2RawPictureHeader
,
245 picture_header
, &cbs_mpeg2_free_picture_header
);
246 START(MPEG2_START_USER_DATA
, MPEG2RawUserData
,
247 user_data
, &cbs_mpeg2_free_user_data
);
248 START(MPEG2_START_SEQUENCE_HEADER
, MPEG2RawSequenceHeader
,
249 sequence_header
, NULL
);
250 START(MPEG2_START_EXTENSION
, MPEG2RawExtensionData
,
251 extension_data
, NULL
);
252 START(MPEG2_START_GROUP
, MPEG2RawGroupOfPicturesHeader
,
253 group_of_pictures_header
, NULL
);
254 START(MPEG2_START_SEQUENCE_END
, MPEG2RawSequenceEnd
,
258 return AVERROR(ENOSYS
);
265 static int cbs_mpeg2_write_header(CodedBitstreamContext
*ctx
,
266 CodedBitstreamUnit
*unit
,
271 switch (unit
->type
) {
272 #define START(start_code, type, func) \
274 err = cbs_mpeg2_write_ ## func(ctx, pbc, unit->content); \
276 START(MPEG2_START_PICTURE
, MPEG2RawPictureHeader
, picture_header
);
277 START(MPEG2_START_USER_DATA
, MPEG2RawUserData
, user_data
);
278 START(MPEG2_START_SEQUENCE_HEADER
, MPEG2RawSequenceHeader
, sequence_header
);
279 START(MPEG2_START_EXTENSION
, MPEG2RawExtensionData
, extension_data
);
280 START(MPEG2_START_GROUP
, MPEG2RawGroupOfPicturesHeader
,
281 group_of_pictures_header
);
282 START(MPEG2_START_SEQUENCE_END
, MPEG2RawSequenceEnd
, sequence_end
);
285 av_log(ctx
->log_ctx
, AV_LOG_ERROR
, "Write unimplemented for start "
286 "code %02"PRIx32
".\n", unit
->type
);
287 return AVERROR_PATCHWELCOME
;
293 static int cbs_mpeg2_write_slice(CodedBitstreamContext
*ctx
,
294 CodedBitstreamUnit
*unit
,
297 MPEG2RawSlice
*slice
= unit
->content
;
300 err
= cbs_mpeg2_write_slice_header(ctx
, pbc
, &slice
->header
);
305 size_t rest
= slice
->data_size
- (slice
->data_bit_start
+ 7) / 8;
306 uint8_t *pos
= slice
->data
+ slice
->data_bit_start
/ 8;
308 av_assert0(slice
->data_bit_start
>= 0 &&
309 slice
->data_size
> slice
->data_bit_start
/ 8);
311 if (slice
->data_size
* 8 + 8 > put_bits_left(pbc
))
312 return AVERROR(ENOSPC
);
314 // First copy the remaining bits of the first byte
315 if (slice
->data_bit_start
% 8)
316 put_bits(pbc
, 8 - slice
->data_bit_start
% 8,
317 *pos
++ & MAX_UINT_BITS(8 - slice
->data_bit_start
% 8));
319 if (put_bits_count(pbc
) % 8 == 0) {
320 // If the writer is aligned at this point,
321 // memcpy can be used to improve performance.
322 // This is the normal case.
324 memcpy(put_bits_ptr(pbc
), pos
, rest
);
325 skip_put_bytes(pbc
, rest
);
327 // If not, we have to copy manually:
328 for (; rest
> 3; rest
-= 4, pos
+= 4)
329 put_bits32(pbc
, AV_RB32(pos
));
331 for (; rest
; rest
--, pos
++)
332 put_bits(pbc
, 8, *pos
);
335 put_bits(pbc
, 8 - put_bits_count(pbc
) % 8, 0);
342 static int cbs_mpeg2_write_unit(CodedBitstreamContext
*ctx
,
343 CodedBitstreamUnit
*unit
,
346 if (MPEG2_START_IS_SLICE(unit
->type
))
347 return cbs_mpeg2_write_slice (ctx
, unit
, pbc
);
349 return cbs_mpeg2_write_header(ctx
, unit
, pbc
);
352 static int cbs_mpeg2_assemble_fragment(CodedBitstreamContext
*ctx
,
353 CodedBitstreamFragment
*frag
)
360 for (i
= 0; i
< frag
->nb_units
; i
++)
361 size
+= 3 + frag
->units
[i
].data_size
;
363 frag
->data_ref
= av_buffer_alloc(size
+ AV_INPUT_BUFFER_PADDING_SIZE
);
365 return AVERROR(ENOMEM
);
366 data
= frag
->data_ref
->data
;
369 for (i
= 0; i
< frag
->nb_units
; i
++) {
370 CodedBitstreamUnit
*unit
= &frag
->units
[i
];
376 memcpy(data
+ dp
, unit
->data
, unit
->data_size
);
377 dp
+= unit
->data_size
;
380 av_assert0(dp
== size
);
382 memset(data
+ size
, 0, AV_INPUT_BUFFER_PADDING_SIZE
);
384 frag
->data_size
= size
;
389 static const CodedBitstreamUnitTypeDescriptor cbs_mpeg2_unit_types
[] = {
390 CBS_UNIT_TYPE_INTERNAL_REF(MPEG2_START_PICTURE
, MPEG2RawPictureHeader
,
391 extra_information_picture
.extra_information
),
394 .nb_unit_types
= CBS_UNIT_TYPE_RANGE
,
395 .unit_type_range_start
= 0x01,
396 .unit_type_range_end
= 0xaf,
398 .content_type
= CBS_CONTENT_TYPE_INTERNAL_REFS
,
399 .content_size
= sizeof(MPEG2RawSlice
),
401 .ref_offsets
= { offsetof(MPEG2RawSlice
, header
.extra_information_slice
.extra_information
),
402 offsetof(MPEG2RawSlice
, data
) },
405 CBS_UNIT_TYPE_INTERNAL_REF(MPEG2_START_USER_DATA
, MPEG2RawUserData
,
408 CBS_UNIT_TYPE_POD(MPEG2_START_SEQUENCE_HEADER
, MPEG2RawSequenceHeader
),
409 CBS_UNIT_TYPE_POD(MPEG2_START_EXTENSION
, MPEG2RawExtensionData
),
410 CBS_UNIT_TYPE_POD(MPEG2_START_SEQUENCE_END
, MPEG2RawSequenceEnd
),
411 CBS_UNIT_TYPE_POD(MPEG2_START_GROUP
, MPEG2RawGroupOfPicturesHeader
),
413 CBS_UNIT_TYPE_END_OF_LIST
416 const CodedBitstreamType ff_cbs_type_mpeg2
= {
417 .codec_id
= AV_CODEC_ID_MPEG2VIDEO
,
419 .priv_data_size
= sizeof(CodedBitstreamMPEG2Context
),
421 .unit_types
= cbs_mpeg2_unit_types
,
423 .split_fragment
= &cbs_mpeg2_split_fragment
,
424 .read_unit
= &cbs_mpeg2_read_unit
,
425 .write_unit
= &cbs_mpeg2_write_unit
,
426 .assemble_fragment
= &cbs_mpeg2_assemble_fragment
,