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