AvTranscoder  0.9.4
C++APIforLibav/FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
VideoTransform.cpp
Go to the documentation of this file.
1 #include "VideoTransform.hpp"
2 
4 
5 extern "C" {
6 #include <libavcodec/avcodec.h>
7 #include <libswscale/swscale.h>
8 #include <libavutil/imgutils.h>
9 #include <libavutil/pixdesc.h>
10 #if LIBAVCODEC_VERSION_MAJOR > 54
11 #include <libavutil/frame.h>
12 #endif
13 }
14 
15 #include <sstream>
16 #include <iomanip>
17 #include <cassert>
18 #include <stdexcept>
19 
20 namespace avtranscoder
21 {
22 
24  : _imageConvertContext(NULL)
25  , _isInit(false)
26 {
27 }
28 
30 {
31  sws_freeContext(_imageConvertContext);
32 }
33 
34 bool VideoTransform::init(const Frame& srcFrame, const Frame& dstFrame)
35 {
36  // Set convert context
37  const VideoFrame& src = static_cast<const VideoFrame&>(srcFrame);
38  const VideoFrame& dst = static_cast<const VideoFrame&>(dstFrame);
39 
40  const AVPixelFormat srcPixelFormat = src.getPixelFormat();
41  const AVPixelFormat dstPixelFormat = dst.getPixelFormat();
42 
44  sws_getCachedContext(_imageConvertContext, src.getWidth(), src.getHeight(), srcPixelFormat, dst.getWidth(),
45  dst.getHeight(), dstPixelFormat, SWS_POINT, NULL, NULL, NULL);
46 
48  {
49  throw std::runtime_error("unable to create color convert context");
50  }
51 
52  const char* srcPixFmt;
53  srcPixFmt = av_get_pix_fmt_name(srcPixelFormat);
54  const char* dstPixFmt;
55  dstPixFmt = av_get_pix_fmt_name(dstPixelFormat);
56 
57  std::stringstream msg;
58  msg << "Video conversion from " << (srcPixFmt != NULL ? srcPixFmt : "None") << " to "
59  << (dstPixFmt != NULL ? dstPixFmt : "None") << std::endl;
60  msg << "Source, width = " << src.getWidth() << std::endl;
61  msg << "Source, height = " << src.getHeight() << std::endl;
62  msg << "Destination, width = " << dst.getWidth() << std::endl;
63  msg << "Destination, height = " << dst.getHeight() << std::endl;
64  LOG_INFO(msg.str())
65 
66  return true;
67 }
68 
69 void VideoTransform::convert(const Frame& srcFrame, Frame& dstFrame)
70 {
71  const VideoFrame& src = static_cast<const VideoFrame&>(srcFrame);
72  VideoFrame& dst = static_cast<VideoFrame&>(dstFrame);
73 
74  assert(src.getWidth() != 0);
75  assert(src.getHeight() != 0);
76  assert(src.getPixelFormat() != AV_PIX_FMT_NONE);
77 
78  if(!_isInit)
79  _isInit = init(srcFrame, dstFrame);
80 
82  {
83  throw std::runtime_error("unknown color convert context");
84  }
85 
86  // Convert
87  const int ret = sws_scale(_imageConvertContext, src.getData(), src.getLineSize(), 0, src.getHeight(), dst.getData(),
88  dst.getLineSize());
89 
90  if(ret != (int)dst.getHeight())
91  throw std::runtime_error("error in color converter");
92 }
93 }
This class describes decoded video data.
Definition: VideoFrame.hpp:43
#define LOG_INFO(...)
Definition: log.hpp:23
#define AV_PIX_FMT_NONE
Definition: common.hpp:44
AVPixelFormat getPixelFormat() const
Definition: VideoFrame.hpp:51
unsigned char ** getData()
Get all the data of the frame.
Definition: Frame.hpp:35
size_t getHeight() const
Definition: VideoFrame.hpp:50
This class describes decoded (raw) audio or video data.
Definition: Frame.hpp:16
int * getLineSize() const
Returns the size in byte. For video, size in bytes of each picture line. For audio, size in bytes of each plane.
Definition: Frame.hpp:44
size_t getWidth() const
Definition: VideoFrame.hpp:49
#define AVPixelFormat
Definition: common.hpp:43
bool init(const Frame &srcFrame, const Frame &dstFrame)
void convert(const Frame &srcFrame, Frame &dstFrame)