Browse code

oggdec: add support for Xiph's CELT codec

This patch also introduces CODEC_ID_CELT.

Signed-off-by: Nicolas George <nicolas.george@normalesup.org>
Signed-off-by: Diego Biurrun <diego@biurrun.de>

Nicolas George authored on 2011/09/26 20:19:05
Showing 8 changed files
... ...
@@ -48,6 +48,7 @@ easier to use. The changes are:
48 48
 - libcdio-paranoia input device for audio CD grabbing
49 49
 - select filter
50 50
 - Apple ProRes decoder
51
+- CELT in Ogg demuxing
51 52
 
52 53
 
53 54
 version 0.7:
... ...
@@ -341,6 +341,7 @@ enum CodecID {
341 341
     CODEC_ID_BINKAUDIO_DCT,
342 342
     CODEC_ID_AAC_LATM,
343 343
     CODEC_ID_QDMC,
344
+    CODEC_ID_CELT,
344 345
 
345 346
     /* subtitle codecs */
346 347
     CODEC_ID_FIRST_SUBTITLE = 0x17000,          ///< A dummy ID pointing at the start of subtitle codecs.
... ...
@@ -21,7 +21,7 @@
21 21
 #define AVCODEC_VERSION_H
22 22
 
23 23
 #define LIBAVCODEC_VERSION_MAJOR 53
24
-#define LIBAVCODEC_VERSION_MINOR  11
24
+#define LIBAVCODEC_VERSION_MINOR  12
25 25
 #define LIBAVCODEC_VERSION_MICRO  0
26 26
 
27 27
 #define LIBAVCODEC_VERSION_INT  AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \
... ...
@@ -161,6 +161,7 @@ OBJS-$(CONFIG_NUT_DEMUXER)               += nutdec.o nut.o riff.o
161 161
 OBJS-$(CONFIG_NUT_MUXER)                 += nutenc.o nut.o riff.o
162 162
 OBJS-$(CONFIG_NUV_DEMUXER)               += nuv.o riff.o
163 163
 OBJS-$(CONFIG_OGG_DEMUXER)               += oggdec.o         \
164
+                                            oggparsecelt.o   \
164 165
                                             oggparsedirac.o  \
165 166
                                             oggparseflac.o   \
166 167
                                             oggparseogm.o    \
... ...
@@ -45,6 +45,7 @@ static const struct ogg_codec * const ogg_codecs[] = {
45 45
     &ff_vorbis_codec,
46 46
     &ff_theora_codec,
47 47
     &ff_flac_codec,
48
+    &ff_celt_codec,
48 49
     &ff_old_dirac_codec,
49 50
     &ff_old_flac_codec,
50 51
     &ff_ogm_video_codec,
... ...
@@ -98,6 +98,7 @@ struct ogg {
98 98
 #define OGG_FLAG_BOS  2
99 99
 #define OGG_FLAG_EOS  4
100 100
 
101
+extern const struct ogg_codec ff_celt_codec;
101 102
 extern const struct ogg_codec ff_dirac_codec;
102 103
 extern const struct ogg_codec ff_flac_codec;
103 104
 extern const struct ogg_codec ff_ogm_audio_codec;
104 105
new file mode 100644
... ...
@@ -0,0 +1,96 @@
0
+/*
1
+ * Xiph CELT / Opus parser for Ogg
2
+ * Copyright (c) 2011 Nicolas George
3
+ *
4
+ * This file is part of Libav.
5
+ *
6
+ * Libav is free software; you can redistribute it and/or
7
+ * modify it under the terms of the GNU Lesser General Public
8
+ * License as published by the Free Software Foundation; either
9
+ * version 2.1 of the License, or (at your option) any later version.
10
+ *
11
+ * Libav is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
+ * Lesser General Public License for more details.
15
+ *
16
+ * You should have received a copy of the GNU Lesser General Public
17
+ * License along with Libav; if not, write to the Free Software
18
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
+ */
20
+
21
+#include <string.h>
22
+
23
+#include "libavutil/intreadwrite.h"
24
+#include "avformat.h"
25
+#include "oggdec.h"
26
+
27
+struct oggcelt_private {
28
+    int extra_headers_left;
29
+};
30
+
31
+static int celt_header(AVFormatContext *s, int idx)
32
+{
33
+    struct ogg *ogg = s->priv_data;
34
+    struct ogg_stream *os = ogg->streams + idx;
35
+    AVStream *st = s->streams[idx];
36
+    struct oggcelt_private *priv = os->private;
37
+    uint8_t *p = os->buf + os->pstart;
38
+
39
+    if (os->psize == 60 &&
40
+        !memcmp(p, ff_celt_codec.magic, ff_celt_codec.magicsize)) {
41
+        /* Main header */
42
+
43
+        uint32_t version, sample_rate, nb_channels, frame_size;
44
+        uint32_t overlap, extra_headers;
45
+        uint8_t *extradata;
46
+
47
+        extradata = av_malloc(2 * sizeof(uint32_t) +
48
+                              FF_INPUT_BUFFER_PADDING_SIZE);
49
+        priv = av_malloc(sizeof(struct oggcelt_private));
50
+        if (!extradata || !priv) {
51
+            av_free(extradata);
52
+            av_free(priv);
53
+            return AVERROR(ENOMEM);
54
+        }
55
+        version          = AV_RL32(p + 28);
56
+        /* unused header size field skipped */
57
+        sample_rate      = AV_RL32(p + 36);
58
+        nb_channels      = AV_RL32(p + 40);
59
+        frame_size       = AV_RL32(p + 44);
60
+        overlap          = AV_RL32(p + 48);
61
+        /* unused bytes per packet field skipped */
62
+        extra_headers    = AV_RL32(p + 56);
63
+        av_free(os->private);
64
+        av_free(st->codec->extradata);
65
+        st->codec->codec_type     = AVMEDIA_TYPE_AUDIO;
66
+        st->codec->codec_id       = CODEC_ID_CELT;
67
+        st->codec->sample_rate    = sample_rate;
68
+        st->codec->channels       = nb_channels;
69
+        st->codec->frame_size     = frame_size;
70
+        st->codec->sample_fmt     = AV_SAMPLE_FMT_S16;
71
+        st->codec->extradata      = extradata;
72
+        st->codec->extradata_size = 2 * sizeof(uint32_t);
73
+        if (sample_rate)
74
+            av_set_pts_info(st, 64, 1, sample_rate);
75
+        priv->extra_headers_left  = 1 + extra_headers;
76
+        os->private = priv;
77
+        AV_WL32(extradata + 0, overlap);
78
+        AV_WL32(extradata + 4, version);
79
+        return 1;
80
+    } else if (priv && priv->extra_headers_left) {
81
+        /* Extra headers (vorbiscomment) */
82
+
83
+        ff_vorbis_comment(s, &st->metadata, p, os->psize);
84
+        priv->extra_headers_left--;
85
+        return 1;
86
+    } else {
87
+        return 0;
88
+    }
89
+}
90
+
91
+const struct ogg_codec ff_celt_codec = {
92
+    .magic     = "CELT    ",
93
+    .magicsize = 8,
94
+    .header    = celt_header,
95
+};
... ...
@@ -2066,7 +2066,8 @@ static int has_codec_parameters(AVCodecContext *avctx)
2066 2066
              avctx->codec_id == CODEC_ID_MP1 ||
2067 2067
              avctx->codec_id == CODEC_ID_MP2 ||
2068 2068
              avctx->codec_id == CODEC_ID_MP3 ||
2069
-             avctx->codec_id == CODEC_ID_SPEEX))
2069
+             avctx->codec_id == CODEC_ID_SPEEX ||
2070
+             avctx->codec_id == CODEC_ID_CELT))
2070 2071
             return 0;
2071 2072
         break;
2072 2073
     case AVMEDIA_TYPE_VIDEO: