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.

194 lines
5.6KB

  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_readwrite.h"
  27. typedef struct {
  28. int64_t data;
  29. } CAFContext;
  30. static uint32_t codec_flags(enum CodecID codec_id) {
  31. switch (codec_id) {
  32. case CODEC_ID_PCM_F32BE:
  33. case CODEC_ID_PCM_F64BE:
  34. return 1; //< kCAFLinearPCMFormatFlagIsFloat
  35. case CODEC_ID_PCM_S16LE:
  36. case CODEC_ID_PCM_S24LE:
  37. case CODEC_ID_PCM_S32LE:
  38. return 2; //< kCAFLinearPCMFormatFlagIsLittleEndian
  39. case CODEC_ID_PCM_F32LE:
  40. case CODEC_ID_PCM_F64LE:
  41. return 3; //< kCAFLinearPCMFormatFlagIsFloat | kCAFLinearPCMFormatFlagIsLittleEndian
  42. default:
  43. return 0;
  44. }
  45. }
  46. static uint32_t samples_per_packet(enum CodecID codec_id, int channels) {
  47. switch (codec_id) {
  48. case CODEC_ID_PCM_S8:
  49. case CODEC_ID_PCM_S16LE:
  50. case CODEC_ID_PCM_S16BE:
  51. case CODEC_ID_PCM_S24LE:
  52. case CODEC_ID_PCM_S24BE:
  53. case CODEC_ID_PCM_S32LE:
  54. case CODEC_ID_PCM_S32BE:
  55. case CODEC_ID_PCM_F32LE:
  56. case CODEC_ID_PCM_F32BE:
  57. case CODEC_ID_PCM_F64LE:
  58. case CODEC_ID_PCM_F64BE:
  59. case CODEC_ID_PCM_ALAW:
  60. case CODEC_ID_PCM_MULAW:
  61. return 1;
  62. case CODEC_ID_MACE3:
  63. case CODEC_ID_MACE6:
  64. return 6;
  65. case CODEC_ID_ADPCM_IMA_QT:
  66. return 64;
  67. case CODEC_ID_AMR_NB:
  68. case CODEC_ID_GSM:
  69. case CODEC_ID_QCELP:
  70. return 160;
  71. case CODEC_ID_GSM_MS:
  72. return 320;
  73. case CODEC_ID_MP1:
  74. return 384;
  75. case CODEC_ID_MP2:
  76. case CODEC_ID_MP3:
  77. return 1152;
  78. case CODEC_ID_AC3:
  79. return 1536;
  80. case CODEC_ID_ALAC:
  81. case CODEC_ID_QDM2:
  82. return 4096;
  83. case CODEC_ID_ADPCM_IMA_WAV:
  84. return (1024 - 4 * channels) * 8 / (4 * channels) + 1;
  85. case CODEC_ID_ADPCM_MS:
  86. return (1024 - 7 * channels) * 2 / channels + 2;
  87. default:
  88. return 0;
  89. }
  90. }
  91. static int caf_write_header(AVFormatContext *s)
  92. {
  93. AVIOContext *pb = s->pb;
  94. AVCodecContext *enc = s->streams[0]->codec;
  95. CAFContext *caf = s->priv_data;
  96. unsigned int codec_tag = ff_codec_get_tag(ff_codec_caf_tags, enc->codec_id);
  97. switch (enc->codec_id) {
  98. case CODEC_ID_PCM_S8:
  99. case CODEC_ID_PCM_S16LE:
  100. case CODEC_ID_PCM_S16BE:
  101. case CODEC_ID_PCM_S24LE:
  102. case CODEC_ID_PCM_S24BE:
  103. case CODEC_ID_PCM_S32LE:
  104. case CODEC_ID_PCM_S32BE:
  105. case CODEC_ID_PCM_F32LE:
  106. case CODEC_ID_PCM_F32BE:
  107. case CODEC_ID_PCM_F64LE:
  108. case CODEC_ID_PCM_F64BE:
  109. case CODEC_ID_PCM_ALAW:
  110. case CODEC_ID_PCM_MULAW:
  111. codec_tag = MKBETAG('l','p','c','m');
  112. }
  113. if (!codec_tag) {
  114. av_log(s, AV_LOG_ERROR, "unsupported codec\n");
  115. return AVERROR_INVALIDDATA;
  116. }
  117. if (!enc->block_align) {
  118. av_log(s, AV_LOG_ERROR, "muxing with unknown or variable packet size not yet supported\n");
  119. return AVERROR_PATCHWELCOME;
  120. }
  121. ffio_wfourcc(pb, "caff"); //< mFileType
  122. avio_wb16(pb, 1); //< mFileVersion
  123. avio_wb16(pb, 0); //< mFileFlags
  124. ffio_wfourcc(pb, "desc"); //< Audio Description chunk
  125. avio_wb64(pb, 32); //< mChunkSize
  126. avio_wb64(pb, av_dbl2int(enc->sample_rate)); //< mSampleRate
  127. avio_wb32(pb, codec_tag); //< mFormatID
  128. avio_wb32(pb, codec_flags(enc->codec_id)); //< mFormatFlags
  129. avio_wb32(pb, enc->block_align); //< mBytesPerPacket
  130. avio_wb32(pb, samples_per_packet(enc->codec_id, enc->channels)); //< mFramesPerPacket
  131. avio_wb32(pb, enc->channels); //< mChannelsPerFrame
  132. avio_wb32(pb, enc->bits_per_coded_sample); //< mBitsPerChannel
  133. if (enc->channel_layout) {
  134. ffio_wfourcc(pb, "chan");
  135. avio_wb64(pb, 12);
  136. ff_mov_write_chan(pb, enc->channel_layout);
  137. }
  138. ffio_wfourcc(pb, "data"); //< Audio Data chunk
  139. caf->data = avio_tell(pb);
  140. avio_wb64(pb, -1); //< mChunkSize
  141. avio_wb32(pb, 0); //< mEditCount
  142. avio_flush(pb);
  143. return 0;
  144. }
  145. static int caf_write_packet(AVFormatContext *s, AVPacket *pkt)
  146. {
  147. avio_write(s->pb, pkt->data, pkt->size);
  148. return 0;
  149. }
  150. static int caf_write_trailer(AVFormatContext *s)
  151. {
  152. AVIOContext *pb = s->pb;
  153. if (pb->seekable) {
  154. CAFContext *caf = s->priv_data;
  155. int64_t file_size = avio_tell(pb);
  156. avio_seek(pb, caf->data, SEEK_SET);
  157. avio_wb64(pb, file_size - caf->data - 8);
  158. avio_seek(pb, file_size, SEEK_SET);
  159. avio_flush(pb);
  160. }
  161. return 0;
  162. }
  163. AVOutputFormat ff_caf_muxer = {
  164. "caf",
  165. NULL_IF_CONFIG_SMALL("Apple Core Audio Format"),
  166. "audio/x-caf",
  167. "caf",
  168. sizeof(CAFContext),
  169. CODEC_ID_PCM_S16BE,
  170. CODEC_ID_NONE,
  171. caf_write_header,
  172. caf_write_packet,
  173. caf_write_trailer,
  174. .codec_tag= (const AVCodecTag* const []){ff_codec_caf_tags, 0},
  175. };