/libavformat/ape.c

http://github.com/FFmpeg/FFmpeg · C · 473 lines · 369 code · 67 blank · 37 comment · 71 complexity · f21a4043c9c1af7999fa469ebf9235c6 MD5 · raw file

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