qcp.c
Go to the documentation of this file.
1 /*
2  * QCP format (.qcp) demuxer
3  * Copyright (c) 2009 Kenan Gillet
4  *
5  * This file is part of Libav.
6  *
7  * Libav is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * Libav is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
30 #include "libavutil/intreadwrite.h"
31 #include "avformat.h"
32 
33 typedef struct {
34  uint32_t data_size;
35 
36 #define QCP_MAX_MODE 4
37  int16_t rates_per_mode[QCP_MAX_MODE+1];
38 
39 } QCPContext;
40 
45 static const uint8_t guid_qcelp_13k_part[15] = {
46  0x6d, 0x7f, 0x5e, 0x15, 0xb1, 0xd0, 0x11, 0xba,
47  0x91, 0x00, 0x80, 0x5f, 0xb4, 0xb9, 0x7e
48 };
49 
53 static const uint8_t guid_evrc[16] = {
54  0x8d, 0xd4, 0x89, 0xe6, 0x76, 0x90, 0xb5, 0x46,
55  0x91, 0xef, 0x73, 0x6a, 0x51, 0x00, 0xce, 0xb4
56 };
57 
61 static const uint8_t guid_smv[16] = {
62  0x75, 0x2b, 0x7c, 0x8d, 0x97, 0xa7, 0x49, 0xed,
63  0x98, 0x5e, 0xd5, 0x3c, 0x8c, 0xc7, 0x5f, 0x84
64 };
65 
70 static int is_qcelp_13k_guid(const uint8_t *guid) {
71  return (guid[0] == 0x41 || guid[0] == 0x42)
72  && !memcmp(guid+1, guid_qcelp_13k_part, sizeof(guid_qcelp_13k_part));
73 }
74 
75 static int qcp_probe(AVProbeData *pd)
76 {
77  if (AV_RL32(pd->buf ) == AV_RL32("RIFF") &&
78  AV_RL64(pd->buf+8) == AV_RL64("QLCMfmt "))
79  return AVPROBE_SCORE_MAX;
80  return 0;
81 }
82 
84 {
85  AVIOContext *pb = s->pb;
86  QCPContext *c = s->priv_data;
88  uint8_t buf[16];
89  int i, nb_rates;
90 
91  if (!st)
92  return AVERROR(ENOMEM);
93 
94  avio_rb32(pb); // "RIFF"
95  avio_skip(pb, 4 + 8 + 4 + 1 + 1); // filesize + "QLCMfmt " + chunk-size + major-version + minor-version
96 
98  st->codec->channels = 1;
99  avio_read(pb, buf, 16);
100  if (is_qcelp_13k_guid(buf)) {
102  } else if (!memcmp(buf, guid_evrc, 16)) {
103  av_log(s, AV_LOG_ERROR, "EVRC codec is not supported.\n");
104  return AVERROR_PATCHWELCOME;
105  } else if (!memcmp(buf, guid_smv, 16)) {
106  av_log(s, AV_LOG_ERROR, "SMV codec is not supported.\n");
107  return AVERROR_PATCHWELCOME;
108  } else {
109  av_log(s, AV_LOG_ERROR, "Unknown codec GUID.\n");
110  return AVERROR_INVALIDDATA;
111  }
112  avio_skip(pb, 2 + 80); // codec-version + codec-name
113  st->codec->bit_rate = avio_rl16(pb);
114 
115  s->packet_size = avio_rl16(pb);
116  avio_skip(pb, 2); // block-size
117  st->codec->sample_rate = avio_rl16(pb);
118  avio_skip(pb, 2); // sample-size
119 
120  memset(c->rates_per_mode, -1, sizeof(c->rates_per_mode));
121  nb_rates = avio_rl32(pb);
122  nb_rates = FFMIN(nb_rates, 8);
123  for (i=0; i<nb_rates; i++) {
124  int size = avio_r8(pb);
125  int mode = avio_r8(pb);
126  if (mode > QCP_MAX_MODE) {
127  av_log(s, AV_LOG_WARNING, "Unknown entry %d=>%d in rate-map-table.\n ", mode, size);
128  } else
129  c->rates_per_mode[mode] = size;
130  }
131  avio_skip(pb, 16 - 2*nb_rates + 20); // empty entries of rate-map-table + reserved
132 
133  return 0;
134 }
135 
137 {
138  AVIOContext *pb = s->pb;
139  QCPContext *c = s->priv_data;
140  unsigned int chunk_size, tag;
141 
142  while(!pb->eof_reached) {
143  if (c->data_size) {
144  int pkt_size, ret, mode = avio_r8(pb);
145 
146  if (s->packet_size) {
147  pkt_size = s->packet_size - 1;
148  } else if (mode > QCP_MAX_MODE || (pkt_size = c->rates_per_mode[mode]) < 0) {
149  c->data_size--;
150  continue;
151  }
152 
153  if (c->data_size <= pkt_size) {
154  av_log(s, AV_LOG_WARNING, "Data chunk is too small.\n");
155  pkt_size = c->data_size - 1;
156  }
157 
158  if ((ret = av_get_packet(pb, pkt, pkt_size)) >= 0) {
159  if (pkt_size != ret)
160  av_log(s, AV_LOG_ERROR, "Packet size is too small.\n");
161 
162  c->data_size -= pkt_size + 1;
163  }
164  return ret;
165  }
166 
167  if (avio_tell(pb) & 1 && avio_r8(pb))
168  av_log(s, AV_LOG_WARNING, "Padding should be 0.\n");
169 
170  tag = avio_rl32(pb);
171  chunk_size = avio_rl32(pb);
172  switch (tag) {
173  case MKTAG('v', 'r', 'a', 't'):
174  if (avio_rl32(pb)) // var-rate-flag
175  s->packet_size = 0;
176  avio_skip(pb, 4); // size-in-packets
177  break;
178  case MKTAG('d', 'a', 't', 'a'):
179  c->data_size = chunk_size;
180  break;
181 
182  default:
183  avio_skip(pb, chunk_size);
184  }
185  }
186  return AVERROR_EOF;
187 }
188 
190  .name = "qcp",
191  .long_name = NULL_IF_CONFIG_SMALL("QCP format"),
192  .priv_data_size = sizeof(QCPContext),
196 };