Libav
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 Libav.
6  *
7  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 #include "avio_internal.h"
23 #include "rtpdec_formats.h"
24 #include "internal.h"
25 #include "libavutil/avstring.h"
26 #include "libavcodec/get_bits.h"
27 
28 struct PayloadContext {
30  uint8_t *buf;
31  int pos, len;
32  uint32_t timestamp;
33 };
34 
36 {
37  ffio_free_dyn_buf(&data->dyn_buf);
38  av_free(data->buf);
39 }
40 
42  AVStream *st, AVPacket *pkt, uint32_t *timestamp,
43  const uint8_t *buf, int len, uint16_t seq,
44  int flags)
45 {
46  int ret, cur_len;
47 
48  if (buf) {
49  if (!data->dyn_buf || data->timestamp != *timestamp) {
50  av_freep(&data->buf);
51  ffio_free_dyn_buf(&data->dyn_buf);
52 
53  data->timestamp = *timestamp;
54  if ((ret = avio_open_dyn_buf(&data->dyn_buf)) < 0)
55  return ret;
56  }
57  avio_write(data->dyn_buf, buf, len);
58 
59  if (!(flags & RTP_FLAG_MARKER))
60  return AVERROR(EAGAIN);
61  av_free(data->buf);
62  data->len = avio_close_dyn_buf(data->dyn_buf, &data->buf);
63  data->dyn_buf = NULL;
64  data->pos = 0;
65  }
66 
67  if (!data->buf) {
68  av_log(ctx, AV_LOG_ERROR, "No data available yet\n");
69  return AVERROR(EIO);
70  }
71 
72  cur_len = 0;
73  while (data->pos < data->len) {
74  uint8_t val = data->buf[data->pos++];
75  cur_len += val;
76  if (val != 0xff)
77  break;
78  }
79  if (data->pos + cur_len > data->len) {
80  av_log(ctx, AV_LOG_ERROR, "Malformed LATM packet\n");
81  return AVERROR(EIO);
82  }
83 
84  if ((ret = av_new_packet(pkt, cur_len)) < 0)
85  return ret;
86  memcpy(pkt->data, data->buf + data->pos, cur_len);
87  data->pos += cur_len;
88  pkt->stream_index = st->index;
89  return data->pos < data->len;
90 }
91 
92 static int parse_fmtp_config(AVStream *st, const char *value)
93 {
94  int len = ff_hex_to_data(NULL, value), i, ret = 0;
95  GetBitContext gb;
96  uint8_t *config;
97  int audio_mux_version, same_time_framing, num_programs, num_layers;
98 
99  /* Pad this buffer, too, to avoid out of bounds reads with get_bits below */
101  if (!config)
102  return AVERROR(ENOMEM);
103  ff_hex_to_data(config, value);
104  init_get_bits(&gb, config, len*8);
105  audio_mux_version = get_bits(&gb, 1);
106  same_time_framing = get_bits(&gb, 1);
107  skip_bits(&gb, 6); /* num_sub_frames */
108  num_programs = get_bits(&gb, 4);
109  num_layers = get_bits(&gb, 3);
110  if (audio_mux_version != 0 || same_time_framing != 1 || num_programs != 0 ||
111  num_layers != 0) {
112  av_log(NULL, AV_LOG_WARNING, "Unsupported LATM config (%d,%d,%d,%d)\n",
113  audio_mux_version, same_time_framing,
114  num_programs, num_layers);
115  ret = AVERROR_PATCHWELCOME;
116  goto end;
117  }
118  av_freep(&st->codecpar->extradata);
119  st->codecpar->extradata_size = (get_bits_left(&gb) + 7)/8;
122  if (!st->codecpar->extradata) {
123  ret = AVERROR(ENOMEM);
124  goto end;
125  }
126  for (i = 0; i < st->codecpar->extradata_size; i++)
127  st->codecpar->extradata[i] = get_bits(&gb, 8);
128 
129 end:
130  av_free(config);
131  return ret;
132 }
133 
135  AVStream *stream, PayloadContext *data,
136  const char *attr, const char *value)
137 {
138  int res;
139 
140  if (!strcmp(attr, "config")) {
141  res = parse_fmtp_config(stream, value);
142  if (res < 0)
143  return res;
144  } else if (!strcmp(attr, "cpresent")) {
145  int cpresent = atoi(value);
146  if (cpresent != 0)
148  "RTP MP4A-LATM with in-band configuration");
149  }
150 
151  return 0;
152 }
153 
154 static int latm_parse_sdp_line(AVFormatContext *s, int st_index,
155  PayloadContext *data, const char *line)
156 {
157  const char *p;
158 
159  if (st_index < 0)
160  return 0;
161 
162  if (av_strstart(line, "fmtp:", &p))
163  return ff_parse_fmtp(s, s->streams[st_index], data, p, parse_fmtp);
164 
165  return 0;
166 }
167 
169  .enc_name = "MP4A-LATM",
170  .codec_type = AVMEDIA_TYPE_AUDIO,
171  .codec_id = AV_CODEC_ID_AAC,
172  .priv_data_size = sizeof(PayloadContext),
173  .parse_sdp_a_line = latm_parse_sdp_line,
174  .close = latm_close_context,
176 };
AVPacket pkt
Definition: rtpdec_qt.c:37
static int latm_parse_sdp_line(AVFormatContext *s, int st_index, PayloadContext *data, const char *line)
Definition: rtpdec_latm.c:154
Bytestream IO Context.
Definition: avio.h:104
int avio_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
Return the written size and a pointer to the buffer.
Definition: aviobuf.c:1155
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:228
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:130
RTP/JPEG specific private data.
Definition: rdt.c:83
static int parse_fmtp(AVFormatContext *s, AVStream *stream, PayloadContext *data, const char *attr, const char *value)
Definition: rtpdec_latm.c:134
int index
stream index in AVFormatContext
Definition: avformat.h:706
av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (%s)\, len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt), use_generic ? ac->func_descr_generic :ac->func_descr)
int avio_open_dyn_buf(AVIOContext **s)
Open a write only memory stream.
Definition: aviobuf.c:1143
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:202
Format I/O context.
Definition: avformat.h:940
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
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1008
uint8_t * data
Definition: avcodec.h:1346
static int flags
Definition: log.c:50
bitstream reader API header.
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:221
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:84
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:526
uint32_t timestamp
current frame timestamp
Definition: rtpdec_ac3.c:31
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:124
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:190
#define RTP_FLAG_MARKER
RTP marker bit was set for this packet.
Definition: rtpdec.h:93
#define AVERROR(e)
Definition: error.h:43
RTPDynamicProtocolHandler ff_mp4a_latm_dynamic_handler
Definition: rtpdec_latm.c:168
Definition: graph2dot.c:48
int extradata_size
Size of the extradata content in bytes.
Definition: avcodec.h:3501
static void latm_close_context(PayloadContext *data)
Definition: rtpdec_latm.c:35
static int parse_fmtp_config(AVStream *st, const char *value)
Definition: rtpdec_latm.c:92
AVFormatContext * ctx
Definition: movenc.c:48
void ffio_free_dyn_buf(AVIOContext **s)
Free a dynamic buffer.
Definition: aviobuf.c:1183
Stream structure.
Definition: avformat.h:705
#define AVERROR_PATCHWELCOME
Not yet implemented in Libav, patches welcome.
Definition: error.h:57
NULL
Definition: eval.c:55
AVIOContext * data
Definition: rtpdec_vp8.c:36
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:259
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:362
uint8_t * buf
the temporary storage buffer
Definition: rtpdec_asf.c:168
int ff_parse_fmtp(AVFormatContext *s, AVStream *stream, PayloadContext *data, const char *p, int(*parse_fmtp)(AVFormatContext *s, AVStream *stream, PayloadContext *data, const char *attr, const char *value))
Definition: rtpdec.c:853
int ff_hex_to_data(uint8_t *data, const char *p)
Parse a string of hexadecimal strings.
Definition: utils.c:2958
const char * enc_name
Definition: rtpdec.h:116
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:32
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:638
AVIOContext * dyn_buf
Definition: rtpdec_latm.c:29
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:41
static int parse_packet(AVFormatContext *s, AVPacket *pkt, int stream_index)
Parse a packet, add all split parts to parse_queue.
Definition: utils.c:816
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition: avcodec.h:3497
AVCodecParameters * codecpar
Definition: avformat.h:831
int stream_index
Definition: avcodec.h:1348
This structure stores compressed data.
Definition: avcodec.h:1323
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:211