summaryrefslogtreecommitdiff
path: root/src/lib/image.h
blob: b2b9872792a3815de37a86507203419279edb1ff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
    Copyright (C) 2012 Carl Hetherington <cth@carlh.net>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

*/

/** @file src/image.h
 *  @brief A set of classes to describe video images.
 */

#ifndef DVDOMATIC_IMAGE_H
#define DVDOMATIC_IMAGE_H

#include <string>
#include <boost/shared_ptr.hpp>
#include <boost/function.hpp>
extern "C" {
#include <libavcodec/avcodec.h>
#include <libavfilter/avfilter.h>
}
#include "util.h"

class Scaler;
class RGBFrameImage;
class SimpleImage;

/** @class Image
 *  @brief Parent class for wrappers of some image, in some format, that
 *  can present a set of components and a size in pixels.
 *
 *  This class also has some conversion / processing methods.
 *
 *  The main point of this class (and its subclasses) is to abstract
 *  details of FFmpeg's memory management and varying data formats.
 */
class Image
{
public:
	Image (AVPixelFormat p)
		: _pixel_format (p)
	{}
	
	virtual ~Image () {}

	/** @return Array of pointers to arrays of the component data */
	virtual uint8_t ** data () const = 0;

	/** @return Array of sizes of the data in each line, in bytes (without any alignment padding bytes) */
	virtual int * line_size () const = 0;

	/** @return Array of strides for each line (including any alignment padding bytes) */
	virtual int * stride () const = 0;

	/** @return Size of the image, in pixels */
	virtual Size size () const = 0;

	int components () const;
	int lines (int) const;
	boost::shared_ptr<Image> scale_and_convert_to_rgb (Size, int, Scaler const *) const;
	boost::shared_ptr<Image> scale (Size, Scaler const *) const;
	boost::shared_ptr<Image> post_process (std::string) const;
	void alpha_blend (boost::shared_ptr<Image> image, Position pos);
	
	void make_black ();

	void read_from_socket (boost::shared_ptr<Socket>);
	void write_to_socket (boost::shared_ptr<Socket>) const;
	
	AVPixelFormat pixel_format () const {
		return _pixel_format;
	}

private:
	AVPixelFormat _pixel_format; ///< FFmpeg's way of describing the pixel format of this Image
};

/** @class FilterBufferImage
 *  @brief An Image that is held in an AVFilterBufferRef.
 */
class FilterBufferImage : public Image
{
public:
	FilterBufferImage (AVPixelFormat, AVFilterBufferRef *);
	~FilterBufferImage ();

	uint8_t ** data () const;
	int * line_size () const;
	int * stride () const;
	Size size () const;

private:
	AVFilterBufferRef* _buffer;
};

/** @class SimpleImage
 *  @brief An Image for which memory is allocated using a `simple' av_malloc().
 */
class SimpleImage : public Image
{
public:
	SimpleImage (AVPixelFormat, Size, boost::function<int (int)> rounder);
	~SimpleImage ();

	uint8_t ** data () const;
	int * line_size () const;
	int * stride () const;
	Size size () const;
	
private:
	
	Size _size; ///< size in pixels
	uint8_t** _data; ///< array of pointers to components
	int* _line_size; ///< array of sizes of the data in each line, in pixels (without any alignment padding bytes)
	int* _stride; ///< array of strides for each line (including any alignment padding bytes)
};

class AlignedImage : public SimpleImage
{
public:
	AlignedImage (AVPixelFormat, Size);
};

class CompactImage : public SimpleImage
{
public:
	CompactImage (AVPixelFormat, Size);
	CompactImage (boost::shared_ptr<Image>);
};

/** @class RGBFrameImage
 *  @brief An RGB image that is held within an AVFrame.
 */
class RGBFrameImage : public Image
{
public:
	RGBFrameImage (Size);
	~RGBFrameImage ();

	uint8_t ** data () const;
	int * line_size () const;
	int * stride () const;
	Size size () const;
	AVFrame * frame () const {
		return _frame;
	}

private:
	Size _size;
	AVFrame* _frame;
	uint8_t* _data;
};

#endif