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.

882 lines
28KB

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