ffmpeg / libavcodec / h263.c @ 0ed714a7
History  View  Annotate  Download (202 KB)
1 
/*


2 
* H263/MPEG4 backend for ffmpeg encoder and decoder

3 
* Copyright (c) 2000,2001 Fabrice Bellard.

4 
* H263+ support.

5 
* Copyright (c) 2001 Juan J. Sierralta P.

6 
*

7 
* This library is free software; you can redistribute it and/or

8 
* modify it under the terms of the GNU Lesser General Public

9 
* License as published by the Free Software Foundation; either

10 
* version 2 of the License, or (at your option) any later version.

11 
*

12 
* This library is distributed in the hope that it will be useful,

13 
* but WITHOUT ANY WARRANTY; without even the implied warranty of

14 
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU

15 
* Lesser General Public License for more details.

16 
*

17 
* You should have received a copy of the GNU Lesser General Public

18 
* License along with this library; if not, write to the Free Software

19 
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 021111307 USA

20 
*

21 
* ac prediction encoding, bframe support, error resilience, optimizations,

22 
* qpel decoding, gmc decoding, interlaced decoding,

23 
* by Michael Niedermayer <michaelni@gmx.at>

24 
*/

25  
26 
/**

27 
* @file h263.c

28 
* h263/mpeg4 codec.

29 
*/

30 

31 
//#define DEBUG

32 
#include <limits.h> 
33  
34 
#include "common.h" 
35 
#include "dsputil.h" 
36 
#include "avcodec.h" 
37 
#include "mpegvideo.h" 
38 
#include "h263data.h" 
39 
#include "mpeg4data.h" 
40  
41 
//#undef NDEBUG

42 
//#include <assert.h>

43  
44 
#define INTRA_MCBPC_VLC_BITS 6 
45 
#define INTER_MCBPC_VLC_BITS 7 
46 
#define CBPY_VLC_BITS 6 
47 
#define MV_VLC_BITS 9 
48 
#define DC_VLC_BITS 9 
49 
#define SPRITE_TRAJ_VLC_BITS 6 
50 
#define MB_TYPE_B_VLC_BITS 4 
51 
#define TEX_VLC_BITS 9 
52 
#define H263_MBTYPE_B_VLC_BITS 6 
53 
#define CBPC_B_VLC_BITS 3 
54  
55 
#ifdef CONFIG_ENCODERS

56 
static void h263_encode_block(MpegEncContext * s, DCTELEM * block, 
57 
int n);

58 
static void h263_encode_motion(MpegEncContext * s, int val, int fcode); 
59 
static void h263p_encode_umotion(MpegEncContext * s, int val); 
60 
static inline void mpeg4_encode_block(MpegEncContext * s, DCTELEM * block, 
61 
int n, int dc, uint8_t *scan_table, 
62 
PutBitContext *dc_pb, PutBitContext *ac_pb); 
63 
#endif

64  
65 
static int h263_decode_motion(MpegEncContext * s, int pred, int fcode); 
66 
static int h263p_decode_umotion(MpegEncContext * s, int pred); 
67 
static int h263_decode_block(MpegEncContext * s, DCTELEM * block, 
68 
int n, int coded); 
69 
static inline int mpeg4_decode_dc(MpegEncContext * s, int n, int *dir_ptr); 
70 
static inline int mpeg4_decode_block(MpegEncContext * s, DCTELEM * block, 
71 
int n, int coded, int intra, int rvlc); 
72 
static int h263_pred_dc(MpegEncContext * s, int n, uint16_t **dc_val_ptr); 
73 
#ifdef CONFIG_ENCODERS

74 
static void mpeg4_encode_visual_object_header(MpegEncContext * s); 
75 
static void mpeg4_encode_vol_header(MpegEncContext * s, int vo_number, int vol_number); 
76 
#endif //CONFIG_ENCODERS 
77 
static void mpeg4_decode_sprite_trajectory(MpegEncContext * s); 
78 
static inline int ff_mpeg4_pred_dc(MpegEncContext * s, int n, uint16_t **dc_val_ptr, int *dir_ptr); 
79  
80 
#ifdef CONFIG_ENCODERS

81 
static uint8_t uni_DCtab_lum_len[512]; 
82 
static uint8_t uni_DCtab_chrom_len[512]; 
83 
static uint16_t uni_DCtab_lum_bits[512]; 
84 
static uint16_t uni_DCtab_chrom_bits[512]; 
85  
86 
static uint8_t (*mv_penalty)[MAX_MV*2+1]= NULL; 
87 
static uint8_t fcode_tab[MAX_MV*2+1]; 
88 
static uint8_t umv_fcode_tab[MAX_MV*2+1]; 
89  
90 
static uint32_t uni_mpeg4_intra_rl_bits[64*64*2*2]; 
91 
static uint8_t uni_mpeg4_intra_rl_len [64*64*2*2]; 
92 
static uint32_t uni_mpeg4_inter_rl_bits[64*64*2*2]; 
93 
static uint8_t uni_mpeg4_inter_rl_len [64*64*2*2]; 
94 
//#define UNI_MPEG4_ENC_INDEX(last,run,level) ((last)*128 + (run)*256 + (level))

95 
//#define UNI_MPEG4_ENC_INDEX(last,run,level) ((last)*128*64 + (run) + (level)*64)

96 
#define UNI_MPEG4_ENC_INDEX(last,run,level) ((last)*128*64 + (run)*128 + (level)) 
97  
98 
/* mpeg4

99 
inter

100 
max level: 24/6

101 
max run: 53/63

102 

103 
intra

104 
max level: 53/16

105 
max run: 29/41

106 
*/

107 
#endif

108  
109 
#if 0 //3IV1 is quite rare and tis slows things down a tiny bit

110 
#define IS_3IV1 s>avctx>codec_tag == ff_get_fourcc("3IV1")

111 
#else

112 
#define IS_3IV1 0 
113 
#endif

114  
115 
int h263_get_picture_format(int width, int height) 
116 
{ 
117 
int format;

118  
119 
if (width == 128 && height == 96) 
120 
format = 1;

121 
else if (width == 176 && height == 144) 
122 
format = 2;

123 
else if (width == 352 && height == 288) 
124 
format = 3;

125 
else if (width == 704 && height == 576) 
126 
format = 4;

127 
else if (width == 1408 && height == 1152) 
128 
format = 5;

129 
else

130 
format = 7;

131 
return format;

132 
} 
133  
134 
#ifdef CONFIG_ENCODERS

135  
136 
static void aspect_to_info(MpegEncContext * s, AVRational aspect){ 
137 
int i;

138  
139 
if(aspect.num==0) aspect= (AVRational){1,1}; 
140  
141 
for(i=1; i<6; i++){ 
142 
if(av_cmp_q(pixel_aspect[i], aspect) == 0){ 
143 
s>aspect_ratio_info=i; 
144 
return;

145 
} 
146 
} 
147 

148 
s>aspect_ratio_info= FF_ASPECT_EXTENDED; 
149 
} 
150  
151 
void ff_flv_encode_picture_header(MpegEncContext * s, int picture_number) 
152 
{ 
153 
int format;

154  
155 
align_put_bits(&s>pb); 
156  
157 
put_bits(&s>pb, 17, 1); 
158 
put_bits(&s>pb, 5, (s>h263_flv1)); /* 0: h263 escape codes 1: 11bit escape codes */ 
159 
put_bits(&s>pb, 8, (((int64_t)s>picture_number * 30 * s>avctx>frame_rate_base) / 
160 
s>avctx>frame_rate) & 0xff); /* TemporalReference */ 
161 
if (s>width == 352 && s>height == 288) 
162 
format = 2;

163 
else if (s>width == 176 && s>height == 144) 
164 
format = 3;

165 
else if (s>width == 128 && s>height == 96) 
166 
format = 4;

167 
else if (s>width == 320 && s>height == 240) 
168 
format = 5;

169 
else if (s>width == 160 && s>height == 120) 
170 
format = 6;

171 
else if (s>width <= 255 && s>height <= 255) 
172 
format = 0; /* use 1 byte width & height */ 
173 
else

174 
format = 1; /* use 2 bytes width & height */ 
175 
put_bits(&s>pb, 3, format); /* PictureSize */ 
176 
if (format == 0) { 
177 
put_bits(&s>pb, 8, s>width);

178 
put_bits(&s>pb, 8, s>height);

179 
} else if (format == 1) { 
180 
put_bits(&s>pb, 16, s>width);

181 
put_bits(&s>pb, 16, s>height);

182 
} 
183 
put_bits(&s>pb, 2, s>pict_type == P_TYPE); /* PictureType */ 
184 
put_bits(&s>pb, 1, 1); /* DeblockingFlag: on */ 
185 
put_bits(&s>pb, 5, s>qscale); /* Quantizer */ 
186 
put_bits(&s>pb, 1, 0); /* ExtraInformation */ 
187  
188 
if(s>h263_aic){

189 
s>y_dc_scale_table= 
190 
s>c_dc_scale_table= ff_aic_dc_scale_table; 
191 
}else{

192 
s>y_dc_scale_table= 
193 
s>c_dc_scale_table= ff_mpeg1_dc_scale_table; 
194 
} 
195 
} 
196  
197 
void h263_encode_picture_header(MpegEncContext * s, int picture_number) 
198 
{ 
199 
int format;

200  
201 
align_put_bits(&s>pb); 
202  
203 
/* Update the pointer to last GOB */

204 
s>ptr_lastgob = pbBufPtr(&s>pb); 
205 
put_bits(&s>pb, 22, 0x20); /* PSC */ 
206 
put_bits(&s>pb, 8, (((int64_t)s>picture_number * 30 * s>avctx>frame_rate_base) / 
207 
s>avctx>frame_rate) & 0xff);

208  
209 
put_bits(&s>pb, 1, 1); /* marker */ 
210 
put_bits(&s>pb, 1, 0); /* h263 id */ 
211 
put_bits(&s>pb, 1, 0); /* split screen off */ 
212 
put_bits(&s>pb, 1, 0); /* camera off */ 
213 
put_bits(&s>pb, 1, 0); /* freeze picture release off */ 
214 

215 
format = h263_get_picture_format(s>width, s>height); 
216 
if (!s>h263_plus) {

217 
/* H.263v1 */

218 
put_bits(&s>pb, 3, format);

219 
put_bits(&s>pb, 1, (s>pict_type == P_TYPE));

220 
/* By now UMV IS DISABLED ON H.263v1, since the restrictions

221 
of H.263v1 UMV implies to check the predicted MV after

222 
calculation of the current MB to see if we're on the limits */

223 
put_bits(&s>pb, 1, 0); /* unrestricted motion vector: off */ 
224 
put_bits(&s>pb, 1, 0); /* SAC: off */ 
225 
put_bits(&s>pb, 1, s>obmc); /* advanced prediction mode */ 
226 
put_bits(&s>pb, 1, 0); /* not PB frame */ 
227 
put_bits(&s>pb, 5, s>qscale);

228 
put_bits(&s>pb, 1, 0); /* Continuous Presence Multipoint mode: off */ 
229 
} else {

230 
/* H.263v2 */

231 
/* H.263 Plus PTYPE */

232 
put_bits(&s>pb, 3, 7); 
233 
put_bits(&s>pb,3,1); /* Update Full Extended PTYPE */ 
234 
if (format == 7) 
235 
put_bits(&s>pb,3,6); /* Custom Source Format */ 
236 
else

237 
put_bits(&s>pb, 3, format);

238 

239 
put_bits(&s>pb,1,0); /* Custom PCF: off */ 
240 
put_bits(&s>pb,1, s>umvplus); /* Unrestricted Motion Vector */ 
241 
put_bits(&s>pb,1,0); /* SAC: off */ 
242 
put_bits(&s>pb,1,s>obmc); /* Advanced Prediction Mode */ 
243 
put_bits(&s>pb,1,s>h263_aic); /* Advanced Intra Coding */ 
244 
put_bits(&s>pb,1,s>loop_filter); /* Deblocking Filter */ 
245 
put_bits(&s>pb,1,s>h263_slice_structured); /* Slice Structured */ 
246 
put_bits(&s>pb,1,0); /* Reference Picture Selection: off */ 
247 
put_bits(&s>pb,1,0); /* Independent Segment Decoding: off */ 
248 
put_bits(&s>pb,1,s>alt_inter_vlc); /* Alternative Inter VLC */ 
249 
put_bits(&s>pb,1,s>modified_quant); /* Modified Quantization: */ 
250 
put_bits(&s>pb,1,1); /* "1" to prevent start code emulation */ 
251 
put_bits(&s>pb,3,0); /* Reserved */ 
252 

253 
put_bits(&s>pb, 3, s>pict_type == P_TYPE);

254 

255 
put_bits(&s>pb,1,0); /* Reference Picture Resampling: off */ 
256 
put_bits(&s>pb,1,0); /* ReducedResolution Update: off */ 
257 
put_bits(&s>pb,1,s>no_rounding); /* Rounding Type */ 
258 
put_bits(&s>pb,2,0); /* Reserved */ 
259 
put_bits(&s>pb,1,1); /* "1" to prevent start code emulation */ 
260 

261 
/* This should be here if PLUSPTYPE */

262 
put_bits(&s>pb, 1, 0); /* Continuous Presence Multipoint mode: off */ 
263 

264 
if (format == 7) { 
265 
/* Custom Picture Format (CPFMT) */

266 
aspect_to_info(s, s>avctx>sample_aspect_ratio); 
267  
268 
put_bits(&s>pb,4,s>aspect_ratio_info);

269 
put_bits(&s>pb,9,(s>width >> 2)  1); 
270 
put_bits(&s>pb,1,1); /* "1" to prevent start code emulation */ 
271 
put_bits(&s>pb,9,(s>height >> 2)); 
272 
if (s>aspect_ratio_info == FF_ASPECT_EXTENDED){

273 
put_bits(&s>pb, 8, s>avctx>sample_aspect_ratio.num);

274 
put_bits(&s>pb, 8, s>avctx>sample_aspect_ratio.den);

275 
} 
276 
} 
277 

278 
/* Unlimited Unrestricted Motion Vectors Indicator (UUI) */

279 
if (s>umvplus)

280 
// put_bits(&s>pb,1,1); /* Limited according tables of Annex D */

281 
//FIXME check actual requested range

282 
put_bits(&s>pb,2,1); /* unlimited */ 
283 
if(s>h263_slice_structured)

284 
put_bits(&s>pb,2,0); /* no weird submodes */ 
285  
286 
put_bits(&s>pb, 5, s>qscale);

287 
} 
288  
289 
put_bits(&s>pb, 1, 0); /* no PEI */ 
290  
291 
if(s>h263_slice_structured){

292 
put_bits(&s>pb, 1, 1); 
293 

294 
assert(s>mb_x == 0 && s>mb_y == 0); 
295 
ff_h263_encode_mba(s); 
296  
297 
put_bits(&s>pb, 1, 1); 
298 
} 
299  
300 
if(s>h263_aic){

301 
s>y_dc_scale_table= 
302 
s>c_dc_scale_table= ff_aic_dc_scale_table; 
303 
}else{

304 
s>y_dc_scale_table= 
305 
s>c_dc_scale_table= ff_mpeg1_dc_scale_table; 
306 
} 
307 
} 
308  
309 
/**

310 
* Encodes a group of blocks header.

311 
*/

312 
void h263_encode_gob_header(MpegEncContext * s, int mb_line) 
313 
{ 
314 
put_bits(&s>pb, 17, 1); /* GBSC */ 
315  
316 
if(s>h263_slice_structured){

317 
put_bits(&s>pb, 1, 1); 
318  
319 
ff_h263_encode_mba(s); 
320  
321 
if(s>mb_num > 1583) 
322 
put_bits(&s>pb, 1, 1); 
323 
put_bits(&s>pb, 5, s>qscale); /* GQUANT */ 
324 
put_bits(&s>pb, 1, 1); 
325 
put_bits(&s>pb, 2, s>pict_type == I_TYPE); /* GFID */ 
326 
}else{

327 
int gob_number= mb_line / s>gob_index;

328  
329 
put_bits(&s>pb, 5, gob_number); /* GN */ 
330 
put_bits(&s>pb, 2, s>pict_type == I_TYPE); /* GFID */ 
331 
put_bits(&s>pb, 5, s>qscale); /* GQUANT */ 
332 
} 
333 
} 
334  
335 
static inline int get_block_rate(MpegEncContext * s, DCTELEM block[64], int block_last_index, uint8_t scantable[64]){ 
336 
int last=0; 
337 
int j;

338 
int rate=0; 
339  
340 
for(j=1; j<=block_last_index; j++){ 
341 
const int index= scantable[j]; 
342 
int level= block[index];

343 
if(level){

344 
level+= 64;

345 
if((level&(~127)) == 0){ 
346 
if(j<block_last_index) rate+= s>intra_ac_vlc_length [UNI_AC_ENC_INDEX(jlast1, level)]; 
347 
else rate+= s>intra_ac_vlc_last_length[UNI_AC_ENC_INDEX(jlast1, level)]; 
348 
}else

349 
rate += s>ac_esc_length; 
350 
level= 64;

351  
352 
last= j; 
353 
} 
354 
} 
355 

356 
return rate;

357 
} 
358  
359 
static inline int decide_ac_pred(MpegEncContext * s, DCTELEM block[6][64], int dir[6], uint8_t *st[6], int zigzag_last_index[6]) 
360 
{ 
361 
int score= 0; 
362 
int i, n;

363 
int8_t * const qscale_table= s>current_picture.qscale_table;

364  
365 
memcpy(zigzag_last_index, s>block_last_index, sizeof(int)*6); 
366 

367 
for(n=0; n<6; n++){ 
368 
int16_t *ac_val, *ac_val1; 
369 

370 
score = get_block_rate(s, block[n], s>block_last_index[n], s>intra_scantable.permutated); 
371  
372 
ac_val = s>ac_val[0][0] + s>block_index[n] * 16; 
373 
ac_val1= ac_val; 
374 
if(dir[n]){

375 
const int xy= s>mb_x + s>mb_y*s>mb_stride  s>mb_stride; 
376 
/* top prediction */

377 
ac_val= s>block_wrap[n]*16;

378 
if(s>mb_y==0  s>qscale == qscale_table[xy]  n==2  n==3){ 
379 
/* same qscale */

380 
for(i=1; i<8; i++){ 
381 
const int level= block[n][s>dsp.idct_permutation[i ]]; 
382 
block[n][s>dsp.idct_permutation[i ]] = level  ac_val[i+8];

383 
ac_val1[i ]= block[n][s>dsp.idct_permutation[i<<3]];

384 
ac_val1[i+8]= level;

385 
} 
386 
}else{

387 
/* different qscale, we must rescale */

388 
for(i=1; i<8; i++){ 
389 
const int level= block[n][s>dsp.idct_permutation[i ]]; 
390 
block[n][s>dsp.idct_permutation[i ]] = level  ROUNDED_DIV(ac_val[i + 8]*qscale_table[xy], s>qscale);

391 
ac_val1[i ]= block[n][s>dsp.idct_permutation[i<<3]];

392 
ac_val1[i+8]= level;

393 
} 
394 
} 
395 
st[n]= s>intra_h_scantable.permutated; 
396 
}else{

397 
const int xy= s>mb_x1 + s>mb_y*s>mb_stride; 
398 
/* left prediction */

399 
ac_val= 16;

400 
if(s>mb_x==0  s>qscale == qscale_table[xy]  n==1  n==3){ 
401 
/* same qscale */

402 
for(i=1; i<8; i++){ 
403 
const int level= block[n][s>dsp.idct_permutation[i<<3]]; 
404 
block[n][s>dsp.idct_permutation[i<<3]]= level  ac_val[i];

405 
ac_val1[i ]= level; 
406 
ac_val1[i+8]= block[n][s>dsp.idct_permutation[i ]];

407 
} 
408 
}else{

409 
/* different qscale, we must rescale */

410 
for(i=1; i<8; i++){ 
411 
const int level= block[n][s>dsp.idct_permutation[i<<3]]; 
412 
block[n][s>dsp.idct_permutation[i<<3]]= level  ROUNDED_DIV(ac_val[i]*qscale_table[xy], s>qscale);

413 
ac_val1[i ]= level; 
414 
ac_val1[i+8]= block[n][s>dsp.idct_permutation[i ]];

415 
} 
416 
} 
417 
st[n]= s>intra_v_scantable.permutated; 
418 
} 
419  
420 
for(i=63; i>0; i) //FIXME optimize 
421 
if(block[n][ st[n][i] ]) break; 
422 
s>block_last_index[n]= i; 
423  
424 
score += get_block_rate(s, block[n], s>block_last_index[n], st[n]); 
425 
} 
426  
427 
return score < 0; 
428 
} 
429  
430 
static inline void restore_ac_coeffs(MpegEncContext * s, DCTELEM block[6][64], int dir[6], uint8_t *st[6], int zigzag_last_index[6]) 
431 
{ 
432 
int i, n;

433 
memcpy(s>block_last_index, zigzag_last_index, sizeof(int)*6); 
434  
435 
for(n=0; n<6; n++){ 
436 
int16_t *ac_val = s>ac_val[0][0] + s>block_index[n] * 16; 
437  
438 
st[n]= s>intra_scantable.permutated; 
439 
if(dir[n]){

440 
/* top prediction */

441 
for(i=1; i<8; i++){ 
442 
block[n][s>dsp.idct_permutation[i ]] = ac_val[i+8];

443 
} 
444 
}else{

445 
/* left prediction */

446 
for(i=1; i<8; i++){ 
447 
block[n][s>dsp.idct_permutation[i<<3]]= ac_val[i ];

448 
} 
449 
} 
450 
} 
451 
} 
452  
453 
/**

454 
* modify qscale so that encoding is acually possible in h263 (limit difference to 2..2)

455 
*/

456 
void ff_clean_h263_qscales(MpegEncContext *s){

457 
int i;

458 
int8_t * const qscale_table= s>current_picture.qscale_table;

459 

460 
for(i=1; i<s>mb_num; i++){ 
461 
if(qscale_table[ s>mb_index2xy[i] ]  qscale_table[ s>mb_index2xy[i1] ] >2) 
462 
qscale_table[ s>mb_index2xy[i] ]= qscale_table[ s>mb_index2xy[i1] ]+2; 
463 
} 
464 
for(i=s>mb_num2; i>=0; i){ 
465 
if(qscale_table[ s>mb_index2xy[i] ]  qscale_table[ s>mb_index2xy[i+1] ] >2) 
466 
qscale_table[ s>mb_index2xy[i] ]= qscale_table[ s>mb_index2xy[i+1] ]+2; 
467 
} 
468 
} 
469  
470 
/**

471 
* modify mb_type & qscale so that encoding is acually possible in mpeg4

472 
*/

473 
void ff_clean_mpeg4_qscales(MpegEncContext *s){

474 
int i;

475 
int8_t * const qscale_table= s>current_picture.qscale_table;

476  
477 
ff_clean_h263_qscales(s); 
478 

479 
for(i=1; i<s>mb_num; i++){ 
480 
int mb_xy= s>mb_index2xy[i];

481 

482 
if(qscale_table[mb_xy] != qscale_table[s>mb_index2xy[i1]] && (s>mb_type[mb_xy]&MB_TYPE_INTER4V)){ 
483 
s>mb_type[mb_xy]&= ~MB_TYPE_INTER4V; 
484 
s>mb_type[mb_xy]= MB_TYPE_INTER; 
485 
} 
486 
} 
487  
488 
if(s>pict_type== B_TYPE){

489 
int odd=0; 
490 
/* ok, come on, this isnt funny anymore, theres more code for handling this mpeg4 mess than

491 
for the actual adaptive quantization */

492 

493 
for(i=0; i<s>mb_num; i++){ 
494 
int mb_xy= s>mb_index2xy[i];

495 
odd += qscale_table[mb_xy]&1;

496 
} 
497 

498 
if(2*odd > s>mb_num) odd=1; 
499 
else odd=0; 
500 

501 
for(i=0; i<s>mb_num; i++){ 
502 
int mb_xy= s>mb_index2xy[i];

503 
if((qscale_table[mb_xy]&1) != odd) 
504 
qscale_table[mb_xy]++; 
505 
if(qscale_table[mb_xy] > 31) 
506 
qscale_table[mb_xy]= 31;

507 
} 
508 

509 
for(i=1; i<s>mb_num; i++){ 
510 
int mb_xy= s>mb_index2xy[i];

511 
if(qscale_table[mb_xy] != qscale_table[s>mb_index2xy[i1]] && (s>mb_type[mb_xy]&MB_TYPE_DIRECT)){ 
512 
s>mb_type[mb_xy]&= ~MB_TYPE_DIRECT; 
513 
s>mb_type[mb_xy]= MB_TYPE_BIDIR; 
514 
} 
515 
} 
516 
} 
517 
} 
518  
519 
#endif //CONFIG_ENCODERS 
520 
/**

521 
*

522 
* @return the mb_type

523 
*/

