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