FFmpeg
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  uir(12, horizontal_size_value);
30  uir(12, vertical_size_value);
31 
32  mpeg2->horizontal_size = current->horizontal_size_value;
33  mpeg2->vertical_size = current->vertical_size_value;
34 
35  uir(4, aspect_ratio_information);
36  uir(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  uirs(8, intra_quantiser_matrix[i], 1, 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  uirs(8, non_intra_quantiser_matrix[i], 1, 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_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;
78  }
79 #endif
80 
81  for (k = 0; k < current->user_data_length; k++)
82  uis(8, user_data[k], 1, 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 #ifdef READ
129 #define READ_AND_PATCH(name) do { \
130  ui(8, name); \
131  if (current->name == 0) { \
132  current->name = 2; \
133  av_log(ctx->log_ctx, AV_LOG_WARNING, "%s in a sequence display " \
134  "extension had the invalid value 0. Setting it to 2 " \
135  "(meaning unknown) instead.\n", #name); \
136  } \
137  } while (0)
138  READ_AND_PATCH(colour_primaries);
139  READ_AND_PATCH(transfer_characteristics);
140  READ_AND_PATCH(matrix_coefficients);
141 #undef READ_AND_PATCH
142 #else
143  uir(8, colour_primaries);
145  uir(8, matrix_coefficients);
146 #endif
147  } else {
148  infer(colour_primaries, 2);
150  infer(matrix_coefficients, 2);
151  }
152 
153  ui(14, display_horizontal_size);
154  marker_bit();
155  ui(14, display_vertical_size);
156 
157  return 0;
158 }
159 
162 {
163  int err;
164 
165  HEADER("Group of Pictures Header");
166 
167  ui(8, group_start_code);
168 
169  ui(25, time_code);
170  ui(1, closed_gop);
171  ui(1, broken_link);
172 
173  return 0;
174 }
175 
177  MPEG2RawExtraInformation *current,
178  const char *element_name, const char *marker_name)
179 {
180  int err;
181  size_t k;
182 #ifdef READ
183  GetBitContext start = *rw;
184  uint8_t bit;
185 
186  for (k = 0; nextbits(1, 1, bit); k++)
187  skip_bits(rw, 1 + 8);
188  current->extra_information_length = k;
189  if (k > 0) {
190  *rw = start;
191  current->extra_information_ref =
193  if (!current->extra_information_ref)
194  return AVERROR(ENOMEM);
195  current->extra_information = current->extra_information_ref->data;
196  }
197 #endif
198 
199  for (k = 0; k < current->extra_information_length; k++) {
200  bit(marker_name, 1);
201  xuia(8, element_name,
202  current->extra_information[k], 0, 255, 1, k);
203  }
204 
205  bit(marker_name, 0);
206 
207  return 0;
208 }
209 
211  MPEG2RawPictureHeader *current)
212 {
213  int err;
214 
215  HEADER("Picture Header");
216 
217  ui(8, picture_start_code);
218 
219  ui(10, temporal_reference);
220  uir(3, picture_coding_type);
221  ui(16, vbv_delay);
222 
223  if (current->picture_coding_type == 2 ||
224  current->picture_coding_type == 3) {
225  ui(1, full_pel_forward_vector);
226  ui(3, forward_f_code);
227  }
228 
229  if (current->picture_coding_type == 3) {
230  ui(1, full_pel_backward_vector);
231  ui(3, backward_f_code);
232  }
233 
234  CHECK(FUNC(extra_information)(ctx, rw, &current->extra_information_picture,
235  "extra_information_picture[k]", "extra_bit_picture"));
236 
237  return 0;
238 }
239 
242 {
244  int err;
245 
246  HEADER("Picture Coding Extension");
247 
248  uir(4, f_code[0][0]);
249  uir(4, f_code[0][1]);
250  uir(4, f_code[1][0]);
251  uir(4, f_code[1][1]);
252 
253  ui(2, intra_dc_precision);
254  ui(2, picture_structure);
255  ui(1, top_field_first);
256  ui(1, frame_pred_frame_dct);
257  ui(1, concealment_motion_vectors);
258  ui(1, q_scale_type);
259  ui(1, intra_vlc_format);
260  ui(1, alternate_scan);
261  ui(1, repeat_first_field);
262  ui(1, chroma_420_type);
263  ui(1, progressive_frame);
264 
265  if (mpeg2->progressive_sequence) {
266  if (current->repeat_first_field) {
267  if (current->top_field_first)
269  else
271  } else {
273  }
274  } else {
275  if (current->picture_structure == 1 || // Top field.
276  current->picture_structure == 2) { // Bottom field.
278  } else {
279  if (current->repeat_first_field)
281  else
283  }
284  }
285 
286  ui(1, composite_display_flag);
287  if (current->composite_display_flag) {
288  ui(1, v_axis);
289  ui(3, field_sequence);
290  ui(1, sub_carrier);
291  ui(7, burst_amplitude);
292  ui(8, sub_carrier_phase);
293  }
294 
295  return 0;
296 }
297 
300 {
301  int err, i;
302 
303  HEADER("Quant Matrix Extension");
304 
305  ui(1, load_intra_quantiser_matrix);
306  if (current->load_intra_quantiser_matrix) {
307  for (i = 0; i < 64; i++)
308  uirs(8, intra_quantiser_matrix[i], 1, i);
309  }
310 
311  ui(1, load_non_intra_quantiser_matrix);
312  if (current->load_non_intra_quantiser_matrix) {
313  for (i = 0; i < 64; i++)
314  uirs(8, non_intra_quantiser_matrix[i], 1, i);
315  }
316 
317  ui(1, load_chroma_intra_quantiser_matrix);
318  if (current->load_chroma_intra_quantiser_matrix) {
319  for (i = 0; i < 64; i++)
320  uirs(8, intra_quantiser_matrix[i], 1, i);
321  }
322 
323  ui(1, load_chroma_non_intra_quantiser_matrix);
324  if (current->load_chroma_non_intra_quantiser_matrix) {
325  for (i = 0; i < 64; i++)
326  uirs(8, chroma_non_intra_quantiser_matrix[i], 1, i);
327  }
328 
329  return 0;
330 }
331 
334 {
336  int err, i;
337 
338  HEADER("Picture Display Extension");
339 
340  for (i = 0; i < mpeg2->number_of_frame_centre_offsets; i++) {
341  sis(16, frame_centre_horizontal_offset[i], 1, i);
342  marker_bit();
343  sis(16, frame_centre_vertical_offset[i], 1, i);
344  marker_bit();
345  }
346 
347  return 0;
348 }
349 
351  MPEG2RawExtensionData *current)
352 {
353  int err;
354 
355  HEADER("Extension Data");
356 
357  ui(8, extension_start_code);
358  ui(4, extension_start_code_identifier);
359 
360  switch (current->extension_start_code_identifier) {
362  return FUNC(sequence_extension)
363  (ctx, rw, &current->data.sequence);
366  (ctx, rw, &current->data.sequence_display);
369  (ctx, rw, &current->data.quant_matrix);
372  (ctx, rw, &current->data.picture_display);
375  (ctx, rw, &current->data.picture_coding);
376  default:
377  av_log(ctx->log_ctx, AV_LOG_ERROR, "Extension ID %d not supported.\n",
378  current->extension_start_code_identifier);
379  return AVERROR_PATCHWELCOME;
380  }
381 }
382 
384  MPEG2RawSliceHeader *current)
385 {
387  int err;
388 
389  HEADER("Slice Header");
390 
391  ui(8, slice_vertical_position);
392 
393  if (mpeg2->vertical_size > 2800)
394  ui(3, slice_vertical_position_extension);
395  if (mpeg2->scalable) {
396  if (mpeg2->scalable_mode == 0)
397  ui(7, priority_breakpoint);
398  }
399 
400  uir(5, quantiser_scale_code);
401 
402  if (nextbits(1, 1, current->slice_extension_flag)) {
403  ui(1, slice_extension_flag);
404  ui(1, intra_slice);
405  ui(1, slice_picture_id_enable);
406  ui(6, slice_picture_id);
407  }
408 
409  CHECK(FUNC(extra_information)(ctx, rw, &current->extra_information_slice,
410  "extra_information_slice[k]", "extra_bit_slice"));
411 
412  return 0;
413 }
414 
416  MPEG2RawSequenceEnd *current)
417 {
418  int err;
419 
420  HEADER("Sequence End");
421 
422  ui(8, sequence_end_code);
423 
424  return 0;
425 }
MPEG2RawPictureCodingExtension
Definition: cbs_mpeg2.h:138
quant_matrix_extension
static int FUNC() quant_matrix_extension(CodedBitstreamContext *ctx, RWContext *rw, MPEG2RawQuantMatrixExtension *current)
Definition: cbs_mpeg2_syntax_template.c:298
get_bits_left
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:849
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
extra_information
static int FUNC() extra_information(CodedBitstreamContext *ctx, RWContext *rw, MPEG2RawExtraInformation *current, const char *element_name, const char *marker_name)
Definition: cbs_mpeg2_syntax_template.c:176
HEADER
#define HEADER(name)
Definition: cbs_av1.c:537
MPEG2RawPictureHeader
Definition: cbs_mpeg2.h:123
infer
#define infer(name, value)
Definition: cbs_av1.c:713
CodedBitstreamContext
Context structure for coded bitstream operations.
Definition: cbs.h:168
MPEG2_EXTENSION_SEQUENCE
@ MPEG2_EXTENSION_SEQUENCE
Definition: cbs_mpeg2.h:45
picture_header
static int FUNC() picture_header(CodedBitstreamContext *ctx, RWContext *rw, MPEG2RawPictureHeader *current)
Definition: cbs_mpeg2_syntax_template.c:210
av_buffer_allocz
AVBufferRef * av_buffer_allocz(int size)
Same as av_buffer_alloc(), except the returned buffer will be initialized to zero.
Definition: buffer.c:83
MPEG2RawExtensionData
Definition: cbs_mpeg2.h:177
MPEG2RawUserData
Definition: cbs_mpeg2.h:76
bit
#define bit(string, value)
Definition: cbs_mpeg2.c:58
skip_bits
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:467
CHECK
CHECK(-1) CHECK(-2) }} }} CHECK(1) CHECK(2) }} }} } if(diff0+diff1 > 0) temp -
sequence_display_extension
static int FUNC() sequence_display_extension(CodedBitstreamContext *ctx, RWContext *rw, MPEG2RawSequenceDisplayExtension *current)
Definition: cbs_mpeg2_syntax_template.c:117
MPEG2RawGroupOfPicturesHeader
Definition: cbs_mpeg2.h:109
GetBitContext
Definition: get_bits.h:61
MPEG2_EXTENSION_PICTURE_CODING
@ MPEG2_EXTENSION_PICTURE_CODING
Definition: cbs_mpeg2.h:51
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
MPEG2RawSequenceExtension
Definition: cbs_mpeg2.h:84
CodedBitstreamMPEG2Context::scalable_mode
uint8_t scalable_mode
Definition: cbs_mpeg2.h:225
MPEG2RawSequenceDisplayExtension
Definition: cbs_mpeg2.h:97
MPEG2_EXTENSION_QUANT_MATRIX
@ MPEG2_EXTENSION_QUANT_MATRIX
Definition: cbs_mpeg2.h:47
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
ctx
AVFormatContext * ctx
Definition: movenc.c:48
MPEG2_EXTENSION_SEQUENCE_DISPLAY
@ MPEG2_EXTENSION_SEQUENCE_DISPLAY
Definition: cbs_mpeg2.h:46
nextbits
#define nextbits(width, compare, var)
Definition: cbs_mpeg2.c:121
MPEG2RawExtraInformation
Definition: cbs_mpeg2.h:117
AVERROR_PATCHWELCOME
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
extension_data
static int FUNC() extension_data(CodedBitstreamContext *ctx, RWContext *rw, MPEG2RawExtensionData *current)
Definition: cbs_mpeg2_syntax_template.c:350
sequence_extension
static int FUNC() sequence_extension(CodedBitstreamContext *ctx, RWContext *rw, MPEG2RawSequenceExtension *current)
Definition: cbs_mpeg2_syntax_template.c:87
CodedBitstreamMPEG2Context::horizontal_size
uint16_t horizontal_size
Definition: cbs_mpeg2.h:222
uirs
#define uirs(width, name, subs,...)
Definition: cbs_mpeg2.c:49
xuia
#define xuia(width, string, var, range_min, range_max, subs,...)
Definition: cbs_mpeg2.c:108
CodedBitstreamMPEG2Context::number_of_frame_centre_offsets
uint8_t number_of_frame_centre_offsets
Definition: cbs_mpeg2.h:227
transfer_characteristics
static const struct TransferCharacteristics transfer_characteristics[AVCOL_TRC_NB]
Definition: vf_colorspace.c:176
user_data
static int FUNC() user_data(CodedBitstreamContext *ctx, RWContext *rw, MPEG2RawUserData *current)
Definition: cbs_mpeg2_syntax_template.c:59
MPEG2_EXTENSION_PICTURE_DISPLAY
@ MPEG2_EXTENSION_PICTURE_DISPLAY
Definition: cbs_mpeg2.h:50
MPEG2RawSequenceEnd
Definition: cbs_mpeg2.h:215
MPEG2RawSequenceHeader
Definition: cbs_mpeg2.h:59
slice_header
static int FUNC() slice_header(CodedBitstreamContext *ctx, RWContext *rw, MPEG2RawSliceHeader *current)
Definition: cbs_mpeg2_syntax_template.c:383
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
CodedBitstreamMPEG2Context::progressive_sequence
uint8_t progressive_sequence
Definition: cbs_mpeg2.h:226
uint8_t
uint8_t
Definition: audio_convert.c:194
sequence_end
static int FUNC() sequence_end(CodedBitstreamContext *ctx, RWContext *rw, MPEG2RawSequenceEnd *current)
Definition: cbs_mpeg2_syntax_template.c:415
CodedBitstreamMPEG2Context::vertical_size
uint16_t vertical_size
Definition: cbs_mpeg2.h:223
CodedBitstreamMPEG2Context
Definition: cbs_mpeg2.h:220
marker_bit
#define marker_bit()
Definition: cbs_mpeg2.c:56
MPEG2RawQuantMatrixExtension
Definition: cbs_mpeg2.h:161
sis
#define sis(width, name, subs,...)
Definition: cbs_mpeg2.c:53
AV_INPUT_BUFFER_PADDING_SIZE
#define AV_INPUT_BUFFER_PADDING_SIZE
Definition: avcodec.h:215
FUNC
#define FUNC(a)
Definition: bit_depth_template.c:104
picture_display_extension
static int FUNC() picture_display_extension(CodedBitstreamContext *ctx, RWContext *rw, MPEG2RawPictureDisplayExtension *current)
Definition: cbs_mpeg2_syntax_template.c:332
uis
#define uis(width, name, subs,...)
Definition: cbs_mpeg2.c:47
MPEG2RawSliceHeader
Definition: cbs_mpeg2.h:190
CodedBitstreamMPEG2Context::scalable
uint8_t scalable
Definition: cbs_mpeg2.h:224
uir
#define uir(width, name)
Definition: cbs_mpeg2.c:45
group_of_pictures_header
static int FUNC() group_of_pictures_header(CodedBitstreamContext *ctx, RWContext *rw, MPEG2RawGroupOfPicturesHeader *current)
Definition: cbs_mpeg2_syntax_template.c:160
MPEG2RawPictureDisplayExtension
Definition: cbs_mpeg2.h:172
picture_coding_extension
static int FUNC() picture_coding_extension(CodedBitstreamContext *ctx, RWContext *rw, MPEG2RawPictureCodingExtension *current)
Definition: cbs_mpeg2_syntax_template.c:240
sequence_header
static int FUNC() sequence_header(CodedBitstreamContext *ctx, RWContext *rw, MPEG2RawSequenceHeader *current)
Definition: cbs_mpeg2_syntax_template.c:19
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
RWContext
#define RWContext
Definition: cbs_av1.c:666
AVFormatContext::priv_data
void * priv_data
Format private data.
Definition: avformat.h:1363
ui
#define ui(width, name)
Definition: cbs_mpeg2.c:43