cbs: Add buffer padding when splitting fragments
[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;
44cde38c
MT
39 int position, i, j;
40 unsigned int k;
41 char bits[65];
acf06f45 42
44cde38c 43 position = bitstream_tell(bc);
acf06f45 44
44cde38c
MT
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);
acf06f45
MT
49 return AVERROR_INVALIDDATA;
50 }
44cde38c
MT
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;
acf06f45 69
44cde38c 70 if (ctx->trace_enable)
acf06f45 71 ff_cbs_trace_syntax_element(ctx, position, name, bits, value);
acf06f45
MT
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
84static 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;
44cde38c
MT
89 int position, i, j;
90 unsigned int k;
91 uint32_t v;
92 char bits[65];
acf06f45 93
44cde38c 94 position = bitstream_tell(bc);
acf06f45 95
44cde38c
MT
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);
acf06f45
MT
100 return AVERROR_INVALIDDATA;
101 }
44cde38c
MT
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;
acf06f45 123
44cde38c 124 if (ctx->trace_enable)
acf06f45 125 ff_cbs_trace_syntax_element(ctx, position, name, bits, value);
acf06f45
MT
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
138static 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
179static 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)
867381b8 240#define FUNC_H265(rw, name) FUNC_NAME(rw, h265, name)
acf06f45
MT
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
279static 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
867381b8
MT
303#define FUNC(name) FUNC_H265(READWRITE, name)
304#include "cbs_h265_syntax_template.c"
305#undef FUNC
306
acf06f45
MT
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
867381b8
MT
376#define FUNC(name) FUNC_H265(READWRITE, name)
377#include "cbs_h265_syntax_template.c"
378#undef FUNC
379
acf06f45
MT
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
396static 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
421static void cbs_h264_free_slice(H264RawSlice *slice)
422{
423 av_freep(&slice->data);
424}
425
426static 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
867381b8
MT
440static 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
acf06f45
MT
474static 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];
e7f64191 482 size_t size = nal->size;
acf06f45
MT
483 uint8_t *data;
484
e7f64191
MT
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);
acf06f45
MT
491 if (!data)
492 return AVERROR(ENOMEM);
e7f64191
MT
493 memcpy(data, nal->data, size);
494 memset(data + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
acf06f45
MT
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
507static 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
867381b8
MT
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
acf06f45
MT
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) \
667static 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 av_freep(&priv->ps_var[id]); \
678 priv->ps_var[id] = av_malloc(sizeof(*ps_var)); \
679 if (!priv->ps_var[id]) \
680 return AVERROR(ENOMEM); \
681 memcpy(priv->ps_var[id], ps_var, sizeof(*ps_var)); \
682 return 0; \
683}
684
685cbs_h2645_replace_ps(4, SPS, sps, seq_parameter_set_id)
686cbs_h2645_replace_ps(4, PPS, pps, pic_parameter_set_id)
867381b8
MT
687cbs_h2645_replace_ps(5, VPS, vps, vps_video_parameter_set_id)
688cbs_h2645_replace_ps(5, SPS, sps, sps_seq_parameter_set_id)
689cbs_h2645_replace_ps(5, PPS, pps, pps_pic_parameter_set_id)
acf06f45
MT
690
691static int cbs_h264_read_nal_unit(CodedBitstreamContext *ctx,
692 CodedBitstreamUnit *unit)
693{
694 BitstreamContext bc;
695 int err;
696
697 err = bitstream_init(&bc, unit->data, 8 * unit->data_size);
698 if (err < 0)
699 return err;
700
701 switch (unit->type) {
702 case H264_NAL_SPS:
703 {
704 H264RawSPS *sps;
705
706 sps = av_mallocz(sizeof(*sps));
707 if (!sps)
708 return AVERROR(ENOMEM);
709 err = cbs_h264_read_sps(ctx, &bc, sps);
710 if (err >= 0)
711 err = cbs_h264_replace_sps(ctx, sps);
712 if (err < 0) {
713 av_free(sps);
714 return err;
715 }
716
717 unit->content = sps;
718 }
719 break;
720
721 case H264_NAL_SPS_EXT:
722 {
723 H264RawSPSExtension *sps_ext;
724
725 sps_ext = av_mallocz(sizeof(*sps_ext));
726 if (!sps_ext)
727 return AVERROR(ENOMEM);
728 err = cbs_h264_read_sps_extension(ctx, &bc, sps_ext);
729 if (err < 0) {
730 av_free(sps_ext);
731 return err;
732 }
733
734 unit->content = sps_ext;
735 }
736 break;
737
738 case H264_NAL_PPS:
739 {
740 H264RawPPS *pps;
741
742 pps = av_mallocz(sizeof(*pps));
743 if (!pps)
744 return AVERROR(ENOMEM);
745 err = cbs_h264_read_pps(ctx, &bc, pps);
746 if (err >= 0)
747 err = cbs_h264_replace_pps(ctx, pps);
748 if (err < 0) {
749 av_free(pps);
750 return err;
751 }
752
753 unit->content = pps;
754 }
755 break;
756
757 case H264_NAL_SLICE:
758 case H264_NAL_IDR_SLICE:
759 case H264_NAL_AUXILIARY_SLICE:
760 {
761 H264RawSlice *slice;
762 int pos, len;
763
764 slice = av_mallocz(sizeof(*slice));
765 if (!slice)
766 return AVERROR(ENOMEM);
767 err = cbs_h264_read_slice_header(ctx, &bc, &slice->header);
768 if (err < 0) {
769 av_free(slice);
770 return err;
771 }
772
773 pos = bitstream_tell(&bc);
774 len = unit->data_size;
775 if (!unit->data[len - 1]) {
776 int z;
777 for (z = 0; z < len && !unit->data[len - z - 1]; z++);
778 av_log(ctx->log_ctx, AV_LOG_DEBUG, "Deleted %d trailing zeroes "
779 "from slice data.\n", z);
780 len -= z;
781 }
782
783 slice->data_size = len - pos / 8;
784 slice->data = av_malloc(slice->data_size);
785 if (!slice->data) {
786 av_free(slice);
787 return AVERROR(ENOMEM);
788 }
789 memcpy(slice->data,
790 unit->data + pos / 8, slice->data_size);
791 slice->data_bit_start = pos % 8;
792
793 unit->content = slice;
794 }
795 break;
796
797 case H264_NAL_AUD:
798 {
799 H264RawAUD *aud;
800
801 aud = av_mallocz(sizeof(*aud));
802 if (!aud)
803 return AVERROR(ENOMEM);
804 err = cbs_h264_read_aud(ctx, &bc, aud);
805 if (err < 0) {
806 av_free(aud);
807 return err;
808 }
809
810 unit->content = aud;
811 }
812 break;
813
814 case H264_NAL_SEI:
815 {
816 H264RawSEI *sei;
817
818 sei = av_mallocz(sizeof(*sei));
819 if (!sei)
820 return AVERROR(ENOMEM);
821 err = cbs_h264_read_sei(ctx, &bc, sei);
822 if (err < 0) {
823 cbs_h264_free_sei(sei);
824 return err;
825 }
826
827 unit->content = sei;
828 }
829 break;
830
831 default:
832 return AVERROR(ENOSYS);
833 }
834
835 return 0;
836}
837
867381b8
MT
838static int cbs_h265_read_nal_unit(CodedBitstreamContext *ctx,
839 CodedBitstreamUnit *unit)
840{
841 BitstreamContext bc;
842 int err;
843
844 err = bitstream_init(&bc, unit->data, 8 * unit->data_size);
845 if (err < 0)
846 return err;
847
848 switch (unit->type) {
849 case HEVC_NAL_VPS:
850 {
851 H265RawVPS *vps;
852
853 vps = av_mallocz(sizeof(*vps));
854 if (!vps)
855 return AVERROR(ENOMEM);
856 err = cbs_h265_read_vps(ctx, &bc, vps);
857 if (err >= 0)
858 err = cbs_h265_replace_vps(ctx, vps);
859 if (err < 0) {
860 av_free(vps);
861 return err;
862 }
863
864 unit->content = vps;
865 }
866 break;
867 case HEVC_NAL_SPS:
868 {
869 H265RawSPS *sps;
870
871 sps = av_mallocz(sizeof(*sps));
872 if (!sps)
873 return AVERROR(ENOMEM);
874 err = cbs_h265_read_sps(ctx, &bc, sps);
875 if (err >= 0)
876 err = cbs_h265_replace_sps(ctx, sps);
877 if (err < 0) {
878 av_free(sps);
879 return err;
880 }
881
882 unit->content = sps;
883 }
884 break;
885
886 case HEVC_NAL_PPS:
887 {
888 H265RawPPS *pps;
889
890 pps = av_mallocz(sizeof(*pps));
891 if (!pps)
892 return AVERROR(ENOMEM);
893 err = cbs_h265_read_pps(ctx, &bc, pps);
894 if (err >= 0)
895 err = cbs_h265_replace_pps(ctx, pps);
896 if (err < 0) {
897 av_free(pps);
898 return err;
899 }
900
901 unit->content = pps;
902 }
903 break;
904
905 case HEVC_NAL_TRAIL_N:
906 case HEVC_NAL_TRAIL_R:
907 case HEVC_NAL_TSA_N:
908 case HEVC_NAL_TSA_R:
909 case HEVC_NAL_STSA_N:
910 case HEVC_NAL_STSA_R:
911 case HEVC_NAL_RADL_N:
912 case HEVC_NAL_RADL_R:
913 case HEVC_NAL_RASL_N:
914 case HEVC_NAL_RASL_R:
915 case HEVC_NAL_BLA_W_LP:
916 case HEVC_NAL_BLA_W_RADL:
917 case HEVC_NAL_BLA_N_LP:
918 case HEVC_NAL_IDR_W_RADL:
919 case HEVC_NAL_IDR_N_LP:
920 case HEVC_NAL_CRA_NUT:
921 {
922 H265RawSlice *slice;
923 int pos, len;
924
925 slice = av_mallocz(sizeof(*slice));
926 if (!slice)
927 return AVERROR(ENOMEM);
928 err = cbs_h265_read_slice_segment_header(ctx, &bc, &slice->header);
929 if (err < 0) {
930 av_free(slice);
931 return err;
932 }
933
934 pos = bitstream_tell(&bc);
935 len = unit->data_size;
936 if (!unit->data[len - 1]) {
937 int z;
938 for (z = 0; z < len && !unit->data[len - z - 1]; z++);
939 av_log(ctx->log_ctx, AV_LOG_DEBUG, "Deleted %d trailing zeroes "
940 "from slice data.\n", z);
941 len -= z;
942 }
943
944 slice->data_size = len - pos / 8;
945 slice->data = av_malloc(slice->data_size);
946 if (!slice->data) {
947 av_free(slice);
948 return AVERROR(ENOMEM);
949 }
950 memcpy(slice->data,
951 unit->data + pos / 8, slice->data_size);
952 slice->data_bit_start = pos % 8;
953
954 unit->content = slice;
955 }
956 break;
957
958 case HEVC_NAL_AUD:
959 {
960 H265RawAUD *aud;
961
962 aud = av_mallocz(sizeof(*aud));
963 if (!aud)
964 return AVERROR(ENOMEM);
965 err = cbs_h265_read_aud(ctx, &bc, aud);
966 if (err < 0) {
967 av_free(aud);
968 return err;
969 }
970
971 unit->content = aud;
972 }
973 break;
974
975 default:
976 return AVERROR(ENOSYS);
977 }
978
979 return 0;
980}
981
acf06f45
MT
982static int cbs_h264_write_nal_unit(CodedBitstreamContext *ctx,
983 CodedBitstreamUnit *unit,
984 PutBitContext *pbc)
985{
986 int err;
987
988 switch (unit->type) {
989 case H264_NAL_SPS:
990 {
991 H264RawSPS *sps = unit->content;
992
993 err = cbs_h264_write_sps(ctx, pbc, sps);
994 if (err < 0)
995 return err;
996
997 err = cbs_h264_replace_sps(ctx, sps);
998 if (err < 0)
999 return err;
1000 }
1001 break;
1002
1003 case H264_NAL_SPS_EXT:
1004 {
1005 H264RawSPSExtension *sps_ext;
1006
1007 err = cbs_h264_write_sps_extension(ctx, pbc, sps_ext);
1008 if (err < 0)
1009 return err;
1010 }
1011 break;
1012
1013 case H264_NAL_PPS:
1014 {
1015 H264RawPPS *pps = unit->content;
1016
1017 err = cbs_h264_write_pps(ctx, pbc, pps);
1018 if (err < 0)
1019 return err;
1020
1021 err = cbs_h264_replace_pps(ctx, pps);
1022 if (err < 0)
1023 return err;
1024 }
1025 break;
1026
1027 case H264_NAL_SLICE:
1028 case H264_NAL_IDR_SLICE:
1029 {
1030 H264RawSlice *slice = unit->content;
1031 BitstreamContext bc;
1032 int bits_left, end, zeroes;
1033
1034 err = cbs_h264_write_slice_header(ctx, pbc, &slice->header);
1035 if (err < 0)
1036 return err;
1037
1038 if (slice->data) {
1039 if (slice->data_size * 8 + 8 > put_bits_left(pbc))
1040 return AVERROR(ENOSPC);
1041
1042 bitstream_init(&bc, slice->data, slice->data_size * 8);
1043 bitstream_skip(&bc, slice->data_bit_start);
1044
1045 // Copy in two-byte blocks, but stop before copying the
1046 // rbsp_stop_one_bit in the final byte.
1047 while (bitstream_bits_left(&bc) > 23)
1048 put_bits(pbc, 16, bitstream_read(&bc, 16));
1049
1050 bits_left = bitstream_bits_left(&bc);
1051 end = bitstream_read(&bc, bits_left);
1052
1053 // rbsp_stop_one_bit must be present here.
1054 av_assert0(end);
1055 zeroes = ff_ctz(end);
1056 if (bits_left > zeroes + 1)
1057 put_bits(pbc, bits_left - zeroes - 1,
1058 end >> (zeroes + 1));
1059 put_bits(pbc, 1, 1);
1060 while (put_bits_count(pbc) % 8 != 0)
1061 put_bits(pbc, 1, 0);
1062 } else {
1063 // No slice data - that was just the header.
1064 // (Bitstream may be unaligned!)
1065 }
1066 }
1067 break;
1068
1069 case H264_NAL_AUD:
1070 {
1071 err = cbs_h264_write_aud(ctx, pbc, unit->content);
1072 if (err < 0)
1073 return err;
1074 }
1075 break;
1076
1077 case H264_NAL_SEI:
1078 {
1079 err = cbs_h264_write_sei(ctx, pbc, unit->content);
1080 if (err < 0)
1081 return err;
1082 }
1083 break;
1084
1085 default:
1086 av_log(ctx->log_ctx, AV_LOG_ERROR, "Write unimplemented for "
1087 "NAL unit type %"PRIu32".\n", unit->type);
1088 return AVERROR_PATCHWELCOME;
1089 }
1090
1091 return 0;
1092}
1093
867381b8
MT
1094static int cbs_h265_write_nal_unit(CodedBitstreamContext *ctx,
1095 CodedBitstreamUnit *unit,
1096 PutBitContext *pbc)
1097{
1098 int err;
1099
1100 switch (unit->type) {
1101 case HEVC_NAL_VPS:
1102 {
1103 H265RawVPS *vps = unit->content;
1104
1105 err = cbs_h265_write_vps(ctx, pbc, vps);
1106 if (err < 0)
1107 return err;
1108
1109 err = cbs_h265_replace_vps(ctx, vps);
1110 if (err < 0)
1111 return err;
1112 }
1113 break;
1114
1115 case HEVC_NAL_SPS:
1116 {
1117 H265RawSPS *sps = unit->content;
1118
1119 err = cbs_h265_write_sps(ctx, pbc, sps);
1120 if (err < 0)
1121 return err;
1122
1123 err = cbs_h265_replace_sps(ctx, sps);
1124 if (err < 0)
1125 return err;
1126 }
1127 break;
1128
1129 case HEVC_NAL_PPS:
1130 {
1131 H265RawPPS *pps = unit->content;
1132
1133 err = cbs_h265_write_pps(ctx, pbc, pps);
1134 if (err < 0)
1135 return err;
1136
1137 err = cbs_h265_replace_pps(ctx, pps);
1138 if (err < 0)
1139 return err;
1140 }
1141 break;
1142
1143 case HEVC_NAL_TRAIL_N:
1144 case HEVC_NAL_TRAIL_R:
1145 case HEVC_NAL_TSA_N:
1146 case HEVC_NAL_TSA_R:
1147 case HEVC_NAL_STSA_N:
1148 case HEVC_NAL_STSA_R:
1149 case HEVC_NAL_RADL_N:
1150 case HEVC_NAL_RADL_R:
1151 case HEVC_NAL_RASL_N:
1152 case HEVC_NAL_RASL_R:
1153 case HEVC_NAL_BLA_W_LP:
1154 case HEVC_NAL_BLA_W_RADL:
1155 case HEVC_NAL_BLA_N_LP:
1156 case HEVC_NAL_IDR_W_RADL:
1157 case HEVC_NAL_IDR_N_LP:
1158 case HEVC_NAL_CRA_NUT:
1159 {
1160 H265RawSlice *slice = unit->content;
1161 BitstreamContext bc;
1162 int bits_left, end, zeroes;
1163
1164 err = cbs_h265_write_slice_segment_header(ctx, pbc, &slice->header);
1165 if (err < 0)
1166 return err;
1167
1168 if (slice->data) {
1169 if (slice->data_size * 8 + 8 > put_bits_left(pbc))
1170 return AVERROR(ENOSPC);
1171
1172 bitstream_init(&bc, slice->data, slice->data_size * 8);
1173 bitstream_skip(&bc, slice->data_bit_start);
1174
1175 // Copy in two-byte blocks, but stop before copying the
1176 // rbsp_stop_one_bit in the final byte.
1177 while (bitstream_bits_left(&bc) > 23)
1178 put_bits(pbc, 16, bitstream_read(&bc, 16));
1179
1180 bits_left = bitstream_bits_left(&bc);
1181 end = bitstream_read(&bc, bits_left);
1182
1183 // rbsp_stop_one_bit must be present here.
1184 av_assert0(end);
1185 zeroes = ff_ctz(end);
1186 if (bits_left > zeroes + 1)
1187 put_bits(pbc, bits_left - zeroes - 1,
1188 end >> (zeroes + 1));
1189 put_bits(pbc, 1, 1);
1190 while (put_bits_count(pbc) % 8 != 0)
1191 put_bits(pbc, 1, 0);
1192 } else {
1193 // No slice data - that was just the header.
1194 }
1195 }
1196 break;
1197
1198 case HEVC_NAL_AUD:
1199 {
1200 err = cbs_h265_write_aud(ctx, pbc, unit->content);
1201 if (err < 0)
1202 return err;
1203 }
1204 break;
1205
1206 default:
1207 av_log(ctx->log_ctx, AV_LOG_ERROR, "Write unimplemented for "
1208 "NAL unit type %d.\n", unit->type);
1209 return AVERROR_PATCHWELCOME;
1210 }
1211
1212 return 0;
1213}
1214
acf06f45
MT
1215static int cbs_h2645_write_nal_unit(CodedBitstreamContext *ctx,
1216 CodedBitstreamUnit *unit)
1217{
1218 CodedBitstreamH2645Context *priv = ctx->priv_data;
1219 enum AVCodecID codec_id = ctx->codec->codec_id;
1220 PutBitContext pbc;
1221 int err;
1222
1223 if (!priv->write_buffer) {
1224 // Initial write buffer size is 1MB.
1225 priv->write_buffer_size = 1024 * 1024;
1226
1227 reallocate_and_try_again:
1228 err = av_reallocp(&priv->write_buffer, priv->write_buffer_size);
1229 if (err < 0) {
1230 av_log(ctx->log_ctx, AV_LOG_ERROR, "Unable to allocate a "
1231 "sufficiently large write buffer (last attempt "
1232 "%zu bytes).\n", priv->write_buffer_size);
1233 return err;
1234 }
1235 }
1236
1237 init_put_bits(&pbc, priv->write_buffer, priv->write_buffer_size);
1238
867381b8
MT
1239 if (codec_id == AV_CODEC_ID_H264)
1240 err = cbs_h264_write_nal_unit(ctx, unit, &pbc);
1241 else
1242 err = cbs_h265_write_nal_unit(ctx, unit, &pbc);
acf06f45
MT
1243
1244 if (err == AVERROR(ENOSPC)) {
1245 // Overflow.
1246 priv->write_buffer_size *= 2;
1247 goto reallocate_and_try_again;
1248 }
1249 // Overflow but we didn't notice.
1250 av_assert0(put_bits_count(&pbc) <= 8 * priv->write_buffer_size);
1251
768eb918
MT
1252 if (err < 0) {
1253 // Write failed for some other reason.
1254 return err;
1255 }
1256
acf06f45
MT
1257 if (put_bits_count(&pbc) % 8)
1258 unit->data_bit_padding = 8 - put_bits_count(&pbc) % 8;
1259 else
1260 unit->data_bit_padding = 0;
1261
1262 unit->data_size = (put_bits_count(&pbc) + 7) / 8;
1263 flush_put_bits(&pbc);
1264
1265 err = av_reallocp(&unit->data, unit->data_size);
1266 if (err < 0)
1267 return err;
1268
1269 memcpy(unit->data, priv->write_buffer, unit->data_size);
1270
1271 return 0;
1272}
1273
1274static int cbs_h2645_assemble_fragment(CodedBitstreamContext *ctx,
1275 CodedBitstreamFragment *frag)
1276{
1277 uint8_t *data;
1278 size_t max_size, dp, sp;
1279 int err, i, zero_run;
1280
1281 for (i = 0; i < frag->nb_units; i++) {
1282 // Data should already all have been written when we get here.
1283 av_assert0(frag->units[i].data);
1284 }
1285
1286 max_size = 0;
1287 for (i = 0; i < frag->nb_units; i++) {
1288 // Start code + content with worst-case emulation prevention.
1289 max_size += 3 + frag->units[i].data_size * 3 / 2;
1290 }
1291
1292 data = av_malloc(max_size);
1293 if (!data)
1294 return AVERROR(ENOMEM);
1295
1296 dp = 0;
1297 for (i = 0; i < frag->nb_units; i++) {
1298 CodedBitstreamUnit *unit = &frag->units[i];
1299
1300 if (unit->data_bit_padding > 0) {
1301 if (i < frag->nb_units - 1)
1302 av_log(ctx->log_ctx, AV_LOG_WARNING, "Probably invalid "
1303 "unaligned padding on non-final NAL unit.\n");
1304 else
1305 frag->data_bit_padding = unit->data_bit_padding;
1306 }
1307
867381b8
MT
1308 if ((ctx->codec->codec_id == AV_CODEC_ID_H264 &&
1309 (unit->type == H264_NAL_SPS ||
1310 unit->type == H264_NAL_PPS)) ||
1311 (ctx->codec->codec_id == AV_CODEC_ID_HEVC &&
1312 (unit->type == HEVC_NAL_VPS ||
1313 unit->type == HEVC_NAL_SPS ||
1314 unit->type == HEVC_NAL_PPS)) ||
acf06f45
MT
1315 i == 0 /* (Assume this is the start of an access unit.) */) {
1316 // zero_byte
1317 data[dp++] = 0;
1318 }
1319 // start_code_prefix_one_3bytes
1320 data[dp++] = 0;
1321 data[dp++] = 0;
1322 data[dp++] = 1;
1323
1324 zero_run = 0;
1325 for (sp = 0; sp < unit->data_size; sp++) {
1326 if (zero_run < 2) {
1327 if (unit->data[sp] == 0)
1328 ++zero_run;
1329 else
1330 zero_run = 0;
1331 } else {
1332 if ((unit->data[sp] & ~3) == 0) {
1333 // emulation_prevention_three_byte
1334 data[dp++] = 3;
1335 }
1336 zero_run = unit->data[sp] == 0;
1337 }
1338 data[dp++] = unit->data[sp];
1339 }
1340 }
1341
1342 av_assert0(dp <= max_size);
1343 err = av_reallocp(&data, dp);
1344 if (err)
1345 return err;
1346
1347 frag->data = data;
1348 frag->data_size = dp;
1349
1350 return 0;
1351}
1352
1353static void cbs_h264_close(CodedBitstreamContext *ctx)
1354{
1355 CodedBitstreamH264Context *h264 = ctx->priv_data;
1356 int i;
1357
1358 ff_h2645_packet_uninit(&h264->common.read_packet);
1359
1360 av_freep(&h264->common.write_buffer);
1361
1362 for (i = 0; i < FF_ARRAY_ELEMS(h264->sps); i++)
1363 av_freep(&h264->sps[i]);
1364 for (i = 0; i < FF_ARRAY_ELEMS(h264->pps); i++)
1365 av_freep(&h264->pps[i]);
1366}
1367
867381b8
MT
1368static void cbs_h265_close(CodedBitstreamContext *ctx)
1369{
1370 CodedBitstreamH265Context *h265 = ctx->priv_data;
1371 int i;
1372
1373 ff_h2645_packet_uninit(&h265->common.read_packet);
1374
1375 av_freep(&h265->common.write_buffer);
1376
1377 for (i = 0; i < FF_ARRAY_ELEMS(h265->vps); i++)
1378 av_freep(&h265->vps[i]);
1379 for (i = 0; i < FF_ARRAY_ELEMS(h265->sps); i++)
1380 av_freep(&h265->sps[i]);
1381 for (i = 0; i < FF_ARRAY_ELEMS(h265->pps); i++)
1382 av_freep(&h265->pps[i]);
1383}
1384
acf06f45
MT
1385const CodedBitstreamType ff_cbs_type_h264 = {
1386 .codec_id = AV_CODEC_ID_H264,
1387
1388 .priv_data_size = sizeof(CodedBitstreamH264Context),
1389
1390 .split_fragment = &cbs_h2645_split_fragment,
1391 .read_unit = &cbs_h264_read_nal_unit,
1392 .write_unit = &cbs_h2645_write_nal_unit,
1393 .assemble_fragment = &cbs_h2645_assemble_fragment,
1394
1395 .free_unit = &cbs_h264_free_nal_unit,
1396 .close = &cbs_h264_close,
1397};
867381b8
MT
1398
1399const CodedBitstreamType ff_cbs_type_h265 = {
1400 .codec_id = AV_CODEC_ID_HEVC,
1401
1402 .priv_data_size = sizeof(CodedBitstreamH265Context),
1403
1404 .split_fragment = &cbs_h2645_split_fragment,
1405 .read_unit = &cbs_h265_read_nal_unit,
1406 .write_unit = &cbs_h2645_write_nal_unit,
1407 .assemble_fragment = &cbs_h2645_assemble_fragment,
1408
1409 .free_unit = &cbs_h265_free_nal_unit,
1410 .close = &cbs_h265_close,
1411};