Supporters update.
[dcpomatic.git] / src / lib / ffmpeg_image_proxy.cc
1 /*
2     Copyright (C) 2014-2018 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21
22 #include "compose.hpp"
23 #include "cross.h"
24 #include "dcpomatic_assert.h"
25 #include "dcpomatic_socket.h"
26 #include "exceptions.h"
27 #include "ffmpeg_image_proxy.h"
28 #include "image.h"
29 #include "memory_util.h"
30 #include "video_filter_graph.h"
31 #include <dcp/raw_convert.h>
32 #include <dcp/warnings.h>
33 LIBDCP_DISABLE_WARNINGS
34 extern "C" {
35 #include <libavcodec/avcodec.h>
36 #include <libavformat/avformat.h>
37 #include <libavutil/pixdesc.h>
38 }
39 #include <libxml++/libxml++.h>
40 LIBDCP_ENABLE_WARNINGS
41 #include <iostream>
42
43 #include "i18n.h"
44
45
46 using std::cout;
47 using std::make_pair;
48 using std::make_shared;
49 using std::min;
50 using std::pair;
51 using std::shared_ptr;
52 using std::string;
53 using boost::optional;
54 using std::dynamic_pointer_cast;
55 using dcp::raw_convert;
56
57
58 FFmpegImageProxy::FFmpegImageProxy (boost::filesystem::path path)
59         : _data (path)
60         , _pos (0)
61         , _path (path)
62 {
63
64 }
65
66 FFmpegImageProxy::FFmpegImageProxy (dcp::ArrayData data)
67         : _data (data)
68         , _pos (0)
69 {
70
71 }
72
73 FFmpegImageProxy::FFmpegImageProxy (shared_ptr<Socket> socket)
74         : _pos (0)
75 {
76         uint32_t const size = socket->read_uint32 ();
77         _data = dcp::ArrayData (size);
78         socket->read (_data.data(), size);
79 }
80
81 static int
82 avio_read_wrapper (void* data, uint8_t* buffer, int amount)
83 {
84         return reinterpret_cast<FFmpegImageProxy*>(data)->avio_read (buffer, amount);
85 }
86
87 static int64_t
88 avio_seek_wrapper (void* data, int64_t offset, int whence)
89 {
90         return reinterpret_cast<FFmpegImageProxy*>(data)->avio_seek (offset, whence);
91 }
92
93 int
94 FFmpegImageProxy::avio_read (uint8_t* buffer, int const amount)
95 {
96         int const to_do = min(static_cast<int64_t>(amount), static_cast<int64_t>(_data.size()) - _pos);
97         if (to_do == 0) {
98                 return AVERROR_EOF;
99         }
100         memcpy (buffer, _data.data() + _pos, to_do);
101         _pos += to_do;
102         return to_do;
103 }
104
105 int64_t
106 FFmpegImageProxy::avio_seek (int64_t const pos, int whence)
107 {
108         switch (whence) {
109         case AVSEEK_SIZE:
110                 return _data.size();
111         case SEEK_CUR:
112                 _pos += pos;
113                 break;
114         case SEEK_SET:
115                 _pos = pos;
116                 break;
117         case SEEK_END:
118                 _pos = _data.size() - pos;
119                 break;
120         }
121
122         return _pos;
123 }
124
125
126 ImageProxy::Result
127 FFmpegImageProxy::image (Image::Alignment alignment, optional<dcp::Size>) const
128 {
129         auto constexpr name_for_errors = "FFmpegImageProxy::image";
130
131         boost::mutex::scoped_lock lm (_mutex);
132
133         if (_image) {
134                 return Result (_image, 0);
135         }
136
137         uint8_t* avio_buffer = static_cast<uint8_t*> (wrapped_av_malloc(4096));
138         auto avio_context = avio_alloc_context (avio_buffer, 4096, 0, const_cast<FFmpegImageProxy*>(this), avio_read_wrapper, 0, avio_seek_wrapper);
139         AVFormatContext* format_context = avformat_alloc_context ();
140         format_context->pb = avio_context;
141
142         AVDictionary* options = nullptr;
143         /* These durations are in microseconds, and represent how far into the content file
144            we will look for streams.
145         */
146         av_dict_set (&options, "analyzeduration", raw_convert<string>(5 * 60 * 1000000).c_str(), 0);
147         av_dict_set (&options, "probesize", raw_convert<string>(5 * 60 * 1000000).c_str(), 0);
148
149         int e = avformat_open_input (&format_context, 0, 0, &options);
150         if ((e < 0 && e == AVERROR_INVALIDDATA) || (e >= 0 && format_context->probe_score <= 25)) {
151                 /* Hack to fix loading of .tga files through AVIOContexts (rather then
152                    directly from the file).  This code just does enough to allow the
153                    probe code to take a hint from "foo.tga" and so try targa format.
154                 */
155                 auto f = av_find_input_format ("image2");
156                 format_context = avformat_alloc_context ();
157                 format_context->pb = avio_context;
158                 format_context->iformat = f;
159                 e = avformat_open_input (&format_context, "foo.tga", f, &options);
160         }
161         if (e < 0) {
162                 if (_path) {
163                         throw OpenFileError (_path->string(), e, OpenFileError::READ);
164                 } else {
165                         boost::throw_exception(DecodeError(String::compose(_("Could not decode image (%1)"), e)));
166                 }
167         }
168
169         int r = avformat_find_stream_info(format_context, 0);
170         if (r < 0) {
171                 throw DecodeError (N_("avcodec_find_stream_info"), name_for_errors, r, *_path);
172         }
173
174         DCPOMATIC_ASSERT (format_context->nb_streams == 1);
175
176         auto frame = av_frame_alloc ();
177         if (!frame) {
178                 std::bad_alloc ();
179         }
180
181         auto codec = avcodec_find_decoder (format_context->streams[0]->codecpar->codec_id);
182         DCPOMATIC_ASSERT (codec);
183
184         auto context = avcodec_alloc_context3 (codec);
185         if (!context) {
186                 throw DecodeError (N_("avcodec_alloc_context3"), name_for_errors, *_path);
187         }
188
189         r = avcodec_open2 (context, codec, 0);
190         if (r < 0) {
191                 throw DecodeError (N_("avcodec_open2"), name_for_errors, r, *_path);
192         }
193
194         AVPacket packet;
195         r = av_read_frame (format_context, &packet);
196         if (r < 0) {
197                 throw DecodeError (N_("av_read_frame"), name_for_errors, r, *_path);
198         }
199
200         r = avcodec_send_packet (context, &packet);
201         if (r < 0) {
202                 throw DecodeError (N_("avcodec_send_packet"), name_for_errors, r, *_path);
203         }
204
205         r = avcodec_receive_frame (context, frame);
206         if (r < 0) {
207                 throw DecodeError (N_("avcodec_receive_frame"), name_for_errors, r, *_path);
208         }
209
210         if (av_pix_fmt_desc_get(context->pix_fmt)->flags & AV_PIX_FMT_FLAG_ALPHA) {
211                 /* XXX: this repeated setup of a the filter graph could be really slow
212                  * (haven't measured it though).
213                  */
214                 VideoFilterGraph graph(dcp::Size(frame->width, frame->height), context->pix_fmt, dcp::Fraction(24, 1));
215                 auto filter = Filter::from_id("premultiply");
216                 DCPOMATIC_ASSERT(filter);
217                 graph.setup({*filter});
218                 auto images = graph.process(frame);
219                 DCPOMATIC_ASSERT(images.size() == 1);
220                 _image = images.front().first;
221         } else {
222                 _image = make_shared<Image>(frame, alignment);
223         }
224
225         av_packet_unref (&packet);
226         av_frame_free (&frame);
227         avcodec_free_context (&context);
228         avformat_close_input (&format_context);
229         av_free (avio_context->buffer);
230         av_free (avio_context);
231
232         return Result (_image, 0);
233 }
234
235
236 void
237 FFmpegImageProxy::add_metadata (xmlpp::Node* node) const
238 {
239         node->add_child("Type")->add_child_text (N_("FFmpeg"));
240 }
241
242 void
243 FFmpegImageProxy::write_to_socket (shared_ptr<Socket> socket) const
244 {
245         socket->write (_data.size());
246         socket->write (_data.data(), _data.size());
247 }
248
249 bool
250 FFmpegImageProxy::same (shared_ptr<const ImageProxy> other) const
251 {
252         auto mp = dynamic_pointer_cast<const FFmpegImageProxy>(other);
253         if (!mp) {
254                 return false;
255         }
256
257         return _data == mp->_data;
258 }
259
260 size_t
261 FFmpegImageProxy::memory_used () const
262 {
263         size_t m = _data.size();
264         if (_image) {
265                 m += _image->memory_used();
266         }
267         return m;
268 }