rl decoding optimization
[libav.git] / libavcodec / h263.c
CommitLineData
de6d9b64
FB
1/*
2 * H263/MPEG4 backend for ffmpeg encoder and decoder
ff4ec49e 3 * Copyright (c) 2000,2001 Fabrice Bellard.
6dbd39fe 4 * H263+ support.
de6d9b64
FB
5 * Copyright (c) 2001 Juan J. Sierralta P.
6 *
ff4ec49e
FB
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.
de6d9b64 11 *
ff4ec49e 12 * This library is distributed in the hope that it will be useful,
de6d9b64 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
ff4ec49e
FB
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
de6d9b64 16 *
ff4ec49e
FB
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
45870f57 20 *
91029be7 21 * ac prediction encoding & b-frame support by Michael Niedermayer <michaelni@gmx.at>
de6d9b64 22 */
1e209222
J
23
24//#define DEBUG
de6d9b64
FB
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
44eb4951 32//rounded divison & shift
44eb4951
MN
33#define RSHIFT(a,b) ((a) > 0 ? ((a) + (1<<((b)-1)))>>(b) : ((a) + (1<<((b)-1))-1)>>(b))
34
c60a6560
FB
35#define PRINT_MB_TYPE(a) {}
36//#define PRINT_MB_TYPE(a) printf(a)
32cd20de 37
edcf9890
MN
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
de6d9b64
FB
47static void h263_encode_block(MpegEncContext * s, DCTELEM * block,
48 int n);
9dbcbd92 49static void h263_encode_motion(MpegEncContext * s, int val, int fcode);
6dbd39fe 50static void h263p_encode_umotion(MpegEncContext * s, int val);
de6d9b64 51static void mpeg4_encode_block(MpegEncContext * s, DCTELEM * block,
92073d01
MN
52 int n, int dc, UINT8 *scan_table,
53 PutBitContext *dc_pb, PutBitContext *ac_pb);
6f91bcd1 54static int h263_decode_motion(MpegEncContext * s, int pred, int fcode);
6dbd39fe 55static int h263p_decode_umotion(MpegEncContext * s, int pred);
de6d9b64
FB
56static int h263_decode_block(MpegEncContext * s, DCTELEM * block,
57 int n, int coded);
92073d01
MN
58static inline int mpeg4_decode_dc(MpegEncContext * s, int n, int *dir_ptr);
59static inline int mpeg4_decode_block(MpegEncContext * s, DCTELEM * block,
de6d9b64 60 int n, int coded);
ef5b1b5a 61static int h263_pred_dc(MpegEncContext * s, int n, UINT16 **dc_val_ptr);
d6231b9e
MN
62static void mpeg4_inv_pred_ac(MpegEncContext * s, INT16 *block, int n,
63 int dir);
49092244
MN
64static void mpeg4_decode_sprite_trajectory(MpegEncContext * s);
65
d6231b9e 66extern UINT32 inverse[256];
de6d9b64 67
9c15096e 68static UINT16 mv_penalty[MAX_FCODE+1][MAX_MV*2+1];
45870f57 69static UINT8 fcode_tab[MAX_MV*2+1];
9d2a0355 70static UINT8 umv_fcode_tab[MAX_MV*2+1];
45870f57 71
2a164b25
MN
72static UINT16 uni_DCtab_lum [512][2];
73static UINT16 uni_DCtab_chrom[512][2];
74
de6d9b64
FB
75int h263_get_picture_format(int width, int height)
76{
77 int format;
78
79 if (width == 128 && height == 96)
1e209222 80 format = 1;
de6d9b64 81 else if (width == 176 && height == 144)
1e209222 82 format = 2;
de6d9b64 83 else if (width == 352 && height == 288)
1e209222 84 format = 3;
de6d9b64 85 else if (width == 704 && height == 576)
1e209222 86 format = 4;
de6d9b64 87 else if (width == 1408 && height == 1152)
1e209222 88 format = 5;
de6d9b64
FB
89 else
90 format = 7;
91 return format;
92}
93
94void h263_encode_picture_header(MpegEncContext * s, int picture_number)
95{
6dbd39fe 96 int format;
de6d9b64
FB
97
98 align_put_bits(&s->pb);
81401c1f
J
99
100 /* Update the pointer to last GOB */
17592475 101 s->ptr_lastgob = pbBufPtr(&s->pb);
81401c1f
J
102 s->gob_number = 0;
103
104 put_bits(&s->pb, 22, 0x20); /* PSC */
bb6f51ae 105 put_bits(&s->pb, 8, (((INT64)s->picture_number * 30 * FRAME_RATE_BASE) /
de6d9b64
FB
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 */
6dbd39fe
J
113
114 format = h263_get_picture_format(s->width, s->height);
de6d9b64
FB
115 if (!s->h263_plus) {
116 /* H.263v1 */
de6d9b64
FB
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 */
6dbd39fe
J
133 if (format == 7)
134 put_bits(&s->pb,3,6); /* Custom Source Format */
135 else
136 put_bits(&s->pb, 3, format);
137
de6d9b64 138 put_bits(&s->pb,1,0); /* Custom PCF: off */
544286b3
J
139 s->umvplus = (s->pict_type == P_TYPE) && s->unrestricted_mv;
140 put_bits(&s->pb, 1, s->umvplus); /* Unrestricted Motion Vector */
de6d9b64
FB
141 put_bits(&s->pb,1,0); /* SAC: off */
142 put_bits(&s->pb,1,0); /* Advanced Prediction Mode: off */
ef5b1b5a 143 put_bits(&s->pb,1,s->h263_aic); /* Advanced Intra Coding */
de6d9b64
FB
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 */
ef5b1b5a
J
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 */
de6d9b64
FB
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
6dbd39fe
J
168 if (format == 7) {
169 /* Custom Picture Format (CPFMT) */
de6d9b64 170
1e209222
J
171 if (s->aspect_ratio_info)
172 put_bits(&s->pb,4,s->aspect_ratio_info);
173 else
6dbd39fe
J
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
de6d9b64 180 /* Unlimited Unrestricted Motion Vectors Indicator (UUI) */
544286b3 181 if (s->umvplus)
de6d9b64
FB
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 */
8f8402e4
MN
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 }
de6d9b64
FB
195}
196
644d98a4
J
197int 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) {
17592475 204 pdif = pbBufPtr(&s->pb) - s->ptr_lastgob;
644d98a4
J
205 if (pdif >= s->rtp_payload_size) {
206 /* Bad luck, packet must be cut before */
207 align_put_bits(&s->pb);
81401c1f
J
208 flush_put_bits(&s->pb);
209 /* Call the RTP callback to send the last GOB */
210 if (s->rtp_callback) {
17592475 211 pdif = pbBufPtr(&s->pb) - s->ptr_lastgob;
81401c1f
J
212 s->rtp_callback(s->ptr_lastgob, pdif, s->gob_number);
213 }
17592475 214 s->ptr_lastgob = pbBufPtr(&s->pb);
644d98a4 215 put_bits(&s->pb, 17, 1); /* GBSC */
81401c1f 216 s->gob_number = mb_line / s->gob_index;
644d98a4 217 put_bits(&s->pb, 5, s->gob_number); /* GN */
81401c1f 218 put_bits(&s->pb, 2, s->pict_type == I_TYPE); /* GFID */
644d98a4 219 put_bits(&s->pb, 5, s->qscale); /* GQUANT */
81401c1f 220 //fprintf(stderr,"\nGOB: %2d size: %d", s->gob_number - 1, pdif);
644d98a4
J
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);
81401c1f
J
225 flush_put_bits(&s->pb);
226 /* Call the RTP callback to send the last GOB */
227 if (s->rtp_callback) {
17592475 228 pdif = pbBufPtr(&s->pb) - s->ptr_lastgob;
81401c1f
J
229 s->rtp_callback(s->ptr_lastgob, pdif, s->gob_number);
230 }
17592475 231 s->ptr_lastgob = pbBufPtr(&s->pb);
644d98a4 232 put_bits(&s->pb, 17, 1); /* GBSC */
81401c1f 233 s->gob_number = mb_line / s->gob_index;
644d98a4 234 put_bits(&s->pb, 5, s->gob_number); /* GN */
81401c1f 235 put_bits(&s->pb, 2, s->pict_type == I_TYPE); /* GFID */
644d98a4 236 put_bits(&s->pb, 5, s->qscale); /* GQUANT */
81401c1f 237 //fprintf(stderr,"\nGOB: %2d size: %d", s->gob_number - 1, pdif);
644d98a4
J
238 return pdif;
239 }
240 }
241 return 0;
242}
d6231b9e
MN
243
244static 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++){
d6231b9e
MN
250 INT16 *ac_val, *ac_val1;
251
4278e7a6 252 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
d6231b9e
MN
253 ac_val1= ac_val;
254 if(dir[n]){
4278e7a6 255 ac_val-= s->block_wrap[n]*16;
d6231b9e
MN
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
278void mpeg4_encode_mb(MpegEncContext * s,
279 DCTELEM block[6][64],
280 int motion_x, int motion_y)
281{
9dbcbd92 282 int cbpc, cbpy, i, pred_x, pred_y;
098eefe1 283 int bits;
92073d01
MN
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;
644d98a4 288
d6231b9e
MN
289 // printf("**mb x=%d y=%d\n", s->mb_x, s->mb_y);
290 if (!s->mb_intra) {
291 /* compute cbp */
9dbcbd92 292 int cbp = 0;
d6231b9e 293 for (i = 0; i < 6; i++) {
9dbcbd92
MN
294 if (s->block_last_index[i] >= 0)
295 cbp |= 1 << (5 - i);
d6231b9e 296 }
9dbcbd92
MN
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;
91029be7 316 s->mv_dir= MV_DIR_FORWARD; //doesnt matter
9dbcbd92
MN
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 */
92073d01
MN
323
324 if(interleaved_stats){
325 s->misc_bits++;
326 s->last_bits++;
327 }
9dbcbd92
MN
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
92073d01
MN
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 }
cf8039b2 344
9dbcbd92
MN
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;
91029be7
MN
373 default:
374 printf("unknown mb type\n");
9dbcbd92
MN
375 return;
376 }
92073d01
MN
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 }
d6231b9e 383
9dbcbd92
MN
384 /* encode each block */
385 for (i = 0; i < 6; i++) {
92073d01
MN
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;
9dbcbd92 393 }
9dbcbd92
MN
394 }else{ /* s->pict_type==B_TYPE */
395 if ((cbp | motion_x | motion_y) == 0 && s->mv_type==MV_TYPE_16X16) {
ee6f7861
MN
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;
e71a4f74
MN
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
ee6f7861
MN
411 s->mb_skiped=1;
412 for(i=0; i<s->max_b_frames; i++){
d697b89b
MN
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);
e71a4f74 420 if(diff>s->qscale*70){ //FIXME check that 70 is optimal
ee6f7861
MN
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);
92073d01
MN
431
432 if(interleaved_stats){
433 s->misc_bits++;
434 s->last_bits++;
435 }
ee6f7861
MN
436 s->skip_count++;
437 return;
438 }
9dbcbd92 439 }
e71a4f74 440
9dbcbd92
MN
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;
92073d01 449 put_bits(pb2, cbpy_tab[cbpy][1], cbpy_tab[cbpy][0]);
9dbcbd92 450
92073d01
MN
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 }
d6231b9e 456
9dbcbd92
MN
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;
92073d01 469 put_bits(pb2, cbpy_tab[cbpy][1], cbpy_tab[cbpy][0]);
9dbcbd92 470
92073d01
MN
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 }
9dbcbd92
MN
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 }
cf8039b2 484 }
92073d01
MN
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 }
098eefe1 491
9dbcbd92
MN
492 /* encode each block */
493 for (i = 0; i < 6; i++) {
92073d01
MN
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;
9dbcbd92 501 }
9dbcbd92 502 s->p_count++;
d6231b9e
MN
503 }
504 } else {
9dbcbd92 505 int cbp;
d6231b9e
MN
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
8f8402e4 515 dc_diff[i]= level - ff_mpeg4_pred_dc(s, i, &dc_ptr, &dir[i]);
d6231b9e
MN
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 }
92073d01 563 put_bits(pb2, 1, s->ac_pred);
d6231b9e 564 cbpy = cbp >> 2;
92073d01 565 put_bits(pb2, cbpy_tab[cbpy][1], cbpy_tab[cbpy][0]);
d6231b9e 566
92073d01
MN
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 }
098eefe1 572
d6231b9e
MN
573 /* encode each block */
574 for (i = 0; i < 6; i++) {
92073d01 575 mpeg4_encode_block(s, block[i], i, dc_diff[i], scan_table[i], dc_pb, tex_pb);
d6231b9e
MN
576 }
577
92073d01
MN
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 }
098eefe1
MN
583 s->i_count++;
584
d6231b9e
MN
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;
d6231b9e
MN
589 INT16 *ac_val;
590
4278e7a6 591 ac_val = s->ac_val[0][0] + s->block_index[i] * 16;
d6231b9e
MN
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
de6d9b64
FB
606void 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;
ef5b1b5a
J
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);
9dbcbd92 616 if (!s->mb_intra) {
ef5b1b5a 617 /* compute cbp */
9dbcbd92
MN
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 }
ef5b1b5a 628 put_bits(&s->pb, 1, 0); /* mb coded */
9dbcbd92
MN
629 cbpc = cbp & 3;
630 put_bits(&s->pb,
ef5b1b5a
J
631 inter_MCBPC_bits[cbpc],
632 inter_MCBPC_code[cbpc]);
9dbcbd92
MN
633 cbpy = cbp >> 2;
634 cbpy ^= 0xf;
635 put_bits(&s->pb, cbpy_tab[cbpy][1], cbpy_tab[cbpy][0]);
636
ef5b1b5a 637 /* motion vectors: 16x16 mode only now */
9dbcbd92 638 h263_pred_motion(s, 0, &pred_x, &pred_y);
6dbd39fe 639
9dbcbd92
MN
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))
ef5b1b5a 648 /* To prevent Start Code emulation */
9dbcbd92
MN
649 put_bits(&s->pb,1,1);
650 }
ef5b1b5a
J
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 }
de6d9b64 696
ef5b1b5a
J
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]);
de6d9b64
FB
714 }
715
ef5b1b5a
J
716 for(i=0; i<6; i++) {
717 /* encode each block */
ba6802de 718 h263_encode_block(s, block[i], i);
ef5b1b5a
J
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
728static 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;
de6d9b64 748 }
ef5b1b5a
J
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 */
92073d01 756 if (s->first_slice_line && ((n < 2) || (n > 3)))
ef5b1b5a
J
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;
de6d9b64
FB
771}
772
ef5b1b5a 773
9e15ad28 774void h263_pred_acdc(MpegEncContext * s, INT16 *block, int n)
d140623f 775{
9e15ad28
J
776 int x, y, wrap, a, c, pred_dc, scale, i;
777 INT16 *dc_val, *ac_val, *ac_val1;
d140623f
J
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];
9e15ad28 785 ac_val = s->ac_val[0][0];
d140623f
J
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];
9e15ad28 792 ac_val = s->ac_val[n - 4 + 1][0];
d140623f
J
793 scale = s->c_dc_scale;
794 }
9e15ad28
J
795
796 ac_val += ((y) * wrap + (x)) * 16;
797 ac_val1 = ac_val;
798
d140623f
J
799 /* B C
800 * A X
801 */
802 a = dc_val[(x - 1) + (y) * wrap];
803 c = dc_val[(x) + (y - 1) * wrap];
804
ef5b1b5a 805 /* No prediction outside GOB boundary */
92073d01 806 if (s->first_slice_line && ((n < 2) || (n > 3)))
ef5b1b5a 807 c = 1024;
9e15ad28 808 pred_dc = 1024;
d140623f
J
809 if (s->ac_pred) {
810 if (s->h263_aic_dir) {
811 /* left prediction */
9e15ad28
J
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;
d140623f
J
818 }
819 } else {
820 /* top prediction */
9e15ad28
J
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;
d140623f
J
827 }
828 }
9e15ad28
J
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;
d140623f 837 }
9e15ad28
J
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
d140623f
J
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
de6d9b64
FB
858INT16 *h263_pred_motion(MpegEncContext * s, int block,
859 int *px, int *py)
860{
4278e7a6 861 int xy, wrap;
de6d9b64 862 INT16 *A, *B, *C, *mot_val;
49c9325f 863 static const int off[4]= {2, 1, 1, -1};
de6d9b64 864
4278e7a6
MN
865 wrap = s->block_wrap[0];
866 xy = s->block_index[block];
de6d9b64 867
af8793ae 868 mot_val = s->motion_val[xy];
de6d9b64 869
92073d01
MN
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 }
de6d9b64 909 } else {
49c9325f
MN
910 B = s->motion_val[xy - wrap];
911 C = s->motion_val[xy + off[block] - wrap];
de6d9b64
FB
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
9dbcbd92 918static void h263_encode_motion(MpegEncContext * s, int val, int f_code)
de6d9b64
FB
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 {
9dbcbd92 927 bit_size = f_code - 1;
de6d9b64
FB
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) {
de6d9b64
FB
939 sign = 0;
940 } else {
941 val = -val;
de6d9b64
FB
942 sign = 1;
943 }
3bf43d42
MN
944 val--;
945 code = (val >> bit_size) + 1;
946 bits = val & (range - 1);
de6d9b64
FB
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
6dbd39fe
J
955/* Encode MV differences on H.263+ with Unrestricted MV mode */
956static 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
45870f57
MN
994static 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 }
45870f57
MN
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 }
9d2a0355
MN
1030
1031 for(mv=0; mv<MAX_MV*2+1; mv++){
1032 umv_fcode_tab[mv]= 1;
1033 }
45870f57
MN
1034}
1035
20695ec9 1036static void init_uni_dc_tab(void)
2a164b25
MN
1037{
1038 int level, uni_code, uni_len;
1039
3bf43d42 1040 for(level=-256; level<256; level++){
2a164b25
MN
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
45870f57 1088void h263_encode_init(MpegEncContext *s)
de6d9b64
FB
1089{
1090 static int done = 0;
1091
1092 if (!done) {
1093 done = 1;
2a164b25
MN
1094
1095 init_uni_dc_tab();
1096
de6d9b64
FB
1097 init_rl(&rl_inter);
1098 init_rl(&rl_intra);
ef5b1b5a 1099 init_rl(&rl_intra_aic);
45870f57
MN
1100
1101 init_mv_penalty_and_fcode(s);
de6d9b64 1102 }
9d2a0355 1103 s->mv_penalty= mv_penalty; //FIXME exact table for msmpeg4 & h263p
45870f57 1104
9d2a0355 1105 // use fcodes >1 only for mpeg4 & h263 & h263p FIXME
d7e9533a
MN
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;
8f8402e4 1117 //Note for mpeg4 & h263 the dc-scale table will be set per frame as needed later
d7e9533a
MN
1118 default: //nothing needed default table allready set in mpegvideo.c
1119 s->min_qcoeff= -128;
1120 s->max_qcoeff= 127;
8f8402e4
MN
1121 s->y_dc_scale_table=
1122 s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
d7e9533a
MN
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
de6d9b64
FB
1129}
1130
1131static void h263_encode_block(MpegEncContext * s, DCTELEM * block, int n)
1132{
ef5b1b5a
J
1133 int level, run, last, i, j, last_index, last_non_zero, sign, slevel, code;
1134 RLTable *rl;
de6d9b64 1135
ef5b1b5a
J
1136 rl = &rl_inter;
1137 if (s->mb_intra && !s->h263_aic) {
81401c1f
J
1138 /* DC coef */
1139 level = block[0];
de6d9b64
FB
1140 /* 255 cannot be represented, so we clamp */
1141 if (level > 254) {
1142 level = 254;
1143 block[0] = 254;
1144 }
81401c1f
J
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;
de6d9b64 1155 } else {
81401c1f 1156 i = 0;
ef5b1b5a
J
1157 if (s->h263_aic && s->mb_intra)
1158 rl = &rl_intra_aic;
de6d9b64 1159 }
ef5b1b5a 1160
de6d9b64
FB
1161 /* AC coefs */
1162 last_index = s->block_last_index[n];
1163 last_non_zero = i - 1;
1164 for (; i <= last_index; i++) {
ef5b1b5a
J
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 }
de6d9b64
FB
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 }
ef5b1b5a
J
1185 last_non_zero = i;
1186 }
de6d9b64
FB
1187 }
1188}
1189
1190/***************************************************/
1191
92073d01 1192void ff_mpeg4_stuffing(PutBitContext * pbc)
6f91bcd1
MN
1193{
1194 int length;
1195 put_bits(pbc, 1, 0);
1196 length= (-get_bit_count(pbc))&7;
92073d01 1197 if(length) put_bits(pbc, length, (1<<length)-1);
6f91bcd1
MN
1198}
1199
91029be7
MN
1200/* must be called before writing the header */
1201void 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
ff4ec49e 1211 s->time= picture_number*(INT64)FRAME_RATE_BASE*s->time_increment_resolution/s->frame_rate;
91029be7
MN
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
10cd5815 1225static void mpeg4_encode_vol_header(MpegEncContext * s)
de6d9b64 1226{
10cd5815 1227 int vo_ver_id=1; //must be 2 if we want GMC or q-pel
4b1f4f23 1228 char buf[255];
1ff662cc
MN
1229
1230 s->vo_type= s->has_b_frames ? CORE_VO_TYPE : SIMPLE_VO_TYPE;
1231
10cd5815
MN
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 */
1ff662cc 1238 put_bits(&s->pb, 8, s->vo_type); /* video obj type indication */
10cd5815
MN
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 */
11ce8834
MN
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 */
1ff662cc
MN
1246
1247 if(s->low_delay){
1248 put_bits(&s->pb, 1, 1); /* vol control parameters= yes */
1c2a8c7f 1249 put_bits(&s->pb, 2, 1); /* chroma format YUV 420/YV12 */
1ff662cc
MN
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
10cd5815
MN
1256 put_bits(&s->pb, 2, RECT_SHAPE); /* vol shape= rectangle */
1257 put_bits(&s->pb, 1, 1); /* marker bit */
9dbcbd92 1258
9dbcbd92 1259 put_bits(&s->pb, 16, s->time_increment_resolution);
10cd5815
MN
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 */
92073d01
MN
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
10cd5815
MN
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
92073d01 1294 ff_mpeg4_stuffing(&s->pb);
6f91bcd1
MN
1295 put_bits(&s->pb, 16, 0);
1296 put_bits(&s->pb, 16, 0x1B2); /* user_data */
3d1d9b2c 1297 sprintf(buf, "FFmpeg%sb%s", FFMPEG_VERSION, LIBAVCODEC_BUILD_STR);
4b1f4f23 1298 put_string(&s->pb, buf);
49c9325f 1299
92073d01 1300 ff_mpeg4_stuffing(&s->pb);
10cd5815
MN
1301}
1302
1303/* write mpeg4 VOP header */
1304void mpeg4_encode_picture_header(MpegEncContext * s, int picture_number)
1305{
9dbcbd92
MN
1306 int time_incr;
1307 int time_div, time_mod;
1308
92073d01
MN
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 }
9dbcbd92 1314
9dbcbd92
MN
1315//printf("num:%d rate:%d base:%d\n", s->picture_number, s->frame_rate, FRAME_RATE_BASE);
1316
17592475
MN
1317 put_bits(&s->pb, 16, 0); /* vop header */
1318 put_bits(&s->pb, 16, 0x1B6); /* vop header */
de6d9b64 1319 put_bits(&s->pb, 2, s->pict_type - 1); /* pict type: I = 0 , P = 1 */
9dbcbd92 1320
91029be7
MN
1321 time_div= s->time/s->time_increment_resolution;
1322 time_mod= s->time%s->time_increment_resolution;
9dbcbd92
MN
1323 time_incr= time_div - s->last_time_base;
1324 while(time_incr--)
1325 put_bits(&s->pb, 1, 1);
1326
de6d9b64
FB
1327 put_bits(&s->pb, 1, 0);
1328
1329 put_bits(&s->pb, 1, 1); /* marker */
9dbcbd92 1330 put_bits(&s->pb, s->time_increment_bits, time_mod); /* time increment */
de6d9b64
FB
1331 put_bits(&s->pb, 1, 1); /* marker */
1332 put_bits(&s->pb, 1, 1); /* vop coded */
10cd5815
MN
1333 if ( s->pict_type == P_TYPE
1334 || (s->pict_type == S_TYPE && s->vol_sprite_usage==GMC_SPRITE)) {
1335 s->no_rounding ^= 1;
de6d9b64
FB
1336 put_bits(&s->pb, 1, s->no_rounding); /* rounding type */
1337 }
1338 put_bits(&s->pb, 3, 0); /* intra dc VLC threshold */
10cd5815 1339 //FIXME sprite stuff
de6d9b64
FB
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 */
10cd5815
MN
1345 if (s->pict_type == B_TYPE)
1346 put_bits(&s->pb, 3, s->b_code); /* fcode_back */
de6d9b64 1347 // printf("****frame %d\n", picture_number);
8f8402e4
MN
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;
de6d9b64
FB
1351}
1352
8f8402e4 1353static void h263_dc_scale(MpegEncContext * s)
de6d9b64 1354{
8f8402e4
MN
1355 s->y_dc_scale= s->y_dc_scale_table[ s->qscale ];
1356 s->c_dc_scale= s->c_dc_scale_table[ s->qscale ];
de6d9b64
FB
1357}
1358
8f8402e4 1359inline int ff_mpeg4_pred_dc(MpegEncContext * s, int n, UINT16 **dc_val_ptr, int *dir_ptr)
de6d9b64 1360{
4278e7a6 1361 int a, b, c, wrap, pred, scale;
de6d9b64 1362 UINT16 *dc_val;
e7650bec 1363 int dummy;
de6d9b64
FB
1364
1365 /* find prediction */
1366 if (n < 4) {
de6d9b64
FB
1367 scale = s->y_dc_scale;
1368 } else {
de6d9b64
FB
1369 scale = s->c_dc_scale;
1370 }
4278e7a6
MN
1371 wrap= s->block_wrap[n];
1372 dc_val = s->dc_val[0] + s->block_index[n];
de6d9b64
FB
1373
1374 /* B C
1375 * A X
1376 */
4278e7a6
MN
1377 a = dc_val[ - 1];
1378 b = dc_val[ - 1 - wrap];
1379 c = dc_val[ - wrap];
de6d9b64
FB
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 */
d6231b9e
MN
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
de6d9b64 1397 pred = (pred + (scale >> 1)) / scale;
d6231b9e 1398#endif
de6d9b64
FB
1399
1400 /* prepare address for prediction update */
4278e7a6 1401 *dc_val_ptr = &dc_val[0];
de6d9b64
FB
1402
1403 return pred;
1404}
1405
d962f6fd 1406void mpeg4_pred_ac(MpegEncContext * s, INT16 *block, int n,
de6d9b64
FB
1407 int dir)
1408{
4278e7a6 1409 int i;
de6d9b64
FB
1410 INT16 *ac_val, *ac_val1;
1411
1412 /* find prediction */
4278e7a6 1413 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
de6d9b64
FB
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++) {
d962f6fd 1420 block[block_permute_op(i*8)] += ac_val[i];
de6d9b64
FB
1421 }
1422 } else {
1423 /* top prediction */
4278e7a6 1424 ac_val -= 16 * s->block_wrap[n];
de6d9b64 1425 for(i=1;i<8;i++) {
d962f6fd 1426 block[block_permute_op(i)] += ac_val[i + 8];
de6d9b64
FB
1427 }
1428 }
1429 }
1430 /* left copy */
1431 for(i=1;i<8;i++)
d962f6fd 1432 ac_val1[i] = block[block_permute_op(i * 8)];
de6d9b64
FB
1433 /* top copy */
1434 for(i=1;i<8;i++)
d962f6fd 1435 ac_val1[8 + i] = block[block_permute_op(i)];
de6d9b64
FB
1436}
1437
d6231b9e
MN
1438static void mpeg4_inv_pred_ac(MpegEncContext * s, INT16 *block, int n,
1439 int dir)
de6d9b64 1440{
4278e7a6 1441 int i;
d6231b9e 1442 INT16 *ac_val;
de6d9b64 1443
d6231b9e 1444 /* find prediction */
4278e7a6 1445 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
d6231b9e
MN
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 */
4278e7a6 1455 ac_val -= 16 * s->block_wrap[n];
d6231b9e
MN
1456 for(i=1;i<8;i++) {
1457 block[block_permute_op(i)] -= ac_val[i + 8];
1458 }
de6d9b64 1459 }
d6231b9e
MN
1460}
1461
92073d01 1462static inline void mpeg4_encode_dc(PutBitContext * s, int level, int n)
d6231b9e 1463{
2a164b25 1464#if 1
92073d01 1465// if(level<-255 || level>255) printf("dc overflow\n");
2a164b25
MN
1466 level+=256;
1467 if (n < 4) {
1468 /* luminance */
92073d01 1469 put_bits(s, uni_DCtab_lum[level][1], uni_DCtab_lum[level][0]);
2a164b25
MN
1470 } else {
1471 /* chrominance */
92073d01 1472 put_bits(s, uni_DCtab_chrom[level][1], uni_DCtab_chrom[level][0]);
2a164b25
MN
1473 }
1474#else
d6231b9e 1475 int size, v;
de6d9b64
FB
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 }
2a164b25 1500#endif
de6d9b64
FB
1501}
1502
92073d01
MN
1503static void mpeg4_encode_block(MpegEncContext * s, DCTELEM * block, int n, int intra_dc,
1504 UINT8 *scan_table, PutBitContext *dc_pb, PutBitContext *ac_pb)
de6d9b64
FB
1505{
1506 int level, run, last, i, j, last_index, last_non_zero, sign, slevel;
d6231b9e 1507 int code;
de6d9b64
FB
1508 const RLTable *rl;
1509
1510 if (s->mb_intra) {
1511 /* mpeg4 based DC predictor */
92073d01 1512 mpeg4_encode_dc(dc_pb, intra_dc, n);
de6d9b64
FB
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++) {
d6231b9e 1524 j = scan_table[i];
de6d9b64
FB
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);
92073d01 1536 put_bits(ac_pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
de6d9b64
FB
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:
92073d01 1545 put_bits(ac_pb, 1, 1);
de6d9b64
FB
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 */
92073d01
MN
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);
de6d9b64
FB
1561 } else {
1562 /* second escape */
92073d01
MN
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);
de6d9b64
FB
1566 }
1567 } else {
1568 /* first escape */
92073d01
MN
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);
de6d9b64
FB
1572 }
1573 } else {
92073d01 1574 put_bits(ac_pb, 1, sign);
de6d9b64
FB
1575 }
1576 last_non_zero = i;
1577 }
1578 }
1579}
1580
1581
1582
1583/***********************************************/
1584/* decoding */
1585
1586static VLC intra_MCBPC_vlc;
1587static VLC inter_MCBPC_vlc;
1588static VLC cbpy_vlc;
1589static VLC mv_vlc;
1590static VLC dc_lum, dc_chrom;
73c8e514 1591static VLC sprite_trajectory;
6f91bcd1 1592static VLC mb_type_b_vlc;
de6d9b64
FB
1593
1594void 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 }
6000abfa 1623 rl->max_level[last] = av_malloc(MAX_RUN + 1);
de6d9b64 1624 memcpy(rl->max_level[last], max_level, MAX_RUN + 1);
6000abfa 1625 rl->max_run[last] = av_malloc(MAX_LEVEL + 1);
de6d9b64 1626 memcpy(rl->max_run[last], max_run, MAX_LEVEL + 1);
6000abfa 1627 rl->index_run[last] = av_malloc(MAX_RUN + 1);
de6d9b64
FB
1628 memcpy(rl->index_run[last], index_run, MAX_RUN + 1);
1629 }
1630}
1631
1632void init_vlc_rl(RLTable *rl)
1633{
45a82edb
MN
1634 int i, q;
1635
de6d9b64
FB
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);
45a82edb
MN
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 }
de6d9b64
FB
1677}
1678
1679/* init vlcs */
1680
1681/* XXX: find a better solution to handle static init */
1682void h263_decode_init_vlc(MpegEncContext *s)
1683{
1684 static int done = 0;
1685
1686 if (!done) {
1687 done = 1;
1688
edcf9890 1689 init_vlc(&intra_MCBPC_vlc, INTRA_MCBPC_VLC_BITS, 8,
de6d9b64
FB
1690 intra_MCBPC_bits, 1, 1,
1691 intra_MCBPC_code, 1, 1);
edcf9890 1692 init_vlc(&inter_MCBPC_vlc, INTER_MCBPC_VLC_BITS, 25,
de6d9b64
FB
1693 inter_MCBPC_bits, 1, 1,
1694 inter_MCBPC_code, 1, 1);
edcf9890 1695 init_vlc(&cbpy_vlc, CBPY_VLC_BITS, 16,
de6d9b64
FB
1696 &cbpy_tab[0][1], 2, 1,
1697 &cbpy_tab[0][0], 2, 1);
edcf9890 1698 init_vlc(&mv_vlc, MV_VLC_BITS, 33,
de6d9b64
FB
1699 &mvtab[0][1], 2, 1,
1700 &mvtab[0][0], 2, 1);
1701 init_rl(&rl_inter);
1702 init_rl(&rl_intra);
d140623f 1703 init_rl(&rl_intra_aic);
de6d9b64
FB
1704 init_vlc_rl(&rl_inter);
1705 init_vlc_rl(&rl_intra);
d140623f 1706 init_vlc_rl(&rl_intra_aic);
c03a7172 1707 init_vlc(&dc_lum, DC_VLC_BITS, 10 /* 13 */,
de6d9b64
FB
1708 &DCtab_lum[0][1], 2, 1,
1709 &DCtab_lum[0][0], 2, 1);
c03a7172 1710 init_vlc(&dc_chrom, DC_VLC_BITS, 10 /* 13 */,
de6d9b64
FB
1711 &DCtab_chrom[0][1], 2, 1,
1712 &DCtab_chrom[0][0], 2, 1);
edcf9890 1713 init_vlc(&sprite_trajectory, SPRITE_TRAJ_VLC_BITS, 15,
73c8e514
MN
1714 &sprite_trajectory_tab[0][1], 4, 2,
1715 &sprite_trajectory_tab[0][0], 4, 2);
edcf9890 1716 init_vlc(&mb_type_b_vlc, MB_TYPE_B_VLC_BITS, 4,
6f91bcd1
MN
1717 &mb_type_b_tab[0][1], 2, 1,
1718 &mb_type_b_tab[0][0], 2, 1);
de6d9b64
FB
1719 }
1720}
1721
644d98a4 1722int h263_decode_gob_header(MpegEncContext *s)
de6d9b64 1723{
644d98a4 1724 unsigned int val, gfid;
102d3908 1725
4949028f 1726 /* Check for GOB Start Code */
644d98a4
J
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 */
4949028f 1732#ifdef DEBUG
644d98a4 1733 fprintf(stderr,"\nGOB Start Code at MB %d\n", (s->mb_y * s->mb_width) + s->mb_x);
102d3908 1734#endif
644d98a4
J
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 */
4949028f 1738#ifdef DEBUG
17592475 1739 fprintf(stderr, "\nGN: %u GFID: %u Quant: %u\n", s->gob_number, gfid, s->qscale);
4949028f 1740#endif
644d98a4 1741 return 1;
4949028f 1742 }
644d98a4
J
1743 return 0;
1744
1745}
1746
49092244
MN
1747static 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
92073d01
MN
1754void 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
1760void 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
1785void ff_mpeg4_encode_video_packet_header(MpegEncContext *s)
49092244 1786{
49092244 1787 int mb_num_bits= av_log2(s->mb_num - 1) + 1;
49092244 1788
92073d01
MN
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}
49092244 1802
92073d01
MN
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 */
1807static 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");
49092244 1824 }
92073d01
MN
1825
1826//printf("%X\n", show_bits(gb, 24));
49092244 1827 bits=0;
92073d01
MN
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));
49092244
MN
1841
1842 if(s->shape != RECT_SHAPE){
92073d01 1843 header_extension= get_bits1(gb);
49092244
MN
1844 //FIXME more stuff here
1845 }
1846
92073d01
MN
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 }
49092244
MN
1852
1853 if(s->shape != BIN_ONLY_SHAPE){
92073d01
MN
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 }
49092244
MN
1861 }
1862
1863 if(s->shape == RECT_SHAPE){
92073d01 1864 header_extension= get_bits1(gb);
49092244
MN
1865 }
1866 if(header_extension){
92073d01 1867 int time_increment;
49092244 1868 int time_incr=0;
92073d01
MN
1869 printf("header extension not supported\n");
1870 return -1;
1871
1872 while (get_bits1(gb) != 0)
49092244
MN
1873 time_incr++;
1874
92073d01
MN
1875 check_marker(gb, "before time_increment in video packed header");
1876 time_increment= get_bits(gb, s->time_increment_bits);
49092244 1877 if(s->pict_type!=B_TYPE){
9dbcbd92 1878 s->last_time_base= s->time_base;
49092244 1879 s->time_base+= time_incr;
9dbcbd92
MN
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;
49092244 1883 }else{
9dbcbd92
MN
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;
49092244 1886 }
92073d01 1887 check_marker(gb, "before vop_coding_type in video packed header");
49092244 1888
92073d01 1889 skip_bits(gb, 2); /* vop coding type */
49092244
MN
1890 //FIXME not rect stuff here
1891
1892 if(s->shape != BIN_ONLY_SHAPE){
92073d01 1893 skip_bits(gb, 3); /* intra dc vlc threshold */
49092244
MN
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) {
92073d01 1902 s->f_code = get_bits(gb, 3); /* fcode_for */
49092244
MN
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) {
92073d01 1909 s->b_code = get_bits(gb, 3);
49092244
MN
1910 }
1911 }
49092244
MN
1912 }
1913 //FIXME new-pred stuff
92073d01
MN
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
1920void ff_mpeg4_clean_buffers(MpegEncContext *s)
1921{
1922 int c_wrap, c_xy, l_wrap, l_xy;
49092244
MN
1923
1924 l_wrap= s->block_wrap[0];
92073d01 1925 l_xy= s->mb_y*l_wrap*2 + s->mb_x*2;
49092244 1926 c_wrap= s->block_wrap[4];
92073d01 1927 c_xy= s->mb_y*c_wrap + s->mb_x;
49092244
MN
1928
1929 /* clean DC */
92073d01
MN
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);
49092244
MN
1933
1934 /* clean AC */
92073d01
MN
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));
49092244
MN
1938
1939 /* clean MV */
92073d01
MN
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));
49092244 1942// memset(s->motion_val, 0, 2*sizeof(INT16)*(2 + s->mb_width*2)*(2 + s->mb_height*2));
92073d01
MN
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 */
1950int 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
1985static 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
1995static 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 */
2009int 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;
49092244 2016 s->first_slice_line=1;
92073d01
MN
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");
edcf9890 2036 cbpc = get_vlc2(&s->gb, intra_MCBPC_vlc.table, INTRA_MCBPC_VLC_BITS, 1);
92073d01
MN
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 }
edcf9890 2101 cbpc = get_vlc2(&s->gb, inter_MCBPC_vlc.table, INTER_MCBPC_VLC_BITS, 2);
92073d01
MN
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);
edcf9890 2208 int cbpy = get_vlc2(&s->gb, cbpy_vlc.table, CBPY_VLC_BITS, 1);
92073d01
MN
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);
edcf9890 2220 int cbpy = get_vlc2(&s->gb, cbpy_vlc.table, CBPY_VLC_BITS, 1);
92073d01
MN
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{
edcf9890 2254 int cbpy = get_vlc2(&s->gb, cbpy_vlc.table, CBPY_VLC_BITS, 1);
92073d01
MN
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
2284static 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 }
49092244
MN
2397
2398 return 0;
2399}
2400
92073d01 2401
644d98a4
J
2402int 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 };
49c9325f 2408
c60a6560 2409 if(s->mb_x==0) PRINT_MB_TYPE("\n");
32cd20de 2410
49092244 2411 if(s->resync_marker){
92073d01 2412 if(s->resync_mb_x == s->mb_x && s->resync_mb_y+1 == s->mb_y){
49092244 2413 s->first_slice_line=0;
49092244
MN
2414 }
2415 }
2416
92073d01
MN
2417 if(s->data_partitioning && s->pict_type!=B_TYPE)
2418 return mpeg4_decode_partitioned_mb(s, block);
2419
73c8e514 2420 if (s->pict_type == P_TYPE || s->pict_type==S_TYPE) {
612476ef 2421 if (get_bits1(&s->gb)) {
de6d9b64
FB
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;
44eb4951
MN
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;
32cd20de 2431 PRINT_MB_TYPE("G");
44eb4951 2432 s->mcsel=1;
14ae07d8
MN
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 }
44eb4951
MN
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{
32cd20de 2447 PRINT_MB_TYPE("S");
44eb4951
MN
2448 s->mcsel=0;
2449 s->mv[0][0][0] = 0;
2450 s->mv[0][0][1] = 0;
2451 s->mb_skiped = 1;
2452 }
de6d9b64
FB
2453 return 0;
2454 }
edcf9890 2455 cbpc = get_vlc2(&s->gb, inter_MCBPC_vlc.table, INTER_MCBPC_VLC_BITS, 2);
4949028f 2456 //fprintf(stderr, "\tCBPC: %d", cbpc);
de6d9b64
FB
2457 if (cbpc < 0)
2458 return -1;
96815ce9
J
2459 if (cbpc > 20)
2460 cbpc+=3;
2461 else if (cbpc == 20)
2462 fprintf(stderr, "Stuffing !");
4949028f 2463
de6d9b64
FB
2464 dquant = cbpc & 8;
2465 s->mb_intra = ((cbpc & 4) != 0);
6f91bcd1
MN
2466 if (s->mb_intra) goto intra;
2467
73c8e514
MN
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;
edcf9890 2471 cbpy = get_vlc2(&s->gb, cbpy_vlc.table, CBPY_VLC_BITS, 1);
de6d9b64
FB
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;
49092244 2479 h263_dc_scale(s);
de6d9b64
FB
2480 }
2481 s->mv_dir = MV_DIR_FORWARD;
2482 if ((cbpc & 16) == 0) {
32cd20de 2483 PRINT_MB_TYPE("P");
de6d9b64
FB
2484 /* 16x16 motion prediction */
2485 s->mv_type = MV_TYPE_16X16;
2486 h263_pred_motion(s, 0, &pred_x, &pred_y);
544286b3 2487 if (s->umvplus_dec)
6dbd39fe 2488 mx = h263p_decode_umotion(s, pred_x);
73c8e514 2489 else if(!s->mcsel)
6f91bcd1 2490 mx = h263_decode_motion(s, pred_x, s->f_code);
44eb4951
MN
2491 else {
2492 const int a= s->sprite_warping_accuracy;
2493// int l = (1 << (s->f_code - 1)) * 32;
14ae07d8
MN
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");
44eb4951 2501 }
de6d9b64
FB
2502 if (mx >= 0xffff)
2503 return -1;
6dbd39fe 2504
544286b3 2505 if (s->umvplus_dec)
6dbd39fe 2506 my = h263p_decode_umotion(s, pred_y);
73c8e514 2507 else if(!s->mcsel)
6f91bcd1 2508 my = h263_decode_motion(s, pred_y, s->f_code);
44eb4951
MN
2509 else{
2510 const int a= s->sprite_warping_accuracy;
2511// int l = (1 << (s->f_code - 1)) * 32;
14ae07d8
MN
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");
44eb4951 2519 }
de6d9b64
FB
2520 if (my >= 0xffff)
2521 return -1;
2522 s->mv[0][0][0] = mx;
2523 s->mv[0][0][1] = my;
102d3908
J
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);*/
544286b3 2527 if (s->umvplus_dec && (mx - pred_x) == 1 && (my - pred_y) == 1)
6dbd39fe
J
2528 skip_bits1(&s->gb); /* Bit stuffing to prevent PSC */
2529
de6d9b64 2530 } else {
32cd20de 2531 PRINT_MB_TYPE("4");
de6d9b64
FB
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);
544286b3 2535 if (s->umvplus_dec)
6dbd39fe
J
2536 mx = h263p_decode_umotion(s, pred_x);
2537 else
6f91bcd1 2538 mx = h263_decode_motion(s, pred_x, s->f_code);
de6d9b64
FB
2539 if (mx >= 0xffff)
2540 return -1;
6dbd39fe 2541
544286b3 2542 if (s->umvplus_dec)
6dbd39fe
J
2543 my = h263p_decode_umotion(s, pred_y);
2544 else
6f91bcd1 2545 my = h263_decode_motion(s, pred_y, s->f_code);
de6d9b64
FB
2546 if (my >= 0xffff)
2547 return -1;
2548 s->mv[0][i][0] = mx;
2549 s->mv[0][i][1] = my;
544286b3 2550 if (s->umvplus_dec && (mx - pred_x) == 1 && (my - pred_y) == 1)
6dbd39fe 2551 skip_bits1(&s->gb); /* Bit stuffing to prevent PSC */
de6d9b64
FB
2552 mot_val[0] = mx;
2553 mot_val[1] = my;
2554 }
2555 }
6f91bcd1
MN
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;
9dbcbd92
MN
2560 uint16_t time_pp;
2561 uint16_t time_pb;
6f91bcd1
MN
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;
d633c2c0
MN
2588//FIXME is this correct?
2589/* s->last_mv[0][0][0]=
2590 s->last_mv[0][0][1]=0;*/
c60a6560 2591 PRINT_MB_TYPE("s");
6f91bcd1
MN
2592 return 0;
2593 }
2594
2595 modb1= get_bits1(&s->gb);
2596 if(modb1==0){
2597 modb2= get_bits1(&s->gb);
edcf9890 2598 mb_type= get_vlc2(&s->gb, mb_type_b_vlc.table, MB_TYPE_B_VLC_BITS, 1);
6f91bcd1
MN
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;
49092244 2608 h263_dc_scale(s);
6f91bcd1
MN
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 {
9dbcbd92 2619 case 0: /* direct */
6f91bcd1
MN
2620 mx = h263_decode_motion(s, 0, 1);
2621 my = h263_decode_motion(s, 0, 1);
9dbcbd92 2622 case 4: /* direct with mx=my=0 */
6f91bcd1 2623 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
4278e7a6 2624 xy= s->block_index[0];
9dbcbd92
MN
2625 time_pp= s->pp_time;
2626 time_pb= time_pp - s->bp_time;
6f91bcd1
MN
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;*/
32cd20de 2640 PRINT_MB_TYPE("D");
6f91bcd1
MN
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;
32cd20de 2653 PRINT_MB_TYPE("i");
6f91bcd1
MN
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;
32cd20de 2661 PRINT_MB_TYPE("B");
6f91bcd1
MN
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;
32cd20de 2669 PRINT_MB_TYPE("F");
6f91bcd1 2670 break;
e71a4f74
MN
2671 default:
2672 printf("illegal MB_type\n");
2673 return -1;
6f91bcd1
MN
2674 }
2675 } else { /* I-Frame */
edcf9890 2676 cbpc = get_vlc2(&s->gb, intra_MCBPC_vlc.table, INTRA_MCBPC_VLC_BITS, 1);
6f91bcd1
MN
2677 if (cbpc < 0)
2678 return -1;
2679 dquant = cbpc & 4;
2680 s->mb_intra = 1;
2681intra:
32cd20de 2682 PRINT_MB_TYPE("I");
de6d9b64 2683 s->ac_pred = 0;
d140623f 2684 if (s->h263_pred || s->h263_aic) {
612476ef 2685 s->ac_pred = get_bits1(&s->gb);
d140623f
J
2686 if (s->ac_pred && s->h263_aic)
2687 s->h263_aic_dir = get_bits1(&s->gb);
de6d9b64 2688 }
edcf9890 2689 cbpy = get_vlc2(&s->gb, cbpy_vlc.table, CBPY_VLC_BITS, 1);
92073d01 2690 if(cbpy<0) return -1;
de6d9b64
FB
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;
49092244 2698 h263_dc_scale(s);
de6d9b64
FB
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
6f91bcd1 2717static int h263_decode_motion(MpegEncContext * s, int pred, int f_code)
de6d9b64
FB
2718{
2719 int code, val, sign, shift, l, m;
2720
edcf9890 2721 code = get_vlc2(&s->gb, mv_vlc.table, MV_VLC_BITS, 2);
de6d9b64
FB
2722 if (code < 0)
2723 return 0xffff;
2724
2725 if (code == 0)
2726 return pred;
612476ef 2727 sign = get_bits1(&s->gb);
6f91bcd1 2728 shift = f_code - 1;
de6d9b64
FB
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;
dd536160 2736
de6d9b64
FB
2737 /* modulo decoding */
2738 if (!s->h263_long_vectors) {
6f91bcd1 2739 l = (1 << (f_code - 1)) * 32;
de6d9b64
FB
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;
102d3908 2752
de6d9b64
FB
2753 }
2754 return val;
2755}
2756
6dbd39fe
J
2757/* Decodes RVLC of H.263+ UMV */
2758static 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
de6d9b64
FB
2783static 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;
d140623f 2788 const UINT8 *scan_table;
de6d9b64 2789
d140623f 2790 scan_table = zigzag_direct;
9e15ad28 2791 if (s->h263_aic && s->mb_intra) {
d140623f
J
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 */
de6d9b64
FB
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;
d140623f 2822 i = 1;
de6d9b64 2823 } else {
d140623f 2824 i = 0;
de6d9b64
FB
2825 }
2826 if (!coded) {
9e15ad28
J
2827 if (s->mb_intra && s->h263_aic)
2828 goto not_coded;
de6d9b64
FB
2829 s->block_last_index[n] = i - 1;
2830 return 0;
2831 }
2832
2833 for(;;) {
edcf9890 2834 code = get_vlc2(&s->gb, rl->vlc.table, TEX_VLC_BITS, 2);
de6d9b64
FB
2835 if (code < 0)
2836 return -1;
2837 if (code == rl->n) {
2838 /* escape */
612476ef 2839 last = get_bits1(&s->gb);
de6d9b64
FB
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;
612476ef 2851 if (get_bits1(&s->gb))
de6d9b64
FB
2852 level = -level;
2853 }
2854 i += run;
2855 if (i >= 64)
2856 return -1;
d140623f 2857 j = scan_table[i];
de6d9b64
FB
2858 block[j] = level;
2859 if (last)
2860 break;
2861 i++;
2862 }
9e15ad28
J
2863not_coded:
2864 if (s->mb_intra && s->h263_aic) {
2865 h263_pred_acdc(s, block, n);
d6231b9e 2866 i = 63;
d140623f 2867 }
de6d9b64
FB
2868 s->block_last_index[n] = i;
2869 return 0;
2870}
2871
92073d01 2872static inline int mpeg4_decode_dc(MpegEncContext * s, int n, int *dir_ptr)
de6d9b64
FB
2873{
2874 int level, pred, code;
2875 UINT16 *dc_val;
2876
2877 if (n < 4)
edcf9890 2878 code = get_vlc2(&s->gb, dc_lum.table, DC_VLC_BITS, 1);
de6d9b64 2879 else
edcf9890 2880 code = get_vlc2(&s->gb, dc_chrom.table, DC_VLC_BITS, 1);
92073d01
MN
2881 if (code < 0 || code > 9 /* && s->nbit<9 */){
2882 fprintf(stderr, "illegal dc vlc\n");
de6d9b64 2883 return -1;
92073d01 2884 }
de6d9b64
FB
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));
92073d01
MN
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 }
de6d9b64
FB
2897 }
2898
8f8402e4 2899 pred = ff_mpeg4_pred_dc(s, n, &dc_val, dir_ptr);
de6d9b64
FB
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
92073d01
MN
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 */
2917static inline int mpeg4_decode_block(MpegEncContext * s, DCTELEM * block,
de6d9b64
FB
2918 int n, int coded)
2919{
c03a7172 2920 int level, i, last, run;
de6d9b64
FB
2921 int dc_pred_dir;
2922 RLTable *rl;
c03a7172 2923 RL_VLC_ELEM *rl_vlc;
de6d9b64 2924 const UINT8 *scan_table;
c03a7172 2925 int qmul, qadd;
de6d9b64
FB
2926
2927 if (s->mb_intra) {
2928 /* DC coef */
92073d01
MN
2929 if(s->data_partitioning && s->pict_type!=B_TYPE){
2930 level = s->dc_val[0][ s->block_index[n] ];
2931 if(n<4) level= (level + (s->y_dc_scale>>1))/s->y_dc_scale; //FIXME optimizs
2932 else level= (level + (s->c_dc_scale>>1))/s->c_dc_scale;
2933 dc_pred_dir= (s->pred_dir_table[s->mb_x + s->mb_y*s->mb_width]<<n)&32;
2934 }else{
2935 level = mpeg4_decode_dc(s, n, &dc_pred_dir);
2936 if (level < 0)
2937 return DECODING_ACDC_LOST;
2938 }
de6d9b64 2939 block[0] = level;
c03a7172 2940 i = 0;
de6d9b64
FB
2941 if (!coded)
2942 goto not_coded;
2943 rl = &rl_intra;
c03a7172 2944 rl_vlc = rl_intra.rl_vlc[0];
de6d9b64
FB
2945 if (s->ac_pred) {
2946 if (dc_pred_dir == 0)
2947 scan_table = ff_alternate_vertical_scan; /* left */
2948 else
2949 scan_table = ff_alternate_horizontal_scan; /* top */
2950 } else {
2951 scan_table = zigzag_direct;
2952 }
c03a7172
MN
2953 qmul=1;
2954 qadd=0;
de6d9b64 2955 } else {
c03a7172 2956 i = -1;
de6d9b64 2957 if (!coded) {
c03a7172 2958 s->block_last_index[n] = i;
de6d9b64
FB
2959 return 0;
2960 }
2961 rl = &rl_inter;
c03a7172 2962 rl_vlc = rl_inter.rl_vlc[s->qscale];
de6d9b64 2963 scan_table = zigzag_direct;
c03a7172
MN
2964 qmul = s->qscale << 1;
2965 qadd = (s->qscale - 1) | 1;
de6d9b64 2966 }
c03a7172
MN
2967 {
2968 OPEN_READER(re, &s->gb);
de6d9b64 2969 for(;;) {
c03a7172
MN
2970 UPDATE_CACHE(re, &s->gb);
2971 GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2);
2972 if (level==0) {
2973 int cache;
2974 cache= GET_CACHE(re, &s->gb);
de6d9b64 2975 /* escape */
c03a7172
MN
2976 if (cache&0x80000000) {
2977 if (cache&0x40000000) {
de6d9b64 2978 /* third escape */
c03a7172
MN
2979 SKIP_CACHE(re, &s->gb, 2);
2980 last= SHOW_UBITS(re, &s->gb, 1); SKIP_CACHE(re, &s->gb, 1);
2981 run= SHOW_UBITS(re, &s->gb, 6); LAST_SKIP_CACHE(re, &s->gb, 6);
2982 SKIP_COUNTER(re, &s->gb, 2+1+6);
2983 UPDATE_CACHE(re, &s->gb);
2984
2985 if(SHOW_UBITS(re, &s->gb, 1)==0){
92073d01
MN
2986 fprintf(stderr, "1. marker bit missing in 3. esc\n");
2987 return DECODING_AC_LOST;
c03a7172
MN
2988 }; SKIP_CACHE(re, &s->gb, 1);
2989
2990 level= SHOW_SBITS(re, &s->gb, 12); SKIP_CACHE(re, &s->gb, 12);
2991
2992 if(SHOW_UBITS(re, &s->gb, 1)==0){
92073d01
MN
2993 fprintf(stderr, "2. marker bit missing in 3. esc\n");
2994 return DECODING_AC_LOST;
c03a7172
MN
2995 }; LAST_SKIP_CACHE(re, &s->gb, 1);
2996
2997 SKIP_COUNTER(re, &s->gb, 1+12+1);
2998
92073d01
MN
2999 if(level>512 || level<-512){ //FIXME check that QP=1 is ok with this too
3000 fprintf(stderr, "|level| overflow in 3. esc\n");
3001 return DECODING_AC_LOST;
3002 }
3003#if 1
3004 {
3005 const int abs_level= ABS(level);
92073d01 3006 if(abs_level<=MAX_LEVEL && run<=MAX_RUN && s->error_resilience>=0){
8f8402e4 3007 const int run1= run - rl->max_run[last][abs_level] - 1;
92073d01
MN
3008 if(abs_level <= rl->max_level[last][run]){
3009 fprintf(stderr, "illegal 3. esc, vlc encoding possible\n");
3010 return DECODING_AC_LOST;
3011 }
3012 if(abs_level <= rl->max_level[last][run]*2){
3013 fprintf(stderr, "illegal 3. esc, esc 1 encoding possible\n");
3014 return DECODING_AC_LOST;
3015 }
dd536160 3016 if(run1 >= 0 && abs_level <= rl->max_level[last][run1]){
92073d01
MN
3017 fprintf(stderr, "illegal 3. esc, esc 2 encoding possible\n");
3018 return DECODING_AC_LOST;
3019 }
3020 }
3021 }
3022#endif
c03a7172
MN
3023 if (level>0) level= level * qmul + qadd;
3024 else level= level * qmul - qadd;
3025
3026 i+= run + 1;
3027 if(last) i+=192;
de6d9b64
FB
3028 } else {
3029 /* second escape */
c03a7172
MN
3030#if MIN_CACHE_BITS < 20
3031 LAST_SKIP_BITS(re, &s->gb, 2);
3032 UPDATE_CACHE(re, &s->gb);
3033#else
3034 SKIP_BITS(re, &s->gb, 2);
3035#endif
3036 GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2);
3037 i+= run + rl->max_run[run>>7][level/qmul] +1; //FIXME opt indexing
3038 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
3039 LAST_SKIP_BITS(re, &s->gb, 1);
de6d9b64
FB
3040 }
3041 } else {
3042 /* first escape */
c03a7172
MN
3043#if MIN_CACHE_BITS < 19
3044 LAST_SKIP_BITS(re, &s->gb, 1);
3045 UPDATE_CACHE(re, &s->gb);
3046#else
3047 SKIP_BITS(re, &s->gb, 1);
3048#endif
3049 GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2);
3050 i+= run;
3051 level = level + rl->max_level[run>>7][(run-1)&63] * qmul;//FIXME opt indexing
3052 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
3053 LAST_SKIP_BITS(re, &s->gb, 1);
de6d9b64
FB
3054 }
3055 } else {
c03a7172
MN
3056 i+= run;
3057 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
3058 LAST_SKIP_BITS(re, &s->gb, 1);
3059 }
3060 if (i > 62){
3061 i-= 192;
3062 if(i&(~63)){
3063 fprintf(stderr, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
3064 return DECODING_AC_LOST;
3065 }
3066
3067 block[scan_table[i]] = level;
de6d9b64 3068 break;
c03a7172
MN
3069 }
3070
3071 block[scan_table[i]] = level;
de6d9b64 3072 }
c03a7172
MN
3073 CLOSE_READER(re, &s->gb);
3074 }
de6d9b64
FB
3075 not_coded:
3076 if (s->mb_intra) {
3077 mpeg4_pred_ac(s, block, n, dc_pred_dir);
3078 if (s->ac_pred) {
c03a7172 3079 i = 63; /* XXX: not optimal */
de6d9b64
FB
3080 }
3081 }
c03a7172 3082 s->block_last_index[n] = i;
de6d9b64
FB
3083 return 0;
3084}
3085
3086/* most is hardcoded. should extend to handle all h263 streams */
3087int h263_decode_picture_header(MpegEncContext *s)
3088{
3089 int format, width, height;
3090
1e209222
J
3091 /* picture start code */
3092 if (get_bits(&s->gb, 22) != 0x20) {
3093 fprintf(stderr, "Bad picture start code\n");
de6d9b64 3094 return -1;
1e209222
J
3095 }
3096 /* temporal reference */
81401c1f 3097 s->picture_number = get_bits(&s->gb, 8); /* picture timestamp */
1e209222
J
3098
3099 /* PTYPE starts here */
3100 if (get_bits1(&s->gb) != 1) {
3101 /* marker */
3102 fprintf(stderr, "Bad marker\n");
3103 return -1;
3104 }
3105 if (get_bits1(&s->gb) != 0) {
3106 fprintf(stderr, "Bad H263 id\n");
de6d9b64 3107 return -1; /* h263 id */
1e209222 3108 }
612476ef
A
3109 skip_bits1(&s->gb); /* split screen off */
3110 skip_bits1(&s->gb); /* camera off */
3111 skip_bits1(&s->gb); /* freeze picture release off */
de6d9b64 3112
8547282b 3113 /* Reset GOB number */
102d3908 3114 s->gob_number = 0;
8547282b 3115
de6d9b64 3116 format = get_bits(&s->gb, 3);
1e209222
J
3117 /*
3118 0 forbidden
3119 1 sub-QCIF
3120 10 QCIF
3121 7 extended PTYPE (PLUSPTYPE)
3122 */
de6d9b64 3123
96815ce9 3124 if (format != 7 && format != 6) {
de6d9b64
FB
3125 s->h263_plus = 0;
3126 /* H.263v1 */
3127 width = h263_format[format][0];
3128 height = h263_format[format][1];
3129 if (!width)
3130 return -1;
96815ce9
J
3131
3132 s->width = width;
3133 s->height = height;
612476ef 3134 s->pict_type = I_TYPE + get_bits1(&s->gb);
de6d9b64 3135
612476ef 3136 s->unrestricted_mv = get_bits1(&s->gb);
de6d9b64
FB
3137 s->h263_long_vectors = s->unrestricted_mv;
3138
1e209222
J
3139 if (get_bits1(&s->gb) != 0) {
3140 fprintf(stderr, "H263 SAC not supported\n");
de6d9b64 3141 return -1; /* SAC: off */
1e209222 3142 }
96815ce9
J
3143 if (get_bits1(&s->gb) != 0) {
3144 s->mv_type = MV_TYPE_8X8; /* Advanced prediction mode */
3145 }
3146
1e209222
J
3147 if (get_bits1(&s->gb) != 0) {
3148 fprintf(stderr, "H263 PB frame not supported\n");
de6d9b64 3149 return -1; /* not PB frame */
1e209222 3150 }
de6d9b64 3151 s->qscale = get_bits(&s->gb, 5);
612476ef 3152 skip_bits1(&s->gb); /* Continuous Presence Multipoint mode: off */
de6d9b64 3153 } else {
96815ce9
J
3154 int ufep;
3155
de6d9b64 3156 /* H.263v2 */
96815ce9
J
3157 s->h263_plus = 1;
3158 ufep = get_bits(&s->gb, 3); /* Update Full Extended PTYPE */
1e209222
J
3159
3160 /* ufep other than 0 and 1 are reserved */
96815ce9
J
3161 if (ufep == 1) {
3162 /* OPPTYPE */
3163 format = get_bits(&s->gb, 3);
1e209222 3164 dprintf("ufep=1, format: %d\n", format);
96815ce9
J
3165 skip_bits(&s->gb,1); /* Custom PCF */
3166 s->umvplus_dec = get_bits(&s->gb, 1); /* Unrestricted Motion Vector */
3167 skip_bits1(&s->gb); /* Syntax-based Arithmetic Coding (SAC) */
3168 if (get_bits1(&s->gb) != 0) {
3169 s->mv_type = MV_TYPE_8X8; /* Advanced prediction mode */
3170 }
d140623f
J
3171 if (get_bits1(&s->gb) != 0) { /* Advanced Intra Coding (AIC) */
3172 s->h263_aic = 1;
3173 }
1e209222 3174
d140623f 3175 skip_bits(&s->gb, 7);
1e209222
J
3176 /* these are the 7 bits: (in order of appearence */
3177 /* Deblocking Filter */
3178 /* Slice Structured */
3179 /* Reference Picture Selection */
3180 /* Independent Segment Decoding */
3181 /* Alternative Inter VLC */
3182 /* Modified Quantization */
3183 /* Prevent start code emulation */
3184
96815ce9 3185 skip_bits(&s->gb, 3); /* Reserved */
1e209222
J
3186 } else if (ufep != 0) {
3187 fprintf(stderr, "Bad UFEP type (%d)\n", ufep);
96815ce9 3188 return -1;
1e209222 3189 }
96815ce9 3190
6dbd39fe 3191 /* MPPTYPE */
1e209222
J
3192 s->pict_type = get_bits(&s->gb, 3) + I_TYPE;
3193 dprintf("pict_type: %d\n", s->pict_type);
de6d9b64
FB
3194 if (s->pict_type != I_TYPE &&
3195 s->pict_type != P_TYPE)
3196 return -1;
e7774f11
J
3197 skip_bits(&s->gb, 2);
3198 s->no_rounding = get_bits1(&s->gb);
1e209222 3199 dprintf("RTYPE: %d\n", s->no_rounding);
e7774f11 3200 skip_bits(&s->gb, 4);
6dbd39fe
J
3201
3202 /* Get the picture dimensions */
96815ce9
J
3203 if (ufep) {
3204 if (format == 6) {
3205 /* Custom Picture Format (CPFMT) */
1e209222
J
3206 s->aspect_ratio_info = get_bits(&s->gb, 4);
3207 dprintf("aspect: %d\n", s->aspect_ratio_info);
3208 /* aspect ratios:
3209 0 - forbidden
3210 1 - 1:1
3211 2 - 12:11 (CIF 4:3)
3212 3 - 10:11 (525-type 4:3)
3213 4 - 16:11 (CIF 16:9)
3214 5 - 40:33 (525-type 16:9)
3215 6-14 - reserved
3216 */
96815ce9
J
3217 width = (get_bits(&s->gb, 9) + 1) * 4;
3218 skip_bits1(&s->gb);
3219 height = get_bits(&s->gb, 9) * 4;
1e209222
J
3220 dprintf("\nH.263+ Custom picture: %dx%d\n",width,height);
3221 if (s->aspect_ratio_info == EXTENDED_PAR) {
3222 /* aspected dimensions */
3223 skip_bits(&s->gb, 8); /* width */
3224 skip_bits(&s->gb, 8); /* height */
3225 }
3226 } else {
96815ce9
J
3227 width = h263_format[format][0];
3228 height = h263_format[format][1];
3229 }
3230 if ((width == 0) || (height == 0))
3231 return -1;
3232 s->width = width;
3233 s->height = height;
3234 if (s->umvplus_dec) {
3235 skip_bits1(&s->gb); /* Unlimited Unrestricted Motion Vectors Indicator (UUI) */
3236 }
6dbd39fe
J
3237 }
3238
de6d9b64
FB
3239 s->qscale = get_bits(&s->gb, 5);
3240 }
3241 /* PEI */
612476ef
A
3242 while (get_bits1(&s->gb) != 0) {
3243 skip_bits(&s->gb, 8);
de6d9b64
FB
3244 }
3245 s->f_code = 1;
8f8402e4
MN
3246
3247 if(s->h263_aic){
3248 s->y_dc_scale_table=
3249 s->c_dc_scale_table= h263_aic_dc_scale_table;
3250 }else{
3251 s->y_dc_scale_table=
3252 s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
3253 }
3254
de6d9b64
FB
3255 return 0;
3256}
3257
73c8e514
MN
3258static void mpeg4_decode_sprite_trajectory(MpegEncContext * s)
3259{
3260 int i;
3261 int a= 2<<s->sprite_warping_accuracy;
3262 int rho= 3-s->sprite_warping_accuracy;
3263 int r=16/a;
3264 const int vop_ref[4][2]= {{0,0}, {s->width,0}, {0, s->height}, {s->width, s->height}}; // only true for rectangle shapes
3265 int d[4][2]={{0,0}, {0,0}, {0,0}, {0,0}};
3266 int sprite_ref[4][2];
3267 int virtual_ref[2][2];
3268 int w2, h2;
3269 int alpha=0, beta=0;
3270 int w= s->width;
3271 int h= s->height;
44eb4951 3272//printf("SP %d\n", s->sprite_warping_accuracy);
73c8e514
MN
3273 for(i=0; i<s->num_sprite_warping_points; i++){
3274 int length;
3275 int x=0, y=0;
3276
3277 length= get_vlc(&s->gb, &sprite_trajectory);
3278 if(length){
3279 x= get_bits(&s->gb, length);
44eb4951 3280//printf("lx %d %d\n", length, x);
73c8e514
MN
3281 if ((x >> (length - 1)) == 0) /* if MSB not set it is negative*/
3282 x = - (x ^ ((1 << length) - 1));
3283 }
44eb4951 3284 if(!(s->divx_version==500 && s->divx_build==413)) skip_bits1(&s->gb); /* marker bit */
73c8e514
MN
3285
3286 length= get_vlc(&s->gb, &sprite_trajectory);
3287 if(length){
3288 y=get_bits(&s->gb, length);
44eb4951 3289//printf("ly %d %d\n", length, y);
73c8e514
MN
3290 if ((y >> (length - 1)) == 0) /* if MSB not set it is negative*/
3291 y = - (y ^ ((1 << length) - 1));
3292 }
3293 skip_bits1(&s->gb); /* marker bit */
44eb4951 3294//printf("%d %d %d %d\n", x, y, i, s->sprite_warping_accuracy);
73c8e514 3295//if(i>0 && (x!=0 || y!=0)) printf("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n");
44eb4951 3296//x=y=0;
73c8e514
MN
3297 d[i][0]= x;
3298 d[i][1]= y;
3299 }
3300
3301 while((1<<alpha)<w) alpha++;
3302 while((1<<beta )<h) beta++; // there seems to be a typo in the mpeg4 std for the definition of w' and h'
3303 w2= 1<<alpha;
3304 h2= 1<<beta;
3305
3306// Note, the 4th point isnt used for GMC
6f91bcd1
MN
3307 if(s->divx_version==500 && s->divx_build==413){
3308 sprite_ref[0][0]= a*vop_ref[0][0] + d[0][0];
3309 sprite_ref[0][1]= a*vop_ref[0][1] + d[0][1];
3310 sprite_ref[1][0]= a*vop_ref[1][0] + d[0][0] + d[1][0];
3311 sprite_ref[1][1]= a*vop_ref[1][1] + d[0][1] + d[1][1];
3312 sprite_ref[2][0]= a*vop_ref[2][0] + d[0][0] + d[2][0];
3313 sprite_ref[2][1]= a*vop_ref[2][1] + d[0][1] + d[2][1];
3314 } else {
3315 sprite_ref[0][0]= (a>>1)*(2*vop_ref[0][0] + d[0][0]);
3316 sprite_ref[0][1]= (a>>1)*(2*vop_ref[0][1] + d[0][1]);
3317 sprite_ref[1][0]= (a>>1)*(2*vop_ref[1][0] + d[0][0] + d[1][0]);
3318 sprite_ref[1][1]= (a>>1)*(2*vop_ref[1][1] + d[0][1] + d[1][1]);
3319 sprite_ref[2][0]= (a>>1)*(2*vop_ref[2][0] + d[0][0] + d[2][0]);
3320 sprite_ref[2][1]= (a>>1)*(2*vop_ref[2][1] + d[0][1] + d[2][1]);
3321 }
73c8e514
MN
3322/* sprite_ref[3][0]= (a>>1)*(2*vop_ref[3][0] + d[0][0] + d[1][0] + d[2][0] + d[3][0]);
3323 sprite_ref[3][1]= (a>>1)*(2*vop_ref[3][1] + d[0][1] + d[1][1] + d[2][1] + d[3][1]); */
3324
3325// this is mostly identical to the mpeg4 std (and is totally unreadable because of that ...)
3326// perhaps it should be reordered to be more readable ...
3327// the idea behind this virtual_ref mess is to be able to use shifts later per pixel instead of divides
3328// so the distance between points is converted from w&h based to w2&h2 based which are of the 2^x form
73c8e514 3329 virtual_ref[0][0]= 16*(vop_ref[0][0] + w2)
32cd20de 3330 + 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);
73c8e514 3331 virtual_ref[0][1]= 16*vop_ref[0][1]
32cd20de 3332 + 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);
73c8e514 3333 virtual_ref[1][0]= 16*vop_ref[0][0]
32cd20de 3334 + 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);
73c8e514 3335 virtual_ref[1][1]= 16*(vop_ref[0][1] + h2)
32cd20de 3336 + ROUNDED_DIV(((h - h2)*(r*sprite_ref[0][1] - 16*vop_ref[0][1]) + h2*(r*sprite_ref[2][1] - 16*vop_ref[2][1])),h);
73c8e514
MN
3337
3338 switch(s->num_sprite_warping_points)
3339 {
3340 case 0:
3341 s->sprite_offset[0][0]= 0;
3342 s->sprite_offset[0][1]= 0;
3343 s->sprite_offset[1][0]= 0;
3344 s->sprite_offset[1][1]= 0;
3345 s->sprite_delta[0][0][0]= a;
3346 s->sprite_delta[0][0][1]= 0;
3347 s->sprite_delta[0][1][0]= 0;
3348 s->sprite_delta[0][1][1]= a;
3349 s->sprite_delta[1][0][0]= a;
3350 s->sprite_delta[1][0][1]= 0;
3351 s->sprite_delta[1][1][0]= 0;
3352 s->sprite_delta[1][1][1]= a;
3353 s->sprite_shift[0][0]= 0;
3354 s->sprite_shift[0][1]= 0;
3355 s->sprite_shift[1][0]= 0;
3356 s->sprite_shift[1][1]= 0;
3357 break;
3358 case 1: //GMC only
3359 s->sprite_offset[0][0]= sprite_ref[0][0] - a*vop_ref[0][0];
3360 s->sprite_offset[0][1]= sprite_ref[0][1] - a*vop_ref[0][1];
3361 s->sprite_offset[1][0]= ((sprite_ref[0][0]>>1)|(sprite_ref[0][0]&1)) - a*(vop_ref[0][0]/2);
3362 s->sprite_offset[1][1]= ((sprite_ref[0][1]>>1)|(sprite_ref[0][1]&1)) - a*(vop_ref[0][1]/2);
3363 s->sprite_delta[0][0][0]= a;
3364 s->sprite_delta[0][0][1]= 0;
3365 s->sprite_delta[0][1][0]= 0;
3366 s->sprite_delta[0][1][1]= a;
3367 s->sprite_delta[1][0][0]= a;
3368 s->sprite_delta[1][0][1]= 0;
3369 s->sprite_delta[1][1][0]= 0;
3370 s->sprite_delta[1][1][1]= a;
3371 s->sprite_shift[0][0]= 0;
3372 s->sprite_shift[0][1]= 0;
3373 s->sprite_shift[1][0]= 0;
3374 s->sprite_shift[1][1]= 0;
3375 break;
3376 case 2:
3377 case 3: //FIXME
3378 s->sprite_offset[0][0]= (sprite_ref[0][0]<<(alpha+rho))
3379 + ((-r*sprite_ref[0][0] + virtual_ref[0][0])*(-vop_ref[0][0])
3380 +( r*sprite_ref[0][1] - virtual_ref[0][1])*(-vop_ref[0][1]));
3381 s->sprite_offset[0][1]= (sprite_ref[0][1]<<(alpha+rho))
3382 + ((-r*sprite_ref[0][1] + virtual_ref[0][1])*(-vop_ref[0][0])
3383 +(-r*sprite_ref[0][0] + virtual_ref[0][0])*(-vop_ref[0][1]));
3384 s->sprite_offset[1][0]= ((-r*sprite_ref[0][0] + virtual_ref[0][0])*(-2*vop_ref[0][0] + 1)
3385 +( r*sprite_ref[0][1] - virtual_ref[0][1])*(-2*vop_ref[0][1] + 1)
3386 +2*w2*r*sprite_ref[0][0] - 16*w2);
3387 s->sprite_offset[1][1]= ((-r*sprite_ref[0][1] + virtual_ref[0][1])*(-2*vop_ref[0][0] + 1)
3388 +(-r*sprite_ref[0][0] + virtual_ref[0][0])*(-2*vop_ref[0][1] + 1)
3389 +2*w2*r*sprite_ref[0][1] - 16*w2);
3390 s->sprite_delta[0][0][0]= (-r*sprite_ref[0][0] + virtual_ref[0][0]);
3391 s->sprite_delta[0][0][1]= ( r*sprite_ref[0][1] - virtual_ref[0][1]);
3392 s->sprite_delta[0][1][0]= (-r*sprite_ref[0][1] + virtual_ref[0][1]);
3393 s->sprite_delta[0][1][1]= (-r*sprite_ref[0][0] + virtual_ref[0][0]);
3394 s->sprite_delta[1][0][0]= 4*(-r*sprite_ref[0][0] + virtual_ref[0][0]);
3395 s->sprite_delta[1][0][1]= 4*( r*sprite_ref[0][1] - virtual_ref[0][1]);
3396 s->sprite_delta[1][1][0]= 4*(-r*sprite_ref[0][1] + virtual_ref[0][1]);
3397 s->sprite_delta[1][1][1]= 4*(-r*sprite_ref[0][0] + virtual_ref[0][0]);
3398 s->sprite_shift[0][0]= alpha+rho;
3399 s->sprite_shift[0][1]= alpha+rho;
3400 s->sprite_shift[1][0]= alpha+rho+2;
3401 s->sprite_shift[1][1]= alpha+rho+2;
3402 break;
3403// case 3:
3404 break;
3405 }
44eb4951
MN
3406/*printf("%d %d\n", s->sprite_delta[0][0][0], a<<s->sprite_shift[0][0]);
3407printf("%d %d\n", s->sprite_delta[0][0][1], 0);
3408printf("%d %d\n", s->sprite_delta[0][1][0], 0);
3409printf("%d %d\n", s->sprite_delta[0][1][1], a<<s->sprite_shift[0][1]);
3410printf("%d %d\n", s->sprite_delta[1][0][0], a<<s->sprite_shift[1][0]);
3411printf("%d %d\n", s->sprite_delta[1][0][1], 0);
3412printf("%d %d\n", s->sprite_delta[1][1][0], 0);
3413printf("%d %d\n", s->sprite_delta[1][1][1], a<<s->sprite_shift[1][1]);*/
3414 /* try to simplify the situation */
3415 if( s->sprite_delta[0][0][0] == a<<s->sprite_shift[0][0]
3416 && s->sprite_delta[0][0][1] == 0
3417 && s->sprite_delta[0][1][0] == 0
3418 && s->sprite_delta[0][1][1] == a<<s->sprite_shift[0][1]
3419 && s->sprite_delta[1][0][0] == a<<s->sprite_shift[1][0]
3420 && s->sprite_delta[1][0][1] == 0
3421 && s->sprite_delta[1][1][0] == 0
3422 && s->sprite_delta[1][1][1] == a<<s->sprite_shift[1][1])
3423 {
3424 s->sprite_offset[0][0]>>=s->sprite_shift[0][0];
3425 s->sprite_offset[0][1]>>=s->sprite_shift[0][1];
3426 s->sprite_offset[1][0]>>=s->sprite_shift[1][0];
3427 s->sprite_offset[1][1]>>=s->sprite_shift[1][1];
3428 s->sprite_delta[0][0][0]= a;
3429 s->sprite_delta[0][0][1]= 0;
3430 s->sprite_delta[0][1][0]= 0;
3431 s->sprite_delta[0][1][1]= a;
3432 s->sprite_delta[1][0][0]= a;
3433 s->sprite_delta[1][0][1]= 0;
3434 s->sprite_delta[1][1][0]= 0;
3435 s->sprite_delta[1][1][1]= a;
3436 s->sprite_shift[0][0]= 0;
3437 s->sprite_shift[0][1]= 0;
3438 s->sprite_shift[1][0]= 0;
3439 s->sprite_shift[1][1]= 0;
3440 s->real_sprite_warping_points=1;
3441 }
3442 else
3443 s->real_sprite_warping_points= s->num_sprite_warping_points;
73c8e514 3444
14ae07d8 3445//printf("%d %d %d %d\n", d[0][0], d[0][1], s->sprite_offset[0][0], s->sprite_offset[0][1]);
73c8e514
MN
3446}
3447
de6d9b64
FB
3448/* decode mpeg4 VOP header */
3449int mpeg4_decode_picture_header(MpegEncContext * s)
3450{
3451 int time_incr, startcode, state, v;
9dbcbd92 3452 int time_increment;
de6d9b64
FB
3453
3454 redo:
3455 /* search next start code */
3456 align_get_bits(&s->gb);
3457 state = 0xff;
3458 for(;;) {
3459 v = get_bits(&s->gb, 8);
3460 if (state == 0x000001) {
3461 state = ((state << 8) | v) & 0xffffff;
3462 startcode = state;
3463 break;
3464 }
3465 state = ((state << 8) | v) & 0xffffff;
c7d5049c 3466 if( get_bits_count(&s->gb) > s->gb.size*8-32){
eec1c6b9
MN
3467 if(s->gb.size>50){
3468 printf("no VOP startcode found, frame size was=%d\n", s->gb.size);
3469 return -1;
3470 }else{
3471 printf("frame skip\n");
3472 return FRAME_SKIPED;
3473 }
10cd5815 3474 }
de6d9b64 3475 }
cc9ba006
MN
3476//printf("startcode %X %d\n", startcode, get_bits_count(&s->gb));
3477 if (startcode == 0x120) { // Video Object Layer
6f91bcd1 3478 int width, height, vo_ver_id;
de6d9b64
FB
3479
3480 /* vol header */
612476ef 3481 skip_bits(&s->gb, 1); /* random access */
1ff662cc 3482 s->vo_type= get_bits(&s->gb, 8);
d95ecd05
FB
3483 if (get_bits1(&s->gb) != 0) { /* is_ol_id */
3484 vo_ver_id = get_bits(&s->gb, 4); /* vo_ver_id */
3485 skip_bits(&s->gb, 3); /* vo_priority */
3486 } else {
3487 vo_ver_id = 1;
3488 }
1c2a8c7f 3489//printf("vo type:%d\n",s->vo_type);
cc9ba006 3490 s->aspect_ratio_info= get_bits(&s->gb, 4);
1e209222 3491 if(s->aspect_ratio_info == EXTENDED_PAR){
cc9ba006
MN
3492 skip_bits(&s->gb, 8); //par_width
3493 skip_bits(&s->gb, 8); // par_height
3494 }
11ce8834 3495
7aaf3b98 3496 if ((s->vol_control_parameters=get_bits1(&s->gb))) { /* vol control parameter */
1ff662cc
MN
3497 int chroma_format= get_bits(&s->gb, 2);
3498 if(chroma_format!=1){
3499 printf("illegal chroma format\n");
3500 }
3501 s->low_delay= get_bits1(&s->gb);
3502 if(get_bits1(&s->gb)){ /* vbv parameters */
3503 printf("vbv parameters not supported\n");
3504 return -1;
3505 }
3506 }else{
3507 s->low_delay=0;
cc9ba006 3508 }
1ff662cc 3509
d95ecd05 3510 s->shape = get_bits(&s->gb, 2); /* vol shape */
49c9325f 3511 if(s->shape != RECT_SHAPE) printf("only rectangular vol supported\n");
cc9ba006
MN
3512 if(s->shape == GRAY_SHAPE && vo_ver_id != 1){
3513 printf("Gray shape not supported\n");
3514 skip_bits(&s->gb, 4); //video_object_layer_shape_extension
3515 }
3516
612476ef 3517 skip_bits1(&s->gb); /* marker */
de6d9b64 3518
6f91bcd1
MN
3519 s->time_increment_resolution = get_bits(&s->gb, 16);
3520 s->time_increment_bits = av_log2(s->time_increment_resolution - 1) + 1;
d95ecd05
FB
3521 if (s->time_increment_bits < 1)
3522 s->time_increment_bits = 1;
612476ef 3523 skip_bits1(&s->gb); /* marker */
de6d9b64 3524
d95ecd05
FB
3525 if (get_bits1(&s->gb) != 0) { /* fixed_vop_rate */
3526 skip_bits(&s->gb, s->time_increment_bits);
3527 }
3528
cc9ba006
MN
3529 if (s->shape != BIN_ONLY_SHAPE) {
3530 if (s->shape == RECT_SHAPE) {
d95ecd05
FB
3531 skip_bits1(&s->gb); /* marker */
3532 width = get_bits(&s->gb, 13);
3533 skip_bits1(&s->gb); /* marker */
3534 height = get_bits(&s->gb, 13);
3535 skip_bits1(&s->gb); /* marker */
488fa19a
MN
3536 if(width && height){ /* they should be non zero but who knows ... */
3537 s->width = width;
3538 s->height = height;
ee6f7861 3539// printf("width/height: %d %d\n", width, height);
488fa19a 3540 }
d95ecd05
FB
3541 }
3542
49c9325f 3543 if(get_bits1(&s->gb)) printf("interlaced not supported\n"); /* interlaced */
92073d01 3544 if(!get_bits1(&s->gb)) printf("OBMC not supported (very likely buggy encoder)\n"); /* OBMC Disable */
d95ecd05
FB
3545 if (vo_ver_id == 1) {
3546 s->vol_sprite_usage = get_bits1(&s->gb); /* vol_sprite_usage */
3547 } else {
3548 s->vol_sprite_usage = get_bits(&s->gb, 2); /* vol_sprite_usage */
3549 }
73c8e514 3550 if(s->vol_sprite_usage==STATIC_SPRITE) printf("Static Sprites not supported\n");
cc9ba006
MN
3551 if(s->vol_sprite_usage==STATIC_SPRITE || s->vol_sprite_usage==GMC_SPRITE){
3552 if(s->vol_sprite_usage==STATIC_SPRITE){
3553 s->sprite_width = get_bits(&s->gb, 13);
3554 skip_bits1(&s->gb); /* marker */
3555 s->sprite_height= get_bits(&s->gb, 13);
3556 skip_bits1(&s->gb); /* marker */
3557 s->sprite_left = get_bits(&s->gb, 13);
3558 skip_bits1(&s->gb); /* marker */
3559 s->sprite_top = get_bits(&s->gb, 13);
3560 skip_bits1(&s->gb); /* marker */
3561 }