524 
int ff_mpeg4_set_direct_mv(MpegEncContext *s, int mx, int my){ 
525 
const int mb_index= s>mb_x + s>mb_y*s>mb_stride; 
526 
const int colocated_mb_type= s>next_picture.mb_type[mb_index]; //FIXME or next? 
527 
int xy= s>block_index[0]; 
528 
uint16_t time_pp= s>pp_time; 
529 
uint16_t time_pb= s>pb_time; 
530 
int i;

531 

532 
//FIXME avoid divides

533 

534 
if(IS_8X8(colocated_mb_type)){

535 
s>mv_type = MV_TYPE_8X8; 
536 
for(i=0; i<4; i++){ 
537 
xy= s>block_index[i]; 
538 
s>mv[0][i][0] = s>next_picture.motion_val[0][xy][0]*time_pb/time_pp + mx; 
539 
s>mv[0][i][1] = s>next_picture.motion_val[0][xy][1]*time_pb/time_pp + my; 
540 
s>mv[1][i][0] = mx ? s>mv[0][i][0]  s>next_picture.motion_val[0][xy][0] 
541 
: s>next_picture.motion_val[0][xy][0]*(time_pb  time_pp)/time_pp; 
542 
s>mv[1][i][1] = my ? s>mv[0][i][1]  s>next_picture.motion_val[0][xy][1] 
543 
: s>next_picture.motion_val[0][xy][1]*(time_pb  time_pp)/time_pp; 
544 
} 
545 
return MB_TYPE_DIRECT2  MB_TYPE_8x8  MB_TYPE_L0L1;

546 
} else if(IS_INTERLACED(colocated_mb_type)){ 
547 
s>mv_type = MV_TYPE_FIELD; 
548 
for(i=0; i<2; i++){ 
549 
if(s>top_field_first){

550 
time_pp= s>pp_field_time  s>field_select_table[mb_index][i] + i; 
551 
time_pb= s>pb_field_time  s>field_select_table[mb_index][i] + i; 
552 
}else{

553 
time_pp= s>pp_field_time + s>field_select_table[mb_index][i]  i; 
554 
time_pb= s>pb_field_time + s>field_select_table[mb_index][i]  i; 
555 
} 
556 
s>mv[0][i][0] = s>field_mv_table[mb_index][i][0]*time_pb/time_pp + mx; 
557 
s>mv[0][i][1] = s>field_mv_table[mb_index][i][1]*time_pb/time_pp + my; 
558 
s>mv[1][i][0] = mx ? s>mv[0][i][0]  s>field_mv_table[mb_index][i][0] 
559 
: s>field_mv_table[mb_index][i][0]*(time_pb  time_pp)/time_pp;

560 
s>mv[1][i][1] = my ? s>mv[0][i][1]  s>field_mv_table[mb_index][i][1] 
561 
: s>field_mv_table[mb_index][i][1]*(time_pb  time_pp)/time_pp;

562 
} 
563 
return MB_TYPE_DIRECT2  MB_TYPE_16x8  MB_TYPE_L0L1  MB_TYPE_INTERLACED;

564 
}else{

565 
s>mv[0][0][0] = s>mv[0][1][0] = s>mv[0][2][0] = s>mv[0][3][0] = s>next_picture.motion_val[0][xy][0]*time_pb/time_pp + mx; 
566 
s>mv[0][0][1] = s>mv[0][1][1] = s>mv[0][2][1] = s>mv[0][3][1] = s>next_picture.motion_val[0][xy][1]*time_pb/time_pp + my; 
567 
s>mv[1][0][0] = s>mv[1][1][0] = s>mv[1][2][0] = s>mv[1][3][0] = mx ? s>mv[0][0][0]  s>next_picture.motion_val[0][xy][0] 
568 
: s>next_picture.motion_val[0][xy][0]*(time_pb  time_pp)/time_pp; 
569 
s>mv[1][0][1] = s>mv[1][1][1] = s>mv[1][2][1] = s>mv[1][3][1] = my ? s>mv[0][0][1]  s>next_picture.motion_val[0][xy][1] 
570 
: s>next_picture.motion_val[0][xy][1]*(time_pb  time_pp)/time_pp; 
571 
if((s>avctx>workaround_bugs & FF_BUG_DIRECT_BLOCKSIZE)  !s>quarter_sample)

572 
s>mv_type= MV_TYPE_16X16; 
573 
else

574 
s>mv_type= MV_TYPE_8X8; 
575 
return MB_TYPE_DIRECT2  MB_TYPE_16x16  MB_TYPE_L0L1; //Note see prev line 
576 
} 
577 
} 
578  
579 
void ff_h263_update_motion_val(MpegEncContext * s){

580 
const int mb_xy = s>mb_y * s>mb_stride + s>mb_x; 
581 
//FIXME a lot of thet is only needed for !low_delay

582 
const int wrap = s>block_wrap[0]; 
583 
const int xy = s>block_index[0]; 
584 

585 
s>current_picture.mbskip_table[mb_xy]= s>mb_skiped; 
586  
587 
if(s>mv_type != MV_TYPE_8X8){

588 
int motion_x, motion_y;

589 
if (s>mb_intra) {

590 
motion_x = 0;

591 
motion_y = 0;

592 
} else if (s>mv_type == MV_TYPE_16X16) { 
593 
motion_x = s>mv[0][0][0]; 
594 
motion_y = s>mv[0][0][1]; 
595 
} else /*if (s>mv_type == MV_TYPE_FIELD)*/ { 
596 
int i;

597 
motion_x = s>mv[0][0][0] + s>mv[0][1][0]; 
598 
motion_y = s>mv[0][0][1] + s>mv[0][1][1]; 
599 
motion_x = (motion_x>>1)  (motion_x&1); 
600 
for(i=0; i<2; i++){ 
601 
s>field_mv_table[mb_xy][i][0]= s>mv[0][i][0]; 
602 
s>field_mv_table[mb_xy][i][1]= s>mv[0][i][1]; 
603 
s>field_select_table[mb_xy][i]= s>field_select[0][i];

604 
} 
605 
} 
606 

607 
/* no update if 8X8 because it has been done during parsing */

608 
s>current_picture.motion_val[0][xy][0] = motion_x; 
609 
s>current_picture.motion_val[0][xy][1] = motion_y; 
610 
s>current_picture.motion_val[0][xy + 1][0] = motion_x; 
611 
s>current_picture.motion_val[0][xy + 1][1] = motion_y; 
612 
s>current_picture.motion_val[0][xy + wrap][0] = motion_x; 
613 
s>current_picture.motion_val[0][xy + wrap][1] = motion_y; 
614 
s>current_picture.motion_val[0][xy + 1 + wrap][0] = motion_x; 
615 
s>current_picture.motion_val[0][xy + 1 + wrap][1] = motion_y; 
616 
} 
617  
618 
if(s>encoding){ //FIXME encoding MUST be cleaned up 
619 
if (s>mv_type == MV_TYPE_8X8)

620 
s>current_picture.mb_type[mb_xy]= MB_TYPE_L0  MB_TYPE_8x8; 
621 
else if(s>mb_intra) 
622 
s>current_picture.mb_type[mb_xy]= MB_TYPE_INTRA; 
623 
else

624 
s>current_picture.mb_type[mb_xy]= MB_TYPE_L0  MB_TYPE_16x16; 
625 
} 
626 
} 
627  
628 
#ifdef CONFIG_ENCODERS

629  
630 
static inline int get_p_cbp(MpegEncContext * s, 
631 
DCTELEM block[6][64], 
632 
int motion_x, int motion_y){ 
633 
int cbp, i;

634  
635 
if(s>flags & CODEC_FLAG_CBP_RD){

636 
int best_cbpy_score= INT_MAX;

637 
int best_cbpc_score= INT_MAX;

638 
int cbpc = (1), cbpy= (1); 
639 
const int offset= (s>mv_type==MV_TYPE_16X16 ? 0 : 16) + (s>dquant ? 8 : 0); 
640 
const int lambda= s>lambda2 >> (FF_LAMBDA_SHIFT  6); 
641  
642 
for(i=0; i<4; i++){ 
643 
int score= inter_MCBPC_bits[i + offset] * lambda;

644 
if(i&1) score += s>coded_score[5]; 
645 
if(i&2) score += s>coded_score[4]; 
646  
647 
if(score < best_cbpc_score){

648 
best_cbpc_score= score; 
649 
cbpc= i; 
650 
} 
651 
} 
652  
653 
for(i=0; i<16; i++){ 
654 
int score= cbpy_tab[i ^ 0xF][1] * lambda; 
655 
if(i&1) score += s>coded_score[3]; 
656 
if(i&2) score += s>coded_score[2]; 
657 
if(i&4) score += s>coded_score[1]; 
658 
if(i&8) score += s>coded_score[0]; 
659  
660 
if(score < best_cbpy_score){

661 
best_cbpy_score= score; 
662 
cbpy= i; 
663 
} 
664 
} 
665 
cbp= cbpc + 4*cbpy;

666 
if ((motion_x  motion_y  s>dquant) == 0 && s>mv_type==MV_TYPE_16X16){ 
667 
if(best_cbpy_score + best_cbpc_score + 2*lambda >= 0) 
668 
cbp= 0;

669 
} 
670  
671 
for (i = 0; i < 6; i++) { 
672 
if (s>block_last_index[i] >= 0 && ((cbp >> (5  i))&1)==0 ){ 
673 
s>block_last_index[i]= 1;

674 
memset(s>block[i], 0, sizeof(DCTELEM)*64); 
675 
} 
676 
} 
677 
}else{

678 
cbp= 0;

679 
for (i = 0; i < 6; i++) { 
680 
if (s>block_last_index[i] >= 0) 
681 
cbp = 1 << (5  i); 
682 
} 
683 
} 
684 
return cbp;

685 
} 
686  
687 
static inline int get_b_cbp(MpegEncContext * s, DCTELEM block[6][64], 
688 
int motion_x, int motion_y, int mb_type){ 
689 
int cbp=0, i; 
690  
691 
if(s>flags & CODEC_FLAG_CBP_RD){

692 
int score=0; 
693 
const int lambda= s>lambda2 >> (FF_LAMBDA_SHIFT  6); 
694 

695 
for(i=0; i<6; i++){ 
696 
if(s>coded_score[i] < 0){ 
697 
score += s>coded_score[i]; 
698 
cbp = 1 << (5  i); 
699 
} 
700 
} 
701 

702 
if(cbp){

703 
int zero_score= 6; 
704 
if ((motion_x  motion_y  s>dquant  mb_type) == 0){ 
705 
zero_score= 4; //2*MV + mb_type + cbp bit 
706 
} 
707  
708 
zero_score*= lambda; 
709 
if(zero_score <= score){

710 
cbp=0;

711 
} 
712 
} 
713  
714 
for (i = 0; i < 6; i++) { 
715 
if (s>block_last_index[i] >= 0 && ((cbp >> (5  i))&1)==0 ){ 
716 
s>block_last_index[i]= 1;

717 
memset(s>block[i], 0, sizeof(DCTELEM)*64); 
718 
} 
719 
} 
720 
}else{

721 
for (i = 0; i < 6; i++) { 
722 
if (s>block_last_index[i] >= 0) 
723 
cbp = 1 << (5  i); 
724 
} 
725 
} 
726 
return cbp;

727 
} 
728  
729 
void mpeg4_encode_mb(MpegEncContext * s,

730 
DCTELEM block[6][64], 
731 
int motion_x, int motion_y) 
732 
{ 
733 
int cbpc, cbpy, pred_x, pred_y;

734 
PutBitContext * const pb2 = s>data_partitioning ? &s>pb2 : &s>pb;

735 
PutBitContext * const tex_pb = s>data_partitioning && s>pict_type!=B_TYPE ? &s>tex_pb : &s>pb;

736 
PutBitContext * const dc_pb = s>data_partitioning && s>pict_type!=I_TYPE ? &s>pb2 : &s>pb;

737 
const int interleaved_stats= (s>flags&CODEC_FLAG_PASS1) && !s>data_partitioning ? 1 : 0; 
738 
const int dquant_code[5]= {1,0,9,2,3}; 
739 

740 
// printf("**mb x=%d y=%d\n", s>mb_x, s>mb_y);

741 
if (!s>mb_intra) {

742 
int i, cbp;

743 

744 
if(s>pict_type==B_TYPE){

745 
static const int mb_type_table[8]= {1, 2, 3, 1,1,1,1, 0}; /* convert from mv_dir to type */ 
746 
int mb_type= mb_type_table[s>mv_dir];

747 

748 
if(s>mb_x==0){ 
749 
s>last_mv[0][0][0]= 
750 
s>last_mv[0][0][1]= 
751 
s>last_mv[1][0][0]= 
752 
s>last_mv[1][0][1]= 0; 
753 
} 
754 

755 
assert(s>dquant>=2 && s>dquant<=2); 
756 
assert((s>dquant&1)==0); 
757 
assert(mb_type>=0);

758  
759 
/* nothing to do if this MB was skiped in the next P Frame */

760 
if(s>next_picture.mbskip_table[s>mb_y * s>mb_stride + s>mb_x]){ //FIXME avoid DCT & ... 
761 
s>skip_count++; 
762 
s>mv[0][0][0]= 
763 
s>mv[0][0][1]= 
764 
s>mv[1][0][0]= 
765 
s>mv[1][0][1]= 0; 
766 
s>mv_dir= MV_DIR_FORWARD; //doesnt matter

767 
s>qscale = s>dquant; 
768 
// s>mb_skiped=1;

769  
770 
return;

771 
} 
772 

773 
cbp= get_b_cbp(s, block, motion_x, motion_y, mb_type); 
774 

775 
if ((cbp  motion_x  motion_y  mb_type) ==0) { 
776 
/* direct MB with MV={0,0} */

777 
assert(s>dquant==0);

778 

779 
put_bits(&s>pb, 1, 1); /* mb not coded modb1=1 */ 
780  
781 
if(interleaved_stats){

782 
s>misc_bits++; 
783 
s>last_bits++; 
784 
} 
785 
s>skip_count++; 
786 
return;

787 
} 
788 

789 
put_bits(&s>pb, 1, 0); /* mb coded modb1=0 */ 
790 
put_bits(&s>pb, 1, cbp ? 0 : 1); /* modb2 */ //FIXME merge 
791 
put_bits(&s>pb, mb_type+1, 1); // this table is so simple that we dont need it :) 
792 
if(cbp) put_bits(&s>pb, 6, cbp); 
793 

794 
if(cbp && mb_type){

795 
if(s>dquant)

796 
put_bits(&s>pb, 2, (s>dquant>>2)+3); 
797 
else

798 
put_bits(&s>pb, 1, 0); 
799 
}else

800 
s>qscale = s>dquant; 
801 

802 
if(!s>progressive_sequence){

803 
if(cbp)

804 
put_bits(&s>pb, 1, s>interlaced_dct);

805 
if(mb_type) // not diect mode 
806 
put_bits(&s>pb, 1, 0); // no interlaced ME yet 
807 
} 
808  
809 
if(interleaved_stats){

810 
s>misc_bits+= get_bits_diff(s); 
811 
} 
812  
813 
switch(mb_type)

814 
{ 
815 
case 0: /* direct */ 
816 
h263_encode_motion(s, motion_x, 1);

817 
h263_encode_motion(s, motion_y, 1);

818 
s>b_count++; 
819 
s>f_count++; 
820 
break;

821 
case 1: /* bidir */ 
822 
h263_encode_motion(s, s>mv[0][0][0]  s>last_mv[0][0][0], s>f_code); 
823 
h263_encode_motion(s, s>mv[0][0][1]  s>last_mv[0][0][1], s>f_code); 
824 
h263_encode_motion(s, s>mv[1][0][0]  s>last_mv[1][0][0], s>b_code); 
825 
h263_encode_motion(s, s>mv[1][0][1]  s>last_mv[1][0][1], s>b_code); 
826 
s>last_mv[0][0][0]= s>mv[0][0][0]; 
827 
s>last_mv[0][0][1]= s>mv[0][0][1]; 
828 
s>last_mv[1][0][0]= s>mv[1][0][0]; 
829 
s>last_mv[1][0][1]= s>mv[1][0][1]; 
830 
s>b_count++; 
831 
s>f_count++; 
832 
break;

833 
case 2: /* backward */ 
834 
h263_encode_motion(s, motion_x  s>last_mv[1][0][0], s>b_code); 
835 
h263_encode_motion(s, motion_y  s>last_mv[1][0][1], s>b_code); 
836 
s>last_mv[1][0][0]= motion_x; 
837 
s>last_mv[1][0][1]= motion_y; 
838 
s>b_count++; 
839 
break;

840 
case 3: /* forward */ 
841 
h263_encode_motion(s, motion_x  s>last_mv[0][0][0], s>f_code); 
842 
h263_encode_motion(s, motion_y  s>last_mv[0][0][1], s>f_code); 
843 
s>last_mv[0][0][0]= motion_x; 
844 
s>last_mv[0][0][1]= motion_y; 
845 
s>f_count++; 
846 
break;

847 
default:

848 
av_log(s>avctx, AV_LOG_ERROR, "unknown mb type\n");

849 
return;

850 
} 
851  
852 
if(interleaved_stats){

853 
s>mv_bits+= get_bits_diff(s); 
854 
} 
855  
856 
/* encode each block */

857 
for (i = 0; i < 6; i++) { 
858 
mpeg4_encode_block(s, block[i], i, 0, s>intra_scantable.permutated, NULL, &s>pb); 
859 
} 
860  
861 
if(interleaved_stats){

862 
s>p_tex_bits+= get_bits_diff(s); 
863 
} 
864 
}else{ /* s>pict_type==B_TYPE */ 
865 
cbp= get_p_cbp(s, block, motion_x, motion_y); 
866 

867 
if ((cbp  motion_x  motion_y  s>dquant) == 0 && s>mv_type==MV_TYPE_16X16) { 
868 
/* check if the B frames can skip it too, as we must skip it if we skip here

869 
why didnt they just compress the skipmb bits instead of reusing them ?! */

870 
if(s>max_b_frames>0){ 
871 
int i;

872 
int x,y, offset;

873 
uint8_t *p_pic; 
874  
875 
x= s>mb_x*16;

876 
y= s>mb_y*16;

877 
if(x+16 > s>width) x= s>width16; 
878 
if(y+16 > s>height) y= s>height16; 
879  
880 
offset= x + y*s>linesize; 
881 
p_pic= s>new_picture.data[0] + offset;

882 

883 
s>mb_skiped=1;

884 
for(i=0; i<s>max_b_frames; i++){ 
885 
uint8_t *b_pic; 
886 
int diff;

887 
Picture *pic= s>reordered_input_picture[i+1];

888  
889 
if(pic==NULL  pic>pict_type!=B_TYPE) break; 
890  
891 
b_pic= pic>data[0] + offset + 16; //FIXME +16 
892 
diff= s>dsp.pix_abs16x16(p_pic, b_pic, s>linesize); 
893 
if(diff>s>qscale*70){ //FIXME check that 70 is optimal 
894 
s>mb_skiped=0;

895 
break;

896 
} 
897 
} 
898 
}else

899 
s>mb_skiped=1;

900  
901 
if(s>mb_skiped==1){ 
902 
/* skip macroblock */

903 
put_bits(&s>pb, 1, 1); 
904  
905 
if(interleaved_stats){

906 
s>misc_bits++; 
907 
s>last_bits++; 
908 
} 
909 
s>skip_count++; 
910 

911 
return;

912 
} 
913 
} 
914  
915 
put_bits(&s>pb, 1, 0); /* mb coded */ 
916 
cbpc = cbp & 3;

917 
cbpy = cbp >> 2;

918 
cbpy ^= 0xf;

919 
if(s>mv_type==MV_TYPE_16X16){

920 
if(s>dquant) cbpc+= 8; 
921 
put_bits(&s>pb, 
922 
inter_MCBPC_bits[cbpc], 
923 
inter_MCBPC_code[cbpc]); 
924  
925 
put_bits(pb2, cbpy_tab[cbpy][1], cbpy_tab[cbpy][0]); 
926 
if(s>dquant)

927 
put_bits(pb2, 2, dquant_code[s>dquant+2]); 
928  
929 
if(!s>progressive_sequence){

930 
if(cbp)

931 
put_bits(pb2, 1, s>interlaced_dct);

932 
put_bits(pb2, 1, 0); // no interlaced ME yet 
933 
} 
934 

935 
if(interleaved_stats){

936 
s>misc_bits+= get_bits_diff(s); 
937 
} 
938  
939 
/* motion vectors: 16x16 mode */

940 
h263_pred_motion(s, 0, &pred_x, &pred_y);

941 

942 
h263_encode_motion(s, motion_x  pred_x, s>f_code); 
943 
h263_encode_motion(s, motion_y  pred_y, s>f_code); 
944 
}else{

945 
put_bits(&s>pb, 
946 
inter_MCBPC_bits[cbpc+16],

947 
inter_MCBPC_code[cbpc+16]);

948 
put_bits(pb2, cbpy_tab[cbpy][1], cbpy_tab[cbpy][0]); 
949  
950 
if(!s>progressive_sequence){

951 
if(cbp)

952 
put_bits(pb2, 1, s>interlaced_dct);

953 
} 
954 

955 
if(interleaved_stats){

956 
s>misc_bits+= get_bits_diff(s); 
957 
} 
958  
959 
for(i=0; i<4; i++){ 
960 
/* motion vectors: 8x8 mode*/

961 
h263_pred_motion(s, i, &pred_x, &pred_y); 
962  
963 
h263_encode_motion(s, s>current_picture.motion_val[0][ s>block_index[i] ][0]  pred_x, s>f_code); 
964 
h263_encode_motion(s, s>current_picture.motion_val[0][ s>block_index[i] ][1]  pred_y, s>f_code); 
965 
} 
966 
} 
967  
968 
if(interleaved_stats){

969 
s>mv_bits+= get_bits_diff(s); 
970 
} 
971  
972 
/* encode each block */

973 
for (i = 0; i < 6; i++) { 
974 
mpeg4_encode_block(s, block[i], i, 0, s>intra_scantable.permutated, NULL, tex_pb); 
975 
} 
976  
977 
if(interleaved_stats){

978 
s>p_tex_bits+= get_bits_diff(s); 
979 
} 
980 
s>f_count++; 
981 
} 
982 
} else {

983 
int cbp;

984 
int dc_diff[6]; //dc values with the dc prediction subtracted 
985 
int dir[6]; //prediction direction 
986 
int zigzag_last_index[6]; 
987 
uint8_t *scan_table[6];

988 
int i;

989  
990 
for(i=0; i<6; i++){ 
991 
const int level= block[i][0]; 
992 
uint16_t *dc_ptr; 
993  
994 
dc_diff[i]= level  ff_mpeg4_pred_dc(s, i, &dc_ptr, &dir[i]); 
995 
if (i < 4) { 
996 
*dc_ptr = level * s>y_dc_scale; 
997 
} else {

998 
*dc_ptr = level * s>c_dc_scale; 
999 
} 
1000 
} 
1001  
1002 
if(s>flags & CODEC_FLAG_AC_PRED){

1003 
s>ac_pred= decide_ac_pred(s, block, dir, scan_table, zigzag_last_index); 
1004 
if(!s>ac_pred)

1005 
restore_ac_coeffs(s, block, dir, scan_table, zigzag_last_index); 
1006 
}else{

1007 
for(i=0; i<6; i++) 
1008 
scan_table[i]= s>intra_scantable.permutated; 
1009 
} 
1010  
1011 
/* compute cbp */

1012 
cbp = 0;

1013 
for (i = 0; i < 6; i++) { 
1014 
if (s>block_last_index[i] >= 1) 
1015 
cbp = 1 << (5  i); 
1016 
} 
1017  
1018 
cbpc = cbp & 3;

1019 
if (s>pict_type == I_TYPE) {

1020 
if(s>dquant) cbpc+=4; 
1021 
put_bits(&s>pb, 
1022 
intra_MCBPC_bits[cbpc], 
1023 
intra_MCBPC_code[cbpc]); 
1024 
} else {

1025 
if(s>dquant) cbpc+=8; 
1026 
put_bits(&s>pb, 1, 0); /* mb coded */ 
1027 
put_bits(&s>pb, 
1028 
inter_MCBPC_bits[cbpc + 4],

1029 
inter_MCBPC_code[cbpc + 4]);

1030 
} 
1031 
put_bits(pb2, 1, s>ac_pred);

1032 
cbpy = cbp >> 2;

1033 
put_bits(pb2, cbpy_tab[cbpy][1], cbpy_tab[cbpy][0]); 
1034 
if(s>dquant)

1035 
put_bits(dc_pb, 2, dquant_code[s>dquant+2]); 
1036  
1037 
if(!s>progressive_sequence){

1038 
put_bits(dc_pb, 1, s>interlaced_dct);

1039 
} 
1040  
1041 
if(interleaved_stats){

1042 
s>misc_bits+= get_bits_diff(s); 
1043 
} 
1044  
1045 
/* encode each block */

1046 
for (i = 0; i < 6; i++) { 
1047 
mpeg4_encode_block(s, block[i], i, dc_diff[i], scan_table[i], dc_pb, tex_pb); 
1048 
} 
1049  
1050 
if(interleaved_stats){

1051 
s>i_tex_bits+= get_bits_diff(s); 
1052 
} 
1053 
s>i_count++; 
1054  
1055 
/* restore ac coeffs & last_index stuff if we messed them up with the prediction */

1056 
if(s>ac_pred)

1057 
restore_ac_coeffs(s, block, dir, scan_table, zigzag_last_index); 
1058 
} 
1059 
} 
1060  
1061 
void h263_encode_mb(MpegEncContext * s,

1062 
DCTELEM block[6][64], 
1063 
int motion_x, int motion_y) 
1064 
{ 
1065 
int cbpc, cbpy, i, cbp, pred_x, pred_y;

1066 
int16_t pred_dc; 
1067 
int16_t rec_intradc[6];

1068 
uint16_t *dc_ptr[6];

1069 
const int interleaved_stats= (s>flags&CODEC_FLAG_PASS1); 
1070 
const int dquant_code[5]= {1,0,9,2,3}; 
1071 

1072 
//printf("**mb x=%d y=%d\n", s>mb_x, s>mb_y);

1073 
if (!s>mb_intra) {

1074 
/* compute cbp */

1075 
cbp= get_p_cbp(s, block, motion_x, motion_y); 
1076  
1077 
if ((cbp  motion_x  motion_y  s>dquant) == 0) { 
1078 
/* skip macroblock */

1079 
put_bits(&s>pb, 1, 1); 
1080 
if(interleaved_stats){

1081 
s>misc_bits++; 
1082 
s>last_bits++; 
1083 
} 
1084 
return;

1085 
} 
1086 
put_bits(&s>pb, 1, 0); /* mb coded */ 
1087 

1088 
cbpc = cbp & 3;

1089 
cbpy = cbp >> 2;

1090 
if(s>alt_inter_vlc==0  cbpc!=3) 
1091 
cbpy ^= 0xF;

1092 
if(s>dquant) cbpc+= 8; 
1093 
if(s>mv_type==MV_TYPE_16X16){

1094 
put_bits(&s>pb, 
1095 
inter_MCBPC_bits[cbpc], 
1096 
inter_MCBPC_code[cbpc]); 
1097  
1098 
put_bits(&s>pb, cbpy_tab[cbpy][1], cbpy_tab[cbpy][0]); 
1099 
if(s>dquant)

1100 
put_bits(&s>pb, 2, dquant_code[s>dquant+2]); 
1101 

1102 
if(interleaved_stats){

1103 
s>misc_bits+= get_bits_diff(s); 
1104 
} 
1105  
1106 
/* motion vectors: 16x16 mode */

1107 
h263_pred_motion(s, 0, &pred_x, &pred_y);

1108 

1109 
if (!s>umvplus) {

1110 
h263_encode_motion(s, motion_x  pred_x, 1);

1111 
h263_encode_motion(s, motion_y  pred_y, 1);

1112 
} 
1113 
else {

1114 
h263p_encode_umotion(s, motion_x  pred_x); 
1115 
h263p_encode_umotion(s, motion_y  pred_y); 
1116 
if (((motion_x  pred_x) == 1) && ((motion_y  pred_y) == 1)) 
1117 
/* To prevent Start Code emulation */

1118 
put_bits(&s>pb,1,1); 
1119 
} 
1120 
}else{

1121 
put_bits(&s>pb, 
1122 
inter_MCBPC_bits[cbpc+16],

1123 
inter_MCBPC_code[cbpc+16]);

1124 
put_bits(&s>pb, cbpy_tab[cbpy][1], cbpy_tab[cbpy][0]); 
1125 
if(s>dquant)

1126 
put_bits(&s>pb, 2, dquant_code[s>dquant+2]); 
1127  
1128 
if(interleaved_stats){

1129 
s>misc_bits+= get_bits_diff(s); 
1130 
} 
1131  
1132 
for(i=0; i<4; i++){ 
1133 
/* motion vectors: 8x8 mode*/

1134 
h263_pred_motion(s, i, &pred_x, &pred_y); 
1135  
1136 
motion_x= s>current_picture.motion_val[0][ s>block_index[i] ][0]; 
1137 
motion_y= s>current_picture.motion_val[0][ s>block_index[i] ][1]; 
1138 
if (!s>umvplus) {

1139 
h263_encode_motion(s, motion_x  pred_x, 1);

1140 
h263_encode_motion(s, motion_y  pred_y, 1);

1141 
} 
1142 
else {

1143 
h263p_encode_umotion(s, motion_x  pred_x); 
1144 
h263p_encode_umotion(s, motion_y  pred_y); 
1145 
if (((motion_x  pred_x) == 1) && ((motion_y  pred_y) == 1)) 
1146 
/* To prevent Start Code emulation */

1147 
put_bits(&s>pb,1,1); 
1148 
} 
1149 
} 
1150 
} 
1151  
1152 
if(interleaved_stats){

1153 
s>mv_bits+= get_bits_diff(s); 
1154 
} 
1155 
} else {

1156 
assert(s>mb_intra); 
1157 

1158 
cbp = 0;

1159 
if (s>h263_aic) {

1160 
/* Predict DC */

1161 
for(i=0; i<6; i++) { 
1162 
int16_t level = block[i][0];

1163 
int scale;

1164 

1165 
if(i<4) scale= s>y_dc_scale; 
1166 
else scale= s>c_dc_scale;

1167  
1168 
pred_dc = h263_pred_dc(s, i, &dc_ptr[i]); 
1169 
level = pred_dc; 
1170 
/* Quant */

1171 
if (level >= 0) 
1172 
level = (level + (scale>>1))/scale;

1173 
else

1174 
level = (level  (scale>>1))/scale;

1175 

1176 
/* AIC can change CBP */

1177 
if (level == 0 && s>block_last_index[i] == 0) 
1178 
s>block_last_index[i] = 1;

1179  
1180 
if(!s>modified_quant){

1181 
if (level < 127) 
1182 
level = 127;

1183 
else if (level > 127) 
1184 
level = 127;

1185 
} 
1186  
1187 
block[i][0] = level;

1188 
/* Reconstruction */

1189 
rec_intradc[i] = scale*level + pred_dc; 
1190 
/* Oddify */

1191 
rec_intradc[i] = 1;

1192 
//if ((rec_intradc[i] % 2) == 0)

1193 
// rec_intradc[i]++;

1194 
/* Clipping */

1195 
if (rec_intradc[i] < 0) 
1196 
rec_intradc[i] = 0;

1197 
else if (rec_intradc[i] > 2047) 
1198 
rec_intradc[i] = 2047;

1199 

1200 
/* Update AC/DC tables */

1201 
*dc_ptr[i] = rec_intradc[i]; 
1202 
if (s>block_last_index[i] >= 0) 
1203 
cbp = 1 << (5  i); 
1204 
} 
1205 
}else{

1206 
for(i=0; i<6; i++) { 
1207 
/* compute cbp */

1208 
if (s>block_last_index[i] >= 1) 
1209 
cbp = 1 << (5  i); 
1210 
} 
1211 
} 
1212  
1213 
cbpc = cbp & 3;

1214 
if (s>pict_type == I_TYPE) {

1215 
if(s>dquant) cbpc+=4; 
1216 
put_bits(&s>pb, 
1217 
intra_MCBPC_bits[cbpc], 
1218 
intra_MCBPC_code[cbpc]); 
1219 
} else {

1220 
if(s>dquant) cbpc+=8; 
1221 
put_bits(&s>pb, 1, 0); /* mb coded */ 
1222 
put_bits(&s>pb, 
1223 
inter_MCBPC_bits[cbpc + 4],

1224 
inter_MCBPC_code[cbpc + 4]);

1225 
} 
1226 
if (s>h263_aic) {

1227 
/* XXX: currently, we do not try to use ac prediction */

1228 
put_bits(&s>pb, 1, 0); /* no AC prediction */ 
1229 
} 
1230 
cbpy = cbp >> 2;

1231 
put_bits(&s>pb, cbpy_tab[cbpy][1], cbpy_tab[cbpy][0]); 
1232 
if(s>dquant)

1233 
put_bits(&s>pb, 2, dquant_code[s>dquant+2]); 
1234  
1235 
if(interleaved_stats){

1236 
s>misc_bits+= get_bits_diff(s); 
1237 
} 
1238 
} 
1239  
1240 
for(i=0; i<6; i++) { 
1241 
/* encode each block */

1242 
h263_encode_block(s, block[i], i); 
1243 

1244 
/* Update INTRADC for decoding */

1245 
if (s>h263_aic && s>mb_intra) {

1246 
block[i][0] = rec_intradc[i];

1247 

1248 
} 
1249 
} 
1250  
1251 
if(interleaved_stats){

1252 
if (!s>mb_intra) {

1253 
s>p_tex_bits+= get_bits_diff(s); 
1254 
s>f_count++; 
1255 
}else{

1256 
s>i_tex_bits+= get_bits_diff(s); 
1257 
s>i_count++; 
1258 
} 
1259 
} 
1260 
} 
1261 
#endif

