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.

767 lines
23KB

  1. /*
  2. * WAV muxer and 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 "libavutil/avassert.h"
  26. #include "libavutil/dict.h"
  27. #include "libavutil/log.h"
  28. #include "libavutil/mathematics.h"
  29. #include "libavutil/opt.h"
  30. #include "avformat.h"
  31. #include "avio_internal.h"
  32. #include "pcm.h"
  33. #include "riff.h"
  34. #include "avio.h"
  35. #include "avio_internal.h"
  36. #include "metadata.h"
  37. typedef struct {
  38. const AVClass *class;
  39. int64_t data;
  40. int64_t data_end;
  41. int64_t minpts;
  42. int64_t maxpts;
  43. int last_duration;
  44. int w64;
  45. int write_bext;
  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. } WAVContext;
  54. #if CONFIG_WAV_MUXER
  55. static inline void bwf_write_bext_string(AVFormatContext *s, const char *key, int maxlen)
  56. {
  57. AVDictionaryEntry *tag;
  58. int len = 0;
  59. if (tag = av_dict_get(s->metadata, key, NULL, 0)) {
  60. len = strlen(tag->value);
  61. len = FFMIN(len, maxlen);
  62. avio_write(s->pb, tag->value, len);
  63. }
  64. ffio_fill(s->pb, 0, maxlen - len);
  65. }
  66. static void bwf_write_bext_chunk(AVFormatContext *s)
  67. {
  68. AVDictionaryEntry *tmp_tag;
  69. uint64_t time_reference = 0;
  70. int64_t bext = ff_start_tag(s->pb, "bext");
  71. bwf_write_bext_string(s, "description", 256);
  72. bwf_write_bext_string(s, "originator", 32);
  73. bwf_write_bext_string(s, "originator_reference", 32);
  74. bwf_write_bext_string(s, "origination_date", 10);
  75. bwf_write_bext_string(s, "origination_time", 8);
  76. if (tmp_tag = av_dict_get(s->metadata, "time_reference", NULL, 0))
  77. time_reference = strtoll(tmp_tag->value, NULL, 10);
  78. avio_wl64(s->pb, time_reference);
  79. avio_wl16(s->pb, 1); // set version to 1
  80. if (tmp_tag = av_dict_get(s->metadata, "umid", NULL, 0)) {
  81. unsigned char umidpart_str[17] = {0};
  82. int i;
  83. uint64_t umidpart;
  84. int len = strlen(tmp_tag->value+2);
  85. for (i = 0; i < len/16; i++) {
  86. memcpy(umidpart_str, tmp_tag->value + 2 + (i*16), 16);
  87. umidpart = strtoll(umidpart_str, NULL, 16);
  88. avio_wb64(s->pb, umidpart);
  89. }
  90. ffio_fill(s->pb, 0, 64 - i*8);
  91. } else
  92. ffio_fill(s->pb, 0, 64); // zero UMID
  93. ffio_fill(s->pb, 0, 190); // Reserved
  94. if (tmp_tag = av_dict_get(s->metadata, "coding_history", NULL, 0))
  95. avio_put_str(s->pb, tmp_tag->value);
  96. ff_end_tag(s->pb, bext);
  97. }
  98. static int wav_write_header(AVFormatContext *s)
  99. {
  100. WAVContext *wav = s->priv_data;
  101. AVIOContext *pb = s->pb;
  102. int64_t fmt, fact;
  103. ffio_wfourcc(pb, "RIFF");
  104. avio_wl32(pb, 0); /* file length */
  105. ffio_wfourcc(pb, "WAVE");
  106. /* format header */
  107. fmt = ff_start_tag(pb, "fmt ");
  108. if (ff_put_wav_header(pb, s->streams[0]->codec) < 0) {
  109. av_log(s, AV_LOG_ERROR, "%s codec not supported in WAVE format\n",
  110. s->streams[0]->codec->codec ? s->streams[0]->codec->codec->name : "NONE");
  111. return -1;
  112. }
  113. ff_end_tag(pb, fmt);
  114. if (s->streams[0]->codec->codec_tag != 0x01 /* hence for all other than PCM */
  115. && s->pb->seekable) {
  116. fact = ff_start_tag(pb, "fact");
  117. avio_wl32(pb, 0);
  118. ff_end_tag(pb, fact);
  119. }
  120. if (wav->write_bext)
  121. bwf_write_bext_chunk(s);
  122. av_set_pts_info(s->streams[0], 64, 1, s->streams[0]->codec->sample_rate);
  123. wav->maxpts = wav->last_duration = 0;
  124. wav->minpts = INT64_MAX;
  125. /* data header */
  126. wav->data = ff_start_tag(pb, "data");
  127. avio_flush(pb);
  128. return 0;
  129. }
  130. static int wav_write_packet(AVFormatContext *s, AVPacket *pkt)
  131. {
  132. AVIOContext *pb = s->pb;
  133. WAVContext *wav = s->priv_data;
  134. avio_write(pb, pkt->data, pkt->size);
  135. if(pkt->pts != AV_NOPTS_VALUE) {
  136. wav->minpts = FFMIN(wav->minpts, pkt->pts);
  137. wav->maxpts = FFMAX(wav->maxpts, pkt->pts);
  138. wav->last_duration = pkt->duration;
  139. } else
  140. av_log(s, AV_LOG_ERROR, "wav_write_packet: NOPTS\n");
  141. return 0;
  142. }
  143. static int wav_write_trailer(AVFormatContext *s)
  144. {
  145. AVIOContext *pb = s->pb;
  146. WAVContext *wav = s->priv_data;
  147. int64_t file_size;
  148. avio_flush(pb);
  149. if (s->pb->seekable) {
  150. ff_end_tag(pb, wav->data);
  151. /* update file size */
  152. file_size = avio_tell(pb);
  153. avio_seek(pb, 4, SEEK_SET);
  154. avio_wl32(pb, (uint32_t)(file_size - 8));
  155. avio_seek(pb, file_size, SEEK_SET);
  156. avio_flush(pb);
  157. if(s->streams[0]->codec->codec_tag != 0x01) {
  158. /* Update num_samps in fact chunk */
  159. int number_of_samples;
  160. number_of_samples = av_rescale(wav->maxpts - wav->minpts + wav->last_duration,
  161. s->streams[0]->codec->sample_rate * (int64_t)s->streams[0]->time_base.num,
  162. s->streams[0]->time_base.den);
  163. avio_seek(pb, wav->data-12, SEEK_SET);
  164. avio_wl32(pb, number_of_samples);
  165. avio_seek(pb, file_size, SEEK_SET);
  166. avio_flush(pb);
  167. }
  168. }
  169. return 0;
  170. }
  171. #define OFFSET(x) offsetof(WAVContext, x)
  172. #define ENC AV_OPT_FLAG_ENCODING_PARAM
  173. static const AVOption options[] = {
  174. { "write_bext", "Write BEXT chunk.", OFFSET(write_bext), AV_OPT_TYPE_INT, { 0 }, 0, 1, ENC },
  175. { NULL },
  176. };
  177. static const AVClass wav_muxer_class = {
  178. .class_name = "WAV muxer",
  179. .item_name = av_default_item_name,
  180. .option = options,
  181. .version = LIBAVUTIL_VERSION_INT,
  182. };
  183. AVOutputFormat ff_wav_muxer = {
  184. .name = "wav",
  185. .long_name = NULL_IF_CONFIG_SMALL("WAV format"),
  186. .mime_type = "audio/x-wav",
  187. .extensions = "wav",
  188. .priv_data_size = sizeof(WAVContext),
  189. .audio_codec = CODEC_ID_PCM_S16LE,
  190. .video_codec = CODEC_ID_NONE,
  191. .write_header = wav_write_header,
  192. .write_packet = wav_write_packet,
  193. .write_trailer = wav_write_trailer,
  194. .codec_tag= (const AVCodecTag* const []){ff_codec_wav_tags, 0},
  195. .priv_class = &wav_muxer_class,
  196. };
  197. #endif /* CONFIG_WAV_MUXER */
  198. #if CONFIG_WAV_DEMUXER
  199. static int64_t next_tag(AVIOContext *pb, unsigned int *tag)
  200. {
  201. *tag = avio_rl32(pb);
  202. return avio_rl32(pb);
  203. }
  204. /* return the size of the found tag */
  205. static int64_t find_tag(AVIOContext *pb, uint32_t tag1)
  206. {
  207. unsigned int tag;
  208. int64_t size;
  209. for (;;) {
  210. if (url_feof(pb))
  211. return -1;
  212. size = next_tag(pb, &tag);
  213. if (tag == tag1)
  214. break;
  215. avio_skip(pb, size);
  216. }
  217. return size;
  218. }
  219. static int wav_probe(AVProbeData *p)
  220. {
  221. /* check file header */
  222. if (p->buf_size <= 32)
  223. return 0;
  224. if (!memcmp(p->buf + 8, "WAVE", 4)) {
  225. if (!memcmp(p->buf, "RIFF", 4))
  226. /*
  227. Since ACT demuxer has standard WAV header at top of it's own,
  228. returning score is decreased to avoid probe conflict
  229. between ACT and WAV.
  230. */
  231. return AVPROBE_SCORE_MAX - 1;
  232. else if (!memcmp(p->buf, "RF64", 4) &&
  233. !memcmp(p->buf + 12, "ds64", 4))
  234. return AVPROBE_SCORE_MAX;
  235. }
  236. return 0;
  237. }
  238. static int wav_parse_fmt_tag(AVFormatContext *s, int64_t size, AVStream **st)
  239. {
  240. AVIOContext *pb = s->pb;
  241. int ret;
  242. /* parse fmt header */
  243. *st = avformat_new_stream(s, NULL);
  244. if (!*st)
  245. return AVERROR(ENOMEM);
  246. ret = ff_get_wav_header(pb, (*st)->codec, size);
  247. if (ret < 0)
  248. return ret;
  249. (*st)->need_parsing = AVSTREAM_PARSE_FULL;
  250. av_set_pts_info(*st, 64, 1, (*st)->codec->sample_rate);
  251. return 0;
  252. }
  253. static inline int wav_parse_bext_string(AVFormatContext *s, const char *key,
  254. int length)
  255. {
  256. char temp[257];
  257. int ret;
  258. av_assert0(length <= sizeof(temp));
  259. if ((ret = avio_read(s->pb, temp, length)) < 0)
  260. return ret;
  261. temp[length] = 0;
  262. if (strlen(temp))
  263. return av_dict_set(&s->metadata, key, temp, 0);
  264. return 0;
  265. }
  266. static int wav_parse_bext_tag(AVFormatContext *s, int64_t size)
  267. {
  268. char temp[131], *coding_history;
  269. int ret, x;
  270. uint64_t time_reference;
  271. int64_t umid_parts[8], umid_mask = 0;
  272. if ((ret = wav_parse_bext_string(s, "description", 256)) < 0 ||
  273. (ret = wav_parse_bext_string(s, "originator", 32)) < 0 ||
  274. (ret = wav_parse_bext_string(s, "originator_reference", 32)) < 0 ||
  275. (ret = wav_parse_bext_string(s, "origination_date", 10)) < 0 ||
  276. (ret = wav_parse_bext_string(s, "origination_time", 8)) < 0)
  277. return ret;
  278. time_reference = avio_rl64(s->pb);
  279. snprintf(temp, sizeof(temp), "%"PRIu64, time_reference);
  280. if ((ret = av_dict_set(&s->metadata, "time_reference", temp, 0)) < 0)
  281. return ret;
  282. /* check if version is >= 1, in which case an UMID may be present */
  283. if (avio_rl16(s->pb) >= 1) {
  284. for (x = 0; x < 8; x++)
  285. umid_mask |= umid_parts[x] = avio_rb64(s->pb);
  286. if (umid_mask) {
  287. /* the string formatting below is per SMPTE 330M-2004 Annex C */
  288. if (umid_parts[4] == 0 && umid_parts[5] == 0 && umid_parts[6] == 0 && umid_parts[7] == 0) {
  289. /* basic UMID */
  290. snprintf(temp, sizeof(temp), "0x%016"PRIX64"%016"PRIX64"%016"PRIX64"%016"PRIX64,
  291. umid_parts[0], umid_parts[1], umid_parts[2], umid_parts[3]);
  292. } else {
  293. /* extended UMID */
  294. snprintf(temp, sizeof(temp), "0x%016"PRIX64"%016"PRIX64"%016"PRIX64"%016"PRIX64
  295. "0x%016"PRIX64"%016"PRIX64"%016"PRIX64"%016"PRIX64,
  296. umid_parts[0], umid_parts[1], umid_parts[2], umid_parts[3],
  297. umid_parts[4], umid_parts[5], umid_parts[6], umid_parts[7]);
  298. }
  299. if ((ret = av_dict_set(&s->metadata, "umid", temp, 0)) < 0)
  300. return ret;
  301. }
  302. avio_skip(s->pb, 190);
  303. } else
  304. avio_skip(s->pb, 254);
  305. if (size > 602) {
  306. /* CodingHistory present */
  307. size -= 602;
  308. if (!(coding_history = av_malloc(size+1)))
  309. return AVERROR(ENOMEM);
  310. if ((ret = avio_read(s->pb, coding_history, size)) < 0)
  311. return ret;
  312. coding_history[size] = 0;
  313. if ((ret = av_dict_set(&s->metadata, "coding_history", coding_history,
  314. AV_DICT_DONT_STRDUP_VAL)) < 0)
  315. return ret;
  316. }
  317. return 0;
  318. }
  319. static const AVMetadataConv wav_metadata_conv[] = {
  320. {"description", "comment" },
  321. {"originator", "encoded_by" },
  322. {"origination_date", "date" },
  323. {"origination_time", "creation_time"},
  324. {0},
  325. };
  326. /* wav input */
  327. static int wav_read_header(AVFormatContext *s,
  328. AVFormatParameters *ap)
  329. {
  330. int64_t size, av_uninit(data_size);
  331. int64_t sample_count=0;
  332. int rf64;
  333. unsigned int tag;
  334. AVIOContext *pb = s->pb;
  335. AVStream *st = NULL;
  336. WAVContext *wav = s->priv_data;
  337. int ret, got_fmt = 0;
  338. int64_t next_tag_ofs, data_ofs = -1;
  339. wav->smv_data_ofs = -1;
  340. /* check RIFF header */
  341. tag = avio_rl32(pb);
  342. rf64 = tag == MKTAG('R', 'F', '6', '4');
  343. if (!rf64 && tag != MKTAG('R', 'I', 'F', 'F'))
  344. return -1;
  345. avio_rl32(pb); /* file size */
  346. tag = avio_rl32(pb);
  347. if (tag != MKTAG('W', 'A', 'V', 'E'))
  348. return -1;
  349. if (rf64) {
  350. if (avio_rl32(pb) != MKTAG('d', 's', '6', '4'))
  351. return -1;
  352. size = avio_rl32(pb);
  353. if (size < 24)
  354. return -1;
  355. avio_rl64(pb); /* RIFF size */
  356. data_size = avio_rl64(pb);
  357. sample_count = avio_rl64(pb);
  358. if (data_size < 0 || sample_count < 0) {
  359. av_log(s, AV_LOG_ERROR, "negative data_size and/or sample_count in "
  360. "ds64: data_size = %"PRId64", sample_count = %"PRId64"\n",
  361. data_size, sample_count);
  362. return AVERROR_INVALIDDATA;
  363. }
  364. avio_skip(pb, size - 24); /* skip rest of ds64 chunk */
  365. }
  366. for (;;) {
  367. AVStream *vst;
  368. size = next_tag(pb, &tag);
  369. next_tag_ofs = avio_tell(pb) + size;
  370. if (url_feof(pb))
  371. break;
  372. switch (tag) {
  373. case MKTAG('f', 'm', 't', ' '):
  374. /* only parse the first 'fmt ' tag found */
  375. if (!got_fmt && (ret = wav_parse_fmt_tag(s, size, &st) < 0)) {
  376. return ret;
  377. } else if (got_fmt)
  378. av_log(s, AV_LOG_WARNING, "found more than one 'fmt ' tag\n");
  379. got_fmt = 1;
  380. break;
  381. case MKTAG('d', 'a', 't', 'a'):
  382. if (!got_fmt) {
  383. av_log(s, AV_LOG_ERROR, "found no 'fmt ' tag before the 'data' tag\n");
  384. return AVERROR_INVALIDDATA;
  385. }
  386. if (rf64) {
  387. next_tag_ofs = wav->data_end = avio_tell(pb) + data_size;
  388. } else {
  389. data_size = size;
  390. next_tag_ofs = wav->data_end = size ? next_tag_ofs : INT64_MAX;
  391. }
  392. data_ofs = avio_tell(pb);
  393. /* don't look for footer metadata if we can't seek or if we don't
  394. * know where the data tag ends
  395. */
  396. if (!pb->seekable || (!rf64 && !size))
  397. goto break_loop;
  398. break;
  399. case MKTAG('f','a','c','t'):
  400. if (!sample_count)
  401. sample_count = avio_rl32(pb);
  402. break;
  403. case MKTAG('b','e','x','t'):
  404. if ((ret = wav_parse_bext_tag(s, size)) < 0)
  405. return ret;
  406. break;
  407. case MKTAG('S','M','V','0'):
  408. // SMV file, a wav file with video appended.
  409. if (size != MKTAG('0','2','0','0')) {
  410. av_log(s, AV_LOG_ERROR, "Unknown SMV version found\n");
  411. goto break_loop;
  412. }
  413. av_log(s, AV_LOG_DEBUG, "Found SMV data\n");
  414. vst = avformat_new_stream(s, NULL);
  415. if (!vst)
  416. return AVERROR(ENOMEM);
  417. avio_r8(pb);
  418. vst->id = 1;
  419. vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  420. vst->codec->codec_id = CODEC_ID_MJPEG;
  421. vst->codec->width = avio_rl24(pb);
  422. vst->codec->height = avio_rl24(pb);
  423. size = avio_rl24(pb);
  424. wav->smv_data_ofs = avio_tell(pb) + (size - 5) * 3;
  425. avio_rl24(pb);
  426. wav->smv_block_size = avio_rl24(pb);
  427. av_set_pts_info(vst, 32, 1, avio_rl24(pb));
  428. vst->duration = avio_rl24(pb);
  429. avio_rl24(pb);
  430. avio_rl24(pb);
  431. wav->smv_frames_per_jpeg = avio_rl24(pb);
  432. goto break_loop;
  433. }
  434. /* seek to next tag unless we know that we'll run into EOF */
  435. if ((avio_size(pb) > 0 && next_tag_ofs >= avio_size(pb)) ||
  436. avio_seek(pb, next_tag_ofs, SEEK_SET) < 0) {
  437. break;
  438. }
  439. }
  440. break_loop:
  441. if (data_ofs < 0) {
  442. av_log(s, AV_LOG_ERROR, "no 'data' tag found\n");
  443. return AVERROR_INVALIDDATA;
  444. }
  445. avio_seek(pb, data_ofs, SEEK_SET);
  446. if (!sample_count && st->codec->channels && av_get_bits_per_sample(st->codec->codec_id))
  447. sample_count = (data_size<<3) / (st->codec->channels * (uint64_t)av_get_bits_per_sample(st->codec->codec_id));
  448. if (sample_count)
  449. st->duration = sample_count;
  450. ff_metadata_conv_ctx(s, NULL, wav_metadata_conv);
  451. return 0;
  452. }
  453. /** Find chunk with w64 GUID by skipping over other chunks
  454. * @return the size of the found chunk
  455. */
  456. static int64_t find_guid(AVIOContext *pb, const uint8_t guid1[16])
  457. {
  458. uint8_t guid[16];
  459. int64_t size;
  460. while (!url_feof(pb)) {
  461. avio_read(pb, guid, 16);
  462. size = avio_rl64(pb);
  463. if (size <= 24)
  464. return -1;
  465. if (!memcmp(guid, guid1, 16))
  466. return size;
  467. avio_skip(pb, FFALIGN(size, INT64_C(8)) - 24);
  468. }
  469. return -1;
  470. }
  471. static const uint8_t guid_data[16] = { 'd', 'a', 't', 'a',
  472. 0xF3, 0xAC, 0xD3, 0x11, 0x8C, 0xD1, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A };
  473. #define MAX_SIZE 4096
  474. static int wav_read_packet(AVFormatContext *s,
  475. AVPacket *pkt)
  476. {
  477. int ret, size;
  478. int64_t left;
  479. AVStream *st;
  480. WAVContext *wav = s->priv_data;
  481. if (wav->smv_data_ofs > 0) {
  482. int64_t audio_dts, video_dts;
  483. smv_retry:
  484. audio_dts = s->streams[0]->cur_dts;
  485. video_dts = s->streams[1]->cur_dts;
  486. if (audio_dts != AV_NOPTS_VALUE && video_dts != AV_NOPTS_VALUE) {
  487. audio_dts = av_rescale_q(audio_dts, s->streams[0]->time_base, AV_TIME_BASE_Q);
  488. video_dts = av_rescale_q(video_dts, s->streams[1]->time_base, AV_TIME_BASE_Q);
  489. wav->smv_last_stream = video_dts >= audio_dts;
  490. }
  491. wav->smv_last_stream = !wav->smv_last_stream;
  492. wav->smv_last_stream |= wav->audio_eof;
  493. wav->smv_last_stream &= !wav->smv_eof;
  494. if (wav->smv_last_stream) {
  495. uint64_t old_pos = avio_tell(s->pb);
  496. uint64_t new_pos = wav->smv_data_ofs +
  497. wav->smv_block * wav->smv_block_size;
  498. if (avio_seek(s->pb, new_pos, SEEK_SET) < 0) {
  499. ret = AVERROR_EOF;
  500. goto smv_out;
  501. }
  502. size = avio_rl24(s->pb);
  503. ret = av_get_packet(s->pb, pkt, size);
  504. if (ret < 0)
  505. goto smv_out;
  506. pkt->pos -= 3;
  507. pkt->pts = wav->smv_block * wav->smv_frames_per_jpeg;
  508. wav->smv_block++;
  509. pkt->stream_index = 1;
  510. smv_out:
  511. avio_seek(s->pb, old_pos, SEEK_SET);
  512. if (ret == AVERROR_EOF) {
  513. wav->smv_eof = 1;
  514. goto smv_retry;
  515. }
  516. return ret;
  517. }
  518. }
  519. st = s->streams[0];
  520. left = wav->data_end - avio_tell(s->pb);
  521. if (left <= 0){
  522. if (CONFIG_W64_DEMUXER && wav->w64)
  523. left = find_guid(s->pb, guid_data) - 24;
  524. else
  525. left = find_tag(s->pb, MKTAG('d', 'a', 't', 'a'));
  526. if (left < 0) {
  527. wav->audio_eof = 1;
  528. if (wav->smv_data_ofs > 0 && !wav->smv_eof)
  529. goto smv_retry;
  530. return AVERROR_EOF;
  531. }
  532. wav->data_end= avio_tell(s->pb) + left;
  533. }
  534. size = MAX_SIZE;
  535. if (st->codec->block_align > 1) {
  536. if (size < st->codec->block_align)
  537. size = st->codec->block_align;
  538. size = (size / st->codec->block_align) * st->codec->block_align;
  539. }
  540. size = FFMIN(size, left);
  541. ret = av_get_packet(s->pb, pkt, size);
  542. if (ret < 0)
  543. return ret;
  544. pkt->stream_index = 0;
  545. return ret;
  546. }
  547. static int wav_read_seek(AVFormatContext *s,
  548. int stream_index, int64_t timestamp, int flags)
  549. {
  550. WAVContext *wav = s->priv_data;
  551. AVStream *st;
  552. wav->smv_eof = 0;
  553. wav->audio_eof = 0;
  554. if (wav->smv_data_ofs > 0) {
  555. int64_t smv_timestamp = timestamp;
  556. if (stream_index == 0)
  557. smv_timestamp = av_rescale_q(timestamp, s->streams[0]->time_base, s->streams[1]->time_base);
  558. else
  559. timestamp = av_rescale_q(smv_timestamp, s->streams[1]->time_base, s->streams[0]->time_base);
  560. wav->smv_block = smv_timestamp / wav->smv_frames_per_jpeg;
  561. }
  562. st = s->streams[0];
  563. switch (st->codec->codec_id) {
  564. case CODEC_ID_MP2:
  565. case CODEC_ID_MP3:
  566. case CODEC_ID_AC3:
  567. case CODEC_ID_DTS:
  568. /* use generic seeking with dynamically generated indexes */
  569. return -1;
  570. default:
  571. break;
  572. }
  573. return pcm_read_seek(s, stream_index, timestamp, flags);
  574. }
  575. AVInputFormat ff_wav_demuxer = {
  576. .name = "wav",
  577. .long_name = NULL_IF_CONFIG_SMALL("WAV format"),
  578. .priv_data_size = sizeof(WAVContext),
  579. .read_probe = wav_probe,
  580. .read_header = wav_read_header,
  581. .read_packet = wav_read_packet,
  582. .read_seek = wav_read_seek,
  583. .flags= AVFMT_GENERIC_INDEX,
  584. .codec_tag= (const AVCodecTag* const []){ff_codec_wav_tags, 0},
  585. };
  586. #endif /* CONFIG_WAV_DEMUXER */
  587. #if CONFIG_W64_DEMUXER
  588. static const uint8_t guid_riff[16] = { 'r', 'i', 'f', 'f',
  589. 0x2E, 0x91, 0xCF, 0x11, 0xA5, 0xD6, 0x28, 0xDB, 0x04, 0xC1, 0x00, 0x00 };
  590. static const uint8_t guid_wave[16] = { 'w', 'a', 'v', 'e',
  591. 0xF3, 0xAC, 0xD3, 0x11, 0x8C, 0xD1, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A };
  592. static const uint8_t guid_fmt [16] = { 'f', 'm', 't', ' ',
  593. 0xF3, 0xAC, 0xD3, 0x11, 0x8C, 0xD1, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A };
  594. static int w64_probe(AVProbeData *p)
  595. {
  596. if (p->buf_size <= 40)
  597. return 0;
  598. if (!memcmp(p->buf, guid_riff, 16) &&
  599. !memcmp(p->buf + 24, guid_wave, 16))
  600. return AVPROBE_SCORE_MAX;
  601. else
  602. return 0;
  603. }
  604. static int w64_read_header(AVFormatContext *s, AVFormatParameters *ap)
  605. {
  606. int64_t size;
  607. AVIOContext *pb = s->pb;
  608. WAVContext *wav = s->priv_data;
  609. AVStream *st;
  610. uint8_t guid[16];
  611. int ret;
  612. avio_read(pb, guid, 16);
  613. if (memcmp(guid, guid_riff, 16))
  614. return -1;
  615. if (avio_rl64(pb) < 16 + 8 + 16 + 8 + 16 + 8) /* riff + wave + fmt + sizes */
  616. return -1;
  617. avio_read(pb, guid, 16);
  618. if (memcmp(guid, guid_wave, 16)) {
  619. av_log(s, AV_LOG_ERROR, "could not find wave guid\n");
  620. return -1;
  621. }
  622. size = find_guid(pb, guid_fmt);
  623. if (size < 0) {
  624. av_log(s, AV_LOG_ERROR, "could not find fmt guid\n");
  625. return -1;
  626. }
  627. st = avformat_new_stream(s, NULL);
  628. if (!st)
  629. return AVERROR(ENOMEM);
  630. /* subtract chunk header size - normal wav file doesn't count it */
  631. ret = ff_get_wav_header(pb, st->codec, size - 24);
  632. if (ret < 0)
  633. return ret;
  634. avio_skip(pb, FFALIGN(size, INT64_C(8)) - size);
  635. st->need_parsing = AVSTREAM_PARSE_FULL;
  636. av_set_pts_info(st, 64, 1, st->codec->sample_rate);
  637. size = find_guid(pb, guid_data);
  638. if (size < 0) {
  639. av_log(s, AV_LOG_ERROR, "could not find data guid\n");
  640. return -1;
  641. }
  642. wav->data_end = avio_tell(pb) + size - 24;
  643. wav->w64 = 1;
  644. return 0;
  645. }
  646. AVInputFormat ff_w64_demuxer = {
  647. .name = "w64",
  648. .long_name = NULL_IF_CONFIG_SMALL("Sony Wave64 format"),
  649. .priv_data_size = sizeof(WAVContext),
  650. .read_probe = w64_probe,
  651. .read_header = w64_read_header,
  652. .read_packet = wav_read_packet,
  653. .read_seek = wav_read_seek,
  654. .flags = AVFMT_GENERIC_INDEX,
  655. .codec_tag = (const AVCodecTag* const []){ff_codec_wav_tags, 0},
  656. };
  657. #endif /* CONFIG_W64_DEMUXER */