Mercurial > libavcodec.hg
annotate mpeg12.c @ 69:361631a8d140 libavcodec
copy avctx
| author | glantau |
|---|---|
| date | Wed, 15 Aug 2001 13:11:03 +0000 |
| parents | 5aa6292a1660 |
| children | 0b09bd08ef4b |
| rev | line source |
|---|---|
| 0 | 1 /* |
| 2 * MPEG1 encoder / MPEG2 decoder | |
| 3 * Copyright (c) 2000,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 "avcodec.h" | |
| 20 #include "dsputil.h" | |
| 21 #include "mpegvideo.h" | |
| 22 | |
| 23 #include "mpeg12data.h" | |
| 24 | |
| 25 //#define DEBUG | |
| 26 | |
| 64 | 27 #ifndef CONFIG_WIN32 |
| 28 | |
| 0 | 29 #ifdef DEBUG |
| 30 #define dprintf(fmt,args...) printf(fmt, ## args) | |
| 31 #else | |
| 32 #define dprintf(fmt,args...) | |
| 33 #endif | |
| 34 | |
| 64 | 35 #else |
| 36 | |
| 37 inline void dprintf(const char* fmt,...) {} | |
| 38 | |
| 39 #endif | |
| 40 | |
| 0 | 41 /* Start codes. */ |
| 42 #define SEQ_END_CODE 0x000001b7 | |
| 43 #define SEQ_START_CODE 0x000001b3 | |
| 44 #define GOP_START_CODE 0x000001b8 | |
| 45 #define PICTURE_START_CODE 0x00000100 | |
| 46 #define SLICE_MIN_START_CODE 0x00000101 | |
| 47 #define SLICE_MAX_START_CODE 0x000001af | |
| 48 #define EXT_START_CODE 0x000001b5 | |
| 49 #define USER_START_CODE 0x000001b2 | |
| 50 | |
| 51 static void mpeg1_encode_block(MpegEncContext *s, | |
| 52 DCTELEM *block, | |
| 53 int component); | |
| 54 static void mpeg1_encode_motion(MpegEncContext *s, int val); | |
| 55 static void mpeg1_skip_picture(MpegEncContext *s, int pict_num); | |
| 56 static int mpeg1_decode_block(MpegEncContext *s, | |
| 57 DCTELEM *block, | |
| 58 int n); | |
| 59 static int mpeg2_decode_block_non_intra(MpegEncContext *s, | |
| 60 DCTELEM *block, | |
| 61 int n); | |
| 62 static int mpeg2_decode_block_intra(MpegEncContext *s, | |
| 63 DCTELEM *block, | |
| 64 int n); | |
| 65 static int mpeg_decode_motion(MpegEncContext *s, int fcode, int pred); | |
| 66 | |
| 67 static void put_header(MpegEncContext *s, int header) | |
| 68 { | |
| 69 align_put_bits(&s->pb); | |
| 70 put_bits(&s->pb, 32, header); | |
| 71 } | |
| 72 | |
| 73 /* put sequence header if needed */ | |
| 74 static void mpeg1_encode_sequence_header(MpegEncContext *s) | |
| 75 { | |
| 76 unsigned int vbv_buffer_size; | |
| 1 | 77 unsigned int fps, v; |
| 78 int n; | |
| 0 | 79 UINT64 time_code; |
| 80 | |
| 81 if ((s->picture_number % s->gop_size) == 0) { | |
| 82 /* mpeg1 header repeated every gop */ | |
| 83 put_header(s, SEQ_START_CODE); | |
| 84 | |
| 85 /* search closest frame rate */ | |
| 86 { | |
| 87 int i, dmin, d; | |
| 88 s->frame_rate_index = 0; | |
| 89 dmin = 0x7fffffff; | |
| 90 for(i=1;i<9;i++) { | |
| 91 d = abs(s->frame_rate - frame_rate_tab[i]); | |
| 92 if (d < dmin) { | |
| 93 dmin = d; | |
| 94 s->frame_rate_index = i; | |
| 95 } | |
| 96 } | |
| 97 } | |
| 98 | |
| 99 put_bits(&s->pb, 12, s->width); | |
| 100 put_bits(&s->pb, 12, s->height); | |
| 101 put_bits(&s->pb, 4, 1); /* 1/1 aspect ratio */ | |
| 102 put_bits(&s->pb, 4, s->frame_rate_index); | |
| 103 v = s->bit_rate / 400; | |
| 104 if (v > 0x3ffff) | |
| 105 v = 0x3ffff; | |
| 106 put_bits(&s->pb, 18, v); | |
| 107 put_bits(&s->pb, 1, 1); /* marker */ | |
| 108 /* vbv buffer size: slightly greater than an I frame. We add | |
| 109 some margin just in case */ | |
| 110 vbv_buffer_size = (3 * s->I_frame_bits) / (2 * 8); | |
| 111 put_bits(&s->pb, 10, (vbv_buffer_size + 16383) / 16384); | |
| 112 put_bits(&s->pb, 1, 1); /* constrained parameter flag */ | |
| 113 put_bits(&s->pb, 1, 0); /* no custom intra matrix */ | |
| 114 put_bits(&s->pb, 1, 0); /* no custom non intra matrix */ | |
| 115 | |
| 116 put_header(s, GOP_START_CODE); | |
| 117 put_bits(&s->pb, 1, 0); /* do drop frame */ | |
| 118 /* time code : we must convert from the real frame rate to a | |
| 119 fake mpeg frame rate in case of low frame rate */ | |
| 120 fps = frame_rate_tab[s->frame_rate_index]; | |
| 121 time_code = s->fake_picture_number * FRAME_RATE_BASE; | |
| 122 s->gop_picture_number = s->fake_picture_number; | |
| 64 | 123 put_bits(&s->pb, 5, (UINT32)((time_code / (fps * 3600)) % 24)); |
| 124 put_bits(&s->pb, 6, (UINT32)((time_code / (fps * 60)) % 60)); | |
| 0 | 125 put_bits(&s->pb, 1, 1); |
| 64 | 126 put_bits(&s->pb, 6, (UINT32)((time_code / fps) % 60)); |
| 127 put_bits(&s->pb, 6, (UINT32)((time_code % fps) / FRAME_RATE_BASE)); | |
| 0 | 128 put_bits(&s->pb, 1, 1); /* closed gop */ |
| 129 put_bits(&s->pb, 1, 0); /* broken link */ | |
| 130 } | |
| 131 | |
| 132 if (s->frame_rate < (24 * FRAME_RATE_BASE) && s->picture_number > 0) { | |
| 133 /* insert empty P pictures to slow down to the desired | |
| 134 frame rate. Each fake pictures takes about 20 bytes */ | |
| 135 fps = frame_rate_tab[s->frame_rate_index]; | |
| 136 n = ((s->picture_number * fps) / s->frame_rate) - 1; | |
| 137 while (s->fake_picture_number < n) { | |
| 138 mpeg1_skip_picture(s, s->fake_picture_number - | |
| 139 s->gop_picture_number); | |
| 140 s->fake_picture_number++; | |
| 141 } | |
| 142 | |
| 143 } | |
| 144 s->fake_picture_number++; | |
| 145 } | |
| 146 | |
| 147 | |
| 148 /* insert a fake P picture */ | |
| 149 static void mpeg1_skip_picture(MpegEncContext *s, int pict_num) | |
| 150 { | |
| 151 unsigned int mb_incr; | |
| 152 | |
| 153 /* mpeg1 picture header */ | |
| 154 put_header(s, PICTURE_START_CODE); | |
| 155 /* temporal reference */ | |
| 156 put_bits(&s->pb, 10, pict_num & 0x3ff); | |
| 157 | |
| 158 put_bits(&s->pb, 3, P_TYPE); | |
| 159 put_bits(&s->pb, 16, 0xffff); /* non constant bit rate */ | |
| 160 | |
| 161 put_bits(&s->pb, 1, 1); /* integer coordinates */ | |
| 162 put_bits(&s->pb, 3, 1); /* forward_f_code */ | |
| 163 | |
| 164 put_bits(&s->pb, 1, 0); /* extra bit picture */ | |
| 165 | |
| 166 /* only one slice */ | |
| 167 put_header(s, SLICE_MIN_START_CODE); | |
| 168 put_bits(&s->pb, 5, 1); /* quantizer scale */ | |
| 169 put_bits(&s->pb, 1, 0); /* slice extra information */ | |
| 170 | |
| 171 mb_incr = 1; | |
| 172 put_bits(&s->pb, mbAddrIncrTable[mb_incr - 1][1], | |
| 173 mbAddrIncrTable[mb_incr - 1][0]); | |
| 174 | |
| 175 /* empty macroblock */ | |
| 176 put_bits(&s->pb, 3, 1); /* motion only */ | |
| 177 | |
| 178 /* zero motion x & y */ | |
| 179 put_bits(&s->pb, 1, 1); | |
| 180 put_bits(&s->pb, 1, 1); | |
| 181 | |
| 182 /* output a number of empty slice */ | |
| 183 mb_incr = s->mb_width * s->mb_height - 1; | |
| 184 while (mb_incr > 33) { | |
| 185 put_bits(&s->pb, 11, 0x008); | |
| 186 mb_incr -= 33; | |
| 187 } | |
| 188 put_bits(&s->pb, mbAddrIncrTable[mb_incr - 1][1], | |
| 189 mbAddrIncrTable[mb_incr - 1][0]); | |
| 190 | |
| 191 /* empty macroblock */ | |
| 192 put_bits(&s->pb, 3, 1); /* motion only */ | |
| 193 | |
| 194 /* zero motion x & y */ | |
| 195 put_bits(&s->pb, 1, 1); | |
| 196 put_bits(&s->pb, 1, 1); | |
| 197 } | |
| 198 | |
| 199 void mpeg1_encode_picture_header(MpegEncContext *s, int picture_number) | |
| 200 { | |
| 201 static int done; | |
| 202 | |
| 203 if (!done) { | |
| 204 done = 1; | |
| 205 init_rl(&rl_mpeg1); | |
| 206 } | |
| 207 mpeg1_encode_sequence_header(s); | |
| 208 | |
| 209 /* mpeg1 picture header */ | |
| 210 put_header(s, PICTURE_START_CODE); | |
| 211 /* temporal reference */ | |
| 212 put_bits(&s->pb, 10, (s->fake_picture_number - | |
| 213 s->gop_picture_number) & 0x3ff); | |
| 214 | |
| 215 put_bits(&s->pb, 3, s->pict_type); | |
| 216 put_bits(&s->pb, 16, 0xffff); /* non constant bit rate */ | |
| 217 | |
| 218 if (s->pict_type == P_TYPE) { | |
| 219 put_bits(&s->pb, 1, 0); /* half pel coordinates */ | |
| 220 put_bits(&s->pb, 3, s->f_code); /* forward_f_code */ | |
| 221 } | |
| 222 | |
| 223 put_bits(&s->pb, 1, 0); /* extra bit picture */ | |
| 224 | |
| 225 /* only one slice */ | |
| 226 put_header(s, SLICE_MIN_START_CODE); | |
| 227 put_bits(&s->pb, 5, s->qscale); /* quantizer scale */ | |
| 228 put_bits(&s->pb, 1, 0); /* slice extra information */ | |
| 229 } | |
| 230 | |
| 231 void mpeg1_encode_mb(MpegEncContext *s, | |
| 232 DCTELEM block[6][64], | |
| 233 int motion_x, int motion_y) | |
| 234 { | |
| 235 int mb_incr, i, cbp, mb_x, mb_y; | |
| 236 | |
| 237 mb_x = s->mb_x; | |
| 238 mb_y = s->mb_y; | |
| 239 | |
| 240 /* compute cbp */ | |
| 241 cbp = 0; | |
| 242 for(i=0;i<6;i++) { | |
| 243 if (s->block_last_index[i] >= 0) | |
| 244 cbp |= 1 << (5 - i); | |
| 245 } | |
| 246 | |
| 247 /* skip macroblock, except if first or last macroblock of a slice */ | |
| 248 if ((cbp | motion_x | motion_y) == 0 && | |
| 249 (!((mb_x | mb_y) == 0 || | |
| 250 (mb_x == s->mb_width - 1 && mb_y == s->mb_height - 1)))) { | |
| 251 s->mb_incr++; | |
| 252 } else { | |
| 253 /* output mb incr */ | |
| 254 mb_incr = s->mb_incr; | |
| 255 | |
| 256 while (mb_incr > 33) { | |
| 257 put_bits(&s->pb, 11, 0x008); | |
| 258 mb_incr -= 33; | |
| 259 } | |
| 260 put_bits(&s->pb, mbAddrIncrTable[mb_incr - 1][1], | |
| 261 mbAddrIncrTable[mb_incr - 1][0]); | |
| 262 | |
| 263 if (s->pict_type == I_TYPE) { | |
| 264 put_bits(&s->pb, 1, 1); /* macroblock_type : macroblock_quant = 0 */ | |
| 265 } else { | |
| 266 if (s->mb_intra) { | |
| 267 put_bits(&s->pb, 5, 0x03); | |
| 268 } else { | |
| 269 if (cbp != 0) { | |
| 270 if (motion_x == 0 && motion_y == 0) { | |
| 271 put_bits(&s->pb, 2, 1); /* macroblock_pattern only */ | |
| 272 put_bits(&s->pb, mbPatTable[cbp - 1][1], mbPatTable[cbp - 1][0]); | |
| 273 } else { | |
| 274 put_bits(&s->pb, 1, 1); /* motion + cbp */ | |
| 275 mpeg1_encode_motion(s, motion_x - s->last_mv[0][0][0]); | |
| 276 mpeg1_encode_motion(s, motion_y - s->last_mv[0][0][1]); | |
| 277 put_bits(&s->pb, mbPatTable[cbp - 1][1], mbPatTable[cbp - 1][0]); | |
| 278 } | |
| 279 } else { | |
| 280 put_bits(&s->pb, 3, 1); /* motion only */ | |
| 281 mpeg1_encode_motion(s, motion_x - s->last_mv[0][0][0]); | |
| 282 mpeg1_encode_motion(s, motion_y - s->last_mv[0][0][1]); | |
| 283 } | |
| 284 } | |
| 285 } | |
| 286 for(i=0;i<6;i++) { | |
| 287 if (cbp & (1 << (5 - i))) { | |
| 288 mpeg1_encode_block(s, block[i], i); | |
| 289 } | |
| 290 } | |
| 291 s->mb_incr = 1; | |
| 292 } | |
| 293 s->last_mv[0][0][0] = motion_x; | |
| 294 s->last_mv[0][0][1] = motion_y; | |
| 295 } | |
| 296 | |
| 297 static void mpeg1_encode_motion(MpegEncContext *s, int val) | |
| 298 { | |
| 299 int code, bit_size, l, m, bits, range, sign; | |
| 300 | |
| 301 if (val == 0) { | |
| 302 /* zero vector */ | |
| 303 code = 0; | |
| 304 put_bits(&s->pb, | |
| 305 mbMotionVectorTable[0][1], | |
| 306 mbMotionVectorTable[0][0]); | |
| 307 } else { | |
| 308 bit_size = s->f_code - 1; | |
| 309 range = 1 << bit_size; | |
| 310 /* modulo encoding */ | |
| 311 l = 16 * range; | |
| 312 m = 2 * l; | |
| 313 if (val < -l) { | |
| 314 val += m; | |
| 315 } else if (val >= l) { | |
| 316 val -= m; | |
| 317 } | |
| 318 | |
| 319 if (val >= 0) { | |
| 320 val--; | |
| 321 code = (val >> bit_size) + 1; | |
| 322 bits = val & (range - 1); | |
| 323 sign = 0; | |
| 324 } else { | |
| 325 val = -val; | |
| 326 val--; | |
| 327 code = (val >> bit_size) + 1; | |
| 328 bits = val & (range - 1); | |
| 329 sign = 1; | |
| 330 } | |
| 331 put_bits(&s->pb, | |
| 332 mbMotionVectorTable[code][1], | |
| 333 mbMotionVectorTable[code][0]); | |
| 334 put_bits(&s->pb, 1, sign); | |
| 335 if (bit_size > 0) { | |
| 336 put_bits(&s->pb, bit_size, bits); | |
| 337 } | |
| 338 } | |
| 339 } | |
| 340 | |
| 341 static inline void encode_dc(MpegEncContext *s, int diff, int component) | |
| 342 { | |
| 343 int adiff, index; | |
| 344 | |
| 345 adiff = abs(diff); | |
| 346 index = vlc_dc_table[adiff]; | |
| 347 if (component == 0) { | |
| 348 put_bits(&s->pb, vlc_dc_lum_bits[index], vlc_dc_lum_code[index]); | |
| 349 } else { | |
| 350 put_bits(&s->pb, vlc_dc_chroma_bits[index], vlc_dc_chroma_code[index]); | |
| 351 } | |
| 352 if (diff > 0) { | |
| 353 put_bits(&s->pb, index, (diff & ((1 << index) - 1))); | |
| 354 } else if (diff < 0) { | |
| 355 put_bits(&s->pb, index, ((diff - 1) & ((1 << index) - 1))); | |
| 356 } | |
| 357 } | |
| 358 | |
| 359 static void mpeg1_encode_block(MpegEncContext *s, | |
| 360 DCTELEM *block, | |
| 361 int n) | |
| 362 { | |
| 363 int alevel, level, last_non_zero, dc, diff, i, j, run, last_index, sign; | |
| 364 int code, component; | |
| 365 RLTable *rl = &rl_mpeg1; | |
| 366 | |
| 367 last_index = s->block_last_index[n]; | |
| 368 | |
| 369 /* DC coef */ | |
| 370 if (s->mb_intra) { | |
| 371 component = (n <= 3 ? 0 : n - 4 + 1); | |
| 372 dc = block[0]; /* overflow is impossible */ | |
| 373 diff = dc - s->last_dc[component]; | |
| 374 encode_dc(s, diff, component); | |
| 375 s->last_dc[component] = dc; | |
| 376 i = 1; | |
| 377 } else { | |
| 378 /* encode the first coefficient : needs to be done here because | |
| 379 it is handled slightly differently */ | |
| 380 level = block[0]; | |
| 381 if (abs(level) == 1) { | |
| 382 code = ((UINT32)level >> 31); /* the sign bit */ | |
| 383 put_bits(&s->pb, 2, code | 0x02); | |
| 384 i = 1; | |
| 385 } else { | |
| 386 i = 0; | |
| 387 last_non_zero = -1; | |
| 388 goto next_coef; | |
| 389 } | |
| 390 } | |
| 391 | |
| 392 /* now quantify & encode AC coefs */ | |
| 393 last_non_zero = i - 1; | |
| 394 for(;i<=last_index;i++) { | |
| 395 j = zigzag_direct[i]; | |
| 396 level = block[j]; | |
| 397 next_coef: | |
| 398 #if 0 | |
| 399 if (level != 0) | |
| 400 dprintf("level[%d]=%d\n", i, level); | |
| 401 #endif | |
| 402 /* encode using VLC */ | |
| 403 if (level != 0) { | |
| 404 run = i - last_non_zero - 1; | |
| 405 sign = 0; | |
| 406 alevel = level; | |
| 407 if (alevel < 0) { | |
| 408 sign = 1; | |
| 409 alevel = -alevel; | |
| 410 } | |
| 411 code = get_rl_index(rl, 0, run, alevel); | |
| 412 put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]); | |
| 413 if (code != rl->n) { | |
| 414 put_bits(&s->pb, 1, sign); | |
| 415 } else { | |
| 416 /* escape: only clip in this case */ | |
| 417 put_bits(&s->pb, 6, run); | |
| 418 if (alevel < 128) { | |
| 419 put_bits(&s->pb, 8, level & 0xff); | |
| 420 } else { | |
| 421 if (level < 0) { | |
| 422 put_bits(&s->pb, 16, 0x8001 + level + 255); | |
| 423 } else { | |
| 424 put_bits(&s->pb, 16, level & 0xffff); | |
| 425 } | |
| 426 } | |
| 427 } | |
| 428 last_non_zero = i; | |
| 429 } | |
| 430 } | |
| 431 /* end of block */ | |
| 432 put_bits(&s->pb, 2, 0x2); | |
| 433 } | |
| 434 | |
| 435 /******************************************/ | |
| 436 /* decoding */ | |
| 437 | |
| 438 static VLC dc_lum_vlc; | |
| 439 static VLC dc_chroma_vlc; | |
| 440 static VLC mv_vlc; | |
| 441 static VLC mbincr_vlc; | |
| 442 static VLC mb_ptype_vlc; | |
| 443 static VLC mb_btype_vlc; | |
| 444 static VLC mb_pat_vlc; | |
| 445 | |
| 446 void mpeg1_init_vlc(MpegEncContext *s) | |
| 447 { | |
| 448 static int done = 0; | |
| 449 | |
| 450 if (!done) { | |
| 451 | |
| 452 init_vlc(&dc_lum_vlc, 9, 12, | |
| 453 vlc_dc_lum_bits, 1, 1, | |
| 454 vlc_dc_lum_code, 2, 2); | |
| 455 init_vlc(&dc_chroma_vlc, 9, 12, | |
| 456 vlc_dc_chroma_bits, 1, 1, | |
| 457 vlc_dc_chroma_code, 2, 2); | |
| 458 init_vlc(&mv_vlc, 9, 17, | |
| 459 &mbMotionVectorTable[0][1], 2, 1, | |
| 460 &mbMotionVectorTable[0][0], 2, 1); | |
|
45
933cc4acab5c
fixed mpeg1 last block bug (mb stuffing code was not included in vlc table...)
glantau
parents:
38
diff
changeset
|
461 init_vlc(&mbincr_vlc, 9, 35, |
| 0 | 462 &mbAddrIncrTable[0][1], 2, 1, |
| 463 &mbAddrIncrTable[0][0], 2, 1); | |
| 464 init_vlc(&mb_pat_vlc, 9, 63, | |
| 465 &mbPatTable[0][1], 2, 1, | |
| 466 &mbPatTable[0][0], 2, 1); | |
| 467 | |
| 468 init_vlc(&mb_ptype_vlc, 6, 32, | |
| 469 &table_mb_ptype[0][1], 2, 1, | |
| 470 &table_mb_ptype[0][0], 2, 1); | |
| 471 init_vlc(&mb_btype_vlc, 6, 32, | |
| 472 &table_mb_btype[0][1], 2, 1, | |
| 473 &table_mb_btype[0][0], 2, 1); | |
| 474 init_rl(&rl_mpeg1); | |
| 475 init_rl(&rl_mpeg2); | |
| 476 /* cannot use generic init because we must add the EOB code */ | |
| 477 init_vlc(&rl_mpeg1.vlc, 9, rl_mpeg1.n + 2, | |
| 478 &rl_mpeg1.table_vlc[0][1], 4, 2, | |
| 479 &rl_mpeg1.table_vlc[0][0], 4, 2); | |
| 480 init_vlc(&rl_mpeg2.vlc, 9, rl_mpeg2.n + 2, | |
| 481 &rl_mpeg2.table_vlc[0][1], 4, 2, | |
| 482 &rl_mpeg2.table_vlc[0][0], 4, 2); | |
| 483 } | |
| 484 } | |
| 485 | |
| 486 static inline int get_dmv(MpegEncContext *s) | |
| 487 { | |
| 21 | 488 if(get_bits1(&s->gb)) |
| 489 return 1 - (get_bits1(&s->gb) << 1); | |
| 0 | 490 else |
| 491 return 0; | |
| 492 } | |
| 493 | |
| 54 | 494 static inline int get_qscale(MpegEncContext *s) |
| 495 { | |
| 496 int qscale; | |
| 497 if (s->mpeg2) { | |
| 498 if (s->q_scale_type) { | |
| 499 qscale = non_linear_qscale[get_bits(&s->gb, 5)]; | |
| 500 } else { | |
| 501 qscale = get_bits(&s->gb, 5) << 1; | |
| 502 } | |
| 503 } else { | |
| 504 /* for mpeg1, we use the generic unquant code */ | |
| 505 qscale = get_bits(&s->gb, 5); | |
| 506 } | |
| 507 return qscale; | |
| 508 } | |
| 509 | |
| 0 | 510 /* motion type (for mpeg2) */ |
| 511 #define MT_FIELD 1 | |
| 512 #define MT_FRAME 2 | |
| 513 #define MT_16X8 2 | |
| 514 #define MT_DMV 3 | |
| 515 | |
| 516 static int mpeg_decode_mb(MpegEncContext *s, | |
| 517 DCTELEM block[6][64]) | |
| 518 { | |
| 519 int i, j, k, cbp, val, code, mb_type, motion_type; | |
| 520 | |
| 521 /* skip mb handling */ | |
| 522 if (s->mb_incr == 0) { | |
| 523 /* read again increment */ | |
| 524 s->mb_incr = 1; | |
| 525 for(;;) { | |
| 526 code = get_vlc(&s->gb, &mbincr_vlc); | |
| 527 if (code < 0) | |
| 528 return 1; /* error = end of slice */ | |
| 529 if (code >= 33) { | |
| 530 if (code == 33) { | |
| 531 s->mb_incr += 33; | |
| 532 } | |
| 533 /* otherwise, stuffing, nothing to do */ | |
| 534 } else { | |
| 535 s->mb_incr += code; | |
| 536 break; | |
| 537 } | |
| 538 } | |
| 539 } | |
| 540 if (++s->mb_x >= s->mb_width) { | |
| 541 s->mb_x = 0; | |
| 542 if (s->mb_y >= (s->mb_height - 1)) | |
| 543 return -1; | |
| 544 s->mb_y++; | |
| 545 } | |
| 546 dprintf("decode_mb: x=%d y=%d\n", s->mb_x, s->mb_y); | |
| 547 | |
| 548 if (--s->mb_incr != 0) { | |
| 549 /* skip mb */ | |
| 550 s->mb_intra = 0; | |
| 551 for(i=0;i<6;i++) | |
| 552 s->block_last_index[i] = -1; | |
| 553 s->mv_type = MV_TYPE_16X16; | |
| 554 if (s->pict_type == P_TYPE) { | |
| 555 /* if P type, zero motion vector is implied */ | |
| 556 s->mv_dir = MV_DIR_FORWARD; | |
| 557 s->mv[0][0][0] = s->mv[0][0][1] = 0; | |
| 558 s->last_mv[0][0][0] = s->last_mv[0][0][1] = 0; | |
|
59
efd3c19f6d62
fixed mpeg2 non intra dequant - fixed MPEG1 and 2 matrix parsing
glantau
parents:
58
diff
changeset
|
559 s->last_mv[0][1][0] = s->last_mv[0][1][1] = 0; |
| 0 | 560 } else { |
| 561 /* if B type, reuse previous vectors and directions */ | |
| 562 s->mv[0][0][0] = s->last_mv[0][0][0]; | |
| 563 s->mv[0][0][1] = s->last_mv[0][0][1]; | |
| 564 s->mv[1][0][0] = s->last_mv[1][0][0]; | |
| 565 s->mv[1][0][1] = s->last_mv[1][0][1]; | |
| 566 } | |
|
7
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
1
diff
changeset
|
567 s->mb_skiped = 1; |
| 0 | 568 return 0; |
| 569 } | |
| 570 | |
| 571 switch(s->pict_type) { | |
| 572 default: | |
| 573 case I_TYPE: | |
| 21 | 574 if (get_bits1(&s->gb) == 0) { |
| 575 if (get_bits1(&s->gb) == 0) | |
| 0 | 576 return -1; |
| 577 mb_type = MB_QUANT | MB_INTRA; | |
| 578 } else { | |
| 579 mb_type = MB_INTRA; | |
| 580 } | |
| 581 break; | |
| 582 case P_TYPE: | |
| 583 mb_type = get_vlc(&s->gb, &mb_ptype_vlc); | |
| 584 if (mb_type < 0) | |
| 585 return -1; | |
| 586 break; | |
| 587 case B_TYPE: | |
| 588 mb_type = get_vlc(&s->gb, &mb_btype_vlc); | |
| 589 if (mb_type < 0) | |
| 590 return -1; | |
| 591 break; | |
| 592 } | |
| 593 dprintf("mb_type=%x\n", mb_type); | |
| 594 motion_type = 0; /* avoid warning */ | |
| 595 if (mb_type & (MB_FOR|MB_BACK)) { | |
| 596 /* get additionnal motion vector type */ | |
| 597 if (s->picture_structure == PICT_FRAME && s->frame_pred_frame_dct) | |
| 598 motion_type = MT_FRAME; | |
| 599 else | |
| 600 motion_type = get_bits(&s->gb, 2); | |
| 601 } | |
| 602 /* compute dct type */ | |
| 603 if (s->picture_structure == PICT_FRAME && | |
| 604 !s->frame_pred_frame_dct && | |
| 605 (mb_type & (MB_PAT | MB_INTRA))) { | |
| 21 | 606 s->interlaced_dct = get_bits1(&s->gb); |
| 0 | 607 #ifdef DEBUG |
| 608 if (s->interlaced_dct) | |
| 609 printf("interlaced_dct\n"); | |
| 610 #endif | |
| 611 } else { | |
| 612 s->interlaced_dct = 0; /* frame based */ | |
| 613 } | |
| 614 | |
| 615 if (mb_type & MB_QUANT) { | |
| 54 | 616 s->qscale = get_qscale(s); |
| 0 | 617 } |
| 618 if (mb_type & MB_INTRA) { | |
| 619 if (s->concealment_motion_vectors) { | |
| 620 /* just parse them */ | |
| 621 if (s->picture_structure != PICT_FRAME) | |
| 21 | 622 skip_bits1(&s->gb); /* field select */ |
| 0 | 623 mpeg_decode_motion(s, s->mpeg_f_code[0][0], 0); |
| 624 mpeg_decode_motion(s, s->mpeg_f_code[0][1], 0); | |
| 625 } | |
| 626 s->mb_intra = 1; | |
| 627 cbp = 0x3f; | |
| 628 memset(s->last_mv, 0, sizeof(s->last_mv)); /* reset mv prediction */ | |
| 629 } else { | |
| 630 s->mb_intra = 0; | |
| 631 cbp = 0; | |
| 632 } | |
| 633 /* special case of implicit zero motion vector */ | |
| 634 if (s->pict_type == P_TYPE && !(mb_type & MB_FOR)) { | |
| 635 s->mv_dir = MV_DIR_FORWARD; | |
| 636 s->mv_type = MV_TYPE_16X16; | |
| 637 s->last_mv[0][0][0] = 0; | |
| 638 s->last_mv[0][0][1] = 0; | |
|
58
0e0a24def67a
fixed last zero mv for field - fixed mismatch handling for intra coefs
glantau
parents:
54
diff
changeset
|
639 s->last_mv[0][1][0] = 0; |
|
0e0a24def67a
fixed last zero mv for field - fixed mismatch handling for intra coefs
glantau
parents:
54
diff
changeset
|
640 s->last_mv[0][1][1] = 0; |
| 0 | 641 s->mv[0][0][0] = 0; |
| 642 s->mv[0][0][1] = 0; | |
| 643 } else if (mb_type & (MB_FOR | MB_BACK)) { | |
| 644 /* motion vectors */ | |
| 645 s->mv_dir = 0; | |
| 646 for(i=0;i<2;i++) { | |
| 647 if (mb_type & (MB_FOR >> i)) { | |
| 648 s->mv_dir |= (MV_DIR_FORWARD >> i); | |
|
58
0e0a24def67a
fixed last zero mv for field - fixed mismatch handling for intra coefs
glantau
parents:
54
diff
changeset
|
649 dprintf("motion_type=%d\n", motion_type); |
| 0 | 650 switch(motion_type) { |
| 651 case MT_FRAME: /* or MT_16X8 */ | |
| 652 if (s->picture_structure == PICT_FRAME) { | |
| 653 /* MT_FRAME */ | |
| 654 s->mv_type = MV_TYPE_16X16; | |
| 655 for(k=0;k<2;k++) { | |
| 656 val = mpeg_decode_motion(s, s->mpeg_f_code[i][k], | |
| 657 s->last_mv[i][0][k]); | |
| 658 s->last_mv[i][0][k] = val; | |
| 659 s->last_mv[i][1][k] = val; | |
| 660 /* full_pel: only for mpeg1 */ | |
| 661 if (s->full_pel[i]) | |
| 662 val = val << 1; | |
| 663 s->mv[i][0][k] = val; | |
| 664 dprintf("mv%d: %d\n", k, val); | |
| 665 } | |
| 666 } else { | |
| 667 /* MT_16X8 */ | |
| 668 s->mv_type = MV_TYPE_16X8; | |
| 669 for(j=0;j<2;j++) { | |
| 21 | 670 s->field_select[i][j] = get_bits1(&s->gb); |
| 0 | 671 for(k=0;k<2;k++) { |
| 672 val = mpeg_decode_motion(s, s->mpeg_f_code[i][k], | |
| 673 s->last_mv[i][j][k]); | |
| 674 s->last_mv[i][j][k] = val; | |
| 675 s->mv[i][j][k] = val; | |
| 676 } | |
| 677 } | |
| 678 } | |
| 679 break; | |
| 680 case MT_FIELD: | |
| 681 if (s->picture_structure == PICT_FRAME) { | |
| 682 s->mv_type = MV_TYPE_FIELD; | |
| 683 for(j=0;j<2;j++) { | |
| 21 | 684 s->field_select[i][j] = get_bits1(&s->gb); |
| 0 | 685 val = mpeg_decode_motion(s, s->mpeg_f_code[i][0], |
| 686 s->last_mv[i][j][0]); | |
| 687 s->last_mv[i][j][0] = val; | |
| 688 s->mv[i][j][0] = val; | |
| 689 dprintf("fmx=%d\n", val); | |
| 690 val = mpeg_decode_motion(s, s->mpeg_f_code[i][1], | |
| 691 s->last_mv[i][j][1] >> 1); | |
| 692 s->last_mv[i][j][1] = val << 1; | |
| 693 s->mv[i][j][1] = val; | |
| 694 dprintf("fmy=%d\n", val); | |
| 695 } | |
| 696 } else { | |
| 697 s->mv_type = MV_TYPE_16X16; | |
| 21 | 698 s->field_select[i][0] = get_bits1(&s->gb); |
| 0 | 699 for(k=0;k<2;k++) { |
| 700 val = mpeg_decode_motion(s, s->mpeg_f_code[i][k], | |
| 701 s->last_mv[i][0][k]); | |
| 702 s->last_mv[i][0][k] = val; | |
| 703 s->last_mv[i][1][k] = val; | |
| 704 s->mv[i][0][k] = val; | |
| 705 } | |
| 706 } | |
| 707 break; | |
| 708 case MT_DMV: | |
| 709 { | |
| 710 int dmx, dmy, mx, my, m; | |
| 711 | |
| 712 mx = mpeg_decode_motion(s, s->mpeg_f_code[i][0], | |
| 713 s->last_mv[i][0][0]); | |
| 714 s->last_mv[i][0][0] = mx; | |
| 715 s->last_mv[i][1][0] = mx; | |
| 716 dmx = get_dmv(s); | |
| 717 my = mpeg_decode_motion(s, s->mpeg_f_code[i][1], | |
| 718 s->last_mv[i][0][1] >> 1); | |
| 719 dmy = get_dmv(s); | |
| 720 s->mv_type = MV_TYPE_DMV; | |
| 721 /* XXX: totally broken */ | |
| 722 if (s->picture_structure == PICT_FRAME) { | |
| 723 s->last_mv[i][0][1] = my << 1; | |
| 724 s->last_mv[i][1][1] = my << 1; | |
| 725 | |
| 726 m = s->top_field_first ? 1 : 3; | |
| 727 /* top -> top pred */ | |
| 728 s->mv[i][0][0] = mx; | |
| 729 s->mv[i][0][1] = my << 1; | |
| 730 s->mv[i][1][0] = ((mx * m + (mx > 0)) >> 1) + dmx; | |
| 731 s->mv[i][1][1] = ((my * m + (my > 0)) >> 1) + dmy - 1; | |
| 732 m = 4 - m; | |
| 733 s->mv[i][2][0] = mx; | |
| 734 s->mv[i][2][1] = my << 1; | |
| 735 s->mv[i][3][0] = ((mx * m + (mx > 0)) >> 1) + dmx; | |
| 736 s->mv[i][3][1] = ((my * m + (my > 0)) >> 1) + dmy + 1; | |
| 737 } else { | |
| 738 s->last_mv[i][0][1] = my; | |
| 739 s->last_mv[i][1][1] = my; | |
| 740 s->mv[i][0][0] = mx; | |
| 741 s->mv[i][0][1] = my; | |
| 742 s->mv[i][1][0] = ((mx + (mx > 0)) >> 1) + dmx; | |
| 743 s->mv[i][1][1] = ((my + (my > 0)) >> 1) + dmy - 1 | |
| 744 /* + 2 * cur_field */; | |
| 745 } | |
| 746 } | |
| 747 break; | |
| 748 } | |
| 749 } | |
| 750 } | |
| 751 } | |
| 752 | |
| 753 if ((mb_type & MB_INTRA) && s->concealment_motion_vectors) { | |
| 21 | 754 skip_bits1(&s->gb); /* marker */ |
| 0 | 755 } |
| 756 | |
| 757 if (mb_type & MB_PAT) { | |
| 758 cbp = get_vlc(&s->gb, &mb_pat_vlc); | |
| 759 if (cbp < 0) | |
| 760 return -1; | |
| 761 cbp++; | |
| 762 } | |
| 763 dprintf("cbp=%x\n", cbp); | |
| 764 | |
| 765 if (s->mpeg2) { | |
| 766 if (s->mb_intra) { | |
| 767 for(i=0;i<6;i++) { | |
| 768 if (cbp & (1 << (5 - i))) { | |
| 769 if (mpeg2_decode_block_intra(s, block[i], i) < 0) | |
| 770 return -1; | |
| 771 } | |
| 772 } | |
| 773 } else { | |
| 774 for(i=0;i<6;i++) { | |
| 775 if (cbp & (1 << (5 - i))) { | |
| 776 if (mpeg2_decode_block_non_intra(s, block[i], i) < 0) | |
| 777 return -1; | |
| 778 } | |
| 779 } | |
| 780 } | |
| 781 } else { | |
| 782 for(i=0;i<6;i++) { | |
| 783 if (cbp & (1 << (5 - i))) { | |
| 784 if (mpeg1_decode_block(s, block[i], i) < 0) | |
| 785 return -1; | |
| 786 } | |
| 787 } | |
| 788 } | |
| 789 return 0; | |
| 790 } | |
| 791 | |
| 792 /* as h263, but only 17 codes */ | |
| 793 static int mpeg_decode_motion(MpegEncContext *s, int fcode, int pred) | |
| 794 { | |
| 795 int code, sign, val, m, l, shift; | |
| 796 | |
| 797 code = get_vlc(&s->gb, &mv_vlc); | |
| 798 if (code < 0) { | |
| 799 return 0xffff; | |
| 800 } | |
| 801 if (code == 0) { | |
| 802 return pred; | |
| 803 } | |
| 21 | 804 sign = get_bits1(&s->gb); |
| 0 | 805 shift = fcode - 1; |
| 806 val = (code - 1) << shift; | |
| 807 if (shift > 0) | |
| 808 val |= get_bits(&s->gb, shift); | |
| 809 val++; | |
| 810 if (sign) | |
| 811 val = -val; | |
| 812 val += pred; | |
| 813 | |
| 814 /* modulo decoding */ | |
| 815 l = (1 << shift) * 16; | |
| 816 m = 2 * l; | |
| 817 if (val < -l) { | |
| 818 val += m; | |
| 819 } else if (val >= l) { | |
| 820 val -= m; | |
| 821 } | |
| 822 return val; | |
| 823 } | |
| 824 | |
| 825 static inline int decode_dc(MpegEncContext *s, int component) | |
| 826 { | |
| 827 int code, diff; | |
| 828 | |
| 829 if (component == 0) { | |
| 830 code = get_vlc(&s->gb, &dc_lum_vlc); | |
| 831 } else { | |
| 832 code = get_vlc(&s->gb, &dc_chroma_vlc); | |
| 833 } | |
| 834 if (code < 0) | |
| 835 return 0xffff; | |
| 836 if (code == 0) { | |
| 837 diff = 0; | |
| 838 } else { | |
| 839 diff = get_bits(&s->gb, code); | |
| 840 if ((diff & (1 << (code - 1))) == 0) | |
| 841 diff = (-1 << code) | (diff + 1); | |
| 842 } | |
| 843 return diff; | |
| 844 } | |
| 845 | |
| 846 static int mpeg1_decode_block(MpegEncContext *s, | |
| 847 DCTELEM *block, | |
| 848 int n) | |
| 849 { | |
| 850 int level, dc, diff, i, j, run; | |
| 851 int code, component; | |
| 852 RLTable *rl = &rl_mpeg1; | |
| 853 | |
| 854 if (s->mb_intra) { | |
| 855 /* DC coef */ | |
| 856 component = (n <= 3 ? 0 : n - 4 + 1); | |
| 857 diff = decode_dc(s, component); | |
| 858 if (diff >= 0xffff) | |
| 859 return -1; | |
| 860 dc = s->last_dc[component]; | |
| 861 dc += diff; | |
| 862 s->last_dc[component] = dc; | |
| 863 block[0] = dc; | |
| 864 dprintf("dc=%d diff=%d\n", dc, diff); | |
| 865 i = 1; | |
| 866 } else { | |
| 867 int bit_cnt, v; | |
| 868 UINT32 bit_buf; | |
| 869 UINT8 *buf_ptr; | |
| 870 i = 0; | |
| 871 /* special case for the first coef. no need to add a second vlc table */ | |
| 872 SAVE_BITS(&s->gb); | |
| 873 SHOW_BITS(&s->gb, v, 2); | |
| 874 if (v & 2) { | |
| 875 run = 0; | |
| 876 level = 1 - ((v & 1) << 1); | |
| 877 FLUSH_BITS(2); | |
| 878 RESTORE_BITS(&s->gb); | |
| 879 goto add_coef; | |
| 880 } | |
| 881 RESTORE_BITS(&s->gb); | |
| 882 } | |
| 883 | |
| 884 /* now quantify & encode AC coefs */ | |
| 885 for(;;) { | |
| 886 code = get_vlc(&s->gb, &rl->vlc); | |
| 887 if (code < 0) { | |
| 888 return -1; | |
| 889 } | |
| 890 if (code == 112) { | |
| 891 break; | |
| 892 } else if (code == 111) { | |
| 893 /* escape */ | |
| 894 run = get_bits(&s->gb, 6); | |
| 895 level = get_bits(&s->gb, 8); | |
| 896 level = (level << 24) >> 24; | |
| 897 if (level == -128) { | |
| 898 level = get_bits(&s->gb, 8) - 256; | |
| 899 } else if (level == 0) { | |
| 900 level = get_bits(&s->gb, 8); | |
| 901 } | |
| 902 } else { | |
| 903 run = rl->table_run[code]; | |
| 904 level = rl->table_level[code]; | |
| 21 | 905 if (get_bits1(&s->gb)) |
| 0 | 906 level = -level; |
| 907 } | |
| 908 i += run; | |
| 909 if (i >= 64) | |
| 910 return -1; | |
| 911 add_coef: | |
| 912 dprintf("%d: run=%d level=%d\n", n, run, level); | |
| 913 j = zigzag_direct[i]; | |
| 914 block[j] = level; | |
| 915 i++; | |
| 916 } | |
| 917 s->block_last_index[n] = i; | |
| 918 return 0; | |
| 919 } | |
| 920 | |
| 921 /* Also does unquantization here, since I will never support mpeg2 | |
| 922 encoding */ | |
| 923 static int mpeg2_decode_block_non_intra(MpegEncContext *s, | |
| 924 DCTELEM *block, | |
| 925 int n) | |
| 926 { | |
| 927 int level, i, j, run; | |
| 928 int code; | |
| 929 RLTable *rl = &rl_mpeg1; | |
| 930 const UINT8 *scan_table; | |
| 931 const UINT16 *matrix; | |
| 932 int mismatch; | |
| 933 | |
| 934 if (s->alternate_scan) | |
| 935 scan_table = ff_alternate_vertical_scan; | |
| 936 else | |
| 937 scan_table = zigzag_direct; | |
| 938 mismatch = 1; | |
| 939 | |
| 940 { | |
| 941 int bit_cnt, v; | |
| 942 UINT32 bit_buf; | |
| 943 UINT8 *buf_ptr; | |
| 944 i = 0; | |
| 945 if (n < 4) | |
| 946 matrix = s->non_intra_matrix; | |
| 947 else | |
| 948 matrix = s->chroma_non_intra_matrix; | |
| 949 | |
| 950 /* special case for the first coef. no need to add a second vlc table */ | |
| 951 SAVE_BITS(&s->gb); | |
| 952 SHOW_BITS(&s->gb, v, 2); | |
| 953 if (v & 2) { | |
| 954 run = 0; | |
| 955 level = 1 - ((v & 1) << 1); | |
| 956 FLUSH_BITS(2); | |
| 957 RESTORE_BITS(&s->gb); | |
| 958 goto add_coef; | |
| 959 } | |
| 960 RESTORE_BITS(&s->gb); | |
| 961 } | |
| 962 | |
| 963 /* now quantify & encode AC coefs */ | |
| 964 for(;;) { | |
| 965 code = get_vlc(&s->gb, &rl->vlc); | |
| 966 if (code < 0) | |
| 967 return -1; | |
| 968 if (code == 112) { | |
| 969 break; | |
| 970 } else if (code == 111) { | |
| 971 /* escape */ | |
| 972 run = get_bits(&s->gb, 6); | |
| 973 level = get_bits(&s->gb, 12); | |
| 974 level = (level << 20) >> 20; | |
| 975 } else { | |
| 976 run = rl->table_run[code]; | |
| 977 level = rl->table_level[code]; | |
| 21 | 978 if (get_bits1(&s->gb)) |
| 0 | 979 level = -level; |
| 980 } | |
| 981 i += run; | |
| 982 if (i >= 64) | |
| 983 return -1; | |
| 984 add_coef: | |
| 985 j = scan_table[i]; | |
| 986 dprintf("%d: run=%d level=%d\n", n, run, level); | |
|
59
efd3c19f6d62
fixed mpeg2 non intra dequant - fixed MPEG1 and 2 matrix parsing
glantau
parents:
58
diff
changeset
|
987 /* XXX: optimize */ |
|
efd3c19f6d62
fixed mpeg2 non intra dequant - fixed MPEG1 and 2 matrix parsing
glantau
parents:
58
diff
changeset
|
988 if (level > 0) { |
|
efd3c19f6d62
fixed mpeg2 non intra dequant - fixed MPEG1 and 2 matrix parsing
glantau
parents:
58
diff
changeset
|
989 level = ((level * 2 + 1) * s->qscale * matrix[j]) >> 5; |
|
efd3c19f6d62
fixed mpeg2 non intra dequant - fixed MPEG1 and 2 matrix parsing
glantau
parents:
58
diff
changeset
|
990 } else { |
|
efd3c19f6d62
fixed mpeg2 non intra dequant - fixed MPEG1 and 2 matrix parsing
glantau
parents:
58
diff
changeset
|
991 level = ((-level * 2 + 1) * s->qscale * matrix[j]) >> 5; |
|
efd3c19f6d62
fixed mpeg2 non intra dequant - fixed MPEG1 and 2 matrix parsing
glantau
parents:
58
diff
changeset
|
992 level = -level; |
|
efd3c19f6d62
fixed mpeg2 non intra dequant - fixed MPEG1 and 2 matrix parsing
glantau
parents:
58
diff
changeset
|
993 } |
| 0 | 994 /* XXX: is it really necessary to saturate since the encoder |
| 995 knows whats going on ? */ | |
| 996 mismatch ^= level; | |
| 997 block[j] = level; | |
| 998 i++; | |
| 999 } | |
| 1000 block[63] ^= (mismatch & 1); | |
| 1001 s->block_last_index[n] = i; | |
| 1002 return 0; | |
| 1003 } | |
| 1004 | |
| 1005 static int mpeg2_decode_block_intra(MpegEncContext *s, | |
| 1006 DCTELEM *block, | |
| 1007 int n) | |
| 1008 { | |
| 1009 int level, dc, diff, i, j, run; | |
| 1010 int code, component; | |
| 1011 RLTable *rl; | |
| 1012 const UINT8 *scan_table; | |
| 1013 const UINT16 *matrix; | |
| 1014 int mismatch; | |
| 1015 | |
| 1016 if (s->alternate_scan) | |
| 1017 scan_table = ff_alternate_vertical_scan; | |
| 1018 else | |
| 1019 scan_table = zigzag_direct; | |
| 1020 | |
| 1021 /* DC coef */ | |
| 1022 component = (n <= 3 ? 0 : n - 4 + 1); | |
| 1023 diff = decode_dc(s, component); | |
| 1024 if (diff >= 0xffff) | |
| 1025 return -1; | |
| 1026 dc = s->last_dc[component]; | |
| 1027 dc += diff; | |
| 1028 s->last_dc[component] = dc; | |
| 1029 block[0] = dc << (3 - s->intra_dc_precision); | |
| 1030 dprintf("dc=%d\n", block[0]); | |
|
58
0e0a24def67a
fixed last zero mv for field - fixed mismatch handling for intra coefs
glantau
parents:
54
diff
changeset
|
1031 mismatch = block[0] ^ 1; |
| 0 | 1032 i = 1; |
| 1033 if (s->intra_vlc_format) | |
| 1034 rl = &rl_mpeg2; | |
| 1035 else | |
| 1036 rl = &rl_mpeg1; | |
| 1037 if (n < 4) | |
| 1038 matrix = s->intra_matrix; | |
| 1039 else | |
| 1040 matrix = s->chroma_intra_matrix; | |
|
59
efd3c19f6d62
fixed mpeg2 non intra dequant - fixed MPEG1 and 2 matrix parsing
glantau
parents:
58
diff
changeset
|
1041 |
| 0 | 1042 /* now quantify & encode AC coefs */ |
| 1043 for(;;) { | |
| 1044 code = get_vlc(&s->gb, &rl->vlc); | |
| 1045 if (code < 0) | |
| 1046 return -1; | |
| 1047 if (code == 112) { | |
| 1048 break; | |
| 1049 } else if (code == 111) { | |
| 1050 /* escape */ | |
| 1051 run = get_bits(&s->gb, 6); | |
| 1052 level = get_bits(&s->gb, 12); | |
| 1053 level = (level << 20) >> 20; | |
| 1054 } else { | |
| 1055 run = rl->table_run[code]; | |
| 1056 level = rl->table_level[code]; | |
| 21 | 1057 if (get_bits1(&s->gb)) |
| 0 | 1058 level = -level; |
| 1059 } | |
| 1060 i += run; | |
| 1061 if (i >= 64) | |
| 1062 return -1; | |
| 1063 j = scan_table[i]; | |
| 1064 dprintf("%d: run=%d level=%d\n", n, run, level); | |
| 1065 level = (level * s->qscale * matrix[j]) / 16; | |
| 1066 /* XXX: is it really necessary to saturate since the encoder | |
| 1067 knows whats going on ? */ | |
| 1068 mismatch ^= level; | |
| 1069 block[j] = level; | |
| 1070 i++; | |
| 1071 } | |
| 1072 block[63] ^= (mismatch & 1); | |
| 1073 s->block_last_index[n] = i; | |
| 1074 return 0; | |
| 1075 } | |
| 1076 | |
| 1077 /* compressed picture size */ | |
| 1078 #define PICTURE_BUFFER_SIZE 100000 | |
| 1079 | |
| 1080 typedef struct Mpeg1Context { | |
| 1081 MpegEncContext mpeg_enc_ctx; | |
| 1082 UINT32 header_state; | |
| 1083 int start_code; /* current start code */ | |
| 1084 UINT8 buffer[PICTURE_BUFFER_SIZE]; | |
| 1085 UINT8 *buf_ptr; | |
| 1086 int buffer_size; | |
| 1087 int mpeg_enc_ctx_allocated; /* true if decoding context allocated */ | |
| 1088 } Mpeg1Context; | |
| 1089 | |
| 1090 static int mpeg_decode_init(AVCodecContext *avctx) | |
| 1091 { | |
| 1092 Mpeg1Context *s = avctx->priv_data; | |
| 1093 | |
| 1094 s->header_state = 0xff; | |
| 1095 s->mpeg_enc_ctx_allocated = 0; | |
| 1096 s->buffer_size = PICTURE_BUFFER_SIZE; | |
| 1097 s->start_code = -1; | |
| 1098 s->buf_ptr = s->buffer; | |
| 1099 s->mpeg_enc_ctx.picture_number = 0; | |
| 1100 return 0; | |
| 1101 } | |
| 1102 | |
| 1103 /* return the 8 bit start code value and update the search | |
| 1104 state. Return -1 if no start code found */ | |
| 1105 static int find_start_code(UINT8 **pbuf_ptr, UINT8 *buf_end, | |
| 1106 UINT32 *header_state) | |
| 1107 { | |
| 1108 UINT8 *buf_ptr; | |
| 1109 unsigned int state, v; | |
| 1110 int val; | |
| 1111 | |
| 1112 state = *header_state; | |
| 1113 buf_ptr = *pbuf_ptr; | |
| 1114 while (buf_ptr < buf_end) { | |
| 1115 v = *buf_ptr++; | |
| 1116 if (state == 0x000001) { | |
| 1117 state = ((state << 8) | v) & 0xffffff; | |
| 1118 val = state; | |
| 1119 goto found; | |
| 1120 } | |
| 1121 state = ((state << 8) | v) & 0xffffff; | |
| 1122 } | |
| 1123 val = -1; | |
| 1124 found: | |
| 1125 *pbuf_ptr = buf_ptr; | |
| 1126 *header_state = state; | |
| 1127 return val; | |
| 1128 } | |
| 1129 | |
| 1130 static int mpeg1_decode_picture(AVCodecContext *avctx, | |
| 1131 UINT8 *buf, int buf_size) | |
| 1132 { | |
| 1133 Mpeg1Context *s1 = avctx->priv_data; | |
| 1134 MpegEncContext *s = &s1->mpeg_enc_ctx; | |
| 1135 int ref, f_code; | |
| 1136 | |
| 1137 init_get_bits(&s->gb, buf, buf_size); | |
| 1138 | |
| 1139 ref = get_bits(&s->gb, 10); /* temporal ref */ | |
| 1140 s->pict_type = get_bits(&s->gb, 3); | |
|
45
933cc4acab5c
fixed mpeg1 last block bug (mb stuffing code was not included in vlc table...)
glantau
parents:
38
diff
changeset
|
1141 dprintf("pict_type=%d number=%d\n", s->pict_type, s->picture_number); |
| 21 | 1142 skip_bits(&s->gb, 16); |
| 0 | 1143 if (s->pict_type == P_TYPE || s->pict_type == B_TYPE) { |
| 21 | 1144 s->full_pel[0] = get_bits1(&s->gb); |
| 0 | 1145 f_code = get_bits(&s->gb, 3); |
| 1146 if (f_code == 0) | |
| 1147 return -1; | |
| 1148 s->mpeg_f_code[0][0] = f_code; | |
| 1149 s->mpeg_f_code[0][1] = f_code; | |
| 1150 } | |
| 1151 if (s->pict_type == B_TYPE) { | |
| 21 | 1152 s->full_pel[1] = get_bits1(&s->gb); |
| 0 | 1153 f_code = get_bits(&s->gb, 3); |
| 1154 if (f_code == 0) | |
| 1155 return -1; | |
| 1156 s->mpeg_f_code[1][0] = f_code; | |
| 1157 s->mpeg_f_code[1][1] = f_code; | |
| 1158 } | |
| 1159 s->y_dc_scale = 8; | |
| 1160 s->c_dc_scale = 8; | |
| 1161 s->first_slice = 1; | |
| 1162 return 0; | |
| 1163 } | |
| 1164 | |
| 1165 static void mpeg_decode_sequence_extension(MpegEncContext *s) | |
| 1166 { | |
| 1167 int horiz_size_ext, vert_size_ext; | |
| 1168 int bit_rate_ext, vbv_buf_ext, low_delay; | |
| 1169 int frame_rate_ext_n, frame_rate_ext_d; | |
| 1170 | |
| 21 | 1171 skip_bits(&s->gb, 8); /* profil and level */ |
| 1172 skip_bits(&s->gb, 1); /* progressive_sequence */ | |
| 1173 skip_bits(&s->gb, 2); /* chroma_format */ | |
| 0 | 1174 horiz_size_ext = get_bits(&s->gb, 2); |
| 1175 vert_size_ext = get_bits(&s->gb, 2); | |
| 1176 s->width |= (horiz_size_ext << 12); | |
| 1177 s->height |= (vert_size_ext << 12); | |
| 1178 bit_rate_ext = get_bits(&s->gb, 12); /* XXX: handle it */ | |
| 1179 s->bit_rate = ((s->bit_rate / 400) | (bit_rate_ext << 12)) * 400; | |
| 21 | 1180 skip_bits1(&s->gb); /* marker */ |
| 0 | 1181 vbv_buf_ext = get_bits(&s->gb, 8); |
| 21 | 1182 low_delay = get_bits1(&s->gb); |
| 0 | 1183 frame_rate_ext_n = get_bits(&s->gb, 2); |
| 1184 frame_rate_ext_d = get_bits(&s->gb, 5); | |
| 1185 if (frame_rate_ext_d >= 1) | |
| 1186 s->frame_rate = (s->frame_rate * frame_rate_ext_n) / frame_rate_ext_d; | |
| 1187 dprintf("sequence extension\n"); | |
| 1188 s->mpeg2 = 1; | |
| 1189 } | |
| 1190 | |
| 1191 static void mpeg_decode_quant_matrix_extension(MpegEncContext *s) | |
| 1192 { | |
|
38
5bf15419d47e
changed quant matrix order (should fix mmx mpeg decoding bug)
glantau
parents:
21
diff
changeset
|
1193 int i, v, j; |
| 0 | 1194 |
|
59
efd3c19f6d62
fixed mpeg2 non intra dequant - fixed MPEG1 and 2 matrix parsing
glantau
parents:
58
diff
changeset
|
1195 dprintf("matrix extension\n"); |
|
efd3c19f6d62
fixed mpeg2 non intra dequant - fixed MPEG1 and 2 matrix parsing
glantau
parents:
58
diff
changeset
|
1196 |
| 21 | 1197 if (get_bits1(&s->gb)) { |
| 0 | 1198 for(i=0;i<64;i++) { |
| 1199 v = get_bits(&s->gb, 8); | |
|
59
efd3c19f6d62
fixed mpeg2 non intra dequant - fixed MPEG1 and 2 matrix parsing
glantau
parents:
58
diff
changeset
|
1200 j = zigzag_direct[i]; |
|
38
5bf15419d47e
changed quant matrix order (should fix mmx mpeg decoding bug)
glantau
parents:
21
diff
changeset
|
1201 s->intra_matrix[j] = v; |
|
5bf15419d47e
changed quant matrix order (should fix mmx mpeg decoding bug)
glantau
parents:
21
diff
changeset
|
1202 s->chroma_intra_matrix[j] = v; |
| 0 | 1203 } |
| 1204 } | |
| 21 | 1205 if (get_bits1(&s->gb)) { |
| 0 | 1206 for(i=0;i<64;i++) { |
| 1207 v = get_bits(&s->gb, 8); | |
|
59
efd3c19f6d62
fixed mpeg2 non intra dequant - fixed MPEG1 and 2 matrix parsing
glantau
parents:
58
diff
changeset
|
1208 j = zigzag_direct[i]; |
|
38
5bf15419d47e
changed quant matrix order (should fix mmx mpeg decoding bug)
glantau
parents:
21
diff
changeset
|
1209 s->non_intra_matrix[j] = v; |
|
5bf15419d47e
changed quant matrix order (should fix mmx mpeg decoding bug)
glantau
parents:
21
diff
changeset
|
1210 s->chroma_non_intra_matrix[j] = v; |
| 0 | 1211 } |
| 1212 } | |
| 21 | 1213 if (get_bits1(&s->gb)) { |
| 0 | 1214 for(i=0;i<64;i++) { |
| 1215 v = get_bits(&s->gb, 8); | |
|
59
efd3c19f6d62
fixed mpeg2 non intra dequant - fixed MPEG1 and 2 matrix parsing
glantau
parents:
58
diff
changeset
|
1216 j = zigzag_direct[i]; |
|
38
5bf15419d47e
changed quant matrix order (should fix mmx mpeg decoding bug)
glantau
parents:
21
diff
changeset
|
1217 s->chroma_intra_matrix[j] = v; |
| 0 | 1218 } |
| 1219 } | |
| 21 | 1220 if (get_bits1(&s->gb)) { |
| 0 | 1221 for(i=0;i<64;i++) { |
| 1222 v = get_bits(&s->gb, 8); | |
|
59
efd3c19f6d62
fixed mpeg2 non intra dequant - fixed MPEG1 and 2 matrix parsing
glantau
parents:
58
diff
changeset
|
1223 j = zigzag_direct[i]; |
|
38
5bf15419d47e
changed quant matrix order (should fix mmx mpeg decoding bug)
glantau
parents:
21
diff
changeset
|
1224 s->chroma_non_intra_matrix[j] = v; |
| 0 | 1225 } |
| 1226 } | |
| 1227 } | |
| 1228 | |
| 1229 static void mpeg_decode_picture_coding_extension(MpegEncContext *s) | |
| 1230 { | |
| 1231 s->full_pel[0] = s->full_pel[1] = 0; | |
| 1232 s->mpeg_f_code[0][0] = get_bits(&s->gb, 4); | |
| 1233 s->mpeg_f_code[0][1] = get_bits(&s->gb, 4); | |
| 1234 s->mpeg_f_code[1][0] = get_bits(&s->gb, 4); | |
| 1235 s->mpeg_f_code[1][1] = get_bits(&s->gb, 4); | |
| 1236 s->intra_dc_precision = get_bits(&s->gb, 2); | |
| 1237 s->picture_structure = get_bits(&s->gb, 2); | |
| 21 | 1238 s->top_field_first = get_bits1(&s->gb); |
| 1239 s->frame_pred_frame_dct = get_bits1(&s->gb); | |
| 1240 s->concealment_motion_vectors = get_bits1(&s->gb); | |
| 1241 s->q_scale_type = get_bits1(&s->gb); | |
| 1242 s->intra_vlc_format = get_bits1(&s->gb); | |
| 1243 s->alternate_scan = get_bits1(&s->gb); | |
| 1244 s->repeat_first_field = get_bits1(&s->gb); | |
| 1245 s->chroma_420_type = get_bits1(&s->gb); | |
| 1246 s->progressive_frame = get_bits1(&s->gb); | |
| 0 | 1247 /* composite display not parsed */ |
|
59
efd3c19f6d62
fixed mpeg2 non intra dequant - fixed MPEG1 and 2 matrix parsing
glantau
parents:
58
diff
changeset
|
1248 dprintf("intra_dc_precion=%d\n", s->intra_dc_precision); |
|
efd3c19f6d62
fixed mpeg2 non intra dequant - fixed MPEG1 and 2 matrix parsing
glantau
parents:
58
diff
changeset
|
1249 dprintf("picture_structure=%d\n", s->picture_structure); |
| 0 | 1250 dprintf("conceal=%d\n", s->concealment_motion_vectors); |
|
59
efd3c19f6d62
fixed mpeg2 non intra dequant - fixed MPEG1 and 2 matrix parsing
glantau
parents:
58
diff
changeset
|
1251 dprintf("intra_vlc_format=%d\n", s->intra_vlc_format); |
|
efd3c19f6d62
fixed mpeg2 non intra dequant - fixed MPEG1 and 2 matrix parsing
glantau
parents:
58
diff
changeset
|
1252 dprintf("alternate_scan=%d\n", s->alternate_scan); |
| 0 | 1253 dprintf("frame_pred_frame_dct=%d\n", s->frame_pred_frame_dct); |
| 1254 } | |
| 1255 | |
| 1256 static void mpeg_decode_extension(AVCodecContext *avctx, | |
| 1257 UINT8 *buf, int buf_size) | |
| 1258 { | |
| 1259 Mpeg1Context *s1 = avctx->priv_data; | |
| 1260 MpegEncContext *s = &s1->mpeg_enc_ctx; | |
| 1261 int ext_type; | |
| 1262 | |
| 1263 init_get_bits(&s->gb, buf, buf_size); | |
| 1264 | |
| 1265 ext_type = get_bits(&s->gb, 4); | |
| 1266 switch(ext_type) { | |
| 1267 case 0x1: | |
| 1268 /* sequence ext */ | |
| 1269 mpeg_decode_sequence_extension(s); | |
| 1270 break; | |
| 1271 case 0x3: | |
| 1272 /* quant matrix extension */ | |
| 1273 mpeg_decode_quant_matrix_extension(s); | |
| 1274 break; | |
| 1275 case 0x8: | |
| 1276 /* picture extension */ | |
| 1277 mpeg_decode_picture_coding_extension(s); | |
| 1278 break; | |
| 1279 } | |
| 1280 } | |
| 1281 | |
| 1282 /* return 1 if end of frame */ | |
| 1283 static int mpeg_decode_slice(AVCodecContext *avctx, | |
| 1284 AVPicture *pict, | |
| 1285 int start_code, | |
| 1286 UINT8 *buf, int buf_size) | |
| 1287 { | |
| 1288 Mpeg1Context *s1 = avctx->priv_data; | |
| 1289 MpegEncContext *s = &s1->mpeg_enc_ctx; | |
| 1290 int ret; | |
| 1291 | |
| 1292 start_code = (start_code - 1) & 0xff; | |
| 1293 if (start_code >= s->mb_height) | |
| 1294 return -1; | |
| 1295 s->last_dc[0] = 1 << (7 + s->intra_dc_precision); | |
| 1296 s->last_dc[1] = s->last_dc[0]; | |
| 1297 s->last_dc[2] = s->last_dc[0]; | |
| 1298 memset(s->last_mv, 0, sizeof(s->last_mv)); | |
| 1299 s->mb_x = -1; | |
| 1300 s->mb_y = start_code; | |
| 1301 s->mb_incr = 0; | |
| 1302 | |
| 1303 /* start frame decoding */ | |
| 1304 if (s->first_slice) { | |
| 1305 s->first_slice = 0; | |
| 1306 MPV_frame_start(s); | |
| 1307 } | |
| 1308 | |
| 1309 init_get_bits(&s->gb, buf, buf_size); | |
| 1310 | |
| 54 | 1311 s->qscale = get_qscale(s); |
| 0 | 1312 /* extra slice info */ |
| 21 | 1313 while (get_bits1(&s->gb) != 0) { |
| 1314 skip_bits(&s->gb, 8); | |
| 0 | 1315 } |
| 1316 | |
| 1317 for(;;) { | |
|
12
4d50c7d89e0f
use block[] in structure to have it aligned on 8 bytes for mmx optimizations
glantau
parents:
7
diff
changeset
|
1318 memset(s->block, 0, sizeof(s->block)); |
|
4d50c7d89e0f
use block[] in structure to have it aligned on 8 bytes for mmx optimizations
glantau
parents:
7
diff
changeset
|
1319 ret = mpeg_decode_mb(s, s->block); |
| 0 | 1320 dprintf("ret=%d\n", ret); |
| 1321 if (ret < 0) | |
| 1322 return -1; | |
| 1323 if (ret == 1) | |
| 1324 break; | |
|
12
4d50c7d89e0f
use block[] in structure to have it aligned on 8 bytes for mmx optimizations
glantau
parents:
7
diff
changeset
|
1325 MPV_decode_mb(s, s->block); |
| 0 | 1326 } |
| 1327 | |
| 1328 /* end of slice reached */ | |
| 1329 if (s->mb_x == (s->mb_width - 1) && | |
| 1330 s->mb_y == (s->mb_height - 1)) { | |
| 1331 /* end of image */ | |
| 1332 UINT8 **picture; | |
| 1333 | |
| 1334 MPV_frame_end(s); | |
| 1335 | |
| 1336 /* XXX: incorrect reported qscale for mpeg2 */ | |
| 1337 if (s->pict_type == B_TYPE) { | |
| 1338 picture = s->current_picture; | |
| 1339 avctx->quality = s->qscale; | |
| 1340 } else { | |
| 1341 /* latency of 1 frame for I and P frames */ | |
| 1342 /* XXX: use another variable than picture_number */ | |
| 1343 if (s->picture_number == 0) { | |
| 1344 picture = NULL; | |
| 1345 } else { | |
| 1346 picture = s->last_picture; | |
| 1347 avctx->quality = s->last_qscale; | |
| 1348 } | |
| 1349 s->last_qscale = s->qscale; | |
| 1350 s->picture_number++; | |
| 1351 } | |
| 1352 if (picture) { | |
| 1353 pict->data[0] = picture[0]; | |
| 1354 pict->data[1] = picture[1]; | |
| 1355 pict->data[2] = picture[2]; | |
| 1356 pict->linesize[0] = s->linesize; | |
| 1357 pict->linesize[1] = s->linesize / 2; | |
| 1358 pict->linesize[2] = s->linesize / 2; | |
| 1359 return 1; | |
| 1360 } else { | |
| 1361 return 0; | |
| 1362 } | |
| 1363 } else { | |
| 1364 return 0; | |
| 1365 } | |
| 1366 } | |
| 1367 | |
| 1368 static int mpeg1_decode_sequence(AVCodecContext *avctx, | |
| 1369 UINT8 *buf, int buf_size) | |
| 1370 { | |
| 1371 Mpeg1Context *s1 = avctx->priv_data; | |
| 1372 MpegEncContext *s = &s1->mpeg_enc_ctx; | |
|
38
5bf15419d47e
changed quant matrix order (should fix mmx mpeg decoding bug)
glantau
parents:
21
diff
changeset
|
1373 int width, height, i, v, j; |
| 0 | 1374 |
| 1375 init_get_bits(&s->gb, buf, buf_size); | |
| 1376 | |
| 1377 width = get_bits(&s->gb, 12); | |
| 1378 height = get_bits(&s->gb, 12); | |
| 21 | 1379 skip_bits(&s->gb, 4); |
| 0 | 1380 s->frame_rate_index = get_bits(&s->gb, 4); |
| 1381 if (s->frame_rate_index == 0) | |
| 1382 return -1; | |
| 1383 s->bit_rate = get_bits(&s->gb, 18) * 400; | |
| 21 | 1384 if (get_bits1(&s->gb) == 0) /* marker */ |
| 0 | 1385 return -1; |
| 1386 if (width <= 0 || height <= 0 || | |
| 1387 (width % 2) != 0 || (height % 2) != 0) | |
| 1388 return -1; | |
| 1389 if (width != s->width || | |
| 1390 height != s->height) { | |
| 1391 /* start new mpeg1 context decoding */ | |
| 1392 s->out_format = FMT_MPEG1; | |
| 1393 if (s1->mpeg_enc_ctx_allocated) { | |
| 1394 MPV_common_end(s); | |
| 1395 } | |
| 1396 s->width = width; | |
| 1397 s->height = height; | |
| 1398 s->has_b_frames = 1; | |
| 69 | 1399 s->avctx = avctx; |
| 0 | 1400 avctx->width = width; |
| 1401 avctx->height = height; | |
| 1402 avctx->frame_rate = frame_rate_tab[s->frame_rate_index]; | |
| 1403 avctx->bit_rate = s->bit_rate; | |
| 1404 | |
| 1405 if (MPV_common_init(s) < 0) | |
| 1406 return -1; | |
| 1407 mpeg1_init_vlc(s); | |
| 1408 s1->mpeg_enc_ctx_allocated = 1; | |
| 1409 } | |
| 1410 | |
| 21 | 1411 skip_bits(&s->gb, 10); /* vbv_buffer_size */ |
| 1412 skip_bits(&s->gb, 1); | |
| 0 | 1413 |
| 1414 /* get matrix */ | |
| 21 | 1415 if (get_bits1(&s->gb)) { |
| 0 | 1416 for(i=0;i<64;i++) { |
| 1417 v = get_bits(&s->gb, 8); | |
|
59
efd3c19f6d62
fixed mpeg2 non intra dequant - fixed MPEG1 and 2 matrix parsing
glantau
parents:
58
diff
changeset
|
1418 j = zigzag_direct[i]; |
|
38
5bf15419d47e
changed quant matrix order (should fix mmx mpeg decoding bug)
glantau
parents:
21
diff
changeset
|
1419 s->intra_matrix[j] = v; |
|
5bf15419d47e
changed quant matrix order (should fix mmx mpeg decoding bug)
glantau
parents:
21
diff
changeset
|
1420 s->chroma_intra_matrix[j] = v; |
| 0 | 1421 } |
|
59
efd3c19f6d62
fixed mpeg2 non intra dequant - fixed MPEG1 and 2 matrix parsing
glantau
parents:
58
diff
changeset
|
1422 #ifdef DEBUG |
|
efd3c19f6d62
fixed mpeg2 non intra dequant - fixed MPEG1 and 2 matrix parsing
glantau
parents:
58
diff
changeset
|
1423 dprintf("intra matrix present\n"); |
|
efd3c19f6d62
fixed mpeg2 non intra dequant - fixed MPEG1 and 2 matrix parsing
glantau
parents:
58
diff
changeset
|
1424 for(i=0;i<64;i++) |
|
efd3c19f6d62
fixed mpeg2 non intra dequant - fixed MPEG1 and 2 matrix parsing
glantau
parents:
58
diff
changeset
|
1425 dprintf(" %d", s->intra_matrix[zigzag_direct[i]]); |
|
efd3c19f6d62
fixed mpeg2 non intra dequant - fixed MPEG1 and 2 matrix parsing
glantau
parents:
58
diff
changeset
|
1426 printf("\n"); |
|
efd3c19f6d62
fixed mpeg2 non intra dequant - fixed MPEG1 and 2 matrix parsing
glantau
parents:
58
diff
changeset
|
1427 #endif |
| 0 | 1428 } else { |
| 1429 for(i=0;i<64;i++) { | |
| 1430 v = default_intra_matrix[i]; | |
| 1431 s->intra_matrix[i] = v; | |
| 1432 s->chroma_intra_matrix[i] = v; | |
| 1433 } | |
| 1434 } | |
| 21 | 1435 if (get_bits1(&s->gb)) { |
| 0 | 1436 for(i=0;i<64;i++) { |
| 1437 v = get_bits(&s->gb, 8); | |
|
59
efd3c19f6d62
fixed mpeg2 non intra dequant - fixed MPEG1 and 2 matrix parsing
glantau
parents:
58
diff
changeset
|
1438 j = zigzag_direct[i]; |
|
38
5bf15419d47e
changed quant matrix order (should fix mmx mpeg decoding bug)
glantau
parents:
21
diff
changeset
|
1439 s->non_intra_matrix[j] = v; |
|
5bf15419d47e
changed quant matrix order (should fix mmx mpeg decoding bug)
glantau
parents:
21
diff
changeset
|
1440 s->chroma_non_intra_matrix[j] = v; |
| 0 | 1441 } |
|
59
efd3c19f6d62
fixed mpeg2 non intra dequant - fixed MPEG1 and 2 matrix parsing
glantau
parents:
58
diff
changeset
|
1442 #ifdef DEBUG |
|
efd3c19f6d62
fixed mpeg2 non intra dequant - fixed MPEG1 and 2 matrix parsing
glantau
parents:
58
diff
changeset
|
1443 dprintf("non intra matrix present\n"); |
|
efd3c19f6d62
fixed mpeg2 non intra dequant - fixed MPEG1 and 2 matrix parsing
glantau
parents:
58
diff
changeset
|
1444 for(i=0;i<64;i++) |
|
efd3c19f6d62
fixed mpeg2 non intra dequant - fixed MPEG1 and 2 matrix parsing
glantau
parents:
58
diff
changeset
|
1445 dprintf(" %d", s->non_intra_matrix[zigzag_direct[i]]); |
|
efd3c19f6d62
fixed mpeg2 non intra dequant - fixed MPEG1 and 2 matrix parsing
glantau
parents:
58
diff
changeset
|
1446 printf("\n"); |
|
efd3c19f6d62
fixed mpeg2 non intra dequant - fixed MPEG1 and 2 matrix parsing
glantau
parents:
58
diff
changeset
|
1447 #endif |
| 0 | 1448 } else { |
| 1449 for(i=0;i<64;i++) { | |
| 1450 v = default_non_intra_matrix[i]; | |
| 1451 s->non_intra_matrix[i] = v; | |
| 1452 s->chroma_non_intra_matrix[i] = v; | |
| 1453 } | |
| 1454 } | |
| 1455 | |
| 1456 /* we set mpeg2 parameters so that it emulates mpeg1 */ | |
| 1457 s->progressive_sequence = 1; | |
| 1458 s->progressive_frame = 1; | |
| 1459 s->picture_structure = PICT_FRAME; | |
| 1460 s->frame_pred_frame_dct = 1; | |
| 1461 s->mpeg2 = 0; | |
| 1462 return 0; | |
| 1463 } | |
| 1464 | |
| 1465 /* handle buffering and image synchronisation */ | |
| 1466 static int mpeg_decode_frame(AVCodecContext *avctx, | |
| 1467 void *data, int *data_size, | |
| 1468 UINT8 *buf, int buf_size) | |
| 1469 { | |
| 1470 Mpeg1Context *s = avctx->priv_data; | |
| 1471 UINT8 *buf_end, *buf_ptr, *buf_start; | |
| 1472 int len, start_code_found, ret, code, start_code, input_size; | |
| 1473 AVPicture *picture = data; | |
| 1474 | |
| 1475 dprintf("fill_buffer\n"); | |
| 1476 | |
| 1477 *data_size = 0; | |
| 1478 /* special case for last picture */ | |
| 1479 if (buf_size == 0) { | |
| 1480 MpegEncContext *s2 = &s->mpeg_enc_ctx; | |
| 1481 if (s2->picture_number > 0) { | |
| 1482 picture->data[0] = s2->next_picture[0]; | |
| 1483 picture->data[1] = s2->next_picture[1]; | |
| 1484 picture->data[2] = s2->next_picture[2]; | |
| 1485 picture->linesize[0] = s2->linesize; | |
| 1486 picture->linesize[1] = s2->linesize / 2; | |
| 1487 picture->linesize[2] = s2->linesize / 2; | |
| 1488 *data_size = sizeof(AVPicture); | |
| 1489 } | |
| 1490 return 0; | |
| 1491 } | |
| 1492 | |
| 1493 buf_ptr = buf; | |
| 1494 buf_end = buf + buf_size; | |
| 1495 while (buf_ptr < buf_end) { | |
| 1496 buf_start = buf_ptr; | |
| 1497 /* find start next code */ | |
| 1498 code = find_start_code(&buf_ptr, buf_end, &s->header_state); | |
| 1499 if (code >= 0) { | |
| 1500 start_code_found = 1; | |
| 1501 } else { | |
| 1502 start_code_found = 0; | |
| 1503 } | |
| 1504 /* copy to buffer */ | |
| 1505 len = buf_ptr - buf_start; | |
| 1506 if (len + (s->buf_ptr - s->buffer) > s->buffer_size) { | |
| 1507 /* data too big : flush */ | |
| 1508 s->buf_ptr = s->buffer; | |
| 1509 if (start_code_found) | |
| 1510 s->start_code = code; | |
| 1511 } else { | |
| 1512 memcpy(s->buf_ptr, buf_start, len); | |
| 1513 s->buf_ptr += len; | |
| 1514 | |
| 1515 if (start_code_found) { | |
| 1516 /* prepare data for next start code */ | |
| 1517 input_size = s->buf_ptr - s->buffer; | |
| 1518 start_code = s->start_code; | |
| 1519 s->buf_ptr = s->buffer; | |
| 1520 s->start_code = code; | |
| 1521 switch(start_code) { | |
| 1522 case SEQ_START_CODE: | |
| 1523 mpeg1_decode_sequence(avctx, s->buffer, | |
| 1524 input_size); | |
| 1525 break; | |
| 1526 | |
| 1527 case PICTURE_START_CODE: | |
| 1528 /* we have a complete image : we try to decompress it */ | |
| 1529 mpeg1_decode_picture(avctx, | |
| 1530 s->buffer, input_size); | |
| 1531 break; | |
| 1532 case EXT_START_CODE: | |
| 1533 mpeg_decode_extension(avctx, | |
| 1534 s->buffer, input_size); | |
| 1535 break; | |
| 1536 default: | |
| 1537 if (start_code >= SLICE_MIN_START_CODE && | |
| 1538 start_code <= SLICE_MAX_START_CODE) { | |
| 1539 ret = mpeg_decode_slice(avctx, picture, | |
| 1540 start_code, s->buffer, input_size); | |
| 1541 if (ret == 1) { | |
| 1542 /* got a picture: exit */ | |
| 1543 *data_size = sizeof(AVPicture); | |
| 1544 goto the_end; | |
| 1545 } | |
| 1546 } | |
| 1547 break; | |
| 1548 } | |
| 1549 } | |
| 1550 } | |
| 1551 } | |
| 1552 the_end: | |
| 1553 return buf_ptr - buf; | |
| 1554 } | |
| 1555 | |
| 1556 static int mpeg_decode_end(AVCodecContext *avctx) | |
| 1557 { | |
| 1558 Mpeg1Context *s = avctx->priv_data; | |
| 1559 | |
| 1560 if (s->mpeg_enc_ctx_allocated) | |
| 1561 MPV_common_end(&s->mpeg_enc_ctx); | |
| 1562 return 0; | |
| 1563 } | |
| 1564 | |
| 1565 AVCodec mpeg_decoder = { | |
| 1566 "mpegvideo", | |
| 1567 CODEC_TYPE_VIDEO, | |
| 1568 CODEC_ID_MPEG1VIDEO, | |
| 1569 sizeof(Mpeg1Context), | |
| 1570 mpeg_decode_init, | |
| 1571 NULL, | |
| 1572 mpeg_decode_end, | |
| 1573 mpeg_decode_frame, | |
| 1574 }; |
