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. 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 = frame_size;
  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 = enc->strict_std_compliance < FF_COMPLIANCE_NORMAL ||
  160. enc->channel_layout < 0x40000;
  161. /* 22 is WAVEFORMATEXTENSIBLE size */
  162. avio_wl16(pb, riff_extradata - riff_extradata_start + 22);
  163. /* ValidBitsPerSample || SamplesPerBlock || Reserved */
  164. avio_wl16(pb, bps);
  165. /* dwChannelMask */
  166. avio_wl32(pb, write_channel_mask ? enc->channel_layout : 0);
  167. /* GUID + next 3 */
  168. if (enc->codec_id == AV_CODEC_ID_EAC3) {
  169. ff_put_guid(pb, get_codec_guid(enc->codec_id, ff_codec_wav_guids));
  170. } else {
  171. avio_wl32(pb, enc->codec_tag);
  172. avio_wl32(pb, 0x00100000);
  173. avio_wl32(pb, 0xAA000080);
  174. avio_wl32(pb, 0x719B3800);
  175. }
  176. } else if ((flags & FF_PUT_WAV_HEADER_FORCE_WAVEFORMATEX) ||
  177. enc->codec_tag != 0x0001 /* PCM */ ||
  178. riff_extradata - riff_extradata_start) {
  179. /* WAVEFORMATEX */
  180. avio_wl16(pb, riff_extradata - riff_extradata_start); /* cbSize */
  181. } /* else PCMWAVEFORMAT */
  182. avio_write(pb, riff_extradata_start, riff_extradata - riff_extradata_start);
  183. hdrsize = avio_tell(pb) - hdrstart;
  184. if (hdrsize & 1) {
  185. hdrsize++;
  186. avio_w8(pb, 0);
  187. }
  188. return hdrsize;
  189. }
  190. /* BITMAPINFOHEADER header */
  191. void ff_put_bmp_header(AVIOContext *pb, AVCodecContext *enc,
  192. const AVCodecTag *tags, int for_asf, int ignore_extradata)
  193. {
  194. int keep_height = enc->extradata_size >= 9 &&
  195. !memcmp(enc->extradata + enc->extradata_size - 9, "BottomUp", 9);
  196. int extradata_size = enc->extradata_size - 9*keep_height;
  197. /* size */
  198. avio_wl32(pb, 40 + (ignore_extradata ? 0 :extradata_size));
  199. avio_wl32(pb, enc->width);
  200. //We always store RGB TopDown
  201. avio_wl32(pb, enc->codec_tag || keep_height ? enc->height : -enc->height);
  202. /* planes */
  203. avio_wl16(pb, 1);
  204. /* depth */
  205. avio_wl16(pb, enc->bits_per_coded_sample ? enc->bits_per_coded_sample : 24);
  206. /* compression type */
  207. avio_wl32(pb, enc->codec_tag);
  208. avio_wl32(pb, (enc->width * enc->height * (enc->bits_per_coded_sample ? enc->bits_per_coded_sample : 24)+7) / 8);
  209. avio_wl32(pb, 0);
  210. avio_wl32(pb, 0);
  211. avio_wl32(pb, 0);
  212. avio_wl32(pb, 0);
  213. if (!ignore_extradata) {
  214. avio_write(pb, enc->extradata, extradata_size);
  215. if (!for_asf && extradata_size & 1)
  216. avio_w8(pb, 0);
  217. }
  218. }
  219. void ff_parse_specific_params(AVStream *st, int *au_rate,
  220. int *au_ssize, int *au_scale)
  221. {
  222. AVCodecContext *codec = st->codec;
  223. int gcd;
  224. int audio_frame_size;
  225. /* We use the known constant frame size for the codec if known, otherwise
  226. * fall back on using AVCodecContext.frame_size, which is not as reliable
  227. * for indicating packet duration. */
  228. audio_frame_size = av_get_audio_frame_duration(codec, 0);
  229. if (!audio_frame_size)
  230. audio_frame_size = codec->frame_size;
  231. *au_ssize = codec->block_align;
  232. if (audio_frame_size && codec->sample_rate) {
  233. *au_scale = audio_frame_size;
  234. *au_rate = codec->sample_rate;
  235. } else if (codec->codec_type == AVMEDIA_TYPE_VIDEO ||
  236. codec->codec_type == AVMEDIA_TYPE_DATA ||
  237. codec->codec_type == AVMEDIA_TYPE_SUBTITLE) {
  238. *au_scale = st->time_base.num;
  239. *au_rate = st->time_base.den;
  240. } else {
  241. *au_scale = codec->block_align ? codec->block_align * 8 : 8;
  242. *au_rate = codec->bit_rate ? codec->bit_rate :
  243. 8 * codec->sample_rate;
  244. }
  245. gcd = av_gcd(*au_scale, *au_rate);
  246. *au_scale /= gcd;
  247. *au_rate /= gcd;
  248. }
  249. void ff_riff_write_info_tag(AVIOContext *pb, const char *tag, const char *str)
  250. {
  251. int len = strlen(str);
  252. if (len > 0) {
  253. len++;
  254. ffio_wfourcc(pb, tag);
  255. avio_wl32(pb, len);
  256. avio_put_str(pb, str);
  257. if (len & 1)
  258. avio_w8(pb, 0);
  259. }
  260. }
  261. static const char riff_tags[][5] = {
  262. "IARL", "IART", "ICMS", "ICMT", "ICOP", "ICRD", "ICRP", "IDIM", "IDPI",
  263. "IENG", "IGNR", "IKEY", "ILGT", "ILNG", "IMED", "INAM", "IPLT", "IPRD",
  264. "IPRT", "ITRK", "ISBJ", "ISFT", "ISHP", "ISMP", "ISRC", "ISRF", "ITCH",
  265. { 0 }
  266. };
  267. static int riff_has_valid_tags(AVFormatContext *s)
  268. {
  269. int i;
  270. for (i = 0; *riff_tags[i]; i++)
  271. if (av_dict_get(s->metadata, riff_tags[i], NULL, AV_DICT_MATCH_CASE))
  272. return 1;
  273. return 0;
  274. }
  275. void ff_riff_write_info(AVFormatContext *s)
  276. {
  277. AVIOContext *pb = s->pb;
  278. int i;
  279. int64_t list_pos;
  280. AVDictionaryEntry *t = NULL;
  281. ff_metadata_conv(&s->metadata, ff_riff_info_conv, NULL);
  282. /* writing empty LIST is not nice and may cause problems */
  283. if (!riff_has_valid_tags(s))
  284. return;
  285. list_pos = ff_start_tag(pb, "LIST");
  286. ffio_wfourcc(pb, "INFO");
  287. for (i = 0; *riff_tags[i]; i++)
  288. if ((t = av_dict_get(s->metadata, riff_tags[i],
  289. NULL, AV_DICT_MATCH_CASE)))
  290. ff_riff_write_info_tag(s->pb, t->key, t->value);
  291. ff_end_tag(pb, list_pos);
  292. }
  293. void ff_put_guid(AVIOContext *s, const ff_asf_guid *g)
  294. {
  295. av_assert0(sizeof(*g) == 16);
  296. avio_write(s, *g, sizeof(*g));
  297. }
  298. const ff_asf_guid *get_codec_guid(enum AVCodecID id, const AVCodecGuid *av_guid)
  299. {
  300. int i;
  301. for (i = 0; av_guid[i].id != AV_CODEC_ID_NONE; i++) {
  302. if (id == av_guid[i].id)
  303. return &(av_guid[i].guid);
  304. }
  305. return NULL;
  306. }