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