Modify the Ogg/Speex demuxer and the libspeex decoder so that they always treat
[ffmpeg.git] / libavformat / oggparsespeex.c
index eb7709c..cc00dd2 100644 (file)
@@ -25,7 +25,7 @@
 #include <stdlib.h>
 #include "libavutil/bswap.h"
 #include "libavutil/avstring.h"
 #include <stdlib.h>
 #include "libavutil/bswap.h"
 #include "libavutil/avstring.h"
-#include "libavcodec/bitstream.h"
+#include "libavcodec/get_bits.h"
 #include "libavcodec/bytestream.h"
 #include "avformat.h"
 #include "oggdec.h"
 #include "libavcodec/bytestream.h"
 #include "avformat.h"
 #include "oggdec.h"
@@ -36,22 +36,37 @@ static int speex_header(AVFormatContext *s, int idx) {
     AVStream *st = s->streams[idx];
     uint8_t *p = os->buf + os->pstart;
 
     AVStream *st = s->streams[idx];
     uint8_t *p = os->buf + os->pstart;
 
-    if (os->psize < 80)
-        return 1;
+    if (os->seq > 1)
+        return 0;
 
 
-    st->codec->codec_type = CODEC_TYPE_AUDIO;
-    st->codec->codec_id = CODEC_ID_SPEEX;
+    if (os->seq == 0) {
+        int frames_per_packet;
+        st->codec->codec_type = CODEC_TYPE_AUDIO;
+        st->codec->codec_id = CODEC_ID_SPEEX;
 
 
-    st->codec->sample_rate = AV_RL32(p + 36);
-    st->codec->channels = AV_RL32(p + 48);
-    st->codec->extradata_size = os->psize;
-    st->codec->extradata = av_malloc(st->codec->extradata_size);
-    memcpy(st->codec->extradata, p, st->codec->extradata_size);
+        st->codec->sample_rate = AV_RL32(p + 36);
+        st->codec->channels = AV_RL32(p + 48);
 
 
-    st->time_base.num = 1;
-    st->time_base.den = st->codec->sample_rate;
+        /* We treat the whole Speex packet as a single frame everywhere Speex
+           is handled in FFmpeg.  This avoids the complexities of splitting
+           and joining individual Speex frames, which are not always
+           byte-aligned. */
+        st->codec->frame_size = AV_RL32(p + 56);
+        frames_per_packet     = AV_RL32(p + 64);
+        if (frames_per_packet)
+            st->codec->frame_size *= frames_per_packet;
 
 
-    return 0;
+        st->codec->extradata_size = os->psize;
+        st->codec->extradata = av_malloc(st->codec->extradata_size
+                                         + FF_INPUT_BUFFER_PADDING_SIZE);
+        memcpy(st->codec->extradata, p, st->codec->extradata_size);
+
+        st->time_base.num = 1;
+        st->time_base.den = st->codec->sample_rate;
+    } else
+        vorbis_comment(s, p, os->psize);
+
+    return 1;
 }
 
 const struct ogg_codec ff_speex_codec = {
 }
 
 const struct ogg_codec ff_speex_codec = {