9d05d59156f64563fdd02a6a4dd5abb1a073925e
[libav.git] / libavcodec / cbs_h2645.c
1 /*
2 * This file is part of Libav.
3 *
4 * Libav 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 * Libav 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 Libav; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19 #include "libavutil/attributes.h"
20 #include "libavutil/avassert.h"
21
22 #include "bytestream.h"
23 #include "cbs.h"
24 #include "cbs_internal.h"
25 #include "cbs_h264.h"
26 #include "cbs_h265.h"
27 #include "golomb.h"
28 #include "h264.h"
29 #include "h264_sei.h"
30 #include "h2645_parse.h"
31 #include "hevc.h"
32
33
34 static int cbs_read_ue_golomb(CodedBitstreamContext *ctx, BitstreamContext *bc,
35 const char *name, uint32_t *write_to,
36 uint32_t range_min, uint32_t range_max)
37 {
38 uint32_t value;
39 int position, i, j;
40 unsigned int k;
41 char bits[65];
42
43 position = bitstream_tell(bc);
44
45 for (i = 0; i < 32; i++) {
46 if (bitstream_bits_left(bc) < i + 1) {
47 av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid ue-golomb code at "
48 "%s: bitstream ended.\n", name);
49 return AVERROR_INVALIDDATA;
50 }
51 k = bitstream_read_bit(bc);
52 bits[i] = k ? '1' : '0';
53 if (k)
54 break;
55 }
56 if (i >= 32) {
57 av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid ue-golomb code at "
58 "%s: more than 31 zeroes.\n", name);
59 return AVERROR_INVALIDDATA;
60 }
61 value = 1;
62 for (j = 0; j < i; j++) {
63 k = bitstream_read_bit(bc);
64 bits[i + j + 1] = k ? '1' : '0';
65 value = value << 1 | k;
66 }
67 bits[i + j + 1] = 0;
68 --value;
69
70 if (ctx->trace_enable)
71 ff_cbs_trace_syntax_element(ctx, position, name, bits, value);
72
73 if (value < range_min || value > range_max) {
74 av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
75 "%"PRIu32", but must be in [%"PRIu32",%"PRIu32"].\n",
76 name, value, range_min, range_max);
77 return AVERROR_INVALIDDATA;
78 }
79
80 *write_to = value;
81 return 0;
82 }
83
84 static int cbs_read_se_golomb(CodedBitstreamContext *ctx, BitstreamContext *bc,
85 const char *name, int32_t *write_to,
86 int32_t range_min, int32_t range_max)
87 {
88 int32_t value;
89 int position, i, j;
90 unsigned int k;
91 uint32_t v;
92 char bits[65];
93
94 position = bitstream_tell(bc);
95
96 for (i = 0; i < 32; i++) {
97 if (bitstream_bits_left(bc) < i + 1) {
98 av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid se-golomb code at "
99 "%s: bitstream ended.\n", name);
100 return AVERROR_INVALIDDATA;
101 }
102 k = bitstream_read_bit(bc);
103 bits[i] = k ? '1' : '0';
104 if (k)
105 break;
106 }
107 if (i >= 32) {
108 av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid se-golomb code at "
109 "%s: more than 31 zeroes.\n", name);
110 return AVERROR_INVALIDDATA;
111 }
112 v = 1;
113 for (j = 0; j < i; j++) {
114 k = bitstream_read_bit(bc);
115 bits[i + j + 1] = k ? '1' : '0';
116 v = v << 1 | k;
117 }
118 bits[i + j + 1] = 0;
119 if (v & 1)
120 value = -(int32_t)(v / 2);
121 else
122 value = v / 2;
123
124 if (ctx->trace_enable)
125 ff_cbs_trace_syntax_element(ctx, position, name, bits, value);
126
127 if (value < range_min || value > range_max) {
128 av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
129 "%"PRId32", but must be in [%"PRId32",%"PRId32"].\n",
130 name, value, range_min, range_max);
131 return AVERROR_INVALIDDATA;
132 }
133
134 *write_to = value;
135 return 0;
136 }
137
138 static int cbs_write_ue_golomb(CodedBitstreamContext *ctx, PutBitContext *pbc,
139 const char *name, uint32_t value,
140 uint32_t range_min, uint32_t range_max)
141 {
142 int len;
143
144 if (value < range_min || value > range_max) {
145 av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
146 "%"PRIu32", but must be in [%"PRIu32",%"PRIu32"].\n",
147 name, value, range_min, range_max);
148 return AVERROR_INVALIDDATA;
149 }
150 av_assert0(value != UINT32_MAX);
151
152 len = av_log2(value + 1);
153 if (put_bits_left(pbc) < 2 * len + 1)
154 return AVERROR(ENOSPC);
155
156 if (ctx->trace_enable) {
157 char bits[65];
158 int i;
159
160 for (i = 0; i < len; i++)
161 bits[i] = '0';
162 bits[len] = '1';
163 for (i = 0; i < len; i++)
164 bits[len + i + 1] = (value + 1) >> (len - i - 1) & 1 ? '1' : '0';
165 bits[len + len + 1] = 0;
166
167 ff_cbs_trace_syntax_element(ctx, put_bits_count(pbc), name, bits, value);
168 }
169
170 put_bits(pbc, len, 0);
171 if (len + 1 < 32)
172 put_bits(pbc, len + 1, value + 1);
173 else
174 put_bits32(pbc, value + 1);
175
176 return 0;
177 }
178
179 static int cbs_write_se_golomb(CodedBitstreamContext *ctx, PutBitContext *pbc,
180 const char *name, int32_t value,
181 int32_t range_min, int32_t range_max)
182 {
183 int len;
184 uint32_t uvalue;
185
186 if (value < range_min || value > range_max) {
187 av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
188 "%"PRId32", but must be in [%"PRId32",%"PRId32"].\n",
189 name, value, range_min, range_max);
190 return AVERROR_INVALIDDATA;
191 }
192 av_assert0(value != INT32_MIN);
193
194 if (value == 0)
195 uvalue = 0;
196 else if (value > 0)
197 uvalue = 2 * (uint32_t)value - 1;
198 else
199 uvalue = 2 * (uint32_t)-value;
200
201 len = av_log2(uvalue + 1);
202 if (put_bits_left(pbc) < 2 * len + 1)
203 return AVERROR(ENOSPC);
204
205 if (ctx->trace_enable) {
206 char bits[65];
207 int i;
208
209 for (i = 0; i < len; i++)
210 bits[i] = '0';
211 bits[len] = '1';
212 for (i = 0; i < len; i++)
213 bits[len + i + 1] = (uvalue + 1) >> (len - i - 1) & 1 ? '1' : '0';
214 bits[len + len + 1] = 0;
215
216 ff_cbs_trace_syntax_element(ctx, put_bits_count(pbc), name, bits, value);
217 }
218
219 put_bits(pbc, len, 0);
220 if (len + 1 < 32)
221 put_bits(pbc, len + 1, uvalue + 1);
222 else
223 put_bits32(pbc, uvalue + 1);
224
225 return 0;
226 }
227
228 #define HEADER(name) do { \
229 ff_cbs_trace_header(ctx, name); \
230 } while (0)
231
232 #define CHECK(call) do { \
233 err = (call); \
234 if (err < 0) \
235 return err; \
236 } while (0)
237
238 #define FUNC_NAME(rw, codec, name) cbs_ ## codec ## _ ## rw ## _ ## name
239 #define FUNC_H264(rw, name) FUNC_NAME(rw, h264, name)
240 #define FUNC_H265(rw, name) FUNC_NAME(rw, h265, name)
241
242
243 #define READ
244 #define READWRITE read
245 #define RWContext BitstreamContext
246
247 #define xu(width, name, var, range_min, range_max) do { \
248 uint32_t value = range_min; \
249 CHECK(ff_cbs_read_unsigned(ctx, rw, width, #name, \
250 &value, range_min, range_max)); \
251 var = value; \
252 } while (0)
253 #define xue(name, var, range_min, range_max) do { \
254 uint32_t value = range_min; \
255 CHECK(cbs_read_ue_golomb(ctx, rw, #name, \
256 &value, range_min, range_max)); \
257 var = value; \
258 } while (0)
259 #define xse(name, var, range_min, range_max) do { \
260 int32_t value = range_min; \
261 CHECK(cbs_read_se_golomb(ctx, rw, #name, \
262 &value, range_min, range_max)); \
263 var = value; \
264 } while (0)
265
266
267 #define u(width, name, range_min, range_max) \
268 xu(width, name, current->name, range_min, range_max)
269 #define flag(name) u(1, name, 0, 1)
270 #define ue(name, range_min, range_max) \
271 xue(name, current->name, range_min, range_max)
272 #define se(name, range_min, range_max) \
273 xse(name, current->name, range_min, range_max)
274
275 #define infer(name, value) do { \
276 current->name = value; \
277 } while (0)
278
279 static int cbs_h2645_read_more_rbsp_data(BitstreamContext *bc)
280 {
281 int bits_left = bitstream_bits_left(bc);
282 if (bits_left > 8)
283 return 1;
284 if (bitstream_peek(bc, bits_left) == 1 << (bits_left - 1))
285 return 0;
286 return 1;
287 }
288
289 #define more_rbsp_data(var) ((var) = cbs_h2645_read_more_rbsp_data(rw))
290
291 #define byte_alignment(rw) (bitstream_tell(rw) % 8)
292
293 #define allocate(name, size) do { \
294 name = av_mallocz(size); \
295 if (!name) \
296 return AVERROR(ENOMEM); \
297 } while (0)
298
299 #define FUNC(name) FUNC_H264(READWRITE, name)
300 #include "cbs_h264_syntax_template.c"
301 #undef FUNC
302
303 #define FUNC(name) FUNC_H265(READWRITE, name)
304 #include "cbs_h265_syntax_template.c"
305 #undef FUNC
306
307 #undef READ
308 #undef READWRITE
309 #undef RWContext
310 #undef xu
311 #undef xue
312 #undef xse
313 #undef u
314 #undef flag
315 #undef ue
316 #undef se
317 #undef infer
318 #undef more_rbsp_data
319 #undef byte_alignment
320 #undef allocate
321
322
323 #define WRITE
324 #define READWRITE write
325 #define RWContext PutBitContext
326
327 #define xu(width, name, var, range_min, range_max) do { \
328 uint32_t value = var; \
329 CHECK(ff_cbs_write_unsigned(ctx, rw, width, #name, \
330 value, range_min, range_max)); \
331 } while (0)
332 #define xue(name, var, range_min, range_max) do { \
333 uint32_t value = var; \
334 CHECK(cbs_write_ue_golomb(ctx, rw, #name, \
335 value, range_min, range_max)); \
336 } while (0)
337 #define xse(name, var, range_min, range_max) do { \
338 int32_t value = var; \
339 CHECK(cbs_write_se_golomb(ctx, rw, #name, \
340 value, range_min, range_max)); \
341 } while (0)
342
343 #define u(width, name, range_min, range_max) \
344 xu(width, name, current->name, range_min, range_max)
345 #define flag(name) u(1, name, 0, 1)
346 #define ue(name, range_min, range_max) \
347 xue(name, current->name, range_min, range_max)
348 #define se(name, range_min, range_max) \
349 xse(name, current->name, range_min, range_max)
350
351 #define infer(name, value) do { \
352 if (current->name != (value)) { \
353 av_log(ctx->log_ctx, AV_LOG_WARNING, "Warning: " \
354 "%s does not match inferred value: " \
355 "%"PRId64", but should be %"PRId64".\n", \
356 #name, (int64_t)current->name, (int64_t)(value)); \
357 } \
358 } while (0)
359
360 #define more_rbsp_data(var) (var)
361
362 #define byte_alignment(rw) (put_bits_count(rw) % 8)
363
364 #define allocate(name, size) do { \
365 if (!name) { \
366 av_log(ctx->log_ctx, AV_LOG_ERROR, "%s must be set " \
367 "for writing.\n", #name); \
368 return AVERROR_INVALIDDATA; \
369 } \
370 } while (0)
371
372 #define FUNC(name) FUNC_H264(READWRITE, name)
373 #include "cbs_h264_syntax_template.c"
374 #undef FUNC
375
376 #define FUNC(name) FUNC_H265(READWRITE, name)
377 #include "cbs_h265_syntax_template.c"
378 #undef FUNC
379
380 #undef WRITE
381 #undef READWRITE
382 #undef RWContext
383 #undef xu
384 #undef xue
385 #undef xse
386 #undef u
387 #undef flag
388 #undef ue
389 #undef se
390 #undef infer
391 #undef more_rbsp_data
392 #undef byte_alignment
393 #undef allocate
394
395
396 static void cbs_h264_free_sei(H264RawSEI *sei)
397 {
398 int i;
399 for (i = 0; i < sei->payload_count; i++) {
400 H264RawSEIPayload *payload = &sei->payload[i];
401
402 switch (payload->payload_type) {
403 case H264_SEI_TYPE_BUFFERING_PERIOD:
404 case H264_SEI_TYPE_PIC_TIMING:
405 case H264_SEI_TYPE_RECOVERY_POINT:
406 case H264_SEI_TYPE_DISPLAY_ORIENTATION:
407 break;
408 case H264_SEI_TYPE_USER_DATA_REGISTERED:
409 av_freep(&payload->payload.user_data_registered.data);
410 break;
411 case H264_SEI_TYPE_USER_DATA_UNREGISTERED:
412 av_freep(&payload->payload.user_data_unregistered.data);
413 break;
414 default:
415 av_freep(&payload->payload.other.data);
416 break;
417 }
418 }
419 }
420
421 static void cbs_h264_free_slice(H264RawSlice *slice)
422 {
423 av_freep(&slice->data);
424 }
425
426 static void cbs_h264_free_nal_unit(CodedBitstreamUnit *unit)
427 {
428 switch (unit->type) {
429 case H264_NAL_SEI:
430 cbs_h264_free_sei(unit->content);
431 break;
432 case H264_NAL_IDR_SLICE:
433 case H264_NAL_SLICE:
434 cbs_h264_free_slice(unit->content);
435 break;
436 }
437 av_freep(&unit->content);
438 }
439
440 static void cbs_h265_free_nal_unit(CodedBitstreamUnit *unit)
441 {
442 switch (unit->type) {
443 case HEVC_NAL_VPS:
444 av_freep(&((H265RawVPS*)unit->content)->extension_data.data);
445 break;
446 case HEVC_NAL_SPS:
447 av_freep(&((H265RawSPS*)unit->content)->extension_data.data);
448 break;
449 case HEVC_NAL_PPS:
450 av_freep(&((H265RawPPS*)unit->content)->extension_data.data);
451 break;
452 case HEVC_NAL_TRAIL_N:
453 case HEVC_NAL_TRAIL_R:
454 case HEVC_NAL_TSA_N:
455 case HEVC_NAL_TSA_R:
456 case HEVC_NAL_STSA_N:
457 case HEVC_NAL_STSA_R:
458 case HEVC_NAL_RADL_N:
459 case HEVC_NAL_RADL_R:
460 case HEVC_NAL_RASL_N:
461 case HEVC_NAL_RASL_R:
462 case HEVC_NAL_BLA_W_LP:
463 case HEVC_NAL_BLA_W_RADL:
464 case HEVC_NAL_BLA_N_LP:
465 case HEVC_NAL_IDR_W_RADL:
466 case HEVC_NAL_IDR_N_LP:
467 case HEVC_NAL_CRA_NUT:
468 av_freep(&((H265RawSlice*)unit->content)->data);
469 break;
470 }
471 av_freep(&unit->content);
472 }
473
474 static int cbs_h2645_fragment_add_nals(CodedBitstreamContext *ctx,
475 CodedBitstreamFragment *frag,
476 const H2645Packet *packet)
477 {
478 int err, i;
479
480 for (i = 0; i < packet->nb_nals; i++) {
481 const H2645NAL *nal = &packet->nals[i];
482 size_t size = nal->size;
483 uint8_t *data;
484
485 // Remove trailing zeroes.
486 while (size > 0 && nal->data[size - 1] == 0)
487 --size;
488 av_assert0(size > 0);
489
490 data = av_malloc(size + AV_INPUT_BUFFER_PADDING_SIZE);
491 if (!data)
492 return AVERROR(ENOMEM);
493 memcpy(data, nal->data, size);
494 memset(data + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
495
496 err = ff_cbs_insert_unit_data(ctx, frag, -1, nal->type,
497 data, nal->size);
498 if (err < 0) {
499 av_freep(&data);
500 return err;
501 }
502 }
503
504 return 0;
505 }
506
507 static int cbs_h2645_split_fragment(CodedBitstreamContext *ctx,
508 CodedBitstreamFragment *frag,
509 int header)
510 {
511 enum AVCodecID codec_id = ctx->codec->codec_id;
512 CodedBitstreamH2645Context *priv = ctx->priv_data;
513 GetByteContext gbc;
514 int err;
515
516 av_assert0(frag->data && frag->nb_units == 0);
517 if (frag->data_size == 0)
518 return 0;
519
520 if (header && frag->data[0] && codec_id == AV_CODEC_ID_H264) {
521 // AVCC header.
522 size_t size, start, end;
523 int i, count, version;
524
525 priv->mp4 = 1;
526
527 bytestream2_init(&gbc, frag->data, frag->data_size);
528
529 if (bytestream2_get_bytes_left(&gbc) < 6)
530 return AVERROR_INVALIDDATA;
531
532 version = bytestream2_get_byte(&gbc);
533 if (version != 1) {
534 av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid AVCC header: "
535 "first byte %u.", version);
536 return AVERROR_INVALIDDATA;
537 }
538
539 bytestream2_skip(&gbc, 3);
540 priv->nal_length_size = (bytestream2_get_byte(&gbc) & 3) + 1;
541
542 // SPS array.
543 count = bytestream2_get_byte(&gbc) & 0x1f;
544 start = bytestream2_tell(&gbc);
545 for (i = 0; i < count; i++) {
546 if (bytestream2_get_bytes_left(&gbc) < 2 * (count - i))
547 return AVERROR_INVALIDDATA;
548 size = bytestream2_get_be16(&gbc);
549 if (bytestream2_get_bytes_left(&gbc) < size)
550 return AVERROR_INVALIDDATA;
551 bytestream2_skip(&gbc, size);
552 }
553 end = bytestream2_tell(&gbc);
554
555 err = ff_h2645_packet_split(&priv->read_packet,
556 frag->data + start, end - start,
557 ctx->log_ctx, 1, 2, AV_CODEC_ID_H264);
558 if (err < 0) {
559 av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to split AVCC SPS array.\n");
560 return err;
561 }
562 err = cbs_h2645_fragment_add_nals(ctx, frag, &priv->read_packet);
563 if (err < 0)
564 return err;
565
566 // PPS array.
567 count = bytestream2_get_byte(&gbc);
568 start = bytestream2_tell(&gbc);
569 for (i = 0; i < count; i++) {
570 if (bytestream2_get_bytes_left(&gbc) < 2 * (count - i))
571 return AVERROR_INVALIDDATA;
572 size = bytestream2_get_be16(&gbc);
573 if (bytestream2_get_bytes_left(&gbc) < size)
574 return AVERROR_INVALIDDATA;
575 bytestream2_skip(&gbc, size);
576 }
577 end = bytestream2_tell(&gbc);
578
579 err = ff_h2645_packet_split(&priv->read_packet,
580 frag->data + start, end - start,
581 ctx->log_ctx, 1, 2, AV_CODEC_ID_H264);
582 if (err < 0) {
583 av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to split AVCC PPS array.\n");
584 return err;
585 }
586 err = cbs_h2645_fragment_add_nals(ctx, frag, &priv->read_packet);
587 if (err < 0)
588 return err;
589
590 if (bytestream2_get_bytes_left(&gbc) > 0) {
591 av_log(ctx->log_ctx, AV_LOG_WARNING, "%u bytes left at end of AVCC "
592 "header.\n", bytestream2_get_bytes_left(&gbc));
593 }
594
595 } else if (header && frag->data[0] && codec_id == AV_CODEC_ID_HEVC) {
596 // HVCC header.
597 size_t size, start, end;
598 int i, j, nb_arrays, nal_unit_type, nb_nals, version;
599
600 priv->mp4 = 1;
601
602 bytestream2_init(&gbc, frag->data, frag->data_size);
603
604 if (bytestream2_get_bytes_left(&gbc) < 23)
605 return AVERROR_INVALIDDATA;
606
607 version = bytestream2_get_byte(&gbc);
608 if (version != 1) {
609 av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid HVCC header: "
610 "first byte %u.", version);
611 return AVERROR_INVALIDDATA;
612 }
613
614 bytestream2_skip(&gbc, 20);
615 priv->nal_length_size = (bytestream2_get_byte(&gbc) & 3) + 1;
616
617 nb_arrays = bytestream2_get_byte(&gbc);
618 for (i = 0; i < nb_arrays; i++) {
619 nal_unit_type = bytestream2_get_byte(&gbc) & 0x3f;
620 nb_nals = bytestream2_get_be16(&gbc);
621
622 start = bytestream2_tell(&gbc);
623 for (j = 0; j < nb_nals; j++) {
624 if (bytestream2_get_bytes_left(&gbc) < 2)
625 return AVERROR_INVALIDDATA;
626 size = bytestream2_get_be16(&gbc);
627 if (bytestream2_get_bytes_left(&gbc) < size)
628 return AVERROR_INVALIDDATA;
629 bytestream2_skip(&gbc, size);
630 }
631 end = bytestream2_tell(&gbc);
632
633 err = ff_h2645_packet_split(&priv->read_packet,
634 frag->data + start, end - start,
635 ctx->log_ctx, 1, 2, AV_CODEC_ID_HEVC);
636 if (err < 0) {
637 av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to split "
638 "HVCC array %d (%d NAL units of type %d).\n",
639 i, nb_nals, nal_unit_type);
640 return err;
641 }
642 err = cbs_h2645_fragment_add_nals(ctx, frag, &priv->read_packet);
643 if (err < 0)
644 return err;
645 }
646
647 } else {
648 // Annex B, or later MP4 with already-known parameters.
649
650 err = ff_h2645_packet_split(&priv->read_packet,
651 frag->data, frag->data_size,
652 ctx->log_ctx,
653 priv->mp4, priv->nal_length_size,
654 codec_id);
655 if (err < 0)
656 return err;
657
658 err = cbs_h2645_fragment_add_nals(ctx, frag, &priv->read_packet);
659 if (err < 0)
660 return err;
661 }
662
663 return 0;
664 }
665
666 #define cbs_h2645_replace_ps(h26n, ps_name, ps_var, id_element) \
667 static int cbs_h26 ## h26n ## _replace_ ## ps_var(CodedBitstreamContext *ctx, \
668 const H26 ## h26n ## Raw ## ps_name *ps_var) \
669 { \
670 CodedBitstreamH26 ## h26n ## Context *priv = ctx->priv_data; \
671 unsigned int id = ps_var->id_element; \
672 if (id > FF_ARRAY_ELEMS(priv->ps_var)) { \
673 av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid " #ps_name \
674 " id : %d.\n", id); \
675 return AVERROR_INVALIDDATA; \
676 } \
677 if (priv->ps_var[id] == priv->active_ ## ps_var) \
678 priv->active_ ## ps_var = NULL ; \
679 av_freep(&priv->ps_var[id]); \
680 priv->ps_var[id] = av_malloc(sizeof(*ps_var)); \
681 if (!priv->ps_var[id]) \
682 return AVERROR(ENOMEM); \
683 memcpy(priv->ps_var[id], ps_var, sizeof(*ps_var)); \
684 return 0; \
685 }
686
687 cbs_h2645_replace_ps(4, SPS, sps, seq_parameter_set_id)
688 cbs_h2645_replace_ps(4, PPS, pps, pic_parameter_set_id)
689 cbs_h2645_replace_ps(5, VPS, vps, vps_video_parameter_set_id)
690 cbs_h2645_replace_ps(5, SPS, sps, sps_seq_parameter_set_id)
691 cbs_h2645_replace_ps(5, PPS, pps, pps_pic_parameter_set_id)
692
693 static int cbs_h264_read_nal_unit(CodedBitstreamContext *ctx,
694 CodedBitstreamUnit *unit)
695 {
696 BitstreamContext bc;
697 int err;
698
699 err = bitstream_init(&bc, unit->data, 8 * unit->data_size);
700 if (err < 0)
701 return err;
702
703 switch (unit->type) {
704 case H264_NAL_SPS:
705 {
706 H264RawSPS *sps;
707
708 sps = av_mallocz(sizeof(*sps));
709 if (!sps)
710 return AVERROR(ENOMEM);
711 err = cbs_h264_read_sps(ctx, &bc, sps);
712 if (err >= 0)
713 err = cbs_h264_replace_sps(ctx, sps);
714 if (err < 0) {
715 av_free(sps);
716 return err;
717 }
718
719 unit->content = sps;
720 }
721 break;
722
723 case H264_NAL_SPS_EXT:
724 {
725 H264RawSPSExtension *sps_ext;
726
727 sps_ext = av_mallocz(sizeof(*sps_ext));
728 if (!sps_ext)
729 return AVERROR(ENOMEM);
730 err = cbs_h264_read_sps_extension(ctx, &bc, sps_ext);
731 if (err < 0) {
732 av_free(sps_ext);
733 return err;
734 }
735
736 unit->content = sps_ext;
737 }
738 break;
739
740 case H264_NAL_PPS:
741 {
742 H264RawPPS *pps;
743
744 pps = av_mallocz(sizeof(*pps));
745 if (!pps)
746 return AVERROR(ENOMEM);
747 err = cbs_h264_read_pps(ctx, &bc, pps);
748 if (err >= 0)
749 err = cbs_h264_replace_pps(ctx, pps);
750 if (err < 0) {
751 av_free(pps);
752 return err;
753 }
754
755 unit->content = pps;
756 }
757 break;
758
759 case H264_NAL_SLICE:
760 case H264_NAL_IDR_SLICE:
761 case H264_NAL_AUXILIARY_SLICE:
762 {
763 H264RawSlice *slice;
764 int pos, len;
765
766 slice = av_mallocz(sizeof(*slice));
767 if (!slice)
768 return AVERROR(ENOMEM);
769 err = cbs_h264_read_slice_header(ctx, &bc, &slice->header);
770 if (err < 0) {
771 av_free(slice);
772 return err;
773 }
774
775 pos = bitstream_tell(&bc);
776 len = unit->data_size;
777 if (!unit->data[len - 1]) {
778 int z;
779 for (z = 0; z < len && !unit->data[len - z - 1]; z++);
780 av_log(ctx->log_ctx, AV_LOG_DEBUG, "Deleted %d trailing zeroes "
781 "from slice data.\n", z);
782 len -= z;
783 }
784
785 slice->data_size = len - pos / 8;
786 slice->data = av_malloc(slice->data_size +
787 AV_INPUT_BUFFER_PADDING_SIZE);
788 if (!slice->data) {
789 av_free(slice);
790 return AVERROR(ENOMEM);
791 }
792 memcpy(slice->data,
793 unit->data + pos / 8, slice->data_size);
794 memset(slice->data + slice->data_size, 0,
795 AV_INPUT_BUFFER_PADDING_SIZE);
796 slice->data_bit_start = pos % 8;
797
798 unit->content = slice;
799 }
800 break;
801
802 case H264_NAL_AUD:
803 {
804 H264RawAUD *aud;
805
806 aud = av_mallocz(sizeof(*aud));
807 if (!aud)
808 return AVERROR(ENOMEM);
809 err = cbs_h264_read_aud(ctx, &bc, aud);
810 if (err < 0) {
811 av_free(aud);
812 return err;
813 }
814
815 unit->content = aud;
816 }
817 break;
818
819 case H264_NAL_SEI:
820 {
821 H264RawSEI *sei;
822
823 sei = av_mallocz(sizeof(*sei));
824 if (!sei)
825 return AVERROR(ENOMEM);
826 err = cbs_h264_read_sei(ctx, &bc, sei);
827 if (err < 0) {
828 cbs_h264_free_sei(sei);
829 av_free(sei);
830 return err;
831 }
832
833 unit->content = sei;
834 }
835 break;
836
837 default:
838 return AVERROR(ENOSYS);
839 }
840
841 return 0;
842 }
843
844 static int cbs_h265_read_nal_unit(CodedBitstreamContext *ctx,
845 CodedBitstreamUnit *unit)
846 {
847 BitstreamContext bc;
848 int err;
849
850 err = bitstream_init(&bc, unit->data, 8 * unit->data_size);
851 if (err < 0)
852 return err;
853
854 switch (unit->type) {
855 case HEVC_NAL_VPS:
856 {
857 H265RawVPS *vps;
858
859 vps = av_mallocz(sizeof(*vps));
860 if (!vps)
861 return AVERROR(ENOMEM);
862 err = cbs_h265_read_vps(ctx, &bc, vps);
863 if (err >= 0)
864 err = cbs_h265_replace_vps(ctx, vps);
865 if (err < 0) {
866 av_free(vps);
867 return err;
868 }
869
870 unit->content = vps;
871 }
872 break;
873 case HEVC_NAL_SPS:
874 {
875 H265RawSPS *sps;
876
877 sps = av_mallocz(sizeof(*sps));
878 if (!sps)
879 return AVERROR(ENOMEM);
880 err = cbs_h265_read_sps(ctx, &bc, sps);
881 if (err >= 0)
882 err = cbs_h265_replace_sps(ctx, sps);
883 if (err < 0) {
884 av_free(sps);
885 return err;
886 }
887
888 unit->content = sps;
889 }
890 break;
891
892 case HEVC_NAL_PPS:
893 {
894 H265RawPPS *pps;
895
896 pps = av_mallocz(sizeof(*pps));
897 if (!pps)
898 return AVERROR(ENOMEM);
899 err = cbs_h265_read_pps(ctx, &bc, pps);
900 if (err >= 0)
901 err = cbs_h265_replace_pps(ctx, pps);
902 if (err < 0) {
903 av_free(pps);
904 return err;
905 }
906
907 unit->content = pps;
908 }
909 break;
910
911 case HEVC_NAL_TRAIL_N:
912 case HEVC_NAL_TRAIL_R:
913 case HEVC_NAL_TSA_N:
914 case HEVC_NAL_TSA_R:
915 case HEVC_NAL_STSA_N:
916 case HEVC_NAL_STSA_R:
917 case HEVC_NAL_RADL_N:
918 case HEVC_NAL_RADL_R:
919 case HEVC_NAL_RASL_N:
920 case HEVC_NAL_RASL_R:
921 case HEVC_NAL_BLA_W_LP:
922 case HEVC_NAL_BLA_W_RADL:
923 case HEVC_NAL_BLA_N_LP:
924 case HEVC_NAL_IDR_W_RADL:
925 case HEVC_NAL_IDR_N_LP:
926 case HEVC_NAL_CRA_NUT:
927 {
928 H265RawSlice *slice;
929 int pos, len;
930
931 slice = av_mallocz(sizeof(*slice));
932 if (!slice)
933 return AVERROR(ENOMEM);
934 err = cbs_h265_read_slice_segment_header(ctx, &bc, &slice->header);
935 if (err < 0) {
936 av_free(slice);
937 return err;
938 }
939
940 pos = bitstream_tell(&bc);
941 len = unit->data_size;
942 if (!unit->data[len - 1]) {
943 int z;
944 for (z = 0; z < len && !unit->data[len - z - 1]; z++);
945 av_log(ctx->log_ctx, AV_LOG_DEBUG, "Deleted %d trailing zeroes "
946 "from slice data.\n", z);
947 len -= z;
948 }
949
950 slice->data_size = len - pos / 8;
951 slice->data = av_malloc(slice->data_size +
952 AV_INPUT_BUFFER_PADDING_SIZE);
953 if (!slice->data) {
954 av_free(slice);
955 return AVERROR(ENOMEM);
956 }
957 memcpy(slice->data,
958 unit->data + pos / 8, slice->data_size);
959 memset(slice->data + slice->data_size, 0,
960 AV_INPUT_BUFFER_PADDING_SIZE);
961 slice->data_bit_start = pos % 8;
962
963 unit->content = slice;
964 }
965 break;
966
967 case HEVC_NAL_AUD:
968 {
969 H265RawAUD *aud;
970
971 aud = av_mallocz(sizeof(*aud));
972 if (!aud)
973 return AVERROR(ENOMEM);
974 err = cbs_h265_read_aud(ctx, &bc, aud);
975 if (err < 0) {
976 av_free(aud);
977 return err;
978 }
979
980 unit->content = aud;
981 }
982 break;
983
984 default:
985 return AVERROR(ENOSYS);
986 }
987
988 return 0;
989 }
990
991 static int cbs_h264_write_nal_unit(CodedBitstreamContext *ctx,
992 CodedBitstreamUnit *unit,
993 PutBitContext *pbc)
994 {
995 int err;
996
997 switch (unit->type) {
998 case H264_NAL_SPS:
999 {
1000 H264RawSPS *sps = unit->content;
1001
1002 err = cbs_h264_write_sps(ctx, pbc, sps);
1003 if (err < 0)
1004 return err;
1005
1006 err = cbs_h264_replace_sps(ctx, sps);
1007 if (err < 0)
1008 return err;
1009 }
1010 break;
1011
1012 case H264_NAL_SPS_EXT:
1013 {
1014 H264RawSPSExtension *sps_ext = unit->content;
1015
1016 err = cbs_h264_write_sps_extension(ctx, pbc, sps_ext);
1017 if (err < 0)
1018 return err;
1019 }
1020 break;
1021
1022 case H264_NAL_PPS:
1023 {
1024 H264RawPPS *pps = unit->content;
1025
1026 err = cbs_h264_write_pps(ctx, pbc, pps);
1027 if (err < 0)
1028 return err;
1029
1030 err = cbs_h264_replace_pps(ctx, pps);
1031 if (err < 0)
1032 return err;
1033 }
1034 break;
1035
1036 case H264_NAL_SLICE:
1037 case H264_NAL_IDR_SLICE:
1038 case H264_NAL_AUXILIARY_SLICE:
1039 {
1040 H264RawSlice *slice = unit->content;
1041 BitstreamContext bc;
1042 int bits_left, end, zeroes;
1043
1044 err = cbs_h264_write_slice_header(ctx, pbc, &slice->header);
1045 if (err < 0)
1046 return err;
1047
1048 if (slice->data) {
1049 if (slice->data_size * 8 + 8 > put_bits_left(pbc))
1050 return AVERROR(ENOSPC);
1051
1052 bitstream_init(&bc, slice->data, slice->data_size * 8);
1053 bitstream_skip(&bc, slice->data_bit_start);
1054
1055 // Copy in two-byte blocks, but stop before copying the
1056 // rbsp_stop_one_bit in the final byte.
1057 while (bitstream_bits_left(&bc) > 23)
1058 put_bits(pbc, 16, bitstream_read(&bc, 16));
1059
1060 bits_left = bitstream_bits_left(&bc);
1061 end = bitstream_read(&bc, bits_left);
1062
1063 // rbsp_stop_one_bit must be present here.
1064 av_assert0(end);
1065 zeroes = ff_ctz(end);
1066 if (bits_left > zeroes + 1)
1067 put_bits(pbc, bits_left - zeroes - 1,
1068 end >> (zeroes + 1));
1069 put_bits(pbc, 1, 1);
1070 while (put_bits_count(pbc) % 8 != 0)
1071 put_bits(pbc, 1, 0);
1072 } else {
1073 // No slice data - that was just the header.
1074 // (Bitstream may be unaligned!)
1075 }
1076 }
1077 break;
1078
1079 case H264_NAL_AUD:
1080 {
1081 err = cbs_h264_write_aud(ctx, pbc, unit->content);
1082 if (err < 0)
1083 return err;
1084 }
1085 break;
1086
1087 case H264_NAL_SEI:
1088 {
1089 err = cbs_h264_write_sei(ctx, pbc, unit->content);
1090 if (err < 0)
1091 return err;
1092 }
1093 break;
1094
1095 default:
1096 av_log(ctx->log_ctx, AV_LOG_ERROR, "Write unimplemented for "
1097 "NAL unit type %"PRIu32".\n", unit->type);
1098 return AVERROR_PATCHWELCOME;
1099 }
1100
1101 return 0;
1102 }
1103
1104 static int cbs_h265_write_nal_unit(CodedBitstreamContext *ctx,
1105 CodedBitstreamUnit *unit,
1106 PutBitContext *pbc)
1107 {
1108 int err;
1109
1110 switch (unit->type) {
1111 case HEVC_NAL_VPS:
1112 {
1113 H265RawVPS *vps = unit->content;
1114
1115 err = cbs_h265_write_vps(ctx, pbc, vps);
1116 if (err < 0)
1117 return err;
1118
1119 err = cbs_h265_replace_vps(ctx, vps);
1120 if (err < 0)
1121 return err;
1122 }
1123 break;
1124
1125 case HEVC_NAL_SPS:
1126 {
1127 H265RawSPS *sps = unit->content;
1128
1129 err = cbs_h265_write_sps(ctx, pbc, sps);
1130 if (err < 0)
1131 return err;
1132
1133 err = cbs_h265_replace_sps(ctx, sps);
1134 if (err < 0)
1135 return err;
1136 }
1137 break;
1138
1139 case HEVC_NAL_PPS:
1140 {
1141 H265RawPPS *pps = unit->content;
1142
1143 err = cbs_h265_write_pps(ctx, pbc, pps);
1144 if (err < 0)
1145 return err;
1146
1147 err = cbs_h265_replace_pps(ctx, pps);
1148 if (err < 0)
1149 return err;
1150 }
1151 break;
1152
1153 case HEVC_NAL_TRAIL_N:
1154 case HEVC_NAL_TRAIL_R:
1155 case HEVC_NAL_TSA_N:
1156 case HEVC_NAL_TSA_R:
1157 case HEVC_NAL_STSA_N:
1158 case HEVC_NAL_STSA_R:
1159 case HEVC_NAL_RADL_N:
1160 case HEVC_NAL_RADL_R:
1161 case HEVC_NAL_RASL_N:
1162 case HEVC_NAL_RASL_R:
1163 case HEVC_NAL_BLA_W_LP:
1164 case HEVC_NAL_BLA_W_RADL:
1165 case HEVC_NAL_BLA_N_LP:
1166 case HEVC_NAL_IDR_W_RADL:
1167 case HEVC_NAL_IDR_N_LP:
1168 case HEVC_NAL_CRA_NUT:
1169 {
1170 H265RawSlice *slice = unit->content;
1171 BitstreamContext bc;
1172 int bits_left, end, zeroes;
1173
1174 err = cbs_h265_write_slice_segment_header(ctx, pbc, &slice->header);
1175 if (err < 0)
1176 return err;
1177
1178 if (slice->data) {
1179 if (slice->data_size * 8 + 8 > put_bits_left(pbc))
1180 return AVERROR(ENOSPC);
1181
1182 bitstream_init(&bc, slice->data, slice->data_size * 8);
1183 bitstream_skip(&bc, slice->data_bit_start);
1184
1185 // Copy in two-byte blocks, but stop before copying the
1186 // rbsp_stop_one_bit in the final byte.
1187 while (bitstream_bits_left(&bc) > 23)
1188 put_bits(pbc, 16, bitstream_read(&bc, 16));
1189
1190 bits_left = bitstream_bits_left(&bc);
1191 end = bitstream_read(&bc, bits_left);
1192
1193 // rbsp_stop_one_bit must be present here.
1194 av_assert0(end);
1195 zeroes = ff_ctz(end);
1196 if (bits_left > zeroes + 1)
1197 put_bits(pbc, bits_left - zeroes - 1,
1198 end >> (zeroes + 1));
1199 put_bits(pbc, 1, 1);
1200 while (put_bits_count(pbc) % 8 != 0)
1201 put_bits(pbc, 1, 0);
1202 } else {
1203 // No slice data - that was just the header.
1204 }
1205 }
1206 break;
1207
1208 case HEVC_NAL_AUD:
1209 {
1210 err = cbs_h265_write_aud(ctx, pbc, unit->content);
1211 if (err < 0)
1212 return err;
1213 }
1214 break;
1215
1216 default:
1217 av_log(ctx->log_ctx, AV_LOG_ERROR, "Write unimplemented for "
1218 "NAL unit type %"PRIu32".\n", unit->type);
1219 return AVERROR_PATCHWELCOME;
1220 }
1221
1222 return 0;
1223 }
1224
1225 static int cbs_h2645_write_nal_unit(CodedBitstreamContext *ctx,
1226 CodedBitstreamUnit *unit)
1227 {
1228 CodedBitstreamH2645Context *priv = ctx->priv_data;
1229 enum AVCodecID codec_id = ctx->codec->codec_id;
1230 PutBitContext pbc;
1231 int err;
1232
1233 if (!priv->write_buffer) {
1234 // Initial write buffer size is 1MB.
1235 priv->write_buffer_size = 1024 * 1024;
1236
1237 reallocate_and_try_again:
1238 err = av_reallocp(&priv->write_buffer, priv->write_buffer_size);
1239 if (err < 0) {
1240 av_log(ctx->log_ctx, AV_LOG_ERROR, "Unable to allocate a "
1241 "sufficiently large write buffer (last attempt "
1242 "%zu bytes).\n", priv->write_buffer_size);
1243 return err;
1244 }
1245 }
1246
1247 init_put_bits(&pbc, priv->write_buffer, priv->write_buffer_size);
1248
1249 if (codec_id == AV_CODEC_ID_H264)
1250 err = cbs_h264_write_nal_unit(ctx, unit, &pbc);
1251 else
1252 err = cbs_h265_write_nal_unit(ctx, unit, &pbc);
1253
1254 if (err == AVERROR(ENOSPC)) {
1255 // Overflow.
1256 priv->write_buffer_size *= 2;
1257 goto reallocate_and_try_again;
1258 }
1259 // Overflow but we didn't notice.
1260 av_assert0(put_bits_count(&pbc) <= 8 * priv->write_buffer_size);
1261
1262 if (err < 0) {
1263 // Write failed for some other reason.
1264 return err;
1265 }
1266
1267 if (put_bits_count(&pbc) % 8)
1268 unit->data_bit_padding = 8 - put_bits_count(&pbc) % 8;
1269 else
1270 unit->data_bit_padding = 0;
1271
1272 unit->data_size = (put_bits_count(&pbc) + 7) / 8;
1273 flush_put_bits(&pbc);
1274
1275 err = av_reallocp(&unit->data, unit->data_size);
1276 if (err < 0)
1277 return err;
1278
1279 memcpy(unit->data, priv->write_buffer, unit->data_size);
1280
1281 return 0;
1282 }
1283
1284 static int cbs_h2645_assemble_fragment(CodedBitstreamContext *ctx,
1285 CodedBitstreamFragment *frag)
1286 {
1287 uint8_t *data;
1288 size_t max_size, dp, sp;
1289 int err, i, zero_run;
1290
1291 for (i = 0; i < frag->nb_units; i++) {
1292 // Data should already all have been written when we get here.
1293 av_assert0(frag->units[i].data);
1294 }
1295
1296 max_size = 0;
1297 for (i = 0; i < frag->nb_units; i++) {
1298 // Start code + content with worst-case emulation prevention.
1299 max_size += 3 + frag->units[i].data_size * 3 / 2;
1300 }
1301
1302 data = av_malloc(max_size);
1303 if (!data)
1304 return AVERROR(ENOMEM);
1305
1306 dp = 0;
1307 for (i = 0; i < frag->nb_units; i++) {
1308 CodedBitstreamUnit *unit = &frag->units[i];
1309
1310 if (unit->data_bit_padding > 0) {
1311 if (i < frag->nb_units - 1)
1312 av_log(ctx->log_ctx, AV_LOG_WARNING, "Probably invalid "
1313 "unaligned padding on non-final NAL unit.\n");
1314 else
1315 frag->data_bit_padding = unit->data_bit_padding;
1316 }
1317
1318 if ((ctx->codec->codec_id == AV_CODEC_ID_H264 &&
1319 (unit->type == H264_NAL_SPS ||
1320 unit->type == H264_NAL_PPS)) ||
1321 (ctx->codec->codec_id == AV_CODEC_ID_HEVC &&
1322 (unit->type == HEVC_NAL_VPS ||
1323 unit->type == HEVC_NAL_SPS ||
1324 unit->type == HEVC_NAL_PPS)) ||
1325 i == 0 /* (Assume this is the start of an access unit.) */) {
1326 // zero_byte
1327 data[dp++] = 0;
1328 }
1329 // start_code_prefix_one_3bytes
1330 data[dp++] = 0;
1331 data[dp++] = 0;
1332 data[dp++] = 1;
1333
1334 zero_run = 0;
1335 for (sp = 0; sp < unit->data_size; sp++) {
1336 if (zero_run < 2) {
1337 if (unit->data[sp] == 0)
1338 ++zero_run;
1339 else
1340 zero_run = 0;
1341 } else {
1342 if ((unit->data[sp] & ~3) == 0) {
1343 // emulation_prevention_three_byte
1344 data[dp++] = 3;
1345 }
1346 zero_run = unit->data[sp] == 0;
1347 }
1348 data[dp++] = unit->data[sp];
1349 }
1350 }
1351
1352 av_assert0(dp <= max_size);
1353 err = av_reallocp(&data, dp);
1354 if (err)
1355 return err;
1356
1357 frag->data = data;
1358 frag->data_size = dp;
1359
1360 return 0;
1361 }
1362
1363 static void cbs_h264_close(CodedBitstreamContext *ctx)
1364 {
1365 CodedBitstreamH264Context *h264 = ctx->priv_data;
1366 int i;
1367
1368 ff_h2645_packet_uninit(&h264->common.read_packet);
1369
1370 av_freep(&h264->common.write_buffer);
1371
1372 for (i = 0; i < FF_ARRAY_ELEMS(h264->sps); i++)
1373 av_freep(&h264->sps[i]);
1374 for (i = 0; i < FF_ARRAY_ELEMS(h264->pps); i++)
1375 av_freep(&h264->pps[i]);
1376 }
1377
1378 static void cbs_h265_close(CodedBitstreamContext *ctx)
1379 {
1380 CodedBitstreamH265Context *h265 = ctx->priv_data;
1381 int i;
1382
1383 ff_h2645_packet_uninit(&h265->common.read_packet);
1384
1385 av_freep(&h265->common.write_buffer);
1386
1387 for (i = 0; i < FF_ARRAY_ELEMS(h265->vps); i++)
1388 av_freep(&h265->vps[i]);
1389 for (i = 0; i < FF_ARRAY_ELEMS(h265->sps); i++)
1390 av_freep(&h265->sps[i]);
1391 for (i = 0; i < FF_ARRAY_ELEMS(h265->pps); i++)
1392 av_freep(&h265->pps[i]);
1393 }
1394
1395 const CodedBitstreamType ff_cbs_type_h264 = {
1396 .codec_id = AV_CODEC_ID_H264,
1397
1398 .priv_data_size = sizeof(CodedBitstreamH264Context),
1399
1400 .split_fragment = &cbs_h2645_split_fragment,
1401 .read_unit = &cbs_h264_read_nal_unit,
1402 .write_unit = &cbs_h2645_write_nal_unit,
1403 .assemble_fragment = &cbs_h2645_assemble_fragment,
1404
1405 .free_unit = &cbs_h264_free_nal_unit,
1406 .close = &cbs_h264_close,
1407 };
1408
1409 const CodedBitstreamType ff_cbs_type_h265 = {
1410 .codec_id = AV_CODEC_ID_HEVC,
1411
1412 .priv_data_size = sizeof(CodedBitstreamH265Context),
1413
1414 .split_fragment = &cbs_h2645_split_fragment,
1415 .read_unit = &cbs_h265_read_nal_unit,
1416 .write_unit = &cbs_h2645_write_nal_unit,
1417 .assemble_fragment = &cbs_h2645_assemble_fragment,
1418
1419 .free_unit = &cbs_h265_free_nal_unit,
1420 .close = &cbs_h265_close,
1421 };