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.

846 lines
26KB

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