1 |
/***************************************************************************** |
/***************************************************************************** |
2 |
* |
* |
3 |
* XVID MPEG-4 VIDEO CODEC |
* XVID MPEG-4 VIDEO CODEC |
4 |
* - MacroBlock transfer and quantization - |
* - MB Transfert/Quantization functions - |
5 |
* |
* |
6 |
* Copyright(C) 2002-2001 Michael Militzer <isibaar@xvid.org> |
* Copyright(C) 2001-2003 Peter Ross <pross@xvid.org> |
7 |
* 2002-2001 Peter Ross <pross@xvid.org> |
* 2001-2003 Michael Militzer <isibaar@xvid.org> |
8 |
* |
* 2003 Edouard Gomez <ed.gomez@free.fr> |
|
* This program is an implementation of a part of one or more MPEG-4 |
|
|
* Video tools as specified in ISO/IEC 14496-2 standard. Those intending |
|
|
* to use this software module in hardware or software products are |
|
|
* advised that its use may infringe existing patents or copyrights, and |
|
|
* any such use would be at such party's own risk. The original |
|
|
* developer of this software module and his/her company, and subsequent |
|
|
* editors and their companies, will have no liability for use of this |
|
|
* software or modifications or derivatives thereof. |
|
9 |
* |
* |
10 |
* This program is free software; you can redistribute it and/or modify |
* This program is free software; you can redistribute it and/or modify |
11 |
* it under the terms of the GNU General Public License as published by |
* it under the terms of the GNU General Public License as published by |
26 |
****************************************************************************/ |
****************************************************************************/ |
27 |
|
|
28 |
#include <string.h> |
#include <string.h> |
29 |
|
#include <stdlib.h> |
30 |
|
|
31 |
#include "../portab.h" |
#include "../portab.h" |
32 |
#include "mbfunctions.h" |
#include "mbfunctions.h" |
34 |
#include "../global.h" |
#include "../global.h" |
35 |
#include "mem_transfer.h" |
#include "mem_transfer.h" |
36 |
#include "timer.h" |
#include "timer.h" |
37 |
|
#include "../bitstream/mbcoding.h" |
38 |
|
#include "../bitstream/zigzag.h" |
39 |
#include "../dct/fdct.h" |
#include "../dct/fdct.h" |
40 |
#include "../dct/idct.h" |
#include "../dct/idct.h" |
41 |
#include "../quant/quant_mpeg4.h" |
#include "../quant/quant_mpeg4.h" |
42 |
#include "../quant/quant_h263.h" |
#include "../quant/quant_h263.h" |
43 |
#include "../encoder.h" |
#include "../encoder.h" |
44 |
|
|
45 |
#define MIN(X, Y) ((X)<(Y)?(X):(Y)) |
#include "../image/reduced.h" |
|
#define MAX(X, Y) ((X)>(Y)?(X):(Y)) |
|
|
|
|
|
#define TOOSMALL_LIMIT 3 /* skip blocks having a coefficient sum below this value */ |
|
46 |
|
|
47 |
/* this isnt pretty, but its better than 20 ifdefs */ |
MBFIELDTEST_PTR MBFieldTest; |
48 |
|
|
|
void |
|
|
MBTransQuantIntra(const MBParam * pParam, |
|
|
FRAMEINFO * frame, |
|
|
MACROBLOCK * pMB, |
|
|
const uint32_t x_pos, |
|
|
const uint32_t y_pos, |
|
|
int16_t data[6 * 64], |
|
|
int16_t qcoeff[6 * 64]) |
|
|
{ |
|
|
|
|
|
uint32_t stride = pParam->edged_width; |
|
|
uint32_t stride2 = stride / 2; |
|
|
uint32_t next_block = stride * 8; |
|
|
uint32_t i; |
|
|
uint32_t iQuant = frame->quant; |
|
|
uint8_t *pY_Cur, *pU_Cur, *pV_Cur; |
|
|
IMAGE *pCurrent = &frame->image; |
|
|
|
|
|
pY_Cur = pCurrent->y + (y_pos << 4) * stride + (x_pos << 4); |
|
|
pU_Cur = pCurrent->u + (y_pos << 3) * stride2 + (x_pos << 3); |
|
|
pV_Cur = pCurrent->v + (y_pos << 3) * stride2 + (x_pos << 3); |
|
|
|
|
|
start_timer(); |
|
|
transfer_8to16copy(&data[0 * 64], pY_Cur, stride); |
|
|
transfer_8to16copy(&data[1 * 64], pY_Cur + 8, stride); |
|
|
transfer_8to16copy(&data[2 * 64], pY_Cur + next_block, stride); |
|
|
transfer_8to16copy(&data[3 * 64], pY_Cur + next_block + 8, stride); |
|
|
transfer_8to16copy(&data[4 * 64], pU_Cur, stride2); |
|
|
transfer_8to16copy(&data[5 * 64], pV_Cur, stride2); |
|
|
stop_transfer_timer(); |
|
|
|
|
|
start_timer(); |
|
|
pMB->field_dct = 0; |
|
|
if ((frame->global_flags & XVID_INTERLACING) && |
|
|
(x_pos>0) && (x_pos<pParam->mb_width-1) && |
|
|
(y_pos>0) && (y_pos<pParam->mb_height-1)) { |
|
|
pMB->field_dct = MBDecideFieldDCT(data); |
|
|
} |
|
|
stop_interlacing_timer(); |
|
|
|
|
|
for (i = 0; i < 6; i++) { |
|
|
uint32_t iDcScaler = get_dc_scaler(iQuant, i < 4); |
|
|
|
|
|
start_timer(); |
|
|
fdct(&data[i * 64]); |
|
|
stop_dct_timer(); |
|
|
|
|
|
if (pParam->m_quant_type == H263_QUANT) { |
|
|
start_timer(); |
|
|
quant_intra(&qcoeff[i * 64], &data[i * 64], iQuant, iDcScaler); |
|
|
stop_quant_timer(); |
|
|
|
|
|
start_timer(); |
|
|
dequant_intra(&data[i * 64], &qcoeff[i * 64], iQuant, iDcScaler); |
|
|
stop_iquant_timer(); |
|
|
} else { |
|
|
start_timer(); |
|
|
quant4_intra(&qcoeff[i * 64], &data[i * 64], iQuant, iDcScaler); |
|
|
stop_quant_timer(); |
|
|
|
|
|
start_timer(); |
|
|
dequant4_intra(&data[i * 64], &qcoeff[i * 64], iQuant, iDcScaler); |
|
|
stop_iquant_timer(); |
|
|
} |
|
|
|
|
|
start_timer(); |
|
|
idct(&data[i * 64]); |
|
|
stop_idct_timer(); |
|
|
} |
|
|
|
|
|
if (pMB->field_dct) { |
|
|
next_block = stride; |
|
|
stride *= 2; |
|
|
} |
|
|
|
|
|
start_timer(); |
|
|
transfer_16to8copy(pY_Cur, &data[0 * 64], stride); |
|
|
transfer_16to8copy(pY_Cur + 8, &data[1 * 64], stride); |
|
|
transfer_16to8copy(pY_Cur + next_block, &data[2 * 64], stride); |
|
|
transfer_16to8copy(pY_Cur + next_block + 8, &data[3 * 64], stride); |
|
|
transfer_16to8copy(pU_Cur, &data[4 * 64], stride2); |
|
|
transfer_16to8copy(pV_Cur, &data[5 * 64], stride2); |
|
|
stop_transfer_timer(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
uint8_t |
|
|
MBTransQuantInter(const MBParam * pParam, |
|
|
FRAMEINFO * frame, |
|
|
MACROBLOCK * pMB, |
|
|
const uint32_t x_pos, |
|
|
const uint32_t y_pos, |
|
|
int16_t data[6 * 64], |
|
|
int16_t qcoeff[6 * 64]) |
|
|
{ |
|
|
|
|
|
uint32_t stride = pParam->edged_width; |
|
|
uint32_t stride2 = stride / 2; |
|
|
uint32_t next_block = stride * 8; |
|
|
uint32_t i; |
|
|
uint32_t iQuant = frame->quant; |
|
|
uint8_t *pY_Cur, *pU_Cur, *pV_Cur; |
|
|
uint8_t cbp = 0; |
|
|
uint32_t sum; |
|
|
IMAGE *pCurrent = &frame->image; |
|
|
|
|
|
pY_Cur = pCurrent->y + (y_pos << 4) * stride + (x_pos << 4); |
|
|
pU_Cur = pCurrent->u + (y_pos << 3) * stride2 + (x_pos << 3); |
|
|
pV_Cur = pCurrent->v + (y_pos << 3) * stride2 + (x_pos << 3); |
|
|
|
|
|
start_timer(); |
|
|
pMB->field_dct = 0; |
|
|
if ((frame->global_flags & XVID_INTERLACING) && |
|
|
(x_pos>0) && (x_pos<pParam->mb_width-1) && |
|
|
(y_pos>0) && (y_pos<pParam->mb_height-1)) { |
|
|
pMB->field_dct = MBDecideFieldDCT(data); |
|
|
} |
|
|
stop_interlacing_timer(); |
|
|
|
|
|
for (i = 0; i < 6; i++) { |
|
49 |
/* |
/* |
50 |
* no need to transfer 8->16-bit |
* Skip blocks having a coefficient sum below this value. This value will be |
51 |
* (this is performed already in motion compensation) |
* corrected according to the MB quantizer to avoid artifacts for quant==1 |
52 |
*/ |
*/ |
53 |
start_timer(); |
#define PVOP_TOOSMALL_LIMIT 1 |
54 |
fdct(&data[i * 64]); |
#define BVOP_TOOSMALL_LIMIT 3 |
|
stop_dct_timer(); |
|
|
|
|
|
if (pParam->m_quant_type == 0) { |
|
|
start_timer(); |
|
|
sum = quant_inter(&qcoeff[i * 64], &data[i * 64], iQuant); |
|
|
stop_quant_timer(); |
|
|
} else { |
|
|
start_timer(); |
|
|
sum = quant4_inter(&qcoeff[i * 64], &data[i * 64], iQuant); |
|
|
stop_quant_timer(); |
|
|
} |
|
|
|
|
|
if ((sum >= TOOSMALL_LIMIT) || (qcoeff[i*64] != 0) || |
|
|
(qcoeff[i*64+1] != 0) || (qcoeff[i*64+8] != 0)) { |
|
|
|
|
|
if (pParam->m_quant_type == H263_QUANT) { |
|
|
start_timer(); |
|
|
dequant_inter(&data[i * 64], &qcoeff[i * 64], iQuant); |
|
|
stop_iquant_timer(); |
|
|
} else { |
|
|
start_timer(); |
|
|
dequant4_inter(&data[i * 64], &qcoeff[i * 64], iQuant); |
|
|
stop_iquant_timer(); |
|
|
} |
|
|
|
|
|
cbp |= 1 << (5 - i); |
|
55 |
|
|
56 |
start_timer(); |
/***************************************************************************** |
57 |
idct(&data[i * 64]); |
* Local functions |
58 |
stop_idct_timer(); |
****************************************************************************/ |
|
} |
|
|
} |
|
|
|
|
|
if (pMB->field_dct) { |
|
|
next_block = stride; |
|
|
stride *= 2; |
|
|
} |
|
|
|
|
|
start_timer(); |
|
|
if (cbp & 32) |
|
|
transfer_16to8add(pY_Cur, &data[0 * 64], stride); |
|
|
if (cbp & 16) |
|
|
transfer_16to8add(pY_Cur + 8, &data[1 * 64], stride); |
|
|
if (cbp & 8) |
|
|
transfer_16to8add(pY_Cur + next_block, &data[2 * 64], stride); |
|
|
if (cbp & 4) |
|
|
transfer_16to8add(pY_Cur + next_block + 8, &data[3 * 64], stride); |
|
|
if (cbp & 2) |
|
|
transfer_16to8add(pU_Cur, &data[4 * 64], stride2); |
|
|
if (cbp & 1) |
|
|
transfer_16to8add(pV_Cur, &data[5 * 64], stride2); |
|
|
stop_transfer_timer(); |
|
|
|
|
|
return cbp; |
|
|
|
|
|
} |
|
|
|
|
|
void |
|
|
MBTransQuantIntra2(const MBParam * pParam, |
|
|
FRAMEINFO * frame, |
|
|
MACROBLOCK * pMB, |
|
|
const uint32_t x_pos, |
|
|
const uint32_t y_pos, |
|
|
int16_t data[6 * 64], |
|
|
int16_t qcoeff[6 * 64]) |
|
|
{ |
|
|
MBTrans(pParam,frame,pMB,x_pos,y_pos,data); |
|
|
MBfDCT(pParam,frame,pMB,data); |
|
|
MBQuantIntra(pParam,frame,pMB,data,qcoeff); |
|
|
MBDeQuantIntra(pParam,frame->quant,data,qcoeff); |
|
|
MBiDCT(data,0x3F); |
|
|
MBTransAdd(pParam,frame,pMB,x_pos,y_pos,data,0x3F); |
|
|
} |
|
|
|
|
|
|
|
|
uint8_t |
|
|
MBTransQuantInter2(const MBParam * pParam, |
|
|
FRAMEINFO * frame, |
|
|
MACROBLOCK * pMB, |
|
|
const uint32_t x_pos, |
|
|
const uint32_t y_pos, |
|
|
int16_t data[6 * 64], |
|
|
int16_t qcoeff[6 * 64]) |
|
|
{ |
|
|
uint8_t cbp; |
|
|
|
|
|
/* there is no MBTrans for Inter block, that's done in motion compensation already */ |
|
|
|
|
|
MBfDCT(pParam,frame,pMB,data); |
|
|
cbp = MBQuantInter(pParam,frame->quant,data,qcoeff); |
|
|
MBDeQuantInter(pParam,frame->quant,data,qcoeff,cbp); |
|
|
MBiDCT(data,cbp); |
|
|
MBTransAdd(pParam,frame,pMB,x_pos,y_pos,data,cbp); |
|
|
|
|
|
return cbp; |
|
|
} |
|
59 |
|
|
60 |
uint8_t |
/* permute block and return field dct choice */ |
61 |
MBTransQuantInterBVOP(const MBParam * pParam, |
static __inline uint32_t |
62 |
FRAMEINFO * frame, |
MBDecideFieldDCT(int16_t data[6 * 64]) |
|
MACROBLOCK * pMB, |
|
|
int16_t data[6 * 64], |
|
|
int16_t qcoeff[6 * 64]) |
|
63 |
{ |
{ |
64 |
uint8_t cbp; |
uint32_t field = MBFieldTest(data); |
|
|
|
|
/* there is no MBTrans for Inter block, that's done in motion compensation already */ |
|
65 |
|
|
66 |
MBfDCT(pParam,frame,pMB,data); |
if (field) |
67 |
cbp = MBQuantInter(pParam,frame->quant,data,qcoeff); |
MBFrameToField(data); |
|
|
|
|
/* we don't have to DeQuant, iDCT and Transfer back data for B-frames */ |
|
68 |
|
|
69 |
return cbp; |
return field; |
70 |
} |
} |
71 |
|
|
72 |
|
/* Performs Forward DCT on all blocks */ |
73 |
void |
static __inline void |
74 |
MBfDCT(const MBParam * pParam, |
MBfDCT(const MBParam * const pParam, |
75 |
FRAMEINFO * frame, |
const FRAMEINFO * const frame, |
76 |
MACROBLOCK * pMB, |
MACROBLOCK * const pMB, |
77 |
|
uint32_t x_pos, |
78 |
|
uint32_t y_pos, |
79 |
int16_t data[6 * 64]) |
int16_t data[6 * 64]) |
80 |
{ |
{ |
81 |
int i; |
/* Handles interlacing */ |
|
|
|
82 |
start_timer(); |
start_timer(); |
83 |
pMB->field_dct = 0; |
pMB->field_dct = 0; |
84 |
if ((frame->global_flags & XVID_INTERLACING)) { |
if ((frame->vol_flags & XVID_VOL_INTERLACING) && |
85 |
|
(x_pos>0) && (x_pos<pParam->mb_width-1) && |
86 |
|
(y_pos>0) && (y_pos<pParam->mb_height-1)) { |
87 |
pMB->field_dct = MBDecideFieldDCT(data); |
pMB->field_dct = MBDecideFieldDCT(data); |
88 |
} |
} |
89 |
stop_interlacing_timer(); |
stop_interlacing_timer(); |
90 |
|
|
91 |
for (i = 0; i < 6; i++) { |
/* Perform DCT */ |
92 |
start_timer(); |
start_timer(); |
93 |
fdct(&data[i * 64]); |
fdct(&data[0 * 64]); |
94 |
|
fdct(&data[1 * 64]); |
95 |
|
fdct(&data[2 * 64]); |
96 |
|
fdct(&data[3 * 64]); |
97 |
|
fdct(&data[4 * 64]); |
98 |
|
fdct(&data[5 * 64]); |
99 |
stop_dct_timer(); |
stop_dct_timer(); |
100 |
} |
} |
|
} |
|
101 |
|
|
102 |
void |
/* Performs Inverse DCT on all blocks */ |
103 |
MBQuantDeQuantIntra(const MBParam * pParam, |
static __inline void |
104 |
FRAMEINFO * frame, |
MBiDCT(int16_t data[6 * 64], |
105 |
MACROBLOCK * pMB, |
const uint8_t cbp) |
|
int16_t qcoeff[6 * 64], |
|
|
int16_t data[6*64]) |
|
106 |
{ |
{ |
|
int i; |
|
|
int iQuant = frame->quant; |
|
|
|
|
|
start_timer(); |
|
|
pMB->field_dct = 0; |
|
|
if ((frame->global_flags & XVID_INTERLACING)) { |
|
|
pMB->field_dct = MBDecideFieldDCT(data); |
|
|
} |
|
|
stop_interlacing_timer(); |
|
|
|
|
|
for (i = 0; i < 6; i++) { |
|
|
uint32_t iDcScaler = get_dc_scaler(iQuant, i < 4); |
|
|
|
|
|
if (pParam->m_quant_type == H263_QUANT) { |
|
|
start_timer(); |
|
|
quant_intra(&qcoeff[i * 64], &data[i * 64], iQuant, iDcScaler); |
|
|
stop_quant_timer(); |
|
|
|
|
|
start_timer(); |
|
|
dequant_intra(&data[i * 64], &qcoeff[i * 64], iQuant, iDcScaler); |
|
|
stop_iquant_timer(); |
|
|
} else { |
|
|
start_timer(); |
|
|
quant4_intra(&qcoeff[i * 64], &data[i * 64], iQuant, iDcScaler); |
|
|
stop_quant_timer(); |
|
|
|
|
107 |
start_timer(); |
start_timer(); |
108 |
dequant4_intra(&data[i * 64], &qcoeff[i * 64], iQuant, iDcScaler); |
if(cbp & (1 << (5 - 0))) idct(&data[0 * 64]); |
109 |
stop_iquant_timer(); |
if(cbp & (1 << (5 - 1))) idct(&data[1 * 64]); |
110 |
} |
if(cbp & (1 << (5 - 2))) idct(&data[2 * 64]); |
111 |
} |
if(cbp & (1 << (5 - 3))) idct(&data[3 * 64]); |
112 |
|
if(cbp & (1 << (5 - 4))) idct(&data[4 * 64]); |
113 |
|
if(cbp & (1 << (5 - 5))) idct(&data[5 * 64]); |
114 |
|
stop_idct_timer(); |
115 |
} |
} |
116 |
|
|
117 |
void |
/* Quantize all blocks -- Intra mode */ |
118 |
|
static __inline void |
119 |
MBQuantIntra(const MBParam * pParam, |
MBQuantIntra(const MBParam * pParam, |
120 |
FRAMEINFO * frame, |
const FRAMEINFO * const frame, |
121 |
MACROBLOCK *pMB, |
const MACROBLOCK * pMB, |
122 |
int16_t data[6 * 64], |
int16_t qcoeff[6 * 64], |
123 |
int16_t qcoeff[6 * 64]) |
int16_t data[6*64]) |
124 |
{ |
{ |
125 |
int i; |
int i; |
|
int iQuant = frame->quant; |
|
|
|
|
|
start_timer(); |
|
|
pMB->field_dct = 0; |
|
|
if ((frame->global_flags & XVID_INTERLACING)) { |
|
|
pMB->field_dct = MBDecideFieldDCT(data); |
|
|
} |
|
|
stop_interlacing_timer(); |
|
126 |
|
|
127 |
for (i = 0; i < 6; i++) { |
for (i = 0; i < 6; i++) { |
128 |
uint32_t iDcScaler = get_dc_scaler(iQuant, i < 4); |
uint32_t iDcScaler = get_dc_scaler(pMB->quant, i < 4); |
129 |
|
|
130 |
if (pParam->m_quant_type == H263_QUANT) { |
/* Quantize the block */ |
131 |
start_timer(); |
start_timer(); |
132 |
quant_intra(&qcoeff[i * 64], &data[i * 64], iQuant, iDcScaler); |
if (!(pParam->vol_flags & XVID_VOL_MPEGQUANT)) { |
133 |
stop_quant_timer(); |
quant_intra(&data[i * 64], &qcoeff[i * 64], pMB->quant, iDcScaler); |
134 |
} else { |
} else { |
135 |
start_timer(); |
quant4_intra(&data[i * 64], &qcoeff[i * 64], pMB->quant, iDcScaler); |
|
quant4_intra(&qcoeff[i * 64], &data[i * 64], iQuant, iDcScaler); |
|
|
stop_quant_timer(); |
|
136 |
} |
} |
137 |
|
stop_quant_timer(); |
138 |
} |
} |
139 |
} |
} |
140 |
|
|
141 |
void |
/* DeQuantize all blocks -- Intra mode */ |
142 |
|
static __inline void |
143 |
MBDeQuantIntra(const MBParam * pParam, |
MBDeQuantIntra(const MBParam * pParam, |
144 |
const int iQuant, |
const int iQuant, |
145 |
int16_t qcoeff[6 * 64], |
int16_t qcoeff[6 * 64], |
150 |
for (i = 0; i < 6; i++) { |
for (i = 0; i < 6; i++) { |
151 |
uint32_t iDcScaler = get_dc_scaler(iQuant, i < 4); |
uint32_t iDcScaler = get_dc_scaler(iQuant, i < 4); |
152 |
|
|
|
if (pParam->m_quant_type == H263_QUANT) { |
|
|
start_timer(); |
|
|
dequant_intra(&data[i * 64], &qcoeff[i * 64], iQuant, iDcScaler); |
|
|
stop_iquant_timer(); |
|
|
} else { |
|
153 |
start_timer(); |
start_timer(); |
154 |
dequant4_intra(&data[i * 64], &qcoeff[i * 64], iQuant, iDcScaler); |
if (!(pParam->vol_flags & XVID_VOL_MPEGQUANT)) |
155 |
|
dequant_intra(&qcoeff[i * 64], &data[i * 64], iQuant, iDcScaler); |
156 |
|
else |
157 |
|
dequant4_intra(&qcoeff[i * 64], &data[i * 64], iQuant, iDcScaler); |
158 |
stop_iquant_timer(); |
stop_iquant_timer(); |
159 |
} |
} |
160 |
} |
} |
|
} |
|
161 |
|
|
162 |
uint8_t |
|
163 |
|
static int |
164 |
|
dct_quantize_trellis_h263_c(int16_t *const Out, const int16_t *const In, int Q, const uint16_t * const Zigzag, int Non_Zero); |
165 |
|
|
166 |
|
static int |
167 |
|
dct_quantize_trellis_mpeg_c(int16_t *const Out, const int16_t *const In, int Q, const uint16_t * const Zigzag, int Non_Zero); |
168 |
|
|
169 |
|
|
170 |
|
/* Quantize all blocks -- Inter mode */ |
171 |
|
static __inline uint8_t |
172 |
MBQuantInter(const MBParam * pParam, |
MBQuantInter(const MBParam * pParam, |
173 |
const int iQuant, |
const FRAMEINFO * const frame, |
174 |
|
const MACROBLOCK * pMB, |
175 |
int16_t data[6 * 64], |
int16_t data[6 * 64], |
176 |
int16_t qcoeff[6 * 64]) |
int16_t qcoeff[6 * 64], |
177 |
|
int bvop, |
178 |
|
int limit) |
179 |
{ |
{ |
180 |
|
|
181 |
int i; |
int i; |
182 |
uint8_t cbp = 0; |
uint8_t cbp = 0; |
183 |
int sum; |
int sum; |
184 |
|
int code_block; |
185 |
|
|
186 |
for (i = 0; i < 6; i++) { |
for (i = 0; i < 6; i++) { |
187 |
|
|
188 |
if (pParam->m_quant_type == 0) { |
/* Quantize the block */ |
189 |
start_timer(); |
start_timer(); |
190 |
sum = quant_inter(&qcoeff[i * 64], &data[i * 64], iQuant); |
if (!(pParam->vol_flags & XVID_VOL_MPEGQUANT)) { |
191 |
stop_quant_timer(); |
sum = quant_inter(&qcoeff[i*64], &data[i*64], pMB->quant); |
192 |
|
if ( (sum) && (frame->vop_flags & XVID_VOP_TRELLISQUANT) ) { |
193 |
|
sum = dct_quantize_trellis_h263_c(&qcoeff[i*64], &data[i*64], pMB->quant, &scan_tables[0][0], 63)+1; |
194 |
|
limit = 1; |
195 |
|
} |
196 |
} else { |
} else { |
197 |
start_timer(); |
sum = quant4_inter(&qcoeff[i * 64], &data[i * 64], pMB->quant); |
198 |
sum = quant4_inter(&qcoeff[i * 64], &data[i * 64], iQuant); |
// if ( (sum) && (frame->vop_flags & XVID_VOP_TRELLISQUANT) ) |
199 |
stop_quant_timer(); |
// sum = dct_quantize_trellis_mpeg_c (&qcoeff[i*64], &data[i*64], pMB->quant)+1; |
200 |
} |
} |
201 |
|
stop_quant_timer(); |
202 |
|
|
203 |
|
/* |
204 |
|
* We code the block if the sum is higher than the limit and if the first |
205 |
|
* two AC coefficients in zig zag order are not zero. |
206 |
|
*/ |
207 |
|
code_block = 0; |
208 |
|
if ((sum >= limit) || (qcoeff[i*64+1] != 0) || (qcoeff[i*64+8] != 0)) { |
209 |
|
code_block = 1; |
210 |
|
} else { |
211 |
|
|
212 |
if (sum >= TOOSMALL_LIMIT) { // skip block ? |
if (bvop && (pMB->mode == MODE_DIRECT || pMB->mode == MODE_DIRECT_NO4V)) { |
213 |
cbp |= 1 << (5 - i); |
/* dark blocks prevention for direct mode */ |
214 |
|
if ((qcoeff[i*64] < -1) || (qcoeff[i*64] > 0)) |
215 |
|
code_block = 1; |
216 |
|
} else { |
217 |
|
/* not direct mode */ |
218 |
|
if (qcoeff[i*64] != 0) |
219 |
|
code_block = 1; |
220 |
} |
} |
221 |
} |
} |
222 |
return cbp; |
|
223 |
|
/* Set the corresponding cbp bit */ |
224 |
|
cbp |= code_block << (5 - i); |
225 |
} |
} |
226 |
|
|
227 |
void |
return(cbp); |
228 |
|
} |
229 |
|
|
230 |
|
/* DeQuantize all blocks -- Inter mode */ |
231 |
|
static __inline void |
232 |
MBDeQuantInter( const MBParam * pParam, |
MBDeQuantInter( const MBParam * pParam, |
233 |
const int iQuant, |
const int iQuant, |
234 |
int16_t data[6 * 64], |
int16_t data[6 * 64], |
238 |
int i; |
int i; |
239 |
|
|
240 |
for (i = 0; i < 6; i++) { |
for (i = 0; i < 6; i++) { |
241 |
if (cbp & (1 << (5 - i))) |
if (cbp & (1 << (5 - i))) { |
|
{ |
|
|
if (pParam->m_quant_type == H263_QUANT) { |
|
242 |
start_timer(); |
start_timer(); |
243 |
|
if (!(pParam->vol_flags & XVID_VOL_MPEGQUANT)) |
244 |
dequant_inter(&data[i * 64], &qcoeff[i * 64], iQuant); |
dequant_inter(&data[i * 64], &qcoeff[i * 64], iQuant); |
245 |
stop_iquant_timer(); |
else |
|
} else { |
|
|
start_timer(); |
|
246 |
dequant4_inter(&data[i * 64], &qcoeff[i * 64], iQuant); |
dequant4_inter(&data[i * 64], &qcoeff[i * 64], iQuant); |
247 |
stop_iquant_timer(); |
stop_iquant_timer(); |
248 |
} |
} |
249 |
} |
} |
250 |
} |
} |
|
} |
|
|
|
|
|
void |
|
|
MBiDCT( int16_t data[6 * 64], |
|
|
const uint8_t cbp) |
|
|
{ |
|
|
int i; |
|
|
|
|
|
for (i = 0; i < 6; i++) { |
|
|
if (cbp & (1 << (5 - i))) |
|
|
{ |
|
|
start_timer(); |
|
|
idct(&data[i * 64]); |
|
|
stop_idct_timer(); |
|
251 |
|
|
252 |
} |
typedef void (transfer_operation_8to16_t) (int16_t *Dst, const uint8_t *Src, int BpS); |
253 |
} |
typedef void (transfer_operation_16to8_t) (uint8_t *Dst, const int16_t *Src, int BpS); |
|
} |
|
254 |
|
|
255 |
|
|
256 |
void |
static __inline void |
257 |
MBTrans(const MBParam * pParam, |
MBTrans8to16(const MBParam * const pParam, |
258 |
FRAMEINFO * frame, |
const FRAMEINFO * const frame, |
259 |
MACROBLOCK * pMB, |
const MACROBLOCK * const pMB, |
260 |
const uint32_t x_pos, |
const uint32_t x_pos, |
261 |
const uint32_t y_pos, |
const uint32_t y_pos, |
262 |
int16_t data[6 * 64]) |
int16_t data[6 * 64]) |
264 |
uint32_t stride = pParam->edged_width; |
uint32_t stride = pParam->edged_width; |
265 |
uint32_t stride2 = stride / 2; |
uint32_t stride2 = stride / 2; |
266 |
uint32_t next_block = stride * 8; |
uint32_t next_block = stride * 8; |
267 |
|
int32_t cst; |
268 |
uint8_t *pY_Cur, *pU_Cur, *pV_Cur; |
uint8_t *pY_Cur, *pU_Cur, *pV_Cur; |
269 |
IMAGE *pCurrent = &frame->image; |
const IMAGE * const pCurrent = &frame->image; |
270 |
|
transfer_operation_8to16_t *transfer_op = NULL; |
271 |
|
|
272 |
|
if ((frame->vop_flags & XVID_VOP_REDUCED)) { |
273 |
|
|
274 |
|
/* Image pointers */ |
275 |
|
pY_Cur = pCurrent->y + (y_pos << 5) * stride + (x_pos << 5); |
276 |
|
pU_Cur = pCurrent->u + (y_pos << 4) * stride2 + (x_pos << 4); |
277 |
|
pV_Cur = pCurrent->v + (y_pos << 4) * stride2 + (x_pos << 4); |
278 |
|
|
279 |
|
/* Block size */ |
280 |
|
cst = 16; |
281 |
|
|
282 |
|
/* Operation function */ |
283 |
|
transfer_op = (transfer_operation_8to16_t*)filter_18x18_to_8x8; |
284 |
|
} else { |
285 |
|
|
286 |
|
/* Image pointers */ |
287 |
pY_Cur = pCurrent->y + (y_pos << 4) * stride + (x_pos << 4); |
pY_Cur = pCurrent->y + (y_pos << 4) * stride + (x_pos << 4); |
288 |
pU_Cur = pCurrent->u + (y_pos << 3) * stride2 + (x_pos << 3); |
pU_Cur = pCurrent->u + (y_pos << 3) * stride2 + (x_pos << 3); |
289 |
pV_Cur = pCurrent->v + (y_pos << 3) * stride2 + (x_pos << 3); |
pV_Cur = pCurrent->v + (y_pos << 3) * stride2 + (x_pos << 3); |
290 |
|
|
291 |
|
/* Block size */ |
292 |
|
cst = 8; |
293 |
|
|
294 |
|
/* Operation function */ |
295 |
|
transfer_op = (transfer_operation_8to16_t*)transfer_8to16copy; |
296 |
|
} |
297 |
|
|
298 |
|
/* Do the transfer */ |
299 |
start_timer(); |
start_timer(); |
300 |
transfer_8to16copy(&data[0 * 64], pY_Cur, stride); |
transfer_op(&data[0 * 64], pY_Cur, stride); |
301 |
transfer_8to16copy(&data[1 * 64], pY_Cur + 8, stride); |
transfer_op(&data[1 * 64], pY_Cur + cst, stride); |
302 |
transfer_8to16copy(&data[2 * 64], pY_Cur + next_block, stride); |
transfer_op(&data[2 * 64], pY_Cur + next_block, stride); |
303 |
transfer_8to16copy(&data[3 * 64], pY_Cur + next_block + 8, stride); |
transfer_op(&data[3 * 64], pY_Cur + next_block + cst, stride); |
304 |
transfer_8to16copy(&data[4 * 64], pU_Cur, stride2); |
transfer_op(&data[4 * 64], pU_Cur, stride2); |
305 |
transfer_8to16copy(&data[5 * 64], pV_Cur, stride2); |
transfer_op(&data[5 * 64], pV_Cur, stride2); |
306 |
stop_transfer_timer(); |
stop_transfer_timer(); |
307 |
} |
} |
308 |
|
|
309 |
void |
static __inline void |
310 |
MBTransAdd(const MBParam * pParam, |
MBTrans16to8(const MBParam * const pParam, |
311 |
FRAMEINFO * frame, |
const FRAMEINFO * const frame, |
312 |
MACROBLOCK * pMB, |
const MACROBLOCK * const pMB, |
313 |
const uint32_t x_pos, |
const uint32_t x_pos, |
314 |
const uint32_t y_pos, |
const uint32_t y_pos, |
315 |
int16_t data[6 * 64], |
int16_t data[6 * 64], |
316 |
|
const uint32_t add, |
317 |
const uint8_t cbp) |
const uint8_t cbp) |
318 |
{ |
{ |
319 |
uint8_t *pY_Cur, *pU_Cur, *pV_Cur; |
uint8_t *pY_Cur, *pU_Cur, *pV_Cur; |
320 |
uint32_t stride = pParam->edged_width; |
uint32_t stride = pParam->edged_width; |
321 |
uint32_t stride2 = stride / 2; |
uint32_t stride2 = stride / 2; |
322 |
uint32_t next_block = stride * 8; |
uint32_t next_block = stride * 8; |
323 |
IMAGE *pCurrent = &frame->image; |
uint32_t cst; |
324 |
|
const IMAGE * const pCurrent = &frame->image; |
325 |
pY_Cur = pCurrent->y + (y_pos << 4) * stride + (x_pos << 4); |
transfer_operation_16to8_t *transfer_op = NULL; |
|
pU_Cur = pCurrent->u + (y_pos << 3) * stride2 + (x_pos << 3); |
|
|
pV_Cur = pCurrent->v + (y_pos << 3) * stride2 + (x_pos << 3); |
|
326 |
|
|
327 |
if (pMB->field_dct) { |
if (pMB->field_dct) { |
328 |
next_block = stride; |
next_block = stride; |
329 |
stride *= 2; |
stride *= 2; |
330 |
} |
} |
331 |
|
|
332 |
start_timer(); |
if ((frame->vop_flags & XVID_VOP_REDUCED)) { |
333 |
if (cbp & 32) |
|
334 |
transfer_16to8add(pY_Cur, &data[0 * 64], stride); |
/* Image pointers */ |
335 |
if (cbp & 16) |
pY_Cur = pCurrent->y + (y_pos << 5) * stride + (x_pos << 5); |
336 |
transfer_16to8add(pY_Cur + 8, &data[1 * 64], stride); |
pU_Cur = pCurrent->u + (y_pos << 4) * stride2 + (x_pos << 4); |
337 |
if (cbp & 8) |
pV_Cur = pCurrent->v + (y_pos << 4) * stride2 + (x_pos << 4); |
338 |
transfer_16to8add(pY_Cur + next_block, &data[2 * 64], stride); |
|
339 |
if (cbp & 4) |
/* Block size */ |
340 |
transfer_16to8add(pY_Cur + next_block + 8, &data[3 * 64], stride); |
cst = 16; |
341 |
if (cbp & 2) |
|
342 |
transfer_16to8add(pU_Cur, &data[4 * 64], stride2); |
/* Operation function */ |
343 |
if (cbp & 1) |
if(add) |
344 |
transfer_16to8add(pV_Cur, &data[5 * 64], stride2); |
transfer_op = (transfer_operation_16to8_t*)add_upsampled_8x8_16to8; |
345 |
|
else |
346 |
|
transfer_op = (transfer_operation_16to8_t*)copy_upsampled_8x8_16to8; |
347 |
|
} else { |
348 |
|
|
349 |
|
/* Image pointers */ |
350 |
|
pY_Cur = pCurrent->y + (y_pos << 4) * stride + (x_pos << 4); |
351 |
|
pU_Cur = pCurrent->u + (y_pos << 3) * stride2 + (x_pos << 3); |
352 |
|
pV_Cur = pCurrent->v + (y_pos << 3) * stride2 + (x_pos << 3); |
353 |
|
|
354 |
|
/* Block size */ |
355 |
|
cst = 8; |
356 |
|
|
357 |
|
/* Operation function */ |
358 |
|
if(add) |
359 |
|
transfer_op = (transfer_operation_16to8_t*)transfer_16to8add; |
360 |
|
else |
361 |
|
transfer_op = (transfer_operation_16to8_t*)transfer_16to8copy; |
362 |
|
} |
363 |
|
|
364 |
|
/* Do the operation */ |
365 |
|
start_timer(); |
366 |
|
if (cbp&32) transfer_op(pY_Cur, &data[0 * 64], stride); |
367 |
|
if (cbp&16) transfer_op(pY_Cur + cst, &data[1 * 64], stride); |
368 |
|
if (cbp& 8) transfer_op(pY_Cur + next_block, &data[2 * 64], stride); |
369 |
|
if (cbp& 4) transfer_op(pY_Cur + next_block + cst, &data[3 * 64], stride); |
370 |
|
if (cbp& 2) transfer_op(pU_Cur, &data[4 * 64], stride2); |
371 |
|
if (cbp& 1) transfer_op(pV_Cur, &data[5 * 64], stride2); |
372 |
stop_transfer_timer(); |
stop_transfer_timer(); |
373 |
} |
} |
374 |
|
|
375 |
|
/***************************************************************************** |
376 |
|
* Module functions |
377 |
|
****************************************************************************/ |
378 |
|
|
379 |
|
void |
380 |
|
MBTransQuantIntra(const MBParam * const pParam, |
381 |
|
const FRAMEINFO * const frame, |
382 |
|
MACROBLOCK * const pMB, |
383 |
|
const uint32_t x_pos, |
384 |
|
const uint32_t y_pos, |
385 |
|
int16_t data[6 * 64], |
386 |
|
int16_t qcoeff[6 * 64]) |
387 |
|
{ |
388 |
|
|
389 |
/* if sum(diff between field lines) < sum(diff between frame lines), use field dct */ |
/* Transfer data */ |
390 |
|
MBTrans8to16(pParam, frame, pMB, x_pos, y_pos, data); |
391 |
|
|
392 |
|
/* Perform DCT (and field decision) */ |
393 |
|
MBfDCT(pParam, frame, pMB, x_pos, y_pos, data); |
394 |
|
|
395 |
uint32_t |
/* Quantize the block */ |
396 |
MBDecideFieldDCT(int16_t data[6 * 64]) |
MBQuantIntra(pParam, frame, pMB, data, qcoeff); |
397 |
|
|
398 |
|
/* DeQuantize the block */ |
399 |
|
MBDeQuantIntra(pParam, pMB->quant, data, qcoeff); |
400 |
|
|
401 |
|
/* Perform inverse DCT*/ |
402 |
|
MBiDCT(data, 0x3F); |
403 |
|
|
404 |
|
/* Transfer back the data -- Don't add data */ |
405 |
|
MBTrans16to8(pParam, frame, pMB, x_pos, y_pos, data, 0, 0x3F); |
406 |
|
} |
407 |
|
|
408 |
|
|
409 |
|
uint8_t |
410 |
|
MBTransQuantInter(const MBParam * const pParam, |
411 |
|
const FRAMEINFO * const frame, |
412 |
|
MACROBLOCK * const pMB, |
413 |
|
const uint32_t x_pos, |
414 |
|
const uint32_t y_pos, |
415 |
|
int16_t data[6 * 64], |
416 |
|
int16_t qcoeff[6 * 64]) |
417 |
{ |
{ |
418 |
|
uint8_t cbp; |
419 |
|
uint32_t limit; |
420 |
|
|
421 |
|
/* |
422 |
|
* There is no MBTrans8to16 for Inter block, that's done in motion compensation |
423 |
|
* already |
424 |
|
*/ |
425 |
|
|
426 |
|
/* Perform DCT (and field decision) */ |
427 |
|
MBfDCT(pParam, frame, pMB, x_pos, y_pos, data); |
428 |
|
|
429 |
|
/* Set the limit threshold */ |
430 |
|
limit = PVOP_TOOSMALL_LIMIT + ((pMB->quant == 1)? 1 : 0); |
431 |
|
|
432 |
|
/* Quantize the block */ |
433 |
|
cbp = MBQuantInter(pParam, frame, pMB, data, qcoeff, 0, limit); |
434 |
|
|
435 |
|
/* DeQuantize the block */ |
436 |
|
MBDeQuantInter(pParam, pMB->quant, data, qcoeff, cbp); |
437 |
|
|
438 |
|
/* Perform inverse DCT*/ |
439 |
|
MBiDCT(data, cbp); |
440 |
|
|
441 |
|
/* Transfer back the data -- Add the data */ |
442 |
|
MBTrans16to8(pParam, frame, pMB, x_pos, y_pos, data, 1, cbp); |
443 |
|
|
444 |
|
return(cbp); |
445 |
|
} |
446 |
|
|
447 |
|
uint8_t |
448 |
|
MBTransQuantInterBVOP(const MBParam * pParam, |
449 |
|
FRAMEINFO * frame, |
450 |
|
MACROBLOCK * pMB, |
451 |
|
const uint32_t x_pos, |
452 |
|
const uint32_t y_pos, |
453 |
|
int16_t data[6 * 64], |
454 |
|
int16_t qcoeff[6 * 64]) |
455 |
|
{ |
456 |
|
uint8_t cbp; |
457 |
|
uint32_t limit; |
458 |
|
|
459 |
|
/* |
460 |
|
* There is no MBTrans8to16 for Inter block, that's done in motion compensation |
461 |
|
* already |
462 |
|
*/ |
463 |
|
|
464 |
|
/* Perform DCT (and field decision) */ |
465 |
|
MBfDCT(pParam, frame, pMB, x_pos, y_pos, data); |
466 |
|
|
467 |
|
/* Set the limit threshold */ |
468 |
|
limit = BVOP_TOOSMALL_LIMIT; |
469 |
|
|
470 |
|
/* Quantize the block */ |
471 |
|
cbp = MBQuantInter(pParam, frame, pMB, data, qcoeff, 1, limit); |
472 |
|
|
473 |
|
/* |
474 |
|
* History comment: |
475 |
|
* We don't have to DeQuant, iDCT and Transfer back data for B-frames. |
476 |
|
* |
477 |
|
* BUT some plugins require the original frame to be passed so we have |
478 |
|
* to take care of that here |
479 |
|
*/ |
480 |
|
if((pParam->plugin_flags & XVID_REQORIGINAL)) { |
481 |
|
|
482 |
|
/* DeQuantize the block */ |
483 |
|
MBDeQuantInter(pParam, pMB->quant, data, qcoeff, cbp); |
484 |
|
|
485 |
|
/* Perform inverse DCT*/ |
486 |
|
MBiDCT(data, cbp); |
487 |
|
|
488 |
|
/* Transfer back the data -- Add the data */ |
489 |
|
MBTrans16to8(pParam, frame, pMB, x_pos, y_pos, data, 1, cbp); |
490 |
|
} |
491 |
|
|
492 |
|
return(cbp); |
493 |
|
} |
494 |
|
|
495 |
|
/* if sum(diff between field lines) < sum(diff between frame lines), use field dct */ |
496 |
|
uint32_t |
497 |
|
MBFieldTest_c(int16_t data[6 * 64]) |
498 |
|
{ |
499 |
const uint8_t blocks[] = |
const uint8_t blocks[] = |
500 |
{ 0 * 64, 0 * 64, 0 * 64, 0 * 64, 2 * 64, 2 * 64, 2 * 64, 2 * 64 }; |
{ 0 * 64, 0 * 64, 0 * 64, 0 * 64, 2 * 64, 2 * 64, 2 * 64, 2 * 64 }; |
501 |
const uint8_t lines[] = { 0, 16, 32, 48, 0, 16, 32, 48 }; |
const uint8_t lines[] = { 0, 16, 32, 48, 0, 16, 32, 48 }; |
506 |
for (i = 0; i < 7; ++i) { |
for (i = 0; i < 7; ++i) { |
507 |
for (j = 0; j < 8; ++j) { |
for (j = 0; j < 8; ++j) { |
508 |
frame += |
frame += |
509 |
ABS(data[0 * 64 + (i + 1) * 8 + j] - data[0 * 64 + i * 8 + j]); |
abs(data[0 * 64 + (i + 1) * 8 + j] - data[0 * 64 + i * 8 + j]); |
510 |
frame += |
frame += |
511 |
ABS(data[1 * 64 + (i + 1) * 8 + j] - data[1 * 64 + i * 8 + j]); |
abs(data[1 * 64 + (i + 1) * 8 + j] - data[1 * 64 + i * 8 + j]); |
512 |
frame += |
frame += |
513 |
ABS(data[2 * 64 + (i + 1) * 8 + j] - data[2 * 64 + i * 8 + j]); |
abs(data[2 * 64 + (i + 1) * 8 + j] - data[2 * 64 + i * 8 + j]); |
514 |
frame += |
frame += |
515 |
ABS(data[3 * 64 + (i + 1) * 8 + j] - data[3 * 64 + i * 8 + j]); |
abs(data[3 * 64 + (i + 1) * 8 + j] - data[3 * 64 + i * 8 + j]); |
516 |
|
|
517 |
field += |
field += |
518 |
ABS(data[blocks[i + 1] + lines[i + 1] + j] - |
abs(data[blocks[i + 1] + lines[i + 1] + j] - |
519 |
data[blocks[i] + lines[i] + j]); |
data[blocks[i] + lines[i] + j]); |
520 |
field += |
field += |
521 |
ABS(data[blocks[i + 1] + lines[i + 1] + 8 + j] - |
abs(data[blocks[i + 1] + lines[i + 1] + 8 + j] - |
522 |
data[blocks[i] + lines[i] + 8 + j]); |
data[blocks[i] + lines[i] + 8 + j]); |
523 |
field += |
field += |
524 |
ABS(data[blocks[i + 1] + 64 + lines[i + 1] + j] - |
abs(data[blocks[i + 1] + 64 + lines[i + 1] + j] - |
525 |
data[blocks[i] + 64 + lines[i] + j]); |
data[blocks[i] + 64 + lines[i] + j]); |
526 |
field += |
field += |
527 |
ABS(data[blocks[i + 1] + 64 + lines[i + 1] + 8 + j] - |
abs(data[blocks[i + 1] + 64 + lines[i + 1] + 8 + j] - |
528 |
data[blocks[i] + 64 + lines[i] + 8 + j]); |
data[blocks[i] + 64 + lines[i] + 8 + j]); |
529 |
} |
} |
530 |
} |
} |
531 |
|
|
532 |
if (frame > field) { |
return (frame >= (field + 350)); |
|
MBFrameToField(data); |
|
|
} |
|
|
|
|
|
return (frame > (field + 350)); |
|
533 |
} |
} |
534 |
|
|
535 |
|
|
599 |
MOVLINE(LINE(3, 5), LINE(3, 3)); |
MOVLINE(LINE(3, 5), LINE(3, 3)); |
600 |
MOVLINE(LINE(3, 3), tmp); |
MOVLINE(LINE(3, 3), tmp); |
601 |
} |
} |
602 |
|
|
603 |
|
|
604 |
|
|
605 |
|
|
606 |
|
|
607 |
|
/************************************************************************ |
608 |
|
* Trellis based R-D optimal quantization * |
609 |
|
* * |
610 |
|
* Trellis Quant code (C) 2003 Pascal Massimino skal(at)planet-d.net * |
611 |
|
* * |
612 |
|
************************************************************************/ |
613 |
|
|
614 |
|
|
615 |
|
static int |
616 |
|
dct_quantize_trellis_inter_mpeg_c (int16_t *qcoeff, const int16_t *data, int quant) |
617 |
|
{ return 63; } |
618 |
|
|
619 |
|
|
620 |
|
////////////////////////////////////////////////////////// |
621 |
|
// |
622 |
|
// Trellis-Based quantization |
623 |
|
// |
624 |
|
// So far I understand this paper: |
625 |
|
// |
626 |
|
// "Trellis-Based R-D Optimal Quantization in H.263+" |
627 |
|
// J.Wen, M.Luttrell, J.Villasenor |
628 |
|
// IEEE Transactions on Image Processing, Vol.9, No.8, Aug. 2000. |
629 |
|
// |
630 |
|
// we are at stake with a simplified Bellmand-Ford / Dijkstra Single |
631 |
|
// Source Shorted Path algo. But due to the underlying graph structure |
632 |
|
// ("Trellis"), it can be turned into a dynamic programming algo, |
633 |
|
// partially saving the explicit graph's nodes representation. And |
634 |
|
// without using a heap, since the open frontier of the DAG is always |
635 |
|
// known, and of fixed sized. |
636 |
|
// |
637 |
|
////////////////////////////////////////////////////////// |
638 |
|
|
639 |
|
|
640 |
|
////////////////////////////////////////////////////////// |
641 |
|
// Codes lengths for relevant levels. |
642 |
|
|
643 |
|
// let's factorize: |
644 |
|
static const uint8_t Code_Len0[64] = { |
645 |
|
30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30, |
646 |
|
30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30 }; |
647 |
|
static const uint8_t Code_Len1[64] = { |
648 |
|
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,30, |
649 |
|
30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30 }; |
650 |
|
static const uint8_t Code_Len2[64] = { |
651 |
|
19,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30, |
652 |
|
30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30 }; |
653 |
|
static const uint8_t Code_Len3[64] = { |
654 |
|
18,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30, |
655 |
|
30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30 }; |
656 |
|
static const uint8_t Code_Len4[64] = { |
657 |
|
17,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30, |
658 |
|
30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30 }; |
659 |
|
static const uint8_t Code_Len5[64] = { |
660 |
|
16,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30, |
661 |
|
30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30 }; |
662 |
|
static const uint8_t Code_Len6[64] = { |
663 |
|
15,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30, |
664 |
|
30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30 }; |
665 |
|
static const uint8_t Code_Len7[64] = { |
666 |
|
13,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30, |
667 |
|
30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30 }; |
668 |
|
static const uint8_t Code_Len8[64] = { |
669 |
|
11,30,30,30,30,30,30,30,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 }; |
671 |
|
static const uint8_t Code_Len9[64] = { |
672 |
|
12,21,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30, |
673 |
|
30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30 }; |
674 |
|
static const uint8_t Code_Len10[64] = { |
675 |
|
12,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, |
676 |
|
30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30 }; |
677 |
|
static const uint8_t Code_Len11[64] = { |
678 |
|
12,19,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30, |
679 |
|
30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30 }; |
680 |
|
static const uint8_t Code_Len12[64] = { |
681 |
|
11,17,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30, |
682 |
|
30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30 }; |
683 |
|
static const uint8_t Code_Len13[64] = { |
684 |
|
11,15,21,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30, |
685 |
|
30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30 }; |
686 |
|
static const uint8_t Code_Len14[64] = { |
687 |
|
10,12,19,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30, |
688 |
|
30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30 }; |
689 |
|
static const uint8_t Code_Len15[64] = { |
690 |
|
10,13,17,19,21,21,21,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30, |
691 |
|
30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30 }; |
692 |
|
static const uint8_t Code_Len16[64] = { |
693 |
|
9,12,13,18,18,19,19,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30, |
694 |
|
30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30}; |
695 |
|
static const uint8_t Code_Len17[64] = { |
696 |
|
8,11,13,14,14,14,15,19,19,19,21,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30, |
697 |
|
30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30 }; |
698 |
|
static const uint8_t Code_Len18[64] = { |
699 |
|
7, 9,11,11,13,13,13,15,15,15,16,22,22,22,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30, |
700 |
|
30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30 }; |
701 |
|
static const uint8_t Code_Len19[64] = { |
702 |
|
5, 7, 9,10,10,11,11,11,11,11,13,14,16,17,17,18,18,18,18,18,18,18,18,20,20,21,21,30,30,30,30,30, |
703 |
|
30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30 }; |
704 |
|
static const uint8_t Code_Len20[64] = { |
705 |
|
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, |
706 |
|
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 }; |
707 |
|
|
708 |
|
// a few more table for LAST table: |
709 |
|
static const uint8_t Code_Len21[64] = { |
710 |
|
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, |
711 |
|
30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30}; |
712 |
|
static const uint8_t Code_Len22[64] = { |
713 |
|
12,15,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30, |
714 |
|
30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30}; |
715 |
|
static const uint8_t Code_Len23[64] = { |
716 |
|
10,12,15,15,15,16,16,16,16,17,17,17,17,17,17,17,17,18,18,18,18,18,18,18,18,19,19,19,19,20,20,20, |
717 |
|
20,21,21,21,21,21,21,21,21,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30}; |
718 |
|
static const uint8_t Code_Len24[64] = { |
719 |
|
5, 7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9,10,10,10,10,10,10,10,10,11,11,11,11,12,12,12, |
720 |
|
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}; |
721 |
|
|
722 |
|
|
723 |
|
static const uint8_t * const B16_17_Code_Len[24] = { // levels [1..24] |
724 |
|
Code_Len20,Code_Len19,Code_Len18,Code_Len17, |
725 |
|
Code_Len16,Code_Len15,Code_Len14,Code_Len13, |
726 |
|
Code_Len12,Code_Len11,Code_Len10,Code_Len9, |
727 |
|
Code_Len8, Code_Len7 ,Code_Len6 ,Code_Len5, |
728 |
|
Code_Len4, Code_Len3, Code_Len3 ,Code_Len2, |
729 |
|
Code_Len2, Code_Len1, Code_Len1, Code_Len1, |
730 |
|
}; |
731 |
|
|
732 |
|
static const uint8_t * const B16_17_Code_Len_Last[6] = { // levels [1..6] |
733 |
|
Code_Len24,Code_Len23,Code_Len22,Code_Len21, Code_Len3, Code_Len1, |
734 |
|
}; |
735 |
|
|
736 |
|
#define TL(q) 0xfe00/(q*q) |
737 |
|
|
738 |
|
static const int Trellis_Lambda_Tabs[31] = { |
739 |
|
TL( 1),TL( 2),TL( 3),TL( 4),TL( 5),TL( 6), TL( 7), |
740 |
|
TL( 8),TL( 9),TL(10),TL(11),TL(12),TL(13),TL(14), TL(15), |
741 |
|
TL(16),TL(17),TL(18),TL(19),TL(20),TL(21),TL(22), TL(23), |
742 |
|
TL(24),TL(25),TL(26),TL(27),TL(28),TL(29),TL(30), TL(31) |
743 |
|
}; |
744 |
|
#undef TL |
745 |
|
|
746 |
|
static inline int Find_Last(const int16_t *C, const uint16_t *Zigzag, int i) |
747 |
|
{ |
748 |
|
while(i>=0) |
749 |
|
if (C[Zigzag[i]]) |
750 |
|
return i; |
751 |
|
else i--; |
752 |
|
return -1; |
753 |
|
} |
754 |
|
|
755 |
|
////////////////////////////////////////////////////////// |
756 |
|
|
757 |
|
#define DBG 0 |
758 |
|
|
759 |
|
static uint32_t Evaluate_Cost(const int16_t *C, int Mult, int Bias, |
760 |
|
const uint16_t * Zigzag, int Max, int Lambda) |
761 |
|
{ |
762 |
|
#if (DBG>0) |
763 |
|
const int16_t * const Ref = C + 6*64; |
764 |
|
int Last = Max; |
765 |
|
while(Last>=0 && C[Zigzag[Last]]==0) Last--; |
766 |
|
int Bits = 0; |
767 |
|
if (Last>=0) { |
768 |
|
Bits = 2; // CBP |
769 |
|
int j=0, j0=0; |
770 |
|
int Run, Level; |
771 |
|
while(j<Last) { |
772 |
|
while(!C[Zigzag[j]]) j++; |
773 |
|
if (j==Last) break; |
774 |
|
Level=C[Zigzag[j]]; |
775 |
|
Run = j - j0; |
776 |
|
j0 = ++j; |
777 |
|
if (Level>=-24 && Level<=24) Bits += B16_17_Code_Len[(Level<0) ? -Level-1 : Level-1][Run]; |
778 |
|
else Bits += 30; |
779 |
|
} |
780 |
|
Level = C[Zigzag[Last]]; |
781 |
|
Run = j - j0; |
782 |
|
if (Level>=-6 && Level<=6) Bits += B16_17_Code_Len_Last[(Level<0) ? -Level-1 : Level-1][Run]; |
783 |
|
else Bits += 30; |
784 |
|
} |
785 |
|
|
786 |
|
int Dist = 0; |
787 |
|
int i; |
788 |
|
for(i=0; i<=Last; ++i) { |
789 |
|
int V = C[Zigzag[i]]*Mult; |
790 |
|
if (V>0) V += Bias; |
791 |
|
else if (V<0) V -= Bias; |
792 |
|
V -= Ref[Zigzag[i]]; |
793 |
|
Dist += V*V; |
794 |
|
} |
795 |
|
uint32_t Cost = Lambda*Dist + (Bits<<16); |
796 |
|
if (DBG==1) |
797 |
|
printf( " Last:%2d/%2d Cost = [(Bits=%5.0d) + Lambda*(Dist=%6.0d) = %d ] >>12= %d ", Last,Max, Bits, Dist, Cost, Cost>>12 ); |
798 |
|
return Cost; |
799 |
|
|
800 |
|
#else |
801 |
|
return 0; |
802 |
|
#endif |
803 |
|
} |
804 |
|
|
805 |
|
|
806 |
|
static int |
807 |
|
dct_quantize_trellis_h263_c(int16_t *const Out, const int16_t *const In, int Q, const uint16_t * const Zigzag, int Non_Zero) |
808 |
|
{ |
809 |
|
|
810 |
|
// Note: We should search last non-zero coeffs on *real* DCT input coeffs (In[]), |
811 |
|
// not quantized one (Out[]). However, it only improves the result *very* |
812 |
|
// slightly (~0.01dB), whereas speed drops to crawling level :) |
813 |
|
// Well, actually, taking 1 more coeff past Non_Zero into account sometimes helps, |
814 |
|
|
815 |
|
typedef struct { int16_t Run, Level; } NODE; |
816 |
|
|
817 |
|
NODE Nodes[65], Last; |
818 |
|
uint32_t Run_Costs0[64+1], * const Run_Costs = Run_Costs0 + 1; |
819 |
|
|
820 |
|
const int Mult = 2*Q; |
821 |
|
const int Bias = (Q-1) | 1; |
822 |
|
const int Lev0 = Mult + Bias; |
823 |
|
const int Lambda = Trellis_Lambda_Tabs[Q-1]; // it's 1/lambda, actually |
824 |
|
|
825 |
|
int Run_Start = -1; |
826 |
|
Run_Costs[-1] = 2<<16; // source (w/ CBP penalty) |
827 |
|
uint32_t Min_Cost = 2<<16; |
828 |
|
|
829 |
|
int Last_Node = -1; |
830 |
|
uint32_t Last_Cost = 0; |
831 |
|
|
832 |
|
#if (DBG>0) |
833 |
|
Last.Level = 0; Last.Run = -1; // just initialize to smthg |
834 |
|
#endif |
835 |
|
|
836 |
|
int i, j; |
837 |
|
|
838 |
|
Non_Zero = Find_Last(Out, Zigzag, Non_Zero); |
839 |
|
if (Non_Zero<0) |
840 |
|
return -1; |
841 |
|
|
842 |
|
for(i=0; i<=Non_Zero; i++) |
843 |
|
{ |
844 |
|
const int AC = In[Zigzag[i]]; |
845 |
|
const int Level1 = Out[Zigzag[i]]; |
846 |
|
const int Dist0 = Lambda* AC*AC; |
847 |
|
uint32_t Best_Cost = 0xf0000000; |
848 |
|
Last_Cost += Dist0; |
849 |
|
|
850 |
|
if ((uint32_t)(Level1+1)<3) // very specialized loop for -1,0,+1 |
851 |
|
{ |
852 |
|
int dQ; |
853 |
|
int Run; |
854 |
|
|
855 |
|
if (AC<0) { |
856 |
|
Nodes[i].Level = -1; |
857 |
|
dQ = Lev0 + AC; |
858 |
|
} else { |
859 |
|
Nodes[i].Level = 1; |
860 |
|
dQ = Lev0 - AC; |
861 |
|
} |
862 |
|
const uint32_t Cost0 = Lambda*dQ*dQ; |
863 |
|
|
864 |
|
Nodes[i].Run = 1; |
865 |
|
Best_Cost = (Code_Len20[0]<<16) + Run_Costs[i-1]+Cost0; |
866 |
|
for(Run=i-Run_Start; Run>0; --Run) |
867 |
|
{ |
868 |
|
const uint32_t Cost_Base = Cost0 + Run_Costs[i-Run]; |
869 |
|
const uint32_t Cost = Cost_Base + (Code_Len20[Run-1]<<16); |
870 |
|
// TODO: what about tie-breaks? Should we favor short runs or |
871 |
|
// long runs? Although the error is the same, it would not be |
872 |
|
// spread the same way along high and low frequencies... |
873 |
|
if (Cost<Best_Cost) |
874 |
|
{ |
875 |
|
Best_Cost = Cost; |
876 |
|
Nodes[i].Run = Run; |
877 |
|
} |
878 |
|
const uint32_t lCost = Cost_Base + (Code_Len24[Run-1]<<16); |
879 |
|
if (lCost<Last_Cost) |
880 |
|
{ |
881 |
|
Last_Cost = lCost; |
882 |
|
Last.Run = Run; |
883 |
|
Last_Node = i; |
884 |
|
} |
885 |
|
} |
886 |
|
if (Last_Node==i) Last.Level = Nodes[i].Level; |
887 |
|
|
888 |
|
|
889 |
|
if (DBG==1) { |
890 |
|
Run_Costs[i] = Best_Cost; |
891 |
|
printf( "Costs #%2d: ", i); |
892 |
|
for(j=-1;j<=Non_Zero;++j) { |
893 |
|
if (j==Run_Start) printf( " %3.0d|", Run_Costs[j]>>12 ); |
894 |
|
else if (j>Run_Start && j<i) printf( " %3.0d|", Run_Costs[j]>>12 ); |
895 |
|
else if (j==i) printf( "(%3.0d)", Run_Costs[j]>>12 ); |
896 |
|
else printf( " - |" ); |
897 |
|
} |
898 |
|
printf( "<%3.0d %2d %d>", Min_Cost>>12, Nodes[i].Level, Nodes[i].Run ); |
899 |
|
printf( " Last:#%2d {%3.0d %2d %d}", Last_Node, Last_Cost>>12, Last.Level, Last.Run ); |
900 |
|
printf( " AC:%3.0d Dist0:%3d Dist(%d)=%d", AC, Dist0>>12, Nodes[i].Level, Cost0>>12 ); |
901 |
|
printf( "\n" ); |
902 |
|
} |
903 |
|
} |
904 |
|
else // "big" levels |
905 |
|
{ |
906 |
|
const uint8_t *Tbl_L1, *Tbl_L2, *Tbl_L1_Last, *Tbl_L2_Last; |
907 |
|
int Level2; |
908 |
|
int dQ1, dQ2; |
909 |
|
int Run; |
910 |
|
|
911 |
|
if (Level1>1) { |
912 |
|
dQ1 = Level1*Mult-AC + Bias; |
913 |
|
dQ2 = dQ1 - Mult; |
914 |
|
Level2 = Level1-1; |
915 |
|
Tbl_L1 = (Level1<=24) ? B16_17_Code_Len[Level1-1] : Code_Len0; |
916 |
|
Tbl_L2 = (Level2<=24) ? B16_17_Code_Len[Level2-1] : Code_Len0; |
917 |
|
Tbl_L1_Last = (Level1<=6) ? B16_17_Code_Len_Last[Level1-1] : Code_Len0; |
918 |
|
Tbl_L2_Last = (Level2<=6) ? B16_17_Code_Len_Last[Level2-1] : Code_Len0; |
919 |
|
} |
920 |
|
else { // Level1<-1 |
921 |
|
dQ1 = Level1*Mult-AC - Bias; |
922 |
|
dQ2 = dQ1 + Mult; |
923 |
|
Level2 = Level1 + 1; |
924 |
|
Tbl_L1 = (Level1>=-24) ? B16_17_Code_Len[Level1^-1] : Code_Len0; |
925 |
|
Tbl_L2 = (Level2>=-24) ? B16_17_Code_Len[Level2^-1] : Code_Len0; |
926 |
|
Tbl_L1_Last = (Level1>=- 6) ? B16_17_Code_Len_Last[Level1^-1] : Code_Len0; |
927 |
|
Tbl_L2_Last = (Level2>=- 6) ? B16_17_Code_Len_Last[Level2^-1] : Code_Len0; |
928 |
|
} |
929 |
|
const uint32_t Dist1 = Lambda*dQ1*dQ1; |
930 |
|
const uint32_t Dist2 = Lambda*dQ2*dQ2; |
931 |
|
const int dDist21 = Dist2-Dist1; |
932 |
|
|
933 |
|
for(Run=i-Run_Start; Run>0; --Run) |
934 |
|
{ |
935 |
|
const uint32_t Cost_Base = Dist1 + Run_Costs[i-Run]; |
936 |
|
|
937 |
|
// for sub-optimal (but slightly worth it, speed-wise) search, uncomment the following: |
938 |
|
// if (Cost_Base>=Best_Cost) continue; |
939 |
|
|
940 |
|
uint32_t Cost1, Cost2; |
941 |
|
int bLevel; |
942 |
|
|
943 |
|
Cost1 = Cost_Base + (Tbl_L1[Run-1]<<16); |
944 |
|
Cost2 = Cost_Base + (Tbl_L2[Run-1]<<16) + dDist21; |
945 |
|
|
946 |
|
if (Cost2<Cost1) { Cost1 = Cost2; bLevel = Level2; } |
947 |
|
else bLevel = Level1; |
948 |
|
|
949 |
|
if (Cost1<Best_Cost) |
950 |
|
{ |
951 |
|
Best_Cost = Cost1; |
952 |
|
Nodes[i].Run = Run; |
953 |
|
Nodes[i].Level = bLevel; |
954 |
|
} |
955 |
|
|
956 |
|
Cost1 = Cost_Base + (Tbl_L1_Last[Run-1]<<16); |
957 |
|
Cost2 = Cost_Base + (Tbl_L2_Last[Run-1]<<16) + dDist21; |
958 |
|
|
959 |
|
if (Cost2<Cost1) { Cost1 = Cost2; bLevel = Level2; } |
960 |
|
else bLevel = Level1; |
961 |
|
if (Cost1<Last_Cost) |
962 |
|
{ |
963 |
|
Last_Cost = Cost1; |
964 |
|
Last.Run = Run; |
965 |
|
Last.Level = bLevel; |
966 |
|
Last_Node = i; |
967 |
|
} |
968 |
|
} |
969 |
|
|
970 |
|
if (DBG==1) { |
971 |
|
Run_Costs[i] = Best_Cost; |
972 |
|
printf( "Costs #%2d: ", i); |
973 |
|
for(j=-1;j<=Non_Zero;++j) { |
974 |
|
if (j==Run_Start) printf( " %3.0d|", Run_Costs[j]>>12 ); |
975 |
|
else if (j>Run_Start && j<i) printf( " %3.0d|", Run_Costs[j]>>12 ); |
976 |
|
else if (j==i) printf( "(%3.0d)", Run_Costs[j]>>12 ); |
977 |
|
else printf( " - |" ); |
978 |
|
} |
979 |
|
printf( "<%3.0d %2d %d>", Min_Cost>>12, Nodes[i].Level, Nodes[i].Run ); |
980 |
|
printf( " Last:#%2d {%3.0d %2d %d}", Last_Node, Last_Cost>>12, Last.Level, Last.Run ); |
981 |
|
printf( " AC:%3.0d Dist0:%3d Dist(%2d):%3d Dist(%2d):%3d", AC, Dist0>>12, Level1, Dist1>>12, Level2, Dist2>>12 ); |
982 |
|
printf( "\n" ); |
983 |
|
} |
984 |
|
} |
985 |
|
|
986 |
|
Run_Costs[i] = Best_Cost; |
987 |
|
|
988 |
|
if (Best_Cost < Min_Cost + Dist0) { |
989 |
|
Min_Cost = Best_Cost; |
990 |
|
Run_Start = i; |
991 |
|
} |
992 |
|
else |
993 |
|
{ |
994 |
|
// as noticed by Michael Niedermayer (michaelni at gmx.at), there's |
995 |
|
// a code shorter by 1 bit for a larger run (!), same level. We give |
996 |
|
// it a chance by not moving the left barrier too much. |
997 |
|
while( Run_Costs[Run_Start]>Min_Cost+(1<<16) ) |
998 |
|
Run_Start++; |
999 |
|
|
1000 |
|
// spread on preceding coeffs the cost incurred by skipping this one |
1001 |
|
for(j=Run_Start; j<i; ++j) Run_Costs[j] += Dist0; |
1002 |
|
Min_Cost += Dist0; |
1003 |
|
} |
1004 |
|
} |
1005 |
|
|
1006 |
|
if (DBG) { |
1007 |
|
Last_Cost = Evaluate_Cost(Out,Mult,Bias, Zigzag,Non_Zero, Lambda); |
1008 |
|
if (DBG==1) { |
1009 |
|
printf( "=> " ); |
1010 |
|
for(i=0; i<=Non_Zero; ++i) printf( "[%3.0d] ", Out[Zigzag[i]] ); |
1011 |
|
printf( "\n" ); |
1012 |
|
} |
1013 |
|
} |
1014 |
|
|
1015 |
|
if (Last_Node<0) |
1016 |
|
return -1; |
1017 |
|
|
1018 |
|
// reconstruct optimal sequence backward with surviving paths |
1019 |
|
bzero(Out, 64*sizeof(*Out)); |
1020 |
|
Out[Zigzag[Last_Node]] = Last.Level; |
1021 |
|
i = Last_Node - Last.Run; |
1022 |
|
while(i>=0) { |
1023 |
|
Out[Zigzag[i]] = Nodes[i].Level; |
1024 |
|
i -= Nodes[i].Run; |
1025 |
|
} |
1026 |
|
|
1027 |
|
if (DBG) { |
1028 |
|
uint32_t Cost = Evaluate_Cost(Out,Mult,Bias, Zigzag,Non_Zero, Lambda); |
1029 |
|
if (DBG==1) { |
1030 |
|
printf( "<= " ); |
1031 |
|
for(i=0; i<=Last_Node; ++i) printf( "[%3.0d] ", Out[Zigzag[i]] ); |
1032 |
|
printf( "\n--------------------------------\n" ); |
1033 |
|
} |
1034 |
|
if (Cost>Last_Cost) printf( "!!! %u > %u\n", Cost, Last_Cost ); |
1035 |
|
} |
1036 |
|
return Last_Node; |
1037 |
|
} |
1038 |
|
|
1039 |
|
#undef DBG |