forked from godotengine/godot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecode.c
2943 lines (2862 loc) · 103 KB
/
decode.c
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
/********************************************************************
* *
* THIS FILE IS PART OF THE OggTheora SOFTWARE CODEC SOURCE CODE. *
* USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS *
* GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
* IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. *
* *
* THE Theora SOURCE CODE IS COPYRIGHT (C) 2002-2009 *
* by the Xiph.Org Foundation and contributors http://www.xiph.org/ *
* *
********************************************************************
function:
last mod: $Id: decode.c 16581 2009-09-25 22:56:16Z gmaxwell $
********************************************************************/
#include <stdlib.h>
#include <string.h>
#include <ogg/ogg.h>
#include "decint.h"
#if defined(OC_DUMP_IMAGES)
# include <stdio.h>
# include "png.h"
#endif
#if defined(HAVE_CAIRO)
# include <cairo.h>
#endif
/*No post-processing.*/
#define OC_PP_LEVEL_DISABLED (0)
/*Keep track of DC qi for each block only.*/
#define OC_PP_LEVEL_TRACKDCQI (1)
/*Deblock the luma plane.*/
#define OC_PP_LEVEL_DEBLOCKY (2)
/*Dering the luma plane.*/
#define OC_PP_LEVEL_DERINGY (3)
/*Stronger luma plane deringing.*/
#define OC_PP_LEVEL_SDERINGY (4)
/*Deblock the chroma planes.*/
#define OC_PP_LEVEL_DEBLOCKC (5)
/*Dering the chroma planes.*/
#define OC_PP_LEVEL_DERINGC (6)
/*Stronger chroma plane deringing.*/
#define OC_PP_LEVEL_SDERINGC (7)
/*Maximum valid post-processing level.*/
#define OC_PP_LEVEL_MAX (7)
/*The mode alphabets for the various mode coding schemes.
Scheme 0 uses a custom alphabet, which is not stored in this table.*/
static const unsigned char OC_MODE_ALPHABETS[7][OC_NMODES]={
/*Last MV dominates */
{
OC_MODE_INTER_MV_LAST,OC_MODE_INTER_MV_LAST2,OC_MODE_INTER_MV,
OC_MODE_INTER_NOMV,OC_MODE_INTRA,OC_MODE_GOLDEN_NOMV,OC_MODE_GOLDEN_MV,
OC_MODE_INTER_MV_FOUR
},
{
OC_MODE_INTER_MV_LAST,OC_MODE_INTER_MV_LAST2,OC_MODE_INTER_NOMV,
OC_MODE_INTER_MV,OC_MODE_INTRA,OC_MODE_GOLDEN_NOMV,OC_MODE_GOLDEN_MV,
OC_MODE_INTER_MV_FOUR
},
{
OC_MODE_INTER_MV_LAST,OC_MODE_INTER_MV,OC_MODE_INTER_MV_LAST2,
OC_MODE_INTER_NOMV,OC_MODE_INTRA,OC_MODE_GOLDEN_NOMV,OC_MODE_GOLDEN_MV,
OC_MODE_INTER_MV_FOUR
},
{
OC_MODE_INTER_MV_LAST,OC_MODE_INTER_MV,OC_MODE_INTER_NOMV,
OC_MODE_INTER_MV_LAST2,OC_MODE_INTRA,OC_MODE_GOLDEN_NOMV,
OC_MODE_GOLDEN_MV,OC_MODE_INTER_MV_FOUR
},
/*No MV dominates.*/
{
OC_MODE_INTER_NOMV,OC_MODE_INTER_MV_LAST,OC_MODE_INTER_MV_LAST2,
OC_MODE_INTER_MV,OC_MODE_INTRA,OC_MODE_GOLDEN_NOMV,OC_MODE_GOLDEN_MV,
OC_MODE_INTER_MV_FOUR
},
{
OC_MODE_INTER_NOMV,OC_MODE_GOLDEN_NOMV,OC_MODE_INTER_MV_LAST,
OC_MODE_INTER_MV_LAST2,OC_MODE_INTER_MV,OC_MODE_INTRA,OC_MODE_GOLDEN_MV,
OC_MODE_INTER_MV_FOUR
},
/*Default ordering.*/
{
OC_MODE_INTER_NOMV,OC_MODE_INTRA,OC_MODE_INTER_MV,OC_MODE_INTER_MV_LAST,
OC_MODE_INTER_MV_LAST2,OC_MODE_GOLDEN_NOMV,OC_MODE_GOLDEN_MV,
OC_MODE_INTER_MV_FOUR
}
};
/*The original DCT tokens are extended and reordered during the construction of
the Huffman tables.
The extension means more bits can be read with fewer calls to the bitpacker
during the Huffman decoding process (at the cost of larger Huffman tables),
and fewer tokens require additional extra bits (reducing the average storage
per decoded token).
The revised ordering reveals essential information in the token value
itself; specifically, whether or not there are additional extra bits to read
and the parameter to which those extra bits are applied.
The token is used to fetch a code word from the OC_DCT_CODE_WORD table below.
The extra bits are added into code word at the bit position inferred from the
token value, giving the final code word from which all required parameters
are derived.
The number of EOBs and the leading zero run length can be extracted directly.
The coefficient magnitude is optionally negated before extraction, according
to a 'flip' bit.*/
/*The number of additional extra bits that are decoded with each of the
internal DCT tokens.*/
static const unsigned char OC_INTERNAL_DCT_TOKEN_EXTRA_BITS[15]={
12,4,3,3,4,4,5,5,8,8,8,8,3,3,6
};
/*Whether or not an internal token needs any additional extra bits.*/
#define OC_DCT_TOKEN_NEEDS_MORE(token) \
(token<(sizeof(OC_INTERNAL_DCT_TOKEN_EXTRA_BITS)/ \
sizeof(*OC_INTERNAL_DCT_TOKEN_EXTRA_BITS)))
/*This token (OC_DCT_REPEAT_RUN3_TOKEN) requires more than 8 extra bits.*/
#define OC_DCT_TOKEN_FAT_EOB (0)
/*The number of EOBs to use for an end-of-frame token.
Note: We want to set eobs to PTRDIFF_MAX here, but that requires C99, which
is not yet available everywhere; this should be equivalent.*/
#define OC_DCT_EOB_FINISH (~(size_t)0>>1)
/*The location of the (6) run legth bits in the code word.
These are placed at index 0 and given 8 bits (even though 6 would suffice)
because it may be faster to extract the lower byte on some platforms.*/
#define OC_DCT_CW_RLEN_SHIFT (0)
/*The location of the (12) EOB bits in the code word.*/
#define OC_DCT_CW_EOB_SHIFT (8)
/*The location of the (1) flip bit in the code word.
This must be right under the magnitude bits.*/
#define OC_DCT_CW_FLIP_BIT (20)
/*The location of the (11) token magnitude bits in the code word.
These must be last, and rely on a sign-extending right shift.*/
#define OC_DCT_CW_MAG_SHIFT (21)
/*Pack the given fields into a code word.*/
#define OC_DCT_CW_PACK(_eobs,_rlen,_mag,_flip) \
((_eobs)<<OC_DCT_CW_EOB_SHIFT| \
(_rlen)<<OC_DCT_CW_RLEN_SHIFT| \
(_flip)<<OC_DCT_CW_FLIP_BIT| \
(_mag)-(_flip)<<OC_DCT_CW_MAG_SHIFT)
/*A special code word value that signals the end of the frame (a long EOB run
of zero).*/
#define OC_DCT_CW_FINISH (0)
/*The position at which to insert the extra bits in the code word.
We use this formulation because Intel has no useful cmov.
A real architecture would probably do better with two of those.
This translates to 11 instructions(!), and is _still_ faster than either a
table lookup (just barely) or the naive double-ternary implementation (which
gcc translates to a jump and a cmov).
This assumes OC_DCT_CW_RLEN_SHIFT is zero, but could easily be reworked if
you want to make one of the other shifts zero.*/
#define OC_DCT_TOKEN_EB_POS(_token) \
((OC_DCT_CW_EOB_SHIFT-OC_DCT_CW_MAG_SHIFT&-((_token)<2)) \
+(OC_DCT_CW_MAG_SHIFT&-((_token)<12)))
/*The code words for each internal token.
See the notes at OC_DCT_TOKEN_MAP for the reasons why things are out of
order.*/
static const ogg_int32_t OC_DCT_CODE_WORD[92]={
/*These tokens require additional extra bits for the EOB count.*/
/*OC_DCT_REPEAT_RUN3_TOKEN (12 extra bits)*/
OC_DCT_CW_FINISH,
/*OC_DCT_REPEAT_RUN2_TOKEN (4 extra bits)*/
OC_DCT_CW_PACK(16, 0, 0,0),
/*These tokens require additional extra bits for the magnitude.*/
/*OC_DCT_VAL_CAT5 (4 extra bits-1 already read)*/
OC_DCT_CW_PACK( 0, 0, 13,0),
OC_DCT_CW_PACK( 0, 0, 13,1),
/*OC_DCT_VAL_CAT6 (5 extra bits-1 already read)*/
OC_DCT_CW_PACK( 0, 0, 21,0),
OC_DCT_CW_PACK( 0, 0, 21,1),
/*OC_DCT_VAL_CAT7 (6 extra bits-1 already read)*/
OC_DCT_CW_PACK( 0, 0, 37,0),
OC_DCT_CW_PACK( 0, 0, 37,1),
/*OC_DCT_VAL_CAT8 (10 extra bits-2 already read)*/
OC_DCT_CW_PACK( 0, 0, 69,0),
OC_DCT_CW_PACK( 0, 0,325,0),
OC_DCT_CW_PACK( 0, 0, 69,1),
OC_DCT_CW_PACK( 0, 0,325,1),
/*These tokens require additional extra bits for the run length.*/
/*OC_DCT_RUN_CAT1C (4 extra bits-1 already read)*/
OC_DCT_CW_PACK( 0,10, +1,0),
OC_DCT_CW_PACK( 0,10, -1,0),
/*OC_DCT_ZRL_TOKEN (6 extra bits)
Flip is set to distinguish this from OC_DCT_CW_FINISH.*/
OC_DCT_CW_PACK( 0, 0, 0,1),
/*The remaining tokens require no additional extra bits.*/
/*OC_DCT_EOB1_TOKEN (0 extra bits)*/
OC_DCT_CW_PACK( 1, 0, 0,0),
/*OC_DCT_EOB2_TOKEN (0 extra bits)*/
OC_DCT_CW_PACK( 2, 0, 0,0),
/*OC_DCT_EOB3_TOKEN (0 extra bits)*/
OC_DCT_CW_PACK( 3, 0, 0,0),
/*OC_DCT_RUN_CAT1A (1 extra bit-1 already read)x5*/
OC_DCT_CW_PACK( 0, 1, +1,0),
OC_DCT_CW_PACK( 0, 1, -1,0),
OC_DCT_CW_PACK( 0, 2, +1,0),
OC_DCT_CW_PACK( 0, 2, -1,0),
OC_DCT_CW_PACK( 0, 3, +1,0),
OC_DCT_CW_PACK( 0, 3, -1,0),
OC_DCT_CW_PACK( 0, 4, +1,0),
OC_DCT_CW_PACK( 0, 4, -1,0),
OC_DCT_CW_PACK( 0, 5, +1,0),
OC_DCT_CW_PACK( 0, 5, -1,0),
/*OC_DCT_RUN_CAT2A (2 extra bits-2 already read)*/
OC_DCT_CW_PACK( 0, 1, +2,0),
OC_DCT_CW_PACK( 0, 1, +3,0),
OC_DCT_CW_PACK( 0, 1, -2,0),
OC_DCT_CW_PACK( 0, 1, -3,0),
/*OC_DCT_RUN_CAT1B (3 extra bits-3 already read)*/
OC_DCT_CW_PACK( 0, 6, +1,0),
OC_DCT_CW_PACK( 0, 7, +1,0),
OC_DCT_CW_PACK( 0, 8, +1,0),
OC_DCT_CW_PACK( 0, 9, +1,0),
OC_DCT_CW_PACK( 0, 6, -1,0),
OC_DCT_CW_PACK( 0, 7, -1,0),
OC_DCT_CW_PACK( 0, 8, -1,0),
OC_DCT_CW_PACK( 0, 9, -1,0),
/*OC_DCT_RUN_CAT2B (3 extra bits-3 already read)*/
OC_DCT_CW_PACK( 0, 2, +2,0),
OC_DCT_CW_PACK( 0, 3, +2,0),
OC_DCT_CW_PACK( 0, 2, +3,0),
OC_DCT_CW_PACK( 0, 3, +3,0),
OC_DCT_CW_PACK( 0, 2, -2,0),
OC_DCT_CW_PACK( 0, 3, -2,0),
OC_DCT_CW_PACK( 0, 2, -3,0),
OC_DCT_CW_PACK( 0, 3, -3,0),
/*OC_DCT_SHORT_ZRL_TOKEN (3 extra bits-3 already read)
Flip is set on the first one to distinguish it from OC_DCT_CW_FINISH.*/
OC_DCT_CW_PACK( 0, 0, 0,1),
OC_DCT_CW_PACK( 0, 1, 0,0),
OC_DCT_CW_PACK( 0, 2, 0,0),
OC_DCT_CW_PACK( 0, 3, 0,0),
OC_DCT_CW_PACK( 0, 4, 0,0),
OC_DCT_CW_PACK( 0, 5, 0,0),
OC_DCT_CW_PACK( 0, 6, 0,0),
OC_DCT_CW_PACK( 0, 7, 0,0),
/*OC_ONE_TOKEN (0 extra bits)*/
OC_DCT_CW_PACK( 0, 0, +1,0),
/*OC_MINUS_ONE_TOKEN (0 extra bits)*/
OC_DCT_CW_PACK( 0, 0, -1,0),
/*OC_TWO_TOKEN (0 extra bits)*/
OC_DCT_CW_PACK( 0, 0, +2,0),
/*OC_MINUS_TWO_TOKEN (0 extra bits)*/
OC_DCT_CW_PACK( 0, 0, -2,0),
/*OC_DCT_VAL_CAT2 (1 extra bit-1 already read)x4*/
OC_DCT_CW_PACK( 0, 0, +3,0),
OC_DCT_CW_PACK( 0, 0, -3,0),
OC_DCT_CW_PACK( 0, 0, +4,0),
OC_DCT_CW_PACK( 0, 0, -4,0),
OC_DCT_CW_PACK( 0, 0, +5,0),
OC_DCT_CW_PACK( 0, 0, -5,0),
OC_DCT_CW_PACK( 0, 0, +6,0),
OC_DCT_CW_PACK( 0, 0, -6,0),
/*OC_DCT_VAL_CAT3 (2 extra bits-2 already read)*/
OC_DCT_CW_PACK( 0, 0, +7,0),
OC_DCT_CW_PACK( 0, 0, +8,0),
OC_DCT_CW_PACK( 0, 0, -7,0),
OC_DCT_CW_PACK( 0, 0, -8,0),
/*OC_DCT_VAL_CAT4 (3 extra bits-3 already read)*/
OC_DCT_CW_PACK( 0, 0, +9,0),
OC_DCT_CW_PACK( 0, 0,+10,0),
OC_DCT_CW_PACK( 0, 0,+11,0),
OC_DCT_CW_PACK( 0, 0,+12,0),
OC_DCT_CW_PACK( 0, 0, -9,0),
OC_DCT_CW_PACK( 0, 0,-10,0),
OC_DCT_CW_PACK( 0, 0,-11,0),
OC_DCT_CW_PACK( 0, 0,-12,0),
/*OC_DCT_REPEAT_RUN1_TOKEN (3 extra bits-3 already read)*/
OC_DCT_CW_PACK( 8, 0, 0,0),
OC_DCT_CW_PACK( 9, 0, 0,0),
OC_DCT_CW_PACK(10, 0, 0,0),
OC_DCT_CW_PACK(11, 0, 0,0),
OC_DCT_CW_PACK(12, 0, 0,0),
OC_DCT_CW_PACK(13, 0, 0,0),
OC_DCT_CW_PACK(14, 0, 0,0),
OC_DCT_CW_PACK(15, 0, 0,0),
/*OC_DCT_REPEAT_RUN0_TOKEN (2 extra bits-2 already read)*/
OC_DCT_CW_PACK( 4, 0, 0,0),
OC_DCT_CW_PACK( 5, 0, 0,0),
OC_DCT_CW_PACK( 6, 0, 0,0),
OC_DCT_CW_PACK( 7, 0, 0,0),
};
static int oc_sb_run_unpack(oc_pack_buf *_opb){
long bits;
int ret;
/*Coding scheme:
Codeword Run Length
0 1
10x 2-3
110x 4-5
1110xx 6-9
11110xxx 10-17
111110xxxx 18-33
111111xxxxxxxxxxxx 34-4129*/
bits=oc_pack_read1(_opb);
if(bits==0)return 1;
bits=oc_pack_read(_opb,2);
if((bits&2)==0)return 2+(int)bits;
else if((bits&1)==0){
bits=oc_pack_read1(_opb);
return 4+(int)bits;
}
bits=oc_pack_read(_opb,3);
if((bits&4)==0)return 6+(int)bits;
else if((bits&2)==0){
ret=10+((bits&1)<<2);
bits=oc_pack_read(_opb,2);
return ret+(int)bits;
}
else if((bits&1)==0){
bits=oc_pack_read(_opb,4);
return 18+(int)bits;
}
bits=oc_pack_read(_opb,12);
return 34+(int)bits;
}
static int oc_block_run_unpack(oc_pack_buf *_opb){
long bits;
long bits2;
/*Coding scheme:
Codeword Run Length
0x 1-2
10x 3-4
110x 5-6
1110xx 7-10
11110xx 11-14
11111xxxx 15-30*/
bits=oc_pack_read(_opb,2);
if((bits&2)==0)return 1+(int)bits;
else if((bits&1)==0){
bits=oc_pack_read1(_opb);
return 3+(int)bits;
}
bits=oc_pack_read(_opb,2);
if((bits&2)==0)return 5+(int)bits;
else if((bits&1)==0){
bits=oc_pack_read(_opb,2);
return 7+(int)bits;
}
bits=oc_pack_read(_opb,3);
if((bits&4)==0)return 11+bits;
bits2=oc_pack_read(_opb,2);
return 15+((bits&3)<<2)+bits2;
}
static int oc_dec_init(oc_dec_ctx *_dec,const th_info *_info,
const th_setup_info *_setup){
int qti;
int pli;
int qi;
int ret;
ret=oc_state_init(&_dec->state,_info,3);
if(ret<0)return ret;
ret=oc_huff_trees_copy(_dec->huff_tables,
(const oc_huff_node *const *)_setup->huff_tables);
if(ret<0){
oc_state_clear(&_dec->state);
return ret;
}
/*For each fragment, allocate one byte for every DCT coefficient token, plus
one byte for extra-bits for each token, plus one more byte for the long
EOB run, just in case it's the very last token and has a run length of
one.*/
_dec->dct_tokens=(unsigned char *)_ogg_malloc((64+64+1)*
_dec->state.nfrags*sizeof(_dec->dct_tokens[0]));
if(_dec->dct_tokens==NULL){
oc_huff_trees_clear(_dec->huff_tables);
oc_state_clear(&_dec->state);
return TH_EFAULT;
}
for(qi=0;qi<64;qi++)for(pli=0;pli<3;pli++)for(qti=0;qti<2;qti++){
_dec->state.dequant_tables[qi][pli][qti]=
_dec->state.dequant_table_data[qi][pli][qti];
}
oc_dequant_tables_init(_dec->state.dequant_tables,_dec->pp_dc_scale,
&_setup->qinfo);
for(qi=0;qi<64;qi++){
int qsum;
qsum=0;
for(qti=0;qti<2;qti++)for(pli=0;pli<3;pli++){
qsum+=_dec->state.dequant_tables[qi][pli][qti][12]+
_dec->state.dequant_tables[qi][pli][qti][17]+
_dec->state.dequant_tables[qi][pli][qti][18]+
_dec->state.dequant_tables[qi][pli][qti][24]<<(pli==0);
}
_dec->pp_sharp_mod[qi]=-(qsum>>11);
}
memcpy(_dec->state.loop_filter_limits,_setup->qinfo.loop_filter_limits,
sizeof(_dec->state.loop_filter_limits));
_dec->pp_level=OC_PP_LEVEL_DISABLED;
_dec->dc_qis=NULL;
_dec->variances=NULL;
_dec->pp_frame_data=NULL;
_dec->stripe_cb.ctx=NULL;
_dec->stripe_cb.stripe_decoded=NULL;
#if defined(HAVE_CAIRO)
_dec->telemetry=0;
_dec->telemetry_bits=0;
_dec->telemetry_qi=0;
_dec->telemetry_mbmode=0;
_dec->telemetry_mv=0;
_dec->telemetry_frame_data=NULL;
#endif
return 0;
}
static void oc_dec_clear(oc_dec_ctx *_dec){
#if defined(HAVE_CAIRO)
_ogg_free(_dec->telemetry_frame_data);
#endif
_ogg_free(_dec->pp_frame_data);
_ogg_free(_dec->variances);
_ogg_free(_dec->dc_qis);
_ogg_free(_dec->dct_tokens);
oc_huff_trees_clear(_dec->huff_tables);
oc_state_clear(&_dec->state);
}
static int oc_dec_frame_header_unpack(oc_dec_ctx *_dec){
long val;
/*Check to make sure this is a data packet.*/
val=oc_pack_read1(&_dec->opb);
if(val!=0)return TH_EBADPACKET;
/*Read in the frame type (I or P).*/
val=oc_pack_read1(&_dec->opb);
_dec->state.frame_type=(int)val;
/*Read in the qi list.*/
val=oc_pack_read(&_dec->opb,6);
_dec->state.qis[0]=(unsigned char)val;
val=oc_pack_read1(&_dec->opb);
if(!val)_dec->state.nqis=1;
else{
val=oc_pack_read(&_dec->opb,6);
_dec->state.qis[1]=(unsigned char)val;
val=oc_pack_read1(&_dec->opb);
if(!val)_dec->state.nqis=2;
else{
val=oc_pack_read(&_dec->opb,6);
_dec->state.qis[2]=(unsigned char)val;
_dec->state.nqis=3;
}
}
if(_dec->state.frame_type==OC_INTRA_FRAME){
/*Keyframes have 3 unused configuration bits, holdovers from VP3 days.
Most of the other unused bits in the VP3 headers were eliminated.
I don't know why these remain.*/
/*I wanted to eliminate wasted bits, but not all config wiggle room
--Monty.*/
val=oc_pack_read(&_dec->opb,3);
if(val!=0)return TH_EIMPL;
}
return 0;
}
/*Mark all fragments as coded and in OC_MODE_INTRA.
This also builds up the coded fragment list (in coded order), and clears the
uncoded fragment list.
It does not update the coded macro block list nor the super block flags, as
those are not used when decoding INTRA frames.*/
static void oc_dec_mark_all_intra(oc_dec_ctx *_dec){
const oc_sb_map *sb_maps;
const oc_sb_flags *sb_flags;
oc_fragment *frags;
ptrdiff_t *coded_fragis;
ptrdiff_t ncoded_fragis;
ptrdiff_t prev_ncoded_fragis;
unsigned nsbs;
unsigned sbi;
int pli;
coded_fragis=_dec->state.coded_fragis;
prev_ncoded_fragis=ncoded_fragis=0;
sb_maps=(const oc_sb_map *)_dec->state.sb_maps;
sb_flags=_dec->state.sb_flags;
frags=_dec->state.frags;
sbi=nsbs=0;
for(pli=0;pli<3;pli++){
nsbs+=_dec->state.fplanes[pli].nsbs;
for(;sbi<nsbs;sbi++){
int quadi;
for(quadi=0;quadi<4;quadi++)if(sb_flags[sbi].quad_valid&1<<quadi){
int bi;
for(bi=0;bi<4;bi++){
ptrdiff_t fragi;
fragi=sb_maps[sbi][quadi][bi];
if(fragi>=0){
frags[fragi].coded=1;
frags[fragi].mb_mode=OC_MODE_INTRA;
coded_fragis[ncoded_fragis++]=fragi;
}
}
}
}
_dec->state.ncoded_fragis[pli]=ncoded_fragis-prev_ncoded_fragis;
prev_ncoded_fragis=ncoded_fragis;
}
_dec->state.ntotal_coded_fragis=ncoded_fragis;
}
/*Decodes the bit flags indicating whether each super block is partially coded
or not.
Return: The number of partially coded super blocks.*/
static unsigned oc_dec_partial_sb_flags_unpack(oc_dec_ctx *_dec){
oc_sb_flags *sb_flags;
unsigned nsbs;
unsigned sbi;
unsigned npartial;
unsigned run_count;
long val;
int flag;
val=oc_pack_read1(&_dec->opb);
flag=(int)val;
sb_flags=_dec->state.sb_flags;
nsbs=_dec->state.nsbs;
sbi=npartial=0;
while(sbi<nsbs){
int full_run;
run_count=oc_sb_run_unpack(&_dec->opb);
full_run=run_count>=4129;
do{
sb_flags[sbi].coded_partially=flag;
sb_flags[sbi].coded_fully=0;
npartial+=flag;
sbi++;
}
while(--run_count>0&&sbi<nsbs);
if(full_run&&sbi<nsbs){
val=oc_pack_read1(&_dec->opb);
flag=(int)val;
}
else flag=!flag;
}
/*TODO: run_count should be 0 here.
If it's not, we should issue a warning of some kind.*/
return npartial;
}
/*Decodes the bit flags for whether or not each non-partially-coded super
block is fully coded or not.
This function should only be called if there is at least one
non-partially-coded super block.
Return: The number of partially coded super blocks.*/
static void oc_dec_coded_sb_flags_unpack(oc_dec_ctx *_dec){
oc_sb_flags *sb_flags;
unsigned nsbs;
unsigned sbi;
unsigned run_count;
long val;
int flag;
sb_flags=_dec->state.sb_flags;
nsbs=_dec->state.nsbs;
/*Skip partially coded super blocks.*/
for(sbi=0;sb_flags[sbi].coded_partially;sbi++);
val=oc_pack_read1(&_dec->opb);
flag=(int)val;
do{
int full_run;
run_count=oc_sb_run_unpack(&_dec->opb);
full_run=run_count>=4129;
for(;sbi<nsbs;sbi++){
if(sb_flags[sbi].coded_partially)continue;
if(run_count--<=0)break;
sb_flags[sbi].coded_fully=flag;
}
if(full_run&&sbi<nsbs){
val=oc_pack_read1(&_dec->opb);
flag=(int)val;
}
else flag=!flag;
}
while(sbi<nsbs);
/*TODO: run_count should be 0 here.
If it's not, we should issue a warning of some kind.*/
}
static void oc_dec_coded_flags_unpack(oc_dec_ctx *_dec){
const oc_sb_map *sb_maps;
const oc_sb_flags *sb_flags;
oc_fragment *frags;
unsigned nsbs;
unsigned sbi;
unsigned npartial;
long val;
int pli;
int flag;
int run_count;
ptrdiff_t *coded_fragis;
ptrdiff_t *uncoded_fragis;
ptrdiff_t ncoded_fragis;
ptrdiff_t nuncoded_fragis;
ptrdiff_t prev_ncoded_fragis;
npartial=oc_dec_partial_sb_flags_unpack(_dec);
if(npartial<_dec->state.nsbs)oc_dec_coded_sb_flags_unpack(_dec);
if(npartial>0){
val=oc_pack_read1(&_dec->opb);
flag=!(int)val;
}
else flag=0;
sb_maps=(const oc_sb_map *)_dec->state.sb_maps;
sb_flags=_dec->state.sb_flags;
frags=_dec->state.frags;
sbi=nsbs=run_count=0;
coded_fragis=_dec->state.coded_fragis;
uncoded_fragis=coded_fragis+_dec->state.nfrags;
prev_ncoded_fragis=ncoded_fragis=nuncoded_fragis=0;
for(pli=0;pli<3;pli++){
nsbs+=_dec->state.fplanes[pli].nsbs;
for(;sbi<nsbs;sbi++){
int quadi;
for(quadi=0;quadi<4;quadi++)if(sb_flags[sbi].quad_valid&1<<quadi){
int bi;
for(bi=0;bi<4;bi++){
ptrdiff_t fragi;
fragi=sb_maps[sbi][quadi][bi];
if(fragi>=0){
int coded;
if(sb_flags[sbi].coded_fully)coded=1;
else if(!sb_flags[sbi].coded_partially)coded=0;
else{
if(run_count<=0){
run_count=oc_block_run_unpack(&_dec->opb);
flag=!flag;
}
run_count--;
coded=flag;
}
if(coded)coded_fragis[ncoded_fragis++]=fragi;
else *(uncoded_fragis-++nuncoded_fragis)=fragi;
frags[fragi].coded=coded;
}
}
}
}
_dec->state.ncoded_fragis[pli]=ncoded_fragis-prev_ncoded_fragis;
prev_ncoded_fragis=ncoded_fragis;
}
_dec->state.ntotal_coded_fragis=ncoded_fragis;
/*TODO: run_count should be 0 here.
If it's not, we should issue a warning of some kind.*/
}
typedef int (*oc_mode_unpack_func)(oc_pack_buf *_opb);
static int oc_vlc_mode_unpack(oc_pack_buf *_opb){
long val;
int i;
for(i=0;i<7;i++){
val=oc_pack_read1(_opb);
if(!val)break;
}
return i;
}
static int oc_clc_mode_unpack(oc_pack_buf *_opb){
long val;
val=oc_pack_read(_opb,3);
return (int)val;
}
/*Unpacks the list of macro block modes for INTER frames.*/
static void oc_dec_mb_modes_unpack(oc_dec_ctx *_dec){
const oc_mb_map *mb_maps;
signed char *mb_modes;
const oc_fragment *frags;
const unsigned char *alphabet;
unsigned char scheme0_alphabet[8];
oc_mode_unpack_func mode_unpack;
size_t nmbs;
size_t mbi;
long val;
int mode_scheme;
val=oc_pack_read(&_dec->opb,3);
mode_scheme=(int)val;
if(mode_scheme==0){
int mi;
/*Just in case, initialize the modes to something.
If the bitstream doesn't contain each index exactly once, it's likely
corrupt and the rest of the packet is garbage anyway, but this way we
won't crash, and we'll decode SOMETHING.*/
/*LOOP VECTORIZES*/
for(mi=0;mi<OC_NMODES;mi++)scheme0_alphabet[mi]=OC_MODE_INTER_NOMV;
for(mi=0;mi<OC_NMODES;mi++){
val=oc_pack_read(&_dec->opb,3);
scheme0_alphabet[val]=OC_MODE_ALPHABETS[6][mi];
}
alphabet=scheme0_alphabet;
}
else alphabet=OC_MODE_ALPHABETS[mode_scheme-1];
if(mode_scheme==7)mode_unpack=oc_clc_mode_unpack;
else mode_unpack=oc_vlc_mode_unpack;
mb_modes=_dec->state.mb_modes;
mb_maps=(const oc_mb_map *)_dec->state.mb_maps;
nmbs=_dec->state.nmbs;
frags=_dec->state.frags;
for(mbi=0;mbi<nmbs;mbi++){
if(mb_modes[mbi]!=OC_MODE_INVALID){
int bi;
/*Check for a coded luma block in this macro block.*/
for(bi=0;bi<4&&!frags[mb_maps[mbi][0][bi]].coded;bi++);
/*We found one, decode a mode.*/
if(bi<4)mb_modes[mbi]=alphabet[(*mode_unpack)(&_dec->opb)];
/*There were none: INTER_NOMV is forced.*/
else mb_modes[mbi]=OC_MODE_INTER_NOMV;
}
}
}
typedef int (*oc_mv_comp_unpack_func)(oc_pack_buf *_opb);
static int oc_vlc_mv_comp_unpack(oc_pack_buf *_opb){
long bits;
int mask;
int mv;
bits=oc_pack_read(_opb,3);
switch(bits){
case 0:return 0;
case 1:return 1;
case 2:return -1;
case 3:
case 4:{
mv=(int)(bits-1);
bits=oc_pack_read1(_opb);
}break;
/*case 5:
case 6:
case 7:*/
default:{
mv=1<<bits-3;
bits=oc_pack_read(_opb,bits-2);
mv+=(int)(bits>>1);
bits&=1;
}break;
}
mask=-(int)bits;
return mv+mask^mask;
}
static int oc_clc_mv_comp_unpack(oc_pack_buf *_opb){
long bits;
int mask;
int mv;
bits=oc_pack_read(_opb,6);
mv=(int)bits>>1;
mask=-((int)bits&1);
return mv+mask^mask;
}
/*Unpacks the list of motion vectors for INTER frames, and propagtes the macro
block modes and motion vectors to the individual fragments.*/
static void oc_dec_mv_unpack_and_frag_modes_fill(oc_dec_ctx *_dec){
const oc_mb_map *mb_maps;
const signed char *mb_modes;
oc_set_chroma_mvs_func set_chroma_mvs;
oc_mv_comp_unpack_func mv_comp_unpack;
oc_fragment *frags;
oc_mv *frag_mvs;
const unsigned char *map_idxs;
int map_nidxs;
oc_mv last_mv[2];
oc_mv cbmvs[4];
size_t nmbs;
size_t mbi;
long val;
set_chroma_mvs=OC_SET_CHROMA_MVS_TABLE[_dec->state.info.pixel_fmt];
val=oc_pack_read1(&_dec->opb);
mv_comp_unpack=val?oc_clc_mv_comp_unpack:oc_vlc_mv_comp_unpack;
map_idxs=OC_MB_MAP_IDXS[_dec->state.info.pixel_fmt];
map_nidxs=OC_MB_MAP_NIDXS[_dec->state.info.pixel_fmt];
memset(last_mv,0,sizeof(last_mv));
frags=_dec->state.frags;
frag_mvs=_dec->state.frag_mvs;
mb_maps=(const oc_mb_map *)_dec->state.mb_maps;
mb_modes=_dec->state.mb_modes;
nmbs=_dec->state.nmbs;
for(mbi=0;mbi<nmbs;mbi++){
int mb_mode;
mb_mode=mb_modes[mbi];
if(mb_mode!=OC_MODE_INVALID){
oc_mv mbmv;
ptrdiff_t fragi;
int coded[13];
int codedi;
int ncoded;
int mapi;
int mapii;
/*Search for at least one coded fragment.*/
ncoded=mapii=0;
do{
mapi=map_idxs[mapii];
fragi=mb_maps[mbi][mapi>>2][mapi&3];
if(frags[fragi].coded)coded[ncoded++]=mapi;
}
while(++mapii<map_nidxs);
if(ncoded<=0)continue;
switch(mb_mode){
case OC_MODE_INTER_MV_FOUR:{
oc_mv lbmvs[4];
int bi;
/*Mark the tail of the list, so we don't accidentally go past it.*/
coded[ncoded]=-1;
for(bi=codedi=0;bi<4;bi++){
if(coded[codedi]==bi){
codedi++;
fragi=mb_maps[mbi][0][bi];
frags[fragi].mb_mode=mb_mode;
lbmvs[bi][0]=(signed char)(*mv_comp_unpack)(&_dec->opb);
lbmvs[bi][1]=(signed char)(*mv_comp_unpack)(&_dec->opb);
memcpy(frag_mvs[fragi],lbmvs[bi],sizeof(lbmvs[bi]));
}
else lbmvs[bi][0]=lbmvs[bi][1]=0;
}
if(codedi>0){
memcpy(last_mv[1],last_mv[0],sizeof(last_mv[1]));
memcpy(last_mv[0],lbmvs[coded[codedi-1]],sizeof(last_mv[0]));
}
if(codedi<ncoded){
(*set_chroma_mvs)(cbmvs,(const oc_mv *)lbmvs);
for(;codedi<ncoded;codedi++){
mapi=coded[codedi];
bi=mapi&3;
fragi=mb_maps[mbi][mapi>>2][bi];
frags[fragi].mb_mode=mb_mode;
memcpy(frag_mvs[fragi],cbmvs[bi],sizeof(cbmvs[bi]));
}
}
}break;
case OC_MODE_INTER_MV:{
memcpy(last_mv[1],last_mv[0],sizeof(last_mv[1]));
mbmv[0]=last_mv[0][0]=(signed char)(*mv_comp_unpack)(&_dec->opb);
mbmv[1]=last_mv[0][1]=(signed char)(*mv_comp_unpack)(&_dec->opb);
}break;
case OC_MODE_INTER_MV_LAST:memcpy(mbmv,last_mv[0],sizeof(mbmv));break;
case OC_MODE_INTER_MV_LAST2:{
memcpy(mbmv,last_mv[1],sizeof(mbmv));
memcpy(last_mv[1],last_mv[0],sizeof(last_mv[1]));
memcpy(last_mv[0],mbmv,sizeof(last_mv[0]));
}break;
case OC_MODE_GOLDEN_MV:{
mbmv[0]=(signed char)(*mv_comp_unpack)(&_dec->opb);
mbmv[1]=(signed char)(*mv_comp_unpack)(&_dec->opb);
}break;
default:memset(mbmv,0,sizeof(mbmv));break;
}
/*4MV mode fills in the fragments itself.
For all other modes we can use this common code.*/
if(mb_mode!=OC_MODE_INTER_MV_FOUR){
for(codedi=0;codedi<ncoded;codedi++){
mapi=coded[codedi];
fragi=mb_maps[mbi][mapi>>2][mapi&3];
frags[fragi].mb_mode=mb_mode;
memcpy(frag_mvs[fragi],mbmv,sizeof(mbmv));
}
}
}
}
}
static void oc_dec_block_qis_unpack(oc_dec_ctx *_dec){
oc_fragment *frags;
const ptrdiff_t *coded_fragis;
ptrdiff_t ncoded_fragis;
ptrdiff_t fragii;
ptrdiff_t fragi;
ncoded_fragis=_dec->state.ntotal_coded_fragis;
if(ncoded_fragis<=0)return;
frags=_dec->state.frags;
coded_fragis=_dec->state.coded_fragis;
if(_dec->state.nqis==1){
/*If this frame has only a single qi value, then just use it for all coded
fragments.*/
for(fragii=0;fragii<ncoded_fragis;fragii++){
frags[coded_fragis[fragii]].qii=0;
}
}
else{
long val;
int flag;
int nqi1;
int run_count;
/*Otherwise, we decode a qi index for each fragment, using two passes of
the same binary RLE scheme used for super-block coded bits.
The first pass marks each fragment as having a qii of 0 or greater than
0, and the second pass (if necessary), distinguishes between a qii of
1 and 2.
At first we just store the qii in the fragment.
After all the qii's are decoded, we make a final pass to replace them
with the corresponding qi's for this frame.*/
val=oc_pack_read1(&_dec->opb);
flag=(int)val;
nqi1=0;
fragii=0;
while(fragii<ncoded_fragis){
int full_run;
run_count=oc_sb_run_unpack(&_dec->opb);
full_run=run_count>=4129;
do{
frags[coded_fragis[fragii++]].qii=flag;
nqi1+=flag;
}
while(--run_count>0&&fragii<ncoded_fragis);
if(full_run&&fragii<ncoded_fragis){
val=oc_pack_read1(&_dec->opb);
flag=(int)val;
}
else flag=!flag;
}
/*TODO: run_count should be 0 here.
If it's not, we should issue a warning of some kind.*/
/*If we have 3 different qi's for this frame, and there was at least one
fragment with a non-zero qi, make the second pass.*/
if(_dec->state.nqis==3&&nqi1>0){
/*Skip qii==0 fragments.*/
for(fragii=0;frags[coded_fragis[fragii]].qii==0;fragii++);
val=oc_pack_read1(&_dec->opb);
flag=(int)val;
do{
int full_run;
run_count=oc_sb_run_unpack(&_dec->opb);
full_run=run_count>=4129;
for(;fragii<ncoded_fragis;fragii++){
fragi=coded_fragis[fragii];
if(frags[fragi].qii==0)continue;
if(run_count--<=0)break;
frags[fragi].qii+=flag;
}
if(full_run&&fragii<ncoded_fragis){
val=oc_pack_read1(&_dec->opb);
flag=(int)val;
}
else flag=!flag;
}
while(fragii<ncoded_fragis);
/*TODO: run_count should be 0 here.
If it's not, we should issue a warning of some kind.*/
}
}
}
/*Unpacks the DC coefficient tokens.
Unlike when unpacking the AC coefficient tokens, we actually need to decode
the DC coefficient values now so that we can do DC prediction.
_huff_idx: The index of the Huffman table to use for each color plane.
_ntoks_left: The number of tokens left to be decoded in each color plane for
each coefficient.
This is updated as EOB tokens and zero run tokens are decoded.
Return: The length of any outstanding EOB run.*/
static ptrdiff_t oc_dec_dc_coeff_unpack(oc_dec_ctx *_dec,int _huff_idxs[2],
ptrdiff_t _ntoks_left[3][64]){
unsigned char *dct_tokens;
oc_fragment *frags;
const ptrdiff_t *coded_fragis;
ptrdiff_t ncoded_fragis;
ptrdiff_t fragii;
ptrdiff_t eobs;
ptrdiff_t ti;
int pli;
dct_tokens=_dec->dct_tokens;
frags=_dec->state.frags;
coded_fragis=_dec->state.coded_fragis;
ncoded_fragis=fragii=eobs=ti=0;
for(pli=0;pli<3;pli++){
ptrdiff_t run_counts[64];
ptrdiff_t eob_count;
ptrdiff_t eobi;
int rli;
ncoded_fragis+=_dec->state.ncoded_fragis[pli];
memset(run_counts,0,sizeof(run_counts));
_dec->eob_runs[pli][0]=eobs;
_dec->ti0[pli][0]=ti;
/*Continue any previous EOB run, if there was one.*/
eobi=eobs;
if(ncoded_fragis-fragii<eobi)eobi=ncoded_fragis-fragii;
eob_count=eobi;
eobs-=eobi;
while(eobi-->0)frags[coded_fragis[fragii++]].dc=0;