c8a13f16796a27897ab31e34b20392cd71e74b67
[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 uint8_t *data;
483
484 data = av_malloc(nal->size);
485 if (!data)
486 return AVERROR(ENOMEM);
487 memcpy(data, nal->data, nal->size);
488
489 err = ff_cbs_insert_unit_data(ctx, frag, -1, nal->type,
490 data, nal->size);
491 if (err < 0) {
492 av_freep(&data);
493 return err;
494 }
495 }
496
497 return 0;
498 }
499
500 static int cbs_h2645_split_fragment(CodedBitstreamContext *ctx,
501 CodedBitstreamFragment *frag,
502 int header)
503 {
504 enum AVCodecID codec_id = ctx->codec->codec_id;
505 CodedBitstreamH2645Context *priv = ctx->priv_data;
506 GetByteContext gbc;
507 int err;
508
509 av_assert0(frag->data && frag->nb_units == 0);
510 if (frag->data_size == 0)
511 return 0;
512
513 if (header && frag->data[0] && codec_id == AV_CODEC_ID_H264) {
514 // AVCC header.
515 size_t size, start, end;
516 int i, count, version;
517
518 priv->mp4 = 1;
519
520 bytestream2_init(&gbc, frag->data, frag->data_size);
521
522 if (bytestream2_get_bytes_left(&gbc) < 6)
523 return AVERROR_INVALIDDATA;
524
525 version = bytestream2_get_byte(&gbc);
526 if (version != 1) {
527 av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid AVCC header: "
528 "first byte %u.", version);
529 return AVERROR_INVALIDDATA;
530 }
531
532 bytestream2_skip(&gbc, 3);
533 priv->nal_length_size = (bytestream2_get_byte(&gbc) & 3) + 1;
534
535 // SPS array.
536 count = bytestream2_get_byte(&gbc) & 0x1f;
537 start = bytestream2_tell(&gbc);
538 for (i = 0; i < count; i++) {
539 if (bytestream2_get_bytes_left(&gbc) < 2 * (count - i))
540 return AVERROR_INVALIDDATA;
541 size = bytestream2_get_be16(&gbc);
542 if (bytestream2_get_bytes_left(&gbc) < size)
543 return AVERROR_INVALIDDATA;
544 bytestream2_skip(&gbc, size);
545 }
546 end = bytestream2_tell(&gbc);
547
548 err = ff_h2645_packet_split(&priv->read_packet,
549 frag->data + start, end - start,
550 ctx->log_ctx, 1, 2, AV_CODEC_ID_H264);
551 if (err < 0) {
552 av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to split AVCC SPS array.\n");
553 return err;
554 }
555 err = cbs_h2645_fragment_add_nals(ctx, frag, &priv->read_packet);
556 if (err < 0)
557 return err;
558
559 // PPS array.
560 count = bytestream2_get_byte(&gbc);
561 start = bytestream2_tell(&gbc);
562 for (i = 0; i < count; i++) {
563 if (bytestream2_get_bytes_left(&gbc) < 2 * (count - i))
564 return AVERROR_INVALIDDATA;
565 size = bytestream2_get_be16(&gbc);
566 if (bytestream2_get_bytes_left(&gbc) < size)
567 return AVERROR_INVALIDDATA;
568 bytestream2_skip(&gbc, size);
569 }
570 end = bytestream2_tell(&gbc);
571
572 err = ff_h2645_packet_split(&priv->read_packet,
573 frag->data + start, end - start,
574 ctx->log_ctx, 1, 2, AV_CODEC_ID_H264);
575 if (err < 0) {
576 av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to split AVCC PPS array.\n");
577 return err;
578 }
579 err = cbs_h2645_fragment_add_nals(ctx, frag, &priv->read_packet);
580 if (err < 0)
581 return err;
582
583 if (bytestream2_get_bytes_left(&gbc) > 0) {
584 av_log(ctx->log_ctx, AV_LOG_WARNING, "%u bytes left at end of AVCC "
585 "header.\n", bytestream2_get_bytes_left(&gbc));
586 }
587
588 } else if (header && frag->data[0] && codec_id == AV_CODEC_ID_HEVC) {
589 // HVCC header.
590 size_t size, start, end;
591 int i, j, nb_arrays, nal_unit_type, nb_nals, version;
592
593 priv->mp4 = 1;
594
595 bytestream2_init(&gbc, frag->data, frag->data_size);
596
597 if (bytestream2_get_bytes_left(&gbc) < 23)
598 return AVERROR_INVALIDDATA;
599
600 version = bytestream2_get_byte(&gbc);
601 if (version != 1) {
602 av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid HVCC header: "
603 "first byte %u.", version);
604 return AVERROR_INVALIDDATA;
605 }
606
607 bytestream2_skip(&gbc, 20);
608 priv->nal_length_size = (bytestream2_get_byte(&gbc) & 3) + 1;
609
610 nb_arrays = bytestream2_get_byte(&gbc);
611 for (i = 0; i < nb_arrays; i++) {
612 nal_unit_type = bytestream2_get_byte(&gbc) & 0x3f;
613 nb_nals = bytestream2_get_be16(&gbc);
614
615 start = bytestream2_tell(&gbc);
616 for (j = 0; j < nb_nals; j++) {
617 if (bytestream2_get_bytes_left(&gbc) < 2)
618 return AVERROR_INVALIDDATA;
619 size = bytestream2_get_be16(&gbc);
620 if (bytestream2_get_bytes_left(&gbc) < size)
621 return AVERROR_INVALIDDATA;
622 bytestream2_skip(&gbc, size);
623 }
624 end = bytestream2_tell(&gbc);
625
626 err = ff_h2645_packet_split(&priv->read_packet,
627 frag->data + start, end - start,
628 ctx->log_ctx, 1, 2, AV_CODEC_ID_HEVC);
629 if (err < 0) {
630 av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to split "
631 "HVCC array %d (%d NAL units of type %d).\n",
632 i, nb_nals, nal_unit_type);
633 return err;
634 }
635 err = cbs_h2645_fragment_add_nals(ctx, frag, &priv->read_packet);
636 if (err < 0)
637 return err;
638 }
639
640 } else {
641 // Annex B, or later MP4 with already-known parameters.
642
643 err = ff_h2645_packet_split(&priv->read_packet,
644 frag->data, frag->data_size,
645 ctx->log_ctx,
646 priv->mp4, priv->nal_length_size,
647 codec_id);
648 if (err < 0)
649 return err;
650
651 err = cbs_h2645_fragment_add_nals(ctx, frag, &priv->read_packet);
652 if (err < 0)
653 return err;
654 }
655
656 return 0;
657 }
658
659 #define cbs_h2645_replace_ps(h26n, ps_name, ps_var, id_element) \
660 static int cbs_h26 ## h26n ## _replace_ ## ps_var(CodedBitstreamContext *ctx, \
661 const H26 ## h26n ## Raw ## ps_name *ps_var) \
662 { \
663 CodedBitstreamH26 ## h26n ## Context *priv = ctx->priv_data; \
664 unsigned int id = ps_var->id_element; \
665 if (id > FF_ARRAY_ELEMS(priv->ps_var)) { \
666 av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid " #ps_name \
667 " id : %d.\n", id); \
668 return AVERROR_INVALIDDATA; \
669 } \
670 av_freep(&priv->ps_var[id]); \
671 priv->ps_var[id] = av_malloc(sizeof(*ps_var)); \
672 if (!priv->ps_var[id]) \
673 return AVERROR(ENOMEM); \
674 memcpy(priv->ps_var[id], ps_var, sizeof(*ps_var)); \
675 return 0; \
676 }
677
678 cbs_h2645_replace_ps(4, SPS, sps, seq_parameter_set_id)
679 cbs_h2645_replace_ps(4, PPS, pps, pic_parameter_set_id)
680 cbs_h2645_replace_ps(5, VPS, vps, vps_video_parameter_set_id)
681 cbs_h2645_replace_ps(5, SPS, sps, sps_seq_parameter_set_id)
682 cbs_h2645_replace_ps(5, PPS, pps, pps_pic_parameter_set_id)
683
684 static int cbs_h264_read_nal_unit(CodedBitstreamContext *ctx,
685 CodedBitstreamUnit *unit)
686 {
687 BitstreamContext bc;
688 int err;
689
690 err = bitstream_init(&bc, unit->data, 8 * unit->data_size);
691 if (err < 0)
692 return err;
693
694 switch (unit->type) {
695 case H264_NAL_SPS:
696 {
697 H264RawSPS *sps;
698
699 sps = av_mallocz(sizeof(*sps));
700 if (!sps)
701 return AVERROR(ENOMEM);
702 err = cbs_h264_read_sps(ctx, &bc, sps);
703 if (err >= 0)
704 err = cbs_h264_replace_sps(ctx, sps);
705 if (err < 0) {
706 av_free(sps);
707 return err;
708 }
709
710 unit->content = sps;
711 }
712 break;
713
714 case H264_NAL_SPS_EXT:
715 {
716 H264RawSPSExtension *sps_ext;
717
718 sps_ext = av_mallocz(sizeof(*sps_ext));
719 if (!sps_ext)
720 return AVERROR(ENOMEM);
721 err = cbs_h264_read_sps_extension(ctx, &bc, sps_ext);
722 if (err < 0) {
723 av_free(sps_ext);
724 return err;
725 }
726
727 unit->content = sps_ext;
728 }
729 break;
730
731 case H264_NAL_PPS:
732 {
733 H264RawPPS *pps;
734
735 pps = av_mallocz(sizeof(*pps));
736 if (!pps)
737 return AVERROR(ENOMEM);
738 err = cbs_h264_read_pps(ctx, &bc, pps);
739 if (err >= 0)
740 err = cbs_h264_replace_pps(ctx, pps);
741 if (err < 0) {
742 av_free(pps);
743 return err;
744 }
745
746 unit->content = pps;
747 }
748 break;
749
750 case H264_NAL_SLICE:
751 case H264_NAL_IDR_SLICE:
752 case H264_NAL_AUXILIARY_SLICE:
753 {
754 H264RawSlice *slice;
755 int pos, len;
756
757 slice = av_mallocz(sizeof(*slice));
758 if (!slice)
759 return AVERROR(ENOMEM);
760 err = cbs_h264_read_slice_header(ctx, &bc, &slice->header);
761 if (err < 0) {
762 av_free(slice);
763 return err;
764 }
765
766 pos = bitstream_tell(&bc);
767 len = unit->data_size;
768 if (!unit->data[len - 1]) {
769 int z;
770 for (z = 0; z < len && !unit->data[len - z - 1]; z++);
771 av_log(ctx->log_ctx, AV_LOG_DEBUG, "Deleted %d trailing zeroes "
772 "from slice data.\n", z);
773 len -= z;
774 }
775
776 slice->data_size = len - pos / 8;
777 slice->data = av_malloc(slice->data_size);
778 if (!slice->data) {
779 av_free(slice);
780 return AVERROR(ENOMEM);
781 }
782 memcpy(slice->data,
783 unit->data + pos / 8, slice->data_size);
784 slice->data_bit_start = pos % 8;
785
786 unit->content = slice;
787 }
788 break;
789
790 case H264_NAL_AUD:
791 {
792 H264RawAUD *aud;
793
794 aud = av_mallocz(sizeof(*aud));
795 if (!aud)
796 return AVERROR(ENOMEM);
797 err = cbs_h264_read_aud(ctx, &bc, aud);
798 if (err < 0) {
799 av_free(aud);
800 return err;
801 }
802
803 unit->content = aud;
804 }
805 break;
806
807 case H264_NAL_SEI:
808 {
809 H264RawSEI *sei;
810
811 sei = av_mallocz(sizeof(*sei));
812 if (!sei)
813 return AVERROR(ENOMEM);
814 err = cbs_h264_read_sei(ctx, &bc, sei);
815 if (err < 0) {
816 cbs_h264_free_sei(sei);
817 return err;
818 }
819
820 unit->content = sei;
821 }
822 break;
823
824 default:
825 return AVERROR(ENOSYS);
826 }
827
828 return 0;
829 }
830
831 static int cbs_h265_read_nal_unit(CodedBitstreamContext *ctx,
832 CodedBitstreamUnit *unit)
833 {
834 BitstreamContext bc;
835 int err;
836
837 err = bitstream_init(&bc, unit->data, 8 * unit->data_size);
838 if (err < 0)
839 return err;
840
841 switch (unit->type) {
842 case HEVC_NAL_VPS:
843 {
844 H265RawVPS *vps;
845
846 vps = av_mallocz(sizeof(*vps));
847 if (!vps)
848 return AVERROR(ENOMEM);
849 err = cbs_h265_read_vps(ctx, &bc, vps);
850 if (err >= 0)
851 err = cbs_h265_replace_vps(ctx, vps);
852 if (err < 0) {
853 av_free(vps);
854 return err;
855 }
856
857 unit->content = vps;
858 }
859 break;
860 case HEVC_NAL_SPS:
861 {
862 H265RawSPS *sps;
863
864 sps = av_mallocz(sizeof(*sps));
865 if (!sps)
866 return AVERROR(ENOMEM);
867 err = cbs_h265_read_sps(ctx, &bc, sps);
868 if (err >= 0)
869 err = cbs_h265_replace_sps(ctx, sps);
870 if (err < 0) {
871 av_free(sps);
872 return err;
873 }
874
875 unit->content = sps;
876 }
877 break;
878
879 case HEVC_NAL_PPS:
880 {
881 H265RawPPS *pps;
882
883 pps = av_mallocz(sizeof(*pps));
884 if (!pps)
885 return AVERROR(ENOMEM);
886 err = cbs_h265_read_pps(ctx, &bc, pps);
887 if (err >= 0)
888 err = cbs_h265_replace_pps(ctx, pps);
889 if (err < 0) {
890 av_free(pps);
891 return err;
892 }
893
894 unit->content = pps;
895 }
896 break;
897
898 case HEVC_NAL_TRAIL_N:
899 case HEVC_NAL_TRAIL_R:
900 case HEVC_NAL_TSA_N:
901 case HEVC_NAL_TSA_R:
902 case HEVC_NAL_STSA_N:
903 case HEVC_NAL_STSA_R:
904 case HEVC_NAL_RADL_N:
905 case HEVC_NAL_RADL_R:
906 case HEVC_NAL_RASL_N:
907 case HEVC_NAL_RASL_R:
908 case HEVC_NAL_BLA_W_LP:
909 case HEVC_NAL_BLA_W_RADL:
910 case HEVC_NAL_BLA_N_LP:
911 case HEVC_NAL_IDR_W_RADL:
912 case HEVC_NAL_IDR_N_LP:
913 case HEVC_NAL_CRA_NUT:
914 {
915 H265RawSlice *slice;
916 int pos, len;
917
918 slice = av_mallocz(sizeof(*slice));
919 if (!slice)
920 return AVERROR(ENOMEM);
921 err = cbs_h265_read_slice_segment_header(ctx, &bc, &slice->header);
922 if (err < 0) {
923 av_free(slice);
924 return err;
925 }
926
927 pos = bitstream_tell(&bc);
928 len = unit->data_size;
929 if (!unit->data[len - 1]) {
930 int z;
931 for (z = 0; z < len && !unit->data[len - z - 1]; z++);
932 av_log(ctx->log_ctx, AV_LOG_DEBUG, "Deleted %d trailing zeroes "
933 "from slice data.\n", z);
934 len -= z;
935 }
936
937 slice->data_size = len - pos / 8;
938 slice->data = av_malloc(slice->data_size);
939 if (!slice->data) {
940 av_free(slice);
941 return AVERROR(ENOMEM);
942 }
943 memcpy(slice->data,
944 unit->data + pos / 8, slice->data_size);
945 slice->data_bit_start = pos % 8;
946
947 unit->content = slice;
948 }
949 break;
950
951 case HEVC_NAL_AUD:
952 {
953 H265RawAUD *aud;
954
955 aud = av_mallocz(sizeof(*aud));
956 if (!aud)
957 return AVERROR(ENOMEM);
958 err = cbs_h265_read_aud(ctx, &bc, aud);
959 if (err < 0) {
960 av_free(aud);
961 return err;
962 }
963
964 unit->content = aud;
965 }
966 break;
967
968 default:
969 return AVERROR(ENOSYS);
970 }
971
972 return 0;
973 }
974
975 static int cbs_h264_write_nal_unit(CodedBitstreamContext *ctx,
976 CodedBitstreamUnit *unit,
977 PutBitContext *pbc)
978 {
979 int err;
980
981 switch (unit->type) {
982 case H264_NAL_SPS:
983 {
984 H264RawSPS *sps = unit->content;
985
986 err = cbs_h264_write_sps(ctx, pbc, sps);
987 if (err < 0)
988 return err;
989
990 err = cbs_h264_replace_sps(ctx, sps);
991 if (err < 0)
992 return err;
993 }
994 break;
995
996 case H264_NAL_SPS_EXT:
997 {
998 H264RawSPSExtension *sps_ext;
999
1000 err = cbs_h264_write_sps_extension(ctx, pbc, sps_ext);
1001 if (err < 0)
1002 return err;
1003 }
1004 break;
1005
1006 case H264_NAL_PPS:
1007 {
1008 H264RawPPS *pps = unit->content;
1009
1010 err = cbs_h264_write_pps(ctx, pbc, pps);
1011 if (err < 0)
1012 return err;
1013
1014 err = cbs_h264_replace_pps(ctx, pps);
1015 if (err < 0)
1016 return err;
1017 }
1018 break;
1019
1020 case H264_NAL_SLICE:
1021 case H264_NAL_IDR_SLICE:
1022 {
1023 H264RawSlice *slice = unit->content;
1024 BitstreamContext bc;
1025 int bits_left, end, zeroes;
1026
1027 err = cbs_h264_write_slice_header(ctx, pbc, &slice->header);
1028 if (err < 0)
1029 return err;
1030
1031 if (slice->data) {
1032 if (slice->data_size * 8 + 8 > put_bits_left(pbc))
1033 return AVERROR(ENOSPC);
1034
1035 bitstream_init(&bc, slice->data, slice->data_size * 8);
1036 bitstream_skip(&bc, slice->data_bit_start);
1037
1038 // Copy in two-byte blocks, but stop before copying the
1039 // rbsp_stop_one_bit in the final byte.
1040 while (bitstream_bits_left(&bc) > 23)
1041 put_bits(pbc, 16, bitstream_read(&bc, 16));
1042
1043 bits_left = bitstream_bits_left(&bc);
1044 end = bitstream_read(&bc, bits_left);
1045
1046 // rbsp_stop_one_bit must be present here.
1047 av_assert0(end);
1048 zeroes = ff_ctz(end);
1049 if (bits_left > zeroes + 1)
1050 put_bits(pbc, bits_left - zeroes - 1,
1051 end >> (zeroes + 1));
1052 put_bits(pbc, 1, 1);
1053 while (put_bits_count(pbc) % 8 != 0)
1054 put_bits(pbc, 1, 0);
1055 } else {
1056 // No slice data - that was just the header.
1057 // (Bitstream may be unaligned!)
1058 }
1059 }
1060 break;
1061
1062 case H264_NAL_AUD:
1063 {
1064 err = cbs_h264_write_aud(ctx, pbc, unit->content);
1065 if (err < 0)
1066 return err;
1067 }
1068 break;
1069
1070 case H264_NAL_SEI:
1071 {
1072 err = cbs_h264_write_sei(ctx, pbc, unit->content);
1073 if (err < 0)
1074 return err;
1075 }
1076 break;
1077
1078 default:
1079 av_log(ctx->log_ctx, AV_LOG_ERROR, "Write unimplemented for "
1080 "NAL unit type %"PRIu32".\n", unit->type);
1081 return AVERROR_PATCHWELCOME;
1082 }
1083
1084 return 0;
1085 }
1086
1087 static int cbs_h265_write_nal_unit(CodedBitstreamContext *ctx,
1088 CodedBitstreamUnit *unit,
1089 PutBitContext *pbc)
1090 {
1091 int err;
1092
1093 switch (unit->type) {
1094 case HEVC_NAL_VPS:
1095 {
1096 H265RawVPS *vps = unit->content;
1097
1098 err = cbs_h265_write_vps(ctx, pbc, vps);
1099 if (err < 0)
1100 return err;
1101
1102 err = cbs_h265_replace_vps(ctx, vps);
1103 if (err < 0)
1104 return err;
1105 }
1106 break;
1107
1108 case HEVC_NAL_SPS:
1109 {
1110 H265RawSPS *sps = unit->content;
1111
1112 err = cbs_h265_write_sps(ctx, pbc, sps);
1113 if (err < 0)
1114 return err;
1115
1116 err = cbs_h265_replace_sps(ctx, sps);
1117 if (err < 0)
1118 return err;
1119 }
1120 break;
1121
1122 case HEVC_NAL_PPS:
1123 {
1124 H265RawPPS *pps = unit->content;
1125
1126 err = cbs_h265_write_pps(ctx, pbc, pps);
1127 if (err < 0)
1128 return err;
1129
1130 err = cbs_h265_replace_pps(ctx, pps);
1131 if (err < 0)
1132 return err;
1133 }
1134 break;
1135
1136 case HEVC_NAL_TRAIL_N:
1137 case HEVC_NAL_TRAIL_R:
1138 case HEVC_NAL_TSA_N:
1139 case HEVC_NAL_TSA_R:
1140 case HEVC_NAL_STSA_N:
1141 case HEVC_NAL_STSA_R:
1142 case HEVC_NAL_RADL_N:
1143 case HEVC_NAL_RADL_R:
1144 case HEVC_NAL_RASL_N:
1145 case HEVC_NAL_RASL_R:
1146 case HEVC_NAL_BLA_W_LP:
1147 case HEVC_NAL_BLA_W_RADL:
1148 case HEVC_NAL_BLA_N_LP:
1149 case HEVC_NAL_IDR_W_RADL:
1150 case HEVC_NAL_IDR_N_LP:
1151 case HEVC_NAL_CRA_NUT:
1152 {
1153 H265RawSlice *slice = unit->content;
1154 BitstreamContext bc;
1155 int bits_left, end, zeroes;
1156
1157 err = cbs_h265_write_slice_segment_header(ctx, pbc, &slice->header);
1158 if (err < 0)
1159 return err;
1160
1161 if (slice->data) {
1162 if (slice->data_size * 8 + 8 > put_bits_left(pbc))
1163 return AVERROR(ENOSPC);
1164
1165 bitstream_init(&bc, slice->data, slice->data_size * 8);
1166 bitstream_skip(&bc, slice->data_bit_start);
1167
1168 // Copy in two-byte blocks, but stop before copying the
1169 // rbsp_stop_one_bit in the final byte.
1170 while (bitstream_bits_left(&bc) > 23)
1171 put_bits(pbc, 16, bitstream_read(&bc, 16));
1172
1173 bits_left = bitstream_bits_left(&bc);
1174 end = bitstream_read(&bc, bits_left);
1175
1176 // rbsp_stop_one_bit must be present here.
1177 av_assert0(end);
1178 zeroes = ff_ctz(end);
1179 if (bits_left > zeroes + 1)
1180 put_bits(pbc, bits_left - zeroes - 1,
1181 end >> (zeroes + 1));
1182 put_bits(pbc, 1, 1);
1183 while (put_bits_count(pbc) % 8 != 0)
1184 put_bits(pbc, 1, 0);
1185 } else {
1186 // No slice data - that was just the header.
1187 }
1188 }
1189 break;
1190
1191 case HEVC_NAL_AUD:
1192 {
1193 err = cbs_h265_write_aud(ctx, pbc, unit->content);
1194 if (err < 0)
1195 return err;
1196 }
1197 break;
1198
1199 default:
1200 av_log(ctx->log_ctx, AV_LOG_ERROR, "Write unimplemented for "
1201 "NAL unit type %d.\n", unit->type);
1202 return AVERROR_PATCHWELCOME;
1203 }
1204
1205 return 0;
1206 }
1207
1208 static int cbs_h2645_write_nal_unit(CodedBitstreamContext *ctx,
1209 CodedBitstreamUnit *unit)
1210 {
1211 CodedBitstreamH2645Context *priv = ctx->priv_data;
1212 enum AVCodecID codec_id = ctx->codec->codec_id;
1213 PutBitContext pbc;
1214 int err;
1215
1216 if (!priv->write_buffer) {
1217 // Initial write buffer size is 1MB.
1218 priv->write_buffer_size = 1024 * 1024;
1219
1220 reallocate_and_try_again:
1221 err = av_reallocp(&priv->write_buffer, priv->write_buffer_size);
1222 if (err < 0) {
1223 av_log(ctx->log_ctx, AV_LOG_ERROR, "Unable to allocate a "
1224 "sufficiently large write buffer (last attempt "
1225 "%zu bytes).\n", priv->write_buffer_size);
1226 return err;
1227 }
1228 }
1229
1230 init_put_bits(&pbc, priv->write_buffer, priv->write_buffer_size);
1231
1232 if (codec_id == AV_CODEC_ID_H264)
1233 err = cbs_h264_write_nal_unit(ctx, unit, &pbc);
1234 else
1235 err = cbs_h265_write_nal_unit(ctx, unit, &pbc);
1236
1237 if (err == AVERROR(ENOSPC)) {
1238 // Overflow.
1239 priv->write_buffer_size *= 2;
1240 goto reallocate_and_try_again;
1241 }
1242 // Overflow but we didn't notice.
1243 av_assert0(put_bits_count(&pbc) <= 8 * priv->write_buffer_size);
1244
1245 if (err < 0) {
1246 // Write failed for some other reason.
1247 return err;
1248 }
1249
1250 if (put_bits_count(&pbc) % 8)
1251 unit->data_bit_padding = 8 - put_bits_count(&pbc) % 8;
1252 else
1253 unit->data_bit_padding = 0;
1254
1255 unit->data_size = (put_bits_count(&pbc) + 7) / 8;
1256 flush_put_bits(&pbc);
1257
1258 err = av_reallocp(&unit->data, unit->data_size);
1259 if (err < 0)
1260 return err;
1261
1262 memcpy(unit->data, priv->write_buffer, unit->data_size);
1263
1264 return 0;
1265 }
1266
1267 static int cbs_h2645_assemble_fragment(CodedBitstreamContext *ctx,
1268 CodedBitstreamFragment *frag)
1269 {
1270 uint8_t *data;
1271 size_t max_size, dp, sp;
1272 int err, i, zero_run;
1273
1274 for (i = 0; i < frag->nb_units; i++) {
1275 // Data should already all have been written when we get here.
1276 av_assert0(frag->units[i].data);
1277 }
1278
1279 max_size = 0;
1280 for (i = 0; i < frag->nb_units; i++) {
1281 // Start code + content with worst-case emulation prevention.
1282 max_size += 3 + frag->units[i].data_size * 3 / 2;
1283 }
1284
1285 data = av_malloc(max_size);
1286 if (!data)
1287 return AVERROR(ENOMEM);
1288
1289 dp = 0;
1290 for (i = 0; i < frag->nb_units; i++) {
1291 CodedBitstreamUnit *unit = &frag->units[i];
1292
1293 if (unit->data_bit_padding > 0) {
1294 if (i < frag->nb_units - 1)
1295 av_log(ctx->log_ctx, AV_LOG_WARNING, "Probably invalid "
1296 "unaligned padding on non-final NAL unit.\n");
1297 else
1298 frag->data_bit_padding = unit->data_bit_padding;
1299 }
1300
1301 if ((ctx->codec->codec_id == AV_CODEC_ID_H264 &&
1302 (unit->type == H264_NAL_SPS ||
1303 unit->type == H264_NAL_PPS)) ||
1304 (ctx->codec->codec_id == AV_CODEC_ID_HEVC &&
1305 (unit->type == HEVC_NAL_VPS ||
1306 unit->type == HEVC_NAL_SPS ||
1307 unit->type == HEVC_NAL_PPS)) ||
1308 i == 0 /* (Assume this is the start of an access unit.) */) {
1309 // zero_byte
1310 data[dp++] = 0;
1311 }
1312 // start_code_prefix_one_3bytes
1313 data[dp++] = 0;
1314 data[dp++] = 0;
1315 data[dp++] = 1;
1316
1317 zero_run = 0;
1318 for (sp = 0; sp < unit->data_size; sp++) {
1319 if (zero_run < 2) {
1320 if (unit->data[sp] == 0)
1321 ++zero_run;
1322 else
1323 zero_run = 0;
1324 } else {
1325 if ((unit->data[sp] & ~3) == 0) {
1326 // emulation_prevention_three_byte
1327 data[dp++] = 3;
1328 }
1329 zero_run = unit->data[sp] == 0;
1330 }
1331 data[dp++] = unit->data[sp];
1332 }
1333 }
1334
1335 av_assert0(dp <= max_size);
1336 err = av_reallocp(&data, dp);
1337 if (err)
1338 return err;
1339
1340 frag->data = data;
1341 frag->data_size = dp;
1342
1343 return 0;
1344 }
1345
1346 static void cbs_h264_close(CodedBitstreamContext *ctx)
1347 {
1348 CodedBitstreamH264Context *h264 = ctx->priv_data;
1349 int i;
1350
1351 ff_h2645_packet_uninit(&h264->common.read_packet);
1352
1353 av_freep(&h264->common.write_buffer);
1354
1355 for (i = 0; i < FF_ARRAY_ELEMS(h264->sps); i++)
1356 av_freep(&h264->sps[i]);
1357 for (i = 0; i < FF_ARRAY_ELEMS(h264->pps); i++)
1358 av_freep(&h264->pps[i]);
1359 }
1360
1361 static void cbs_h265_close(CodedBitstreamContext *ctx)
1362 {
1363 CodedBitstreamH265Context *h265 = ctx->priv_data;
1364 int i;
1365
1366 ff_h2645_packet_uninit(&h265->common.read_packet);
1367
1368 av_freep(&h265->common.write_buffer);
1369
1370 for (i = 0; i < FF_ARRAY_ELEMS(h265->vps); i++)
1371 av_freep(&h265->vps[i]);
1372 for (i = 0; i < FF_ARRAY_ELEMS(h265->sps); i++)
1373 av_freep(&h265->sps[i]);
1374 for (i = 0; i < FF_ARRAY_ELEMS(h265->pps); i++)
1375 av_freep(&h265->pps[i]);
1376 }
1377
1378 const CodedBitstreamType ff_cbs_type_h264 = {
1379 .codec_id = AV_CODEC_ID_H264,
1380
1381 .priv_data_size = sizeof(CodedBitstreamH264Context),
1382
1383 .split_fragment = &cbs_h2645_split_fragment,
1384 .read_unit = &cbs_h264_read_nal_unit,
1385 .write_unit = &cbs_h2645_write_nal_unit,
1386 .assemble_fragment = &cbs_h2645_assemble_fragment,
1387
1388 .free_unit = &cbs_h264_free_nal_unit,
1389 .close = &cbs_h264_close,
1390 };
1391
1392 const CodedBitstreamType ff_cbs_type_h265 = {
1393 .codec_id = AV_CODEC_ID_HEVC,
1394
1395 .priv_data_size = sizeof(CodedBitstreamH265Context),
1396
1397 .split_fragment = &cbs_h2645_split_fragment,
1398 .read_unit = &cbs_h265_read_nal_unit,
1399 .write_unit = &cbs_h2645_write_nal_unit,
1400 .assemble_fragment = &cbs_h2645_assemble_fragment,
1401
1402 .free_unit = &cbs_h265_free_nal_unit,
1403 .close = &cbs_h265_close,
1404 };