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