~ubuntu-branches/ubuntu/oneiric/libav/oneiric

« back to all changes in this revision

Viewing changes to libavformat/mpegenc.c

  • Committer: Bazaar Package Importer
  • Author(s): Reinhard Tartler
  • Date: 2011-04-30 14:27:42 UTC
  • mfrom: (1.1.2 experimental)
  • Revision ID: james.westby@ubuntu.com-20110430142742-quvblxk1tj6adlh5
Tags: 4:0.7~b1-1ubuntu1
* Merge from debian. Remaining changes:
  - don't build against libfaad, libdirac, librtmp and libopenjpeg
    (all in universe)
  - explicitly --enable-pic on powerpc, cf. LP #654666
  - different arm configure bits that should probably better be
    merged into debian
* Cherry-picked from git: 
  - install doc/APIChanges and refer to them in NEWS.Debian (Closes: #623682)
  - don't try to install non-existing documentation, fixes FTBFS on powerpc

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
 * MPEG1/2 muxer
3
3
 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
4
4
 *
5
 
 * This file is part of FFmpeg.
 
5
 * This file is part of Libav.
6
6
 *
7
 
 * FFmpeg is free software; you can redistribute it and/or
 
7
 * Libav is free software; you can redistribute it and/or
8
8
 * modify it under the terms of the GNU Lesser General Public
9
9
 * License as published by the Free Software Foundation; either
10
10
 * version 2.1 of the License, or (at your option) any later version.
11
11
 *
12
 
 * FFmpeg is distributed in the hope that it will be useful,
 
12
 * Libav is distributed in the hope that it will be useful,
13
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
15
 * Lesser General Public License for more details.
16
16
 *
17
17
 * You should have received a copy of the GNU Lesser General Public
18
 
 * License along with FFmpeg; if not, write to the Free Software
 
18
 * License along with Libav; if not, write to the Free Software
19
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
20
 */
21
21
 
76
76
 
77
77
} MpegMuxContext;
78
78
 
79
 
extern AVOutputFormat mpeg1vcd_muxer;
80
 
extern AVOutputFormat mpeg2dvd_muxer;
81
 
extern AVOutputFormat mpeg2svcd_muxer;
82
 
extern AVOutputFormat mpeg2vob_muxer;
 
79
extern AVOutputFormat ff_mpeg1vcd_muxer;
 
80
extern AVOutputFormat ff_mpeg2dvd_muxer;
 
81
extern AVOutputFormat ff_mpeg2svcd_muxer;
 
82
extern AVOutputFormat ff_mpeg2vob_muxer;
83
83
 
84
84
static int put_pack_header(AVFormatContext *ctx,
85
85
                           uint8_t *buf, int64_t timestamp)
297
297
    int video_bitrate;
298
298
 
299
299
    s->packet_number = 0;
300
 
    s->is_vcd =    (CONFIG_MPEG1VCD_MUXER  && ctx->oformat == &mpeg1vcd_muxer);
301
 
    s->is_svcd =   (CONFIG_MPEG2SVCD_MUXER && ctx->oformat == &mpeg2svcd_muxer);
302
 
    s->is_mpeg2 = ((CONFIG_MPEG2VOB_MUXER  && ctx->oformat == &mpeg2vob_muxer) ||
303
 
                   (CONFIG_MPEG2DVD_MUXER  && ctx->oformat == &mpeg2dvd_muxer) ||
304
 
                   (CONFIG_MPEG2SVCD_MUXER && ctx->oformat == &mpeg2svcd_muxer));
305
 
    s->is_dvd =    (CONFIG_MPEG2DVD_MUXER  && ctx->oformat == &mpeg2dvd_muxer);
 
300
    s->is_vcd =    (CONFIG_MPEG1VCD_MUXER  && ctx->oformat == &ff_mpeg1vcd_muxer);
 
301
    s->is_svcd =   (CONFIG_MPEG2SVCD_MUXER && ctx->oformat == &ff_mpeg2svcd_muxer);
 
