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.

796 lines
24KB

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