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.

921 lines
30KB

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