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