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.

496 lines
17KB

  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 "libavutil/intreadwrite.h"
  24. #include "avformat.h"
  25. #define ENABLE_DEBUG 0
  26. /* The earliest and latest file formats supported by this library */
  27. #define APE_MIN_VERSION 3950
  28. #define APE_MAX_VERSION 3990
  29. #define MAC_FORMAT_FLAG_8_BIT 1 // is 8-bit [OBSOLETE]
  30. #define MAC_FORMAT_FLAG_CRC 2 // uses the new CRC32 error detection [OBSOLETE]
  31. #define MAC_FORMAT_FLAG_HAS_PEAK_LEVEL 4 // uint32 nPeakLevel after the header [OBSOLETE]
  32. #define MAC_FORMAT_FLAG_24_BIT 8 // is 24-bit [OBSOLETE]
  33. #define MAC_FORMAT_FLAG_HAS_SEEK_ELEMENTS 16 // has the number of seek elements after the peak level
  34. #define MAC_FORMAT_FLAG_CREATE_WAV_HEADER 32 // create the wave header on decompression (not stored)
  35. #define MAC_SUBFRAME_SIZE 4608
  36. #define APE_EXTRADATA_SIZE 6
  37. /* APE tags */
  38. #define APE_TAG_VERSION 2000
  39. #define APE_TAG_FOOTER_BYTES 32
  40. #define APE_TAG_FLAG_CONTAINS_HEADER (1 << 31)
  41. #define APE_TAG_FLAG_IS_HEADER (1 << 29)
  42. typedef struct {
  43. int64_t pos;
  44. int nblocks;
  45. int size;
  46. int skip;
  47. int64_t pts;
  48. } APEFrame;
  49. typedef struct {
  50. /* Derived fields */
  51. uint32_t junklength;
  52. uint32_t firstframe;
  53. uint32_t totalsamples;
  54. int currentframe;
  55. APEFrame *frames;
  56. /* Info from Descriptor Block */
  57. char magic[4];
  58. int16_t fileversion;
  59. int16_t padding1;
  60. uint32_t descriptorlength;
  61. uint32_t headerlength;
  62. uint32_t seektablelength;
  63. uint32_t wavheaderlength;
  64. uint32_t audiodatalength;
  65. uint32_t audiodatalength_high;
  66. uint32_t wavtaillength;
  67. uint8_t md5[16];
  68. /* Info from Header Block */
  69. uint16_t compressiontype;
  70. uint16_t formatflags;
  71. uint32_t blocksperframe;
  72. uint32_t finalframeblocks;
  73. uint32_t totalframes;
  74. uint16_t bps;
  75. uint16_t channels;
  76. uint32_t samplerate;
  77. /* Seektable */
  78. uint32_t *seektable;
  79. } APEContext;
  80. static void ape_tag_read_field(AVFormatContext *s)
  81. {
  82. ByteIOContext *pb = s->pb;
  83. uint8_t key[1024], value[1024];
  84. uint32_t size;
  85. int i, l;
  86. size = get_le32(pb); /* field size */
  87. url_fskip(pb, 4); /* skip field flags */
  88. for (i=0; pb->buf_ptr[i]!='0' && pb->buf_ptr[i]>=0x20 && pb->buf_ptr[i]<=0x7E; i++);
  89. l = FFMIN(i, sizeof(key) -1);
  90. get_buffer(pb, key, l);
  91. key[l] = 0;
  92. url_fskip(pb, 1 + i-l);
  93. l = FFMIN(size, sizeof(value)-1);
  94. get_buffer(pb, value, l);
  95. value[l] = 0;
  96. url_fskip(pb, size-l);
  97. av_metadata_set(&s->metadata, key, value);
  98. }
  99. static void ape_parse_tag(AVFormatContext *s)
  100. {
  101. ByteIOContext *pb = s->pb;
  102. int file_size = url_fsize(pb);
  103. uint32_t val, fields, tag_bytes;
  104. uint8_t buf[8];
  105. int i;
  106. if (file_size < APE_TAG_FOOTER_BYTES)
  107. return;
  108. url_fseek(pb, file_size - APE_TAG_FOOTER_BYTES, SEEK_SET);
  109. get_buffer(pb, buf, 8); /* APETAGEX */
  110. if (strncmp(buf, "APETAGEX", 8)) {
  111. return;
  112. }
  113. val = get_le32(pb); /* APE tag version */
  114. if (val > APE_TAG_VERSION) {
  115. av_log(s, AV_LOG_ERROR, "Unsupported tag version. (>=%d)\n", APE_TAG_VERSION);
  116. return;
  117. }
  118. tag_bytes = get_le32(pb); /* tag size */
  119. if (tag_bytes - APE_TAG_FOOTER_BYTES > (1024 * 1024 * 16)) {
  120. av_log(s, AV_LOG_ERROR, "Tag size is way too big\n");
  121. return;
  122. }
  123. fields = get_le32(pb); /* number of fields */
  124. if (fields > 65536) {
  125. av_log(s, AV_LOG_ERROR, "Too many tag fields (%d)\n", fields);
  126. return;
  127. }
  128. val = get_le32(pb); /* flags */
  129. if (val & APE_TAG_FLAG_IS_HEADER) {
  130. av_log(s, AV_LOG_ERROR, "APE Tag is a header\n");
  131. return;
  132. }
  133. if (val & APE_TAG_FLAG_CONTAINS_HEADER)
  134. tag_bytes += 2*APE_TAG_FOOTER_BYTES;
  135. url_fseek(pb, file_size - tag_bytes, SEEK_SET);
  136. for (i=0; i<fields; i++)
  137. ape_tag_read_field(s);
  138. #if ENABLE_DEBUG
  139. av_log(s, AV_LOG_DEBUG, "\nAPE Tags:\n\n");
  140. av_log(s, AV_LOG_DEBUG, "title = %s\n", s->title);
  141. av_log(s, AV_LOG_DEBUG, "author = %s\n", s->author);
  142. av_log(s, AV_LOG_DEBUG, "copyright = %s\n", s->copyright);
  143. av_log(s, AV_LOG_DEBUG, "comment = %s\n", s->comment);
  144. av_log(s, AV_LOG_DEBUG, "album = %s\n", s->album);
  145. av_log(s, AV_LOG_DEBUG, "year = %d\n", s->year);
  146. av_log(s, AV_LOG_DEBUG, "track = %d\n", s->track);
  147. av_log(s, AV_LOG_DEBUG, "genre = %s\n", s->genre);
  148. #endif
  149. }
  150. static int ape_probe(AVProbeData * p)
  151. {
  152. if (p->buf[0] == 'M' && p->buf[1] == 'A' && p->buf[2] == 'C' && p->buf[3] == ' ')
  153. return AVPROBE_SCORE_MAX;
  154. return 0;
  155. }
  156. static void ape_dumpinfo(AVFormatContext * s, APEContext * ape_ctx)
  157. {
  158. #if ENABLE_DEBUG
  159. int i;
  160. av_log(s, AV_LOG_DEBUG, "Descriptor Block:\n\n");
  161. 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]);
  162. av_log(s, AV_LOG_DEBUG, "fileversion = %d\n", ape_ctx->fileversion);
  163. av_log(s, AV_LOG_DEBUG, "descriptorlength = %d\n", ape_ctx->descriptorlength);
  164. av_log(s, AV_LOG_DEBUG, "headerlength = %d\n", ape_ctx->headerlength);
  165. av_log(s, AV_LOG_DEBUG, "seektablelength = %d\n", ape_ctx->seektablelength);
  166. av_log(s, AV_LOG_DEBUG, "wavheaderlength = %d\n", ape_ctx->wavheaderlength);
  167. av_log(s, AV_LOG_DEBUG, "audiodatalength = %d\n", ape_ctx->audiodatalength);
  168. av_log(s, AV_LOG_DEBUG, "audiodatalength_high = %d\n", ape_ctx->audiodatalength_high);
  169. av_log(s, AV_LOG_DEBUG, "wavtaillength = %d\n", ape_ctx->wavtaillength);
  170. av_log(s, AV_LOG_DEBUG, "md5 = ");
  171. for (i = 0; i < 16; i++)
  172. av_log(s, AV_LOG_DEBUG, "%02x", ape_ctx->md5[i]);
  173. av_log(s, AV_LOG_DEBUG, "\n");
  174. av_log(s, AV_LOG_DEBUG, "\nHeader Block:\n\n");
  175. av_log(s, AV_LOG_DEBUG, "compressiontype = %d\n", ape_ctx->compressiontype);
  176. av_log(s, AV_LOG_DEBUG, "formatflags = %d\n", ape_ctx->formatflags);
  177. av_log(s, AV_LOG_DEBUG, "blocksperframe = %d\n", ape_ctx->blocksperframe);
  178. av_log(s, AV_LOG_DEBUG, "finalframeblocks = %d\n", ape_ctx->finalframeblocks);
  179. av_log(s, AV_LOG_DEBUG, "totalframes = %d\n", ape_ctx->totalframes);
  180. av_log(s, AV_LOG_DEBUG, "bps = %d\n", ape_ctx->bps);
  181. av_log(s, AV_LOG_DEBUG, "channels = %d\n", ape_ctx->channels);
  182. av_log(s, AV_LOG_DEBUG, "samplerate = %d\n", ape_ctx->samplerate);
  183. av_log(s, AV_LOG_DEBUG, "\nSeektable\n\n");
  184. if ((ape_ctx->seektablelength / sizeof(uint32_t)) != ape_ctx->totalframes) {
  185. av_log(s, AV_LOG_DEBUG, "No seektable\n");
  186. } else {
  187. for (i = 0; i < ape_ctx->seektablelength / sizeof(uint32_t); i++) {
  188. if (i < ape_ctx->totalframes - 1) {
  189. 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]);
  190. } else {
  191. av_log(s, AV_LOG_DEBUG, "%8d %d\n", i, ape_ctx->seektable[i]);
  192. }
  193. }
  194. }
  195. av_log(s, AV_LOG_DEBUG, "\nFrames\n\n");
  196. for (i = 0; i < ape_ctx->totalframes; i++)
  197. 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);
  198. av_log(s, AV_LOG_DEBUG, "\nCalculated information:\n\n");
  199. av_log(s, AV_LOG_DEBUG, "junklength = %d\n", ape_ctx->junklength);
  200. av_log(s, AV_LOG_DEBUG, "firstframe = %d\n", ape_ctx->firstframe);
  201. av_log(s, AV_LOG_DEBUG, "totalsamples = %d\n", ape_ctx->totalsamples);
  202. #endif
  203. }
  204. static int ape_read_header(AVFormatContext * s, AVFormatParameters * ap)
  205. {
  206. ByteIOContext *pb = s->pb;
  207. APEContext *ape = s->priv_data;
  208. AVStream *st;
  209. uint32_t tag;
  210. int i;
  211. int total_blocks;
  212. int64_t pts;
  213. /* TODO: Skip any leading junk such as id3v2 tags */
  214. ape->junklength = 0;
  215. tag = get_le32(pb);
  216. if (tag != MKTAG('M', 'A', 'C', ' '))
  217. return -1;
  218. ape->fileversion = get_le16(pb);
  219. if (ape->fileversion < APE_MIN_VERSION || ape->fileversion > APE_MAX_VERSION) {
  220. av_log(s, AV_LOG_ERROR, "Unsupported file version - %d.%02d\n", ape->fileversion / 1000, (ape->fileversion % 1000) / 10);
  221. return -1;
  222. }
  223. if (ape->fileversion >= 3980) {
  224. ape->padding1 = get_le16(pb);
  225. ape->descriptorlength = get_le32(pb);
  226. ape->headerlength = get_le32(pb);
  227. ape->seektablelength = get_le32(pb);
  228. ape->wavheaderlength = get_le32(pb);
  229. ape->audiodatalength = get_le32(pb);
  230. ape->audiodatalength_high = get_le32(pb);
  231. ape->wavtaillength = get_le32(pb);
  232. get_buffer(pb, ape->md5, 16);
  233. /* Skip any unknown bytes at the end of the descriptor.
  234. This is for future compatibility */
  235. if (ape->descriptorlength > 52)
  236. url_fseek(pb, ape->descriptorlength - 52, SEEK_CUR);
  237. /* Read header data */
  238. ape->compressiontype = get_le16(pb);
  239. ape->formatflags = get_le16(pb);
  240. ape->blocksperframe = get_le32(pb);
  241. ape->finalframeblocks = get_le32(pb);
  242. ape->totalframes = get_le32(pb);
  243. ape->bps = get_le16(pb);
  244. ape->channels = get_le16(pb);
  245. ape->samplerate = get_le32(pb);
  246. } else {
  247. ape->descriptorlength = 0;
  248. ape->headerlength = 32;
  249. ape->compressiontype = get_le16(pb);
  250. ape->formatflags = get_le16(pb);
  251. ape->channels = get_le16(pb);
  252. ape->samplerate = get_le32(pb);
  253. ape->wavheaderlength = get_le32(pb);
  254. ape->wavtaillength = get_le32(pb);
  255. ape->totalframes = get_le32(pb);
  256. ape->finalframeblocks = get_le32(pb);
  257. if (ape->formatflags & MAC_FORMAT_FLAG_HAS_PEAK_LEVEL) {
  258. url_fseek(pb, 4, SEEK_CUR); /* Skip the peak level */
  259. ape->headerlength += 4;
  260. }
  261. if (ape->formatflags & MAC_FORMAT_FLAG_HAS_SEEK_ELEMENTS) {
  262. ape->seektablelength = get_le32(pb);
  263. ape->headerlength += 4;
  264. ape->seektablelength *= sizeof(int32_t);
  265. } else
  266. ape->seektablelength = ape->totalframes * sizeof(int32_t);
  267. if (ape->formatflags & MAC_FORMAT_FLAG_8_BIT)
  268. ape->bps = 8;
  269. else if (ape->formatflags & MAC_FORMAT_FLAG_24_BIT)
  270. ape->bps = 24;
  271. else
  272. ape->bps = 16;
  273. if (ape->fileversion >= 3950)
  274. ape->blocksperframe = 73728 * 4;
  275. else if (ape->fileversion >= 3900 || (ape->fileversion >= 3800 && ape->compressiontype >= 4000))
  276. ape->blocksperframe = 73728;
  277. else
  278. ape->blocksperframe = 9216;
  279. /* Skip any stored wav header */
  280. if (!(ape->formatflags & MAC_FORMAT_FLAG_CREATE_WAV_HEADER))
  281. url_fskip(pb, ape->wavheaderlength);
  282. }
  283. if(ape->totalframes > UINT_MAX / sizeof(APEFrame)){
  284. av_log(s, AV_LOG_ERROR, "Too many frames: %d\n", ape->totalframes);
  285. return -1;
  286. }
  287. ape->frames = av_malloc(ape->totalframes * sizeof(APEFrame));
  288. if(!ape->frames)
  289. return AVERROR_NOMEM;
  290. ape->firstframe = ape->junklength + ape->descriptorlength + ape->headerlength + ape->seektablelength + ape->wavheaderlength;
  291. ape->currentframe = 0;
  292. ape->totalsamples = ape->finalframeblocks;
  293. if (ape->totalframes > 1)
  294. ape->totalsamples += ape->blocksperframe * (ape->totalframes - 1);
  295. if (ape->seektablelength > 0) {
  296. ape->seektable = av_malloc(ape->seektablelength);
  297. for (i = 0; i < ape->seektablelength / sizeof(uint32_t); i++)
  298. ape->seektable[i] = get_le32(pb);
  299. }
  300. ape->frames[0].pos = ape->firstframe;
  301. ape->frames[0].nblocks = ape->blocksperframe;
  302. ape->frames[0].skip = 0;
  303. for (i = 1; i < ape->totalframes; i++) {
  304. ape->frames[i].pos = ape->seektable[i]; //ape->frames[i-1].pos + ape->blocksperframe;
  305. ape->frames[i].nblocks = ape->blocksperframe;
  306. ape->frames[i - 1].size = ape->frames[i].pos - ape->frames[i - 1].pos;
  307. ape->frames[i].skip = (ape->frames[i].pos - ape->frames[0].pos) & 3;
  308. }
  309. ape->frames[ape->totalframes - 1].size = ape->finalframeblocks * 4;
  310. ape->frames[ape->totalframes - 1].nblocks = ape->finalframeblocks;
  311. for (i = 0; i < ape->totalframes; i++) {
  312. if(ape->frames[i].skip){
  313. ape->frames[i].pos -= ape->frames[i].skip;
  314. ape->frames[i].size += ape->frames[i].skip;
  315. }
  316. ape->frames[i].size = (ape->frames[i].size + 3) & ~3;
  317. }
  318. ape_dumpinfo(s, ape);
  319. /* try to read APE tags */
  320. if (!url_is_streamed(pb)) {
  321. ape_parse_tag(s);
  322. url_fseek(pb, 0, SEEK_SET);
  323. }
  324. av_log(s, AV_LOG_DEBUG, "Decoding file - v%d.%02d, compression level %d\n", ape->fileversion / 1000, (ape->fileversion % 1000) / 10, ape->compressiontype);
  325. /* now we are ready: build format streams */
  326. st = av_new_stream(s, 0);
  327. if (!st)
  328. return -1;
  329. total_blocks = (ape->totalframes == 0) ? 0 : ((ape->totalframes - 1) * ape->blocksperframe) + ape->finalframeblocks;
  330. st->codec->codec_type = CODEC_TYPE_AUDIO;
  331. st->codec->codec_id = CODEC_ID_APE;
  332. st->codec->codec_tag = MKTAG('A', 'P', 'E', ' ');
  333. st->codec->channels = ape->channels;
  334. st->codec->sample_rate = ape->samplerate;
  335. st->codec->bits_per_coded_sample = ape->bps;
  336. st->codec->frame_size = MAC_SUBFRAME_SIZE;
  337. st->nb_frames = ape->totalframes;
  338. s->start_time = 0;
  339. s->duration = (int64_t) total_blocks * AV_TIME_BASE / ape->samplerate;
  340. av_set_pts_info(st, 64, MAC_SUBFRAME_SIZE, ape->samplerate);
  341. st->codec->extradata = av_malloc(APE_EXTRADATA_SIZE);
  342. st->codec->extradata_size = APE_EXTRADATA_SIZE;
  343. AV_WL16(st->codec->extradata + 0, ape->fileversion);
  344. AV_WL16(st->codec->extradata + 2, ape->compressiontype);
  345. AV_WL16(st->codec->extradata + 4, ape->formatflags);
  346. pts = 0;
  347. for (i = 0; i < ape->totalframes; i++) {
  348. ape->frames[i].pts = pts;
  349. av_add_index_entry(st, ape->frames[i].pos, ape->frames[i].pts, 0, 0, AVINDEX_KEYFRAME);
  350. pts += ape->blocksperframe / MAC_SUBFRAME_SIZE;
  351. }
  352. return 0;
  353. }
  354. static int ape_read_packet(AVFormatContext * s, AVPacket * pkt)
  355. {
  356. int ret;
  357. int nblocks;
  358. APEContext *ape = s->priv_data;
  359. uint32_t extra_size = 8;
  360. if (url_feof(s->pb))
  361. return AVERROR_IO;
  362. if (ape->currentframe > ape->totalframes)
  363. return AVERROR_IO;
  364. url_fseek (s->pb, ape->frames[ape->currentframe].pos, SEEK_SET);
  365. /* Calculate how many blocks there are in this frame */
  366. if (ape->currentframe == (ape->totalframes - 1))
  367. nblocks = ape->finalframeblocks;
  368. else
  369. nblocks = ape->blocksperframe;
  370. if (av_new_packet(pkt, ape->frames[ape->currentframe].size + extra_size) < 0)
  371. return AVERROR_NOMEM;
  372. AV_WL32(pkt->data , nblocks);
  373. AV_WL32(pkt->data + 4, ape->frames[ape->currentframe].skip);
  374. ret = get_buffer(s->pb, pkt->data + extra_size, ape->frames[ape->currentframe].size);
  375. pkt->pts = ape->frames[ape->currentframe].pts;
  376. pkt->stream_index = 0;
  377. /* note: we need to modify the packet size here to handle the last
  378. packet */
  379. pkt->size = ret + extra_size;
  380. ape->currentframe++;
  381. return 0;
  382. }
  383. static int ape_read_close(AVFormatContext * s)
  384. {
  385. APEContext *ape = s->priv_data;
  386. av_freep(&ape->frames);
  387. av_freep(&ape->seektable);
  388. return 0;
  389. }
  390. static int ape_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
  391. {
  392. AVStream *st = s->streams[stream_index];
  393. APEContext *ape = s->priv_data;
  394. int index = av_index_search_timestamp(st, timestamp, flags);
  395. if (index < 0)
  396. return -1;
  397. ape->currentframe = index;
  398. return 0;
  399. }
  400. AVInputFormat ape_demuxer = {
  401. "ape",
  402. NULL_IF_CONFIG_SMALL("Monkey's Audio"),
  403. sizeof(APEContext),
  404. ape_probe,
  405. ape_read_header,
  406. ape_read_packet,
  407. ape_read_close,
  408. ape_read_seek,
  409. .extensions = "ape,apl,mac"
  410. };