annotate ffmpeg/libavformat/ape.c @ 13:844d341cf643 tip

Back up before ISMIR
author Yading Song <yading.song@eecs.qmul.ac.uk>
date Thu, 31 Oct 2013 13:17:06 +0000
parents f445c3017523
children
rev   line source
yading@11 1 /*
yading@11 2 * Monkey's Audio APE demuxer
yading@11 3 * Copyright (c) 2007 Benjamin Zores <ben@geexbox.org>
yading@11 4 * based upon libdemac from Dave Chapman.
yading@11 5 *
yading@11 6 * This file is part of FFmpeg.
yading@11 7 *
yading@11 8 * FFmpeg is free software; you can redistribute it and/or
yading@11 9 * modify it under the terms of the GNU Lesser General Public
yading@11 10 * License as published by the Free Software Foundation; either
yading@11 11 * version 2.1 of the License, or (at your option) any later version.
yading@11 12 *
yading@11 13 * FFmpeg is distributed in the hope that it will be useful,
yading@11 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
yading@11 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
yading@11 16 * Lesser General Public License for more details.
yading@11 17 *
yading@11 18 * You should have received a copy of the GNU Lesser General Public
yading@11 19 * License along with FFmpeg; if not, write to the Free Software
yading@11 20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
yading@11 21 */
yading@11 22
yading@11 23 #include <stdio.h>
yading@11 24
yading@11 25 #include "libavutil/intreadwrite.h"
yading@11 26 #include "avformat.h"
yading@11 27 #include "internal.h"
yading@11 28 #include "apetag.h"
yading@11 29
yading@11 30 /* The earliest and latest file formats supported by this library */
yading@11 31 #define APE_MIN_VERSION 3800
yading@11 32 #define APE_MAX_VERSION 3990
yading@11 33
yading@11 34 #define MAC_FORMAT_FLAG_8_BIT 1 // is 8-bit [OBSOLETE]
yading@11 35 #define MAC_FORMAT_FLAG_CRC 2 // uses the new CRC32 error detection [OBSOLETE]
yading@11 36 #define MAC_FORMAT_FLAG_HAS_PEAK_LEVEL 4 // uint32 nPeakLevel after the header [OBSOLETE]
yading@11 37 #define MAC_FORMAT_FLAG_24_BIT 8 // is 24-bit [OBSOLETE]
yading@11 38 #define MAC_FORMAT_FLAG_HAS_SEEK_ELEMENTS 16 // has the number of seek elements after the peak level
yading@11 39 #define MAC_FORMAT_FLAG_CREATE_WAV_HEADER 32 // create the wave header on decompression (not stored)
yading@11 40
yading@11 41 #define APE_EXTRADATA_SIZE 6
yading@11 42
yading@11 43 typedef struct {
yading@11 44 int64_t pos;
yading@11 45 int nblocks;
yading@11 46 int size;
yading@11 47 int skip;
yading@11 48 int64_t pts;
yading@11 49 } APEFrame;
yading@11 50
yading@11 51 typedef struct {
yading@11 52 /* Derived fields */
yading@11 53 uint32_t junklength;
yading@11 54 uint32_t firstframe;
yading@11 55 uint32_t totalsamples;
yading@11 56 int currentframe;
yading@11 57 APEFrame *frames;
yading@11 58
yading@11 59 /* Info from Descriptor Block */
yading@11 60 char magic[4];
yading@11 61 int16_t fileversion;
yading@11 62 int16_t padding1;
yading@11 63 uint32_t descriptorlength;
yading@11 64 uint32_t headerlength;
yading@11 65 uint32_t seektablelength;
yading@11 66 uint32_t wavheaderlength;
yading@11 67 uint32_t audiodatalength;
yading@11 68 uint32_t audiodatalength_high;
yading@11 69 uint32_t wavtaillength;
yading@11 70 uint8_t md5[16];
yading@11 71
yading@11 72 /* Info from Header Block */
yading@11 73 uint16_t compressiontype;
yading@11 74 uint16_t formatflags;
yading@11 75 uint32_t blocksperframe;
yading@11 76 uint32_t finalframeblocks;
yading@11 77 uint32_t totalframes;
yading@11 78 uint16_t bps;
yading@11 79 uint16_t channels;
yading@11 80 uint32_t samplerate;
yading@11 81
yading@11 82 /* Seektable */
yading@11 83 uint32_t *seektable;
yading@11 84 uint8_t *bittable;
yading@11 85 } APEContext;
yading@11 86
yading@11 87 static int ape_probe(AVProbeData * p)
yading@11 88 {
yading@11 89 if (p->buf[0] == 'M' && p->buf[1] == 'A' && p->buf[2] == 'C' && p->buf[3] == ' ')
yading@11 90 return AVPROBE_SCORE_MAX;
yading@11 91
yading@11 92 return 0;
yading@11 93 }
yading@11 94
yading@11 95 static void ape_dumpinfo(AVFormatContext * s, APEContext * ape_ctx)
yading@11 96 {
yading@11 97 #ifdef DEBUG
yading@11 98 int i;
yading@11 99
yading@11 100 av_log(s, AV_LOG_DEBUG, "Descriptor Block:\n\n");
yading@11 101 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]);
yading@11 102 av_log(s, AV_LOG_DEBUG, "fileversion = %"PRId16"\n", ape_ctx->fileversion);
yading@11 103 av_log(s, AV_LOG_DEBUG, "descriptorlength = %"PRIu32"\n", ape_ctx->descriptorlength);
yading@11 104 av_log(s, AV_LOG_DEBUG, "headerlength = %"PRIu32"\n", ape_ctx->headerlength);
yading@11 105 av_log(s, AV_LOG_DEBUG, "seektablelength = %"PRIu32"\n", ape_ctx->seektablelength);
yading@11 106 av_log(s, AV_LOG_DEBUG, "wavheaderlength = %"PRIu32"\n", ape_ctx->wavheaderlength);
yading@11 107 av_log(s, AV_LOG_DEBUG, "audiodatalength = %"PRIu32"\n", ape_ctx->audiodatalength);
yading@11 108 av_log(s, AV_LOG_DEBUG, "audiodatalength_high = %"PRIu32"\n", ape_ctx->audiodatalength_high);
yading@11 109 av_log(s, AV_LOG_DEBUG, "wavtaillength = %"PRIu32"\n", ape_ctx->wavtaillength);
yading@11 110 av_log(s, AV_LOG_DEBUG, "md5 = ");
yading@11 111 for (i = 0; i < 16; i++)
yading@11 112 av_log(s, AV_LOG_DEBUG, "%02x", ape_ctx->md5[i]);
yading@11 113 av_log(s, AV_LOG_DEBUG, "\n");
yading@11 114
yading@11 115 av_log(s, AV_LOG_DEBUG, "\nHeader Block:\n\n");
yading@11 116
yading@11 117 av_log(s, AV_LOG_DEBUG, "compressiontype = %"PRIu16"\n", ape_ctx->compressiontype);
yading@11 118 av_log(s, AV_LOG_DEBUG, "formatflags = %"PRIu16"\n", ape_ctx->formatflags);
yading@11 119 av_log(s, AV_LOG_DEBUG, "blocksperframe = %"PRIu32"\n", ape_ctx->blocksperframe);
yading@11 120 av_log(s, AV_LOG_DEBUG, "finalframeblocks = %"PRIu32"\n", ape_ctx->finalframeblocks);
yading@11 121 av_log(s, AV_LOG_DEBUG, "totalframes = %"PRIu32"\n", ape_ctx->totalframes);
yading@11 122 av_log(s, AV_LOG_DEBUG, "bps = %"PRIu16"\n", ape_ctx->bps);
yading@11 123 av_log(s, AV_LOG_DEBUG, "channels = %"PRIu16"\n", ape_ctx->channels);
yading@11 124 av_log(s, AV_LOG_DEBUG, "samplerate = %"PRIu32"\n", ape_ctx->samplerate);
yading@11 125
yading@11 126 av_log(s, AV_LOG_DEBUG, "\nSeektable\n\n");
yading@11 127 if ((ape_ctx->seektablelength / sizeof(uint32_t)) != ape_ctx->totalframes) {
yading@11 128 av_log(s, AV_LOG_DEBUG, "No seektable\n");
yading@11 129 } else {
yading@11 130 for (i = 0; i < ape_ctx->seektablelength / sizeof(uint32_t); i++) {
yading@11 131 if (i < ape_ctx->totalframes - 1) {
yading@11 132 av_log(s, AV_LOG_DEBUG, "%8d %"PRIu32" (%"PRIu32" bytes)",
yading@11 133 i, ape_ctx->seektable[i],
yading@11 134 ape_ctx->seektable[i + 1] - ape_ctx->seektable[i]);
yading@11 135 if (ape_ctx->bittable)
yading@11 136 av_log(s, AV_LOG_DEBUG, " + %2d bits\n",
yading@11 137 ape_ctx->bittable[i]);
yading@11 138 av_log(s, AV_LOG_DEBUG, "\n");
yading@11 139 } else {
yading@11 140 av_log(s, AV_LOG_DEBUG, "%8d %"PRIu32"\n", i, ape_ctx->seektable[i]);
yading@11 141 }
yading@11 142 }
yading@11 143 }
yading@11 144
yading@11 145 av_log(s, AV_LOG_DEBUG, "\nFrames\n\n");
yading@11 146 for (i = 0; i < ape_ctx->totalframes; i++)
yading@11 147 av_log(s, AV_LOG_DEBUG, "%8d %8"PRId64" %8d (%d samples)\n", i,
yading@11 148 ape_ctx->frames[i].pos, ape_ctx->frames[i].size,
yading@11 149 ape_ctx->frames[i].nblocks);
yading@11 150
yading@11 151 av_log(s, AV_LOG_DEBUG, "\nCalculated information:\n\n");
yading@11 152 av_log(s, AV_LOG_DEBUG, "junklength = %"PRIu32"\n", ape_ctx->junklength);
yading@11 153 av_log(s, AV_LOG_DEBUG, "firstframe = %"PRIu32"\n", ape_ctx->firstframe);
yading@11 154 av_log(s, AV_LOG_DEBUG, "totalsamples = %"PRIu32"\n", ape_ctx->totalsamples);
yading@11 155 #endif
yading@11 156 }
yading@11 157
yading@11 158 static int ape_read_header(AVFormatContext * s)
yading@11 159 {
yading@11 160 AVIOContext *pb = s->pb;
yading@11 161 APEContext *ape = s->priv_data;
yading@11 162 AVStream *st;
yading@11 163 uint32_t tag;
yading@11 164 int i;
yading@11 165 int total_blocks, final_size = 0;
yading@11 166 int64_t pts, file_size;
yading@11 167
yading@11 168 /* Skip any leading junk such as id3v2 tags */
yading@11 169 ape->junklength = avio_tell(pb);
yading@11 170
yading@11 171 tag = avio_rl32(pb);
yading@11 172 if (tag != MKTAG('M', 'A', 'C', ' '))
yading@11 173 return AVERROR_INVALIDDATA;
yading@11 174
yading@11 175 ape->fileversion = avio_rl16(pb);
yading@11 176
yading@11 177 if (ape->fileversion < APE_MIN_VERSION || ape->fileversion > APE_MAX_VERSION) {
yading@11 178 av_log(s, AV_LOG_ERROR, "Unsupported file version - %d.%02d\n",
yading@11 179 ape->fileversion / 1000, (ape->fileversion % 1000) / 10);
yading@11 180 return AVERROR_PATCHWELCOME;
yading@11 181 }
yading@11 182
yading@11 183 if (ape->fileversion >= 3980) {
yading@11 184 ape->padding1 = avio_rl16(pb);
yading@11 185 ape->descriptorlength = avio_rl32(pb);
yading@11 186 ape->headerlength = avio_rl32(pb);
yading@11 187 ape->seektablelength = avio_rl32(pb);
yading@11 188 ape->wavheaderlength = avio_rl32(pb);
yading@11 189 ape->audiodatalength = avio_rl32(pb);
yading@11 190 ape->audiodatalength_high = avio_rl32(pb);
yading@11 191 ape->wavtaillength = avio_rl32(pb);
yading@11 192 avio_read(pb, ape->md5, 16);
yading@11 193
yading@11 194 /* Skip any unknown bytes at the end of the descriptor.
yading@11 195 This is for future compatibility */
yading@11 196 if (ape->descriptorlength > 52)
yading@11 197 avio_skip(pb, ape->descriptorlength - 52);
yading@11 198
yading@11 199 /* Read header data */
yading@11 200 ape->compressiontype = avio_rl16(pb);
yading@11 201 ape->formatflags = avio_rl16(pb);
yading@11 202 ape->blocksperframe = avio_rl32(pb);
yading@11 203 ape->finalframeblocks = avio_rl32(pb);
yading@11 204 ape->totalframes = avio_rl32(pb);
yading@11 205 ape->bps = avio_rl16(pb);
yading@11 206 ape->channels = avio_rl16(pb);
yading@11 207 ape->samplerate = avio_rl32(pb);
yading@11 208 } else {
yading@11 209 ape->descriptorlength = 0;
yading@11 210 ape->headerlength = 32;
yading@11 211
yading@11 212 ape->compressiontype = avio_rl16(pb);
yading@11 213 ape->formatflags = avio_rl16(pb);
yading@11 214 ape->channels = avio_rl16(pb);
yading@11 215 ape->samplerate = avio_rl32(pb);
yading@11 216 ape->wavheaderlength = avio_rl32(pb);
yading@11 217 ape->wavtaillength = avio_rl32(pb);
yading@11 218 ape->totalframes = avio_rl32(pb);
yading@11 219 ape->finalframeblocks = avio_rl32(pb);
yading@11 220
yading@11 221 if (ape->formatflags & MAC_FORMAT_FLAG_HAS_PEAK_LEVEL) {
yading@11 222 avio_skip(pb, 4); /* Skip the peak level */
yading@11 223 ape->headerlength += 4;
yading@11 224 }
yading@11 225
yading@11 226 if (ape->formatflags & MAC_FORMAT_FLAG_HAS_SEEK_ELEMENTS) {
yading@11 227 ape->seektablelength = avio_rl32(pb);
yading@11 228 ape->headerlength += 4;
yading@11 229 ape->seektablelength *= sizeof(int32_t);
yading@11 230 } else
yading@11 231 ape->seektablelength = ape->totalframes * sizeof(int32_t);
yading@11 232
yading@11 233 if (ape->formatflags & MAC_FORMAT_FLAG_8_BIT)
yading@11 234 ape->bps = 8;
yading@11 235 else if (ape->formatflags & MAC_FORMAT_FLAG_24_BIT)
yading@11 236 ape->bps = 24;
yading@11 237 else
yading@11 238 ape->bps = 16;
yading@11 239
yading@11 240 if (ape->fileversion >= 3950)
yading@11 241 ape->blocksperframe = 73728 * 4;
yading@11 242 else if (ape->fileversion >= 3900 || (ape->fileversion >= 3800 && ape->compressiontype >= 4000))
yading@11 243 ape->blocksperframe = 73728;
yading@11 244 else
yading@11 245 ape->blocksperframe = 9216;
yading@11 246
yading@11 247 /* Skip any stored wav header */
yading@11 248 if (!(ape->formatflags & MAC_FORMAT_FLAG_CREATE_WAV_HEADER))
yading@11 249 avio_skip(pb, ape->wavheaderlength);
yading@11 250 }
yading@11 251
yading@11 252 if(!ape->totalframes){
yading@11 253 av_log(s, AV_LOG_ERROR, "No frames in the file!\n");
yading@11 254 return AVERROR(EINVAL);
yading@11 255 }
yading@11 256 if(ape->totalframes > UINT_MAX / sizeof(APEFrame)){
yading@11 257 av_log(s, AV_LOG_ERROR, "Too many frames: %"PRIu32"\n",
yading@11 258 ape->totalframes);
yading@11 259 return AVERROR_INVALIDDATA;
yading@11 260 }
yading@11 261 if (ape->seektablelength && (ape->seektablelength / sizeof(*ape->seektable)) < ape->totalframes) {
yading@11 262 av_log(s, AV_LOG_ERROR,
yading@11 263 "Number of seek entries is less than number of frames: %zu vs. %"PRIu32"\n",
yading@11 264 ape->seektablelength / sizeof(*ape->seektable), ape->totalframes);
yading@11 265 return AVERROR_INVALIDDATA;
yading@11 266 }
yading@11 267 ape->frames = av_malloc(ape->totalframes * sizeof(APEFrame));
yading@11 268 if(!ape->frames)
yading@11 269 return AVERROR(ENOMEM);
yading@11 270 ape->firstframe = ape->junklength + ape->descriptorlength + ape->headerlength + ape->seektablelength + ape->wavheaderlength;
yading@11 271 if (ape->fileversion < 3810)
yading@11 272 ape->firstframe += ape->totalframes;
yading@11 273 ape->currentframe = 0;
yading@11 274
yading@11 275
yading@11 276 ape->totalsamples = ape->finalframeblocks;
yading@11 277 if (ape->totalframes > 1)
yading@11 278 ape->totalsamples += ape->blocksperframe * (ape->totalframes - 1);
yading@11 279
yading@11 280 if (ape->seektablelength > 0) {
yading@11 281 ape->seektable = av_malloc(ape->seektablelength);
yading@11 282 if (!ape->seektable)
yading@11 283 return AVERROR(ENOMEM);
yading@11 284 for (i = 0; i < ape->seektablelength / sizeof(uint32_t); i++)
yading@11 285 ape->seektable[i] = avio_rl32(pb);
yading@11 286 if (ape->fileversion < 3810) {
yading@11 287 ape->bittable = av_malloc(ape->totalframes);
yading@11 288 if (!ape->bittable)
yading@11 289 return AVERROR(ENOMEM);
yading@11 290 for (i = 0; i < ape->totalframes; i++)
yading@11 291 ape->bittable[i] = avio_r8(pb);
yading@11 292 }
yading@11 293 }else{
yading@11 294 av_log(s, AV_LOG_ERROR, "Missing seektable\n");
yading@11 295 return AVERROR_INVALIDDATA;
yading@11 296 }
yading@11 297
yading@11 298 ape->frames[0].pos = ape->firstframe;
yading@11 299 ape->frames[0].nblocks = ape->blocksperframe;
yading@11 300 ape->frames[0].skip = 0;
yading@11 301 for (i = 1; i < ape->totalframes; i++) {
yading@11 302 ape->frames[i].pos = ape->seektable[i] + ape->junklength;
yading@11 303 ape->frames[i].nblocks = ape->blocksperframe;
yading@11 304 ape->frames[i - 1].size = ape->frames[i].pos - ape->frames[i - 1].pos;
yading@11 305 ape->frames[i].skip = (ape->frames[i].pos - ape->frames[0].pos) & 3;
yading@11 306 }
yading@11 307 ape->frames[ape->totalframes - 1].nblocks = ape->finalframeblocks;
yading@11 308 /* calculate final packet size from total file size, if available */
yading@11 309 file_size = avio_size(pb);
yading@11 310 if (file_size > 0) {
yading@11 311 final_size = file_size - ape->frames[ape->totalframes - 1].pos -
yading@11 312 ape->wavtaillength;
yading@11 313 final_size -= final_size & 3;
yading@11 314 }
yading@11 315 if (file_size <= 0 || final_size <= 0)
yading@11 316 final_size = ape->finalframeblocks * 8;
yading@11 317 ape->frames[ape->totalframes - 1].size = final_size;
yading@11 318
yading@11 319 for (i = 0; i < ape->totalframes; i++) {
yading@11 320 if(ape->frames[i].skip){
yading@11 321 ape->frames[i].pos -= ape->frames[i].skip;
yading@11 322 ape->frames[i].size += ape->frames[i].skip;
yading@11 323 }
yading@11 324 ape->frames[i].size = (ape->frames[i].size + 3) & ~3;
yading@11 325 }
yading@11 326 if (ape->fileversion < 3810) {
yading@11 327 for (i = 0; i < ape->totalframes; i++) {
yading@11 328 if (i < ape->totalframes - 1 && ape->bittable[i + 1])
yading@11 329 ape->frames[i].size += 4;
yading@11 330 ape->frames[i].skip <<= 3;
yading@11 331 ape->frames[i].skip += ape->bittable[i];
yading@11 332 }
yading@11 333 }
yading@11 334
yading@11 335 ape_dumpinfo(s, ape);
yading@11 336
yading@11 337 av_log(s, AV_LOG_DEBUG, "Decoding file - v%d.%02d, compression level %"PRIu16"\n",
yading@11 338 ape->fileversion / 1000, (ape->fileversion % 1000) / 10,
yading@11 339 ape->compressiontype);
yading@11 340
yading@11 341 /* now we are ready: build format streams */
yading@11 342 st = avformat_new_stream(s, NULL);
yading@11 343 if (!st)
yading@11 344 return AVERROR(ENOMEM);
yading@11 345
yading@11 346 total_blocks = (ape->totalframes == 0) ? 0 : ((ape->totalframes - 1) * ape->blocksperframe) + ape->finalframeblocks;
yading@11 347
yading@11 348 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
yading@11 349 st->codec->codec_id = AV_CODEC_ID_APE;
yading@11 350 st->codec->codec_tag = MKTAG('A', 'P', 'E', ' ');
yading@11 351 st->codec->channels = ape->channels;
yading@11 352 st->codec->sample_rate = ape->samplerate;
yading@11 353 st->codec->bits_per_coded_sample = ape->bps;
yading@11 354
yading@11 355 st->nb_frames = ape->totalframes;
yading@11 356 st->start_time = 0;
yading@11 357 st->duration = total_blocks;
yading@11 358 avpriv_set_pts_info(st, 64, 1, ape->samplerate);
yading@11 359
yading@11 360 st->codec->extradata = av_malloc(APE_EXTRADATA_SIZE);
yading@11 361 st->codec->extradata_size = APE_EXTRADATA_SIZE;
yading@11 362 AV_WL16(st->codec->extradata + 0, ape->fileversion);
yading@11 363 AV_WL16(st->codec->extradata + 2, ape->compressiontype);
yading@11 364 AV_WL16(st->codec->extradata + 4, ape->formatflags);
yading@11 365
yading@11 366 pts = 0;
yading@11 367 for (i = 0; i < ape->totalframes; i++) {
yading@11 368 ape->frames[i].pts = pts;
yading@11 369 av_add_index_entry(st, ape->frames[i].pos, ape->frames[i].pts, 0, 0, AVINDEX_KEYFRAME);
yading@11 370 pts += ape->blocksperframe;
yading@11 371 }
yading@11 372
yading@11 373 /* try to read APE tags */
yading@11 374 if (pb->seekable) {
yading@11 375 ff_ape_parse_tag(s);
yading@11 376 avio_seek(pb, 0, SEEK_SET);
yading@11 377 }
yading@11 378
yading@11 379 return 0;
yading@11 380 }
yading@11 381
yading@11 382 static int ape_read_packet(AVFormatContext * s, AVPacket * pkt)
yading@11 383 {
yading@11 384 int ret;
yading@11 385 int nblocks;
yading@11 386 APEContext *ape = s->priv_data;
yading@11 387 uint32_t extra_size = 8;
yading@11 388
yading@11 389 if (url_feof(s->pb))
yading@11 390 return AVERROR_EOF;
yading@11 391 if (ape->currentframe >= ape->totalframes)
yading@11 392 return AVERROR_EOF;
yading@11 393
yading@11 394 if (avio_seek(s->pb, ape->frames[ape->currentframe].pos, SEEK_SET) < 0)
yading@11 395 return AVERROR(EIO);
yading@11 396
yading@11 397 /* Calculate how many blocks there are in this frame */
yading@11 398 if (ape->currentframe == (ape->totalframes - 1))
yading@11 399 nblocks = ape->finalframeblocks;
yading@11 400 else
yading@11 401 nblocks = ape->blocksperframe;
yading@11 402
yading@11 403 if (ape->frames[ape->currentframe].size <= 0 ||
yading@11 404 ape->frames[ape->currentframe].size > INT_MAX - extra_size) {
yading@11 405 av_log(s, AV_LOG_ERROR, "invalid packet size: %d\n",
yading@11 406 ape->frames[ape->currentframe].size);
yading@11 407 ape->currentframe++;
yading@11 408 return AVERROR(EIO);
yading@11 409 }
yading@11 410
yading@11 411 if (av_new_packet(pkt, ape->frames[ape->currentframe].size + extra_size) < 0)
yading@11 412 return AVERROR(ENOMEM);
yading@11 413
yading@11 414 AV_WL32(pkt->data , nblocks);
yading@11 415 AV_WL32(pkt->data + 4, ape->frames[ape->currentframe].skip);
yading@11 416 ret = avio_read(s->pb, pkt->data + extra_size, ape->frames[ape->currentframe].size);
yading@11 417
yading@11 418 pkt->pts = ape->frames[ape->currentframe].pts;
yading@11 419 pkt->stream_index = 0;
yading@11 420
yading@11 421 /* note: we need to modify the packet size here to handle the last
yading@11 422 packet */
yading@11 423 pkt->size = ret + extra_size;
yading@11 424
yading@11 425 ape->currentframe++;
yading@11 426
yading@11 427 return 0;
yading@11 428 }
yading@11 429
yading@11 430 static int ape_read_close(AVFormatContext * s)
yading@11 431 {
yading@11 432 APEContext *ape = s->priv_data;
yading@11 433
yading@11 434 av_freep(&ape->frames);
yading@11 435 av_freep(&ape->seektable);
yading@11 436 av_freep(&ape->bittable);
yading@11 437 return 0;
yading@11 438 }
yading@11 439
yading@11 440 static int ape_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
yading@11 441 {
yading@11 442 AVStream *st = s->streams[stream_index];
yading@11 443 APEContext *ape = s->priv_data;
yading@11 444 int index = av_index_search_timestamp(st, timestamp, flags);
yading@11 445
yading@11 446 if (index < 0)
yading@11 447 return -1;
yading@11 448
yading@11 449 if (avio_seek(s->pb, st->index_entries[index].pos, SEEK_SET) < 0)
yading@11 450 return -1;
yading@11 451 ape->currentframe = index;
yading@11 452 return 0;
yading@11 453 }
yading@11 454
yading@11 455 AVInputFormat ff_ape_demuxer = {
yading@11 456 .name = "ape",
yading@11 457 .long_name = NULL_IF_CONFIG_SMALL("Monkey's Audio"),
yading@11 458 .priv_data_size = sizeof(APEContext),
yading@11 459 .read_probe = ape_probe,
yading@11 460 .read_header = ape_read_header,
yading@11 461 .read_packet = ape_read_packet,
yading@11 462 .read_close = ape_read_close,
yading@11 463 .read_seek = ape_read_seek,
yading@11 464 .extensions = "ape,apl,mac",
yading@11 465 };