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.

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