FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
cbs_mpeg2_syntax_template.c
Go to the documentation of this file.
1 /*
2  * This file is part of FFmpeg.
3  *
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.
8  *
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.
13  *
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
17  */
18 
20  MPEG2RawSequenceHeader *current)
21 {
23  int err, i;
24 
25  HEADER("Sequence Header");
26 
27  ui(8, sequence_header_code);
28 
29  ui(12, horizontal_size_value);
30  ui(12, vertical_size_value);
31 
32  mpeg2->horizontal_size = current->horizontal_size_value;
33  mpeg2->vertical_size = current->vertical_size_value;
34 
35  ui(4, aspect_ratio_information);
36  ui(4, frame_rate_code);
37  ui(18, bit_rate_value);
38 
39  marker_bit();
40 
41  ui(10, vbv_buffer_size_value);
42  ui(1, constrained_parameters_flag);
43 
44  ui(1, load_intra_quantiser_matrix);
45  if (current->load_intra_quantiser_matrix) {
46  for (i = 0; i < 64; i++)
47  ui(8, intra_quantiser_matrix[i]);
48  }
49 
50  ui(1, load_non_intra_quantiser_matrix);
51  if (current->load_non_intra_quantiser_matrix) {
52  for (i = 0; i < 64; i++)
53  ui(8, non_intra_quantiser_matrix[i]);
54  }
55 
56  return 0;
57 }
58 
60  MPEG2RawUserData *current)
61 {
62  size_t k;
63  int err;
64 
65  HEADER("User Data");
66 
67  ui(8, user_data_start_code);
68 
69 #ifdef READ
70  k = get_bits_left(rw);
71  av_assert0(k % 8 == 0);
72  current->user_data_length = k /= 8;
73  if (k > 0) {
74  current->user_data = av_malloc(k);
75  if (!current->user_data)
76  return AVERROR(ENOMEM);
77  }
78 #endif
79 
80  for (k = 0; k < current->user_data_length; k++)
81  xui(8, user_data, current->user_data[k]);
82 
83  return 0;
84 }
85 
88 {
90  int err;
91 
92  HEADER("Sequence Extension");
93 
94  ui(8, profile_and_level_indication);
95  ui(1, progressive_sequence);
96  ui(2, chroma_format);
97  ui(2, horizontal_size_extension);
98  ui(2, vertical_size_extension);
99 
100  mpeg2->horizontal_size = (mpeg2->horizontal_size & 0xfff) |
101  current->horizontal_size_extension << 12;
102  mpeg2->vertical_size = (mpeg2->vertical_size & 0xfff) |
103  current->vertical_size_extension << 12;
104  mpeg2->progressive_sequence = current->progressive_sequence;
105 
106  ui(12, bit_rate_extension);
107  marker_bit();
108  ui(8, vbv_buffer_size_extension);
109  ui(1, low_delay);
110  ui(2, frame_rate_extension_n);
111  ui(5, frame_rate_extension_d);
112 
113  return 0;
114 }
115 
118 {
119  int err;
120 
121  HEADER("Sequence Display Extension");
122 
123  ui(3, video_format);
124 
125  ui(1, colour_description);
126  if (current->colour_description) {
127  ui(8, colour_primaries);
129  ui(8, matrix_coefficients);
130  }
131 
132  ui(14, display_horizontal_size);
133  marker_bit();
134  ui(14, display_vertical_size);
135 
136  return 0;
137 }
138 
141 {
142  int err;
143 
144  HEADER("Group of Pictures Header");
145 
146  ui(8, group_start_code);
147 
148  ui(25, time_code);
149  ui(1, closed_gop);
150  ui(1, broken_link);
151 
152  return 0;
153 }
154 
156  MPEG2RawPictureHeader *current)
157 {
158  int err;
159 
160  HEADER("Picture Header");
161 
162  ui(8, picture_start_code);
163 
164  ui(10, temporal_reference);
165  ui(3, picture_coding_type);
166  ui(16, vbv_delay);
167 
168  if (current->picture_coding_type == 2 ||
169  current->picture_coding_type == 3) {
170  ui(1, full_pel_forward_vector);
171  ui(3, forward_f_code);
172  }
173 
174  if (current->picture_coding_type == 3) {
175  ui(1, full_pel_backward_vector);
176  ui(3, backward_f_code);
177  }
178 
179  ui(1, extra_bit_picture);
180 
181  return 0;
182 }
183 
186 {
188  int err;
189 
190  HEADER("Picture Coding Extension");
191 
192  ui(4, f_code[0][0]);
193  ui(4, f_code[0][1]);
194  ui(4, f_code[1][0]);
195  ui(4, f_code[1][1]);
196 
197  ui(2, intra_dc_precision);
198  ui(2, picture_structure);
199  ui(1, top_field_first);
200  ui(1, frame_pred_frame_dct);
201  ui(1, concealment_motion_vectors);
202  ui(1, q_scale_type);
203  ui(1, intra_vlc_format);
204  ui(1, alternate_scan);
205  ui(1, repeat_first_field);
206  ui(1, chroma_420_type);
207  ui(1, progressive_frame);
208 
209  if (mpeg2->progressive_sequence) {
210  if (current->repeat_first_field) {
211  if (current->top_field_first)
213  else
215  } else {
217  }
218  } else {
219  if (current->picture_structure == 1 || // Top field.
220  current->picture_structure == 2) { // Bottom field.
222  } else {
223  if (current->repeat_first_field)
225  else
227  }
228  }
229 
230  ui(1, composite_display_flag);
231  if (current->composite_display_flag) {
232  ui(1, v_axis);
233  ui(3, field_sequence);
234  ui(1, sub_carrier);
235  ui(7, burst_amplitude);
236  ui(8, sub_carrier_phase);
237  }
238 
239  return 0;
240 }
241 
244 {
245  int err, i;
246 
247  HEADER("Quant Matrix Extension");
248 
249  ui(1, load_intra_quantiser_matrix);
250  if (current->load_intra_quantiser_matrix) {
251  for (i = 0; i < 64; i++)
252  ui(8, intra_quantiser_matrix[i]);
253  }
254 
255  ui(1, load_non_intra_quantiser_matrix);
256  if (current->load_non_intra_quantiser_matrix) {
257  for (i = 0; i < 64; i++)
258  ui(8, non_intra_quantiser_matrix[i]);
259  }
260 
261  ui(1, load_chroma_intra_quantiser_matrix);
262  if (current->load_chroma_intra_quantiser_matrix) {
263  for (i = 0; i < 64; i++)
264  ui(8, intra_quantiser_matrix[i]);
265  }
266 
267  ui(1, load_chroma_non_intra_quantiser_matrix);
268  if (current->load_chroma_non_intra_quantiser_matrix) {
269  for (i = 0; i < 64; i++)
270  ui(8, chroma_non_intra_quantiser_matrix[i]);
271  }
272 
273  return 0;
274 }
275 
278 {
280  int err, i;
281 
282  HEADER("Picture Display Extension");
283 
284  for (i = 0; i < mpeg2->number_of_frame_centre_offsets; i++) {
285  ui(16, frame_centre_horizontal_offset[i]);
286  marker_bit();
287  ui(16, frame_centre_vertical_offset[i]);
288  marker_bit();
289  }
290 
291  return 0;
292 }
293 
295  MPEG2RawExtensionData *current)
296 {
297  int err;
298 
299  HEADER("Extension Data");
300 
301  ui(8, extension_start_code);
302  ui(4, extension_start_code_identifier);
303 
304  switch (current->extension_start_code_identifier) {
305  case 1:
306  return FUNC(sequence_extension)
307  (ctx, rw, &current->data.sequence);
308  case 2:
310  (ctx, rw, &current->data.sequence_display);
311  case 3:
313  (ctx, rw, &current->data.quant_matrix);
314  case 7:
316  (ctx, rw, &current->data.picture_display);
317  case 8:
319  (ctx, rw, &current->data.picture_coding);
320  default:
321  av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid extension ID %d.\n",
322  current->extension_start_code_identifier);
323  return AVERROR_INVALIDDATA;
324  }
325 }
326 
328  MPEG2RawSliceHeader *current)
329 {
331  int err;
332 
333  HEADER("Slice Header");
334 
335  ui(8, slice_vertical_position);
336 
337  if (mpeg2->vertical_size > 2800)
338  ui(3, slice_vertical_position_extension);
339  if (mpeg2->scalable) {
340  if (mpeg2->scalable_mode == 0)
341  ui(7, priority_breakpoint);
342  }
343 
344  ui(5, quantiser_scale_code);
345 
346  if (nextbits(1, 1, current->slice_extension_flag)) {
347  ui(1, slice_extension_flag);
348  ui(1, intra_slice);
349  ui(1, slice_picture_id_enable);
350  ui(6, slice_picture_id);
351 
352  {
353  size_t k;
354 #ifdef READ
356  uint8_t bit;
357  start = *rw;
358  for (k = 0; nextbits(1, 1, bit); k++)
359  skip_bits(rw, 8);
360  current->extra_information_length = k;
361  if (k > 0) {
362  *rw = start;
363  current->extra_information =
364  av_malloc(current->extra_information_length);
365  if (!current->extra_information)
366  return AVERROR(ENOMEM);
367  for (k = 0; k < current->extra_information_length; k++) {
368  xui(1, extra_bit_slice, bit);
369  xui(8, extra_information_slice,
370  current->extra_information[k]);
371  }
372  }
373 #else
374  for (k = 0; k < current->extra_information_length; k++) {
375  xui(1, extra_bit_slice, 1);
376  xui(8, extra_information_slice, current->extra_information[k]);
377  }
378 #endif
379  }
380  }
381  ui(1, extra_bit_slice);
382 
383  return 0;
384 }
#define nextbits(width, compare, var)
Definition: cbs_mpeg2.c:92
static int FUNC() picture_coding_extension(CodedBitstreamContext *ctx, RWContext *rw, MPEG2RawPictureCodingExtension *current)
uint8_t number_of_frame_centre_offsets
Definition: cbs_mpeg2.h:216
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
static int FUNC() extension_data(CodedBitstreamContext *ctx, RWContext *rw, MPEG2RawExtensionData *current)
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
uint8_t
#define av_malloc(s)
static int FUNC() picture_display_extension(CodedBitstreamContext *ctx, RWContext *rw, MPEG2RawPictureDisplayExtension *current)
#define av_log(a,...)
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:587
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
#define FUNC(a)
#define AVERROR(e)
Definition: error.h:43
AVS_Value void * user_data
Definition: avisynth_c.h:699
static int FUNC() sequence_display_extension(CodedBitstreamContext *ctx, RWContext *rw, MPEG2RawSequenceDisplayExtension *current)
static int FUNC() quant_matrix_extension(CodedBitstreamContext *ctx, RWContext *rw, MPEG2RawQuantMatrixExtension *current)
static int FUNC() picture_header(CodedBitstreamContext *ctx, RWContext *rw, MPEG2RawPictureHeader *current)
static const struct TransferCharacteristics transfer_characteristics[AVCOL_TRC_NB]
AVFormatContext * ctx
Definition: movenc.c:48
static int FUNC() slice_header(CodedBitstreamContext *ctx, RWContext *rw, MPEG2RawSliceHeader *current)
static int FUNC() sequence_extension(CodedBitstreamContext *ctx, RWContext *rw, MPEG2RawSequenceExtension *current)
#define marker_bit()
Definition: cbs_mpeg2.c:88
static int FUNC() sequence_header(CodedBitstreamContext *ctx, RWContext *rw, MPEG2RawSequenceHeader *current)
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:306
Context structure for coded bitstream operations.
Definition: cbs.h:145
#define xui(width, name, var)
Definition: cbs_mpeg2.c:80
#define ui(width, name)
Definition: cbs_mpeg2.c:85
static int FUNC() group_of_pictures_header(CodedBitstreamContext *ctx, RWContext *rw, MPEG2RawGroupOfPicturesHeader *current)
#define HEADER(name)
Definition: cbs_h2645.c:228
void * priv_data
Format private data.
Definition: avformat.h:1353
#define RWContext
Definition: cbs_h2645.c:325
void INT64 start
Definition: avisynth_c.h:690