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.

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