]> git.sesse.net Git - ffmpeg/blob - libavformat/aiffenc.c
Merge remote-tracking branch 'qatar/master'
[ffmpeg] / libavformat / aiffenc.c
1 /*
2  * AIFF/AIFF-C muxer
3  * Copyright (c) 2006  Patrick Guimond
4  *
5  * This file is part of FFmpeg.
6  *
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.
11  *
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.
16  *
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
20  */
21
22 #include "avformat.h"
23 #include "aiff.h"
24 #include "avio_internal.h"
25 #include "isom.h"
26
27 typedef struct {
28     int64_t form;
29     int64_t frames;
30     int64_t ssnd;
31 } AIFFOutputContext;
32
33 static int aiff_write_header(AVFormatContext *s)
34 {
35     AIFFOutputContext *aiff = s->priv_data;
36     AVIOContext *pb = s->pb;
37     AVCodecContext *enc = s->streams[0]->codec;
38     AVExtFloat sample_rate;
39     int aifc = 0;
40
41     /* First verify if format is ok */
42     if (!enc->codec_tag)
43         return -1;
44     if (enc->codec_tag != MKTAG('N','O','N','E'))
45         aifc = 1;
46
47     /* FORM AIFF header */
48     ffio_wfourcc(pb, "FORM");
49     aiff->form = avio_tell(pb);
50     avio_wb32(pb, 0);                    /* file length */
51     ffio_wfourcc(pb, aifc ? "AIFC" : "AIFF");
52
53     if (aifc) { // compressed audio
54         enc->bits_per_coded_sample = 16;
55         if (!enc->block_align) {
56             av_log(s, AV_LOG_ERROR, "block align not set\n");
57             return -1;
58         }
59         /* Version chunk */
60         ffio_wfourcc(pb, "FVER");
61         avio_wb32(pb, 4);
62         avio_wb32(pb, 0xA2805140);
63     }
64
65     if (enc->channels > 2 && enc->channel_layout) {
66         ffio_wfourcc(pb, "CHAN");
67         avio_wb32(pb, 12);
68         ff_mov_write_chan(pb, enc->channel_layout);
69     }
70
71     /* Common chunk */
72     ffio_wfourcc(pb, "COMM");
73     avio_wb32(pb, aifc ? 24 : 18); /* size */
74     avio_wb16(pb, enc->channels);  /* Number of channels */
75
76     aiff->frames = avio_tell(pb);
77     avio_wb32(pb, 0);              /* Number of frames */
78
79     if (!enc->bits_per_coded_sample)
80         enc->bits_per_coded_sample = av_get_bits_per_sample(enc->codec_id);
81     if (!enc->bits_per_coded_sample) {
82         av_log(s, AV_LOG_ERROR, "could not compute bits per sample\n");
83         return -1;
84     }
85     if (!enc->block_align)
86         enc->block_align = (enc->bits_per_coded_sample * enc->channels) >> 3;
87
88     avio_wb16(pb, enc->bits_per_coded_sample); /* Sample size */
89
90     sample_rate = av_dbl2ext((double)enc->sample_rate);
91     avio_write(pb, (uint8_t*)&sample_rate, sizeof(sample_rate));
92
93     if (aifc) {
94         avio_wl32(pb, enc->codec_tag);
95         avio_wb16(pb, 0);
96     }
97
98     /* Sound data chunk */
99     ffio_wfourcc(pb, "SSND");
100     aiff->ssnd = avio_tell(pb);         /* Sound chunk size */
101     avio_wb32(pb, 0);                    /* Sound samples data size */
102     avio_wb32(pb, 0);                    /* Data offset */
103     avio_wb32(pb, 0);                    /* Block-size (block align) */
104
105     av_set_pts_info(s->streams[0], 64, 1, s->streams[0]->codec->sample_rate);
106
107     /* Data is starting here */
108     avio_flush(pb);
109
110     return 0;
111 }
112
113 static int aiff_write_packet(AVFormatContext *s, AVPacket *pkt)
114 {
115     AVIOContext *pb = s->pb;
116     avio_write(pb, pkt->data, pkt->size);
117     return 0;
118 }
119
120 static int aiff_write_trailer(AVFormatContext *s)
121 {
122     AVIOContext *pb = s->pb;
123     AIFFOutputContext *aiff = s->priv_data;
124     AVCodecContext *enc = s->streams[0]->codec;
125
126     /* Chunks sizes must be even */
127     int64_t file_size, end_size;
128     end_size = file_size = avio_tell(pb);
129     if (file_size & 1) {
130         avio_w8(pb, 0);
131         end_size++;
132     }
133
134     if (s->pb->seekable) {
135         /* File length */
136         avio_seek(pb, aiff->form, SEEK_SET);
137         avio_wb32(pb, file_size - aiff->form - 4);
138
139         /* Number of sample frames */
140         avio_seek(pb, aiff->frames, SEEK_SET);
141         avio_wb32(pb, (file_size-aiff->ssnd-12)/enc->block_align);
142
143         /* Sound Data chunk size */
144         avio_seek(pb, aiff->ssnd, SEEK_SET);
145         avio_wb32(pb, file_size - aiff->ssnd - 4);
146
147         /* return to the end */
148         avio_seek(pb, end_size, SEEK_SET);
149
150         avio_flush(pb);
151     }
152
153     return 0;
154 }
155
156 AVOutputFormat ff_aiff_muxer = {
157     "aiff",
158     NULL_IF_CONFIG_SMALL("Audio IFF"),
159     "audio/aiff",
160     "aif,aiff,afc,aifc",
161     sizeof(AIFFOutputContext),
162     CODEC_ID_PCM_S16BE,
163     CODEC_ID_NONE,
164     aiff_write_header,
165     aiff_write_packet,
166     aiff_write_trailer,
167     .codec_tag= (const AVCodecTag* const []){ff_codec_aiff_tags, 0},
168 };