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.

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