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.

343 lines
9.3KB

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