dc scale simplification/optimization
[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"
24
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
35/* motion vector table */
36typedef struct MVTable {
37 int n;
38 const UINT16 *table_mv_code;
39 const UINT8 *table_mv_bits;
40 const UINT8 *table_mvx;
41 const UINT8 *table_mvy;
42 UINT16 *table_mv_index; /* encoding: convert mv to index in table_mv */
43 VLC vlc; /* decoding: vlc */
44} MVTable;
45
84afee34
MN
46static UINT32 v2_dc_lum_table[512][2];
47static UINT32 v2_dc_chroma_table[512][2];
48
de6d9b64
FB
49static void msmpeg4_encode_block(MpegEncContext * s, DCTELEM * block, int n);
50static int msmpeg4_decode_block(MpegEncContext * s, DCTELEM * block,
51 int n, int coded);
52static int msmpeg4_decode_dc(MpegEncContext * s, int n, int *dir_ptr);
53static int msmpeg4_decode_motion(MpegEncContext * s,
54 int *mx_ptr, int *my_ptr);
3825cd1d 55static void msmpeg4v2_encode_motion(MpegEncContext * s, int val);
20695ec9 56static void init_h263_dc_for_msmpeg4(void);
3825cd1d 57
de6d9b64 58
6fe84b43
MN
59extern UINT32 inverse[256];
60
de6d9b64
FB
61#ifdef DEBUG
62int intra_count = 0;
63int frame_count = 0;
64#endif
65/* XXX: move it to mpegvideo.h */
66
67static int init_done = 0;
68
69#include "msmpeg4data.h"
70
71#ifdef STATS
72
73const char *st_names[ST_NB] = {
74 "unknown",
75 "dc",
76 "intra_ac",
77 "inter_ac",
78 "intra_mb",
79 "inter_mb",
80 "mv",
81};
82
83int st_current_index = 0;
84unsigned int st_bit_counts[ST_NB];
85unsigned int st_out_bit_counts[ST_NB];
86
87#define set_stat(var) st_current_index = var;
88
89void print_stats(void)
90{
91 unsigned int total;
92 int i;
93
94 printf("Input:\n");
95 total = 0;
96 for(i=0;i<ST_NB;i++)
97 total += st_bit_counts[i];
98 if (total == 0)
99 total = 1;
100 for(i=0;i<ST_NB;i++) {
101 printf("%-10s : %10.1f %5.1f%%\n",
102 st_names[i],
103 (double)st_bit_counts[i] / 8.0,
104 (double)st_bit_counts[i] * 100.0 / total);
105 }
106 printf("%-10s : %10.1f %5.1f%%\n",
107 "total",
108 (double)total / 8.0,
109 100.0);
110
111 printf("Output:\n");
112 total = 0;
113 for(i=0;i<ST_NB;i++)
114 total += st_out_bit_counts[i];
115 if (total == 0)
116 total = 1;
117 for(i=0;i<ST_NB;i++) {
118 printf("%-10s : %10.1f %5.1f%%\n",
119 st_names[i],
120 (double)st_out_bit_counts[i] / 8.0,
121 (double)st_out_bit_counts[i] * 100.0 / total);
122 }
123 printf("%-10s : %10.1f %5.1f%%\n",
124 "total",
125 (double)total / 8.0,
126 100.0);
127}
128
129#else
130
131#define set_stat(var)
132
133#endif
134
135/* build the table which associate a (x,y) motion vector to a vlc */
136static void init_mv_table(MVTable *tab)
137{
138 int i, x, y;
139
6000abfa 140 tab->table_mv_index = av_malloc(sizeof(UINT16) * 4096);
de6d9b64
FB
141 /* mark all entries as not used */
142 for(i=0;i<4096;i++)
143 tab->table_mv_index[i] = tab->n;
144
145 for(i=0;i<tab->n;i++) {
146 x = tab->table_mvx[i];
147 y = tab->table_mvy[i];
148 tab->table_mv_index[(x << 6) | y] = i;
149 }
150}
151
152static void code012(PutBitContext *pb, int n)
153{
154 if (n == 0) {
155 put_bits(pb, 1, 0);
156 } else {
157 put_bits(pb, 1, 1);
158 put_bits(pb, 1, (n >= 2));
159 }
160}
161
287229e5 162/* write MSMPEG4 compatible frame header */
de6d9b64
FB
163void msmpeg4_encode_picture_header(MpegEncContext * s, int picture_number)
164{
165 int i;
166
167 align_put_bits(&s->pb);
168
169 put_bits(&s->pb, 2, s->pict_type - 1);
170
171 put_bits(&s->pb, 5, s->qscale);
172
173 s->rl_table_index = 2;
287229e5 174 if(s->msmpeg4_version<=2)
3825cd1d
MN
175 s->rl_chroma_table_index = 2; /* only for I frame */
176 else
177 s->rl_chroma_table_index = 1; /* only for I frame */
178
de6d9b64
FB
179 s->dc_table_index = 1;
180 s->mv_table_index = 1; /* only if P frame */
181 s->use_skip_mb_code = 1; /* only if P frame */
182
183 if (s->pict_type == I_TYPE) {
184 put_bits(&s->pb, 5, 0x17); /* indicate only one "slice" */
185
287229e5 186 if(s->msmpeg4_version>2){
3825cd1d
MN
187 code012(&s->pb, s->rl_chroma_table_index);
188 code012(&s->pb, s->rl_table_index);
de6d9b64 189
3825cd1d
MN
190 put_bits(&s->pb, 1, s->dc_table_index);
191 }
de6d9b64
FB
192 s->no_rounding = 1;
193 } else {
194 put_bits(&s->pb, 1, s->use_skip_mb_code);
195
196 s->rl_chroma_table_index = s->rl_table_index;
287229e5 197 if(s->msmpeg4_version>2){
3825cd1d 198 code012(&s->pb, s->rl_table_index);
de6d9b64 199
3825cd1d
MN
200 put_bits(&s->pb, 1, s->dc_table_index);
201
202 put_bits(&s->pb, 1, s->mv_table_index);
203 }
de6d9b64 204
ae40484c
MN
205 if(s->flipflop_rounding){
206 s->no_rounding ^= 1;
207 }else{
208 s->no_rounding = 0;
209 }
de6d9b64
FB
210 }
211
212 if (!init_done) {
213 /* init various encoding tables */
214 init_done = 1;
215 init_mv_table(&mv_tables[0]);
216 init_mv_table(&mv_tables[1]);
217 for(i=0;i<NB_RL_TABLES;i++)
218 init_rl(&rl_table[i]);
3825cd1d
MN
219
220 init_h263_dc_for_msmpeg4();
de6d9b64
FB
221 }
222
223#ifdef DEBUG
224 intra_count = 0;
225 printf("*****frame %d:\n", frame_count++);
226#endif
227}
228
ae40484c
MN
229void msmpeg4_encode_ext_header(MpegEncContext * s)
230{
2b9ab1d5 231 put_bits(&s->pb, 5, s->frame_rate / FRAME_RATE_BASE); //yes 29.97 -> 29
ae40484c 232
287229e5 233 put_bits(&s->pb, 11, MIN(s->bit_rate, 2047));
ae40484c 234
287229e5
MN
235 if(s->msmpeg4_version<3)
236 s->flipflop_rounding=0;
237 else{
238 s->flipflop_rounding=1;
239 put_bits(&s->pb, 1, s->flipflop_rounding);
240 }
ae40484c
MN
241}
242
de6d9b64
FB
243/* predict coded block */
244static inline int coded_block_pred(MpegEncContext * s, int n, UINT8 **coded_block_ptr)
245{
dbbe8999 246 int xy, wrap, pred, a, b, c;
de6d9b64 247
dbbe8999
MN
248 xy = s->block_index[n];
249 wrap = s->block_wrap[0];
de6d9b64
FB
250
251 /* B C
252 * A X
253 */
dbbe8999
MN
254 a = s->coded_block[xy - 1 ];
255 b = s->coded_block[xy - 1 - wrap];
256 c = s->coded_block[xy - wrap];
de6d9b64
FB
257
258 if (b == c) {
259 pred = a;
260 } else {
261 pred = c;
262 }
263
264 /* store value */
dbbe8999 265 *coded_block_ptr = &s->coded_block[xy];
de6d9b64
FB
266
267 return pred;
268}
269
270static void msmpeg4_encode_motion(MpegEncContext * s,
271 int mx, int my)
272{
273 int code;
274 MVTable *mv;
275
276 /* modulo encoding */
277 /* WARNING : you cannot reach all the MVs even with the modulo
278 encoding. This is a somewhat strange compromise they took !!! */
279 if (mx <= -64)
280 mx += 64;
281 else if (mx >= 64)
282 mx -= 64;
283 if (my <= -64)
284 my += 64;
285 else if (my >= 64)
286 my -= 64;
287
288 mx += 32;
289 my += 32;
290#if 0
291 if ((unsigned)mx >= 64 ||
292 (unsigned)my >= 64)
293 fprintf(stderr, "error mx=%d my=%d\n", mx, my);
294#endif
295 mv = &mv_tables[s->mv_table_index];
296
297 code = mv->table_mv_index[(mx << 6) | my];
298 set_stat(ST_MV);
299 put_bits(&s->pb,
300 mv->table_mv_bits[code],
301 mv->table_mv_code[code]);
302 if (code == mv->n) {
303 /* escape : code litterally */
304 put_bits(&s->pb, 6, mx);
305 put_bits(&s->pb, 6, my);
306 }
307}
308
309void msmpeg4_encode_mb(MpegEncContext * s,
310 DCTELEM block[6][64],
311 int motion_x, int motion_y)
312{
313 int cbp, coded_cbp, i;
314 int pred_x, pred_y;
315 UINT8 *coded_block;
316
317 if (!s->mb_intra) {
318 /* compute cbp */
319 set_stat(ST_INTER_MB);
320 cbp = 0;
321 for (i = 0; i < 6; i++) {
322 if (s->block_last_index[i] >= 0)
323 cbp |= 1 << (5 - i);
324 }
325 if (s->use_skip_mb_code && (cbp | motion_x | motion_y) == 0) {
326 /* skip macroblock */
327 put_bits(&s->pb, 1, 1);
328 return;
329 }
330 if (s->use_skip_mb_code)
331 put_bits(&s->pb, 1, 0); /* mb coded */
332
287229e5 333 if(s->msmpeg4_version<=2){
3825cd1d
MN
334 put_bits(&s->pb,
335 v2_mb_type[cbp&3][1],
336 v2_mb_type[cbp&3][0]);
337 if((cbp&3) != 3) coded_cbp= cbp ^ 0x3C;
338 else coded_cbp= cbp;
339
340 put_bits(&s->pb,
341 cbpy_tab[coded_cbp>>2][1],
342 cbpy_tab[coded_cbp>>2][0]);
343
344 h263_pred_motion(s, 0, &pred_x, &pred_y);
345 msmpeg4v2_encode_motion(s, motion_x - pred_x);
346 msmpeg4v2_encode_motion(s, motion_y - pred_y);
347 }else{
348 put_bits(&s->pb,
349 table_mb_non_intra[cbp + 64][1],
350 table_mb_non_intra[cbp + 64][0]);
351
352 /* motion vector */
353 h263_pred_motion(s, 0, &pred_x, &pred_y);
354 msmpeg4_encode_motion(s, motion_x - pred_x,
355 motion_y - pred_y);
356 }
de6d9b64
FB
357 } else {
358 /* compute cbp */
359 cbp = 0;
360 coded_cbp = 0;
361 for (i = 0; i < 6; i++) {
362 int val, pred;
363 val = (s->block_last_index[i] >= 1);
364 cbp |= val << (5 - i);
365 if (i < 4) {
366 /* predict value for close blocks only for luma */
367 pred = coded_block_pred(s, i, &coded_block);
368 *coded_block = val;
369 val = val ^ pred;
370 }
371 coded_cbp |= val << (5 - i);
372 }
373#if 0
374 if (coded_cbp)
375 printf("cbp=%x %x\n", cbp, coded_cbp);
376#endif
377
287229e5 378 if(s->msmpeg4_version<=2){
3825cd1d
MN
379 if (s->pict_type == I_TYPE) {
380 put_bits(&s->pb,
381 v2_intra_cbpc[cbp&3][1], v2_intra_cbpc[cbp&3][0]);
382 } else {
383 if (s->use_skip_mb_code)
384 put_bits(&s->pb, 1, 0); /* mb coded */
385 put_bits(&s->pb,
386 v2_mb_type[(cbp&3) + 4][1],
387 v2_mb_type[(cbp&3) + 4][0]);
388 }
389 put_bits(&s->pb, 1, 0); /* no AC prediction yet */
de6d9b64 390 put_bits(&s->pb,
3825cd1d
MN
391 cbpy_tab[cbp>>2][1],
392 cbpy_tab[cbp>>2][0]);
393 }else{
394 if (s->pict_type == I_TYPE) {
395 set_stat(ST_INTRA_MB);
396 put_bits(&s->pb,
397 table_mb_intra[coded_cbp][1], table_mb_intra[coded_cbp][0]);
398 } else {
399 if (s->use_skip_mb_code)
400 put_bits(&s->pb, 1, 0); /* mb coded */
401 put_bits(&s->pb,
402 table_mb_non_intra[cbp][1],
403 table_mb_non_intra[cbp][0]);
404 }
405 set_stat(ST_INTRA_MB);
406 put_bits(&s->pb, 1, 0); /* no AC prediction yet */
de6d9b64 407 }
de6d9b64
FB
408 }
409
410 for (i = 0; i < 6; i++) {
411 msmpeg4_encode_block(s, block[i], i);
412 }
413}
414
92ba5ffb
MN
415/* old ffmpeg msmpeg4v3 mode */
416void ff_old_msmpeg4_dc_scale(MpegEncContext * s)
de6d9b64 417{
92ba5ffb 418 if (s->qscale < 5){
6fb904ce
MN
419 s->y_dc_scale = 8;
420 s->c_dc_scale = 8;
6fb904ce
MN
421 }else if (s->qscale < 9){
422 s->y_dc_scale = 2 * s->qscale;
423 s->c_dc_scale = (s->qscale + 13)>>1;
92ba5ffb 424 }else{
6fb904ce
MN
425 s->y_dc_scale = s->qscale + 8;
426 s->c_dc_scale = (s->qscale + 13)>>1;
427 }
de6d9b64
FB
428}
429
287229e5
MN
430static int msmpeg4v1_pred_dc(MpegEncContext * s, int n,
431 INT32 **dc_val_ptr)
432{
433 int i;
434
435 if (n < 4) {
436 i= 0;
437 } else {
438 i= n-3;
439 }
440
441 *dc_val_ptr= &s->last_dc[i];
442 return s->last_dc[i];
443}
444
de6d9b64
FB
445/* dir = 0: left, dir = 1: top prediction */
446static int msmpeg4_pred_dc(MpegEncContext * s, int n,
98be975d 447 INT16 **dc_val_ptr, int *dir_ptr)
de6d9b64 448{
dbbe8999 449 int a, b, c, wrap, pred, scale;
98be975d 450 INT16 *dc_val;
de6d9b64
FB
451
452 /* find prediction */
453 if (n < 4) {
de6d9b64
FB
454 scale = s->y_dc_scale;
455 } else {
de6d9b64
FB
456 scale = s->c_dc_scale;
457 }
287229e5 458
dbbe8999
MN
459 wrap = s->block_wrap[n];
460 dc_val= s->dc_val[0] + s->block_index[n];
de6d9b64
FB
461
462 /* B C
463 * A X
464 */
dbbe8999
MN
465 a = dc_val[ - 1];
466 b = dc_val[ - 1 - wrap];
467 c = dc_val[ - wrap];
de6d9b64
FB
468
469 /* XXX: the following solution consumes divisions, but it does not
470 necessitate to modify mpegvideo.c. The problem comes from the
471 fact they decided to store the quantized DC (which would lead
472 to problems if Q could vary !) */
320680d4 473#if defined ARCH_X86 && !defined PIC
6f903d8e
MN
474 asm volatile(
475 "movl %3, %%eax \n\t"
476 "shrl $1, %%eax \n\t"
477 "addl %%eax, %2 \n\t"
478 "addl %%eax, %1 \n\t"
479 "addl %0, %%eax \n\t"
6fe84b43
MN
480 "mull %4 \n\t"
481 "movl %%edx, %0 \n\t"
6f903d8e 482 "movl %1, %%eax \n\t"
6fe84b43
MN
483 "mull %4 \n\t"
484 "movl %%edx, %1 \n\t"
6f903d8e 485 "movl %2, %%eax \n\t"
6fe84b43
MN
486 "mull %4 \n\t"
487 "movl %%edx, %2 \n\t"
fa778d53
MN
488 : "+b" (a), "+c" (b), "+D" (c)
489 : "g" (scale), "S" (inverse[scale])
6f903d8e
MN
490 : "%eax", "%edx"
491 );
320680d4
ZK
492#else
493 /* #elif defined (ARCH_ALPHA) */
1e98dffb 494 /* Divisions are extremely costly on Alpha; optimize the most
320680d4
ZK
495 common case. But they are costly everywhere...
496 */
1e98dffb
NK
497 if (scale == 8) {
498 a = (a + (8 >> 1)) / 8;
499 b = (b + (8 >> 1)) / 8;
500 c = (c + (8 >> 1)) / 8;
501 } else {
502 a = (a + (scale >> 1)) / scale;
503 b = (b + (scale >> 1)) / scale;
504 c = (c + (scale >> 1)) / scale;
505 }
6f903d8e 506#endif
de6d9b64
FB
507 /* XXX: WARNING: they did not choose the same test as MPEG4. This
508 is very important ! */
509 if (abs(a - b) <= abs(b - c)) {
510 pred = c;
511 *dir_ptr = 1;
512 } else {
513 pred = a;
514 *dir_ptr = 0;
515 }
516
517 /* update predictor */
dbbe8999 518 *dc_val_ptr = &dc_val[0];
de6d9b64
FB
519 return pred;
520}
521
522#define DC_MAX 119
523
524static void msmpeg4_encode_dc(MpegEncContext * s, int level, int n, int *dir_ptr)
525{
526 int sign, code;
527 int pred;
de6d9b64 528
287229e5
MN
529 if(s->msmpeg4_version==1){
530 INT32 *dc_val;
531 pred = msmpeg4v1_pred_dc(s, n, &dc_val);
532
533 /* update predictor */
534 *dc_val= level;
535 }else{
536 INT16 *dc_val;
537 pred = msmpeg4_pred_dc(s, n, &dc_val, dir_ptr);
de6d9b64 538
287229e5
MN
539 /* update predictor */
540 if (n < 4) {
541 *dc_val = level * s->y_dc_scale;
542 } else {
543 *dc_val = level * s->c_dc_scale;
544 }
de6d9b64
FB
545 }
546
547 /* do the prediction */
548 level -= pred;
549
287229e5 550 if(s->msmpeg4_version<=2){
de6d9b64 551 if (n < 4) {
3825cd1d
MN
552 put_bits(&s->pb,
553 v2_dc_lum_table[level+256][1],
554 v2_dc_lum_table[level+256][0]);
555 }else{
556 put_bits(&s->pb,
557 v2_dc_chroma_table[level+256][1],
558 v2_dc_chroma_table[level+256][0]);
de6d9b64 559 }
3825cd1d
MN
560 }else{
561 sign = 0;
562 if (level < 0) {
563 level = -level;
564 sign = 1;
565 }
566 code = level;
567 if (code > DC_MAX)
568 code = DC_MAX;
569
570 if (s->dc_table_index == 0) {
571 if (n < 4) {
572 put_bits(&s->pb, table0_dc_lum[code][1], table0_dc_lum[code][0]);
573 } else {
574 put_bits(&s->pb, table0_dc_chroma[code][1], table0_dc_chroma[code][0]);
575 }
de6d9b64 576 } else {
3825cd1d
MN
577 if (n < 4) {
578 put_bits(&s->pb, table1_dc_lum[code][1], table1_dc_lum[code][0]);
579 } else {
580 put_bits(&s->pb, table1_dc_chroma[code][1], table1_dc_chroma[code][0]);
581 }
582 }
583
584 if (code == DC_MAX)
585 put_bits(&s->pb, 8, level);
586
587 if (level != 0) {
588 put_bits(&s->pb, 1, sign);
de6d9b64 589 }
de6d9b64
FB
590 }
591}
592
593/* Encoding of a block. Very similar to MPEG4 except for a different
594 escape coding (same as H263) and more vlc tables.
595 */
596static void msmpeg4_encode_block(MpegEncContext * s, DCTELEM * block, int n)
597{
598 int level, run, last, i, j, last_index;
599 int last_non_zero, sign, slevel;
600 int code, run_diff, dc_pred_dir;
601 const RLTable *rl;
602
603 if (s->mb_intra) {
604 set_stat(ST_DC);
605 msmpeg4_encode_dc(s, block[0], n, &dc_pred_dir);
606 i = 1;
607 if (n < 4) {
608 rl = &rl_table[s->rl_table_index];
609 } else {
610 rl = &rl_table[3 + s->rl_chroma_table_index];
611 }
612 run_diff = 0;
613 set_stat(ST_INTRA_AC);
614 } else {
615 i = 0;
616 rl = &rl_table[3 + s->rl_table_index];
287229e5 617 if(s->msmpeg4_version<=2)
3825cd1d
MN
618 run_diff = 0;
619 else
620 run_diff = 1;
de6d9b64
FB
621 set_stat(ST_INTER_AC);
622 }
623
624 /* AC coefs */
625 last_index = s->block_last_index[n];
626 last_non_zero = i - 1;
627 for (; i <= last_index; i++) {
628 j = zigzag_direct[i];
629 level = block[j];
630 if (level) {
631 run = i - last_non_zero - 1;
632 last = (i == last_index);
633 sign = 0;
634 slevel = level;
635 if (level < 0) {
636 sign = 1;
637 level = -level;
638 }
639 code = get_rl_index(rl, last, run, level);
640 put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
641 if (code == rl->n) {
642 int level1, run1;
643
644 level1 = level - rl->max_level[last][run];
645 if (level1 < 1)
646 goto esc2;
647 code = get_rl_index(rl, last, run, level1);
648 if (code == rl->n) {
649 esc2:
650 put_bits(&s->pb, 1, 0);
651 if (level > MAX_LEVEL)
652 goto esc3;
653 run1 = run - rl->max_run[last][level] - run_diff;
654 if (run1 < 0)
655 goto esc3;
656 code = get_rl_index(rl, last, run1, level);
657 if (code == rl->n) {
658 esc3:
659 /* third escape */
660 put_bits(&s->pb, 1, 0);
661 put_bits(&s->pb, 1, last);
662 put_bits(&s->pb, 6, run);
663 put_bits(&s->pb, 8, slevel & 0xff);
664 } else {
665 /* second escape */
666 put_bits(&s->pb, 1, 1);
667 put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
668 put_bits(&s->pb, 1, sign);
669 }
670 } else {
671 /* first escape */
672 put_bits(&s->pb, 1, 1);
673 put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
674 put_bits(&s->pb, 1, sign);
675 }
676 } else {
677 put_bits(&s->pb, 1, sign);
678 }
679 last_non_zero = i;
680 }
681 }
682}
683
684/****************************************/
685/* decoding stuff */
686
687static VLC mb_non_intra_vlc;
688static VLC mb_intra_vlc;
689static VLC dc_lum_vlc[2];
690static VLC dc_chroma_vlc[2];
84afee34
MN
691static VLC v2_dc_lum_vlc;
692static VLC v2_dc_chroma_vlc;
693static VLC cbpy_vlc;
694static VLC v2_intra_cbpc_vlc;
695static VLC v2_mb_type_vlc;
696static VLC v2_mv_vlc;
287229e5
MN
697static VLC v1_intra_cbpc_vlc;
698static VLC v1_inter_cbpc_vlc;
84afee34
MN
699
700/* this table is practically identical to the one from h263 except that its inverted */
20695ec9 701static void init_h263_dc_for_msmpeg4(void)
84afee34
MN
702{
703 static int inited=0;
704
705 if(!inited){
706 int level, uni_code, uni_len;
707 inited=1;
708
2ed627e6 709 for(level=-256; level<256; level++){
84afee34
MN
710 int size, v, l;
711 /* find number of bits */
712 size = 0;
713 v = abs(level);
714 while (v) {
715 v >>= 1;
716 size++;
717 }
718
719 if (level < 0)
720 l= (-level) ^ ((1 << size) - 1);
721 else
722 l= level;
723
724 /* luminance h263 */
725 uni_code= DCtab_lum[size][0];
726 uni_len = DCtab_lum[size][1];
727 uni_code ^= (1<<uni_len)-1; //M$ doesnt like compatibility
728
729 if (size > 0) {
730 uni_code<<=size; uni_code|=l;
731 uni_len+=size;
732 if (size > 8){
733 uni_code<<=1; uni_code|=1;
734 uni_len++;
735 }
736 }
737 v2_dc_lum_table[level+256][0]= uni_code;
738 v2_dc_lum_table[level+256][1]= uni_len;
739
740 /* chrominance h263 */
741 uni_code= DCtab_chrom[size][0];
742 uni_len = DCtab_chrom[size][1];
743 uni_code ^= (1<<uni_len)-1; //M$ doesnt like compatibility
744
745 if (size > 0) {
746 uni_code<<=size; uni_code|=l;
747 uni_len+=size;
748 if (size > 8){
749 uni_code<<=1; uni_code|=1;
750 uni_len++;
751 }
752 }
753 v2_dc_chroma_table[level+256][0]= uni_code;
754 v2_dc_chroma_table[level+256][1]= uni_len;
755
756 }
757 }
758}
de6d9b64
FB
759
760/* init all vlc decoding tables */
761int msmpeg4_decode_init_vlc(MpegEncContext *s)
762{
d81c5983 763 static int done = 0;
de6d9b64
FB
764 int i;
765 MVTable *mv;
766
de6d9b64 767
d81c5983
FB
768 if (!done) {
769 done = 1;
770
771 for(i=0;i<NB_RL_TABLES;i++) {
772 init_rl(&rl_table[i]);
773 init_vlc_rl(&rl_table[i]);
774 }
775 for(i=0;i<2;i++) {
776 mv = &mv_tables[i];
777 init_vlc(&mv->vlc, 9, mv->n + 1,
778 mv->table_mv_bits, 1, 1,
779 mv->table_mv_code, 2, 2);
780 }
781
782 init_vlc(&dc_lum_vlc[0], 9, 120,
783 &table0_dc_lum[0][1], 8, 4,
784 &table0_dc_lum[0][0], 8, 4);
785 init_vlc(&dc_chroma_vlc[0], 9, 120,
786 &table0_dc_chroma[0][1], 8, 4,
787 &table0_dc_chroma[0][0], 8, 4);
788 init_vlc(&dc_lum_vlc[1], 9, 120,
789 &table1_dc_lum[0][1], 8, 4,
790 &table1_dc_lum[0][0], 8, 4);
791 init_vlc(&dc_chroma_vlc[1], 9, 120,
792 &table1_dc_chroma[0][1], 8, 4,
793 &table1_dc_chroma[0][0], 8, 4);
84afee34 794
d81c5983
FB
795 init_h263_dc_for_msmpeg4();
796 init_vlc(&v2_dc_lum_vlc, 9, 512,
797 &v2_dc_lum_table[0][1], 8, 4,
798 &v2_dc_lum_table[0][0], 8, 4);
799 init_vlc(&v2_dc_chroma_vlc, 9, 512,
800 &v2_dc_chroma_table[0][1], 8, 4,
801 &v2_dc_chroma_table[0][0], 8, 4);
84afee34 802
d81c5983
FB
803 init_vlc(&cbpy_vlc, 6, 16,
804 &cbpy_tab[0][1], 2, 1,
805 &cbpy_tab[0][0], 2, 1);
806 init_vlc(&v2_intra_cbpc_vlc, 3, 4,
807 &v2_intra_cbpc[0][1], 2, 1,
808 &v2_intra_cbpc[0][0], 2, 1);
809 init_vlc(&v2_mb_type_vlc, 5, 8,
810 &v2_mb_type[0][1], 2, 1,
811 &v2_mb_type[0][0], 2, 1);
812 init_vlc(&v2_mv_vlc, 9, 33,
813 &mvtab[0][1], 2, 1,
814 &mvtab[0][0], 2, 1);
815
816 init_vlc(&mb_non_intra_vlc, 9, 128,
817 &table_mb_non_intra[0][1], 8, 4,
818 &table_mb_non_intra[0][0], 8, 4);
819 init_vlc(&mb_intra_vlc, 9, 64,
820 &table_mb_intra[0][1], 4, 2,
821 &table_mb_intra[0][0], 4, 2);
287229e5 822
d81c5983
FB
823 init_vlc(&v1_intra_cbpc_vlc, 6, 8,
824 intra_MCBPC_bits, 1, 1,
825 intra_MCBPC_code, 1, 1);
826 init_vlc(&v1_inter_cbpc_vlc, 6, 25,
827 inter_MCBPC_bits, 1, 1,
828 inter_MCBPC_code, 1, 1);
829 }
de6d9b64
FB
830 return 0;
831}
832
833static int decode012(GetBitContext *gb)
834{
835 int n;
612476ef 836 n = get_bits1(gb);
de6d9b64
FB
837 if (n == 0)
838 return 0;
839 else
612476ef 840 return get_bits1(gb) + 1;
de6d9b64
FB
841}
842
84afee34 843int msmpeg4_decode_picture_header(MpegEncContext * s)
de6d9b64 844{
e1a9dbff 845 int code, code2;
de6d9b64 846
e1a9dbff
MN
847#if 0
848{
849int i;
850for(i=0; i<s->gb.size*8; i++)
851 printf("%d", get_bits1(&s->gb));
852// get_bits1(&s->gb);
853printf("END\n");
854return -1;
855}
856#endif
287229e5
MN
857
858 if(s->msmpeg4_version==1){
859 int start_code, num;
860 start_code = (get_bits(&s->gb, 16)<<16) | get_bits(&s->gb, 16);
861 if(start_code!=0x00000100){
862 fprintf(stderr, "invalid startcode\n");
863 return -1;
864 }
865
866 num= get_bits(&s->gb, 5); // frame number */
867 }
868
de6d9b64
FB
869 s->pict_type = get_bits(&s->gb, 2) + 1;
870 if (s->pict_type != I_TYPE &&
287229e5
MN
871 s->pict_type != P_TYPE){
872 fprintf(stderr, "invalid picture type\n");
de6d9b64 873 return -1;
287229e5 874 }
de6d9b64
FB
875
876 s->qscale = get_bits(&s->gb, 5);
877
878 if (s->pict_type == I_TYPE) {
879 code = get_bits(&s->gb, 5);
287229e5
MN
880 if(s->msmpeg4_version==1){
881 if(code==0 || code>s->mb_height){
882 fprintf(stderr, "invalid slice height %d\n", code);
883 return -1;
884 }
885
886 s->slice_height = code;
887 }else{
888 /* 0x17: one slice, 0x18: two slices, ... */
889 if (code < 0x17)
890 return -1;
891
892 s->slice_height = s->mb_height / (code - 0x16);
893 }
e1a9dbff
MN
894
895 switch(s->msmpeg4_version){
287229e5 896 case 1:
e1a9dbff 897 case 2:
84afee34
MN
898 s->rl_chroma_table_index = 2;
899 s->rl_table_index = 2;
7f89b6fb 900
84afee34 901 s->dc_table_index = 0; //not used
e1a9dbff
MN
902 break;
903 case 3:
84afee34
MN
904 s->rl_chroma_table_index = decode012(&s->gb);
905 s->rl_table_index = decode012(&s->gb);
7f89b6fb 906
84afee34 907 s->dc_table_index = get_bits1(&s->gb);
e1a9dbff
MN
908 break;
909 case 4:
910 msmpeg4_decode_ext_header(s, 999 /* bufer size (useless here) */);
911 printf("%X\n", show_bits(&s->gb, 24));
912 code= get_bits(&s->gb, 2);
913 if(code==1){
914 code2= get_bits(&s->gb, 3);
915 if(code2==7) skip_bits(&s->gb, 1);
916 }
917 printf("%X\n", show_bits(&s->gb, 24));
918 s->rl_chroma_table_index = 2;
919 s->rl_table_index = 2;
920
921 s->dc_table_index = 0;
922 break;
84afee34 923 }
de6d9b64 924 s->no_rounding = 1;
badaf88e
MN
925/* printf(" %d %d %d %d \n",
926 s->qscale,
927 s->rl_chroma_table_index,
928 s->rl_table_index,
929 s->dc_table_index);*/
de6d9b64 930 } else {
de6d9b64 931
287229e5
MN
932 switch(s->msmpeg4_version){
933 case 1:
934 case 2:
935 if(s->msmpeg4_version==1)
936 s->use_skip_mb_code = 1;
937 else
938 s->use_skip_mb_code = get_bits1(&s->gb);
84afee34
MN
939 s->rl_table_index = 2;
940 s->rl_chroma_table_index = s->rl_table_index;
84afee34 941 s->dc_table_index = 0; //not used
84afee34 942 s->mv_table_index = 0;
287229e5
MN
943 break;
944 case 3:
945 s->use_skip_mb_code = get_bits1(&s->gb);
84afee34
MN
946 s->rl_table_index = decode012(&s->gb);
947 s->rl_chroma_table_index = s->rl_table_index;
de6d9b64 948
84afee34 949 s->dc_table_index = get_bits1(&s->gb);
de6d9b64 950
84afee34 951 s->mv_table_index = get_bits1(&s->gb);
287229e5 952 break;
84afee34 953 }
badaf88e
MN
954/* printf(" %d %d %d %d %d \n",
955 s->use_skip_mb_code,
956 s->rl_table_index,
957 s->rl_chroma_table_index,
958 s->dc_table_index,
959 s->mv_table_index);*/
ae40484c
MN
960 if(s->flipflop_rounding){
961 s->no_rounding ^= 1;
962 }else{
963 s->no_rounding = 0;
964 }
965// printf("%d", s->no_rounding);
e1a9dbff 966//return -1;
de6d9b64 967 }
ae40484c 968
84afee34
MN
969#if 0
970if(s->msmpeg4_version==2)
971{
972int i;
84afee34
MN
973for(i=0; i<s->gb.size*8; i++)
974// printf("%d", get_bits1(&s->gb));
975 get_bits1(&s->gb);
976printf("END\n");
977return -1;
978}
979#endif
980
de6d9b64
FB
981#ifdef DEBUG
982 printf("*****frame %d:\n", frame_count++);
983#endif
984 return 0;
985}
986
ae40484c
MN
987int msmpeg4_decode_ext_header(MpegEncContext * s, int buf_size)
988{
287229e5
MN
989 int left= buf_size*8 - get_bits_count(&s->gb);
990 int length= s->msmpeg4_version>=3 ? 17 : 16;
ae40484c 991 /* the alt_bitstream reader could read over the end so we need to check it */
287229e5 992 if(left>=length && left<length+8)
ae40484c 993 {
2b9ab1d5
MN
994 int fps;
995
996 fps= get_bits(&s->gb, 5);
287229e5
MN
997 s->bit_rate= get_bits(&s->gb, 11);
998 if(s->msmpeg4_version>=3)
999 s->flipflop_rounding= get_bits1(&s->gb);
1000 else
1001 s->flipflop_rounding= 0;
2b9ab1d5 1002
287229e5 1003// printf("fps:%2d bps:%2d roundingType:%1d\n", fps, s->bit_rate, s->flipflop_rounding);
ae40484c 1004 }
287229e5 1005 else if(left<length+8)
ae40484c 1006 {
2b9ab1d5 1007 s->flipflop_rounding= 0;
287229e5
MN
1008 printf("ext header missing, %d left\n", left);
1009 }
1010 else
1011 {
1012 fprintf(stderr, "I frame too long, ignoring ext header\n");
ae40484c 1013 }
2b9ab1d5 1014
ae40484c
MN
1015 return 0;
1016}
1017
cd4af68a 1018static inline void msmpeg4_memsetw(short *tab, int val, int n)
de6d9b64
FB
1019{
1020 int i;
1021 for(i=0;i<n;i++)
1022 tab[i] = val;
1023}
1024
3825cd1d
MN
1025static void msmpeg4v2_encode_motion(MpegEncContext * s, int val)
1026{
1027 int range, bit_size, sign, code, bits;
1028
1029 if (val == 0) {
1030 /* zero vector */
1031 code = 0;
1032 put_bits(&s->pb, mvtab[code][1], mvtab[code][0]);
1033 } else {
1034 bit_size = s->f_code - 1;
1035 range = 1 << bit_size;
1036 if (val <= -64)
1037 val += 64;
1038 else if (val >= 64)
1039 val -= 64;
1040
1041 if (val >= 0) {
1042 sign = 0;
1043 } else {
1044 val = -val;
1045 sign = 1;
1046 }
1047 val--;
1048 code = (val >> bit_size) + 1;
1049 bits = val & (range - 1);
1050
1051 put_bits(&s->pb, mvtab[code][1] + 1, (mvtab[code][0] << 1) | sign);
1052 if (bit_size > 0) {
1053 put_bits(&s->pb, bit_size, bits);
1054 }
1055 }
1056}
1057
84afee34
MN
1058/* this is identical to h263 except that its range is multiplied by 2 */
1059static int msmpeg4v2_decode_motion(MpegEncContext * s, int pred, int f_code)
1060{
1061 int code, val, sign, shift;
1062
1063 code = get_vlc(&s->gb, &v2_mv_vlc);
287229e5 1064// printf("MV code %d at %d %d pred: %d\n", code, s->mb_x,s->mb_y, pred);
84afee34
MN
1065 if (code < 0)
1066 return 0xffff;
1067
1068 if (code == 0)
1069 return pred;
1070 sign = get_bits1(&s->gb);
1071 shift = f_code - 1;
1072 val = (code - 1) << shift;
1073 if (shift > 0)
1074 val |= get_bits(&s->gb, shift);
1075 val++;
1076 if (sign)
1077 val = -val;
84afee34 1078
287229e5 1079 val += pred;
84afee34
MN
1080 if (val <= -64)
1081 val += 64;
1082 else if (val >= 64)
1083 val -= 64;
1084
1085 return val;
1086}
1087
1088
287229e5 1089static int msmpeg4v12_decode_mb(MpegEncContext *s,
84afee34
MN
1090 DCTELEM block[6][64])
1091{
1092 int cbp, code, i;
84afee34
MN
1093 if (s->pict_type == P_TYPE) {
1094 if (s->use_skip_mb_code) {
1095 if (get_bits1(&s->gb)) {
1096 /* skip mb */
1097 s->mb_intra = 0;
1098 for(i=0;i<6;i++)
1099 s->block_last_index[i] = -1;
1100 s->mv_dir = MV_DIR_FORWARD;
1101 s->mv_type = MV_TYPE_16X16;
1102 s->mv[0][0][0] = 0;
1103 s->mv[0][0][1] = 0;
1104 s->mb_skiped = 1;
1105 return 0;
1106 }
1107 }
1108
287229e5
MN
1109 if(s->msmpeg4_version==2)
1110 code = get_vlc(&s->gb, &v2_mb_type_vlc);
1111 else
1112 code = get_vlc(&s->gb, &v1_inter_cbpc_vlc);
1113 if(code<0 || code>7){
1114 fprintf(stderr, "cbpc %d invalid at %d %d\n", code, s->mb_x, s->mb_y);
1115 return -1;
1116 }
1117
84afee34
MN
1118 s->mb_intra = code >>2;
1119
1120 cbp = code & 0x3;
1121 } else {
1122 s->mb_intra = 1;
287229e5
MN
1123 if(s->msmpeg4_version==2)
1124 cbp= get_vlc(&s->gb, &v2_intra_cbpc_vlc);
1125 else
1126 cbp= get_vlc(&s->gb, &v1_intra_cbpc_vlc);
1127 if(cbp<0 || cbp>3){
1128 fprintf(stderr, "cbpc %d invalid at %d %d\n", cbp, s->mb_x, s->mb_y);
1129 return -1;
1130 }
84afee34
MN
1131 }
1132
1133 if (!s->mb_intra) {
287229e5
MN
1134 int mx, my, cbpy;
1135
1136 cbpy= get_vlc(&s->gb, &cbpy_vlc);
1137 if(cbpy<0){
1138 fprintf(stderr, "cbpy %d invalid at %d %d\n", cbp, s->mb_x, s->mb_y);
1139 return -1;
1140 }
84afee34 1141
287229e5
MN
1142 cbp|= cbpy<<2;
1143 if(s->msmpeg4_version==1 || (cbp&3) != 3) cbp^= 0x3C;
84afee34
MN
1144
1145 h263_pred_motion(s, 0, &mx, &my);
1146 mx= msmpeg4v2_decode_motion(s, mx, 1);
1147 my= msmpeg4v2_decode_motion(s, my, 1);
1148
1149 s->mv_dir = MV_DIR_FORWARD;
1150 s->mv_type = MV_TYPE_16X16;
1151 s->mv[0][0][0] = mx;
1152 s->mv[0][0][1] = my;
1153 } else {
287229e5
MN
1154 if(s->msmpeg4_version==2){
1155 s->ac_pred = get_bits1(&s->gb);
1156 cbp|= get_vlc(&s->gb, &cbpy_vlc)<<2; //FIXME check errors
1157 } else{
1158 s->ac_pred = 0;
1159 cbp|= get_vlc(&s->gb, &cbpy_vlc)<<2; //FIXME check errors
1160 if(s->pict_type==P_TYPE) cbp^=0x3C;
1161 }
84afee34
MN
1162 }
1163
1164 for (i = 0; i < 6; i++) {
1165 if (msmpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1) < 0)
1166 {
287229e5 1167 fprintf(stderr,"\nerror while decoding block: %d x %d (%d)\n", s->mb_x, s->mb_y, i);
84afee34
MN
1168 return -1;
1169 }
1170 }
1171 return 0;
1172}
1173
de6d9b64
FB
1174int msmpeg4_decode_mb(MpegEncContext *s,
1175 DCTELEM block[6][64])
1176{
1177 int cbp, code, i;
de6d9b64
FB
1178 UINT8 *coded_val;
1179
1180 /* special slice handling */
1181 if (s->mb_x == 0) {
525782f3 1182 if (s->slice_height && (s->mb_y % s->slice_height) == 0) {
de6d9b64
FB
1183 int wrap;
1184 /* reset DC pred (set previous line to 1024) */
1185 wrap = 2 * s->mb_width + 2;
cd4af68a
ZK
1186 msmpeg4_memsetw(&s->dc_val[0][(1) + (2 * s->mb_y) * wrap],
1187 1024, 2 * s->mb_width);
1188 wrap = s->mb_width + 2;
1189 msmpeg4_memsetw(&s->dc_val[1][(1) + (s->mb_y) * wrap],
1190 1024, s->mb_width);
1191 msmpeg4_memsetw(&s->dc_val[2][(1) + (s->mb_y) * wrap],
1192 1024, s->mb_width);
1193
1194 /* reset AC pred (set previous line to 0) */
1195 wrap = s->mb_width * 2 + 2;
1196 msmpeg4_memsetw(s->ac_val[0][0] + (1 + (2 * s->mb_y) * wrap)*16,
1197 0, 2 * s->mb_width*16);
1198 wrap = s->mb_width + 2;
1199 msmpeg4_memsetw(s->ac_val[1][0] + (1 + (s->mb_y) * wrap)*16,
1200 0, s->mb_width*16);
1201 msmpeg4_memsetw(s->ac_val[2][0] + (1 + (s->mb_y) * wrap)*16,
1202 0, s->mb_width*16);
de6d9b64
FB
1203
1204 s->first_slice_line = 1;
1205 } else {
1206 s->first_slice_line = 0;
1207 }
1208 }
1209
287229e5 1210 if(s->msmpeg4_version<=2) return msmpeg4v12_decode_mb(s, block); //FIXME export function & call from outside perhaps
84afee34 1211
de6d9b64
FB
1212 if (s->pict_type == P_TYPE) {
1213 set_stat(ST_INTER_MB);
1214 if (s->use_skip_mb_code) {
612476ef 1215 if (get_bits1(&s->gb)) {
de6d9b64
FB
1216 /* skip mb */
1217 s->mb_intra = 0;
1218 for(i=0;i<6;i++)
1219 s->block_last_index[i] = -1;
1220 s->mv_dir = MV_DIR_FORWARD;
1221 s->mv_type = MV_TYPE_16X16;
1222 s->mv[0][0][0] = 0;
1223 s->mv[0][0][1] = 0;
3bb4e23a 1224 s->mb_skiped = 1;
de6d9b64
FB
1225 return 0;
1226 }
1227 }
1228
1229 code = get_vlc(&s->gb, &mb_non_intra_vlc);
1230 if (code < 0)
1231 return -1;
38d171eb
ZK
1232 //s->mb_intra = (code & 0x40) ? 0 : 1;
1233 s->mb_intra = (~code & 0x40) >> 6;
de6d9b64
FB
1234
1235 cbp = code & 0x3f;
1236 } else {
1237 set_stat(ST_INTRA_MB);
1238 s->mb_intra = 1;
1239 code = get_vlc(&s->gb, &mb_intra_vlc);
1240 if (code < 0)
1241 return -1;
1242 /* predict coded block pattern */
1243 cbp = 0;
1244 for(i=0;i<6;i++) {
38d171eb 1245 int val = ((code >> (5 - i)) & 1);
de6d9b64 1246 if (i < 4) {
38d171eb 1247 int pred = coded_block_pred(s, i, &coded_val);
de6d9b64
FB
1248 val = val ^ pred;
1249 *coded_val = val;
1250 }
1251 cbp |= val << (5 - i);
1252 }
1253 }
1254
1255 if (!s->mb_intra) {
1256 int mx, my;
1257 set_stat(ST_MV);
1258 h263_pred_motion(s, 0, &mx, &my);
1259 if (msmpeg4_decode_motion(s, &mx, &my) < 0)
1260 return -1;
1261 s->mv_dir = MV_DIR_FORWARD;
1262 s->mv_type = MV_TYPE_16X16;
1263 s->mv[0][0][0] = mx;
1264 s->mv[0][0][1] = my;
1265 } else {
1266 set_stat(ST_INTRA_MB);
612476ef 1267 s->ac_pred = get_bits1(&s->gb);
de6d9b64
FB
1268 }
1269
1270 for (i = 0; i < 6; i++) {
1271 if (msmpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1) < 0)
38d171eb 1272 {
287229e5
MN
1273 fprintf(stderr,"\nerror while decoding block: %d x %d (%d)\n", s->mb_x, s->mb_y, i);
1274 return -1;
38d171eb 1275 }
de6d9b64 1276 }
287229e5 1277
de6d9b64
FB
1278 return 0;
1279}
1280
1281static int msmpeg4_decode_block(MpegEncContext * s, DCTELEM * block,
1282 int n, int coded)
1283{
1284 int code, level, i, j, last, run, run_diff;
1285 int dc_pred_dir;
1286 RLTable *rl;
1287 const UINT8 *scan_table;
badaf88e 1288 int qmul, qadd;
de6d9b64
FB
1289
1290 if (s->mb_intra) {
badaf88e
MN
1291 qmul=1;
1292 qadd=0;
1293
de6d9b64
FB
1294 /* DC coef */
1295 set_stat(ST_DC);
1296 level = msmpeg4_decode_dc(s, n, &dc_pred_dir);
287229e5
MN
1297 if (level < 0){
1298 fprintf(stderr, "dc overflow-\n");
de6d9b64 1299 return -1;
287229e5 1300 }
de6d9b64
FB
1301 if (n < 4) {
1302 rl = &rl_table[s->rl_table_index];
287229e5
MN
1303 if(level > 256*s->y_dc_scale){
1304 fprintf(stderr, "dc overflow+\n");
1305 return -1;
1306 }
de6d9b64
FB
1307 } else {
1308 rl = &rl_table[3 + s->rl_chroma_table_index];
287229e5
MN
1309 if(level > 256*s->c_dc_scale){
1310 fprintf(stderr, "dc overflow+\n");
1311 return -1;
1312 }
de6d9b64 1313 }
287229e5 1314 block[0] = level;
badaf88e 1315
de6d9b64
FB
1316 run_diff = 0;
1317 i = 1;
1318 if (!coded) {
1319 goto not_coded;
1320 }
1321 if (s->ac_pred) {
1322 if (dc_pred_dir == 0)
1323 scan_table = ff_alternate_vertical_scan; /* left */
1324 else
1325 scan_table = ff_alternate_horizontal_scan; /* top */
1326 } else {
1327 scan_table = zigzag_direct;
1328 }
1329 set_stat(ST_INTRA_AC);
1330 } else {
badaf88e
MN
1331 qmul = s->qscale << 1;
1332 qadd = (s->qscale - 1) | 1;
de6d9b64
FB
1333 i = 0;
1334 rl = &rl_table[3 + s->rl_table_index];
84afee34
MN
1335
1336 if(s->msmpeg4_version==2)
1337 run_diff = 0;
1338 else
1339 run_diff = 1;
1340
de6d9b64
FB
1341 if (!coded) {
1342 s->block_last_index[n] = i - 1;
1343 return 0;
1344 }
1345 scan_table = zigzag_direct;
1346 set_stat(ST_INTER_AC);
1347 }
1348
1349 for(;;) {
1350 code = get_vlc(&s->gb, &rl->vlc);
1351 if (code < 0)
1352 return -1;
1353 if (code == rl->n) {
1354 /* escape */
287229e5
MN
1355 if (s->msmpeg4_version==1 || get_bits1(&s->gb) == 0) {
1356 if (s->msmpeg4_version==1 || get_bits1(&s->gb) == 0) {
de6d9b64 1357 /* third escape */
612476ef 1358 last = get_bits1(&s->gb);
de6d9b64
FB
1359 run = get_bits(&s->gb, 6);
1360 level = get_bits(&s->gb, 8);
1361 level = (level << 24) >> 24; /* sign extend */
287229e5
MN
1362#if 0 // waste of time / this will detect very few errors
1363 {
1364 const int abs_level= ABS(level);
1365 const int run1= run - rl->max_run[last][abs_level] - run_diff;
1366 if(abs_level<=MAX_LEVEL && run<=MAX_RUN){
1367 if(abs_level <= rl->max_level[last][run]){
1368 fprintf(stderr, "illegal 3. esc, vlc encoding possible\n");
1369 return DECODING_AC_LOST;
1370 }
1371 if(abs_level <= rl->max_level[last][run]*2){
1372 fprintf(stderr, "illegal 3. esc, esc 1 encoding possible\n");
1373 return DECODING_AC_LOST;
1374 }
1375 if(abs_level <= rl->max_level[last][run1] && 0){
1376 fprintf(stderr, "illegal 3. esc, esc 2 encoding possible\n");
1377 return DECODING_AC_LOST;
1378 }
1379 }
1380 }
1381#endif
38d171eb
ZK
1382 //level = level * qmul + (level>0) * qadd - (level<=0) * qadd ;
1383 if (level>0) level= level * qmul + qadd;
287229e5
MN
1384 else level= level * qmul - qadd;
1385#if 0 // waste of time too :(
1386 if(level>2048 || level<-2048){
1387 fprintf(stderr, "|level| overflow in 3. esc\n");
1388 return DECODING_AC_LOST;
1389 }
1390#endif
de6d9b64
FB
1391 } else {
1392 /* second escape */
1393 code = get_vlc(&s->gb, &rl->vlc);
1394 if (code < 0 || code >= rl->n)
1395 return -1;
1396 run = rl->table_run[code];
99180fe3 1397 level = rl->table_level[code];
de6d9b64
FB
1398 last = code >= rl->last;
1399 run += rl->max_run[last][level] + run_diff;
99180fe3 1400 level= level * qmul + qadd;
612476ef 1401 if (get_bits1(&s->gb))
de6d9b64
FB
1402 level = -level;
1403 }
1404 } else {
1405 /* first escape */
1406 code = get_vlc(&s->gb, &rl->vlc);
1407 if (code < 0 || code >= rl->n)
1408 return -1;
1409 run = rl->table_run[code];
1410 level = rl->table_level[code];
1411 last = code >= rl->last;
1412 level += rl->max_level[last][run];
badaf88e 1413 level= level * qmul + qadd;
612476ef 1414 if (get_bits1(&s->gb))
de6d9b64
FB
1415 level = -level;
1416 }
1417 } else {
1418 run = rl->table_run[code];
badaf88e 1419 level = rl->table_level[code] * qmul + qadd;
de6d9b64 1420 last = code >= rl->last;
612476ef 1421 if (get_bits1(&s->gb))
de6d9b64
FB
1422 level = -level;
1423 }
1424 i += run;
1425 if (i >= 64)
1426 return -1;
287229e5 1427
de6d9b64
FB
1428 j = scan_table[i];
1429 block[j] = level;
1430 i++;
1431 if (last)
1432 break;
1433 }
1434 not_coded:
1435 if (s->mb_intra) {
1436 mpeg4_pred_ac(s, block, n, dc_pred_dir);
1437 if (s->ac_pred) {
1438 i = 64; /* XXX: not optimal */
1439 }
1440 }
1441 s->block_last_index[n] = i - 1;
1442
1443 return 0;
1444}
1445
1446static int msmpeg4_decode_dc(MpegEncContext * s, int n, int *dir_ptr)
1447{
1448 int level, pred;
de6d9b64 1449
287229e5 1450 if(s->msmpeg4_version<=2){
84afee34
MN
1451 if (n < 4) {
1452 level = get_vlc(&s->gb, &v2_dc_lum_vlc);
1453 } else {
1454 level = get_vlc(&s->gb, &v2_dc_chroma_vlc);
1455 }
2ed627e6 1456 if (level < 0)
84afee34 1457 return -1;
84afee34
MN
1458 level-=256;
1459 }else{ //FIXME optimize use unified tables & index
1460 if (n < 4) {
1461 level = get_vlc(&s->gb, &dc_lum_vlc[s->dc_table_index]);
1462 } else {
1463 level = get_vlc(&s->gb, &dc_chroma_vlc[s->dc_table_index]);
1464 }
287229e5
MN
1465 if (level < 0){
1466 fprintf(stderr, "illegal dc vlc\n");
84afee34 1467 return -1;
287229e5 1468 }
84afee34
MN
1469
1470 if (level == DC_MAX) {
1471 level = get_bits(&s->gb, 8);
1472 if (get_bits1(&s->gb))
1473 level = -level;
1474 } else if (level != 0) {
1475 if (get_bits1(&s->gb))
1476 level = -level;
1477 }
de6d9b64
FB
1478 }
1479
287229e5
MN
1480 if(s->msmpeg4_version==1){
1481 INT32 *dc_val;
1482 pred = msmpeg4v1_pred_dc(s, n, &dc_val);
1483 level += pred;
1484
1485 /* update predictor */
1486 *dc_val= level;
1487 }else{
1488 INT16 *dc_val;
1489 pred = msmpeg4_pred_dc(s, n, &dc_val, dir_ptr);
1490 level += pred;
de6d9b64 1491
287229e5
MN
1492 /* update predictor */
1493 if (n < 4) {
1494 *dc_val = level * s->y_dc_scale;
1495 } else {
1496 *dc_val = level * s->c_dc_scale;
1497 }
de6d9b64
FB
1498 }
1499
1500 return level;
1501}
1502
1503static int msmpeg4_decode_motion(MpegEncContext * s,
1504 int *mx_ptr, int *my_ptr)
1505{
1506 MVTable *mv;
1507 int code, mx, my;
1508
1509 mv = &mv_tables[s->mv_table_index];
1510
1511 code = get_vlc(&s->gb, &mv->vlc);
1512 if (code < 0)
1513 return -1;
1514 if (code == mv->n) {
1515 mx = get_bits(&s->gb, 6);
1516 my = get_bits(&s->gb, 6);
1517 } else {
1518 mx = mv->table_mvx[code];
1519 my = mv->table_mvy[code];
1520 }
1521
1522 mx += *mx_ptr - 32;
1523 my += *my_ptr - 32;
1524 /* WARNING : they do not do exactly modulo encoding */
1525 if (mx <= -64)
1526 mx += 64;
1527 else if (mx >= 64)
1528 mx -= 64;
1529
1530 if (my <= -64)
1531 my += 64;
1532 else if (my >= 64)
1533 my -= 64;
1534 *mx_ptr = mx;
1535 *my_ptr = my;
1536 return 0;
1537}