~medibuntu-maintainers/mplayer/medibuntu.precise

« back to all changes in this revision

Viewing changes to ffmpeg/libavcodec/mpeg12enc.c

  • Committer: Package Import Robot
  • Author(s): Reinhard Tartler
  • Date: 2012-01-12 22:23:28 UTC
  • mfrom: (0.4.7 sid)
  • mto: This revision was merged to the branch mainline in revision 76.
  • Revision ID: package-import@ubuntu.com-20120112222328-8jqdyodym3p84ygu
Tags: 2:1.0~rc4.dfsg1+svn34540-1
* New upstream snapshot
* upload to unstable

Show diffs side-by-side

added added

removed removed

Lines of Context:
27
27
 
28
28
#include "avcodec.h"
29
29
#include "dsputil.h"
 
30
#include "mathops.h"
30
31
#include "mpegvideo.h"
31
32
 
32
33
#include "mpeg12.h"
33
34
#include "mpeg12data.h"
34
35
#include "bytestream.h"
35
36
 
 
37
#include "libavutil/log.h"
 
38
#include "libavutil/opt.h"
36
39
 
37
40
static const uint8_t inv_non_linear_qscale[13] = {
38
41
    0, 2, 4, 6, 8,
71
74
    for(i=0; i<128; i++){
72
75
        int level= i-64;
73
76
        int run;
 
77
        if (!level)
 
78
            continue;
74
79
        for(run=0; run<64; run++){
75
 
            int len, bits, code;
 
80
            int len, code;
76
81
 
77
82
            int alevel= FFABS(level);
78
 
            int sign= (level>>31)&1;
79
83
 
80
84
            if (alevel > rl->max_level[0][run])
81
85
                code= 111; /*rl->n*/
83
87
                code= rl->index_run[0][run] + alevel - 1;
84
88
 
85
89
            if (code < 111 /* rl->n */) {
86
 
                /* store the vlc & sign at once */
 
90
                /* length of vlc and sign */
87
91
                len=   rl->table_vlc[code][1]+1;
88
 
                bits= (rl->table_vlc[code][0]<<1) + sign;
89
92
            } else {
90
93
                len=  rl->table_vlc[111/*rl->n*/][1]+6;
91
 
                bits= rl->table_vlc[111/*rl->n*/][0]<<6;
92
94
 
93
 
                bits|= run;
94
95
                if (alevel < 128) {
95
 
                    bits<<=8; len+=8;
96
 
                    bits|= level & 0xff;
 
96
                    len += 8;
97
97
                } else {
98
 
                    bits<<=16; len+=16;
99
 
                    bits|= level & 0xff;
100
 
                    if (level < 0) {
101
 
                        bits|= 0x8001 + level + 255;
102
 
                    } else {
103
 
                        bits|= level & 0xffff;
104
 
                    }
 
98
                    len += 16;
105
99
                }
106
100
            }
107
101
 
117
111
    int64_t d;
118
112
 
119
113
    for(i=1;i<14;i++) {
120
 
        int64_t n0= 1001LL/ff_frame_rate_tab[i].den*ff_frame_rate_tab[i].num*s->avctx->time_base.num;
 
114
        int64_t n0= 1001LL/avpriv_frame_rate_tab[i].den*avpriv_frame_rate_tab[i].num*s->avctx->time_base.num;
121
115
        int64_t n1= 1001LL*s->avctx->time_base.den;
122
116
        if(s->avctx->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL && i>=9) break;
123
117
 
140
134
    if(MPV_encode_init(avctx) < 0)
141
135
        return -1;
142
136
 
 
137
#if FF_API_MPEGVIDEO_GLOBAL_OPTS
 
138
    if (avctx->flags2 & CODEC_FLAG2_DROP_FRAME_TIMECODE)
 
139
        s->drop_frame_timecode = 1;
 
140
    if (avctx->flags & CODEC_FLAG_SVCD_SCAN_OFFSET)
 
141
        s->scan_offset = 1;
 
142
#endif
 
143
 
143
144
    if(find_frame_rate_index(s) < 0){
144
145
        if(s->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL){
145
146
            av_log(avctx, AV_LOG_ERROR, "MPEG1/2 does not support %d/%d fps\n", avctx->time_base.den, avctx->time_base.num);
172
173
        }
173
174
    }
174
175
 
175
 
    if((avctx->flags2 & CODEC_FLAG2_DROP_FRAME_TIMECODE) && s->frame_rate_index != 4){
 
176
    if (s->drop_frame_timecode && s->frame_rate_index != 4) {
176
177
        av_log(avctx, AV_LOG_ERROR, "Drop frame time code only allowed with 1001/30000 fps\n");
177
178
        return -1;
178
179
    }
182
183
 
183
184
static void put_header(MpegEncContext *s, int header)
184
185
{
185
 
    align_put_bits(&s->pb);
 
186
    avpriv_align_put_bits(&s->pb);
186
187
    put_bits(&s->pb, 16, header>>16);
187
188
    put_sbits(&s->pb, 16, header);
188
189
}
200
201
 
201
202
        if(aspect_ratio==0.0) aspect_ratio= 1.0; //pixel aspect 1:1 (VGA)
202
203
 
203
 
        if (s->current_picture.key_frame) {
204
 
            AVRational framerate= ff_frame_rate_tab[s->frame_rate_index];
 
204
        if (s->current_picture.f.key_frame) {
 
205
            AVRational framerate= avpriv_frame_rate_tab[s->frame_rate_index];
205
206
 
206
207
            /* mpeg1 header repeated every gop */
207
208
            put_header(s, SEQ_START_CODE);
283
284
            }
284
285
 
285
286
            put_header(s, GOP_START_CODE);
286
 
            put_bits(&s->pb, 1, !!(s->avctx->flags2 & CODEC_FLAG2_DROP_FRAME_TIMECODE)); /* drop frame flag */
 
287
            put_bits(&s->pb, 1, s->drop_frame_timecode); /* drop frame flag */
287
288
            /* time code : we must convert from the real frame rate to a
288
289
               fake mpeg frame rate in case of low frame rate */
289
290
            fps = (framerate.num + framerate.den/2)/ framerate.den;
290
 
            time_code = s->current_picture_ptr->coded_picture_number + s->avctx->timecode_frame_start;
 
291
            time_code = s->current_picture_ptr->f.coded_picture_number + s->avctx->timecode_frame_start;
291
292
 
292
 
            s->gop_picture_number = s->current_picture_ptr->coded_picture_number;
293
 
            if (s->avctx->flags2 & CODEC_FLAG2_DROP_FRAME_TIMECODE) {
 
293
            s->gop_picture_number = s->current_picture_ptr->f.coded_picture_number;
 
294
            if (s->drop_frame_timecode) {
294
295
                /* only works for NTSC 29.97 */
295
296
                int d = time_code / 17982;
296
297
                int m = time_code % 17982;
396
397
        if (s->progressive_sequence) {
397
398
            put_bits(&s->pb, 1, 0); /* no repeat */
398
399
        } else {
399
 
            put_bits(&s->pb, 1, s->current_picture_ptr->top_field_first);
 
400
            put_bits(&s->pb, 1, s->current_picture_ptr->f.top_field_first);
400
401
        }
401
402
        /* XXX: optimize the generation of this flag with entropy
402
403
           measures */
413
414
        put_bits(&s->pb, 1, s->progressive_frame);
414
415
        put_bits(&s->pb, 1, 0); //composite_display_flag
415
416
    }
416
 
    if(s->flags & CODEC_FLAG_SVCD_SCAN_OFFSET){
 
417
    if (s->scan_offset) {
417
418
        int i;
418
419
 
419
420
        put_header(s, USER_START_CODE);
681
682
        int bit_size = f_or_b_code - 1;
682
683
        int range = 1 << bit_size;
683
684
        /* modulo encoding */
684
 
        int l= INT_BIT - 5 - bit_size;
685
 
        val= (val<<l)>>l;
 
685
        val = sign_extend(val, 5 + bit_size);
686
686
 
687
687
        if (val >= 0) {
688
688
            val--;
925
925
    put_bits(&s->pb, table_vlc[112][1], table_vlc[112][0]);
926
926
}
927
927
 
 
928
#define OFFSET(x) offsetof(MpegEncContext, x)
 
929
#define VE AV_OPT_FLAG_ENCODING_PARAM | AV_OPT_FLAG_VIDEO_PARAM
 
930
#define COMMON_OPTS\
 
931
    { "intra_vlc",           "Use MPEG-2 intra VLC table.",       OFFSET(intra_vlc_format),    AV_OPT_TYPE_INT, { 0 }, 0, 1, VE },\
 
932
    { "drop_frame_timecode", "Timecode is in drop frame format.", OFFSET(drop_frame_timecode), AV_OPT_TYPE_INT, { 0 }, 0, 1, VE}, \
 
933
    { "scan_offset",         "Reserve space for SVCD scan offset user data.", OFFSET(scan_offset), AV_OPT_TYPE_INT, { 0 }, 0, 1, VE },
 
934
 
 
935
static const AVOption mpeg1_options[] = {
 
936
    COMMON_OPTS
 
937
    { NULL },
 
938
};
 
939
 
 
940
static const AVOption mpeg2_options[] = {
 
941
    COMMON_OPTS
 
942
    { "non_linear_quant",    "Use nonlinear quantizer.",          OFFSET(q_scale_type),         AV_OPT_TYPE_INT, { 0 }, 0, 1, VE },
 
943
    { "alternate_scan",      "Enable alternate scantable.",       OFFSET(alternate_scan),       AV_OPT_TYPE_INT, { 0 }, 0, 1, VE },
 
944
    { NULL },
 
945
};
 
946
 
 
947
#define mpeg12_class(x)\
 
948
static const AVClass mpeg## x ##_class = {\
 
949
    .class_name   = "mpeg" #x "video encoder",\
 
950
    .item_name    = av_default_item_name,\
 
951
    .option       = mpeg## x ##_options,\
 
952
    .version      = LIBAVUTIL_VERSION_INT,\
 
953
};
 
954
 
 
955
mpeg12_class(1)
 
956
mpeg12_class(2)
 
957
 
928
958
AVCodec ff_mpeg1video_encoder = {
929
 
    "mpeg1video",
930
 
    AVMEDIA_TYPE_VIDEO,
931
 
    CODEC_ID_MPEG1VIDEO,
932
 
    sizeof(MpegEncContext),
933
 
    encode_init,
934
 
    MPV_encode_picture,
935
 
    MPV_encode_end,
936
 
    .supported_framerates= ff_frame_rate_tab+1,
 
959
    .name           = "mpeg1video",
 
960
    .type           = AVMEDIA_TYPE_VIDEO,
 
961
    .id             = CODEC_ID_MPEG1VIDEO,
 
962
    .priv_data_size = sizeof(MpegEncContext),
 
963
    .init           = encode_init,
 
964
    .encode         = MPV_encode_picture,
 
965
    .close          = MPV_encode_end,
 
966
    .supported_framerates= avpriv_frame_rate_tab+1,
937
967
    .pix_fmts= (const enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_NONE},
938
968
    .capabilities= CODEC_CAP_DELAY | CODEC_CAP_SLICE_THREADS,
939
969
    .long_name= NULL_IF_CONFIG_SMALL("MPEG-1 video"),
 
970
    .priv_class     = &mpeg1_class,
940
971
};
941
972
 
942
973
AVCodec ff_mpeg2video_encoder = {
943
 
    "mpeg2video",
944
 
    AVMEDIA_TYPE_VIDEO,
945
 
    CODEC_ID_MPEG2VIDEO,
946
 
    sizeof(MpegEncContext),
947
 
    encode_init,
948
 
    MPV_encode_picture,
949
 
    MPV_encode_end,
950
 
    .supported_framerates= ff_frame_rate_tab+1,
 
974
    .name           = "mpeg2video",
 
975
    .type           = AVMEDIA_TYPE_VIDEO,
 
976
    .id             = CODEC_ID_MPEG2VIDEO,
 
977
    .priv_data_size = sizeof(MpegEncContext),
 
978
    .init           = encode_init,
 
979
    .encode         = MPV_encode_picture,
 
980
    .close          = MPV_encode_end,
 
981
    .supported_framerates= avpriv_frame_rate_tab+1,
951
982
    .pix_fmts= (const enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_YUV422P, PIX_FMT_NONE},
952
983
    .capabilities= CODEC_CAP_DELAY | CODEC_CAP_SLICE_THREADS,
953
984
    .long_name= NULL_IF_CONFIG_SMALL("MPEG-2 video"),
 
985
    .priv_class     = &mpeg2_class,
954
986
};