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.

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