3 * Copyright (c) 2006 Patrick Guimond
5 * This file is part of Libav.
7 * Libav 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.
12 * Libav 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.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "libavutil/intfloat.h"
26 #include "avio_internal.h"
34 static int aiff_write_header(AVFormatContext *s)
36 AIFFOutputContext *aiff = s->priv_data;
37 AVIOContext *pb = s->pb;
38 AVCodecContext *enc = s->streams[0]->codec;
42 /* First verify if format is ok */
45 if (enc->codec_tag != MKTAG('N','O','N','E'))
48 /* FORM AIFF header */
49 ffio_wfourcc(pb, "FORM");
50 aiff->form = avio_tell(pb);
51 avio_wb32(pb, 0); /* file length */
52 ffio_wfourcc(pb, aifc ? "AIFC" : "AIFF");
54 if (aifc) { // compressed audio
55 enc->bits_per_coded_sample = 16;
56 if (!enc->block_align) {
57 av_log(s, AV_LOG_ERROR, "block align not set\n");
61 ffio_wfourcc(pb, "FVER");
63 avio_wb32(pb, 0xA2805140);
67 ffio_wfourcc(pb, "COMM");
68 avio_wb32(pb, aifc ? 24 : 18); /* size */
69 avio_wb16(pb, enc->channels); /* Number of channels */
71 aiff->frames = avio_tell(pb);
72 avio_wb32(pb, 0); /* Number of frames */
74 if (!enc->bits_per_coded_sample)
75 enc->bits_per_coded_sample = av_get_bits_per_sample(enc->codec_id);
76 if (!enc->bits_per_coded_sample) {
77 av_log(s, AV_LOG_ERROR, "could not compute bits per sample\n");
80 if (!enc->block_align)
81 enc->block_align = (enc->bits_per_coded_sample * enc->channels) >> 3;
83 avio_wb16(pb, enc->bits_per_coded_sample); /* Sample size */
85 sample_rate = av_double2int(enc->sample_rate);
86 avio_wb16(pb, (sample_rate >> 52) + (16383 - 1023));
87 avio_wb64(pb, UINT64_C(1) << 63 | sample_rate << 11);
90 avio_wl32(pb, enc->codec_tag);
94 /* Sound data chunk */
95 ffio_wfourcc(pb, "SSND");
96 aiff->ssnd = avio_tell(pb); /* Sound chunk size */
97 avio_wb32(pb, 0); /* Sound samples data size */
98 avio_wb32(pb, 0); /* Data offset */
99 avio_wb32(pb, 0); /* Block-size (block align) */
101 avpriv_set_pts_info(s->streams[0], 64, 1, s->streams[0]->codec->sample_rate);
103 /* Data is starting here */
109 static int aiff_write_packet(AVFormatContext *s, AVPacket *pkt)
111 AVIOContext *pb = s->pb;
112 avio_write(pb, pkt->data, pkt->size);
116 static int aiff_write_trailer(AVFormatContext *s)
118 AVIOContext *pb = s->pb;
119 AIFFOutputContext *aiff = s->priv_data;
120 AVCodecContext *enc = s->streams[0]->codec;
122 /* Chunks sizes must be even */
123 int64_t file_size, end_size;
124 end_size = file_size = avio_tell(pb);
130 if (s->pb->seekable) {
132 avio_seek(pb, aiff->form, SEEK_SET);
133 avio_wb32(pb, file_size - aiff->form - 4);
135 /* Number of sample frames */
136 avio_seek(pb, aiff->frames, SEEK_SET);
137 avio_wb32(pb, (file_size-aiff->ssnd-12)/enc->block_align);
139 /* Sound Data chunk size */
140 avio_seek(pb, aiff->ssnd, SEEK_SET);
141 avio_wb32(pb, file_size - aiff->ssnd - 4);
143 /* return to the end */
144 avio_seek(pb, end_size, SEEK_SET);
152 AVOutputFormat ff_aiff_muxer = {
154 .long_name = NULL_IF_CONFIG_SMALL("Audio IFF"),
155 .mime_type = "audio/aiff",
156 .extensions = "aif,aiff,afc,aifc",
157 .priv_data_size = sizeof(AIFFOutputContext),
158 .audio_codec = CODEC_ID_PCM_S16BE,
159 .video_codec = CODEC_ID_NONE,
160 .write_header = aiff_write_header,
161 .write_packet = aiff_write_packet,
162 .write_trailer = aiff_write_trailer,
163 .codec_tag = (const AVCodecTag* const []){ ff_codec_aiff_tags, 0 },