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 
34 typedef struct {
35  uint32_t data_size; ///< size of data chunk
36 
37 #define QCP_MAX_MODE 4
38  int16_t rates_per_mode[QCP_MAX_MODE+1]; ///< contains the packet size corresponding
39  ///< to each mode, -1 if no size.
40 } QCPContext;
41 
42 /**
43  * Last 15 out of 16 bytes of QCELP-13K GUID, as stored in the file;
44  * the first byte of the GUID can be either 0x41 or 0x42.
45  */
46 static const uint8_t guid_qcelp_13k_part[15] = {
47  0x6d, 0x7f, 0x5e, 0x15, 0xb1, 0xd0, 0x11, 0xba,
48  0x91, 0x00, 0x80, 0x5f, 0xb4, 0xb9, 0x7e
49 };
50 
51 /**
52  * EVRC GUID as stored in the file
53  */
54 static const uint8_t guid_evrc[16] = {
55  0x8d, 0xd4, 0x89, 0xe6, 0x76, 0x90, 0xb5, 0x46,
56  0x91, 0xef, 0x73, 0x6a, 0x51, 0x00, 0xce, 0xb4
57 };
58 
59 /**
60  * SMV GUID as stored in the file
61  */
62 static const uint8_t guid_smv[16] = {
63  0x75, 0x2b, 0x7c, 0x8d, 0x97, 0xa7, 0x49, 0xed,
64  0x98, 0x5e, 0xd5, 0x3c, 0x8c, 0xc7, 0x5f, 0x84
65 };
66 
67 /**
68  * @param guid contains at least 16 bytes
69  * @return 1 if the guid is a qcelp_13k guid, 0 otherwise
70  */
71 static int is_qcelp_13k_guid(const uint8_t *guid) {
72  return (guid[0] == 0x41 || guid[0] == 0x42)
73  && !memcmp(guid+1, guid_qcelp_13k_part, sizeof(guid_qcelp_13k_part));
74 }
75 
76 static int qcp_probe(AVProbeData *pd)
77 {
78  if (AV_RL32(pd->buf ) == AV_RL32("RIFF") &&
79  AV_RL64(pd->buf+8) == AV_RL64("QLCMfmt "))
80  return AVPROBE_SCORE_MAX;
81  return 0;
82 }
83 
85 {
86  AVIOContext *pb = s->pb;
87  QCPContext *c = s->priv_data;
89  uint8_t buf[16];
90  int i, nb_rates;
91 
92  if (!st)
93  return AVERROR(ENOMEM);
94 
95  avio_rb32(pb); // "RIFF"
96  avio_skip(pb, 4 + 8 + 4 + 1 + 1); // filesize + "QLCMfmt " + chunk-size + major-version + minor-version
97 
99  st->codec->channels = 1;
101  avio_read(pb, buf, 16);
102  if (is_qcelp_13k_guid(buf)) {
104  } else if (!memcmp(buf, guid_evrc, 16)) {
106  } else if (!memcmp(buf, guid_smv, 16)) {
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(!url_feof(pb)) {
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"),
192  .priv_data_size = sizeof(QCPContext),
196 };
unsigned int packet_size
Definition: avformat.h:1018
const char * s
Definition: avisynth_c.h:668
Bytestream IO Context.
Definition: avio.h:68
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
int size
static const uint8_t guid_smv[16]
SMV GUID as stored in the file.
Definition: qcp.c:62
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:154
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:256
Format I/O context.
Definition: avformat.h:944
#define AV_RL64
static int qcp_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: qcp.c:136
mode
Definition: f_perms.c:27
unsigned int avio_rb32(AVIOContext *s)
Definition: aviobuf.c:610
uint32_t data_size
size of data chunk
Definition: qcp.c:35
static AVPacket pkt
Definition: demuxing.c:56
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
uint32_t tag
Definition: movenc.c:894
#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.
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:248
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:478
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:579
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
void av_log(void *avcl, int level, const char *fmt,...)
Send the specified message to the log if the level is less than or equal to the current av_log_level...
Definition: log.c:246
uint64_t channel_layout
Audio channel layout.
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:469
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:662
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:336
int bit_rate
the average bitrate
audio channel layout utility functions
#define FFMIN(a, b)
Definition: common.h:58
static int read_probe(AVProbeData *pd)
ret
Definition: avfilter.c:821
static int qcp_probe(AVProbeData *pd)
Definition: qcp.c:76
#define AV_RL32
int url_feof(AVIOContext *s)
feof() equivalent for AVIOContext.
Definition: aviobuf.c:280
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:517
Stream structure.
Definition: avformat.h:643
NULL
Definition: eval.c:55
enum AVMediaType codec_type
static int qcp_read_header(AVFormatContext *s)
Definition: qcp.c:84
enum AVCodecID codec_id
int sample_rate
samples per second
AVIOContext * pb
I/O context.
Definition: avformat.h:977
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:148
void * buf
Definition: avisynth_c.h:594
static int read_packet(AVFormatContext *ctx, AVPacket *pkt)
Definition: libcdio.c:114
synthesis window for stochastic i
AVInputFormat ff_qcp_demuxer
Definition: qcp.c:189
static const uint8_t guid_evrc[16]
EVRC GUID as stored in the file.
Definition: qcp.c:54
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:46
#define QCP_MAX_MODE
Definition: qcp.c:37
This structure contains the data a format has to probe a file.
Definition: avformat.h:334
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFilterBuffer 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.Buffer references ownership and permissions
#define AVPROBE_SCORE_MAX
maximum score, half of that is used for file-extension-based detection
Definition: avformat.h:340
unsigned int avio_rl16(AVIOContext *s)
Definition: aviobuf.c:563
Main libavformat public API header.
int16_t rates_per_mode[4+1]
contains the packet size corresponding
Definition: qcp.c:38
static double c[64]
int channels
number of audio channels
void * priv_data
Format private data.
Definition: avformat.h:964
Definition: qcp.c:34
static int is_qcelp_13k_guid(const uint8_t *guid)
Definition: qcp.c:71
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:461
#define AV_CH_LAYOUT_MONO
#define MKTAG(a, b, c, d)
Definition: common.h:282
This structure stores compressed data.