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.

387 lines
10KB

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