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