removed unused variables
[libav.git] / libavcodec / msmpeg4.c
CommitLineData
de6d9b64
FB
1/*
2 * MSMPEG4 backend for ffmpeg encoder and decoder
ff4ec49e 3 * Copyright (c) 2001 Fabrice Bellard.
de6d9b64 4 *
ff4ec49e
FB
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
de6d9b64 9 *
ff4ec49e 10 * This library is distributed in the hope that it will be useful,
de6d9b64 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
ff4ec49e
FB
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
de6d9b64 14 *
ff4ec49e
FB
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
92ba5ffb 18 *
287229e5 19 * msmpeg4v1 & v2 stuff by Michael Niedermayer <michaelni@gmx.at>
de6d9b64 20 */
6000abfa 21#include "avcodec.h"
de6d9b64
FB
22#include "dsputil.h"
23#include "mpegvideo.h"
bd5e1c72 24//#define PRINT_MB
92ba5ffb 25
de6d9b64
FB
26/*
27 * You can also call this codec : MPEG4 with a twist !
28 *
29 * TODO:
30 * - (encoding) select best mv table (two choices)
31 * - (encoding) select best vlc/dc table
de6d9b64
FB
32 */
33//#define DEBUG
34
08dce7b0
MN
35#define DC_VLC_BITS 9
36#define CBPY_VLC_BITS 6
37#define INTER_INTRA_VLC_BITS 3
38#define V1_INTRA_CBPC_VLC_BITS 6
39#define V1_INTER_CBPC_VLC_BITS 6
40#define V2_INTRA_CBPC_VLC_BITS 3
41#define V2_MB_TYPE_VLC_BITS 7
42#define MV_VLC_BITS 9
43#define V2_MV_VLC_BITS 9
44#define TEX_VLC_BITS 9
45#define MB_NON_INTRA_VLC_BITS 9
46#define MB_INTRA_VLC_BITS 9
47
05174fd4
MN
48#define II_BITRATE 128*1024
49#define MBAC_BITRATE 50*1024
50
84afee34
MN
51static UINT32 v2_dc_lum_table[512][2];
52static UINT32 v2_dc_chroma_table[512][2];
53
f5957f3f
MN
54static inline void msmpeg4_encode_block(MpegEncContext * s, DCTELEM * block, int n);
55static inline int msmpeg4_decode_block(MpegEncContext * s, DCTELEM * block,
56 int n, int coded);
de6d9b64
FB
57static int msmpeg4_decode_dc(MpegEncContext * s, int n, int *dir_ptr);
58static int msmpeg4_decode_motion(MpegEncContext * s,
59 int *mx_ptr, int *my_ptr);
3825cd1d 60static void msmpeg4v2_encode_motion(MpegEncContext * s, int val);
20695ec9 61static void init_h263_dc_for_msmpeg4(void);
de0f2f4c 62static inline void msmpeg4_memsetw(short *tab, int val, int n);
62959862 63static int get_size_of_code(MpegEncContext * s, RLTable *rl, int last, int run, int level, int intra);
3825cd1d 64
de6d9b64 65
6fe84b43
MN
66extern UINT32 inverse[256];
67
62959862 68
de6d9b64
FB
69#ifdef DEBUG
70int intra_count = 0;
71int frame_count = 0;
72#endif
de6d9b64
FB
73
74#include "msmpeg4data.h"
75
62959862
MN
76static int rl_length[2][NB_RL_TABLES][MAX_LEVEL+1][MAX_RUN+1][2];
77
de6d9b64
FB
78#ifdef STATS
79
80const char *st_names[ST_NB] = {
81 "unknown",
82 "dc",
83 "intra_ac",
84 "inter_ac",
85 "intra_mb",
86 "inter_mb",
87 "mv",
88};
89
90int st_current_index = 0;
91unsigned int st_bit_counts[ST_NB];
92unsigned int st_out_bit_counts[ST_NB];
93
94#define set_stat(var) st_current_index = var;
95
96void print_stats(void)
97{
98 unsigned int total;
99 int i;
100
101 printf("Input:\n");
102 total = 0;
103 for(i=0;i<ST_NB;i++)
104 total += st_bit_counts[i];
105 if (total == 0)
106 total = 1;
107 for(i=0;i<ST_NB;i++) {
108 printf("%-10s : %10.1f %5.1f%%\n",
109 st_names[i],
110 (double)st_bit_counts[i] / 8.0,
111 (double)st_bit_counts[i] * 100.0 / total);
112 }
113 printf("%-10s : %10.1f %5.1f%%\n",
114 "total",
115 (double)total / 8.0,
116 100.0);
117
118 printf("Output:\n");
119 total = 0;
120 for(i=0;i<ST_NB;i++)
121 total += st_out_bit_counts[i];
122 if (total == 0)
123 total = 1;
124 for(i=0;i<ST_NB;i++) {
125 printf("%-10s : %10.1f %5.1f%%\n",
126 st_names[i],
127 (double)st_out_bit_counts[i] / 8.0,
128 (double)st_out_bit_counts[i] * 100.0 / total);
129 }
130 printf("%-10s : %10.1f %5.1f%%\n",
131 "total",
132 (double)total / 8.0,
133 100.0);
134}
135
136#else
137
138#define set_stat(var)
139
140#endif
141
f5957f3f
MN
142static void common_init(MpegEncContext * s)
143{
144 static int inited=0;
145
146 switch(s->msmpeg4_version){
147 case 1:
148 case 2:
149 s->y_dc_scale_table=
150 s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
151 break;
152 case 3:
153 if(s->workaround_bugs){
154 s->y_dc_scale_table= old_ff_y_dc_scale_table;
155 s->c_dc_scale_table= old_ff_c_dc_scale_table;
156 } else{
157 s->y_dc_scale_table= ff_mpeg4_y_dc_scale_table;
158 s->c_dc_scale_table= ff_mpeg4_c_dc_scale_table;
159 }
160 break;
161 case 4:
162 s->y_dc_scale_table= wmv1_y_dc_scale_table;
163 s->c_dc_scale_table= wmv1_c_dc_scale_table;
164 break;
165 }
166
2ad1516a 167
f5957f3f 168 if(s->msmpeg4_version==4){
2ad1516a
MN
169 ff_init_scantable(s, &s->intra_scantable , wmv1_scantable[1]);
170 ff_init_scantable(s, &s->intra_h_scantable, wmv1_scantable[2]);
171 ff_init_scantable(s, &s->intra_v_scantable, wmv1_scantable[3]);
172 ff_init_scantable(s, &s->inter_scantable , wmv1_scantable[0]);
f5957f3f 173 }
2ad1516a 174 //Note the default tables are set in common_init in mpegvideo.c
f5957f3f
MN
175
176 if(!inited){
f5957f3f
MN
177 inited=1;
178
179 init_h263_dc_for_msmpeg4();
f5957f3f
MN
180 }
181}
182
de6d9b64
FB
183/* build the table which associate a (x,y) motion vector to a vlc */
184static void init_mv_table(MVTable *tab)
185{
186 int i, x, y;
187
6000abfa 188 tab->table_mv_index = av_malloc(sizeof(UINT16) * 4096);
de6d9b64
FB
189 /* mark all entries as not used */
190 for(i=0;i<4096;i++)
191 tab->table_mv_index[i] = tab->n;
192
193 for(i=0;i<tab->n;i++) {
194 x = tab->table_mvx[i];
195 y = tab->table_mvy[i];
196 tab->table_mv_index[(x << 6) | y] = i;
197 }
198}
199
200static void code012(PutBitContext *pb, int n)
201{
202 if (n == 0) {
203 put_bits(pb, 1, 0);
204 } else {
205 put_bits(pb, 1, 1);
206 put_bits(pb, 1, (n >= 2));
207 }
208}
209
f5957f3f
MN
210void ff_msmpeg4_encode_init(MpegEncContext *s)
211{
212 static int init_done=0;
213 int i;
214
215 common_init(s);
216 if(s->msmpeg4_version>=4){
217 s->min_qcoeff= -255;
218 s->max_qcoeff= 255;
219 }
220
221 if (!init_done) {
222 /* init various encoding tables */
223 init_done = 1;
224 init_mv_table(&mv_tables[0]);
225 init_mv_table(&mv_tables[1]);
226 for(i=0;i<NB_RL_TABLES;i++)
227 init_rl(&rl_table[i]);
62959862
MN
228
229 for(i=0; i<NB_RL_TABLES; i++){
230 int level;
231 for(level=0; level<=MAX_LEVEL; level++){
232 int run;
233 for(run=0; run<=MAX_RUN; run++){
234 int last;
235 for(last=0; last<2; last++){
236 rl_length[0][i][level][run][last]= get_size_of_code(s, &rl_table[ i], last, run, level,0);
237 rl_length[1][i][level][run][last]= get_size_of_code(s, &rl_table[ i], last, run, level,1);
238 }
239 }
240 }
241 }
f5957f3f
MN
242 }
243}
244
245static int get_size_of_code(MpegEncContext * s, RLTable *rl, int last, int run, int level, int intra){
246 int size=0;
247 int code;
248 int run_diff= intra ? 0 : 1;
249
250 code = get_rl_index(rl, last, run, level);
251 size+= rl->table_vlc[code][1];
252 if (code == rl->n) {
253 int level1, run1;
254
255 level1 = level - rl->max_level[last][run];
256 if (level1 < 1)
257 goto esc2;
258 code = get_rl_index(rl, last, run, level1);
259 if (code == rl->n) {
260 esc2:
261 size++;
262 if (level > MAX_LEVEL)
263 goto esc3;
264 run1 = run - rl->max_run[last][level] - run_diff;
265 if (run1 < 0)
266 goto esc3;
267 code = get_rl_index(rl, last, run1, level);
268 if (code == rl->n) {
269 esc3:
270 /* third escape */
271 size+=1+1+6+8;
272 } else {
273 /* second escape */
274 size+= 1+1+ rl->table_vlc[code][1];
275 }
276 } else {
277 /* first escape */
278 size+= 1+1+ rl->table_vlc[code][1];
279 }
280 } else {
281 size++;
282 }
283 return size;
284}
285
286static void find_best_tables(MpegEncContext * s)
287{
288 int i;
289 int best =-1, best_size =9999999;
290 int chroma_best=-1, best_chroma_size=9999999;
62959862 291
f5957f3f
MN
292 for(i=0; i<3; i++){
293 int level;
294 int chroma_size=0;
295 int size=0;
296
297 if(i>0){// ;)
298 size++;
299 chroma_size++;
300 }
301 for(level=0; level<=MAX_LEVEL; level++){
302 int run;
303 for(run=0; run<=MAX_RUN; run++){
304 int last;
62959862 305 const int last_size= size + chroma_size;
f5957f3f
MN
306 for(last=0; last<2; last++){
307 int inter_count = s->ac_stats[0][0][level][run][last] + s->ac_stats[0][1][level][run][last];
308 int intra_luma_count = s->ac_stats[1][0][level][run][last];
309 int intra_chroma_count= s->ac_stats[1][1][level][run][last];
62959862 310
f5957f3f 311 if(s->pict_type==I_TYPE){
62959862
MN
312 size += intra_luma_count *rl_length[1][i ][level][run][last];
313 chroma_size+= intra_chroma_count*rl_length[1][i+3][level][run][last];
f5957f3f 314 }else{
62959862
MN
315 size+= intra_luma_count *rl_length[1][i ][level][run][last]
316 +intra_chroma_count*rl_length[1][i+3][level][run][last]
317 +inter_count *rl_length[0][i+3][level][run][last];
f5957f3f
MN
318 }
319 }
62959862 320 if(last_size == size+chroma_size) break;
f5957f3f
MN
321 }
322 }
323 if(size<best_size){
324 best_size= size;
325 best= i;
326 }
327 if(chroma_size<best_chroma_size){
328 best_chroma_size= chroma_size;
329 chroma_best= i;
330 }
331 }
62959862 332
f5957f3f
MN
333// printf("type:%d, best:%d, qp:%d, var:%d, mcvar:%d, size:%d //\n",
334// s->pict_type, best, s->qscale, s->mb_var_sum, s->mc_mb_var_sum, best_size);
335
336 if(s->pict_type==P_TYPE) chroma_best= best;
337
338 memset(s->ac_stats, 0, sizeof(int)*(MAX_LEVEL+1)*(MAX_RUN+1)*2*2*2);
339
340 s->rl_table_index = best;
341 s->rl_chroma_table_index= chroma_best;
342
343 if(s->pict_type != s->last_non_b_pict_type){
344 s->rl_table_index= 2;
345 if(s->pict_type==I_TYPE)
346 s->rl_chroma_table_index= 1;
347 else
348 s->rl_chroma_table_index= 2;
349 }
350
351}
352
287229e5 353/* write MSMPEG4 compatible frame header */
de6d9b64
FB
354void msmpeg4_encode_picture_header(MpegEncContext * s, int picture_number)
355{
f5957f3f 356 find_best_tables(s);
de6d9b64
FB
357
358 align_put_bits(&s->pb);
de6d9b64
FB
359 put_bits(&s->pb, 2, s->pict_type - 1);
360
361 put_bits(&s->pb, 5, s->qscale);
f5957f3f
MN
362 if(s->msmpeg4_version<=2){
363 s->rl_table_index = 2;
364 s->rl_chroma_table_index = 2;
365 }
3825cd1d 366
de6d9b64
FB
367 s->dc_table_index = 1;
368 s->mv_table_index = 1; /* only if P frame */
369 s->use_skip_mb_code = 1; /* only if P frame */
f5957f3f 370 s->per_mb_rl_table = 0;
05174fd4 371 s->inter_intra_pred= (s->width*s->height < 320*240 && s->bit_rate<=II_BITRATE && s->pict_type==P_TYPE);
f5957f3f 372
de6d9b64 373 if (s->pict_type == I_TYPE) {
f5957f3f 374 s->no_rounding = 1;
de0f2f4c
MN
375 s->slice_height= s->mb_height/1;
376 put_bits(&s->pb, 5, 0x16 + s->mb_height/s->slice_height);
f5957f3f
MN
377
378 if(s->msmpeg4_version==4){
379 msmpeg4_encode_ext_header(s);
05174fd4 380 if(s->bit_rate>MBAC_BITRATE)
de0f2f4c 381 put_bits(&s->pb, 1, s->per_mb_rl_table);
f5957f3f 382 }
de6d9b64 383
287229e5 384 if(s->msmpeg4_version>2){
f5957f3f
MN
385 if(!s->per_mb_rl_table){
386 code012(&s->pb, s->rl_chroma_table_index);
387 code012(&s->pb, s->rl_table_index);
388 }
de6d9b64 389
3825cd1d
MN
390 put_bits(&s->pb, 1, s->dc_table_index);
391 }
de6d9b64
FB
392 } else {
393 put_bits(&s->pb, 1, s->use_skip_mb_code);
394
05174fd4 395 if(s->msmpeg4_version==4 && s->bit_rate>MBAC_BITRATE)
f5957f3f
MN
396 put_bits(&s->pb, 1, s->per_mb_rl_table);
397
287229e5 398 if(s->msmpeg4_version>2){
f5957f3f
MN
399 if(!s->per_mb_rl_table)
400 code012(&s->pb, s->rl_table_index);
de6d9b64 401
3825cd1d
MN
402 put_bits(&s->pb, 1, s->dc_table_index);
403
404 put_bits(&s->pb, 1, s->mv_table_index);
405 }
de6d9b64 406
ae40484c
MN
407 if(s->flipflop_rounding){
408 s->no_rounding ^= 1;
409 }else{
410 s->no_rounding = 0;
411 }
de6d9b64
FB
412 }
413
f5957f3f
MN
414 s->esc3_level_length= 0;
415 s->esc3_run_length= 0;
de6d9b64
FB
416
417#ifdef DEBUG
418 intra_count = 0;
419 printf("*****frame %d:\n", frame_count++);
420#endif
421}
422
ae40484c
MN
423void msmpeg4_encode_ext_header(MpegEncContext * s)
424{
2b9ab1d5 425 put_bits(&s->pb, 5, s->frame_rate / FRAME_RATE_BASE); //yes 29.97 -> 29
ae40484c 426
05174fd4 427 put_bits(&s->pb, 11, MIN(s->bit_rate/1024, 2047));
ae40484c 428
287229e5
MN
429 if(s->msmpeg4_version<3)
430 s->flipflop_rounding=0;
431 else{
432 s->flipflop_rounding=1;
433 put_bits(&s->pb, 1, s->flipflop_rounding);
434 }
ae40484c
MN
435}
436
de6d9b64
FB
437/* predict coded block */
438static inline int coded_block_pred(MpegEncContext * s, int n, UINT8 **coded_block_ptr)
439{
dbbe8999 440 int xy, wrap, pred, a, b, c;
de6d9b64 441
dbbe8999
MN
442 xy = s->block_index[n];
443 wrap = s->block_wrap[0];
de6d9b64
FB
444
445 /* B C
446 * A X
447 */
dbbe8999
MN
448 a = s->coded_block[xy - 1 ];
449 b = s->coded_block[xy - 1 - wrap];
450 c = s->coded_block[xy - wrap];
de6d9b64
FB
451
452 if (b == c) {
453 pred = a;
454 } else {
455 pred = c;
456 }
457
458 /* store value */
dbbe8999 459 *coded_block_ptr = &s->coded_block[xy];
de6d9b64
FB
460
461 return pred;
462}
463
464static void msmpeg4_encode_motion(MpegEncContext * s,
465 int mx, int my)
466{
467 int code;
468 MVTable *mv;
469
470 /* modulo encoding */
471 /* WARNING : you cannot reach all the MVs even with the modulo
472 encoding. This is a somewhat strange compromise they took !!! */
473 if (mx <= -64)
474 mx += 64;
475 else if (mx >= 64)
476 mx -= 64;
477 if (my <= -64)
478 my += 64;
479 else if (my >= 64)
480 my -= 64;
481
482 mx += 32;
483 my += 32;
484#if 0
485 if ((unsigned)mx >= 64 ||
486 (unsigned)my >= 64)
487 fprintf(stderr, "error mx=%d my=%d\n", mx, my);
488#endif
489 mv = &mv_tables[s->mv_table_index];
490
491 code = mv->table_mv_index[(mx << 6) | my];
492 set_stat(ST_MV);
493 put_bits(&s->pb,
494 mv->table_mv_bits[code],
495 mv->table_mv_code[code]);
496 if (code == mv->n) {
497 /* escape : code litterally */
498 put_bits(&s->pb, 6, mx);
499 put_bits(&s->pb, 6, my);
500 }
501}
502
de0f2f4c
MN
503static inline void handle_slices(MpegEncContext *s){
504 if (s->mb_x == 0) {
505 if (s->slice_height && (s->mb_y % s->slice_height) == 0) {
506 if(s->msmpeg4_version != 4){
507 int wrap;
508 /* reset DC pred (set previous line to 1024) */
509 wrap = 2 * s->mb_width + 2;
510 msmpeg4_memsetw(&s->dc_val[0][(1) + (2 * s->mb_y) * wrap],
511 1024, 2 * s->mb_width);
512 wrap = s->mb_width + 2;
513 msmpeg4_memsetw(&s->dc_val[1][(1) + (s->mb_y) * wrap],
514 1024, s->mb_width);
515 msmpeg4_memsetw(&s->dc_val[2][(1) + (s->mb_y) * wrap],
516 1024, s->mb_width);
517
518 /* reset AC pred (set previous line to 0) */
519 wrap = s->mb_width * 2 + 2;
520 msmpeg4_memsetw(s->ac_val[0][0] + (1 + (2 * s->mb_y) * wrap)*16,
521 0, 2 * s->mb_width*16);
522 wrap = s->mb_width + 2;
523 msmpeg4_memsetw(s->ac_val[1][0] + (1 + (s->mb_y) * wrap)*16,
524 0, s->mb_width*16);
525 msmpeg4_memsetw(s->ac_val[2][0] + (1 + (s->mb_y) * wrap)*16,
526 0, s->mb_width*16);
527 }
528 s->first_slice_line = 1;
529 } else {
530 s->first_slice_line = 0;
531 }
532 }
533}
534
de6d9b64
FB
535void msmpeg4_encode_mb(MpegEncContext * s,
536 DCTELEM block[6][64],
537 int motion_x, int motion_y)
538{
539 int cbp, coded_cbp, i;
540 int pred_x, pred_y;
541 UINT8 *coded_block;
542
de0f2f4c
MN
543 handle_slices(s);
544
de6d9b64
FB
545 if (!s->mb_intra) {
546 /* compute cbp */
547 set_stat(ST_INTER_MB);
548 cbp = 0;
549 for (i = 0; i < 6; i++) {
550 if (s->block_last_index[i] >= 0)
551 cbp |= 1 << (5 - i);
552 }
553 if (s->use_skip_mb_code && (cbp | motion_x | motion_y) == 0) {
554 /* skip macroblock */
555 put_bits(&s->pb, 1, 1);
556 return;
557 }
558 if (s->use_skip_mb_code)
559 put_bits(&s->pb, 1, 0); /* mb coded */
560
287229e5 561 if(s->msmpeg4_version<=2){
3825cd1d
MN
562 put_bits(&s->pb,
563 v2_mb_type[cbp&3][1],
564 v2_mb_type[cbp&3][0]);
565 if((cbp&3) != 3) coded_cbp= cbp ^ 0x3C;
566 else coded_cbp= cbp;
567
568 put_bits(&s->pb,
569 cbpy_tab[coded_cbp>>2][1],
570 cbpy_tab[coded_cbp>>2][0]);
571
572 h263_pred_motion(s, 0, &pred_x, &pred_y);
573 msmpeg4v2_encode_motion(s, motion_x - pred_x);
574 msmpeg4v2_encode_motion(s, motion_y - pred_y);
575 }else{
576 put_bits(&s->pb,
577 table_mb_non_intra[cbp + 64][1],
578 table_mb_non_intra[cbp + 64][0]);
579
580 /* motion vector */
581 h263_pred_motion(s, 0, &pred_x, &pred_y);
582 msmpeg4_encode_motion(s, motion_x - pred_x,
583 motion_y - pred_y);
584 }
de6d9b64
FB
585 } else {
586 /* compute cbp */
587 cbp = 0;
588 coded_cbp = 0;
589 for (i = 0; i < 6; i++) {
590 int val, pred;
591 val = (s->block_last_index[i] >= 1);
592 cbp |= val << (5 - i);
593 if (i < 4) {
594 /* predict value for close blocks only for luma */
595 pred = coded_block_pred(s, i, &coded_block);
596 *coded_block = val;
597 val = val ^ pred;
598 }
599 coded_cbp |= val << (5 - i);
600 }
601#if 0
602 if (coded_cbp)
603 printf("cbp=%x %x\n", cbp, coded_cbp);
604#endif
605
287229e5 606 if(s->msmpeg4_version<=2){
3825cd1d
MN
607 if (s->pict_type == I_TYPE) {
608 put_bits(&s->pb,
609 v2_intra_cbpc[cbp&3][1], v2_intra_cbpc[cbp&3][0]);
610 } else {
611 if (s->use_skip_mb_code)
612 put_bits(&s->pb, 1, 0); /* mb coded */
613 put_bits(&s->pb,
614 v2_mb_type[(cbp&3) + 4][1],
615 v2_mb_type[(cbp&3) + 4][0]);
616 }
617 put_bits(&s->pb, 1, 0); /* no AC prediction yet */
de6d9b64 618 put_bits(&s->pb,
3825cd1d
MN
619 cbpy_tab[cbp>>2][1],
620 cbpy_tab[cbp>>2][0]);
621 }else{
622 if (s->pict_type == I_TYPE) {
623 set_stat(ST_INTRA_MB);
624 put_bits(&s->pb,
625 table_mb_intra[coded_cbp][1], table_mb_intra[coded_cbp][0]);
626 } else {
627 if (s->use_skip_mb_code)
628 put_bits(&s->pb, 1, 0); /* mb coded */
629 put_bits(&s->pb,
630 table_mb_non_intra[cbp][1],
631 table_mb_non_intra[cbp][0]);
632 }
633 set_stat(ST_INTRA_MB);
634 put_bits(&s->pb, 1, 0); /* no AC prediction yet */
05174fd4
MN
635 if(s->inter_intra_pred){
636 s->h263_aic_dir=0;
637 put_bits(&s->pb, table_inter_intra[s->h263_aic_dir][1], table_inter_intra[s->h263_aic_dir][0]);
638 }
de6d9b64 639 }
de6d9b64
FB
640 }
641
642 for (i = 0; i < 6; i++) {
643 msmpeg4_encode_block(s, block[i], i);
644 }
645}
646
92ba5ffb
MN
647/* old ffmpeg msmpeg4v3 mode */
648void ff_old_msmpeg4_dc_scale(MpegEncContext * s)
de6d9b64 649{
92ba5ffb 650 if (s->qscale < 5){
6fb904ce
MN
651 s->y_dc_scale = 8;
652 s->c_dc_scale = 8;
6fb904ce
MN
653 }else if (s->qscale < 9){
654 s->y_dc_scale = 2 * s->qscale;
655 s->c_dc_scale = (s->qscale + 13)>>1;
92ba5ffb 656 }else{
6fb904ce
MN
657 s->y_dc_scale = s->qscale + 8;
658 s->c_dc_scale = (s->qscale + 13)>>1;
659 }
de6d9b64
FB
660}
661
f5957f3f
MN
662static inline int msmpeg4v1_pred_dc(MpegEncContext * s, int n,
663 INT32 **dc_val_ptr)
287229e5
MN
664{
665 int i;
666
667 if (n < 4) {
668 i= 0;
669 } else {
670 i= n-3;
671 }
672
673 *dc_val_ptr= &s->last_dc[i];
674 return s->last_dc[i];
675}
676
de0f2f4c
MN
677static int get_dc(uint8_t *src, int stride, int scale)
678{
679 int y;
680 int sum=0;
681 for(y=0; y<8; y++){
682 int x;
683 for(x=0; x<8; x++){
684 sum+=src[x + y*stride];
685 }
686 }
687 return (sum + (scale>>1))/scale;
688}
689
de6d9b64 690/* dir = 0: left, dir = 1: top prediction */
f5957f3f
MN
691static inline int msmpeg4_pred_dc(MpegEncContext * s, int n,
692 UINT16 **dc_val_ptr, int *dir_ptr)
de6d9b64 693{
dbbe8999 694 int a, b, c, wrap, pred, scale;
98be975d 695 INT16 *dc_val;
de6d9b64
FB
696
697 /* find prediction */
698 if (n < 4) {
de6d9b64
FB
699 scale = s->y_dc_scale;
700 } else {
de6d9b64
FB
701 scale = s->c_dc_scale;
702 }
287229e5 703
dbbe8999
MN
704 wrap = s->block_wrap[n];
705 dc_val= s->dc_val[0] + s->block_index[n];
de6d9b64
FB
706
707 /* B C
708 * A X
709 */
dbbe8999
MN
710 a = dc_val[ - 1];
711 b = dc_val[ - 1 - wrap];
712 c = dc_val[ - wrap];
de6d9b64
FB
713
714 /* XXX: the following solution consumes divisions, but it does not
715 necessitate to modify mpegvideo.c. The problem comes from the
716 fact they decided to store the quantized DC (which would lead
717 to problems if Q could vary !) */
320680d4 718#if defined ARCH_X86 && !defined PIC
6f903d8e
MN
719 asm volatile(
720 "movl %3, %%eax \n\t"
721 "shrl $1, %%eax \n\t"
722 "addl %%eax, %2 \n\t"
723 "addl %%eax, %1 \n\t"
724 "addl %0, %%eax \n\t"
6fe84b43
MN
725 "mull %4 \n\t"
726 "movl %%edx, %0 \n\t"
6f903d8e 727 "movl %1, %%eax \n\t"
6fe84b43
MN
728 "mull %4 \n\t"
729 "movl %%edx, %1 \n\t"
6f903d8e 730 "movl %2, %%eax \n\t"
6fe84b43
MN
731 "mull %4 \n\t"
732 "movl %%edx, %2 \n\t"
fa778d53
MN
733 : "+b" (a), "+c" (b), "+D" (c)
734 : "g" (scale), "S" (inverse[scale])
6f903d8e
MN
735 : "%eax", "%edx"
736 );
320680d4
ZK
737#else
738 /* #elif defined (ARCH_ALPHA) */
1e98dffb 739 /* Divisions are extremely costly on Alpha; optimize the most
320680d4
ZK
740 common case. But they are costly everywhere...
741 */
1e98dffb
NK
742 if (scale == 8) {
743 a = (a + (8 >> 1)) / 8;
744 b = (b + (8 >> 1)) / 8;
745 c = (c + (8 >> 1)) / 8;
746 } else {
747 a = (a + (scale >> 1)) / scale;
748 b = (b + (scale >> 1)) / scale;
749 c = (c + (scale >> 1)) / scale;
750 }
6f903d8e 751#endif
de6d9b64
FB
752 /* XXX: WARNING: they did not choose the same test as MPEG4. This
753 is very important ! */
bd5e1c72 754 if(s->msmpeg4_version>3){
de0f2f4c
MN
755 if(s->inter_intra_pred){
756 uint8_t *dest;
757 int wrap;
758
759 if(n==1){
760 pred=a;
761 *dir_ptr = 0;
762 }else if(n==2){
763 pred=c;
764 *dir_ptr = 1;
765 }else if(n==3){
766 if (abs(a - b) < abs(b - c)) {
767 pred = c;
768 *dir_ptr = 1;
769 } else {
770 pred = a;
771 *dir_ptr = 0;
772 }
773 }else{
774 if(n<4){
775 wrap= s->linesize;
776 dest= s->current_picture[0] + (((n>>1) + 2*s->mb_y) * 8* wrap ) + ((n&1) + 2*s->mb_x) * 8;
777 }else{
0fd90455 778 wrap= s->uvlinesize;
de0f2f4c
MN
779 dest= s->current_picture[n-3] + (s->mb_y * 8 * wrap) + s->mb_x * 8;
780 }
781 if(s->mb_x==0) a= (1024 + (scale>>1))/scale;
782 else a= get_dc(dest-8, wrap, scale*8);
783 if(s->mb_y==0) c= (1024 + (scale>>1))/scale;
784 else c= get_dc(dest-8*wrap, wrap, scale*8);
785
786 if (s->h263_aic_dir==0) {
787 pred= a;
788 *dir_ptr = 0;
789 }else if (s->h263_aic_dir==1) {
790 if(n==0){
791 pred= c;
792 *dir_ptr = 1;
793 }else{
794 pred= a;
795 *dir_ptr = 0;
796 }
797 }else if (s->h263_aic_dir==2) {
798 if(n==0){
799 pred= a;
800 *dir_ptr = 0;
801 }else{
802 pred= c;
803 *dir_ptr = 1;
804 }
805 } else {
806 pred= c;
807 *dir_ptr = 1;
808 }
809 }
810 }else{
811 if (abs(a - b) < abs(b - c)) {
812 pred = c;
813 *dir_ptr = 1;
814 } else {
815 pred = a;
816 *dir_ptr = 0;
817 }
bd5e1c72
MN
818 }
819 }else{
820 if (abs(a - b) <= abs(b - c)) {
821 pred = c;
822 *dir_ptr = 1;
823 } else {
824 pred = a;
825 *dir_ptr = 0;
826 }
de6d9b64
FB
827 }
828
829 /* update predictor */
dbbe8999 830 *dc_val_ptr = &dc_val[0];
de6d9b64
FB
831 return pred;
832}
833
834#define DC_MAX 119
835
836static void msmpeg4_encode_dc(MpegEncContext * s, int level, int n, int *dir_ptr)
837{
838 int sign, code;
839 int pred;
de6d9b64 840
287229e5
MN
841 if(s->msmpeg4_version==1){
842 INT32 *dc_val;
843 pred = msmpeg4v1_pred_dc(s, n, &dc_val);
844
845 /* update predictor */
846 *dc_val= level;
847 }else{
f5957f3f 848 UINT16 *dc_val;
bd5e1c72 849 pred = msmpeg4_pred_dc(s, n, &dc_val, dir_ptr);
de6d9b64 850
287229e5
MN
851 /* update predictor */
852 if (n < 4) {
853 *dc_val = level * s->y_dc_scale;
854 } else {
855 *dc_val = level * s->c_dc_scale;
856 }
de6d9b64
FB
857 }
858
859 /* do the prediction */
860 level -= pred;
861
287229e5 862 if(s->msmpeg4_version<=2){
de6d9b64 863 if (n < 4) {
3825cd1d
MN
864 put_bits(&s->pb,
865 v2_dc_lum_table[level+256][1],
866 v2_dc_lum_table[level+256][0]);
867 }else{
868 put_bits(&s->pb,
869 v2_dc_chroma_table[level+256][1],
870 v2_dc_chroma_table[level+256][0]);
de6d9b64 871 }
3825cd1d
MN
872 }else{
873 sign = 0;
874 if (level < 0) {
875 level = -level;
876 sign = 1;
877 }
878 code = level;
879 if (code > DC_MAX)
880 code = DC_MAX;
881
882 if (s->dc_table_index == 0) {
883 if (n < 4) {
884 put_bits(&s->pb, table0_dc_lum[code][1], table0_dc_lum[code][0]);
885 } else {
886 put_bits(&s->pb, table0_dc_chroma[code][1], table0_dc_chroma[code][0]);
887 }
de6d9b64 888 } else {
3825cd1d
MN
889 if (n < 4) {
890 put_bits(&s->pb, table1_dc_lum[code][1], table1_dc_lum[code][0]);
891 } else {
892 put_bits(&s->pb, table1_dc_chroma[code][1], table1_dc_chroma[code][0]);
893 }
894 }
895
896 if (code == DC_MAX)
897 put_bits(&s->pb, 8, level);
898
899 if (level != 0) {
900 put_bits(&s->pb, 1, sign);
de6d9b64 901 }
de6d9b64
FB
902 }
903}
904
905/* Encoding of a block. Very similar to MPEG4 except for a different
906 escape coding (same as H263) and more vlc tables.
907 */
f5957f3f 908static inline void msmpeg4_encode_block(MpegEncContext * s, DCTELEM * block, int n)
de6d9b64
FB
909{
910 int level, run, last, i, j, last_index;
911 int last_non_zero, sign, slevel;
912 int code, run_diff, dc_pred_dir;
913 const RLTable *rl;
f5957f3f 914 const UINT8 *scantable;
de6d9b64
FB
915
916 if (s->mb_intra) {
917 set_stat(ST_DC);
918 msmpeg4_encode_dc(s, block[0], n, &dc_pred_dir);
919 i = 1;
920 if (n < 4) {
921 rl = &rl_table[s->rl_table_index];
922 } else {
923 rl = &rl_table[3 + s->rl_chroma_table_index];
924 }
925 run_diff = 0;
2ad1516a 926 scantable= s->intra_scantable.permutated;
de6d9b64
FB
927 set_stat(ST_INTRA_AC);
928 } else {
929 i = 0;
930 rl = &rl_table[3 + s->rl_table_index];
287229e5 931 if(s->msmpeg4_version<=2)
3825cd1d
MN
932 run_diff = 0;
933 else
934 run_diff = 1;
2ad1516a 935 scantable= s->inter_scantable.permutated;
de6d9b64
FB
936 set_stat(ST_INTER_AC);
937 }
938
f5957f3f 939 /* recalculate block_last_index for M$ wmv1 */
2ad1516a 940 if(s->msmpeg4_version==4 && s->block_last_index[n]>0){
f5957f3f
MN
941 for(last_index=63; last_index>=0; last_index--){
942 if(block[scantable[last_index]]) break;
943 }
944 }else
945 last_index = s->block_last_index[n];
de6d9b64 946 /* AC coefs */
de6d9b64
FB
947 last_non_zero = i - 1;
948 for (; i <= last_index; i++) {
f5957f3f 949 j = scantable[i];
de6d9b64
FB
950 level = block[j];
951 if (level) {
952 run = i - last_non_zero - 1;
953 last = (i == last_index);
954 sign = 0;
955 slevel = level;
956 if (level < 0) {
957 sign = 1;
958 level = -level;
959 }
62959862 960
f5957f3f
MN
961 if(level<=MAX_LEVEL && run<=MAX_RUN){
962 s->ac_stats[s->mb_intra][n>3][level][run][last]++;
963 }
964#if 0
965else
966 s->ac_stats[s->mb_intra][n>3][40][63][0]++; //esc3 like
967#endif
de6d9b64
FB
968 code = get_rl_index(rl, last, run, level);
969 put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
970 if (code == rl->n) {
971 int level1, run1;
972
973 level1 = level - rl->max_level[last][run];
974 if (level1 < 1)
975 goto esc2;
976 code = get_rl_index(rl, last, run, level1);
977 if (code == rl->n) {
978 esc2:
979 put_bits(&s->pb, 1, 0);
980 if (level > MAX_LEVEL)
981 goto esc3;
982 run1 = run - rl->max_run[last][level] - run_diff;
983 if (run1 < 0)
984 goto esc3;
985 code = get_rl_index(rl, last, run1, level);
986 if (code == rl->n) {
987 esc3:
988 /* third escape */
989 put_bits(&s->pb, 1, 0);
990 put_bits(&s->pb, 1, last);
f5957f3f
MN
991 if(s->msmpeg4_version==4){
992 if(s->esc3_level_length==0){
993 s->esc3_level_length=8;
994 s->esc3_run_length= 6;
995 if(s->qscale<8)
996 put_bits(&s->pb, 6, 3);
997 else
998 put_bits(&s->pb, 8, 3);
999 }
1000 put_bits(&s->pb, s->esc3_run_length, run);
1001 put_bits(&s->pb, 1, sign);
1002 put_bits(&s->pb, s->esc3_level_length, level);
1003 }else{
1004 put_bits(&s->pb, 6, run);
1005 put_bits(&s->pb, 8, slevel & 0xff);
1006 }
de6d9b64
FB
1007 } else {
1008 /* second escape */
1009 put_bits(&s->pb, 1, 1);
1010 put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
1011 put_bits(&s->pb, 1, sign);
1012 }
1013 } else {
1014 /* first escape */
1015 put_bits(&s->pb, 1, 1);
1016 put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
1017 put_bits(&s->pb, 1, sign);
1018 }
1019 } else {
1020 put_bits(&s->pb, 1, sign);
1021 }
1022 last_non_zero = i;
1023 }
1024 }
1025}
1026
1027/****************************************/
1028/* decoding stuff */
1029
1030static VLC mb_non_intra_vlc;
1031static VLC mb_intra_vlc;
1032static VLC dc_lum_vlc[2];
1033static VLC dc_chroma_vlc[2];
84afee34
MN
1034static VLC v2_dc_lum_vlc;
1035static VLC v2_dc_chroma_vlc;
1036static VLC cbpy_vlc;
1037static VLC v2_intra_cbpc_vlc;
1038static VLC v2_mb_type_vlc;
1039static VLC v2_mv_vlc;
287229e5
MN
1040static VLC v1_intra_cbpc_vlc;
1041static VLC v1_inter_cbpc_vlc;
de0f2f4c 1042static VLC inter_intra_vlc;
84afee34
MN
1043
1044/* this table is practically identical to the one from h263 except that its inverted */
20695ec9 1045static void init_h263_dc_for_msmpeg4(void)
84afee34 1046{
84afee34 1047 int level, uni_code, uni_len;
84afee34 1048
2ed627e6 1049 for(level=-256; level<256; level++){
84afee34
MN
1050 int size, v, l;
1051 /* find number of bits */
1052 size = 0;
1053 v = abs(level);
1054 while (v) {
1055 v >>= 1;
1056 size++;
1057 }
1058
1059 if (level < 0)
1060 l= (-level) ^ ((1 << size) - 1);
1061 else
1062 l= level;
1063
1064 /* luminance h263 */
1065 uni_code= DCtab_lum[size][0];
1066 uni_len = DCtab_lum[size][1];
1067 uni_code ^= (1<<uni_len)-1; //M$ doesnt like compatibility
1068
1069 if (size > 0) {
1070 uni_code<<=size; uni_code|=l;
1071 uni_len+=size;
1072 if (size > 8){
1073 uni_code<<=1; uni_code|=1;
1074 uni_len++;
1075 }
1076 }
1077 v2_dc_lum_table[level+256][0]= uni_code;
1078 v2_dc_lum_table[level+256][1]= uni_len;
1079
1080 /* chrominance h263 */
1081 uni_code= DCtab_chrom[size][0];
1082 uni_len = DCtab_chrom[size][1];
1083 uni_code ^= (1<<uni_len)-1; //M$ doesnt like compatibility
1084
1085 if (size > 0) {
1086 uni_code<<=size; uni_code|=l;
1087 uni_len+=size;
1088 if (size > 8){
1089 uni_code<<=1; uni_code|=1;
1090 uni_len++;
1091 }
1092 }
1093 v2_dc_chroma_table[level+256][0]= uni_code;
1094 v2_dc_chroma_table[level+256][1]= uni_len;
1095
1096 }
84afee34 1097}
de6d9b64
FB
1098
1099/* init all vlc decoding tables */
f5957f3f 1100int ff_msmpeg4_decode_init(MpegEncContext *s)
de6d9b64 1101{
d81c5983 1102 static int done = 0;
de6d9b64
FB
1103 int i;
1104 MVTable *mv;
1105
f5957f3f 1106 common_init(s);
de6d9b64 1107
d81c5983
FB
1108 if (!done) {
1109 done = 1;
1110
1111 for(i=0;i<NB_RL_TABLES;i++) {
1112 init_rl(&rl_table[i]);
1113 init_vlc_rl(&rl_table[i]);
1114 }
1115 for(i=0;i<2;i++) {
1116 mv = &mv_tables[i];
08dce7b0 1117 init_vlc(&mv->vlc, MV_VLC_BITS, mv->n + 1,
d81c5983
FB
1118 mv->table_mv_bits, 1, 1,
1119 mv->table_mv_code, 2, 2);
1120 }
1121
08dce7b0 1122 init_vlc(&dc_lum_vlc[0], DC_VLC_BITS, 120,
d81c5983
FB
1123 &table0_dc_lum[0][1], 8, 4,
1124 &table0_dc_lum[0][0], 8, 4);
08dce7b0 1125 init_vlc(&dc_chroma_vlc[0], DC_VLC_BITS, 120,
d81c5983
FB
1126 &table0_dc_chroma[0][1], 8, 4,
1127 &table0_dc_chroma[0][0], 8, 4);
08dce7b0 1128 init_vlc(&dc_lum_vlc[1], DC_VLC_BITS, 120,
d81c5983
FB
1129 &table1_dc_lum[0][1], 8, 4,
1130 &table1_dc_lum[0][0], 8, 4);
08dce7b0 1131 init_vlc(&dc_chroma_vlc[1], DC_VLC_BITS, 120,
d81c5983
FB
1132 &table1_dc_chroma[0][1], 8, 4,
1133 &table1_dc_chroma[0][0], 8, 4);
84afee34 1134
08dce7b0 1135 init_vlc(&v2_dc_lum_vlc, DC_VLC_BITS, 512,
d81c5983
FB
1136 &v2_dc_lum_table[0][1], 8, 4,
1137 &v2_dc_lum_table[0][0], 8, 4);
08dce7b0 1138 init_vlc(&v2_dc_chroma_vlc, DC_VLC_BITS, 512,
d81c5983
FB
1139 &v2_dc_chroma_table[0][1], 8, 4,
1140 &v2_dc_chroma_table[0][0], 8, 4);
84afee34 1141
08dce7b0 1142 init_vlc(&cbpy_vlc, CBPY_VLC_BITS, 16,
d81c5983
FB
1143 &cbpy_tab[0][1], 2, 1,
1144 &cbpy_tab[0][0], 2, 1);
08dce7b0 1145 init_vlc(&v2_intra_cbpc_vlc, V2_INTRA_CBPC_VLC_BITS, 4,
d81c5983
FB
1146 &v2_intra_cbpc[0][1], 2, 1,
1147 &v2_intra_cbpc[0][0], 2, 1);
08dce7b0 1148 init_vlc(&v2_mb_type_vlc, V2_MB_TYPE_VLC_BITS, 8,
d81c5983
FB
1149 &v2_mb_type[0][1], 2, 1,
1150 &v2_mb_type[0][0], 2, 1);
08dce7b0 1151 init_vlc(&v2_mv_vlc, V2_MV_VLC_BITS, 33,
d81c5983
FB
1152 &mvtab[0][1], 2, 1,
1153 &mvtab[0][0], 2, 1);
1154
08dce7b0 1155 init_vlc(&mb_non_intra_vlc, MB_NON_INTRA_VLC_BITS, 128,
d81c5983
FB
1156 &table_mb_non_intra[0][1], 8, 4,
1157 &table_mb_non_intra[0][0], 8, 4);
08dce7b0 1158 init_vlc(&mb_intra_vlc, MB_INTRA_VLC_BITS, 64,
d81c5983
FB
1159 &table_mb_intra[0][1], 4, 2,
1160 &table_mb_intra[0][0], 4, 2);
287229e5 1161
08dce7b0 1162 init_vlc(&v1_intra_cbpc_vlc, V1_INTRA_CBPC_VLC_BITS, 8,
d81c5983
FB
1163 intra_MCBPC_bits, 1, 1,
1164 intra_MCBPC_code, 1, 1);
08dce7b0 1165 init_vlc(&v1_inter_cbpc_vlc, V1_INTER_CBPC_VLC_BITS, 25,
d81c5983
FB
1166 inter_MCBPC_bits, 1, 1,
1167 inter_MCBPC_code, 1, 1);
de0f2f4c 1168
08dce7b0 1169 init_vlc(&inter_intra_vlc, INTER_INTRA_VLC_BITS, 4,
de0f2f4c
MN
1170 &table_inter_intra[0][1], 2, 1,
1171 &table_inter_intra[0][0], 2, 1);
d81c5983 1172 }
de6d9b64
FB
1173 return 0;
1174}
1175
1176static int decode012(GetBitContext *gb)
1177{
1178 int n;
612476ef 1179 n = get_bits1(gb);
de6d9b64
FB
1180 if (n == 0)
1181 return 0;
1182 else
612476ef 1183 return get_bits1(gb) + 1;
de6d9b64
FB
1184}
1185
84afee34 1186int msmpeg4_decode_picture_header(MpegEncContext * s)
de6d9b64 1187{
f5957f3f 1188 int code;
de6d9b64 1189
e1a9dbff
MN
1190#if 0
1191{
1192int i;
1193for(i=0; i<s->gb.size*8; i++)
1194 printf("%d", get_bits1(&s->gb));
1195// get_bits1(&s->gb);
1196printf("END\n");
1197return -1;
1198}
1199#endif
287229e5
MN
1200
1201 if(s->msmpeg4_version==1){
1202 int start_code, num;
1203 start_code = (get_bits(&s->gb, 16)<<16) | get_bits(&s->gb, 16);
1204 if(start_code!=0x00000100){
1205 fprintf(stderr, "invalid startcode\n");
1206 return -1;
1207 }
1208
1209 num= get_bits(&s->gb, 5); // frame number */
1210 }
1211
de6d9b64
FB
1212 s->pict_type = get_bits(&s->gb, 2) + 1;
1213 if (s->pict_type != I_TYPE &&
287229e5
MN
1214 s->pict_type != P_TYPE){
1215 fprintf(stderr, "invalid picture type\n");
de6d9b64 1216 return -1;
287229e5 1217 }
de0f2f4c
MN
1218#if 0
1219{
1220 static int had_i=0;
1221 if(s->pict_type == I_TYPE) had_i=1;
1222 if(!had_i) return -1;
1223}
1224#endif
de6d9b64
FB
1225 s->qscale = get_bits(&s->gb, 5);
1226
1227 if (s->pict_type == I_TYPE) {
1228 code = get_bits(&s->gb, 5);
287229e5
MN
1229 if(s->msmpeg4_version==1){
1230 if(code==0 || code>s->mb_height){
1231 fprintf(stderr, "invalid slice height %d\n", code);
1232 return -1;
1233 }
1234
1235 s->slice_height = code;
1236 }else{
1237 /* 0x17: one slice, 0x18: two slices, ... */
de0f2f4c
MN
1238 if (code < 0x17){
1239 fprintf(stderr, "error, slice code was %X\n", code);
287229e5 1240 return -1;
de0f2f4c 1241 }
287229e5
MN
1242
1243 s->slice_height = s->mb_height / (code - 0x16);
1244 }
e1a9dbff
MN
1245
1246 switch(s->msmpeg4_version){
287229e5 1247 case 1:
e1a9dbff 1248 case 2:
84afee34
MN
1249 s->rl_chroma_table_index = 2;
1250 s->rl_table_index = 2;
7f89b6fb 1251
84afee34 1252 s->dc_table_index = 0; //not used
e1a9dbff
MN
1253 break;
1254 case 3:
84afee34
MN
1255 s->rl_chroma_table_index = decode012(&s->gb);
1256 s->rl_table_index = decode012(&s->gb);
7f89b6fb 1257
84afee34 1258 s->dc_table_index = get_bits1(&s->gb);
e1a9dbff
MN
1259 break;
1260 case 4:
f5957f3f
MN
1261 msmpeg4_decode_ext_header(s, (2+5+5+17+7)/8);
1262
05174fd4
MN
1263 if(s->bit_rate > MBAC_BITRATE) s->per_mb_rl_table= get_bits1(&s->gb);
1264 else s->per_mb_rl_table= 0;
de0f2f4c 1265
f5957f3f
MN
1266 if(!s->per_mb_rl_table){
1267 s->rl_chroma_table_index = decode012(&s->gb);
1268 s->rl_table_index = decode012(&s->gb);
e1a9dbff 1269 }
e1a9dbff 1270
f5957f3f 1271 s->dc_table_index = get_bits1(&s->gb);
de0f2f4c 1272 s->inter_intra_pred= 0;
e1a9dbff 1273 break;
84afee34 1274 }
de6d9b64 1275 s->no_rounding = 1;
de0f2f4c 1276/* printf("qscale:%d rlc:%d rl:%d dc:%d mbrl:%d slice:%d \n",
badaf88e
MN
1277 s->qscale,
1278 s->rl_chroma_table_index,
1279 s->rl_table_index,
f5957f3f 1280 s->dc_table_index,
de0f2f4c
MN
1281 s->per_mb_rl_table,
1282 s->slice_height);*/
de6d9b64 1283 } else {
287229e5
MN
1284 switch(s->msmpeg4_version){
1285 case 1:
1286 case 2:
1287 if(s->msmpeg4_version==1)
1288 s->use_skip_mb_code = 1;
1289 else
1290 s->use_skip_mb_code = get_bits1(&s->gb);
84afee34
MN
1291 s->rl_table_index = 2;
1292 s->rl_chroma_table_index = s->rl_table_index;
84afee34 1293 s->dc_table_index = 0; //not used
84afee34 1294 s->mv_table_index = 0;
287229e5
MN
1295 break;
1296 case 3:
1297 s->use_skip_mb_code = get_bits1(&s->gb);
84afee34
MN
1298 s->rl_table_index = decode012(&s->gb);
1299 s->rl_chroma_table_index = s->rl_table_index;
de6d9b64 1300
84afee34 1301 s->dc_table_index = get_bits1(&s->gb);
de6d9b64 1302
84afee34 1303 s->mv_table_index = get_bits1(&s->gb);
287229e5 1304 break;
f5957f3f
MN
1305 case 4:
1306 s->use_skip_mb_code = get_bits1(&s->gb);
de0f2f4c 1307
05174fd4
MN
1308 if(s->bit_rate > MBAC_BITRATE) s->per_mb_rl_table= get_bits1(&s->gb);
1309 else s->per_mb_rl_table= 0;
de0f2f4c 1310
f5957f3f
MN
1311 if(!s->per_mb_rl_table){
1312 s->rl_table_index = decode012(&s->gb);
1313 s->rl_chroma_table_index = s->rl_table_index;
1314 }
1315
1316 s->dc_table_index = get_bits1(&s->gb);
1317
1318 s->mv_table_index = get_bits1(&s->gb);
05174fd4 1319 s->inter_intra_pred= (s->width*s->height < 320*240 && s->bit_rate<=II_BITRATE);
f5957f3f 1320 break;
84afee34 1321 }
de0f2f4c 1322/* printf("skip:%d rl:%d rlc:%d dc:%d mv:%d mbrl:%d qp:%d \n",
badaf88e
MN
1323 s->use_skip_mb_code,
1324 s->rl_table_index,
1325 s->rl_chroma_table_index,
1326 s->dc_table_index,
f5957f3f 1327 s->mv_table_index,
de0f2f4c
MN
1328 s->per_mb_rl_table,
1329 s->qscale);*/
ae40484c
MN
1330 if(s->flipflop_rounding){
1331 s->no_rounding ^= 1;
1332 }else{
1333 s->no_rounding = 0;
1334 }
de6d9b64 1335 }
f5957f3f
MN
1336
1337 s->esc3_level_length= 0;
1338 s->esc3_run_length= 0;
84afee34 1339
de6d9b64
FB
1340#ifdef DEBUG
1341 printf("*****frame %d:\n", frame_count++);
1342#endif
1343 return 0;
1344}
1345
ae40484c
MN
1346int msmpeg4_decode_ext_header(MpegEncContext * s, int buf_size)
1347{
287229e5
MN
1348 int left= buf_size*8 - get_bits_count(&s->gb);
1349 int length= s->msmpeg4_version>=3 ? 17 : 16;
ae40484c 1350 /* the alt_bitstream reader could read over the end so we need to check it */
287229e5 1351 if(left>=length && left<length+8)
ae40484c 1352 {
2b9ab1d5
MN
1353 int fps;
1354
1355 fps= get_bits(&s->gb, 5);
05174fd4 1356 s->bit_rate= get_bits(&s->gb, 11)*1024;
287229e5
MN
1357 if(s->msmpeg4_version>=3)
1358 s->flipflop_rounding= get_bits1(&s->gb);
1359 else
1360 s->flipflop_rounding= 0;
2b9ab1d5 1361
05174fd4 1362// printf("fps:%2d bps:%2d roundingType:%1d\n", fps, s->bit_rate/1024, s->flipflop_rounding);
ae40484c 1363 }
287229e5 1364 else if(left<length+8)
ae40484c 1365 {
2b9ab1d5 1366 s->flipflop_rounding= 0;
287229e5
MN
1367 printf("ext header missing, %d left\n", left);
1368 }
1369 else
1370 {
1371 fprintf(stderr, "I frame too long, ignoring ext header\n");
ae40484c 1372 }
2b9ab1d5 1373
ae40484c
MN
1374 return 0;
1375}
1376
cd4af68a 1377static inline void msmpeg4_memsetw(short *tab, int val, int n)
de6d9b64
FB
1378{
1379 int i;
1380 for(i=0;i<n;i++)
1381 tab[i] = val;
1382}
1383
3825cd1d
MN
1384static void msmpeg4v2_encode_motion(MpegEncContext * s, int val)
1385{
1386 int range, bit_size, sign, code, bits;
1387
1388 if (val == 0) {
1389 /* zero vector */
1390 code = 0;
1391 put_bits(&s->pb, mvtab[code][1], mvtab[code][0]);
1392 } else {
1393 bit_size = s->f_code - 1;
1394 range = 1 << bit_size;
1395 if (val <= -64)
1396 val += 64;
1397 else if (val >= 64)
1398 val -= 64;
1399
1400 if (val >= 0) {
1401 sign = 0;
1402 } else {
1403 val = -val;
1404 sign = 1;
1405 }
1406 val--;
1407 code = (val >> bit_size) + 1;
1408 bits = val & (range - 1);
1409
1410 put_bits(&s->pb, mvtab[code][1] + 1, (mvtab[code][0] << 1) | sign);
1411 if (bit_size > 0) {
1412 put_bits(&s->pb, bit_size, bits);
1413 }
1414 }
1415}
1416
84afee34
MN
1417/* this is identical to h263 except that its range is multiplied by 2 */
1418static int msmpeg4v2_decode_motion(MpegEncContext * s, int pred, int f_code)
1419{
1420 int code, val, sign, shift;
1421
08dce7b0 1422 code = get_vlc2(&s->gb, v2_mv_vlc.table, V2_MV_VLC_BITS, 2);
287229e5 1423// printf("MV code %d at %d %d pred: %d\n", code, s->mb_x,s->mb_y, pred);
84afee34
MN
1424 if (code < 0)
1425 return 0xffff;
1426
1427 if (code == 0)
1428 return pred;
1429 sign = get_bits1(&s->gb);
1430 shift = f_code - 1;
1431 val = (code - 1) << shift;
1432 if (shift > 0)
1433 val |= get_bits(&s->gb, shift);
1434 val++;
1435 if (sign)
1436 val = -val;
84afee34 1437
287229e5 1438 val += pred;
84afee34
MN
1439 if (val <= -64)
1440 val += 64;
1441 else if (val >= 64)
1442 val -= 64;
1443
1444 return val;
1445}
1446
1447
287229e5 1448static int msmpeg4v12_decode_mb(MpegEncContext *s,
84afee34
MN
1449 DCTELEM block[6][64])
1450{
1451 int cbp, code, i;
84afee34
MN
1452 if (s->pict_type == P_TYPE) {
1453 if (s->use_skip_mb_code) {
1454 if (get_bits1(&s->gb)) {
1455 /* skip mb */
1456 s->mb_intra = 0;
1457 for(i=0;i<6;i++)
1458 s->block_last_index[i] = -1;
1459 s->mv_dir = MV_DIR_FORWARD;
1460 s->mv_type = MV_TYPE_16X16;
1461 s->mv[0][0][0] = 0;
1462 s->mv[0][0][1] = 0;
1463 s->mb_skiped = 1;
1464 return 0;
1465 }
1466 }
1467
287229e5 1468 if(s->msmpeg4_version==2)
08dce7b0 1469 code = get_vlc2(&s->gb, v2_mb_type_vlc.table, V2_MB_TYPE_VLC_BITS, 1);
287229e5 1470 else
08dce7b0 1471 code = get_vlc2(&s->gb, v1_inter_cbpc_vlc.table, V1_INTER_CBPC_VLC_BITS, 3);
287229e5
MN
1472 if(code<0 || code>7){
1473 fprintf(stderr, "cbpc %d invalid at %d %d\n", code, s->mb_x, s->mb_y);
1474 return -1;
1475 }
1476
84afee34
MN
1477 s->mb_intra = code >>2;
1478
1479 cbp = code & 0x3;
1480 } else {
1481 s->mb_intra = 1;
287229e5 1482 if(s->msmpeg4_version==2)
08dce7b0 1483 cbp= get_vlc2(&s->gb, v2_intra_cbpc_vlc.table, V2_INTRA_CBPC_VLC_BITS, 1);
287229e5 1484 else
08dce7b0 1485 cbp= get_vlc2(&s->gb, v1_intra_cbpc_vlc.table, V1_INTRA_CBPC_VLC_BITS, 1);
287229e5
MN
1486 if(cbp<0 || cbp>3){
1487 fprintf(stderr, "cbpc %d invalid at %d %d\n", cbp, s->mb_x, s->mb_y);
1488 return -1;
1489 }
84afee34
MN
1490 }
1491
1492 if (!s->mb_intra) {
287229e5
MN
1493 int mx, my, cbpy;
1494
08dce7b0 1495 cbpy= get_vlc2(&s->gb, cbpy_vlc.table, CBPY_VLC_BITS, 1);
287229e5
MN
1496 if(cbpy<0){
1497 fprintf(stderr, "cbpy %d invalid at %d %d\n", cbp, s->mb_x, s->mb_y);
1498 return -1;
1499 }
84afee34 1500
287229e5
MN
1501 cbp|= cbpy<<2;
1502 if(s->msmpeg4_version==1 || (cbp&3) != 3) cbp^= 0x3C;
84afee34
MN
1503
1504 h263_pred_motion(s, 0, &mx, &my);
1505 mx= msmpeg4v2_decode_motion(s, mx, 1);
1506 my= msmpeg4v2_decode_motion(s, my, 1);
1507
1508 s->mv_dir = MV_DIR_FORWARD;
1509 s->mv_type = MV_TYPE_16X16;
1510 s->mv[0][0][0] = mx;
1511 s->mv[0][0][1] = my;
1512 } else {
287229e5
MN
1513 if(s->msmpeg4_version==2){
1514 s->ac_pred = get_bits1(&s->gb);
08dce7b0 1515 cbp|= get_vlc2(&s->gb, cbpy_vlc.table, CBPY_VLC_BITS, 1)<<2; //FIXME check errors
287229e5
MN
1516 } else{
1517 s->ac_pred = 0;
08dce7b0 1518 cbp|= get_vlc2(&s->gb, cbpy_vlc.table, CBPY_VLC_BITS, 1)<<2; //FIXME check errors
287229e5
MN
1519 if(s->pict_type==P_TYPE) cbp^=0x3C;
1520 }
84afee34
MN
1521 }
1522
1523 for (i = 0; i < 6; i++) {
1524 if (msmpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1) < 0)
1525 {
287229e5 1526 fprintf(stderr,"\nerror while decoding block: %d x %d (%d)\n", s->mb_x, s->mb_y, i);
84afee34
MN
1527 return -1;
1528 }
1529 }
1530 return 0;
1531}
1532
de6d9b64
FB
1533int msmpeg4_decode_mb(MpegEncContext *s,
1534 DCTELEM block[6][64])
1535{
1536 int cbp, code, i;
de6d9b64
FB
1537 UINT8 *coded_val;
1538
bd5e1c72
MN
1539#ifdef PRINT_MB
1540if(s->mb_x==0){
1541 printf("\n");
1542 if(s->mb_y==0) printf("\n");
1543}
1544#endif
de6d9b64 1545 /* special slice handling */
de0f2f4c 1546 handle_slices(s);
de6d9b64 1547
287229e5 1548 if(s->msmpeg4_version<=2) return msmpeg4v12_decode_mb(s, block); //FIXME export function & call from outside perhaps
84afee34 1549
de6d9b64
FB
1550 if (s->pict_type == P_TYPE) {
1551 set_stat(ST_INTER_MB);
1552 if (s->use_skip_mb_code) {
612476ef 1553 if (get_bits1(&s->gb)) {
de6d9b64
FB
1554 /* skip mb */
1555 s->mb_intra = 0;
1556 for(i=0;i<6;i++)
1557 s->block_last_index[i] = -1;
1558 s->mv_dir = MV_DIR_FORWARD;
1559 s->mv_type = MV_TYPE_16X16;
1560 s->mv[0][0][0] = 0;
1561 s->mv[0][0][1] = 0;
3bb4e23a 1562 s->mb_skiped = 1;
bd5e1c72
MN
1563#ifdef PRINT_MB
1564printf("S ");
1565#endif
de6d9b64
FB
1566 return 0;
1567 }
1568 }
1569
08dce7b0 1570 code = get_vlc2(&s->gb, mb_non_intra_vlc.table, MB_NON_INTRA_VLC_BITS, 3);
de6d9b64
FB
1571 if (code < 0)
1572 return -1;
38d171eb
ZK
1573 //s->mb_intra = (code & 0x40) ? 0 : 1;
1574 s->mb_intra = (~code & 0x40) >> 6;
de6d9b64
FB
1575
1576 cbp = code & 0x3f;
1577 } else {
1578 set_stat(ST_INTRA_MB);
1579 s->mb_intra = 1;
08dce7b0 1580 code = get_vlc2(&s->gb, mb_intra_vlc.table, MB_INTRA_VLC_BITS, 2);
de6d9b64
FB
1581 if (code < 0)
1582 return -1;
1583 /* predict coded block pattern */
1584 cbp = 0;
1585 for(i=0;i<6;i++) {
38d171eb 1586 int val = ((code >> (5 - i)) & 1);
de6d9b64 1587 if (i < 4) {
38d171eb 1588 int pred = coded_block_pred(s, i, &coded_val);
de6d9b64
FB
1589 val = val ^ pred;
1590 *coded_val = val;
1591 }
1592 cbp |= val << (5 - i);
1593 }
1594 }
1595
1596 if (!s->mb_intra) {
1597 int mx, my;
f5957f3f
MN
1598//printf("P at %d %d\n", s->mb_x, s->mb_y);
1599 if(s->per_mb_rl_table && cbp){
1600 s->rl_table_index = decode012(&s->gb);
1601 s->rl_chroma_table_index = s->rl_table_index;
1602 }
de6d9b64
FB
1603 set_stat(ST_MV);
1604 h263_pred_motion(s, 0, &mx, &my);
1605 if (msmpeg4_decode_motion(s, &mx, &my) < 0)
1606 return -1;
1607 s->mv_dir = MV_DIR_FORWARD;
1608 s->mv_type = MV_TYPE_16X16;
1609 s->mv[0][0][0] = mx;
1610 s->mv[0][0][1] = my;
bd5e1c72
MN
1611#ifdef PRINT_MB
1612printf("P ");
1613#endif
de6d9b64 1614 } else {
f5957f3f 1615//printf("I at %d %d %d %06X\n", s->mb_x, s->mb_y, ((cbp&3)? 1 : 0) +((cbp&0x3C)? 2 : 0), show_bits(&s->gb, 24));
de6d9b64 1616 set_stat(ST_INTRA_MB);
612476ef 1617 s->ac_pred = get_bits1(&s->gb);
bd5e1c72
MN
1618#ifdef PRINT_MB
1619printf("%c", s->ac_pred ? 'A' : 'I');
1620#endif
de0f2f4c 1621 if(s->inter_intra_pred){
08dce7b0 1622 s->h263_aic_dir= get_vlc2(&s->gb, inter_intra_vlc.table, INTER_INTRA_VLC_BITS, 1);
de0f2f4c
MN
1623// printf("%d%d %d %d/", s->ac_pred, s->h263_aic_dir, s->mb_x, s->mb_y);
1624 }
f5957f3f
MN
1625 if(s->per_mb_rl_table && cbp){
1626 s->rl_table_index = decode012(&s->gb);
1627 s->rl_chroma_table_index = s->rl_table_index;
1628 }
de6d9b64
FB
1629 }
1630
1631 for (i = 0; i < 6; i++) {
1632 if (msmpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1) < 0)
38d171eb 1633 {
287229e5
MN
1634 fprintf(stderr,"\nerror while decoding block: %d x %d (%d)\n", s->mb_x, s->mb_y, i);
1635 return -1;
38d171eb 1636 }
de6d9b64 1637 }
bd5e1c72 1638
de6d9b64
FB
1639 return 0;
1640}
1a013246 1641//#define ERROR_DETAILS
f5957f3f 1642static inline int msmpeg4_decode_block(MpegEncContext * s, DCTELEM * block,
de6d9b64
FB
1643 int n, int coded)
1644{
45a82edb 1645 int level, i, last, run, run_diff;
de6d9b64
FB
1646 int dc_pred_dir;
1647 RLTable *rl;
45a82edb 1648 RL_VLC_ELEM *rl_vlc;
de6d9b64 1649 const UINT8 *scan_table;
badaf88e 1650 int qmul, qadd;
de6d9b64
FB
1651
1652 if (s->mb_intra) {
badaf88e
MN
1653 qmul=1;
1654 qadd=0;
1655
de6d9b64
FB
1656 /* DC coef */
1657 set_stat(ST_DC);
1658 level = msmpeg4_decode_dc(s, n, &dc_pred_dir);
bd5e1c72
MN
1659#ifdef PRINT_MB
1660{
1661 static int c;
1662 if(n==0) c=0;
1663 if(n==4) printf("%X", c);
1664 c+= c +dc_pred_dir;
1665}
1666#endif
287229e5 1667 if (level < 0){
bd5e1c72 1668 fprintf(stderr, "dc overflow- block: %d qscale: %d//\n", n, s->qscale);
de0f2f4c
MN
1669 if(s->inter_intra_pred) level=0;
1670 else return -1;
287229e5 1671 }
de6d9b64
FB
1672 if (n < 4) {
1673 rl = &rl_table[s->rl_table_index];
287229e5 1674 if(level > 256*s->y_dc_scale){
bd5e1c72 1675 fprintf(stderr, "dc overflow+ L qscale: %d//\n", s->qscale);
de0f2f4c 1676 if(!s->inter_intra_pred) return -1;
287229e5 1677 }
de6d9b64
FB
1678 } else {
1679 rl = &rl_table[3 + s->rl_chroma_table_index];
287229e5 1680 if(level > 256*s->c_dc_scale){
bd5e1c72 1681 fprintf(stderr, "dc overflow+ C qscale: %d//\n", s->qscale);
de0f2f4c 1682 if(!s->inter_intra_pred) return -1;
287229e5 1683 }
de6d9b64 1684 }
287229e5 1685 block[0] = level;
badaf88e 1686
de6d9b64 1687 run_diff = 0;
45a82edb 1688 i = 0;
de6d9b64
FB
1689 if (!coded) {
1690 goto not_coded;
1691 }
1692 if (s->ac_pred) {
1693 if (dc_pred_dir == 0)
2ad1516a 1694 scan_table = s->intra_v_scantable.permutated; /* left */
de6d9b64 1695 else
2ad1516a 1696 scan_table = s->intra_h_scantable.permutated; /* top */
de6d9b64 1697 } else {
2ad1516a 1698 scan_table = s->intra_scantable.permutated;
de6d9b64
FB
1699 }
1700 set_stat(ST_INTRA_AC);
45a82edb 1701 rl_vlc= rl->rl_vlc[0];
de6d9b64 1702 } else {
badaf88e
MN
1703 qmul = s->qscale << 1;
1704 qadd = (s->qscale - 1) | 1;
45a82edb 1705 i = -1;
de6d9b64 1706 rl = &rl_table[3 + s->rl_table_index];
84afee34
MN
1707
1708 if(s->msmpeg4_version==2)
1709 run_diff = 0;
1710 else
1711 run_diff = 1;
1712
de6d9b64 1713 if (!coded) {
45a82edb 1714 s->block_last_index[n] = i;
de6d9b64
FB
1715 return 0;
1716 }
2ad1516a 1717 scan_table = s->inter_scantable.permutated;
de6d9b64 1718 set_stat(ST_INTER_AC);
45a82edb 1719 rl_vlc= rl->rl_vlc[s->qscale];
de6d9b64 1720 }
45a82edb
MN
1721 {
1722 OPEN_READER(re, &s->gb);
de6d9b64 1723 for(;;) {
45a82edb
MN
1724 UPDATE_CACHE(re, &s->gb);
1725 GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2);
1726 if (level==0) {
1727 int cache;
1728 cache= GET_CACHE(re, &s->gb);
de6d9b64 1729 /* escape */
45a82edb
MN
1730 if (s->msmpeg4_version==1 || (cache&0x80000000)==0) {
1731 if (s->msmpeg4_version==1 || (cache&0x40000000)==0) {
de6d9b64 1732 /* third escape */
45a82edb
MN
1733 if(s->msmpeg4_version!=1) LAST_SKIP_BITS(re, &s->gb, 2);
1734 UPDATE_CACHE(re, &s->gb);
f5957f3f 1735 if(s->msmpeg4_version<=3){
45a82edb
MN
1736 last= SHOW_UBITS(re, &s->gb, 1); SKIP_CACHE(re, &s->gb, 1);
1737 run= SHOW_UBITS(re, &s->gb, 6); SKIP_CACHE(re, &s->gb, 6);
1738 level= SHOW_SBITS(re, &s->gb, 8); LAST_SKIP_CACHE(re, &s->gb, 8);
1739 SKIP_COUNTER(re, &s->gb, 1+6+8);
1740 }else{
f5957f3f 1741 int sign;
45a82edb 1742 last= SHOW_UBITS(re, &s->gb, 1); SKIP_BITS(re, &s->gb, 1);
f5957f3f
MN
1743 if(!s->esc3_level_length){
1744 int ll;
1745 //printf("ESC-3 %X at %d %d\n", show_bits(&s->gb, 24), s->mb_x, s->mb_y);
1746 if(s->qscale<8){
45a82edb 1747 ll= SHOW_UBITS(re, &s->gb, 3); SKIP_BITS(re, &s->gb, 3);
f5957f3f 1748 if(ll==0){
45a82edb
MN
1749 if(SHOW_UBITS(re, &s->gb, 1)) printf("cool a new vlc code ,contact the ffmpeg developers and upload the file\n");
1750 SKIP_BITS(re, &s->gb, 1);
f5957f3f
MN
1751 ll=8;
1752 }
1753 }else{
1754 ll=2;
45a82edb
MN
1755 while(ll<8 && SHOW_UBITS(re, &s->gb, 1)==0){
1756 ll++;
1757 SKIP_BITS(re, &s->gb, 1);
1758 }
05174fd4 1759 if(ll<8) SKIP_BITS(re, &s->gb, 1);
f5957f3f
MN
1760 }
1761
1762 s->esc3_level_length= ll;
45a82edb 1763 s->esc3_run_length= SHOW_UBITS(re, &s->gb, 2) + 3; SKIP_BITS(re, &s->gb, 2);
f5957f3f 1764//printf("level length:%d, run length: %d\n", ll, s->esc3_run_length);
05174fd4 1765 UPDATE_CACHE(re, &s->gb);
f5957f3f 1766 }
45a82edb
MN
1767 run= SHOW_UBITS(re, &s->gb, s->esc3_run_length);
1768 SKIP_BITS(re, &s->gb, s->esc3_run_length);
1769
1770 sign= SHOW_UBITS(re, &s->gb, 1);
1771 SKIP_BITS(re, &s->gb, 1);
1772
1773 level= SHOW_UBITS(re, &s->gb, s->esc3_level_length);
1774 SKIP_BITS(re, &s->gb, s->esc3_level_length);
f5957f3f
MN
1775 if(sign) level= -level;
1776 }
1777//printf("level: %d, run: %d at %d %d\n", level, run, s->mb_x, s->mb_y);
287229e5
MN
1778#if 0 // waste of time / this will detect very few errors
1779 {
1780 const int abs_level= ABS(level);
1781 const int run1= run - rl->max_run[last][abs_level] - run_diff;
1782 if(abs_level<=MAX_LEVEL && run<=MAX_RUN){
1783 if(abs_level <= rl->max_level[last][run]){
1784 fprintf(stderr, "illegal 3. esc, vlc encoding possible\n");
1785 return DECODING_AC_LOST;
1786 }
1787 if(abs_level <= rl->max_level[last][run]*2){
1788 fprintf(stderr, "illegal 3. esc, esc 1 encoding possible\n");
1789 return DECODING_AC_LOST;
1790 }
f5957f3f 1791 if(run1>=0 && abs_level <= rl->max_level[last][run1]){
287229e5
MN
1792 fprintf(stderr, "illegal 3. esc, esc 2 encoding possible\n");
1793 return DECODING_AC_LOST;
1794 }
1795 }
1796 }
1797#endif
38d171eb
ZK
1798 //level = level * qmul + (level>0) * qadd - (level<=0) * qadd ;
1799 if (level>0) level= level * qmul + qadd;
287229e5
MN
1800 else level= level * qmul - qadd;
1801#if 0 // waste of time too :(
1802 if(level>2048 || level<-2048){
1803 fprintf(stderr, "|level| overflow in 3. esc\n");
1804 return DECODING_AC_LOST;
1805 }
1806#endif
45a82edb
MN
1807 i+= run + 1;
1808 if(last) i+=192;
1a013246
MN
1809#ifdef ERROR_DETAILS
1810 if(run==66)
1811 fprintf(stderr, "illegal vlc code in ESC3 level=%d\n", level);
1812 else if((i>62 && i<192) || i>192+63)
1813 fprintf(stderr, "run overflow in ESC3 i=%d run=%d level=%d\n", i, run, level);
1814#endif
de6d9b64
FB
1815 } else {
1816 /* second escape */
45a82edb
MN
1817#if MIN_CACHE_BITS < 23
1818 LAST_SKIP_BITS(re, &s->gb, 2);
1819 UPDATE_CACHE(re, &s->gb);
1820#else
1821 SKIP_BITS(re, &s->gb, 2);
1822#endif
1823 GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2);
1824 i+= run + rl->max_run[run>>7][level/qmul] + run_diff; //FIXME opt indexing
1825 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1826 LAST_SKIP_BITS(re, &s->gb, 1);
1a013246
MN
1827#ifdef ERROR_DETAILS
1828 if(run==66)
1829 fprintf(stderr, "illegal vlc code in ESC2 level=%d\n", level);
1830 else if((i>62 && i<192) || i>192+63)
1831 fprintf(stderr, "run overflow in ESC2 i=%d run=%d level=%d\n", i, run, level);
1832#endif
de6d9b64
FB
1833 }
1834 } else {
1835 /* first escape */
45a82edb
MN
1836#if MIN_CACHE_BITS < 22
1837 LAST_SKIP_BITS(re, &s->gb, 1);
1838 UPDATE_CACHE(re, &s->gb);
1839#else
1840 SKIP_BITS(re, &s->gb, 1);
1841#endif
1842 GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2);
1843 i+= run;
1844 level = level + rl->max_level[run>>7][(run-1)&63] * qmul;//FIXME opt indexing
1845 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1846 LAST_SKIP_BITS(re, &s->gb, 1);
1a013246
MN
1847#ifdef ERROR_DETAILS
1848 if(run==66)
1849 fprintf(stderr, "illegal vlc code in ESC1 level=%d\n", level);
1850 else if((i>62 && i<192) || i>192+63)
1851 fprintf(stderr, "run overflow in ESC1 i=%d run=%d level=%d\n", i, run, level);
1852#endif
de6d9b64
FB
1853 }
1854 } else {
45a82edb
MN
1855 i+= run;
1856 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1857 LAST_SKIP_BITS(re, &s->gb, 1);
1a013246
MN
1858#ifdef ERROR_DETAILS
1859 if(run==66)
1860 fprintf(stderr, "illegal vlc code level=%d\n", level);
1861 else if((i>62 && i<192) || i>192+63)
1862 fprintf(stderr, "run overflow i=%d run=%d level=%d\n", i, run, level);
1863#endif
f5957f3f 1864 }
45a82edb
MN
1865 if (i > 62){
1866 i-= 192;
1867 if(i&(~63)){
159d10fc
MN
1868 const int left= s->gb.size*8 - get_bits_count(&s->gb);
1869 if(((i+192 == 64 && level/qmul==-1) || s->error_resilience<0) && left>=0){
1a013246
MN
1870 fprintf(stderr, "ignoring overflow at %d %d\n", s->mb_x, s->mb_y);
1871 break;
1872 }else{
1873 fprintf(stderr, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
1874 return -1;
1875 }
45a82edb 1876 }
287229e5 1877
45a82edb 1878 block[scan_table[i]] = level;
de6d9b64 1879 break;
45a82edb
MN
1880 }
1881
1882 block[scan_table[i]] = level;
de6d9b64 1883 }
45a82edb
MN
1884 CLOSE_READER(re, &s->gb);
1885 }
de6d9b64
FB
1886 not_coded:
1887 if (s->mb_intra) {
1888 mpeg4_pred_ac(s, block, n, dc_pred_dir);
1889 if (s->ac_pred) {
45a82edb 1890 i = 63; /* XXX: not optimal */
de6d9b64
FB
1891 }
1892 }
45a82edb
MN
1893 if(s->msmpeg4_version==4 && i>0) i=63; //FIXME/XXX optimize
1894 s->block_last_index[n] = i;
f5957f3f 1895
de6d9b64
FB
1896 return 0;
1897}
1898
1899static int msmpeg4_decode_dc(MpegEncContext * s, int n, int *dir_ptr)
1900{
1901 int level, pred;
de6d9b64 1902
287229e5 1903 if(s->msmpeg4_version<=2){
84afee34 1904 if (n < 4) {
08dce7b0 1905 level = get_vlc2(&s->gb, v2_dc_lum_vlc.table, DC_VLC_BITS, 3);
84afee34 1906 } else {
08dce7b0 1907 level = get_vlc2(&s->gb, v2_dc_chroma_vlc.table, DC_VLC_BITS, 3);
84afee34 1908 }
2ed627e6 1909 if (level < 0)
84afee34 1910 return -1;
84afee34
MN
1911 level-=256;
1912 }else{ //FIXME optimize use unified tables & index
1913 if (n < 4) {
08dce7b0 1914 level = get_vlc2(&s->gb, dc_lum_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
84afee34 1915 } else {
08dce7b0 1916 level = get_vlc2(&s->gb, dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
84afee34 1917 }
287229e5
MN
1918 if (level < 0){
1919 fprintf(stderr, "illegal dc vlc\n");
84afee34 1920 return -1;
287229e5 1921 }
84afee34
MN
1922
1923 if (level == DC_MAX) {
1924 level = get_bits(&s->gb, 8);
1925 if (get_bits1(&s->gb))
1926 level = -level;
1927 } else if (level != 0) {
1928 if (get_bits1(&s->gb))
1929 level = -level;
1930 }
de6d9b64
FB
1931 }
1932
287229e5
MN
1933 if(s->msmpeg4_version==1){
1934 INT32 *dc_val;
1935 pred = msmpeg4v1_pred_dc(s, n, &dc_val);
1936 level += pred;
1937
1938 /* update predictor */
1939 *dc_val= level;
1940 }else{
f5957f3f 1941 UINT16 *dc_val;
bd5e1c72 1942 pred = msmpeg4_pred_dc(s, n, &dc_val, dir_ptr);
287229e5 1943 level += pred;
de6d9b64 1944
287229e5
MN
1945 /* update predictor */
1946 if (n < 4) {
1947 *dc_val = level * s->y_dc_scale;
1948 } else {
1949 *dc_val = level * s->c_dc_scale;
1950 }
de6d9b64
FB
1951 }
1952
1953 return level;
1954}
1955
1956static int msmpeg4_decode_motion(MpegEncContext * s,
1957 int *mx_ptr, int *my_ptr)
1958{
1959 MVTable *mv;
1960 int code, mx, my;
1961
1962 mv = &mv_tables[s->mv_table_index];
1963
08dce7b0 1964 code = get_vlc2(&s->gb, mv->vlc.table, MV_VLC_BITS, 2);
f5957f3f
MN
1965 if (code < 0){
1966 fprintf(stderr, "illegal MV code at %d %d\n", s->mb_x, s->mb_y);
de6d9b64 1967 return -1;
f5957f3f 1968 }
de6d9b64 1969 if (code == mv->n) {
f5957f3f 1970//printf("MV ESC %X at %d %d\n", show_bits(&s->gb, 24), s->mb_x, s->mb_y);
de6d9b64
FB
1971 mx = get_bits(&s->gb, 6);
1972 my = get_bits(&s->gb, 6);
1973 } else {
1974 mx = mv->table_mvx[code];
1975 my = mv->table_mvy[code];
1976 }
1977
1978 mx += *mx_ptr - 32;
1979 my += *my_ptr - 32;
1980 /* WARNING : they do not do exactly modulo encoding */
1981 if (mx <= -64)
1982 mx += 64;
1983 else if (mx >= 64)
1984 mx -= 64;
1985
1986 if (my <= -64)
1987 my += 64;
1988 else if (my >= 64)
1989 my -= 64;
1990 *mx_ptr = mx;
1991 *my_ptr = my;
1992 return 0;
1993}