[FFmpeg-devel] [PATCH 3/3] avformat/rtpdec_ac3: add AC3 RTP depacketization (RFC 4184)

Gilles Chanteperdrix gilles.chanteperdrix at xenomai.org
Fri Feb 13 22:51:34 CET 2015


Signed-off-by: Gilles Chanteperdrix <gilles.chanteperdrix at xenomai.org>
---
 Changelog                    |   1 +
 MAINTAINERS                  |   1 +
 libavformat/Makefile         |   1 +
 libavformat/rtpdec.c         |   1 +
 libavformat/rtpdec_ac3.c     | 157 +++++++++++++++++++++++++++++++++++++++++++
 libavformat/rtpdec_formats.h |   1 +
 libavformat/version.h        |   4 +-
 7 files changed, 164 insertions(+), 2 deletions(-)
 create mode 100644 libavformat/rtpdec_ac3.c

diff --git a/Changelog b/Changelog
index 775fc79..085fd17 100644
--- a/Changelog
+++ b/Changelog
@@ -24,6 +24,7 @@ version <next>:
 - ported softpulldown filter from libmpcodecs as repeatfields filter
 - dcshift filter
 - RTP parser for loss tolerant payload format for MP3 audio (RFC 5219)
+- RTP parser for AC3 payload format (RFC 4184)
 
 
 version 2.5:
diff --git a/MAINTAINERS b/MAINTAINERS
index 13b211e..8430222 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -465,6 +465,7 @@ Muxers/Demuxers:
   rmdec.c, rmenc.c                      Ronald S. Bultje, Kostya Shishkov
   rtmp*                                 Kostya Shishkov
   rtp.c, rtpenc.c                       Martin Storsjo
+  rtpdec_ac3.*                          Gilles Chanteperdrix
   rtpdec_h261.*, rtpenc_h261.*          Thomas Volkert
   rtpdec_hevc.*, rtpenc_hevc.*          Thomas Volkert
   rtpdec_asf.*                          Ronald S. Bultje
diff --git a/libavformat/Makefile b/libavformat/Makefile
index ec312ba..073a42d 100644
--- a/libavformat/Makefile
+++ b/libavformat/Makefile
@@ -29,6 +29,7 @@ OBJS-$(CONFIG_RIFFENC)                   += riffenc.o
 OBJS-$(CONFIG_RTPDEC)                    += rdt.o                       \
                                             rtp.o                       \
                                             rtpdec.o                    \
+                                            rtpdec_ac3.o                \
                                             rtpdec_amr.o                \
                                             rtpdec_asf.o                \
                                             rtpdec_g726.o               \
