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.

338 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. /* size */
  199. avio_wl32(pb, 40 + (ignore_extradata ? 0 : enc->extradata_size));
  200. avio_wl32(pb, enc->width);
  201. //We always store RGB TopDown
  202. avio_wl32(pb, enc->codec_tag ? enc->height : -enc->height);
  203. /* planes */
  204. avio_wl16(pb, 1);
  205. /* depth */
  206. avio_wl16(pb, enc->bits_per_coded_sample ? enc->bits_per_coded_sample : 24);
  207. /* compression type */
  208. avio_wl32(pb, enc->codec_tag);
  209. avio_wl32(pb, (enc->width * enc->height * (enc->bits_per_coded_sample ? enc->bits_per_coded_sample : 24)+7) / 8);
  210. avio_wl32(pb, 0);
  211. avio_wl32(pb, 0);
  212. avio_wl32(pb, 0);
  213. avio_wl32(pb, 0);
  214. if (!ignore_extradata) {
  215. avio_write(pb, enc->extradata, enc->extradata_size);
  216. if (!for_asf && enc->extradata_size & 1)
  217. avio_w8(pb, 0);
  218. }
  219. }
  220. void ff_parse_specific_params(AVStream *st, int *au_rate,
  221. int *au_ssize, int *au_scale)
  222. {
  223. AVCodecContext *codec = st->codec;
  224. int gcd;
  225. int audio_frame_size;
  226. /* We use the known constant frame size for the codec if known, otherwise
  227. * fall back on using AVCodecContext.frame_size, which is not as reliable
  228. * for indicating packet duration. */
  229. audio_frame_size = av_get_audio_frame_duration(codec, 0);
  230. if (!audio_frame_size)
  231. audio_frame_size = codec->frame_size;
  232. *au_ssize = codec->block_align;
  233. if (audio_frame_size && codec->sample_rate) {
  234. *au_scale = audio_frame_size;
  235. *au_rate = codec->sample_rate;
  236. } else if (codec->codec_type == AVMEDIA_TYPE_VIDEO ||
  237. codec->codec_type == AVMEDIA_TYPE_DATA ||
  238. codec->codec_type == AVMEDIA_TYPE_SUBTITLE) {
  239. *au_scale = st->time_base.num;
  240. *au_rate = st->time_base.den;
  241. } else {
  242. *au_scale = codec->block_align ? codec->block_align * 8 : 8;
  243. *au_rate = codec->bit_rate ? codec->bit_rate :
  244. 8 * codec->sample_rate;
  245. }
  246. gcd = av_gcd(*au_scale, *au_rate);
  247. *au_scale /= gcd;
  248. *au_rate /= gcd;
  249. }
  250. void ff_riff_write_info_tag(AVIOContext *pb, const char *tag, const char *str)
  251. {
  252. int len = strlen(str);
  253. if (len > 0) {
  254. len++;
  255. ffio_wfourcc(pb, tag);
  256. avio_wl32(pb, len);
  257. avio_put_str(pb, str);
  258. if (len & 1)
  259. avio_w8(pb, 0);
  260. }
  261. }
  262. static const char riff_tags[][5] = {
  263. "IARL", "IART", "ICMS", "ICMT", "ICOP", "ICRD", "ICRP", "IDIM", "IDPI",
  264. "IENG", "IGNR", "IKEY", "ILGT", "ILNG", "IMED", "INAM", "IPLT", "IPRD",
  265. "IPRT", "ITRK", "ISBJ", "ISFT", "ISHP", "ISMP", "ISRC", "ISRF", "ITCH",
  266. { 0 }
  267. };
  268. static int riff_has_valid_tags(AVFormatContext *s)
  269. {
  270. int i;
  271. for (i = 0; *riff_tags[i]; i++)
  272. if (av_dict_get(s->metadata, riff_tags[i], NULL, AV_DICT_MATCH_CASE))
  273. return 1;
  274. return 0;
  275. }
  276. void ff_riff_write_info(AVFormatContext *s)
  277. {
  278. AVIOContext *pb = s->pb;
  279. int i;
  280. int64_t list_pos;
  281. AVDictionaryEntry *t = NULL;
  282. ff_metadata_conv(&s->metadata, ff_riff_info_conv, NULL);
  283. /* writing empty LIST is not nice and may cause problems */
  284. if (!riff_has_valid_tags(s))
  285. return;
  286. list_pos = ff_start_tag(pb, "LIST");
  287. ffio_wfourcc(pb, "INFO");
  288. for (i = 0; *riff_tags[i]; i++)
  289. if ((t = av_dict_get(s->metadata, riff_tags[i],
  290. NULL, AV_DICT_MATCH_CASE)))
  291. ff_riff_write_info_tag(s->pb, t->key, t->value);
  292. ff_end_tag(pb, list_pos);
  293. }
  294. void ff_put_guid(AVIOContext *s, const ff_asf_guid *g)
  295. {
  296. av_assert0(sizeof(*g) == 16);
  297. avio_write(s, *g, sizeof(*g));
  298. }
  299. const ff_asf_guid *get_codec_guid(enum AVCodecID id, const AVCodecGuid *av_guid)
  300. {
  301. int i;
  302. for (i = 0; av_guid[i].id != AV_CODEC_ID_NONE; i++) {
  303. if (id == av_guid[i].id)
  304. return &(av_guid[i].guid);
  305. }
  306. return NULL;
  307. }