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