Mercurial > libavcodec.hg
annotate msmpeg4.c @ 483:97da217aed7f libavcodec
fixed multiple allocation bug
| author | bellard |
|---|---|
| date | Thu, 06 Jun 2002 14:31:18 +0000 |
| parents | 1e23eae32087 |
| children | 8b7a54d58549 |
| 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" | |
| 24 | |
|
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 | |
| 35 /* motion vector table */ | |
| 36 typedef struct MVTable { | |
| 37 int n; | |
| 38 const UINT16 *table_mv_code; | |
| 39 const UINT8 *table_mv_bits; | |
| 40 const UINT8 *table_mvx; | |
| 41 const UINT8 *table_mvy; | |
| 42 UINT16 *table_mv_index; /* encoding: convert mv to index in table_mv */ | |
| 43 VLC vlc; /* decoding: vlc */ | |
| 44 } MVTable; | |
| 45 | |
| 307 | 46 static UINT32 v2_dc_lum_table[512][2]; |
| 47 static UINT32 v2_dc_chroma_table[512][2]; | |
| 48 | |
| 0 | 49 static void msmpeg4_encode_block(MpegEncContext * s, DCTELEM * block, int n); |
| 50 static int msmpeg4_decode_block(MpegEncContext * s, DCTELEM * block, | |
| 51 int n, int coded); | |
| 52 static int msmpeg4_decode_dc(MpegEncContext * s, int n, int *dir_ptr); | |
| 53 static int msmpeg4_decode_motion(MpegEncContext * s, | |
| 54 int *mx_ptr, int *my_ptr); | |
| 310 | 55 static void msmpeg4v2_encode_motion(MpegEncContext * s, int val); |
| 468 | 56 static void init_h263_dc_for_msmpeg4(void); |
| 310 | 57 |
| 0 | 58 |
|
225
ae145876789d
use multiply instead of divides for DC prediction on X86
michaelni
parents:
221
diff
changeset
|
59 extern UINT32 inverse[256]; |
|
ae145876789d
use multiply instead of divides for DC prediction on X86
michaelni
parents:
221
diff
changeset
|
60 |
| 0 | 61 #ifdef DEBUG |
| 62 int intra_count = 0; | |
| 63 int frame_count = 0; | |
| 64 #endif | |
| 65 /* XXX: move it to mpegvideo.h */ | |
| 66 | |
| 67 static int init_done = 0; | |
| 68 | |
| 69 #include "msmpeg4data.h" | |
| 70 | |
| 71 #ifdef STATS | |
| 72 | |
| 73 const char *st_names[ST_NB] = { | |
| 74 "unknown", | |
| 75 "dc", | |
| 76 "intra_ac", | |
| 77 "inter_ac", | |
| 78 "intra_mb", | |
| 79 "inter_mb", | |
| 80 "mv", | |
| 81 }; | |
| 82 | |
| 83 int st_current_index = 0; | |
| 84 unsigned int st_bit_counts[ST_NB]; | |
| 85 unsigned int st_out_bit_counts[ST_NB]; | |
| 86 | |
| 87 #define set_stat(var) st_current_index = var; | |
| 88 | |
| 89 void print_stats(void) | |
| 90 { | |
| 91 unsigned int total; | |
| 92 int i; | |
| 93 | |
| 94 printf("Input:\n"); | |
| 95 total = 0; | |
| 96 for(i=0;i<ST_NB;i++) | |
| 97 total += st_bit_counts[i]; | |
| 98 if (total == 0) | |
| 99 total = 1; | |
| 100 for(i=0;i<ST_NB;i++) { | |
| 101 printf("%-10s : %10.1f %5.1f%%\n", | |
| 102 st_names[i], | |
| 103 (double)st_bit_counts[i] / 8.0, | |
| 104 (double)st_bit_counts[i] * 100.0 / total); | |
| 105 } | |
| 106 printf("%-10s : %10.1f %5.1f%%\n", | |
| 107 "total", | |
| 108 (double)total / 8.0, | |
| 109 100.0); | |
| 110 | |
| 111 printf("Output:\n"); | |
| 112 total = 0; | |
| 113 for(i=0;i<ST_NB;i++) | |
| 114 total += st_out_bit_counts[i]; | |
| 115 if (total == 0) | |
| 116 total = 1; | |
| 117 for(i=0;i<ST_NB;i++) { | |
| 118 printf("%-10s : %10.1f %5.1f%%\n", | |
| 119 st_names[i], | |
| 120 (double)st_out_bit_counts[i] / 8.0, | |
| 121 (double)st_out_bit_counts[i] * 100.0 / total); | |
| 122 } | |
| 123 printf("%-10s : %10.1f %5.1f%%\n", | |
| 124 "total", | |
| 125 (double)total / 8.0, | |
| 126 100.0); | |
| 127 } | |
| 128 | |
| 129 #else | |
| 130 | |
| 131 #define set_stat(var) | |
| 132 | |
| 133 #endif | |
| 134 | |
| 135 /* build the table which associate a (x,y) motion vector to a vlc */ | |
| 136 static void init_mv_table(MVTable *tab) | |
| 137 { | |
| 138 int i, x, y; | |
| 139 | |
|
396
fce0a2520551
removed useless header includes - use av memory functions
glantau
parents:
347
diff
changeset
|
140 tab->table_mv_index = av_malloc(sizeof(UINT16) * 4096); |
| 0 | 141 /* mark all entries as not used */ |
| 142 for(i=0;i<4096;i++) | |
| 143 tab->table_mv_index[i] = tab->n; | |
| 144 | |
| 145 for(i=0;i<tab->n;i++) { | |
| 146 x = tab->table_mvx[i]; | |
| 147 y = tab->table_mvy[i]; | |
| 148 tab->table_mv_index[(x << 6) | y] = i; | |
| 149 } | |
| 150 } | |
| 151 | |
| 152 static void code012(PutBitContext *pb, int n) | |
| 153 { | |
| 154 if (n == 0) { | |
| 155 put_bits(pb, 1, 0); | |
| 156 } else { | |
| 157 put_bits(pb, 1, 1); | |
| 158 put_bits(pb, 1, (n >= 2)); | |
| 159 } | |
| 160 } | |
| 161 | |
| 457 | 162 /* write MSMPEG4 compatible frame header */ |
| 0 | 163 void msmpeg4_encode_picture_header(MpegEncContext * s, int picture_number) |
| 164 { | |
| 165 int i; | |
| 166 | |
| 167 align_put_bits(&s->pb); | |
| 168 | |
| 169 put_bits(&s->pb, 2, s->pict_type - 1); | |
| 170 | |
| 171 put_bits(&s->pb, 5, s->qscale); | |
| 172 | |
| 173 s->rl_table_index = 2; | |
| 457 | 174 if(s->msmpeg4_version<=2) |
| 310 | 175 s->rl_chroma_table_index = 2; /* only for I frame */ |
| 176 else | |
| 177 s->rl_chroma_table_index = 1; /* only for I frame */ | |
| 178 | |
| 0 | 179 s->dc_table_index = 1; |
| 180 s->mv_table_index = 1; /* only if P frame */ | |
| 181 s->use_skip_mb_code = 1; /* only if P frame */ | |
| 182 | |
| 183 if (s->pict_type == I_TYPE) { | |
| 184 put_bits(&s->pb, 5, 0x17); /* indicate only one "slice" */ | |
| 185 | |
| 457 | 186 if(s->msmpeg4_version>2){ |
| 310 | 187 code012(&s->pb, s->rl_chroma_table_index); |
| 188 code012(&s->pb, s->rl_table_index); | |
| 0 | 189 |
| 310 | 190 put_bits(&s->pb, 1, s->dc_table_index); |
| 191 } | |
| 0 | 192 s->no_rounding = 1; |
| 193 } else { | |
| 194 put_bits(&s->pb, 1, s->use_skip_mb_code); | |
| 195 | |
| 196 s->rl_chroma_table_index = s->rl_table_index; | |
| 457 | 197 if(s->msmpeg4_version>2){ |
| 310 | 198 code012(&s->pb, s->rl_table_index); |
| 0 | 199 |
| 310 | 200 put_bits(&s->pb, 1, s->dc_table_index); |
| 0 | 201 |
| 310 | 202 put_bits(&s->pb, 1, s->mv_table_index); |
| 203 } | |
| 204 | |
| 208 | 205 if(s->flipflop_rounding){ |
| 206 s->no_rounding ^= 1; | |
| 207 }else{ | |
| 208 s->no_rounding = 0; | |
| 209 } | |
| 0 | 210 } |
| 211 | |
| 212 if (!init_done) { | |
| 213 /* init various encoding tables */ | |
| 214 init_done = 1; | |
| 215 init_mv_table(&mv_tables[0]); | |
| 216 init_mv_table(&mv_tables[1]); | |
| 217 for(i=0;i<NB_RL_TABLES;i++) | |
| 218 init_rl(&rl_table[i]); | |
| 310 | 219 |
| 220 init_h263_dc_for_msmpeg4(); | |
| 0 | 221 } |
| 222 | |
| 223 #ifdef DEBUG | |
| 224 intra_count = 0; | |
| 225 printf("*****frame %d:\n", frame_count++); | |
| 226 #endif | |
| 227 } | |
| 228 | |
| 208 | 229 void msmpeg4_encode_ext_header(MpegEncContext * s) |
| 230 { | |
|
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
|
231 put_bits(&s->pb, 5, s->frame_rate / FRAME_RATE_BASE); //yes 29.97 -> 29 |
| 208 | 232 |
| 457 | 233 put_bits(&s->pb, 11, MIN(s->bit_rate, 2047)); |
| 208 | 234 |
| 457 | 235 if(s->msmpeg4_version<3) |
| 236 s->flipflop_rounding=0; | |
| 237 else{ | |
| 238 s->flipflop_rounding=1; | |
| 239 put_bits(&s->pb, 1, s->flipflop_rounding); | |
| 240 } | |
| 208 | 241 } |
| 242 | |
| 0 | 243 /* predict coded block */ |
| 244 static inline int coded_block_pred(MpegEncContext * s, int n, UINT8 **coded_block_ptr) | |
| 245 { | |
| 299 | 246 int xy, wrap, pred, a, b, c; |
| 0 | 247 |
| 299 | 248 xy = s->block_index[n]; |
| 249 wrap = s->block_wrap[0]; | |
| 0 | 250 |
| 251 /* B C | |
| 252 * A X | |
| 253 */ | |
| 299 | 254 a = s->coded_block[xy - 1 ]; |
| 255 b = s->coded_block[xy - 1 - wrap]; | |
| 256 c = s->coded_block[xy - wrap]; | |
| 0 | 257 |
| 258 if (b == c) { | |
| 259 pred = a; | |
| 260 } else { | |
| 261 pred = c; | |
| 262 } | |
| 263 | |
| 264 /* store value */ | |
| 299 | 265 *coded_block_ptr = &s->coded_block[xy]; |
| 0 | 266 |
| 267 return pred; | |
| 268 } | |
| 269 | |
| 270 static void msmpeg4_encode_motion(MpegEncContext * s, | |
| 271 int mx, int my) | |
| 272 { | |
| 273 int code; | |
| 274 MVTable *mv; | |
| 275 | |
| 276 /* modulo encoding */ | |
| 277 /* WARNING : you cannot reach all the MVs even with the modulo | |
| 278 encoding. This is a somewhat strange compromise they took !!! */ | |
| 279 if (mx <= -64) | |
| 280 mx += 64; | |
| 281 else if (mx >= 64) | |
| 282 mx -= 64; | |
| 283 if (my <= -64) | |
| 284 my += 64; | |
| 285 else if (my >= 64) | |
| 286 my -= 64; | |
| 287 | |
| 288 mx += 32; | |
| 289 my += 32; | |
| 290 #if 0 | |
| 291 if ((unsigned)mx >= 64 || | |
| 292 (unsigned)my >= 64) | |
| 293 fprintf(stderr, "error mx=%d my=%d\n", mx, my); | |
| 294 #endif | |
| 295 mv = &mv_tables[s->mv_table_index]; | |
| 296 | |
| 297 code = mv->table_mv_index[(mx << 6) | my]; | |
| 298 set_stat(ST_MV); | |
| 299 put_bits(&s->pb, | |
| 300 mv->table_mv_bits[code], | |
| 301 mv->table_mv_code[code]); | |
| 302 if (code == mv->n) { | |
| 303 /* escape : code litterally */ | |
| 304 put_bits(&s->pb, 6, mx); | |
| 305 put_bits(&s->pb, 6, my); | |
| 306 } | |
| 307 } | |
| 308 | |
| 309 void msmpeg4_encode_mb(MpegEncContext * s, | |
| 310 DCTELEM block[6][64], | |
| 311 int motion_x, int motion_y) | |
| 312 { | |
| 313 int cbp, coded_cbp, i; | |
| 314 int pred_x, pred_y; | |
| 315 UINT8 *coded_block; | |
| 316 | |
| 317 if (!s->mb_intra) { | |
| 318 /* compute cbp */ | |
| 319 set_stat(ST_INTER_MB); | |
| 320 cbp = 0; | |
| 321 for (i = 0; i < 6; i++) { | |
| 322 if (s->block_last_index[i] >= 0) | |
| 323 cbp |= 1 << (5 - i); | |
| 324 } | |
| 325 if (s->use_skip_mb_code && (cbp | motion_x | motion_y) == 0) { | |
| 326 /* skip macroblock */ | |
| 327 put_bits(&s->pb, 1, 1); | |
| 328 return; | |
| 329 } | |
| 330 if (s->use_skip_mb_code) | |
| 331 put_bits(&s->pb, 1, 0); /* mb coded */ | |
| 332 | |
| 457 | 333 if(s->msmpeg4_version<=2){ |
| 310 | 334 put_bits(&s->pb, |
| 335 v2_mb_type[cbp&3][1], | |
| 336 v2_mb_type[cbp&3][0]); | |
| 337 if((cbp&3) != 3) coded_cbp= cbp ^ 0x3C; | |
| 338 else coded_cbp= cbp; | |
| 0 | 339 |
| 310 | 340 put_bits(&s->pb, |
| 341 cbpy_tab[coded_cbp>>2][1], | |
| 342 cbpy_tab[coded_cbp>>2][0]); | |
| 343 | |
| 344 h263_pred_motion(s, 0, &pred_x, &pred_y); | |
| 345 msmpeg4v2_encode_motion(s, motion_x - pred_x); | |
| 346 msmpeg4v2_encode_motion(s, motion_y - pred_y); | |
| 347 }else{ | |
| 348 put_bits(&s->pb, | |
| 349 table_mb_non_intra[cbp + 64][1], | |
| 350 table_mb_non_intra[cbp + 64][0]); | |
| 351 | |
| 352 /* motion vector */ | |
| 353 h263_pred_motion(s, 0, &pred_x, &pred_y); | |
| 354 msmpeg4_encode_motion(s, motion_x - pred_x, | |
| 355 motion_y - pred_y); | |
| 356 } | |
| 0 | 357 } else { |
| 358 /* compute cbp */ | |
| 359 cbp = 0; | |
| 360 coded_cbp = 0; | |
| 361 for (i = 0; i < 6; i++) { | |
| 362 int val, pred; | |
| 363 val = (s->block_last_index[i] >= 1); | |
| 364 cbp |= val << (5 - i); | |
| 365 if (i < 4) { | |
| 366 /* predict value for close blocks only for luma */ | |
| 367 pred = coded_block_pred(s, i, &coded_block); | |
| 368 *coded_block = val; | |
| 369 val = val ^ pred; | |
| 370 } | |
| 371 coded_cbp |= val << (5 - i); | |
| 372 } | |
| 373 #if 0 | |
| 374 if (coded_cbp) | |
| 375 printf("cbp=%x %x\n", cbp, coded_cbp); | |
| 376 #endif | |
| 377 | |
| 457 | 378 if(s->msmpeg4_version<=2){ |
| 310 | 379 if (s->pict_type == I_TYPE) { |
| 380 put_bits(&s->pb, | |
| 381 v2_intra_cbpc[cbp&3][1], v2_intra_cbpc[cbp&3][0]); | |
| 382 } else { | |
| 383 if (s->use_skip_mb_code) | |
| 384 put_bits(&s->pb, 1, 0); /* mb coded */ | |
| 385 put_bits(&s->pb, | |
| 386 v2_mb_type[(cbp&3) + 4][1], | |
| 387 v2_mb_type[(cbp&3) + 4][0]); | |
| 388 } | |
| 389 put_bits(&s->pb, 1, 0); /* no AC prediction yet */ | |
| 0 | 390 put_bits(&s->pb, |
| 310 | 391 cbpy_tab[cbp>>2][1], |
| 392 cbpy_tab[cbp>>2][0]); | |
| 393 }else{ | |
| 394 if (s->pict_type == I_TYPE) { | |
| 395 set_stat(ST_INTRA_MB); | |
| 396 put_bits(&s->pb, | |
| 397 table_mb_intra[coded_cbp][1], table_mb_intra[coded_cbp][0]); | |
| 398 } else { | |
| 399 if (s->use_skip_mb_code) | |
| 400 put_bits(&s->pb, 1, 0); /* mb coded */ | |
| 401 put_bits(&s->pb, | |
| 402 table_mb_non_intra[cbp][1], | |
| 403 table_mb_non_intra[cbp][0]); | |
| 404 } | |
| 405 set_stat(ST_INTRA_MB); | |
| 406 put_bits(&s->pb, 1, 0); /* no AC prediction yet */ | |
| 0 | 407 } |
| 408 } | |
| 409 | |
| 410 for (i = 0; i < 6; i++) { | |
| 411 msmpeg4_encode_block(s, block[i], i); | |
| 412 } | |
| 413 } | |
| 414 | |
|
411
5c8b3a717929
workaround dc_scale bug in old ffmpeg msmpeg4v3 encoder (set workaround_bugs=1 for this)
michaelni
parents:
396
diff
changeset
|
415 /* 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
|
416 void ff_old_msmpeg4_dc_scale(MpegEncContext * s) |
| 0 | 417 { |
|
411
5c8b3a717929
workaround dc_scale bug in old ffmpeg msmpeg4v3 encoder (set workaround_bugs=1 for this)
michaelni
parents:
396
diff
changeset
|
418 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
|
419 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
|
420 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
|
421 }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
|
422 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
|
423 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
|
424 }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
|
425 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
|
426 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
|
427 } |
| 0 | 428 } |
| 429 | |
| 457 | 430 static int msmpeg4v1_pred_dc(MpegEncContext * s, int n, |
| 431 INT32 **dc_val_ptr) | |
| 432 { | |
| 433 int i; | |
| 434 | |
| 435 if (n < 4) { | |
| 436 i= 0; | |
| 437 } else { | |
| 438 i= n-3; | |
| 439 } | |
| 440 | |
| 441 *dc_val_ptr= &s->last_dc[i]; | |
| 442 return s->last_dc[i]; | |
| 443 } | |
| 444 | |
| 0 | 445 /* dir = 0: left, dir = 1: top prediction */ |
| 446 static int msmpeg4_pred_dc(MpegEncContext * s, int n, | |
| 25 | 447 INT16 **dc_val_ptr, int *dir_ptr) |
| 0 | 448 { |
| 299 | 449 int a, b, c, wrap, pred, scale; |
| 25 | 450 INT16 *dc_val; |
| 0 | 451 |
| 452 /* find prediction */ | |
| 453 if (n < 4) { | |
| 454 scale = s->y_dc_scale; | |
| 455 } else { | |
| 456 scale = s->c_dc_scale; | |
| 457 } | |
| 457 | 458 |
| 299 | 459 wrap = s->block_wrap[n]; |
| 460 dc_val= s->dc_val[0] + s->block_index[n]; | |
| 0 | 461 |
| 462 /* B C | |
| 463 * A X | |
| 464 */ | |
| 299 | 465 a = dc_val[ - 1]; |
| 466 b = dc_val[ - 1 - wrap]; | |
| 467 c = dc_val[ - wrap]; | |
| 0 | 468 |
| 469 /* XXX: the following solution consumes divisions, but it does not | |
| 470 necessitate to modify mpegvideo.c. The problem comes from the | |
| 471 fact they decided to store the quantized DC (which would lead | |
| 472 to problems if Q could vary !) */ | |
| 221 | 473 #if defined ARCH_X86 && !defined PIC |
| 204 | 474 asm volatile( |
| 475 "movl %3, %%eax \n\t" | |
| 476 "shrl $1, %%eax \n\t" | |
| 477 "addl %%eax, %2 \n\t" | |
| 478 "addl %%eax, %1 \n\t" | |
| 479 "addl %0, %%eax \n\t" | |
|
225
ae145876789d
use multiply instead of divides for DC prediction on X86
michaelni
parents:
221
diff
changeset
|
480 "mull %4 \n\t" |
|
ae145876789d
use multiply instead of divides for DC prediction on X86
michaelni
parents:
221
diff
changeset
|
481 "movl %%edx, %0 \n\t" |
| 204 | 482 "movl %1, %%eax \n\t" |
|
225
ae145876789d
use multiply instead of divides for DC prediction on X86
michaelni
parents:
221
diff
changeset
|
483 "mull %4 \n\t" |
|
ae145876789d
use multiply instead of divides for DC prediction on X86
michaelni
parents:
221
diff
changeset
|
484 "movl %%edx, %1 \n\t" |
| 204 | 485 "movl %2, %%eax \n\t" |
|
225
ae145876789d
use multiply instead of divides for DC prediction on X86
michaelni
parents:
221
diff
changeset
|
486 "mull %4 \n\t" |
|
ae145876789d
use multiply instead of divides for DC prediction on X86
michaelni
parents:
221
diff
changeset
|
487 "movl %%edx, %2 \n\t" |
| 228 | 488 : "+b" (a), "+c" (b), "+D" (c) |
| 489 : "g" (scale), "S" (inverse[scale]) | |
| 204 | 490 : "%eax", "%edx" |
| 491 ); | |
| 221 | 492 #else |
| 493 /* #elif defined (ARCH_ALPHA) */ | |
|
214
73df666cacc7
Alpha optimizations by Falk Hueffner <falk.hueffner@student.uni-tuebingen.de>
nickols_k
parents:
208
diff
changeset
|
494 /* Divisions are extremely costly on Alpha; optimize the most |
| 221 | 495 common case. But they are costly everywhere... |
| 496 */ | |
|
214
73df666cacc7
Alpha optimizations by Falk Hueffner <falk.hueffner@student.uni-tuebingen.de>
nickols_k
parents:
208
diff
changeset
|
497 if (scale == 8) { |
|
73df666cacc7
Alpha optimizations by Falk Hueffner <falk.hueffner@student.uni-tuebingen.de>
nickols_k
parents:
208
diff
changeset
|
498 a = (a + (8 >> 1)) / 8; |
|
73df666cacc7
Alpha optimizations by Falk Hueffner <falk.hueffner@student.uni-tuebingen.de>
nickols_k
parents:
208
diff
changeset
|
499 b = (b + (8 >> 1)) / 8; |
|
73df666cacc7
Alpha optimizations by Falk Hueffner <falk.hueffner@student.uni-tuebingen.de>
nickols_k
parents:
208
diff
changeset
|
500 c = (c + (8 >> 1)) / 8; |
|
73df666cacc7
Alpha optimizations by Falk Hueffner <falk.hueffner@student.uni-tuebingen.de>
nickols_k
parents:
208
diff
changeset
|
501 } else { |
|
73df666cacc7
Alpha optimizations by Falk Hueffner <falk.hueffner@student.uni-tuebingen.de>
nickols_k
parents:
208
diff
changeset
|
502 a = (a + (scale >> 1)) / scale; |
|
73df666cacc7
Alpha optimizations by Falk Hueffner <falk.hueffner@student.uni-tuebingen.de>
nickols_k
parents:
208
diff
changeset
|
503 b = (b + (scale >> 1)) / scale; |
|
73df666cacc7
Alpha optimizations by Falk Hueffner <falk.hueffner@student.uni-tuebingen.de>
nickols_k
parents:
208
diff
changeset
|
504 c = (c + (scale >> 1)) / scale; |
|
73df666cacc7
Alpha optimizations by Falk Hueffner <falk.hueffner@student.uni-tuebingen.de>
nickols_k
parents:
208
diff
changeset
|
505 } |
| 204 | 506 #endif |
| 0 | 507 /* XXX: WARNING: they did not choose the same test as MPEG4. This |
| 508 is very important ! */ | |
| 509 if (abs(a - b) <= abs(b - c)) { | |
| 510 pred = c; | |
| 511 *dir_ptr = 1; | |
| 512 } else { | |
| 513 pred = a; | |
| 514 *dir_ptr = 0; | |
| 515 } | |
| 516 | |
| 517 /* update predictor */ | |
| 299 | 518 *dc_val_ptr = &dc_val[0]; |
| 0 | 519 return pred; |
| 520 } | |
| 521 | |
| 522 #define DC_MAX 119 | |
| 523 | |
| 524 static void msmpeg4_encode_dc(MpegEncContext * s, int level, int n, int *dir_ptr) | |
| 525 { | |
| 526 int sign, code; | |
| 527 int pred; | |
| 528 | |
| 457 | 529 if(s->msmpeg4_version==1){ |
| 530 INT32 *dc_val; | |
| 531 pred = msmpeg4v1_pred_dc(s, n, &dc_val); | |
| 532 | |
| 533 /* update predictor */ | |
| 534 *dc_val= level; | |
| 535 }else{ | |
| 536 INT16 *dc_val; | |
| 537 pred = msmpeg4_pred_dc(s, n, &dc_val, dir_ptr); | |
| 538 | |
| 539 /* update predictor */ | |
| 540 if (n < 4) { | |
| 541 *dc_val = level * s->y_dc_scale; | |
| 542 } else { | |
| 543 *dc_val = level * s->c_dc_scale; | |
| 544 } | |
| 0 | 545 } |
| 546 | |
| 547 /* do the prediction */ | |
| 548 level -= pred; | |
| 549 | |
| 457 | 550 if(s->msmpeg4_version<=2){ |
| 310 | 551 if (n < 4) { |
| 552 put_bits(&s->pb, | |
| 553 v2_dc_lum_table[level+256][1], | |
| 554 v2_dc_lum_table[level+256][0]); | |
| 555 }else{ | |
| 556 put_bits(&s->pb, | |
| 557 v2_dc_chroma_table[level+256][1], | |
| 558 v2_dc_chroma_table[level+256][0]); | |
| 559 } | |
| 560 }else{ | |
| 561 sign = 0; | |
| 562 if (level < 0) { | |
| 563 level = -level; | |
| 564 sign = 1; | |
| 565 } | |
| 566 code = level; | |
| 567 if (code > DC_MAX) | |
| 568 code = DC_MAX; | |
| 0 | 569 |
| 310 | 570 if (s->dc_table_index == 0) { |
| 571 if (n < 4) { | |
| 572 put_bits(&s->pb, table0_dc_lum[code][1], table0_dc_lum[code][0]); | |
| 573 } else { | |
| 574 put_bits(&s->pb, table0_dc_chroma[code][1], table0_dc_chroma[code][0]); | |
| 575 } | |
| 0 | 576 } else { |
| 310 | 577 if (n < 4) { |
| 578 put_bits(&s->pb, table1_dc_lum[code][1], table1_dc_lum[code][0]); | |
| 579 } else { | |
| 580 put_bits(&s->pb, table1_dc_chroma[code][1], table1_dc_chroma[code][0]); | |
| 581 } | |
| 0 | 582 } |
| 310 | 583 |
| 584 if (code == DC_MAX) | |
| 585 put_bits(&s->pb, 8, level); | |
| 586 | |
| 587 if (level != 0) { | |
| 588 put_bits(&s->pb, 1, sign); | |
| 589 } | |
| 0 | 590 } |
| 591 } | |
| 592 | |
| 593 /* Encoding of a block. Very similar to MPEG4 except for a different | |
| 594 escape coding (same as H263) and more vlc tables. | |
| 595 */ | |
| 596 static void msmpeg4_encode_block(MpegEncContext * s, DCTELEM * block, int n) | |
| 597 { | |
| 598 int level, run, last, i, j, last_index; | |
| 599 int last_non_zero, sign, slevel; | |
| 600 int code, run_diff, dc_pred_dir; | |
| 601 const RLTable *rl; | |
| 602 | |
| 603 if (s->mb_intra) { | |
| 604 set_stat(ST_DC); | |
| 605 msmpeg4_encode_dc(s, block[0], n, &dc_pred_dir); | |
| 606 i = 1; | |
| 607 if (n < 4) { | |
| 608 rl = &rl_table[s->rl_table_index]; | |
| 609 } else { | |
| 610 rl = &rl_table[3 + s->rl_chroma_table_index]; | |
| 611 } | |
| 612 run_diff = 0; | |
| 613 set_stat(ST_INTRA_AC); | |
| 614 } else { | |
| 615 i = 0; | |
| 616 rl = &rl_table[3 + s->rl_table_index]; | |
| 457 | 617 if(s->msmpeg4_version<=2) |
| 310 | 618 run_diff = 0; |
| 619 else | |
| 620 run_diff = 1; | |
| 0 | 621 set_stat(ST_INTER_AC); |
| 622 } | |
| 623 | |
| 624 /* AC coefs */ | |
| 625 last_index = s->block_last_index[n]; | |
| 626 last_non_zero = i - 1; | |
| 627 for (; i <= last_index; i++) { | |
| 628 j = zigzag_direct[i]; | |
| 629 level = block[j]; | |
| 630 if (level) { | |
| 631 run = i - last_non_zero - 1; | |
| 632 last = (i == last_index); | |
| 633 sign = 0; | |
| 634 slevel = level; | |
| 635 if (level < 0) { | |
| 636 sign = 1; | |
| 637 level = -level; | |
| 638 } | |
| 639 code = get_rl_index(rl, last, run, level); | |
| 640 put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]); | |
| 641 if (code == rl->n) { | |
| 642 int level1, run1; | |
| 643 | |
| 644 level1 = level - rl->max_level[last][run]; | |
| 645 if (level1 < 1) | |
| 646 goto esc2; | |
| 647 code = get_rl_index(rl, last, run, level1); | |
| 648 if (code == rl->n) { | |
| 649 esc2: | |
| 650 put_bits(&s->pb, 1, 0); | |
| 651 if (level > MAX_LEVEL) | |
| 652 goto esc3; | |
| 653 run1 = run - rl->max_run[last][level] - run_diff; | |
| 654 if (run1 < 0) | |
| 655 goto esc3; | |
| 656 code = get_rl_index(rl, last, run1, level); | |
| 657 if (code == rl->n) { | |
| 658 esc3: | |
| 659 /* third escape */ | |
| 660 put_bits(&s->pb, 1, 0); | |
| 661 put_bits(&s->pb, 1, last); | |
| 662 put_bits(&s->pb, 6, run); | |
| 663 put_bits(&s->pb, 8, slevel & 0xff); | |
| 664 } else { | |
| 665 /* second escape */ | |
| 666 put_bits(&s->pb, 1, 1); | |
| 667 put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]); | |
| 668 put_bits(&s->pb, 1, sign); | |
| 669 } | |
| 670 } else { | |
| 671 /* first escape */ | |
| 672 put_bits(&s->pb, 1, 1); | |
| 673 put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]); | |
| 674 put_bits(&s->pb, 1, sign); | |
| 675 } | |
| 676 } else { | |
| 677 put_bits(&s->pb, 1, sign); | |
| 678 } | |
| 679 last_non_zero = i; | |
| 680 } | |
| 681 } | |
| 682 } | |
| 683 | |
| 684 /****************************************/ | |
| 685 /* decoding stuff */ | |
| 686 | |
| 687 static VLC mb_non_intra_vlc; | |
| 688 static VLC mb_intra_vlc; | |
| 689 static VLC dc_lum_vlc[2]; | |
| 690 static VLC dc_chroma_vlc[2]; | |
| 307 | 691 static VLC v2_dc_lum_vlc; |
| 692 static VLC v2_dc_chroma_vlc; | |
| 693 static VLC cbpy_vlc; | |
| 694 static VLC v2_intra_cbpc_vlc; | |
| 695 static VLC v2_mb_type_vlc; | |
| 696 static VLC v2_mv_vlc; | |
| 457 | 697 static VLC v1_intra_cbpc_vlc; |
| 698 static VLC v1_inter_cbpc_vlc; | |
| 307 | 699 |
| 700 /* this table is practically identical to the one from h263 except that its inverted */ | |
| 468 | 701 static void init_h263_dc_for_msmpeg4(void) |
| 307 | 702 { |
| 703 static int inited=0; | |
| 704 | |
| 705 if(!inited){ | |
| 706 int level, uni_code, uni_len; | |
| 707 inited=1; | |
| 708 | |
| 309 | 709 for(level=-256; level<256; level++){ |
| 307 | 710 int size, v, l; |
| 711 /* find number of bits */ | |
| 712 size = 0; | |
| 713 v = abs(level); | |
| 714 while (v) { | |
| 715 v >>= 1; | |
| 716 size++; | |
| 717 } | |
| 718 | |
| 719 if (level < 0) | |
| 720 l= (-level) ^ ((1 << size) - 1); | |
| 721 else | |
| 722 l= level; | |
| 723 | |
| 724 /* luminance h263 */ | |
| 725 uni_code= DCtab_lum[size][0]; | |
| 726 uni_len = DCtab_lum[size][1]; | |
| 727 uni_code ^= (1<<uni_len)-1; //M$ doesnt like compatibility | |
| 728 | |
| 729 if (size > 0) { | |
| 730 uni_code<<=size; uni_code|=l; | |
| 731 uni_len+=size; | |
| 732 if (size > 8){ | |
| 733 uni_code<<=1; uni_code|=1; | |
| 734 uni_len++; | |
| 735 } | |
| 736 } | |
| 737 v2_dc_lum_table[level+256][0]= uni_code; | |
| 738 v2_dc_lum_table[level+256][1]= uni_len; | |
| 739 | |
| 740 /* chrominance h263 */ | |
| 741 uni_code= DCtab_chrom[size][0]; | |
| 742 uni_len = DCtab_chrom[size][1]; | |
| 743 uni_code ^= (1<<uni_len)-1; //M$ doesnt like compatibility | |
| 744 | |
| 745 if (size > 0) { | |
| 746 uni_code<<=size; uni_code|=l; | |
| 747 uni_len+=size; | |
| 748 if (size > 8){ | |
| 749 uni_code<<=1; uni_code|=1; | |
| 750 uni_len++; | |
| 751 } | |
| 752 } | |
| 753 v2_dc_chroma_table[level+256][0]= uni_code; | |
| 754 v2_dc_chroma_table[level+256][1]= uni_len; | |
| 755 | |
| 756 } | |
| 757 } | |
| 758 } | |
| 0 | 759 |
| 760 /* init all vlc decoding tables */ | |
| 761 int msmpeg4_decode_init_vlc(MpegEncContext *s) | |
| 762 { | |
| 483 | 763 static int done = 0; |
| 0 | 764 int i; |
| 765 MVTable *mv; | |
| 766 | |
| 483 | 767 |
| 768 if (!done) { | |
| 769 done = 1; | |
| 0 | 770 |
| 483 | 771 for(i=0;i<NB_RL_TABLES;i++) { |
| 772 init_rl(&rl_table[i]); | |
| 773 init_vlc_rl(&rl_table[i]); | |
| 774 } | |
| 775 for(i=0;i<2;i++) { | |
| 776 mv = &mv_tables[i]; | |
| 777 init_vlc(&mv->vlc, 9, mv->n + 1, | |
| 778 mv->table_mv_bits, 1, 1, | |
| 779 mv->table_mv_code, 2, 2); | |
| 780 } | |
| 781 | |
| 782 init_vlc(&dc_lum_vlc[0], 9, 120, | |
| 783 &table0_dc_lum[0][1], 8, 4, | |
| 784 &table0_dc_lum[0][0], 8, 4); | |
| 785 init_vlc(&dc_chroma_vlc[0], 9, 120, | |
| 786 &table0_dc_chroma[0][1], 8, 4, | |
| 787 &table0_dc_chroma[0][0], 8, 4); | |
| 788 init_vlc(&dc_lum_vlc[1], 9, 120, | |
| 789 &table1_dc_lum[0][1], 8, 4, | |
| 790 &table1_dc_lum[0][0], 8, 4); | |
| 791 init_vlc(&dc_chroma_vlc[1], 9, 120, | |
| 792 &table1_dc_chroma[0][1], 8, 4, | |
| 793 &table1_dc_chroma[0][0], 8, 4); | |
| 307 | 794 |
| 483 | 795 init_h263_dc_for_msmpeg4(); |
| 796 init_vlc(&v2_dc_lum_vlc, 9, 512, | |
| 797 &v2_dc_lum_table[0][1], 8, 4, | |
| 798 &v2_dc_lum_table[0][0], 8, 4); | |
| 799 init_vlc(&v2_dc_chroma_vlc, 9, 512, | |
| 800 &v2_dc_chroma_table[0][1], 8, 4, | |
| 801 &v2_dc_chroma_table[0][0], 8, 4); | |
| 307 | 802 |
| 483 | 803 init_vlc(&cbpy_vlc, 6, 16, |
| 804 &cbpy_tab[0][1], 2, 1, | |
| 805 &cbpy_tab[0][0], 2, 1); | |
| 806 init_vlc(&v2_intra_cbpc_vlc, 3, 4, | |
| 807 &v2_intra_cbpc[0][1], 2, 1, | |
| 808 &v2_intra_cbpc[0][0], 2, 1); | |
| 809 init_vlc(&v2_mb_type_vlc, 5, 8, | |
| 810 &v2_mb_type[0][1], 2, 1, | |
| 811 &v2_mb_type[0][0], 2, 1); | |
| 812 init_vlc(&v2_mv_vlc, 9, 33, | |
| 813 &mvtab[0][1], 2, 1, | |
| 814 &mvtab[0][0], 2, 1); | |
| 0 | 815 |
| 483 | 816 init_vlc(&mb_non_intra_vlc, 9, 128, |
| 817 &table_mb_non_intra[0][1], 8, 4, | |
| 818 &table_mb_non_intra[0][0], 8, 4); | |
| 819 init_vlc(&mb_intra_vlc, 9, 64, | |
| 820 &table_mb_intra[0][1], 4, 2, | |
| 821 &table_mb_intra[0][0], 4, 2); | |
| 457 | 822 |
| 483 | 823 init_vlc(&v1_intra_cbpc_vlc, 6, 8, |
| 824 intra_MCBPC_bits, 1, 1, | |
| 825 intra_MCBPC_code, 1, 1); | |
| 826 init_vlc(&v1_inter_cbpc_vlc, 6, 25, | |
| 827 inter_MCBPC_bits, 1, 1, | |
| 828 inter_MCBPC_code, 1, 1); | |
| 829 } | |
| 0 | 830 return 0; |
| 831 } | |
| 832 | |
| 833 static int decode012(GetBitContext *gb) | |
| 834 { | |
| 835 int n; | |
| 21 | 836 n = get_bits1(gb); |
| 0 | 837 if (n == 0) |
| 838 return 0; | |
| 839 else | |
| 21 | 840 return get_bits1(gb) + 1; |
| 0 | 841 } |
| 842 | |
| 307 | 843 int msmpeg4_decode_picture_header(MpegEncContext * s) |
|
300
d874359e58f1
msmpeg4v2 header parser & some dump bits code behind #if 0
michaelni
parents:
299
diff
changeset
|
844 { |
| 311 | 845 int code, code2; |
|
300
d874359e58f1
msmpeg4v2 header parser & some dump bits code behind #if 0
michaelni
parents:
299
diff
changeset
|
846 |
| 311 | 847 #if 0 |
| 848 { | |
| 849 int i; | |
| 850 for(i=0; i<s->gb.size*8; i++) | |
| 851 printf("%d", get_bits1(&s->gb)); | |
| 852 // get_bits1(&s->gb); | |
| 853 printf("END\n"); | |
| 854 return -1; | |
| 855 } | |
| 856 #endif | |
| 457 | 857 |
| 858 if(s->msmpeg4_version==1){ | |
| 859 int start_code, num; | |
| 860 start_code = (get_bits(&s->gb, 16)<<16) | get_bits(&s->gb, 16); | |
| 861 if(start_code!=0x00000100){ | |
| 862 fprintf(stderr, "invalid startcode\n"); | |
| 863 return -1; | |
| 864 } | |
| 865 | |
| 866 num= get_bits(&s->gb, 5); // frame number */ | |
| 867 } | |
| 868 | |
|
300
d874359e58f1
msmpeg4v2 header parser & some dump bits code behind #if 0
michaelni
parents:
299
diff
changeset
|
869 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
|
870 if (s->pict_type != I_TYPE && |
| 457 | 871 s->pict_type != P_TYPE){ |
| 872 fprintf(stderr, "invalid picture type\n"); | |
|
300
d874359e58f1
msmpeg4v2 header parser & some dump bits code behind #if 0
michaelni
parents:
299
diff
changeset
|
873 return -1; |
| 457 | 874 } |
|
300
d874359e58f1
msmpeg4v2 header parser & some dump bits code behind #if 0
michaelni
parents:
299
diff
changeset
|
875 |
|
d874359e58f1
msmpeg4v2 header parser & some dump bits code behind #if 0
michaelni
parents:
299
diff
changeset
|
876 s->qscale = get_bits(&s->gb, 5); |
|
d874359e58f1
msmpeg4v2 header parser & some dump bits code behind #if 0
michaelni
parents:
299
diff
changeset
|
877 |
|
d874359e58f1
msmpeg4v2 header parser & some dump bits code behind #if 0
michaelni
parents:
299
diff
changeset
|
878 if (s->pict_type == I_TYPE) { |
|
d874359e58f1
msmpeg4v2 header parser & some dump bits code behind #if 0
michaelni
parents:
299
diff
changeset
|
879 code = get_bits(&s->gb, 5); |
| 457 | 880 if(s->msmpeg4_version==1){ |
| 881 if(code==0 || code>s->mb_height){ | |
| 882 fprintf(stderr, "invalid slice height %d\n", code); | |
| 883 return -1; | |
| 884 } | |
| 885 | |
| 886 s->slice_height = code; | |
| 887 }else{ | |
| 888 /* 0x17: one slice, 0x18: two slices, ... */ | |
| 889 if (code < 0x17) | |
| 890 return -1; | |
| 891 | |
| 892 s->slice_height = s->mb_height / (code - 0x16); | |
| 893 } | |
| 311 | 894 |
| 895 switch(s->msmpeg4_version){ | |
| 457 | 896 case 1: |
| 311 | 897 case 2: |
| 307 | 898 s->rl_chroma_table_index = 2; |
| 899 s->rl_table_index = 2; | |
|
300
d874359e58f1
msmpeg4v2 header parser & some dump bits code behind #if 0
michaelni
parents:
299
diff
changeset
|
900 |
| 307 | 901 s->dc_table_index = 0; //not used |
| 311 | 902 break; |
| 903 case 3: | |
| 307 | 904 s->rl_chroma_table_index = decode012(&s->gb); |
| 905 s->rl_table_index = decode012(&s->gb); | |
| 0 | 906 |
| 307 | 907 s->dc_table_index = get_bits1(&s->gb); |
| 311 | 908 break; |
| 909 case 4: | |
| 910 msmpeg4_decode_ext_header(s, 999 /* bufer size (useless here) */); | |
| 911 printf("%X\n", show_bits(&s->gb, 24)); | |
| 912 code= get_bits(&s->gb, 2); | |
| 913 if(code==1){ | |
| 914 code2= get_bits(&s->gb, 3); | |
| 915 if(code2==7) skip_bits(&s->gb, 1); | |
| 916 } | |
| 917 printf("%X\n", show_bits(&s->gb, 24)); | |
| 918 s->rl_chroma_table_index = 2; | |
| 919 s->rl_table_index = 2; | |
| 920 | |
| 921 s->dc_table_index = 0; | |
| 922 break; | |
| 307 | 923 } |
| 0 | 924 s->no_rounding = 1; |
| 200 | 925 /* printf(" %d %d %d %d \n", |
| 926 s->qscale, | |
| 927 s->rl_chroma_table_index, | |
| 928 s->rl_table_index, | |
| 929 s->dc_table_index);*/ | |
| 0 | 930 } else { |
| 931 | |
| 457 | 932 switch(s->msmpeg4_version){ |
| 933 case 1: | |
| 934 case 2: | |
| 935 if(s->msmpeg4_version==1) | |
| 936 s->use_skip_mb_code = 1; | |
| 937 else | |
| 938 s->use_skip_mb_code = get_bits1(&s->gb); | |
| 307 | 939 s->rl_table_index = 2; |
| 940 s->rl_chroma_table_index = s->rl_table_index; | |
| 941 s->dc_table_index = 0; //not used | |
| 942 s->mv_table_index = 0; | |
| 457 | 943 break; |
| 944 case 3: | |
| 945 s->use_skip_mb_code = get_bits1(&s->gb); | |
| 307 | 946 s->rl_table_index = decode012(&s->gb); |
| 947 s->rl_chroma_table_index = s->rl_table_index; | |
| 0 | 948 |
| 307 | 949 s->dc_table_index = get_bits1(&s->gb); |
| 950 | |
| 951 s->mv_table_index = get_bits1(&s->gb); | |
| 457 | 952 break; |
| 307 | 953 } |
| 200 | 954 /* printf(" %d %d %d %d %d \n", |
| 955 s->use_skip_mb_code, | |
| 956 s->rl_table_index, | |
| 957 s->rl_chroma_table_index, | |
| 958 s->dc_table_index, | |
| 959 s->mv_table_index);*/ | |
| 208 | 960 if(s->flipflop_rounding){ |
| 961 s->no_rounding ^= 1; | |
| 962 }else{ | |
| 963 s->no_rounding = 0; | |
| 964 } | |
| 965 // printf("%d", s->no_rounding); | |
| 311 | 966 //return -1; |
| 0 | 967 } |
| 208 | 968 |
| 307 | 969 #if 0 |
| 970 if(s->msmpeg4_version==2) | |
| 971 { | |
| 972 int i; | |
| 973 for(i=0; i<s->gb.size*8; i++) | |
| 974 // printf("%d", get_bits1(&s->gb)); | |
| 975 get_bits1(&s->gb); | |
| 976 printf("END\n"); | |
| 977 return -1; | |
| 978 } | |
| 979 #endif | |
| 980 | |
| 0 | 981 #ifdef DEBUG |
| 982 printf("*****frame %d:\n", frame_count++); | |
| 983 #endif | |
| 984 return 0; | |
| 985 } | |
| 986 | |
| 208 | 987 int msmpeg4_decode_ext_header(MpegEncContext * s, int buf_size) |
| 988 { | |
| 457 | 989 int left= buf_size*8 - get_bits_count(&s->gb); |
| 990 int length= s->msmpeg4_version>=3 ? 17 : 16; | |
| 208 | 991 /* the alt_bitstream reader could read over the end so we need to check it */ |
| 457 | 992 if(left>=length && left<length+8) |
| 208 | 993 { |
|
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
|
994 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
|
995 |
|
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
|
996 fps= get_bits(&s->gb, 5); |
| 457 | 997 s->bit_rate= get_bits(&s->gb, 11); |
| 998 if(s->msmpeg4_version>=3) | |
| 999 s->flipflop_rounding= get_bits1(&s->gb); | |
| 1000 else | |
| 1001 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
|
1002 |
| 457 | 1003 // printf("fps:%2d bps:%2d roundingType:%1d\n", fps, s->bit_rate, s->flipflop_rounding); |
| 1004 } | |
| 1005 else if(left<length+8) | |
| 1006 { | |
| 1007 s->flipflop_rounding= 0; | |
| 1008 printf("ext header missing, %d left\n", left); | |
| 208 | 1009 } |
| 1010 else | |
| 1011 { | |
| 457 | 1012 fprintf(stderr, "I frame too long, ignoring ext header\n"); |
| 208 | 1013 } |
|
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
|
1014 |
| 208 | 1015 return 0; |
| 1016 } | |
| 1017 | |
|
440
000aeeac27a2
* started to cleanup name clashes for onetime compilation
kabi
parents:
429
diff
changeset
|
1018 static inline void msmpeg4_memsetw(short *tab, int val, int n) |
| 0 | 1019 { |
| 1020 int i; | |
| 1021 for(i=0;i<n;i++) | |
| 1022 tab[i] = val; | |
| 1023 } | |
| 1024 | |
| 310 | 1025 static void msmpeg4v2_encode_motion(MpegEncContext * s, int val) |
| 1026 { | |
| 1027 int range, bit_size, sign, code, bits; | |
| 1028 | |
| 1029 if (val == 0) { | |
| 1030 /* zero vector */ | |
| 1031 code = 0; | |
| 1032 put_bits(&s->pb, mvtab[code][1], mvtab[code][0]); | |
| 1033 } else { | |
| 1034 bit_size = s->f_code - 1; | |
| 1035 range = 1 << bit_size; | |
| 1036 if (val <= -64) | |
| 1037 val += 64; | |
| 1038 else if (val >= 64) | |
| 1039 val -= 64; | |
| 1040 | |
| 1041 if (val >= 0) { | |
| 1042 sign = 0; | |
| 1043 } else { | |
| 1044 val = -val; | |
| 1045 sign = 1; | |
| 1046 } | |
| 1047 val--; | |
| 1048 code = (val >> bit_size) + 1; | |
| 1049 bits = val & (range - 1); | |
| 1050 | |
| 1051 put_bits(&s->pb, mvtab[code][1] + 1, (mvtab[code][0] << 1) | sign); | |
| 1052 if (bit_size > 0) { | |
| 1053 put_bits(&s->pb, bit_size, bits); | |
| 1054 } | |
| 1055 } | |
| 1056 } | |
| 1057 | |
| 307 | 1058 /* this is identical to h263 except that its range is multiplied by 2 */ |
| 1059 static int msmpeg4v2_decode_motion(MpegEncContext * s, int pred, int f_code) | |
| 1060 { | |
| 1061 int code, val, sign, shift; | |
| 1062 | |
| 1063 code = get_vlc(&s->gb, &v2_mv_vlc); | |
| 457 | 1064 // printf("MV code %d at %d %d pred: %d\n", code, s->mb_x,s->mb_y, pred); |
| 307 | 1065 if (code < 0) |
| 1066 return 0xffff; | |
| 1067 | |
| 1068 if (code == 0) | |
| 1069 return pred; | |
| 1070 sign = get_bits1(&s->gb); | |
| 1071 shift = f_code - 1; | |
| 1072 val = (code - 1) << shift; | |
| 1073 if (shift > 0) | |
| 1074 val |= get_bits(&s->gb, shift); | |
| 1075 val++; | |
| 1076 if (sign) | |
| 1077 val = -val; | |
| 457 | 1078 |
| 307 | 1079 val += pred; |
| 1080 if (val <= -64) | |
| 1081 val += 64; | |
| 1082 else if (val >= 64) | |
| 1083 val -= 64; | |
| 1084 | |
| 1085 return val; | |
| 1086 } | |
| 1087 | |
| 1088 | |
| 457 | 1089 static int msmpeg4v12_decode_mb(MpegEncContext *s, |
| 307 | 1090 DCTELEM block[6][64]) |
| 1091 { | |
| 1092 int cbp, code, i; | |
| 1093 if (s->pict_type == P_TYPE) { | |
| 1094 if (s->use_skip_mb_code) { | |
| 1095 if (get_bits1(&s->gb)) { | |
| 1096 /* skip mb */ | |
| 1097 s->mb_intra = 0; | |
| 1098 for(i=0;i<6;i++) | |
| 1099 s->block_last_index[i] = -1; | |
| 1100 s->mv_dir = MV_DIR_FORWARD; | |
| 1101 s->mv_type = MV_TYPE_16X16; | |
| 1102 s->mv[0][0][0] = 0; | |
| 1103 s->mv[0][0][1] = 0; | |
| 1104 s->mb_skiped = 1; | |
| 1105 return 0; | |
| 1106 } | |
| 1107 } | |
| 1108 | |
| 457 | 1109 if(s->msmpeg4_version==2) |
| 1110 code = get_vlc(&s->gb, &v2_mb_type_vlc); | |
| 1111 else | |
| 1112 code = get_vlc(&s->gb, &v1_inter_cbpc_vlc); | |
| 1113 if(code<0 || code>7){ | |
| 1114 fprintf(stderr, "cbpc %d invalid at %d %d\n", code, s->mb_x, s->mb_y); | |
| 1115 return -1; | |
| 1116 } | |
| 1117 | |
| 307 | 1118 s->mb_intra = code >>2; |
| 1119 | |
| 1120 cbp = code & 0x3; | |
| 1121 } else { | |
| 1122 s->mb_intra = 1; | |
| 457 | 1123 if(s->msmpeg4_version==2) |
| 1124 cbp= get_vlc(&s->gb, &v2_intra_cbpc_vlc); | |
| 1125 else | |
| 1126 cbp= get_vlc(&s->gb, &v1_intra_cbpc_vlc); | |
| 1127 if(cbp<0 || cbp>3){ | |
| 1128 fprintf(stderr, "cbpc %d invalid at %d %d\n", cbp, s->mb_x, s->mb_y); | |
| 1129 return -1; | |
| 1130 } | |
| 307 | 1131 } |
| 1132 | |
| 1133 if (!s->mb_intra) { | |
| 457 | 1134 int mx, my, cbpy; |
| 1135 | |
| 1136 cbpy= get_vlc(&s->gb, &cbpy_vlc); | |
| 1137 if(cbpy<0){ | |
| 1138 fprintf(stderr, "cbpy %d invalid at %d %d\n", cbp, s->mb_x, s->mb_y); | |
| 1139 return -1; | |
| 1140 } | |
| 307 | 1141 |
| 457 | 1142 cbp|= cbpy<<2; |
| 1143 if(s->msmpeg4_version==1 || (cbp&3) != 3) cbp^= 0x3C; | |
| 307 | 1144 |
| 1145 h263_pred_motion(s, 0, &mx, &my); | |
| 1146 mx= msmpeg4v2_decode_motion(s, mx, 1); | |
| 1147 my= msmpeg4v2_decode_motion(s, my, 1); | |
| 1148 | |
| 1149 s->mv_dir = MV_DIR_FORWARD; | |
| 1150 s->mv_type = MV_TYPE_16X16; | |
| 1151 s->mv[0][0][0] = mx; | |
| 1152 s->mv[0][0][1] = my; | |
| 1153 } else { | |
| 457 | 1154 if(s->msmpeg4_version==2){ |
| 1155 s->ac_pred = get_bits1(&s->gb); | |
| 1156 cbp|= get_vlc(&s->gb, &cbpy_vlc)<<2; //FIXME check errors | |
| 1157 } else{ | |
| 1158 s->ac_pred = 0; | |
| 1159 cbp|= get_vlc(&s->gb, &cbpy_vlc)<<2; //FIXME check errors | |
| 1160 if(s->pict_type==P_TYPE) cbp^=0x3C; | |
| 1161 } | |
| 307 | 1162 } |
| 1163 | |
| 1164 for (i = 0; i < 6; i++) { | |
| 1165 if (msmpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1) < 0) | |
| 1166 { | |
| 457 | 1167 fprintf(stderr,"\nerror while decoding block: %d x %d (%d)\n", s->mb_x, s->mb_y, i); |
| 307 | 1168 return -1; |
| 1169 } | |
| 1170 } | |
| 1171 return 0; | |
| 1172 } | |
| 1173 | |
| 0 | 1174 int msmpeg4_decode_mb(MpegEncContext *s, |
| 1175 DCTELEM block[6][64]) | |
| 1176 { | |
| 1177 int cbp, code, i; | |
| 1178 UINT8 *coded_val; | |
| 1179 | |
| 1180 /* special slice handling */ | |
| 1181 if (s->mb_x == 0) { | |
| 122 | 1182 if (s->slice_height && (s->mb_y % s->slice_height) == 0) { |
| 0 | 1183 int wrap; |
| 1184 /* reset DC pred (set previous line to 1024) */ | |
| 1185 wrap = 2 * s->mb_width + 2; | |
|
440
000aeeac27a2
* started to cleanup name clashes for onetime compilation
kabi
parents:
429
diff
changeset
|
1186 msmpeg4_memsetw(&s->dc_val[0][(1) + (2 * s->mb_y) * wrap], |
|
000aeeac27a2
* started to cleanup name clashes for onetime compilation
kabi
parents:
429
diff
changeset
|
1187 1024, 2 * s->mb_width); |
|
000aeeac27a2
* started to cleanup name clashes for onetime compilation
kabi
parents:
429
diff
changeset
|
1188 wrap = s->mb_width + 2; |
|
000aeeac27a2
* started to cleanup name clashes for onetime compilation
kabi
parents:
429
diff
changeset
|
1189 msmpeg4_memsetw(&s->dc_val[1][(1) + (s->mb_y) * wrap], |
|
000aeeac27a2
* started to cleanup name clashes for onetime compilation
kabi
parents:
429
diff
changeset
|
1190 1024, s->mb_width); |
|
000aeeac27a2
* started to cleanup name clashes for onetime compilation
kabi
parents:
429
diff
changeset
|
1191 msmpeg4_memsetw(&s->dc_val[2][(1) + (s->mb_y) * wrap], |
|
000aeeac27a2
* started to cleanup name clashes for onetime compilation
kabi
parents:
429
diff
changeset
|
1192 1024, s->mb_width); |
|
000aeeac27a2
* started to cleanup name clashes for onetime compilation
kabi
parents:
429
diff
changeset
|
1193 |
|
000aeeac27a2
* started to cleanup name clashes for onetime compilation
kabi
parents:
429
diff
changeset
|
1194 /* reset AC pred (set previous line to 0) */ |
|
000aeeac27a2
* started to cleanup name clashes for onetime compilation
kabi
parents:
429
diff
changeset
|
1195 wrap = s->mb_width * 2 + 2; |
|
000aeeac27a2
* started to cleanup name clashes for onetime compilation
kabi
parents:
429
diff
changeset
|
1196 msmpeg4_memsetw(s->ac_val[0][0] + (1 + (2 * s->mb_y) * wrap)*16, |
|
000aeeac27a2
* started to cleanup name clashes for onetime compilation
kabi
parents:
429
diff
changeset
|
1197 0, 2 * s->mb_width*16); |
|
000aeeac27a2
* started to cleanup name clashes for onetime compilation
kabi
parents:
429
diff
changeset
|
1198 wrap = s->mb_width + 2; |
|
000aeeac27a2
* started to cleanup name clashes for onetime compilation
kabi
parents:
429
diff
changeset
|
1199 msmpeg4_memsetw(s->ac_val[1][0] + (1 + (s->mb_y) * wrap)*16, |
|
000aeeac27a2
* started to cleanup name clashes for onetime compilation
kabi
parents:
429
diff
changeset
|
1200 0, s->mb_width*16); |
|
000aeeac27a2
* started to cleanup name clashes for onetime compilation
kabi
parents:
429
diff
changeset
|
1201 msmpeg4_memsetw(s->ac_val[2][0] + (1 + (s->mb_y) * wrap)*16, |
|
000aeeac27a2
* started to cleanup name clashes for onetime compilation
kabi
parents:
429
diff
changeset
|
1202 0, s->mb_width*16); |
| 0 | 1203 |
| 1204 s->first_slice_line = 1; | |
| 1205 } else { | |
| 1206 s->first_slice_line = 0; | |
| 1207 } | |
| 1208 } | |
| 1209 | |
| 457 | 1210 if(s->msmpeg4_version<=2) return msmpeg4v12_decode_mb(s, block); //FIXME export function & call from outside perhaps |
| 307 | 1211 |
| 0 | 1212 if (s->pict_type == P_TYPE) { |
| 1213 set_stat(ST_INTER_MB); | |
| 1214 if (s->use_skip_mb_code) { | |
| 21 | 1215 if (get_bits1(&s->gb)) { |
| 0 | 1216 /* skip mb */ |
| 1217 s->mb_intra = 0; | |
| 1218 for(i=0;i<6;i++) | |
| 1219 s->block_last_index[i] = -1; | |
| 1220 s->mv_dir = MV_DIR_FORWARD; | |
| 1221 s->mv_type = MV_TYPE_16X16; | |
| 1222 s->mv[0][0][0] = 0; | |
| 1223 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
|
1224 s->mb_skiped = 1; |
| 0 | 1225 return 0; |
| 1226 } | |
| 1227 } | |
| 1228 | |
| 1229 code = get_vlc(&s->gb, &mb_non_intra_vlc); | |
| 1230 if (code < 0) | |
| 1231 return -1; | |
| 246 | 1232 //s->mb_intra = (code & 0x40) ? 0 : 1; |
| 1233 s->mb_intra = (~code & 0x40) >> 6; | |
| 0 | 1234 |
| 1235 cbp = code & 0x3f; | |
| 1236 } else { | |
| 1237 set_stat(ST_INTRA_MB); | |
| 1238 s->mb_intra = 1; | |
| 1239 code = get_vlc(&s->gb, &mb_intra_vlc); | |
| 1240 if (code < 0) | |
| 1241 return -1; | |
| 1242 /* predict coded block pattern */ | |
| 1243 cbp = 0; | |
| 1244 for(i=0;i<6;i++) { | |
| 246 | 1245 int val = ((code >> (5 - i)) & 1); |
| 0 | 1246 if (i < 4) { |
| 246 | 1247 int pred = coded_block_pred(s, i, &coded_val); |
| 0 | 1248 val = val ^ pred; |
| 1249 *coded_val = val; | |
| 1250 } | |
| 1251 cbp |= val << (5 - i); | |
| 1252 } | |
| 1253 } | |
| 1254 | |
| 1255 if (!s->mb_intra) { | |
| 1256 int mx, my; | |
| 1257 set_stat(ST_MV); | |
| 1258 h263_pred_motion(s, 0, &mx, &my); | |
| 1259 if (msmpeg4_decode_motion(s, &mx, &my) < 0) | |
| 1260 return -1; | |
| 1261 s->mv_dir = MV_DIR_FORWARD; | |
| 1262 s->mv_type = MV_TYPE_16X16; | |
| 1263 s->mv[0][0][0] = mx; | |
| 1264 s->mv[0][0][1] = my; | |
| 1265 } else { | |
| 1266 set_stat(ST_INTRA_MB); | |
| 21 | 1267 s->ac_pred = get_bits1(&s->gb); |
| 0 | 1268 } |
| 1269 | |
| 1270 for (i = 0; i < 6; i++) { | |
| 1271 if (msmpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1) < 0) | |
| 246 | 1272 { |
| 457 | 1273 fprintf(stderr,"\nerror while decoding block: %d x %d (%d)\n", s->mb_x, s->mb_y, i); |
| 1274 return -1; | |
| 246 | 1275 } |
| 0 | 1276 } |
| 457 | 1277 |
| 0 | 1278 return 0; |
| 1279 } | |
| 1280 | |
| 1281 static int msmpeg4_decode_block(MpegEncContext * s, DCTELEM * block, | |
| 1282 int n, int coded) | |
| 1283 { | |
| 1284 int code, level, i, j, last, run, run_diff; | |
| 1285 int dc_pred_dir; | |
| 1286 RLTable *rl; | |
| 1287 const UINT8 *scan_table; | |
| 200 | 1288 int qmul, qadd; |
| 0 | 1289 |
| 1290 if (s->mb_intra) { | |
| 200 | 1291 qmul=1; |
| 1292 qadd=0; | |
| 1293 | |
| 0 | 1294 /* DC coef */ |
| 1295 set_stat(ST_DC); | |
| 1296 level = msmpeg4_decode_dc(s, n, &dc_pred_dir); | |
| 457 | 1297 if (level < 0){ |
| 1298 fprintf(stderr, "dc overflow-\n"); | |
| 0 | 1299 return -1; |
| 457 | 1300 } |
| 0 | 1301 if (n < 4) { |
| 1302 rl = &rl_table[s->rl_table_index]; | |
| 457 | 1303 if(level > 256*s->y_dc_scale){ |
| 1304 fprintf(stderr, "dc overflow+\n"); | |
| 1305 return -1; | |
| 1306 } | |
| 0 | 1307 } else { |
| 1308 rl = &rl_table[3 + s->rl_chroma_table_index]; | |
| 457 | 1309 if(level > 256*s->c_dc_scale){ |
| 1310 fprintf(stderr, "dc overflow+\n"); | |
| 1311 return -1; | |
| 1312 } | |
| 0 | 1313 } |
| 457 | 1314 block[0] = level; |
| 200 | 1315 |
| 0 | 1316 run_diff = 0; |
| 1317 i = 1; | |
| 1318 if (!coded) { | |
| 1319 goto not_coded; | |
| 1320 } | |
| 1321 if (s->ac_pred) { | |
| 1322 if (dc_pred_dir == 0) | |
| 1323 scan_table = ff_alternate_vertical_scan; /* left */ | |
| 1324 else | |
| 1325 scan_table = ff_alternate_horizontal_scan; /* top */ | |
| 1326 } else { | |
| 1327 scan_table = zigzag_direct; | |
| 1328 } | |
| 1329 set_stat(ST_INTRA_AC); | |
| 1330 } else { | |
| 200 | 1331 qmul = s->qscale << 1; |
| 1332 qadd = (s->qscale - 1) | 1; | |
| 0 | 1333 i = 0; |
| 1334 rl = &rl_table[3 + s->rl_table_index]; | |
| 307 | 1335 |
| 1336 if(s->msmpeg4_version==2) | |
| 1337 run_diff = 0; | |
| 1338 else | |
| 1339 run_diff = 1; | |
| 1340 | |
| 0 | 1341 if (!coded) { |
| 1342 s->block_last_index[n] = i - 1; | |
| 1343 return 0; | |
| 1344 } | |
| 1345 scan_table = zigzag_direct; | |
| 1346 set_stat(ST_INTER_AC); | |
| 1347 } | |
| 1348 | |
| 1349 for(;;) { | |
| 1350 code = get_vlc(&s->gb, &rl->vlc); | |
| 1351 if (code < 0) | |
| 1352 return -1; | |
| 1353 if (code == rl->n) { | |
| 1354 /* escape */ | |
| 457 | 1355 if (s->msmpeg4_version==1 || get_bits1(&s->gb) == 0) { |
| 1356 if (s->msmpeg4_version==1 || get_bits1(&s->gb) == 0) { | |
| 0 | 1357 /* third escape */ |
| 21 | 1358 last = get_bits1(&s->gb); |
| 0 | 1359 run = get_bits(&s->gb, 6); |
| 1360 level = get_bits(&s->gb, 8); | |
| 1361 level = (level << 24) >> 24; /* sign extend */ | |
| 457 | 1362 #if 0 // waste of time / this will detect very few errors |
| 1363 { | |
| 1364 const int abs_level= ABS(level); | |
| 1365 const int run1= run - rl->max_run[last][abs_level] - run_diff; | |
| 1366 if(abs_level<=MAX_LEVEL && run<=MAX_RUN){ | |
| 1367 if(abs_level <= rl->max_level[last][run]){ | |
| 1368 fprintf(stderr, "illegal 3. esc, vlc encoding possible\n"); | |
| 1369 return DECODING_AC_LOST; | |
| 1370 } | |
| 1371 if(abs_level <= rl->max_level[last][run]*2){ | |
| 1372 fprintf(stderr, "illegal 3. esc, esc 1 encoding possible\n"); | |
| 1373 return DECODING_AC_LOST; | |
| 1374 } | |
| 1375 if(abs_level <= rl->max_level[last][run1] && 0){ | |
| 1376 fprintf(stderr, "illegal 3. esc, esc 2 encoding possible\n"); | |
| 1377 return DECODING_AC_LOST; | |
| 1378 } | |
| 1379 } | |
| 1380 } | |
| 1381 #endif | |
| 246 | 1382 //level = level * qmul + (level>0) * qadd - (level<=0) * qadd ; |
| 1383 if (level>0) level= level * qmul + qadd; | |
| 457 | 1384 else level= level * qmul - qadd; |
| 1385 #if 0 // waste of time too :( | |
| 1386 if(level>2048 || level<-2048){ | |
| 1387 fprintf(stderr, "|level| overflow in 3. esc\n"); | |
| 1388 return DECODING_AC_LOST; | |
| 1389 } | |
| 1390 #endif | |
| 0 | 1391 } else { |
| 1392 /* second escape */ | |
| 1393 code = get_vlc(&s->gb, &rl->vlc); | |
| 1394 if (code < 0 || code >= rl->n) | |
| 1395 return -1; | |
| 1396 run = rl->table_run[code]; | |
| 201 | 1397 level = rl->table_level[code]; |
| 0 | 1398 last = code >= rl->last; |
| 1399 run += rl->max_run[last][level] + run_diff; | |
| 201 | 1400 level= level * qmul + qadd; |
| 21 | 1401 if (get_bits1(&s->gb)) |
| 0 | 1402 level = -level; |
| 1403 } | |
| 1404 } else { | |
| 1405 /* first escape */ | |
| 1406 code = get_vlc(&s->gb, &rl->vlc); | |
| 1407 if (code < 0 || code >= rl->n) | |
| 1408 return -1; | |
| 1409 run = rl->table_run[code]; | |
| 1410 level = rl->table_level[code]; | |
| 1411 last = code >= rl->last; | |
| 1412 level += rl->max_level[last][run]; | |
| 200 | 1413 level= level * qmul + qadd; |
| 21 | 1414 if (get_bits1(&s->gb)) |
| 0 | 1415 level = -level; |
| 1416 } | |
| 1417 } else { | |
| 1418 run = rl->table_run[code]; | |
| 200 | 1419 level = rl->table_level[code] * qmul + qadd; |
| 0 | 1420 last = code >= rl->last; |
| 21 | 1421 if (get_bits1(&s->gb)) |
| 0 | 1422 level = -level; |
| 1423 } | |
| 1424 i += run; | |
| 1425 if (i >= 64) | |
| 1426 return -1; | |
| 457 | 1427 |
| 0 | 1428 j = scan_table[i]; |
| 1429 block[j] = level; | |
| 1430 i++; | |
| 1431 if (last) | |
| 1432 break; | |
| 1433 } | |
| 1434 not_coded: | |
| 1435 if (s->mb_intra) { | |
| 1436 mpeg4_pred_ac(s, block, n, dc_pred_dir); | |
| 1437 if (s->ac_pred) { | |
| 1438 i = 64; /* XXX: not optimal */ | |
| 1439 } | |
| 1440 } | |
| 1441 s->block_last_index[n] = i - 1; | |
| 1442 | |
| 1443 return 0; | |
| 1444 } | |
| 1445 | |
| 1446 static int msmpeg4_decode_dc(MpegEncContext * s, int n, int *dir_ptr) | |
| 1447 { | |
| 1448 int level, pred; | |
| 1449 | |
| 457 | 1450 if(s->msmpeg4_version<=2){ |
| 307 | 1451 if (n < 4) { |
| 1452 level = get_vlc(&s->gb, &v2_dc_lum_vlc); | |
| 1453 } else { | |
| 1454 level = get_vlc(&s->gb, &v2_dc_chroma_vlc); | |
| 1455 } | |
| 309 | 1456 if (level < 0) |
| 307 | 1457 return -1; |
| 1458 level-=256; | |
| 1459 }else{ //FIXME optimize use unified tables & index | |
| 1460 if (n < 4) { | |
| 1461 level = get_vlc(&s->gb, &dc_lum_vlc[s->dc_table_index]); | |
| 1462 } else { | |
| 1463 level = get_vlc(&s->gb, &dc_chroma_vlc[s->dc_table_index]); | |
| 1464 } | |
| 457 | 1465 if (level < 0){ |
| 1466 fprintf(stderr, "illegal dc vlc\n"); | |
| 307 | 1467 return -1; |
| 457 | 1468 } |
| 307 | 1469 |
| 1470 if (level == DC_MAX) { | |
| 1471 level = get_bits(&s->gb, 8); | |
| 1472 if (get_bits1(&s->gb)) | |
| 1473 level = -level; | |
| 1474 } else if (level != 0) { | |
| 1475 if (get_bits1(&s->gb)) | |
| 1476 level = -level; | |
| 1477 } | |
| 0 | 1478 } |
| 1479 | |
| 457 | 1480 if(s->msmpeg4_version==1){ |
| 1481 INT32 *dc_val; | |
| 1482 pred = msmpeg4v1_pred_dc(s, n, &dc_val); | |
| 1483 level += pred; | |
| 1484 | |
| 1485 /* update predictor */ | |
| 1486 *dc_val= level; | |
| 1487 }else{ | |
| 1488 INT16 *dc_val; | |
| 1489 pred = msmpeg4_pred_dc(s, n, &dc_val, dir_ptr); | |
| 1490 level += pred; | |
| 0 | 1491 |
| 457 | 1492 /* update predictor */ |
| 1493 if (n < 4) { | |
| 1494 *dc_val = level * s->y_dc_scale; | |
| 1495 } else { | |
| 1496 *dc_val = level * s->c_dc_scale; | |
| 1497 } | |
| 0 | 1498 } |
| 1499 | |
| 1500 return level; | |
| 1501 } | |
| 1502 | |
| 1503 static int msmpeg4_decode_motion(MpegEncContext * s, | |
| 1504 int *mx_ptr, int *my_ptr) | |
| 1505 { | |
| 1506 MVTable *mv; | |
| 1507 int code, mx, my; | |
| 1508 | |
| 1509 mv = &mv_tables[s->mv_table_index]; | |
| 1510 | |
| 1511 code = get_vlc(&s->gb, &mv->vlc); | |
| 1512 if (code < 0) | |
| 1513 return -1; | |
| 1514 if (code == mv->n) { | |
| 1515 mx = get_bits(&s->gb, 6); | |
| 1516 my = get_bits(&s->gb, 6); | |
| 1517 } else { | |
| 1518 mx = mv->table_mvx[code]; | |
| 1519 my = mv->table_mvy[code]; | |
| 1520 } | |
| 1521 | |
| 1522 mx += *mx_ptr - 32; | |
| 1523 my += *my_ptr - 32; | |
| 1524 /* WARNING : they do not do exactly modulo encoding */ | |
| 1525 if (mx <= -64) | |
| 1526 mx += 64; | |
| 1527 else if (mx >= 64) | |
| 1528 mx -= 64; | |
| 1529 | |
| 1530 if (my <= -64) | |
| 1531 my += 64; | |
| 1532 else if (my >= 64) | |
| 1533 my -= 64; | |
| 1534 *mx_ptr = mx; | |
| 1535 *my_ptr = my; | |
| 1536 return 0; | |
| 1537 } |
