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.

442 lines
13KB

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