added skip macroblock optimization (big perf win on black regions for example)
[libav.git] / libavcodec / msmpeg4.c
CommitLineData
de6d9b64
FB
1/*
2 * MSMPEG4 backend for ffmpeg encoder and decoder
3 * Copyright (c) 2001 Gerard Lantau.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19#include <stdlib.h>
20#include <stdio.h>
21#include "common.h"
22#include "dsputil.h"
23#include "mpegvideo.h"
24
25/*
26 * You can also call this codec : MPEG4 with a twist !
27 *
28 * TODO:
29 * - (encoding) select best mv table (two choices)
30 * - (encoding) select best vlc/dc table
31 * - (decoding) handle slice indication
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
46static void msmpeg4_encode_block(MpegEncContext * s, DCTELEM * block, int n);
47static int msmpeg4_decode_block(MpegEncContext * s, DCTELEM * block,
48 int n, int coded);
49static int msmpeg4_decode_dc(MpegEncContext * s, int n, int *dir_ptr);
50static int msmpeg4_decode_motion(MpegEncContext * s,
51 int *mx_ptr, int *my_ptr);
52
53#ifdef DEBUG
54int intra_count = 0;
55int frame_count = 0;
56#endif
57/* XXX: move it to mpegvideo.h */
58
59static int init_done = 0;
60
61#include "msmpeg4data.h"
62
63#ifdef STATS
64
65const char *st_names[ST_NB] = {
66 "unknown",
67 "dc",
68 "intra_ac",
69 "inter_ac",
70 "intra_mb",
71 "inter_mb",
72 "mv",
73};
74
75int st_current_index = 0;
76unsigned int st_bit_counts[ST_NB];
77unsigned int st_out_bit_counts[ST_NB];
78
79#define set_stat(var) st_current_index = var;
80
81void print_stats(void)
82{
83 unsigned int total;
84 int i;
85
86 printf("Input:\n");
87 total = 0;
88 for(i=0;i<ST_NB;i++)
89 total += st_bit_counts[i];
90 if (total == 0)
91 total = 1;
92 for(i=0;i<ST_NB;i++) {
93 printf("%-10s : %10.1f %5.1f%%\n",
94 st_names[i],
95 (double)st_bit_counts[i] / 8.0,
96 (double)st_bit_counts[i] * 100.0 / total);
97 }
98 printf("%-10s : %10.1f %5.1f%%\n",
99 "total",
100 (double)total / 8.0,
101 100.0);
102
103 printf("Output:\n");
104 total = 0;
105 for(i=0;i<ST_NB;i++)
106 total += st_out_bit_counts[i];
107 if (total == 0)
108 total = 1;
109 for(i=0;i<ST_NB;i++) {
110 printf("%-10s : %10.1f %5.1f%%\n",
111 st_names[i],
112 (double)st_out_bit_counts[i] / 8.0,
113 (double)st_out_bit_counts[i] * 100.0 / total);
114 }
115 printf("%-10s : %10.1f %5.1f%%\n",
116 "total",
117 (double)total / 8.0,
118 100.0);
119}
120
121#else
122
123#define set_stat(var)
124
125#endif
126
127/* build the table which associate a (x,y) motion vector to a vlc */
128static void init_mv_table(MVTable *tab)
129{
130 int i, x, y;
131
132 tab->table_mv_index = malloc(sizeof(UINT16) * 4096);
133 /* mark all entries as not used */
134 for(i=0;i<4096;i++)
135 tab->table_mv_index[i] = tab->n;
136
137 for(i=0;i<tab->n;i++) {
138 x = tab->table_mvx[i];
139 y = tab->table_mvy[i];
140 tab->table_mv_index[(x << 6) | y] = i;
141 }
142}
143
144static void code012(PutBitContext *pb, int n)
145{
146 if (n == 0) {
147 put_bits(pb, 1, 0);
148 } else {
149 put_bits(pb, 1, 1);
150 put_bits(pb, 1, (n >= 2));
151 }
152}
153
154/* write MSMPEG4 V3 compatible frame header */
155void msmpeg4_encode_picture_header(MpegEncContext * s, int picture_number)
156{
157 int i;
158
159 align_put_bits(&s->pb);
160
161 put_bits(&s->pb, 2, s->pict_type - 1);
162
163 put_bits(&s->pb, 5, s->qscale);
164
165 s->rl_table_index = 2;
166 s->rl_chroma_table_index = 1; /* only for I frame */
167 s->dc_table_index = 1;
168 s->mv_table_index = 1; /* only if P frame */
169 s->use_skip_mb_code = 1; /* only if P frame */
170
171 if (s->pict_type == I_TYPE) {
172 put_bits(&s->pb, 5, 0x17); /* indicate only one "slice" */
173
174 code012(&s->pb, s->rl_chroma_table_index);
175 code012(&s->pb, s->rl_table_index);
176
177 put_bits(&s->pb, 1, s->dc_table_index);
178 s->no_rounding = 1;
179 } else {
180 put_bits(&s->pb, 1, s->use_skip_mb_code);
181
182 s->rl_chroma_table_index = s->rl_table_index;
183 code012(&s->pb, s->rl_table_index);
184
185 put_bits(&s->pb, 1, s->dc_table_index);
186
187 put_bits(&s->pb, 1, s->mv_table_index);
188 s->no_rounding ^= 1;
189 }
190
191 if (!init_done) {
192 /* init various encoding tables */
193 init_done = 1;
194 init_mv_table(&mv_tables[0]);
195 init_mv_table(&mv_tables[1]);
196 for(i=0;i<NB_RL_TABLES;i++)
197 init_rl(&rl_table[i]);
198 }
199
200#ifdef DEBUG
201 intra_count = 0;
202 printf("*****frame %d:\n", frame_count++);
203#endif
204}
205
206/* predict coded block */
207static inline int coded_block_pred(MpegEncContext * s, int n, UINT8 **coded_block_ptr)
208{
209 int x, y, wrap, pred, a, b, c;
210
211 x = 2 * s->mb_x + 1 + (n & 1);
212 y = 2 * s->mb_y + 1 + ((n & 2) >> 1);
213 wrap = s->mb_width * 2 + 2;
214
215 /* B C
216 * A X
217 */
218 a = s->coded_block[(x - 1) + (y) * wrap];
219 b = s->coded_block[(x - 1) + (y - 1) * wrap];
220 c = s->coded_block[(x) + (y - 1) * wrap];
221
222 if (b == c) {
223 pred = a;
224 } else {
225 pred = c;
226 }
227
228 /* store value */
229 *coded_block_ptr = &s->coded_block[(x) + (y) * wrap];
230
231 return pred;
232}
233
234static void msmpeg4_encode_motion(MpegEncContext * s,
235 int mx, int my)
236{
237 int code;
238 MVTable *mv;
239
240 /* modulo encoding */
241 /* WARNING : you cannot reach all the MVs even with the modulo
242 encoding. This is a somewhat strange compromise they took !!! */
243 if (mx <= -64)
244 mx += 64;
245 else if (mx >= 64)
246 mx -= 64;
247 if (my <= -64)
248 my += 64;
249 else if (my >= 64)
250 my -= 64;
251
252 mx += 32;
253 my += 32;
254#if 0
255 if ((unsigned)mx >= 64 ||
256 (unsigned)my >= 64)
257 fprintf(stderr, "error mx=%d my=%d\n", mx, my);
258#endif
259 mv = &mv_tables[s->mv_table_index];
260
261 code = mv->table_mv_index[(mx << 6) | my];
262 set_stat(ST_MV);
263 put_bits(&s->pb,
264 mv->table_mv_bits[code],
265 mv->table_mv_code[code]);
266 if (code == mv->n) {
267 /* escape : code litterally */
268 put_bits(&s->pb, 6, mx);
269 put_bits(&s->pb, 6, my);
270 }
271}
272
273void msmpeg4_encode_mb(MpegEncContext * s,
274 DCTELEM block[6][64],
275 int motion_x, int motion_y)
276{
277 int cbp, coded_cbp, i;
278 int pred_x, pred_y;
279 UINT8 *coded_block;
280
281 if (!s->mb_intra) {
282 /* compute cbp */
283 set_stat(ST_INTER_MB);
284 cbp = 0;
285 for (i = 0; i < 6; i++) {
286 if (s->block_last_index[i] >= 0)
287 cbp |= 1 << (5 - i);
288 }
289 if (s->use_skip_mb_code && (cbp | motion_x | motion_y) == 0) {
290 /* skip macroblock */
291 put_bits(&s->pb, 1, 1);
292 return;
293 }
294 if (s->use_skip_mb_code)
295 put_bits(&s->pb, 1, 0); /* mb coded */
296
297 put_bits(&s->pb,
298 table_mb_non_intra[cbp + 64][1],
299 table_mb_non_intra[cbp + 64][0]);
300
301 /* motion vector */
302 h263_pred_motion(s, 0, &pred_x, &pred_y);
303 msmpeg4_encode_motion(s, motion_x - pred_x,
304 motion_y - pred_y);
305 } else {
306 /* compute cbp */
307 cbp = 0;
308 coded_cbp = 0;
309 for (i = 0; i < 6; i++) {
310 int val, pred;
311 val = (s->block_last_index[i] >= 1);
312 cbp |= val << (5 - i);
313 if (i < 4) {
314 /* predict value for close blocks only for luma */
315 pred = coded_block_pred(s, i, &coded_block);
316 *coded_block = val;
317 val = val ^ pred;
318 }
319 coded_cbp |= val << (5 - i);
320 }
321#if 0
322 if (coded_cbp)
323 printf("cbp=%x %x\n", cbp, coded_cbp);
324#endif
325
326 if (s->pict_type == I_TYPE) {
327 set_stat(ST_INTRA_MB);
328 put_bits(&s->pb,
329 table_mb_intra[coded_cbp][1], table_mb_intra[coded_cbp][0]);
330 } else {
331 if (s->use_skip_mb_code)
332 put_bits(&s->pb, 1, 0); /* mb coded */
333 put_bits(&s->pb,
334 table_mb_non_intra[cbp][1],
335 table_mb_non_intra[cbp][0]);
336 }
337 set_stat(ST_INTRA_MB);
338 put_bits(&s->pb, 1, 0); /* no AC prediction yet */
339 }
340
341 for (i = 0; i < 6; i++) {
342 msmpeg4_encode_block(s, block[i], i);
343 }
344}
345
346
347/* strongly inspirated from MPEG4, but not exactly the same ! */
348void msmpeg4_dc_scale(MpegEncContext * s)
349{
350 int scale;
351
352 if (s->qscale < 5)
353 scale = 8;
354 else if (s->qscale < 9)
355 scale = 2 * s->qscale;
356 else
357 scale = s->qscale + 8;
358 s->y_dc_scale = scale;
359 s->c_dc_scale = (s->qscale + 13) / 2;
360}
361
362/* dir = 0: left, dir = 1: top prediction */
363static int msmpeg4_pred_dc(MpegEncContext * s, int n,
364 UINT16 **dc_val_ptr, int *dir_ptr)
365{
366 int a, b, c, x, y, wrap, pred, scale;
367 UINT16 *dc_val;
368
369 /* find prediction */
370 if (n < 4) {
371 x = 2 * s->mb_x + 1 + (n & 1);
372 y = 2 * s->mb_y + 1 + ((n & 2) >> 1);
373 wrap = s->mb_width * 2 + 2;
374 dc_val = s->dc_val[0];
375 scale = s->y_dc_scale;
376 } else {
377 x = s->mb_x + 1;
378 y = s->mb_y + 1;
379 wrap = s->mb_width + 2;
380 dc_val = s->dc_val[n - 4 + 1];
381 scale = s->c_dc_scale;
382 }
383
384 /* B C
385 * A X
386 */
387 a = dc_val[(x - 1) + (y) * wrap];
388 b = dc_val[(x - 1) + (y - 1) * wrap];
389 c = dc_val[(x) + (y - 1) * wrap];
390
391 /* XXX: the following solution consumes divisions, but it does not
392 necessitate to modify mpegvideo.c. The problem comes from the
393 fact they decided to store the quantized DC (which would lead
394 to problems if Q could vary !) */
395 a = (a + (scale >> 1)) / scale;
396 b = (b + (scale >> 1)) / scale;
397 c = (c + (scale >> 1)) / scale;
398
399 /* XXX: WARNING: they did not choose the same test as MPEG4. This
400 is very important ! */
401 if (abs(a - b) <= abs(b - c)) {
402 pred = c;
403 *dir_ptr = 1;
404 } else {
405 pred = a;
406 *dir_ptr = 0;
407 }
408
409 /* update predictor */
410 *dc_val_ptr = &dc_val[(x) + (y) * wrap];
411 return pred;
412}
413
414#define DC_MAX 119
415
416static void msmpeg4_encode_dc(MpegEncContext * s, int level, int n, int *dir_ptr)
417{
418 int sign, code;
419 int pred;
420 UINT16 *dc_val;
421
422 pred = msmpeg4_pred_dc(s, n, &dc_val, dir_ptr);
423
424 /* update predictor */
425 if (n < 4) {
426 *dc_val = level * s->y_dc_scale;
427 } else {
428 *dc_val = level * s->c_dc_scale;
429 }
430
431 /* do the prediction */
432 level -= pred;
433
434 sign = 0;
435 if (level < 0) {
436 level = -level;
437 sign = 1;
438 }
439
440 code = level;
441 if (code > DC_MAX)
442 code = DC_MAX;
443
444 if (s->dc_table_index == 0) {
445 if (n < 4) {
446 put_bits(&s->pb, table0_dc_lum[code][1], table0_dc_lum[code][0]);
447 } else {
448 put_bits(&s->pb, table0_dc_chroma[code][1], table0_dc_chroma[code][0]);
449 }
450 } else {
451 if (n < 4) {
452 put_bits(&s->pb, table1_dc_lum[code][1], table1_dc_lum[code][0]);
453 } else {
454 put_bits(&s->pb, table1_dc_chroma[code][1], table1_dc_chroma[code][0]);
455 }
456 }
457
458 if (code == DC_MAX)
459 put_bits(&s->pb, 8, level);
460
461 if (level != 0) {
462 put_bits(&s->pb, 1, sign);
463 }
464}
465
466/* Encoding of a block. Very similar to MPEG4 except for a different
467 escape coding (same as H263) and more vlc tables.
468 */
469static void msmpeg4_encode_block(MpegEncContext * s, DCTELEM * block, int n)
470{
471 int level, run, last, i, j, last_index;
472 int last_non_zero, sign, slevel;
473 int code, run_diff, dc_pred_dir;
474 const RLTable *rl;
475
476 if (s->mb_intra) {
477 set_stat(ST_DC);
478 msmpeg4_encode_dc(s, block[0], n, &dc_pred_dir);
479 i = 1;
480 if (n < 4) {
481 rl = &rl_table[s->rl_table_index];
482 } else {
483 rl = &rl_table[3 + s->rl_chroma_table_index];
484 }
485 run_diff = 0;
486 set_stat(ST_INTRA_AC);
487 } else {
488 i = 0;
489 rl = &rl_table[3 + s->rl_table_index];
490 run_diff = 1;
491 set_stat(ST_INTER_AC);
492 }
493
494 /* AC coefs */
495 last_index = s->block_last_index[n];
496 last_non_zero = i - 1;
497 for (; i <= last_index; i++) {
498 j = zigzag_direct[i];
499 level = block[j];
500 if (level) {
501 run = i - last_non_zero - 1;
502 last = (i == last_index);
503 sign = 0;
504 slevel = level;
505 if (level < 0) {
506 sign = 1;
507 level = -level;
508 }
509 code = get_rl_index(rl, last, run, level);
510 put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
511 if (code == rl->n) {
512 int level1, run1;
513
514 level1 = level - rl->max_level[last][run];
515 if (level1 < 1)
516 goto esc2;
517 code = get_rl_index(rl, last, run, level1);
518 if (code == rl->n) {
519 esc2:
520 put_bits(&s->pb, 1, 0);
521 if (level > MAX_LEVEL)
522 goto esc3;
523 run1 = run - rl->max_run[last][level] - run_diff;
524 if (run1 < 0)
525 goto esc3;
526 code = get_rl_index(rl, last, run1, level);
527 if (code == rl->n) {
528 esc3:
529 /* third escape */
530 put_bits(&s->pb, 1, 0);
531 put_bits(&s->pb, 1, last);
532 put_bits(&s->pb, 6, run);
533 put_bits(&s->pb, 8, slevel & 0xff);
534 } else {
535 /* second escape */
536 put_bits(&s->pb, 1, 1);
537 put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
538 put_bits(&s->pb, 1, sign);
539 }
540 } else {
541 /* first escape */
542 put_bits(&s->pb, 1, 1);
543 put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
544 put_bits(&s->pb, 1, sign);
545 }
546 } else {
547 put_bits(&s->pb, 1, sign);
548 }
549 last_non_zero = i;
550 }
551 }
552}
553
554/****************************************/
555/* decoding stuff */
556
557static VLC mb_non_intra_vlc;
558static VLC mb_intra_vlc;
559static VLC dc_lum_vlc[2];
560static VLC dc_chroma_vlc[2];
561
562/* init all vlc decoding tables */
563int msmpeg4_decode_init_vlc(MpegEncContext *s)
564{
565 int i;
566 MVTable *mv;
567
568 for(i=0;i<NB_RL_TABLES;i++) {
569 init_rl(&rl_table[i]);
570 init_vlc_rl(&rl_table[i]);
571 }
572 for(i=0;i<2;i++) {
573 mv = &mv_tables[i];
574 init_vlc(&mv->vlc, 9, mv->n + 1,
575 mv->table_mv_bits, 1, 1,
576 mv->table_mv_code, 2, 2);
577 }
578
579 init_vlc(&dc_lum_vlc[0], 9, 120,
580 &table0_dc_lum[0][1], 8, 4,
581 &table0_dc_lum[0][0], 8, 4);
582 init_vlc(&dc_chroma_vlc[0], 9, 120,
583 &table0_dc_chroma[0][1], 8, 4,
584 &table0_dc_chroma[0][0], 8, 4);
585 init_vlc(&dc_lum_vlc[1], 9, 120,
586 &table1_dc_lum[0][1], 8, 4,
587 &table1_dc_lum[0][0], 8, 4);
588 init_vlc(&dc_chroma_vlc[1], 9, 120,
589 &table1_dc_chroma[0][1], 8, 4,
590 &table1_dc_chroma[0][0], 8, 4);
591
592 init_vlc(&mb_non_intra_vlc, 9, 128,
593 &table_mb_non_intra[0][1], 8, 4,
594 &table_mb_non_intra[0][0], 8, 4);
595 init_vlc(&mb_intra_vlc, 9, 128,
596 &table_mb_intra[0][1], 4, 2,
597 &table_mb_intra[0][0], 4, 2);
598 return 0;
599}
600
601static int decode012(GetBitContext *gb)
602{
603 int n;
604 n = get_bits(gb, 1);
605 if (n == 0)
606 return 0;
607 else
608 return get_bits(gb, 1) + 1;
609}
610
611int msmpeg4_decode_picture_header(MpegEncContext * s)
612{
613 int code;
614
615 s->pict_type = get_bits(&s->gb, 2) + 1;
616 if (s->pict_type != I_TYPE &&
617 s->pict_type != P_TYPE)
618 return -1;
619
620 s->qscale = get_bits(&s->gb, 5);
621
622 if (s->pict_type == I_TYPE) {
623 code = get_bits(&s->gb, 5);
624 /* 0x17: one slice, 0x18: three slices */
625 /* XXX: implement it */
626 if (code < 0x17)
627 return -1;
628 s->slice_height = s->mb_height / (code - 0x16);
629 s->rl_chroma_table_index = decode012(&s->gb);
630 s->rl_table_index = decode012(&s->gb);
631
632 s->dc_table_index = get_bits(&s->gb, 1);
633 s->no_rounding = 1;
634 } else {
635 s->use_skip_mb_code = get_bits(&s->gb, 1);
636
637 s->rl_table_index = decode012(&s->gb);
638 s->rl_chroma_table_index = s->rl_table_index;
639
640 s->dc_table_index = get_bits(&s->gb, 1);
641
642 s->mv_table_index = get_bits(&s->gb, 1);
643 s->no_rounding ^= 1;
644 }
645#ifdef DEBUG
646 printf("*****frame %d:\n", frame_count++);
647#endif
648 return 0;
649}
650
651void memsetw(short *tab, int val, int n)
652{
653 int i;
654 for(i=0;i<n;i++)
655 tab[i] = val;
656}
657
658int msmpeg4_decode_mb(MpegEncContext *s,
659 DCTELEM block[6][64])
660{
661 int cbp, code, i;
662 int pred, val;
663 UINT8 *coded_val;
664
665 /* special slice handling */
666 if (s->mb_x == 0) {
667 if ((s->mb_y % s->slice_height) == 0) {
668 int wrap;
669 /* reset DC pred (set previous line to 1024) */
670 wrap = 2 * s->mb_width + 2;
671 memsetw(&s->dc_val[0][(1) + (2 * s->mb_y) * wrap],
672 1024, 2 * s->mb_width);
673 wrap = s->mb_width + 2;
674 memsetw(&s->dc_val[1][(1) + (s->mb_y) * wrap],
675 1024, s->mb_width);
676 memsetw(&s->dc_val[2][(1) + (s->mb_y) * wrap],
677 1024, s->mb_width);
678
679 s->first_slice_line = 1;
680 } else {
681 s->first_slice_line = 0;
682 }
683 }
684
685 if (s->pict_type == P_TYPE) {
686 set_stat(ST_INTER_MB);
687 if (s->use_skip_mb_code) {
688 if (get_bits(&s->gb, 1)) {
689 /* skip mb */
690 s->mb_intra = 0;
691 for(i=0;i<6;i++)
692 s->block_last_index[i] = -1;
693 s->mv_dir = MV_DIR_FORWARD;
694 s->mv_type = MV_TYPE_16X16;
695 s->mv[0][0][0] = 0;
696 s->mv[0][0][1] = 0;
3bb4e23a 697 s->mb_skiped = 1;
de6d9b64
FB
698 return 0;
699 }
700 }
701
702 code = get_vlc(&s->gb, &mb_non_intra_vlc);
703 if (code < 0)
704 return -1;
705 if (code & 0x40)
706 s->mb_intra = 0;
707 else
708 s->mb_intra = 1;
709
710 cbp = code & 0x3f;
711 } else {
712 set_stat(ST_INTRA_MB);
713 s->mb_intra = 1;
714 code = get_vlc(&s->gb, &mb_intra_vlc);
715 if (code < 0)
716 return -1;
717 /* predict coded block pattern */
718 cbp = 0;
719 for(i=0;i<6;i++) {
720 val = ((code >> (5 - i)) & 1);
721 if (i < 4) {
722 pred = coded_block_pred(s, i, &coded_val);
723 val = val ^ pred;
724 *coded_val = val;
725 }
726 cbp |= val << (5 - i);
727 }
728 }
729
730 if (!s->mb_intra) {
731 int mx, my;
732 set_stat(ST_MV);
733 h263_pred_motion(s, 0, &mx, &my);
734 if (msmpeg4_decode_motion(s, &mx, &my) < 0)
735 return -1;
736 s->mv_dir = MV_DIR_FORWARD;
737 s->mv_type = MV_TYPE_16X16;
738 s->mv[0][0][0] = mx;
739 s->mv[0][0][1] = my;
740 } else {
741 set_stat(ST_INTRA_MB);
742 s->ac_pred = get_bits(&s->gb, 1);
743 }
744
745 for (i = 0; i < 6; i++) {
746 if (msmpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1) < 0)
747 return -1;
748 }
749 return 0;
750}
751
752static int msmpeg4_decode_block(MpegEncContext * s, DCTELEM * block,
753 int n, int coded)
754{
755 int code, level, i, j, last, run, run_diff;
756 int dc_pred_dir;
757 RLTable *rl;
758 const UINT8 *scan_table;
759
760 if (s->mb_intra) {
761 /* DC coef */
762 set_stat(ST_DC);
763 level = msmpeg4_decode_dc(s, n, &dc_pred_dir);
764 if (level < 0)
765 return -1;
766 block[0] = level;
767 if (n < 4) {
768 rl = &rl_table[s->rl_table_index];
769 } else {
770 rl = &rl_table[3 + s->rl_chroma_table_index];
771 }
772 run_diff = 0;
773 i = 1;
774 if (!coded) {
775 goto not_coded;
776 }
777 if (s->ac_pred) {
778 if (dc_pred_dir == 0)
779 scan_table = ff_alternate_vertical_scan; /* left */
780 else
781 scan_table = ff_alternate_horizontal_scan; /* top */
782 } else {
783 scan_table = zigzag_direct;
784 }
785 set_stat(ST_INTRA_AC);
786 } else {
787 i = 0;
788 rl = &rl_table[3 + s->rl_table_index];
789 run_diff = 1;
790 if (!coded) {
791 s->block_last_index[n] = i - 1;
792 return 0;
793 }
794 scan_table = zigzag_direct;
795 set_stat(ST_INTER_AC);
796 }
797
798 for(;;) {
799 code = get_vlc(&s->gb, &rl->vlc);
800 if (code < 0)
801 return -1;
802 if (code == rl->n) {
803 /* escape */
804 if (get_bits(&s->gb, 1) == 0) {
805 if (get_bits(&s->gb, 1) == 0) {
806 /* third escape */
807 last = get_bits(&s->gb, 1);
808 run = get_bits(&s->gb, 6);
809 level = get_bits(&s->gb, 8);
810 level = (level << 24) >> 24; /* sign extend */
811 } else {
812 /* second escape */
813 code = get_vlc(&s->gb, &rl->vlc);
814 if (code < 0 || code >= rl->n)
815 return -1;
816 run = rl->table_run[code];
817 level = rl->table_level[code];
818 last = code >= rl->last;
819 run += rl->max_run[last][level] + run_diff;
820 if (get_bits(&s->gb, 1))
821 level = -level;
822 }
823 } else {
824 /* first escape */
825 code = get_vlc(&s->gb, &rl->vlc);
826 if (code < 0 || code >= rl->n)
827 return -1;
828 run = rl->table_run[code];
829 level = rl->table_level[code];
830 last = code >= rl->last;
831 level += rl->max_level[last][run];
832 if (get_bits(&s->gb, 1))
833 level = -level;
834 }
835 } else {
836 run = rl->table_run[code];
837 level = rl->table_level[code];
838 last = code >= rl->last;
839 if (get_bits(&s->gb, 1))
840 level = -level;
841 }
842 i += run;
843 if (i >= 64)
844 return -1;
845 j = scan_table[i];
846 block[j] = level;
847 i++;
848 if (last)
849 break;
850 }
851 not_coded:
852 if (s->mb_intra) {
853 mpeg4_pred_ac(s, block, n, dc_pred_dir);
854 if (s->ac_pred) {
855 i = 64; /* XXX: not optimal */
856 }
857 }
858 s->block_last_index[n] = i - 1;
859
860 return 0;
861}
862
863static int msmpeg4_decode_dc(MpegEncContext * s, int n, int *dir_ptr)
864{
865 int level, pred;
866 UINT16 *dc_val;
867
868 if (n < 4) {
869 level = get_vlc(&s->gb, &dc_lum_vlc[s->dc_table_index]);
870 } else {
871 level = get_vlc(&s->gb, &dc_chroma_vlc[s->dc_table_index]);
872 }
873 if (level < 0)
874 return -1;
875
876 if (level == DC_MAX) {
877 level = get_bits(&s->gb, 8);
878 if (get_bits(&s->gb, 1))
879 level = -level;
880 } else if (level != 0) {
881 if (get_bits(&s->gb, 1))
882 level = -level;
883 }
884
885 pred = msmpeg4_pred_dc(s, n, &dc_val, dir_ptr);
886 level += pred;
887
888 /* update predictor */
889 if (n < 4) {
890 *dc_val = level * s->y_dc_scale;
891 } else {
892 *dc_val = level * s->c_dc_scale;
893 }
894
895 return level;
896}
897
898static int msmpeg4_decode_motion(MpegEncContext * s,
899 int *mx_ptr, int *my_ptr)
900{
901 MVTable *mv;
902 int code, mx, my;
903
904 mv = &mv_tables[s->mv_table_index];
905
906 code = get_vlc(&s->gb, &mv->vlc);
907 if (code < 0)
908 return -1;
909 if (code == mv->n) {
910 mx = get_bits(&s->gb, 6);
911 my = get_bits(&s->gb, 6);
912 } else {
913 mx = mv->table_mvx[code];
914 my = mv->table_mvy[code];
915 }
916
917 mx += *mx_ptr - 32;
918 my += *my_ptr - 32;
919 /* WARNING : they do not do exactly modulo encoding */
920 if (mx <= -64)
921 mx += 64;
922 else if (mx >= 64)
923 mx -= 64;
924
925 if (my <= -64)
926 my += 64;
927 else if (my >= 64)
928 my -= 64;
929 *mx_ptr = mx;
930 *my_ptr = my;
931 return 0;
932}