00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00028
00029 #include "internal.h"
00030 #include "avcodec.h"
00031 #include "dsputil.h"
00032 #include "mpegvideo.h"
00033
00034 #include "mpeg12.h"
00035 #include "mpeg12data.h"
00036 #include "mpeg12decdata.h"
00037 #include "bytestream.h"
00038 #include "vdpau_internal.h"
00039 #include "xvmc_internal.h"
00040 #include "thread.h"
00041
00042
00043
00044
00045
00046 #define MV_VLC_BITS 9
00047 #define MBINCR_VLC_BITS 9
00048 #define MB_PAT_VLC_BITS 9
00049 #define MB_PTYPE_VLC_BITS 6
00050 #define MB_BTYPE_VLC_BITS 6
00051
00052 static VLC mv_vlc;
00053
00054
00055 static int mpeg_decode_motion(MpegEncContext *s, int fcode, int pred)
00056 {
00057 int code, sign, val, shift;
00058
00059 code = get_vlc2(&s->gb, mv_vlc.table, MV_VLC_BITS, 2);
00060 if (code == 0) {
00061 return pred;
00062 }
00063 if (code < 0) {
00064 return 0xffff;
00065 }
00066
00067 sign = get_bits1(&s->gb);
00068 shift = fcode - 1;
00069 val = code;
00070 if (shift) {
00071 val = (val - 1) << shift;
00072 val |= get_bits(&s->gb, shift);
00073 val++;
00074 }
00075 if (sign)
00076 val = -val;
00077 val += pred;
00078
00079
00080 return sign_extend(val, 5 + shift);
00081 }
00082
00083 static inline int mpeg1_decode_block_intra(MpegEncContext *s, DCTELEM *block, int n)
00084 {
00085 int level, dc, diff, i, j, run;
00086 int component;
00087 RLTable *rl = &ff_rl_mpeg1;
00088 uint8_t * const scantable = s->intra_scantable.permutated;
00089 const uint16_t *quant_matrix = s->intra_matrix;
00090 const int qscale = s->qscale;
00091
00092
00093 component = (n <= 3 ? 0 : n - 4 + 1);
00094 diff = decode_dc(&s->gb, component);
00095 if (diff >= 0xffff)
00096 return -1;
00097 dc = s->last_dc[component];
00098 dc += diff;
00099 s->last_dc[component] = dc;
00100 block[0] = dc * quant_matrix[0];
00101 av_dlog(s->avctx, "dc=%d diff=%d\n", dc, diff);
00102 i = 0;
00103 {
00104 OPEN_READER(re, &s->gb);
00105
00106 for (;;) {
00107 UPDATE_CACHE(re, &s->gb);
00108 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
00109
00110 if (level == 127) {
00111 break;
00112 } else if (level != 0) {
00113 i += run;
00114 j = scantable[i];
00115 level = (level * qscale * quant_matrix[j]) >> 4;
00116 level = (level - 1) | 1;
00117 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
00118 LAST_SKIP_BITS(re, &s->gb, 1);
00119 } else {
00120
00121 run = SHOW_UBITS(re, &s->gb, 6) + 1; LAST_SKIP_BITS(re, &s->gb, 6);
00122 UPDATE_CACHE(re, &s->gb);
00123 level = SHOW_SBITS(re, &s->gb, 8); SKIP_BITS(re, &s->gb, 8);
00124 if (level == -128) {
00125 level = SHOW_UBITS(re, &s->gb, 8) - 256; LAST_SKIP_BITS(re, &s->gb, 8);
00126 } else if (level == 0) {
00127 level = SHOW_UBITS(re, &s->gb, 8) ; LAST_SKIP_BITS(re, &s->gb, 8);
00128 }
00129 i += run;
00130 j = scantable[i];
00131 if (level < 0) {
00132 level = -level;
00133 level = (level * qscale * quant_matrix[j]) >> 4;
00134 level = (level - 1) | 1;
00135 level = -level;
00136 } else {
00137 level = (level * qscale * quant_matrix[j]) >> 4;
00138 level = (level - 1) | 1;
00139 }
00140 }
00141 if (i > 63) {
00142 av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
00143 return -1;
00144 }
00145
00146 block[j] = level;
00147 }
00148 CLOSE_READER(re, &s->gb);
00149 }
00150 s->block_last_index[n] = i;
00151 return 0;
00152 }
00153
00154 int ff_mpeg1_decode_block_intra(MpegEncContext *s, DCTELEM *block, int n)
00155 {
00156 return mpeg1_decode_block_intra(s, block, n);
00157 }
00158
00159 static inline int mpeg1_decode_block_inter(MpegEncContext *s, DCTELEM *block, int n)
00160 {
00161 int level, i, j, run;
00162 RLTable *rl = &ff_rl_mpeg1;
00163 uint8_t * const scantable = s->intra_scantable.permutated;
00164 const uint16_t *quant_matrix = s->inter_matrix;
00165 const int qscale = s->qscale;
00166
00167 {
00168 OPEN_READER(re, &s->gb);
00169 i = -1;
00170
00171 UPDATE_CACHE(re, &s->gb);
00172 if (((int32_t)GET_CACHE(re, &s->gb)) < 0) {
00173 level = (3 * qscale * quant_matrix[0]) >> 5;
00174 level = (level - 1) | 1;
00175 if (GET_CACHE(re, &s->gb) & 0x40000000)
00176 level = -level;
00177 block[0] = level;
00178 i++;
00179 SKIP_BITS(re, &s->gb, 2);
00180 if (((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
00181 goto end;
00182 }
00183
00184 for (;;) {
00185 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
00186
00187 if (level != 0) {
00188 i += run;
00189 j = scantable[i];
00190 level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
00191 level = (level - 1) | 1;
00192 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
00193 SKIP_BITS(re, &s->gb, 1);
00194 } else {
00195
00196 run = SHOW_UBITS(re, &s->gb, 6) + 1; LAST_SKIP_BITS(re, &s->gb, 6);
00197 UPDATE_CACHE(re, &s->gb);
00198 level = SHOW_SBITS(re, &s->gb, 8); SKIP_BITS(re, &s->gb, 8);
00199 if (level == -128) {
00200 level = SHOW_UBITS(re, &s->gb, 8) - 256; SKIP_BITS(re, &s->gb, 8);
00201 } else if (level == 0) {
00202 level = SHOW_UBITS(re, &s->gb, 8) ; SKIP_BITS(re, &s->gb, 8);
00203 }
00204 i += run;
00205 j = scantable[i];
00206 if (level < 0) {
00207 level = -level;
00208 level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
00209 level = (level - 1) | 1;
00210 level = -level;
00211 } else {
00212 level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
00213 level = (level - 1) | 1;
00214 }
00215 }
00216 if (i > 63) {
00217 av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
00218 return -1;
00219 }
00220
00221 block[j] = level;
00222 if (((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
00223 break;
00224 UPDATE_CACHE(re, &s->gb);
00225 }
00226 end:
00227 LAST_SKIP_BITS(re, &s->gb, 2);
00228 CLOSE_READER(re, &s->gb);
00229 }
00230 s->block_last_index[n] = i;
00231 return 0;
00232 }
00233
00234 static inline int mpeg1_fast_decode_block_inter(MpegEncContext *s, DCTELEM *block, int n)
00235 {
00236 int level, i, j, run;
00237 RLTable *rl = &ff_rl_mpeg1;
00238 uint8_t * const scantable = s->intra_scantable.permutated;
00239 const int qscale = s->qscale;
00240
00241 {
00242 OPEN_READER(re, &s->gb);
00243 i = -1;
00244
00245 UPDATE_CACHE(re, &s->gb);
00246 if (((int32_t)GET_CACHE(re, &s->gb)) < 0) {
00247 level = (3 * qscale) >> 1;
00248 level = (level - 1) | 1;
00249 if (GET_CACHE(re, &s->gb) & 0x40000000)
00250 level = -level;
00251 block[0] = level;
00252 i++;
00253 SKIP_BITS(re, &s->gb, 2);
00254 if (((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
00255 goto end;
00256 }
00257
00258
00259 for (;;) {
00260 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
00261
00262 if (level != 0) {
00263 i += run;
00264 j = scantable[i];
00265 level = ((level * 2 + 1) * qscale) >> 1;
00266 level = (level - 1) | 1;
00267 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
00268 SKIP_BITS(re, &s->gb, 1);
00269 } else {
00270
00271 run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
00272 UPDATE_CACHE(re, &s->gb);
00273 level = SHOW_SBITS(re, &s->gb, 8); SKIP_BITS(re, &s->gb, 8);
00274 if (level == -128) {
00275 level = SHOW_UBITS(re, &s->gb, 8) - 256; SKIP_BITS(re, &s->gb, 8);
00276 } else if (level == 0) {
00277 level = SHOW_UBITS(re, &s->gb, 8) ; SKIP_BITS(re, &s->gb, 8);
00278 }
00279 i += run;
00280 j = scantable[i];
00281 if (level < 0) {
00282 level = -level;
00283 level = ((level * 2 + 1) * qscale) >> 1;
00284 level = (level - 1) | 1;
00285 level = -level;
00286 } else {
00287 level = ((level * 2 + 1) * qscale) >> 1;
00288 level = (level - 1) | 1;
00289 }
00290 }
00291
00292 block[j] = level;
00293 if (((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
00294 break;
00295 UPDATE_CACHE(re, &s->gb);
00296 }
00297 end:
00298 LAST_SKIP_BITS(re, &s->gb, 2);
00299 CLOSE_READER(re, &s->gb);
00300 }
00301 s->block_last_index[n] = i;
00302 return 0;
00303 }
00304
00305
00306 static inline int mpeg2_decode_block_non_intra(MpegEncContext *s, DCTELEM *block, int n)
00307 {
00308 int level, i, j, run;
00309 RLTable *rl = &ff_rl_mpeg1;
00310 uint8_t * const scantable = s->intra_scantable.permutated;
00311 const uint16_t *quant_matrix;
00312 const int qscale = s->qscale;
00313 int mismatch;
00314
00315 mismatch = 1;
00316
00317 {
00318 OPEN_READER(re, &s->gb);
00319 i = -1;
00320 if (n < 4)
00321 quant_matrix = s->inter_matrix;
00322 else
00323 quant_matrix = s->chroma_inter_matrix;
00324
00325
00326 UPDATE_CACHE(re, &s->gb);
00327 if (((int32_t)GET_CACHE(re, &s->gb)) < 0) {
00328 level= (3 * qscale * quant_matrix[0]) >> 5;
00329 if (GET_CACHE(re, &s->gb) & 0x40000000)
00330 level = -level;
00331 block[0] = level;
00332 mismatch ^= level;
00333 i++;
00334 SKIP_BITS(re, &s->gb, 2);
00335 if (((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
00336 goto end;
00337 }
00338
00339
00340 for (;;) {
00341 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
00342
00343 if (level != 0) {
00344 i += run;
00345 j = scantable[i];
00346 level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
00347 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
00348 SKIP_BITS(re, &s->gb, 1);
00349 } else {
00350
00351 run = SHOW_UBITS(re, &s->gb, 6) + 1; LAST_SKIP_BITS(re, &s->gb, 6);
00352 UPDATE_CACHE(re, &s->gb);
00353 level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
00354
00355 i += run;
00356 j = scantable[i];
00357 if (level < 0) {
00358 level = ((-level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
00359 level = -level;
00360 } else {
00361 level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
00362 }
00363 }
00364 if (i > 63) {
00365 av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
00366 return -1;
00367 }
00368
00369 mismatch ^= level;
00370 block[j] = level;
00371 if (((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
00372 break;
00373 UPDATE_CACHE(re, &s->gb);
00374 }
00375 end:
00376 LAST_SKIP_BITS(re, &s->gb, 2);
00377 CLOSE_READER(re, &s->gb);
00378 }
00379 block[63] ^= (mismatch & 1);
00380
00381 s->block_last_index[n] = i;
00382 return 0;
00383 }
00384
00385 static inline int mpeg2_fast_decode_block_non_intra(MpegEncContext *s,
00386 DCTELEM *block, int n)
00387 {
00388 int level, i, j, run;
00389 RLTable *rl = &ff_rl_mpeg1;
00390 uint8_t * const scantable = s->intra_scantable.permutated;
00391 const int qscale = s->qscale;
00392 OPEN_READER(re, &s->gb);
00393 i = -1;
00394
00395
00396 UPDATE_CACHE(re, &s->gb);
00397 if (((int32_t)GET_CACHE(re, &s->gb)) < 0) {
00398 level = (3 * qscale) >> 1;
00399 if (GET_CACHE(re, &s->gb) & 0x40000000)
00400 level = -level;
00401 block[0] = level;
00402 i++;
00403 SKIP_BITS(re, &s->gb, 2);
00404 if (((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
00405 goto end;
00406 }
00407
00408
00409 for (;;) {
00410 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
00411
00412 if (level != 0) {
00413 i += run;
00414 j = scantable[i];
00415 level = ((level * 2 + 1) * qscale) >> 1;
00416 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
00417 SKIP_BITS(re, &s->gb, 1);
00418 } else {
00419
00420 run = SHOW_UBITS(re, &s->gb, 6) + 1; LAST_SKIP_BITS(re, &s->gb, 6);
00421 UPDATE_CACHE(re, &s->gb);
00422 level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
00423
00424 i += run;
00425 j = scantable[i];
00426 if (level < 0) {
00427 level = ((-level * 2 + 1) * qscale) >> 1;
00428 level = -level;
00429 } else {
00430 level = ((level * 2 + 1) * qscale) >> 1;
00431 }
00432 }
00433
00434 block[j] = level;
00435 if (((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
00436 break;
00437 UPDATE_CACHE(re, &s->gb);
00438 }
00439 end:
00440 LAST_SKIP_BITS(re, &s->gb, 2);
00441 CLOSE_READER(re, &s->gb);
00442 s->block_last_index[n] = i;
00443 return 0;
00444 }
00445
00446
00447 static inline int mpeg2_decode_block_intra(MpegEncContext *s, DCTELEM *block, int n)
00448 {
00449 int level, dc, diff, i, j, run;
00450 int component;
00451 RLTable *rl;
00452 uint8_t * const scantable = s->intra_scantable.permutated;
00453 const uint16_t *quant_matrix;
00454 const int qscale = s->qscale;
00455 int mismatch;
00456
00457
00458 if (n < 4) {
00459 quant_matrix = s->intra_matrix;
00460 component = 0;
00461 } else {
00462 quant_matrix = s->chroma_intra_matrix;
00463 component = (n & 1) + 1;
00464 }
00465 diff = decode_dc(&s->gb, component);
00466 if (diff >= 0xffff)
00467 return -1;
00468 dc = s->last_dc[component];
00469 dc += diff;
00470 s->last_dc[component] = dc;
00471 block[0] = dc << (3 - s->intra_dc_precision);
00472 av_dlog(s->avctx, "dc=%d\n", block[0]);
00473 mismatch = block[0] ^ 1;
00474 i = 0;
00475 if (s->intra_vlc_format)
00476 rl = &ff_rl_mpeg2;
00477 else
00478 rl = &ff_rl_mpeg1;
00479
00480 {
00481 OPEN_READER(re, &s->gb);
00482
00483 for (;;) {
00484 UPDATE_CACHE(re, &s->gb);
00485 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
00486
00487 if (level == 127) {
00488 break;
00489 } else if (level != 0) {
00490 i += run;
00491 j = scantable[i];
00492 level = (level * qscale * quant_matrix[j]) >> 4;
00493 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
00494 LAST_SKIP_BITS(re, &s->gb, 1);
00495 } else {
00496
00497 run = SHOW_UBITS(re, &s->gb, 6) + 1; LAST_SKIP_BITS(re, &s->gb, 6);
00498 UPDATE_CACHE(re, &s->gb);
00499 level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
00500 i += run;
00501 j = scantable[i];
00502 if (level < 0) {
00503 level = (-level * qscale * quant_matrix[j]) >> 4;
00504 level = -level;
00505 } else {
00506 level = (level * qscale * quant_matrix[j]) >> 4;
00507 }
00508 }
00509 if (i > 63) {
00510 av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
00511 return -1;
00512 }
00513
00514 mismatch ^= level;
00515 block[j] = level;
00516 }
00517 CLOSE_READER(re, &s->gb);
00518 }
00519 block[63] ^= mismatch & 1;
00520
00521 s->block_last_index[n] = i;
00522 return 0;
00523 }
00524
00525 static inline int mpeg2_fast_decode_block_intra(MpegEncContext *s, DCTELEM *block, int n)
00526 {
00527 int level, dc, diff, j, run;
00528 int component;
00529 RLTable *rl;
00530 uint8_t * scantable = s->intra_scantable.permutated;
00531 const uint16_t *quant_matrix;
00532 const int qscale = s->qscale;
00533
00534
00535 if (n < 4) {
00536 quant_matrix = s->intra_matrix;
00537 component = 0;
00538 } else {
00539 quant_matrix = s->chroma_intra_matrix;
00540 component = (n & 1) + 1;
00541 }
00542 diff = decode_dc(&s->gb, component);
00543 if (diff >= 0xffff)
00544 return -1;
00545 dc = s->last_dc[component];
00546 dc += diff;
00547 s->last_dc[component] = dc;
00548 block[0] = dc << (3 - s->intra_dc_precision);
00549 if (s->intra_vlc_format)
00550 rl = &ff_rl_mpeg2;
00551 else
00552 rl = &ff_rl_mpeg1;
00553
00554 {
00555 OPEN_READER(re, &s->gb);
00556
00557 for (;;) {
00558 UPDATE_CACHE(re, &s->gb);
00559 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
00560
00561 if (level == 127) {
00562 break;
00563 } else if (level != 0) {
00564 scantable += run;
00565 j = *scantable;
00566 level = (level * qscale * quant_matrix[j]) >> 4;
00567 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
00568 LAST_SKIP_BITS(re, &s->gb, 1);
00569 } else {
00570
00571 run = SHOW_UBITS(re, &s->gb, 6) + 1; LAST_SKIP_BITS(re, &s->gb, 6);
00572 UPDATE_CACHE(re, &s->gb);
00573 level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
00574 scantable += run;
00575 j = *scantable;
00576 if (level < 0) {
00577 level = (-level * qscale * quant_matrix[j]) >> 4;
00578 level = -level;
00579 } else {
00580 level = (level * qscale * quant_matrix[j]) >> 4;
00581 }
00582 }
00583
00584 block[j] = level;
00585 }
00586 CLOSE_READER(re, &s->gb);
00587 }
00588
00589 s->block_last_index[n] = scantable - s->intra_scantable.permutated;
00590 return 0;
00591 }
00592
00593 uint8_t ff_mpeg12_static_rl_table_store[2][2][2*MAX_RUN + MAX_LEVEL + 3];
00594
00595 #define INIT_2D_VLC_RL(rl, static_size)\
00596 {\
00597 static RL_VLC_ELEM rl_vlc_table[static_size];\
00598 INIT_VLC_STATIC(&rl.vlc, TEX_VLC_BITS, rl.n + 2,\
00599 &rl.table_vlc[0][1], 4, 2,\
00600 &rl.table_vlc[0][0], 4, 2, static_size);\
00601 \
00602 rl.rl_vlc[0] = rl_vlc_table;\
00603 init_2d_vlc_rl(&rl);\
00604 }
00605
00606 static void init_2d_vlc_rl(RLTable *rl)
00607 {
00608 int i;
00609
00610 for (i = 0; i < rl->vlc.table_size; i++) {
00611 int code = rl->vlc.table[i][0];
00612 int len = rl->vlc.table[i][1];
00613 int level, run;
00614
00615 if (len == 0) {
00616 run = 65;
00617 level = MAX_LEVEL;
00618 } else if (len<0) {
00619 run = 0;
00620 level = code;
00621 } else {
00622 if (code == rl->n) {
00623 run = 65;
00624 level = 0;
00625 } else if (code == rl->n+1) {
00626 run = 0;
00627 level = 127;
00628 } else {
00629 run = rl->table_run [code] + 1;
00630 level = rl->table_level[code];
00631 }
00632 }
00633 rl->rl_vlc[0][i].len = len;
00634 rl->rl_vlc[0][i].level = level;
00635 rl->rl_vlc[0][i].run = run;
00636 }
00637 }
00638
00639 void ff_mpeg12_common_init(MpegEncContext *s)
00640 {
00641
00642 s->y_dc_scale_table =
00643 s->c_dc_scale_table = ff_mpeg2_dc_scale_table[s->intra_dc_precision];
00644
00645 }
00646
00647 void ff_mpeg1_clean_buffers(MpegEncContext *s)
00648 {
00649 s->last_dc[0] = 1 << (7 + s->intra_dc_precision);
00650 s->last_dc[1] = s->last_dc[0];
00651 s->last_dc[2] = s->last_dc[0];
00652 memset(s->last_mv, 0, sizeof(s->last_mv));
00653 }
00654
00655
00656
00657
00658
00659 VLC ff_dc_lum_vlc;
00660 VLC ff_dc_chroma_vlc;
00661
00662 static VLC mbincr_vlc;
00663 static VLC mb_ptype_vlc;
00664 static VLC mb_btype_vlc;
00665 static VLC mb_pat_vlc;
00666
00667 av_cold void ff_mpeg12_init_vlcs(void)
00668 {
00669 static int done = 0;
00670
00671 if (!done) {
00672 done = 1;
00673
00674 INIT_VLC_STATIC(&ff_dc_lum_vlc, DC_VLC_BITS, 12,
00675 ff_mpeg12_vlc_dc_lum_bits, 1, 1,
00676 ff_mpeg12_vlc_dc_lum_code, 2, 2, 512);
00677 INIT_VLC_STATIC(&ff_dc_chroma_vlc, DC_VLC_BITS, 12,
00678 ff_mpeg12_vlc_dc_chroma_bits, 1, 1,
00679 ff_mpeg12_vlc_dc_chroma_code, 2, 2, 514);
00680 INIT_VLC_STATIC(&mv_vlc, MV_VLC_BITS, 17,
00681 &ff_mpeg12_mbMotionVectorTable[0][1], 2, 1,
00682 &ff_mpeg12_mbMotionVectorTable[0][0], 2, 1, 518);
00683 INIT_VLC_STATIC(&mbincr_vlc, MBINCR_VLC_BITS, 36,
00684 &ff_mpeg12_mbAddrIncrTable[0][1], 2, 1,
00685 &ff_mpeg12_mbAddrIncrTable[0][0], 2, 1, 538);
00686 INIT_VLC_STATIC(&mb_pat_vlc, MB_PAT_VLC_BITS, 64,
00687 &ff_mpeg12_mbPatTable[0][1], 2, 1,
00688 &ff_mpeg12_mbPatTable[0][0], 2, 1, 512);
00689
00690 INIT_VLC_STATIC(&mb_ptype_vlc, MB_PTYPE_VLC_BITS, 7,
00691 &table_mb_ptype[0][1], 2, 1,
00692 &table_mb_ptype[0][0], 2, 1, 64);
00693 INIT_VLC_STATIC(&mb_btype_vlc, MB_BTYPE_VLC_BITS, 11,
00694 &table_mb_btype[0][1], 2, 1,
00695 &table_mb_btype[0][0], 2, 1, 64);
00696 init_rl(&ff_rl_mpeg1, ff_mpeg12_static_rl_table_store[0]);
00697 init_rl(&ff_rl_mpeg2, ff_mpeg12_static_rl_table_store[1]);
00698
00699 INIT_2D_VLC_RL(ff_rl_mpeg1, 680);
00700 INIT_2D_VLC_RL(ff_rl_mpeg2, 674);
00701 }
00702 }
00703
00704 static inline int get_dmv(MpegEncContext *s)
00705 {
00706 if (get_bits1(&s->gb))
00707 return 1 - (get_bits1(&s->gb) << 1);
00708 else
00709 return 0;
00710 }
00711
00712 static inline int get_qscale(MpegEncContext *s)
00713 {
00714 int qscale = get_bits(&s->gb, 5);
00715 if (s->q_scale_type) {
00716 return non_linear_qscale[qscale];
00717 } else {
00718 return qscale << 1;
00719 }
00720 }
00721
00722 static void exchange_uv(MpegEncContext *s)
00723 {
00724 DCTELEM (*tmp)[64];
00725
00726 tmp = s->pblocks[4];
00727 s->pblocks[4] = s->pblocks[5];
00728 s->pblocks[5] = tmp;
00729 }
00730
00731
00732 #define MT_FIELD 1
00733 #define MT_FRAME 2
00734 #define MT_16X8 2
00735 #define MT_DMV 3
00736
00737 static int mpeg_decode_mb(MpegEncContext *s, DCTELEM block[12][64])
00738 {
00739 int i, j, k, cbp, val, mb_type, motion_type;
00740 const int mb_block_count = 4 + (1 << s->chroma_format);
00741
00742 av_dlog(s->avctx, "decode_mb: x=%d y=%d\n", s->mb_x, s->mb_y);
00743
00744 assert(s->mb_skipped == 0);
00745
00746 if (s->mb_skip_run-- != 0) {
00747 if (s->pict_type == AV_PICTURE_TYPE_P) {
00748 s->mb_skipped = 1;
00749 s->current_picture.f.mb_type[s->mb_x + s->mb_y * s->mb_stride] = MB_TYPE_SKIP | MB_TYPE_L0 | MB_TYPE_16x16;
00750 } else {
00751 int mb_type;
00752
00753 if (s->mb_x)
00754 mb_type = s->current_picture.f.mb_type[s->mb_x + s->mb_y * s->mb_stride - 1];
00755 else
00756 mb_type = s->current_picture.f.mb_type[s->mb_width + (s->mb_y - 1) * s->mb_stride - 1];
00757 if (IS_INTRA(mb_type))
00758 return -1;
00759 s->current_picture.f.mb_type[s->mb_x + s->mb_y*s->mb_stride] =
00760 mb_type | MB_TYPE_SKIP;
00761
00762
00763 if ((s->mv[0][0][0] | s->mv[0][0][1] | s->mv[1][0][0] | s->mv[1][0][1]) == 0)
00764 s->mb_skipped = 1;
00765 }
00766
00767 return 0;
00768 }
00769
00770 switch (s->pict_type) {
00771 default:
00772 case AV_PICTURE_TYPE_I:
00773 if (get_bits1(&s->gb) == 0) {
00774 if (get_bits1(&s->gb) == 0) {
00775 av_log(s->avctx, AV_LOG_ERROR, "invalid mb type in I Frame at %d %d\n", s->mb_x, s->mb_y);
00776 return -1;
00777 }
00778 mb_type = MB_TYPE_QUANT | MB_TYPE_INTRA;
00779 } else {
00780 mb_type = MB_TYPE_INTRA;
00781 }
00782 break;
00783 case AV_PICTURE_TYPE_P:
00784 mb_type = get_vlc2(&s->gb, mb_ptype_vlc.table, MB_PTYPE_VLC_BITS, 1);
00785 if (mb_type < 0) {
00786 av_log(s->avctx, AV_LOG_ERROR, "invalid mb type in P Frame at %d %d\n", s->mb_x, s->mb_y);
00787 return -1;
00788 }
00789 mb_type = ptype2mb_type[mb_type];
00790 break;
00791 case AV_PICTURE_TYPE_B:
00792 mb_type = get_vlc2(&s->gb, mb_btype_vlc.table, MB_BTYPE_VLC_BITS, 1);
00793 if (mb_type < 0) {
00794 av_log(s->avctx, AV_LOG_ERROR, "invalid mb type in B Frame at %d %d\n", s->mb_x, s->mb_y);
00795 return -1;
00796 }
00797 mb_type = btype2mb_type[mb_type];
00798 break;
00799 }
00800 av_dlog(s->avctx, "mb_type=%x\n", mb_type);
00801
00802 if (IS_INTRA(mb_type)) {
00803 s->dsp.clear_blocks(s->block[0]);
00804
00805 if (!s->chroma_y_shift) {
00806 s->dsp.clear_blocks(s->block[6]);
00807 }
00808
00809
00810 if (s->picture_structure == PICT_FRAME &&
00811 !s->frame_pred_frame_dct) {
00812 s->interlaced_dct = get_bits1(&s->gb);
00813 }
00814
00815 if (IS_QUANT(mb_type))
00816 s->qscale = get_qscale(s);
00817
00818 if (s->concealment_motion_vectors) {
00819
00820 if (s->picture_structure != PICT_FRAME)
00821 skip_bits1(&s->gb);
00822
00823 s->mv[0][0][0]= s->last_mv[0][0][0]= s->last_mv[0][1][0] =
00824 mpeg_decode_motion(s, s->mpeg_f_code[0][0], s->last_mv[0][0][0]);
00825 s->mv[0][0][1]= s->last_mv[0][0][1]= s->last_mv[0][1][1] =
00826 mpeg_decode_motion(s, s->mpeg_f_code[0][1], s->last_mv[0][0][1]);
00827
00828 skip_bits1(&s->gb);
00829 } else
00830 memset(s->last_mv, 0, sizeof(s->last_mv));
00831 s->mb_intra = 1;
00832
00833 if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration > 1) {
00834 ff_xvmc_pack_pblocks(s, -1);
00835 if (s->swap_uv) {
00836 exchange_uv(s);
00837 }
00838 }
00839
00840 if (s->codec_id == CODEC_ID_MPEG2VIDEO) {
00841 if (s->flags2 & CODEC_FLAG2_FAST) {
00842 for (i = 0; i < 6; i++) {
00843 mpeg2_fast_decode_block_intra(s, *s->pblocks[i], i);
00844 }
00845 } else {
00846 for (i = 0; i < mb_block_count; i++) {
00847 if (mpeg2_decode_block_intra(s, *s->pblocks[i], i) < 0)
00848 return -1;
00849 }
00850 }
00851 } else {
00852 for (i = 0; i < 6; i++) {
00853 if (mpeg1_decode_block_intra(s, *s->pblocks[i], i) < 0)
00854 return -1;
00855 }
00856 }
00857 } else {
00858 if (mb_type & MB_TYPE_ZERO_MV) {
00859 assert(mb_type & MB_TYPE_CBP);
00860
00861 s->mv_dir = MV_DIR_FORWARD;
00862 if (s->picture_structure == PICT_FRAME) {
00863 if (!s->frame_pred_frame_dct)
00864 s->interlaced_dct = get_bits1(&s->gb);
00865 s->mv_type = MV_TYPE_16X16;
00866 } else {
00867 s->mv_type = MV_TYPE_FIELD;
00868 mb_type |= MB_TYPE_INTERLACED;
00869 s->field_select[0][0] = s->picture_structure - 1;
00870 }
00871
00872 if (IS_QUANT(mb_type))
00873 s->qscale = get_qscale(s);
00874
00875 s->last_mv[0][0][0] = 0;
00876 s->last_mv[0][0][1] = 0;
00877 s->last_mv[0][1][0] = 0;
00878 s->last_mv[0][1][1] = 0;
00879 s->mv[0][0][0] = 0;
00880 s->mv[0][0][1] = 0;
00881 } else {
00882 assert(mb_type & MB_TYPE_L0L1);
00883
00884
00885 if (s->frame_pred_frame_dct)
00886 motion_type = MT_FRAME;
00887 else {
00888 motion_type = get_bits(&s->gb, 2);
00889 if (s->picture_structure == PICT_FRAME && HAS_CBP(mb_type))
00890 s->interlaced_dct = get_bits1(&s->gb);
00891 }
00892
00893 if (IS_QUANT(mb_type))
00894 s->qscale = get_qscale(s);
00895
00896
00897 s->mv_dir = (mb_type >> 13) & 3;
00898 av_dlog(s->avctx, "motion_type=%d\n", motion_type);
00899 switch (motion_type) {
00900 case MT_FRAME:
00901 if (s->picture_structure == PICT_FRAME) {
00902 mb_type |= MB_TYPE_16x16;
00903 s->mv_type = MV_TYPE_16X16;
00904 for (i = 0; i < 2; i++) {
00905 if (USES_LIST(mb_type, i)) {
00906
00907 s->mv[i][0][0]= s->last_mv[i][0][0]= s->last_mv[i][1][0] =
00908 mpeg_decode_motion(s, s->mpeg_f_code[i][0], s->last_mv[i][0][0]);
00909 s->mv[i][0][1]= s->last_mv[i][0][1]= s->last_mv[i][1][1] =
00910 mpeg_decode_motion(s, s->mpeg_f_code[i][1], s->last_mv[i][0][1]);
00911
00912 if (s->full_pel[i]) {
00913 s->mv[i][0][0] <<= 1;
00914 s->mv[i][0][1] <<= 1;
00915 }
00916 }
00917 }
00918 } else {
00919 mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED;
00920 s->mv_type = MV_TYPE_16X8;
00921 for (i = 0; i < 2; i++) {
00922 if (USES_LIST(mb_type, i)) {
00923
00924 for (j = 0; j < 2; j++) {
00925 s->field_select[i][j] = get_bits1(&s->gb);
00926 for (k = 0; k < 2; k++) {
00927 val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
00928 s->last_mv[i][j][k]);
00929 s->last_mv[i][j][k] = val;
00930 s->mv[i][j][k] = val;
00931 }
00932 }
00933 }
00934 }
00935 }
00936 break;
00937 case MT_FIELD:
00938 s->mv_type = MV_TYPE_FIELD;
00939 if (s->picture_structure == PICT_FRAME) {
00940 mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED;
00941 for (i = 0; i < 2; i++) {
00942 if (USES_LIST(mb_type, i)) {
00943 for (j = 0; j < 2; j++) {
00944 s->field_select[i][j] = get_bits1(&s->gb);
00945 val = mpeg_decode_motion(s, s->mpeg_f_code[i][0],
00946 s->last_mv[i][j][0]);
00947 s->last_mv[i][j][0] = val;
00948 s->mv[i][j][0] = val;
00949 av_dlog(s->avctx, "fmx=%d\n", val);
00950 val = mpeg_decode_motion(s, s->mpeg_f_code[i][1],
00951 s->last_mv[i][j][1] >> 1);
00952 s->last_mv[i][j][1] = val << 1;
00953 s->mv[i][j][1] = val;
00954 av_dlog(s->avctx, "fmy=%d\n", val);
00955 }
00956 }
00957 }
00958 } else {
00959 mb_type |= MB_TYPE_16x16 | MB_TYPE_INTERLACED;
00960 for (i = 0; i < 2; i++) {
00961 if (USES_LIST(mb_type, i)) {
00962 s->field_select[i][0] = get_bits1(&s->gb);
00963 for (k = 0; k < 2; k++) {
00964 val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
00965 s->last_mv[i][0][k]);
00966 s->last_mv[i][0][k] = val;
00967 s->last_mv[i][1][k] = val;
00968 s->mv[i][0][k] = val;
00969 }
00970 }
00971 }
00972 }
00973 break;
00974 case MT_DMV:
00975 s->mv_type = MV_TYPE_DMV;
00976 for (i = 0; i < 2; i++) {
00977 if (USES_LIST(mb_type, i)) {
00978 int dmx, dmy, mx, my, m;
00979 const int my_shift = s->picture_structure == PICT_FRAME;
00980
00981 mx = mpeg_decode_motion(s, s->mpeg_f_code[i][0],
00982 s->last_mv[i][0][0]);
00983 s->last_mv[i][0][0] = mx;
00984 s->last_mv[i][1][0] = mx;
00985 dmx = get_dmv(s);
00986 my = mpeg_decode_motion(s, s->mpeg_f_code[i][1],
00987 s->last_mv[i][0][1] >> my_shift);
00988 dmy = get_dmv(s);
00989
00990
00991 s->last_mv[i][0][1] = my << my_shift;
00992 s->last_mv[i][1][1] = my << my_shift;
00993
00994 s->mv[i][0][0] = mx;
00995 s->mv[i][0][1] = my;
00996 s->mv[i][1][0] = mx;
00997 s->mv[i][1][1] = my;
00998
00999 if (s->picture_structure == PICT_FRAME) {
01000 mb_type |= MB_TYPE_16x16 | MB_TYPE_INTERLACED;
01001
01002
01003 m = s->top_field_first ? 1 : 3;
01004
01005
01006 s->mv[i][2][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
01007 s->mv[i][2][1] = ((my * m + (my > 0)) >> 1) + dmy - 1;
01008 m = 4 - m;
01009 s->mv[i][3][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
01010 s->mv[i][3][1] = ((my * m + (my > 0)) >> 1) + dmy + 1;
01011 } else {
01012 mb_type |= MB_TYPE_16x16;
01013
01014 s->mv[i][2][0] = ((mx + (mx > 0)) >> 1) + dmx;
01015 s->mv[i][2][1] = ((my + (my > 0)) >> 1) + dmy;
01016 if (s->picture_structure == PICT_TOP_FIELD)
01017 s->mv[i][2][1]--;
01018 else
01019 s->mv[i][2][1]++;
01020 }
01021 }
01022 }
01023 break;
01024 default:
01025 av_log(s->avctx, AV_LOG_ERROR, "00 motion_type at %d %d\n", s->mb_x, s->mb_y);
01026 return -1;
01027 }
01028 }
01029
01030 s->mb_intra = 0;
01031 if (HAS_CBP(mb_type)) {
01032 s->dsp.clear_blocks(s->block[0]);
01033
01034 cbp = get_vlc2(&s->gb, mb_pat_vlc.table, MB_PAT_VLC_BITS, 1);
01035 if (mb_block_count > 6) {
01036 cbp <<= mb_block_count - 6;
01037 cbp |= get_bits(&s->gb, mb_block_count - 6);
01038 s->dsp.clear_blocks(s->block[6]);
01039 }
01040 if (cbp <= 0) {
01041 av_log(s->avctx, AV_LOG_ERROR, "invalid cbp at %d %d\n", s->mb_x, s->mb_y);
01042 return -1;
01043 }
01044
01045
01046 if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration > 1) {
01047 ff_xvmc_pack_pblocks(s, cbp);
01048 if (s->swap_uv) {
01049 exchange_uv(s);
01050 }
01051 }
01052
01053 if (s->codec_id == CODEC_ID_MPEG2VIDEO) {
01054 if (s->flags2 & CODEC_FLAG2_FAST) {
01055 for (i = 0; i < 6; i++) {
01056 if (cbp & 32) {
01057 mpeg2_fast_decode_block_non_intra(s, *s->pblocks[i], i);
01058 } else {
01059 s->block_last_index[i] = -1;
01060 }
01061 cbp += cbp;
01062 }
01063 } else {
01064 cbp <<= 12-mb_block_count;
01065
01066 for (i = 0; i < mb_block_count; i++) {
01067 if (cbp & (1 << 11)) {
01068 if (mpeg2_decode_block_non_intra(s, *s->pblocks[i], i) < 0)
01069 return -1;
01070 } else {
01071 s->block_last_index[i] = -1;
01072 }
01073 cbp += cbp;
01074 }
01075 }
01076 } else {
01077 if (s->flags2 & CODEC_FLAG2_FAST) {
01078 for (i = 0; i < 6; i++) {
01079 if (cbp & 32) {
01080 mpeg1_fast_decode_block_inter(s, *s->pblocks[i], i);
01081 } else {
01082 s->block_last_index[i] = -1;
01083 }
01084 cbp += cbp;
01085 }
01086 } else {
01087 for (i = 0; i < 6; i++) {
01088 if (cbp & 32) {
01089 if (mpeg1_decode_block_inter(s, *s->pblocks[i], i) < 0)
01090 return -1;
01091 } else {
01092 s->block_last_index[i] = -1;
01093 }
01094 cbp += cbp;
01095 }
01096 }
01097 }
01098 } else {
01099 for (i = 0; i < 12; i++)
01100 s->block_last_index[i] = -1;
01101 }
01102 }
01103
01104 s->current_picture.f.mb_type[s->mb_x + s->mb_y * s->mb_stride] = mb_type;
01105
01106 return 0;
01107 }
01108
01109 static av_cold int mpeg_decode_init(AVCodecContext *avctx)
01110 {
01111 Mpeg1Context *s = avctx->priv_data;
01112 MpegEncContext *s2 = &s->mpeg_enc_ctx;
01113 int i;
01114
01115
01116
01117 for (i = 0; i < 64; i++)
01118 s2->dsp.idct_permutation[i]=i;
01119
01120 MPV_decode_defaults(s2);
01121
01122 s->mpeg_enc_ctx.avctx = avctx;
01123 s->mpeg_enc_ctx.flags = avctx->flags;
01124 s->mpeg_enc_ctx.flags2 = avctx->flags2;
01125 ff_mpeg12_common_init(&s->mpeg_enc_ctx);
01126 ff_mpeg12_init_vlcs();
01127
01128 s->mpeg_enc_ctx_allocated = 0;
01129 s->mpeg_enc_ctx.picture_number = 0;
01130 s->repeat_field = 0;
01131 s->mpeg_enc_ctx.codec_id = avctx->codec->id;
01132 avctx->color_range = AVCOL_RANGE_MPEG;
01133 if (avctx->codec->id == CODEC_ID_MPEG1VIDEO)
01134 avctx->chroma_sample_location = AVCHROMA_LOC_CENTER;
01135 else
01136 avctx->chroma_sample_location = AVCHROMA_LOC_LEFT;
01137 return 0;
01138 }
01139
01140 static int mpeg_decode_update_thread_context(AVCodecContext *avctx, const AVCodecContext *avctx_from)
01141 {
01142 Mpeg1Context *ctx = avctx->priv_data, *ctx_from = avctx_from->priv_data;
01143 MpegEncContext *s = &ctx->mpeg_enc_ctx, *s1 = &ctx_from->mpeg_enc_ctx;
01144 int err;
01145
01146 if (avctx == avctx_from || !ctx_from->mpeg_enc_ctx_allocated || !s1->context_initialized)
01147 return 0;
01148
01149 err = ff_mpeg_update_thread_context(avctx, avctx_from);
01150 if (err) return err;
01151
01152 if (!ctx->mpeg_enc_ctx_allocated)
01153 memcpy(s + 1, s1 + 1, sizeof(Mpeg1Context) - sizeof(MpegEncContext));
01154
01155 if (!(s->pict_type == AV_PICTURE_TYPE_B || s->low_delay))
01156 s->picture_number++;
01157
01158 return 0;
01159 }
01160
01161 static void quant_matrix_rebuild(uint16_t *matrix, const uint8_t *old_perm,
01162 const uint8_t *new_perm)
01163 {
01164 uint16_t temp_matrix[64];
01165 int i;
01166
01167 memcpy(temp_matrix, matrix, 64 * sizeof(uint16_t));
01168
01169 for (i = 0; i < 64; i++) {
01170 matrix[new_perm[i]] = temp_matrix[old_perm[i]];
01171 }
01172 }
01173
01174 static const enum PixelFormat pixfmt_xvmc_mpg2_420[] = {
01175 PIX_FMT_XVMC_MPEG2_IDCT,
01176 PIX_FMT_XVMC_MPEG2_MC,
01177 PIX_FMT_NONE };
01178
01179 static enum PixelFormat mpeg_get_pixelformat(AVCodecContext *avctx)
01180 {
01181 Mpeg1Context *s1 = avctx->priv_data;
01182 MpegEncContext *s = &s1->mpeg_enc_ctx;
01183
01184 if (avctx->xvmc_acceleration)
01185 return avctx->get_format(avctx, pixfmt_xvmc_mpg2_420);
01186 else if (avctx->codec->capabilities & CODEC_CAP_HWACCEL_VDPAU) {
01187 if (avctx->codec_id == CODEC_ID_MPEG1VIDEO)
01188 return PIX_FMT_VDPAU_MPEG1;
01189 else
01190 return PIX_FMT_VDPAU_MPEG2;
01191 } else {
01192 if (s->chroma_format < 2)
01193 return avctx->get_format(avctx, ff_hwaccel_pixfmt_list_420);
01194 else if (s->chroma_format == 2)
01195 return PIX_FMT_YUV422P;
01196 else
01197 return PIX_FMT_YUV444P;
01198 }
01199 }
01200
01201
01202
01203 static int mpeg_decode_postinit(AVCodecContext *avctx)
01204 {
01205 Mpeg1Context *s1 = avctx->priv_data;
01206 MpegEncContext *s = &s1->mpeg_enc_ctx;
01207 uint8_t old_permutation[64];
01208
01209 if ((s1->mpeg_enc_ctx_allocated == 0) ||
01210 avctx->coded_width != s->width ||
01211 avctx->coded_height != s->height ||
01212 s1->save_width != s->width ||
01213 s1->save_height != s->height ||
01214 s1->save_aspect_info != s->aspect_ratio_info ||
01215 s1->save_progressive_seq != s->progressive_sequence ||
01216 0)
01217 {
01218
01219 if (s1->mpeg_enc_ctx_allocated) {
01220 ParseContext pc = s->parse_context;
01221 s->parse_context.buffer = 0;
01222 MPV_common_end(s);
01223 s->parse_context = pc;
01224 }
01225
01226 if ((s->width == 0) || (s->height == 0))
01227 return -2;
01228
01229 avcodec_set_dimensions(avctx, s->width, s->height);
01230 avctx->bit_rate = s->bit_rate;
01231 s1->save_aspect_info = s->aspect_ratio_info;
01232 s1->save_width = s->width;
01233 s1->save_height = s->height;
01234 s1->save_progressive_seq = s->progressive_sequence;
01235
01236
01237
01238 avctx->has_b_frames = !s->low_delay;
01239
01240 assert((avctx->sub_id == 1) == (avctx->codec_id == CODEC_ID_MPEG1VIDEO));
01241 if (avctx->codec_id == CODEC_ID_MPEG1VIDEO) {
01242
01243 avctx->time_base.den = avpriv_frame_rate_tab[s->frame_rate_index].num;
01244 avctx->time_base.num = avpriv_frame_rate_tab[s->frame_rate_index].den;
01245
01246 avctx->sample_aspect_ratio = av_d2q(1.0/ff_mpeg1_aspect[s->aspect_ratio_info], 255);
01247 avctx->ticks_per_frame=1;
01248 } else {
01249
01250 av_reduce(&s->avctx->time_base.den,
01251 &s->avctx->time_base.num,
01252 avpriv_frame_rate_tab[s->frame_rate_index].num * s1->frame_rate_ext.num*2,
01253 avpriv_frame_rate_tab[s->frame_rate_index].den * s1->frame_rate_ext.den,
01254 1 << 30);
01255 avctx->ticks_per_frame = 2;
01256
01257 if (s->aspect_ratio_info > 1) {
01258 AVRational dar =
01259 av_mul_q(av_div_q(ff_mpeg2_aspect[s->aspect_ratio_info],
01260 (AVRational) {s1->pan_scan.width, s1->pan_scan.height}),
01261 (AVRational) {s->width, s->height});
01262
01263
01264
01265
01266 if ((s1->pan_scan.width == 0) || (s1->pan_scan.height == 0) ||
01267 (av_cmp_q(dar, (AVRational) {4, 3}) && av_cmp_q(dar, (AVRational) {16, 9}))) {
01268 s->avctx->sample_aspect_ratio =
01269 av_div_q(ff_mpeg2_aspect[s->aspect_ratio_info],
01270 (AVRational) {s->width, s->height});
01271 } else {
01272 s->avctx->sample_aspect_ratio =
01273 av_div_q(ff_mpeg2_aspect[s->aspect_ratio_info],
01274 (AVRational) {s1->pan_scan.width, s1->pan_scan.height});
01275
01276
01277
01278
01279
01280
01281 }
01282 } else {
01283 s->avctx->sample_aspect_ratio =
01284 ff_mpeg2_aspect[s->aspect_ratio_info];
01285 }
01286 }
01287
01288 avctx->pix_fmt = mpeg_get_pixelformat(avctx);
01289 avctx->hwaccel = ff_find_hwaccel(avctx->codec->id, avctx->pix_fmt);
01290
01291 if (avctx->pix_fmt == PIX_FMT_XVMC_MPEG2_IDCT ||
01292 avctx->hwaccel ||
01293 s->avctx->codec->capabilities & CODEC_CAP_HWACCEL_VDPAU)
01294 if (avctx->idct_algo == FF_IDCT_AUTO)
01295 avctx->idct_algo = FF_IDCT_SIMPLE;
01296
01297
01298
01299 memcpy(old_permutation, s->dsp.idct_permutation, 64 * sizeof(uint8_t));
01300
01301 if (MPV_common_init(s) < 0)
01302 return -2;
01303
01304 quant_matrix_rebuild(s->intra_matrix, old_permutation, s->dsp.idct_permutation);
01305 quant_matrix_rebuild(s->inter_matrix, old_permutation, s->dsp.idct_permutation);
01306 quant_matrix_rebuild(s->chroma_intra_matrix, old_permutation, s->dsp.idct_permutation);
01307 quant_matrix_rebuild(s->chroma_inter_matrix, old_permutation, s->dsp.idct_permutation);
01308
01309 s1->mpeg_enc_ctx_allocated = 1;
01310 }
01311 return 0;
01312 }
01313
01314 static int mpeg1_decode_picture(AVCodecContext *avctx,
01315 const uint8_t *buf, int buf_size)
01316 {
01317 Mpeg1Context *s1 = avctx->priv_data;
01318 MpegEncContext *s = &s1->mpeg_enc_ctx;
01319 int ref, f_code, vbv_delay;
01320
01321 init_get_bits(&s->gb, buf, buf_size*8);
01322
01323 ref = get_bits(&s->gb, 10);
01324 s->pict_type = get_bits(&s->gb, 3);
01325 if (s->pict_type == 0 || s->pict_type > 3)
01326 return -1;
01327
01328 vbv_delay = get_bits(&s->gb, 16);
01329 if (s->pict_type == AV_PICTURE_TYPE_P || s->pict_type == AV_PICTURE_TYPE_B) {
01330 s->full_pel[0] = get_bits1(&s->gb);
01331 f_code = get_bits(&s->gb, 3);
01332 if (f_code == 0 && (avctx->err_recognition & AV_EF_BITSTREAM))
01333 return -1;
01334 s->mpeg_f_code[0][0] = f_code;
01335 s->mpeg_f_code[0][1] = f_code;
01336 }
01337 if (s->pict_type == AV_PICTURE_TYPE_B) {
01338 s->full_pel[1] = get_bits1(&s->gb);
01339 f_code = get_bits(&s->gb, 3);
01340 if (f_code == 0 && (avctx->err_recognition & AV_EF_BITSTREAM))
01341 return -1;
01342 s->mpeg_f_code[1][0] = f_code;
01343 s->mpeg_f_code[1][1] = f_code;
01344 }
01345 s->current_picture.f.pict_type = s->pict_type;
01346 s->current_picture.f.key_frame = s->pict_type == AV_PICTURE_TYPE_I;
01347
01348 if (avctx->debug & FF_DEBUG_PICT_INFO)
01349 av_log(avctx, AV_LOG_DEBUG, "vbv_delay %d, ref %d type:%d\n", vbv_delay, ref, s->pict_type);
01350
01351 s->y_dc_scale = 8;
01352 s->c_dc_scale = 8;
01353 return 0;
01354 }
01355
01356 static void mpeg_decode_sequence_extension(Mpeg1Context *s1)
01357 {
01358 MpegEncContext *s= &s1->mpeg_enc_ctx;
01359 int horiz_size_ext, vert_size_ext;
01360 int bit_rate_ext;
01361
01362 skip_bits(&s->gb, 1);
01363 s->avctx->profile = get_bits(&s->gb, 3);
01364 s->avctx->level = get_bits(&s->gb, 4);
01365 s->progressive_sequence = get_bits1(&s->gb);
01366 s->chroma_format = get_bits(&s->gb, 2);
01367 horiz_size_ext = get_bits(&s->gb, 2);
01368 vert_size_ext = get_bits(&s->gb, 2);
01369 s->width |= (horiz_size_ext << 12);
01370 s->height |= (vert_size_ext << 12);
01371 bit_rate_ext = get_bits(&s->gb, 12);
01372 s->bit_rate += (bit_rate_ext << 18) * 400;
01373 skip_bits1(&s->gb);
01374 s->avctx->rc_buffer_size += get_bits(&s->gb, 8) * 1024 * 16 << 10;
01375
01376 s->low_delay = get_bits1(&s->gb);
01377 if (s->flags & CODEC_FLAG_LOW_DELAY)
01378 s->low_delay = 1;
01379
01380 s1->frame_rate_ext.num = get_bits(&s->gb, 2) + 1;
01381 s1->frame_rate_ext.den = get_bits(&s->gb, 5) + 1;
01382
01383 av_dlog(s->avctx, "sequence extension\n");
01384 s->codec_id = s->avctx->codec_id = CODEC_ID_MPEG2VIDEO;
01385 s->avctx->sub_id = 2;
01386
01387 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
01388 av_log(s->avctx, AV_LOG_DEBUG, "profile: %d, level: %d vbv buffer: %d, bitrate:%d\n",
01389 s->avctx->profile, s->avctx->level, s->avctx->rc_buffer_size, s->bit_rate);
01390
01391 }
01392
01393 static void mpeg_decode_sequence_display_extension(Mpeg1Context *s1)
01394 {
01395 MpegEncContext *s = &s1->mpeg_enc_ctx;
01396 int color_description, w, h;
01397
01398 skip_bits(&s->gb, 3);
01399 color_description = get_bits1(&s->gb);
01400 if (color_description) {
01401 s->avctx->color_primaries = get_bits(&s->gb, 8);
01402 s->avctx->color_trc = get_bits(&s->gb, 8);
01403 s->avctx->colorspace = get_bits(&s->gb, 8);
01404 }
01405 w = get_bits(&s->gb, 14);
01406 skip_bits(&s->gb, 1);
01407 h = get_bits(&s->gb, 14);
01408
01409
01410 s1->pan_scan.width = 16 * w;
01411 s1->pan_scan.height = 16 * h;
01412
01413 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
01414 av_log(s->avctx, AV_LOG_DEBUG, "sde w:%d, h:%d\n", w, h);
01415 }
01416
01417 static void mpeg_decode_picture_display_extension(Mpeg1Context *s1)
01418 {
01419 MpegEncContext *s = &s1->mpeg_enc_ctx;
01420 int i, nofco;
01421
01422 nofco = 1;
01423 if (s->progressive_sequence) {
01424 if (s->repeat_first_field) {
01425 nofco++;
01426 if (s->top_field_first)
01427 nofco++;
01428 }
01429 } else {
01430 if (s->picture_structure == PICT_FRAME) {
01431 nofco++;
01432 if (s->repeat_first_field)
01433 nofco++;
01434 }
01435 }
01436 for (i = 0; i < nofco; i++) {
01437 s1->pan_scan.position[i][0] = get_sbits(&s->gb, 16);
01438 skip_bits(&s->gb, 1);
01439 s1->pan_scan.position[i][1] = get_sbits(&s->gb, 16);
01440 skip_bits(&s->gb, 1);
01441 }
01442
01443 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
01444 av_log(s->avctx, AV_LOG_DEBUG, "pde (%d,%d) (%d,%d) (%d,%d)\n",
01445 s1->pan_scan.position[0][0], s1->pan_scan.position[0][1],
01446 s1->pan_scan.position[1][0], s1->pan_scan.position[1][1],
01447 s1->pan_scan.position[2][0], s1->pan_scan.position[2][1]);
01448 }
01449
01450 static int load_matrix(MpegEncContext *s, uint16_t matrix0[64], uint16_t matrix1[64], int intra)
01451 {
01452 int i;
01453
01454 for (i = 0; i < 64; i++) {
01455 int j = s->dsp.idct_permutation[ff_zigzag_direct[i]];
01456 int v = get_bits(&s->gb, 8);
01457 if (v == 0) {
01458 av_log(s->avctx, AV_LOG_ERROR, "matrix damaged\n");
01459 return -1;
01460 }
01461 if (intra && i == 0 && v != 8) {
01462 av_log(s->avctx, AV_LOG_ERROR, "intra matrix invalid, ignoring\n");
01463 v = 8;
01464 }
01465 matrix0[j] = v;
01466 if (matrix1)
01467 matrix1[j] = v;
01468 }
01469 return 0;
01470 }
01471
01472 static void mpeg_decode_quant_matrix_extension(MpegEncContext *s)
01473 {
01474 av_dlog(s->avctx, "matrix extension\n");
01475
01476 if (get_bits1(&s->gb)) load_matrix(s, s->chroma_intra_matrix, s->intra_matrix, 1);
01477 if (get_bits1(&s->gb)) load_matrix(s, s->chroma_inter_matrix, s->inter_matrix, 0);
01478 if (get_bits1(&s->gb)) load_matrix(s, s->chroma_intra_matrix, NULL , 1);
01479 if (get_bits1(&s->gb)) load_matrix(s, s->chroma_inter_matrix, NULL , 0);
01480 }
01481
01482 static void mpeg_decode_picture_coding_extension(Mpeg1Context *s1)
01483 {
01484 MpegEncContext *s = &s1->mpeg_enc_ctx;
01485
01486 s->full_pel[0] = s->full_pel[1] = 0;
01487 s->mpeg_f_code[0][0] = get_bits(&s->gb, 4);
01488 s->mpeg_f_code[0][1] = get_bits(&s->gb, 4);
01489 s->mpeg_f_code[1][0] = get_bits(&s->gb, 4);
01490 s->mpeg_f_code[1][1] = get_bits(&s->gb, 4);
01491 if (!s->pict_type && s1->mpeg_enc_ctx_allocated) {
01492 av_log(s->avctx, AV_LOG_ERROR, "Missing picture start code, guessing missing values\n");
01493 if (s->mpeg_f_code[1][0] == 15 && s->mpeg_f_code[1][1] == 15) {
01494 if (s->mpeg_f_code[0][0] == 15 && s->mpeg_f_code[0][1] == 15)
01495 s->pict_type = AV_PICTURE_TYPE_I;
01496 else
01497 s->pict_type = AV_PICTURE_TYPE_P;
01498 } else
01499 s->pict_type = AV_PICTURE_TYPE_B;
01500 s->current_picture.f.pict_type = s->pict_type;
01501 s->current_picture.f.key_frame = s->pict_type == AV_PICTURE_TYPE_I;
01502 }
01503 s->intra_dc_precision = get_bits(&s->gb, 2);
01504 s->picture_structure = get_bits(&s->gb, 2);
01505 s->top_field_first = get_bits1(&s->gb);
01506 s->frame_pred_frame_dct = get_bits1(&s->gb);
01507 s->concealment_motion_vectors = get_bits1(&s->gb);
01508 s->q_scale_type = get_bits1(&s->gb);
01509 s->intra_vlc_format = get_bits1(&s->gb);
01510 s->alternate_scan = get_bits1(&s->gb);
01511 s->repeat_first_field = get_bits1(&s->gb);
01512 s->chroma_420_type = get_bits1(&s->gb);
01513 s->progressive_frame = get_bits1(&s->gb);
01514
01515 if (s->progressive_sequence && !s->progressive_frame) {
01516 s->progressive_frame = 1;
01517 av_log(s->avctx, AV_LOG_ERROR, "interlaced frame in progressive sequence, ignoring\n");
01518 }
01519
01520 if (s->picture_structure == 0 || (s->progressive_frame && s->picture_structure != PICT_FRAME)) {
01521 av_log(s->avctx, AV_LOG_ERROR, "picture_structure %d invalid, ignoring\n", s->picture_structure);
01522 s->picture_structure = PICT_FRAME;
01523 }
01524
01525 if (s->progressive_sequence && !s->frame_pred_frame_dct) {
01526 av_log(s->avctx, AV_LOG_ERROR, "invalid frame_pred_frame_dct\n");
01527 s->frame_pred_frame_dct = 1;
01528 }
01529
01530 if (s->picture_structure == PICT_FRAME) {
01531 s->first_field = 0;
01532 s->v_edge_pos = 16 * s->mb_height;
01533 } else {
01534 s->first_field ^= 1;
01535 s->v_edge_pos = 8 * s->mb_height;
01536 memset(s->mbskip_table, 0, s->mb_stride * s->mb_height);
01537 }
01538
01539 if (s->alternate_scan) {
01540 ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable, ff_alternate_vertical_scan);
01541 ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable, ff_alternate_vertical_scan);
01542 } else {
01543 ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable, ff_zigzag_direct);
01544 ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable, ff_zigzag_direct);
01545 }
01546
01547
01548 av_dlog(s->avctx, "intra_dc_precision=%d\n", s->intra_dc_precision);
01549 av_dlog(s->avctx, "picture_structure=%d\n", s->picture_structure);
01550 av_dlog(s->avctx, "top field first=%d\n", s->top_field_first);
01551 av_dlog(s->avctx, "repeat first field=%d\n", s->repeat_first_field);
01552 av_dlog(s->avctx, "conceal=%d\n", s->concealment_motion_vectors);
01553 av_dlog(s->avctx, "intra_vlc_format=%d\n", s->intra_vlc_format);
01554 av_dlog(s->avctx, "alternate_scan=%d\n", s->alternate_scan);
01555 av_dlog(s->avctx, "frame_pred_frame_dct=%d\n", s->frame_pred_frame_dct);
01556 av_dlog(s->avctx, "progressive_frame=%d\n", s->progressive_frame);
01557 }
01558
01559 static int mpeg_field_start(MpegEncContext *s, const uint8_t *buf, int buf_size)
01560 {
01561 AVCodecContext *avctx = s->avctx;
01562 Mpeg1Context *s1 = (Mpeg1Context*)s;
01563
01564
01565 if (s->first_field || s->picture_structure == PICT_FRAME) {
01566 if (MPV_frame_start(s, avctx) < 0)
01567 return -1;
01568
01569 ff_er_frame_start(s);
01570
01571
01572 s->current_picture_ptr->f.repeat_pict = 0;
01573 if (s->repeat_first_field) {
01574 if (s->progressive_sequence) {
01575 if (s->top_field_first)
01576 s->current_picture_ptr->f.repeat_pict = 4;
01577 else
01578 s->current_picture_ptr->f.repeat_pict = 2;
01579 } else if (s->progressive_frame) {
01580 s->current_picture_ptr->f.repeat_pict = 1;
01581 }
01582 }
01583
01584 *s->current_picture_ptr->f.pan_scan = s1->pan_scan;
01585
01586 if (HAVE_THREADS && (avctx->active_thread_type & FF_THREAD_FRAME))
01587 ff_thread_finish_setup(avctx);
01588 } else {
01589 int i;
01590
01591 if (!s->current_picture_ptr) {
01592 av_log(s->avctx, AV_LOG_ERROR, "first field missing\n");
01593 return -1;
01594 }
01595
01596 for (i = 0; i < 4; i++) {
01597 s->current_picture.f.data[i] = s->current_picture_ptr->f.data[i];
01598 if (s->picture_structure == PICT_BOTTOM_FIELD) {
01599 s->current_picture.f.data[i] += s->current_picture_ptr->f.linesize[i];
01600 }
01601 }
01602 }
01603
01604 if (avctx->hwaccel) {
01605 if (avctx->hwaccel->start_frame(avctx, buf, buf_size) < 0)
01606 return -1;
01607 }
01608
01609
01610
01611 if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration)
01612 if (ff_xvmc_field_start(s, avctx) < 0)
01613 return -1;
01614
01615 return 0;
01616 }
01617
01618 #define DECODE_SLICE_ERROR -1
01619 #define DECODE_SLICE_OK 0
01620
01627 static int mpeg_decode_slice(MpegEncContext *s, int mb_y,
01628 const uint8_t **buf, int buf_size)
01629 {
01630 AVCodecContext *avctx = s->avctx;
01631 const int lowres = s->avctx->lowres;
01632 const int field_pic = s->picture_structure != PICT_FRAME;
01633
01634 s->resync_mb_x =
01635 s->resync_mb_y = -1;
01636
01637 assert(mb_y < s->mb_height);
01638
01639 init_get_bits(&s->gb, *buf, buf_size * 8);
01640
01641 ff_mpeg1_clean_buffers(s);
01642 s->interlaced_dct = 0;
01643
01644 s->qscale = get_qscale(s);
01645
01646 if (s->qscale == 0) {
01647 av_log(s->avctx, AV_LOG_ERROR, "qscale == 0\n");
01648 return -1;
01649 }
01650
01651
01652 while (get_bits1(&s->gb) != 0) {
01653 skip_bits(&s->gb, 8);
01654 }
01655
01656 s->mb_x = 0;
01657
01658 if (mb_y == 0 && s->codec_tag == AV_RL32("SLIF")) {
01659 skip_bits1(&s->gb);
01660 } else {
01661 while (get_bits_left(&s->gb) > 0) {
01662 int code = get_vlc2(&s->gb, mbincr_vlc.table, MBINCR_VLC_BITS, 2);
01663 if (code < 0) {
01664 av_log(s->avctx, AV_LOG_ERROR, "first mb_incr damaged\n");
01665 return -1;
01666 }
01667 if (code >= 33) {
01668 if (code == 33) {
01669 s->mb_x += 33;
01670 }
01671
01672 } else {
01673 s->mb_x += code;
01674 break;
01675 }
01676 }
01677 }
01678
01679 if (s->mb_x >= (unsigned)s->mb_width) {
01680 av_log(s->avctx, AV_LOG_ERROR, "initial skip overflow\n");
01681 return -1;
01682 }
01683
01684 if (avctx->hwaccel) {
01685 const uint8_t *buf_end, *buf_start = *buf - 4;
01686 int start_code = -1;
01687 buf_end = avpriv_mpv_find_start_code(buf_start + 2, *buf + buf_size, &start_code);
01688 if (buf_end < *buf + buf_size)
01689 buf_end -= 4;
01690 s->mb_y = mb_y;
01691 if (avctx->hwaccel->decode_slice(avctx, buf_start, buf_end - buf_start) < 0)
01692 return DECODE_SLICE_ERROR;
01693 *buf = buf_end;
01694 return DECODE_SLICE_OK;
01695 }
01696
01697 s->resync_mb_x = s->mb_x;
01698 s->resync_mb_y = s->mb_y = mb_y;
01699 s->mb_skip_run = 0;
01700 ff_init_block_index(s);
01701
01702 if (s->mb_y == 0 && s->mb_x == 0 && (s->first_field || s->picture_structure == PICT_FRAME)) {
01703 if (s->avctx->debug & FF_DEBUG_PICT_INFO) {
01704 av_log(s->avctx, AV_LOG_DEBUG, "qp:%d fc:%2d%2d%2d%2d %s %s %s %s %s dc:%d pstruct:%d fdct:%d cmv:%d qtype:%d ivlc:%d rff:%d %s\n",
01705 s->qscale, s->mpeg_f_code[0][0], s->mpeg_f_code[0][1], s->mpeg_f_code[1][0], s->mpeg_f_code[1][1],
01706 s->pict_type == AV_PICTURE_TYPE_I ? "I" : (s->pict_type == AV_PICTURE_TYPE_P ? "P" : (s->pict_type == AV_PICTURE_TYPE_B ? "B" : "S")),
01707 s->progressive_sequence ? "ps" :"", s->progressive_frame ? "pf" : "", s->alternate_scan ? "alt" :"", s->top_field_first ? "top" :"",
01708 s->intra_dc_precision, s->picture_structure, s->frame_pred_frame_dct, s->concealment_motion_vectors,
01709 s->q_scale_type, s->intra_vlc_format, s->repeat_first_field, s->chroma_420_type ? "420" :"");
01710 }
01711 }
01712
01713 for (;;) {
01714
01715 if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration > 1)
01716 ff_xvmc_init_block(s);
01717
01718 if (mpeg_decode_mb(s, s->block) < 0)
01719 return -1;
01720
01721 if (s->current_picture.f.motion_val[0] && !s->encoding) {
01722 const int wrap = s->b8_stride;
01723 int xy = s->mb_x * 2 + s->mb_y * 2 * wrap;
01724 int b8_xy = 4 * (s->mb_x + s->mb_y * s->mb_stride);
01725 int motion_x, motion_y, dir, i;
01726
01727 for (i = 0; i < 2; i++) {
01728 for (dir = 0; dir < 2; dir++) {
01729 if (s->mb_intra || (dir == 1 && s->pict_type != AV_PICTURE_TYPE_B)) {
01730 motion_x = motion_y = 0;
01731 } else if (s->mv_type == MV_TYPE_16X16 || (s->mv_type == MV_TYPE_FIELD && field_pic)) {
01732 motion_x = s->mv[dir][0][0];
01733 motion_y = s->mv[dir][0][1];
01734 } else {
01735 motion_x = s->mv[dir][i][0];
01736 motion_y = s->mv[dir][i][1];
01737 }
01738
01739 s->current_picture.f.motion_val[dir][xy ][0] = motion_x;
01740 s->current_picture.f.motion_val[dir][xy ][1] = motion_y;
01741 s->current_picture.f.motion_val[dir][xy + 1][0] = motion_x;
01742 s->current_picture.f.motion_val[dir][xy + 1][1] = motion_y;
01743 s->current_picture.f.ref_index [dir][b8_xy ] =
01744 s->current_picture.f.ref_index [dir][b8_xy + 1] = s->field_select[dir][i];
01745 assert(s->field_select[dir][i] == 0 || s->field_select[dir][i] == 1);
01746 }
01747 xy += wrap;
01748 b8_xy +=2;
01749 }
01750 }
01751
01752 s->dest[0] += 16 >> lowres;
01753 s->dest[1] +=(16 >> lowres) >> s->chroma_x_shift;
01754 s->dest[2] +=(16 >> lowres) >> s->chroma_x_shift;
01755
01756 MPV_decode_mb(s, s->block);
01757
01758 if (++s->mb_x >= s->mb_width) {
01759 const int mb_size = 16 >> s->avctx->lowres;
01760
01761 ff_draw_horiz_band(s, mb_size*(s->mb_y >> field_pic), mb_size);
01762 MPV_report_decode_progress(s);
01763
01764 s->mb_x = 0;
01765 s->mb_y += 1 << field_pic;
01766
01767 if (s->mb_y >= s->mb_height) {
01768 int left = get_bits_left(&s->gb);
01769 int is_d10 = s->chroma_format == 2 && s->pict_type == AV_PICTURE_TYPE_I && avctx->profile == 0 && avctx->level == 5
01770 && s->intra_dc_precision == 2 && s->q_scale_type == 1 && s->alternate_scan == 0
01771 && s->progressive_frame == 0 ;
01772
01773 if (left < 0 || (left && show_bits(&s->gb, FFMIN(left, 23)) && !is_d10)
01774 || ((avctx->err_recognition & AV_EF_BUFFER) && left > 8)) {
01775 av_log(avctx, AV_LOG_ERROR, "end mismatch left=%d %0X\n", left, show_bits(&s->gb, FFMIN(left, 23)));
01776 return -1;
01777 } else
01778 goto eos;
01779 }
01780
01781 ff_init_block_index(s);
01782 }
01783
01784
01785 if (s->mb_skip_run == -1) {
01786
01787 s->mb_skip_run = 0;
01788 for (;;) {
01789 int code = get_vlc2(&s->gb, mbincr_vlc.table, MBINCR_VLC_BITS, 2);
01790 if (code < 0) {
01791 av_log(s->avctx, AV_LOG_ERROR, "mb incr damaged\n");
01792 return -1;
01793 }
01794 if (code >= 33) {
01795 if (code == 33) {
01796 s->mb_skip_run += 33;
01797 } else if (code == 35) {
01798 if (s->mb_skip_run != 0 || show_bits(&s->gb, 15) != 0) {
01799 av_log(s->avctx, AV_LOG_ERROR, "slice mismatch\n");
01800 return -1;
01801 }
01802 goto eos;
01803 }
01804
01805 } else {
01806 s->mb_skip_run += code;
01807 break;
01808 }
01809 }
01810 if (s->mb_skip_run) {
01811 int i;
01812 if (s->pict_type == AV_PICTURE_TYPE_I) {
01813 av_log(s->avctx, AV_LOG_ERROR, "skipped MB in I frame at %d %d\n", s->mb_x, s->mb_y);
01814 return -1;
01815 }
01816
01817
01818 s->mb_intra = 0;
01819 for (i = 0; i < 12; i++)
01820 s->block_last_index[i] = -1;
01821 if (s->picture_structure == PICT_FRAME)
01822 s->mv_type = MV_TYPE_16X16;
01823 else
01824 s->mv_type = MV_TYPE_FIELD;
01825 if (s->pict_type == AV_PICTURE_TYPE_P) {
01826
01827 s->mv_dir = MV_DIR_FORWARD;
01828 s->mv[0][0][0] = s->mv[0][0][1] = 0;
01829 s->last_mv[0][0][0] = s->last_mv[0][0][1] = 0;
01830 s->last_mv[0][1][0] = s->last_mv[0][1][1] = 0;
01831 s->field_select[0][0] = (s->picture_structure - 1) & 1;
01832 } else {
01833
01834 s->mv[0][0][0] = s->last_mv[0][0][0];
01835 s->mv[0][0][1] = s->last_mv[0][0][1];
01836 s->mv[1][0][0] = s->last_mv[1][0][0];
01837 s->mv[1][0][1] = s->last_mv[1][0][1];
01838 }
01839 }
01840 }
01841 }
01842 eos:
01843 *buf += (get_bits_count(&s->gb)-1)/8;
01844
01845 return 0;
01846 }
01847
01848 static int slice_decode_thread(AVCodecContext *c, void *arg)
01849 {
01850 MpegEncContext *s = *(void**)arg;
01851 const uint8_t *buf = s->gb.buffer;
01852 int mb_y = s->start_mb_y;
01853 const int field_pic = s->picture_structure != PICT_FRAME;
01854
01855 s->error_count = (3 * (s->end_mb_y - s->start_mb_y) * s->mb_width) >> field_pic;
01856
01857 for (;;) {
01858 uint32_t start_code;
01859 int ret;
01860
01861 ret = mpeg_decode_slice(s, mb_y, &buf, s->gb.buffer_end - buf);
01862 emms_c();
01863
01864
01865 if (ret < 0) {
01866 if (c->err_recognition & AV_EF_EXPLODE)
01867 return ret;
01868 if (s->resync_mb_x >= 0 && s->resync_mb_y >= 0)
01869 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, ER_AC_ERROR | ER_DC_ERROR | ER_MV_ERROR);
01870 } else {
01871 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, ER_AC_END | ER_DC_END | ER_MV_END);
01872 }
01873
01874 if (s->mb_y == s->end_mb_y)
01875 return 0;
01876
01877 start_code = -1;
01878 buf = avpriv_mpv_find_start_code(buf, s->gb.buffer_end, &start_code);
01879 mb_y= (start_code - SLICE_MIN_START_CODE) << field_pic;
01880 if (s->picture_structure == PICT_BOTTOM_FIELD)
01881 mb_y++;
01882 if (mb_y < 0 || mb_y >= s->end_mb_y)
01883 return -1;
01884 }
01885 }
01886
01891 static int slice_end(AVCodecContext *avctx, AVFrame *pict)
01892 {
01893 Mpeg1Context *s1 = avctx->priv_data;
01894 MpegEncContext *s = &s1->mpeg_enc_ctx;
01895
01896 if (!s1->mpeg_enc_ctx_allocated || !s->current_picture_ptr)
01897 return 0;
01898
01899 if (s->avctx->hwaccel) {
01900 if (s->avctx->hwaccel->end_frame(s->avctx) < 0)
01901 av_log(avctx, AV_LOG_ERROR, "hardware accelerator failed to decode picture\n");
01902 }
01903
01904 if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration)
01905 ff_xvmc_field_end(s);
01906
01907
01908 if ( !s->first_field) {
01909
01910
01911 s->current_picture_ptr->f.qscale_type = FF_QSCALE_TYPE_MPEG2;
01912
01913 ff_er_frame_end(s);
01914
01915 MPV_frame_end(s);
01916
01917 if (s->pict_type == AV_PICTURE_TYPE_B || s->low_delay) {
01918 *pict = *(AVFrame*)s->current_picture_ptr;
01919 ff_print_debug_info(s, pict);
01920 } else {
01921 if (avctx->active_thread_type & FF_THREAD_FRAME)
01922 s->picture_number++;
01923
01924
01925 if (s->last_picture_ptr != NULL) {
01926 *pict = *(AVFrame*)s->last_picture_ptr;
01927 ff_print_debug_info(s, pict);
01928 }
01929 }
01930
01931 return 1;
01932 } else {
01933 return 0;
01934 }
01935 }
01936
01937 static int mpeg1_decode_sequence(AVCodecContext *avctx,
01938 const uint8_t *buf, int buf_size)
01939 {
01940 Mpeg1Context *s1 = avctx->priv_data;
01941 MpegEncContext *s = &s1->mpeg_enc_ctx;
01942 int width, height;
01943 int i, v, j;
01944
01945 init_get_bits(&s->gb, buf, buf_size*8);
01946
01947 width = get_bits(&s->gb, 12);
01948 height = get_bits(&s->gb, 12);
01949 if (width <= 0 || height <= 0)
01950 return -1;
01951 s->aspect_ratio_info = get_bits(&s->gb, 4);
01952 if (s->aspect_ratio_info == 0) {
01953 av_log(avctx, AV_LOG_ERROR, "aspect ratio has forbidden 0 value\n");
01954 if (avctx->err_recognition & AV_EF_BITSTREAM)
01955 return -1;
01956 }
01957 s->frame_rate_index = get_bits(&s->gb, 4);
01958 if (s->frame_rate_index == 0 || s->frame_rate_index > 13)
01959 return -1;
01960 s->bit_rate = get_bits(&s->gb, 18) * 400;
01961 if (get_bits1(&s->gb) == 0)
01962 return -1;
01963 s->width = width;
01964 s->height = height;
01965
01966 s->avctx->rc_buffer_size = get_bits(&s->gb, 10) * 1024 * 16;
01967 skip_bits(&s->gb, 1);
01968
01969
01970 if (get_bits1(&s->gb)) {
01971 load_matrix(s, s->chroma_intra_matrix, s->intra_matrix, 1);
01972 } else {
01973 for (i = 0; i < 64; i++) {
01974 j = s->dsp.idct_permutation[i];
01975 v = ff_mpeg1_default_intra_matrix[i];
01976 s->intra_matrix[j] = v;
01977 s->chroma_intra_matrix[j] = v;
01978 }
01979 }
01980 if (get_bits1(&s->gb)) {
01981 load_matrix(s, s->chroma_inter_matrix, s->inter_matrix, 0);
01982 } else {
01983 for (i = 0; i < 64; i++) {
01984 int j = s->dsp.idct_permutation[i];
01985 v = ff_mpeg1_default_non_intra_matrix[i];
01986 s->inter_matrix[j] = v;
01987 s->chroma_inter_matrix[j] = v;
01988 }
01989 }
01990
01991 if (show_bits(&s->gb, 23) != 0) {
01992 av_log(s->avctx, AV_LOG_ERROR, "sequence header damaged\n");
01993 return -1;
01994 }
01995
01996
01997 s->progressive_sequence = 1;
01998 s->progressive_frame = 1;
01999 s->picture_structure = PICT_FRAME;
02000 s->frame_pred_frame_dct = 1;
02001 s->chroma_format = 1;
02002 s->codec_id = s->avctx->codec_id = CODEC_ID_MPEG1VIDEO;
02003 avctx->sub_id = 1;
02004 s->out_format = FMT_MPEG1;
02005 s->swap_uv = 0;
02006 if (s->flags & CODEC_FLAG_LOW_DELAY)
02007 s->low_delay = 1;
02008
02009 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
02010 av_log(s->avctx, AV_LOG_DEBUG, "vbv buffer: %d, bitrate:%d\n",
02011 s->avctx->rc_buffer_size, s->bit_rate);
02012
02013 return 0;
02014 }
02015
02016 static int vcr2_init_sequence(AVCodecContext *avctx)
02017 {
02018 Mpeg1Context *s1 = avctx->priv_data;
02019 MpegEncContext *s = &s1->mpeg_enc_ctx;
02020 int i, v;
02021
02022
02023 s->out_format = FMT_MPEG1;
02024 if (s1->mpeg_enc_ctx_allocated) {
02025 MPV_common_end(s);
02026 }
02027 s->width = avctx->coded_width;
02028 s->height = avctx->coded_height;
02029 avctx->has_b_frames = 0;
02030 s->low_delay = 1;
02031
02032 avctx->pix_fmt = mpeg_get_pixelformat(avctx);
02033 avctx->hwaccel = ff_find_hwaccel(avctx->codec->id, avctx->pix_fmt);
02034
02035 if (avctx->pix_fmt == PIX_FMT_XVMC_MPEG2_IDCT || avctx->hwaccel ||
02036 s->avctx->codec->capabilities & CODEC_CAP_HWACCEL_VDPAU)
02037 if (avctx->idct_algo == FF_IDCT_AUTO)
02038 avctx->idct_algo = FF_IDCT_SIMPLE;
02039
02040 if (MPV_common_init(s) < 0)
02041 return -1;
02042 exchange_uv(s);
02043 s->swap_uv = 1;
02044 s1->mpeg_enc_ctx_allocated = 1;
02045
02046 for (i = 0; i < 64; i++) {
02047 int j = s->dsp.idct_permutation[i];
02048 v = ff_mpeg1_default_intra_matrix[i];
02049 s->intra_matrix[j] = v;
02050 s->chroma_intra_matrix[j] = v;
02051
02052 v = ff_mpeg1_default_non_intra_matrix[i];
02053 s->inter_matrix[j] = v;
02054 s->chroma_inter_matrix[j] = v;
02055 }
02056
02057 s->progressive_sequence = 1;
02058 s->progressive_frame = 1;
02059 s->picture_structure = PICT_FRAME;
02060 s->frame_pred_frame_dct = 1;
02061 s->chroma_format = 1;
02062 s->codec_id = s->avctx->codec_id = CODEC_ID_MPEG2VIDEO;
02063 avctx->sub_id = 2;
02064 s1->save_width = s->width;
02065 s1->save_height = s->height;
02066 s1->save_progressive_seq = s->progressive_sequence;
02067 return 0;
02068 }
02069
02070
02071 static void mpeg_decode_user_data(AVCodecContext *avctx,
02072 const uint8_t *p, int buf_size)
02073 {
02074 const uint8_t *buf_end = p + buf_size;
02075
02076
02077 if (buf_end - p >= 5 &&
02078 p[0] == 'D' && p[1] == 'T' && p[2] == 'G' && p[3] == '1') {
02079 int flags = p[4];
02080 p += 5;
02081 if (flags & 0x80) {
02082
02083 p += 2;
02084 }
02085 if (flags & 0x40) {
02086 if (buf_end - p < 1)
02087 return;
02088 avctx->dtg_active_format = p[0] & 0x0f;
02089 }
02090 }
02091 }
02092
02093 static void mpeg_decode_gop(AVCodecContext *avctx,
02094 const uint8_t *buf, int buf_size)
02095 {
02096 Mpeg1Context *s1 = avctx->priv_data;
02097 MpegEncContext *s = &s1->mpeg_enc_ctx;
02098
02099 int time_code_hours, time_code_minutes;
02100 int time_code_seconds, time_code_pictures;
02101 int broken_link;
02102
02103 init_get_bits(&s->gb, buf, buf_size*8);
02104
02105 skip_bits1(&s->gb);
02106
02107 time_code_hours = get_bits(&s->gb, 5);
02108 time_code_minutes = get_bits(&s->gb, 6);
02109 skip_bits1(&s->gb);
02110 time_code_seconds = get_bits(&s->gb, 6);
02111 time_code_pictures = get_bits(&s->gb, 6);
02112
02113 s1->closed_gop = get_bits1(&s->gb);
02114
02115
02116
02117 broken_link = get_bits1(&s->gb);
02118
02119 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
02120 av_log(s->avctx, AV_LOG_DEBUG, "GOP (%2d:%02d:%02d.[%02d]) closed_gop=%d broken_link=%d\n",
02121 time_code_hours, time_code_minutes, time_code_seconds,
02122 time_code_pictures, s1->closed_gop, broken_link);
02123 }
02128 int ff_mpeg1_find_frame_end(ParseContext *pc, const uint8_t *buf, int buf_size, AVCodecParserContext *s)
02129 {
02130 int i;
02131 uint32_t state = pc->state;
02132
02133
02134 if (buf_size == 0)
02135 return 0;
02136
02137
02138
02139
02140
02141
02142
02143
02144
02145 for (i = 0; i < buf_size; i++) {
02146 assert(pc->frame_start_found >= 0 && pc->frame_start_found <= 4);
02147 if (pc->frame_start_found & 1) {
02148 if (state == EXT_START_CODE && (buf[i] & 0xF0) != 0x80)
02149 pc->frame_start_found--;
02150 else if (state == EXT_START_CODE + 2) {
02151 if ((buf[i] & 3) == 3)
02152 pc->frame_start_found = 0;
02153 else
02154 pc->frame_start_found = (pc->frame_start_found + 1) & 3;
02155 }
02156 state++;
02157 } else {
02158 i = avpriv_mpv_find_start_code(buf + i, buf + buf_size, &state) - buf - 1;
02159 if (pc->frame_start_found == 0 && state >= SLICE_MIN_START_CODE && state <= SLICE_MAX_START_CODE) {
02160 i++;
02161 pc->frame_start_found = 4;
02162 }
02163 if (state == SEQ_END_CODE) {
02164 pc->state=-1;
02165 return i+1;
02166 }
02167 if (pc->frame_start_found == 2 && state == SEQ_START_CODE)
02168 pc->frame_start_found = 0;
02169 if (pc->frame_start_found < 4 && state == EXT_START_CODE)
02170 pc->frame_start_found++;
02171 if (pc->frame_start_found == 4 && (state & 0xFFFFFF00) == 0x100) {
02172 if (state < SLICE_MIN_START_CODE || state > SLICE_MAX_START_CODE) {
02173 pc->frame_start_found = 0;
02174 pc->state = -1;
02175 return i - 3;
02176 }
02177 }
02178 if (pc->frame_start_found == 0 && s && state == PICTURE_START_CODE) {
02179 ff_fetch_timestamp(s, i - 3, 1);
02180 }
02181 }
02182 }
02183 pc->state = state;
02184 return END_NOT_FOUND;
02185 }
02186
02187 static int decode_chunks(AVCodecContext *avctx,
02188 AVFrame *picture, int *data_size,
02189 const uint8_t *buf, int buf_size);
02190
02191
02192 static int mpeg_decode_frame(AVCodecContext *avctx,
02193 void *data, int *data_size,
02194 AVPacket *avpkt)
02195 {
02196 const uint8_t *buf = avpkt->data;
02197 int buf_size = avpkt->size;
02198 Mpeg1Context *s = avctx->priv_data;
02199 AVFrame *picture = data;
02200 MpegEncContext *s2 = &s->mpeg_enc_ctx;
02201 av_dlog(avctx, "fill_buffer\n");
02202
02203 if (buf_size == 0 || (buf_size == 4 && AV_RB32(buf) == SEQ_END_CODE)) {
02204
02205 if (s2->low_delay == 0 && s2->next_picture_ptr) {
02206 *picture = *(AVFrame*)s2->next_picture_ptr;
02207 s2->next_picture_ptr = NULL;
02208
02209 *data_size = sizeof(AVFrame);
02210 }
02211 return buf_size;
02212 }
02213
02214 if (s2->flags & CODEC_FLAG_TRUNCATED) {
02215 int next = ff_mpeg1_find_frame_end(&s2->parse_context, buf, buf_size, NULL);
02216
02217 if (ff_combine_frame(&s2->parse_context, next, (const uint8_t **)&buf, &buf_size) < 0)
02218 return buf_size;
02219 }
02220
02221 if (s->mpeg_enc_ctx_allocated == 0 && avctx->codec_tag == AV_RL32("VCR2"))
02222 vcr2_init_sequence(avctx);
02223
02224 s->slice_count = 0;
02225
02226 if (avctx->extradata && !avctx->frame_number) {
02227 int ret = decode_chunks(avctx, picture, data_size, avctx->extradata, avctx->extradata_size);
02228 if (ret < 0 && (avctx->err_recognition & AV_EF_EXPLODE))
02229 return ret;
02230 }
02231
02232 return decode_chunks(avctx, picture, data_size, buf, buf_size);
02233 }
02234
02235 static int decode_chunks(AVCodecContext *avctx,
02236 AVFrame *picture, int *data_size,
02237 const uint8_t *buf, int buf_size)
02238 {
02239 Mpeg1Context *s = avctx->priv_data;
02240 MpegEncContext *s2 = &s->mpeg_enc_ctx;
02241 const uint8_t *buf_ptr = buf;
02242 const uint8_t *buf_end = buf + buf_size;
02243 int ret, input_size;
02244 int last_code = 0;
02245
02246 for (;;) {
02247
02248 uint32_t start_code = -1;
02249 buf_ptr = avpriv_mpv_find_start_code(buf_ptr, buf_end, &start_code);
02250 if (start_code > 0x1ff) {
02251 if (s2->pict_type != AV_PICTURE_TYPE_B || avctx->skip_frame <= AVDISCARD_DEFAULT) {
02252 if (HAVE_THREADS && (avctx->active_thread_type & FF_THREAD_SLICE)) {
02253 int i;
02254
02255 avctx->execute(avctx, slice_decode_thread, &s2->thread_context[0], NULL, s->slice_count, sizeof(void*));
02256 for (i = 0; i < s->slice_count; i++)
02257 s2->error_count += s2->thread_context[i]->error_count;
02258 }
02259
02260 if (CONFIG_MPEG_VDPAU_DECODER && avctx->codec->capabilities & CODEC_CAP_HWACCEL_VDPAU)
02261 ff_vdpau_mpeg_picture_complete(s2, buf, buf_size, s->slice_count);
02262
02263 if (slice_end(avctx, picture)) {
02264 if (s2->last_picture_ptr || s2->low_delay)
02265 *data_size = sizeof(AVPicture);
02266 }
02267 }
02268 s2->pict_type = 0;
02269 return FFMAX(0, buf_ptr - buf - s2->parse_context.last_index);
02270 }
02271
02272 input_size = buf_end - buf_ptr;
02273
02274 if (avctx->debug & FF_DEBUG_STARTCODE) {
02275 av_log(avctx, AV_LOG_DEBUG, "%3X at %td left %d\n", start_code, buf_ptr-buf, input_size);
02276 }
02277
02278
02279 switch (start_code) {
02280 case SEQ_START_CODE:
02281 if (last_code == 0) {
02282 mpeg1_decode_sequence(avctx, buf_ptr, input_size);
02283 s->sync=1;
02284 } else {
02285 av_log(avctx, AV_LOG_ERROR, "ignoring SEQ_START_CODE after %X\n", last_code);
02286 if (avctx->err_recognition & AV_EF_EXPLODE)
02287 return AVERROR_INVALIDDATA;
02288 }
02289 break;
02290
02291 case PICTURE_START_CODE:
02292 if (HAVE_THREADS && (avctx->active_thread_type & FF_THREAD_SLICE) && s->slice_count) {
02293 int i;
02294
02295 avctx->execute(avctx, slice_decode_thread,
02296 s2->thread_context, NULL,
02297 s->slice_count, sizeof(void*));
02298 for (i = 0; i < s->slice_count; i++)
02299 s2->error_count += s2->thread_context[i]->error_count;
02300 s->slice_count = 0;
02301 }
02302 if (last_code == 0 || last_code == SLICE_MIN_START_CODE) {
02303 ret = mpeg_decode_postinit(avctx);
02304 if (ret < 0) {
02305 av_log(avctx, AV_LOG_ERROR, "mpeg_decode_postinit() failure\n");
02306 return ret;
02307 }
02308
02309
02310 if (mpeg1_decode_picture(avctx, buf_ptr, input_size) < 0)
02311 s2->pict_type = 0;
02312 s2->first_slice = 1;
02313 last_code = PICTURE_START_CODE;
02314 } else {
02315 av_log(avctx, AV_LOG_ERROR, "ignoring pic after %X\n", last_code);
02316 if (avctx->err_recognition & AV_EF_EXPLODE)
02317 return AVERROR_INVALIDDATA;
02318 }
02319 break;
02320 case EXT_START_CODE:
02321 init_get_bits(&s2->gb, buf_ptr, input_size*8);
02322
02323 switch (get_bits(&s2->gb, 4)) {
02324 case 0x1:
02325 if (last_code == 0) {
02326 mpeg_decode_sequence_extension(s);
02327 } else {
02328 av_log(avctx, AV_LOG_ERROR, "ignoring seq ext after %X\n", last_code);
02329 if (avctx->err_recognition & AV_EF_EXPLODE)
02330 return AVERROR_INVALIDDATA;
02331 }
02332 break;
02333 case 0x2:
02334 mpeg_decode_sequence_display_extension(s);
02335 break;
02336 case 0x3:
02337 mpeg_decode_quant_matrix_extension(s2);
02338 break;
02339 case 0x7:
02340 mpeg_decode_picture_display_extension(s);
02341 break;
02342 case 0x8:
02343 if (last_code == PICTURE_START_CODE) {
02344 mpeg_decode_picture_coding_extension(s);
02345 } else {
02346 av_log(avctx, AV_LOG_ERROR, "ignoring pic cod ext after %X\n", last_code);
02347 if (avctx->err_recognition & AV_EF_EXPLODE)
02348 return AVERROR_INVALIDDATA;
02349 }
02350 break;
02351 }
02352 break;
02353 case USER_START_CODE:
02354 mpeg_decode_user_data(avctx, buf_ptr, input_size);
02355 break;
02356 case GOP_START_CODE:
02357 if (last_code == 0) {
02358 s2->first_field=0;
02359 mpeg_decode_gop(avctx, buf_ptr, input_size);
02360 s->sync=1;
02361 } else {
02362 av_log(avctx, AV_LOG_ERROR, "ignoring GOP_START_CODE after %X\n", last_code);
02363 if (avctx->err_recognition & AV_EF_EXPLODE)
02364 return AVERROR_INVALIDDATA;
02365 }
02366 break;
02367 default:
02368 if (start_code >= SLICE_MIN_START_CODE &&
02369 start_code <= SLICE_MAX_START_CODE && last_code != 0) {
02370 const int field_pic = s2->picture_structure != PICT_FRAME;
02371 int mb_y = (start_code - SLICE_MIN_START_CODE) << field_pic;
02372 last_code = SLICE_MIN_START_CODE;
02373
02374 if (s2->picture_structure == PICT_BOTTOM_FIELD)
02375 mb_y++;
02376
02377 if (mb_y >= s2->mb_height) {
02378 av_log(s2->avctx, AV_LOG_ERROR, "slice below image (%d >= %d)\n", mb_y, s2->mb_height);
02379 return -1;
02380 }
02381
02382 if (s2->last_picture_ptr == NULL) {
02383
02384 if (s2->pict_type == AV_PICTURE_TYPE_B) {
02385 if (!s->closed_gop)
02386 break;
02387 }
02388 }
02389 if (s2->pict_type == AV_PICTURE_TYPE_I)
02390 s->sync=1;
02391 if (s2->next_picture_ptr == NULL) {
02392
02393 if (s2->pict_type == AV_PICTURE_TYPE_P && !s->sync) break;
02394 }
02395 if ((avctx->skip_frame >= AVDISCARD_NONREF && s2->pict_type == AV_PICTURE_TYPE_B) ||
02396 (avctx->skip_frame >= AVDISCARD_NONKEY && s2->pict_type != AV_PICTURE_TYPE_I) ||
02397 avctx->skip_frame >= AVDISCARD_ALL)
02398 break;
02399
02400 if (!s->mpeg_enc_ctx_allocated)
02401 break;
02402
02403 if (s2->codec_id == CODEC_ID_MPEG2VIDEO) {
02404 if (mb_y < avctx->skip_top || mb_y >= s2->mb_height - avctx->skip_bottom)
02405 break;
02406 }
02407
02408 if (!s2->pict_type) {
02409 av_log(avctx, AV_LOG_ERROR, "Missing picture start code\n");
02410 if (avctx->err_recognition & AV_EF_EXPLODE)
02411 return AVERROR_INVALIDDATA;
02412 break;
02413 }
02414
02415 if (s2->first_slice) {
02416 s2->first_slice = 0;
02417 if (mpeg_field_start(s2, buf, buf_size) < 0)
02418 return -1;
02419 }
02420 if (!s2->current_picture_ptr) {
02421 av_log(avctx, AV_LOG_ERROR, "current_picture not initialized\n");
02422 return AVERROR_INVALIDDATA;
02423 }
02424
02425 if (avctx->codec->capabilities & CODEC_CAP_HWACCEL_VDPAU) {
02426 s->slice_count++;
02427 break;
02428 }
02429
02430 if (HAVE_THREADS && (avctx->active_thread_type & FF_THREAD_SLICE)) {
02431 int threshold = (s2->mb_height * s->slice_count +
02432 s2->slice_context_count / 2) /
02433 s2->slice_context_count;
02434 if (threshold <= mb_y) {
02435 MpegEncContext *thread_context = s2->thread_context[s->slice_count];
02436
02437 thread_context->start_mb_y = mb_y;
02438 thread_context->end_mb_y = s2->mb_height;
02439 if (s->slice_count) {
02440 s2->thread_context[s->slice_count-1]->end_mb_y = mb_y;
02441 ff_update_duplicate_context(thread_context, s2);
02442 }
02443 init_get_bits(&thread_context->gb, buf_ptr, input_size*8);
02444 s->slice_count++;
02445 }
02446 buf_ptr += 2;
02447 } else {
02448 ret = mpeg_decode_slice(s2, mb_y, &buf_ptr, input_size);
02449 emms_c();
02450
02451 if (ret < 0) {
02452 if (avctx->err_recognition & AV_EF_EXPLODE)
02453 return ret;
02454 if (s2->resync_mb_x >= 0 && s2->resync_mb_y >= 0)
02455 ff_er_add_slice(s2, s2->resync_mb_x, s2->resync_mb_y, s2->mb_x, s2->mb_y, ER_AC_ERROR | ER_DC_ERROR | ER_MV_ERROR);
02456 } else {
02457 ff_er_add_slice(s2, s2->resync_mb_x, s2->resync_mb_y, s2->mb_x-1, s2->mb_y, ER_AC_END | ER_DC_END | ER_MV_END);
02458 }
02459 }
02460 }
02461 break;
02462 }
02463 }
02464 }
02465
02466 static void flush(AVCodecContext *avctx)
02467 {
02468 Mpeg1Context *s = avctx->priv_data;
02469
02470 s->sync=0;
02471 s->closed_gop = 0;
02472
02473 ff_mpeg_flush(avctx);
02474 }
02475
02476 static int mpeg_decode_end(AVCodecContext *avctx)
02477 {
02478 Mpeg1Context *s = avctx->priv_data;
02479
02480 if (s->mpeg_enc_ctx_allocated)
02481 MPV_common_end(&s->mpeg_enc_ctx);
02482 return 0;
02483 }
02484
02485 static const AVProfile mpeg2_video_profiles[] = {
02486 { FF_PROFILE_MPEG2_422, "4:2:2" },
02487 { FF_PROFILE_MPEG2_HIGH, "High" },
02488 { FF_PROFILE_MPEG2_SS, "Spatially Scalable" },
02489 { FF_PROFILE_MPEG2_SNR_SCALABLE, "SNR Scalable" },
02490 { FF_PROFILE_MPEG2_MAIN, "Main" },
02491 { FF_PROFILE_MPEG2_SIMPLE, "Simple" },
02492 { FF_PROFILE_RESERVED, "Reserved" },
02493 { FF_PROFILE_RESERVED, "Reserved" },
02494 { FF_PROFILE_UNKNOWN },
02495 };
02496
02497
02498 AVCodec ff_mpeg1video_decoder = {
02499 .name = "mpeg1video",
02500 .type = AVMEDIA_TYPE_VIDEO,
02501 .id = CODEC_ID_MPEG1VIDEO,
02502 .priv_data_size = sizeof(Mpeg1Context),
02503 .init = mpeg_decode_init,
02504 .close = mpeg_decode_end,
02505 .decode = mpeg_decode_frame,
02506 .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY | CODEC_CAP_SLICE_THREADS,
02507 .flush = flush,
02508 .max_lowres = 3,
02509 .long_name = NULL_IF_CONFIG_SMALL("MPEG-1 video"),
02510 .update_thread_context = ONLY_IF_THREADS_ENABLED(mpeg_decode_update_thread_context)
02511 };
02512
02513 AVCodec ff_mpeg2video_decoder = {
02514 .name = "mpeg2video",
02515 .type = AVMEDIA_TYPE_VIDEO,
02516 .id = CODEC_ID_MPEG2VIDEO,
02517 .priv_data_size = sizeof(Mpeg1Context),
02518 .init = mpeg_decode_init,
02519 .close = mpeg_decode_end,
02520 .decode = mpeg_decode_frame,
02521 .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY | CODEC_CAP_SLICE_THREADS,
02522 .flush = flush,
02523 .max_lowres = 3,
02524 .long_name = NULL_IF_CONFIG_SMALL("MPEG-2 video"),
02525 .profiles = NULL_IF_CONFIG_SMALL(mpeg2_video_profiles),
02526 };
02527
02528 #if CONFIG_MPEG_XVMC_DECODER
02529 static av_cold int mpeg_mc_decode_init(AVCodecContext *avctx)
02530 {
02531 if (avctx->active_thread_type & FF_THREAD_SLICE)
02532 return -1;
02533 if (!(avctx->slice_flags & SLICE_FLAG_CODED_ORDER))
02534 return -1;
02535 if (!(avctx->slice_flags & SLICE_FLAG_ALLOW_FIELD)) {
02536 av_dlog(avctx, "mpeg12.c: XvMC decoder will work better if SLICE_FLAG_ALLOW_FIELD is set\n");
02537 }
02538 mpeg_decode_init(avctx);
02539
02540 avctx->pix_fmt = PIX_FMT_XVMC_MPEG2_IDCT;
02541 avctx->xvmc_acceleration = 2;
02542
02543 return 0;
02544 }
02545
02546 AVCodec ff_mpeg_xvmc_decoder = {
02547 .name = "mpegvideo_xvmc",
02548 .type = AVMEDIA_TYPE_VIDEO,
02549 .id = CODEC_ID_MPEG2VIDEO_XVMC,
02550 .priv_data_size = sizeof(Mpeg1Context),
02551 .init = mpeg_mc_decode_init,
02552 .close = mpeg_decode_end,
02553 .decode = mpeg_decode_frame,
02554 .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED| CODEC_CAP_HWACCEL | CODEC_CAP_DELAY,
02555 .flush = flush,
02556 .long_name = NULL_IF_CONFIG_SMALL("MPEG-1/2 video XvMC (X-Video Motion Compensation)"),
02557 };
02558
02559 #endif
02560
02561 #if CONFIG_MPEG_VDPAU_DECODER
02562 AVCodec ff_mpeg_vdpau_decoder = {
02563 .name = "mpegvideo_vdpau",
02564 .type = AVMEDIA_TYPE_VIDEO,
02565 .id = CODEC_ID_MPEG2VIDEO,
02566 .priv_data_size = sizeof(Mpeg1Context),
02567 .init = mpeg_decode_init,
02568 .close = mpeg_decode_end,
02569 .decode = mpeg_decode_frame,
02570 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_HWACCEL_VDPAU | CODEC_CAP_DELAY,
02571 .flush = flush,
02572 .long_name = NULL_IF_CONFIG_SMALL("MPEG-1/2 video (VDPAU acceleration)"),
02573 };
02574 #endif
02575
02576 #if CONFIG_MPEG1_VDPAU_DECODER
02577 AVCodec ff_mpeg1_vdpau_decoder = {
02578 .name = "mpeg1video_vdpau",
02579 .type = AVMEDIA_TYPE_VIDEO,
02580 .id = CODEC_ID_MPEG1VIDEO,
02581 .priv_data_size = sizeof(Mpeg1Context),
02582 .init = mpeg_decode_init,
02583 .close = mpeg_decode_end,
02584 .decode = mpeg_decode_frame,
02585 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_HWACCEL_VDPAU | CODEC_CAP_DELAY,
02586 .flush = flush,
02587 .long_name = NULL_IF_CONFIG_SMALL("MPEG-1 video (VDPAU acceleration)"),
02588 };
02589 #endif
02590