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.

347 lines
9.5KB

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