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.

287 lines
9.5KB

  1. /*
  2. * Core Audio Format muxer
  3. * Copyright (c) 2011 Carl Eugen Hoyos
  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 "avformat.h"
  22. #include "caf.h"
  23. #include "isom.h"
  24. #include "avio_internal.h"
  25. #include "libavutil/intfloat.h"
  26. #include "libavutil/dict.h"
  27. typedef struct {
  28. int64_t data;
  29. uint8_t *pkt_sizes;
  30. int size_buffer_size;
  31. int size_entries_used;
  32. int packets;
  33. } CAFContext;
  34. static uint32_t codec_flags(enum AVCodecID codec_id) {
  35. switch (codec_id) {
  36. case AV_CODEC_ID_PCM_F32BE:
  37. case AV_CODEC_ID_PCM_F64BE:
  38. return 1; //< kCAFLinearPCMFormatFlagIsFloat
  39. case AV_CODEC_ID_PCM_S16LE:
  40. case AV_CODEC_ID_PCM_S24LE:
  41. case AV_CODEC_ID_PCM_S32LE:
  42. return 2; //< kCAFLinearPCMFormatFlagIsLittleEndian
  43. case AV_CODEC_ID_PCM_F32LE:
  44. case AV_CODEC_ID_PCM_F64LE:
  45. return 3; //< kCAFLinearPCMFormatFlagIsFloat | kCAFLinearPCMFormatFlagIsLittleEndian
  46. default:
  47. return 0;
  48. }
  49. }
  50. static uint32_t samples_per_packet(enum AVCodecID codec_id, int channels, int block_align) {
  51. switch (codec_id) {
  52. case AV_CODEC_ID_PCM_S8:
  53. case AV_CODEC_ID_PCM_S16LE:
  54. case AV_CODEC_ID_PCM_S16BE:
  55. case AV_CODEC_ID_PCM_S24LE:
  56. case AV_CODEC_ID_PCM_S24BE:
  57. case AV_CODEC_ID_PCM_S32LE:
  58. case AV_CODEC_ID_PCM_S32BE:
  59. case AV_CODEC_ID_PCM_F32LE:
  60. case AV_CODEC_ID_PCM_F32BE:
  61. case AV_CODEC_ID_PCM_F64LE:
  62. case AV_CODEC_ID_PCM_F64BE:
  63. case AV_CODEC_ID_PCM_ALAW:
  64. case AV_CODEC_ID_PCM_MULAW:
  65. return 1;
  66. case AV_CODEC_ID_MACE3:
  67. case AV_CODEC_ID_MACE6:
  68. return 6;
  69. case AV_CODEC_ID_ADPCM_IMA_QT:
  70. return 64;
  71. case AV_CODEC_ID_AMR_NB:
  72. case AV_CODEC_ID_GSM:
  73. case AV_CODEC_ID_ILBC:
  74. case AV_CODEC_ID_QCELP:
  75. return 160;
  76. case AV_CODEC_ID_GSM_MS:
  77. return 320;
  78. case AV_CODEC_ID_MP1:
  79. return 384;
  80. case AV_CODEC_ID_MP2:
  81. case AV_CODEC_ID_MP3:
  82. return 1152;
  83. case AV_CODEC_ID_AC3:
  84. return 1536;
  85. case AV_CODEC_ID_QDM2:
  86. return 2048 * channels;
  87. case AV_CODEC_ID_ALAC:
  88. return 4096;
  89. case AV_CODEC_ID_ADPCM_IMA_WAV:
  90. return (block_align - 4 * channels) * 8 / (4 * channels) + 1;
  91. case AV_CODEC_ID_ADPCM_MS:
  92. return (block_align - 7 * channels) * 2 / channels + 2;
  93. default:
  94. return 0;
  95. }
  96. }
  97. static int caf_write_header(AVFormatContext *s)
  98. {
  99. AVIOContext *pb = s->pb;
  100. AVCodecContext *enc = s->streams[0]->codec;
  101. CAFContext *caf = s->priv_data;
  102. AVDictionaryEntry *t = NULL;
  103. unsigned int codec_tag = ff_codec_get_tag(ff_codec_caf_tags, enc->codec_id);
  104. int64_t chunk_size = 0;
  105. int frame_size = enc->frame_size;
  106. if (s->nb_streams != 1) {
  107. av_log(s, AV_LOG_ERROR, "CAF files have exactly one stream\n");
  108. return AVERROR(EINVAL);
  109. }
  110. switch (enc->codec_id) {
  111. case AV_CODEC_ID_AAC:
  112. av_log(s, AV_LOG_ERROR, "muxing codec currently unsupported\n");
  113. return AVERROR_PATCHWELCOME;
  114. }
  115. switch (enc->codec_id) {
  116. case AV_CODEC_ID_PCM_S8:
  117. case AV_CODEC_ID_PCM_S16LE:
  118. case AV_CODEC_ID_PCM_S16BE:
  119. case AV_CODEC_ID_PCM_S24LE:
  120. case AV_CODEC_ID_PCM_S24BE:
  121. case AV_CODEC_ID_PCM_S32LE:
  122. case AV_CODEC_ID_PCM_S32BE:
  123. case AV_CODEC_ID_PCM_F32LE:
  124. case AV_CODEC_ID_PCM_F32BE:
  125. case AV_CODEC_ID_PCM_F64LE:
  126. case AV_CODEC_ID_PCM_F64BE:
  127. codec_tag = MKTAG('l','p','c','m');
  128. }
  129. if (!codec_tag) {
  130. av_log(s, AV_LOG_ERROR, "unsupported codec\n");
  131. return AVERROR_INVALIDDATA;
  132. }
  133. if (!enc->block_align && !pb->seekable) {
  134. av_log(s, AV_LOG_ERROR, "Muxing variable packet size not supported on non seekable output\n");
  135. return AVERROR_INVALIDDATA;
  136. }
  137. if (enc->codec_id != AV_CODEC_ID_MP3 || frame_size != 576)
  138. frame_size = samples_per_packet(enc->codec_id, enc->channels, enc->block_align);
  139. ffio_wfourcc(pb, "caff"); //< mFileType
  140. avio_wb16(pb, 1); //< mFileVersion
  141. avio_wb16(pb, 0); //< mFileFlags
  142. ffio_wfourcc(pb, "desc"); //< Audio Description chunk
  143. avio_wb64(pb, 32); //< mChunkSize
  144. avio_wb64(pb, av_double2int(enc->sample_rate)); //< mSampleRate
  145. avio_wl32(pb, codec_tag); //< mFormatID
  146. avio_wb32(pb, codec_flags(enc->codec_id)); //< mFormatFlags
  147. avio_wb32(pb, enc->block_align); //< mBytesPerPacket
  148. avio_wb32(pb, frame_size); //< mFramesPerPacket
  149. avio_wb32(pb, enc->channels); //< mChannelsPerFrame
  150. avio_wb32(pb, av_get_bits_per_sample(enc->codec_id)); //< mBitsPerChannel
  151. if (enc->channel_layout) {
  152. ffio_wfourcc(pb, "chan");
  153. avio_wb64(pb, 12);
  154. ff_mov_write_chan(pb, enc->channel_layout);
  155. }
  156. if (enc->codec_id == AV_CODEC_ID_ALAC) {
  157. ffio_wfourcc(pb, "kuki");
  158. avio_wb64(pb, 12 + enc->extradata_size);
  159. avio_write(pb, "\0\0\0\14frmaalac", 12);
  160. avio_write(pb, enc->extradata, enc->extradata_size);
  161. } else if (enc->codec_id == AV_CODEC_ID_AMR_NB) {
  162. ffio_wfourcc(pb, "kuki");
  163. avio_wb64(pb, 29);
  164. avio_write(pb, "\0\0\0\14frmasamr", 12);
  165. avio_wb32(pb, 0x11); /* size */
  166. avio_write(pb, "samrFFMP", 8);
  167. avio_w8(pb, 0); /* decoder version */
  168. avio_wb16(pb, 0x81FF); /* Mode set (all modes for AMR_NB) */
  169. avio_w8(pb, 0x00); /* Mode change period (no restriction) */
  170. avio_w8(pb, 0x01); /* Frames per sample */
  171. } else if (enc->codec_id == AV_CODEC_ID_QDM2) {
  172. ffio_wfourcc(pb, "kuki");
  173. avio_wb64(pb, enc->extradata_size);
  174. avio_write(pb, enc->extradata, enc->extradata_size);
  175. }
  176. if (av_dict_count(s->metadata)) {
  177. ffio_wfourcc(pb, "info"); //< Information chunk
  178. while ((t = av_dict_get(s->metadata, "", t, AV_DICT_IGNORE_SUFFIX))) {
  179. chunk_size += strlen(t->key) + strlen(t->value) + 2;
  180. }
  181. avio_wb64(pb, chunk_size + 4);
  182. avio_wb32(pb, av_dict_count(s->metadata));
  183. t = NULL;
  184. while ((t = av_dict_get(s->metadata, "", t, AV_DICT_IGNORE_SUFFIX))) {
  185. avio_put_str(pb, t->key);
  186. avio_put_str(pb, t->value);
  187. }
  188. }
  189. ffio_wfourcc(pb, "data"); //< Audio Data chunk
  190. caf->data = avio_tell(pb);
  191. avio_wb64(pb, -1); //< mChunkSize
  192. avio_wb32(pb, 0); //< mEditCount
  193. avio_flush(pb);
  194. return 0;
  195. }
  196. static int caf_write_packet(AVFormatContext *s, AVPacket *pkt)
  197. {
  198. CAFContext *caf = s->priv_data;
  199. avio_write(s->pb, pkt->data, pkt->size);
  200. if (!s->streams[0]->codec->block_align) {
  201. void *pkt_sizes = caf->pkt_sizes;
  202. int i, alloc_size = caf->size_entries_used + 5;
  203. if (alloc_size < 0) {
  204. caf->pkt_sizes = NULL;
  205. } else {
  206. caf->pkt_sizes = av_fast_realloc(caf->pkt_sizes,
  207. &caf->size_buffer_size,
  208. alloc_size);
  209. }
  210. if (!caf->pkt_sizes) {
  211. av_free(pkt_sizes);
  212. return AVERROR(ENOMEM);
  213. }
  214. for (i = 4; i > 0; i--) {
  215. unsigned top = pkt->size >> i * 7;
  216. if (top)
  217. caf->pkt_sizes[caf->size_entries_used++] = 128 | top;
  218. }
  219. caf->pkt_sizes[caf->size_entries_used++] = pkt->size & 127;
  220. caf->packets++;
  221. }
  222. return 0;
  223. }
  224. static int caf_write_trailer(AVFormatContext *s)
  225. {
  226. CAFContext *caf = s->priv_data;
  227. AVIOContext *pb = s->pb;
  228. AVCodecContext *enc = s->streams[0]->codec;
  229. if (pb->seekable) {
  230. int64_t file_size = avio_tell(pb);
  231. avio_seek(pb, caf->data, SEEK_SET);
  232. avio_wb64(pb, file_size - caf->data - 8);
  233. avio_seek(pb, file_size, SEEK_SET);
  234. if (!enc->block_align) {
  235. ffio_wfourcc(pb, "pakt");
  236. avio_wb64(pb, caf->size_entries_used + 24);
  237. avio_wb64(pb, caf->packets); ///< mNumberPackets
  238. avio_wb64(pb, caf->packets * samples_per_packet(enc->codec_id, enc->channels, enc->block_align)); ///< mNumberValidFrames
  239. avio_wb32(pb, 0); ///< mPrimingFrames
  240. avio_wb32(pb, 0); ///< mRemainderFrames
  241. avio_write(pb, caf->pkt_sizes, caf->size_entries_used);
  242. caf->size_buffer_size = 0;
  243. }
  244. avio_flush(pb);
  245. }
  246. av_freep(&caf->pkt_sizes);
  247. return 0;
  248. }
  249. AVOutputFormat ff_caf_muxer = {
  250. .name = "caf",
  251. .long_name = NULL_IF_CONFIG_SMALL("Apple CAF (Core Audio Format)"),
  252. .mime_type = "audio/x-caf",
  253. .extensions = "caf",
  254. .priv_data_size = sizeof(CAFContext),
  255. .audio_codec = AV_CODEC_ID_PCM_S16BE,
  256. .video_codec = AV_CODEC_ID_NONE,
  257. .write_header = caf_write_header,
  258. .write_packet = caf_write_packet,
  259. .write_trailer = caf_write_trailer,
  260. .codec_tag = (const AVCodecTag* const []){ff_codec_caf_tags, 0},
  261. };