minor optimizations / simplifications
[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"
2b9ab1d5 24#include "avcodec.h"
de6d9b64
FB
25
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
32 * - (decoding) handle slice indication
33 */
34//#define DEBUG
35
36/* motion vector table */
37typedef struct MVTable {
38 int n;
39 const UINT16 *table_mv_code;
40 const UINT8 *table_mv_bits;
41 const UINT8 *table_mvx;
42 const UINT8 *table_mvy;
43 UINT16 *table_mv_index; /* encoding: convert mv to index in table_mv */
44 VLC vlc; /* decoding: vlc */
45} MVTable;
46
47static void msmpeg4_encode_block(MpegEncContext * s, DCTELEM * block, int n);
48static int msmpeg4_decode_block(MpegEncContext * s, DCTELEM * block,
49 int n, int coded);
50static int msmpeg4_decode_dc(MpegEncContext * s, int n, int *dir_ptr);
51static int msmpeg4_decode_motion(MpegEncContext * s,
52 int *mx_ptr, int *my_ptr);
53
6fe84b43
MN
54extern UINT32 inverse[256];
55
de6d9b64
FB
56#ifdef DEBUG
57int intra_count = 0;
58int frame_count = 0;
59#endif
60/* XXX: move it to mpegvideo.h */
61
62static int init_done = 0;
63
64#include "msmpeg4data.h"
65
66#ifdef STATS
67
68const char *st_names[ST_NB] = {
69 "unknown",
70 "dc",
71 "intra_ac",
72 "inter_ac",
73 "intra_mb",
74 "inter_mb",
75 "mv",
76};
77
78int st_current_index = 0;
79unsigned int st_bit_counts[ST_NB];
80unsigned int st_out_bit_counts[ST_NB];
81
82#define set_stat(var) st_current_index = var;
83
84void print_stats(void)
85{
86 unsigned int total;
87 int i;
88
89 printf("Input:\n");
90 total = 0;
91 for(i=0;i<ST_NB;i++)
92 total += st_bit_counts[i];
93 if (total == 0)
94 total = 1;
95 for(i=0;i<ST_NB;i++) {
96 printf("%-10s : %10.1f %5.1f%%\n",
97 st_names[i],
98 (double)st_bit_counts[i] / 8.0,
99 (double)st_bit_counts[i] * 100.0 / total);
100 }
101 printf("%-10s : %10.1f %5.1f%%\n",
102 "total",
103 (double)total / 8.0,
104 100.0);
105
106 printf("Output:\n");
107 total = 0;
108 for(i=0;i<ST_NB;i++)
109 total += st_out_bit_counts[i];
110 if (total == 0)
111 total = 1;
112 for(i=0;i<ST_NB;i++) {
113 printf("%-10s : %10.1f %5.1f%%\n",
114 st_names[i],
115 (double)st_out_bit_counts[i] / 8.0,
116 (double)st_out_bit_counts[i] * 100.0 / total);
117 }
118 printf("%-10s : %10.1f %5.1f%%\n",
119 "total",
120 (double)total / 8.0,
121 100.0);
122}
123
124#else
125
126#define set_stat(var)
127
128#endif
129
130/* build the table which associate a (x,y) motion vector to a vlc */
131static void init_mv_table(MVTable *tab)
132{
133 int i, x, y;
134
135 tab->table_mv_index = malloc(sizeof(UINT16) * 4096);
136 /* mark all entries as not used */
137 for(i=0;i<4096;i++)
138 tab->table_mv_index[i] = tab->n;
139
140 for(i=0;i<tab->n;i++) {
141 x = tab->table_mvx[i];
142 y = tab->table_mvy[i];
143 tab->table_mv_index[(x << 6) | y] = i;
144 }
145}
146
147static void code012(PutBitContext *pb, int n)
148{
149 if (n == 0) {
150 put_bits(pb, 1, 0);
151 } else {
152 put_bits(pb, 1, 1);
153 put_bits(pb, 1, (n >= 2));
154 }
155}
156
157/* write MSMPEG4 V3 compatible frame header */
158void msmpeg4_encode_picture_header(MpegEncContext * s, int picture_number)
159{
160 int i;
161
162 align_put_bits(&s->pb);
163
164 put_bits(&s->pb, 2, s->pict_type - 1);
165
166 put_bits(&s->pb, 5, s->qscale);
167
168 s->rl_table_index = 2;
169 s->rl_chroma_table_index = 1; /* only for I frame */
170 s->dc_table_index = 1;
171 s->mv_table_index = 1; /* only if P frame */
172 s->use_skip_mb_code = 1; /* only if P frame */
173
174 if (s->pict_type == I_TYPE) {
175 put_bits(&s->pb, 5, 0x17); /* indicate only one "slice" */
176
177 code012(&s->pb, s->rl_chroma_table_index);
178 code012(&s->pb, s->rl_table_index);
179
180 put_bits(&s->pb, 1, s->dc_table_index);
181 s->no_rounding = 1;
182 } else {
183 put_bits(&s->pb, 1, s->use_skip_mb_code);
184
185 s->rl_chroma_table_index = s->rl_table_index;
186 code012(&s->pb, s->rl_table_index);
187
188 put_bits(&s->pb, 1, s->dc_table_index);
189
190 put_bits(&s->pb, 1, s->mv_table_index);
ae40484c
MN
191
192 if(s->flipflop_rounding){
193 s->no_rounding ^= 1;
194 }else{
195 s->no_rounding = 0;
196 }
de6d9b64
FB
197 }
198
199 if (!init_done) {
200 /* init various encoding tables */
201 init_done = 1;
202 init_mv_table(&mv_tables[0]);
203 init_mv_table(&mv_tables[1]);
204 for(i=0;i<NB_RL_TABLES;i++)
205 init_rl(&rl_table[i]);
206 }
207
208#ifdef DEBUG
209 intra_count = 0;
210 printf("*****frame %d:\n", frame_count++);
211#endif
212}
213
ae40484c
MN
214void msmpeg4_encode_ext_header(MpegEncContext * s)
215{
ae40484c 216 s->flipflop_rounding=1;
2b9ab1d5 217 s->bitrate= 910; // FIXME
ae40484c 218
2b9ab1d5 219 put_bits(&s->pb, 5, s->frame_rate / FRAME_RATE_BASE); //yes 29.97 -> 29
ae40484c
MN
220
221 put_bits(&s->pb, 11, s->bitrate);
222
223 put_bits(&s->pb, 1, s->flipflop_rounding);
ae40484c
MN
224}
225
de6d9b64
FB
226/* predict coded block */
227static inline int coded_block_pred(MpegEncContext * s, int n, UINT8 **coded_block_ptr)
228{
dbbe8999 229 int xy, wrap, pred, a, b, c;
de6d9b64 230
dbbe8999
MN
231 xy = s->block_index[n];
232 wrap = s->block_wrap[0];
de6d9b64
FB
233
234 /* B C
235 * A X
236 */
dbbe8999
MN
237 a = s->coded_block[xy - 1 ];
238 b = s->coded_block[xy - 1 - wrap];
239 c = s->coded_block[xy - wrap];
de6d9b64
FB
240
241 if (b == c) {
242 pred = a;
243 } else {
244 pred = c;
245 }
246
247 /* store value */
dbbe8999 248 *coded_block_ptr = &s->coded_block[xy];
de6d9b64
FB
249
250 return pred;
251}
252
253static void msmpeg4_encode_motion(MpegEncContext * s,
254 int mx, int my)
255{
256 int code;
257 MVTable *mv;
258
259 /* modulo encoding */
260 /* WARNING : you cannot reach all the MVs even with the modulo
261 encoding. This is a somewhat strange compromise they took !!! */
262 if (mx <= -64)
263 mx += 64;
264 else if (mx >= 64)
265 mx -= 64;
266 if (my <= -64)
267 my += 64;
268 else if (my >= 64)
269 my -= 64;
270
271 mx += 32;
272 my += 32;
273#if 0
274 if ((unsigned)mx >= 64 ||
275 (unsigned)my >= 64)
276 fprintf(stderr, "error mx=%d my=%d\n", mx, my);
277#endif
278 mv = &mv_tables[s->mv_table_index];
279
280 code = mv->table_mv_index[(mx << 6) | my];
281 set_stat(ST_MV);
282 put_bits(&s->pb,
283 mv->table_mv_bits[code],
284 mv->table_mv_code[code]);
285 if (code == mv->n) {
286 /* escape : code litterally */
287 put_bits(&s->pb, 6, mx);
288 put_bits(&s->pb, 6, my);
289 }
290}
291
292void msmpeg4_encode_mb(MpegEncContext * s,
293 DCTELEM block[6][64],
294 int motion_x, int motion_y)
295{
296 int cbp, coded_cbp, i;
297 int pred_x, pred_y;
298 UINT8 *coded_block;
299
300 if (!s->mb_intra) {
301 /* compute cbp */
302 set_stat(ST_INTER_MB);
303 cbp = 0;
304 for (i = 0; i < 6; i++) {
305 if (s->block_last_index[i] >= 0)
306 cbp |= 1 << (5 - i);
307 }
308 if (s->use_skip_mb_code && (cbp | motion_x | motion_y) == 0) {
309 /* skip macroblock */
310 put_bits(&s->pb, 1, 1);
311 return;
312 }
313 if (s->use_skip_mb_code)
314 put_bits(&s->pb, 1, 0); /* mb coded */
315
316 put_bits(&s->pb,
317 table_mb_non_intra[cbp + 64][1],
318 table_mb_non_intra[cbp + 64][0]);
319
320 /* motion vector */
321 h263_pred_motion(s, 0, &pred_x, &pred_y);
322 msmpeg4_encode_motion(s, motion_x - pred_x,
323 motion_y - pred_y);
324 } else {
325 /* compute cbp */
326 cbp = 0;
327 coded_cbp = 0;
328 for (i = 0; i < 6; i++) {
329 int val, pred;
330 val = (s->block_last_index[i] >= 1);
331 cbp |= val << (5 - i);
332 if (i < 4) {
333 /* predict value for close blocks only for luma */
334 pred = coded_block_pred(s, i, &coded_block);
335 *coded_block = val;
336 val = val ^ pred;
337 }
338 coded_cbp |= val << (5 - i);
339 }
340#if 0
341 if (coded_cbp)
342 printf("cbp=%x %x\n", cbp, coded_cbp);
343#endif
344
345 if (s->pict_type == I_TYPE) {
346 set_stat(ST_INTRA_MB);
347 put_bits(&s->pb,
348 table_mb_intra[coded_cbp][1], table_mb_intra[coded_cbp][0]);
349 } else {
350 if (s->use_skip_mb_code)
351 put_bits(&s->pb, 1, 0); /* mb coded */
352 put_bits(&s->pb,
353 table_mb_non_intra[cbp][1],
354 table_mb_non_intra[cbp][0]);
355 }
356 set_stat(ST_INTRA_MB);
357 put_bits(&s->pb, 1, 0); /* no AC prediction yet */
358 }
359
360 for (i = 0; i < 6; i++) {
361 msmpeg4_encode_block(s, block[i], i);
362 }
363}
364
365
366/* strongly inspirated from MPEG4, but not exactly the same ! */
367void msmpeg4_dc_scale(MpegEncContext * s)
368{
6fb904ce
MN
369 if (s->qscale < 5){
370 s->y_dc_scale = 8;
371 s->c_dc_scale = 8;
372// s->c_dc_scale = (s->qscale + 13)>>1;
373 }else if (s->qscale < 9){
374 s->y_dc_scale = 2 * s->qscale;
375 s->c_dc_scale = (s->qscale + 13)>>1;
376 }else{
377 s->y_dc_scale = s->qscale + 8;
378 s->c_dc_scale = (s->qscale + 13)>>1;
379 }
380 // this differs for quant >24 from mpeg4
381
382// if(s->qscale==13) s->c_dc_scale=14;
383
384// if(s->qscale>=6)
385// printf("%d", s->qscale);
386
387 /* s->c_dc_scale values (found by Michael Nidermayer)
388 qscale=2 -> 8 (yes iam sure about that)
389 qscale=3 -> 8
390 qscale=4 -> 8
391 qscale=5 -> 9
392 qscale=6 -> 9
393 qscale=7 -> 10
394 qscale=8 -> 10
395 qscale=9 -> 11
396 qscale=10-> 11
397 */
de6d9b64
FB
398}
399
400/* dir = 0: left, dir = 1: top prediction */
401static int msmpeg4_pred_dc(MpegEncContext * s, int n,
98be975d 402 INT16 **dc_val_ptr, int *dir_ptr)
de6d9b64 403{
dbbe8999 404 int a, b, c, wrap, pred, scale;
98be975d 405 INT16 *dc_val;
de6d9b64
FB
406
407 /* find prediction */
408 if (n < 4) {
de6d9b64
FB
409 scale = s->y_dc_scale;
410 } else {
de6d9b64
FB
411 scale = s->c_dc_scale;
412 }
dbbe8999
MN
413 wrap = s->block_wrap[n];
414 dc_val= s->dc_val[0] + s->block_index[n];
de6d9b64
FB
415
416 /* B C
417 * A X
418 */
dbbe8999
MN
419 a = dc_val[ - 1];
420 b = dc_val[ - 1 - wrap];
421 c = dc_val[ - wrap];
de6d9b64
FB
422
423 /* XXX: the following solution consumes divisions, but it does not
424 necessitate to modify mpegvideo.c. The problem comes from the
425 fact they decided to store the quantized DC (which would lead
426 to problems if Q could vary !) */
320680d4 427#if defined ARCH_X86 && !defined PIC
6f903d8e
MN
428 asm volatile(
429 "movl %3, %%eax \n\t"
430 "shrl $1, %%eax \n\t"
431 "addl %%eax, %2 \n\t"
432 "addl %%eax, %1 \n\t"
433 "addl %0, %%eax \n\t"
6fe84b43
MN
434 "mull %4 \n\t"
435 "movl %%edx, %0 \n\t"
6f903d8e 436 "movl %1, %%eax \n\t"
6fe84b43
MN
437 "mull %4 \n\t"
438 "movl %%edx, %1 \n\t"
6f903d8e 439 "movl %2, %%eax \n\t"
6fe84b43
MN
440 "mull %4 \n\t"
441 "movl %%edx, %2 \n\t"
fa778d53
MN
442 : "+b" (a), "+c" (b), "+D" (c)
443 : "g" (scale), "S" (inverse[scale])
6f903d8e
MN
444 : "%eax", "%edx"
445 );
320680d4
ZK
446#else
447 /* #elif defined (ARCH_ALPHA) */
1e98dffb 448 /* Divisions are extremely costly on Alpha; optimize the most
320680d4
ZK
449 common case. But they are costly everywhere...
450 */
1e98dffb
NK
451 if (scale == 8) {
452 a = (a + (8 >> 1)) / 8;
453 b = (b + (8 >> 1)) / 8;
454 c = (c + (8 >> 1)) / 8;
455 } else {
456 a = (a + (scale >> 1)) / scale;
457 b = (b + (scale >> 1)) / scale;
458 c = (c + (scale >> 1)) / scale;
459 }
6f903d8e 460#endif
de6d9b64
FB
461 /* XXX: WARNING: they did not choose the same test as MPEG4. This
462 is very important ! */
463 if (abs(a - b) <= abs(b - c)) {
464 pred = c;
465 *dir_ptr = 1;
466 } else {
467 pred = a;
468 *dir_ptr = 0;
469 }
470
471 /* update predictor */
dbbe8999 472 *dc_val_ptr = &dc_val[0];
de6d9b64
FB
473 return pred;
474}
475
476#define DC_MAX 119
477
478static void msmpeg4_encode_dc(MpegEncContext * s, int level, int n, int *dir_ptr)
479{
480 int sign, code;
481 int pred;
98be975d 482 INT16 *dc_val;
de6d9b64
FB
483
484 pred = msmpeg4_pred_dc(s, n, &dc_val, dir_ptr);
485
486 /* update predictor */
487 if (n < 4) {
488 *dc_val = level * s->y_dc_scale;
489 } else {
490 *dc_val = level * s->c_dc_scale;
491 }
492
493 /* do the prediction */
494 level -= pred;
495
496 sign = 0;
497 if (level < 0) {
498 level = -level;
499 sign = 1;
500 }
501
502 code = level;
503 if (code > DC_MAX)
504 code = DC_MAX;
505
506 if (s->dc_table_index == 0) {
507 if (n < 4) {
508 put_bits(&s->pb, table0_dc_lum[code][1], table0_dc_lum[code][0]);
509 } else {
510 put_bits(&s->pb, table0_dc_chroma[code][1], table0_dc_chroma[code][0]);
511 }
512 } else {
513 if (n < 4) {
514 put_bits(&s->pb, table1_dc_lum[code][1], table1_dc_lum[code][0]);
515 } else {
516 put_bits(&s->pb, table1_dc_chroma[code][1], table1_dc_chroma[code][0]);
517 }
518 }
519
520 if (code == DC_MAX)
521 put_bits(&s->pb, 8, level);
522
523 if (level != 0) {
524 put_bits(&s->pb, 1, sign);
525 }
526}
527
528/* Encoding of a block. Very similar to MPEG4 except for a different
529 escape coding (same as H263) and more vlc tables.
530 */
531static void msmpeg4_encode_block(MpegEncContext * s, DCTELEM * block, int n)
532{
533 int level, run, last, i, j, last_index;
534 int last_non_zero, sign, slevel;
535 int code, run_diff, dc_pred_dir;
536 const RLTable *rl;
537
538 if (s->mb_intra) {
539 set_stat(ST_DC);
540 msmpeg4_encode_dc(s, block[0], n, &dc_pred_dir);
541 i = 1;
542 if (n < 4) {
543 rl = &rl_table[s->rl_table_index];
544 } else {
545 rl = &rl_table[3 + s->rl_chroma_table_index];
546 }
547 run_diff = 0;
548 set_stat(ST_INTRA_AC);
549 } else {
550 i = 0;
551 rl = &rl_table[3 + s->rl_table_index];
552 run_diff = 1;
553 set_stat(ST_INTER_AC);
554 }
555
556 /* AC coefs */
557 last_index = s->block_last_index[n];
558 last_non_zero = i - 1;
559 for (; i <= last_index; i++) {
560 j = zigzag_direct[i];
561 level = block[j];
562 if (level) {
563 run = i - last_non_zero - 1;
564 last = (i == last_index);
565 sign = 0;
566 slevel = level;
567 if (level < 0) {
568 sign = 1;
569 level = -level;
570 }
571 code = get_rl_index(rl, last, run, level);
572 put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
573 if (code == rl->n) {
574 int level1, run1;
575
576 level1 = level - rl->max_level[last][run];
577 if (level1 < 1)
578 goto esc2;
579 code = get_rl_index(rl, last, run, level1);
580 if (code == rl->n) {
581 esc2:
582 put_bits(&s->pb, 1, 0);
583 if (level > MAX_LEVEL)
584 goto esc3;
585 run1 = run - rl->max_run[last][level] - run_diff;
586 if (run1 < 0)
587 goto esc3;
588 code = get_rl_index(rl, last, run1, level);
589 if (code == rl->n) {
590 esc3:
591 /* third escape */
592 put_bits(&s->pb, 1, 0);
593 put_bits(&s->pb, 1, last);
594 put_bits(&s->pb, 6, run);
595 put_bits(&s->pb, 8, slevel & 0xff);
596 } else {
597 /* second escape */
598 put_bits(&s->pb, 1, 1);
599 put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
600 put_bits(&s->pb, 1, sign);
601 }
602 } else {
603 /* first escape */
604 put_bits(&s->pb, 1, 1);
605 put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
606 put_bits(&s->pb, 1, sign);
607 }
608 } else {
609 put_bits(&s->pb, 1, sign);
610 }
611 last_non_zero = i;
612 }
613 }
614}
615
616/****************************************/
617/* decoding stuff */
618
619static VLC mb_non_intra_vlc;
620static VLC mb_intra_vlc;
621static VLC dc_lum_vlc[2];
622static VLC dc_chroma_vlc[2];
623
624/* init all vlc decoding tables */
625int msmpeg4_decode_init_vlc(MpegEncContext *s)
626{
627 int i;
628 MVTable *mv;
629
630 for(i=0;i<NB_RL_TABLES;i++) {
631 init_rl(&rl_table[i]);
632 init_vlc_rl(&rl_table[i]);
633 }
634 for(i=0;i<2;i++) {
635 mv = &mv_tables[i];
636 init_vlc(&mv->vlc, 9, mv->n + 1,
637 mv->table_mv_bits, 1, 1,
638 mv->table_mv_code, 2, 2);
639 }
640
641 init_vlc(&dc_lum_vlc[0], 9, 120,
642 &table0_dc_lum[0][1], 8, 4,
643 &table0_dc_lum[0][0], 8, 4);
644 init_vlc(&dc_chroma_vlc[0], 9, 120,
645 &table0_dc_chroma[0][1], 8, 4,
646 &table0_dc_chroma[0][0], 8, 4);
647 init_vlc(&dc_lum_vlc[1], 9, 120,
648 &table1_dc_lum[0][1], 8, 4,
649 &table1_dc_lum[0][0], 8, 4);
650 init_vlc(&dc_chroma_vlc[1], 9, 120,
651 &table1_dc_chroma[0][1], 8, 4,
652 &table1_dc_chroma[0][0], 8, 4);
653
654 init_vlc(&mb_non_intra_vlc, 9, 128,
655 &table_mb_non_intra[0][1], 8, 4,
656 &table_mb_non_intra[0][0], 8, 4);
2cb1773d 657 init_vlc(&mb_intra_vlc, 9, 64,
de6d9b64
FB
658 &table_mb_intra[0][1], 4, 2,
659 &table_mb_intra[0][0], 4, 2);
660 return 0;
661}
662
663static int decode012(GetBitContext *gb)
664{
665 int n;
612476ef 666 n = get_bits1(gb);
de6d9b64
FB
667 if (n == 0)
668 return 0;
669 else
612476ef 670 return get_bits1(gb) + 1;
de6d9b64
FB
671}
672
673int msmpeg4_decode_picture_header(MpegEncContext * s)
674{
675 int code;
676
677 s->pict_type = get_bits(&s->gb, 2) + 1;
678 if (s->pict_type != I_TYPE &&
679 s->pict_type != P_TYPE)
680 return -1;
681
682 s->qscale = get_bits(&s->gb, 5);
683
684 if (s->pict_type == I_TYPE) {
685 code = get_bits(&s->gb, 5);
686 /* 0x17: one slice, 0x18: three slices */
687 /* XXX: implement it */
badaf88e 688 //printf("%d %d %d\n", code, s->slice_height, s->first_slice_line);
de6d9b64
FB
689 if (code < 0x17)
690 return -1;
691 s->slice_height = s->mb_height / (code - 0x16);
692 s->rl_chroma_table_index = decode012(&s->gb);
693 s->rl_table_index = decode012(&s->gb);
694
612476ef 695 s->dc_table_index = get_bits1(&s->gb);
de6d9b64 696 s->no_rounding = 1;
badaf88e
MN
697/* printf(" %d %d %d %d \n",
698 s->qscale,
699 s->rl_chroma_table_index,
700 s->rl_table_index,
701 s->dc_table_index);*/
de6d9b64 702 } else {
612476ef 703 s->use_skip_mb_code = get_bits1(&s->gb);
de6d9b64
FB
704
705 s->rl_table_index = decode012(&s->gb);
706 s->rl_chroma_table_index = s->rl_table_index;
707
612476ef 708 s->dc_table_index = get_bits1(&s->gb);
de6d9b64 709
612476ef 710 s->mv_table_index = get_bits1(&s->gb);
badaf88e
MN
711/* printf(" %d %d %d %d %d \n",
712 s->use_skip_mb_code,
713 s->rl_table_index,
714 s->rl_chroma_table_index,
715 s->dc_table_index,
716 s->mv_table_index);*/
ae40484c
MN
717 if(s->flipflop_rounding){
718 s->no_rounding ^= 1;
719 }else{
720 s->no_rounding = 0;
721 }
722// printf("%d", s->no_rounding);
de6d9b64 723 }
ae40484c
MN
724
725
de6d9b64
FB
726#ifdef DEBUG
727 printf("*****frame %d:\n", frame_count++);
728#endif
729 return 0;
730}
731
ae40484c
MN
732int msmpeg4_decode_ext_header(MpegEncContext * s, int buf_size)
733{
ae40484c 734 /* the alt_bitstream reader could read over the end so we need to check it */
2b9ab1d5 735 if(get_bits_count(&s->gb) + 16 < buf_size*8)
ae40484c 736 {
2b9ab1d5
MN
737 int fps;
738
739 fps= get_bits(&s->gb, 5);
740 s->bitrate= get_bits(&s->gb, 11);
741 s->flipflop_rounding= get_bits1(&s->gb);
742
743// printf("fps:%2d bps:%2d roundingType:%1d\n", fps, s->bitrate, s->flipflop_rounding);
ae40484c
MN
744 }
745 else
746 {
2b9ab1d5
MN
747 s->flipflop_rounding= 0;
748 s->bitrate= 0;
ae40484c 749 }
2b9ab1d5 750
ae40484c
MN
751 return 0;
752}
753
38d171eb 754static inline void memsetw(short *tab, int val, int n)
de6d9b64
FB
755{
756 int i;
757 for(i=0;i<n;i++)
758 tab[i] = val;
759}
760
761int msmpeg4_decode_mb(MpegEncContext *s,
762 DCTELEM block[6][64])
763{
764 int cbp, code, i;
de6d9b64
FB
765 UINT8 *coded_val;
766
767 /* special slice handling */
768 if (s->mb_x == 0) {
525782f3 769 if (s->slice_height && (s->mb_y % s->slice_height) == 0) {
de6d9b64
FB
770 int wrap;
771 /* reset DC pred (set previous line to 1024) */
772 wrap = 2 * s->mb_width + 2;
773 memsetw(&s->dc_val[0][(1) + (2 * s->mb_y) * wrap],
774 1024, 2 * s->mb_width);
775 wrap = s->mb_width + 2;
776 memsetw(&s->dc_val[1][(1) + (s->mb_y) * wrap],
777 1024, s->mb_width);
778 memsetw(&s->dc_val[2][(1) + (s->mb_y) * wrap],
779 1024, s->mb_width);
3046f265
MN
780
781 /* reset AC pred (set previous line to 0) */
782 wrap = s->mb_width * 2 + 2;
783 memsetw(s->ac_val[0][0] + (1 + (2 * s->mb_y) * wrap)*16,
784 0, 2 * s->mb_width*16);
785 wrap = s->mb_width + 2;
786 memsetw(s->ac_val[1][0] + (1 + (s->mb_y) * wrap)*16,
787 0, s->mb_width*16);
788 memsetw(s->ac_val[2][0] + (1 + (s->mb_y) * wrap)*16,
789 0, s->mb_width*16);
de6d9b64
FB
790
791 s->first_slice_line = 1;
792 } else {
793 s->first_slice_line = 0;
794 }
795 }
796
797 if (s->pict_type == P_TYPE) {
798 set_stat(ST_INTER_MB);
799 if (s->use_skip_mb_code) {
612476ef 800 if (get_bits1(&s->gb)) {
de6d9b64
FB
801 /* skip mb */
802 s->mb_intra = 0;
803 for(i=0;i<6;i++)
804 s->block_last_index[i] = -1;
805 s->mv_dir = MV_DIR_FORWARD;
806 s->mv_type = MV_TYPE_16X16;
807 s->mv[0][0][0] = 0;
808 s->mv[0][0][1] = 0;
3bb4e23a 809 s->mb_skiped = 1;
de6d9b64
FB
810 return 0;
811 }
812 }
813
814 code = get_vlc(&s->gb, &mb_non_intra_vlc);
815 if (code < 0)
816 return -1;
38d171eb
ZK
817 //s->mb_intra = (code & 0x40) ? 0 : 1;
818 s->mb_intra = (~code & 0x40) >> 6;
de6d9b64
FB
819
820 cbp = code & 0x3f;
821 } else {
822 set_stat(ST_INTRA_MB);
823 s->mb_intra = 1;
824 code = get_vlc(&s->gb, &mb_intra_vlc);
825 if (code < 0)
826 return -1;
827 /* predict coded block pattern */
828 cbp = 0;
829 for(i=0;i<6;i++) {
38d171eb 830 int val = ((code >> (5 - i)) & 1);
de6d9b64 831 if (i < 4) {
38d171eb 832 int pred = coded_block_pred(s, i, &coded_val);
de6d9b64
FB
833 val = val ^ pred;
834 *coded_val = val;
835 }
836 cbp |= val << (5 - i);
837 }
838 }
839
840 if (!s->mb_intra) {
841 int mx, my;
842 set_stat(ST_MV);
843 h263_pred_motion(s, 0, &mx, &my);
844 if (msmpeg4_decode_motion(s, &mx, &my) < 0)
845 return -1;
846 s->mv_dir = MV_DIR_FORWARD;
847 s->mv_type = MV_TYPE_16X16;
848 s->mv[0][0][0] = mx;
849 s->mv[0][0][1] = my;
850 } else {
851 set_stat(ST_INTRA_MB);
612476ef 852 s->ac_pred = get_bits1(&s->gb);
de6d9b64
FB
853 }
854
855 for (i = 0; i < 6; i++) {
856 if (msmpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1) < 0)
38d171eb
ZK
857 {
858 fprintf(stderr,"\nIgnoring error while decoding block: %d x %d (%d)\n", s->mb_x, s->mb_y, i);
859 // return -1;
860 }
de6d9b64
FB
861 }
862 return 0;
863}
864
865static int msmpeg4_decode_block(MpegEncContext * s, DCTELEM * block,
866 int n, int coded)
867{
868 int code, level, i, j, last, run, run_diff;
869 int dc_pred_dir;
870 RLTable *rl;
871 const UINT8 *scan_table;
badaf88e 872 int qmul, qadd;
de6d9b64
FB
873
874 if (s->mb_intra) {
badaf88e
MN
875 qmul=1;
876 qadd=0;
877
de6d9b64
FB
878 /* DC coef */
879 set_stat(ST_DC);
880 level = msmpeg4_decode_dc(s, n, &dc_pred_dir);
881 if (level < 0)
882 return -1;
883 block[0] = level;
884 if (n < 4) {
885 rl = &rl_table[s->rl_table_index];
886 } else {
887 rl = &rl_table[3 + s->rl_chroma_table_index];
888 }
badaf88e 889
de6d9b64
FB
890 run_diff = 0;
891 i = 1;
892 if (!coded) {
893 goto not_coded;
894 }
895 if (s->ac_pred) {
896 if (dc_pred_dir == 0)
897 scan_table = ff_alternate_vertical_scan; /* left */
898 else
899 scan_table = ff_alternate_horizontal_scan; /* top */
900 } else {
901 scan_table = zigzag_direct;
902 }
903 set_stat(ST_INTRA_AC);
904 } else {
badaf88e
MN
905 qmul = s->qscale << 1;
906 qadd = (s->qscale - 1) | 1;
de6d9b64
FB
907 i = 0;
908 rl = &rl_table[3 + s->rl_table_index];
909 run_diff = 1;
910 if (!coded) {
911 s->block_last_index[n] = i - 1;
912 return 0;
913 }
914 scan_table = zigzag_direct;
915 set_stat(ST_INTER_AC);
916 }
917
918 for(;;) {
919 code = get_vlc(&s->gb, &rl->vlc);
920 if (code < 0)
921 return -1;
922 if (code == rl->n) {
923 /* escape */
612476ef
A
924 if (get_bits1(&s->gb) == 0) {
925 if (get_bits1(&s->gb) == 0) {
de6d9b64 926 /* third escape */
612476ef 927 last = get_bits1(&s->gb);
de6d9b64
FB
928 run = get_bits(&s->gb, 6);
929 level = get_bits(&s->gb, 8);
930 level = (level << 24) >> 24; /* sign extend */
38d171eb
ZK
931 //level = level * qmul + (level>0) * qadd - (level<=0) * qadd ;
932 if (level>0) level= level * qmul + qadd;
badaf88e 933 else level= level * qmul - qadd;
de6d9b64
FB
934 } else {
935 /* second escape */
936 code = get_vlc(&s->gb, &rl->vlc);
937 if (code < 0 || code >= rl->n)
938 return -1;
939 run = rl->table_run[code];
99180fe3 940 level = rl->table_level[code];
de6d9b64
FB
941 last = code >= rl->last;
942 run += rl->max_run[last][level] + run_diff;
99180fe3 943 level= level * qmul + qadd;
612476ef 944 if (get_bits1(&s->gb))
de6d9b64
FB
945 level = -level;
946 }
947 } else {
948 /* first escape */
949 code = get_vlc(&s->gb, &rl->vlc);
950 if (code < 0 || code >= rl->n)
951 return -1;
952 run = rl->table_run[code];
953 level = rl->table_level[code];
954 last = code >= rl->last;
955 level += rl->max_level[last][run];
badaf88e 956 level= level * qmul + qadd;
612476ef 957 if (get_bits1(&s->gb))
de6d9b64
FB
958 level = -level;
959 }
960 } else {
961 run = rl->table_run[code];
badaf88e 962 level = rl->table_level[code] * qmul + qadd;
de6d9b64 963 last = code >= rl->last;
612476ef 964 if (get_bits1(&s->gb))
de6d9b64
FB
965 level = -level;
966 }
967 i += run;
968 if (i >= 64)
969 return -1;
970 j = scan_table[i];
971 block[j] = level;
972 i++;
973 if (last)
974 break;
975 }
976 not_coded:
977 if (s->mb_intra) {
978 mpeg4_pred_ac(s, block, n, dc_pred_dir);
979 if (s->ac_pred) {
980 i = 64; /* XXX: not optimal */
981 }
982 }
983 s->block_last_index[n] = i - 1;
984
985 return 0;
986}
987
988static int msmpeg4_decode_dc(MpegEncContext * s, int n, int *dir_ptr)
989{
990 int level, pred;
98be975d 991 INT16 *dc_val;
de6d9b64
FB
992
993 if (n < 4) {
994 level = get_vlc(&s->gb, &dc_lum_vlc[s->dc_table_index]);
995 } else {
996 level = get_vlc(&s->gb, &dc_chroma_vlc[s->dc_table_index]);
997 }
998 if (level < 0)
999 return -1;
1000
1001 if (level == DC_MAX) {
1002 level = get_bits(&s->gb, 8);
612476ef 1003 if (get_bits1(&s->gb))
de6d9b64
FB
1004 level = -level;
1005 } else if (level != 0) {
612476ef 1006 if (get_bits1(&s->gb))
de6d9b64
FB
1007 level = -level;
1008 }
1009
1010 pred = msmpeg4_pred_dc(s, n, &dc_val, dir_ptr);
1011 level += pred;
1012
1013 /* update predictor */
1014 if (n < 4) {
1015 *dc_val = level * s->y_dc_scale;
1016 } else {
1017 *dc_val = level * s->c_dc_scale;
1018 }
1019
1020 return level;
1021}
1022
1023static int msmpeg4_decode_motion(MpegEncContext * s,
1024 int *mx_ptr, int *my_ptr)
1025{
1026 MVTable *mv;
1027 int code, mx, my;
1028
1029 mv = &mv_tables[s->mv_table_index];
1030
1031 code = get_vlc(&s->gb, &mv->vlc);
1032 if (code < 0)
1033 return -1;
1034 if (code == mv->n) {
1035 mx = get_bits(&s->gb, 6);
1036 my = get_bits(&s->gb, 6);
1037 } else {
1038 mx = mv->table_mvx[code];
1039 my = mv->table_mvy[code];
1040 }
1041
1042 mx += *mx_ptr - 32;
1043 my += *my_ptr - 32;
1044 /* WARNING : they do not do exactly modulo encoding */
1045 if (mx <= -64)
1046 mx += 64;
1047 else if (mx >= 64)
1048 mx -= 64;
1049
1050 if (my <= -64)
1051 my += 64;
1052 else if (my >= 64)
1053 my -= 64;
1054 *mx_ptr = mx;
1055 *my_ptr = my;
1056 return 0;
1057}