Move things round a bit.
[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 extern "C" {
30 #include <libavcodec/avcodec.h>
31 #include <libavfilter/avfilter.h>
32 }
33 #include "util.h"
34
35 class Scaler;
36 class RGBFrameImage;
37 class PostProcessImage;
38
39 /** @class Image
40  *  @brief Parent class for wrappers of some image, in some format, that
41  *  can present a set of components and a size in pixels.
42  *
43  *  This class also has some conversion / processing methods.
44  *
45  *  The main point of this class (and its subclasses) is to abstract
46  *  details of FFmpeg's memory management and varying data formats.
47  */
48 class Image
49 {
50 public:
51         Image (PixelFormat p)
52                 : _pixel_format (p)
53         {}
54         
55         virtual ~Image () {}
56
57         /** @return Array of pointers to arrays of the component data */
58         virtual uint8_t ** data () const = 0;
59
60         /** @return Array of sizes of each line, in pixels */
61         virtual int * line_size () const = 0;
62
63         /** @return Size of the image, in pixels */
64         virtual Size size () const = 0;
65
66         int components () const;
67         int lines (int) const;
68         boost::shared_ptr<RGBFrameImage> scale_and_convert_to_rgb (Size, int, Scaler const *) const;
69         boost::shared_ptr<PostProcessImage> post_process (std::string) const;
70         
71 #ifdef DEBUG_HASH       
72         void hash (std::string) const;
73 #endif
74
75         void make_black ();
76         
77         PixelFormat pixel_format () const {
78                 return _pixel_format;
79         }
80
81 private:
82         PixelFormat _pixel_format; ///< FFmpeg's way of describing the pixel format of this Image
83 };
84
85 /** @class FilterBufferImage
86  *  @brief An Image that is held in an AVFilterBufferRef.
87  */
88 class FilterBufferImage : public Image
89 {
90 public:
91         FilterBufferImage (PixelFormat, AVFilterBufferRef *);
92         ~FilterBufferImage ();
93
94         uint8_t ** data () const;
95         int * line_size () const;
96         Size size () const;
97
98 private:
99         AVFilterBufferRef* _buffer;
100 };
101
102 /** @class SimpleImage
103  *  @brief An Image for which memory is allocated using a `simple' av_malloc().
104  */
105 class SimpleImage : public Image
106 {
107 public:
108         SimpleImage (PixelFormat, Size);
109         ~SimpleImage ();
110
111         uint8_t ** data () const;
112         int * line_size () const;
113         Size size () const;
114         
115         void set_line_size (int, int);
116
117 private:
118         Size _size; ///< size in pixels
119         uint8_t** _data; ///< array of pointers to components
120         int* _line_size; ///< array of widths of each line, in bytes
121 };
122
123 /** @class RGBFrameImage
124  *  @brief An RGB image that is held within an AVFrame.
125  */
126 class RGBFrameImage : public Image
127 {
128 public:
129         RGBFrameImage (Size);
130         ~RGBFrameImage ();
131
132         uint8_t ** data () const;
133         int * line_size () const;
134         Size size () const;
135         AVFrame * frame () const {
136                 return _frame;
137         }
138
139 private:
140         Size _size;
141         AVFrame* _frame;
142         uint8_t* _data;
143 };
144
145 /** @class PostProcessImage
146  *  @brief An image that is the result of an FFmpeg post-processing run.
147  */
148 class PostProcessImage : public Image
149 {
150 public:
151         PostProcessImage (PixelFormat, Size);
152         ~PostProcessImage ();
153
154         uint8_t ** data () const;
155         int * line_size () const;
156         Size size () const;
157
158 private:
159         Size _size;
160         uint8_t** _data;
161         int* _line_size;
162 };
163
164 #endif