Mercurial > libavcodec.hg
annotate msmpeg4.c @ 309:32485ad4cc4e libavcodec
fixing msmpeg4v2 bugs (is bugfree now afaik)
| author | michaelni |
|---|---|
| date | Fri, 05 Apr 2002 16:51:12 +0000 |
| parents | 764aeec1320e |
| children | 49e73229a5e6 |
| rev | line source |
|---|---|
| 0 | 1 /* |
| 2 * MSMPEG4 backend for ffmpeg encoder and decoder | |
| 3 * Copyright (c) 2001 Gerard Lantau. | |
| 4 * | |
| 5 * This program is free software; you can redistribute it and/or modify | |
| 6 * it under the terms of the GNU General Public License as published by | |
| 7 * the Free Software Foundation; either version 2 of the License, or | |
| 8 * (at your option) any later version. | |
| 9 * | |
| 10 * This program is distributed in the hope that it will be useful, | |
| 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 13 * GNU General Public License for more details. | |
| 14 * | |
| 15 * You should have received a copy of the GNU General Public License | |
| 16 * along with this program; if not, write to the Free Software | |
| 17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
| 18 */ | |
| 19 #include <stdlib.h> | |
| 20 #include <stdio.h> | |
| 21 #include "common.h" | |
| 22 #include "dsputil.h" | |
| 23 #include "mpegvideo.h" | |
|
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
|
24 #include "avcodec.h" |
| 0 | 25 |
| 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); | |
| 55 | |
|
225
ae145876789d
use multiply instead of divides for DC prediction on X86
michaelni
parents:
221
diff
changeset
|
56 extern UINT32 inverse[256]; |
|
ae145876789d
use multiply instead of divides for DC prediction on X86
michaelni
parents:
221
diff
changeset
|
57 |
| 0 | 58 #ifdef DEBUG |
| 59 int intra_count = 0; | |
| 60 int frame_count = 0; | |
| 61 #endif | |
| 62 /* XXX: move it to mpegvideo.h */ | |
| 63 | |
| 64 static int init_done = 0; | |
| 65 | |
| 66 #include "msmpeg4data.h" | |
| 67 | |
| 68 #ifdef STATS | |
| 69 | |
| 70 const char *st_names[ST_NB] = { | |
| 71 "unknown", | |
| 72 "dc", | |
| 73 "intra_ac", | |
| 74 "inter_ac", | |
| 75 "intra_mb", | |
| 76 "inter_mb", | |
| 77 "mv", | |
| 78 }; | |
| 79 | |
| 80 int st_current_index = 0; | |
| 81 unsigned int st_bit_counts[ST_NB]; | |
| 82 unsigned int st_out_bit_counts[ST_NB]; | |
| 83 | |
| 84 #define set_stat(var) st_current_index = var; | |
| 85 | |
| 86 void print_stats(void) | |
| 87 { | |
| 88 unsigned int total; | |
| 89 int i; | |
| 90 | |
| 91 printf("Input:\n"); | |
| 92 total = 0; | |
| 93 for(i=0;i<ST_NB;i++) | |
| 94 total += st_bit_counts[i]; | |
| 95 if (total == 0) | |
| 96 total = 1; | |
| 97 for(i=0;i<ST_NB;i++) { | |
| 98 printf("%-10s : %10.1f %5.1f%%\n", | |
| 99 st_names[i], | |
| 100 (double)st_bit_counts[i] / 8.0, | |
| 101 (double)st_bit_counts[i] * 100.0 / total); | |
| 102 } | |
| 103 printf("%-10s : %10.1f %5.1f%%\n", | |
| 104 "total", | |
| 105 (double)total / 8.0, | |
| 106 100.0); | |
| 107 | |
| 108 printf("Output:\n"); | |
| 109 total = 0; | |
| 110 for(i=0;i<ST_NB;i++) | |
| 111 total += st_out_bit_counts[i]; | |
| 112 if (total == 0) | |
| 113 total = 1; | |
| 114 for(i=0;i<ST_NB;i++) { | |
| 115 printf("%-10s : %10.1f %5.1f%%\n", | |
| 116 st_names[i], | |
| 117 (double)st_out_bit_counts[i] / 8.0, | |
| 118 (double)st_out_bit_counts[i] * 100.0 / total); | |
| 119 } | |
| 120 printf("%-10s : %10.1f %5.1f%%\n", | |
| 121 "total", | |
| 122 (double)total / 8.0, | |
| 123 100.0); | |
| 124 } | |
| 125 | |
| 126 #else | |
| 127 | |
| 128 #define set_stat(var) | |
| 129 | |
| 130 #endif | |
| 131 | |
| 132 /* build the table which associate a (x,y) motion vector to a vlc */ | |
| 133 static void init_mv_table(MVTable *tab) | |
| 134 { | |
| 135 int i, x, y; | |
| 136 | |
| 137 tab->table_mv_index = malloc(sizeof(UINT16) * 4096); | |
| 138 /* mark all entries as not used */ | |
| 139 for(i=0;i<4096;i++) | |
| 140 tab->table_mv_index[i] = tab->n; | |
| 141 | |
| 142 for(i=0;i<tab->n;i++) { | |
| 143 x = tab->table_mvx[i]; | |
| 144 y = tab->table_mvy[i]; | |
| 145 tab->table_mv_index[(x << 6) | y] = i; | |
| 146 } | |
| 147 } | |
| 148 | |
| 149 static void code012(PutBitContext *pb, int n) | |
| 150 { | |
| 151 if (n == 0) { | |
| 152 put_bits(pb, 1, 0); | |
| 153 } else { | |
| 154 put_bits(pb, 1, 1); | |
| 155 put_bits(pb, 1, (n >= 2)); | |
| 156 } | |
| 157 } | |
| 158 | |
| 159 /* write MSMPEG4 V3 compatible frame header */ | |
| 160 void msmpeg4_encode_picture_header(MpegEncContext * s, int picture_number) | |
| 161 { | |
| 162 int i; | |
| 163 | |
| 164 align_put_bits(&s->pb); | |
| 165 | |
| 166 put_bits(&s->pb, 2, s->pict_type - 1); | |
| 167 | |
| 168 put_bits(&s->pb, 5, s->qscale); | |
| 169 | |
| 170 s->rl_table_index = 2; | |
| 171 s->rl_chroma_table_index = 1; /* only for I frame */ | |
| 172 s->dc_table_index = 1; | |
| 173 s->mv_table_index = 1; /* only if P frame */ | |
| 174 s->use_skip_mb_code = 1; /* only if P frame */ | |
| 175 | |
| 176 if (s->pict_type == I_TYPE) { | |
| 177 put_bits(&s->pb, 5, 0x17); /* indicate only one "slice" */ | |
| 178 | |
| 179 code012(&s->pb, s->rl_chroma_table_index); | |
| 180 code012(&s->pb, s->rl_table_index); | |
| 181 | |
| 182 put_bits(&s->pb, 1, s->dc_table_index); | |
| 183 s->no_rounding = 1; | |
| 184 } else { | |
| 185 put_bits(&s->pb, 1, s->use_skip_mb_code); | |
| 186 | |
| 187 s->rl_chroma_table_index = s->rl_table_index; | |
| 188 code012(&s->pb, s->rl_table_index); | |
| 189 | |
| 190 put_bits(&s->pb, 1, s->dc_table_index); | |
| 191 | |
| 192 put_bits(&s->pb, 1, s->mv_table_index); | |
| 208 | 193 |
| 194 if(s->flipflop_rounding){ | |
| 195 s->no_rounding ^= 1; | |
| 196 }else{ | |
| 197 s->no_rounding = 0; | |
| 198 } | |
| 0 | 199 } |
| 200 | |
| 201 if (!init_done) { | |
| 202 /* init various encoding tables */ | |
| 203 init_done = 1; | |
| 204 init_mv_table(&mv_tables[0]); | |
| 205 init_mv_table(&mv_tables[1]); | |
| 206 for(i=0;i<NB_RL_TABLES;i++) | |
| 207 init_rl(&rl_table[i]); | |
| 208 } | |
| 209 | |
| 210 #ifdef DEBUG | |
| 211 intra_count = 0; | |
| 212 printf("*****frame %d:\n", frame_count++); | |
| 213 #endif | |
| 214 } | |
| 215 | |
| 208 | 216 void msmpeg4_encode_ext_header(MpegEncContext * s) |
| 217 { | |
| 218 s->flipflop_rounding=1; | |
|
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
|
219 s->bitrate= 910; // FIXME |
| 208 | 220 |
|
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
|
221 put_bits(&s->pb, 5, s->frame_rate / FRAME_RATE_BASE); //yes 29.97 -> 29 |
| 208 | 222 |
| 223 put_bits(&s->pb, 11, s->bitrate); | |
| 224 | |
| 225 put_bits(&s->pb, 1, s->flipflop_rounding); | |
| 226 } | |
| 227 | |
| 0 | 228 /* predict coded block */ |
| 229 static inline int coded_block_pred(MpegEncContext * s, int n, UINT8 **coded_block_ptr) | |
| 230 { | |
| 299 | 231 int xy, wrap, pred, a, b, c; |
| 0 | 232 |
| 299 | 233 xy = s->block_index[n]; |
| 234 wrap = s->block_wrap[0]; | |
| 0 | 235 |
| 236 /* B C | |
| 237 * A X | |
| 238 */ | |
| 299 | 239 a = s->coded_block[xy - 1 ]; |
| 240 b = s->coded_block[xy - 1 - wrap]; | |
| 241 c = s->coded_block[xy - wrap]; | |
| 0 | 242 |
| 243 if (b == c) { | |
| 244 pred = a; | |
| 245 } else { | |
| 246 pred = c; | |
| 247 } | |
| 248 | |
| 249 /* store value */ | |
| 299 | 250 *coded_block_ptr = &s->coded_block[xy]; |
| 0 | 251 |
| 252 return pred; | |
| 253 } | |
| 254 | |
| 255 static void msmpeg4_encode_motion(MpegEncContext * s, | |
| 256 int mx, int my) | |
| 257 { | |
| 258 int code; | |
| 259 MVTable *mv; | |
| 260 | |
| 261 /* modulo encoding */ | |
| 262 /* WARNING : you cannot reach all the MVs even with the modulo | |
| 263 encoding. This is a somewhat strange compromise they took !!! */ | |
| 264 if (mx <= -64) | |
| 265 mx += 64; | |
| 266 else if (mx >= 64) | |
| 267 mx -= 64; | |
| 268 if (my <= -64) | |
| 269 my += 64; | |
| 270 else if (my >= 64) | |
| 271 my -= 64; | |
| 272 | |
| 273 mx += 32; | |
| 274 my += 32; | |
| 275 #if 0 | |
| 276 if ((unsigned)mx >= 64 || | |
| 277 (unsigned)my >= 64) | |
| 278 fprintf(stderr, "error mx=%d my=%d\n", mx, my); | |
| 279 #endif | |
| 280 mv = &mv_tables[s->mv_table_index]; | |
| 281 | |
| 282 code = mv->table_mv_index[(mx << 6) | my]; | |
| 283 set_stat(ST_MV); | |
| 284 put_bits(&s->pb, | |
| 285 mv->table_mv_bits[code], | |
| 286 mv->table_mv_code[code]); | |
| 287 if (code == mv->n) { | |
| 288 /* escape : code litterally */ | |
| 289 put_bits(&s->pb, 6, mx); | |
| 290 put_bits(&s->pb, 6, my); | |
| 291 } | |
| 292 } | |
| 293 | |
| 294 void msmpeg4_encode_mb(MpegEncContext * s, | |
| 295 DCTELEM block[6][64], | |
| 296 int motion_x, int motion_y) | |
| 297 { | |
| 298 int cbp, coded_cbp, i; | |
| 299 int pred_x, pred_y; | |
| 300 UINT8 *coded_block; | |
| 301 | |
| 302 if (!s->mb_intra) { | |
| 303 /* compute cbp */ | |
| 304 set_stat(ST_INTER_MB); | |
| 305 cbp = 0; | |
| 306 for (i = 0; i < 6; i++) { | |
| 307 if (s->block_last_index[i] >= 0) | |
| 308 cbp |= 1 << (5 - i); | |
| 309 } | |
| 310 if (s->use_skip_mb_code && (cbp | motion_x | motion_y) == 0) { | |
| 311 /* skip macroblock */ | |
| 312 put_bits(&s->pb, 1, 1); | |
| 313 return; | |
| 314 } | |
| 315 if (s->use_skip_mb_code) | |
| 316 put_bits(&s->pb, 1, 0); /* mb coded */ | |
| 317 | |
| 318 put_bits(&s->pb, | |
| 319 table_mb_non_intra[cbp + 64][1], | |
| 320 table_mb_non_intra[cbp + 64][0]); | |
| 321 | |
| 322 /* motion vector */ | |
| 323 h263_pred_motion(s, 0, &pred_x, &pred_y); | |
| 324 msmpeg4_encode_motion(s, motion_x - pred_x, | |
| 325 motion_y - pred_y); | |
| 326 } else { | |
| 327 /* compute cbp */ | |
| 328 cbp = 0; | |
| 329 coded_cbp = 0; | |
| 330 for (i = 0; i < 6; i++) { | |
| 331 int val, pred; | |
| 332 val = (s->block_last_index[i] >= 1); | |
| 333 cbp |= val << (5 - i); | |
| 334 if (i < 4) { | |
| 335 /* predict value for close blocks only for luma */ | |
| 336 pred = coded_block_pred(s, i, &coded_block); | |
| 337 *coded_block = val; | |
| 338 val = val ^ pred; | |
| 339 } | |
| 340 coded_cbp |= val << (5 - i); | |
| 341 } | |
| 342 #if 0 | |
| 343 if (coded_cbp) | |
| 344 printf("cbp=%x %x\n", cbp, coded_cbp); | |
| 345 #endif | |
| 346 | |
| 347 if (s->pict_type == I_TYPE) { | |
| 348 set_stat(ST_INTRA_MB); | |
| 349 put_bits(&s->pb, | |
| 350 table_mb_intra[coded_cbp][1], table_mb_intra[coded_cbp][0]); | |
| 351 } else { | |
| 352 if (s->use_skip_mb_code) | |
| 353 put_bits(&s->pb, 1, 0); /* mb coded */ | |
| 354 put_bits(&s->pb, | |
| 355 table_mb_non_intra[cbp][1], | |
| 356 table_mb_non_intra[cbp][0]); | |
| 357 } | |
| 358 set_stat(ST_INTRA_MB); | |
| 359 put_bits(&s->pb, 1, 0); /* no AC prediction yet */ | |
| 360 } | |
| 361 | |
| 362 for (i = 0; i < 6; i++) { | |
| 363 msmpeg4_encode_block(s, block[i], i); | |
| 364 } | |
| 365 } | |
| 366 | |
| 367 | |
| 368 /* strongly inspirated from MPEG4, but not exactly the same ! */ | |
| 369 void msmpeg4_dc_scale(MpegEncContext * s) | |
| 370 { | |
| 309 | 371 if (s->qscale < 5 || s->msmpeg4_version==2){ |
|
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
|
372 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
|
373 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
|
374 }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
|
375 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
|
376 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
|
377 }else{ |
|
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
|
378 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
|
379 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
|
380 } |
| 0 | 381 } |
| 382 | |
| 383 /* dir = 0: left, dir = 1: top prediction */ | |
| 384 static int msmpeg4_pred_dc(MpegEncContext * s, int n, | |
| 25 | 385 INT16 **dc_val_ptr, int *dir_ptr) |
| 0 | 386 { |
| 299 | 387 int a, b, c, wrap, pred, scale; |
| 25 | 388 INT16 *dc_val; |
| 0 | 389 |
| 390 /* find prediction */ | |
| 391 if (n < 4) { | |
| 392 scale = s->y_dc_scale; | |
| 393 } else { | |
| 394 scale = s->c_dc_scale; | |
| 395 } | |
| 299 | 396 wrap = s->block_wrap[n]; |
| 397 dc_val= s->dc_val[0] + s->block_index[n]; | |
| 0 | 398 |
| 399 /* B C | |
| 400 * A X | |
| 401 */ | |
| 299 | 402 a = dc_val[ - 1]; |
| 403 b = dc_val[ - 1 - wrap]; | |
| 404 c = dc_val[ - wrap]; | |
| 0 | 405 |
| 406 /* XXX: the following solution consumes divisions, but it does not | |
| 407 necessitate to modify mpegvideo.c. The problem comes from the | |
| 408 fact they decided to store the quantized DC (which would lead | |
| 409 to problems if Q could vary !) */ | |
| 221 | 410 #if defined ARCH_X86 && !defined PIC |
| 204 | 411 asm volatile( |
| 412 "movl %3, %%eax \n\t" | |
| 413 "shrl $1, %%eax \n\t" | |
| 414 "addl %%eax, %2 \n\t" | |
| 415 "addl %%eax, %1 \n\t" | |
| 416 "addl %0, %%eax \n\t" | |
|
225
ae145876789d
use multiply instead of divides for DC prediction on X86
michaelni
parents:
221
diff
changeset
|
417 "mull %4 \n\t" |
|
ae145876789d
use multiply instead of divides for DC prediction on X86
michaelni
parents:
221
diff
changeset
|
418 "movl %%edx, %0 \n\t" |
| 204 | 419 "movl %1, %%eax \n\t" |
|
225
ae145876789d
use multiply instead of divides for DC prediction on X86
michaelni
parents:
221
diff
changeset
|
420 "mull %4 \n\t" |
|
ae145876789d
use multiply instead of divides for DC prediction on X86
michaelni
parents:
221
diff
changeset
|
421 "movl %%edx, %1 \n\t" |
| 204 | 422 "movl %2, %%eax \n\t" |
|
225
ae145876789d
use multiply instead of divides for DC prediction on X86
michaelni
parents:
221
diff
changeset
|
423 "mull %4 \n\t" |
|
ae145876789d
use multiply instead of divides for DC prediction on X86
michaelni
parents:
221
diff
changeset
|
424 "movl %%edx, %2 \n\t" |
| 228 | 425 : "+b" (a), "+c" (b), "+D" (c) |
| 426 : "g" (scale), "S" (inverse[scale]) | |
| 204 | 427 : "%eax", "%edx" |
| 428 ); | |
| 221 | 429 #else |
| 430 /* #elif defined (ARCH_ALPHA) */ | |
|
214
73df666cacc7
Alpha optimizations by Falk Hueffner <falk.hueffner@student.uni-tuebingen.de>
nickols_k
parents:
208
diff
changeset
|
431 /* Divisions are extremely costly on Alpha; optimize the most |
| 221 | 432 common case. But they are costly everywhere... |
| 433 */ | |
|
214
73df666cacc7
Alpha optimizations by Falk Hueffner <falk.hueffner@student.uni-tuebingen.de>
nickols_k
parents:
208
diff
changeset
|
434 if (scale == 8) { |
|
73df666cacc7
Alpha optimizations by Falk Hueffner <falk.hueffner@student.uni-tuebingen.de>
nickols_k
parents:
208
diff
changeset
|
435 a = (a + (8 >> 1)) / 8; |
|
73df666cacc7
Alpha optimizations by Falk Hueffner <falk.hueffner@student.uni-tuebingen.de>
nickols_k
parents:
208
diff
changeset
|
436 b = (b + (8 >> 1)) / 8; |
|
73df666cacc7
Alpha optimizations by Falk Hueffner <falk.hueffner@student.uni-tuebingen.de>
nickols_k
parents:
208
diff
changeset
|
437 c = (c + (8 >> 1)) / 8; |
|
73df666cacc7
Alpha optimizations by Falk Hueffner <falk.hueffner@student.uni-tuebingen.de>
nickols_k
parents:
208
diff
changeset
|
438 } else { |
|
73df666cacc7
Alpha optimizations by Falk Hueffner <falk.hueffner@student.uni-tuebingen.de>
nickols_k
parents:
208
diff
changeset
|
439 a = (a + (scale >> 1)) / scale; |
|
73df666cacc7
Alpha optimizations by Falk Hueffner <falk.hueffner@student.uni-tuebingen.de>
nickols_k
parents:
208
diff
changeset
|
440 b = (b + (scale >> 1)) / scale; |
|
73df666cacc7
Alpha optimizations by Falk Hueffner <falk.hueffner@student.uni-tuebingen.de>
nickols_k
parents:
208
diff
changeset
|
441 c = (c + (scale >> 1)) / scale; |
|
73df666cacc7
Alpha optimizations by Falk Hueffner <falk.hueffner@student.uni-tuebingen.de>
nickols_k
parents:
208
diff
changeset
|
442 } |
| 204 | 443 #endif |
| 0 | 444 /* XXX: WARNING: they did not choose the same test as MPEG4. This |
| 445 is very important ! */ | |
| 446 if (abs(a - b) <= abs(b - c)) { | |
| 447 pred = c; | |
| 448 *dir_ptr = 1; | |
| 449 } else { | |
| 450 pred = a; | |
| 451 *dir_ptr = 0; | |
| 452 } | |
| 453 | |
| 454 /* update predictor */ | |
| 299 | 455 *dc_val_ptr = &dc_val[0]; |
| 0 | 456 return pred; |
| 457 } | |
| 458 | |
| 459 #define DC_MAX 119 | |
| 460 | |
| 461 static void msmpeg4_encode_dc(MpegEncContext * s, int level, int n, int *dir_ptr) | |
| 462 { | |
| 463 int sign, code; | |
| 464 int pred; | |
| 25 | 465 INT16 *dc_val; |
| 0 | 466 |
| 467 pred = msmpeg4_pred_dc(s, n, &dc_val, dir_ptr); | |
| 468 | |
| 469 /* update predictor */ | |
| 470 if (n < 4) { | |
| 471 *dc_val = level * s->y_dc_scale; | |
| 472 } else { | |
| 473 *dc_val = level * s->c_dc_scale; | |
| 474 } | |
| 475 | |
| 476 /* do the prediction */ | |
| 477 level -= pred; | |
| 478 | |
| 479 sign = 0; | |
| 480 if (level < 0) { | |
| 481 level = -level; | |
| 482 sign = 1; | |
| 483 } | |
| 484 | |
| 485 code = level; | |
| 486 if (code > DC_MAX) | |
| 487 code = DC_MAX; | |
| 488 | |
| 489 if (s->dc_table_index == 0) { | |
| 490 if (n < 4) { | |
| 491 put_bits(&s->pb, table0_dc_lum[code][1], table0_dc_lum[code][0]); | |
| 492 } else { | |
| 493 put_bits(&s->pb, table0_dc_chroma[code][1], table0_dc_chroma[code][0]); | |
| 494 } | |
| 495 } else { | |
| 496 if (n < 4) { | |
| 497 put_bits(&s->pb, table1_dc_lum[code][1], table1_dc_lum[code][0]); | |
| 498 } else { | |
| 499 put_bits(&s->pb, table1_dc_chroma[code][1], table1_dc_chroma[code][0]); | |
| 500 } | |
| 501 } | |
| 502 | |
| 503 if (code == DC_MAX) | |
| 504 put_bits(&s->pb, 8, level); | |
| 505 | |
| 506 if (level != 0) { | |
| 507 put_bits(&s->pb, 1, sign); | |
| 508 } | |
| 509 } | |
| 510 | |
| 511 /* Encoding of a block. Very similar to MPEG4 except for a different | |
| 512 escape coding (same as H263) and more vlc tables. | |
| 513 */ | |
| 514 static void msmpeg4_encode_block(MpegEncContext * s, DCTELEM * block, int n) | |
| 515 { | |
| 516 int level, run, last, i, j, last_index; | |
| 517 int last_non_zero, sign, slevel; | |
| 518 int code, run_diff, dc_pred_dir; | |
| 519 const RLTable *rl; | |
| 520 | |
| 521 if (s->mb_intra) { | |
| 522 set_stat(ST_DC); | |
| 523 msmpeg4_encode_dc(s, block[0], n, &dc_pred_dir); | |
| 524 i = 1; | |
| 525 if (n < 4) { | |
| 526 rl = &rl_table[s->rl_table_index]; | |
| 527 } else { | |
| 528 rl = &rl_table[3 + s->rl_chroma_table_index]; | |
| 529 } | |
| 530 run_diff = 0; | |
| 531 set_stat(ST_INTRA_AC); | |
| 532 } else { | |
| 533 i = 0; | |
| 534 rl = &rl_table[3 + s->rl_table_index]; | |
| 535 run_diff = 1; | |
| 536 set_stat(ST_INTER_AC); | |
| 537 } | |
| 538 | |
| 539 /* AC coefs */ | |
| 540 last_index = s->block_last_index[n]; | |
| 541 last_non_zero = i - 1; | |
| 542 for (; i <= last_index; i++) { | |
| 543 j = zigzag_direct[i]; | |
| 544 level = block[j]; | |
| 545 if (level) { | |
| 546 run = i - last_non_zero - 1; | |
| 547 last = (i == last_index); | |
| 548 sign = 0; | |
| 549 slevel = level; | |
| 550 if (level < 0) { | |
| 551 sign = 1; | |
| 552 level = -level; | |
| 553 } | |
| 554 code = get_rl_index(rl, last, run, level); | |
| 555 put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]); | |
| 556 if (code == rl->n) { | |
| 557 int level1, run1; | |
| 558 | |
| 559 level1 = level - rl->max_level[last][run]; | |
| 560 if (level1 < 1) | |
| 561 goto esc2; | |
| 562 code = get_rl_index(rl, last, run, level1); | |
| 563 if (code == rl->n) { | |
| 564 esc2: | |
| 565 put_bits(&s->pb, 1, 0); | |
| 566 if (level > MAX_LEVEL) | |
| 567 goto esc3; | |
| 568 run1 = run - rl->max_run[last][level] - run_diff; | |
| 569 if (run1 < 0) | |
| 570 goto esc3; | |
| 571 code = get_rl_index(rl, last, run1, level); | |
| 572 if (code == rl->n) { | |
| 573 esc3: | |
| 574 /* third escape */ | |
| 575 put_bits(&s->pb, 1, 0); | |
| 576 put_bits(&s->pb, 1, last); | |
| 577 put_bits(&s->pb, 6, run); | |
| 578 put_bits(&s->pb, 8, slevel & 0xff); | |
| 579 } else { | |
| 580 /* second escape */ | |
| 581 put_bits(&s->pb, 1, 1); | |
| 582 put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]); | |
| 583 put_bits(&s->pb, 1, sign); | |
| 584 } | |
| 585 } else { | |
| 586 /* first escape */ | |
| 587 put_bits(&s->pb, 1, 1); | |
| 588 put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]); | |
| 589 put_bits(&s->pb, 1, sign); | |
| 590 } | |
| 591 } else { | |
| 592 put_bits(&s->pb, 1, sign); | |
| 593 } | |
| 594 last_non_zero = i; | |
| 595 } | |
| 596 } | |
| 597 } | |
| 598 | |
| 599 /****************************************/ | |
| 600 /* decoding stuff */ | |
| 601 | |
| 602 static VLC mb_non_intra_vlc; | |
| 603 static VLC mb_intra_vlc; | |
| 604 static VLC dc_lum_vlc[2]; | |
| 605 static VLC dc_chroma_vlc[2]; | |
| 307 | 606 static VLC v2_dc_lum_vlc; |
| 607 static VLC v2_dc_chroma_vlc; | |
| 608 static VLC cbpy_vlc; | |
| 609 static VLC v2_intra_cbpc_vlc; | |
| 610 static VLC v2_mb_type_vlc; | |
| 611 static VLC v2_mv_vlc; | |
| 612 | |
| 613 /* this table is practically identical to the one from h263 except that its inverted */ | |
| 614 static void init_h263_dc_for_msmpeg4() | |
| 615 { | |
| 616 static int inited=0; | |
| 617 | |
| 618 if(!inited){ | |
| 619 int level, uni_code, uni_len; | |
| 620 inited=1; | |
| 621 | |
| 309 | 622 for(level=-256; level<256; level++){ |
| 307 | 623 int size, v, l; |
| 624 /* find number of bits */ | |
| 625 size = 0; | |
| 626 v = abs(level); | |
| 627 while (v) { | |
| 628 v >>= 1; | |
| 629 size++; | |
| 630 } | |
| 631 | |
| 632 if (level < 0) | |
| 633 l= (-level) ^ ((1 << size) - 1); | |
| 634 else | |
| 635 l= level; | |
| 636 | |
| 637 /* luminance h263 */ | |
| 638 uni_code= DCtab_lum[size][0]; | |
| 639 uni_len = DCtab_lum[size][1]; | |
| 640 uni_code ^= (1<<uni_len)-1; //M$ doesnt like compatibility | |
| 641 | |
| 642 if (size > 0) { | |
| 643 uni_code<<=size; uni_code|=l; | |
| 644 uni_len+=size; | |
| 645 if (size > 8){ | |
| 646 uni_code<<=1; uni_code|=1; | |
| 647 uni_len++; | |
| 648 } | |
| 649 } | |
| 650 v2_dc_lum_table[level+256][0]= uni_code; | |
| 651 v2_dc_lum_table[level+256][1]= uni_len; | |
| 652 | |
| 653 /* chrominance h263 */ | |
| 654 uni_code= DCtab_chrom[size][0]; | |
| 655 uni_len = DCtab_chrom[size][1]; | |
| 656 uni_code ^= (1<<uni_len)-1; //M$ doesnt like compatibility | |
| 657 | |
| 658 if (size > 0) { | |
| 659 uni_code<<=size; uni_code|=l; | |
| 660 uni_len+=size; | |
| 661 if (size > 8){ | |
| 662 uni_code<<=1; uni_code|=1; | |
| 663 uni_len++; | |
| 664 } | |
| 665 } | |
| 666 v2_dc_chroma_table[level+256][0]= uni_code; | |
| 667 v2_dc_chroma_table[level+256][1]= uni_len; | |
| 668 | |
| 669 } | |
| 670 } | |
| 671 } | |
| 0 | 672 |
| 673 /* init all vlc decoding tables */ | |
| 674 int msmpeg4_decode_init_vlc(MpegEncContext *s) | |
| 675 { | |
| 676 int i; | |
| 677 MVTable *mv; | |
| 678 | |
| 679 for(i=0;i<NB_RL_TABLES;i++) { | |
| 680 init_rl(&rl_table[i]); | |
| 681 init_vlc_rl(&rl_table[i]); | |
| 682 } | |
| 683 for(i=0;i<2;i++) { | |
| 684 mv = &mv_tables[i]; | |
| 685 init_vlc(&mv->vlc, 9, mv->n + 1, | |
| 686 mv->table_mv_bits, 1, 1, | |
| 687 mv->table_mv_code, 2, 2); | |
| 688 } | |
| 689 | |
| 690 init_vlc(&dc_lum_vlc[0], 9, 120, | |
| 691 &table0_dc_lum[0][1], 8, 4, | |
| 692 &table0_dc_lum[0][0], 8, 4); | |
| 693 init_vlc(&dc_chroma_vlc[0], 9, 120, | |
| 694 &table0_dc_chroma[0][1], 8, 4, | |
| 695 &table0_dc_chroma[0][0], 8, 4); | |
| 696 init_vlc(&dc_lum_vlc[1], 9, 120, | |
| 697 &table1_dc_lum[0][1], 8, 4, | |
| 698 &table1_dc_lum[0][0], 8, 4); | |
| 699 init_vlc(&dc_chroma_vlc[1], 9, 120, | |
| 700 &table1_dc_chroma[0][1], 8, 4, | |
| 701 &table1_dc_chroma[0][0], 8, 4); | |
| 307 | 702 |
| 703 init_h263_dc_for_msmpeg4(); | |
| 704 init_vlc(&v2_dc_lum_vlc, 9, 512, | |
| 705 &v2_dc_lum_table[0][1], 8, 4, | |
| 706 &v2_dc_lum_table[0][0], 8, 4); | |
| 707 init_vlc(&v2_dc_chroma_vlc, 9, 512, | |
| 708 &v2_dc_chroma_table[0][1], 8, 4, | |
| 709 &v2_dc_chroma_table[0][0], 8, 4); | |
| 710 | |
| 711 init_vlc(&cbpy_vlc, 6, 16, | |
| 712 &cbpy_tab[0][1], 2, 1, | |
| 713 &cbpy_tab[0][0], 2, 1); | |
| 714 init_vlc(&v2_intra_cbpc_vlc, 3, 4, | |
| 715 &v2_intra_cbpc[0][1], 2, 1, | |
| 716 &v2_intra_cbpc[0][0], 2, 1); | |
| 717 init_vlc(&v2_mb_type_vlc, 5, 8, | |
| 718 &v2_mb_type[0][1], 2, 1, | |
| 719 &v2_mb_type[0][0], 2, 1); | |
| 720 init_vlc(&v2_mv_vlc, 9, 33, | |
| 721 &mvtab[0][1], 2, 1, | |
| 722 &mvtab[0][0], 2, 1); | |
| 0 | 723 |
| 724 init_vlc(&mb_non_intra_vlc, 9, 128, | |
| 725 &table_mb_non_intra[0][1], 8, 4, | |
| 726 &table_mb_non_intra[0][0], 8, 4); | |
| 48 | 727 init_vlc(&mb_intra_vlc, 9, 64, |
| 0 | 728 &table_mb_intra[0][1], 4, 2, |
| 729 &table_mb_intra[0][0], 4, 2); | |
| 730 return 0; | |
| 731 } | |
| 732 | |
| 733 static int decode012(GetBitContext *gb) | |
| 734 { | |
| 735 int n; | |
| 21 | 736 n = get_bits1(gb); |
| 0 | 737 if (n == 0) |
| 738 return 0; | |
| 739 else | |
| 21 | 740 return get_bits1(gb) + 1; |
| 0 | 741 } |
| 742 | |
| 307 | 743 int msmpeg4_decode_picture_header(MpegEncContext * s) |
|
300
d874359e58f1
msmpeg4v2 header parser & some dump bits code behind #if 0
michaelni
parents:
299
diff
changeset
|
744 { |
|
d874359e58f1
msmpeg4v2 header parser & some dump bits code behind #if 0
michaelni
parents:
299
diff
changeset
|
745 int code; |
|
d874359e58f1
msmpeg4v2 header parser & some dump bits code behind #if 0
michaelni
parents:
299
diff
changeset
|
746 |
|
d874359e58f1
msmpeg4v2 header parser & some dump bits code behind #if 0
michaelni
parents:
299
diff
changeset
|
747 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
|
748 if (s->pict_type != I_TYPE && |
|
d874359e58f1
msmpeg4v2 header parser & some dump bits code behind #if 0
michaelni
parents:
299
diff
changeset
|
749 s->pict_type != P_TYPE) |
|
d874359e58f1
msmpeg4v2 header parser & some dump bits code behind #if 0
michaelni
parents:
299
diff
changeset
|
750 return -1; |
|
d874359e58f1
msmpeg4v2 header parser & some dump bits code behind #if 0
michaelni
parents:
299
diff
changeset
|
751 |
|
d874359e58f1
msmpeg4v2 header parser & some dump bits code behind #if 0
michaelni
parents:
299
diff
changeset
|
752 s->qscale = get_bits(&s->gb, 5); |
|
d874359e58f1
msmpeg4v2 header parser & some dump bits code behind #if 0
michaelni
parents:
299
diff
changeset
|
753 |
|
d874359e58f1
msmpeg4v2 header parser & some dump bits code behind #if 0
michaelni
parents:
299
diff
changeset
|
754 if (s->pict_type == I_TYPE) { |
|
d874359e58f1
msmpeg4v2 header parser & some dump bits code behind #if 0
michaelni
parents:
299
diff
changeset
|
755 code = get_bits(&s->gb, 5); |
| 307 | 756 /* 0x17: one slice, 0x18: two slices */ |
|
300
d874359e58f1
msmpeg4v2 header parser & some dump bits code behind #if 0
michaelni
parents:
299
diff
changeset
|
757 if (code < 0x17) |
|
d874359e58f1
msmpeg4v2 header parser & some dump bits code behind #if 0
michaelni
parents:
299
diff
changeset
|
758 return -1; |
|
d874359e58f1
msmpeg4v2 header parser & some dump bits code behind #if 0
michaelni
parents:
299
diff
changeset
|
759 s->slice_height = s->mb_height / (code - 0x16); |
| 307 | 760 if(s->msmpeg4_version==2){ |
| 761 s->rl_chroma_table_index = 2; | |
| 762 s->rl_table_index = 2; | |
|
300
d874359e58f1
msmpeg4v2 header parser & some dump bits code behind #if 0
michaelni
parents:
299
diff
changeset
|
763 |
| 307 | 764 s->dc_table_index = 0; //not used |
| 765 }else{ | |
| 766 s->rl_chroma_table_index = decode012(&s->gb); | |
| 767 s->rl_table_index = decode012(&s->gb); | |
| 0 | 768 |
| 307 | 769 s->dc_table_index = get_bits1(&s->gb); |
| 770 } | |
| 0 | 771 s->no_rounding = 1; |
| 200 | 772 /* printf(" %d %d %d %d \n", |
| 773 s->qscale, | |
| 774 s->rl_chroma_table_index, | |
| 775 s->rl_table_index, | |
| 776 s->dc_table_index);*/ | |
| 0 | 777 } else { |
| 21 | 778 s->use_skip_mb_code = get_bits1(&s->gb); |
| 0 | 779 |
| 307 | 780 if(s->msmpeg4_version==2){ |
| 781 s->rl_table_index = 2; | |
| 782 s->rl_chroma_table_index = s->rl_table_index; | |
| 783 | |
| 784 s->dc_table_index = 0; //not used | |
| 0 | 785 |
| 307 | 786 s->mv_table_index = 0; |
| 787 }else{ | |
| 788 s->rl_table_index = decode012(&s->gb); | |
| 789 s->rl_chroma_table_index = s->rl_table_index; | |
| 0 | 790 |
| 307 | 791 s->dc_table_index = get_bits1(&s->gb); |
| 792 | |
| 793 s->mv_table_index = get_bits1(&s->gb); | |
| 794 } | |
| 200 | 795 /* printf(" %d %d %d %d %d \n", |
| 796 s->use_skip_mb_code, | |
| 797 s->rl_table_index, | |
| 798 s->rl_chroma_table_index, | |
| 799 s->dc_table_index, | |
| 800 s->mv_table_index);*/ | |
| 208 | 801 if(s->flipflop_rounding){ |
| 802 s->no_rounding ^= 1; | |
| 803 }else{ | |
| 804 s->no_rounding = 0; | |
| 805 } | |
| 806 // printf("%d", s->no_rounding); | |
| 0 | 807 } |
| 208 | 808 |
| 307 | 809 #if 0 |
| 810 if(s->msmpeg4_version==2) | |
| 811 { | |
| 812 int i; | |
| 813 for(i=0; i<s->gb.size*8; i++) | |
| 814 // printf("%d", get_bits1(&s->gb)); | |
| 815 get_bits1(&s->gb); | |
| 816 printf("END\n"); | |
| 817 return -1; | |
| 818 } | |
| 819 #endif | |
| 820 | |
| 0 | 821 #ifdef DEBUG |
| 822 printf("*****frame %d:\n", frame_count++); | |
| 823 #endif | |
| 824 return 0; | |
| 825 } | |
| 826 | |
| 208 | 827 int msmpeg4_decode_ext_header(MpegEncContext * s, int buf_size) |
| 828 { | |
| 829 /* the alt_bitstream reader could read over the end so we need to check it */ | |
|
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
|
830 if(get_bits_count(&s->gb) + 16 < buf_size*8) |
| 208 | 831 { |
|
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
|
832 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
|
833 |
|
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
|
834 fps= get_bits(&s->gb, 5); |
|
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
|
835 s->bitrate= get_bits(&s->gb, 11); |
|
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
|
836 s->flipflop_rounding= get_bits1(&s->gb); |
|
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
|
837 |
|
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
|
838 // printf("fps:%2d bps:%2d roundingType:%1d\n", fps, s->bitrate, s->flipflop_rounding); |
| 208 | 839 } |
| 840 else | |
| 841 { | |
|
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
|
842 s->flipflop_rounding= 0; |
|
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
|
843 s->bitrate= 0; |
| 208 | 844 } |
|
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
|
845 |
| 208 | 846 return 0; |
| 847 } | |
| 848 | |
| 246 | 849 static inline void memsetw(short *tab, int val, int n) |
| 0 | 850 { |
| 851 int i; | |
| 852 for(i=0;i<n;i++) | |
| 853 tab[i] = val; | |
| 854 } | |
| 855 | |
| 307 | 856 /* this is identical to h263 except that its range is multiplied by 2 */ |
| 857 static int msmpeg4v2_decode_motion(MpegEncContext * s, int pred, int f_code) | |
| 858 { | |
| 859 int code, val, sign, shift; | |
| 860 | |
| 861 code = get_vlc(&s->gb, &v2_mv_vlc); | |
| 862 if (code < 0) | |
| 863 return 0xffff; | |
| 864 | |
| 865 if (code == 0) | |
| 866 return pred; | |
| 867 sign = get_bits1(&s->gb); | |
| 868 shift = f_code - 1; | |
| 869 val = (code - 1) << shift; | |
| 870 if (shift > 0) | |
| 871 val |= get_bits(&s->gb, shift); | |
| 872 val++; | |
| 873 if (sign) | |
| 874 val = -val; | |
| 875 val += pred; | |
| 876 | |
| 877 if (val <= -64) | |
| 878 val += 64; | |
| 879 else if (val >= 64) | |
| 880 val -= 64; | |
| 881 | |
| 882 return val; | |
| 883 } | |
| 884 | |
| 885 | |
| 886 int msmpeg4v2_decode_mb(MpegEncContext *s, | |
| 887 DCTELEM block[6][64]) | |
| 888 { | |
| 889 int cbp, code, i; | |
| 890 if (s->pict_type == P_TYPE) { | |
| 891 if (s->use_skip_mb_code) { | |
| 892 if (get_bits1(&s->gb)) { | |
| 893 /* skip mb */ | |
| 894 s->mb_intra = 0; | |
| 895 for(i=0;i<6;i++) | |
| 896 s->block_last_index[i] = -1; | |
| 897 s->mv_dir = MV_DIR_FORWARD; | |
| 898 s->mv_type = MV_TYPE_16X16; | |
| 899 s->mv[0][0][0] = 0; | |
| 900 s->mv[0][0][1] = 0; | |
| 901 s->mb_skiped = 1; | |
| 902 return 0; | |
| 903 } | |
| 904 } | |
| 905 | |
| 906 code = get_vlc(&s->gb, &v2_mb_type_vlc); | |
| 907 s->mb_intra = code >>2; | |
| 908 | |
| 909 cbp = code & 0x3; | |
| 910 } else { | |
| 911 s->mb_intra = 1; | |
| 912 cbp= get_vlc(&s->gb, &v2_intra_cbpc_vlc); | |
| 913 } | |
| 914 | |
| 915 if (!s->mb_intra) { | |
| 916 int mx, my; | |
| 917 | |
| 918 cbp|= get_vlc(&s->gb, &cbpy_vlc)<<2; | |
| 919 if((cbp&3) != 3) cbp^= 0x3C; | |
| 920 | |
| 921 h263_pred_motion(s, 0, &mx, &my); | |
| 922 mx= msmpeg4v2_decode_motion(s, mx, 1); | |
| 923 my= msmpeg4v2_decode_motion(s, my, 1); | |
| 924 | |
| 925 s->mv_dir = MV_DIR_FORWARD; | |
| 926 s->mv_type = MV_TYPE_16X16; | |
| 927 s->mv[0][0][0] = mx; | |
| 928 s->mv[0][0][1] = my; | |
| 929 } else { | |
| 930 s->ac_pred = get_bits1(&s->gb); | |
| 931 cbp|= get_vlc(&s->gb, &cbpy_vlc)<<2; | |
| 932 } | |
| 933 | |
| 934 for (i = 0; i < 6; i++) { | |
| 935 if (msmpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1) < 0) | |
| 936 { | |
| 937 fprintf(stderr,"\nIgnoring error while decoding block: %d x %d (%d)\n", s->mb_x, s->mb_y, i); | |
| 938 return -1; | |
| 939 } | |
| 940 } | |
| 941 return 0; | |
| 942 } | |
| 943 | |
| 0 | 944 int msmpeg4_decode_mb(MpegEncContext *s, |
| 945 DCTELEM block[6][64]) | |
| 946 { | |
| 947 int cbp, code, i; | |
| 948 UINT8 *coded_val; | |
| 949 | |
| 950 /* special slice handling */ | |
| 951 if (s->mb_x == 0) { | |
| 122 | 952 if (s->slice_height && (s->mb_y % s->slice_height) == 0) { |
| 0 | 953 int wrap; |
| 954 /* reset DC pred (set previous line to 1024) */ | |
| 955 wrap = 2 * s->mb_width + 2; | |
| 956 memsetw(&s->dc_val[0][(1) + (2 * s->mb_y) * wrap], | |
| 957 1024, 2 * s->mb_width); | |
| 958 wrap = s->mb_width + 2; | |
| 959 memsetw(&s->dc_val[1][(1) + (s->mb_y) * wrap], | |
| 960 1024, s->mb_width); | |
| 961 memsetw(&s->dc_val[2][(1) + (s->mb_y) * wrap], | |
| 962 1024, s->mb_width); | |
|
186
cf37da86d990
fix slices when code=0x18, patch by Michael Niedermayer <michael@mplayer.dev.hu>
arpi_esp
parents:
122
diff
changeset
|
963 |
|
cf37da86d990
fix slices when code=0x18, patch by Michael Niedermayer <michael@mplayer.dev.hu>
arpi_esp
parents:
122
diff
changeset
|
964 /* reset AC pred (set previous line to 0) */ |
|
cf37da86d990
fix slices when code=0x18, patch by Michael Niedermayer <michael@mplayer.dev.hu>
arpi_esp
parents:
122
diff
changeset
|
965 wrap = s->mb_width * 2 + 2; |
|
cf37da86d990
fix slices when code=0x18, patch by Michael Niedermayer <michael@mplayer.dev.hu>
arpi_esp
parents:
122
diff
changeset
|
966 memsetw(s->ac_val[0][0] + (1 + (2 * s->mb_y) * wrap)*16, |
|
cf37da86d990
fix slices when code=0x18, patch by Michael Niedermayer <michael@mplayer.dev.hu>
arpi_esp
parents:
122
diff
changeset
|
967 0, 2 * s->mb_width*16); |
|
cf37da86d990
fix slices when code=0x18, patch by Michael Niedermayer <michael@mplayer.dev.hu>
arpi_esp
parents:
122
diff
changeset
|
968 wrap = s->mb_width + 2; |
|
cf37da86d990
fix slices when code=0x18, patch by Michael Niedermayer <michael@mplayer.dev.hu>
arpi_esp
parents:
122
diff
changeset
|
969 memsetw(s->ac_val[1][0] + (1 + (s->mb_y) * wrap)*16, |
|
cf37da86d990
fix slices when code=0x18, patch by Michael Niedermayer <michael@mplayer.dev.hu>
arpi_esp
parents:
122
diff
changeset
|
970 0, s->mb_width*16); |
|
cf37da86d990
fix slices when code=0x18, patch by Michael Niedermayer <michael@mplayer.dev.hu>
arpi_esp
parents:
122
diff
changeset
|
971 memsetw(s->ac_val[2][0] + (1 + (s->mb_y) * wrap)*16, |
|
cf37da86d990
fix slices when code=0x18, patch by Michael Niedermayer <michael@mplayer.dev.hu>
arpi_esp
parents:
122
diff
changeset
|
972 0, s->mb_width*16); |
| 0 | 973 |
| 974 s->first_slice_line = 1; | |
| 975 } else { | |
| 976 s->first_slice_line = 0; | |
| 977 } | |
| 978 } | |
| 979 | |
| 307 | 980 if(s->msmpeg4_version==2) return msmpeg4v2_decode_mb(s, block); //FIXME merge if possible |
| 981 | |
| 0 | 982 if (s->pict_type == P_TYPE) { |
| 983 set_stat(ST_INTER_MB); | |
| 984 if (s->use_skip_mb_code) { | |
| 21 | 985 if (get_bits1(&s->gb)) { |
| 0 | 986 /* skip mb */ |
| 987 s->mb_intra = 0; | |
| 988 for(i=0;i<6;i++) | |
| 989 s->block_last_index[i] = -1; | |
| 990 s->mv_dir = MV_DIR_FORWARD; | |
| 991 s->mv_type = MV_TYPE_16X16; | |
| 992 s->mv[0][0][0] = 0; | |
| 993 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
|
994 s->mb_skiped = 1; |
| 0 | 995 return 0; |
| 996 } | |
| 997 } | |
| 998 | |
| 999 code = get_vlc(&s->gb, &mb_non_intra_vlc); | |
| 1000 if (code < 0) | |
| 1001 return -1; | |
| 246 | 1002 //s->mb_intra = (code & 0x40) ? 0 : 1; |
| 1003 s->mb_intra = (~code & 0x40) >> 6; | |
| 0 | 1004 |
| 1005 cbp = code & 0x3f; | |
| 1006 } else { | |
| 1007 set_stat(ST_INTRA_MB); | |
| 1008 s->mb_intra = 1; | |
| 1009 code = get_vlc(&s->gb, &mb_intra_vlc); | |
| 1010 if (code < 0) | |
| 1011 return -1; | |
| 1012 /* predict coded block pattern */ | |
| 1013 cbp = 0; | |
| 1014 for(i=0;i<6;i++) { | |
| 246 | 1015 int val = ((code >> (5 - i)) & 1); |
| 0 | 1016 if (i < 4) { |
| 246 | 1017 int pred = coded_block_pred(s, i, &coded_val); |
| 0 | 1018 val = val ^ pred; |
| 1019 *coded_val = val; | |
| 1020 } | |
| 1021 cbp |= val << (5 - i); | |
| 1022 } | |
| 1023 } | |
| 1024 | |
| 1025 if (!s->mb_intra) { | |
| 1026 int mx, my; | |
| 1027 set_stat(ST_MV); | |
| 1028 h263_pred_motion(s, 0, &mx, &my); | |
| 1029 if (msmpeg4_decode_motion(s, &mx, &my) < 0) | |
| 1030 return -1; | |
| 1031 s->mv_dir = MV_DIR_FORWARD; | |
| 1032 s->mv_type = MV_TYPE_16X16; | |
| 1033 s->mv[0][0][0] = mx; | |
| 1034 s->mv[0][0][1] = my; | |
| 1035 } else { | |
| 1036 set_stat(ST_INTRA_MB); | |
| 21 | 1037 s->ac_pred = get_bits1(&s->gb); |
| 0 | 1038 } |
| 1039 | |
| 1040 for (i = 0; i < 6; i++) { | |
| 1041 if (msmpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1) < 0) | |
| 246 | 1042 { |
| 1043 fprintf(stderr,"\nIgnoring error while decoding block: %d x %d (%d)\n", s->mb_x, s->mb_y, i); | |
| 1044 // return -1; | |
| 1045 } | |
| 0 | 1046 } |
| 1047 return 0; | |
| 1048 } | |
| 1049 | |
| 1050 static int msmpeg4_decode_block(MpegEncContext * s, DCTELEM * block, | |
| 1051 int n, int coded) | |
| 1052 { | |
| 1053 int code, level, i, j, last, run, run_diff; | |
| 1054 int dc_pred_dir; | |
| 1055 RLTable *rl; | |
| 1056 const UINT8 *scan_table; | |
| 200 | 1057 int qmul, qadd; |
| 0 | 1058 |
| 1059 if (s->mb_intra) { | |
| 200 | 1060 qmul=1; |
| 1061 qadd=0; | |
| 1062 | |
| 0 | 1063 /* DC coef */ |
| 1064 set_stat(ST_DC); | |
| 1065 level = msmpeg4_decode_dc(s, n, &dc_pred_dir); | |
| 1066 if (level < 0) | |
| 1067 return -1; | |
| 1068 block[0] = level; | |
| 1069 if (n < 4) { | |
| 1070 rl = &rl_table[s->rl_table_index]; | |
| 1071 } else { | |
| 1072 rl = &rl_table[3 + s->rl_chroma_table_index]; | |
| 1073 } | |
| 200 | 1074 |
| 0 | 1075 run_diff = 0; |
| 1076 i = 1; | |
| 1077 if (!coded) { | |
| 1078 goto not_coded; | |
| 1079 } | |
| 1080 if (s->ac_pred) { | |
| 1081 if (dc_pred_dir == 0) | |
| 1082 scan_table = ff_alternate_vertical_scan; /* left */ | |
| 1083 else | |
| 1084 scan_table = ff_alternate_horizontal_scan; /* top */ | |
| 1085 } else { | |
| 1086 scan_table = zigzag_direct; | |
| 1087 } | |
| 1088 set_stat(ST_INTRA_AC); | |
| 1089 } else { | |
| 200 | 1090 qmul = s->qscale << 1; |
| 1091 qadd = (s->qscale - 1) | 1; | |
| 0 | 1092 i = 0; |
| 1093 rl = &rl_table[3 + s->rl_table_index]; | |
| 307 | 1094 |
| 1095 if(s->msmpeg4_version==2) | |
| 1096 run_diff = 0; | |
| 1097 else | |
| 1098 run_diff = 1; | |
| 1099 | |
| 0 | 1100 if (!coded) { |
| 1101 s->block_last_index[n] = i - 1; | |
| 1102 return 0; | |
| 1103 } | |
| 1104 scan_table = zigzag_direct; | |
| 1105 set_stat(ST_INTER_AC); | |
| 1106 } | |
| 1107 | |
| 1108 for(;;) { | |
| 1109 code = get_vlc(&s->gb, &rl->vlc); | |
| 1110 if (code < 0) | |
| 1111 return -1; | |
| 1112 if (code == rl->n) { | |
| 1113 /* escape */ | |
| 21 | 1114 if (get_bits1(&s->gb) == 0) { |
| 1115 if (get_bits1(&s->gb) == 0) { | |
| 0 | 1116 /* third escape */ |
| 21 | 1117 last = get_bits1(&s->gb); |
| 0 | 1118 run = get_bits(&s->gb, 6); |
| 1119 level = get_bits(&s->gb, 8); | |
| 1120 level = (level << 24) >> 24; /* sign extend */ | |
| 246 | 1121 //level = level * qmul + (level>0) * qadd - (level<=0) * qadd ; |
| 1122 if (level>0) level= level * qmul + qadd; | |
| 200 | 1123 else level= level * qmul - qadd; |
| 0 | 1124 } else { |
| 1125 /* second escape */ | |
| 1126 code = get_vlc(&s->gb, &rl->vlc); | |
| 1127 if (code < 0 || code >= rl->n) | |
| 1128 return -1; | |
| 1129 run = rl->table_run[code]; | |
| 201 | 1130 level = rl->table_level[code]; |
| 0 | 1131 last = code >= rl->last; |
| 1132 run += rl->max_run[last][level] + run_diff; | |
| 201 | 1133 level= level * qmul + qadd; |
| 21 | 1134 if (get_bits1(&s->gb)) |
| 0 | 1135 level = -level; |
| 1136 } | |
| 1137 } else { | |
| 1138 /* first escape */ | |
| 1139 code = get_vlc(&s->gb, &rl->vlc); | |
| 1140 if (code < 0 || code >= rl->n) | |
| 1141 return -1; | |
| 1142 run = rl->table_run[code]; | |
| 1143 level = rl->table_level[code]; | |
| 1144 last = code >= rl->last; | |
| 1145 level += rl->max_level[last][run]; | |
| 200 | 1146 level= level * qmul + qadd; |
| 21 | 1147 if (get_bits1(&s->gb)) |
| 0 | 1148 level = -level; |
| 1149 } | |
| 1150 } else { | |
| 1151 run = rl->table_run[code]; | |
| 200 | 1152 level = rl->table_level[code] * qmul + qadd; |
| 0 | 1153 last = code >= rl->last; |
| 21 | 1154 if (get_bits1(&s->gb)) |
| 0 | 1155 level = -level; |
| 1156 } | |
| 1157 i += run; | |
| 1158 if (i >= 64) | |
| 1159 return -1; | |
| 1160 j = scan_table[i]; | |
| 1161 block[j] = level; | |
| 1162 i++; | |
| 1163 if (last) | |
| 1164 break; | |
| 1165 } | |
| 1166 not_coded: | |
| 1167 if (s->mb_intra) { | |
| 1168 mpeg4_pred_ac(s, block, n, dc_pred_dir); | |
| 1169 if (s->ac_pred) { | |
| 1170 i = 64; /* XXX: not optimal */ | |
| 1171 } | |
| 1172 } | |
| 1173 s->block_last_index[n] = i - 1; | |
| 1174 | |
| 1175 return 0; | |
| 1176 } | |
| 1177 | |
| 1178 static int msmpeg4_decode_dc(MpegEncContext * s, int n, int *dir_ptr) | |
| 1179 { | |
| 1180 int level, pred; | |
| 25 | 1181 INT16 *dc_val; |
| 0 | 1182 |
| 307 | 1183 if(s->msmpeg4_version==2){ |
| 1184 if (n < 4) { | |
| 1185 level = get_vlc(&s->gb, &v2_dc_lum_vlc); | |
| 1186 } else { | |
| 1187 level = get_vlc(&s->gb, &v2_dc_chroma_vlc); | |
| 1188 } | |
| 309 | 1189 if (level < 0) |
| 307 | 1190 return -1; |
| 1191 level-=256; | |
| 1192 }else{ //FIXME optimize use unified tables & index | |
| 1193 if (n < 4) { | |
| 1194 level = get_vlc(&s->gb, &dc_lum_vlc[s->dc_table_index]); | |
| 1195 } else { | |
| 1196 level = get_vlc(&s->gb, &dc_chroma_vlc[s->dc_table_index]); | |
| 1197 } | |
| 1198 if (level < 0) | |
| 1199 return -1; | |
| 1200 | |
| 1201 if (level == DC_MAX) { | |
| 1202 level = get_bits(&s->gb, 8); | |
| 1203 if (get_bits1(&s->gb)) | |
| 1204 level = -level; | |
| 1205 } else if (level != 0) { | |
| 1206 if (get_bits1(&s->gb)) | |
| 1207 level = -level; | |
| 1208 } | |
| 0 | 1209 } |
| 1210 | |
| 1211 pred = msmpeg4_pred_dc(s, n, &dc_val, dir_ptr); | |
| 1212 level += pred; | |
| 1213 | |
| 1214 /* update predictor */ | |
| 1215 if (n < 4) { | |
| 1216 *dc_val = level * s->y_dc_scale; | |
| 1217 } else { | |
| 1218 *dc_val = level * s->c_dc_scale; | |
| 1219 } | |
| 1220 | |
| 1221 return level; | |
| 1222 } | |
| 1223 | |
| 1224 static int msmpeg4_decode_motion(MpegEncContext * s, | |
| 1225 int *mx_ptr, int *my_ptr) | |
| 1226 { | |
| 1227 MVTable *mv; | |
| 1228 int code, mx, my; | |
| 1229 | |
| 1230 mv = &mv_tables[s->mv_table_index]; | |
| 1231 | |
| 1232 code = get_vlc(&s->gb, &mv->vlc); | |
| 1233 if (code < 0) | |
| 1234 return -1; | |
| 1235 if (code == mv->n) { | |
| 1236 mx = get_bits(&s->gb, 6); | |
| 1237 my = get_bits(&s->gb, 6); | |
| 1238 } else { | |
| 1239 mx = mv->table_mvx[code]; | |
| 1240 my = mv->table_mvy[code]; | |
| 1241 } | |
| 1242 | |
| 1243 mx += *mx_ptr - 32; | |
| 1244 my += *my_ptr - 32; | |
| 1245 /* WARNING : they do not do exactly modulo encoding */ | |
| 1246 if (mx <= -64) | |
| 1247 mx += 64; | |
| 1248 else if (mx >= 64) | |
| 1249 mx -= 64; | |
| 1250 | |
| 1251 if (my <= -64) | |
| 1252 my += 64; | |
| 1253 else if (my >= 64) | |
| 1254 my -= 64; | |
| 1255 *mx_ptr = mx; | |
| 1256 *my_ptr = my; | |
| 1257 return 0; | |
| 1258 } |
