X-Git-Url: https://git.carlh.net/gitweb/?a=blobdiff_plain;ds=sidebyside;f=src%2Flib%2Fffmpeg_file_encoder.cc;h=6d1ad68f79bb79d939e4baf25991b792ef4bbaa9;hb=9d1d1cea1cdf17b4cc2208800ca22288f979d3ec;hp=6f33700a249422314a31cc52102a45cf8e66b4e7;hpb=e55b2b3a7eb05b3cc69411b133aeec9772420c83;p=dcpomatic.git diff --git a/src/lib/ffmpeg_file_encoder.cc b/src/lib/ffmpeg_file_encoder.cc index 6f33700a2..6d1ad68f7 100644 --- a/src/lib/ffmpeg_file_encoder.cc +++ b/src/lib/ffmpeg_file_encoder.cc @@ -229,16 +229,16 @@ FFmpegFileEncoder::FFmpegFileEncoder ( switch (format) { case ExportFormat::PRORES_4444: - _sample_format = AV_SAMPLE_FMT_S16; + _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_S16; + _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", "3", 0); av_dict_set (&_video_options, "threads", "auto", 0); break; @@ -281,6 +281,7 @@ FFmpegFileEncoder::~FFmpegFileEncoder () _audio_streams.clear (); avcodec_close (_video_codec_context); avio_close (_format_context->pb); + _format_context->pb = nullptr; avformat_free_context (_format_context); } @@ -325,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); @@ -382,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); + } } @@ -410,19 +415,8 @@ FFmpegFileEncoder::video (shared_ptr video, DCPTime time) auto frame = av_frame_alloc (); DCPOMATIC_ASSERT (frame); - { - boost::mutex::scoped_lock lm (_pending_images_mutex); - auto key = image->data()[0]; - auto iter = _pending_images.find(key); - if (iter != _pending_images.end()) { - iter->second.second++; - } else { - _pending_images[key] = { image, 1 }; - } - } - 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]; @@ -447,6 +441,7 @@ FFmpegFileEncoder::video (shared_ptr 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()); } } @@ -496,24 +491,3 @@ FFmpegFileEncoder::subtitle (PlayerText, DCPTimePeriod) { } - - -void -FFmpegFileEncoder::buffer_free (void* opaque, uint8_t* data) -{ - reinterpret_cast(opaque)->buffer_free2(data); -} - - -void -FFmpegFileEncoder::buffer_free2 (uint8_t* data) -{ - boost::mutex::scoped_lock lm (_pending_images_mutex); - auto iter = _pending_images.find(data); - if (iter != _pending_images.end()) { - iter->second.second--; - if (iter->second.second == 0) { - _pending_images.erase(data); - } - } -}