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