1262  
1263 
void ff_h263_loop_filter(MpegEncContext * s){

1264 
int qp_c;

1265 
const int linesize = s>linesize; 
1266 
const int uvlinesize= s>uvlinesize; 
1267 
const int xy = s>mb_y * s>mb_stride + s>mb_x; 
1268 
uint8_t *dest_y = s>dest[0];

1269 
uint8_t *dest_cb= s>dest[1];

1270 
uint8_t *dest_cr= s>dest[2];

1271 

1272 
// if(s>pict_type==B_TYPE && !s>readable) return;

1273  
1274 
/*

1275 
Diag Top

1276 
Left Center

1277 
*/

1278 
if(!IS_SKIP(s>current_picture.mb_type[xy])){

1279 
qp_c= s>qscale; 
1280 
s>dsp.h263_v_loop_filter(dest_y+8*linesize , linesize, qp_c);

1281 
s>dsp.h263_v_loop_filter(dest_y+8*linesize+8, linesize, qp_c); 
1282 
}else

1283 
qp_c= 0;

1284  
1285 
if(s>mb_y){

1286 
int qp_dt, qp_t, qp_tc;

1287  
1288 
if(IS_SKIP(s>current_picture.mb_type[xys>mb_stride]))

1289 
qp_t=0;

1290 
else

1291 
qp_t= s>current_picture.qscale_table[xys>mb_stride]; 
1292  
1293 
if(qp_c)

1294 
qp_tc= qp_c; 
1295 
else

1296 
qp_tc= qp_t; 
1297 

1298 
if(qp_tc){

1299 
const int chroma_qp= s>chroma_qscale_table[qp_tc]; 
1300 
s>dsp.h263_v_loop_filter(dest_y , linesize, qp_tc); 
1301 
s>dsp.h263_v_loop_filter(dest_y+8, linesize, qp_tc);

1302 

1303 
s>dsp.h263_v_loop_filter(dest_cb , uvlinesize, chroma_qp); 
1304 
s>dsp.h263_v_loop_filter(dest_cr , uvlinesize, chroma_qp); 
1305 
} 
1306 

1307 
if(qp_t)

1308 
s>dsp.h263_h_loop_filter(dest_y8*linesize+8 , linesize, qp_t); 
1309 

1310 
if(s>mb_x){

1311 
if(qp_t  IS_SKIP(s>current_picture.mb_type[xy1s>mb_stride])) 
1312 
qp_dt= qp_t; 
1313 
else

1314 
qp_dt= s>current_picture.qscale_table[xy1s>mb_stride];

1315 

1316 
if(qp_dt){

1317 
const int chroma_qp= s>chroma_qscale_table[qp_dt]; 
1318 
s>dsp.h263_h_loop_filter(dest_y 8*linesize , linesize, qp_dt);

1319 
s>dsp.h263_h_loop_filter(dest_cb8*uvlinesize, uvlinesize, chroma_qp);

1320 
s>dsp.h263_h_loop_filter(dest_cb8*uvlinesize, uvlinesize, chroma_qp);

1321 
} 
1322 
} 
1323 
} 
1324  
1325 
if(qp_c){

1326 
s>dsp.h263_h_loop_filter(dest_y +8, linesize, qp_c);

1327 
if(s>mb_y + 1 == s>mb_height) 
1328 
s>dsp.h263_h_loop_filter(dest_y+8*linesize+8, linesize, qp_c); 
1329 
} 
1330 

1331 
if(s>mb_x){

1332 
int qp_lc;

1333 
if(qp_c  IS_SKIP(s>current_picture.mb_type[xy1])) 
1334 
qp_lc= qp_c; 
1335 
else

1336 
qp_lc= s>current_picture.qscale_table[xy1];

1337 

1338 
if(qp_lc){

1339 
s>dsp.h263_h_loop_filter(dest_y, linesize, qp_lc); 
1340 
if(s>mb_y + 1 == s>mb_height){ 
1341 
const int chroma_qp= s>chroma_qscale_table[qp_lc]; 
1342 
s>dsp.h263_h_loop_filter(dest_y +8* linesize, linesize, qp_lc);

1343 
s>dsp.h263_h_loop_filter(dest_cb , uvlinesize, chroma_qp); 
1344 
s>dsp.h263_h_loop_filter(dest_cr , uvlinesize, chroma_qp); 
1345 
} 
1346 
} 
1347 
} 
1348 
} 
1349  
1350 
static int h263_pred_dc(MpegEncContext * s, int n, uint16_t **dc_val_ptr) 
1351 
{ 
1352 
int x, y, wrap, a, c, pred_dc, scale;

1353 
int16_t *dc_val, *ac_val; 
1354  
1355 
/* find prediction */

1356 
if (n < 4) { 
1357 
x = 2 * s>mb_x + 1 + (n & 1); 
1358 
y = 2 * s>mb_y + 1 + ((n & 2) >> 1); 
1359 
wrap = s>mb_width * 2 + 2; 
1360 
dc_val = s>dc_val[0];

1361 
ac_val = s>ac_val[0][0]; 
1362 
scale = s>y_dc_scale; 
1363 
} else {

1364 
x = s>mb_x + 1;

1365 
y = s>mb_y + 1;

1366 
wrap = s>mb_width + 2;

1367 
dc_val = s>dc_val[n  4 + 1]; 
1368 
ac_val = s>ac_val[n  4 + 1][0]; 
1369 
scale = s>c_dc_scale; 
1370 
} 
1371 
/* B C

1372 
* A X

1373 
*/

1374 
a = dc_val[(x  1) + (y) * wrap];

1375 
c = dc_val[(x) + (y  1) * wrap];

1376 

1377 
/* No prediction outside GOB boundary */

1378 
if (s>first_slice_line && ((n < 2)  (n > 3))) 
1379 
c = 1024;

1380 
pred_dc = 1024;

1381 
/* just DC prediction */

1382 
if (a != 1024 && c != 1024) 
1383 
pred_dc = (a + c) >> 1;

1384 
else if (a != 1024) 
1385 
pred_dc = a; 
1386 
else

1387 
pred_dc = c; 
1388 

1389 
/* we assume pred is positive */

1390 
//pred_dc = (pred_dc + (scale >> 1)) / scale;

1391 
*dc_val_ptr = &dc_val[x + y * wrap]; 
1392 
return pred_dc;

1393 
} 
1394  
1395 
static void h263_pred_acdc(MpegEncContext * s, DCTELEM *block, int n) 
1396 
{ 
1397 
int x, y, wrap, a, c, pred_dc, scale, i;

1398 
int16_t *dc_val, *ac_val, *ac_val1; 
1399  
1400 
/* find prediction */

1401 
if (n < 4) { 
1402 
x = 2 * s>mb_x + 1 + (n & 1); 
1403 
y = 2 * s>mb_y + 1 + (n>> 1); 
1404 
wrap = s>mb_width * 2 + 2; 
1405 
dc_val = s>dc_val[0];

1406 
ac_val = s>ac_val[0][0]; 
1407 
scale = s>y_dc_scale; 
1408 
} else {

1409 
x = s>mb_x + 1;

1410 
y = s>mb_y + 1;

1411 
wrap = s>mb_width + 2;

1412 
dc_val = s>dc_val[n  4 + 1]; 
1413 
ac_val = s>ac_val[n  4 + 1][0]; 
1414 
scale = s>c_dc_scale; 
1415 
} 
1416 

1417 
ac_val += ((y) * wrap + (x)) * 16;

1418 
ac_val1 = ac_val; 
1419 

1420 
/* B C

1421 
* A X

1422 
*/

1423 
a = dc_val[(x  1) + (y) * wrap];

1424 
c = dc_val[(x) + (y  1) * wrap];

1425 

1426 
/* No prediction outside GOB boundary */

1427 
if(s>first_slice_line && n!=3){ 
1428 
if(n!=2) c= 1024; 
1429 
if(n!=1 && s>mb_x == s>resync_mb_x) a= 1024; 
1430 
} 
1431 

1432 
if (s>ac_pred) {

1433 
pred_dc = 1024;

1434 
if (s>h263_aic_dir) {

1435 
/* left prediction */

1436 
if (a != 1024) { 
1437 
ac_val = 16;

1438 
for(i=1;i<8;i++) { 
1439 
block[s>dsp.idct_permutation[i<<3]] += ac_val[i];

1440 
} 
1441 
pred_dc = a; 
1442 
} 
1443 
} else {

1444 
/* top prediction */

1445 
if (c != 1024) { 
1446 
ac_val = 16 * wrap;

1447 
for(i=1;i<8;i++) { 
1448 
block[s>dsp.idct_permutation[i ]] += ac_val[i + 8];

1449 
} 
1450 
pred_dc = c; 
1451 
} 
1452 
} 
1453 
} else {

1454 
/* just DC prediction */

1455 
if (a != 1024 && c != 1024) 
1456 
pred_dc = (a + c) >> 1;

1457 
else if (a != 1024) 
1458 
pred_dc = a; 
1459 
else

1460 
pred_dc = c; 
1461 
} 
1462 

1463 
/* we assume pred is positive */

1464 
block[0]=block[0]*scale + pred_dc; 
1465 

1466 
if (block[0] < 0) 
1467 
block[0] = 0; 
1468 
else

1469 
block[0] = 1; 
1470 

1471 
/* Update AC/DC tables */

1472 
dc_val[(x) + (y) * wrap] = block[0];

1473 

1474 
/* left copy */

1475 
for(i=1;i<8;i++) 
1476 
ac_val1[i ] = block[s>dsp.idct_permutation[i<<3]];

1477 
/* top copy */

1478 
for(i=1;i<8;i++) 
1479 
ac_val1[8 + i] = block[s>dsp.idct_permutation[i ]];

1480 
} 
1481  
1482 
int16_t *h263_pred_motion(MpegEncContext * s, int block,

1483 
int *px, int *py) 
1484 
{ 
1485 
int xy, wrap;

1486 
int16_t *A, *B, *C, *mot_val; 
1487 
static const int off[4]= {2, 1, 1, 1}; 
1488  
1489 
wrap = s>block_wrap[0];

1490 
xy = s>block_index[block]; 
1491  
1492 
mot_val = s>current_picture.motion_val[0][xy];

1493  
1494 
A = s>current_picture.motion_val[0][xy  1]; 
1495 
/* special case for first (slice) line */

1496 
if (s>first_slice_line && block<3) { 
1497 
// we cant just change some MVs to simulate that as we need them for the B frames (and ME)

1498 
// and if we ever support non rectangular objects than we need to do a few ifs here anyway :(

1499 
if(block==0){ //most common case 
1500 
if(s>mb_x == s>resync_mb_x){ //rare 
1501 
*px= *py = 0;

1502 
}else if(s>mb_x + 1 == s>resync_mb_x && s>h263_pred){ //rare 
1503 
C = s>current_picture.motion_val[0][xy + off[block]  wrap];

1504 
if(s>mb_x==0){ 
1505 
*px = C[0];

1506 
*py = C[1];

1507 
}else{

1508 
*px = mid_pred(A[0], 0, C[0]); 
1509 
*py = mid_pred(A[1], 0, C[1]); 
1510 
} 
1511 
}else{

1512 
*px = A[0];

1513 
*py = A[1];

1514 
} 
1515 
}else if(block==1){ 
1516 
if(s>mb_x + 1 == s>resync_mb_x && s>h263_pred){ //rare 
1517 
C = s>current_picture.motion_val[0][xy + off[block]  wrap];

1518 
*px = mid_pred(A[0], 0, C[0]); 
1519 
*py = mid_pred(A[1], 0, C[1]); 
1520 
}else{

1521 
*px = A[0];

1522 
*py = A[1];

1523 
} 
1524 
}else{ /* block==2*/ 
1525 
B = s>current_picture.motion_val[0][xy  wrap];

1526 
C = s>current_picture.motion_val[0][xy + off[block]  wrap];

1527 
if(s>mb_x == s>resync_mb_x) //rare 
1528 
A[0]=A[1]=0; 
1529 

1530 
*px = mid_pred(A[0], B[0], C[0]); 
1531 
*py = mid_pred(A[1], B[1], C[1]); 
1532 
} 
1533 
} else {

1534 
B = s>current_picture.motion_val[0][xy  wrap];

1535 
C = s>current_picture.motion_val[0][xy + off[block]  wrap];

1536 
*px = mid_pred(A[0], B[0], C[0]); 
1537 
*py = mid_pred(A[1], B[1], C[1]); 
1538 
} 
1539 
return mot_val;

1540 
} 
1541  
1542 
// identical to above but with s>current_picture>motion_val, the above one will be removed, and this renamed to it

1543 
int16_t *h263_pred_motion2(MpegEncContext * s, int block, int dir, 
1544 
int *px, int *py) 
1545 
{ 
1546 
int xy, wrap;

1547 
int16_t *A, *B, *C, (*mot_val)[2];

1548 
static const int off[4]= {2, 1, 1, 1}; 
1549  
1550 
wrap = s>b8_stride; 
1551 
xy = s>mb_x + s>mb_y * wrap; 
1552  
1553 
mot_val = s>current_picture.motion_val[0][dir] + xy;

1554  
1555 
A = mot_val[  1];

1556 
/* special case for first (slice) line */

1557 
if (s>first_slice_line && block<3) { 
1558 
// we cant just change some MVs to simulate that as we need them for the B frames (and ME)

1559 
// and if we ever support non rectangular objects than we need to do a few ifs here anyway :(

1560 
if(block==0){ //most common case 
1561 
if(s>mb_x == s>resync_mb_x){ //rare 
1562 
*px= *py = 0;

1563 
}else if(s>mb_x + 1 == s>resync_mb_x && s>h263_pred){ //rare 
1564 
C = mot_val[off[block]  wrap]; 
1565 
if(s>mb_x==0){ 
1566 
*px = C[0];

1567 
*py = C[1];

1568 
}else{

1569 
*px = mid_pred(A[0], 0, C[0]); 
1570 
*py = mid_pred(A[1], 0, C[1]); 
1571 
} 
1572 
}else{

1573 
*px = A[0];

1574 
*py = A[1];

1575 
} 
1576 
}else if(block==1){ 
1577 
if(s>mb_x + 1 == s>resync_mb_x && s>h263_pred){ //rare 
1578 
C = mot_val[off[block]  wrap]; 
1579 
*px = mid_pred(A[0], 0, C[0]); 
1580 
*py = mid_pred(A[1], 0, C[1]); 
1581 
}else{

1582 
*px = A[0];

1583 
*py = A[1];

1584 
} 
1585 
}else{ /* block==2*/ 
1586 
B = mot_val[  wrap]; 
1587 
C = mot_val[off[block]  wrap]; 
1588 
if(s>mb_x == s>resync_mb_x) //rare 
1589 
A[0]=A[1]=0; 
1590 

1591 
*px = mid_pred(A[0], B[0], C[0]); 
1592 
*py = mid_pred(A[1], B[1], C[1]); 
1593 
} 
1594 
} else {

1595 
B = mot_val[  wrap]; 
1596 
C = mot_val[off[block]  wrap]; 
1597 
*px = mid_pred(A[0], B[0], C[0]); 
1598 
*py = mid_pred(A[1], B[1], C[1]); 
1599 
} 
1600 
return *mot_val;

1601 
} 
1602  
1603 
#ifdef CONFIG_ENCODERS

1604 
static void h263_encode_motion(MpegEncContext * s, int val, int f_code) 
1605 
{ 
1606 
int range, l, bit_size, sign, code, bits;

1607  
1608 
if (val == 0) { 
1609 
/* zero vector */

1610 
code = 0;

1611 
put_bits(&s>pb, mvtab[code][1], mvtab[code][0]); 
1612 
} else {

1613 
bit_size = f_code  1;

1614 
range = 1 << bit_size;

1615 
/* modulo encoding */

1616 
l = range * 32;

1617 
#if 1 
1618 
val+= l; 
1619 
val&= 2*l1; 
1620 
val= l; 
1621 
sign = val>>31;

1622 
val= (val^sign)sign; 
1623 
sign&=1;

1624 
#else

1625 
if (val < l) {

1626 
val += 2*l;

1627 
} else if (val >= l) { 
1628 
val = 2*l;

1629 
} 
1630  
1631 
assert(val>=l && val<l); 
1632  
1633 
if (val >= 0) { 
1634 
sign = 0;

1635 
} else {

1636 
val = val; 
1637 
sign = 1;

1638 
} 
1639 
#endif

1640 
val; 
1641 
code = (val >> bit_size) + 1;

1642 
bits = val & (range  1);

1643  
1644 
put_bits(&s>pb, mvtab[code][1] + 1, (mvtab[code][0] << 1)  sign); 
1645 
if (bit_size > 0) { 
1646 
put_bits(&s>pb, bit_size, bits); 
1647 
} 
1648 
} 
1649  
1650 
} 
1651  
1652 
/* Encode MV differences on H.263+ with Unrestricted MV mode */

1653 
static void h263p_encode_umotion(MpegEncContext * s, int val) 
1654 
{ 
1655 
short sval = 0; 
1656 
short i = 0; 
1657 
short n_bits = 0; 
1658 
short temp_val;

1659 
int code = 0; 
1660 
int tcode;

1661 

1662 
if ( val == 0) 
1663 
put_bits(&s>pb, 1, 1); 
1664 
else if (val == 1) 
1665 
put_bits(&s>pb, 3, 0); 
1666 
else if (val == 1) 
1667 
put_bits(&s>pb, 3, 2); 
1668 
else {

1669 

1670 
sval = ((val < 0) ? (short)(val):(short)val); 
1671 
temp_val = sval; 
1672 

1673 
while (temp_val != 0) { 
1674 
temp_val = temp_val >> 1;

1675 
n_bits++; 
1676 
} 
1677 

1678 
i = n_bits  1;

1679 
while (i > 0) { 
1680 
tcode = (sval & (1 << (i1))) >> (i1); 
1681 
tcode = (tcode << 1)  1; 
1682 
code = (code << 2)  tcode;

1683 
i; 
1684 
} 
1685 
code = ((code << 1)  (val < 0)) << 1; 
1686 
put_bits(&s>pb, (2*n_bits)+1, code); 
1687 
//printf("\nVal = %d\tCode = %d", sval, code);

1688 
} 
1689 
} 
1690  
1691 
static void init_mv_penalty_and_fcode(MpegEncContext *s) 
1692 
{ 
1693 
int f_code;

1694 
int mv;

1695 

1696 
if(mv_penalty==NULL) 
1697 
mv_penalty= av_mallocz( sizeof(uint8_t)*(MAX_FCODE+1)*(2*MAX_MV+1) ); 
1698 

1699 
for(f_code=1; f_code<=MAX_FCODE; f_code++){ 
1700 
for(mv=MAX_MV; mv<=MAX_MV; mv++){

1701 
int len;

1702  
1703 
if(mv==0) len= mvtab[0][1]; 
1704 
else{

1705 
int val, bit_size, range, code;

1706  
1707 
bit_size = s>f_code  1;

1708 
range = 1 << bit_size;

1709  
1710 
val=mv; 
1711 
if (val < 0) 
1712 
val = val; 
1713 
val; 
1714 
code = (val >> bit_size) + 1;

1715 
if(code<33){ 
1716 
len= mvtab[code][1] + 1 + bit_size; 
1717 
}else{

1718 
len= mvtab[32][1] + 2 + bit_size; 
1719 
} 
1720 
} 
1721  
1722 
mv_penalty[f_code][mv+MAX_MV]= len; 
1723 
} 
1724 
} 
1725  
1726 
for(f_code=MAX_FCODE; f_code>0; f_code){ 
1727 
for(mv=(16<<f_code); mv<(16<<f_code); mv++){ 
1728 
fcode_tab[mv+MAX_MV]= f_code; 
1729 
} 
1730 
} 
1731  
1732 
for(mv=0; mv<MAX_MV*2+1; mv++){ 
1733 
umv_fcode_tab[mv]= 1;

1734 
} 
1735 
} 
1736 
#endif

1737  
1738 
#ifdef CONFIG_ENCODERS

1739  
1740 
static void init_uni_dc_tab(void) 
1741 
{ 
1742 
int level, uni_code, uni_len;

1743  
1744 
for(level=256; level<256; level++){ 
1745 
int size, v, l;

1746 
/* find number of bits */

1747 
size = 0;

1748 
v = abs(level); 
1749 
while (v) {

1750 
v >>= 1;

1751 
size++; 
1752 
} 
1753  
1754 
if (level < 0) 
1755 
l= (level) ^ ((1 << size)  1); 
1756 
else

1757 
l= level; 
1758  
1759 
/* luminance */

1760 
uni_code= DCtab_lum[size][0];

1761 
uni_len = DCtab_lum[size][1];

1762  
1763 
if (size > 0) { 
1764 
uni_code<<=size; uni_code=l; 
1765 
uni_len+=size; 
1766 
if (size > 8){ 
1767 
uni_code<<=1; uni_code=1; 
1768 
uni_len++; 
1769 
} 
1770 
} 
1771 
uni_DCtab_lum_bits[level+256]= uni_code;

1772 
uni_DCtab_lum_len [level+256]= uni_len;

1773  
1774 
/* chrominance */

1775 
uni_code= DCtab_chrom[size][0];

1776 
uni_len = DCtab_chrom[size][1];

1777 

1778 
if (size > 0) { 
1779 
uni_code<<=size; uni_code=l; 
1780 
uni_len+=size; 
1781 
if (size > 8){ 
1782 
uni_code<<=1; uni_code=1; 
1783 
uni_len++; 
1784 
} 
1785 
} 
1786 
uni_DCtab_chrom_bits[level+256]= uni_code;

1787 
uni_DCtab_chrom_len [level+256]= uni_len;

1788  
1789 
} 
1790 
} 
1791  
1792 
#endif //CONFIG_ENCODERS 
1793  
1794 
#ifdef CONFIG_ENCODERS

