Merge branch 'master' of /home/carl/git/dvdomatic
[dcpomatic.git] / src / lib / image.cc
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.cc
21  *  @brief A set of classes to describe video images.
22  */
23
24 #include <sstream>
25 #include <iomanip>
26 #include <iostream>
27 #include <sys/time.h>
28 #include <boost/algorithm/string.hpp>
29 #include <openjpeg.h>
30 extern "C" {
31 #include <libavcodec/avcodec.h>
32 #include <libavformat/avformat.h>
33 #include <libswscale/swscale.h>
34 #include <libavfilter/avfiltergraph.h>
35 #include <libpostproc/postprocess.h>
36 #include <libavutil/pixfmt.h>
37 }
38 #include "image.h"
39 #include "exceptions.h"
40 #include "scaler.h"
41
42 using namespace std;
43 using namespace boost;
44
45 /** @param n Component index.
46  *  @return Number of lines in the image for the given component.
47  */
48 int
49 Image::lines (int n) const
50 {
51         switch (_pixel_format) {
52         case PIX_FMT_YUV420P:
53                 if (n == 0) {
54                         return size().height;
55                 } else {
56                         return size().height / 2;
57                 }
58                 break;
59         case PIX_FMT_RGB24:
60                 return size().height;
61         default:
62                 assert (false);
63         }
64
65         return 0;
66 }
67
68 /** @return Number of components */
69 int
70 Image::components () const
71 {
72         switch (_pixel_format) {
73         case PIX_FMT_YUV420P:
74                 return 3;
75         case PIX_FMT_RGB24:
76                 return 1;
77         default:
78                 assert (false);
79         }
80
81         return 0;
82 }
83
84 /** Scale this image to a given size and convert it to RGB.
85  *  @param out_size Output image size in pixels.
86  *  @param scaler Scaler to use.
87  */
88 shared_ptr<RGBFrameImage>
89 Image::scale_and_convert_to_rgb (Size out_size, int padding, Scaler const * scaler) const
90 {
91         assert (scaler);
92
93         Size content_size = out_size;
94         content_size.width -= (padding * 2);
95
96         shared_ptr<RGBFrameImage> rgb (new RGBFrameImage (content_size));
97
98         struct SwsContext* scale_context = sws_getContext (
99                 size().width, size().height, pixel_format(),
100                 content_size.width, content_size.height, PIX_FMT_RGB24,
101                 scaler->ffmpeg_id (), 0, 0, 0
102                 );
103
104         /* Scale and convert to RGB from whatever its currently in (which may be RGB) */
105         sws_scale (
106                 scale_context,
107                 data(), line_size(),
108                 0, size().height,
109                 rgb->data (), rgb->line_size ()
110                 );
111
112         /* Put the image in the right place in a black frame if are padding; this is
113            a bit grubby and expensive, but probably inconsequential in the great
114            scheme of things.
115         */
116         if (padding > 0) {
117                 shared_ptr<RGBFrameImage> padded_rgb (new RGBFrameImage (out_size));
118                 padded_rgb->make_black ();
119
120                 /* XXX: we are cheating a bit here; we know the frame is RGB so we can
121                    make assumptions about its composition.
122                 */
123                 uint8_t* p = padded_rgb->data()[0] + padding * 3;
124                 uint8_t* q = rgb->data()[0];
125                 for (int j = 0; j < rgb->lines(0); ++j) {
126                         memcpy (p, q, rgb->line_size()[0]);
127                         p += padded_rgb->line_size()[0];
128                         q += rgb->line_size()[0];
129                 }
130
131                 rgb = padded_rgb;
132         }
133
134         sws_freeContext (scale_context);
135
136         return rgb;
137 }
138
139 /** Run a FFmpeg post-process on this image and return the processed version.
140  *  @param pp Flags for the required set of post processes.
141  *  @return Post-processed image.
142  */
143 shared_ptr<PostProcessImage>
144 Image::post_process (string pp) const
145 {
146         shared_ptr<PostProcessImage> out (new PostProcessImage (PIX_FMT_YUV420P, size ()));
147         
148         pp_mode* mode = pp_get_mode_by_name_and_quality (pp.c_str (), PP_QUALITY_MAX);
149         pp_context* context = pp_get_context (size().width, size().height, PP_FORMAT_420 | PP_CPU_CAPS_MMX2);
150
151         pp_postprocess (
152                 (const uint8_t **) data(), line_size(),
153                 out->data(), out->line_size(),
154                 size().width, size().height,
155                 0, 0, mode, context, 0
156                 );
157                 
158         pp_free_mode (mode);
159         pp_free_context (context);
160
161         return out;
162 }
163
164 void
165 Image::make_black ()
166 {
167         switch (_pixel_format) {
168         case PIX_FMT_YUV420P:
169                 memset (data()[0], 0, lines(0) * line_size()[0]);
170                 memset (data()[1], 0x80, lines(1) * line_size()[1]);
171                 memset (data()[2], 0x80, lines(2) * line_size()[2]);
172                 break;
173
174         case PIX_FMT_RGB24:             
175                 memset (data()[0], 0, lines(0) * line_size()[0]);
176                 break;
177
178         default:
179                 assert (false);
180         }
181 }
182
183 /** Construct a SimpleImage of a given size and format, allocating memory
184  *  as required.
185  *
186  *  @param p Pixel format.
187  *  @param s Size in pixels.
188  */
189 SimpleImage::SimpleImage (PixelFormat p, Size s)
190         : Image (p)
191         , _size (s)
192 {
193         _data = (uint8_t **) av_malloc (components() * sizeof (uint8_t *));
194         _line_size = (int *) av_malloc (components() * sizeof (int));
195         
196         for (int i = 0; i < components(); ++i) {
197                 _data[i] = 0;
198                 _line_size[i] = 0;
199         }
200 }
201
202 /** Destroy a SimpleImage */
203 SimpleImage::~SimpleImage ()
204 {
205         for (int i = 0; i < components(); ++i) {
206                 av_free (_data[i]);
207         }
208
209         av_free (_data);
210         av_free (_line_size);
211 }
212
213 /** Set the size in bytes of each horizontal line of a given component.
214  *  @param i Component index.
215  *  @param s Size of line in bytes.
216  */
217 void
218 SimpleImage::set_line_size (int i, int s)
219 {
220         _line_size[i] = s;
221         _data[i] = (uint8_t *) av_malloc (s * lines (i));
222 }
223
224 uint8_t **
225 SimpleImage::data () const
226 {
227         return _data;
228 }
229
230 int *
231 SimpleImage::line_size () const
232 {
233         return _line_size;
234 }
235
236 Size
237 SimpleImage::size () const
238 {
239         return _size;
240 }
241
242
243 FilterBufferImage::FilterBufferImage (PixelFormat p, AVFilterBufferRef* b)
244         : Image (p)
245         , _buffer (b)
246 {
247
248 }
249
250 FilterBufferImage::~FilterBufferImage ()
251 {
252         avfilter_unref_buffer (_buffer);
253 }
254
255 uint8_t **
256 FilterBufferImage::data () const
257 {
258         return _buffer->data;
259 }
260
261 int *
262 FilterBufferImage::line_size () const
263 {
264         return _buffer->linesize;
265 }
266
267 Size
268 FilterBufferImage::size () const
269 {
270         return Size (_buffer->video->w, _buffer->video->h);
271 }
272
273 /** XXX: this could be generalised to use any format, but I don't
274  *  understand how avpicture_fill is supposed to be called with
275  *  multi-planar images.
276  */
277 RGBFrameImage::RGBFrameImage (Size s)
278         : Image (PIX_FMT_RGB24)
279         , _size (s)
280 {
281         _frame = avcodec_alloc_frame ();
282         if (_frame == 0) {
283                 throw EncodeError ("could not allocate frame");
284         }
285
286         _data = (uint8_t *) av_malloc (size().width * size().height * 3);
287         avpicture_fill ((AVPicture *) _frame, _data, PIX_FMT_RGB24, size().width, size().height);
288         _frame->width = size().width;
289         _frame->height = size().height;
290         _frame->format = PIX_FMT_RGB24;
291 }
292
293 RGBFrameImage::~RGBFrameImage ()
294 {
295         av_free (_data);
296         av_free (_frame);
297 }
298
299 uint8_t **
300 RGBFrameImage::data () const
301 {
302         return _frame->data;
303 }
304
305 int *
306 RGBFrameImage::line_size () const
307 {
308         return _frame->linesize;
309 }
310
311 Size
312 RGBFrameImage::size () const
313 {
314         return _size;
315 }
316
317 PostProcessImage::PostProcessImage (PixelFormat p, Size s)
318         : Image (p)
319         , _size (s)
320 {
321         _data = new uint8_t*[4];
322         _line_size = new int[4];
323         
324         for (int i = 0; i < 4; ++i) {
325                 _data[i] = (uint8_t *) av_malloc (s.width * s.height);
326                 _line_size[i] = s.width;
327         }
328 }
329
330 PostProcessImage::~PostProcessImage ()
331 {
332         for (int i = 0; i < 4; ++i) {
333                 av_free (_data[i]);
334         }
335         
336         delete[] _data;
337         delete[] _line_size;
338 }
339
340 uint8_t **
341 PostProcessImage::data () const
342 {
343         return _data;
344 }
345
346 int *
347 PostProcessImage::line_size () const
348 {
349         return _line_size;
350 }
351
352 Size
353 PostProcessImage::size () const
354 {
355         return _size;
356 }