File: avc_functions.c

package info (click to toggle)
ccextractor 0.87%2Bds1-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 10,064 kB
  • sloc: ansic: 172,772; makefile: 777; sh: 622; python: 319
file content (1183 lines) | stat: -rw-r--r-- 40,865 bytes parent folder | download
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
#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 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->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;
}

void do_NAL (struct lib_cc_decode *ctx, unsigned char *NAL_start, LLONG NAL_length, struct cc_subtitle *sub)
{
	unsigned char *NAL_stop;
	enum ccx_avc_nal_types nal_unit_type = *NAL_start & 0x1F;

	NAL_stop = NAL_length+NAL_start;
	NAL_stop = remove_03emu(NAL_start+1, NAL_stop); // Add +1 to NAL_stop for TS, without it for MP4. Still don't know why

	dvprint("BEGIN NAL unit type: %d length %d ref_idc: %d - Buffered captions before: %d\n",
				nal_unit_type,  NAL_stop-NAL_start-1, ctx->avc_ctx->nal_ref_idc, !ctx->avc_ctx->cc_buffer_saved);

	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 ( 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)
		ctx->avc_ctx->num_nal_unit_type_7++;
		seq_parameter_set_rbsp(ctx->avc_ctx, NAL_start+1, NAL_stop);
		ctx->avc_ctx->got_seq_para = 1;
	}
	else if ( 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)) // Only if nal_unit_type=1
	{
		// Found coded slice of a non-IDR picture
		// We only need the slice header data, no need to implement
		// slice_layer_without_partitioning_rbsp( );
		slice_header(ctx, NAL_start+1, NAL_stop, nal_unit_type, sub);
	}
	else if ( ctx->avc_ctx->got_seq_para && nal_unit_type == CCX_NAL_TYPE_SEI )
	{
		// Found SEI (used for subtitles)
		//set_fts(ctx->timing); // FIXME - check this!!!
		sei_rbsp(ctx->avc_ctx, NAL_start+1, NAL_stop);
	}
	else if ( 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 - (NAL_start + 1);
		dbg_print(CCX_DMT_VIDES,"\n After decoding, the actual thing was (length =%d)\n", len);
		dump(CCX_DMT_VIDES, NAL_start + 1, 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-1, ctx->avc_ctx->nal_ref_idc, !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.
size_t process_avc ( struct lib_cc_decode *ctx, unsigned char *avcbuf, size_t avcbuflen ,struct cc_subtitle *sub)
{
	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 ...");
		}

		ctx->avc_ctx->nal_ref_idc = *NAL_start >> 5;
                dvprint("process_avc: zeropad %d\n", zeropad);
		do_NAL (ctx, NAL_start, NAL_stop-NAL_start, sub);
	}

	return avcbuflen;
}

