2 * RTP parser for AC3 payload format (RFC 4184)
3 * Copyright (c) 2015 Gilles Chanteperdrix <gch@xenomai.org>
5 * This file is part of Libav.
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.
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.
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
23 #include "rtpdec_formats.h"
25 #define RTP_AC3_PAYLOAD_HEADER_SIZE 2
27 struct PayloadContext {
31 AVIOContext *fragment;
34 static void free_fragment(PayloadContext *data)
38 avio_close_dyn_buf(data->fragment, &p);
40 data->fragment = NULL;
44 static void ac3_free_context(PayloadContext *data)
49 static int ac3_handle_packet(AVFormatContext *ctx, PayloadContext *data,
50 AVStream *st, AVPacket *pkt, uint32_t *timestamp,
51 const uint8_t *buf, int len, uint16_t seq,
58 if (len < RTP_AC3_PAYLOAD_HEADER_SIZE + 1) {
59 av_log(ctx, AV_LOG_ERROR, "Invalid %d bytes packet\n", len);
60 return AVERROR_INVALIDDATA;
63 frame_type = buf[0] & 0x3;
65 buf += RTP_AC3_PAYLOAD_HEADER_SIZE;
66 len -= RTP_AC3_PAYLOAD_HEADER_SIZE;
69 case 0: /* One or more complete frames */
71 av_log(ctx, AV_LOG_ERROR, "Invalid AC3 packet data\n");
72 return AVERROR_INVALIDDATA;
74 if (av_new_packet(pkt, len)) {
75 av_log(ctx, AV_LOG_ERROR, "Out of memory.\n");
76 return AVERROR(ENOMEM);
79 pkt->stream_index = st->index;
80 memcpy(pkt->data, buf, len);
84 case 2: /* First fragment */
88 data->nr_frames = nr_frames;
89 err = avio_open_dyn_buf(&data->fragment);
93 avio_write(data->fragment, buf, len);
94 data->timestamp = *timestamp;
95 return AVERROR(EAGAIN);
97 case 3: /* Fragment other than first */
98 if (!data->fragment) {
99 av_log(ctx, AV_LOG_WARNING,
100 "Received packet without a start fragment; dropping.\n");
101 return AVERROR(EAGAIN);
103 if (nr_frames != data->nr_frames ||
104 data->timestamp != *timestamp) {
106 av_log(ctx, AV_LOG_ERROR, "Invalid packet received\n");
107 return AVERROR_INVALIDDATA;
110 avio_write(data->fragment, buf, len);
114 if (!(flags & RTP_FLAG_MARKER))
115 return AVERROR(EAGAIN);
117 if (data->last_frame != data->nr_frames) {
119 av_log(ctx, AV_LOG_ERROR, "Missed %d packets\n",
120 data->nr_frames - data->last_frame);
121 return AVERROR_INVALIDDATA;
124 err = ff_rtp_finalize_packet(pkt, &data->fragment, st->index);
126 av_log(ctx, AV_LOG_ERROR,
127 "Error occurred when getting fragment buffer.\n");
134 RTPDynamicProtocolHandler ff_ac3_dynamic_handler = {
136 .codec_type = AVMEDIA_TYPE_AUDIO,
137 .codec_id = AV_CODEC_ID_AC3,
138 .need_parsing = AVSTREAM_PARSE_FULL,
139 .priv_data_size = sizeof(PayloadContext),
140 .free = ac3_free_context,
141 .parse_packet = ac3_handle_packet,