1795 
static void init_uni_mpeg4_rl_tab(RLTable *rl, uint32_t *bits_tab, uint8_t *len_tab){ 
1796 
int slevel, run, last;

1797 

1798 
assert(MAX_LEVEL >= 64);

1799 
assert(MAX_RUN >= 63);

1800  
1801 
for(slevel=64; slevel<64; slevel++){ 
1802 
if(slevel==0) continue; 
1803 
for(run=0; run<64; run++){ 
1804 
for(last=0; last<=1; last++){ 
1805 
const int index= UNI_MPEG4_ENC_INDEX(last, run, slevel+64); 
1806 
int level= slevel < 0 ? slevel : slevel; 
1807 
int sign= slevel < 0 ? 1 : 0; 
1808 
int bits, len, code;

1809 
int level1, run1;

1810 

1811 
len_tab[index]= 100;

1812 

1813 
/* ESC0 */

1814 
code= get_rl_index(rl, last, run, level); 
1815 
bits= rl>table_vlc[code][0];

1816 
len= rl>table_vlc[code][1];

1817 
bits=bits*2+sign; len++;

1818 

1819 
if(code!=rl>n && len < len_tab[index]){

1820 
bits_tab[index]= bits; 
1821 
len_tab [index]= len; 
1822 
} 
1823 
#if 1 
1824 
/* ESC1 */

1825 
bits= rl>table_vlc[rl>n][0];

1826 
len= rl>table_vlc[rl>n][1];

1827 
bits=bits*2; len++; //esc1 
1828 
level1= level  rl>max_level[last][run]; 
1829 
if(level1>0){ 
1830 
code= get_rl_index(rl, last, run, level1); 
1831 
bits<<= rl>table_vlc[code][1];

1832 
len += rl>table_vlc[code][1];

1833 
bits += rl>table_vlc[code][0];

1834 
bits=bits*2+sign; len++;

1835 

1836 
if(code!=rl>n && len < len_tab[index]){

1837 
bits_tab[index]= bits; 
1838 
len_tab [index]= len; 
1839 
} 
1840 
} 
1841 
#endif

1842 
#if 1 
1843 
/* ESC2 */

1844 
bits= rl>table_vlc[rl>n][0];

1845 
len= rl>table_vlc[rl>n][1];

1846 
bits=bits*4+2; len+=2; //esc2 
1847 
run1 = run  rl>max_run[last][level]  1;

1848 
if(run1>=0){ 
1849 
code= get_rl_index(rl, last, run1, level); 
1850 
bits<<= rl>table_vlc[code][1];

1851 
len += rl>table_vlc[code][1];

1852 
bits += rl>table_vlc[code][0];

1853 
bits=bits*2+sign; len++;

1854 

1855 
if(code!=rl>n && len < len_tab[index]){

1856 
bits_tab[index]= bits; 
1857 
len_tab [index]= len; 
1858 
} 
1859 
} 
1860 
#endif

1861 
/* ESC3 */

1862 
bits= rl>table_vlc[rl>n][0];

1863 
len = rl>table_vlc[rl>n][1];

1864 
bits=bits*4+3; len+=2; //esc3 
1865 
bits=bits*2+last; len++;

1866 
bits=bits*64+run; len+=6; 
1867 
bits=bits*2+1; len++; //marker 
1868 
bits=bits*4096+(slevel&0xfff); len+=12; 
1869 
bits=bits*2+1; len++; //marker 
1870 

1871 
if(len < len_tab[index]){

1872 
bits_tab[index]= bits; 
1873 
len_tab [index]= len; 
1874 
} 
1875 
} 
1876 
} 
1877 
} 
1878 
} 
1879  
1880 
void h263_encode_init(MpegEncContext *s)

1881 
{ 
1882 
static int done = 0; 
1883  
1884 
if (!done) {

1885 
done = 1;

1886  
1887 
init_uni_dc_tab(); 
1888  
1889 
init_rl(&rl_inter); 
1890 
init_rl(&rl_intra); 
1891 
init_rl(&rl_intra_aic); 
1892 

1893 
init_uni_mpeg4_rl_tab(&rl_intra, uni_mpeg4_intra_rl_bits, uni_mpeg4_intra_rl_len); 
1894 
init_uni_mpeg4_rl_tab(&rl_inter, uni_mpeg4_inter_rl_bits, uni_mpeg4_inter_rl_len); 
1895  
1896 
init_mv_penalty_and_fcode(s); 
1897 
} 
1898 
s>me.mv_penalty= mv_penalty; //FIXME exact table for msmpeg4 & h263p

1899 

1900 
// use fcodes >1 only for mpeg4 & h263 & h263p FIXME

1901 
switch(s>codec_id){

1902 
case CODEC_ID_MPEG4:

1903 
s>fcode_tab= fcode_tab; 
1904 
s>min_qcoeff= 2048;

1905 
s>max_qcoeff= 2047;

1906 
s>intra_ac_vlc_length = uni_mpeg4_intra_rl_len; 
1907 
s>intra_ac_vlc_last_length= uni_mpeg4_intra_rl_len + 128*64; 
1908 
s>inter_ac_vlc_length = uni_mpeg4_inter_rl_len; 
1909 
s>inter_ac_vlc_last_length= uni_mpeg4_inter_rl_len + 128*64; 
1910 
s>luma_dc_vlc_length= uni_DCtab_lum_len; 
1911 
s>chroma_dc_vlc_length= uni_DCtab_chrom_len; 
1912 
s>ac_esc_length= 7+2+1+6+1+12+1; 
1913 

1914 
if(s>flags & CODEC_FLAG_GLOBAL_HEADER){

1915  
1916 
s>avctx>extradata= av_malloc(1024);

1917 
init_put_bits(&s>pb, s>avctx>extradata, 1024);

1918 

1919 
mpeg4_encode_visual_object_header(s); 
1920 
mpeg4_encode_vol_header(s, 0, 0); 
1921  
1922 
// ff_mpeg4_stuffing(&s>pb); ?

1923 
flush_put_bits(&s>pb); 
1924 
s>avctx>extradata_size= (get_bit_count(&s>pb)+7)>>3; 
1925 
} 
1926 

1927 
break;

1928 
case CODEC_ID_H263P:

1929 
if(s>umvplus)

1930 
s>fcode_tab= umv_fcode_tab; 
1931 
else

1932 
s>fcode_tab= fcode_tab; 
1933 
if(s>modified_quant){

1934 
s>min_qcoeff= 2047;

1935 
s>max_qcoeff= 2047;

1936 
}else{

1937 
s>min_qcoeff= 127;

1938 
s>max_qcoeff= 127;

1939 
} 
1940 
break;

1941 
//Note for mpeg4 & h263 the dcscale table will be set per frame as needed later

1942 
case CODEC_ID_FLV1:

1943 
if (s>h263_flv > 1) { 
1944 
s>min_qcoeff= 1023;

1945 
s>max_qcoeff= 1023;

1946 
} else {

1947 
s>min_qcoeff= 127;

1948 
s>max_qcoeff= 127;

1949 
} 
1950 
s>y_dc_scale_table= 
1951 
s>c_dc_scale_table= ff_mpeg1_dc_scale_table; 
1952 
break;

1953 
default: //nothing needed default table allready set in mpegvideo.c 
1954 
s>min_qcoeff= 127;

1955 
s>max_qcoeff= 127;

1956 
s>y_dc_scale_table= 
1957 
s>c_dc_scale_table= ff_mpeg1_dc_scale_table; 
1958 
} 
1959 
} 
1960  
1961 
/**

1962 
* encodes a 8x8 block.

1963 
* @param block the 8x8 block

1964 
* @param n block index (03 are luma, 45 are chroma)

1965 
*/

1966 
static void h263_encode_block(MpegEncContext * s, DCTELEM * block, int n) 
1967 
{ 
1968 
int level, run, last, i, j, last_index, last_non_zero, sign, slevel, code;

1969 
RLTable *rl; 
1970  
1971 
rl = &rl_inter; 
1972 
if (s>mb_intra && !s>h263_aic) {

1973 
/* DC coef */

1974 
level = block[0];

1975 
/* 255 cannot be represented, so we clamp */

1976 
if (level > 254) { 
1977 
level = 254;

1978 
block[0] = 254; 
1979 
} 
1980 
/* 0 cannot be represented also */

1981 
else if (level < 1) { 
1982 
level = 1;

1983 
block[0] = 1; 
1984 
} 
1985 
if (level == 128) //FIXME check rv10 
1986 
put_bits(&s>pb, 8, 0xff); 
1987 
else

1988 
put_bits(&s>pb, 8, level & 0xff); 
1989 
i = 1;

1990 
} else {

1991 
i = 0;

1992 
if (s>h263_aic && s>mb_intra)

1993 
rl = &rl_intra_aic; 
1994 

1995 
if(s>alt_inter_vlc && !s>mb_intra){

1996 
int aic_vlc_bits=0; 
1997 
int inter_vlc_bits=0; 
1998 
int wrong_pos=1; 
1999 
int aic_code;

2000 

2001 
last_index = s>block_last_index[n]; 
2002 
last_non_zero = i  1;

2003 
for (; i <= last_index; i++) {

2004 
j = s>intra_scantable.permutated[i]; 
2005 
level = block[j]; 
2006 
if (level) {

2007 
run = i  last_non_zero  1;

2008 
last = (i == last_index); 
2009 

2010 
if(level<0) level= level; 
2011 

2012 
code = get_rl_index(rl, last, run, level); 
2013 
aic_code = get_rl_index(&rl_intra_aic, last, run, level); 
2014 
inter_vlc_bits += rl>table_vlc[code][1]+1; 
2015 
aic_vlc_bits += rl_intra_aic.table_vlc[aic_code][1]+1; 
2016  
2017 
if (code == rl>n) {

2018 
inter_vlc_bits += 1+6+81; 
2019 
} 
2020 
if (aic_code == rl_intra_aic.n) {

2021 
aic_vlc_bits += 1+6+81; 
2022 
wrong_pos += run + 1;

2023 
}else

2024 
wrong_pos += wrong_run[aic_code]; 
2025 
last_non_zero = i; 
2026 
} 
2027 
} 
2028 
i = 0;

2029 
if(aic_vlc_bits < inter_vlc_bits && wrong_pos > 63) 
2030 
rl = &rl_intra_aic; 
2031 
} 
2032 
} 
2033 

2034 
/* AC coefs */

2035 
last_index = s>block_last_index[n]; 
2036 
last_non_zero = i  1;

2037 
for (; i <= last_index; i++) {

2038 
j = s>intra_scantable.permutated[i]; 
2039 
level = block[j]; 
2040 
if (level) {

2041 
run = i  last_non_zero  1;

2042 
last = (i == last_index); 
2043 
sign = 0;

2044 
slevel = level; 
2045 
if (level < 0) { 
2046 
sign = 1;

2047 
level = level; 
2048 
} 
2049 
code = get_rl_index(rl, last, run, level); 
2050 
put_bits(&s>pb, rl>table_vlc[code][1], rl>table_vlc[code][0]); 
2051 
if (code == rl>n) {

2052 
if(s>h263_flv <= 1){ 
2053 
put_bits(&s>pb, 1, last);

2054 
put_bits(&s>pb, 6, run);

2055 

2056 
assert(slevel != 0);

2057  
2058 
if(slevel < 128 && slevel > 128) 
2059 
put_bits(&s>pb, 8, slevel & 0xff); 
2060 
else{

2061 
put_bits(&s>pb, 8, 128); 
2062 
put_bits(&s>pb, 5, slevel & 0x1f); 
2063 
put_bits(&s>pb, 6, (slevel>>5)&0x3f); 
2064 
} 
2065 
}else{

2066 
if(slevel < 64 && slevel > 64) { 
2067 
/* 7bit level */

2068 
put_bits(&s>pb, 1, 0); 
2069 
put_bits(&s>pb, 1, last);

2070 
put_bits(&s>pb, 6, run);

2071  
2072 
put_bits(&s>pb, 7, slevel & 0x7f); 
2073 
} else {

2074 
/* 11bit level */

2075 
put_bits(&s>pb, 1, 1); 
2076 
put_bits(&s>pb, 1, last);

2077 
put_bits(&s>pb, 6, run);

2078  
2079 
put_bits(&s>pb, 11, slevel & 0x7ff); 
2080 
} 
2081 
} 
2082 
} else {

2083 
put_bits(&s>pb, 1, sign);

2084 
} 
2085 
last_non_zero = i; 
2086 
} 
2087 
} 
2088 
} 
2089 
#endif

2090  
2091 
#ifdef CONFIG_ENCODERS

2092  
2093 
/***************************************************/

2094 
/**

2095 
* add mpeg4 stuffing bits (01...1)

2096 
*/

2097 
void ff_mpeg4_stuffing(PutBitContext * pbc)

2098 
{ 
2099 
int length;

2100 
put_bits(pbc, 1, 0); 
2101 
length= (get_bit_count(pbc))&7;

2102 
if(length) put_bits(pbc, length, (1<<length)1); 
2103 
} 
2104  
2105 
/* must be called before writing the header */

2106 
void ff_set_mpeg4_time(MpegEncContext * s, int picture_number){ 
2107 
int time_div, time_mod;

2108  
2109 
if(s>current_picture_ptr>pts)

2110 
s>time= (s>current_picture_ptr>pts*s>time_increment_resolution + 500*1000)/(1000*1000); 
2111 
else

2112 
s>time= av_rescale(picture_number*(int64_t)s>avctx>frame_rate_base, s>time_increment_resolution, s>avctx>frame_rate); 
2113 
time_div= s>time/s>time_increment_resolution; 
2114 
time_mod= s>time%s>time_increment_resolution; 
2115  
2116 
if(s>pict_type==B_TYPE){

2117 
s>pb_time= s>pp_time  (s>last_non_b_time  s>time); 
2118 
}else{

2119 
s>last_time_base= s>time_base; 
2120 
s>time_base= time_div; 
2121 
s>pp_time= s>time  s>last_non_b_time; 
2122 
s>last_non_b_time= s>time; 
2123 
} 
2124 
} 
2125  
2126 
static void mpeg4_encode_gop_header(MpegEncContext * s){ 
2127 
int hours, minutes, seconds;

2128 

2129 
put_bits(&s>pb, 16, 0); 
2130 
put_bits(&s>pb, 16, GOP_STARTCODE);

2131 

2132 
seconds= s>time/s>time_increment_resolution; 
2133 
minutes= seconds/60; seconds %= 60; 
2134 
hours= minutes/60; minutes %= 60; 
2135 
hours%=24;

2136  
2137 
put_bits(&s>pb, 5, hours);

2138 
put_bits(&s>pb, 6, minutes);

2139 
put_bits(&s>pb, 1, 1); 
2140 
put_bits(&s>pb, 6, seconds);

2141 

2142 
put_bits(&s>pb, 1, 0); //closed gov == NO 
2143 
put_bits(&s>pb, 1, 0); //broken link == NO 
2144  
2145 
ff_mpeg4_stuffing(&s>pb); 
2146 
} 
2147  
2148 
static void mpeg4_encode_visual_object_header(MpegEncContext * s){ 
2149 
int profile_and_level_indication;

2150 
int vo_ver_id;

2151 

2152 
if(s>max_b_frames  s>quarter_sample){

2153 
profile_and_level_indication= 0xF1; // adv simple level 1 
2154 
vo_ver_id= 5;

2155 
}else{

2156 
profile_and_level_indication= 0x01; // simple level 1 
2157 
vo_ver_id= 1;

2158 
} 
2159 
//FIXME levels

2160  
2161 
put_bits(&s>pb, 16, 0); 
2162 
put_bits(&s>pb, 16, VOS_STARTCODE);

2163  
2164 
put_bits(&s>pb, 8, profile_and_level_indication);

2165  
2166 
put_bits(&s>pb, 16, 0); 
2167 
put_bits(&s>pb, 16, VISUAL_OBJ_STARTCODE);

2168 

2169 
put_bits(&s>pb, 1, 1); 
2170 
put_bits(&s>pb, 4, vo_ver_id);

2171 
put_bits(&s>pb, 3, 1); //priority 
2172 

2173 
put_bits(&s>pb, 4, 1); //visual obj type== video obj 
2174 

2175 
put_bits(&s>pb, 1, 0); //video signal type == no clue //FIXME 
2176  
2177 
ff_mpeg4_stuffing(&s>pb); 
2178 
} 
2179  
2180 
static void mpeg4_encode_vol_header(MpegEncContext * s, int vo_number, int vol_number) 
2181 
{ 
2182 
int vo_ver_id;

2183  
2184 
if(s>max_b_frames  s>quarter_sample){

2185 
vo_ver_id= 5;

2186 
s>vo_type= ADV_SIMPLE_VO_TYPE; 
2187 
}else{

2188 
vo_ver_id= 1;

2189 
s>vo_type= SIMPLE_VO_TYPE; 
2190 
} 
2191  
2192 
put_bits(&s>pb, 16, 0); 
2193 
put_bits(&s>pb, 16, 0x100 + vo_number); /* video obj */ 
2194 
put_bits(&s>pb, 16, 0); 
2195 
put_bits(&s>pb, 16, 0x120 + vol_number); /* video obj layer */ 
2196  
2197 
put_bits(&s>pb, 1, 0); /* random access vol */ 
2198 
put_bits(&s>pb, 8, s>vo_type); /* video obj type indication */ 
2199 
put_bits(&s>pb, 1, 1); /* is obj layer id= yes */ 
2200 
put_bits(&s>pb, 4, vo_ver_id); /* is obj layer ver id */ 
2201 
put_bits(&s>pb, 3, 1); /* is obj layer priority */ 
2202 

2203 
aspect_to_info(s, s>avctx>sample_aspect_ratio); 
2204  
2205 
put_bits(&s>pb, 4, s>aspect_ratio_info);/* aspect ratio info */ 
2206 
if (s>aspect_ratio_info == FF_ASPECT_EXTENDED){

2207 
put_bits(&s>pb, 8, s>avctx>sample_aspect_ratio.num);

2208 
put_bits(&s>pb, 8, s>avctx>sample_aspect_ratio.den);

2209 
} 
2210  
2211 
if(s>low_delay){

2212 
put_bits(&s>pb, 1, 1); /* vol control parameters= yes */ 
2213 
put_bits(&s>pb, 2, 1); /* chroma format YUV 420/YV12 */ 
2214 
put_bits(&s>pb, 1, s>low_delay);

2215 
put_bits(&s>pb, 1, 0); /* vbv parameters= no */ 
2216 
}else{

2217 
put_bits(&s>pb, 1, 0); /* vol control parameters= no */ 
2218 
} 
2219  
2220 
put_bits(&s>pb, 2, RECT_SHAPE); /* vol shape= rectangle */ 
2221 
put_bits(&s>pb, 1, 1); /* marker bit */ 
2222 

2223 
put_bits(&s>pb, 16, s>time_increment_resolution);

2224 
if (s>time_increment_bits < 1) 
2225 
s>time_increment_bits = 1;

2226 
put_bits(&s>pb, 1, 1); /* marker bit */ 
2227 
put_bits(&s>pb, 1, 0); /* fixed vop rate=no */ 
2228 
put_bits(&s>pb, 1, 1); /* marker bit */ 
2229 
put_bits(&s>pb, 13, s>width); /* vol width */ 
2230 
put_bits(&s>pb, 1, 1); /* marker bit */ 
2231 
put_bits(&s>pb, 13, s>height); /* vol height */ 
2232 
put_bits(&s>pb, 1, 1); /* marker bit */ 
2233 
put_bits(&s>pb, 1, s>progressive_sequence ? 0 : 1); 
2234 
put_bits(&s>pb, 1, 1); /* obmc disable */ 
2235 
if (vo_ver_id == 1) { 
2236 
put_bits(&s>pb, 1, s>vol_sprite_usage=0); /* sprite enable */ 
2237 
}else{

2238 
put_bits(&s>pb, 2, s>vol_sprite_usage=0); /* sprite enable */ 
2239 
} 
2240 

2241 
s>quant_precision=5;

2242 
put_bits(&s>pb, 1, 0); /* not 8 bit == false */ 
2243 
put_bits(&s>pb, 1, s>mpeg_quant); /* quant type= (0=h263 style)*/ 
2244  
2245 
if(s>mpeg_quant){

2246 
ff_write_quant_matrix(&s>pb, s>avctx>intra_matrix); 
2247 
ff_write_quant_matrix(&s>pb, s>avctx>inter_matrix); 
2248 
} 
2249  
2250 
if (vo_ver_id != 1) 
2251 
put_bits(&s>pb, 1, s>quarter_sample);

2252 
put_bits(&s>pb, 1, 1); /* complexity estimation disable */ 
2253 
s>resync_marker= s>rtp_mode; 
2254 
put_bits(&s>pb, 1, s>resync_marker ? 0 : 1);/* resync marker disable */ 
2255 
put_bits(&s>pb, 1, s>data_partitioning ? 1 : 0); 
2256 
if(s>data_partitioning){

2257 
put_bits(&s>pb, 1, 0); /* no rvlc */ 
2258 
} 
2259  
2260 
if (vo_ver_id != 1){ 
2261 
put_bits(&s>pb, 1, 0); /* newpred */ 
2262 
put_bits(&s>pb, 1, 0); /* reduced res vop */ 
2263 
} 
2264 
put_bits(&s>pb, 1, 0); /* scalability */ 
2265 

2266 
ff_mpeg4_stuffing(&s>pb); 
2267  
2268 
/* user data */

2269 
if(!(s>flags & CODEC_FLAG_BITEXACT)){

2270 
put_bits(&s>pb, 16, 0); 
2271 
put_bits(&s>pb, 16, 0x1B2); /* user_data */ 
2272 
put_string(&s>pb, LIBAVCODEC_IDENT); 
2273 
ff_mpeg4_stuffing(&s>pb); 
2274 
} 
2275 
} 
2276  
2277 
/* write mpeg4 VOP header */

2278 
void mpeg4_encode_picture_header(MpegEncContext * s, int picture_number) 
2279 
{ 
2280 
int time_incr;

2281 
int time_div, time_mod;

2282 

2283 
if(s>pict_type==I_TYPE){

2284 
if(!(s>flags&CODEC_FLAG_GLOBAL_HEADER)){

2285 
if(s>strict_std_compliance < 2) //HACK, the reference sw is buggy 
2286 
mpeg4_encode_visual_object_header(s); 
2287 
if(s>strict_std_compliance < 2  picture_number==0) //HACK, the reference sw is buggy 
2288 
mpeg4_encode_vol_header(s, 0, 0); 
2289 
} 
2290 
mpeg4_encode_gop_header(s); 
2291 
} 
2292 

2293 
s>partitioned_frame= s>data_partitioning && s>pict_type!=B_TYPE; 
2294  
2295 
//printf("num:%d rate:%d base:%d\n", s>picture_number, s>frame_rate, FRAME_RATE_BASE);

2296 

2297 
put_bits(&s>pb, 16, 0); /* vop header */ 
2298 
put_bits(&s>pb, 16, VOP_STARTCODE); /* vop header */ 
2299 
put_bits(&s>pb, 2, s>pict_type  1); /* pict type: I = 0 , P = 1 */ 
2300  
2301 
time_div= s>time/s>time_increment_resolution; 
2302 
time_mod= s>time%s>time_increment_resolution; 
2303 
time_incr= time_div  s>last_time_base; 
2304 
while(time_incr)

2305 
put_bits(&s>pb, 1, 1); 
2306 

2307 
put_bits(&s>pb, 1, 0); 
2308  
2309 
put_bits(&s>pb, 1, 1); /* marker */ 
2310 
put_bits(&s>pb, s>time_increment_bits, time_mod); /* time increment */

2311 
put_bits(&s>pb, 1, 1); /* marker */ 
2312 
put_bits(&s>pb, 1, 1); /* vop coded */ 
2313 
if ( s>pict_type == P_TYPE

2314 
 (s>pict_type == S_TYPE && s>vol_sprite_usage==GMC_SPRITE)) { 
2315 
put_bits(&s>pb, 1, s>no_rounding); /* rounding type */ 
2316 
} 
2317 
put_bits(&s>pb, 3, 0); /* intra dc VLC threshold */ 
2318 
if(!s>progressive_sequence){

2319 
put_bits(&s>pb, 1, s>current_picture_ptr>top_field_first);

2320 
put_bits(&s>pb, 1, s>alternate_scan);

2321 
} 
2322 
//FIXME sprite stuff

2323  
2324 
put_bits(&s>pb, 5, s>qscale);

2325  
2326 
if (s>pict_type != I_TYPE)

2327 
put_bits(&s>pb, 3, s>f_code); /* fcode_for */ 
2328 
if (s>pict_type == B_TYPE)

2329 
put_bits(&s>pb, 3, s>b_code); /* fcode_back */ 
2330 
// printf("****frame %d\n", picture_number);

2331  
2332 
s>y_dc_scale_table= ff_mpeg4_y_dc_scale_table; //FIXME add short header support

2333 
s>c_dc_scale_table= ff_mpeg4_c_dc_scale_table; 
2334 
} 
2335  
2336 
#endif //CONFIG_ENCODERS 
2337  
2338 
/**

2339 
* set qscale and update qscale dependant variables.

2340 
*/

2341 
void ff_set_qscale(MpegEncContext * s, int qscale) 
2342 
{ 
2343 
if (qscale < 1) 
2344 
qscale = 1;

2345 
else if (qscale > 31) 
2346 
qscale = 31;

2347 

2348 
s>qscale = qscale; 
2349 
s>chroma_qscale= s>chroma_qscale_table[qscale]; 
2350  
2351 
s>y_dc_scale= s>y_dc_scale_table[ qscale ]; 
2352 
s>c_dc_scale= s>c_dc_scale_table[ s>chroma_qscale ]; 
2353 
} 
2354  
2355 
/**

2356 
* predicts the dc.

2357 
* @param n block index (03 are luma, 45 are chroma)

2358 
* @param dc_val_ptr a pointer to the dc_val entry for the current MB will be stored here

2359 
* @param dir_ptr pointer to an integer where the prediction direction will be stored

2360 
* @return the quantized predicted dc

2361 
*/

2362 
static inline int ff_mpeg4_pred_dc(MpegEncContext * s, int n, uint16_t **dc_val_ptr, int *dir_ptr) 
2363 
{ 
2364 
int a, b, c, wrap, pred, scale;

2365 
uint16_t *dc_val; 
2366  
2367 
/* find prediction */

2368 
if (n < 4) { 
2369 
scale = s>y_dc_scale; 
2370 
} else {

2371 
scale = s>c_dc_scale; 
2372 
} 
2373 
if(IS_3IV1)

2374 
scale= 8;

2375  
2376 
wrap= s>block_wrap[n]; 
2377 
dc_val = s>dc_val[0] + s>block_index[n];

2378  
2379 
/* B C

2380 
* A X

2381 
*/

2382 
a = dc_val[  1];

2383 
b = dc_val[  1  wrap];

2384 
c = dc_val[  wrap]; 
2385  
2386 
/* outside slice handling (we cant do that by memset as we need the dc for error resilience) */

2387 
if(s>first_slice_line && n!=3){ 
2388 
if(n!=2) b=c= 1024; 
2389 
if(n!=1 && s>mb_x == s>resync_mb_x) b=a= 1024; 
2390 
} 
2391 
if(s>mb_x == s>resync_mb_x && s>mb_y == s>resync_mb_y+1){ 
2392 
if(n==0  n==4  n==5) 
2393 
b=1024;

2394 
} 
2395  
2396 
if (abs(a  b) < abs(b  c)) {

2397 
pred = c; 
2398 
*dir_ptr = 1; /* top */ 
2399 
} else {

2400 
pred = a; 
2401 
*dir_ptr = 0; /* left */ 
2402 
} 
2403 
/* we assume pred is positive */

2404 
pred = FASTDIV((pred + (scale >> 1)), scale);

2405  
2406 
/* prepare address for prediction update */

2407 
*dc_val_ptr = &dc_val[0];

2408  
2409 
return pred;

2410 
} 
2411  
2412 
/**

2413 
* predicts the ac.

2414 
* @param n block index (03 are luma, 45 are chroma)

2415 
* @param dir the ac prediction direction

2416 
*/

