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