eff6e59fc4dc843dbbed40445cd67d7f60aa7741
[libav.git] / libavcodec / h263.c
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 02111-1307 USA
20 *
21 * ac prediction encoding & b-frame support by Michael Niedermayer <michaelni@gmx.at>
22 */
23
24 //#define DEBUG
25 #include "common.h"
26 #include "dsputil.h"
27 #include "avcodec.h"
28 #include "mpegvideo.h"
29 #include "h263data.h"
30 #include "mpeg4data.h"
31
32 //rounded divison & shift
33 #define RSHIFT(a,b) ((a) > 0 ? ((a) + (1<<((b)-1)))>>(b) : ((a) + (1<<((b)-1))-1)>>(b))
34
35 #define PRINT_MB_TYPE(a) {}
36 //#define PRINT_MB_TYPE(a) printf(a)
37
38 #define INTRA_MCBPC_VLC_BITS 6
39 #define INTER_MCBPC_VLC_BITS 6
40 #define CBPY_VLC_BITS 6
41 #define MV_VLC_BITS 9
42 #define DC_VLC_BITS 9
43 #define SPRITE_TRAJ_VLC_BITS 6
44 #define MB_TYPE_B_VLC_BITS 4
45 #define TEX_VLC_BITS 9
46
47 static void h263_encode_block(MpegEncContext * s, DCTELEM * block,
48 int n);
49 static void h263_encode_motion(MpegEncContext * s, int val, int fcode);
50 static void h263p_encode_umotion(MpegEncContext * s, int val);
51 static void mpeg4_encode_block(MpegEncContext * s, DCTELEM * block,
52 int n, int dc, UINT8 *scan_table,
53 PutBitContext *dc_pb, PutBitContext *ac_pb);
54 static int h263_decode_motion(MpegEncContext * s, int pred, int fcode);
55 static int h263p_decode_umotion(MpegEncContext * s, int pred);
56 static int h263_decode_block(MpegEncContext * s, DCTELEM * block,
57 int n, int coded);
58 static inline int mpeg4_decode_dc(MpegEncContext * s, int n, int *dir_ptr);
59 static inline int mpeg4_decode_block(MpegEncContext * s, DCTELEM * block,
60 int n, int coded);
61 static int h263_pred_dc(MpegEncContext * s, int n, UINT16 **dc_val_ptr);
62 static void mpeg4_inv_pred_ac(MpegEncContext * s, INT16 *block, int n,
63 int dir);
64 static void mpeg4_decode_sprite_trajectory(MpegEncContext * s);
65
66 extern UINT32 inverse[256];
67
68 static UINT16 mv_penalty[MAX_FCODE+1][MAX_MV*2+1];
69 static UINT8 fcode_tab[MAX_MV*2+1];
70 static UINT8 umv_fcode_tab[MAX_MV*2+1];
71
72 static UINT16 uni_DCtab_lum [512][2];
73 static UINT16 uni_DCtab_chrom[512][2];
74
75 int h263_get_picture_format(int width, int height)
76 {
77 int format;
78
79 if (width == 128 && height == 96)
80 format = 1;
81 else if (width == 176 && height == 144)
82 format = 2;
83 else if (width == 352 && height == 288)
84 format = 3;
85 else if (width == 704 && height == 576)
86 format = 4;
87 else if (width == 1408 && height == 1152)
88 format = 5;
89 else
90 format = 7;
91 return format;
92 }
93
94 void h263_encode_picture_header(MpegEncContext * s, int picture_number)
95 {
96 int format;
97
98 align_put_bits(&s->pb);
99
100 /* Update the pointer to last GOB */
101 s->ptr_lastgob = pbBufPtr(&s->pb);
102 s->gob_number = 0;
103
104 put_bits(&s->pb, 22, 0x20); /* PSC */
105 put_bits(&s->pb, 8, (((INT64)s->picture_number * 30 * FRAME_RATE_BASE) /
106 s->frame_rate) & 0xff);
107
108 put_bits(&s->pb, 1, 1); /* marker */
109 put_bits(&s->pb, 1, 0); /* h263 id */
110 put_bits(&s->pb, 1, 0); /* split screen off */
111 put_bits(&s->pb, 1, 0); /* camera off */
112 put_bits(&s->pb, 1, 0); /* freeze picture release off */
113
114 format = h263_get_picture_format(s->width, s->height);
115 if (!s->h263_plus) {
116 /* H.263v1 */
117 put_bits(&s->pb, 3, format);
118 put_bits(&s->pb, 1, (s->pict_type == P_TYPE));
119 /* By now UMV IS DISABLED ON H.263v1, since the restrictions
120 of H.263v1 UMV implies to check the predicted MV after
121 calculation of the current MB to see if we're on the limits */
122 put_bits(&s->pb, 1, 0); /* unrestricted motion vector: off */
123 put_bits(&s->pb, 1, 0); /* SAC: off */
124 put_bits(&s->pb, 1, 0); /* advanced prediction mode: off */
125 put_bits(&s->pb, 1, 0); /* not PB frame */
126 put_bits(&s->pb, 5, s->qscale);
127 put_bits(&s->pb, 1, 0); /* Continuous Presence Multipoint mode: off */
128 } else {
129 /* H.263v2 */
130 /* H.263 Plus PTYPE */
131 put_bits(&s->pb, 3, 7);
132 put_bits(&s->pb,3,1); /* Update Full Extended PTYPE */
133 if (format == 7)
134 put_bits(&s->pb,3,6); /* Custom Source Format */
135 else
136 put_bits(&s->pb, 3, format);
137
138 put_bits(&s->pb,1,0); /* Custom PCF: off */
139 s->umvplus = (s->pict_type == P_TYPE) && s->unrestricted_mv;
140 put_bits(&s->pb, 1, s->umvplus); /* Unrestricted Motion Vector */
141 put_bits(&s->pb,1,0); /* SAC: off */
142 put_bits(&s->pb,1,0); /* Advanced Prediction Mode: off */
143 put_bits(&s->pb,1,s->h263_aic); /* Advanced Intra Coding */
144 put_bits(&s->pb,1,0); /* Deblocking Filter: off */
145 put_bits(&s->pb,1,0); /* Slice Structured: off */
146 put_bits(&s->pb,1,0); /* Reference Picture Selection: off */
147 put_bits(&s->pb,1,0); /* Independent Segment Decoding: off */
148 put_bits(&s->pb,1,0); /* Alternative Inter VLC: off */
149 put_bits(&s->pb,1,0); /* Modified Quantization: off */
150 put_bits(&s->pb,1,1); /* "1" to prevent start code emulation */
151 put_bits(&s->pb,3,0); /* Reserved */
152
153 put_bits(&s->pb, 3, s->pict_type == P_TYPE);
154
155 put_bits(&s->pb,1,0); /* Reference Picture Resampling: off */
156 put_bits(&s->pb,1,0); /* Reduced-Resolution Update: off */
157 if (s->pict_type == I_TYPE)
158 s->no_rounding = 0;
159 else
160 s->no_rounding ^= 1;
161 put_bits(&s->pb,1,s->no_rounding); /* Rounding Type */
162 put_bits(&s->pb,2,0); /* Reserved */
163 put_bits(&s->pb,1,1); /* "1" to prevent start code emulation */
164
165 /* This should be here if PLUSPTYPE */
166 put_bits(&s->pb, 1, 0); /* Continuous Presence Multipoint mode: off */
167
168 if (format == 7) {
169 /* Custom Picture Format (CPFMT) */
170
171 if (s->aspect_ratio_info)
172 put_bits(&s->pb,4,s->aspect_ratio_info);
173 else
174 put_bits(&s->pb,4,2); /* Aspect ratio: CIF 12:11 (4:3) picture */
175 put_bits(&s->pb,9,(s->width >> 2) - 1);
176 put_bits(&s->pb,1,1); /* "1" to prevent start code emulation */
177 put_bits(&s->pb,9,(s->height >> 2));
178 }
179
180 /* Unlimited Unrestricted Motion Vectors Indicator (UUI) */
181 if (s->umvplus)
182 put_bits(&s->pb,1,1); /* Limited according tables of Annex D */
183 put_bits(&s->pb, 5, s->qscale);
184 }
185
186 put_bits(&s->pb, 1, 0); /* no PEI */
187
188 if(s->h263_aic){
189 s->y_dc_scale_table=
190 s->c_dc_scale_table= h263_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 int h263_encode_gob_header(MpegEncContext * s, int mb_line)
198 {
199 int pdif=0;
200
201 /* Check to see if we need to put a new GBSC */
202 /* for RTP packetization */
203 if (s->rtp_mode) {
204 pdif = pbBufPtr(&s->pb) - s->ptr_lastgob;
205 if (pdif >= s->rtp_payload_size) {
206 /* Bad luck, packet must be cut before */
207 align_put_bits(&s->pb);
208 flush_put_bits(&s->pb);
209 /* Call the RTP callback to send the last GOB */
210 if (s->rtp_callback) {
211 pdif = pbBufPtr(&s->pb) - s->ptr_lastgob;
212 s->rtp_callback(s->ptr_lastgob, pdif, s->gob_number);
213 }
214 s->ptr_lastgob = pbBufPtr(&s->pb);
215 put_bits(&s->pb, 17, 1); /* GBSC */
216 s->gob_number = mb_line / s->gob_index;
217 put_bits(&s->pb, 5, s->gob_number); /* GN */
218 put_bits(&s->pb, 2, s->pict_type == I_TYPE); /* GFID */
219 put_bits(&s->pb, 5, s->qscale); /* GQUANT */
220 //fprintf(stderr,"\nGOB: %2d size: %d", s->gob_number - 1, pdif);
221 return pdif;
222 } else if (pdif + s->mb_line_avgsize >= s->rtp_payload_size) {
223 /* Cut the packet before we can't */
224 align_put_bits(&s->pb);
225 flush_put_bits(&s->pb);
226 /* Call the RTP callback to send the last GOB */
227 if (s->rtp_callback) {
228 pdif = pbBufPtr(&s->pb) - s->ptr_lastgob;
229 s->rtp_callback(s->ptr_lastgob, pdif, s->gob_number);
230 }
231 s->ptr_lastgob = pbBufPtr(&s->pb);
232 put_bits(&s->pb, 17, 1); /* GBSC */
233 s->gob_number = mb_line / s->gob_index;
234 put_bits(&s->pb, 5, s->gob_number); /* GN */
235 put_bits(&s->pb, 2, s->pict_type == I_TYPE); /* GFID */
236 put_bits(&s->pb, 5, s->qscale); /* GQUANT */
237 //fprintf(stderr,"\nGOB: %2d size: %d", s->gob_number - 1, pdif);
238 return pdif;
239 }
240 }
241 return 0;
242 }
243
244 static inline int decide_ac_pred(MpegEncContext * s, DCTELEM block[6][64], int dir[6])
245 {
246 int score0=0, score1=0;
247 int i, n;
248
249 for(n=0; n<6; n++){
250 INT16 *ac_val, *ac_val1;
251
252 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
253 ac_val1= ac_val;
254 if(dir[n]){
255 ac_val-= s->block_wrap[n]*16;
256 for(i=1; i<8; i++){
257 const int level= block[n][block_permute_op(i )];
258 score0+= ABS(level);
259 score1+= ABS(level - ac_val[i+8]);
260 ac_val1[i ]= block[n][block_permute_op(i<<3)];
261 ac_val1[i+8]= level;
262 }
263 }else{
264 ac_val-= 16;
265 for(i=1; i<8; i++){
266 const int level= block[n][block_permute_op(i<<3)];
267 score0+= ABS(level);
268 score1+= ABS(level - ac_val[i]);
269 ac_val1[i ]= level;
270 ac_val1[i+8]= block[n][block_permute_op(i )];
271 }
272 }
273 }
274
275 return score0 > score1 ? 1 : 0;
276 }
277
278 void mpeg4_encode_mb(MpegEncContext * s,
279 DCTELEM block[6][64],
280 int motion_x, int motion_y)
281 {
282 int cbpc, cbpy, i, pred_x, pred_y;
283 int bits;
284 PutBitContext * const pb2 = s->data_partitioning ? &s->pb2 : &s->pb;
285 PutBitContext * const tex_pb = s->data_partitioning && s->pict_type!=B_TYPE ? &s->tex_pb : &s->pb;
286 PutBitContext * const dc_pb = s->data_partitioning && s->pict_type!=I_TYPE ? &s->pb2 : &s->pb;
287 const int interleaved_stats= (s->flags&CODEC_FLAG_PASS1) && !s->data_partitioning ? 1 : 0;
288
289 // printf("**mb x=%d y=%d\n", s->mb_x, s->mb_y);
290 if (!s->mb_intra) {
291 /* compute cbp */
292 int cbp = 0;
293 for (i = 0; i < 6; i++) {
294 if (s->block_last_index[i] >= 0)
295 cbp |= 1 << (5 - i);
296 }
297
298 if(s->pict_type==B_TYPE){
299 static const int mb_type_table[8]= {-1, 2, 3, 1,-1,-1,-1, 0}; /* convert from mv_dir to type */
300 int mb_type= mb_type_table[s->mv_dir];
301
302 if(s->mb_x==0){
303 s->last_mv[0][0][0]=
304 s->last_mv[0][0][1]=
305 s->last_mv[1][0][0]=
306 s->last_mv[1][0][1]= 0;
307 }
308
309 /* nothing to do if this MB was skiped in the next P Frame */
310 if(s->mbskip_table[s->mb_y * s->mb_width + s->mb_x]){
311 s->skip_count++;
312 s->mv[0][0][0]=
313 s->mv[0][0][1]=
314 s->mv[1][0][0]=
315 s->mv[1][0][1]= 0;
316 s->mv_dir= MV_DIR_FORWARD; //doesnt matter
317 return;
318 }
319
320 if ((cbp | motion_x | motion_y | mb_type) ==0) {
321 /* direct MB with MV={0,0} */
322 put_bits(&s->pb, 1, 1); /* mb not coded modb1=1 */
323
324 if(interleaved_stats){
325 s->misc_bits++;
326 s->last_bits++;
327 }
328 s->skip_count++;
329 return;
330 }
331 put_bits(&s->pb, 1, 0); /* mb coded modb1=0 */
332 put_bits(&s->pb, 1, cbp ? 0 : 1); /* modb2 */ //FIXME merge
333 put_bits(&s->pb, mb_type+1, 1); // this table is so simple that we dont need it :)
334 if(cbp) put_bits(&s->pb, 6, cbp);
335
336 if(cbp && mb_type)
337 put_bits(&s->pb, 1, 0); /* no q-scale change */
338
339 if(interleaved_stats){
340 bits= get_bit_count(&s->pb);
341 s->misc_bits+= bits - s->last_bits;
342 s->last_bits=bits;
343 }
344
345 switch(mb_type)
346 {
347 case 0: /* direct */
348 h263_encode_motion(s, motion_x, 1);
349 h263_encode_motion(s, motion_y, 1);
350 break;
351 case 1: /* bidir */
352 h263_encode_motion(s, s->mv[0][0][0] - s->last_mv[0][0][0], s->f_code);
353 h263_encode_motion(s, s->mv[0][0][1] - s->last_mv[0][0][1], s->f_code);
354 h263_encode_motion(s, s->mv[1][0][0] - s->last_mv[1][0][0], s->b_code);
355 h263_encode_motion(s, s->mv[1][0][1] - s->last_mv[1][0][1], s->b_code);
356 s->last_mv[0][0][0]= s->mv[0][0][0];
357 s->last_mv[0][0][1]= s->mv[0][0][1];
358 s->last_mv[1][0][0]= s->mv[1][0][0];
359 s->last_mv[1][0][1]= s->mv[1][0][1];
360 break;
361 case 2: /* backward */
362 h263_encode_motion(s, motion_x - s->last_mv[1][0][0], s->b_code);
363 h263_encode_motion(s, motion_y - s->last_mv[1][0][1], s->b_code);
364 s->last_mv[1][0][0]= motion_x;
365 s->last_mv[1][0][1]= motion_y;
366 break;
367 case 3: /* forward */
368 h263_encode_motion(s, motion_x - s->last_mv[0][0][0], s->f_code);
369 h263_encode_motion(s, motion_y - s->last_mv[0][0][1], s->f_code);
370 s->last_mv[0][0][0]= motion_x;
371 s->last_mv[0][0][1]= motion_y;
372 break;
373 default:
374 printf("unknown mb type\n");
375 return;
376 }
377
378 if(interleaved_stats){
379 bits= get_bit_count(&s->pb);
380 s->mv_bits+= bits - s->last_bits;
381 s->last_bits=bits;
382 }
383
384 /* encode each block */
385 for (i = 0; i < 6; i++) {
386 mpeg4_encode_block(s, block[i], i, 0, zigzag_direct, NULL, &s->pb);
387 }
388
389 if(interleaved_stats){
390 bits= get_bit_count(&s->pb);
391 s->p_tex_bits+= bits - s->last_bits;
392 s->last_bits=bits;
393 }
394 }else{ /* s->pict_type==B_TYPE */
395 if ((cbp | motion_x | motion_y) == 0 && s->mv_type==MV_TYPE_16X16) {
396 /* check if the B frames can skip it too, as we must skip it if we skip here
397 why didnt they just compress the skip-mb bits instead of reusing them ?! */
398 if(s->max_b_frames>0){
399 int i;
400 int x,y, offset;
401 uint8_t *p_pic;
402
403 x= s->mb_x*16;
404 y= s->mb_y*16;
405 if(x+16 > s->width) x= s->width-16;
406 if(y+16 > s->height) y= s->height-16;
407
408 offset= x + y*s->linesize;
409 p_pic= s->new_picture[0] + offset;
410
411 s->mb_skiped=1;
412 for(i=0; i<s->max_b_frames; i++){
413 uint8_t *b_pic;
414 int diff;
415
416 if(s->coded_order[i+1].pict_type!=B_TYPE) break;
417
418 b_pic= s->coded_order[i+1].picture[0] + offset;
419 diff= pix_abs16x16(p_pic, b_pic, s->linesize);
420 if(diff>s->qscale*70){ //FIXME check that 70 is optimal
421 s->mb_skiped=0;
422 break;
423 }
424 }
425 }else
426 s->mb_skiped=1;
427
428 if(s->mb_skiped==1){
429 /* skip macroblock */
430 put_bits(&s->pb, 1, 1);
431
432 if(interleaved_stats){
433 s->misc_bits++;
434 s->last_bits++;
435 }
436 s->skip_count++;
437 return;
438 }
439 }
440
441 put_bits(&s->pb, 1, 0); /* mb coded */
442 if(s->mv_type==MV_TYPE_16X16){
443 cbpc = cbp & 3;
444 put_bits(&s->pb,
445 inter_MCBPC_bits[cbpc],
446 inter_MCBPC_code[cbpc]);
447 cbpy = cbp >> 2;
448 cbpy ^= 0xf;
449 put_bits(pb2, cbpy_tab[cbpy][1], cbpy_tab[cbpy][0]);
450
451 if(interleaved_stats){
452 bits= get_bit_count(&s->pb);
453 s->misc_bits+= bits - s->last_bits;
454 s->last_bits=bits;
455 }
456
457 /* motion vectors: 16x16 mode */
458 h263_pred_motion(s, 0, &pred_x, &pred_y);
459
460 h263_encode_motion(s, motion_x - pred_x, s->f_code);
461 h263_encode_motion(s, motion_y - pred_y, s->f_code);
462 }else{
463 cbpc = (cbp & 3)+16;
464 put_bits(&s->pb,
465 inter_MCBPC_bits[cbpc],
466 inter_MCBPC_code[cbpc]);
467 cbpy = cbp >> 2;
468 cbpy ^= 0xf;
469 put_bits(pb2, cbpy_tab[cbpy][1], cbpy_tab[cbpy][0]);
470
471 if(interleaved_stats){
472 bits= get_bit_count(&s->pb);
473 s->misc_bits+= bits - s->last_bits;
474 s->last_bits=bits;
475 }
476
477 for(i=0; i<4; i++){
478 /* motion vectors: 8x8 mode*/
479 h263_pred_motion(s, i, &pred_x, &pred_y);
480
481 h263_encode_motion(s, s->motion_val[ s->block_index[i] ][0] - pred_x, s->f_code);
482 h263_encode_motion(s, s->motion_val[ s->block_index[i] ][1] - pred_y, s->f_code);
483 }
484 }
485
486 if(interleaved_stats){
487 bits= get_bit_count(&s->pb);
488 s->mv_bits+= bits - s->last_bits;
489 s->last_bits=bits;
490 }
491
492 /* encode each block */
493 for (i = 0; i < 6; i++) {
494 mpeg4_encode_block(s, block[i], i, 0, zigzag_direct, NULL, tex_pb);
495 }
496
497 if(interleaved_stats){
498 bits= get_bit_count(&s->pb);
499 s->p_tex_bits+= bits - s->last_bits;
500 s->last_bits=bits;
501 }
502 s->p_count++;
503 }
504 } else {
505 int cbp;
506 int dc_diff[6]; //dc values with the dc prediction subtracted
507 int dir[6]; //prediction direction
508 int zigzag_last_index[6];
509 UINT8 *scan_table[6];
510
511 for(i=0; i<6; i++){
512 const int level= block[i][0];
513 UINT16 *dc_ptr;
514
515 dc_diff[i]= level - ff_mpeg4_pred_dc(s, i, &dc_ptr, &dir[i]);
516 if (i < 4) {
517 *dc_ptr = level * s->y_dc_scale;
518 } else {
519 *dc_ptr = level * s->c_dc_scale;
520 }
521 }
522
523 s->ac_pred= decide_ac_pred(s, block, dir);
524
525 if(s->ac_pred){
526 for(i=0; i<6; i++){
527 UINT8 *st;
528 int last_index;
529
530 mpeg4_inv_pred_ac(s, block[i], i, dir[i]);
531 if (dir[i]==0) st = ff_alternate_vertical_scan; /* left */
532 else st = ff_alternate_horizontal_scan; /* top */
533
534 for(last_index=63; last_index>=0; last_index--) //FIXME optimize
535 if(block[i][st[last_index]]) break;
536 zigzag_last_index[i]= s->block_last_index[i];
537 s->block_last_index[i]= last_index;
538 scan_table[i]= st;
539 }
540 }else{
541 for(i=0; i<6; i++)
542 scan_table[i]= zigzag_direct;
543 }
544
545 /* compute cbp */
546 cbp = 0;
547 for (i = 0; i < 6; i++) {
548 if (s->block_last_index[i] >= 1)
549 cbp |= 1 << (5 - i);
550 }
551
552 cbpc = cbp & 3;
553 if (s->pict_type == I_TYPE) {
554 put_bits(&s->pb,
555 intra_MCBPC_bits[cbpc],
556 intra_MCBPC_code[cbpc]);
557 } else {
558 put_bits(&s->pb, 1, 0); /* mb coded */
559 put_bits(&s->pb,
560 inter_MCBPC_bits[cbpc + 4],
561 inter_MCBPC_code[cbpc + 4]);
562 }
563 put_bits(pb2, 1, s->ac_pred);
564 cbpy = cbp >> 2;
565 put_bits(pb2, cbpy_tab[cbpy][1], cbpy_tab[cbpy][0]);
566
567 if(interleaved_stats){
568 bits= get_bit_count(&s->pb);
569 s->misc_bits+= bits - s->last_bits;
570 s->last_bits=bits;
571 }
572
573 /* encode each block */
574 for (i = 0; i < 6; i++) {
575 mpeg4_encode_block(s, block[i], i, dc_diff[i], scan_table[i], dc_pb, tex_pb);
576 }
577
578 if(interleaved_stats){
579 bits= get_bit_count(&s->pb);
580 s->i_tex_bits+= bits - s->last_bits;
581 s->last_bits=bits;
582 }
583 s->i_count++;
584
585 /* restore ac coeffs & last_index stuff if we messed them up with the prediction */
586 if(s->ac_pred){
587 for(i=0; i<6; i++){
588 int j;
589 INT16 *ac_val;
590
591 ac_val = s->ac_val[0][0] + s->block_index[i] * 16;
592
593 if(dir[i]){
594 for(j=1; j<8; j++)
595 block[i][block_permute_op(j )]= ac_val[j+8];
596 }else{
597 for(j=1; j<8; j++)
598 block[i][block_permute_op(j<<3)]= ac_val[j ];
599 }
600 s->block_last_index[i]= zigzag_last_index[i];
601 }
602 }
603 }
604 }
605
606 void h263_encode_mb(MpegEncContext * s,
607 DCTELEM block[6][64],
608 int motion_x, int motion_y)
609 {
610 int cbpc, cbpy, i, cbp, pred_x, pred_y;
611 INT16 pred_dc;
612 INT16 rec_intradc[6];
613 UINT16 *dc_ptr[6];
614
615 //printf("**mb x=%d y=%d\n", s->mb_x, s->mb_y);
616 if (!s->mb_intra) {
617 /* compute cbp */
618 cbp = 0;
619 for (i = 0; i < 6; i++) {
620 if (s->block_last_index[i] >= 0)
621 cbp |= 1 << (5 - i);
622 }
623 if ((cbp | motion_x | motion_y) == 0) {
624 /* skip macroblock */
625 put_bits(&s->pb, 1, 1);
626 return;
627 }
628 put_bits(&s->pb, 1, 0); /* mb coded */
629 cbpc = cbp & 3;
630 put_bits(&s->pb,
631 inter_MCBPC_bits[cbpc],
632 inter_MCBPC_code[cbpc]);
633 cbpy = cbp >> 2;
634 cbpy ^= 0xf;
635 put_bits(&s->pb, cbpy_tab[cbpy][1], cbpy_tab[cbpy][0]);
636
637 /* motion vectors: 16x16 mode only now */
638 h263_pred_motion(s, 0, &pred_x, &pred_y);
639
640 if (!s->umvplus) {
641 h263_encode_motion(s, motion_x - pred_x, s->f_code);
642 h263_encode_motion(s, motion_y - pred_y, s->f_code);
643 }
644 else {
645 h263p_encode_umotion(s, motion_x - pred_x);
646 h263p_encode_umotion(s, motion_y - pred_y);
647 if (((motion_x - pred_x) == 1) && ((motion_y - pred_y) == 1))
648 /* To prevent Start Code emulation */
649 put_bits(&s->pb,1,1);
650 }
651 } else {
652 int li = s->h263_aic ? 0 : 1;
653
654 cbp = 0;
655 for(i=0; i<6; i++) {
656 /* Predict DC */
657 if (s->h263_aic && s->mb_intra) {
658 INT16 level = block[i][0];
659
660 pred_dc = h263_pred_dc(s, i, &dc_ptr[i]);
661 level -= pred_dc;
662 /* Quant */
663 if (level < 0)
664 level = (level + (s->qscale >> 1))/(s->y_dc_scale);
665 else
666 level = (level - (s->qscale >> 1))/(s->y_dc_scale);
667
668 /* AIC can change CBP */
669 if (level == 0 && s->block_last_index[i] == 0)
670 s->block_last_index[i] = -1;
671 else if (level < -127)
672 level = -127;
673 else if (level > 127)
674 level = 127;
675
676 block[i][0] = level;
677 /* Reconstruction */
678 rec_intradc[i] = (s->y_dc_scale*level) + pred_dc;
679 /* Oddify */
680 rec_intradc[i] |= 1;
681 //if ((rec_intradc[i] % 2) == 0)
682 // rec_intradc[i]++;
683 /* Clipping */
684 if (rec_intradc[i] < 0)
685 rec_intradc[i] = 0;
686 else if (rec_intradc[i] > 2047)
687 rec_intradc[i] = 2047;
688
689 /* Update AC/DC tables */
690 *dc_ptr[i] = rec_intradc[i];
691 }
692 /* compute cbp */
693 if (s->block_last_index[i] >= li)
694 cbp |= 1 << (5 - i);
695 }
696
697 cbpc = cbp & 3;
698 if (s->pict_type == I_TYPE) {
699 put_bits(&s->pb,
700 intra_MCBPC_bits[cbpc],
701 intra_MCBPC_code[cbpc]);
702 } else {
703 put_bits(&s->pb, 1, 0); /* mb coded */
704 put_bits(&s->pb,
705 inter_MCBPC_bits[cbpc + 4],
706 inter_MCBPC_code[cbpc + 4]);
707 }
708 if (s->h263_aic) {
709 /* XXX: currently, we do not try to use ac prediction */
710 put_bits(&s->pb, 1, 0); /* no AC prediction */
711 }
712 cbpy = cbp >> 2;
713 put_bits(&s->pb, cbpy_tab[cbpy][1], cbpy_tab[cbpy][0]);
714 }
715
716 for(i=0; i<6; i++) {
717 /* encode each block */
718 h263_encode_block(s, block[i], i);
719
720 /* Update INTRADC for decoding */
721 if (s->h263_aic && s->mb_intra) {
722 block[i][0] = rec_intradc[i];
723
724 }
725 }
726 }
727
728 static int h263_pred_dc(MpegEncContext * s, int n, UINT16 **dc_val_ptr)
729 {
730 int x, y, wrap, a, c, pred_dc, scale;
731 INT16 *dc_val, *ac_val;
732
733 /* find prediction */
734 if (n < 4) {
735 x = 2 * s->mb_x + 1 + (n & 1);
736 y = 2 * s->mb_y + 1 + ((n & 2) >> 1);
737 wrap = s->mb_width * 2 + 2;
738 dc_val = s->dc_val[0];
739 ac_val = s->ac_val[0][0];
740 scale = s->y_dc_scale;
741 } else {
742 x = s->mb_x + 1;
743 y = s->mb_y + 1;
744 wrap = s->mb_width + 2;
745 dc_val = s->dc_val[n - 4 + 1];
746 ac_val = s->ac_val[n - 4 + 1][0];
747 scale = s->c_dc_scale;
748 }
749 /* B C
750 * A X
751 */
752 a = dc_val[(x - 1) + (y) * wrap];
753 c = dc_val[(x) + (y - 1) * wrap];
754
755 /* No prediction outside GOB boundary */
756 if (s->first_slice_line && ((n < 2) || (n > 3)))
757 c = 1024;
758 pred_dc = 1024;
759 /* just DC prediction */
760 if (a != 1024 && c != 1024)
761 pred_dc = (a + c) >> 1;
762 else if (a != 1024)
763 pred_dc = a;
764 else
765 pred_dc = c;
766
767 /* we assume pred is positive */
768 //pred_dc = (pred_dc + (scale >> 1)) / scale;
769 *dc_val_ptr = &dc_val[x + y * wrap];
770 return pred_dc;
771 }
772
773
774 void h263_pred_acdc(MpegEncContext * s, INT16 *block, int n)
775 {
776 int x, y, wrap, a, c, pred_dc, scale, i;
777 INT16 *dc_val, *ac_val, *ac_val1;
778
779 /* find prediction */
780 if (n < 4) {
781 x = 2 * s->mb_x + 1 + (n & 1);
782 y = 2 * s->mb_y + 1 + ((n & 2) >> 1);
783 wrap = s->mb_width * 2 + 2;
784 dc_val = s->dc_val[0];
785 ac_val = s->ac_val[0][0];
786 scale = s->y_dc_scale;
787 } else {
788 x = s->mb_x + 1;
789 y = s->mb_y + 1;
790 wrap = s->mb_width + 2;
791 dc_val = s->dc_val[n - 4 + 1];
792 ac_val = s->ac_val[n - 4 + 1][0];
793 scale = s->c_dc_scale;
794 }
795
796 ac_val += ((y) * wrap + (x)) * 16;
797 ac_val1 = ac_val;
798
799 /* B C
800 * A X
801 */
802 a = dc_val[(x - 1) + (y) * wrap];
803 c = dc_val[(x) + (y - 1) * wrap];
804
805 /* No prediction outside GOB boundary */
806 if (s->first_slice_line && ((n < 2) || (n > 3)))
807 c = 1024;
808 pred_dc = 1024;
809 if (s->ac_pred) {
810 if (s->h263_aic_dir) {
811 /* left prediction */
812 if (a != 1024) {
813 ac_val -= 16;
814 for(i=1;i<8;i++) {
815 block[block_permute_op(i*8)] += ac_val[i];
816 }
817 pred_dc = a;
818 }
819 } else {
820 /* top prediction */
821 if (c != 1024) {
822 ac_val -= 16 * wrap;
823 for(i=1;i<8;i++) {
824 block[block_permute_op(i)] += ac_val[i + 8];
825 }
826 pred_dc = c;
827 }
828 }
829 } else {
830 /* just DC prediction */
831 if (a != 1024 && c != 1024)
832 pred_dc = (a + c) >> 1;
833 else if (a != 1024)
834 pred_dc = a;
835 else
836 pred_dc = c;
837 }
838
839 /* we assume pred is positive */
840 block[0]=block[0]*scale + pred_dc;
841
842 if (block[0] < 0)
843 block[0] = 0;
844 else if (!(block[0] & 1))
845 block[0]++;
846
847 /* Update AC/DC tables */
848 dc_val[(x) + (y) * wrap] = block[0];
849
850 /* left copy */
851 for(i=1;i<8;i++)
852 ac_val1[i] = block[block_permute_op(i * 8)];
853 /* top copy */
854 for(i=1;i<8;i++)
855 ac_val1[8 + i] = block[block_permute_op(i)];
856 }
857
858 INT16 *h263_pred_motion(MpegEncContext * s, int block,
859 int *px, int *py)
860 {
861 int xy, wrap;
862 INT16 *A, *B, *C, *mot_val;
863 static const int off[4]= {2, 1, 1, -1};
864
865 wrap = s->block_wrap[0];
866 xy = s->block_index[block];
867
868 mot_val = s->motion_val[xy];
869
870 A = s->motion_val[xy - 1];
871 /* special case for first (slice) line */
872 if ((s->mb_y == 0 || s->first_slice_line) && block<3) {
873 // we cant just change some MVs to simulate that as we need them for the B frames (and ME)
874 // and if we ever support non rectangular objects than we need to do a few ifs here anyway :(
875 if(block==0){ //most common case
876 if(s->mb_x == s->resync_mb_x){ //rare
877 *px= *py = 0;
878 }else if(s->mb_x + 1 == s->resync_mb_x){ //rare
879 C = s->motion_val[xy + off[block] - wrap];
880 if(s->mb_x==0){
881 *px = C[0];
882 *py = C[1];
883 }else{
884 *px = mid_pred(A[0], 0, C[0]);
885 *py = mid_pred(A[1], 0, C[1]);
886 }
887 }else{
888 *px = A[0];
889 *py = A[1];
890 }
891 }else if(block==1){
892 if(s->mb_x + 1 == s->resync_mb_x){ //rare
893 C = s->motion_val[xy + off[block] - wrap];
894 *px = mid_pred(A[0], 0, C[0]);
895 *py = mid_pred(A[1], 0, C[1]);
896 }else{
897 *px = A[0];
898 *py = A[1];
899 }
900 }else{ /* block==2*/
901 B = s->motion_val[xy - wrap];
902 C = s->motion_val[xy + off[block] - wrap];
903 if(s->mb_x == s->resync_mb_x) //rare
904 A[0]=A[1]=0;
905
906 *px = mid_pred(A[0], B[0], C[0]);
907 *py = mid_pred(A[1], B[1], C[1]);
908 }
909 } else {
910 B = s->motion_val[xy - wrap];
911 C = s->motion_val[xy + off[block] - wrap];
912 *px = mid_pred(A[0], B[0], C[0]);
913 *py = mid_pred(A[1], B[1], C[1]);
914 }
915 return mot_val;
916 }
917
918 static void h263_encode_motion(MpegEncContext * s, int val, int f_code)
919 {
920 int range, l, m, bit_size, sign, code, bits;
921
922 if (val == 0) {
923 /* zero vector */
924 code = 0;
925 put_bits(&s->pb, mvtab[code][1], mvtab[code][0]);
926 } else {
927 bit_size = f_code - 1;
928 range = 1 << bit_size;
929 /* modulo encoding */
930 l = range * 32;
931 m = 2 * l;
932 if (val < -l) {
933 val += m;
934 } else if (val >= l) {
935 val -= m;
936 }
937
938 if (val >= 0) {
939 sign = 0;
940 } else {
941 val = -val;
942 sign = 1;
943 }
944 val--;
945 code = (val >> bit_size) + 1;
946 bits = val & (range - 1);
947
948 put_bits(&s->pb, mvtab[code][1] + 1, (mvtab[code][0] << 1) | sign);
949 if (bit_size > 0) {
950 put_bits(&s->pb, bit_size, bits);
951 }
952 }
953 }
954
955 /* Encode MV differences on H.263+ with Unrestricted MV mode */
956 static void h263p_encode_umotion(MpegEncContext * s, int val)
957 {
958 short sval = 0;
959 short i = 0;
960 short n_bits = 0;
961 short temp_val;
962 int code = 0;
963 int tcode;
964
965 if ( val == 0)
966 put_bits(&s->pb, 1, 1);
967 else if (val == 1)
968 put_bits(&s->pb, 3, 0);
969 else if (val == -1)
970 put_bits(&s->pb, 3, 2);
971 else {
972
973 sval = ((val < 0) ? (short)(-val):(short)val);
974 temp_val = sval;
975
976 while (temp_val != 0) {
977 temp_val = temp_val >> 1;
978 n_bits++;
979 }
980
981 i = n_bits - 1;
982 while (i > 0) {
983 tcode = (sval & (1 << (i-1))) >> (i-1);
984 tcode = (tcode << 1) | 1;
985 code = (code << 2) | tcode;
986 i--;
987 }
988 code = ((code << 1) | (val < 0)) << 1;
989 put_bits(&s->pb, (2*n_bits)+1, code);
990 //printf("\nVal = %d\tCode = %d", sval, code);
991 }
992 }
993
994 static void init_mv_penalty_and_fcode(MpegEncContext *s)
995 {
996 int f_code;
997 int mv;
998 for(f_code=1; f_code<=MAX_FCODE; f_code++){
999 for(mv=-MAX_MV; mv<=MAX_MV; mv++){
1000 int len;
1001
1002 if(mv==0) len= mvtab[0][1];
1003 else{
1004 int val, bit_size, range, code;
1005
1006 bit_size = s->f_code - 1;
1007 range = 1 << bit_size;
1008
1009 val=mv;
1010 if (val < 0)
1011 val = -val;
1012 val--;
1013 code = (val >> bit_size) + 1;
1014 if(code<33){
1015 len= mvtab[code][1] + 1 + bit_size;
1016 }else{
1017 len= mvtab[32][1] + 2 + bit_size;
1018 }
1019 }
1020
1021 mv_penalty[f_code][mv+MAX_MV]= len;
1022 }
1023 }
1024
1025 for(f_code=MAX_FCODE; f_code>0; f_code--){
1026 for(mv=-(16<<f_code); mv<(16<<f_code); mv++){
1027 fcode_tab[mv+MAX_MV]= f_code;
1028 }
1029 }
1030
1031 for(mv=0; mv<MAX_MV*2+1; mv++){
1032 umv_fcode_tab[mv]= 1;
1033 }
1034 }
1035
1036 static void init_uni_dc_tab(void)
1037 {
1038 int level, uni_code, uni_len;
1039
1040 for(level=-256; level<256; level++){
1041 int size, v, l;
1042 /* find number of bits */
1043 size = 0;
1044 v = abs(level);
1045 while (v) {
1046 v >>= 1;
1047 size++;
1048 }
1049
1050 if (level < 0)
1051 l= (-level) ^ ((1 << size) - 1);
1052 else
1053 l= level;
1054
1055 /* luminance */
1056 uni_code= DCtab_lum[size][0];
1057 uni_len = DCtab_lum[size][1];
1058
1059 if (size > 0) {
1060 uni_code<<=size; uni_code|=l;
1061 uni_len+=size;
1062 if (size > 8){
1063 uni_code<<=1; uni_code|=1;
1064 uni_len++;
1065 }
1066 }
1067 uni_DCtab_lum[level+256][0]= uni_code;
1068 uni_DCtab_lum[level+256][1]= uni_len;
1069
1070 /* chrominance */
1071 uni_code= DCtab_chrom[size][0];
1072 uni_len = DCtab_chrom[size][1];
1073
1074 if (size > 0) {
1075 uni_code<<=size; uni_code|=l;
1076 uni_len+=size;
1077 if (size > 8){
1078 uni_code<<=1; uni_code|=1;
1079 uni_len++;
1080 }
1081 }
1082 uni_DCtab_chrom[level+256][0]= uni_code;
1083 uni_DCtab_chrom[level+256][1]= uni_len;
1084
1085 }
1086 }
1087
1088 void h263_encode_init(MpegEncContext *s)
1089 {
1090 static int done = 0;
1091
1092 if (!done) {
1093 done = 1;
1094
1095 init_uni_dc_tab();
1096
1097 init_rl(&rl_inter);
1098 init_rl(&rl_intra);
1099 init_rl(&rl_intra_aic);
1100
1101 init_mv_penalty_and_fcode(s);
1102 }
1103 s->mv_penalty= mv_penalty; //FIXME exact table for msmpeg4 & h263p
1104
1105 // use fcodes >1 only for mpeg4 & h263 & h263p FIXME
1106 switch(s->codec_id){
1107 case CODEC_ID_MPEG4:
1108 s->fcode_tab= fcode_tab;
1109 s->min_qcoeff= -2048;
1110 s->max_qcoeff= 2047;
1111 break;
1112 case CODEC_ID_H263P:
1113 s->fcode_tab= umv_fcode_tab;
1114 s->min_qcoeff= -128;
1115 s->max_qcoeff= 127;
1116 break;
1117 //Note for mpeg4 & h263 the dc-scale table will be set per frame as needed later
1118 default: //nothing needed default table allready set in mpegvideo.c
1119 s->min_qcoeff= -128;
1120 s->max_qcoeff= 127;
1121 s->y_dc_scale_table=
1122 s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
1123 }
1124
1125 /* h263 type bias */
1126 //FIXME mpeg4 mpeg quantizer
1127 s->intra_quant_bias=0;
1128 s->inter_quant_bias=-(1<<(QUANT_BIAS_SHIFT-2)); //(a - x/4)/x
1129 }
1130
1131 static void h263_encode_block(MpegEncContext * s, DCTELEM * block, int n)
1132 {
1133 int level, run, last, i, j, last_index, last_non_zero, sign, slevel, code;
1134 RLTable *rl;
1135
1136 rl = &rl_inter;
1137 if (s->mb_intra && !s->h263_aic) {
1138 /* DC coef */
1139 level = block[0];
1140 /* 255 cannot be represented, so we clamp */
1141 if (level > 254) {
1142 level = 254;
1143 block[0] = 254;
1144 }
1145 /* 0 cannot be represented also */
1146 else if (!level) {
1147 level = 1;
1148 block[0] = 1;
1149 }
1150 if (level == 128)
1151 put_bits(&s->pb, 8, 0xff);
1152 else
1153 put_bits(&s->pb, 8, level & 0xff);
1154 i = 1;
1155 } else {
1156 i = 0;
1157 if (s->h263_aic && s->mb_intra)
1158 rl = &rl_intra_aic;
1159 }
1160
1161 /* AC coefs */
1162 last_index = s->block_last_index[n];
1163 last_non_zero = i - 1;
1164 for (; i <= last_index; i++) {
1165 j = zigzag_direct[i];
1166 level = block[j];
1167 if (level) {
1168 run = i - last_non_zero - 1;
1169 last = (i == last_index);
1170 sign = 0;
1171 slevel = level;
1172 if (level < 0) {
1173 sign = 1;
1174 level = -level;
1175 }
1176 code = get_rl_index(rl, last, run, level);
1177 put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
1178 if (code == rl->n) {
1179 put_bits(&s->pb, 1, last);
1180 put_bits(&s->pb, 6, run);
1181 put_bits(&s->pb, 8, slevel & 0xff);
1182 } else {
1183 put_bits(&s->pb, 1, sign);
1184 }
1185 last_non_zero = i;
1186 }
1187 }
1188 }
1189
1190 /***************************************************/
1191
1192 void ff_mpeg4_stuffing(PutBitContext * pbc)
1193 {
1194 int length;
1195 put_bits(pbc, 1, 0);
1196 length= (-get_bit_count(pbc))&7;
1197 if(length) put_bits(pbc, length, (1<<length)-1);
1198 }
1199
1200 /* must be called before writing the header */
1201 void ff_set_mpeg4_time(MpegEncContext * s, int picture_number){
1202 int time_div, time_mod;
1203
1204 if(s->pict_type==I_TYPE){ //we will encode a vol header
1205 s->time_increment_resolution= s->frame_rate/ff_gcd(s->frame_rate, FRAME_RATE_BASE);
1206 if(s->time_increment_resolution>=256*256) s->time_increment_resolution= 256*128;
1207
1208 s->time_increment_bits = av_log2(s->time_increment_resolution - 1) + 1;
1209 }
1210
1211 s->time= picture_number*(INT64)FRAME_RATE_BASE*s->time_increment_resolution/s->frame_rate;
1212 time_div= s->time/s->time_increment_resolution;
1213 time_mod= s->time%s->time_increment_resolution;
1214
1215 if(s->pict_type==B_TYPE){
1216 s->bp_time= s->last_non_b_time - s->time;
1217 }else{
1218 s->last_time_base= s->time_base;
1219 s->time_base= time_div;
1220 s->pp_time= s->time - s->last_non_b_time;
1221 s->last_non_b_time= s->time;
1222 }
1223 }
1224
1225 static void mpeg4_encode_vol_header(MpegEncContext * s)
1226 {
1227 int vo_ver_id=1; //must be 2 if we want GMC or q-pel
1228 char buf[255];
1229
1230 s->vo_type= s->has_b_frames ? CORE_VO_TYPE : SIMPLE_VO_TYPE;
1231
1232 put_bits(&s->pb, 16, 0);
1233 put_bits(&s->pb, 16, 0x100); /* video obj */
1234 put_bits(&s->pb, 16, 0);
1235 put_bits(&s->pb, 16, 0x120); /* video obj layer */
1236
1237 put_bits(&s->pb, 1, 0); /* random access vol */
1238 put_bits(&s->pb, 8, s->vo_type); /* video obj type indication */
1239 put_bits(&s->pb, 1, 1); /* is obj layer id= yes */
1240 put_bits(&s->pb, 4, vo_ver_id); /* is obj layer ver id */
1241 put_bits(&s->pb, 3, 1); /* is obj layer priority */
1242 if(s->aspect_ratio_info)
1243 put_bits(&s->pb, 4, s->aspect_ratio_info);/* aspect ratio info */
1244 else
1245 put_bits(&s->pb, 4, 1); /* aspect ratio info= sqare pixel */
1246
1247 if(s->low_delay){
1248 put_bits(&s->pb, 1, 1); /* vol control parameters= yes */
1249 put_bits(&s->pb, 2, 1); /* chroma format YUV 420/YV12 */
1250 put_bits(&s->pb, 1, s->low_delay);
1251 put_bits(&s->pb, 1, 0); /* vbv parameters= no */
1252 }else{
1253 put_bits(&s->pb, 1, 0); /* vol control parameters= no */
1254 }
1255
1256 put_bits(&s->pb, 2, RECT_SHAPE); /* vol shape= rectangle */
1257 put_bits(&s->pb, 1, 1); /* marker bit */
1258
1259 put_bits(&s->pb, 16, s->time_increment_resolution);
1260 if (s->time_increment_bits < 1)
1261 s->time_increment_bits = 1;
1262 put_bits(&s->pb, 1, 1); /* marker bit */
1263 put_bits(&s->pb, 1, 0); /* fixed vop rate=no */
1264 put_bits(&s->pb, 1, 1); /* marker bit */
1265 put_bits(&s->pb, 13, s->width); /* vol width */
1266 put_bits(&s->pb, 1, 1); /* marker bit */
1267 put_bits(&s->pb, 13, s->height); /* vol height */
1268 put_bits(&s->pb, 1, 1); /* marker bit */
1269 put_bits(&s->pb, 1, 0); /* interlace */
1270 put_bits(&s->pb, 1, 1); /* obmc disable */
1271 if (vo_ver_id == 1) {
1272 put_bits(&s->pb, 1, s->vol_sprite_usage=0); /* sprite enable */
1273 }else{ /* vo_ver_id == 2 */
1274 put_bits(&s->pb, 2, s->vol_sprite_usage=0); /* sprite enable */
1275 }
1276 put_bits(&s->pb, 1, 0); /* not 8 bit */
1277 put_bits(&s->pb, 1, 0); /* quant type= h263 style*/
1278 if (vo_ver_id != 1)
1279 put_bits(&s->pb, 1, s->quarter_sample=0);
1280 put_bits(&s->pb, 1, 1); /* complexity estimation disable */
1281 s->resync_marker= s->rtp_mode;
1282 put_bits(&s->pb, 1, s->resync_marker ? 0 : 1);/* resync marker disable */
1283 put_bits(&s->pb, 1, s->data_partitioning ? 1 : 0);
1284 if(s->data_partitioning){
1285 put_bits(&s->pb, 1, 0); /* no rvlc */
1286 }
1287
1288 if (vo_ver_id != 1){
1289 put_bits(&s->pb, 1, 0); /* newpred */
1290 put_bits(&s->pb, 1, 0); /* reduced res vop */
1291 }
1292 put_bits(&s->pb, 1, 0); /* scalability */
1293
1294 ff_mpeg4_stuffing(&s->pb);
1295 put_bits(&s->pb, 16, 0);
1296 put_bits(&s->pb, 16, 0x1B2); /* user_data */
1297 sprintf(buf, "FFmpeg%sb%s", FFMPEG_VERSION, LIBAVCODEC_BUILD_STR);
1298 put_string(&s->pb, buf);
1299
1300 ff_mpeg4_stuffing(&s->pb);
1301 }
1302
1303 /* write mpeg4 VOP header */
1304 void mpeg4_encode_picture_header(MpegEncContext * s, int picture_number)
1305 {
1306 int time_incr;
1307 int time_div, time_mod;
1308
1309 if(s->pict_type==I_TYPE){
1310 s->no_rounding=0;
1311 if(picture_number==0 || !s->strict_std_compliance)
1312 mpeg4_encode_vol_header(s);
1313 }
1314
1315 //printf("num:%d rate:%d base:%d\n", s->picture_number, s->frame_rate, FRAME_RATE_BASE);
1316
1317 put_bits(&s->pb, 16, 0); /* vop header */
1318 put_bits(&s->pb, 16, 0x1B6); /* vop header */
1319 put_bits(&s->pb, 2, s->pict_type - 1); /* pict type: I = 0 , P = 1 */
1320
1321 time_div= s->time/s->time_increment_resolution;
1322 time_mod= s->time%s->time_increment_resolution;
1323 time_incr= time_div - s->last_time_base;
1324 while(time_incr--)
1325 put_bits(&s->pb, 1, 1);
1326
1327 put_bits(&s->pb, 1, 0);
1328
1329 put_bits(&s->pb, 1, 1); /* marker */
1330 put_bits(&s->pb, s->time_increment_bits, time_mod); /* time increment */
1331 put_bits(&s->pb, 1, 1); /* marker */
1332 put_bits(&s->pb, 1, 1); /* vop coded */
1333 if ( s->pict_type == P_TYPE
1334 || (s->pict_type == S_TYPE && s->vol_sprite_usage==GMC_SPRITE)) {
1335 s->no_rounding ^= 1;
1336 put_bits(&s->pb, 1, s->no_rounding); /* rounding type */
1337 }
1338 put_bits(&s->pb, 3, 0); /* intra dc VLC threshold */
1339 //FIXME sprite stuff
1340
1341 put_bits(&s->pb, 5, s->qscale);
1342
1343 if (s->pict_type != I_TYPE)
1344 put_bits(&s->pb, 3, s->f_code); /* fcode_for */
1345 if (s->pict_type == B_TYPE)
1346 put_bits(&s->pb, 3, s->b_code); /* fcode_back */
1347 // printf("****frame %d\n", picture_number);
1348
1349 s->y_dc_scale_table= ff_mpeg4_y_dc_scale_table; //FIXME add short header support
1350 s->c_dc_scale_table= ff_mpeg4_c_dc_scale_table;
1351 }
1352
1353 static void h263_dc_scale(MpegEncContext * s)
1354 {
1355 s->y_dc_scale= s->y_dc_scale_table[ s->qscale ];
1356 s->c_dc_scale= s->c_dc_scale_table[ s->qscale ];
1357 }
1358
1359 inline int ff_mpeg4_pred_dc(MpegEncContext * s, int n, UINT16 **dc_val_ptr, int *dir_ptr)
1360 {
1361 int a, b, c, wrap, pred, scale;
1362 UINT16 *dc_val;
1363 int dummy;
1364
1365 /* find prediction */
1366 if (n < 4) {
1367 scale = s->y_dc_scale;
1368 } else {
1369 scale = s->c_dc_scale;
1370 }
1371 wrap= s->block_wrap[n];
1372 dc_val = s->dc_val[0] + s->block_index[n];
1373
1374 /* B C
1375 * A X
1376 */
1377 a = dc_val[ - 1];
1378 b = dc_val[ - 1 - wrap];
1379 c = dc_val[ - wrap];
1380
1381 if (abs(a - b) < abs(b - c)) {
1382 pred = c;
1383 *dir_ptr = 1; /* top */
1384 } else {
1385 pred = a;
1386 *dir_ptr = 0; /* left */
1387 }
1388 /* we assume pred is positive */
1389 #ifdef ARCH_X86
1390 asm volatile (
1391 "xorl %%edx, %%edx \n\t"
1392 "mul %%ecx \n\t"
1393 : "=d" (pred), "=a"(dummy)
1394 : "a" (pred + (scale >> 1)), "c" (inverse[scale])
1395 );
1396 #else
1397 pred = (pred + (scale >> 1)) / scale;
1398 #endif
1399
1400 /* prepare address for prediction update */
1401 *dc_val_ptr = &dc_val[0];
1402
1403 return pred;
1404 }
1405
1406 void mpeg4_pred_ac(MpegEncContext * s, INT16 *block, int n,
1407 int dir)
1408 {
1409 int i;
1410 INT16 *ac_val, *ac_val1;
1411
1412 /* find prediction */
1413 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
1414 ac_val1 = ac_val;
1415 if (s->ac_pred) {
1416 if (dir == 0) {
1417 /* left prediction */
1418 ac_val -= 16;
1419 for(i=1;i<8;i++) {
1420 block[block_permute_op(i*8)] += ac_val[i];
1421 }
1422 } else {
1423 /* top prediction */
1424 ac_val -= 16 * s->block_wrap[n];
1425 for(i=1;i<8;i++) {
1426 block[block_permute_op(i)] += ac_val[i + 8];
1427 }
1428 }
1429 }
1430 /* left copy */
1431 for(i=1;i<8;i++)
1432 ac_val1[i] = block[block_permute_op(i * 8)];
1433 /* top copy */
1434 for(i=1;i<8;i++)
1435 ac_val1[8 + i] = block[block_permute_op(i)];
1436 }
1437
1438 static void mpeg4_inv_pred_ac(MpegEncContext * s, INT16 *block, int n,
1439 int dir)
1440 {
1441 int i;
1442 INT16 *ac_val;
1443
1444 /* find prediction */
1445 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
1446
1447 if (dir == 0) {
1448 /* left prediction */
1449 ac_val -= 16;
1450 for(i=1;i<8;i++) {
1451 block[block_permute_op(i*8)] -= ac_val[i];
1452 }
1453 } else {
1454 /* top prediction */
1455 ac_val -= 16 * s->block_wrap[n];
1456 for(i=1;i<8;i++) {
1457 block[block_permute_op(i)] -= ac_val[i + 8];
1458 }
1459 }
1460 }
1461
1462 static inline void mpeg4_encode_dc(PutBitContext * s, int level, int n)
1463 {
1464 #if 1
1465 // if(level<-255 || level>255) printf("dc overflow\n");
1466 level+=256;
1467 if (n < 4) {
1468 /* luminance */
1469 put_bits(s, uni_DCtab_lum[level][1], uni_DCtab_lum[level][0]);
1470 } else {
1471 /* chrominance */
1472 put_bits(s, uni_DCtab_chrom[level][1], uni_DCtab_chrom[level][0]);
1473 }
1474 #else
1475 int size, v;
1476 /* find number of bits */
1477 size = 0;
1478 v = abs(level);
1479 while (v) {
1480 v >>= 1;
1481 size++;
1482 }
1483
1484 if (n < 4) {
1485 /* luminance */
1486 put_bits(&s->pb, DCtab_lum[size][1], DCtab_lum[size][0]);
1487 } else {
1488 /* chrominance */
1489 put_bits(&s->pb, DCtab_chrom[size][1], DCtab_chrom[size][0]);
1490 }
1491
1492 /* encode remaining bits */
1493 if (size > 0) {
1494 if (level < 0)
1495 level = (-level) ^ ((1 << size) - 1);
1496 put_bits(&s->pb, size, level);
1497 if (size > 8)
1498 put_bits(&s->pb, 1, 1);
1499 }
1500 #endif
1501 }
1502
1503 static void mpeg4_encode_block(MpegEncContext * s, DCTELEM * block, int n, int intra_dc,
1504 UINT8 *scan_table, PutBitContext *dc_pb, PutBitContext *ac_pb)
1505 {
1506 int level, run, last, i, j, last_index, last_non_zero, sign, slevel;
1507 int code;
1508 const RLTable *rl;
1509
1510 if (s->mb_intra) {
1511 /* mpeg4 based DC predictor */
1512 mpeg4_encode_dc(dc_pb, intra_dc, n);
1513 i = 1;
1514 rl = &rl_intra;
1515 } else {
1516 i = 0;
1517 rl = &rl_inter;
1518 }
1519
1520 /* AC coefs */
1521 last_index = s->block_last_index[n];
1522 last_non_zero = i - 1;
1523 for (; i <= last_index; i++) {
1524 j = scan_table[i];
1525 level = block[j];
1526 if (level) {
1527 run = i - last_non_zero - 1;
1528 last = (i == last_index);
1529 sign = 0;
1530 slevel = level;
1531 if (level < 0) {
1532 sign = 1;
1533 level = -level;
1534 }
1535 code = get_rl_index(rl, last, run, level);
1536 put_bits(ac_pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
1537 if (code == rl->n) {
1538 int level1, run1;
1539 level1 = level - rl->max_level[last][run];
1540 if (level1 < 1)
1541 goto esc2;
1542 code = get_rl_index(rl, last, run, level1);
1543 if (code == rl->n) {
1544 esc2:
1545 put_bits(ac_pb, 1, 1);
1546 if (level > MAX_LEVEL)
1547 goto esc3;
1548 run1 = run - rl->max_run[last][level] - 1;
1549 if (run1 < 0)
1550 goto esc3;
1551 code = get_rl_index(rl, last, run1, level);
1552 if (code == rl->n) {
1553 esc3:
1554 /* third escape */
1555 put_bits(ac_pb, 1, 1);
1556 put_bits(ac_pb, 1, last);
1557 put_bits(ac_pb, 6, run);
1558 put_bits(ac_pb, 1, 1);
1559 put_bits(ac_pb, 12, slevel & 0xfff);
1560 put_bits(ac_pb, 1, 1);
1561 } else {
1562 /* second escape */
1563 put_bits(ac_pb, 1, 0);
1564 put_bits(ac_pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
1565 put_bits(ac_pb, 1, sign);
1566 }
1567 } else {
1568 /* first escape */
1569 put_bits(ac_pb, 1, 0);
1570 put_bits(ac_pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
1571 put_bits(ac_pb, 1, sign);
1572 }
1573 } else {
1574 put_bits(ac_pb, 1, sign);
1575 }
1576 last_non_zero = i;
1577 }
1578 }
1579 }
1580
1581
1582
1583 /***********************************************/
1584 /* decoding */
1585
1586 static VLC intra_MCBPC_vlc;
1587 static VLC inter_MCBPC_vlc;
1588 static VLC cbpy_vlc;
1589 static VLC mv_vlc;
1590 static VLC dc_lum, dc_chrom;
1591 static VLC sprite_trajectory;
1592 static VLC mb_type_b_vlc;
1593
1594 void init_rl(RLTable *rl)
1595 {
1596 INT8 max_level[MAX_RUN+1], max_run[MAX_LEVEL+1];
1597 UINT8 index_run[MAX_RUN+1];
1598 int last, run, level, start, end, i;
1599
1600 /* compute max_level[], max_run[] and index_run[] */
1601 for(last=0;last<2;last++) {
1602 if (last == 0) {
1603 start = 0;
1604 end = rl->last;
1605 } else {
1606 start = rl->last;
1607 end = rl->n;
1608 }
1609
1610 memset(max_level, 0, MAX_RUN + 1);
1611 memset(max_run, 0, MAX_LEVEL + 1);
1612 memset(index_run, rl->n, MAX_RUN + 1);
1613 for(i=start;i<end;i++) {
1614 run = rl->table_run[i];
1615 level = rl->table_level[i];
1616 if (index_run[run] == rl->n)
1617 index_run[run] = i;
1618 if (level > max_level[run])
1619 max_level[run] = level;
1620 if (run > max_run[level])
1621 max_run[level] = run;
1622 }
1623 rl->max_level[last] = av_malloc(MAX_RUN + 1);
1624 memcpy(rl->max_level[last], max_level, MAX_RUN + 1);
1625 rl->max_run[last] = av_malloc(MAX_LEVEL + 1);
1626 memcpy(rl->max_run[last], max_run, MAX_LEVEL + 1);
1627 rl->index_run[last] = av_malloc(MAX_RUN + 1);
1628 memcpy(rl->index_run[last], index_run, MAX_RUN + 1);
1629 }
1630 }
1631
1632 void init_vlc_rl(RLTable *rl)
1633 {
1634 int i, q;
1635
1636 init_vlc(&rl->vlc, 9, rl->n + 1,
1637 &rl->table_vlc[0][1], 4, 2,
1638 &rl->table_vlc[0][0], 4, 2);
1639
1640
1641 for(q=0; q<32; q++){
1642 int qmul= q*2;
1643 int qadd= (q-1)|1;
1644
1645 if(q==0){
1646 qmul=1;
1647 qadd=0;
1648 }
1649
1650 rl->rl_vlc[q]= av_malloc(rl->vlc.table_size*sizeof(RL_VLC_ELEM));
1651 for(i=0; i<rl->vlc.table_size; i++){
1652 int code= rl->vlc.table[i][0];
1653 int len = rl->vlc.table[i][1];
1654 int level, run;
1655
1656 if(len==0){ // illegal code
1657 run= 65;
1658 level= MAX_LEVEL;
1659 }else if(len<0){ //more bits needed
1660 run= 0;
1661 level= code;
1662 }else{
1663 if(code==rl->n){ //esc
1664 run= 65;
1665 level= 0;
1666 }else{
1667 run= rl->table_run [code] + 1;
1668 level= rl->table_level[code] * qmul + qadd;
1669 if(code >= rl->last) run+=192;
1670 }
1671 }
1672 rl->rl_vlc[q][i].len= len;
1673 rl->rl_vlc[q][i].level= level;
1674 rl->rl_vlc[q][i].run= run;
1675 }
1676 }
1677 }
1678
1679 /* init vlcs */
1680
1681 /* XXX: find a better solution to handle static init */
1682 void h263_decode_init_vlc(MpegEncContext *s)
1683 {
1684 static int done = 0;
1685
1686 if (!done) {
1687 done = 1;
1688
1689 init_vlc(&intra_MCBPC_vlc, INTRA_MCBPC_VLC_BITS, 8,
1690 intra_MCBPC_bits, 1, 1,
1691 intra_MCBPC_code, 1, 1);
1692 init_vlc(&inter_MCBPC_vlc, INTER_MCBPC_VLC_BITS, 25,
1693 inter_MCBPC_bits, 1, 1,
1694 inter_MCBPC_code, 1, 1);
1695 init_vlc(&cbpy_vlc, CBPY_VLC_BITS, 16,
1696 &cbpy_tab[0][1], 2, 1,
1697 &cbpy_tab[0][0], 2, 1);
1698 init_vlc(&mv_vlc, MV_VLC_BITS, 33,
1699 &mvtab[0][1], 2, 1,
1700 &mvtab[0][0], 2, 1);
1701 init_rl(&rl_inter);
1702 init_rl(&rl_intra);
1703 init_rl(&rl_intra_aic);
1704 init_vlc_rl(&rl_inter);
1705 init_vlc_rl(&rl_intra);
1706 init_vlc_rl(&rl_intra_aic);
1707 init_vlc(&dc_lum, DC_VLC_BITS, 9 /* 13 */,
1708 &DCtab_lum[0][1], 2, 1,
1709 &DCtab_lum[0][0], 2, 1);
1710 init_vlc(&dc_chrom, DC_VLC_BITS, 9 /* 13 */,
1711 &DCtab_chrom[0][1], 2, 1,
1712 &DCtab_chrom[0][0], 2, 1);
1713 init_vlc(&sprite_trajectory, SPRITE_TRAJ_VLC_BITS, 15,
1714 &sprite_trajectory_tab[0][1], 4, 2,
1715 &sprite_trajectory_tab[0][0], 4, 2);
1716 init_vlc(&mb_type_b_vlc, MB_TYPE_B_VLC_BITS, 4,
1717 &mb_type_b_tab[0][1], 2, 1,
1718 &mb_type_b_tab[0][0], 2, 1);
1719 }
1720 }
1721
1722 int h263_decode_gob_header(MpegEncContext *s)
1723 {
1724 unsigned int val, gfid;
1725
1726 /* Check for GOB Start Code */
1727 val = show_bits(&s->gb, 16);
1728 if (val == 0) {
1729 /* We have a GBSC probably with GSTUFF */
1730 skip_bits(&s->gb, 16); /* Drop the zeros */
1731 while (get_bits1(&s->gb) == 0); /* Seek the '1' bit */
1732 #ifdef DEBUG
1733 fprintf(stderr,"\nGOB Start Code at MB %d\n", (s->mb_y * s->mb_width) + s->mb_x);
1734 #endif
1735 s->gob_number = get_bits(&s->gb, 5); /* GN */
1736 gfid = get_bits(&s->gb, 2); /* GFID */
1737 s->qscale = get_bits(&s->gb, 5); /* GQUANT */
1738 #ifdef DEBUG
1739 fprintf(stderr, "\nGN: %u GFID: %u Quant: %u\n", s->gob_number, gfid, s->qscale);
1740 #endif
1741 return 1;
1742 }
1743 return 0;
1744
1745 }
1746
1747 static inline void memsetw(short *tab, int val, int n)
1748 {
1749 int i;
1750 for(i=0;i<n;i++)
1751 tab[i] = val;
1752 }
1753
1754 void ff_mpeg4_init_partitions(MpegEncContext *s)
1755 {
1756 init_put_bits(&s->tex_pb, s->tex_pb_buffer, PB_BUFFER_SIZE, NULL, NULL);
1757 init_put_bits(&s->pb2 , s->pb2_buffer , PB_BUFFER_SIZE, NULL, NULL);
1758 }
1759
1760 void ff_mpeg4_merge_partitions(MpegEncContext *s)
1761 {
1762 const int pb2_len = get_bit_count(&s->pb2 );
1763 const int tex_pb_len= get_bit_count(&s->tex_pb);
1764 const int bits= get_bit_count(&s->pb);
1765
1766 if(s->pict_type==I_TYPE){
1767 put_bits(&s->pb, 19, DC_MARKER);
1768 s->misc_bits+=19 + pb2_len + bits - s->last_bits;
1769 s->i_tex_bits+= tex_pb_len;
1770 }else{
1771 put_bits(&s->pb, 17, MOTION_MARKER);
1772 s->misc_bits+=17 + pb2_len;;
1773 s->mv_bits+= bits - s->last_bits;
1774 s->p_tex_bits+= tex_pb_len;
1775 }
1776
1777 flush_put_bits(&s->pb2);
1778 flush_put_bits(&s->tex_pb);
1779
1780 ff_copy_bits(&s->pb, s->pb2_buffer , pb2_len);
1781 ff_copy_bits(&s->pb, s->tex_pb_buffer, tex_pb_len);
1782 s->last_bits= get_bit_count(&s->pb);
1783 }
1784
1785 void ff_mpeg4_encode_video_packet_header(MpegEncContext *s)
1786 {
1787 int mb_num_bits= av_log2(s->mb_num - 1) + 1;
1788
1789 ff_mpeg4_stuffing(&s->pb);
1790 if(s->pict_type==I_TYPE)
1791 put_bits(&s->pb, 16, 0);
1792 else if(s->pict_type==B_TYPE)
1793 put_bits(&s->pb, MAX(MAX(s->f_code, s->b_code)+15, 17), 0);
1794 else /* S/P_TYPE */
1795 put_bits(&s->pb, s->f_code+15, 0);
1796 put_bits(&s->pb, 1, 1);
1797
1798 put_bits(&s->pb, mb_num_bits, s->mb_x + s->mb_y*s->mb_width);
1799 put_bits(&s->pb, 5, s->qscale);
1800 put_bits(&s->pb, 1, 0); /* no HEC */
1801 }
1802
1803 /**
1804 * decodes the next video packet and sets s->next_qscale
1805 * returns mb_num of the next packet or <0 if something went wrong
1806 */
1807 static int decode_video_packet_header(MpegEncContext *s, GetBitContext *gb)
1808 {
1809 int bits;
1810 int mb_num_bits= av_log2(s->mb_num - 1) + 1;
1811 int header_extension=0, mb_num;
1812 //printf("%X\n", show_bits(&gb, 24));
1813 //printf("parse_video_packet_header\n");
1814 // if(show_aligned_bits(gb, 1, 16) != 0) return -1;
1815
1816 /* is there enough space left for a video packet + header */
1817 if( get_bits_count(gb) > gb->size*8-20) return -1;
1818
1819 //printf("resync at %d %d\n", s->mb_x, s->mb_y);
1820 // skip_bits(gb, 1);
1821 // align_get_bits(gb);
1822 if(get_bits(gb, 16)!=0){
1823 printf("internal error while decoding video packet header\n");
1824 }
1825
1826 //printf("%X\n", show_bits(gb, 24));
1827 bits=0;
1828 while(!get_bits1(gb) && bits<30) bits++;
1829 if((s->pict_type == P_TYPE || s->pict_type == S_TYPE) && bits != s->f_code-1){
1830 printf("marker does not match f_code (is: %d should be: %d pos: %d end %d x: %d y: %d)\n",
1831 bits+1, s->f_code, get_bits_count(gb), gb->size*8, s->mb_x, s->mb_y);
1832 return -1;
1833 }else if(s->pict_type == I_TYPE && bits != 0){
1834 printf("marker too long\n");
1835 return -1;
1836 }else if(s->pict_type == B_TYPE && bits != MAX(MAX(s->f_code, s->b_code)-1, 1)){
1837 printf("marker does not match f/b_code\n");
1838 return -1;
1839 }
1840 //printf("%X\n", show_bits(gb, 24));
1841
1842 if(s->shape != RECT_SHAPE){
1843 header_extension= get_bits1(gb);
1844 //FIXME more stuff here
1845 }
1846
1847 mb_num= get_bits(gb, mb_num_bits);
1848 if(mb_num < s->mb_x + s->mb_y*s->mb_width || mb_num>=s->mb_num){
1849 fprintf(stderr, "illegal mb_num in video packet (%d %d) \n", mb_num, s->mb_x + s->mb_y*s->mb_width);
1850 return -1;
1851 }
1852
1853 if(s->shape != BIN_ONLY_SHAPE){
1854 s->next_resync_qscale= get_bits(gb, 5);
1855 if(s->next_resync_qscale==0)
1856 s->next_resync_qscale= s->qscale;
1857 if(s->next_resync_qscale==0){
1858 fprintf(stderr, "qscale==0\n");
1859 return -1;
1860 }
1861 }
1862
1863 if(s->shape == RECT_SHAPE){
1864 header_extension= get_bits1(gb);
1865 }
1866 if(header_extension){
1867 int time_increment;
1868 int time_incr=0;
1869 printf("header extension not supported\n");
1870 return -1;
1871
1872 while (get_bits1(gb) != 0)
1873 time_incr++;
1874
1875 check_marker(gb, "before time_increment in video packed header");
1876 time_increment= get_bits(gb, s->time_increment_bits);
1877 if(s->pict_type!=B_TYPE){
1878 s->last_time_base= s->time_base;
1879 s->time_base+= time_incr;
1880 s->time= s->time_base*s->time_increment_resolution + time_increment;
1881 s->pp_time= s->time - s->last_non_b_time;
1882 s->last_non_b_time= s->time;
1883 }else{
1884 s->time= (s->last_time_base + time_incr)*s->time_increment_resolution + time_increment;
1885 s->bp_time= s->last_non_b_time - s->time;
1886 }
1887 check_marker(gb, "before vop_coding_type in video packed header");
1888
1889 skip_bits(gb, 2); /* vop coding type */
1890 //FIXME not rect stuff here
1891
1892 if(s->shape != BIN_ONLY_SHAPE){
1893 skip_bits(gb, 3); /* intra dc vlc threshold */
1894
1895 if(s->pict_type == S_TYPE && s->vol_sprite_usage==GMC_SPRITE && s->num_sprite_warping_points){
1896 mpeg4_decode_sprite_trajectory(s);
1897 }
1898
1899 //FIXME reduced res stuff here
1900
1901 if (s->pict_type != I_TYPE) {
1902 s->f_code = get_bits(gb, 3); /* fcode_for */
1903 if(s->f_code==0){
1904 printf("Error, video packet header damaged or not MPEG4 header (f_code=0)\n");
1905 return -1; // makes no sense to continue, as the MV decoding will break very quickly
1906 }
1907 }
1908 if (s->pict_type == B_TYPE) {
1909 s->b_code = get_bits(gb, 3);
1910 }
1911 }
1912 }
1913 //FIXME new-pred stuff
1914
1915 //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));
1916
1917 return mb_num;
1918 }
1919
1920 void ff_mpeg4_clean_buffers(MpegEncContext *s)
1921 {
1922 int c_wrap, c_xy, l_wrap, l_xy;
1923
1924 l_wrap= s->block_wrap[0];
1925 l_xy= s->mb_y*l_wrap*2 + s->mb_x*2;
1926 c_wrap= s->block_wrap[4];
1927 c_xy= s->mb_y*c_wrap + s->mb_x;
1928
1929 /* clean DC */
1930 memsetw(s->dc_val[0] + l_xy, 1024, l_wrap*2+1);
1931 memsetw(s->dc_val[1] + c_xy, 1024, c_wrap+1);
1932 memsetw(s->dc_val[2] + c_xy, 1024, c_wrap+1);
1933
1934 /* clean AC */
1935 memset(s->ac_val[0] + l_xy, 0, (l_wrap*2+1)*16*sizeof(INT16));
1936 memset(s->ac_val[1] + c_xy, 0, (c_wrap +1)*16*sizeof(INT16));
1937 memset(s->ac_val[2] + c_xy, 0, (c_wrap +1)*16*sizeof(INT16));
1938
1939 /* clean MV */
1940 // we cant clear the MVs as they might be needed by a b frame
1941 // memset(s->motion_val + l_xy, 0, (l_wrap*2+1)*2*sizeof(INT16));
1942 // memset(s->motion_val, 0, 2*sizeof(INT16)*(2 + s->mb_width*2)*(2 + s->mb_height*2));
1943 s->last_mv[0][0][0]=
1944 s->last_mv[0][0][1]=
1945 s->last_mv[1][0][0]=
1946 s->last_mv[1][0][1]= 0;
1947 }
1948
1949 /* searches for the next resync marker clears ac,dc,mc, and sets s->next_resync_gb, s->mb_num_left */
1950 int ff_mpeg4_resync(MpegEncContext *s)
1951 {
1952 GetBitContext gb;
1953
1954 /* search & parse next resync marker */
1955
1956 gb= s->next_resync_gb;
1957 align_get_bits(&gb);
1958 //printf("mpeg4_resync %d next:%d \n", get_bits_count(&gb), get_bits_count(&s->next_resync_gb));
1959 for(;;) {
1960 int v= show_bits(&gb, 24);
1961 if( get_bits_count(&gb) >= gb.size*8-24 || v == 1 /* start-code */){
1962 s->mb_num_left= s->mb_num - s->mb_x - s->mb_y*s->mb_width;
1963 //printf("mpeg4_resync end\n");
1964 s->gb= s->next_resync_gb; //continue at the next resync marker
1965 return -1;
1966 }else if(v>>8 == 0){
1967 int next;
1968 s->next_resync_pos= get_bits_count(&gb);
1969
1970 next= decode_video_packet_header(s, &gb);
1971 if(next >= 0){
1972 s->mb_num_left= next - s->mb_x - s->mb_y*s->mb_width;
1973 break;
1974 }
1975
1976 align_get_bits(&gb);
1977 }
1978 skip_bits(&gb, 8);
1979 }
1980 s->next_resync_gb=gb;
1981
1982 return 0;
1983 }
1984
1985 static inline void init_block_index(MpegEncContext *s)
1986 {
1987 s->block_index[0]= s->block_wrap[0]*(s->mb_y*2 + 1) - 1 + s->mb_x*2;
1988 s->block_index[1]= s->block_wrap[0]*(s->mb_y*2 + 1) + s->mb_x*2;
1989 s->block_index[2]= s->block_wrap[0]*(s->mb_y*2 + 2) - 1 + s->mb_x*2;
1990 s->block_index[3]= s->block_wrap[0]*(s->mb_y*2 + 2) + s->mb_x*2;
1991 s->block_index[4]= s->block_wrap[4]*(s->mb_y + 1) + s->block_wrap[0]*(s->mb_height*2 + 2) + s->mb_x;
1992 s->block_index[5]= s->block_wrap[4]*(s->mb_y + 1 + s->mb_height + 2) + s->block_wrap[0]*(s->mb_height*2 + 2) + s->mb_x;
1993 }
1994
1995 static inline void update_block_index(MpegEncContext *s)
1996 {
1997 s->block_index[0]+=2;
1998 s->block_index[1]+=2;
1999 s->block_index[2]+=2;
2000 s->block_index[3]+=2;
2001 s->block_index[4]++;
2002 s->block_index[5]++;
2003 }
2004
2005 /**
2006 * decodes the first & second partition
2007 * returns error type or 0 if no error
2008 */
2009 int ff_mpeg4_decode_partitions(MpegEncContext *s)
2010 {
2011 static const INT8 quant_tab[4] = { -1, -2, 1, 2 };
2012 int mb_num;
2013
2014 /* decode first partition */
2015 mb_num=0;
2016 s->first_slice_line=1;
2017 s->mb_x= s->resync_mb_x;
2018 for(s->mb_y= s->resync_mb_y; mb_num < s->mb_num_left; s->mb_y++){
2019 init_block_index(s);
2020 for(; mb_num < s->mb_num_left && s->mb_x<s->mb_width; s->mb_x++){
2021 const int xy= s->mb_x + s->mb_y*s->mb_width;
2022 int cbpc;
2023 int dir=0;
2024
2025 mb_num++;
2026 update_block_index(s);
2027 if(s->mb_x == s->resync_mb_x && s->mb_y == s->resync_mb_y+1)
2028 s->first_slice_line=0;
2029
2030 if(s->mb_x==0) PRINT_MB_TYPE("\n");
2031
2032 if(s->pict_type==I_TYPE){
2033 int i;
2034
2035 PRINT_MB_TYPE("I");
2036 cbpc = get_vlc2(&s->gb, intra_MCBPC_vlc.table, INTRA_MCBPC_VLC_BITS, 1);
2037 if (cbpc < 0){
2038 fprintf(stderr, "cbpc corrupted at %d %d\n", s->mb_x, s->mb_y);
2039 return DECODING_DESYNC;
2040 }
2041 s->cbp_table[xy]= cbpc & 3;
2042 s->mb_type[xy]= MB_TYPE_INTRA;
2043 s->mb_intra = 1;
2044
2045 if(cbpc & 4) {
2046 s->qscale += quant_tab[get_bits(&s->gb, 2)];
2047 if (s->qscale < 1)
2048 s->qscale = 1;
2049 else if (s->qscale > 31)
2050 s->qscale = 31;
2051 h263_dc_scale(s);
2052 }
2053 s->qscale_table[xy]= s->qscale;
2054
2055 s->mbintra_table[xy]= 1;
2056 for(i=0; i<6; i++){
2057 int dc_pred_dir;
2058 int dc= mpeg4_decode_dc(s, i, &dc_pred_dir);
2059 if(dc < 0){
2060 fprintf(stderr, "DC corrupted at %d %d\n", s->mb_x, s->mb_y);
2061 return DECODING_DESYNC;
2062 }
2063 dir<<=1;
2064 if(dc_pred_dir) dir|=1;
2065 }
2066 s->pred_dir_table[xy]= dir;
2067 }else{ /* P/S_TYPE */
2068 int mx, my, pred_x, pred_y;
2069 INT16 * const mot_val= s->motion_val[s->block_index[0]];
2070 const int stride= s->block_wrap[0]*2;
2071
2072 if(get_bits1(&s->gb)){
2073 /* skip mb */
2074 s->mb_type[xy]= MB_TYPE_SKIPED;
2075 if(s->pict_type==S_TYPE && s->vol_sprite_usage==GMC_SPRITE){
2076 const int a= s->sprite_warping_accuracy;
2077 PRINT_MB_TYPE("G");
2078 if(s->divx_version==500 && s->divx_build==413){
2079 mx = s->sprite_offset[0][0] / (1<<(a-s->quarter_sample));
2080 my = s->sprite_offset[0][1] / (1<<(a-s->quarter_sample));
2081 }else{
2082 mx = RSHIFT(s->sprite_offset[0][0], a-s->quarter_sample);
2083 my = RSHIFT(s->sprite_offset[0][1], a-s->quarter_sample);
2084 s->mb_type[xy]= MB_TYPE_GMC | MB_TYPE_SKIPED;
2085 }
2086 }else{
2087 PRINT_MB_TYPE("S");
2088 mx = 0;
2089 my = 0;
2090 }
2091 mot_val[0 ]= mot_val[2 ]=
2092 mot_val[0+stride]= mot_val[2+stride]= mx;
2093 mot_val[1 ]= mot_val[3 ]=
2094 mot_val[1+stride]= mot_val[3+stride]= my;
2095
2096 if(s->mbintra_table[xy])
2097 ff_clean_intra_table_entries(s);
2098
2099 continue;
2100 }
2101 cbpc = get_vlc2(&s->gb, inter_MCBPC_vlc.table, INTER_MCBPC_VLC_BITS, 2);
2102 if (cbpc < 0){
2103 fprintf(stderr, "cbpc corrupted at %d %d\n", s->mb_x, s->mb_y);
2104 return DECODING_DESYNC;
2105 }
2106 if (cbpc > 20)
2107 cbpc+=3;
2108 else if (cbpc == 20)
2109 fprintf(stderr, "Stuffing !");
2110 s->cbp_table[xy]= cbpc&(8+3); //8 is dquant
2111
2112 s->mb_intra = ((cbpc & 4) != 0);
2113
2114 if(s->mb_intra){
2115 PRINT_MB_TYPE("I");
2116 s->mbintra_table[xy]= 1;
2117 s->mb_type[xy]= MB_TYPE_INTRA;
2118 mot_val[0 ]= mot_val[2 ]=
2119 mot_val[0+stride]= mot_val[2+stride]= 0;
2120 mot_val[1 ]= mot_val[3 ]=
2121 mot_val[1+stride]= mot_val[3+stride]= 0;
2122 }else{
2123 if(s->mbintra_table[xy])
2124 ff_clean_intra_table_entries(s);
2125
2126 if(s->pict_type==S_TYPE && s->vol_sprite_usage==GMC_SPRITE && (cbpc & 16) == 0)
2127 s->mcsel= get_bits1(&s->gb);
2128 else s->mcsel= 0;
2129
2130 if ((cbpc & 16) == 0) {
2131 PRINT_MB_TYPE("P");
2132 /* 16x16 motion prediction */
2133 s->mb_type[xy]= MB_TYPE_INTER;
2134
2135 h263_pred_motion(s, 0, &pred_x, &pred_y);
2136 if(!s->mcsel)
2137 mx = h263_decode_motion(s, pred_x, s->f_code);
2138 else {
2139 const int a= s->sprite_warping_accuracy;
2140 if(s->divx_version==500 && s->divx_build==413){
2141 mx = s->sprite_offset[0][0] / (1<<(a-s->quarter_sample));
2142 }else{
2143 mx = RSHIFT(s->sprite_offset[0][0], a-s->quarter_sample);
2144 }
2145 }
2146 if (mx >= 0xffff)
2147 return DECODING_DESYNC;
2148
2149 if(!s->mcsel)
2150 my = h263_decode_motion(s, pred_y, s->f_code);
2151 else{
2152 const int a= s->sprite_warping_accuracy;
2153 if(s->divx_version==500 && s->divx_build==413){
2154 my = s->sprite_offset[0][1] / (1<<(a-s->quarter_sample));
2155 }else{
2156 my = RSHIFT(s->sprite_offset[0][1], a-s->quarter_sample);
2157 }
2158 }
2159 if (my >= 0xffff)
2160 return DECODING_DESYNC;
2161 mot_val[0 ]= mot_val[2 ] =
2162 mot_val[0+stride]= mot_val[2+stride]= mx;
2163 mot_val[1 ]= mot_val[3 ]=
2164 mot_val[1+stride]= mot_val[3+stride]= my;
2165 } else {
2166 int i;
2167 PRINT_MB_TYPE("4");
2168 s->mb_type[xy]= MB_TYPE_INTER4V;
2169 for(i=0;i<4;i++) {
2170 INT16 *mot_val= h263_pred_motion(s, i, &pred_x, &pred_y);
2171 mx = h263_decode_motion(s, pred_x, s->f_code);
2172 if (mx >= 0xffff)
2173 return DECODING_DESYNC;
2174
2175 my = h263_decode_motion(s, pred_y, s->f_code);
2176 if (my >= 0xffff)
2177 return DECODING_DESYNC;
2178 mot_val[0] = mx;
2179 mot_val[1] = my;
2180 }
2181 }
2182 }
2183 }
2184 }
2185 s->mb_x= 0;
2186 }
2187
2188 if (s->pict_type==I_TYPE && get_bits(&s->gb, 19)!=DC_MARKER ) s->decoding_error= DECODING_DESYNC;
2189 else if(s->pict_type!=I_TYPE && get_bits(&s->gb, 17)!=MOTION_MARKER) s->decoding_error= DECODING_DESYNC;
2190 if(s->decoding_error== DECODING_DESYNC){
2191 fprintf(stderr, "marker missing after first partition at %d %d\n", s->mb_x, s->mb_y);
2192 return DECODING_DESYNC;
2193 }
2194
2195 /* decode second partition */
2196 mb_num=0;
2197 s->mb_x= s->resync_mb_x;
2198 for(s->mb_y= s->resync_mb_y; mb_num < s->mb_num_left; s->mb_y++){
2199 init_block_index(s);
2200 for(; mb_num < s->mb_num_left && s->mb_x<s->mb_width; s->mb_x++){
2201 const int xy= s->mb_x + s->mb_y*s->mb_width;
2202
2203 mb_num++;
2204 update_block_index(s);
2205
2206 if(s->pict_type==I_TYPE){
2207 int ac_pred= get_bits1(&s->gb);
2208 int cbpy = get_vlc2(&s->gb, cbpy_vlc.table, CBPY_VLC_BITS, 1);
2209 if(cbpy<0){
2210 fprintf(stderr, "cbpy corrupted at %d %d\n", s->mb_x, s->mb_y);
2211 return DECODING_AC_LOST;
2212 }
2213
2214 s->cbp_table[xy]|= cbpy<<2;
2215 s->pred_dir_table[xy]|= ac_pred<<7;
2216 }else{ /* P || S_TYPE */
2217 if(s->mb_type[xy]&MB_TYPE_INTRA){
2218 int dir=0,i;
2219 int ac_pred = get_bits1(&s->gb);
2220 int cbpy = get_vlc2(&s->gb, cbpy_vlc.table, CBPY_VLC_BITS, 1);
2221
2222 if(cbpy<0){
2223 fprintf(stderr, "I cbpy corrupted at %d %d\n", s->mb_x, s->mb_y);
2224 return DECODING_ACDC_LOST;
2225 }
2226
2227 if(s->cbp_table[xy] & 8) {
2228 s->qscale += quant_tab[get_bits(&s->gb, 2)];
2229 if (s->qscale < 1)
2230 s->qscale = 1;
2231 else if (s->qscale > 31)
2232 s->qscale = 31;
2233 h263_dc_scale(s);
2234 }
2235 s->qscale_table[xy]= s->qscale;
2236
2237 for(i=0; i<6; i++){
2238 int dc_pred_dir;
2239 int dc= mpeg4_decode_dc(s, i, &dc_pred_dir);
2240 if(dc < 0){
2241 fprintf(stderr, "DC corrupted at %d %d\n", s->mb_x, s->mb_y);
2242 return DECODING_ACDC_LOST;
2243 }
2244 dir<<=1;
2245 if(dc_pred_dir) dir|=1;
2246 }
2247 s->cbp_table[xy]&= 3; //remove dquant
2248 s->cbp_table[xy]|= cbpy<<2;
2249 s->pred_dir_table[xy]= dir | (ac_pred<<7);
2250 }else if(s->mb_type[xy]&MB_TYPE_SKIPED){
2251 s->qscale_table[xy]= s->qscale;
2252 s->cbp_table[xy]= 0;
2253 }else{
2254 int cbpy = get_vlc2(&s->gb, cbpy_vlc.table, CBPY_VLC_BITS, 1);
2255
2256 if(cbpy<0){
2257 fprintf(stderr, "P cbpy corrupted at %d %d\n", s->mb_x, s->mb_y);
2258 return DECODING_ACDC_LOST;
2259 }
2260
2261 if(s->cbp_table[xy] & 8) {
2262 //fprintf(stderr, "dquant\n");
2263 s->qscale += quant_tab[get_bits(&s->gb, 2)];
2264 if (s->qscale < 1)
2265 s->qscale = 1;
2266 else if (s->qscale > 31)
2267 s->qscale = 31;
2268 h263_dc_scale(s);
2269 }
2270 s->qscale_table[xy]= s->qscale;
2271
2272 s->cbp_table[xy]&= 3; //remove dquant
2273 s->cbp_table[xy]|= (cbpy^0xf)<<2;
2274 }
2275 }
2276 }
2277 s->mb_x= 0;
2278 }
2279
2280
2281 return 0;
2282 }
2283
2284 static int mpeg4_decode_partitioned_mb(MpegEncContext *s,
2285 DCTELEM block[6][64])
2286 {
2287 int cbp, mb_type;
2288 const int xy= s->mb_x + s->mb_y*s->mb_width;
2289
2290 if(s->mb_x==s->resync_mb_x && s->mb_y==s->resync_mb_y){ //Note resync_mb_{x,y}==0 at the start
2291 int i;
2292 int block_index_backup[6];
2293 int qscale= s->qscale;
2294
2295 for(i=0; i<6; i++) block_index_backup[i]= s->block_index[i];
2296
2297 s->decoding_error= ff_mpeg4_decode_partitions(s);
2298
2299 for(i=0; i<6; i++) s->block_index[i]= block_index_backup[i];
2300 s->first_slice_line=1;
2301 s->mb_x= s->resync_mb_x;
2302 s->mb_y= s->resync_mb_y;
2303 s->qscale= qscale;
2304 h263_dc_scale(s);
2305
2306 if(s->decoding_error==DECODING_DESYNC) return -1;
2307 }
2308
2309 mb_type= s->mb_type[xy];
2310 if(s->decoding_error)
2311 cbp=0;
2312 else
2313 cbp = s->cbp_table[xy];
2314
2315 if(s->decoding_error!=DECODING_ACDC_LOST && s->qscale_table[xy] != s->qscale){
2316 s->qscale= s->qscale_table[xy];
2317 h263_dc_scale(s);
2318 }
2319
2320 if (s->pict_type == P_TYPE || s->pict_type==S_TYPE) {
2321 int i;
2322 for(i=0; i<4; i++){
2323 s->mv[0][i][0] = s->motion_val[ s->block_index[i] ][0];
2324 s->mv[0][i][1] = s->motion_val[ s->block_index[i] ][1];
2325 }
2326 s->mb_intra = mb_type&MB_TYPE_INTRA;
2327
2328 if (mb_type&MB_TYPE_SKIPED) {
2329 /* skip mb */
2330 for(i=0;i<6;i++)
2331 s->block_last_index[i] = -1;
2332 s->mv_dir = MV_DIR_FORWARD;
2333 s->mv_type = MV_TYPE_16X16;
2334 if(s->pict_type==S_TYPE && s->vol_sprite_usage==GMC_SPRITE){
2335 s->mcsel=1;
2336 s->mb_skiped = 0;
2337 }else{
2338 s->mcsel=0;
2339 s->mb_skiped = 1;
2340 }
2341 return 0;
2342 }else if(s->mb_intra && s->decoding_error!=DECODING_ACDC_LOST){
2343 s->ac_pred = s->pred_dir_table[xy]>>7;
2344
2345 /* decode each block */
2346 for (i = 0; i < 6; i++) {
2347 int ret= mpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1);
2348 if(ret==DECODING_AC_LOST){
2349 fprintf(stderr, "texture corrupted at %d %d (trying to continue with mc/dc only)\n", s->mb_x, s->mb_y);
2350 s->decoding_error=DECODING_AC_LOST;
2351 cbp=0;
2352 }else if(ret==DECODING_ACDC_LOST){
2353 fprintf(stderr, "dc corrupted at %d %d (trying to continue with mc only)\n", s->mb_x, s->mb_y);
2354 s->decoding_error=DECODING_ACDC_LOST;
2355 break;
2356 }
2357 }
2358 }else if(!s->mb_intra){
2359 // s->mcsel= 0; //FIXME do we need to init that
2360
2361 s->mv_dir = MV_DIR_FORWARD;
2362 if (mb_type&MB_TYPE_INTER4V) {
2363 s->mv_type = MV_TYPE_8X8;
2364 } else {
2365 s->mv_type = MV_TYPE_16X16;
2366 }
2367 if(s->decoding_error==0 && cbp){
2368 /* decode each block */
2369 for (i = 0; i < 6; i++) {
2370 int ret= mpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1);
2371 if(ret==DECODING_AC_LOST){
2372 fprintf(stderr, "texture corrupted at %d %d (trying to continue with mc/dc only)\n", s->mb_x, s->mb_y);
2373 s->decoding_error=DECODING_AC_LOST;
2374 break;
2375 }
2376 }
2377 }
2378 }
2379 } else { /* I-Frame */
2380 int i;
2381 s->mb_intra = 1;
2382 s->ac_pred = s->pred_dir_table[xy]>>7;
2383
2384 /* decode each block */
2385 for (i = 0; i < 6; i++) {
2386 int ret= mpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1);
2387 if(ret==DECODING_AC_LOST){
2388 fprintf(stderr, "texture corrupted at %d %d (trying to continue with dc only)\n", s->mb_x, s->mb_y);
2389 s->decoding_error=DECODING_AC_LOST;
2390 cbp=0;
2391 }else if(ret==DECODING_ACDC_LOST){
2392 fprintf(stderr, "dc corrupted at %d %d\n", s->mb_x, s->mb_y);
2393 return -1;
2394 }
2395 }
2396 }
2397
2398 return 0;
2399 }
2400
2401
2402 int h263_decode_mb(MpegEncContext *s,
2403 DCTELEM block[6][64])
2404 {
2405 int cbpc, cbpy, i, cbp, pred_x, pred_y, mx, my, dquant;
2406 INT16 *mot_val;
2407 static INT8 quant_tab[4] = { -1, -2, 1, 2 };
2408
2409 if(s->mb_x==0) PRINT_MB_TYPE("\n");
2410
2411 if(s->resync_marker){
2412 if(s->resync_mb_x == s->mb_x && s->resync_mb_y+1 == s->mb_y){
2413 s->first_slice_line=0;
2414 }
2415 }
2416
2417 if(s->data_partitioning && s->pict_type!=B_TYPE)
2418 return mpeg4_decode_partitioned_mb(s, block);
2419
2420 if (s->pict_type == P_TYPE || s->pict_type==S_TYPE) {
2421 if (get_bits1(&s->gb)) {
2422 /* skip mb */
2423 s->mb_intra = 0;
2424 for(i=0;i<6;i++)
2425 s->block_last_index[i] = -1;
2426 s->mv_dir = MV_DIR_FORWARD;
2427 s->mv_type = MV_TYPE_16X16;
2428 if(s->pict_type==S_TYPE && s->vol_sprite_usage==GMC_SPRITE){
2429 const int a= s->sprite_warping_accuracy;
2430 // int l = (1 << (s->f_code - 1)) * 32;
2431 PRINT_MB_TYPE("G");
2432 s->mcsel=1;
2433 if(s->divx_version==500 && s->divx_build==413){
2434 s->mv[0][0][0] = s->sprite_offset[0][0] / (1<<(a-s->quarter_sample));
2435 s->mv[0][0][1] = s->sprite_offset[0][1] / (1<<(a-s->quarter_sample));
2436 }else{
2437 s->mv[0][0][0] = RSHIFT(s->sprite_offset[0][0], a-s->quarter_sample);
2438 s->mv[0][0][1] = RSHIFT(s->sprite_offset[0][1], a-s->quarter_sample);
2439 }
2440 /* if (s->mv[0][0][0] < -l) s->mv[0][0][0]= -l;
2441 else if (s->mv[0][0][0] >= l) s->mv[0][0][0]= l-1;
2442 if (s->mv[0][0][1] < -l) s->mv[0][0][1]= -l;
2443 else if (s->mv[0][0][1] >= l) s->mv[0][0][1]= l-1;*/
2444
2445 s->mb_skiped = 0;
2446 }else{
2447 PRINT_MB_TYPE("S");
2448 s->mcsel=0;
2449 s->mv[0][0][0] = 0;
2450 s->mv[0][0][1] = 0;
2451 s->mb_skiped = 1;
2452 }
2453 return 0;
2454 }
2455 cbpc = get_vlc2(&s->gb, inter_MCBPC_vlc.table, INTER_MCBPC_VLC_BITS, 2);
2456 //fprintf(stderr, "\tCBPC: %d", cbpc);
2457 if (cbpc < 0)
2458 return -1;
2459 if (cbpc > 20)
2460 cbpc+=3;
2461 else if (cbpc == 20)
2462 fprintf(stderr, "Stuffing !");
2463
2464 dquant = cbpc & 8;
2465 s->mb_intra = ((cbpc & 4) != 0);
2466 if (s->mb_intra) goto intra;
2467
2468 if(s->pict_type==S_TYPE && s->vol_sprite_usage==GMC_SPRITE && (cbpc & 16) == 0)
2469 s->mcsel= get_bits1(&s->gb);
2470 else s->mcsel= 0;
2471 cbpy = get_vlc2(&s->gb, cbpy_vlc.table, CBPY_VLC_BITS, 1);
2472 cbp = (cbpc & 3) | ((cbpy ^ 0xf) << 2);
2473 if (dquant) {
2474 s->qscale += quant_tab[get_bits(&s->gb, 2)];
2475 if (s->qscale < 1)
2476 s->qscale = 1;
2477 else if (s->qscale > 31)
2478 s->qscale = 31;
2479 h263_dc_scale(s);
2480 }
2481 s->mv_dir = MV_DIR_FORWARD;
2482 if ((cbpc & 16) == 0) {
2483 PRINT_MB_TYPE("P");
2484 /* 16x16 motion prediction */
2485 s->mv_type = MV_TYPE_16X16;
2486 h263_pred_motion(s, 0, &pred_x, &pred_y);
2487 if (s->umvplus_dec)
2488 mx = h263p_decode_umotion(s, pred_x);
2489 else if(!s->mcsel)
2490 mx = h263_decode_motion(s, pred_x, s->f_code);
2491 else {
2492 const int a= s->sprite_warping_accuracy;
2493 // int l = (1 << (s->f_code - 1)) * 32;
2494 if(s->divx_version==500 && s->divx_build==413){
2495 mx = s->sprite_offset[0][0] / (1<<(a-s->quarter_sample));
2496 }else{
2497 mx = RSHIFT(s->sprite_offset[0][0], a-s->quarter_sample);
2498 }
2499 // if (mx < -l) mx= -l, printf("C");
2500 // else if (mx >= l) mx= l-1, printf("C");
2501 }
2502 if (mx >= 0xffff)
2503 return -1;
2504
2505 if (s->umvplus_dec)
2506 my = h263p_decode_umotion(s, pred_y);
2507 else if(!s->mcsel)
2508 my = h263_decode_motion(s, pred_y, s->f_code);
2509 else{
2510 const int a= s->sprite_warping_accuracy;
2511 // int l = (1 << (s->f_code - 1)) * 32;
2512 if(s->divx_version==500 && s->divx_build==413){
2513 my = s->sprite_offset[0][1] / (1<<(a-s->quarter_sample));
2514 }else{
2515 my = RSHIFT(s->sprite_offset[0][1], a-s->quarter_sample);
2516 }
2517 // if (my < -l) my= -l, printf("C");
2518 // else if (my >= l) my= l-1, printf("C");
2519 }
2520 if (my >= 0xffff)
2521 return -1;
2522 s->mv[0][0][0] = mx;
2523 s->mv[0][0][1] = my;
2524 /*fprintf(stderr, "\n MB %d", (s->mb_y * s->mb_width) + s->mb_x);
2525 fprintf(stderr, "\n\tmvx: %d\t\tpredx: %d", mx, pred_x);
2526 fprintf(stderr, "\n\tmvy: %d\t\tpredy: %d", my, pred_y);*/
2527 if (s->umvplus_dec && (mx - pred_x) == 1 && (my - pred_y) == 1)
2528 skip_bits1(&s->gb); /* Bit stuffing to prevent PSC */
2529
2530 } else {
2531 PRINT_MB_TYPE("4");
2532 s->mv_type = MV_TYPE_8X8;
2533 for(i=0;i<4;i++) {
2534 mot_val = h263_pred_motion(s, i, &pred_x, &pred_y);
2535 if (s->umvplus_dec)
2536 mx = h263p_decode_umotion(s, pred_x);
2537 else
2538 mx = h263_decode_motion(s, pred_x, s->f_code);
2539 if (mx >= 0xffff)
2540 return -1;
2541
2542 if (s->umvplus_dec)
2543 my = h263p_decode_umotion(s, pred_y);
2544 else
2545 my = h263_decode_motion(s, pred_y, s->f_code);
2546 if (my >= 0xffff)
2547 return -1;
2548 s->mv[0][i][0] = mx;
2549 s->mv[0][i][1] = my;
2550 if (s->umvplus_dec && (mx - pred_x) == 1 && (my - pred_y) == 1)
2551 skip_bits1(&s->gb); /* Bit stuffing to prevent PSC */
2552 mot_val[0] = mx;
2553 mot_val[1] = my;
2554 }
2555 }
2556 } else if(s->pict_type==B_TYPE) {
2557 int modb1; // first bit of modb
2558 int modb2; // second bit of modb
2559 int mb_type;
2560 uint16_t time_pp;
2561 uint16_t time_pb;
2562 int xy;
2563
2564 s->mb_intra = 0; //B-frames never contain intra blocks
2565 s->mcsel=0; // ... true gmc blocks
2566
2567 if(s->mb_x==0){
2568 s->last_mv[0][0][0]=
2569 s->last_mv[0][0][1]=
2570 s->last_mv[1][0][0]=
2571 s->last_mv[1][0][1]= 0;
2572 }
2573
2574 /* if we skipped it in the future P Frame than skip it now too */
2575 s->mb_skiped= s->mbskip_table[s->mb_y * s->mb_width + s->mb_x]; // Note, skiptab=0 if last was GMC
2576
2577 if(s->mb_skiped){
2578 /* skip mb */
2579 for(i=0;i<6;i++)
2580 s->block_last_index[i] = -1;
2581
2582 s->mv_dir = MV_DIR_FORWARD;
2583 s->mv_type = MV_TYPE_16X16;
2584 s->mv[0][0][0] = 0;
2585 s->mv[0][0][1] = 0;
2586 s->mv[1][0][0] = 0;
2587 s->mv[1][0][1] = 0;
2588 //FIXME is this correct?
2589 /* s->last_mv[0][0][0]=
2590 s->last_mv[0][0][1]=0;*/
2591 PRINT_MB_TYPE("s");
2592 return 0;
2593 }
2594
2595 modb1= get_bits1(&s->gb);
2596 if(modb1==0){
2597 modb2= get_bits1(&s->gb);
2598 mb_type= get_vlc2(&s->gb, mb_type_b_vlc.table, MB_TYPE_B_VLC_BITS, 1);
2599 if(modb2==0) cbp= get_bits(&s->gb, 6);
2600 else cbp=0;
2601 if (mb_type && cbp) {
2602 if(get_bits1(&s->gb)){
2603 s->qscale +=get_bits1(&s->gb)*4 - 2;
2604 if (s->qscale < 1)
2605 s->qscale = 1;
2606 else if (s->qscale > 31)
2607 s->qscale = 31;
2608 h263_dc_scale(s);
2609 }
2610 }
2611 }else{
2612 mb_type=4; //like 0 but no vectors coded
2613 cbp=0;
2614 }
2615 s->mv_type = MV_TYPE_16X16; // we'll switch to 8x8 only if the last P frame had 8x8 for this MB and mb_type=0 here
2616 mx=my=0; //for case 4, we could put this to the mb_type=4 but than gcc compains about uninitalized mx/my
2617 switch(mb_type)
2618 {
2619 case 0: /* direct */
2620 mx = h263_decode_motion(s, 0, 1);
2621 my = h263_decode_motion(s, 0, 1);
2622 case 4: /* direct with mx=my=0 */
2623 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
2624 xy= s->block_index[0];
2625 time_pp= s->pp_time;
2626 time_pb= time_pp - s->bp_time;
2627 //if(time_pp>3000 )printf("%d %d ", time_pp, time_pb);
2628 //FIXME 4MV
2629 //FIXME avoid divides
2630 s->mv[0][0][0] = s->motion_val[xy][0]*time_pb/time_pp + mx;
2631 s->mv[0][0][1] = s->motion_val[xy][1]*time_pb/time_pp + my;
2632 s->mv[1][0][0] = mx ? s->mv[0][0][0] - s->motion_val[xy][0]
2633 : s->motion_val[xy][0]*(time_pb - time_pp)/time_pp + mx;
2634 s->mv[1][0][1] = my ? s->mv[0][0][1] - s->motion_val[xy][1]
2635 : s->motion_val[xy][1]*(time_pb - time_pp)/time_pp + my;
2636 /* s->mv[0][0][0] =
2637 s->mv[0][0][1] =
2638 s->mv[1][0][0] =
2639 s->mv[1][0][1] = 1000;*/
2640 PRINT_MB_TYPE("D");
2641 break;
2642 case 1:
2643 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
2644 mx = h263_decode_motion(s, s->last_mv[0][0][0], s->f_code);
2645 my = h263_decode_motion(s, s->last_mv[0][0][1], s->f_code);
2646 s->last_mv[0][0][0]= s->mv[0][0][0] = mx;
2647 s->last_mv[0][0][1]= s->mv[0][0][1] = my;
2648
2649 mx = h263_decode_motion(s, s->last_mv[1][0][0], s->b_code);
2650 my = h263_decode_motion(s, s->last_mv[1][0][1], s->b_code);
2651 s->last_mv[1][0][0]= s->mv[1][0][0] = mx;
2652 s->last_mv[1][0][1]= s->mv[1][0][1] = my;
2653 PRINT_MB_TYPE("i");
2654 break;
2655 case 2:
2656 s->mv_dir = MV_DIR_BACKWARD;
2657 mx = h263_decode_motion(s, s->last_mv[1][0][0], s->b_code);
2658 my = h263_decode_motion(s, s->last_mv[1][0][1], s->b_code);
2659 s->last_mv[1][0][0]= s->mv[1][0][0] = mx;
2660 s->last_mv[1][0][1]= s->mv[1][0][1] = my;
2661 PRINT_MB_TYPE("B");
2662 break;
2663 case 3:
2664 s->mv_dir = MV_DIR_FORWARD;
2665 mx = h263_decode_motion(s, s->last_mv[0][0][0], s->f_code);
2666 my = h263_decode_motion(s, s->last_mv[0][0][1], s->f_code);
2667 s->last_mv[0][0][0]= s->mv[0][0][0] = mx;
2668 s->last_mv[0][0][1]= s->mv[0][0][1] = my;
2669 PRINT_MB_TYPE("F");
2670 break;
2671 default:
2672 printf("illegal MB_type\n");
2673 return -1;
2674 }
2675 } else { /* I-Frame */
2676 cbpc = get_vlc2(&s->gb, intra_MCBPC_vlc.table, INTRA_MCBPC_VLC_BITS, 1);
2677 if (cbpc < 0)
2678 return -1;
2679 dquant = cbpc & 4;
2680 s->mb_intra = 1;
2681 intra:
2682 PRINT_MB_TYPE("I");
2683 s->ac_pred = 0;
2684 if (s->h263_pred || s->h263_aic) {
2685 s->ac_pred = get_bits1(&s->gb);
2686 if (s->ac_pred && s->h263_aic)
2687 s->h263_aic_dir = get_bits1(&s->gb);
2688 }
2689 cbpy = get_vlc2(&s->gb, cbpy_vlc.table, CBPY_VLC_BITS, 1);
2690 if(cbpy<0) return -1;
2691 cbp = (cbpc & 3) | (cbpy << 2);
2692 if (dquant) {
2693 s->qscale += quant_tab[get_bits(&s->gb, 2)];
2694 if (s->qscale < 1)
2695 s->qscale = 1;
2696 else if (s->qscale > 31)
2697 s->qscale = 31;
2698 h263_dc_scale(s);
2699 }
2700 }
2701
2702 /* decode each block */
2703 if (s->h263_pred) {
2704 for (i = 0; i < 6; i++) {
2705 if (mpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1) < 0)
2706 return -1;
2707 }
2708 } else {
2709 for (i = 0; i < 6; i++) {
2710 if (h263_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1) < 0)
2711 return -1;
2712 }
2713 }
2714 return 0;
2715 }
2716
2717 static int h263_decode_motion(MpegEncContext * s, int pred, int f_code)
2718 {
2719 int code, val, sign, shift, l, m;
2720
2721 code = get_vlc2(&s->gb, mv_vlc.table, MV_VLC_BITS, 2);
2722 if (code < 0)
2723 return 0xffff;
2724
2725 if (code == 0)
2726 return pred;
2727 sign = get_bits1(&s->gb);
2728 shift = f_code - 1;
2729 val = (code - 1) << shift;
2730 if (shift > 0)
2731 val |= get_bits(&s->gb, shift);
2732 val++;
2733 if (sign)
2734 val = -val;
2735 val += pred;
2736
2737 /* modulo decoding */
2738 if (!s->h263_long_vectors) {
2739 l = (1 << (f_code - 1)) * 32;
2740 m = 2 * l;
2741 if (val < -l) {
2742 val += m;
2743 } else if (val >= l) {
2744 val -= m;
2745 }
2746 } else {
2747 /* horrible h263 long vector mode */
2748 if (pred < -31 && val < -63)
2749 val += 64;
2750 if (pred > 32 && val > 63)
2751 val -= 64;
2752
2753 }
2754 return val;
2755 }
2756
2757 /* Decodes RVLC of H.263+ UMV */
2758 static int h263p_decode_umotion(MpegEncContext * s, int pred)
2759 {
2760 int code = 0, sign;
2761
2762 if (get_bits1(&s->gb)) /* Motion difference = 0 */
2763 return pred;
2764
2765 code = 2 + get_bits1(&s->gb);
2766
2767 while (get_bits1(&s->gb))
2768 {
2769 code <<= 1;
2770 code += get_bits1(&s->gb);
2771 }
2772 sign = code & 1;
2773 code >>= 1;
2774
2775 code = (sign) ? (pred - code) : (pred + code);
2776 #ifdef DEBUG
2777 fprintf(stderr,"H.263+ UMV Motion = %d\n", code);
2778 #endif
2779 return code;
2780
2781 }
2782
2783 static int h263_decode_block(MpegEncContext * s, DCTELEM * block,
2784 int n, int coded)
2785 {
2786 int code, level, i, j, last, run;
2787 RLTable *rl = &rl_inter;
2788 const UINT8 *scan_table;
2789
2790 scan_table = zigzag_direct;
2791 if (s->h263_aic && s->mb_intra) {
2792 rl = &rl_intra_aic;
2793 i = 0;
2794 if (s->ac_pred) {
2795 if (s->h263_aic_dir)
2796 scan_table = ff_alternate_vertical_scan; /* left */
2797 else
2798 scan_table = ff_alternate_horizontal_scan; /* top */
2799 }
2800 } else if (s->mb_intra) {
2801 /* DC coef */
2802 if (s->h263_rv10 && s->rv10_version == 3 && s->pict_type == I_TYPE) {
2803 int component, diff;
2804 component = (n <= 3 ? 0 : n - 4 + 1);
2805 level = s->last_dc[component];
2806 if (s->rv10_first_dc_coded[component]) {
2807 diff = rv_decode_dc(s, n);
2808 if (diff == 0xffff)
2809 return -1;
2810 level += diff;
2811 level = level & 0xff; /* handle wrap round */
2812 s->last_dc[component] = level;
2813 } else {
2814 s->rv10_first_dc_coded[component] = 1;
2815 }
2816 } else {
2817 level = get_bits(&s->gb, 8);
2818 if (level == 255)
2819 level = 128;
2820 }
2821 block[0] = level;
2822 i = 1;
2823 } else {
2824 i = 0;
2825 }
2826 if (!coded) {
2827 if (s->mb_intra && s->h263_aic)
2828 goto not_coded;
2829 s->block_last_index[n] = i - 1;
2830 return 0;
2831 }
2832
2833 for(;;) {
2834 code = get_vlc2(&s->gb, rl->vlc.table, TEX_VLC_BITS, 2);
2835 if (code < 0)
2836 return -1;
2837 if (code == rl->n) {
2838 /* escape */
2839 last = get_bits1(&s->gb);
2840 run = get_bits(&s->gb, 6);
2841 level = (INT8)get_bits(&s->gb, 8);
2842 if (s->h263_rv10 && level == -128) {
2843 /* XXX: should patch encoder too */
2844 level = get_bits(&s->gb, 12);
2845 level = (level << 20) >> 20;
2846 }
2847 } else {
2848 run = rl->table_run[code];
2849 level = rl->table_level[code];
2850 last = code >= rl->last;
2851 if (get_bits1(&s->gb))
2852 level = -level;
2853 }
2854 i += run;
2855 if (i >= 64)
2856 return -1;
2857 j = scan_table[i];
2858 block[j] = level;
2859 if (last)
2860 break;
2861 i++;
2862 }
2863 not_coded:
2864 if (s->mb_intra && s->h263_aic) {
2865 h263_pred_acdc(s, block, n);
2866 i = 63;
2867 }
2868 s->block_last_index[n] = i;
2869 return 0;
2870 }
2871
2872 static inline int mpeg4_decode_dc(MpegEncContext * s, int n, int *dir_ptr)
2873 {
2874 int level, pred, code;
2875 UINT16 *dc_val;
2876
2877 if (n < 4)
2878 code = get_vlc2(&s->gb, dc_lum.table, DC_VLC_BITS, 1);
2879 else
2880 code = get_vlc2(&s->gb, dc_chrom.table, DC_VLC_BITS, 1);
2881 if (code < 0 || code > 9 /* && s->nbit<9 */){
2882 fprintf(stderr, "illegal dc vlc\n");
2883 return -1;
2884 }
2885 if (code == 0) {
2886 level = 0;
2887 } else {
2888 level = get_bits(&s->gb, code);
2889 if ((level >> (code - 1)) == 0) /* if MSB not set it is negative*/
2890 level = - (level ^ ((1 << code) - 1));
2891 if (code > 8){
2892 if(get_bits1(&s->gb)==0){ /* marker */
2893 fprintf(stderr, "dc marker bit missing\n");
2894 return -1;
2895 }
2896 }
2897 }
2898
2899 pred = ff_mpeg4_pred_dc(s, n, &dc_val, dir_ptr);
2900 level += pred;
2901 if (level < 0)
2902 level = 0;
2903 if (n < 4) {
2904 *dc_val = level * s->y_dc_scale;
2905 } else {
2906 *dc_val = level * s->c_dc_scale;
2907 }
2908 return level;
2909 }
2910
2911 /**
2912 * decode a block
2913 * returns 0 if everything went ok
2914 * returns DECODING_AC_LOST if an error was detected during AC decoding
2915 * returns DECODING_ACDC_LOST if an error was detected during DC decoding
2916 */
2917 static inline int mpeg4_decode_block(MpegEncContext * s, DCTELEM * block,
2918 int n, int coded)
2919 {
2920 int code, level, i, j, last, run;
2921 int dc_pred_dir;
2922 RLTable *rl;
2923 const UINT8 *scan_table;
2924
2925 if (s->mb_intra) {
2926 /* DC coef */
2927 if(s->data_partitioning && s->pict_type!=B_TYPE){
2928 level = s->dc_val[0][ s->block_index[n] ];
2929 if(n<4) level= (level + (s->y_dc_scale>>1))/s->y_dc_scale; //FIXME optimizs
2930 else level= (level + (s->c_dc_scale>>1))/s->c_dc_scale;
2931 dc_pred_dir= (s->pred_dir_table[s->mb_x + s->mb_y*s->mb_width]<<n)&32;
2932 }else{
2933 level = mpeg4_decode_dc(s, n, &dc_pred_dir);
2934 if (level < 0)
2935 return DECODING_ACDC_LOST;
2936 }
2937 block[0] = level;
2938 i = 1;
2939 if (!coded)
2940 goto not_coded;
2941 rl = &rl_intra;
2942 if (s->ac_pred) {
2943 if (dc_pred_dir == 0)
2944 scan_table = ff_alternate_vertical_scan; /* left */
2945 else
2946 scan_table = ff_alternate_horizontal_scan; /* top */
2947 } else {
2948 scan_table = zigzag_direct;
2949 }
2950 } else {
2951 i = 0;
2952 if (!coded) {
2953 s->block_last_index[n] = i - 1;
2954 return 0;
2955 }
2956 rl = &rl_inter;
2957 scan_table = zigzag_direct;
2958 }
2959
2960 for(;;) {
2961 code = get_vlc2(&s->gb, rl->vlc.table, TEX_VLC_BITS, 2);
2962 if (code < 0)
2963 return DECODING_AC_LOST;
2964 if (code == rl->n) {
2965 /* escape */
2966 if (get_bits1(&s->gb) != 0) {
2967 if (get_bits1(&s->gb) != 0) {
2968 /* third escape */
2969 last = get_bits1(&s->gb);
2970 run = get_bits(&s->gb, 6);
2971 if(get_bits1(&s->gb)==0){
2972 fprintf(stderr, "1. marker bit missing in 3. esc\n");
2973 return DECODING_AC_LOST;
2974 }
2975 level = get_bits(&s->gb, 12);
2976 level = (level << 20) >> 20; /* sign extend */
2977 if(get_bits1(&s->gb)==0){
2978 fprintf(stderr, "2. marker bit missing in 3. esc\n");
2979 return DECODING_AC_LOST;
2980 }
2981 if(level>512 || level<-512){ //FIXME check that QP=1 is ok with this too
2982 fprintf(stderr, "|level| overflow in 3. esc\n");
2983 return DECODING_AC_LOST;
2984 }
2985 #if 1
2986 {
2987 const int abs_level= ABS(level);
2988 if(abs_level<=MAX_LEVEL && run<=MAX_RUN && s->error_resilience>=0){
2989 const int run1= run - rl->max_run[last][abs_level] - 1;
2990 if(abs_level <= rl->max_level[last][run]){
2991 fprintf(stderr, "illegal 3. esc, vlc encoding possible\n");
2992 return DECODING_AC_LOST;
2993 }
2994 if(abs_level <= rl->max_level[last][run]*2){
2995 fprintf(stderr, "illegal 3. esc, esc 1 encoding possible\n");
2996 return DECODING_AC_LOST;
2997 }
2998 if(run1 >= 0 && abs_level <= rl->max_level[last][run1]){
2999 fprintf(stderr, "illegal 3. esc, esc 2 encoding possible\n");
3000 return DECODING_AC_LOST;
3001 }
3002 }
3003 }
3004 #endif
3005 } else {
3006 /* second escape */
3007 code = get_vlc2(&s->gb, rl->vlc.table, TEX_VLC_BITS, 2);
3008 if (code < 0 || code >= rl->n)
3009 return DECODING_AC_LOST;
3010 run = rl->table_run[code];
3011 level = rl->table_level[code];
3012 last = code >= rl->last;
3013 run += rl->max_run[last][level] + 1;
3014 if (get_bits1(&s->gb))
3015 level = -level;
3016 }
3017 } else {
3018 /* first escape */
3019 code = get_vlc2(&s->gb, rl->vlc.table, TEX_VLC_BITS, 2);
3020 if (code < 0 || code >= rl->n)
3021 return DECODING_AC_LOST;
3022 run = rl->table_run[code];
3023 level = rl->table_level[code];
3024 last = code >= rl->last;
3025 level += rl->max_level[last][run];
3026 if (get_bits1(&s->gb))
3027 level = -level;
3028 }
3029 } else {
3030 run = rl->table_run[code];
3031 level = rl->table_level[code];
3032 last = code >= rl->last;
3033 if (get_bits1(&s->gb))
3034 level = -level;
3035 }
3036 i += run;
3037 if (i >= 64)
3038 return DECODING_AC_LOST;
3039 j = scan_table[i];
3040 block[j] = level;
3041 i++;
3042 if (last)
3043 break;
3044 }
3045 not_coded:
3046 if (s->mb_intra) {
3047 mpeg4_pred_ac(s, block, n, dc_pred_dir);
3048 if (s->ac_pred) {
3049 i = 64; /* XXX: not optimal */
3050 }
3051 }
3052 s->block_last_index[n] = i - 1;
3053 return 0;
3054 }
3055
3056 /* most is hardcoded. should extend to handle all h263 streams */
3057 int h263_decode_picture_header(MpegEncContext *s)
3058 {
3059 int format, width, height;
3060
3061 /* picture start code */
3062 if (get_bits(&s->gb, 22) != 0x20) {
3063 fprintf(stderr, "Bad picture start code\n");
3064 return -1;
3065 }
3066 /* temporal reference */
3067 s->picture_number = get_bits(&s->gb, 8); /* picture timestamp */
3068
3069 /* PTYPE starts here */
3070 if (get_bits1(&s->gb) != 1) {
3071 /* marker */
3072 fprintf(stderr, "Bad marker\n");
3073 return -1;
3074 }
3075 if (get_bits1(&s->gb) != 0) {
3076 fprintf(stderr, "Bad H263 id\n");
3077 return -1; /* h263 id */
3078 }
3079 skip_bits1(&s->gb); /* split screen off */
3080 skip_bits1(&s->gb); /* camera off */
3081 skip_bits1(&s->gb); /* freeze picture release off */
3082
3083 /* Reset GOB number */
3084 s->gob_number = 0;
3085
3086 format = get_bits(&s->gb, 3);
3087 /*
3088 0 forbidden
3089 1 sub-QCIF
3090 10 QCIF
3091 7 extended PTYPE (PLUSPTYPE)
3092 */
3093
3094 if (format != 7 && format != 6) {
3095 s->h263_plus = 0;
3096 /* H.263v1 */
3097 width = h263_format[format][0];
3098 height = h263_format[format][1];
3099 if (!width)
3100 return -1;
3101
3102 s->width = width;
3103 s->height = height;
3104 s->pict_type = I_TYPE + get_bits1(&s->gb);
3105
3106 s->unrestricted_mv = get_bits1(&s->gb);
3107 s->h263_long_vectors = s->unrestricted_mv;
3108
3109 if (get_bits1(&s->gb) != 0) {
3110 fprintf(stderr, "H263 SAC not supported\n");
3111 return -1; /* SAC: off */
3112 }
3113 if (get_bits1(&s->gb) != 0) {
3114 s->mv_type = MV_TYPE_8X8; /* Advanced prediction mode */
3115 }
3116
3117 if (get_bits1(&s->gb) != 0) {
3118 fprintf(stderr, "H263 PB frame not supported\n");
3119 return -1; /* not PB frame */
3120 }
3121 s->qscale = get_bits(&s->gb, 5);
3122 skip_bits1(&s->gb); /* Continuous Presence Multipoint mode: off */
3123 } else {
3124 int ufep;
3125
3126 /* H.263v2 */
3127 s->h263_plus = 1;
3128 ufep = get_bits(&s->gb, 3); /* Update Full Extended PTYPE */
3129
3130 /* ufep other than 0 and 1 are reserved */
3131 if (ufep == 1) {
3132 /* OPPTYPE */
3133 format = get_bits(&s->gb, 3);
3134 dprintf("ufep=1, format: %d\n", format);
3135 skip_bits(&s->gb,1); /* Custom PCF */
3136 s->umvplus_dec = get_bits(&s->gb, 1); /* Unrestricted Motion Vector */
3137 skip_bits1(&s->gb); /* Syntax-based Arithmetic Coding (SAC) */
3138 if (get_bits1(&s->gb) != 0) {
3139 s->mv_type = MV_TYPE_8X8; /* Advanced prediction mode */
3140 }
3141 if (get_bits1(&s->gb) != 0) { /* Advanced Intra Coding (AIC) */
3142 s->h263_aic = 1;
3143 }
3144
3145 skip_bits(&s->gb, 7);
3146 /* these are the 7 bits: (in order of appearence */
3147 /* Deblocking Filter */
3148 /* Slice Structured */
3149 /* Reference Picture Selection */
3150 /* Independent Segment Decoding */
3151 /* Alternative Inter VLC */
3152 /* Modified Quantization */
3153 /* Prevent start code emulation */
3154
3155 skip_bits(&s->gb, 3); /* Reserved */
3156 } else if (ufep != 0) {
3157 fprintf(stderr, "Bad UFEP type (%d)\n", ufep);
3158 return -1;
3159 }
3160
3161 /* MPPTYPE */
3162 s->pict_type = get_bits(&s->gb, 3) + I_TYPE;
3163 dprintf("pict_type: %d\n", s->pict_type);
3164 if (s->pict_type != I_TYPE &&
3165 s->pict_type != P_TYPE)
3166 return -1;
3167 skip_bits(&s->gb, 2);
3168 s->no_rounding = get_bits1(&s->gb);
3169 dprintf("RTYPE: %d\n", s->no_rounding);
3170 skip_bits(&s->gb, 4);
3171
3172 /* Get the picture dimensions */
3173 if (ufep) {
3174 if (format == 6) {
3175 /* Custom Picture Format (CPFMT) */
3176 s->aspect_ratio_info = get_bits(&s->gb, 4);
3177 dprintf("aspect: %d\n", s->aspect_ratio_info);
3178 /* aspect ratios:
3179 0 - forbidden
3180 1 - 1:1
3181 2 - 12:11 (CIF 4:3)
3182 3 - 10:11 (525-type 4:3)
3183 4 - 16:11 (CIF 16:9)
3184 5 - 40:33 (525-type 16:9)
3185 6-14 - reserved
3186 */
3187 width = (get_bits(&s->gb, 9) + 1) * 4;
3188 skip_bits1(&s->gb);
3189 height = get_bits(&s->gb, 9) * 4;
3190 dprintf("\nH.263+ Custom picture: %dx%d\n",width,height);
3191 if (s->aspect_ratio_info == EXTENDED_PAR) {
3192 /* aspected dimensions */
3193 skip_bits(&s->gb, 8); /* width */
3194 skip_bits(&s->gb, 8); /* height */
3195 }
3196 } else {
3197 width = h263_format[format][0];
3198 height = h263_format[format][1];
3199 }
3200 if ((width == 0) || (height == 0))
3201 return -1;
3202 s->width = width;
3203 s->height = height;
3204 if (s->umvplus_dec) {
3205 skip_bits1(&s->gb); /* Unlimited Unrestricted Motion Vectors Indicator (UUI) */
3206 }
3207 }
3208
3209 s->qscale = get_bits(&s->gb, 5);
3210 }
3211 /* PEI */
3212 while (get_bits1(&s->gb) != 0) {
3213 skip_bits(&s->gb, 8);
3214 }
3215 s->f_code = 1;
3216
3217 if(s->h263_aic){
3218 s->y_dc_scale_table=
3219 s->c_dc_scale_table= h263_aic_dc_scale_table;
3220 }else{
3221 s->y_dc_scale_table=
3222 s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
3223 }
3224
3225 return 0;
3226 }
3227
3228 static void mpeg4_decode_sprite_trajectory(MpegEncContext * s)
3229 {
3230 int i;
3231 int a= 2<<s->sprite_warping_accuracy;
3232 int rho= 3-s->sprite_warping_accuracy;
3233 int r=16/a;
3234 const int vop_ref[4][2]= {{0,0}, {s->width,0}, {0, s->height}, {s->width, s->height}}; // only true for rectangle shapes
3235 int d[4][2]={{0,0}, {0,0}, {0,0}, {0,0}};
3236 int sprite_ref[4][2];
3237 int virtual_ref[2][2];
3238 int w2, h2;
3239 int alpha=0, beta=0;
3240 int w= s->width;
3241 int h= s->height;
3242 //printf("SP %d\n", s->sprite_warping_accuracy);
3243 for(i=0; i<s->num_sprite_warping_points; i++){
3244 int length;
3245 int x=0, y=0;
3246
3247 length= get_vlc(&s->gb, &sprite_trajectory);
3248 if(length){
3249 x= get_bits(&s->gb, length);
3250 //printf("lx %d %d\n", length, x);
3251 if ((x >> (length - 1)) == 0) /* if MSB not set it is negative*/
3252 x = - (x ^ ((1 << length) - 1));
3253 }
3254 if(!(s->divx_version==500 && s->divx_build==413)) skip_bits1(&s->gb); /* marker bit */
3255
3256 length= get_vlc(&s->gb, &sprite_trajectory);
3257 if(length){
3258 y=get_bits(&s->gb, length);
3259 //printf("ly %d %d\n", length, y);
3260 if ((y >> (length - 1)) == 0) /* if MSB not set it is negative*/
3261 y = - (y ^ ((1 << length) - 1));
3262 }
3263 skip_bits1(&s->gb); /* marker bit */
3264 //printf("%d %d %d %d\n", x, y, i, s->sprite_warping_accuracy);
3265 //if(i>0 && (x!=0 || y!=0)) printf("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n");
3266 //x=y=0;
3267 d[i][0]= x;
3268 d[i][1]= y;
3269 }
3270
3271 while((1<<alpha)<w) alpha++;
3272 while((1<<beta )<h) beta++; // there seems to be a typo in the mpeg4 std for the definition of w' and h'
3273 w2= 1<<alpha;
3274 h2= 1<<beta;
3275
3276 // Note, the 4th point isnt used for GMC
3277 if(s->divx_version==500 && s->divx_build==413){
3278 sprite_ref[0][0]= a*vop_ref[0][0] + d[0][0];
3279 sprite_ref[0][1]= a*vop_ref[0][1] + d[0][1];
3280 sprite_ref[1][0]= a*vop_ref[1][0] + d[0][0] + d[1][0];
3281 sprite_ref[1][1]= a*vop_ref[1][1] + d[0][1] + d[1][1];
3282 sprite_ref[2][0]= a*vop_ref[2][0] + d[0][0] + d[2][0];
3283 sprite_ref[2][1]= a*vop_ref[2][1] + d[0][1] + d[2][1];
3284 } else {
3285 sprite_ref[0][0]= (a>>1)*(2*vop_ref[0][0] + d[0][0]);
3286 sprite_ref[0][1]= (a>>1)*(2*vop_ref[0][1] + d[0][1]);
3287 sprite_ref[1][0]= (a>>1)*(2*vop_ref[1][0] + d[0][0] + d[1][0]);
3288 sprite_ref[1][1]= (a>>1)*(2*vop_ref[1][1] + d[0][1] + d[1][1]);
3289 sprite_ref[2][0]= (a>>1)*(2*vop_ref[2][0] + d[0][0] + d[2][0]);
3290 sprite_ref[2][1]= (a>>1)*(2*vop_ref[2][1] + d[0][1] + d[2][1]);
3291 }
3292 /* sprite_ref[3][0]= (a>>1)*(2*vop_ref[3][0] + d[0][0] + d[1][0] + d[2][0] + d[3][0]);
3293 sprite_ref[3][1]= (a>>1)*(2*vop_ref[3][1] + d[0][1] + d[1][1] + d[2][1] + d[3][1]); */
3294
3295 // this is mostly identical to the mpeg4 std (and is totally unreadable because of that ...)
3296 // perhaps it should be reordered to be more readable ...
3297 // the idea behind this virtual_ref mess is to be able to use shifts later per pixel instead of divides
3298 // so the distance between points is converted from w&h based to w2&h2 based which are of the 2^x form
3299 virtual_ref[0][0]= 16*(vop_ref[0][0] + w2)
3300 + ROUNDED_DIV(((w - w2)*(r*sprite_ref[0][0] - 16*vop_ref[0][0]) + w2*(r*sprite_ref[1][0] - 16*vop_ref[1][0])),w);
3301 virtual_ref[0][1]= 16*vop_ref[0][1]
3302 + ROUNDED_DIV(((w - w2)*(r*sprite_ref[0][1] - 16*vop_ref[0][1]) + w2*(r*sprite_ref[1][1] - 16*vop_ref[1][1])),w);
3303 virtual_ref[1][0]= 16*vop_ref[0][0]
3304 + ROUNDED_DIV(((h - h2)*(r*sprite_ref[0][0] - 16*vop_ref[0][0]) + h2*(r*sprite_ref[2][0] - 16*vop_ref[2][0])),h);
3305 virtual_ref[1][1]= 16*(vop_ref[0][1] + h2)
3306 + ROUNDED_DIV(((h - h2)*(r*sprite_ref[0][1] - 16*vop_ref[0][1]) + h2*(r*sprite_ref[2][1] - 16*vop_ref