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.

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