cafenc.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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 "riff.h"
  24. #include "isom.h"
  25. #include "avio_internal.h"
  26. #include "libavutil/intfloat.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 CodecID codec_id) {
  35. switch (codec_id) {
  36. case CODEC_ID_PCM_F32BE:
  37. case CODEC_ID_PCM_F64BE:
  38. return 1; //< kCAFLinearPCMFormatFlagIsFloat
  39. case CODEC_ID_PCM_S16LE:
  40. case CODEC_ID_PCM_S24LE:
  41. case CODEC_ID_PCM_S32LE:
  42. return 2; //< kCAFLinearPCMFormatFlagIsLittleEndian
  43. case CODEC_ID_PCM_F32LE:
  44. case CODEC_ID_PCM_F64LE:
  45. return 3; //< kCAFLinearPCMFormatFlagIsFloat | kCAFLinearPCMFormatFlagIsLittleEndian
  46. default:
  47. return 0;
  48. }
  49. }
  50. static uint32_t samples_per_packet(enum CodecID codec_id, int channels) {
  51. switch (codec_id) {
  52. case CODEC_ID_PCM_S8:
  53. case CODEC_ID_PCM_S16LE:
  54. case CODEC_ID_PCM_S16BE:
  55. case CODEC_ID_PCM_S24LE:
  56. case CODEC_ID_PCM_S24BE:
  57. case CODEC_ID_PCM_S32LE:
  58. case CODEC_ID_PCM_S32BE:
  59. case CODEC_ID_PCM_F32LE:
  60. case CODEC_ID_PCM_F32BE:
  61. case CODEC_ID_PCM_F64LE:
  62. case CODEC_ID_PCM_F64BE:
  63. case CODEC_ID_PCM_ALAW:
  64. case CODEC_ID_PCM_MULAW:
  65. return 1;
  66. case CODEC_ID_MACE3:
  67. case CODEC_ID_MACE6:
  68. return 6;
  69. case CODEC_ID_ADPCM_IMA_QT:
  70. return 64;
  71. case CODEC_ID_AMR_NB:
  72. case CODEC_ID_GSM:
  73. case CODEC_ID_QCELP:
  74. return 160;
  75. case CODEC_ID_GSM_MS:
  76. return 320;
  77. case CODEC_ID_MP1:
  78. return 384;
  79. case CODEC_ID_MP2:
  80. case CODEC_ID_MP3:
  81. return 1152;
  82. case CODEC_ID_AC3:
  83. return 1536;
  84. case CODEC_ID_ALAC:
  85. case CODEC_ID_QDM2:
  86. return 4096;
  87. case CODEC_ID_ADPCM_IMA_WAV:
  88. return (1024 - 4 * channels) * 8 / (4 * channels) + 1;
  89. case CODEC_ID_ADPCM_MS:
  90. return (1024 - 7 * channels) * 2 / channels + 2;
  91. default:
  92. return 0;
  93. }
  94. }
  95. static int caf_write_header(AVFormatContext *s)
  96. {
  97. AVIOContext *pb = s->pb;
  98. AVCodecContext *enc = s->streams[0]->codec;
  99. CAFContext *caf = s->priv_data;
  100. unsigned int codec_tag = ff_codec_get_tag(ff_codec_caf_tags, enc->codec_id);
  101. switch (enc->codec_id) {
  102. case CODEC_ID_AAC:
  103. case CODEC_ID_AC3:
  104. av_log(s, AV_LOG_ERROR, "muxing codec currently unsupported\n");
  105. return AVERROR_PATCHWELCOME;
  106. }
  107. switch (enc->codec_id) {
  108. case CODEC_ID_PCM_S8:
  109. case CODEC_ID_PCM_S16LE:
  110. case CODEC_ID_PCM_S16BE:
  111. case CODEC_ID_PCM_S24LE:
  112. case CODEC_ID_PCM_S24BE:
  113. case CODEC_ID_PCM_S32LE:
  114. case CODEC_ID_PCM_S32BE:
  115. case CODEC_ID_PCM_F32LE:
  116. case CODEC_ID_PCM_F32BE:
  117. case CODEC_ID_PCM_F64LE:
  118. case CODEC_ID_PCM_F64BE:
  119. case CODEC_ID_PCM_ALAW:
  120. case CODEC_ID_PCM_MULAW:
  121. codec_tag = MKTAG('l','p','c','m');
  122. }
  123. if (!codec_tag) {
  124. av_log(s, AV_LOG_ERROR, "unsupported codec\n");
  125. return AVERROR_INVALIDDATA;
  126. }
  127. if (!enc->block_align && !pb->seekable) {
  128. av_log(s, AV_LOG_ERROR, "Muxing variable packet size not supported on non seekable output\n");
  129. return AVERROR_INVALIDDATA;
  130. }
  131. ffio_wfourcc(pb, "caff"); //< mFileType
  132. avio_wb16(pb, 1); //< mFileVersion
  133. avio_wb16(pb, 0); //< mFileFlags
  134. ffio_wfourcc(pb, "desc"); //< Audio Description chunk
  135. avio_wb64(pb, 32); //< mChunkSize
  136. avio_wb64(pb, av_double2int(enc->sample_rate)); //< mSampleRate
  137. avio_wl32(pb, codec_tag); //< mFormatID
  138. avio_wb32(pb, codec_flags(enc->codec_id)); //< mFormatFlags
  139. avio_wb32(pb, enc->block_align); //< mBytesPerPacket
  140. avio_wb32(pb, samples_per_packet(enc->codec_id, enc->channels)); //< mFramesPerPacket
  141. avio_wb32(pb, enc->channels); //< mChannelsPerFrame
  142. avio_wb32(pb, av_get_bits_per_sample(enc->codec_id)); //< mBitsPerChannel
  143. if (enc->channel_layout) {
  144. ffio_wfourcc(pb, "chan");
  145. avio_wb64(pb, 12);
  146. ff_mov_write_chan(pb, enc->channel_layout);
  147. }
  148. if (enc->codec_id == CODEC_ID_ALAC) {
  149. ffio_wfourcc(pb, "kuki");
  150. avio_wb64(pb, 12 + enc->extradata_size);
  151. avio_write(pb, "\0\0\0\14frmaalac", 12);
  152. avio_write(pb, enc->extradata, enc->extradata_size);
  153. } else if (enc->codec_id == CODEC_ID_AMR_NB) {
  154. ffio_wfourcc(pb, "kuki");
  155. avio_wb64(pb, 29);
  156. avio_write(pb, "\0\0\0\14frmasamr", 12);
  157. avio_wb32(pb, 0x11); /* size */
  158. avio_write(pb, "samrFFMP", 8);
  159. avio_w8(pb, 0); /* decoder version */
  160. avio_wb16(pb, 0x81FF); /* Mode set (all modes for AMR_NB) */
  161. avio_w8(pb, 0x00); /* Mode change period (no restriction) */
  162. avio_w8(pb, 0x01); /* Frames per sample */
  163. } else if (enc->codec_id == CODEC_ID_QDM2) {
  164. ffio_wfourcc(pb, "kuki");
  165. avio_wb64(pb, enc->extradata_size);
  166. avio_write(pb, enc->extradata, enc->extradata_size);
  167. }
  168. ffio_wfourcc(pb, "data"); //< Audio Data chunk
  169. caf->data = avio_tell(pb);
  170. avio_wb64(pb, -1); //< mChunkSize
  171. avio_wb32(pb, 0); //< mEditCount
  172. avio_flush(pb);
  173. return 0;
  174. }
  175. static int caf_write_packet(AVFormatContext *s, AVPacket *pkt)
  176. {
  177. CAFContext *caf = s->priv_data;
  178. avio_write(s->pb, pkt->data, pkt->size);
  179. if (!s->streams[0]->codec->block_align) {
  180. void *pkt_sizes = caf->pkt_sizes;
  181. int i, alloc_size = caf->size_entries_used + 5;
  182. if (alloc_size < 0) {
  183. caf->pkt_sizes = NULL;
  184. } else {
  185. caf->pkt_sizes = av_fast_realloc(caf->pkt_sizes,
  186. &caf->size_buffer_size,
  187. alloc_size);
  188. }
  189. if (!caf->pkt_sizes) {
  190. av_free(pkt_sizes);
  191. return AVERROR(ENOMEM);
  192. }
  193. for (i = 4; i > 0; i--) {
  194. unsigned top = pkt->size >> i * 7;
  195. if (top)
  196. caf->pkt_sizes[caf->size_entries_used++] = 128 | top;
  197. }
  198. caf->pkt_sizes[caf->size_entries_used++] = pkt->size & 127;
  199. caf->packets++;
  200. }
  201. return 0;
  202. }
  203. static int caf_write_trailer(AVFormatContext *s)
  204. {
  205. AVIOContext *pb = s->pb;
  206. AVCodecContext *enc = s->streams[0]->codec;
  207. if (pb->seekable) {
  208. CAFContext *caf = s->priv_data;
  209. int64_t file_size = avio_tell(pb);
  210. avio_seek(pb, caf->data, SEEK_SET);
  211. avio_wb64(pb, file_size - caf->data - 8);
  212. avio_seek(pb, file_size, SEEK_SET);
  213. if (!enc->block_align) {
  214. ffio_wfourcc(pb, "pakt");
  215. avio_wb64(pb, caf->size_entries_used + 24);
  216. avio_wb64(pb, caf->packets); ///< mNumberPackets
  217. avio_wb64(pb, caf->packets * samples_per_packet(enc->codec_id, enc->channels)); ///< mNumberValidFrames
  218. avio_wb32(pb, 0); ///< mPrimingFrames
  219. avio_wb32(pb, 0); ///< mRemainderFrames
  220. avio_write(pb, caf->pkt_sizes, caf->size_entries_used);
  221. av_freep(&caf->pkt_sizes);
  222. caf->size_buffer_size = 0;
  223. }
  224. avio_flush(pb);
  225. }
  226. return 0;
  227. }
  228. AVOutputFormat ff_caf_muxer = {
  229. .name = "caf",
  230. .long_name = NULL_IF_CONFIG_SMALL("Apple Core Audio Format"),
  231. .mime_type = "audio/x-caf",
  232. .extensions = "caf",
  233. .priv_data_size = sizeof(CAFContext),
  234. .audio_codec = CODEC_ID_PCM_S16BE,
  235. .video_codec = CODEC_ID_NONE,
  236. .write_header = caf_write_header,
  237. .write_packet = caf_write_packet,
  238. .write_trailer = caf_write_trailer,
  239. .codec_tag= (const AVCodecTag* const []){ff_codec_caf_tags, 0},
  240. };