3
3
* Copyright (c) 2000,2001 Fabrice Bellard
4
4
* Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
6
* This file is part of FFmpeg.
6
* This file is part of Libav.
8
* FFmpeg is free software; you can redistribute it and/or
8
* Libav is free software; you can redistribute it and/or
9
9
* modify it under the terms of the GNU Lesser General Public
10
10
* License as published by the Free Software Foundation; either
11
11
* version 2.1 of the License, or (at your option) any later version.
13
* FFmpeg is distributed in the hope that it will be useful,
13
* Libav is distributed in the hope that it will be useful,
14
14
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15
15
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16
16
* Lesser General Public License for more details.
18
18
* You should have received a copy of the GNU Lesser General Public
19
* License along with FFmpeg; if not, write to the Free Software
19
* License along with Libav; if not, write to the Free Software
20
20
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
153
INIT_VLC_STATIC(&dc_lum_vlc, DC_VLC_BITS, 12,
156
INIT_VLC_STATIC(&ff_dc_lum_vlc, DC_VLC_BITS, 12,
154
157
ff_mpeg12_vlc_dc_lum_bits, 1, 1,
155
158
ff_mpeg12_vlc_dc_lum_code, 2, 2, 512);
156
INIT_VLC_STATIC(&dc_chroma_vlc, DC_VLC_BITS, 12,
159
INIT_VLC_STATIC(&ff_dc_chroma_vlc, DC_VLC_BITS, 12,
157
160
ff_mpeg12_vlc_dc_chroma_bits, 1, 1,
158
161
ff_mpeg12_vlc_dc_chroma_code, 2, 2, 514);
159
162
INIT_VLC_STATIC(&mv_vlc, MV_VLC_BITS, 17,
210
213
int i, j, k, cbp, val, mb_type, motion_type;
211
214
const int mb_block_count = 4 + (1<< s->chroma_format);
213
dprintf(s->avctx, "decode_mb: x=%d y=%d\n", s->mb_x, s->mb_y);
216
av_dlog(s->avctx, "decode_mb: x=%d y=%d\n", s->mb_x, s->mb_y);
215
218
assert(s->mb_skipped==0);
269
272
mb_type = btype2mb_type[ mb_type ];
272
dprintf(s->avctx, "mb_type=%x\n", mb_type);
275
av_dlog(s->avctx, "mb_type=%x\n", mb_type);
273
276
// motion_type = 0; /* avoid warning */
274
277
if (IS_INTRA(mb_type)) {
275
278
s->dsp.clear_blocks(s->block[0]);
368
371
/* motion vectors */
369
372
s->mv_dir= (mb_type>>13)&3;
370
dprintf(s->avctx, "motion_type=%d\n", motion_type);
373
av_dlog(s->avctx, "motion_type=%d\n", motion_type);
371
374
switch(motion_type) {
372
375
case MT_FRAME: /* or MT_16X8 */
373
376
if (s->picture_structure == PICT_FRAME) {
418
421
s->last_mv[i][j][0]);
419
422
s->last_mv[i][j][0] = val;
420
423
s->mv[i][j][0] = val;
421
dprintf(s->avctx, "fmx=%d\n", val);
424
av_dlog(s->avctx, "fmx=%d\n", val);
422
425
val = mpeg_decode_motion(s, s->mpeg_f_code[i][1],
423
426
s->last_mv[i][j][1] >> 1);
424
427
s->last_mv[i][j][1] = val << 1;
425
428
s->mv[i][j][1] = val;
426
dprintf(s->avctx, "fmy=%d\n", val);
429
av_dlog(s->avctx, "fmy=%d\n", val);
630
633
s->last_dc[component] = dc;
631
634
block[0] = dc*quant_matrix[0];
632
dprintf(s->avctx, "dc=%d diff=%d\n", dc, diff);
635
av_dlog(s->avctx, "dc=%d diff=%d\n", dc, diff);
635
638
OPEN_READER(re, &s->gb);
715
718
if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
718
#if MIN_CACHE_BITS < 19
719
UPDATE_CACHE(re, &s->gb);
721
721
/* now quantify & encode AC coefficients */
723
723
GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
759
759
block[j] = level;
760
#if MIN_CACHE_BITS < 19
761
UPDATE_CACHE(re, &s->gb);
763
760
if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
765
#if MIN_CACHE_BITS >= 19
766
762
UPDATE_CACHE(re, &s->gb);
770
765
LAST_SKIP_BITS(re, &s->gb, 2);
838
830
block[j] = level;
839
#if MIN_CACHE_BITS < 19
840
UPDATE_CACHE(re, &s->gb);
842
831
if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
844
#if MIN_CACHE_BITS >= 19
845
833
UPDATE_CACHE(re, &s->gb);
849
836
LAST_SKIP_BITS(re, &s->gb, 2);
925
909
mismatch ^= level;
926
910
block[j] = level;
927
#if MIN_CACHE_BITS < 19
928
UPDATE_CACHE(re, &s->gb);
930
911
if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
932
#if MIN_CACHE_BITS >= 19
933
913
UPDATE_CACHE(re, &s->gb);
937
916
LAST_SKIP_BITS(re, &s->gb, 2);
999
975
block[j] = level;
1000
#if MIN_CACHE_BITS < 19
1001
UPDATE_CACHE(re, &s->gb);
1003
976
if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
1005
#if MIN_CACHE_BITS >=19
1006
978
UPDATE_CACHE(re, &s->gb);
1010
981
LAST_SKIP_BITS(re, &s->gb, 2);
1042
1013
s->last_dc[component] = dc;
1043
1014
block[0] = dc << (3 - s->intra_dc_precision);
1044
dprintf(s->avctx, "dc=%d\n", block[0]);
1015
av_dlog(s->avctx, "dc=%d\n", block[0]);
1045
1016
mismatch = block[0] ^ 1;
1047
1018
if (s->intra_vlc_format)
1168
1139
MpegEncContext mpeg_enc_ctx;
1169
1140
int mpeg_enc_ctx_allocated; /* true if decoding context allocated */
1170
1141
int repeat_field; /* true if we must repeat the field */
1171
AVPanScan pan_scan; /** some temporary storage for the panscan */
1142
AVPanScan pan_scan; /**< some temporary storage for the panscan */
1172
1143
int slice_count;
1173
1144
int swap_uv;//indicate VCR2
1174
1145
int save_aspect_info;
1415
1386
s1->frame_rate_ext.num = get_bits(&s->gb, 2)+1;
1416
1387
s1->frame_rate_ext.den = get_bits(&s->gb, 5)+1;
1418
dprintf(s->avctx, "sequence extension\n");
1389
av_dlog(s->avctx, "sequence extension\n");
1419
1390
s->codec_id= s->avctx->codec_id= CODEC_ID_MPEG2VIDEO;
1420
1391
s->avctx->sub_id = 2; /* indicates MPEG-2 found */
1440
1411
w= get_bits(&s->gb, 14);
1441
1412
skip_bits(&s->gb, 1); //marker
1442
1413
h= get_bits(&s->gb, 14);
1443
skip_bits(&s->gb, 1); //marker
1414
// remaining 3 bits are zero padding
1445
1416
s1->pan_scan.width= 16*w;
1446
1417
s1->pan_scan.height=16*h;
1507
1478
static void mpeg_decode_quant_matrix_extension(MpegEncContext *s)
1509
dprintf(s->avctx, "matrix extension\n");
1480
av_dlog(s->avctx, "matrix extension\n");
1511
1482
if(get_bits1(&s->gb)) load_matrix(s, s->chroma_intra_matrix, s->intra_matrix, 1);
1512
1483
if(get_bits1(&s->gb)) load_matrix(s, s->chroma_inter_matrix, s->inter_matrix, 0);
1582
1553
/* composite display not parsed */
1583
dprintf(s->avctx, "intra_dc_precision=%d\n", s->intra_dc_precision);
1584
dprintf(s->avctx, "picture_structure=%d\n", s->picture_structure);
1585
dprintf(s->avctx, "top field first=%d\n", s->top_field_first);
1586
dprintf(s->avctx, "repeat first field=%d\n", s->repeat_first_field);
1587
dprintf(s->avctx, "conceal=%d\n", s->concealment_motion_vectors);
1588
dprintf(s->avctx, "intra_vlc_format=%d\n", s->intra_vlc_format);
1589
dprintf(s->avctx, "alternate_scan=%d\n", s->alternate_scan);
1590
dprintf(s->avctx, "frame_pred_frame_dct=%d\n", s->frame_pred_frame_dct);
1591
dprintf(s->avctx, "progressive_frame=%d\n", s->progressive_frame);
1554
av_dlog(s->avctx, "intra_dc_precision=%d\n", s->intra_dc_precision);
1555
av_dlog(s->avctx, "picture_structure=%d\n", s->picture_structure);
1556
av_dlog(s->avctx, "top field first=%d\n", s->top_field_first);
1557
av_dlog(s->avctx, "repeat first field=%d\n", s->repeat_first_field);
1558
av_dlog(s->avctx, "conceal=%d\n", s->concealment_motion_vectors);
1559
av_dlog(s->avctx, "intra_vlc_format=%d\n", s->intra_vlc_format);
1560
av_dlog(s->avctx, "alternate_scan=%d\n", s->alternate_scan);
1561
av_dlog(s->avctx, "frame_pred_frame_dct=%d\n", s->frame_pred_frame_dct);
1562
av_dlog(s->avctx, "progressive_frame=%d\n", s->progressive_frame);
1594
1565
static void exchange_uv(MpegEncContext *s){
1912
1883
start_code= -1;
1913
1884
buf = ff_find_start_code(buf, s->gb.buffer_end, &start_code);
1914
mb_y= start_code - SLICE_MIN_START_CODE;
1885
mb_y= (start_code - SLICE_MIN_START_CODE) << field_pic;
1886
if (s->picture_structure == PICT_BOTTOM_FIELD)
1915
1888
if(mb_y < 0 || mb_y >= s->end_mb_y)
2155
2128
time_code_pictures, s->closed_gop, broken_link);
2158
* Finds the end of the current frame in the bitstream.
2131
* Find the end of the current frame in the bitstream.
2159
2132
* @return the position of the first byte of the next frame, or -1
2161
2134
int ff_mpeg1_find_frame_end(ParseContext *pc, const uint8_t *buf, int buf_size, AVCodecParserContext *s)
2229
2202
Mpeg1Context *s = avctx->priv_data;
2230
2203
AVFrame *picture = data;
2231
2204
MpegEncContext *s2 = &s->mpeg_enc_ctx;
2232
dprintf(avctx, "fill_buffer\n");
2205
av_dlog(avctx, "fill_buffer\n");
2234
2207
if (buf_size == 0 || (buf_size == 4 && AV_RB32(buf) == SEQ_END_CODE)) {
2235
2208
/* special case for last picture */
2330
2303
case PICTURE_START_CODE:
2304
if (HAVE_THREADS && (avctx->active_thread_type&FF_THREAD_SLICE) && s->slice_count) {
2307
avctx->execute(avctx, slice_decode_thread,
2308
s2->thread_context, NULL,
2309
s->slice_count, sizeof(void*));
2310
for (i = 0; i < s->slice_count; i++)
2311
s2->error_count += s2->thread_context[i]->error_count;
2331
2314
if(last_code == 0 || last_code == SLICE_MIN_START_CODE){
2332
2315
if(mpeg_decode_postinit(avctx) < 0){
2333
2316
av_log(avctx, AV_LOG_ERROR, "mpeg_decode_postinit() failure\n");
2415
2398
/* Skip P-frames if we do not have a reference frame or we have an invalid header. */
2416
2399
if(s2->pict_type==FF_P_TYPE && !s->sync) break;
2418
2402
/* Skip B-frames if we are in a hurry. */
2419
2403
if(avctx->hurry_up && s2->pict_type==FF_B_TYPE) break;
2420
2405
if( (avctx->skip_frame >= AVDISCARD_NONREF && s2->pict_type==FF_B_TYPE)
2421
2406
||(avctx->skip_frame >= AVDISCARD_NONKEY && s2->pict_type!=FF_I_TYPE)
2422
2407
|| avctx->skip_frame >= AVDISCARD_ALL)
2424
2410
/* Skip everything if we are in a hurry>=5. */
2425
2411
if(avctx->hurry_up>=5) break;
2427
2414
if (!s->mpeg_enc_ctx_allocated) break;
2503
AVCodec mpeg1video_decoder = {
2490
static const AVProfile mpeg2_video_profiles[] = {
2491
{ FF_PROFILE_MPEG2_422, "4:2:2" },
2492
{ FF_PROFILE_MPEG2_HIGH, "High" },
2493
{ FF_PROFILE_MPEG2_SS, "Spatially Scalable" },
2494
{ FF_PROFILE_MPEG2_SNR_SCALABLE, "SNR Scalable" },
2495
{ FF_PROFILE_MPEG2_MAIN, "Main" },
2496
{ FF_PROFILE_MPEG2_SIMPLE, "Simple" },
2497
{ FF_PROFILE_RESERVED, "Reserved" },
2498
{ FF_PROFILE_RESERVED, "Reserved" },
2502
AVCodec ff_mpeg1video_decoder = {
2505
2504
AVMEDIA_TYPE_VIDEO,
2506
2505
CODEC_ID_MPEG1VIDEO,
2511
2510
mpeg_decode_frame,
2512
2511
CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY,
2514
2514
.long_name= NULL_IF_CONFIG_SMALL("MPEG-1 video"),
2517
AVCodec mpeg2video_decoder = {
2517
AVCodec ff_mpeg2video_decoder = {
2519
2519
AVMEDIA_TYPE_VIDEO,
2520
2520
CODEC_ID_MPEG2VIDEO,
2525
2525
mpeg_decode_frame,
2526
2526
CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY,
2528
2529
.long_name= NULL_IF_CONFIG_SMALL("MPEG-2 video"),
2530
.profiles = NULL_IF_CONFIG_SMALL(mpeg2_video_profiles),
2531
2533
//legacy decoder
2532
AVCodec mpegvideo_decoder = {
2534
AVCodec ff_mpegvideo_decoder = {
2534
2536
AVMEDIA_TYPE_VIDEO,
2535
2537
CODEC_ID_MPEG2VIDEO,
2550
2553
if( !(avctx->slice_flags & SLICE_FLAG_CODED_ORDER) )
2552
2555
if( !(avctx->slice_flags & SLICE_FLAG_ALLOW_FIELD) ){
2553
dprintf(avctx, "mpeg12.c: XvMC decoder will work better if SLICE_FLAG_ALLOW_FIELD is set\n");
2556
av_dlog(avctx, "mpeg12.c: XvMC decoder will work better if SLICE_FLAG_ALLOW_FIELD is set\n");
2555
2558
mpeg_decode_init(avctx);