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.

773 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 if (size != 0xFFFFFFFF) {
  289. data_size = size;
  290. next_tag_ofs = wav->data_end = size ? next_tag_ofs : INT64_MAX;
  291. } else {
  292. av_log(s, AV_LOG_WARNING, "Ignoring maximum wav data size, "
  293. "file may be invalid\n");
  294. data_size = 0;
  295. next_tag_ofs = wav->data_end = INT64_MAX;
  296. }
  297. data_ofs = avio_tell(pb);
  298. /* don't look for footer metadata if we can't seek or if we don't
  299. * know where the data tag ends
  300. */
  301. if (!pb->seekable || (!rf64 && !size))
  302. goto break_loop;
  303. break;
  304. case MKTAG('f', 'a', 'c', 't'):
  305. if (!sample_count)
  306. sample_count = (!wav->rifx ? avio_rl32(pb) : avio_rb32(pb));
  307. break;
  308. case MKTAG('b', 'e', 'x', 't'):
  309. if ((ret = wav_parse_bext_tag(s, size)) < 0)
  310. return ret;
  311. break;
  312. case MKTAG('S','M','V','0'):
  313. if (!got_fmt) {
  314. av_log(s, AV_LOG_ERROR, "found no 'fmt ' tag before the 'SMV0' tag\n");
  315. return AVERROR_INVALIDDATA;
  316. }
  317. // SMV file, a wav file with video appended.
  318. if (size != MKTAG('0','2','0','0')) {
  319. av_log(s, AV_LOG_ERROR, "Unknown SMV version found\n");
  320. goto break_loop;
  321. }
  322. av_log(s, AV_LOG_DEBUG, "Found SMV data\n");
  323. wav->smv_given_first = 0;
  324. vst = avformat_new_stream(s, NULL);
  325. if (!vst)
  326. return AVERROR(ENOMEM);
  327. avio_r8(pb);
  328. vst->id = 1;
  329. vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  330. vst->codec->codec_id = AV_CODEC_ID_SMVJPEG;
  331. vst->codec->width = avio_rl24(pb);
  332. vst->codec->height = avio_rl24(pb);
  333. if (ff_alloc_extradata(vst->codec, 4)) {
  334. av_log(s, AV_LOG_ERROR, "Could not allocate extradata.\n");
  335. return AVERROR(ENOMEM);
  336. }
  337. size = avio_rl24(pb);
  338. wav->smv_data_ofs = avio_tell(pb) + (size - 5) * 3;
  339. avio_rl24(pb);
  340. wav->smv_block_size = avio_rl24(pb);
  341. avpriv_set_pts_info(vst, 32, 1, avio_rl24(pb));
  342. vst->duration = avio_rl24(pb);
  343. avio_rl24(pb);
  344. avio_rl24(pb);
  345. wav->smv_frames_per_jpeg = avio_rl24(pb);
  346. if (wav->smv_frames_per_jpeg > 65536) {
  347. av_log(s, AV_LOG_ERROR, "too many frames per jpeg\n");
  348. return AVERROR_INVALIDDATA;
  349. }
  350. AV_WL32(vst->codec->extradata, wav->smv_frames_per_jpeg);
  351. wav->smv_cur_pt = 0;
  352. goto break_loop;
  353. case MKTAG('L', 'I', 'S', 'T'):
  354. if (size < 4) {
  355. av_log(s, AV_LOG_ERROR, "too short LIST tag\n");
  356. return AVERROR_INVALIDDATA;
  357. }
  358. switch (avio_rl32(pb)) {
  359. case MKTAG('I', 'N', 'F', 'O'):
  360. ff_read_riff_info(s, size - 4);
  361. }
  362. break;
  363. }
  364. /* seek to next tag unless we know that we'll run into EOF */
  365. if ((avio_size(pb) > 0 && next_tag_ofs >= avio_size(pb)) ||
  366. wav_seek_tag(wav, pb, next_tag_ofs, SEEK_SET) < 0) {
  367. break;
  368. }
  369. }
  370. break_loop:
  371. if (data_ofs < 0) {
  372. av_log(s, AV_LOG_ERROR, "no 'data' tag found\n");
  373. return AVERROR_INVALIDDATA;
  374. }
  375. avio_seek(pb, data_ofs, SEEK_SET);
  376. if ( data_size > 0 && sample_count && st->codec->channels
  377. && data_size / sample_count / st->codec->channels > 8) {
  378. av_log(s, AV_LOG_WARNING, "ignoring wrong sample_count %"PRId64"\n", sample_count);
  379. sample_count = 0;
  380. }
  381. if (!sample_count || av_get_exact_bits_per_sample(st->codec->codec_id) > 0)
  382. if ( st->codec->channels
  383. && data_size
  384. && av_get_bits_per_sample(st->codec->codec_id)
  385. && wav->data_end <= avio_size(pb))
  386. sample_count = (data_size << 3)
  387. /
  388. (st->codec->channels * (uint64_t)av_get_bits_per_sample(st->codec->codec_id));
  389. if (sample_count)
  390. st->duration = sample_count;
  391. ff_metadata_conv_ctx(s, NULL, wav_metadata_conv);
  392. ff_metadata_conv_ctx(s, NULL, ff_riff_info_conv);
  393. return 0;
  394. }
  395. /**
  396. * Find chunk with w64 GUID by skipping over other chunks.
  397. * @return the size of the found chunk
  398. */
  399. static int64_t find_guid(AVIOContext *pb, const uint8_t guid1[16])
  400. {
  401. uint8_t guid[16];
  402. int64_t size;
  403. while (!avio_feof(pb)) {
  404. avio_read(pb, guid, 16);
  405. size = avio_rl64(pb);
  406. if (size <= 24)
  407. return AVERROR_INVALIDDATA;
  408. if (!memcmp(guid, guid1, 16))
  409. return size;
  410. avio_skip(pb, FFALIGN(size, INT64_C(8)) - 24);
  411. }
  412. return AVERROR_EOF;
  413. }
  414. #define MAX_SIZE 4096
  415. static int wav_read_packet(AVFormatContext *s, AVPacket *pkt)
  416. {
  417. int ret, size;
  418. int64_t left;
  419. AVStream *st;
  420. WAVDemuxContext *wav = s->priv_data;
  421. if (CONFIG_SPDIF_DEMUXER && wav->spdif == 0 &&
  422. s->streams[0]->codec->codec_tag == 1) {
  423. enum AVCodecID codec;
  424. ret = ff_spdif_probe(s->pb->buffer, s->pb->buf_end - s->pb->buffer,
  425. &codec);
  426. if (ret > AVPROBE_SCORE_EXTENSION) {
  427. s->streams[0]->codec->codec_id = codec;
  428. wav->spdif = 1;
  429. } else {
  430. wav->spdif = -1;
  431. }
  432. }
  433. if (CONFIG_SPDIF_DEMUXER && wav->spdif == 1)
  434. return ff_spdif_read_packet(s, pkt);
  435. if (wav->smv_data_ofs > 0) {
  436. int64_t audio_dts, video_dts;
  437. smv_retry:
  438. audio_dts = (int32_t)s->streams[0]->cur_dts;
  439. video_dts = (int32_t)s->streams[1]->cur_dts;
  440. if (audio_dts != AV_NOPTS_VALUE && video_dts != AV_NOPTS_VALUE) {
  441. /*We always return a video frame first to get the pixel format first*/
  442. wav->smv_last_stream = wav->smv_given_first ?
  443. av_compare_ts(video_dts, s->streams[1]->time_base,
  444. audio_dts, s->streams[0]->time_base) > 0 : 0;
  445. wav->smv_given_first = 1;
  446. }
  447. wav->smv_last_stream = !wav->smv_last_stream;
  448. wav->smv_last_stream |= wav->audio_eof;
  449. wav->smv_last_stream &= !wav->smv_eof;
  450. if (wav->smv_last_stream) {
  451. uint64_t old_pos = avio_tell(s->pb);
  452. uint64_t new_pos = wav->smv_data_ofs +
  453. wav->smv_block * wav->smv_block_size;
  454. if (avio_seek(s->pb, new_pos, SEEK_SET) < 0) {
  455. ret = AVERROR_EOF;
  456. goto smv_out;
  457. }
  458. size = avio_rl24(s->pb);
  459. ret = av_get_packet(s->pb, pkt, size);
  460. if (ret < 0)
  461. goto smv_out;
  462. pkt->pos -= 3;
  463. pkt->pts = wav->smv_block * wav->smv_frames_per_jpeg + wav->smv_cur_pt;
  464. wav->smv_cur_pt++;
  465. if (wav->smv_frames_per_jpeg > 0)
  466. wav->smv_cur_pt %= wav->smv_frames_per_jpeg;
  467. if (!wav->smv_cur_pt)
  468. wav->smv_block++;
  469. pkt->stream_index = 1;
  470. smv_out:
  471. avio_seek(s->pb, old_pos, SEEK_SET);
  472. if (ret == AVERROR_EOF) {
  473. wav->smv_eof = 1;
  474. goto smv_retry;
  475. }
  476. return ret;
  477. }
  478. }
  479. st = s->streams[0];
  480. left = wav->data_end - avio_tell(s->pb);
  481. if (wav->ignore_length)
  482. left = INT_MAX;
  483. if (left <= 0) {
  484. if (CONFIG_W64_DEMUXER && wav->w64)
  485. left = find_guid(s->pb, ff_w64_guid_data) - 24;
  486. else
  487. left = find_tag(wav, s->pb, MKTAG('d', 'a', 't', 'a'));
  488. if (left < 0) {
  489. wav->audio_eof = 1;
  490. if (wav->smv_data_ofs > 0 && !wav->smv_eof)
  491. goto smv_retry;
  492. return AVERROR_EOF;
  493. }
  494. wav->data_end = avio_tell(s->pb) + left;
  495. }
  496. size = MAX_SIZE;
  497. if (st->codec->block_align > 1) {
  498. if (size < st->codec->block_align)
  499. size = st->codec->block_align;
  500. size = (size / st->codec->block_align) * st->codec->block_align;
  501. }
  502. size = FFMIN(size, left);
  503. ret = av_get_packet(s->pb, pkt, size);
  504. if (ret < 0)
  505. return ret;
  506. pkt->stream_index = 0;
  507. return ret;
  508. }
  509. static int wav_read_seek(AVFormatContext *s,
  510. int stream_index, int64_t timestamp, int flags)
  511. {
  512. WAVDemuxContext *wav = s->priv_data;
  513. AVStream *st;
  514. wav->smv_eof = 0;
  515. wav->audio_eof = 0;
  516. if (wav->smv_data_ofs > 0) {
  517. int64_t smv_timestamp = timestamp;
  518. if (stream_index == 0)
  519. smv_timestamp = av_rescale_q(timestamp, s->streams[0]->time_base, s->streams[1]->time_base);
  520. else
  521. timestamp = av_rescale_q(smv_timestamp, s->streams[1]->time_base, s->streams[0]->time_base);
  522. if (wav->smv_frames_per_jpeg > 0) {
  523. wav->smv_block = smv_timestamp / wav->smv_frames_per_jpeg;
  524. wav->smv_cur_pt = smv_timestamp % wav->smv_frames_per_jpeg;
  525. }
  526. }
  527. st = s->streams[0];
  528. switch (st->codec->codec_id) {
  529. case AV_CODEC_ID_MP2:
  530. case AV_CODEC_ID_MP3:
  531. case AV_CODEC_ID_AC3:
  532. case AV_CODEC_ID_DTS:
  533. /* use generic seeking with dynamically generated indexes */
  534. return -1;
  535. default:
  536. break;
  537. }
  538. return ff_pcm_read_seek(s, stream_index, timestamp, flags);
  539. }
  540. #define OFFSET(x) offsetof(WAVDemuxContext, x)
  541. #define DEC AV_OPT_FLAG_DECODING_PARAM
  542. static const AVOption demux_options[] = {
  543. { "ignore_length", "Ignore length", OFFSET(ignore_length), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, DEC },
  544. { NULL },
  545. };
  546. static const AVClass wav_demuxer_class = {
  547. .class_name = "WAV demuxer",
  548. .item_name = av_default_item_name,
  549. .option = demux_options,
  550. .version = LIBAVUTIL_VERSION_INT,
  551. };
  552. AVInputFormat ff_wav_demuxer = {
  553. .name = "wav",
  554. .long_name = NULL_IF_CONFIG_SMALL("WAV / WAVE (Waveform Audio)"),
  555. .priv_data_size = sizeof(WAVDemuxContext),
  556. .read_probe = wav_probe,
  557. .read_header = wav_read_header,
  558. .read_packet = wav_read_packet,
  559. .read_seek = wav_read_seek,
  560. .flags = AVFMT_GENERIC_INDEX,
  561. .codec_tag = (const AVCodecTag * const []) { ff_codec_wav_tags, 0 },
  562. .priv_class = &wav_demuxer_class,
  563. };
  564. #endif /* CONFIG_WAV_DEMUXER */
  565. #if CONFIG_W64_DEMUXER
  566. static int w64_probe(AVProbeData *p)
  567. {
  568. if (p->buf_size <= 40)
  569. return 0;
  570. if (!memcmp(p->buf, ff_w64_guid_riff, 16) &&
  571. !memcmp(p->buf + 24, ff_w64_guid_wave, 16))
  572. return AVPROBE_SCORE_MAX;
  573. else
  574. return 0;
  575. }
  576. static int w64_read_header(AVFormatContext *s)
  577. {
  578. int64_t size, data_ofs = 0;
  579. AVIOContext *pb = s->pb;
  580. WAVDemuxContext *wav = s->priv_data;
  581. AVStream *st;
  582. uint8_t guid[16];
  583. int ret;
  584. avio_read(pb, guid, 16);
  585. if (memcmp(guid, ff_w64_guid_riff, 16))
  586. return AVERROR_INVALIDDATA;
  587. /* riff + wave + fmt + sizes */
  588. if (avio_rl64(pb) < 16 + 8 + 16 + 8 + 16 + 8)
  589. return AVERROR_INVALIDDATA;
  590. avio_read(pb, guid, 16);
  591. if (memcmp(guid, ff_w64_guid_wave, 16)) {
  592. av_log(s, AV_LOG_ERROR, "could not find wave guid\n");
  593. return AVERROR_INVALIDDATA;
  594. }
  595. wav->w64 = 1;
  596. st = avformat_new_stream(s, NULL);
  597. if (!st)
  598. return AVERROR(ENOMEM);
  599. while (!avio_feof(pb)) {
  600. if (avio_read(pb, guid, 16) != 16)
  601. break;
  602. size = avio_rl64(pb);
  603. if (size <= 24 || INT64_MAX - size < avio_tell(pb))
  604. return AVERROR_INVALIDDATA;
  605. if (!memcmp(guid, ff_w64_guid_fmt, 16)) {
  606. /* subtract chunk header size - normal wav file doesn't count it */
  607. ret = ff_get_wav_header(pb, st->codec, size - 24, 0);
  608. if (ret < 0)
  609. return ret;
  610. avio_skip(pb, FFALIGN(size, INT64_C(8)) - size);
  611. avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
  612. } else if (!memcmp(guid, ff_w64_guid_fact, 16)) {
  613. int64_t samples;
  614. samples = avio_rl64(pb);
  615. if (samples > 0)
  616. st->duration = samples;
  617. } else if (!memcmp(guid, ff_w64_guid_data, 16)) {
  618. wav->data_end = avio_tell(pb) + size - 24;
  619. data_ofs = avio_tell(pb);
  620. if (!pb->seekable)
  621. break;
  622. avio_skip(pb, size - 24);
  623. } else if (!memcmp(guid, ff_w64_guid_summarylist, 16)) {
  624. int64_t start, end, cur;
  625. uint32_t count, chunk_size, i;
  626. start = avio_tell(pb);
  627. end = start + FFALIGN(size, INT64_C(8)) - 24;
  628. count = avio_rl32(pb);
  629. for (i = 0; i < count; i++) {
  630. char chunk_key[5], *value;
  631. if (avio_feof(pb) || (cur = avio_tell(pb)) < 0 || cur > end - 8 /* = tag + size */)
  632. break;
  633. chunk_key[4] = 0;
  634. avio_read(pb, chunk_key, 4);
  635. chunk_size = avio_rl32(pb);
  636. value = av_mallocz(chunk_size + 1);
  637. if (!value)
  638. return AVERROR(ENOMEM);
  639. ret = avio_get_str16le(pb, chunk_size, value, chunk_size);
  640. avio_skip(pb, chunk_size - ret);
  641. av_dict_set(&s->metadata, chunk_key, value, AV_DICT_DONT_STRDUP_VAL);
  642. }
  643. avio_skip(pb, end - avio_tell(pb));
  644. } else {
  645. av_log(s, AV_LOG_DEBUG, "unknown guid: "FF_PRI_GUID"\n", FF_ARG_GUID(guid));
  646. avio_skip(pb, FFALIGN(size, INT64_C(8)) - 24);
  647. }
  648. }
  649. if (!data_ofs)
  650. return AVERROR_EOF;
  651. ff_metadata_conv_ctx(s, NULL, wav_metadata_conv);
  652. ff_metadata_conv_ctx(s, NULL, ff_riff_info_conv);
  653. handle_stream_probing(st);
  654. st->need_parsing = AVSTREAM_PARSE_FULL_RAW;
  655. avio_seek(pb, data_ofs, SEEK_SET);
  656. return 0;
  657. }
  658. AVInputFormat ff_w64_demuxer = {
  659. .name = "w64",
  660. .long_name = NULL_IF_CONFIG_SMALL("Sony Wave64"),
  661. .priv_data_size = sizeof(WAVDemuxContext),
  662. .read_probe = w64_probe,
  663. .read_header = w64_read_header,
  664. .read_packet = wav_read_packet,
  665. .read_seek = wav_read_seek,
  666. .flags = AVFMT_GENERIC_INDEX,
  667. .codec_tag = (const AVCodecTag * const []) { ff_codec_wav_tags, 0 },
  668. };
  669. #endif /* CONFIG_W64_DEMUXER */