2417 
void mpeg4_pred_ac(MpegEncContext * s, DCTELEM *block, int n, 
2418 
int dir)

2419 
{ 
2420 
int i;

2421 
int16_t *ac_val, *ac_val1; 
2422 
int8_t * const qscale_table= s>current_picture.qscale_table;

2423  
2424 
/* find prediction */

2425 
ac_val = s>ac_val[0][0] + s>block_index[n] * 16; 
2426 
ac_val1 = ac_val; 
2427 
if (s>ac_pred) {

2428 
if (dir == 0) { 
2429 
const int xy= s>mb_x1 + s>mb_y*s>mb_stride; 
2430 
/* left prediction */

2431 
ac_val = 16;

2432 

2433 
if(s>mb_x==0  s>qscale == qscale_table[xy]  n==1  n==3){ 
2434 
/* same qscale */

2435 
for(i=1;i<8;i++) { 
2436 
block[s>dsp.idct_permutation[i<<3]] += ac_val[i];

2437 
} 
2438 
}else{

2439 
/* different qscale, we must rescale */

2440 
for(i=1;i<8;i++) { 
2441 
block[s>dsp.idct_permutation[i<<3]] += ROUNDED_DIV(ac_val[i]*qscale_table[xy], s>qscale);

2442 
} 
2443 
} 
2444 
} else {

2445 
const int xy= s>mb_x + s>mb_y*s>mb_stride  s>mb_stride; 
2446 
/* top prediction */

2447 
ac_val = 16 * s>block_wrap[n];

2448  
2449 
if(s>mb_y==0  s>qscale == qscale_table[xy]  n==2  n==3){ 
2450 
/* same qscale */

2451 
for(i=1;i<8;i++) { 
2452 
block[s>dsp.idct_permutation[i]] += ac_val[i + 8];

2453 
} 
2454 
}else{

2455 
/* different qscale, we must rescale */

2456 
for(i=1;i<8;i++) { 
2457 
block[s>dsp.idct_permutation[i]] += ROUNDED_DIV(ac_val[i + 8]*qscale_table[xy], s>qscale);

2458 
} 
2459 
} 
2460 
} 
2461 
} 
2462 
/* left copy */

2463 
for(i=1;i<8;i++) 
2464 
ac_val1[i ] = block[s>dsp.idct_permutation[i<<3]];

2465  
2466 
/* top copy */

2467 
for(i=1;i<8;i++) 
2468 
ac_val1[8 + i] = block[s>dsp.idct_permutation[i ]];

2469  
2470 
} 
2471  
2472 
#ifdef CONFIG_ENCODERS

2473  
2474 
/**

2475 
* encodes the dc value.

2476 
* @param n block index (03 are luma, 45 are chroma)

2477 
*/

2478 
static inline void mpeg4_encode_dc(PutBitContext * s, int level, int n) 
2479 
{ 
2480 
#if 1 
2481 
// if(level<255  level>255) printf("dc overflow\n");

2482 
level+=256;

2483 
if (n < 4) { 
2484 
/* luminance */

2485 
put_bits(s, uni_DCtab_lum_len[level], uni_DCtab_lum_bits[level]); 
2486 
} else {

2487 
/* chrominance */

2488 
put_bits(s, uni_DCtab_chrom_len[level], uni_DCtab_chrom_bits[level]); 
2489 
} 
2490 
#else

2491 
int size, v;

2492 
/* find number of bits */

2493 
size = 0;

2494 
v = abs(level); 
2495 
while (v) {

2496 
v >>= 1;

2497 
size++; 
2498 
} 
2499  
2500 
if (n < 4) { 
2501 
/* luminance */

2502 
put_bits(&s>pb, DCtab_lum[size][1], DCtab_lum[size][0]); 
2503 
} else {

2504 
/* chrominance */

2505 
put_bits(&s>pb, DCtab_chrom[size][1], DCtab_chrom[size][0]); 
2506 
} 
2507  
2508 
/* encode remaining bits */

2509 
if (size > 0) { 
2510 
if (level < 0) 
2511 
level = (level) ^ ((1 << size)  1); 
2512 
put_bits(&s>pb, size, level); 
2513 
if (size > 8) 
2514 
put_bits(&s>pb, 1, 1); 
2515 
} 
2516 
#endif

2517 
} 
2518  
2519 
/**

2520 
* encodes a 8x8 block

2521 
* @param n block index (03 are luma, 45 are chroma)

2522 
*/

2523 
static inline void mpeg4_encode_block(MpegEncContext * s, DCTELEM * block, int n, int intra_dc, 
2524 
uint8_t *scan_table, PutBitContext *dc_pb, PutBitContext *ac_pb) 
2525 
{ 
2526 
int i, last_non_zero;

2527 
#if 0 //variables for the outcommented version

2528 
int code, sign, last;

2529 
#endif

2530 
const RLTable *rl;

2531 
uint32_t *bits_tab; 
2532 
uint8_t *len_tab; 
2533 
const int last_index = s>block_last_index[n]; 
2534  
2535 
if (s>mb_intra) { //Note gcc (3.2.1 at least) will optimize this away 
2536 
/* mpeg4 based DC predictor */

2537 
mpeg4_encode_dc(dc_pb, intra_dc, n); 
2538 
if(last_index<1) return; 
2539 
i = 1;

2540 
rl = &rl_intra; 
2541 
bits_tab= uni_mpeg4_intra_rl_bits; 
2542 
len_tab = uni_mpeg4_intra_rl_len; 
2543 
} else {

2544 
if(last_index<0) return; 
2545 
i = 0;

2546 
rl = &rl_inter; 
2547 
bits_tab= uni_mpeg4_inter_rl_bits; 
2548 
len_tab = uni_mpeg4_inter_rl_len; 
2549 
} 
2550  
2551 
/* AC coefs */

2552 
last_non_zero = i  1;

2553 
#if 1 
2554 
for (; i < last_index; i++) {

2555 
int level = block[ scan_table[i] ];

2556 
if (level) {

2557 
int run = i  last_non_zero  1; 
2558 
level+=64;

2559 
if((level&(~127)) == 0){ 
2560 
const int index= UNI_MPEG4_ENC_INDEX(0, run, level); 
2561 
put_bits(ac_pb, len_tab[index], bits_tab[index]); 
2562 
}else{ //ESC3 
2563 
put_bits(ac_pb, 7+2+1+6+1+12+1, (3<<23)+(3<<21)+(0<<20)+(run<<14)+(1<<13)+(((level64)&0xfff)<<1)+1); 
2564 
} 
2565 
last_non_zero = i; 
2566 
} 
2567 
} 
2568 
/*if(i<=last_index)*/{

2569 
int level = block[ scan_table[i] ];

2570 
int run = i  last_non_zero  1; 
2571 
level+=64;

2572 
if((level&(~127)) == 0){ 
2573 
const int index= UNI_MPEG4_ENC_INDEX(1, run, level); 
2574 
put_bits(ac_pb, len_tab[index], bits_tab[index]); 
2575 
}else{ //ESC3 
2576 
put_bits(ac_pb, 7+2+1+6+1+12+1, (3<<23)+(3<<21)+(1<<20)+(run<<14)+(1<<13)+(((level64)&0xfff)<<1)+1); 
2577 
} 
2578 
} 
2579 
#else

2580 
for (; i <= last_index; i++) {

2581 
const int slevel = block[ scan_table[i] ]; 
2582 
if (slevel) {

2583 
int level;

2584 
int run = i  last_non_zero  1; 
2585 
last = (i == last_index); 
2586 
sign = 0;

2587 
level = slevel; 
2588 
if (level < 0) { 
2589 
sign = 1;

2590 
level = level; 
2591 
} 
2592 
code = get_rl_index(rl, last, run, level); 
2593 
put_bits(ac_pb, rl>table_vlc[code][1], rl>table_vlc[code][0]); 
2594 
if (code == rl>n) {

2595 
int level1, run1;

2596 
level1 = level  rl>max_level[last][run]; 
2597 
if (level1 < 1) 
2598 
goto esc2;

2599 
code = get_rl_index(rl, last, run, level1); 
2600 
if (code == rl>n) {

2601 
esc2:

2602 
put_bits(ac_pb, 1, 1); 
2603 
if (level > MAX_LEVEL)

2604 
goto esc3;

2605 
run1 = run  rl>max_run[last][level]  1;

2606 
if (run1 < 0) 
2607 
goto esc3;

2608 
code = get_rl_index(rl, last, run1, level); 
2609 
if (code == rl>n) {

2610 
esc3:

2611 
/* third escape */

2612 
put_bits(ac_pb, 1, 1); 
2613 
put_bits(ac_pb, 1, last);

2614 
put_bits(ac_pb, 6, run);

2615 
put_bits(ac_pb, 1, 1); 
2616 
put_bits(ac_pb, 12, slevel & 0xfff); 
2617 
put_bits(ac_pb, 1, 1); 
2618 
} else {

2619 
/* second escape */

2620 
put_bits(ac_pb, 1, 0); 
2621 
put_bits(ac_pb, rl>table_vlc[code][1], rl>table_vlc[code][0]); 
2622 
put_bits(ac_pb, 1, sign);

2623 
} 
2624 
} else {

2625 
/* first escape */

2626 
put_bits(ac_pb, 1, 0); 
2627 
put_bits(ac_pb, rl>table_vlc[code][1], rl>table_vlc[code][0]); 
2628 
put_bits(ac_pb, 1, sign);

2629 
} 
2630 
} else {

2631 
put_bits(ac_pb, 1, sign);

2632 
} 
2633 
last_non_zero = i; 
2634 
} 
2635 
} 
2636 
#endif

2637 
} 
2638  
2639 
static inline int mpeg4_get_block_length(MpegEncContext * s, DCTELEM * block, int n, int intra_dc, 
2640 
uint8_t *scan_table) 
2641 
{ 
2642 
int i, last_non_zero;

2643 
const RLTable *rl;

2644 
uint8_t *len_tab; 
2645 
const int last_index = s>block_last_index[n]; 
2646 
int len=0; 
2647  
2648 
if (s>mb_intra) { //Note gcc (3.2.1 at least) will optimize this away 
2649 
/* mpeg4 based DC predictor */

2650 
//mpeg4_encode_dc(dc_pb, intra_dc, n); //FIXME

2651 
if(last_index<1) return len; 
2652 
i = 1;

2653 
rl = &rl_intra; 
2654 
len_tab = uni_mpeg4_intra_rl_len; 
2655 
} else {

2656 
if(last_index<0) return 0; 
2657 
i = 0;

2658 
rl = &rl_inter; 
2659 
len_tab = uni_mpeg4_inter_rl_len; 
2660 
} 
2661  
2662 
/* AC coefs */

2663 
last_non_zero = i  1;

2664 
for (; i < last_index; i++) {

2665 
int level = block[ scan_table[i] ];

2666 
if (level) {

2667 
int run = i  last_non_zero  1; 
2668 
level+=64;

2669 
if((level&(~127)) == 0){ 
2670 
const int index= UNI_MPEG4_ENC_INDEX(0, run, level); 
2671 
len += len_tab[index]; 
2672 
}else{ //ESC3 
2673 
len += 7+2+1+6+1+12+1; 
2674 
} 
2675 
last_non_zero = i; 
2676 
} 
2677 
} 
2678 
/*if(i<=last_index)*/{

2679 
int level = block[ scan_table[i] ];

2680 
int run = i  last_non_zero  1; 
2681 
level+=64;

2682 
if((level&(~127)) == 0){ 
2683 
const int index= UNI_MPEG4_ENC_INDEX(1, run, level); 
2684 
len += len_tab[index]; 
2685 
}else{ //ESC3 
2686 
len += 7+2+1+6+1+12+1; 
2687 
} 
2688 
} 
2689 

2690 
return len;

2691 
} 
2692  
2693 
#endif

2694  
2695  
2696 
/***********************************************/

2697 
/* decoding */

2698  
2699 
static VLC intra_MCBPC_vlc;

2700 
static VLC inter_MCBPC_vlc;

2701 
static VLC cbpy_vlc;

2702 
static VLC mv_vlc;

2703 
static VLC dc_lum, dc_chrom;

2704 
static VLC sprite_trajectory;

2705 
static VLC mb_type_b_vlc;

2706 
static VLC h263_mbtype_b_vlc;

2707 
static VLC cbpc_b_vlc;

2708  
2709 
void init_vlc_rl(RLTable *rl)

2710 
{ 
2711 
int i, q;

2712 

2713 
init_vlc(&rl>vlc, 9, rl>n + 1, 
2714 
&rl>table_vlc[0][1], 4, 2, 
2715 
&rl>table_vlc[0][0], 4, 2); 
2716  
2717 

2718 
for(q=0; q<32; q++){ 
2719 
int qmul= q*2; 
2720 
int qadd= (q1)1; 
2721 

2722 
if(q==0){ 
2723 
qmul=1;

2724 
qadd=0;

2725 
} 
2726 

2727 
rl>rl_vlc[q]= av_malloc(rl>vlc.table_size*sizeof(RL_VLC_ELEM));

2728 
for(i=0; i<rl>vlc.table_size; i++){ 
2729 
int code= rl>vlc.table[i][0]; 
2730 
int len = rl>vlc.table[i][1]; 
2731 
int level, run;

2732 

2733 
if(len==0){ // illegal code 
2734 
run= 66;

2735 
level= MAX_LEVEL; 
2736 
}else if(len<0){ //more bits needed 
2737 
run= 0;

2738 
level= code; 
2739 
}else{

2740 
if(code==rl>n){ //esc 
2741 
run= 66;

2742 
level= 0;

2743 
}else{

2744 
run= rl>table_run [code] + 1;

2745 
level= rl>table_level[code] * qmul + qadd; 
2746 
if(code >= rl>last) run+=192; 
2747 
} 
2748 
} 
2749 
rl>rl_vlc[q][i].len= len; 
2750 
rl>rl_vlc[q][i].level= level; 
2751 
rl>rl_vlc[q][i].run= run; 
2752 
} 
2753 
} 
2754 
} 
2755  
2756 
/* init vlcs */

2757  
2758 
/* XXX: find a better solution to handle static init */

2759 
void h263_decode_init_vlc(MpegEncContext *s)

2760 
{ 
2761 
static int done = 0; 
2762  
2763 
if (!done) {

2764 
done = 1;

2765  
2766 
init_vlc(&intra_MCBPC_vlc, INTRA_MCBPC_VLC_BITS, 9,

2767 
intra_MCBPC_bits, 1, 1, 
2768 
intra_MCBPC_code, 1, 1); 
2769 
init_vlc(&inter_MCBPC_vlc, INTER_MCBPC_VLC_BITS, 28,

2770 
inter_MCBPC_bits, 1, 1, 
2771 
inter_MCBPC_code, 1, 1); 
2772 
init_vlc(&cbpy_vlc, CBPY_VLC_BITS, 16,

2773 
&cbpy_tab[0][1], 2, 1, 
2774 
&cbpy_tab[0][0], 2, 1); 
2775 
init_vlc(&mv_vlc, MV_VLC_BITS, 33,

2776 
&mvtab[0][1], 2, 1, 
2777 
&mvtab[0][0], 2, 1); 
2778 
init_rl(&rl_inter); 
2779 
init_rl(&rl_intra); 
2780 
init_rl(&rvlc_rl_inter); 
2781 
init_rl(&rvlc_rl_intra); 
2782 
init_rl(&rl_intra_aic); 
2783 
init_vlc_rl(&rl_inter); 
2784 
init_vlc_rl(&rl_intra); 
2785 
init_vlc_rl(&rvlc_rl_inter); 
2786 
init_vlc_rl(&rvlc_rl_intra); 
2787 
init_vlc_rl(&rl_intra_aic); 
2788 
init_vlc(&dc_lum, DC_VLC_BITS, 10 /* 13 */, 
2789 
&DCtab_lum[0][1], 2, 1, 
2790 
&DCtab_lum[0][0], 2, 1); 
2791 
init_vlc(&dc_chrom, DC_VLC_BITS, 10 /* 13 */, 
2792 
&DCtab_chrom[0][1], 2, 1, 
2793 
&DCtab_chrom[0][0], 2, 1); 
2794 
init_vlc(&sprite_trajectory, SPRITE_TRAJ_VLC_BITS, 15,

2795 
&sprite_trajectory_tab[0][1], 4, 2, 
2796 
&sprite_trajectory_tab[0][0], 4, 2); 
2797 
init_vlc(&mb_type_b_vlc, MB_TYPE_B_VLC_BITS, 4,

2798 
&mb_type_b_tab[0][1], 2, 1, 
2799 
&mb_type_b_tab[0][0], 2, 1); 
2800 
init_vlc(&h263_mbtype_b_vlc, H263_MBTYPE_B_VLC_BITS, 15,

2801 
&h263_mbtype_b_tab[0][1], 2, 1, 
2802 
&h263_mbtype_b_tab[0][0], 2, 1); 
2803 
init_vlc(&cbpc_b_vlc, CBPC_B_VLC_BITS, 4,

2804 
&cbpc_b_tab[0][1], 2, 1, 
2805 
&cbpc_b_tab[0][0], 2, 1); 
2806 
} 
2807 
} 
2808  
2809 
/**

2810 
* Get the GOB height based on picture height.

2811 
*/

2812 
int ff_h263_get_gob_height(MpegEncContext *s){

2813 
if (s>height <= 400) 
2814 
return 1; 
2815 
else if (s>height <= 800) 
2816 
return 2; 
2817 
else

2818 
return 4; 
2819 
} 
2820  
2821 
int ff_h263_decode_mba(MpegEncContext *s)

2822 
{ 
2823 
int i, mb_pos;

2824  
2825 
for(i=0; i<6; i++){ 
2826 
if(s>mb_num1 <= ff_mba_max[i]) break; 
2827 
} 
2828 
mb_pos= get_bits(&s>gb, ff_mba_length[i]); 
2829 
s>mb_x= mb_pos % s>mb_width; 
2830 
s>mb_y= mb_pos / s>mb_width; 
2831  
2832 
return mb_pos;

2833 
} 
2834  
2835 
void ff_h263_encode_mba(MpegEncContext *s)

2836 
{ 
2837 
int i, mb_pos;

2838  
2839 
for(i=0; i<6; i++){ 
2840 
if(s>mb_num1 <= ff_mba_max[i]) break; 
2841 
} 
2842 
mb_pos= s>mb_x + s>mb_width*s>mb_y; 
2843 
put_bits(&s>pb, ff_mba_length[i], mb_pos); 
2844 
} 
2845  
2846 
/**

2847 
* decodes the group of blocks header or slice header.

2848 
* @return <0 if an error occured

2849 
*/

2850 
static int h263_decode_gob_header(MpegEncContext *s) 
2851 
{ 
2852 
unsigned int val, gfid, gob_number; 
2853 
int left;

2854 

2855 
/* Check for GOB Start Code */

2856 
val = show_bits(&s>gb, 16);

2857 
if(val)

2858 
return 1; 
2859  
2860 
/* We have a GBSC probably with GSTUFF */

2861 
skip_bits(&s>gb, 16); /* Drop the zeros */ 
2862 
left= s>gb.size_in_bits  get_bits_count(&s>gb); 
2863 
//MN: we must check the bits left or we might end in a infinite loop (or segfault)

2864 
for(;left>13; left){ 
2865 
if(get_bits1(&s>gb)) break; /* Seek the '1' bit */ 
2866 
} 
2867 
if(left<=13) 
2868 
return 1; 
2869  
2870 
if(s>h263_slice_structured){

2871 
if(get_bits1(&s>gb)==0) 
2872 
return 1; 
2873  
2874 
ff_h263_decode_mba(s); 
2875  
2876 
if(s>mb_num > 1583) 
2877 
if(get_bits1(&s>gb)==0) 
2878 
return 1; 
2879 

2880 
s>qscale = get_bits(&s>gb, 5); /* SQUANT */ 
2881 
if(get_bits1(&s>gb)==0) 
2882 
return 1; 
2883 
gfid = get_bits(&s>gb, 2); /* GFID */ 
2884 
}else{

2885 
gob_number = get_bits(&s>gb, 5); /* GN */ 
2886 
s>mb_x= 0;

2887 
s>mb_y= s>gob_index* gob_number; 
2888 
gfid = get_bits(&s>gb, 2); /* GFID */ 
2889 
s>qscale = get_bits(&s>gb, 5); /* GQUANT */ 
2890 
} 
2891 

2892 
if(s>mb_y >= s>mb_height)

2893 
return 1; 
2894  
2895 
if(s>qscale==0) 
2896 
return 1; 
2897  
2898 
return 0; 
2899 
} 
2900  
2901 
static inline void memsetw(short *tab, int val, int n) 
2902 
{ 
2903 
int i;

2904 
for(i=0;i<n;i++) 
2905 
tab[i] = val; 
2906 
} 
2907  
2908 
#ifdef CONFIG_ENCODERS

2909  
2910 
void ff_mpeg4_init_partitions(MpegEncContext *s)

2911 
{ 
2912 
init_put_bits(&s>tex_pb, s>tex_pb_buffer, PB_BUFFER_SIZE); 
2913 
init_put_bits(&s>pb2 , s>pb2_buffer , PB_BUFFER_SIZE); 
2914 
} 
2915  
2916 
void ff_mpeg4_merge_partitions(MpegEncContext *s)

2917 
{ 
2918 
const int pb2_len = get_bit_count(&s>pb2 ); 
2919 
const int tex_pb_len= get_bit_count(&s>tex_pb); 
2920 
const int bits= get_bit_count(&s>pb); 
2921  
2922 
if(s>pict_type==I_TYPE){

2923 
put_bits(&s>pb, 19, DC_MARKER);

2924 
s>misc_bits+=19 + pb2_len + bits  s>last_bits;

2925 
s>i_tex_bits+= tex_pb_len; 
2926 
}else{

2927 
put_bits(&s>pb, 17, MOTION_MARKER);

2928 
s>misc_bits+=17 + pb2_len;

2929 
s>mv_bits+= bits  s>last_bits; 
2930 
s>p_tex_bits+= tex_pb_len; 
2931 
} 
2932  
2933 
flush_put_bits(&s>pb2); 
2934 
flush_put_bits(&s>tex_pb); 
2935  
2936 
ff_copy_bits(&s>pb, s>pb2_buffer , pb2_len); 
2937 
ff_copy_bits(&s>pb, s>tex_pb_buffer, tex_pb_len); 
2938 
s>last_bits= get_bit_count(&s>pb); 
2939 
} 
2940  
2941 
#endif //CONFIG_ENCODERS 
2942  
2943 
int ff_mpeg4_get_video_packet_prefix_length(MpegEncContext *s){

2944 
switch(s>pict_type){

2945 
case I_TYPE:

2946 
return 16; 
2947 
case P_TYPE:

2948 
case S_TYPE:

2949 
return s>f_code+15; 
2950 
case B_TYPE:

2951 
return FFMAX(FFMAX(s>f_code, s>b_code)+15, 17); 
2952 
default:

2953 
return 1; 
2954 
} 
2955 
} 
2956  
2957 
#ifdef CONFIG_ENCODERS

2958  
2959 
void ff_mpeg4_encode_video_packet_header(MpegEncContext *s)

2960 
{ 
2961 
int mb_num_bits= av_log2(s>mb_num  1) + 1; 
2962  
2963 
put_bits(&s>pb, ff_mpeg4_get_video_packet_prefix_length(s), 0);

2964 
put_bits(&s>pb, 1, 1); 
2965 

2966 
put_bits(&s>pb, mb_num_bits, s>mb_x + s>mb_y*s>mb_width); 
2967 
put_bits(&s>pb, s>quant_precision, s>qscale); 
2968 
put_bits(&s>pb, 1, 0); /* no HEC */ 
2969 
} 
2970  
2971 
#endif //CONFIG_ENCODERS 
2972  
2973 
/**

2974 
* check if the next stuff is a resync marker or the end.

2975 
* @return 0 if not

2976 
*/

2977 
static inline int mpeg4_is_resync(MpegEncContext *s){ 
2978 
const int bits_count= get_bits_count(&s>gb); 
2979 

2980 
if(s>workaround_bugs&FF_BUG_NO_PADDING){

2981 
return 0; 
2982 
} 
2983  
2984 
if(bits_count + 8 >= s>gb.size_in_bits){ 
2985 
int v= show_bits(&s>gb, 8); 
2986 
v= 0x7F >> (7(bits_count&7)); 
2987 

2988 
if(v==0x7F) 
2989 
return 1; 
2990 
}else{

2991 
if(show_bits(&s>gb, 16) == ff_mpeg4_resync_prefix[bits_count&7]){ 
2992 
int len;

2993 
GetBitContext gb= s>gb; 
2994 

2995 
skip_bits(&s>gb, 1);

2996 
align_get_bits(&s>gb); 
2997 

2998 
for(len=0; len<32; len++){ 
2999 
if(get_bits1(&s>gb)) break; 
3000 
} 
3001  
3002 
s>gb= gb; 
3003  
3004 
if(len>=ff_mpeg4_get_video_packet_prefix_length(s))

3005 
return 1; 
3006 
} 
3007 
} 
3008 
return 0; 
3009 
} 
3010  
3011 
/**

3012 
* decodes the next video packet.

3013 
* @return <0 if something went wrong

3014 
*/

