FFmpeg
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 "demux.h"
34 #include "riff.h"
35 
36 typedef struct QCPContext {
37  uint32_t data_size; ///< size of data chunk
38 
39 #define QCP_MAX_MODE 4
40  int16_t rates_per_mode[QCP_MAX_MODE+1]; ///< contains the packet size corresponding
41  ///< to each mode, -1 if no size.
42 } QCPContext;
43 
44 /**
45  * Last 15 out of 16 bytes of QCELP-13K GUID, as stored in the file;
46  * the first byte of the GUID can be either 0x41 or 0x42.
47  */
48 static const uint8_t guid_qcelp_13k_part[15] = {
49  0x6d, 0x7f, 0x5e, 0x15, 0xb1, 0xd0, 0x11, 0xba,
50  0x91, 0x00, 0x80, 0x5f, 0xb4, 0xb9, 0x7e
51 };
52 
53 /**
54  * EVRC GUID as stored in the file
55  */
56 static const uint8_t guid_evrc[16] = {
57  0x8d, 0xd4, 0x89, 0xe6, 0x76, 0x90, 0xb5, 0x46,
58  0x91, 0xef, 0x73, 0x6a, 0x51, 0x00, 0xce, 0xb4
59 };
60 
61 static const uint8_t guid_4gv[16] = {
62  0xca, 0x29, 0xfd, 0x3c, 0x53, 0xf6, 0xf5, 0x4e,
63  0x90, 0xe9, 0xf4, 0x23, 0x6d, 0x59, 0x9b, 0x61
64 };
65 
66 /**
67  * SMV GUID as stored in the file
68  */
69 static const uint8_t guid_smv[16] = {
70  0x75, 0x2b, 0x7c, 0x8d, 0x97, 0xa7, 0x49, 0xed,
71  0x98, 0x5e, 0xd5, 0x3c, 0x8c, 0xc7, 0x5f, 0x84
72 };
73 
74 /**
75  * @param guid contains at least 16 bytes
76  * @return 1 if the guid is a qcelp_13k guid, 0 otherwise
77  */
78 static int is_qcelp_13k_guid(const uint8_t *guid) {
79  return (guid[0] == 0x41 || guid[0] == 0x42)
80  && !memcmp(guid+1, guid_qcelp_13k_part, sizeof(guid_qcelp_13k_part));
81 }
82 
83 static int qcp_probe(const AVProbeData *pd)
84 {
85  if (AV_RL32(pd->buf ) == AV_RL32("RIFF") &&
86  AV_RL64(pd->buf+8) == AV_RL64("QLCMfmt "))
87  return AVPROBE_SCORE_MAX;
88  return 0;
89 }
90 
92 {
93  AVIOContext *pb = s->pb;
94  QCPContext *c = s->priv_data;
96  uint8_t buf[16];
97  int i;
98  unsigned nb_rates;
99 
100  if (!st)
101  return AVERROR(ENOMEM);
102 
103  avio_rb32(pb); // "RIFF"
104  avio_skip(pb, 4 + 8 + 4 + 1 + 1); // filesize + "QLCMfmt " + chunk-size + major-version + minor-version
105 
108  avio_read(pb, buf, 16);
109  if (is_qcelp_13k_guid(buf)) {
111  } else if (!memcmp(buf, guid_evrc, 16)) {
113  } else if (!memcmp(buf, guid_smv, 16)) {
115  } else if (!memcmp(buf, guid_4gv, 16)) {
117  } else {
118  av_log(s, AV_LOG_ERROR, "Unknown codec GUID "FF_PRI_GUID".\n",
119  FF_ARG_GUID(buf));
120  return AVERROR_INVALIDDATA;
121  }
122  avio_skip(pb, 2 + 80); // codec-version + codec-name
123  st->codecpar->bit_rate = avio_rl16(pb);
124 
125  s->packet_size = avio_rl16(pb);
126  avio_skip(pb, 2); // block-size
127  st->codecpar->sample_rate = avio_rl16(pb);
128  avio_skip(pb, 2); // sample-size
129 
130  memset(c->rates_per_mode, -1, sizeof(c->rates_per_mode));
131  nb_rates = avio_rl32(pb);
132  nb_rates = FFMIN(nb_rates, 8);
133  for (i=0; i<nb_rates; i++) {
134  int size = avio_r8(pb);
135  int mode = avio_r8(pb);
136  if (mode > QCP_MAX_MODE) {
137  av_log(s, AV_LOG_WARNING, "Unknown entry %d=>%d in rate-map-table.\n ", mode, size);
138  } else
139  c->rates_per_mode[mode] = size;
140  }
141  avio_skip(pb, 16 - 2*nb_rates + 20); // empty entries of rate-map-table + reserved
142 
143  return 0;
144 }
145 
147 {
148  AVIOContext *pb = s->pb;
149  QCPContext *c = s->priv_data;
150  unsigned int chunk_size, tag;
151 
152  while(!avio_feof(pb)) {
153  if (c->data_size) {
154  int pkt_size, ret, mode = avio_r8(pb);
155 
156  if (s->packet_size) {
157  pkt_size = s->packet_size - 1;
158  } else if (mode > QCP_MAX_MODE || (pkt_size = c->rates_per_mode[mode]) < 0) {
159  c->data_size--;
160  continue;
161  }
162 
163  if (c->data_size <= pkt_size) {
164  av_log(s, AV_LOG_WARNING, "Data chunk is too small.\n");
165  pkt_size = c->data_size - 1;
166  }
167 
168  if ((ret = av_get_packet(pb, pkt, pkt_size)) >= 0) {
169  if (pkt_size != ret)
170  av_log(s, AV_LOG_ERROR, "Packet size is too small.\n");
171 
172  c->data_size -= pkt_size + 1;
173  }
174  return ret;
175  }
176 
177  if (avio_tell(pb) & 1 && avio_r8(pb))
178  av_log(s, AV_LOG_WARNING, "Padding should be 0.\n");
179 
180  tag = avio_rl32(pb);
181  chunk_size = avio_rl32(pb);
182  switch (tag) {
183  case MKTAG('v', 'r', 'a', 't'):
184  if (avio_rl32(pb)) // var-rate-flag
185  s->packet_size = 0;
186  avio_skip(pb, 4); // size-in-packets
187  break;
188  case MKTAG('d', 'a', 't', 'a'):
189  c->data_size = chunk_size;
190  break;
191 
192  default:
193  avio_skip(pb, chunk_size);
194  }
195  }
196  return AVERROR_EOF;
197 }
198 
200  .p.name = "qcp",
201  .p.long_name = NULL_IF_CONFIG_SMALL("QCP"),
202  .priv_data_size = sizeof(QCPContext),
206 };
guid_smv
static const uint8_t guid_smv[16]
SMV GUID as stored in the file.
Definition: qcp.c:69
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:186
guid_4gv
static const uint8_t guid_4gv[16]
Definition: qcp.c:61
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
AVCodecParameters::codec_type
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:51
avformat_new_stream
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
AV_RL64
uint64_t_TMPL AV_RL64
Definition: bytestream.h:91
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:57
is_qcelp_13k_guid
static int is_qcelp_13k_guid(const uint8_t *guid)
Definition: qcp.c:78
qcp_probe
static int qcp_probe(const AVProbeData *pd)
Definition: qcp.c:83
AVPROBE_SCORE_MAX
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:463
avio_tell
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:494
avio_rl16
unsigned int avio_rl16(AVIOContext *s)
Definition: aviobuf.c:713
avio_rb32
unsigned int avio_rb32(AVIOContext *s)
Definition: aviobuf.c:760
pkt
AVPacket * pkt
Definition: movenc.c:59
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
read_packet
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_read_callback.c:41
FF_ARG_GUID
#define FF_ARG_GUID(g)
Definition: riff.h:109
intreadwrite.h
s
#define s(width, name)
Definition: cbs_vp9.c:198
AVInputFormat::name
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:553
AVProbeData::buf
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:453
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
QCPContext::data_size
uint32_t data_size
size of data chunk
Definition: qcp.c:37
guid_evrc
static const uint8_t guid_evrc[16]
EVRC GUID as stored in the file.
Definition: qcp.c:56
AVFormatContext
Format I/O context.
Definition: avformat.h:1255
ff_qcp_demuxer
const FFInputFormat ff_qcp_demuxer
Definition: qcp.c:199
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:766
read_header
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:550
NULL
#define NULL
Definition: coverity.c:32
qcp_read_packet
static int qcp_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: qcp.c:146
AV_CODEC_ID_SMV
@ AV_CODEC_ID_SMV
Definition: codec_id.h:512
AVProbeData
This structure contains the data a format has to probe a file.
Definition: avformat.h:451
guid_qcelp_13k_part
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:48
AVCodecParameters::ch_layout
AVChannelLayout ch_layout
Audio only.
Definition: codec_par.h:180
c
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
AVCodecParameters::sample_rate
int sample_rate
Audio only.
Definition: codec_par.h:184
AV_CODEC_ID_QCELP
@ AV_CODEC_ID_QCELP
Definition: codec_id.h:464
avio_rl32
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:729
AVIOContext
Bytestream IO Context.
Definition: avio.h:160
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:106
AVChannelLayout
An AVChannelLayout holds information about the channel layout of audio data.
Definition: channel_layout.h:303
size
int size
Definition: twinvq_data.h:10344
QCPContext::rates_per_mode
int16_t rates_per_mode[QCP_MAX_MODE+1]
contains the packet size corresponding to each mode, -1 if no size.
Definition: qcp.c:40
FFInputFormat::p
AVInputFormat p
The public AVInputFormat.
Definition: demux.h:41
avio_r8
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:602
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:255
AV_CODEC_ID_EVRC
@ AV_CODEC_ID_EVRC
Definition: codec_id.h:511
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
demux.h
av_get_packet
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:103
tag
uint32_t tag
Definition: movenc.c:1786
ret
ret
Definition: filter_design.txt:187
AVStream
Stream structure.
Definition: avformat.h:743
avformat.h
AV_RL32
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:92
qcp_read_header
static int qcp_read_header(AVFormatContext *s)
Definition: qcp.c:91
QCP_MAX_MODE
#define QCP_MAX_MODE
Definition: qcp.c:39
channel_layout.h
mode
mode
Definition: ebur128.h:83
avio_read
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:611
avio_skip
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:317
read_probe
static int read_probe(const AVProbeData *p)
Definition: cdg.c:30
AV_CHANNEL_LAYOUT_MONO
#define AV_CHANNEL_LAYOUT_MONO
Definition: channel_layout.h:378
QCPContext
Definition: qcp.c:36
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:55
AVPacket
This structure stores compressed data.
Definition: packet.h:499
riff.h
FFInputFormat
Definition: demux.h:37
AVCodecParameters::bit_rate
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
Definition: codec_par.h:97
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
AV_CODEC_ID_4GV
@ AV_CODEC_ID_4GV
Definition: codec_id.h:517
MKTAG
#define MKTAG(a, b, c, d)
Definition: macros.h:55
FF_PRI_GUID
#define FF_PRI_GUID
Definition: riff.h:105
avio_feof
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition: aviobuf.c:345