X-Git-Url: https://git.carlh.net/gitweb/?a=blobdiff_plain;f=src%2Flib%2Fffmpeg_image_proxy.cc;h=c978fc3834f95f8e63c691876315744ace4c5746;hb=8a1da1da65ba0e6a6fb915c73ff6e6a81da349f2;hp=c83bebcb54b63a1ec17e842f83c23841c2deb7ec;hpb=72b11d5eb036651b6ff68edf3ed270e8fc52960f;p=dcpomatic.git diff --git a/src/lib/ffmpeg_image_proxy.cc b/src/lib/ffmpeg_image_proxy.cc index c83bebcb5..c978fc383 100644 --- a/src/lib/ffmpeg_image_proxy.cc +++ b/src/lib/ffmpeg_image_proxy.cc @@ -25,12 +25,16 @@ #include "image.h" #include "compose.hpp" #include "util.h" +#include "warnings.h" #include extern "C" { #include #include +#include } +DCPOMATIC_DISABLE_WARNINGS #include +DCPOMATIC_ENABLE_WARNINGS #include #include "i18n.h" @@ -45,27 +49,30 @@ using boost::optional; using boost::dynamic_pointer_cast; using dcp::raw_convert; -FFmpegImageProxy::FFmpegImageProxy (boost::filesystem::path path) +FFmpegImageProxy::FFmpegImageProxy (boost::filesystem::path path, VideoRange video_range) : _data (path) + , _video_range (video_range) , _pos (0) , _path (path) { } -FFmpegImageProxy::FFmpegImageProxy (dcp::Data data) +FFmpegImageProxy::FFmpegImageProxy (dcp::ArrayData data, VideoRange video_range) : _data (data) + , _video_range (video_range) , _pos (0) { } -FFmpegImageProxy::FFmpegImageProxy (shared_ptr, shared_ptr socket) - : _pos (0) +FFmpegImageProxy::FFmpegImageProxy (shared_ptr node, shared_ptr socket) + : _video_range (string_to_video_range(node->string_child("VideoRange"))) + , _pos (0) { uint32_t const size = socket->read_uint32 (); - _data = dcp::Data (size); - socket->read (_data.data().get(), size); + _data = dcp::ArrayData (size); + socket->read (_data.data(), size); } static int @@ -83,8 +90,11 @@ avio_seek_wrapper (void* data, int64_t offset, int whence) int FFmpegImageProxy::avio_read (uint8_t* buffer, int const amount) { - int const to_do = min(int64_t(amount), _data.size() - _pos); - memcpy (buffer, _data.data().get() + _pos, to_do); + int const to_do = min(static_cast(amount), static_cast(_data.size()) - _pos); + if (to_do == 0) { + return AVERROR_EOF; + } + memcpy (buffer, _data.data() + _pos, to_do); _pos += to_do; return to_do; } @@ -109,13 +119,15 @@ FFmpegImageProxy::avio_seek (int64_t const pos, int whence) return _pos; } -pair, int> -FFmpegImageProxy::image (optional, optional) const +DCPOMATIC_DISABLE_WARNINGS + +ImageProxy::Result +FFmpegImageProxy::image (optional) const { boost::mutex::scoped_lock lm (_mutex); if (_image) { - return make_pair (_image, 0); + return Result (_image, 0); } uint8_t* avio_buffer = static_cast (wrapped_av_malloc(4096)); @@ -131,8 +143,23 @@ FFmpegImageProxy::image (optional, optional) const av_dict_set (&options, "probesize", raw_convert(5 * 60 * 1000000).c_str(), 0); int e = avformat_open_input (&format_context, 0, 0, &options); + if ((e < 0 && e == AVERROR_INVALIDDATA) || (e >= 0 && format_context->probe_score <= 25)) { + /* Hack to fix loading of .tga files through AVIOContexts (rather then + directly from the file). This code just does enough to allow the + probe code to take a hint from "foo.tga" and so try targa format. + */ + AVInputFormat* f = av_find_input_format ("image2"); + format_context = avformat_alloc_context (); + format_context->pb = avio_context; + format_context->iformat = f; + e = avformat_open_input (&format_context, "foo.tga", f, &options); + } if (e < 0) { - throw OpenFileError (_path->string(), e, true); + if (_path) { + throw OpenFileError (_path->string(), e, OpenFileError::READ); + } else { + boost::throw_exception(DecodeError(String::compose(_("Could not decode image (%1)"), e))); + } } if (avformat_find_stream_info(format_context, 0) < 0) { @@ -165,27 +192,41 @@ FFmpegImageProxy::image (optional, optional) const throw DecodeError (N_("could not decode video")); } - _image.reset (new Image (frame)); + AVPixelFormat const pix_fmt = static_cast(frame->format); + + _image.reset (new Image(frame)); + if (_video_range == VIDEO_RANGE_VIDEO && av_pix_fmt_desc_get(pix_fmt)->flags & AV_PIX_FMT_FLAG_RGB) { + /* Asking for the video range to be converted by libswscale (in Image) will not work for + * RGB sources since that method only processes video range in YUV and greyscale. So we have + * to do it ourselves here. + */ + _image->video_range_to_full_range(); + } + av_packet_unref (&packet); av_frame_free (&frame); + avcodec_close (codec_context); avformat_close_input (&format_context); av_free (avio_context->buffer); av_free (avio_context); - return make_pair (_image, 0); + return Result (_image, 0); } +DCPOMATIC_ENABLE_WARNINGS + void FFmpegImageProxy::add_metadata (xmlpp::Node* node) const { node->add_child("Type")->add_child_text (N_("FFmpeg")); + node->add_child("VideoRange")->add_child_text(video_range_to_string(_video_range)); } void -FFmpegImageProxy::send_binary (shared_ptr socket) const +FFmpegImageProxy::write_to_socket (shared_ptr socket) const { socket->write (_data.size()); - socket->write (_data.data().get(), _data.size()); + socket->write (_data.data(), _data.size()); } bool @@ -196,11 +237,7 @@ FFmpegImageProxy::same (shared_ptr other) const return false; } - if (_data.size() != mp->_data.size()) { - return false; - } - - return memcmp (_data.data().get(), mp->_data.data().get(), _data.size()) == 0; + return _data == mp->_data; } size_t