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.

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