Remove FFmpegDecoder minimal_run and care on seeking, as the VideoDecoder/AudioDecode...
[dcpomatic.git] / src / lib / player_video.cc
1 /*
2     Copyright (C) 2013-2014 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include <dcp/raw_convert.h>
21 #include "player_video.h"
22 #include "image.h"
23 #include "image_proxy.h"
24 #include "scaler.h"
25
26 using std::string;
27 using std::cout;
28 using boost::shared_ptr;
29 using dcp::raw_convert;
30
31 PlayerVideo::PlayerVideo (
32         shared_ptr<const ImageProxy> in,
33         DCPTime time,
34         Crop crop,
35         dcp::Size inter_size,
36         dcp::Size out_size,
37         Scaler const * scaler,
38         Eyes eyes,
39         Part part,
40         ColourConversion colour_conversion
41         )
42         : _in (in)
43         , _time (time)
44         , _crop (crop)
45         , _inter_size (inter_size)
46         , _out_size (out_size)
47         , _scaler (scaler)
48         , _eyes (eyes)
49         , _part (part)
50         , _colour_conversion (colour_conversion)
51 {
52
53 }
54
55 PlayerVideo::PlayerVideo (shared_ptr<cxml::Node> node, shared_ptr<Socket> socket, shared_ptr<Log> log)
56 {
57         _time = DCPTime (node->number_child<DCPTime::Type> ("Time"));
58         _crop = Crop (node);
59
60         _inter_size = dcp::Size (node->number_child<int> ("InterWidth"), node->number_child<int> ("InterHeight"));
61         _out_size = dcp::Size (node->number_child<int> ("OutWidth"), node->number_child<int> ("OutHeight"));
62         _scaler = Scaler::from_id (node->string_child ("Scaler"));
63         _eyes = (Eyes) node->number_child<int> ("Eyes");
64         _part = (Part) node->number_child<int> ("Part");
65         _colour_conversion = ColourConversion (node);
66
67         _in = image_proxy_factory (node->node_child ("In"), socket, log);
68
69         if (node->optional_number_child<int> ("SubtitleX")) {
70                 
71                 _subtitle.position = Position<int> (node->number_child<int> ("SubtitleX"), node->number_child<int> ("SubtitleY"));
72
73                 _subtitle.image.reset (
74                         new Image (PIX_FMT_RGBA, dcp::Size (node->number_child<int> ("SubtitleWidth"), node->number_child<int> ("SubtitleHeight")), true)
75                         );
76                 
77                 _subtitle.image->read_from_socket (socket);
78         }
79 }
80
81 void
82 PlayerVideo::set_subtitle (PositionImage image)
83 {
84         _subtitle = image;
85 }
86
87 shared_ptr<Image>
88 PlayerVideo::image () const
89 {
90         shared_ptr<Image> im = _in->image ();
91         
92         Crop total_crop = _crop;
93         switch (_part) {
94         case PART_LEFT_HALF:
95                 total_crop.right += im->size().width / 2;
96                 break;
97         case PART_RIGHT_HALF:
98                 total_crop.left += im->size().width / 2;
99                 break;
100         case PART_TOP_HALF:
101                 total_crop.bottom += im->size().height / 2;
102                 break;
103         case PART_BOTTOM_HALF:
104                 total_crop.top += im->size().height / 2;
105                 break;
106         default:
107                 break;
108         }
109                 
110         shared_ptr<Image> out = im->crop_scale_window (total_crop, _inter_size, _out_size, _scaler, PIX_FMT_RGB24, true);
111
112         Position<int> const container_offset ((_out_size.width - _inter_size.width) / 2, (_out_size.height - _inter_size.width) / 2);
113
114         if (_subtitle.image) {
115                 out->alpha_blend (_subtitle.image, _subtitle.position);
116         }
117
118         return out;
119 }
120
121 void
122 PlayerVideo::add_metadata (xmlpp::Node* node) const
123 {
124         node->add_child("Time")->add_child_text (raw_convert<string> (_time.get ()));
125         _crop.as_xml (node);
126         _in->add_metadata (node->add_child ("In"));
127         node->add_child("InterWidth")->add_child_text (raw_convert<string> (_inter_size.width));
128         node->add_child("InterHeight")->add_child_text (raw_convert<string> (_inter_size.height));
129         node->add_child("OutWidth")->add_child_text (raw_convert<string> (_out_size.width));
130         node->add_child("OutHeight")->add_child_text (raw_convert<string> (_out_size.height));
131         node->add_child("Scaler")->add_child_text (_scaler->id ());
132         node->add_child("Eyes")->add_child_text (raw_convert<string> (_eyes));
133         node->add_child("Part")->add_child_text (raw_convert<string> (_part));
134         _colour_conversion.as_xml (node);
135         if (_subtitle.image) {
136                 node->add_child ("SubtitleWidth")->add_child_text (raw_convert<string> (_subtitle.image->size().width));
137                 node->add_child ("SubtitleHeight")->add_child_text (raw_convert<string> (_subtitle.image->size().height));
138                 node->add_child ("SubtitleX")->add_child_text (raw_convert<string> (_subtitle.position.x));
139                 node->add_child ("SubtitleY")->add_child_text (raw_convert<string> (_subtitle.position.y));
140         }
141 }
142
143 void
144 PlayerVideo::send_binary (shared_ptr<Socket> socket) const
145 {
146         _in->send_binary (socket);
147         if (_subtitle.image) {
148                 _subtitle.image->write_to_socket (socket);
149         }
150 }