[FFmpeg-devel] [PATCH 1/3] oggdec: add support for Opus codec.

Nicolas George nicolas.george at normalesup.org
Wed Jul 4 16:43:48 CEST 2012


This patch also introduces CODEC_ID_OPUS.

Signed-off-by: Nicolas George <nicolas.george at normalesup.org>
---
 libavcodec/avcodec.h       |    1 +
 libavformat/Makefile       |    1 +
 libavformat/oggdec.c       |    1 +
 libavformat/oggdec.h       |    1 +
 libavformat/oggparseopus.c |  121 ++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 125 insertions(+)
 create mode 100644 libavformat/oggparseopus.c


Changes since previous version:

codec->extradata now holds the whole OpusHead packet, as mandated by the
MatroskaOpus draft.

codec->sample_rate is set to 48000 unconditionnally.


diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h
index 9748ba6..71e9e9e 100644
--- a/libavcodec/avcodec.h
+++ b/libavcodec/avcodec.h
@@ -416,6 +416,7 @@ enum CodecID {
     CODEC_ID_8SVX_RAW    = MKBETAG('8','S','V','X'),
     CODEC_ID_SONIC       = MKBETAG('S','O','N','C'),
     CODEC_ID_SONIC_LS    = MKBETAG('S','O','N','L'),
+    CODEC_ID_OPUS        = MKBETAG('O','P','U','S'),
 
     /* subtitle codecs */
     CODEC_ID_FIRST_SUBTITLE = 0x17000,          ///< A dummy ID pointing at the start of subtitle codecs.
diff --git a/libavformat/Makefile b/libavformat/Makefile
index 9eaf28a..69eec0c 100644
--- a/libavformat/Makefile
+++ b/libavformat/Makefile
@@ -200,6 +200,7 @@ OBJS-$(CONFIG_OGG_DEMUXER)               += oggdec.o         \
                                             oggparsedirac.o  \
                                             oggparseflac.o   \
                                             oggparseogm.o    \
+                                            oggparseopus.o   \
                                             oggparseskeleton.o \
                                             oggparsespeex.o  \
                                             oggparsetheora.o \
diff --git a/libavformat/oggdec.c b/libavformat/oggdec.c
index d30c17c..b2d734a 100644
--- a/libavformat/oggdec.c
+++ b/libavformat/oggdec.c
@@ -46,6 +46,7 @@ static const struct ogg_codec * const ogg_codecs[] = {
     &ff_theora_codec,
     &ff_flac_codec,
     &ff_celt_codec,
+    &ff_opus_codec,
     &ff_old_dirac_codec,
     &ff_old_flac_codec,
     &ff_ogm_video_codec,
diff --git a/libavformat/oggdec.h b/libavformat/oggdec.h
index 7f5452f..aa94db5 100644
--- a/libavformat/oggdec.h
+++ b/libavformat/oggdec.h
@@ -108,6 +108,7 @@ extern const struct ogg_codec ff_ogm_text_codec;
 extern const struct ogg_codec ff_ogm_video_codec;
 extern const struct ogg_codec ff_old_dirac_codec;
 extern const struct ogg_codec ff_old_flac_codec;
+extern const struct ogg_codec ff_opus_codec;
 extern const struct ogg_codec ff_skeleton_codec;
 extern const struct ogg_codec ff_speex_codec;
 extern const struct ogg_codec ff_theora_codec;
diff --git a/libavformat/oggparseopus.c b/libavformat/oggparseopus.c
new file mode 100644
index 0000000..861bddd
--- /dev/null
+++ b/libavformat/oggparseopus.c
@@ -0,0 +1,121 @@
+/*
+ * Opus parser for Ogg
+ * Copyright (c) 2012 Nicolas George
+ *
+ * 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 <string.h>
+
+#include "libavutil/intreadwrite.h"
+#include "avformat.h"
+#include "internal.h"
+#include "oggdec.h"
+
+struct oggopus_private {
+    int need_comments;
+};
+
+#define OPUS_HEAD_SIZE 19
+
+static int opus_header(AVFormatContext *avf, int idx)
+{
+    struct ogg *ogg = avf->priv_data;
+    struct ogg_stream *os = &ogg->streams[idx];
+    AVStream *st = avf->streams[idx];
+    struct oggopus_private *priv = os->private;
+    uint8_t *packet = os->buf + os->pstart;
+
+    if (!priv) {
+        priv = os->private = av_mallocz(sizeof(struct oggopus_private));
+        if (!priv)
+            return AVERROR(ENOMEM);
+    }
+    if (os->flags & OGG_FLAG_BOS) {
+        if (os->psize < OPUS_HEAD_SIZE || AV_RL8(packet + 8) != 1)
+            return AVERROR_INVALIDDATA;
+        st->codec->codec_type  = AVMEDIA_TYPE_AUDIO;
+        st->codec->codec_id    = CODEC_ID_OPUS;
+        st->codec->channels    = AV_RL8 (packet + 9);
+        /*pre_skip             = AV_RL16(packet + 10);*/
+        /*orig_sample_rate     = AV_RL32(packet + 12);*/
+        /*gain                 = AV_RL16(packet + 16);*/
+        /*channel_map          = AV_RL8 (packet + 18);*/
+        if (os->psize > OPUS_HEAD_SIZE) {
+            uint8_t *extradata = av_malloc(os->psize +
+                                           FF_INPUT_BUFFER_PADDING_SIZE);
+            if (!extradata)
+                return AVERROR(ENOMEM);
+            memcpy(extradata, packet, os->psize);
+            st->codec->extradata = extradata;
+            st->codec->extradata_size = os->psize;
+        }
+
+        st->codec->sample_rate = 48000;
+        avpriv_set_pts_info(st, 64, 1, 48000);
+        priv->need_comments = 1;
+        return 1;
+    }
+
+    if (priv->need_comments) {
+        if (os->psize < 8 || memcmp(packet, "OpusTags", 8))
+            return AVERROR_INVALIDDATA;
+        ff_vorbis_comment(avf, &st->metadata, packet + 8, os->psize - 8);
+        priv->need_comments--;
+        return 1;
+    }
+    return 0;
+}
+
+static int opus_packet(AVFormatContext *avf, int idx)
+{
+    struct ogg *ogg = avf->priv_data;
+    struct ogg_stream *os = &ogg->streams[idx];
+    uint8_t *packet = os->buf + os->pstart;
+    unsigned toc, toc_config, frame_size, nb_frames = 1;
+
+    if (!os->psize)
+        return AVERROR_INVALIDDATA;
+    toc = *packet;
+    toc_config = toc >> 2;
+    frame_size = toc_config < 12 ? FFMAX(480, 960 * (toc_config & 3)) :
+                 toc_config < 16 ? 480 << (toc_config & 1) :
+                                   120 << (toc_config & 3);
+    if ((toc & 3) == 3) {
+        if (os->psize < 2)
+            return AVERROR_INVALIDDATA;
+        nb_frames = packet[1] & 0x3F;
+    } else if ((toc & 3)) {
+        nb_frames = 2;
+    }
+    os->pduration = frame_size * nb_frames;
+    /*if (os->lastpts != AV_NOPTS_VALUE) {
+        if (st->start_time == AV_NOPTS_VALUE)
+            st->start_time = os->lastpts;
+        os->lastdts = os->lastpts -= st->codec->skip_data;
+    }*/
+    return 0;
+}
+
+const struct ogg_codec ff_opus_codec = {
+    .name      = "Opus",
+    .magic     = "OpusHead",
+    .magicsize = 8,
+    .header    = opus_header,
+    .packet    = opus_packet,
+};
+
-- 
1.7.10



More information about the ffmpeg-devel mailing list