FFmpeg
rtpenc_mpegts.c
Go to the documentation of this file.
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 "libavutil/opt.h"
24 #include "avformat.h"
25 #include "avio_internal.h"
26 
27 typedef struct MuxChain {
28  const AVClass *class;
34 } MuxChain;
35 
37 {
38  MuxChain *chain = s->priv_data;
39 
40  if (chain->mpegts_ctx) {
44  }
45  if (chain->rtp_ctx) {
46  av_write_trailer(chain->rtp_ctx);
48  }
49 
50  av_packet_free(&chain->pkt);
51 
52  return 0;
53 }
54 
56 {
57  MuxChain *chain = s->priv_data;
59  ff_const59 AVOutputFormat *mpegts_format = av_guess_format("mpegts", NULL, NULL);
60  ff_const59 AVOutputFormat *rtp_format = av_guess_format("rtp", NULL, NULL);
61  int i, ret = AVERROR(ENOMEM);
62  AVStream *st;
65 
66  if (!mpegts_format || !rtp_format)
67  return AVERROR(ENOSYS);
68  mpegts_ctx = avformat_alloc_context();
69  if (!mpegts_ctx)
70  return AVERROR(ENOMEM);
71  chain->pkt = av_packet_alloc();
72  if (!chain->pkt)
73  goto fail;
74  mpegts_ctx->oformat = mpegts_format;
75  mpegts_ctx->max_delay = s->max_delay;
76  av_dict_copy(&mpegts_ctx->metadata, s->metadata, 0);
77  for (i = 0; i < s->nb_streams; i++) {
78  AVStream* st = avformat_new_stream(mpegts_ctx, NULL);
79  if (!st)
80  goto fail;
81  st->time_base = s->streams[i]->time_base;
83  st->id = s->streams[i]->id;
85  }
86  if ((ret = avio_open_dyn_buf(&mpegts_ctx->pb)) < 0)
87  goto fail;
88 
89  av_dict_copy(&mpegts_muxer_options, chain->mpegts_muxer_options, 0);
90 
91  ret = avformat_write_header(mpegts_ctx, &mpegts_muxer_options);
92  av_dict_free(&mpegts_muxer_options);
93  if (ret < 0)
94  goto fail;
95 
96  for (i = 0; i < s->nb_streams; i++)
97  s->streams[i]->time_base = mpegts_ctx->streams[i]->time_base;
98 
99  chain->mpegts_ctx = mpegts_ctx;
100  mpegts_ctx = NULL;
101 
102  rtp_ctx = avformat_alloc_context();
103  if (!rtp_ctx) {
104  ret = AVERROR(ENOMEM);
105  goto fail;
106  }
107  rtp_ctx->oformat = rtp_format;
108  st = avformat_new_stream(rtp_ctx, NULL);
109  if (!st) {
110  ret = AVERROR(ENOMEM);
111  goto fail;
112  }
113  st->time_base.num = 1;
114  st->time_base.den = 90000;
116  rtp_ctx->pb = s->pb;
117  av_dict_copy(&rtp_muxer_options, chain->rtp_muxer_options, 0);
118  ret = avformat_write_header(rtp_ctx, &rtp_muxer_options);
119  av_dict_free(&rtp_muxer_options);
120  if (ret < 0)
121  goto fail;
122 
123  chain->rtp_ctx = rtp_ctx;
124 
125  return 0;
126 
127 fail:
128  if (mpegts_ctx) {
129  ffio_free_dyn_buf(&mpegts_ctx->pb);
130  av_dict_free(&mpegts_ctx->metadata);
131  avformat_free_context(mpegts_ctx);
132  }
133  avformat_free_context(rtp_ctx);
135  return ret;
136 }
137 
139 {
140  MuxChain *chain = s->priv_data;
141  int ret = 0, size;
142  uint8_t *buf;
143  AVPacket *local_pkt = chain->pkt;
144 
145  if (!chain->mpegts_ctx->pb) {
146  if ((ret = avio_open_dyn_buf(&chain->mpegts_ctx->pb)) < 0)
147  return ret;
148  }
149  if ((ret = av_write_frame(chain->mpegts_ctx, pkt)) < 0)
150  return ret;
151  size = avio_close_dyn_buf(chain->mpegts_ctx->pb, &buf);
152  chain->mpegts_ctx->pb = NULL;
153  if (size == 0) {
154  av_free(buf);
155  return 0;
156  }
157  av_packet_unref(local_pkt);
158  local_pkt->data = buf;
159  local_pkt->size = size;
160  local_pkt->stream_index = 0;
161  if (pkt->pts != AV_NOPTS_VALUE)
162  local_pkt->pts = av_rescale_q(pkt->pts,
163  s->streams[pkt->stream_index]->time_base,
164  chain->rtp_ctx->streams[0]->time_base);
165  if (pkt->dts != AV_NOPTS_VALUE)
166  local_pkt->dts = av_rescale_q(pkt->dts,
167  s->streams[pkt->stream_index]->time_base,
168  chain->rtp_ctx->streams[0]->time_base);
169  ret = av_write_frame(chain->rtp_ctx, local_pkt);
170  av_free(buf);
171 
172  return ret;
173 }
174 
175 #define OFFSET(x) offsetof(MuxChain, x)
176 #define E AV_OPT_FLAG_ENCODING_PARAM
177 static const AVOption options[] = {
178  { "mpegts_muxer_options", "set list of options for the MPEG-TS muxer", OFFSET(mpegts_muxer_options), AV_OPT_TYPE_DICT, {.str = NULL}, 0, 0, E },
179  { "rtp_muxer_options", "set list of options for the RTP muxer", OFFSET(rtp_muxer_options), AV_OPT_TYPE_DICT, {.str = NULL}, 0, 0, E },
180  { NULL },
181 };
182 
183 static const AVClass rtp_mpegts_class = {
184  .class_name = "rtp_mpegts muxer",
185  .item_name = av_default_item_name,
186  .option = options,
187  .version = LIBAVUTIL_VERSION_INT,
188 };
189 
191  .name = "rtp_mpegts",
192  .long_name = NULL_IF_CONFIG_SMALL("RTP/mpegts output format"),
193  .priv_data_size = sizeof(MuxChain),
194  .audio_codec = AV_CODEC_ID_AAC,
195  .video_codec = AV_CODEC_ID_MPEG4,
199  .priv_class = &rtp_mpegts_class,
200 };
AVOutputFormat ff_rtp_mpegts_muxer
static void write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, int unqueue)
Definition: ffmpeg.c:729
#define NULL
Definition: coverity.c:32
int avio_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
Return the written size and a pointer to the buffer.
Definition: aviobuf.c:1424
AVOption.
Definition: opt.h:248
int av_write_frame(AVFormatContext *s, AVPacket *pkt)
Write a packet to an output media file.
Definition: mux.c:1212
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
#define OFFSET(x)
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:60
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown)
Definition: avformat.h:946
int num
Numerator.
Definition: rational.h:59
int size
Definition: packet.h:370
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:235
int av_dict_copy(AVDictionary **dst, const AVDictionary *src, int flags)
Copy entries from one AVDictionary struct into another.
Definition: dict.c:217
int avio_open_dyn_buf(AVIOContext **s)
Open a write only memory stream.
Definition: aviobuf.c:1379
Format I/O context.
Definition: avformat.h:1247
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:72
void av_packet_free(AVPacket **pkt)
Free the packet, if the packet is reference counted, it will be unreferenced first.
Definition: avpacket.c:75
uint8_t
AVOptions.
AVDictionary * rtp_muxer_options
Definition: rtpenc_mpegts.c:33
static int rtp_mpegts_write_header(AVFormatContext *s)
Definition: rtpenc_mpegts.c:55
int id
Format-specific stream ID.
Definition: avformat.h:891
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:4553
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1315
AVFormatContext * avformat_alloc_context(void)
Allocate an AVFormatContext.
Definition: options.c:211
uint8_t * data
Definition: packet.h:369
ptrdiff_t size
Definition: opengl_enc.c:100
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:142
static const AVClass rtp_mpegts_class
#define ff_const59
The ff_const59 define is not part of the public API and will be removed without further warning...
Definition: avformat.h:544
AVDictionary * metadata
Metadata that applies to the whole file.
Definition: avformat.h:1489
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:117
void av_dict_free(AVDictionary **pm)
Free all the memory allocated for an AVDictionary struct and all keys and values. ...
Definition: dict.c:203
static int rtp_mpegts_write_close(AVFormatContext *s)
Definition: rtpenc_mpegts.c:36
#define fail()
Definition: checkasm.h:133
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:1303
av_warn_unused_result int avformat_write_header(AVFormatContext *s, AVDictionary **options)
Allocate the stream private data and write the stream header to an output media file.
Definition: mux.c:506
static int rtp_mpegts_write_packet(AVFormatContext *s, AVPacket *pkt)
static int write_trailer(AVFormatContext *s1)
Definition: v4l2enc.c:98
const char * name
Definition: avformat.h:500
static const AVOption options[]
#define s(width, name)
Definition: cbs_vp9.c:257
ff_const59 struct AVOutputFormat * oformat
The output container format.
Definition: avformat.h:1266
void ffio_free_dyn_buf(AVIOContext **s)
Free a dynamic buffer.
Definition: aviobuf.c:1454
Stream structure.
Definition: avformat.h:884
AVIOContext * pb
I/O context.
Definition: avformat.h:1289
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:634
Describe the class of an AVClass context structure.
Definition: log.h:67
ff_const59 AVOutputFormat * av_guess_format(const char *short_name, const char *filename, const char *mime_type)
Return the output format in the list of registered output formats which best matches the provided par...
Definition: format.c:51
void avformat_free_context(AVFormatContext *s)
Free an AVFormatContext and all its streams.
Definition: utils.c:4480
Main libavformat public API header.
AVDictionary * mpegts_muxer_options
Definition: rtpenc_mpegts.c:32
int avcodec_parameters_copy(AVCodecParameters *dst, const AVCodecParameters *src)
Copy the contents of src to dst.
Definition: codec_par.c:72
#define E
int den
Denominator.
Definition: rational.h:60
FAKE codec to indicate a raw MPEG-2 TS stream (only used by libavformat)
Definition: codec_id.h:569
#define av_free(p)
void * priv_data
Format private data.
Definition: avformat.h:1275
AVPacket * av_packet_alloc(void)
Allocate an AVPacket and set its fields to default values.
Definition: avpacket.c:64
static void write_header(FFV1Context *f)
Definition: ffv1enc.c:346
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
Definition: packet.h:368
int av_write_trailer(AVFormatContext *s)
Write the stream trailer to an output media file and free the file private data.
Definition: mux.c:1274
AVFormatContext * mpegts_ctx
Definition: rtpenc_mpegts.c:29
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1049
int stream_index
Definition: packet.h:371
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avformat.h:913
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later.That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another.Frame references ownership and permissions
This structure stores compressed data.
Definition: packet.h:346
AVFormatContext * rtp_ctx
Definition: rtpenc_mpegts.c:30
AVPacket * pkt
Definition: rtpenc_mpegts.c:31
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:362
int i
Definition: input.c:407
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248