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 "../dct/fdct.h" |
#include "../dct/fdct.h" |
39 |
#include "../dct/idct.h" |
#include "../dct/idct.h" |
40 |
#include "../quant/quant_mpeg4.h" |
#include "../quant/quant_mpeg4.h" |
41 |
#include "../quant/quant_h263.h" |
#include "../quant/quant_h263.h" |
42 |
#include "../encoder.h" |
#include "../encoder.h" |
43 |
|
|
44 |
#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 */ |
|
|
|
|
|
/* this isnt pretty, but its better than 20 ifdefs */ |
|
|
|
|
|
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; |
|
45 |
|
|
46 |
pY_Cur = pCurrent->y + (y_pos << 4) * stride + (x_pos << 4); |
MBFIELDTEST_PTR MBFieldTest; |
|
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(); |
|
47 |
|
|
|
for (i = 0; i < 6; i++) { |
|
48 |
/* |
/* |
49 |
* no need to transfer 8->16-bit |
* Skip blocks having a coefficient sum below this value. This value will be |
50 |
* (this is performed already in motion compensation) |
* corrected according to the MB quantizer to avoid artifacts for quant==1 |
51 |
*/ |
*/ |
52 |
start_timer(); |
#define PVOP_TOOSMALL_LIMIT 1 |
53 |
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); |
|
|
|
|
|
start_timer(); |
|
|
idct(&data[i * 64]); |
|
|
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); |
|
|
} |
|
54 |
|
|
55 |
|
/***************************************************************************** |
56 |
|
* Local functions |
57 |
|
****************************************************************************/ |
58 |
|
|
59 |
uint8_t |
/* permute block and return field dct choice */ |
60 |
MBTransQuantInter2(const MBParam * pParam, |
static __inline uint32_t |
61 |
FRAMEINFO * frame, |
MBDecideFieldDCT(int16_t data[6 * 64]) |
|
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; |
|
|
} |
|
|
|
|
|
uint8_t |
|
|
MBTransQuantInterBVOP(const MBParam * pParam, |
|
|
FRAMEINFO * frame, |
|
|
MACROBLOCK * pMB, |
|
|
int16_t data[6 * 64], |
|
|
int16_t qcoeff[6 * 64]) |
|
62 |
{ |
{ |
63 |
uint8_t cbp; |
uint32_t field = MBFieldTest(data); |
|
|
|
|
/* there is no MBTrans for Inter block, that's done in motion compensation already */ |
|
64 |
|
|
65 |
MBfDCT(pParam,frame,pMB,data); |
if (field) |
66 |
cbp = MBQuantInter(pParam,frame->quant,data,qcoeff); |
MBFrameToField(data); |
|
|
|
|
/* we don't have to DeQuant, iDCT and Transfer back data for B-frames */ |
|
67 |
|
|
68 |
return cbp; |
return field; |
69 |
} |
} |
70 |
|
|
71 |
|
/* Performs Forward DCT on all blocks */ |
72 |
void |
static __inline void |
73 |
MBfDCT(const MBParam * pParam, |
MBfDCT(const MBParam * const pParam, |
74 |
FRAMEINFO * frame, |
const FRAMEINFO * const frame, |
75 |
MACROBLOCK * pMB, |
MACROBLOCK * const pMB, |
76 |
|
uint32_t x_pos, |
77 |
|
uint32_t y_pos, |
78 |
int16_t data[6 * 64]) |
int16_t data[6 * 64]) |
79 |
{ |
{ |
80 |
int i; |
/* Handles interlacing */ |
|
|
|
81 |
start_timer(); |
start_timer(); |
82 |
pMB->field_dct = 0; |
pMB->field_dct = 0; |
83 |
if ((frame->global_flags & XVID_INTERLACING)) { |
if ((frame->vol_flags & XVID_VOL_INTERLACING) && |
84 |
|
(x_pos>0) && (x_pos<pParam->mb_width-1) && |
85 |
|
(y_pos>0) && (y_pos<pParam->mb_height-1)) { |
86 |
pMB->field_dct = MBDecideFieldDCT(data); |
pMB->field_dct = MBDecideFieldDCT(data); |
87 |
} |
} |
88 |
stop_interlacing_timer(); |
stop_interlacing_timer(); |
89 |
|
|
90 |
for (i = 0; i < 6; i++) { |
/* Perform DCT */ |
91 |
start_timer(); |
start_timer(); |
92 |
fdct(&data[i * 64]); |
fdct(&data[0 * 64]); |
93 |
|
fdct(&data[1 * 64]); |
94 |
|
fdct(&data[2 * 64]); |
95 |
|
fdct(&data[3 * 64]); |
96 |
|
fdct(&data[4 * 64]); |
97 |
|
fdct(&data[5 * 64]); |
98 |
stop_dct_timer(); |
stop_dct_timer(); |
99 |
} |
} |
|
} |
|
100 |
|
|
101 |
void |
/* Performs Inverse DCT on all blocks */ |
102 |
MBQuantDeQuantIntra(const MBParam * pParam, |
static __inline void |
103 |
FRAMEINFO * frame, |
MBiDCT(int16_t data[6 * 64], |
104 |
MACROBLOCK * pMB, |
const uint8_t cbp) |
|
int16_t qcoeff[6 * 64], |
|
|
int16_t data[6*64]) |
|
105 |
{ |
{ |
|
int i; |
|
|
int iQuant = frame->quant; |
|
|
|
|
106 |
start_timer(); |
start_timer(); |
107 |
pMB->field_dct = 0; |
if(cbp & (1 << (5 - 0))) idct(&data[0 * 64]); |
108 |
if ((frame->global_flags & XVID_INTERLACING)) { |
if(cbp & (1 << (5 - 1))) idct(&data[1 * 64]); |
109 |
pMB->field_dct = MBDecideFieldDCT(data); |
if(cbp & (1 << (5 - 2))) idct(&data[2 * 64]); |
110 |
} |
if(cbp & (1 << (5 - 3))) idct(&data[3 * 64]); |
111 |
stop_interlacing_timer(); |
if(cbp & (1 << (5 - 4))) idct(&data[4 * 64]); |
112 |
|
if(cbp & (1 << (5 - 5))) idct(&data[5 * 64]); |
113 |
for (i = 0; i < 6; i++) { |
stop_idct_timer(); |
|
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(); |
|
|
|
|
|
start_timer(); |
|
|
dequant4_intra(&data[i * 64], &qcoeff[i * 64], iQuant, iDcScaler); |
|
|
stop_iquant_timer(); |
|
|
} |
|
|
} |
|
114 |
} |
} |
115 |
|
|
116 |
void |
/* Quantize all blocks -- Intra mode */ |
117 |
|
static __inline void |
118 |
MBQuantIntra(const MBParam * pParam, |
MBQuantIntra(const MBParam * pParam, |
119 |
FRAMEINFO * frame, |
const FRAMEINFO * const frame, |
120 |
MACROBLOCK *pMB, |
const MACROBLOCK * pMB, |
121 |
int16_t data[6*64], |
int16_t qcoeff[6 * 64], |
122 |
int16_t qcoeff[6 * 64]) |
int16_t data[6*64]) |
|
) |
|
123 |
{ |
{ |
124 |
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(); |
|
125 |
|
|
126 |
for (i = 0; i < 6; i++) { |
for (i = 0; i < 6; i++) { |
127 |
uint32_t iDcScaler = get_dc_scaler(iQuant, i < 4); |
uint32_t iDcScaler = get_dc_scaler(pMB->quant, i < 4); |
128 |
|
|
129 |
if (pParam->m_quant_type == H263_QUANT) { |
/* Quantize the block */ |
130 |
start_timer(); |
start_timer(); |
131 |
quant_intra(&qcoeff[i * 64], &data[i * 64], iQuant, iDcScaler); |
if (!(pParam->vol_flags & XVID_VOL_MPEGQUANT)) { |
132 |
stop_quant_timer(); |
quant_intra(&data[i * 64], &qcoeff[i * 64], pMB->quant, iDcScaler); |
133 |
} else { |
} else { |
134 |
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(); |
|
135 |
} |
} |
136 |
|
stop_quant_timer(); |
137 |
} |
} |
138 |
} |
} |
139 |
|
|
140 |
void |
/* DeQuantize all blocks -- Intra mode */ |
141 |
|
static __inline void |
142 |
MBDeQuantIntra(const MBParam * pParam, |
MBDeQuantIntra(const MBParam * pParam, |
143 |
const int iQuant, |
const int iQuant, |
144 |
int16_t qcoeff[6 * 64], |
int16_t qcoeff[6 * 64], |
149 |
for (i = 0; i < 6; i++) { |
for (i = 0; i < 6; i++) { |
150 |
uint32_t iDcScaler = get_dc_scaler(iQuant, i < 4); |
uint32_t iDcScaler = get_dc_scaler(iQuant, i < 4); |
151 |
|
|
|
if (pParam->m_quant_type == H263_QUANT) { |
|
|
start_timer(); |
|
|
dequant_intra(&data[i * 64], &qcoeff[i * 64], iQuant, iDcScaler); |
|
|
stop_iquant_timer(); |
|
|
} else { |
|
152 |
start_timer(); |
start_timer(); |
153 |
dequant4_intra(&data[i * 64], &qcoeff[i * 64], iQuant, iDcScaler); |
if (!(pParam->vol_flags & XVID_VOL_MPEGQUANT)) |
154 |
|
dequant_intra(&qcoeff[i * 64], &data[i * 64], iQuant, iDcScaler); |
155 |
|
else |
156 |
|
dequant4_intra(&qcoeff[i * 64], &data[i * 64], iQuant, iDcScaler); |
157 |
stop_iquant_timer(); |
stop_iquant_timer(); |
158 |
} |
} |
159 |
} |
} |
|
} |
|
160 |
|
|
161 |
uint8_t |
/* Quantize all blocks -- Inter mode */ |
162 |
|
static __inline uint8_t |
163 |
MBQuantInter(const MBParam * pParam, |
MBQuantInter(const MBParam * pParam, |
164 |
const int iQuant, |
const FRAMEINFO * const frame, |
165 |
|
const MACROBLOCK * pMB, |
166 |
int16_t data[6 * 64], |
int16_t data[6 * 64], |
167 |
int16_t qcoeff[6 * 64]) |
int16_t qcoeff[6 * 64], |
168 |
|
int bvop, |
169 |
|
int limit) |
170 |
{ |
{ |
171 |
|
|
172 |
int i; |
int i; |
173 |
uint8_t cbp = 0; |
uint8_t cbp = 0; |
174 |
int sum; |
int sum; |
175 |
|
int code_block; |
176 |
|
|
177 |
for (i = 0; i < 6; i++) { |
for (i = 0; i < 6; i++) { |
178 |
|
|
179 |
if (pParam->m_quant_type == 0) { |
/* Quantize the block */ |
180 |
start_timer(); |
start_timer(); |
181 |
sum = quant_inter(&qcoeff[i * 64], &data[i * 64], iQuant); |
if (!(pParam->vol_flags & XVID_VOL_MPEGQUANT)) { |
182 |
stop_quant_timer(); |
sum = quant_inter(&qcoeff[i*64], &data[i*64], pMB->quant); |
183 |
|
if ( (sum) && (frame->vop_flags & XVID_VOP_TRELLISQUANT) ) { |
184 |
|
sum = dct_quantize_trellis_inter_h263_c (&qcoeff[i*64], &data[i*64], pMB->quant)+1; |
185 |
|
limit = 1; |
186 |
|
} |
187 |
} else { |
} else { |
188 |
start_timer(); |
sum = quant4_inter(&qcoeff[i * 64], &data[i * 64], pMB->quant); |
189 |
sum = quant4_inter(&qcoeff[i * 64], &data[i * 64], iQuant); |
// if ( (sum) && (frame->vop_flags & XVID_VOP_TRELLISQUANT) ) |
190 |
stop_quant_timer(); |
// sum = dct_quantize_trellis_inter_mpeg_c (&qcoeff[i*64], &data[i*64], pMB->quant)+1; |
191 |
} |
} |
192 |
|
stop_quant_timer(); |
193 |
|
|
194 |
if (sum >= TOOSMALL_LIMIT) { // skip block ? |
/* |
195 |
cbp |= 1 << (5 - i); |
* We code the block if the sum is higher than the limit and if the first |
196 |
|
* two AC coefficients in zig zag order are not zero. |
197 |
|
*/ |
198 |
|
code_block = 0; |
199 |
|
if ((sum >= limit) || (qcoeff[i*64+1] != 0) || (qcoeff[i*64+8] != 0)) { |
200 |
|
code_block = 1; |
201 |
|
} else { |
202 |
|
|
203 |
|
if (bvop && (pMB->mode == MODE_DIRECT || pMB->mode == MODE_DIRECT_NO4V)) { |
204 |
|
/* dark blocks prevention for direct mode */ |
205 |
|
if ((qcoeff[i*64] < -1) || (qcoeff[i*64] > 0)) |
206 |
|
code_block = 1; |
207 |
|
} else { |
208 |
|
/* not direct mode */ |
209 |
|
if (qcoeff[i*64] != 0) |
210 |
|
code_block = 1; |
211 |
} |
} |
212 |
} |
} |
213 |
return cbp; |
|
214 |
|
/* Set the corresponding cbp bit */ |
215 |
|
cbp |= code_block << (5 - i); |
216 |
} |
} |
217 |
|
|
218 |
void |
return(cbp); |
219 |
|
} |
220 |
|
|
221 |
|
/* DeQuantize all blocks -- Inter mode */ |
222 |
|
static __inline void |
223 |
MBDeQuantInter( const MBParam * pParam, |
MBDeQuantInter( const MBParam * pParam, |
224 |
const int iQuant, |
const int iQuant, |
225 |
int16_t data[6 * 64], |
int16_t data[6 * 64], |
229 |
int i; |
int i; |
230 |
|
|
231 |
for (i = 0; i < 6; i++) { |
for (i = 0; i < 6; i++) { |
232 |
if (cbp & (1 << (5 - i))) |
if (cbp & (1 << (5 - i))) { |
|
{ |
|
|
if (pParam->m_quant_type == H263_QUANT) { |
|
233 |
start_timer(); |
start_timer(); |
234 |
|
if (!(pParam->vol_flags & XVID_VOL_MPEGQUANT)) |
235 |
dequant_inter(&data[i * 64], &qcoeff[i * 64], iQuant); |
dequant_inter(&data[i * 64], &qcoeff[i * 64], iQuant); |
236 |
stop_iquant_timer(); |
else |
|
} else { |
|
|
start_timer(); |
|
237 |
dequant4_inter(&data[i * 64], &qcoeff[i * 64], iQuant); |
dequant4_inter(&data[i * 64], &qcoeff[i * 64], iQuant); |
238 |
stop_iquant_timer(); |
stop_iquant_timer(); |
239 |
} |
} |
240 |
} |
} |
241 |
} |
} |
|
} |
|
242 |
|
|
243 |
void |
typedef void (transfer_operation_8to16_t) (int16_t *Dst, const uint8_t *Src, int BpS); |
244 |
MBiDCT( int16_t data[6 * 64], |
typedef void (transfer_operation_16to8_t) (uint8_t *Dst, const int16_t *Src, int BpS); |
|
const uint8_t cbp) |
|
|
{ |
|
|
int i; |
|
245 |
|
|
|
for (i = 0; i < 6; i++) { |
|
|
if (cbp & (1 << (5 - i))) |
|
|
{ |
|
|
start_timer(); |
|
|
idct(&data[i * 64]); |
|
|
stop_idct_timer(); |
|
246 |
|
|
247 |
} |
static __inline void |
248 |
} |
MBTrans8to16(const MBParam * const pParam, |
249 |
} |
const FRAMEINFO * const frame, |
250 |
|
const MACROBLOCK * const pMB, |
|
|
|
|
void |
|
|
MBTrans(const MBParam * pParam, |
|
|
FRAMEINFO * frame, |
|
|
MACROBLOCK * pMB, |
|
251 |
const uint32_t x_pos, |
const uint32_t x_pos, |
252 |
const uint32_t y_pos, |
const uint32_t y_pos, |
253 |
int16_t data[6 * 64]) |
int16_t data[6 * 64]) |
255 |
uint32_t stride = pParam->edged_width; |
uint32_t stride = pParam->edged_width; |
256 |
uint32_t stride2 = stride / 2; |
uint32_t stride2 = stride / 2; |
257 |
uint32_t next_block = stride * 8; |
uint32_t next_block = stride * 8; |
258 |
|
int32_t cst; |
259 |
uint8_t *pY_Cur, *pU_Cur, *pV_Cur; |
uint8_t *pY_Cur, *pU_Cur, *pV_Cur; |
260 |
IMAGE *pCurrent = &frame->image; |
const IMAGE * const pCurrent = &frame->image; |
261 |
|
transfer_operation_8to16_t *transfer_op = NULL; |
262 |
|
|
263 |
|
if ((frame->vop_flags & XVID_VOP_REDUCED)) { |
264 |
|
|
265 |
|
/* Image pointers */ |
266 |
|
pY_Cur = pCurrent->y + (y_pos << 5) * stride + (x_pos << 5); |
267 |
|
pU_Cur = pCurrent->u + (y_pos << 4) * stride2 + (x_pos << 4); |
268 |
|
pV_Cur = pCurrent->v + (y_pos << 4) * stride2 + (x_pos << 4); |
269 |
|
|
270 |
|
/* Block size */ |
271 |
|
cst = 16; |
272 |
|
|
273 |
|
/* Operation function */ |
274 |
|
transfer_op = (transfer_operation_8to16_t*)filter_18x18_to_8x8; |
275 |
|
} else { |
276 |
|
|
277 |
|
/* Image pointers */ |
278 |
pY_Cur = pCurrent->y + (y_pos << 4) * stride + (x_pos << 4); |
pY_Cur = pCurrent->y + (y_pos << 4) * stride + (x_pos << 4); |
279 |
pU_Cur = pCurrent->u + (y_pos << 3) * stride2 + (x_pos << 3); |
pU_Cur = pCurrent->u + (y_pos << 3) * stride2 + (x_pos << 3); |
280 |
pV_Cur = pCurrent->v + (y_pos << 3) * stride2 + (x_pos << 3); |
pV_Cur = pCurrent->v + (y_pos << 3) * stride2 + (x_pos << 3); |
281 |
|
|
282 |
|
/* Block size */ |
283 |
|
cst = 8; |
284 |
|
|
285 |
|
/* Operation function */ |
286 |
|
transfer_op = (transfer_operation_8to16_t*)transfer_8to16copy; |
287 |
|
} |
288 |
|
|
289 |
|
/* Do the transfer */ |
290 |
start_timer(); |
start_timer(); |
291 |
transfer_8to16copy(&data[0 * 64], pY_Cur, stride); |
transfer_op(&data[0 * 64], pY_Cur, stride); |
292 |
transfer_8to16copy(&data[1 * 64], pY_Cur + 8, stride); |
transfer_op(&data[1 * 64], pY_Cur + cst, stride); |
293 |
transfer_8to16copy(&data[2 * 64], pY_Cur + next_block, stride); |
transfer_op(&data[2 * 64], pY_Cur + next_block, stride); |
294 |
transfer_8to16copy(&data[3 * 64], pY_Cur + next_block + 8, stride); |
transfer_op(&data[3 * 64], pY_Cur + next_block + cst, stride); |
295 |
transfer_8to16copy(&data[4 * 64], pU_Cur, stride2); |
transfer_op(&data[4 * 64], pU_Cur, stride2); |
296 |
transfer_8to16copy(&data[5 * 64], pV_Cur, stride2); |
transfer_op(&data[5 * 64], pV_Cur, stride2); |
297 |
stop_transfer_timer(); |
stop_transfer_timer(); |
298 |
} |
} |
299 |
|
|
300 |
void |
static __inline void |
301 |
MBTransAdd(const MBParam * pParam, |
MBTrans16to8(const MBParam * const pParam, |
302 |
FRAMEINFO * frame, |
const FRAMEINFO * const frame, |
303 |
MACROBLOCK * pMB, |
const MACROBLOCK * const pMB, |
304 |
const uint32_t x_pos, |
const uint32_t x_pos, |
305 |
const uint32_t y_pos, |
const uint32_t y_pos, |
306 |
int16_t data[6 * 64], |
int16_t data[6 * 64], |
307 |
|
const uint32_t add, |
308 |
const uint8_t cbp) |
const uint8_t cbp) |
309 |
{ |
{ |
310 |
uint8_t *pY_Cur, *pU_Cur, *pV_Cur; |
uint8_t *pY_Cur, *pU_Cur, *pV_Cur; |
311 |
uint32_t stride = pParam->edged_width; |
uint32_t stride = pParam->edged_width; |
312 |
uint32_t stride2 = stride / 2; |
uint32_t stride2 = stride / 2; |
313 |
uint32_t next_block = stride * 8; |
uint32_t next_block = stride * 8; |
314 |
IMAGE *pCurrent = &frame->image; |
uint32_t cst; |
315 |
|
const IMAGE * const pCurrent = &frame->image; |
316 |
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); |
|
317 |
|
|
318 |
if (pMB->field_dct) { |
if (pMB->field_dct) { |
319 |
next_block = stride; |
next_block = stride; |
320 |
stride *= 2; |
stride *= 2; |
321 |
} |
} |
322 |
|
|
323 |
start_timer(); |
if ((frame->vop_flags & XVID_VOP_REDUCED)) { |
324 |
if (cbp & 32) |
|
325 |
transfer_16to8add(pY_Cur, &data[0 * 64], stride); |
/* Image pointers */ |
326 |
if (cbp & 16) |
pY_Cur = pCurrent->y + (y_pos << 5) * stride + (x_pos << 5); |
327 |
transfer_16to8add(pY_Cur + 8, &data[1 * 64], stride); |
pU_Cur = pCurrent->u + (y_pos << 4) * stride2 + (x_pos << 4); |
328 |
if (cbp & 8) |
pV_Cur = pCurrent->v + (y_pos << 4) * stride2 + (x_pos << 4); |
329 |
transfer_16to8add(pY_Cur + next_block, &data[2 * 64], stride); |
|
330 |
if (cbp & 4) |
/* Block size */ |
331 |
transfer_16to8add(pY_Cur + next_block + 8, &data[3 * 64], stride); |
cst = 16; |
332 |
if (cbp & 2) |
|
333 |
transfer_16to8add(pU_Cur, &data[4 * 64], stride2); |
/* Operation function */ |
334 |
if (cbp & 1) |
if(add) |
335 |
transfer_16to8add(pV_Cur, &data[5 * 64], stride2); |
transfer_op = (transfer_operation_16to8_t*)add_upsampled_8x8_16to8; |
336 |
|
else |
337 |
|
transfer_op = (transfer_operation_16to8_t*)copy_upsampled_8x8_16to8; |
338 |
|
} else { |
339 |
|
|
340 |
|
/* Image pointers */ |
341 |
|
pY_Cur = pCurrent->y + (y_pos << 4) * stride + (x_pos << 4); |
342 |
|
pU_Cur = pCurrent->u + (y_pos << 3) * stride2 + (x_pos << 3); |
343 |
|
pV_Cur = pCurrent->v + (y_pos << 3) * stride2 + (x_pos << 3); |
344 |
|
|
345 |
|
/* Block size */ |
346 |
|
cst = 8; |
347 |
|
|
348 |
|
/* Operation function */ |
349 |
|
if(add) |
350 |
|
transfer_op = (transfer_operation_16to8_t*)transfer_16to8add; |
351 |
|
else |
352 |
|
transfer_op = (transfer_operation_16to8_t*)transfer_16to8copy; |
353 |
|
} |
354 |
|
|
355 |
|
/* Do the operation */ |
356 |
|
start_timer(); |
357 |
|
if (cbp&32) transfer_op(pY_Cur, &data[0 * 64], stride); |
358 |
|
if (cbp&16) transfer_op(pY_Cur + cst, &data[1 * 64], stride); |
359 |
|
if (cbp& 8) transfer_op(pY_Cur + next_block, &data[2 * 64], stride); |
360 |
|
if (cbp& 4) transfer_op(pY_Cur + next_block + cst, &data[3 * 64], stride); |
361 |
|
if (cbp& 2) transfer_op(pU_Cur, &data[4 * 64], stride2); |
362 |
|
if (cbp& 1) transfer_op(pV_Cur, &data[5 * 64], stride2); |
363 |
stop_transfer_timer(); |
stop_transfer_timer(); |
364 |
} |
} |
365 |
|
|
366 |
|
/***************************************************************************** |
367 |
|
* Module functions |
368 |
|
****************************************************************************/ |
369 |
|
|
370 |
|
void |
371 |
|
MBTransQuantIntra(const MBParam * const pParam, |
372 |
|
const FRAMEINFO * const frame, |
373 |
|
MACROBLOCK * const pMB, |
374 |
|
const uint32_t x_pos, |
375 |
|
const uint32_t y_pos, |
376 |
|
int16_t data[6 * 64], |
377 |
|
int16_t qcoeff[6 * 64]) |
378 |
|
{ |
379 |
|
|
380 |
|
/* Transfer data */ |
381 |
|
MBTrans8to16(pParam, frame, pMB, x_pos, y_pos, data); |
382 |
|
|
383 |
|
/* Perform DCT (and field decision) */ |
384 |
|
MBfDCT(pParam, frame, pMB, x_pos, y_pos, data); |
385 |
|
|
386 |
/* if sum(diff between field lines) < sum(diff between frame lines), use field dct */ |
/* Quantize the block */ |
387 |
|
MBQuantIntra(pParam, frame, pMB, data, qcoeff); |
388 |
|
|
389 |
|
/* DeQuantize the block */ |
390 |
|
MBDeQuantIntra(pParam, pMB->quant, data, qcoeff); |
391 |
|
|
392 |
uint32_t |
/* Perform inverse DCT*/ |
393 |
MBDecideFieldDCT(int16_t data[6 * 64]) |
MBiDCT(data, 0x3F); |
394 |
|
|
395 |
|
/* Transfer back the data -- Don't add data */ |
396 |
|
MBTrans16to8(pParam, frame, pMB, x_pos, y_pos, data, 0, 0x3F); |
397 |
|
} |
398 |
|
|
399 |
|
|
400 |
|
uint8_t |
401 |
|
MBTransQuantInter(const MBParam * const pParam, |
402 |
|
const FRAMEINFO * const frame, |
403 |
|
MACROBLOCK * const pMB, |
404 |
|
const uint32_t x_pos, |
405 |
|
const uint32_t y_pos, |
406 |
|
int16_t data[6 * 64], |
407 |
|
int16_t qcoeff[6 * 64]) |
408 |
|
{ |
409 |
|
uint8_t cbp; |
410 |
|
uint32_t limit; |
411 |
|
|
412 |
|
/* |
413 |
|
* There is no MBTrans8to16 for Inter block, that's done in motion compensation |
414 |
|
* already |
415 |
|
*/ |
416 |
|
|
417 |
|
/* Perform DCT (and field decision) */ |
418 |
|
MBfDCT(pParam, frame, pMB, x_pos, y_pos, data); |
419 |
|
|
420 |
|
/* Set the limit threshold */ |
421 |
|
limit = PVOP_TOOSMALL_LIMIT + ((pMB->quant == 1)? 1 : 0); |
422 |
|
|
423 |
|
/* Quantize the block */ |
424 |
|
cbp = MBQuantInter(pParam, frame, pMB, data, qcoeff, 0, limit); |
425 |
|
|
426 |
|
/* DeQuantize the block */ |
427 |
|
MBDeQuantInter(pParam, pMB->quant, data, qcoeff, cbp); |
428 |
|
|
429 |
|
/* Perform inverse DCT*/ |
430 |
|
MBiDCT(data, cbp); |
431 |
|
|
432 |
|
/* Transfer back the data -- Add the data */ |
433 |
|
MBTrans16to8(pParam, frame, pMB, x_pos, y_pos, data, 1, cbp); |
434 |
|
|
435 |
|
return(cbp); |
436 |
|
} |
437 |
|
|
438 |
|
uint8_t |
439 |
|
MBTransQuantInterBVOP(const MBParam * pParam, |
440 |
|
FRAMEINFO * frame, |
441 |
|
MACROBLOCK * pMB, |
442 |
|
const uint32_t x_pos, |
443 |
|
const uint32_t y_pos, |
444 |
|
int16_t data[6 * 64], |
445 |
|
int16_t qcoeff[6 * 64]) |
446 |
{ |
{ |
447 |
|
uint8_t cbp; |
448 |
|
uint32_t limit; |
449 |
|
|
450 |
|
/* |
451 |
|
* There is no MBTrans8to16 for Inter block, that's done in motion compensation |
452 |
|
* already |
453 |
|
*/ |
454 |
|
|
455 |
|
/* Perform DCT (and field decision) */ |
456 |
|
MBfDCT(pParam, frame, pMB, x_pos, y_pos, data); |
457 |
|
|
458 |
|
/* Set the limit threshold */ |
459 |
|
limit = BVOP_TOOSMALL_LIMIT; |
460 |
|
|
461 |
|
/* Quantize the block */ |
462 |
|
cbp = MBQuantInter(pParam, frame, pMB, data, qcoeff, 1, limit); |
463 |
|
|
464 |
|
/* |
465 |
|
* History comment: |
466 |
|
* We don't have to DeQuant, iDCT and Transfer back data for B-frames. |
467 |
|
* |
468 |
|
* BUT some plugins require the original frame to be passed so we have |
469 |
|
* to take care of that here |
470 |
|
*/ |
471 |
|
if((pParam->plugin_flags & XVID_REQORIGINAL)) { |
472 |
|
|
473 |
|
/* DeQuantize the block */ |
474 |
|
MBDeQuantInter(pParam, pMB->quant, data, qcoeff, cbp); |
475 |
|
|
476 |
|
/* Perform inverse DCT*/ |
477 |
|
MBiDCT(data, cbp); |
478 |
|
|
479 |
|
/* Transfer back the data -- Add the data */ |
480 |
|
MBTrans16to8(pParam, frame, pMB, x_pos, y_pos, data, 1, cbp); |
481 |
|
} |
482 |
|
|
483 |
|
return(cbp); |
484 |
|
} |
485 |
|
|
486 |
|
/* if sum(diff between field lines) < sum(diff between frame lines), use field dct */ |
487 |
|
uint32_t |
488 |
|
MBFieldTest_c(int16_t data[6 * 64]) |
489 |
|
{ |
490 |
const uint8_t blocks[] = |
const uint8_t blocks[] = |
491 |
{ 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 }; |
492 |
const uint8_t lines[] = { 0, 16, 32, 48, 0, 16, 32, 48 }; |
const uint8_t lines[] = { 0, 16, 32, 48, 0, 16, 32, 48 }; |
497 |
for (i = 0; i < 7; ++i) { |
for (i = 0; i < 7; ++i) { |
498 |
for (j = 0; j < 8; ++j) { |
for (j = 0; j < 8; ++j) { |
499 |
frame += |
frame += |
500 |
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]); |
501 |
frame += |
frame += |
502 |
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]); |
503 |
frame += |
frame += |
504 |
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]); |
505 |
frame += |
frame += |
506 |
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]); |
507 |
|
|
508 |
field += |
field += |
509 |
ABS(data[blocks[i + 1] + lines[i + 1] + j] - |
abs(data[blocks[i + 1] + lines[i + 1] + j] - |
510 |
data[blocks[i] + lines[i] + j]); |
data[blocks[i] + lines[i] + j]); |
511 |
field += |
field += |
512 |
ABS(data[blocks[i + 1] + lines[i + 1] + 8 + j] - |
abs(data[blocks[i + 1] + lines[i + 1] + 8 + j] - |
513 |
data[blocks[i] + lines[i] + 8 + j]); |
data[blocks[i] + lines[i] + 8 + j]); |
514 |
field += |
field += |
515 |
ABS(data[blocks[i + 1] + 64 + lines[i + 1] + j] - |
abs(data[blocks[i + 1] + 64 + lines[i + 1] + j] - |
516 |
data[blocks[i] + 64 + lines[i] + j]); |
data[blocks[i] + 64 + lines[i] + j]); |
517 |
field += |
field += |
518 |
ABS(data[blocks[i + 1] + 64 + lines[i + 1] + 8 + j] - |
abs(data[blocks[i + 1] + 64 + lines[i + 1] + 8 + j] - |
519 |
data[blocks[i] + 64 + lines[i] + 8 + j]); |
data[blocks[i] + 64 + lines[i] + 8 + j]); |
520 |
} |
} |
521 |
} |
} |
522 |
|
|
523 |
if (frame > field) { |
return (frame >= (field + 350)); |
|
MBFrameToField(data); |
|
|
} |
|
|
|
|
|
return (frame > (field + 350)); |
|
524 |
} |
} |
525 |
|
|
526 |
|
|