+int FFmpegFileEncoder::_audio_stream_index_base = 1;
+
+
+class ExportAudioStream : public boost::noncopyable
+{
+public:
+ ExportAudioStream (string codec_name, int channels, int frame_rate, AVSampleFormat sample_format, AVFormatContext* format_context, int stream_index)
+ : _format_context (format_context)
+ , _stream_index (stream_index)
+ {
+ _codec = avcodec_find_encoder_by_name (codec_name.c_str());
+ if (!_codec) {
+ throw runtime_error (String::compose("could not find FFmpeg encoder %1", codec_name));
+ }
+
+ _codec_context = avcodec_alloc_context3 (_codec);
+ if (!_codec_context) {
+ throw runtime_error ("could not allocate FFmpeg audio context");
+ }
+
+ avcodec_get_context_defaults3 (_codec_context, _codec);
+
+ /* XXX: configurable */
+ _codec_context->bit_rate = channels * 128 * 1024;
+ _codec_context->sample_fmt = sample_format;
+ _codec_context->sample_rate = frame_rate;
+ _codec_context->channel_layout = av_get_default_channel_layout (channels);
+ _codec_context->channels = channels;
+
+ _stream = avformat_new_stream (format_context, _codec);
+ if (!_stream) {
+ throw runtime_error ("could not create FFmpeg output audio stream");
+ }
+
+ _stream->id = stream_index;
+ _stream->disposition |= AV_DISPOSITION_DEFAULT;
+DCPOMATIC_DISABLE_WARNINGS
+ _stream->codec = _codec_context;
+DCPOMATIC_ENABLE_WARNINGS
+
+ int r = avcodec_open2 (_codec_context, _codec, 0);
+ if (r < 0) {
+ char buffer[256];
+ av_strerror (r, buffer, sizeof(buffer));
+ throw runtime_error (String::compose("could not open FFmpeg audio codec (%1)", buffer));
+ }
+ }
+
+ ~ExportAudioStream ()
+ {
+ avcodec_close (_codec_context);
+ }
+
+ int frame_size () const {
+ return _codec_context->frame_size;
+ }
+
+ bool flush ()
+ {
+ AVPacket packet;
+ av_init_packet (&packet);
+ packet.data = 0;
+ packet.size = 0;
+ bool flushed = false;
+
+ int got_packet;
+DCPOMATIC_DISABLE_WARNINGS
+ avcodec_encode_audio2 (_codec_context, &packet, 0, &got_packet);
+DCPOMATIC_ENABLE_WARNINGS
+ if (got_packet) {
+ packet.stream_index = 0;
+ av_interleaved_write_frame (_format_context, &packet);
+ } else {
+ flushed = true;
+ }
+ av_packet_unref (&packet);
+ return flushed;
+ }
+
+ void write (int size, int channel_offset, int channels, float** data, int64_t sample_offset)
+ {
+ DCPOMATIC_ASSERT (size);
+
+ AVFrame* frame = av_frame_alloc ();
+ DCPOMATIC_ASSERT (frame);
+
+ int const buffer_size = av_samples_get_buffer_size (0, channels, size, _codec_context->sample_fmt, 0);
+ DCPOMATIC_ASSERT (buffer_size >= 0);
+
+ void* samples = av_malloc (buffer_size);
+ DCPOMATIC_ASSERT (samples);
+
+ frame->nb_samples = size;
+ int r = avcodec_fill_audio_frame (frame, channels, _codec_context->sample_fmt, (const uint8_t *) samples, buffer_size, 0);
+ DCPOMATIC_ASSERT (r >= 0);
+
+ switch (_codec_context->sample_fmt) {
+ case AV_SAMPLE_FMT_S16:
+ {
+ int16_t* q = reinterpret_cast<int16_t*> (samples);
+ for (int i = 0; i < size; ++i) {
+ for (int j = 0; j < channels; ++j) {
+ *q++ = data[j + channel_offset][i] * 32767;
+ }
+ }
+ break;
+ }
+ case AV_SAMPLE_FMT_S32:
+ {
+ int32_t* q = reinterpret_cast<int32_t*> (samples);
+ for (int i = 0; i < size; ++i) {
+ for (int j = 0; j < channels; ++j) {
+ *q++ = data[j + channel_offset][i] * 2147483647;
+ }
+ }
+ break;
+ }
+ case AV_SAMPLE_FMT_FLTP:
+ {
+ float* q = reinterpret_cast<float*> (samples);
+ for (int i = 0; i < channels; ++i) {
+ memcpy (q, data[i + channel_offset], sizeof(float) * size);
+ q += size;
+ }
+ break;
+ }
+ default:
+ DCPOMATIC_ASSERT (false);
+ }
+
+ DCPOMATIC_ASSERT (_codec_context->time_base.num == 1);
+ frame->pts = sample_offset * _codec_context->time_base.den / _codec_context->sample_rate;
+
+ AVPacket packet;
+ av_init_packet (&packet);
+ packet.data = 0;
+ packet.size = 0;
+ int got_packet;
+
+ DCPOMATIC_DISABLE_WARNINGS
+ if (avcodec_encode_audio2 (_codec_context, &packet, frame, &got_packet) < 0) {
+ throw EncodeError ("FFmpeg audio encode failed");
+ }
+ DCPOMATIC_ENABLE_WARNINGS
+
+ if (got_packet && packet.size) {
+ packet.stream_index = _stream_index;
+ av_interleaved_write_frame (_format_context, &packet);
+ av_packet_unref (&packet);
+ }
+
+ av_free (samples);
+ av_frame_free (&frame);
+ }
+
+private:
+ AVFormatContext* _format_context;
+ AVCodec* _codec;
+ AVCodecContext* _codec_context;
+ AVStream* _stream;
+ int _stream_index;
+};
+