forked from godotengine/godot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencode.c
1615 lines (1552 loc) · 53.7 KB
/
encode.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 http://www.xiph.org/ *
* *
********************************************************************
function:
last mod: $Id: encode.c 16503 2009-08-22 18:14:02Z giles $
********************************************************************/
#include <stdlib.h>
#include <string.h>
#include "encint.h"
#if defined(OC_X86_ASM)
# include "x86/x86enc.h"
#endif
/*The default quantization parameters used by VP3.1.*/
static const int OC_VP31_RANGE_SIZES[1]={63};
static const th_quant_base OC_VP31_BASES_INTRA_Y[2]={
{
16, 11, 10, 16, 24, 40, 51, 61,
12, 12, 14, 19, 26, 58, 60, 55,
14, 13, 16, 24, 40, 57, 69, 56,
14, 17, 22, 29, 51, 87, 80, 62,
18, 22, 37, 58, 68,109,103, 77,
24, 35, 55, 64, 81,104,113, 92,
49, 64, 78, 87,103,121,120,101,
72, 92, 95, 98,112,100,103, 99
},
{
16, 11, 10, 16, 24, 40, 51, 61,
12, 12, 14, 19, 26, 58, 60, 55,
14, 13, 16, 24, 40, 57, 69, 56,
14, 17, 22, 29, 51, 87, 80, 62,
18, 22, 37, 58, 68,109,103, 77,
24, 35, 55, 64, 81,104,113, 92,
49, 64, 78, 87,103,121,120,101,
72, 92, 95, 98,112,100,103, 99
}
};
static const th_quant_base OC_VP31_BASES_INTRA_C[2]={
{
17, 18, 24, 47, 99, 99, 99, 99,
18, 21, 26, 66, 99, 99, 99, 99,
24, 26, 56, 99, 99, 99, 99, 99,
47, 66, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99
},
{
17, 18, 24, 47, 99, 99, 99, 99,
18, 21, 26, 66, 99, 99, 99, 99,
24, 26, 56, 99, 99, 99, 99, 99,
47, 66, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99
}
};
static const th_quant_base OC_VP31_BASES_INTER[2]={
{
16, 16, 16, 20, 24, 28, 32, 40,
16, 16, 20, 24, 28, 32, 40, 48,
16, 20, 24, 28, 32, 40, 48, 64,
20, 24, 28, 32, 40, 48, 64, 64,
24, 28, 32, 40, 48, 64, 64, 64,
28, 32, 40, 48, 64, 64, 64, 96,
32, 40, 48, 64, 64, 64, 96,128,
40, 48, 64, 64, 64, 96,128,128
},
{
16, 16, 16, 20, 24, 28, 32, 40,
16, 16, 20, 24, 28, 32, 40, 48,
16, 20, 24, 28, 32, 40, 48, 64,
20, 24, 28, 32, 40, 48, 64, 64,
24, 28, 32, 40, 48, 64, 64, 64,
28, 32, 40, 48, 64, 64, 64, 96,
32, 40, 48, 64, 64, 64, 96,128,
40, 48, 64, 64, 64, 96,128,128
}
};
const th_quant_info TH_VP31_QUANT_INFO={
{
220,200,190,180,170,170,160,160,
150,150,140,140,130,130,120,120,
110,110,100,100, 90, 90, 90, 80,
80, 80, 70, 70, 70, 60, 60, 60,
60, 50, 50, 50, 50, 40, 40, 40,
40, 40, 30, 30, 30, 30, 30, 30,
30, 20, 20, 20, 20, 20, 20, 20,
20, 10, 10, 10, 10, 10, 10, 10
},
{
500,450,400,370,340,310,285,265,
245,225,210,195,185,180,170,160,
150,145,135,130,125,115,110,107,
100, 96, 93, 89, 85, 82, 75, 74,
70, 68, 64, 60, 57, 56, 52, 50,
49, 45, 44, 43, 40, 38, 37, 35,
33, 32, 30, 29, 28, 25, 24, 22,
21, 19, 18, 17, 15, 13, 12, 10
},
{
30,25,20,20,15,15,14,14,
13,13,12,12,11,11,10,10,
9, 9, 8, 8, 7, 7, 7, 7,
6, 6, 6, 6, 5, 5, 5, 5,
4, 4, 4, 4, 3, 3, 3, 3,
2, 2, 2, 2, 2, 2, 2, 2,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0
},
{
{
{1,OC_VP31_RANGE_SIZES,OC_VP31_BASES_INTRA_Y},
{1,OC_VP31_RANGE_SIZES,OC_VP31_BASES_INTRA_C},
{1,OC_VP31_RANGE_SIZES,OC_VP31_BASES_INTRA_C}
},
{
{1,OC_VP31_RANGE_SIZES,OC_VP31_BASES_INTER},
{1,OC_VP31_RANGE_SIZES,OC_VP31_BASES_INTER},
{1,OC_VP31_RANGE_SIZES,OC_VP31_BASES_INTER}
}
}
};
/*The current default quantization parameters.*/
static const int OC_DEF_QRANGE_SIZES[3]={32,16,15};
static const th_quant_base OC_DEF_BASES_INTRA_Y[4]={
{
15, 15, 15, 15, 15, 15, 15, 15,
15, 15, 15, 15, 15, 15, 15, 15,
15, 15, 15, 15, 15, 15, 15, 15,
15, 15, 15, 15, 15, 15, 15, 15,
15, 15, 15, 15, 15, 15, 15, 15,
15, 15, 15, 15, 15, 15, 15, 15,
15, 15, 15, 15, 15, 15, 15, 15,
15, 15, 15, 15, 15, 15, 15, 15,
},
{
15, 12, 12, 15, 18, 20, 20, 21,
13, 13, 14, 17, 18, 21, 21, 20,
14, 14, 15, 18, 20, 21, 21, 21,
14, 16, 17, 19, 20, 21, 21, 21,
16, 17, 20, 21, 21, 21, 21, 21,
18, 19, 20, 21, 21, 21, 21, 21,
20, 21, 21, 21, 21, 21, 21, 21,
21, 21, 21, 21, 21, 21, 21, 21
},
{
16, 12, 11, 16, 20, 25, 27, 28,
13, 13, 14, 18, 21, 28, 28, 27,
14, 13, 16, 20, 25, 28, 28, 28,
14, 16, 19, 22, 27, 29, 29, 28,
17, 19, 25, 28, 28, 30, 30, 29,
20, 24, 27, 28, 29, 30, 30, 29,
27, 28, 29, 29, 30, 30, 30, 30,
29, 29, 29, 29, 30, 30, 30, 29
},
{
16, 11, 10, 16, 24, 40, 51, 61,
12, 12, 14, 19, 26, 58, 60, 55,
14, 13, 16, 24, 40, 57, 69, 56,
14, 17, 22, 29, 51, 87, 80, 62,
18, 22, 37, 58, 68,109,103, 77,
24, 35, 55, 64, 81,104,113, 92,
49, 64, 78, 87,103,121,120,101,
72, 92, 95, 98,112,100,103, 99
}
};
static const th_quant_base OC_DEF_BASES_INTRA_C[4]={
{
19, 19, 19, 19, 19, 19, 19, 19,
19, 19, 19, 19, 19, 19, 19, 19,
19, 19, 19, 19, 19, 19, 19, 19,
19, 19, 19, 19, 19, 19, 19, 19,
19, 19, 19, 19, 19, 19, 19, 19,
19, 19, 19, 19, 19, 19, 19, 19,
19, 19, 19, 19, 19, 19, 19, 19,
19, 19, 19, 19, 19, 19, 19, 19
},
{
18, 18, 21, 25, 26, 26, 26, 26,
18, 20, 22, 26, 26, 26, 26, 26,
21, 22, 25, 26, 26, 26, 26, 26,
25, 26, 26, 26, 26, 26, 26, 26,
26, 26, 26, 26, 26, 26, 26, 26,
26, 26, 26, 26, 26, 26, 26, 26,
26, 26, 26, 26, 26, 26, 26, 26,
26, 26, 26, 26, 26, 26, 26, 26
},
{
17, 18, 22, 31, 36, 36, 36, 36,
18, 20, 24, 34, 36, 36, 36, 36,
22, 24, 33, 36, 36, 36, 36, 36,
31, 34, 36, 36, 36, 36, 36, 36,
36, 36, 36, 36, 36, 36, 36, 36,
36, 36, 36, 36, 36, 36, 36, 36,
36, 36, 36, 36, 36, 36, 36, 36,
36, 36, 36, 36, 36, 36, 36, 36
},
{
17, 18, 24, 47, 99, 99, 99, 99,
18, 21, 26, 66, 99, 99, 99, 99,
24, 26, 56, 99, 99, 99, 99, 99,
47, 66, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99
}
};
static const th_quant_base OC_DEF_BASES_INTER[4]={
{
21, 21, 21, 21, 21, 21, 21, 21,
21, 21, 21, 21, 21, 21, 21, 21,
21, 21, 21, 21, 21, 21, 21, 21,
21, 21, 21, 21, 21, 21, 21, 21,
21, 21, 21, 21, 21, 21, 21, 21,
21, 21, 21, 21, 21, 21, 21, 21,
21, 21, 21, 21, 21, 21, 21, 21,
21, 21, 21, 21, 21, 21, 21, 21
},
{
18, 18, 18, 21, 23, 24, 25, 27,
18, 18, 21, 23, 24, 25, 27, 28,
18, 21, 23, 24, 25, 27, 28, 29,
21, 23, 24, 25, 27, 28, 29, 29,
23, 24, 25, 27, 28, 29, 29, 29,
24, 25, 27, 28, 29, 29, 29, 30,
25, 27, 28, 29, 29, 29, 30, 30,
27, 28, 29, 29, 29, 30, 30, 30
},
{
17, 17, 17, 20, 23, 26, 28, 32,
17, 17, 20, 23, 26, 28, 32, 34,
17, 20, 23, 26, 28, 32, 34, 37,
20, 23, 26, 28, 32, 34, 37, 37,
23, 26, 28, 32, 34, 37, 37, 37,
26, 28, 32, 34, 37, 37, 37, 41,
28, 32, 34, 37, 37, 37, 41, 42,
32, 34, 37, 37, 37, 41, 42, 42
},
{
16, 16, 16, 20, 24, 28, 32, 40,
16, 16, 20, 24, 28, 32, 40, 48,
16, 20, 24, 28, 32, 40, 48, 64,
20, 24, 28, 32, 40, 48, 64, 64,
24, 28, 32, 40, 48, 64, 64, 64,
28, 32, 40, 48, 64, 64, 64, 96,
32, 40, 48, 64, 64, 64, 96,128,
40, 48, 64, 64, 64, 96,128,128
}
};
const th_quant_info TH_DEF_QUANT_INFO={
{
365,348,333,316,300,287,277,265,
252,240,229,219,206,197,189,180,
171,168,160,153,146,139,132,127,
121,115,110,107,101, 97, 94, 89,
85, 83, 78, 73, 72, 67, 66, 62,
60, 59, 56, 53, 52, 48, 47, 43,
42, 40, 36, 35, 34, 33, 31, 30,
28, 25, 24, 22, 20, 17, 14, 10
},
{
365,348,333,316,300,287,277,265,
252,240,229,219,206,197,189,180,
171,168,160,153,146,139,132,127,
121,115,110,107,101, 97, 94, 89,
85, 83, 78, 73, 72, 67, 66, 62,
60, 59, 56, 53, 52, 48, 47, 43,
42, 40, 36, 35, 34, 33, 31, 30,
28, 25, 24, 22, 20, 17, 14, 10
},
{
30,25,20,20,15,15,14,14,
13,13,12,12,11,11,10,10,
9, 9, 8, 8, 7, 7, 7, 7,
6, 6, 6, 6, 5, 5, 5, 5,
4, 4, 4, 4, 3, 3, 3, 3,
2, 2, 2, 2, 2, 2, 2, 2,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0
},
{
{
{3,OC_DEF_QRANGE_SIZES,OC_DEF_BASES_INTRA_Y},
{3,OC_DEF_QRANGE_SIZES,OC_DEF_BASES_INTRA_C},
{3,OC_DEF_QRANGE_SIZES,OC_DEF_BASES_INTRA_C}
},
{
{3,OC_DEF_QRANGE_SIZES,OC_DEF_BASES_INTER},
{3,OC_DEF_QRANGE_SIZES,OC_DEF_BASES_INTER},
{3,OC_DEF_QRANGE_SIZES,OC_DEF_BASES_INTER}
}
}
};
/*The Huffman codes used for macro block modes.*/
const unsigned char OC_MODE_BITS[2][OC_NMODES]={
/*Codebook 0: a maximally skewed prefix code.*/
{1,2,3,4,5,6,7,7},
/*Codebook 1: a fixed-length code.*/
{3,3,3,3,3,3,3,3}
};
static const unsigned char OC_MODE_CODES[2][OC_NMODES]={
/*Codebook 0: a maximally skewed prefix code.*/
{0x00,0x02,0x06,0x0E,0x1E,0x3E,0x7E,0x7F},
/*Codebook 1: a fixed-length code.*/
{0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07}
};
/*The Huffman codes used for motion vectors.*/
const unsigned char OC_MV_BITS[2][64]={
/*Codebook 0: VLC code.*/
{
8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
8,7,7,7,7,7,7,7,7,6,6,6,6,4,4,3,
3,
3,4,4,6,6,6,6,7,7,7,7,7,7,7,7,8,
8,8,8,8,8,8,8,8,8,8,8,8,8,8,8
},
/*Codebook 1: (5 bit magnitude, 1 bit sign).
This wastes a code word (0x01, negative zero), or a bit (0x00, positive
zero, requires only 5 bits to uniquely decode), but is hopefully not used
very often.*/
{
6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
6,
6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
6,6,6,6,6,6,6,6,6,6,6,6,6,6,6
}
};
static const unsigned char OC_MV_CODES[2][64]={
/*Codebook 0: VLC code.*/
{
0xFF,0xFD,0xFB,0xF9,0xF7,0xF5,0xF3,
0xF1,0xEF,0xED,0xEB,0xE9,0xE7,0xE5,0xE3,
0xE1,0x6F,0x6D,0x6B,0x69,0x67,0x65,0x63,
0x61,0x2F,0x2D,0x2B,0x29,0x09,0x07,0x02,
0x00,
0x01,0x06,0x08,0x28,0x2A,0x2C,0x2E,0x60,
0x62,0x64,0x66,0x68,0x6A,0x6C,0x6E,0xE0,
0xE2,0xE4,0xE6,0xE8,0xEA,0xEC,0xEE,0xF0,
0xF2,0xF4,0xF6,0xF8,0xFA,0xFC,0xFE
},
/*Codebook 1: (5 bit magnitude, 1 bit sign).*/
{
0x3F,0x3D,0x3B,0x39,0x37,0x35,0x33,
0x31,0x2F,0x2D,0x2B,0x29,0x27,0x25,0x23,
0x21,0x1F,0x1D,0x1B,0x19,0x17,0x15,0x13,
0x11,0x0F,0x0D,0x0B,0x09,0x07,0x05,0x03,
0x00,
0x02,0x04,0x06,0x08,0x0A,0x0C,0x0E,0x10,
0x12,0x14,0x16,0x18,0x1A,0x1C,0x1E,0x20,
0x22,0x24,0x26,0x28,0x2A,0x2C,0x2E,0x30,
0x32,0x34,0x36,0x38,0x3A,0x3C,0x3E
}
};
/*Super block run 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*/
const ogg_uint16_t OC_SB_RUN_VAL_MIN[8]={1,2,4,6,10,18,34,4130};
static const unsigned OC_SB_RUN_CODE_PREFIX[7]={
0,4,0xC,0x38,0xF0,0x3E0,0x3F000
};
const unsigned char OC_SB_RUN_CODE_NBITS[7]={1,3,4,6,8,10,18};
/*Writes the bit pattern for the run length of a super block run to the given
oggpack_buffer.
_opb: The buffer to write to.
_run_count: The length of the run, which must be positive.
_flag: The current flag.
_done: Whether or not more flags are to be encoded.*/
static void oc_sb_run_pack(oggpack_buffer *_opb,ptrdiff_t _run_count,
int _flag,int _done){
int i;
if(_run_count>=4129){
do{
oggpackB_write(_opb,0x3FFFF,18);
_run_count-=4129;
if(_run_count>0)oggpackB_write(_opb,_flag,1);
else if(!_done)oggpackB_write(_opb,!_flag,1);
}
while(_run_count>=4129);
if(_run_count<=0)return;
}
for(i=0;_run_count>=OC_SB_RUN_VAL_MIN[i+1];i++);
oggpackB_write(_opb,OC_SB_RUN_CODE_PREFIX[i]+_run_count-OC_SB_RUN_VAL_MIN[i],
OC_SB_RUN_CODE_NBITS[i]);
}
/*Block run coding scheme:
Codeword Run Length
0x 1-2
10x 3-4
110x 5-6
1110xx 7-10
11110xx 11-14
11111xxxx 15-30*/
const unsigned char OC_BLOCK_RUN_CODE_NBITS[30]={
2,2,3,3,4,4,6,6,6,6,7,7,7,7,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9
};
static const ogg_uint16_t OC_BLOCK_RUN_CODE_PATTERN[30]={
0x000,0x001,0x004,0x005,0x00C,0x00D,0x038,
0x039,0x03A,0x03B,0x078,0x079,0x07A,0x07B,0x1F0,
0x1F1,0x1F2,0x1F3,0x1F4,0x1F5,0x1F6,0x1F7,0x1F8,
0x1F9,0x1FA,0x1FB,0x1FC,0x1FD,0x1FE,0x1FF
};
/*Writes the bit pattern for the run length of a block run to the given
oggpack_buffer.
_opb: The buffer to write to.
_run_count: The length of the run.
This must be positive, and no more than 30.*/
static void oc_block_run_pack(oggpack_buffer *_opb,int _run_count){
oggpackB_write(_opb,OC_BLOCK_RUN_CODE_PATTERN[_run_count-1],
OC_BLOCK_RUN_CODE_NBITS[_run_count-1]);
}
static void oc_enc_frame_header_pack(oc_enc_ctx *_enc){
/*Mark this as a data packet.*/
oggpackB_write(&_enc->opb,0,1);
/*Output the frame type (key frame or delta frame).*/
oggpackB_write(&_enc->opb,_enc->state.frame_type,1);
/*Write out the current qi list.*/
oggpackB_write(&_enc->opb,_enc->state.qis[0],6);
if(_enc->state.nqis>1){
oggpackB_write(&_enc->opb,1,1);
oggpackB_write(&_enc->opb,_enc->state.qis[1],6);
if(_enc->state.nqis>2){
oggpackB_write(&_enc->opb,1,1);
oggpackB_write(&_enc->opb,_enc->state.qis[2],6);
}
else oggpackB_write(&_enc->opb,0,1);
}
else oggpackB_write(&_enc->opb,0,1);
if(_enc->state.frame_type==OC_INTRA_FRAME){
/*Key frames have 3 unused configuration bits, holdovers from the VP3 days.
Most of the other unused bits in the VP3 headers were eliminated.
Monty kept these to leave us some wiggle room for future expansion,
though a single bit in all frames would have been far more useful.*/
oggpackB_write(&_enc->opb,0,3);
}
}
/*Writes the bit flags for whether or not each super block is partially coded
or not.
These flags are run-length encoded, with the flag value alternating between
each run.
Return: The number partially coded SBs.*/
static unsigned oc_enc_partial_sb_flags_pack(oc_enc_ctx *_enc){
const oc_sb_flags *sb_flags;
unsigned nsbs;
unsigned sbi;
unsigned npartial;
int flag;
sb_flags=_enc->state.sb_flags;
nsbs=_enc->state.nsbs;
flag=sb_flags[0].coded_partially;
oggpackB_write(&_enc->opb,flag,1);
sbi=npartial=0;
do{
unsigned run_count;
for(run_count=0;sbi<nsbs;sbi++){
if(sb_flags[sbi].coded_partially!=flag)break;
run_count++;
npartial+=flag;
}
oc_sb_run_pack(&_enc->opb,run_count,flag,sbi>=nsbs);
flag=!flag;
}
while(sbi<nsbs);
return npartial;
}
/*Writes the coded/not coded flags for each super block that is not partially
coded.
These flags are run-length encoded, with the flag value altenating between
each run.*/
static void oc_enc_coded_sb_flags_pack(oc_enc_ctx *_enc){
const oc_sb_flags *sb_flags;
unsigned nsbs;
unsigned sbi;
int flag;
sb_flags=_enc->state.sb_flags;
nsbs=_enc->state.nsbs;
/*Skip partially coded super blocks; their flags have already been coded.*/
for(sbi=0;sb_flags[sbi].coded_partially;sbi++);
flag=sb_flags[sbi].coded_fully;
oggpackB_write(&_enc->opb,flag,1);
do{
unsigned run_count;
for(run_count=0;sbi<nsbs;sbi++){
if(sb_flags[sbi].coded_partially)continue;
if(sb_flags[sbi].coded_fully!=flag)break;
run_count++;
}
oc_sb_run_pack(&_enc->opb,run_count,flag,sbi>=nsbs);
flag=!flag;
}
while(sbi<nsbs);
}
static void oc_enc_coded_flags_pack(oc_enc_ctx *_enc){
const oc_sb_map *sb_maps;
const oc_sb_flags *sb_flags;
unsigned nsbs;
const oc_fragment *frags;
unsigned npartial;
int run_count;
int flag;
int pli;
unsigned sbi;
npartial=oc_enc_partial_sb_flags_pack(_enc);
if(npartial<_enc->state.nsbs)oc_enc_coded_sb_flags_pack(_enc);
sb_maps=(const oc_sb_map *)_enc->state.sb_maps;
sb_flags=_enc->state.sb_flags;
nsbs=_enc->state.nsbs;
frags=_enc->state.frags;
for(sbi=0;sbi<nsbs&&!sb_flags[sbi].coded_partially;sbi++);
/*If there's at least one partial SB, store individual coded block flags.*/
if(sbi<nsbs){
flag=frags[sb_maps[sbi][0][0]].coded;
oggpackB_write(&_enc->opb,flag,1);
run_count=0;
nsbs=sbi=0;
for(pli=0;pli<3;pli++){
nsbs+=_enc->state.fplanes[pli].nsbs;
for(;sbi<nsbs;sbi++){
int quadi;
int bi;
ptrdiff_t fragi;
if(sb_flags[sbi].coded_partially){
for(quadi=0;quadi<4;quadi++){
for(bi=0;bi<4;bi++){
fragi=sb_maps[sbi][quadi][bi];
if(fragi>=0){
if(frags[fragi].coded!=flag){
oc_block_run_pack(&_enc->opb,run_count);
flag=!flag;
run_count=1;
}
else run_count++;
}
}
}
}
}
}
/*Flush any trailing block coded run.*/
if(run_count>0)oc_block_run_pack(&_enc->opb,run_count);
}
}
static void oc_enc_mb_modes_pack(oc_enc_ctx *_enc){
const unsigned char *mode_codes;
const unsigned char *mode_bits;
const unsigned char *mode_ranks;
unsigned *coded_mbis;
size_t ncoded_mbis;
const signed char *mb_modes;
unsigned mbii;
int scheme;
int mb_mode;
scheme=_enc->chooser.scheme_list[0];
/*Encode the best scheme.*/
oggpackB_write(&_enc->opb,scheme,3);
/*If the chosen scheme is scheme 0, send the mode frequency ordering.*/
if(scheme==0){
for(mb_mode=0;mb_mode<OC_NMODES;mb_mode++){
oggpackB_write(&_enc->opb,_enc->chooser.scheme0_ranks[mb_mode],3);
}
}
mode_ranks=_enc->chooser.mode_ranks[scheme];
mode_bits=OC_MODE_BITS[scheme+1>>3];
mode_codes=OC_MODE_CODES[scheme+1>>3];
coded_mbis=_enc->coded_mbis;
ncoded_mbis=_enc->ncoded_mbis;
mb_modes=_enc->state.mb_modes;
for(mbii=0;mbii<ncoded_mbis;mbii++){
int rank;
rank=mode_ranks[mb_modes[coded_mbis[mbii]]];
oggpackB_write(&_enc->opb,mode_codes[rank],mode_bits[rank]);
}
}
static void oc_enc_mv_pack(oc_enc_ctx *_enc,int _mv_scheme,int _dx,int _dy){
oggpackB_write(&_enc->opb,
OC_MV_CODES[_mv_scheme][_dx+31],OC_MV_BITS[_mv_scheme][_dx+31]);
oggpackB_write(&_enc->opb,
OC_MV_CODES[_mv_scheme][_dy+31],OC_MV_BITS[_mv_scheme][_dy+31]);
}
static void oc_enc_mvs_pack(oc_enc_ctx *_enc){
const unsigned *coded_mbis;
size_t ncoded_mbis;
const oc_mb_map *mb_maps;
const signed char *mb_modes;
const oc_fragment *frags;
const oc_mv *frag_mvs;
unsigned mbii;
int mv_scheme;
/*Choose the coding scheme.*/
mv_scheme=_enc->mv_bits[1]<_enc->mv_bits[0];
oggpackB_write(&_enc->opb,mv_scheme,1);
/*Encode the motion vectors.
Macro blocks are iterated in Hilbert scan order, but the MVs within the
macro block are coded in raster order.*/
coded_mbis=_enc->coded_mbis;
ncoded_mbis=_enc->ncoded_mbis;
mb_modes=_enc->state.mb_modes;
mb_maps=(const oc_mb_map *)_enc->state.mb_maps;
frags=_enc->state.frags;
frag_mvs=(const oc_mv *)_enc->state.frag_mvs;
for(mbii=0;mbii<ncoded_mbis;mbii++){
ptrdiff_t fragi;
unsigned mbi;
int bi;
mbi=coded_mbis[mbii];
switch(mb_modes[mbi]){
case OC_MODE_INTER_MV:
case OC_MODE_GOLDEN_MV:{
for(bi=0;;bi++){
fragi=mb_maps[mbi][0][bi];
if(frags[fragi].coded){
oc_enc_mv_pack(_enc,mv_scheme,
frag_mvs[fragi][0],frag_mvs[fragi][1]);
/*Only code a single MV for this macro block.*/
break;
}
}
}break;
case OC_MODE_INTER_MV_FOUR:{
for(bi=0;bi<4;bi++){
fragi=mb_maps[mbi][0][bi];
if(frags[fragi].coded){
oc_enc_mv_pack(_enc,mv_scheme,
frag_mvs[fragi][0],frag_mvs[fragi][1]);
/*Keep coding all the MVs for this macro block.*/
}
}
}break;
}
}
}
static void oc_enc_block_qis_pack(oc_enc_ctx *_enc){
const oc_fragment *frags;
ptrdiff_t *coded_fragis;
ptrdiff_t ncoded_fragis;
ptrdiff_t fragii;
ptrdiff_t run_count;
ptrdiff_t nqi0;
int flag;
if(_enc->state.nqis<=1)return;
ncoded_fragis=_enc->state.ntotal_coded_fragis;
if(ncoded_fragis<=0)return;
coded_fragis=_enc->state.coded_fragis;
frags=_enc->state.frags;
flag=!!frags[coded_fragis[0]].qii;
oggpackB_write(&_enc->opb,flag,1);
nqi0=0;
for(fragii=0;fragii<ncoded_fragis;){
for(run_count=0;fragii<ncoded_fragis;fragii++){
if(!!frags[coded_fragis[fragii]].qii!=flag)break;
run_count++;
nqi0+=!flag;
}
oc_sb_run_pack(&_enc->opb,run_count,flag,fragii>=ncoded_fragis);
flag=!flag;
}
if(_enc->state.nqis<3||nqi0>=ncoded_fragis)return;
for(fragii=0;!frags[coded_fragis[fragii]].qii;fragii++);
flag=frags[coded_fragis[fragii]].qii-1;
oggpackB_write(&_enc->opb,flag,1);
while(fragii<ncoded_fragis){
for(run_count=0;fragii<ncoded_fragis;fragii++){
int qii;
qii=frags[coded_fragis[fragii]].qii;
if(!qii)continue;
if(qii-1!=flag)break;
run_count++;
}
oc_sb_run_pack(&_enc->opb,run_count,flag,fragii>=ncoded_fragis);
flag=!flag;
}
}
/*Counts the tokens of each type used for the given range of coefficient
indices in zig-zag order.
_zzi_start: The first zig-zag index to include.
_zzi_end: The first zig-zag index to not include.
_token_counts_y: Returns the token counts for the Y' plane.
_token_counts_c: Returns the token counts for the Cb and Cr planes.*/
static void oc_enc_count_tokens(oc_enc_ctx *_enc,int _zzi_start,int _zzi_end,
ptrdiff_t _token_counts_y[32],ptrdiff_t _token_counts_c[32]){
const unsigned char *dct_tokens;
ptrdiff_t ndct_tokens;
int pli;
int zzi;
ptrdiff_t ti;
memset(_token_counts_y,0,32*sizeof(*_token_counts_y));
memset(_token_counts_c,0,32*sizeof(*_token_counts_c));
for(zzi=_zzi_start;zzi<_zzi_end;zzi++){
dct_tokens=_enc->dct_tokens[0][zzi];
ndct_tokens=_enc->ndct_tokens[0][zzi];
for(ti=_enc->dct_token_offs[0][zzi];ti<ndct_tokens;ti++){
_token_counts_y[dct_tokens[ti]]++;
}
}
for(pli=1;pli<3;pli++){
for(zzi=_zzi_start;zzi<_zzi_end;zzi++){
dct_tokens=_enc->dct_tokens[pli][zzi];
ndct_tokens=_enc->ndct_tokens[pli][zzi];
for(ti=_enc->dct_token_offs[pli][zzi];ti<ndct_tokens;ti++){
_token_counts_c[dct_tokens[ti]]++;
}
}
}
}
/*Computes the number of bits used for each of the potential Huffman code for
the given list of token counts.
The bits are added to whatever the current bit counts are.*/
static void oc_enc_count_bits(oc_enc_ctx *_enc,int _hgi,
const ptrdiff_t _token_counts[32],size_t _bit_counts[16]){
int huffi;
int huff_offs;
int token;
huff_offs=_hgi<<4;
for(huffi=0;huffi<16;huffi++){
for(token=0;token<32;token++){
_bit_counts[huffi]+=
_token_counts[token]*_enc->huff_codes[huffi+huff_offs][token].nbits;
}
}
}
/*Returns the Huffman index using the fewest number of bits.*/
static int oc_select_huff_idx(size_t _bit_counts[16]){
int best_huffi;
int huffi;
best_huffi=0;
for(huffi=1;huffi<16;huffi++)if(_bit_counts[huffi]<_bit_counts[best_huffi]){
best_huffi=huffi;
}
return best_huffi;
}
static void oc_enc_huff_group_pack(oc_enc_ctx *_enc,
int _zzi_start,int _zzi_end,const int _huff_idxs[2]){
int zzi;
for(zzi=_zzi_start;zzi<_zzi_end;zzi++){
int pli;
for(pli=0;pli<3;pli++){
const unsigned char *dct_tokens;
const ogg_uint16_t *extra_bits;
ptrdiff_t ndct_tokens;
const th_huff_code *huff_codes;
ptrdiff_t ti;
dct_tokens=_enc->dct_tokens[pli][zzi];
extra_bits=_enc->extra_bits[pli][zzi];
ndct_tokens=_enc->ndct_tokens[pli][zzi];
huff_codes=_enc->huff_codes[_huff_idxs[pli+1>>1]];
for(ti=_enc->dct_token_offs[pli][zzi];ti<ndct_tokens;ti++){
int token;
int neb;
token=dct_tokens[ti];
oggpackB_write(&_enc->opb,huff_codes[token].pattern,
huff_codes[token].nbits);
neb=OC_DCT_TOKEN_EXTRA_BITS[token];
if(neb)oggpackB_write(&_enc->opb,extra_bits[ti],neb);
}
}
}
}
static void oc_enc_residual_tokens_pack(oc_enc_ctx *_enc){
static const unsigned char OC_HUFF_GROUP_MIN[6]={0,1,6,15,28,64};
static const unsigned char *OC_HUFF_GROUP_MAX=OC_HUFF_GROUP_MIN+1;
ptrdiff_t token_counts_y[32];
ptrdiff_t token_counts_c[32];
size_t bits_y[16];
size_t bits_c[16];
int huff_idxs[2];
int frame_type;
int hgi;
frame_type=_enc->state.frame_type;
/*Choose which Huffman tables to use for the DC token list.*/
oc_enc_count_tokens(_enc,0,1,token_counts_y,token_counts_c);
memset(bits_y,0,sizeof(bits_y));
memset(bits_c,0,sizeof(bits_c));
oc_enc_count_bits(_enc,0,token_counts_y,bits_y);
oc_enc_count_bits(_enc,0,token_counts_c,bits_c);
huff_idxs[0]=oc_select_huff_idx(bits_y);
huff_idxs[1]=oc_select_huff_idx(bits_c);
/*Write the DC token list with the chosen tables.*/
oggpackB_write(&_enc->opb,huff_idxs[0],4);
oggpackB_write(&_enc->opb,huff_idxs[1],4);
_enc->huff_idxs[frame_type][0][0]=(unsigned char)huff_idxs[0];
_enc->huff_idxs[frame_type][0][1]=(unsigned char)huff_idxs[1];
oc_enc_huff_group_pack(_enc,0,1,huff_idxs);
/*Choose which Huffman tables to use for the AC token lists.*/
memset(bits_y,0,sizeof(bits_y));
memset(bits_c,0,sizeof(bits_c));
for(hgi=1;hgi<5;hgi++){
oc_enc_count_tokens(_enc,OC_HUFF_GROUP_MIN[hgi],OC_HUFF_GROUP_MAX[hgi],
token_counts_y,token_counts_c);
oc_enc_count_bits(_enc,hgi,token_counts_y,bits_y);
oc_enc_count_bits(_enc,hgi,token_counts_c,bits_c);
}
huff_idxs[0]=oc_select_huff_idx(bits_y);
huff_idxs[1]=oc_select_huff_idx(bits_c);
/*Write the AC token lists using the chosen tables.*/
oggpackB_write(&_enc->opb,huff_idxs[0],4);
oggpackB_write(&_enc->opb,huff_idxs[1],4);
_enc->huff_idxs[frame_type][1][0]=(unsigned char)huff_idxs[0];
_enc->huff_idxs[frame_type][1][1]=(unsigned char)huff_idxs[1];
for(hgi=1;hgi<5;hgi++){
huff_idxs[0]+=16;
huff_idxs[1]+=16;
oc_enc_huff_group_pack(_enc,
OC_HUFF_GROUP_MIN[hgi],OC_HUFF_GROUP_MAX[hgi],huff_idxs);
}
}
static void oc_enc_frame_pack(oc_enc_ctx *_enc){
oggpackB_reset(&_enc->opb);
/*Only proceed if we have some coded blocks.
If there are no coded blocks, we can drop this frame simply by emitting a
0 byte packet.*/
if(_enc->state.ntotal_coded_fragis>0){
oc_enc_frame_header_pack(_enc);
if(_enc->state.frame_type==OC_INTER_FRAME){
/*Coded block flags, MB modes, and MVs are only needed for delta frames.*/
oc_enc_coded_flags_pack(_enc);
oc_enc_mb_modes_pack(_enc);
oc_enc_mvs_pack(_enc);
}
oc_enc_block_qis_pack(_enc);
oc_enc_tokenize_finish(_enc);
oc_enc_residual_tokens_pack(_enc);
}
/*Success: Mark the packet as ready to be flushed.*/
_enc->packet_state=OC_PACKET_READY;
#if defined(OC_COLLECT_METRICS)
oc_enc_mode_metrics_collect(_enc);
#endif
}
void oc_enc_vtable_init_c(oc_enc_ctx *_enc){
/*The implementations prefixed with oc_enc_ are encoder-specific.
The rest we re-use from the decoder.*/
_enc->opt_vtable.frag_sad=oc_enc_frag_sad_c;
_enc->opt_vtable.frag_sad_thresh=oc_enc_frag_sad_thresh_c;
_enc->opt_vtable.frag_sad2_thresh=oc_enc_frag_sad2_thresh_c;
_enc->opt_vtable.frag_satd_thresh=oc_enc_frag_satd_thresh_c;
_enc->opt_vtable.frag_satd2_thresh=oc_enc_frag_satd2_thresh_c;
_enc->opt_vtable.frag_intra_satd=oc_enc_frag_intra_satd_c;
_enc->opt_vtable.frag_sub=oc_enc_frag_sub_c;
_enc->opt_vtable.frag_sub_128=oc_enc_frag_sub_128_c;
_enc->opt_vtable.frag_copy2=oc_enc_frag_copy2_c;
_enc->opt_vtable.frag_recon_intra=oc_frag_recon_intra_c;
_enc->opt_vtable.frag_recon_inter=oc_frag_recon_inter_c;
_enc->opt_vtable.fdct8x8=oc_enc_fdct8x8_c;
}
/*Initialize the macro block neighbor lists for MC analysis.
This assumes that the entire mb_info memory region has been initialized with
zeros.*/
static void oc_enc_mb_info_init(oc_enc_ctx *_enc){
oc_mb_enc_info *embs;
const signed char *mb_modes;
unsigned nhsbs;
unsigned nvsbs;
unsigned nhmbs;
unsigned nvmbs;
unsigned sby;
mb_modes=_enc->state.mb_modes;
embs=_enc->mb_info;
nhsbs=_enc->state.fplanes[0].nhsbs;
nvsbs=_enc->state.fplanes[0].nvsbs;
nhmbs=_enc->state.nhmbs;
nvmbs=_enc->state.nvmbs;
for(sby=0;sby<nvsbs;sby++){
unsigned sbx;
for(sbx=0;sbx<nhsbs;sbx++){
int quadi;
for(quadi=0;quadi<4;quadi++){
/*Because of the Hilbert curve ordering the macro blocks are
visited in, the available neighbors change depending on where in
a super block the macro block is located.
Only the first three vectors are used in the median calculation
for the optimal predictor, and so the most important should be
listed first.
Additional vectors are used, so there will always be at least 3,
except for in the upper-left most macro block.*/
/*The number of current neighbors for each macro block position.*/
static const unsigned char NCNEIGHBORS[4]={4,3,2,4};
/*The offset of each current neighbor in the X direction.*/
static const signed char CDX[4][4]={
{-1,0,1,-1},
{-1,0,-1,},
{-1,-1},
{-1,0,0,1}
};
/*The offset of each current neighbor in the Y direction.*/
static const signed char CDY[4][4]={
{0,-1,-1,-1},
{0,-1,-1},
{0,-1},
{0,-1,1,-1}
};
/*The offset of each previous neighbor in the X direction.*/
static const signed char PDX[4]={-1,0,1,0};
/*The offset of each previous neighbor in the Y direction.*/
static const signed char PDY[4]={0,-1,0,1};
unsigned mbi;
int mbx;
int mby;
unsigned nmbi;
int nmbx;
int nmby;
int ni;
mbi=(sby*nhsbs+sbx<<2)+quadi;
if(mb_modes[mbi]==OC_MODE_INVALID)continue;
mbx=2*sbx+(quadi>>1);
mby=2*sby+(quadi+1>>1&1);
/*Fill in the neighbors with current motion vectors available.*/
for(ni=0;ni<NCNEIGHBORS[quadi];ni++){
nmbx=mbx+CDX[quadi][ni];
nmby=mby+CDY[quadi][ni];
if(nmbx<0||nmbx>=nhmbs||nmby<0||nmby>=nvmbs)continue;
nmbi=(nmby&~1)*nhmbs+((nmbx&~1)<<1)+OC_MB_MAP[nmby&1][nmbx&1];
if(mb_modes[nmbi]==OC_MODE_INVALID)continue;
embs[mbi].cneighbors[embs[mbi].ncneighbors++]=nmbi;
}
/*Fill in the neighbors with previous motion vectors available.*/
for(ni=0;ni<4;ni++){
nmbx=mbx+PDX[ni];
nmby=mby+PDY[ni];
if(nmbx<0||nmbx>=nhmbs||nmby<0||nmby>=nvmbs)continue;
nmbi=(nmby&~1)*nhmbs+((nmbx&~1)<<1)+OC_MB_MAP[nmby&1][nmbx&1];
if(mb_modes[nmbi]==OC_MODE_INVALID)continue;
embs[mbi].pneighbors[embs[mbi].npneighbors++]=nmbi;
}
}
}
}
}
static int oc_enc_set_huffman_codes(oc_enc_ctx *_enc,
const th_huff_code _codes[TH_NHUFFMAN_TABLES][TH_NDCT_TOKENS]){
int ret;
if(_enc==NULL)return TH_EFAULT;
if(_enc->packet_state>OC_PACKET_SETUP_HDR)return TH_EINVAL;
if(_codes==NULL)_codes=TH_VP31_HUFF_CODES;
/*Validate the codes.*/
oggpackB_reset(&_enc->opb);
ret=oc_huff_codes_pack(&_enc->opb,_codes);