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.

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