OSDN Git Service

0bc9edef97f3e9098286167463f9cb2ee5f5bc23
[android-x86/external-ffmpeg.git] / libavformat / rtpdec_mpa_robust.c
1 /*
2  * RTP parser for loss tolerant payload format for MP3 audio (RFC 5219)
3  * Copyright (c) 2015 Gilles Chanteperdrix <gch@xenomai.org>
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 "libavutil/attributes.h"
23 #include "libavutil/intreadwrite.h"
24
25 #include "rtpdec_formats.h"
26
27 struct PayloadContext {
28     unsigned adu_size;
29     unsigned cur_size;
30     uint32_t timestamp;
31     uint8_t *split_buf;
32     int split_pos, split_buf_size, split_pkts;
33     AVIOContext *fragment;
34 };
35
36 static inline void free_fragment(PayloadContext *data)
37 {
38     if (data->fragment) {
39         uint8_t *p;
40         avio_close_dyn_buf(data->fragment, &p);
41         av_free(p);
42         data->fragment = NULL;
43     }
44 }
45
46 static void mpa_robust_free_context(PayloadContext *data)
47 {
48     free_fragment(data);
49     av_free(data->split_buf);
50     av_free(data);
51 }
52
53 static int mpa_robust_parse_rtp_header(AVFormatContext *ctx,
54                                        const uint8_t *buf, int len,
55                                        unsigned *adu_size, unsigned *cont)
56 {
57     unsigned header_size;
58
59     if (len < 2) {
60         av_log(ctx, AV_LOG_ERROR, "Invalid %d bytes packet\n", len);
61         return AVERROR_INVALIDDATA;
62     }
63
64     *cont = !!(buf[0] & 0x80);
65     if (!(buf[0] & 0x40)) {
66         header_size = 1;
67         *adu_size = buf[0] & ~0xc0;
68     } else {
69         header_size = 2;
70         *adu_size = AV_RB16(buf) & ~0xc000;
71     }
72
73     return header_size;
74 }
75
76 static int mpa_robust_parse_packet(AVFormatContext *ctx, PayloadContext *data,
77                                    AVStream *st, AVPacket *pkt,
78                                    uint32_t *timestamp, const uint8_t *buf,
79                                    int len, uint16_t seq, int flags)
80 {
81     unsigned adu_size, continuation;
82     int err, header_size;
83
84     if (!buf) {
85         buf = &data->split_buf[data->split_pos];
86         len = data->split_buf_size - data->split_pos;
87
88         header_size = mpa_robust_parse_rtp_header(ctx, buf, len, &adu_size,
89                                                   &continuation);
90         if (header_size < 0) {
91             av_freep(&data->split_buf);
92             return header_size;
93         }
94         buf += header_size;
95         len -= header_size;
96
97         if (continuation || adu_size > len) {
98             av_freep(&data->split_buf);
99             av_log(ctx, AV_LOG_ERROR, "Invalid frame\n");
100             return AVERROR_INVALIDDATA;
101         }
102
103         if (av_new_packet(pkt, adu_size)) {
104             av_log(ctx, AV_LOG_ERROR, "Out of memory.\n");
105             return AVERROR(ENOMEM);
106         }
107
108         pkt->stream_index = st->index;
109         memcpy(pkt->data, buf, adu_size);
110
111         data->split_pos += adu_size;
112
113         if (data->split_pos == data->split_buf_size) {
114             av_freep(&data->split_buf);
115             return 0;
116         }
117
118         return 1;
119     }
120
121
122     header_size = mpa_robust_parse_rtp_header(ctx, buf, len, &adu_size,
123                                               &continuation);
124     if (header_size < 0)
125         return header_size;
126
127     buf += header_size;
128     len -= header_size;
129
130     if (!continuation && adu_size <= len) {
131         /* One or more complete frames */
132
133         if (av_new_packet(pkt, adu_size)) {
134             av_log(ctx, AV_LOG_ERROR, "Out of memory.\n");
135             return AVERROR(ENOMEM);
136         }
137
138         pkt->stream_index = st->index;
139         memcpy(pkt->data, buf, adu_size);
140
141         buf += adu_size;
142         len -= adu_size;
143         if (len) {
144             data->split_buf_size = len;
145             data->split_buf = av_malloc(data->split_buf_size);
146             data->split_pos = 0;
147             if (!data->split_buf) {
148                 av_log(ctx, AV_LOG_ERROR, "Out of memory.\n");
149                 av_free_packet(pkt);
150                 return AVERROR(ENOMEM);
151             }
152             memcpy(data->split_buf, buf, data->split_buf_size);
153             return 1;
154         }
155         return 0;
156     } else if (!continuation) { /* && adu_size > len */
157         /* First fragment */
158         free_fragment(data);
159
160         data->adu_size = adu_size;
161         data->cur_size = len;
162         data->timestamp = *timestamp;
163
164         err = avio_open_dyn_buf(&data->fragment);
165         if (err < 0)
166             return err;
167
168         avio_write(data->fragment, buf, len);
169         return AVERROR(EAGAIN);
170     }
171     /* else continuation == 1 */
172
173     /* Fragment other than first */
174     if (!data->fragment) {
175         av_log(ctx, AV_LOG_WARNING,
176             "Received packet without a start fragment; dropping.\n");
177         return AVERROR(EAGAIN);
178     }
179     if (adu_size = data->adu_size ||
180         data->timestamp != *timestamp) {
181         free_fragment(data);
182         av_log(ctx, AV_LOG_ERROR, "Invalid packet received\n");
183         return AVERROR_INVALIDDATA;
184     }
185
186     avio_write(data->fragment, buf, len);
187     data->cur_size += len;
188
189     if (data->cur_size < data->adu_size)
190         return AVERROR(EAGAIN);
191
192     err = ff_rtp_finalize_packet(pkt, &data->fragment, st->index);
193     if (err < 0) {
194         av_log(ctx, AV_LOG_ERROR,
195                "Error occurred when getting fragment buffer.\n");
196         return err;
197     }
198
199     return 0;
200 }
201
202 RTPDynamicProtocolHandler ff_mpeg_audio_robust_dynamic_handler = {
203     .enc_name          = "mpa-robust",
204     .codec_type        = AVMEDIA_TYPE_AUDIO,
205     .codec_id          = AV_CODEC_ID_MP3ADU,
206     .need_parsing      = AVSTREAM_PARSE_HEADERS,
207     .priv_data_size    = sizeof(PayloadContext),
208     .free              = mpa_robust_free_context,
209     .parse_packet      = mpa_robust_parse_packet,
210 };