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(filler_payload)
20 (CodedBitstreamContext *ctx, RWContext *rw,
21 SEIRawFillerPayload *current, SEIMessageState *state)
25 HEADER("Filler Payload");
28 current->payload_size = state->payload_size;
31 for (i = 0; i < current->payload_size; i++)
32 fixed(8, ff_byte, 0xff);
37 static int FUNC(user_data_registered)
38 (CodedBitstreamContext *ctx, RWContext *rw,
39 SEIRawUserDataRegistered *current, SEIMessageState *state)
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)
49 u(8, itu_t_t35_country_code_extension_byte, 0x00, 0xff);
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;
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);
69 static int FUNC(user_data_unregistered)
70 (CodedBitstreamContext *ctx, RWContext *rw,
71 SEIRawUserDataUnregistered *current, SEIMessageState *state)
75 HEADER("User Data Unregistered");
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;
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);
97 static int FUNC(mastering_display_colour_volume)
98 (CodedBitstreamContext *ctx, RWContext *rw,
99 SEIRawMasteringDisplayColourVolume *current, SEIMessageState *state)
103 HEADER("Mastering Display Colour Volume");
105 for (c = 0; c < 3; c++) {
106 ubs(16, display_primaries_x[c], 1, c);
107 ubs(16, display_primaries_y[c], 1, c);
110 ub(16, white_point_x);
111 ub(16, white_point_y);
113 ub(32, max_display_mastering_luminance);
114 ub(32, min_display_mastering_luminance);
119 static int FUNC(content_light_level_info)
120 (CodedBitstreamContext *ctx, RWContext *rw,
121 SEIRawContentLightLevelInfo *current, SEIMessageState *state)
125 HEADER("Content Light Level Information");
127 ub(16, max_content_light_level);
128 ub(16, max_pic_average_light_level);
133 static int FUNC(alternative_transfer_characteristics)
134 (CodedBitstreamContext *ctx, RWContext *rw,
135 SEIRawAlternativeTransferCharacteristics *current,
136 SEIMessageState *state)
140 HEADER("Alternative Transfer Characteristics");
142 ub(8, preferred_transfer_characteristics);
147 static int FUNC(message)(CodedBitstreamContext *ctx, RWContext *rw,
148 SEIRawMessage *current)
150 const SEIMessageTypeDescriptor *desc;
153 desc = ff_cbs_sei_find_type(ctx, current->payload_type);
155 SEIMessageState state = {
156 .payload_type = current->payload_type,
157 .payload_size = current->payload_size,
158 .extension_present = current->extension_bit_length > 0,
160 int start_position, current_position, bits_written;
163 CHECK(ff_cbs_sei_alloc_message_payload(current, desc));
166 start_position = bit_position(rw);
168 CHECK(desc->READWRITE(ctx, rw, current->payload, &state));
170 current_position = bit_position(rw);
171 bits_written = current_position - start_position;
173 if (byte_alignment(rw) || state.extension_present ||
174 bits_written < 8 * current->payload_size) {
178 GetBitContext tmp = *rw;
179 int trailing_bits, trailing_zero_bits;
181 bits_left = 8 * current->payload_size - bits_written;
183 skip_bits_long(&tmp, bits_left - 8);
184 trailing_bits = get_bits(&tmp, FFMIN(bits_left, 8));
185 if (trailing_bits == 0) {
186 // The trailing bits must contain a bit_equal_to_one, so
187 // they can't all be zero.
188 return AVERROR_INVALIDDATA;
190 trailing_zero_bits = ff_ctz(trailing_bits);
191 current->extension_bit_length =
192 bits_left - 1 - trailing_zero_bits;
195 if (current->extension_bit_length > 0) {
196 allocate(current->extension_data,
197 (current->extension_bit_length + 7) / 8);
199 bits_left = current->extension_bit_length;
200 for (i = 0; bits_left > 0; i++) {
201 int length = FFMIN(bits_left, 8);
202 xu(length, reserved_payload_extension_data,
203 current->extension_data[i],
204 0, MAX_UINT_BITS(length), 0);
209 fixed(1, bit_equal_to_one, 1);
210 while (byte_alignment(rw))
211 fixed(1, bit_equal_to_zero, 0);
215 current->payload_size = (put_bits_count(rw) - start_position) / 8;
220 allocate(current->payload, current->payload_size);
221 data = current->payload;
223 for (i = 0; i < current->payload_size; i++)
224 xu(8, payload_byte[i], data[i], 0, 255, 1, i);
230 static int FUNC(message_list)(CodedBitstreamContext *ctx, RWContext *rw,
231 SEIRawMessageList *current, int prefix)
233 SEIRawMessage *message;
238 uint32_t payload_type = 0;
239 uint32_t payload_size = 0;
242 while (show_bits(rw, 8) == 0xff) {
243 fixed(8, ff_byte, 0xff);
246 xu(8, last_payload_type_byte, tmp, 0, 254, 0);
249 while (show_bits(rw, 8) == 0xff) {
250 fixed(8, ff_byte, 0xff);
253 xu(8, last_payload_size_byte, tmp, 0, 254, 0);
256 CHECK(ff_cbs_sei_list_add(current));
257 message = ¤t->messages[k];
259 message->payload_type = payload_type;
260 message->payload_size = payload_size;
262 CHECK(FUNC(message)(ctx, rw, message));
264 if (!cbs_h2645_read_more_rbsp_data(rw))
268 for (k = 0; k < current->nb_messages; k++) {
269 PutBitContext start_state;
273 message = ¤t->messages[k];
275 // We write the payload twice in order to find the size. Trace
276 // output is switched off for the first write.
277 trace = ctx->trace_enable;
278 ctx->trace_enable = 0;
281 for (i = 0; i < 2; i++) {
284 tmp = message->payload_type;
286 fixed(8, ff_byte, 0xff);
289 xu(8, last_payload_type_byte, tmp, 0, 254, 0);
291 tmp = message->payload_size;
293 fixed(8, ff_byte, 0xff);
296 xu(8, last_payload_size_byte, tmp, 0, 254, 0);
298 err = FUNC(message)(ctx, rw, message);
299 ctx->trace_enable = trace;