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.

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