FFmpeg  4.0
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_ref = av_buffer_alloc(k);
75  if (!current->user_data_ref)
76  return AVERROR(ENOMEM);
77  current->user_data = current->user_data_ref->data;
78  }
79 #endif
80 
81  for (k = 0; k < current->user_data_length; k++)
82  xui(8, user_data, current->user_data[k]);
83 
84  return 0;
85 }
86 
89 {
91  int err;
92 
93  HEADER("Sequence Extension");
94 
95  ui(8, profile_and_level_indication);
96  ui(1, progressive_sequence);
97  ui(2, chroma_format);
98  ui(2, horizontal_size_extension);
99  ui(2, vertical_size_extension);
100 
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;
106 
107  ui(12, bit_rate_extension);
108  marker_bit();
109  ui(8, vbv_buffer_size_extension);
110  ui(1, low_delay);
111  ui(2, frame_rate_extension_n);
112  ui(5, frame_rate_extension_d);
113 
114  return 0;
115 }
116 
119 {
120  int err;
121 
122  HEADER("Sequence Display Extension");
123 
124  ui(3, video_format);
125 
126  ui(1, colour_description);
127  if (current->colour_description) {
128  ui(8, colour_primaries);
130  ui(8, matrix_coefficients);
131  }
132 
133  ui(14, display_horizontal_size);
134  marker_bit();
135  ui(14, display_vertical_size);
136 
137  return 0;
138 }
139 
142 {
143  int err;
144 
145  HEADER("Group of Pictures Header");
146 
147  ui(8, group_start_code);
148 
149  ui(25, time_code);
150  ui(1, closed_gop);
151  ui(1, broken_link);
152 
153  return 0;
154 }
155 
157  MPEG2RawPictureHeader *current)
158 {
159  int err;
160 
161  HEADER("Picture Header");
162 
163  ui(8, picture_start_code);
164 
165  ui(10, temporal_reference);
166  ui(3, picture_coding_type);
167  ui(16, vbv_delay);
168 
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);
173  }
174 
175  if (current->picture_coding_type == 3) {
176  ui(1, full_pel_backward_vector);
177  ui(3, backward_f_code);
178  }
179 
180  ui(1, extra_bit_picture);
181 
182  return 0;
183 }
184 
187 {
189  int err;
190 
191  HEADER("Picture Coding Extension");
192 
193  ui(4, f_code[0][0]);
194  ui(4, f_code[0][1]);
195  ui(4, f_code[1][0]);
196  ui(4, f_code[1][1]);
197 
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);
203  ui(1, q_scale_type);
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);
209 
210  if (mpeg2->progressive_sequence) {
211  if (current->repeat_first_field) {
212  if (current->top_field_first)
214  else
216  } else {
218  }
219  } else {
220  if (current->picture_structure == 1 || // Top field.
221  current->picture_structure == 2) { // Bottom field.
223  } else {
224  if (current->repeat_first_field)
226  else
228  }
229  }
230 
231  ui(1, composite_display_flag);
232  if (current->composite_display_flag) {
233  ui(1, v_axis);
234  ui(3, field_sequence);
235  ui(1, sub_carrier);
236  ui(7, burst_amplitude);
237  ui(8, sub_carrier_phase);
238  }
239 
240  return 0;
241 }
242 
245 {
246  int err, i;
247 
248  HEADER("Quant Matrix Extension");
249 
250  ui(1, load_intra_quantiser_matrix);
251  if (current->load_intra_quantiser_matrix) {
252  for (i = 0; i < 64; i++)
253  ui(8, intra_quantiser_matrix[i]);
254  }
255 
256  ui(1, load_non_intra_quantiser_matrix);
257  if (current->load_non_intra_quantiser_matrix) {
258  for (i = 0; i < 64; i++)
259  ui(8, non_intra_quantiser_matrix[i]);
260  }
261 
262  ui(1, load_chroma_intra_quantiser_matrix);
263  if (current->load_chroma_intra_quantiser_matrix) {
264  for (i = 0; i < 64; i++)
265  ui(8, intra_quantiser_matrix[i]);
266  }
267 
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  ui(8, chroma_non_intra_quantiser_matrix[i]);
272  }
273 
274  return 0;
275 }
276 
279 {
281  int err, i;
282 
283  HEADER("Picture Display Extension");
284 
285  for (i = 0; i < mpeg2->number_of_frame_centre_offsets; i++) {
286  ui(16, frame_centre_horizontal_offset[i]);
287  marker_bit();
288  ui(16, frame_centre_vertical_offset[i]);
289  marker_bit();
290  }
291 
292  return 0;
293 }
294 
296  MPEG2RawExtensionData *current)
297 {
298  int err;
299 
300  HEADER("Extension Data");
301 
302  ui(8, extension_start_code);
303  ui(4, extension_start_code_identifier);
304 
305  switch (current->extension_start_code_identifier) {
306  case 1:
307  return FUNC(sequence_extension)
308  (ctx, rw, &current->data.sequence);
309  case 2:
311  (ctx, rw, &current->data.sequence_display);
312  case 3:
314  (ctx, rw, &current->data.quant_matrix);
315  case 7:
317  (ctx, rw, &current->data.picture_display);
318  case 8:
320  (ctx, rw, &current->data.picture_coding);
321  default:
322  av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid extension ID %d.\n",
323  current->extension_start_code_identifier);
324  return AVERROR_INVALIDDATA;
325  }
326 }
327 
329  MPEG2RawSliceHeader *current)
330 {
332  int err;
333 
334  HEADER("Slice Header");
335 
336  ui(8, slice_vertical_position);
337 
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);
343  }
344 
345  ui(5, quantiser_scale_code);
346 
347  if (nextbits(1, 1, current->slice_extension_flag)) {
348  ui(1, slice_extension_flag);
349  ui(1, intra_slice);
350  ui(1, slice_picture_id_enable);
351  ui(6, slice_picture_id);
352 
353  {
354  size_t k;
355 #ifdef READ
357  uint8_t bit;
358  start = *rw;
359  for (k = 0; nextbits(1, 1, bit); k++)
360  skip_bits(rw, 8);
361  current->extra_information_length = k;
362  if (k > 0) {
363  *rw = start;
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);
370  xui(8, extra_information_slice,
371  current->extra_information[k]);
372  }
373  }
374 #else
375  for (k = 0; k < current->extra_information_length; k++) {
376  xui(1, extra_bit_slice, 1);
377  xui(8, extra_information_slice, current->extra_information[k]);
378  }
379 #endif
380  }
381  }
382  ui(1, extra_bit_slice);
383 
384  return 0;
385 }
#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:221
#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:596
#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
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]
static int FUNC() user_data(CodedBitstreamContext *ctx, RWContext *rw, MPEG2RawUserData *current)
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)
AVBufferRef * av_buffer_alloc(int size)
Allocate an AVBuffer of the given size using av_malloc().
Definition: buffer.c:67
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:314
Context structure for coded bitstream operations.
Definition: cbs.h:156
#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:1370
#define RWContext
Definition: cbs_h2645.c:326
void INT64 start
Definition: avisynth_c.h:690