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.

768 lines
24KB

  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 FFmpeg.
  10. *
  11. * FFmpeg 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. * FFmpeg 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 FFmpeg; 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/intreadwrite.h"
  29. #include "libavutil/log.h"
  30. #include "libavutil/mathematics.h"
  31. #include "libavutil/opt.h"
  32. #include "avformat.h"
  33. #include "avio.h"
  34. #include "avio_internal.h"
  35. #include "internal.h"
  36. #include "metadata.h"
  37. #include "pcm.h"
  38. #include "riff.h"
  39. #include "w64.h"
  40. #include "spdif.h"
  41. typedef struct WAVDemuxContext {
  42. const AVClass *class;
  43. int64_t data_end;
  44. int w64;
  45. int64_t smv_data_ofs;
  46. int smv_block_size;
  47. int smv_frames_per_jpeg;
  48. int smv_block;
  49. int smv_last_stream;
  50. int smv_eof;
  51. int audio_eof;
  52. int ignore_length;
  53. int spdif;
  54. int smv_cur_pt;
  55. int smv_given_first;
  56. int unaligned; // e.g. if an odd number of bytes ID3 tag was prepended
  57. int rifx; // RIFX: integer byte order for parameters is big endian
  58. } WAVDemuxContext;
  59. #if CONFIG_WAV_DEMUXER
  60. static int64_t next_tag(AVIOContext *pb, uint32_t *tag, int big_endian)
  61. {
  62. *tag = avio_rl32(pb);
  63. if (!big_endian) {
  64. return avio_rl32(pb);
  65. } else {
  66. return avio_rb32(pb);
  67. }
  68. }
  69. /* RIFF chunks are always at even offsets relative to where they start. */
  70. static int64_t wav_seek_tag(WAVDemuxContext * wav, AVIOContext *s, int64_t offset, int whence)
  71. {
  72. offset += offset < INT64_MAX && offset + wav->unaligned & 1;
  73. return avio_seek(s, offset, whence);
  74. }
  75. /* return the size of the found tag */
  76. static int64_t find_tag(WAVDemuxContext * wav, AVIOContext *pb, uint32_t tag1)
  77. {
  78. unsigned int tag;
  79. int64_t size;
  80. for (;;) {
  81. if (avio_feof(pb))
  82. return AVERROR_EOF;
  83. size = next_tag(pb, &tag, wav->rifx);
  84. if (tag == tag1)
  85. break;
  86. wav_seek_tag(wav, pb, size, SEEK_CUR);
  87. }
  88. return size;
  89. }
  90. static int wav_probe(AVProbeData *p)
  91. {
  92. /* check file header */
  93. if (p->buf_size <= 32)
  94. return 0;
  95. if (!memcmp(p->buf + 8, "WAVE", 4)) {
  96. if (!memcmp(p->buf, "RIFF", 4) || !memcmp(p->buf, "RIFX", 4))
  97. /* Since the ACT demuxer has a standard WAV header at the top of
  98. * its own, the returned score is decreased to avoid a probe
  99. * conflict between ACT and WAV. */
  100. return AVPROBE_SCORE_MAX - 1;
  101. else if (!memcmp(p->buf, "RF64", 4) &&
  102. !memcmp(p->buf + 12, "ds64", 4))
  103. return AVPROBE_SCORE_MAX;
  104. }
  105. return 0;
  106. }
  107. static void handle_stream_probing(AVStream *st)
  108. {
  109. if (st->codec->codec_id == AV_CODEC_ID_PCM_S16LE) {
  110. st->request_probe = AVPROBE_SCORE_EXTENSION;
  111. st->probe_packets = FFMIN(st->probe_packets, 14);
  112. }
  113. }
  114. static int wav_parse_fmt_tag(AVFormatContext *s, int64_t size, AVStream **st)
  115. {
  116. AVIOContext *pb = s->pb;
  117. WAVDemuxContext *wav = s->priv_data;
  118. int ret;
  119. /* parse fmt header */
  120. *st = avformat_new_stream(s, NULL);
  121. if (!*st)
  122. return AVERROR(ENOMEM);
  123. ret = ff_get_wav_header(pb, (*st)->codec, size, wav->rifx);
  124. if (ret < 0)
  125. return ret;
  126. handle_stream_probing(*st);
  127. (*st)->need_parsing = AVSTREAM_PARSE_FULL_RAW;
  128. avpriv_set_pts_info(*st, 64, 1, (*st)->codec->sample_rate);
  129. return 0;
  130. }
  131. static inline int wav_parse_bext_string(AVFormatContext *s, const char *key,
  132. int length)
  133. {
  134. char temp[257];
  135. int ret;
  136. av_assert0(length <= sizeof(temp));
  137. if ((ret = avio_read(s->pb, temp, length)) < 0)
  138. return ret;
  139. temp[length] = 0;
  140. if (strlen(temp))
  141. return av_dict_set(&s->metadata, key, temp, 0);
  142. return 0;
  143. }
  144. static int wav_parse_bext_tag(AVFormatContext *s, int64_t size)
  145. {
  146. char temp[131], *coding_history;
  147. int ret, x;
  148. uint64_t time_reference;
  149. int64_t umid_parts[8], umid_mask = 0;
  150. if ((ret = wav_parse_bext_string(s, "description", 256)) < 0 ||
  151. (ret = wav_parse_bext_string(s, "originator", 32)) < 0 ||
  152. (ret = wav_parse_bext_string(s, "originator_reference", 32)) < 0 ||
  153. (ret = wav_parse_bext_string(s, "origination_date", 10)) < 0 ||
  154. (ret = wav_parse_bext_string(s, "origination_time", 8)) < 0)
  155. return ret;
  156. time_reference = avio_rl64(s->pb);
  157. snprintf(temp, sizeof(temp), "%"PRIu64, time_reference);
  158. if ((ret = av_dict_set(&s->metadata, "time_reference", temp, 0)) < 0)
  159. return ret;
  160. /* check if version is >= 1, in which case an UMID may be present */
  161. if (avio_rl16(s->pb) >= 1) {
  162. for (x = 0; x < 8; x++)
  163. umid_mask |= umid_parts[x] = avio_rb64(s->pb);
  164. if (umid_mask) {
  165. /* the string formatting below is per SMPTE 330M-2004 Annex C */
  166. if (umid_parts[4] == 0 && umid_parts[5] == 0 &&
  167. umid_parts[6] == 0 && umid_parts[7] == 0) {
  168. /* basic UMID */
  169. snprintf(temp, sizeof(temp),
  170. "0x%016"PRIX64"%016"PRIX64"%016"PRIX64"%016"PRIX64,
  171. umid_parts[0], umid_parts[1],
  172. umid_parts[2], umid_parts[3]);
  173. } else {
  174. /* extended UMID */
  175. snprintf(temp, sizeof(temp),
  176. "0x%016"PRIX64"%016"PRIX64"%016"PRIX64"%016"PRIX64
  177. "%016"PRIX64"%016"PRIX64"%016"PRIX64"%016"PRIX64,
  178. umid_parts[0], umid_parts[1],
  179. umid_parts[2], umid_parts[3],
  180. umid_parts[4], umid_parts[5],
  181. umid_parts[6], umid_parts[7]);
  182. }
  183. if ((ret = av_dict_set(&s->metadata, "umid", temp, 0)) < 0)
  184. return ret;
  185. }
  186. avio_skip(s->pb, 190);
  187. } else
  188. avio_skip(s->pb, 254);
  189. if (size > 602) {
  190. /* CodingHistory present */
  191. size -= 602;
  192. if (!(coding_history = av_malloc(size + 1)))
  193. return AVERROR(ENOMEM);
  194. if ((ret = avio_read(s->pb, coding_history, size)) < 0)
  195. return ret;
  196. coding_history[size] = 0;
  197. if ((ret = av_dict_set(&s->metadata, "coding_history", coding_history,
  198. AV_DICT_DONT_STRDUP_VAL)) < 0)
  199. return ret;
  200. }
  201. return 0;
  202. }
  203. static const AVMetadataConv wav_metadata_conv[] = {
  204. { "description", "comment" },
  205. { "originator", "encoded_by" },
  206. { "origination_date", "date" },
  207. { "origination_time", "creation_time" },
  208. { 0 },
  209. };
  210. /* wav input */
  211. static int wav_read_header(AVFormatContext *s)
  212. {
  213. int64_t size, av_uninit(data_size);
  214. int64_t sample_count = 0;
  215. int rf64 = 0;
  216. char start_code[32];
  217. uint32_t tag;
  218. AVIOContext *pb = s->pb;
  219. AVStream *st = NULL;
  220. WAVDemuxContext *wav = s->priv_data;
  221. int ret, got_fmt = 0;
  222. int64_t next_tag_ofs, data_ofs = -1;
  223. wav->unaligned = avio_tell(s->pb) & 1;
  224. wav->smv_data_ofs = -1;
  225. /* read chunk ID */
  226. tag = avio_rl32(pb);
  227. switch (tag) {
  228. case MKTAG('R', 'I', 'F', 'F'):
  229. break;
  230. case MKTAG('R', 'I', 'F', 'X'):
  231. wav->rifx = 1;
  232. break;
  233. case MKTAG('R', 'F', '6', '4'):
  234. rf64 = 1;
  235. break;
  236. default:
  237. av_get_codec_tag_string(start_code, sizeof(start_code), tag);
  238. av_log(s, AV_LOG_ERROR, "invalid start code %s in RIFF header\n", start_code);
  239. return AVERROR_INVALIDDATA;
  240. }
  241. /* read chunk size */
  242. avio_rl32(pb);
  243. /* read format */
  244. if (avio_rl32(pb) != MKTAG('W', 'A', 'V', 'E')) {
  245. av_log(s, AV_LOG_ERROR, "invalid format in RIFF header\n");
  246. return AVERROR_INVALIDDATA;
  247. }
  248. if (rf64) {
  249. if (avio_rl32(pb) != MKTAG('d', 's', '6', '4'))
  250. return AVERROR_INVALIDDATA;
  251. size = avio_rl32(pb);
  252. if (size < 24)
  253. return AVERROR_INVALIDDATA;
  254. avio_rl64(pb); /* RIFF size */
  255. data_size = avio_rl64(pb);
  256. sample_count = avio_rl64(pb);
  257. if (data_size < 0 || sample_count < 0) {
  258. av_log(s, AV_LOG_ERROR, "negative data_size and/or sample_count in "
  259. "ds64: data_size = %"PRId64", sample_count = %"PRId64"\n",
  260. data_size, sample_count);
  261. return AVERROR_INVALIDDATA;
  262. }
  263. avio_skip(pb, size - 24); /* skip rest of ds64 chunk */
  264. }
  265. for (;;) {
  266. AVStream *vst;
  267. size = next_tag(pb, &tag, wav->rifx);
  268. next_tag_ofs = avio_tell(pb) + size;
  269. if (avio_feof(pb))
  270. break;
  271. switch (tag) {
  272. case MKTAG('f', 'm', 't', ' '):
  273. /* only parse the first 'fmt ' tag found */
  274. if (!got_fmt && (ret = wav_parse_fmt_tag(s, size, &st)) < 0) {
  275. return ret;
  276. } else if (got_fmt)
  277. av_log(s, AV_LOG_WARNING, "found more than one 'fmt ' tag\n");
  278. got_fmt = 1;
  279. break;
  280. case MKTAG('d', 'a', 't', 'a'):
  281. if (!got_fmt) {
  282. av_log(s, AV_LOG_ERROR,
  283. "found no 'fmt ' tag before the 'data' tag\n");
  284. return AVERROR_INVALIDDATA;
  285. }
  286. if (rf64) {
  287. next_tag_ofs = wav->data_end = avio_tell(pb) + data_size;
  288. } else {
  289. data_size = size;
  290. next_tag_ofs = wav->data_end = size ? next_tag_ofs : INT64_MAX;
  291. }
  292. data_ofs = avio_tell(pb);
  293. /* don't look for footer metadata if we can't seek or if we don't
  294. * know where the data tag ends
  295. */
  296. if (!pb->seekable || (!rf64 && !size))
  297. goto break_loop;
  298. break;
  299. case MKTAG('f', 'a', 'c', 't'):
  300. if (!sample_count)
  301. sample_count = (!wav->rifx ? avio_rl32(pb) : avio_rb32(pb));
  302. break;
  303. case MKTAG('b', 'e', 'x', 't'):
  304. if ((ret = wav_parse_bext_tag(s, size)) < 0)
  305. return ret;
  306. break;
  307. case MKTAG('S','M','V','0'):
  308. if (!got_fmt) {
  309. av_log(s, AV_LOG_ERROR, "found no 'fmt ' tag before the 'SMV0' tag\n");
  310. return AVERROR_INVALIDDATA;
  311. }
  312. // SMV file, a wav file with video appended.
  313. if (size != MKTAG('0','2','0','0')) {
  314. av_log(s, AV_LOG_ERROR, "Unknown SMV version found\n");
  315. goto break_loop;
  316. }
  317. av_log(s, AV_LOG_DEBUG, "Found SMV data\n");
  318. wav->smv_given_first = 0;
  319. vst = avformat_new_stream(s, NULL);
  320. if (!vst)
  321. return AVERROR(ENOMEM);
  322. avio_r8(pb);
  323. vst->id = 1;
  324. vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  325. vst->codec->codec_id = AV_CODEC_ID_SMVJPEG;
  326. vst->codec->width = avio_rl24(pb);
  327. vst->codec->height = avio_rl24(pb);
  328. if (ff_alloc_extradata(vst->codec, 4)) {
  329. av_log(s, AV_LOG_ERROR, "Could not allocate extradata.\n");
  330. return AVERROR(ENOMEM);
  331. }
  332. size = avio_rl24(pb);
  333. wav->smv_data_ofs = avio_tell(pb) + (size - 5) * 3;
  334. avio_rl24(pb);
  335. wav->smv_block_size = avio_rl24(pb);
  336. avpriv_set_pts_info(vst, 32, 1, avio_rl24(pb));
  337. vst->duration = avio_rl24(pb);
  338. avio_rl24(pb);
  339. avio_rl24(pb);
  340. wav->smv_frames_per_jpeg = avio_rl24(pb);
  341. if (wav->smv_frames_per_jpeg > 65536) {
  342. av_log(s, AV_LOG_ERROR, "too many frames per jpeg\n");
  343. return AVERROR_INVALIDDATA;
  344. }
  345. AV_WL32(vst->codec->extradata, wav->smv_frames_per_jpeg);
  346. wav->smv_cur_pt = 0;
  347. goto break_loop;
  348. case MKTAG('L', 'I', 'S', 'T'):
  349. if (size < 4) {
  350. av_log(s, AV_LOG_ERROR, "too short LIST tag\n");
  351. return AVERROR_INVALIDDATA;
  352. }
  353. switch (avio_rl32(pb)) {
  354. case MKTAG('I', 'N', 'F', 'O'):
  355. ff_read_riff_info(s, size - 4);
  356. }
  357. break;
  358. }
  359. /* seek to next tag unless we know that we'll run into EOF */
  360. if ((avio_size(pb) > 0 && next_tag_ofs >= avio_size(pb)) ||
  361. wav_seek_tag(wav, pb, next_tag_ofs, SEEK_SET) < 0) {
  362. break;
  363. }
  364. }
  365. break_loop:
  366. if (data_ofs < 0) {
  367. av_log(s, AV_LOG_ERROR, "no 'data' tag found\n");
  368. return AVERROR_INVALIDDATA;
  369. }
  370. avio_seek(pb, data_ofs, SEEK_SET);
  371. if ( data_size > 0 && sample_count && st->codec->channels
  372. && data_size / sample_count / st->codec->channels > 8) {
  373. av_log(s, AV_LOG_WARNING, "ignoring wrong sample_count %"PRId64"\n", sample_count);
  374. sample_count = 0;
  375. }
  376. if (!sample_count || av_get_exact_bits_per_sample(st->codec->codec_id) > 0)
  377. if ( st->codec->channels
  378. && data_size
  379. && av_get_bits_per_sample(st->codec->codec_id)
  380. && wav->data_end <= avio_size(pb))
  381. sample_count = (data_size << 3)
  382. /
  383. (st->codec->channels * (uint64_t)av_get_bits_per_sample(st->codec->codec_id));
  384. if (sample_count)
  385. st->duration = sample_count;
  386. ff_metadata_conv_ctx(s, NULL, wav_metadata_conv);
  387. ff_metadata_conv_ctx(s, NULL, ff_riff_info_conv);
  388. return 0;
  389. }
  390. /**
  391. * Find chunk with w64 GUID by skipping over other chunks.
  392. * @return the size of the found chunk
  393. */
  394. static int64_t find_guid(AVIOContext *pb, const uint8_t guid1[16])
  395. {
  396. uint8_t guid[16];
  397. int64_t size;
  398. while (!avio_feof(pb)) {
  399. avio_read(pb, guid, 16);
  400. size = avio_rl64(pb);
  401. if (size <= 24)
  402. return AVERROR_INVALIDDATA;
  403. if (!memcmp(guid, guid1, 16))
  404. return size;
  405. avio_skip(pb, FFALIGN(size, INT64_C(8)) - 24);
  406. }
  407. return AVERROR_EOF;
  408. }
  409. #define MAX_SIZE 4096
  410. static int wav_read_packet(AVFormatContext *s, AVPacket *pkt)
  411. {
  412. int ret, size;
  413. int64_t left;
  414. AVStream *st;
  415. WAVDemuxContext *wav = s->priv_data;
  416. if (CONFIG_SPDIF_DEMUXER && wav->spdif == 0 &&
  417. s->streams[0]->codec->codec_tag == 1) {
  418. enum AVCodecID codec;
  419. ret = ff_spdif_probe(s->pb->buffer, s->pb->buf_end - s->pb->buffer,
  420. &codec);
  421. if (ret > AVPROBE_SCORE_EXTENSION) {
  422. s->streams[0]->codec->codec_id = codec;
  423. wav->spdif = 1;
  424. } else {
  425. wav->spdif = -1;
  426. }
  427. }
  428. if (CONFIG_SPDIF_DEMUXER && wav->spdif == 1)
  429. return ff_spdif_read_packet(s, pkt);
  430. if (wav->smv_data_ofs > 0) {
  431. int64_t audio_dts, video_dts;
  432. smv_retry:
  433. audio_dts = (int32_t)s->streams[0]->cur_dts;
  434. video_dts = (int32_t)s->streams[1]->cur_dts;
  435. if (audio_dts != AV_NOPTS_VALUE && video_dts != AV_NOPTS_VALUE) {
  436. /*We always return a video frame first to get the pixel format first*/
  437. wav->smv_last_stream = wav->smv_given_first ?
  438. av_compare_ts(video_dts, s->streams[1]->time_base,
  439. audio_dts, s->streams[0]->time_base) > 0 : 0;
  440. wav->smv_given_first = 1;
  441. }
  442. wav->smv_last_stream = !wav->smv_last_stream;
  443. wav->smv_last_stream |= wav->audio_eof;
  444. wav->smv_last_stream &= !wav->smv_eof;
  445. if (wav->smv_last_stream) {
  446. uint64_t old_pos = avio_tell(s->pb);
  447. uint64_t new_pos = wav->smv_data_ofs +
  448. wav->smv_block * wav->smv_block_size;
  449. if (avio_seek(s->pb, new_pos, SEEK_SET) < 0) {
  450. ret = AVERROR_EOF;
  451. goto smv_out;
  452. }
  453. size = avio_rl24(s->pb);
  454. ret = av_get_packet(s->pb, pkt, size);
  455. if (ret < 0)
  456. goto smv_out;
  457. pkt->pos -= 3;
  458. pkt->pts = wav->smv_block * wav->smv_frames_per_jpeg + wav->smv_cur_pt;
  459. wav->smv_cur_pt++;
  460. if (wav->smv_frames_per_jpeg > 0)
  461. wav->smv_cur_pt %= wav->smv_frames_per_jpeg;
  462. if (!wav->smv_cur_pt)
  463. wav->smv_block++;
  464. pkt->stream_index = 1;
  465. smv_out:
  466. avio_seek(s->pb, old_pos, SEEK_SET);
  467. if (ret == AVERROR_EOF) {
  468. wav->smv_eof = 1;
  469. goto smv_retry;
  470. }
  471. return ret;
  472. }
  473. }
  474. st = s->streams[0];
  475. left = wav->data_end - avio_tell(s->pb);
  476. if (wav->ignore_length)
  477. left = INT_MAX;
  478. if (left <= 0) {
  479. if (CONFIG_W64_DEMUXER && wav->w64)
  480. left = find_guid(s->pb, ff_w64_guid_data) - 24;
  481. else
  482. left = find_tag(wav, s->pb, MKTAG('d', 'a', 't', 'a'));
  483. if (left < 0) {
  484. wav->audio_eof = 1;
  485. if (wav->smv_data_ofs > 0 && !wav->smv_eof)
  486. goto smv_retry;
  487. return AVERROR_EOF;
  488. }
  489. wav->data_end = avio_tell(s->pb) + left;
  490. }
  491. size = MAX_SIZE;
  492. if (st->codec->block_align > 1) {
  493. if (size < st->codec->block_align)
  494. size = st->codec->block_align;
  495. size = (size / st->codec->block_align) * st->codec->block_align;
  496. }
  497. size = FFMIN(size, left);
  498. ret = av_get_packet(s->pb, pkt, size);
  499. if (ret < 0)
  500. return ret;
  501. pkt->stream_index = 0;
  502. return ret;
  503. }
  504. static int wav_read_seek(AVFormatContext *s,
  505. int stream_index, int64_t timestamp, int flags)
  506. {
  507. WAVDemuxContext *wav = s->priv_data;
  508. AVStream *st;
  509. wav->smv_eof = 0;
  510. wav->audio_eof = 0;
  511. if (wav->smv_data_ofs > 0) {
  512. int64_t smv_timestamp = timestamp;
  513. if (stream_index == 0)
  514. smv_timestamp = av_rescale_q(timestamp, s->streams[0]->time_base, s->streams[1]->time_base);
  515. else
  516. timestamp = av_rescale_q(smv_timestamp, s->streams[1]->time_base, s->streams[0]->time_base);
  517. if (wav->smv_frames_per_jpeg > 0) {
  518. wav->smv_block = smv_timestamp / wav->smv_frames_per_jpeg;
  519. wav->smv_cur_pt = smv_timestamp % wav->smv_frames_per_jpeg;
  520. }
  521. }
  522. st = s->streams[0];
  523. switch (st->codec->codec_id) {
  524. case AV_CODEC_ID_MP2:
  525. case AV_CODEC_ID_MP3:
  526. case AV_CODEC_ID_AC3:
  527. case AV_CODEC_ID_DTS:
  528. /* use generic seeking with dynamically generated indexes */
  529. return -1;
  530. default:
  531. break;
  532. }
  533. return ff_pcm_read_seek(s, stream_index, timestamp, flags);
  534. }
  535. #define OFFSET(x) offsetof(WAVDemuxContext, x)
  536. #define DEC AV_OPT_FLAG_DECODING_PARAM
  537. static const AVOption demux_options[] = {
  538. { "ignore_length", "Ignore length", OFFSET(ignore_length), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, DEC },
  539. { NULL },
  540. };
  541. static const AVClass wav_demuxer_class = {
  542. .class_name = "WAV demuxer",
  543. .item_name = av_default_item_name,
  544. .option = demux_options,
  545. .version = LIBAVUTIL_VERSION_INT,
  546. };
  547. AVInputFormat ff_wav_demuxer = {
  548. .name = "wav",
  549. .long_name = NULL_IF_CONFIG_SMALL("WAV / WAVE (Waveform Audio)"),
  550. .priv_data_size = sizeof(WAVDemuxContext),
  551. .read_probe = wav_probe,
  552. .read_header = wav_read_header,
  553. .read_packet = wav_read_packet,
  554. .read_seek = wav_read_seek,
  555. .flags = AVFMT_GENERIC_INDEX,
  556. .codec_tag = (const AVCodecTag * const []) { ff_codec_wav_tags, 0 },
  557. .priv_class = &wav_demuxer_class,
  558. };
  559. #endif /* CONFIG_WAV_DEMUXER */
  560. #if CONFIG_W64_DEMUXER
  561. static int w64_probe(AVProbeData *p)
  562. {
  563. if (p->buf_size <= 40)
  564. return 0;
  565. if (!memcmp(p->buf, ff_w64_guid_riff, 16) &&
  566. !memcmp(p->buf + 24, ff_w64_guid_wave, 16))
  567. return AVPROBE_SCORE_MAX;
  568. else
  569. return 0;
  570. }
  571. static int w64_read_header(AVFormatContext *s)
  572. {
  573. int64_t size, data_ofs = 0;
  574. AVIOContext *pb = s->pb;
  575. WAVDemuxContext *wav = s->priv_data;
  576. AVStream *st;
  577. uint8_t guid[16];
  578. int ret;
  579. avio_read(pb, guid, 16);
  580. if (memcmp(guid, ff_w64_guid_riff, 16))
  581. return AVERROR_INVALIDDATA;
  582. /* riff + wave + fmt + sizes */
  583. if (avio_rl64(pb) < 16 + 8 + 16 + 8 + 16 + 8)
  584. return AVERROR_INVALIDDATA;
  585. avio_read(pb, guid, 16);
  586. if (memcmp(guid, ff_w64_guid_wave, 16)) {
  587. av_log(s, AV_LOG_ERROR, "could not find wave guid\n");
  588. return AVERROR_INVALIDDATA;
  589. }
  590. wav->w64 = 1;
  591. st = avformat_new_stream(s, NULL);
  592. if (!st)
  593. return AVERROR(ENOMEM);
  594. while (!avio_feof(pb)) {
  595. if (avio_read(pb, guid, 16) != 16)
  596. break;
  597. size = avio_rl64(pb);
  598. if (size <= 24 || INT64_MAX - size < avio_tell(pb))
  599. return AVERROR_INVALIDDATA;
  600. if (!memcmp(guid, ff_w64_guid_fmt, 16)) {
  601. /* subtract chunk header size - normal wav file doesn't count it */
  602. ret = ff_get_wav_header(pb, st->codec, size - 24, 0);
  603. if (ret < 0)
  604. return ret;
  605. avio_skip(pb, FFALIGN(size, INT64_C(8)) - size);
  606. avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
  607. } else if (!memcmp(guid, ff_w64_guid_fact, 16)) {
  608. int64_t samples;
  609. samples = avio_rl64(pb);
  610. if (samples > 0)
  611. st->duration = samples;
  612. } else if (!memcmp(guid, ff_w64_guid_data, 16)) {
  613. wav->data_end = avio_tell(pb) + size - 24;
  614. data_ofs = avio_tell(pb);
  615. if (!pb->seekable)
  616. break;
  617. avio_skip(pb, size - 24);
  618. } else if (!memcmp(guid, ff_w64_guid_summarylist, 16)) {
  619. int64_t start, end, cur;
  620. uint32_t count, chunk_size, i;
  621. start = avio_tell(pb);
  622. end = start + FFALIGN(size, INT64_C(8)) - 24;
  623. count = avio_rl32(pb);
  624. for (i = 0; i < count; i++) {
  625. char chunk_key[5], *value;
  626. if (avio_feof(pb) || (cur = avio_tell(pb)) < 0 || cur > end - 8 /* = tag + size */)
  627. break;
  628. chunk_key[4] = 0;
  629. avio_read(pb, chunk_key, 4);
  630. chunk_size = avio_rl32(pb);
  631. value = av_mallocz(chunk_size + 1);
  632. if (!value)
  633. return AVERROR(ENOMEM);
  634. ret = avio_get_str16le(pb, chunk_size, value, chunk_size);
  635. avio_skip(pb, chunk_size - ret);
  636. av_dict_set(&s->metadata, chunk_key, value, AV_DICT_DONT_STRDUP_VAL);
  637. }
  638. avio_skip(pb, end - avio_tell(pb));
  639. } else {
  640. av_log(s, AV_LOG_DEBUG, "unknown guid: "FF_PRI_GUID"\n", FF_ARG_GUID(guid));
  641. avio_skip(pb, FFALIGN(size, INT64_C(8)) - 24);
  642. }
  643. }
  644. if (!data_ofs)
  645. return AVERROR_EOF;
  646. ff_metadata_conv_ctx(s, NULL, wav_metadata_conv);
  647. ff_metadata_conv_ctx(s, NULL, ff_riff_info_conv);
  648. handle_stream_probing(st);
  649. st->need_parsing = AVSTREAM_PARSE_FULL_RAW;
  650. avio_seek(pb, data_ofs, SEEK_SET);
  651. return 0;
  652. }
  653. AVInputFormat ff_w64_demuxer = {
  654. .name = "w64",
  655. .long_name = NULL_IF_CONFIG_SMALL("Sony Wave64"),
  656. .priv_data_size = sizeof(WAVDemuxContext),
  657. .read_probe = w64_probe,
  658. .read_header = w64_read_header,
  659. .read_packet = wav_read_packet,
  660. .read_seek = wav_read_seek,
  661. .flags = AVFMT_GENERIC_INDEX,
  662. .codec_tag = (const AVCodecTag * const []) { ff_codec_wav_tags, 0 },
  663. };
  664. #endif /* CONFIG_W64_DEMUXER */