cafenc.c 5.3 KB

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