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.

314 lines
8.0KB

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