ad2aa79bd0bd6de3cd7e517ce236766a0deb0e49
[dcpomatic.git] / src / lib / image.h
1 /*
2     Copyright (C) 2012 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 /** @file src/image.h
21  *  @brief A set of classes to describe video images.
22  */
23
24 #ifndef DVDOMATIC_IMAGE_H
25 #define DVDOMATIC_IMAGE_H
26
27 #include <string>
28 #include <boost/shared_ptr.hpp>
29 #include <boost/function.hpp>
30 extern "C" {
31 #include <libavcodec/avcodec.h>
32 #include <libavfilter/avfilter.h>
33 }
34 #include "util.h"
35
36 class Scaler;
37 class RGBFrameImage;
38 class SimpleImage;
39
40 /** @class Image
41  *  @brief Parent class for wrappers of some image, in some format, that
42  *  can present a set of components and a size in pixels.
43  *
44  *  This class also has some conversion / processing methods.
45  *
46  *  The main point of this class (and its subclasses) is to abstract
47  *  details of FFmpeg's memory management and varying data formats.
48  */
49 class Image
50 {
51 public:
52         Image (AVPixelFormat p)
53                 : _pixel_format (p)
54         {}
55         
56         virtual ~Image () {}
57
58         /** @return Array of pointers to arrays of the component data */
59         virtual uint8_t ** data () const = 0;
60
61         /** @return Array of sizes of the data in each line, in bytes (without any alignment padding bytes) */
62         virtual int * line_size () const = 0;
63
64         /** @return Array of strides for each line (including any alignment padding bytes) */
65         virtual int * stride () const = 0;
66
67         /** @return Size of the image, in pixels */
68         virtual libdcp::Size size () const = 0;
69
70         virtual bool aligned () const = 0;
71
72         int components () const;
73         int lines (int) const;
74
75         boost::shared_ptr<Image> scale_and_convert_to_rgb (libdcp::Size out_size, int padding, Scaler const * scaler, bool aligned) const;
76         boost::shared_ptr<Image> scale (libdcp::Size, Scaler const *, bool aligned) const;
77         boost::shared_ptr<Image> post_process (std::string, bool aligned) const;
78         void alpha_blend (boost::shared_ptr<const Image> image, Position pos);
79         boost::shared_ptr<Image> crop (Crop c, bool aligned) const;
80         
81         void make_black ();
82
83         void read_from_socket (boost::shared_ptr<Socket>);
84         void write_to_socket (boost::shared_ptr<Socket>) const;
85         
86         AVPixelFormat pixel_format () const {
87                 return _pixel_format;
88         }
89
90 protected:
91         virtual void swap (Image &);
92         float bytes_per_pixel (int) const;
93
94 private:
95         void yuv_16_black (uint16_t);
96         static uint16_t swap_16 (uint16_t);
97         
98         AVPixelFormat _pixel_format; ///< FFmpeg's way of describing the pixel format of this Image
99 };
100
101 /** @class FilterBufferImage
102  *  @brief An Image that is held in an AVFilterBufferRef.
103  */
104 class FilterBufferImage : public Image
105 {
106 public:
107         FilterBufferImage (AVPixelFormat, AVFilterBufferRef *);
108         ~FilterBufferImage ();
109
110         uint8_t ** data () const;
111         int * line_size () const;
112         int * stride () const;
113         libdcp::Size size () const;
114         bool aligned () const;
115
116 private:
117         /* Not allowed */
118         FilterBufferImage (FilterBufferImage const &);
119         FilterBufferImage& operator= (FilterBufferImage const &);
120         
121         AVFilterBufferRef* _buffer;
122         int* _line_size;
123 };
124
125 /** @class SimpleImage
126  *  @brief An Image for which memory is allocated using a `simple' av_malloc().
127  */
128 class SimpleImage : public Image
129 {
130 public:
131         SimpleImage (AVPixelFormat, libdcp::Size, bool);
132         SimpleImage (SimpleImage const &);
133         SimpleImage (boost::shared_ptr<const Image>);
134         SimpleImage& operator= (SimpleImage const &);
135         ~SimpleImage ();
136
137         uint8_t ** data () const;
138         int * line_size () const;
139         int * stride () const;
140         libdcp::Size size () const;
141         bool aligned () const;
142
143 protected:
144         void allocate ();
145         void swap (SimpleImage &);
146         
147 private:
148         libdcp::Size _size; ///< size in pixels
149         uint8_t** _data; ///< array of pointers to components
150         int* _line_size; ///< array of sizes of the data in each line, in pixels (without any alignment padding bytes)
151         int* _stride; ///< array of strides for each line (including any alignment padding bytes)
152         bool _aligned;
153 };
154
155 class RGBPlusAlphaImage : public SimpleImage
156 {
157 public:
158         RGBPlusAlphaImage (boost::shared_ptr<const Image>);
159         ~RGBPlusAlphaImage ();
160
161         uint8_t* alpha () const {
162                 return _alpha;
163         }
164         
165 private:
166         uint8_t* _alpha;
167 };
168
169 #endif