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.

396 lines
10KB

  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. st = s->streams[0];
  218. left = wav->data_end - url_ftell(s->pb);
  219. if (left <= 0){
  220. if (CONFIG_W64_DEMUXER && wav->w64)
  221. left = find_guid(s->pb, guid_data) - 24;
  222. else
  223. left = find_tag(s->pb, MKTAG('d', 'a', 't', 'a'));
  224. if (left < 0)
  225. return AVERROR_EOF;
  226. wav->data_end= url_ftell(s->pb) + left;
  227. }
  228. size = MAX_SIZE;
  229. if (st->codec->block_align > 1) {
  230. if (size < st->codec->block_align)
  231. size = st->codec->block_align;
  232. size = (size / st->codec->block_align) * st->codec->block_align;
  233. }
  234. size = FFMIN(size, left);
  235. ret = av_get_packet(s->pb, pkt, size);
  236. if (ret < 0)
  237. return ret;
  238. pkt->stream_index = 0;
  239. return ret;
  240. }
  241. static int wav_read_seek(AVFormatContext *s,
  242. int stream_index, int64_t timestamp, int flags)
  243. {
  244. AVStream *st;
  245. st = s->streams[0];
  246. switch (st->codec->codec_id) {
  247. case CODEC_ID_MP2:
  248. case CODEC_ID_MP3:
  249. case CODEC_ID_AC3:
  250. case CODEC_ID_DTS:
  251. /* use generic seeking with dynamically generated indexes */
  252. return -1;
  253. default:
  254. break;
  255. }
  256. return pcm_read_seek(s, stream_index, timestamp, flags);
  257. }
  258. AVInputFormat wav_demuxer = {
  259. "wav",
  260. NULL_IF_CONFIG_SMALL("WAV format"),
  261. sizeof(WAVContext),
  262. wav_probe,
  263. wav_read_header,
  264. wav_read_packet,
  265. NULL,
  266. wav_read_seek,
  267. .flags= AVFMT_GENERIC_INDEX,
  268. .codec_tag= (const AVCodecTag* const []){ff_codec_wav_tags, 0},
  269. };
  270. #endif /* CONFIG_WAV_DEMUXER */
  271. #if CONFIG_W64_DEMUXER
  272. static const uint8_t guid_riff[16] = { 'r', 'i', 'f', 'f',
  273. 0x2E, 0x91, 0xCF, 0x11, 0xA5, 0xD6, 0x28, 0xDB, 0x04, 0xC1, 0x00, 0x00 };
  274. static const uint8_t guid_wave[16] = { 'w', 'a', 'v', 'e',
  275. 0xF3, 0xAC, 0xD3, 0x11, 0x8C, 0xD1, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A };
  276. static const uint8_t guid_fmt [16] = { 'f', 'm', 't', ' ',
  277. 0xF3, 0xAC, 0xD3, 0x11, 0x8C, 0xD1, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A };
  278. static int w64_probe(AVProbeData *p)
  279. {
  280. if (p->buf_size <= 40)
  281. return 0;
  282. if (!memcmp(p->buf, guid_riff, 16) &&
  283. !memcmp(p->buf + 24, guid_wave, 16))
  284. return AVPROBE_SCORE_MAX;
  285. else
  286. return 0;
  287. }
  288. static int w64_read_header(AVFormatContext *s, AVFormatParameters *ap)
  289. {
  290. int64_t size;
  291. ByteIOContext *pb = s->pb;
  292. WAVContext *wav = s->priv_data;
  293. AVStream *st;
  294. uint8_t guid[16];
  295. get_buffer(pb, guid, 16);
  296. if (memcmp(guid, guid_riff, 16))
  297. return -1;
  298. if (get_le64(pb) < 16 + 8 + 16 + 8 + 16 + 8) /* riff + wave + fmt + sizes */
  299. return -1;
  300. get_buffer(pb, guid, 16);
  301. if (memcmp(guid, guid_wave, 16)) {
  302. av_log(s, AV_LOG_ERROR, "could not find wave guid\n");
  303. return -1;
  304. }
  305. size = find_guid(pb, guid_fmt);
  306. if (size < 0) {
  307. av_log(s, AV_LOG_ERROR, "could not find fmt guid\n");
  308. return -1;
  309. }
  310. st = av_new_stream(s, 0);
  311. if (!st)
  312. return AVERROR(ENOMEM);
  313. /* subtract chunk header size - normal wav file doesn't count it */
  314. ff_get_wav_header(pb, st->codec, size - 24);
  315. url_fskip(pb, FFALIGN(size, INT64_C(8)) - size);
  316. st->need_parsing = AVSTREAM_PARSE_FULL;
  317. av_set_pts_info(st, 64, 1, st->codec->sample_rate);
  318. size = find_guid(pb, guid_data);
  319. if (size < 0) {
  320. av_log(s, AV_LOG_ERROR, "could not find data guid\n");
  321. return -1;
  322. }
  323. wav->data_end = url_ftell(pb) + size - 24;
  324. wav->w64 = 1;
  325. return 0;
  326. }
  327. AVInputFormat w64_demuxer = {
  328. "w64",
  329. NULL_IF_CONFIG_SMALL("Sony Wave64 format"),
  330. sizeof(WAVContext),
  331. w64_probe,
  332. w64_read_header,
  333. wav_read_packet,
  334. NULL,
  335. wav_read_seek,
  336. .flags = AVFMT_GENERIC_INDEX,
  337. .codec_tag = (const AVCodecTag* const []){ff_codec_wav_tags, 0},
  338. };
  339. #endif /* CONFIG_W64_DEMUXER */