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.

746 lines
23KB

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