Add missing #includes for avutil.h, required for the AV_VERSION* macros.
[ffmpeg-lucabe.git] / libavformat / aiffenc.c
blobe3c6a0b1bf9099b9aff82799fd05c91ef53884a8
1 /*
2 * AIFF/AIFF-C muxer
3 * Copyright (c) 2006 Patrick Guimond
5 * This file is part of FFmpeg.
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.
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.
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
22 #include "avformat.h"
23 #include "aiff.h"
25 typedef struct {
26 int64_t form;
27 int64_t frames;
28 int64_t ssnd;
29 } AIFFOutputContext;
31 static int aiff_write_header(AVFormatContext *s)
33 AIFFOutputContext *aiff = s->priv_data;
34 ByteIOContext *pb = s->pb;
35 AVCodecContext *enc = s->streams[0]->codec;
36 AVExtFloat sample_rate;
37 int aifc = 0;
39 /* First verify if format is ok */
40 if (!enc->codec_tag)
41 return -1;
42 if (enc->codec_tag != MKTAG('N','O','N','E'))
43 aifc = 1;
45 /* FORM AIFF header */
46 put_tag(pb, "FORM");
47 aiff->form = url_ftell(pb);
48 put_be32(pb, 0); /* file length */
49 put_tag(pb, aifc ? "AIFC" : "AIFF");
51 if (aifc) { // compressed audio
52 enc->bits_per_coded_sample = 16;
53 if (!enc->block_align) {
54 av_log(s, AV_LOG_ERROR, "block align not set\n");
55 return -1;
57 /* Version chunk */
58 put_tag(pb, "FVER");
59 put_be32(pb, 4);
60 put_be32(pb, 0xA2805140);
63 /* Common chunk */
64 put_tag(pb, "COMM");
65 put_be32(pb, aifc ? 24 : 18); /* size */
66 put_be16(pb, enc->channels); /* Number of channels */
68 aiff->frames = url_ftell(pb);
69 put_be32(pb, 0); /* Number of frames */
71 if (!enc->bits_per_coded_sample)
72 enc->bits_per_coded_sample = av_get_bits_per_sample(enc->codec_id);
73 if (!enc->bits_per_coded_sample) {
74 av_log(s, AV_LOG_ERROR, "could not compute bits per sample\n");
75 return -1;
77 if (!enc->block_align)
78 enc->block_align = (enc->bits_per_coded_sample * enc->channels) >> 3;
80 put_be16(pb, enc->bits_per_coded_sample); /* Sample size */
82 sample_rate = av_dbl2ext((double)enc->sample_rate);
83 put_buffer(pb, (uint8_t*)&sample_rate, sizeof(sample_rate));
85 if (aifc) {
86 put_le32(pb, enc->codec_tag);
87 put_be16(pb, 0);
90 /* Sound data chunk */
91 put_tag(pb, "SSND");
92 aiff->ssnd = url_ftell(pb); /* Sound chunk size */
93 put_be32(pb, 0); /* Sound samples data size */
94 put_be32(pb, 0); /* Data offset */
95 put_be32(pb, 0); /* Block-size (block align) */
97 av_set_pts_info(s->streams[0], 64, 1, s->streams[0]->codec->sample_rate);
99 /* Data is starting here */
100 put_flush_packet(pb);
102 return 0;
105 static int aiff_write_packet(AVFormatContext *s, AVPacket *pkt)
107 ByteIOContext *pb = s->pb;
108 put_buffer(pb, pkt->data, pkt->size);
109 return 0;
112 static int aiff_write_trailer(AVFormatContext *s)
114 ByteIOContext *pb = s->pb;
115 AIFFOutputContext *aiff = s->priv_data;
116 AVCodecContext *enc = s->streams[0]->codec;
118 /* Chunks sizes must be even */
119 int64_t file_size, end_size;
120 end_size = file_size = url_ftell(pb);
121 if (file_size & 1) {
122 put_byte(pb, 0);
123 end_size++;
126 if (!url_is_streamed(s->pb)) {
127 /* File length */
128 url_fseek(pb, aiff->form, SEEK_SET);
129 put_be32(pb, file_size - aiff->form - 4);
131 /* Number of sample frames */
132 url_fseek(pb, aiff->frames, SEEK_SET);
133 put_be32(pb, (file_size-aiff->ssnd-12)/enc->block_align);
135 /* Sound Data chunk size */
136 url_fseek(pb, aiff->ssnd, SEEK_SET);
137 put_be32(pb, file_size - aiff->ssnd - 4);
139 /* return to the end */
140 url_fseek(pb, end_size, SEEK_SET);
142 put_flush_packet(pb);
145 return 0;
148 AVOutputFormat aiff_muxer = {
149 "aiff",
150 NULL_IF_CONFIG_SMALL("Audio IFF"),
151 "audio/aiff",
152 "aif,aiff,afc,aifc",
153 sizeof(AIFFOutputContext),
154 CODEC_ID_PCM_S16BE,
155 CODEC_ID_NONE,
156 aiff_write_header,
157 aiff_write_packet,
158 aiff_write_trailer,
159 .codec_tag= (const AVCodecTag* const []){ff_codec_aiff_tags, 0},