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.

394 lines
11KB

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