00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "avformat.h"
00023 #include "caf.h"
00024 #include "riff.h"
00025 #include "isom.h"
00026 #include "avio_internal.h"
00027
00028 typedef struct {
00029 int64_t data;
00030 } CAFContext;
00031
00032 static uint32_t codec_flags(enum CodecID codec_id) {
00033 switch (codec_id) {
00034 case CODEC_ID_PCM_F32BE:
00035 case CODEC_ID_PCM_F64BE:
00036 return 1;
00037 case CODEC_ID_PCM_S16LE:
00038 case CODEC_ID_PCM_S24LE:
00039 case CODEC_ID_PCM_S32LE:
00040 return 2;
00041 case CODEC_ID_PCM_F32LE:
00042 case CODEC_ID_PCM_F64LE:
00043 return 3;
00044 default:
00045 return 0;
00046 }
00047 }
00048
00049 static uint32_t samples_per_packet(enum CodecID codec_id) {
00050 switch (codec_id) {
00051 case CODEC_ID_PCM_S8:
00052 case CODEC_ID_PCM_S16LE:
00053 case CODEC_ID_PCM_S16BE:
00054 case CODEC_ID_PCM_S24LE:
00055 case CODEC_ID_PCM_S24BE:
00056 case CODEC_ID_PCM_S32LE:
00057 case CODEC_ID_PCM_S32BE:
00058 case CODEC_ID_PCM_F32LE:
00059 case CODEC_ID_PCM_F32BE:
00060 case CODEC_ID_PCM_F64LE:
00061 case CODEC_ID_PCM_F64BE:
00062 case CODEC_ID_PCM_ALAW:
00063 case CODEC_ID_PCM_MULAW:
00064 return 1;
00065 case CODEC_ID_MACE3:
00066 case CODEC_ID_MACE6:
00067 return 6;
00068 case CODEC_ID_ADPCM_IMA_QT:
00069 return 64;
00070 case CODEC_ID_AMR_NB:
00071 case CODEC_ID_GSM:
00072 case CODEC_ID_QCELP:
00073 return 160;
00074 case CODEC_ID_MP1:
00075 return 384;
00076 case CODEC_ID_MP2:
00077 case CODEC_ID_MP3:
00078 return 1152;
00079 case CODEC_ID_AC3:
00080 return 1536;
00081 case CODEC_ID_ALAC:
00082 case CODEC_ID_QDM2:
00083 return 4096;
00084 default:
00085 return 0;
00086 }
00087 }
00088
00089 static int caf_write_header(AVFormatContext *s)
00090 {
00091 AVIOContext *pb = s->pb;
00092 AVCodecContext *enc = s->streams[0]->codec;
00093 CAFContext *caf = s->priv_data;
00094 unsigned int codec_tag = ff_codec_get_tag(ff_codec_caf_tags, enc->codec_id);
00095
00096 switch (enc->codec_id) {
00097 case CODEC_ID_PCM_S8:
00098 case CODEC_ID_PCM_S16LE:
00099 case CODEC_ID_PCM_S16BE:
00100 case CODEC_ID_PCM_S24LE:
00101 case CODEC_ID_PCM_S24BE:
00102 case CODEC_ID_PCM_S32LE:
00103 case CODEC_ID_PCM_S32BE:
00104 case CODEC_ID_PCM_F32LE:
00105 case CODEC_ID_PCM_F32BE:
00106 case CODEC_ID_PCM_F64LE:
00107 case CODEC_ID_PCM_F64BE:
00108 case CODEC_ID_PCM_ALAW:
00109 case CODEC_ID_PCM_MULAW:
00110 codec_tag = MKBETAG('l','p','c','m');
00111 }
00112
00113 if (!codec_tag) {
00114 av_log(s, AV_LOG_ERROR, "unsupported codec\n");
00115 return AVERROR_INVALIDDATA;
00116 }
00117
00118 if (!enc->block_align) {
00119 av_log(s, AV_LOG_ERROR, "muxing with unknown or variable packet size not yet supported\n");
00120 return AVERROR_PATCHWELCOME;
00121 }
00122
00123 ffio_wfourcc(pb, "caff");
00124 avio_wb16(pb, 1);
00125 avio_wb16(pb, 0);
00126
00127 ffio_wfourcc(pb, "desc");
00128 avio_wb64(pb, 32);
00129 avio_wb64(pb, av_dbl2int(enc->sample_rate));
00130 avio_wb32(pb, codec_tag);
00131 avio_wb32(pb, codec_flags(enc->codec_id));
00132 avio_wb32(pb, enc->block_align);
00133 avio_wb32(pb, samples_per_packet(enc->codec_id));
00134 avio_wb32(pb, enc->channels);
00135 avio_wb32(pb, enc->bits_per_coded_sample);
00136
00137 if (enc->channel_layout) {
00138 ffio_wfourcc(pb, "chan");
00139 avio_wb64(pb, 12);
00140 ff_mov_write_chan(pb, enc->channel_layout);
00141 }
00142
00143 ffio_wfourcc(pb, "data");
00144 caf->data = avio_tell(pb);
00145 avio_wb64(pb, -1);
00146 avio_wb32(pb, 0);
00147
00148 avio_flush(pb);
00149 return 0;
00150 }
00151
00152 static int caf_write_packet(AVFormatContext *s, AVPacket *pkt)
00153 {
00154 avio_write(s->pb, pkt->data, pkt->size);
00155 return 0;
00156 }
00157
00158 static int caf_write_trailer(AVFormatContext *s)
00159 {
00160 AVIOContext *pb = s->pb;
00161
00162 if (pb->seekable) {
00163 CAFContext *caf = s->priv_data;
00164 int64_t file_size = avio_tell(pb);
00165
00166 avio_seek(pb, caf->data, SEEK_SET);
00167 avio_wb64(pb, file_size - caf->data - 8);
00168 avio_seek(pb, file_size, SEEK_SET);
00169 avio_flush(pb);
00170 }
00171 return 0;
00172 }
00173
00174 AVOutputFormat ff_caf_muxer = {
00175 "caf",
00176 NULL_IF_CONFIG_SMALL("Apple Core Audio Format"),
00177 "audio/x-caf",
00178 "caf",
00179 sizeof(CAFContext),
00180 CODEC_ID_PCM_S16BE,
00181 CODEC_ID_NONE,
00182 caf_write_header,
00183 caf_write_packet,
00184 caf_write_trailer,
00185 .codec_tag= (const AVCodecTag* const []){ff_codec_caf_tags, 0},
00186 };