FFmpeg
rtpdec_mpa_robust.c
Go to the documentation of this file.
1 /*
2  * RTP parser for loss tolerant payload format for MP3 audio (RFC 5219)
3  * Copyright (c) 2015 Gilles Chanteperdrix <gch@xenomai.org>
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/attributes.h"
23 #include "libavutil/intreadwrite.h"
24 #include "libavutil/mem.h"
25 
26 #include "avio_internal.h"
27 #include "rtpdec_formats.h"
28 
29 struct PayloadContext {
30  unsigned adu_size;
31  unsigned cur_size;
32  uint32_t timestamp;
33  uint8_t *split_buf;
36 };
37 
39 {
40  ffio_free_dyn_buf(&data->fragment);
41  av_free(data->split_buf);
42 }
43 
45  const uint8_t *buf, int len,
46  unsigned *adu_size, unsigned *cont)
47 {
48  unsigned header_size;
49 
50  if (len < 2) {
51  av_log(ctx, AV_LOG_ERROR, "Invalid %d bytes packet\n", len);
52  return AVERROR_INVALIDDATA;
53  }
54 
55  *cont = !!(buf[0] & 0x80);
56  if (!(buf[0] & 0x40)) {
57  header_size = 1;
58  *adu_size = buf[0] & ~0xc0;
59  } else {
60  header_size = 2;
61  *adu_size = AV_RB16(buf) & ~0xc000;
62  }
63 
64  return header_size;
65 }
66 
68  AVStream *st, AVPacket *pkt,
69  uint32_t *timestamp, const uint8_t *buf,
70  int len, uint16_t seq, int flags)
71 {
72  unsigned adu_size, continuation;
73  int err, header_size;
74 
75  if (!buf) {
76  buf = &data->split_buf[data->split_pos];
77  len = data->split_buf_size - data->split_pos;
78 
79  header_size = mpa_robust_parse_rtp_header(ctx, buf, len, &adu_size,
80  &continuation);
81  if (header_size < 0) {
82  av_freep(&data->split_buf);
83  return header_size;
84  }
85  buf += header_size;
86  len -= header_size;
87 
88  if (continuation || adu_size > len) {
89  av_freep(&data->split_buf);
90  av_log(ctx, AV_LOG_ERROR, "Invalid frame\n");
91  return AVERROR_INVALIDDATA;
92  }
93 
94  if ((err = av_new_packet(pkt, adu_size)) < 0) {
95  av_log(ctx, AV_LOG_ERROR, "Out of memory.\n");
96  return err;
97  }
98 
99  pkt->stream_index = st->index;
100  memcpy(pkt->data, buf, adu_size);
101 
102  data->split_pos += header_size + adu_size;
103 
104  if (data->split_pos == data->split_buf_size) {
105  av_freep(&data->split_buf);
106  return 0;
107  }
108 
109  return 1;
110  }
111 
112 
113  header_size = mpa_robust_parse_rtp_header(ctx, buf, len, &adu_size,
114  &continuation);
115  if (header_size < 0)
116  return header_size;
117 
118  buf += header_size;
119  len -= header_size;
120 
121  if (!continuation && adu_size <= len) {
122  /* One or more complete frames */
123 
124  if ((err = av_new_packet(pkt, adu_size)) < 0) {
125  av_log(ctx, AV_LOG_ERROR, "Out of memory.\n");
126  return err;
127  }
128 
129  pkt->stream_index = st->index;
130  memcpy(pkt->data, buf, adu_size);
131 
132  buf += adu_size;
133  len -= adu_size;
134  if (len) {
135  data->split_buf_size = len;
136  data->split_buf = av_malloc(data->split_buf_size);
137  data->split_pos = 0;
138  if (!data->split_buf) {
139  av_log(ctx, AV_LOG_ERROR, "Out of memory.\n");
141  return AVERROR(ENOMEM);
142  }
143  memcpy(data->split_buf, buf, data->split_buf_size);
144  return 1;
145  }
146  return 0;
147  } else if (!continuation) { /* && adu_size > len */
148  /* First fragment */
149  ffio_free_dyn_buf(&data->fragment);
150 
151  data->adu_size = adu_size;
152  data->cur_size = len;
153  data->timestamp = *timestamp;
154 
155  err = avio_open_dyn_buf(&data->fragment);
156  if (err < 0)
157  return err;
158 
159  avio_write(data->fragment, buf, len);
160  return AVERROR(EAGAIN);
161  }
162  /* else continuation == 1 */
163 
164  /* Fragment other than first */
165  if (!data->fragment) {
167  "Received packet without a start fragment; dropping.\n");
168  return AVERROR(EAGAIN);
169  }
170  if (adu_size != data->adu_size ||
171  data->timestamp != *timestamp) {
172  ffio_free_dyn_buf(&data->fragment);
173  av_log(ctx, AV_LOG_ERROR, "Invalid packet received\n");
174  return AVERROR_INVALIDDATA;
175  }
176 
177  avio_write(data->fragment, buf, len);
178  data->cur_size += len;
179 
180  if (data->cur_size < data->adu_size)
181  return AVERROR(EAGAIN);
182 
183  err = ff_rtp_finalize_packet(pkt, &data->fragment, st->index);
184  if (err < 0) {
186  "Error occurred when getting fragment buffer.\n");
187  return err;
188  }
189 
190  return 0;
191 }
192 
194  .enc_name = "mpa-robust",
195  .codec_type = AVMEDIA_TYPE_AUDIO,
196  .codec_id = AV_CODEC_ID_MP3ADU,
197  .need_parsing = AVSTREAM_PARSE_HEADERS,
198  .priv_data_size = sizeof(PayloadContext),
199  .close = mpa_robust_close_context,
201 };
av_packet_unref
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: packet.c:429
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:215
PayloadContext::adu_size
unsigned adu_size
Definition: rtpdec_mpa_robust.c:30
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
PayloadContext::split_buf_size
int split_buf_size
Definition: rtpdec_mpa_robust.c:34
rtpdec_formats.h
ff_mpeg_audio_robust_dynamic_handler
const RTPDynamicProtocolHandler ff_mpeg_audio_robust_dynamic_handler
Definition: rtpdec_mpa_robust.c:193
AVPacket::data
uint8_t * data
Definition: packet.h:539
mpa_robust_parse_rtp_header
static int mpa_robust_parse_rtp_header(AVFormatContext *ctx, const uint8_t *buf, int len, unsigned *adu_size, unsigned *cont)
Definition: rtpdec_mpa_robust.c:44
data
const char data[16]
Definition: mxf.c:149
av_malloc
#define av_malloc(s)
Definition: tableprint_vlc.h:30
ff_rtp_finalize_packet
int ff_rtp_finalize_packet(AVPacket *pkt, AVIOContext **dyn_buf, int stream_idx)
Close the dynamic buffer and make a packet from it.
Definition: rtpdec.c:1003
AV_CODEC_ID_MP3ADU
@ AV_CODEC_ID_MP3ADU
Definition: codec_id.h:458
PayloadContext::timestamp
uint32_t timestamp
current frame timestamp
Definition: rtpdec_ac3.c:31
RTPDynamicProtocolHandler::enc_name
const char * enc_name
Definition: rtpdec.h:117
pkt
AVPacket * pkt
Definition: movenc.c:60
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:209
PayloadContext::split_buf
uint8_t * split_buf
Definition: rtpdec_mpa_robust.c:33
avio_open_dyn_buf
int avio_open_dyn_buf(AVIOContext **s)
Open a write only memory stream.
Definition: aviobuf.c:1362
PayloadContext::fragment
AVIOContext * fragment
buffer for split payloads
Definition: rtpdec_ac3.c:32
intreadwrite.h
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: packet.c:98
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
ctx
AVFormatContext * ctx
Definition: movenc.c:49
AVFormatContext
Format I/O context.
Definition: avformat.h:1300
mpa_robust_parse_packet
static int mpa_robust_parse_packet(AVFormatContext *ctx, PayloadContext *data, AVStream *st, AVPacket *pkt, uint32_t *timestamp, const uint8_t *buf, int len, uint16_t seq, int flags)
Definition: rtpdec_mpa_robust.c:67
mpa_robust_close_context
static void mpa_robust_close_context(PayloadContext *data)
Definition: rtpdec_mpa_robust.c:38
PayloadContext::split_pos
int split_pos
Definition: rtpdec_mpa_robust.c:34
AVIOContext
Bytestream IO Context.
Definition: avio.h:160
avio_write
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:201
attributes.h
avio_internal.h
PayloadContext::split_pkts
int split_pkts
Definition: rtpdec_mpa_robust.c:34
len
int len
Definition: vorbis_enc_data.h:426
ffio_free_dyn_buf
void ffio_free_dyn_buf(AVIOContext **s)
Free a dynamic buffer.
Definition: aviobuf.c:1435
AVStream
Stream structure.
Definition: avformat.h:748
AVSTREAM_PARSE_HEADERS
@ AVSTREAM_PARSE_HEADERS
Only parse headers, do not repack.
Definition: avformat.h:594
AVStream::index
int index
stream index in AVFormatContext
Definition: avformat.h:754
AVPacket::stream_index
int stream_index
Definition: packet.h:541
parse_packet
static int parse_packet(AVFormatContext *s, AVPacket *pkt, int stream_index, int flush)
Parse a packet, add all split parts to parse_queue.
Definition: demux.c:1162
mem.h
av_free
#define av_free(p)
Definition: tableprint_vlc.h:33
PayloadContext::cur_size
unsigned cur_size
Definition: rtpdec_mpa_robust.c:31
AVPacket
This structure stores compressed data.
Definition: packet.h:516
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:482
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
PayloadContext
RTP/JPEG specific private data.
Definition: rdt.c:85
RTPDynamicProtocolHandler
Definition: rtpdec.h:116
AV_RB16
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_WB32 unsigned int_TMPL AV_WB24 unsigned int_TMPL AV_RB16
Definition: bytestream.h:98