rtpdec_latm.c
Go to the documentation of this file.
1 /*
2  * RTP Depacketization of MP4A-LATM, RFC 3016
3  * Copyright (c) 2010 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 "rtpdec_formats.h"
23 #include "internal.h"
24 #include "libavutil/avstring.h"
25 #include "libavcodec/get_bits.h"
26 
27 struct PayloadContext {
29  uint8_t *buf;
30  int pos, len;
31  uint32_t timestamp;
32 };
33 
35 {
36  return av_mallocz(sizeof(PayloadContext));
37 }
38 
40 {
41  if (!data)
42  return;
43  if (data->dyn_buf) {
44  uint8_t *p;
45  avio_close_dyn_buf(data->dyn_buf, &p);
46  av_free(p);
47  }
48  av_free(data->buf);
49  av_free(data);
50 }
51 
53  AVStream *st, AVPacket *pkt, uint32_t *timestamp,
54  const uint8_t *buf, int len, uint16_t seq,
55  int flags)
56 {
57  int ret, cur_len;
58 
59  if (buf) {
60  if (!data->dyn_buf || data->timestamp != *timestamp) {
61  av_freep(&data->buf);
62  if (data->dyn_buf)
63  avio_close_dyn_buf(data->dyn_buf, &data->buf);
64  data->dyn_buf = NULL;
65  av_freep(&data->buf);
66 
67  data->timestamp = *timestamp;
68  if ((ret = avio_open_dyn_buf(&data->dyn_buf)) < 0)
69  return ret;
70  }
71  avio_write(data->dyn_buf, buf, len);
72 
73  if (!(flags & RTP_FLAG_MARKER))
74  return AVERROR(EAGAIN);
75  av_free(data->buf);
76  data->len = avio_close_dyn_buf(data->dyn_buf, &data->buf);
77  data->dyn_buf = NULL;
78  data->pos = 0;
79  }
80 
81  if (!data->buf) {
82  av_log(ctx, AV_LOG_ERROR, "No data available yet\n");
83  return AVERROR(EIO);
84  }
85 
86  cur_len = 0;
87  while (data->pos < data->len) {
88  uint8_t val = data->buf[data->pos++];
89  cur_len += val;
90  if (val != 0xff)
91  break;
92  }
93  if (data->pos + cur_len > data->len) {
94  av_log(ctx, AV_LOG_ERROR, "Malformed LATM packet\n");
95  return AVERROR(EIO);
96  }
97 
98  if ((ret = av_new_packet(pkt, cur_len)) < 0)
99  return ret;
100  memcpy(pkt->data, data->buf + data->pos, cur_len);
101  data->pos += cur_len;
102  pkt->stream_index = st->index;
103  return data->pos < data->len;
104 }
105 
106 static int parse_fmtp_config(AVStream *st, char *value)
107 {
108  int len = ff_hex_to_data(NULL, value), i, ret = 0;
109  GetBitContext gb;
110  uint8_t *config;
111  int audio_mux_version, same_time_framing, num_programs, num_layers;
112 
113  /* Pad this buffer, too, to avoid out of bounds reads with get_bits below */
115  if (!config)
116  return AVERROR(ENOMEM);
117  ff_hex_to_data(config, value);
118  init_get_bits(&gb, config, len*8);
119  audio_mux_version = get_bits(&gb, 1);
120  same_time_framing = get_bits(&gb, 1);
121  skip_bits(&gb, 6); /* num_sub_frames */
122  num_programs = get_bits(&gb, 4);
123  num_layers = get_bits(&gb, 3);
124  if (audio_mux_version != 0 || same_time_framing != 1 || num_programs != 0 ||
125  num_layers != 0) {
126  av_log(NULL, AV_LOG_WARNING, "Unsupported LATM config (%d,%d,%d,%d)\n",
127  audio_mux_version, same_time_framing,
128  num_programs, num_layers);
129  ret = AVERROR_PATCHWELCOME;
130  goto end;
131  }
132  av_freep(&st->codec->extradata);
133  st->codec->extradata_size = (get_bits_left(&gb) + 7)/8;
136  if (!st->codec->extradata) {
137  ret = AVERROR(ENOMEM);
138  goto end;
139  }
140  for (i = 0; i < st->codec->extradata_size; i++)
141  st->codec->extradata[i] = get_bits(&gb, 8);
142 
143 end:
144  av_free(config);
145  return ret;
146 }
147 
148 static int parse_fmtp(AVStream *stream, PayloadContext *data,
149  char *attr, char *value)
150 {
151  int res;
152 
153  if (!strcmp(attr, "config")) {
154  res = parse_fmtp_config(stream, value);
155  if (res < 0)
156  return res;
157  } else if (!strcmp(attr, "cpresent")) {
158  int cpresent = atoi(value);
159  if (cpresent != 0)
161  "RTP MP4A-LATM with in-band configuration");
162  }
163 
164  return 0;
165 }
166 
167 static int latm_parse_sdp_line(AVFormatContext *s, int st_index,
168  PayloadContext *data, const char *line)
169 {
170  const char *p;
171 
172  if (st_index < 0)
173  return 0;
174 
175  if (av_strstart(line, "fmtp:", &p))
176  return ff_parse_fmtp(s->streams[st_index], data, p, parse_fmtp);
177 
178  return 0;
179 }
180 
182  .enc_name = "MP4A-LATM",
183  .codec_type = AVMEDIA_TYPE_AUDIO,
184  .codec_id = AV_CODEC_ID_AAC,
185  .parse_sdp_a_line = latm_parse_sdp_line,
186  .alloc = latm_new_context,
187  .free = latm_free_context,
188  .parse_packet = latm_parse_packet
189 };
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
static int latm_parse_sdp_line(AVFormatContext *s, int st_index, PayloadContext *data, const char *line)
Definition: rtpdec_latm.c:167
const char * s
Definition: avisynth_c.h:668
Bytestream IO Context.
Definition: avio.h:68
int avio_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
Return the written size and a pointer to the buffer.
Definition: aviobuf.c:988
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:240
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:154
RTP/JPEG specific private data.
Definition: rdt.c:83
int index
stream index in AVFormatContext
Definition: avformat.h:644
uint8_t * buf
the temporary storage buffer
Definition: rtpdec_asf.c:160
int avio_open_dyn_buf(AVIOContext **s)
Open a write only memory stream.
Definition: aviobuf.c:976
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
end end
static int parse_fmtp_config(AVStream *st, char *value)
Definition: rtpdec_latm.c:106
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
AVStream ** streams
Definition: avformat.h:992
uint8_t * data
bitstream reader API header.
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:173
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
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:557
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
#define RTP_FLAG_MARKER
RTP marker bit was set for this packet.
Definition: rtpdec.h:97
RTPDynamicProtocolHandler ff_mp4a_latm_dynamic_handler
Definition: rtpdec_latm.c:181
Definition: graph2dot.c:48
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:246
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...
ret
Definition: avfilter.c:821
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
AVIOContext * data
Definition: rtpdec_vp8.c:35
static int parse_fmtp(AVStream *stream, PayloadContext *data, char *attr, char *value)
Definition: rtpdec_latm.c:148
static PayloadContext * latm_new_context(void)
Definition: rtpdec_latm.c:34
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:148
double value
Definition: eval.c:82
static int config(struct vf_instance *vf, int width, int height, int d_width, int d_height, unsigned int flags, unsigned int outfmt)
Definition: vf_dint.c:45
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:265
synthesis window for stochastic i
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:379
const char enc_name[50]
Definition: rtpdec.h:120
int ff_hex_to_data(uint8_t *data, const char *p)
Parse a string of hexadecimal strings.
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
int av_strstart(const char *str, const char *pfx, const char **ptr)
Return non-zero if pfx is a prefix of str.
Definition: avstring.c:33
AVIOContext * dyn_buf
Definition: rtpdec_latm.c:28
static int latm_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_latm.c:52
int ff_parse_fmtp(AVStream *stream, PayloadContext *data, const char *p, int(*parse_fmtp)(AVStream *stream, PayloadContext *data, char *attr, char *value))
Definition: rtpdec.c:829
This structure stores compressed data.
static void latm_free_context(PayloadContext *data)
Definition: rtpdec_latm.c:39