-
Notifications
You must be signed in to change notification settings - Fork 578
Expand file tree
/
Copy pathavc_functions.c
More file actions
1291 lines (1175 loc) · 44.5 KB
/
Copy pathavc_functions.c
File metadata and controls
1291 lines (1175 loc) · 44.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "lib_ccx.h"
#include "ccx_common_option.h"
#include "utility.h"
#include <math.h>
#include "avc_functions.h"
#define dvprint(...) dbg_print(CCX_DMT_VIDES, __VA_ARGS__)
// Functions to parse a AVC/H.264 data stream, see ISO/IEC 14496-10
// local functions
static unsigned char *remove_03emu(unsigned char *from, unsigned char *to);
static void sei_rbsp(struct avc_ctx *ctx, unsigned char *seibuf, unsigned char *seiend);
static unsigned char *sei_message(struct avc_ctx *ctx, unsigned char *seibuf, unsigned char *seiend);
static void user_data_registered_itu_t_t35(struct avc_ctx *ctx, unsigned char *userbuf, unsigned char *userend);
static void seq_parameter_set_rbsp(struct avc_ctx *ctx, unsigned char *seqbuf, unsigned char *seqend);
static void slice_header(struct encoder_ctx *enc_ctx, struct lib_cc_decode *ctx, unsigned char *heabuf, unsigned char *heaend, int nal_unit_type, struct cc_subtitle *sub);
double roundportable(double x) { return floor(x + 0.5); }
int ebsp_to_rbsp(char *rbsp, char *ebsp, int length);
void dinit_avc(struct avc_ctx **ctx)
{
struct avc_ctx *lctx = *ctx;
if (lctx->ccblocks_in_avc_lost > 0)
{
mprint("Total caption blocks received: %d\n", lctx->ccblocks_in_avc_total);
mprint("Total caption blocks lost: %d\n", lctx->ccblocks_in_avc_lost);
}
freep(&lctx->cc_data);
freep(ctx);
}
struct avc_ctx *init_avc(void)
{
struct avc_ctx *ctx = malloc(sizeof(struct avc_ctx));
if (!ctx)
return NULL;
ctx->cc_data = (unsigned char *)malloc(1024);
if (!ctx->cc_data)
{
free(ctx);
return NULL;
}
ctx->cc_count = 0;
// buffer to hold cc data
ctx->cc_databufsize = 1024;
ctx->cc_buffer_saved = CCX_TRUE; // Was the CC buffer saved after it was last updated?
ctx->is_hevc = 0;
ctx->got_seq_para = 0;
ctx->nal_ref_idc = 0;
ctx->seq_parameter_set_id = 0;
ctx->log2_max_frame_num = 0;
ctx->pic_order_cnt_type = 0;
ctx->log2_max_pic_order_cnt_lsb = 0;
ctx->frame_mbs_only_flag = 0;
ctx->ccblocks_in_avc_total = 0;
ctx->ccblocks_in_avc_lost = 0;
ctx->frame_num = -1;
ctx->lastframe_num = -1;
ctx->currref = 0;
ctx->maxidx = -1;
ctx->lastmaxidx = -1;
// Used to find tref zero in PTS mode
ctx->minidx = 10000;
ctx->lastminidx = 10000;
// Used to remember the max temporal reference number (poc mode)
ctx->maxtref = 0;
ctx->last_gop_maxtref = 0;
// Used for PTS ordering of CC blocks
ctx->currefpts = 0;
ctx->last_pic_order_cnt_lsb = -1;
ctx->last_slice_pts = -1;
ctx->num_nal_unit_type_7 = 0;
ctx->num_vcl_hrd = 0;
ctx->num_nal_hrd = 0;
ctx->num_jump_in_frames = 0;
ctx->num_unexpected_sei_length = 0;
return ctx;
}
// HEVC NAL unit types for SEI messages
#define HEVC_NAL_PREFIX_SEI 39
#define HEVC_NAL_SUFFIX_SEI 40
#define HEVC_NAL_VPS 32
#define HEVC_NAL_SPS 33
#define HEVC_NAL_PPS 34
void do_NAL(struct encoder_ctx *enc_ctx, struct lib_cc_decode *dec_ctx, unsigned char *NAL_start, LLONG NAL_length, struct cc_subtitle *sub)
{
unsigned char *NAL_stop;
int nal_unit_type;
int nal_header_size;
unsigned char *payload_start;
// Determine if this is HEVC or H.264 based on NAL header
// H.264 NAL header: 1 byte, type in bits [4:0]
// HEVC NAL header: 2 bytes, type in bits [6:1] of first byte
if (dec_ctx->avc_ctx->is_hevc)
{
// HEVC: NAL type is in bits [6:1] of byte 0
nal_unit_type = (NAL_start[0] >> 1) & 0x3F;
nal_header_size = 2;
}
else
{
// H.264: NAL type is in bits [4:0] of byte 0
nal_unit_type = NAL_start[0] & 0x1F;
nal_header_size = 1;
}
NAL_stop = NAL_length + NAL_start;
NAL_stop = remove_03emu(NAL_start + nal_header_size, NAL_stop);
payload_start = NAL_start + nal_header_size;
dvprint("BEGIN NAL unit type: %d length %d ref_idc: %d - Buffered captions before: %d (HEVC: %d)\n",
nal_unit_type, NAL_stop - NAL_start - nal_header_size, dec_ctx->avc_ctx->nal_ref_idc,
!dec_ctx->avc_ctx->cc_buffer_saved, dec_ctx->avc_ctx->is_hevc);
if (NAL_stop == NULL) // remove_03emu failed.
{
mprint("\rNotice: NAL of type %u had to be skipped because remove_03emu failed.\n", nal_unit_type);
return;
}
if (dec_ctx->avc_ctx->is_hevc)
{
// HEVC NAL unit processing
if (nal_unit_type == HEVC_NAL_VPS || nal_unit_type == HEVC_NAL_SPS || nal_unit_type == HEVC_NAL_PPS)
{
// Found HEVC parameter set - mark as having sequence params
// We don't parse HEVC SPS fully, but we need to enable SEI processing
dec_ctx->avc_ctx->got_seq_para = 1;
}
else if (nal_unit_type == HEVC_NAL_PREFIX_SEI || nal_unit_type == HEVC_NAL_SUFFIX_SEI)
{
// Found HEVC SEI (used for subtitles)
// SEI payload format is similar to H.264
sei_rbsp(dec_ctx->avc_ctx, payload_start, NAL_stop);
}
}
else
{
// H.264 NAL unit processing (original code)
if (nal_unit_type == CCX_NAL_TYPE_ACCESS_UNIT_DELIMITER_9)
{
// Found Access Unit Delimiter
}
else if (nal_unit_type == CCX_NAL_TYPE_SEQUENCE_PARAMETER_SET_7)
{
// Found sequence parameter set
// We need this to parse NAL type 1 (CCX_NAL_TYPE_CODED_SLICE_NON_IDR_PICTURE_1)
dec_ctx->avc_ctx->num_nal_unit_type_7++;
seq_parameter_set_rbsp(dec_ctx->avc_ctx, payload_start, NAL_stop);
dec_ctx->avc_ctx->got_seq_para = 1;
}
else if (dec_ctx->avc_ctx->got_seq_para && (nal_unit_type == CCX_NAL_TYPE_CODED_SLICE_NON_IDR_PICTURE_1 ||
nal_unit_type == CCX_NAL_TYPE_CODED_SLICE_IDR_PICTURE))
{
// Found coded slice of a non-IDR picture
// We only need the slice header data
slice_header(enc_ctx, dec_ctx, payload_start, NAL_stop, nal_unit_type, sub);
}
else if (dec_ctx->avc_ctx->got_seq_para && nal_unit_type == CCX_NAL_TYPE_SEI)
{
// Found SEI (used for subtitles)
sei_rbsp(dec_ctx->avc_ctx, payload_start, NAL_stop);
}
else if (dec_ctx->avc_ctx->got_seq_para && nal_unit_type == CCX_NAL_TYPE_PICTURE_PARAMETER_SET)
{
// Found Picture parameter set
}
}
if (temp_debug)
{
int len = NAL_stop - payload_start;
dbg_print(CCX_DMT_VIDES, "\n After decoding, the actual thing was (length =%d)\n", len);
dump(CCX_DMT_VIDES, payload_start, len > 160 ? 160 : len, 0, 0);
}
dvprint("END NAL unit type: %d length %d ref_idc: %d - Buffered captions after: %d\n",
nal_unit_type, NAL_stop - NAL_start - nal_header_size, dec_ctx->avc_ctx->nal_ref_idc, !dec_ctx->avc_ctx->cc_buffer_saved);
}
// Process inbuf bytes in buffer holding and AVC (H.264) video stream.
// The number of processed bytes is returned.
#ifndef DISABLE_RUST
size_t ccxr_process_avc(struct encoder_ctx *enc_ctx, struct lib_cc_decode *dec_ctx, unsigned char *avcbuf, size_t avcbuflen, struct cc_subtitle *sub);
#endif
size_t process_avc(struct encoder_ctx *enc_ctx, struct lib_cc_decode *dec_ctx, unsigned char *avcbuf, size_t avcbuflen, struct cc_subtitle *sub)
{
#ifndef DISABLE_RUST
return ccxr_process_avc(enc_ctx, dec_ctx, avcbuf, avcbuflen, sub);
#else
unsigned char *buffer_position = avcbuf;
unsigned char *NAL_start;
unsigned char *NAL_stop;
// At least 5 bytes are needed for a NAL unit
if (avcbuflen <= 5)
{
fatal(CCX_COMMON_EXIT_BUG_BUG,
"NAL unit needs at last 5 bytes in the buffer to process AVC video stream...");
}
// Warning there should be only leading zeros, nothing else
if (!(buffer_position[0] == 0x00 && buffer_position[1] == 0x00))
{
fatal(CCX_COMMON_EXIT_BUG_BUG,
"Broken AVC stream - Leading bytes are non-zero...");
}
buffer_position = buffer_position + 2;
int firstloop = 1; // Check for valid start code at buffer start
// Loop over NAL units
while (buffer_position < avcbuf + avcbuflen - 2) // buffer_position points to 0x01 plus at least two bytes
{
int zeropad = 0; // Count leading zeros
// Find next NAL_start
while (buffer_position < avcbuf + avcbuflen)
{
if (*buffer_position == 0x01)
{
// OK, found a start code
break;
}
else if (firstloop && *buffer_position != 0x00)
{
// Not 0x00 or 0x01
fatal(CCX_COMMON_EXIT_BUG_BUG,
"Broken AVC stream - Leading bytes are non-zero...");
}
buffer_position++;
zeropad++;
}
firstloop = 0;
if (buffer_position >= avcbuf + avcbuflen)
{
// No new start sequence
break;
}
NAL_start = buffer_position + 1;
// Find next start code or buffer end
LLONG restlen;
do
{
// Search for next 000000 or 000001
buffer_position++;
restlen = avcbuf - buffer_position + avcbuflen - 2; // leave room for two more bytes
// Find the next zero
if (restlen > 0)
{
buffer_position = (unsigned char *)memchr(buffer_position, 0x00, (size_t)restlen);
if (!buffer_position)
{
// No 0x00 till the end of the buffer
NAL_stop = avcbuf + avcbuflen;
buffer_position = NAL_stop;
break;
}
if (buffer_position[1] == 0x00 && (buffer_position[2] | 0x01) == 0x01)
{
// Found new start code
NAL_stop = buffer_position;
buffer_position = buffer_position + 2; // Move after the two leading 0x00
break;
}
}
else
{
NAL_stop = avcbuf + avcbuflen;
buffer_position = NAL_stop;
break;
}
} while (restlen); // Should never be true - loop is exited via break
if (*NAL_start & 0x80)
{
dump(CCX_DMT_GENERIC_NOTICES, NAL_start - 4, 10, 0, 0);
fatal(CCX_COMMON_EXIT_BUG_BUG,
"Broken AVC stream - forbidden_zero_bit not zero ...");
}
dec_ctx->avc_ctx->nal_ref_idc = *NAL_start >> 5;
dvprint("process_avc: zeropad %d\n", zeropad);
do_NAL(enc_ctx, dec_ctx, NAL_start, NAL_stop - NAL_start, sub);
}
return avcbuflen;
#endif
}
#define ZEROBYTES_SHORTSTARTCODE 2
// Copied for reference decoder, see if it behaves different that Volker's code
int EBSPtoRBSP(unsigned char *streamBuffer, int end_bytepos, int begin_bytepos)
{
int i, j, count;
count = 0;
if (end_bytepos < begin_bytepos)
return end_bytepos;
j = begin_bytepos;
for (i = begin_bytepos; i < end_bytepos; ++i)
{ // starting from begin_bytepos to avoid header information
// in NAL unit, 0x000000, 0x000001 or 0x000002 shall not occur at any byte-aligned position
if (count == ZEROBYTES_SHORTSTARTCODE && streamBuffer[i] < 0x03)
return -1;
if (count == ZEROBYTES_SHORTSTARTCODE && streamBuffer[i] == 0x03)
{
// check the 4th byte after 0x000003, except when cabac_zero_word is used, in which case the last three bytes of this NAL unit must be 0x000003
if ((i < end_bytepos - 1) && (streamBuffer[i + 1] > 0x03))
return -1;
// if cabac_zero_word is used, the final byte of this NAL unit(0x03) is discarded, and the last two bytes of RBSP must be 0x0000
if (i == end_bytepos - 1)
return j;
++i;
count = 0;
}
streamBuffer[j] = streamBuffer[i];
if (streamBuffer[i] == 0x00)
++count;
else
count = 0;
++j;
}
return j;
}
typedef unsigned int u32;
// Remove 0x000003 emulation sequences. We do this on-site (same buffer) because the new array
// will never be longer than the source.
u32 avc_remove_emulation_bytes(const unsigned char *buffer_src, unsigned char *buffer_dst, u32 nal_size);
unsigned char *remove_03emu(unsigned char *from, unsigned char *to)
{
int num = to - from;
int newsize = EBSPtoRBSP(from, num, 0); // TODO: Do something if newsize == -1 (broken NAL)
if (newsize == -1)
return NULL;
return from + newsize;
}
// Process SEI payload in AVC data. This function combines sei_rbsp()
// and rbsp_trailing_bits().
void sei_rbsp(struct avc_ctx *ctx, unsigned char *seibuf, unsigned char *seiend)
{
unsigned char *tbuf = seibuf;
while (tbuf < seiend - 1) // Use -1 because of trailing marker
{
tbuf = sei_message(ctx, tbuf, seiend - 1);
}
if (tbuf == seiend - 1)
{
if (*tbuf != 0x80)
mprint("Strange rbsp_trailing_bits value: %02X\n", *tbuf);
else
dvprint("\n");
}
else
{
// Unexpected SEI length - common with malformed streams, don't spam output
dbg_print(CCX_DMT_VERBOSE, "WARNING: Unexpected SEI unit length (parsed to %p, expected %p)...trying to continue.\n",
(void *)tbuf, (void *)(seiend - 1));
dump(CCX_DMT_VERBOSE, (unsigned char *)seibuf, seiend - seibuf, 0, 0);
ctx->num_unexpected_sei_length++;
}
}
// This combines sei_message() and sei_payload().
unsigned char *sei_message(struct avc_ctx *ctx, unsigned char *seibuf, unsigned char *seiend)
{
int payload_type = 0;
while (seibuf < seiend && *seibuf == 0xff)
{
payload_type += 255;
seibuf++;
}
if (seibuf >= seiend)
return NULL;
payload_type += *seibuf;
seibuf++;
int payload_size = 0;
while (seibuf < seiend && *seibuf == 0xff)
{
payload_size += 255;
seibuf++;
}
if (seibuf >= seiend)
return NULL;
payload_size += *seibuf;
seibuf++;
int broken = 0;
unsigned char *payload_start = seibuf;
seibuf += payload_size;
dvprint("Payload type: %d size: %d - ", payload_type, payload_size);
if (seibuf > seiend)
{
// TODO: What do we do here?
broken = 1;
if (payload_type == 4)
{
dbg_print(CCX_DMT_VERBOSE, "Warning: Subtitles payload seems incorrect (too long), continuing but it doesn't look good..");
}
else
{
dbg_print(CCX_DMT_VERBOSE, "Warning: Non-subtitles payload seems incorrect (too long), continuing but it doesn't look good..");
}
}
dbg_print(CCX_DMT_VERBOSE, "\n");
// Ignore all except user_data_registered_itu_t_t35() payload
if (!broken && payload_type == 4)
user_data_registered_itu_t_t35(ctx, payload_start, payload_start + payload_size);
return seibuf;
}
void copy_ccdata_to_buffer(struct avc_ctx *ctx, char *source, int new_cc_count)
{
ctx->ccblocks_in_avc_total++;
if (ctx->cc_buffer_saved == CCX_FALSE)
{
mprint("Warning: Probably loss of CC data, unsaved buffer being rewritten, trailing end might get lost\n");
ctx->ccblocks_in_avc_lost++;
}
memcpy(ctx->cc_data + ctx->cc_count * 3, source, new_cc_count * 3 + 1);
ctx->cc_count += new_cc_count;
ctx->cc_buffer_saved = CCX_FALSE;
}
void user_data_registered_itu_t_t35(struct avc_ctx *ctx, unsigned char *userbuf, unsigned char *userend)
{
unsigned char *tbuf = userbuf;
unsigned char *cc_tmp_data;
unsigned char process_cc_data_flag;
int user_data_type_code;
int user_data_len;
int local_cc_count = 0;
// int cc_count;
int itu_t_t35_country_code = *((uint8_t *)tbuf);
tbuf++;
int itu_t_35_provider_code = *tbuf * 256 + *(tbuf + 1);
tbuf += 2;
// ANSI/SCTE 128 2008:
// itu_t_t35_country_code == 0xB5
// itu_t_35_provider_code == 0x0031
// see spec for details - no example -> no support
// Example files (sample.ts, ...):
// itu_t_t35_country_code == 0xB5
// itu_t_35_provider_code == 0x002F
// user_data_type_code == 0x03 (cc_data)
// user_data_len == next byte (length after this byte up to (incl) marker.)
// cc_data struct (CEA-708)
// marker == 0xFF
if (itu_t_t35_country_code != 0xB5)
{
mprint("Not a supported user data SEI\n");
mprint(" itu_t_35_country_code: %02x\n", itu_t_t35_country_code);
return;
}
switch (itu_t_35_provider_code)
{
case 0x0031: // ANSI/SCTE 128
dbg_print(CCX_DMT_VERBOSE, "Caption block in ANSI/SCTE 128...");
if (*tbuf == 0x47 && *(tbuf + 1) == 0x41 && *(tbuf + 2) == 0x39 && *(tbuf + 3) == 0x34) // ATSC1_data() - GA94
{
dbg_print(CCX_DMT_VERBOSE, "ATSC1_data()...\n");
tbuf += 4;
unsigned char user_data_type_code = *tbuf;
tbuf++;
switch (user_data_type_code)
{
case 0x03:
dbg_print(CCX_DMT_VERBOSE, "cc_data (finally)!\n");
/*
cc_count = 2; // Forced test
process_cc_data_flag = (*tbuf & 2) >> 1;
mandatory_1 = (*tbuf & 1);
mandatory_0 = (*tbuf & 4) >>2;
if (!mandatory_1 || mandatory_0)
{
printf ("Essential tests not passed.\n");
break;
}
*/
local_cc_count = *tbuf & 0x1F;
process_cc_data_flag = (*tbuf & 0x40) >> 6;
/* if (!process_cc_data_flag)
{
mprint ("process_cc_data_flag == 0, skipping this caption block.\n");
break;
} */
/*
The following tests are not passed in Comcast's sample videos. *tbuf here is always 0x41.
if (! (*tbuf & 0x80)) // First bit must be 1
{
printf ("Fixed bit should be 1, but it's 0 - skipping this caption block.\n");
break;
}
if (*tbuf & 0x20) // Third bit must be 0
{
printf ("Fixed bit should be 0, but it's 1 - skipping this caption block.\n");
break;
} */
tbuf++;
/*
Another test that the samples ignore. They contain 00!
if (*tbuf!=0xFF)
{
printf ("Fixed value should be 0xFF, but it's %02X - skipping this caption block.\n", *tbuf);
} */
// OK, all checks passed!
tbuf++;
cc_tmp_data = tbuf;
/* TODO: I don't think we have user_data_len here
if (cc_count*3+3 != user_data_len)
fatal(CCX_COMMON_EXIT_BUG_BUG,
"Syntax problem: user_data_len != cc_count*3+3."); */
// Enough room for CC captions?
if (cc_tmp_data + local_cc_count * 3 >= userend)
fatal(CCX_COMMON_EXIT_BUG_BUG,
"Syntax problem: Too many caption blocks.");
if (cc_tmp_data[local_cc_count * 3] != 0xFF)
{
// See GitHub Issue #1001 for the related change
mprint("\rWarning! Syntax problem: Final 0xFF marker missing. Continuing...\n");
break; // Skip Block
}
// Save the data and process once we know the sequence number
if (((ctx->cc_count + local_cc_count) * 3) + 1 > ctx->cc_databufsize)
{
unsigned char *tmp = (unsigned char *)realloc(ctx->cc_data, (size_t)((ctx->cc_count + local_cc_count) * 6) + 1);
if (!tmp)
{
free(ctx->cc_data);
fatal(EXIT_NOT_ENOUGH_MEMORY, "In user_data_registered_itu_t_t35: Out of memory to allocate buffer for CC data.");
}
ctx->cc_data = tmp;
ctx->cc_databufsize = (long)((ctx->cc_count + local_cc_count) * 6) + 1;
}
// Copy new cc data into cc_data
copy_ccdata_to_buffer(ctx, (char *)cc_tmp_data, local_cc_count);
break;
case 0x06:
dbg_print(CCX_DMT_VERBOSE, "bar_data (unsupported for now)\n");
break;
default:
dbg_print(CCX_DMT_VERBOSE, "SCTE/ATSC reserved.\n");
}
}
else if (*tbuf == 0x44 && *(tbuf + 1) == 0x54 && *(tbuf + 2) == 0x47 && *(tbuf + 3) == 0x31) // afd_data() - DTG1
{
;
// Active Format Description Data. Actually unrelated to captions. Left
// here in case we want to do some reporting eventually. From specs:
// "Active Format Description (AFD) should be included in video user
// data whenever the rectangular picture area containing useful
// information does not extend to the full height or width of the coded
// frame. AFD data may also be included in user data when the
// rectangular picture area containing
// useful information extends to the fullheight and width of the
// coded frame."
}
else
{
dbg_print(CCX_DMT_VERBOSE, "SCTE/ATSC reserved.\n");
}
break;
case 0x002F:
dbg_print(CCX_DMT_VERBOSE, "ATSC1_data() - provider_code = 0x002F\n");
user_data_type_code = *((uint8_t *)tbuf);
if (user_data_type_code != 0x03)
{
dbg_print(CCX_DMT_VERBOSE, "Not supported user_data_type_code: %02x\n",
user_data_type_code);
return;
}
tbuf++;
user_data_len = *((uint8_t *)tbuf);
tbuf++;
local_cc_count = *tbuf & 0x1F;
process_cc_data_flag = (*tbuf & 0x40) >> 6;
if (!process_cc_data_flag)
{
mprint("process_cc_data_flag == 0, skipping this caption block.\n");
break;
}
cc_tmp_data = tbuf + 2;
if (local_cc_count * 3 + 3 != user_data_len)
fatal(CCX_COMMON_EXIT_BUG_BUG,
"Syntax problem: user_data_len != cc_count*3+3.");
// Enough room for CC captions?
if (cc_tmp_data + local_cc_count * 3 >= userend)
fatal(CCX_COMMON_EXIT_BUG_BUG,
"Syntax problem: Too many caption blocks.");
if (cc_tmp_data[local_cc_count * 3] != 0xFF)
fatal(CCX_COMMON_EXIT_BUG_BUG,
"Syntax problem: Final 0xFF marker missing.");
// Save the data and process once we know the sequence number
if ((((local_cc_count + ctx->cc_count) * 3) + 1) > ctx->cc_databufsize)
{
unsigned char *tmp = (unsigned char *)realloc(ctx->cc_data, (size_t)(((local_cc_count + ctx->cc_count) * 6) + 1));
if (!tmp)
{
free(ctx->cc_data);
fatal(EXIT_NOT_ENOUGH_MEMORY, "In user_data_registered_itu_t_t35: Not enough memory trying to allocate buffer for CC data.");
}
ctx->cc_data = tmp;
ctx->cc_databufsize = (long)(((local_cc_count + ctx->cc_count) * 6) + 1);
}
// Copy new cc data into cc_data - replace command below.
copy_ccdata_to_buffer(ctx, (char *)cc_tmp_data, local_cc_count);
// dump(tbuf,user_data_len-1,0);
break;
default:
mprint("Not a supported user data SEI\n");
mprint(" itu_t_35_provider_code: %04x\n", itu_t_35_provider_code);
break;
}
}
// Process sequence parameters in AVC data.
void seq_parameter_set_rbsp(struct avc_ctx *ctx, unsigned char *seqbuf, unsigned char *seqend)
{
LLONG tmp, tmp1;
struct bitstream q1;
init_bitstream(&q1, seqbuf, seqend);
dvprint("SEQUENCE PARAMETER SET (bitlen: %lld)\n", q1.bitsleft);
tmp = read_int_unsigned(&q1, 8);
LLONG profile_idc = tmp;
dvprint("profile_idc= % 4lld (%#llX)\n", tmp, tmp);
tmp = read_int_unsigned(&q1, 1);
dvprint("constraint_set0_flag= % 4lld (%#llX)\n", tmp, tmp);
tmp = read_int_unsigned(&q1, 1);
dvprint("constraint_set1_flag= % 4lld (%#llX)\n", tmp, tmp);
tmp = read_int_unsigned(&q1, 1);
dvprint("constraint_set2_flag= % 4lld (%#llX)\n", tmp, tmp);
tmp = read_int_unsigned(&q1, 1);
dvprint("constraint_set3_flag= % 4lld (%#llX)\n", tmp, tmp);
tmp = read_int_unsigned(&q1, 1);
dvprint("constraint_set4_flag= % 4lld (%#llX)\n", tmp, tmp);
tmp = read_int_unsigned(&q1, 1);
dvprint("constraint_set5_flag= % 4lld (%#llX)\n", tmp, tmp);
tmp = read_int_unsigned(&q1, 2);
dvprint("reserved= % 4lld (%#llX)\n", tmp, tmp);
tmp = read_int_unsigned(&q1, 8);
dvprint("level_idc= % 4lld (%#llX)\n", tmp, tmp);
ctx->seq_parameter_set_id = read_exp_golomb_unsigned(&q1);
dvprint("seq_parameter_set_id= % 4lld (%#llX)\n", ctx->seq_parameter_set_id, ctx->seq_parameter_set_id);
if (profile_idc == 100 || profile_idc == 110 || profile_idc == 122 || profile_idc == 244 || profile_idc == 44 || profile_idc == 83 || profile_idc == 86 || profile_idc == 118 || profile_idc == 128)
{
LLONG chroma_format_idc = read_exp_golomb_unsigned(&q1);
dvprint("chroma_format_idc= % 4lld (%#llX)\n", chroma_format_idc, chroma_format_idc);
if (chroma_format_idc == 3)
{
tmp = read_int_unsigned(&q1, 1);
dvprint("separate_colour_plane_flag= % 4lld (%#llX)\n", tmp, tmp);
}
tmp = read_exp_golomb_unsigned(&q1);
dvprint("bit_depth_luma_minus8= % 4lld (%#llX)\n", tmp, tmp);
tmp = read_exp_golomb_unsigned(&q1);
dvprint("bit_depth_chroma_minus8= % 4lld (%#llX)\n", tmp, tmp);
tmp = read_int_unsigned(&q1, 1);
dvprint("qpprime_y_zero_transform_bypass_flag= % 4lld (%#llX)\n", tmp, tmp);
tmp = read_int_unsigned(&q1, 1);
dvprint("seq_scaling_matrix_present_flag= % 4lld (%#llX)\n", tmp, tmp);
if (tmp == 1)
{
// WVI: untested, just copied from specs.
for (int i = 0; i < ((chroma_format_idc != 3) ? 8 : 12); i++)
{
tmp = read_int_unsigned(&q1, 1);
dvprint("seq_scaling_list_present_flag[%d]= % 4lld (%#llX)\n", i, tmp, tmp);
if (tmp)
{
// We use a "dummy"/slimmed-down replacement here. Actual/full code can be found in the spec (ISO/IEC 14496-10:2012(E)) chapter 7.3.2.1.1.1 - Scaling list syntax
if (i < 6)
{
// Scaling list size 16
// TODO: replace with full scaling list implementation?
int nextScale = 8;
int lastScale = 8;
for (int j = 0; j < 16; j++)
{
if (nextScale != 0)
{
int64_t delta_scale = read_exp_golomb(&q1);
nextScale = (lastScale + delta_scale + 256) % 256;
}
lastScale = (nextScale == 0) ? lastScale : nextScale;
}
// END of TODO
}
else
{
// Scaling list size 64
// TODO: replace with full scaling list implementation?
int nextScale = 8;
int lastScale = 8;
for (int j = 0; j < 64; j++)
{
if (nextScale != 0)
{
int64_t delta_scale = read_exp_golomb(&q1);
nextScale = (lastScale + delta_scale + 256) % 256;
}
lastScale = (nextScale == 0) ? lastScale : nextScale;
}
// END of TODO
}
}
}
}
}
ctx->log2_max_frame_num = (int)read_exp_golomb_unsigned(&q1);
dvprint("log2_max_frame_num4_minus4= % 4d (%#X)\n", ctx->log2_max_frame_num, ctx->log2_max_frame_num);
ctx->log2_max_frame_num += 4; // 4 is added due to the formula.
ctx->pic_order_cnt_type = (int)read_exp_golomb_unsigned(&q1);
dvprint("pic_order_cnt_type= % 4d (%#X)\n", ctx->pic_order_cnt_type, ctx->pic_order_cnt_type);
if (ctx->pic_order_cnt_type == 0)
{
ctx->log2_max_pic_order_cnt_lsb = (int)read_exp_golomb_unsigned(&q1);
dvprint("log2_max_pic_order_cnt_lsb_minus4= % 4d (%#X)\n", ctx->log2_max_pic_order_cnt_lsb, ctx->log2_max_pic_order_cnt_lsb);
ctx->log2_max_pic_order_cnt_lsb += 4; // 4 is added due to formula.
}
else if (ctx->pic_order_cnt_type == 1)
{
// CFS: Untested, just copied from specs.
tmp = read_int_unsigned(&q1, 1);
dvprint("delta_pic_order_always_zero_flag= % 4lld (%#llX)\n", tmp, tmp);
tmp = read_exp_golomb(&q1);
dvprint("offset_for_non_ref_pic= % 4lld (%#llX)\n", tmp, tmp);
tmp = read_exp_golomb(&q1);
dvprint("offset_for_top_to_bottom_field % 4lld (%#llX)\n", tmp, tmp);
LLONG num_ref_frame_in_pic_order_cnt_cycle = read_exp_golomb_unsigned(&q1);
dvprint("num_ref_frame_in_pic_order_cnt_cycle % 4lld (%#llX)\n", num_ref_frame_in_pic_order_cnt_cycle, num_ref_frame_in_pic_order_cnt_cycle);
for (int i = 0; i < num_ref_frame_in_pic_order_cnt_cycle; i++)
{
tmp = read_exp_golomb(&q1);
dvprint("offset_for_ref_frame [%d / %d] = % 4lld (%#llX)\n", i, num_ref_frame_in_pic_order_cnt_cycle, tmp, tmp);
}
}
else
{
// Nothing needs to be parsed when pic_order_cnt_type == 2
}
tmp = read_exp_golomb_unsigned(&q1);
dvprint("max_num_ref_frames= % 4lld (%#llX)\n", tmp, tmp);
tmp = read_int_unsigned(&q1, 1);
dvprint("gaps_in_frame_num_value_allowed_flag= % 4lld (%#llX)\n", tmp, tmp);
tmp = read_exp_golomb_unsigned(&q1);
dvprint("pic_width_in_mbs_minus1= % 4lld (%#llX)\n", tmp, tmp);
tmp = read_exp_golomb_unsigned(&q1);
dvprint("pic_height_in_map_units_minus1= % 4lld (%#llX)\n", tmp, tmp);
ctx->frame_mbs_only_flag = (int)read_int_unsigned(&q1, 1);
dvprint("frame_mbs_only_flag= % 4d (%#X)\n", ctx->frame_mbs_only_flag, ctx->frame_mbs_only_flag);
if (!ctx->frame_mbs_only_flag)
{
tmp = read_int_unsigned(&q1, 1);
dvprint("mb_adaptive_fr_fi_flag= % 4lld (%#llX)\n", tmp, tmp);
}
tmp = read_int_unsigned(&q1, 1);
dvprint("direct_8x8_inference_f= % 4lld (%#llX)\n", tmp, tmp);
tmp = read_int_unsigned(&q1, 1);
dvprint("frame_cropping_flag= % 4lld (%#llX)\n", tmp, tmp);
if (tmp)
{
tmp = read_exp_golomb_unsigned(&q1);
dvprint("frame_crop_left_offset= % 4lld (%#llX)\n", tmp, tmp);
tmp = read_exp_golomb_unsigned(&q1);
dvprint("frame_crop_right_offset= % 4lld (%#llX)\n", tmp, tmp);
tmp = read_exp_golomb_unsigned(&q1);
dvprint("frame_crop_top_offset= % 4lld (%#llX)\n", tmp, tmp);
tmp = read_exp_golomb_unsigned(&q1);
dvprint("frame_crop_bottom_offset= % 4lld (%#llX)\n", tmp, tmp);
}
tmp = read_int_unsigned(&q1, 1);
dvprint("vui_parameters_present= % 4lld (%#llX)\n", tmp, tmp);
if (tmp)
{
dvprint("\nVUI parameters\n");
tmp = read_int_unsigned(&q1, 1);
dvprint("aspect_ratio_info_pres= % 4lld (%#llX)\n", tmp, tmp);
if (tmp)
{
tmp = read_int_unsigned(&q1, 8);
dvprint("aspect_ratio_idc= % 4lld (%#llX)\n", tmp, tmp);
if (tmp == 255)
{
tmp = read_int_unsigned(&q1, 16);
dvprint("sar_width= % 4lld (%#llX)\n", tmp, tmp);
tmp = read_int_unsigned(&q1, 16);
dvprint("sar_height= % 4lld (%#llX)\n", tmp, tmp);
}
}
tmp = read_int_unsigned(&q1, 1);
dvprint("overscan_info_pres_flag= % 4lld (%#llX)\n", tmp, tmp);
if (tmp)
{
tmp = read_int_unsigned(&q1, 1);
dvprint("overscan_appropriate_flag= % 4lld (%#llX)\n", tmp, tmp);
}
tmp = read_int_unsigned(&q1, 1);
dvprint("video_signal_type_present_flag= % 4lld (%#llX)\n", tmp, tmp);
if (tmp)
{
tmp = read_int_unsigned(&q1, 3);
dvprint("video_format= % 4lld (%#llX)\n", tmp, tmp);
tmp = read_int_unsigned(&q1, 1);
dvprint("video_full_range_flag= % 4lld (%#llX)\n", tmp, tmp);
tmp = read_int_unsigned(&q1, 1);
dvprint("colour_description_present_flag= % 4lld (%#llX)\n", tmp, tmp);
if (tmp)
{
tmp = read_int_unsigned(&q1, 8);
dvprint("colour_primaries= % 4lld (%#llX)\n", tmp, tmp);
tmp = read_int_unsigned(&q1, 8);
dvprint("transfer_characteristics= % 4lld (%#llX)\n", tmp, tmp);
tmp = read_int_unsigned(&q1, 8);
dvprint("matrix_coefficients= % 4lld (%#llX)\n", tmp, tmp);
}
}
tmp = read_int_unsigned(&q1, 1);
dvprint("chroma_loc_info_present_flag= % 4lld (%#llX)\n", tmp, tmp);
if (tmp)
{
tmp = read_exp_golomb_unsigned(&q1);
dvprint("chroma_sample_loc_type_top_field= % 4lld (%#llX)\n", tmp, tmp);
tmp = read_exp_golomb_unsigned(&q1);
dvprint("chroma_sample_loc_type_bottom_field= % 4lld (%#llX)\n", tmp, tmp);
}
tmp = read_int_unsigned(&q1, 1);
dvprint("timing_info_present_flag= % 4lld (%#llX)\n", tmp, tmp);
if (tmp)
{
tmp = read_int_unsigned(&q1, 32);
LLONG num_units_in_tick = tmp;
dvprint("num_units_in_tick= % 4lld (%#llX)\n", tmp, tmp);
tmp = read_int_unsigned(&q1, 32);
LLONG time_scale = tmp;
dvprint("time_scale= % 4lld (%#llX)\n", tmp, tmp);
tmp = read_int_unsigned(&q1, 1);
int fixed_frame_rate_flag = (int)tmp;
dvprint("fixed_frame_rate_flag= % 4lld (%#llX)\n", tmp, tmp);
// Change: use num_units_in_tick and time_scale to calculate FPS. (ISO/IEC 14496-10:2012(E), page 397 & further)
if (fixed_frame_rate_flag)
{
double clock_tick = (double)num_units_in_tick / time_scale;
dvprint("clock_tick= %f\n", clock_tick);
if (current_fps != (double)time_scale / (2 * num_units_in_tick))
{
current_fps = (double)time_scale / (2 * num_units_in_tick); // Based on formula D-2, p. 359 of the ISO/IEC 14496-10:2012(E) spec.
mprint("Changed fps using NAL to: %f\n", current_fps);
}
}
}
tmp = read_int_unsigned(&q1, 1);
dvprint("nal_hrd_parameters_present_flag= % 4lld (%#llX)\n", tmp, tmp);
if (tmp)
{
dvprint("nal_hrd. Not implemented for now. Hopefully not needed. Skipping rest of NAL\n");
// printf("Boom nal_hrd\n");
// exit(1);
ctx->num_nal_hrd++;
return;
}
tmp1 = read_int_unsigned(&q1, 1);
dvprint("vcl_hrd_parameters_present_flag= %llX\n", tmp1);
if (tmp)
{
// VCL HRD parameters are for video buffering compliance, not needed for caption extraction.
// Just skip and continue - this doesn't affect our ability to extract captions.
mprint("Skipping VCL HRD parameters (not needed for caption extraction)\n");
ctx->num_vcl_hrd++;
}
if (tmp || tmp1)
{
tmp = read_int_unsigned(&q1, 1);
dvprint("low_delay_hrd_flag= % 4lld (%#llX)\n", tmp, tmp);
return;
}
tmp = read_int_unsigned(&q1, 1);
dvprint("pic_struct_present_flag= % 4lld (%#llX)\n", tmp, tmp);
tmp = read_int_unsigned(&q1, 1);
dvprint("bitstream_restriction_flag= % 4lld (%#llX)\n", tmp, tmp);
// ..
// The hope was to find the GOP length in max_dec_frame_buffering, but
// it was not set in the testfile. Ignore the rest here, it's
// currently not needed.
}
// exit(1);
}
/**
Process slice header in AVC data.
Slice Header is parsed to get sequence of frames
*/
void slice_header(struct encoder_ctx *enc_ctx, struct lib_cc_decode *dec_ctx, unsigned char *heabuf, unsigned char *heaend, int nal_unit_type, struct cc_subtitle *sub)
{
LLONG tmp;
struct bitstream q1;
int max_frame_num;
LLONG slice_type, bottom_field_flag = 0, pic_order_cnt_lsb = -1;
int current_index;
int ird_pic_flag;
LLONG field_pic_flag = 0; // Moved here because it's needed for ctx->avc_ctx->pic_order_cnt_type==2
if (init_bitstream(&q1, heabuf, heaend))
{
mprint("Skipping slice header due to failure in init_bitstream.\n");
return;
}
ird_pic_flag = ((nal_unit_type == 5) ? 1 : 0);
dvprint("\nSLICE HEADER\n");
tmp = read_exp_golomb_unsigned(&q1);
dvprint("first_mb_in_slice= % 4lld (%#llX)\n", tmp, tmp);
slice_type = read_exp_golomb_unsigned(&q1);
dvprint("slice_type= % 4llX\n", slice_type);
// Validate slice_type to prevent buffer overflow in slice_types[] array
// Valid H.264 slice_type values are 0-9 (H.264 spec Table 7-6)
if (slice_type >= 10)
{
mprint("Invalid slice_type %lld in slice header, skipping.\n", slice_type);
return;
}
tmp = read_exp_golomb_unsigned(&q1);
dvprint("pic_parameter_set_id= % 4lld (%#llX)\n", tmp, tmp);
dec_ctx->avc_ctx->lastframe_num = dec_ctx->avc_ctx->frame_num;
max_frame_num = (int)((1 << dec_ctx->avc_ctx->log2_max_frame_num) - 1);
// Needs log2_max_frame_num_minus4 + 4 bits
dec_ctx->avc_ctx->frame_num = read_int_unsigned(&q1, dec_ctx->avc_ctx->log2_max_frame_num);
dvprint("frame_num= % 4llX\n", dec_ctx->avc_ctx->frame_num);
if (!dec_ctx->avc_ctx->frame_mbs_only_flag)
{
field_pic_flag = read_int_unsigned(&q1, 1);
dvprint("field_pic_flag= % 4llX\n", field_pic_flag);
if (field_pic_flag)
{
// bottom_field_flag
bottom_field_flag = read_int_unsigned(&q1, 1);
dvprint("bottom_field_flag= % 4llX\n", bottom_field_flag);
// When bottom_field_flag is set the video is interlaced,
// override current_fps.
current_fps = framerates_values[dec_ctx->current_frame_rate];
}
}
dvprint("ird_pic_flag= % 4d\n", ird_pic_flag);
if (nal_unit_type == 5)
{
// idr_pic_id: Read to advance bitstream position; value not needed for caption extraction
tmp = read_exp_golomb_unsigned(&q1);