qcp.c 6.32 KB
Newer Older
Kenan Gillet's avatar
Kenan Gillet committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*
 * 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
 */

/**
23
 * @file
Kenan Gillet's avatar
Kenan Gillet committed
24 25
 * QCP format (.qcp) demuxer
 * @author Kenan Gillet
26
 * @see RFC 3625: "The QCP File Format and Media Types for Speech Data"
Kenan Gillet's avatar
Kenan Gillet committed
27 28 29
 *     http://tools.ietf.org/html/rfc3625
 */

Justin Ruggles's avatar
Justin Ruggles committed
30
#include "libavutil/channel_layout.h"
Kenan Gillet's avatar
Kenan Gillet committed
31 32
#include "libavutil/intreadwrite.h"
#include "avformat.h"
33
#include "riff.h"
Kenan Gillet's avatar
Kenan Gillet committed
34

35
typedef struct QCPContext {
Kenan Gillet's avatar
Kenan Gillet committed
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
    uint32_t data_size;                     ///< size of data chunk

#define QCP_MAX_MODE 4
    int16_t rates_per_mode[QCP_MAX_MODE+1]; ///< contains the packet size corresponding
                                            ///< to each mode, -1 if no size.
} QCPContext;

/**
 * Last 15 out of 16 bytes of QCELP-13K GUID, as stored in the file;
 * the first byte of the GUID can be either 0x41 or 0x42.
 */
static const uint8_t guid_qcelp_13k_part[15] = {
    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
};

60 61 62 63 64
static const uint8_t guid_4gv[16] = {
    0xca, 0x29, 0xfd, 0x3c, 0x53, 0xf6, 0xf5, 0x4e,
    0x90, 0xe9, 0xf4, 0x23, 0x6d, 0x59, 0x9b, 0x61
};

