552430adfc720a47401696c9d3fa84b406554167
[libav.git] / libavcodec / msmpeg4.c
1 /*
2 * MSMPEG4 backend for ffmpeg encoder and decoder
3 * Copyright (c) 2001 Fabrice Bellard.
4 *
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.
9 *
10 * This library 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 GNU
13 * Lesser General Public License for more details.
14 *
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
18 *
19 * msmpeg4v1 & v2 stuff by Michael Niedermayer <michaelni@gmx.at>
20 */
21 #include "avcodec.h"
22 #include "dsputil.h"
23 #include "mpegvideo.h"
24 //#define PRINT_MB
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 */
33 //#define DEBUG
34
35 static UINT32 v2_dc_lum_table[512][2];
36 static UINT32 v2_dc_chroma_table[512][2];
37
38 static inline void msmpeg4_encode_block(MpegEncContext * s, DCTELEM * block, int n);
39 static inline int msmpeg4_decode_block(MpegEncContext * s, DCTELEM * block,
40 int n, int coded);
41 static int msmpeg4_decode_dc(MpegEncContext * s, int n, int *dir_ptr);
42 static int msmpeg4_decode_motion(MpegEncContext * s,
43 int *mx_ptr, int *my_ptr);
44 static void msmpeg4v2_encode_motion(MpegEncContext * s, int val);
45 static void init_h263_dc_for_msmpeg4(void);
46 static inline void msmpeg4_memsetw(short *tab, int val, int n);
47
48
49
50 extern UINT32 inverse[256];
51
52 #ifdef DEBUG
53 int intra_count = 0;
54 int frame_count = 0;
55 #endif
56
57 #include "msmpeg4data.h"
58
59 #ifdef STATS
60
61 const char *st_names[ST_NB] = {
62 "unknown",
63 "dc",
64 "intra_ac",
65 "inter_ac",
66 "intra_mb",
67 "inter_mb",
68 "mv",
69 };
70
71 int st_current_index = 0;
72 unsigned int st_bit_counts[ST_NB];
73 unsigned int st_out_bit_counts[ST_NB];
74
75 #define set_stat(var) st_current_index = var;
76
77 void print_stats(void)
78 {
79 unsigned int total;
80 int i;
81
82 printf("Input:\n");
83 total = 0;
84 for(i=0;i<ST_NB;i++)
85 total += st_bit_counts[i];
86 if (total == 0)
87 total = 1;
88 for(i=0;i<ST_NB;i++) {
89 printf("%-10s : %10.1f %5.1f%%\n",
90 st_names[i],
91 (double)st_bit_counts[i] / 8.0,
92 (double)st_bit_counts[i] * 100.0 / total);
93 }
94 printf("%-10s : %10.1f %5.1f%%\n",
95 "total",
96 (double)total / 8.0,
97 100.0);
98
99 printf("Output:\n");
100 total = 0;
101 for(i=0;i<ST_NB;i++)
102 total += st_out_bit_counts[i];
103 if (total == 0)
104 total = 1;
105 for(i=0;i<ST_NB;i++) {
106 printf("%-10s : %10.1f %5.1f%%\n",
107 st_names[i],
108 (double)st_out_bit_counts[i] / 8.0,
109 (double)st_out_bit_counts[i] * 100.0 / total);
110 }
111 printf("%-10s : %10.1f %5.1f%%\n",
112 "total",
113 (double)total / 8.0,
114 100.0);
115 }
116
117 #else
118
119 #define set_stat(var)
120
121 #endif
122
123 static void common_init(MpegEncContext * s)
124 {
125 static int inited=0;
126
127 switch(s->msmpeg4_version){
128 case 1:
129 case 2:
130 s->y_dc_scale_table=
131 s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
132 break;
133 case 3:
134 if(s->workaround_bugs){
135 s->y_dc_scale_table= old_ff_y_dc_scale_table;
136 s->c_dc_scale_table= old_ff_c_dc_scale_table;
137 } else{
138 s->y_dc_scale_table= ff_mpeg4_y_dc_scale_table;
139 s->c_dc_scale_table= ff_mpeg4_c_dc_scale_table;
140 }
141 break;
142 case 4:
143 s->y_dc_scale_table= wmv1_y_dc_scale_table;
144 s->c_dc_scale_table= wmv1_c_dc_scale_table;
145 break;
146 }
147
148 if(s->msmpeg4_version==4){
149 s->intra_scantable = wmv1_scantable[1];
150 s->intra_h_scantable= wmv1_scantable[2];
151 s->intra_v_scantable= wmv1_scantable[3];
152 s->inter_scantable = wmv1_scantable[0];
153 }else{
154 s->intra_scantable = zigzag_direct;
155 s->intra_h_scantable= ff_alternate_horizontal_scan;
156 s->intra_v_scantable= ff_alternate_vertical_scan;
157 s->inter_scantable = zigzag_direct;
158 }
159
160 if(!inited){
161 int i;
162 inited=1;
163
164 init_h263_dc_for_msmpeg4();
165
166 /* permute for IDCT */
167 for(i=0; i<WMV1_SCANTABLE_COUNT; i++){
168 int k;
169 for(k=0;k<64;k++) {
170 int j = wmv1_scantable[i][k];
171 wmv1_scantable[i][k]= block_permute_op(j);
172 }
173 }
174
175 }
176 }
177
178 /* build the table which associate a (x,y) motion vector to a vlc */
179 static void init_mv_table(MVTable *tab)
180 {
181 int i, x, y;
182
183 tab->table_mv_index = av_malloc(sizeof(UINT16) * 4096);
184 /* mark all entries as not used */
185 for(i=0;i<4096;i++)
186 tab->table_mv_index[i] = tab->n;
187
188 for(i=0;i<tab->n;i++) {
189 x = tab->table_mvx[i];
190 y = tab->table_mvy[i];
191 tab->table_mv_index[(x << 6) | y] = i;
192 }
193 }
194
195 static void code012(PutBitContext *pb, int n)
196 {
197 if (n == 0) {
198 put_bits(pb, 1, 0);
199 } else {
200 put_bits(pb, 1, 1);
201 put_bits(pb, 1, (n >= 2));
202 }
203 }
204
205 void ff_msmpeg4_encode_init(MpegEncContext *s)
206 {
207 static int init_done=0;
208 int i;
209
210 common_init(s);
211 if(s->msmpeg4_version>=4){
212 s->min_qcoeff= -255;
213 s->max_qcoeff= 255;
214 }
215
216 if (!init_done) {
217 /* init various encoding tables */
218 init_done = 1;
219 init_mv_table(&mv_tables[0]);
220 init_mv_table(&mv_tables[1]);
221 for(i=0;i<NB_RL_TABLES;i++)
222 init_rl(&rl_table[i]);
223 }
224 }
225
226 static int get_size_of_code(MpegEncContext * s, RLTable *rl, int last, int run, int level, int intra){
227 int size=0;
228 int code;
229 int run_diff= intra ? 0 : 1;
230
231 code = get_rl_index(rl, last, run, level);
232 size+= rl->table_vlc[code][1];
233 if (code == rl->n) {
234 int level1, run1;
235
236 level1 = level - rl->max_level[last][run];
237 if (level1 < 1)
238 goto esc2;
239 code = get_rl_index(rl, last, run, level1);
240 if (code == rl->n) {
241 esc2:
242 size++;
243 if (level > MAX_LEVEL)
244 goto esc3;
245 run1 = run - rl->max_run[last][level] - run_diff;
246 if (run1 < 0)
247 goto esc3;
248 code = get_rl_index(rl, last, run1, level);
249 if (code == rl->n) {
250 esc3:
251 /* third escape */
252 size+=1+1+6+8;
253 } else {
254 /* second escape */
255 size+= 1+1+ rl->table_vlc[code][1];
256 }
257 } else {
258 /* first escape */
259 size+= 1+1+ rl->table_vlc[code][1];
260 }
261 } else {
262 size++;
263 }
264 return size;
265 }
266
267 static void find_best_tables(MpegEncContext * s)
268 {
269 int i;
270 int best =-1, best_size =9999999;
271 int chroma_best=-1, best_chroma_size=9999999;
272 int last_size=0;
273
274 for(i=0; i<3; i++){
275 int level;
276 int chroma_size=0;
277 int size=0;
278
279 if(i>0){// ;)
280 size++;
281 chroma_size++;
282 }
283 for(level=0; level<=MAX_LEVEL; level++){
284 int run;
285 for(run=0; run<=MAX_RUN; run++){
286 int last;
287 for(last=0; last<2; last++){
288 int inter_count = s->ac_stats[0][0][level][run][last] + s->ac_stats[0][1][level][run][last];
289 int intra_luma_count = s->ac_stats[1][0][level][run][last];
290 int intra_chroma_count= s->ac_stats[1][1][level][run][last];
291
292 if(s->pict_type==I_TYPE){
293 size += intra_luma_count *get_size_of_code(s, &rl_table[ i], last, run, level,1);
294 chroma_size+= intra_chroma_count*get_size_of_code(s, &rl_table[3+i], last, run, level,1);
295 }else{
296 size+= intra_luma_count *get_size_of_code(s, &rl_table[ i], last, run, level,1)
297 +intra_chroma_count*get_size_of_code(s, &rl_table[3+i], last, run, level,1)
298 +inter_count *get_size_of_code(s, &rl_table[3+i], last, run, level,0);
299 }
300 }
301 }
302 }
303 if(size<best_size){
304 best_size= size;
305 best= i;
306 }
307 if(chroma_size<best_chroma_size){
308 best_chroma_size= chroma_size;
309 chroma_best= i;
310 }
311 }
312 // printf("type:%d, best:%d, qp:%d, var:%d, mcvar:%d, size:%d //\n",
313 // s->pict_type, best, s->qscale, s->mb_var_sum, s->mc_mb_var_sum, best_size);
314
315 if(s->pict_type==P_TYPE) chroma_best= best;
316
317 memset(s->ac_stats, 0, sizeof(int)*(MAX_LEVEL+1)*(MAX_RUN+1)*2*2*2);
318
319 s->rl_table_index = best;
320 s->rl_chroma_table_index= chroma_best;
321
322 if(s->pict_type != s->last_non_b_pict_type){
323 s->rl_table_index= 2;
324 if(s->pict_type==I_TYPE)
325 s->rl_chroma_table_index= 1;
326 else
327 s->rl_chroma_table_index= 2;
328 }
329
330 }
331
332 /* write MSMPEG4 compatible frame header */
333 void msmpeg4_encode_picture_header(MpegEncContext * s, int picture_number)
334 {
335 find_best_tables(s);
336
337 align_put_bits(&s->pb);
338 put_bits(&s->pb, 2, s->pict_type - 1);
339
340 put_bits(&s->pb, 5, s->qscale);
341 if(s->msmpeg4_version<=2){
342 s->rl_table_index = 2;
343 s->rl_chroma_table_index = 2;
344 }
345
346 s->dc_table_index = 1;
347 s->mv_table_index = 1; /* only if P frame */
348 s->use_skip_mb_code = 1; /* only if P frame */
349 s->per_mb_rl_table = 0;
350 s->inter_intra_pred= (s->width*s->height < 320*240 && s->bit_rate<=128 && s->pict_type==P_TYPE);
351
352 if (s->pict_type == I_TYPE) {
353 s->no_rounding = 1;
354 s->slice_height= s->mb_height/1;
355 put_bits(&s->pb, 5, 0x16 + s->mb_height/s->slice_height);
356
357 if(s->msmpeg4_version==4){
358 msmpeg4_encode_ext_header(s);
359 if(s->bit_rate>50)
360 put_bits(&s->pb, 1, s->per_mb_rl_table);
361 }
362
363 if(s->msmpeg4_version>2){
364 if(!s->per_mb_rl_table){
365 code012(&s->pb, s->rl_chroma_table_index);
366 code012(&s->pb, s->rl_table_index);
367 }
368
369 put_bits(&s->pb, 1, s->dc_table_index);
370 }
371 } else {
372 put_bits(&s->pb, 1, s->use_skip_mb_code);
373
374 if(s->msmpeg4_version==4 && s->bit_rate>50)
375 put_bits(&s->pb, 1, s->per_mb_rl_table);
376
377 if(s->msmpeg4_version>2){
378 if(!s->per_mb_rl_table)
379 code012(&s->pb, s->rl_table_index);
380
381 put_bits(&s->pb, 1, s->dc_table_index);
382
383 put_bits(&s->pb, 1, s->mv_table_index);
384 }
385
386 if(s->flipflop_rounding){
387 s->no_rounding ^= 1;
388 }else{
389 s->no_rounding = 0;
390 }
391 }
392
393 s->esc3_level_length= 0;
394 s->esc3_run_length= 0;
395
396 #ifdef DEBUG
397 intra_count = 0;
398 printf("*****frame %d:\n", frame_count++);
399 #endif
400 }
401
402 void msmpeg4_encode_ext_header(MpegEncContext * s)
403 {
404 put_bits(&s->pb, 5, s->frame_rate / FRAME_RATE_BASE); //yes 29.97 -> 29
405
406 put_bits(&s->pb, 11, MIN(s->bit_rate, 2047));
407
408 if(s->msmpeg4_version<3)
409 s->flipflop_rounding=0;
410 else{
411 s->flipflop_rounding=1;
412 put_bits(&s->pb, 1, s->flipflop_rounding);
413 }
414 }
415
416 /* predict coded block */
417 static inline int coded_block_pred(MpegEncContext * s, int n, UINT8 **coded_block_ptr)
418 {
419 int xy, wrap, pred, a, b, c;
420
421 xy = s->block_index[n];
422 wrap = s->block_wrap[0];
423
424 /* B C
425 * A X
426 */
427 a = s->coded_block[xy - 1 ];
428 b = s->coded_block[xy - 1 - wrap];
429 c = s->coded_block[xy - wrap];
430
431 if (b == c) {
432 pred = a;
433 } else {
434 pred = c;
435 }
436
437 /* store value */
438 *coded_block_ptr = &s->coded_block[xy];
439
440 return pred;
441 }
442
443 static void msmpeg4_encode_motion(MpegEncContext * s,
444 int mx, int my)
445 {
446 int code;
447 MVTable *mv;
448
449 /* modulo encoding */
450 /* WARNING : you cannot reach all the MVs even with the modulo
451 encoding. This is a somewhat strange compromise they took !!! */
452 if (mx <= -64)
453 mx += 64;
454 else if (mx >= 64)
455 mx -= 64;
456 if (my <= -64)
457 my += 64;
458 else if (my >= 64)
459 my -= 64;
460
461 mx += 32;
462 my += 32;
463 #if 0
464 if ((unsigned)mx >= 64 ||
465 (unsigned)my >= 64)
466 fprintf(stderr, "error mx=%d my=%d\n", mx, my);
467 #endif
468 mv = &mv_tables[s->mv_table_index];
469
470 code = mv->table_mv_index[(mx << 6) | my];
471 set_stat(ST_MV);
472 put_bits(&s->pb,
473 mv->table_mv_bits[code],
474 mv->table_mv_code[code]);
475 if (code == mv->n) {
476 /* escape : code litterally */
477 put_bits(&s->pb, 6, mx);
478 put_bits(&s->pb, 6, my);
479 }
480 }
481
482 static inline void handle_slices(MpegEncContext *s){
483 if (s->mb_x == 0) {
484 if (s->slice_height && (s->mb_y % s->slice_height) == 0) {
485 if(s->msmpeg4_version != 4){
486 int wrap;
487 /* reset DC pred (set previous line to 1024) */
488 wrap = 2 * s->mb_width + 2;
489 msmpeg4_memsetw(&s->dc_val[0][(1) + (2 * s->mb_y) * wrap],
490 1024, 2 * s->mb_width);
491 wrap = s->mb_width + 2;
492 msmpeg4_memsetw(&s->dc_val[1][(1) + (s->mb_y) * wrap],
493 1024, s->mb_width);
494 msmpeg4_memsetw(&s->dc_val[2][(1) + (s->mb_y) * wrap],
495 1024, s->mb_width);
496
497 /* reset AC pred (set previous line to 0) */
498 wrap = s->mb_width * 2 + 2;
499 msmpeg4_memsetw(s->ac_val[0][0] + (1 + (2 * s->mb_y) * wrap)*16,
500 0, 2 * s->mb_width*16);
501 wrap = s->mb_width + 2;
502 msmpeg4_memsetw(s->ac_val[1][0] + (1 + (s->mb_y) * wrap)*16,
503 0, s->mb_width*16);
504 msmpeg4_memsetw(s->ac_val[2][0] + (1 + (s->mb_y) * wrap)*16,
505 0, s->mb_width*16);
506 }
507 s->first_slice_line = 1;
508 } else {
509 s->first_slice_line = 0;
510 }
511 }
512 }
513
514 void msmpeg4_encode_mb(MpegEncContext * s,
515 DCTELEM block[6][64],
516 int motion_x, int motion_y)
517 {
518 int cbp, coded_cbp, i;
519 int pred_x, pred_y;
520 UINT8 *coded_block;
521
522 handle_slices(s);
523
524 if (!s->mb_intra) {
525 /* compute cbp */
526 set_stat(ST_INTER_MB);
527 cbp = 0;
528 for (i = 0; i < 6; i++) {
529 if (s->block_last_index[i] >= 0)
530 cbp |= 1 << (5 - i);
531 }
532 if (s->use_skip_mb_code && (cbp | motion_x | motion_y) == 0) {
533 /* skip macroblock */
534 put_bits(&s->pb, 1, 1);
535 return;
536 }
537 if (s->use_skip_mb_code)
538 put_bits(&s->pb, 1, 0); /* mb coded */
539
540 if(s->msmpeg4_version<=2){
541 put_bits(&s->pb,
542 v2_mb_type[cbp&3][1],
543 v2_mb_type[cbp&3][0]);
544 if((cbp&3) != 3) coded_cbp= cbp ^ 0x3C;
545 else coded_cbp= cbp;
546
547 put_bits(&s->pb,
548 cbpy_tab[coded_cbp>>2][1],
549 cbpy_tab[coded_cbp>>2][0]);
550
551 h263_pred_motion(s, 0, &pred_x, &pred_y);
552 msmpeg4v2_encode_motion(s, motion_x - pred_x);
553 msmpeg4v2_encode_motion(s, motion_y - pred_y);
554 }else{
555 put_bits(&s->pb,
556 table_mb_non_intra[cbp + 64][1],
557 table_mb_non_intra[cbp + 64][0]);
558
559 /* motion vector */
560 h263_pred_motion(s, 0, &pred_x, &pred_y);
561 msmpeg4_encode_motion(s, motion_x - pred_x,
562 motion_y - pred_y);
563 }
564 } else {
565 /* compute cbp */
566 cbp = 0;
567 coded_cbp = 0;
568 for (i = 0; i < 6; i++) {
569 int val, pred;
570 val = (s->block_last_index[i] >= 1);
571 cbp |= val << (5 - i);
572 if (i < 4) {
573 /* predict value for close blocks only for luma */
574 pred = coded_block_pred(s, i, &coded_block);
575 *coded_block = val;
576 val = val ^ pred;
577 }
578 coded_cbp |= val << (5 - i);
579 }
580 #if 0
581 if (coded_cbp)
582 printf("cbp=%x %x\n", cbp, coded_cbp);
583 #endif
584
585 if(s->msmpeg4_version<=2){
586 if (s->pict_type == I_TYPE) {
587 put_bits(&s->pb,
588 v2_intra_cbpc[cbp&3][1], v2_intra_cbpc[cbp&3][0]);
589 } else {
590 if (s->use_skip_mb_code)
591 put_bits(&s->pb, 1, 0); /* mb coded */
592 put_bits(&s->pb,
593 v2_mb_type[(cbp&3) + 4][1],
594 v2_mb_type[(cbp&3) + 4][0]);
595 }
596 put_bits(&s->pb, 1, 0); /* no AC prediction yet */
597 put_bits(&s->pb,
598 cbpy_tab[cbp>>2][1],
599 cbpy_tab[cbp>>2][0]);
600 }else{
601 if (s->pict_type == I_TYPE) {
602 set_stat(ST_INTRA_MB);
603 put_bits(&s->pb,
604 table_mb_intra[coded_cbp][1], table_mb_intra[coded_cbp][0]);
605 } else {
606 if (s->use_skip_mb_code)
607 put_bits(&s->pb, 1, 0); /* mb coded */
608 put_bits(&s->pb,
609 table_mb_non_intra[cbp][1],
610 table_mb_non_intra[cbp][0]);
611 }
612 set_stat(ST_INTRA_MB);
613 put_bits(&s->pb, 1, 0); /* no AC prediction yet */
614 }
615 }
616
617 for (i = 0; i < 6; i++) {
618 msmpeg4_encode_block(s, block[i], i);
619 }
620 }
621
622 /* old ffmpeg msmpeg4v3 mode */
623 void ff_old_msmpeg4_dc_scale(MpegEncContext * s)
624 {
625 if (s->qscale < 5){
626 s->y_dc_scale = 8;
627 s->c_dc_scale = 8;
628 }else if (s->qscale < 9){
629 s->y_dc_scale = 2 * s->qscale;
630 s->c_dc_scale = (s->qscale + 13)>>1;
631 }else{
632 s->y_dc_scale = s->qscale + 8;
633 s->c_dc_scale = (s->qscale + 13)>>1;
634 }
635 }
636
637 static inline int msmpeg4v1_pred_dc(MpegEncContext * s, int n,
638 INT32 **dc_val_ptr)
639 {
640 int i;
641
642 if (n < 4) {
643 i= 0;
644 } else {
645 i= n-3;
646 }
647
648 *dc_val_ptr= &s->last_dc[i];
649 return s->last_dc[i];
650 }
651
652 static int get_dc(uint8_t *src, int stride, int scale)
653 {
654 int y;
655 int sum=0;
656 for(y=0; y<8; y++){
657 int x;
658 for(x=0; x<8; x++){
659 sum+=src[x + y*stride];
660 }
661 }
662 return (sum + (scale>>1))/scale;
663 }
664
665 /* dir = 0: left, dir = 1: top prediction */
666 static inline int msmpeg4_pred_dc(MpegEncContext * s, int n,
667 UINT16 **dc_val_ptr, int *dir_ptr)
668 {
669 int a, b, c, wrap, pred, scale;
670 INT16 *dc_val;
671
672 /* find prediction */
673 if (n < 4) {
674 scale = s->y_dc_scale;
675 } else {
676 scale = s->c_dc_scale;
677 }
678
679 wrap = s->block_wrap[n];
680 dc_val= s->dc_val[0] + s->block_index[n];
681
682 /* B C
683 * A X
684 */
685 a = dc_val[ - 1];
686 b = dc_val[ - 1 - wrap];
687 c = dc_val[ - wrap];
688
689 /* XXX: the following solution consumes divisions, but it does not
690 necessitate to modify mpegvideo.c. The problem comes from the
691 fact they decided to store the quantized DC (which would lead
692 to problems if Q could vary !) */
693 #if defined ARCH_X86 && !defined PIC
694 asm volatile(
695 "movl %3, %%eax \n\t"
696 "shrl $1, %%eax \n\t"
697 "addl %%eax, %2 \n\t"
698 "addl %%eax, %1 \n\t"
699 "addl %0, %%eax \n\t"
700 "mull %4 \n\t"
701 "movl %%edx, %0 \n\t"
702 "movl %1, %%eax \n\t"
703 "mull %4 \n\t"
704 "movl %%edx, %1 \n\t"
705 "movl %2, %%eax \n\t"
706 "mull %4 \n\t"
707 "movl %%edx, %2 \n\t"
708 : "+b" (a), "+c" (b), "+D" (c)
709 : "g" (scale), "S" (inverse[scale])
710 : "%eax", "%edx"
711 );
712 #else
713 /* #elif defined (ARCH_ALPHA) */
714 /* Divisions are extremely costly on Alpha; optimize the most
715 common case. But they are costly everywhere...
716 */
717 if (scale == 8) {
718 a = (a + (8 >> 1)) / 8;
719 b = (b + (8 >> 1)) / 8;
720 c = (c + (8 >> 1)) / 8;
721 } else {
722 a = (a + (scale >> 1)) / scale;
723 b = (b + (scale >> 1)) / scale;
724 c = (c + (scale >> 1)) / scale;
725 }
726 #endif
727 /* XXX: WARNING: they did not choose the same test as MPEG4. This
728 is very important ! */
729 if(s->msmpeg4_version>3){
730 if(s->inter_intra_pred){
731 uint8_t *dest;
732 int wrap;
733
734 if(n==1){
735 pred=a;
736 *dir_ptr = 0;
737 }else if(n==2){
738 pred=c;
739 *dir_ptr = 1;
740 }else if(n==3){
741 if (abs(a - b) < abs(b - c)) {
742 pred = c;
743 *dir_ptr = 1;
744 } else {
745 pred = a;
746 *dir_ptr = 0;
747 }
748 }else{
749 if(n<4){
750 wrap= s->linesize;
751 dest= s->current_picture[0] + (((n>>1) + 2*s->mb_y) * 8* wrap ) + ((n&1) + 2*s->mb_x) * 8;
752 }else{
753 wrap= s->linesize>>1;
754 dest= s->current_picture[n-3] + (s->mb_y * 8 * wrap) + s->mb_x * 8;
755 }
756 if(s->mb_x==0) a= (1024 + (scale>>1))/scale;
757 else a= get_dc(dest-8, wrap, scale*8);
758 if(s->mb_y==0) c= (1024 + (scale>>1))/scale;
759 else c= get_dc(dest-8*wrap, wrap, scale*8);
760
761 if (s->h263_aic_dir==0) {
762 pred= a;
763 *dir_ptr = 0;
764 }else if (s->h263_aic_dir==1) {
765 if(n==0){
766 pred= c;
767 *dir_ptr = 1;
768 }else{
769 pred= a;
770 *dir_ptr = 0;
771 }
772 }else if (s->h263_aic_dir==2) {
773 if(n==0){
774 pred= a;
775 *dir_ptr = 0;
776 }else{
777 pred= c;
778 *dir_ptr = 1;
779 }
780 } else {
781 pred= c;
782 *dir_ptr = 1;
783 }
784 }
785 }else{
786 if (abs(a - b) < abs(b - c)) {
787 pred = c;
788 *dir_ptr = 1;
789 } else {
790 pred = a;
791 *dir_ptr = 0;
792 }
793 }
794 }else{
795 if (abs(a - b) <= abs(b - c)) {
796 pred = c;
797 *dir_ptr = 1;
798 } else {
799 pred = a;
800 *dir_ptr = 0;
801 }
802 }
803
804 /* update predictor */
805 *dc_val_ptr = &dc_val[0];
806 return pred;
807 }
808
809 #define DC_MAX 119
810
811 static void msmpeg4_encode_dc(MpegEncContext * s, int level, int n, int *dir_ptr)
812 {
813 int sign, code;
814 int pred;
815
816 if(s->msmpeg4_version==1){
817 INT32 *dc_val;
818 pred = msmpeg4v1_pred_dc(s, n, &dc_val);
819
820 /* update predictor */
821 *dc_val= level;
822 }else{
823 UINT16 *dc_val;
824 pred = msmpeg4_pred_dc(s, n, &dc_val, dir_ptr);
825
826 /* update predictor */
827 if (n < 4) {
828 *dc_val = level * s->y_dc_scale;
829 } else {
830 *dc_val = level * s->c_dc_scale;
831 }
832 }
833
834 /* do the prediction */
835 level -= pred;
836
837 if(s->msmpeg4_version<=2){
838 if (n < 4) {
839 put_bits(&s->pb,
840 v2_dc_lum_table[level+256][1],
841 v2_dc_lum_table[level+256][0]);
842 }else{
843 put_bits(&s->pb,
844 v2_dc_chroma_table[level+256][1],
845 v2_dc_chroma_table[level+256][0]);
846 }
847 }else{
848 sign = 0;
849 if (level < 0) {
850 level = -level;
851 sign = 1;
852 }
853 code = level;
854 if (code > DC_MAX)
855 code = DC_MAX;
856
857 if (s->dc_table_index == 0) {
858 if (n < 4) {
859 put_bits(&s->pb, table0_dc_lum[code][1], table0_dc_lum[code][0]);
860 } else {
861 put_bits(&s->pb, table0_dc_chroma[code][1], table0_dc_chroma[code][0]);
862 }
863 } else {
864 if (n < 4) {
865 put_bits(&s->pb, table1_dc_lum[code][1], table1_dc_lum[code][0]);
866 } else {
867 put_bits(&s->pb, table1_dc_chroma[code][1], table1_dc_chroma[code][0]);
868 }
869 }
870
871 if (code == DC_MAX)
872 put_bits(&s->pb, 8, level);
873
874 if (level != 0) {
875 put_bits(&s->pb, 1, sign);
876 }
877 }
878 }
879
880 /* Encoding of a block. Very similar to MPEG4 except for a different
881 escape coding (same as H263) and more vlc tables.
882 */
883 static inline void msmpeg4_encode_block(MpegEncContext * s, DCTELEM * block, int n)
884 {
885 int level, run, last, i, j, last_index;
886 int last_non_zero, sign, slevel;
887 int code, run_diff, dc_pred_dir;
888 const RLTable *rl;
889 const UINT8 *scantable;
890
891 if (s->mb_intra) {
892 set_stat(ST_DC);
893 msmpeg4_encode_dc(s, block[0], n, &dc_pred_dir);
894 i = 1;
895 if (n < 4) {
896 rl = &rl_table[s->rl_table_index];
897 } else {
898 rl = &rl_table[3 + s->rl_chroma_table_index];
899 }
900 run_diff = 0;
901 scantable= s->intra_scantable;
902 set_stat(ST_INTRA_AC);
903 } else {
904 i = 0;
905 rl = &rl_table[3 + s->rl_table_index];
906 if(s->msmpeg4_version<=2)
907 run_diff = 0;
908 else
909 run_diff = 1;
910 scantable= s->inter_scantable;
911 set_stat(ST_INTER_AC);
912 }
913
914 /* recalculate block_last_index for M$ wmv1 */
915 if(scantable!=zigzag_direct && s->block_last_index[n]>0){
916 for(last_index=63; last_index>=0; last_index--){
917 if(block[scantable[last_index]]) break;
918 }
919 }else
920 last_index = s->block_last_index[n];
921 /* AC coefs */
922 last_non_zero = i - 1;
923 for (; i <= last_index; i++) {
924 j = scantable[i];
925 level = block[j];
926 if (level) {
927 run = i - last_non_zero - 1;
928 last = (i == last_index);
929 sign = 0;
930 slevel = level;
931 if (level < 0) {
932 sign = 1;
933 level = -level;
934 }
935 if(level<=MAX_LEVEL && run<=MAX_RUN){
936 s->ac_stats[s->mb_intra][n>3][level][run][last]++;
937 }
938 #if 0
939 else
940 s->ac_stats[s->mb_intra][n>3][40][63][0]++; //esc3 like
941 #endif
942 code = get_rl_index(rl, last, run, level);
943 put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
944 if (code == rl->n) {
945 int level1, run1;
946
947 level1 = level - rl->max_level[last][run];
948 if (level1 < 1)
949 goto esc2;
950 code = get_rl_index(rl, last, run, level1);
951 if (code == rl->n) {
952 esc2:
953 put_bits(&s->pb, 1, 0);
954 if (level > MAX_LEVEL)
955 goto esc3;
956 run1 = run - rl->max_run[last][level] - run_diff;
957 if (run1 < 0)
958 goto esc3;
959 code = get_rl_index(rl, last, run1, level);
960 if (code == rl->n) {
961 esc3:
962 /* third escape */
963 put_bits(&s->pb, 1, 0);
964 put_bits(&s->pb, 1, last);
965 if(s->msmpeg4_version==4){
966 if(s->esc3_level_length==0){
967 s->esc3_level_length=8;
968 s->esc3_run_length= 6;
969 if(s->qscale<8)
970 put_bits(&s->pb, 6, 3);
971 else
972 put_bits(&s->pb, 8, 3);
973 }
974 put_bits(&s->pb, s->esc3_run_length, run);
975 put_bits(&s->pb, 1, sign);
976 put_bits(&s->pb, s->esc3_level_length, level);
977 }else{
978 put_bits(&s->pb, 6, run);
979 put_bits(&s->pb, 8, slevel & 0xff);
980 }
981 } else {
982 /* second escape */
983 put_bits(&s->pb, 1, 1);
984 put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
985 put_bits(&s->pb, 1, sign);
986 }
987 } else {
988 /* first escape */
989 put_bits(&s->pb, 1, 1);
990 put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
991 put_bits(&s->pb, 1, sign);
992 }
993 } else {
994 put_bits(&s->pb, 1, sign);
995 }
996 last_non_zero = i;
997 }
998 }
999 }
1000
1001 /****************************************/
1002 /* decoding stuff */
1003
1004 static VLC mb_non_intra_vlc;
1005 static VLC mb_intra_vlc;
1006 static VLC dc_lum_vlc[2];
1007 static VLC dc_chroma_vlc[2];
1008 static VLC v2_dc_lum_vlc;
1009 static VLC v2_dc_chroma_vlc;
1010 static VLC cbpy_vlc;
1011 static VLC v2_intra_cbpc_vlc;
1012 static VLC v2_mb_type_vlc;
1013 static VLC v2_mv_vlc;
1014 static VLC v1_intra_cbpc_vlc;
1015 static VLC v1_inter_cbpc_vlc;
1016 static VLC inter_intra_vlc;
1017
1018 /* this table is practically identical to the one from h263 except that its inverted */
1019 static void init_h263_dc_for_msmpeg4(void)
1020 {
1021 int level, uni_code, uni_len;
1022
1023 for(level=-256; level<256; level++){
1024 int size, v, l;
1025 /* find number of bits */
1026 size = 0;
1027 v = abs(level);
1028 while (v) {
1029 v >>= 1;
1030 size++;
1031 }
1032
1033 if (level < 0)
1034 l= (-level) ^ ((1 << size) - 1);
1035 else
1036 l= level;
1037
1038 /* luminance h263 */
1039 uni_code= DCtab_lum[size][0];
1040 uni_len = DCtab_lum[size][1];
1041 uni_code ^= (1<<uni_len)-1; //M$ doesnt like compatibility
1042
1043 if (size > 0) {
1044 uni_code<<=size; uni_code|=l;
1045 uni_len+=size;
1046 if (size > 8){
1047 uni_code<<=1; uni_code|=1;
1048 uni_len++;
1049 }
1050 }
1051 v2_dc_lum_table[level+256][0]= uni_code;
1052 v2_dc_lum_table[level+256][1]= uni_len;
1053
1054 /* chrominance h263 */
1055 uni_code= DCtab_chrom[size][0];
1056 uni_len = DCtab_chrom[size][1];
1057 uni_code ^= (1<<uni_len)-1; //M$ doesnt like compatibility
1058
1059 if (size > 0) {
1060 uni_code<<=size; uni_code|=l;
1061 uni_len+=size;
1062 if (size > 8){
1063 uni_code<<=1; uni_code|=1;
1064 uni_len++;
1065 }
1066 }
1067 v2_dc_chroma_table[level+256][0]= uni_code;
1068 v2_dc_chroma_table[level+256][1]= uni_len;
1069
1070 }
1071 }
1072
1073 /* init all vlc decoding tables */
1074 int ff_msmpeg4_decode_init(MpegEncContext *s)
1075 {
1076 static int done = 0;
1077 int i;
1078 MVTable *mv;
1079
1080 common_init(s);
1081
1082 if (!done) {
1083 done = 1;
1084
1085 for(i=0;i<NB_RL_TABLES;i++) {
1086 init_rl(&rl_table[i]);
1087 init_vlc_rl(&rl_table[i]);
1088 }
1089 for(i=0;i<2;i++) {
1090 mv = &mv_tables[i];
1091 init_vlc(&mv->vlc, 9, mv->n + 1,
1092 mv->table_mv_bits, 1, 1,
1093 mv->table_mv_code, 2, 2);
1094 }
1095
1096 init_vlc(&dc_lum_vlc[0], 9, 120,
1097 &table0_dc_lum[0][1], 8, 4,
1098 &table0_dc_lum[0][0], 8, 4);
1099 init_vlc(&dc_chroma_vlc[0], 9, 120,
1100 &table0_dc_chroma[0][1], 8, 4,
1101 &table0_dc_chroma[0][0], 8, 4);
1102 init_vlc(&dc_lum_vlc[1], 9, 120,
1103 &table1_dc_lum[0][1], 8, 4,
1104 &table1_dc_lum[0][0], 8, 4);
1105 init_vlc(&dc_chroma_vlc[1], 9, 120,
1106 &table1_dc_chroma[0][1], 8, 4,
1107 &table1_dc_chroma[0][0], 8, 4);
1108
1109 init_vlc(&v2_dc_lum_vlc, 9, 512,
1110 &v2_dc_lum_table[0][1], 8, 4,
1111 &v2_dc_lum_table[0][0], 8, 4);
1112 init_vlc(&v2_dc_chroma_vlc, 9, 512,
1113 &v2_dc_chroma_table[0][1], 8, 4,
1114 &v2_dc_chroma_table[0][0], 8, 4);
1115
1116 init_vlc(&cbpy_vlc, 6, 16,
1117 &cbpy_tab[0][1], 2, 1,
1118 &cbpy_tab[0][0], 2, 1);
1119 init_vlc(&v2_intra_cbpc_vlc, 3, 4,
1120 &v2_intra_cbpc[0][1], 2, 1,
1121 &v2_intra_cbpc[0][0], 2, 1);
1122 init_vlc(&v2_mb_type_vlc, 5, 8,
1123 &v2_mb_type[0][1], 2, 1,
1124 &v2_mb_type[0][0], 2, 1);
1125 init_vlc(&v2_mv_vlc, 9, 33,
1126 &mvtab[0][1], 2, 1,
1127 &mvtab[0][0], 2, 1);
1128
1129 init_vlc(&mb_non_intra_vlc, 9, 128,
1130 &table_mb_non_intra[0][1], 8, 4,
1131 &table_mb_non_intra[0][0], 8, 4);
1132 init_vlc(&mb_intra_vlc, 9, 64,
1133 &table_mb_intra[0][1], 4, 2,
1134 &table_mb_intra[0][0], 4, 2);
1135
1136 init_vlc(&v1_intra_cbpc_vlc, 6, 8,
1137 intra_MCBPC_bits, 1, 1,
1138 intra_MCBPC_code, 1, 1);
1139 init_vlc(&v1_inter_cbpc_vlc, 6, 25,
1140 inter_MCBPC_bits, 1, 1,
1141 inter_MCBPC_code, 1, 1);
1142
1143 init_vlc(&inter_intra_vlc, 3, 4,
1144 &table_inter_intra[0][1], 2, 1,
1145 &table_inter_intra[0][0], 2, 1);
1146 }
1147 return 0;
1148 }
1149
1150 static int decode012(GetBitContext *gb)
1151 {
1152 int n;
1153 n = get_bits1(gb);
1154 if (n == 0)
1155 return 0;
1156 else
1157 return get_bits1(gb) + 1;
1158 }
1159
1160 int msmpeg4_decode_picture_header(MpegEncContext * s)
1161 {
1162 int code;
1163
1164 #if 0
1165 {
1166 int i;
1167 for(i=0; i<s->gb.size*8; i++)
1168 printf("%d", get_bits1(&s->gb));
1169 // get_bits1(&s->gb);
1170 printf("END\n");
1171 return -1;
1172 }
1173 #endif
1174
1175 if(s->msmpeg4_version==1){
1176 int start_code, num;
1177 start_code = (get_bits(&s->gb, 16)<<16) | get_bits(&s->gb, 16);
1178 if(start_code!=0x00000100){
1179 fprintf(stderr, "invalid startcode\n");
1180 return -1;
1181 }
1182
1183 num= get_bits(&s->gb, 5); // frame number */
1184 }
1185
1186 s->pict_type = get_bits(&s->gb, 2) + 1;
1187 if (s->pict_type != I_TYPE &&
1188 s->pict_type != P_TYPE){
1189 fprintf(stderr, "invalid picture type\n");
1190 return -1;
1191 }
1192 #if 0
1193 {
1194 static int had_i=0;
1195 if(s->pict_type == I_TYPE) had_i=1;
1196 if(!had_i) return -1;
1197 }
1198 #endif
1199 s->qscale = get_bits(&s->gb, 5);
1200
1201 if (s->pict_type == I_TYPE) {
1202 code = get_bits(&s->gb, 5);
1203 if(s->msmpeg4_version==1){
1204 if(code==0 || code>s->mb_height){
1205 fprintf(stderr, "invalid slice height %d\n", code);
1206 return -1;
1207 }
1208
1209 s->slice_height = code;
1210 }else{
1211 /* 0x17: one slice, 0x18: two slices, ... */
1212 if (code < 0x17){
1213 fprintf(stderr, "error, slice code was %X\n", code);
1214 return -1;
1215 }
1216
1217 s->slice_height = s->mb_height / (code - 0x16);
1218 }
1219
1220 switch(s->msmpeg4_version){
1221 case 1:
1222 case 2:
1223 s->rl_chroma_table_index = 2;
1224 s->rl_table_index = 2;
1225
1226 s->dc_table_index = 0; //not used
1227 break;
1228 case 3:
1229 s->rl_chroma_table_index = decode012(&s->gb);
1230 s->rl_table_index = decode012(&s->gb);
1231
1232 s->dc_table_index = get_bits1(&s->gb);
1233 break;
1234 case 4:
1235 msmpeg4_decode_ext_header(s, (2+5+5+17+7)/8);
1236
1237 if(s->bit_rate > 50) s->per_mb_rl_table= get_bits1(&s->gb);
1238 else s->per_mb_rl_table= 0;
1239
1240 if(!s->per_mb_rl_table){
1241 s->rl_chroma_table_index = decode012(&s->gb);
1242 s->rl_table_index = decode012(&s->gb);
1243 }
1244
1245 s->dc_table_index = get_bits1(&s->gb);
1246 s->inter_intra_pred= 0;
1247 break;
1248 }
1249 s->no_rounding = 1;
1250 /* printf("qscale:%d rlc:%d rl:%d dc:%d mbrl:%d slice:%d \n",
1251 s->qscale,
1252 s->rl_chroma_table_index,
1253 s->rl_table_index,
1254 s->dc_table_index,
1255 s->per_mb_rl_table,
1256 s->slice_height);*/
1257 } else {
1258 switch(s->msmpeg4_version){
1259 case 1:
1260 case 2:
1261 if(s->msmpeg4_version==1)
1262 s->use_skip_mb_code = 1;
1263 else
1264 s->use_skip_mb_code = get_bits1(&s->gb);
1265 s->rl_table_index = 2;
1266 s->rl_chroma_table_index = s->rl_table_index;
1267 s->dc_table_index = 0; //not used
1268 s->mv_table_index = 0;
1269 break;
1270 case 3:
1271 s->use_skip_mb_code = get_bits1(&s->gb);
1272 s->rl_table_index = decode012(&s->gb);
1273 s->rl_chroma_table_index = s->rl_table_index;
1274
1275 s->dc_table_index = get_bits1(&s->gb);
1276
1277 s->mv_table_index = get_bits1(&s->gb);
1278 break;
1279 case 4:
1280 s->use_skip_mb_code = get_bits1(&s->gb);
1281
1282 if(s->bit_rate > 50) s->per_mb_rl_table= get_bits1(&s->gb);
1283 else s->per_mb_rl_table= 0;
1284
1285 if(!s->per_mb_rl_table){
1286 s->rl_table_index = decode012(&s->gb);
1287 s->rl_chroma_table_index = s->rl_table_index;
1288 }
1289
1290 s->dc_table_index = get_bits1(&s->gb);
1291
1292 s->mv_table_index = get_bits1(&s->gb);
1293 s->inter_intra_pred= (s->width*s->height < 320*240 && s->bit_rate<=128);
1294 break;
1295 }
1296 /* printf("skip:%d rl:%d rlc:%d dc:%d mv:%d mbrl:%d qp:%d \n",
1297 s->use_skip_mb_code,
1298 s->rl_table_index,
1299 s->rl_chroma_table_index,
1300 s->dc_table_index,
1301 s->mv_table_index,
1302 s->per_mb_rl_table,
1303 s->qscale);*/
1304 if(s->flipflop_rounding){
1305 s->no_rounding ^= 1;
1306 }else{
1307 s->no_rounding = 0;
1308 }
1309 }
1310
1311 s->esc3_level_length= 0;
1312 s->esc3_run_length= 0;
1313
1314 #ifdef DEBUG
1315 printf("*****frame %d:\n", frame_count++);
1316 #endif
1317 return 0;
1318 }
1319
1320 int msmpeg4_decode_ext_header(MpegEncContext * s, int buf_size)
1321 {
1322 int left= buf_size*8 - get_bits_count(&s->gb);
1323 int length= s->msmpeg4_version>=3 ? 17 : 16;
1324 /* the alt_bitstream reader could read over the end so we need to check it */
1325 if(left>=length && left<length+8)
1326 {
1327 int fps;
1328
1329 fps= get_bits(&s->gb, 5);
1330 s->bit_rate= get_bits(&s->gb, 11);
1331 if(s->msmpeg4_version>=3)
1332 s->flipflop_rounding= get_bits1(&s->gb);
1333 else
1334 s->flipflop_rounding= 0;
1335
1336 // printf("fps:%2d bps:%2d roundingType:%1d\n", fps, s->bit_rate, s->flipflop_rounding);
1337 }
1338 else if(left<length+8)
1339 {
1340 s->flipflop_rounding= 0;
1341 printf("ext header missing, %d left\n", left);
1342 }
1343 else
1344 {
1345 fprintf(stderr, "I frame too long, ignoring ext header\n");
1346 }
1347
1348 return 0;
1349 }
1350
1351 static inline void msmpeg4_memsetw(short *tab, int val, int n)
1352 {
1353 int i;
1354 for(i=0;i<n;i++)
1355 tab[i] = val;
1356 }
1357
1358 static void msmpeg4v2_encode_motion(MpegEncContext * s, int val)
1359 {
1360 int range, bit_size, sign, code, bits;
1361
1362 if (val == 0) {
1363 /* zero vector */
1364 code = 0;
1365 put_bits(&s->pb, mvtab[code][1], mvtab[code][0]);
1366 } else {
1367 bit_size = s->f_code - 1;
1368 range = 1 << bit_size;
1369 if (val <= -64)
1370 val += 64;
1371 else if (val >= 64)
1372 val -= 64;
1373
1374 if (val >= 0) {
1375 sign = 0;
1376 } else {
1377 val = -val;
1378 sign = 1;
1379 }
1380 val--;
1381 code = (val >> bit_size) + 1;
1382 bits = val & (range - 1);
1383
1384 put_bits(&s->pb, mvtab[code][1] + 1, (mvtab[code][0] << 1) | sign);
1385 if (bit_size > 0) {
1386 put_bits(&s->pb, bit_size, bits);
1387 }
1388 }
1389 }
1390
1391 /* this is identical to h263 except that its range is multiplied by 2 */
1392 static int msmpeg4v2_decode_motion(MpegEncContext * s, int pred, int f_code)
1393 {
1394 int code, val, sign, shift;
1395
1396 code = get_vlc(&s->gb, &v2_mv_vlc);
1397 // printf("MV code %d at %d %d pred: %d\n", code, s->mb_x,s->mb_y, pred);
1398 if (code < 0)
1399 return 0xffff;
1400
1401 if (code == 0)
1402 return pred;
1403 sign = get_bits1(&s->gb);
1404 shift = f_code - 1;
1405 val = (code - 1) << shift;
1406 if (shift > 0)
1407 val |= get_bits(&s->gb, shift);
1408 val++;
1409 if (sign)
1410 val = -val;
1411
1412 val += pred;
1413 if (val <= -64)
1414 val += 64;
1415 else if (val >= 64)
1416 val -= 64;
1417
1418 return val;
1419 }
1420
1421
1422 static int msmpeg4v12_decode_mb(MpegEncContext *s,
1423 DCTELEM block[6][64])
1424 {
1425 int cbp, code, i;
1426 if (s->pict_type == P_TYPE) {
1427 if (s->use_skip_mb_code) {
1428 if (get_bits1(&s->gb)) {
1429 /* skip mb */
1430 s->mb_intra = 0;
1431 for(i=0;i<6;i++)
1432 s->block_last_index[i] = -1;
1433 s->mv_dir = MV_DIR_FORWARD;
1434 s->mv_type = MV_TYPE_16X16;
1435 s->mv[0][0][0] = 0;
1436 s->mv[0][0][1] = 0;
1437 s->mb_skiped = 1;
1438 return 0;
1439 }
1440 }
1441
1442 if(s->msmpeg4_version==2)
1443 code = get_vlc(&s->gb, &v2_mb_type_vlc);
1444 else
1445 code = get_vlc(&s->gb, &v1_inter_cbpc_vlc);
1446 if(code<0 || code>7){
1447 fprintf(stderr, "cbpc %d invalid at %d %d\n", code, s->mb_x, s->mb_y);
1448 return -1;
1449 }
1450
1451 s->mb_intra = code >>2;
1452
1453 cbp = code & 0x3;
1454 } else {
1455 s->mb_intra = 1;
1456 if(s->msmpeg4_version==2)
1457 cbp= get_vlc(&s->gb, &v2_intra_cbpc_vlc);
1458 else
1459 cbp= get_vlc(&s->gb, &v1_intra_cbpc_vlc);
1460 if(cbp<0 || cbp>3){
1461 fprintf(stderr, "cbpc %d invalid at %d %d\n", cbp, s->mb_x, s->mb_y);
1462 return -1;
1463 }
1464 }
1465
1466 if (!s->mb_intra) {
1467 int mx, my, cbpy;
1468
1469 cbpy= get_vlc(&s->gb, &cbpy_vlc);
1470 if(cbpy<0){
1471 fprintf(stderr, "cbpy %d invalid at %d %d\n", cbp, s->mb_x, s->mb_y);
1472 return -1;
1473 }
1474
1475 cbp|= cbpy<<2;
1476 if(s->msmpeg4_version==1 || (cbp&3) != 3) cbp^= 0x3C;
1477
1478 h263_pred_motion(s, 0, &mx, &my);
1479 mx= msmpeg4v2_decode_motion(s, mx, 1);
1480 my= msmpeg4v2_decode_motion(s, my, 1);
1481
1482 s->mv_dir = MV_DIR_FORWARD;
1483 s->mv_type = MV_TYPE_16X16;
1484 s->mv[0][0][0] = mx;
1485 s->mv[0][0][1] = my;
1486 } else {
1487 if(s->msmpeg4_version==2){
1488 s->ac_pred = get_bits1(&s->gb);
1489 cbp|= get_vlc(&s->gb, &cbpy_vlc)<<2; //FIXME check errors
1490 } else{
1491 s->ac_pred = 0;
1492 cbp|= get_vlc(&s->gb, &cbpy_vlc)<<2; //FIXME check errors
1493 if(s->pict_type==P_TYPE) cbp^=0x3C;
1494 }
1495 }
1496
1497 for (i = 0; i < 6; i++) {
1498 if (msmpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1) < 0)
1499 {
1500 fprintf(stderr,"\nerror while decoding block: %d x %d (%d)\n", s->mb_x, s->mb_y, i);
1501 return -1;
1502 }
1503 }
1504 return 0;
1505 }
1506
1507 int msmpeg4_decode_mb(MpegEncContext *s,
1508 DCTELEM block[6][64])
1509 {
1510 int cbp, code, i;
1511 UINT8 *coded_val;
1512
1513 #ifdef PRINT_MB
1514 if(s->mb_x==0){
1515 printf("\n");
1516 if(s->mb_y==0) printf("\n");
1517 }
1518 #endif
1519 /* special slice handling */
1520 handle_slices(s);
1521
1522 if(s->msmpeg4_version<=2) return msmpeg4v12_decode_mb(s, block); //FIXME export function & call from outside perhaps
1523
1524 if (s->pict_type == P_TYPE) {
1525 set_stat(ST_INTER_MB);
1526 if (s->use_skip_mb_code) {
1527 if (get_bits1(&s->gb)) {
1528 /* skip mb */
1529 s->mb_intra = 0;
1530 for(i=0;i<6;i++)
1531 s->block_last_index[i] = -1;
1532 s->mv_dir = MV_DIR_FORWARD;
1533 s->mv_type = MV_TYPE_16X16;
1534 s->mv[0][0][0] = 0;
1535 s->mv[0][0][1] = 0;
1536 s->mb_skiped = 1;
1537 #ifdef PRINT_MB
1538 printf("S ");
1539 #endif
1540 return 0;
1541 }
1542 }
1543
1544 code = get_vlc(&s->gb, &mb_non_intra_vlc);
1545 if (code < 0)
1546 return -1;
1547 //s->mb_intra = (code & 0x40) ? 0 : 1;
1548 s->mb_intra = (~code & 0x40) >> 6;
1549
1550 cbp = code & 0x3f;
1551 } else {
1552 set_stat(ST_INTRA_MB);
1553 s->mb_intra = 1;
1554 code = get_vlc(&s->gb, &mb_intra_vlc);
1555 if (code < 0)
1556 return -1;
1557 /* predict coded block pattern */
1558 cbp = 0;
1559 for(i=0;i<6;i++) {
1560 int val = ((code >> (5 - i)) & 1);
1561 if (i < 4) {
1562 int pred = coded_block_pred(s, i, &coded_val);
1563 val = val ^ pred;
1564 *coded_val = val;
1565 }
1566 cbp |= val << (5 - i);
1567 }
1568 }
1569
1570 if (!s->mb_intra) {
1571 int mx, my;
1572 //printf("P at %d %d\n", s->mb_x, s->mb_y);
1573 if(s->per_mb_rl_table && cbp){
1574 s->rl_table_index = decode012(&s->gb);
1575 s->rl_chroma_table_index = s->rl_table_index;
1576 }
1577 set_stat(ST_MV);
1578 h263_pred_motion(s, 0, &mx, &my);
1579 if (msmpeg4_decode_motion(s, &mx, &my) < 0)
1580 return -1;
1581 s->mv_dir = MV_DIR_FORWARD;
1582 s->mv_type = MV_TYPE_16X16;
1583 s->mv[0][0][0] = mx;
1584 s->mv[0][0][1] = my;
1585 #ifdef PRINT_MB
1586 printf("P ");
1587 #endif
1588 } else {
1589 //printf("I at %d %d %d %06X\n", s->mb_x, s->mb_y, ((cbp&3)? 1 : 0) +((cbp&0x3C)? 2 : 0), show_bits(&s->gb, 24));
1590 set_stat(ST_INTRA_MB);
1591 s->ac_pred = get_bits1(&s->gb);
1592 #ifdef PRINT_MB
1593 printf("%c", s->ac_pred ? 'A' : 'I');
1594 #endif
1595 if(s->inter_intra_pred){
1596 s->h263_aic_dir= get_vlc(&s->gb, &inter_intra_vlc);
1597 // printf("%d%d %d %d/", s->ac_pred, s->h263_aic_dir, s->mb_x, s->mb_y);
1598 }
1599 if(s->per_mb_rl_table && cbp){
1600 s->rl_table_index = decode012(&s->gb);
1601 s->rl_chroma_table_index = s->rl_table_index;
1602 }
1603 }
1604
1605 for (i = 0; i < 6; i++) {
1606 if (msmpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1) < 0)
1607 {
1608 fprintf(stderr,"\nerror while decoding block: %d x %d (%d)\n", s->mb_x, s->mb_y, i);
1609 return -1;
1610 }
1611 }
1612
1613 return 0;
1614 }
1615
1616 static inline int msmpeg4_decode_block(MpegEncContext * s, DCTELEM * block,
1617 int n, int coded)
1618 {
1619 int code, level, i, j, last, run, run_diff;
1620 int dc_pred_dir;
1621 RLTable *rl;
1622 const UINT8 *scan_table;
1623 int qmul, qadd;
1624
1625 if (s->mb_intra) {
1626 qmul=1;
1627 qadd=0;
1628
1629 /* DC coef */
1630 set_stat(ST_DC);
1631 level = msmpeg4_decode_dc(s, n, &dc_pred_dir);
1632 #ifdef PRINT_MB
1633 {
1634 static int c;
1635 if(n==0) c=0;
1636 if(n==4) printf("%X", c);
1637 c+= c +dc_pred_dir;
1638 }
1639 #endif
1640 if (level < 0){
1641 fprintf(stderr, "dc overflow- block: %d qscale: %d//\n", n, s->qscale);
1642 if(s->inter_intra_pred) level=0;
1643 else return -1;
1644 }
1645 if (n < 4) {
1646 rl = &rl_table[s->rl_table_index];
1647 if(level > 256*s->y_dc_scale){
1648 fprintf(stderr, "dc overflow+ L qscale: %d//\n", s->qscale);
1649 if(!s->inter_intra_pred) return -1;
1650 }
1651 } else {
1652 rl = &rl_table[3 + s->rl_chroma_table_index];
1653 if(level > 256*s->c_dc_scale){
1654 fprintf(stderr, "dc overflow+ C qscale: %d//\n", s->qscale);
1655 if(!s->inter_intra_pred) return -1;
1656 }
1657 }
1658 block[0] = level;
1659
1660 run_diff = 0;
1661 i = 1;
1662 if (!coded) {
1663 goto not_coded;
1664 }
1665 if (s->ac_pred) {
1666 if (dc_pred_dir == 0)
1667 scan_table = s->intra_v_scantable; /* left */
1668 else
1669 scan_table = s->intra_h_scantable; /* top */
1670 } else {
1671 scan_table = s->intra_scantable;
1672 }
1673 set_stat(ST_INTRA_AC);
1674 } else {
1675 qmul = s->qscale << 1;
1676 qadd = (s->qscale - 1) | 1;
1677 i = 0;
1678 rl = &rl_table[3 + s->rl_table_index];
1679
1680 if(s->msmpeg4_version==2)
1681 run_diff = 0;
1682 else
1683 run_diff = 1;
1684
1685 if (!coded) {
1686 s->block_last_index[n] = i - 1;
1687 return 0;
1688 }
1689 scan_table = s->inter_scantable;
1690 set_stat(ST_INTER_AC);
1691 }
1692
1693 for(;;) {
1694 code = get_vlc(&s->gb, &rl->vlc);
1695 if (code < 0){
1696 fprintf(stderr, "illegal AC-VLC code at %d %d\n", s->mb_x, s->mb_y);
1697 return -1;
1698 }
1699 if (code == rl->n) {
1700 /* escape */
1701 if (s->msmpeg4_version==1 || get_bits1(&s->gb) == 0) {
1702 if (s->msmpeg4_version==1 || get_bits1(&s->gb) == 0) {
1703 /* third escape */
1704 if(s->msmpeg4_version<=3){
1705 last= get_bits1(&s->gb);
1706 run= get_bits(&s->gb, 6);
1707 level= get_bits(&s->gb, 8);
1708 level= ((int8_t)level);
1709 }else{
1710 int sign;
1711 last= get_bits1(&s->gb);
1712 if(!s->esc3_level_length){
1713 int ll;
1714 //printf("ESC-3 %X at %d %d\n", show_bits(&s->gb, 24), s->mb_x, s->mb_y);
1715 if(s->qscale<8){
1716 ll= get_bits(&s->gb, 3);
1717 if(ll==0){
1718 if(get_bits1(&s->gb)) printf("cool a new vlc code ,contact the ffmpeg developers and upload the file\n");
1719 ll=8;
1720 }
1721 }else{
1722 ll=2;
1723 while(ll<8 && get_bits1(&s->gb)==0) ll++;
1724 }
1725
1726 s->esc3_level_length= ll;
1727 s->esc3_run_length= get_bits(&s->gb, 2) + 3;
1728 //printf("level length:%d, run length: %d\n", ll, s->esc3_run_length);
1729 }
1730 run= get_bits(&s->gb, s->esc3_run_length);
1731 sign= get_bits1(&s->gb);
1732 level= get_bits(&s->gb, s->esc3_level_length);
1733 if(sign) level= -level;
1734 }
1735 //printf("level: %d, run: %d at %d %d\n", level, run, s->mb_x, s->mb_y);
1736 #if 0 // waste of time / this will detect very few errors
1737 {
1738 const int abs_level= ABS(level);
1739 const int run1= run - rl->max_run[last][abs_level] - run_diff;
1740 if(abs_level<=MAX_LEVEL && run<=MAX_RUN){
1741 if(abs_level <= rl->max_level[last][run]){
1742 fprintf(stderr, "illegal 3. esc, vlc encoding possible\n");
1743 return DECODING_AC_LOST;
1744 }
1745 if(abs_level <= rl->max_level[last][run]*2){
1746 fprintf(stderr, "illegal 3. esc, esc 1 encoding possible\n");
1747 return DECODING_AC_LOST;
1748 }
1749 if(run1>=0 && abs_level <= rl->max_level[last][run1]){
1750 fprintf(stderr, "illegal 3. esc, esc 2 encoding possible\n");
1751 return DECODING_AC_LOST;
1752 }
1753 }
1754 }
1755 #endif
1756 //level = level * qmul + (level>0) * qadd - (level<=0) * qadd ;
1757 if (level>0) level= level * qmul + qadd;
1758 else level= level * qmul - qadd;
1759 #if 0 // waste of time too :(
1760 if(level>2048 || level<-2048){
1761 fprintf(stderr, "|level| overflow in 3. esc\n");
1762 return DECODING_AC_LOST;
1763 }
1764 #endif
1765 } else {
1766 /* second escape */
1767 code = get_vlc(&s->gb, &rl->vlc);
1768 if (code < 0 || code >= rl->n){
1769 fprintf(stderr, "illegal ESC2-VLC code %d at %d %d\n", code, s->mb_x, s->mb_y);
1770 return -1;
1771 }
1772 run = rl->table_run[code];
1773 level = rl->table_level[code];
1774 last = code >= rl->last;
1775 run += rl->max_run[last][level] + run_diff;
1776 level= level * qmul + qadd;
1777 if (get_bits1(&s->gb))
1778 level = -level;
1779 }
1780 } else {
1781 /* first escape */
1782 code = get_vlc(&s->gb, &rl->vlc);
1783 if (code < 0 || code >= rl->n){
1784 fprintf(stderr, "illegal ESC2-VLC code %d at %d %d\n", code, s->mb_x, s->mb_y);
1785 return -1;
1786 }
1787 run = rl->table_run[code];
1788 level = rl->table_level[code];
1789 last = code >= rl->last;
1790 level += rl->max_level[last][run];
1791 level= level * qmul + qadd;
1792 if (get_bits1(&s->gb))
1793 level = -level;
1794 }
1795 } else {
1796 run = rl->table_run[code];
1797 level = rl->table_level[code] * qmul + qadd;
1798 last = code >= rl->last;
1799 if (get_bits1(&s->gb))
1800 level = -level;
1801 }
1802 i += run;
1803 if (i >= 64){
1804 fprintf(stderr, "run too long at %d %d\n", s->mb_x, s->mb_y);
1805 return -1;
1806 }
1807
1808 j = scan_table[i];
1809 block[j] = level;
1810 i++;
1811 if (last)
1812 break;
1813 }
1814 not_coded:
1815 if (s->mb_intra) {
1816 mpeg4_pred_ac(s, block, n, dc_pred_dir);
1817 if (s->ac_pred) {
1818 i = 64; /* XXX: not optimal */
1819 }
1820 }
1821 if(s->msmpeg4_version==4 && i>1) i=64; //FIXME/XXX optimize
1822 s->block_last_index[n] = i - 1;
1823
1824 return 0;
1825 }
1826
1827 static int msmpeg4_decode_dc(MpegEncContext * s, int n, int *dir_ptr)
1828 {
1829 int level, pred;
1830
1831 if(s->msmpeg4_version<=2){
1832 if (n < 4) {
1833 level = get_vlc(&s->gb, &v2_dc_lum_vlc);
1834 } else {
1835 level = get_vlc(&s->gb, &v2_dc_chroma_vlc);
1836 }
1837 if (level < 0)
1838 return -1;
1839 level-=256;
1840 }else{ //FIXME optimize use unified tables & index
1841 if (n < 4) {
1842 level = get_vlc(&s->gb, &dc_lum_vlc[s->dc_table_index]);
1843 } else {
1844 level = get_vlc(&s->gb, &dc_chroma_vlc[s->dc_table_index]);
1845 }
1846 if (level < 0){
1847 fprintf(stderr, "illegal dc vlc\n");
1848 return -1;
1849 }
1850
1851 if (level == DC_MAX) {
1852 level = get_bits(&s->gb, 8);
1853 if (get_bits1(&s->gb))
1854 level = -level;
1855 } else if (level != 0) {
1856 if (get_bits1(&s->gb))
1857 level = -level;
1858 }
1859 }
1860
1861 if(s->msmpeg4_version==1){
1862 INT32 *dc_val;
1863 pred = msmpeg4v1_pred_dc(s, n, &dc_val);
1864 level += pred;
1865
1866 /* update predictor */
1867 *dc_val= level;
1868 }else{
1869 UINT16 *dc_val;
1870 pred = msmpeg4_pred_dc(s, n, &dc_val, dir_ptr);
1871 level += pred;
1872
1873 /* update predictor */
1874 if (n < 4) {
1875 *dc_val = level * s->y_dc_scale;
1876 } else {
1877 *dc_val = level * s->c_dc_scale;
1878 }
1879 }
1880
1881 return level;
1882 }
1883
1884 static int msmpeg4_decode_motion(MpegEncContext * s,
1885 int *mx_ptr, int *my_ptr)
1886 {
1887 MVTable *mv;
1888 int code, mx, my;
1889
1890 mv = &mv_tables[s->mv_table_index];
1891
1892 code = get_vlc(&s->gb, &mv->vlc);
1893 if (code < 0){
1894 fprintf(stderr, "illegal MV code at %d %d\n", s->mb_x, s->mb_y);
1895 return -1;
1896 }
1897 if (code == mv->n) {
1898 //printf("MV ESC %X at %d %d\n", show_bits(&s->gb, 24), s->mb_x, s->mb_y);
1899 mx = get_bits(&s->gb, 6);
1900 my = get_bits(&s->gb, 6);
1901 } else {
1902 mx = mv->table_mvx[code];
1903 my = mv->table_mvy[code];
1904 }
1905
1906 mx += *mx_ptr - 32;
1907 my += *my_ptr - 32;
1908 /* WARNING : they do not do exactly modulo encoding */
1909 if (mx <= -64)
1910 mx += 64;
1911 else if (mx >= 64)
1912 mx -= 64;
1913
1914 if (my <= -64)
1915 my += 64;
1916 else if (my >= 64)
1917 my -= 64;
1918 *mx_ptr = mx;
1919 *my_ptr = my;
1920 return 0;
1921 }