FFmpeg
Loading...
Searching...
No Matches
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
32#include "avformat.h"
33#include "avio_internal.h"
34#include "demux.h"
35#include "riff.h"
36
37typedef struct QCPContext {
38 uint32_t data_size; ///< size of data chunk
39
40#define QCP_MAX_MODE 4
41 int16_t rates_per_mode[QCP_MAX_MODE+1]; ///< contains the packet size corresponding
42 ///< to each mode, -1 if no size.
44
45/**
46 * Last 15 out of 16 bytes of QCELP-13K GUID, as stored in the file;
47 * the first byte of the GUID can be either 0x41 or 0x42.
48 */
49static const uint8_t guid_qcelp_13k_part[15] = {
50 0x6d, 0x7f, 0x5e, 0x15, 0xb1, 0xd0, 0x11, 0xba,
51 0x91, 0x00, 0x80, 0x5f, 0xb4, 0xb9, 0x7e
52};
53
54/**
55 * EVRC GUID as stored in the file
56 */
57static const uint8_t guid_evrc[16] = {
58 0x8d, 0xd4, 0x89, 0xe6, 0x76, 0x90, 0xb5, 0x46,
59 0x91, 0xef, 0x73, 0x6a, 0x51, 0x00, 0xce, 0xb4
60};
61
62static const uint8_t guid_4gv[16] = {
63 0xca, 0x29, 0xfd, 0x3c, 0x53, 0xf6, 0xf5, 0x4e,
64 0x90, 0xe9, 0xf4, 0x23, 0x6d, 0x59, 0x9b, 0x61
65};
66
67/**
68 * SMV GUID as stored in the file
69 */
70static const uint8_t guid_smv[16] = {
71 0x75, 0x2b, 0x7c, 0x8d, 0x97, 0xa7, 0x49, 0xed,
72 0x98, 0x5e, 0xd5, 0x3c, 0x8c, 0xc7, 0x5f, 0x84
73};
74
75/**
76 * @param guid contains at least 16 bytes
77 * @return 1 if the guid is a qcelp_13k guid, 0 otherwise
78 */
79static int is_qcelp_13k_guid(const uint8_t *guid) {
80 return (guid[0] == 0x41 || guid[0] == 0x42)
81 && !memcmp(guid+1, guid_qcelp_13k_part, sizeof(guid_qcelp_13k_part));
82}
83
84static int qcp_probe(const AVProbeData *pd)
85{
86 if (AV_RL32(pd->buf ) == AV_RL32("RIFF") &&
87 AV_RL64(pd->buf+8) == AV_RL64("QLCMfmt "))
88 return AVPROBE_SCORE_MAX;
89 return 0;
90}
91
93{
94 AVIOContext *pb = s->pb;
95 QCPContext *c = s->priv_data;
97 uint8_t buf[16];
98 int ret;
99 int i;
100 unsigned nb_rates;
101
102 if (!st)
103 return AVERROR(ENOMEM);
104
105 avio_rb32(pb); // "RIFF"
106 avio_skip(pb, 4 + 8 + 4 + 1 + 1); // filesize + "QLCMfmt " + chunk-size + major-version + minor-version
107
110 ret = ffio_read_size(pb, buf, 16);
111 if (ret < 0)
112 return ret;
113 if (is_qcelp_13k_guid(buf)) {
115 } else if (!memcmp(buf, guid_evrc, 16)) {
117 } else if (!memcmp(buf, guid_smv, 16)) {
119 } else if (!memcmp(buf, guid_4gv, 16)) {
121 } else {
122 av_log(s, AV_LOG_ERROR, "Unknown codec GUID "FF_PRI_GUID".\n",
123 FF_ARG_GUID(buf));
124 return AVERROR_INVALIDDATA;
125 }
126 avio_skip(pb, 2 + 80); // codec-version + codec-name
127 st->codecpar->bit_rate = avio_rl16(pb);
128
129 s->packet_size = avio_rl16(pb);
130 avio_skip(pb, 2); // block-size
131 st->codecpar->sample_rate = avio_rl16(pb);
132 avio_skip(pb, 2); // sample-size
133
134 memset(c->rates_per_mode, -1, sizeof(c->rates_per_mode));
135 nb_rates = avio_rl32(pb);
136 nb_rates = FFMIN(nb_rates, 8);
137 for (i=0; i<nb_rates; i++) {
138 int size = avio_r8(pb);
139 int mode = avio_r8(pb);
140 if (mode > QCP_MAX_MODE) {
141 av_log(s, AV_LOG_WARNING, "Unknown entry %d=>%d in rate-map-table.\n ", mode, size);
142 } else
143 c->rates_per_mode[mode] = size;
144 }
145 avio_skip(pb, 16 - 2*nb_rates + 20); // empty entries of rate-map-table + reserved
146
147 return 0;
148}
149
151{
152 AVIOContext *pb = s->pb;
153 QCPContext *c = s->priv_data;
154 unsigned int chunk_size, tag;
155
156 while(!avio_feof(pb)) {
157 if (c->data_size) {
158 int pkt_size, ret, mode = avio_r8(pb);
159
160 if (s->packet_size) {
161 pkt_size = s->packet_size - 1;
162 } else if (mode > QCP_MAX_MODE || (pkt_size = c->rates_per_mode[mode]) < 0) {
163 c->data_size--;
164 continue;
165 }
166
167 if (c->data_size <= pkt_size) {
168 av_log(s, AV_LOG_WARNING, "Data chunk is too small.\n");
169 pkt_size = c->data_size - 1;
170 }
171
172 if ((ret = av_get_packet(pb, pkt, pkt_size)) >= 0) {
173 if (pkt_size != ret)
174 av_log(s, AV_LOG_ERROR, "Packet size is too small.\n");
175
176 c->data_size -= pkt_size + 1;
177 }
178 return ret;
179 }
180
181 if (avio_tell(pb) & 1 && avio_r8(pb))
182 av_log(s, AV_LOG_WARNING, "Padding should be 0.\n");
183
184 tag = avio_rl32(pb);
185 chunk_size = avio_rl32(pb);
186 switch (tag) {
187 case MKTAG('v', 'r', 'a', 't'):
188 if (avio_rl32(pb)) // var-rate-flag
189 s->packet_size = 0;
190 avio_skip(pb, 4); // size-in-packets
191 break;
192 case MKTAG('d', 'a', 't', 'a'):
193 c->data_size = chunk_size;
194 break;
195
196 default:
197 avio_skip(pb, chunk_size);
198 }
199 }
200 return AVERROR_EOF;
201}
202
204 .p.name = "qcp",
205 .p.long_name = NULL_IF_CONFIG_SMALL("QCP"),
206 .priv_data_size = sizeof(QCPContext),
210};
const FFInputFormat ff_qcp_demuxer
Definition qcp.c:203
Main libavformat public API header.
#define AVPROBE_SCORE_MAX
maximum score
Definition avformat.h:483
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:98
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition aviobuf.c:349
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition avio.h:494
unsigned int avio_rl16(AVIOContext *s)
Definition aviobuf.c:717
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition aviobuf.c:321
unsigned int avio_rl32(AVIOContext *s)
Definition aviobuf.c:733
unsigned int avio_rb32(AVIOContext *s)
Definition aviobuf.c:764
int avio_r8(AVIOContext *s)
Definition aviobuf.c:606
int ffio_read_size(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition aviobuf.c:665
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
static int read_probe(const AVProbeData *p)
Definition cdg.c:30
Public libavutil channel layout APIs header.
#define NULL
Definition coverity.c:32
static AVPacket * pkt
static int read_header(FFV1Context *f, RangeCoder *c)
Definition ffv1dec.c:578
@ AV_CODEC_ID_4GV
Definition codec_id.h:530
@ AV_CODEC_ID_SMV
Definition codec_id.h:525
@ AV_CODEC_ID_EVRC
Definition codec_id.h:524
@ AV_CODEC_ID_QCELP
Definition codec_id.h:477
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
#define AV_CHANNEL_LAYOUT_MONO
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR_EOF
End of file.
Definition error.h:57
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition log.h:216
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
#define AV_RL64(p)
#define AV_RL32(p)
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
#define FFMIN(a, b)
Definition macros.h:49
#define MKTAG(a, b, c, d)
Definition macros.h:55
uint32_t tag
Definition movenc.c:2087
static const uint8_t guid_smv[16]
SMV GUID as stored in the file.
Definition qcp.c:70
#define QCP_MAX_MODE
Definition qcp.c:40
static int is_qcelp_13k_guid(const uint8_t *guid)
Definition qcp.c:79
static int qcp_probe(const AVProbeData *pd)
Definition qcp.c:84
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:49
static int qcp_read_header(AVFormatContext *s)
Definition qcp.c:92
static const uint8_t guid_4gv[16]
Definition qcp.c:62
static const uint8_t guid_evrc[16]
EVRC GUID as stored in the file.
Definition qcp.c:57
static int qcp_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition qcp.c:150
internal header for RIFF based (de)muxers do NOT include this in end user applications
#define FF_PRI_GUID
Definition riff.h:105
#define FF_ARG_GUID(g)
Definition riff.h:109
An AVChannelLayout holds information about the channel layout of audio data.
AVChannelLayout ch_layout
The channel layout and number of channels.
Definition codec_par.h:207
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
Definition codec_par.h:99
enum AVMediaType codec_type
General type of the encoded data.
Definition codec_par.h:53
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition codec_par.h:57
int sample_rate
The number of audio samples per second.
Definition codec_par.h:213
Format I/O context.
Definition avformat.h:1333
Bytestream IO Context.
Definition avio.h:160
This structure stores compressed data.
Definition packet.h:580
This structure contains the data a format has to probe a file.
Definition avformat.h:471
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition avformat.h:473
Stream structure.
Definition avformat.h:766
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition avformat.h:789
int16_t rates_per_mode[QCP_MAX_MODE+1]
contains the packet size corresponding to each mode, -1 if no size.
Definition qcp.c:41
uint32_t data_size
size of data chunk
Definition qcp.c:38
Definition swscale.c:71
#define av_log(a,...)
int size
static double c[64]