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.

313 lines
11KB

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