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> |
#include <stdlib.h> |
52 |
#include "../portab.h" |
#include "../portab.h" |
53 |
#include "bitstream.h" |
#include "bitstream.h" |
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 |
VLC intra_table[65536]; |
VLC intra_table[524032]; |
64 |
VLC inter_table[65536]; |
VLC inter_table[524032]; |
65 |
|
|
66 |
VLC DCT3Dintra[4096]; |
VLC DCT3Dintra[4096]; |
67 |
VLC DCT3Dinter[4096]; |
VLC DCT3Dinter[4096]; |
68 |
|
|
69 |
static int16_t clip_table[4096]; |
void |
70 |
|
init_vlc_tables(void) |
|
void init_vlc_tables(void) |
|
71 |
{ |
{ |
72 |
|
|
73 |
int32_t k, l, i, intra, last; |
int32_t k, l, i, intra, last; |
81 |
vlc[0] = intra_table; |
vlc[0] = intra_table; |
82 |
vlc[1] = inter_table; |
vlc[1] = inter_table; |
83 |
|
|
|
// initialize the clipping table |
|
|
for(i = -2048; i < 2048; i++) { |
|
|
clip_table[i + 2048] = i; |
|
|
if(i < -255) |
|
|
clip_table[i + 2048] = -255; |
|
|
if(i > 255) |
|
|
clip_table[i + 2048] = 255; |
|
|
} |
|
|
|
|
84 |
// generate encoding vlc lookup tables |
// generate encoding vlc lookup tables |
85 |
|
// the lookup table idea is taken from the excellent fame project by Vivien Chapellier |
86 |
for(i = 0; i < 4; i++) { |
for(i = 0; i < 4; i++) { |
87 |
intra = i % 2; |
intra = i % 2; |
88 |
last = i / 2; |
last = i / 2; |
89 |
|
|
90 |
coeff_ptr = coeff_vlc[last + 2 * intra]; |
coeff_ptr = coeff_vlc[last + 2 * intra]; |
91 |
|
|
92 |
for(k = -255; k < 256; k++) { // level |
for (k = -2047; k < 2048; k++) { // level |
93 |
int8_t *max_level_ptr = max_level[last + 2 * intra]; |
int8_t *max_level_ptr = max_level[last + 2 * intra]; |
94 |
int8_t *max_run_ptr = max_run[last + 2 * intra]; |
int8_t *max_run_ptr = max_run[last + 2 * intra]; |
95 |
|
|
96 |
for(l = 0; l < 64; l++) { // run |
for(l = 0; l < 64; l++) { // run |
97 |
int32_t level = k; |
int32_t level = k; |
98 |
uint32_t run = l; |
ptr_t run = l; |
99 |
|
|
100 |
if((abs(level) <= max_level_ptr[run]) && |
if ((abs(level) <= max_level_ptr[run]) && (run <= (uint32_t) max_run_ptr[abs(level)])) { // level < max_level and run < max_run |
|
(run <= max_run_ptr[abs(level)])) { // level < max_level and run < max_run |
|
101 |
|
|
102 |
vlc[intra]->code = 0; |
vlc[intra]->code = 0; |
103 |
vlc[intra]->len = 0; |
vlc[intra]->len = 0; |
104 |
goto loop_end; |
goto loop_end; |
105 |
} |
} else { |
|
else { |
|
106 |
if(level > 0) // correct level |
if(level > 0) // correct level |
107 |
level -= max_level_ptr[run]; |
level -= max_level_ptr[run]; |
108 |
else |
else |
109 |
level += max_level_ptr[run]; |
level += max_level_ptr[run]; |
110 |
|
|
111 |
if((abs(level) <= max_level_ptr[run]) && |
if((abs(level) <= max_level_ptr[run]) && |
112 |
(run <= max_run_ptr[abs(level)])) { |
(run <= (uint32_t) max_run_ptr[abs(level)])) { |
113 |
|
|
114 |
vlc[intra]->code = 0x06; |
vlc[intra]->code = 0x06; |
115 |
vlc[intra]->len = 8; |
vlc[intra]->len = 8; |
124 |
run -= max_run_ptr[abs(level)] + 1; // and change run |
run -= max_run_ptr[abs(level)] + 1; // and change run |
125 |
|
|
126 |
if((abs(level) <= max_level_ptr[run]) && |
if((abs(level) <= max_level_ptr[run]) && |
127 |
(run <= max_run_ptr[abs(level)])) { |
(run <= (uint32_t) max_run_ptr[abs(level)])) { |
128 |
|
|
129 |
vlc[intra]->code = 0x0e; |
vlc[intra]->code = 0x0e; |
130 |
vlc[intra]->len = 9; |
vlc[intra]->len = 9; |
133 |
run += max_run_ptr[abs(level)] + 1; |
run += max_run_ptr[abs(level)] + 1; |
134 |
} |
} |
135 |
|
|
136 |
vlc[intra]->code = (uint32_t) ((l << 14) | (0x1e + last) << 20) | |
vlc[intra]->code = |
137 |
(1 << 13) | ((k & 0xfff) << 1) | 1; |
(uint32_t) ((l << 14) | (0x1e + last) << 20) | (1 << 13) | |
138 |
|
((k & 0xfff) << 1) | 1; |
139 |
|
|
140 |
vlc[intra]->len = 30; |
vlc[intra]->len = 30; |
141 |
vlc[intra]++; |
vlc[intra]++; |
143 |
|
|
144 |
loop_end: |
loop_end: |
145 |
if(level != 0) { |
if(level != 0) { |
146 |
vlc[intra]->code = (vlc[intra]->code << (coeff_ptr[run][abs(level) - 1].len + 1)) | |
vlc[intra]->code = |
147 |
(coeff_ptr[run][abs(level) - 1].code << 1); |
(vlc[intra]-> |
148 |
vlc[intra]->len = (coeff_ptr[run][abs(level) - 1].len + 1) + vlc[intra]->len; |
code << (coeff_ptr[run][abs(level) - 1].len + |
149 |
|
1)) | (coeff_ptr[run][abs(level) - |
150 |
|
1].code << 1); |
151 |
|
vlc[intra]->len = |
152 |
|
(coeff_ptr[run][abs(level) - 1].len + 1) + |
153 |
|
vlc[intra]->len; |
154 |
|
|
155 |
if(level < 0) |
if(level < 0) |
156 |
vlc[intra]->code += 1; |
vlc[intra]->code += 1; |
165 |
if(i >= 512) { |
if(i >= 512) { |
166 |
*vlc1 = DCT3Dtab3[(i >> 5) - 16]; |
*vlc1 = DCT3Dtab3[(i >> 5) - 16]; |
167 |
*vlc2 = DCT3Dtab0[(i >> 5) - 16]; |
*vlc2 = DCT3Dtab0[(i >> 5) - 16]; |
168 |
} |
} else if (i >= 128) { |
|
else if(i >= 128) { |
|
169 |
*vlc1 = DCT3Dtab4[(i >> 2) - 32]; |
*vlc1 = DCT3Dtab4[(i >> 2) - 32]; |
170 |
*vlc2 = DCT3Dtab1[(i >> 2) - 32]; |
*vlc2 = DCT3Dtab1[(i >> 2) - 32]; |
171 |
} |
} else if (i >= 8) { |
|
else if(i >= 8) { |
|
172 |
*vlc1 = DCT3Dtab5[i - 8]; |
*vlc1 = DCT3Dtab5[i - 8]; |
173 |
*vlc2 = DCT3Dtab2[i - 8]; |
*vlc2 = DCT3Dtab2[i - 8]; |
174 |
} |
} else { |
|
else { |
|
175 |
*vlc1 = ERRtab[i]; |
*vlc1 = ERRtab[i]; |
176 |
*vlc2 = ERRtab[i]; |
*vlc2 = ERRtab[i]; |
177 |
} |
} |
184 |
|
|
185 |
} |
} |
186 |
|
|
187 |
static __inline void CodeVector(Bitstream *bs, |
static __inline void |
188 |
int16_t value, |
CodeVector(Bitstream * bs, |
189 |
int16_t f_code, |
int32_t value, |
190 |
|
int32_t f_code, |
191 |
Statistics *pStat) |
Statistics *pStat) |
192 |
{ |
{ |
193 |
|
|
204 |
pStat->iMvCount++; |
pStat->iMvCount++; |
205 |
|
|
206 |
if (value == 0) { |
if (value == 0) { |
207 |
BitstreamPutBits(bs, mb_motion_table[32].code, mb_motion_table[32].len); |
BitstreamPutBits(bs, mb_motion_table[32].code, |
208 |
|
mb_motion_table[32].len); |
209 |
} else { |
} else { |
210 |
uint16_t length, code, mv_res, sign; |
uint16_t length, code, mv_res, sign; |
211 |
|
|
230 |
code = -code; |
code = -code; |
231 |
|
|
232 |
code += 32; |
code += 32; |
233 |
BitstreamPutBits(bs, mb_motion_table[code].code, mb_motion_table[code].len); |
BitstreamPutBits(bs, mb_motion_table[code].code, |
234 |
|
mb_motion_table[code].len); |
235 |
|
|
236 |
if(f_code) |
if(f_code) |
237 |
BitstreamPutBits(bs, mv_res, f_code); |
BitstreamPutBits(bs, mv_res, f_code); |
240 |
} |
} |
241 |
|
|
242 |
|
|
243 |
static __inline void CodeCoeff(Bitstream *bs, |
static __inline void |
244 |
int16_t qcoeff[64], |
CodeCoeff(Bitstream * bs, |
245 |
|
const int16_t qcoeff[64], |
246 |
VLC *table, |
VLC *table, |
247 |
const uint16_t *zigzag, |
const uint16_t *zigzag, |
248 |
uint16_t intra) |
uint16_t intra) |
253 |
VLC *vlc; |
VLC *vlc; |
254 |
|
|
255 |
j = intra; |
j = intra; |
256 |
last = 1 + intra; |
last = intra; |
257 |
|
|
258 |
while((v = qcoeff[zigzag[j++]]) == 0); |
while (j < 64 && (v = qcoeff[zigzag[j]]) == 0) |
259 |
|
j++; |
260 |
|
|
261 |
do { |
do { |
262 |
|
vlc = table + 64 * 2047 + (v << 6) + j - last; |
263 |
|
last = ++j; |
264 |
|
|
265 |
// count zeroes |
// count zeroes |
266 |
vlc = table + 64*255 + (clip_table[2048+v] << 6) + j - last; |
while (j < 64 && (v = qcoeff[zigzag[j]]) == 0) |
267 |
last = j + 1; |
j++; |
|
while(j < 64 && (v = qcoeff[zigzag[j++]]) == 0); |
|
268 |
|
|
269 |
// write code |
// write code |
270 |
if(j != 64) { |
if(j != 64) { |
271 |
BitstreamPutBits(bs, vlc->code, vlc->len); |
BitstreamPutBits(bs, vlc->code, vlc->len); |
272 |
} else { |
} else { |
273 |
vlc += 64*511; |
vlc += 64 * 4095; |
274 |
BitstreamPutBits(bs, vlc->code, vlc->len); |
BitstreamPutBits(bs, vlc->code, vlc->len); |
275 |
break; |
break; |
276 |
} |
} |
279 |
} |
} |
280 |
|
|
281 |
|
|
282 |
static void CodeBlockIntra(const MBParam * pParam, |
static void |
283 |
|
CodeBlockIntra(const FRAMEINFO * frame, |
284 |
const MACROBLOCK *pMB, |
const MACROBLOCK *pMB, |
285 |
int16_t qcoeff[6*64], |
int16_t qcoeff[6*64], |
286 |
Bitstream * bs, |
Bitstream * bs, |
292 |
cbpy = pMB->cbp >> 2; |
cbpy = pMB->cbp >> 2; |
293 |
|
|
294 |
// write mcbpc |
// write mcbpc |
295 |
if(pParam->coding_type == I_VOP) { |
if (frame->coding_type == I_VOP) { |
296 |
mcbpc = ((pMB->mode >> 1) & 3) | ((pMB->cbp & 3) << 2); |
mcbpc = ((pMB->mode >> 1) & 3) | ((pMB->cbp & 3) << 2); |
297 |
BitstreamPutBits(bs, mcbpc_intra_tab[mcbpc].code, mcbpc_intra_tab[mcbpc].len); |
BitstreamPutBits(bs, mcbpc_intra_tab[mcbpc].code, |
298 |
} |
mcbpc_intra_tab[mcbpc].len); |
299 |
else { |
} else { |
300 |
mcbpc = (pMB->mode & 7) | ((pMB->cbp & 3) << 3); |
mcbpc = (pMB->mode & 7) | ((pMB->cbp & 3) << 3); |
301 |
BitstreamPutBits(bs, mcbpc_inter_tab[mcbpc].code, mcbpc_inter_tab[mcbpc].len); |
BitstreamPutBits(bs, mcbpc_inter_tab[mcbpc].code, |
302 |
|
mcbpc_inter_tab[mcbpc].len); |
303 |
} |
} |
304 |
|
|
305 |
// ac prediction flag |
// ac prediction flag |
316 |
BitstreamPutBits(bs, pMB->dquant, 2); |
BitstreamPutBits(bs, pMB->dquant, 2); |
317 |
|
|
318 |
// write interlacing |
// write interlacing |
319 |
if (pParam->global_flags & XVID_INTERLACING) |
if (frame->global_flags & XVID_INTERLACING) { |
|
{ |
|
320 |
BitstreamPutBit(bs, pMB->field_dct); |
BitstreamPutBit(bs, pMB->field_dct); |
321 |
} |
} |
|
|
|
322 |
// code block coeffs |
// code block coeffs |
323 |
for(i = 0; i < 6; i++) |
for (i = 0; i < 6; i++) { |
|
{ |
|
324 |
if(i < 4) |
if(i < 4) |
325 |
BitstreamPutBits(bs, |
BitstreamPutBits(bs, dcy_tab[qcoeff[i * 64 + 0] + 255].code, |
|
dcy_tab[qcoeff[i*64 + 0] + 255].code, |
|
326 |
dcy_tab[qcoeff[i*64 + 0] + 255].len); |
dcy_tab[qcoeff[i*64 + 0] + 255].len); |
327 |
else |
else |
328 |
BitstreamPutBits(bs, |
BitstreamPutBits(bs, dcc_tab[qcoeff[i * 64 + 0] + 255].code, |
|
dcc_tab[qcoeff[i*64 + 0] + 255].code, |
|
329 |
dcc_tab[qcoeff[i*64 + 0] + 255].len); |
dcc_tab[qcoeff[i*64 + 0] + 255].len); |
330 |
|
|
331 |
if(pMB->cbp & (1 << (5 - i))) |
if (pMB->cbp & (1 << (5 - i))) { |
|
{ |
|
332 |
bits = BitstreamPos(bs); |
bits = BitstreamPos(bs); |
333 |
|
|
334 |
CodeCoeff(bs, |
CodeCoeff(bs, &qcoeff[i * 64], intra_table, |
335 |
&qcoeff[i*64], |
scan_tables[pMB->acpred_directions[i]], 1); |
|
intra_table, |
|
|
scan_tables[pMB->acpred_directions[i]], |
|
|
1); |
|
336 |
|
|
337 |
bits = BitstreamPos(bs) - bits; |
bits = BitstreamPos(bs) - bits; |
338 |
pStat->iTextBits += bits; |
pStat->iTextBits += bits; |
342 |
} |
} |
343 |
|
|
344 |
|
|
345 |
static void CodeBlockInter(const MBParam * pParam, |
static void |
346 |
|
CodeBlockInter(const FRAMEINFO * frame, |
347 |
const MACROBLOCK *pMB, |
const MACROBLOCK *pMB, |
348 |
int16_t qcoeff[6*64], |
int16_t qcoeff[6*64], |
349 |
Bitstream * bs, |
Bitstream * bs, |
357 |
cbpy = 15 - (pMB->cbp >> 2); |
cbpy = 15 - (pMB->cbp >> 2); |
358 |
|
|
359 |
// write mcbpc |
// write mcbpc |
360 |
BitstreamPutBits(bs, mcbpc_inter_tab[mcbpc].code, mcbpc_inter_tab[mcbpc].len); |
BitstreamPutBits(bs, mcbpc_inter_tab[mcbpc].code, |
361 |
|
mcbpc_inter_tab[mcbpc].len); |
362 |
|
|
363 |
// write cbpy |
// write cbpy |
364 |
BitstreamPutBits(bs, cbpy_tab[cbpy].code, cbpy_tab[cbpy].len); |
BitstreamPutBits(bs, cbpy_tab[cbpy].code, cbpy_tab[cbpy].len); |
368 |
BitstreamPutBits(bs, pMB->dquant, 2); |
BitstreamPutBits(bs, pMB->dquant, 2); |
369 |
|
|
370 |
// interlacing |
// interlacing |
371 |
if (pParam->global_flags & XVID_INTERLACING) |
if (frame->global_flags & XVID_INTERLACING) { |
|
{ |
|
372 |
BitstreamPutBit(bs, pMB->field_dct); |
BitstreamPutBit(bs, pMB->field_dct); |
373 |
DEBUG1("codep: field_dct: ", pMB->field_dct); |
DEBUG1("codep: field_dct: ", pMB->field_dct); |
374 |
|
|
375 |
// if inter block, write field ME flag |
// if inter block, write field ME flag |
376 |
if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) |
if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) { |
|
{ |
|
377 |
BitstreamPutBit(bs, pMB->field_pred); |
BitstreamPutBit(bs, pMB->field_pred); |
378 |
DEBUG1("codep: field_pred: ", pMB->field_pred); |
DEBUG1("codep: field_pred: ", pMB->field_pred); |
379 |
|
|
380 |
// write field prediction references |
// write field prediction references |
381 |
if (pMB->field_pred) |
if (pMB->field_pred) { |
|
{ |
|
382 |
BitstreamPutBit(bs, pMB->field_for_top); |
BitstreamPutBit(bs, pMB->field_for_top); |
383 |
BitstreamPutBit(bs, pMB->field_for_bot); |
BitstreamPutBit(bs, pMB->field_for_bot); |
384 |
} |
} |
385 |
} |
} |
386 |
} |
} |
|
|
|
387 |
// code motion vector(s) |
// code motion vector(s) |
388 |
for(i = 0; i < (pMB->mode == MODE_INTER4V ? 4 : 1); i++) |
for (i = 0; i < (pMB->mode == MODE_INTER4V ? 4 : 1); i++) { |
389 |
{ |
CodeVector(bs, pMB->pmvs[i].x, frame->fcode, pStat); |
390 |
CodeVector(bs, pMB->pmvs[i].x, pParam->fixed_code, pStat); |
CodeVector(bs, pMB->pmvs[i].y, frame->fcode, pStat); |
|
CodeVector(bs, pMB->pmvs[i].y, pParam->fixed_code, pStat); |
|
391 |
} |
} |
392 |
|
|
393 |
bits = BitstreamPos(bs); |
bits = BitstreamPos(bs); |
403 |
} |
} |
404 |
|
|
405 |
|
|
406 |
void MBCoding(const MBParam * pParam, |
void |
407 |
|
MBCoding(const FRAMEINFO * frame, |
408 |
MACROBLOCK *pMB, |
MACROBLOCK *pMB, |
409 |
int16_t qcoeff[6*64], |
int16_t qcoeff[6*64], |
410 |
Bitstream * bs, |
Bitstream * bs, |
413 |
|
|
414 |
int intra = (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q); |
int intra = (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q); |
415 |
|
|
416 |
if(pParam->coding_type == P_VOP) { |
if (frame->coding_type == P_VOP) { |
417 |
if(pMB->cbp == 0 && pMB->mode == MODE_INTER && |
if (pMB->cbp == 0 && pMB->mode == MODE_INTER && pMB->mvs[0].x == 0 && |
418 |
pMB->mvs[0].x == 0 && pMB->mvs[0].y == 0) |
pMB->mvs[0].y == 0) { |
|
{ |
|
419 |
BitstreamPutBit(bs, 1); // not_coded |
BitstreamPutBit(bs, 1); // not_coded |
420 |
return; |
return; |
421 |
} |
} else |
|
else |
|
422 |
BitstreamPutBit(bs, 0); // coded |
BitstreamPutBit(bs, 0); // coded |
423 |
} |
} |
424 |
|
|
425 |
if(intra) |
if(intra) |
426 |
CodeBlockIntra(pParam, pMB, qcoeff, bs, pStat); |
CodeBlockIntra(frame, pMB, qcoeff, bs, pStat); |
427 |
else |
else |
428 |
CodeBlockInter(pParam, pMB, qcoeff, bs, pStat); |
CodeBlockInter(frame, pMB, qcoeff, bs, pStat); |
429 |
|
|
430 |
} |
} |
431 |
|
|
432 |
|
/*************************************************************** |
433 |
|
* bframe encoding start |
434 |
|
***************************************************************/ |
435 |
|
|
436 |
|
/* |
437 |
|
mbtype |
438 |
|
0 1b direct(h263) mvdb |
439 |
|
1 01b interpolate mc+q dbquant, mvdf, mvdb |
440 |
|
2 001b backward mc+q dbquant, mvdb |
441 |
|
3 0001b forward mc+q dbquant, mvdf |
442 |
|
*/ |
443 |
|
|
444 |
|
void |
445 |
|
put_bvop_mbtype(Bitstream * bs, |
446 |
|
int value) |
447 |
|
{ |
448 |
|
switch (value) { |
449 |
|
case 0: |
450 |
|
BitstreamPutBit(bs, 1); |
451 |
|
return; |
452 |
|
|
453 |
|
case 1: |
454 |
|
BitstreamPutBit(bs, 0); |
455 |
|
BitstreamPutBit(bs, 1); |
456 |
|
return; |
457 |
|
|
458 |
|
case 2: |
459 |
|
BitstreamPutBit(bs, 0); |
460 |
|
BitstreamPutBit(bs, 0); |
461 |
|
BitstreamPutBit(bs, 1); |
462 |
|
return; |
463 |
|
|
464 |
|
case 3: |
465 |
|
BitstreamPutBit(bs, 0); |
466 |
|
BitstreamPutBit(bs, 0); |
467 |
|
BitstreamPutBit(bs, 0); |
468 |
|
BitstreamPutBit(bs, 1); |
469 |
|
return; |
470 |
|
|
471 |
|
default:; // invalid! |
472 |
|
|
473 |
|
} |
474 |
|
|
475 |
|
} |
476 |
|
|
477 |
|
/* |
478 |
|
dbquant |
479 |
|
-2 10b |
480 |
|
0 0b |
481 |
|
+2 11b |
482 |
|
*/ |
483 |
|
|
484 |
|
void |
485 |
|
put_bvop_dbquant(Bitstream * bs, |
486 |
|
int value) |
487 |
|
{ |
488 |
|
switch (value) { |
489 |
|
case 0: |
490 |
|
BitstreamPutBit(bs, 0); |
491 |
|
return; |
492 |
|
|
493 |
|
case -2: |
494 |
|
BitstreamPutBit(bs, 1); |
495 |
|
BitstreamPutBit(bs, 0); |
496 |
|
return; |
497 |
|
|
498 |
|
case 2: |
499 |
|
BitstreamPutBit(bs, 1); |
500 |
|
BitstreamPutBit(bs, 1); |
501 |
|
return; |
502 |
|
|
503 |
|
default:; // invalid |
504 |
|
} |
505 |
|
} |
506 |
|
|
507 |
|
|
508 |
|
|
509 |
|
void |
510 |
|
MBCodingBVOP(const MACROBLOCK * mb, |
511 |
|
const int16_t qcoeff[6 * 64], |
512 |
|
const int32_t fcode, |
513 |
|
const int32_t bcode, |
514 |
|
Bitstream * bs, |
515 |
|
Statistics * pStat) |
516 |
|
{ |
517 |
|
int i; |
518 |
|
|
519 |
|
/* ------------------------------------------------------------------ |
520 |
|
when a block is skipped it is decoded DIRECT(0,) |
521 |
|
hence are interpolated from forward & backward frames |
522 |
|
------------------------------------------------------------------ */ |
523 |
|
|
524 |
|
if (mb->mode == 5) { |
525 |
|
BitstreamPutBit(bs, 1); // skipped |
526 |
|
return; |
527 |
|
} |
528 |
|
|
529 |
|
BitstreamPutBit(bs, 0); // not skipped |
530 |
|
|
531 |
|
if (mb->cbp == 0) { |
532 |
|
BitstreamPutBit(bs, 1); // cbp == 0 |
533 |
|
} else { |
534 |
|
BitstreamPutBit(bs, 0); // cbp == xxx |
535 |
|
} |
536 |
|
|
537 |
|
put_bvop_mbtype(bs, mb->mode); |
538 |
|
|
539 |
|
if (mb->cbp) { |
540 |
|
BitstreamPutBits(bs, mb->cbp, 6); |
541 |
|
} |
542 |
|
|
543 |
|
if (mb->mode != MODE_DIRECT && mb->cbp != 0) { |
544 |
|
put_bvop_dbquant(bs, 0); // todo: mb->dquant = 0 |
545 |
|
} |
546 |
|
|
547 |
|
if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_FORWARD) { |
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 |
|
CodeVector(bs, mb->b_pmvs[0].x, bcode, pStat); |
554 |
|
CodeVector(bs, mb->b_pmvs[0].y, bcode, pStat); |
555 |
|
} |
556 |
|
|
557 |
|
if (mb->mode == MODE_DIRECT) { |
558 |
|
// TODO: direct |
559 |
|
} |
560 |
|
|
561 |
|
for (i = 0; i < 6; i++) { |
562 |
|
if (mb->cbp & (1 << (5 - i))) { |
563 |
|
CodeCoeff(bs, &qcoeff[i * 64], inter_table, scan_tables[0], 0); |
564 |
|
} |
565 |
|
} |
566 |
|
} |
567 |
|
|
568 |
|
|
569 |
|
|
570 |
/*************************************************************** |
/*************************************************************** |
571 |
* decoding stuff starts here * |
* decoding stuff starts here * |
572 |
***************************************************************/ |
***************************************************************/ |
573 |
|
|
574 |
int get_mcbpc_intra(Bitstream * bs) |
int |
575 |
|
get_mcbpc_intra(Bitstream * bs) |
576 |
{ |
{ |
577 |
|
|
578 |
uint32_t index; |
uint32_t index; |
588 |
|
|
589 |
} |
} |
590 |
|
|
591 |
int get_mcbpc_inter(Bitstream * bs) |
int |
592 |
|
get_mcbpc_inter(Bitstream * bs) |
593 |
{ |
{ |
594 |
|
|
595 |
uint32_t index; |
uint32_t index; |
603 |
|
|
604 |
} |
} |
605 |
|
|
606 |
int get_cbpy(Bitstream * bs, int intra) |
int |
607 |
|
get_cbpy(Bitstream * bs, |
608 |
|
int intra) |
609 |
{ |
{ |
610 |
|
|
611 |
int cbpy; |
int cbpy; |
621 |
|
|
622 |
} |
} |
623 |
|
|
624 |
int get_mv_data(Bitstream * bs) |
int |
625 |
|
get_mv_data(Bitstream * bs) |
626 |
{ |
{ |
627 |
|
|
628 |
uint32_t index; |
uint32_t index; |
632 |
|
|
633 |
index = BitstreamShowBits(bs, 12); |
index = BitstreamShowBits(bs, 12); |
634 |
|
|
635 |
if(index >= 512) |
if (index >= 512) { |
|
{ |
|
636 |
index = (index >> 8) - 2; |
index = (index >> 8) - 2; |
637 |
BitstreamSkip(bs, TMNMVtab0[index].len); |
BitstreamSkip(bs, TMNMVtab0[index].len); |
638 |
return TMNMVtab0[index].code; |
return TMNMVtab0[index].code; |
639 |
} |
} |
640 |
|
|
641 |
if(index >= 128) |
if (index >= 128) { |
|
{ |
|
642 |
index = (index >> 2) - 32; |
index = (index >> 2) - 32; |
643 |
BitstreamSkip(bs, TMNMVtab1[index].len); |
BitstreamSkip(bs, TMNMVtab1[index].len); |
644 |
return TMNMVtab1[index].code; |
return TMNMVtab1[index].code; |
651 |
|
|
652 |
} |
} |
653 |
|
|
654 |
int get_mv(Bitstream * bs, int fcode) |
int |
655 |
|
get_mv(Bitstream * bs, |
656 |
|
int fcode) |
657 |
{ |
{ |
658 |
|
|
659 |
int data; |
int data; |
673 |
|
|
674 |
} |
} |
675 |
|
|
676 |
int get_dc_dif(Bitstream * bs, uint32_t dc_size) |
int |
677 |
|
get_dc_dif(Bitstream * bs, |
678 |
|
uint32_t dc_size) |
679 |
{ |
{ |
680 |
|
|
681 |
int code = BitstreamGetBits(bs, dc_size); |
int code = BitstreamGetBits(bs, dc_size); |
688 |
|
|
689 |
} |
} |
690 |
|
|
691 |
int get_dc_size_lum(Bitstream * bs) |
int |
692 |
|
get_dc_size_lum(Bitstream * bs) |
693 |
{ |
{ |
694 |
|
|
695 |
int code, i; |
int code, i; |
696 |
|
|
697 |
code = BitstreamShowBits(bs, 11); |
code = BitstreamShowBits(bs, 11); |
698 |
|
|
699 |
for(i = 11; i > 3; i--) { |
for(i = 11; i > 3; i--) { |
710 |
} |
} |
711 |
|
|
712 |
|
|
713 |
int get_dc_size_chrom(Bitstream * bs) |
int |
714 |
|
get_dc_size_chrom(Bitstream * bs) |
715 |
{ |
{ |
716 |
|
|
717 |
uint32_t code, i; |
uint32_t code, i; |
718 |
|
|
719 |
code = BitstreamShowBits(bs, 12); |
code = BitstreamShowBits(bs, 12); |
720 |
|
|
721 |
for(i = 12; i > 2; i--) { |
for(i = 12; i > 2; i--) { |
730 |
|
|
731 |
} |
} |
732 |
|
|
733 |
void get_intra_block(Bitstream * bs, int16_t * block, int direction, int coeff) |
void |
734 |
|
get_intra_block(Bitstream * bs, |
735 |
|
int16_t * block, |
736 |
|
int direction, |
737 |
|
int coeff) |
738 |
{ |
{ |
739 |
|
|
740 |
const uint16_t * scan = scan_tables[ direction ]; |
const uint16_t * scan = scan_tables[ direction ]; |
742 |
int run; |
int run; |
743 |
int last; |
int last; |
744 |
|
|
745 |
do |
do { |
|
{ |
|
746 |
level = get_coeff(bs, &run, &last, 1, 0); |
level = get_coeff(bs, &run, &last, 1, 0); |
747 |
if (run == -1) |
if (run == -1) { |
|
{ |
|
748 |
DEBUG("fatal: invalid run"); |
DEBUG("fatal: invalid run"); |
749 |
break; |
break; |
750 |
} |
} |
751 |
coeff += run; |
coeff += run; |
752 |
block[ scan[coeff] ] = level; |
block[ scan[coeff] ] = level; |
753 |
if (level < -127 || level > 127) |
if (level < -127 || level > 127) { |
|
{ |
|
754 |
DEBUG1("warning: intra_overflow", level); |
DEBUG1("warning: intra_overflow", level); |
755 |
} |
} |
756 |
coeff++; |
coeff++; |
758 |
|
|
759 |
} |
} |
760 |
|
|
761 |
void get_inter_block(Bitstream * bs, int16_t * block) |
void |
762 |
|
get_inter_block(Bitstream * bs, |
763 |
|
int16_t * block) |
764 |
{ |
{ |
765 |
|
|
766 |
const uint16_t * scan = scan_tables[0]; |
const uint16_t * scan = scan_tables[0]; |
770 |
int last; |
int last; |
771 |
|
|
772 |
p = 0; |
p = 0; |
773 |
do |
do { |
|
{ |
|
774 |
level = get_coeff(bs, &run, &last, 0, 0); |
level = get_coeff(bs, &run, &last, 0, 0); |
775 |
if (run == -1) |
if (run == -1) { |
|
{ |
|
776 |
DEBUG("fatal: invalid run"); |
DEBUG("fatal: invalid run"); |
777 |
break; |
break; |
778 |
} |
} |
779 |
p += run; |
p += run; |
780 |
|
|
781 |
block[ scan[p] ] = level; |
block[ scan[p] ] = level; |
782 |
if (level < -127 || level > 127) |
if (level < -127 || level > 127) { |
|
{ |
|
783 |
DEBUG1("warning: inter_overflow", level); |
DEBUG1("warning: inter_overflow", level); |
784 |
} |
} |
785 |
p++; |
p++; |