Remove in-place translations support.
[dcpomatic.git] / src / lib / ffmpeg_file_encoder.cc
index 57103abc712ab5523fb4c867c28198ed2dc32dcc..6d1ad68f79bb79d939e4baf25991b792ef4bbaa9 100644 (file)
@@ -127,7 +127,7 @@ public:
                return false;
        }
 
-       void write (int size, int channel_offset, int channels, float** data, int64_t sample_offset)
+       void write (int size, int channel_offset, int channels, float* const* data, int64_t sample_offset)
        {
                DCPOMATIC_ASSERT (size);
 
@@ -228,10 +228,17 @@ FFmpegFileEncoder::FFmpegFileEncoder (
        _pixel_format = pixel_format (format);
 
        switch (format) {
-       case ExportFormat::PRORES:
-               _sample_format = AV_SAMPLE_FMT_S16;
+       case ExportFormat::PRORES_4444:
+               _sample_format = AV_SAMPLE_FMT_S32;
                _video_codec_name = "prores_ks";
-               _audio_codec_name = "pcm_s16le";
+               _audio_codec_name = "pcm_s24le";
+               av_dict_set(&_video_options, "profile", "4", 0);
+               av_dict_set(&_video_options, "threads", "auto", 0);
+               break;
+       case ExportFormat::PRORES_HQ:
+               _sample_format = AV_SAMPLE_FMT_S32;
+               _video_codec_name = "prores_ks";
+               _audio_codec_name = "pcm_s24le";
                av_dict_set (&_video_options, "profile", "3", 0);
                av_dict_set (&_video_options, "threads", "auto", 0);
                break;
@@ -241,12 +248,6 @@ FFmpegFileEncoder::FFmpegFileEncoder (
                _audio_codec_name = "aac";
                av_dict_set_int (&_video_options, "crf", x264_crf, 0);
                break;
-       case ExportFormat::H264_PCM:
-               _sample_format = AV_SAMPLE_FMT_S32;
-               _video_codec_name = "libx264";
-               _audio_codec_name = "pcm_s24le";
-               av_dict_set_int (&_video_options, "crf", x264_crf, 0);
-               break;
        default:
                DCPOMATIC_ASSERT (false);
        }
@@ -271,7 +272,7 @@ FFmpegFileEncoder::FFmpegFileEncoder (
                throw EncodeError (N_("avformat_write_header"), N_("FFmpegFileEncoder::FFmpegFileEncoder"), r);
        }
 
-       _pending_audio.reset (new AudioBuffers(channels, 0));
+       _pending_audio = make_shared<AudioBuffers>(channels, 0);
 }
 
 
@@ -279,6 +280,8 @@ FFmpegFileEncoder::~FFmpegFileEncoder ()
 {
        _audio_streams.clear ();
        avcodec_close (_video_codec_context);
+       avio_close (_format_context->pb);
+       _format_context->pb = nullptr;
        avformat_free_context (_format_context);
 }
 
@@ -287,10 +290,11 @@ AVPixelFormat
 FFmpegFileEncoder::pixel_format (ExportFormat format)
 {
        switch (format) {
-       case ExportFormat::PRORES:
+       case ExportFormat::PRORES_4444:
+               return AV_PIX_FMT_YUV444P10;
+       case ExportFormat::PRORES_HQ:
                return AV_PIX_FMT_YUV422P10;
        case ExportFormat::H264_AAC:
-       case ExportFormat::H264_PCM:
                return AV_PIX_FMT_YUV420P;
        default:
                DCPOMATIC_ASSERT (false);
@@ -322,7 +326,7 @@ FFmpegFileEncoder::setup_video ()
        _video_codec_context->flags |= AV_CODEC_FLAG_QSCALE | AV_CODEC_FLAG_GLOBAL_HEADER;
 
        if (avcodec_open2 (_video_codec_context, _video_codec, &_video_options) < 0) {
-               throw EncodeError (N_("avcodec_open"), N_("FFmpegFileEncoder::setup_video"));
+               throw EncodeError(N_("avcodec_open2"), N_("FFmpegFileEncoder::setup_video"));
        }
 
        _video_stream = avformat_new_stream (_format_context, _video_codec);
@@ -379,18 +383,22 @@ FFmpegFileEncoder::flush ()
                        throw EncodeError (N_("avcodec_receive_packet"), N_("FFmpegFileEncoder::flush"), r);
                } else {
                        packet->stream_index = _video_stream_index;
+                       packet->duration = _video_stream->time_base.den / _video_frame_rate;
                        av_interleaved_write_frame (_format_context, packet.get());
                }
 
                flushed_audio = true;
-               for (auto i: _audio_streams) {
+               for (auto const& i: _audio_streams) {
                        if (!i->flush()) {
                                flushed_audio = false;
                        }
                }
        }
 
-       av_write_trailer (_format_context);
+       auto const r = av_write_trailer(_format_context);
+       if (r) {
+               throw EncodeError(N_("av_write_trailer"), N_("FFmpegFileEncoder::flush"), r);
+       }
 }
 
 
@@ -407,13 +415,8 @@ FFmpegFileEncoder::video (shared_ptr<PlayerVideo> video, DCPTime time)
        auto frame = av_frame_alloc ();
        DCPOMATIC_ASSERT (frame);
 
-       {
-               boost::mutex::scoped_lock lm (_pending_images_mutex);
-               _pending_images[image->data()[0]] = image;
-       }
-
        for (int i = 0; i < 3; ++i) {
-               auto buffer = av_buffer_create(image->data()[i], image->stride()[i] * image->size().height, &buffer_free, this, 0);
+               auto buffer = _pending_images.create_buffer(image, i);
                frame->buf[i] = av_buffer_ref (buffer);
                frame->data[i] = buffer->data;
                frame->linesize[i] = image->stride()[i];
@@ -438,6 +441,7 @@ FFmpegFileEncoder::video (shared_ptr<PlayerVideo> video, DCPTime time)
                throw EncodeError (N_("avcodec_receive_packet"), N_("FFmpegFileEncoder::video"), r);
        } else if (r >= 0) {
                packet->stream_index = _video_stream_index;
+               packet->duration = _video_stream->time_base.den / _video_frame_rate;
                av_interleaved_write_frame (_format_context, packet.get());
        }
 }
@@ -487,20 +491,3 @@ FFmpegFileEncoder::subtitle (PlayerText, DCPTimePeriod)
 {
 
 }
-
-
-void
-FFmpegFileEncoder::buffer_free (void* opaque, uint8_t* data)
-{
-       reinterpret_cast<FFmpegFileEncoder*>(opaque)->buffer_free2(data);
-}
-
-
-void
-FFmpegFileEncoder::buffer_free2 (uint8_t* data)
-{
-       boost::mutex::scoped_lock lm (_pending_images_mutex);
-       if (_pending_images.find(data) != _pending_images.end()) {
-               _pending_images.erase (data);
-       }
-}