Mercurial > libavcodec.hg
annotate mpeg12.c @ 54:764f2eaf711e libavcodec
fixed mpeg2 qscale decoding
| author | glantau |
|---|---|
| date | Sat, 11 Aug 2001 19:04:08 +0000 |
| parents | 933cc4acab5c |
| children | 0e0a24def67a |
| 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; | |
| 557 } else { | |
| 558 /* if B type, reuse previous vectors and directions */ | |
| 559 s->mv[0][0][0] = s->last_mv[0][0][0]; | |
| 560 s->mv[0][0][1] = s->last_mv[0][0][1]; | |
| 561 s->mv[1][0][0] = s->last_mv[1][0][0]; | |
| 562 s->mv[1][0][1] = s->last_mv[1][0][1]; | |
| 563 } | |
|
7
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
1
diff
changeset
|
564 s->mb_skiped = 1; |
| 0 | 565 return 0; |
| 566 } | |
| 567 | |
| 568 switch(s->pict_type) { | |
| 569 default: | |
| 570 case I_TYPE: | |
| 21 | 571 if (get_bits1(&s->gb) == 0) { |
| 572 if (get_bits1(&s->gb) == 0) | |
| 0 | 573 return -1; |
| 574 mb_type = MB_QUANT | MB_INTRA; | |
| 575 } else { | |
| 576 mb_type = MB_INTRA; | |
| 577 } | |
| 578 break; | |
| 579 case P_TYPE: | |
| 580 mb_type = get_vlc(&s->gb, &mb_ptype_vlc); | |
| 581 if (mb_type < 0) | |
| 582 return -1; | |
| 583 break; | |
| 584 case B_TYPE: | |
| 585 mb_type = get_vlc(&s->gb, &mb_btype_vlc); | |
| 586 if (mb_type < 0) | |
| 587 return -1; | |
| 588 break; | |
| 589 } | |
| 590 dprintf("mb_type=%x\n", mb_type); | |
| 591 motion_type = 0; /* avoid warning */ | |
| 592 if (mb_type & (MB_FOR|MB_BACK)) { | |
| 593 /* get additionnal motion vector type */ | |
| 594 if (s->picture_structure == PICT_FRAME && s->frame_pred_frame_dct) | |
| 595 motion_type = MT_FRAME; | |
| 596 else | |
| 597 motion_type = get_bits(&s->gb, 2); | |
| 598 } | |
| 599 /* compute dct type */ | |
| 600 if (s->picture_structure == PICT_FRAME && | |
| 601 !s->frame_pred_frame_dct && | |
| 602 (mb_type & (MB_PAT | MB_INTRA))) { | |
| 21 | 603 s->interlaced_dct = get_bits1(&s->gb); |
| 0 | 604 #ifdef DEBUG |
| 605 if (s->interlaced_dct) | |
| 606 printf("interlaced_dct\n"); | |
| 607 #endif | |
| 608 } else { | |
| 609 s->interlaced_dct = 0; /* frame based */ | |
| 610 } | |
| 611 | |
| 612 if (mb_type & MB_QUANT) { | |
| 54 | 613 s->qscale = get_qscale(s); |
| 0 | 614 } |
| 615 if (mb_type & MB_INTRA) { | |
| 616 if (s->concealment_motion_vectors) { | |
| 617 /* just parse them */ | |
| 618 if (s->picture_structure != PICT_FRAME) | |
| 21 | 619 skip_bits1(&s->gb); /* field select */ |
| 0 | 620 mpeg_decode_motion(s, s->mpeg_f_code[0][0], 0); |
| 621 mpeg_decode_motion(s, s->mpeg_f_code[0][1], 0); | |
| 622 } | |
| 623 s->mb_intra = 1; | |
| 624 cbp = 0x3f; | |
| 625 memset(s->last_mv, 0, sizeof(s->last_mv)); /* reset mv prediction */ | |
| 626 } else { | |
| 627 s->mb_intra = 0; | |
| 628 cbp = 0; | |
| 629 } | |
| 630 /* special case of implicit zero motion vector */ | |
| 631 if (s->pict_type == P_TYPE && !(mb_type & MB_FOR)) { | |
| 632 s->mv_dir = MV_DIR_FORWARD; | |
| 633 s->mv_type = MV_TYPE_16X16; | |
| 634 s->last_mv[0][0][0] = 0; | |
| 635 s->last_mv[0][0][1] = 0; | |
| 636 s->mv[0][0][0] = 0; | |
| 637 s->mv[0][0][1] = 0; | |
| 638 } else if (mb_type & (MB_FOR | MB_BACK)) { | |
| 639 /* motion vectors */ | |
| 640 s->mv_dir = 0; | |
| 641 for(i=0;i<2;i++) { | |
| 642 if (mb_type & (MB_FOR >> i)) { | |
| 643 s->mv_dir |= (MV_DIR_FORWARD >> i); | |
| 644 dprintf("mv_type=%d\n", motion_type); | |
| 645 switch(motion_type) { | |
| 646 case MT_FRAME: /* or MT_16X8 */ | |
| 647 if (s->picture_structure == PICT_FRAME) { | |
| 648 /* MT_FRAME */ | |
| 649 s->mv_type = MV_TYPE_16X16; | |
| 650 for(k=0;k<2;k++) { | |
| 651 val = mpeg_decode_motion(s, s->mpeg_f_code[i][k], | |
| 652 s->last_mv[i][0][k]); | |
| 653 s->last_mv[i][0][k] = val; | |
| 654 s->last_mv[i][1][k] = val; | |
| 655 /* full_pel: only for mpeg1 */ | |
| 656 if (s->full_pel[i]) | |
| 657 val = val << 1; | |
| 658 s->mv[i][0][k] = val; | |
| 659 dprintf("mv%d: %d\n", k, val); | |
| 660 } | |
| 661 } else { | |
| 662 /* MT_16X8 */ | |
| 663 s->mv_type = MV_TYPE_16X8; | |
| 664 for(j=0;j<2;j++) { | |
| 21 | 665 s->field_select[i][j] = get_bits1(&s->gb); |
| 0 | 666 for(k=0;k<2;k++) { |
| 667 val = mpeg_decode_motion(s, s->mpeg_f_code[i][k], | |
| 668 s->last_mv[i][j][k]); | |
| 669 s->last_mv[i][j][k] = val; | |
| 670 s->mv[i][j][k] = val; | |
| 671 } | |
| 672 } | |
| 673 } | |
| 674 break; | |
| 675 case MT_FIELD: | |
| 676 if (s->picture_structure == PICT_FRAME) { | |
| 677 s->mv_type = MV_TYPE_FIELD; | |
| 678 for(j=0;j<2;j++) { | |
| 21 | 679 s->field_select[i][j] = get_bits1(&s->gb); |
| 0 | 680 val = mpeg_decode_motion(s, s->mpeg_f_code[i][0], |
| 681 s->last_mv[i][j][0]); | |
| 682 s->last_mv[i][j][0] = val; | |
| 683 s->mv[i][j][0] = val; | |
| 684 dprintf("fmx=%d\n", val); | |
| 685 val = mpeg_decode_motion(s, s->mpeg_f_code[i][1], | |
| 686 s->last_mv[i][j][1] >> 1); | |
| 687 s->last_mv[i][j][1] = val << 1; | |
| 688 s->mv[i][j][1] = val; | |
| 689 dprintf("fmy=%d\n", val); | |
| 690 } | |
| 691 } else { | |
| 692 s->mv_type = MV_TYPE_16X16; | |
| 21 | 693 s->field_select[i][0] = get_bits1(&s->gb); |
| 0 | 694 for(k=0;k<2;k++) { |
| 695 val = mpeg_decode_motion(s, s->mpeg_f_code[i][k], | |
| 696 s->last_mv[i][0][k]); | |
| 697 s->last_mv[i][0][k] = val; | |
| 698 s->last_mv[i][1][k] = val; | |
| 699 s->mv[i][0][k] = val; | |
| 700 } | |
| 701 } | |
| 702 break; | |
| 703 case MT_DMV: | |
| 704 { | |
| 705 int dmx, dmy, mx, my, m; | |
| 706 | |
| 707 mx = mpeg_decode_motion(s, s->mpeg_f_code[i][0], | |
| 708 s->last_mv[i][0][0]); | |
| 709 s->last_mv[i][0][0] = mx; | |
| 710 s->last_mv[i][1][0] = mx; | |
| 711 dmx = get_dmv(s); | |
| 712 my = mpeg_decode_motion(s, s->mpeg_f_code[i][1], | |
| 713 s->last_mv[i][0][1] >> 1); | |
| 714 dmy = get_dmv(s); | |
| 715 s->mv_type = MV_TYPE_DMV; | |
| 716 /* XXX: totally broken */ | |
| 717 if (s->picture_structure == PICT_FRAME) { | |
| 718 s->last_mv[i][0][1] = my << 1; | |
| 719 s->last_mv[i][1][1] = my << 1; | |
| 720 | |
| 721 m = s->top_field_first ? 1 : 3; | |
| 722 /* top -> top pred */ | |
| 723 s->mv[i][0][0] = mx; | |
| 724 s->mv[i][0][1] = my << 1; | |
| 725 s->mv[i][1][0] = ((mx * m + (mx > 0)) >> 1) + dmx; | |
| 726 s->mv[i][1][1] = ((my * m + (my > 0)) >> 1) + dmy - 1; | |
| 727 m = 4 - m; | |
| 728 s->mv[i][2][0] = mx; | |
| 729 s->mv[i][2][1] = my << 1; | |
| 730 s->mv[i][3][0] = ((mx * m + (mx > 0)) >> 1) + dmx; | |
| 731 s->mv[i][3][1] = ((my * m + (my > 0)) >> 1) + dmy + 1; | |
| 732 } else { | |
| 733 s->last_mv[i][0][1] = my; | |
| 734 s->last_mv[i][1][1] = my; | |
| 735 s->mv[i][0][0] = mx; | |
| 736 s->mv[i][0][1] = my; | |
| 737 s->mv[i][1][0] = ((mx + (mx > 0)) >> 1) + dmx; | |
| 738 s->mv[i][1][1] = ((my + (my > 0)) >> 1) + dmy - 1 | |
| 739 /* + 2 * cur_field */; | |
| 740 } | |
| 741 } | |
| 742 break; | |
| 743 } | |
| 744 } | |
| 745 } | |
| 746 } | |
| 747 | |
| 748 if ((mb_type & MB_INTRA) && s->concealment_motion_vectors) { | |
| 21 | 749 skip_bits1(&s->gb); /* marker */ |
| 0 | 750 } |
| 751 | |
| 752 if (mb_type & MB_PAT) { | |
| 753 cbp = get_vlc(&s->gb, &mb_pat_vlc); | |
| 754 if (cbp < 0) | |
| 755 return -1; | |
| 756 cbp++; | |
| 757 } | |
| 758 dprintf("cbp=%x\n", cbp); | |
| 759 | |
| 760 if (s->mpeg2) { | |
| 761 if (s->mb_intra) { | |
| 762 for(i=0;i<6;i++) { | |
| 763 if (cbp & (1 << (5 - i))) { | |
| 764 if (mpeg2_decode_block_intra(s, block[i], i) < 0) | |
| 765 return -1; | |
| 766 } | |
| 767 } | |
| 768 } else { | |
| 769 for(i=0;i<6;i++) { | |
| 770 if (cbp & (1 << (5 - i))) { | |
| 771 if (mpeg2_decode_block_non_intra(s, block[i], i) < 0) | |
| 772 return -1; | |
| 773 } | |
| 774 } | |
| 775 } | |
| 776 } else { | |
| 777 for(i=0;i<6;i++) { | |
| 778 if (cbp & (1 << (5 - i))) { | |
| 779 if (mpeg1_decode_block(s, block[i], i) < 0) | |
| 780 return -1; | |
| 781 } | |
| 782 } | |
| 783 } | |
| 784 return 0; | |
| 785 } | |
| 786 | |
| 787 /* as h263, but only 17 codes */ | |
| 788 static int mpeg_decode_motion(MpegEncContext *s, int fcode, int pred) | |
| 789 { | |
| 790 int code, sign, val, m, l, shift; | |
| 791 | |
| 792 code = get_vlc(&s->gb, &mv_vlc); | |
| 793 if (code < 0) { | |
| 794 return 0xffff; | |
| 795 } | |
| 796 if (code == 0) { | |
| 797 return pred; | |
| 798 } | |
| 21 | 799 sign = get_bits1(&s->gb); |
| 0 | 800 shift = fcode - 1; |
| 801 val = (code - 1) << shift; | |
| 802 if (shift > 0) | |
| 803 val |= get_bits(&s->gb, shift); | |
| 804 val++; | |
| 805 if (sign) | |
| 806 val = -val; | |
| 807 val += pred; | |
| 808 | |
| 809 /* modulo decoding */ | |
| 810 l = (1 << shift) * 16; | |
| 811 m = 2 * l; | |
| 812 if (val < -l) { | |
| 813 val += m; | |
| 814 } else if (val >= l) { | |
| 815 val -= m; | |
| 816 } | |
| 817 return val; | |
| 818 } | |
| 819 | |
| 820 static inline int decode_dc(MpegEncContext *s, int component) | |
| 821 { | |
| 822 int code, diff; | |
| 823 | |
| 824 if (component == 0) { | |
| 825 code = get_vlc(&s->gb, &dc_lum_vlc); | |
| 826 } else { | |
| 827 code = get_vlc(&s->gb, &dc_chroma_vlc); | |
| 828 } | |
| 829 if (code < 0) | |
| 830 return 0xffff; | |
| 831 if (code == 0) { | |
| 832 diff = 0; | |
| 833 } else { | |
| 834 diff = get_bits(&s->gb, code); | |
| 835 if ((diff & (1 << (code - 1))) == 0) | |
| 836 diff = (-1 << code) | (diff + 1); | |
| 837 } | |
| 838 return diff; | |
| 839 } | |
| 840 | |
| 841 static int mpeg1_decode_block(MpegEncContext *s, | |
| 842 DCTELEM *block, | |
| 843 int n) | |
| 844 { | |
| 845 int level, dc, diff, i, j, run; | |
| 846 int code, component; | |
| 847 RLTable *rl = &rl_mpeg1; | |
| 848 | |
| 849 if (s->mb_intra) { | |
| 850 /* DC coef */ | |
| 851 component = (n <= 3 ? 0 : n - 4 + 1); | |
| 852 diff = decode_dc(s, component); | |
| 853 if (diff >= 0xffff) | |
| 854 return -1; | |
| 855 dc = s->last_dc[component]; | |
| 856 dc += diff; | |
| 857 s->last_dc[component] = dc; | |
| 858 block[0] = dc; | |
| 859 dprintf("dc=%d diff=%d\n", dc, diff); | |
| 860 i = 1; | |
| 861 } else { | |
| 862 int bit_cnt, v; | |
| 863 UINT32 bit_buf; | |
| 864 UINT8 *buf_ptr; | |
| 865 i = 0; | |
| 866 /* special case for the first coef. no need to add a second vlc table */ | |
| 867 SAVE_BITS(&s->gb); | |
| 868 SHOW_BITS(&s->gb, v, 2); | |
| 869 if (v & 2) { | |
| 870 run = 0; | |
| 871 level = 1 - ((v & 1) << 1); | |
| 872 FLUSH_BITS(2); | |
| 873 RESTORE_BITS(&s->gb); | |
| 874 goto add_coef; | |
| 875 } | |
| 876 RESTORE_BITS(&s->gb); | |
| 877 } | |
| 878 | |
| 879 /* now quantify & encode AC coefs */ | |
| 880 for(;;) { | |
| 881 code = get_vlc(&s->gb, &rl->vlc); | |
| 882 if (code < 0) { | |
| 883 return -1; | |
| 884 } | |
| 885 if (code == 112) { | |
| 886 break; | |
| 887 } else if (code == 111) { | |
| 888 /* escape */ | |
| 889 run = get_bits(&s->gb, 6); | |
| 890 level = get_bits(&s->gb, 8); | |
| 891 level = (level << 24) >> 24; | |
| 892 if (level == -128) { | |
| 893 level = get_bits(&s->gb, 8) - 256; | |
| 894 } else if (level == 0) { | |
| 895 level = get_bits(&s->gb, 8); | |
| 896 } | |
| 897 } else { | |
| 898 run = rl->table_run[code]; | |
| 899 level = rl->table_level[code]; | |
| 21 | 900 if (get_bits1(&s->gb)) |
| 0 | 901 level = -level; |
| 902 } | |
| 903 i += run; | |
| 904 if (i >= 64) | |
| 905 return -1; | |
| 906 add_coef: | |
| 907 dprintf("%d: run=%d level=%d\n", n, run, level); | |
| 908 j = zigzag_direct[i]; | |
| 909 block[j] = level; | |
| 910 i++; | |
| 911 } | |
| 912 s->block_last_index[n] = i; | |
| 913 return 0; | |
| 914 } | |
| 915 | |
| 916 /* Also does unquantization here, since I will never support mpeg2 | |
| 917 encoding */ | |
| 918 static int mpeg2_decode_block_non_intra(MpegEncContext *s, | |
| 919 DCTELEM *block, | |
| 920 int n) | |
| 921 { | |
| 922 int level, i, j, run; | |
| 923 int code; | |
| 924 RLTable *rl = &rl_mpeg1; | |
| 925 const UINT8 *scan_table; | |
| 926 const UINT16 *matrix; | |
| 927 int mismatch; | |
| 928 | |
| 929 if (s->alternate_scan) | |
| 930 scan_table = ff_alternate_vertical_scan; | |
| 931 else | |
| 932 scan_table = zigzag_direct; | |
| 933 mismatch = 1; | |
| 934 | |
| 935 { | |
| 936 int bit_cnt, v; | |
| 937 UINT32 bit_buf; | |
| 938 UINT8 *buf_ptr; | |
| 939 i = 0; | |
| 940 if (n < 4) | |
| 941 matrix = s->non_intra_matrix; | |
| 942 else | |
| 943 matrix = s->chroma_non_intra_matrix; | |
| 944 | |
| 945 /* special case for the first coef. no need to add a second vlc table */ | |
| 946 SAVE_BITS(&s->gb); | |
| 947 SHOW_BITS(&s->gb, v, 2); | |
| 948 if (v & 2) { | |
| 949 run = 0; | |
| 950 level = 1 - ((v & 1) << 1); | |
| 951 FLUSH_BITS(2); | |
| 952 RESTORE_BITS(&s->gb); | |
| 953 goto add_coef; | |
| 954 } | |
| 955 RESTORE_BITS(&s->gb); | |
| 956 } | |
| 957 | |
| 958 /* now quantify & encode AC coefs */ | |
| 959 for(;;) { | |
| 960 code = get_vlc(&s->gb, &rl->vlc); | |
| 961 if (code < 0) | |
| 962 return -1; | |
| 963 if (code == 112) { | |
| 964 break; | |
| 965 } else if (code == 111) { | |
| 966 /* escape */ | |
| 967 run = get_bits(&s->gb, 6); | |
| 968 level = get_bits(&s->gb, 12); | |
| 969 level = (level << 20) >> 20; | |
| 970 } else { | |
| 971 run = rl->table_run[code]; | |
| 972 level = rl->table_level[code]; | |
| 21 | 973 if (get_bits1(&s->gb)) |
| 0 | 974 level = -level; |
| 975 } | |
| 976 i += run; | |
| 977 if (i >= 64) | |
| 978 return -1; | |
| 979 add_coef: | |
| 980 j = scan_table[i]; | |
| 981 dprintf("%d: run=%d level=%d\n", n, run, level); | |
| 982 level = ((level * 2 + 1) * s->qscale * matrix[j]) / 32; | |
| 983 /* XXX: is it really necessary to saturate since the encoder | |
| 984 knows whats going on ? */ | |
| 985 mismatch ^= level; | |
| 986 block[j] = level; | |
| 987 i++; | |
| 988 } | |
| 989 block[63] ^= (mismatch & 1); | |
| 990 s->block_last_index[n] = i; | |
| 991 return 0; | |
| 992 } | |
| 993 | |
| 994 static int mpeg2_decode_block_intra(MpegEncContext *s, | |
| 995 DCTELEM *block, | |
| 996 int n) | |
| 997 { | |
| 998 int level, dc, diff, i, j, run; | |
| 999 int code, component; | |
| 1000 RLTable *rl; | |
| 1001 const UINT8 *scan_table; | |
| 1002 const UINT16 *matrix; | |
| 1003 int mismatch; | |
| 1004 | |
| 1005 if (s->alternate_scan) | |
| 1006 scan_table = ff_alternate_vertical_scan; | |
| 1007 else | |
| 1008 scan_table = zigzag_direct; | |
| 1009 mismatch = 1; | |
| 1010 | |
| 1011 /* DC coef */ | |
| 1012 component = (n <= 3 ? 0 : n - 4 + 1); | |
| 1013 diff = decode_dc(s, component); | |
| 1014 if (diff >= 0xffff) | |
| 1015 return -1; | |
| 1016 dc = s->last_dc[component]; | |
| 1017 dc += diff; | |
| 1018 s->last_dc[component] = dc; | |
| 1019 block[0] = dc << (3 - s->intra_dc_precision); | |
| 1020 dprintf("dc=%d\n", block[0]); | |
| 1021 i = 1; | |
| 1022 if (s->intra_vlc_format) | |
| 1023 rl = &rl_mpeg2; | |
| 1024 else | |
| 1025 rl = &rl_mpeg1; | |
| 1026 if (n < 4) | |
| 1027 matrix = s->intra_matrix; | |
| 1028 else | |
| 1029 matrix = s->chroma_intra_matrix; | |
| 1030 | |
| 1031 /* now quantify & encode AC coefs */ | |
| 1032 for(;;) { | |
| 1033 code = get_vlc(&s->gb, &rl->vlc); | |
| 1034 if (code < 0) | |
| 1035 return -1; | |
| 1036 if (code == 112) { | |
| 1037 break; | |
| 1038 } else if (code == 111) { | |
| 1039 /* escape */ | |
| 1040 run = get_bits(&s->gb, 6); | |
| 1041 level = get_bits(&s->gb, 12); | |
| 1042 level = (level << 20) >> 20; | |
| 1043 } else { | |
| 1044 run = rl->table_run[code]; | |
| 1045 level = rl->table_level[code]; | |
| 21 | 1046 if (get_bits1(&s->gb)) |
| 0 | 1047 level = -level; |
| 1048 } | |
| 1049 i += run; | |
| 1050 if (i >= 64) | |
| 1051 return -1; | |
| 1052 j = scan_table[i]; | |
| 1053 dprintf("%d: run=%d level=%d\n", n, run, level); | |
| 1054 level = (level * s->qscale * matrix[j]) / 16; | |
| 1055 /* XXX: is it really necessary to saturate since the encoder | |
| 1056 knows whats going on ? */ | |
| 1057 mismatch ^= level; | |
| 1058 block[j] = level; | |
| 1059 i++; | |
| 1060 } | |
| 1061 block[63] ^= (mismatch & 1); | |
| 1062 s->block_last_index[n] = i; | |
| 1063 return 0; | |
| 1064 } | |
| 1065 | |
| 1066 /* compressed picture size */ | |
| 1067 #define PICTURE_BUFFER_SIZE 100000 | |
| 1068 | |
| 1069 typedef struct Mpeg1Context { | |
| 1070 MpegEncContext mpeg_enc_ctx; | |
| 1071 UINT32 header_state; | |
| 1072 int start_code; /* current start code */ | |
| 1073 UINT8 buffer[PICTURE_BUFFER_SIZE]; | |
| 1074 UINT8 *buf_ptr; | |
| 1075 int buffer_size; | |
| 1076 int mpeg_enc_ctx_allocated; /* true if decoding context allocated */ | |
| 1077 } Mpeg1Context; | |
| 1078 | |
| 1079 static int mpeg_decode_init(AVCodecContext *avctx) | |
| 1080 { | |
| 1081 Mpeg1Context *s = avctx->priv_data; | |
| 1082 | |
| 1083 s->header_state = 0xff; | |
| 1084 s->mpeg_enc_ctx_allocated = 0; | |
| 1085 s->buffer_size = PICTURE_BUFFER_SIZE; | |
| 1086 s->start_code = -1; | |
| 1087 s->buf_ptr = s->buffer; | |
| 1088 s->mpeg_enc_ctx.picture_number = 0; | |
| 1089 return 0; | |
| 1090 } | |
| 1091 | |
| 1092 /* return the 8 bit start code value and update the search | |
| 1093 state. Return -1 if no start code found */ | |
| 1094 static int find_start_code(UINT8 **pbuf_ptr, UINT8 *buf_end, | |
| 1095 UINT32 *header_state) | |
| 1096 { | |
| 1097 UINT8 *buf_ptr; | |
| 1098 unsigned int state, v; | |
| 1099 int val; | |
| 1100 | |
| 1101 state = *header_state; | |
| 1102 buf_ptr = *pbuf_ptr; | |
| 1103 while (buf_ptr < buf_end) { | |
| 1104 v = *buf_ptr++; | |
| 1105 if (state == 0x000001) { | |
| 1106 state = ((state << 8) | v) & 0xffffff; | |
| 1107 val = state; | |
| 1108 goto found; | |
| 1109 } | |
| 1110 state = ((state << 8) | v) & 0xffffff; | |
| 1111 } | |
| 1112 val = -1; | |
| 1113 found: | |
| 1114 *pbuf_ptr = buf_ptr; | |
| 1115 *header_state = state; | |
| 1116 return val; | |
| 1117 } | |
| 1118 | |
| 1119 static int mpeg1_decode_picture(AVCodecContext *avctx, | |
| 1120 UINT8 *buf, int buf_size) | |
| 1121 { | |
| 1122 Mpeg1Context *s1 = avctx->priv_data; | |
| 1123 MpegEncContext *s = &s1->mpeg_enc_ctx; | |
| 1124 int ref, f_code; | |
| 1125 | |
| 1126 init_get_bits(&s->gb, buf, buf_size); | |
| 1127 | |
| 1128 ref = get_bits(&s->gb, 10); /* temporal ref */ | |
| 1129 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
|
1130 dprintf("pict_type=%d number=%d\n", s->pict_type, s->picture_number); |
| 21 | 1131 skip_bits(&s->gb, 16); |
| 0 | 1132 if (s->pict_type == P_TYPE || s->pict_type == B_TYPE) { |
| 21 | 1133 s->full_pel[0] = get_bits1(&s->gb); |
| 0 | 1134 f_code = get_bits(&s->gb, 3); |
| 1135 if (f_code == 0) | |
| 1136 return -1; | |
| 1137 s->mpeg_f_code[0][0] = f_code; | |
| 1138 s->mpeg_f_code[0][1] = f_code; | |
| 1139 } | |
| 1140 if (s->pict_type == B_TYPE) { | |
| 21 | 1141 s->full_pel[1] = get_bits1(&s->gb); |
| 0 | 1142 f_code = get_bits(&s->gb, 3); |
| 1143 if (f_code == 0) | |
| 1144 return -1; | |
| 1145 s->mpeg_f_code[1][0] = f_code; | |
| 1146 s->mpeg_f_code[1][1] = f_code; | |
| 1147 } | |
| 1148 s->y_dc_scale = 8; | |
| 1149 s->c_dc_scale = 8; | |
| 1150 s->first_slice = 1; | |
| 1151 return 0; | |
| 1152 } | |
| 1153 | |
| 1154 static void mpeg_decode_sequence_extension(MpegEncContext *s) | |
| 1155 { | |
| 1156 int horiz_size_ext, vert_size_ext; | |
| 1157 int bit_rate_ext, vbv_buf_ext, low_delay; | |
| 1158 int frame_rate_ext_n, frame_rate_ext_d; | |
| 1159 | |
| 21 | 1160 skip_bits(&s->gb, 8); /* profil and level */ |
| 1161 skip_bits(&s->gb, 1); /* progressive_sequence */ | |
| 1162 skip_bits(&s->gb, 2); /* chroma_format */ | |
| 0 | 1163 horiz_size_ext = get_bits(&s->gb, 2); |
| 1164 vert_size_ext = get_bits(&s->gb, 2); | |
| 1165 s->width |= (horiz_size_ext << 12); | |
| 1166 s->height |= (vert_size_ext << 12); | |
| 1167 bit_rate_ext = get_bits(&s->gb, 12); /* XXX: handle it */ | |
| 1168 s->bit_rate = ((s->bit_rate / 400) | (bit_rate_ext << 12)) * 400; | |
| 21 | 1169 skip_bits1(&s->gb); /* marker */ |
| 0 | 1170 vbv_buf_ext = get_bits(&s->gb, 8); |
| 21 | 1171 low_delay = get_bits1(&s->gb); |
| 0 | 1172 frame_rate_ext_n = get_bits(&s->gb, 2); |
| 1173 frame_rate_ext_d = get_bits(&s->gb, 5); | |
| 1174 if (frame_rate_ext_d >= 1) | |
| 1175 s->frame_rate = (s->frame_rate * frame_rate_ext_n) / frame_rate_ext_d; | |
| 1176 dprintf("sequence extension\n"); | |
| 1177 s->mpeg2 = 1; | |
| 1178 } | |
| 1179 | |
| 1180 static void mpeg_decode_quant_matrix_extension(MpegEncContext *s) | |
| 1181 { | |
|
38
5bf15419d47e
changed quant matrix order (should fix mmx mpeg decoding bug)
glantau
parents:
21
diff
changeset
|
1182 int i, v, j; |
| 0 | 1183 |
| 21 | 1184 if (get_bits1(&s->gb)) { |
| 0 | 1185 for(i=0;i<64;i++) { |
| 1186 v = get_bits(&s->gb, 8); | |
|
38
5bf15419d47e
changed quant matrix order (should fix mmx mpeg decoding bug)
glantau
parents:
21
diff
changeset
|
1187 j = block_permute_op(i); |
|
5bf15419d47e
changed quant matrix order (should fix mmx mpeg decoding bug)
glantau
parents:
21
diff
changeset
|
1188 s->intra_matrix[j] = v; |
|
5bf15419d47e
changed quant matrix order (should fix mmx mpeg decoding bug)
glantau
parents:
21
diff
changeset
|
1189 s->chroma_intra_matrix[j] = v; |
| 0 | 1190 } |
| 1191 } | |
| 21 | 1192 if (get_bits1(&s->gb)) { |
| 0 | 1193 for(i=0;i<64;i++) { |
| 1194 v = get_bits(&s->gb, 8); | |
|
38
5bf15419d47e
changed quant matrix order (should fix mmx mpeg decoding bug)
glantau
parents:
21
diff
changeset
|
1195 j = block_permute_op(i); |
|
5bf15419d47e
changed quant matrix order (should fix mmx mpeg decoding bug)
glantau
parents:
21
diff
changeset
|
1196 s->non_intra_matrix[j] = v; |
|
5bf15419d47e
changed quant matrix order (should fix mmx mpeg decoding bug)
glantau
parents:
21
diff
changeset
|
1197 s->chroma_non_intra_matrix[j] = v; |
| 0 | 1198 } |
| 1199 } | |
| 21 | 1200 if (get_bits1(&s->gb)) { |
| 0 | 1201 for(i=0;i<64;i++) { |
| 1202 v = get_bits(&s->gb, 8); | |
|
38
5bf15419d47e
changed quant matrix order (should fix mmx mpeg decoding bug)
glantau
parents:
21
diff
changeset
|
1203 j = block_permute_op(i); |
|
5bf15419d47e
changed quant matrix order (should fix mmx mpeg decoding bug)
glantau
parents:
21
diff
changeset
|
1204 s->chroma_intra_matrix[j] = v; |
| 0 | 1205 } |
| 1206 } | |
| 21 | 1207 if (get_bits1(&s->gb)) { |
| 0 | 1208 for(i=0;i<64;i++) { |
| 1209 v = get_bits(&s->gb, 8); | |
|
38
5bf15419d47e
changed quant matrix order (should fix mmx mpeg decoding bug)
glantau
parents:
21
diff
changeset
|
1210 j = block_permute_op(i); |
|
5bf15419d47e
changed quant matrix order (should fix mmx mpeg decoding bug)
glantau
parents:
21
diff
changeset
|
1211 s->chroma_non_intra_matrix[j] = v; |
| 0 | 1212 } |
| 1213 } | |
| 1214 } | |
| 1215 | |
| 1216 static void mpeg_decode_picture_coding_extension(MpegEncContext *s) | |
| 1217 { | |
| 1218 s->full_pel[0] = s->full_pel[1] = 0; | |
| 1219 s->mpeg_f_code[0][0] = get_bits(&s->gb, 4); | |
| 1220 s->mpeg_f_code[0][1] = get_bits(&s->gb, 4); | |
| 1221 s->mpeg_f_code[1][0] = get_bits(&s->gb, 4); | |
| 1222 s->mpeg_f_code[1][1] = get_bits(&s->gb, 4); | |
| 1223 s->intra_dc_precision = get_bits(&s->gb, 2); | |
| 1224 s->picture_structure = get_bits(&s->gb, 2); | |
| 21 | 1225 s->top_field_first = get_bits1(&s->gb); |
| 1226 s->frame_pred_frame_dct = get_bits1(&s->gb); | |
| 1227 s->concealment_motion_vectors = get_bits1(&s->gb); | |
| 1228 s->q_scale_type = get_bits1(&s->gb); | |
| 1229 s->intra_vlc_format = get_bits1(&s->gb); | |
| 1230 s->alternate_scan = get_bits1(&s->gb); | |
| 1231 s->repeat_first_field = get_bits1(&s->gb); | |
| 1232 s->chroma_420_type = get_bits1(&s->gb); | |
| 1233 s->progressive_frame = get_bits1(&s->gb); | |
| 0 | 1234 /* composite display not parsed */ |
| 1235 dprintf("dc_preci=%d\n", s->intra_dc_precision); | |
| 1236 dprintf("pict_structure=%d\n", s->picture_structure); | |
| 1237 dprintf("conceal=%d\n", s->concealment_motion_vectors); | |
| 1238 dprintf("intrafmt=%d\n", s->intra_vlc_format); | |
| 1239 dprintf("frame_pred_frame_dct=%d\n", s->frame_pred_frame_dct); | |
| 1240 } | |
| 1241 | |
| 1242 static void mpeg_decode_extension(AVCodecContext *avctx, | |
| 1243 UINT8 *buf, int buf_size) | |
| 1244 { | |
| 1245 Mpeg1Context *s1 = avctx->priv_data; | |
| 1246 MpegEncContext *s = &s1->mpeg_enc_ctx; | |
| 1247 int ext_type; | |
| 1248 | |
| 1249 init_get_bits(&s->gb, buf, buf_size); | |
| 1250 | |
| 1251 ext_type = get_bits(&s->gb, 4); | |
| 1252 switch(ext_type) { | |
| 1253 case 0x1: | |
| 1254 /* sequence ext */ | |
| 1255 mpeg_decode_sequence_extension(s); | |
| 1256 break; | |
| 1257 case 0x3: | |
| 1258 /* quant matrix extension */ | |
| 1259 mpeg_decode_quant_matrix_extension(s); | |
| 1260 break; | |
| 1261 case 0x8: | |
| 1262 /* picture extension */ | |
| 1263 mpeg_decode_picture_coding_extension(s); | |
| 1264 break; | |
| 1265 } | |
| 1266 } | |
| 1267 | |
| 1268 /* return 1 if end of frame */ | |
| 1269 static int mpeg_decode_slice(AVCodecContext *avctx, | |
| 1270 AVPicture *pict, | |
| 1271 int start_code, | |
| 1272 UINT8 *buf, int buf_size) | |
| 1273 { | |
| 1274 Mpeg1Context *s1 = avctx->priv_data; | |
| 1275 MpegEncContext *s = &s1->mpeg_enc_ctx; | |
| 1276 int ret; | |
| 1277 | |
| 1278 start_code = (start_code - 1) & 0xff; | |
| 1279 if (start_code >= s->mb_height) | |
| 1280 return -1; | |
| 1281 s->last_dc[0] = 1 << (7 + s->intra_dc_precision); | |
| 1282 s->last_dc[1] = s->last_dc[0]; | |
| 1283 s->last_dc[2] = s->last_dc[0]; | |
| 1284 memset(s->last_mv, 0, sizeof(s->last_mv)); | |
| 1285 s->mb_x = -1; | |
| 1286 s->mb_y = start_code; | |
| 1287 s->mb_incr = 0; | |
| 1288 | |
| 1289 /* start frame decoding */ | |
| 1290 if (s->first_slice) { | |
| 1291 s->first_slice = 0; | |
| 1292 MPV_frame_start(s); | |
| 1293 } | |
| 1294 | |
| 1295 init_get_bits(&s->gb, buf, buf_size); | |
| 1296 | |
| 54 | 1297 s->qscale = get_qscale(s); |
| 0 | 1298 /* extra slice info */ |
| 21 | 1299 while (get_bits1(&s->gb) != 0) { |
| 1300 skip_bits(&s->gb, 8); | |
| 0 | 1301 } |
| 1302 | |
| 1303 for(;;) { | |
|
12
4d50c7d89e0f
use block[] in structure to have it aligned on 8 bytes for mmx optimizations
glantau
parents:
7
diff
changeset
|
1304 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
|
1305 ret = mpeg_decode_mb(s, s->block); |
| 0 | 1306 dprintf("ret=%d\n", ret); |
| 1307 if (ret < 0) | |
| 1308 return -1; | |
| 1309 if (ret == 1) | |
| 1310 break; | |
|
12
4d50c7d89e0f
use block[] in structure to have it aligned on 8 bytes for mmx optimizations
glantau
parents:
7
diff
changeset
|
1311 MPV_decode_mb(s, s->block); |
| 0 | 1312 } |
| 1313 | |
| 1314 /* end of slice reached */ | |
| 1315 if (s->mb_x == (s->mb_width - 1) && | |
| 1316 s->mb_y == (s->mb_height - 1)) { | |
| 1317 /* end of image */ | |
| 1318 UINT8 **picture; | |
| 1319 | |
| 1320 MPV_frame_end(s); | |
| 1321 | |
| 1322 /* XXX: incorrect reported qscale for mpeg2 */ | |
| 1323 if (s->pict_type == B_TYPE) { | |
| 1324 picture = s->current_picture; | |
| 1325 avctx->quality = s->qscale; | |
| 1326 } else { | |
| 1327 /* latency of 1 frame for I and P frames */ | |
| 1328 /* XXX: use another variable than picture_number */ | |
| 1329 if (s->picture_number == 0) { | |
| 1330 picture = NULL; | |
| 1331 } else { | |
| 1332 picture = s->last_picture; | |
| 1333 avctx->quality = s->last_qscale; | |
| 1334 } | |
| 1335 s->last_qscale = s->qscale; | |
| 1336 s->picture_number++; | |
| 1337 } | |
| 1338 if (picture) { | |
| 1339 pict->data[0] = picture[0]; | |
| 1340 pict->data[1] = picture[1]; | |
| 1341 pict->data[2] = picture[2]; | |
| 1342 pict->linesize[0] = s->linesize; | |
| 1343 pict->linesize[1] = s->linesize / 2; | |
| 1344 pict->linesize[2] = s->linesize / 2; | |
| 1345 return 1; | |
| 1346 } else { | |
| 1347 return 0; | |
| 1348 } | |
| 1349 } else { | |
| 1350 return 0; | |
| 1351 } | |
| 1352 } | |
| 1353 | |
| 1354 static int mpeg1_decode_sequence(AVCodecContext *avctx, | |
| 1355 UINT8 *buf, int buf_size) | |
| 1356 { | |
| 1357 Mpeg1Context *s1 = avctx->priv_data; | |
| 1358 MpegEncContext *s = &s1->mpeg_enc_ctx; | |
|
38
5bf15419d47e
changed quant matrix order (should fix mmx mpeg decoding bug)
glantau
parents:
21
diff
changeset
|
1359 int width, height, i, v, j; |
| 0 | 1360 |
| 1361 init_get_bits(&s->gb, buf, buf_size); | |
| 1362 | |
| 1363 width = get_bits(&s->gb, 12); | |
| 1364 height = get_bits(&s->gb, 12); | |
| 21 | 1365 skip_bits(&s->gb, 4); |
| 0 | 1366 s->frame_rate_index = get_bits(&s->gb, 4); |
| 1367 if (s->frame_rate_index == 0) | |
| 1368 return -1; | |
| 1369 s->bit_rate = get_bits(&s->gb, 18) * 400; | |
| 21 | 1370 if (get_bits1(&s->gb) == 0) /* marker */ |
| 0 | 1371 return -1; |
| 1372 if (width <= 0 || height <= 0 || | |
| 1373 (width % 2) != 0 || (height % 2) != 0) | |
| 1374 return -1; | |
| 1375 if (width != s->width || | |
| 1376 height != s->height) { | |
| 1377 /* start new mpeg1 context decoding */ | |
| 1378 s->out_format = FMT_MPEG1; | |
| 1379 if (s1->mpeg_enc_ctx_allocated) { | |
| 1380 MPV_common_end(s); | |
| 1381 } | |
| 1382 s->width = width; | |
| 1383 s->height = height; | |
| 1384 s->has_b_frames = 1; | |
| 1385 avctx->width = width; | |
| 1386 avctx->height = height; | |
| 1387 avctx->frame_rate = frame_rate_tab[s->frame_rate_index]; | |
| 1388 avctx->bit_rate = s->bit_rate; | |
| 1389 | |
| 1390 if (MPV_common_init(s) < 0) | |
| 1391 return -1; | |
| 1392 mpeg1_init_vlc(s); | |
| 1393 s1->mpeg_enc_ctx_allocated = 1; | |
| 1394 } | |
| 1395 | |
| 21 | 1396 skip_bits(&s->gb, 10); /* vbv_buffer_size */ |
| 1397 skip_bits(&s->gb, 1); | |
| 0 | 1398 |
| 1399 /* get matrix */ | |
| 21 | 1400 if (get_bits1(&s->gb)) { |
| 0 | 1401 for(i=0;i<64;i++) { |
| 1402 v = get_bits(&s->gb, 8); | |
|
38
5bf15419d47e
changed quant matrix order (should fix mmx mpeg decoding bug)
glantau
parents:
21
diff
changeset
|
1403 j = block_permute_op(i); |
|
5bf15419d47e
changed quant matrix order (should fix mmx mpeg decoding bug)
glantau
parents:
21
diff
changeset
|
1404 s->intra_matrix[j] = v; |
|
5bf15419d47e
changed quant matrix order (should fix mmx mpeg decoding bug)
glantau
parents:
21
diff
changeset
|
1405 s->chroma_intra_matrix[j] = v; |
| 0 | 1406 } |
| 1407 } else { | |
| 1408 for(i=0;i<64;i++) { | |
| 1409 v = default_intra_matrix[i]; | |
| 1410 s->intra_matrix[i] = v; | |
| 1411 s->chroma_intra_matrix[i] = v; | |
| 1412 } | |
| 1413 } | |
| 21 | 1414 if (get_bits1(&s->gb)) { |
| 0 | 1415 for(i=0;i<64;i++) { |
| 1416 v = get_bits(&s->gb, 8); | |
|
38
5bf15419d47e
changed quant matrix order (should fix mmx mpeg decoding bug)
glantau
parents:
21
diff
changeset
|
1417 j = block_permute_op(i); |
|
5bf15419d47e
changed quant matrix order (should fix mmx mpeg decoding bug)
glantau
parents:
21
diff
changeset
|
1418 s->non_intra_matrix[j] = v; |
|
5bf15419d47e
changed quant matrix order (should fix mmx mpeg decoding bug)
glantau
parents:
21
diff
changeset
|
1419 s->chroma_non_intra_matrix[j] = v; |
| 0 | 1420 } |
| 1421 } else { | |
| 1422 for(i=0;i<64;i++) { | |
| 1423 v = default_non_intra_matrix[i]; | |
| 1424 s->non_intra_matrix[i] = v; | |
| 1425 s->chroma_non_intra_matrix[i] = v; | |
| 1426 } | |
| 1427 } | |
| 1428 | |
| 1429 /* we set mpeg2 parameters so that it emulates mpeg1 */ | |
| 1430 s->progressive_sequence = 1; | |
| 1431 s->progressive_frame = 1; | |
| 1432 s->picture_structure = PICT_FRAME; | |
| 1433 s->frame_pred_frame_dct = 1; | |
| 1434 s->mpeg2 = 0; | |
| 1435 return 0; | |
| 1436 } | |
| 1437 | |
| 1438 /* handle buffering and image synchronisation */ | |
| 1439 static int mpeg_decode_frame(AVCodecContext *avctx, | |
| 1440 void *data, int *data_size, | |
| 1441 UINT8 *buf, int buf_size) | |
| 1442 { | |
| 1443 Mpeg1Context *s = avctx->priv_data; | |
| 1444 UINT8 *buf_end, *buf_ptr, *buf_start; | |
| 1445 int len, start_code_found, ret, code, start_code, input_size; | |
| 1446 AVPicture *picture = data; | |
| 1447 | |
| 1448 dprintf("fill_buffer\n"); | |
| 1449 | |
| 1450 *data_size = 0; | |
| 1451 /* special case for last picture */ | |
| 1452 if (buf_size == 0) { | |
| 1453 MpegEncContext *s2 = &s->mpeg_enc_ctx; | |
| 1454 if (s2->picture_number > 0) { | |
| 1455 picture->data[0] = s2->next_picture[0]; | |
| 1456 picture->data[1] = s2->next_picture[1]; | |
| 1457 picture->data[2] = s2->next_picture[2]; | |
| 1458 picture->linesize[0] = s2->linesize; | |
| 1459 picture->linesize[1] = s2->linesize / 2; | |
| 1460 picture->linesize[2] = s2->linesize / 2; | |
| 1461 *data_size = sizeof(AVPicture); | |
| 1462 } | |
| 1463 return 0; | |
| 1464 } | |
| 1465 | |
| 1466 buf_ptr = buf; | |
| 1467 buf_end = buf + buf_size; | |
| 1468 while (buf_ptr < buf_end) { | |
| 1469 buf_start = buf_ptr; | |
| 1470 /* find start next code */ | |
| 1471 code = find_start_code(&buf_ptr, buf_end, &s->header_state); | |
| 1472 if (code >= 0) { | |
| 1473 start_code_found = 1; | |
| 1474 } else { | |
| 1475 start_code_found = 0; | |
| 1476 } | |
| 1477 /* copy to buffer */ | |
| 1478 len = buf_ptr - buf_start; | |
| 1479 if (len + (s->buf_ptr - s->buffer) > s->buffer_size) { | |
| 1480 /* data too big : flush */ | |
| 1481 s->buf_ptr = s->buffer; | |
| 1482 if (start_code_found) | |
| 1483 s->start_code = code; | |
| 1484 } else { | |
| 1485 memcpy(s->buf_ptr, buf_start, len); | |
| 1486 s->buf_ptr += len; | |
| 1487 | |
| 1488 if (start_code_found) { | |
| 1489 /* prepare data for next start code */ | |
| 1490 input_size = s->buf_ptr - s->buffer; | |
| 1491 start_code = s->start_code; | |
| 1492 s->buf_ptr = s->buffer; | |
| 1493 s->start_code = code; | |
| 1494 switch(start_code) { | |
| 1495 case SEQ_START_CODE: | |
| 1496 mpeg1_decode_sequence(avctx, s->buffer, | |
| 1497 input_size); | |
| 1498 break; | |
| 1499 | |
| 1500 case PICTURE_START_CODE: | |
| 1501 /* we have a complete image : we try to decompress it */ | |
| 1502 mpeg1_decode_picture(avctx, | |
| 1503 s->buffer, input_size); | |
| 1504 break; | |
| 1505 case EXT_START_CODE: | |
| 1506 mpeg_decode_extension(avctx, | |
| 1507 s->buffer, input_size); | |
| 1508 break; | |
| 1509 default: | |
| 1510 if (start_code >= SLICE_MIN_START_CODE && | |
| 1511 start_code <= SLICE_MAX_START_CODE) { | |
| 1512 ret = mpeg_decode_slice(avctx, picture, | |
| 1513 start_code, s->buffer, input_size); | |
| 1514 if (ret == 1) { | |
| 1515 /* got a picture: exit */ | |
| 1516 *data_size = sizeof(AVPicture); | |
| 1517 goto the_end; | |
| 1518 } | |
| 1519 } | |
| 1520 break; | |
| 1521 } | |
| 1522 } | |
| 1523 } | |
| 1524 } | |
| 1525 the_end: | |
| 1526 return buf_ptr - buf; | |
| 1527 } | |
| 1528 | |
| 1529 static int mpeg_decode_end(AVCodecContext *avctx) | |
| 1530 { | |
| 1531 Mpeg1Context *s = avctx->priv_data; | |
| 1532 | |
| 1533 if (s->mpeg_enc_ctx_allocated) | |
| 1534 MPV_common_end(&s->mpeg_enc_ctx); | |
| 1535 return 0; | |
| 1536 } | |
| 1537 | |
| 1538 AVCodec mpeg_decoder = { | |
| 1539 "mpegvideo", | |
| 1540 CODEC_TYPE_VIDEO, | |
| 1541 CODEC_ID_MPEG1VIDEO, | |
| 1542 sizeof(Mpeg1Context), | |
| 1543 mpeg_decode_init, | |
| 1544 NULL, | |
| 1545 mpeg_decode_end, | |
| 1546 mpeg_decode_frame, | |
| 1547 }; |
