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.

418 lines
11KB

  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 "raw.h"
  27. #include "riff.h"
  28. typedef struct {
  29. int64_t data;
  30. int64_t data_end;
  31. int64_t minpts;
  32. int64_t maxpts;
  33. int last_duration;
  34. int w64;
  35. } WAVContext;
  36. #if CONFIG_WAV_MUXER
  37. static int wav_write_header(AVFormatContext *s)
  38. {
  39. WAVContext *wav = s->priv_data;
  40. ByteIOContext *pb = s->pb;
  41. int64_t fmt, fact;
  42. put_tag(pb, "RIFF");
  43. put_le32(pb, 0); /* file length */
  44. put_tag(pb, "WAVE");
  45. /* format header */
  46. fmt = ff_start_tag(pb, "fmt ");
  47. if (ff_put_wav_header(pb, s->streams[0]->codec) < 0) {
  48. av_log(s, AV_LOG_ERROR, "%s codec not supported in WAVE format\n",
  49. s->streams[0]->codec->codec ? s->streams[0]->codec->codec->name : "NONE");
  50. av_free(wav);
  51. return -1;
  52. }
  53. ff_end_tag(pb, fmt);
  54. if (s->streams[0]->codec->codec_tag != 0x01 /* hence for all other than PCM */
  55. && !url_is_streamed(s->pb)) {
  56. fact = ff_start_tag(pb, "fact");
  57. put_le32(pb, 0);
  58. ff_end_tag(pb, fact);
  59. }
  60. av_set_pts_info(s->streams[0], 64, 1, s->streams[0]->codec->sample_rate);
  61. wav->maxpts = wav->last_duration = 0;
  62. wav->minpts = INT64_MAX;
  63. /* data header */
  64. wav->data = ff_start_tag(pb, "data");
  65. put_flush_packet(pb);
  66. return 0;
  67. }
  68. static int wav_write_packet(AVFormatContext *s, AVPacket *pkt)
  69. {
  70. ByteIOContext *pb = s->pb;
  71. WAVContext *wav = s->priv_data;
  72. put_buffer(pb, pkt->data, pkt->size);
  73. if(pkt->pts != AV_NOPTS_VALUE) {
  74. wav->minpts = FFMIN(wav->minpts, pkt->pts);
  75. wav->maxpts = FFMAX(wav->maxpts, pkt->pts);
  76. wav->last_duration = pkt->duration;
  77. } else
  78. av_log(s, AV_LOG_ERROR, "wav_write_packet: NOPTS\n");
  79. return 0;
  80. }
  81. static int wav_write_trailer(AVFormatContext *s)
  82. {
  83. ByteIOContext *pb = s->pb;
  84. WAVContext *wav = s->priv_data;
  85. int64_t file_size;
  86. put_flush_packet(pb);
  87. if (!url_is_streamed(s->pb)) {
  88. ff_end_tag(pb, wav->data);
  89. /* update file size */
  90. file_size = url_ftell(pb);
  91. url_fseek(pb, 4, SEEK_SET);
  92. put_le32(pb, (uint32_t)(file_size - 8));
  93. url_fseek(pb, file_size, SEEK_SET);
  94. put_flush_packet(pb);
  95. if(s->streams[0]->codec->codec_tag != 0x01) {
  96. /* Update num_samps in fact chunk */
  97. int number_of_samples;
  98. number_of_samples = av_rescale(wav->maxpts - wav->minpts + wav->last_duration,
  99. s->streams[0]->codec->sample_rate * (int64_t)s->streams[0]->time_base.num,
  100. s->streams[0]->time_base.den);
  101. url_fseek(pb, wav->data-12, SEEK_SET);
  102. put_le32(pb, number_of_samples);
  103. url_fseek(pb, file_size, SEEK_SET);
  104. put_flush_packet(pb);
  105. }
  106. }
  107. return 0;
  108. }
  109. AVOutputFormat wav_muxer = {
  110. "wav",
  111. NULL_IF_CONFIG_SMALL("WAV format"),
  112. "audio/x-wav",
  113. "wav",
  114. sizeof(WAVContext),
  115. CODEC_ID_PCM_S16LE,
  116. CODEC_ID_NONE,
  117. wav_write_header,
  118. wav_write_packet,
  119. wav_write_trailer,
  120. .codec_tag= (const AVCodecTag* const []){ff_codec_wav_tags, 0},
  121. };
  122. #endif /* CONFIG_WAV_MUXER */
  123. #if CONFIG_WAV_DEMUXER
  124. /* return the size of the found tag */
  125. static int64_t find_tag(ByteIOContext *pb, uint32_t tag1)
  126. {
  127. unsigned int tag;
  128. int64_t size;
  129. for (;;) {
  130. if (url_feof(pb))
  131. return -1;
  132. tag = get_le32(pb);
  133. size = get_le32(pb);
  134. if (tag == tag1)
  135. break;
  136. url_fseek(pb, size, SEEK_CUR);
  137. }
  138. return size;
  139. }
  140. static int wav_probe(AVProbeData *p)
  141. {
  142. /* check file header */
  143. if (p->buf_size <= 32)
  144. return 0;
  145. if (!memcmp(p->buf + 8, "WAVE", 4)) {
  146. if (!memcmp(p->buf, "RIFF", 4))
  147. /*
  148. Since ACT demuxer has standard WAV header at top of it's own,
  149. returning score is decreased to avoid probe conflict
  150. between ACT and WAV.
  151. */
  152. return AVPROBE_SCORE_MAX - 1;
  153. else if (!memcmp(p->buf, "RF64", 4) &&
  154. !memcmp(p->buf + 12, "ds64", 4))
  155. return AVPROBE_SCORE_MAX;
  156. }
  157. return 0;
  158. }
  159. /* wav input */
  160. static int wav_read_header(AVFormatContext *s,
  161. AVFormatParameters *ap)
  162. {
  163. int64_t size, av_uninit(data_size);
  164. int rf64;
  165. unsigned int tag;
  166. ByteIOContext *pb = s->pb;
  167. AVStream *st;
  168. WAVContext *wav = s->priv_data;
  169. /* check RIFF header */
  170. tag = get_le32(pb);
  171. rf64 = tag == MKTAG('R', 'F', '6', '4');
  172. if (!rf64 && tag != MKTAG('R', 'I', 'F', 'F'))
  173. return -1;
  174. get_le32(pb); /* file size */
  175. tag = get_le32(pb);
  176. if (tag != MKTAG('W', 'A', 'V', 'E'))
  177. return -1;
  178. if (rf64) {
  179. if (get_le32(pb) != MKTAG('d', 's', '6', '4'))
  180. return -1;
  181. size = get_le32(pb);
  182. if (size < 16)
  183. return -1;
  184. get_le64(pb); /* RIFF size */
  185. data_size = get_le64(pb);
  186. url_fskip(pb, size - 16); /* skip rest of ds64 chunk */
  187. }
  188. /* parse fmt header */
  189. size = find_tag(pb, MKTAG('f', 'm', 't', ' '));
  190. if (size < 0)
  191. return -1;
  192. st = av_new_stream(s, 0);
  193. if (!st)
  194. return AVERROR(ENOMEM);
  195. ff_get_wav_header(pb, st->codec, size);
  196. st->need_parsing = AVSTREAM_PARSE_FULL;
  197. av_set_pts_info(st, 64, 1, st->codec->sample_rate);
  198. size = find_tag(pb, MKTAG('d', 'a', 't', 'a'));
  199. if (rf64)
  200. size = data_size;
  201. if (size < 0)
  202. return -1;
  203. if (!size) {
  204. wav->data_end = INT64_MAX;
  205. } else
  206. wav->data_end= url_ftell(pb) + size;
  207. return 0;
  208. }
  209. /** Find chunk with w64 GUID by skipping over other chunks
  210. * @return the size of the found chunk
  211. */
  212. static int64_t find_guid(ByteIOContext *pb, const uint8_t guid1[16])
  213. {
  214. uint8_t guid[16];
  215. int64_t size;
  216. while (!url_feof(pb)) {
  217. get_buffer(pb, guid, 16);
  218. size = get_le64(pb);
  219. if (size <= 24)
  220. return -1;
  221. if (!memcmp(guid, guid1, 16))
  222. return size;
  223. url_fskip(pb, FFALIGN(size, INT64_C(8)) - 24);
  224. }
  225. return -1;
  226. }
  227. static const uint8_t guid_data[16] = { 'd', 'a', 't', 'a',
  228. 0xF3, 0xAC, 0xD3, 0x11, 0x8C, 0xD1, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A };
  229. #define MAX_SIZE 4096
  230. static int wav_read_packet(AVFormatContext *s,
  231. AVPacket *pkt)
  232. {
  233. int ret, size;
  234. int64_t left;
  235. AVStream *st;
  236. WAVContext *wav = s->priv_data;
  237. st = s->streams[0];
  238. left = wav->data_end - url_ftell(s->pb);
  239. if (left <= 0){
  240. if (CONFIG_W64_DEMUXER && wav->w64)
  241. left = find_guid(s->pb, guid_data) - 24;
  242. else
  243. left = find_tag(s->pb, MKTAG('d', 'a', 't', 'a'));
  244. if (left < 0)
  245. return AVERROR_EOF;
  246. wav->data_end= url_ftell(s->pb) + left;
  247. }
  248. size = MAX_SIZE;
  249. if (st->codec->block_align > 1) {
  250. if (size < st->codec->block_align)
  251. size = st->codec->block_align;
  252. size = (size / st->codec->block_align) * st->codec->block_align;
  253. }
  254. size = FFMIN(size, left);
  255. ret = av_get_packet(s->pb, pkt, size);
  256. if (ret < 0)
  257. return ret;
  258. pkt->stream_index = 0;
  259. return ret;
  260. }
  261. static int wav_read_seek(AVFormatContext *s,
  262. int stream_index, int64_t timestamp, int flags)
  263. {
  264. AVStream *st;
  265. st = s->streams[0];
  266. switch (st->codec->codec_id) {
  267. case CODEC_ID_MP2:
  268. case CODEC_ID_MP3:
  269. case CODEC_ID_AC3:
  270. case CODEC_ID_DTS:
  271. /* use generic seeking with dynamically generated indexes */
  272. return -1;
  273. default:
  274. break;
  275. }
  276. return pcm_read_seek(s, stream_index, timestamp, flags);
  277. }
  278. AVInputFormat wav_demuxer = {
  279. "wav",
  280. NULL_IF_CONFIG_SMALL("WAV format"),
  281. sizeof(WAVContext),
  282. wav_probe,
  283. wav_read_header,
  284. wav_read_packet,
  285. NULL,
  286. wav_read_seek,
  287. .flags= AVFMT_GENERIC_INDEX,
  288. .codec_tag= (const AVCodecTag* const []){ff_codec_wav_tags, 0},
  289. };
  290. #endif /* CONFIG_WAV_DEMUXER */
  291. #if CONFIG_W64_DEMUXER
  292. static const uint8_t guid_riff[16] = { 'r', 'i', 'f', 'f',
  293. 0x2E, 0x91, 0xCF, 0x11, 0xA5, 0xD6, 0x28, 0xDB, 0x04, 0xC1, 0x00, 0x00 };
  294. static const uint8_t guid_wave[16] = { 'w', 'a', 'v', 'e',
  295. 0xF3, 0xAC, 0xD3, 0x11, 0x8C, 0xD1, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A };
  296. static const uint8_t guid_fmt [16] = { 'f', 'm', 't', ' ',
  297. 0xF3, 0xAC, 0xD3, 0x11, 0x8C, 0xD1, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A };
  298. static int w64_probe(AVProbeData *p)
  299. {
  300. if (p->buf_size <= 40)
  301. return 0;
  302. if (!memcmp(p->buf, guid_riff, 16) &&
  303. !memcmp(p->buf + 24, guid_wave, 16))
  304. return AVPROBE_SCORE_MAX;
  305. else
  306. return 0;
  307. }
  308. static int w64_read_header(AVFormatContext *s, AVFormatParameters *ap)
  309. {
  310. int64_t size;
  311. ByteIOContext *pb = s->pb;
  312. WAVContext *wav = s->priv_data;
  313. AVStream *st;
  314. uint8_t guid[16];
  315. get_buffer(pb, guid, 16);
  316. if (memcmp(guid, guid_riff, 16))
  317. return -1;
  318. if (get_le64(pb) < 16 + 8 + 16 + 8 + 16 + 8) /* riff + wave + fmt + sizes */
  319. return -1;
  320. get_buffer(pb, guid, 16);
  321. if (memcmp(guid, guid_wave, 16)) {
  322. av_log(s, AV_LOG_ERROR, "could not find wave guid\n");
  323. return -1;
  324. }
  325. size = find_guid(pb, guid_fmt);
  326. if (size < 0) {
  327. av_log(s, AV_LOG_ERROR, "could not find fmt guid\n");
  328. return -1;
  329. }
  330. st = av_new_stream(s, 0);
  331. if (!st)
  332. return AVERROR(ENOMEM);
  333. /* subtract chunk header size - normal wav file doesn't count it */
  334. ff_get_wav_header(pb, st->codec, size - 24);
  335. url_fskip(pb, FFALIGN(size, INT64_C(8)) - size);
  336. st->need_parsing = AVSTREAM_PARSE_FULL;
  337. av_set_pts_info(st, 64, 1, st->codec->sample_rate);
  338. size = find_guid(pb, guid_data);
  339. if (size < 0) {
  340. av_log(s, AV_LOG_ERROR, "could not find data guid\n");
  341. return -1;
  342. }
  343. wav->data_end = url_ftell(pb) + size - 24;
  344. wav->w64 = 1;
  345. return 0;
  346. }
  347. AVInputFormat w64_demuxer = {
  348. "w64",
  349. NULL_IF_CONFIG_SMALL("Sony Wave64 format"),
  350. sizeof(WAVContext),
  351. w64_probe,
  352. w64_read_header,
  353. wav_read_packet,
  354. NULL,
  355. wav_read_seek,
  356. .flags = AVFMT_GENERIC_INDEX,
  357. .codec_tag = (const AVCodecTag* const []){ff_codec_wav_tags, 0},
  358. };
  359. #endif /* CONFIG_W64_DEMUXER */