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.

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