Libav 0.7.1
libavformat/aiffenc.c
Go to the documentation of this file.
00001 /*
00002  * AIFF/AIFF-C muxer
00003  * Copyright (c) 2006  Patrick Guimond
00004  *
00005  * This file is part of Libav.
00006  *
00007  * Libav 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  * Libav 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 Libav; 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 "aiff.h"
00024 #include "avio_internal.h"
00025 
00026 typedef struct {
00027     int64_t form;
00028     int64_t frames;
00029     int64_t ssnd;
00030 } AIFFOutputContext;
00031 
00032 static int aiff_write_header(AVFormatContext *s)
00033 {
00034     AIFFOutputContext *aiff = s->priv_data;
00035     AVIOContext *pb = s->pb;
00036     AVCodecContext *enc = s->streams[0]->codec;
00037     AVExtFloat sample_rate;
00038     int aifc = 0;
00039 
00040     /* First verify if format is ok */
00041     if (!enc->codec_tag)
00042         return -1;
00043     if (enc->codec_tag != MKTAG('N','O','N','E'))
00044         aifc = 1;
00045 
00046     /* FORM AIFF header */
00047     ffio_wfourcc(pb, "FORM");
00048     aiff->form = avio_tell(pb);
00049     avio_wb32(pb, 0);                    /* file length */
00050     ffio_wfourcc(pb, aifc ? "AIFC" : "AIFF");
00051 
00052     if (aifc) { // compressed audio
00053         enc->bits_per_coded_sample = 16;
00054         if (!enc->block_align) {
00055             av_log(s, AV_LOG_ERROR, "block align not set\n");
00056             return -1;
00057         }
00058         /* Version chunk */
00059         ffio_wfourcc(pb, "FVER");
00060         avio_wb32(pb, 4);
00061         avio_wb32(pb, 0xA2805140);
00062     }
00063 
00064     /* Common chunk */
00065     ffio_wfourcc(pb, "COMM");
00066     avio_wb32(pb, aifc ? 24 : 18); /* size */
00067     avio_wb16(pb, enc->channels);  /* Number of channels */
00068 
00069     aiff->frames = avio_tell(pb);
00070     avio_wb32(pb, 0);              /* Number of frames */
00071 
00072     if (!enc->bits_per_coded_sample)
00073         enc->bits_per_coded_sample = av_get_bits_per_sample(enc->codec_id);
00074     if (!enc->bits_per_coded_sample) {
00075         av_log(s, AV_LOG_ERROR, "could not compute bits per sample\n");
00076         return -1;
00077     }
00078     if (!enc->block_align)
00079         enc->block_align = (enc->bits_per_coded_sample * enc->channels) >> 3;
00080 
00081     avio_wb16(pb, enc->bits_per_coded_sample); /* Sample size */
00082 
00083     sample_rate = av_dbl2ext((double)enc->sample_rate);
00084     avio_write(pb, (uint8_t*)&sample_rate, sizeof(sample_rate));
00085 
00086     if (aifc) {
00087         avio_wl32(pb, enc->codec_tag);
00088         avio_wb16(pb, 0);
00089     }
00090 
00091     /* Sound data chunk */
00092     ffio_wfourcc(pb, "SSND");
00093     aiff->ssnd = avio_tell(pb);         /* Sound chunk size */
00094     avio_wb32(pb, 0);                    /* Sound samples data size */
00095     avio_wb32(pb, 0);                    /* Data offset */
00096     avio_wb32(pb, 0);                    /* Block-size (block align) */
00097 
00098     av_set_pts_info(s->streams[0], 64, 1, s->streams[0]->codec->sample_rate);
00099 
00100     /* Data is starting here */
00101     avio_flush(pb);
00102 
00103     return 0;
00104 }
00105 
00106 static int aiff_write_packet(AVFormatContext *s, AVPacket *pkt)
00107 {
00108     AVIOContext *pb = s->pb;
00109     avio_write(pb, pkt->data, pkt->size);
00110     return 0;
00111 }
00112 
00113 static int aiff_write_trailer(AVFormatContext *s)
00114 {
00115     AVIOContext *pb = s->pb;
00116     AIFFOutputContext *aiff = s->priv_data;
00117     AVCodecContext *enc = s->streams[0]->codec;
00118 
00119     /* Chunks sizes must be even */
00120     int64_t file_size, end_size;
00121     end_size = file_size = avio_tell(pb);
00122     if (file_size & 1) {
00123         avio_w8(pb, 0);
00124         end_size++;
00125     }
00126 
00127     if (s->pb->seekable) {
00128         /* File length */
00129         avio_seek(pb, aiff->form, SEEK_SET);
00130         avio_wb32(pb, file_size - aiff->form - 4);
00131 
00132         /* Number of sample frames */
00133         avio_seek(pb, aiff->frames, SEEK_SET);
00134         avio_wb32(pb, (file_size-aiff->ssnd-12)/enc->block_align);
00135 
00136         /* Sound Data chunk size */
00137         avio_seek(pb, aiff->ssnd, SEEK_SET);
00138         avio_wb32(pb, file_size - aiff->ssnd - 4);
00139 
00140         /* return to the end */
00141         avio_seek(pb, end_size, SEEK_SET);
00142 
00143         avio_flush(pb);
00144     }
00145 
00146     return 0;
00147 }
00148 
00149 AVOutputFormat ff_aiff_muxer = {
00150     "aiff",
00151     NULL_IF_CONFIG_SMALL("Audio IFF"),
00152     "audio/aiff",
00153     "aif,aiff,afc,aifc",
00154     sizeof(AIFFOutputContext),
00155     CODEC_ID_PCM_S16BE,
00156     CODEC_ID_NONE,
00157     aiff_write_header,
00158     aiff_write_packet,
00159     aiff_write_trailer,
00160     .codec_tag= (const AVCodecTag* const []){ff_codec_aiff_tags, 0},
00161 };