#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
	{
		// TODO: This really really looks bad
		mprint ("WARNING: Unexpected SEI unit length...trying to continue.");
		temp_debug = 1;
		mprint ("\n Failed block (at sei_rbsp) was:\n");
		dump (CCX_DMT_GENERIC_NOTICES,(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==0xff)
	{
		payload_type+=255;
		seibuf++;
	}
	payload_type += *seibuf;
	seibuf++;


	int payload_size = 0;
	while (*seibuf==0xff)
	{
		payload_size+=255;
		seibuf++;
	}
	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)
							fatal(CCX_COMMON_EXIT_BUG_BUG,
									"Syntax problem: Final 0xFF marker missing.");

						// Save the data and process once we know the sequence number
						if ( ( (ctx->cc_count + local_cc_count) * 3) + 1 > ctx->cc_databufsize)
						{
							ctx->cc_data = (unsigned char*)realloc(ctx->cc_data, (size_t) ( (ctx->cc_count + local_cc_count) * 6) + 1);
							if (!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_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)
			{
				ctx->cc_data = (unsigned char*)realloc(ctx->cc_data, (size_t) (((local_cc_count + ctx->cc_count) * 6) + 1));
				if (!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_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 )
		{
			// TODO.
			mprint ("vcl_hrd. Not implemented for now. Hopefully not needed. Skipping rest of NAL\n");
			ctx->num_vcl_hrd++;
			// exit(1);
		}
		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 lib_cc_decode *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);
	tmp = read_exp_golomb_unsigned(&q1);
	dvprint("pic_parameter_set_id=  % 4lld (%#llX)\n",tmp,tmp);

	ctx->avc_ctx->lastframe_num = ctx->avc_ctx->frame_num;
	max_frame_num = (int) ((1<<ctx->avc_ctx->log2_max_frame_num) - 1);

	// Needs log2_max_frame_num_minus4 + 4 bits
	ctx->avc_ctx->frame_num = read_int_unsigned(&q1,ctx->avc_ctx->log2_max_frame_num);
	dvprint("frame_num=             % 4llX\n", ctx->avc_ctx->frame_num);

	if( !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[ctx->current_frame_rate];
		}
	}

	dvprint("ird_pic_flag=            % 4d\n", ird_pic_flag	);

	if( nal_unit_type == 5 )
	{
		tmp=read_exp_golomb_unsigned(&q1);
		dvprint("idr_pic_id=            % 4lld (%#llX)\n",tmp,tmp);
		//TODO
	}
	if( ctx->avc_ctx->pic_order_cnt_type == 0 )
	{
		pic_order_cnt_lsb=read_int_unsigned(&q1,ctx->avc_ctx->log2_max_pic_order_cnt_lsb);
		dvprint("pic_order_cnt_lsb=     % 4llX\n", pic_order_cnt_lsb);
	}
	if( ctx->avc_ctx->pic_order_cnt_type == 1 )
	{
		fatal(CCX_COMMON_EXIT_BUG_BUG, "In slice_header: AVC: ctx->avc_ctx->pic_order_cnt_type == 1 not yet supported.");
	}

        //Ignore slice with same pic order or pts
	if ( ccx_options.usepicorder )
	{
		if ( ctx->avc_ctx->last_pic_order_cnt_lsb == pic_order_cnt_lsb)
			return;
		ctx->avc_ctx->last_pic_order_cnt_lsb = pic_order_cnt_lsb;
	}
	else
	{
		if (ctx->timing->current_pts == ctx->avc_ctx->last_slice_pts)
			return;
		ctx->avc_ctx->last_slice_pts = ctx->timing->current_pts;
	}
#if 0
	else
	{
		/* CFS: Warning!!: Untested stuff, copied from specs (8.2.1.3) */
		LLONG FrameNumOffset = 0;
		if (ird_pic_flag == 1)
			FrameNumOffset=0;
		else if (lastframe_num > frame_num)
			FrameNumOffset = lastframe_num + max_frame_num;
		else
			FrameNumOffset = lastframe_num;

		LLONG tempPicOrderCnt=0;
		if (ird_pic_flag == 1)
			tempPicOrderCnt=0;
		else if (ctx->avc_ctx->nal_ref_idc == 0)
			tempPicOrderCnt = 2*(FrameNumOffset + frame_num) -1 ;
		else
			tempPicOrderCnt = 2*(FrameNumOffset + frame_num);

		LLONG TopFieldOrderCnt = tempPicOrderCnt;
		LLONG BottomFieldOrderCnt = tempPicOrderCnt;

		if (!field_pic_flag) {
			TopFieldOrderCnt = tempPicOrderCnt;
			BottomFieldOrderCnt = tempPicOrderCnt;
		} else if (bottom_field_flag)
			BottomFieldOrderCnt = tempPicOrderCnt;
		else
			TopFieldOrderCnt = tempPicOrderCnt;

		//pic_order_cnt_lsb=tempPicOrderCnt;
		//pic_order_cnt_lsb=read_int_unsigned(&q1,tempPicOrderCnt);
		//fatal(CCX_COMMON_EXIT_BUG_BUG, "AVC: ctx->avc_ctx->pic_order_cnt_type != 0 not yet supported.");
		//TODO
		// Calculate picture order count (POC) according to 8.2.1
	}
#endif
	// The rest of the data in slice_header() is currently unused.

	// A reference pic (I or P is always the last displayed picture of a POC
	// sequence. B slices can be reference pics, so ignore ctx->avc_ctx->nal_ref_idc.
	int isref = 0;
	switch (slice_type)
	{
		// P-SLICES
		case 0:
		case 5:
		// I-SLICES
		case 2:
		case 7:
			isref=1;
			break;
	}

	int maxrefcnt = (int) ((1<<ctx->avc_ctx->log2_max_pic_order_cnt_lsb) - 1);

	// If we saw a jump set maxidx, lastmaxidx to -1
	LLONG dif = ctx->avc_ctx->frame_num - ctx->avc_ctx->lastframe_num;
	if (dif == -max_frame_num)
		dif = 0;
	if ( ctx->avc_ctx->lastframe_num > -1 && (dif < 0 || dif > 1) )
	{
		ctx->avc_ctx->num_jump_in_frames++;
		dvprint("\nJump in frame numbers (%lld/%lld)\n", ctx->avc_ctx->frame_num, ctx->avc_ctx->lastframe_num);
		// This will prohibit setting current_tref on potential
		// jumps.
		ctx->avc_ctx->maxidx = -1;
		ctx->avc_ctx->lastmaxidx = -1;
	}


	// Sometimes two P-slices follow each other, see garbled_dishHD.mpg,
	// in this case we only treat the first as a reference pic
	if (isref && ctx->frames_since_last_gop <= 3) // Used to be == 1, but the sample file
	{ // 2014 SugarHouse Casino Mummers Parade Fancy Brigades_new.ts was garbled
		// Probably doing a proper PTS sort would be a better solution.
		isref = 0;
		dbg_print(CCX_DMT_TIME, "Ignoring this reference pic.\n");
	}

	// if slices are buffered - flush
	if (isref)
	{
		dvprint("\nReference pic! [%s]\n", slice_types[slice_type]);
		dbg_print(CCX_DMT_TIME, "\nReference pic! [%s] maxrefcnt: %3d\n",
				slice_types[slice_type], maxrefcnt);

		// Flush buffered cc blocks before doing the housekeeping
		if (ctx->has_ccdata_buffered)
		{
			process_hdcc(ctx, sub);
		}
		ctx->last_gop_length = ctx->frames_since_last_gop;
		ctx->frames_since_last_gop = 0;
		ctx->avc_ctx->last_gop_maxtref = ctx->avc_ctx->maxtref;
		ctx->avc_ctx->maxtref = 0;
		ctx->avc_ctx->lastmaxidx = ctx->avc_ctx->maxidx;
		ctx->avc_ctx->maxidx = 0;
		ctx->avc_ctx->lastminidx = ctx->avc_ctx->minidx;
		ctx->avc_ctx->minidx = 10000;

		if ( ccx_options.usepicorder ) {
			// Use pic_order_cnt_lsb

			// Make sure that current_index never wraps for curidx values that
			// are smaller than currref
			ctx->avc_ctx->currref = (int)pic_order_cnt_lsb;
			if (ctx->avc_ctx->currref < maxrefcnt/3)
			{
				ctx->avc_ctx->currref += maxrefcnt+1;
			}

			// If we wrapped arround lastmaxidx might be larger than
			// the current index - fix this.
			if (ctx->avc_ctx->lastmaxidx > ctx->avc_ctx->currref + maxrefcnt/2) // implies lastmaxidx > 0
				ctx->avc_ctx->lastmaxidx -=maxrefcnt+1;
		} else {
			// Use PTS ordering
			ctx->avc_ctx->currefpts = ctx->timing->current_pts;
			ctx->avc_ctx->currref = 0;
		}

		anchor_hdcc( ctx, ctx->avc_ctx->currref );
	}

	if ( ccx_options.usepicorder ) {
		// Use pic_order_cnt_lsb
		// Wrap (add max index value) current_index if needed.
		if( ctx->avc_ctx->currref - pic_order_cnt_lsb > maxrefcnt/2 )
			current_index = (int)pic_order_cnt_lsb + maxrefcnt+1;
		else
			current_index = (int)pic_order_cnt_lsb;

		// Track maximum index for this GOP
		if ( current_index > ctx->avc_ctx->maxidx )
			ctx->avc_ctx->maxidx = current_index;

		// Calculate tref
		if ( ctx->avc_ctx->lastmaxidx > 0 ) {
			ctx->timing->current_tref = current_index - ctx->avc_ctx->lastmaxidx -1;
			// Set maxtref
			if( ctx->timing->current_tref > ctx->avc_ctx->maxtref ) {
				ctx->avc_ctx->maxtref = ctx->timing->current_tref;
			}
			// Now an ugly workaround where pic_order_cnt_lsb increases in
			// steps of two. The 1.5 is an approximation, it should be:
			// last_gop_maxtref+1 == last_gop_length*2
			if ( ctx->avc_ctx->last_gop_maxtref > ctx->last_gop_length*1.5 ) {
				ctx->timing->current_tref = ctx->timing->current_tref/2;
			}
		}
		else
			ctx->timing->current_tref = 0;

		if ( ctx->timing->current_tref < 0 ) {
			mprint("current_tref is negative!?\n");
		}
	} else {
		// Use PTS ordering - calculate index position from PTS difference and
		// frame rate
		// The 2* accounts for a discrepancy between current and actual FPS
		// seen in some files (CCSample2.mpg)
		current_index = (int)roundportable(2*(ctx->timing->current_pts - ctx->avc_ctx->currefpts)/(MPEG_CLOCK_FREQ/current_fps));

		if (abs(current_index) >= MAXBFRAMES) {
			// Probably a jump in the timeline. Warn and handle gracefully.
			mprint("\nFound large gap(%d) in PTS! Trying to recover ...\n", current_index);
			current_index = 0;
		}

		// Track maximum index for this GOP
		if ( current_index > ctx->avc_ctx->maxidx )
			ctx->avc_ctx->maxidx = current_index;

		// Track minimum index for this GOP
		if ( current_index < ctx->avc_ctx->minidx )
			ctx->avc_ctx->minidx = current_index;

		ctx->timing->current_tref = 1;
		if ( current_index == ctx->avc_ctx->lastminidx ) {
			// This implies that the minimal index (assuming its number is
			// fairly constant) sets the temporal reference to zero - needed to set sync_pts.
			ctx->timing->current_tref = 0;
		}
		if ( ctx->avc_ctx->lastmaxidx == -1) {
			// Set temporal reference to zero on minimal index and in the first GOP
			// to avoid setting a wrong fts_offset
			ctx->timing->current_tref = 0;
		}
	}

	set_fts(ctx->timing); // Keep frames_since_ref_time==0, use current_tref

	dbg_print(CCX_DMT_TIME, "  picordercnt:%3lld tref:%3d idx:%3d refidx:%3d lmaxidx:%3d maxtref:%3d\n",
			pic_order_cnt_lsb, ctx->timing->current_tref,
			current_index, ctx->avc_ctx->currref, ctx->avc_ctx->lastmaxidx, ctx->avc_ctx->maxtref);
	dbg_print(CCX_DMT_TIME, "  sync_pts:%s (%8u)",
			print_mstime_static(ctx->timing->sync_pts/(MPEG_CLOCK_FREQ/1000)),
			(unsigned) (ctx->timing->sync_pts));
	dbg_print(CCX_DMT_TIME, " - %s since GOP: %2u",
			slice_types[slice_type],
			(unsigned) (ctx->frames_since_last_gop));
	dbg_print(CCX_DMT_TIME, "  b:%lld  frame# %lld\n", bottom_field_flag, ctx->avc_ctx->frame_num);

	// sync_pts is (was) set when current_tref was zero
	if ( ctx->avc_ctx->lastmaxidx > -1 && ctx->timing->current_tref == 0 )
	{
		if (ccx_options.debug_mask & CCX_DMT_TIME )
		{
			dbg_print(CCX_DMT_TIME, "\nNew temporal reference:\n");
			print_debug_timing(ctx->timing);
		}
	}

	total_frames_count++;
	ctx->frames_since_last_gop++;

	store_hdcc(ctx, ctx->avc_ctx->cc_data, ctx->avc_ctx->cc_count, current_index, ctx->timing->fts_now, sub);
	ctx->avc_ctx->cc_buffer_saved = CCX_TRUE; // CFS: store_hdcc supposedly saves the CC buffer to a sequence buffer
	ctx->avc_ctx->cc_count = 0;
}

// max_dec_frame_buffering .. Max frames in buffer