From cdce0fb84518cb3b906dceb3fd3f8610407fbdd4 Mon Sep 17 00:00:00 2001 From: Kenan Gillet Date: Wed, 20 May 2009 17:19:39 +0000 Subject: [PATCH] Add QCP demuxer. Originally committed as revision 18883 to svn://svn.ffmpeg.org/ffmpeg/trunk --- Changelog | 1 + doc/general.texi | 1 + libavformat/Makefile | 1 + libavformat/allformats.c | 1 + libavformat/avformat.h | 2 +- libavformat/qcp.c | 197 +++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 202 insertions(+), 1 deletion(-) create mode 100644 libavformat/qcp.c diff --git a/Changelog b/Changelog index a602ad9..c00e1e7 100644 --- a/Changelog +++ b/Changelog @@ -19,6 +19,7 @@ version : - per-stream language-tags extraction in asfdec - V210 decoder and encoder - remaining GPL parts in AC-3 decoder converted to LGPL +- QCP demuxer diff --git a/doc/general.texi b/doc/general.texi index 05e361b..292b8af 100644 --- a/doc/general.texi +++ b/doc/general.texi @@ -139,6 +139,7 @@ library: @item Ogg @tab X @tab X @item TechnoTrend PVA @tab @tab X @tab Used by TechnoTrend DVB PCI boards. +@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 diff --git a/libavformat/Makefile b/libavformat/Makefile index c0be725..0e35318 100644 --- a/libavformat/Makefile +++ b/libavformat/Makefile @@ -177,6 +177,7 @@ OBJS-$(CONFIG_PCM_U8_DEMUXER) += raw.o 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 diff --git a/libavformat/allformats.c b/libavformat/allformats.c index 626b66c..822682b 100644 --- a/libavformat/allformats.c +++ b/libavformat/allformats.c @@ -157,6 +157,7 @@ void av_register_all(void) 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); diff --git a/libavformat/avformat.h b/libavformat/avformat.h index 8732b56..96b10cc 100644 --- a/libavformat/avformat.h +++ b/libavformat/avformat.h @@ -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, \ diff --git a/libavformat/qcp.c b/libavformat/qcp.c new file mode 100644 index 0000000..2aadb2d --- /dev/null +++ b/libavformat/qcp.c @@ -0,0 +1,197 @@ +/* + * 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 + +#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 +}; + +/** + * 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; +} + +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); + uint8_t buf[16]; + int i, nb_rates; + + 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_guid(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); // codec-version + codec-name + st->codec->bit_rate = get_le16(pb); + + s->packet_size = get_le16(pb); + url_fskip(pb, 2); // block-size + st->codec->sample_rate = get_le16(pb); + url_fskip(pb, 2); // sample-size + + memset(c->rates_per_mode, -1, sizeof(c->rates_per_mode)); + nb_rates = get_le32(pb); + nb_rates = FFMIN(nb_rates, 8); + for (i=0; i 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; + } + url_fskip(pb, 16 - 2*nb_rates + 20); // empty entries of rate-map-table + reserved + + return 0; +} + +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 pkt_size, ret, mode = get_byte(pb); + + 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; + } + + if (url_ftell(pb) & 1 && get_byte(pb)) + av_log(s, AV_LOG_WARNING, "Padding should be 0.\n"); + + tag = get_le32(pb); + chunk_size = get_le32(pb); + switch (tag) { + case MKTAG('v', 'r', 'a', 't'): + if (get_le32(pb)) // var-rate-flag + s->packet_size = 0; + url_fskip(pb, 4); // size-in-packets + break; + case MKTAG('d', 'a', 't', 'a'): + c->data_size = chunk_size; + break; + + default: + url_fskip(pb, chunk_size); + } + } + 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, +}; -- 2.7.4