]> git.sesse.net Git - ffmpeg/blob - libavformat/rtpenc_mpegts.c
avformat/kvag: allow seeking to start
[ffmpeg] / libavformat / rtpenc_mpegts.c
1 /*
2  * RTP/mpegts muxer
3  * Copyright (c) 2011 Martin Storsjo
4  *
5  * This file is part of FFmpeg.
6  *
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.
11  *
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.
16  *
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
20  */
21
22 #include "libavutil/mathematics.h"
23 #include "avformat.h"
24 #include "avio_internal.h"
25
26 typedef struct MuxChain {
27     AVFormatContext *mpegts_ctx;
28     AVFormatContext *rtp_ctx;
29     AVPacket *pkt;
30 } MuxChain;
31
32 static int rtp_mpegts_write_close(AVFormatContext *s)
33 {
34     MuxChain *chain = s->priv_data;
35
36     if (chain->mpegts_ctx) {
37         av_write_trailer(chain->mpegts_ctx);
38         ffio_free_dyn_buf(&chain->mpegts_ctx->pb);
39         avformat_free_context(chain->mpegts_ctx);
40     }
41     if (chain->rtp_ctx) {
42         av_write_trailer(chain->rtp_ctx);
43         avformat_free_context(chain->rtp_ctx);
44     }
45
46     av_packet_free(&chain->pkt);
47
48     return 0;
49 }
50
51 static int rtp_mpegts_write_header(AVFormatContext *s)
52 {
53     MuxChain *chain = s->priv_data;
54     AVFormatContext *mpegts_ctx = NULL, *rtp_ctx = NULL;
55     ff_const59 AVOutputFormat *mpegts_format = av_guess_format("mpegts", NULL, NULL);
56     ff_const59 AVOutputFormat *rtp_format    = av_guess_format("rtp", NULL, NULL);
57     int i, ret = AVERROR(ENOMEM);
58     AVStream *st;
59
60     if (!mpegts_format || !rtp_format)
61         return AVERROR(ENOSYS);
62     mpegts_ctx = avformat_alloc_context();
63     if (!mpegts_ctx)
64         return AVERROR(ENOMEM);
65     chain->pkt = av_packet_alloc();
66     if (!chain->pkt)
67         goto fail;
68     mpegts_ctx->oformat   = mpegts_format;
69     mpegts_ctx->max_delay = s->max_delay;
70     av_dict_copy(&mpegts_ctx->metadata, s->metadata, 0);
71     for (i = 0; i < s->nb_streams; i++) {
72         AVStream* st = avformat_new_stream(mpegts_ctx, NULL);
73         if (!st)
74             goto fail;
75         st->time_base           = s->streams[i]->time_base;
76         st->sample_aspect_ratio = s->streams[i]->sample_aspect_ratio;
77         avcodec_parameters_copy(st->codecpar, s->streams[i]->codecpar);
78     }
79     if ((ret = avio_open_dyn_buf(&mpegts_ctx->pb)) < 0)
80         goto fail;
81     if ((ret = avformat_write_header(mpegts_ctx, NULL)) < 0)
82         goto fail;
83     for (i = 0; i < s->nb_streams; i++)
84         s->streams[i]->time_base = mpegts_ctx->streams[i]->time_base;
85
86     chain->mpegts_ctx = mpegts_ctx;
87     mpegts_ctx = NULL;
88
89     rtp_ctx = avformat_alloc_context();
90     if (!rtp_ctx) {
91         ret = AVERROR(ENOMEM);
92         goto fail;
93     }
94     rtp_ctx->oformat = rtp_format;
95     st = avformat_new_stream(rtp_ctx, NULL);
96     if (!st) {
97         ret = AVERROR(ENOMEM);
98         goto fail;
99     }
100     st->time_base.num   = 1;
101     st->time_base.den   = 90000;
102     st->codecpar->codec_id = AV_CODEC_ID_MPEG2TS;
103     rtp_ctx->pb = s->pb;
104     if ((ret = avformat_write_header(rtp_ctx, NULL)) < 0)
105         goto fail;
106     chain->rtp_ctx = rtp_ctx;
107
108     return 0;
109
110 fail:
111     if (mpegts_ctx) {
112         ffio_free_dyn_buf(&mpegts_ctx->pb);
113         av_dict_free(&mpegts_ctx->metadata);
114         avformat_free_context(mpegts_ctx);
115     }
116     avformat_free_context(rtp_ctx);
117     rtp_mpegts_write_close(s);
118     return ret;
119 }
120
121 static int rtp_mpegts_write_packet(AVFormatContext *s, AVPacket *pkt)
122 {
123     MuxChain *chain = s->priv_data;
124     int ret = 0, size;
125     uint8_t *buf;
126     AVPacket *local_pkt = chain->pkt;
127
128     if (!chain->mpegts_ctx->pb) {
129         if ((ret = avio_open_dyn_buf(&chain->mpegts_ctx->pb)) < 0)
130             return ret;
131     }
132     if ((ret = av_write_frame(chain->mpegts_ctx, pkt)) < 0)
133         return ret;
134     size = avio_close_dyn_buf(chain->mpegts_ctx->pb, &buf);
135     chain->mpegts_ctx->pb = NULL;
136     if (size == 0) {
137         av_free(buf);
138         return 0;
139     }
140     av_packet_unref(local_pkt);
141     local_pkt->data         = buf;
142     local_pkt->size         = size;
143     local_pkt->stream_index = 0;
144     if (pkt->pts != AV_NOPTS_VALUE)
145         local_pkt->pts = av_rescale_q(pkt->pts,
146                                      s->streams[pkt->stream_index]->time_base,
147                                      chain->rtp_ctx->streams[0]->time_base);
148     if (pkt->dts != AV_NOPTS_VALUE)
149         local_pkt->dts = av_rescale_q(pkt->dts,
150                                      s->streams[pkt->stream_index]->time_base,
151                                      chain->rtp_ctx->streams[0]->time_base);
152     ret = av_write_frame(chain->rtp_ctx, local_pkt);
153     av_free(buf);
154
155     return ret;
156 }
157
158 AVOutputFormat ff_rtp_mpegts_muxer = {
159     .name              = "rtp_mpegts",
160     .long_name         = NULL_IF_CONFIG_SMALL("RTP/mpegts output format"),
161     .priv_data_size    = sizeof(MuxChain),
162     .audio_codec       = AV_CODEC_ID_AAC,
163     .video_codec       = AV_CODEC_ID_MPEG4,
164     .write_header      = rtp_mpegts_write_header,
165     .write_packet      = rtp_mpegts_write_packet,
166     .write_trailer     = rtp_mpegts_write_close,
167 };