[FFmpeg-devel] [PATCH] add tag/comment support to the raw flac demuxer

Jim Radford radford
Mon Dec 1 23:18:02 CET 2008


This patch adds support for parsing vorbis comments in plain flac
streams.  Only metadata packets are parsed leaving the frame data to
be parsed in raw 1024 byte chunks like before.

-Jim
-------------- next part --------------
Index: libavformat/raw.c
===================================================================
--- libavformat/raw.c	(revision 15974)
+++ libavformat/raw.c	(working copy)
@@ -28,6 +28,64 @@
 #include "raw.h"
 
 /* simple formats */
+
+#ifdef CONFIG_FLAC_DEMUXER
+#include "oggdec.h" // vorbis_comment()
+
+struct flac
+{
+    int flac_done, metadata_done;
+};
+
+static int flac_read_packet(AVFormatContext *s, AVPacket *pkt)
+{
+    struct flac *flac = s->priv_data;
+
+    if (!flac->flac_done) {
+        if (av_get_packet(s->pb, pkt, 4) <= 0)
+            return AVERROR(EIO);
+        flac->flac_done = 1;
+    } else if (!flac->metadata_done) {
+        GetBitContext gb;
+        unsigned char header[4];
+        int type, length, ret;
+
+        if ((ret = get_buffer(s->pb, header, sizeof(header))) <= 0)
+            return AVERROR(EIO);
+        if (ret != sizeof(header))
+            return AVERROR(EINVAL);
+
+        init_get_bits(&gb, header, ret*8);
+        flac->metadata_done = get_bits(&gb, 1);
+        type = get_bits(&gb, 7);
+        length = get_bits(&gb, 24);
+
+        if (av_new_packet(pkt, sizeof(header) + length) < 0)
+            return AVERROR(EIO);
+
+        memcpy(pkt->data, header, sizeof(header));
+        pkt->pos= url_ftell(s->pb) - sizeof(header);
+
+        ret = get_buffer(s->pb, pkt->data + sizeof(header), length);
+        if (ret < 0)
+            return AVERROR(EIO);
+        if (ret != length)
+            return AVERROR(EINVAL);
+
+        pkt->size = ret += sizeof(header);
+
+        if (type == 4)
+            vorbis_comment(s, pkt->data+sizeof(header), length);
+    } else {
+        if (av_get_packet(s->pb, pkt, 1024) <= 0)
+            return AVERROR(EIO);
+    }
+
+    pkt->stream_index = 0;
+    return pkt->size;
+}
+#endif
+
 #ifdef CONFIG_FLAC_MUXER
 static int flac_write_header(struct AVFormatContext *s)
 {
@@ -741,10 +799,10 @@
 AVInputFormat flac_demuxer = {
     "flac",
     NULL_IF_CONFIG_SMALL("raw FLAC"),
-    0,
+    sizeof(struct flac),
     flac_probe,
     audio_read_header,
-    raw_read_partial_packet,
+    flac_read_packet,
     .flags= AVFMT_GENERIC_INDEX,
     .extensions = "flac",
     .value = CODEC_ID_FLAC,



More information about the ffmpeg-devel mailing list