840
if (s->codec_id == CODEC_ID_MPEG2VIDEO) {
840
if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
841
841
if (s->flags2 & CODEC_FLAG2_FAST) {
842
842
for (i = 0; i < 6; i++) {
843
843
mpeg2_fast_decode_block_intra(s, *s->pblocks[i], i);
1130
1130
s->repeat_field = 0;
1131
1131
s->mpeg_enc_ctx.codec_id = avctx->codec->id;
1132
1132
avctx->color_range = AVCOL_RANGE_MPEG;
1133
if (avctx->codec->id == CODEC_ID_MPEG1VIDEO)
1133
if (avctx->codec->id == AV_CODEC_ID_MPEG1VIDEO)
1134
1134
avctx->chroma_sample_location = AVCHROMA_LOC_CENTER;
1136
1136
avctx->chroma_sample_location = AVCHROMA_LOC_LEFT;
1174
static const enum PixelFormat pixfmt_xvmc_mpg2_420[] = {
1175
PIX_FMT_XVMC_MPEG2_IDCT,
1176
PIX_FMT_XVMC_MPEG2_MC,
1174
static const enum AVPixelFormat pixfmt_xvmc_mpg2_420[] = {
1175
AV_PIX_FMT_XVMC_MPEG2_IDCT,
1176
AV_PIX_FMT_XVMC_MPEG2_MC,
1179
static enum PixelFormat mpeg_get_pixelformat(AVCodecContext *avctx)
1179
static enum AVPixelFormat mpeg_get_pixelformat(AVCodecContext *avctx)
1181
1181
Mpeg1Context *s1 = avctx->priv_data;
1182
1182
MpegEncContext *s = &s1->mpeg_enc_ctx;
1184
1184
if (avctx->xvmc_acceleration)
1185
1185
return avctx->get_format(avctx, pixfmt_xvmc_mpg2_420);
1186
1186
else if (avctx->codec->capabilities & CODEC_CAP_HWACCEL_VDPAU) {
1187
if (avctx->codec_id == CODEC_ID_MPEG1VIDEO)
1188
return PIX_FMT_VDPAU_MPEG1;
1187
if (avctx->codec_id == AV_CODEC_ID_MPEG1VIDEO)
1188
return AV_PIX_FMT_VDPAU_MPEG1;
1190
return PIX_FMT_VDPAU_MPEG2;
1190
return AV_PIX_FMT_VDPAU_MPEG2;
1192
1192
if (s->chroma_format < 2)
1193
1193
return avctx->get_format(avctx, ff_hwaccel_pixfmt_list_420);
1194
1194
else if (s->chroma_format == 2)
1195
return PIX_FMT_YUV422P;
1195
return AV_PIX_FMT_YUV422P;
1197
return PIX_FMT_YUV444P;
1197
return AV_PIX_FMT_YUV444P;
1237
1237
* that behave like P-frames. */
1238
1238
avctx->has_b_frames = !s->low_delay;
1240
assert((avctx->sub_id == 1) == (avctx->codec_id == CODEC_ID_MPEG1VIDEO));
1241
if (avctx->codec_id == CODEC_ID_MPEG1VIDEO) {
1240
if (avctx->codec_id == AV_CODEC_ID_MPEG1VIDEO) {
1243
avctx->time_base.den = avpriv_frame_rate_tab[s->frame_rate_index].num;
1244
avctx->time_base.num = avpriv_frame_rate_tab[s->frame_rate_index].den;
1242
avctx->time_base.den = ff_mpeg12_frame_rate_tab[s->frame_rate_index].num;
1243
avctx->time_base.num = ff_mpeg12_frame_rate_tab[s->frame_rate_index].den;
1245
1244
//MPEG-1 aspect
1246
1245
avctx->sample_aspect_ratio = av_d2q(1.0/ff_mpeg1_aspect[s->aspect_ratio_info], 255);
1247
1246
avctx->ticks_per_frame=1;
1250
1249
av_reduce(&s->avctx->time_base.den,
1251
1250
&s->avctx->time_base.num,
1252
avpriv_frame_rate_tab[s->frame_rate_index].num * s1->frame_rate_ext.num*2,
1253
avpriv_frame_rate_tab[s->frame_rate_index].den * s1->frame_rate_ext.den,
1251
ff_mpeg12_frame_rate_tab[s->frame_rate_index].num * s1->frame_rate_ext.num*2,
1252
ff_mpeg12_frame_rate_tab[s->frame_rate_index].den * s1->frame_rate_ext.den,
1255
1254
avctx->ticks_per_frame = 2;
1256
1255
//MPEG-2 aspect
1276
1275
//res_change_ffmpeg_aspect.ts 4/3 225/44 ->4/3
1277
1276
//widescreen-issue562.mpg 4/3 16/9 -> 16/9
1278
1277
// s->avctx->sample_aspect_ratio = av_mul_q(s->avctx->sample_aspect_ratio, (AVRational) {s->width, s->height});
1279
//av_log(NULL, AV_LOG_ERROR, "A %d/%d\n", ff_mpeg2_aspect[s->aspect_ratio_info].num, ff_mpeg2_aspect[s->aspect_ratio_info].den);
1280
//av_log(NULL, AV_LOG_ERROR, "B %d/%d\n", s->avctx->sample_aspect_ratio.num, s->avctx->sample_aspect_ratio.den);
1278
av_dlog(avctx, "A %d/%d\n",
1279
ff_mpeg2_aspect[s->aspect_ratio_info].num, ff_mpeg2_aspect[s->aspect_ratio_info].den);
1280
av_dlog(avctx, "B %d/%d\n", s->avctx->sample_aspect_ratio.num,
1281
s->avctx->sample_aspect_ratio.den);
1283
1284
s->avctx->sample_aspect_ratio =
1288
1289
avctx->pix_fmt = mpeg_get_pixelformat(avctx);
1289
1290
avctx->hwaccel = ff_find_hwaccel(avctx->codec->id, avctx->pix_fmt);
1290
1291
// until then pix_fmt may be changed right after codec init
1291
if (avctx->pix_fmt == PIX_FMT_XVMC_MPEG2_IDCT ||
1292
if (avctx->pix_fmt == AV_PIX_FMT_XVMC_MPEG2_IDCT ||
1292
1293
avctx->hwaccel ||
1293
1294
s->avctx->codec->capabilities & CODEC_CAP_HWACCEL_VDPAU)
1294
1295
if (avctx->idct_algo == FF_IDCT_AUTO)
1298
1299
* if DCT permutation is changed. */
1299
1300
memcpy(old_permutation, s->dsp.idct_permutation, 64 * sizeof(uint8_t));
1301
if (MPV_common_init(s) < 0)
1302
if (ff_MPV_common_init(s) < 0)
1304
1305
quant_matrix_rebuild(s->intra_matrix, old_permutation, s->dsp.idct_permutation);
1381
1382
s1->frame_rate_ext.den = get_bits(&s->gb, 5) + 1;
1383
1384
av_dlog(s->avctx, "sequence extension\n");
1384
s->codec_id = s->avctx->codec_id = CODEC_ID_MPEG2VIDEO;
1385
s->avctx->sub_id = 2; /* indicates MPEG-2 found */
1385
s->codec_id = s->avctx->codec_id = AV_CODEC_ID_MPEG2VIDEO;
1387
1387
if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1388
1388
av_log(s->avctx, AV_LOG_DEBUG, "profile: %d, level: %d vbv buffer: %d, bitrate:%d\n",
1525
1525
if (s->progressive_sequence && !s->frame_pred_frame_dct) {
1526
av_log(s->avctx, AV_LOG_ERROR, "invalid frame_pred_frame_dct\n");
1527
s->frame_pred_frame_dct = 1;
1526
av_log(s->avctx, AV_LOG_WARNING, "invalid frame_pred_frame_dct\n");
1530
1529
if (s->picture_structure == PICT_FRAME) {
1595
if (s->avctx->hwaccel &&
1596
(s->avctx->slice_flags & SLICE_FLAG_ALLOW_FIELD)) {
1597
if (s->avctx->hwaccel->end_frame(s->avctx) < 0)
1598
av_log(avctx, AV_LOG_ERROR, "hardware accelerator failed to decode first field\n");
1596
1601
for (i = 0; i < 4; i++) {
1597
1602
s->current_picture.f.data[i] = s->current_picture_ptr->f.data[i];
1598
1603
if (s->picture_structure == PICT_BOTTOM_FIELD) {
1752
s->dest[0] += 16 >> lowres;
1753
s->dest[1] +=(16 >> lowres) >> s->chroma_x_shift;
1754
s->dest[2] +=(16 >> lowres) >> s->chroma_x_shift;
1757
s->dest[1] += 16 >> s->chroma_x_shift;
1758
s->dest[2] += 16 >> s->chroma_x_shift;
1756
MPV_decode_mb(s, s->block);
1760
ff_MPV_decode_mb(s, s->block);
1758
1762
if (++s->mb_x >= s->mb_width) {
1759
const int mb_size = 16 >> s->avctx->lowres;
1763
const int mb_size = 16;
1761
1765
ff_draw_horiz_band(s, mb_size*(s->mb_y >> field_pic), mb_size);
1762
MPV_report_decode_progress(s);
1766
ff_MPV_report_decode_progress(s);
1765
1769
s->mb_y += 1 << field_pic;
1842
1846
eos: // end of slice
1843
1847
*buf += (get_bits_count(&s->gb)-1)/8;
1844
//printf("y %d %d %d %d\n", s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y);
1848
av_dlog(s, "y %d %d %d %d\n", s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y);
1861
1865
ret = mpeg_decode_slice(s, mb_y, &buf, s->gb.buffer_end - buf);
1863
//av_log(c, AV_LOG_DEBUG, "ret:%d resync:%d/%d mb:%d/%d ts:%d/%d ec:%d\n",
1864
//ret, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, s->start_mb_y, s->end_mb_y, s->error_count);
1867
av_dlog(c, "ret:%d resync:%d/%d mb:%d/%d ts:%d/%d ec:%d\n",
1868
ret, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y,
1869
s->start_mb_y, s->end_mb_y, s->error_count);
1866
1871
if (c->err_recognition & AV_EF_EXPLODE)
1923
1928
/* latency of 1 frame for I- and P-frames */
1924
1929
/* XXX: use another variable than picture_number */
1925
1930
if (s->last_picture_ptr != NULL) {
1926
*pict = *(AVFrame*)s->last_picture_ptr;
1931
*pict = s->last_picture_ptr->f;
1927
1932
ff_print_debug_info(s, pict);
1999
2004
s->picture_structure = PICT_FRAME;
2000
2005
s->frame_pred_frame_dct = 1;
2001
2006
s->chroma_format = 1;
2002
s->codec_id = s->avctx->codec_id = CODEC_ID_MPEG1VIDEO;
2003
avctx->sub_id = 1; /* indicates MPEG-1 */
2007
s->codec_id = s->avctx->codec_id = AV_CODEC_ID_MPEG1VIDEO;
2004
2008
s->out_format = FMT_MPEG1;
2005
2009
s->swap_uv = 0; // AFAIK VCR2 does not have SEQ_HEADER
2006
2010
if (s->flags & CODEC_FLAG_LOW_DELAY)
2032
2036
avctx->pix_fmt = mpeg_get_pixelformat(avctx);
2033
2037
avctx->hwaccel = ff_find_hwaccel(avctx->codec->id, avctx->pix_fmt);
2035
if (avctx->pix_fmt == PIX_FMT_XVMC_MPEG2_IDCT || avctx->hwaccel ||
2039
if (avctx->pix_fmt == AV_PIX_FMT_XVMC_MPEG2_IDCT || avctx->hwaccel ||
2036
2040
s->avctx->codec->capabilities & CODEC_CAP_HWACCEL_VDPAU)
2037
2041
if (avctx->idct_algo == FF_IDCT_AUTO)
2038
2042
avctx->idct_algo = FF_IDCT_SIMPLE;
2040
if (MPV_common_init(s) < 0)
2044
if (ff_MPV_common_init(s) < 0)
2042
2046
exchange_uv(s); // common init reset pblocks, so we swap them here
2043
2047
s->swap_uv = 1; // in case of xvmc we need to swap uv for each MB
2059
2063
s->picture_structure = PICT_FRAME;
2060
2064
s->frame_pred_frame_dct = 1;
2061
2065
s->chroma_format = 1;
2062
s->codec_id = s->avctx->codec_id = CODEC_ID_MPEG2VIDEO;
2063
avctx->sub_id = 2; /* indicates MPEG-2 */
2066
s->codec_id = s->avctx->codec_id = AV_CODEC_ID_MPEG2VIDEO;
2064
2067
s1->save_width = s->width;
2065
2068
s1->save_height = s->height;
2066
2069
s1->save_progressive_seq = s->progressive_sequence;
2187
2191
static int decode_chunks(AVCodecContext *avctx,
2188
AVFrame *picture, int *data_size,
2189
const uint8_t *buf, int buf_size);
2191
/* handle buffering and image synchronisation */
2192
static int mpeg_decode_frame(AVCodecContext *avctx,
2193
void *data, int *data_size,
2196
const uint8_t *buf = avpkt->data;
2197
int buf_size = avpkt->size;
2198
Mpeg1Context *s = avctx->priv_data;
2199
AVFrame *picture = data;
2200
MpegEncContext *s2 = &s->mpeg_enc_ctx;
2201
av_dlog(avctx, "fill_buffer\n");
2203
if (buf_size == 0 || (buf_size == 4 && AV_RB32(buf) == SEQ_END_CODE)) {
2204
/* special case for last picture */
2205
if (s2->low_delay == 0 && s2->next_picture_ptr) {
2206
*picture = *(AVFrame*)s2->next_picture_ptr;
2207
s2->next_picture_ptr = NULL;
2209
*data_size = sizeof(AVFrame);
2214
if (s2->flags & CODEC_FLAG_TRUNCATED) {
2215
int next = ff_mpeg1_find_frame_end(&s2->parse_context, buf, buf_size, NULL);
2217
if (ff_combine_frame(&s2->parse_context, next, (const uint8_t **)&buf, &buf_size) < 0)
2221
if (s->mpeg_enc_ctx_allocated == 0 && avctx->codec_tag == AV_RL32("VCR2"))
2222
vcr2_init_sequence(avctx);
2226
if (avctx->extradata && !s->extradata_decoded) {
2227
int ret = decode_chunks(avctx, picture, data_size, avctx->extradata, avctx->extradata_size);
2228
s->extradata_decoded = 1;
2229
if (ret < 0 && (avctx->err_recognition & AV_EF_EXPLODE))
2233
return decode_chunks(avctx, picture, data_size, buf, buf_size);
2236
static int decode_chunks(AVCodecContext *avctx,
2237
AVFrame *picture, int *data_size,
2192
AVFrame *picture, int *got_output,
2238
2193
const uint8_t *buf, int buf_size)
2240
2195
Mpeg1Context *s = avctx->priv_data;
2439
2394
thread_context->end_mb_y = s2->mb_height;
2440
2395
if (s->slice_count) {
2441
2396
s2->thread_context[s->slice_count-1]->end_mb_y = mb_y;
2442
ff_update_duplicate_context(thread_context, s2);
2397
ret = ff_update_duplicate_context(thread_context,
2444
2402
init_get_bits(&thread_context->gb, buf_ptr, input_size*8);
2445
2403
s->slice_count++;
2425
static int mpeg_decode_frame(AVCodecContext *avctx,
2426
void *data, int *got_output,
2429
const uint8_t *buf = avpkt->data;
2430
int buf_size = avpkt->size;
2431
Mpeg1Context *s = avctx->priv_data;
2432
AVFrame *picture = data;
2433
MpegEncContext *s2 = &s->mpeg_enc_ctx;
2434
av_dlog(avctx, "fill_buffer\n");
2436
if (buf_size == 0 || (buf_size == 4 && AV_RB32(buf) == SEQ_END_CODE)) {
2437
/* special case for last picture */
2438
if (s2->low_delay == 0 && s2->next_picture_ptr) {
2439
*picture = s2->next_picture_ptr->f;
2440
s2->next_picture_ptr = NULL;
2447
if (s2->flags & CODEC_FLAG_TRUNCATED) {
2448
int next = ff_mpeg1_find_frame_end(&s2->parse_context, buf, buf_size, NULL);
2450
if (ff_combine_frame(&s2->parse_context, next, (const uint8_t **)&buf, &buf_size) < 0)
2454
if (s->mpeg_enc_ctx_allocated == 0 && avctx->codec_tag == AV_RL32("VCR2"))
2455
vcr2_init_sequence(avctx);
2459
if (avctx->extradata && !s->extradata_decoded) {
2460
int ret = decode_chunks(avctx, picture, got_output, avctx->extradata, avctx->extradata_size);
2461
s->extradata_decoded = 1;
2462
if (ret < 0 && (avctx->err_recognition & AV_EF_EXPLODE))
2466
return decode_chunks(avctx, picture, got_output, buf, buf_size);
2467
2470
static void flush(AVCodecContext *avctx)
2469
2472
Mpeg1Context *s = avctx->priv_data;
2499
2502
AVCodec ff_mpeg1video_decoder = {
2500
.name = "mpeg1video",
2501
.type = AVMEDIA_TYPE_VIDEO,
2502
.id = CODEC_ID_MPEG1VIDEO,
2503
.priv_data_size = sizeof(Mpeg1Context),
2504
.init = mpeg_decode_init,
2505
.close = mpeg_decode_end,
2506
.decode = mpeg_decode_frame,
2507
.capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY | CODEC_CAP_SLICE_THREADS,
2510
.long_name = NULL_IF_CONFIG_SMALL("MPEG-1 video"),
2503
.name = "mpeg1video",
2504
.type = AVMEDIA_TYPE_VIDEO,
2505
.id = AV_CODEC_ID_MPEG1VIDEO,
2506
.priv_data_size = sizeof(Mpeg1Context),
2507
.init = mpeg_decode_init,
2508
.close = mpeg_decode_end,
2509
.decode = mpeg_decode_frame,
2510
.capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 |
2511
CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY |
2512
CODEC_CAP_SLICE_THREADS,
2514
.long_name = NULL_IF_CONFIG_SMALL("MPEG-1 video"),
2511
2515
.update_thread_context = ONLY_IF_THREADS_ENABLED(mpeg_decode_update_thread_context)
2514
2518
AVCodec ff_mpeg2video_decoder = {
2515
2519
.name = "mpeg2video",
2516
2520
.type = AVMEDIA_TYPE_VIDEO,
2517
.id = CODEC_ID_MPEG2VIDEO,
2521
.id = AV_CODEC_ID_MPEG2VIDEO,
2518
2522
.priv_data_size = sizeof(Mpeg1Context),
2519
2523
.init = mpeg_decode_init,
2520
2524
.close = mpeg_decode_end,
2521
2525
.decode = mpeg_decode_frame,
2522
.capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY | CODEC_CAP_SLICE_THREADS,
2526
.capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 |
2527
CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY |
2528
CODEC_CAP_SLICE_THREADS,
2523
2529
.flush = flush,
2525
2530
.long_name = NULL_IF_CONFIG_SMALL("MPEG-2 video"),
2526
2531
.profiles = NULL_IF_CONFIG_SMALL(mpeg2_video_profiles),
2547
2552
AVCodec ff_mpeg_xvmc_decoder = {
2548
2553
.name = "mpegvideo_xvmc",
2549
2554
.type = AVMEDIA_TYPE_VIDEO,
2550
.id = CODEC_ID_MPEG2VIDEO_XVMC,
2555
.id = AV_CODEC_ID_MPEG2VIDEO_XVMC,
2551
2556
.priv_data_size = sizeof(Mpeg1Context),
2552
2557
.init = mpeg_mc_decode_init,
2553
2558
.close = mpeg_decode_end,
2554
2559
.decode = mpeg_decode_frame,
2555
.capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED| CODEC_CAP_HWACCEL | CODEC_CAP_DELAY,
2560
.capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 |
2561
CODEC_CAP_TRUNCATED| CODEC_CAP_HWACCEL | CODEC_CAP_DELAY,
2556
2562
.flush = flush,
2557
2563
.long_name = NULL_IF_CONFIG_SMALL("MPEG-1/2 video XvMC (X-Video Motion Compensation)"),
2563
2569
AVCodec ff_mpeg_vdpau_decoder = {
2564
2570
.name = "mpegvideo_vdpau",
2565
2571
.type = AVMEDIA_TYPE_VIDEO,
2566
.id = CODEC_ID_MPEG2VIDEO,
2572
.id = AV_CODEC_ID_MPEG2VIDEO,
2567
2573
.priv_data_size = sizeof(Mpeg1Context),
2568
2574
.init = mpeg_decode_init,
2569
2575
.close = mpeg_decode_end,
2570
2576
.decode = mpeg_decode_frame,
2571
.capabilities = CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_HWACCEL_VDPAU | CODEC_CAP_DELAY,
2577
.capabilities = CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED |
2578
CODEC_CAP_HWACCEL_VDPAU | CODEC_CAP_DELAY,
2572
2579
.flush = flush,
2573
2580
.long_name = NULL_IF_CONFIG_SMALL("MPEG-1/2 video (VDPAU acceleration)"),
2578
2585
AVCodec ff_mpeg1_vdpau_decoder = {
2579
2586
.name = "mpeg1video_vdpau",
2580
2587
.type = AVMEDIA_TYPE_VIDEO,
2581
.id = CODEC_ID_MPEG1VIDEO,
2588
.id = AV_CODEC_ID_MPEG1VIDEO,
2582
2589
.priv_data_size = sizeof(Mpeg1Context),
2583
2590
.init = mpeg_decode_init,
2584
2591
.close = mpeg_decode_end,
2585
2592
.decode = mpeg_decode_frame,
2586
.capabilities = CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_HWACCEL_VDPAU | CODEC_CAP_DELAY,
2593
.capabilities = CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED |
2594
CODEC_CAP_HWACCEL_VDPAU | CODEC_CAP_DELAY,
2587
2595
.flush = flush,
2588
2596
.long_name = NULL_IF_CONFIG_SMALL("MPEG-1 video (VDPAU acceleration)"),