]> git.sesse.net Git - ffmpeg/blob - libavformat/rtpdec_ac3.c
Merge commit 'b651c9139e1ab222d5aab9151dcd7d6e40e49885'
[ffmpeg] / libavformat / rtpdec_ac3.c
1 /*
2  * RTP parser for AC3 payload format (RFC 4184)
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 "avformat.h"
23 #include "rtpdec_formats.h"
24
25 #define RTP_AC3_PAYLOAD_HEADER_SIZE 2
26
27 struct PayloadContext {
28     unsigned nr_frames;
29     unsigned last_frame;
30     uint32_t timestamp;
31     AVIOContext *fragment;
32 };
33
34 static av_cold int ac3_init(AVFormatContext *s, int st_index,
35                             PayloadContext *data)
36 {
37     if (st_index < 0)
38         return 0;
39     s->streams[st_index]->need_parsing = AVSTREAM_PARSE_FULL;
40     return 0;
41 }
42
43 static PayloadContext *ac3_new_context(void)
44 {
45     return av_mallocz(sizeof(PayloadContext));
46 }
47
48 static void free_fragment(PayloadContext *data)
49 {
50     if (data->fragment) {
51         uint8_t *p;
52         avio_close_dyn_buf(data->fragment, &p);
53         av_free(p);
54         data->fragment = NULL;
55     }
56 }
57
58 static void ac3_free_context(PayloadContext *data)
59 {
60     free_fragment(data);
61     av_free(data);
62 }
63
64 static int ac3_handle_packet(AVFormatContext *ctx, PayloadContext *data,
65                              AVStream *st, AVPacket *pkt, uint32_t *timestamp,
66                              const uint8_t *buf, int len, uint16_t seq,
67                              int flags)
68 {
69     unsigned frame_type;
70     unsigned nr_frames;
71     int err;
72
73     if (len < RTP_AC3_PAYLOAD_HEADER_SIZE + 1) {
74         av_log(ctx, AV_LOG_ERROR, "Invalid %d bytes packet\n", len);
75         return AVERROR_INVALIDDATA;
76     }
77
78     frame_type = buf[0] & 0x3;
79     nr_frames = buf[1];
80     buf += RTP_AC3_PAYLOAD_HEADER_SIZE;
81     len -= RTP_AC3_PAYLOAD_HEADER_SIZE;
82
83     switch (frame_type) {
84     case 0: /* One or more complete frames */
85         if (!nr_frames) {
86             av_log(ctx, AV_LOG_ERROR, "Invalid AC3 packet data\n");
87             return AVERROR_INVALIDDATA;
88         }
89         if (av_new_packet(pkt, len)) {
90             av_log(ctx, AV_LOG_ERROR, "Out of memory.\n");
91             return AVERROR(ENOMEM);
92         }
93
94         pkt->stream_index = st->index;
95         memcpy(pkt->data, buf, len);
96         return 0;
97
98     case 1:
99     case 2: /* First fragment */
100         free_fragment(data);
101
102         data->last_frame = 1;
103         data->nr_frames = nr_frames;
104         err = avio_open_dyn_buf(&data->fragment);
105         if (err < 0)
106             return err;
107
108         avio_write(data->fragment, buf, len);
109         data->timestamp = *timestamp;
110         return AVERROR(EAGAIN);
111
112     case 3: /* Fragment other than first */
113         if (!data->fragment) {
114             av_log(ctx, AV_LOG_WARNING,
115                    "Received packet without a start fragment; dropping.\n");
116             return AVERROR(EAGAIN);
117         }
118         if (nr_frames != data->nr_frames ||
119             data->timestamp != *timestamp) {
120             free_fragment(data);
121             av_log(ctx, AV_LOG_ERROR, "Invalid packet received\n");
122             return AVERROR_INVALIDDATA;
123         }
124
125         avio_write(data->fragment, buf, len);
126         data->last_frame++;
127     }
128
129     if (!(flags & RTP_FLAG_MARKER))
130         return AVERROR(EAGAIN);
131
132     if (data->last_frame != data->nr_frames) {
133         free_fragment(data);
134         av_log(ctx, AV_LOG_ERROR, "Missed %d packets\n",
135                data->nr_frames - data->last_frame);
136         return AVERROR_INVALIDDATA;
137     }
138
139     err = ff_rtp_finalize_packet(pkt, &data->fragment, st->index);
140     if (err < 0) {
141         av_log(ctx, AV_LOG_ERROR,
142                "Error occurred when getting fragment buffer.\n");
143         return err;
144     }
145
146     return 0;
147 }
148
149 RTPDynamicProtocolHandler ff_ac3_dynamic_handler = {
150     .enc_name           = "ac3",
151     .codec_type         = AVMEDIA_TYPE_AUDIO,
152     .codec_id           = AV_CODEC_ID_AC3,
153     .init               = ac3_init,
154     .alloc              = ac3_new_context,
155     .free               = ac3_free_context,
156     .parse_packet       = ac3_handle_packet,
157 };