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 int size_entries_used;
37 static uint32_t codec_flags(enum CodecID codec_id) {
39 case CODEC_ID_PCM_F32BE:
40 case CODEC_ID_PCM_F64BE:
41 return 1; //< kCAFLinearPCMFormatFlagIsFloat
42 case CODEC_ID_PCM_S16LE:
43 case CODEC_ID_PCM_S24LE:
44 case CODEC_ID_PCM_S32LE:
45 return 2; //< kCAFLinearPCMFormatFlagIsLittleEndian
46 case CODEC_ID_PCM_F32LE:
47 case CODEC_ID_PCM_F64LE:
48 return 3; //< kCAFLinearPCMFormatFlagIsFloat | kCAFLinearPCMFormatFlagIsLittleEndian
54 static uint32_t samples_per_packet(enum CodecID codec_id, int channels) {
57 case CODEC_ID_PCM_S16LE:
58 case CODEC_ID_PCM_S16BE:
59 case CODEC_ID_PCM_S24LE:
60 case CODEC_ID_PCM_S24BE:
61 case CODEC_ID_PCM_S32LE:
62 case CODEC_ID_PCM_S32BE:
63 case CODEC_ID_PCM_F32LE:
64 case CODEC_ID_PCM_F32BE:
65 case CODEC_ID_PCM_F64LE:
66 case CODEC_ID_PCM_F64BE:
67 case CODEC_ID_PCM_ALAW:
68 case CODEC_ID_PCM_MULAW:
73 case CODEC_ID_ADPCM_IMA_QT:
91 case CODEC_ID_ADPCM_IMA_WAV:
92 return (1024 - 4 * channels) * 8 / (4 * channels) + 1;
93 case CODEC_ID_ADPCM_MS:
94 return (1024 - 7 * channels) * 2 / channels + 2;
100 static int caf_write_header(AVFormatContext *s)
102 AVIOContext *pb = s->pb;
103 AVCodecContext *enc = s->streams[0]->codec;
104 CAFContext *caf = s->priv_data;
105 unsigned int codec_tag = ff_codec_get_tag(ff_codec_caf_tags, enc->codec_id);
107 switch (enc->codec_id) {
111 case CODEC_ID_AMR_NB:
114 av_log(s, AV_LOG_ERROR, "muxing codec currently unsupported\n");
115 return AVERROR_PATCHWELCOME;
118 switch (enc->codec_id) {
119 case CODEC_ID_PCM_S8:
120 case CODEC_ID_PCM_S16LE:
121 case CODEC_ID_PCM_S16BE:
122 case CODEC_ID_PCM_S24LE:
123 case CODEC_ID_PCM_S24BE:
124 case CODEC_ID_PCM_S32LE:
125 case CODEC_ID_PCM_S32BE:
126 case CODEC_ID_PCM_F32LE:
127 case CODEC_ID_PCM_F32BE:
128 case CODEC_ID_PCM_F64LE:
129 case CODEC_ID_PCM_F64BE:
130 case CODEC_ID_PCM_ALAW:
131 case CODEC_ID_PCM_MULAW:
132 codec_tag = MKTAG('l','p','c','m');
136 av_log(s, AV_LOG_ERROR, "unsupported codec\n");
137 return AVERROR_INVALIDDATA;
140 if (!enc->block_align && !pb->seekable) {
141 av_log(s, AV_LOG_ERROR, "Muxing variable packet size not supported on non seekable output\n");
142 return AVERROR_INVALIDDATA;
145 ffio_wfourcc(pb, "caff"); //< mFileType
146 avio_wb16(pb, 1); //< mFileVersion
147 avio_wb16(pb, 0); //< mFileFlags
149 ffio_wfourcc(pb, "desc"); //< Audio Description chunk
150 avio_wb64(pb, 32); //< mChunkSize
151 avio_wb64(pb, av_dbl2int(enc->sample_rate)); //< mSampleRate
152 avio_wl32(pb, codec_tag); //< mFormatID
153 avio_wb32(pb, codec_flags(enc->codec_id)); //< mFormatFlags
154 avio_wb32(pb, enc->block_align); //< mBytesPerPacket
155 avio_wb32(pb, samples_per_packet(enc->codec_id, enc->channels)); //< mFramesPerPacket
156 avio_wb32(pb, enc->channels); //< mChannelsPerFrame
157 avio_wb32(pb, av_get_bits_per_sample(enc->codec_id)); //< mBitsPerChannel
159 if (enc->channel_layout) {
160 ffio_wfourcc(pb, "chan");
162 ff_mov_write_chan(pb, enc->channel_layout);
165 ffio_wfourcc(pb, "data"); //< Audio Data chunk
166 caf->data = avio_tell(pb);
167 avio_wb64(pb, -1); //< mChunkSize
168 avio_wb32(pb, 0); //< mEditCount
174 static int caf_write_packet(AVFormatContext *s, AVPacket *pkt)
176 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;
185 caf->pkt_sizes = av_fast_realloc(caf->pkt_sizes,
186 &caf->size_buffer_size,
189 if (!caf->pkt_sizes) {
191 return AVERROR(ENOMEM);
193 for (i = 4; i > 0; i--) {
194 unsigned top = pkt->size >> i * 7;
196 caf->pkt_sizes[caf->size_entries_used++] = 128 | top;
198 caf->pkt_sizes[caf->size_entries_used++] = pkt->size & 127;
204 static int caf_write_trailer(AVFormatContext *s)
206 AVIOContext *pb = s->pb;
207 AVCodecContext *enc = s->streams[0]->codec;
210 CAFContext *caf = s->priv_data;
211 int64_t file_size = avio_tell(pb);
213 avio_seek(pb, caf->data, SEEK_SET);
214 avio_wb64(pb, file_size - caf->data - 8);
215 avio_seek(pb, file_size, SEEK_SET);
216 if (!enc->block_align) {
217 ffio_wfourcc(pb, "pakt");
218 avio_wb64(pb, caf->size_entries_used + 24);
219 avio_wb64(pb, caf->packets); ///< mNumberPackets
220 avio_wb64(pb, caf->packets * samples_per_packet(enc->codec_id, enc->channels)); ///< mNumberValidFrames
221 avio_wb32(pb, 0); ///< mPrimingFrames
222 avio_wb32(pb, 0); ///< mRemainderFrames
223 avio_write(pb, caf->pkt_sizes, caf->size_entries_used);
224 av_freep(&caf->pkt_sizes);
225 caf->size_buffer_size = 0;
232 AVOutputFormat ff_caf_muxer = {
234 NULL_IF_CONFIG_SMALL("Apple Core Audio Format"),
243 .codec_tag= (const AVCodecTag* const []){ff_codec_caf_tags, 0},