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.

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