wav.c 20 KB

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