3015 
static int mpeg4_decode_video_packet_header(MpegEncContext *s) 
3016 
{ 
3017 
int mb_num_bits= av_log2(s>mb_num  1) + 1; 
3018 
int header_extension=0, mb_num, len; 
3019 

3020 
/* is there enough space left for a video packet + header */

3021 
if( get_bits_count(&s>gb) > s>gb.size_in_bits20) return 1; 
3022  
3023 
for(len=0; len<32; len++){ 
3024 
if(get_bits1(&s>gb)) break; 
3025 
} 
3026  
3027 
if(len!=ff_mpeg4_get_video_packet_prefix_length(s)){

3028 
av_log(s>avctx, AV_LOG_ERROR, "marker does not match f_code\n");

3029 
return 1; 
3030 
} 
3031 

3032 
if(s>shape != RECT_SHAPE){

3033 
header_extension= get_bits1(&s>gb); 
3034 
//FIXME more stuff here

3035 
} 
3036  
3037 
mb_num= get_bits(&s>gb, mb_num_bits); 
3038 
if(mb_num>=s>mb_num){

3039 
av_log(s>avctx, AV_LOG_ERROR, "illegal mb_num in video packet (%d %d) \n", mb_num, s>mb_num);

3040 
return 1; 
3041 
} 
3042 
if(s>pict_type == B_TYPE){

3043 
while(s>next_picture.mbskip_table[ s>mb_index2xy[ mb_num ] ]) mb_num++;

3044 
if(mb_num >= s>mb_num) return 1; // slice contains just skiped MBs which where allready decoded 
3045 
} 
3046 

3047 
s>mb_x= mb_num % s>mb_width; 
3048 
s>mb_y= mb_num / s>mb_width; 
3049  
3050 
if(s>shape != BIN_ONLY_SHAPE){

3051 
int qscale= get_bits(&s>gb, s>quant_precision);

3052 
if(qscale)

3053 
s>chroma_qscale=s>qscale= qscale; 
3054 
} 
3055  
3056 
if(s>shape == RECT_SHAPE){

3057 
header_extension= get_bits1(&s>gb); 
3058 
} 
3059 
if(header_extension){

3060 
int time_increment;

3061 
int time_incr=0; 
3062  
3063 
while (get_bits1(&s>gb) != 0) 
3064 
time_incr++; 
3065  
3066 
check_marker(&s>gb, "before time_increment in video packed header");

3067 
time_increment= get_bits(&s>gb, s>time_increment_bits); 
3068 
check_marker(&s>gb, "before vop_coding_type in video packed header");

3069 

3070 
skip_bits(&s>gb, 2); /* vop coding type */ 
3071 
//FIXME not rect stuff here

3072  
3073 
if(s>shape != BIN_ONLY_SHAPE){

3074 
skip_bits(&s>gb, 3); /* intra dc vlc threshold */ 
3075 
//FIXME dont just ignore everything

3076 
if(s>pict_type == S_TYPE && s>vol_sprite_usage==GMC_SPRITE){

3077 
mpeg4_decode_sprite_trajectory(s); 
3078 
av_log(s>avctx, AV_LOG_ERROR, "untested\n");

3079 
} 
3080  
3081 
//FIXME reduced res stuff here

3082 

3083 
if (s>pict_type != I_TYPE) {

3084 
int f_code = get_bits(&s>gb, 3); /* fcode_for */ 
3085 
if(f_code==0){ 
3086 
av_log(s>avctx, AV_LOG_ERROR, "Error, video packet header damaged (f_code=0)\n");

3087 
} 
3088 
} 
3089 
if (s>pict_type == B_TYPE) {

3090 
int b_code = get_bits(&s>gb, 3); 
3091 
if(b_code==0){ 
3092 
av_log(s>avctx, AV_LOG_ERROR, "Error, video packet header damaged (b_code=0)\n");

3093 
} 
3094 
} 
3095 
} 
3096 
} 
3097 
//FIXME newpred stuff

3098 

3099 
//printf("parse ok %d %d %d %d\n", mb_num, s>mb_x + s>mb_y*s>mb_width, get_bits_count(gb), get_bits_count(&s>gb));

3100  
3101 
return 0; 
3102 
} 
3103  
3104 
void ff_mpeg4_clean_buffers(MpegEncContext *s)

3105 
{ 
3106 
int c_wrap, c_xy, l_wrap, l_xy;

3107  
3108 
l_wrap= s>block_wrap[0];

3109 
l_xy= s>mb_y*l_wrap*2 + s>mb_x*2; 
3110 
c_wrap= s>block_wrap[4];

3111 
c_xy= s>mb_y*c_wrap + s>mb_x; 
3112  
3113 
#if 0

3114 
/* clean DC */

3115 
memsetw(s>dc_val[0] + l_xy, 1024, l_wrap*2+1);

3116 
memsetw(s>dc_val[1] + c_xy, 1024, c_wrap+1);

3117 
memsetw(s>dc_val[2] + c_xy, 1024, c_wrap+1);

3118 
#endif

3119  
3120 
/* clean AC */

3121 
memset(s>ac_val[0] + l_xy, 0, (l_wrap*2+1)*16*sizeof(int16_t)); 
3122 
memset(s>ac_val[1] + c_xy, 0, (c_wrap +1)*16*sizeof(int16_t)); 
3123 
memset(s>ac_val[2] + c_xy, 0, (c_wrap +1)*16*sizeof(int16_t)); 
3124  
3125 
/* clean MV */

3126 
// we cant clear the MVs as they might be needed by a b frame

3127 
// memset(s>motion_val + l_xy, 0, (l_wrap*2+1)*2*sizeof(int16_t));

3128 
// memset(s>motion_val, 0, 2*sizeof(int16_t)*(2 + s>mb_width*2)*(2 + s>mb_height*2));

3129 
s>last_mv[0][0][0]= 
3130 
s>last_mv[0][0][1]= 
3131 
s>last_mv[1][0][0]= 
3132 
s>last_mv[1][0][1]= 0; 
3133 
} 
3134  
3135 
/**

3136 
* decodes the group of blocks / video packet header.

3137 
* @return <0 if no resync found

3138 
*/

3139 
int ff_h263_resync(MpegEncContext *s){

3140 
int left, ret;

3141 

3142 
if(s>codec_id==CODEC_ID_MPEG4){

3143 
skip_bits1(&s>gb); 
3144 
align_get_bits(&s>gb); 
3145 
} 
3146  
3147 
//printf("checking next 16 %X\n", show_bits(&s>gb, 24));

3148 
if(show_bits(&s>gb, 16)==0){ 
3149 
if(s>codec_id==CODEC_ID_MPEG4)

3150 
ret= mpeg4_decode_video_packet_header(s); 
3151 
else

3152 
ret= h263_decode_gob_header(s); 
3153 
if(ret>=0) 
3154 
return 0; 
3155 
} 
3156 
//ok, its not where its supposed to be ...

3157 
s>gb= s>last_resync_gb; 
3158 
align_get_bits(&s>gb); 
3159 
printf("align %X\n", show_bits(&s>gb, 24)); 
3160 
left= s>gb.size_in_bits  get_bits_count(&s>gb); 
3161 

3162 
for(;left>16+1+5+5; left=8){ 
3163 
if(show_bits(&s>gb, 16)==0){ 
3164 
GetBitContext bak= s>gb; 
3165  
3166 
if(s>codec_id==CODEC_ID_MPEG4)

3167 
ret= mpeg4_decode_video_packet_header(s); 
3168 
else

3169 
ret= h263_decode_gob_header(s); 
3170 
if(ret>=0) 
3171 
return 0; 
3172  
3173 
s>gb= bak; 
3174 
} 
3175 
skip_bits(&s>gb, 8);

3176 
} 
3177 
printf("no resync\n");

3178 
return 1; 
3179 
} 
3180  
3181 
/**

3182 
* gets the average motion vector for a GMC MB.

3183 
* @param n either 0 for the x component or 1 for y

3184 
* @returns the average MV for a GMC MB

3185 
*/

3186 
static inline int get_amv(MpegEncContext *s, int n){ 
3187 
int x, y, mb_v, sum, dx, dy, shift;

3188 
int len = 1 << (s>f_code + 4); 
3189 
const int a= s>sprite_warping_accuracy; 
3190  
3191 
if(s>real_sprite_warping_points==1){ 
3192 
if(s>divx_version==500 && s>divx_build==413) 
3193 
sum= s>sprite_offset[0][n] / (1<<(a  s>quarter_sample)); 
3194 
else

3195 
sum= RSHIFT(s>sprite_offset[0][n]<<s>quarter_sample, a);

3196 
}else{

3197 
dx= s>sprite_delta[n][0];

3198 
dy= s>sprite_delta[n][1];

3199 
shift= s>sprite_shift[0];

3200 
if(n) dy = 1<<(shift + a + 1); 
3201 
else dx = 1<<(shift + a + 1); 
3202 
mb_v= s>sprite_offset[0][n] + dx*s>mb_x*16 + dy*s>mb_y*16; 
3203  
3204 
sum=0;

3205 
for(y=0; y<16; y++){ 
3206 
int v;

3207 

3208 
v= mb_v + dy*y; 
3209 
//XXX FIXME optimize

3210 
for(x=0; x<16; x++){ 
3211 
sum+= v>>shift; 
3212 
v+= dx; 
3213 
} 
3214 
} 
3215 
sum= RSHIFT(sum, a+8s>quarter_sample);

3216 
} 
3217  
3218 
if (sum < len) sum= len;

3219 
else if (sum >= len) sum= len1; 
3220  
3221 
return sum;

3222 
} 
3223  
3224 
/**

3225 
* decodes first partition.

3226 
* @return number of MBs decoded or <0 if an error occured

3227 
*/

3228 
static int mpeg4_decode_partition_a(MpegEncContext *s){ 
3229 
int mb_num;

3230 
static const int8_t quant_tab[4] = { 1, 2, 1, 2 }; 
3231 

3232 
/* decode first partition */

3233 
mb_num=0;

3234 
s>first_slice_line=1;

3235 
for(; s>mb_y<s>mb_height; s>mb_y++){

3236 
ff_init_block_index(s); 
3237 
for(; s>mb_x<s>mb_width; s>mb_x++){

3238 
const int xy= s>mb_x + s>mb_y*s>mb_stride; 
3239 
int cbpc;

3240 
int dir=0; 
3241 

3242 
mb_num++; 
3243 
ff_update_block_index(s); 
3244 
if(s>mb_x == s>resync_mb_x && s>mb_y == s>resync_mb_y+1) 
3245 
s>first_slice_line=0;

3246 

3247 
if(s>pict_type==I_TYPE){

3248 
int i;

3249  
3250 
if(show_bits_long(&s>gb, 19)==DC_MARKER){ 
3251 
return mb_num1; 
3252 
} 
3253  
3254 
do{

3255 
cbpc = get_vlc2(&s>gb, intra_MCBPC_vlc.table, INTRA_MCBPC_VLC_BITS, 2);

3256 
if (cbpc < 0){ 
3257 
av_log(s>avctx, AV_LOG_ERROR, "cbpc corrupted at %d %d\n", s>mb_x, s>mb_y);

3258 
return 1; 
3259 
} 
3260 
}while(cbpc == 8); 
3261  
3262 
s>cbp_table[xy]= cbpc & 3;

3263 
s>current_picture.mb_type[xy]= MB_TYPE_INTRA; 
3264 
s>mb_intra = 1;

3265  
3266 
if(cbpc & 4) { 
3267 
ff_set_qscale(s, s>qscale + quant_tab[get_bits(&s>gb, 2)]);

3268 
} 
3269 
s>current_picture.qscale_table[xy]= s>qscale; 
3270  
3271 
s>mbintra_table[xy]= 1;

3272 
for(i=0; i<6; i++){ 
3273 
int dc_pred_dir;

3274 
int dc= mpeg4_decode_dc(s, i, &dc_pred_dir);

3275 
if(dc < 0){ 
3276 
av_log(s>avctx, AV_LOG_ERROR, "DC corrupted at %d %d\n", s>mb_x, s>mb_y);

3277 
return 1; 
3278 
} 
3279 
dir<<=1;

3280 
if(dc_pred_dir) dir=1; 
3281 
} 
3282 
s>pred_dir_table[xy]= dir; 
3283 
}else{ /* P/S_TYPE */ 
3284 
int mx, my, pred_x, pred_y, bits;

3285 
int16_t * const mot_val= s>current_picture.motion_val[0][s>block_index[0]]; 
3286 
const int stride= s>block_wrap[0]*2; 
3287  
3288 
// do{ //FIXME

3289 
bits= show_bits(&s>gb, 17);

3290 
if(bits==MOTION_MARKER){

3291 
return mb_num1; 
3292 
} 
3293 
skip_bits1(&s>gb); 
3294 
if(bits&0x10000){ 
3295 
/* skip mb */

3296 
if(s>pict_type==S_TYPE && s>vol_sprite_usage==GMC_SPRITE){

3297 
s>current_picture.mb_type[xy]= MB_TYPE_SKIP  MB_TYPE_16x16  MB_TYPE_GMC  MB_TYPE_L0; 
3298 
mx= get_amv(s, 0);

3299 
my= get_amv(s, 1);

3300 
}else{

3301 
s>current_picture.mb_type[xy]= MB_TYPE_SKIP  MB_TYPE_16x16  MB_TYPE_L0; 
3302 
mx=my=0;

3303 
} 
3304 
mot_val[0 ]= mot_val[2 ]= 
3305 
mot_val[0+stride]= mot_val[2+stride]= mx; 
3306 
mot_val[1 ]= mot_val[3 ]= 
3307 
mot_val[1+stride]= mot_val[3+stride]= my; 
3308  
3309 
if(s>mbintra_table[xy])

3310 
ff_clean_intra_table_entries(s); 
3311 
continue;

3312 
} 
3313  
3314 
cbpc = get_vlc2(&s>gb, inter_MCBPC_vlc.table, INTER_MCBPC_VLC_BITS, 2);

3315 
if (cbpc < 0){ 
3316 
av_log(s>avctx, AV_LOG_ERROR, "cbpc corrupted at %d %d\n", s>mb_x, s>mb_y);

3317 
return 1; 
3318 
} 
3319 
// }while(cbpc == 20);

3320  
3321 
s>cbp_table[xy]= cbpc&(8+3); //8 is dquant 
3322 

3323 
s>mb_intra = ((cbpc & 4) != 0); 
3324 

3325 
if(s>mb_intra){

3326 
s>current_picture.mb_type[xy]= MB_TYPE_INTRA; 
3327 
s>mbintra_table[xy]= 1;

3328 
mot_val[0 ]= mot_val[2 ]= 
3329 
mot_val[0+stride]= mot_val[2+stride]= 0; 
3330 
mot_val[1 ]= mot_val[3 ]= 
3331 
mot_val[1+stride]= mot_val[3+stride]= 0; 
3332 
}else{

3333 
if(s>mbintra_table[xy])

3334 
ff_clean_intra_table_entries(s); 
3335  
3336 
if(s>pict_type==S_TYPE && s>vol_sprite_usage==GMC_SPRITE && (cbpc & 16) == 0) 
3337 
s>mcsel= get_bits1(&s>gb); 
3338 
else s>mcsel= 0; 
3339 

3340 
if ((cbpc & 16) == 0) { 
3341 
/* 16x16 motion prediction */

3342  
3343 
h263_pred_motion(s, 0, &pred_x, &pred_y);

3344 
if(!s>mcsel){

3345 
mx = h263_decode_motion(s, pred_x, s>f_code); 
3346 
if (mx >= 0xffff) 
3347 
return 1; 
3348  
3349 
my = h263_decode_motion(s, pred_y, s>f_code); 
3350 
if (my >= 0xffff) 
3351 
return 1; 
3352 
s>current_picture.mb_type[xy]= MB_TYPE_16x16  MB_TYPE_L0; 
3353 
} else {

3354 
mx = get_amv(s, 0);

3355 
my = get_amv(s, 1);

3356 
s>current_picture.mb_type[xy]= MB_TYPE_16x16  MB_TYPE_GMC  MB_TYPE_L0; 
3357 
} 
3358  
3359 
mot_val[0 ]= mot_val[2 ] = 
3360 
mot_val[0+stride]= mot_val[2+stride]= mx; 
3361 
mot_val[1 ]= mot_val[3 ]= 
3362 
mot_val[1+stride]= mot_val[3+stride]= my; 
3363 
} else {

3364 
int i;

3365 
s>current_picture.mb_type[xy]= MB_TYPE_8x8  MB_TYPE_L0; 
3366 
for(i=0;i<4;i++) { 
3367 
int16_t *mot_val= h263_pred_motion(s, i, &pred_x, &pred_y); 
3368 
mx = h263_decode_motion(s, pred_x, s>f_code); 
3369 
if (mx >= 0xffff) 
3370 
return 1; 
3371 

3372 
my = h263_decode_motion(s, pred_y, s>f_code); 
3373 
if (my >= 0xffff) 
3374 
return 1; 
3375 
mot_val[0] = mx;

3376 
mot_val[1] = my;

3377 
} 
3378 
} 
3379 
} 
3380 
} 
3381 
} 
3382 
s>mb_x= 0;

3383 
} 
3384  
3385 
return mb_num;

3386 
} 
3387  
3388 
/**

3389 
* decode second partition.

3390 
* @return <0 if an error occured

3391 
*/

3392 
static int mpeg4_decode_partition_b(MpegEncContext *s, int mb_count){ 
3393 
int mb_num=0; 
3394 
static const int8_t quant_tab[4] = { 1, 2, 1, 2 }; 
3395  
3396 
s>mb_x= s>resync_mb_x; 
3397 
s>first_slice_line=1;

3398 
for(s>mb_y= s>resync_mb_y; mb_num < mb_count; s>mb_y++){

3399 
ff_init_block_index(s); 
3400 
for(; mb_num < mb_count && s>mb_x<s>mb_width; s>mb_x++){

3401 
const int xy= s>mb_x + s>mb_y*s>mb_stride; 
3402  
3403 
mb_num++; 
3404 
ff_update_block_index(s); 
3405 
if(s>mb_x == s>resync_mb_x && s>mb_y == s>resync_mb_y+1) 
3406 
s>first_slice_line=0;

3407 

3408 
if(s>pict_type==I_TYPE){

3409 
int ac_pred= get_bits1(&s>gb);

3410 
int cbpy = get_vlc2(&s>gb, cbpy_vlc.table, CBPY_VLC_BITS, 1); 
3411 
if(cbpy<0){ 
3412 
av_log(s>avctx, AV_LOG_ERROR, "cbpy corrupted at %d %d\n", s>mb_x, s>mb_y);

3413 
return 1; 
3414 
} 
3415 

3416 
s>cbp_table[xy]= cbpy<<2;

3417 
s>current_picture.mb_type[xy] = ac_pred*MB_TYPE_ACPRED; 
3418 
}else{ /* P  S_TYPE */ 
3419 
if(IS_INTRA(s>current_picture.mb_type[xy])){

3420 
int dir=0,i; 
3421 
int ac_pred = get_bits1(&s>gb);

3422 
int cbpy = get_vlc2(&s>gb, cbpy_vlc.table, CBPY_VLC_BITS, 1); 
3423  
3424 
if(cbpy<0){ 
3425 
av_log(s>avctx, AV_LOG_ERROR, "I cbpy corrupted at %d %d\n", s>mb_x, s>mb_y);

3426 
return 1; 
3427 
} 
3428 

3429 
if(s>cbp_table[xy] & 8) { 
3430 
ff_set_qscale(s, s>qscale + quant_tab[get_bits(&s>gb, 2)]);

3431 
} 
3432 
s>current_picture.qscale_table[xy]= s>qscale; 
3433  
3434 
for(i=0; i<6; i++){ 
3435 
int dc_pred_dir;

3436 
int dc= mpeg4_decode_dc(s, i, &dc_pred_dir);

3437 
if(dc < 0){ 
3438 
av_log(s>avctx, AV_LOG_ERROR, "DC corrupted at %d %d\n", s>mb_x, s>mb_y);

3439 
return 1; 
3440 
} 
3441 
dir<<=1;

3442 
if(dc_pred_dir) dir=1; 
3443 
} 
3444 
s>cbp_table[xy]&= 3; //remove dquant 
3445 
s>cbp_table[xy]= cbpy<<2;

3446 
s>current_picture.mb_type[xy] = ac_pred*MB_TYPE_ACPRED; 
3447 
s>pred_dir_table[xy]= dir; 
3448 
}else if(IS_SKIP(s>current_picture.mb_type[xy])){ 
3449 
s>current_picture.qscale_table[xy]= s>qscale; 
3450 
s>cbp_table[xy]= 0;

3451 
}else{

3452 
int cbpy = get_vlc2(&s>gb, cbpy_vlc.table, CBPY_VLC_BITS, 1); 
3453  
3454 
if(cbpy<0){ 
3455 
av_log(s>avctx, AV_LOG_ERROR, "P cbpy corrupted at %d %d\n", s>mb_x, s>mb_y);

3456 
return 1; 
3457 
} 
3458 

3459 
if(s>cbp_table[xy] & 8) { 
3460 
ff_set_qscale(s, s>qscale + quant_tab[get_bits(&s>gb, 2)]);

3461 
} 
3462 
s>current_picture.qscale_table[xy]= s>qscale; 
3463  
3464 
s>cbp_table[xy]&= 3; //remove dquant 
3465 
s>cbp_table[xy]= (cbpy^0xf)<<2; 
3466 
} 
3467 
} 
3468 
} 
3469 
if(mb_num >= mb_count) return 0; 
3470 
s>mb_x= 0;

3471 
} 
3472 
return 0; 
3473 
} 
3474  
3475 
/**

3476 
* decodes the first & second partition

3477 
* @return <0 if error (and sets error type in the error_status_table)

3478 
*/

3479 
int ff_mpeg4_decode_partitions(MpegEncContext *s)

3480 
{ 
3481 
int mb_num;

3482 
const int part_a_error= s>pict_type==I_TYPE ? (DC_ERRORMV_ERROR) : MV_ERROR; 
3483 
const int part_a_end = s>pict_type==I_TYPE ? (DC_END MV_END) : MV_END; 
3484 

3485 
mb_num= mpeg4_decode_partition_a(s); 
3486 
if(mb_num<0){ 
3487 
ff_er_add_slice(s, s>resync_mb_x, s>resync_mb_y, s>mb_x, s>mb_y, part_a_error); 
3488 
return 1; 
3489 
} 
3490 

3491 
if(s>resync_mb_x + s>resync_mb_y*s>mb_width + mb_num > s>mb_num){

3492 
av_log(s>avctx, AV_LOG_ERROR, "slice below monitor ...\n");

3493 
ff_er_add_slice(s, s>resync_mb_x, s>resync_mb_y, s>mb_x, s>mb_y, part_a_error); 
3494 
return 1; 
3495 
} 
3496  
3497 
s>mb_num_left= mb_num; 
3498 

3499 
if(s>pict_type==I_TYPE){

3500 
if(get_bits_long(&s>gb, 19)!=DC_MARKER){ 
3501 
av_log(s>avctx, AV_LOG_ERROR, "marker missing after first I partition at %d %d\n", s>mb_x, s>mb_y);

3502 
return 1; 
3503 
} 
3504 
}else{

3505 
if(get_bits(&s>gb, 17)!=MOTION_MARKER){ 
3506 
av_log(s>avctx, AV_LOG_ERROR, "marker missing after first P partition at %d %d\n", s>mb_x, s>mb_y);

3507 
return 1; 
3508 
} 
3509 
} 
3510 
ff_er_add_slice(s, s>resync_mb_x, s>resync_mb_y, s>mb_x1, s>mb_y, part_a_end);

3511 

3512 
if( mpeg4_decode_partition_b(s, mb_num) < 0){ 
3513 
if(s>pict_type==P_TYPE)

3514 
ff_er_add_slice(s, s>resync_mb_x, s>resync_mb_y, s>mb_x, s>mb_y, DC_ERROR); 
3515 
return 1; 
3516 
}else{

3517 
if(s>pict_type==P_TYPE)

3518 
ff_er_add_slice(s, s>resync_mb_x, s>resync_mb_y, s>mb_x1, s>mb_y, DC_END);

3519 
} 
3520  
3521 
return 0; 
3522 
} 
3523  
3524 
/**

3525 
* decode partition C of one MB.

3526 
* @return <0 if an error occured

3527 
*/

3528 
static int mpeg4_decode_partitioned_mb(MpegEncContext *s, DCTELEM block[6][64]) 
3529 
{ 
3530 
int cbp, mb_type;

3531 
const int xy= s>mb_x + s>mb_y*s>mb_stride; 
3532  
3533 
mb_type= s>current_picture.mb_type[xy]; 
3534 
cbp = s>cbp_table[xy]; 
3535  
3536 
if(s>current_picture.qscale_table[xy] != s>qscale){

3537 
ff_set_qscale(s, s>current_picture.qscale_table[xy] ); 
3538 
} 
3539 

3540 
if (s>pict_type == P_TYPE  s>pict_type==S_TYPE) {

3541 
int i;

3542 
for(i=0; i<4; i++){ 
3543 
s>mv[0][i][0] = s>current_picture.motion_val[0][ s>block_index[i] ][0]; 
3544 
s>mv[0][i][1] = s>current_picture.motion_val[0][ s>block_index[i] ][1]; 
3545 
} 
3546 
s>mb_intra = IS_INTRA(mb_type); 
3547  
3548 
if (IS_SKIP(mb_type)) {

3549 
/* skip mb */

3550 
for(i=0;i<6;i++) 
3551 
s>block_last_index[i] = 1;

3552 
s>mv_dir = MV_DIR_FORWARD; 
3553 
s>mv_type = MV_TYPE_16X16; 
3554 
if(s>pict_type==S_TYPE && s>vol_sprite_usage==GMC_SPRITE){

3555 
s>mcsel=1;

3556 
s>mb_skiped = 0;

3557 
}else{

3558 
s>mcsel=0;

3559 
s>mb_skiped = 1;

3560 
} 
3561 
}else if(s>mb_intra){ 
3562 
s>ac_pred = IS_ACPRED(s>current_picture.mb_type[xy]); 
3563 
}else if(!s>mb_intra){ 
3564 
// s>mcsel= 0; //FIXME do we need to init that

3565 

3566 
s>mv_dir = MV_DIR_FORWARD; 
3567 
if (IS_8X8(mb_type)) {

3568 
s>mv_type = MV_TYPE_8X8; 
3569 
} else {

3570 
s>mv_type = MV_TYPE_16X16; 
3571 
} 
3572 
} 
3573 
} else { /* IFrame */ 
3574 
s>mb_intra = 1;

3575 
s>ac_pred = IS_ACPRED(s>current_picture.mb_type[xy]); 
3576 
} 
3577  
3578 
if (!IS_SKIP(mb_type)) {

3579 
int i;

3580 
/* decode each block */

3581 
for (i = 0; i < 6; i++) { 
3582 
if(mpeg4_decode_block(s, block[i], i, cbp&32, s>mb_intra, s>rvlc) < 0){ 
3583 
av_log(s>avctx, AV_LOG_ERROR, "texture corrupted at %d %d %d\n", s>mb_x, s>mb_y, s>mb_intra);

3584 
return 1; 
3585 
} 
3586 
cbp+=cbp; 
3587 
} 
3588 
} 
3589  
3590 
/* perMB end of slice check */

3591  
3592 
if(s>mb_num_left <= 0){ 
3593 
//printf("%06X %d\n", show_bits(&s>gb, 24), s>gb.size_in_bits  get_bits_count(&s>gb));

3594 
if(mpeg4_is_resync(s))

3595 
return SLICE_END;

3596 
else

3597 
return SLICE_NOEND;

3598 
}else{

3599 
if(mpeg4_is_resync(s)){

3600 
const int delta= s>mb_x + 1 == s>mb_width ? 2 : 1; 
3601 
if(s>cbp_table[xy+delta])

3602 
return SLICE_END;

3603 
} 
3604 
return SLICE_OK;

3605 
} 
3606 
} 
3607  
3608 
/**

3609 
* read the next MVs for OBMC. yes this is a ugly hack, feel free to send a patch :)

3610 
*/

