Libav
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 Libav.
6  *
7  * Libav 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  * Libav 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 Libav; 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 "avformat.h"
26 #include "internal.h"
27 #include "aiff.h"
28 #include "avio_internal.h"
29 
30 typedef struct {
31  int64_t form;
32  int64_t frames;
33  int64_t ssnd;
35 
37 {
38  AIFFOutputContext *aiff = s->priv_data;
39  AVIOContext *pb = s->pb;
40  AVCodecContext *enc = s->streams[0]->codec;
41  uint64_t sample_rate;
42  int aifc = 0;
43 
44  /* First verify if format is ok */
45  if (!enc->codec_tag)
46  return -1;
47  if (enc->codec_tag != MKTAG('N','O','N','E'))
48  aifc = 1;
49 
50  /* FORM AIFF header */
51  ffio_wfourcc(pb, "FORM");
52  aiff->form = avio_tell(pb);
53  avio_wb32(pb, 0); /* file length */
54  ffio_wfourcc(pb, aifc ? "AIFC" : "AIFF");
55 
56  if (aifc) { // compressed audio
57  enc->bits_per_coded_sample = 16;
58  if (!enc->block_align) {
59  av_log(s, AV_LOG_ERROR, "block align not set\n");
60  return -1;
61  }
62  /* Version chunk */
63  ffio_wfourcc(pb, "FVER");
64  avio_wb32(pb, 4);
65  avio_wb32(pb, 0xA2805140);
66  }
67 
68  /* Common chunk */
69  ffio_wfourcc(pb, "COMM");
70  avio_wb32(pb, aifc ? 24 : 18); /* size */
71  avio_wb16(pb, enc->channels); /* Number of channels */
72 
73  aiff->frames = avio_tell(pb);
74  avio_wb32(pb, 0); /* Number of frames */
75 
76  if (!enc->bits_per_coded_sample)
78  if (!enc->bits_per_coded_sample) {
79  av_log(s, AV_LOG_ERROR, "could not compute bits per sample\n");
80  return -1;
81  }
82  if (!enc->block_align)
83  enc->block_align = (enc->bits_per_coded_sample * enc->channels) >> 3;
84 
85  avio_wb16(pb, enc->bits_per_coded_sample); /* Sample size */
86 
87  sample_rate = av_double2int(enc->sample_rate);
88  avio_wb16(pb, (sample_rate >> 52) + (16383 - 1023));
89  avio_wb64(pb, UINT64_C(1) << 63 | sample_rate << 11);
90 
91  if (aifc) {
92  avio_wl32(pb, enc->codec_tag);
93  avio_wb16(pb, 0);
94  }
95 
96  /* Sound data chunk */
97  ffio_wfourcc(pb, "SSND");
98  aiff->ssnd = avio_tell(pb); /* Sound chunk size */
99  avio_wb32(pb, 0); /* Sound samples data size */
100  avio_wb32(pb, 0); /* Data offset */
101  avio_wb32(pb, 0); /* Block-size (block align) */
102 
103  avpriv_set_pts_info(s->streams[0], 64, 1, s->streams[0]->codec->sample_rate);
104 
105  /* Data is starting here */
106  avio_flush(pb);
107 
108  return 0;
109 }
110 
112 {
113  AVIOContext *pb = s->pb;
114  avio_write(pb, pkt->data, pkt->size);
115  return 0;
116 }
117 
119 {
120  AVIOContext *pb = s->pb;
121  AIFFOutputContext *aiff = s->priv_data;
122  AVCodecContext *enc = s->streams[0]->codec;
123 
124  /* Chunks sizes must be even */
125  int64_t file_size, end_size;
126  end_size = file_size = avio_tell(pb);
127  if (file_size & 1) {
128  avio_w8(pb, 0);
129  end_size++;
130  }
131 
132  if (s->pb->seekable) {
133  /* File length */
134  avio_seek(pb, aiff->form, SEEK_SET);
135  avio_wb32(pb, file_size - aiff->form - 4);
136 
137  /* Number of sample frames */
138  avio_seek(pb, aiff->frames, SEEK_SET);
139  avio_wb32(pb, (file_size-aiff->ssnd-12)/enc->block_align);
140 
141  /* Sound Data chunk size */
142  avio_seek(pb, aiff->ssnd, SEEK_SET);
143  avio_wb32(pb, file_size - aiff->ssnd - 4);
144 
145  /* return to the end */
146  avio_seek(pb, end_size, SEEK_SET);
147 
148  avio_flush(pb);
149  }
150 
151  return 0;
152 }
153 
155  .name = "aiff",
156  .long_name = NULL_IF_CONFIG_SMALL("Audio IFF"),
157  .mime_type = "audio/aiff",
158  .extensions = "aif,aiff,afc,aifc",
159  .priv_data_size = sizeof(AIFFOutputContext),
160  .audio_codec = AV_CODEC_ID_PCM_S16BE,
161  .video_codec = AV_CODEC_ID_NONE,
165  .codec_tag = (const AVCodecTag* const []){ ff_codec_aiff_tags, 0 },
166 };
void avio_wb64(AVIOContext *s, uint64_t val)
Definition: aviobuf.c:330
Bytestream IO Context.
Definition: avio.h:68
void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition: utils.c:2829
static int write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: assenc.c:58
static int aiff_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: aiffenc.c:111
int size
Definition: avcodec.h:974
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:186
static av_always_inline uint64_t av_double2int(double f)
Reinterpret a double as a 64-bit integer.
Definition: intfloat.h:70
int block_align
number of bytes per packet if constant and known or 0 Used by some WAV based audio codecs...
Definition: avcodec.h:1828
Format I/O context.
Definition: avformat.h:922
void avio_wl32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:260
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:990
uint8_t * data
Definition: avcodec.h:973
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:219
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition: avcodec.h:2507
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:165
static av_always_inline void ffio_wfourcc(AVIOContext *pb, const uint8_t *s)
Definition: avio_internal.h:67
static int write_trailer(AVFormatContext *s)
Definition: assenc.c:64
int64_t frames
Definition: aiffenc.c:32
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:123
int av_get_bits_per_sample(enum AVCodecID codec_id)
Return codec bits per sample.
Definition: utils.c:2043
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:150
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:169
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:718
int seekable
A combination of AVIO_SEEKABLE_ flags or 0 when the stream is not seekable.
Definition: avio.h:117
int void avio_flush(AVIOContext *s)
Definition: aviobuf.c:180
const char * name
Definition: avformat.h:446
static int aiff_write_trailer(AVFormatContext *s)
Definition: aiffenc.c:118
int64_t ssnd
Definition: aiffenc.c:33
enum AVCodecID codec_id
Definition: avcodec.h:1067
int sample_rate
samples per second
Definition: avcodec.h:1791
AVIOContext * pb
I/O context.
Definition: avformat.h:964
void avio_w8(AVIOContext *s, int b)
Definition: aviobuf.c:144
main external API structure.
Definition: avcodec.h:1050
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
Definition: avcodec.h:1082
static int aiff_write_header(AVFormatContext *s)
Definition: aiffenc.c:36
void avio_wb16(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:342
Main libavformat public API header.
static const AVCodecTag ff_codec_aiff_tags[]
Definition: aiff.h:33
int channels
number of audio channels
Definition: avcodec.h:1792
void * priv_data
Format private data.
Definition: avformat.h:950
static void write_header(FFV1Context *f)
Definition: ffv1enc.c:380
common header for AIFF muxer and demuxer
void avio_wb32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:268
#define MKTAG(a, b, c, d)
Definition: common.h:238
This structure stores compressed data.
Definition: avcodec.h:950
int64_t form
Definition: aiffenc.c:31
AVOutputFormat ff_aiff_muxer
Definition: aiffenc.c:154