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