FFmpeg  4.4.9
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  AVIOContext pb;
58  GetBitContext gb;
59  int packing_scheme, has_payload_desc, has_packet_info, alen,
60  has_marker_bit = flags & RTP_FLAG_MARKER,
61  keyframe, ret;
62 
63  if (qt->remaining) {
64  int num = qt->pkt->size / qt->bytes_per_frame;
65 
66  if ((ret = av_new_packet(pkt, qt->bytes_per_frame)) < 0)
67  return ret;
68  pkt->stream_index = st->index;
69  pkt->flags = qt->pkt->flags;
70  memcpy(pkt->data,
71  &qt->pkt->data[(num - qt->remaining) * qt->bytes_per_frame],
72  qt->bytes_per_frame);
73  if (--qt->remaining == 0) {
74  av_freep(&qt->pkt->data);
75  qt->pkt->size = 0;
76  }
77  return qt->remaining > 0;
78  }
79 
80  /**
81  * The RTP payload is described in:
82  * http://developer.apple.com/quicktime/icefloe/dispatch026.html
83  */
84  ret = init_get_bits(&gb, buf, len << 3);
85  if (ret < 0)
86  return ret;
87  ffio_init_context(&pb, (uint8_t*)buf, len, 0, NULL, NULL, NULL, NULL);
88 
89  if (len < 4)
90  return AVERROR_INVALIDDATA;
91 
92  skip_bits(&gb, 4); // version
93  if ((packing_scheme = get_bits(&gb, 2)) == 0)
94  return AVERROR_INVALIDDATA;
95  keyframe = get_bits1(&gb);
96  has_payload_desc = get_bits1(&gb);
97  has_packet_info = get_bits1(&gb);
98  skip_bits(&gb, 23); // reserved:7, cache payload info:1, payload ID:15
99 
100  if (has_payload_desc) {
101  int data_len, pos, is_start, is_finish;
102  uint32_t tag;
103 
104  pos = get_bits_count(&gb) >> 3;
105  if (pos + 12 > len)
106  return AVERROR_INVALIDDATA;
107 
108  skip_bits(&gb, 2); // has non-I-frames:1, is sparse:1
109  is_start = get_bits1(&gb);
110  is_finish = get_bits1(&gb);
111  if (!is_start || !is_finish) {
112  avpriv_request_sample(s, "RTP-X-QT with payload description "
113  "split over several packets");
114  return AVERROR_PATCHWELCOME;
115  }
116  skip_bits(&gb, 12); // reserved
117  data_len = get_bits(&gb, 16);
118 
119  avio_seek(&pb, pos + 4, SEEK_SET);
120  tag = avio_rl32(&pb);
121  if ((st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
122  tag != MKTAG('v','i','d','e')) ||
124  tag != MKTAG('s','o','u','n')))
125  return AVERROR_INVALIDDATA;
126  avpriv_set_pts_info(st, 32, 1, avio_rb32(&pb));
127 
128  if (pos + data_len > len)
129  return AVERROR_INVALIDDATA;
130  /* TLVs */
131  while (avio_tell(&pb) + 4 < pos + data_len) {
132  int tlv_len = avio_rb16(&pb);
133  tag = avio_rl16(&pb);
134  if (avio_tell(&pb) + tlv_len > pos + data_len)
135  return AVERROR_INVALIDDATA;
136 
137 #define MKTAG16(a,b) MKTAG(a,b,0,0)
138  switch (tag) {
139  case MKTAG16('s','d'): {
140  MOVStreamContext *msc;
141  void *priv_data = st->priv_data;
142  int nb_streams = s->nb_streams;
143  MOVContext *mc = av_mallocz(sizeof(*mc));
144  if (!mc)
145  return AVERROR(ENOMEM);
146  mc->fc = s;
147  mc->trak_index = st->index;
148  st->priv_data = msc = av_mallocz(sizeof(MOVStreamContext));
149  if (!msc) {
150  av_free(mc);
151  st->priv_data = priv_data;
152  return AVERROR(ENOMEM);
153  }
154  /* ff_mov_read_stsd_entries updates stream s->nb_streams-1,
155  * so set it temporarily to indicate which stream to update. */
156  s->nb_streams = st->index + 1;
157  ff_mov_read_stsd_entries(mc, &pb, 1);
158  qt->bytes_per_frame = msc->bytes_per_frame;
159  av_free(msc);
160  av_free(mc);
161  st->priv_data = priv_data;
162  s->nb_streams = nb_streams;
163  break;
164  }
165  default:
166  avio_skip(&pb, tlv_len);
167  break;
168  }
169  }
170 
171  /* 32-bit alignment */
172  avio_skip(&pb, ((avio_tell(&pb) + 3) & ~3) - avio_tell(&pb));
173  } else
174  avio_seek(&pb, 4, SEEK_SET);
175 
176  if (has_packet_info) {
177  avpriv_request_sample(s, "RTP-X-QT with packet-specific info");
178  return AVERROR_PATCHWELCOME;
179  }
180 
181  alen = len - avio_tell(&pb);
182  if (alen <= 0)
183  return AVERROR_INVALIDDATA;
184 
185  switch (packing_scheme) {
186  case 3: /* one data packet spread over 1 or multiple RTP packets */
187  if (qt->pkt->size > 0 && qt->timestamp == *timestamp) {
188  int err;
189  if ((err = av_reallocp(&qt->pkt->data, qt->pkt->size + alen +
191  qt->pkt->size = 0;
192  return err;
193  }
194  } else {
195  av_freep(&qt->pkt->data);
196  av_packet_unref(qt->pkt);
198  if (!qt->pkt->data)
199  return AVERROR(ENOMEM);
200  qt->pkt->size = 0;
201  qt->timestamp = *timestamp;
202  }
203  memcpy(qt->pkt->data + qt->pkt->size, buf + avio_tell(&pb), alen);
204  qt->pkt->size += alen;
205  if (has_marker_bit) {
206  int ret = av_packet_from_data(pkt, qt->pkt->data, qt->pkt->size);
207  if (ret < 0)
208  return ret;
209 
210  qt->pkt->size = 0;
211  qt->pkt->data = NULL;
212  pkt->flags = keyframe ? AV_PKT_FLAG_KEY : 0;
213  pkt->stream_index = st->index;
214  memset(pkt->data + pkt->size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
215  return 0;
216  }
217  return AVERROR(EAGAIN);
218 
219  case 1: /* constant packet size, multiple packets per RTP packet */
220  if (qt->bytes_per_frame == 0 ||
221  alen % qt->bytes_per_frame != 0)
222  return AVERROR_INVALIDDATA; /* wrongly padded */
223  qt->remaining = (alen / qt->bytes_per_frame) - 1;
224  if ((ret = av_new_packet(pkt, qt->bytes_per_frame)) < 0)
225  return ret;
226  memcpy(pkt->data, buf + avio_tell(&pb), qt->bytes_per_frame);
227  pkt->flags = keyframe ? AV_PKT_FLAG_KEY : 0;
228  pkt->stream_index = st->index;
229  if (qt->remaining > 0) {
230  av_freep(&qt->pkt->data);
231  qt->pkt->data = av_realloc(NULL, qt->remaining * qt->bytes_per_frame);
232  if (!qt->pkt->data) {
234  return AVERROR(ENOMEM);
235  }
236  qt->pkt->size = qt->remaining * qt->bytes_per_frame;
237  memcpy(qt->pkt->data,
238  buf + avio_tell(&pb) + qt->bytes_per_frame,
239  qt->remaining * qt->bytes_per_frame);
240  qt->pkt->flags = pkt->flags;
241  return 1;
242  }
243  return 0;
244 
245  default: /* unimplemented */
246  avpriv_request_sample(NULL, "RTP-X-QT with packing scheme 2");
247  return AVERROR_PATCHWELCOME;
248  }
249 }
250 
251 static void qt_rtp_close(PayloadContext *qt)
252 {
253  av_freep(&qt->pkt->data);
254  av_packet_free(&qt->pkt);
255 }
256 
257 #define RTP_QT_HANDLER(m, n, s, t) \
258 const RTPDynamicProtocolHandler ff_ ## m ## _rtp_ ## n ## _handler = { \
259  .enc_name = s, \
260  .codec_type = t, \
261  .codec_id = AV_CODEC_ID_NONE, \
262  .priv_data_size = sizeof(PayloadContext), \
263  .init = qt_rtp_init, \
264  .close = qt_rtp_close, \
265  .parse_packet = qt_rtp_parse_packet, \
266 }
267 
270 RTP_QT_HANDLER(quicktime, vid, "X-QUICKTIME", AVMEDIA_TYPE_VIDEO);
271 RTP_QT_HANDLER(quicktime, aud, "X-QUICKTIME", AVMEDIA_TYPE_AUDIO);
#define av_cold
Definition: attributes.h:88
uint8_t
Main libavformat public API header.
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:260
unsigned int avio_rb16(AVIOContext *s)
Definition: aviobuf.c:782
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:557
unsigned int avio_rl16(AVIOContext *s)
Definition: aviobuf.c:750
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:344
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:766
unsigned int avio_rb32(AVIOContext *s)
Definition: aviobuf.c:797
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:88
#define flags(name, subs,...)
Definition: cbs_av1.c:572
static int FUNC() aud(CodedBitstreamContext *ctx, RWContext *rw, H264RawAUD *current)
#define s(width, name)
Definition: cbs_vp9.c:257
#define MKTAG(a, b, c, d)
Definition: common.h:478
#define NULL
Definition: coverity.c:32
static int nb_streams
Definition: ffprobe.c:283
bitstream reader API header.
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:498
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:467
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:219
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:379
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:659
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding.
Definition: avcodec.h:215
void av_packet_free(AVPacket **pkt)
Free the packet, if the packet is reference counted, it will be unreferenced first.
Definition: avpacket.c:75
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:634
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: packet.h:410
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
AVPacket * av_packet_alloc(void)
Allocate an AVPacket and set its fields to default values.
Definition: avpacket.c:64
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
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
#define AVERROR(e)
Definition: error.h:43
void * av_realloc(void *ptr, size_t size)
Allocate, reallocate, or free a block of memory.
Definition: mem.c:134
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:237
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:161
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
int ff_mov_read_stsd_entries(MOVContext *c, AVIOContext *pb, int entries)
Definition: mov.c:2558
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.
Definition: utils.c:4944
common internal API header
uint32_t tag
Definition: movenc.c:1611
#define RTP_FLAG_MARKER
RTP marker bit was set for this packet.
Definition: rtpdec.h:93
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
static av_cold int qt_rtp_init(AVFormatContext *ctx, int st_index, PayloadContext *qt)
Definition: rtpdec_qt.c:42
#define RTP_QT_HANDLER(m, n, s, t)
Definition: rtpdec_qt.c:257
static void qt_rtp_close(PayloadContext *qt)
Definition: rtpdec_qt.c:251
#define MKTAG16(a, b)
unsigned int pos
Definition: spdifenc.c:412
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:56
Format I/O context.
Definition: avformat.h:1232
Bytestream IO Context.
Definition: avio.h:161
This structure stores compressed data.
Definition: packet.h:346
int stream_index
Definition: packet.h:371
int flags
A combination of AV_PKT_FLAG values.
Definition: packet.h:375
int size
Definition: packet.h:370
uint8_t * data
Definition: packet.h:369
Stream structure.
Definition: avformat.h:873
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1038
void * priv_data
Definition: avformat.h:888
int index
stream index in AVFormatContext
Definition: avformat.h:874
unsigned int bytes_per_frame
Definition: isom.h:197
RTP/JPEG specific private data.
Definition: rdt.c:83
AVPacket * pkt
Definition: rtpdec_qt.c:37
uint32_t timestamp
current frame timestamp
Definition: rtpdec_ac3.c:31
int bytes_per_frame
Definition: rtpdec_qt.c:38
#define av_free(p)
#define avpriv_request_sample(...)
#define av_freep(p)
AVPacket * pkt
Definition: movenc.c:59
AVFormatContext * ctx
Definition: movenc.c:48
#define mc
int len