[FFmpeg-devel] [PATCH][RFC] QCP demuxer

Kenan Gillet kenan.gillet
Thu May 14 00:11:57 CEST 2009


Hi,

this demuxer is based on the RFC 3625 and works for all the samples at
http://samples.mplayerhq.hu/A-codecs/qcp/

All those samples constains QCELP data, and I could not find any samples
for either SMV or EVRC data.

Currently, the demuxer skips:
- text chunk which contains any information defined by the application
- offs chunk which contains offsets of packets for seekin purpose.


Thanks for the reviews,

Kenan
-------------- next part --------------
Index: Changelog
===================================================================
--- Changelog	(revision 18816)
+++ Changelog	(working copy)
@@ -18,6 +18,7 @@
 - 8088flex TMV demuxer and decoder
 - per-stream language-tags extraction in asfdec
 - V210 decoder and encoder
+- QCP demuxer
 
 
 version 0.5:
Index: doc/general.texi
===================================================================
--- doc/general.texi	(revision 18816)
+++ doc/general.texi	(working copy)
@@ -139,6 +139,7 @@
 @item Ogg                       @tab X @tab X
 @item TechnoTrend PVA           @tab   @tab X
     @tab Used by TechnoTrend DVB PCI boards.
+ at item QCP                       @tab   @tab X
 @item raw ADTS (AAC)            @tab X @tab X
 @item raw AC-3                  @tab X @tab X
 @item raw Chinese AVS video     @tab   @tab X
Index: libavformat/Makefile
===================================================================
--- libavformat/Makefile	(revision 18816)
+++ libavformat/Makefile	(working copy)
@@ -177,6 +177,7 @@
 OBJS-$(CONFIG_PCM_U8_MUXER)              += raw.o
 OBJS-$(CONFIG_PSP_MUXER)                 += movenc.o riff.o isom.o avc.o
 OBJS-$(CONFIG_PVA_DEMUXER)               += pva.o
+OBJS-$(CONFIG_QCP_DEMUXER)               += qcp.o
 OBJS-$(CONFIG_R3D_DEMUXER)               += r3d.o
 OBJS-$(CONFIG_RAWVIDEO_DEMUXER)          += raw.o
 OBJS-$(CONFIG_RAWVIDEO_MUXER)            += raw.o
Index: libavformat/avformat.h
===================================================================
--- libavformat/avformat.h	(revision 18816)
+++ libavformat/avformat.h	(working copy)
@@ -22,7 +22,7 @@
 #define AVFORMAT_AVFORMAT_H
 
 #define LIBAVFORMAT_VERSION_MAJOR 52
-#define LIBAVFORMAT_VERSION_MINOR 32
+#define LIBAVFORMAT_VERSION_MINOR 33
 #define LIBAVFORMAT_VERSION_MICRO  0
 
 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \
Index: libavformat/allformats.c
===================================================================
--- libavformat/allformats.c	(revision 18816)
+++ libavformat/allformats.c	(working copy)
@@ -157,6 +158,7 @@
     REGISTER_MUXDEMUX (PCM_U8,    pcm_u8);
     REGISTER_MUXER    (PSP, psp);
     REGISTER_DEMUXER  (PVA, pva);
+    REGISTER_DEMUXER  (QCP, qcp);
     REGISTER_DEMUXER  (R3D, r3d);
     REGISTER_MUXDEMUX (RAWVIDEO, rawvideo);
     REGISTER_DEMUXER  (REDIR, redir);
