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.

291 lines
7.4KB

  1. /*
  2. * WAV muxer and demuxer
  3. * Copyright (c) 2001, 2002 Fabrice Bellard.
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "avformat.h"
  22. #include "raw.h"
  23. #include "riff.h"
  24. typedef struct {
  25. offset_t data;
  26. offset_t data_end;
  27. int64_t minpts;
  28. int64_t maxpts;
  29. int last_duration;
  30. } WAVContext;
  31. #ifdef CONFIG_MUXERS
  32. static int wav_write_header(AVFormatContext *s)
  33. {
  34. WAVContext *wav = s->priv_data;
  35. ByteIOContext *pb = s->pb;
  36. offset_t fmt, fact;
  37. put_tag(pb, "RIFF");
  38. put_le32(pb, 0); /* file length */
  39. put_tag(pb, "WAVE");
  40. /* format header */
  41. fmt = start_tag(pb, "fmt ");
  42. if (put_wav_header(pb, s->streams[0]->codec) < 0) {
  43. av_free(wav);
  44. return -1;
  45. }
  46. end_tag(pb, fmt);
  47. if(s->streams[0]->codec->codec_tag != 0x01 /* hence for all other than PCM */
  48. && !url_is_streamed(s->pb)) {
  49. fact = start_tag(pb, "fact");
  50. put_le32(pb, 0);
  51. end_tag(pb, fact);
  52. }
  53. av_set_pts_info(s->streams[0], 64, 1, s->streams[0]->codec->sample_rate);
  54. wav->maxpts = wav->last_duration = 0;
  55. wav->minpts = INT64_MAX;
  56. /* data header */
  57. wav->data = start_tag(pb, "data");
  58. put_flush_packet(pb);
  59. return 0;
  60. }
  61. static int wav_write_packet(AVFormatContext *s, AVPacket *pkt)
  62. {
  63. ByteIOContext *pb = s->pb;
  64. WAVContext *wav = s->priv_data;
  65. put_buffer(pb, pkt->data, pkt->size);
  66. if(pkt->pts != AV_NOPTS_VALUE) {
  67. wav->minpts = FFMIN(wav->minpts, pkt->pts);
  68. wav->maxpts = FFMAX(wav->maxpts, pkt->pts);
  69. wav->last_duration = pkt->duration;
  70. } else
  71. av_log(s, AV_LOG_ERROR, "wav_write_packet: NOPTS\n");
  72. return 0;
  73. }
  74. static int wav_write_trailer(AVFormatContext *s)
  75. {
  76. ByteIOContext *pb = s->pb;
  77. WAVContext *wav = s->priv_data;
  78. offset_t file_size;
  79. if (!url_is_streamed(s->pb)) {
  80. end_tag(pb, wav->data);
  81. /* update file size */
  82. file_size = url_ftell(pb);
  83. url_fseek(pb, 4, SEEK_SET);
  84. put_le32(pb, (uint32_t)(file_size - 8));
  85. url_fseek(pb, file_size, SEEK_SET);
  86. put_flush_packet(pb);
  87. if(s->streams[0]->codec->codec_tag != 0x01) {
  88. /* Update num_samps in fact chunk */
  89. int number_of_samples;
  90. number_of_samples = av_rescale(wav->maxpts - wav->minpts + wav->last_duration,
  91. s->streams[0]->codec->sample_rate * (int64_t)s->streams[0]->time_base.num,
  92. s->streams[0]->time_base.den);
  93. url_fseek(pb, wav->data-12, SEEK_SET);
  94. put_le32(pb, number_of_samples);
  95. url_fseek(pb, file_size, SEEK_SET);
  96. put_flush_packet(pb);
  97. }
  98. }
  99. return 0;
  100. }
  101. #endif //CONFIG_MUXERS
  102. /* return the size of the found tag */
  103. /* XXX: > 2GB ? */
  104. static int find_tag(ByteIOContext *pb, uint32_t tag1)
  105. {
  106. unsigned int tag;
  107. int size;
  108. for(;;) {
  109. if (url_feof(pb))
  110. return -1;
  111. tag = get_le32(pb);
  112. size = get_le32(pb);
  113. if (tag == tag1)
  114. break;
  115. url_fseek(pb, size, SEEK_CUR);
  116. }
  117. if (size < 0)
  118. size = 0x7fffffff;
  119. return size;
  120. }
  121. static int wav_probe(AVProbeData *p)
  122. {
  123. /* check file header */
  124. if (p->buf_size <= 32)
  125. return 0;
  126. if (p->buf[0] == 'R' && p->buf[1] == 'I' &&
  127. p->buf[2] == 'F' && p->buf[3] == 'F' &&
  128. p->buf[8] == 'W' && p->buf[9] == 'A' &&
  129. p->buf[10] == 'V' && p->buf[11] == 'E')
  130. /*
  131. Since ACT demuxer has standard WAV header at top of it's own,
  132. returning score is decreased to avoid probe conflict
  133. between ACT and WAV.
  134. */
  135. return AVPROBE_SCORE_MAX - 1;
  136. else
  137. return 0;
  138. }
  139. /* wav input */
  140. static int wav_read_header(AVFormatContext *s,
  141. AVFormatParameters *ap)
  142. {
  143. int size;
  144. unsigned int tag;
  145. ByteIOContext *pb = s->pb;
  146. AVStream *st;
  147. WAVContext *wav = s->priv_data;
  148. /* check RIFF header */
  149. tag = get_le32(pb);
  150. if (tag != MKTAG('R', 'I', 'F', 'F'))
  151. return -1;
  152. get_le32(pb); /* file size */
  153. tag = get_le32(pb);
  154. if (tag != MKTAG('W', 'A', 'V', 'E'))
  155. return -1;
  156. /* parse fmt header */
  157. size = find_tag(pb, MKTAG('f', 'm', 't', ' '));
  158. if (size < 0)
  159. return -1;
  160. st = av_new_stream(s, 0);
  161. if (!st)
  162. return AVERROR(ENOMEM);
  163. get_wav_header(pb, st->codec, size);
  164. st->need_parsing = AVSTREAM_PARSE_FULL;
  165. av_set_pts_info(st, 64, 1, st->codec->sample_rate);
  166. size = find_tag(pb, MKTAG('d', 'a', 't', 'a'));
  167. if (size < 0)
  168. return -1;
  169. wav->data_end= url_ftell(pb) + size;
  170. return 0;
  171. }
  172. #define MAX_SIZE 4096
  173. static int wav_read_packet(AVFormatContext *s,
  174. AVPacket *pkt)
  175. {
  176. int ret, size, left;
  177. AVStream *st;
  178. WAVContext *wav = s->priv_data;
  179. if (url_feof(s->pb))
  180. return AVERROR(EIO);
  181. st = s->streams[0];
  182. left= wav->data_end - url_ftell(s->pb);
  183. if(left <= 0){
  184. left = find_tag(s->pb, MKTAG('d', 'a', 't', 'a'));
  185. if (left < 0) {
  186. return AVERROR(EIO);
  187. }
  188. wav->data_end= url_ftell(s->pb) + left;
  189. }
  190. size = MAX_SIZE;
  191. if (st->codec->block_align > 1) {
  192. if (size < st->codec->block_align)
  193. size = st->codec->block_align;
  194. size = (size / st->codec->block_align) * st->codec->block_align;
  195. }
  196. size= FFMIN(size, left);
  197. ret= av_get_packet(s->pb, pkt, size);
  198. if (ret <= 0)
  199. return AVERROR(EIO);
  200. pkt->stream_index = 0;
  201. /* note: we need to modify the packet size here to handle the last
  202. packet */
  203. pkt->size = ret;
  204. return ret;
  205. }
  206. static int wav_read_close(AVFormatContext *s)
  207. {
  208. return 0;
  209. }
  210. static int wav_read_seek(AVFormatContext *s,
  211. int stream_index, int64_t timestamp, int flags)
  212. {
  213. AVStream *st;
  214. st = s->streams[0];
  215. switch(st->codec->codec_id) {
  216. case CODEC_ID_MP2:
  217. case CODEC_ID_MP3:
  218. case CODEC_ID_AC3:
  219. case CODEC_ID_DTS:
  220. /* use generic seeking with dynamically generated indexes */
  221. return -1;
  222. default:
  223. break;
  224. }
  225. return pcm_read_seek(s, stream_index, timestamp, flags);
  226. }
  227. #ifdef CONFIG_WAV_DEMUXER
  228. AVInputFormat wav_demuxer = {
  229. "wav",
  230. "wav format",
  231. sizeof(WAVContext),
  232. wav_probe,
  233. wav_read_header,
  234. wav_read_packet,
  235. wav_read_close,
  236. wav_read_seek,
  237. .flags= AVFMT_GENERIC_INDEX,
  238. .codec_tag= (const AVCodecTag*[]){codec_wav_tags, 0},
  239. };
  240. #endif
  241. #ifdef CONFIG_WAV_MUXER
  242. AVOutputFormat wav_muxer = {
  243. "wav",
  244. "wav format",
  245. "audio/x-wav",
  246. "wav",
  247. sizeof(WAVContext),
  248. CODEC_ID_PCM_S16LE,
  249. CODEC_ID_NONE,
  250. wav_write_header,
  251. wav_write_packet,
  252. wav_write_trailer,
  253. .codec_tag= (const AVCodecTag*[]){codec_wav_tags, 0},
  254. };
  255. #endif