FFmpeg
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 "avio_internal.h"
23 #include "rtpdec_formats.h"
24 #include "internal.h"
25 #include "libavutil/avstring.h"
26 #include "libavutil/mem.h"
27 #include "libavcodec/get_bits.h"
28 
29 struct PayloadContext {
31  uint8_t *buf;
32  int pos, len;
33  uint32_t timestamp;
34 };
35 
37 {
38  ffio_free_dyn_buf(&data->dyn_buf);
39  av_freep(&data->buf);
40 }
41 
43  AVStream *st, AVPacket *pkt, uint32_t *timestamp,
44  const uint8_t *buf, int len, uint16_t seq,
45  int flags)
46 {
47  int ret, cur_len;
48 
49  if (buf) {
50  if (!data->dyn_buf || data->timestamp != *timestamp) {
51  av_freep(&data->buf);
52  ffio_free_dyn_buf(&data->dyn_buf);
53 
54  data->timestamp = *timestamp;
55  if ((ret = avio_open_dyn_buf(&data->dyn_buf)) < 0)
56  return ret;
57  }
58  avio_write(data->dyn_buf, buf, len);
59 
60  if (!(flags & RTP_FLAG_MARKER))
61  return AVERROR(EAGAIN);
62  av_freep(&data->buf);
63  data->len = avio_close_dyn_buf(data->dyn_buf, &data->buf);
64  data->dyn_buf = NULL;
65  data->pos = 0;
66  }
67 
68  if (!data->buf) {
69  av_log(ctx, AV_LOG_ERROR, "No data available yet\n");
70  return AVERROR(EIO);
71  }
72 
73  cur_len = 0;
74  while (data->pos < data->len) {
75  uint8_t val = data->buf[data->pos++];
76  cur_len += val;
77  if (val != 0xff)
78  break;
79  }
80  if (data->pos + cur_len > data->len) {
81  av_log(ctx, AV_LOG_ERROR, "Malformed LATM packet\n");
82  return AVERROR(EIO);
83  }
84 
85  if ((ret = av_new_packet(pkt, cur_len)) < 0)
86  return ret;
87  memcpy(pkt->data, data->buf + data->pos, cur_len);
88  data->pos += cur_len;
89  pkt->stream_index = st->index;
90  return data->pos < data->len;
91 }
92 
93 static int parse_fmtp_config(AVStream *st, const char *value)
94 {
95  int len = ff_hex_to_data(NULL, value), i, ret = 0;
96  GetBitContext gb;
97  uint8_t *config;
98  int audio_mux_version, same_time_framing, num_programs, num_layers;
99 
100  /* Pad this buffer, too, to avoid out of bounds reads with get_bits below */
102  if (!config)
103  return AVERROR(ENOMEM);
105  ret = init_get_bits(&gb, config, len*8);
106  if (ret < 0)
107  return ret;
108  audio_mux_version = get_bits(&gb, 1);
109  same_time_framing = get_bits(&gb, 1);
110  skip_bits(&gb, 6); /* num_sub_frames */
111  num_programs = get_bits(&gb, 4);
112  num_layers = get_bits(&gb, 3);
113  if (audio_mux_version != 0 || same_time_framing != 1 || num_programs != 0 ||
114  num_layers != 0) {
115  avpriv_report_missing_feature(NULL, "LATM config (%d,%d,%d,%d)",
116  audio_mux_version, same_time_framing,
117  num_programs, num_layers);
119  goto end;
120  }
121  ret = ff_alloc_extradata(st->codecpar, (get_bits_left(&gb) + 7)/8);
122  if (ret < 0) {
123  goto end;
124  }
125  for (i = 0; i < st->codecpar->extradata_size; i++)
126  st->codecpar->extradata[i] = get_bits(&gb, 8);
127 
128 end:
129  av_free(config);
130  return ret;
131 }
132 
134  AVStream *stream, PayloadContext *data,
135  const char *attr, const char *value)
136 {
137  int res;
138 
139  if (!strcmp(attr, "config")) {
140  res = parse_fmtp_config(stream, value);
141  if (res < 0)
142  return res;
143  } else if (!strcmp(attr, "cpresent")) {
144  int cpresent = atoi(value);
145  if (cpresent != 0)
147  "RTP MP4A-LATM with in-band configuration");
148  }
149 
150  return 0;
151 }
152 
153 static int latm_parse_sdp_line(AVFormatContext *s, int st_index,
154  PayloadContext *data, const char *line)
155 {
156  const char *p;
157 
158  if (st_index < 0)
159  return 0;
160 
161  if (av_strstart(line, "fmtp:", &p))
162  return ff_parse_fmtp(s, s->streams[st_index], data, p, parse_fmtp);
163 
164  return 0;
165 }
166 
168  .enc_name = "MP4A-LATM",
169  .codec_type = AVMEDIA_TYPE_AUDIO,
170  .codec_id = AV_CODEC_ID_AAC,
171  .priv_data_size = sizeof(PayloadContext),
172  .parse_sdp_a_line = latm_parse_sdp_line,
173  .close = latm_close_context,
175 };
AVCodecParameters::extradata
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition: codec_par.h:69
get_bits_left
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:695
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
parse_fmtp
static int parse_fmtp(AVFormatContext *s, AVStream *stream, PayloadContext *data, const char *attr, const char *value)
Definition: rtpdec_latm.c:133
rtpdec_formats.h
ff_parse_fmtp
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:965
RTP_FLAG_MARKER
#define RTP_FLAG_MARKER
RTP marker bit was set for this packet.
Definition: rtpdec.h:94
AVPacket::data
uint8_t * data
Definition: packet.h:539
data
const char data[16]
Definition: mxf.c:149
tf_sess_config.config
config
Definition: tf_sess_config.py:33
init_get_bits
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:514
PayloadContext::dyn_buf
AVIOContext * dyn_buf
Definition: rtpdec_latm.c:30
PayloadContext::timestamp
uint32_t timestamp
current frame timestamp
Definition: rtpdec_ac3.c:31
RTPDynamicProtocolHandler::enc_name
const char * enc_name
Definition: rtpdec.h:117
skip_bits
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:381
get_bits
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:335
parse_fmtp_config
static int parse_fmtp_config(AVStream *st, const char *value)
Definition: rtpdec_latm.c:93
GetBitContext
Definition: get_bits.h:108
val
static double val(void *priv, double ch)
Definition: aeval.c:77
latm_parse_sdp_line
static int latm_parse_sdp_line(AVFormatContext *s, int st_index, PayloadContext *data, const char *line)
Definition: rtpdec_latm.c:153
avio_close_dyn_buf
int avio_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
Return the written size and a pointer to the buffer.
Definition: aviobuf.c:1407
latm_close_context
static void latm_close_context(PayloadContext *data)
Definition: rtpdec_latm.c:36
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
avio_open_dyn_buf
int avio_open_dyn_buf(AVIOContext **s)
Open a write only memory stream.
Definition: aviobuf.c:1362
s
#define s(width, name)
Definition: cbs_vp9.c:198
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
get_bits.h
PayloadContext::len
int len
Definition: rtpdec_latm.c:32
ff_hex_to_data
int ff_hex_to_data(uint8_t *data, const char *p)
Parse a string of hexadecimal strings.
Definition: utils.c:469
AVFormatContext
Format I/O context.
Definition: avformat.h:1300
ff_mp4a_latm_dynamic_handler
const RTPDynamicProtocolHandler ff_mp4a_latm_dynamic_handler
Definition: rtpdec_latm.c:167
internal.h
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:771
NULL
#define NULL
Definition: coverity.c:32
AVERROR_PATCHWELCOME
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:64
AVCodecParameters::extradata_size
int extradata_size
Size of the extradata content in bytes.
Definition: codec_par.h:73
AV_CODEC_ID_AAC
@ AV_CODEC_ID_AAC
Definition: codec_id.h:448
AVIOContext
Bytestream IO Context.
Definition: avio.h:160
avpriv_report_missing_feature
void avpriv_report_missing_feature(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
avio_write
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:201
line
Definition: graph2dot.c:48
av_strstart
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:36
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
avio_internal.h
value
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf default value
Definition: writing_filters.txt:86
av_mallocz
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:256
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
ret
ret
Definition: filter_design.txt:187
AVStream
Stream structure.
Definition: avformat.h:748
AV_INPUT_BUFFER_PADDING_SIZE
#define AV_INPUT_BUFFER_PADDING_SIZE
Definition: defs.h:40
PayloadContext::buf
uint8_t * buf
the temporary storage buffer
Definition: rtpdec_asf.c:184
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
avpriv_request_sample
#define avpriv_request_sample(...)
Definition: tableprint_vlc.h:36
av_free
#define av_free(p)
Definition: tableprint_vlc.h:33
latm_parse_packet
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:42
AVPacket
This structure stores compressed data.
Definition: packet.h:516
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
PayloadContext::pos
int pos
Definition: rtpdec_latm.c:32
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:482
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
avstring.h
PayloadContext
RTP/JPEG specific private data.
Definition: rdt.c:85
RTPDynamicProtocolHandler
Definition: rtpdec.h:116
ff_alloc_extradata
int ff_alloc_extradata(AVCodecParameters *par, int size)
Allocate extradata with additional AV_INPUT_BUFFER_PADDING_SIZE at end which is always set to 0.
Definition: utils.c:227