FFmpeg
Loading...
Searching...
No Matches
aiffenc.c
Go to the documentation of this file.
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 <stdint.h>
23
24#include "libavutil/intfloat.h"
25#include "libavutil/opt.h"
26#include "packet_internal.h"
27#include "avformat.h"
28#include "internal.h"
29#include "aiff.h"
30#include "avio_internal.h"
31#include "isom.h"
32#include "id3v2.h"
33#include "mux.h"
34
45
47{
48 int ret;
49 uint64_t pos, end, size;
50 ID3v2EncContext id3v2 = { 0 };
51 AVIOContext *pb = s->pb;
52 PacketListEntry *list_entry = aiff->pict_list.head;
53
54 if (!s->metadata && !s->nb_chapters && !list_entry)
55 return 0;
56
57 avio_wb32(pb, MKBETAG('I', 'D', '3', ' '));
58 avio_wb32(pb, 0);
59 pos = avio_tell(pb);
60
63 while (list_entry) {
64 if ((ret = ff_id3v2_write_apic(s, &id3v2, &list_entry->pkt)) < 0)
65 return ret;
66 list_entry = list_entry->next;
67 }
68 ff_id3v2_finish(&id3v2, pb, s->metadata_header_padding);
69
70 end = avio_tell(pb);
71 size = end - pos;
72
73 /* Update chunk size */
74 avio_seek(pb, pos - 4, SEEK_SET);
75 avio_wb32(pb, size);
76 avio_seek(pb, end, SEEK_SET);
77
78 if (size & 1)
79 avio_w8(pb, 0);
80
81 return 0;
82}
83
84static void put_meta(AVFormatContext *s, const char *key, uint32_t id)
85{
87 AVIOContext *pb = s->pb;
88
89 if (tag = av_dict_get(s->metadata, key, NULL, 0)) {
90 size_t size = strlen(tag->value);
91
92 // AIFF tags are zero-padded to an even length.
93 // So simply copy the terminating \0 if the length is odd.
94 size = FFALIGN(size, 2);
95
96 avio_wb32(pb, id);
97 avio_wb32(pb, size);
98 avio_write(pb, tag->value, size);
99 }
100}
101
103{
104 AIFFOutputContext *aiff = s->priv_data;
105 AVIOContext *pb = s->pb;
107 uint64_t sample_rate;
108 int i, aifc = 0;
109
110 aiff->audio_stream_idx = -1;
111 for (i = 0; i < s->nb_streams; i++) {
112 AVStream *st = s->streams[i];
113 if (aiff->audio_stream_idx < 0 && st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
114 aiff->audio_stream_idx = i;
115 } else if (st->codecpar->codec_type != AVMEDIA_TYPE_VIDEO) {
116 av_log(s, AV_LOG_ERROR, "AIFF allows only one audio stream and a picture.\n");
117 return AVERROR(EINVAL);
118 }
119 }
120 if (aiff->audio_stream_idx < 0) {
121 av_log(s, AV_LOG_ERROR, "No audio stream present.\n");
122 return AVERROR(EINVAL);
123 }
124
125 par = s->streams[aiff->audio_stream_idx]->codecpar;
126
127 /* First verify if format is ok */
128 if (!par->codec_tag)
129 return AVERROR(EINVAL);
130 if (par->codec_tag != MKTAG('N','O','N','E'))
131 aifc = 1;
132
133 /* FORM AIFF header */
134 ffio_wfourcc(pb, "FORM");
135 aiff->form = avio_tell(pb);
136 avio_wb32(pb, 0); /* file length */
137 ffio_wfourcc(pb, aifc ? "AIFC" : "AIFF");
138
139 if (aifc) { // compressed audio
140 if (!par->block_align) {
141 av_log(s, AV_LOG_ERROR, "block align not set\n");
142 return AVERROR(EINVAL);
143 }
144 /* Version chunk */
145 ffio_wfourcc(pb, "FVER");
146 avio_wb32(pb, 4);
147 avio_wb32(pb, 0xA2805140);
148 }
149
150 put_meta(s, "title", MKBETAG('N', 'A', 'M', 'E'));
151 put_meta(s, "author", MKBETAG('A', 'U', 'T', 'H'));
152 put_meta(s, "copyright", MKBETAG('(', 'c', ')', ' '));
153 put_meta(s, "comment", MKBETAG('A', 'N', 'N', 'O'));
154
155 /* Common chunk */
156 ffio_wfourcc(pb, "COMM");
157 avio_wb32(pb, aifc ? 24 : 18); /* size */
158 avio_wb16(pb, par->ch_layout.nb_channels); /* Number of channels */
159
160 aiff->frames = avio_tell(pb);
161 avio_wb32(pb, 0); /* Number of frames */
162
163 if (!par->bits_per_coded_sample)
165 if (!par->bits_per_coded_sample) {
166 av_log(s, AV_LOG_ERROR, "could not compute bits per sample\n");
167 return AVERROR(EINVAL);
168 }
169 if (!par->block_align)
170 par->block_align = (par->bits_per_coded_sample * par->ch_layout.nb_channels) >> 3;
171
172 avio_wb16(pb, par->bits_per_coded_sample); /* Sample size */
173
174 sample_rate = av_double2int(par->sample_rate);
175 avio_wb16(pb, (sample_rate >> 52) + (16383 - 1023));
176 avio_wb64(pb, UINT64_C(1) << 63 | sample_rate << 11);
177
178 if (aifc) {
179 avio_wl32(pb, par->codec_tag);
180 avio_wb16(pb, 0);
181 }
182
183 if ( (par->codec_tag == MKTAG('Q','D','M','2')
184 || par->codec_tag == MKTAG('Q','c','l','p')) && par->extradata_size) {
185 ffio_wfourcc(pb, "wave");
186 avio_wb32(pb, par->extradata_size);
187 avio_write(pb, par->extradata, par->extradata_size);
188 }
189
190 /* CHAN chunk; a decoder may use the channel count when parsing this chunk,
191 * so let's write it after the COMM chunk which indicates said channel count. */
193 ffio_wfourcc(pb, "CHAN");
194 avio_wb32(pb, 12);
196 }
197
198 /* Sound data chunk */
199 ffio_wfourcc(pb, "SSND");
200 aiff->ssnd = avio_tell(pb); /* Sound chunk size */
201 avio_wb32(pb, 0); /* Sound samples data size */
202 avio_wb32(pb, 0); /* Data offset */
203 avio_wb32(pb, 0); /* Block-size (block align) */
204
205 avpriv_set_pts_info(s->streams[aiff->audio_stream_idx], 64, 1,
206 s->streams[aiff->audio_stream_idx]->codecpar->sample_rate);
207
208 return 0;
209}
210
212{
213 AIFFOutputContext *aiff = s->priv_data;
214 AVIOContext *pb = s->pb;
215 if (pkt->stream_index == aiff->audio_stream_idx)
216 avio_write(pb, pkt->data, pkt->size);
217 else {
218 /* warn only once for each stream */
219 if (s->streams[pkt->stream_index]->nb_frames == 1) {
220 av_log(s, AV_LOG_WARNING, "Got more than one picture in stream %d,"
221 " ignoring.\n", pkt->stream_index);
222 }
223 if (s->streams[pkt->stream_index]->nb_frames >= 1)
224 return 0;
225
226 return ff_packet_list_put(&aiff->pict_list, pkt, NULL, 0);
227 }
228
229 return 0;
230}
231
233{
234 int ret = 0;
235 AVIOContext *pb = s->pb;
236 AIFFOutputContext *aiff = s->priv_data;
237 AVCodecParameters *par = s->streams[aiff->audio_stream_idx]->codecpar;
238
239 /* Chunks sizes must be even */
240 int64_t file_size, data_size;
241 data_size = avio_tell(pb);
242 if (data_size & 1)
243 avio_w8(pb, 0);
244
245 if (s->pb->seekable & AVIO_SEEKABLE_NORMAL) {
246 /* Write ID3 tags */
247 if (aiff->write_id3v2)
248 if ((ret = put_id3v2_tags(s, aiff)) < 0)
249 return ret;
250
251 /* File length */
252 file_size = avio_tell(pb);
253 avio_seek(pb, aiff->form, SEEK_SET);
254 avio_wb32(pb, file_size - aiff->form - 4);
255
256 /* Number of sample frames */
257 avio_seek(pb, aiff->frames, SEEK_SET);
258 avio_wb32(pb, (data_size - aiff->ssnd - 12) / par->block_align);
259
260 /* Sound Data chunk size */
261 avio_seek(pb, aiff->ssnd, SEEK_SET);
262 avio_wb32(pb, data_size - aiff->ssnd - 4);
263 }
264
265 return ret;
266}
267
269{
270 AIFFOutputContext *aiff = s->priv_data;
271
273}
274
275#define OFFSET(x) offsetof(AIFFOutputContext, x)
276#define ENC AV_OPT_FLAG_ENCODING_PARAM
277static const AVOption options[] = {
278 { "write_id3v2", "Enable ID3 tags writing.",
279 OFFSET(write_id3v2), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, ENC },
280 { "id3v2_version", "Select ID3v2 version to write. Currently 3 and 4 are supported.",
281 OFFSET(id3v2_version), AV_OPT_TYPE_INT, {.i64 = 4}, 3, 4, ENC },
282 { NULL },
283};
284
285static const AVClass aiff_muxer_class = {
286 .class_name = "AIFF muxer",
287 .item_name = av_default_item_name,
288 .option = options,
289 .version = LIBAVUTIL_VERSION_INT,
290};
291
293 .p.name = "aiff",
294 .p.long_name = NULL_IF_CONFIG_SMALL("Audio IFF"),
295 .p.mime_type = "audio/aiff",
296 .p.extensions = "aif,aiff,afc,aifc",
297 .priv_data_size = sizeof(AIFFOutputContext),
298 .p.audio_codec = AV_CODEC_ID_PCM_S16BE,
299 .p.video_codec = AV_CODEC_ID_PNG,
300 .write_header = aiff_write_header,
301 .write_packet = aiff_write_packet,
302 .write_trailer = aiff_write_trailer,
303 .deinit = aiff_deinit,
304 .p.codec_tag = ff_aiff_codec_tags_list,
305 .p.priv_class = &aiff_muxer_class,
306};
const AVCodecTag *const ff_aiff_codec_tags_list[]
Definition aiff.c:57
common header for AIFF muxer and demuxer
static int aiff_write_header(AVFormatContext *s)
Definition aiffenc.c:102
static const AVClass aiff_muxer_class
Definition aiffenc.c:285
static int aiff_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition aiffenc.c:211
static int put_id3v2_tags(AVFormatContext *s, AIFFOutputContext *aiff)
Definition aiffenc.c:46
static int aiff_write_trailer(AVFormatContext *s)
Definition aiffenc.c:232
static void aiff_deinit(AVFormatContext *s)
Definition aiffenc.c:268
const FFOutputFormat ff_aiff_muxer
Definition aiffenc.c:292
static void put_meta(AVFormatContext *s, const char *key, uint32_t id)
Definition aiffenc.c:84
#define OFFSET(x)
Definition aiffenc.c:275
void avpriv_set_pts_info(AVStream *st, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition avformat.c:834
Main libavformat public API header.
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition aviobuf.c:236
#define AVIO_SEEKABLE_NORMAL
Seeking works like for a local file.
Definition avio.h:41
void avio_wl32(AVIOContext *s, unsigned int val)
Definition aviobuf.c:360
void avio_w8(AVIOContext *s, int b)
Definition aviobuf.c:184
void avio_wb32(AVIOContext *s, unsigned int val)
Definition aviobuf.c:368
void avio_wb16(AVIOContext *s, unsigned int val)
Definition aviobuf.c:446
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition avio.h:494
void avio_wb64(AVIOContext *s, uint64_t val)
Definition aviobuf.c:434
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition aviobuf.c:206
static av_always_inline void ffio_wfourcc(AVIOContext *pb, const uint8_t *s)
#define ENC
Definition caca.c:198
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
static AVPacket * pkt
const char * key
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition opt.h:258
@ AV_OPT_TYPE_BOOL
Underlying C type is int.
Definition opt.h:326
int av_get_bits_per_sample(enum AVCodecID codec_id)
Return codec bits per sample.
Definition utils.c:556
@ AV_CODEC_ID_PNG
Definition codec_id.h:111
@ AV_CODEC_ID_PCM_S16BE
Definition codec_id.h:331
@ AV_CHANNEL_ORDER_NATIVE
The native channel order, i.e.
AVDictionaryEntry * av_dict_get(const AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags)
Get a dictionary entry with matching key.
Definition dict.c:60
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition log.h:216
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
const char * av_default_item_name(void *ptr)
Return the context name.
Definition log.c:241
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
#define LIBAVUTIL_VERSION_INT
Definition version.h:85
void ff_id3v2_start(ID3v2EncContext *id3, AVIOContext *pb, int id3v2_version, const char *magic)
Initialize an ID3v2 tag.
Definition id3v2enc.c:349
void ff_id3v2_finish(ID3v2EncContext *id3, AVIOContext *pb, int padding_bytes)
Finalize an opened ID3v2 tag.
Definition id3v2enc.c:552
int ff_id3v2_write_metadata(AVFormatContext *s, ID3v2EncContext *id3)
Convert and write all global metadata from s into an ID3v2 tag.
Definition id3v2enc.c:474
int ff_id3v2_write_apic(AVFormatContext *s, ID3v2EncContext *id3, AVPacket *pkt)
Write an attached picture from pkt into an ID3v2 tag.
Definition id3v2enc.c:495
#define ID3v2_DEFAULT_MAGIC
Default magic bytes for ID3v2 header: "ID3".
Definition id3v2.h:35
static av_always_inline uint64_t av_double2int(double f)
Reinterpret a double as a 64-bit integer.
Definition intfloat.h:70
void ff_mov_write_chan(AVIOContext *pb, int64_t channel_layout)
Definition isom.c:430
void ff_packet_list_free(PacketList *list)
Wipe the list and unref all the packets in it.
int ff_packet_list_put(PacketList *list, AVPacket *pkt, int(*copy)(AVPacket *dst, const AVPacket *src), int flags)
Append an AVPacket to the list.
Definition packet_list.c:40
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
#define MKTAG(a, b, c, d)
Definition macros.h:55
#define MKBETAG(a, b, c, d)
Definition macros.h:56
#define FFALIGN(x, a)
Definition macros.h:78
uint32_t tag
Definition movenc.c:2073
AVOptions.
unsigned int pos
Definition spdifenc.c:431
int64_t frames
Definition aiffenc.c:38
PacketList pict_list
Definition aiffenc.c:41
enum AVChannelOrder order
Channel order used in this layout.
uint64_t mask
This member must be used for AV_CHANNEL_ORDER_NATIVE, and may be used for AV_CHANNEL_ORDER_AMBISONIC ...
union AVChannelLayout::@162063043056170047076125117143030261346263330336 u
Details about which channels are present in this layout.
int nb_channels
Number of channels in this layout.
Describe the class of an AVClass context structure.
Definition log.h:76
This struct describes the properties of an encoded stream.
Definition codec_par.h:49
int extradata_size
Size of the extradata content in bytes.
Definition codec_par.h:75
int bits_per_coded_sample
The number of bits per sample in the codedwords.
Definition codec_par.h:113
AVChannelLayout ch_layout
The channel layout and number of channels.
Definition codec_par.h:207
enum AVMediaType codec_type
General type of the encoded data.
Definition codec_par.h:53
int block_align
The number of bytes per coded audio frame, required by some formats.
Definition codec_par.h:221
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition codec_par.h:61
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition codec_par.h:71
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition codec_par.h:57
int sample_rate
The number of audio samples per second.
Definition codec_par.h:213
Format I/O context.
Definition avformat.h:1333
Bytestream IO Context.
Definition avio.h:160
AVOption.
Definition opt.h:428
This structure stores compressed data.
Definition packet.h:580
Stream structure.
Definition avformat.h:766
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition avformat.h:789
struct PacketListEntry * next
PacketListEntry * head
#define av_log(a,...)
int size