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 
43  AVStream *st, AVPacket *pkt,
44  uint32_t *timestamp, const uint8_t *buf,
45  int len, uint16_t seq, int flags)
46 {
48  GetBitContext gb;
49  int packing_scheme, has_payload_desc, has_packet_info, alen,
50  has_marker_bit = flags & RTP_FLAG_MARKER;
51 
52  if (qt->remaining) {
53  int num = qt->pkt.size / qt->bytes_per_frame;
54 
55  if (av_new_packet(pkt, qt->bytes_per_frame))
56  return AVERROR(ENOMEM);
57  pkt->stream_index = st->index;
58  pkt->flags = qt->pkt.flags;
59  memcpy(pkt->data,
60  &qt->pkt.data[(num - qt->remaining) * qt->bytes_per_frame],
61  qt->bytes_per_frame);
62  if (--qt->remaining == 0) {
63  av_freep(&qt->pkt.data);
64  qt->pkt.size = 0;
65  }
66  return qt->remaining > 0;
67  }
68 
69  /**
70  * The RTP payload is described in:
71  * http://developer.apple.com/quicktime/icefloe/dispatch026.html
72  */
73  init_get_bits(&gb, buf, len << 3);
74  ffio_init_context(&pb, buf, len, 0, NULL, NULL, NULL, NULL);
75 
76  if (len < 4)
77  return AVERROR_INVALIDDATA;
78 
79  skip_bits(&gb, 4); // version
80  if ((packing_scheme = get_bits(&gb, 2)) == 0)
81  return AVERROR_INVALIDDATA;
82  if (get_bits1(&gb))
83  flags |= RTP_FLAG_KEY;
84  has_payload_desc = get_bits1(&gb);
85  has_packet_info = get_bits1(&gb);
86  skip_bits(&gb, 23); // reserved:7, cache payload info:1, payload ID:15
87 
88  if (has_payload_desc) {
89  int data_len, pos, is_start, is_finish;
90  uint32_t tag;
91 
92  pos = get_bits_count(&gb) >> 3;
93  if (pos + 12 > len)
94  return AVERROR_INVALIDDATA;
95 
96  skip_bits(&gb, 2); // has non-I frames:1, is sparse:1
97  is_start = get_bits1(&gb);
98  is_finish = get_bits1(&gb);
99  if (!is_start || !is_finish) {
100  avpriv_request_sample(s, "RTP-X-QT with payload description "
101  "split over several packets");
102  return AVERROR_PATCHWELCOME;
103  }
104  skip_bits(&gb, 12); // reserved
105  data_len = get_bits(&gb, 16);
106 
107  avio_seek(&pb, pos + 4, SEEK_SET);
108  tag = avio_rl32(&pb);
109  if ((st->codec->codec_type == AVMEDIA_TYPE_VIDEO &&
110  tag != MKTAG('v','i','d','e')) ||
112  tag != MKTAG('s','o','u','n')))
113  return AVERROR_INVALIDDATA;
114  avpriv_set_pts_info(st, 32, 1, avio_rb32(&pb));
115 
116  if (pos + data_len > len)
117  return AVERROR_INVALIDDATA;
118  /* TLVs */
119  while (avio_tell(&pb) + 4 < pos + data_len) {
120  int tlv_len = avio_rb16(&pb);
121  tag = avio_rl16(&pb);
122  if (avio_tell(&pb) + tlv_len > pos + data_len)
123  return AVERROR_INVALIDDATA;
124 
125 #define MKTAG16(a,b) MKTAG(a,b,0,0)
126  switch (tag) {
127  case MKTAG16('s','d'): {
128  MOVStreamContext *msc;
129  void *priv_data = st->priv_data;
130  int nb_streams = s->nb_streams;
131  MOVContext *mc = av_mallocz(sizeof(*mc));
132  if (!mc)
133  return AVERROR(ENOMEM);
134  mc->fc = s;
135  st->priv_data = msc = av_mallocz(sizeof(MOVStreamContext));
136  if (!msc) {
137  av_free(mc);
138  st->priv_data = priv_data;
139  return AVERROR(ENOMEM);
140  }
141  /* ff_mov_read_stsd_entries updates stream s->nb_streams-1,
142  * so set it temporarily to indicate which stream to update. */
143  s->nb_streams = st->index + 1;
144  ff_mov_read_stsd_entries(mc, &pb, 1);
145  qt->bytes_per_frame = msc->bytes_per_frame;
146  av_free(msc);
147  av_free(mc);
148  st->priv_data = priv_data;
149  s->nb_streams = nb_streams;
150  break;
151  }
152  default:
153  avio_skip(&pb, tlv_len);
154  break;
155  }
156  }
157 
158  /* 32-bit alignment */
159  avio_skip(&pb, ((avio_tell(&pb) + 3) & ~3) - avio_tell(&pb));
160  } else
161  avio_seek(&pb, 4, SEEK_SET);
162 
163  if (has_packet_info) {
164  avpriv_request_sample(s, "RTP-X-QT with packet-specific info");
165  return AVERROR_PATCHWELCOME;
166  }
167 
168  alen = len - avio_tell(&pb);
169  if (alen <= 0)
170  return AVERROR_INVALIDDATA;
171 
172  switch (packing_scheme) {
173  case 3: /* one data packet spread over 1 or multiple RTP packets */
174  if (qt->pkt.size > 0 && qt->timestamp == *timestamp) {
175  qt->pkt.data = av_realloc(qt->pkt.data, qt->pkt.size + alen +
177  } else {
178  av_freep(&qt->pkt.data);
179  av_init_packet(&qt->pkt);
181  qt->pkt.size = 0;
182  qt->timestamp = *timestamp;
183  }
184  if (!qt->pkt.data)
185  return AVERROR(ENOMEM);
186  memcpy(qt->pkt.data + qt->pkt.size, buf + avio_tell(&pb), alen);
187  qt->pkt.size += alen;
188  if (has_marker_bit) {
189  int ret = av_packet_from_data(pkt, qt->pkt.data, qt->pkt.size);
190  if (ret < 0)
191  return ret;
192 
193  qt->pkt.size = 0;
194  qt->pkt.data = NULL;
195  pkt->flags = flags & RTP_FLAG_KEY ? AV_PKT_FLAG_KEY : 0;
196  pkt->stream_index = st->index;
197  memset(pkt->data + pkt->size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
198  return 0;
199  }
200  return AVERROR(EAGAIN);
201 
202  case 1: /* constant packet size, multiple packets per RTP packet */
203  if (qt->bytes_per_frame == 0 ||
204  alen % qt->bytes_per_frame != 0)
205  return AVERROR_INVALIDDATA; /* wrongly padded */
206  qt->remaining = (alen / qt->bytes_per_frame) - 1;
207  if (av_new_packet(pkt, qt->bytes_per_frame))
208  return AVERROR(ENOMEM);
209  memcpy(pkt->data, buf + avio_tell(&pb), qt->bytes_per_frame);
210  pkt->flags = flags & RTP_FLAG_KEY ? AV_PKT_FLAG_KEY : 0;
211  pkt->stream_index = st->index;
212  if (qt->remaining > 0) {
213  av_freep(&qt->pkt.data);
214  qt->pkt.data = av_malloc(qt->remaining * qt->bytes_per_frame);
215  if (!qt->pkt.data) {
216  av_free_packet(pkt);
217  return AVERROR(ENOMEM);
218  }
219  qt->pkt.size = qt->remaining * qt->bytes_per_frame;
220  memcpy(qt->pkt.data,
221  buf + avio_tell(&pb) + qt->bytes_per_frame,
222  qt->remaining * qt->bytes_per_frame);
223  qt->pkt.flags = pkt->flags;
224  return 1;
225  }
226  return 0;
227 
228  default: /* unimplemented */
229  avpriv_request_sample(NULL, "RTP-X-QT with packing scheme 2");
230  return AVERROR_PATCHWELCOME;
231  }
232 }
233 
235 {
236  return av_mallocz(sizeof(PayloadContext));
237 }
238 
239 static void qt_rtp_free(PayloadContext *qt)
240 {
241  av_freep(&qt->pkt.data);
242  av_free(qt);
243 }
244 
245 #define RTP_QT_HANDLER(m, n, s, t) \
246 RTPDynamicProtocolHandler ff_ ## m ## _rtp_ ## n ## _handler = { \
247  .enc_name = s, \
248  .codec_type = t, \
249  .codec_id = AV_CODEC_ID_NONE, \
250  .alloc = qt_rtp_new, \
251  .free = qt_rtp_free, \
252  .parse_packet = qt_rtp_parse_packet, \
253 }
254 
255 RTP_QT_HANDLER(qt, vid, "X-QT", AVMEDIA_TYPE_VIDEO);
256 RTP_QT_HANDLER(qt, aud, "X-QT", AVMEDIA_TYPE_AUDIO);
257 RTP_QT_HANDLER(quicktime, vid, "X-QUICKTIME", AVMEDIA_TYPE_VIDEO);
258 RTP_QT_HANDLER(quicktime, aud, "X-QUICKTIME", AVMEDIA_TYPE_AUDIO);
AVPacket pkt
Definition: rtpdec_qt.c:37
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:205
const char * s
Definition: avisynth_c.h:668
Bytestream IO Context.
Definition: avio.h:68
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
void av_free_packet(AVPacket *pkt)
Free a packet.
Definition: avpacket.c:242
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:240
int bytes_per_frame
Definition: rtpdec_qt.c:38
void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
RTP/JPEG specific private data.
Definition: rdt.c:83
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:42
int index
stream index in AVFormatContext
Definition: avformat.h:644
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:199
void * priv_data
Definition: avformat.h:663
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:256
void * av_realloc(void *ptr, size_t size)
Allocate or reallocate a block of memory.
Definition: mem.c:141
uint8_t * buf
the temporary storage buffer
Definition: rtpdec_asf.c:160
unsigned int avio_rb16(AVIOContext *s)
Definition: aviobuf.c:595
#define MKTAG16(a, b)
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:198
Format I/O context.
Definition: avformat.h:944
void void avpriv_request_sample(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
uint8_t
unsigned int avio_rb32(AVIOContext *s)
Definition: aviobuf.c:610
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:136
uint8_t * data
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:193
uint32_t tag
Definition: movenc.c:894
bitstream reader API header.
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:248
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
static PayloadContext * qt_rtp_new(void)
Definition: rtpdec_qt.c:234
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:73
uint32_t timestamp
current frame timestamp
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:183
static void qt_rtp_free(PayloadContext *qt)
Definition: rtpdec_qt.c:239
#define RTP_FLAG_MARKER
RTP marker bit was set for this packet.
Definition: rtpdec.h:97
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:579
AVFormatContext * fc
Definition: isom.h:149
int flags
A combination of AV_PKT_FLAG values.
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:662
#define FF_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
unsigned int nb_streams
A list of all streams in the file.
Definition: avformat.h:991
ret
Definition: avfilter.c:821
#define RTP_QT_HANDLER(m, n, s, t)
Definition: rtpdec_qt.c:245
#define mc
Stream structure.
Definition: avformat.h:643
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
NULL
Definition: eval.c:55
enum AVMediaType codec_type
#define RTP_FLAG_KEY
RTP packet contains a keyframe.
Definition: rtpdec.h:96
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:273
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:73
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:265
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:379
int ff_mov_read_stsd_entries(MOVContext *c, AVIOContext *pb, int entries)
Definition: mov.c:1201
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFilterBuffer 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.Buffer references ownership and permissions
static int flags
Definition: cpu.c:23
unsigned int avio_rl16(AVIOContext *s)
Definition: aviobuf.c:563
Main libavformat public API header.
unsigned int bytes_per_frame
Definition: isom.h:124
int ffio_init_context(AVIOContext *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:71
void av_init_packet(AVPacket *pkt)
Initialize optional fields of a packet with default values.
Definition: avpacket.c:56
#define MKTAG(a, b, c, d)
Definition: common.h:282
AVIOContext pb
Definition: rtpdec_asf.c:159
This structure stores compressed data.