wav.c 17 KB

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