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