Kenan Gillet's avatar
Kenan Gillet committed
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
/**
 * 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_guid(const uint8_t *guid) {
    return (guid[0] == 0x41 || guid[0] == 0x42)
        && !memcmp(guid+1, guid_qcelp_13k_part, sizeof(guid_qcelp_13k_part));
}

static int qcp_probe(AVProbeData *pd)
{
    if (AV_RL32(pd->buf  ) == AV_RL32("RIFF") &&
        AV_RL64(pd->buf+8) == AV_RL64("QLCMfmt "))
        return AVPROBE_SCORE_MAX;
    return 0;
}

90
static int qcp_read_header(AVFormatContext *s)
Kenan Gillet's avatar
Kenan Gillet committed
91
{
92
    AVIOContext *pb = s->pb;
Kenan Gillet's avatar
Kenan Gillet committed
93
    QCPContext    *c  = s->priv_data;
94
    AVStream      *st = avformat_new_stream(s, NULL);
Kenan Gillet's avatar
Kenan Gillet committed
95 96 97 98 99 100
    uint8_t       buf[16];
    int           i, nb_rates;

    if (!st)
        return AVERROR(ENOMEM);

101
    avio_rb32(pb);                    // "RIFF"
102
    avio_skip(pb, 4 + 8 + 4 + 1 + 1);    // filesize + "QLCMfmt " + chunk-size + major-version + minor-version
Kenan Gillet's avatar
Kenan Gillet committed
103

104 105 106
    st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
    st->codecpar->channels   = 1;
    st->codecpar->channel_layout = AV_CH_LAYOUT_MONO;
107
    avio_read(pb, buf, 16);
Kenan Gillet's avatar
Kenan Gillet committed
108
    if (is_qcelp_13k_guid(buf)) {
109
        st->codecpar->codec_id = AV_CODEC_ID_QCELP;
Kenan Gillet's avatar
Kenan Gillet committed
110
    } else if (!memcmp(buf, guid_evrc, 16)) {
111
        st->codecpar->codec_id = AV_CODEC_ID_EVRC;
Kenan Gillet's avatar
Kenan Gillet committed
112
    } else if (!memcmp(buf, guid_smv, 16)) {
113
        st->codecpar->codec_id = AV_CODEC_ID_SMV;
114
    } else if (!memcmp(buf, guid_4gv, 16)) {
115
        st->codecpar->codec_id = AV_CODEC_ID_4GV;
Kenan Gillet's avatar
Kenan Gillet committed
116
    } else {
117 118
        av_log(s, AV_LOG_ERROR, "Unknown codec GUID "FF_PRI_GUID".\n",
               FF_ARG_GUID(buf));
Kenan Gillet's avatar
Kenan Gillet committed
119 120
        return AVERROR_INVALIDDATA;
    }
121
    avio_skip(pb, 2 + 80); // codec-version + codec-name
122
    st->codecpar->bit_rate = avio_rl16(pb);
Kenan Gillet's avatar
Kenan Gillet committed
123

124
    s->packet_size = avio_rl16(pb);
125
    avio_skip(pb, 2); // block-size
126
    st->codecpar->sample_rate = avio_rl16(pb);
127
    avio_skip(pb, 2); // sample-size
Kenan Gillet's avatar
Kenan Gillet committed
128 129

    memset(c->rates_per_mode, -1, sizeof(c->rates_per_mode));
130
    nb_rates = avio_rl32(pb);
Kenan Gillet's avatar
Kenan Gillet committed
131 132
    nb_rates = FFMIN(nb_rates, 8);
    for (i=0; i<nb_rates; i++) {
133 134
        int size = avio_r8(pb);
        int mode = avio_r8(pb);
Kenan Gillet's avatar
Kenan Gillet committed
135 136 137 138 139
        if (mode > QCP_MAX_MODE) {
            av_log(s, AV_LOG_WARNING, "Unknown entry %d=>%d in rate-map-table.\n ", mode, size);
        } else
            c->rates_per_mode[mode] = size;
    }
140
    avio_skip(pb, 16 - 2*nb_rates + 20); // empty entries of rate-map-table + reserved
Kenan Gillet's avatar
Kenan Gillet committed
141 142 143 144 145 146

    return 0;
}

static int qcp_read_packet(AVFormatContext *s, AVPacket *pkt)
{
147
    AVIOContext *pb = s->pb;
Kenan Gillet's avatar
Kenan Gillet committed
148 149 150
    QCPContext    *c  = s->priv_data;
    unsigned int  chunk_size, tag;

151
    while(!avio_feof(pb)) {
Kenan Gillet's avatar
Kenan Gillet committed
152
        if (c->data_size) {
153
            int pkt_size, ret, mode = avio_r8(pb);
Kenan Gillet's avatar
Kenan Gillet committed
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175

            if (s->packet_size) {
                pkt_size = s->packet_size - 1;
            } else if (mode > QCP_MAX_MODE || (pkt_size = c->rates_per_mode[mode]) < 0) {
                c->data_size--;
                continue;
            }

            if (c->data_size <= pkt_size) {
                av_log(s, AV_LOG_WARNING, "Data chunk is too small.\n");
                pkt_size = c->data_size - 1;
            }

            if ((ret = av_get_packet(pb, pkt, pkt_size)) >= 0) {
                if (pkt_size != ret)
                    av_log(s, AV_LOG_ERROR, "Packet size is too small.\n");

                c->data_size -= pkt_size + 1;
            }
            return ret;
        }

176
        if (avio_tell(pb) & 1 && avio_r8(pb))
Kenan Gillet's avatar
Kenan Gillet committed
177 178
            av_log(s, AV_LOG_WARNING, "Padding should be 0.\n");

179 180
        tag        = avio_rl32(pb);
        chunk_size = avio_rl32(pb);
Kenan Gillet's avatar
Kenan Gillet committed
181 182
        switch (tag) {
        case MKTAG('v', 'r', 'a', 't'):
183
            if (avio_rl32(pb)) // var-rate-flag
Kenan Gillet's avatar
Kenan Gillet committed
184
                s->packet_size = 0;
185
            avio_skip(pb, 4); // size-in-packets
Kenan Gillet's avatar
Kenan Gillet committed
186 187 188 189 190 191
            break;
        case MKTAG('d', 'a', 't', 'a'):
            c->data_size = chunk_size;
            break;

        default:
192
            avio_skip(pb, chunk_size);
Kenan Gillet's avatar
Kenan Gillet committed
193 194 195 196 197
        }
    }
    return AVERROR_EOF;
}

198
AVInputFormat ff_qcp_demuxer = {
Kenan Gillet's avatar
Kenan Gillet committed
199
    .name           = "qcp",
200
    .long_name      = NULL_IF_CONFIG_SMALL("QCP"),
Kenan Gillet's avatar
Kenan Gillet committed
201 202 203 204 205
    .priv_data_size = sizeof(QCPContext),
    .read_probe     = qcp_probe,
    .read_header    = qcp_read_header,
    .read_packet    = qcp_read_packet,
};