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.

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