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.

321 lines
8.5KB

  1. /*
  2. * WAV encoder and decoder
  3. * Copyright (c) 2001 Fabrice Bellard.
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include "avformat.h"
  20. #include "avi.h"
  21. CodecTag codec_wav_tags[] = {
  22. { CODEC_ID_MP2, 0x50 },
  23. { CODEC_ID_MP3LAME, 0x55 },
  24. { CODEC_ID_AC3, 0x2000 },
  25. { CODEC_ID_PCM_S16LE, 0x01 },
  26. { CODEC_ID_PCM_U8, 0x01 }, /* must come after s16le in this list */
  27. { CODEC_ID_PCM_ALAW, 0x06 },
  28. { CODEC_ID_PCM_MULAW, 0x07 },
  29. { CODEC_ID_ADPCM_MS, 0x02 },
  30. { CODEC_ID_ADPCM_IMA_WAV, 0x11 },
  31. { 0, 0 },
  32. };
  33. /* WAVEFORMATEX header */
  34. /* returns the size or -1 on error */
  35. int put_wav_header(ByteIOContext *pb, AVCodecContext *enc)
  36. {
  37. int tag, bps, blkalign, bytespersec;
  38. int hdrsize = 18;
  39. tag = codec_get_tag(codec_wav_tags, enc->codec_id);
  40. if (tag == 0)
  41. return -1;
  42. put_le16(pb, tag);
  43. put_le16(pb, enc->channels);
  44. put_le32(pb, enc->sample_rate);
  45. if (enc->codec_id == CODEC_ID_PCM_U8 ||
  46. enc->codec_id == CODEC_ID_PCM_ALAW ||
  47. enc->codec_id == CODEC_ID_PCM_MULAW) {
  48. bps = 8;
  49. } else if (enc->codec_id == CODEC_ID_MP2 || enc->codec_id == CODEC_ID_MP3LAME) {
  50. bps = 0;
  51. } else if (enc->codec_id == CODEC_ID_ADPCM_IMA_WAV || enc->codec_id == CODEC_ID_ADPCM_MS) {
  52. bps = 4;
  53. } else {
  54. bps = 16;
  55. }
  56. if (enc->codec_id == CODEC_ID_MP2 || enc->codec_id == CODEC_ID_MP3LAME) {
  57. blkalign = 1;
  58. //blkalign = 144 * enc->bit_rate/enc->sample_rate;
  59. } else if (enc->block_align != 0) { /* specified by the codec */
  60. blkalign = enc->block_align;
  61. } else
  62. blkalign = enc->channels*bps >> 3;
  63. if (enc->codec_id == CODEC_ID_PCM_U8 ||
  64. enc->codec_id == CODEC_ID_PCM_S16LE) {
  65. bytespersec = enc->sample_rate * blkalign;
  66. } else {
  67. bytespersec = enc->bit_rate / 8;
  68. }
  69. put_le32(pb, bytespersec); /* bytes per second */
  70. put_le16(pb, blkalign); /* block align */
  71. put_le16(pb, bps); /* bits per sample */
  72. if (enc->codec_id == CODEC_ID_MP3LAME) {
  73. put_le16(pb, 12); /* wav_extra_size */
  74. hdrsize += 12;
  75. put_le16(pb, 1); /* wID */
  76. put_le32(pb, 2); /* fdwFlags */
  77. put_le16(pb, 1152); /* nBlockSize */
  78. put_le16(pb, 1); /* nFramesPerBlock */
  79. put_le16(pb, 1393); /* nCodecDelay */
  80. } else if (enc->codec_id == CODEC_ID_MP2) {
  81. put_le16(pb, 22); /* wav_extra_size */
  82. hdrsize += 22;
  83. put_le16(pb, 2); /* fwHeadLayer */
  84. put_le32(pb, enc->bit_rate); /* dwHeadBitrate */
  85. put_le16(pb, enc->channels == 2 ? 1 : 8); /* fwHeadMode */
  86. put_le16(pb, 0); /* fwHeadModeExt */
  87. put_le16(pb, 1); /* wHeadEmphasis */
  88. put_le16(pb, 16); /* fwHeadFlags */
  89. put_le32(pb, 0); /* dwPTSLow */
  90. put_le32(pb, 0); /* dwPTSHigh */
  91. } else if (enc->codec_id == CODEC_ID_ADPCM_IMA_WAV) {
  92. put_le16(pb, 2); /* wav_extra_size */
  93. put_le16(pb, ((enc->block_align - 4 * enc->channels) / (4 * enc->channels)) * 8 + 1); /* wSamplesPerBlock */
  94. } else
  95. put_le16(pb, 0); /* wav_extra_size */
  96. return hdrsize;
  97. }
  98. int wav_codec_get_id(unsigned int tag, int bps)
  99. {
  100. int id;
  101. id = codec_get_id(codec_wav_tags, tag);
  102. if (id <= 0)
  103. return id;
  104. /* handle specific u8 codec */
  105. if (id == CODEC_ID_PCM_S16LE && bps == 8)
  106. id = CODEC_ID_PCM_U8;
  107. return id;
  108. }
  109. typedef struct {
  110. offset_t data;
  111. } WAVContext;
  112. static int wav_write_header(AVFormatContext *s)
  113. {
  114. WAVContext *wav = s->priv_data;
  115. ByteIOContext *pb = &s->pb;
  116. offset_t fmt;
  117. put_tag(pb, "RIFF");
  118. put_le32(pb, 0); /* file length */
  119. put_tag(pb, "WAVE");
  120. /* format header */
  121. fmt = start_tag(pb, "fmt ");
  122. if (put_wav_header(pb, &s->streams[0]->codec) < 0) {
  123. av_free(wav);
  124. return -1;
  125. }
  126. end_tag(pb, fmt);
  127. /* data header */
  128. wav->data = start_tag(pb, "data");
  129. put_flush_packet(pb);
  130. return 0;
  131. }
  132. static int wav_write_packet(AVFormatContext *s, int stream_index_ptr,
  133. UINT8 *buf, int size, int force_pts)
  134. {
  135. ByteIOContext *pb = &s->pb;
  136. put_buffer(pb, buf, size);
  137. return 0;
  138. }
  139. static int wav_write_trailer(AVFormatContext *s)
  140. {
  141. ByteIOContext *pb = &s->pb;
  142. WAVContext *wav = s->priv_data;
  143. offset_t file_size;
  144. if (!url_is_streamed(&s->pb)) {
  145. end_tag(pb, wav->data);
  146. /* update file size */
  147. file_size = url_ftell(pb);
  148. url_fseek(pb, 4, SEEK_SET);
  149. put_le32(pb, (UINT32)(file_size - 8));
  150. url_fseek(pb, file_size, SEEK_SET);
  151. put_flush_packet(pb);
  152. }
  153. return 0;
  154. }
  155. /* return the size of the found tag */
  156. /* XXX: > 2GB ? */
  157. static int find_tag(ByteIOContext *pb, UINT32 tag1)
  158. {
  159. unsigned int tag;
  160. int size;
  161. for(;;) {
  162. if (url_feof(pb))
  163. return -1;
  164. tag = get_le32(pb);
  165. size = get_le32(pb);
  166. if (tag == tag1)
  167. break;
  168. url_fseek(pb, size, SEEK_CUR);
  169. }
  170. if (size < 0)
  171. size = 0x7fffffff;
  172. return size;
  173. }
  174. static int wav_probe(AVProbeData *p)
  175. {
  176. /* check file header */
  177. if (p->buf_size <= 32)
  178. return 0;
  179. if (p->buf[0] == 'R' && p->buf[1] == 'I' &&
  180. p->buf[2] == 'F' && p->buf[3] == 'F' &&
  181. p->buf[8] == 'W' && p->buf[9] == 'A' &&
  182. p->buf[10] == 'V' && p->buf[11] == 'E')
  183. return AVPROBE_SCORE_MAX;
  184. else
  185. return 0;
  186. }
  187. /* wav input */
  188. static int wav_read_header(AVFormatContext *s,
  189. AVFormatParameters *ap)
  190. {
  191. int size;
  192. unsigned int tag;
  193. ByteIOContext *pb = &s->pb;
  194. unsigned int id, channels, rate, bit_rate, extra_size, bps;
  195. unsigned int blkalign;
  196. AVStream *st;
  197. /* check RIFF header */
  198. tag = get_le32(pb);
  199. if (tag != MKTAG('R', 'I', 'F', 'F'))
  200. return -1;
  201. get_le32(pb); /* file size */
  202. tag = get_le32(pb);
  203. if (tag != MKTAG('W', 'A', 'V', 'E'))
  204. return -1;
  205. /* parse fmt header */
  206. size = find_tag(pb, MKTAG('f', 'm', 't', ' '));
  207. if (size < 0)
  208. return -1;
  209. id = get_le16(pb);
  210. channels = get_le16(pb);
  211. rate = get_le32(pb);
  212. bit_rate = get_le32(pb) * 8;
  213. blkalign = get_le16(pb); /* block align */
  214. bps = get_le16(pb); /* bits per sample */
  215. if (size >= 18) {
  216. /* wav_extra_size */
  217. extra_size = get_le16(pb);
  218. /* skip unused data */
  219. url_fseek(pb, size - 18, SEEK_CUR);
  220. }
  221. size = find_tag(pb, MKTAG('d', 'a', 't', 'a'));
  222. if (size < 0)
  223. return -1;
  224. /* now we are ready: build format streams */
  225. st = av_new_stream(s, 0);
  226. if (!st)
  227. return AVERROR_NOMEM;
  228. st->codec.codec_type = CODEC_TYPE_AUDIO;
  229. st->codec.codec_tag = id;
  230. st->codec.codec_id = wav_codec_get_id(id, bps);
  231. st->codec.channels = channels;
  232. st->codec.sample_rate = rate;
  233. st->codec.block_align = blkalign;
  234. return 0;
  235. }
  236. #define MAX_SIZE 4096
  237. static int wav_read_packet(AVFormatContext *s,
  238. AVPacket *pkt)
  239. {
  240. int ret;
  241. if (url_feof(&s->pb))
  242. return -EIO;
  243. if (av_new_packet(pkt, MAX_SIZE))
  244. return -EIO;
  245. pkt->stream_index = 0;
  246. ret = get_buffer(&s->pb, pkt->data, pkt->size);
  247. if (ret < 0)
  248. av_free_packet(pkt);
  249. /* note: we need to modify the packet size here to handle the last
  250. packet */
  251. pkt->size = ret;
  252. return ret;
  253. }
  254. static int wav_read_close(AVFormatContext *s)
  255. {
  256. return 0;
  257. }
  258. static AVInputFormat wav_iformat = {
  259. "wav",
  260. "wav format",
  261. 0,
  262. wav_probe,
  263. wav_read_header,
  264. wav_read_packet,
  265. wav_read_close,
  266. };
  267. static AVOutputFormat wav_oformat = {
  268. "wav",
  269. "wav format",
  270. "audio/x-wav",
  271. "wav",
  272. sizeof(WAVContext),
  273. CODEC_ID_PCM_S16LE,
  274. CODEC_ID_NONE,
  275. wav_write_header,
  276. wav_write_packet,
  277. wav_write_trailer,
  278. };
  279. int wav_init(void)
  280. {
  281. av_register_input_format(&wav_iformat);
  282. av_register_output_format(&wav_oformat);
  283. return 0;
  284. }