dump_stream support (nicely formated 0s and 1s)
[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
7f89b6fb 673int msmpeg4v2_decode_picture_header(MpegEncContext * s)
de6d9b64
FB
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);
7f89b6fb
MN
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
695 s->dc_table_index = get_bits1(&s->gb);*/
696 s->no_rounding = 1;
697 } else {
698 s->use_skip_mb_code = get_bits1(&s->gb);
699
700/* s->rl_table_index = decode012(&s->gb);
701 s->rl_chroma_table_index = s->rl_table_index;
702
703 s->dc_table_index = get_bits1(&s->gb);
704
705 s->mv_table_index = get_bits1(&s->gb);*/
706 if(s->flipflop_rounding){
707 s->no_rounding ^= 1;
708 }else{
709 s->no_rounding = 0;
710 }
711// printf("%d", s->no_rounding);
712 }
713
714printf("%s q:%d s:%X ", s->pict_type == I_TYPE ? "I" : "P" , s->qscale,
715 s->pict_type == I_TYPE ? code : s->use_skip_mb_code);
716
717 return 0;
718}
719
720int msmpeg4_decode_picture_header(MpegEncContext * s)
721{
722 int code;
723#if 0
724{
725int i;
726msmpeg4v2_decode_picture_header(s);
727for(i=0; i<s->gb.size*8; i++)
728 printf("%d", get_bits1(&s->gb));
729printf("END\n");
730return -1;
731}
732#endif
733 s->pict_type = get_bits(&s->gb, 2) + 1;
734 if (s->pict_type != I_TYPE &&
735 s->pict_type != P_TYPE)
736 return -1;
737
738 s->qscale = get_bits(&s->gb, 5);
739
740 if (s->pict_type == I_TYPE) {
741 code = get_bits(&s->gb, 5);
742 /* 0x17: one slice, 0x18: three slices */
743 /* XXX: implement it */
744 //printf("%d %d %d\n", code, s->slice_height, s->first_slice_line);
de6d9b64
FB
745 if (code < 0x17)
746 return -1;
747 s->slice_height = s->mb_height / (code - 0x16);
748 s->rl_chroma_table_index = decode012(&s->gb);
749 s->rl_table_index = decode012(&s->gb);
750
612476ef 751 s->dc_table_index = get_bits1(&s->gb);
de6d9b64 752 s->no_rounding = 1;
badaf88e
MN
753/* printf(" %d %d %d %d \n",
754 s->qscale,
755 s->rl_chroma_table_index,
756 s->rl_table_index,
757 s->dc_table_index);*/
de6d9b64 758 } else {
612476ef 759 s->use_skip_mb_code = get_bits1(&s->gb);
de6d9b64
FB
760
761 s->rl_table_index = decode012(&s->gb);
762 s->rl_chroma_table_index = s->rl_table_index;
763
612476ef 764 s->dc_table_index = get_bits1(&s->gb);
de6d9b64 765
612476ef 766 s->mv_table_index = get_bits1(&s->gb);
badaf88e
MN
767/* printf(" %d %d %d %d %d \n",
768 s->use_skip_mb_code,
769 s->rl_table_index,
770 s->rl_chroma_table_index,
771 s->dc_table_index,
772 s->mv_table_index);*/
ae40484c
MN
773 if(s->flipflop_rounding){
774 s->no_rounding ^= 1;
775 }else{
776 s->no_rounding = 0;
777 }
778// printf("%d", s->no_rounding);
de6d9b64 779 }
ae40484c
MN
780
781
de6d9b64
FB
782#ifdef DEBUG
783 printf("*****frame %d:\n", frame_count++);
784#endif
785 return 0;
786}
787
ae40484c
MN
788int msmpeg4_decode_ext_header(MpegEncContext * s, int buf_size)
789{
ae40484c 790 /* the alt_bitstream reader could read over the end so we need to check it */
2b9ab1d5 791 if(get_bits_count(&s->gb) + 16 < buf_size*8)
ae40484c 792 {
2b9ab1d5
MN
793 int fps;
794
795 fps= get_bits(&s->gb, 5);
796 s->bitrate= get_bits(&s->gb, 11);
797 s->flipflop_rounding= get_bits1(&s->gb);
798
799// printf("fps:%2d bps:%2d roundingType:%1d\n", fps, s->bitrate, s->flipflop_rounding);
ae40484c
MN
800 }
801 else
802 {
2b9ab1d5
MN
803 s->flipflop_rounding= 0;
804 s->bitrate= 0;
ae40484c 805 }
2b9ab1d5 806
ae40484c
MN
807 return 0;
808}
809
38d171eb 810static inline void memsetw(short *tab, int val, int n)
de6d9b64
FB
811{
812 int i;
813 for(i=0;i<n;i++)
814 tab[i] = val;
815}
816
817int msmpeg4_decode_mb(MpegEncContext *s,
818 DCTELEM block[6][64])
819{
820 int cbp, code, i;
de6d9b64
FB
821 UINT8 *coded_val;
822
823 /* special slice handling */
824 if (s->mb_x == 0) {
525782f3 825 if (s->slice_height && (s->mb_y % s->slice_height) == 0) {
de6d9b64
FB
826 int wrap;
827 /* reset DC pred (set previous line to 1024) */
828 wrap = 2 * s->mb_width + 2;
829 memsetw(&s->dc_val[0][(1) + (2 * s->mb_y) * wrap],
830 1024, 2 * s->mb_width);
831 wrap = s->mb_width + 2;
832 memsetw(&s->dc_val[1][(1) + (s->mb_y) * wrap],
833 1024, s->mb_width);
834 memsetw(&s->dc_val[2][(1) + (s->mb_y) * wrap],
835 1024, s->mb_width);
3046f265
MN
836
837 /* reset AC pred (set previous line to 0) */
838 wrap = s->mb_width * 2 + 2;
839 memsetw(s->ac_val[0][0] + (1 + (2 * s->mb_y) * wrap)*16,
840 0, 2 * s->mb_width*16);
841 wrap = s->mb_width + 2;
842 memsetw(s->ac_val[1][0] + (1 + (s->mb_y) * wrap)*16,
843 0, s->mb_width*16);
844 memsetw(s->ac_val[2][0] + (1 + (s->mb_y) * wrap)*16,
845 0, s->mb_width*16);
de6d9b64
FB
846
847 s->first_slice_line = 1;
848 } else {
849 s->first_slice_line = 0;
850 }
851 }
852
853 if (s->pict_type == P_TYPE) {
854 set_stat(ST_INTER_MB);
855 if (s->use_skip_mb_code) {
612476ef 856 if (get_bits1(&s->gb)) {
de6d9b64
FB
857 /* skip mb */
858 s->mb_intra = 0;
859 for(i=0;i<6;i++)
860 s->block_last_index[i] = -1;
861 s->mv_dir = MV_DIR_FORWARD;
862 s->mv_type = MV_TYPE_16X16;
863 s->mv[0][0][0] = 0;
864 s->mv[0][0][1] = 0;
3bb4e23a 865 s->mb_skiped = 1;
de6d9b64
FB
866 return 0;
867 }
868 }
869
870 code = get_vlc(&s->gb, &mb_non_intra_vlc);
871 if (code < 0)
872 return -1;
38d171eb
ZK
873 //s->mb_intra = (code & 0x40) ? 0 : 1;
874 s->mb_intra = (~code & 0x40) >> 6;
de6d9b64
FB
875
876 cbp = code & 0x3f;
877 } else {
878 set_stat(ST_INTRA_MB);
879 s->mb_intra = 1;
880 code = get_vlc(&s->gb, &mb_intra_vlc);
881 if (code < 0)
882 return -1;
883 /* predict coded block pattern */
884 cbp = 0;
885 for(i=0;i<6;i++) {
38d171eb 886 int val = ((code >> (5 - i)) & 1);
de6d9b64 887 if (i < 4) {
38d171eb 888 int pred = coded_block_pred(s, i, &coded_val);
de6d9b64
FB
889 val = val ^ pred;
890 *coded_val = val;
891 }
892 cbp |= val << (5 - i);
893 }
894 }
895
896 if (!s->mb_intra) {
897 int mx, my;
898 set_stat(ST_MV);
899 h263_pred_motion(s, 0, &mx, &my);
900 if (msmpeg4_decode_motion(s, &mx, &my) < 0)
901 return -1;
902 s->mv_dir = MV_DIR_FORWARD;
903 s->mv_type = MV_TYPE_16X16;
904 s->mv[0][0][0] = mx;
905 s->mv[0][0][1] = my;
906 } else {
907 set_stat(ST_INTRA_MB);
612476ef 908 s->ac_pred = get_bits1(&s->gb);
de6d9b64
FB
909 }
910
911 for (i = 0; i < 6; i++) {
912 if (msmpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1) < 0)
38d171eb
ZK
913 {
914 fprintf(stderr,"\nIgnoring error while decoding block: %d x %d (%d)\n", s->mb_x, s->mb_y, i);
915 // return -1;
916 }
de6d9b64
FB
917 }
918 return 0;
919}
920
921static int msmpeg4_decode_block(MpegEncContext * s, DCTELEM * block,
922 int n, int coded)
923{
924 int code, level, i, j, last, run, run_diff;
925 int dc_pred_dir;
926 RLTable *rl;
927 const UINT8 *scan_table;
badaf88e 928 int qmul, qadd;
de6d9b64
FB
929
930 if (s->mb_intra) {
badaf88e
MN
931 qmul=1;
932 qadd=0;
933
de6d9b64
FB
934 /* DC coef */
935 set_stat(ST_DC);
936 level = msmpeg4_decode_dc(s, n, &dc_pred_dir);
937 if (level < 0)
938 return -1;
939 block[0] = level;
940 if (n < 4) {
941 rl = &rl_table[s->rl_table_index];
942 } else {
943 rl = &rl_table[3 + s->rl_chroma_table_index];
944 }
badaf88e 945
de6d9b64
FB
946 run_diff = 0;
947 i = 1;
948 if (!coded) {
949 goto not_coded;
950 }
951 if (s->ac_pred) {
952 if (dc_pred_dir == 0)
953 scan_table = ff_alternate_vertical_scan; /* left */
954 else
955 scan_table = ff_alternate_horizontal_scan; /* top */
956 } else {
957 scan_table = zigzag_direct;
958 }
959 set_stat(ST_INTRA_AC);
960 } else {
badaf88e
MN
961 qmul = s->qscale << 1;
962 qadd = (s->qscale - 1) | 1;
de6d9b64
FB
963 i = 0;
964 rl = &rl_table[3 + s->rl_table_index];
965 run_diff = 1;
966 if (!coded) {
967 s->block_last_index[n] = i - 1;
968 return 0;
969 }
970 scan_table = zigzag_direct;
971 set_stat(ST_INTER_AC);
972 }
973
974 for(;;) {
975 code = get_vlc(&s->gb, &rl->vlc);
976 if (code < 0)
977 return -1;
978 if (code == rl->n) {
979 /* escape */
612476ef
A
980 if (get_bits1(&s->gb) == 0) {
981 if (get_bits1(&s->gb) == 0) {
de6d9b64 982 /* third escape */
612476ef 983 last = get_bits1(&s->gb);
de6d9b64
FB
984 run = get_bits(&s->gb, 6);
985 level = get_bits(&s->gb, 8);
986 level = (level << 24) >> 24; /* sign extend */
38d171eb
ZK
987 //level = level * qmul + (level>0) * qadd - (level<=0) * qadd ;
988 if (level>0) level= level * qmul + qadd;
badaf88e 989 else level= level * qmul - qadd;
de6d9b64
FB
990 } else {
991 /* second escape */
992 code = get_vlc(&s->gb, &rl->vlc);
993 if (code < 0 || code >= rl->n)
994 return -1;
995 run = rl->table_run[code];
99180fe3 996 level = rl->table_level[code];
de6d9b64
FB
997 last = code >= rl->last;
998 run += rl->max_run[last][level] + run_diff;
99180fe3 999 level= level * qmul + qadd;
612476ef 1000 if (get_bits1(&s->gb))
de6d9b64
FB
1001 level = -level;
1002 }
1003 } else {
1004 /* first escape */
1005 code = get_vlc(&s->gb, &rl->vlc);
1006 if (code < 0 || code >= rl->n)
1007 return -1;
1008 run = rl->table_run[code];
1009 level = rl->table_level[code];
1010 last = code >= rl->last;
1011 level += rl->max_level[last][run];
badaf88e 1012 level= level * qmul + qadd;
612476ef 1013 if (get_bits1(&s->gb))
de6d9b64
FB
1014 level = -level;
1015 }
1016 } else {
1017 run = rl->table_run[code];
badaf88e 1018 level = rl->table_level[code] * qmul + qadd;
de6d9b64 1019 last = code >= rl->last;
612476ef 1020 if (get_bits1(&s->gb))
de6d9b64
FB
1021 level = -level;
1022 }
1023 i += run;
1024 if (i >= 64)
1025 return -1;
1026 j = scan_table[i];
1027 block[j] = level;
1028 i++;
1029 if (last)
1030 break;
1031 }
1032 not_coded:
1033 if (s->mb_intra) {
1034 mpeg4_pred_ac(s, block, n, dc_pred_dir);
1035 if (s->ac_pred) {
1036 i = 64; /* XXX: not optimal */
1037 }
1038 }
1039 s->block_last_index[n] = i - 1;
1040
1041 return 0;
1042}
1043
1044static int msmpeg4_decode_dc(MpegEncContext * s, int n, int *dir_ptr)
1045{
1046 int level, pred;
98be975d 1047 INT16 *dc_val;
de6d9b64
FB
1048
1049 if (n < 4) {
1050 level = get_vlc(&s->gb, &dc_lum_vlc[s->dc_table_index]);
1051 } else {
1052 level = get_vlc(&s->gb, &dc_chroma_vlc[s->dc_table_index]);
1053 }
1054 if (level < 0)
1055 return -1;
1056
1057 if (level == DC_MAX) {
1058 level = get_bits(&s->gb, 8);
612476ef 1059 if (get_bits1(&s->gb))
de6d9b64
FB
1060 level = -level;
1061 } else if (level != 0) {
612476ef 1062 if (get_bits1(&s->gb))
de6d9b64
FB
1063 level = -level;
1064 }
1065
1066 pred = msmpeg4_pred_dc(s, n, &dc_val, dir_ptr);
1067 level += pred;
1068
1069 /* update predictor */
1070 if (n < 4) {
1071 *dc_val = level * s->y_dc_scale;
1072 } else {
1073 *dc_val = level * s->c_dc_scale;
1074 }
1075
1076 return level;
1077}
1078
1079static int msmpeg4_decode_motion(MpegEncContext * s,
1080 int *mx_ptr, int *my_ptr)
1081{
1082 MVTable *mv;
1083 int code, mx, my;
1084
1085 mv = &mv_tables[s->mv_table_index];
1086
1087 code = get_vlc(&s->gb, &mv->vlc);
1088 if (code < 0)
1089 return -1;
1090 if (code == mv->n) {
1091 mx = get_bits(&s->gb, 6);
1092 my = get_bits(&s->gb, 6);
1093 } else {
1094 mx = mv->table_mvx[code];
1095 my = mv->table_mvy[code];
1096 }
1097
1098 mx += *mx_ptr - 32;
1099 my += *my_ptr - 32;
1100 /* WARNING : they do not do exactly modulo encoding */
1101 if (mx <= -64)
1102 mx += 64;
1103 else if (mx >= 64)
1104 mx -= 64;
1105
1106 if (my <= -64)
1107 my += 64;
1108 else if (my >= 64)
1109 my -= 64;
1110 *mx_ptr = mx;
1111 *my_ptr = my;
1112 return 0;
1113}