rl: Rename ff_*_rl() to ff_rl_*()
[libav.git] / libavcodec / msmpeg4dec.c
1 /*
2 * MSMPEG4 backend for encoder and decoder
3 * Copyright (c) 2001 Fabrice Bellard
4 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
5 *
6 * msmpeg4v1 & v2 stuff by Michael Niedermayer <michaelni@gmx.at>
7 *
8 * This file is part of Libav.
9 *
10 * Libav is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
14 *
15 * Libav is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with Libav; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 */
24
25 #include "avcodec.h"
26 #include "internal.h"
27 #include "mpegutils.h"
28 #include "mpegvideo.h"
29 #include "msmpeg4.h"
30 #include "libavutil/x86/asm.h"
31 #include "h263.h"
32 #include "mpeg4video.h"
33 #include "msmpeg4data.h"
34 #include "vc1data.h"
35
36 #define DC_VLC_BITS 9
37 #define V2_INTRA_CBPC_VLC_BITS 3
38 #define V2_MB_TYPE_VLC_BITS 7
39 #define MV_VLC_BITS 9
40 #define V2_MV_VLC_BITS 9
41 #define TEX_VLC_BITS 9
42
43 #define DEFAULT_INTER_INDEX 3
44
45 static inline int msmpeg4v1_pred_dc(MpegEncContext * s, int n,
46 int32_t **dc_val_ptr)
47 {
48 int i;
49
50 if (n < 4) {
51 i= 0;
52 } else {
53 i= n-3;
54 }
55
56 *dc_val_ptr= &s->last_dc[i];
57 return s->last_dc[i];
58 }
59
60 /****************************************/
61 /* decoding stuff */
62
63 VLC ff_mb_non_intra_vlc[4];
64 static VLC v2_dc_lum_vlc;
65 static VLC v2_dc_chroma_vlc;
66 static VLC v2_intra_cbpc_vlc;
67 static VLC v2_mb_type_vlc;
68 static VLC v2_mv_vlc;
69 VLC ff_inter_intra_vlc;
70
71 /* This is identical to h263 except that its range is multiplied by 2. */
72 static int msmpeg4v2_decode_motion(MpegEncContext * s, int pred, int f_code)
73 {
74 int code, val, sign, shift;
75
76 code = get_vlc2(&s->gb, v2_mv_vlc.table, V2_MV_VLC_BITS, 2);
77 ff_dlog(s, "MV code %d at %d %d pred: %d\n", code, s->mb_x,s->mb_y, pred);
78 if (code < 0)
79 return 0xffff;
80
81 if (code == 0)
82 return pred;
83 sign = get_bits1(&s->gb);
84 shift = f_code - 1;
85 val = code;
86 if (shift) {
87 val = (val - 1) << shift;
88 val |= get_bits(&s->gb, shift);
89 val++;
90 }
91 if (sign)
92 val = -val;
93
94 val += pred;
95 if (val <= -64)
96 val += 64;
97 else if (val >= 64)
98 val -= 64;
99
100 return val;
101 }
102
103 static int msmpeg4v12_decode_mb(MpegEncContext *s, int16_t block[6][64])
104 {
105 int cbp, code, i;
106
107 if (s->pict_type == AV_PICTURE_TYPE_P) {
108 if (s->use_skip_mb_code) {
109 if (get_bits1(&s->gb)) {
110 /* skip mb */
111 s->mb_intra = 0;
112 for(i=0;i<6;i++)
113 s->block_last_index[i] = -1;
114 s->mv_dir = MV_DIR_FORWARD;
115 s->mv_type = MV_TYPE_16X16;
116 s->mv[0][0][0] = 0;
117 s->mv[0][0][1] = 0;
118 s->mb_skipped = 1;
119 return 0;
120 }
121 }
122
123 if(s->msmpeg4_version==2)
124 code = get_vlc2(&s->gb, v2_mb_type_vlc.table, V2_MB_TYPE_VLC_BITS, 1);
125 else
126 code = get_vlc2(&s->gb, ff_h263_inter_MCBPC_vlc.table, INTER_MCBPC_VLC_BITS, 2);
127 if(code<0 || code>7){
128 av_log(s->avctx, AV_LOG_ERROR, "cbpc %d invalid at %d %d\n", code, s->mb_x, s->mb_y);
129 return -1;
130 }
131
132 s->mb_intra = code >>2;
133
134 cbp = code & 0x3;
135 } else {
136 s->mb_intra = 1;
137 if(s->msmpeg4_version==2)
138 cbp= get_vlc2(&s->gb, v2_intra_cbpc_vlc.table, V2_INTRA_CBPC_VLC_BITS, 1);
139 else
140 cbp= get_vlc2(&s->gb, ff_h263_intra_MCBPC_vlc.table, INTRA_MCBPC_VLC_BITS, 1);
141 if(cbp<0 || cbp>3){
142 av_log(s->avctx, AV_LOG_ERROR, "cbpc %d invalid at %d %d\n", cbp, s->mb_x, s->mb_y);
143 return -1;
144 }
145 }
146
147 if (!s->mb_intra) {
148 int mx, my, cbpy;
149
150 cbpy= get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
151 if(cbpy<0){
152 av_log(s->avctx, AV_LOG_ERROR, "cbpy %d invalid at %d %d\n", cbp, s->mb_x, s->mb_y);
153 return -1;
154 }
155
156 cbp|= cbpy<<2;
157 if(s->msmpeg4_version==1 || (cbp&3) != 3) cbp^= 0x3C;
158
159 ff_h263_pred_motion(s, 0, 0, &mx, &my);
160 mx= msmpeg4v2_decode_motion(s, mx, 1);
161 my= msmpeg4v2_decode_motion(s, my, 1);
162
163 s->mv_dir = MV_DIR_FORWARD;
164 s->mv_type = MV_TYPE_16X16;
165 s->mv[0][0][0] = mx;
166 s->mv[0][0][1] = my;
167 } else {
168 if(s->msmpeg4_version==2){
169 s->ac_pred = get_bits1(&s->gb);
170 cbp|= get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1)<<2; //FIXME check errors
171 } else{
172 s->ac_pred = 0;
173 cbp|= get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1)<<2; //FIXME check errors
174 if(s->pict_type==AV_PICTURE_TYPE_P) cbp^=0x3C;
175 }
176 }
177
178 s->bdsp.clear_blocks(s->block[0]);
179 for (i = 0; i < 6; i++) {
180 if (ff_msmpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1, NULL) < 0)
181 {
182 av_log(s->avctx, AV_LOG_ERROR, "\nerror while decoding block: %d x %d (%d)\n", s->mb_x, s->mb_y, i);
183 return -1;
184 }
185 }
186 return 0;
187 }
188
189 static int msmpeg4v34_decode_mb(MpegEncContext *s, int16_t block[6][64])
190 {
191 int cbp, code, i;
192 uint8_t *coded_val;
193 uint32_t * const mb_type_ptr = &s->current_picture.mb_type[s->mb_x + s->mb_y*s->mb_stride];
194
195 if (s->pict_type == AV_PICTURE_TYPE_P) {
196 if (s->use_skip_mb_code) {
197 if (get_bits1(&s->gb)) {
198 /* skip mb */
199 s->mb_intra = 0;
200 for(i=0;i<6;i++)
201 s->block_last_index[i] = -1;
202 s->mv_dir = MV_DIR_FORWARD;
203 s->mv_type = MV_TYPE_16X16;
204 s->mv[0][0][0] = 0;
205 s->mv[0][0][1] = 0;
206 s->mb_skipped = 1;
207 *mb_type_ptr = MB_TYPE_SKIP | MB_TYPE_L0 | MB_TYPE_16x16;
208
209 return 0;
210 }
211 }
212
213 code = get_vlc2(&s->gb, ff_mb_non_intra_vlc[DEFAULT_INTER_INDEX].table, MB_NON_INTRA_VLC_BITS, 3);
214 if (code < 0)
215 return -1;
216 //s->mb_intra = (code & 0x40) ? 0 : 1;
217 s->mb_intra = (~code & 0x40) >> 6;
218
219 cbp = code & 0x3f;
220 } else {
221 s->mb_intra = 1;
222 code = get_vlc2(&s->gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
223 if (code < 0)
224 return -1;
225 /* predict coded block pattern */
226 cbp = 0;
227 for(i=0;i<6;i++) {
228 int val = ((code >> (5 - i)) & 1);
229 if (i < 4) {
230 int pred = ff_msmpeg4_coded_block_pred(s, i, &coded_val);
231 val = val ^ pred;
232 *coded_val = val;
233 }
234 cbp |= val << (5 - i);
235 }
236 }
237
238 if (!s->mb_intra) {
239 int mx, my;
240 if(s->per_mb_rl_table && cbp){
241 s->rl_table_index = decode012(&s->gb);
242 s->rl_chroma_table_index = s->rl_table_index;
243 }
244 ff_h263_pred_motion(s, 0, 0, &mx, &my);
245 if (ff_msmpeg4_decode_motion(s, &mx, &my) < 0)
246 return -1;
247 s->mv_dir = MV_DIR_FORWARD;
248 s->mv_type = MV_TYPE_16X16;
249 s->mv[0][0][0] = mx;
250 s->mv[0][0][1] = my;
251 *mb_type_ptr = MB_TYPE_L0 | MB_TYPE_16x16;
252 } else {
253 ff_dlog(s, "I at %d %d %d %06X\n", s->mb_x, s->mb_y,
254 ((cbp & 3) ? 1 : 0) +((cbp & 0x3C)? 2 : 0),
255 show_bits(&s->gb, 24));
256 s->ac_pred = get_bits1(&s->gb);
257 *mb_type_ptr = MB_TYPE_INTRA;
258 if(s->inter_intra_pred){
259 s->h263_aic_dir= get_vlc2(&s->gb, ff_inter_intra_vlc.table, INTER_INTRA_VLC_BITS, 1);
260 ff_dlog(s, "%d%d %d %d/",
261 s->ac_pred, s->h263_aic_dir, s->mb_x, s->mb_y);
262 }
263 if(s->per_mb_rl_table && cbp){
264 s->rl_table_index = decode012(&s->gb);
265 s->rl_chroma_table_index = s->rl_table_index;
266 }
267 }
268
269 s->bdsp.clear_blocks(s->block[0]);
270 for (i = 0; i < 6; i++) {
271 if (ff_msmpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1, NULL) < 0)
272 {
273 av_log(s->avctx, AV_LOG_ERROR, "\nerror while decoding block: %d x %d (%d)\n", s->mb_x, s->mb_y, i);
274 return -1;
275 }
276 }
277
278 return 0;
279 }
280
281 /* init all vlc decoding tables */
282 av_cold int ff_msmpeg4_decode_init(AVCodecContext *avctx)
283 {
284 MpegEncContext *s = avctx->priv_data;
285 static int done = 0;
286 int i;
287 MVTable *mv;
288
289 if (ff_h263_decode_init(avctx) < 0)
290 return -1;
291
292 ff_msmpeg4_common_init(s);
293
294 if (!done) {
295 done = 1;
296
297 for(i=0;i<NB_RL_TABLES;i++) {
298 ff_rl_init(&ff_rl_table[i], ff_static_rl_table_store[i]);
299 }
300 INIT_VLC_RL(ff_rl_table[0], 642);
301 INIT_VLC_RL(ff_rl_table[1], 1104);
302 INIT_VLC_RL(ff_rl_table[2], 554);
303 INIT_VLC_RL(ff_rl_table[3], 940);
304 INIT_VLC_RL(ff_rl_table[4], 962);
305 INIT_VLC_RL(ff_rl_table[5], 554);
306
307 mv = &ff_mv_tables[0];
308 INIT_VLC_STATIC(&mv->vlc, MV_VLC_BITS, mv->n + 1,
309 mv->table_mv_bits, 1, 1,
310 mv->table_mv_code, 2, 2, 3714);
311 mv = &ff_mv_tables[1];
312 INIT_VLC_STATIC(&mv->vlc, MV_VLC_BITS, mv->n + 1,
313 mv->table_mv_bits, 1, 1,
314 mv->table_mv_code, 2, 2, 2694);
315
316 INIT_VLC_STATIC(&ff_msmp4_dc_luma_vlc[0], DC_VLC_BITS, 120,
317 &ff_table0_dc_lum[0][1], 8, 4,
318 &ff_table0_dc_lum[0][0], 8, 4, 1158);
319 INIT_VLC_STATIC(&ff_msmp4_dc_chroma_vlc[0], DC_VLC_BITS, 120,
320 &ff_table0_dc_chroma[0][1], 8, 4,
321 &ff_table0_dc_chroma[0][0], 8, 4, 1118);
322 INIT_VLC_STATIC(&ff_msmp4_dc_luma_vlc[1], DC_VLC_BITS, 120,
323 &ff_table1_dc_lum[0][1], 8, 4,
324 &ff_table1_dc_lum[0][0], 8, 4, 1476);
325 INIT_VLC_STATIC(&ff_msmp4_dc_chroma_vlc[1], DC_VLC_BITS, 120,
326 &ff_table1_dc_chroma[0][1], 8, 4,
327 &ff_table1_dc_chroma[0][0], 8, 4, 1216);
328
329 INIT_VLC_STATIC(&v2_dc_lum_vlc, DC_VLC_BITS, 512,
330 &ff_v2_dc_lum_table[0][1], 8, 4,
331 &ff_v2_dc_lum_table[0][0], 8, 4, 1472);
332 INIT_VLC_STATIC(&v2_dc_chroma_vlc, DC_VLC_BITS, 512,
333 &ff_v2_dc_chroma_table[0][1], 8, 4,
334 &ff_v2_dc_chroma_table[0][0], 8, 4, 1506);
335
336 INIT_VLC_STATIC(&v2_intra_cbpc_vlc, V2_INTRA_CBPC_VLC_BITS, 4,
337 &ff_v2_intra_cbpc[0][1], 2, 1,
338 &ff_v2_intra_cbpc[0][0], 2, 1, 8);
339 INIT_VLC_STATIC(&v2_mb_type_vlc, V2_MB_TYPE_VLC_BITS, 8,
340 &ff_v2_mb_type[0][1], 2, 1,
341 &ff_v2_mb_type[0][0], 2, 1, 128);
342 INIT_VLC_STATIC(&v2_mv_vlc, V2_MV_VLC_BITS, 33,
343 &ff_mvtab[0][1], 2, 1,
344 &ff_mvtab[0][0], 2, 1, 538);
345
346 INIT_VLC_STATIC(&ff_mb_non_intra_vlc[0], MB_NON_INTRA_VLC_BITS, 128,
347 &ff_wmv2_inter_table[0][0][1], 8, 4,
348 &ff_wmv2_inter_table[0][0][0], 8, 4, 1636);
349 INIT_VLC_STATIC(&ff_mb_non_intra_vlc[1], MB_NON_INTRA_VLC_BITS, 128,
350 &ff_wmv2_inter_table[1][0][1], 8, 4,
351 &ff_wmv2_inter_table[1][0][0], 8, 4, 2648);
352 INIT_VLC_STATIC(&ff_mb_non_intra_vlc[2], MB_NON_INTRA_VLC_BITS, 128,
353 &ff_wmv2_inter_table[2][0][1], 8, 4,
354 &ff_wmv2_inter_table[2][0][0], 8, 4, 1532);
355 INIT_VLC_STATIC(&ff_mb_non_intra_vlc[3], MB_NON_INTRA_VLC_BITS, 128,
356 &ff_wmv2_inter_table[3][0][1], 8, 4,
357 &ff_wmv2_inter_table[3][0][0], 8, 4, 2488);
358
359 INIT_VLC_STATIC(&ff_msmp4_mb_i_vlc, MB_INTRA_VLC_BITS, 64,
360 &ff_msmp4_mb_i_table[0][1], 4, 2,
361 &ff_msmp4_mb_i_table[0][0], 4, 2, 536);
362
363 INIT_VLC_STATIC(&ff_inter_intra_vlc, INTER_INTRA_VLC_BITS, 4,
364 &ff_table_inter_intra[0][1], 2, 1,
365 &ff_table_inter_intra[0][0], 2, 1, 8);
366 }
367
368 switch(s->msmpeg4_version){
369 case 1:
370 case 2:
371 s->decode_mb= msmpeg4v12_decode_mb;
372 break;
373 case 3:
374 case 4:
375 s->decode_mb= msmpeg4v34_decode_mb;
376 break;
377 case 5:
378 if (CONFIG_WMV2_DECODER)
379 s->decode_mb= ff_wmv2_decode_mb;
380 case 6:
381 //FIXME + TODO VC1 decode mb
382 break;
383 }
384
385 s->slice_height= s->mb_height; //to avoid 1/0 if the first frame is not a keyframe
386
387 return 0;
388 }
389
390 int ff_msmpeg4_decode_picture_header(MpegEncContext * s)
391 {
392 int code;
393
394 if(s->msmpeg4_version==1){
395 int start_code = get_bits_long(&s->gb, 32);
396 if(start_code!=0x00000100){
397 av_log(s->avctx, AV_LOG_ERROR, "invalid startcode\n");
398 return -1;
399 }
400
401 skip_bits(&s->gb, 5); // frame number */
402 }
403
404 s->pict_type = get_bits(&s->gb, 2) + 1;
405 if (s->pict_type != AV_PICTURE_TYPE_I &&
406 s->pict_type != AV_PICTURE_TYPE_P){
407 av_log(s->avctx, AV_LOG_ERROR, "invalid picture type\n");
408 return -1;
409 }
410 #if 0
411 {
412 static int had_i=0;
413 if(s->pict_type == AV_PICTURE_TYPE_I) had_i=1;
414 if(!had_i) return -1;
415 }
416 #endif
417 s->chroma_qscale= s->qscale = get_bits(&s->gb, 5);
418 if(s->qscale==0){
419 av_log(s->avctx, AV_LOG_ERROR, "invalid qscale\n");
420 return -1;
421 }
422
423 if (s->pict_type == AV_PICTURE_TYPE_I) {
424 code = get_bits(&s->gb, 5);
425 if(s->msmpeg4_version==1){
426 if(code==0 || code>s->mb_height){
427 av_log(s->avctx, AV_LOG_ERROR, "invalid slice height %d\n", code);
428 return -1;
429 }
430
431 s->slice_height = code;
432 }else{
433 /* 0x17: one slice, 0x18: two slices, ... */
434 if (code < 0x17){
435 av_log(s->avctx, AV_LOG_ERROR, "error, slice code was %X\n", code);
436 return -1;
437 }
438
439 s->slice_height = s->mb_height / (code - 0x16);
440 }
441
442 switch(s->msmpeg4_version){
443 case 1:
444 case 2:
445 s->rl_chroma_table_index = 2;
446 s->rl_table_index = 2;
447
448 s->dc_table_index = 0; //not used
449 break;
450 case 3:
451 s->rl_chroma_table_index = decode012(&s->gb);
452 s->rl_table_index = decode012(&s->gb);
453
454 s->dc_table_index = get_bits1(&s->gb);
455 break;
456 case 4:
457 ff_msmpeg4_decode_ext_header(s, (2+5+5+17+7)/8);
458
459 if(s->bit_rate > MBAC_BITRATE) s->per_mb_rl_table= get_bits1(&s->gb);
460 else s->per_mb_rl_table= 0;
461
462 if(!s->per_mb_rl_table){
463 s->rl_chroma_table_index = decode012(&s->gb);
464 s->rl_table_index = decode012(&s->gb);
465 }
466
467 s->dc_table_index = get_bits1(&s->gb);
468 s->inter_intra_pred= 0;
469 break;
470 }
471 s->no_rounding = 1;
472 if(s->avctx->debug&FF_DEBUG_PICT_INFO)
473 av_log(s->avctx, AV_LOG_DEBUG, "qscale:%d rlc:%d rl:%d dc:%d mbrl:%d slice:%d \n",
474 s->qscale,
475 s->rl_chroma_table_index,
476 s->rl_table_index,
477 s->dc_table_index,
478 s->per_mb_rl_table,
479 s->slice_height);
480 } else {
481 switch(s->msmpeg4_version){
482 case 1:
483 case 2:
484 if(s->msmpeg4_version==1)
485 s->use_skip_mb_code = 1;
486 else
487 s->use_skip_mb_code = get_bits1(&s->gb);
488 s->rl_table_index = 2;
489 s->rl_chroma_table_index = s->rl_table_index;
490 s->dc_table_index = 0; //not used
491 s->mv_table_index = 0;
492 break;
493 case 3:
494 s->use_skip_mb_code = get_bits1(&s->gb);
495 s->rl_table_index = decode012(&s->gb);
496 s->rl_chroma_table_index = s->rl_table_index;
497
498 s->dc_table_index = get_bits1(&s->gb);
499
500 s->mv_table_index = get_bits1(&s->gb);
501 break;
502 case 4:
503 s->use_skip_mb_code = get_bits1(&s->gb);
504
505 if(s->bit_rate > MBAC_BITRATE) s->per_mb_rl_table= get_bits1(&s->gb);
506 else s->per_mb_rl_table= 0;
507
508 if(!s->per_mb_rl_table){
509 s->rl_table_index = decode012(&s->gb);
510 s->rl_chroma_table_index = s->rl_table_index;
511 }
512
513 s->dc_table_index = get_bits1(&s->gb);
514
515 s->mv_table_index = get_bits1(&s->gb);
516 s->inter_intra_pred= (s->width*s->height < 320*240 && s->bit_rate<=II_BITRATE);
517 break;
518 }
519
520 if(s->avctx->debug&FF_DEBUG_PICT_INFO)
521 av_log(s->avctx, AV_LOG_DEBUG, "skip:%d rl:%d rlc:%d dc:%d mv:%d mbrl:%d qp:%d \n",
522 s->use_skip_mb_code,
523 s->rl_table_index,
524 s->rl_chroma_table_index,
525 s->dc_table_index,
526 s->mv_table_index,
527 s->per_mb_rl_table,
528 s->qscale);
529
530 if(s->flipflop_rounding){
531 s->no_rounding ^= 1;
532 }else{
533 s->no_rounding = 0;
534 }
535 }
536 ff_dlog(s->avctx, "%d %d %d %d %d\n", s->pict_type, s->bit_rate,
537 s->inter_intra_pred, s->width, s->height);
538
539 s->esc3_level_length= 0;
540 s->esc3_run_length= 0;
541
542 return 0;
543 }
544
545 int ff_msmpeg4_decode_ext_header(MpegEncContext * s, int buf_size)
546 {
547 int left= buf_size*8 - get_bits_count(&s->gb);
548 int length= s->msmpeg4_version>=3 ? 17 : 16;
549 /* the alt_bitstream reader could read over the end so we need to check it */
550 if(left>=length && left<length+8)
551 {
552 skip_bits(&s->gb, 5); /* fps */
553 s->bit_rate= get_bits(&s->gb, 11)*1024;
554 if(s->msmpeg4_version>=3)
555 s->flipflop_rounding= get_bits1(&s->gb);
556 else
557 s->flipflop_rounding= 0;
558 }
559 else if(left<length+8)
560 {
561 s->flipflop_rounding= 0;
562 if(s->msmpeg4_version != 2)
563 av_log(s->avctx, AV_LOG_ERROR, "ext header missing, %d left\n", left);
564 }
565 else
566 {
567 av_log(s->avctx, AV_LOG_ERROR, "I frame too long, ignoring ext header\n");
568 }
569
570 return 0;
571 }
572
573 static int msmpeg4_decode_dc(MpegEncContext * s, int n, int *dir_ptr)
574 {
575 int level, pred;
576
577 if(s->msmpeg4_version<=2){
578 if (n < 4) {
579 level = get_vlc2(&s->gb, v2_dc_lum_vlc.table, DC_VLC_BITS, 3);
580 } else {
581 level = get_vlc2(&s->gb, v2_dc_chroma_vlc.table, DC_VLC_BITS, 3);
582 }
583 if (level < 0)
584 return -1;
585 level-=256;
586 }else{ //FIXME optimize use unified tables & index
587 if (n < 4) {
588 level = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
589 } else {
590 level = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
591 }
592 if (level < 0){
593 av_log(s->avctx, AV_LOG_ERROR, "illegal dc vlc\n");
594 return -1;
595 }
596
597 if (level == DC_MAX) {
598 level = get_bits(&s->gb, 8);
599 if (get_bits1(&s->gb))
600 level = -level;
601 } else if (level != 0) {
602 if (get_bits1(&s->gb))
603 level = -level;
604 }
605 }
606
607 if(s->msmpeg4_version==1){
608 int32_t *dc_val;
609 pred = msmpeg4v1_pred_dc(s, n, &dc_val);
610 level += pred;
611
612 /* update predictor */
613 *dc_val= level;
614 }else{
615 int16_t *dc_val;
616 pred = ff_msmpeg4_pred_dc(s, n, &dc_val, dir_ptr);
617 level += pred;
618
619 /* update predictor */
620 if (n < 4) {
621 *dc_val = level * s->y_dc_scale;
622 } else {
623 *dc_val = level * s->c_dc_scale;
624 }
625 }
626
627 return level;
628 }
629
630 //#define ERROR_DETAILS
631 int ff_msmpeg4_decode_block(MpegEncContext * s, int16_t * block,
632 int n, int coded, const uint8_t *scan_table)
633 {
634 int level, i, last, run, run_diff;
635 int av_uninit(dc_pred_dir);
636 RLTable *rl;
637 RL_VLC_ELEM *rl_vlc;
638 int qmul, qadd;
639
640 if (s->mb_intra) {
641 qmul=1;
642 qadd=0;
643
644 /* DC coef */
645 level = msmpeg4_decode_dc(s, n, &dc_pred_dir);
646
647 if (level < 0){
648 av_log(s->avctx, AV_LOG_ERROR, "dc overflow- block: %d qscale: %d//\n", n, s->qscale);
649 if(s->inter_intra_pred) level=0;
650 else return -1;
651 }
652 if (n < 4) {
653 rl = &ff_rl_table[s->rl_table_index];
654 if(level > 256*s->y_dc_scale){
655 av_log(s->avctx, AV_LOG_ERROR, "dc overflow+ L qscale: %d//\n", s->qscale);
656 if(!s->inter_intra_pred) return -1;
657 }
658 } else {
659 rl = &ff_rl_table[3 + s->rl_chroma_table_index];
660 if(level > 256*s->c_dc_scale){
661 av_log(s->avctx, AV_LOG_ERROR, "dc overflow+ C qscale: %d//\n", s->qscale);
662 if(!s->inter_intra_pred) return -1;
663 }
664 }
665 block[0] = level;
666
667 run_diff = s->msmpeg4_version >= 4;
668 i = 0;
669 if (!coded) {
670 goto not_coded;
671 }
672 if (s->ac_pred) {
673 if (dc_pred_dir == 0)
674 scan_table = s->intra_v_scantable.permutated; /* left */
675 else
676 scan_table = s->intra_h_scantable.permutated; /* top */
677 } else {
678 scan_table = s->intra_scantable.permutated;
679 }
680 rl_vlc= rl->rl_vlc[0];
681 } else {
682 qmul = s->qscale << 1;
683 qadd = (s->qscale - 1) | 1;
684 i = -1;
685 rl = &ff_rl_table[3 + s->rl_table_index];
686
687 if(s->msmpeg4_version==2)
688 run_diff = 0;
689 else
690 run_diff = 1;
691
692 if (!coded) {
693 s->block_last_index[n] = i;
694 return 0;
695 }
696 if(!scan_table)
697 scan_table = s->inter_scantable.permutated;
698 rl_vlc= rl->rl_vlc[s->qscale];
699 }
700 {
701 OPEN_READER(re, &s->gb);
702 for(;;) {
703 UPDATE_CACHE(re, &s->gb);
704 GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 0);
705 if (level==0) {
706 int cache;
707 cache= GET_CACHE(re, &s->gb);
708 /* escape */
709 if (s->msmpeg4_version==1 || (cache&0x80000000)==0) {
710 if (s->msmpeg4_version==1 || (cache&0x40000000)==0) {
711 /* third escape */
712 if(s->msmpeg4_version!=1) LAST_SKIP_BITS(re, &s->gb, 2);
713 UPDATE_CACHE(re, &s->gb);
714 if(s->msmpeg4_version<=3){
715 last= SHOW_UBITS(re, &s->gb, 1); SKIP_CACHE(re, &s->gb, 1);
716 run= SHOW_UBITS(re, &s->gb, 6); SKIP_CACHE(re, &s->gb, 6);
717 level= SHOW_SBITS(re, &s->gb, 8);
718 SKIP_COUNTER(re, &s->gb, 1+6+8);
719 }else{
720 int sign;
721 last= SHOW_UBITS(re, &s->gb, 1); SKIP_BITS(re, &s->gb, 1);
722 if(!s->esc3_level_length){
723 int ll;
724 ff_dlog(s->avctx, "ESC-3 %X at %d %d\n",
725 show_bits(&s->gb, 24), s->mb_x, s->mb_y);
726 if(s->qscale<8){
727 ll= SHOW_UBITS(re, &s->gb, 3); SKIP_BITS(re, &s->gb, 3);
728 if(ll==0){
729 ll= 8+SHOW_UBITS(re, &s->gb, 1); SKIP_BITS(re, &s->gb, 1);
730 }
731 }else{
732 ll=2;
733 while(ll<8 && SHOW_UBITS(re, &s->gb, 1)==0){
734 ll++;
735 SKIP_BITS(re, &s->gb, 1);
736 }
737 if(ll<8) SKIP_BITS(re, &s->gb, 1);
738 }
739
740 s->esc3_level_length= ll;
741 s->esc3_run_length= SHOW_UBITS(re, &s->gb, 2) + 3; SKIP_BITS(re, &s->gb, 2);
742 UPDATE_CACHE(re, &s->gb);
743 }
744 run= SHOW_UBITS(re, &s->gb, s->esc3_run_length);
745 SKIP_BITS(re, &s->gb, s->esc3_run_length);
746
747 sign= SHOW_UBITS(re, &s->gb, 1);
748 SKIP_BITS(re, &s->gb, 1);
749
750 level= SHOW_UBITS(re, &s->gb, s->esc3_level_length);
751 SKIP_BITS(re, &s->gb, s->esc3_level_length);
752 if(sign) level= -level;
753 }
754
755 #if 0 // waste of time / this will detect very few errors
756 {
757 const int abs_level= FFABS(level);
758 const int run1= run - rl->max_run[last][abs_level] - run_diff;
759 if(abs_level<=MAX_LEVEL && run<=MAX_RUN){
760 if(abs_level <= rl->max_level[last][run]){
761 av_log(s->avctx, AV_LOG_ERROR, "illegal 3. esc, vlc encoding possible\n");
762 return DECODING_AC_LOST;
763 }
764 if(abs_level <= rl->max_level[last][run]*2){
765 av_log(s->avctx, AV_LOG_ERROR, "illegal 3. esc, esc 1 encoding possible\n");
766 return DECODING_AC_LOST;
767 }
768 if(run1>=0 && abs_level <= rl->max_level[last][run1]){
769 av_log(s->avctx, AV_LOG_ERROR, "illegal 3. esc, esc 2 encoding possible\n");
770 return DECODING_AC_LOST;
771 }
772 }
773 }
774 #endif
775 //level = level * qmul + (level>0) * qadd - (level<=0) * qadd ;
776 if (level>0) level= level * qmul + qadd;
777 else level= level * qmul - qadd;
778 #if 0 // waste of time too :(
779 if(level>2048 || level<-2048){
780 av_log(s->avctx, AV_LOG_ERROR, "|level| overflow in 3. esc\n");
781 return DECODING_AC_LOST;
782 }
783 #endif
784 i+= run + 1;
785 if(last) i+=192;
786 #ifdef ERROR_DETAILS
787 if(run==66)
788 av_log(s->avctx, AV_LOG_ERROR, "illegal vlc code in ESC3 level=%d\n", level);
789 else if((i>62 && i<192) || i>192+63)
790 av_log(s->avctx, AV_LOG_ERROR, "run overflow in ESC3 i=%d run=%d level=%d\n", i, run, level);
791 #endif
792 } else {
793 /* second escape */
794 SKIP_BITS(re, &s->gb, 2);
795 GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 1);
796 i+= run + rl->max_run[run>>7][level/qmul] + run_diff; //FIXME opt indexing
797 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
798 LAST_SKIP_BITS(re, &s->gb, 1);
799 #ifdef ERROR_DETAILS
800 if(run==66)
801 av_log(s->avctx, AV_LOG_ERROR, "illegal vlc code in ESC2 level=%d\n", level);
802 else if((i>62 && i<192) || i>192+63)
803 av_log(s->avctx, AV_LOG_ERROR, "run overflow in ESC2 i=%d run=%d level=%d\n", i, run, level);
804 #endif
805 }
806 } else {
807 /* first escape */
808 SKIP_BITS(re, &s->gb, 1);
809 GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 1);
810 i+= run;
811 level = level + rl->max_level[run>>7][(run-1)&63] * qmul;//FIXME opt indexing
812 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
813 LAST_SKIP_BITS(re, &s->gb, 1);
814 #ifdef ERROR_DETAILS
815 if(run==66)
816 av_log(s->avctx, AV_LOG_ERROR, "illegal vlc code in ESC1 level=%d\n", level);
817 else if((i>62 && i<192) || i>192+63)
818 av_log(s->avctx, AV_LOG_ERROR, "run overflow in ESC1 i=%d run=%d level=%d\n", i, run, level);
819 #endif
820 }
821 } else {
822 i+= run;
823 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
824 LAST_SKIP_BITS(re, &s->gb, 1);
825 #ifdef ERROR_DETAILS
826 if(run==66)
827 av_log(s->avctx, AV_LOG_ERROR, "illegal vlc code level=%d\n", level);
828 else if((i>62 && i<192) || i>192+63)
829 av_log(s->avctx, AV_LOG_ERROR, "run overflow i=%d run=%d level=%d\n", i, run, level);
830 #endif
831 }
832 if (i > 62){
833 i-= 192;
834 if(i&(~63)){
835 const int left= get_bits_left(&s->gb);
836 if (((i + 192 == 64 && level / qmul == -1) ||
837 !(s->avctx->err_recognition & AV_EF_BITSTREAM)) &&
838 left >= 0) {
839 av_log(s->avctx, AV_LOG_ERROR, "ignoring overflow at %d %d\n", s->mb_x, s->mb_y);
840 break;
841 }else{
842 av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
843 return -1;
844 }
845 }
846
847 block[scan_table[i]] = level;
848 break;
849 }
850
851 block[scan_table[i]] = level;
852 }
853 CLOSE_READER(re, &s->gb);
854 }
855 not_coded:
856 if (s->mb_intra) {
857 ff_mpeg4_pred_ac(s, block, n, dc_pred_dir);
858 if (s->ac_pred) {
859 i = 63; /* XXX: not optimal */
860 }
861 }
862 if(s->msmpeg4_version>=4 && i>0) i=63; //FIXME/XXX optimize
863 s->block_last_index[n] = i;
864
865 return 0;
866 }
867
868 int ff_msmpeg4_decode_motion(MpegEncContext * s,
869 int *mx_ptr, int *my_ptr)
870 {
871 MVTable *mv;
872 int code, mx, my;
873
874 mv = &ff_mv_tables[s->mv_table_index];
875
876 code = get_vlc2(&s->gb, mv->vlc.table, MV_VLC_BITS, 2);
877 if (code < 0){
878 av_log(s->avctx, AV_LOG_ERROR, "illegal MV code at %d %d\n", s->mb_x, s->mb_y);
879 return -1;
880 }
881 if (code == mv->n) {
882 mx = get_bits(&s->gb, 6);
883 my = get_bits(&s->gb, 6);
884 } else {
885 mx = mv->table_mvx[code];
886 my = mv->table_mvy[code];
887 }
888
889 mx += *mx_ptr - 32;
890 my += *my_ptr - 32;
891 /* WARNING : they do not do exactly modulo encoding */
892 if (mx <= -64)
893 mx += 64;
894 else if (mx >= 64)
895 mx -= 64;
896
897 if (my <= -64)
898 my += 64;
899 else if (my >= 64)
900 my -= 64;
901 *mx_ptr = mx;
902 *my_ptr = my;
903 return 0;
904 }
905
906 AVCodec ff_msmpeg4v1_decoder = {
907 .name = "msmpeg4v1",
908 .long_name = NULL_IF_CONFIG_SMALL("MPEG-4 part 2 Microsoft variant version 1"),
909 .type = AVMEDIA_TYPE_VIDEO,
910 .id = AV_CODEC_ID_MSMPEG4V1,
911 .priv_data_size = sizeof(MpegEncContext),
912 .init = ff_msmpeg4_decode_init,
913 .close = ff_h263_decode_end,
914 .decode = ff_h263_decode_frame,
915 .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
916 .pix_fmts = (const enum AVPixelFormat[]) {
917 AV_PIX_FMT_YUV420P,
918 AV_PIX_FMT_NONE
919 },
920 };
921
922 AVCodec ff_msmpeg4v2_decoder = {
923 .name = "msmpeg4v2",
924 .long_name = NULL_IF_CONFIG_SMALL("MPEG-4 part 2 Microsoft variant version 2"),
925 .type = AVMEDIA_TYPE_VIDEO,
926 .id = AV_CODEC_ID_MSMPEG4V2,
927 .priv_data_size = sizeof(MpegEncContext),
928 .init = ff_msmpeg4_decode_init,
929 .close = ff_h263_decode_end,
930 .decode = ff_h263_decode_frame,
931 .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
932 .pix_fmts = (const enum AVPixelFormat[]) {
933 AV_PIX_FMT_YUV420P,
934 AV_PIX_FMT_NONE
935 },
936 };
937
938 AVCodec ff_msmpeg4v3_decoder = {
939 .name = "msmpeg4",
940 .long_name = NULL_IF_CONFIG_SMALL("MPEG-4 part 2 Microsoft variant version 3"),
941 .type = AVMEDIA_TYPE_VIDEO,
942 .id = AV_CODEC_ID_MSMPEG4V3,
943 .priv_data_size = sizeof(MpegEncContext),
944 .init = ff_msmpeg4_decode_init,
945 .close = ff_h263_decode_end,
946 .decode = ff_h263_decode_frame,
947 .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
948 .pix_fmts = (const enum AVPixelFormat[]) {
949 AV_PIX_FMT_YUV420P,
950 AV_PIX_FMT_NONE
951 },
952 };
953
954 AVCodec ff_wmv1_decoder = {
955 .name = "wmv1",
956 .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 7"),
957 .type = AVMEDIA_TYPE_VIDEO,
958 .id = AV_CODEC_ID_WMV1,
959 .priv_data_size = sizeof(MpegEncContext),
960 .init = ff_msmpeg4_decode_init,
961 .close = ff_h263_decode_end,
962 .decode = ff_h263_decode_frame,
963 .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
964 .pix_fmts = (const enum AVPixelFormat[]) {
965 AV_PIX_FMT_YUV420P,
966 AV_PIX_FMT_NONE
967 },
968 };