single coefficient elimination
[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
MN
18 *
19 * msmpeg4v2 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
MN
55static void msmpeg4v2_encode_motion(MpegEncContext * s, int val);
56static void init_h263_dc_for_msmpeg4();
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
162/* write MSMPEG4 V3 compatible frame header */
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;
3825cd1d
MN
174 if(s->msmpeg4_version==2)
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
3825cd1d
MN
186 if(s->msmpeg4_version!=2){
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;
3825cd1d
MN
197 if(s->msmpeg4_version!=2){
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{
ae40484c 231 s->flipflop_rounding=1;
2b9ab1d5 232 s->bitrate= 910; // FIXME
ae40484c 233
2b9ab1d5 234 put_bits(&s->pb, 5, s->frame_rate / FRAME_RATE_BASE); //yes 29.97 -> 29
ae40484c
MN
235
236 put_bits(&s->pb, 11, s->bitrate);
237
238 put_bits(&s->pb, 1, s->flipflop_rounding);
ae40484c
MN
239}
240
de6d9b64
FB
241/* predict coded block */
242static inline int coded_block_pred(MpegEncContext * s, int n, UINT8 **coded_block_ptr)
243{
dbbe8999 244 int xy, wrap, pred, a, b, c;
de6d9b64 245
dbbe8999
MN
246 xy = s->block_index[n];
247 wrap = s->block_wrap[0];
de6d9b64
FB
248
249 /* B C
250 * A X
251 */
dbbe8999
MN
252 a = s->coded_block[xy - 1 ];
253 b = s->coded_block[xy - 1 - wrap];
254 c = s->coded_block[xy - wrap];
de6d9b64
FB
255
256 if (b == c) {
257 pred = a;
258 } else {
259 pred = c;
260 }
261
262 /* store value */
dbbe8999 263 *coded_block_ptr = &s->coded_block[xy];
de6d9b64
FB
264
265 return pred;
266}
267
268static void msmpeg4_encode_motion(MpegEncContext * s,
269 int mx, int my)
270{
271 int code;
272 MVTable *mv;
273
274 /* modulo encoding */
275 /* WARNING : you cannot reach all the MVs even with the modulo
276 encoding. This is a somewhat strange compromise they took !!! */
277 if (mx <= -64)
278 mx += 64;
279 else if (mx >= 64)
280 mx -= 64;
281 if (my <= -64)
282 my += 64;
283 else if (my >= 64)
284 my -= 64;
285
286 mx += 32;
287 my += 32;
288#if 0
289 if ((unsigned)mx >= 64 ||
290 (unsigned)my >= 64)
291 fprintf(stderr, "error mx=%d my=%d\n", mx, my);
292#endif
293 mv = &mv_tables[s->mv_table_index];
294
295 code = mv->table_mv_index[(mx << 6) | my];
296 set_stat(ST_MV);
297 put_bits(&s->pb,
298 mv->table_mv_bits[code],
299 mv->table_mv_code[code]);
300 if (code == mv->n) {
301 /* escape : code litterally */
302 put_bits(&s->pb, 6, mx);
303 put_bits(&s->pb, 6, my);
304 }
305}
306
307void msmpeg4_encode_mb(MpegEncContext * s,
308 DCTELEM block[6][64],
309 int motion_x, int motion_y)
310{
311 int cbp, coded_cbp, i;
312 int pred_x, pred_y;
313 UINT8 *coded_block;
314
315 if (!s->mb_intra) {
316 /* compute cbp */
317 set_stat(ST_INTER_MB);
318 cbp = 0;
319 for (i = 0; i < 6; i++) {
320 if (s->block_last_index[i] >= 0)
321 cbp |= 1 << (5 - i);
322 }
323 if (s->use_skip_mb_code && (cbp | motion_x | motion_y) == 0) {
324 /* skip macroblock */
325 put_bits(&s->pb, 1, 1);
326 return;
327 }
328 if (s->use_skip_mb_code)
329 put_bits(&s->pb, 1, 0); /* mb coded */
330
3825cd1d
MN
331 if(s->msmpeg4_version==2){
332 put_bits(&s->pb,
333 v2_mb_type[cbp&3][1],
334 v2_mb_type[cbp&3][0]);
335 if((cbp&3) != 3) coded_cbp= cbp ^ 0x3C;
336 else coded_cbp= cbp;
337
338 put_bits(&s->pb,
339 cbpy_tab[coded_cbp>>2][1],
340 cbpy_tab[coded_cbp>>2][0]);
341
342 h263_pred_motion(s, 0, &pred_x, &pred_y);
343 msmpeg4v2_encode_motion(s, motion_x - pred_x);
344 msmpeg4v2_encode_motion(s, motion_y - pred_y);
345 }else{
346 put_bits(&s->pb,
347 table_mb_non_intra[cbp + 64][1],
348 table_mb_non_intra[cbp + 64][0]);
349
350 /* motion vector */
351 h263_pred_motion(s, 0, &pred_x, &pred_y);
352 msmpeg4_encode_motion(s, motion_x - pred_x,
353 motion_y - pred_y);
354 }
de6d9b64
FB
355 } else {
356 /* compute cbp */
357 cbp = 0;
358 coded_cbp = 0;
359 for (i = 0; i < 6; i++) {
360 int val, pred;
361 val = (s->block_last_index[i] >= 1);
362 cbp |= val << (5 - i);
363 if (i < 4) {
364 /* predict value for close blocks only for luma */
365 pred = coded_block_pred(s, i, &coded_block);
366 *coded_block = val;
367 val = val ^ pred;
368 }
369 coded_cbp |= val << (5 - i);
370 }
371#if 0
372 if (coded_cbp)
373 printf("cbp=%x %x\n", cbp, coded_cbp);
374#endif
375
3825cd1d
MN
376 if(s->msmpeg4_version==2){
377 if (s->pict_type == I_TYPE) {
378 put_bits(&s->pb,
379 v2_intra_cbpc[cbp&3][1], v2_intra_cbpc[cbp&3][0]);
380 } else {
381 if (s->use_skip_mb_code)
382 put_bits(&s->pb, 1, 0); /* mb coded */
383 put_bits(&s->pb,
384 v2_mb_type[(cbp&3) + 4][1],
385 v2_mb_type[(cbp&3) + 4][0]);
386 }
387 put_bits(&s->pb, 1, 0); /* no AC prediction yet */
de6d9b64 388 put_bits(&s->pb,
3825cd1d
MN
389 cbpy_tab[cbp>>2][1],
390 cbpy_tab[cbp>>2][0]);
391 }else{
392 if (s->pict_type == I_TYPE) {
393 set_stat(ST_INTRA_MB);
394 put_bits(&s->pb,
395 table_mb_intra[coded_cbp][1], table_mb_intra[coded_cbp][0]);
396 } else {
397 if (s->use_skip_mb_code)
398 put_bits(&s->pb, 1, 0); /* mb coded */
399 put_bits(&s->pb,
400 table_mb_non_intra[cbp][1],
401 table_mb_non_intra[cbp][0]);
402 }
403 set_stat(ST_INTRA_MB);
404 put_bits(&s->pb, 1, 0); /* no AC prediction yet */
de6d9b64 405 }
de6d9b64
FB
406 }
407
408 for (i = 0; i < 6; i++) {
409 msmpeg4_encode_block(s, block[i], i);
410 }
411}
412
92ba5ffb
MN
413/* old ffmpeg msmpeg4v3 mode */
414void ff_old_msmpeg4_dc_scale(MpegEncContext * s)
de6d9b64 415{
92ba5ffb 416 if (s->qscale < 5){
6fb904ce
MN
417 s->y_dc_scale = 8;
418 s->c_dc_scale = 8;
6fb904ce
MN
419 }else if (s->qscale < 9){
420 s->y_dc_scale = 2 * s->qscale;
421 s->c_dc_scale = (s->qscale + 13)>>1;
92ba5ffb 422 }else{
6fb904ce
MN
423 s->y_dc_scale = s->qscale + 8;
424 s->c_dc_scale = (s->qscale + 13)>>1;
425 }
de6d9b64
FB
426}
427
428/* dir = 0: left, dir = 1: top prediction */
429static int msmpeg4_pred_dc(MpegEncContext * s, int n,
98be975d 430 INT16 **dc_val_ptr, int *dir_ptr)
de6d9b64 431{
dbbe8999 432 int a, b, c, wrap, pred, scale;
98be975d 433 INT16 *dc_val;
de6d9b64
FB
434
435 /* find prediction */
436 if (n < 4) {
de6d9b64
FB
437 scale = s->y_dc_scale;
438 } else {
de6d9b64
FB
439 scale = s->c_dc_scale;
440 }
dbbe8999
MN
441 wrap = s->block_wrap[n];
442 dc_val= s->dc_val[0] + s->block_index[n];
de6d9b64
FB
443
444 /* B C
445 * A X
446 */
dbbe8999
MN
447 a = dc_val[ - 1];
448 b = dc_val[ - 1 - wrap];
449 c = dc_val[ - wrap];
de6d9b64
FB
450
451 /* XXX: the following solution consumes divisions, but it does not
452 necessitate to modify mpegvideo.c. The problem comes from the
453 fact they decided to store the quantized DC (which would lead
454 to problems if Q could vary !) */
320680d4 455#if defined ARCH_X86 && !defined PIC
6f903d8e
MN
456 asm volatile(
457 "movl %3, %%eax \n\t"
458 "shrl $1, %%eax \n\t"
459 "addl %%eax, %2 \n\t"
460 "addl %%eax, %1 \n\t"
461 "addl %0, %%eax \n\t"
6fe84b43
MN
462 "mull %4 \n\t"
463 "movl %%edx, %0 \n\t"
6f903d8e 464 "movl %1, %%eax \n\t"
6fe84b43
MN
465 "mull %4 \n\t"
466 "movl %%edx, %1 \n\t"
6f903d8e 467 "movl %2, %%eax \n\t"
6fe84b43
MN
468 "mull %4 \n\t"
469 "movl %%edx, %2 \n\t"
fa778d53
MN
470 : "+b" (a), "+c" (b), "+D" (c)
471 : "g" (scale), "S" (inverse[scale])
6f903d8e
MN
472 : "%eax", "%edx"
473 );
320680d4
ZK
474#else
475 /* #elif defined (ARCH_ALPHA) */
1e98dffb 476 /* Divisions are extremely costly on Alpha; optimize the most
320680d4
ZK
477 common case. But they are costly everywhere...
478 */
1e98dffb
NK
479 if (scale == 8) {
480 a = (a + (8 >> 1)) / 8;
481 b = (b + (8 >> 1)) / 8;
482 c = (c + (8 >> 1)) / 8;
483 } else {
484 a = (a + (scale >> 1)) / scale;
485 b = (b + (scale >> 1)) / scale;
486 c = (c + (scale >> 1)) / scale;
487 }
6f903d8e 488#endif
de6d9b64
FB
489 /* XXX: WARNING: they did not choose the same test as MPEG4. This
490 is very important ! */
491 if (abs(a - b) <= abs(b - c)) {
492 pred = c;
493 *dir_ptr = 1;
494 } else {
495 pred = a;
496 *dir_ptr = 0;
497 }
498
499 /* update predictor */
dbbe8999 500 *dc_val_ptr = &dc_val[0];
de6d9b64
FB
501 return pred;
502}
503
504#define DC_MAX 119
505
506static void msmpeg4_encode_dc(MpegEncContext * s, int level, int n, int *dir_ptr)
507{
508 int sign, code;
509 int pred;
98be975d 510 INT16 *dc_val;
de6d9b64
FB
511
512 pred = msmpeg4_pred_dc(s, n, &dc_val, dir_ptr);
513
514 /* update predictor */
515 if (n < 4) {
516 *dc_val = level * s->y_dc_scale;
517 } else {
518 *dc_val = level * s->c_dc_scale;
519 }
520
521 /* do the prediction */
522 level -= pred;
523
3825cd1d 524 if(s->msmpeg4_version==2){
de6d9b64 525 if (n < 4) {
3825cd1d
MN
526 put_bits(&s->pb,
527 v2_dc_lum_table[level+256][1],
528 v2_dc_lum_table[level+256][0]);
529 }else{
530 put_bits(&s->pb,
531 v2_dc_chroma_table[level+256][1],
532 v2_dc_chroma_table[level+256][0]);
de6d9b64 533 }
3825cd1d
MN
534 }else{
535 sign = 0;
536 if (level < 0) {
537 level = -level;
538 sign = 1;
539 }
540 code = level;
541 if (code > DC_MAX)
542 code = DC_MAX;
543
544 if (s->dc_table_index == 0) {
545 if (n < 4) {
546 put_bits(&s->pb, table0_dc_lum[code][1], table0_dc_lum[code][0]);
547 } else {
548 put_bits(&s->pb, table0_dc_chroma[code][1], table0_dc_chroma[code][0]);
549 }
de6d9b64 550 } else {
3825cd1d
MN
551 if (n < 4) {
552 put_bits(&s->pb, table1_dc_lum[code][1], table1_dc_lum[code][0]);
553 } else {
554 put_bits(&s->pb, table1_dc_chroma[code][1], table1_dc_chroma[code][0]);
555 }
556 }
557
558 if (code == DC_MAX)
559 put_bits(&s->pb, 8, level);
560
561 if (level != 0) {
562 put_bits(&s->pb, 1, sign);
de6d9b64 563 }
de6d9b64
FB
564 }
565}
566
567/* Encoding of a block. Very similar to MPEG4 except for a different
568 escape coding (same as H263) and more vlc tables.
569 */
570static void msmpeg4_encode_block(MpegEncContext * s, DCTELEM * block, int n)
571{
572 int level, run, last, i, j, last_index;
573 int last_non_zero, sign, slevel;
574 int code, run_diff, dc_pred_dir;
575 const RLTable *rl;
576
577 if (s->mb_intra) {
578 set_stat(ST_DC);
579 msmpeg4_encode_dc(s, block[0], n, &dc_pred_dir);
580 i = 1;
581 if (n < 4) {
582 rl = &rl_table[s->rl_table_index];
583 } else {
584 rl = &rl_table[3 + s->rl_chroma_table_index];
585 }
586 run_diff = 0;
587 set_stat(ST_INTRA_AC);
588 } else {
589 i = 0;
590 rl = &rl_table[3 + s->rl_table_index];
3825cd1d
MN
591 if(s->msmpeg4_version==2)
592 run_diff = 0;
593 else
594 run_diff = 1;
de6d9b64
FB
595 set_stat(ST_INTER_AC);
596 }
597
598 /* AC coefs */
599 last_index = s->block_last_index[n];
600 last_non_zero = i - 1;
601 for (; i <= last_index; i++) {
602 j = zigzag_direct[i];
603 level = block[j];
604 if (level) {
605 run = i - last_non_zero - 1;
606 last = (i == last_index);
607 sign = 0;
608 slevel = level;
609 if (level < 0) {
610 sign = 1;
611 level = -level;
612 }
613 code = get_rl_index(rl, last, run, level);
614 put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
615 if (code == rl->n) {
616 int level1, run1;
617
618 level1 = level - rl->max_level[last][run];
619 if (level1 < 1)
620 goto esc2;
621 code = get_rl_index(rl, last, run, level1);
622 if (code == rl->n) {
623 esc2:
624 put_bits(&s->pb, 1, 0);
625 if (level > MAX_LEVEL)
626 goto esc3;
627 run1 = run - rl->max_run[last][level] - run_diff;
628 if (run1 < 0)
629 goto esc3;
630 code = get_rl_index(rl, last, run1, level);
631 if (code == rl->n) {
632 esc3:
633 /* third escape */
634 put_bits(&s->pb, 1, 0);
635 put_bits(&s->pb, 1, last);
636 put_bits(&s->pb, 6, run);
637 put_bits(&s->pb, 8, slevel & 0xff);
638 } else {
639 /* second escape */
640 put_bits(&s->pb, 1, 1);
641 put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
642 put_bits(&s->pb, 1, sign);
643 }
644 } else {
645 /* first escape */
646 put_bits(&s->pb, 1, 1);
647 put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
648 put_bits(&s->pb, 1, sign);
649 }
650 } else {
651 put_bits(&s->pb, 1, sign);
652 }
653 last_non_zero = i;
654 }
655 }
656}
657
658/****************************************/
659/* decoding stuff */
660
661static VLC mb_non_intra_vlc;
662static VLC mb_intra_vlc;
663static VLC dc_lum_vlc[2];
664static VLC dc_chroma_vlc[2];
84afee34
MN
665static VLC v2_dc_lum_vlc;
666static VLC v2_dc_chroma_vlc;
667static VLC cbpy_vlc;
668static VLC v2_intra_cbpc_vlc;
669static VLC v2_mb_type_vlc;
670static VLC v2_mv_vlc;
671
672/* this table is practically identical to the one from h263 except that its inverted */
673static void init_h263_dc_for_msmpeg4()
674{
675 static int inited=0;
676
677 if(!inited){
678 int level, uni_code, uni_len;
679 inited=1;
680
2ed627e6 681 for(level=-256; level<256; level++){
84afee34
MN
682 int size, v, l;
683 /* find number of bits */
684 size = 0;
685 v = abs(level);
686 while (v) {
687 v >>= 1;
688 size++;
689 }
690
691 if (level < 0)
692 l= (-level) ^ ((1 << size) - 1);
693 else
694 l= level;
695
696 /* luminance h263 */
697 uni_code= DCtab_lum[size][0];
698 uni_len = DCtab_lum[size][1];
699 uni_code ^= (1<<uni_len)-1; //M$ doesnt like compatibility
700
701 if (size > 0) {
702 uni_code<<=size; uni_code|=l;
703 uni_len+=size;
704 if (size > 8){
705 uni_code<<=1; uni_code|=1;
706 uni_len++;
707 }
708 }
709 v2_dc_lum_table[level+256][0]= uni_code;
710 v2_dc_lum_table[level+256][1]= uni_len;
711
712 /* chrominance h263 */
713 uni_code= DCtab_chrom[size][0];
714 uni_len = DCtab_chrom[size][1];
715 uni_code ^= (1<<uni_len)-1; //M$ doesnt like compatibility
716
717 if (size > 0) {
718 uni_code<<=size; uni_code|=l;
719 uni_len+=size;
720 if (size > 8){
721 uni_code<<=1; uni_code|=1;
722 uni_len++;
723 }
724 }
725 v2_dc_chroma_table[level+256][0]= uni_code;
726 v2_dc_chroma_table[level+256][1]= uni_len;
727
728 }
729 }
730}
de6d9b64
FB
731
732/* init all vlc decoding tables */
733int msmpeg4_decode_init_vlc(MpegEncContext *s)
734{
735 int i;
736 MVTable *mv;
737
738 for(i=0;i<NB_RL_TABLES;i++) {
739 init_rl(&rl_table[i]);
740 init_vlc_rl(&rl_table[i]);
741 }
742 for(i=0;i<2;i++) {
743 mv = &mv_tables[i];
744 init_vlc(&mv->vlc, 9, mv->n + 1,
745 mv->table_mv_bits, 1, 1,
746 mv->table_mv_code, 2, 2);
747 }
748
749 init_vlc(&dc_lum_vlc[0], 9, 120,
750 &table0_dc_lum[0][1], 8, 4,
751 &table0_dc_lum[0][0], 8, 4);
752 init_vlc(&dc_chroma_vlc[0], 9, 120,
753 &table0_dc_chroma[0][1], 8, 4,
754 &table0_dc_chroma[0][0], 8, 4);
755 init_vlc(&dc_lum_vlc[1], 9, 120,
756 &table1_dc_lum[0][1], 8, 4,
757 &table1_dc_lum[0][0], 8, 4);
758 init_vlc(&dc_chroma_vlc[1], 9, 120,
759 &table1_dc_chroma[0][1], 8, 4,
760 &table1_dc_chroma[0][0], 8, 4);
84afee34
MN
761
762 init_h263_dc_for_msmpeg4();
763 init_vlc(&v2_dc_lum_vlc, 9, 512,
764 &v2_dc_lum_table[0][1], 8, 4,
765 &v2_dc_lum_table[0][0], 8, 4);
766 init_vlc(&v2_dc_chroma_vlc, 9, 512,
767 &v2_dc_chroma_table[0][1], 8, 4,
768 &v2_dc_chroma_table[0][0], 8, 4);
769
770 init_vlc(&cbpy_vlc, 6, 16,
771 &cbpy_tab[0][1], 2, 1,
772 &cbpy_tab[0][0], 2, 1);
773 init_vlc(&v2_intra_cbpc_vlc, 3, 4,
774 &v2_intra_cbpc[0][1], 2, 1,
775 &v2_intra_cbpc[0][0], 2, 1);
776 init_vlc(&v2_mb_type_vlc, 5, 8,
777 &v2_mb_type[0][1], 2, 1,
778 &v2_mb_type[0][0], 2, 1);
779 init_vlc(&v2_mv_vlc, 9, 33,
780 &mvtab[0][1], 2, 1,
781 &mvtab[0][0], 2, 1);
de6d9b64
FB
782
783 init_vlc(&mb_non_intra_vlc, 9, 128,
784 &table_mb_non_intra[0][1], 8, 4,
785 &table_mb_non_intra[0][0], 8, 4);
2cb1773d 786 init_vlc(&mb_intra_vlc, 9, 64,
de6d9b64
FB
787 &table_mb_intra[0][1], 4, 2,
788 &table_mb_intra[0][0], 4, 2);
789 return 0;
790}
791
792static int decode012(GetBitContext *gb)
793{
794 int n;
612476ef 795 n = get_bits1(gb);
de6d9b64
FB
796 if (n == 0)
797 return 0;
798 else
612476ef 799 return get_bits1(gb) + 1;
de6d9b64
FB
800}
801
84afee34 802int msmpeg4_decode_picture_header(MpegEncContext * s)
de6d9b64 803{
e1a9dbff 804 int code, code2;
de6d9b64 805
e1a9dbff
MN
806#if 0
807{
808int i;
809for(i=0; i<s->gb.size*8; i++)
810 printf("%d", get_bits1(&s->gb));
811// get_bits1(&s->gb);
812printf("END\n");
813return -1;
814}
815#endif
de6d9b64
FB
816 s->pict_type = get_bits(&s->gb, 2) + 1;
817 if (s->pict_type != I_TYPE &&
818 s->pict_type != P_TYPE)
819 return -1;
820
821 s->qscale = get_bits(&s->gb, 5);
822
823 if (s->pict_type == I_TYPE) {
824 code = get_bits(&s->gb, 5);
84afee34 825 /* 0x17: one slice, 0x18: two slices */
7f89b6fb
MN
826 if (code < 0x17)
827 return -1;
828 s->slice_height = s->mb_height / (code - 0x16);
e1a9dbff
MN
829
830 switch(s->msmpeg4_version){
831 case 2:
84afee34
MN
832 s->rl_chroma_table_index = 2;
833 s->rl_table_index = 2;
7f89b6fb 834
84afee34 835 s->dc_table_index = 0; //not used
e1a9dbff
MN
836 break;
837 case 3:
84afee34
MN
838 s->rl_chroma_table_index = decode012(&s->gb);
839 s->rl_table_index = decode012(&s->gb);
7f89b6fb 840
84afee34 841 s->dc_table_index = get_bits1(&s->gb);
e1a9dbff
MN
842 break;
843 case 4:
844 msmpeg4_decode_ext_header(s, 999 /* bufer size (useless here) */);
845 printf("%X\n", show_bits(&s->gb, 24));
846 code= get_bits(&s->gb, 2);
847 if(code==1){
848 code2= get_bits(&s->gb, 3);
849 if(code2==7) skip_bits(&s->gb, 1);
850 }
851 printf("%X\n", show_bits(&s->gb, 24));
852 s->rl_chroma_table_index = 2;
853 s->rl_table_index = 2;
854
855 s->dc_table_index = 0;
856 break;
84afee34 857 }
de6d9b64 858 s->no_rounding = 1;
badaf88e
MN
859/* printf(" %d %d %d %d \n",
860 s->qscale,
861 s->rl_chroma_table_index,
862 s->rl_table_index,
863 s->dc_table_index);*/
de6d9b64 864 } else {
612476ef 865 s->use_skip_mb_code = get_bits1(&s->gb);
de6d9b64 866
84afee34
MN
867 if(s->msmpeg4_version==2){
868 s->rl_table_index = 2;
869 s->rl_chroma_table_index = s->rl_table_index;
870
871 s->dc_table_index = 0; //not used
872
873 s->mv_table_index = 0;
874 }else{
875 s->rl_table_index = decode012(&s->gb);
876 s->rl_chroma_table_index = s->rl_table_index;
de6d9b64 877
84afee34 878 s->dc_table_index = get_bits1(&s->gb);
de6d9b64 879
84afee34
MN
880 s->mv_table_index = get_bits1(&s->gb);
881 }
badaf88e
MN
882/* printf(" %d %d %d %d %d \n",
883 s->use_skip_mb_code,
884 s->rl_table_index,
885 s->rl_chroma_table_index,
886 s->dc_table_index,
887 s->mv_table_index);*/
ae40484c
MN
888 if(s->flipflop_rounding){
889 s->no_rounding ^= 1;
890 }else{
891 s->no_rounding = 0;
892 }
893// printf("%d", s->no_rounding);
e1a9dbff 894//return -1;
de6d9b64 895 }
ae40484c 896
84afee34
MN
897#if 0
898if(s->msmpeg4_version==2)
899{
900int i;
84afee34
MN
901for(i=0; i<s->gb.size*8; i++)
902// printf("%d", get_bits1(&s->gb));
903 get_bits1(&s->gb);
904printf("END\n");
905return -1;
906}
907#endif
908
de6d9b64
FB
909#ifdef DEBUG
910 printf("*****frame %d:\n", frame_count++);
911#endif
912 return 0;
913}
914
ae40484c
MN
915int msmpeg4_decode_ext_header(MpegEncContext * s, int buf_size)
916{
ae40484c 917 /* the alt_bitstream reader could read over the end so we need to check it */
2b9ab1d5 918 if(get_bits_count(&s->gb) + 16 < buf_size*8)
ae40484c 919 {
2b9ab1d5
MN
920 int fps;
921
922 fps= get_bits(&s->gb, 5);
923 s->bitrate= get_bits(&s->gb, 11);
924 s->flipflop_rounding= get_bits1(&s->gb);
925
926// printf("fps:%2d bps:%2d roundingType:%1d\n", fps, s->bitrate, s->flipflop_rounding);
ae40484c
MN
927 }
928 else
929 {
2b9ab1d5
MN
930 s->flipflop_rounding= 0;
931 s->bitrate= 0;
ae40484c 932 }
2b9ab1d5 933
ae40484c
MN
934 return 0;
935}
936
cd4af68a 937static inline void msmpeg4_memsetw(short *tab, int val, int n)
de6d9b64
FB
938{
939 int i;
940 for(i=0;i<n;i++)
941 tab[i] = val;
942}
943
3825cd1d
MN
944static void msmpeg4v2_encode_motion(MpegEncContext * s, int val)
945{
946 int range, bit_size, sign, code, bits;
947
948 if (val == 0) {
949 /* zero vector */
950 code = 0;
951 put_bits(&s->pb, mvtab[code][1], mvtab[code][0]);
952 } else {
953 bit_size = s->f_code - 1;
954 range = 1 << bit_size;
955 if (val <= -64)
956 val += 64;
957 else if (val >= 64)
958 val -= 64;
959
960 if (val >= 0) {
961 sign = 0;
962 } else {
963 val = -val;
964 sign = 1;
965 }
966 val--;
967 code = (val >> bit_size) + 1;
968 bits = val & (range - 1);
969
970 put_bits(&s->pb, mvtab[code][1] + 1, (mvtab[code][0] << 1) | sign);
971 if (bit_size > 0) {
972 put_bits(&s->pb, bit_size, bits);
973 }
974 }
975}
976
84afee34
MN
977/* this is identical to h263 except that its range is multiplied by 2 */
978static int msmpeg4v2_decode_motion(MpegEncContext * s, int pred, int f_code)
979{
980 int code, val, sign, shift;
981
982 code = get_vlc(&s->gb, &v2_mv_vlc);
983 if (code < 0)
984 return 0xffff;
985
986 if (code == 0)
987 return pred;
988 sign = get_bits1(&s->gb);
989 shift = f_code - 1;
990 val = (code - 1) << shift;
991 if (shift > 0)
992 val |= get_bits(&s->gb, shift);
993 val++;
994 if (sign)
995 val = -val;
996 val += pred;
997
998 if (val <= -64)
999 val += 64;
1000 else if (val >= 64)
1001 val -= 64;
1002
1003 return val;
1004}
1005
1006
1007int msmpeg4v2_decode_mb(MpegEncContext *s,
1008 DCTELEM block[6][64])
1009{
1010 int cbp, code, i;
84afee34
MN
1011 if (s->pict_type == P_TYPE) {
1012 if (s->use_skip_mb_code) {
1013 if (get_bits1(&s->gb)) {
1014 /* skip mb */
1015 s->mb_intra = 0;
1016 for(i=0;i<6;i++)
1017 s->block_last_index[i] = -1;
1018 s->mv_dir = MV_DIR_FORWARD;
1019 s->mv_type = MV_TYPE_16X16;
1020 s->mv[0][0][0] = 0;
1021 s->mv[0][0][1] = 0;
1022 s->mb_skiped = 1;
1023 return 0;
1024 }
1025 }
1026
1027 code = get_vlc(&s->gb, &v2_mb_type_vlc);
1028 s->mb_intra = code >>2;
1029
1030 cbp = code & 0x3;
1031 } else {
1032 s->mb_intra = 1;
84afee34 1033 cbp= get_vlc(&s->gb, &v2_intra_cbpc_vlc);
84afee34
MN
1034 }
1035
1036 if (!s->mb_intra) {
1037 int mx, my;
1038
1039 cbp|= get_vlc(&s->gb, &cbpy_vlc)<<2;
1040 if((cbp&3) != 3) cbp^= 0x3C;
1041
1042 h263_pred_motion(s, 0, &mx, &my);
1043 mx= msmpeg4v2_decode_motion(s, mx, 1);
1044 my= msmpeg4v2_decode_motion(s, my, 1);
1045
1046 s->mv_dir = MV_DIR_FORWARD;
1047 s->mv_type = MV_TYPE_16X16;
1048 s->mv[0][0][0] = mx;
1049 s->mv[0][0][1] = my;
1050 } else {
84afee34 1051 s->ac_pred = get_bits1(&s->gb);
84afee34 1052 cbp|= get_vlc(&s->gb, &cbpy_vlc)<<2;
84afee34
MN
1053 }
1054
1055 for (i = 0; i < 6; i++) {
1056 if (msmpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1) < 0)
1057 {
84afee34
MN
1058 fprintf(stderr,"\nIgnoring error while decoding block: %d x %d (%d)\n", s->mb_x, s->mb_y, i);
1059 return -1;
1060 }
1061 }
1062 return 0;
1063}
1064
de6d9b64
FB
1065int msmpeg4_decode_mb(MpegEncContext *s,
1066 DCTELEM block[6][64])
1067{
1068 int cbp, code, i;
de6d9b64
FB
1069 UINT8 *coded_val;
1070
1071 /* special slice handling */
1072 if (s->mb_x == 0) {
525782f3 1073 if (s->slice_height && (s->mb_y % s->slice_height) == 0) {
de6d9b64
FB
1074 int wrap;
1075 /* reset DC pred (set previous line to 1024) */
1076 wrap = 2 * s->mb_width + 2;
cd4af68a
ZK
1077 msmpeg4_memsetw(&s->dc_val[0][(1) + (2 * s->mb_y) * wrap],
1078 1024, 2 * s->mb_width);
1079 wrap = s->mb_width + 2;
1080 msmpeg4_memsetw(&s->dc_val[1][(1) + (s->mb_y) * wrap],
1081 1024, s->mb_width);
1082 msmpeg4_memsetw(&s->dc_val[2][(1) + (s->mb_y) * wrap],
1083 1024, s->mb_width);
1084
1085 /* reset AC pred (set previous line to 0) */
1086 wrap = s->mb_width * 2 + 2;
1087 msmpeg4_memsetw(s->ac_val[0][0] + (1 + (2 * s->mb_y) * wrap)*16,
1088 0, 2 * s->mb_width*16);
1089 wrap = s->mb_width + 2;
1090 msmpeg4_memsetw(s->ac_val[1][0] + (1 + (s->mb_y) * wrap)*16,
1091 0, s->mb_width*16);
1092 msmpeg4_memsetw(s->ac_val[2][0] + (1 + (s->mb_y) * wrap)*16,
1093 0, s->mb_width*16);
de6d9b64
FB
1094
1095 s->first_slice_line = 1;
1096 } else {
1097 s->first_slice_line = 0;
1098 }
1099 }
1100
84afee34
MN
1101 if(s->msmpeg4_version==2) return msmpeg4v2_decode_mb(s, block); //FIXME merge if possible
1102
de6d9b64
FB
1103 if (s->pict_type == P_TYPE) {
1104 set_stat(ST_INTER_MB);
1105 if (s->use_skip_mb_code) {
612476ef 1106 if (get_bits1(&s->gb)) {
de6d9b64
FB
1107 /* skip mb */
1108 s->mb_intra = 0;
1109 for(i=0;i<6;i++)
1110 s->block_last_index[i] = -1;
1111 s->mv_dir = MV_DIR_FORWARD;
1112 s->mv_type = MV_TYPE_16X16;
1113 s->mv[0][0][0] = 0;
1114 s->mv[0][0][1] = 0;
3bb4e23a 1115 s->mb_skiped = 1;
de6d9b64
FB
1116 return 0;
1117 }
1118 }
1119
1120 code = get_vlc(&s->gb, &mb_non_intra_vlc);
1121 if (code < 0)
1122 return -1;
38d171eb
ZK
1123 //s->mb_intra = (code & 0x40) ? 0 : 1;
1124 s->mb_intra = (~code & 0x40) >> 6;
de6d9b64
FB
1125
1126 cbp = code & 0x3f;
1127 } else {
1128 set_stat(ST_INTRA_MB);
1129 s->mb_intra = 1;
1130 code = get_vlc(&s->gb, &mb_intra_vlc);
1131 if (code < 0)
1132 return -1;
1133 /* predict coded block pattern */
1134 cbp = 0;
1135 for(i=0;i<6;i++) {
38d171eb 1136 int val = ((code >> (5 - i)) & 1);
de6d9b64 1137 if (i < 4) {
38d171eb 1138 int pred = coded_block_pred(s, i, &coded_val);
de6d9b64
FB
1139 val = val ^ pred;
1140 *coded_val = val;
1141 }
1142 cbp |= val << (5 - i);
1143 }
1144 }
1145
1146 if (!s->mb_intra) {
1147 int mx, my;
1148 set_stat(ST_MV);
1149 h263_pred_motion(s, 0, &mx, &my);
1150 if (msmpeg4_decode_motion(s, &mx, &my) < 0)
1151 return -1;
1152 s->mv_dir = MV_DIR_FORWARD;
1153 s->mv_type = MV_TYPE_16X16;
1154 s->mv[0][0][0] = mx;
1155 s->mv[0][0][1] = my;
1156 } else {
1157 set_stat(ST_INTRA_MB);
612476ef 1158 s->ac_pred = get_bits1(&s->gb);
de6d9b64
FB
1159 }
1160
1161 for (i = 0; i < 6; i++) {
1162 if (msmpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1) < 0)
38d171eb
ZK
1163 {
1164 fprintf(stderr,"\nIgnoring error while decoding block: %d x %d (%d)\n", s->mb_x, s->mb_y, i);
1165 // return -1;
1166 }
de6d9b64
FB
1167 }
1168 return 0;
1169}
1170
1171static int msmpeg4_decode_block(MpegEncContext * s, DCTELEM * block,
1172 int n, int coded)
1173{
1174 int code, level, i, j, last, run, run_diff;
1175 int dc_pred_dir;
1176 RLTable *rl;
1177 const UINT8 *scan_table;
badaf88e 1178 int qmul, qadd;
de6d9b64
FB
1179
1180 if (s->mb_intra) {
badaf88e
MN
1181 qmul=1;
1182 qadd=0;
1183
de6d9b64
FB
1184 /* DC coef */
1185 set_stat(ST_DC);
1186 level = msmpeg4_decode_dc(s, n, &dc_pred_dir);
1187 if (level < 0)
1188 return -1;
1189 block[0] = level;
1190 if (n < 4) {
1191 rl = &rl_table[s->rl_table_index];
1192 } else {
1193 rl = &rl_table[3 + s->rl_chroma_table_index];
1194 }
badaf88e 1195
de6d9b64
FB
1196 run_diff = 0;
1197 i = 1;
1198 if (!coded) {
1199 goto not_coded;
1200 }
1201 if (s->ac_pred) {
1202 if (dc_pred_dir == 0)
1203 scan_table = ff_alternate_vertical_scan; /* left */
1204 else
1205 scan_table = ff_alternate_horizontal_scan; /* top */
1206 } else {
1207 scan_table = zigzag_direct;
1208 }
1209 set_stat(ST_INTRA_AC);
1210 } else {
badaf88e
MN
1211 qmul = s->qscale << 1;
1212 qadd = (s->qscale - 1) | 1;
de6d9b64
FB
1213 i = 0;
1214 rl = &rl_table[3 + s->rl_table_index];
84afee34
MN
1215
1216 if(s->msmpeg4_version==2)
1217 run_diff = 0;
1218 else
1219 run_diff = 1;
1220
de6d9b64
FB
1221 if (!coded) {
1222 s->block_last_index[n] = i - 1;
1223 return 0;
1224 }
1225 scan_table = zigzag_direct;
1226 set_stat(ST_INTER_AC);
1227 }
1228
1229 for(;;) {
1230 code = get_vlc(&s->gb, &rl->vlc);
1231 if (code < 0)
1232 return -1;
1233 if (code == rl->n) {
1234 /* escape */
612476ef
A
1235 if (get_bits1(&s->gb) == 0) {
1236 if (get_bits1(&s->gb) == 0) {
de6d9b64 1237 /* third escape */
612476ef 1238 last = get_bits1(&s->gb);
de6d9b64
FB
1239 run = get_bits(&s->gb, 6);
1240 level = get_bits(&s->gb, 8);
1241 level = (level << 24) >> 24; /* sign extend */
38d171eb
ZK
1242 //level = level * qmul + (level>0) * qadd - (level<=0) * qadd ;
1243 if (level>0) level= level * qmul + qadd;
badaf88e 1244 else level= level * qmul - qadd;
de6d9b64
FB
1245 } else {
1246 /* second escape */
1247 code = get_vlc(&s->gb, &rl->vlc);
1248 if (code < 0 || code >= rl->n)
1249 return -1;
1250 run = rl->table_run[code];
99180fe3 1251 level = rl->table_level[code];
de6d9b64
FB
1252 last = code >= rl->last;
1253 run += rl->max_run[last][level] + run_diff;
99180fe3 1254 level= level * qmul + qadd;
612476ef 1255 if (get_bits1(&s->gb))
de6d9b64
FB
1256 level = -level;
1257 }
1258 } else {
1259 /* first escape */
1260 code = get_vlc(&s->gb, &rl->vlc);
1261 if (code < 0 || code >= rl->n)
1262 return -1;
1263 run = rl->table_run[code];
1264 level = rl->table_level[code];
1265 last = code >= rl->last;
1266 level += rl->max_level[last][run];
badaf88e 1267 level= level * qmul + qadd;
612476ef 1268 if (get_bits1(&s->gb))
de6d9b64
FB
1269 level = -level;
1270 }
1271 } else {
1272 run = rl->table_run[code];
badaf88e 1273 level = rl->table_level[code] * qmul + qadd;
de6d9b64 1274 last = code >= rl->last;
612476ef 1275 if (get_bits1(&s->gb))
de6d9b64
FB
1276 level = -level;
1277 }
1278 i += run;
1279 if (i >= 64)
1280 return -1;
e1a9dbff 1281//printf("RL:%d %d %d ", run, level, last);
de6d9b64
FB
1282 j = scan_table[i];
1283 block[j] = level;
1284 i++;
1285 if (last)
1286 break;
1287 }
1288 not_coded:
1289 if (s->mb_intra) {
1290 mpeg4_pred_ac(s, block, n, dc_pred_dir);
1291 if (s->ac_pred) {
1292 i = 64; /* XXX: not optimal */
1293 }
1294 }
1295 s->block_last_index[n] = i - 1;
1296
1297 return 0;
1298}
1299
1300static int msmpeg4_decode_dc(MpegEncContext * s, int n, int *dir_ptr)
1301{
1302 int level, pred;
98be975d 1303 INT16 *dc_val;
de6d9b64 1304
84afee34
MN
1305 if(s->msmpeg4_version==2){
1306 if (n < 4) {
1307 level = get_vlc(&s->gb, &v2_dc_lum_vlc);
1308 } else {
1309 level = get_vlc(&s->gb, &v2_dc_chroma_vlc);
1310 }
2ed627e6 1311 if (level < 0)
84afee34 1312 return -1;
84afee34
MN
1313 level-=256;
1314 }else{ //FIXME optimize use unified tables & index
1315 if (n < 4) {
1316 level = get_vlc(&s->gb, &dc_lum_vlc[s->dc_table_index]);
1317 } else {
1318 level = get_vlc(&s->gb, &dc_chroma_vlc[s->dc_table_index]);
1319 }
1320 if (level < 0)
1321 return -1;
1322
1323 if (level == DC_MAX) {
1324 level = get_bits(&s->gb, 8);
1325 if (get_bits1(&s->gb))
1326 level = -level;
1327 } else if (level != 0) {
1328 if (get_bits1(&s->gb))
1329 level = -level;
1330 }
de6d9b64
FB
1331 }
1332
1333 pred = msmpeg4_pred_dc(s, n, &dc_val, dir_ptr);
1334 level += pred;
1335
1336 /* update predictor */
1337 if (n < 4) {
1338 *dc_val = level * s->y_dc_scale;
1339 } else {
1340 *dc_val = level * s->c_dc_scale;
1341 }
1342
1343 return level;
1344}
1345
1346static int msmpeg4_decode_motion(MpegEncContext * s,
1347 int *mx_ptr, int *my_ptr)
1348{
1349 MVTable *mv;
1350 int code, mx, my;
1351
1352 mv = &mv_tables[s->mv_table_index];
1353
1354 code = get_vlc(&s->gb, &mv->vlc);
1355 if (code < 0)
1356 return -1;
1357 if (code == mv->n) {
1358 mx = get_bits(&s->gb, 6);
1359 my = get_bits(&s->gb, 6);
1360 } else {
1361 mx = mv->table_mvx[code];
1362 my = mv->table_mvy[code];
1363 }
1364
1365 mx += *mx_ptr - 32;
1366 my += *my_ptr - 32;
1367 /* WARNING : they do not do exactly modulo encoding */
1368 if (mx <= -64)
1369 mx += 64;
1370 else if (mx >= 64)
1371 mx -= 64;
1372
1373 if (my <= -64)
1374 my += 64;
1375 else if (my >= 64)
1376 my -= 64;
1377 *mx_ptr = mx;
1378 *my_ptr = my;
1379 return 0;
1380}