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.

363 lines
13KB

  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. waveformatextensible = (enc->channels > 2 && enc->channel_layout) ||
  64. enc->sample_rate > 48000 ||
  65. enc->codec_id == AV_CODEC_ID_EAC3 ||
  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. blkalign = (144 * enc->bit_rate - 1)/enc->sample_rate + 1;
  95. } else if (enc->codec_id == AV_CODEC_ID_MP3) {
  96. blkalign = 576 * (enc->sample_rate <= (24000 + 32000)/2 ? 1 : 2);
  97. } else if (enc->codec_id == AV_CODEC_ID_AC3) {
  98. blkalign = 3840; /* maximum bytes per frame */
  99. } else if (enc->codec_id == AV_CODEC_ID_AAC) {
  100. blkalign = 768 * enc->channels; /* maximum bytes per frame */
  101. } else if (enc->codec_id == AV_CODEC_ID_G723_1) {
  102. blkalign = 24;
  103. } else if (enc->block_align != 0) { /* specified by the codec */
  104. blkalign = enc->block_align;
  105. } else
  106. blkalign = bps * enc->channels / av_gcd(8, bps);
  107. if (enc->codec_id == AV_CODEC_ID_PCM_U8 ||
  108. enc->codec_id == AV_CODEC_ID_PCM_S24LE ||
  109. enc->codec_id == AV_CODEC_ID_PCM_S32LE ||
  110. enc->codec_id == AV_CODEC_ID_PCM_F32LE ||
  111. enc->codec_id == AV_CODEC_ID_PCM_F64LE ||
  112. enc->codec_id == AV_CODEC_ID_PCM_S16LE) {
  113. bytespersec = enc->sample_rate * blkalign;
  114. } else if (enc->codec_id == AV_CODEC_ID_G723_1) {
  115. bytespersec = 800;
  116. } else {
  117. bytespersec = enc->bit_rate / 8;
  118. }
  119. avio_wl32(pb, bytespersec); /* bytes per second */
  120. avio_wl16(pb, blkalign); /* block align */
  121. avio_wl16(pb, bps); /* bits per sample */
  122. if (enc->codec_id == AV_CODEC_ID_MP3) {
  123. bytestream_put_le16(&riff_extradata, 1); /* wID */
  124. bytestream_put_le32(&riff_extradata, 2); /* fdwFlags */
  125. bytestream_put_le16(&riff_extradata, 1152); /* nBlockSize */
  126. bytestream_put_le16(&riff_extradata, 1); /* nFramesPerBlock */
  127. bytestream_put_le16(&riff_extradata, 1393); /* nCodecDelay */
  128. } else if (enc->codec_id == AV_CODEC_ID_MP2) {
  129. /* fwHeadLayer */
  130. bytestream_put_le16(&riff_extradata, 2);
  131. /* dwHeadBitrate */
  132. bytestream_put_le32(&riff_extradata, enc->bit_rate);
  133. /* fwHeadMode */
  134. bytestream_put_le16(&riff_extradata, enc->channels == 2 ? 1 : 8);
  135. /* fwHeadModeExt */
  136. bytestream_put_le16(&riff_extradata, 0);
  137. /* wHeadEmphasis */
  138. bytestream_put_le16(&riff_extradata, 1);
  139. /* fwHeadFlags */
  140. bytestream_put_le16(&riff_extradata, 16);
  141. /* dwPTSLow */
  142. bytestream_put_le32(&riff_extradata, 0);
  143. /* dwPTSHigh */
  144. bytestream_put_le32(&riff_extradata, 0);
  145. } else if (enc->codec_id == AV_CODEC_ID_G723_1) {
  146. bytestream_put_le32(&riff_extradata, 0x9ace0002); /* extradata needed for msacm g723.1 codec */
  147. bytestream_put_le32(&riff_extradata, 0xaea2f732);
  148. bytestream_put_le16(&riff_extradata, 0xacde);
  149. } else if (enc->codec_id == AV_CODEC_ID_GSM_MS ||
  150. enc->codec_id == AV_CODEC_ID_ADPCM_IMA_WAV) {
  151. /* wSamplesPerBlock */
  152. bytestream_put_le16(&riff_extradata, frame_size);
  153. } else if (enc->extradata_size) {
  154. riff_extradata_start = enc->extradata;
  155. riff_extradata = enc->extradata + enc->extradata_size;
  156. }
  157. /* write WAVEFORMATEXTENSIBLE extensions */
  158. if (waveformatextensible) {
  159. int write_channel_mask = !(flags & FF_PUT_WAV_HEADER_SKIP_CHANNELMASK) &&
  160. (enc->strict_std_compliance < FF_COMPLIANCE_NORMAL ||
  161. enc->channel_layout < 0x40000);
  162. /* 22 is WAVEFORMATEXTENSIBLE size */
  163. avio_wl16(pb, riff_extradata - riff_extradata_start + 22);
  164. /* ValidBitsPerSample || SamplesPerBlock || Reserved */
  165. avio_wl16(pb, bps);
  166. /* dwChannelMask */
  167. avio_wl32(pb, write_channel_mask ? enc->channel_layout : 0);
  168. /* GUID + next 3 */
  169. if (enc->codec_id == AV_CODEC_ID_EAC3) {
  170. ff_put_guid(pb, ff_get_codec_guid(enc->codec_id, ff_codec_wav_guids));
  171. } else {
  172. avio_wl32(pb, enc->codec_tag);
  173. avio_wl32(pb, 0x00100000);
  174. avio_wl32(pb, 0xAA000080);
  175. avio_wl32(pb, 0x719B3800);
  176. }
  177. } else if ((flags & FF_PUT_WAV_HEADER_FORCE_WAVEFORMATEX) ||
  178. enc->codec_tag != 0x0001 /* PCM */ ||
  179. riff_extradata - riff_extradata_start) {
  180. /* WAVEFORMATEX */
  181. avio_wl16(pb, riff_extradata - riff_extradata_start); /* cbSize */
  182. } /* else PCMWAVEFORMAT */
  183. avio_write(pb, riff_extradata_start, riff_extradata - riff_extradata_start);
  184. hdrsize = avio_tell(pb) - hdrstart;
  185. if (hdrsize & 1) {
  186. hdrsize++;
  187. avio_w8(pb, 0);
  188. }
  189. return hdrsize;
  190. }
  191. /* BITMAPINFOHEADER header */
  192. void ff_put_bmp_header(AVIOContext *pb, AVCodecContext *enc,
  193. const AVCodecTag *tags, int for_asf, int ignore_extradata)
  194. {
  195. int keep_height = enc->extradata_size >= 9 &&
  196. !memcmp(enc->extradata + enc->extradata_size - 9, "BottomUp", 9);
  197. int extradata_size = enc->extradata_size - 9*keep_height;
  198. enum AVPixelFormat pix_fmt = enc->pix_fmt;
  199. int pal_avi;
  200. if (pix_fmt == AV_PIX_FMT_NONE && enc->bits_per_coded_sample == 1)
  201. pix_fmt = AV_PIX_FMT_MONOWHITE;
  202. pal_avi = !for_asf &&
  203. (pix_fmt == AV_PIX_FMT_PAL8 ||
  204. pix_fmt == AV_PIX_FMT_MONOWHITE ||
  205. pix_fmt == AV_PIX_FMT_MONOBLACK);
  206. /* Size (not including the size of the color table or color masks) */
  207. avio_wl32(pb, 40 + (ignore_extradata || pal_avi ? 0 : extradata_size));
  208. avio_wl32(pb, enc->width);
  209. //We always store RGB TopDown
  210. avio_wl32(pb, enc->codec_tag || keep_height ? enc->height : -enc->height);
  211. /* planes */
  212. avio_wl16(pb, 1);
  213. /* depth */
  214. avio_wl16(pb, enc->bits_per_coded_sample ? enc->bits_per_coded_sample : 24);
  215. /* compression type */
  216. avio_wl32(pb, enc->codec_tag);
  217. avio_wl32(pb, (enc->width * enc->height * (enc->bits_per_coded_sample ? enc->bits_per_coded_sample : 24)+7) / 8);
  218. avio_wl32(pb, 0);
  219. avio_wl32(pb, 0);
  220. /* Number of color indices in the color table that are used.
  221. * A value of 0 means 2^biBitCount indices, but this doesn't work
  222. * with Windows Media Player and files containing xxpc chunks. */
  223. avio_wl32(pb, pal_avi ? 1 << enc->bits_per_coded_sample : 0);
  224. avio_wl32(pb, 0);
  225. if (!ignore_extradata) {
  226. if (enc->extradata_size) {
  227. avio_write(pb, enc->extradata, extradata_size);
  228. if (!for_asf && extradata_size & 1)
  229. avio_w8(pb, 0);
  230. } else if (pal_avi) {
  231. int i;
  232. for (i = 0; i < 1 << enc->bits_per_coded_sample; i++) {
  233. /* Initialize 1 bpp palette to black & white */
  234. if (i == 0 && pix_fmt == AV_PIX_FMT_MONOWHITE)
  235. avio_wl32(pb, 0xffffff);
  236. else if (i == 1 && pix_fmt == AV_PIX_FMT_MONOBLACK)
  237. avio_wl32(pb, 0xffffff);
  238. else
  239. avio_wl32(pb, 0);
  240. }
  241. }
  242. }
  243. }
  244. void ff_parse_specific_params(AVStream *st, int *au_rate,
  245. int *au_ssize, int *au_scale)
  246. {
  247. AVCodecContext *codec = st->codec;
  248. int gcd;
  249. int audio_frame_size;
  250. /* We use the known constant frame size for the codec if known, otherwise
  251. * fall back on using AVCodecContext.frame_size, which is not as reliable
  252. * for indicating packet duration. */
  253. audio_frame_size = av_get_audio_frame_duration(codec, 0);
  254. if (!audio_frame_size)
  255. audio_frame_size = codec->frame_size;
  256. *au_ssize = codec->block_align;
  257. if (audio_frame_size && codec->sample_rate) {
  258. *au_scale = audio_frame_size;
  259. *au_rate = codec->sample_rate;
  260. } else if (codec->codec_type == AVMEDIA_TYPE_VIDEO ||
  261. codec->codec_type == AVMEDIA_TYPE_DATA ||
  262. codec->codec_type == AVMEDIA_TYPE_SUBTITLE) {
  263. *au_scale = st->time_base.num;
  264. *au_rate = st->time_base.den;
  265. } else {
  266. *au_scale = codec->block_align ? codec->block_align * 8 : 8;
  267. *au_rate = codec->bit_rate ? codec->bit_rate :
  268. 8 * codec->sample_rate;
  269. }
  270. gcd = av_gcd(*au_scale, *au_rate);
  271. *au_scale /= gcd;
  272. *au_rate /= gcd;
  273. }
  274. void ff_riff_write_info_tag(AVIOContext *pb, const char *tag, const char *str)
  275. {
  276. size_t len = strlen(str);
  277. if (len > 0 && len < UINT32_MAX) {
  278. len++;
  279. ffio_wfourcc(pb, tag);
  280. avio_wl32(pb, len);
  281. avio_put_str(pb, str);
  282. if (len & 1)
  283. avio_w8(pb, 0);
  284. }
  285. }
  286. static const char riff_tags[][5] = {
  287. "IARL", "IART", "ICMS", "ICMT", "ICOP", "ICRD", "ICRP", "IDIM", "IDPI",
  288. "IENG", "IGNR", "IKEY", "ILGT", "ILNG", "IMED", "INAM", "IPLT", "IPRD",
  289. "IPRT", "ITRK", "ISBJ", "ISFT", "ISHP", "ISMP", "ISRC", "ISRF", "ITCH",
  290. { 0 }
  291. };
  292. static int riff_has_valid_tags(AVFormatContext *s)
  293. {
  294. int i;
  295. for (i = 0; *riff_tags[i]; i++)
  296. if (av_dict_get(s->metadata, riff_tags[i], NULL, AV_DICT_MATCH_CASE))
  297. return 1;
  298. return 0;
  299. }
  300. void ff_riff_write_info(AVFormatContext *s)
  301. {
  302. AVIOContext *pb = s->pb;
  303. int i;
  304. int64_t list_pos;
  305. AVDictionaryEntry *t = NULL;
  306. ff_metadata_conv(&s->metadata, ff_riff_info_conv, NULL);
  307. /* writing empty LIST is not nice and may cause problems */
  308. if (!riff_has_valid_tags(s))
  309. return;
  310. list_pos = ff_start_tag(pb, "LIST");
  311. ffio_wfourcc(pb, "INFO");
  312. for (i = 0; *riff_tags[i]; i++)
  313. if ((t = av_dict_get(s->metadata, riff_tags[i],
  314. NULL, AV_DICT_MATCH_CASE)))
  315. ff_riff_write_info_tag(s->pb, t->key, t->value);
  316. ff_end_tag(pb, list_pos);
  317. }
  318. void ff_put_guid(AVIOContext *s, const ff_asf_guid *g)
  319. {
  320. av_assert0(sizeof(*g) == 16);
  321. avio_write(s, *g, sizeof(*g));
  322. }
  323. const ff_asf_guid *ff_get_codec_guid(enum AVCodecID id, const AVCodecGuid *av_guid)
  324. {
  325. int i;
  326. for (i = 0; av_guid[i].id != AV_CODEC_ID_NONE; i++) {
  327. if (id == av_guid[i].id)
  328. return &(av_guid[i].guid);
  329. }
  330. return NULL;
  331. }