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