1 /*
2 * This file is part of FFmpeg.
3 *
4 * FFmpeg is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * FFmpeg is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with FFmpeg; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
21
33
34
36 const char *
name,
const int *subscripts,
37 uint32_t *write_to,
38 uint32_t range_min, uint32_t range_max)
39 {
40 uint32_t leading_bits,
value;
41 int max_length, leading_zeroes;
42
44
46
48 if (leading_bits == 0) {
49 if (max_length >= 32) {
51 "%s: more than 31 zeroes.\n",
name);
53 } else {
55 "%s: bitstream ended.\n",
name);
57 }
58 }
59
60 leading_zeroes = max_length - 1 -
av_log2(leading_bits);
62
65 "%s: bitstream ended.\n",
name);
67 }
68
70
72
73 if (value < range_min || value > range_max) {
75 "%"PRIu32", but must be in [%"PRIu32",%"PRIu32"].\n",
78 }
79
81 return 0;
82 }
83
85 const char *
name,
const int *subscripts,
88 {
89 uint32_t leading_bits, unsigned_value;
90 int max_length, leading_zeroes;
92
94
96
98 if (leading_bits == 0) {
99 if (max_length >= 32) {
101 "%s: more than 31 zeroes.\n",
name);
103 } else {
105 "%s: bitstream ended.\n",
name);
107 }
108 }
109
110 leading_zeroes = max_length - 1 -
av_log2(leading_bits);
112
115 "%s: bitstream ended.\n",
name);
117 }
118
120
121 if (unsigned_value & 1)
123 else
124 value = unsigned_value / 2;
125
127
128 if (value < range_min || value > range_max) {
130 "%"PRId32", but must be in [%"PRId32",%"PRId32"].\n",
133 }
134
136 return 0;
137 }
138
140 const char *
name,
const int *subscripts,
142 uint32_t range_min, uint32_t range_max)
143 {
145
147
148 if (value < range_min || value > range_max) {
150 "%"PRIu32", but must be in [%"PRIu32",%"PRIu32"].\n",
153 }
155
159
163 else
165
167
168 return 0;
169 }
170
172 const char *
name,
const int *subscripts,
175 {
177 uint32_t uvalue;
178
180
181 if (value < range_min || value > range_max) {
183 "%"PRId32", but must be in [%"PRId32",%"PRId32"].\n",
186 }
188
190 uvalue = 0;
192 uvalue = 2 * (uint32_t)
value - 1;
193 else
194 uvalue = 2 * (uint32_t)-
value;
195
199
203 else
205
207
208 return 0;
209 }
210
211 // payload_extension_present() - true if we are before the last 1-bit
212 // in the payload structure, which must be in the last byte.
214 int cur_pos)
215 {
216 int bits_left = payload_size * 8 - cur_pos;
219 }
220
221 #define HEADER(name) do { \
222 ff_cbs_trace_header(ctx, name); \
223 } while (0)
224
225 #define CHECK(call) do { \
226 err = (call); \
227 if (err < 0) \
228 return err; \
229 } while (0)
230
231 #define FUNC_NAME2(rw, codec, name) cbs_ ## codec ## _ ## rw ## _ ## name
232 #define FUNC_NAME1(rw, codec, name) FUNC_NAME2(rw, codec, name)
233 #define FUNC_H264(name) FUNC_NAME1(READWRITE, h264, name)
234 #define FUNC_H265(name) FUNC_NAME1(READWRITE, h265, name)
235 #define FUNC_H266(name) FUNC_NAME1(READWRITE, h266, name)
236 #define FUNC_SEI(name) FUNC_NAME1(READWRITE, sei, name)
237
238 #define SEI_FUNC(name, args) \
239 static int FUNC(name) args; \
240 static int FUNC(name ## _internal)(CodedBitstreamContext *ctx, \
241 RWContext *rw, void *cur, \
242 SEIMessageState *state) \
243 { \
244 return FUNC(name)(ctx, rw, cur, state); \
245 } \
246 static int FUNC(name) args
247
248 #define SUBSCRIPTS(subs, ...) (subs > 0 ? ((int[subs + 1]){ subs, __VA_ARGS__ }) : NULL)
249
250 #define u(width, name, range_min, range_max) \
251 xu(width, name, current->name, range_min, range_max, 0, )
252 #define flag(name) ub(1, name)
253 #define ue(name, range_min, range_max) \
254 xue(name, current->name, range_min, range_max, 0, )
255 #define i(width, name, range_min, range_max) \
256 xi(width, name, current->name, range_min, range_max, 0, )
257 #define ib(width, name) \
258 xi(width, name, current->name, MIN_INT_BITS(width), MAX_INT_BITS(width), 0, )
259 #define se(name, range_min, range_max) \
260 xse(name, current->name, range_min, range_max, 0, )
261
262 #define us(width, name, range_min, range_max, subs, ...) \
263 xu(width, name, current->name, range_min, range_max, subs, __VA_ARGS__)
264 #define ubs(width, name, subs, ...) \
265 xu(width, name, current->name, 0, MAX_UINT_BITS(width), subs, __VA_ARGS__)
266 #define flags(name, subs, ...) \
267 xu(1, name, current->name, 0, 1, subs, __VA_ARGS__)
268 #define ues(name, range_min, range_max, subs, ...) \
269 xue(name, current->name, range_min, range_max, subs, __VA_ARGS__)
270 #define is(width, name, range_min, range_max, subs, ...) \
271 xi(width, name, current->name, range_min, range_max, subs, __VA_ARGS__)
272 #define ibs(width, name, subs, ...) \
273 xi(width, name, current->name, MIN_INT_BITS(width), MAX_INT_BITS(width), subs, __VA_ARGS__)
274 #define ses(name, range_min, range_max, subs, ...) \
275 xse(name, current->name, range_min, range_max, subs, __VA_ARGS__)
276
277 #define fixed(width, name, value) do { \
278 av_unused uint32_t fixed_value = value; \
279 xu(width, name, fixed_value, value, value, 0, ); \
280 } while (0)
281
282
284 #define READWRITE read
285 #define RWContext GetBitContext
286
287 #define ub(width, name) do { \
288 uint32_t value; \
289 CHECK(ff_cbs_read_simple_unsigned(ctx, rw, width, #name, \
290 &value)); \
291 current->name = value; \
292 } while (0)
293 #define xu(width, name, var, range_min, range_max, subs, ...) do { \
294 uint32_t value; \
295 CHECK(ff_cbs_read_unsigned(ctx, rw, width, #name, \
296 SUBSCRIPTS(subs, __VA_ARGS__), \
297 &value, range_min, range_max)); \
298 var = value; \
299 } while (0)
300 #define xue(name, var, range_min, range_max, subs, ...) do { \
301 uint32_t value; \
302 CHECK(cbs_read_ue_golomb(ctx, rw, #name, \
303 SUBSCRIPTS(subs, __VA_ARGS__), \
304 &value, range_min, range_max)); \
305 var = value; \
306 } while (0)
307 #define xi(width, name, var, range_min, range_max, subs, ...) do { \
308 int32_t value; \
309 CHECK(ff_cbs_read_signed(ctx, rw, width, #name, \
310 SUBSCRIPTS(subs, __VA_ARGS__), \
311 &value, range_min, range_max)); \
312 var = value; \
313 } while (0)
314 #define xse(name, var, range_min, range_max, subs, ...) do { \
315 int32_t value; \
316 CHECK(cbs_read_se_golomb(ctx, rw, #name, \
317 SUBSCRIPTS(subs, __VA_ARGS__), \
318 &value, range_min, range_max)); \
319 var = value; \
320 } while (0)
321
322
323 #define infer(name, value) do { \
324 current->name = value; \
325 } while (0)
326
328 {
331 return 1;
333 return 0;
335 return 1;
336 return 0;
337 }
338
339 #define more_rbsp_data(var) ((var) = cbs_h2645_read_more_rbsp_data(rw))
340
341 #define bit_position(rw) (get_bits_count(rw))
342 #define byte_alignment(rw) (get_bits_count(rw) % 8)
343
344 /* The CBS SEI code uses the refstruct API for the allocation
345 * of its child buffers. */
346 #define allocate(name, size) do { \
347 name = ff_refstruct_allocz(size + \
348 AV_INPUT_BUFFER_PADDING_SIZE); \
349 if (!name) \
350 return AVERROR(ENOMEM); \
351 } while (0)
352
353 #define FUNC(name) FUNC_SEI(name)
355 #undef FUNC
356
357 #undef allocate
358
359 /* The other code uses the refstruct API for the allocation
360 * of its child buffers. */
361 #define allocate(name, size) do { \
362 name ## _ref = av_buffer_allocz(size + \
363 AV_INPUT_BUFFER_PADDING_SIZE); \
364 if (!name ## _ref) \
365 return AVERROR(ENOMEM); \
366 name = name ## _ref->data; \
367 } while (0)
368
369 #define FUNC(name) FUNC_H264(name)
371 #undef FUNC
372
373 #define FUNC(name) FUNC_H265(name)
375 #undef FUNC
376
377 #define FUNC(name) FUNC_H266(name)
379 #undef FUNC
380
381 #undef READ
382 #undef READWRITE
383 #undef RWContext
384 #undef ub
385 #undef xu
386 #undef xi
387 #undef xue
388 #undef xse
389 #undef infer
390 #undef more_rbsp_data
391 #undef bit_position
392 #undef byte_alignment
393 #undef allocate
394
395
397 #define READWRITE write
398 #define RWContext PutBitContext
399
400 #define ub(width, name) do { \
401 uint32_t value = current->name; \
402 CHECK(ff_cbs_write_simple_unsigned(ctx, rw, width, #name, \
403 value)); \
404 } while (0)
405 #define xu(width, name, var, range_min, range_max, subs, ...) do { \
406 uint32_t value = var; \
407 CHECK(ff_cbs_write_unsigned(ctx, rw, width, #name, \
408 SUBSCRIPTS(subs, __VA_ARGS__), \
409 value, range_min, range_max)); \
410 } while (0)
411 #define xue(name, var, range_min, range_max, subs, ...) do { \
412 uint32_t value = var; \
413 CHECK(cbs_write_ue_golomb(ctx, rw, #name, \
414 SUBSCRIPTS(subs, __VA_ARGS__), \
415 value, range_min, range_max)); \
416 } while (0)
417 #define xi(width, name, var, range_min, range_max, subs, ...) do { \
418 int32_t value = var; \
419 CHECK(ff_cbs_write_signed(ctx, rw, width, #name, \
420 SUBSCRIPTS(subs, __VA_ARGS__), \
421 value, range_min, range_max)); \
422 } while (0)
423 #define xse(name, var, range_min, range_max, subs, ...) do { \
424 int32_t value = var; \
425 CHECK(cbs_write_se_golomb(ctx, rw, #name, \
426 SUBSCRIPTS(subs, __VA_ARGS__), \
427 value, range_min, range_max)); \
428 } while (0)
429
430 #define infer(name, value) do { \
431 if (current->name != (value)) { \
432 av_log(ctx->log_ctx, AV_LOG_ERROR, \
433 "%s does not match inferred value: " \
434 "%"PRId64", but should be %"PRId64".\n", \
435 #name, (int64_t)current->name, (int64_t)(value)); \
436 return AVERROR_INVALIDDATA; \
437 } \
438 } while (0)
439
440 #define more_rbsp_data(var) (var)
441
442 #define bit_position(rw) (put_bits_count(rw))
443 #define byte_alignment(rw) (put_bits_count(rw) % 8)
444
445 #define allocate(name, size) do { \
446 if (!name) { \
447 av_log(ctx->log_ctx, AV_LOG_ERROR, "%s must be set " \
448 "for writing.\n", #name); \
449 return AVERROR_INVALIDDATA; \
450 } \
451 } while (0)
452
453 #define FUNC(name) FUNC_SEI(name)
455 #undef FUNC
456
457 #define FUNC(name) FUNC_H264(name)
459 #undef FUNC
460
461 #define FUNC(name) FUNC_H265(name)
463 #undef FUNC
464
465 #define FUNC(name) FUNC_H266(name)
467 #undef FUNC
468
469 #undef WRITE
470 #undef READWRITE
471 #undef RWContext
472 #undef ub
473 #undef xu
474 #undef xi
475 #undef xue
476 #undef xse
477 #undef u
478 #undef i
479 #undef flag
480 #undef ue
481 #undef se
482 #undef infer
483 #undef more_rbsp_data
484 #undef bit_position
485 #undef byte_alignment
486 #undef allocate
487
488
492 {
494
500
502 continue;
503
504 // Remove trailing zeroes.
509 continue;
510 }
511
513 :
packet->rbsp.rbsp_buffer_ref;
514
517 if (err < 0)
518 return err;
519 }
520
521 return 0;
522 }
523
527 {
531 int err;
532
535 return 0;
536
538 // AVCC header.
539 size_t size, start, end;
541
543
545
548
549 version = bytestream2_get_byte(&gbc);
554 }
555
558
559 // SPS array.
560 count = bytestream2_get_byte(&gbc) & 0x1f;
562 for (
i = 0;
i < count;
i++) {
565 size = bytestream2_get_be16(&gbc);
569 }
571
573 frag->
data + start, end - start,
575 if (err < 0) {
577 return err;
578 }
580 if (err < 0)
581 return err;
582
583 // PPS array.
584 count = bytestream2_get_byte(&gbc);
586 for (
i = 0;
i < count;
i++) {
589 size = bytestream2_get_be16(&gbc);
593 }
595
597 frag->
data + start, end - start,
599 if (err < 0) {
601 return err;
602 }
604 if (err < 0)
605 return err;
606
610 }
611
613 // HVCC header.
614 size_t size, start, end;
615 int i, j, nb_arrays, nal_unit_type, nb_nals,
version;
616
618
620
623
624 version = bytestream2_get_byte(&gbc);
629 }
630
633
634 nb_arrays = bytestream2_get_byte(&gbc);
635 for (
i = 0;
i < nb_arrays;
i++) {
636 nal_unit_type = bytestream2_get_byte(&gbc) & 0x3f;
637 nb_nals = bytestream2_get_be16(&gbc);
638
640 for (j = 0; j < nb_nals; j++) {
643 size = bytestream2_get_be16(&gbc);
647 }
649
651 frag->
data + start, end - start,
653 if (err < 0) {
655 "HVCC array %d (%d NAL units of type %d).\n",
656 i, nb_nals, nal_unit_type);
657 return err;
658 }
660 if (err < 0)
661 return err;
662 }
663
665 // VVCC header.
666 int ptl_present_flag, num_arrays;
668
670
672
673 b = bytestream2_get_byte(&gbc);
675 ptl_present_flag =
b & 1;
676
677 if(ptl_present_flag) {
678 int num_sublayers, num_bytes_constraint_info, num_sub_profiles;
679 num_sublayers = (bytestream2_get_be16u(&gbc) >> 4) & 7;
681
682 // begin VvcPTLRecord(num_sublayers);
683 num_bytes_constraint_info = bytestream2_get_byte(&gbc) & 0x3f;
685 if(num_sublayers > 1) {
686 int count_present_flags = 0;
687 b = bytestream2_get_byte(&gbc);
688 for(
i = num_sublayers - 2;
i >= 0;
i--) {
689 if((
b >> (7 - (num_sublayers - 2 -
i))) & 0x01)
690 count_present_flags++;
691 }
693 }
694 num_sub_profiles = bytestream2_get_byte(&gbc);
696 // end VvcPTLRecord(num_sublayers);
697
699 }
700
701 num_arrays = bytestream2_get_byte(&gbc);
702 for(j = 0; j < num_arrays; j++) {
703 size_t start, end,
size;
704 int nal_unit_type = bytestream2_get_byte(&gbc) & 0x1f;
705 unsigned int num_nalus = 1;
707 num_nalus = bytestream2_get_be16(&gbc);
708
710 for(
i = 0;
i < num_nalus;
i++) {
713 size = bytestream2_get_be16(&gbc);
717 }
719
721 frag->
data + start, end - start,
723 if (err < 0) {
725 "VVCC array %d (%d NAL units of type %d).\n",
726 i, num_nalus, nal_unit_type);
727 return err;
728 }
730 if (err < 0)
731 return err;
732 }
733 } else {
734 // Annex B, or later MP4 with already-known parameters.
735
741 if (err < 0)
742 return err;
743
745 if (err < 0)
746 return err;
747 }
748
749 return 0;
750 }
751
752 #define cbs_h2645_replace_ps(h26n, ps_name, ps_var, id_element) \
753 static int cbs_h26 ## h26n ## _replace_ ## ps_var(CodedBitstreamContext *ctx, \
754 CodedBitstreamUnit *unit) \
755 { \
756 CodedBitstreamH26 ## h26n ## Context *priv = ctx->priv_data; \
757 H26 ## h26n ## Raw ## ps_name *ps_var = unit->content; \
758 unsigned int id = ps_var->id_element; \
759 int err = ff_cbs_make_unit_refcounted(ctx, unit); \
760 if (err < 0) \
761 return err; \
762 if (priv->ps_var[id] == priv->active_ ## ps_var) \
763 priv->active_ ## ps_var = NULL ; \
764 av_assert0(unit->content_ref); \
765 ff_refstruct_replace(&priv->ps_var[id], unit->content_ref); \
766 return 0; \
767 }
768
774
775 #define cbs_h266_replace_ps(h26n, ps_name, ps_var, id_element) \
776 static int cbs_h26 ## h26n ## _replace_ ## ps_var(CodedBitstreamContext *ctx, \
777 CodedBitstreamUnit *unit) \
778 { \
779 CodedBitstreamH26 ## h26n ## Context *priv = ctx->priv_data; \
780 H26 ## h26n ## Raw ## ps_name *ps_var = unit->content; \
781 unsigned int id = ps_var->id_element; \
782 int err = ff_cbs_make_unit_refcounted(ctx, unit); \
783 if (err < 0) \
784 return err; \
785 av_assert0(unit->content_ref); \
786 ff_refstruct_replace(&priv->ps_var[id], unit->content_ref); \
787 return 0; \
788 }
789
793
797 {
799 int err;
800
802 if (err < 0)
803 return err;
807 return 0;
808 }
809
812 {
814 int err;
815
817 if (err < 0)
818 return err;
819
821 if (err < 0)
822 return err;
823
824 switch (unit->
type) {
826 {
828
829 err = cbs_h264_read_sps(
ctx, &gbc,
sps);
830 if (err < 0)
831 return err;
832
833 err = cbs_h264_replace_sps(
ctx, unit);
834 if (err < 0)
835 return err;
836 }
837 break;
838
840 {
841 err = cbs_h264_read_sps_extension(
ctx, &gbc, unit->
content);
842 if (err < 0)
843 return err;
844 }
845 break;
846
848 {
850
851 err = cbs_h264_read_pps(
ctx, &gbc,
pps);
852 if (err < 0)
853 return err;
854
855 err = cbs_h264_replace_pps(
ctx, unit);
856 if (err < 0)
857 return err;
858 }
859 break;
860
864 {
867
868 err = cbs_h264_read_slice_header(
ctx, &gbc, &slice->
header);
869 if (err < 0)
870 return err;
871
874
877
884 }
885 break;
886
888 {
889 err = cbs_h264_read_aud(
ctx, &gbc, unit->
content);
890 if (err < 0)
891 return err;
892 }
893 break;
894
896 {
897 err = cbs_h264_read_sei(
ctx, &gbc, unit->
content);
898 if (err < 0)
899 return err;
900 }
901 break;
902
904 {
905 err = cbs_h264_read_filler(
ctx, &gbc, unit->
content);
906 if (err < 0)
907 return err;
908 }
909 break;
910
913 {
915 cbs_h264_read_end_of_sequence :
916 cbs_h264_read_end_of_stream)(
ctx, &gbc, unit->
content);
917 if (err < 0)
918 return err;
919 }
920 break;
921
922 default:
924 }
925
926 return 0;
927 }
928
931 {
933 int err;
934
936 if (err < 0)
937 return err;
938
940 if (err < 0)
941 return err;
942
943 switch (unit->
type) {
945 {
947
948 err = cbs_h265_read_vps(
ctx, &gbc,
vps);
949 if (err < 0)
950 return err;
951
952 err = cbs_h265_replace_vps(
ctx, unit);
953 if (err < 0)
954 return err;
955 }
956 break;
958 {
960
961 err = cbs_h265_read_sps(
ctx, &gbc,
sps);
962 if (err < 0)
963 return err;
964
965 err = cbs_h265_replace_sps(
ctx, unit);
966 if (err < 0)
967 return err;
968 }
969 break;
970
972 {
974
975 err = cbs_h265_read_pps(
ctx, &gbc,
pps);
976 if (err < 0)
977 return err;
978
979 err = cbs_h265_replace_pps(
ctx, unit);
980 if (err < 0)
981 return err;
982 }
983 break;
984
1001 {
1004
1005 err = cbs_h265_read_slice_segment_header(
ctx, &gbc, &slice->
header);
1006 if (err < 0)
1007 return err;
1008
1011
1014
1021 }
1022 break;
1023
1025 {
1026 err = cbs_h265_read_aud(
ctx, &gbc, unit->
content);
1027 if (err < 0)
1028 return err;
1029 }
1030 break;
1031
1034 {
1035 err = cbs_h265_read_sei(
ctx, &gbc, unit->
content,
1037
1038 if (err < 0)
1039 return err;
1040 }
1041 break;
1042
1043 default:
1045 }
1046
1047 return 0;
1048 }
1049
1052 {
1054 int err;
1055
1057 if (err < 0)
1058 return err;
1059
1061 if (err < 0)
1062 return err;
1063
1064 switch (unit->
type) {
1066 {
1067 err = cbs_h266_read_dci(
ctx, &gbc, unit->
content);
1068
1069 if (err < 0)
1070 return err;
1071 }
1072 break;
1074 {
1075 err = cbs_h266_read_opi(
ctx, &gbc, unit->
content);
1076
1077 if (err < 0)
1078 return err;
1079 }
1080 break;
1082 {
1084
1085 err = cbs_h266_read_vps(
ctx, &gbc,
vps);
1086 if (err < 0)
1087 return err;
1088
1089 err = cbs_h266_replace_vps(
ctx, unit);
1090 if (err < 0)
1091 return err;
1092 }
1093 break;
1095 {
1097
1098 err = cbs_h266_read_sps(
ctx, &gbc,
sps);
1099 if (err < 0)
1100 return err;
1101
1102 err = cbs_h266_replace_sps(
ctx, unit);
1103 if (err < 0)
1104 return err;
1105 }
1106 break;
1107
1109 {
1111
1112 err = cbs_h266_read_pps(
ctx, &gbc,
pps);
1113 if (err < 0)
1114 return err;
1115
1116 err = cbs_h266_replace_pps(
ctx, unit);
1117 if (err < 0)
1118 return err;
1119 }
1120 break;
1121
1124 {
1125 err = cbs_h266_read_aps(
ctx, &gbc, unit->
content,
1127
1128 if (err < 0)
1129 return err;
1130 }
1131 break;
1133 {
1135 err = cbs_h266_read_ph(
ctx, &gbc,
ph);
1136 if (err < 0)
1137 return err;
1138 err = cbs_h266_replace_ph(
ctx, unit, &
ph->ph_picture_header);
1139 if (err < 0)
1140 return err;
1141 }
1142 break;
1143
1152 {
1155
1156 err = cbs_h266_read_slice_header(
ctx, &gbc, &slice->
header);
1157 if (err < 0)
1158 return err;
1159
1162
1165
1168 if (err < 0)
1169 return err;
1170 }
1171
1179 }
1180 break;
1181
1183 {
1184 err = cbs_h266_read_aud(
ctx, &gbc, unit->
content);
1185 if (err < 0)
1186 return err;
1187 }
1188 break;
1189
1192 {
1193 err = cbs_h266_read_sei(
ctx, &gbc, unit->
content,
1195
1196 if (err < 0)
1197 return err;
1198 }
1199 break;
1200
1201 default:
1203 }
1204 return 0;
1205 }
1206
1209 size_t data_size, int data_bit_start)
1210 {
1211 size_t rest = data_size - (data_bit_start + 7) / 8;
1212 const uint8_t *
pos =
data + data_bit_start / 8;
1213
1215 data_size > data_bit_start / 8);
1216
1219
1220 if (!rest)
1221 goto rbsp_stop_one_bit;
1222
1223 // First copy the remaining bits of the first byte
1224 // The above check ensures that we do not accidentally
1225 // copy beyond the rbsp_stop_one_bit.
1226 if (data_bit_start % 8)
1227 put_bits(pbc, 8 - data_bit_start % 8,
1229
1231 // If the writer is aligned at this point,
1232 // memcpy can be used to improve performance.
1233 // This happens normally for CABAC.
1237 } else {
1238 // If not, we have to copy manually.
1239 // rbsp_stop_one_bit forces us to special-case
1240 // the last byte.
1243
1244 for (; rest > 4; rest -= 4,
pos += 4)
1246
1247 for (; rest > 1; rest--,
pos++)
1249
1250 rbsp_stop_one_bit:
1252
1256 i = rest ? (8 -
i) : (8 -
i - data_bit_start % 8);
1260 }
1261
1262 return 0;
1263 }
1264
1268 {
1269 int err;
1270
1271 switch (unit->
type) {
1273 {
1275
1276 err = cbs_h264_write_sps(
ctx, pbc,
sps);
1277 if (err < 0)
1278 return err;
1279
1280 err = cbs_h264_replace_sps(
ctx, unit);
1281 if (err < 0)
1282 return err;
1283 }
1284 break;
1285
1287 {
1289
1290 err = cbs_h264_write_sps_extension(
ctx, pbc, sps_ext);
1291 if (err < 0)
1292 return err;
1293 }
1294 break;
1295
1297 {
1299
1300 err = cbs_h264_write_pps(
ctx, pbc,
pps);
1301 if (err < 0)
1302 return err;
1303
1304 err = cbs_h264_replace_pps(
ctx, unit);
1305 if (err < 0)
1306 return err;
1307 }
1308 break;
1309
1313 {
1315
1316 err = cbs_h264_write_slice_header(
ctx, pbc, &slice->
header);
1317 if (err < 0)
1318 return err;
1319
1324 if (err < 0)
1325 return err;
1326 } else {
1327 // No slice data - that was just the header.
1328 // (Bitstream may be unaligned!)
1329 }
1330 }
1331 break;
1332
1334 {
1335 err = cbs_h264_write_aud(
ctx, pbc, unit->
content);
1336 if (err < 0)
1337 return err;
1338 }
1339 break;
1340
1342 {
1343 err = cbs_h264_write_sei(
ctx, pbc, unit->
content);
1344 if (err < 0)
1345 return err;
1346 }
1347 break;
1348
1350 {
1351 err = cbs_h264_write_filler(
ctx, pbc, unit->
content);
1352 if (err < 0)
1353 return err;
1354 }
1355 break;
1356
1358 {
1359 err = cbs_h264_write_end_of_sequence(
ctx, pbc, unit->
content);
1360 if (err < 0)
1361 return err;
1362 }
1363 break;
1364
1366 {
1367 err = cbs_h264_write_end_of_stream(
ctx, pbc, unit->
content);
1368 if (err < 0)
1369 return err;
1370 }
1371 break;
1372
1373 default:
1375 "NAL unit type %"PRIu32
".\n", unit->
type);
1377 }
1378
1379 return 0;
1380 }
1381
1385 {
1386 int err;
1387
1388 switch (unit->
type) {
1390 {
1392
1393 err = cbs_h265_write_vps(
ctx, pbc,
vps);
1394 if (err < 0)
1395 return err;
1396
1397 err = cbs_h265_replace_vps(
ctx, unit);
1398 if (err < 0)
1399 return err;
1400 }
1401 break;
1402
1404 {
1406
1407 err = cbs_h265_write_sps(
ctx, pbc,
sps);
1408 if (err < 0)
1409 return err;
1410
1411 err = cbs_h265_replace_sps(
ctx, unit);
1412 if (err < 0)
1413 return err;
1414 }
1415 break;
1416
1418 {
1420
1421 err = cbs_h265_write_pps(
ctx, pbc,
pps);
1422 if (err < 0)
1423 return err;
1424
1425 err = cbs_h265_replace_pps(
ctx, unit);
1426 if (err < 0)
1427 return err;
1428 }
1429 break;
1430
1447 {
1449
1450 err = cbs_h265_write_slice_segment_header(
ctx, pbc, &slice->
header);
1451 if (err < 0)
1452 return err;
1453
1458 if (err < 0)
1459 return err;
1460 } else {
1461 // No slice data - that was just the header.
1462 }
1463 }
1464 break;
1465
1467 {
1468 err = cbs_h265_write_aud(
ctx, pbc, unit->
content);
1469 if (err < 0)
1470 return err;
1471 }
1472 break;
1473
1476 {
1477 err = cbs_h265_write_sei(
ctx, pbc, unit->
content,
1479
1480 if (err < 0)
1481 return err;
1482 }
1483 break;
1484
1485 default:
1487 "NAL unit type %"PRIu32
".\n", unit->
type);
1489 }
1490
1491 return 0;
1492 }
1493
1497 {
1500 int slice_type_i, slice_type_b, slice_type_si;
1501
1503 return 0;
1504
1505 // keep non-VCL
1509 return 0;
1510
1512 return 1;
1513
1515 return 1;
1516
1520 "h264 nal unit header is null, missing decompose?\n");
1521 return 0;
1522 }
1523
1525 return 1;
1526
1530 "h264 slice header is null, missing decompose?\n");
1531 return 0;
1532 }
1533
1537
1539 return 1;
1541 return 1;
1542
1543 return 0;
1544 }
1545
1549 {
1551
1553 return 0;
1554
1555 switch (unit->
type) {
1562 // IRAP slice
1564 return 0;
1565 break;
1566
1577 // Slice
1578 break;
1579 default:
1580 // Don't discard non-slice nal.
1581 return 0;
1582 }
1583
1585 return 1;
1586
1590 "h265 slice header is null, missing decompose?\n");
1591 return 0;
1592 }
1593
1595 return 1;
1597 return 1;
1598
1600 switch (unit->
type) {
1609 // non-ref
1610 return 1;
1611 default:
1612 break;
1613 }
1614 }
1615
1616 return 0;
1617 }
1618
1622 {
1623 int err;
1624
1625 switch (unit->
type) {
1627 {
1629
1630 err = cbs_h266_write_dci(
ctx, pbc,
dci);
1631 if (err < 0)
1632 return err;
1633 }
1634 break;
1636 {
1638
1639 err = cbs_h266_write_opi(
ctx, pbc,
opi);
1640 if (err < 0)
1641 return err;
1642 }
1643 break;
1645 {
1647
1648 err = cbs_h266_write_vps(
ctx, pbc,
vps);
1649 if (err < 0)
1650 return err;
1651
1652 err = cbs_h266_replace_vps(
ctx, unit);
1653 if (err < 0)
1654 return err;
1655 }
1656 break;
1658 {
1660
1661 err = cbs_h266_write_sps(
ctx, pbc,
sps);
1662 if (err < 0)
1663 return err;
1664
1665 err = cbs_h266_replace_sps(
ctx, unit);
1666 if (err < 0)
1667 return err;
1668 }
1669 break;
1670
1672 {
1674
1675 err = cbs_h266_write_pps(
ctx, pbc,
pps);
1676 if (err < 0)
1677 return err;
1678
1679 err = cbs_h266_replace_pps(
ctx, unit);
1680 if (err < 0)
1681 return err;
1682 }
1683 break;
1684
1687 {
1688 err = cbs_h266_write_aps(
ctx, pbc, unit->
content,
1690 if (err < 0)
1691 return err;
1692 }
1693 break;
1695 {
1697 err = cbs_h266_write_ph(
ctx, pbc,
ph);
1698 if (err < 0)
1699 return err;
1700
1701 err = cbs_h266_replace_ph(
ctx, unit, &
ph->ph_picture_header);
1702 if (err < 0)
1703 return err;
1704 }
1705 break;
1706
1715 {
1717
1718 err = cbs_h266_write_slice_header(
ctx, pbc, &slice->
header);
1719 if (err < 0)
1720 return err;
1721
1724 if (err < 0)
1725 return err;
1726 }
1727
1732 if (err < 0)
1733 return err;
1734 } else {
1735 // No slice data - that was just the header.
1736 }
1737 }
1738 break;
1739
1741 {
1742 err = cbs_h266_write_aud(
ctx, pbc, unit->
content);
1743 if (err < 0)
1744 return err;
1745 }
1746 break;
1747
1750 {
1751 err = cbs_h266_write_sei(
ctx, pbc, unit->
content,
1753
1754 if (err < 0)
1755 return err;
1756 }
1757 break;
1758
1759 default:
1761 "NAL unit type %"PRIu32
".\n", unit->
type);
1763 }
1764
1765 return 0;
1766 }
1767
1770 int nal_unit_index)
1771 {
1772 // Section B.1.2 in H.264, section B.2.2 in H.265, H.266.
1773 if (nal_unit_index == 0) {
1774 // Assume that this is the first NAL unit in an access unit.
1775 return 1;
1776 }
1783 return 0;
1784 }
1785
1788 {
1790 size_t max_size, dp,
sp;
1791 int err,
i, zero_run;
1792
1794 // Data should already all have been written when we get here.
1796 }
1797
1798 max_size = 0;
1800 // Start code + content with worst-case emulation prevention.
1802 }
1803
1807
1808 dp = 0;
1811
1813 if (i < frag->nb_units - 1)
1815 "unaligned padding on non-final NAL unit.\n");
1816 else
1818 }
1819
1821 // zero_byte
1823 }
1824 // start_code_prefix_one_3bytes
1828
1829 zero_run = 0;
1831 if (zero_run < 2) {
1833 ++zero_run;
1834 else
1835 zero_run = 0;
1836 } else {
1837 if ((unit->
data[
sp] & ~3) == 0) {
1838 // emulation_prevention_three_byte
1840 }
1841 zero_run = unit->
data[
sp] == 0;
1842 }
1844 }
1845 }
1846
1849 if (err)
1850 return err;
1852
1858 }
1859
1862
1863 return 0;
1864 }
1865
1867 {
1869
1874
1878 }
1879
1881 {
1884
1886
1891 }
1892
1894 {
1896
1903
1907 }
1908
1910 {
1913
1915
1922 }
1923
1925 {
1927
1935 }
1936
1938 {
1940
1943 }
1944
1946 {
1949 }
1950
1954
1956
1960
1965
1967
1969 };
1970
1972 {
1975 }
1976
1981
1983
1984 // Slices of non-IRAP pictures.
1987 // Slices of IRAP pictures.
1990
1993
1995 };
1996
1998 {
2001 }
2002
2007 {
2008 .nb_unit_types = 1,
2013 .nb_offsets = 2,
2016 },
2017 },
2021
2024
2027
2030
2033
2035 };
2036
2039
2041
2043
2049
2052 };
2053
2056
2058
2060
2066
2069 };
2070
2073
2075
2077
2082
2085 };
2086
2087 // Macro for the read/write pair.
2088 #define SEI_MESSAGE_RW(codec, name) \
2089 .read = cbs_ ## codec ## _read_ ## name ## _internal, \
2090 .write = cbs_ ## codec ## _write_ ## name ## _internal
2091
2093 {
2095 1, 1,
2098 },
2099 {
2101 1, 1,
2104 },
2105 {
2107 1, 1,
2110 },
2111 {
2113 1, 0,
2116 },
2117 {
2119 1, 0,
2122 },
2123 {
2125 1, 0,
2128 },
2129 {
2131 1, 0,
2134 },
2136 };
2137
2139 {
2141 1, 0,
2144 },
2145 {
2147 1, 0,
2150 },
2151 {
2153 1, 0,
2156 },
2157 {
2159 1, 0,
2162 },
2163 {
2165 1, 0,
2168 },
2169 {
2171 1, 0,
2174 },
2176 };
2177
2179 {
2181 1, 0,
2184 },
2185 {
2187 1, 0,
2190 },
2191 {
2193 1, 0,
2196 },
2197 {
2199 1, 0,
2202 },
2203 {
2205 1, 0,
2208 },
2209 {
2211 1, 0,
2214 },
2215 {
2217 1, 0,
2220 },
2221 {
2223 0, 1,
2226 },
2227 {
2229 1, 0,
2232 },
2233 {
2235 1, 0,
2238 },
2240 };
2241
2243 {
2245 0, 1,
2248 },
2250 };
2251
2253 int payload_type)
2254 {
2257
2261 }
2262
2263 switch (
ctx->codec->codec_id) {
2266 break;
2269 break;
2272 break;
2273 default:
2275 }
2276
2280 }
2281
2283 }