FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 FFmpeg.
6  *
7  * FFmpeg 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  * FFmpeg 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 FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 /**
23  * @file
24  * QCP format (.qcp) demuxer
25  * @author Kenan Gillet
26  * @see RFC 3625: "The QCP File Format and Media Types for Speech Data"
27  * http://tools.ietf.org/html/rfc3625
28  */
29 
31 #include "libavutil/intreadwrite.h"
32 #include "avformat.h"
33 #include "riff.h"
34 
35 typedef struct QCPContext {
36  uint32_t data_size; ///< size of data chunk
37 
38 #define QCP_MAX_MODE 4
39  int16_t rates_per_mode[QCP_MAX_MODE+1]; ///< contains the packet size corresponding
40  ///< to each mode, -1 if no size.
41 } QCPContext;
42 
43 /**
44  * Last 15 out of 16 bytes of QCELP-13K GUID, as stored in the file;
45  * the first byte of the GUID can be either 0x41 or 0x42.
46  */
47 static const uint8_t guid_qcelp_13k_part[15] = {
48  0x6d, 0x7f, 0x5e, 0x15, 0xb1, 0xd0, 0x11, 0xba,
49  0x91, 0x00, 0x80, 0x5f, 0xb4, 0xb9, 0x7e
50 };
51 
52 /**
53  * EVRC GUID as stored in the file
54  */
55 static const uint8_t guid_evrc[16] = {
56  0x8d, 0xd4, 0x89, 0xe6, 0x76, 0x90, 0xb5, 0x46,
57  0x91, 0xef, 0x73, 0x6a, 0x51, 0x00, 0xce, 0xb4
58 };
59 
60 /**
61  * SMV GUID as stored in the file
62  */
63 static const uint8_t guid_smv[16] = {
64  0x75, 0x2b, 0x7c, 0x8d, 0x97, 0xa7, 0x49, 0xed,
65  0x98, 0x5e, 0xd5, 0x3c, 0x8c, 0xc7, 0x5f, 0x84
66 };
67 
68 /**
69  * @param guid contains at least 16 bytes
70  * @return 1 if the guid is a qcelp_13k guid, 0 otherwise
71  */
72 static int is_qcelp_13k_guid(const uint8_t *guid) {
73  return (guid[0] == 0x41 || guid[0] == 0x42)
74  && !memcmp(guid+1, guid_qcelp_13k_part, sizeof(guid_qcelp_13k_part));
75 }
76 
77 static int qcp_probe(AVProbeData *pd)
78 {
79  if (AV_RL32(pd->buf ) == AV_RL32("RIFF") &&
80  AV_RL64(pd->buf+8) == AV_RL64("QLCMfmt "))
81  return AVPROBE_SCORE_MAX;
82  return 0;
83 }
84 
86 {
87  AVIOContext *pb = s->pb;
88  QCPContext *c = s->priv_data;
90  uint8_t buf[16];
91  int i, nb_rates;
92 
93  if (!st)
94  return AVERROR(ENOMEM);
95 
96  avio_rb32(pb); // "RIFF"
97  avio_skip(pb, 4 + 8 + 4 + 1 + 1); // filesize + "QLCMfmt " + chunk-size + major-version + minor-version
98 
100  st->codec->channels = 1;
102  avio_read(pb, buf, 16);
103  if (is_qcelp_13k_guid(buf)) {
105  } else if (!memcmp(buf, guid_evrc, 16)) {
107  } else if (!memcmp(buf, guid_smv, 16)) {
109  } else {
110  av_log(s, AV_LOG_ERROR, "Unknown codec GUID "FF_PRI_GUID".\n",
111  FF_ARG_GUID(buf));
112  return AVERROR_INVALIDDATA;
113  }
114  avio_skip(pb, 2 + 80); // codec-version + codec-name
115  st->codec->bit_rate = avio_rl16(pb);
116 
117  s->packet_size = avio_rl16(pb);
118  avio_skip(pb, 2); // block-size
119  st->codec->sample_rate = avio_rl16(pb);
120  avio_skip(pb, 2); // sample-size
121 
122  memset(c->rates_per_mode, -1, sizeof(c->rates_per_mode));
123  nb_rates = avio_rl32(pb);
124  nb_rates = FFMIN(nb_rates, 8);
125  for (i=0; i<nb_rates; i++) {
126  int size = avio_r8(pb);
127  int mode = avio_r8(pb);
128  if (mode > QCP_MAX_MODE) {
129  av_log(s, AV_LOG_WARNING, "Unknown entry %d=>%d in rate-map-table.\n ", mode, size);
130  } else
131  c->rates_per_mode[mode] = size;
132  }
133  avio_skip(pb, 16 - 2*nb_rates + 20); // empty entries of rate-map-table + reserved
134 
135  return 0;
136 }
137 
139 {
140  AVIOContext *pb = s->pb;
141  QCPContext *c = s->priv_data;
142  unsigned int chunk_size, tag;
143 
144  while(!avio_feof(pb)) {
145  if (c->data_size) {
146  int pkt_size, ret, mode = avio_r8(pb);
147 
148  if (s->packet_size) {
149  pkt_size = s->packet_size - 1;
150  } else if (mode > QCP_MAX_MODE || (pkt_size = c->rates_per_mode[mode]) < 0) {
151  c->data_size--;
152  continue;
153  }
154 
155  if (c->data_size <= pkt_size) {
156  av_log(s, AV_LOG_WARNING, "Data chunk is too small.\n");
157  pkt_size = c->data_size - 1;
158  }
159 
160  if ((ret = av_get_packet(pb, pkt, pkt_size)) >= 0) {
161  if (pkt_size != ret)
162  av_log(s, AV_LOG_ERROR, "Packet size is too small.\n");
163 
164  c->data_size -= pkt_size + 1;
165  }
166  return ret;
167  }
168 
169  if (avio_tell(pb) & 1 && avio_r8(pb))
170  av_log(s, AV_LOG_WARNING, "Padding should be 0.\n");
171 
172  tag = avio_rl32(pb);
173  chunk_size = avio_rl32(pb);
174  switch (tag) {
175  case MKTAG('v', 'r', 'a', 't'):
176  if (avio_rl32(pb)) // var-rate-flag
177  s->packet_size = 0;
178  avio_skip(pb, 4); // size-in-packets
179  break;
180  case MKTAG('d', 'a', 't', 'a'):
181  c->data_size = chunk_size;
182  break;
183 
184  default:
185  avio_skip(pb, chunk_size);
186  }
187  }
188  return AVERROR_EOF;
189 }
190 
192  .name = "qcp",
193  .long_name = NULL_IF_CONFIG_SMALL("QCP"),
194  .priv_data_size = sizeof(QCPContext),
198 };
unsigned int packet_size
Definition: avformat.h:1376
#define NULL
Definition: coverity.c:32
const char * s
Definition: avisynth_c.h:631
Bytestream IO Context.
Definition: avio.h:111
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
static const uint8_t guid_smv[16]
SMV GUID as stored in the file.
Definition: qcp.c:63
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
#define FF_ARG_GUID(g)
Definition: riff.h:97
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:276
uint64_t_TMPL AV_RL64
Definition: bytestream.h:85
static AVPacket pkt
Format I/O context.
Definition: avformat.h:1272
static int qcp_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: qcp.c:138
uint8_t
mode
Definition: f_perms.c:27
unsigned int avio_rb32(AVIOContext *s)
Definition: aviobuf.c:689
uint32_t data_size
size of data chunk
Definition: qcp.c:36
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:3672
uint32_t tag
Definition: movenc.c:1333
#define AVERROR_EOF
End of file.
Definition: error.h:55
int av_get_packet(AVIOContext *s, AVPacket *pkt, int size)
Allocate and read the payload of a packet and initialize its fields with default values.
Definition: utils.c:241
ptrdiff_t size
Definition: opengl_enc.c:101
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:365
#define av_log(a,...)
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:537
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:658
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:175
uint64_t channel_layout
Audio channel layout.
Definition: avcodec.h:2046
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:528
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:861
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:450
int bit_rate
the average bitrate
Definition: avcodec.h:1305
audio channel layout utility functions
#define FFMIN(a, b)
Definition: common.h:66
static int read_probe(AVProbeData *pd)
Definition: jvdec.c:55
ret
Definition: avfilter.c:974
static int qcp_probe(AVProbeData *pd)
Definition: qcp.c:77
internal header for RIFF based (de)muxers do NOT include this in end user applications ...
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:620
Stream structure.
Definition: avformat.h:842
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_reading.c:42
enum AVMediaType codec_type
Definition: avcodec.h:1249
static int qcp_read_header(AVFormatContext *s)
Definition: qcp.c:85
enum AVCodecID codec_id
Definition: avcodec.h:1258
int sample_rate
samples per second
Definition: avcodec.h:1985
AVIOContext * pb
I/O context.
Definition: avformat.h:1314
void * buf
Definition: avisynth_c.h:553
AVInputFormat ff_qcp_demuxer
Definition: qcp.c:191
static const uint8_t guid_evrc[16]
EVRC GUID as stored in the file.
Definition: qcp.c:55
static const uint8_t guid_qcelp_13k_part[15]
Last 15 out of 16 bytes of QCELP-13K GUID, as stored in the file; the first byte of the GUID can be e...
Definition: qcp.c:47
#define QCP_MAX_MODE
Definition: qcp.c:38
This structure contains the data a format has to probe a file.
Definition: avformat.h:448
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:460
unsigned int avio_rl16(AVIOContext *s)
Definition: aviobuf.c:642
Main libavformat public API header.
int16_t rates_per_mode[QCP_MAX_MODE+1]
contains the packet size corresponding to each mode, -1 if no size.
Definition: qcp.c:39
static double c[64]
#define FF_PRI_GUID
Definition: riff.h:93
int channels
number of audio channels
Definition: avcodec.h:1986
void * priv_data
Format private data.
Definition: avformat.h:1300
Definition: qcp.c:35
static int is_qcelp_13k_guid(const uint8_t *guid)
Definition: qcp.c:72
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:628
int avio_feof(AVIOContext *s)
feof() equivalent for AVIOContext.
Definition: aviobuf.c:300
#define AV_CH_LAYOUT_MONO
#define MKTAG(a, b, c, d)
Definition: common.h:315
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:85
This structure stores compressed data.
Definition: avcodec.h:1139