forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
analyze.c
2709 lines (2603 loc) · 92.5 KB
/
analyze.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: mode selection code
last mod: $Id$
********************************************************************/
#include <limits.h>
#include <string.h>
#include "encint.h"
#include "modedec.h"
typedef struct oc_fr_state oc_fr_state;
typedef struct oc_qii_state oc_qii_state;
typedef struct oc_enc_pipeline_state oc_enc_pipeline_state;
typedef struct oc_rd_metric oc_rd_metric;
typedef struct oc_mode_choice oc_mode_choice;
/*There are 8 possible schemes used to encode macro block modes.
Schemes 0-6 use a maximally-skewed Huffman code to code each of the modes.
The same set of Huffman codes is used for each of these 7 schemes, but the
mode assigned to each codeword varies.
Scheme 0 writes a custom mapping from codeword to MB mode to the bitstream,
while schemes 1-6 have a fixed mapping.
Scheme 7 just encodes each mode directly in 3 bits.*/
/*The mode orderings for the various mode coding schemes.
Scheme 0 uses a custom alphabet, which is not stored in this table.
This is the inverse of the equivalent table OC_MODE_ALPHABETS in the
decoder.*/
static const unsigned char OC_MODE_RANKS[7][OC_NMODES]={
/*Last MV dominates.*/
/*L P M N I G GM 4*/
{3,4,2,0,1,5,6,7},
/*L P N M I G GM 4*/
{2,4,3,0,1,5,6,7},
/*L M P N I G GM 4*/
{3,4,1,0,2,5,6,7},
/*L M N P I G GM 4*/
{2,4,1,0,3,5,6,7},
/*No MV dominates.*/
/*N L P M I G GM 4*/
{0,4,3,1,2,5,6,7},
/*N G L P M I GM 4*/
{0,5,4,2,3,1,6,7},
/*Default ordering.*/
/*N I M L P G GM 4*/
{0,1,2,3,4,5,6,7}
};
/*Initialize the mode scheme chooser.
This need only be called once per encoder.*/
void oc_mode_scheme_chooser_init(oc_mode_scheme_chooser *_chooser){
int si;
_chooser->mode_ranks[0]=_chooser->scheme0_ranks;
for(si=1;si<8;si++)_chooser->mode_ranks[si]=OC_MODE_RANKS[si-1];
}
/*Reset the mode scheme chooser.
This needs to be called once for each frame, including the first.*/
static void oc_mode_scheme_chooser_reset(oc_mode_scheme_chooser *_chooser){
int si;
memset(_chooser->mode_counts,0,OC_NMODES*sizeof(*_chooser->mode_counts));
/*Scheme 0 starts with 24 bits to store the mode list in.*/
_chooser->scheme_bits[0]=24;
memset(_chooser->scheme_bits+1,0,7*sizeof(*_chooser->scheme_bits));
for(si=0;si<8;si++){
/*Scheme 7 should always start first, and scheme 0 should always start
last.*/
_chooser->scheme_list[si]=7-si;
_chooser->scheme0_list[si]=_chooser->scheme0_ranks[si]=si;
}
}
/*This is the real purpose of this data structure: not actually selecting a
mode scheme, but estimating the cost of coding a given mode given all the
modes selected so far.
This is done via opportunity cost: the cost is defined as the number of bits
required to encode all the modes selected so far including the current one
using the best possible scheme, minus the number of bits required to encode
all the modes selected so far not including the current one using the best
possible scheme.
The computational expense of doing this probably makes it overkill.
Just be happy we take a greedy approach instead of trying to solve the
global mode-selection problem (which is NP-hard).
_mb_mode: The mode to determine the cost of.
Return: The number of bits required to code this mode.*/
static int oc_mode_scheme_chooser_cost(oc_mode_scheme_chooser *_chooser,
int _mb_mode){
int scheme0;
int scheme1;
int best_bits;
int mode_bits;
int si;
int scheme_bits;
scheme0=_chooser->scheme_list[0];
scheme1=_chooser->scheme_list[1];
best_bits=_chooser->scheme_bits[scheme0];
mode_bits=OC_MODE_BITS[scheme0+1>>3][_chooser->mode_ranks[scheme0][_mb_mode]];
/*Typical case: If the difference between the best scheme and the next best
is greater than 6 bits, then adding just one mode cannot change which
scheme we use.*/
if(_chooser->scheme_bits[scheme1]-best_bits>6)return mode_bits;
/*Otherwise, check to see if adding this mode selects a different scheme as
the best.*/
si=1;
best_bits+=mode_bits;
do{
/*For any scheme except 0, we can just use the bit cost of the mode's rank
in that scheme.*/
if(scheme1!=0){
scheme_bits=_chooser->scheme_bits[scheme1]+
OC_MODE_BITS[scheme1+1>>3][_chooser->mode_ranks[scheme1][_mb_mode]];
}
else{
int ri;
/*For scheme 0, incrementing the mode count could potentially change the
mode's rank.
Find the index where the mode would be moved to in the optimal list,
and use its bit cost instead of the one for the mode's current
position in the list.*/
/*We don't recompute scheme bits; this is computing opportunity cost, not
an update.*/
for(ri=_chooser->scheme0_ranks[_mb_mode];ri>0&&
_chooser->mode_counts[_mb_mode]>=
_chooser->mode_counts[_chooser->scheme0_list[ri-1]];ri--);
scheme_bits=_chooser->scheme_bits[0]+OC_MODE_BITS[0][ri];
}
if(scheme_bits<best_bits)best_bits=scheme_bits;
if(++si>=8)break;
scheme1=_chooser->scheme_list[si];
}
while(_chooser->scheme_bits[scheme1]-_chooser->scheme_bits[scheme0]<=6);
return best_bits-_chooser->scheme_bits[scheme0];
}
/*Incrementally update the mode counts and per-scheme bit counts and re-order
the scheme lists once a mode has been selected.
_mb_mode: The mode that was chosen.*/
static void oc_mode_scheme_chooser_update(oc_mode_scheme_chooser *_chooser,
int _mb_mode){
int ri;
int si;
_chooser->mode_counts[_mb_mode]++;
/*Re-order the scheme0 mode list if necessary.*/
for(ri=_chooser->scheme0_ranks[_mb_mode];ri>0;ri--){
int pmode;
pmode=_chooser->scheme0_list[ri-1];
if(_chooser->mode_counts[pmode]>=_chooser->mode_counts[_mb_mode])break;
/*Reorder the mode ranking.*/
_chooser->scheme0_ranks[pmode]++;
_chooser->scheme0_list[ri]=pmode;
}
_chooser->scheme0_ranks[_mb_mode]=ri;
_chooser->scheme0_list[ri]=_mb_mode;
/*Now add the bit cost for the mode to each scheme.*/
for(si=0;si<8;si++){
_chooser->scheme_bits[si]+=
OC_MODE_BITS[si+1>>3][_chooser->mode_ranks[si][_mb_mode]];
}
/*Finally, re-order the list of schemes.*/
for(si=1;si<8;si++){
int sj;
int scheme0;
int bits0;
sj=si;
scheme0=_chooser->scheme_list[si];
bits0=_chooser->scheme_bits[scheme0];
do{
int scheme1;
scheme1=_chooser->scheme_list[sj-1];
if(bits0>=_chooser->scheme_bits[scheme1])break;
_chooser->scheme_list[sj]=scheme1;
}
while(--sj>0);
_chooser->scheme_list[sj]=scheme0;
}
}
/*The number of bits required to encode a super block run.
_run_count: The desired run count; must be positive and less than 4130.*/
static int oc_sb_run_bits(int _run_count){
int i;
for(i=0;_run_count>=OC_SB_RUN_VAL_MIN[i+1];i++);
return OC_SB_RUN_CODE_NBITS[i];
}
/*The number of bits required to encode a block run.
_run_count: The desired run count; must be positive and less than 30.*/
static int oc_block_run_bits(int _run_count){
return OC_BLOCK_RUN_CODE_NBITS[_run_count-1];
}
/*State to track coded block flags and their bit cost.*/
struct oc_fr_state{
ptrdiff_t bits;
unsigned sb_partial_count:16;
unsigned sb_full_count:16;
unsigned b_coded_count_prev:8;
unsigned b_coded_count:8;
unsigned b_count:8;
signed int sb_partial:2;
signed int sb_full:2;
signed int b_coded_prev:2;
signed int b_coded:2;
};
static void oc_fr_state_init(oc_fr_state *_fr){
_fr->bits=0;
_fr->sb_partial_count=0;
_fr->sb_full_count=0;
_fr->b_coded_count_prev=0;
_fr->b_coded_count=0;
_fr->b_count=0;
_fr->sb_partial=-1;
_fr->sb_full=-1;
_fr->b_coded_prev=-1;
_fr->b_coded=-1;
}
static void oc_fr_state_advance_sb(oc_fr_state *_fr,
int _sb_partial,int _sb_full){
ptrdiff_t bits;
int sb_partial_count;
int sb_full_count;
bits=_fr->bits;
/*Extend the sb_partial run, or start a new one.*/
sb_partial_count=_fr->sb_partial;
if(_fr->sb_partial==_sb_partial){
if(sb_partial_count>=4129){
bits++;
sb_partial_count=0;
}
else bits-=oc_sb_run_bits(sb_partial_count);
}
else sb_partial_count=0;
sb_partial_count++;
bits+=oc_sb_run_bits(sb_partial_count);
if(!_sb_partial){
/*Extend the sb_full run, or start a new one.*/
sb_full_count=_fr->sb_full_count;
if(_fr->sb_full==_sb_full){
if(sb_full_count>=4129){
bits++;
sb_full_count=0;
}
else bits-=oc_sb_run_bits(sb_full_count);
}
else sb_full_count=0;
sb_full_count++;
bits+=oc_sb_run_bits(sb_full_count);
_fr->sb_full=_sb_full;
_fr->sb_full_count=sb_full_count;
}
_fr->bits=bits;
_fr->sb_partial=_sb_partial;
_fr->sb_partial_count=sb_partial_count;
}
/*Flush any outstanding block flags for a SB (e.g., one with fewer than 16
blocks).*/
static void oc_fr_state_flush_sb(oc_fr_state *_fr){
ptrdiff_t bits;
int sb_partial;
int sb_full=sb_full;
int b_coded_count;
int b_coded;
int b_count;
b_count=_fr->b_count;
if(b_count>0){
bits=_fr->bits;
b_coded=_fr->b_coded;
b_coded_count=_fr->b_coded_count;
if(b_coded_count>=b_count){
/*This SB was fully coded/uncoded; roll back the partial block flags.*/
bits-=oc_block_run_bits(b_coded_count);
if(b_coded_count>b_count)bits+=oc_block_run_bits(b_coded_count-b_count);
sb_partial=0;
sb_full=b_coded;
b_coded=_fr->b_coded_prev;
b_coded_count=_fr->b_coded_count_prev;
}
else{
/*It was partially coded.*/
sb_partial=1;
/*sb_full is unused.*/
}
_fr->bits=bits;
_fr->b_coded_count=b_coded_count;
_fr->b_coded_count_prev=b_coded_count;
_fr->b_count=0;
_fr->b_coded=b_coded;
_fr->b_coded_prev=b_coded;
oc_fr_state_advance_sb(_fr,sb_partial,sb_full);
}
}
static void oc_fr_state_advance_block(oc_fr_state *_fr,int _b_coded){
ptrdiff_t bits;
int b_coded_count;
int b_count;
int sb_partial;
int sb_full=sb_full;
bits=_fr->bits;
/*Extend the b_coded run, or start a new one.*/
b_coded_count=_fr->b_coded_count;
if(_fr->b_coded==_b_coded)bits-=oc_block_run_bits(b_coded_count);
else b_coded_count=0;
b_coded_count++;
b_count=_fr->b_count+1;
if(b_count>=16){
/*We finished a superblock.*/
if(b_coded_count>=16){
/*It was fully coded/uncoded; roll back the partial block flags.*/
if(b_coded_count>16)bits+=oc_block_run_bits(b_coded_count-16);
sb_partial=0;
sb_full=_b_coded;
_b_coded=_fr->b_coded_prev;
b_coded_count=_fr->b_coded_count_prev;
}
else{
bits+=oc_block_run_bits(b_coded_count);
/*It was partially coded.*/
sb_partial=1;
/*sb_full is unused.*/
}
_fr->bits=bits;
_fr->b_coded_count=b_coded_count;
_fr->b_coded_count_prev=b_coded_count;
_fr->b_count=0;
_fr->b_coded=_b_coded;
_fr->b_coded_prev=_b_coded;
oc_fr_state_advance_sb(_fr,sb_partial,sb_full);
}
else{
bits+=oc_block_run_bits(b_coded_count);
_fr->bits=bits;
_fr->b_coded_count=b_coded_count;
_fr->b_count=b_count;
_fr->b_coded=_b_coded;
}
}
static void oc_fr_skip_block(oc_fr_state *_fr){
oc_fr_state_advance_block(_fr,0);
}
static void oc_fr_code_block(oc_fr_state *_fr){
oc_fr_state_advance_block(_fr,1);
}
static int oc_fr_cost1(const oc_fr_state *_fr){
oc_fr_state tmp;
ptrdiff_t bits;
*&tmp=*_fr;
oc_fr_skip_block(&tmp);
bits=tmp.bits;
*&tmp=*_fr;
oc_fr_code_block(&tmp);
return (int)(tmp.bits-bits);
}
static int oc_fr_cost4(const oc_fr_state *_pre,const oc_fr_state *_post){
oc_fr_state tmp;
*&tmp=*_pre;
oc_fr_skip_block(&tmp);
oc_fr_skip_block(&tmp);
oc_fr_skip_block(&tmp);
oc_fr_skip_block(&tmp);
return (int)(_post->bits-tmp.bits);
}
struct oc_qii_state{
ptrdiff_t bits;
unsigned qi01_count:14;
signed int qi01:2;
unsigned qi12_count:14;
signed int qi12:2;
};
static void oc_qii_state_init(oc_qii_state *_qs){
_qs->bits=0;
_qs->qi01_count=0;
_qs->qi01=-1;
_qs->qi12_count=0;
_qs->qi12=-1;
}
static void oc_qii_state_advance(oc_qii_state *_qd,
const oc_qii_state *_qs,int _qii){
ptrdiff_t bits;
int qi01;
int qi01_count;
int qi12;
int qi12_count;
bits=_qs->bits;
qi01=_qii+1>>1;
qi01_count=_qs->qi01_count;
if(qi01==_qs->qi01){
if(qi01_count>=4129){
bits++;
qi01_count=0;
}
else bits-=oc_sb_run_bits(qi01_count);
}
else qi01_count=0;
qi01_count++;
bits+=oc_sb_run_bits(qi01_count);
qi12_count=_qs->qi12_count;
if(_qii){
qi12=_qii>>1;
if(qi12==_qs->qi12){
if(qi12_count>=4129){
bits++;
qi12_count=0;
}
else bits-=oc_sb_run_bits(qi12_count);
}
else qi12_count=0;
qi12_count++;
bits+=oc_sb_run_bits(qi12_count);
}
else qi12=_qs->qi12;
_qd->bits=bits;
_qd->qi01=qi01;
_qd->qi01_count=qi01_count;
_qd->qi12=qi12;
_qd->qi12_count=qi12_count;
}
/*Temporary encoder state for the analysis pipeline.*/
struct oc_enc_pipeline_state{
int bounding_values[256];
oc_fr_state fr[3];
oc_qii_state qs[3];
/*Condensed dequantization tables.*/
const ogg_uint16_t *dequant[3][3][2];
/*Condensed quantization tables.*/
const oc_iquant *enquant[3][3][2];
/*Skip SSD storage for the current MCU in each plane.*/
unsigned *skip_ssd[3];
/*Coded/uncoded fragment lists for each plane for the current MCU.*/
ptrdiff_t *coded_fragis[3];
ptrdiff_t *uncoded_fragis[3];
ptrdiff_t ncoded_fragis[3];
ptrdiff_t nuncoded_fragis[3];
/*The starting fragment for the current MCU in each plane.*/
ptrdiff_t froffset[3];
/*The starting row for the current MCU in each plane.*/
int fragy0[3];
/*The ending row for the current MCU in each plane.*/
int fragy_end[3];
/*The starting superblock for the current MCU in each plane.*/
unsigned sbi0[3];
/*The ending superblock for the current MCU in each plane.*/
unsigned sbi_end[3];
/*The number of tokens for zzi=1 for each color plane.*/
int ndct_tokens1[3];
/*The outstanding eob_run count for zzi=1 for each color plane.*/
int eob_run1[3];
/*Whether or not the loop filter is enabled.*/
int loop_filter;
};
static void oc_enc_pipeline_init(oc_enc_ctx *_enc,oc_enc_pipeline_state *_pipe){
ptrdiff_t *coded_fragis;
unsigned mcu_nvsbs;
ptrdiff_t mcu_nfrags;
int hdec;
int vdec;
int pli;
int qii;
int qti;
/*Initialize the per-plane coded block flag trackers.
These are used for bit-estimation purposes only; the real flag bits span
all three planes, so we can't compute them in parallel.*/
for(pli=0;pli<3;pli++)oc_fr_state_init(_pipe->fr+pli);
for(pli=0;pli<3;pli++)oc_qii_state_init(_pipe->qs+pli);
/*Set up the per-plane skip SSD storage pointers.*/
mcu_nvsbs=_enc->mcu_nvsbs;
mcu_nfrags=mcu_nvsbs*_enc->state.fplanes[0].nhsbs*16;
hdec=!(_enc->state.info.pixel_fmt&1);
vdec=!(_enc->state.info.pixel_fmt&2);
_pipe->skip_ssd[0]=_enc->mcu_skip_ssd;
_pipe->skip_ssd[1]=_pipe->skip_ssd[0]+mcu_nfrags;
_pipe->skip_ssd[2]=_pipe->skip_ssd[1]+(mcu_nfrags>>hdec+vdec);
/*Set up per-plane pointers to the coded and uncoded fragments lists.
Unlike the decoder, each planes' coded and uncoded fragment list is kept
separate during the analysis stage; we only make the coded list for all
three planes contiguous right before the final packet is output
(destroying the uncoded lists, which are no longer needed).*/
coded_fragis=_enc->state.coded_fragis;
for(pli=0;pli<3;pli++){
_pipe->coded_fragis[pli]=coded_fragis;
coded_fragis+=_enc->state.fplanes[pli].nfrags;
_pipe->uncoded_fragis[pli]=coded_fragis;
}
memset(_pipe->ncoded_fragis,0,sizeof(_pipe->ncoded_fragis));
memset(_pipe->nuncoded_fragis,0,sizeof(_pipe->nuncoded_fragis));
/*Set up condensed quantizer tables.*/
for(pli=0;pli<3;pli++){
for(qii=0;qii<_enc->state.nqis;qii++){
int qi;
qi=_enc->state.qis[qii];
for(qti=0;qti<2;qti++){
_pipe->dequant[pli][qii][qti]=_enc->state.dequant_tables[qi][pli][qti];
_pipe->enquant[pli][qii][qti]=_enc->enquant_tables[qi][pli][qti];
}
}
}
/*Initialize the tokenization state.*/
for(pli=0;pli<3;pli++){
_pipe->ndct_tokens1[pli]=0;
_pipe->eob_run1[pli]=0;
}
/*Initialize the bounding value array for the loop filter.*/
_pipe->loop_filter=!oc_state_loop_filter_init(&_enc->state,
_pipe->bounding_values);
}
/*Sets the current MCU stripe to super block row _sby.
Return: A non-zero value if this was the last MCU.*/
static int oc_enc_pipeline_set_stripe(oc_enc_ctx *_enc,
oc_enc_pipeline_state *_pipe,int _sby){
const oc_fragment_plane *fplane;
unsigned mcu_nvsbs;
int sby_end;
int notdone;
int vdec;
int pli;
mcu_nvsbs=_enc->mcu_nvsbs;
sby_end=_enc->state.fplanes[0].nvsbs;
notdone=_sby+mcu_nvsbs<sby_end;
if(notdone)sby_end=_sby+mcu_nvsbs;
vdec=0;
for(pli=0;pli<3;pli++){
fplane=_enc->state.fplanes+pli;
_pipe->sbi0[pli]=fplane->sboffset+(_sby>>vdec)*fplane->nhsbs;
_pipe->fragy0[pli]=_sby<<2-vdec;
_pipe->froffset[pli]=fplane->froffset
+_pipe->fragy0[pli]*(ptrdiff_t)fplane->nhfrags;
if(notdone){
_pipe->sbi_end[pli]=fplane->sboffset+(sby_end>>vdec)*fplane->nhsbs;
_pipe->fragy_end[pli]=sby_end<<2-vdec;
}
else{
_pipe->sbi_end[pli]=fplane->sboffset+fplane->nsbs;
_pipe->fragy_end[pli]=fplane->nvfrags;
}
vdec=!(_enc->state.info.pixel_fmt&2);
}
return notdone;
}
static void oc_enc_pipeline_finish_mcu_plane(oc_enc_ctx *_enc,
oc_enc_pipeline_state *_pipe,int _pli,int _sdelay,int _edelay){
int refi;
/*Copy over all the uncoded fragments from this plane and advance the uncoded
fragment list.*/
_pipe->uncoded_fragis[_pli]-=_pipe->nuncoded_fragis[_pli];
oc_state_frag_copy_list(&_enc->state,_pipe->uncoded_fragis[_pli],
_pipe->nuncoded_fragis[_pli],OC_FRAME_SELF,OC_FRAME_PREV,_pli);
_pipe->nuncoded_fragis[_pli]=0;
/*Perform DC prediction.*/
oc_enc_pred_dc_frag_rows(_enc,_pli,
_pipe->fragy0[_pli],_pipe->fragy_end[_pli]);
/*Finish DC tokenization.*/
oc_enc_tokenize_dc_frag_list(_enc,_pli,
_pipe->coded_fragis[_pli],_pipe->ncoded_fragis[_pli],
_pipe->ndct_tokens1[_pli],_pipe->eob_run1[_pli]);
_pipe->ndct_tokens1[_pli]=_enc->ndct_tokens[_pli][1];
_pipe->eob_run1[_pli]=_enc->eob_run[_pli][1];
/*And advance the coded fragment list.*/
_enc->state.ncoded_fragis[_pli]+=_pipe->ncoded_fragis[_pli];
_pipe->coded_fragis[_pli]+=_pipe->ncoded_fragis[_pli];
_pipe->ncoded_fragis[_pli]=0;
/*Apply the loop filter if necessary.*/
refi=_enc->state.ref_frame_idx[OC_FRAME_SELF];
if(_pipe->loop_filter){
oc_state_loop_filter_frag_rows(&_enc->state,_pipe->bounding_values,
refi,_pli,_pipe->fragy0[_pli]-_sdelay,_pipe->fragy_end[_pli]-_edelay);
}
else _sdelay=_edelay=0;
/*To fill borders, we have an additional two pixel delay, since a fragment
in the next row could filter its top edge, using two pixels from a
fragment in this row.
But there's no reason to delay a full fragment between the two.*/
oc_state_borders_fill_rows(&_enc->state,refi,_pli,
(_pipe->fragy0[_pli]-_sdelay<<3)-(_sdelay<<1),
(_pipe->fragy_end[_pli]-_edelay<<3)-(_edelay<<1));
}
/*Cost information about the coded blocks in a MB.*/
struct oc_rd_metric{
int uncoded_ac_ssd;
int coded_ac_ssd;
int ac_bits;
int dc_flag;
};
static int oc_enc_block_transform_quantize(oc_enc_ctx *_enc,
oc_enc_pipeline_state *_pipe,int _pli,ptrdiff_t _fragi,int _overhead_bits,
oc_rd_metric *_mo,oc_token_checkpoint **_stack){
OC_ALIGN16(ogg_int16_t dct[64]);
OC_ALIGN16(ogg_int16_t data[64]);
ogg_uint16_t dc_dequant;
const ogg_uint16_t *dequant;
const oc_iquant *enquant;
ptrdiff_t frag_offs;
int ystride;
const unsigned char *src;
const unsigned char *ref;
unsigned char *dst;
int frame_type;
int nonzero;
unsigned uncoded_ssd;
unsigned coded_ssd;
int coded_dc;
oc_token_checkpoint *checkpoint;
oc_fragment *frags;
int mb_mode;
int mv_offs[2];
int nmv_offs;
int ac_bits;
int borderi;
int qti;
int qii;
int pi;
int zzi;
int v;
int val;
int d;
int s;
int dc;
frags=_enc->state.frags;
frag_offs=_enc->state.frag_buf_offs[_fragi];
ystride=_enc->state.ref_ystride[_pli];
src=_enc->state.ref_frame_data[OC_FRAME_IO]+frag_offs;
borderi=frags[_fragi].borderi;
qii=frags[_fragi].qii;
if(qii&~3){
#if !defined(OC_COLLECT_METRICS)
if(_enc->sp_level>=OC_SP_LEVEL_EARLY_SKIP){
/*Enable early skip detection.*/
frags[_fragi].coded=0;
return 0;
}
#endif
/*Try and code this block anyway.*/
qii&=3;
frags[_fragi].qii=qii;
}
mb_mode=frags[_fragi].mb_mode;
ref=_enc->state.ref_frame_data[
_enc->state.ref_frame_idx[OC_FRAME_FOR_MODE(mb_mode)]]+frag_offs;
dst=_enc->state.ref_frame_data[_enc->state.ref_frame_idx[OC_FRAME_SELF]]
+frag_offs;
/*Motion compensation:*/
switch(mb_mode){
case OC_MODE_INTRA:{
nmv_offs=0;
oc_enc_frag_sub_128(_enc,data,src,ystride);
}break;
case OC_MODE_GOLDEN_NOMV:
case OC_MODE_INTER_NOMV:{
nmv_offs=1;
mv_offs[0]=0;
oc_enc_frag_sub(_enc,data,src,ref,ystride);
}break;
default:{
const oc_mv *frag_mvs;
frag_mvs=(const oc_mv *)_enc->state.frag_mvs;
nmv_offs=oc_state_get_mv_offsets(&_enc->state,mv_offs,_pli,
frag_mvs[_fragi][0],frag_mvs[_fragi][1]);
if(nmv_offs>1){
oc_enc_frag_copy2(_enc,dst,
ref+mv_offs[0],ref+mv_offs[1],ystride);
oc_enc_frag_sub(_enc,data,src,dst,ystride);
}
else oc_enc_frag_sub(_enc,data,src,ref+mv_offs[0],ystride);
}break;
}
#if defined(OC_COLLECT_METRICS)
{
unsigned satd;
switch(nmv_offs){
case 0:satd=oc_enc_frag_intra_satd(_enc,src,ystride);break;
case 1:{
satd=oc_enc_frag_satd_thresh(_enc,src,ref+mv_offs[0],ystride,UINT_MAX);
}break;
default:{
satd=oc_enc_frag_satd_thresh(_enc,src,dst,ystride,UINT_MAX);
}
}
_enc->frag_satd[_fragi]=satd;
}
#endif
/*Transform:*/
oc_enc_fdct8x8(_enc,dct,data);
/*Quantize the DC coefficient:*/
qti=mb_mode!=OC_MODE_INTRA;
enquant=_pipe->enquant[_pli][0][qti];
dc_dequant=_pipe->dequant[_pli][0][qti][0];
v=dct[0];
val=v<<1;
s=OC_SIGNMASK(val);
val+=dc_dequant+s^s;
val=((enquant[0].m*(ogg_int32_t)val>>16)+val>>enquant[0].l)-s;
dc=OC_CLAMPI(-580,val,580);
nonzero=0;
/*Quantize the AC coefficients:*/
dequant=_pipe->dequant[_pli][qii][qti];
enquant=_pipe->enquant[_pli][qii][qti];
for(zzi=1;zzi<64;zzi++){
v=dct[OC_FZIG_ZAG[zzi]];
d=dequant[zzi];
val=v<<1;
v=abs(val);
if(v>=d){
s=OC_SIGNMASK(val);
/*The bias added here rounds ties away from zero, since token
optimization can only decrease the magnitude of the quantized
value.*/
val+=d+s^s;
/*Note the arithmetic right shift is not guaranteed by ANSI C.
Hopefully no one still uses ones-complement architectures.*/
val=((enquant[zzi].m*(ogg_int32_t)val>>16)+val>>enquant[zzi].l)-s;
data[zzi]=OC_CLAMPI(-580,val,580);
nonzero=zzi;
}
else data[zzi]=0;
}
/*Tokenize.*/
checkpoint=*_stack;
ac_bits=oc_enc_tokenize_ac(_enc,_pli,_fragi,data,dequant,dct,nonzero+1,
_stack,qti?0:3);
/*Reconstruct.
TODO: nonzero may need to be adjusted after tokenization.*/
if(nonzero==0){
ogg_int16_t p;
int ci;
/*We round this dequant product (and not any of the others) because there's
no iDCT rounding.*/
p=(ogg_int16_t)(dc*(ogg_int32_t)dc_dequant+15>>5);
/*LOOP VECTORIZES.*/
for(ci=0;ci<64;ci++)data[ci]=p;
}
else{
data[0]=dc*dc_dequant;
oc_idct8x8(&_enc->state,data,nonzero+1);
}
if(!qti)oc_enc_frag_recon_intra(_enc,dst,ystride,data);
else{
oc_enc_frag_recon_inter(_enc,dst,
nmv_offs==1?ref+mv_offs[0]:dst,ystride,data);
}
frame_type=_enc->state.frame_type;
#if !defined(OC_COLLECT_METRICS)
if(frame_type!=OC_INTRA_FRAME)
#endif
{
/*In retrospect, should we have skipped this block?*/
oc_enc_frag_sub(_enc,data,src,dst,ystride);
coded_ssd=coded_dc=0;
if(borderi<0){
for(pi=0;pi<64;pi++){
coded_ssd+=data[pi]*data[pi];
coded_dc+=data[pi];
}
}
else{
ogg_int64_t mask;
mask=_enc->state.borders[borderi].mask;
for(pi=0;pi<64;pi++,mask>>=1)if(mask&1){
coded_ssd+=data[pi]*data[pi];
coded_dc+=data[pi];
}
}
/*Scale to match DCT domain.*/
coded_ssd<<=4;
/*We actually only want the AC contribution to the SSD.*/
coded_ssd-=coded_dc*coded_dc>>2;
#if defined(OC_COLLECT_METRICS)
_enc->frag_ssd[_fragi]=coded_ssd;
}
if(frame_type!=OC_INTRA_FRAME){
#endif
uncoded_ssd=_pipe->skip_ssd[_pli][_fragi-_pipe->froffset[_pli]];
if(uncoded_ssd<UINT_MAX){
/*Although the fragment coding overhead determination is accurate, it is
greedy, using very coarse-grained local information.
Allowing it to mildly discourage coding turns out to be beneficial, but
it's not clear that allowing it to encourage coding through negative
coding overhead deltas is useful.
For that reason, we disallow negative coding_overheads.*/
if(_overhead_bits<0)_overhead_bits=0;
if(uncoded_ssd<=coded_ssd+(_overhead_bits+ac_bits)*_enc->lambda&&
/*Don't allow luma blocks to be skipped in 4MV mode when VP3
compatibility is enabled.*/
(!_enc->vp3_compatible||mb_mode!=OC_MODE_INTER_MV_FOUR||_pli)){
/*Hm, not worth it; roll back.*/
oc_enc_tokenlog_rollback(_enc,checkpoint,(*_stack)-checkpoint);
*_stack=checkpoint;
frags[_fragi].coded=0;
return 0;
}
}
else _mo->dc_flag=1;
_mo->uncoded_ac_ssd+=uncoded_ssd;
_mo->coded_ac_ssd+=coded_ssd;
_mo->ac_bits+=ac_bits;
}
oc_qii_state_advance(_pipe->qs+_pli,_pipe->qs+_pli,qii);
frags[_fragi].dc=dc;
frags[_fragi].coded=1;
return 1;
}
static int oc_enc_mb_transform_quantize_luma(oc_enc_ctx *_enc,
oc_enc_pipeline_state *_pipe,unsigned _mbi,int _mode_overhead){
/*Worst case token stack usage for 4 fragments.*/
oc_token_checkpoint stack[64*4];
oc_token_checkpoint *stackptr;
const oc_sb_map *sb_maps;
signed char *mb_modes;
oc_fragment *frags;
ptrdiff_t *coded_fragis;
ptrdiff_t ncoded_fragis;
ptrdiff_t *uncoded_fragis;
ptrdiff_t nuncoded_fragis;
oc_rd_metric mo;
oc_fr_state fr_checkpoint;
oc_qii_state qs_checkpoint;
int mb_mode;
int ncoded;
ptrdiff_t fragi;
int bi;
*&fr_checkpoint=*(_pipe->fr+0);
*&qs_checkpoint=*(_pipe->qs+0);
sb_maps=(const oc_sb_map *)_enc->state.sb_maps;
mb_modes=_enc->state.mb_modes;
frags=_enc->state.frags;
coded_fragis=_pipe->coded_fragis[0];
ncoded_fragis=_pipe->ncoded_fragis[0];
uncoded_fragis=_pipe->uncoded_fragis[0];
nuncoded_fragis=_pipe->nuncoded_fragis[0];
mb_mode=mb_modes[_mbi];
ncoded=0;
stackptr=stack;
memset(&mo,0,sizeof(mo));
for(bi=0;bi<4;bi++){
fragi=sb_maps[_mbi>>2][_mbi&3][bi];
frags[fragi].mb_mode=mb_mode;
if(oc_enc_block_transform_quantize(_enc,
_pipe,0,fragi,oc_fr_cost1(_pipe->fr+0),&mo,&stackptr)){
oc_fr_code_block(_pipe->fr+0);
coded_fragis[ncoded_fragis++]=fragi;
ncoded++;
}
else{
*(uncoded_fragis-++nuncoded_fragis)=fragi;
oc_fr_skip_block(_pipe->fr+0);
}
}
if(_enc->state.frame_type!=OC_INTRA_FRAME){
if(ncoded>0&&!mo.dc_flag){
int cost;
/*Some individual blocks were worth coding.
See if that's still true when accounting for mode and MV overhead.*/
cost=mo.coded_ac_ssd+_enc->lambda*(mo.ac_bits
+oc_fr_cost4(&fr_checkpoint,_pipe->fr+0)+_mode_overhead);
if(mo.uncoded_ac_ssd<=cost){
/*Taking macroblock overhead into account, it is not worth coding this
MB.*/
oc_enc_tokenlog_rollback(_enc,stack,stackptr-stack);
*(_pipe->fr+0)=*&fr_checkpoint;
*(_pipe->qs+0)=*&qs_checkpoint;
for(bi=0;bi<4;bi++){
fragi=sb_maps[_mbi>>2][_mbi&3][bi];
if(frags[fragi].coded){
*(uncoded_fragis-++nuncoded_fragis)=fragi;
frags[fragi].coded=0;
}
oc_fr_skip_block(_pipe->fr+0);
}
ncoded_fragis-=ncoded;
ncoded=0;
}
}
/*If no luma blocks coded, the mode is forced.*/
if(ncoded==0)mb_modes[_mbi]=OC_MODE_INTER_NOMV;
/*Assume that a 1MV with a single coded block is always cheaper than a 4MV
with a single coded block.
This may not be strictly true: a 4MV computes chroma MVs using (0,0) for
skipped blocks, while a 1MV does not.*/
else if(ncoded==1&&mb_mode==OC_MODE_INTER_MV_FOUR){
mb_modes[_mbi]=OC_MODE_INTER_MV;
}
}
_pipe->ncoded_fragis[0]=ncoded_fragis;
_pipe->nuncoded_fragis[0]=nuncoded_fragis;
return ncoded;
}
static void oc_enc_sb_transform_quantize_chroma(oc_enc_ctx *_enc,
oc_enc_pipeline_state *_pipe,int _pli,int _sbi_start,int _sbi_end){
const oc_sb_map *sb_maps;
oc_sb_flags *sb_flags;
ptrdiff_t *coded_fragis;
ptrdiff_t ncoded_fragis;
ptrdiff_t *uncoded_fragis;
ptrdiff_t nuncoded_fragis;
int sbi;
sb_maps=(const oc_sb_map *)_enc->state.sb_maps;
sb_flags=_enc->state.sb_flags;
coded_fragis=_pipe->coded_fragis[_pli];
ncoded_fragis=_pipe->ncoded_fragis[_pli];
uncoded_fragis=_pipe->uncoded_fragis[_pli];
nuncoded_fragis=_pipe->nuncoded_fragis[_pli];
for(sbi=_sbi_start;sbi<_sbi_end;sbi++){
/*Worst case token stack usage for 1 fragment.*/
oc_token_checkpoint stack[64];
oc_rd_metric mo;
int quadi;
int bi;
memset(&mo,0,sizeof(mo));
for(quadi=0;quadi<4;quadi++)for(bi=0;bi<4;bi++){
ptrdiff_t fragi;
fragi=sb_maps[sbi][quadi][bi];
if(fragi>=0){
oc_token_checkpoint *stackptr;
stackptr=stack;
if(oc_enc_block_transform_quantize(_enc,
_pipe,_pli,fragi,oc_fr_cost1(_pipe->fr+_pli),&mo,&stackptr)){
coded_fragis[ncoded_fragis++]=fragi;
oc_fr_code_block(_pipe->fr+_pli);
}
else{
*(uncoded_fragis-++nuncoded_fragis)=fragi;
oc_fr_skip_block(_pipe->fr+_pli);
}
}
}
oc_fr_state_flush_sb(_pipe->fr+_pli);
sb_flags[sbi].coded_fully=_pipe->fr[_pli].sb_full;
sb_flags[sbi].coded_partially=_pipe->fr[_pli].sb_partial;
}
_pipe->ncoded_fragis[_pli]=ncoded_fragis;
_pipe->nuncoded_fragis[_pli]=nuncoded_fragis;
}
/*Mode decision is done by exhaustively examining all potential choices.
Obviously, doing the motion compensation, fDCT, tokenization, and then
counting the bits each token uses is computationally expensive.
Theora's EOB runs can also split the cost of these tokens across multiple
fragments, and naturally we don't know what the optimal choice of Huffman
codes will be until we know all the tokens we're going to encode in all the
fragments.
So we use a simple approach to estimating the bit cost and distortion of each
mode based upon the SATD value of the residual before coding.
The mathematics behind the technique are outlined by Kim \cite{Kim03}, but
the process (modified somewhat from that of the paper) is very simple.
We build a non-linear regression of the mappings from
(pre-transform+quantization) SATD to (post-transform+quantization) bits and
SSD for each qi.