FFmpeg
Loading...
Searching...
No Matches
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
24#include "libavutil/mem.h"
25#include "libavutil/opt.h"
26#include "avformat.h"
27#include "avio_internal.h"
28#include "mux.h"
29
38
40{
41 MuxChain *chain = s->priv_data;
42
43 if (chain->mpegts_ctx) {
47 }
48 if (chain->rtp_ctx) {
51 }
52
53 av_packet_free(&chain->pkt);
54
55 return 0;
56}
57
59{
60 MuxChain *chain = s->priv_data;
61 AVFormatContext *mpegts_ctx = NULL, *rtp_ctx = NULL;
62 int i, ret = AVERROR(ENOMEM);
63 AVStream *new_st;
64 AVDictionary *mpegts_muxer_options = NULL;
65 AVDictionary *rtp_muxer_options = NULL;
66
67 mpegts_ctx = avformat_alloc_context();
68 if (!mpegts_ctx)
69 return AVERROR(ENOMEM);
70 chain->pkt = av_packet_alloc();
71 if (!chain->pkt)
72 goto fail;
74 mpegts_ctx->oformat = &ff_mpegts_muxer.p;
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;
82 st->sample_aspect_ratio = s->streams[i]->sample_aspect_ratio;
83 st->id = s->streams[i]->id;
84 avcodec_parameters_copy(st->codecpar, s->streams[i]->codecpar);
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 }
108 rtp_ctx->oformat = &ff_rtp_muxer.p;
109 new_st = avformat_new_stream(rtp_ctx, NULL);
110 if (!new_st) {
111 ret = AVERROR(ENOMEM);
112 goto fail;
113 }
114 new_st->time_base.num = 1;
115 new_st->time_base.den = 90000;
117 rtp_ctx->pb = s->pb;
118 av_dict_copy(&rtp_muxer_options, chain->rtp_muxer_options, 0);
119 ret = avformat_write_header(rtp_ctx, &rtp_muxer_options);
120 av_dict_free(&rtp_muxer_options);
121 if (ret < 0)
122 goto fail;
123
124 chain->rtp_ctx = rtp_ctx;
125
126 return 0;
127
128fail:
129 if (mpegts_ctx) {
130 ffio_free_dyn_buf(&mpegts_ctx->pb);
131 av_dict_free(&mpegts_ctx->metadata);
132 avformat_free_context(mpegts_ctx);
133 }
134 avformat_free_context(rtp_ctx);
136 return ret;
137}
138
140{
141 MuxChain *chain = s->priv_data;
142 int ret = 0, size;
143 uint8_t *buf;
144 AVPacket *local_pkt = chain->pkt;
145
146 if (!chain->mpegts_ctx->pb) {
147 if ((ret = avio_open_dyn_buf(&chain->mpegts_ctx->pb)) < 0)
148 return ret;
149 }
150 if ((ret = av_write_frame(chain->mpegts_ctx, pkt)) < 0)
151 return ret;
152 size = avio_close_dyn_buf(chain->mpegts_ctx->pb, &buf);
153 chain->mpegts_ctx->pb = NULL;
154 if (size == 0) {
155 av_free(buf);
156 return 0;
157 }
158 av_packet_unref(local_pkt);
159 local_pkt->data = buf;
160 local_pkt->size = size;
161 local_pkt->stream_index = 0;
162 if (pkt->pts != AV_NOPTS_VALUE)
163 local_pkt->pts = av_rescale_q(pkt->pts,
164 s->streams[pkt->stream_index]->time_base,
165 chain->rtp_ctx->streams[0]->time_base);
166 if (pkt->dts != AV_NOPTS_VALUE)
167 local_pkt->dts = av_rescale_q(pkt->dts,
168 s->streams[pkt->stream_index]->time_base,
169 chain->rtp_ctx->streams[0]->time_base);
170 ret = av_write_frame(chain->rtp_ctx, local_pkt);
171 av_free(buf);
172
173 return ret;
174}
175
176#define OFFSET(x) offsetof(MuxChain, x)
177#define E AV_OPT_FLAG_ENCODING_PARAM
178static const AVOption options[] = {
179 { "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 },
180 { "rtp_muxer_options", "set list of options for the RTP muxer", OFFSET(rtp_muxer_options), AV_OPT_TYPE_DICT, {.str = NULL}, 0, 0, E },
181 { NULL },
182};
183
184static const AVClass rtp_mpegts_class = {
185 .class_name = "rtp_mpegts muxer",
186 .item_name = av_default_item_name,
187 .option = options,
188 .version = LIBAVUTIL_VERSION_INT,
189};
190
192 .p.name = "rtp_mpegts",
193 .p.long_name = NULL_IF_CONFIG_SMALL("RTP/mpegts output format"),
194 .priv_data_size = sizeof(MuxChain),
195 .p.audio_codec = AV_CODEC_ID_AAC,
196 .p.video_codec = AV_CODEC_ID_MPEG4,
197 .write_header = rtp_mpegts_write_header,
198 .write_packet = rtp_mpegts_write_packet,
199 .write_trailer = rtp_mpegts_write_close,
200 .p.priv_class = &rtp_mpegts_class,
201};
const FFOutputFormat ff_mpegts_muxer
Definition mpegtsenc.c:2455
const FFOutputFormat ff_rtp_muxer
Definition rtpenc.c:701
const FFOutputFormat ff_rtp_mpegts_muxer
#define EXTERN
#define E
Definition avdct.c:34
Main libavformat public API header.
int avio_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
Return the written size and a pointer to the buffer.
Definition aviobuf.c:1410
int avio_open_dyn_buf(AVIOContext **s)
Open a write only memory stream.
Definition aviobuf.c:1365
void ffio_free_dyn_buf(AVIOContext **s)
Free a dynamic buffer.
Definition aviobuf.c:1438
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
int avcodec_parameters_copy(AVCodecParameters *dst, const AVCodecParameters *src)
Definition codec_par.c:107
#define NULL
Definition coverity.c:32
static AVPacket * pkt
#define fail
Definition test.h:479
@ AV_OPT_TYPE_DICT
Underlying C type is AVDictionary*.
Definition opt.h:289
@ AV_CODEC_ID_MPEG2TS
FAKE codec to indicate a raw MPEG-2 TS stream (only used by libavformat)
Definition codec_id.h:616
@ AV_CODEC_ID_AAC
Definition codec_id.h:455
@ AV_CODEC_ID_MPEG4
Definition codec_id.h:62
void av_packet_free(AVPacket **pkt)
Free the packet, if the packet is reference counted, it will be unreferenced first.
Definition packet.c:74
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition packet.c:434
AVPacket * av_packet_alloc(void)
Allocate an AVPacket and set its fields to default values.
Definition packet.c:63
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
AVFormatContext * avformat_alloc_context(void)
Allocate an AVFormatContext.
Definition options.c:165
void avformat_free_context(AVFormatContext *s)
Free an AVFormatContext and all its streams.
Definition avformat.c:150
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:467
int av_write_trailer(AVFormatContext *s)
Write the stream trailer to an output media file and free the file private data.
Definition mux.c:1238
int av_write_frame(AVFormatContext *s, AVPacket *pkt)
Write a packet to an output media file.
Definition mux.c:1176
void av_dict_free(AVDictionary **pm)
Free all the memory allocated for an AVDictionary struct and all keys and values.
Definition dict.c:233
int av_dict_copy(AVDictionary **dst, const AVDictionary *src, int flags)
Copy entries from one AVDictionary struct into another.
Definition dict.c:247
#define AVERROR(e)
Definition error.h:45
const char * av_default_item_name(void *ptr)
Return the context name.
Definition log.c:241
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition avutil.h:247
#define LIBAVUTIL_VERSION_INT
Definition version.h:85
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
Memory handling functions.
AVOptions.
static const AVClass rtp_mpegts_class
static int rtp_mpegts_write_header(AVFormatContext *s)
static int rtp_mpegts_write_close(AVFormatContext *s)
#define OFFSET(x)
static int rtp_mpegts_write_packet(AVFormatContext *s, AVPacket *pkt)
Describe the class of an AVClass context structure.
Definition log.h:76
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition codec_par.h:57
Format I/O context.
Definition avformat.h:1333
AVIOContext * pb
I/O context.
Definition avformat.h:1375
AVDictionary * metadata
Metadata that applies to the whole file.
Definition avformat.h:1580
const struct AVOutputFormat * oformat
The output container format.
Definition avformat.h:1352
AVStream ** streams
A list of all streams in the file.
Definition avformat.h:1401
AVOption.
Definition opt.h:428
This structure stores compressed data.
Definition packet.h:580
int stream_index
Definition packet.h:605
int size
Definition packet.h:604
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition packet.h:596
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed.
Definition packet.h:602
uint8_t * data
Definition packet.h:603
int num
Numerator.
Definition rational.h:59
int den
Denominator.
Definition rational.h:60
Stream structure.
Definition avformat.h:766
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition avformat.h:789
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown)
Definition avformat.h:844
int id
Format-specific stream ID.
Definition avformat.h:778
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented.
Definition avformat.h:805
AVFormatContext * mpegts_ctx
AVDictionary * mpegts_muxer_options
AVPacket * pkt
AVFormatContext * rtp_ctx
AVDictionary * rtp_muxer_options
#define av_free(p)
int size