OSDN Git Service

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