OSDN Git Service

ffmdec: handle wrapped file in ffm_seek
[coroid/ffmpeg_saccubus.git] / libavformat / aiffenc.c
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 "libavutil/intfloat_readwrite.h"
23 #include "avformat.h"
24 #include "aiff.h"
25 #include "avio_internal.h"
26 #include "isom.h"
27
28 typedef struct {
29     int64_t form;
30     int64_t frames;
31     int64_t ssnd;
32 } AIFFOutputContext;
33
34 static int aiff_write_header(AVFormatContext *s)
35 {
36     AIFFOutputContext *aiff = s->priv_data;
37     AVIOContext *pb = s->pb;
38     AVCodecContext *enc = s->streams[0]->codec;
39     AVExtFloat 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     if (enc->channels > 2 && enc->channel_layout) {
67         ffio_wfourcc(pb, "CHAN");
68         avio_wb32(pb, 12);
69         ff_mov_write_chan(pb, enc->channel_layout);
70     }
71
72     /* Common chunk */
73     ffio_wfourcc(pb, "COMM");
74     avio_wb32(pb, aifc ? 24 : 18); /* size */
75     avio_wb16(pb, enc->channels);  /* Number of channels */
76
77     aiff->frames = avio_tell(pb);
78     avio_wb32(pb, 0);              /* Number of frames */
79
80     if (!enc->bits_per_coded_sample)
81         enc->bits_per_coded_sample = av_get_bits_per_sample(enc->codec_id);
82     if (!enc->bits_per_coded_sample) {
83         av_log(s, AV_LOG_ERROR, "could not compute bits per sample\n");
84         return -1;
85     }
86     if (!enc->block_align)
87         enc->block_align = (enc->bits_per_coded_sample * enc->channels) >> 3;
88
89     avio_wb16(pb, enc->bits_per_coded_sample); /* Sample size */
90
91     sample_rate = av_dbl2ext((double)enc->sample_rate);
92     avio_write(pb, (uint8_t*)&sample_rate, sizeof(sample_rate));
93
94     if (aifc) {
95         avio_wl32(pb, enc->codec_tag);
96         avio_wb16(pb, 0);
97     }
98
99     /* Sound data chunk */
100     ffio_wfourcc(pb, "SSND");
101     aiff->ssnd = avio_tell(pb);         /* Sound chunk size */
102     avio_wb32(pb, 0);                    /* Sound samples data size */
103     avio_wb32(pb, 0);                    /* Data offset */
104     avio_wb32(pb, 0);                    /* Block-size (block align) */
105
106     av_set_pts_info(s->streams[0], 64, 1, s->streams[0]->codec->sample_rate);
107
108     /* Data is starting here */
109     avio_flush(pb);
110
111     return 0;
112 }
113
114 static int aiff_write_packet(AVFormatContext *s, AVPacket *pkt)
115 {
116     AVIOContext *pb = s->pb;
117     avio_write(pb, pkt->data, pkt->size);
118     return 0;
119 }
120
121 static int aiff_write_trailer(AVFormatContext *s)
122 {
123     AVIOContext *pb = s->pb;
124     AIFFOutputContext *aiff = s->priv_data;
125     AVCodecContext *enc = s->streams[0]->codec;
126
127     /* Chunks sizes must be even */
128     int64_t file_size, end_size;
129     end_size = file_size = avio_tell(pb);
130     if (file_size & 1) {
131         avio_w8(pb, 0);
132         end_size++;
133     }
134
135     if (s->pb->seekable) {
136         /* File length */
137         avio_seek(pb, aiff->form, SEEK_SET);
138         avio_wb32(pb, file_size - aiff->form - 4);
139
140         /* Number of sample frames */
141         avio_seek(pb, aiff->frames, SEEK_SET);
142         avio_wb32(pb, (file_size-aiff->ssnd-12)/enc->block_align);
143
144         /* Sound Data chunk size */
145         avio_seek(pb, aiff->ssnd, SEEK_SET);
146         avio_wb32(pb, file_size - aiff->ssnd - 4);
147
148         /* return to the end */
149         avio_seek(pb, end_size, SEEK_SET);
150
151         avio_flush(pb);
152     }
153
154     return 0;
155 }
156
157 AVOutputFormat ff_aiff_muxer = {
158     .name              = "aiff",
159     .long_name         = NULL_IF_CONFIG_SMALL("Audio IFF"),
160     .mime_type         = "audio/aiff",
161     .extensions        = "aif,aiff,afc,aifc",
162     .priv_data_size    = sizeof(AIFFOutputContext),
163     .audio_codec       = CODEC_ID_PCM_S16BE,
164     .video_codec       = CODEC_ID_NONE,
165     .write_header      = aiff_write_header,
166     .write_packet      = aiff_write_packet,
167     .write_trailer     = aiff_write_trailer,
168     .codec_tag= (const AVCodecTag* const []){ff_codec_aiff_tags, 0},
169 };