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 "avformat.h"
24 #include "avio_internal.h"
25 
26 struct MuxChain {
29 };
30 
32 {
33  struct MuxChain *chain = s->priv_data;
34 
35  if (chain->mpegts_ctx) {
39  }
40  if (chain->rtp_ctx) {
41  av_write_trailer(chain->rtp_ctx);
43  }
44  return 0;
45 }
46 
48 {
49  struct MuxChain *chain = s->priv_data;
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);
54  AVStream *st;
55 
56  if (!mpegts_format || !rtp_format)
57  return AVERROR(ENOSYS);
59  if (!mpegts_ctx)
60  return AVERROR(ENOMEM);
61  mpegts_ctx->oformat = mpegts_format;
62  mpegts_ctx->max_delay = s->max_delay;
63  for (i = 0; i < s->nb_streams; i++) {
65  if (!st)
66  goto fail;
67  st->time_base = s->streams[i]->time_base;
68  st->sample_aspect_ratio = s->streams[i]->sample_aspect_ratio;
69  avcodec_parameters_copy(st->codecpar, s->streams[i]->codecpar);
70  }
71  if ((ret = avio_open_dyn_buf(&mpegts_ctx->pb)) < 0)
72  goto fail;
74  goto fail;
75  for (i = 0; i < s->nb_streams; i++)
76  s->streams[i]->time_base = mpegts_ctx->streams[i]->time_base;
77 
78  chain->mpegts_ctx = mpegts_ctx;
79  mpegts_ctx = NULL;
80 
82  if (!rtp_ctx) {
83  ret = AVERROR(ENOMEM);
84  goto fail;
85  }
86  rtp_ctx->oformat = rtp_format;
88  if (!st) {
89  ret = AVERROR(ENOMEM);
90  goto fail;
91  }
92  st->time_base.num = 1;
93  st->time_base.den = 90000;
95  rtp_ctx->pb = s->pb;
96  if ((ret = avformat_write_header(rtp_ctx, NULL)) < 0)
97  goto fail;
98  chain->rtp_ctx = rtp_ctx;
99 
100  return 0;
101 
102 fail:
103  if (mpegts_ctx) {
106  }
107  if (rtp_ctx)
110  return ret;
111 }
112 
114 {
115  struct MuxChain *chain = s->priv_data;
116  int ret = 0, size;
117  uint8_t *buf;
118  AVPacket local_pkt;
119 
120  if (!chain->mpegts_ctx->pb) {
121  if ((ret = avio_open_dyn_buf(&chain->mpegts_ctx->pb)) < 0)
122  return ret;
123  }
124  if ((ret = av_write_frame(chain->mpegts_ctx, pkt)) < 0)
125  return ret;
126  size = avio_close_dyn_buf(chain->mpegts_ctx->pb, &buf);
127  chain->mpegts_ctx->pb = NULL;
128  if (size == 0) {
129  av_free(buf);
130  return 0;
131  }
132  av_init_packet(&local_pkt);
133  local_pkt.data = buf;
134  local_pkt.size = size;
135  local_pkt.stream_index = 0;
136  if (pkt->pts != AV_NOPTS_VALUE)
137  local_pkt.pts = av_rescale_q(pkt->pts,
138  s->streams[pkt->stream_index]->time_base,
139  chain->rtp_ctx->streams[0]->time_base);
140  if (pkt->dts != AV_NOPTS_VALUE)
141  local_pkt.dts = av_rescale_q(pkt->dts,
142  s->streams[pkt->stream_index]->time_base,
143  chain->rtp_ctx->streams[0]->time_base);
144  ret = av_write_frame(chain->rtp_ctx, &local_pkt);
145  av_free(buf);
146 
147  return ret;
148 }
149 
151  .name = "rtp_mpegts",
152  .long_name = NULL_IF_CONFIG_SMALL("RTP/mpegts output format"),
153  .priv_data_size = sizeof(struct MuxChain),
154  .audio_codec = AV_CODEC_ID_AAC,
155  .video_codec = AV_CODEC_ID_MPEG4,
156  .write_header = rtp_mpegts_write_header,
157  .write_packet = rtp_mpegts_write_packet,
158  .write_trailer = rtp_mpegts_write_close,
159 };
AVOutputFormat::name
const char * name
Definition: avformat.h:496
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
avformat_new_stream
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:4480
ff_rtp_mpegts_muxer
AVOutputFormat ff_rtp_mpegts_muxer
Definition: rtpenc_mpegts.c:150
AV_CODEC_ID_MPEG4
@ AV_CODEC_ID_MPEG4
Definition: avcodec.h:230
AVFormatContext::streams
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1410
AVPacket::data
uint8_t * data
Definition: avcodec.h:1477
mathematics.h
ff_const59
#define ff_const59
The ff_const59 define is not part of the public API and will be removed without further warning.
Definition: avformat.h:540
av_guess_format
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
fail
#define fail()
Definition: checkasm.h:120
AVRational::num
int num
Numerator.
Definition: rational.h:59
MuxChain::mpegts_ctx
AVFormatContext * mpegts_ctx
Definition: rtpenc_mpegts.c:27
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:1415
buf
void * buf
Definition: avisynth_c.h:766
avio_open_dyn_buf
int avio_open_dyn_buf(AVIOContext **s)
Open a write only memory stream.
Definition: aviobuf.c:1386
s
#define s(width, name)
Definition: cbs_vp9.c:257
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:508
AVFormatContext
Format I/O context.
Definition: avformat.h:1342
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1017
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:899
NULL
#define NULL
Definition: coverity.c:32
AVFormatContext::pb
AVIOContext * pb
I/O context.
Definition: avformat.h:1384
AV_CODEC_ID_MPEG2TS
@ AV_CODEC_ID_MPEG2TS
FAKE codec to indicate a raw MPEG-2 TS stream (only used by libavformat)
Definition: avcodec.h:703
av_write_frame
int av_write_frame(AVFormatContext *s, AVPacket *pkt)
Write a packet to an output media file.
Definition: mux.c:878
rtp_mpegts_write_header
static int rtp_mpegts_write_header(AVFormatContext *s)
Definition: rtpenc_mpegts.c:47
AVFormatContext::oformat
ff_const59 struct AVOutputFormat * oformat
The output container format.
Definition: avformat.h:1361
AV_CODEC_ID_AAC
@ AV_CODEC_ID_AAC
Definition: avcodec.h:566
AVPacket::size
int size
Definition: avcodec.h:1478
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:188
avformat_alloc_context
AVFormatContext * avformat_alloc_context(void)
Allocate an AVFormatContext.
Definition: options.c:144
MuxChain
Definition: rtpenc_mpegts.c:26
size
int size
Definition: twinvq_data.h:11134
AV_NOPTS_VALUE
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
AVStream::sample_aspect_ratio
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown)
Definition: avformat.h:932
AVPacket::dts
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed.
Definition: avcodec.h:1476
rtp_mpegts_write_close
static int rtp_mpegts_write_close(AVFormatContext *s)
Definition: rtpenc_mpegts.c:31
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:1254
AVOutputFormat
Definition: avformat.h:495
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:259
AVPacket::pts
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1470
avio_internal.h
rtp_mpegts_write_packet
static int rtp_mpegts_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: rtpenc_mpegts.c:113
uint8_t
uint8_t
Definition: audio_convert.c:194
AVFormatContext::max_delay
int max_delay
Definition: avformat.h:1467
ffio_free_dyn_buf
void ffio_free_dyn_buf(AVIOContext **s)
Free a dynamic buffer.
Definition: aviobuf.c:1445
ret
ret
Definition: filter_design.txt:187
AVStream
Stream structure.
Definition: avformat.h:870
avformat.h
pkt
static AVPacket pkt
Definition: demuxing_decoding.c:54
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: utils.c:4414
AVPacket::stream_index
int stream_index
Definition: avcodec.h:1479
av_free
#define av_free(p)
Definition: tableprint_vlc.h:34
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:3957
AVPacket
This structure stores compressed data.
Definition: avcodec.h:1454
MuxChain::rtp_ctx
AVFormatContext * rtp_ctx
Definition: rtpenc_mpegts.c:28
avcodec_parameters_copy
int avcodec_parameters_copy(AVCodecParameters *dst, const AVCodecParameters *src)
Copy the contents of src to dst.
Definition: utils.c:2080
av_init_packet
void av_init_packet(AVPacket *pkt)
Initialize optional fields of a packet with default values.
Definition: avpacket.c:33