• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

libavformat/cafenc.c

Go to the documentation of this file.
00001 /*
00002  * Core Audio Format muxer
00003  * Copyright (c) 2011 Carl Eugen Hoyos
00004  *
00005  * This file is part of FFmpeg.
00006  *
00007  * FFmpeg is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * FFmpeg is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with FFmpeg; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00020  */
00021 
00022 #include "avformat.h"
00023 #include "caf.h"
00024 #include "riff.h"
00025 #include "isom.h"
00026 #include "avio_internal.h"
00027 
00028 typedef struct {
00029     int64_t data;
00030 } CAFContext;
00031 
00032 static uint32_t codec_flags(enum CodecID codec_id) {
00033     switch (codec_id) {
00034     case CODEC_ID_PCM_F32BE:
00035     case CODEC_ID_PCM_F64BE:
00036         return 1; //< kCAFLinearPCMFormatFlagIsFloat
00037     case CODEC_ID_PCM_S16LE:
00038     case CODEC_ID_PCM_S24LE:
00039     case CODEC_ID_PCM_S32LE:
00040         return 2; //< kCAFLinearPCMFormatFlagIsLittleEndian
00041     case CODEC_ID_PCM_F32LE:
00042     case CODEC_ID_PCM_F64LE:
00043         return 3; //< kCAFLinearPCMFormatFlagIsFloat | kCAFLinearPCMFormatFlagIsLittleEndian
00044     default:
00045         return 0;
00046     }
00047 }
00048 
00049 static uint32_t samples_per_packet(enum CodecID codec_id) {
00050     switch (codec_id) {
00051     case CODEC_ID_PCM_S8:
00052     case CODEC_ID_PCM_S16LE:
00053     case CODEC_ID_PCM_S16BE:
00054     case CODEC_ID_PCM_S24LE:
00055     case CODEC_ID_PCM_S24BE:
00056     case CODEC_ID_PCM_S32LE:
00057     case CODEC_ID_PCM_S32BE:
00058     case CODEC_ID_PCM_F32LE:
00059     case CODEC_ID_PCM_F32BE:
00060     case CODEC_ID_PCM_F64LE:
00061     case CODEC_ID_PCM_F64BE:
00062     case CODEC_ID_PCM_ALAW:
00063     case CODEC_ID_PCM_MULAW:
00064         return 1;
00065     case CODEC_ID_MACE3:
00066     case CODEC_ID_MACE6:
00067         return 6;
00068     case CODEC_ID_ADPCM_IMA_QT:
00069         return 64;
00070     case CODEC_ID_AMR_NB:
00071     case CODEC_ID_GSM:
00072     case CODEC_ID_QCELP:
00073         return 160;
00074     case CODEC_ID_MP1:
00075         return 384;
00076     case CODEC_ID_MP2:
00077     case CODEC_ID_MP3:
00078         return 1152;
00079     case CODEC_ID_AC3:
00080         return 1536;
00081     case CODEC_ID_ALAC:
00082     case CODEC_ID_QDM2:
00083         return 4096;
00084     default:
00085         return 0;
00086     }
00087 }
00088 
00089 static int caf_write_header(AVFormatContext *s)
00090 {
00091     AVIOContext *pb = s->pb;
00092     AVCodecContext *enc = s->streams[0]->codec;
00093     CAFContext *caf = s->priv_data;
00094     unsigned int codec_tag = ff_codec_get_tag(ff_codec_caf_tags, enc->codec_id);
00095 
00096     switch (enc->codec_id) {
00097     case CODEC_ID_PCM_S8:
00098     case CODEC_ID_PCM_S16LE:
00099     case CODEC_ID_PCM_S16BE:
00100     case CODEC_ID_PCM_S24LE:
00101     case CODEC_ID_PCM_S24BE:
00102     case CODEC_ID_PCM_S32LE:
00103     case CODEC_ID_PCM_S32BE:
00104     case CODEC_ID_PCM_F32LE:
00105     case CODEC_ID_PCM_F32BE:
00106     case CODEC_ID_PCM_F64LE:
00107     case CODEC_ID_PCM_F64BE:
00108     case CODEC_ID_PCM_ALAW:
00109     case CODEC_ID_PCM_MULAW:
00110         codec_tag = MKBETAG('l','p','c','m');
00111     }
00112 
00113     if (!codec_tag) {
00114         av_log(s, AV_LOG_ERROR, "unsupported codec\n");
00115         return AVERROR_INVALIDDATA;
00116     }
00117 
00118     if (!enc->block_align) {
00119         av_log(s, AV_LOG_ERROR, "muxing with unknown or variable packet size not yet supported\n");
00120         return AVERROR_PATCHWELCOME;
00121     }
00122 
00123     ffio_wfourcc(pb, "caff"); //< mFileType
00124     avio_wb16(pb, 1);         //< mFileVersion
00125     avio_wb16(pb, 0);         //< mFileFlags
00126 
00127     ffio_wfourcc(pb, "desc");                         //< Audio Description chunk
00128     avio_wb64(pb, 32);                                //< mChunkSize
00129     avio_wb64(pb, av_dbl2int(enc->sample_rate));      //< mSampleRate
00130     avio_wb32(pb, codec_tag);                         //< mFormatID
00131     avio_wb32(pb, codec_flags(enc->codec_id));        //< mFormatFlags
00132     avio_wb32(pb, enc->block_align);                  //< mBytesPerPacket
00133     avio_wb32(pb, samples_per_packet(enc->codec_id)); //< mFramesPerPacket
00134     avio_wb32(pb, enc->channels);                     //< mChannelsPerFrame
00135     avio_wb32(pb, enc->bits_per_coded_sample);        //< mBitsPerChannel
00136 
00137     if (enc->channel_layout) {
00138         ffio_wfourcc(pb, "chan");
00139         avio_wb64(pb, 12);
00140         ff_mov_write_chan(pb, enc->channel_layout);
00141     }
00142 
00143     ffio_wfourcc(pb, "data"); //< Audio Data chunk
00144     caf->data = avio_tell(pb);
00145     avio_wb64(pb, -1);        //< mChunkSize
00146     avio_wb32(pb, 0);         //< mEditCount
00147 
00148     avio_flush(pb);
00149     return 0;
00150 }
00151 
00152 static int caf_write_packet(AVFormatContext *s, AVPacket *pkt)
00153 {
00154     avio_write(s->pb, pkt->data, pkt->size);
00155     return 0;
00156 }
00157 
00158 static int caf_write_trailer(AVFormatContext *s)
00159 {
00160     AVIOContext *pb = s->pb;
00161 
00162     if (pb->seekable) {
00163         CAFContext *caf = s->priv_data;
00164         int64_t file_size = avio_tell(pb);
00165 
00166         avio_seek(pb, caf->data, SEEK_SET);
00167         avio_wb64(pb, file_size - caf->data - 8);
00168         avio_seek(pb, file_size, SEEK_SET);
00169         avio_flush(pb);
00170     }
00171     return 0;
00172 }
00173 
00174 AVOutputFormat ff_caf_muxer = {
00175     "caf",
00176     NULL_IF_CONFIG_SMALL("Apple Core Audio Format"),
00177     "audio/x-caf",
00178     "caf",
00179     sizeof(CAFContext),
00180     CODEC_ID_PCM_S16BE,
00181     CODEC_ID_NONE,
00182     caf_write_header,
00183     caf_write_packet,
00184     caf_write_trailer,
00185     .codec_tag= (const AVCodecTag* const []){ff_codec_caf_tags, 0},
00186 };

Generated on Wed Apr 11 2012 07:31:36 for FFmpeg by  doxygen 1.7.1