Replace dcp::Data with dcp::ArrayData
[dcpomatic.git] / src / lib / ffmpeg_image_proxy.cc
index c83bebcb54b63a1ec17e842f83c23841c2deb7ec..602185bb8b4ade76013935cd6a096a08c957d995 100644 (file)
 #include "image.h"
 #include "compose.hpp"
 #include "util.h"
+#include "warnings.h"
 #include <dcp/raw_convert.h>
 extern "C" {
 #include <libavcodec/avcodec.h>
 #include <libavformat/avformat.h>
 }
+DCPOMATIC_DISABLE_WARNINGS
 #include <libxml++/libxml++.h>
+DCPOMATIC_ENABLE_WARNINGS
 #include <iostream>
 
 #include "i18n.h"
@@ -53,7 +56,7 @@ FFmpegImageProxy::FFmpegImageProxy (boost::filesystem::path path)
 
 }
 
-FFmpegImageProxy::FFmpegImageProxy (dcp::Data data)
+FFmpegImageProxy::FFmpegImageProxy (dcp::ArrayData data)
        : _data (data)
        , _pos (0)
 {
@@ -64,8 +67,8 @@ FFmpegImageProxy::FFmpegImageProxy (shared_ptr<cxml::Node>, shared_ptr<Socket> s
        : _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 +86,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<int64_t>(amount), static_cast<int64_t>(_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 +115,15 @@ FFmpegImageProxy::avio_seek (int64_t const pos, int whence)
        return _pos;
 }
 
-pair<shared_ptr<Image>, int>
-FFmpegImageProxy::image (optional<dcp::NoteHandler>, optional<dcp::Size>) const
+DCPOMATIC_DISABLE_WARNINGS
+
+ImageProxy::Result
+FFmpegImageProxy::image (optional<dcp::Size>) const
 {
        boost::mutex::scoped_lock lm (_mutex);
 
        if (_image) {
-               return make_pair (_image, 0);
+               return Result (_image, 0);
        }
 
        uint8_t* avio_buffer = static_cast<uint8_t*> (wrapped_av_malloc(4096));
@@ -131,8 +139,23 @@ FFmpegImageProxy::image (optional<dcp::NoteHandler>, optional<dcp::Size>) const
        av_dict_set (&options, "probesize", raw_convert<string>(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) {
@@ -167,14 +190,18 @@ FFmpegImageProxy::image (optional<dcp::NoteHandler>, optional<dcp::Size>) const
 
        _image.reset (new Image (frame));
 
+       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
 {
@@ -182,10 +209,10 @@ FFmpegImageProxy::add_metadata (xmlpp::Node* node) const
 }
 
 void
-FFmpegImageProxy::send_binary (shared_ptr<Socket> socket) const
+FFmpegImageProxy::write_to_socket (shared_ptr<Socket> socket) const
 {
        socket->write (_data.size());
-       socket->write (_data.data().get(), _data.size());
+       socket->write (_data.data(), _data.size());
 }
 
 bool
@@ -196,11 +223,7 @@ FFmpegImageProxy::same (shared_ptr<const ImageProxy> 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