annotate ape.c @ 3007:bec20ba1e194 libavformat

fix 8 "initialization discards qualifiers from pointer target type" warnings
author michael
date Mon, 04 Feb 2008 00:42:27 +0000
parents d52c718e83f9
children ef617b3c568d
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 av_log(NULL, AV_LOG_ERROR, "Invalid APE Tags\n");
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
160 return;
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
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
163 val = get_le32(pb); /* APE tag version */
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
164 if (val > APE_TAG_VERSION) {
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
165 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
166 return;
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
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
169 tag_bytes = get_le32(pb); /* tag size */
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
170 if (tag_bytes - APE_TAG_FOOTER_BYTES > (1024 * 1024 * 16)) {
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
171 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
172 return;
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
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
175 fields = get_le32(pb); /* number of fields */
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
176 if (fields > 65536) {
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
177 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
178 return;
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
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
181 val = get_le32(pb); /* flags */
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
182 if (val & APE_TAG_FLAG_IS_HEADER) {
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
183 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
184 return;
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
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
187 if (val & APE_TAG_FLAG_CONTAINS_HEADER)
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
188 tag_bytes += 2*APE_TAG_FOOTER_BYTES;
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
189
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
190 url_fseek(pb, file_size - tag_bytes, SEEK_SET);
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
191
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
192 for (i=0; i<fields; i++)
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
193 ape_tag_read_field(s);
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
194
2556
cb131102b256 disable loads of debug messages to reduce object size
aurel
parents: 2554
diff changeset
195 #if ENABLE_DEBUG
2554
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
196 av_log(NULL, AV_LOG_DEBUG, "\nAPE Tags:\n\n");
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
197 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
198 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
199 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
200 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
201 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
202 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
203 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
204 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
205 #endif
2554
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
206 }
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
207
2548
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
208 static int ape_probe(AVProbeData * p)
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
209 {
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
210 if (p->buf[0] == 'M' && p->buf[1] == 'A' && p->buf[2] == 'C' && p->buf[3] == ' ')
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
211 return AVPROBE_SCORE_MAX;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
212
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
213 return 0;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
214 }
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
215
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
216 static void ape_dumpinfo(APEContext * ape_ctx)
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
217 {
2556
cb131102b256 disable loads of debug messages to reduce object size
aurel
parents: 2554
diff changeset
218 #if ENABLE_DEBUG
2548
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
219 int i;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
220
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
221 av_log(NULL, AV_LOG_DEBUG, "Descriptor Block:\n\n");
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
222 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
223 av_log(NULL, AV_LOG_DEBUG, "fileversion = %d\n", ape_ctx->fileversion);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
224 av_log(NULL, AV_LOG_DEBUG, "descriptorlength = %d\n", ape_ctx->descriptorlength);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
225 av_log(NULL, AV_LOG_DEBUG, "headerlength = %d\n", ape_ctx->headerlength);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
226 av_log(NULL, AV_LOG_DEBUG, "seektablelength = %d\n", ape_ctx->seektablelength);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
227 av_log(NULL, AV_LOG_DEBUG, "wavheaderlength = %d\n", ape_ctx->wavheaderlength);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
228 av_log(NULL, AV_LOG_DEBUG, "audiodatalength = %d\n", ape_ctx->audiodatalength);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
229 av_log(NULL, AV_LOG_DEBUG, "audiodatalength_high = %d\n", ape_ctx->audiodatalength_high);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
230 av_log(NULL, AV_LOG_DEBUG, "wavtaillength = %d\n", ape_ctx->wavtaillength);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
231 av_log(NULL, AV_LOG_DEBUG, "md5 = ");
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
232 for (i = 0; i < 16; i++)
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
233 av_log(NULL, AV_LOG_DEBUG, "%02x", ape_ctx->md5[i]);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
234 av_log(NULL, AV_LOG_DEBUG, "\n");
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
235
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
236 av_log(NULL, AV_LOG_DEBUG, "\nHeader Block:\n\n");
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
237
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
238 av_log(NULL, AV_LOG_DEBUG, "compressiontype = %d\n", ape_ctx->compressiontype);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
239 av_log(NULL, AV_LOG_DEBUG, "formatflags = %d\n", ape_ctx->formatflags);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
240 av_log(NULL, AV_LOG_DEBUG, "blocksperframe = %d\n", ape_ctx->blocksperframe);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
241 av_log(NULL, AV_LOG_DEBUG, "finalframeblocks = %d\n", ape_ctx->finalframeblocks);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
242 av_log(NULL, AV_LOG_DEBUG, "totalframes = %d\n", ape_ctx->totalframes);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
243 av_log(NULL, AV_LOG_DEBUG, "bps = %d\n", ape_ctx->bps);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
244 av_log(NULL, AV_LOG_DEBUG, "channels = %d\n", ape_ctx->channels);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
245 av_log(NULL, AV_LOG_DEBUG, "samplerate = %d\n", ape_ctx->samplerate);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
246
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
247 av_log(NULL, AV_LOG_DEBUG, "\nSeektable\n\n");
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
248 if ((ape_ctx->seektablelength / sizeof(uint32_t)) != ape_ctx->totalframes) {
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
249 av_log(NULL, AV_LOG_DEBUG, "No seektable\n");
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
250 } else {
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
251 for (i = 0; i < ape_ctx->seektablelength / sizeof(uint32_t); i++) {
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
252 if (i < ape_ctx->totalframes - 1) {
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
253 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
254 } else {
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
255 av_log(NULL, AV_LOG_DEBUG, "%8d %d\n", i, ape_ctx->seektable[i]);
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
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
260 av_log(NULL, AV_LOG_DEBUG, "\nFrames\n\n");
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
261 for (i = 0; i < ape_ctx->totalframes; i++)
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
262 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
263
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
264 av_log(NULL, AV_LOG_DEBUG, "\nCalculated information:\n\n");
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
265 av_log(NULL, AV_LOG_DEBUG, "junklength = %d\n", ape_ctx->junklength);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
266 av_log(NULL, AV_LOG_DEBUG, "firstframe = %d\n", ape_ctx->firstframe);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
267 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
268 #endif
2548
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
269 }
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
270
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
271 static int ape_read_header(AVFormatContext * s, AVFormatParameters * ap)
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
272 {
2771
d52c718e83f9 Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents: 2568
diff changeset
273 ByteIOContext *pb = s->pb;
2548
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
274 APEContext *ape = s->priv_data;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
275 AVStream *st;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
276 uint32_t tag;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
277 int i;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
278 int total_blocks;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
279 int64_t pts;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
280
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
281 /* TODO: Skip any leading junk such as id3v2 tags */
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
282 ape->junklength = 0;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
283
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
284 tag = get_le32(pb);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
285 if (tag != MKTAG('M', 'A', 'C', ' '))
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
286 return -1;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
287
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
288 ape->fileversion = get_le16(pb);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
289
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
290 if (ape->fileversion < APE_MIN_VERSION || ape->fileversion > APE_MAX_VERSION) {
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
291 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
292 return -1;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
293 }
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
294
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
295 if (ape->fileversion >= 3980) {
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
296 ape->padding1 = get_le16(pb);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
297 ape->descriptorlength = get_le32(pb);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
298 ape->headerlength = get_le32(pb);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
299 ape->seektablelength = get_le32(pb);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
300 ape->wavheaderlength = get_le32(pb);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
301 ape->audiodatalength = get_le32(pb);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
302 ape->audiodatalength_high = get_le32(pb);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
303 ape->wavtaillength = get_le32(pb);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
304 get_buffer(pb, ape->md5, 16);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
305
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
306 /* Skip any unknown bytes at the end of the descriptor.
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
307 This is for future compatibility */
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
308 if (ape->descriptorlength > 52)
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
309 url_fseek(pb, ape->descriptorlength - 52, SEEK_CUR);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
310
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
311 /* Read header data */
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
312 ape->compressiontype = get_le16(pb);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
313 ape->formatflags = get_le16(pb);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
314 ape->blocksperframe = get_le32(pb);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
315 ape->finalframeblocks = get_le32(pb);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
316 ape->totalframes = get_le32(pb);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
317 ape->bps = get_le16(pb);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
318 ape->channels = get_le16(pb);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
319 ape->samplerate = get_le32(pb);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
320 } else {
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
321 ape->descriptorlength = 0;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
322 ape->headerlength = 32;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
323
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
324 ape->compressiontype = get_le16(pb);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
325 ape->formatflags = get_le16(pb);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
326 ape->channels = get_le16(pb);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
327 ape->samplerate = get_le32(pb);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
328 ape->wavheaderlength = get_le32(pb);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
329 ape->wavtaillength = get_le32(pb);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
330 ape->totalframes = get_le32(pb);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
331 ape->finalframeblocks = get_le32(pb);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
332
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
333 if (ape->formatflags & MAC_FORMAT_FLAG_HAS_PEAK_LEVEL) {
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
334 url_fseek(pb, 4, SEEK_CUR); /* Skip the peak level */
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
335 ape->headerlength += 4;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
336 }
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
337
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
338 if (ape->formatflags & MAC_FORMAT_FLAG_HAS_SEEK_ELEMENTS) {
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
339 ape->seektablelength = get_le32(pb);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
340 ape->headerlength += 4;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
341 ape->seektablelength *= sizeof(int32_t);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
342 } else
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
343 ape->seektablelength = ape->totalframes * sizeof(int32_t);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
344
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
345 if (ape->formatflags & MAC_FORMAT_FLAG_8_BIT)
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
346 ape->bps = 8;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
347 else if (ape->formatflags & MAC_FORMAT_FLAG_24_BIT)
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
348 ape->bps = 24;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
349 else
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
350 ape->bps = 16;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
351
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
352 if (ape->fileversion >= 3950)
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
353 ape->blocksperframe = 73728 * 4;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
354 else if (ape->fileversion >= 3900 || (ape->fileversion >= 3800 && ape->compressiontype >= 4000))
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
355 ape->blocksperframe = 73728;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
356 else
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
357 ape->blocksperframe = 9216;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
358
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
359 /* Skip any stored wav header */
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
360 if (!(ape->formatflags & MAC_FORMAT_FLAG_CREATE_WAV_HEADER))
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
361 url_fskip(pb, ape->wavheaderlength);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
362 }
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
363
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
364 if(ape->totalframes > UINT_MAX / sizeof(APEFrame)){
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
365 av_log(s, AV_LOG_ERROR, "Too many frames: %d\n", ape->totalframes);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
366 return -1;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
367 }
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
368 ape->frames = av_malloc(ape->totalframes * sizeof(APEFrame));
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
369 if(!ape->frames)
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
370 return AVERROR_NOMEM;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
371 ape->firstframe = ape->junklength + ape->descriptorlength + ape->headerlength + ape->seektablelength + ape->wavheaderlength;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
372 ape->currentframe = 0;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
373
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
374
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
375 ape->totalsamples = ape->finalframeblocks;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
376 if (ape->totalframes > 1)
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
377 ape->totalsamples += ape->blocksperframe * (ape->totalframes - 1);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
378
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
379 if (ape->seektablelength > 0) {
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
380 ape->seektable = av_malloc(ape->seektablelength);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
381 for (i = 0; i < ape->seektablelength / sizeof(uint32_t); i++)
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
382 ape->seektable[i] = get_le32(pb);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
383 }
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
384
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
385 ape->frames[0].pos = ape->firstframe;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
386 ape->frames[0].nblocks = ape->blocksperframe;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
387 ape->frames[0].skip = 0;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
388 for (i = 1; i < ape->totalframes; i++) {
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
389 ape->frames[i].pos = ape->seektable[i]; //ape->frames[i-1].pos + ape->blocksperframe;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
390 ape->frames[i].nblocks = ape->blocksperframe;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
391 ape->frames[i - 1].size = ape->frames[i].pos - ape->frames[i - 1].pos;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
392 ape->frames[i].skip = (ape->frames[i].pos - ape->frames[0].pos) & 3;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
393 }
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
394 ape->frames[ape->totalframes - 1].size = ape->finalframeblocks * 4;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
395 ape->frames[ape->totalframes - 1].nblocks = ape->finalframeblocks;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
396
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
397 for (i = 0; i < ape->totalframes; i++) {
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
398 if(ape->frames[i].skip){
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
399 ape->frames[i].pos -= ape->frames[i].skip;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
400 ape->frames[i].size += ape->frames[i].skip;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
401 }
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
402 ape->frames[i].size = (ape->frames[i].size + 3) & ~3;
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
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
406 ape_dumpinfo(ape);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
407
2554
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
408 /* try to read APE tags */
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
409 if (!url_is_streamed(pb)) {
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
410 ape_parse_tag(s);
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
411 url_fseek(pb, 0, SEEK_SET);
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
412 }
2d6bfd63d606 monkey audio demuxer now can parse ape tags
ben
parents: 2548
diff changeset
413
2548
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
414 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
415
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
416 /* now we are ready: build format streams */
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
417 st = av_new_stream(s, 0);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
418 if (!st)
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
419 return -1;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
420
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
421 total_blocks = (ape->totalframes == 0) ? 0 : ((ape->totalframes - 1) * ape->blocksperframe) + ape->finalframeblocks;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
422
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
423 st->codec->codec_type = CODEC_TYPE_AUDIO;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
424 st->codec->codec_id = CODEC_ID_APE;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
425 st->codec->codec_tag = MKTAG('A', 'P', 'E', ' ');
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
426 st->codec->channels = ape->channels;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
427 st->codec->sample_rate = ape->samplerate;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
428 st->codec->bits_per_sample = ape->bps;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
429 st->codec->frame_size = MAC_SUBFRAME_SIZE;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
430
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
431 st->nb_frames = ape->totalframes;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
432 s->start_time = 0;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
433 s->duration = (int64_t) total_blocks * AV_TIME_BASE / ape->samplerate;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
434 av_set_pts_info(st, 64, MAC_SUBFRAME_SIZE, ape->samplerate);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
435
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
436 st->codec->extradata = av_malloc(APE_EXTRADATA_SIZE);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
437 st->codec->extradata_size = APE_EXTRADATA_SIZE;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
438 AV_WL16(st->codec->extradata + 0, ape->fileversion);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
439 AV_WL16(st->codec->extradata + 2, ape->compressiontype);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
440 AV_WL16(st->codec->extradata + 4, ape->formatflags);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
441
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
442 pts = 0;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
443 for (i = 0; i < ape->totalframes; i++) {
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
444 ape->frames[i].pts = pts;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
445 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
446 pts += ape->blocksperframe / MAC_SUBFRAME_SIZE;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
447 }
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
448
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
449 return 0;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
450 }
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
451
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
452 static int ape_read_packet(AVFormatContext * s, AVPacket * pkt)
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
453 {
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
454 int ret;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
455 int nblocks;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
456 APEContext *ape = s->priv_data;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
457 uint32_t extra_size = 8;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
458
2771
d52c718e83f9 Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents: 2568
diff changeset
459 if (url_feof(s->pb))
2548
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
460 return AVERROR_IO;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
461 if (ape->currentframe > ape->totalframes)
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
462 return AVERROR_IO;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
463
2771
d52c718e83f9 Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents: 2568
diff changeset
464 url_fseek (s->pb, ape->frames[ape->currentframe].pos, SEEK_SET);
2548
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
465
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
466 /* Calculate how many blocks there are in this frame */
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
467 if (ape->currentframe == (ape->totalframes - 1))
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
468 nblocks = ape->finalframeblocks;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
469 else
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
470 nblocks = ape->blocksperframe;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
471
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
472 if (av_new_packet(pkt, ape->frames[ape->currentframe].size + extra_size) < 0)
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
473 return AVERROR_NOMEM;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
474
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
475 AV_WL32(pkt->data , nblocks);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
476 AV_WL32(pkt->data + 4, ape->frames[ape->currentframe].skip);
2771
d52c718e83f9 Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents: 2568
diff changeset
477 ret = get_buffer(s->pb, pkt->data + extra_size, ape->frames[ape->currentframe].size);
2548
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
478
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
479 pkt->pts = ape->frames[ape->currentframe].pts;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
480 pkt->stream_index = 0;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
481
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
482 /* note: we need to modify the packet size here to handle the last
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
483 packet */
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
484 pkt->size = ret + extra_size;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
485
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
486 ape->currentframe++;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
487
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
488 return 0;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
489 }
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
490
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
491 static int ape_read_close(AVFormatContext * s)
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
492 {
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
493 APEContext *ape = s->priv_data;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
494
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
495 av_freep(&ape->frames);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
496 av_freep(&ape->seektable);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
497 return 0;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
498 }
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
499
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
500 static int ape_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
501 {
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
502 AVStream *st = s->streams[stream_index];
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
503 APEContext *ape = s->priv_data;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
504 int index = av_index_search_timestamp(st, timestamp, flags);
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
505
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
506 if (index < 0)
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
507 return -1;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
508
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
509 ape->currentframe = index;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
510 return 0;
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
511 }
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
512
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
513 AVInputFormat ape_demuxer = {
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
514 "ape",
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
515 "Monkey's Audio",
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
516 sizeof(APEContext),
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
517 ape_probe,
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
518 ape_read_header,
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
519 ape_read_packet,
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
520 ape_read_close,
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
521 ape_read_seek,
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
522 .extensions = "ape,apl,mac"
276257e703af Monkey Audio decoder
kostya
parents:
diff changeset
523 };