avformat/mpeg: demux ivtv captions
[ffmpeg.git] / libavcodec / cbs_sei_syntax_template.c
blob0205bb47aa3f415a0984eb6c8e89eae82a89d265
1 /*
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 SEI_FUNC(filler_payload, (CodedBitstreamContext *ctx, RWContext *rw,
20 SEIRawFillerPayload *current,
21 SEIMessageState *state))
23 int err, i;
25 HEADER("Filler Payload");
27 #ifdef READ
28 current->payload_size = state->payload_size;
29 #endif
31 for (i = 0; i < current->payload_size; i++)
32 fixed(8, ff_byte, 0xff);
34 return 0;
37 SEI_FUNC(user_data_registered, (CodedBitstreamContext *ctx, RWContext *rw,
38 SEIRawUserDataRegistered *current,
39 SEIMessageState *state))
41 int err, i, j;
43 HEADER("User Data Registered ITU-T T.35");
45 u(8, itu_t_t35_country_code, 0x00, 0xff);
46 if (current->itu_t_t35_country_code != 0xff)
47 i = 1;
48 else {
49 u(8, itu_t_t35_country_code_extension_byte, 0x00, 0xff);
50 i = 2;
53 #ifdef READ
54 if (state->payload_size < i) {
55 av_log(ctx->log_ctx, AV_LOG_ERROR,
56 "Invalid SEI user data registered payload.\n");
57 return AVERROR_INVALIDDATA;
59 current->data_length = state->payload_size - i;
60 #endif
62 allocate(current->data, current->data_length);
63 for (j = 0; j < current->data_length; j++)
64 xu(8, itu_t_t35_payload_byte[], current->data[j], 0x00, 0xff, 1, i + j);
66 return 0;
69 SEI_FUNC(user_data_unregistered, (CodedBitstreamContext *ctx, RWContext *rw,
70 SEIRawUserDataUnregistered *current,
71 SEIMessageState *state))
73 int err, i;
75 HEADER("User Data Unregistered");
77 #ifdef READ
78 if (state->payload_size < 16) {
79 av_log(ctx->log_ctx, AV_LOG_ERROR,
80 "Invalid SEI user data unregistered payload.\n");
81 return AVERROR_INVALIDDATA;
83 current->data_length = state->payload_size - 16;
84 #endif
86 for (i = 0; i < 16; i++)
87 us(8, uuid_iso_iec_11578[i], 0x00, 0xff, 1, i);
89 allocate(current->data, current->data_length);
91 for (i = 0; i < current->data_length; i++)
92 xu(8, user_data_payload_byte[i], current->data[i], 0x00, 0xff, 1, i);
94 return 0;
97 SEI_FUNC(frame_packing_arrangement, (CodedBitstreamContext *ctx, RWContext *rw,
98 SEIRawFramePackingArrangement *current,
99 SEIMessageState *unused))
101 int err;
103 HEADER("Frame Packing Arrangement");
105 ue(fp_arrangement_id, 0, MAX_UINT_BITS(31));
106 flag(fp_arrangement_cancel_flag);
107 if (!current->fp_arrangement_cancel_flag) {
108 u(7, fp_arrangement_type, 3, 5);
109 flag(fp_quincunx_sampling_flag);
110 u(6, fp_content_interpretation_type, 0, 2);
111 flag(fp_spatial_flipping_flag);
112 flag(fp_frame0_flipped_flag);
113 flag(fp_field_views_flag);
114 flag(fp_current_frame_is_frame0_flag);
115 flag(fp_frame0_self_contained_flag);
116 flag(fp_frame1_self_contained_flag);
117 if (!current->fp_quincunx_sampling_flag && current->fp_arrangement_type != 5) {
118 ub(4, fp_frame0_grid_position_x);
119 ub(4, fp_frame0_grid_position_y);
120 ub(4, fp_frame1_grid_position_x);
121 ub(4, fp_frame1_grid_position_y);
123 fixed(8, fp_arrangement_reserved_byte, 0);
124 flag(fp_arrangement_persistence_flag);
126 flag(fp_upsampled_aspect_ratio_flag);
128 return 0;
131 SEI_FUNC(decoded_picture_hash, (CodedBitstreamContext *ctx,
132 RWContext *rw,
133 SEIRawDecodedPictureHash *current,
134 SEIMessageState *unused))
136 int err, c_idx, i;
138 HEADER("Decoded Picture Hash");
140 u(8, dph_sei_hash_type, 0, 2);
141 flag(dph_sei_single_component_flag);
142 ub(7, dph_sei_reserved_zero_7bits);
144 for (c_idx = 0; c_idx < (current->dph_sei_single_component_flag ? 1 : 3);
145 c_idx++) {
146 if (current->dph_sei_hash_type == 0) {
147 for (i = 0; i < 16; i++)
148 us(8, dph_sei_picture_md5[c_idx][i], 0x00, 0xff, 2, c_idx, i);
149 } else if (current->dph_sei_hash_type == 1) {
150 us(16, dph_sei_picture_crc[c_idx], 0x0000, 0xffff, 1, c_idx);
151 } else if (current->dph_sei_hash_type == 2) {
152 us(32, dph_sei_picture_checksum[c_idx], 0x00000000, 0xffffffff, 1,
153 c_idx);
156 return 0;
159 SEI_FUNC(mastering_display_colour_volume,
160 (CodedBitstreamContext *ctx, RWContext *rw,
161 SEIRawMasteringDisplayColourVolume *current,
162 SEIMessageState *state))
164 int err, c;
166 HEADER("Mastering Display Colour Volume");
168 for (c = 0; c < 3; c++) {
169 ubs(16, display_primaries_x[c], 1, c);
170 ubs(16, display_primaries_y[c], 1, c);
173 ub(16, white_point_x);
174 ub(16, white_point_y);
176 ub(32, max_display_mastering_luminance);
177 ub(32, min_display_mastering_luminance);
179 return 0;
182 SEI_FUNC(content_light_level_info, (CodedBitstreamContext *ctx, RWContext *rw,
183 SEIRawContentLightLevelInfo *current,
184 SEIMessageState *state))
186 int err;
188 HEADER("Content Light Level Information");
190 ub(16, max_content_light_level);
191 ub(16, max_pic_average_light_level);
193 return 0;
196 SEI_FUNC(alternative_transfer_characteristics,
197 (CodedBitstreamContext *ctx, RWContext *rw,
198 SEIRawAlternativeTransferCharacteristics *current,
199 SEIMessageState *state))
201 int err;
203 HEADER("Alternative Transfer Characteristics");
205 ub(8, preferred_transfer_characteristics);
207 return 0;
210 SEI_FUNC(ambient_viewing_environment,
211 (CodedBitstreamContext *ctx, RWContext *rw,
212 SEIRawAmbientViewingEnvironment *current,
213 SEIMessageState *state))
215 static const uint16_t max_ambient_light_value = 50000;
216 int err;
218 HEADER("Ambient Viewing Environment");
220 u(32, ambient_illuminance, 1, MAX_UINT_BITS(32));
221 u(16, ambient_light_x, 0, max_ambient_light_value);
222 u(16, ambient_light_y, 0, max_ambient_light_value);
224 return 0;
227 static int FUNC(message)(CodedBitstreamContext *ctx, RWContext *rw,
228 SEIRawMessage *current)
230 const SEIMessageTypeDescriptor *desc;
231 int err, i;
233 desc = ff_cbs_sei_find_type(ctx, current->payload_type);
234 if (desc) {
235 SEIMessageState state = {
236 .payload_type = current->payload_type,
237 .payload_size = current->payload_size,
238 .extension_present = current->extension_bit_length > 0,
240 int start_position, current_position, bits_written;
242 #ifdef READ
243 CHECK(ff_cbs_sei_alloc_message_payload(current, desc));
244 #endif
246 start_position = bit_position(rw);
248 CHECK(desc->READWRITE(ctx, rw, current->payload, &state));
250 current_position = bit_position(rw);
251 bits_written = current_position - start_position;
253 if (byte_alignment(rw) || state.extension_present ||
254 bits_written < 8 * current->payload_size) {
255 size_t bits_left;
257 #ifdef READ
258 GetBitContext tmp = *rw;
259 int trailing_bits, trailing_zero_bits;
261 bits_left = 8 * current->payload_size - bits_written;
262 if (bits_left > 8)
263 skip_bits_long(&tmp, bits_left - 8);
264 trailing_bits = get_bits(&tmp, FFMIN(bits_left, 8));
265 if (trailing_bits == 0) {
266 // The trailing bits must contain a bit_equal_to_one, so
267 // they can't all be zero.
268 return AVERROR_INVALIDDATA;
270 trailing_zero_bits = ff_ctz(trailing_bits);
271 current->extension_bit_length =
272 bits_left - 1 - trailing_zero_bits;
273 #endif
275 if (current->extension_bit_length > 0) {
276 allocate(current->extension_data,
277 (current->extension_bit_length + 7) / 8);
279 bits_left = current->extension_bit_length;
280 for (i = 0; bits_left > 0; i++) {
281 int length = FFMIN(bits_left, 8);
282 xu(length, reserved_payload_extension_data,
283 current->extension_data[i],
284 0, MAX_UINT_BITS(length), 0);
285 bits_left -= length;
289 fixed(1, bit_equal_to_one, 1);
290 while (byte_alignment(rw))
291 fixed(1, bit_equal_to_zero, 0);
294 #ifdef WRITE
295 current->payload_size = (put_bits_count(rw) - start_position) / 8;
296 #endif
297 } else {
298 uint8_t *data;
300 #ifdef READ
301 allocate(current->payload_ref, current->payload_size);
302 current->payload = current->payload_ref;
303 #else
304 allocate(current->payload, current->payload_size);
305 #endif
306 data = current->payload;
308 for (i = 0; i < current->payload_size; i++)
309 xu(8, payload_byte[i], data[i], 0, 255, 1, i);
312 return 0;
315 static int FUNC(message_list)(CodedBitstreamContext *ctx, RWContext *rw,
316 SEIRawMessageList *current, int prefix)
318 SEIRawMessage *message;
319 int err, k;
321 #ifdef READ
322 for (k = 0;; k++) {
323 uint32_t payload_type = 0;
324 uint32_t payload_size = 0;
325 uint32_t tmp;
326 GetBitContext payload_gbc;
328 while (show_bits(rw, 8) == 0xff) {
329 fixed(8, ff_byte, 0xff);
330 payload_type += 255;
332 xu(8, last_payload_type_byte, tmp, 0, 254, 0);
333 payload_type += tmp;
335 while (show_bits(rw, 8) == 0xff) {
336 fixed(8, ff_byte, 0xff);
337 payload_size += 255;
339 xu(8, last_payload_size_byte, tmp, 0, 254, 0);
340 payload_size += tmp;
342 // There must be space remaining for both the payload and
343 // the trailing bits on the SEI NAL unit.
344 if (payload_size + 1 > get_bits_left(rw) / 8) {
345 av_log(ctx->log_ctx, AV_LOG_ERROR,
346 "Invalid SEI message: payload_size too large "
347 "(%"PRIu32" bytes).\n", payload_size);
348 return AVERROR_INVALIDDATA;
350 CHECK(init_get_bits(&payload_gbc, rw->buffer,
351 get_bits_count(rw) + 8 * payload_size));
352 skip_bits_long(&payload_gbc, get_bits_count(rw));
354 CHECK(ff_cbs_sei_list_add(current));
355 message = &current->messages[k];
357 message->payload_type = payload_type;
358 message->payload_size = payload_size;
360 CHECK(FUNC(message)(ctx, &payload_gbc, message));
362 skip_bits_long(rw, 8 * payload_size);
364 if (!cbs_h2645_read_more_rbsp_data(rw))
365 break;
367 #else
368 for (k = 0; k < current->nb_messages; k++) {
369 PutBitContext start_state;
370 uint32_t tmp;
371 int trace, i;
373 message = &current->messages[k];
375 // We write the payload twice in order to find the size. Trace
376 // output is switched off for the first write.
377 trace = ctx->trace_enable;
378 ctx->trace_enable = 0;
380 start_state = *rw;
381 for (i = 0; i < 2; i++) {
382 *rw = start_state;
384 tmp = message->payload_type;
385 while (tmp >= 255) {
386 fixed(8, ff_byte, 0xff);
387 tmp -= 255;
389 xu(8, last_payload_type_byte, tmp, 0, 254, 0);
391 tmp = message->payload_size;
392 while (tmp >= 255) {
393 fixed(8, ff_byte, 0xff);
394 tmp -= 255;
396 xu(8, last_payload_size_byte, tmp, 0, 254, 0);
398 err = FUNC(message)(ctx, rw, message);
399 ctx->trace_enable = trace;
400 if (err < 0)
401 return err;
404 #endif
406 return 0;