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