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