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