302
    s->is_mpeg2 = ((CONFIG_MPEG2VOB_MUXER  && ctx->oformat == &ff_mpeg2vob_muxer) ||
 
303
                   (CONFIG_MPEG2DVD_MUXER  && ctx->oformat == &ff_mpeg2dvd_muxer) ||
 
304
                   (CONFIG_MPEG2SVCD_MUXER && ctx->oformat == &ff_mpeg2svcd_muxer));
 
305
    s->is_dvd =    (CONFIG_MPEG2DVD_MUXER  && ctx->oformat == &ff_mpeg2dvd_muxer);
306
306
 
307
307
    if(ctx->packet_size) {
308
308
        if (ctx->packet_size < 20 || ctx->packet_size > (1 << 23) + 10) {
367
367
            stream->id = mpv_id++;
368
368
            if (st->codec->rc_buffer_size)
369
369
                stream->max_buffer_size = 6*1024 + st->codec->rc_buffer_size/8;
370
 
            else
 
370
            else {
 
371
                av_log(ctx, AV_LOG_WARNING, "VBV buffer size not set, muxing may fail\n");
371
372
                stream->max_buffer_size = 230*1024; //FIXME this is probably too small as default
 
373
            }
372
374
#if 0
373
375
                /* see VCD standard, p. IV-7*/
374
376
                stream->max_buffer_size = 46 * 1024;
489
491
    return AVERROR(ENOMEM);
490
492
}
491
493
 
492
 
static inline void put_timestamp(ByteIOContext *pb, int id, int64_t timestamp)
 
494
static inline void put_timestamp(AVIOContext *pb, int id, int64_t timestamp)
493
495
{
494
 
    put_byte(pb,
 
496
    avio_w8(pb,
495
497
             (id << 4) |
496
498
             (((timestamp >> 30) & 0x07) << 1) |
497
499
             1);
498
 
    put_be16(pb, (uint16_t)((((timestamp >> 15) & 0x7fff) << 1) | 1));
499
 
    put_be16(pb, (uint16_t)((((timestamp      ) & 0x7fff) << 1) | 1));
 
500
    avio_wb16(pb, (uint16_t)((((timestamp >> 15) & 0x7fff) << 1) | 1));
 
501
    avio_wb16(pb, (uint16_t)((((timestamp      ) & 0x7fff) << 1) | 1));
500
502
}
501
503
 
502
504
 
611
613
#endif
612
614
 
613
615
/* Write an MPEG padding packet header. */
614
 
static void put_padding_packet(AVFormatContext *ctx, ByteIOContext *pb,int packet_bytes)
 
616
static void put_padding_packet(AVFormatContext *ctx, AVIOContext *pb,int packet_bytes)
615
617
{
616
618
    MpegMuxContext *s = ctx->priv_data;
617
619
    int i;
618
620
 
619
 
    put_be32(pb, PADDING_STREAM);
620
 
    put_be16(pb, packet_bytes - 6);
 
621
    avio_wb32(pb, PADDING_STREAM);
 
622
    avio_wb16(pb, packet_bytes - 6);
621
623
    if (!s->is_mpeg2) {
622
 
        put_byte(pb, 0x0f);
 
624
        avio_w8(pb, 0x0f);
623
625
        packet_bytes -= 7;
624
626
    } else
625
627
        packet_bytes -= 6;
626
628
 
627
629
    for(i=0;i<packet_bytes;i++)
628
 
        put_byte(pb, 0xff);
 
630
        avio_w8(pb, 0xff);
629
631
}
630
632
 
631
633
static int get_nb_frames(AVFormatContext *ctx, StreamInfo *stream, int len){
697
699
                    size = put_system_header(ctx, buf_ptr, 0);
698
700
                    buf_ptr += size;
699
701
                    size = buf_ptr - buffer;
700
 
                    put_buffer(ctx->pb, buffer, size);
 
702
                    avio_write(ctx->pb, buffer, size);
701
703
 
702
 
                    put_be32(ctx->pb, PRIVATE_STREAM_2);
703
 
                    put_be16(ctx->pb, 0x03d4);         // length
704
 
                    put_byte(ctx->pb, 0x00);           // substream ID, 00=PCI
 
704
                    avio_wb32(ctx->pb, PRIVATE_STREAM_2);
 
705
                    avio_wb16(ctx->pb, 0x03d4);         // length
 
706
                    avio_w8(ctx->pb, 0x00);           // substream ID, 00=PCI
705
707
                    for (i = 0; i < 979; i++)
706
 
                        put_byte(ctx->pb, 0x00);
 
708
                        avio_w8(ctx->pb, 0x00);
707
709
 
708
 
                    put_be32(ctx->pb, PRIVATE_STREAM_2);
709
 
                    put_be16(ctx->pb, 0x03fa);         // length
710
 
                    put_byte(ctx->pb, 0x01);           // substream ID, 01=DSI
 
710
                    avio_wb32(ctx->pb, PRIVATE_STREAM_2);
 
711
                    avio_wb16(ctx->pb, 0x03fa);         // length
 
712
                    avio_w8(ctx->pb, 0x01);           // substream ID, 01=DSI
711
713
                    for (i = 0; i < 1017; i++)
712
 
                        put_byte(ctx->pb, 0x00);
 
714
                        avio_w8(ctx->pb, 0x00);
713
715
 
714
716
                    memset(buffer, 0, 128);
715
717
                    buf_ptr = buffer;
732
734
        }
733
735
    }
734
736
    size = buf_ptr - buffer;
735
 
    put_buffer(ctx->pb, buffer, size);
 
737
    avio_write(ctx->pb, buffer, size);
736
738
 
737
739
    packet_size = s->packet_size - size;
738
740
 
837
839
 
838
840
        nb_frames= get_nb_frames(ctx, stream, payload_size - stuffing_size);
839
841
 
840
 
        put_be32(ctx->pb, startcode);
 
842
        avio_wb32(ctx->pb, startcode);
841
843
 
842
 
        put_be16(ctx->pb, packet_size);
 
844
        avio_wb16(ctx->pb, packet_size);
843
845
 
844
846
        if (!s->is_mpeg2)
845
847
            for(i=0;i<stuffing_size;i++)
846
 
                put_byte(ctx->pb, 0xff);
 
848
                avio_w8(ctx->pb, 0xff);
847
849
 
848
850
        if (s->is_mpeg2) {
849
 
            put_byte(ctx->pb, 0x80); /* mpeg2 id */
 
851
            avio_w8(ctx->pb, 0x80); /* mpeg2 id */
850
852
 
851
853
            pes_flags=0;
852
854
 
863
865
            if (stream->packet_number == 0)
864
866
                pes_flags |= 0x01;
865
867
 
866
 
            put_byte(ctx->pb, pes_flags); /* flags */
867
 
            put_byte(ctx->pb, header_len - 3 + stuffing_size);
 
868
            avio_w8(ctx->pb, pes_flags); /* flags */
 
869
            avio_w8(ctx->pb, header_len - 3 + stuffing_size);
868
870
 
869
871
            if (pes_flags & 0x80)  /*write pts*/
870
872
                put_timestamp(ctx->pb, (pes_flags & 0x40) ? 0x03 : 0x02, pts);
872
874
                put_timestamp(ctx->pb, 0x01, dts);
873
875
 
874
876
            if (pes_flags & 0x01) {  /*write pes extension*/
875
 
                put_byte(ctx->pb, 0x10); /* flags */
 
877
                avio_w8(ctx->pb, 0x10); /* flags */
876
878
 
877
879
                /* P-STD buffer info */
878
880
                if ((id & 0xe0) == AUDIO_ID)
879
 
                    put_be16(ctx->pb, 0x4000 | stream->max_buffer_size/ 128);
 
881
                    avio_wb16(ctx->pb, 0x4000 | stream->max_buffer_size/ 128);
880
882
                else
881
 
                    put_be16(ctx->pb, 0x6000 | stream->max_buffer_size/1024);
 
883
                    avio_wb16(ctx->pb, 0x6000 | stream->max_buffer_size/1024);
882
884
            }
883
885
 
884
886
        } else {
890
892
                    put_timestamp(ctx->pb, 0x02, pts);
891
893
                }
892
894
            } else {
893
 
                put_byte(ctx->pb, 0x0f);
 
895
                avio_w8(ctx->pb, 0x0f);
894
896
            }
