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