2 * Core Audio Format muxer
3 * Copyright (c) 2011 Carl Eugen Hoyos
5 * This file is part of FFmpeg.
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.
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.
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
26 #include "avio_internal.h"
27 #include "libavutil/intfloat_readwrite.h"
33 static uint32_t codec_flags(enum CodecID codec_id) {
35 case CODEC_ID_PCM_F32BE:
36 case CODEC_ID_PCM_F64BE:
37 return 1; //< kCAFLinearPCMFormatFlagIsFloat
38 case CODEC_ID_PCM_S16LE:
39 case CODEC_ID_PCM_S24LE:
40 case CODEC_ID_PCM_S32LE:
41 return 2; //< kCAFLinearPCMFormatFlagIsLittleEndian
42 case CODEC_ID_PCM_F32LE:
43 case CODEC_ID_PCM_F64LE:
44 return 3; //< kCAFLinearPCMFormatFlagIsFloat | kCAFLinearPCMFormatFlagIsLittleEndian
50 static uint32_t samples_per_packet(enum CodecID codec_id) {
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:
69 case CODEC_ID_ADPCM_IMA_QT:
90 static int caf_write_header(AVFormatContext *s)
92 AVIOContext *pb = s->pb;
93 AVCodecContext *enc = s->streams[0]->codec;
94 CAFContext *caf = s->priv_data;
95 unsigned int codec_tag = ff_codec_get_tag(ff_codec_caf_tags, enc->codec_id);
97 switch (enc->codec_id) {
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');
115 av_log(s, AV_LOG_ERROR, "unsupported codec\n");
116 return AVERROR_INVALIDDATA;
119 if (!enc->block_align) {
120 av_log(s, AV_LOG_ERROR, "muxing with unknown or variable packet size not yet supported\n");
121 return AVERROR_PATCHWELCOME;
124 ffio_wfourcc(pb, "caff"); //< mFileType
125 avio_wb16(pb, 1); //< mFileVersion
126 avio_wb16(pb, 0); //< mFileFlags
128 ffio_wfourcc(pb, "desc"); //< Audio Description chunk
129 avio_wb64(pb, 32); //< mChunkSize
130 avio_wb64(pb, av_dbl2int(enc->sample_rate)); //< mSampleRate
131 avio_wb32(pb, codec_tag); //< mFormatID
132 avio_wb32(pb, codec_flags(enc->codec_id)); //< mFormatFlags
133 avio_wb32(pb, enc->block_align); //< mBytesPerPacket
134 avio_wb32(pb, samples_per_packet(enc->codec_id)); //< mFramesPerPacket
135 avio_wb32(pb, enc->channels); //< mChannelsPerFrame
136 avio_wb32(pb, enc->bits_per_coded_sample); //< mBitsPerChannel
138 if (enc->channel_layout) {
139 ffio_wfourcc(pb, "chan");
141 ff_mov_write_chan(pb, enc->channel_layout);
144 ffio_wfourcc(pb, "data"); //< Audio Data chunk
145 caf->data = avio_tell(pb);
146 avio_wb64(pb, -1); //< mChunkSize
147 avio_wb32(pb, 0); //< mEditCount
153 static int caf_write_packet(AVFormatContext *s, AVPacket *pkt)
155 avio_write(s->pb, pkt->data, pkt->size);
159 static int caf_write_trailer(AVFormatContext *s)
161 AVIOContext *pb = s->pb;
164 CAFContext *caf = s->priv_data;
165 int64_t file_size = avio_tell(pb);
167 avio_seek(pb, caf->data, SEEK_SET);
168 avio_wb64(pb, file_size - caf->data - 8);
169 avio_seek(pb, file_size, SEEK_SET);
175 AVOutputFormat ff_caf_muxer = {
177 NULL_IF_CONFIG_SMALL("Apple Core Audio Format"),
186 .codec_tag= (const AVCodecTag* const []){ff_codec_caf_tags, 0},