]> git.sesse.net Git - ffmpeg/blob - libavformat/rtpdec_latm.c
6d5b46a72651d2613c825353bc1191ef5fe783f9
[ffmpeg] / libavformat / rtpdec_latm.c
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 "rtpdec_formats.h"
23 #include "internal.h"
24 #include "libavutil/avstring.h"
25 #include "libavcodec/get_bits.h"
26
27 struct PayloadContext {
28     AVIOContext *dyn_buf;
29     uint8_t *buf;
30     int pos, len;
31     uint32_t timestamp;
32 };
33
34 static void latm_free_context(PayloadContext *data)
35 {
36     if (data->dyn_buf) {
37         uint8_t *p;
38         avio_close_dyn_buf(data->dyn_buf, &p);
39         av_free(p);
40     }
41     av_free(data->buf);
42     av_free(data);
43 }
44
45 static int latm_parse_packet(AVFormatContext *ctx, PayloadContext *data,
46                              AVStream *st, AVPacket *pkt, uint32_t *timestamp,
47                              const uint8_t *buf, int len, uint16_t seq,
48                              int flags)
49 {
50     int ret, cur_len;
51
52     if (buf) {
53         if (!data->dyn_buf || data->timestamp != *timestamp) {
54             av_freep(&data->buf);
55             if (data->dyn_buf)
56                 avio_close_dyn_buf(data->dyn_buf, &data->buf);
57             data->dyn_buf = NULL;
58             av_freep(&data->buf);
59
60             data->timestamp = *timestamp;
61             if ((ret = avio_open_dyn_buf(&data->dyn_buf)) < 0)
62                 return ret;
63         }
64         avio_write(data->dyn_buf, buf, len);
65
66         if (!(flags & RTP_FLAG_MARKER))
67             return AVERROR(EAGAIN);
68         av_free(data->buf);
69         data->len = avio_close_dyn_buf(data->dyn_buf, &data->buf);
70         data->dyn_buf = NULL;
71         data->pos = 0;
72     }
73
74     if (!data->buf) {
75         av_log(ctx, AV_LOG_ERROR, "No data available yet\n");
76         return AVERROR(EIO);
77     }
78
79     cur_len = 0;
80     while (data->pos < data->len) {
81         uint8_t val = data->buf[data->pos++];
82         cur_len += val;
83         if (val != 0xff)
84             break;
85     }
86     if (data->pos + cur_len > data->len) {
87         av_log(ctx, AV_LOG_ERROR, "Malformed LATM packet\n");
88         return AVERROR(EIO);
89     }
90
91     if ((ret = av_new_packet(pkt, cur_len)) < 0)
92         return ret;
93     memcpy(pkt->data, data->buf + data->pos, cur_len);
94     data->pos += cur_len;
95     pkt->stream_index = st->index;
96     return data->pos < data->len;
97 }
98
99 static int parse_fmtp_config(AVStream *st, const char *value)
100 {
101     int len = ff_hex_to_data(NULL, value), i, ret = 0;
102     GetBitContext gb;
103     uint8_t *config;
104     int audio_mux_version, same_time_framing, num_programs, num_layers;
105
106     /* Pad this buffer, too, to avoid out of bounds reads with get_bits below */
107     config = av_mallocz(len + FF_INPUT_BUFFER_PADDING_SIZE);
108     if (!config)
109         return AVERROR(ENOMEM);
110     ff_hex_to_data(config, value);
111     init_get_bits(&gb, config, len*8);
112     audio_mux_version = get_bits(&gb, 1);
113     same_time_framing = get_bits(&gb, 1);
114     skip_bits(&gb, 6); /* num_sub_frames */
115     num_programs      = get_bits(&gb, 4);
116     num_layers        = get_bits(&gb, 3);
117     if (audio_mux_version != 0 || same_time_framing != 1 || num_programs != 0 ||
118         num_layers != 0) {
119         av_log(NULL, AV_LOG_WARNING, "Unsupported LATM config (%d,%d,%d,%d)\n",
120                                      audio_mux_version, same_time_framing,
121                                      num_programs, num_layers);
122         ret = AVERROR_PATCHWELCOME;
123         goto end;
124     }
125     av_freep(&st->codec->extradata);
126     st->codec->extradata_size = (get_bits_left(&gb) + 7)/8;
127     st->codec->extradata = av_mallocz(st->codec->extradata_size +
128                                       FF_INPUT_BUFFER_PADDING_SIZE);
129     if (!st->codec->extradata) {
130         ret = AVERROR(ENOMEM);
131         goto end;
132     }
133     for (i = 0; i < st->codec->extradata_size; i++)
134         st->codec->extradata[i] = get_bits(&gb, 8);
135
136 end:
137     av_free(config);
138     return ret;
139 }
140
141 static int parse_fmtp(AVFormatContext *s,
142                       AVStream *stream, PayloadContext *data,
143                       const char *attr, const char *value)
144 {
145     int res;
146
147     if (!strcmp(attr, "config")) {
148         res = parse_fmtp_config(stream, value);
149         if (res < 0)
150             return res;
151     } else if (!strcmp(attr, "cpresent")) {
152         int cpresent = atoi(value);
153         if (cpresent != 0)
154             avpriv_request_sample(s,
155                                   "RTP MP4A-LATM with in-band configuration");
156     }
157
158     return 0;
159 }
160
161 static int latm_parse_sdp_line(AVFormatContext *s, int st_index,
162                                PayloadContext *data, const char *line)
163 {
164     const char *p;
165
166     if (st_index < 0)
167         return 0;
168
169     if (av_strstart(line, "fmtp:", &p))
170         return ff_parse_fmtp(s, s->streams[st_index], data, p, parse_fmtp);
171
172     return 0;
173 }
174
175 RTPDynamicProtocolHandler ff_mp4a_latm_dynamic_handler = {
176     .enc_name           = "MP4A-LATM",
177     .codec_type         = AVMEDIA_TYPE_AUDIO,
178     .codec_id           = AV_CODEC_ID_AAC,
179     .priv_data_size     = sizeof(PayloadContext),
180     .parse_sdp_a_line   = latm_parse_sdp_line,
181     .free               = latm_free_context,
182     .parse_packet       = latm_parse_packet,
183 };