2 Copyright (C) 2014-2018 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
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.
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.
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/>.
21 #include "ffmpeg_image_proxy.h"
23 #include "exceptions.h"
24 #include "dcpomatic_socket.h"
26 #include "compose.hpp"
29 #include <dcp/raw_convert.h>
31 #include <libavcodec/avcodec.h>
32 #include <libavformat/avformat.h>
34 DCPOMATIC_DISABLE_WARNINGS
35 #include <libxml++/libxml++.h>
36 DCPOMATIC_ENABLE_WARNINGS
46 using boost::shared_ptr;
47 using boost::optional;
48 using boost::dynamic_pointer_cast;
49 using dcp::raw_convert;
51 FFmpegImageProxy::FFmpegImageProxy (boost::filesystem::path path)
59 FFmpegImageProxy::FFmpegImageProxy (dcp::ArrayData data)
66 FFmpegImageProxy::FFmpegImageProxy (shared_ptr<cxml::Node>, shared_ptr<Socket> socket)
69 uint32_t const size = socket->read_uint32 ();
70 _data = dcp::ArrayData (size);
71 socket->read (_data.data(), size);
75 avio_read_wrapper (void* data, uint8_t* buffer, int amount)
77 return reinterpret_cast<FFmpegImageProxy*>(data)->avio_read (buffer, amount);
81 avio_seek_wrapper (void* data, int64_t offset, int whence)
83 return reinterpret_cast<FFmpegImageProxy*>(data)->avio_seek (offset, whence);
87 FFmpegImageProxy::avio_read (uint8_t* buffer, int const amount)
89 int const to_do = min(static_cast<int64_t>(amount), static_cast<int64_t>(_data.size()) - _pos);
93 memcpy (buffer, _data.data() + _pos, to_do);
99 FFmpegImageProxy::avio_seek (int64_t const pos, int whence)
111 _pos = _data.size() - pos;
118 DCPOMATIC_DISABLE_WARNINGS
121 FFmpegImageProxy::image (optional<dcp::Size>) const
123 boost::mutex::scoped_lock lm (_mutex);
126 return Result (_image, 0);
129 uint8_t* avio_buffer = static_cast<uint8_t*> (wrapped_av_malloc(4096));
130 AVIOContext* avio_context = avio_alloc_context (avio_buffer, 4096, 0, const_cast<FFmpegImageProxy*>(this), avio_read_wrapper, 0, avio_seek_wrapper);
131 AVFormatContext* format_context = avformat_alloc_context ();
132 format_context->pb = avio_context;
134 AVDictionary* options = 0;
135 /* These durations are in microseconds, and represent how far into the content file
136 we will look for streams.
138 av_dict_set (&options, "analyzeduration", raw_convert<string>(5 * 60 * 1000000).c_str(), 0);
139 av_dict_set (&options, "probesize", raw_convert<string>(5 * 60 * 1000000).c_str(), 0);
141 int e = avformat_open_input (&format_context, 0, 0, &options);
142 if ((e < 0 && e == AVERROR_INVALIDDATA) || (e >= 0 && format_context->probe_score <= 25)) {
143 /* Hack to fix loading of .tga files through AVIOContexts (rather then
144 directly from the file). This code just does enough to allow the
145 probe code to take a hint from "foo.tga" and so try targa format.
147 AVInputFormat* f = av_find_input_format ("image2");
148 format_context = avformat_alloc_context ();
149 format_context->pb = avio_context;
150 format_context->iformat = f;
151 e = avformat_open_input (&format_context, "foo.tga", f, &options);
155 throw OpenFileError (_path->string(), e, OpenFileError::READ);
157 boost::throw_exception(DecodeError(String::compose(_("Could not decode image (%1)"), e)));
161 if (avformat_find_stream_info(format_context, 0) < 0) {
162 throw DecodeError (_("could not find stream information"));
165 DCPOMATIC_ASSERT (format_context->nb_streams == 1);
167 AVFrame* frame = av_frame_alloc ();
169 throw DecodeError (N_("could not allocate frame"));
172 AVCodecContext* codec_context = format_context->streams[0]->codec;
173 AVCodec* codec = avcodec_find_decoder (codec_context->codec_id);
174 DCPOMATIC_ASSERT (codec);
176 if (avcodec_open2 (codec_context, codec, 0) < 0) {
177 throw DecodeError (N_("could not open decoder"));
181 int r = av_read_frame (format_context, &packet);
183 throw DecodeError (N_("could not read frame"));
187 if (avcodec_decode_video2(codec_context, frame, &frame_finished, &packet) < 0 || !frame_finished) {
188 throw DecodeError (N_("could not decode video"));
191 _image.reset (new Image (frame));
193 av_packet_unref (&packet);
194 av_frame_free (&frame);
195 avcodec_close (codec_context);
196 avformat_close_input (&format_context);
197 av_free (avio_context->buffer);
198 av_free (avio_context);
200 return Result (_image, 0);
203 DCPOMATIC_ENABLE_WARNINGS
206 FFmpegImageProxy::add_metadata (xmlpp::Node* node) const
208 node->add_child("Type")->add_child_text (N_("FFmpeg"));
212 FFmpegImageProxy::write_to_socket (shared_ptr<Socket> socket) const
214 socket->write (_data.size());
215 socket->write (_data.data(), _data.size());
219 FFmpegImageProxy::same (shared_ptr<const ImageProxy> other) const
221 shared_ptr<const FFmpegImageProxy> mp = dynamic_pointer_cast<const FFmpegImageProxy> (other);
226 return _data == mp->_data;
230 FFmpegImageProxy::memory_used () const
232 size_t m = _data.size();
234 m += _image->memory_used();