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.

342 lines
12KB

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