FFmpeg
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"
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 
35 typedef struct AIFFOutputContext {
36  const AVClass *class;
37  int64_t form;
38  int64_t frames;
39  int64_t ssnd;
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_wl32(pb, MKTAG('I', 'D', '3', ' '));
58  avio_wb32(pb, 0);
59  pos = avio_tell(pb);
60 
62  ff_id3v2_write_metadata(s, &id3v2);
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 
84 static 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  int size = strlen(tag->value);
91 
92  avio_wl32(pb, id);
93  avio_wb32(pb, FFALIGN(size, 2));
94  avio_write(pb, tag->value, size);
95  if (size & 1)
96  avio_w8(pb, 0);
97  }
98 }
99 
101 {
102  AIFFOutputContext *aiff = s->priv_data;
103  AVIOContext *pb = s->pb;
104  AVCodecParameters *par;
105  uint64_t sample_rate;
106  int i, aifc = 0;
107 
108  aiff->audio_stream_idx = -1;
109  for (i = 0; i < s->nb_streams; i++) {
110  AVStream *st = s->streams[i];
111  if (aiff->audio_stream_idx < 0 && st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
112  aiff->audio_stream_idx = i;
113  } else if (st->codecpar->codec_type != AVMEDIA_TYPE_VIDEO) {
114  av_log(s, AV_LOG_ERROR, "AIFF allows only one audio stream and a picture.\n");
115  return AVERROR(EINVAL);
116  }
117  }
118  if (aiff->audio_stream_idx < 0) {
119  av_log(s, AV_LOG_ERROR, "No audio stream present.\n");
120  return AVERROR(EINVAL);
121  }
122 
123  par = s->streams[aiff->audio_stream_idx]->codecpar;
124 
125  /* First verify if format is ok */
126  if (!par->codec_tag)
127  return AVERROR(EINVAL);
128  if (par->codec_tag != MKTAG('N','O','N','E'))
129  aifc = 1;
130 
131  /* FORM AIFF header */
132  ffio_wfourcc(pb, "FORM");
133  aiff->form = avio_tell(pb);
134  avio_wb32(pb, 0); /* file length */
135  ffio_wfourcc(pb, aifc ? "AIFC" : "AIFF");
136 
137  if (aifc) { // compressed audio
138  if (!par->block_align) {
139  av_log(s, AV_LOG_ERROR, "block align not set\n");
140  return AVERROR(EINVAL);
141  }
142  /* Version chunk */
143  ffio_wfourcc(pb, "FVER");
144  avio_wb32(pb, 4);
145  avio_wb32(pb, 0xA2805140);
146  }
147 
148  if (par->ch_layout.order == AV_CHANNEL_ORDER_NATIVE && par->ch_layout.nb_channels > 2) {
149  ffio_wfourcc(pb, "CHAN");
150  avio_wb32(pb, 12);
151  ff_mov_write_chan(pb, par->ch_layout.u.mask);
152  }
153 
154  put_meta(s, "title", MKTAG('N', 'A', 'M', 'E'));
155  put_meta(s, "author", MKTAG('A', 'U', 'T', 'H'));
156  put_meta(s, "copyright", MKTAG('(', 'c', ')', ' '));
157  put_meta(s, "comment", MKTAG('A', 'N', 'N', 'O'));
158 
159  /* Common chunk */
160  ffio_wfourcc(pb, "COMM");
161  avio_wb32(pb, aifc ? 24 : 18); /* size */
162  avio_wb16(pb, par->ch_layout.nb_channels); /* Number of channels */
163 
164  aiff->frames = avio_tell(pb);
165  avio_wb32(pb, 0); /* Number of frames */
166 
167  if (!par->bits_per_coded_sample)
169  if (!par->bits_per_coded_sample) {
170  av_log(s, AV_LOG_ERROR, "could not compute bits per sample\n");
171  return AVERROR(EINVAL);
172  }
173  if (!par->block_align)
174  par->block_align = (par->bits_per_coded_sample * par->ch_layout.nb_channels) >> 3;
175 
176  avio_wb16(pb, par->bits_per_coded_sample); /* Sample size */
177 
179  avio_wb16(pb, (sample_rate >> 52) + (16383 - 1023));
180  avio_wb64(pb, UINT64_C(1) << 63 | sample_rate << 11);
181 
182  if (aifc) {
183  avio_wl32(pb, par->codec_tag);
184  avio_wb16(pb, 0);
185  }
186 
187  if ( (par->codec_tag == MKTAG('Q','D','M','2')
188  || par->codec_tag == MKTAG('Q','c','l','p')) && par->extradata_size) {
189  ffio_wfourcc(pb, "wave");
190  avio_wb32(pb, par->extradata_size);
191  avio_write(pb, par->extradata, par->extradata_size);
192  }
193 
194  /* Sound data chunk */
195  ffio_wfourcc(pb, "SSND");
196  aiff->ssnd = avio_tell(pb); /* Sound chunk size */
197  avio_wb32(pb, 0); /* Sound samples data size */
198  avio_wb32(pb, 0); /* Data offset */
199  avio_wb32(pb, 0); /* Block-size (block align) */
200 
201  avpriv_set_pts_info(s->streams[aiff->audio_stream_idx], 64, 1,
202  s->streams[aiff->audio_stream_idx]->codecpar->sample_rate);
203 
204  return 0;
205 }
206 
208 {
209  AIFFOutputContext *aiff = s->priv_data;
210  AVIOContext *pb = s->pb;
211  if (pkt->stream_index == aiff->audio_stream_idx)
212  avio_write(pb, pkt->data, pkt->size);
213  else {
214  /* warn only once for each stream */
215  if (s->streams[pkt->stream_index]->nb_frames == 1) {
216  av_log(s, AV_LOG_WARNING, "Got more than one picture in stream %d,"
217  " ignoring.\n", pkt->stream_index);
218  }
219  if (s->streams[pkt->stream_index]->nb_frames >= 1)
220  return 0;
221 
222  return avpriv_packet_list_put(&aiff->pict_list, pkt, NULL, 0);
223  }
224 
225  return 0;
226 }
227 
229 {
230  int ret = 0;
231  AVIOContext *pb = s->pb;
232  AIFFOutputContext *aiff = s->priv_data;
233  AVCodecParameters *par = s->streams[aiff->audio_stream_idx]->codecpar;
234 
235  /* Chunks sizes must be even */
236  int64_t file_size, data_size;
237  data_size = avio_tell(pb);
238  if (data_size & 1)
239  avio_w8(pb, 0);
240 
241  if (s->pb->seekable & AVIO_SEEKABLE_NORMAL) {
242  /* Write ID3 tags */
243  if (aiff->write_id3v2)
244  if ((ret = put_id3v2_tags(s, aiff)) < 0)
245  return ret;
246 
247  /* File length */
248  file_size = avio_tell(pb);
249  avio_seek(pb, aiff->form, SEEK_SET);
250  avio_wb32(pb, file_size - aiff->form - 4);
251 
252  /* Number of sample frames */
253  avio_seek(pb, aiff->frames, SEEK_SET);
254  avio_wb32(pb, (data_size - aiff->ssnd - 12) / par->block_align);
255 
256  /* Sound Data chunk size */
257  avio_seek(pb, aiff->ssnd, SEEK_SET);
258  avio_wb32(pb, data_size - aiff->ssnd - 4);
259  }
260 
261  return ret;
262 }
263 
265 {
266  AIFFOutputContext *aiff = s->priv_data;
267 
269 }
270 
271 #define OFFSET(x) offsetof(AIFFOutputContext, x)
272 #define ENC AV_OPT_FLAG_ENCODING_PARAM
273 static const AVOption options[] = {
274  { "write_id3v2", "Enable ID3 tags writing.",
275  OFFSET(write_id3v2), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, ENC },
276  { "id3v2_version", "Select ID3v2 version to write. Currently 3 and 4 are supported.",
277  OFFSET(id3v2_version), AV_OPT_TYPE_INT, {.i64 = 4}, 3, 4, ENC },
278  { NULL },
279 };
280 
281 static const AVClass aiff_muxer_class = {
282  .class_name = "AIFF muxer",
283  .item_name = av_default_item_name,
284  .option = options,
285  .version = LIBAVUTIL_VERSION_INT,
286 };
287 
289  .p.name = "aiff",
290  .p.long_name = NULL_IF_CONFIG_SMALL("Audio IFF"),
291  .p.mime_type = "audio/aiff",
292  .p.extensions = "aif,aiff,afc,aifc",
293  .priv_data_size = sizeof(AIFFOutputContext),
294  .p.audio_codec = AV_CODEC_ID_PCM_S16BE,
295  .p.video_codec = AV_CODEC_ID_PNG,
296  .write_header = aiff_write_header,
297  .write_packet = aiff_write_packet,
298  .write_trailer = aiff_write_trailer,
299  .deinit = aiff_deinit,
300  .p.codec_tag = ff_aiff_codec_tags_list,
301  .p.priv_class = &aiff_muxer_class,
302 };
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:186
avpriv_packet_list_put
int avpriv_packet_list_put(PacketList *packet_buffer, AVPacket *pkt, int(*copy)(AVPacket *dst, const AVPacket *src), int flags)
Append an AVPacket to the list.
Definition: avpacket.c:536
AVCodecParameters::extradata
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition: codec_par.h:76
PacketList::head
PacketListEntry * head
Definition: packet_internal.h:32
ID3v2EncContext
Definition: id3v2.h:51
AIFFOutputContext::frames
int64_t frames
Definition: aiffenc.c:38
AVOutputFormat::name
const char * name
Definition: avformat.h:508
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
opt.h
AVCodecParameters::codec_type
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:58
ffio_wfourcc
static av_always_inline void ffio_wfourcc(AVIOContext *pb, const uint8_t *s)
Definition: avio_internal.h:116
AVCodecParameters
This struct describes the properties of an encoded stream.
Definition: codec_par.h:54
id3v2.h
AIFFOutputContext::id3v2_version
int id3v2_version
Definition: aiffenc.c:43
AVPacket::data
uint8_t * data
Definition: packet.h:374
AVOption
AVOption.
Definition: opt.h:251
PacketList
Definition: packet_internal.h:31
aiff_write_packet
static int aiff_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: aiffenc.c:207
AVCodecParameters::codec_tag
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition: codec_par.h:66
AIFFOutputContext::ssnd
int64_t ssnd
Definition: aiffenc.c:39
AVChannelLayout::order
enum AVChannelOrder order
Channel order used in this layout.
Definition: channel_layout.h:306
AVChannelLayout::mask
uint64_t mask
This member must be used for AV_CHANNEL_ORDER_NATIVE, and may be used for AV_CHANNEL_ORDER_AMBISONIC ...
Definition: channel_layout.h:333
AVChannelLayout::nb_channels
int nb_channels
Number of channels in this layout.
Definition: channel_layout.h:311
intfloat.h
ff_id3v2_start
void ff_id3v2_start(ID3v2EncContext *id3, AVIOContext *pb, int id3v2_version, const char *magic)
Initialize an ID3v2 tag.
Definition: id3v2enc.c:206
sample_rate
sample_rate
Definition: ffmpeg_filter.c:156
AIFFOutputContext::write_id3v2
int write_id3v2
Definition: aiffenc.c:42
FFOutputFormat::p
AVOutputFormat p
The public AVOutputFormat.
Definition: mux.h:34
avpriv_set_pts_info
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:771
AV_CODEC_ID_PCM_S16BE
@ AV_CODEC_ID_PCM_S16BE
Definition: codec_id.h:327
ENC
#define ENC
Definition: aiffenc.c:272
avio_tell
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:500
av_get_bits_per_sample
int av_get_bits_per_sample(enum AVCodecID codec_id)
Return codec bits per sample.
Definition: utils.c:583
pkt
AVPacket * pkt
Definition: movenc.c:59
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
av_dict_get
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
s
#define s(width, name)
Definition: cbs_vp9.c:256
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
options
static const AVOption options[]
Definition: aiffenc.c:273
ff_id3v2_write_metadata
int ff_id3v2_write_metadata(AVFormatContext *s, ID3v2EncContext *id3)
Convert and write all global metadata from s into an ID3v2 tag.
Definition: id3v2enc.c:331
key
const char * key
Definition: hwcontext_opencl.c:174
ff_aiff_muxer
const FFOutputFormat ff_aiff_muxer
Definition: aiffenc.c:288
AV_CODEC_ID_PNG
@ AV_CODEC_ID_PNG
Definition: codec_id.h:113
AVFormatContext
Format I/O context.
Definition: avformat.h:1104
internal.h
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:861
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
NULL
#define NULL
Definition: coverity.c:32
isom.h
aiff.h
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:237
AIFFOutputContext::form
int64_t form
Definition: aiffenc.c:37
FFOutputFormat
Definition: mux.h:30
avio_w8
void avio_w8(AVIOContext *s, int b)
Definition: aviobuf.c:200
AVCodecParameters::ch_layout
AVChannelLayout ch_layout
Audio only.
Definition: codec_par.h:213
PacketListEntry::next
struct PacketListEntry * next
Definition: packet_internal.h:27
AVCodecParameters::sample_rate
int sample_rate
Audio only.
Definition: codec_par.h:178
AVCodecParameters::extradata_size
int extradata_size
Size of the extradata content in bytes.
Definition: codec_par.h:80
ff_id3v2_write_apic
int ff_id3v2_write_apic(AVFormatContext *s, ID3v2EncContext *id3, AVPacket *pkt)
Write an attached picture from pkt into an ID3v2 tag.
Definition: id3v2enc.c:352
AVIOContext
Bytestream IO Context.
Definition: avio.h:166
AVPacket::size
int size
Definition: packet.h:375
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:115
avpriv_packet_list_free
void avpriv_packet_list_free(PacketList *pkt_buf)
Wipe the list and unref all the packets in it.
Definition: avpacket.c:589
aiff_write_trailer
static int aiff_write_trailer(AVFormatContext *s)
Definition: aiffenc.c:228
size
int size
Definition: twinvq_data.h:10344
ID3v2_DEFAULT_MAGIC
#define ID3v2_DEFAULT_MAGIC
Default magic bytes for ID3v2 header: "ID3".
Definition: id3v2.h:35
PacketListEntry::pkt
AVPacket pkt
Definition: packet_internal.h:28
avio_write
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:222
avio_wb32
void avio_wb32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:386
avio_wl32
void avio_wl32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:378
put_id3v2_tags
static int put_id3v2_tags(AVFormatContext *s, AIFFOutputContext *aiff)
Definition: aiffenc.c:46
AV_CHANNEL_ORDER_NATIVE
@ AV_CHANNEL_ORDER_NATIVE
The native channel order, i.e.
Definition: channel_layout.h:118
av_double2int
static av_always_inline uint64_t av_double2int(double f)
Reinterpret a double as a 64-bit integer.
Definition: intfloat.h:70
PacketListEntry
Definition: packet_internal.h:26
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
avio_internal.h
AVCodecParameters::block_align
int block_align
Audio only.
Definition: codec_par.h:185
OFFSET
#define OFFSET(x)
Definition: aiffenc.c:271
AIFFOutputContext
Definition: aiffenc.c:35
tag
uint32_t tag
Definition: movenc.c:1641
ret
ret
Definition: filter_design.txt:187
AVStream
Stream structure.
Definition: avformat.h:838
avio_seek
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:252
AVClass::class_name
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:71
aiff_write_header
static int aiff_write_header(AVFormatContext *s)
Definition: aiffenc.c:100
pos
unsigned int pos
Definition: spdifenc.c:413
avformat.h
AVIO_SEEKABLE_NORMAL
#define AVIO_SEEKABLE_NORMAL
Seeking works like for a local file.
Definition: avio.h:41
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Definition: opt.h:225
AVPacket::stream_index
int stream_index
Definition: packet.h:376
aiff_muxer_class
static const AVClass aiff_muxer_class
Definition: aiffenc.c:281
ff_id3v2_finish
void ff_id3v2_finish(ID3v2EncContext *id3, AVIOContext *pb, int padding_bytes)
Finalize an opened ID3v2 tag.
Definition: id3v2enc.c:421
aiff_deinit
static void aiff_deinit(AVFormatContext *s)
Definition: aiffenc.c:264
put_meta
static void put_meta(AVFormatContext *s, const char *key, uint32_t id)
Definition: aiffenc.c:84
avio_wb64
void avio_wb64(AVIOContext *s, uint64_t val)
Definition: aviobuf.c:452
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
AVCodecParameters::bits_per_coded_sample
int bits_per_coded_sample
The number of bits per sample in the codedwords.
Definition: codec_par.h:104
packet_internal.h
AVDictionaryEntry
Definition: dict.h:89
FFALIGN
#define FFALIGN(x, a)
Definition: macros.h:78
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:62
ff_aiff_codec_tags_list
const AVCodecTag *const ff_aiff_codec_tags_list[]
Definition: aiff.c:55
AVPacket
This structure stores compressed data.
Definition: packet.h:351
AV_OPT_TYPE_BOOL
@ AV_OPT_TYPE_BOOL
Definition: opt.h:244
avio_wb16
void avio_wb16(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:464
AVChannelLayout::u
union AVChannelLayout::@314 u
Details about which channels are present in this layout.
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AIFFOutputContext::pict_list
PacketList pict_list
Definition: aiffenc.c:41
MKTAG
#define MKTAG(a, b, c, d)
Definition: macros.h:55
ff_mov_write_chan
void ff_mov_write_chan(AVIOContext *pb, int64_t channel_layout)
Definition: isom.c:412
AIFFOutputContext::audio_stream_idx
int audio_stream_idx
Definition: aiffenc.c:40
mux.h