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