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.

531 lines
15KB

  1. /*
  2. * WAV demuxer
  3. * Copyright (c) 2001, 2002 Fabrice Bellard
  4. *
  5. * Sony Wave64 demuxer
  6. * RF64 demuxer
  7. * Copyright (c) 2009 Daniel Verkamp
  8. *
  9. * This file is part of Libav.
  10. *
  11. * Libav is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU Lesser General Public
  13. * License as published by the Free Software Foundation; either
  14. * version 2.1 of the License, or (at your option) any later version.
  15. *
  16. * Libav is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * Lesser General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Lesser General Public
  22. * License along with Libav; if not, write to the Free Software
  23. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  24. */
  25. #include "libavutil/avassert.h"
  26. #include "libavutil/dict.h"
  27. #include "libavutil/log.h"
  28. #include "libavutil/mathematics.h"
  29. #include "libavutil/opt.h"
  30. #include "avformat.h"
  31. #include "avio.h"
  32. #include "avio_internal.h"
  33. #include "internal.h"
  34. #include "metadata.h"
  35. #include "pcm.h"
  36. #include "riff.h"
  37. typedef struct WAVDemuxContext {
  38. int64_t data_end;
  39. int w64;
  40. } WAVDemuxContext;
  41. #if CONFIG_WAV_DEMUXER
  42. static int64_t next_tag(AVIOContext *pb, uint32_t *tag)
  43. {
  44. *tag = avio_rl32(pb);
  45. return avio_rl32(pb);
  46. }
  47. /* RIFF chunks are always on a even offset. */
  48. static int64_t wav_seek_tag(AVIOContext *s, int64_t offset, int whence)
  49. {
  50. return avio_seek(s, offset + (offset & 1), whence);
  51. }
  52. /* return the size of the found tag */
  53. static int64_t find_tag(AVIOContext *pb, uint32_t tag1)
  54. {
  55. unsigned int tag;
  56. int64_t size;
  57. for (;;) {
  58. if (pb->eof_reached)
  59. return -1;
  60. size = next_tag(pb, &tag);
  61. if (tag == tag1)
  62. break;
  63. wav_seek_tag(pb, size, SEEK_CUR);
  64. }
  65. return size;
  66. }
  67. static int wav_probe(AVProbeData *p)
  68. {
  69. /* check file header */
  70. if (p->buf_size <= 32)
  71. return 0;
  72. if (!memcmp(p->buf + 8, "WAVE", 4)) {
  73. if (!memcmp(p->buf, "RIFF", 4))
  74. /* Since the ACT demuxer has a standard WAV header at the top of
  75. * its own, the returned score is decreased to avoid a probe
  76. * conflict between ACT and WAV. */
  77. return AVPROBE_SCORE_MAX - 1;
  78. else if (!memcmp(p->buf, "RF64", 4) &&
  79. !memcmp(p->buf + 12, "ds64", 4))
  80. return AVPROBE_SCORE_MAX;
  81. }
  82. return 0;
  83. }
  84. static int wav_parse_fmt_tag(AVFormatContext *s, int64_t size, AVStream **st)
  85. {
  86. AVIOContext *pb = s->pb;
  87. int ret;
  88. /* parse fmt header */
  89. *st = avformat_new_stream(s, NULL);
  90. if (!*st)
  91. return AVERROR(ENOMEM);
  92. ret = ff_get_wav_header(pb, (*st)->codec, size);
  93. if (ret < 0)
  94. return ret;
  95. (*st)->need_parsing = AVSTREAM_PARSE_FULL;
  96. avpriv_set_pts_info(*st, 64, 1, (*st)->codec->sample_rate);
  97. return 0;
  98. }
  99. static inline int wav_parse_bext_string(AVFormatContext *s, const char *key,
  100. int length)
  101. {
  102. char temp[257];
  103. int ret;
  104. av_assert0(length <= sizeof(temp));
  105. if ((ret = avio_read(s->pb, temp, length)) < 0)
  106. return ret;
  107. temp[length] = 0;
  108. if (strlen(temp))
  109. return av_dict_set(&s->metadata, key, temp, 0);
  110. return 0;
  111. }
  112. static int wav_parse_bext_tag(AVFormatContext *s, int64_t size)
  113. {
  114. char temp[131], *coding_history;
  115. int ret, x;
  116. uint64_t time_reference;
  117. int64_t umid_parts[8], umid_mask = 0;
  118. if ((ret = wav_parse_bext_string(s, "description", 256)) < 0 ||
  119. (ret = wav_parse_bext_string(s, "originator", 32)) < 0 ||
  120. (ret = wav_parse_bext_string(s, "originator_reference", 32)) < 0 ||
  121. (ret = wav_parse_bext_string(s, "origination_date", 10)) < 0 ||
  122. (ret = wav_parse_bext_string(s, "origination_time", 8)) < 0)
  123. return ret;
  124. time_reference = avio_rl64(s->pb);
  125. snprintf(temp, sizeof(temp), "%"PRIu64, time_reference);
  126. if ((ret = av_dict_set(&s->metadata, "time_reference", temp, 0)) < 0)
  127. return ret;
  128. /* check if version is >= 1, in which case an UMID may be present */
  129. if (avio_rl16(s->pb) >= 1) {
  130. for (x = 0; x < 8; x++)
  131. umid_mask |= umid_parts[x] = avio_rb64(s->pb);
  132. if (umid_mask) {
  133. /* the string formatting below is per SMPTE 330M-2004 Annex C */
  134. if (umid_parts[4] == 0 && umid_parts[5] == 0 &&
  135. umid_parts[6] == 0 && umid_parts[7] == 0) {
  136. /* basic UMID */
  137. snprintf(temp, sizeof(temp),
  138. "0x%016"PRIX64"%016"PRIX64"%016"PRIX64"%016"PRIX64,
  139. umid_parts[0], umid_parts[1],
  140. umid_parts[2], umid_parts[3]);
  141. } else {
  142. /* extended UMID */
  143. snprintf(temp, sizeof(temp),
  144. "0x%016"PRIX64"%016"PRIX64"%016"PRIX64"%016"PRIX64
  145. "0x%016"PRIX64"%016"PRIX64"%016"PRIX64"%016"PRIX64,
  146. umid_parts[0], umid_parts[1],
  147. umid_parts[2], umid_parts[3],
  148. umid_parts[4], umid_parts[5],
  149. umid_parts[6], umid_parts[7]);
  150. }
  151. if ((ret = av_dict_set(&s->metadata, "umid", temp, 0)) < 0)
  152. return ret;
  153. }
  154. avio_skip(s->pb, 190);
  155. } else
  156. avio_skip(s->pb, 254);
  157. if (size > 602) {
  158. /* CodingHistory present */
  159. size -= 602;
  160. if (!(coding_history = av_malloc(size + 1)))
  161. return AVERROR(ENOMEM);
  162. if ((ret = avio_read(s->pb, coding_history, size)) < 0)
  163. return ret;
  164. coding_history[size] = 0;
  165. if ((ret = av_dict_set(&s->metadata, "coding_history", coding_history,
  166. AV_DICT_DONT_STRDUP_VAL)) < 0)
  167. return ret;
  168. }
  169. return 0;
  170. }
  171. static const AVMetadataConv wav_metadata_conv[] = {
  172. { "description", "comment" },
  173. { "originator", "encoded_by" },
  174. { "origination_date", "date" },
  175. { "origination_time", "creation_time" },
  176. { 0 },
  177. };
  178. /* wav input */
  179. static int wav_read_header(AVFormatContext *s)
  180. {
  181. int64_t size, av_uninit(data_size);
  182. int64_t sample_count = 0;
  183. int rf64;
  184. uint32_t tag;
  185. AVIOContext *pb = s->pb;
  186. AVStream *st = NULL;
  187. WAVDemuxContext *wav = s->priv_data;
  188. int ret, got_fmt = 0;
  189. int64_t next_tag_ofs, data_ofs = -1;
  190. /* check RIFF header */
  191. tag = avio_rl32(pb);
  192. rf64 = tag == MKTAG('R', 'F', '6', '4');
  193. if (!rf64 && tag != MKTAG('R', 'I', 'F', 'F'))
  194. return AVERROR_INVALIDDATA;
  195. avio_rl32(pb); /* file size */
  196. tag = avio_rl32(pb);
  197. if (tag != MKTAG('W', 'A', 'V', 'E'))
  198. return AVERROR_INVALIDDATA;
  199. if (rf64) {
  200. if (avio_rl32(pb) != MKTAG('d', 's', '6', '4'))
  201. return AVERROR_INVALIDDATA;
  202. size = avio_rl32(pb);
  203. if (size < 16)
  204. return AVERROR_INVALIDDATA;
  205. avio_rl64(pb); /* RIFF size */
  206. data_size = avio_rl64(pb);
  207. sample_count = avio_rl64(pb);
  208. if (data_size < 0 || sample_count < 0) {
  209. av_log(s, AV_LOG_ERROR, "negative data_size and/or sample_count in "
  210. "ds64: data_size = %"PRId64", sample_count = %"PRId64"\n",
  211. data_size, sample_count);
  212. return AVERROR_INVALIDDATA;
  213. }
  214. avio_skip(pb, size - 16); /* skip rest of ds64 chunk */
  215. }
  216. for (;;) {
  217. size = next_tag(pb, &tag);
  218. next_tag_ofs = avio_tell(pb) + size;
  219. if (pb->eof_reached)
  220. break;
  221. switch (tag) {
  222. case MKTAG('f', 'm', 't', ' '):
  223. /* only parse the first 'fmt ' tag found */
  224. if (!got_fmt && (ret = wav_parse_fmt_tag(s, size, &st) < 0)) {
  225. return ret;
  226. } else if (got_fmt)
  227. av_log(s, AV_LOG_WARNING, "found more than one 'fmt ' tag\n");
  228. got_fmt = 1;
  229. break;
  230. case MKTAG('d', 'a', 't', 'a'):
  231. if (!got_fmt) {
  232. av_log(s, AV_LOG_ERROR,
  233. "found no 'fmt ' tag before the 'data' tag\n");
  234. return AVERROR_INVALIDDATA;
  235. }
  236. if (rf64) {
  237. next_tag_ofs = wav->data_end = avio_tell(pb) + data_size;
  238. } else {
  239. data_size = size;
  240. next_tag_ofs = wav->data_end = size ? next_tag_ofs : INT64_MAX;
  241. }
  242. data_ofs = avio_tell(pb);
  243. /* don't look for footer metadata if we can't seek or if we don't
  244. * know where the data tag ends
  245. */
  246. if (!pb->seekable || (!rf64 && !size))
  247. goto break_loop;
  248. break;
  249. case MKTAG('f', 'a', 'c', 't'):
  250. if (!sample_count)
  251. sample_count = avio_rl32(pb);
  252. break;
  253. case MKTAG('b', 'e', 'x', 't'):
  254. if ((ret = wav_parse_bext_tag(s, size)) < 0)
  255. return ret;
  256. break;
  257. case MKTAG('L', 'I', 'S', 'T'):
  258. if (size < 4) {
  259. av_log(s, AV_LOG_ERROR, "too short LIST");
  260. return AVERROR_INVALIDDATA;
  261. }
  262. switch (avio_rl32(pb)) {
  263. case MKTAG('I', 'N', 'F', 'O'):
  264. if ((ret = ff_read_riff_info(s, size - 4)) < 0)
  265. return ret;
  266. }
  267. break;
  268. }
  269. /* seek to next tag unless we know that we'll run into EOF */
  270. if ((avio_size(pb) > 0 && next_tag_ofs >= avio_size(pb)) ||
  271. wav_seek_tag(pb, next_tag_ofs, SEEK_SET) < 0) {
  272. break;
  273. }
  274. }
  275. break_loop:
  276. if (data_ofs < 0) {
  277. av_log(s, AV_LOG_ERROR, "no 'data' tag found\n");
  278. return AVERROR_INVALIDDATA;
  279. }
  280. avio_seek(pb, data_ofs, SEEK_SET);
  281. if (!sample_count && st->codec->channels &&
  282. av_get_bits_per_sample(st->codec->codec_id))
  283. sample_count = (data_size << 3) /
  284. (st->codec->channels *
  285. (uint64_t)av_get_bits_per_sample(st->codec->codec_id));
  286. if (sample_count)
  287. st->duration = sample_count;
  288. ff_metadata_conv_ctx(s, NULL, wav_metadata_conv);
  289. ff_metadata_conv_ctx(s, NULL, ff_riff_info_conv);
  290. return 0;
  291. }
  292. /**
  293. * Find chunk with w64 GUID by skipping over other chunks.
  294. * @return the size of the found chunk
  295. */
  296. static int64_t find_guid(AVIOContext *pb, const uint8_t guid1[16])
  297. {
  298. uint8_t guid[16];
  299. int64_t size;
  300. while (!pb->eof_reached) {
  301. avio_read(pb, guid, 16);
  302. size = avio_rl64(pb);
  303. if (size <= 24)
  304. return -1;
  305. if (!memcmp(guid, guid1, 16))
  306. return size;
  307. avio_skip(pb, FFALIGN(size, INT64_C(8)) - 24);
  308. }
  309. return -1;
  310. }
  311. static const uint8_t guid_data[16] = {
  312. 'd', 'a', 't', 'a',
  313. 0xF3, 0xAC, 0xD3, 0x11,0x8C, 0xD1, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A
  314. };
  315. #define MAX_SIZE 4096
  316. static int wav_read_packet(AVFormatContext *s, AVPacket *pkt)
  317. {
  318. int ret, size;
  319. int64_t left;
  320. AVStream *st;
  321. WAVDemuxContext *wav = s->priv_data;
  322. st = s->streams[0];
  323. left = wav->data_end - avio_tell(s->pb);
  324. if (left <= 0) {
  325. if (CONFIG_W64_DEMUXER && wav->w64)
  326. left = find_guid(s->pb, guid_data) - 24;
  327. else
  328. left = find_tag(s->pb, MKTAG('d', 'a', 't', 'a'));
  329. if (left < 0)
  330. return AVERROR_EOF;
  331. wav->data_end = avio_tell(s->pb) + left;
  332. }
  333. size = MAX_SIZE;
  334. if (st->codec->block_align > 1) {
  335. if (size < st->codec->block_align)
  336. size = st->codec->block_align;
  337. size = (size / st->codec->block_align) * st->codec->block_align;
  338. }
  339. size = FFMIN(size, left);
  340. ret = av_get_packet(s->pb, pkt, size);
  341. if (ret < 0)
  342. return ret;
  343. pkt->stream_index = 0;
  344. return ret;
  345. }
  346. static int wav_read_seek(AVFormatContext *s,
  347. int stream_index, int64_t timestamp, int flags)
  348. {
  349. AVStream *st;
  350. st = s->streams[0];
  351. switch (st->codec->codec_id) {
  352. case AV_CODEC_ID_MP2:
  353. case AV_CODEC_ID_MP3:
  354. case AV_CODEC_ID_AC3:
  355. case AV_CODEC_ID_DTS:
  356. /* use generic seeking with dynamically generated indexes */
  357. return -1;
  358. default:
  359. break;
  360. }
  361. return ff_pcm_read_seek(s, stream_index, timestamp, flags);
  362. }
  363. AVInputFormat ff_wav_demuxer = {
  364. .name = "wav",
  365. .long_name = NULL_IF_CONFIG_SMALL("WAV / WAVE (Waveform Audio)"),
  366. .priv_data_size = sizeof(WAVDemuxContext),
  367. .read_probe = wav_probe,
  368. .read_header = wav_read_header,
  369. .read_packet = wav_read_packet,
  370. .read_seek = wav_read_seek,
  371. .flags = AVFMT_GENERIC_INDEX,
  372. .codec_tag = (const AVCodecTag * const []) { ff_codec_wav_tags, 0 },
  373. };
  374. #endif /* CONFIG_WAV_DEMUXER */
  375. #if CONFIG_W64_DEMUXER
  376. static const uint8_t guid_riff[16] = {
  377. 'r', 'i', 'f', 'f',
  378. 0x2E, 0x91, 0xCF, 0x11,0xA5, 0xD6, 0x28, 0xDB, 0x04, 0xC1, 0x00, 0x00
  379. };
  380. static const uint8_t guid_wave[16] = {
  381. 'w', 'a', 'v', 'e',
  382. 0xF3, 0xAC, 0xD3, 0x11,0x8C, 0xD1, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A
  383. };
  384. static const uint8_t guid_fmt[16] = {
  385. 'f', 'm', 't', ' ',
  386. 0xF3, 0xAC, 0xD3, 0x11,0x8C, 0xD1, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A
  387. };
  388. static int w64_probe(AVProbeData *p)
  389. {
  390. if (p->buf_size <= 40)
  391. return 0;
  392. if (!memcmp(p->buf, guid_riff, 16) &&
  393. !memcmp(p->buf + 24, guid_wave, 16))
  394. return AVPROBE_SCORE_MAX;
  395. else
  396. return 0;
  397. }
  398. static int w64_read_header(AVFormatContext *s)
  399. {
  400. int64_t size;
  401. AVIOContext *pb = s->pb;
  402. WAVDemuxContext *wav = s->priv_data;
  403. AVStream *st;
  404. uint8_t guid[16];
  405. int ret;
  406. avio_read(pb, guid, 16);
  407. if (memcmp(guid, guid_riff, 16))
  408. return AVERROR_INVALIDDATA;
  409. /* riff + wave + fmt + sizes */
  410. if (avio_rl64(pb) < 16 + 8 + 16 + 8 + 16 + 8)
  411. return AVERROR_INVALIDDATA;
  412. avio_read(pb, guid, 16);
  413. if (memcmp(guid, guid_wave, 16)) {
  414. av_log(s, AV_LOG_ERROR, "could not find wave guid\n");
  415. return AVERROR_INVALIDDATA;
  416. }
  417. size = find_guid(pb, guid_fmt);
  418. if (size < 0) {
  419. av_log(s, AV_LOG_ERROR, "could not find fmt guid\n");
  420. return AVERROR_INVALIDDATA;
  421. }
  422. st = avformat_new_stream(s, NULL);
  423. if (!st)
  424. return AVERROR(ENOMEM);
  425. /* subtract chunk header size - normal wav file doesn't count it */
  426. ret = ff_get_wav_header(pb, st->codec, size - 24);
  427. if (ret < 0)
  428. return ret;
  429. avio_skip(pb, FFALIGN(size, INT64_C(8)) - size);
  430. st->need_parsing = AVSTREAM_PARSE_FULL;
  431. avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
  432. size = find_guid(pb, guid_data);
  433. if (size < 0) {
  434. av_log(s, AV_LOG_ERROR, "could not find data guid\n");
  435. return AVERROR_INVALIDDATA;
  436. }
  437. wav->data_end = avio_tell(pb) + size - 24;
  438. wav->w64 = 1;
  439. return 0;
  440. }
  441. AVInputFormat ff_w64_demuxer = {
  442. .name = "w64",
  443. .long_name = NULL_IF_CONFIG_SMALL("Sony Wave64"),
  444. .priv_data_size = sizeof(WAVDemuxContext),
  445. .read_probe = w64_probe,
  446. .read_header = w64_read_header,
  447. .read_packet = wav_read_packet,
  448. .read_seek = wav_read_seek,
  449. .flags = AVFMT_GENERIC_INDEX,
  450. .codec_tag = (const AVCodecTag * const []) { ff_codec_wav_tags, 0 },
  451. };
  452. #endif /* CONFIG_W64_DEMUXER */