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.

401 lines
11KB

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