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