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.

914 lines
29KB

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