162 |
|
|
163 |
|
|
164 |
static int |
static int |
165 |
dct_quantize_trellis_h263_c(int16_t *const Out, const int16_t *const In, int Q, const uint16_t * const Zigzag, int Non_Zero); |
dct_quantize_trellis_h263_c(int16_t *const Out, |
166 |
|
const int16_t *const In, |
167 |
|
int Q, |
168 |
|
const uint16_t * const Zigzag, |
169 |
|
int Non_Zero); |
170 |
|
|
171 |
|
#if 0 |
172 |
static int |
static int |
173 |
dct_quantize_trellis_mpeg_c(int16_t *const Out, const int16_t *const In, int Q, const uint16_t * const Zigzag, int Non_Zero); |
dct_quantize_trellis_mpeg_c(int16_t *const Out, |
174 |
|
const int16_t *const In, |
175 |
|
int Q, |
176 |
|
const uint16_t * const Zigzag, |
177 |
|
int Non_Zero); |
178 |
|
#endif |
179 |
|
|
180 |
/* Quantize all blocks -- Inter mode */ |
/* Quantize all blocks -- Inter mode */ |
181 |
static __inline uint8_t |
static __inline uint8_t |
201 |
sum = quant_inter(&qcoeff[i*64], &data[i*64], pMB->quant); |
sum = quant_inter(&qcoeff[i*64], &data[i*64], pMB->quant); |
202 |
if ( (sum) && (frame->vop_flags & XVID_VOP_TRELLISQUANT) ) { |
if ( (sum) && (frame->vop_flags & XVID_VOP_TRELLISQUANT) ) { |
203 |
sum = dct_quantize_trellis_h263_c(&qcoeff[i*64], &data[i*64], pMB->quant, &scan_tables[0][0], 63)+1; |
sum = dct_quantize_trellis_h263_c(&qcoeff[i*64], &data[i*64], pMB->quant, &scan_tables[0][0], 63)+1; |
204 |
limit = 1; |
/* limit = 1; // Isibaar: why? deactivated so far - so please complain! ;-) */ |
205 |
} |
} |
206 |
} else { |
} else { |
207 |
sum = quant4_inter(&qcoeff[i * 64], &data[i * 64], pMB->quant); |
sum = quant4_inter(&qcoeff[i * 64], &data[i * 64], pMB->quant); |
208 |
// if ( (sum) && (frame->vop_flags & XVID_VOP_TRELLISQUANT) ) |
#if 0 |
209 |
// sum = dct_quantize_trellis_mpeg_c (&qcoeff[i*64], &data[i*64], pMB->quant)+1; |
if ( (sum) && (frame->vop_flags & XVID_VOP_TRELLISQUANT) ) |
210 |
|
sum = dct_quantize_trellis_mpeg_c (&qcoeff[i*64], &data[i*64], pMB->quant)+1; |
211 |
|
#endif |
212 |
} |
} |
213 |
stop_quant_timer(); |
stop_quant_timer(); |
214 |
|
|
441 |
/* Set the limit threshold */ |
/* Set the limit threshold */ |
442 |
limit = PVOP_TOOSMALL_LIMIT + ((pMB->quant == 1)? 1 : 0); |
limit = PVOP_TOOSMALL_LIMIT + ((pMB->quant == 1)? 1 : 0); |
443 |
|
|
444 |
|
if (frame->vop_flags & XVID_VOP_CARTOON) |
445 |
|
limit *= 3; |
446 |
|
|
447 |
/* Quantize the block */ |
/* Quantize the block */ |
448 |
cbp = MBQuantInter(pParam, frame, pMB, data, qcoeff, 0, limit); |
cbp = MBQuantInter(pParam, frame, pMB, data, qcoeff, 0, limit); |
449 |
|
|
482 |
/* Set the limit threshold */ |
/* Set the limit threshold */ |
483 |
limit = BVOP_TOOSMALL_LIMIT; |
limit = BVOP_TOOSMALL_LIMIT; |
484 |
|
|
485 |
|
if (frame->vop_flags & XVID_VOP_CARTOON) |
486 |
|
limit *= 2; |
487 |
|
|
488 |
/* Quantize the block */ |
/* Quantize the block */ |
489 |
cbp = MBQuantInter(pParam, frame, pMB, data, qcoeff, 1, limit); |
cbp = MBQuantInter(pParam, frame, pMB, data, qcoeff, 1, limit); |
490 |
|
|
563 |
|
|
564 |
/* left blocks */ |
/* left blocks */ |
565 |
|
|
566 |
// 1=2, 2=4, 4=8, 8=1 |
/* 1=2, 2=4, 4=8, 8=1 */ |
567 |
MOVLINE(tmp, LINE(0, 1)); |
MOVLINE(tmp, LINE(0, 1)); |
568 |
MOVLINE(LINE(0, 1), LINE(0, 2)); |
MOVLINE(LINE(0, 1), LINE(0, 2)); |
569 |
MOVLINE(LINE(0, 2), LINE(0, 4)); |
MOVLINE(LINE(0, 2), LINE(0, 4)); |
570 |
MOVLINE(LINE(0, 4), LINE(2, 0)); |
MOVLINE(LINE(0, 4), LINE(2, 0)); |
571 |
MOVLINE(LINE(2, 0), tmp); |
MOVLINE(LINE(2, 0), tmp); |
572 |
|
|
573 |
// 3=6, 6=12, 12=9, 9=3 |
/* 3=6, 6=12, 12=9, 9=3 */ |
574 |
MOVLINE(tmp, LINE(0, 3)); |
MOVLINE(tmp, LINE(0, 3)); |
575 |
MOVLINE(LINE(0, 3), LINE(0, 6)); |
MOVLINE(LINE(0, 3), LINE(0, 6)); |
576 |
MOVLINE(LINE(0, 6), LINE(2, 4)); |
MOVLINE(LINE(0, 6), LINE(2, 4)); |
577 |
MOVLINE(LINE(2, 4), LINE(2, 1)); |
MOVLINE(LINE(2, 4), LINE(2, 1)); |
578 |
MOVLINE(LINE(2, 1), tmp); |
MOVLINE(LINE(2, 1), tmp); |
579 |
|
|
580 |
// 5=10, 10=5 |
/* 5=10, 10=5 */ |
581 |
MOVLINE(tmp, LINE(0, 5)); |
MOVLINE(tmp, LINE(0, 5)); |
582 |
MOVLINE(LINE(0, 5), LINE(2, 2)); |
MOVLINE(LINE(0, 5), LINE(2, 2)); |
583 |
MOVLINE(LINE(2, 2), tmp); |
MOVLINE(LINE(2, 2), tmp); |
584 |
|
|
585 |
// 7=14, 14=13, 13=11, 11=7 |
/* 7=14, 14=13, 13=11, 11=7 */ |
586 |
MOVLINE(tmp, LINE(0, 7)); |
MOVLINE(tmp, LINE(0, 7)); |
587 |
MOVLINE(LINE(0, 7), LINE(2, 6)); |
MOVLINE(LINE(0, 7), LINE(2, 6)); |
588 |
MOVLINE(LINE(2, 6), LINE(2, 5)); |
MOVLINE(LINE(2, 6), LINE(2, 5)); |
591 |
|
|
592 |
/* right blocks */ |
/* right blocks */ |
593 |
|
|
594 |
// 1=2, 2=4, 4=8, 8=1 |
/* 1=2, 2=4, 4=8, 8=1 */ |
595 |
MOVLINE(tmp, LINE(1, 1)); |
MOVLINE(tmp, LINE(1, 1)); |
596 |
MOVLINE(LINE(1, 1), LINE(1, 2)); |
MOVLINE(LINE(1, 1), LINE(1, 2)); |
597 |
MOVLINE(LINE(1, 2), LINE(1, 4)); |
MOVLINE(LINE(1, 2), LINE(1, 4)); |
598 |
MOVLINE(LINE(1, 4), LINE(3, 0)); |
MOVLINE(LINE(1, 4), LINE(3, 0)); |
599 |
MOVLINE(LINE(3, 0), tmp); |
MOVLINE(LINE(3, 0), tmp); |
600 |
|
|
601 |
// 3=6, 6=12, 12=9, 9=3 |
/* 3=6, 6=12, 12=9, 9=3 */ |
602 |
MOVLINE(tmp, LINE(1, 3)); |
MOVLINE(tmp, LINE(1, 3)); |
603 |
MOVLINE(LINE(1, 3), LINE(1, 6)); |
MOVLINE(LINE(1, 3), LINE(1, 6)); |
604 |
MOVLINE(LINE(1, 6), LINE(3, 4)); |
MOVLINE(LINE(1, 6), LINE(3, 4)); |
605 |
MOVLINE(LINE(3, 4), LINE(3, 1)); |
MOVLINE(LINE(3, 4), LINE(3, 1)); |
606 |
MOVLINE(LINE(3, 1), tmp); |
MOVLINE(LINE(3, 1), tmp); |
607 |
|
|
608 |
// 5=10, 10=5 |
/* 5=10, 10=5 */ |
609 |
MOVLINE(tmp, LINE(1, 5)); |
MOVLINE(tmp, LINE(1, 5)); |
610 |
MOVLINE(LINE(1, 5), LINE(3, 2)); |
MOVLINE(LINE(1, 5), LINE(3, 2)); |
611 |
MOVLINE(LINE(3, 2), tmp); |
MOVLINE(LINE(3, 2), tmp); |
612 |
|
|
613 |
// 7=14, 14=13, 13=11, 11=7 |
/* 7=14, 14=13, 13=11, 11=7 */ |
614 |
MOVLINE(tmp, LINE(1, 7)); |
MOVLINE(tmp, LINE(1, 7)); |
615 |
MOVLINE(LINE(1, 7), LINE(3, 6)); |
MOVLINE(LINE(1, 7), LINE(3, 6)); |
616 |
MOVLINE(LINE(3, 6), LINE(3, 5)); |
MOVLINE(LINE(3, 6), LINE(3, 5)); |
622 |
|
|
623 |
|
|
624 |
|
|
625 |
/************************************************************************ |
/***************************************************************************** |
626 |
* Trellis based R-D optimal quantization * |
* Trellis based R-D optimal quantization |
627 |
* * |
* |
628 |
* Trellis Quant code (C) 2003 Pascal Massimino skal(at)planet-d.net * |
* Trellis Quant code (C) 2003 Pascal Massimino skal(at)planet-d.net |
629 |
* * |
* |
630 |
************************************************************************/ |
****************************************************************************/ |
631 |
|
|
632 |
|
|
633 |
|
#if 0 |
634 |
static int |
static int |
635 |
dct_quantize_trellis_mpeg_c(int16_t *const Out, const int16_t *const In, int Q, |
dct_quantize_trellis_mpeg_c(int16_t *const Out, |
636 |
const uint16_t * const Zigzag, int Non_Zero) |
const int16_t *const In, |
637 |
{ return 63; } |
int Q, |
638 |
|
const uint16_t * const Zigzag, |
639 |
|
int Non_Zero) |
640 |
////////////////////////////////////////////////////////// |
{ |
641 |
// |
return 63; |
642 |
// Trellis-Based quantization |
} |
643 |
// |
#endif |
644 |
// So far I understand this paper: |
|
645 |
// |
/*---------------------------------------------------------------------------- |
646 |
// "Trellis-Based R-D Optimal Quantization in H.263+" |
* |
647 |
// J.Wen, M.Luttrell, J.Villasenor |
* Trellis-Based quantization |
648 |
// IEEE Transactions on Image Processing, Vol.9, No.8, Aug. 2000. |
* |
649 |
// |
* So far I understand this paper: |
650 |
// we are at stake with a simplified Bellmand-Ford / Dijkstra Single |
* |
651 |
// Source Shorted Path algo. But due to the underlying graph structure |
* "Trellis-Based R-D Optimal Quantization in H.263+" |
652 |
// ("Trellis"), it can be turned into a dynamic programming algo, |
* J.Wen, M.Luttrell, J.Villasenor |
653 |
// partially saving the explicit graph's nodes representation. And |
* IEEE Transactions on Image Processing, Vol.9, No.8, Aug. 2000. |
654 |
// without using a heap, since the open frontier of the DAG is always |
* |
655 |
// known, and of fixed sized. |
* we are at stake with a simplified Bellmand-Ford / Dijkstra Single |
656 |
// |
* Source Shorted Path algo. But due to the underlying graph structure |
657 |
////////////////////////////////////////////////////////// |
* ("Trellis"), it can be turned into a dynamic programming algo, |
658 |
|
* partially saving the explicit graph's nodes representation. And |
659 |
|
* without using a heap, since the open frontier of the DAG is always |
660 |
|
* known, and of fixed sized. |
661 |
|
*--------------------------------------------------------------------------*/ |
662 |
|
|
663 |
|
|
|
////////////////////////////////////////////////////////// |
|
|
// Codes lengths for relevant levels. |
|
664 |
|
|
665 |
// let's factorize: |
/* Codes lengths for relevant levels. */ |
666 |
|
|
667 |
|
/* let's factorize: */ |
668 |
static const uint8_t Code_Len0[64] = { |
static const uint8_t Code_Len0[64] = { |
669 |
30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30, |
30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30, |
670 |
30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30 }; |
30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30 }; |
729 |
3, 4, 5, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 9, 9,10,10,10,10,10,10,10,10,12,12,13,13,12,13,14,15,15, |
3, 4, 5, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 9, 9,10,10,10,10,10,10,10,10,12,12,13,13,12,13,14,15,15, |
730 |
15,16,16,16,16,17,17,17,18,18,19,19,19,19,19,19,19,19,21,21,22,22,30,30,30,30,30,30,30,30,30,30 }; |
15,16,16,16,16,17,17,17,18,18,19,19,19,19,19,19,19,19,21,21,22,22,30,30,30,30,30,30,30,30,30,30 }; |
731 |
|
|
732 |
// a few more table for LAST table: |
/* a few more table for LAST table: */ |
733 |
static const uint8_t Code_Len21[64] = { |
static const uint8_t Code_Len21[64] = { |
734 |
13,20,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30, |
13,20,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30, |
735 |
30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30}; |
30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30}; |
744 |
12,13,13,13,13,13,13,13,13,14,16,16,16,16,17,17,17,17,18,18,18,18,18,18,18,18,19,19,19,19,19,19}; |
12,13,13,13,13,13,13,13,13,14,16,16,16,16,17,17,17,17,18,18,18,18,18,18,18,18,19,19,19,19,19,19}; |
745 |
|
|
746 |
|
|
747 |
static const uint8_t * const B16_17_Code_Len[24] = { // levels [1..24] |
static const uint8_t * const B16_17_Code_Len[24] = { /* levels [1..24] */ |
748 |
Code_Len20,Code_Len19,Code_Len18,Code_Len17, |
Code_Len20,Code_Len19,Code_Len18,Code_Len17, |
749 |
Code_Len16,Code_Len15,Code_Len14,Code_Len13, |
Code_Len16,Code_Len15,Code_Len14,Code_Len13, |
750 |
Code_Len12,Code_Len11,Code_Len10,Code_Len9, |
Code_Len12,Code_Len11,Code_Len10,Code_Len9, |
753 |
Code_Len2, Code_Len1, Code_Len1, Code_Len1, |
Code_Len2, Code_Len1, Code_Len1, Code_Len1, |
754 |
}; |
}; |
755 |
|
|
756 |
static const uint8_t * const B16_17_Code_Len_Last[6] = { // levels [1..6] |
static const uint8_t * const B16_17_Code_Len_Last[6] = { /* levels [1..6] */ |
757 |
Code_Len24,Code_Len23,Code_Len22,Code_Len21, Code_Len3, Code_Len1, |
Code_Len24,Code_Len23,Code_Len22,Code_Len21, Code_Len3, Code_Len1, |
758 |
}; |
}; |
759 |
|
|
767 |
}; |
}; |
768 |
#undef TL |
#undef TL |
769 |
|
|
770 |
static inline int Find_Last(const int16_t *C, const uint16_t *Zigzag, int i) |
static __inline int Find_Last(const int16_t *C, const uint16_t *Zigzag, int i) |
771 |
{ |
{ |
772 |
while(i>=0) |
while(i>=0) |
773 |
if (C[Zigzag[i]]) |
if (C[Zigzag[i]]) |
776 |
return -1; |
return -1; |
777 |
} |
} |
778 |
|
|
779 |
////////////////////////////////////////////////////////// |
/* this routine has been strippen of all debug code */ |
|
// this routine has been strippen of all debug code |
|
|
////////////////////////////////////////////////////////// |
|
780 |
|
|
781 |
static int |
static int |
782 |
dct_quantize_trellis_h263_c(int16_t *const Out, const int16_t *const In, int Q, const uint16_t * const Zigzag, int Non_Zero) |
dct_quantize_trellis_h263_c(int16_t *const Out, const int16_t *const In, int Q, const uint16_t * const Zigzag, int Non_Zero) |
783 |
{ |
{ |
784 |
|
|
785 |
// Note: We should search last non-zero coeffs on *real* DCT input coeffs (In[]), |
/* |
786 |
// not quantized one (Out[]). However, it only improves the result *very* |
* Note: We should search last non-zero coeffs on *real* DCT input coeffs (In[]), |
787 |
// slightly (~0.01dB), whereas speed drops to crawling level :) |
* not quantized one (Out[]). However, it only improves the result *very* |
788 |
// Well, actually, taking 1 more coeff past Non_Zero into account sometimes helps, |
* slightly (~0.01dB), whereas speed drops to crawling level :) |
789 |
|
* Well, actually, taking 1 more coeff past Non_Zero into account sometimes helps. |
790 |
|
*/ |
791 |
typedef struct { int16_t Run, Level; } NODE; |
typedef struct { int16_t Run, Level; } NODE; |
792 |
|
|
793 |
NODE Nodes[65], Last; |
NODE Nodes[65], Last; |
796 |
const int Mult = 2*Q; |
const int Mult = 2*Q; |
797 |
const int Bias = (Q-1) | 1; |
const int Bias = (Q-1) | 1; |
798 |
const int Lev0 = Mult + Bias; |
const int Lev0 = Mult + Bias; |
799 |
const int Lambda = Trellis_Lambda_Tabs[Q-1]; // it's 1/lambda, actually |
const int Lambda = Trellis_Lambda_Tabs[Q-1]; /* it's 1/lambda, actually */ |
800 |
|
|
801 |
int Run_Start = -1; |
int Run_Start = -1; |
|
Run_Costs[-1] = 2<<16; // source (w/ CBP penalty) |
|
802 |
uint32_t Min_Cost = 2<<16; |
uint32_t Min_Cost = 2<<16; |
803 |
|
|
804 |
int Last_Node = -1; |
int Last_Node = -1; |
805 |
uint32_t Last_Cost = 0; |
uint32_t Last_Cost = 0; |
806 |
|
|
807 |
int i, j; |
int i, j; |
808 |
|
Run_Costs[-1] = 2<<16; /* source (w/ CBP penalty) */ |
809 |
|
|
810 |
Non_Zero = Find_Last(Out, Zigzag, Non_Zero); |
Non_Zero = Find_Last(Out, Zigzag, Non_Zero); |
811 |
if (Non_Zero<0) |
if (Non_Zero<0) |
819 |
uint32_t Best_Cost = 0xf0000000; |
uint32_t Best_Cost = 0xf0000000; |
820 |
Last_Cost += Dist0; |
Last_Cost += Dist0; |
821 |
|
|
822 |
if ((uint32_t)(Level1+1)<3) // very specialized loop for -1,0,+1 |
if ((uint32_t)(Level1+1)<3) /* very specialized loop for -1,0,+1 */ |
823 |
{ |
{ |
824 |
int dQ; |
int dQ; |
825 |
int Run; |
int Run; |
842 |
const uint32_t Cost = Cost_Base + (Code_Len20[Run-1]<<16); |
const uint32_t Cost = Cost_Base + (Code_Len20[Run-1]<<16); |
843 |
const uint32_t lCost = Cost_Base + (Code_Len24[Run-1]<<16); |
const uint32_t lCost = Cost_Base + (Code_Len24[Run-1]<<16); |
844 |
|
|
845 |
// TODO: what about tie-breaks? Should we favor short runs or |
/* |
846 |
// long runs? Although the error is the same, it would not be |
* TODO: what about tie-breaks? Should we favor short runs or |
847 |
// spread the same way along high and low frequencies... |
* long runs? Although the error is the same, it would not be |
848 |
|
* spread the same way along high and low frequencies... |
849 |
|
*/ |
850 |
|
|
851 |
// (I'd say: favour short runs => hifreq errors (HVS) -- gruel ) |
/* (I'd say: favour short runs => hifreq errors (HVS) -- gruel ) */ |
852 |
|
|
853 |
if (Cost<Best_Cost) { |
if (Cost<Best_Cost) { |
854 |
Best_Cost = Cost; |
Best_Cost = Cost; |
864 |
if (Last_Node==i) |
if (Last_Node==i) |
865 |
Last.Level = Nodes[i].Level; |
Last.Level = Nodes[i].Level; |
866 |
} |
} |
867 |
else // "big" levels |
else /* "big" levels */ |
868 |
{ |
{ |
869 |
const uint8_t *Tbl_L1, *Tbl_L2, *Tbl_L1_Last, *Tbl_L2_Last; |
const uint8_t *Tbl_L1, *Tbl_L2, *Tbl_L1_Last, *Tbl_L2_Last; |
870 |
int Level2; |
int Level2; |
881 |
Tbl_L2 = (Level2<=24) ? B16_17_Code_Len[Level2-1] : Code_Len0; |
Tbl_L2 = (Level2<=24) ? B16_17_Code_Len[Level2-1] : Code_Len0; |
882 |
Tbl_L1_Last = (Level1<=6) ? B16_17_Code_Len_Last[Level1-1] : Code_Len0; |
Tbl_L1_Last = (Level1<=6) ? B16_17_Code_Len_Last[Level1-1] : Code_Len0; |
883 |
Tbl_L2_Last = (Level2<=6) ? B16_17_Code_Len_Last[Level2-1] : Code_Len0; |
Tbl_L2_Last = (Level2<=6) ? B16_17_Code_Len_Last[Level2-1] : Code_Len0; |
884 |
} else { // Level1<-1 |
} else { /* Level1<-1 */ |
885 |
dQ1 = Level1*Mult-AC - Bias; |
dQ1 = Level1*Mult-AC - Bias; |
886 |
dQ2 = dQ1 + Mult; |
dQ2 = dQ1 + Mult; |
887 |
Level2 = Level1 + 1; |
Level2 = Level1 + 1; |
900 |
uint32_t Cost1, Cost2; |
uint32_t Cost1, Cost2; |
901 |
int bLevel; |
int bLevel; |
902 |
|
|
903 |
// for sub-optimal (but slightly worth it, speed-wise) search, uncomment the following: |
/* |
904 |
// if (Cost_Base>=Best_Cost) continue; |
* for sub-optimal (but slightly worth it, speed-wise) search, uncomment the following: |
905 |
// (? doesn't seem to have any effect -- gruel ) |
* if (Cost_Base>=Best_Cost) continue; |
906 |
|
* (? doesn't seem to have any effect -- gruel ) |
907 |
|
*/ |
908 |
|
|
909 |
Cost1 = Cost_Base + (Tbl_L1[Run-1]<<16); |
Cost1 = Cost_Base + (Tbl_L1[Run-1]<<16); |
910 |
Cost2 = Cost_Base + (Tbl_L2[Run-1]<<16) + dDist21; |
Cost2 = Cost_Base + (Tbl_L2[Run-1]<<16) + dDist21; |
936 |
Last.Level = bLevel; |
Last.Level = bLevel; |
937 |
Last_Node = i; |
Last_Node = i; |
938 |
} |
} |
939 |
} //end of "for Run" |
} /* end of "for Run" */ |
940 |
|
|
941 |
} |
} |
942 |
|
|
948 |
} |
} |
949 |
else |
else |
950 |
{ |
{ |
951 |
// as noticed by Michael Niedermayer (michaelni at gmx.at), there's |
/* |
952 |
// a code shorter by 1 bit for a larger run (!), same level. We give |
* as noticed by Michael Niedermayer (michaelni at gmx.at), there's |
953 |
// it a chance by not moving the left barrier too much. |
* a code shorter by 1 bit for a larger run (!), same level. We give |
954 |
|
* it a chance by not moving the left barrier too much. |
955 |
|
*/ |
956 |
|
|
957 |
while( Run_Costs[Run_Start]>Min_Cost+(1<<16) ) |
while( Run_Costs[Run_Start]>Min_Cost+(1<<16) ) |
958 |
Run_Start++; |
Run_Start++; |
959 |
|
|
960 |
// spread on preceding coeffs the cost incurred by skipping this one |
/* spread on preceding coeffs the cost incurred by skipping this one */ |
961 |
for(j=Run_Start; j<i; ++j) Run_Costs[j] += Dist0; |
for(j=Run_Start; j<i; ++j) Run_Costs[j] += Dist0; |
962 |
Min_Cost += Dist0; |
Min_Cost += Dist0; |
963 |
} |
} |
966 |
if (Last_Node<0) |
if (Last_Node<0) |
967 |
return -1; |
return -1; |
968 |
|
|
969 |
// reconstruct optimal sequence backward with surviving paths |
/* reconstruct optimal sequence backward with surviving paths */ |
970 |
memset(Out, 0x00, 64*sizeof(*Out)); |
memset(Out, 0x00, 64*sizeof(*Out)); |
971 |
Out[Zigzag[Last_Node]] = Last.Level; |
Out[Zigzag[Last_Node]] = Last.Level; |
972 |
i = Last_Node - Last.Run; |
i = Last_Node - Last.Run; |
987 |
|
|
988 |
|
|
989 |
|
|
990 |
////////////////////////////////////////////////////////// |
/* original version including heavy debugging info */ |
|
// original version including heavy debugging info |
|
|
////////////////////////////////////////////////////////// |
|
|
|
|
991 |
|
|
992 |
#ifdef DBGTRELL |
#ifdef DBGTRELL |
993 |
|
|
994 |
#define DBG 0 |
#define DBG 0 |
995 |
|
|
996 |
static inline uint32_t Evaluate_Cost(const int16_t *C, int Mult, int Bias, |
static __inline uint32_t Evaluate_Cost(const int16_t *C, int Mult, int Bias, |
997 |
const uint16_t * Zigzag, int Max, int Lambda) |
const uint16_t * Zigzag, int Max, int Lambda) |
998 |
{ |
{ |
999 |
#if (DBG>0) |
#if (DBG>0) |
1011 |
int j=0, j0=0; |
int j=0, j0=0; |
1012 |
int Run, Level; |
int Run, Level; |
1013 |
|
|
1014 |
Bits = 2; // CBP |
Bits = 2; /* CBP */ |
1015 |
while(j<Last) { |
while(j<Last) { |
1016 |
while(!C[Zigzag[j]]) |
while(!C[Zigzag[j]]) |
1017 |
j++; |
j++; |
1058 |
dct_quantize_trellis_h263_c(int16_t *const Out, const int16_t *const In, int Q, const uint16_t * const Zigzag, int Non_Zero) |
dct_quantize_trellis_h263_c(int16_t *const Out, const int16_t *const In, int Q, const uint16_t * const Zigzag, int Non_Zero) |
1059 |
{ |
{ |
1060 |
|
|
1061 |
// Note: We should search last non-zero coeffs on *real* DCT input coeffs (In[]), |
/* |
1062 |
// not quantized one (Out[]). However, it only improves the result *very* |
* Note: We should search last non-zero coeffs on *real* DCT input coeffs (In[]), |
1063 |
// slightly (~0.01dB), whereas speed drops to crawling level :) |
* not quantized one (Out[]). However, it only improves the result *very* |
1064 |
// Well, actually, taking 1 more coeff past Non_Zero into account sometimes helps, |
* slightly (~0.01dB), whereas speed drops to crawling level :) |
1065 |
|
* Well, actually, taking 1 more coeff past Non_Zero into account sometimes helps. |
1066 |
|
*/ |
1067 |
typedef struct { int16_t Run, Level; } NODE; |
typedef struct { int16_t Run, Level; } NODE; |
1068 |
|
|
1069 |
NODE Nodes[65], Last; |
NODE Nodes[65], Last; |
1072 |
const int Mult = 2*Q; |
const int Mult = 2*Q; |
1073 |
const int Bias = (Q-1) | 1; |
const int Bias = (Q-1) | 1; |
1074 |
const int Lev0 = Mult + Bias; |
const int Lev0 = Mult + Bias; |
1075 |
const int Lambda = Trellis_Lambda_Tabs[Q-1]; // it's 1/lambda, actually |
const int Lambda = Trellis_Lambda_Tabs[Q-1]; /* it's 1/lambda, actually */ |
1076 |
|
|
1077 |
int Run_Start = -1; |
int Run_Start = -1; |
1078 |
Run_Costs[-1] = 2<<16; // source (w/ CBP penalty) |
Run_Costs[-1] = 2<<16; /* source (w/ CBP penalty) */ |
1079 |
uint32_t Min_Cost = 2<<16; |
uint32_t Min_Cost = 2<<16; |
1080 |
|
|
1081 |
int Last_Node = -1; |
int Last_Node = -1; |
1084 |
int i, j; |
int i, j; |
1085 |
|
|
1086 |
#if (DBG>0) |
#if (DBG>0) |
1087 |
Last.Level = 0; Last.Run = -1; // just initialize to smthg |
Last.Level = 0; Last.Run = -1; /* just initialize to smthg */ |
1088 |
#endif |
#endif |
1089 |
|
|
1090 |
Non_Zero = Find_Last(Out, Zigzag, Non_Zero); |
Non_Zero = Find_Last(Out, Zigzag, Non_Zero); |
1099 |
uint32_t Best_Cost = 0xf0000000; |
uint32_t Best_Cost = 0xf0000000; |
1100 |
Last_Cost += Dist0; |
Last_Cost += Dist0; |
1101 |
|
|
1102 |
if ((uint32_t)(Level1+1)<3) // very specialized loop for -1,0,+1 |
if ((uint32_t)(Level1+1)<3) /* very specialized loop for -1,0,+1 */ |
1103 |
{ |
{ |
1104 |
int dQ; |
int dQ; |
1105 |
int Run; |
int Run; |
1122 |
const uint32_t Cost = Cost_Base + (Code_Len20[Run-1]<<16); |
const uint32_t Cost = Cost_Base + (Code_Len20[Run-1]<<16); |
1123 |
const uint32_t lCost = Cost_Base + (Code_Len24[Run-1]<<16); |
const uint32_t lCost = Cost_Base + (Code_Len24[Run-1]<<16); |
1124 |
|
|
1125 |
// TODO: what about tie-breaks? Should we favor short runs or |
/* |
1126 |
// long runs? Although the error is the same, it would not be |
* TODO: what about tie-breaks? Should we favor short runs or |
1127 |
// spread the same way along high and low frequencies... |
* long runs? Although the error is the same, it would not be |
1128 |
|
* spread the same way along high and low frequencies... |
1129 |
|
*/ |
1130 |
if (Cost<Best_Cost) { |
if (Cost<Best_Cost) { |
1131 |
Best_Cost = Cost; |
Best_Cost = Cost; |
1132 |
Nodes[i].Run = Run; |
Nodes[i].Run = Run; |
1156 |
printf( "\n" ); |
printf( "\n" ); |
1157 |
} |
} |
1158 |
} |
} |
1159 |
else // "big" levels |
else /* "big" levels */ |
1160 |
{ |
{ |
1161 |
const uint8_t *Tbl_L1, *Tbl_L2, *Tbl_L1_Last, *Tbl_L2_Last; |
const uint8_t *Tbl_L1, *Tbl_L2, *Tbl_L1_Last, *Tbl_L2_Last; |
1162 |
int Level2; |
int Level2; |
1173 |
Tbl_L2 = (Level2<=24) ? B16_17_Code_Len[Level2-1] : Code_Len0; |
Tbl_L2 = (Level2<=24) ? B16_17_Code_Len[Level2-1] : Code_Len0; |
1174 |
Tbl_L1_Last = (Level1<=6) ? B16_17_Code_Len_Last[Level1-1] : Code_Len0; |
Tbl_L1_Last = (Level1<=6) ? B16_17_Code_Len_Last[Level1-1] : Code_Len0; |
1175 |
Tbl_L2_Last = (Level2<=6) ? B16_17_Code_Len_Last[Level2-1] : Code_Len0; |
Tbl_L2_Last = (Level2<=6) ? B16_17_Code_Len_Last[Level2-1] : Code_Len0; |
1176 |
} else { // Level1<-1 |
} else { /* Level1<-1 */ |
1177 |
dQ1 = Level1*Mult-AC - Bias; |
dQ1 = Level1*Mult-AC - Bias; |
1178 |
dQ2 = dQ1 + Mult; |
dQ2 = dQ1 + Mult; |
1179 |
Level2 = Level1 + 1; |
Level2 = Level1 + 1; |
1192 |
uint32_t Cost1, Cost2; |
uint32_t Cost1, Cost2; |
1193 |
int bLevel; |
int bLevel; |
1194 |
|
|
1195 |
// for sub-optimal (but slightly worth it, speed-wise) search, uncomment the following: |
/* |
1196 |
// if (Cost_Base>=Best_Cost) continue; |
* for sub-optimal (but slightly worth it, speed-wise) search, uncomment the following: |
1197 |
|
* if (Cost_Base>=Best_Cost) continue; |
1198 |
|
*/ |
1199 |
Cost1 = Cost_Base + (Tbl_L1[Run-1]<<16); |
Cost1 = Cost_Base + (Tbl_L1[Run-1]<<16); |
1200 |
Cost2 = Cost_Base + (Tbl_L2[Run-1]<<16) + dDist21; |
Cost2 = Cost_Base + (Tbl_L2[Run-1]<<16) + dDist21; |
1201 |
|
|
1226 |
Last.Level = bLevel; |
Last.Level = bLevel; |
1227 |
Last_Node = i; |
Last_Node = i; |
1228 |
} |
} |
1229 |
} //end of "for Run" |
} /* end of "for Run" */ |
1230 |
|
|
1231 |
if (DBG==1) { |
if (DBG==1) { |
1232 |
Run_Costs[i] = Best_Cost; |
Run_Costs[i] = Best_Cost; |
1252 |
} |
} |
1253 |
else |
else |
1254 |
{ |
{ |
1255 |
// as noticed by Michael Niedermayer (michaelni at gmx.at), there's |
/* |
1256 |
// a code shorter by 1 bit for a larger run (!), same level. We give |
* as noticed by Michael Niedermayer (michaelni at gmx.at), there's |
1257 |
// it a chance by not moving the left barrier too much. |
* a code shorter by 1 bit for a larger run (!), same level. We give |
1258 |
|
* it a chance by not moving the left barrier too much. |
1259 |
|
*/ |
1260 |
|
|
1261 |
while( Run_Costs[Run_Start]>Min_Cost+(1<<16) ) |
while( Run_Costs[Run_Start]>Min_Cost+(1<<16) ) |
1262 |
Run_Start++; |
Run_Start++; |
1263 |
|
|
1264 |
// spread on preceding coeffs the cost incurred by skipping this one |
/* spread on preceding coeffs the cost incurred by skipping this one */ |
1265 |
for(j=Run_Start; j<i; ++j) Run_Costs[j] += Dist0; |
for(j=Run_Start; j<i; ++j) Run_Costs[j] += Dist0; |
1266 |
Min_Cost += Dist0; |
Min_Cost += Dist0; |
1267 |
} |
} |
1279 |
if (Last_Node<0) |
if (Last_Node<0) |
1280 |
return -1; |
return -1; |
1281 |
|
|
1282 |
// reconstruct optimal sequence backward with surviving paths |
/* reconstruct optimal sequence backward with surviving paths */ |
1283 |
memset(Out, 0x00, 64*sizeof(*Out)); |
memset(Out, 0x00, 64*sizeof(*Out)); |
1284 |
Out[Zigzag[Last_Node]] = Last.Level; |
Out[Zigzag[Last_Node]] = Last.Level; |
1285 |
i = Last_Node - Last.Run; |
i = Last_Node - Last.Run; |