annotate ape.c @ 5105:ce86dfc507ec libavformat

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