Make DCPVideoFrame use PlayerVideoFrame to store its image.
[dcpomatic.git] / src / lib / player_video_frame.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 <libdcp/raw_convert.h>
21 #include "player_video_frame.h"
22 #include "image.h"
23 #include "scaler.h"
24
25 using std::string;
26 using std::cout;
27 using boost::shared_ptr;
28 using libdcp::raw_convert;
29
30 PlayerVideoFrame::PlayerVideoFrame (
31         shared_ptr<const Image> in,
32         Crop crop,
33         libdcp::Size inter_size,
34         libdcp::Size out_size,
35         Scaler const * scaler,
36         Eyes eyes,
37         ColourConversion colour_conversion
38         )
39         : _in (in)
40         , _crop (crop)
41         , _inter_size (inter_size)
42         , _out_size (out_size)
43         , _scaler (scaler)
44         , _eyes (eyes)
45         , _colour_conversion (colour_conversion)
46 {
47
48 }
49
50 PlayerVideoFrame::PlayerVideoFrame (shared_ptr<cxml::Node> node, shared_ptr<Socket> socket)
51 {
52         _crop = Crop (node);
53
54         _inter_size = libdcp::Size (node->number_child<int> ("InterWidth"), node->number_child<int> ("InterHeight"));
55         _out_size = libdcp::Size (node->number_child<int> ("OutWidth"), node->number_child<int> ("OutHeight"));
56         _scaler = Scaler::from_id (node->string_child ("Scaler"));
57         _eyes = (Eyes) node->number_child<int> ("Eyes");
58         _colour_conversion = ColourConversion (node);
59
60         shared_ptr<Image> image (new Image (PIX_FMT_RGB24, libdcp::Size (node->number_child<int> ("InWidth"), node->number_child<int> ("InHeight")), true));
61         image->read_from_socket (socket);
62         _in = image;
63
64         if (node->optional_number_child<int> ("SubtitleX")) {
65                 
66                 _subtitle_position = Position<int> (node->number_child<int> ("SubtitleX"), node->number_child<int> ("SubtitleY"));
67
68                 shared_ptr<Image> image (
69                         new Image (PIX_FMT_RGBA, libdcp::Size (node->number_child<int> ("SubtitleWidth"), node->number_child<int> ("SubtitleHeight")), true)
70                         );
71                 
72                 image->read_from_socket (socket);
73                 _subtitle_image = image;
74         }
75 }
76
77 void
78 PlayerVideoFrame::set_subtitle (shared_ptr<const Image> image, Position<int> pos)
79 {
80         _subtitle_image = image;
81         _subtitle_position = pos;
82 }
83
84 shared_ptr<Image>
85 PlayerVideoFrame::image () const
86 {
87         shared_ptr<Image> out = _in->crop_scale_window (_crop, _inter_size, _out_size, _scaler, PIX_FMT_RGB24, false);
88
89         Position<int> const container_offset ((_out_size.width - _inter_size.width) / 2, (_out_size.height - _inter_size.width) / 2);
90
91         if (_subtitle_image) {
92                 out->alpha_blend (_subtitle_image, _subtitle_position);
93         }
94
95         return out;
96 }
97
98 void
99 PlayerVideoFrame::add_metadata (xmlpp::Element* node) const
100 {
101         _crop.as_xml (node);
102         node->add_child("InWidth")->add_child_text (raw_convert<string> (_in->size().width));
103         node->add_child("InHeight")->add_child_text (raw_convert<string> (_in->size().height));
104         node->add_child("InterWidth")->add_child_text (raw_convert<string> (_inter_size.width));
105         node->add_child("InterHeight")->add_child_text (raw_convert<string> (_inter_size.height));
106         node->add_child("OutWidth")->add_child_text (raw_convert<string> (_out_size.width));
107         node->add_child("OutHeight")->add_child_text (raw_convert<string> (_out_size.height));
108         node->add_child("Scaler")->add_child_text (_scaler->id ());
109         node->add_child("Eyes")->add_child_text (raw_convert<string> (_eyes));
110         _colour_conversion.as_xml (node);
111         if (_subtitle_image) {
112                 node->add_child ("SubtitleWidth")->add_child_text (raw_convert<string> (_subtitle_image->size().width));
113                 node->add_child ("SubtitleHeight")->add_child_text (raw_convert<string> (_subtitle_image->size().height));
114                 node->add_child ("SubtitleX")->add_child_text (raw_convert<string> (_subtitle_position.x));
115                 node->add_child ("SubtitleY")->add_child_text (raw_convert<string> (_subtitle_position.y));
116         }
117 }
118
119 void
120 PlayerVideoFrame::send_binary (shared_ptr<Socket> socket) const
121 {
122         _in->write_to_socket (socket);
123         if (_subtitle_image) {
124                 _subtitle_image->write_to_socket (socket);
125         }
126 }