25 |
* |
* |
26 |
****************************************************************************/ |
****************************************************************************/ |
27 |
|
|
28 |
#include <string.h> |
#include <stdio.h> |
29 |
#include <stdlib.h> |
#include <stdlib.h> |
30 |
|
#include <string.h> |
31 |
|
|
32 |
#include "../portab.h" |
#include "../portab.h" |
33 |
#include "mbfunctions.h" |
#include "mbfunctions.h" |
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_inter_mpeg_c (int16_t *qcoeff, const int16_t *data, int quant) |
dct_quantize_trellis_mpeg_c(int16_t *const Out, |
636 |
{ return 63; } |
const int16_t *const In, |
637 |
|
int Q, |
638 |
|
const uint16_t * const Zigzag, |
639 |
|
int Non_Zero) |
640 |
|
{ |
641 |
|
return 63; |
642 |
|
} |
643 |
|
#endif |
644 |
|
|
645 |
|
/*---------------------------------------------------------------------------- |
646 |
|
* |
647 |
|
* Trellis-Based quantization |
648 |
|
* |
649 |
|
* So far I understand this paper: |
650 |
|
* |
651 |
|
* "Trellis-Based R-D Optimal Quantization in H.263+" |
652 |
|
* J.Wen, M.Luttrell, J.Villasenor |
653 |
|
* IEEE Transactions on Image Processing, Vol.9, No.8, Aug. 2000. |
654 |
|
* |
655 |
|
* 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 |
|
|
|
////////////////////////////////////////////////////////// |
|
|
// |
|
|
// Trellis-Based quantization |
|
|
// |
|
|
// So far I understand this paper: |
|
|
// |
|
|
// "Trellis-Based R-D Optimal Quantization in H.263+" |
|
|
// J.Wen, M.Luttrell, J.Villasenor |
|
|
// IEEE Transactions on Image Processing, Vol.9, No.8, Aug. 2000. |
|
|
// |
|
|
// we are at stake with a simplified Bellmand-Ford / Dijkstra Single |
|
|
// Source Shorted Path algo. But due to the underlying graph structure |
|
|
// ("Trellis"), it can be turned into a dynamic programming algo, |
|
|
// partially saving the explicit graph's nodes representation. And |
|
|
// without using a heap, since the open frontier of the DAG is always |
|
|
// known, and of fixed sized. |
|
|
// |
|
|
////////////////////////////////////////////////////////// |
|
663 |
|
|
664 |
|
|
665 |
////////////////////////////////////////////////////////// |
/* Codes lengths for relevant levels. */ |
|
// Codes lengths for relevant levels. |
|
666 |
|
|
667 |
// let's factorize: |
/* 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 */ |
780 |
|
|
781 |
|
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) |
783 |
|
{ |
784 |
|
|
785 |
|
/* |
786 |
|
* Note: We should search last non-zero coeffs on *real* DCT input coeffs (In[]), |
787 |
|
* not quantized one (Out[]). However, it only improves the result *very* |
788 |
|
* 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; |
792 |
|
|
793 |
|
NODE Nodes[65], Last; |
794 |
|
uint32_t Run_Costs0[64+1]; |
795 |
|
uint32_t * const Run_Costs = Run_Costs0 + 1; |
796 |
|
const int Mult = 2*Q; |
797 |
|
const int Bias = (Q-1) | 1; |
798 |
|
const int Lev0 = Mult + Bias; |
799 |
|
const int Lambda = Trellis_Lambda_Tabs[Q-1]; /* it's 1/lambda, actually */ |
800 |
|
|
801 |
|
int Run_Start = -1; |
802 |
|
uint32_t Min_Cost = 2<<16; |
803 |
|
|
804 |
|
int Last_Node = -1; |
805 |
|
uint32_t Last_Cost = 0; |
806 |
|
|
807 |
|
int i, j; |
808 |
|
Run_Costs[-1] = 2<<16; /* source (w/ CBP penalty) */ |
809 |
|
|
810 |
|
Non_Zero = Find_Last(Out, Zigzag, Non_Zero); |
811 |
|
if (Non_Zero<0) |
812 |
|
return -1; |
813 |
|
|
814 |
|
for(i=0; i<=Non_Zero; i++) |
815 |
|
{ |
816 |
|
const int AC = In[Zigzag[i]]; |
817 |
|
const int Level1 = Out[Zigzag[i]]; |
818 |
|
const int Dist0 = Lambda* AC*AC; |
819 |
|
uint32_t Best_Cost = 0xf0000000; |
820 |
|
Last_Cost += Dist0; |
821 |
|
|
822 |
|
if ((uint32_t)(Level1+1)<3) /* very specialized loop for -1,0,+1 */ |
823 |
|
{ |
824 |
|
int dQ; |
825 |
|
int Run; |
826 |
|
uint32_t Cost0; |
827 |
|
|
828 |
|
if (AC<0) { |
829 |
|
Nodes[i].Level = -1; |
830 |
|
dQ = Lev0 + AC; |
831 |
|
} else { |
832 |
|
Nodes[i].Level = 1; |
833 |
|
dQ = Lev0 - AC; |
834 |
|
} |
835 |
|
Cost0 = Lambda*dQ*dQ; |
836 |
|
|
837 |
|
Nodes[i].Run = 1; |
838 |
|
Best_Cost = (Code_Len20[0]<<16) + Run_Costs[i-1]+Cost0; |
839 |
|
for(Run=i-Run_Start; Run>0; --Run) |
840 |
|
{ |
841 |
|
const uint32_t Cost_Base = Cost0 + Run_Costs[i-Run]; |
842 |
|
const uint32_t Cost = Cost_Base + (Code_Len20[Run-1]<<16); |
843 |
|
const uint32_t lCost = Cost_Base + (Code_Len24[Run-1]<<16); |
844 |
|
|
845 |
|
/* |
846 |
|
* TODO: what about tie-breaks? Should we favor short runs or |
847 |
|
* 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 ) */ |
852 |
|
|
853 |
|
if (Cost<Best_Cost) { |
854 |
|
Best_Cost = Cost; |
855 |
|
Nodes[i].Run = Run; |
856 |
|
} |
857 |
|
|
858 |
|
if (lCost<Last_Cost) { |
859 |
|
Last_Cost = lCost; |
860 |
|
Last.Run = Run; |
861 |
|
Last_Node = i; |
862 |
|
} |
863 |
|
} |
864 |
|
if (Last_Node==i) |
865 |
|
Last.Level = Nodes[i].Level; |
866 |
|
} |
867 |
|
else /* "big" levels */ |
868 |
|
{ |
869 |
|
const uint8_t *Tbl_L1, *Tbl_L2, *Tbl_L1_Last, *Tbl_L2_Last; |
870 |
|
int Level2; |
871 |
|
int dQ1, dQ2; |
872 |
|
int Run; |
873 |
|
uint32_t Dist1,Dist2; |
874 |
|
int dDist21; |
875 |
|
|
876 |
|
if (Level1>1) { |
877 |
|
dQ1 = Level1*Mult-AC + Bias; |
878 |
|
dQ2 = dQ1 - Mult; |
879 |
|
Level2 = Level1-1; |
880 |
|
Tbl_L1 = (Level1<=24) ? B16_17_Code_Len[Level1-1] : Code_Len0; |
881 |
|
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; |
883 |
|
Tbl_L2_Last = (Level2<=6) ? B16_17_Code_Len_Last[Level2-1] : Code_Len0; |
884 |
|
} else { /* Level1<-1 */ |
885 |
|
dQ1 = Level1*Mult-AC - Bias; |
886 |
|
dQ2 = dQ1 + Mult; |
887 |
|
Level2 = Level1 + 1; |
888 |
|
Tbl_L1 = (Level1>=-24) ? B16_17_Code_Len[Level1^-1] : Code_Len0; |
889 |
|
Tbl_L2 = (Level2>=-24) ? B16_17_Code_Len[Level2^-1] : Code_Len0; |
890 |
|
Tbl_L1_Last = (Level1>=- 6) ? B16_17_Code_Len_Last[Level1^-1] : Code_Len0; |
891 |
|
Tbl_L2_Last = (Level2>=- 6) ? B16_17_Code_Len_Last[Level2^-1] : Code_Len0; |
892 |
|
} |
893 |
|
Dist1 = Lambda*dQ1*dQ1; |
894 |
|
Dist2 = Lambda*dQ2*dQ2; |
895 |
|
dDist21 = Dist2-Dist1; |
896 |
|
|
897 |
|
for(Run=i-Run_Start; Run>0; --Run) |
898 |
|
{ |
899 |
|
const uint32_t Cost_Base = Dist1 + Run_Costs[i-Run]; |
900 |
|
uint32_t Cost1, Cost2; |
901 |
|
int bLevel; |
902 |
|
|
903 |
|
/* |
904 |
|
* for sub-optimal (but slightly worth it, speed-wise) search, uncomment the following: |
905 |
|
* 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); |
910 |
|
Cost2 = Cost_Base + (Tbl_L2[Run-1]<<16) + dDist21; |
911 |
|
|
912 |
|
if (Cost2<Cost1) { |
913 |
|
Cost1 = Cost2; |
914 |
|
bLevel = Level2; |
915 |
|
} else |
916 |
|
bLevel = Level1; |
917 |
|
|
918 |
|
if (Cost1<Best_Cost) { |
919 |
|
Best_Cost = Cost1; |
920 |
|
Nodes[i].Run = Run; |
921 |
|
Nodes[i].Level = bLevel; |
922 |
|
} |
923 |
|
|
924 |
|
Cost1 = Cost_Base + (Tbl_L1_Last[Run-1]<<16); |
925 |
|
Cost2 = Cost_Base + (Tbl_L2_Last[Run-1]<<16) + dDist21; |
926 |
|
|
927 |
|
if (Cost2<Cost1) { |
928 |
|
Cost1 = Cost2; |
929 |
|
bLevel = Level2; |
930 |
|
} else |
931 |
|
bLevel = Level1; |
932 |
|
|
933 |
|
if (Cost1<Last_Cost) { |
934 |
|
Last_Cost = Cost1; |
935 |
|
Last.Run = Run; |
936 |
|
Last.Level = bLevel; |
937 |
|
Last_Node = i; |
938 |
|
} |
939 |
|
} /* end of "for Run" */ |
940 |
|
|
941 |
|
} |
942 |
|
|
943 |
|
Run_Costs[i] = Best_Cost; |
944 |
|
|
945 |
|
if (Best_Cost < Min_Cost + Dist0) { |
946 |
|
Min_Cost = Best_Cost; |
947 |
|
Run_Start = i; |
948 |
|
} |
949 |
|
else |
950 |
|
{ |
951 |
|
/* |
952 |
|
* as noticed by Michael Niedermayer (michaelni at gmx.at), there's |
953 |
|
* 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) ) |
958 |
|
Run_Start++; |
959 |
|
|
960 |
|
/* spread on preceding coeffs the cost incurred by skipping this one */ |
961 |
|
for(j=Run_Start; j<i; ++j) Run_Costs[j] += Dist0; |
962 |
|
Min_Cost += Dist0; |
963 |
|
} |
964 |
|
} |
965 |
|
|
966 |
|
if (Last_Node<0) |
967 |
|
return -1; |
968 |
|
|
969 |
|
/* reconstruct optimal sequence backward with surviving paths */ |
970 |
|
memset(Out, 0x00, 64*sizeof(*Out)); |
971 |
|
Out[Zigzag[Last_Node]] = Last.Level; |
972 |
|
i = Last_Node - Last.Run; |
973 |
|
while(i>=0) { |
974 |
|
Out[Zigzag[i]] = Nodes[i].Level; |
975 |
|
i -= Nodes[i].Run; |
976 |
|
} |
977 |
|
return Last_Node; |
978 |
|
} |
979 |
|
|
980 |
|
|
981 |
|
|
982 |
|
|
983 |
|
|
984 |
|
|
985 |
|
|
986 |
|
|
987 |
|
|
988 |
|
|
989 |
|
|
990 |
|
/* original version including heavy debugging info */ |
991 |
|
|
992 |
|
#ifdef DBGTRELL |
993 |
|
|
994 |
#define DBG 0 |
#define DBG 0 |
995 |
|
|
996 |
static 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) |
1000 |
const int16_t * const Ref = C + 6*64; |
const int16_t * const Ref = C + 6*64; |
1001 |
int Last = Max; |
int Last = Max; |
|
while(Last>=0 && C[Zigzag[Last]]==0) Last--; |
|
1002 |
int Bits = 0; |
int Bits = 0; |
1003 |
|
int Dist = 0; |
1004 |
|
int i; |
1005 |
|
uint32_t Cost; |
1006 |
|
|
1007 |
|
while(Last>=0 && C[Zigzag[Last]]==0) |
1008 |
|
Last--; |
1009 |
|
|
1010 |
if (Last>=0) { |
if (Last>=0) { |
|
Bits = 2; // CBP |
|
1011 |
int j=0, j0=0; |
int j=0, j0=0; |
1012 |
int Run, Level; |
int Run, Level; |
1013 |
|
|
1014 |
|
Bits = 2; /* CBP */ |
1015 |
while(j<Last) { |
while(j<Last) { |
1016 |
while(!C[Zigzag[j]]) j++; |
while(!C[Zigzag[j]]) |
1017 |
if (j==Last) break; |
j++; |
1018 |
|
if (j==Last) |
1019 |
|
break; |
1020 |
Level=C[Zigzag[j]]; |
Level=C[Zigzag[j]]; |
1021 |
Run = j - j0; |
Run = j - j0; |
1022 |
j0 = ++j; |
j0 = ++j; |
1023 |
if (Level>=-24 && Level<=24) Bits += B16_17_Code_Len[(Level<0) ? -Level-1 : Level-1][Run]; |
if (Level>=-24 && Level<=24) |
1024 |
else Bits += 30; |
Bits += B16_17_Code_Len[(Level<0) ? -Level-1 : Level-1][Run]; |
1025 |
|
else |
1026 |
|
Bits += 30; |
1027 |
} |
} |
1028 |
Level = C[Zigzag[Last]]; |
Level = C[Zigzag[Last]]; |
1029 |
Run = j - j0; |
Run = j - j0; |
1030 |
if (Level>=-6 && Level<=6) Bits += B16_17_Code_Len_Last[(Level<0) ? -Level-1 : Level-1][Run]; |
if (Level>=-6 && Level<=6) |
1031 |
else Bits += 30; |
Bits += B16_17_Code_Len_Last[(Level<0) ? -Level-1 : Level-1][Run]; |
1032 |
|
else |
1033 |
|
Bits += 30; |
1034 |
} |
} |
1035 |
|
|
|
int Dist = 0; |
|
|
int i; |
|
1036 |
for(i=0; i<=Last; ++i) { |
for(i=0; i<=Last; ++i) { |
1037 |
int V = C[Zigzag[i]]*Mult; |
int V = C[Zigzag[i]]*Mult; |
1038 |
if (V>0) V += Bias; |
if (V>0) |
1039 |
else if (V<0) V -= Bias; |
V += Bias; |
1040 |
|
else |
1041 |
|
if (V<0) |
1042 |
|
V -= Bias; |
1043 |
V -= Ref[Zigzag[i]]; |
V -= Ref[Zigzag[i]]; |
1044 |
Dist += V*V; |
Dist += V*V; |
1045 |
} |
} |
1046 |
uint32_t Cost = Lambda*Dist + (Bits<<16); |
Cost = Lambda*Dist + (Bits<<16); |
1047 |
if (DBG==1) |
if (DBG==1) |
1048 |
printf( " Last:%2d/%2d Cost = [(Bits=%5.0d) + Lambda*(Dist=%6.0d) = %d ] >>12= %d ", Last,Max, Bits, Dist, Cost, Cost>>12 ); |
printf( " Last:%2d/%2d Cost = [(Bits=%5.0d) + Lambda*(Dist=%6.0d) = %d ] >>12= %d ", Last,Max, Bits, Dist, Cost, Cost>>12 ); |
1049 |
return Cost; |
return Cost; |
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; |
1070 |
uint32_t Run_Costs0[64+1], * const Run_Costs = Run_Costs0 + 1; |
uint32_t Run_Costs0[64+1]; |
1071 |
|
uint32_t * const Run_Costs = Run_Costs0 + 1; |
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; |
1082 |
uint32_t Last_Cost = 0; |
uint32_t Last_Cost = 0; |
1083 |
|
|
1084 |
|
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 |
|
|
|
int i, j; |
|
|
|
|
1090 |
Non_Zero = Find_Last(Out, Zigzag, Non_Zero); |
Non_Zero = Find_Last(Out, Zigzag, Non_Zero); |
1091 |
if (Non_Zero<0) |
if (Non_Zero<0) |
1092 |
return -1; |
return -1; |
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; |
1106 |
|
uint32_t Cost0; |
1107 |
|
|
1108 |
if (AC<0) { |
if (AC<0) { |
1109 |
Nodes[i].Level = -1; |
Nodes[i].Level = -1; |
1112 |
Nodes[i].Level = 1; |
Nodes[i].Level = 1; |
1113 |
dQ = Lev0 - AC; |
dQ = Lev0 - AC; |
1114 |
} |
} |
1115 |
const uint32_t Cost0 = Lambda*dQ*dQ; |
Cost0 = Lambda*dQ*dQ; |
1116 |
|
|
1117 |
Nodes[i].Run = 1; |
Nodes[i].Run = 1; |
1118 |
Best_Cost = (Code_Len20[0]<<16) + Run_Costs[i-1]+Cost0; |
Best_Cost = (Code_Len20[0]<<16) + Run_Costs[i-1]+Cost0; |
1120 |
{ |
{ |
1121 |
const uint32_t Cost_Base = Cost0 + Run_Costs[i-Run]; |
const uint32_t Cost_Base = Cost0 + Run_Costs[i-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 |
// TODO: what about tie-breaks? Should we favor short runs or |
const uint32_t lCost = Cost_Base + (Code_Len24[Run-1]<<16); |
1124 |
// long runs? Although the error is the same, it would not be |
|
1125 |
// spread the same way along high and low frequencies... |
/* |
1126 |
if (Cost<Best_Cost) |
* TODO: what about tie-breaks? Should we favor short runs or |
1127 |
{ |
* 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) { |
1131 |
Best_Cost = Cost; |
Best_Cost = Cost; |
1132 |
Nodes[i].Run = Run; |
Nodes[i].Run = Run; |
1133 |
} |
} |
1134 |
const uint32_t lCost = Cost_Base + (Code_Len24[Run-1]<<16); |
|
1135 |
if (lCost<Last_Cost) |
if (lCost<Last_Cost) { |
|
{ |
|
1136 |
Last_Cost = lCost; |
Last_Cost = lCost; |
1137 |
Last.Run = Run; |
Last.Run = Run; |
1138 |
Last_Node = i; |
Last_Node = i; |
1139 |
} |
} |
1140 |
} |
} |
1141 |
if (Last_Node==i) Last.Level = Nodes[i].Level; |
if (Last_Node==i) |
1142 |
|
Last.Level = Nodes[i].Level; |
1143 |
|
|
1144 |
if (DBG==1) { |
if (DBG==1) { |
1145 |
Run_Costs[i] = Best_Cost; |
Run_Costs[i] = Best_Cost; |
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; |
1163 |
int dQ1, dQ2; |
int dQ1, dQ2; |
1164 |
int Run; |
int Run; |
1165 |
|
uint32_t Dist1,Dist2; |
1166 |
|
int dDist21; |
1167 |
|
|
1168 |
if (Level1>1) { |
if (Level1>1) { |
1169 |
dQ1 = Level1*Mult-AC + Bias; |
dQ1 = Level1*Mult-AC + Bias; |
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; |
1182 |
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; |
1183 |
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; |
1184 |
} |
} |
1185 |
const uint32_t Dist1 = Lambda*dQ1*dQ1; |
Dist1 = Lambda*dQ1*dQ1; |
1186 |
const uint32_t Dist2 = Lambda*dQ2*dQ2; |
Dist2 = Lambda*dQ2*dQ2; |
1187 |
const int dDist21 = Dist2-Dist1; |
dDist21 = Dist2-Dist1; |
1188 |
|
|
1189 |
for(Run=i-Run_Start; Run>0; --Run) |
for(Run=i-Run_Start; Run>0; --Run) |
1190 |
{ |
{ |
1191 |
const uint32_t Cost_Base = Dist1 + Run_Costs[i-Run]; |
const uint32_t Cost_Base = Dist1 + Run_Costs[i-Run]; |
|
|
|
|
// for sub-optimal (but slightly worth it, speed-wise) search, uncomment the following: |
|
|
// if (Cost_Base>=Best_Cost) continue; |
|
|
|
|
1192 |
uint32_t Cost1, Cost2; |
uint32_t Cost1, Cost2; |
1193 |
int bLevel; |
int bLevel; |
1194 |
|
|
1195 |
|
/* |
1196 |
|
* 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 |
|
|
1202 |
if (Cost2<Cost1) { Cost1 = Cost2; bLevel = Level2; } |
if (Cost2<Cost1) { |
1203 |
else bLevel = Level1; |
Cost1 = Cost2; |
1204 |
|
bLevel = Level2; |
1205 |
|
} else |
1206 |
|
bLevel = Level1; |
1207 |
|
|
1208 |
if (Cost1<Best_Cost) |
if (Cost1<Best_Cost) { |
|
{ |
|
1209 |
Best_Cost = Cost1; |
Best_Cost = Cost1; |
1210 |
Nodes[i].Run = Run; |
Nodes[i].Run = Run; |
1211 |
Nodes[i].Level = bLevel; |
Nodes[i].Level = bLevel; |
1214 |
Cost1 = Cost_Base + (Tbl_L1_Last[Run-1]<<16); |
Cost1 = Cost_Base + (Tbl_L1_Last[Run-1]<<16); |
1215 |
Cost2 = Cost_Base + (Tbl_L2_Last[Run-1]<<16) + dDist21; |
Cost2 = Cost_Base + (Tbl_L2_Last[Run-1]<<16) + dDist21; |
1216 |
|
|
1217 |
if (Cost2<Cost1) { Cost1 = Cost2; bLevel = Level2; } |
if (Cost2<Cost1) { |
1218 |
else bLevel = Level1; |
Cost1 = Cost2; |
1219 |
if (Cost1<Last_Cost) |
bLevel = Level2; |
1220 |
{ |
} else |
1221 |
|
bLevel = Level1; |
1222 |
|
|
1223 |
|
if (Cost1<Last_Cost) { |
1224 |
Last_Cost = Cost1; |
Last_Cost = Cost1; |
1225 |
Last.Run = Run; |
Last.Run = Run; |
1226 |
Last.Level = bLevel; |
Last.Level = bLevel; |
1227 |
Last_Node = i; |
Last_Node = i; |
1228 |
} |
} |
1229 |
} |
} /* 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 |
bzero(Out, 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; |
1286 |
while(i>=0) { |
while(i>=0) { |
1301 |
} |
} |
1302 |
|
|
1303 |
#undef DBG |
#undef DBG |
1304 |
|
|
1305 |
|
#endif |