comparison rtpdec_latm.c @ 6373:920c249caa55 libavformat

Add RTP depacketization of MP4A-LATM
author mstorsjo
date Thu, 12 Aug 2010 21:07:17 +0000
parents
children
comparison
equal deleted inserted replaced
6372:f8c3cb8c503e 6373:920c249caa55
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 #include <strings.h>
27
28 struct PayloadContext {
29 ByteIOContext *dyn_buf;
30 uint8_t *buf;
31 int pos, len;
32 uint32_t timestamp;
33 };
34
35 static PayloadContext *latm_new_context(void)
36 {
37 return av_mallocz(sizeof(PayloadContext));
38 }
39
40 static void latm_free_context(PayloadContext *data)
41 {
42 if (!data)
43 return;
44 if (data->dyn_buf) {
45 uint8_t *p;
46 url_close_dyn_buf(data->dyn_buf, &p);
47 av_free(p);
48 }
49 av_free(data->buf);
50 av_free(data);
51 }
52
53 static int latm_parse_packet(AVFormatContext *ctx, PayloadContext *data,
54 AVStream *st, AVPacket *pkt, uint32_t *timestamp,
55 const uint8_t *buf, int len, 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 url_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 = url_open_dyn_buf(&data->dyn_buf)) < 0)
69 return ret;
70 }
71 put_buffer(data->dyn_buf, buf, len);
72
73 if (!(flags & RTP_FLAG_MARKER))
74 return AVERROR(EAGAIN);
75 av_free(data->buf);
76 data->len = url_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_sub_frames,
112 num_programs, num_layers;
113
114 /* Pad this buffer, too, to avoid out of bounds reads with get_bits below */
115 config = av_mallocz(len + FF_INPUT_BUFFER_PADDING_SIZE);
116 if (!config)
117 return AVERROR(ENOMEM);
118 ff_hex_to_data(config, value);
119 init_get_bits(&gb, config, len*8);
120 audio_mux_version = get_bits(&gb, 1);
121 same_time_framing = get_bits(&gb, 1);
122 num_sub_frames = get_bits(&gb, 6);
123 num_programs = get_bits(&gb, 4);
124 num_layers = get_bits(&gb, 3);
125 if (audio_mux_version != 0 || same_time_framing != 1 || num_programs != 0 ||
126 num_layers != 0) {
127 av_log(NULL, AV_LOG_WARNING, "Unsupported LATM config (%d,%d,%d,%d)\n",
128 audio_mux_version, same_time_framing,
129 num_programs, num_layers);
130 ret = AVERROR_PATCHWELCOME;
131 goto end;
132 }
133 av_freep(&st->codec->extradata);
134 st->codec->extradata_size = (get_bits_left(&gb) + 7)/8;
135 st->codec->extradata = av_mallocz(st->codec->extradata_size +
136 FF_INPUT_BUFFER_PADDING_SIZE);
137 if (!st->codec->extradata) {
138 ret = AVERROR(ENOMEM);
139 goto end;
140 }
141 for (i = 0; i < st->codec->extradata_size; i++)
142 st->codec->extradata[i] = get_bits(&gb, 8);
143
144 end:
145 av_free(config);
146 return ret;
147 }
148
149 static int parse_fmtp(AVStream *stream, PayloadContext *data,
150 char *attr, char *value)
151 {
152 int res;
153
154 if (!strcmp(attr, "config")) {
155 res = parse_fmtp_config(stream, value);
156 if (res < 0)
157 return res;
158 } else if (!strcmp(attr, "cpresent")) {
159 int cpresent = atoi(value);
160 if (cpresent != 0)
161 av_log_missing_feature(NULL, "RTP MP4A-LATM with in-band "
162 "configuration", 1);
163 }
164
165 return 0;
166 }
167
168 static int latm_parse_sdp_line(AVFormatContext *s, int st_index,
169 PayloadContext *data, const char *line)
170 {
171 const char *p;
172
173 if (av_strstart(line, "fmtp:", &p))
174 return ff_parse_fmtp(s->streams[st_index], data, p, parse_fmtp);
175
176 return 0;
177 }
178
179 RTPDynamicProtocolHandler ff_mp4a_latm_dynamic_handler = {
180 .enc_name = "MP4A-LATM",
181 .codec_type = AVMEDIA_TYPE_AUDIO,
182 .codec_id = CODEC_ID_AAC,
183 .parse_sdp_a_line = latm_parse_sdp_line,
184 .open = latm_new_context,
185 .close = latm_free_context,
186 .parse_packet = latm_parse_packet
187 };