annotate ape.c @ 2548:276257e703af libavformat

Monkey Audio decoder
author kostya
date Thu, 13 Sep 2007 03:22:47 +0000
parents
children 2d6bfd63d606
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2548
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
1 /*
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
2 * Monkey's Audio APE demuxer
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
3 * Copyright (c) 2007 Benjamin Zores <ben@geexbox.org>
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
4 * based upon libdemac from Dave Chapman.
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
5 *
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
6 * This file is part of FFmpeg.
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
7 *
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
8 * FFmpeg is free software; you can redistribute it and/or
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
9 * modify it under the terms of the GNU Lesser General Public
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
10 * License as published by the Free Software Foundation; either
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
11 * version 2.1 of the License, or (at your option) any later version.
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
12 *
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
13 * FFmpeg is distributed in the hope that it will be useful,
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
16 * Lesser General Public License for more details.
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
17 *
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
18 * You should have received a copy of the GNU Lesser General Public
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
19 * License along with FFmpeg; if not, write to the Free Software
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
21 */
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
22
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
23 #include <stdio.h>
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
24
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
25 #include "avformat.h"
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
26
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
27 /* The earliest and latest file formats supported by this library */
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
28 #define APE_MIN_VERSION 3970
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
29 #define APE_MAX_VERSION 3990
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
30
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
31 #define MAC_FORMAT_FLAG_8_BIT 1 // is 8-bit [OBSOLETE]
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
32 #define MAC_FORMAT_FLAG_CRC 2 // uses the new CRC32 error detection [OBSOLETE]
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
33 #define MAC_FORMAT_FLAG_HAS_PEAK_LEVEL 4 // uint32 nPeakLevel after the header [OBSOLETE]
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
34 #define MAC_FORMAT_FLAG_24_BIT 8 // is 24-bit [OBSOLETE]
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
35 #define MAC_FORMAT_FLAG_HAS_SEEK_ELEMENTS 16 // has the number of seek elements after the peak level
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
36 #define MAC_FORMAT_FLAG_CREATE_WAV_HEADER 32 // create the wave header on decompression (not stored)
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
37
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
38 #define MAC_SUBFRAME_SIZE 4608
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
39
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
40 #define APE_EXTRADATA_SIZE 6
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
41
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
42 typedef struct {
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
43 int64_t pos;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
44 int nblocks;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
45 int size;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
46 int skip;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
47 int64_t pts;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
48 } APEFrame;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
49
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
50 typedef struct {
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
51 /* Derived fields */
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
52 uint32_t junklength;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
53 uint32_t firstframe;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
54 uint32_t totalsamples;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
55 int currentframe;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
56 APEFrame *frames;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
57
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
58 /* Info from Descriptor Block */
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
59 char magic[4];
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
60 int16_t fileversion;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
61 int16_t padding1;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
62 uint32_t descriptorlength;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
63 uint32_t headerlength;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
64 uint32_t seektablelength;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
65 uint32_t wavheaderlength;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
66 uint32_t audiodatalength;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
67 uint32_t audiodatalength_high;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
68 uint32_t wavtaillength;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
69 uint8_t md5[16];
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
70
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
71 /* Info from Header Block */
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
72 uint16_t compressiontype;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
73 uint16_t formatflags;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
74 uint32_t blocksperframe;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
75 uint32_t finalframeblocks;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
76 uint32_t totalframes;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
77 uint16_t bps;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
78 uint16_t channels;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
79 uint32_t samplerate;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
80
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
81 /* Seektable */
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
82 uint32_t *seektable;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
83 } APEContext;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
84
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
85 static int ape_probe(AVProbeData * p)
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
86 {
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
87 if (p->buf[0] == 'M' && p->buf[1] == 'A' && p->buf[2] == 'C' && p->buf[3] == ' ')
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
88 return AVPROBE_SCORE_MAX;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
89
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
90 return 0;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
91 }
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
92
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
93 static void ape_dumpinfo(APEContext * ape_ctx)
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
94 {
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
95 int i;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
96
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
97 av_log(NULL, AV_LOG_DEBUG, "Descriptor Block:\n\n");
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
98 av_log(NULL, AV_LOG_DEBUG, "magic = \"%c%c%c%c\"\n", ape_ctx->magic[0], ape_ctx->magic[1], ape_ctx->magic[2], ape_ctx->magic[3]);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
99 av_log(NULL, AV_LOG_DEBUG, "fileversion = %d\n", ape_ctx->fileversion);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
100 av_log(NULL, AV_LOG_DEBUG, "descriptorlength = %d\n", ape_ctx->descriptorlength);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
101 av_log(NULL, AV_LOG_DEBUG, "headerlength = %d\n", ape_ctx->headerlength);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
102 av_log(NULL, AV_LOG_DEBUG, "seektablelength = %d\n", ape_ctx->seektablelength);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
103 av_log(NULL, AV_LOG_DEBUG, "wavheaderlength = %d\n", ape_ctx->wavheaderlength);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
104 av_log(NULL, AV_LOG_DEBUG, "audiodatalength = %d\n", ape_ctx->audiodatalength);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
105 av_log(NULL, AV_LOG_DEBUG, "audiodatalength_high = %d\n", ape_ctx->audiodatalength_high);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
106 av_log(NULL, AV_LOG_DEBUG, "wavtaillength = %d\n", ape_ctx->wavtaillength);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
107 av_log(NULL, AV_LOG_DEBUG, "md5 = ");
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
108 for (i = 0; i < 16; i++)
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
109 av_log(NULL, AV_LOG_DEBUG, "%02x", ape_ctx->md5[i]);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
110 av_log(NULL, AV_LOG_DEBUG, "\n");
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
111
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
112 av_log(NULL, AV_LOG_DEBUG, "\nHeader Block:\n\n");
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
113
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
114 av_log(NULL, AV_LOG_DEBUG, "compressiontype = %d\n", ape_ctx->compressiontype);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
115 av_log(NULL, AV_LOG_DEBUG, "formatflags = %d\n", ape_ctx->formatflags);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
116 av_log(NULL, AV_LOG_DEBUG, "blocksperframe = %d\n", ape_ctx->blocksperframe);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
117 av_log(NULL, AV_LOG_DEBUG, "finalframeblocks = %d\n", ape_ctx->finalframeblocks);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
118 av_log(NULL, AV_LOG_DEBUG, "totalframes = %d\n", ape_ctx->totalframes);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
119 av_log(NULL, AV_LOG_DEBUG, "bps = %d\n", ape_ctx->bps);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
120 av_log(NULL, AV_LOG_DEBUG, "channels = %d\n", ape_ctx->channels);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
121 av_log(NULL, AV_LOG_DEBUG, "samplerate = %d\n", ape_ctx->samplerate);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
122
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
123 av_log(NULL, AV_LOG_DEBUG, "\nSeektable\n\n");
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
124 if ((ape_ctx->seektablelength / sizeof(uint32_t)) != ape_ctx->totalframes) {
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
125 av_log(NULL, AV_LOG_DEBUG, "No seektable\n");
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
126 } else {
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
127 for (i = 0; i < ape_ctx->seektablelength / sizeof(uint32_t); i++) {
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
128 if (i < ape_ctx->totalframes - 1) {
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
129 av_log(NULL, AV_LOG_DEBUG, "%8d %d (%d bytes)\n", i, ape_ctx->seektable[i], ape_ctx->seektable[i + 1] - ape_ctx->seektable[i]);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
130 } else {
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
131 av_log(NULL, AV_LOG_DEBUG, "%8d %d\n", i, ape_ctx->seektable[i]);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
132 }
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
133 }
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
134 }
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
135
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
136 av_log(NULL, AV_LOG_DEBUG, "\nFrames\n\n");
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
137 for (i = 0; i < ape_ctx->totalframes; i++)
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
138 av_log(NULL, AV_LOG_DEBUG, "%8d %8lld %8d (%d samples)\n", i, ape_ctx->frames[i].pos, ape_ctx->frames[i].size, ape_ctx->frames[i].nblocks);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
139
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
140 av_log(NULL, AV_LOG_DEBUG, "\nCalculated information:\n\n");
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
141 av_log(NULL, AV_LOG_DEBUG, "junklength = %d\n", ape_ctx->junklength);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
142 av_log(NULL, AV_LOG_DEBUG, "firstframe = %d\n", ape_ctx->firstframe);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
143 av_log(NULL, AV_LOG_DEBUG, "totalsamples = %d\n", ape_ctx->totalsamples);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
144 }
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
145
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
146 static int ape_read_header(AVFormatContext * s, AVFormatParameters * ap)
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
147 {
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
148 ByteIOContext *pb = &s->pb;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
149 APEContext *ape = s->priv_data;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
150 AVStream *st;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
151 uint32_t tag;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
152 int i;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
153 int total_blocks;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
154 int64_t pts;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
155
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
156 /* TODO: Skip any leading junk such as id3v2 tags */
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
157 ape->junklength = 0;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
158
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
159 tag = get_le32(pb);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
160 if (tag != MKTAG('M', 'A', 'C', ' '))
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
161 return -1;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
162
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
163 ape->fileversion = get_le16(pb);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
164
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
165 if (ape->fileversion < APE_MIN_VERSION || ape->fileversion > APE_MAX_VERSION) {
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
166 av_log(s, AV_LOG_ERROR, "Unsupported file version - %d.%02d\n", ape->fileversion / 1000, (ape->fileversion % 1000) / 10);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
167 return -1;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
168 }
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
169
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
170 if (ape->fileversion >= 3980) {
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
171 ape->padding1 = get_le16(pb);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
172 ape->descriptorlength = get_le32(pb);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
173 ape->headerlength = get_le32(pb);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
174 ape->seektablelength = get_le32(pb);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
175 ape->wavheaderlength = get_le32(pb);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
176 ape->audiodatalength = get_le32(pb);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
177 ape->audiodatalength_high = get_le32(pb);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
178 ape->wavtaillength = get_le32(pb);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
179 get_buffer(pb, ape->md5, 16);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
180
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
181 /* Skip any unknown bytes at the end of the descriptor.
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
182 This is for future compatibility */
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
183 if (ape->descriptorlength > 52)
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
184 url_fseek(pb, ape->descriptorlength - 52, SEEK_CUR);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
185
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
186 /* Read header data */
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
187 ape->compressiontype = get_le16(pb);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
188 ape->formatflags = get_le16(pb);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
189 ape->blocksperframe = get_le32(pb);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
190 ape->finalframeblocks = get_le32(pb);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
191 ape->totalframes = get_le32(pb);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
192 ape->bps = get_le16(pb);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
193 ape->channels = get_le16(pb);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
194 ape->samplerate = get_le32(pb);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
195 } else {
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
196 ape->descriptorlength = 0;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
197 ape->headerlength = 32;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
198
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
199 ape->compressiontype = get_le16(pb);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
200 ape->formatflags = get_le16(pb);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
201 ape->channels = get_le16(pb);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
202 ape->samplerate = get_le32(pb);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
203 ape->wavheaderlength = get_le32(pb);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
204 ape->wavtaillength = get_le32(pb);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
205 ape->totalframes = get_le32(pb);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
206 ape->finalframeblocks = get_le32(pb);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
207
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
208 if (ape->formatflags & MAC_FORMAT_FLAG_HAS_PEAK_LEVEL) {
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
209 url_fseek(pb, 4, SEEK_CUR); /* Skip the peak level */
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
210 ape->headerlength += 4;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
211 }
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
212
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
213 if (ape->formatflags & MAC_FORMAT_FLAG_HAS_SEEK_ELEMENTS) {
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
214 ape->seektablelength = get_le32(pb);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
215 ape->headerlength += 4;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
216 ape->seektablelength *= sizeof(int32_t);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
217 } else
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
218 ape->seektablelength = ape->totalframes * sizeof(int32_t);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
219
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
220 if (ape->formatflags & MAC_FORMAT_FLAG_8_BIT)
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
221 ape->bps = 8;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
222 else if (ape->formatflags & MAC_FORMAT_FLAG_24_BIT)
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
223 ape->bps = 24;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
224 else
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
225 ape->bps = 16;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
226
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
227 if (ape->fileversion >= 3950)
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
228 ape->blocksperframe = 73728 * 4;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
229 else if (ape->fileversion >= 3900 || (ape->fileversion >= 3800 && ape->compressiontype >= 4000))
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
230 ape->blocksperframe = 73728;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
231 else
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
232 ape->blocksperframe = 9216;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
233
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
234 /* Skip any stored wav header */
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
235 if (!(ape->formatflags & MAC_FORMAT_FLAG_CREATE_WAV_HEADER))
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
236 url_fskip(pb, ape->wavheaderlength);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
237 }
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
238
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
239 if(ape->totalframes > UINT_MAX / sizeof(APEFrame)){
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
240 av_log(s, AV_LOG_ERROR, "Too many frames: %d\n", ape->totalframes);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
241 return -1;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
242 }
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
243 ape->frames = av_malloc(ape->totalframes * sizeof(APEFrame));
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
244 if(!ape->frames)
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
245 return AVERROR_NOMEM;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
246 ape->firstframe = ape->junklength + ape->descriptorlength + ape->headerlength + ape->seektablelength + ape->wavheaderlength;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
247 ape->currentframe = 0;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
248
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
249
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
250 ape->totalsamples = ape->finalframeblocks;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
251 if (ape->totalframes > 1)
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
252 ape->totalsamples += ape->blocksperframe * (ape->totalframes - 1);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
253
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
254 if (ape->seektablelength > 0) {
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
255 ape->seektable = av_malloc(ape->seektablelength);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
256 for (i = 0; i < ape->seektablelength / sizeof(uint32_t); i++)
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
257 ape->seektable[i] = get_le32(pb);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
258 }
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
259
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
260 ape->frames[0].pos = ape->firstframe;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
261 ape->frames[0].nblocks = ape->blocksperframe;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
262 ape->frames[0].skip = 0;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
263 for (i = 1; i < ape->totalframes; i++) {
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
264 ape->frames[i].pos = ape->seektable[i]; //ape->frames[i-1].pos + ape->blocksperframe;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
265 ape->frames[i].nblocks = ape->blocksperframe;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
266 ape->frames[i - 1].size = ape->frames[i].pos - ape->frames[i - 1].pos;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
267 ape->frames[i].skip = (ape->frames[i].pos - ape->frames[0].pos) & 3;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
268 }
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
269 ape->frames[ape->totalframes - 1].size = ape->finalframeblocks * 4;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
270 ape->frames[ape->totalframes - 1].nblocks = ape->finalframeblocks;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
271
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
272 for (i = 0; i < ape->totalframes; i++) {
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
273 if(ape->frames[i].skip){
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
274 ape->frames[i].pos -= ape->frames[i].skip;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
275 ape->frames[i].size += ape->frames[i].skip;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
276 }
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
277 ape->frames[i].size = (ape->frames[i].size + 3) & ~3;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
278 }
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
279
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
280
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
281 ape_dumpinfo(ape);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
282
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
283 av_log(s, AV_LOG_DEBUG, "Decoding file - v%d.%02d, compression level %d\n", ape->fileversion / 1000, (ape->fileversion % 1000) / 10, ape->compressiontype);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
284
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
285 /* now we are ready: build format streams */
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
286 st = av_new_stream(s, 0);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
287 if (!st)
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
288 return -1;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
289
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
290 total_blocks = (ape->totalframes == 0) ? 0 : ((ape->totalframes - 1) * ape->blocksperframe) + ape->finalframeblocks;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
291
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
292 st->codec->codec_type = CODEC_TYPE_AUDIO;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
293 st->codec->codec_id = CODEC_ID_APE;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
294 st->codec->codec_tag = MKTAG('A', 'P', 'E', ' ');
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
295 st->codec->channels = ape->channels;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
296 st->codec->sample_rate = ape->samplerate;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
297 st->codec->bits_per_sample = ape->bps;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
298 st->codec->frame_size = MAC_SUBFRAME_SIZE;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
299
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
300 st->nb_frames = ape->totalframes;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
301 s->start_time = 0;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
302 s->duration = (int64_t) total_blocks * AV_TIME_BASE / ape->samplerate;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
303 av_set_pts_info(st, 64, MAC_SUBFRAME_SIZE, ape->samplerate);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
304
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
305 st->codec->extradata = av_malloc(APE_EXTRADATA_SIZE);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
306 st->codec->extradata_size = APE_EXTRADATA_SIZE;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
307 AV_WL16(st->codec->extradata + 0, ape->fileversion);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
308 AV_WL16(st->codec->extradata + 2, ape->compressiontype);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
309 AV_WL16(st->codec->extradata + 4, ape->formatflags);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
310
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
311 pts = 0;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
312 for (i = 0; i < ape->totalframes; i++) {
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
313 ape->frames[i].pts = pts;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
314 av_add_index_entry(st, ape->frames[i].pos, ape->frames[i].pts, 0, 0, AVINDEX_KEYFRAME);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
315 pts += ape->blocksperframe / MAC_SUBFRAME_SIZE;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
316 }
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
317
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
318 return 0;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
319 }
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
320
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
321 static int ape_read_packet(AVFormatContext * s, AVPacket * pkt)
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
322 {
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
323 int ret;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
324 int nblocks;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
325 APEContext *ape = s->priv_data;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
326 uint32_t extra_size = 8;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
327
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
328 if (url_feof(&s->pb))
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
329 return AVERROR_IO;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
330 if (ape->currentframe > ape->totalframes)
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
331 return AVERROR_IO;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
332
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
333 url_fseek (&s->pb, ape->frames[ape->currentframe].pos, SEEK_SET);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
334
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
335 /* Calculate how many blocks there are in this frame */
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
336 if (ape->currentframe == (ape->totalframes - 1))
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
337 nblocks = ape->finalframeblocks;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
338 else
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
339 nblocks = ape->blocksperframe;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
340
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
341 if (av_new_packet(pkt, ape->frames[ape->currentframe].size + extra_size) < 0)
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
342 return AVERROR_NOMEM;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
343
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
344 AV_WL32(pkt->data , nblocks);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
345 AV_WL32(pkt->data + 4, ape->frames[ape->currentframe].skip);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
346 ret = get_buffer(&s->pb, pkt->data + extra_size, ape->frames[ape->currentframe].size);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
347
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
348 pkt->pts = ape->frames[ape->currentframe].pts;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
349 pkt->stream_index = 0;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
350
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
351 /* note: we need to modify the packet size here to handle the last
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
352 packet */
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
353 pkt->size = ret + extra_size;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
354
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
355 ape->currentframe++;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
356
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
357 return 0;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
358 }
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
359
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
360 static int ape_read_close(AVFormatContext * s)
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
361 {
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
362 APEContext *ape = s->priv_data;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
363
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
364 av_freep(&ape->frames);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
365 av_freep(&ape->seektable);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
366 return 0;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
367 }
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
368
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
369 static int ape_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
370 {
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
371 AVStream *st = s->streams[stream_index];
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
372 APEContext *ape = s->priv_data;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
373 int index = av_index_search_timestamp(st, timestamp, flags);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
374
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
375 if (index < 0)
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
376 return -1;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
377
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
378 ape->currentframe = index;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
379 return 0;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
380 }
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
381
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
382 AVInputFormat ape_demuxer = {
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
383 "ape",
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
384 "Monkey's Audio",
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
385 sizeof(APEContext),
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
386 ape_probe,
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
387 ape_read_header,
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
388 ape_read_packet,
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
389 ape_read_close,
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
390 ape_read_seek,
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
391 .extensions = "ape,apl,mac"
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
392 };