Index: libavformat/qcp.c
===================================================================
--- libavformat/qcp.c	(revision 0)
+++ libavformat/qcp.c	(revision 0)
@@ -0,0 +1,243 @@
+/*
+ * QCP format (.qcp) demuxer
+ * Copyright (c) 2009 Kenan Gillet
+ *
+ * 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
+ */
+
+/**
+ * @file libavformat/qcp.c
+ * QCP format (.qcp) demuxer
+ * @author Kenan Gillet
+ * @sa RFC 3625: "The QCP File Format and Media Types for Speech Data"
+ *     http://tools.ietf.org/html/rfc3625
+ */
+
+#include "libavutil/intreadwrite.h"
+#include "avformat.h"
+
+typedef struct {
+    uint32_t data_size;    ///< size of data chunk
+    uint8_t  data_has_pad; ///< 1 if the data chunk ends with a zero padding, 0 otherwise
+
+    uint16_t config;       ///< a bitmapped configuration word, for application use
+
+    uint8_t label[48];     ///< in label chunk, generic storage for application use
+
+    int fixed_packet_size; ///< packet size if fixed for all packets, 0 otherwise
+
+    uint8_t nb_rates;
+    struct {
+        uint8_t size;      ///< size of the packet
+        uint8_t mode;      ///< byte mode, 1st byte of each packet
+    } rates_mapping[8];
+} QCPContext;
+
+/**
+ * QCELP-13K GUID as stored in the file
+ */
+static const uint8_t guid_qcelp_13k[16] = {
+    0x41, 0x6d, 0x7f, 0x5e, 0x15, 0xb1, 0xd0, 0x11,
+    0xba, 0x91, 0x00, 0x80, 0x5f, 0xb4, 0xb9, 0x7e
+};
+
+/**
+ * EVRC GUID as stored in the file
+ */
+static const uint8_t guid_evrc[16] = {
+    0x8d, 0xd4, 0x89, 0xe6, 0x76, 0x90, 0xb5, 0x46,
+    0x91, 0xef, 0x73, 0x6a, 0x51, 0x00, 0xce, 0xb4
+};
+
+/**
+ * SMV GUID as stored in the file
+ */
+static const uint8_t guid_smv[16] = {
+    0x75, 0x2b, 0x7c, 0x8d, 0x97, 0xa7, 0x49, 0xed,
+    0x98, 0x5e, 0xd5, 0x3c, 0x8c, 0xc7, 0x5f, 0x84
+};
+
+/**
+ * @param guid contains at least 16 bytes
+ * @return 1 if the guid is a qcelp_13k guid, 0 otherwise
+ */
+static int is_qcelp_13k_quid(const uint8_t *guid) {
+    return !memcmp(guid,     guid_qcelp_13k,   16) ||
+          (!memcmp(guid + 1, guid_qcelp_13k+1, 15) && guid[0] == 0x42);
+}
+
+static int qcp_probe(AVProbeData *pd)
+{
+    if (AV_RL32(pd->buf   ) == MKTAG('R', 'I', 'F', 'F') &&
+        AV_RL32(pd->buf+ 8) == MKTAG('Q', 'L', 'C', 'M') &&
+        AV_RL32(pd->buf+12) == MKTAG('f', 'm', 't', ' '))
+        return AVPROBE_SCORE_MAX;
+    return 0;
+}
+
+static int qcp_read_header(AVFormatContext *s, AVFormatParameters *ap)
+{
+    ByteIOContext *pb = s->pb;
+    QCPContext    *c  = s->priv_data;
+    AVStream      *st = av_new_stream(s, 0);
+    unsigned char buf[16];
+    int i;
+
+    if (!st)
+        return AVERROR(ENOMEM);
+
+    get_be32(pb);                    // "RIFF"
+    s->file_size = get_le32(pb) + 8;
+    url_fskip(pb, 8 + 4 + 1 + 1);    // "QLCMfmt " + chunk-size + major-version + minor-version
+
+    st->codec->codec_type = CODEC_TYPE_AUDIO;
+    st->codec->channels   = 1;
+    get_buffer(pb, buf, 16);
+    if (is_qcelp_13k_quid(buf)) {
+        st->codec->codec_id = CODEC_ID_QCELP;
+    } else if (!memcmp(buf, guid_evrc, 16)) {
+        av_log(s, AV_LOG_ERROR, "EVRC codec is not supported.\n");
+        return AVERROR_PATCHWELCOME;
+    } else if (!memcmp(buf, guid_smv, 16)) {
+        av_log(s, AV_LOG_ERROR, "SMV codec is not supported.\n");
+        return AVERROR_PATCHWELCOME;
+    } else {
+        av_log(s, AV_LOG_ERROR, "Unknown codec GUID.\n");
+        return AVERROR_INVALIDDATA;
+    }
+    url_fskip(pb, 2 + 80 + 2); // codec-version + codec-name + average-bps
+
+    s->packet_size = get_le16(pb);
+    get_le16(pb); // block-size
+    st->codec->sample_rate = get_le16(pb);
+    get_le16(pb); // sample-size
+
+    c->nb_rates = get_le32(pb);
+    for (i=0; i<8; i++) {
+        c->rates_mapping[i].size = get_byte(pb);
+        c->rates_mapping[i].mode = get_byte(pb);
+    }
+
+    url_fskip(pb, 20 + 4 + 4); // reserved + "vrat" + chunk-size
+    if (!get_le32(pb))         // var-rate-flag
+        c->fixed_packet_size = s->packet_size;
+    get_le32(pb);              // size-in-packets
+
+    return 0;
+}
+
+/**
+ * @param c the demuxer context
+ * @param byte_mode the first byte of a packet in data chunk
+ *
+ * @return size of the packet, -1 on error
+ */
+static int get_packet_size(QCPContext *c, const int byte_mode)
+{
+    int i;
+
+    if (c->fixed_packet_size)
+        return c->fixed_packet_size;
+
+    for (i=0; i<c->nb_rates; i++)
+        if (c->rates_mapping[i].mode == byte_mode)
+            return c->rates_mapping[i].size + 1;
+
+    return -1;
+}
+
+static int qcp_read_packet(AVFormatContext *s, AVPacket *pkt)
+{
+    ByteIOContext *pb = s->pb;
+    QCPContext    *c  = s->priv_data;
+    unsigned int chunk_size, tag;
+
+    while(!url_feof(pb)) {
+        if (c->data_size) {
+            int buf_size, mode = get_byte(pb);
+
+            if ((buf_size = get_packet_size(c, mode)) < 0) {
+                av_log(s, AV_LOG_ERROR, "wrong byte mode=%d@%llx\n", mode, url_ftell(pb));
+                return AVERROR_INVALIDDATA;
+            }
+            if (c->data_size >= buf_size) {
+                int ret = av_new_packet(pkt, buf_size);
+
+                if (ret < 0)
+                    return ret;
+
+                pkt->pos = url_ftell(pb);
+
+                ret = get_buffer(pb, pkt->data+1, buf_size-1);
+                if (ret <= 0 || buf_size != ret + 1) {
+                    av_free_packet(pkt);
+                    av_log(s, AV_LOG_ERROR, "Packet size is too small.\n");
+                    return ret <= 0 ? ret : AVERROR(EIO);
+                }
+
+                c->data_size -= buf_size;
+                pkt->data[0] = mode;
+                av_shrink_packet(pkt, buf_size);
+                if (!c->data_size && c->data_has_pad)
+                    get_byte(pb);
+                return buf_size;
+            } else {
+                av_log(s, AV_LOG_ERROR, "Data chunk is too small.\n");
+                return AVERROR(EIO);
+            }
+        }
+
+        tag        = get_le32(pb);
+        chunk_size = get_le32(pb);
+        switch (tag) {
+        case MKTAG('d', 'a', 't', 'a'):
+            c->data_size    = chunk_size;
+            c->data_has_pad = chunk_size & 1;
+            break;
+        case MKTAG('c', 'n', 'f', 'g'):
+            c->config = get_le16(pb);
+            break;
+        case MKTAG('o', 'f', 'f', 's'):
+            url_fskip(pb, chunk_size);
+            break;
+        case MKTAG('t', 'e', 'x', 't'):
+            url_fskip(pb, chunk_size);
+            if (chunk_size & 1)
+                get_byte(pb); // 0 padding
+            break;
+        case MKTAG('l', 'a', 'b', 'l'):
+            get_buffer(pb, c->label, 48);
+            break;
+        case 0:
+            if (url_feof(pb))
+                break;
+        default:
+            av_log(s, AV_LOG_ERROR, "No correct chunk tag could be found.\n");
+            return AVERROR_INVALIDDATA;
+        }
+    }
+    return AVERROR_EOF;
+}
+
+AVInputFormat qcp_demuxer = {
+    .name           = "qcp",
+    .long_name      = NULL_IF_CONFIG_SMALL("QCP format"),
+    .priv_data_size = sizeof(QCPContext),
+    .read_probe     = qcp_probe,
+    .read_header    = qcp_read_header,
+    .read_packet    = qcp_read_packet,
+};



More information about the ffmpeg-devel mailing list