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.

766 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), FF_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 = av_new_stream(s, 0);
  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 = av_new_stream(s, 1);
  415. if (!vst)
  416. return AVERROR(ENOMEM);
  417. avio_r8(pb);
  418. vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  419. vst->codec->codec_id = CODEC_ID_MJPEG;
  420. vst->codec->width = avio_rl24(pb);
  421. vst->codec->height = avio_rl24(pb);
  422. size = avio_rl24(pb);
  423. wav->smv_data_ofs = avio_tell(pb) + (size - 5) * 3;
  424. avio_rl24(pb);
  425. wav->smv_block_size = avio_rl24(pb);
  426. av_set_pts_info(vst, 32, 1, avio_rl24(pb));
  427. vst->duration = avio_rl24(pb);
  428. avio_rl24(pb);
  429. avio_rl24(pb);
  430. wav->smv_frames_per_jpeg = avio_rl24(pb);
  431. goto break_loop;
  432. }
  433. /* seek to next tag unless we know that we'll run into EOF */
  434. if ((avio_size(pb) > 0 && next_tag_ofs >= avio_size(pb)) ||
  435. avio_seek(pb, next_tag_ofs, SEEK_SET) < 0) {
  436. break;
  437. }
  438. }
  439. break_loop:
  440. if (data_ofs < 0) {
  441. av_log(s, AV_LOG_ERROR, "no 'data' tag found\n");
  442. return AVERROR_INVALIDDATA;
  443. }
  444. avio_seek(pb, data_ofs, SEEK_SET);
  445. if (!sample_count && st->codec->channels && av_get_bits_per_sample(st->codec->codec_id))
  446. sample_count = (data_size<<3) / (st->codec->channels * (uint64_t)av_get_bits_per_sample(st->codec->codec_id));
  447. if (sample_count)
  448. st->duration = sample_count;
  449. ff_metadata_conv_ctx(s, NULL, wav_metadata_conv);
  450. return 0;
  451. }
  452. /** Find chunk with w64 GUID by skipping over other chunks
  453. * @return the size of the found chunk
  454. */
  455. static int64_t find_guid(AVIOContext *pb, const uint8_t guid1[16])
  456. {
  457. uint8_t guid[16];
  458. int64_t size;
  459. while (!url_feof(pb)) {
  460. avio_read(pb, guid, 16);
  461. size = avio_rl64(pb);
  462. if (size <= 24)
  463. return -1;
  464. if (!memcmp(guid, guid1, 16))
  465. return size;
  466. avio_skip(pb, FFALIGN(size, INT64_C(8)) - 24);
  467. }
  468. return -1;
  469. }
  470. static const uint8_t guid_data[16] = { 'd', 'a', 't', 'a',
  471. 0xF3, 0xAC, 0xD3, 0x11, 0x8C, 0xD1, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A };
  472. #define MAX_SIZE 4096
  473. static int wav_read_packet(AVFormatContext *s,
  474. AVPacket *pkt)
  475. {
  476. int ret, size;
  477. int64_t left;
  478. AVStream *st;
  479. WAVContext *wav = s->priv_data;
  480. if (wav->smv_data_ofs > 0) {
  481. int64_t audio_dts, video_dts;
  482. smv_retry:
  483. audio_dts = s->streams[0]->cur_dts;
  484. video_dts = s->streams[1]->cur_dts;
  485. if (audio_dts != AV_NOPTS_VALUE && video_dts != AV_NOPTS_VALUE) {
  486. audio_dts = av_rescale_q(audio_dts, s->streams[0]->time_base, AV_TIME_BASE_Q);
  487. video_dts = av_rescale_q(video_dts, s->streams[1]->time_base, AV_TIME_BASE_Q);
  488. wav->smv_last_stream = video_dts >= audio_dts;
  489. }
  490. wav->smv_last_stream = !wav->smv_last_stream;
  491. wav->smv_last_stream |= wav->audio_eof;
  492. wav->smv_last_stream &= !wav->smv_eof;
  493. if (wav->smv_last_stream) {
  494. uint64_t old_pos = avio_tell(s->pb);
  495. uint64_t new_pos = wav->smv_data_ofs +
  496. wav->smv_block * wav->smv_block_size;
  497. if (avio_seek(s->pb, new_pos, SEEK_SET) < 0) {
  498. ret = AVERROR_EOF;
  499. goto smv_out;
  500. }
  501. size = avio_rl24(s->pb);
  502. ret = av_get_packet(s->pb, pkt, size);
  503. if (ret < 0)
  504. goto smv_out;
  505. pkt->pos -= 3;
  506. pkt->pts = wav->smv_block * wav->smv_frames_per_jpeg;
  507. wav->smv_block++;
  508. pkt->stream_index = 1;
  509. smv_out:
  510. avio_seek(s->pb, old_pos, SEEK_SET);
  511. if (ret == AVERROR_EOF) {
  512. wav->smv_eof = 1;
  513. goto smv_retry;
  514. }
  515. return ret;
  516. }
  517. }
  518. st = s->streams[0];
  519. left = wav->data_end - avio_tell(s->pb);
  520. if (left <= 0){
  521. if (CONFIG_W64_DEMUXER && wav->w64)
  522. left = find_guid(s->pb, guid_data) - 24;
  523. else
  524. left = find_tag(s->pb, MKTAG('d', 'a', 't', 'a'));
  525. if (left < 0) {
  526. wav->audio_eof = 1;
  527. if (wav->smv_data_ofs > 0 && !wav->smv_eof)
  528. goto smv_retry;
  529. return AVERROR_EOF;
  530. }
  531. wav->data_end= avio_tell(s->pb) + left;
  532. }
  533. size = MAX_SIZE;
  534. if (st->codec->block_align > 1) {
  535. if (size < st->codec->block_align)
  536. size = st->codec->block_align;
  537. size = (size / st->codec->block_align) * st->codec->block_align;
  538. }
  539. size = FFMIN(size, left);
  540. ret = av_get_packet(s->pb, pkt, size);
  541. if (ret < 0)
  542. return ret;
  543. pkt->stream_index = 0;
  544. return ret;
  545. }
  546. static int wav_read_seek(AVFormatContext *s,
  547. int stream_index, int64_t timestamp, int flags)
  548. {
  549. WAVContext *wav = s->priv_data;
  550. AVStream *st;
  551. wav->smv_eof = 0;
  552. wav->audio_eof = 0;
  553. if (wav->smv_data_ofs > 0) {
  554. int64_t smv_timestamp = timestamp;
  555. if (stream_index == 0)
  556. smv_timestamp = av_rescale_q(timestamp, s->streams[0]->time_base, s->streams[1]->time_base);
  557. else
  558. timestamp = av_rescale_q(smv_timestamp, s->streams[1]->time_base, s->streams[0]->time_base);
  559. wav->smv_block = smv_timestamp / wav->smv_frames_per_jpeg;
  560. }
  561. st = s->streams[0];
  562. switch (st->codec->codec_id) {
  563. case CODEC_ID_MP2:
  564. case CODEC_ID_MP3:
  565. case CODEC_ID_AC3:
  566. case CODEC_ID_DTS:
  567. /* use generic seeking with dynamically generated indexes */
  568. return -1;
  569. default:
  570. break;
  571. }
  572. return pcm_read_seek(s, stream_index, timestamp, flags);
  573. }
  574. AVInputFormat ff_wav_demuxer = {
  575. .name = "wav",
  576. .long_name = NULL_IF_CONFIG_SMALL("WAV format"),
  577. .priv_data_size = sizeof(WAVContext),
  578. .read_probe = wav_probe,
  579. .read_header = wav_read_header,
  580. .read_packet = wav_read_packet,
  581. .read_seek = wav_read_seek,
  582. .flags= AVFMT_GENERIC_INDEX,
  583. .codec_tag= (const AVCodecTag* const []){ff_codec_wav_tags, 0},
  584. };
  585. #endif /* CONFIG_WAV_DEMUXER */
  586. #if CONFIG_W64_DEMUXER
  587. static const uint8_t guid_riff[16] = { 'r', 'i', 'f', 'f',
  588. 0x2E, 0x91, 0xCF, 0x11, 0xA5, 0xD6, 0x28, 0xDB, 0x04, 0xC1, 0x00, 0x00 };
  589. static const uint8_t guid_wave[16] = { 'w', 'a', 'v', 'e',
  590. 0xF3, 0xAC, 0xD3, 0x11, 0x8C, 0xD1, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A };
  591. static const uint8_t guid_fmt [16] = { 'f', 'm', 't', ' ',
  592. 0xF3, 0xAC, 0xD3, 0x11, 0x8C, 0xD1, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A };
  593. static int w64_probe(AVProbeData *p)
  594. {
  595. if (p->buf_size <= 40)
  596. return 0;
  597. if (!memcmp(p->buf, guid_riff, 16) &&
  598. !memcmp(p->buf + 24, guid_wave, 16))
  599. return AVPROBE_SCORE_MAX;
  600. else
  601. return 0;
  602. }
  603. static int w64_read_header(AVFormatContext *s, AVFormatParameters *ap)
  604. {
  605. int64_t size;
  606. AVIOContext *pb = s->pb;
  607. WAVContext *wav = s->priv_data;
  608. AVStream *st;
  609. uint8_t guid[16];
  610. int ret;
  611. avio_read(pb, guid, 16);
  612. if (memcmp(guid, guid_riff, 16))
  613. return -1;
  614. if (avio_rl64(pb) < 16 + 8 + 16 + 8 + 16 + 8) /* riff + wave + fmt + sizes */
  615. return -1;
  616. avio_read(pb, guid, 16);
  617. if (memcmp(guid, guid_wave, 16)) {
  618. av_log(s, AV_LOG_ERROR, "could not find wave guid\n");
  619. return -1;
  620. }
  621. size = find_guid(pb, guid_fmt);
  622. if (size < 0) {
  623. av_log(s, AV_LOG_ERROR, "could not find fmt guid\n");
  624. return -1;
  625. }
  626. st = av_new_stream(s, 0);
  627. if (!st)
  628. return AVERROR(ENOMEM);
  629. /* subtract chunk header size - normal wav file doesn't count it */
  630. ret = ff_get_wav_header(pb, st->codec, size - 24);
  631. if (ret < 0)
  632. return ret;
  633. avio_skip(pb, FFALIGN(size, INT64_C(8)) - size);
  634. st->need_parsing = AVSTREAM_PARSE_FULL;
  635. av_set_pts_info(st, 64, 1, st->codec->sample_rate);
  636. size = find_guid(pb, guid_data);
  637. if (size < 0) {
  638. av_log(s, AV_LOG_ERROR, "could not find data guid\n");
  639. return -1;
  640. }
  641. wav->data_end = avio_tell(pb) + size - 24;
  642. wav->w64 = 1;
  643. return 0;
  644. }
  645. AVInputFormat ff_w64_demuxer = {
  646. .name = "w64",
  647. .long_name = NULL_IF_CONFIG_SMALL("Sony Wave64 format"),
  648. .priv_data_size = sizeof(WAVContext),
  649. .read_probe = w64_probe,
  650. .read_header = w64_read_header,
  651. .read_packet = wav_read_packet,
  652. .read_seek = wav_read_seek,
  653. .flags = AVFMT_GENERIC_INDEX,
  654. .codec_tag = (const AVCodecTag* const []){ff_codec_wav_tags, 0},
  655. };
  656. #endif /* CONFIG_W64_DEMUXER */