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