3611 
static void preview_obmc(MpegEncContext *s){ 
3612 
GetBitContext gb= s>gb; 
3613 

3614 
int cbpc, i, pred_x, pred_y, mx, my;

3615 
int16_t *mot_val; 
3616 
const int xy= s>mb_x + 1 + s>mb_y * s>mb_stride; 
3617 
const int stride= s>block_wrap[0]*2; 
3618 

3619 
for(i=0; i<4; i++) 
3620 
s>block_index[i]+= 2;

3621 
for(i=4; i<6; i++) 
3622 
s>block_index[i]+= 1;

3623 
s>mb_x++; 
3624 

3625 
assert(s>pict_type == P_TYPE); 
3626  
3627 
do{

3628 
if (get_bits1(&s>gb)) {

3629 
/* skip mb */

3630 
mot_val = s>current_picture.motion_val[0][ s>block_index[0] ]; 
3631 
mot_val[0 ]= mot_val[2 ]= 
3632 
mot_val[0+stride]= mot_val[2+stride]= 0; 
3633 
mot_val[1 ]= mot_val[3 ]= 
3634 
mot_val[1+stride]= mot_val[3+stride]= 0; 
3635 

3636 
s>current_picture.mb_type[xy]= MB_TYPE_SKIP  MB_TYPE_16x16  MB_TYPE_L0; 
3637 
goto end;

3638 
} 
3639 
cbpc = get_vlc2(&s>gb, inter_MCBPC_vlc.table, INTER_MCBPC_VLC_BITS, 2);

3640 
}while(cbpc == 20); 
3641 

3642 
if(cbpc & 4){ 
3643 
s>current_picture.mb_type[xy]= MB_TYPE_INTRA; 
3644 
}else{

3645 
get_vlc2(&s>gb, cbpy_vlc.table, CBPY_VLC_BITS, 1);

3646 
if (cbpc & 8) { 
3647 
if(s>modified_quant){

3648 
if(get_bits1(&s>gb)) skip_bits(&s>gb, 1); 
3649 
else skip_bits(&s>gb, 5); 
3650 
}else

3651 
skip_bits(&s>gb, 2);

3652 
} 
3653 

3654 
if ((cbpc & 16) == 0) { 
3655 
s>current_picture.mb_type[xy]= MB_TYPE_16x16  MB_TYPE_L0; 
3656 
/* 16x16 motion prediction */

3657 
mot_val= h263_pred_motion(s, 0, &pred_x, &pred_y);

3658 
if (s>umvplus)

3659 
mx = h263p_decode_umotion(s, pred_x); 
3660 
else

3661 
mx = h263_decode_motion(s, pred_x, 1);

3662 

3663 
if (s>umvplus)

3664 
my = h263p_decode_umotion(s, pred_y); 
3665 
else

3666 
my = h263_decode_motion(s, pred_y, 1);

3667 

3668 
mot_val[0 ]= mot_val[2 ]= 
3669 
mot_val[0+stride]= mot_val[2+stride]= mx; 
3670 
mot_val[1 ]= mot_val[3 ]= 
3671 
mot_val[1+stride]= mot_val[3+stride]= my; 
3672 
} else {

3673 
s>current_picture.mb_type[xy]= MB_TYPE_8x8  MB_TYPE_L0; 
3674 
for(i=0;i<4;i++) { 
3675 
mot_val = h263_pred_motion(s, i, &pred_x, &pred_y); 
3676 
if (s>umvplus)

3677 
mx = h263p_decode_umotion(s, pred_x); 
3678 
else

3679 
mx = h263_decode_motion(s, pred_x, 1);

3680 

3681 
if (s>umvplus)

3682 
my = h263p_decode_umotion(s, pred_y); 
3683 
else

3684 
my = h263_decode_motion(s, pred_y, 1);

3685 
if (s>umvplus && (mx  pred_x) == 1 && (my  pred_y) == 1) 
3686 
skip_bits1(&s>gb); /* Bit stuffing to prevent PSC */

3687 
mot_val[0] = mx;

3688 
mot_val[1] = my;

3689 
} 
3690 
} 
3691 
} 
3692 
end:

3693 

3694 
for(i=0; i<4; i++) 
3695 
s>block_index[i]= 2;

3696 
for(i=4; i<6; i++) 
3697 
s>block_index[i]= 1;

3698 
s>mb_x; 
3699  
3700 
s>gb= gb; 
3701 
} 
3702  
3703 
static void h263_decode_dquant(MpegEncContext *s){ 
3704 
static const int8_t quant_tab[4] = { 1, 2, 1, 2 }; 
3705  
3706 
if(s>modified_quant){

3707 
if(get_bits1(&s>gb))

3708 
s>qscale= modified_quant_tab[get_bits1(&s>gb)][ s>qscale ]; 
3709 
else

3710 
s>qscale= get_bits(&s>gb, 5);

3711 
}else

3712 
s>qscale += quant_tab[get_bits(&s>gb, 2)];

3713 
ff_set_qscale(s, s>qscale); 
3714 
} 
3715  
3716 
int ff_h263_decode_mb(MpegEncContext *s,

3717 
DCTELEM block[6][64]) 
3718 
{ 
3719 
int cbpc, cbpy, i, cbp, pred_x, pred_y, mx, my, dquant;

3720 
int16_t *mot_val; 
3721 
const int xy= s>mb_x + s>mb_y * s>mb_stride; 
3722 

3723 
assert(!s>h263_pred); 
3724 

3725 
if (s>pict_type == P_TYPE) {

3726 
do{

3727 
if (get_bits1(&s>gb)) {

3728 
/* skip mb */

3729 
s>mb_intra = 0;

3730 
for(i=0;i<6;i++) 
3731 
s>block_last_index[i] = 1;

3732 
s>mv_dir = MV_DIR_FORWARD; 
3733 
s>mv_type = MV_TYPE_16X16; 
3734 
s>current_picture.mb_type[xy]= MB_TYPE_SKIP  MB_TYPE_16x16  MB_TYPE_L0; 
3735 
s>mv[0][0][0] = 0; 
3736 
s>mv[0][0][1] = 0; 
3737 
s>mb_skiped = !(s>obmc  s>loop_filter); 
3738 
goto end;

3739 
} 
3740 
cbpc = get_vlc2(&s>gb, inter_MCBPC_vlc.table, INTER_MCBPC_VLC_BITS, 2);

3741 
//fprintf(stderr, "\tCBPC: %d", cbpc);

3742 
if (cbpc < 0){ 
3743 
av_log(s>avctx, AV_LOG_ERROR, "cbpc damaged at %d %d\n", s>mb_x, s>mb_y);

3744 
return 1; 
3745 
} 
3746 
}while(cbpc == 20); 
3747 

3748 
dquant = cbpc & 8;

3749 
s>mb_intra = ((cbpc & 4) != 0); 
3750 
if (s>mb_intra) goto intra; 
3751 

3752 
cbpy = get_vlc2(&s>gb, cbpy_vlc.table, CBPY_VLC_BITS, 1);

3753 

3754 
if(s>alt_inter_vlc==0  (cbpc & 3)!=3) 
3755 
cbpy ^= 0xF;

3756 

3757 
cbp = (cbpc & 3)  (cbpy << 2); 
3758 
if (dquant) {

3759 
h263_decode_dquant(s); 
3760 
} 
3761 

3762 
s>mv_dir = MV_DIR_FORWARD; 
3763 
if ((cbpc & 16) == 0) { 
3764 
s>current_picture.mb_type[xy]= MB_TYPE_16x16  MB_TYPE_L0; 
3765 
/* 16x16 motion prediction */

3766 
s>mv_type = MV_TYPE_16X16; 
3767 
h263_pred_motion(s, 0, &pred_x, &pred_y);

3768 
if (s>umvplus)

3769 
mx = h263p_decode_umotion(s, pred_x); 
3770 
else

3771 
mx = h263_decode_motion(s, pred_x, 1);

3772 

3773 
if (mx >= 0xffff) 
3774 
return 1; 
3775 

3776 
if (s>umvplus)

3777 
my = h263p_decode_umotion(s, pred_y); 
3778 
else

3779 
my = h263_decode_motion(s, pred_y, 1);

3780 

3781 
if (my >= 0xffff) 
3782 
return 1; 
3783 
s>mv[0][0][0] = mx; 
3784 
s>mv[0][0][1] = my; 
3785  
3786 
if (s>umvplus && (mx  pred_x) == 1 && (my  pred_y) == 1) 
3787 
skip_bits1(&s>gb); /* Bit stuffing to prevent PSC */

3788 
} else {

3789 
s>current_picture.mb_type[xy]= MB_TYPE_8x8  MB_TYPE_L0; 
3790 
s>mv_type = MV_TYPE_8X8; 
3791 
for(i=0;i<4;i++) { 
3792 
mot_val = h263_pred_motion(s, i, &pred_x, &pred_y); 
3793 
if (s>umvplus)

3794 
mx = h263p_decode_umotion(s, pred_x); 
3795 
else

3796 
mx = h263_decode_motion(s, pred_x, 1);

3797 
if (mx >= 0xffff) 
3798 
return 1; 
3799 

3800 
if (s>umvplus)

3801 
my = h263p_decode_umotion(s, pred_y); 
3802 
else

3803 
my = h263_decode_motion(s, pred_y, 1);

3804 
if (my >= 0xffff) 
3805 
return 1; 
3806 
s>mv[0][i][0] = mx; 
3807 
s>mv[0][i][1] = my; 
3808 
if (s>umvplus && (mx  pred_x) == 1 && (my  pred_y) == 1) 
3809 
skip_bits1(&s>gb); /* Bit stuffing to prevent PSC */

3810 
mot_val[0] = mx;

3811 
mot_val[1] = my;

3812 
} 
3813 
} 
3814  
3815 
if(s>obmc){

3816 
if(s>pict_type == P_TYPE && s>mb_x+1<s>mb_width) 
3817 
preview_obmc(s); 
3818 
} 
3819 
} else if(s>pict_type==B_TYPE) { 
3820 
int mb_type;

3821 
const int stride= s>b8_stride; 
3822 
int16_t *mot_val0 = s>current_picture.motion_val[0][ s>mb_x + s>mb_y*stride ];

3823 
int16_t *mot_val1 = s>current_picture.motion_val[1][ s>mb_x + s>mb_y*stride ];

3824 
// const int mv_xy= s>mb_x + 1 + s>mb_y * s>mb_stride;

3825  
3826 
//FIXME ugly

3827 
mot_val0[0 ]= mot_val0[2 ]= mot_val0[0+stride]= mot_val0[2+stride]= 0; 
3828 
mot_val0[1 ]= mot_val0[3 ]= mot_val0[1+stride]= mot_val0[3+stride]= 0; 
3829 
mot_val1[0 ]= mot_val1[2 ]= mot_val1[0+stride]= mot_val1[2+stride]= 0; 
3830 
mot_val1[1 ]= mot_val1[3 ]= mot_val1[1+stride]= mot_val1[3+stride]= 0; 
3831  
3832 
do{

3833 
mb_type= get_vlc2(&s>gb, h263_mbtype_b_vlc.table, H263_MBTYPE_B_VLC_BITS, 2);

3834 
if (mb_type < 0){ 
3835 
av_log(s>avctx, AV_LOG_ERROR, "b mb_type damaged at %d %d\n", s>mb_x, s>mb_y);

3836 
return 1; 
3837 
} 
3838  
3839 
mb_type= h263_mb_type_b_map[ mb_type ]; 
3840 
}while(!mb_type);

3841  
3842 
s>mb_intra = IS_INTRA(mb_type); 
3843 
if(HAS_CBP(mb_type)){

3844 
cbpc = get_vlc2(&s>gb, cbpc_b_vlc.table, CBPC_B_VLC_BITS, 1);

3845 
if(s>mb_intra){

3846 
dquant = IS_QUANT(mb_type); 
3847 
goto intra;

3848 
} 
3849  
3850 
cbpy = get_vlc2(&s>gb, cbpy_vlc.table, CBPY_VLC_BITS, 1);

3851  
3852 
if (cbpy < 0){ 
3853 
av_log(s>avctx, AV_LOG_ERROR, "b cbpy damaged at %d %d\n", s>mb_x, s>mb_y);

3854 
return 1; 
3855 
} 
3856 

3857 
if(s>alt_inter_vlc==0  (cbpc & 3)!=3) 
3858 
cbpy ^= 0xF;

3859 

3860 
cbp = (cbpc & 3)  (cbpy << 2); 
3861 
}else

3862 
cbp=0;

3863 

3864 
assert(!s>mb_intra); 
3865  
3866 
if(IS_QUANT(mb_type)){

3867 
h263_decode_dquant(s); 
3868 
} 
3869  
3870 
if(IS_DIRECT(mb_type)){

3871 
s>mv_dir = MV_DIR_FORWARD  MV_DIR_BACKWARD  MV_DIRECT; 
3872 
mb_type = ff_mpeg4_set_direct_mv(s, 0, 0); 
3873 
}else{

3874 
s>mv_dir = 0;

3875 
s>mv_type= MV_TYPE_16X16; 
3876 
//FIXME UMV

3877  
3878 
if(USES_LIST(mb_type, 0)){ 
3879 
int16_t *mot_val= h263_pred_motion2(s, 0, 0, &mx, &my); 
3880 
s>mv_dir = MV_DIR_FORWARD; 
3881  
3882 
mx = h263_decode_motion(s, mx, 1);

3883 
my = h263_decode_motion(s, my, 1);

3884 
s>mv[0][0][0] = mx; 
3885 
s>mv[0][0][1] = my; 
3886 
mot_val[0 ]= mot_val[2 ]= mot_val[0+stride]= mot_val[2+stride]= mx; 
3887 
mot_val[1 ]= mot_val[3 ]= mot_val[1+stride]= mot_val[3+stride]= my; 
3888 
} 
3889 

3890 
if(USES_LIST(mb_type, 1)){ 
3891 
int16_t *mot_val= h263_pred_motion2(s, 0, 1, &mx, &my); 
3892 
s>mv_dir = MV_DIR_BACKWARD; 
3893  
3894 
mx = h263_decode_motion(s, mx, 1);

3895 
my = h263_decode_motion(s, my, 1);

3896 
s>mv[1][0][0] = mx; 
3897 
s>mv[1][0][1] = my; 
3898 
mot_val[0 ]= mot_val[2 ]= mot_val[0+stride]= mot_val[2+stride]= mx; 
3899 
mot_val[1 ]= mot_val[3 ]= mot_val[1+stride]= mot_val[3+stride]= my; 
3900 
} 
3901 
} 
3902 

3903 
s>current_picture.mb_type[xy]= mb_type; 
3904 
} else { /* IFrame */ 
3905 
do{

3906 
cbpc = get_vlc2(&s>gb, intra_MCBPC_vlc.table, INTRA_MCBPC_VLC_BITS, 2);

3907 
if (cbpc < 0){ 
3908 
av_log(s>avctx, AV_LOG_ERROR, "I cbpc damaged at %d %d\n", s>mb_x, s>mb_y);

3909 
return 1; 
3910 
} 
3911 
}while(cbpc == 8); 
3912  
3913 
dquant = cbpc & 4;

3914 
s>mb_intra = 1;

3915 
intra:

3916 
s>current_picture.mb_type[xy]= MB_TYPE_INTRA; 
3917 
if (s>h263_aic) {

3918 
s>ac_pred = get_bits1(&s>gb); 
3919 
if(s>ac_pred){

3920 
s>current_picture.mb_type[xy]= MB_TYPE_INTRA  MB_TYPE_ACPRED; 
3921 

3922 
s>h263_aic_dir = get_bits1(&s>gb); 
3923 
} 
3924 
}else

3925 
s>ac_pred = 0;

3926 

3927 
cbpy = get_vlc2(&s>gb, cbpy_vlc.table, CBPY_VLC_BITS, 1);

3928 
if(cbpy<0){ 
3929 
av_log(s>avctx, AV_LOG_ERROR, "I cbpy damaged at %d %d\n", s>mb_x, s>mb_y);

3930 
return 1; 
3931 
} 
3932 
cbp = (cbpc & 3)  (cbpy << 2); 
3933 
if (dquant) {

3934 
h263_decode_dquant(s); 
3935 
} 
3936 
} 
3937  
3938 
/* decode each block */

3939 
for (i = 0; i < 6; i++) { 
3940 
if (h263_decode_block(s, block[i], i, cbp&32) < 0) 
3941 
return 1; 
3942 
cbp+=cbp; 
3943 
} 
3944 
end:

3945  
3946 
/* perMB end of slice check */

3947 
{ 
3948 
int v= show_bits(&s>gb, 16); 
3949 

3950 
if(get_bits_count(&s>gb) + 16 > s>gb.size_in_bits){ 
3951 
v>>= get_bits_count(&s>gb) + 16  s>gb.size_in_bits;

3952 
} 
3953  
3954 
if(v==0) 
3955 
return SLICE_END;

3956 
} 
3957  
3958 
return SLICE_OK;

3959 
} 
3960  
3961 
int ff_mpeg4_decode_mb(MpegEncContext *s,

3962 
DCTELEM block[6][64]) 
3963 
{ 
3964 
int cbpc, cbpy, i, cbp, pred_x, pred_y, mx, my, dquant;

3965 
int16_t *mot_val; 
3966 
static int8_t quant_tab[4] = { 1, 2, 1, 2 }; 
3967 
const int xy= s>mb_x + s>mb_y * s>mb_stride; 
3968 

3969 
assert(s>h263_pred); 
3970 

3971 
if (s>pict_type == P_TYPE  s>pict_type==S_TYPE) {

3972 
do{

3973 
if (get_bits1(&s>gb)) {

3974 
/* skip mb */

3975 
s>mb_intra = 0;

3976 
for(i=0;i<6;i++) 
3977 
s>block_last_index[i] = 1;

3978 
s>mv_dir = MV_DIR_FORWARD; 
3979 
s>mv_type = MV_TYPE_16X16; 
3980 
if(s>pict_type==S_TYPE && s>vol_sprite_usage==GMC_SPRITE){

3981 
s>current_picture.mb_type[xy]= MB_TYPE_SKIP  MB_TYPE_GMC  MB_TYPE_16x16  MB_TYPE_L0; 
3982 
s>mcsel=1;

3983 
s>mv[0][0][0]= get_amv(s, 0); 
3984 
s>mv[0][0][1]= get_amv(s, 1); 
3985  
3986 
s>mb_skiped = 0;

3987 
}else{

3988 
s>current_picture.mb_type[xy]= MB_TYPE_SKIP  MB_TYPE_16x16  MB_TYPE_L0; 
3989 
s>mcsel=0;

3990 
s>mv[0][0][0] = 0; 
3991 
s>mv[0][0][1] = 0; 
3992 
s>mb_skiped = 1;

3993 
} 
3994 
goto end;

3995 
} 
3996 
cbpc = get_vlc2(&s>gb, inter_MCBPC_vlc.table, INTER_MCBPC_VLC_BITS, 2);

3997 
//fprintf(stderr, "\tCBPC: %d", cbpc);

3998 
if (cbpc < 0){ 
3999 
av_log(s>avctx, AV_LOG_ERROR, "cbpc damaged at %d %d\n", s>mb_x, s>mb_y);

4000 
return 1; 
4001 
} 
4002 
}while(cbpc == 20); 
4003 

4004 
dquant = cbpc & 8;

4005 
s>mb_intra = ((cbpc & 4) != 0); 
4006 
if (s>mb_intra) goto intra; 
4007 

4008 
if(s>pict_type==S_TYPE && s>vol_sprite_usage==GMC_SPRITE && (cbpc & 16) == 0) 
4009 
s>mcsel= get_bits1(&s>gb); 
4010 
else s>mcsel= 0; 
4011 
cbpy = get_vlc2(&s>gb, cbpy_vlc.table, CBPY_VLC_BITS, 1) ^ 0x0F; 
4012 

4013 
cbp = (cbpc & 3)  (cbpy << 2); 
4014 
if (dquant) {

4015 
ff_set_qscale(s, s>qscale + quant_tab[get_bits(&s>gb, 2)]);

4016 
} 
4017 
if((!s>progressive_sequence) && (cbp  (s>workaround_bugs&FF_BUG_XVID_ILACE)))

4018 
s>interlaced_dct= get_bits1(&s>gb); 
4019 

4020 
s>mv_dir = MV_DIR_FORWARD; 
4021 
if ((cbpc & 16) == 0) { 
4022 
if(s>mcsel){

4023 
s>current_picture.mb_type[xy]= MB_TYPE_GMC  MB_TYPE_16x16  MB_TYPE_L0; 
4024 
/* 16x16 global motion prediction */

4025 
s>mv_type = MV_TYPE_16X16; 
4026 
mx= get_amv(s, 0);

4027 
my= get_amv(s, 1);

4028 
s>mv[0][0][0] = mx; 
4029 
s>mv[0][0][1] = my; 
4030 
}else if((!s>progressive_sequence) && get_bits1(&s>gb)){ 
4031 
s>current_picture.mb_type[xy]= MB_TYPE_16x8  MB_TYPE_L0  MB_TYPE_INTERLACED; 
4032 
/* 16x8 field motion prediction */

4033 
s>mv_type= MV_TYPE_FIELD; 
4034  
4035 
s>field_select[0][0]= get_bits1(&s>gb); 
4036 
s>field_select[0][1]= get_bits1(&s>gb); 
4037  
4038 
h263_pred_motion(s, 0, &pred_x, &pred_y);

4039 

4040 
for(i=0; i<2; i++){ 
4041 
mx = h263_decode_motion(s, pred_x, s>f_code); 
4042 
if (mx >= 0xffff) 
4043 
return 1; 
4044 

4045 
my = h263_decode_motion(s, pred_y/2, s>f_code);

4046 
if (my >= 0xffff) 
4047 
return 1; 
4048  
4049 
s>mv[0][i][0] = mx; 
4050 
s>mv[0][i][1] = my; 
4051 
} 
4052 
}else{

4053 
s>current_picture.mb_type[xy]= MB_TYPE_16x16  MB_TYPE_L0; 
4054 
/* 16x16 motion prediction */

4055 
s>mv_type = MV_TYPE_16X16; 
4056 
h263_pred_motion(s, 0, &pred_x, &pred_y);

4057 
mx = h263_decode_motion(s, pred_x, s>f_code); 
4058 

4059 
if (mx >= 0xffff) 
4060 
return 1; 
4061 

4062 
my = h263_decode_motion(s, pred_y, s>f_code); 
4063 

4064 
if (my >= 0xffff) 
4065 
return 1; 
4066 
s>mv[0][0][0] = mx; 
4067 
s>mv[0][0][1] = my; 
4068 
} 
4069 
} else {

4070 
s>current_picture.mb_type[xy]= MB_TYPE_8x8  MB_TYPE_L0; 
4071 
s>mv_type = MV_TYPE_8X8; 
4072 
for(i=0;i<4;i++) { 
4073 
mot_val = h263_pred_motion(s, i, &pred_x, &pred_y); 
4074 
mx = h263_decode_motion(s, pred_x, s>f_code); 
4075 
if (mx >= 0xffff) 
4076 
return 1; 
4077 

4078 
my = h263_decode_motion(s, pred_y, s>f_code); 
4079 
if (my >= 0xffff) 
4080 
return 1; 
4081 
s>mv[0][i][0] = mx; 
4082 
s>mv[0][i][1] = my; 
4083 
mot_val[0] = mx;

4084 
mot_val[1] = my;

4085 
} 
4086 
} 
4087 
} else if(s>pict_type==B_TYPE) { 
4088 
int modb1; // first bit of modb 
4089 
int modb2; // second bit of modb 
4090 
int mb_type;

4091  
4092 
s>mb_intra = 0; //Bframes never contain intra blocks 
4093 
s>mcsel=0; // ... true gmc blocks 
4094  
4095 
if(s>mb_x==0){ 
4096 
for(i=0; i<2; i++){ 
4097 
s>last_mv[i][0][0]= 
4098 
s>last_mv[i][0][1]= 
4099 
s>last_mv[i][1][0]= 
4100 
s>last_mv[i][1][1]= 0; 
4101 
} 
4102 
} 
4103  
4104 
/* if we skipped it in the future P Frame than skip it now too */

4105 
s>mb_skiped= s>next_picture.mbskip_table[s>mb_y * s>mb_stride + s>mb_x]; // Note, skiptab=0 if last was GMC

4106  
4107 
if(s>mb_skiped){

4108 
/* skip mb */

4109 
for(i=0;i<6;i++) 
4110 
s>block_last_index[i] = 1;

4111  
4112 
s>mv_dir = MV_DIR_FORWARD; 
4113 
s>mv_type = MV_TYPE_16X16; 
4114 
s>mv[0][0][0] = 0; 
4115 
s>mv[0][0][1] = 0; 
4116 
s>mv[1][0][0] = 0; 
4117 
s>mv[1][0][1] = 0; 
4118 
s>current_picture.mb_type[xy]= MB_TYPE_SKIP  MB_TYPE_16x16  MB_TYPE_L0; 
4119 
goto end;

4120 
} 
4121  
4122 
modb1= get_bits1(&s>gb); 
4123 
if(modb1){

4124 
mb_type= MB_TYPE_DIRECT2  MB_TYPE_SKIP  MB_TYPE_L0L1; //like MB_TYPE_B_DIRECT but no vectors coded

4125 
cbp=0;

4126 
}else{

4127 
modb2= get_bits1(&s>gb); 
4128 
mb_type= get_vlc2(&s>gb, mb_type_b_vlc.table, MB_TYPE_B_VLC_BITS, 1);

4129 
if(mb_type<0){ 
4130 
av_log(s>avctx, AV_LOG_ERROR, "illegal MB_type\n");

4131 
return 1; 
4132 
} 
4133 
mb_type= mb_type_b_map[ mb_type ]; 
4134 
if(modb2) cbp= 0; 
4135 
else cbp= get_bits(&s>gb, 6); 
4136  
4137 
if ((!IS_DIRECT(mb_type)) && cbp) {

4138 
if(get_bits1(&s>gb)){

4139 
ff_set_qscale(s, s>qscale + get_bits1(&s>gb)*4  2); 
4140 
} 
4141 
} 
4142  
4143 
if(!s>progressive_sequence){

4144 
if(cbp)

4145 
s>interlaced_dct= get_bits1(&s>gb); 
4146  
4147 
if(!IS_DIRECT(mb_type) && get_bits1(&s>gb)){

4148 
mb_type = MB_TYPE_16x8  MB_TYPE_INTERLACED; 
4149 
mb_type &= ~MB_TYPE_16x16; 
4150  
4151 
if(USES_LIST(mb_type, 0)){ 
4152 
s>field_select[0][0]= get_bits1(&s>gb); 
4153 
s>field_select[0][1]= get_bits1(&s>gb); 
4154 
} 
4155 
if(USES_LIST(mb_type, 1)){ 
4156 
s>field_select[1][0]= get_bits1(&s>gb); 
4157 
s>field_select[1][1]= get_bits1(&s>gb); 
4158 
} 
4159 
} 
4160 
} 
4161  
4162 
s>mv_dir = 0;

4163 
if((mb_type & (MB_TYPE_DIRECT2MB_TYPE_INTERLACED)) == 0){ 
4164 
s>mv_type= MV_TYPE_16X16; 
4165  
4166 
if(USES_LIST(mb_type, 0)){ 
4167 
s>mv_dir = MV_DIR_FORWARD; 
4168  
4169 
mx = h263_decode_motion(s, s>last_mv[0][0][0], s>f_code); 
4170 
my = h263_decode_motion(s, s>last_mv[0][0][1], s>f_code); 
4171 
s>last_mv[0][1][0]= s>last_mv[0][0][0]= s>mv[0][0][0] = mx; 
4172 
s>last_mv[0][1][1]= s>last_mv[0][0][1]= s>mv[0][0][1] = my; 
4173 
} 
4174 

4175 
if(USES_LIST(mb_type, 1)){ 
4176 
s>mv_dir = MV_DIR_BACKWARD; 
4177  
4178 
mx = h263_decode_motion(s, s>last_mv[1][0][0], s>b_code); 
4179 
my = h263_decode_motion(s, s>last_mv[1][0][1], s>b_code); 
4180 
s>last_mv[1][1][0]= s>last_mv[1][0][0]= s>mv[1][0][0] = mx; 
4181 
s>last_mv[1][1][1]= s>last_mv[1][0][1]= s>mv[1][0][1] = my; 
4182 
} 
4183 
}else if(!IS_DIRECT(mb_type)){ 
4184 
s>mv_type= MV_TYPE_FIELD; 
4185  
4186 
if(USES_LIST(mb_type, 0)){ 
4187 
s>mv_dir = MV_DIR_FORWARD; 
4188 

4189 
for(i=0; i<2; i++){ 
4190 
mx = h263_decode_motion(s, s>last_mv[0][i][0] , s>f_code); 
4191 
my = h263_decode_motion(s, s>last_mv[0][i][1]/2, s>f_code); 
4192 
s>last_mv[0][i][0]= s>mv[0][i][0] = mx; 
4193 
s>last_mv[0][i][1]= (s>mv[0][i][1] = my)*2; 
4194 
} 
4195 
} 
4196 

4197 
if(USES_LIST(mb_type, 1)){ 
4198 
s>mv_dir = MV_DIR_BACKWARD; 
4199  
4200 
for(i=0; i<2; i++){ 
4201 
mx = h263_decode_motion(s, s>last_mv[1][i][0] , s>b_code); 
4202 
my = h263_decode_motion(s, s>last_mv[1][i][1]/2, s>b_code); 
4203 
s>last_mv[1][i][0]= s>mv[1][i][0] = mx; 
4204 
s>last_mv[1][i][1]= (s>mv[1][i][1] = my)*2; 
4205 
} 
4206 
} 
4207 
} 
4208 
} 
4209 

4210 
if(IS_DIRECT(mb_type)){

4211 
if(IS_SKIP(mb_type))

4212 
mx=my=0;

4213 
else{

4214 
mx = h263_decode_motion(s, 0, 1); 
4215 
my = h263_decode_motion(s, 0, 1); 
4216 
} 
4217 

4218 
s>mv_dir = MV_DIR_FORWARD  MV_DIR_BACKWARD  MV_DIRECT; 
4219 
mb_type = ff_mpeg4_set_direct_mv(s, mx, my); 
4220 
} 
4221 
s>current_picture.mb_type[xy]= mb_type; 
4222 
} else { /* IFrame */ 
4223 
do{

4224 
cbpc = get_vlc2(&s>gb, intra_MCBPC_vlc.table, INTRA_MCBPC_VLC_BITS, 2);

4225 
if (cbpc < 0){ 
4226 
av_log(s>avctx, AV_LOG_ERROR, "I cbpc damaged at %d %d\n", s>mb_x, s>mb_y);

4227 
return 1; 
4228 
} 
4229 
}while(cbpc == 8); 
4230  
4231 
dquant = cbpc & 4;

4232 
s>mb_intra = 1;

4233 
intra:

4234 
s>ac_pred = get_bits1(&s>gb); 
4235 
if(s>ac_pred)

4236 
s>current_picture.mb_type[xy]= MB_TYPE_INTRA  MB_TYPE_ACPRED; 
4237 
else

4238 
s>current_picture.mb_type[xy]= MB_TYPE_INTRA; 
4239 

4240 
cbpy = get_vlc2(&s>gb, cbpy_vlc.table, CBPY_VLC_BITS, 1);

4241 
if(cbpy<0){ 
4242 
av_log(s>avctx, AV_LOG_ERROR, "I cbpy damaged at %d %d\n", s>mb_x, s>mb_y);

4243 
return 1; 
4244 
} 
4245 
cbp = (cbpc & 3)  (cbpy << 2); 
4246 
if (dquant) {

4247 
ff_set_qscale(s, s>qscale + quant_tab[get_bits(&s>gb, 2)]);

4248 
} 
4249 

4250 
if(!s>progressive_sequence)

4251 
s>interlaced_dct= get_bits1(&s>gb); 
4252  
4253 
/* decode each block */

4254 
for (i = 0; i < 6; i++) { 
4255 
if (mpeg4_decode_block(s, block[i], i, cbp&32, 1, 0) < 0) 
4256 
return 1; 
4257 
cbp+=cbp; 
4258 
} 
4259 
goto end;

4260 
} 
4261  
4262 
/* decode each block */

4263 
for (i = 0; i < 6; i++) { 
4264 
if (mpeg4_decode_block(s, block[i], i, cbp&32, 0, 0) < 0) 
4265 
return 1; 
4266 
cbp+=cbp; 
4267 
} 
4268 
end:

4269  
4270 
/* perMB end of slice check */

4271 
if(s>codec_id==CODEC_ID_MPEG4){

4272 
if(mpeg4_is_resync(s)){

4273 
const int delta= s>mb_x + 1 == s>mb_width ? 2 : 1; 
4274 
if(s>pict_type==B_TYPE && s>next_picture.mbskip_table[xy + delta])

4275 
return SLICE_OK;

4276 
return SLICE_END;

4277 
} 
4278 
} 
4279  
4280 
return SLICE_OK;

4281 
} 
4282  
4283 
static int h263_decode_motion(MpegEncContext * s, int pred, int f_code) 
4284 
{ 
4285 
int code, val, sign, shift, l;

4286 
code = get_vlc2(&s>gb, mv_vlc.table, MV_VLC_BITS, 2);

4287  
4288 
if (code == 0) 
4289 
return pred;

4290 
if (code < 0) 
4291 
return 0xffff; 
4292  
4293 
sign = get_bits1(&s>gb); 
4294 
shift = f_code  1;

4295 
val = code; 
4296 
if (shift) {

4297 
val = (val  1) << shift;

4298 
val = get_bits(&s>gb, shift); 
4299 
val++; 
4300 
} 
4301 
if (sign)

4302 
val = val; 
4303 
val += pred; 
4304  
4305 
/* modulo decoding */

4306 
if (!s>h263_long_vectors) {

4307 
l = 1 << (f_code + 4); 
4308 
val = ((val + l)&(l*21))  l; 
4309 
} else {

4310 
/* horrible h263 long vector mode */

4311 
if (pred < 31 && val < 63) 
4312 
val += 64;

4313 
if (pred > 32 && val > 63) 
4314 
val = 64;

4315 

4316 
} 
4317 
return val;

4318 
} 
4319  
4320 
/* Decodes RVLC of H.263+ UMV */

