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