2 Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
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.
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.
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.
20 /** @file src/image.cc
21 * @brief A set of classes to describe video images.
30 #include <boost/algorithm/string.hpp>
33 #include <libavcodec/avcodec.h>
34 #include <libavformat/avformat.h>
35 #include <libswscale/swscale.h>
36 #include <libavfilter/avfiltergraph.h>
37 #include <libpostproc/postprocess.h>
38 #include <libavutil/pixfmt.h>
41 #include "exceptions.h"
49 using namespace boost;
51 /** @param n Component index.
52 * @return Number of lines in the image for the given component.
55 Image::lines (int n) const
57 switch (_pixel_format) {
62 return size().height / 2;
74 /** @return Number of components */
76 Image::components () const
78 switch (_pixel_format) {
91 /** Write a MD5 hash of the image's data to stdout.
92 * @param n Title to give the output.
95 Image::hash (string n) const
97 MHASH ht = mhash_init (MHASH_MD5);
98 if (ht == MHASH_FAILED) {
99 throw EncodeError ("could not create hash thread");
102 for (int i = 0; i < components(); ++i) {
103 mhash (ht, data()[i], line_size()[i] * lines(i));
107 mhash_deinit (ht, hash);
109 printf ("%s: ", n.c_str ());
110 for (int i = 0; i < int (mhash_get_block_size (MHASH_MD5)); ++i) {
111 printf ("%.2x", hash[i]);
117 /** Scale this image to a given size and convert it to RGB.
118 * @param out_size Output image size in pixels.
119 * @param scaler Scaler to use.
121 shared_ptr<RGBFrameImage>
122 Image::scale_and_convert_to_rgb (Size out_size, int padding, Scaler const * scaler) const
126 Size content_size = out_size;
127 content_size.width -= (padding * 2);
129 shared_ptr<RGBFrameImage> rgb (new RGBFrameImage (content_size));
131 struct SwsContext* scale_context = sws_getContext (
132 size().width, size().height, pixel_format(),
133 content_size.width, content_size.height, PIX_FMT_RGB24,
134 scaler->ffmpeg_id (), 0, 0, 0
137 /* Scale and convert to RGB from whatever its currently in (which may be RGB) */
142 rgb->data (), rgb->line_size ()
145 /* Put the image in the right place in a black frame if are padding; this is
146 a bit grubby and expensive, but probably inconsequential in the great
150 shared_ptr<RGBFrameImage> padded_rgb (new RGBFrameImage (out_size));
151 padded_rgb->make_black ();
153 /* XXX: we are cheating a bit here; we know the frame is RGB so we can
154 make assumptions about its composition.
156 uint8_t* p = padded_rgb->data()[0] + padding * 3;
157 uint8_t* q = rgb->data()[0];
158 for (int j = 0; j < rgb->lines(0); ++j) {
159 memcpy (p, q, rgb->line_size()[0]);
160 p += padded_rgb->line_size()[0];
161 q += rgb->line_size()[0];
167 sws_freeContext (scale_context);
172 /** Run a FFmpeg post-process on this image and return the processed version.
173 * @param pp Flags for the required set of post processes.
174 * @return Post-processed image.
176 shared_ptr<PostProcessImage>
177 Image::post_process (string pp) const
179 shared_ptr<PostProcessImage> out (new PostProcessImage (PIX_FMT_YUV420P, size ()));
181 pp_mode* mode = pp_get_mode_by_name_and_quality (pp.c_str (), PP_QUALITY_MAX);
182 pp_context* context = pp_get_context (size().width, size().height, PP_FORMAT_420 | PP_CPU_CAPS_MMX2);
185 (const uint8_t **) data(), line_size(),
186 out->data(), out->line_size(),
187 size().width, size().height,
188 0, 0, mode, context, 0
192 pp_free_context (context);
200 switch (_pixel_format) {
201 case PIX_FMT_YUV420P:
202 memset (data()[0], 0, lines(0) * line_size()[0]);
203 memset (data()[1], 0x80, lines(1) * line_size()[1]);
204 memset (data()[2], 0x80, lines(2) * line_size()[2]);
208 memset (data()[0], 0, lines(0) * line_size()[0]);
216 /** Construct a SimpleImage of a given size and format, allocating memory
219 * @param p Pixel format.
220 * @param s Size in pixels.
222 SimpleImage::SimpleImage (PixelFormat p, Size s)
226 _data = (uint8_t **) av_malloc (components() * sizeof (uint8_t *));
227 _line_size = (int *) av_malloc (components() * sizeof (int));
229 for (int i = 0; i < components(); ++i) {
235 /** Destroy a SimpleImage */
236 SimpleImage::~SimpleImage ()
238 for (int i = 0; i < components(); ++i) {
243 av_free (_line_size);
246 /** Set the size in bytes of each horizontal line of a given component.
247 * @param i Component index.
248 * @param s Size of line in bytes.
251 SimpleImage::set_line_size (int i, int s)
254 _data[i] = (uint8_t *) av_malloc (s * lines (i));
258 SimpleImage::data () const
264 SimpleImage::line_size () const
270 SimpleImage::size () const
276 FilterBufferImage::FilterBufferImage (PixelFormat p, AVFilterBufferRef* b)
283 FilterBufferImage::~FilterBufferImage ()
285 avfilter_unref_buffer (_buffer);
289 FilterBufferImage::data () const
291 return _buffer->data;
295 FilterBufferImage::line_size () const
297 return _buffer->linesize;
301 FilterBufferImage::size () const
303 return Size (_buffer->video->w, _buffer->video->h);
306 /** XXX: this could be generalised to use any format, but I don't
307 * understand how avpicture_fill is supposed to be called with
308 * multi-planar images.
310 RGBFrameImage::RGBFrameImage (Size s)
311 : Image (PIX_FMT_RGB24)
314 _frame = avcodec_alloc_frame ();
316 throw EncodeError ("could not allocate frame");
319 _data = (uint8_t *) av_malloc (size().width * size().height * 3);
320 avpicture_fill ((AVPicture *) _frame, _data, PIX_FMT_RGB24, size().width, size().height);
321 _frame->width = size().width;
322 _frame->height = size().height;
323 _frame->format = PIX_FMT_RGB24;
326 RGBFrameImage::~RGBFrameImage ()
333 RGBFrameImage::data () const
339 RGBFrameImage::line_size () const
341 return _frame->linesize;
345 RGBFrameImage::size () const
350 PostProcessImage::PostProcessImage (PixelFormat p, Size s)
354 _data = new uint8_t*[4];
355 _line_size = new int[4];
357 for (int i = 0; i < 4; ++i) {
358 _data[i] = (uint8_t *) av_malloc (s.width * s.height);
359 _line_size[i] = s.width;
363 PostProcessImage::~PostProcessImage ()
365 for (int i = 0; i < 4; ++i) {
374 PostProcessImage::data () const
380 PostProcessImage::line_size () const
386 PostProcessImage::size () const