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.

422 lines
12KB

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