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