+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 EncodeError (String::compose("avcodec_find_encoder_by_name failed for %1", codec_name));
+ }
+
+ _codec_context = avcodec_alloc_context3 (_codec);
+ if (!_codec_context) {
+ throw std::bad_alloc ();
+ }
+
+ /* 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;
+
+ int r = avcodec_open2 (_codec_context, _codec, 0);
+ if (r < 0) {
+ throw EncodeError (N_("avcodec_open2"), N_("ExportAudioStream::ExportAudioStream"), r);
+ }
+
+ _stream = avformat_new_stream (format_context, _codec);
+ if (!_stream) {
+ throw EncodeError (N_("avformat_new_stream"), N_("ExportAudioStream::ExportAudioStream"));
+ }
+
+ _stream->id = stream_index;
+ _stream->disposition |= AV_DISPOSITION_DEFAULT;
+ r = avcodec_parameters_from_context (_stream->codecpar, _codec_context);
+ if (r < 0) {
+ throw EncodeError (N_("avcodec_parameters_from_context"), N_("ExportAudioStream::ExportAudioStream"), r);
+ }
+ }
+
+ ~ExportAudioStream ()
+ {
+ avcodec_close (_codec_context);
+ }
+
+ ExportAudioStream (ExportAudioStream const&) = delete;
+ ExportAudioStream& operator= (ExportAudioStream const&) = delete;
+
+ int frame_size () const {
+ return _codec_context->frame_size;
+ }
+
+ bool flush ()
+ {
+ int r = avcodec_send_frame (_codec_context, nullptr);
+ if (r < 0 && r != AVERROR_EOF) {
+ /* We get EOF if we've already flushed the stream once */
+ throw EncodeError (N_("avcodec_send_frame"), N_("ExportAudioStream::flush"), r);
+ }
+
+ ffmpeg::Packet packet;
+ r = avcodec_receive_packet (_codec_context, packet.get());
+ if (r == AVERROR_EOF) {
+ return true;
+ } else if (r < 0) {
+ throw EncodeError (N_("avcodec_receive_packet"), N_("ExportAudioStream::flush"), r);
+ }
+
+ packet->stream_index = _stream_index;
+ av_interleaved_write_frame (_format_context, packet.get());
+ return false;
+ }
+
+ void write (int size, int channel_offset, int channels, float** data, int64_t sample_offset)
+ {
+ DCPOMATIC_ASSERT (size);
+
+ auto 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);
+
+ auto samples = av_malloc (buffer_size);
+ DCPOMATIC_ASSERT (samples);
+
+ frame->nb_samples = size;
+ frame->format = _codec_context->sample_fmt;
+ frame->channels = channels;
+ 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;
+
+ r = avcodec_send_frame (_codec_context, frame);
+ av_free (samples);
+ av_frame_free (&frame);
+ if (r < 0) {
+ throw EncodeError (N_("avcodec_send_frame"), N_("ExportAudioStream::write"), r);
+ }
+
+ ffmpeg::Packet packet;
+ r = avcodec_receive_packet (_codec_context, packet.get());
+ if (r < 0 && r != AVERROR(EAGAIN)) {
+ throw EncodeError (N_("avcodec_receive_packet"), N_("ExportAudioStream::write"), r);
+ } else if (r >= 0) {
+ packet->stream_index = _stream_index;
+ av_interleaved_write_frame (_format_context, packet.get());
+ }
+ }
+
+private:
+ AVFormatContext* _format_context;
+ AVCodec const * _codec;
+ AVCodecContext* _codec_context;
+ AVStream* _stream;
+ int _stream_index;
+};
+