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.

289 lines
9.5KB

  1. /*
  2. * RIFF muxing functions
  3. * Copyright (c) 2000 Fabrice Bellard
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * Libav is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "libavutil/dict.h"
  22. #include "libavutil/log.h"
  23. #include "libavutil/mathematics.h"
  24. #include "libavcodec/avcodec.h"
  25. #include "libavcodec/bytestream.h"
  26. #include "avformat.h"
  27. #include "avio_internal.h"
  28. #include "riff.h"
  29. int64_t ff_start_tag(AVIOContext *pb, const char *tag)
  30. {
  31. ffio_wfourcc(pb, tag);
  32. avio_wl32(pb, 0);
  33. return avio_tell(pb);
  34. }
  35. void ff_end_tag(AVIOContext *pb, int64_t start)
  36. {
  37. int64_t pos;
  38. pos = avio_tell(pb);
  39. avio_seek(pb, start - 4, SEEK_SET);
  40. avio_wl32(pb, (uint32_t)(pos - start));
  41. avio_seek(pb, pos, SEEK_SET);
  42. }
  43. /* WAVEFORMATEX header */
  44. /* returns the size or -1 on error */
  45. int ff_put_wav_header(AVFormatContext *s, AVIOContext *pb,
  46. AVCodecParameters *par)
  47. {
  48. int bps, blkalign, bytespersec, frame_size;
  49. int hdrsize = 18;
  50. int waveformatextensible;
  51. uint8_t temp[256];
  52. uint8_t *riff_extradata = temp;
  53. uint8_t *riff_extradata_start = temp;
  54. if (!par->codec_tag || par->codec_tag > 0xffff)
  55. return -1;
  56. /* We use the known constant frame size for the codec if known, otherwise
  57. * fall back on using AVCodecContext.frame_size, which is not as reliable
  58. * for indicating packet duration. */
  59. frame_size = av_get_audio_frame_duration2(par, par->block_align);
  60. waveformatextensible = (par->channels > 2 && par->channel_layout) ||
  61. par->sample_rate > 48000 ||
  62. av_get_bits_per_sample(par->codec_id) > 16;
  63. if (waveformatextensible)
  64. avio_wl16(pb, 0xfffe);
  65. else
  66. avio_wl16(pb, par->codec_tag);
  67. avio_wl16(pb, par->channels);
  68. avio_wl32(pb, par->sample_rate);
  69. if (par->codec_id == AV_CODEC_ID_MP2 ||
  70. par->codec_id == AV_CODEC_ID_MP3 ||
  71. par->codec_id == AV_CODEC_ID_GSM_MS) {
  72. bps = 0;
  73. } else {
  74. if (!(bps = av_get_bits_per_sample(par->codec_id))) {
  75. if (par->bits_per_coded_sample)
  76. bps = par->bits_per_coded_sample;
  77. else
  78. bps = 16; // default to 16
  79. }
  80. }
  81. if (bps != par->bits_per_coded_sample && par->bits_per_coded_sample) {
  82. av_log(s, AV_LOG_WARNING,
  83. "requested bits_per_coded_sample (%d) "
  84. "and actually stored (%d) differ\n",
  85. par->bits_per_coded_sample, bps);
  86. }
  87. if (par->codec_id == AV_CODEC_ID_MP2) {
  88. blkalign = frame_size;
  89. } else if (par->codec_id == AV_CODEC_ID_MP3) {
  90. blkalign = 576 * (par->sample_rate <= 24000 ? 1 : 2);
  91. } else if (par->codec_id == AV_CODEC_ID_AC3) {
  92. blkalign = 3840; /* maximum bytes per frame */
  93. } else if (par->block_align != 0) { /* specified by the codec */
  94. blkalign = par->block_align;
  95. } else
  96. blkalign = bps * par->channels / av_gcd(8, bps);
  97. if (par->codec_id == AV_CODEC_ID_PCM_U8 ||
  98. par->codec_id == AV_CODEC_ID_PCM_S24LE ||
  99. par->codec_id == AV_CODEC_ID_PCM_S32LE ||
  100. par->codec_id == AV_CODEC_ID_PCM_F32LE ||
  101. par->codec_id == AV_CODEC_ID_PCM_F64LE ||
  102. par->codec_id == AV_CODEC_ID_PCM_S16LE) {
  103. bytespersec = par->sample_rate * blkalign;
  104. } else {
  105. bytespersec = par->bit_rate / 8;
  106. }
  107. avio_wl32(pb, bytespersec); /* bytes per second */
  108. avio_wl16(pb, blkalign); /* block align */
  109. avio_wl16(pb, bps); /* bits per sample */
  110. if (par->codec_id == AV_CODEC_ID_MP3) {
  111. hdrsize += 12;
  112. bytestream_put_le16(&riff_extradata, 1); /* wID */
  113. bytestream_put_le32(&riff_extradata, 2); /* fdwFlags */
  114. bytestream_put_le16(&riff_extradata, 1152); /* nBlockSize */
  115. bytestream_put_le16(&riff_extradata, 1); /* nFramesPerBlock */
  116. bytestream_put_le16(&riff_extradata, 1393); /* nCodecDelay */
  117. } else if (par->codec_id == AV_CODEC_ID_MP2) {
  118. hdrsize += 22;
  119. /* fwHeadLayer */
  120. bytestream_put_le16(&riff_extradata, 2);
  121. /* dwHeadBitrate */
  122. bytestream_put_le32(&riff_extradata, par->bit_rate);
  123. /* fwHeadMode */
  124. bytestream_put_le16(&riff_extradata, par->channels == 2 ? 1 : 8);
  125. /* fwHeadModeExt */
  126. bytestream_put_le16(&riff_extradata, 0);
  127. /* wHeadEmphasis */
  128. bytestream_put_le16(&riff_extradata, 1);
  129. /* fwHeadFlags */
  130. bytestream_put_le16(&riff_extradata, 16);
  131. /* dwPTSLow */
  132. bytestream_put_le32(&riff_extradata, 0);
  133. /* dwPTSHigh */
  134. bytestream_put_le32(&riff_extradata, 0);
  135. } else if (par->codec_id == AV_CODEC_ID_GSM_MS ||
  136. par->codec_id == AV_CODEC_ID_ADPCM_IMA_WAV) {
  137. hdrsize += 2;
  138. /* wSamplesPerBlock */
  139. bytestream_put_le16(&riff_extradata, frame_size);
  140. } else if (par->extradata_size) {
  141. riff_extradata_start = par->extradata;
  142. riff_extradata = par->extradata + par->extradata_size;
  143. hdrsize += par->extradata_size;
  144. }
  145. /* write WAVEFORMATEXTENSIBLE extensions */
  146. if (waveformatextensible) {
  147. hdrsize += 22;
  148. /* 22 is WAVEFORMATEXTENSIBLE size */
  149. avio_wl16(pb, riff_extradata - riff_extradata_start + 22);
  150. /* ValidBitsPerSample || SamplesPerBlock || Reserved */
  151. avio_wl16(pb, bps);
  152. /* dwChannelMask */
  153. avio_wl32(pb, par->channel_layout);
  154. /* GUID + next 3 */
  155. avio_wl32(pb, par->codec_tag);
  156. avio_wl32(pb, 0x00100000);
  157. avio_wl32(pb, 0xAA000080);
  158. avio_wl32(pb, 0x719B3800);
  159. } else {
  160. avio_wl16(pb, riff_extradata - riff_extradata_start); /* cbSize */
  161. }
  162. avio_write(pb, riff_extradata_start, riff_extradata - riff_extradata_start);
  163. if (hdrsize & 1) {
  164. hdrsize++;
  165. avio_w8(pb, 0);
  166. }
  167. return hdrsize;
  168. }
  169. /* BITMAPINFOHEADER header */
  170. void ff_put_bmp_header(AVIOContext *pb, AVCodecParameters *par,
  171. const AVCodecTag *tags, int for_asf)
  172. {
  173. /* size */
  174. avio_wl32(pb, 40 + par->extradata_size);
  175. avio_wl32(pb, par->width);
  176. //We always store RGB TopDown
  177. avio_wl32(pb, par->codec_tag ? par->height : -par->height);
  178. /* planes */
  179. avio_wl16(pb, 1);
  180. /* depth */
  181. avio_wl16(pb, par->bits_per_coded_sample ? par->bits_per_coded_sample : 24);
  182. /* compression type */
  183. avio_wl32(pb, par->codec_tag);
  184. avio_wl32(pb, par->width * par->height * 3);
  185. avio_wl32(pb, 0);
  186. avio_wl32(pb, 0);
  187. avio_wl32(pb, 0);
  188. avio_wl32(pb, 0);
  189. avio_write(pb, par->extradata, par->extradata_size);
  190. if (!for_asf && par->extradata_size & 1)
  191. avio_w8(pb, 0);
  192. }
  193. void ff_parse_specific_params(AVStream *st, int *au_rate,
  194. int *au_ssize, int *au_scale)
  195. {
  196. AVCodecParameters *par = st->codecpar;
  197. int gcd;
  198. int audio_frame_size;
  199. audio_frame_size = av_get_audio_frame_duration2(par, 0);
  200. *au_ssize = par->block_align;
  201. if (audio_frame_size && par->sample_rate) {
  202. *au_scale = audio_frame_size;
  203. *au_rate = par->sample_rate;
  204. } else if (par->codec_type == AVMEDIA_TYPE_VIDEO ||
  205. par->codec_type == AVMEDIA_TYPE_DATA ||
  206. par->codec_type == AVMEDIA_TYPE_SUBTITLE) {
  207. *au_scale = st->time_base.num;
  208. *au_rate = st->time_base.den;
  209. } else {
  210. *au_scale = par->block_align ? par->block_align * 8 : 8;
  211. *au_rate = par->bit_rate ? par->bit_rate :
  212. 8 * par->sample_rate;
  213. }
  214. gcd = av_gcd(*au_scale, *au_rate);
  215. *au_scale /= gcd;
  216. *au_rate /= gcd;
  217. }
  218. void ff_riff_write_info_tag(AVIOContext *pb, const char *tag, const char *str)
  219. {
  220. int len = strlen(str);
  221. if (len > 0) {
  222. len++;
  223. ffio_wfourcc(pb, tag);
  224. avio_wl32(pb, len);
  225. avio_put_str(pb, str);
  226. if (len & 1)
  227. avio_w8(pb, 0);
  228. }
  229. }
  230. static const char riff_tags[][5] = {
  231. "IARL", "IART", "ICMS", "ICMT", "ICOP", "ICRD", "ICRP", "IDIM", "IDPI",
  232. "IENG", "IGNR", "IKEY", "ILGT", "ILNG", "IMED", "INAM", "IPLT", "IPRD",
  233. "IPRT", "ITRK", "ISBJ", "ISFT", "ISHP", "ISMP", "ISRC", "ISRF", "ITCH",
  234. { 0 }
  235. };
  236. static int riff_has_valid_tags(AVFormatContext *s)
  237. {
  238. int i;
  239. for (i = 0; *riff_tags[i]; i++)
  240. if (av_dict_get(s->metadata, riff_tags[i], NULL, AV_DICT_MATCH_CASE))
  241. return 1;
  242. return 0;
  243. }
  244. void ff_riff_write_info(AVFormatContext *s)
  245. {
  246. AVIOContext *pb = s->pb;
  247. int i;
  248. int64_t list_pos;
  249. AVDictionaryEntry *t = NULL;
  250. ff_metadata_conv(&s->metadata, ff_riff_info_conv, NULL);
  251. /* writing empty LIST is not nice and may cause problems */
  252. if (!riff_has_valid_tags(s))
  253. return;
  254. list_pos = ff_start_tag(pb, "LIST");
  255. ffio_wfourcc(pb, "INFO");
  256. for (i = 0; *riff_tags[i]; i++)
  257. if ((t = av_dict_get(s->metadata, riff_tags[i],
  258. NULL, AV_DICT_MATCH_CASE)))
  259. ff_riff_write_info_tag(s->pb, t->key, t->value);
  260. ff_end_tag(pb, list_pos);
  261. }