00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00030 #include "libavutil/channel_layout.h"
00031 #include "libavutil/intreadwrite.h"
00032 #include "avformat.h"
00033
00034 typedef struct {
00035 uint32_t data_size;
00036
00037 #define QCP_MAX_MODE 4
00038 int16_t rates_per_mode[QCP_MAX_MODE+1];
00039
00040 } QCPContext;
00041
00046 static const uint8_t guid_qcelp_13k_part[15] = {
00047 0x6d, 0x7f, 0x5e, 0x15, 0xb1, 0xd0, 0x11, 0xba,
00048 0x91, 0x00, 0x80, 0x5f, 0xb4, 0xb9, 0x7e
00049 };
00050
00054 static const uint8_t guid_evrc[16] = {
00055 0x8d, 0xd4, 0x89, 0xe6, 0x76, 0x90, 0xb5, 0x46,
00056 0x91, 0xef, 0x73, 0x6a, 0x51, 0x00, 0xce, 0xb4
00057 };
00058
00062 static const uint8_t guid_smv[16] = {
00063 0x75, 0x2b, 0x7c, 0x8d, 0x97, 0xa7, 0x49, 0xed,
00064 0x98, 0x5e, 0xd5, 0x3c, 0x8c, 0xc7, 0x5f, 0x84
00065 };
00066
00071 static int is_qcelp_13k_guid(const uint8_t *guid) {
00072 return (guid[0] == 0x41 || guid[0] == 0x42)
00073 && !memcmp(guid+1, guid_qcelp_13k_part, sizeof(guid_qcelp_13k_part));
00074 }
00075
00076 static int qcp_probe(AVProbeData *pd)
00077 {
00078 if (AV_RL32(pd->buf ) == AV_RL32("RIFF") &&
00079 AV_RL64(pd->buf+8) == AV_RL64("QLCMfmt "))
00080 return AVPROBE_SCORE_MAX;
00081 return 0;
00082 }
00083
00084 static int qcp_read_header(AVFormatContext *s)
00085 {
00086 AVIOContext *pb = s->pb;
00087 QCPContext *c = s->priv_data;
00088 AVStream *st = avformat_new_stream(s, NULL);
00089 uint8_t buf[16];
00090 int i, nb_rates;
00091
00092 if (!st)
00093 return AVERROR(ENOMEM);
00094
00095 avio_rb32(pb);
00096 avio_skip(pb, 4 + 8 + 4 + 1 + 1);
00097
00098 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
00099 st->codec->channels = 1;
00100 st->codec->channel_layout = AV_CH_LAYOUT_MONO;
00101 avio_read(pb, buf, 16);
00102 if (is_qcelp_13k_guid(buf)) {
00103 st->codec->codec_id = AV_CODEC_ID_QCELP;
00104 } else if (!memcmp(buf, guid_evrc, 16)) {
00105 av_log(s, AV_LOG_ERROR, "EVRC codec is not supported.\n");
00106 return AVERROR_PATCHWELCOME;
00107 } else if (!memcmp(buf, guid_smv, 16)) {
00108 av_log(s, AV_LOG_ERROR, "SMV codec is not supported.\n");
00109 return AVERROR_PATCHWELCOME;
00110 } else {
00111 av_log(s, AV_LOG_ERROR, "Unknown codec GUID.\n");
00112 return AVERROR_INVALIDDATA;
00113 }
00114 avio_skip(pb, 2 + 80);
00115 st->codec->bit_rate = avio_rl16(pb);
00116
00117 s->packet_size = avio_rl16(pb);
00118 avio_skip(pb, 2);
00119 st->codec->sample_rate = avio_rl16(pb);
00120 avio_skip(pb, 2);
00121
00122 memset(c->rates_per_mode, -1, sizeof(c->rates_per_mode));
00123 nb_rates = avio_rl32(pb);
00124 nb_rates = FFMIN(nb_rates, 8);
00125 for (i=0; i<nb_rates; i++) {
00126 int size = avio_r8(pb);
00127 int mode = avio_r8(pb);
00128 if (mode > QCP_MAX_MODE) {
00129 av_log(s, AV_LOG_WARNING, "Unknown entry %d=>%d in rate-map-table.\n ", mode, size);
00130 } else
00131 c->rates_per_mode[mode] = size;
00132 }
00133 avio_skip(pb, 16 - 2*nb_rates + 20);
00134
00135 return 0;
00136 }
00137
00138 static int qcp_read_packet(AVFormatContext *s, AVPacket *pkt)
00139 {
00140 AVIOContext *pb = s->pb;
00141 QCPContext *c = s->priv_data;
00142 unsigned int chunk_size, tag;
00143
00144 while(!url_feof(pb)) {
00145 if (c->data_size) {
00146 int pkt_size, ret, mode = avio_r8(pb);
00147
00148 if (s->packet_size) {
00149 pkt_size = s->packet_size - 1;
00150 } else if (mode > QCP_MAX_MODE || (pkt_size = c->rates_per_mode[mode]) < 0) {
00151 c->data_size--;
00152 continue;
00153 }
00154
00155 if (c->data_size <= pkt_size) {
00156 av_log(s, AV_LOG_WARNING, "Data chunk is too small.\n");
00157 pkt_size = c->data_size - 1;
00158 }
00159
00160 if ((ret = av_get_packet(pb, pkt, pkt_size)) >= 0) {
00161 if (pkt_size != ret)
00162 av_log(s, AV_LOG_ERROR, "Packet size is too small.\n");
00163
00164 c->data_size -= pkt_size + 1;
00165 }
00166 return ret;
00167 }
00168
00169 if (avio_tell(pb) & 1 && avio_r8(pb))
00170 av_log(s, AV_LOG_WARNING, "Padding should be 0.\n");
00171
00172 tag = avio_rl32(pb);
00173 chunk_size = avio_rl32(pb);
00174 switch (tag) {
00175 case MKTAG('v', 'r', 'a', 't'):
00176 if (avio_rl32(pb))
00177 s->packet_size = 0;
00178 avio_skip(pb, 4);
00179 break;
00180 case MKTAG('d', 'a', 't', 'a'):
00181 c->data_size = chunk_size;
00182 break;
00183
00184 default:
00185 avio_skip(pb, chunk_size);
00186 }
00187 }
00188 return AVERROR_EOF;
00189 }
00190
00191 AVInputFormat ff_qcp_demuxer = {
00192 .name = "qcp",
00193 .long_name = NULL_IF_CONFIG_SMALL("QCP"),
00194 .priv_data_size = sizeof(QCPContext),
00195 .read_probe = qcp_probe,
00196 .read_header = qcp_read_header,
00197 .read_packet = qcp_read_packet,
00198 };