895
897
        }
896
898
 
897
899
        if (s->is_mpeg2) {
898
900
            /* special stuffing byte that is always written
899
901
               to prevent accidental generation of start codes. */
900
 
            put_byte(ctx->pb, 0xff);
 
902
            avio_w8(ctx->pb, 0xff);
901
903
 
902
904
            for(i=0;i<stuffing_size;i++)
903
 
                put_byte(ctx->pb, 0xff);
 
905
                avio_w8(ctx->pb, 0xff);
904
906
        }
905
907
 
906
908
        if (startcode == PRIVATE_STREAM_1) {
907
 
            put_byte(ctx->pb, id);
 
909
            avio_w8(ctx->pb, id);
908
910
            if (id >= 0xa0) {
909
911
                /* LPCM (XXX: check nb_frames) */
910
 
                put_byte(ctx->pb, 7);
911
 
                put_be16(ctx->pb, 4); /* skip 3 header bytes */
912
 
                put_byte(ctx->pb, stream->lpcm_header[0]);
913
 
                put_byte(ctx->pb, stream->lpcm_header[1]);
914
 
                put_byte(ctx->pb, stream->lpcm_header[2]);
 
912
                avio_w8(ctx->pb, 7);
 
913
                avio_wb16(ctx->pb, 4); /* skip 3 header bytes */
 
914
                avio_w8(ctx->pb, stream->lpcm_header[0]);
 
915
                avio_w8(ctx->pb, stream->lpcm_header[1]);
 
916
                avio_w8(ctx->pb, stream->lpcm_header[2]);
915
917
            } else if (id >= 0x40) {
916
918
                /* AC-3 */
917
 
                put_byte(ctx->pb, nb_frames);
918
 
                put_be16(ctx->pb, trailer_size+1);
 
919
                avio_w8(ctx->pb, nb_frames);
 
920
                avio_wb16(ctx->pb, trailer_size+1);
919
921
            }
920
922
        }
