3
3
* Copyright (c) 2000, 2001, 2002 Fabrice Bellard
5
* This file is part of FFmpeg.
5
* This file is part of Libav.
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.
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.
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
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;
84
84
static int put_pack_header(AVFormatContext *ctx,
85
85
uint8_t *buf, int64_t timestamp)
297
297
int video_bitrate;
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);
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;
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
375
/* see VCD standard, p. IV-7*/
374
376
stream->max_buffer_size = 46 * 1024;
489
491
return AVERROR(ENOMEM);
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)
496
498
(((timestamp >> 30) & 0x07) << 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));
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)
616
618
MpegMuxContext *s = ctx->priv_data;
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) {
623
625
packet_bytes -= 7;
625
627
packet_bytes -= 6;
627
629
for(i=0;i<packet_bytes;i++)
631
633
static int get_nb_frames(AVFormatContext *ctx, StreamInfo *stream, int len){
697
699
size = put_system_header(ctx, buf_ptr, 0);
699
701
size = buf_ptr - buffer;
700
put_buffer(ctx->pb, buffer, size);
702
avio_write(ctx->pb, buffer, size);
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);
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);
714
716
memset(buffer, 0, 128);
715
717
buf_ptr = buffer;
734
736
size = buf_ptr - buffer;
735
put_buffer(ctx->pb, buffer, size);
737
avio_write(ctx->pb, buffer, size);
737
739
packet_size = s->packet_size - size;
838
840
nb_frames= get_nb_frames(ctx, stream, payload_size - stuffing_size);
840
put_be32(ctx->pb, startcode);
842
avio_wb32(ctx->pb, startcode);
842
put_be16(ctx->pb, packet_size);
844
avio_wb16(ctx->pb, packet_size);
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);
848
850
if (s->is_mpeg2) {
849
put_byte(ctx->pb, 0x80); /* mpeg2 id */
851
avio_w8(ctx->pb, 0x80); /* mpeg2 id */
863
865
if (stream->packet_number == 0)
864
866
pes_flags |= 0x01;
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);
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);
874
876
if (pes_flags & 0x01) { /*write pes extension*/
875
put_byte(ctx->pb, 0x10); /* flags */
877
avio_w8(ctx->pb, 0x10); /* flags */
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);
881
put_be16(ctx->pb, 0x6000 | stream->max_buffer_size/1024);
883
avio_wb16(ctx->pb, 0x6000 | stream->max_buffer_size/1024);
890
892
put_timestamp(ctx->pb, 0x02, pts);
893
put_byte(ctx->pb, 0x0f);
895
avio_w8(ctx->pb, 0x0f);
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);
902
904
for(i=0;i<stuffing_size;i++)
903
put_byte(ctx->pb, 0xff);
905
avio_w8(ctx->pb, 0xff);
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]);
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) {
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);
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;
961
963
for(i=0;i<s->packet_size;i++)
962
put_byte(ctx->pb, 0);
964
966
s->vcd_padding_bytes_written += s->packet_size;
966
put_flush_packet(ctx->pb);
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
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){
1169
s->last_scr= dts + preload;
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);
1220
1226
for(i=0;i<ctx->nb_streams;i++) {
1221
1227
stream = ctx->streams[i]->priv_data;
1229
1235
#if CONFIG_MPEG1SYSTEM_MUXER
1230
AVOutputFormat mpeg1system_muxer = {
1236
AVOutputFormat ff_mpeg1system_muxer = {
1232
1238
NULL_IF_CONFIG_SMALL("MPEG-1 System format"),
1243
1249
#if CONFIG_MPEG1VCD_MUXER
1244
AVOutputFormat mpeg1vcd_muxer = {
1250
AVOutputFormat ff_mpeg1vcd_muxer = {
1246
1252
NULL_IF_CONFIG_SMALL("MPEG-1 System format (VCD)"),
1257
1263
#if CONFIG_MPEG2VOB_MUXER
1258
AVOutputFormat mpeg2vob_muxer = {
1264
AVOutputFormat ff_mpeg2vob_muxer = {
1260
1266
NULL_IF_CONFIG_SMALL("MPEG-2 PS format (VOB)"),
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 = {
1276
1282
NULL_IF_CONFIG_SMALL("MPEG-2 PS format (VOB)"),
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 = {
1292
1298
NULL_IF_CONFIG_SMALL("MPEG-2 PS format (DVD VOB)"),