svn merge -r39781:39792 https://svn.blender.org/svnroot/bf-blender/trunk/blender...
[blender-staging.git] / intern / audaspace / ffmpeg / AUD_FFMPEGWriter.cpp
1 /*
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * Copyright 2009-2011 Jörg Hermann Müller
7  *
8  * This file is part of AudaSpace.
9  *
10  * Audaspace is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * AudaSpace is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Audaspace; if not, write to the Free Software Foundation,
22  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23  *
24  * ***** END GPL LICENSE BLOCK *****
25  */
26
27 /** \file audaspace/ffmpeg/AUD_FFMPEGWriter.cpp
28  *  \ingroup audffmpeg
29  */
30
31
32 // needed for INT64_C
33 #ifndef __STDC_CONSTANT_MACROS
34 #define __STDC_CONSTANT_MACROS
35 #endif
36
37 #include "AUD_FFMPEGWriter.h"
38
39 extern "C" {
40 #include <libavcodec/avcodec.h>
41 #include <libavformat/avformat.h>
42 #include <libavformat/avio.h>
43 #include "ffmpeg_compat.h"
44 }
45
46 static const char* context_error = "AUD_FFMPEGWriter: Couldn't allocate context.";
47 static const char* codec_error = "AUD_FFMPEGWriter: Invalid codec or codec not found.";
48 static const char* stream_error = "AUD_FFMPEGWriter: Couldn't allocate stream.";
49 static const char* format_error = "AUD_FFMPEGWriter: Unsupported sample format.";
50 static const char* file_error = "AUD_FFMPEGWriter: File couldn't be written.";
51 static const char* write_error = "AUD_FFMPEGWriter: Error writing packet.";
52
53 AUD_FFMPEGWriter::AUD_FFMPEGWriter(std::string filename, AUD_DeviceSpecs specs, AUD_Container format, AUD_Codec codec, unsigned int bitrate) :
54         m_position(0),
55         m_specs(specs),
56         m_input_samples(0)
57 {
58         static const char* formats[] = { NULL, "ac3", "flac", "matroska", "mp2", "mp3", "ogg", "wav" };
59
60         if(avformat_alloc_output_context2(&m_formatCtx, NULL, formats[format], filename.c_str()))
61                 AUD_THROW(AUD_ERROR_FFMPEG, context_error);
62
63         m_outputFmt = m_formatCtx->oformat;
64
65         switch(codec)
66         {
67         case AUD_CODEC_AAC:
68                 m_outputFmt->audio_codec = CODEC_ID_AAC;
69                 break;
70         case AUD_CODEC_AC3:
71                 m_outputFmt->audio_codec = CODEC_ID_AC3;
72                 break;
73         case AUD_CODEC_FLAC:
74                 m_outputFmt->audio_codec = CODEC_ID_FLAC;
75                 break;
76         case AUD_CODEC_MP2:
77                 m_outputFmt->audio_codec = CODEC_ID_MP2;
78                 break;
79         case AUD_CODEC_MP3:
80                 m_outputFmt->audio_codec = CODEC_ID_MP3;
81                 break;
82         case AUD_CODEC_PCM:
83                 switch(specs.format)
84                 {
85                 case AUD_FORMAT_U8:
86                         m_outputFmt->audio_codec = CODEC_ID_PCM_U8;
87                         break;
88                 case AUD_FORMAT_S16:
89                         m_outputFmt->audio_codec = CODEC_ID_PCM_S16LE;
90                         break;
91                 case AUD_FORMAT_S24:
92                         m_outputFmt->audio_codec = CODEC_ID_PCM_S24LE;
93                         break;
94                 case AUD_FORMAT_S32:
95                         m_outputFmt->audio_codec = CODEC_ID_PCM_S32LE;
96                         break;
97                 case AUD_FORMAT_FLOAT32:
98                         m_outputFmt->audio_codec = CODEC_ID_PCM_F32LE;
99                         break;
100                 case AUD_FORMAT_FLOAT64:
101                         m_outputFmt->audio_codec = CODEC_ID_PCM_F64LE;
102                         break;
103                 default:
104                         m_outputFmt->audio_codec = CODEC_ID_NONE;
105                         break;
106                 }
107                 break;
108         case AUD_CODEC_VORBIS:
109                 m_outputFmt->audio_codec = CODEC_ID_VORBIS;
110                 break;
111         default:
112                 m_outputFmt->audio_codec = CODEC_ID_NONE;
113                 break;
114         }
115
116         try
117         {
118                 if(m_outputFmt->audio_codec == CODEC_ID_NONE)
119                         AUD_THROW(AUD_ERROR_SPECS, codec_error);
120
121                 m_stream = av_new_stream(m_formatCtx, 0);
122                 if(!m_stream)
123                         AUD_THROW(AUD_ERROR_FFMPEG, stream_error);
124
125                 m_codecCtx = m_stream->codec;
126                 m_codecCtx->codec_id = m_outputFmt->audio_codec;
127                 m_codecCtx->codec_type = AVMEDIA_TYPE_AUDIO;
128                 m_codecCtx->bit_rate = bitrate;
129                 m_codecCtx->sample_rate = int(m_specs.rate);
130                 m_codecCtx->channels = m_specs.channels;
131                 m_codecCtx->time_base.num = 1;
132                 m_codecCtx->time_base.den = m_codecCtx->sample_rate;
133
134                 switch(m_specs.format)
135                 {
136                 case AUD_FORMAT_U8:
137                         m_convert = AUD_convert_float_u8;
138                         m_codecCtx->sample_fmt = SAMPLE_FMT_U8;
139                         break;
140                 case AUD_FORMAT_S16:
141                         m_convert = AUD_convert_float_s16;
142                         m_codecCtx->sample_fmt = SAMPLE_FMT_S16;
143                         break;
144                 case AUD_FORMAT_S32:
145                         m_convert = AUD_convert_float_s32;
146                         m_codecCtx->sample_fmt = SAMPLE_FMT_S32;
147                         break;
148                 case AUD_FORMAT_FLOAT32:
149                         m_convert = AUD_convert_copy<float>;
150                         m_codecCtx->sample_fmt = SAMPLE_FMT_FLT;
151                         break;
152                 case AUD_FORMAT_FLOAT64:
153                         m_convert = AUD_convert_float_double;
154                         m_codecCtx->sample_fmt = SAMPLE_FMT_DBL;
155                         break;
156                 default:
157                         AUD_THROW(AUD_ERROR_FFMPEG, format_error);
158                 }
159
160                 try
161                 {
162                         if(m_formatCtx->oformat->flags & AVFMT_GLOBALHEADER)
163                                 m_codecCtx->flags |= CODEC_FLAG_GLOBAL_HEADER;
164
165                         AVCodec* codec = avcodec_find_encoder(m_codecCtx->codec_id);
166                         if(!codec)
167                                 AUD_THROW(AUD_ERROR_FFMPEG, codec_error);
168
169                         if(avcodec_open(m_codecCtx, codec))
170                                 AUD_THROW(AUD_ERROR_FFMPEG, codec_error);
171
172                         m_output_buffer.resize(FF_MIN_BUFFER_SIZE);
173                         int samplesize = AUD_MAX(AUD_SAMPLE_SIZE(m_specs), AUD_DEVICE_SAMPLE_SIZE(m_specs));
174
175                         if(m_codecCtx->frame_size <= 1)
176                                 m_input_size = 0;
177                         else
178                         {
179                                 m_input_buffer.resize(m_codecCtx->frame_size * samplesize);
180                                 m_input_size = m_codecCtx->frame_size;
181                         }
182
183                         try
184                         {
185                                 if(avio_open(&m_formatCtx->pb, filename.c_str(), AVIO_FLAG_WRITE))
186                                         AUD_THROW(AUD_ERROR_FILE, file_error);
187
188                                 avformat_write_header(m_formatCtx, NULL);
189                         }
190                         catch(AUD_Exception&)
191                         {
192                                 avcodec_close(m_codecCtx);
193                                 av_freep(&m_formatCtx->streams[0]->codec);
194                                 throw;
195                         }
196                 }
197                 catch(AUD_Exception&)
198                 {
199                         av_freep(&m_formatCtx->streams[0]);
200                         throw;
201                 }
202         }
203         catch(AUD_Exception&)
204         {
205                 av_free(m_formatCtx);
206                 throw;
207         }
208 }
209
210 AUD_FFMPEGWriter::~AUD_FFMPEGWriter()
211 {
212         // writte missing data
213         if(m_input_samples)
214         {
215                 sample_t* buf = m_input_buffer.getBuffer();
216                 memset(buf + m_specs.channels * m_input_samples, 0,
217                            (m_input_size - m_input_samples) * AUD_DEVICE_SAMPLE_SIZE(m_specs));
218
219                 encode(buf);
220         }
221
222         av_write_trailer(m_formatCtx);
223
224         avcodec_close(m_codecCtx);
225
226         av_freep(&m_formatCtx->streams[0]->codec);
227         av_freep(&m_formatCtx->streams[0]);
228
229         avio_close(m_formatCtx->pb);
230         av_free(m_formatCtx);
231 }
232
233 int AUD_FFMPEGWriter::getPosition() const
234 {
235         return m_position;
236 }
237
238 AUD_DeviceSpecs AUD_FFMPEGWriter::getSpecs() const
239 {
240         return m_specs;
241 }
242
243 void AUD_FFMPEGWriter::encode(sample_t* data)
244 {
245         sample_t* outbuf = m_output_buffer.getBuffer();
246
247         // convert first
248         if(m_input_size)
249                 m_convert(reinterpret_cast<data_t*>(data), reinterpret_cast<data_t*>(data), m_input_size * m_specs.channels);
250
251         AVPacket packet;
252         av_init_packet(&packet);
253         packet.size = avcodec_encode_audio(m_codecCtx, reinterpret_cast<uint8_t*>(outbuf), m_output_buffer.getSize(), reinterpret_cast<short*>(data));
254         if(m_codecCtx->coded_frame && m_codecCtx->coded_frame->pts != AV_NOPTS_VALUE)
255                 packet.pts = av_rescale_q(m_codecCtx->coded_frame->pts, m_codecCtx->time_base, m_stream->time_base);
256         packet.flags |= AV_PKT_FLAG_KEY;
257         packet.stream_index = m_stream->index;
258         packet.data = reinterpret_cast<uint8_t*>(outbuf);
259
260         if(av_interleaved_write_frame(m_formatCtx, &packet))
261                 AUD_THROW(AUD_ERROR_FFMPEG, write_error);
262 }
263
264 void AUD_FFMPEGWriter::write(unsigned int length, sample_t* buffer)
265 {
266         unsigned int samplesize = AUD_SAMPLE_SIZE(m_specs);
267
268         if(m_input_size)
269         {
270                 sample_t* inbuf = m_input_buffer.getBuffer();
271
272                 while(length)
273                 {
274                         unsigned int len = AUD_MIN(m_input_size - m_input_samples, length);
275
276                         memcpy(inbuf + m_input_samples * m_specs.channels, buffer, len * samplesize);
277
278                         buffer += len * m_specs.channels;
279                         m_input_samples += len;
280                         m_position += len;
281                         length -= len;
282
283                         if(m_input_samples == m_input_size)
284                         {
285                                 encode(inbuf);
286
287                                 m_input_samples = 0;
288                         }
289                 }
290         }
291         else // PCM data, can write directly!
292         {
293                 int samplesize = AUD_SAMPLE_SIZE(m_specs);
294                 if(m_output_buffer.getSize() != length * m_specs.channels * m_codecCtx->bits_per_coded_sample / 8)
295                         m_output_buffer.resize(length * m_specs.channels * m_codecCtx->bits_per_coded_sample / 8);
296                 m_input_buffer.assureSize(length * AUD_MAX(AUD_DEVICE_SAMPLE_SIZE(m_specs), samplesize));
297
298                 sample_t* buf = m_input_buffer.getBuffer();
299                 m_convert(reinterpret_cast<data_t*>(buf), reinterpret_cast<data_t*>(buffer), length * m_specs.channels);
300
301                 encode(buf);
302
303                 m_position += length;
304         }
305 }