921
923
 
922
924
        /* output data */
923
925
        assert(payload_size - stuffing_size <= av_fifo_size(stream->fifo));
924
 
        av_fifo_generic_read(stream->fifo, ctx->pb, payload_size - stuffing_size, &put_buffer);
 
926
        av_fifo_generic_read(stream->fifo, ctx->pb, payload_size - stuffing_size, &avio_write);
925
927
        stream->bytes_to_iframe -= payload_size - stuffing_size;
926
928
    }else{
927
929
        payload_size=
932
934
        put_padding_packet(ctx,ctx->pb, pad_packet_bytes);
933
935
 
934
936
    for(i=0;i<zero_trail_bytes;i++)
935
 
        put_byte(ctx->pb, 0x00);
 
937
        avio_w8(ctx->pb, 0x00);
936
938
 
937
 
    put_flush_packet(ctx->pb);
 
939
    avio_flush(ctx->pb);
938
940
 
939
941
    s->packet_number++;
940
942
 
959
961
    int i;
960
962
 
961
963
    for(i=0;i<s->packet_size;i++)
962
 
        put_byte(ctx->pb, 0);
 
964
        avio_w8(ctx->pb, 0);
963
965
 
964
966
    s->vcd_padding_bytes_written += s->packet_size;
965
967
 
966
 
    put_flush_packet(ctx->pb);
 
968
    avio_flush(ctx->pb);
967
969
 
968
970
    /* increasing the packet number is correct. The SCR of the following packs
969
971
       is calculated from the packet_number and it has to include the padding
1161
1163
    pts= pkt->pts;
1162
1164
    dts= pkt->dts;
1163
1165
 
1164
 
    if(pts != AV_NOPTS_VALUE) pts += preload;
1165
 
    if(dts != AV_NOPTS_VALUE) dts += preload;
 
1166
    if(pts != AV_NOPTS_VALUE) pts += 2*preload;
 
1167
    if(dts != AV_NOPTS_VALUE){
 
1168
        if(!s->last_scr)
 
1169
            s->last_scr= dts + preload;
 
1170
        dts += 2*preload;
 
1171
    }
1166
1172
 
1167
1173
//av_log(ctx, AV_LOG_DEBUG, "dts:%f pts:%f flags:%d stream:%d nopts:%d\n", dts/90000.0, pts/90000.0, pkt->flags, pkt->stream_index, pts != AV_NOPTS_VALUE);
1168
1174
    if (!stream->premux_packet)
1214
1220
    /* End header according to MPEG1 systems standard. We do not write
1215
1221
       it as it is usually not needed by decoders and because it
1216
1222
       complicates MPEG stream concatenation. */
1217
 
    //put_be32(ctx->pb, ISO_11172_END_CODE);
1218
 
    //put_flush_packet(ctx->pb);
 
1223
    //avio_wb32(ctx->pb, ISO_11172_END_CODE);
 
1224
    //avio_flush(ctx->pb);
1219
1225
 
1220
1226
    for(i=0;i<ctx->nb_streams;i++) {
1221
1227
        stream = ctx->streams[i]->priv_data;
1227
1233
}
1228
1234
 
1229
1235
#if CONFIG_MPEG1SYSTEM_MUXER
1230
 
AVOutputFormat mpeg1system_muxer = {
 
1236
AVOutputFormat ff_mpeg1system_muxer = {
1231
1237
    "mpeg",
1232
1238
    NULL_IF_CONFIG_SMALL("MPEG-1 System format"),
1233
1239
    "video/mpeg",
1241
1247
};
1242
1248
#endif
1243
1249
#if CONFIG_MPEG1VCD_MUXER
1244
 
AVOutputFormat mpeg1vcd_muxer = {
 
1250
AVOutputFormat ff_mpeg1vcd_muxer = {
1245
1251
    "vcd",
1246
1252
    NULL_IF_CONFIG_SMALL("MPEG-1 System format (VCD)"),
1247
1253
    "video/mpeg",
1255
1261
};
1256
1262
#endif
1257
1263
#if CONFIG_MPEG2VOB_MUXER
1258
 
AVOutputFormat mpeg2vob_muxer = {
 
1264
AVOutputFormat ff_mpeg2vob_muxer = {
1259
1265
    "vob",
1260
1266
    NULL_IF_CONFIG_SMALL("MPEG-2 PS format (VOB)"),
1261
1267
    "video/mpeg",
1271
1277
 
1272
1278
/* Same as mpeg2vob_mux except that the pack size is 2324 */
1273
1279
#if CONFIG_MPEG2SVCD_MUXER
1274
 
AVOutputFormat mpeg2svcd_muxer = {
 
1280
AVOutputFormat ff_mpeg2svcd_muxer = {
1275
1281
    "svcd",
1276
1282
    NULL_IF_CONFIG_SMALL("MPEG-2 PS format (VOB)"),
1277
1283
    "video/mpeg",
1287
1293
 
1288
1294
/*  Same as mpeg2vob_mux except the 'is_dvd' flag is set to produce NAV pkts */
1289
1295
#if CONFIG_MPEG2DVD_MUXER
1290
 
AVOutputFormat mpeg2dvd_muxer = {
 
1296
AVOutputFormat ff_mpeg2dvd_muxer = {
1291
1297
    "dvd",
1292
1298
    NULL_IF_CONFIG_SMALL("MPEG-2 PS format (DVD VOB)"),
1293
1299
    "video/mpeg",