annotate ape.c @ 3733:f8aa0763f334 libavformat

Support Electronic Arts files containing MP3 audio.
author pross
date Mon, 18 Aug 2008 12:39:57 +0000
parents 7a0230981402
children 1d3d17de20ba
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
2556
cb131102b256 disable loads of debug messages to reduce object size
aurel
parents: 2554
diff changeset
27 #define ENABLE_DEBUG 0
cb131102b256 disable loads of debug messages to reduce object size
aurel
parents: 2554
diff changeset
28
2548
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
29 /* The earliest and latest file formats supported by this library */
2568
59f7ce8ad381 Looks like this APE decoder support versions starting from 3.95
kostya
parents: 2556
diff changeset
30 #define APE_MIN_VERSION 3950
2548
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
31 #define APE_MAX_VERSION 3990
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
32
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
33 #define MAC_FORMAT_FLAG_8_BIT 1 // is 8-bit [OBSOLETE]
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
34 #define MAC_FORMAT_FLAG_CRC 2 // uses the new CRC32 error detection [OBSOLETE]
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
35 #define MAC_FORMAT_FLAG_HAS_PEAK_LEVEL 4 // uint32 nPeakLevel after the header [OBSOLETE]
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
36 #define MAC_FORMAT_FLAG_24_BIT 8 // is 24-bit [OBSOLETE]
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
37 #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
38 #define MAC_FORMAT_FLAG_CREATE_WAV_HEADER 32 // create the wave header on decompression (not stored)
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
39
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
40 #define MAC_SUBFRAME_SIZE 4608
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
41
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
42 #define APE_EXTRADATA_SIZE 6
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
43
2554
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
44 /* APE tags */
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
45 #define APE_TAG_VERSION 2000
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
46 #define APE_TAG_FOOTER_BYTES 32
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
47 #define APE_TAG_FLAG_CONTAINS_HEADER (1 << 31)
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
48 #define APE_TAG_FLAG_IS_HEADER (1 << 29)
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
49
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
50 #define TAG(name, field) {name, offsetof(AVFormatContext, field), sizeof(((AVFormatContext *)0)->field)}
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
51
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
52 static const struct {
3007
bec20ba1e194 fix 8 "initialization discards qualifiers from pointer target type"
michael
parents: 2771
diff changeset
53 const char *name;
2554
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
54 int offset;
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
55 int size;
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
56 } tags[] = {
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
57 TAG("Title" , title ),
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
58 TAG("Artist" , author ),
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
59 TAG("Copyright", copyright),
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
60 TAG("Comment" , comment ),
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
61 TAG("Album" , album ),
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
62 TAG("Year" , year ),
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
63 TAG("Track" , track ),
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
64 TAG("Genre" , genre ),
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
65 { NULL }
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
66 };
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
67
2548
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
68 typedef struct {
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
69 int64_t pos;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
70 int nblocks;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
71 int size;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
72 int skip;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
73 int64_t pts;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
74 } APEFrame;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
75
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
76 typedef struct {
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
77 /* Derived fields */
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
78 uint32_t junklength;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
79 uint32_t firstframe;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
80 uint32_t totalsamples;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
81 int currentframe;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
82 APEFrame *frames;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
83
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
84 /* Info from Descriptor Block */
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
85 char magic[4];
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
86 int16_t fileversion;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
87 int16_t padding1;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
88 uint32_t descriptorlength;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
89 uint32_t headerlength;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
90 uint32_t seektablelength;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
91 uint32_t wavheaderlength;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
92 uint32_t audiodatalength;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
93 uint32_t audiodatalength_high;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
94 uint32_t wavtaillength;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
95 uint8_t md5[16];
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
96
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
97 /* Info from Header Block */
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
98 uint16_t compressiontype;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
99 uint16_t formatflags;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
100 uint32_t blocksperframe;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
101 uint32_t finalframeblocks;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
102 uint32_t totalframes;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
103 uint16_t bps;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
104 uint16_t channels;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
105 uint32_t samplerate;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
106
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
107 /* Seektable */
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
108 uint32_t *seektable;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
109 } APEContext;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
110
2554
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
111 static void ape_tag_read_field(AVFormatContext *s)
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
112 {
2771
d52c718e83f9 Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents: 2568
diff changeset
113 ByteIOContext *pb = s->pb;
2554
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
114 uint8_t buf[1024];
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
115 uint32_t size;
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
116 int i;
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
117
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
118 memset(buf, 0, 1024);
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
119 size = get_le32(pb); /* field size */
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
120 url_fskip(pb, 4); /* skip field flags */
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
121
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
122 for (i=0; pb->buf_ptr[i]!='0' && pb->buf_ptr[i]>=0x20 && pb->buf_ptr[i]<=0x7E; i++);
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
123
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
124 get_buffer(pb, buf, FFMIN(i, 1024));
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
125 url_fskip(pb, 1);
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
126
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
127 for (i=0; tags[i].name; i++)
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
128 if (!strcmp (buf, tags[i].name)) {
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
129 if (tags[i].size == sizeof(int)) {
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
130 char tmp[16];
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
131 get_buffer(pb, tmp, FFMIN(sizeof(tmp), size));
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
132 *(int *)(((char *)s)+tags[i].offset) = atoi(tmp);
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
133 } else {
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
134 get_buffer(pb, ((char *)s) + tags[i].offset,
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
135 FFMIN(tags[i].size, size));
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
136 }
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
137 break;
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
138 }
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
139
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
140 if (!tags[i].name)
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
141 url_fskip(pb, size);
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
142 }
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
143
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
144 static void ape_parse_tag(AVFormatContext *s)
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
145 {
2771
d52c718e83f9 Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents: 2568
diff changeset
146 ByteIOContext *pb = s->pb;
2554
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
147 int file_size = url_fsize(pb);
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
148 uint32_t val, fields, tag_bytes;
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
149 uint8_t buf[8];
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
150 int i;
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
151
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
152 if (file_size < APE_TAG_FOOTER_BYTES)
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
153 return;
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
154
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
155 url_fseek(pb, file_size - APE_TAG_FOOTER_BYTES, SEEK_SET);
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
156
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
157 get_buffer(pb, buf, 8); /* APETAGEX */
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
158 if (strncmp(buf, "APETAGEX", 8)) {
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
159 return;
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
160 }
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
161
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
162 val = get_le32(pb); /* APE tag version */
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
163 if (val > APE_TAG_VERSION) {
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
164 av_log(NULL, AV_LOG_ERROR, "Unsupported tag version. (>=%d)\n", APE_TAG_VERSION);
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
165 return;
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
166 }
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
167
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
168 tag_bytes = get_le32(pb); /* tag size */
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
169 if (tag_bytes - APE_TAG_FOOTER_BYTES > (1024 * 1024 * 16)) {
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
170 av_log(NULL, AV_LOG_ERROR, "Tag size is way too big\n");
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
171 return;
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
172 }
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
173
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
174 fields = get_le32(pb); /* number of fields */
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
175 if (fields > 65536) {
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
176 av_log(NULL, AV_LOG_ERROR, "Too many tag fields (%d)\n", fields);
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
177 return;
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
178 }
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
179
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
180 val = get_le32(pb); /* flags */
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
181 if (val & APE_TAG_FLAG_IS_HEADER) {
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
182 av_log(NULL, AV_LOG_ERROR, "APE Tag is a header\n");
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
183 return;
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
184 }
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
185
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
186 if (val & APE_TAG_FLAG_CONTAINS_HEADER)
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
187 tag_bytes += 2*APE_TAG_FOOTER_BYTES;
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
188
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
189 url_fseek(pb, file_size - tag_bytes, SEEK_SET);
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
190
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
191 for (i=0; i<fields; i++)
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
192 ape_tag_read_field(s);
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
193
2556
cb131102b256 disable loads of debug messages to reduce object size
aurel
parents: 2554
diff changeset
194 #if ENABLE_DEBUG
2554
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
195 av_log(NULL, AV_LOG_DEBUG, "\nAPE Tags:\n\n");
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
196 av_log(NULL, AV_LOG_DEBUG, "title = %s\n", s->title);
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
197 av_log(NULL, AV_LOG_DEBUG, "author = %s\n", s->author);
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
198 av_log(NULL, AV_LOG_DEBUG, "copyright = %s\n", s->copyright);
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
199 av_log(NULL, AV_LOG_DEBUG, "comment = %s\n", s->comment);
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
200 av_log(NULL, AV_LOG_DEBUG, "album = %s\n", s->album);
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
201 av_log(NULL, AV_LOG_DEBUG, "year = %d\n", s->year);
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
202 av_log(NULL, AV_LOG_DEBUG, "track = %d\n", s->track);
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
203 av_log(NULL, AV_LOG_DEBUG, "genre = %s\n", s->genre);
2556
cb131102b256 disable loads of debug messages to reduce object size
aurel
parents: 2554
diff changeset
204 #endif
2554
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
205 }
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
206
2548
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
207 static int ape_probe(AVProbeData * p)
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
208 {
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
209 if (p->buf[0] == 'M' && p->buf[1] == 'A' && p->buf[2] == 'C' && p->buf[3] == ' ')
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
210 return AVPROBE_SCORE_MAX;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
211
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
212 return 0;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
213 }
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
214
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
215 static void ape_dumpinfo(APEContext * ape_ctx)
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
216 {
2556
cb131102b256 disable loads of debug messages to reduce object size
aurel
parents: 2554
diff changeset
217 #if ENABLE_DEBUG
2548
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
218 int i;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
219
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
220 av_log(NULL, AV_LOG_DEBUG, "Descriptor Block:\n\n");
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
221 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
222 av_log(NULL, AV_LOG_DEBUG, "fileversion = %d\n", ape_ctx->fileversion);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
223 av_log(NULL, AV_LOG_DEBUG, "descriptorlength = %d\n", ape_ctx->descriptorlength);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
224 av_log(NULL, AV_LOG_DEBUG, "headerlength = %d\n", ape_ctx->headerlength);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
225 av_log(NULL, AV_LOG_DEBUG, "seektablelength = %d\n", ape_ctx->seektablelength);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
226 av_log(NULL, AV_LOG_DEBUG, "wavheaderlength = %d\n", ape_ctx->wavheaderlength);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
227 av_log(NULL, AV_LOG_DEBUG, "audiodatalength = %d\n", ape_ctx->audiodatalength);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
228 av_log(NULL, AV_LOG_DEBUG, "audiodatalength_high = %d\n", ape_ctx->audiodatalength_high);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
229 av_log(NULL, AV_LOG_DEBUG, "wavtaillength = %d\n", ape_ctx->wavtaillength);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
230 av_log(NULL, AV_LOG_DEBUG, "md5 = ");
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
231 for (i = 0; i < 16; i++)
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
232 av_log(NULL, AV_LOG_DEBUG, "%02x", ape_ctx->md5[i]);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
233 av_log(NULL, AV_LOG_DEBUG, "\n");
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
234
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
235 av_log(NULL, AV_LOG_DEBUG, "\nHeader Block:\n\n");
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
236
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
237 av_log(NULL, AV_LOG_DEBUG, "compressiontype = %d\n", ape_ctx->compressiontype);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
238 av_log(NULL, AV_LOG_DEBUG, "formatflags = %d\n", ape_ctx->formatflags);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
239 av_log(NULL, AV_LOG_DEBUG, "blocksperframe = %d\n", ape_ctx->blocksperframe);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
240 av_log(NULL, AV_LOG_DEBUG, "finalframeblocks = %d\n", ape_ctx->finalframeblocks);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
241 av_log(NULL, AV_LOG_DEBUG, "totalframes = %d\n", ape_ctx->totalframes);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
242 av_log(NULL, AV_LOG_DEBUG, "bps = %d\n", ape_ctx->bps);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
243 av_log(NULL, AV_LOG_DEBUG, "channels = %d\n", ape_ctx->channels);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
244 av_log(NULL, AV_LOG_DEBUG, "samplerate = %d\n", ape_ctx->samplerate);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
245
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
246 av_log(NULL, AV_LOG_DEBUG, "\nSeektable\n\n");
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
247 if ((ape_ctx->seektablelength / sizeof(uint32_t)) != ape_ctx->totalframes) {
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
248 av_log(NULL, AV_LOG_DEBUG, "No seektable\n");
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
249 } else {
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
250 for (i = 0; i < ape_ctx->seektablelength / sizeof(uint32_t); i++) {
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
251 if (i < ape_ctx->totalframes - 1) {
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
252 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
253 } else {
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
254 av_log(NULL, AV_LOG_DEBUG, "%8d %d\n", i, ape_ctx->seektable[i]);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
255 }
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
256 }
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
257 }
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
258
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
259 av_log(NULL, AV_LOG_DEBUG, "\nFrames\n\n");
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
260 for (i = 0; i < ape_ctx->totalframes; i++)
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
261 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
262
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
263 av_log(NULL, AV_LOG_DEBUG, "\nCalculated information:\n\n");
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
264 av_log(NULL, AV_LOG_DEBUG, "junklength = %d\n", ape_ctx->junklength);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
265 av_log(NULL, AV_LOG_DEBUG, "firstframe = %d\n", ape_ctx->firstframe);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
266 av_log(NULL, AV_LOG_DEBUG, "totalsamples = %d\n", ape_ctx->totalsamples);
2556
cb131102b256 disable loads of debug messages to reduce object size
aurel
parents: 2554
diff changeset
267 #endif
2548
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
268 }
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
269
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
270 static int ape_read_header(AVFormatContext * s, AVFormatParameters * ap)
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
271 {
2771
d52c718e83f9 Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents: 2568
diff changeset
272 ByteIOContext *pb = s->pb;
2548
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
273 APEContext *ape = s->priv_data;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
274 AVStream *st;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
275 uint32_t tag;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
276 int i;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
277 int total_blocks;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
278 int64_t pts;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
279
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
280 /* TODO: Skip any leading junk such as id3v2 tags */
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
281 ape->junklength = 0;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
282
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
283 tag = get_le32(pb);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
284 if (tag != MKTAG('M', 'A', 'C', ' '))
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
285 return -1;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
286
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
287 ape->fileversion = get_le16(pb);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
288
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
289 if (ape->fileversion < APE_MIN_VERSION || ape->fileversion > APE_MAX_VERSION) {
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
290 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
291 return -1;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
292 }
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
293
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
294 if (ape->fileversion >= 3980) {
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
295 ape->padding1 = get_le16(pb);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
296 ape->descriptorlength = get_le32(pb);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
297 ape->headerlength = get_le32(pb);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
298 ape->seektablelength = get_le32(pb);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
299 ape->wavheaderlength = get_le32(pb);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
300 ape->audiodatalength = get_le32(pb);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
301 ape->audiodatalength_high = get_le32(pb);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
302 ape->wavtaillength = get_le32(pb);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
303 get_buffer(pb, ape->md5, 16);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
304
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
305 /* Skip any unknown bytes at the end of the descriptor.
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
306 This is for future compatibility */
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
307 if (ape->descriptorlength > 52)
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
308 url_fseek(pb, ape->descriptorlength - 52, SEEK_CUR);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
309
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
310 /* Read header data */
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
311 ape->compressiontype = get_le16(pb);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
312 ape->formatflags = get_le16(pb);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
313 ape->blocksperframe = get_le32(pb);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
314 ape->finalframeblocks = get_le32(pb);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
315 ape->totalframes = get_le32(pb);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
316 ape->bps = get_le16(pb);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
317 ape->channels = get_le16(pb);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
318 ape->samplerate = get_le32(pb);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
319 } else {
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
320 ape->descriptorlength = 0;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
321 ape->headerlength = 32;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
322
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
323 ape->compressiontype = get_le16(pb);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
324 ape->formatflags = get_le16(pb);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
325 ape->channels = get_le16(pb);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
326 ape->samplerate = get_le32(pb);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
327 ape->wavheaderlength = get_le32(pb);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
328 ape->wavtaillength = get_le32(pb);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
329 ape->totalframes = get_le32(pb);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
330 ape->finalframeblocks = get_le32(pb);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
331
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
332 if (ape->formatflags & MAC_FORMAT_FLAG_HAS_PEAK_LEVEL) {
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
333 url_fseek(pb, 4, SEEK_CUR); /* Skip the peak level */
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
334 ape->headerlength += 4;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
335 }
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
336
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
337 if (ape->formatflags & MAC_FORMAT_FLAG_HAS_SEEK_ELEMENTS) {
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
338 ape->seektablelength = get_le32(pb);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
339 ape->headerlength += 4;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
340 ape->seektablelength *= sizeof(int32_t);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
341 } else
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
342 ape->seektablelength = ape->totalframes * sizeof(int32_t);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
343
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
344 if (ape->formatflags & MAC_FORMAT_FLAG_8_BIT)
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
345 ape->bps = 8;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
346 else if (ape->formatflags & MAC_FORMAT_FLAG_24_BIT)
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
347 ape->bps = 24;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
348 else
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
349 ape->bps = 16;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
350
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
351 if (ape->fileversion >= 3950)
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
352 ape->blocksperframe = 73728 * 4;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
353 else if (ape->fileversion >= 3900 || (ape->fileversion >= 3800 && ape->compressiontype >= 4000))
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
354 ape->blocksperframe = 73728;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
355 else
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
356 ape->blocksperframe = 9216;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
357
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
358 /* Skip any stored wav header */
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
359 if (!(ape->formatflags & MAC_FORMAT_FLAG_CREATE_WAV_HEADER))
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
360 url_fskip(pb, ape->wavheaderlength);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
361 }
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
362
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
363 if(ape->totalframes > UINT_MAX / sizeof(APEFrame)){
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
364 av_log(s, AV_LOG_ERROR, "Too many frames: %d\n", ape->totalframes);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
365 return -1;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
366 }
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
367 ape->frames = av_malloc(ape->totalframes * sizeof(APEFrame));
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
368 if(!ape->frames)
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
369 return AVERROR_NOMEM;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
370 ape->firstframe = ape->junklength + ape->descriptorlength + ape->headerlength + ape->seektablelength + ape->wavheaderlength;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
371 ape->currentframe = 0;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
372
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
373
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
374 ape->totalsamples = ape->finalframeblocks;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
375 if (ape->totalframes > 1)
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
376 ape->totalsamples += ape->blocksperframe * (ape->totalframes - 1);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
377
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
378 if (ape->seektablelength > 0) {
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
379 ape->seektable = av_malloc(ape->seektablelength);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
380 for (i = 0; i < ape->seektablelength / sizeof(uint32_t); i++)
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
381 ape->seektable[i] = get_le32(pb);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
382 }
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
383
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
384 ape->frames[0].pos = ape->firstframe;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
385 ape->frames[0].nblocks = ape->blocksperframe;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
386 ape->frames[0].skip = 0;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
387 for (i = 1; i < ape->totalframes; i++) {
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
388 ape->frames[i].pos = ape->seektable[i]; //ape->frames[i-1].pos + ape->blocksperframe;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
389 ape->frames[i].nblocks = ape->blocksperframe;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
390 ape->frames[i - 1].size = ape->frames[i].pos - ape->frames[i - 1].pos;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
391 ape->frames[i].skip = (ape->frames[i].pos - ape->frames[0].pos) & 3;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
392 }
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
393 ape->frames[ape->totalframes - 1].size = ape->finalframeblocks * 4;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
394 ape->frames[ape->totalframes - 1].nblocks = ape->finalframeblocks;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
395
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
396 for (i = 0; i < ape->totalframes; i++) {
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
397 if(ape->frames[i].skip){
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
398 ape->frames[i].pos -= ape->frames[i].skip;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
399 ape->frames[i].size += ape->frames[i].skip;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
400 }
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
401 ape->frames[i].size = (ape->frames[i].size + 3) & ~3;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
402 }
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
403
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
404
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
405 ape_dumpinfo(ape);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
406
2554
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
407 /* try to read APE tags */
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
408 if (!url_is_streamed(pb)) {
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
409 ape_parse_tag(s);
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
410 url_fseek(pb, 0, SEEK_SET);
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
411 }
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
412
2548
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
413 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
414
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
415 /* now we are ready: build format streams */
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
416 st = av_new_stream(s, 0);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
417 if (!st)
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
418 return -1;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
419
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
420 total_blocks = (ape->totalframes == 0) ? 0 : ((ape->totalframes - 1) * ape->blocksperframe) + ape->finalframeblocks;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
421
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
422 st->codec->codec_type = CODEC_TYPE_AUDIO;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
423 st->codec->codec_id = CODEC_ID_APE;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
424 st->codec->codec_tag = MKTAG('A', 'P', 'E', ' ');
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
425 st->codec->channels = ape->channels;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
426 st->codec->sample_rate = ape->samplerate;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
427 st->codec->bits_per_sample = ape->bps;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
428 st->codec->frame_size = MAC_SUBFRAME_SIZE;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
429
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
430 st->nb_frames = ape->totalframes;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
431 s->start_time = 0;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
432 s->duration = (int64_t) total_blocks * AV_TIME_BASE / ape->samplerate;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
433 av_set_pts_info(st, 64, MAC_SUBFRAME_SIZE, ape->samplerate);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
434
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
435 st->codec->extradata = av_malloc(APE_EXTRADATA_SIZE);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
436 st->codec->extradata_size = APE_EXTRADATA_SIZE;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
437 AV_WL16(st->codec->extradata + 0, ape->fileversion);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
438 AV_WL16(st->codec->extradata + 2, ape->compressiontype);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
439 AV_WL16(st->codec->extradata + 4, ape->formatflags);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
440
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
441 pts = 0;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
442 for (i = 0; i < ape->totalframes; i++) {
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
443 ape->frames[i].pts = pts;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
444 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
445 pts += ape->blocksperframe / MAC_SUBFRAME_SIZE;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
446 }
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
447
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
448 return 0;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
449 }
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
450
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
451 static int ape_read_packet(AVFormatContext * s, AVPacket * pkt)
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
452 {
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
453 int ret;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
454 int nblocks;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
455 APEContext *ape = s->priv_data;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
456 uint32_t extra_size = 8;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
457
2771
d52c718e83f9 Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents: 2568
diff changeset
458 if (url_feof(s->pb))
2548
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
459 return AVERROR_IO;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
460 if (ape->currentframe > ape->totalframes)
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
461 return AVERROR_IO;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
462
2771
d52c718e83f9 Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents: 2568
diff changeset
463 url_fseek (s->pb, ape->frames[ape->currentframe].pos, SEEK_SET);
2548
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
464
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
465 /* Calculate how many blocks there are in this frame */
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
466 if (ape->currentframe == (ape->totalframes - 1))
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
467 nblocks = ape->finalframeblocks;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
468 else
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
469 nblocks = ape->blocksperframe;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
470
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
471 if (av_new_packet(pkt, ape->frames[ape->currentframe].size + extra_size) < 0)
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
472 return AVERROR_NOMEM;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
473
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
474 AV_WL32(pkt->data , nblocks);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
475 AV_WL32(pkt->data + 4, ape->frames[ape->currentframe].skip);
2771
d52c718e83f9 Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents: 2568
diff changeset
476 ret = get_buffer(s->pb, pkt->data + extra_size, ape->frames[ape->currentframe].size);
2548
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
477
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
478 pkt->pts = ape->frames[ape->currentframe].pts;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
479 pkt->stream_index = 0;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
480
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
481 /* note: we need to modify the packet size here to handle the last
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
482 packet */
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
483 pkt->size = ret + extra_size;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
484
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
485 ape->currentframe++;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
486
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
487 return 0;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
488 }
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
489
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
490 static int ape_read_close(AVFormatContext * s)
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
491 {
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
492 APEContext *ape = s->priv_data;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
493
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
494 av_freep(&ape->frames);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
495 av_freep(&ape->seektable);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
496 return 0;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
497 }
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
498
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
499 static int ape_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
500 {
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
501 AVStream *st = s->streams[stream_index];
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
502 APEContext *ape = s->priv_data;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
503 int index = av_index_search_timestamp(st, timestamp, flags);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
504
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
505 if (index < 0)
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
506 return -1;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
507
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
508 ape->currentframe = index;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
509 return 0;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
510 }
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
511
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
512 AVInputFormat ape_demuxer = {
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
513 "ape",
3424
7a0230981402 Make long_names in lavf/lavdev optional depending on CONFIG_SMALL.
diego
parents: 3266
diff changeset
514 NULL_IF_CONFIG_SMALL("Monkey's Audio"),
2548
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
515 sizeof(APEContext),
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
516 ape_probe,
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
517 ape_read_header,
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
518 ape_read_packet,
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
519 ape_read_close,
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
520 ape_read_seek,
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
521 .extensions = "ape,apl,mac"
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
522 };