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 static int FUNC(sequence_header)(CodedBitstreamContext *ctx, RWContext *rw,
20 MPEG2RawSequenceHeader *current)
22 CodedBitstreamMPEG2Context *mpeg2 = ctx->priv_data;
25 HEADER("Sequence Header");
27 ui(8, sequence_header_code);
29 ui(12, horizontal_size_value);
30 ui(12, vertical_size_value);
32 mpeg2->horizontal_size = current->horizontal_size_value;
33 mpeg2->vertical_size = current->vertical_size_value;
35 ui(4, aspect_ratio_information);
36 ui(4, frame_rate_code);
37 ui(18, bit_rate_value);
41 ui(10, vbv_buffer_size_value);
42 ui(1, constrained_parameters_flag);
44 ui(1, load_intra_quantiser_matrix);
45 if (current->load_intra_quantiser_matrix) {
46 for (i = 0; i < 64; i++)
47 uis(8, intra_quantiser_matrix[i], 1, i);
50 ui(1, load_non_intra_quantiser_matrix);
51 if (current->load_non_intra_quantiser_matrix) {
52 for (i = 0; i < 64; i++)
53 uis(8, non_intra_quantiser_matrix[i], 1, i);
59 static int FUNC(user_data)(CodedBitstreamContext *ctx, RWContext *rw,
60 MPEG2RawUserData *current)
67 ui(8, user_data_start_code);
70 k = get_bits_left(rw);
71 av_assert0(k % 8 == 0);
72 current->user_data_length = k /= 8;
74 current->user_data_ref = av_buffer_allocz(k + AV_INPUT_BUFFER_PADDING_SIZE);
75 if (!current->user_data_ref)
76 return AVERROR(ENOMEM);
77 current->user_data = current->user_data_ref->data;
81 for (k = 0; k < current->user_data_length; k++)
82 xui(8, user_data, current->user_data[k], 0);
87 static int FUNC(sequence_extension)(CodedBitstreamContext *ctx, RWContext *rw,
88 MPEG2RawSequenceExtension *current)
90 CodedBitstreamMPEG2Context *mpeg2 = ctx->priv_data;
93 HEADER("Sequence Extension");
95 ui(8, profile_and_level_indication);
96 ui(1, progressive_sequence);
98 ui(2, horizontal_size_extension);
99 ui(2, vertical_size_extension);
101 mpeg2->horizontal_size = (mpeg2->horizontal_size & 0xfff) |
102 current->horizontal_size_extension << 12;
103 mpeg2->vertical_size = (mpeg2->vertical_size & 0xfff) |
104 current->vertical_size_extension << 12;
105 mpeg2->progressive_sequence = current->progressive_sequence;
107 ui(12, bit_rate_extension);
109 ui(8, vbv_buffer_size_extension);
111 ui(2, frame_rate_extension_n);
112 ui(5, frame_rate_extension_d);
117 static int FUNC(sequence_display_extension)(CodedBitstreamContext *ctx, RWContext *rw,
118 MPEG2RawSequenceDisplayExtension *current)
122 HEADER("Sequence Display Extension");
126 ui(1, colour_description);
127 if (current->colour_description) {
128 ui(8, colour_primaries);
129 ui(8, transfer_characteristics);
130 ui(8, matrix_coefficients);
133 ui(14, display_horizontal_size);
135 ui(14, display_vertical_size);
140 static int FUNC(group_of_pictures_header)(CodedBitstreamContext *ctx, RWContext *rw,
141 MPEG2RawGroupOfPicturesHeader *current)
145 HEADER("Group of Pictures Header");
147 ui(8, group_start_code);
156 static int FUNC(picture_header)(CodedBitstreamContext *ctx, RWContext *rw,
157 MPEG2RawPictureHeader *current)
161 HEADER("Picture Header");
163 ui(8, picture_start_code);
165 ui(10, temporal_reference);
166 ui(3, picture_coding_type);
169 if (current->picture_coding_type == 2 ||
170 current->picture_coding_type == 3) {
171 ui(1, full_pel_forward_vector);
172 ui(3, forward_f_code);
175 if (current->picture_coding_type == 3) {
176 ui(1, full_pel_backward_vector);
177 ui(3, backward_f_code);
180 ui(1, extra_bit_picture);
185 static int FUNC(picture_coding_extension)(CodedBitstreamContext *ctx, RWContext *rw,
186 MPEG2RawPictureCodingExtension *current)
188 CodedBitstreamMPEG2Context *mpeg2 = ctx->priv_data;
191 HEADER("Picture Coding Extension");
198 ui(2, intra_dc_precision);
199 ui(2, picture_structure);
200 ui(1, top_field_first);
201 ui(1, frame_pred_frame_dct);
202 ui(1, concealment_motion_vectors);
204 ui(1, intra_vlc_format);
205 ui(1, alternate_scan);
206 ui(1, repeat_first_field);
207 ui(1, chroma_420_type);
208 ui(1, progressive_frame);
210 if (mpeg2->progressive_sequence) {
211 if (current->repeat_first_field) {
212 if (current->top_field_first)
213 mpeg2->number_of_frame_centre_offsets = 3;
215 mpeg2->number_of_frame_centre_offsets = 2;
217 mpeg2->number_of_frame_centre_offsets = 1;
220 if (current->picture_structure == 1 || // Top field.
221 current->picture_structure == 2) { // Bottom field.
222 mpeg2->number_of_frame_centre_offsets = 1;
224 if (current->repeat_first_field)
225 mpeg2->number_of_frame_centre_offsets = 3;
227 mpeg2->number_of_frame_centre_offsets = 2;
231 ui(1, composite_display_flag);
232 if (current->composite_display_flag) {
234 ui(3, field_sequence);
236 ui(7, burst_amplitude);
237 ui(8, sub_carrier_phase);
243 static int FUNC(quant_matrix_extension)(CodedBitstreamContext *ctx, RWContext *rw,
244 MPEG2RawQuantMatrixExtension *current)
248 HEADER("Quant Matrix Extension");
250 ui(1, load_intra_quantiser_matrix);
251 if (current->load_intra_quantiser_matrix) {
252 for (i = 0; i < 64; i++)
253 uis(8, intra_quantiser_matrix[i], 1, i);
256 ui(1, load_non_intra_quantiser_matrix);
257 if (current->load_non_intra_quantiser_matrix) {
258 for (i = 0; i < 64; i++)
259 uis(8, non_intra_quantiser_matrix[i], 1, i);
262 ui(1, load_chroma_intra_quantiser_matrix);
263 if (current->load_chroma_intra_quantiser_matrix) {
264 for (i = 0; i < 64; i++)
265 uis(8, intra_quantiser_matrix[i], 1, i);
268 ui(1, load_chroma_non_intra_quantiser_matrix);
269 if (current->load_chroma_non_intra_quantiser_matrix) {
270 for (i = 0; i < 64; i++)
271 uis(8, chroma_non_intra_quantiser_matrix[i], 1, i);
277 static int FUNC(picture_display_extension)(CodedBitstreamContext *ctx, RWContext *rw,
278 MPEG2RawPictureDisplayExtension *current)
280 CodedBitstreamMPEG2Context *mpeg2 = ctx->priv_data;
283 HEADER("Picture Display Extension");
285 for (i = 0; i < mpeg2->number_of_frame_centre_offsets; i++) {
286 ui(16, frame_centre_horizontal_offset[i]);
288 ui(16, frame_centre_vertical_offset[i]);
295 static int FUNC(extension_data)(CodedBitstreamContext *ctx, RWContext *rw,
296 MPEG2RawExtensionData *current)
300 HEADER("Extension Data");
302 ui(8, extension_start_code);
303 ui(4, extension_start_code_identifier);
305 switch (current->extension_start_code_identifier) {
307 return FUNC(sequence_extension)
308 (ctx, rw, ¤t->data.sequence);
310 return FUNC(sequence_display_extension)
311 (ctx, rw, ¤t->data.sequence_display);
313 return FUNC(quant_matrix_extension)
314 (ctx, rw, ¤t->data.quant_matrix);
316 return FUNC(picture_display_extension)
317 (ctx, rw, ¤t->data.picture_display);
319 return FUNC(picture_coding_extension)
320 (ctx, rw, ¤t->data.picture_coding);
322 av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid extension ID %d.\n",
323 current->extension_start_code_identifier);
324 return AVERROR_INVALIDDATA;
328 static int FUNC(slice_header)(CodedBitstreamContext *ctx, RWContext *rw,
329 MPEG2RawSliceHeader *current)
331 CodedBitstreamMPEG2Context *mpeg2 = ctx->priv_data;
334 HEADER("Slice Header");
336 ui(8, slice_vertical_position);
338 if (mpeg2->vertical_size > 2800)
339 ui(3, slice_vertical_position_extension);
340 if (mpeg2->scalable) {
341 if (mpeg2->scalable_mode == 0)
342 ui(7, priority_breakpoint);
345 ui(5, quantiser_scale_code);
347 if (nextbits(1, 1, current->slice_extension_flag)) {
348 ui(1, slice_extension_flag);
350 ui(1, slice_picture_id_enable);
351 ui(6, slice_picture_id);
359 for (k = 0; nextbits(1, 1, bit); k++)
361 current->extra_information_length = k;
364 current->extra_information =
365 av_malloc(current->extra_information_length);
366 if (!current->extra_information)
367 return AVERROR(ENOMEM);
368 for (k = 0; k < current->extra_information_length; k++) {
369 xui(1, extra_bit_slice, bit, 0);
370 xui(8, extra_information_slice[k],
371 current->extra_information[k], 1, k);
375 for (k = 0; k < current->extra_information_length; k++) {
376 xui(1, extra_bit_slice, 1, 0);
377 xui(8, extra_information_slice[k],
378 current->extra_information[k], 1, k);
383 ui(1, extra_bit_slice);