You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

393 lines
14KB

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