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.

297 lines
9.9KB

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