]> git.sesse.net Git - ffmpeg/blob - libavformat/cafenc.c
Merge remote-tracking branch 'qatar/master'
[ffmpeg] / libavformat / cafenc.c
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
22 #include "avformat.h"
23 #include "caf.h"
24 #include "riff.h"
25 #include "isom.h"
26 #include "avio_internal.h"
27 #include "libavutil/intfloat_readwrite.h"
28
29 typedef struct {
30     int64_t data;
31     uint8_t *pkt_sizes;
32     int size_buffer_size;
33     int size_entries_used;
34     int packets;
35 } CAFContext;
36
37 static uint32_t codec_flags(enum CodecID codec_id) {
38     switch (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
49     default:
50         return 0;
51     }
52 }
53
54 static uint32_t samples_per_packet(enum CodecID codec_id, int channels) {
55     switch (codec_id) {
56     case CODEC_ID_PCM_S8:
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:
69         return 1;
70     case CODEC_ID_MACE3:
71     case CODEC_ID_MACE6:
72         return 6;
73     case CODEC_ID_ADPCM_IMA_QT:
74         return 64;
75     case CODEC_ID_AMR_NB:
76     case CODEC_ID_GSM:
77     case CODEC_ID_QCELP:
78         return 160;
79     case CODEC_ID_GSM_MS:
80         return 320;
81     case CODEC_ID_MP1:
82         return 384;
83     case CODEC_ID_MP2:
84     case CODEC_ID_MP3:
85         return 1152;
86     case CODEC_ID_AC3:
87         return 1536;
88     case CODEC_ID_ALAC:
89     case CODEC_ID_QDM2:
90         return 4096;
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;
95     default:
96         return 0;
97     }
98 }
99
100 static int caf_write_header(AVFormatContext *s)
101 {
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);
106
107     switch (enc->codec_id) {
108     case CODEC_ID_AAC:
109     case CODEC_ID_AC3:
110     case CODEC_ID_ALAC:
111     case CODEC_ID_AMR_NB:
112     case CODEC_ID_QCELP:
113     case CODEC_ID_QDM2:
114         av_log(s, AV_LOG_ERROR, "muxing codec currently unsupported\n");
115         return AVERROR_PATCHWELCOME;
116     }
117
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');
133     }
134
135     if (!codec_tag) {
136         av_log(s, AV_LOG_ERROR, "unsupported codec\n");
137         return AVERROR_INVALIDDATA;
138     }
139
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;
143     }
144
145     ffio_wfourcc(pb, "caff"); //< mFileType
146     avio_wb16(pb, 1);         //< mFileVersion
147     avio_wb16(pb, 0);         //< mFileFlags
148
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
158
159     if (enc->channel_layout) {
160         ffio_wfourcc(pb, "chan");
161         avio_wb64(pb, 12);
162         ff_mov_write_chan(pb, enc->channel_layout);
163     }
164
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
169
170     avio_flush(pb);
171     return 0;
172 }
173
174 static int caf_write_packet(AVFormatContext *s, AVPacket *pkt)
175 {
176     CAFContext *caf = s->priv_data;
177
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;
184         } else {
185             caf->pkt_sizes = av_fast_realloc(caf->pkt_sizes,
186                                              &caf->size_buffer_size,
187                                              alloc_size);
188         }
189         if (!caf->pkt_sizes) {
190             av_free(pkt_sizes);
191             return AVERROR(ENOMEM);
192         }
193         for (i = 4; i > 0; i--) {
194             unsigned top = pkt->size >> i * 7;
195             if (top)
196                 caf->pkt_sizes[caf->size_entries_used++] = 128 | top;
197         }
198         caf->pkt_sizes[caf->size_entries_used++] = pkt->size & 127;
199         caf->packets++;
200     }
201     return 0;
202 }
203
204 static int caf_write_trailer(AVFormatContext *s)
205 {
206     AVIOContext *pb = s->pb;
207     AVCodecContext *enc = s->streams[0]->codec;
208
209     if (pb->seekable) {
210         CAFContext *caf = s->priv_data;
211         int64_t file_size = avio_tell(pb);
212
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;
226         }
227         avio_flush(pb);
228     }
229     return 0;
230 }
231
232 AVOutputFormat ff_caf_muxer = {
233     "caf",
234     NULL_IF_CONFIG_SMALL("Apple Core Audio Format"),
235     "audio/x-caf",
236     "caf",
237     sizeof(CAFContext),
238     CODEC_ID_PCM_S16BE,
239     CODEC_ID_NONE,
240     caf_write_header,
241     caf_write_packet,
242     caf_write_trailer,
243     .codec_tag= (const AVCodecTag* const []){ff_codec_caf_tags, 0},
244 };