FFmpeg
rtpdec_qt.c
Go to the documentation of this file.
1 /*
2  * RTP/Quicktime support.
3  * Copyright (c) 2009 Ronald S. Bultje
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 /**
23  * @file
24  * @brief Quicktime-style RTP support
25  * @author Ronald S. Bultje <rbultje@ronald.bitfreak.net>
26  */
27 
28 #include "avformat.h"
29 #include "internal.h"
30 #include "avio_internal.h"
31 #include "rtp.h"
32 #include "rtpdec.h"
33 #include "isom.h"
34 #include "libavcodec/get_bits.h"
35 
36 struct PayloadContext {
39  uint32_t timestamp;
40 };
41 
42 static av_cold int qt_rtp_init(AVFormatContext *ctx, int st_index,
43  PayloadContext *qt)
44 {
45  qt->pkt = av_packet_alloc();
46  if (!qt->pkt)
47  return AVERROR(ENOMEM);
48 
49  return 0;
50 }
51 
53  AVStream *st, AVPacket *pkt,
54  uint32_t *timestamp, const uint8_t *buf,
55  int len, uint16_t seq, int flags)
56 {
57  FFIOContext pb0;
58  AVIOContext *const pb = &pb0.pub;
59  GetBitContext gb;
60  int packing_scheme, has_payload_desc, has_packet_info, alen,
61  has_marker_bit = flags & RTP_FLAG_MARKER,
62  keyframe, ret;
63 
64  if (qt->remaining) {
65  int num = qt->pkt->size / qt->bytes_per_frame;
66 
67  if ((ret = av_new_packet(pkt, qt->bytes_per_frame)) < 0)
68  return ret;
69  pkt->stream_index = st->index;
70  pkt->flags = qt->pkt->flags;
71  memcpy(pkt->data,
72  &qt->pkt->data[(num - qt->remaining) * qt->bytes_per_frame],
73  qt->bytes_per_frame);
74  if (--qt->remaining == 0) {
75  av_freep(&qt->pkt->data);
76  qt->pkt->size = 0;
77  }
78  return qt->remaining > 0;
79  }
80 
81  /**
82  * The RTP payload is described in:
83  * http://developer.apple.com/quicktime/icefloe/dispatch026.html
84  */
85  init_get_bits(&gb, buf, len << 3);
86  ffio_init_context(&pb0, (uint8_t*)buf, len, 0, NULL, NULL, NULL, NULL);
87 
88  if (len < 4)
89  return AVERROR_INVALIDDATA;
90 
91  skip_bits(&gb, 4); // version
92  if ((packing_scheme = get_bits(&gb, 2)) == 0)
93  return AVERROR_INVALIDDATA;
94  keyframe = get_bits1(&gb);
95  has_payload_desc = get_bits1(&gb);
96  has_packet_info = get_bits1(&gb);
97  skip_bits(&gb, 23); // reserved:7, cache payload info:1, payload ID:15
98 
99  if (has_payload_desc) {
100  int data_len, pos, is_start, is_finish;
101  uint32_t tag;
102 
103  pos = get_bits_count(&gb) >> 3;
104  if (pos + 12 > len)
105  return AVERROR_INVALIDDATA;
106 
107  skip_bits(&gb, 2); // has non-I-frames:1, is sparse:1
108  is_start = get_bits1(&gb);
109  is_finish = get_bits1(&gb);
110  if (!is_start || !is_finish) {
111  avpriv_request_sample(s, "RTP-X-QT with payload description "
112  "split over several packets");
113  return AVERROR_PATCHWELCOME;
114  }
115  skip_bits(&gb, 12); // reserved
116  data_len = get_bits(&gb, 16);
117 
118  avio_seek(pb, pos + 4, SEEK_SET);
119  tag = avio_rl32(pb);
120  if ((st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
121  tag != MKTAG('v','i','d','e')) ||
123  tag != MKTAG('s','o','u','n')))
124  return AVERROR_INVALIDDATA;
125  avpriv_set_pts_info(st, 32, 1, avio_rb32(pb));
126 
127  if (pos + data_len > len)
128  return AVERROR_INVALIDDATA;
129  /* TLVs */
130  while (avio_tell(pb) + 4 < pos + data_len) {
131  int tlv_len = avio_rb16(pb);
132  tag = avio_rl16(pb);
133  if (avio_tell(pb) + tlv_len > pos + data_len)
134  return AVERROR_INVALIDDATA;
135 
136 #define MKTAG16(a,b) MKTAG(a,b,0,0)
137  switch (tag) {
138  case MKTAG16('s','d'): {
139  MOVStreamContext *msc;
140  void *priv_data = st->priv_data;
141  int nb_streams = s->nb_streams;
142  MOVContext *mc = av_mallocz(sizeof(*mc));
143  if (!mc)
144  return AVERROR(ENOMEM);
145  mc->fc = s;
146  st->priv_data = msc = av_mallocz(sizeof(MOVStreamContext));
147  if (!msc) {
148  av_free(mc);
149  st->priv_data = priv_data;
150  return AVERROR(ENOMEM);
151  }
152  /* ff_mov_read_stsd_entries updates stream s->nb_streams-1,
153  * so set it temporarily to indicate which stream to update. */
154  s->nb_streams = st->index + 1;
156  qt->bytes_per_frame = msc->bytes_per_frame;
157  av_free(msc);
158  av_free(mc);
159  st->priv_data = priv_data;
160  s->nb_streams = nb_streams;
161  break;
162  }
163  default:
164  avio_skip(pb, tlv_len);
165  break;
166  }
167  }
168 
169  /* 32-bit alignment */
170  avio_skip(pb, ((avio_tell(pb) + 3) & ~3) - avio_tell(pb));
171  } else
172  avio_seek(pb, 4, SEEK_SET);
173 
174  if (has_packet_info) {
175  avpriv_request_sample(s, "RTP-X-QT with packet-specific info");
176  return AVERROR_PATCHWELCOME;
177  }
178 
179  alen = len - avio_tell(pb);
180  if (alen <= 0)
181  return AVERROR_INVALIDDATA;
182 
183  switch (packing_scheme) {
184  case 3: /* one data packet spread over 1 or multiple RTP packets */
185  if (qt->pkt->size > 0 && qt->timestamp == *timestamp) {
186  int err;
187  if ((err = av_reallocp(&qt->pkt->data, qt->pkt->size + alen +
189  qt->pkt->size = 0;
190  return err;
191  }
192  } else {
193  av_freep(&qt->pkt->data);
194  av_packet_unref(qt->pkt);
196  if (!qt->pkt->data)
197  return AVERROR(ENOMEM);
198  qt->pkt->size = 0;
199  qt->timestamp = *timestamp;
200  }
201  memcpy(qt->pkt->data + qt->pkt->size, buf + avio_tell(pb), alen);
202  qt->pkt->size += alen;
203  if (has_marker_bit) {
204  int ret = av_packet_from_data(pkt, qt->pkt->data, qt->pkt->size);
205  if (ret < 0)
206  return ret;
207 
208  qt->pkt->size = 0;
209  qt->pkt->data = NULL;
210  pkt->flags = keyframe ? AV_PKT_FLAG_KEY : 0;
211  pkt->stream_index = st->index;
212  memset(pkt->data + pkt->size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
213  return 0;
214  }
215  return AVERROR(EAGAIN);
216 
217  case 1: /* constant packet size, multiple packets per RTP packet */
218  if (qt->bytes_per_frame == 0 ||
219  alen % qt->bytes_per_frame != 0)
220  return AVERROR_INVALIDDATA; /* wrongly padded */
221  qt->remaining = (alen / qt->bytes_per_frame) - 1;
222  if ((ret = av_new_packet(pkt, qt->bytes_per_frame)) < 0)
223  return ret;
224  memcpy(pkt->data, buf + avio_tell(pb), qt->bytes_per_frame);
225  pkt->flags = keyframe ? AV_PKT_FLAG_KEY : 0;
226  pkt->stream_index = st->index;
227  if (qt->remaining > 0) {
228  av_freep(&qt->pkt->data);
229  qt->pkt->data = av_realloc(NULL, qt->remaining * qt->bytes_per_frame);
230  if (!qt->pkt->data) {
232  return AVERROR(ENOMEM);
233  }
234  qt->pkt->size = qt->remaining * qt->bytes_per_frame;
235  memcpy(qt->pkt->data,
236  buf + avio_tell(pb) + qt->bytes_per_frame,
237  qt->remaining * qt->bytes_per_frame);
238  qt->pkt->flags = pkt->flags;
239  return 1;
240  }
241  return 0;
242 
243  default: /* unimplemented */
244  avpriv_request_sample(NULL, "RTP-X-QT with packing scheme 2");
245  return AVERROR_PATCHWELCOME;
246  }
247 }
248 
249 static void qt_rtp_close(PayloadContext *qt)
250 {
251  av_freep(&qt->pkt->data);
252  av_packet_free(&qt->pkt);
253 }
254 
255 #define RTP_QT_HANDLER(m, n, s, t) \
256 const RTPDynamicProtocolHandler ff_ ## m ## _rtp_ ## n ## _handler = { \
257  .enc_name = s, \
258  .codec_type = t, \
259  .codec_id = AV_CODEC_ID_NONE, \
260  .priv_data_size = sizeof(PayloadContext), \
261  .init = qt_rtp_init, \
262  .close = qt_rtp_close, \
263  .parse_packet = qt_rtp_parse_packet, \
264 }
265 
266 RTP_QT_HANDLER(qt, vid, "X-QT", AVMEDIA_TYPE_VIDEO);
268 RTP_QT_HANDLER(quicktime, vid, "X-QUICKTIME", AVMEDIA_TYPE_VIDEO);
269 RTP_QT_HANDLER(quicktime, aud, "X-QUICKTIME", AVMEDIA_TYPE_AUDIO);
av_packet_unref
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:417
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
AVCodecParameters::codec_type
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:56
AVStream::priv_data
void * priv_data
Definition: avformat.h:854
MOVStreamContext
Definition: isom.h:158
get_bits_count
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:220
RTP_FLAG_MARKER
#define RTP_FLAG_MARKER
RTP marker bit was set for this packet.
Definition: rtpdec.h:94
AVPacket::data
uint8_t * data
Definition: packet.h:373
FFIOContext
Definition: avio_internal.h:29
AV_PKT_FLAG_KEY
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: packet.h:425
init_get_bits
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:660
av_packet_free
void av_packet_free(AVPacket **pkt)
Free the packet, if the packet is reference counted, it will be unreferenced first.
Definition: avpacket.c:75
PayloadContext::timestamp
uint32_t timestamp
current frame timestamp
Definition: rtpdec_ac3.c:31
skip_bits
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:468
get_bits
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:380
GetBitContext
Definition: get_bits.h:62
avio_tell
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:486
MOVContext
Definition: isom.h:246
avio_rl16
unsigned int avio_rl16(AVIOContext *s)
Definition: aviobuf.c:726
qt_rtp_init
static av_cold int qt_rtp_init(AVFormatContext *ctx, int st_index, PayloadContext *qt)
Definition: rtpdec_qt.c:42
avio_rb32
unsigned int avio_rb32(AVIOContext *s)
Definition: aviobuf.c:773
pkt
AVPacket * pkt
Definition: movenc.c:59
av_cold
#define av_cold
Definition: attributes.h:90
s
#define s(width, name)
Definition: cbs_vp9.c:257
av_new_packet
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:99
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
PayloadContext::bytes_per_frame
int bytes_per_frame
Definition: rtpdec_qt.c:38
ctx
AVFormatContext * ctx
Definition: movenc.c:48
get_bits.h
nb_streams
static int nb_streams
Definition: ffprobe.c:289
AVFormatContext
Format I/O context.
Definition: avformat.h:1097
internal.h
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:992
NULL
#define NULL
Definition: coverity.c:32
AVERROR_PATCHWELCOME
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:64
av_realloc
void * av_realloc(void *ptr, size_t size)
Allocate, reallocate, or free a block of memory.
Definition: mem.c:152
isom.h
get_bits1
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:499
rtpdec.h
aud
static int FUNC() aud(CodedBitstreamContext *ctx, RWContext *rw, H264RawAUD *current)
Definition: cbs_h264_syntax_template.c:842
avio_rl32
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:742
av_packet_from_data
int av_packet_from_data(AVPacket *pkt, uint8_t *data, int size)
Initialize a reference-counted packet from av_malloc()ed data.
Definition: avpacket.c:166
AVIOContext
Bytestream IO Context.
Definition: avio.h:161
AVPacket::size
int size
Definition: packet.h:374
FFIOContext::pub
AVIOContext pub
Definition: avio_internal.h:30
av_reallocp
int av_reallocp(void *ptr, size_t size)
Allocate, reallocate, or free a block of memory through a pointer to a pointer.
Definition: mem.c:185
AVPacket::flags
int flags
A combination of AV_PKT_FLAG values.
Definition: packet.h:379
av_packet_alloc
AVPacket * av_packet_alloc(void)
Allocate an AVPacket and set its fields to default values.
Definition: avpacket.c:64
rtp.h
PayloadContext::pkt
AVPacket * pkt
Definition: rtpdec_qt.c:37
RTP_QT_HANDLER
#define RTP_QT_HANDLER(m, n, s, t)
Definition: rtpdec_qt.c:255
MOVStreamContext::bytes_per_frame
unsigned int bytes_per_frame
Definition: isom.h:196
avio_internal.h
ffio_init_context
void ffio_init_context(FFIOContext *s, unsigned char *buffer, int buffer_size, int write_flag, void *opaque, int(*read_packet)(void *opaque, uint8_t *buf, int buf_size), int(*write_packet)(void *opaque, uint8_t *buf, int buf_size), int64_t(*seek)(void *opaque, int64_t offset, int whence))
Definition: aviobuf.c:80
av_mallocz
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:263
len
int len
Definition: vorbis_enc_data.h:426
tag
uint32_t tag
Definition: movenc.c:1597
ret
ret
Definition: filter_design.txt:187
AVStream
Stream structure.
Definition: avformat.h:845
avio_seek
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:246
avio_rb16
unsigned int avio_rb16(AVIOContext *s)
Definition: aviobuf.c:758
qt_rtp_close
static void qt_rtp_close(PayloadContext *qt)
Definition: rtpdec_qt.c:249
pos
unsigned int pos
Definition: spdifenc.c:412
avformat.h
AV_INPUT_BUFFER_PADDING_SIZE
#define AV_INPUT_BUFFER_PADDING_SIZE
Definition: defs.h:40
AVStream::index
int index
stream index in AVFormatContext
Definition: avformat.h:846
PayloadContext::remaining
int remaining
Definition: rtpdec_qt.c:38
avpriv_set_pts_info
void avpriv_set_pts_info(AVStream *st, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition: utils.c:1146
AVPacket::stream_index
int stream_index
Definition: packet.h:375
avio_skip
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:333
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
avpriv_request_sample
#define avpriv_request_sample(...)
Definition: tableprint_vlc.h:37
av_free
#define av_free(p)
Definition: tableprint_vlc.h:34
MKTAG16
#define MKTAG16(a, b)
AVPacket
This structure stores compressed data.
Definition: packet.h:350
ff_mov_read_stsd_entries
int ff_mov_read_stsd_entries(MOVContext *c, AVIOContext *pb, int entries)
Definition: mov.c:2482
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:35
qt_rtp_parse_packet
static int qt_rtp_parse_packet(AVFormatContext *s, PayloadContext *qt, AVStream *st, AVPacket *pkt, uint32_t *timestamp, const uint8_t *buf, int len, uint16_t seq, int flags)
Definition: rtpdec_qt.c:52
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:561
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
MKTAG
#define MKTAG(a, b, c, d)
Definition: macros.h:55
PayloadContext
RTP/JPEG specific private data.
Definition: rdt.c:83
mc
#define mc
Definition: vf_colormatrix.c:102