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.

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