Mercurial > libavcodec.hg
annotate msmpeg4.c @ 2871:f4afa3812818 libavcodec
Fix compile without CONFIG_GPL, misplaced #endif caused a missing }.
author | reimar |
---|---|
date | Sat, 10 Sep 2005 19:30:40 +0000 |
parents | 40765c51a7a9 |
children | ef2149182f1c |
rev | line source |
---|---|
0 | 1 /* |
2 * MSMPEG4 backend for ffmpeg encoder and decoder | |
429 | 3 * Copyright (c) 2001 Fabrice Bellard. |
1739
07a484280a82
copyright year update of the files i touched and remembered, things look annoyingly unmaintained otherwise
michael
parents:
1651
diff
changeset
|
4 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at> |
0 | 5 * |
429 | 6 * This library is free software; you can redistribute it and/or |
7 * modify it under the terms of the GNU Lesser General Public | |
8 * License as published by the Free Software Foundation; either | |
9 * version 2 of the License, or (at your option) any later version. | |
0 | 10 * |
429 | 11 * This library is distributed in the hope that it will be useful, |
0 | 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
429 | 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14 * Lesser General Public License for more details. | |
0 | 15 * |
429 | 16 * You should have received a copy of the GNU Lesser General Public |
17 * License along with this library; if not, write to the Free Software | |
18 * 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
|
19 * |
457 | 20 * msmpeg4v1 & v2 stuff by Michael Niedermayer <michaelni@gmx.at> |
0 | 21 */ |
1106 | 22 |
23 /** | |
24 * @file msmpeg4.c | |
25 * MSMPEG4 backend for ffmpeg encoder and decoder. | |
26 */ | |
27 | |
396
fce0a2520551
removed useless header includes - use av memory functions
glantau
parents:
347
diff
changeset
|
28 #include "avcodec.h" |
0 | 29 #include "dsputil.h" |
30 #include "mpegvideo.h" | |
411
5c8b3a717929
workaround dc_scale bug in old ffmpeg msmpeg4v3 encoder (set workaround_bugs=1 for this)
michaelni
parents:
396
diff
changeset
|
31 |
0 | 32 /* |
33 * You can also call this codec : MPEG4 with a twist ! | |
34 * | |
35 * TODO: | |
36 * - (encoding) select best mv table (two choices) | |
37 * - (encoding) select best vlc/dc table | |
38 */ | |
39 //#define DEBUG | |
40 | |
531 | 41 #define DC_VLC_BITS 9 |
42 #define CBPY_VLC_BITS 6 | |
43 #define INTER_INTRA_VLC_BITS 3 | |
44 #define V1_INTRA_CBPC_VLC_BITS 6 | |
45 #define V1_INTER_CBPC_VLC_BITS 6 | |
46 #define V2_INTRA_CBPC_VLC_BITS 3 | |
47 #define V2_MB_TYPE_VLC_BITS 7 | |
48 #define MV_VLC_BITS 9 | |
49 #define V2_MV_VLC_BITS 9 | |
50 #define TEX_VLC_BITS 9 | |
51 #define MB_NON_INTRA_VLC_BITS 9 | |
52 #define MB_INTRA_VLC_BITS 9 | |
53 | |
578 | 54 #define II_BITRATE 128*1024 |
55 #define MBAC_BITRATE 50*1024 | |
56 | |
936 | 57 #define DEFAULT_INTER_INDEX 3 |
58 | |
1064 | 59 static uint32_t v2_dc_lum_table[512][2]; |
60 static uint32_t v2_dc_chroma_table[512][2]; | |
307 | 61 |
499 | 62 static inline void msmpeg4_encode_block(MpegEncContext * s, DCTELEM * block, int n); |
63 static inline int msmpeg4_decode_block(MpegEncContext * s, DCTELEM * block, | |
936 | 64 int n, int coded, const uint8_t *scantable); |
0 | 65 static int msmpeg4_decode_dc(MpegEncContext * s, int n, int *dir_ptr); |
66 static int msmpeg4_decode_motion(MpegEncContext * s, | |
67 int *mx_ptr, int *my_ptr); | |
310 | 68 static void msmpeg4v2_encode_motion(MpegEncContext * s, int val); |
468 | 69 static void init_h263_dc_for_msmpeg4(void); |
519 | 70 static inline void msmpeg4_memsetw(short *tab, int val, int n); |
1070
6da5ae9ee199
more #ifdef CONFIG_ENCODERS patch by (Wolfgang Hesseler <qv at multimediaware dot com>) with modifications by me (s/WOLFGANG/CONFIG_ENCODERS/ and some other fixes)
michaelni
parents:
1064
diff
changeset
|
71 #ifdef CONFIG_ENCODERS |
601 | 72 static int get_size_of_code(MpegEncContext * s, RLTable *rl, int last, int run, int level, int intra); |
1070
6da5ae9ee199
more #ifdef CONFIG_ENCODERS patch by (Wolfgang Hesseler <qv at multimediaware dot com>) with modifications by me (s/WOLFGANG/CONFIG_ENCODERS/ and some other fixes)
michaelni
parents:
1064
diff
changeset
|
73 #endif //CONFIG_ENCODERS |
745
25d7fb7c89be
better/cleaner error resilience (done in a 2nd pass after decoding)
michaelni
parents:
743
diff
changeset
|
74 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
|
75 static int msmpeg4v34_decode_mb(MpegEncContext *s, DCTELEM block[6][64]); |
936 | 76 static int wmv2_decode_mb(MpegEncContext *s, DCTELEM block[6][64]); |
0 | 77 |
2474 | 78 /* vc9 externs */ |
79 extern uint8_t wmv3_dc_scale_table[32]; | |
601 | 80 |
0 | 81 #ifdef DEBUG |
82 int intra_count = 0; | |
83 int frame_count = 0; | |
84 #endif | |
85 | |
86 #include "msmpeg4data.h" | |
87 | |
1325 | 88 #ifdef CONFIG_ENCODERS //strangely gcc includes this even if its not references |
1064 | 89 static uint8_t rl_length[NB_RL_TABLES][MAX_LEVEL+1][MAX_RUN+1][2]; |
1325 | 90 #endif //CONFIG_ENCODERS |
601 | 91 |
0 | 92 #ifdef STATS |
93 | |
94 const char *st_names[ST_NB] = { | |
95 "unknown", | |
96 "dc", | |
97 "intra_ac", | |
98 "inter_ac", | |
99 "intra_mb", | |
100 "inter_mb", | |
101 "mv", | |
102 }; | |
103 | |
104 int st_current_index = 0; | |
105 unsigned int st_bit_counts[ST_NB]; | |
106 unsigned int st_out_bit_counts[ST_NB]; | |
107 | |
108 #define set_stat(var) st_current_index = var; | |
109 | |
110 void print_stats(void) | |
111 { | |
112 unsigned int total; | |
113 int i; | |
114 | |
115 printf("Input:\n"); | |
116 total = 0; | |
117 for(i=0;i<ST_NB;i++) | |
118 total += st_bit_counts[i]; | |
119 if (total == 0) | |
120 total = 1; | |
121 for(i=0;i<ST_NB;i++) { | |
122 printf("%-10s : %10.1f %5.1f%%\n", | |
123 st_names[i], | |
124 (double)st_bit_counts[i] / 8.0, | |
125 (double)st_bit_counts[i] * 100.0 / total); | |
126 } | |
127 printf("%-10s : %10.1f %5.1f%%\n", | |
128 "total", | |
129 (double)total / 8.0, | |
130 100.0); | |
131 | |
132 printf("Output:\n"); | |
133 total = 0; | |
134 for(i=0;i<ST_NB;i++) | |
135 total += st_out_bit_counts[i]; | |
136 if (total == 0) | |
137 total = 1; | |
138 for(i=0;i<ST_NB;i++) { | |
139 printf("%-10s : %10.1f %5.1f%%\n", | |
140 st_names[i], | |
141 (double)st_out_bit_counts[i] / 8.0, | |
142 (double)st_out_bit_counts[i] * 100.0 / total); | |
143 } | |
144 printf("%-10s : %10.1f %5.1f%%\n", | |
145 "total", | |
146 (double)total / 8.0, | |
147 100.0); | |
148 } | |
149 | |
150 #else | |
151 | |
152 #define set_stat(var) | |
153 | |
154 #endif | |
155 | |
499 | 156 static void common_init(MpegEncContext * s) |
157 { | |
158 static int inited=0; | |
159 | |
160 switch(s->msmpeg4_version){ | |
161 case 1: | |
162 case 2: | |
163 s->y_dc_scale_table= | |
164 s->c_dc_scale_table= ff_mpeg1_dc_scale_table; | |
165 break; | |
166 case 3: | |
167 if(s->workaround_bugs){ | |
168 s->y_dc_scale_table= old_ff_y_dc_scale_table; | |
169 s->c_dc_scale_table= old_ff_c_dc_scale_table; | |
170 } else{ | |
171 s->y_dc_scale_table= ff_mpeg4_y_dc_scale_table; | |
172 s->c_dc_scale_table= ff_mpeg4_c_dc_scale_table; | |
173 } | |
174 break; | |
175 case 4: | |
936 | 176 case 5: |
499 | 177 s->y_dc_scale_table= wmv1_y_dc_scale_table; |
178 s->c_dc_scale_table= wmv1_c_dc_scale_table; | |
179 break; | |
2639 | 180 #if defined(CONFIG_WMV3_DECODER)||defined(CONFIG_VC9_DECODER) |
2474 | 181 case 6: |
182 s->y_dc_scale_table= wmv3_dc_scale_table; | |
183 s->c_dc_scale_table= wmv3_dc_scale_table; | |
184 break; | |
2639 | 185 #endif |
2474 | 186 |
499 | 187 } |
188 | |
706
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
601
diff
changeset
|
189 |
936 | 190 if(s->msmpeg4_version>=4){ |
1273 | 191 ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , wmv1_scantable[1]); |
192 ff_init_scantable(s->dsp.idct_permutation, &s->intra_h_scantable, wmv1_scantable[2]); | |
193 ff_init_scantable(s->dsp.idct_permutation, &s->intra_v_scantable, wmv1_scantable[3]); | |
194 ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , wmv1_scantable[0]); | |
499 | 195 } |
706
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
601
diff
changeset
|
196 //Note the default tables are set in common_init in mpegvideo.c |
499 | 197 |
198 if(!inited){ | |
199 inited=1; | |
200 | |
201 init_h263_dc_for_msmpeg4(); | |
202 } | |
203 } | |
204 | |
1070
6da5ae9ee199
more #ifdef CONFIG_ENCODERS patch by (Wolfgang Hesseler <qv at multimediaware dot com>) with modifications by me (s/WOLFGANG/CONFIG_ENCODERS/ and some other fixes)
michaelni
parents:
1064
diff
changeset
|
205 #ifdef CONFIG_ENCODERS |
6da5ae9ee199
more #ifdef CONFIG_ENCODERS patch by (Wolfgang Hesseler <qv at multimediaware dot com>) with modifications by me (s/WOLFGANG/CONFIG_ENCODERS/ and some other fixes)
michaelni
parents:
1064
diff
changeset
|
206 |
0 | 207 /* build the table which associate a (x,y) motion vector to a vlc */ |
208 static void init_mv_table(MVTable *tab) | |
209 { | |
210 int i, x, y; | |
211 | |
1064 | 212 tab->table_mv_index = av_malloc(sizeof(uint16_t) * 4096); |
0 | 213 /* mark all entries as not used */ |
214 for(i=0;i<4096;i++) | |
215 tab->table_mv_index[i] = tab->n; | |
216 | |
217 for(i=0;i<tab->n;i++) { | |
218 x = tab->table_mvx[i]; | |
219 y = tab->table_mvy[i]; | |
220 tab->table_mv_index[(x << 6) | y] = i; | |
221 } | |
222 } | |
223 | |
224 static void code012(PutBitContext *pb, int n) | |
225 { | |
226 if (n == 0) { | |
227 put_bits(pb, 1, 0); | |
228 } else { | |
229 put_bits(pb, 1, 1); | |
230 put_bits(pb, 1, (n >= 2)); | |
231 } | |
232 } | |
233 | |
499 | 234 void ff_msmpeg4_encode_init(MpegEncContext *s) |
235 { | |
236 static int init_done=0; | |
237 int i; | |
238 | |
239 common_init(s); | |
240 if(s->msmpeg4_version>=4){ | |
241 s->min_qcoeff= -255; | |
242 s->max_qcoeff= 255; | |
243 } | |
244 | |
245 if (!init_done) { | |
246 /* init various encoding tables */ | |
247 init_done = 1; | |
248 init_mv_table(&mv_tables[0]); | |
249 init_mv_table(&mv_tables[1]); | |
250 for(i=0;i<NB_RL_TABLES;i++) | |
2370
26560d4fdb1f
Memory leak fix patch by (Burkhard Plaum <plaum >at< ipf.uni-stuttgart )dot( de>)
michael
parents:
2293
diff
changeset
|
251 init_rl(&rl_table[i], 1); |
601 | 252 |
253 for(i=0; i<NB_RL_TABLES; i++){ | |
254 int level; | |
255 for(level=0; level<=MAX_LEVEL; level++){ | |
256 int run; | |
257 for(run=0; run<=MAX_RUN; run++){ | |
258 int last; | |
259 for(last=0; last<2; last++){ | |
774
baa66649df35
mergeing inter & intra rl_length tables (50% smaller & no meassureable difference in filesize/quality)
michaelni
parents:
773
diff
changeset
|
260 rl_length[i][level][run][last]= get_size_of_code(s, &rl_table[ i], last, run, level, 0); |
601 | 261 } |
262 } | |
263 } | |
264 } | |
499 | 265 } |
266 } | |
267 | |
268 static int get_size_of_code(MpegEncContext * s, RLTable *rl, int last, int run, int level, int intra){ | |
269 int size=0; | |
270 int code; | |
271 int run_diff= intra ? 0 : 1; | |
272 | |
273 code = get_rl_index(rl, last, run, level); | |
274 size+= rl->table_vlc[code][1]; | |
275 if (code == rl->n) { | |
276 int level1, run1; | |
277 | |
278 level1 = level - rl->max_level[last][run]; | |
279 if (level1 < 1) | |
280 goto esc2; | |
281 code = get_rl_index(rl, last, run, level1); | |
282 if (code == rl->n) { | |
283 esc2: | |
284 size++; | |
285 if (level > MAX_LEVEL) | |
286 goto esc3; | |
287 run1 = run - rl->max_run[last][level] - run_diff; | |
288 if (run1 < 0) | |
289 goto esc3; | |
290 code = get_rl_index(rl, last, run1, level); | |
291 if (code == rl->n) { | |
292 esc3: | |
293 /* third escape */ | |
294 size+=1+1+6+8; | |
295 } else { | |
296 /* second escape */ | |
297 size+= 1+1+ rl->table_vlc[code][1]; | |
298 } | |
299 } else { | |
300 /* first escape */ | |
301 size+= 1+1+ rl->table_vlc[code][1]; | |
302 } | |
303 } else { | |
304 size++; | |
305 } | |
306 return size; | |
307 } | |
308 | |
309 static void find_best_tables(MpegEncContext * s) | |
310 { | |
311 int i; | |
312 int best =-1, best_size =9999999; | |
313 int chroma_best=-1, best_chroma_size=9999999; | |
601 | 314 |
499 | 315 for(i=0; i<3; i++){ |
316 int level; | |
317 int chroma_size=0; | |
318 int size=0; | |
319 | |
320 if(i>0){// ;) | |
321 size++; | |
322 chroma_size++; | |
323 } | |
324 for(level=0; level<=MAX_LEVEL; level++){ | |
325 int run; | |
326 for(run=0; run<=MAX_RUN; run++){ | |
327 int last; | |
601 | 328 const int last_size= size + chroma_size; |
499 | 329 for(last=0; last<2; last++){ |
330 int inter_count = s->ac_stats[0][0][level][run][last] + s->ac_stats[0][1][level][run][last]; | |
331 int intra_luma_count = s->ac_stats[1][0][level][run][last]; | |
332 int intra_chroma_count= s->ac_stats[1][1][level][run][last]; | |
601 | 333 |
499 | 334 if(s->pict_type==I_TYPE){ |
774
baa66649df35
mergeing inter & intra rl_length tables (50% smaller & no meassureable difference in filesize/quality)
michaelni
parents:
773
diff
changeset
|
335 size += intra_luma_count *rl_length[i ][level][run][last]; |
baa66649df35
mergeing inter & intra rl_length tables (50% smaller & no meassureable difference in filesize/quality)
michaelni
parents:
773
diff
changeset
|
336 chroma_size+= intra_chroma_count*rl_length[i+3][level][run][last]; |
499 | 337 }else{ |
774
baa66649df35
mergeing inter & intra rl_length tables (50% smaller & no meassureable difference in filesize/quality)
michaelni
parents:
773
diff
changeset
|
338 size+= intra_luma_count *rl_length[i ][level][run][last] |
baa66649df35
mergeing inter & intra rl_length tables (50% smaller & no meassureable difference in filesize/quality)
michaelni
parents:
773
diff
changeset
|
339 +intra_chroma_count*rl_length[i+3][level][run][last] |
baa66649df35
mergeing inter & intra rl_length tables (50% smaller & no meassureable difference in filesize/quality)
michaelni
parents:
773
diff
changeset
|
340 +inter_count *rl_length[i+3][level][run][last]; |
499 | 341 } |
342 } | |
601 | 343 if(last_size == size+chroma_size) break; |
499 | 344 } |
345 } | |
346 if(size<best_size){ | |
347 best_size= size; | |
348 best= i; | |
349 } | |
350 if(chroma_size<best_chroma_size){ | |
351 best_chroma_size= chroma_size; | |
352 chroma_best= i; | |
353 } | |
354 } | |
601 | 355 |
499 | 356 // printf("type:%d, best:%d, qp:%d, var:%d, mcvar:%d, size:%d //\n", |
357 // s->pict_type, best, s->qscale, s->mb_var_sum, s->mc_mb_var_sum, best_size); | |
358 | |
359 if(s->pict_type==P_TYPE) chroma_best= best; | |
360 | |
361 memset(s->ac_stats, 0, sizeof(int)*(MAX_LEVEL+1)*(MAX_RUN+1)*2*2*2); | |
362 | |
363 s->rl_table_index = best; | |
364 s->rl_chroma_table_index= chroma_best; | |
365 | |
366 if(s->pict_type != s->last_non_b_pict_type){ | |
367 s->rl_table_index= 2; | |
368 if(s->pict_type==I_TYPE) | |
369 s->rl_chroma_table_index= 1; | |
370 else | |
371 s->rl_chroma_table_index= 2; | |
372 } | |
373 | |
374 } | |
375 | |
457 | 376 /* write MSMPEG4 compatible frame header */ |
0 | 377 void msmpeg4_encode_picture_header(MpegEncContext * s, int picture_number) |
378 { | |
499 | 379 find_best_tables(s); |
0 | 380 |
381 align_put_bits(&s->pb); | |
382 put_bits(&s->pb, 2, s->pict_type - 1); | |
383 | |
384 put_bits(&s->pb, 5, s->qscale); | |
499 | 385 if(s->msmpeg4_version<=2){ |
386 s->rl_table_index = 2; | |
387 s->rl_chroma_table_index = 2; | |
388 } | |
310 | 389 |
0 | 390 s->dc_table_index = 1; |
391 s->mv_table_index = 1; /* only if P frame */ | |
392 s->use_skip_mb_code = 1; /* only if P frame */ | |
499 | 393 s->per_mb_rl_table = 0; |
759 | 394 if(s->msmpeg4_version==4) |
395 s->inter_intra_pred= (s->width*s->height < 320*240 && s->bit_rate<=II_BITRATE && s->pict_type==P_TYPE); | |
936 | 396 //printf("%d %d %d %d %d\n", s->pict_type, s->bit_rate, s->inter_intra_pred, s->width, s->height); |
499 | 397 |
0 | 398 if (s->pict_type == I_TYPE) { |
519 | 399 s->slice_height= s->mb_height/1; |
400 put_bits(&s->pb, 5, 0x16 + s->mb_height/s->slice_height); | |
499 | 401 |
402 if(s->msmpeg4_version==4){ | |
403 msmpeg4_encode_ext_header(s); | |
578 | 404 if(s->bit_rate>MBAC_BITRATE) |
519 | 405 put_bits(&s->pb, 1, s->per_mb_rl_table); |
499 | 406 } |
0 | 407 |
457 | 408 if(s->msmpeg4_version>2){ |
499 | 409 if(!s->per_mb_rl_table){ |
410 code012(&s->pb, s->rl_chroma_table_index); | |
411 code012(&s->pb, s->rl_table_index); | |
412 } | |
0 | 413 |
310 | 414 put_bits(&s->pb, 1, s->dc_table_index); |
415 } | |
0 | 416 } else { |
417 put_bits(&s->pb, 1, s->use_skip_mb_code); | |
418 | |
578 | 419 if(s->msmpeg4_version==4 && s->bit_rate>MBAC_BITRATE) |
499 | 420 put_bits(&s->pb, 1, s->per_mb_rl_table); |
421 | |
457 | 422 if(s->msmpeg4_version>2){ |
499 | 423 if(!s->per_mb_rl_table) |
424 code012(&s->pb, s->rl_table_index); | |
0 | 425 |
310 | 426 put_bits(&s->pb, 1, s->dc_table_index); |
0 | 427 |
310 | 428 put_bits(&s->pb, 1, s->mv_table_index); |
429 } | |
0 | 430 } |
431 | |
499 | 432 s->esc3_level_length= 0; |
433 s->esc3_run_length= 0; | |
0 | 434 |
435 #ifdef DEBUG | |
436 intra_count = 0; | |
437 printf("*****frame %d:\n", frame_count++); | |
438 #endif | |
439 } | |
440 | |
208 | 441 void msmpeg4_encode_ext_header(MpegEncContext * s) |
442 { | |
2637 | 443 put_bits(&s->pb, 5, s->avctx->time_base.den / s->avctx->time_base.num); //yes 29.97 -> 29 |
208 | 444 |
847 | 445 put_bits(&s->pb, 11, FFMIN(s->bit_rate/1024, 2047)); |
208 | 446 |
1163 | 447 if(s->msmpeg4_version>=3) |
457 | 448 put_bits(&s->pb, 1, s->flipflop_rounding); |
1163 | 449 else |
450 assert(s->flipflop_rounding==0); | |
208 | 451 } |
452 | |
1070
6da5ae9ee199
more #ifdef CONFIG_ENCODERS patch by (Wolfgang Hesseler <qv at multimediaware dot com>) with modifications by me (s/WOLFGANG/CONFIG_ENCODERS/ and some other fixes)
michaelni
parents:
1064
diff
changeset
|
453 #endif //CONFIG_ENCODERS |
6da5ae9ee199
more #ifdef CONFIG_ENCODERS patch by (Wolfgang Hesseler <qv at multimediaware dot com>) with modifications by me (s/WOLFGANG/CONFIG_ENCODERS/ and some other fixes)
michaelni
parents:
1064
diff
changeset
|
454 |
0 | 455 /* predict coded block */ |
1064 | 456 static inline int coded_block_pred(MpegEncContext * s, int n, uint8_t **coded_block_ptr) |
0 | 457 { |
299 | 458 int xy, wrap, pred, a, b, c; |
0 | 459 |
299 | 460 xy = s->block_index[n]; |
1938
e2501e6e7ff7
unify table indexing (motion_val,dc_val,ac_val,coded_block changed)
michael
parents:
1739
diff
changeset
|
461 wrap = s->b8_stride; |
0 | 462 |
463 /* B C | |
464 * A X | |
465 */ | |
299 | 466 a = s->coded_block[xy - 1 ]; |
467 b = s->coded_block[xy - 1 - wrap]; | |
468 c = s->coded_block[xy - wrap]; | |
0 | 469 |
470 if (b == c) { | |
471 pred = a; | |
472 } else { | |
473 pred = c; | |
474 } | |
475 | |
476 /* store value */ | |
299 | 477 *coded_block_ptr = &s->coded_block[xy]; |
0 | 478 |
479 return pred; | |
480 } | |
481 | |
1070
6da5ae9ee199
more #ifdef CONFIG_ENCODERS patch by (Wolfgang Hesseler <qv at multimediaware dot com>) with modifications by me (s/WOLFGANG/CONFIG_ENCODERS/ and some other fixes)
michaelni
parents:
1064
diff
changeset
|
482 #ifdef CONFIG_ENCODERS |
6da5ae9ee199
more #ifdef CONFIG_ENCODERS patch by (Wolfgang Hesseler <qv at multimediaware dot com>) with modifications by me (s/WOLFGANG/CONFIG_ENCODERS/ and some other fixes)
michaelni
parents:
1064
diff
changeset
|
483 |
0 | 484 static void msmpeg4_encode_motion(MpegEncContext * s, |
485 int mx, int my) | |
486 { | |
487 int code; | |
488 MVTable *mv; | |
489 | |
490 /* modulo encoding */ | |
491 /* WARNING : you cannot reach all the MVs even with the modulo | |
492 encoding. This is a somewhat strange compromise they took !!! */ | |
493 if (mx <= -64) | |
494 mx += 64; | |
495 else if (mx >= 64) | |
496 mx -= 64; | |
497 if (my <= -64) | |
498 my += 64; | |
499 else if (my >= 64) | |
500 my -= 64; | |
501 | |
502 mx += 32; | |
503 my += 32; | |
504 #if 0 | |
505 if ((unsigned)mx >= 64 || | |
506 (unsigned)my >= 64) | |
507 fprintf(stderr, "error mx=%d my=%d\n", mx, my); | |
508 #endif | |
509 mv = &mv_tables[s->mv_table_index]; | |
510 | |
511 code = mv->table_mv_index[(mx << 6) | my]; | |
512 set_stat(ST_MV); | |
513 put_bits(&s->pb, | |
514 mv->table_mv_bits[code], | |
515 mv->table_mv_code[code]); | |
516 if (code == mv->n) { | |
517 /* escape : code litterally */ | |
518 put_bits(&s->pb, 6, mx); | |
519 put_bits(&s->pb, 6, my); | |
520 } | |
521 } | |
522 | |
519 | 523 static inline void handle_slices(MpegEncContext *s){ |
524 if (s->mb_x == 0) { | |
525 if (s->slice_height && (s->mb_y % s->slice_height) == 0) { | |
983 | 526 if(s->msmpeg4_version < 4){ |
745
25d7fb7c89be
better/cleaner error resilience (done in a 2nd pass after decoding)
michaelni
parents:
743
diff
changeset
|
527 ff_mpeg4_clean_buffers(s); |
519 | 528 } |
529 s->first_slice_line = 1; | |
530 } else { | |
531 s->first_slice_line = 0; | |
532 } | |
533 } | |
534 } | |
535 | |
0 | 536 void msmpeg4_encode_mb(MpegEncContext * s, |
537 DCTELEM block[6][64], | |
538 int motion_x, int motion_y) | |
539 { | |
540 int cbp, coded_cbp, i; | |
541 int pred_x, pred_y; | |
1064 | 542 uint8_t *coded_block; |
0 | 543 |
519 | 544 handle_slices(s); |
545 | |
0 | 546 if (!s->mb_intra) { |
547 /* compute cbp */ | |
548 set_stat(ST_INTER_MB); | |
549 cbp = 0; | |
550 for (i = 0; i < 6; i++) { | |
551 if (s->block_last_index[i] >= 0) | |
552 cbp |= 1 << (5 - i); | |
553 } | |
554 if (s->use_skip_mb_code && (cbp | motion_x | motion_y) == 0) { | |
555 /* skip macroblock */ | |
556 put_bits(&s->pb, 1, 1); | |
1164 | 557 s->last_bits++; |
558 s->misc_bits++; | |
1216 | 559 s->skip_count++; |
1164 | 560 |
0 | 561 return; |
562 } | |
563 if (s->use_skip_mb_code) | |
564 put_bits(&s->pb, 1, 0); /* mb coded */ | |
565 | |
457 | 566 if(s->msmpeg4_version<=2){ |
310 | 567 put_bits(&s->pb, |
568 v2_mb_type[cbp&3][1], | |
569 v2_mb_type[cbp&3][0]); | |
570 if((cbp&3) != 3) coded_cbp= cbp ^ 0x3C; | |
571 else coded_cbp= cbp; | |
0 | 572 |
310 | 573 put_bits(&s->pb, |
574 cbpy_tab[coded_cbp>>2][1], | |
575 cbpy_tab[coded_cbp>>2][0]); | |
1164 | 576 |
577 s->misc_bits += get_bits_diff(s); | |
578 | |
1938
e2501e6e7ff7
unify table indexing (motion_val,dc_val,ac_val,coded_block changed)
michael
parents:
1739
diff
changeset
|
579 h263_pred_motion(s, 0, 0, &pred_x, &pred_y); |
310 | 580 msmpeg4v2_encode_motion(s, motion_x - pred_x); |
581 msmpeg4v2_encode_motion(s, motion_y - pred_y); | |
582 }else{ | |
583 put_bits(&s->pb, | |
584 table_mb_non_intra[cbp + 64][1], | |
585 table_mb_non_intra[cbp + 64][0]); | |
586 | |
1164 | 587 s->misc_bits += get_bits_diff(s); |
588 | |
310 | 589 /* motion vector */ |
1938
e2501e6e7ff7
unify table indexing (motion_val,dc_val,ac_val,coded_block changed)
michael
parents:
1739
diff
changeset
|
590 h263_pred_motion(s, 0, 0, &pred_x, &pred_y); |
310 | 591 msmpeg4_encode_motion(s, motion_x - pred_x, |
592 motion_y - pred_y); | |
593 } | |
1164 | 594 |
595 s->mv_bits += get_bits_diff(s); | |
596 | |
597 for (i = 0; i < 6; i++) { | |
598 msmpeg4_encode_block(s, block[i], i); | |
599 } | |
600 s->p_tex_bits += get_bits_diff(s); | |
0 | 601 } else { |
602 /* compute cbp */ | |
603 cbp = 0; | |
604 coded_cbp = 0; | |
605 for (i = 0; i < 6; i++) { | |
606 int val, pred; | |
607 val = (s->block_last_index[i] >= 1); | |
608 cbp |= val << (5 - i); | |
609 if (i < 4) { | |
610 /* predict value for close blocks only for luma */ | |
611 pred = coded_block_pred(s, i, &coded_block); | |
612 *coded_block = val; | |
613 val = val ^ pred; | |
614 } | |
615 coded_cbp |= val << (5 - i); | |
616 } | |
617 #if 0 | |
618 if (coded_cbp) | |
619 printf("cbp=%x %x\n", cbp, coded_cbp); | |
620 #endif | |
621 | |
457 | 622 if(s->msmpeg4_version<=2){ |
310 | 623 if (s->pict_type == I_TYPE) { |
624 put_bits(&s->pb, | |
625 v2_intra_cbpc[cbp&3][1], v2_intra_cbpc[cbp&3][0]); | |
626 } else { | |
627 if (s->use_skip_mb_code) | |
628 put_bits(&s->pb, 1, 0); /* mb coded */ | |
629 put_bits(&s->pb, | |
630 v2_mb_type[(cbp&3) + 4][1], | |
631 v2_mb_type[(cbp&3) + 4][0]); | |
632 } | |
633 put_bits(&s->pb, 1, 0); /* no AC prediction yet */ | |
0 | 634 put_bits(&s->pb, |
310 | 635 cbpy_tab[cbp>>2][1], |
636 cbpy_tab[cbp>>2][0]); | |
637 }else{ | |
638 if (s->pict_type == I_TYPE) { | |
639 set_stat(ST_INTRA_MB); | |
640 put_bits(&s->pb, | |
2474 | 641 ff_msmp4_mb_i_table[coded_cbp][1], ff_msmp4_mb_i_table[coded_cbp][0]); |
310 | 642 } else { |
643 if (s->use_skip_mb_code) | |
644 put_bits(&s->pb, 1, 0); /* mb coded */ | |
645 put_bits(&s->pb, | |
646 table_mb_non_intra[cbp][1], | |
647 table_mb_non_intra[cbp][0]); | |
648 } | |
649 set_stat(ST_INTRA_MB); | |
650 put_bits(&s->pb, 1, 0); /* no AC prediction yet */ | |
578 | 651 if(s->inter_intra_pred){ |
652 s->h263_aic_dir=0; | |
653 put_bits(&s->pb, table_inter_intra[s->h263_aic_dir][1], table_inter_intra[s->h263_aic_dir][0]); | |
654 } | |
0 | 655 } |
1164 | 656 s->misc_bits += get_bits_diff(s); |
0 | 657 |
1164 | 658 for (i = 0; i < 6; i++) { |
659 msmpeg4_encode_block(s, block[i], i); | |
660 } | |
661 s->i_tex_bits += get_bits_diff(s); | |
1216 | 662 s->i_count++; |
0 | 663 } |
664 } | |
665 | |
1070
6da5ae9ee199
more #ifdef CONFIG_ENCODERS patch by (Wolfgang Hesseler <qv at multimediaware dot com>) with modifications by me (s/WOLFGANG/CONFIG_ENCODERS/ and some other fixes)
michaelni
parents:
1064
diff
changeset
|
666 #endif //CONFIG_ENCODERS |
6da5ae9ee199
more #ifdef CONFIG_ENCODERS patch by (Wolfgang Hesseler <qv at multimediaware dot com>) with modifications by me (s/WOLFGANG/CONFIG_ENCODERS/ and some other fixes)
michaelni
parents:
1064
diff
changeset
|
667 |
499 | 668 static inline int msmpeg4v1_pred_dc(MpegEncContext * s, int n, |
1064 | 669 int32_t **dc_val_ptr) |
457 | 670 { |
671 int i; | |
672 | |
673 if (n < 4) { | |
674 i= 0; | |
675 } else { | |
676 i= n-3; | |
677 } | |
678 | |
679 *dc_val_ptr= &s->last_dc[i]; | |
680 return s->last_dc[i]; | |
681 } | |
682 | |
519 | 683 static int get_dc(uint8_t *src, int stride, int scale) |
684 { | |
685 int y; | |
686 int sum=0; | |
687 for(y=0; y<8; y++){ | |
688 int x; | |
689 for(x=0; x<8; x++){ | |
690 sum+=src[x + y*stride]; | |
691 } | |
692 } | |
1261
362947395f5c
fastdiv patch by (BERO <bero at geocities dot co dot jp>) with fixes & cleanup by me
michaelni
parents:
1255
diff
changeset
|
693 return FASTDIV((sum + (scale>>1)), scale); |
519 | 694 } |
695 | |
0 | 696 /* dir = 0: left, dir = 1: top prediction */ |
499 | 697 static inline int msmpeg4_pred_dc(MpegEncContext * s, int n, |
1064 | 698 uint16_t **dc_val_ptr, int *dir_ptr) |
0 | 699 { |
299 | 700 int a, b, c, wrap, pred, scale; |
1064 | 701 int16_t *dc_val; |
0 | 702 |
703 /* find prediction */ | |
704 if (n < 4) { | |
705 scale = s->y_dc_scale; | |
706 } else { | |
707 scale = s->c_dc_scale; | |
708 } | |
457 | 709 |
299 | 710 wrap = s->block_wrap[n]; |
711 dc_val= s->dc_val[0] + s->block_index[n]; | |
0 | 712 |
713 /* B C | |
714 * A X | |
715 */ | |
299 | 716 a = dc_val[ - 1]; |
717 b = dc_val[ - 1 - wrap]; | |
718 c = dc_val[ - wrap]; | |
745
25d7fb7c89be
better/cleaner error resilience (done in a 2nd pass after decoding)
michaelni
parents:
743
diff
changeset
|
719 |
983 | 720 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
|
721 b=c=1024; |
25d7fb7c89be
better/cleaner error resilience (done in a 2nd pass after decoding)
michaelni
parents:
743
diff
changeset
|
722 } |
0 | 723 |
724 /* XXX: the following solution consumes divisions, but it does not | |
725 necessitate to modify mpegvideo.c. The problem comes from the | |
726 fact they decided to store the quantized DC (which would lead | |
727 to problems if Q could vary !) */ | |
2293
15cfba1b97b5
adapting existing mmx/mmx2/sse/3dnow optimizations so they work on x86_64 patch by (Aurelien Jacobs <aurel at gnuage dot org>)
michael
parents:
2031
diff
changeset
|
728 #if (defined(ARCH_X86) || defined(ARCH_X86_64)) && !defined PIC |
204 | 729 asm volatile( |
730 "movl %3, %%eax \n\t" | |
731 "shrl $1, %%eax \n\t" | |
732 "addl %%eax, %2 \n\t" | |
733 "addl %%eax, %1 \n\t" | |
734 "addl %0, %%eax \n\t" | |
225
ae145876789d
use multiply instead of divides for DC prediction on X86
michaelni
parents:
221
diff
changeset
|
735 "mull %4 \n\t" |
ae145876789d
use multiply instead of divides for DC prediction on X86
michaelni
parents:
221
diff
changeset
|
736 "movl %%edx, %0 \n\t" |
204 | 737 "movl %1, %%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, %1 \n\t" |
204 | 740 "movl %2, %%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, %2 \n\t" |
228 | 743 : "+b" (a), "+c" (b), "+D" (c) |
744 : "g" (scale), "S" (inverse[scale]) | |
204 | 745 : "%eax", "%edx" |
746 ); | |
221 | 747 #else |
748 /* #elif defined (ARCH_ALPHA) */ | |
214
73df666cacc7
Alpha optimizations by Falk Hueffner <falk.hueffner@student.uni-tuebingen.de>
nickols_k
parents:
208
diff
changeset
|
749 /* Divisions are extremely costly on Alpha; optimize the most |
221 | 750 common case. But they are costly everywhere... |
751 */ | |
214
73df666cacc7
Alpha optimizations by Falk Hueffner <falk.hueffner@student.uni-tuebingen.de>
nickols_k
parents:
208
diff
changeset
|
752 if (scale == 8) { |
73df666cacc7
Alpha optimizations by Falk Hueffner <falk.hueffner@student.uni-tuebingen.de>
nickols_k
parents:
208
diff
changeset
|
753 a = (a + (8 >> 1)) / 8; |
73df666cacc7
Alpha optimizations by Falk Hueffner <falk.hueffner@student.uni-tuebingen.de>
nickols_k
parents:
208
diff
changeset
|
754 b = (b + (8 >> 1)) / 8; |
73df666cacc7
Alpha optimizations by Falk Hueffner <falk.hueffner@student.uni-tuebingen.de>
nickols_k
parents:
208
diff
changeset
|
755 c = (c + (8 >> 1)) / 8; |
73df666cacc7
Alpha optimizations by Falk Hueffner <falk.hueffner@student.uni-tuebingen.de>
nickols_k
parents:
208
diff
changeset
|
756 } else { |
1261
362947395f5c
fastdiv patch by (BERO <bero at geocities dot co dot jp>) with fixes & cleanup by me
michaelni
parents:
1255
diff
changeset
|
757 a = FASTDIV((a + (scale >> 1)), scale); |
362947395f5c
fastdiv patch by (BERO <bero at geocities dot co dot jp>) with fixes & cleanup by me
michaelni
parents:
1255
diff
changeset
|
758 b = FASTDIV((b + (scale >> 1)), scale); |
362947395f5c
fastdiv patch by (BERO <bero at geocities dot co dot jp>) with fixes & cleanup by me
michaelni
parents:
1255
diff
changeset
|
759 c = FASTDIV((c + (scale >> 1)), scale); |
214
73df666cacc7
Alpha optimizations by Falk Hueffner <falk.hueffner@student.uni-tuebingen.de>
nickols_k
parents:
208
diff
changeset
|
760 } |
204 | 761 #endif |
0 | 762 /* XXX: WARNING: they did not choose the same test as MPEG4. This |
763 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
|
764 if(s->msmpeg4_version>3){ |
519 | 765 if(s->inter_intra_pred){ |
766 uint8_t *dest; | |
767 int wrap; | |
768 | |
769 if(n==1){ | |
770 pred=a; | |
771 *dir_ptr = 0; | |
772 }else if(n==2){ | |
773 pred=c; | |
774 *dir_ptr = 1; | |
775 }else if(n==3){ | |
776 if (abs(a - b) < abs(b - c)) { | |
777 pred = c; | |
778 *dir_ptr = 1; | |
779 } else { | |
780 pred = a; | |
781 *dir_ptr = 0; | |
782 } | |
783 }else{ | |
784 if(n<4){ | |
785 wrap= s->linesize; | |
903 | 786 dest= s->current_picture.data[0] + (((n>>1) + 2*s->mb_y) * 8* wrap ) + ((n&1) + 2*s->mb_x) * 8; |
519 | 787 }else{ |
556 | 788 wrap= s->uvlinesize; |
903 | 789 dest= s->current_picture.data[n-3] + (s->mb_y * 8 * wrap) + s->mb_x * 8; |
519 | 790 } |
791 if(s->mb_x==0) a= (1024 + (scale>>1))/scale; | |
792 else a= get_dc(dest-8, wrap, scale*8); | |
793 if(s->mb_y==0) c= (1024 + (scale>>1))/scale; | |
794 else c= get_dc(dest-8*wrap, wrap, scale*8); | |
795 | |
796 if (s->h263_aic_dir==0) { | |
797 pred= a; | |
798 *dir_ptr = 0; | |
799 }else if (s->h263_aic_dir==1) { | |
800 if(n==0){ | |
801 pred= c; | |
802 *dir_ptr = 1; | |
803 }else{ | |
804 pred= a; | |
805 *dir_ptr = 0; | |
806 } | |
807 }else if (s->h263_aic_dir==2) { | |
808 if(n==0){ | |
809 pred= a; | |
810 *dir_ptr = 0; | |
811 }else{ | |
812 pred= c; | |
813 *dir_ptr = 1; | |
814 } | |
815 } else { | |
816 pred= c; | |
817 *dir_ptr = 1; | |
818 } | |
819 } | |
820 }else{ | |
821 if (abs(a - b) < abs(b - c)) { | |
822 pred = c; | |
823 *dir_ptr = 1; | |
824 } else { | |
825 pred = a; | |
826 *dir_ptr = 0; | |
827 } | |
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
|
828 } |
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
|
829 }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
|
830 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
|
831 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
|
832 *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
|
833 } 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
|
834 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
|
835 *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
|
836 } |
0 | 837 } |
838 | |
839 /* update predictor */ | |
299 | 840 *dc_val_ptr = &dc_val[0]; |
0 | 841 return pred; |
842 } | |
843 | |
844 #define DC_MAX 119 | |
845 | |
846 static void msmpeg4_encode_dc(MpegEncContext * s, int level, int n, int *dir_ptr) | |
847 { | |
848 int sign, code; | |
849 int pred; | |
850 | |
457 | 851 if(s->msmpeg4_version==1){ |
1064 | 852 int32_t *dc_val; |
457 | 853 pred = msmpeg4v1_pred_dc(s, n, &dc_val); |
854 | |
855 /* update predictor */ | |
856 *dc_val= level; | |
857 }else{ | |
1064 | 858 uint16_t *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
|
859 pred = msmpeg4_pred_dc(s, n, &dc_val, dir_ptr); |
457 | 860 |
861 /* update predictor */ | |
862 if (n < 4) { | |
863 *dc_val = level * s->y_dc_scale; | |
864 } else { | |
865 *dc_val = level * s->c_dc_scale; | |
866 } | |
0 | 867 } |
868 | |
869 /* do the prediction */ | |
870 level -= pred; | |
871 | |
457 | 872 if(s->msmpeg4_version<=2){ |
310 | 873 if (n < 4) { |
874 put_bits(&s->pb, | |
875 v2_dc_lum_table[level+256][1], | |
876 v2_dc_lum_table[level+256][0]); | |
877 }else{ | |
878 put_bits(&s->pb, | |
879 v2_dc_chroma_table[level+256][1], | |
880 v2_dc_chroma_table[level+256][0]); | |
881 } | |
882 }else{ | |
883 sign = 0; | |
884 if (level < 0) { | |
885 level = -level; | |
886 sign = 1; | |
887 } | |
888 code = level; | |
889 if (code > DC_MAX) | |
890 code = DC_MAX; | |
0 | 891 |
310 | 892 if (s->dc_table_index == 0) { |
893 if (n < 4) { | |
2465
5565203c95ee
use dc tables from msmpeg4 instead of duplicating them patch by anonymous
michael
parents:
2464
diff
changeset
|
894 put_bits(&s->pb, ff_table0_dc_lum[code][1], ff_table0_dc_lum[code][0]); |
310 | 895 } else { |
2465
5565203c95ee
use dc tables from msmpeg4 instead of duplicating them patch by anonymous
michael
parents:
2464
diff
changeset
|
896 put_bits(&s->pb, ff_table0_dc_chroma[code][1], ff_table0_dc_chroma[code][0]); |
310 | 897 } |
0 | 898 } else { |
310 | 899 if (n < 4) { |
2465
5565203c95ee
use dc tables from msmpeg4 instead of duplicating them patch by anonymous
michael
parents:
2464
diff
changeset
|
900 put_bits(&s->pb, ff_table1_dc_lum[code][1], ff_table1_dc_lum[code][0]); |
310 | 901 } else { |
2465
5565203c95ee
use dc tables from msmpeg4 instead of duplicating them patch by anonymous
michael
parents:
2464
diff
changeset
|
902 put_bits(&s->pb, ff_table1_dc_chroma[code][1], ff_table1_dc_chroma[code][0]); |
310 | 903 } |
0 | 904 } |
310 | 905 |
906 if (code == DC_MAX) | |
907 put_bits(&s->pb, 8, level); | |
908 | |
909 if (level != 0) { | |
910 put_bits(&s->pb, 1, sign); | |
911 } | |
0 | 912 } |
913 } | |
914 | |
915 /* Encoding of a block. Very similar to MPEG4 except for a different | |
916 escape coding (same as H263) and more vlc tables. | |
917 */ | |
499 | 918 static inline void msmpeg4_encode_block(MpegEncContext * s, DCTELEM * block, int n) |
0 | 919 { |
920 int level, run, last, i, j, last_index; | |
921 int last_non_zero, sign, slevel; | |
922 int code, run_diff, dc_pred_dir; | |
923 const RLTable *rl; | |
1064 | 924 const uint8_t *scantable; |
0 | 925 |
926 if (s->mb_intra) { | |
927 set_stat(ST_DC); | |
928 msmpeg4_encode_dc(s, block[0], n, &dc_pred_dir); | |
929 i = 1; | |
930 if (n < 4) { | |
931 rl = &rl_table[s->rl_table_index]; | |
932 } else { | |
933 rl = &rl_table[3 + s->rl_chroma_table_index]; | |
934 } | |
935 run_diff = 0; | |
706
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
601
diff
changeset
|
936 scantable= s->intra_scantable.permutated; |
0 | 937 set_stat(ST_INTRA_AC); |
938 } else { | |
939 i = 0; | |
940 rl = &rl_table[3 + s->rl_table_index]; | |
457 | 941 if(s->msmpeg4_version<=2) |
310 | 942 run_diff = 0; |
943 else | |
944 run_diff = 1; | |
706
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
601
diff
changeset
|
945 scantable= s->inter_scantable.permutated; |
0 | 946 set_stat(ST_INTER_AC); |
947 } | |
948 | |
499 | 949 /* recalculate block_last_index for M$ wmv1 */ |
936 | 950 if(s->msmpeg4_version>=4 && s->block_last_index[n]>0){ |
499 | 951 for(last_index=63; last_index>=0; last_index--){ |
952 if(block[scantable[last_index]]) break; | |
953 } | |
745
25d7fb7c89be
better/cleaner error resilience (done in a 2nd pass after decoding)
michaelni
parents:
743
diff
changeset
|
954 s->block_last_index[n]= last_index; |
499 | 955 }else |
956 last_index = s->block_last_index[n]; | |
0 | 957 /* AC coefs */ |
958 last_non_zero = i - 1; | |
959 for (; i <= last_index; i++) { | |
499 | 960 j = scantable[i]; |
0 | 961 level = block[j]; |
962 if (level) { | |
963 run = i - last_non_zero - 1; | |
964 last = (i == last_index); | |
965 sign = 0; | |
966 slevel = level; | |
967 if (level < 0) { | |
968 sign = 1; | |
969 level = -level; | |
970 } | |
601 | 971 |
499 | 972 if(level<=MAX_LEVEL && run<=MAX_RUN){ |
973 s->ac_stats[s->mb_intra][n>3][level][run][last]++; | |
974 } | |
975 #if 0 | |
976 else | |
977 s->ac_stats[s->mb_intra][n>3][40][63][0]++; //esc3 like | |
978 #endif | |
0 | 979 code = get_rl_index(rl, last, run, level); |
980 put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]); | |
981 if (code == rl->n) { | |
982 int level1, run1; | |
983 | |
984 level1 = level - rl->max_level[last][run]; | |
985 if (level1 < 1) | |
986 goto esc2; | |
987 code = get_rl_index(rl, last, run, level1); | |
988 if (code == rl->n) { | |
989 esc2: | |
990 put_bits(&s->pb, 1, 0); | |
991 if (level > MAX_LEVEL) | |
992 goto esc3; | |
993 run1 = run - rl->max_run[last][level] - run_diff; | |
994 if (run1 < 0) | |
995 goto esc3; | |
996 code = get_rl_index(rl, last, run1, level); | |
997 if (code == rl->n) { | |
998 esc3: | |
999 /* third escape */ | |
1000 put_bits(&s->pb, 1, 0); | |
1001 put_bits(&s->pb, 1, last); | |
936 | 1002 if(s->msmpeg4_version>=4){ |
499 | 1003 if(s->esc3_level_length==0){ |
1004 s->esc3_level_length=8; | |
1005 s->esc3_run_length= 6; | |
1006 if(s->qscale<8) | |
1007 put_bits(&s->pb, 6, 3); | |
1008 else | |
1009 put_bits(&s->pb, 8, 3); | |
1010 } | |
1011 put_bits(&s->pb, s->esc3_run_length, run); | |
1012 put_bits(&s->pb, 1, sign); | |
1013 put_bits(&s->pb, s->esc3_level_length, level); | |
1014 }else{ | |
1015 put_bits(&s->pb, 6, run); | |
1016 put_bits(&s->pb, 8, slevel & 0xff); | |
1017 } | |
0 | 1018 } else { |
1019 /* second escape */ | |
1020 put_bits(&s->pb, 1, 1); | |
1021 put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]); | |
1022 put_bits(&s->pb, 1, sign); | |
1023 } | |
1024 } else { | |
1025 /* first escape */ | |
1026 put_bits(&s->pb, 1, 1); | |
1027 put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]); | |
1028 put_bits(&s->pb, 1, sign); | |
1029 } | |
1030 } else { | |
1031 put_bits(&s->pb, 1, sign); | |
1032 } | |
1033 last_non_zero = i; | |
1034 } | |
1035 } | |
1036 } | |
1037 | |
1038 /****************************************/ | |
1039 /* decoding stuff */ | |
1040 | |
936 | 1041 static VLC mb_non_intra_vlc[4]; |
2474 | 1042 VLC ff_msmp4_mb_i_vlc; |
1043 VLC ff_msmp4_dc_luma_vlc[2]; | |
1044 VLC ff_msmp4_dc_chroma_vlc[2]; | |
307 | 1045 static VLC v2_dc_lum_vlc; |
1046 static VLC v2_dc_chroma_vlc; | |
1047 static VLC cbpy_vlc; | |
1048 static VLC v2_intra_cbpc_vlc; | |
1049 static VLC v2_mb_type_vlc; | |
1050 static VLC v2_mv_vlc; | |
457 | 1051 static VLC v1_intra_cbpc_vlc; |
1052 static VLC v1_inter_cbpc_vlc; | |
519 | 1053 static VLC inter_intra_vlc; |
307 | 1054 |
1055 /* this table is practically identical to the one from h263 except that its inverted */ | |
468 | 1056 static void init_h263_dc_for_msmpeg4(void) |
307 | 1057 { |
1058 int level, uni_code, uni_len; | |
1059 | |
309 | 1060 for(level=-256; level<256; level++){ |
307 | 1061 int size, v, l; |
1062 /* find number of bits */ | |
1063 size = 0; | |
1064 v = abs(level); | |
1065 while (v) { | |
1066 v >>= 1; | |
1067 size++; | |
1068 } | |
1069 | |
1070 if (level < 0) | |
1071 l= (-level) ^ ((1 << size) - 1); | |
1072 else | |
1073 l= level; | |
1074 | |
1075 /* luminance h263 */ | |
1076 uni_code= DCtab_lum[size][0]; | |
1077 uni_len = DCtab_lum[size][1]; | |
1078 uni_code ^= (1<<uni_len)-1; //M$ doesnt like compatibility | |
1079 | |
1080 if (size > 0) { | |
1081 uni_code<<=size; uni_code|=l; | |
1082 uni_len+=size; | |
1083 if (size > 8){ | |
1084 uni_code<<=1; uni_code|=1; | |
1085 uni_len++; | |
1086 } | |
1087 } | |
1088 v2_dc_lum_table[level+256][0]= uni_code; | |
1089 v2_dc_lum_table[level+256][1]= uni_len; | |
1090 | |
1091 /* chrominance h263 */ | |
1092 uni_code= DCtab_chrom[size][0]; | |
1093 uni_len = DCtab_chrom[size][1]; | |
1094 uni_code ^= (1<<uni_len)-1; //M$ doesnt like compatibility | |
1095 | |
1096 if (size > 0) { | |
1097 uni_code<<=size; uni_code|=l; | |
1098 uni_len+=size; | |
1099 if (size > 8){ | |
1100 uni_code<<=1; uni_code|=1; | |
1101 uni_len++; | |
1102 } | |
1103 } | |
1104 v2_dc_chroma_table[level+256][0]= uni_code; | |
1105 v2_dc_chroma_table[level+256][1]= uni_len; | |
1106 | |
1107 } | |
1108 } | |
0 | 1109 |
1110 /* init all vlc decoding tables */ | |
499 | 1111 int ff_msmpeg4_decode_init(MpegEncContext *s) |
0 | 1112 { |
483 | 1113 static int done = 0; |
0 | 1114 int i; |
1115 MVTable *mv; | |
1116 | |
499 | 1117 common_init(s); |
483 | 1118 |
1119 if (!done) { | |
1120 done = 1; | |
0 | 1121 |
483 | 1122 for(i=0;i<NB_RL_TABLES;i++) { |
2370
26560d4fdb1f
Memory leak fix patch by (Burkhard Plaum <plaum >at< ipf.uni-stuttgart )dot( de>)
michael
parents:
2293
diff
changeset
|
1123 init_rl(&rl_table[i], 1); |
26560d4fdb1f
Memory leak fix patch by (Burkhard Plaum <plaum >at< ipf.uni-stuttgart )dot( de>)
michael
parents:
2293
diff
changeset
|
1124 init_vlc_rl(&rl_table[i], 1); |
483 | 1125 } |
1126 for(i=0;i<2;i++) { | |
1127 mv = &mv_tables[i]; | |
531 | 1128 init_vlc(&mv->vlc, MV_VLC_BITS, mv->n + 1, |
483 | 1129 mv->table_mv_bits, 1, 1, |
2370
26560d4fdb1f
Memory leak fix patch by (Burkhard Plaum <plaum >at< ipf.uni-stuttgart )dot( de>)
michael
parents:
2293
diff
changeset
|
1130 mv->table_mv_code, 2, 2, 1); |
483 | 1131 } |
1132 | |
2474 | 1133 init_vlc(&ff_msmp4_dc_luma_vlc[0], DC_VLC_BITS, 120, |
2465
5565203c95ee
use dc tables from msmpeg4 instead of duplicating them patch by anonymous
michael
parents:
2464
diff
changeset
|
1134 &ff_table0_dc_lum[0][1], 8, 4, |
5565203c95ee
use dc tables from msmpeg4 instead of duplicating them patch by anonymous
michael
parents:
2464
diff
changeset
|
1135 &ff_table0_dc_lum[0][0], 8, 4, 1); |
2474 | 1136 init_vlc(&ff_msmp4_dc_chroma_vlc[0], DC_VLC_BITS, 120, |
2465
5565203c95ee
use dc tables from msmpeg4 instead of duplicating them patch by anonymous
michael
parents:
2464
diff
changeset
|
1137 &ff_table0_dc_chroma[0][1], 8, 4, |
5565203c95ee
use dc tables from msmpeg4 instead of duplicating them patch by anonymous
michael
parents:
2464
diff
changeset
|
1138 &ff_table0_dc_chroma[0][0], 8, 4, 1); |
2474 | 1139 init_vlc(&ff_msmp4_dc_luma_vlc[1], DC_VLC_BITS, 120, |
2465
5565203c95ee
use dc tables from msmpeg4 instead of duplicating them patch by anonymous
michael
parents:
2464
diff
changeset
|
1140 &ff_table1_dc_lum[0][1], 8, 4, |
5565203c95ee
use dc tables from msmpeg4 instead of duplicating them patch by anonymous
michael
parents:
2464
diff
changeset
|
1141 &ff_table1_dc_lum[0][0], 8, 4, 1); |
2474 | 1142 init_vlc(&ff_msmp4_dc_chroma_vlc[1], DC_VLC_BITS, 120, |
2465
5565203c95ee
use dc tables from msmpeg4 instead of duplicating them patch by anonymous
michael
parents:
2464
diff
changeset
|
1143 &ff_table1_dc_chroma[0][1], 8, 4, |
5565203c95ee
use dc tables from msmpeg4 instead of duplicating them patch by anonymous
michael
parents:
2464
diff
changeset
|
1144 &ff_table1_dc_chroma[0][0], 8, 4, 1); |
307 | 1145 |
531 | 1146 init_vlc(&v2_dc_lum_vlc, DC_VLC_BITS, 512, |
483 | 1147 &v2_dc_lum_table[0][1], 8, 4, |
2370
26560d4fdb1f
Memory leak fix patch by (Burkhard Plaum <plaum >at< ipf.uni-stuttgart )dot( de>)
michael
parents:
2293
diff
changeset
|
1148 &v2_dc_lum_table[0][0], 8, 4, 1); |
531 | 1149 init_vlc(&v2_dc_chroma_vlc, DC_VLC_BITS, 512, |
483 | 1150 &v2_dc_chroma_table[0][1], 8, 4, |
2370
26560d4fdb1f
Memory leak fix patch by (Burkhard Plaum <plaum >at< ipf.uni-stuttgart )dot( de>)
michael
parents:
2293
diff
changeset
|
1151 &v2_dc_chroma_table[0][0], 8, 4, 1); |
307 | 1152 |
531 | 1153 init_vlc(&cbpy_vlc, CBPY_VLC_BITS, 16, |
483 | 1154 &cbpy_tab[0][1], 2, 1, |
2370
26560d4fdb1f
Memory leak fix patch by (Burkhard Plaum <plaum >at< ipf.uni-stuttgart )dot( de>)
michael
parents:
2293
diff
changeset
|
1155 &cbpy_tab[0][0], 2, 1, 1); |
531 | 1156 init_vlc(&v2_intra_cbpc_vlc, V2_INTRA_CBPC_VLC_BITS, 4, |
483 | 1157 &v2_intra_cbpc[0][1], 2, 1, |
2370
26560d4fdb1f
Memory leak fix patch by (Burkhard Plaum <plaum >at< ipf.uni-stuttgart )dot( de>)
michael
parents:
2293
diff
changeset
|
1158 &v2_intra_cbpc[0][0], 2, 1, 1); |
531 | 1159 init_vlc(&v2_mb_type_vlc, V2_MB_TYPE_VLC_BITS, 8, |
483 | 1160 &v2_mb_type[0][1], 2, 1, |
2370
26560d4fdb1f
Memory leak fix patch by (Burkhard Plaum <plaum >at< ipf.uni-stuttgart )dot( de>)
michael
parents:
2293
diff
changeset
|
1161 &v2_mb_type[0][0], 2, 1, 1); |
531 | 1162 init_vlc(&v2_mv_vlc, V2_MV_VLC_BITS, 33, |
483 | 1163 &mvtab[0][1], 2, 1, |
2370
26560d4fdb1f
Memory leak fix patch by (Burkhard Plaum <plaum >at< ipf.uni-stuttgart )dot( de>)
michael
parents:
2293
diff
changeset
|
1164 &mvtab[0][0], 2, 1, 1); |
0 | 1165 |
936 | 1166 for(i=0; i<4; i++){ |
1167 init_vlc(&mb_non_intra_vlc[i], MB_NON_INTRA_VLC_BITS, 128, | |
1168 &wmv2_inter_table[i][0][1], 8, 4, | |
2370
26560d4fdb1f
Memory leak fix patch by (Burkhard Plaum <plaum >at< ipf.uni-stuttgart )dot( de>)
michael
parents:
2293
diff
changeset
|
1169 &wmv2_inter_table[i][0][0], 8, 4, 1); //FIXME name? |
936 | 1170 } |
1171 | |
2474 | 1172 init_vlc(&ff_msmp4_mb_i_vlc, MB_INTRA_VLC_BITS, 64, |
1173 &ff_msmp4_mb_i_table[0][1], 4, 2, | |
1174 &ff_msmp4_mb_i_table[0][0], 4, 2, 1); | |
457 | 1175 |
531 | 1176 init_vlc(&v1_intra_cbpc_vlc, V1_INTRA_CBPC_VLC_BITS, 8, |
483 | 1177 intra_MCBPC_bits, 1, 1, |
2370
26560d4fdb1f
Memory leak fix patch by (Burkhard Plaum <plaum >at< ipf.uni-stuttgart )dot( de>)
michael
parents:
2293
diff
changeset
|
1178 intra_MCBPC_code, 1, 1, 1); |
531 | 1179 init_vlc(&v1_inter_cbpc_vlc, V1_INTER_CBPC_VLC_BITS, 25, |
483 | 1180 inter_MCBPC_bits, 1, 1, |
2370
26560d4fdb1f
Memory leak fix patch by (Burkhard Plaum <plaum >at< ipf.uni-stuttgart )dot( de>)
michael
parents:
2293
diff
changeset
|
1181 inter_MCBPC_code, 1, 1, 1); |
519 | 1182 |
531 | 1183 init_vlc(&inter_intra_vlc, INTER_INTRA_VLC_BITS, 4, |
519 | 1184 &table_inter_intra[0][1], 2, 1, |
2370
26560d4fdb1f
Memory leak fix patch by (Burkhard Plaum <plaum >at< ipf.uni-stuttgart )dot( de>)
michael
parents:
2293
diff
changeset
|
1185 &table_inter_intra[0][0], 2, 1, 1); |
483 | 1186 } |
745
25d7fb7c89be
better/cleaner error resilience (done in a 2nd pass after decoding)
michaelni
parents:
743
diff
changeset
|
1187 |
25d7fb7c89be
better/cleaner error resilience (done in a 2nd pass after decoding)
michaelni
parents:
743
diff
changeset
|
1188 switch(s->msmpeg4_version){ |
25d7fb7c89be
better/cleaner error resilience (done in a 2nd pass after decoding)
michaelni
parents:
743
diff
changeset
|
1189 case 1: |
25d7fb7c89be
better/cleaner error resilience (done in a 2nd pass after decoding)
michaelni
parents:
743
diff
changeset
|
1190 case 2: |
25d7fb7c89be
better/cleaner error resilience (done in a 2nd pass after decoding)
michaelni
parents:
743
diff
changeset
|
1191 s->decode_mb= msmpeg4v12_decode_mb; |
25d7fb7c89be
better/cleaner error resilience (done in a 2nd pass after decoding)
michaelni
parents:
743
diff
changeset
|
1192 break; |
25d7fb7c89be
better/cleaner error resilience (done in a 2nd pass after decoding)
michaelni
parents:
743
diff
changeset
|
1193 case 3: |
25d7fb7c89be
better/cleaner error resilience (done in a 2nd pass after decoding)
michaelni
parents:
743
diff
changeset
|
1194 case 4: |
25d7fb7c89be
better/cleaner error resilience (done in a 2nd pass after decoding)
michaelni
parents:
743
diff
changeset
|
1195 s->decode_mb= msmpeg4v34_decode_mb; |
25d7fb7c89be
better/cleaner error resilience (done in a 2nd pass after decoding)
michaelni
parents:
743
diff
changeset
|
1196 break; |
936 | 1197 case 5: |
1198 s->decode_mb= wmv2_decode_mb; | |
2474 | 1199 case 6: |
1200 //FIXME + TODO VC9 decode mb | |
936 | 1201 break; |
745
25d7fb7c89be
better/cleaner error resilience (done in a 2nd pass after decoding)
michaelni
parents:
743
diff
changeset
|
1202 } |
25d7fb7c89be
better/cleaner error resilience (done in a 2nd pass after decoding)
michaelni
parents:
743
diff
changeset
|
1203 |
769
9f8c4905928b
fixing decoding of streams with no keyframe at the start
michaelni
parents:
759
diff
changeset
|
1204 s->slice_height= s->mb_height; //to avoid 1/0 if the first frame isnt a keyframe |
9f8c4905928b
fixing decoding of streams with no keyframe at the start
michaelni
parents:
759
diff
changeset
|
1205 |
0 | 1206 return 0; |
1207 } | |
1208 | |
307 | 1209 int msmpeg4_decode_picture_header(MpegEncContext * s) |
300
d874359e58f1
msmpeg4v2 header parser & some dump bits code behind #if 0
michaelni
parents:
299
diff
changeset
|
1210 { |
499 | 1211 int code; |
300
d874359e58f1
msmpeg4v2 header parser & some dump bits code behind #if 0
michaelni
parents:
299
diff
changeset
|
1212 |
311 | 1213 #if 0 |
1214 { | |
1215 int i; | |
1025
1f9afd8b9131
GetBitContext.size is allways multiplied by 8 -> use size_in_bits to avoid useless *8 in a few inner loops
michaelni
parents:
983
diff
changeset
|
1216 for(i=0; i<s->gb.size_in_bits; i++) |
311 | 1217 printf("%d", get_bits1(&s->gb)); |
1218 // get_bits1(&s->gb); | |
1219 printf("END\n"); | |
1220 return -1; | |
1221 } | |
1222 #endif | |
457 | 1223 |
1224 if(s->msmpeg4_version==1){ | |
1225 int start_code, num; | |
1226 start_code = (get_bits(&s->gb, 16)<<16) | get_bits(&s->gb, 16); | |
1227 if(start_code!=0x00000100){ | |
1598
932d306bf1dc
av_log() patch by (Michel Bardiaux <mbardiaux at peaktime dot be>)
michael
parents:
1399
diff
changeset
|
1228 av_log(s->avctx, AV_LOG_ERROR, "invalid startcode\n"); |
457 | 1229 return -1; |
1230 } | |
1231 | |
1232 num= get_bits(&s->gb, 5); // frame number */ | |
1233 } | |
1234 | |
300
d874359e58f1
msmpeg4v2 header parser & some dump bits code behind #if 0
michaelni
parents:
299
diff
changeset
|
1235 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
|
1236 if (s->pict_type != I_TYPE && |
457 | 1237 s->pict_type != P_TYPE){ |
1598
932d306bf1dc
av_log() patch by (Michel Bardiaux <mbardiaux at peaktime dot be>)
michael
parents:
1399
diff
changeset
|
1238 av_log(s->avctx, AV_LOG_ERROR, "invalid picture type\n"); |
300
d874359e58f1
msmpeg4v2 header parser & some dump bits code behind #if 0
michaelni
parents:
299
diff
changeset
|
1239 return -1; |
457 | 1240 } |
519 | 1241 #if 0 |
1242 { | |
1243 static int had_i=0; | |
1244 if(s->pict_type == I_TYPE) had_i=1; | |
1245 if(!had_i) return -1; | |
1246 } | |
1247 #endif | |
1651 | 1248 s->chroma_qscale= s->qscale = get_bits(&s->gb, 5); |
1063
fdeac9642346
check for qscale==0 (fixes 1/0 on one corrupted stream)
michaelni
parents:
1057
diff
changeset
|
1249 if(s->qscale==0){ |
1598
932d306bf1dc
av_log() patch by (Michel Bardiaux <mbardiaux at peaktime dot be>)
michael
parents:
1399
diff
changeset
|
1250 av_log(s->avctx, AV_LOG_ERROR, "invalid qscale\n"); |
1063
fdeac9642346
check for qscale==0 (fixes 1/0 on one corrupted stream)
michaelni
parents:
1057
diff
changeset
|
1251 return -1; |
fdeac9642346
check for qscale==0 (fixes 1/0 on one corrupted stream)
michaelni
parents:
1057
diff
changeset
|
1252 } |
300
d874359e58f1
msmpeg4v2 header parser & some dump bits code behind #if 0
michaelni
parents:
299
diff
changeset
|
1253 |
d874359e58f1
msmpeg4v2 header parser & some dump bits code behind #if 0
michaelni
parents:
299
diff
changeset
|
1254 if (s->pict_type == I_TYPE) { |
d874359e58f1
msmpeg4v2 header parser & some dump bits code behind #if 0
michaelni
parents:
299
diff
changeset
|
1255 code = get_bits(&s->gb, 5); |
457 | 1256 if(s->msmpeg4_version==1){ |
1257 if(code==0 || code>s->mb_height){ | |
1598
932d306bf1dc
av_log() patch by (Michel Bardiaux <mbardiaux at peaktime dot be>)
michael
parents:
1399
diff
changeset
|
1258 av_log(s->avctx, AV_LOG_ERROR, "invalid slice height %d\n", code); |
457 | 1259 return -1; |
1260 } | |
1261 | |
1262 s->slice_height = code; | |
1263 }else{ | |
1264 /* 0x17: one slice, 0x18: two slices, ... */ | |
519 | 1265 if (code < 0x17){ |
1598
932d306bf1dc
av_log() patch by (Michel Bardiaux <mbardiaux at peaktime dot be>)
michael
parents:
1399
diff
changeset
|
1266 av_log(s->avctx, AV_LOG_ERROR, "error, slice code was %X\n", code); |
457 | 1267 return -1; |
519 | 1268 } |
457 | 1269 |
1270 s->slice_height = s->mb_height / (code - 0x16); | |
1271 } | |
311 | 1272 |
1273 switch(s->msmpeg4_version){ | |
457 | 1274 case 1: |
311 | 1275 case 2: |
307 | 1276 s->rl_chroma_table_index = 2; |
1277 s->rl_table_index = 2; | |
300
d874359e58f1
msmpeg4v2 header parser & some dump bits code behind #if 0
michaelni
parents:
299
diff
changeset
|
1278 |
307 | 1279 s->dc_table_index = 0; //not used |
311 | 1280 break; |
1281 case 3: | |
307 | 1282 s->rl_chroma_table_index = decode012(&s->gb); |
1283 s->rl_table_index = decode012(&s->gb); | |
0 | 1284 |
307 | 1285 s->dc_table_index = get_bits1(&s->gb); |
311 | 1286 break; |
1287 case 4: | |
499 | 1288 msmpeg4_decode_ext_header(s, (2+5+5+17+7)/8); |
1289 | |
578 | 1290 if(s->bit_rate > MBAC_BITRATE) s->per_mb_rl_table= get_bits1(&s->gb); |
1291 else s->per_mb_rl_table= 0; | |
519 | 1292 |
499 | 1293 if(!s->per_mb_rl_table){ |
1294 s->rl_chroma_table_index = decode012(&s->gb); | |
1295 s->rl_table_index = decode012(&s->gb); | |
311 | 1296 } |
1297 | |
499 | 1298 s->dc_table_index = get_bits1(&s->gb); |
519 | 1299 s->inter_intra_pred= 0; |
311 | 1300 break; |
307 | 1301 } |
0 | 1302 s->no_rounding = 1; |
1388 | 1303 if(s->avctx->debug&FF_DEBUG_PICT_INFO) |
1598
932d306bf1dc
av_log() patch by (Michel Bardiaux <mbardiaux at peaktime dot be>)
michael
parents:
1399
diff
changeset
|
1304 av_log(s->avctx, AV_LOG_DEBUG, "qscale:%d rlc:%d rl:%d dc:%d mbrl:%d slice:%d \n", |
200 | 1305 s->qscale, |
1306 s->rl_chroma_table_index, | |
1307 s->rl_table_index, | |
499 | 1308 s->dc_table_index, |
519 | 1309 s->per_mb_rl_table, |
1388 | 1310 s->slice_height); |
0 | 1311 } else { |
457 | 1312 switch(s->msmpeg4_version){ |
1313 case 1: | |
1314 case 2: | |
1315 if(s->msmpeg4_version==1) | |
1316 s->use_skip_mb_code = 1; | |
1317 else | |
1318 s->use_skip_mb_code = get_bits1(&s->gb); | |
307 | 1319 s->rl_table_index = 2; |
1320 s->rl_chroma_table_index = s->rl_table_index; | |
1321 s->dc_table_index = 0; //not used | |
1322 s->mv_table_index = 0; | |
457 | 1323 break; |
1324 case 3: | |
1325 s->use_skip_mb_code = get_bits1(&s->gb); | |
307 | 1326 s->rl_table_index = decode012(&s->gb); |
1327 s->rl_chroma_table_index = s->rl_table_index; | |
0 | 1328 |
307 | 1329 s->dc_table_index = get_bits1(&s->gb); |
1330 | |
1331 s->mv_table_index = get_bits1(&s->gb); | |
457 | 1332 break; |
499 | 1333 case 4: |
1334 s->use_skip_mb_code = get_bits1(&s->gb); | |
519 | 1335 |
578 | 1336 if(s->bit_rate > MBAC_BITRATE) s->per_mb_rl_table= get_bits1(&s->gb); |
1337 else s->per_mb_rl_table= 0; | |
519 | 1338 |
499 | 1339 if(!s->per_mb_rl_table){ |
1340 s->rl_table_index = decode012(&s->gb); | |
1341 s->rl_chroma_table_index = s->rl_table_index; | |
1342 } | |
1343 | |
1344 s->dc_table_index = get_bits1(&s->gb); | |
1345 | |
1346 s->mv_table_index = get_bits1(&s->gb); | |
578 | 1347 s->inter_intra_pred= (s->width*s->height < 320*240 && s->bit_rate<=II_BITRATE); |
499 | 1348 break; |
307 | 1349 } |
1388 | 1350 |
1351 if(s->avctx->debug&FF_DEBUG_PICT_INFO) | |
1598
932d306bf1dc
av_log() patch by (Michel Bardiaux <mbardiaux at peaktime dot be>)
michael
parents:
1399
diff
changeset
|
1352 av_log(s->avctx, AV_LOG_DEBUG, "skip:%d rl:%d rlc:%d dc:%d mv:%d mbrl:%d qp:%d \n", |
200 | 1353 s->use_skip_mb_code, |
1354 s->rl_table_index, | |
1355 s->rl_chroma_table_index, | |
1356 s->dc_table_index, | |
499 | 1357 s->mv_table_index, |
519 | 1358 s->per_mb_rl_table, |
1388 | 1359 s->qscale); |
1360 | |
208 | 1361 if(s->flipflop_rounding){ |
1362 s->no_rounding ^= 1; | |
1363 }else{ | |
1364 s->no_rounding = 0; | |
1365 } | |
0 | 1366 } |
936 | 1367 //printf("%d %d %d %d %d\n", s->pict_type, s->bit_rate, s->inter_intra_pred, s->width, s->height); |
499 | 1368 |
1369 s->esc3_level_length= 0; | |
1370 s->esc3_run_length= 0; | |
307 | 1371 |
0 | 1372 #ifdef DEBUG |
1373 printf("*****frame %d:\n", frame_count++); | |
1374 #endif | |
1375 return 0; | |
1376 } | |
1377 | |
208 | 1378 int msmpeg4_decode_ext_header(MpegEncContext * s, int buf_size) |
1379 { | |
457 | 1380 int left= buf_size*8 - get_bits_count(&s->gb); |
1381 int length= s->msmpeg4_version>=3 ? 17 : 16; | |
208 | 1382 /* the alt_bitstream reader could read over the end so we need to check it */ |
457 | 1383 if(left>=length && left<length+8) |
208 | 1384 { |
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
|
1385 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
|
1386 |
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
|
1387 fps= get_bits(&s->gb, 5); |
578 | 1388 s->bit_rate= get_bits(&s->gb, 11)*1024; |
457 | 1389 if(s->msmpeg4_version>=3) |
1390 s->flipflop_rounding= get_bits1(&s->gb); | |
1391 else | |
1392 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
|
1393 |
578 | 1394 // printf("fps:%2d bps:%2d roundingType:%1d\n", fps, s->bit_rate/1024, s->flipflop_rounding); |
457 | 1395 } |
1396 else if(left<length+8) | |
1397 { | |
1398 s->flipflop_rounding= 0; | |
1399 | 1399 if(s->msmpeg4_version != 2) |
1598
932d306bf1dc
av_log() patch by (Michel Bardiaux <mbardiaux at peaktime dot be>)
michael
parents:
1399
diff
changeset
|
1400 av_log(s->avctx, AV_LOG_ERROR, "ext header missing, %d left\n", left); |
208 | 1401 } |
1402 else | |
1403 { | |
1598
932d306bf1dc
av_log() patch by (Michel Bardiaux <mbardiaux at peaktime dot be>)
michael
parents:
1399
diff
changeset
|
1404 av_log(s->avctx, AV_LOG_ERROR, "I frame too long, ignoring ext header\n"); |
208 | 1405 } |
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
|
1406 |
208 | 1407 return 0; |
1408 } | |
1409 | |
440
000aeeac27a2
* started to cleanup name clashes for onetime compilation
kabi
parents:
429
diff
changeset
|
1410 static inline void msmpeg4_memsetw(short *tab, int val, int n) |
0 | 1411 { |
1412 int i; | |
1413 for(i=0;i<n;i++) | |
1414 tab[i] = val; | |
1415 } | |
1416 | |
310 | 1417 static void msmpeg4v2_encode_motion(MpegEncContext * s, int val) |
1418 { | |
1419 int range, bit_size, sign, code, bits; | |
1420 | |
1421 if (val == 0) { | |
1422 /* zero vector */ | |
1423 code = 0; | |
1424 put_bits(&s->pb, mvtab[code][1], mvtab[code][0]); | |
1425 } else { | |
1426 bit_size = s->f_code - 1; | |
1427 range = 1 << bit_size; | |
1428 if (val <= -64) | |
1429 val += 64; | |
1430 else if (val >= 64) | |
1431 val -= 64; | |
1432 | |
1433 if (val >= 0) { | |
1434 sign = 0; | |
1435 } else { | |
1436 val = -val; | |
1437 sign = 1; | |
1438 } | |
1439 val--; | |
1440 code = (val >> bit_size) + 1; | |
1441 bits = val & (range - 1); | |
1442 | |
1443 put_bits(&s->pb, mvtab[code][1] + 1, (mvtab[code][0] << 1) | sign); | |
1444 if (bit_size > 0) { | |
1445 put_bits(&s->pb, bit_size, bits); | |
1446 } | |
1447 } | |
1448 } | |
1449 | |
307 | 1450 /* this is identical to h263 except that its range is multiplied by 2 */ |
1451 static int msmpeg4v2_decode_motion(MpegEncContext * s, int pred, int f_code) | |
1452 { | |
1453 int code, val, sign, shift; | |
1454 | |
531 | 1455 code = get_vlc2(&s->gb, v2_mv_vlc.table, V2_MV_VLC_BITS, 2); |
457 | 1456 // printf("MV code %d at %d %d pred: %d\n", code, s->mb_x,s->mb_y, pred); |
307 | 1457 if (code < 0) |
1458 return 0xffff; | |
1459 | |
1460 if (code == 0) | |
1461 return pred; | |
1462 sign = get_bits1(&s->gb); | |
1463 shift = f_code - 1; | |
1255
625ccacd1113
decode motion & modulo optimize patch by (BERO <bero at geocities dot co dot jp>)
michaelni
parents:
1216
diff
changeset
|
1464 val = code; |
625ccacd1113
decode motion & modulo optimize patch by (BERO <bero at geocities dot co dot jp>)
michaelni
parents:
1216
diff
changeset
|
1465 if (shift) { |
625ccacd1113
decode motion & modulo optimize patch by (BERO <bero at geocities dot co dot jp>)
michaelni
parents:
1216
diff
changeset
|
1466 val = (val - 1) << shift; |
307 | 1467 val |= get_bits(&s->gb, shift); |
1255
625ccacd1113
decode motion & modulo optimize patch by (BERO <bero at geocities dot co dot jp>)
michaelni
parents:
1216
diff
changeset
|
1468 val++; |
625ccacd1113
decode motion & modulo optimize patch by (BERO <bero at geocities dot co dot jp>)
michaelni
parents:
1216
diff
changeset
|
1469 } |
307 | 1470 if (sign) |
1471 val = -val; | |
457 | 1472 |
307 | 1473 val += pred; |
1474 if (val <= -64) | |
1475 val += 64; | |
1476 else if (val >= 64) | |
1477 val -= 64; | |
1478 | |
1479 return val; | |
1480 } | |
1481 | |
745
25d7fb7c89be
better/cleaner error resilience (done in a 2nd pass after decoding)
michaelni
parents:
743
diff
changeset
|
1482 static int msmpeg4v12_decode_mb(MpegEncContext *s, DCTELEM block[6][64]) |
307 | 1483 { |
1484 int cbp, code, i; | |
745
25d7fb7c89be
better/cleaner error resilience (done in a 2nd pass after decoding)
michaelni
parents:
743
diff
changeset
|
1485 |
307 | 1486 if (s->pict_type == P_TYPE) { |
1487 if (s->use_skip_mb_code) { | |
1488 if (get_bits1(&s->gb)) { | |
1489 /* skip mb */ | |
1490 s->mb_intra = 0; | |
1491 for(i=0;i<6;i++) | |
1492 s->block_last_index[i] = -1; | |
1493 s->mv_dir = MV_DIR_FORWARD; | |
1494 s->mv_type = MV_TYPE_16X16; | |
1495 s->mv[0][0][0] = 0; | |
1496 s->mv[0][0][1] = 0; | |
2628
511e3afc43e1
Ministry of English Composition, reporting for duty (and the word is "skipped", not "skiped"; "skiped" would rhyme with "hyped")
melanson
parents:
2615
diff
changeset
|
1497 s->mb_skipped = 1; |
307 | 1498 return 0; |
1499 } | |
1500 } | |
1501 | |
457 | 1502 if(s->msmpeg4_version==2) |
531 | 1503 code = get_vlc2(&s->gb, v2_mb_type_vlc.table, V2_MB_TYPE_VLC_BITS, 1); |
457 | 1504 else |
531 | 1505 code = get_vlc2(&s->gb, v1_inter_cbpc_vlc.table, V1_INTER_CBPC_VLC_BITS, 3); |
457 | 1506 if(code<0 || code>7){ |
1598
932d306bf1dc
av_log() patch by (Michel Bardiaux <mbardiaux at peaktime dot be>)
michael
parents:
1399
diff
changeset
|
1507 av_log(s->avctx, AV_LOG_ERROR, "cbpc %d invalid at %d %d\n", code, s->mb_x, s->mb_y); |
457 | 1508 return -1; |
1509 } | |
1510 | |
307 | 1511 s->mb_intra = code >>2; |
1512 | |
1513 cbp = code & 0x3; | |
1514 } else { | |
1515 s->mb_intra = 1; | |
457 | 1516 if(s->msmpeg4_version==2) |
531 | 1517 cbp= get_vlc2(&s->gb, v2_intra_cbpc_vlc.table, V2_INTRA_CBPC_VLC_BITS, 1); |
457 | 1518 else |
531 | 1519 cbp= get_vlc2(&s->gb, v1_intra_cbpc_vlc.table, V1_INTRA_CBPC_VLC_BITS, 1); |
457 | 1520 if(cbp<0 || cbp>3){ |
1598
932d306bf1dc
av_log() patch by (Michel Bardiaux <mbardiaux at peaktime dot be>)
michael
parents:
1399
diff
changeset
|
1521 av_log(s->avctx, AV_LOG_ERROR, "cbpc %d invalid at %d %d\n", cbp, s->mb_x, s->mb_y); |
457 | 1522 return -1; |
1523 } | |
307 | 1524 } |
1525 | |
1526 if (!s->mb_intra) { | |
457 | 1527 int mx, my, cbpy; |
1528 | |
531 | 1529 cbpy= get_vlc2(&s->gb, cbpy_vlc.table, CBPY_VLC_BITS, 1); |
457 | 1530 if(cbpy<0){ |
1598
932d306bf1dc
av_log() patch by (Michel Bardiaux <mbardiaux at peaktime dot be>)
michael
parents:
1399
diff
changeset
|
1531 av_log(s->avctx, AV_LOG_ERROR, "cbpy %d invalid at %d %d\n", cbp, s->mb_x, s->mb_y); |
457 | 1532 return -1; |
1533 } | |
307 | 1534 |
457 | 1535 cbp|= cbpy<<2; |
1536 if(s->msmpeg4_version==1 || (cbp&3) != 3) cbp^= 0x3C; | |
307 | 1537 |
1938
e2501e6e7ff7
unify table indexing (motion_val,dc_val,ac_val,coded_block changed)
michael
parents:
1739
diff
changeset
|
1538 h263_pred_motion(s, 0, 0, &mx, &my); |
307 | 1539 mx= msmpeg4v2_decode_motion(s, mx, 1); |
1540 my= msmpeg4v2_decode_motion(s, my, 1); | |
1541 | |
1542 s->mv_dir = MV_DIR_FORWARD; | |
1543 s->mv_type = MV_TYPE_16X16; | |
1544 s->mv[0][0][0] = mx; | |
1545 s->mv[0][0][1] = my; | |
1546 } else { | |
457 | 1547 if(s->msmpeg4_version==2){ |
1548 s->ac_pred = get_bits1(&s->gb); | |
531 | 1549 cbp|= get_vlc2(&s->gb, cbpy_vlc.table, CBPY_VLC_BITS, 1)<<2; //FIXME check errors |
457 | 1550 } else{ |
1551 s->ac_pred = 0; | |
531 | 1552 cbp|= get_vlc2(&s->gb, cbpy_vlc.table, CBPY_VLC_BITS, 1)<<2; //FIXME check errors |
457 | 1553 if(s->pict_type==P_TYPE) cbp^=0x3C; |
1554 } | |
307 | 1555 } |
1556 | |
2632 | 1557 s->dsp.clear_blocks(s->block[0]); |
307 | 1558 for (i = 0; i < 6; i++) { |
936 | 1559 if (msmpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1, NULL) < 0) |
307 | 1560 { |
1598
932d306bf1dc
av_log() patch by (Michel Bardiaux <mbardiaux at peaktime dot be>)
michael
parents:
1399
diff
changeset
|
1561 av_log(s->avctx, AV_LOG_ERROR, "\nerror while decoding block: %d x %d (%d)\n", s->mb_x, s->mb_y, i); |
307 | 1562 return -1; |
1563 } | |
1564 } | |
1565 return 0; | |
1566 } | |
1567 | |
745
25d7fb7c89be
better/cleaner error resilience (done in a 2nd pass after decoding)
michaelni
parents:
743
diff
changeset
|
1568 static int msmpeg4v34_decode_mb(MpegEncContext *s, DCTELEM block[6][64]) |
0 | 1569 { |
1570 int cbp, code, i; | |
1064 | 1571 uint8_t *coded_val; |
1177
fea03d2c4946
simplified adressing of most mb based arrays (mb_x + mb_y*s->mb_stride) now instead of mb_x + mb_y*mb_width and 1+mb_x + (1+mb_y)*(mb_width+2) and ... mixture
michaelni
parents:
1164
diff
changeset
|
1572 uint32_t * const mb_type_ptr= &s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride ]; |
0 | 1573 |
1574 if (s->pict_type == P_TYPE) { | |
1575 set_stat(ST_INTER_MB); | |
1576 if (s->use_skip_mb_code) { | |
21 | 1577 if (get_bits1(&s->gb)) { |
0 | 1578 /* skip mb */ |
1579 s->mb_intra = 0; | |
1580 for(i=0;i<6;i++) | |
1581 s->block_last_index[i] = -1; | |
1582 s->mv_dir = MV_DIR_FORWARD; | |
1583 s->mv_type = MV_TYPE_16X16; | |
1584 s->mv[0][0][0] = 0; | |
1585 s->mv[0][0][1] = 0; | |
2628
511e3afc43e1
Ministry of English Composition, reporting for duty (and the word is "skipped", not "skiped"; "skiped" would rhyme with "hyped")
melanson
parents:
2615
diff
changeset
|
1586 s->mb_skipped = 1; |
1177
fea03d2c4946
simplified adressing of most mb based arrays (mb_x + mb_y*s->mb_stride) now instead of mb_x + mb_y*mb_width and 1+mb_x + (1+mb_y)*(mb_width+2) and ... mixture
michaelni
parents:
1164
diff
changeset
|
1587 *mb_type_ptr = MB_TYPE_SKIP | MB_TYPE_L0 | MB_TYPE_16x16; |
fea03d2c4946
simplified adressing of most mb based arrays (mb_x + mb_y*s->mb_stride) now instead of mb_x + mb_y*mb_width and 1+mb_x + (1+mb_y)*(mb_width+2) and ... mixture
michaelni
parents:
1164
diff
changeset
|
1588 |
0 | 1589 return 0; |
1590 } | |
1591 } | |
1592 | |
936 | 1593 code = get_vlc2(&s->gb, mb_non_intra_vlc[DEFAULT_INTER_INDEX].table, MB_NON_INTRA_VLC_BITS, 3); |
0 | 1594 if (code < 0) |
1595 return -1; | |
246 | 1596 //s->mb_intra = (code & 0x40) ? 0 : 1; |
1597 s->mb_intra = (~code & 0x40) >> 6; | |
0 | 1598 |
1599 cbp = code & 0x3f; | |
1600 } else { | |
1601 set_stat(ST_INTRA_MB); | |
1602 s->mb_intra = 1; | |
2474 | 1603 code = get_vlc2(&s->gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2); |
0 | 1604 if (code < 0) |
1605 return -1; | |
1606 /* predict coded block pattern */ | |
1607 cbp = 0; | |
1608 for(i=0;i<6;i++) { | |
246 | 1609 int val = ((code >> (5 - i)) & 1); |
0 | 1610 if (i < 4) { |
246 | 1611 int pred = coded_block_pred(s, i, &coded_val); |
0 | 1612 val = val ^ pred; |
1613 *coded_val = val; | |
1614 } | |
1615 cbp |= val << (5 - i); | |
1616 } | |
1617 } | |
1618 | |
1619 if (!s->mb_intra) { | |
1620 int mx, my; | |
499 | 1621 //printf("P at %d %d\n", s->mb_x, s->mb_y); |
1622 if(s->per_mb_rl_table && cbp){ | |
1623 s->rl_table_index = decode012(&s->gb); | |
1624 s->rl_chroma_table_index = s->rl_table_index; | |
1625 } | |
0 | 1626 set_stat(ST_MV); |
1938
e2501e6e7ff7
unify table indexing (motion_val,dc_val,ac_val,coded_block changed)
michael
parents:
1739
diff
changeset
|
1627 h263_pred_motion(s, 0, 0, &mx, &my); |
0 | 1628 if (msmpeg4_decode_motion(s, &mx, &my) < 0) |
1629 return -1; | |
1630 s->mv_dir = MV_DIR_FORWARD; | |
1631 s->mv_type = MV_TYPE_16X16; | |
1632 s->mv[0][0][0] = mx; | |
1633 s->mv[0][0][1] = my; | |
1177
fea03d2c4946
simplified adressing of most mb based arrays (mb_x + mb_y*s->mb_stride) now instead of mb_x + mb_y*mb_width and 1+mb_x + (1+mb_y)*(mb_width+2) and ... mixture
michaelni
parents:
1164
diff
changeset
|
1634 *mb_type_ptr = MB_TYPE_L0 | MB_TYPE_16x16; |
0 | 1635 } else { |
499 | 1636 //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 | 1637 set_stat(ST_INTRA_MB); |
21 | 1638 s->ac_pred = get_bits1(&s->gb); |
1177
fea03d2c4946
simplified adressing of most mb based arrays (mb_x + mb_y*s->mb_stride) now instead of mb_x + mb_y*mb_width and 1+mb_x + (1+mb_y)*(mb_width+2) and ... mixture
michaelni
parents:
1164
diff
changeset
|
1639 *mb_type_ptr = MB_TYPE_INTRA; |
519 | 1640 if(s->inter_intra_pred){ |
531 | 1641 s->h263_aic_dir= get_vlc2(&s->gb, inter_intra_vlc.table, INTER_INTRA_VLC_BITS, 1); |
519 | 1642 // printf("%d%d %d %d/", s->ac_pred, s->h263_aic_dir, s->mb_x, s->mb_y); |
1643 } | |
499 | 1644 if(s->per_mb_rl_table && cbp){ |
1645 s->rl_table_index = decode012(&s->gb); | |
1646 s->rl_chroma_table_index = s->rl_table_index; | |
1647 } | |
0 | 1648 } |
1649 | |
2632 | 1650 s->dsp.clear_blocks(s->block[0]); |
0 | 1651 for (i = 0; i < 6; i++) { |
936 | 1652 if (msmpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1, NULL) < 0) |
246 | 1653 { |
1598
932d306bf1dc
av_log() patch by (Michel Bardiaux <mbardiaux at peaktime dot be>)
michael
parents:
1399
diff
changeset
|
1654 av_log(s->avctx, AV_LOG_ERROR, "\nerror while decoding block: %d x %d (%d)\n", s->mb_x, s->mb_y, i); |
457 | 1655 return -1; |
246 | 1656 } |
0 | 1657 } |
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
|
1658 |
0 | 1659 return 0; |
1660 } | |
563 | 1661 //#define ERROR_DETAILS |
499 | 1662 static inline int msmpeg4_decode_block(MpegEncContext * s, DCTELEM * block, |
936 | 1663 int n, int coded, const uint8_t *scan_table) |
0 | 1664 { |
542 | 1665 int level, i, last, run, run_diff; |
0 | 1666 int dc_pred_dir; |
1667 RLTable *rl; | |
542 | 1668 RL_VLC_ELEM *rl_vlc; |
200 | 1669 int qmul, qadd; |
0 | 1670 |
1671 if (s->mb_intra) { | |
200 | 1672 qmul=1; |
1673 qadd=0; | |
1674 | |
0 | 1675 /* DC coef */ |
1676 set_stat(ST_DC); | |
1677 level = msmpeg4_decode_dc(s, n, &dc_pred_dir); | |
1177
fea03d2c4946
simplified adressing of most mb based arrays (mb_x + mb_y*s->mb_stride) now instead of mb_x + mb_y*mb_width and 1+mb_x + (1+mb_y)*(mb_width+2) and ... mixture
michaelni
parents:
1164
diff
changeset
|
1678 |
457 | 1679 if (level < 0){ |
1598
932d306bf1dc
av_log() patch by (Michel Bardiaux <mbardiaux at peaktime dot be>)
michael
parents:
1399
diff
changeset
|
1680 av_log(s->avctx, AV_LOG_ERROR, "dc overflow- block: %d qscale: %d//\n", n, s->qscale); |
519 | 1681 if(s->inter_intra_pred) level=0; |
1682 else return -1; | |
457 | 1683 } |
0 | 1684 if (n < 4) { |
1685 rl = &rl_table[s->rl_table_index]; | |
457 | 1686 if(level > 256*s->y_dc_scale){ |
1598
932d306bf1dc
av_log() patch by (Michel Bardiaux <mbardiaux at peaktime dot be>)
michael
parents:
1399
diff
changeset
|
1687 av_log(s->avctx, AV_LOG_ERROR, "dc overflow+ L qscale: %d//\n", s->qscale); |
519 | 1688 if(!s->inter_intra_pred) return -1; |
457 | 1689 } |
0 | 1690 } else { |
1691 rl = &rl_table[3 + s->rl_chroma_table_index]; | |
457 | 1692 if(level > 256*s->c_dc_scale){ |
1598
932d306bf1dc
av_log() patch by (Michel Bardiaux <mbardiaux at peaktime dot be>)
michael
parents:
1399
diff
changeset
|
1693 av_log(s->avctx, AV_LOG_ERROR, "dc overflow+ C qscale: %d//\n", s->qscale); |
519 | 1694 if(!s->inter_intra_pred) return -1; |
457 | 1695 } |
0 | 1696 } |
457 | 1697 block[0] = level; |
200 | 1698 |
0 | 1699 run_diff = 0; |
542 | 1700 i = 0; |
0 | 1701 if (!coded) { |
1702 goto not_coded; | |
1703 } | |
1704 if (s->ac_pred) { | |
1705 if (dc_pred_dir == 0) | |
706
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
601
diff
changeset
|
1706 scan_table = s->intra_v_scantable.permutated; /* left */ |
0 | 1707 else |
706
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
601
diff
changeset
|
1708 scan_table = s->intra_h_scantable.permutated; /* top */ |
0 | 1709 } else { |
706
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
601
diff
changeset
|
1710 scan_table = s->intra_scantable.permutated; |
0 | 1711 } |
1712 set_stat(ST_INTRA_AC); | |
542 | 1713 rl_vlc= rl->rl_vlc[0]; |
0 | 1714 } else { |
200 | 1715 qmul = s->qscale << 1; |
1716 qadd = (s->qscale - 1) | 1; | |
542 | 1717 i = -1; |
0 | 1718 rl = &rl_table[3 + s->rl_table_index]; |
307 | 1719 |
1720 if(s->msmpeg4_version==2) | |
1721 run_diff = 0; | |
1722 else | |
1723 run_diff = 1; | |
1724 | |
0 | 1725 if (!coded) { |
542 | 1726 s->block_last_index[n] = i; |
0 | 1727 return 0; |
1728 } | |
936 | 1729 if(!scan_table) |
1730 scan_table = s->inter_scantable.permutated; | |
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); |
2615
0d88e3f89379
avoid UPDATE_CACHE() in GET_RL_VLC() if not needed
michael
parents:
2474
diff
changeset
|
1738 GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 0); |
542 | 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){ |
1598
932d306bf1dc
av_log() patch by (Michel Bardiaux <mbardiaux at peaktime dot be>)
michael
parents:
1399
diff
changeset
|
1762 if(SHOW_UBITS(re, &s->gb, 1)) av_log(s->avctx, AV_LOG_ERROR, "cool a new vlc code ,contact the ffmpeg developers and upload the file\n"); |
542 | 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) | |
2846
40765c51a7a9
Compilation fixes part 1 patch by (Arvind R. and Burkhard Plaum, plaum, ipf uni-stuttgart de)
michael
parents:
2639
diff
changeset
|
1824 av_log(s->avctx, AV_LOG_ERROR, "illegal vlc code in ESC3 level=%d\n", level); |
563 | 1825 else if((i>62 && i<192) || i>192+63) |
2846
40765c51a7a9
Compilation fixes part 1 patch by (Arvind R. and Burkhard Plaum, plaum, ipf uni-stuttgart de)
michael
parents:
2639
diff
changeset
|
1826 av_log(s->avctx, AV_LOG_ERROR, "run overflow in ESC3 i=%d run=%d level=%d\n", i, run, level); |
563 | 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 | |
2615
0d88e3f89379
avoid UPDATE_CACHE() in GET_RL_VLC() if not needed
michael
parents:
2474
diff
changeset
|
1836 GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 1); |
542 | 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) | |
2846
40765c51a7a9
Compilation fixes part 1 patch by (Arvind R. and Burkhard Plaum, plaum, ipf uni-stuttgart de)
michael
parents:
2639
diff
changeset
|
1842 av_log(s->avctx, AV_LOG_ERROR, "illegal vlc code in ESC2 level=%d\n", level); |
563 | 1843 else if((i>62 && i<192) || i>192+63) |
2846
40765c51a7a9
Compilation fixes part 1 patch by (Arvind R. and Burkhard Plaum, plaum, ipf uni-stuttgart de)
michael
parents:
2639
diff
changeset
|
1844 av_log(s->avctx, AV_LOG_ERROR, "run overflow in ESC2 i=%d run=%d level=%d\n", i, run, level); |
563 | 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 | |
2615
0d88e3f89379
avoid UPDATE_CACHE() in GET_RL_VLC() if not needed
michael
parents:
2474
diff
changeset
|
1855 GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 1); |
542 | 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) | |
2846
40765c51a7a9
Compilation fixes part 1 patch by (Arvind R. and Burkhard Plaum, plaum, ipf uni-stuttgart de)
michael
parents:
2639
diff
changeset
|
1862 av_log(s->avctx, AV_LOG_ERROR, "illegal vlc code in ESC1 level=%d\n", level); |
563 | 1863 else if((i>62 && i<192) || i>192+63) |
2846
40765c51a7a9
Compilation fixes part 1 patch by (Arvind R. and Burkhard Plaum, plaum, ipf uni-stuttgart de)
michael
parents:
2639
diff
changeset
|
1864 av_log(s->avctx, AV_LOG_ERROR, "run overflow in ESC1 i=%d run=%d level=%d\n", i, run, level); |
563 | 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) | |
2846
40765c51a7a9
Compilation fixes part 1 patch by (Arvind R. and Burkhard Plaum, plaum, ipf uni-stuttgart de)
michael
parents:
2639
diff
changeset
|
1873 av_log(s->avctx, AV_LOG_ERROR, "illegal vlc code level=%d\n", level); |
563 | 1874 else if((i>62 && i<192) || i>192+63) |
2846
40765c51a7a9
Compilation fixes part 1 patch by (Arvind R. and Burkhard Plaum, plaum, ipf uni-stuttgart de)
michael
parents:
2639
diff
changeset
|
1875 av_log(s->avctx, AV_LOG_ERROR, "run overflow i=%d run=%d level=%d\n", i, run, level); |
563 | 1876 #endif |
0 | 1877 } |
542 | 1878 if (i > 62){ |
1879 i-= 192; | |
1880 if(i&(~63)){ | |
1025
1f9afd8b9131
GetBitContext.size is allways multiplied by 8 -> use size_in_bits to avoid useless *8 in a few inner loops
michaelni
parents:
983
diff
changeset
|
1881 const int left= s->gb.size_in_bits - get_bits_count(&s->gb); |
745
25d7fb7c89be
better/cleaner error resilience (done in a 2nd pass after decoding)
michaelni
parents:
743
diff
changeset
|
1882 if(((i+192 == 64 && level/qmul==-1) || s->error_resilience<=1) && left>=0){ |
1598
932d306bf1dc
av_log() patch by (Michel Bardiaux <mbardiaux at peaktime dot be>)
michael
parents:
1399
diff
changeset
|
1883 av_log(s->avctx, AV_LOG_ERROR, "ignoring overflow at %d %d\n", s->mb_x, s->mb_y); |
563 | 1884 break; |
1885 }else{ | |
1598
932d306bf1dc
av_log() patch by (Michel Bardiaux <mbardiaux at peaktime dot be>)
michael
parents:
1399
diff
changeset
|
1886 av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y); |
563 | 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 } | |
936 | 1906 if(s->msmpeg4_version>=4 && i>0) i=63; //FIXME/XXX optimize |
542 | 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) { | |
2474 | 1927 level = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3); |
307 | 1928 } else { |
2474 | 1929 level = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3); |
307 | 1930 } |
457 | 1931 if (level < 0){ |
1598
932d306bf1dc
av_log() patch by (Michel Bardiaux <mbardiaux at peaktime dot be>)
michael
parents:
1399
diff
changeset
|
1932 av_log(s->avctx, AV_LOG_ERROR, "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){ |
1064 | 1947 int32_t *dc_val; |
457 | 1948 pred = msmpeg4v1_pred_dc(s, n, &dc_val); |
1949 level += pred; | |
1950 | |
1951 /* update predictor */ | |
1952 *dc_val= level; | |
1953 }else{ | |
1064 | 1954 uint16_t *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){ |
1598
932d306bf1dc
av_log() patch by (Michel Bardiaux <mbardiaux at peaktime dot be>)
michael
parents:
1399
diff
changeset
|
1979 av_log(s->avctx, AV_LOG_ERROR, "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 } | |
936 | 2007 |
2008 /* cleanest way to support it | |
2009 * there is too much shared between versions so that we cant have 1 file per version & 1 common | |
2010 * as allmost everything would be in the common file | |
2011 */ | |
2012 #include "wmv2.c" |