AvTranscoder  0.9.4
C++APIforLibav/FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
VideoFrame.cpp
Go to the documentation of this file.
1 #include "VideoFrame.hpp"
2 
3 #include <AvTranscoder/util.hpp>
4 
5 extern "C" {
6 #include <libavcodec/avcodec.h>
7 #include <libavutil/pixdesc.h>
8 }
9 
10 #include <stdexcept>
11 #include <stdlib.h>
12 
13 namespace avtranscoder
14 {
15 
16 VideoFrameDesc::VideoFrameDesc(const size_t width, const size_t height, const AVPixelFormat pixelFormat)
17  : _width(width)
18  , _height(height)
19  , _pixelFormat(pixelFormat)
20  , _fps(1.0)
21 {
22 }
23 
24 VideoFrameDesc::VideoFrameDesc(const size_t width, const size_t height, const std::string& pixelFormatName)
25  : _width(width)
26  , _height(height)
27  , _pixelFormat(getAVPixelFormat(pixelFormatName))
28  , _fps(1.0)
29 {
30 }
31 
33 {
34  // width
35  if(profile.count(constants::avProfileWidth))
36  _width = atoi(profile.find(constants::avProfileWidth)->second.c_str());
37  // height
38  if(profile.count(constants::avProfileHeight))
39  _height = atoi(profile.find(constants::avProfileHeight)->second.c_str());
40  // pixel format
41  if(profile.count(constants::avProfilePixelFormat))
43  // fps
44  if(profile.count(constants::avProfileFrameRate))
45  _fps = atof(profile.find(constants::avProfileFrameRate)->second.c_str());
46 }
47 
49  : Frame()
50 {
51  allocateAVPicture(ref);
52 }
53 
54 VideoFrame::VideoFrame(const Frame& otherFrame)
55  : Frame(otherFrame)
56 {
57 }
58 
59 size_t VideoFrame::getSize() const
60 {
62  {
63  LOG_WARN("Incorrect pixel format when get size of video frame: return a size of 0.")
64  return 0;
65  }
66 
67  const size_t size = avpicture_get_size(getPixelFormat(), getWidth(), getHeight());
68  if(size == 0)
69  throw std::runtime_error("unable to determine image buffer size");
70  return size;
71 }
72 
74 {
75  const int ret = avpicture_alloc(reinterpret_cast<AVPicture*>(_frame), desc._pixelFormat, desc._width, desc._height);
76  if(ret < 0)
77  {
78  std::stringstream os;
79  os << "Unable to allocate an image frame of ";
80  os << "width = " << desc._width << ", ";
81  os << "height = " << desc._height << ", ";
82  os << "pixel format = " << desc._pixelFormat;
83  throw std::runtime_error(os.str());
84  }
85  _frame->width = desc._width;
86  _frame->height = desc._height;
87  _frame->format = desc._pixelFormat;
88 }
89 
90 void VideoFrame::assign(const unsigned char value)
91 {
92  // Create the image buffer
93  // The buffer will be freed in destructor of based class
94  const int imageSize = getSize();
95  unsigned char* imageBuffer = new unsigned char[imageSize];
96  memset(imageBuffer, value, imageSize);
97 
98  // Fill the picture
99  assign(imageBuffer);
100 }
101 
102 void VideoFrame::assign(const unsigned char* ptrValue)
103 {
104  const int ret =
105  avpicture_fill(reinterpret_cast<AVPicture*>(_frame), ptrValue, getPixelFormat(), getWidth(), getHeight());
106  if(ret < 0)
107  {
108  std::stringstream os;
109  os << "Unable to assign an image buffer of " << getSize() << " bytes: " << getDescriptionFromErrorCode(ret);
110  throw std::runtime_error(os.str());
111  }
112 }
113 }
Description to create a video frame.
Definition: VideoFrame.hpp:21
VideoFrame(const VideoFrameDesc &ref)
Definition: VideoFrame.cpp:48
const std::string avProfileFrameRate
std::string getDescriptionFromErrorCode(const int code)
Get the string description corresponding to the error code provided by ffmpeg/libav.
Definition: common.cpp:22
std::map< std::string, std::string > Profile
AVPixelFormat getAVPixelFormat(const std::string &pixelFormat)
Get the corresponding AVPixelFormat from the pixel format name.
Definition: util.cpp:90
const std::string avProfilePixelFormat
#define AV_PIX_FMT_NONE
Definition: common.hpp:44
AVPixelFormat getPixelFormat() const
Definition: VideoFrame.hpp:51
void assign(const unsigned char value)
Assign the given value to all the data of the picture.
Definition: VideoFrame.cpp:90
VideoFrameDesc(const size_t width=0, const size_t height=0, const AVPixelFormat pixelFormat=AV_PIX_FMT_NONE)
Definition: VideoFrame.cpp:16
size_t getSize() const
in bytes/**
Definition: VideoFrame.cpp:59
#define LOG_WARN(...)
Definition: log.hpp:29
AVFrame * _frame
Definition: Frame.hpp:97
size_t getHeight() const
Definition: VideoFrame.hpp:50
void allocateAVPicture(const VideoFrameDesc &desc)
Allocate the image buffer of the frame.
Definition: VideoFrame.cpp:73
This class describes decoded (raw) audio or video data.
Definition: Frame.hpp:16
const std::string avProfileWidth
const std::string avProfileHeight
size_t getWidth() const
Definition: VideoFrame.hpp:49
void setParameters(const ProfileLoader::Profile &profile)
Set the attributes from the given profile.
Definition: VideoFrame.cpp:32
#define AVPixelFormat
Definition: common.hpp:43