]> git.sesse.net Git - ffmpeg/blob - libavcodec/cbs_sei_syntax_template.c
cbs_h2645: Merge SEI message handling in common between codecs
[ffmpeg] / libavcodec / cbs_sei_syntax_template.c
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
19 static int FUNC(filler_payload)
20     (CodedBitstreamContext *ctx, RWContext *rw,
21      SEIRawFillerPayload *current, SEIMessageState *state)
22 {
23     int err, i;
24
25     HEADER("Filler Payload");
26
27 #ifdef READ
28     current->payload_size = state->payload_size;
29 #endif
30
31     for (i = 0; i < current->payload_size; i++)
32         fixed(8, ff_byte, 0xff);
33
34     return 0;
35 }
36
37 static int FUNC(user_data_registered)
38     (CodedBitstreamContext *ctx, RWContext *rw,
39      SEIRawUserDataRegistered *current, SEIMessageState *state)
40 {
41     int err, i, j;
42
43     HEADER("User Data Registered ITU-T T.35");
44
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;
51     }
52
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;
58     }
59     current->data_length = state->payload_size - i;
60 #endif
61
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);
65
66     return 0;
67 }
68
69 static int FUNC(user_data_unregistered)
70     (CodedBitstreamContext *ctx, RWContext *rw,
71      SEIRawUserDataUnregistered *current, SEIMessageState *state)
72 {
73     int err, i;
74
75     HEADER("User Data Unregistered");
76
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;
82     }
83     current->data_length = state->payload_size - 16;
84 #endif
85
86     for (i = 0; i < 16; i++)
87         us(8, uuid_iso_iec_11578[i], 0x00, 0xff, 1, i);
88
89     allocate(current->data, current->data_length);
90
91     for (i = 0; i < current->data_length; i++)
92         xu(8, user_data_payload_byte[i], current->data[i], 0x00, 0xff, 1, i);
93
94     return 0;
95 }
96
97 static int FUNC(mastering_display_colour_volume)
98     (CodedBitstreamContext *ctx, RWContext *rw,
99      SEIRawMasteringDisplayColourVolume *current, SEIMessageState *state)
100 {
101     int err, c;
102
103     HEADER("Mastering Display Colour Volume");
104
105     for (c = 0; c < 3; c++) {
106         us(16, display_primaries_x[c], 0, 50000, 1, c);
107         us(16, display_primaries_y[c], 0, 50000, 1, c);
108     }
109
110     u(16, white_point_x, 0, 50000);
111     u(16, white_point_y, 0, 50000);
112
113     u(32, max_display_mastering_luminance,
114       1, MAX_UINT_BITS(32));
115     u(32, min_display_mastering_luminance,
116       0, current->max_display_mastering_luminance - 1);
117
118     return 0;
119 }
120
121 static int FUNC(content_light_level_info)
122     (CodedBitstreamContext *ctx, RWContext *rw,
123      SEIRawContentLightLevelInfo *current, SEIMessageState *state)
124 {
125     int err;
126
127     HEADER("Content Light Level Information");
128
129     ub(16, max_content_light_level);
130     ub(16, max_pic_average_light_level);
131
132     return 0;
133 }
134
135 static int FUNC(alternative_transfer_characteristics)
136     (CodedBitstreamContext *ctx, RWContext *rw,
137      SEIRawAlternativeTransferCharacteristics *current,
138      SEIMessageState *state)
139 {
140     int err;
141
142     HEADER("Alternative Transfer Characteristics");
143
144     ub(8, preferred_transfer_characteristics);
145
146     return 0;
147 }
148
149 static int FUNC(message)(CodedBitstreamContext *ctx, RWContext *rw,
150                          SEIRawMessage *current)
151 {
152     const SEIMessageTypeDescriptor *desc;
153     int err, i;
154
155     desc = ff_cbs_sei_find_type(ctx, current->payload_type);
156     if (desc) {
157         SEIMessageState state = {
158             .payload_type      = current->payload_type,
159             .payload_size      = current->payload_size,
160             .extension_present = current->extension_bit_length > 0,
161         };
162         int start_position, current_position, bits_written;
163
164 #ifdef READ
165         CHECK(ff_cbs_sei_alloc_message_payload(current, desc));
166 #endif
167
168         start_position = bit_position(rw);
169
170         CHECK(desc->READWRITE(ctx, rw, current->payload, &state));
171
172         current_position = bit_position(rw);
173         bits_written = current_position - start_position;
174
175         if (byte_alignment(rw) || state.extension_present ||
176             bits_written < 8 * current->payload_size) {
177             size_t bits_left;
178
179 #ifdef READ
180             GetBitContext tmp = *rw;
181             int trailing_bits, trailing_zero_bits;
182
183             bits_left = 8 * current->payload_size - bits_written;
184             if (bits_left > 8)
185                 skip_bits_long(&tmp, bits_left - 8);
186             trailing_bits = get_bits(&tmp, FFMIN(bits_left, 8));
187             if (trailing_bits == 0) {
188                 // The trailing bits must contain a bit_equal_to_one, so
189                 // they can't all be zero.
190                 return AVERROR_INVALIDDATA;
191             }
192             trailing_zero_bits = ff_ctz(trailing_bits);
193             current->extension_bit_length =
194                 bits_left - 1 - trailing_zero_bits;
195 #endif
196
197             if (current->extension_bit_length > 0) {
198                 allocate(current->extension_data,
199                          (current->extension_bit_length + 7) / 8);
200
201                 bits_left = current->extension_bit_length;
202                 for (i = 0; bits_left > 0; i++) {
203                     int length = FFMIN(bits_left, 8);
204                     xu(length, reserved_payload_extension_data,
205                        current->extension_data[i],
206                        0, MAX_UINT_BITS(length), 0);
207                     bits_left -= length;
208                 }
209             }
210
211             fixed(1, bit_equal_to_one, 1);
212             while (byte_alignment(rw))
213                 fixed(1, bit_equal_to_zero, 0);
214         }
215
216 #ifdef WRITE
217         current->payload_size = (put_bits_count(rw) - start_position) / 8;
218 #endif
219     } else {
220         uint8_t *data;
221
222         allocate(current->payload, current->payload_size);
223         data = current->payload;
224
225         for (i = 0; i < current->payload_size; i++)
226             xu(8, payload_byte[i], data[i], 0, 255, 1, i);
227     }
228
229     return 0;
230 }
231
232 static int FUNC(message_list)(CodedBitstreamContext *ctx, RWContext *rw,
233                               SEIRawMessageList *current, int prefix)
234 {
235     SEIRawMessage *message;
236     int err, k;
237
238 #ifdef READ
239     for (k = 0;; k++) {
240         uint32_t payload_type = 0;
241         uint32_t payload_size = 0;
242         uint32_t tmp;
243
244         while (show_bits(rw, 8) == 0xff) {
245             fixed(8, ff_byte, 0xff);
246             payload_type += 255;
247         }
248         xu(8, last_payload_type_byte, tmp, 0, 254, 0);
249         payload_type += tmp;
250
251         while (show_bits(rw, 8) == 0xff) {
252             fixed(8, ff_byte, 0xff);
253             payload_size += 255;
254         }
255         xu(8, last_payload_size_byte, tmp, 0, 254, 0);
256         payload_size += tmp;
257
258         CHECK(ff_cbs_sei_list_add(current));
259         message = &current->messages[k];
260
261         message->payload_type = payload_type;
262         message->payload_size = payload_size;
263
264         CHECK(FUNC(message)(ctx, rw, message));
265
266         if (!cbs_h2645_read_more_rbsp_data(rw))
267             break;
268     }
269 #else
270     for (k = 0; k < current->nb_messages; k++) {
271         PutBitContext start_state;
272         uint32_t tmp;
273         int trace, i;
274
275         message = &current->messages[k];
276
277         // We write the payload twice in order to find the size.  Trace
278         // output is switched off for the first write.
279         trace = ctx->trace_enable;
280         ctx->trace_enable = 0;
281
282         start_state = *rw;
283         for (i = 0; i < 2; i++) {
284             *rw = start_state;
285
286             tmp = message->payload_type;
287             while (tmp >= 255) {
288                 fixed(8, ff_byte, 0xff);
289                 tmp -= 255;
290             }
291             xu(8, last_payload_type_byte, tmp, 0, 254, 0);
292
293             tmp = message->payload_size;
294             while (tmp >= 255) {
295                 fixed(8, ff_byte, 0xff);
296                 tmp -= 255;
297             }
298             xu(8, last_payload_size_byte, tmp, 0, 254, 0);
299
300             err = FUNC(message)(ctx, rw, message);
301             ctx->trace_enable = trace;
302             if (err < 0)
303                 return err;
304         }
305     }
306 #endif
307
308     return 0;
309 }