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 #include "libavutil/intfloat.h"
00028
00029 typedef struct {
00030 int64_t data;
00031 uint8_t *pkt_sizes;
00032 int size_buffer_size;
00033 int size_entries_used;
00034 int packets;
00035 } CAFContext;
00036
00037 static uint32_t codec_flags(enum AVCodecID codec_id) {
00038 switch (codec_id) {
00039 case AV_CODEC_ID_PCM_F32BE:
00040 case AV_CODEC_ID_PCM_F64BE:
00041 return 1;
00042 case AV_CODEC_ID_PCM_S16LE:
00043 case AV_CODEC_ID_PCM_S24LE:
00044 case AV_CODEC_ID_PCM_S32LE:
00045 return 2;
00046 case AV_CODEC_ID_PCM_F32LE:
00047 case AV_CODEC_ID_PCM_F64LE:
00048 return 3;
00049 default:
00050 return 0;
00051 }
00052 }
00053
00054 static uint32_t samples_per_packet(enum AVCodecID codec_id, int channels) {
00055 switch (codec_id) {
00056 case AV_CODEC_ID_PCM_S8:
00057 case AV_CODEC_ID_PCM_S16LE:
00058 case AV_CODEC_ID_PCM_S16BE:
00059 case AV_CODEC_ID_PCM_S24LE:
00060 case AV_CODEC_ID_PCM_S24BE:
00061 case AV_CODEC_ID_PCM_S32LE:
00062 case AV_CODEC_ID_PCM_S32BE:
00063 case AV_CODEC_ID_PCM_F32LE:
00064 case AV_CODEC_ID_PCM_F32BE:
00065 case AV_CODEC_ID_PCM_F64LE:
00066 case AV_CODEC_ID_PCM_F64BE:
00067 case AV_CODEC_ID_PCM_ALAW:
00068 case AV_CODEC_ID_PCM_MULAW:
00069 return 1;
00070 case AV_CODEC_ID_MACE3:
00071 case AV_CODEC_ID_MACE6:
00072 return 6;
00073 case AV_CODEC_ID_ADPCM_IMA_QT:
00074 return 64;
00075 case AV_CODEC_ID_AMR_NB:
00076 case AV_CODEC_ID_GSM:
00077 case AV_CODEC_ID_QCELP:
00078 return 160;
00079 case AV_CODEC_ID_GSM_MS:
00080 return 320;
00081 case AV_CODEC_ID_MP1:
00082 return 384;
00083 case AV_CODEC_ID_MP2:
00084 case AV_CODEC_ID_MP3:
00085 return 1152;
00086 case AV_CODEC_ID_AC3:
00087 return 1536;
00088 case AV_CODEC_ID_ALAC:
00089 case AV_CODEC_ID_QDM2:
00090 return 4096;
00091 case AV_CODEC_ID_ADPCM_IMA_WAV:
00092 return (1024 - 4 * channels) * 8 / (4 * channels) + 1;
00093 case AV_CODEC_ID_ADPCM_MS:
00094 return (1024 - 7 * channels) * 2 / channels + 2;
00095 default:
00096 return 0;
00097 }
00098 }
00099
00100 static int caf_write_header(AVFormatContext *s)
00101 {
00102 AVIOContext *pb = s->pb;
00103 AVCodecContext *enc = s->streams[0]->codec;
00104 CAFContext *caf = s->priv_data;
00105 unsigned int codec_tag = ff_codec_get_tag(ff_codec_caf_tags, enc->codec_id);
00106
00107 switch (enc->codec_id) {
00108 case AV_CODEC_ID_AAC:
00109 case AV_CODEC_ID_AC3:
00110 av_log(s, AV_LOG_ERROR, "muxing codec currently unsupported\n");
00111 return AVERROR_PATCHWELCOME;
00112 }
00113
00114 switch (enc->codec_id) {
00115 case AV_CODEC_ID_PCM_S8:
00116 case AV_CODEC_ID_PCM_S16LE:
00117 case AV_CODEC_ID_PCM_S16BE:
00118 case AV_CODEC_ID_PCM_S24LE:
00119 case AV_CODEC_ID_PCM_S24BE:
00120 case AV_CODEC_ID_PCM_S32LE:
00121 case AV_CODEC_ID_PCM_S32BE:
00122 case AV_CODEC_ID_PCM_F32LE:
00123 case AV_CODEC_ID_PCM_F32BE:
00124 case AV_CODEC_ID_PCM_F64LE:
00125 case AV_CODEC_ID_PCM_F64BE:
00126 case AV_CODEC_ID_PCM_ALAW:
00127 case AV_CODEC_ID_PCM_MULAW:
00128 codec_tag = MKTAG('l','p','c','m');
00129 }
00130
00131 if (!codec_tag) {
00132 av_log(s, AV_LOG_ERROR, "unsupported codec\n");
00133 return AVERROR_INVALIDDATA;
00134 }
00135
00136 if (!enc->block_align && !pb->seekable) {
00137 av_log(s, AV_LOG_ERROR, "Muxing variable packet size not supported on non seekable output\n");
00138 return AVERROR_INVALIDDATA;
00139 }
00140
00141 ffio_wfourcc(pb, "caff");
00142 avio_wb16(pb, 1);
00143 avio_wb16(pb, 0);
00144
00145 ffio_wfourcc(pb, "desc");
00146 avio_wb64(pb, 32);
00147 avio_wb64(pb, av_double2int(enc->sample_rate));
00148 avio_wl32(pb, codec_tag);
00149 avio_wb32(pb, codec_flags(enc->codec_id));
00150 avio_wb32(pb, enc->block_align);
00151 avio_wb32(pb, samples_per_packet(enc->codec_id, enc->channels));
00152 avio_wb32(pb, enc->channels);
00153 avio_wb32(pb, av_get_bits_per_sample(enc->codec_id));
00154
00155 if (enc->channel_layout) {
00156 ffio_wfourcc(pb, "chan");
00157 avio_wb64(pb, 12);
00158 ff_mov_write_chan(pb, enc->channel_layout);
00159 }
00160
00161 if (enc->codec_id == AV_CODEC_ID_ALAC) {
00162 ffio_wfourcc(pb, "kuki");
00163 avio_wb64(pb, 12 + enc->extradata_size);
00164 avio_write(pb, "\0\0\0\14frmaalac", 12);
00165 avio_write(pb, enc->extradata, enc->extradata_size);
00166 } else if (enc->codec_id == AV_CODEC_ID_AMR_NB) {
00167 ffio_wfourcc(pb, "kuki");
00168 avio_wb64(pb, 29);
00169 avio_write(pb, "\0\0\0\14frmasamr", 12);
00170 avio_wb32(pb, 0x11);
00171 avio_write(pb, "samrFFMP", 8);
00172 avio_w8(pb, 0);
00173
00174 avio_wb16(pb, 0x81FF);
00175 avio_w8(pb, 0x00);
00176 avio_w8(pb, 0x01);
00177 } else if (enc->codec_id == AV_CODEC_ID_QDM2) {
00178 ffio_wfourcc(pb, "kuki");
00179 avio_wb64(pb, enc->extradata_size);
00180 avio_write(pb, enc->extradata, enc->extradata_size);
00181 }
00182
00183 ffio_wfourcc(pb, "data");
00184 caf->data = avio_tell(pb);
00185 avio_wb64(pb, -1);
00186 avio_wb32(pb, 0);
00187
00188 avio_flush(pb);
00189 return 0;
00190 }
00191
00192 static int caf_write_packet(AVFormatContext *s, AVPacket *pkt)
00193 {
00194 CAFContext *caf = s->priv_data;
00195
00196 avio_write(s->pb, pkt->data, pkt->size);
00197 if (!s->streams[0]->codec->block_align) {
00198 void *pkt_sizes = caf->pkt_sizes;
00199 int i, alloc_size = caf->size_entries_used + 5;
00200 if (alloc_size < 0) {
00201 caf->pkt_sizes = NULL;
00202 } else {
00203 caf->pkt_sizes = av_fast_realloc(caf->pkt_sizes,
00204 &caf->size_buffer_size,
00205 alloc_size);
00206 }
00207 if (!caf->pkt_sizes) {
00208 av_free(pkt_sizes);
00209 return AVERROR(ENOMEM);
00210 }
00211 for (i = 4; i > 0; i--) {
00212 unsigned top = pkt->size >> i * 7;
00213 if (top)
00214 caf->pkt_sizes[caf->size_entries_used++] = 128 | top;
00215 }
00216 caf->pkt_sizes[caf->size_entries_used++] = pkt->size & 127;
00217 caf->packets++;
00218 }
00219 return 0;
00220 }
00221
00222 static int caf_write_trailer(AVFormatContext *s)
00223 {
00224 AVIOContext *pb = s->pb;
00225 AVCodecContext *enc = s->streams[0]->codec;
00226
00227 if (pb->seekable) {
00228 CAFContext *caf = s->priv_data;
00229 int64_t file_size = avio_tell(pb);
00230
00231 avio_seek(pb, caf->data, SEEK_SET);
00232 avio_wb64(pb, file_size - caf->data - 8);
00233 avio_seek(pb, file_size, SEEK_SET);
00234 if (!enc->block_align) {
00235 ffio_wfourcc(pb, "pakt");
00236 avio_wb64(pb, caf->size_entries_used + 24);
00237 avio_wb64(pb, caf->packets);
00238 avio_wb64(pb, caf->packets * samples_per_packet(enc->codec_id, enc->channels));
00239 avio_wb32(pb, 0);
00240 avio_wb32(pb, 0);
00241 avio_write(pb, caf->pkt_sizes, caf->size_entries_used);
00242 av_freep(&caf->pkt_sizes);
00243 caf->size_buffer_size = 0;
00244 }
00245 avio_flush(pb);
00246 }
00247 return 0;
00248 }
00249
00250 AVOutputFormat ff_caf_muxer = {
00251 .name = "caf",
00252 .long_name = NULL_IF_CONFIG_SMALL("Apple Core Audio Format"),
00253 .mime_type = "audio/x-caf",
00254 .extensions = "caf",
00255 .priv_data_size = sizeof(CAFContext),
00256 .audio_codec = AV_CODEC_ID_PCM_S16BE,
00257 .video_codec = AV_CODEC_ID_NONE,
00258 .write_header = caf_write_header,
00259 .write_packet = caf_write_packet,
00260 .write_trailer = caf_write_trailer,
00261 .codec_tag= (const AVCodecTag* const []){ff_codec_caf_tags, 0},
00262 };