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