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.

286 lines
7.2KB

  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 "allformats.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. return AVPROBE_SCORE_MAX;
  131. else
  132. return 0;
  133. }
  134. /* wav input */
  135. static int wav_read_header(AVFormatContext *s,
  136. AVFormatParameters *ap)
  137. {
  138. int size;
  139. unsigned int tag;
  140. ByteIOContext *pb = &s->pb;
  141. AVStream *st;
  142. WAVContext *wav = s->priv_data;
  143. /* check RIFF header */
  144. tag = get_le32(pb);
  145. if (tag != MKTAG('R', 'I', 'F', 'F'))
  146. return -1;
  147. get_le32(pb); /* file size */
  148. tag = get_le32(pb);
  149. if (tag != MKTAG('W', 'A', 'V', 'E'))
  150. return -1;
  151. /* parse fmt header */
  152. size = find_tag(pb, MKTAG('f', 'm', 't', ' '));
  153. if (size < 0)
  154. return -1;
  155. st = av_new_stream(s, 0);
  156. if (!st)
  157. return AVERROR_NOMEM;
  158. get_wav_header(pb, st->codec, size);
  159. st->need_parsing = 1;
  160. av_set_pts_info(st, 64, 1, st->codec->sample_rate);
  161. size = find_tag(pb, MKTAG('d', 'a', 't', 'a'));
  162. if (size < 0)
  163. return -1;
  164. wav->data_end= url_ftell(pb) + size;
  165. return 0;
  166. }
  167. #define MAX_SIZE 4096
  168. static int wav_read_packet(AVFormatContext *s,
  169. AVPacket *pkt)
  170. {
  171. int ret, size, left;
  172. AVStream *st;
  173. WAVContext *wav = s->priv_data;
  174. if (url_feof(&s->pb))
  175. return AVERROR_IO;
  176. st = s->streams[0];
  177. left= wav->data_end - url_ftell(&s->pb);
  178. if(left <= 0){
  179. left = find_tag(&(s->pb), MKTAG('d', 'a', 't', 'a'));
  180. if (left < 0) {
  181. return AVERROR_IO;
  182. }
  183. wav->data_end= url_ftell(&s->pb) + left;
  184. }
  185. size = MAX_SIZE;
  186. if (st->codec->block_align > 1) {
  187. if (size < st->codec->block_align)
  188. size = st->codec->block_align;
  189. size = (size / st->codec->block_align) * st->codec->block_align;
  190. }
  191. size= FFMIN(size, left);
  192. ret= av_get_packet(&s->pb, pkt, size);
  193. if (ret <= 0)
  194. return AVERROR_IO;
  195. pkt->stream_index = 0;
  196. /* note: we need to modify the packet size here to handle the last
  197. packet */
  198. pkt->size = ret;
  199. return ret;
  200. }
  201. static int wav_read_close(AVFormatContext *s)
  202. {
  203. return 0;
  204. }
  205. static int wav_read_seek(AVFormatContext *s,
  206. int stream_index, int64_t timestamp, int flags)
  207. {
  208. AVStream *st;
  209. st = s->streams[0];
  210. switch(st->codec->codec_id) {
  211. case CODEC_ID_MP2:
  212. case CODEC_ID_MP3:
  213. case CODEC_ID_AC3:
  214. case CODEC_ID_DTS:
  215. /* use generic seeking with dynamically generated indexes */
  216. return -1;
  217. default:
  218. break;
  219. }
  220. return pcm_read_seek(s, stream_index, timestamp, flags);
  221. }
  222. #ifdef CONFIG_WAV_DEMUXER
  223. AVInputFormat wav_demuxer = {
  224. "wav",
  225. "wav format",
  226. sizeof(WAVContext),
  227. wav_probe,
  228. wav_read_header,
  229. wav_read_packet,
  230. wav_read_close,
  231. wav_read_seek,
  232. .flags= AVFMT_GENERIC_INDEX,
  233. .codec_tag= (const AVCodecTag*[]){codec_wav_tags, 0},
  234. };
  235. #endif
  236. #ifdef CONFIG_WAV_MUXER
  237. AVOutputFormat wav_muxer = {
  238. "wav",
  239. "wav format",
  240. "audio/x-wav",
  241. "wav",
  242. sizeof(WAVContext),
  243. CODEC_ID_PCM_S16LE,
  244. CODEC_ID_NONE,
  245. wav_write_header,
  246. wav_write_packet,
  247. wav_write_trailer,
  248. .codec_tag= (const AVCodecTag*[]){codec_wav_tags, 0},
  249. };
  250. #endif