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.

345 lines
9.4KB

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