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/mem.h"
24 #include "libavutil/opt.h"
25 #include "avformat.h"
26 #include "avio_internal.h"
27 #include "mux.h"
28 
29 typedef struct MuxChain {
30  const AVClass *class;
36 } MuxChain;
37 
39 {
40  MuxChain *chain = s->priv_data;
41 
42  if (chain->mpegts_ctx) {
46  }
47  if (chain->rtp_ctx) {
48  av_write_trailer(chain->rtp_ctx);
50  }
51 
52  av_packet_free(&chain->pkt);
53 
54  return 0;
55 }
56 
58 {
59  MuxChain *chain = s->priv_data;
60  AVFormatContext *mpegts_ctx = NULL, *rtp_ctx = NULL;
61  const AVOutputFormat *mpegts_format = av_guess_format("mpegts", NULL, NULL);
62  const AVOutputFormat *rtp_format = av_guess_format("rtp", NULL, NULL);
63  int i, ret = AVERROR(ENOMEM);
64  AVStream *st;
65  AVDictionary *mpegts_muxer_options = NULL;
66  AVDictionary *rtp_muxer_options = NULL;
67 
68  if (!mpegts_format || !rtp_format)
69  return AVERROR(ENOSYS);
70  mpegts_ctx = avformat_alloc_context();
71  if (!mpegts_ctx)
72  return AVERROR(ENOMEM);
73  chain->pkt = av_packet_alloc();
74  if (!chain->pkt)
75  goto fail;
76  mpegts_ctx->oformat = mpegts_format;
77  mpegts_ctx->max_delay = s->max_delay;
78  av_dict_copy(&mpegts_ctx->metadata, s->metadata, 0);
79  for (i = 0; i < s->nb_streams; i++) {
80  AVStream* st = avformat_new_stream(mpegts_ctx, NULL);
81  if (!st)
82  goto fail;
83  st->time_base = s->streams[i]->time_base;
84  st->sample_aspect_ratio = s->streams[i]->sample_aspect_ratio;
85  st->id = s->streams[i]->id;
86  avcodec_parameters_copy(st->codecpar, s->streams[i]->codecpar);
87  }
88  if ((ret = avio_open_dyn_buf(&mpegts_ctx->pb)) < 0)
89  goto fail;
90 
91  av_dict_copy(&mpegts_muxer_options, chain->mpegts_muxer_options, 0);
92 
93  ret = avformat_write_header(mpegts_ctx, &mpegts_muxer_options);
94  av_dict_free(&mpegts_muxer_options);
95  if (ret < 0)
96  goto fail;
97 
98  for (i = 0; i < s->nb_streams; i++)
99  s->streams[i]->time_base = mpegts_ctx->streams[i]->time_base;
100 
101  chain->mpegts_ctx = mpegts_ctx;
102  mpegts_ctx = NULL;
103 
104  rtp_ctx = avformat_alloc_context();
105  if (!rtp_ctx) {
106  ret = AVERROR(ENOMEM);
107  goto fail;
108  }
109  rtp_ctx->oformat = rtp_format;
110  st = avformat_new_stream(rtp_ctx, NULL);
111  if (!st) {
112  ret = AVERROR(ENOMEM);
113  goto fail;
114  }
115  st->time_base.num = 1;
116  st->time_base.den = 90000;
118  rtp_ctx->pb = s->pb;
119  av_dict_copy(&rtp_muxer_options, chain->rtp_muxer_options, 0);
120  ret = avformat_write_header(rtp_ctx, &rtp_muxer_options);
121  av_dict_free(&rtp_muxer_options);
122  if (ret < 0)
123  goto fail;
124 
125  chain->rtp_ctx = rtp_ctx;
126 
127  return 0;
128 
129 fail:
130  if (mpegts_ctx) {
131  ffio_free_dyn_buf(&mpegts_ctx->pb);
132  av_dict_free(&mpegts_ctx->metadata);
133  avformat_free_context(mpegts_ctx);
134  }
135  avformat_free_context(rtp_ctx);
137  return ret;
138 }
139 
141 {
142  MuxChain *chain = s->priv_data;
143  int ret = 0, size;
144  uint8_t *buf;
145  AVPacket *local_pkt = chain->pkt;
146 
147  if (!chain->mpegts_ctx->pb) {
148  if ((ret = avio_open_dyn_buf(&chain->mpegts_ctx->pb)) < 0)
149  return ret;
150  }
151  if ((ret = av_write_frame(chain->mpegts_ctx, pkt)) < 0)
152  return ret;
153  size = avio_close_dyn_buf(chain->mpegts_ctx->pb, &buf);
154  chain->mpegts_ctx->pb = NULL;
155  if (size == 0) {
156  av_free(buf);
157  return 0;
158  }
159  av_packet_unref(local_pkt);
160  local_pkt->data = buf;
161  local_pkt->size = size;
162  local_pkt->stream_index = 0;
163  if (pkt->pts != AV_NOPTS_VALUE)
164  local_pkt->pts = av_rescale_q(pkt->pts,
165  s->streams[pkt->stream_index]->time_base,
166  chain->rtp_ctx->streams[0]->time_base);
167  if (pkt->dts != AV_NOPTS_VALUE)
168  local_pkt->dts = av_rescale_q(pkt->dts,
169  s->streams[pkt->stream_index]->time_base,
170  chain->rtp_ctx->streams[0]->time_base);
171  ret = av_write_frame(chain->rtp_ctx, local_pkt);
172  av_free(buf);
173 
174  return ret;
175 }
176 
177 #define OFFSET(x) offsetof(MuxChain, x)
178 #define E AV_OPT_FLAG_ENCODING_PARAM
179 static const AVOption options[] = {
180  { "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 },
181  { "rtp_muxer_options", "set list of options for the RTP muxer", OFFSET(rtp_muxer_options), AV_OPT_TYPE_DICT, {.str = NULL}, 0, 0, E },
182  { NULL },
183 };
184 
185 static const AVClass rtp_mpegts_class = {
186  .class_name = "rtp_mpegts muxer",
187  .item_name = av_default_item_name,
188  .option = options,
189  .version = LIBAVUTIL_VERSION_INT,
190 };
191 
193  .p.name = "rtp_mpegts",
194  .p.long_name = NULL_IF_CONFIG_SMALL("RTP/mpegts output format"),
195  .priv_data_size = sizeof(MuxChain),
196  .p.audio_codec = AV_CODEC_ID_AAC,
197  .p.video_codec = AV_CODEC_ID_MPEG4,
198  .write_header = rtp_mpegts_write_header,
199  .write_packet = rtp_mpegts_write_packet,
200  .write_trailer = rtp_mpegts_write_close,
201  .p.priv_class = &rtp_mpegts_class,
202 };
av_packet_unref
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: packet.c:427
AVOutputFormat::name
const char * name
Definition: avformat.h:510
AVERROR
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
opt.h
avformat_new_stream
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
rtp_mpegts_class
static const AVClass rtp_mpegts_class
Definition: rtpenc_mpegts.c:185
AV_CODEC_ID_MPEG4
@ AV_CODEC_ID_MPEG4
Definition: codec_id.h:64
AVFormatContext::streams
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1323
AVPacket::data
uint8_t * data
Definition: packet.h:524
AVOption
AVOption.
Definition: opt.h:346
mathematics.h
AVDictionary
Definition: dict.c:34
av_packet_free
void av_packet_free(AVPacket **pkt)
Free the packet, if the packet is reference counted, it will be unreferenced first.
Definition: packet.c:74
FFOutputFormat::p
AVOutputFormat p
The public AVOutputFormat.
Definition: mux.h:65
ff_rtp_mpegts_muxer
const FFOutputFormat ff_rtp_mpegts_muxer
Definition: rtpenc_mpegts.c:192
fail
#define fail()
Definition: checkasm.h:179
AVRational::num
int num
Numerator.
Definition: rational.h:59
MuxChain::mpegts_ctx
AVFormatContext * mpegts_ctx
Definition: rtpenc_mpegts.c:31
MuxChain::rtp_muxer_options
AVDictionary * rtp_muxer_options
Definition: rtpenc_mpegts.c:35
avio_close_dyn_buf
int avio_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
Return the written size and a pointer to the buffer.
Definition: aviobuf.c:1407
pkt
AVPacket * pkt
Definition: movenc.c:60
AVFormatContext::metadata
AVDictionary * metadata
Metadata that applies to the whole file.
Definition: avformat.h:1490
avio_open_dyn_buf
int avio_open_dyn_buf(AVIOContext **s)
Open a write only memory stream.
Definition: aviobuf.c:1362
s
#define s(width, name)
Definition: cbs_vp9.c:198
OFFSET
#define OFFSET(x)
Definition: rtpenc_mpegts.c:177
av_rescale_q
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
avformat_write_header
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:487
AVFormatContext
Format I/O context.
Definition: avformat.h:1255
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:766
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
AVStream::time_base
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented.
Definition: avformat.h:782
NULL
#define NULL
Definition: coverity.c:32
AV_OPT_TYPE_DICT
@ AV_OPT_TYPE_DICT
Definition: opt.h:242
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:237
AVFormatContext::pb
AVIOContext * pb
I/O context.
Definition: avformat.h:1297
FFOutputFormat
Definition: mux.h:61
AV_CODEC_ID_MPEG2TS
@ AV_CODEC_ID_MPEG2TS
FAKE codec to indicate a raw MPEG-2 TS stream (only used by libavformat)
Definition: codec_id.h:596
av_write_frame
int av_write_frame(AVFormatContext *s, AVPacket *pkt)
Write a packet to an output media file.
Definition: mux.c:1233
rtp_mpegts_write_header
static int rtp_mpegts_write_header(AVFormatContext *s)
Definition: rtpenc_mpegts.c:57
MuxChain::mpegts_muxer_options
AVDictionary * mpegts_muxer_options
Definition: rtpenc_mpegts.c:34
options
static const AVOption options[]
Definition: rtpenc_mpegts.c:179
AV_CODEC_ID_AAC
@ AV_CODEC_ID_AAC
Definition: codec_id.h:442
AVPacket::size
int size
Definition: packet.h:525
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:94
avformat_alloc_context
AVFormatContext * avformat_alloc_context(void)
Allocate an AVFormatContext.
Definition: options.c:161
MuxChain
Definition: rtpenc_mpegts.c:29
size
int size
Definition: twinvq_data.h:10344
AV_NOPTS_VALUE
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
MuxChain::pkt
AVPacket * pkt
Definition: rtpenc_mpegts.c:33
AVStream::sample_aspect_ratio
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown)
Definition: avformat.h:821
AVPacket::dts
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed.
Definition: packet.h:523
rtp_mpegts_write_close
static int rtp_mpegts_write_close(AVFormatContext *s)
Definition: rtpenc_mpegts.c:38
av_packet_alloc
AVPacket * av_packet_alloc(void)
Allocate an AVPacket and set its fields to default values.
Definition: packet.c:63
av_dict_free
void av_dict_free(AVDictionary **pm)
Free all the memory allocated for an AVDictionary struct and all keys and values.
Definition: dict.c:223
av_write_trailer
int av_write_trailer(AVFormatContext *s)
Write the stream trailer to an output media file and free the file private data.
Definition: mux.c:1295
AVOutputFormat
Definition: avformat.h:509
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
AVPacket::pts
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:517
avio_internal.h
rtp_mpegts_write_packet
static int rtp_mpegts_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: rtpenc_mpegts.c:140
AVFormatContext::max_delay
int max_delay
Definition: avformat.h:1400
ffio_free_dyn_buf
void ffio_free_dyn_buf(AVIOContext **s)
Free a dynamic buffer.
Definition: aviobuf.c:1435
AVStream::id
int id
Format-specific stream ID.
Definition: avformat.h:755
ret
ret
Definition: filter_design.txt:187
AVStream
Stream structure.
Definition: avformat.h:743
AVClass::class_name
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:71
AVFormatContext::oformat
const struct AVOutputFormat * oformat
The output container format.
Definition: avformat.h:1274
avformat.h
E
#define E
Definition: rtpenc_mpegts.c:178
AVRational::den
int den
Denominator.
Definition: rational.h:60
avformat_free_context
void avformat_free_context(AVFormatContext *s)
Free an AVFormatContext and all its streams.
Definition: avformat.c:141
AVPacket::stream_index
int stream_index
Definition: packet.h:526
av_guess_format
const 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:79
mem.h
av_free
#define av_free(p)
Definition: tableprint_vlc.h:33
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:55
AVPacket
This structure stores compressed data.
Definition: packet.h:501
av_dict_copy
int av_dict_copy(AVDictionary **dst, const AVDictionary *src, int flags)
Copy entries from one AVDictionary struct into another.
Definition: dict.c:237
MuxChain::rtp_ctx
AVFormatContext * rtp_ctx
Definition: rtpenc_mpegts.c:32
avcodec_parameters_copy
int avcodec_parameters_copy(AVCodecParameters *dst, const AVCodecParameters *src)
Copy the contents of src to dst.
Definition: codec_par.c:106
mux.h