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.

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