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.

293 lines
9.8KB

  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(AVIOContext *pb, AVCodecContext *enc)
  46. {
  47. int bps, blkalign, bytespersec, frame_size;
  48. int hdrsize = 18;
  49. int waveformatextensible;
  50. uint8_t temp[256];
  51. uint8_t *riff_extradata = temp;
  52. uint8_t *riff_extradata_start = temp;
  53. if (!enc->codec_tag || enc->codec_tag > 0xffff)
  54. return -1;
  55. /* We use the known constant frame size for the codec if known, otherwise
  56. * fall back on using AVCodecContext.frame_size, which is not as reliable
  57. * for indicating packet duration. */
  58. frame_size = av_get_audio_frame_duration(enc, enc->block_align);
  59. waveformatextensible = (enc->channels > 2 && enc->channel_layout) ||
  60. enc->sample_rate > 48000 ||
  61. av_get_bits_per_sample(enc->codec_id) > 16;
  62. if (waveformatextensible)
  63. avio_wl16(pb, 0xfffe);
  64. else
  65. avio_wl16(pb, enc->codec_tag);
  66. avio_wl16(pb, enc->channels);
  67. avio_wl32(pb, enc->sample_rate);
  68. if (enc->codec_id == AV_CODEC_ID_MP2 ||
  69. enc->codec_id == AV_CODEC_ID_MP3 ||
  70. enc->codec_id == AV_CODEC_ID_GSM_MS) {
  71. bps = 0;
  72. } else {
  73. if (!(bps = av_get_bits_per_sample(enc->codec_id))) {
  74. if (enc->bits_per_coded_sample)
  75. bps = enc->bits_per_coded_sample;
  76. else
  77. bps = 16; // default to 16
  78. }
  79. }
  80. if (bps != enc->bits_per_coded_sample && enc->bits_per_coded_sample) {
  81. av_log(enc, AV_LOG_WARNING,
  82. "requested bits_per_coded_sample (%d) "
  83. "and actually stored (%d) differ\n",
  84. enc->bits_per_coded_sample, bps);
  85. }
  86. if (enc->codec_id == AV_CODEC_ID_MP2) {
  87. blkalign = frame_size;
  88. } else if (enc->codec_id == AV_CODEC_ID_MP3) {
  89. blkalign = 576 * (enc->sample_rate <= 24000 ? 1 : 2);
  90. } else if (enc->codec_id == AV_CODEC_ID_AC3) {
  91. blkalign = 3840; /* maximum bytes per frame */
  92. } else if (enc->block_align != 0) { /* specified by the codec */
  93. blkalign = enc->block_align;
  94. } else
  95. blkalign = bps * enc->channels / av_gcd(8, bps);
  96. if (enc->codec_id == AV_CODEC_ID_PCM_U8 ||
  97. enc->codec_id == AV_CODEC_ID_PCM_S24LE ||
  98. enc->codec_id == AV_CODEC_ID_PCM_S32LE ||
  99. enc->codec_id == AV_CODEC_ID_PCM_F32LE ||
  100. enc->codec_id == AV_CODEC_ID_PCM_F64LE ||
  101. enc->codec_id == AV_CODEC_ID_PCM_S16LE) {
  102. bytespersec = enc->sample_rate * blkalign;
  103. } else {
  104. bytespersec = enc->bit_rate / 8;
  105. }
  106. avio_wl32(pb, bytespersec); /* bytes per second */
  107. avio_wl16(pb, blkalign); /* block align */
  108. avio_wl16(pb, bps); /* bits per sample */
  109. if (enc->codec_id == AV_CODEC_ID_MP3) {
  110. hdrsize += 12;
  111. bytestream_put_le16(&riff_extradata, 1); /* wID */
  112. bytestream_put_le32(&riff_extradata, 2); /* fdwFlags */
  113. bytestream_put_le16(&riff_extradata, 1152); /* nBlockSize */
  114. bytestream_put_le16(&riff_extradata, 1); /* nFramesPerBlock */
  115. bytestream_put_le16(&riff_extradata, 1393); /* nCodecDelay */
  116. } else if (enc->codec_id == AV_CODEC_ID_MP2) {
  117. hdrsize += 22;
  118. /* fwHeadLayer */
  119. bytestream_put_le16(&riff_extradata, 2);
  120. /* dwHeadBitrate */
  121. bytestream_put_le32(&riff_extradata, enc->bit_rate);
  122. /* fwHeadMode */
  123. bytestream_put_le16(&riff_extradata, enc->channels == 2 ? 1 : 8);
  124. /* fwHeadModeExt */
  125. bytestream_put_le16(&riff_extradata, 0);
  126. /* wHeadEmphasis */
  127. bytestream_put_le16(&riff_extradata, 1);
  128. /* fwHeadFlags */
  129. bytestream_put_le16(&riff_extradata, 16);
  130. /* dwPTSLow */
  131. bytestream_put_le32(&riff_extradata, 0);
  132. /* dwPTSHigh */
  133. bytestream_put_le32(&riff_extradata, 0);
  134. } else if (enc->codec_id == AV_CODEC_ID_GSM_MS ||
  135. enc->codec_id == AV_CODEC_ID_ADPCM_IMA_WAV) {
  136. hdrsize += 2;
  137. /* wSamplesPerBlock */
  138. bytestream_put_le16(&riff_extradata, frame_size);
  139. } else if (enc->extradata_size) {
  140. riff_extradata_start = enc->extradata;
  141. riff_extradata = enc->extradata + enc->extradata_size;
  142. hdrsize += enc->extradata_size;
  143. }
  144. /* write WAVEFORMATEXTENSIBLE extensions */
  145. if (waveformatextensible) {
  146. hdrsize += 22;
  147. /* 22 is WAVEFORMATEXTENSIBLE size */
  148. avio_wl16(pb, riff_extradata - riff_extradata_start + 22);
  149. /* ValidBitsPerSample || SamplesPerBlock || Reserved */
  150. avio_wl16(pb, bps);
  151. /* dwChannelMask */
  152. avio_wl32(pb, enc->channel_layout);
  153. /* GUID + next 3 */
  154. avio_wl32(pb, enc->codec_tag);
  155. avio_wl32(pb, 0x00100000);
  156. avio_wl32(pb, 0xAA000080);
  157. avio_wl32(pb, 0x719B3800);
  158. } else {
  159. avio_wl16(pb, riff_extradata - riff_extradata_start); /* cbSize */
  160. }
  161. avio_write(pb, riff_extradata_start, riff_extradata - riff_extradata_start);
  162. if (hdrsize & 1) {
  163. hdrsize++;
  164. avio_w8(pb, 0);
  165. }
  166. return hdrsize;
  167. }
  168. /* BITMAPINFOHEADER header */
  169. void ff_put_bmp_header(AVIOContext *pb, AVCodecContext *enc,
  170. const AVCodecTag *tags, int for_asf)
  171. {
  172. /* size */
  173. avio_wl32(pb, 40 + enc->extradata_size);
  174. avio_wl32(pb, enc->width);
  175. //We always store RGB TopDown
  176. avio_wl32(pb, enc->codec_tag ? enc->height : -enc->height);
  177. /* planes */
  178. avio_wl16(pb, 1);
  179. /* depth */
  180. avio_wl16(pb, enc->bits_per_coded_sample ? enc->bits_per_coded_sample : 24);
  181. /* compression type */
  182. avio_wl32(pb, enc->codec_tag);
  183. avio_wl32(pb, enc->width * enc->height * 3);
  184. avio_wl32(pb, 0);
  185. avio_wl32(pb, 0);
  186. avio_wl32(pb, 0);
  187. avio_wl32(pb, 0);
  188. avio_write(pb, enc->extradata, enc->extradata_size);
  189. if (!for_asf && enc->extradata_size & 1)
  190. avio_w8(pb, 0);
  191. }
  192. void ff_parse_specific_params(AVStream *st, int *au_rate,
  193. int *au_ssize, int *au_scale)
  194. {
  195. AVCodecContext *codec = st->codec;
  196. int gcd;
  197. int audio_frame_size;
  198. /* We use the known constant frame size for the codec if known, otherwise
  199. * fall back on using AVCodecContext.frame_size, which is not as reliable
  200. * for indicating packet duration. */
  201. audio_frame_size = av_get_audio_frame_duration(codec, 0);
  202. if (!audio_frame_size)
  203. audio_frame_size = codec->frame_size;
  204. *au_ssize = codec->block_align;
  205. if (audio_frame_size && codec->sample_rate) {
  206. *au_scale = audio_frame_size;
  207. *au_rate = codec->sample_rate;
  208. } else if (codec->codec_type == AVMEDIA_TYPE_VIDEO ||
  209. codec->codec_type == AVMEDIA_TYPE_DATA ||
  210. codec->codec_type == AVMEDIA_TYPE_SUBTITLE) {
  211. *au_scale = st->time_base.num;
  212. *au_rate = st->time_base.den;
  213. } else {
  214. *au_scale = codec->block_align ? codec->block_align * 8 : 8;
  215. *au_rate = codec->bit_rate ? codec->bit_rate :
  216. 8 * codec->sample_rate;
  217. }
  218. gcd = av_gcd(*au_scale, *au_rate);
  219. *au_scale /= gcd;
  220. *au_rate /= gcd;
  221. }
  222. void ff_riff_write_info_tag(AVIOContext *pb, const char *tag, const char *str)
  223. {
  224. int len = strlen(str);
  225. if (len > 0) {
  226. len++;
  227. ffio_wfourcc(pb, tag);
  228. avio_wl32(pb, len);
  229. avio_put_str(pb, str);
  230. if (len & 1)
  231. avio_w8(pb, 0);
  232. }
  233. }
  234. static const char riff_tags[][5] = {
  235. "IARL", "IART", "ICMS", "ICMT", "ICOP", "ICRD", "ICRP", "IDIM", "IDPI",
  236. "IENG", "IGNR", "IKEY", "ILGT", "ILNG", "IMED", "INAM", "IPLT", "IPRD",
  237. "IPRT", "ITRK", "ISBJ", "ISFT", "ISHP", "ISMP", "ISRC", "ISRF", "ITCH",
  238. { 0 }
  239. };
  240. static int riff_has_valid_tags(AVFormatContext *s)
  241. {
  242. int i;
  243. for (i = 0; *riff_tags[i]; i++)
  244. if (av_dict_get(s->metadata, riff_tags[i], NULL, AV_DICT_MATCH_CASE))
  245. return 1;
  246. return 0;
  247. }
  248. void ff_riff_write_info(AVFormatContext *s)
  249. {
  250. AVIOContext *pb = s->pb;
  251. int i;
  252. int64_t list_pos;
  253. AVDictionaryEntry *t = NULL;
  254. ff_metadata_conv(&s->metadata, ff_riff_info_conv, NULL);
  255. /* writing empty LIST is not nice and may cause problems */
  256. if (!riff_has_valid_tags(s))
  257. return;
  258. list_pos = ff_start_tag(pb, "LIST");
  259. ffio_wfourcc(pb, "INFO");
  260. for (i = 0; *riff_tags[i]; i++)
  261. if ((t = av_dict_get(s->metadata, riff_tags[i],
  262. NULL, AV_DICT_MATCH_CASE)))
  263. ff_riff_write_info_tag(s->pb, t->key, t->value);
  264. ff_end_tag(pb, list_pos);
  265. }