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.

450 lines
12KB

  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 Libav.
  10. *
  11. * Libav 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. * Libav 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 Libav; if not, write to the Free Software
  23. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  24. */
  25. #include "libavutil/mathematics.h"
  26. #include "avformat.h"
  27. #include "avio_internal.h"
  28. #include "pcm.h"
  29. #include "riff.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 (pb->eof_reached)
  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. /* wav input */
  165. static int wav_read_header(AVFormatContext *s,
  166. AVFormatParameters *ap)
  167. {
  168. int64_t size, av_uninit(data_size);
  169. int64_t sample_count=0;
  170. int rf64;
  171. unsigned int tag;
  172. AVIOContext *pb = s->pb;
  173. AVStream *st;
  174. WAVContext *wav = s->priv_data;
  175. int ret;
  176. /* check RIFF header */
  177. tag = avio_rl32(pb);
  178. rf64 = tag == MKTAG('R', 'F', '6', '4');
  179. if (!rf64 && tag != MKTAG('R', 'I', 'F', 'F'))
  180. return -1;
  181. avio_rl32(pb); /* file size */
  182. tag = avio_rl32(pb);
  183. if (tag != MKTAG('W', 'A', 'V', 'E'))
  184. return -1;
  185. if (rf64) {
  186. if (avio_rl32(pb) != MKTAG('d', 's', '6', '4'))
  187. return -1;
  188. size = avio_rl32(pb);
  189. if (size < 16)
  190. return -1;
  191. avio_rl64(pb); /* RIFF size */
  192. data_size = avio_rl64(pb);
  193. sample_count = avio_rl64(pb);
  194. avio_skip(pb, size - 16); /* skip rest of ds64 chunk */
  195. }
  196. /* parse fmt header */
  197. size = find_tag(pb, MKTAG('f', 'm', 't', ' '));
  198. if (size < 0)
  199. return -1;
  200. st = av_new_stream(s, 0);
  201. if (!st)
  202. return AVERROR(ENOMEM);
  203. ret = ff_get_wav_header(pb, st->codec, size);
  204. if (ret < 0)
  205. return ret;
  206. st->need_parsing = AVSTREAM_PARSE_FULL;
  207. av_set_pts_info(st, 64, 1, st->codec->sample_rate);
  208. for (;;) {
  209. if (pb->eof_reached)
  210. return -1;
  211. size = next_tag(pb, &tag);
  212. if (tag == MKTAG('d', 'a', 't', 'a')){
  213. break;
  214. }else if (tag == MKTAG('f','a','c','t') && !sample_count){
  215. sample_count = avio_rl32(pb);
  216. size -= 4;
  217. }
  218. avio_skip(pb, size);
  219. }
  220. if (rf64)
  221. size = data_size;
  222. if (size < 0)
  223. return -1;
  224. if (!size) {
  225. wav->data_end = INT64_MAX;
  226. } else
  227. wav->data_end= avio_tell(pb) + size;
  228. if (!sample_count && st->codec->channels && av_get_bits_per_sample(st->codec->codec_id))
  229. sample_count = (size<<3) / (st->codec->channels * (uint64_t)av_get_bits_per_sample(st->codec->codec_id));
  230. if (sample_count)
  231. st->duration = sample_count;
  232. return 0;
  233. }
  234. /** Find chunk with w64 GUID by skipping over other chunks
  235. * @return the size of the found chunk
  236. */
  237. static int64_t find_guid(AVIOContext *pb, const uint8_t guid1[16])
  238. {
  239. uint8_t guid[16];
  240. int64_t size;
  241. while (!pb->eof_reached) {
  242. avio_read(pb, guid, 16);
  243. size = avio_rl64(pb);
  244. if (size <= 24)
  245. return -1;
  246. if (!memcmp(guid, guid1, 16))
  247. return size;
  248. avio_skip(pb, FFALIGN(size, INT64_C(8)) - 24);
  249. }
  250. return -1;
  251. }
  252. static const uint8_t guid_data[16] = { 'd', 'a', 't', 'a',
  253. 0xF3, 0xAC, 0xD3, 0x11, 0x8C, 0xD1, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A };
  254. #define MAX_SIZE 4096
  255. static int wav_read_packet(AVFormatContext *s,
  256. AVPacket *pkt)
  257. {
  258. int ret, size;
  259. int64_t left;
  260. AVStream *st;
  261. WAVContext *wav = s->priv_data;
  262. st = s->streams[0];
  263. left = wav->data_end - avio_tell(s->pb);
  264. if (left <= 0){
  265. if (CONFIG_W64_DEMUXER && wav->w64)
  266. left = find_guid(s->pb, guid_data) - 24;
  267. else
  268. left = find_tag(s->pb, MKTAG('d', 'a', 't', 'a'));
  269. if (left < 0)
  270. return AVERROR_EOF;
  271. wav->data_end= avio_tell(s->pb) + left;
  272. }
  273. size = MAX_SIZE;
  274. if (st->codec->block_align > 1) {
  275. if (size < st->codec->block_align)
  276. size = st->codec->block_align;
  277. size = (size / st->codec->block_align) * st->codec->block_align;
  278. }
  279. size = FFMIN(size, left);
  280. ret = av_get_packet(s->pb, pkt, size);
  281. if (ret < 0)
  282. return ret;
  283. pkt->stream_index = 0;
  284. return ret;
  285. }
  286. static int wav_read_seek(AVFormatContext *s,
  287. int stream_index, int64_t timestamp, int flags)
  288. {
  289. AVStream *st;
  290. st = s->streams[0];
  291. switch (st->codec->codec_id) {
  292. case CODEC_ID_MP2:
  293. case CODEC_ID_MP3:
  294. case CODEC_ID_AC3:
  295. case CODEC_ID_DTS:
  296. /* use generic seeking with dynamically generated indexes */
  297. return -1;
  298. default:
  299. break;
  300. }
  301. return pcm_read_seek(s, stream_index, timestamp, flags);
  302. }
  303. AVInputFormat ff_wav_demuxer = {
  304. "wav",
  305. NULL_IF_CONFIG_SMALL("WAV format"),
  306. sizeof(WAVContext),
  307. wav_probe,
  308. wav_read_header,
  309. wav_read_packet,
  310. NULL,
  311. wav_read_seek,
  312. .flags= AVFMT_GENERIC_INDEX,
  313. .codec_tag= (const AVCodecTag* const []){ff_codec_wav_tags, 0},
  314. };
  315. #endif /* CONFIG_WAV_DEMUXER */
  316. #if CONFIG_W64_DEMUXER
  317. static const uint8_t guid_riff[16] = { 'r', 'i', 'f', 'f',
  318. 0x2E, 0x91, 0xCF, 0x11, 0xA5, 0xD6, 0x28, 0xDB, 0x04, 0xC1, 0x00, 0x00 };
  319. static const uint8_t guid_wave[16] = { 'w', 'a', 'v', 'e',
  320. 0xF3, 0xAC, 0xD3, 0x11, 0x8C, 0xD1, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A };
  321. static const uint8_t guid_fmt [16] = { 'f', 'm', 't', ' ',
  322. 0xF3, 0xAC, 0xD3, 0x11, 0x8C, 0xD1, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A };
  323. static int w64_probe(AVProbeData *p)
  324. {
  325. if (p->buf_size <= 40)
  326. return 0;
  327. if (!memcmp(p->buf, guid_riff, 16) &&
  328. !memcmp(p->buf + 24, guid_wave, 16))
  329. return AVPROBE_SCORE_MAX;
  330. else
  331. return 0;
  332. }
  333. static int w64_read_header(AVFormatContext *s, AVFormatParameters *ap)
  334. {
  335. int64_t size;
  336. AVIOContext *pb = s->pb;
  337. WAVContext *wav = s->priv_data;
  338. AVStream *st;
  339. uint8_t guid[16];
  340. int ret;
  341. avio_read(pb, guid, 16);
  342. if (memcmp(guid, guid_riff, 16))
  343. return -1;
  344. if (avio_rl64(pb) < 16 + 8 + 16 + 8 + 16 + 8) /* riff + wave + fmt + sizes */
  345. return -1;
  346. avio_read(pb, guid, 16);
  347. if (memcmp(guid, guid_wave, 16)) {
  348. av_log(s, AV_LOG_ERROR, "could not find wave guid\n");
  349. return -1;
  350. }
  351. size = find_guid(pb, guid_fmt);
  352. if (size < 0) {
  353. av_log(s, AV_LOG_ERROR, "could not find fmt guid\n");
  354. return -1;
  355. }
  356. st = av_new_stream(s, 0);
  357. if (!st)
  358. return AVERROR(ENOMEM);
  359. /* subtract chunk header size - normal wav file doesn't count it */
  360. ret = ff_get_wav_header(pb, st->codec, size - 24);
  361. if (ret < 0)
  362. return ret;
  363. avio_skip(pb, FFALIGN(size, INT64_C(8)) - size);
  364. st->need_parsing = AVSTREAM_PARSE_FULL;
  365. av_set_pts_info(st, 64, 1, st->codec->sample_rate);
  366. size = find_guid(pb, guid_data);
  367. if (size < 0) {
  368. av_log(s, AV_LOG_ERROR, "could not find data guid\n");
  369. return -1;
  370. }
  371. wav->data_end = avio_tell(pb) + size - 24;
  372. wav->w64 = 1;
  373. return 0;
  374. }
  375. AVInputFormat ff_w64_demuxer = {
  376. "w64",
  377. NULL_IF_CONFIG_SMALL("Sony Wave64 format"),
  378. sizeof(WAVContext),
  379. w64_probe,
  380. w64_read_header,
  381. wav_read_packet,
  382. NULL,
  383. wav_read_seek,
  384. .flags = AVFMT_GENERIC_INDEX,
  385. .codec_tag = (const AVCodecTag* const []){ff_codec_wav_tags, 0},
  386. };
  387. #endif /* CONFIG_W64_DEMUXER */