Mercurial > libavcodec.hg
annotate msmpeg4.c @ 468:1e23eae32087 libavcodec
Minor warning cleanup.
| author | mellum |
|---|---|
| date | Mon, 03 Jun 2002 11:16:11 +0000 |
| parents | 583dcee270d2 |
| children | 97da217aed7f |
| 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 { | |
| 763 int i; | |
| 764 MVTable *mv; | |
| 765 | |
| 766 for(i=0;i<NB_RL_TABLES;i++) { | |
| 767 init_rl(&rl_table[i]); | |
| 768 init_vlc_rl(&rl_table[i]); | |
| 769 } | |
| 770 for(i=0;i<2;i++) { | |
| 771 mv = &mv_tables[i]; | |
| 772 init_vlc(&mv->vlc, 9, mv->n + 1, | |
| 773 mv->table_mv_bits, 1, 1, | |
| 774 mv->table_mv_code, 2, 2); | |
| 775 } | |
| 776 | |
| 777 init_vlc(&dc_lum_vlc[0], 9, 120, | |
| 778 &table0_dc_lum[0][1], 8, 4, | |
| 779 &table0_dc_lum[0][0], 8, 4); | |
| 780 init_vlc(&dc_chroma_vlc[0], 9, 120, | |
| 781 &table0_dc_chroma[0][1], 8, 4, | |
| 782 &table0_dc_chroma[0][0], 8, 4); | |
| 783 init_vlc(&dc_lum_vlc[1], 9, 120, | |
| 784 &table1_dc_lum[0][1], 8, 4, | |
| 785 &table1_dc_lum[0][0], 8, 4); | |
| 786 init_vlc(&dc_chroma_vlc[1], 9, 120, | |
| 787 &table1_dc_chroma[0][1], 8, 4, | |
| 788 &table1_dc_chroma[0][0], 8, 4); | |
| 307 | 789 |
| 790 init_h263_dc_for_msmpeg4(); | |
| 791 init_vlc(&v2_dc_lum_vlc, 9, 512, | |
| 792 &v2_dc_lum_table[0][1], 8, 4, | |
| 793 &v2_dc_lum_table[0][0], 8, 4); | |
| 794 init_vlc(&v2_dc_chroma_vlc, 9, 512, | |
| 795 &v2_dc_chroma_table[0][1], 8, 4, | |
| 796 &v2_dc_chroma_table[0][0], 8, 4); | |
| 797 | |
| 798 init_vlc(&cbpy_vlc, 6, 16, | |
| 799 &cbpy_tab[0][1], 2, 1, | |
| 800 &cbpy_tab[0][0], 2, 1); | |
| 801 init_vlc(&v2_intra_cbpc_vlc, 3, 4, | |
| 802 &v2_intra_cbpc[0][1], 2, 1, | |
| 803 &v2_intra_cbpc[0][0], 2, 1); | |
| 804 init_vlc(&v2_mb_type_vlc, 5, 8, | |
| 805 &v2_mb_type[0][1], 2, 1, | |
| 806 &v2_mb_type[0][0], 2, 1); | |
| 807 init_vlc(&v2_mv_vlc, 9, 33, | |
| 808 &mvtab[0][1], 2, 1, | |
| 809 &mvtab[0][0], 2, 1); | |
| 0 | 810 |
| 811 init_vlc(&mb_non_intra_vlc, 9, 128, | |
| 812 &table_mb_non_intra[0][1], 8, 4, | |
| 813 &table_mb_non_intra[0][0], 8, 4); | |
| 48 | 814 init_vlc(&mb_intra_vlc, 9, 64, |
| 0 | 815 &table_mb_intra[0][1], 4, 2, |
| 816 &table_mb_intra[0][0], 4, 2); | |
| 457 | 817 |
| 818 init_vlc(&v1_intra_cbpc_vlc, 6, 8, | |
| 819 intra_MCBPC_bits, 1, 1, | |
| 820 intra_MCBPC_code, 1, 1); | |
| 821 init_vlc(&v1_inter_cbpc_vlc, 6, 25, | |
| 822 inter_MCBPC_bits, 1, 1, | |
| 823 inter_MCBPC_code, 1, 1); | |
| 824 | |
| 0 | 825 return 0; |
| 826 } | |
| 827 | |
| 828 static int decode012(GetBitContext *gb) | |
| 829 { | |
| 830 int n; | |
| 21 | 831 n = get_bits1(gb); |
| 0 | 832 if (n == 0) |
| 833 return 0; | |
| 834 else | |
| 21 | 835 return get_bits1(gb) + 1; |
| 0 | 836 } |
| 837 | |
| 307 | 838 int msmpeg4_decode_picture_header(MpegEncContext * s) |
|
300
d874359e58f1
msmpeg4v2 header parser & some dump bits code behind #if 0
michaelni
parents:
299
diff
changeset
|
839 { |
| 311 | 840 int code, code2; |
|
300
d874359e58f1
msmpeg4v2 header parser & some dump bits code behind #if 0
michaelni
parents:
299
diff
changeset
|
841 |
| 311 | 842 #if 0 |
| 843 { | |
| 844 int i; | |
| 845 for(i=0; i<s->gb.size*8; i++) | |
| 846 printf("%d", get_bits1(&s->gb)); | |
| 847 // get_bits1(&s->gb); | |
| 848 printf("END\n"); | |
| 849 return -1; | |
| 850 } | |
| 851 #endif | |
| 457 | 852 |
| 853 if(s->msmpeg4_version==1){ | |
| 854 int start_code, num; | |
| 855 start_code = (get_bits(&s->gb, 16)<<16) | get_bits(&s->gb, 16); | |
| 856 if(start_code!=0x00000100){ | |
| 857 fprintf(stderr, "invalid startcode\n"); | |
| 858 return -1; | |
| 859 } | |
| 860 | |
| 861 num= get_bits(&s->gb, 5); // frame number */ | |
| 862 } | |
| 863 | |
|
300
d874359e58f1
msmpeg4v2 header parser & some dump bits code behind #if 0
michaelni
parents:
299
diff
changeset
|
864 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
|
865 if (s->pict_type != I_TYPE && |
| 457 | 866 s->pict_type != P_TYPE){ |
| 867 fprintf(stderr, "invalid picture type\n"); | |
|
300
d874359e58f1
msmpeg4v2 header parser & some dump bits code behind #if 0
michaelni
parents:
299
diff
changeset
|
868 return -1; |
| 457 | 869 } |
|
300
d874359e58f1
msmpeg4v2 header parser & some dump bits code behind #if 0
michaelni
parents:
299
diff
changeset
|
870 |
|
d874359e58f1
msmpeg4v2 header parser & some dump bits code behind #if 0
michaelni
parents:
299
diff
changeset
|
871 s->qscale = get_bits(&s->gb, 5); |
|
d874359e58f1
msmpeg4v2 header parser & some dump bits code behind #if 0
michaelni
parents:
299
diff
changeset
|
872 |
|
d874359e58f1
msmpeg4v2 header parser & some dump bits code behind #if 0
michaelni
parents:
299
diff
changeset
|
873 if (s->pict_type == I_TYPE) { |
|
d874359e58f1
msmpeg4v2 header parser & some dump bits code behind #if 0
michaelni
parents:
299
diff
changeset
|
874 code = get_bits(&s->gb, 5); |
| 457 | 875 if(s->msmpeg4_version==1){ |
| 876 if(code==0 || code>s->mb_height){ | |
| 877 fprintf(stderr, "invalid slice height %d\n", code); | |
| 878 return -1; | |
| 879 } | |
| 880 | |
| 881 s->slice_height = code; | |
| 882 }else{ | |
| 883 /* 0x17: one slice, 0x18: two slices, ... */ | |
| 884 if (code < 0x17) | |
| 885 return -1; | |
| 886 | |
| 887 s->slice_height = s->mb_height / (code - 0x16); | |
| 888 } | |
| 311 | 889 |
| 890 switch(s->msmpeg4_version){ | |
| 457 | 891 case 1: |
| 311 | 892 case 2: |
| 307 | 893 s->rl_chroma_table_index = 2; |
| 894 s->rl_table_index = 2; | |
|
300
d874359e58f1
msmpeg4v2 header parser & some dump bits code behind #if 0
michaelni
parents:
299
diff
changeset
|
895 |
| 307 | 896 s->dc_table_index = 0; //not used |
| 311 | 897 break; |
| 898 case 3: | |
| 307 | 899 s->rl_chroma_table_index = decode012(&s->gb); |
| 900 s->rl_table_index = decode012(&s->gb); | |
| 0 | 901 |
| 307 | 902 s->dc_table_index = get_bits1(&s->gb); |
| 311 | 903 break; |
| 904 case 4: | |
| 905 msmpeg4_decode_ext_header(s, 999 /* bufer size (useless here) */); | |
| 906 printf("%X\n", show_bits(&s->gb, 24)); | |
| 907 code= get_bits(&s->gb, 2); | |
| 908 if(code==1){ | |
| 909 code2= get_bits(&s->gb, 3); | |
| 910 if(code2==7) skip_bits(&s->gb, 1); | |
| 911 } | |
| 912 printf("%X\n", show_bits(&s->gb, 24)); | |
| 913 s->rl_chroma_table_index = 2; | |
| 914 s->rl_table_index = 2; | |
| 915 | |
| 916 s->dc_table_index = 0; | |
| 917 break; | |
| 307 | 918 } |
| 0 | 919 s->no_rounding = 1; |
| 200 | 920 /* printf(" %d %d %d %d \n", |
| 921 s->qscale, | |
| 922 s->rl_chroma_table_index, | |
| 923 s->rl_table_index, | |
| 924 s->dc_table_index);*/ | |
| 0 | 925 } else { |
| 926 | |
| 457 | 927 switch(s->msmpeg4_version){ |
| 928 case 1: | |
| 929 case 2: | |
| 930 if(s->msmpeg4_version==1) | |
| 931 s->use_skip_mb_code = 1; | |
| 932 else | |
| 933 s->use_skip_mb_code = get_bits1(&s->gb); | |
| 307 | 934 s->rl_table_index = 2; |
| 935 s->rl_chroma_table_index = s->rl_table_index; | |
| 936 s->dc_table_index = 0; //not used | |
| 937 s->mv_table_index = 0; | |
| 457 | 938 break; |
| 939 case 3: | |
| 940 s->use_skip_mb_code = get_bits1(&s->gb); | |
| 307 | 941 s->rl_table_index = decode012(&s->gb); |
| 942 s->rl_chroma_table_index = s->rl_table_index; | |
| 0 | 943 |
| 307 | 944 s->dc_table_index = get_bits1(&s->gb); |
| 945 | |
| 946 s->mv_table_index = get_bits1(&s->gb); | |
| 457 | 947 break; |
| 307 | 948 } |
| 200 | 949 /* printf(" %d %d %d %d %d \n", |
| 950 s->use_skip_mb_code, | |
| 951 s->rl_table_index, | |
| 952 s->rl_chroma_table_index, | |
| 953 s->dc_table_index, | |
| 954 s->mv_table_index);*/ | |
| 208 | 955 if(s->flipflop_rounding){ |
| 956 s->no_rounding ^= 1; | |
| 957 }else{ | |
| 958 s->no_rounding = 0; | |
| 959 } | |
| 960 // printf("%d", s->no_rounding); | |
| 311 | 961 //return -1; |
| 0 | 962 } |
| 208 | 963 |
| 307 | 964 #if 0 |
| 965 if(s->msmpeg4_version==2) | |
| 966 { | |
| 967 int i; | |
| 968 for(i=0; i<s->gb.size*8; i++) | |
| 969 // printf("%d", get_bits1(&s->gb)); | |
| 970 get_bits1(&s->gb); | |
| 971 printf("END\n"); | |
| 972 return -1; | |
| 973 } | |
| 974 #endif | |
| 975 | |
| 0 | 976 #ifdef DEBUG |
| 977 printf("*****frame %d:\n", frame_count++); | |
| 978 #endif | |
| 979 return 0; | |
| 980 } | |
| 981 | |
| 208 | 982 int msmpeg4_decode_ext_header(MpegEncContext * s, int buf_size) |
| 983 { | |
| 457 | 984 int left= buf_size*8 - get_bits_count(&s->gb); |
| 985 int length= s->msmpeg4_version>=3 ? 17 : 16; | |
| 208 | 986 /* the alt_bitstream reader could read over the end so we need to check it */ |
| 457 | 987 if(left>=length && left<length+8) |
| 208 | 988 { |
|
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
|
989 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
|
990 |
|
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
|
991 fps= get_bits(&s->gb, 5); |
| 457 | 992 s->bit_rate= get_bits(&s->gb, 11); |
| 993 if(s->msmpeg4_version>=3) | |
| 994 s->flipflop_rounding= get_bits1(&s->gb); | |
| 995 else | |
| 996 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
|
997 |
| 457 | 998 // printf("fps:%2d bps:%2d roundingType:%1d\n", fps, s->bit_rate, s->flipflop_rounding); |
| 999 } | |
| 1000 else if(left<length+8) | |
| 1001 { | |
| 1002 s->flipflop_rounding= 0; | |
| 1003 printf("ext header missing, %d left\n", left); | |
| 208 | 1004 } |
| 1005 else | |
| 1006 { | |
| 457 | 1007 fprintf(stderr, "I frame too long, ignoring ext header\n"); |
| 208 | 1008 } |
|
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
|
1009 |
| 208 | 1010 return 0; |
| 1011 } | |
| 1012 | |
|
440
000aeeac27a2
* started to cleanup name clashes for onetime compilation
kabi
parents:
429
diff
changeset
|
1013 static inline void msmpeg4_memsetw(short *tab, int val, int n) |
| 0 | 1014 { |
| 1015 int i; | |
| 1016 for(i=0;i<n;i++) | |
| 1017 tab[i] = val; | |
| 1018 } | |
| 1019 | |
| 310 | 1020 static void msmpeg4v2_encode_motion(MpegEncContext * s, int val) |
| 1021 { | |
| 1022 int range, bit_size, sign, code, bits; | |
| 1023 | |
| 1024 if (val == 0) { | |
| 1025 /* zero vector */ | |
| 1026 code = 0; | |
| 1027 put_bits(&s->pb, mvtab[code][1], mvtab[code][0]); | |
| 1028 } else { | |
| 1029 bit_size = s->f_code - 1; | |
| 1030 range = 1 << bit_size; | |
| 1031 if (val <= -64) | |
| 1032 val += 64; | |
| 1033 else if (val >= 64) | |
| 1034 val -= 64; | |
| 1035 | |
| 1036 if (val >= 0) { | |
| 1037 sign = 0; | |
| 1038 } else { | |
| 1039 val = -val; | |
| 1040 sign = 1; | |
| 1041 } | |
| 1042 val--; | |
| 1043 code = (val >> bit_size) + 1; | |
| 1044 bits = val & (range - 1); | |
| 1045 | |
| 1046 put_bits(&s->pb, mvtab[code][1] + 1, (mvtab[code][0] << 1) | sign); | |
| 1047 if (bit_size > 0) { | |
| 1048 put_bits(&s->pb, bit_size, bits); | |
| 1049 } | |
| 1050 } | |
| 1051 } | |
| 1052 | |
| 307 | 1053 /* this is identical to h263 except that its range is multiplied by 2 */ |
| 1054 static int msmpeg4v2_decode_motion(MpegEncContext * s, int pred, int f_code) | |
| 1055 { | |
| 1056 int code, val, sign, shift; | |
| 1057 | |
| 1058 code = get_vlc(&s->gb, &v2_mv_vlc); | |
| 457 | 1059 // printf("MV code %d at %d %d pred: %d\n", code, s->mb_x,s->mb_y, pred); |
| 307 | 1060 if (code < 0) |
| 1061 return 0xffff; | |
| 1062 | |
| 1063 if (code == 0) | |
| 1064 return pred; | |
| 1065 sign = get_bits1(&s->gb); | |
| 1066 shift = f_code - 1; | |
| 1067 val = (code - 1) << shift; | |
| 1068 if (shift > 0) | |
| 1069 val |= get_bits(&s->gb, shift); | |
| 1070 val++; | |
| 1071 if (sign) | |
| 1072 val = -val; | |
| 457 | 1073 |
| 307 | 1074 val += pred; |
| 1075 if (val <= -64) | |
| 1076 val += 64; | |
| 1077 else if (val >= 64) | |
| 1078 val -= 64; | |
| 1079 | |
| 1080 return val; | |
| 1081 } | |
| 1082 | |
| 1083 | |
| 457 | 1084 static int msmpeg4v12_decode_mb(MpegEncContext *s, |
| 307 | 1085 DCTELEM block[6][64]) |
| 1086 { | |
| 1087 int cbp, code, i; | |
| 1088 if (s->pict_type == P_TYPE) { | |
| 1089 if (s->use_skip_mb_code) { | |
| 1090 if (get_bits1(&s->gb)) { | |
| 1091 /* skip mb */ | |
| 1092 s->mb_intra = 0; | |
| 1093 for(i=0;i<6;i++) | |
| 1094 s->block_last_index[i] = -1; | |
| 1095 s->mv_dir = MV_DIR_FORWARD; | |
| 1096 s->mv_type = MV_TYPE_16X16; | |
| 1097 s->mv[0][0][0] = 0; | |
| 1098 s->mv[0][0][1] = 0; | |
| 1099 s->mb_skiped = 1; | |
| 1100 return 0; | |
| 1101 } | |
| 1102 } | |
| 1103 | |
| 457 | 1104 if(s->msmpeg4_version==2) |
| 1105 code = get_vlc(&s->gb, &v2_mb_type_vlc); | |
| 1106 else | |
| 1107 code = get_vlc(&s->gb, &v1_inter_cbpc_vlc); | |
| 1108 if(code<0 || code>7){ | |
| 1109 fprintf(stderr, "cbpc %d invalid at %d %d\n", code, s->mb_x, s->mb_y); | |
| 1110 return -1; | |
| 1111 } | |
| 1112 | |
| 307 | 1113 s->mb_intra = code >>2; |
| 1114 | |
| 1115 cbp = code & 0x3; | |
| 1116 } else { | |
| 1117 s->mb_intra = 1; | |
| 457 | 1118 if(s->msmpeg4_version==2) |
| 1119 cbp= get_vlc(&s->gb, &v2_intra_cbpc_vlc); | |
| 1120 else | |
| 1121 cbp= get_vlc(&s->gb, &v1_intra_cbpc_vlc); | |
| 1122 if(cbp<0 || cbp>3){ | |
| 1123 fprintf(stderr, "cbpc %d invalid at %d %d\n", cbp, s->mb_x, s->mb_y); | |
| 1124 return -1; | |
| 1125 } | |
| 307 | 1126 } |
| 1127 | |
| 1128 if (!s->mb_intra) { | |
| 457 | 1129 int mx, my, cbpy; |
| 1130 | |
| 1131 cbpy= get_vlc(&s->gb, &cbpy_vlc); | |
| 1132 if(cbpy<0){ | |
| 1133 fprintf(stderr, "cbpy %d invalid at %d %d\n", cbp, s->mb_x, s->mb_y); | |
| 1134 return -1; | |
| 1135 } | |
| 307 | 1136 |
| 457 | 1137 cbp|= cbpy<<2; |
| 1138 if(s->msmpeg4_version==1 || (cbp&3) != 3) cbp^= 0x3C; | |
| 307 | 1139 |
| 1140 h263_pred_motion(s, 0, &mx, &my); | |
| 1141 mx= msmpeg4v2_decode_motion(s, mx, 1); | |
| 1142 my= msmpeg4v2_decode_motion(s, my, 1); | |
| 1143 | |
| 1144 s->mv_dir = MV_DIR_FORWARD; | |
| 1145 s->mv_type = MV_TYPE_16X16; | |
| 1146 s->mv[0][0][0] = mx; | |
| 1147 s->mv[0][0][1] = my; | |
| 1148 } else { | |
| 457 | 1149 if(s->msmpeg4_version==2){ |
| 1150 s->ac_pred = get_bits1(&s->gb); | |
| 1151 cbp|= get_vlc(&s->gb, &cbpy_vlc)<<2; //FIXME check errors | |
| 1152 } else{ | |
| 1153 s->ac_pred = 0; | |
| 1154 cbp|= get_vlc(&s->gb, &cbpy_vlc)<<2; //FIXME check errors | |
| 1155 if(s->pict_type==P_TYPE) cbp^=0x3C; | |
| 1156 } | |
| 307 | 1157 } |
| 1158 | |
| 1159 for (i = 0; i < 6; i++) { | |
| 1160 if (msmpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1) < 0) | |
| 1161 { | |
| 457 | 1162 fprintf(stderr,"\nerror while decoding block: %d x %d (%d)\n", s->mb_x, s->mb_y, i); |
| 307 | 1163 return -1; |
| 1164 } | |
| 1165 } | |
| 1166 return 0; | |
| 1167 } | |
| 1168 | |
| 0 | 1169 int msmpeg4_decode_mb(MpegEncContext *s, |
| 1170 DCTELEM block[6][64]) | |
| 1171 { | |
| 1172 int cbp, code, i; | |
| 1173 UINT8 *coded_val; | |
| 1174 | |
| 1175 /* special slice handling */ | |
| 1176 if (s->mb_x == 0) { | |
| 122 | 1177 if (s->slice_height && (s->mb_y % s->slice_height) == 0) { |
| 0 | 1178 int wrap; |
| 1179 /* reset DC pred (set previous line to 1024) */ | |
| 1180 wrap = 2 * s->mb_width + 2; | |
|
440
000aeeac27a2
* started to cleanup name clashes for onetime compilation
kabi
parents:
429
diff
changeset
|
1181 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
|
1182 1024, 2 * s->mb_width); |
|
000aeeac27a2
* started to cleanup name clashes for onetime compilation
kabi
parents:
429
diff
changeset
|
1183 wrap = s->mb_width + 2; |
|
000aeeac27a2
* started to cleanup name clashes for onetime compilation
kabi
parents:
429
diff
changeset
|
1184 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
|
1185 1024, s->mb_width); |
|
000aeeac27a2
* started to cleanup name clashes for onetime compilation
kabi
parents:
429
diff
changeset
|
1186 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
|
1187 1024, s->mb_width); |
|
000aeeac27a2
* started to cleanup name clashes for onetime compilation
kabi
parents:
429
diff
changeset
|
1188 |
|
000aeeac27a2
* started to cleanup name clashes for onetime compilation
kabi
parents:
429
diff
changeset
|
1189 /* reset AC pred (set previous line to 0) */ |
|
000aeeac27a2
* started to cleanup name clashes for onetime compilation
kabi
parents:
429
diff
changeset
|
1190 wrap = s->mb_width * 2 + 2; |
|
000aeeac27a2
* started to cleanup name clashes for onetime compilation
kabi
parents:
429
diff
changeset
|
1191 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
|
1192 0, 2 * s->mb_width*16); |
|
000aeeac27a2
* started to cleanup name clashes for onetime compilation
kabi
parents:
429
diff
changeset
|
1193 wrap = s->mb_width + 2; |
|
000aeeac27a2
* started to cleanup name clashes for onetime compilation
kabi
parents:
429
diff
changeset
|
1194 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
|
1195 0, s->mb_width*16); |
|
000aeeac27a2
* started to cleanup name clashes for onetime compilation
kabi
parents:
429
diff
changeset
|
1196 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
|
1197 0, s->mb_width*16); |
| 0 | 1198 |
| 1199 s->first_slice_line = 1; | |
| 1200 } else { | |
| 1201 s->first_slice_line = 0; | |
| 1202 } | |
| 1203 } | |
| 1204 | |
| 457 | 1205 if(s->msmpeg4_version<=2) return msmpeg4v12_decode_mb(s, block); //FIXME export function & call from outside perhaps |
| 307 | 1206 |
| 0 | 1207 if (s->pict_type == P_TYPE) { |
| 1208 set_stat(ST_INTER_MB); | |
| 1209 if (s->use_skip_mb_code) { | |
| 21 | 1210 if (get_bits1(&s->gb)) { |
| 0 | 1211 /* skip mb */ |
| 1212 s->mb_intra = 0; | |
| 1213 for(i=0;i<6;i++) | |
| 1214 s->block_last_index[i] = -1; | |
| 1215 s->mv_dir = MV_DIR_FORWARD; | |
| 1216 s->mv_type = MV_TYPE_16X16; | |
| 1217 s->mv[0][0][0] = 0; | |
| 1218 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
|
1219 s->mb_skiped = 1; |
| 0 | 1220 return 0; |
| 1221 } | |
| 1222 } | |
| 1223 | |
| 1224 code = get_vlc(&s->gb, &mb_non_intra_vlc); | |
| 1225 if (code < 0) | |
| 1226 return -1; | |
| 246 | 1227 //s->mb_intra = (code & 0x40) ? 0 : 1; |
| 1228 s->mb_intra = (~code & 0x40) >> 6; | |
| 0 | 1229 |
| 1230 cbp = code & 0x3f; | |
| 1231 } else { | |
| 1232 set_stat(ST_INTRA_MB); | |
| 1233 s->mb_intra = 1; | |
| 1234 code = get_vlc(&s->gb, &mb_intra_vlc); | |
| 1235 if (code < 0) | |
| 1236 return -1; | |
| 1237 /* predict coded block pattern */ | |
| 1238 cbp = 0; | |
| 1239 for(i=0;i<6;i++) { | |
| 246 | 1240 int val = ((code >> (5 - i)) & 1); |
| 0 | 1241 if (i < 4) { |
| 246 | 1242 int pred = coded_block_pred(s, i, &coded_val); |
| 0 | 1243 val = val ^ pred; |
| 1244 *coded_val = val; | |
| 1245 } | |
| 1246 cbp |= val << (5 - i); | |
| 1247 } | |
| 1248 } | |
| 1249 | |
| 1250 if (!s->mb_intra) { | |
| 1251 int mx, my; | |
| 1252 set_stat(ST_MV); | |
| 1253 h263_pred_motion(s, 0, &mx, &my); | |
| 1254 if (msmpeg4_decode_motion(s, &mx, &my) < 0) | |
| 1255 return -1; | |
| 1256 s->mv_dir = MV_DIR_FORWARD; | |
| 1257 s->mv_type = MV_TYPE_16X16; | |
| 1258 s->mv[0][0][0] = mx; | |
| 1259 s->mv[0][0][1] = my; | |
| 1260 } else { | |
| 1261 set_stat(ST_INTRA_MB); | |
| 21 | 1262 s->ac_pred = get_bits1(&s->gb); |
| 0 | 1263 } |
| 1264 | |
| 1265 for (i = 0; i < 6; i++) { | |
| 1266 if (msmpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1) < 0) | |
| 246 | 1267 { |
| 457 | 1268 fprintf(stderr,"\nerror while decoding block: %d x %d (%d)\n", s->mb_x, s->mb_y, i); |
| 1269 return -1; | |
| 246 | 1270 } |
| 0 | 1271 } |
| 457 | 1272 |
| 0 | 1273 return 0; |
| 1274 } | |
| 1275 | |
| 1276 static int msmpeg4_decode_block(MpegEncContext * s, DCTELEM * block, | |
| 1277 int n, int coded) | |
| 1278 { | |
| 1279 int code, level, i, j, last, run, run_diff; | |
| 1280 int dc_pred_dir; | |
| 1281 RLTable *rl; | |
| 1282 const UINT8 *scan_table; | |
| 200 | 1283 int qmul, qadd; |
| 0 | 1284 |
| 1285 if (s->mb_intra) { | |
| 200 | 1286 qmul=1; |
| 1287 qadd=0; | |
| 1288 | |
| 0 | 1289 /* DC coef */ |
| 1290 set_stat(ST_DC); | |
| 1291 level = msmpeg4_decode_dc(s, n, &dc_pred_dir); | |
| 457 | 1292 if (level < 0){ |
| 1293 fprintf(stderr, "dc overflow-\n"); | |
| 0 | 1294 return -1; |
| 457 | 1295 } |
| 0 | 1296 if (n < 4) { |
| 1297 rl = &rl_table[s->rl_table_index]; | |
| 457 | 1298 if(level > 256*s->y_dc_scale){ |
| 1299 fprintf(stderr, "dc overflow+\n"); | |
| 1300 return -1; | |
| 1301 } | |
| 0 | 1302 } else { |
| 1303 rl = &rl_table[3 + s->rl_chroma_table_index]; | |
| 457 | 1304 if(level > 256*s->c_dc_scale){ |
| 1305 fprintf(stderr, "dc overflow+\n"); | |
| 1306 return -1; | |
| 1307 } | |
| 0 | 1308 } |
| 457 | 1309 block[0] = level; |
| 200 | 1310 |
| 0 | 1311 run_diff = 0; |
| 1312 i = 1; | |
| 1313 if (!coded) { | |
| 1314 goto not_coded; | |
| 1315 } | |
| 1316 if (s->ac_pred) { | |
| 1317 if (dc_pred_dir == 0) | |
| 1318 scan_table = ff_alternate_vertical_scan; /* left */ | |
| 1319 else | |
| 1320 scan_table = ff_alternate_horizontal_scan; /* top */ | |
| 1321 } else { | |
| 1322 scan_table = zigzag_direct; | |
| 1323 } | |
| 1324 set_stat(ST_INTRA_AC); | |
| 1325 } else { | |
| 200 | 1326 qmul = s->qscale << 1; |
| 1327 qadd = (s->qscale - 1) | 1; | |
| 0 | 1328 i = 0; |
| 1329 rl = &rl_table[3 + s->rl_table_index]; | |
| 307 | 1330 |
| 1331 if(s->msmpeg4_version==2) | |
| 1332 run_diff = 0; | |
| 1333 else | |
| 1334 run_diff = 1; | |
| 1335 | |
| 0 | 1336 if (!coded) { |
| 1337 s->block_last_index[n] = i - 1; | |
| 1338 return 0; | |
| 1339 } | |
| 1340 scan_table = zigzag_direct; | |
| 1341 set_stat(ST_INTER_AC); | |
| 1342 } | |
| 1343 | |
| 1344 for(;;) { | |
| 1345 code = get_vlc(&s->gb, &rl->vlc); | |
| 1346 if (code < 0) | |
| 1347 return -1; | |
| 1348 if (code == rl->n) { | |
| 1349 /* escape */ | |
| 457 | 1350 if (s->msmpeg4_version==1 || get_bits1(&s->gb) == 0) { |
| 1351 if (s->msmpeg4_version==1 || get_bits1(&s->gb) == 0) { | |
| 0 | 1352 /* third escape */ |
| 21 | 1353 last = get_bits1(&s->gb); |
| 0 | 1354 run = get_bits(&s->gb, 6); |
| 1355 level = get_bits(&s->gb, 8); | |
| 1356 level = (level << 24) >> 24; /* sign extend */ | |
| 457 | 1357 #if 0 // waste of time / this will detect very few errors |
| 1358 { | |
| 1359 const int abs_level= ABS(level); | |
| 1360 const int run1= run - rl->max_run[last][abs_level] - run_diff; | |
| 1361 if(abs_level<=MAX_LEVEL && run<=MAX_RUN){ | |
| 1362 if(abs_level <= rl->max_level[last][run]){ | |
| 1363 fprintf(stderr, "illegal 3. esc, vlc encoding possible\n"); | |
| 1364 return DECODING_AC_LOST; | |
| 1365 } | |
| 1366 if(abs_level <= rl->max_level[last][run]*2){ | |
| 1367 fprintf(stderr, "illegal 3. esc, esc 1 encoding possible\n"); | |
| 1368 return DECODING_AC_LOST; | |
| 1369 } | |
| 1370 if(abs_level <= rl->max_level[last][run1] && 0){ | |
| 1371 fprintf(stderr, "illegal 3. esc, esc 2 encoding possible\n"); | |
| 1372 return DECODING_AC_LOST; | |
| 1373 } | |
| 1374 } | |
| 1375 } | |
| 1376 #endif | |
| 246 | 1377 //level = level * qmul + (level>0) * qadd - (level<=0) * qadd ; |
| 1378 if (level>0) level= level * qmul + qadd; | |
| 457 | 1379 else level= level * qmul - qadd; |
| 1380 #if 0 // waste of time too :( | |
| 1381 if(level>2048 || level<-2048){ | |
| 1382 fprintf(stderr, "|level| overflow in 3. esc\n"); | |
| 1383 return DECODING_AC_LOST; | |
| 1384 } | |
| 1385 #endif | |
| 0 | 1386 } else { |
| 1387 /* second escape */ | |
| 1388 code = get_vlc(&s->gb, &rl->vlc); | |
| 1389 if (code < 0 || code >= rl->n) | |
| 1390 return -1; | |
| 1391 run = rl->table_run[code]; | |
| 201 | 1392 level = rl->table_level[code]; |
| 0 | 1393 last = code >= rl->last; |
| 1394 run += rl->max_run[last][level] + run_diff; | |
| 201 | 1395 level= level * qmul + qadd; |
| 21 | 1396 if (get_bits1(&s->gb)) |
| 0 | 1397 level = -level; |
| 1398 } | |
| 1399 } else { | |
| 1400 /* first escape */ | |
| 1401 code = get_vlc(&s->gb, &rl->vlc); | |
| 1402 if (code < 0 || code >= rl->n) | |
| 1403 return -1; | |
| 1404 run = rl->table_run[code]; | |
| 1405 level = rl->table_level[code]; | |
| 1406 last = code >= rl->last; | |
| 1407 level += rl->max_level[last][run]; | |
| 200 | 1408 level= level * qmul + qadd; |
| 21 | 1409 if (get_bits1(&s->gb)) |
| 0 | 1410 level = -level; |
| 1411 } | |
| 1412 } else { | |
| 1413 run = rl->table_run[code]; | |
| 200 | 1414 level = rl->table_level[code] * qmul + qadd; |
| 0 | 1415 last = code >= rl->last; |
| 21 | 1416 if (get_bits1(&s->gb)) |
| 0 | 1417 level = -level; |
| 1418 } | |
| 1419 i += run; | |
| 1420 if (i >= 64) | |
| 1421 return -1; | |
| 457 | 1422 |
| 0 | 1423 j = scan_table[i]; |
| 1424 block[j] = level; | |
| 1425 i++; | |
| 1426 if (last) | |
| 1427 break; | |
| 1428 } | |
| 1429 not_coded: | |
| 1430 if (s->mb_intra) { | |
| 1431 mpeg4_pred_ac(s, block, n, dc_pred_dir); | |
| 1432 if (s->ac_pred) { | |
| 1433 i = 64; /* XXX: not optimal */ | |
| 1434 } | |
| 1435 } | |
| 1436 s->block_last_index[n] = i - 1; | |
| 1437 | |
| 1438 return 0; | |
| 1439 } | |
| 1440 | |
| 1441 static int msmpeg4_decode_dc(MpegEncContext * s, int n, int *dir_ptr) | |
| 1442 { | |
| 1443 int level, pred; | |
| 1444 | |
| 457 | 1445 if(s->msmpeg4_version<=2){ |
| 307 | 1446 if (n < 4) { |
| 1447 level = get_vlc(&s->gb, &v2_dc_lum_vlc); | |
| 1448 } else { | |
| 1449 level = get_vlc(&s->gb, &v2_dc_chroma_vlc); | |
| 1450 } | |
| 309 | 1451 if (level < 0) |
| 307 | 1452 return -1; |
| 1453 level-=256; | |
| 1454 }else{ //FIXME optimize use unified tables & index | |
| 1455 if (n < 4) { | |
| 1456 level = get_vlc(&s->gb, &dc_lum_vlc[s->dc_table_index]); | |
| 1457 } else { | |
| 1458 level = get_vlc(&s->gb, &dc_chroma_vlc[s->dc_table_index]); | |
| 1459 } | |
| 457 | 1460 if (level < 0){ |
| 1461 fprintf(stderr, "illegal dc vlc\n"); | |
| 307 | 1462 return -1; |
| 457 | 1463 } |
| 307 | 1464 |
| 1465 if (level == DC_MAX) { | |
| 1466 level = get_bits(&s->gb, 8); | |
| 1467 if (get_bits1(&s->gb)) | |
| 1468 level = -level; | |
| 1469 } else if (level != 0) { | |
| 1470 if (get_bits1(&s->gb)) | |
| 1471 level = -level; | |
| 1472 } | |
| 0 | 1473 } |
| 1474 | |
| 457 | 1475 if(s->msmpeg4_version==1){ |
| 1476 INT32 *dc_val; | |
| 1477 pred = msmpeg4v1_pred_dc(s, n, &dc_val); | |
| 1478 level += pred; | |
| 1479 | |
| 1480 /* update predictor */ | |
| 1481 *dc_val= level; | |
| 1482 }else{ | |
| 1483 INT16 *dc_val; | |
| 1484 pred = msmpeg4_pred_dc(s, n, &dc_val, dir_ptr); | |
| 1485 level += pred; | |
| 0 | 1486 |
| 457 | 1487 /* update predictor */ |
| 1488 if (n < 4) { | |
| 1489 *dc_val = level * s->y_dc_scale; | |
| 1490 } else { | |
| 1491 *dc_val = level * s->c_dc_scale; | |
| 1492 } | |
| 0 | 1493 } |
| 1494 | |
| 1495 return level; | |
| 1496 } | |
| 1497 | |
| 1498 static int msmpeg4_decode_motion(MpegEncContext * s, | |
| 1499 int *mx_ptr, int *my_ptr) | |
| 1500 { | |
| 1501 MVTable *mv; | |
| 1502 int code, mx, my; | |
| 1503 | |
| 1504 mv = &mv_tables[s->mv_table_index]; | |
| 1505 | |
| 1506 code = get_vlc(&s->gb, &mv->vlc); | |
| 1507 if (code < 0) | |
| 1508 return -1; | |
| 1509 if (code == mv->n) { | |
| 1510 mx = get_bits(&s->gb, 6); | |
| 1511 my = get_bits(&s->gb, 6); | |
| 1512 } else { | |
| 1513 mx = mv->table_mvx[code]; | |
| 1514 my = mv->table_mvy[code]; | |
| 1515 } | |
| 1516 | |
| 1517 mx += *mx_ptr - 32; | |
| 1518 my += *my_ptr - 32; | |
| 1519 /* WARNING : they do not do exactly modulo encoding */ | |
| 1520 if (mx <= -64) | |
| 1521 mx += 64; | |
| 1522 else if (mx >= 64) | |
| 1523 mx -= 64; | |
| 1524 | |
| 1525 if (my <= -64) | |
| 1526 my += 64; | |
| 1527 else if (my >= 64) | |
| 1528 my -= 64; | |
| 1529 *mx_ptr = mx; | |
| 1530 *my_ptr = my; | |
| 1531 return 0; | |
| 1532 } |
