rtpdec_latm.c
Go to the documentation of this file.
1 
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, int flags)
55 {
56  int ret, cur_len;
57 
58  if (buf) {
59  if (!data->dyn_buf || data->timestamp != *timestamp) {
60  av_freep(&data->buf);
61  if (data->dyn_buf)
62  avio_close_dyn_buf(data->dyn_buf, &data->buf);
63  data->dyn_buf = NULL;
64  av_freep(&data->buf);
65 
66  data->timestamp = *timestamp;
67  if ((ret = avio_open_dyn_buf(&data->dyn_buf)) < 0)
68  return ret;
69  }
70  avio_write(data->dyn_buf, buf, len);
71 
72  if (!(flags & RTP_FLAG_MARKER))
73  return AVERROR(EAGAIN);
74  av_free(data->buf);
75  data->len = avio_close_dyn_buf(data->dyn_buf, &data->buf);
76  data->dyn_buf = NULL;
77  data->pos = 0;
78  }
79 
80  if (!data->buf) {
81  av_log(ctx, AV_LOG_ERROR, "No data available yet\n");
82  return AVERROR(EIO);
83  }
84 
85  cur_len = 0;
86  while (data->pos < data->len) {
87  uint8_t val = data->buf[data->pos++];
88  cur_len += val;
89  if (val != 0xff)
90  break;
91  }
92  if (data->pos + cur_len > data->len) {
93  av_log(ctx, AV_LOG_ERROR, "Malformed LATM packet\n");
94  return AVERROR(EIO);
95  }
96 
97  if ((ret = av_new_packet(pkt, cur_len)) < 0)
98  return ret;
99  memcpy(pkt->data, data->buf + data->pos, cur_len);
100  data->pos += cur_len;
101  pkt->stream_index = st->index;
102  return data->pos < data->len;
103 }
104 
105 static int parse_fmtp_config(AVStream *st, char *value)
106 {
107  int len = ff_hex_to_data(NULL, value), i, ret = 0;
108  GetBitContext gb;
109  uint8_t *config;
110  int audio_mux_version, same_time_framing, num_programs, num_layers;
111 
112  /* Pad this buffer, too, to avoid out of bounds reads with get_bits below */
114  if (!config)
115  return AVERROR(ENOMEM);
116  ff_hex_to_data(config, value);
117  init_get_bits(&gb, config, len*8);
118  audio_mux_version = get_bits(&gb, 1);
119  same_time_framing = get_bits(&gb, 1);
120  skip_bits(&gb, 6); /* num_sub_frames */
121  num_programs = get_bits(&gb, 4);
122  num_layers = get_bits(&gb, 3);
123  if (audio_mux_version != 0 || same_time_framing != 1 || num_programs != 0 ||
124  num_layers != 0) {
125  av_log(NULL, AV_LOG_WARNING, "Unsupported LATM config (%d,%d,%d,%d)\n",
126  audio_mux_version, same_time_framing,
127  num_programs, num_layers);
128  ret = AVERROR_PATCHWELCOME;
129  goto end;
130  }
131  av_freep(&st->codec->extradata);
132  st->codec->extradata_size = (get_bits_left(&gb) + 7)/8;
135  if (!st->codec->extradata) {
136  ret = AVERROR(ENOMEM);
137  goto end;
138  }
139  for (i = 0; i < st->codec->extradata_size; i++)
140  st->codec->extradata[i] = get_bits(&gb, 8);
141 
142 end:
143  av_free(config);
144  return ret;
145 }
146 
147 static int parse_fmtp(AVStream *stream, PayloadContext *data,
148  char *attr, char *value)
149 {
150  int res;
151 
152  if (!strcmp(attr, "config")) {
153  res = parse_fmtp_config(stream, value);
154  if (res < 0)
155  return res;
156  } else if (!strcmp(attr, "cpresent")) {
157  int cpresent = atoi(value);
158  if (cpresent != 0)
159  av_log_missing_feature(NULL, "RTP MP4A-LATM with in-band "
160  "configuration", 1);
161  }
162 
163  return 0;
164 }
165 
166 static int latm_parse_sdp_line(AVFormatContext *s, int st_index,
167  PayloadContext *data, const char *line)
168 {
169  const char *p;
170 
171  if (av_strstart(line, "fmtp:", &p))
172  return ff_parse_fmtp(s->streams[st_index], data, p, parse_fmtp);
173 
174  return 0;
175 }
176 
178  .enc_name = "MP4A-LATM",
179  .codec_type = AVMEDIA_TYPE_AUDIO,
180  .codec_id = CODEC_ID_AAC,
181  .parse_sdp_a_line = latm_parse_sdp_line,
182  .alloc = latm_new_context,
183  .free = latm_free_context,
184  .parse_packet = latm_parse_packet
185 };
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:154
static int latm_parse_sdp_line(AVFormatContext *s, int st_index, PayloadContext *data, const char *line)
Definition: rtpdec_latm.c:166
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:1196
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:237
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:60
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:117
RTP/H264 specific private data.
Definition: rdt.c:83
int index
stream index in AVFormatContext
Definition: avformat.h:621
int avio_open_dyn_buf(AVIOContext **s)
Open a write only memory stream.
Definition: aviobuf.c:1184
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:147
Format I/O context.
Definition: avformat.h:863
static int parse_fmtp_config(AVStream *st, char *value)
Definition: rtpdec_latm.c:105
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1387
AVStream ** streams
Definition: avformat.h:908
const char data[16]
Definition: mxf.c:60
uint8_t * data
Definition: avcodec.h:908
static int flags
Definition: log.c:34
bitstream reader API header.
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:190
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:513
uint32_t timestamp
timestamp of next-to-be-returned packet
Definition: rtpdec_latm.c:31
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:137
#define RTP_FLAG_MARKER
RTP marker bit was set for this packet.
Definition: rtpdec.h:94
#define AVERROR(e)
Definition: error.h:43
RTPDynamicProtocolHandler ff_mp4a_latm_dynamic_handler
Definition: rtpdec_latm.c:177
Definition: graph2dot.c:39
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:140
const char enc_name[50]
Definition: rtpdec.h:117
AVCodecContext * codec
codec context
Definition: avformat.h:623
uint8_t * buf
Definition: rtpdec_asf.c:155
Stream structure.
Definition: avformat.h:620
#define AVERROR_PATCHWELCOME
Not yet implemented in Libav, patches welcome.
Definition: error.h:57
NULL
Definition: eval.c:50
static int parse_fmtp(AVStream *stream, PayloadContext *data, char *attr, char *value)
Definition: rtpdec_latm.c:147
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:111
int extradata_size
Definition: avcodec.h:1388
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:260
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:354
#define FF_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:497
int ff_hex_to_data(uint8_t *data, const char *p)
Parse a string of hexadecimal strings.
Definition: utils.c:3792
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:29
AVIOContext * dyn_buf
Definition: rtpdec_latm.c:28
int len
static int latm_parse_packet(AVFormatContext *ctx, PayloadContext *data, AVStream *st, AVPacket *pkt, uint32_t *timestamp, const uint8_t *buf, int len, 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:757
int stream_index
Definition: avcodec.h:910
void av_log_missing_feature(void *avc, const char *feature, int want_sample)
Log a generic warning message about a missing feature.
Definition: utils.c:1714
static void latm_free_context(PayloadContext *data)
Definition: rtpdec_latm.c:39