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.

524 lines
18KB

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