1 |
|
/****************************************************************************** |
2 |
|
* * |
3 |
|
* This file is part of XviD, a free MPEG-4 video encoder/decoder * |
4 |
|
* * |
5 |
|
* XviD is an implementation of a part of one or more MPEG-4 Video tools * |
6 |
|
* as specified in ISO/IEC 14496-2 standard. Those intending to use this * |
7 |
|
* software module in hardware or software products are advised that its * |
8 |
|
* use may infringe existing patents or copyrights, and any such use * |
9 |
|
* would be at such party's own risk. The original developer of this * |
10 |
|
* software module and his/her company, and subsequent editors and their * |
11 |
|
* companies, will have no liability for use of this software or * |
12 |
|
* modifications or derivatives thereof. * |
13 |
|
* * |
14 |
|
* XviD is free software; you can redistribute it and/or modify it * |
15 |
|
* under the terms of the GNU General Public License as published by * |
16 |
|
* the Free Software Foundation; either version 2 of the License, or * |
17 |
|
* (at your option) any later version. * |
18 |
|
* * |
19 |
|
* XviD is distributed in the hope that it will be useful, but * |
20 |
|
* WITHOUT ANY WARRANTY; without even the implied warranty of * |
21 |
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
22 |
|
* GNU General Public License for more details. * |
23 |
|
* * |
24 |
|
* You should have received a copy of the GNU General Public License * |
25 |
|
* along with this program; if not, write to the Free Software * |
26 |
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * |
27 |
|
* * |
28 |
|
******************************************************************************/ |
29 |
|
|
30 |
|
/****************************************************************************** |
31 |
|
* * |
32 |
|
* mbcoding.c * |
33 |
|
* * |
34 |
|
* Copyright (C) 2002 - Michael Militzer <isibaar@xvid.org> * |
35 |
|
* * |
36 |
|
* For more information visit the XviD homepage: http://www.xvid.org * |
37 |
|
* * |
38 |
|
******************************************************************************/ |
39 |
|
|
40 |
|
/****************************************************************************** |
41 |
|
* * |
42 |
|
* Revision history: * |
43 |
|
* * |
44 |
|
* 14.04.2002 bframe encoding * |
45 |
|
* 08.03.2002 initial version; isibaar * |
46 |
|
* * |
47 |
|
******************************************************************************/ |
48 |
|
|
49 |
|
|
50 |
|
|
51 |
|
#include <stdlib.h> |
52 |
#include "../portab.h" |
#include "../portab.h" |
53 |
#include "bitstream.h" |
#include "bitstream.h" |
54 |
#include "zigzag.h" |
#include "zigzag.h" |
55 |
#include "vlc_codes.h" |
#include "vlc_codes.h" |
56 |
|
#include "mbcoding.h" |
57 |
|
|
58 |
#include "../utils/mbfunctions.h" |
#include "../utils/mbfunctions.h" |
59 |
|
|
|
#include <stdlib.h> /* malloc, free */ |
|
|
|
|
|
#define ESCAPE 7167 |
|
60 |
#define ABS(X) (((X)>0)?(X):-(X)) |
#define ABS(X) (((X)>0)?(X):-(X)) |
61 |
#define CLIP(X,A) (X > A) ? (A) : (X) |
#define CLIP(X,A) (X > A) ? (A) : (X) |
62 |
|
|
63 |
static VLC *DCT3D[2]; |
VLC intra_table[524032]; |
64 |
|
VLC inter_table[524032]; |
65 |
|
|
66 |
VLC *intra_table, *inter_table; |
VLC DCT3Dintra[4096]; |
67 |
static short clip_table[4096]; |
VLC DCT3Dinter[4096]; |
68 |
|
|
69 |
void create_vlc_tables(void) |
void init_vlc_tables(void) |
70 |
{ |
{ |
71 |
|
|
72 |
int32_t k, l, i, intra, last; |
int32_t k, l, i, intra, last; |
73 |
VLC *vlc[2]; |
VLC *vlc[2]; |
74 |
VLC **coeff_ptr; |
VLC **coeff_ptr; |
75 |
VLC *vlc1, *vlc2; |
VLC *vlc1, *vlc2; |
76 |
|
|
|
VLC *DCT3Dintra; |
|
|
VLC *DCT3Dinter; |
|
|
|
|
|
DCT3Dintra = (VLC *) malloc(sizeof(VLC) * 4096); |
|
|
DCT3Dinter = (VLC *) malloc(sizeof(VLC) * 4096); |
|
|
|
|
77 |
vlc1 = DCT3Dintra; |
vlc1 = DCT3Dintra; |
78 |
vlc2 = DCT3Dinter; |
vlc2 = DCT3Dinter; |
79 |
|
|
80 |
vlc[0] = intra_table = (VLC *) malloc(128 * 511 * sizeof(VLC)); |
vlc[0] = intra_table; |
81 |
vlc[1] = inter_table = (VLC *) malloc(128 * 511 * sizeof(VLC)); |
vlc[1] = inter_table; |
82 |
|
|
83 |
// initialize the clipping table |
// generate encoding vlc lookup tables |
84 |
for(i = -2048; i < 2048; i++) { |
// the lookup table idea is taken from the excellent fame project by Vivien Chapellier |
|
clip_table[i + 2048] = i; |
|
|
if(i < -255) |
|
|
clip_table[i + 2048] = -255; |
|
|
if(i > 255) |
|
|
clip_table[i + 2048] = 255; |
|
|
} |
|
|
|
|
|
// generate intra/inter vlc lookup table |
|
85 |
for(i = 0; i < 4; i++) { |
for(i = 0; i < 4; i++) { |
86 |
intra = i % 2; |
intra = i % 2; |
87 |
last = i >> 1; |
last = i / 2; |
88 |
|
|
89 |
coeff_ptr = coeff_vlc[last + (intra << 1)]; |
coeff_ptr = coeff_vlc[last + 2 * intra]; |
90 |
|
|
91 |
for(k = -255; k < 256; k++) { // level |
for(k = -2047; k < 2048; k++) { // level |
92 |
char *max_level_ptr = max_level[last + (intra << 1)]; |
int8_t *max_level_ptr = max_level[last + 2 * intra]; |
93 |
char *max_run_ptr = max_run[last + (intra << 1)]; |
int8_t *max_run_ptr = max_run[last + 2 * intra]; |
94 |
|
|
95 |
for(l = 0; l < 64; l++) { // run |
for(l = 0; l < 64; l++) { // run |
96 |
int32_t level = k, run = l; |
int32_t level = k; |
97 |
|
uint32_t run = l; |
98 |
|
|
99 |
if(abs(level) <= max_level_ptr[run] && run <= max_run_ptr[abs(level)]) { |
if((abs(level) <= max_level_ptr[run]) && |
100 |
|
(run <= (uint32_t)max_run_ptr[abs(level)])) { // level < max_level and run < max_run |
101 |
|
|
|
if(level > 0) { |
|
|
vlc[intra]->code = (coeff_ptr[run][level - 1].code) << 1; |
|
|
vlc[intra]->len = coeff_ptr[run][level - 1].len + 1; |
|
|
} |
|
|
else if(level < 0) { |
|
|
vlc[intra]->code = ((coeff_ptr[run][-level - 1].code) << 1) + 1; |
|
|
vlc[intra]->len = coeff_ptr[run][-level - 1].len + 1; |
|
|
} |
|
|
else { |
|
102 |
vlc[intra]->code = 0; |
vlc[intra]->code = 0; |
103 |
vlc[intra]->len = 0; |
vlc[intra]->len = 0; |
104 |
|
goto loop_end; |
105 |
} |
} |
106 |
} else { |
else { |
107 |
if(level > 0) |
if(level > 0) // correct level |
108 |
level -= max_level_ptr[run]; |
level -= max_level_ptr[run]; |
109 |
else |
else |
110 |
level += max_level_ptr[run]; |
level += max_level_ptr[run]; |
111 |
|
|
112 |
if(abs(level) <= max_level_ptr[run] && |
if((abs(level) <= max_level_ptr[run]) && |
113 |
run <= max_run_ptr[abs(level)]) { |
(run <= (uint32_t) max_run_ptr[abs(level)])) { |
114 |
|
|
|
if(level > 0) { |
|
|
vlc[intra]->code = (0x06 << (coeff_ptr[run][level - 1].len + 1)) | |
|
|
(coeff_ptr[run][level - 1].code << 1); |
|
|
vlc[intra]->len = (coeff_ptr[run][level - 1].len + 1) + 8; |
|
|
} |
|
|
else if(level < 0) { |
|
|
vlc[intra]->code = (0x06 << (coeff_ptr[run][-level - 1].len + 1)) | |
|
|
((coeff_ptr[run][-level - 1].code << 1) + 1); |
|
|
vlc[intra]->len = (coeff_ptr[run][-level - 1].len + 1) + 8; |
|
|
} |
|
|
else { |
|
115 |
vlc[intra]->code = 0x06; |
vlc[intra]->code = 0x06; |
116 |
vlc[intra]->len = 8; |
vlc[intra]->len = 8; |
117 |
|
goto loop_end; |
118 |
} |
} |
119 |
} else { |
|
120 |
if(level > 0) |
if(level > 0) // still here? |
121 |
level += max_level_ptr[run]; |
level += max_level_ptr[run]; // restore level |
122 |
else |
else |
123 |
level -= max_level_ptr[run]; |
level -= max_level_ptr[run]; |
124 |
|
|
125 |
run -= max_run_ptr[abs(level)] + 1; |
run -= max_run_ptr[abs(level)] + 1; // and change run |
126 |
|
|
127 |
if(abs(level) <= max_level_ptr[run] && |
if((abs(level) <= max_level_ptr[run]) && |
128 |
run <= max_run_ptr[abs(level)]) { |
(run <= (uint32_t) max_run_ptr[abs(level)])) { |
129 |
|
|
|
if(level > 0) { |
|
|
vlc[intra]->code = (0x0e << (coeff_ptr[run][level - 1].len + 1)) | |
|
|
(coeff_ptr[run][level - 1].code << 1); |
|
|
vlc[intra]->len = (coeff_ptr[run][level - 1].len + 1) + 9; |
|
|
} |
|
|
else if(level < 0) { |
|
|
vlc[intra]->code = (0x0e << (coeff_ptr[run][-level - 1].len + 1)) | |
|
|
((coeff_ptr[run][-level - 1].code << 1) + 1); |
|
|
vlc[intra]->len = (coeff_ptr[run][-level - 1].len + 1) + 9; |
|
|
} |
|
|
else { |
|
130 |
vlc[intra]->code = 0x0e; |
vlc[intra]->code = 0x0e; |
131 |
vlc[intra]->len = 9; |
vlc[intra]->len = 9; |
132 |
|
goto loop_end; |
133 |
} |
} |
|
} else { |
|
|
if(level != 0) |
|
134 |
run += max_run_ptr[abs(level)] + 1; |
run += max_run_ptr[abs(level)] + 1; |
135 |
else |
} |
|
run++; |
|
136 |
|
|
137 |
vlc[intra]->code = (uint32_t) ((0x1e + last) << 20) | |
vlc[intra]->code = (uint32_t) ((l << 14) | (0x1e + last) << 20) | |
138 |
(l << 14) | (1 << 13) | ((k & 0xfff) << 1) | 1; |
(1 << 13) | ((k & 0xfff) << 1) | 1; |
139 |
|
|
140 |
vlc[intra]->len = 30; |
vlc[intra]->len = 30; |
141 |
|
vlc[intra]++; |
142 |
|
continue; |
143 |
|
|
144 |
|
loop_end: |
145 |
|
if(level != 0) { |
146 |
|
vlc[intra]->code = (vlc[intra]->code << (coeff_ptr[run][abs(level) - 1].len + 1)) | |
147 |
|
(coeff_ptr[run][abs(level) - 1].code << 1); |
148 |
|
vlc[intra]->len = (coeff_ptr[run][abs(level) - 1].len + 1) + vlc[intra]->len; |
149 |
|
|
150 |
|
if(level < 0) |
151 |
|
vlc[intra]->code += 1; |
152 |
} |
} |
153 |
} |
|
|
} |
|
154 |
vlc[intra]++; |
vlc[intra]++; |
155 |
} |
} |
156 |
} |
} |
157 |
} |
} |
|
intra_table += 64*255; // center vlc tables |
|
|
inter_table += 64*255; // center vlc tables |
|
158 |
|
|
159 |
for(i = 0; i < 4096; i++) { |
for(i = 0; i < 4096; i++) { |
160 |
if(i >= 512) { |
if(i >= 512) { |
182 |
|
|
183 |
} |
} |
184 |
|
|
185 |
void destroy_vlc_tables(void) { |
static __inline void CodeVector(Bitstream *bs, |
186 |
|
int32_t value, |
187 |
if(intra_table != NULL && inter_table != NULL) { |
int32_t f_code, |
188 |
intra_table -= 64*255; // uncenter vlc tables |
Statistics *pStat) |
|
inter_table -= 64*255; // uncenter vlc tables |
|
|
|
|
|
free(intra_table); |
|
|
free(inter_table); |
|
|
} |
|
|
|
|
|
if(DCT3D[0] != NULL && DCT3D[1] != NULL) { |
|
|
free(DCT3D[0]); |
|
|
free(DCT3D[1]); |
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
static __inline void CodeVector(Bitstream *bs, int16_t value, int16_t f_code, Statistics *pStat) |
|
189 |
{ |
{ |
190 |
|
|
191 |
const int scale_factor = 1 << (f_code - 1); |
const int scale_factor = 1 << (f_code - 1); |
192 |
const int cmp = scale_factor << 5; |
const int cmp = scale_factor << 5; |
193 |
|
|
200 |
pStat->iMvSum += value * value; |
pStat->iMvSum += value * value; |
201 |
pStat->iMvCount++; |
pStat->iMvCount++; |
202 |
|
|
203 |
if (value == 0) |
if (value == 0) { |
204 |
BitstreamPutBits(bs, mb_motion_table[32].code, mb_motion_table[32].len); |
BitstreamPutBits(bs, mb_motion_table[32].code, mb_motion_table[32].len); |
205 |
else { |
} else { |
206 |
uint16_t length, code, mv_res, sign; |
uint16_t length, code, mv_res, sign; |
207 |
|
|
208 |
length = 16 << f_code; |
length = 16 << f_code; |
231 |
if(f_code) |
if(f_code) |
232 |
BitstreamPutBits(bs, mv_res, f_code); |
BitstreamPutBits(bs, mv_res, f_code); |
233 |
} |
} |
234 |
|
|
235 |
} |
} |
236 |
|
|
237 |
|
|
238 |
static __inline void CodeCoeff(Bitstream *bs, int16_t qcoeff[64], VLC *table, |
static __inline void CodeCoeff(Bitstream *bs, |
239 |
const uint16_t *zigzag, uint16_t intra) { |
const int16_t qcoeff[64], |
240 |
|
VLC *table, |
241 |
|
const uint16_t *zigzag, |
242 |
|
uint16_t intra) |
243 |
|
{ |
244 |
|
|
245 |
uint32_t j, last; |
uint32_t j, last; |
246 |
short v; |
short v; |
247 |
VLC *vlc; |
VLC *vlc; |
248 |
|
|
249 |
j = intra; |
j = intra; |
250 |
last = 1 + intra; |
last = intra; |
251 |
|
|
252 |
while((v = qcoeff[zigzag[j++]]) == 0); |
while(j < 64 && (v = qcoeff[zigzag[j]]) == 0) j++; |
253 |
|
|
254 |
do { |
do { |
255 |
|
vlc = table + 64 * 2047 + (v << 6) + j - last; |
256 |
|
last = ++j; |
257 |
|
|
258 |
// count zeroes |
// count zeroes |
259 |
vlc = table + (clip_table[2048+v] << 6) + j - last; |
while(j < 64 && (v = qcoeff[zigzag[j]]) == 0) j++; |
|
last = j + 1; |
|
|
while(j < 64 && (v = qcoeff[zigzag[j++]]) == 0); |
|
260 |
|
|
261 |
// write code |
// write code |
262 |
if(j != 64) { |
if(j != 64) { |
263 |
BitstreamPutBits(bs, vlc->code, vlc->len); |
BitstreamPutBits(bs, vlc->code, vlc->len); |
264 |
} else { |
} else { |
265 |
vlc += 64*511; |
vlc += 64 * 4095; |
266 |
BitstreamPutBits(bs, vlc->code, vlc->len); |
BitstreamPutBits(bs, vlc->code, vlc->len); |
267 |
break; |
break; |
268 |
} |
} |
269 |
} while(1); |
} while(1); |
270 |
|
|
271 |
} |
} |
272 |
|
|
273 |
|
|
274 |
static void CodeBlockIntra(const MBParam * pParam, const MACROBLOCK *pMB, |
static void CodeBlockIntra(const FRAMEINFO * frame, |
275 |
int16_t qcoeff[][64], Bitstream * bs, Statistics * pStat) |
const MACROBLOCK *pMB, |
276 |
|
int16_t qcoeff[6*64], |
277 |
|
Bitstream * bs, |
278 |
|
Statistics * pStat) |
279 |
{ |
{ |
280 |
|
|
281 |
uint32_t i, mcbpc, cbpy, bits; |
uint32_t i, mcbpc, cbpy, bits; |
282 |
|
|
|
mcbpc = pMB->cbp & 3; |
|
283 |
cbpy = pMB->cbp >> 2; |
cbpy = pMB->cbp >> 2; |
284 |
|
|
285 |
// write mcbpc |
// write mcbpc |
286 |
if(pParam->coding_type == I_VOP) |
if(frame->coding_type == I_VOP) { |
287 |
BitstreamPutBits(bs, mcbpc_I[mcbpc].code, mcbpc_I[mcbpc].len); |
mcbpc = ((pMB->mode >> 1) & 3) | ((pMB->cbp & 3) << 2); |
288 |
else |
BitstreamPutBits(bs, mcbpc_intra_tab[mcbpc].code, mcbpc_intra_tab[mcbpc].len); |
289 |
BitstreamPutBits(bs, mcbpc_P_intra[mcbpc].code, mcbpc_P_intra[mcbpc].len); |
} |
290 |
|
else { |
291 |
|
mcbpc = (pMB->mode & 7) | ((pMB->cbp & 3) << 3); |
292 |
|
BitstreamPutBits(bs, mcbpc_inter_tab[mcbpc].code, mcbpc_inter_tab[mcbpc].len); |
293 |
|
} |
294 |
|
|
295 |
// ac prediction flag |
// ac prediction flag |
296 |
if(pMB->acpred_directions[0]) |
if(pMB->acpred_directions[0]) |
305 |
if(pMB->mode == MODE_INTRA_Q) |
if(pMB->mode == MODE_INTRA_Q) |
306 |
BitstreamPutBits(bs, pMB->dquant, 2); |
BitstreamPutBits(bs, pMB->dquant, 2); |
307 |
|
|
308 |
|
// write interlacing |
309 |
|
if (frame->global_flags & XVID_INTERLACING) |
310 |
|
{ |
311 |
|
BitstreamPutBit(bs, pMB->field_dct); |
312 |
|
} |
313 |
|
|
314 |
// code block coeffs |
// code block coeffs |
315 |
for(i = 0; i < 6; i++) |
for(i = 0; i < 6; i++) |
316 |
{ |
{ |
317 |
if(i < 4) |
if(i < 4) |
318 |
BitstreamPutBits(bs, dcy_tab[qcoeff[i][0] + 255].code, |
BitstreamPutBits(bs, |
319 |
dcy_tab[qcoeff[i][0] + 255].len); |
dcy_tab[qcoeff[i*64 + 0] + 255].code, |
320 |
else |
dcy_tab[qcoeff[i*64 + 0] + 255].len); |
321 |
BitstreamPutBits(bs, dcc_tab[qcoeff[i][0] + 255].code, |
else |
322 |
dcc_tab[qcoeff[i][0] + 255].len); |
BitstreamPutBits(bs, |
323 |
|
dcc_tab[qcoeff[i*64 + 0] + 255].code, |
324 |
|
dcc_tab[qcoeff[i*64 + 0] + 255].len); |
325 |
|
|
326 |
if(pMB->cbp & (1 << (5 - i))) |
if(pMB->cbp & (1 << (5 - i))) |
327 |
{ |
{ |
328 |
bits = BitstreamPos(bs); |
bits = BitstreamPos(bs); |
329 |
|
|
330 |
CodeCoeff(bs, qcoeff[i], intra_table, scan_tables[pMB->acpred_directions[i]], 1); |
CodeCoeff(bs, |
331 |
|
&qcoeff[i*64], |
332 |
|
intra_table, |
333 |
|
scan_tables[pMB->acpred_directions[i]], |
334 |
|
1); |
335 |
|
|
336 |
bits = BitstreamPos(bs) - bits; |
bits = BitstreamPos(bs) - bits; |
337 |
pStat->iTextBits += bits; |
pStat->iTextBits += bits; |
338 |
} |
} |
339 |
} |
} |
340 |
|
|
341 |
} |
} |
342 |
|
|
343 |
|
|
344 |
static void CodeBlockInter(const MBParam * pParam, const MACROBLOCK *pMB, |
static void CodeBlockInter(const FRAMEINFO * frame, |
345 |
int16_t qcoeff[][64], Bitstream * bs, Statistics * pStat) |
const MACROBLOCK *pMB, |
346 |
|
int16_t qcoeff[6*64], |
347 |
|
Bitstream * bs, |
348 |
|
Statistics * pStat) |
349 |
{ |
{ |
350 |
|
|
351 |
int32_t i; |
int32_t i; |
352 |
uint32_t bits, mcbpc, cbpy; |
uint32_t bits, mcbpc, cbpy; |
353 |
|
|
354 |
mcbpc = pMB->cbp & 3; |
mcbpc = (pMB->mode & 7) | ((pMB->cbp & 3) << 3); |
355 |
cbpy = 15 - (pMB->cbp >> 2); |
cbpy = 15 - (pMB->cbp >> 2); |
356 |
|
|
357 |
// write mcbpc |
// write mcbpc |
358 |
if(pMB->mode == MODE_INTER4V) |
BitstreamPutBits(bs, mcbpc_inter_tab[mcbpc].code, mcbpc_inter_tab[mcbpc].len); |
|
BitstreamPutBits(bs, mcbpc_P_inter4v[mcbpc].code, mcbpc_P_inter4v[mcbpc].len); |
|
|
else |
|
|
BitstreamPutBits(bs, mcbpc_P_inter[mcbpc].code, mcbpc_P_inter[mcbpc].len); |
|
359 |
|
|
360 |
// write cbpy |
// write cbpy |
361 |
BitstreamPutBits(bs, cbpy_tab[cbpy].code, cbpy_tab[cbpy].len); |
BitstreamPutBits(bs, cbpy_tab[cbpy].code, cbpy_tab[cbpy].len); |
364 |
if(pMB->mode == MODE_INTER_Q) |
if(pMB->mode == MODE_INTER_Q) |
365 |
BitstreamPutBits(bs, pMB->dquant, 2); |
BitstreamPutBits(bs, pMB->dquant, 2); |
366 |
|
|
367 |
|
// interlacing |
368 |
|
if (frame->global_flags & XVID_INTERLACING) |
369 |
|
{ |
370 |
|
BitstreamPutBit(bs, pMB->field_dct); |
371 |
|
DEBUG1("codep: field_dct: ", pMB->field_dct); |
372 |
|
|
373 |
|
// if inter block, write field ME flag |
374 |
|
if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) |
375 |
|
{ |
376 |
|
BitstreamPutBit(bs, pMB->field_pred); |
377 |
|
DEBUG1("codep: field_pred: ", pMB->field_pred); |
378 |
|
|
379 |
|
// write field prediction references |
380 |
|
if (pMB->field_pred) |
381 |
|
{ |
382 |
|
BitstreamPutBit(bs, pMB->field_for_top); |
383 |
|
BitstreamPutBit(bs, pMB->field_for_bot); |
384 |
|
} |
385 |
|
} |
386 |
|
} |
387 |
|
|
388 |
// code motion vector(s) |
// code motion vector(s) |
389 |
for(i = 0; i < (pMB->mode == MODE_INTER4V ? 4 : 1); i++) |
for(i = 0; i < (pMB->mode == MODE_INTER4V ? 4 : 1); i++) |
390 |
{ |
{ |
391 |
CodeVector(bs, pMB->pmvs[i].x, pParam->fixed_code, pStat); |
CodeVector(bs, pMB->pmvs[i].x, frame->fcode, pStat); |
392 |
CodeVector(bs, pMB->pmvs[i].y, pParam->fixed_code, pStat); |
CodeVector(bs, pMB->pmvs[i].y, frame->fcode, pStat); |
393 |
} |
} |
394 |
|
|
395 |
bits = BitstreamPos(bs); |
bits = BitstreamPos(bs); |
397 |
// code block coeffs |
// code block coeffs |
398 |
for(i = 0; i < 6; i++) |
for(i = 0; i < 6; i++) |
399 |
if(pMB->cbp & (1 << (5 - i))) |
if(pMB->cbp & (1 << (5 - i))) |
400 |
CodeCoeff(bs, qcoeff[i], inter_table, scan_tables[0], 0); |
CodeCoeff(bs, &qcoeff[i*64], inter_table, scan_tables[0], 0); |
401 |
|
|
402 |
bits = BitstreamPos(bs) - bits; |
bits = BitstreamPos(bs) - bits; |
403 |
pStat->iTextBits += bits; |
pStat->iTextBits += bits; |
404 |
|
|
405 |
} |
} |
406 |
|
|
407 |
|
|
408 |
void MBCoding(const MBParam * pParam, MACROBLOCK *pMB, |
void MBCoding(const FRAMEINFO * frame, |
409 |
int16_t qcoeff[][64], |
MACROBLOCK *pMB, |
410 |
Bitstream * bs, Statistics * pStat) |
int16_t qcoeff[6*64], |
411 |
|
Bitstream * bs, |
412 |
|
Statistics * pStat) |
413 |
{ |
{ |
414 |
|
|
415 |
int intra = (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q); |
int intra = (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q); |
416 |
|
|
417 |
if(pParam->coding_type == P_VOP) { |
if(frame->coding_type == P_VOP) { |
418 |
if(pMB->cbp == 0 && pMB->mode == MODE_INTER && |
if(pMB->cbp == 0 && pMB->mode == MODE_INTER && |
419 |
pMB->mvs[0].x == 0 && pMB->mvs[0].y == 0) |
pMB->mvs[0].x == 0 && pMB->mvs[0].y == 0) |
420 |
{ |
{ |
426 |
} |
} |
427 |
|
|
428 |
if(intra) |
if(intra) |
429 |
CodeBlockIntra(pParam, pMB, qcoeff, bs, pStat); |
CodeBlockIntra(frame, pMB, qcoeff, bs, pStat); |
430 |
else |
else |
431 |
CodeBlockInter(pParam, pMB, qcoeff, bs, pStat); |
CodeBlockInter(frame, pMB, qcoeff, bs, pStat); |
432 |
|
|
433 |
|
} |
434 |
|
|
435 |
|
/*************************************************************** |
436 |
|
* bframe encoding start |
437 |
|
***************************************************************/ |
438 |
|
|
439 |
|
/* |
440 |
|
mbtype |
441 |
|
0 1b direct(h263) mvdb |
442 |
|
1 01b interpolate mc+q dbquant, mvdf, mvdb |
443 |
|
2 001b backward mc+q dbquant, mvdb |
444 |
|
3 0001b forward mc+q dbquant, mvdf |
445 |
|
*/ |
446 |
|
|
447 |
|
void put_bvop_mbtype(Bitstream * bs, int value) |
448 |
|
{ |
449 |
|
switch(value) |
450 |
|
{ |
451 |
|
case 0 : BitstreamPutBit(bs, 1); |
452 |
|
return; |
453 |
|
|
454 |
|
case 1 : BitstreamPutBit(bs, 0); |
455 |
|
BitstreamPutBit(bs, 1); |
456 |
|
return; |
457 |
|
|
458 |
|
case 2 : BitstreamPutBit(bs, 0); |
459 |
|
BitstreamPutBit(bs, 0); |
460 |
|
BitstreamPutBit(bs, 1); |
461 |
|
return; |
462 |
|
|
463 |
|
case 3 : BitstreamPutBit(bs, 0); |
464 |
|
BitstreamPutBit(bs, 0); |
465 |
|
BitstreamPutBit(bs, 0); |
466 |
|
BitstreamPutBit(bs, 1); |
467 |
|
return; |
468 |
|
|
469 |
|
default : ; // invalid! |
470 |
|
|
471 |
} |
} |
472 |
|
|
473 |
|
} |
474 |
|
|
475 |
|
/* |
476 |
|
dbquant |
477 |
|
-2 10b |
478 |
|
0 0b |
479 |
|
+2 11b |
480 |
|
*/ |
481 |
|
|
482 |
|
void put_bvop_dbquant(Bitstream *bs, int value) |
483 |
|
{ |
484 |
|
switch (value) |
485 |
|
{ |
486 |
|
case 0 : BitstreamPutBit(bs, 0); |
487 |
|
return; |
488 |
|
|
489 |
|
case -2 : BitstreamPutBit(bs, 1); |
490 |
|
BitstreamPutBit(bs, 0); |
491 |
|
return; |
492 |
|
|
493 |
|
case 2 : BitstreamPutBit(bs, 1); |
494 |
|
BitstreamPutBit(bs, 1); |
495 |
|
return; |
496 |
|
|
497 |
|
default : ; // invalid |
498 |
|
} |
499 |
|
} |
500 |
|
|
501 |
|
|
502 |
|
|
503 |
|
void MBCodingBVOP(const MACROBLOCK * mb, |
504 |
|
const int16_t qcoeff[6*64], |
505 |
|
const int32_t fcode, |
506 |
|
const int32_t bcode, |
507 |
|
Bitstream * bs, |
508 |
|
Statistics * pStat) |
509 |
|
{ |
510 |
|
int i; |
511 |
|
|
512 |
|
/* ------------------------------------------------------------------ |
513 |
|
when a block is skipped it is decoded DIRECT(0,) |
514 |
|
hence are interpolated from forward & backward frames |
515 |
|
------------------------------------------------------------------ */ |
516 |
|
|
517 |
|
if (mb->mode == 5) |
518 |
|
{ |
519 |
|
BitstreamPutBit(bs, 1); // skipped |
520 |
|
return; |
521 |
|
} |
522 |
|
|
523 |
|
BitstreamPutBit(bs, 0); // not skipped |
524 |
|
|
525 |
|
if (mb->cbp == 0) |
526 |
|
{ |
527 |
|
BitstreamPutBit(bs, 1); // cbp == 0 |
528 |
|
} |
529 |
|
else |
530 |
|
{ |
531 |
|
BitstreamPutBit(bs, 0); // cbp == xxx |
532 |
|
} |
533 |
|
|
534 |
|
put_bvop_mbtype(bs, mb->mode); |
535 |
|
|
536 |
|
if (mb->cbp) |
537 |
|
{ |
538 |
|
BitstreamPutBits(bs, mb->cbp, 6); |
539 |
|
} |
540 |
|
|
541 |
|
if (mb->mode != MODE_DIRECT && mb->cbp != 0) |
542 |
|
{ |
543 |
|
put_bvop_dbquant(bs, 0); // todo: mb->dquant = 0 |
544 |
|
} |
545 |
|
|
546 |
|
if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_FORWARD) |
547 |
|
{ |
548 |
|
CodeVector(bs, mb->pmvs[0].x, fcode, pStat); |
549 |
|
CodeVector(bs, mb->pmvs[0].y, fcode, pStat); |
550 |
|
} |
551 |
|
|
552 |
|
if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_BACKWARD) |
553 |
|
{ |
554 |
|
CodeVector(bs, mb->b_pmvs[0].x, bcode, pStat); |
555 |
|
CodeVector(bs, mb->b_pmvs[0].y, bcode, pStat); |
556 |
|
} |
557 |
|
|
558 |
|
if (mb->mode == MODE_DIRECT) |
559 |
|
{ |
560 |
|
// TODO: direct |
561 |
|
} |
562 |
|
|
563 |
|
for (i = 0; i < 6; i++) |
564 |
|
{ |
565 |
|
if (mb->cbp & (1 << (5 - i))) |
566 |
|
{ |
567 |
|
CodeCoeff(bs, &qcoeff[i*64], inter_table, scan_tables[0], 0); |
568 |
|
} |
569 |
|
} |
570 |
|
} |
571 |
|
|
572 |
|
|
573 |
|
|
574 |
/*************************************************************** |
/*************************************************************** |
575 |
* decoding stuff starts here * |
* decoding stuff starts here * |
577 |
|
|
578 |
int get_mcbpc_intra(Bitstream * bs) |
int get_mcbpc_intra(Bitstream * bs) |
579 |
{ |
{ |
580 |
|
|
581 |
uint32_t index; |
uint32_t index; |
582 |
|
|
583 |
while((index = BitstreamShowBits(bs, 9)) == 1) |
while((index = BitstreamShowBits(bs, 9)) == 1) |
586 |
index >>= 3; |
index >>= 3; |
587 |
|
|
588 |
BitstreamSkip(bs, mcbpc_intra_table[index].len); |
BitstreamSkip(bs, mcbpc_intra_table[index].len); |
589 |
|
|
590 |
return mcbpc_intra_table[index].code; |
return mcbpc_intra_table[index].code; |
591 |
|
|
592 |
} |
} |
593 |
|
|
594 |
int get_mcbpc_inter(Bitstream * bs) |
int get_mcbpc_inter(Bitstream * bs) |
595 |
{ |
{ |
596 |
|
|
597 |
uint32_t index; |
uint32_t index; |
598 |
|
|
599 |
while((index = CLIP(BitstreamShowBits(bs, 9), 256)) == 1) |
while((index = CLIP(BitstreamShowBits(bs, 9), 256)) == 1) |
600 |
BitstreamSkip(bs, 9); |
BitstreamSkip(bs, 9); |
601 |
|
|
602 |
BitstreamSkip(bs, mcbpc_inter_table[index].len); |
BitstreamSkip(bs, mcbpc_inter_table[index].len); |
603 |
|
|
604 |
return mcbpc_inter_table[index].code; |
return mcbpc_inter_table[index].code; |
605 |
|
|
606 |
} |
} |
607 |
|
|
608 |
int get_cbpy(Bitstream * bs, int intra) |
int get_cbpy(Bitstream * bs, int intra) |
609 |
{ |
{ |
610 |
|
|
611 |
int cbpy; |
int cbpy; |
612 |
uint32_t index = BitstreamShowBits(bs, 6); |
uint32_t index = BitstreamShowBits(bs, 6); |
613 |
|
|
618 |
cbpy = 15 - cbpy; |
cbpy = 15 - cbpy; |
619 |
|
|
620 |
return cbpy; |
return cbpy; |
621 |
|
|
622 |
} |
} |
623 |
|
|
624 |
int get_mv_data(Bitstream * bs) |
int get_mv_data(Bitstream * bs) |
625 |
{ |
{ |
626 |
|
|
627 |
uint32_t index; |
uint32_t index; |
628 |
|
|
629 |
if(BitstreamGetBit(bs)) |
if(BitstreamGetBit(bs)) |
649 |
|
|
650 |
BitstreamSkip(bs, TMNMVtab2[index].len); |
BitstreamSkip(bs, TMNMVtab2[index].len); |
651 |
return TMNMVtab2[index].code; |
return TMNMVtab2[index].code; |
652 |
|
|
653 |
} |
} |
654 |
|
|
655 |
int get_mv(Bitstream * bs, int fcode) |
int get_mv(Bitstream * bs, int fcode) |
656 |
{ |
{ |
657 |
|
|
658 |
int data; |
int data; |
659 |
int res; |
int res; |
660 |
int mv; |
int mv; |
669 |
mv = ((ABS(data) - 1) * scale_fac) + res + 1; |
mv = ((ABS(data) - 1) * scale_fac) + res + 1; |
670 |
|
|
671 |
return data < 0 ? -mv : mv; |
return data < 0 ? -mv : mv; |
672 |
|
|
673 |
} |
} |
674 |
|
|
675 |
int get_dc_dif(Bitstream * bs, uint32_t dc_size) |
int get_dc_dif(Bitstream * bs, uint32_t dc_size) |
676 |
{ |
{ |
677 |
|
|
678 |
int code = BitstreamGetBits(bs, dc_size); |
int code = BitstreamGetBits(bs, dc_size); |
679 |
int msb = code >> (dc_size - 1); |
int msb = code >> (dc_size - 1); |
680 |
|
|
682 |
return (-1 * (code^((1 << dc_size) - 1))); |
return (-1 * (code^((1 << dc_size) - 1))); |
683 |
|
|
684 |
return code; |
return code; |
685 |
|
|
686 |
} |
} |
687 |
|
|
688 |
int get_dc_size_lum(Bitstream * bs) |
int get_dc_size_lum(Bitstream * bs) |
689 |
{ |
{ |
690 |
|
|
691 |
int code, i; |
int code, i; |
692 |
code = BitstreamShowBits(bs, 11); |
code = BitstreamShowBits(bs, 11); |
693 |
|
|
701 |
|
|
702 |
BitstreamSkip(bs, dc_lum_tab[code].len); |
BitstreamSkip(bs, dc_lum_tab[code].len); |
703 |
return dc_lum_tab[code].code; |
return dc_lum_tab[code].code; |
704 |
|
|
705 |
} |
} |
706 |
|
|
707 |
|
|
708 |
int get_dc_size_chrom(Bitstream * bs) |
int get_dc_size_chrom(Bitstream * bs) |
709 |
{ |
{ |
710 |
|
|
711 |
uint32_t code, i; |
uint32_t code, i; |
712 |
code = BitstreamShowBits(bs, 12); |
code = BitstreamShowBits(bs, 12); |
713 |
|
|
720 |
} |
} |
721 |
|
|
722 |
return 3 - BitstreamGetBits(bs, 2); |
return 3 - BitstreamGetBits(bs, 2); |
|
} |
|
|
|
|
|
int get_coeff(Bitstream * bs, int *run, int *last, int intra, int short_video_header) |
|
|
{ |
|
|
uint32_t mode; |
|
|
const VLC *tab; |
|
|
int32_t level; |
|
|
|
|
|
if(short_video_header) // inter-VLCs will be used for both intra and inter blocks |
|
|
intra = 0; |
|
|
|
|
|
tab = &DCT3D[intra][BitstreamShowBits(bs, 12)]; |
|
723 |
|
|
|
if(tab->code == -1) |
|
|
goto error; |
|
|
|
|
|
BitstreamSkip(bs, tab->len); |
|
|
|
|
|
if(tab->code != ESCAPE) { |
|
|
if(!intra) |
|
|
{ |
|
|
*run = (tab->code >> 4) & 255; |
|
|
level = tab->code & 15; |
|
|
*last = (tab->code >> 12) & 1; |
|
|
} |
|
|
else |
|
|
{ |
|
|
*run = (tab->code >> 8) & 255; |
|
|
level = tab->code & 255; |
|
|
*last = (tab->code >> 16) & 1; |
|
|
} |
|
|
return BitstreamGetBit(bs) ? -level : level; |
|
724 |
} |
} |
725 |
|
|
|
if(short_video_header) |
|
|
{ |
|
|
// escape mode 4 - H.263 type, only used if short_video_header = 1 |
|
|
*last = BitstreamGetBit(bs); |
|
|
*run = BitstreamGetBits(bs, 6); |
|
|
level = BitstreamGetBits(bs, 8); |
|
|
|
|
|
if (level == 0 || level == 128) |
|
|
DEBUG1("Illegal LEVEL for ESCAPE mode 4:", level); |
|
|
|
|
|
return (level >= 128 ? -(256 - level) : level); |
|
|
} |
|
|
|
|
|
mode = BitstreamShowBits(bs, 2); |
|
|
|
|
|
if(mode < 3) { |
|
|
BitstreamSkip(bs, (mode == 2) ? 2 : 1); |
|
|
|
|
|
tab = &DCT3D[intra][BitstreamShowBits(bs, 12)]; |
|
|
if (tab->code == -1) |
|
|
goto error; |
|
|
|
|
|
BitstreamSkip(bs, tab->len); |
|
|
|
|
|
if (!intra) { |
|
|
*run = (tab->code >> 4) & 255; |
|
|
level = tab->code & 15; |
|
|
*last = (tab->code >> 12) & 1; |
|
|
} |
|
|
else |
|
|
{ |
|
|
*run = (tab->code >> 8) & 255; |
|
|
level = tab->code & 255; |
|
|
*last = (tab->code >> 16) & 1; |
|
|
} |
|
|
|
|
|
if(mode < 2) // first escape mode, level is offset |
|
|
level += max_level[*last + (!intra<<1)][*run]; // need to add back the max level |
|
|
else if(mode == 2) // second escape mode, run is offset |
|
|
*run += max_run[*last + (!intra<<1)][level] + 1; |
|
|
|
|
|
return BitstreamGetBit(bs) ? -level : level; |
|
|
} |
|
|
|
|
|
// third escape mode - fixed length codes |
|
|
BitstreamSkip(bs, 2); |
|
|
*last = BitstreamGetBits(bs, 1); |
|
|
*run = BitstreamGetBits(bs, 6); |
|
|
BitstreamSkip(bs, 1); // marker |
|
|
level = BitstreamGetBits(bs, 12); |
|
|
BitstreamSkip(bs, 1); // marker |
|
|
|
|
|
return (level & 0x800) ? (level | (-1 ^ 0xfff)) : level; |
|
|
|
|
|
error: |
|
|
*run = VLC_ERROR; |
|
|
return 0; |
|
|
} |
|
|
|
|
|
|
|
726 |
void get_intra_block(Bitstream * bs, int16_t * block, int direction, int coeff) |
void get_intra_block(Bitstream * bs, int16_t * block, int direction, int coeff) |
727 |
{ |
{ |
728 |
|
|
729 |
const uint16_t * scan = scan_tables[ direction ]; |
const uint16_t * scan = scan_tables[ direction ]; |
730 |
int level; |
int level; |
731 |
int run; |
int run; |
747 |
} |
} |
748 |
coeff++; |
coeff++; |
749 |
} while (!last); |
} while (!last); |
750 |
|
|
751 |
} |
} |
752 |
|
|
753 |
void get_inter_block(Bitstream * bs, int16_t * block) |
void get_inter_block(Bitstream * bs, int16_t * block) |
754 |
{ |
{ |
755 |
|
|
756 |
const uint16_t * scan = scan_tables[0]; |
const uint16_t * scan = scan_tables[0]; |
757 |
int p; |
int p; |
758 |
int level; |
int level; |
769 |
break; |
break; |
770 |
} |
} |
771 |
p += run; |
p += run; |
772 |
|
|
773 |
block[ scan[p] ] = level; |
block[ scan[p] ] = level; |
774 |
if (level < -127 || level > 127) |
if (level < -127 || level > 127) |
775 |
{ |
{ |
777 |
} |
} |
778 |
p++; |
p++; |
779 |
} while (!last); |
} while (!last); |
780 |
|
|
781 |
} |
} |