Mercurial > libavcodec.hg
annotate msmpeg4.c @ 307:764aeec1320e libavcodec
msmpeg4v2 decoding (no encoding yet)
| author | michaelni |
|---|---|
| date | Fri, 05 Apr 2002 04:09:04 +0000 |
| parents | d874359e58f1 |
| children | 32485ad4cc4e |
| 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 { | |
|
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
|
371 if (s->qscale < 5){ |
|
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 // 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
|
375 }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
|
376 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
|
377 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
|
378 }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
|
379 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
|
380 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
|
381 } |
|
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
|
382 // this differs for quant >24 from mpeg4 |
|
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
|
383 |
|
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
|
384 // if(s->qscale==13) s->c_dc_scale=14; |
|
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
|
385 |
|
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
|
386 // if(s->qscale>=6) |
|
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
|
387 // printf("%d", 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
|
388 |
|
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
|
389 /* s->c_dc_scale values (found by Michael Nidermayer) |
|
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
|
390 qscale=2 -> 8 (yes iam sure about that) |
|
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
|
391 qscale=3 -> 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
|
392 qscale=4 -> 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
|
393 qscale=5 -> 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
|
394 qscale=6 -> 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
|
395 qscale=7 -> 10 |
|
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
|
396 qscale=8 -> 10 |
|
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
|
397 qscale=9 -> 11 |
|
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
|
398 qscale=10-> 11 |
|
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
|
399 */ |
| 0 | 400 } |
| 401 | |
| 402 /* dir = 0: left, dir = 1: top prediction */ | |
| 403 static int msmpeg4_pred_dc(MpegEncContext * s, int n, | |
| 25 | 404 INT16 **dc_val_ptr, int *dir_ptr) |
| 0 | 405 { |
| 299 | 406 int a, b, c, wrap, pred, scale; |
| 25 | 407 INT16 *dc_val; |
| 0 | 408 |
| 409 /* find prediction */ | |
| 410 if (n < 4) { | |
| 411 scale = s->y_dc_scale; | |
| 412 } else { | |
| 413 scale = s->c_dc_scale; | |
| 414 } | |
| 299 | 415 wrap = s->block_wrap[n]; |
| 416 dc_val= s->dc_val[0] + s->block_index[n]; | |
| 0 | 417 |
| 418 /* B C | |
| 419 * A X | |
| 420 */ | |
| 299 | 421 a = dc_val[ - 1]; |
| 422 b = dc_val[ - 1 - wrap]; | |
| 423 c = dc_val[ - wrap]; | |
| 0 | 424 |
| 425 /* XXX: the following solution consumes divisions, but it does not | |
| 426 necessitate to modify mpegvideo.c. The problem comes from the | |
| 427 fact they decided to store the quantized DC (which would lead | |
| 428 to problems if Q could vary !) */ | |
| 221 | 429 #if defined ARCH_X86 && !defined PIC |
| 204 | 430 asm volatile( |
| 431 "movl %3, %%eax \n\t" | |
| 432 "shrl $1, %%eax \n\t" | |
| 433 "addl %%eax, %2 \n\t" | |
| 434 "addl %%eax, %1 \n\t" | |
| 435 "addl %0, %%eax \n\t" | |
|
225
ae145876789d
use multiply instead of divides for DC prediction on X86
michaelni
parents:
221
diff
changeset
|
436 "mull %4 \n\t" |
|
ae145876789d
use multiply instead of divides for DC prediction on X86
michaelni
parents:
221
diff
changeset
|
437 "movl %%edx, %0 \n\t" |
| 204 | 438 "movl %1, %%eax \n\t" |
|
225
ae145876789d
use multiply instead of divides for DC prediction on X86
michaelni
parents:
221
diff
changeset
|
439 "mull %4 \n\t" |
|
ae145876789d
use multiply instead of divides for DC prediction on X86
michaelni
parents:
221
diff
changeset
|
440 "movl %%edx, %1 \n\t" |
| 204 | 441 "movl %2, %%eax \n\t" |
|
225
ae145876789d
use multiply instead of divides for DC prediction on X86
michaelni
parents:
221
diff
changeset
|
442 "mull %4 \n\t" |
|
ae145876789d
use multiply instead of divides for DC prediction on X86
michaelni
parents:
221
diff
changeset
|
443 "movl %%edx, %2 \n\t" |
| 228 | 444 : "+b" (a), "+c" (b), "+D" (c) |
| 445 : "g" (scale), "S" (inverse[scale]) | |
| 204 | 446 : "%eax", "%edx" |
| 447 ); | |
| 221 | 448 #else |
| 449 /* #elif defined (ARCH_ALPHA) */ | |
|
214
73df666cacc7
Alpha optimizations by Falk Hueffner <falk.hueffner@student.uni-tuebingen.de>
nickols_k
parents:
208
diff
changeset
|
450 /* Divisions are extremely costly on Alpha; optimize the most |
| 221 | 451 common case. But they are costly everywhere... |
| 452 */ | |
|
214
73df666cacc7
Alpha optimizations by Falk Hueffner <falk.hueffner@student.uni-tuebingen.de>
nickols_k
parents:
208
diff
changeset
|
453 if (scale == 8) { |
|
73df666cacc7
Alpha optimizations by Falk Hueffner <falk.hueffner@student.uni-tuebingen.de>
nickols_k
parents:
208
diff
changeset
|
454 a = (a + (8 >> 1)) / 8; |
|
73df666cacc7
Alpha optimizations by Falk Hueffner <falk.hueffner@student.uni-tuebingen.de>
nickols_k
parents:
208
diff
changeset
|
455 b = (b + (8 >> 1)) / 8; |
|
73df666cacc7
Alpha optimizations by Falk Hueffner <falk.hueffner@student.uni-tuebingen.de>
nickols_k
parents:
208
diff
changeset
|
456 c = (c + (8 >> 1)) / 8; |
|
73df666cacc7
Alpha optimizations by Falk Hueffner <falk.hueffner@student.uni-tuebingen.de>
nickols_k
parents:
208
diff
changeset
|
457 } else { |
|
73df666cacc7
Alpha optimizations by Falk Hueffner <falk.hueffner@student.uni-tuebingen.de>
nickols_k
parents:
208
diff
changeset
|
458 a = (a + (scale >> 1)) / scale; |
|
73df666cacc7
Alpha optimizations by Falk Hueffner <falk.hueffner@student.uni-tuebingen.de>
nickols_k
parents:
208
diff
changeset
|
459 b = (b + (scale >> 1)) / scale; |
|
73df666cacc7
Alpha optimizations by Falk Hueffner <falk.hueffner@student.uni-tuebingen.de>
nickols_k
parents:
208
diff
changeset
|
460 c = (c + (scale >> 1)) / scale; |
|
73df666cacc7
Alpha optimizations by Falk Hueffner <falk.hueffner@student.uni-tuebingen.de>
nickols_k
parents:
208
diff
changeset
|
461 } |
| 204 | 462 #endif |
| 0 | 463 /* XXX: WARNING: they did not choose the same test as MPEG4. This |
| 464 is very important ! */ | |
| 465 if (abs(a - b) <= abs(b - c)) { | |
| 466 pred = c; | |
| 467 *dir_ptr = 1; | |
| 468 } else { | |
| 469 pred = a; | |
| 470 *dir_ptr = 0; | |
| 471 } | |
| 472 | |
| 473 /* update predictor */ | |
| 299 | 474 *dc_val_ptr = &dc_val[0]; |
| 0 | 475 return pred; |
| 476 } | |
| 477 | |
| 478 #define DC_MAX 119 | |
| 479 | |
| 480 static void msmpeg4_encode_dc(MpegEncContext * s, int level, int n, int *dir_ptr) | |
| 481 { | |
| 482 int sign, code; | |
| 483 int pred; | |
| 25 | 484 INT16 *dc_val; |
| 0 | 485 |
| 486 pred = msmpeg4_pred_dc(s, n, &dc_val, dir_ptr); | |
| 487 | |
| 488 /* update predictor */ | |
| 489 if (n < 4) { | |
| 490 *dc_val = level * s->y_dc_scale; | |
| 491 } else { | |
| 492 *dc_val = level * s->c_dc_scale; | |
| 493 } | |
| 494 | |
| 495 /* do the prediction */ | |
| 496 level -= pred; | |
| 497 | |
| 498 sign = 0; | |
| 499 if (level < 0) { | |
| 500 level = -level; | |
| 501 sign = 1; | |
| 502 } | |
| 503 | |
| 504 code = level; | |
| 505 if (code > DC_MAX) | |
| 506 code = DC_MAX; | |
| 507 | |
| 508 if (s->dc_table_index == 0) { | |
| 509 if (n < 4) { | |
| 510 put_bits(&s->pb, table0_dc_lum[code][1], table0_dc_lum[code][0]); | |
| 511 } else { | |
| 512 put_bits(&s->pb, table0_dc_chroma[code][1], table0_dc_chroma[code][0]); | |
| 513 } | |
| 514 } else { | |
| 515 if (n < 4) { | |
| 516 put_bits(&s->pb, table1_dc_lum[code][1], table1_dc_lum[code][0]); | |
| 517 } else { | |
| 518 put_bits(&s->pb, table1_dc_chroma[code][1], table1_dc_chroma[code][0]); | |
| 519 } | |
| 520 } | |
| 521 | |
| 522 if (code == DC_MAX) | |
| 523 put_bits(&s->pb, 8, level); | |
| 524 | |
| 525 if (level != 0) { | |
| 526 put_bits(&s->pb, 1, sign); | |
| 527 } | |
| 528 } | |
| 529 | |
| 530 /* Encoding of a block. Very similar to MPEG4 except for a different | |
| 531 escape coding (same as H263) and more vlc tables. | |
| 532 */ | |
| 533 static void msmpeg4_encode_block(MpegEncContext * s, DCTELEM * block, int n) | |
| 534 { | |
| 535 int level, run, last, i, j, last_index; | |
| 536 int last_non_zero, sign, slevel; | |
| 537 int code, run_diff, dc_pred_dir; | |
| 538 const RLTable *rl; | |
| 539 | |
| 540 if (s->mb_intra) { | |
| 541 set_stat(ST_DC); | |
| 542 msmpeg4_encode_dc(s, block[0], n, &dc_pred_dir); | |
| 543 i = 1; | |
| 544 if (n < 4) { | |
| 545 rl = &rl_table[s->rl_table_index]; | |
| 546 } else { | |
| 547 rl = &rl_table[3 + s->rl_chroma_table_index]; | |
| 548 } | |
| 549 run_diff = 0; | |
| 550 set_stat(ST_INTRA_AC); | |
| 551 } else { | |
| 552 i = 0; | |
| 553 rl = &rl_table[3 + s->rl_table_index]; | |
| 554 run_diff = 1; | |
| 555 set_stat(ST_INTER_AC); | |
| 556 } | |
| 557 | |
| 558 /* AC coefs */ | |
| 559 last_index = s->block_last_index[n]; | |
| 560 last_non_zero = i - 1; | |
| 561 for (; i <= last_index; i++) { | |
| 562 j = zigzag_direct[i]; | |
| 563 level = block[j]; | |
| 564 if (level) { | |
| 565 run = i - last_non_zero - 1; | |
| 566 last = (i == last_index); | |
| 567 sign = 0; | |
| 568 slevel = level; | |
| 569 if (level < 0) { | |
| 570 sign = 1; | |
| 571 level = -level; | |
| 572 } | |
| 573 code = get_rl_index(rl, last, run, level); | |
| 574 put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]); | |
| 575 if (code == rl->n) { | |
| 576 int level1, run1; | |
| 577 | |
| 578 level1 = level - rl->max_level[last][run]; | |
| 579 if (level1 < 1) | |
| 580 goto esc2; | |
| 581 code = get_rl_index(rl, last, run, level1); | |
| 582 if (code == rl->n) { | |
| 583 esc2: | |
| 584 put_bits(&s->pb, 1, 0); | |
| 585 if (level > MAX_LEVEL) | |
| 586 goto esc3; | |
| 587 run1 = run - rl->max_run[last][level] - run_diff; | |
| 588 if (run1 < 0) | |
| 589 goto esc3; | |
| 590 code = get_rl_index(rl, last, run1, level); | |
| 591 if (code == rl->n) { | |
| 592 esc3: | |
| 593 /* third escape */ | |
| 594 put_bits(&s->pb, 1, 0); | |
| 595 put_bits(&s->pb, 1, last); | |
| 596 put_bits(&s->pb, 6, run); | |
| 597 put_bits(&s->pb, 8, slevel & 0xff); | |
| 598 } else { | |
| 599 /* second escape */ | |
| 600 put_bits(&s->pb, 1, 1); | |
| 601 put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]); | |
| 602 put_bits(&s->pb, 1, sign); | |
| 603 } | |
| 604 } else { | |
| 605 /* first escape */ | |
| 606 put_bits(&s->pb, 1, 1); | |
| 607 put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]); | |
| 608 put_bits(&s->pb, 1, sign); | |
| 609 } | |
| 610 } else { | |
| 611 put_bits(&s->pb, 1, sign); | |
| 612 } | |
| 613 last_non_zero = i; | |
| 614 } | |
| 615 } | |
| 616 } | |
| 617 | |
| 618 /****************************************/ | |
| 619 /* decoding stuff */ | |
| 620 | |
| 621 static VLC mb_non_intra_vlc; | |
| 622 static VLC mb_intra_vlc; | |
| 623 static VLC dc_lum_vlc[2]; | |
| 624 static VLC dc_chroma_vlc[2]; | |
| 307 | 625 static VLC v2_dc_lum_vlc; |
| 626 static VLC v2_dc_chroma_vlc; | |
| 627 static VLC cbpy_vlc; | |
| 628 static VLC v2_intra_cbpc_vlc; | |
| 629 static VLC v2_mb_type_vlc; | |
| 630 static VLC v2_mv_vlc; | |
| 631 | |
| 632 /* this table is practically identical to the one from h263 except that its inverted */ | |
| 633 static void init_h263_dc_for_msmpeg4() | |
| 634 { | |
| 635 static int inited=0; | |
| 636 | |
| 637 if(!inited){ | |
| 638 int level, uni_code, uni_len; | |
| 639 inited=1; | |
| 640 | |
| 641 for(level=-255; level<256; level++){ | |
| 642 int size, v, l; | |
| 643 /* find number of bits */ | |
| 644 size = 0; | |
| 645 v = abs(level); | |
| 646 while (v) { | |
| 647 v >>= 1; | |
| 648 size++; | |
| 649 } | |
| 650 | |
| 651 if (level < 0) | |
| 652 l= (-level) ^ ((1 << size) - 1); | |
| 653 else | |
| 654 l= level; | |
| 655 | |
| 656 /* luminance h263 */ | |
| 657 uni_code= DCtab_lum[size][0]; | |
| 658 uni_len = DCtab_lum[size][1]; | |
| 659 uni_code ^= (1<<uni_len)-1; //M$ doesnt like compatibility | |
| 660 | |
| 661 if (size > 0) { | |
| 662 uni_code<<=size; uni_code|=l; | |
| 663 uni_len+=size; | |
| 664 if (size > 8){ | |
| 665 uni_code<<=1; uni_code|=1; | |
| 666 uni_len++; | |
| 667 } | |
| 668 } | |
| 669 v2_dc_lum_table[level+256][0]= uni_code; | |
| 670 v2_dc_lum_table[level+256][1]= uni_len; | |
| 671 | |
| 672 /* chrominance h263 */ | |
| 673 uni_code= DCtab_chrom[size][0]; | |
| 674 uni_len = DCtab_chrom[size][1]; | |
| 675 uni_code ^= (1<<uni_len)-1; //M$ doesnt like compatibility | |
| 676 | |
| 677 if (size > 0) { | |
| 678 uni_code<<=size; uni_code|=l; | |
| 679 uni_len+=size; | |
| 680 if (size > 8){ | |
| 681 uni_code<<=1; uni_code|=1; | |
| 682 uni_len++; | |
| 683 } | |
| 684 } | |
| 685 v2_dc_chroma_table[level+256][0]= uni_code; | |
| 686 v2_dc_chroma_table[level+256][1]= uni_len; | |
| 687 | |
| 688 } | |
| 689 } | |
| 690 } | |
| 0 | 691 |
| 692 /* init all vlc decoding tables */ | |
| 693 int msmpeg4_decode_init_vlc(MpegEncContext *s) | |
| 694 { | |
| 695 int i; | |
| 696 MVTable *mv; | |
| 697 | |
| 698 for(i=0;i<NB_RL_TABLES;i++) { | |
| 699 init_rl(&rl_table[i]); | |
| 700 init_vlc_rl(&rl_table[i]); | |
| 701 } | |
| 702 for(i=0;i<2;i++) { | |
| 703 mv = &mv_tables[i]; | |
| 704 init_vlc(&mv->vlc, 9, mv->n + 1, | |
| 705 mv->table_mv_bits, 1, 1, | |
| 706 mv->table_mv_code, 2, 2); | |
| 707 } | |
| 708 | |
| 709 init_vlc(&dc_lum_vlc[0], 9, 120, | |
| 710 &table0_dc_lum[0][1], 8, 4, | |
| 711 &table0_dc_lum[0][0], 8, 4); | |
| 712 init_vlc(&dc_chroma_vlc[0], 9, 120, | |
| 713 &table0_dc_chroma[0][1], 8, 4, | |
| 714 &table0_dc_chroma[0][0], 8, 4); | |
| 715 init_vlc(&dc_lum_vlc[1], 9, 120, | |
| 716 &table1_dc_lum[0][1], 8, 4, | |
| 717 &table1_dc_lum[0][0], 8, 4); | |
| 718 init_vlc(&dc_chroma_vlc[1], 9, 120, | |
| 719 &table1_dc_chroma[0][1], 8, 4, | |
| 720 &table1_dc_chroma[0][0], 8, 4); | |
| 307 | 721 |
| 722 init_h263_dc_for_msmpeg4(); | |
| 723 init_vlc(&v2_dc_lum_vlc, 9, 512, | |
| 724 &v2_dc_lum_table[0][1], 8, 4, | |
| 725 &v2_dc_lum_table[0][0], 8, 4); | |
| 726 init_vlc(&v2_dc_chroma_vlc, 9, 512, | |
| 727 &v2_dc_chroma_table[0][1], 8, 4, | |
| 728 &v2_dc_chroma_table[0][0], 8, 4); | |
| 729 | |
| 730 init_vlc(&cbpy_vlc, 6, 16, | |
| 731 &cbpy_tab[0][1], 2, 1, | |
| 732 &cbpy_tab[0][0], 2, 1); | |
| 733 init_vlc(&v2_intra_cbpc_vlc, 3, 4, | |
| 734 &v2_intra_cbpc[0][1], 2, 1, | |
| 735 &v2_intra_cbpc[0][0], 2, 1); | |
| 736 init_vlc(&v2_mb_type_vlc, 5, 8, | |
| 737 &v2_mb_type[0][1], 2, 1, | |
| 738 &v2_mb_type[0][0], 2, 1); | |
| 739 init_vlc(&v2_mv_vlc, 9, 33, | |
| 740 &mvtab[0][1], 2, 1, | |
| 741 &mvtab[0][0], 2, 1); | |
| 0 | 742 |
| 743 init_vlc(&mb_non_intra_vlc, 9, 128, | |
| 744 &table_mb_non_intra[0][1], 8, 4, | |
| 745 &table_mb_non_intra[0][0], 8, 4); | |
| 48 | 746 init_vlc(&mb_intra_vlc, 9, 64, |
| 0 | 747 &table_mb_intra[0][1], 4, 2, |
| 748 &table_mb_intra[0][0], 4, 2); | |
| 749 return 0; | |
| 750 } | |
| 751 | |
| 752 static int decode012(GetBitContext *gb) | |
| 753 { | |
| 754 int n; | |
| 21 | 755 n = get_bits1(gb); |
| 0 | 756 if (n == 0) |
| 757 return 0; | |
| 758 else | |
| 21 | 759 return get_bits1(gb) + 1; |
| 0 | 760 } |
| 761 | |
| 307 | 762 int msmpeg4_decode_picture_header(MpegEncContext * s) |
|
300
d874359e58f1
msmpeg4v2 header parser & some dump bits code behind #if 0
michaelni
parents:
299
diff
changeset
|
763 { |
|
d874359e58f1
msmpeg4v2 header parser & some dump bits code behind #if 0
michaelni
parents:
299
diff
changeset
|
764 int code; |
|
d874359e58f1
msmpeg4v2 header parser & some dump bits code behind #if 0
michaelni
parents:
299
diff
changeset
|
765 |
|
d874359e58f1
msmpeg4v2 header parser & some dump bits code behind #if 0
michaelni
parents:
299
diff
changeset
|
766 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
|
767 if (s->pict_type != I_TYPE && |
|
d874359e58f1
msmpeg4v2 header parser & some dump bits code behind #if 0
michaelni
parents:
299
diff
changeset
|
768 s->pict_type != P_TYPE) |
|
d874359e58f1
msmpeg4v2 header parser & some dump bits code behind #if 0
michaelni
parents:
299
diff
changeset
|
769 return -1; |
|
d874359e58f1
msmpeg4v2 header parser & some dump bits code behind #if 0
michaelni
parents:
299
diff
changeset
|
770 |
|
d874359e58f1
msmpeg4v2 header parser & some dump bits code behind #if 0
michaelni
parents:
299
diff
changeset
|
771 s->qscale = get_bits(&s->gb, 5); |
|
d874359e58f1
msmpeg4v2 header parser & some dump bits code behind #if 0
michaelni
parents:
299
diff
changeset
|
772 |
|
d874359e58f1
msmpeg4v2 header parser & some dump bits code behind #if 0
michaelni
parents:
299
diff
changeset
|
773 if (s->pict_type == I_TYPE) { |
|
d874359e58f1
msmpeg4v2 header parser & some dump bits code behind #if 0
michaelni
parents:
299
diff
changeset
|
774 code = get_bits(&s->gb, 5); |
| 307 | 775 /* 0x17: one slice, 0x18: two slices */ |
|
300
d874359e58f1
msmpeg4v2 header parser & some dump bits code behind #if 0
michaelni
parents:
299
diff
changeset
|
776 if (code < 0x17) |
|
d874359e58f1
msmpeg4v2 header parser & some dump bits code behind #if 0
michaelni
parents:
299
diff
changeset
|
777 return -1; |
|
d874359e58f1
msmpeg4v2 header parser & some dump bits code behind #if 0
michaelni
parents:
299
diff
changeset
|
778 s->slice_height = s->mb_height / (code - 0x16); |
| 307 | 779 if(s->msmpeg4_version==2){ |
| 780 s->rl_chroma_table_index = 2; | |
| 781 s->rl_table_index = 2; | |
|
300
d874359e58f1
msmpeg4v2 header parser & some dump bits code behind #if 0
michaelni
parents:
299
diff
changeset
|
782 |
| 307 | 783 s->dc_table_index = 0; //not used |
| 784 }else{ | |
| 785 s->rl_chroma_table_index = decode012(&s->gb); | |
| 786 s->rl_table_index = decode012(&s->gb); | |
| 0 | 787 |
| 307 | 788 s->dc_table_index = get_bits1(&s->gb); |
| 789 } | |
| 0 | 790 s->no_rounding = 1; |
| 200 | 791 /* printf(" %d %d %d %d \n", |
| 792 s->qscale, | |
| 793 s->rl_chroma_table_index, | |
| 794 s->rl_table_index, | |
| 795 s->dc_table_index);*/ | |
| 0 | 796 } else { |
| 21 | 797 s->use_skip_mb_code = get_bits1(&s->gb); |
| 0 | 798 |
| 307 | 799 if(s->msmpeg4_version==2){ |
| 800 s->rl_table_index = 2; | |
| 801 s->rl_chroma_table_index = s->rl_table_index; | |
| 802 | |
| 803 s->dc_table_index = 0; //not used | |
| 0 | 804 |
| 307 | 805 s->mv_table_index = 0; |
| 806 }else{ | |
| 807 s->rl_table_index = decode012(&s->gb); | |
| 808 s->rl_chroma_table_index = s->rl_table_index; | |
| 0 | 809 |
| 307 | 810 s->dc_table_index = get_bits1(&s->gb); |
| 811 | |
| 812 s->mv_table_index = get_bits1(&s->gb); | |
| 813 } | |
| 200 | 814 /* printf(" %d %d %d %d %d \n", |
| 815 s->use_skip_mb_code, | |
| 816 s->rl_table_index, | |
| 817 s->rl_chroma_table_index, | |
| 818 s->dc_table_index, | |
| 819 s->mv_table_index);*/ | |
| 208 | 820 if(s->flipflop_rounding){ |
| 821 s->no_rounding ^= 1; | |
| 822 }else{ | |
| 823 s->no_rounding = 0; | |
| 824 } | |
| 825 // printf("%d", s->no_rounding); | |
| 0 | 826 } |
| 208 | 827 |
| 307 | 828 #if 0 |
| 829 if(s->msmpeg4_version==2) | |
| 830 { | |
| 831 int i; | |
| 832 printf("%s q:%d s:%X ", s->pict_type == I_TYPE ? "I" : "P" , s->qscale, | |
| 833 s->pict_type == I_TYPE ? code : s->use_skip_mb_code); | |
| 834 for(i=0; i<s->gb.size*8; i++) | |
| 835 // printf("%d", get_bits1(&s->gb)); | |
| 836 get_bits1(&s->gb); | |
| 837 printf("END\n"); | |
| 838 return -1; | |
| 839 } | |
| 840 #endif | |
| 841 | |
| 0 | 842 #ifdef DEBUG |
| 843 printf("*****frame %d:\n", frame_count++); | |
| 844 #endif | |
| 845 return 0; | |
| 846 } | |
| 847 | |
| 208 | 848 int msmpeg4_decode_ext_header(MpegEncContext * s, int buf_size) |
| 849 { | |
| 850 /* 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
|
851 if(get_bits_count(&s->gb) + 16 < buf_size*8) |
| 208 | 852 { |
|
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
|
853 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
|
854 |
|
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
|
855 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
|
856 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
|
857 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
|
858 |
|
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
|
859 // printf("fps:%2d bps:%2d roundingType:%1d\n", fps, s->bitrate, s->flipflop_rounding); |
| 208 | 860 } |
| 861 else | |
| 862 { | |
|
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
|
863 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
|
864 s->bitrate= 0; |
| 208 | 865 } |
|
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
|
866 |
| 208 | 867 return 0; |
| 868 } | |
| 869 | |
| 246 | 870 static inline void memsetw(short *tab, int val, int n) |
| 0 | 871 { |
| 872 int i; | |
| 873 for(i=0;i<n;i++) | |
| 874 tab[i] = val; | |
| 875 } | |
| 876 | |
| 307 | 877 /* this is identical to h263 except that its range is multiplied by 2 */ |
| 878 static int msmpeg4v2_decode_motion(MpegEncContext * s, int pred, int f_code) | |
| 879 { | |
| 880 int code, val, sign, shift; | |
| 881 | |
| 882 code = get_vlc(&s->gb, &v2_mv_vlc); | |
| 883 if (code < 0) | |
| 884 return 0xffff; | |
| 885 | |
| 886 if (code == 0) | |
| 887 return pred; | |
| 888 sign = get_bits1(&s->gb); | |
| 889 shift = f_code - 1; | |
| 890 val = (code - 1) << shift; | |
| 891 if (shift > 0) | |
| 892 val |= get_bits(&s->gb, shift); | |
| 893 val++; | |
| 894 if (sign) | |
| 895 val = -val; | |
| 896 val += pred; | |
| 897 | |
| 898 if (val <= -64) | |
| 899 val += 64; | |
| 900 else if (val >= 64) | |
| 901 val -= 64; | |
| 902 | |
| 903 return val; | |
| 904 } | |
| 905 | |
| 906 | |
| 907 int msmpeg4v2_decode_mb(MpegEncContext *s, | |
| 908 DCTELEM block[6][64]) | |
| 909 { | |
| 910 int cbp, code, i; | |
| 911 //printf("M"); | |
| 912 //fflush(stderr); fflush(stdout); | |
| 913 if (s->pict_type == P_TYPE) { | |
| 914 if (s->use_skip_mb_code) { | |
| 915 if (get_bits1(&s->gb)) { | |
| 916 /* skip mb */ | |
| 917 s->mb_intra = 0; | |
| 918 for(i=0;i<6;i++) | |
| 919 s->block_last_index[i] = -1; | |
| 920 s->mv_dir = MV_DIR_FORWARD; | |
| 921 s->mv_type = MV_TYPE_16X16; | |
| 922 s->mv[0][0][0] = 0; | |
| 923 s->mv[0][0][1] = 0; | |
| 924 s->mb_skiped = 1; | |
| 925 return 0; | |
| 926 } | |
| 927 } | |
| 928 | |
| 929 code = get_vlc(&s->gb, &v2_mb_type_vlc); | |
| 930 s->mb_intra = code >>2; | |
| 931 | |
| 932 cbp = code & 0x3; | |
| 933 } else { | |
| 934 s->mb_intra = 1; | |
| 935 //printf("%X ", show_bits(&s->gb, 24)); | |
| 936 cbp= get_vlc(&s->gb, &v2_intra_cbpc_vlc); | |
| 937 //printf("%X ", show_bits(&s->gb, 24)); | |
| 938 //printf("CBP: %X ",cbp); | |
| 939 } | |
| 940 | |
| 941 if (!s->mb_intra) { | |
| 942 int mx, my; | |
| 943 | |
| 944 cbp|= get_vlc(&s->gb, &cbpy_vlc)<<2; | |
| 945 if((cbp&3) != 3) cbp^= 0x3C; | |
| 946 | |
| 947 h263_pred_motion(s, 0, &mx, &my); | |
| 948 mx= msmpeg4v2_decode_motion(s, mx, 1); | |
| 949 my= msmpeg4v2_decode_motion(s, my, 1); | |
| 950 | |
| 951 s->mv_dir = MV_DIR_FORWARD; | |
| 952 s->mv_type = MV_TYPE_16X16; | |
| 953 s->mv[0][0][0] = mx; | |
| 954 s->mv[0][0][1] = my; | |
| 955 } else { | |
| 956 //if(s->pict_type == P_TYPE) printf("intra cbp: %X", cbp); | |
| 957 s->ac_pred = get_bits1(&s->gb); | |
| 958 //printf("AC: %d ",s->ac_pred); | |
| 959 cbp|= get_vlc(&s->gb, &cbpy_vlc)<<2; | |
| 960 //printf("cbp: %X ",cbp); | |
| 961 } | |
| 962 | |
| 963 for (i = 0; i < 6; i++) { | |
| 964 if (msmpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1) < 0) | |
| 965 { | |
| 966 //fflush(stderr); fflush(stdout); | |
| 967 fprintf(stderr,"\nIgnoring error while decoding block: %d x %d (%d)\n", s->mb_x, s->mb_y, i); | |
| 968 return -1; | |
| 969 } | |
| 970 } | |
| 971 return 0; | |
| 972 } | |
| 973 | |
| 0 | 974 int msmpeg4_decode_mb(MpegEncContext *s, |
| 975 DCTELEM block[6][64]) | |
| 976 { | |
| 977 int cbp, code, i; | |
| 978 UINT8 *coded_val; | |
| 979 | |
| 980 /* special slice handling */ | |
| 981 if (s->mb_x == 0) { | |
| 122 | 982 if (s->slice_height && (s->mb_y % s->slice_height) == 0) { |
| 0 | 983 int wrap; |
| 984 /* reset DC pred (set previous line to 1024) */ | |
| 985 wrap = 2 * s->mb_width + 2; | |
| 986 memsetw(&s->dc_val[0][(1) + (2 * s->mb_y) * wrap], | |
| 987 1024, 2 * s->mb_width); | |
| 988 wrap = s->mb_width + 2; | |
| 989 memsetw(&s->dc_val[1][(1) + (s->mb_y) * wrap], | |
| 990 1024, s->mb_width); | |
| 991 memsetw(&s->dc_val[2][(1) + (s->mb_y) * wrap], | |
| 992 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
|
993 |
|
cf37da86d990
fix slices when code=0x18, patch by Michael Niedermayer <michael@mplayer.dev.hu>
arpi_esp
parents:
122
diff
changeset
|
994 /* 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
|
995 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
|
996 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
|
997 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
|
998 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
|
999 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
|
1000 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
|
1001 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
|
1002 0, s->mb_width*16); |
| 0 | 1003 |
| 1004 s->first_slice_line = 1; | |
| 1005 } else { | |
| 1006 s->first_slice_line = 0; | |
| 1007 } | |
| 1008 } | |
| 1009 | |
| 307 | 1010 if(s->msmpeg4_version==2) return msmpeg4v2_decode_mb(s, block); //FIXME merge if possible |
| 1011 | |
| 0 | 1012 if (s->pict_type == P_TYPE) { |
| 1013 set_stat(ST_INTER_MB); | |
| 1014 if (s->use_skip_mb_code) { | |
| 21 | 1015 if (get_bits1(&s->gb)) { |
| 0 | 1016 /* skip mb */ |
| 1017 s->mb_intra = 0; | |
| 1018 for(i=0;i<6;i++) | |
| 1019 s->block_last_index[i] = -1; | |
| 1020 s->mv_dir = MV_DIR_FORWARD; | |
| 1021 s->mv_type = MV_TYPE_16X16; | |
| 1022 s->mv[0][0][0] = 0; | |
| 1023 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
|
1024 s->mb_skiped = 1; |
| 0 | 1025 return 0; |
| 1026 } | |
| 1027 } | |
| 1028 | |
| 1029 code = get_vlc(&s->gb, &mb_non_intra_vlc); | |
| 1030 if (code < 0) | |
| 1031 return -1; | |
| 246 | 1032 //s->mb_intra = (code & 0x40) ? 0 : 1; |
| 1033 s->mb_intra = (~code & 0x40) >> 6; | |
| 0 | 1034 |
| 1035 cbp = code & 0x3f; | |
| 1036 } else { | |
| 1037 set_stat(ST_INTRA_MB); | |
| 1038 s->mb_intra = 1; | |
| 1039 code = get_vlc(&s->gb, &mb_intra_vlc); | |
| 1040 if (code < 0) | |
| 1041 return -1; | |
| 1042 /* predict coded block pattern */ | |
| 1043 cbp = 0; | |
| 1044 for(i=0;i<6;i++) { | |
| 246 | 1045 int val = ((code >> (5 - i)) & 1); |
| 0 | 1046 if (i < 4) { |
| 246 | 1047 int pred = coded_block_pred(s, i, &coded_val); |
| 0 | 1048 val = val ^ pred; |
| 1049 *coded_val = val; | |
| 1050 } | |
| 1051 cbp |= val << (5 - i); | |
| 1052 } | |
| 1053 } | |
| 1054 | |
| 1055 if (!s->mb_intra) { | |
| 1056 int mx, my; | |
| 1057 set_stat(ST_MV); | |
| 1058 h263_pred_motion(s, 0, &mx, &my); | |
| 1059 if (msmpeg4_decode_motion(s, &mx, &my) < 0) | |
| 1060 return -1; | |
| 1061 s->mv_dir = MV_DIR_FORWARD; | |
| 1062 s->mv_type = MV_TYPE_16X16; | |
| 1063 s->mv[0][0][0] = mx; | |
| 1064 s->mv[0][0][1] = my; | |
| 1065 } else { | |
| 1066 set_stat(ST_INTRA_MB); | |
| 21 | 1067 s->ac_pred = get_bits1(&s->gb); |
| 0 | 1068 } |
| 1069 | |
| 1070 for (i = 0; i < 6; i++) { | |
| 1071 if (msmpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1) < 0) | |
| 246 | 1072 { |
| 1073 fprintf(stderr,"\nIgnoring error while decoding block: %d x %d (%d)\n", s->mb_x, s->mb_y, i); | |
| 1074 // return -1; | |
| 1075 } | |
| 0 | 1076 } |
| 1077 return 0; | |
| 1078 } | |
| 1079 | |
| 1080 static int msmpeg4_decode_block(MpegEncContext * s, DCTELEM * block, | |
| 1081 int n, int coded) | |
| 1082 { | |
| 1083 int code, level, i, j, last, run, run_diff; | |
| 1084 int dc_pred_dir; | |
| 1085 RLTable *rl; | |
| 1086 const UINT8 *scan_table; | |
| 200 | 1087 int qmul, qadd; |
| 0 | 1088 |
| 1089 if (s->mb_intra) { | |
| 200 | 1090 qmul=1; |
| 1091 qadd=0; | |
| 1092 | |
| 0 | 1093 /* DC coef */ |
| 1094 set_stat(ST_DC); | |
| 1095 level = msmpeg4_decode_dc(s, n, &dc_pred_dir); | |
| 1096 if (level < 0) | |
| 1097 return -1; | |
| 1098 block[0] = level; | |
| 1099 if (n < 4) { | |
| 1100 rl = &rl_table[s->rl_table_index]; | |
| 1101 } else { | |
| 1102 rl = &rl_table[3 + s->rl_chroma_table_index]; | |
| 1103 } | |
| 200 | 1104 |
| 0 | 1105 run_diff = 0; |
| 1106 i = 1; | |
| 1107 if (!coded) { | |
| 1108 goto not_coded; | |
| 1109 } | |
| 1110 if (s->ac_pred) { | |
| 1111 if (dc_pred_dir == 0) | |
| 1112 scan_table = ff_alternate_vertical_scan; /* left */ | |
| 1113 else | |
| 1114 scan_table = ff_alternate_horizontal_scan; /* top */ | |
| 1115 } else { | |
| 1116 scan_table = zigzag_direct; | |
| 1117 } | |
| 1118 set_stat(ST_INTRA_AC); | |
| 1119 } else { | |
| 200 | 1120 qmul = s->qscale << 1; |
| 1121 qadd = (s->qscale - 1) | 1; | |
| 0 | 1122 i = 0; |
| 1123 rl = &rl_table[3 + s->rl_table_index]; | |
| 307 | 1124 |
| 1125 if(s->msmpeg4_version==2) | |
| 1126 run_diff = 0; | |
| 1127 else | |
| 1128 run_diff = 1; | |
| 1129 | |
| 0 | 1130 if (!coded) { |
| 1131 s->block_last_index[n] = i - 1; | |
| 1132 return 0; | |
| 1133 } | |
| 1134 scan_table = zigzag_direct; | |
| 1135 set_stat(ST_INTER_AC); | |
| 1136 } | |
| 1137 | |
| 1138 for(;;) { | |
| 1139 code = get_vlc(&s->gb, &rl->vlc); | |
| 1140 if (code < 0) | |
| 1141 return -1; | |
| 1142 if (code == rl->n) { | |
| 1143 /* escape */ | |
| 21 | 1144 if (get_bits1(&s->gb) == 0) { |
| 1145 if (get_bits1(&s->gb) == 0) { | |
| 0 | 1146 /* third escape */ |
| 21 | 1147 last = get_bits1(&s->gb); |
| 0 | 1148 run = get_bits(&s->gb, 6); |
| 1149 level = get_bits(&s->gb, 8); | |
| 1150 level = (level << 24) >> 24; /* sign extend */ | |
| 246 | 1151 //level = level * qmul + (level>0) * qadd - (level<=0) * qadd ; |
| 1152 if (level>0) level= level * qmul + qadd; | |
| 200 | 1153 else level= level * qmul - qadd; |
| 0 | 1154 } else { |
| 1155 /* second escape */ | |
| 1156 code = get_vlc(&s->gb, &rl->vlc); | |
| 1157 if (code < 0 || code >= rl->n) | |
| 1158 return -1; | |
| 1159 run = rl->table_run[code]; | |
| 201 | 1160 level = rl->table_level[code]; |
| 0 | 1161 last = code >= rl->last; |
| 1162 run += rl->max_run[last][level] + run_diff; | |
| 201 | 1163 level= level * qmul + qadd; |
| 21 | 1164 if (get_bits1(&s->gb)) |
| 0 | 1165 level = -level; |
| 1166 } | |
| 1167 } else { | |
| 1168 /* first escape */ | |
| 1169 code = get_vlc(&s->gb, &rl->vlc); | |
| 1170 if (code < 0 || code >= rl->n) | |
| 1171 return -1; | |
| 1172 run = rl->table_run[code]; | |
| 1173 level = rl->table_level[code]; | |
| 1174 last = code >= rl->last; | |
| 1175 level += rl->max_level[last][run]; | |
| 200 | 1176 level= level * qmul + qadd; |
| 21 | 1177 if (get_bits1(&s->gb)) |
| 0 | 1178 level = -level; |
| 1179 } | |
| 1180 } else { | |
| 1181 run = rl->table_run[code]; | |
| 200 | 1182 level = rl->table_level[code] * qmul + qadd; |
| 0 | 1183 last = code >= rl->last; |
| 21 | 1184 if (get_bits1(&s->gb)) |
| 0 | 1185 level = -level; |
| 1186 } | |
| 1187 i += run; | |
| 1188 if (i >= 64) | |
| 1189 return -1; | |
| 1190 j = scan_table[i]; | |
| 1191 block[j] = level; | |
| 1192 i++; | |
| 1193 if (last) | |
| 1194 break; | |
| 1195 } | |
| 1196 not_coded: | |
| 1197 if (s->mb_intra) { | |
| 1198 mpeg4_pred_ac(s, block, n, dc_pred_dir); | |
| 1199 if (s->ac_pred) { | |
| 1200 i = 64; /* XXX: not optimal */ | |
| 1201 } | |
| 1202 } | |
| 1203 s->block_last_index[n] = i - 1; | |
| 1204 | |
| 1205 return 0; | |
| 1206 } | |
| 1207 | |
| 1208 static int msmpeg4_decode_dc(MpegEncContext * s, int n, int *dir_ptr) | |
| 1209 { | |
| 1210 int level, pred; | |
| 25 | 1211 INT16 *dc_val; |
| 0 | 1212 |
| 307 | 1213 if(s->msmpeg4_version==2){ |
| 1214 if (n < 4) { | |
| 1215 level = get_vlc(&s->gb, &v2_dc_lum_vlc); | |
| 1216 } else { | |
| 1217 level = get_vlc(&s->gb, &v2_dc_chroma_vlc); | |
| 1218 } | |
| 1219 if (level < 0) | |
| 1220 return -1; | |
| 0 | 1221 |
| 307 | 1222 level-=256; |
| 1223 }else{ //FIXME optimize use unified tables & index | |
| 1224 if (n < 4) { | |
| 1225 level = get_vlc(&s->gb, &dc_lum_vlc[s->dc_table_index]); | |
| 1226 } else { | |
| 1227 level = get_vlc(&s->gb, &dc_chroma_vlc[s->dc_table_index]); | |
| 1228 } | |
| 1229 if (level < 0) | |
| 1230 return -1; | |
| 1231 | |
| 1232 if (level == DC_MAX) { | |
| 1233 level = get_bits(&s->gb, 8); | |
| 1234 if (get_bits1(&s->gb)) | |
| 1235 level = -level; | |
| 1236 } else if (level != 0) { | |
| 1237 if (get_bits1(&s->gb)) | |
| 1238 level = -level; | |
| 1239 } | |
| 0 | 1240 } |
| 1241 | |
| 1242 pred = msmpeg4_pred_dc(s, n, &dc_val, dir_ptr); | |
| 1243 level += pred; | |
| 1244 | |
| 1245 /* update predictor */ | |
| 1246 if (n < 4) { | |
| 1247 *dc_val = level * s->y_dc_scale; | |
| 1248 } else { | |
| 1249 *dc_val = level * s->c_dc_scale; | |
| 1250 } | |
| 1251 | |
| 1252 return level; | |
| 1253 } | |
| 1254 | |
| 1255 static int msmpeg4_decode_motion(MpegEncContext * s, | |
| 1256 int *mx_ptr, int *my_ptr) | |
| 1257 { | |
| 1258 MVTable *mv; | |
| 1259 int code, mx, my; | |
| 1260 | |
| 1261 mv = &mv_tables[s->mv_table_index]; | |
| 1262 | |
| 1263 code = get_vlc(&s->gb, &mv->vlc); | |
| 1264 if (code < 0) | |
| 1265 return -1; | |
| 1266 if (code == mv->n) { | |
| 1267 mx = get_bits(&s->gb, 6); | |
| 1268 my = get_bits(&s->gb, 6); | |
| 1269 } else { | |
| 1270 mx = mv->table_mvx[code]; | |
| 1271 my = mv->table_mvy[code]; | |
| 1272 } | |
| 1273 | |
| 1274 mx += *mx_ptr - 32; | |
| 1275 my += *my_ptr - 32; | |
| 1276 /* WARNING : they do not do exactly modulo encoding */ | |
| 1277 if (mx <= -64) | |
| 1278 mx += 64; | |
| 1279 else if (mx >= 64) | |
| 1280 mx -= 64; | |
| 1281 | |
| 1282 if (my <= -64) | |
| 1283 my += 64; | |
| 1284 else if (my >= 64) | |
| 1285 my -= 64; | |
| 1286 *mx_ptr = mx; | |
| 1287 *my_ptr = my; | |
| 1288 return 0; | |
| 1289 } |
