Line |
Branch |
Exec |
Source |
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 { |
30 |
|
|
AVIOContext *dyn_buf; |
31 |
|
|
uint8_t *buf; |
32 |
|
|
int pos, len; |
33 |
|
|
uint32_t timestamp; |
34 |
|
|
}; |
35 |
|
|
|
36 |
|
✗ |
static void latm_close_context(PayloadContext *data) |
37 |
|
|
{ |
38 |
|
✗ |
ffio_free_dyn_buf(&data->dyn_buf); |
39 |
|
✗ |
av_freep(&data->buf); |
40 |
|
✗ |
} |
41 |
|
|
|
42 |
|
✗ |
static int latm_parse_packet(AVFormatContext *ctx, PayloadContext *data, |
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 */ |
101 |
|
✗ |
config = av_mallocz(len + AV_INPUT_BUFFER_PADDING_SIZE); |
102 |
|
✗ |
if (!config) |
103 |
|
✗ |
return AVERROR(ENOMEM); |
104 |
|
✗ |
ff_hex_to_data(config, value); |
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); |
118 |
|
✗ |
ret = AVERROR_PATCHWELCOME; |
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 |
|
|
|
133 |
|
✗ |
static int parse_fmtp(AVFormatContext *s, |
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) |
146 |
|
✗ |
avpriv_request_sample(s, |
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 |
|
|
|
167 |
|
|
const RTPDynamicProtocolHandler ff_mp4a_latm_dynamic_handler = { |
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, |
174 |
|
|
.parse_packet = latm_parse_packet, |
175 |
|
|
}; |
176 |
|
|
|