diff --git a/libavformat/rtpdec.c b/libavformat/rtpdec.c
index c632340..550da4a 100644
--- a/libavformat/rtpdec.c
+++ b/libavformat/rtpdec.c
@@ -72,6 +72,7 @@ void ff_register_dynamic_payload_handler(RTPDynamicProtocolHandler *handler)
 
 void ff_register_rtp_dynamic_payload_handlers(void)
 {
+    ff_register_dynamic_payload_handler(&ff_ac3_dynamic_handler);
     ff_register_dynamic_payload_handler(&ff_amr_nb_dynamic_handler);
     ff_register_dynamic_payload_handler(&ff_amr_wb_dynamic_handler);
     ff_register_dynamic_payload_handler(&ff_g726_16_dynamic_handler);
diff --git a/libavformat/rtpdec_ac3.c b/libavformat/rtpdec_ac3.c
new file mode 100644
index 0000000..728f421
--- /dev/null
+++ b/libavformat/rtpdec_ac3.c
@@ -0,0 +1,157 @@
+/*
+ * RTP parser for AC3 payload format (RFC 4184)
+ * Copyright (c) 2015 Gilles Chanteperdrix <gch at xenomai.org>
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include "avformat.h"
+#include "rtpdec_formats.h"
+
+#define RTP_AC3_PAYLOAD_HEADER_SIZE 2
+
+struct PayloadContext {
+    unsigned nr_frames;
+    unsigned last_frame;
+    uint32_t timestamp;
+    AVIOContext *fragment;
+};
+
+static av_cold int ac3_init(AVFormatContext *s, int st_index, 
+			PayloadContext *data)
+{
+    if (st_index < 0)
+        return 0;
+    s->streams[st_index]->need_parsing = AVSTREAM_PARSE_FULL;
+    return 0;
+}
+
+static PayloadContext *ac3_new_context(void)
+{
+    return av_mallocz(sizeof(PayloadContext));
+}
+
+static inline void free_fragment_if_needed(PayloadContext *data)
+{
+    if (data->fragment) {
+        uint8_t *p;
+        avio_close_dyn_buf(data->fragment, &p);
+        av_free(p);
+        data->fragment = NULL;
+    }
+}
+
+static void ac3_free_context(PayloadContext *data)
+{
+    free_fragment_if_needed(data);
+    av_free(data);
+}
+
+static int ac3_handle_packet(AVFormatContext *ctx, PayloadContext *data,
+                              AVStream *st, AVPacket *pkt, uint32_t *timestamp,
+                              const uint8_t *buf, int len, uint16_t seq,
+                              int flags)
+{
+    unsigned frame_type;
+    unsigned nr_frames;
+    int err;
+
+    if (len < RTP_AC3_PAYLOAD_HEADER_SIZE + 1) {
+        av_log(ctx, AV_LOG_ERROR, "Invalid %d bytes packet\n", len);
+        return AVERROR_INVALIDDATA;
+    }
+
+    frame_type = buf[0] & 0x3;
+    nr_frames = buf[1];
+    buf += RTP_AC3_PAYLOAD_HEADER_SIZE;
+    len -= RTP_AC3_PAYLOAD_HEADER_SIZE;
+
+    switch (frame_type) {
+    case 0: /* One or more complete frames */
+        if (!nr_frames) {
+            av_log(ctx, AV_LOG_ERROR, "Invalid AC3 packet data\n");
+            return AVERROR_INVALIDDATA;
+        }
+        if (av_new_packet(pkt, len)) {
+            av_log(ctx, AV_LOG_ERROR, "Out of memory.\n");
+            return AVERROR(ENOMEM);
+        }
+
+        pkt->stream_index = st->index;
+        memcpy(pkt->data, buf, len);
+        return 0;
+
+    case 1:
+    case 2: /* First fragment */
+        free_fragment_if_needed(data);
+
+        data->last_frame = 1;
+        data->nr_frames = nr_frames;
+        err = avio_open_dyn_buf(&data->fragment);
+        if (err < 0)
+            return err;
+
+        avio_write(data->fragment, buf, len);
+        data->timestamp = *timestamp;
+        return AVERROR(EAGAIN);
+
+    case 3: /* Fragment other than first */
+        if (!data->fragment) {
+            av_log(ctx, AV_LOG_WARNING,
+                   "Received packet without a start fragment; dropping.\n");
+            return AVERROR(EAGAIN);
+        }
+        if (nr_frames != data->nr_frames ||
+            data->timestamp != *timestamp) {
+            free_fragment_if_needed(data);
+            av_log(ctx, AV_LOG_ERROR, "Invalid packet received\n");
+            return AVERROR_INVALIDDATA;
+        }
+
+        avio_write(data->fragment, buf, len);
+        data->last_frame++;
+    }
+
+    if (!(flags & RTP_FLAG_MARKER))
+        return AVERROR(EAGAIN);
+
+    if (data->last_frame != data->nr_frames) {
+        free_fragment_if_needed(data);
+        av_log(ctx, AV_LOG_ERROR, "Missed %d packets\n",
+               data->nr_frames - data->last_frame);
+        return AVERROR_INVALIDDATA;
+    }
+
+    err = ff_rtp_finalize_packet(pkt, &data->fragment, st->index);
+    if (err < 0) {
+        av_log(ctx, AV_LOG_ERROR,
+               "Error occurred when getting fragment buffer.");
+        return err;
+    }
+
+    return 0;
+}
+
+RTPDynamicProtocolHandler ff_ac3_dynamic_handler = {
+    .enc_name           = "ac3",
+    .codec_type         = AVMEDIA_TYPE_AUDIO,
+    .codec_id           = AV_CODEC_ID_AC3,
+    .init               = ac3_init,
+    .alloc              = ac3_new_context,
+    .free               = ac3_free_context,
+    .parse_packet       = ac3_handle_packet,
+};
diff --git a/libavformat/rtpdec_formats.h b/libavformat/rtpdec_formats.h
index da22d70..b38cf1b 100644
--- a/libavformat/rtpdec_formats.h
+++ b/libavformat/rtpdec_formats.h
@@ -35,6 +35,7 @@ int ff_h263_handle_packet(AVFormatContext *ctx, PayloadContext *data,
                           AVStream *st, AVPacket *pkt, uint32_t *timestamp,
                           const uint8_t *buf, int len, uint16_t seq, int flags);
 
+extern RTPDynamicProtocolHandler ff_ac3_dynamic_handler;
 extern RTPDynamicProtocolHandler ff_amr_nb_dynamic_handler;
 extern RTPDynamicProtocolHandler ff_amr_wb_dynamic_handler;
 extern RTPDynamicProtocolHandler ff_g726_16_dynamic_handler;
diff --git a/libavformat/version.h b/libavformat/version.h
index 84749ec..f7fe97e 100644
--- a/libavformat/version.h
+++ b/libavformat/version.h
@@ -30,8 +30,8 @@
 #include "libavutil/version.h"
 
 #define LIBAVFORMAT_VERSION_MAJOR 56
-#define LIBAVFORMAT_VERSION_MINOR  20
-#define LIBAVFORMAT_VERSION_MICRO 102
+#define LIBAVFORMAT_VERSION_MINOR  21
+#define LIBAVFORMAT_VERSION_MICRO 103
 
 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \
                                                LIBAVFORMAT_VERSION_MINOR, \
-- 
1.8.4



More information about the ffmpeg-devel mailing list