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