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.

327 lines
8.6KB

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