4321 
static int h263p_decode_umotion(MpegEncContext * s, int pred) 
4322 
{ 
4323 
int code = 0, sign; 
4324 

4325 
if (get_bits1(&s>gb)) /* Motion difference = 0 */ 
4326 
return pred;

4327 

4328 
code = 2 + get_bits1(&s>gb);

4329 

4330 
while (get_bits1(&s>gb))

4331 
{ 
4332 
code <<= 1;

4333 
code += get_bits1(&s>gb); 
4334 
} 
4335 
sign = code & 1;

4336 
code >>= 1;

4337 

4338 
code = (sign) ? (pred  code) : (pred + code); 
4339 
#ifdef DEBUG

4340 
fprintf(stderr,"H.263+ UMV Motion = %d\n", code);

4341 
#endif

4342 
return code;

4343  
4344 
} 
4345  
4346 
static int h263_decode_block(MpegEncContext * s, DCTELEM * block, 
4347 
int n, int coded) 
4348 
{ 
4349 
int code, level, i, j, last, run;

4350 
RLTable *rl = &rl_inter; 
4351 
const uint8_t *scan_table;

4352 
GetBitContext gb= s>gb; 
4353  
4354 
scan_table = s>intra_scantable.permutated; 
4355 
if (s>h263_aic && s>mb_intra) {

4356 
rl = &rl_intra_aic; 
4357 
i = 0;

4358 
if (s>ac_pred) {

4359 
if (s>h263_aic_dir)

4360 
scan_table = s>intra_v_scantable.permutated; /* left */

4361 
else

4362 
scan_table = s>intra_h_scantable.permutated; /* top */

4363 
} 
4364 
} else if (s>mb_intra) { 
4365 
/* DC coef */

4366 
if(s>codec_id == CODEC_ID_RV10){

4367 
if (s>rv10_version == 3 && s>pict_type == I_TYPE) { 
4368 
int component, diff;

4369 
component = (n <= 3 ? 0 : n  4 + 1); 
4370 
level = s>last_dc[component]; 
4371 
if (s>rv10_first_dc_coded[component]) {

4372 
diff = rv_decode_dc(s, n); 
4373 
if (diff == 0xffff) 
4374 
return 1; 
4375 
level += diff; 
4376 
level = level & 0xff; /* handle wrap round */ 
4377 
s>last_dc[component] = level; 
4378 
} else {

4379 
s>rv10_first_dc_coded[component] = 1;

4380 
} 
4381 
} else {

4382 
level = get_bits(&s>gb, 8);

4383 
} 
4384 
}else{

4385 
level = get_bits(&s>gb, 8);

4386 
if((level&0x7F) == 0){ 
4387 
av_log(s>avctx, AV_LOG_ERROR, "illegal dc %d at %d %d\n", level, s>mb_x, s>mb_y);

4388 
return 1; 
4389 
} 
4390 
if (level == 255) 
4391 
level = 128;

4392 
} 
4393 
block[0] = level;

4394 
i = 1;

4395 
} else {

4396 
i = 0;

4397 
} 
4398 
if (!coded) {

4399 
if (s>mb_intra && s>h263_aic)

4400 
goto not_coded;

4401 
s>block_last_index[n] = i  1;

4402 
return 0; 
4403 
} 
4404 
retry:

4405 
for(;;) {

4406 
code = get_vlc2(&s>gb, rl>vlc.table, TEX_VLC_BITS, 2);

4407 
if (code < 0){ 
4408 
av_log(s>avctx, AV_LOG_ERROR, "illegal ac vlc code at %dx%d\n", s>mb_x, s>mb_y);

4409 
return 1; 
4410 
} 
4411 
if (code == rl>n) {

4412 
/* escape */

4413 
if (s>h263_flv > 1) { 
4414 
int is11 = get_bits1(&s>gb);

4415 
last = get_bits1(&s>gb); 
4416 
run = get_bits(&s>gb, 6);

4417 
if(is11){

4418 
level = get_sbits(&s>gb, 11);

4419 
} else {

4420 
level = get_sbits(&s>gb, 7);

4421 
} 
4422 
} else {

4423 
last = get_bits1(&s>gb); 
4424 
run = get_bits(&s>gb, 6);

4425 
level = (int8_t)get_bits(&s>gb, 8);

4426 
if(level == 128){ 
4427 
if (s>codec_id == CODEC_ID_RV10) {

4428 
/* XXX: should patch encoder too */

4429 
level = get_sbits(&s>gb, 12);

4430 
}else{

4431 
level = get_bits(&s>gb, 5);

4432 
level = get_sbits(&s>gb, 6)<<5; 
4433 
} 
4434 
} 
4435 
} 
4436 
} else {

4437 
run = rl>table_run[code]; 
4438 
level = rl>table_level[code]; 
4439 
last = code >= rl>last; 
4440 
if (get_bits1(&s>gb))

4441 
level = level; 
4442 
} 
4443 
i += run; 
4444 
if (i >= 64){ 
4445 
if(s>alt_inter_vlc && rl == &rl_inter && !s>mb_intra){

4446 
//looks like a hack but no, its the way its supposed to work ...

4447 
rl = &rl_intra_aic; 
4448 
i = 0;

4449 
s>gb= gb; 
4450 
memset(block, 0, sizeof(DCTELEM)*64); 
4451 
goto retry;

4452 
} 
4453 
av_log(s>avctx, AV_LOG_ERROR, "run overflow at %dx%d\n", s>mb_x, s>mb_y);

4454 
return 1; 
4455 
} 
4456 
j = scan_table[i]; 
4457 
block[j] = level; 
4458 
if (last)

4459 
break;

4460 
i++; 
4461 
} 
4462 
not_coded:

4463 
if (s>mb_intra && s>h263_aic) {

4464 
h263_pred_acdc(s, block, n); 
4465 
i = 63;

4466 
} 
4467 
s>block_last_index[n] = i; 
4468 
return 0; 
4469 
} 
4470  
4471 
/**

4472 
* decodes the dc value.

4473 
* @param n block index (03 are luma, 45 are chroma)

4474 
* @param dir_ptr the prediction direction will be stored here

4475 
* @return the quantized dc

4476 
*/

4477 
static inline int mpeg4_decode_dc(MpegEncContext * s, int n, int *dir_ptr) 
4478 
{ 
4479 
int level, pred, code;

4480 
uint16_t *dc_val; 
4481  
4482 
if (n < 4) 
4483 
code = get_vlc2(&s>gb, dc_lum.table, DC_VLC_BITS, 1);

4484 
else

4485 
code = get_vlc2(&s>gb, dc_chrom.table, DC_VLC_BITS, 1);

4486 
if (code < 0  code > 9 /* && s>nbit<9 */){ 
4487 
av_log(s>avctx, AV_LOG_ERROR, "illegal dc vlc\n");

4488 
return 1; 
4489 
} 
4490 
if (code == 0) { 
4491 
level = 0;

4492 
} else {

4493 
if(IS_3IV1){

4494 
if(code==1) 
4495 
level= 2*get_bits1(&s>gb)1; 
4496 
else{

4497 
if(get_bits1(&s>gb))

4498 
level = get_bits(&s>gb, code1) + (1<<(code1)); 
4499 
else

4500 
level = get_bits(&s>gb, code1)  (1<<(code1)); 
4501 
} 
4502 
}else{

4503 
level = get_xbits(&s>gb, code); 
4504 
} 
4505  
4506 
if (code > 8){ 
4507 
if(get_bits1(&s>gb)==0){ /* marker */ 
4508 
if(s>error_resilience>=2){ 
4509 
av_log(s>avctx, AV_LOG_ERROR, "dc marker bit missing\n");

4510 
return 1; 
4511 
} 
4512 
} 
4513 
} 
4514 
} 
4515 
pred = ff_mpeg4_pred_dc(s, n, &dc_val, dir_ptr); 
4516 
level += pred; 
4517 
if (level < 0){ 
4518 
if(s>error_resilience>=3){ 
4519 
av_log(s>avctx, AV_LOG_ERROR, "dc<0 at %dx%d\n", s>mb_x, s>mb_y);

4520 
return 1; 
4521 
} 
4522 
level = 0;

4523 
} 
4524 
if (n < 4) { 
4525 
*dc_val = level * s>y_dc_scale; 
4526 
} else {

4527 
*dc_val = level * s>c_dc_scale; 
4528 
} 
4529 
if(IS_3IV1)

4530 
*dc_val = level * 8;

4531 

4532 
if(s>error_resilience>=3){ 
4533 
if(*dc_val > 2048 + s>y_dc_scale + s>c_dc_scale){ 
4534 
av_log(s>avctx, AV_LOG_ERROR, "dc overflow at %dx%d\n", s>mb_x, s>mb_y);

4535 
return 1; 
4536 
} 
4537 
} 
4538 
return level;

4539 
} 
4540  
4541 
/**

4542 
* decodes a block.

4543 
* @return <0 if an error occured

4544 
*/

4545 
static inline int mpeg4_decode_block(MpegEncContext * s, DCTELEM * block, 
4546 
int n, int coded, int intra, int rvlc) 
4547 
{ 
4548 
int level, i, last, run;

4549 
int dc_pred_dir;

4550 
RLTable * rl; 
4551 
RL_VLC_ELEM * rl_vlc; 
4552 
const uint8_t * scan_table;

4553 
int qmul, qadd;

4554  
4555 
//Note intra & rvlc should be optimized away if this is inlined

4556 

4557 
if(intra) {

4558 
if(s>qscale < s>intra_dc_threshold){

4559 
/* DC coef */

4560 
if(s>partitioned_frame){

4561 
level = s>dc_val[0][ s>block_index[n] ];

4562 
if(n<4) level= FASTDIV((level + (s>y_dc_scale>>1)), s>y_dc_scale); 
4563 
else level= FASTDIV((level + (s>c_dc_scale>>1)), s>c_dc_scale); 
4564 
dc_pred_dir= (s>pred_dir_table[s>mb_x + s>mb_y*s>mb_stride]<<n)&32;

4565 
}else{

4566 
level = mpeg4_decode_dc(s, n, &dc_pred_dir); 
4567 
if (level < 0) 
4568 
return 1; 
4569 
} 
4570 
block[0] = level;

4571 
i = 0;

4572 
}else{

4573 
i = 1;

4574 
} 
4575 
if (!coded)

4576 
goto not_coded;

4577 

4578 
if(rvlc){

4579 
rl = &rvlc_rl_intra; 
4580 
rl_vlc = rvlc_rl_intra.rl_vlc[0];

4581 
}else{

4582 
rl = &rl_intra; 
4583 
rl_vlc = rl_intra.rl_vlc[0];

4584 
} 
4585 
if (s>ac_pred) {

4586 
if (dc_pred_dir == 0) 
4587 
scan_table = s>intra_v_scantable.permutated; /* left */

4588 
else

4589 
scan_table = s>intra_h_scantable.permutated; /* top */

4590 
} else {

4591 
scan_table = s>intra_scantable.permutated; 
4592 
} 
4593 
qmul=1;

4594 
qadd=0;

4595 
} else {

4596 
i = 1;

4597 
if (!coded) {

4598 
s>block_last_index[n] = i; 
4599 
return 0; 
4600 
} 
4601 
if(rvlc) rl = &rvlc_rl_inter;

4602 
else rl = &rl_inter;

4603 

4604 
scan_table = s>intra_scantable.permutated; 
4605  
4606 
if(s>mpeg_quant){

4607 
qmul=1;

4608 
qadd=0;

4609 
if(rvlc){

4610 
rl_vlc = rvlc_rl_inter.rl_vlc[0];

4611 
}else{

4612 
rl_vlc = rl_inter.rl_vlc[0];

4613 
} 
4614 
}else{

4615 
qmul = s>qscale << 1;

4616 
qadd = (s>qscale  1)  1; 
4617 
if(rvlc){

4618 
rl_vlc = rvlc_rl_inter.rl_vlc[s>qscale]; 
4619 
}else{

4620 
rl_vlc = rl_inter.rl_vlc[s>qscale]; 
4621 
} 
4622 
} 
4623 
} 
4624 
{ 
4625 
OPEN_READER(re, &s>gb); 
4626 
for(;;) {

4627 
UPDATE_CACHE(re, &s>gb); 
4628 
GET_RL_VLC(level, run, re, &s>gb, rl_vlc, TEX_VLC_BITS, 2);

4629 
if (level==0) { 
4630 
/* escape */

4631 
if(rvlc){

4632 
if(SHOW_UBITS(re, &s>gb, 1)==0){ 
4633 
av_log(s>avctx, AV_LOG_ERROR, "1. marker bit missing in rvlc esc\n");

4634 
return 1; 
4635 
}; SKIP_CACHE(re, &s>gb, 1);

4636 

4637 
last= SHOW_UBITS(re, &s>gb, 1); SKIP_CACHE(re, &s>gb, 1); 
4638 
run= SHOW_UBITS(re, &s>gb, 6); LAST_SKIP_CACHE(re, &s>gb, 6); 
4639 
SKIP_COUNTER(re, &s>gb, 1+1+6); 
4640 
UPDATE_CACHE(re, &s>gb); 
4641 

4642 
if(SHOW_UBITS(re, &s>gb, 1)==0){ 
4643 
av_log(s>avctx, AV_LOG_ERROR, "2. marker bit missing in rvlc esc\n");

4644 
return 1; 
4645 
}; SKIP_CACHE(re, &s>gb, 1);

4646 

4647 
level= SHOW_UBITS(re, &s>gb, 11); SKIP_CACHE(re, &s>gb, 11); 
4648 

4649 
if(SHOW_UBITS(re, &s>gb, 5)!=0x10){ 
4650 
av_log(s>avctx, AV_LOG_ERROR, "reverse esc missing\n");

4651 
return 1; 
4652 
}; SKIP_CACHE(re, &s>gb, 5);

4653  
4654 
level= level * qmul + qadd; 
4655 
level = (level ^ SHOW_SBITS(re, &s>gb, 1))  SHOW_SBITS(re, &s>gb, 1); LAST_SKIP_CACHE(re, &s>gb, 1); 
4656 
SKIP_COUNTER(re, &s>gb, 1+11+5+1); 
4657  
4658 
i+= run + 1;

4659 
if(last) i+=192; 
4660 
}else{

4661 
int cache;

4662 
cache= GET_CACHE(re, &s>gb); 
4663  
4664 
if(IS_3IV1)

4665 
cache ^= 0xC0000000;

4666  
4667 
if (cache&0x80000000) { 
4668 
if (cache&0x40000000) { 
4669 
/* third escape */

4670 
SKIP_CACHE(re, &s>gb, 2);

4671 
last= SHOW_UBITS(re, &s>gb, 1); SKIP_CACHE(re, &s>gb, 1); 
4672 
run= SHOW_UBITS(re, &s>gb, 6); LAST_SKIP_CACHE(re, &s>gb, 6); 
4673 
SKIP_COUNTER(re, &s>gb, 2+1+6); 
4674 
UPDATE_CACHE(re, &s>gb); 
4675  
4676 
if(IS_3IV1){

4677 
level= SHOW_SBITS(re, &s>gb, 12); LAST_SKIP_BITS(re, &s>gb, 12); 
4678 
}else{

4679 
if(SHOW_UBITS(re, &s>gb, 1)==0){ 
4680 
av_log(s>avctx, AV_LOG_ERROR, "1. marker bit missing in 3. esc\n");

4681 
return 1; 
4682 
}; SKIP_CACHE(re, &s>gb, 1);

4683  
4684 
level= SHOW_SBITS(re, &s>gb, 12); SKIP_CACHE(re, &s>gb, 12); 
4685  
4686 
if(SHOW_UBITS(re, &s>gb, 1)==0){ 
4687 
av_log(s>avctx, AV_LOG_ERROR, "2. marker bit missing in 3. esc\n");

4688 
return 1; 
4689 
}; LAST_SKIP_CACHE(re, &s>gb, 1);

4690  
4691 
SKIP_COUNTER(re, &s>gb, 1+12+1); 
4692 
} 
4693 

4694 
if(level*s>qscale>1024  level*s>qscale<1024){ 
4695 
av_log(s>avctx, AV_LOG_ERROR, "level overflow in 3. esc, qp=%d\n", s>qscale);

4696 
return 1; 
4697 
} 
4698 
#if 0

4699 
if(s>error_resilience >= FF_ER_COMPLIANT){

4700 
const int abs_level= ABS(level);

4701 
if(abs_level<=MAX_LEVEL && run<=MAX_RUN){

4702 
const int run1= run  rl>max_run[last][abs_level]  1;

4703 
if(abs_level <= rl>max_level[last][run]){

4704 
av_log(s>avctx, AV_LOG_ERROR, "illegal 3. esc, vlc encoding possible\n");

4705 
return 1;

4706 
}

4707 
if(s>error_resilience > FF_ER_COMPLIANT){

4708 
if(abs_level <= rl>max_level[last][run]*2){

4709 
fprintf(stderr, "illegal 3. esc, esc 1 encoding possible\n");

4710 
return 1;

4711 
}

4712 
if(run1 >= 0 && abs_level <= rl>max_level[last][run1]){

4713 
fprintf(stderr, "illegal 3. esc, esc 2 encoding possible\n");

4714 
return 1;

4715 
}

4716 
}

4717 
}

4718 
}

4719 
#endif

4720 
if (level>0) level= level * qmul + qadd; 
4721 
else level= level * qmul  qadd;

4722  
4723 
i+= run + 1;

4724 
if(last) i+=192; 
4725 
} else {

4726 
/* second escape */

4727 
#if MIN_CACHE_BITS < 20 
4728 
LAST_SKIP_BITS(re, &s>gb, 2);

4729 
UPDATE_CACHE(re, &s>gb); 
4730 
#else

4731 
SKIP_BITS(re, &s>gb, 2);

4732 
#endif

4733 
GET_RL_VLC(level, run, re, &s>gb, rl_vlc, TEX_VLC_BITS, 2);

4734 
i+= run + rl>max_run[run>>7][level/qmul] +1; //FIXME opt indexing 
4735 
level = (level ^ SHOW_SBITS(re, &s>gb, 1))  SHOW_SBITS(re, &s>gb, 1); 
4736 
LAST_SKIP_BITS(re, &s>gb, 1);

4737 
} 
4738 
} else {

4739 
/* first escape */

4740 
#if MIN_CACHE_BITS < 19 
4741 
LAST_SKIP_BITS(re, &s>gb, 1);

4742 
UPDATE_CACHE(re, &s>gb); 
4743 
#else

4744 
SKIP_BITS(re, &s>gb, 1);

4745 
#endif

4746 
GET_RL_VLC(level, run, re, &s>gb, rl_vlc, TEX_VLC_BITS, 2);

4747 
i+= run; 
4748 
level = level + rl>max_level[run>>7][(run1)&63] * qmul;//FIXME opt indexing 
4749 
level = (level ^ SHOW_SBITS(re, &s>gb, 1))  SHOW_SBITS(re, &s>gb, 1); 
4750 
LAST_SKIP_BITS(re, &s>gb, 1);

4751 
} 
4752 
} 
4753 
} else {

4754 
i+= run; 
4755 
level = (level ^ SHOW_SBITS(re, &s>gb, 1))  SHOW_SBITS(re, &s>gb, 1); 
4756 
LAST_SKIP_BITS(re, &s>gb, 1);

4757 
} 
4758 
if (i > 62){ 
4759 
i= 192;

4760 
if(i&(~63)){ 
4761 
av_log(s>avctx, AV_LOG_ERROR, "actex damaged at %d %d\n", s>mb_x, s>mb_y);

4762 
return 1; 
4763 
} 
4764  
4765 
block[scan_table[i]] = level; 
4766 
break;

4767 
} 
4768  
4769 
block[scan_table[i]] = level; 
4770 
} 
4771 
CLOSE_READER(re, &s>gb); 
4772 
} 
4773 
not_coded:

4774 
if (intra) {

4775 
if(s>qscale >= s>intra_dc_threshold){

4776 
uint16_t *dc_val; 
4777 
block[0] += ff_mpeg4_pred_dc(s, n, &dc_val, &dc_pred_dir);

4778 
if (n < 4) { 
4779 