3 * Copyright (c) 2011 Martin Storsjo
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
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,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
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
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "libavutil/mathematics.h"
24 #include "avio_internal.h"
27 AVFormatContext
*mpegts_ctx
;
28 AVFormatContext
*rtp_ctx
;
31 static int rtp_mpegts_write_close(AVFormatContext
*s
)
33 struct MuxChain
*chain
= s
->priv_data
;
35 if (chain
->mpegts_ctx
) {
36 av_write_trailer(chain
->mpegts_ctx
);
37 ffio_free_dyn_buf(&chain
->mpegts_ctx
->pb
);
38 avformat_free_context(chain
->mpegts_ctx
);
41 av_write_trailer(chain
->rtp_ctx
);
42 avformat_free_context(chain
->rtp_ctx
);
47 static int rtp_mpegts_write_header(AVFormatContext
*s
)
49 struct MuxChain
*chain
= s
->priv_data
;
50 AVFormatContext
*mpegts_ctx
= NULL
, *rtp_ctx
= NULL
;
51 ff_const59 AVOutputFormat
*mpegts_format
= av_guess_format("mpegts", NULL
, NULL
);
52 ff_const59 AVOutputFormat
*rtp_format
= av_guess_format("rtp", NULL
, NULL
);
53 int i
, ret
= AVERROR(ENOMEM
);
56 if (!mpegts_format
|| !rtp_format
)
57 return AVERROR(ENOSYS
);
58 mpegts_ctx
= avformat_alloc_context();
60 return AVERROR(ENOMEM
);
61 mpegts_ctx
->oformat
= mpegts_format
;
62 mpegts_ctx
->max_delay
= s
->max_delay
;
63 av_dict_copy(&mpegts_ctx
->metadata
, s
->metadata
, 0);
64 for (i
= 0; i
< s
->nb_streams
; i
++) {
65 AVStream
* st
= avformat_new_stream(mpegts_ctx
, NULL
);
68 st
->time_base
= s
->streams
[i
]->time_base
;
69 st
->sample_aspect_ratio
= s
->streams
[i
]->sample_aspect_ratio
;
70 avcodec_parameters_copy(st
->codecpar
, s
->streams
[i
]->codecpar
);
72 if ((ret
= avio_open_dyn_buf(&mpegts_ctx
->pb
)) < 0)
74 if ((ret
= avformat_write_header(mpegts_ctx
, NULL
)) < 0)
76 for (i
= 0; i
< s
->nb_streams
; i
++)
77 s
->streams
[i
]->time_base
= mpegts_ctx
->streams
[i
]->time_base
;
79 chain
->mpegts_ctx
= mpegts_ctx
;
82 rtp_ctx
= avformat_alloc_context();
84 ret
= AVERROR(ENOMEM
);
87 rtp_ctx
->oformat
= rtp_format
;
88 st
= avformat_new_stream(rtp_ctx
, NULL
);
90 ret
= AVERROR(ENOMEM
);
93 st
->time_base
.num
= 1;
94 st
->time_base
.den
= 90000;
95 st
->codecpar
->codec_id
= AV_CODEC_ID_MPEG2TS
;
97 if ((ret
= avformat_write_header(rtp_ctx
, NULL
)) < 0)
99 chain
->rtp_ctx
= rtp_ctx
;
105 ffio_free_dyn_buf(&mpegts_ctx
->pb
);
106 av_dict_free(&mpegts_ctx
->metadata
);
107 avformat_free_context(mpegts_ctx
);
109 avformat_free_context(rtp_ctx
);
110 rtp_mpegts_write_close(s
);
114 static int rtp_mpegts_write_packet(AVFormatContext
*s
, AVPacket
*pkt
)
116 struct MuxChain
*chain
= s
->priv_data
;
121 if (!chain
->mpegts_ctx
->pb
) {
122 if ((ret
= avio_open_dyn_buf(&chain
->mpegts_ctx
->pb
)) < 0)
125 if ((ret
= av_write_frame(chain
->mpegts_ctx
, pkt
)) < 0)
127 size
= avio_close_dyn_buf(chain
->mpegts_ctx
->pb
, &buf
);
128 chain
->mpegts_ctx
->pb
= NULL
;
133 av_init_packet(&local_pkt
);
134 local_pkt
.data
= buf
;
135 local_pkt
.size
= size
;
136 local_pkt
.stream_index
= 0;
137 if (pkt
->pts
!= AV_NOPTS_VALUE
)
138 local_pkt
.pts
= av_rescale_q(pkt
->pts
,
139 s
->streams
[pkt
->stream_index
]->time_base
,
140 chain
->rtp_ctx
->streams
[0]->time_base
);
141 if (pkt
->dts
!= AV_NOPTS_VALUE
)
142 local_pkt
.dts
= av_rescale_q(pkt
->dts
,
143 s
->streams
[pkt
->stream_index
]->time_base
,
144 chain
->rtp_ctx
->streams
[0]->time_base
);
145 ret
= av_write_frame(chain
->rtp_ctx
, &local_pkt
);
151 AVOutputFormat ff_rtp_mpegts_muxer
= {
152 .name
= "rtp_mpegts",
153 .long_name
= NULL_IF_CONFIG_SMALL("RTP/mpegts output format"),
154 .priv_data_size
= sizeof(struct MuxChain
),
155 .audio_codec
= AV_CODEC_ID_AAC
,
156 .video_codec
= AV_CODEC_ID_MPEG4
,
157 .write_header
= rtp_mpegts_write_header
,
158 .write_packet
= rtp_mpegts_write_packet
,
159 .write_trailer
= rtp_mpegts_write_close
,