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.
28 #include <boost/algorithm/string.hpp>
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>
39 #include "exceptions.h"
43 using namespace boost;
45 /** @param n Component index.
46 * @return Number of lines in the image for the given component.
49 Image::lines (int n) const
51 switch (_pixel_format) {
56 return size().height / 2;
69 /** @return Number of components */
71 Image::components () const
73 switch (_pixel_format) {
87 Image::scale (Size out_size, Scaler const * scaler) const
91 shared_ptr<SimpleImage> scaled (new SimpleImage (pixel_format(), out_size));
93 struct SwsContext* scale_context = sws_getContext (
94 size().width, size().height, pixel_format(),
95 out_size.width, out_size.height, pixel_format(),
96 scaler->ffmpeg_id (), 0, 0, 0
103 scaled->data (), scaled->line_size ()
106 sws_freeContext (scale_context);
111 /** Scale this image to a given size and convert it to RGB.
112 * @param out_size Output image size in pixels.
113 * @param scaler Scaler to use.
115 shared_ptr<RGBFrameImage>
116 Image::scale_and_convert_to_rgb (Size out_size, int padding, Scaler const * scaler) const
120 Size content_size = out_size;
121 content_size.width -= (padding * 2);
123 shared_ptr<RGBFrameImage> rgb (new RGBFrameImage (content_size));
125 struct SwsContext* scale_context = sws_getContext (
126 size().width, size().height, pixel_format(),
127 content_size.width, content_size.height, PIX_FMT_RGB24,
128 scaler->ffmpeg_id (), 0, 0, 0
131 /* Scale and convert to RGB from whatever its currently in (which may be RGB) */
136 rgb->data (), rgb->line_size ()
139 /* Put the image in the right place in a black frame if are padding; this is
140 a bit grubby and expensive, but probably inconsequential in the great
144 shared_ptr<RGBFrameImage> padded_rgb (new RGBFrameImage (out_size));
145 padded_rgb->make_black ();
147 /* XXX: we are cheating a bit here; we know the frame is RGB so we can
148 make assumptions about its composition.
150 uint8_t* p = padded_rgb->data()[0] + padding * 3;
151 uint8_t* q = rgb->data()[0];
152 for (int j = 0; j < rgb->lines(0); ++j) {
153 memcpy (p, q, rgb->line_size()[0]);
154 p += padded_rgb->line_size()[0];
155 q += rgb->line_size()[0];
161 sws_freeContext (scale_context);
166 /** Run a FFmpeg post-process on this image and return the processed version.
167 * @param pp Flags for the required set of post processes.
168 * @return Post-processed image.
170 shared_ptr<PostProcessImage>
171 Image::post_process (string pp) const
173 shared_ptr<PostProcessImage> out (new PostProcessImage (PIX_FMT_YUV420P, size ()));
175 pp_mode* mode = pp_get_mode_by_name_and_quality (pp.c_str (), PP_QUALITY_MAX);
176 pp_context* context = pp_get_context (size().width, size().height, PP_FORMAT_420 | PP_CPU_CAPS_MMX2);
179 (const uint8_t **) data(), line_size(),
180 out->data(), out->line_size(),
181 size().width, size().height,
182 0, 0, mode, context, 0
186 pp_free_context (context);
194 switch (_pixel_format) {
195 case PIX_FMT_YUV420P:
196 memset (data()[0], 0, lines(0) * line_size()[0]);
197 memset (data()[1], 0x80, lines(1) * line_size()[1]);
198 memset (data()[2], 0x80, lines(2) * line_size()[2]);
202 memset (data()[0], 0, lines(0) * line_size()[0]);
210 /** Construct a SimpleImage of a given size and format, allocating memory
213 * @param p Pixel format.
214 * @param s Size in pixels.
216 SimpleImage::SimpleImage (PixelFormat p, Size s)
220 _data = (uint8_t **) av_malloc (components() * sizeof (uint8_t *));
221 _line_size = (int *) av_malloc (components() * sizeof (int));
225 _line_size[0] = s.width * 3;
228 _line_size[0] = s.width * 4;
230 case PIX_FMT_YUV420P:
231 _line_size[0] = s.width;
232 _line_size[1] = s.width / 2;
233 _line_size[2] = s.width / 2;
239 for (int i = 0; i < components(); ++i) {
240 _data[i] = (uint8_t *) av_malloc (_line_size[i] * lines (i));
244 /** Destroy a SimpleImage */
245 SimpleImage::~SimpleImage ()
247 for (int i = 0; i < components(); ++i) {
252 av_free (_line_size);
256 SimpleImage::data () const
262 SimpleImage::line_size () const
268 SimpleImage::size () const
274 FilterBufferImage::FilterBufferImage (PixelFormat p, AVFilterBufferRef* b)
281 FilterBufferImage::~FilterBufferImage ()
283 avfilter_unref_buffer (_buffer);
287 FilterBufferImage::data () const
289 return _buffer->data;
293 FilterBufferImage::line_size () const
295 return _buffer->linesize;
299 FilterBufferImage::size () const
301 return Size (_buffer->video->w, _buffer->video->h);
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.
308 RGBFrameImage::RGBFrameImage (Size s)
309 : Image (PIX_FMT_RGB24)
312 _frame = avcodec_alloc_frame ();
314 throw EncodeError ("could not allocate frame");
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;
324 RGBFrameImage::~RGBFrameImage ()
331 RGBFrameImage::data () const
337 RGBFrameImage::line_size () const
339 return _frame->linesize;
343 RGBFrameImage::size () const
348 PostProcessImage::PostProcessImage (PixelFormat p, Size s)
352 _data = new uint8_t*[4];
353 _line_size = new int[4];
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;
361 PostProcessImage::~PostProcessImage ()
363 for (int i = 0; i < 4; ++i) {
372 PostProcessImage::data () const
378 PostProcessImage::line_size () const
384 PostProcessImage::size () const