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