2 Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
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.
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.
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.
21 * @brief A set of classes to describe video images.
24 #ifndef DVDOMATIC_IMAGE_H
25 #define DVDOMATIC_IMAGE_H
28 #include <boost/shared_ptr.hpp>
29 #include <boost/function.hpp>
31 #include <libavcodec/avcodec.h>
32 #include <libavfilter/avfilter.h>
35 #include "ffmpeg_compatibility.h"
42 * @brief Parent class for wrappers of some image, in some format, that
43 * can present a set of components and a size in pixels.
45 * This class also has some conversion / processing methods.
47 * The main point of this class (and its subclasses) is to abstract
48 * details of FFmpeg's memory management and varying data formats.
53 Image (AVPixelFormat p)
59 /** @return Array of pointers to arrays of the component data */
60 virtual uint8_t ** data () const = 0;
62 /** @return Array of sizes of the data in each line, in bytes (without any alignment padding bytes) */
63 virtual int * line_size () const = 0;
65 /** @return Array of strides for each line (including any alignment padding bytes) */
66 virtual int * stride () const = 0;
68 /** @return libdcp::Size of the image, in pixels */
69 virtual libdcp::Size size () const = 0;
71 int components () const;
72 int lines (int) const;
74 boost::shared_ptr<Image> scale_and_convert_to_rgb (libdcp::Size out_size, int padding, Scaler const * scaler, bool aligned) const;
75 boost::shared_ptr<Image> scale (libdcp::Size, Scaler const *, bool aligned) const;
76 boost::shared_ptr<Image> post_process (std::string, bool aligned) const;
77 void alpha_blend (boost::shared_ptr<const Image> image, Position pos);
78 boost::shared_ptr<Image> crop (Crop c, bool aligned) const;
82 void read_from_socket (boost::shared_ptr<Socket>);
83 void write_to_socket (boost::shared_ptr<Socket>) const;
85 AVPixelFormat pixel_format () const {
90 virtual void swap (Image &);
91 float bytes_per_pixel (int) const;
94 AVPixelFormat _pixel_format; ///< FFmpeg's way of describing the pixel format of this Image
97 /** @class FilterBufferImage
98 * @brief An Image that is held in an AVFilterBufferRef.
100 class FilterBufferImage : public Image
103 FilterBufferImage (AVPixelFormat, AVFilterBufferRef *);
104 ~FilterBufferImage ();
106 uint8_t ** data () const;
107 int * line_size () const;
108 int * stride () const;
109 libdcp::Size size () const;
113 FilterBufferImage (FilterBufferImage const &);
114 FilterBufferImage& operator= (FilterBufferImage const &);
116 AVFilterBufferRef* _buffer;
119 /** @class SimpleImage
120 * @brief An Image for which memory is allocated using a `simple' av_malloc().
122 class SimpleImage : public Image
125 SimpleImage (AVPixelFormat, libdcp::Size, bool);
126 SimpleImage (SimpleImage const &);
127 SimpleImage& operator= (SimpleImage const &);
130 uint8_t ** data () const;
131 int * line_size () const;
132 int * stride () const;
133 libdcp::Size size () const;
137 void swap (SimpleImage &);
140 libdcp::Size _size; ///< size in pixels
141 uint8_t** _data; ///< array of pointers to components
142 int* _line_size; ///< array of sizes of the data in each line, in pixels (without any alignment padding bytes)
143 int* _stride; ///< array of strides for each line (including any alignment padding bytes)
147 class RGBPlusAlphaImage : public SimpleImage
150 RGBPlusAlphaImage (boost::shared_ptr<const Image>);
151 ~RGBPlusAlphaImage ();
153 uint8_t* alpha () const {