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