AvTranscoder  0.9.4
C++APIforLibav/FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
PixelProperties.cpp
Go to the documentation of this file.
1 #include "PixelProperties.hpp"
2 
3 #include <stdexcept>
4 #include <cstring>
5 
6 namespace avtranscoder
7 {
8 
9 PixelProperties::PixelProperties(const std::string& avPixelFormat)
10  : _pixelFormat(AV_PIX_FMT_NONE)
11  , _pixelDesc(NULL)
12 {
13  init(av_get_pix_fmt(avPixelFormat.c_str()));
14 }
15 
17  : _pixelFormat(AV_PIX_FMT_NONE)
18  , _pixelDesc(NULL)
19 {
20  init(avPixelFormat);
21 }
22 
23 void PixelProperties::init(const AVPixelFormat avPixelFormat)
24 {
25  _pixelFormat = avPixelFormat;
26  _pixelDesc = av_pix_fmt_desc_get(avPixelFormat);
27 }
28 
29 std::string PixelProperties::getPixelName() const
30 {
31  if(!_pixelDesc)
32  throw std::runtime_error("unable to find pixel description.");
33 
34  if(!_pixelDesc || !_pixelDesc->name)
35  throw std::runtime_error("unknown pixel name");
36 
37  return std::string(_pixelDesc->name);
38 }
39 
41 {
42  if(!_pixelFormat)
43  throw std::runtime_error("unable to find pixel format.");
44 
45  const char* formatName = av_get_pix_fmt_name(_pixelFormat);
46  if(!formatName)
47  throw std::runtime_error("unknown pixel format");
48 
49  return std::string(formatName);
50 }
51 
53 {
54  if(!_pixelDesc)
55  throw std::runtime_error("unable to find pixel description.");
56  return av_get_bits_per_pixel(_pixelDesc);
57 }
58 
60 {
61  if(!_pixelDesc)
62  throw std::runtime_error("unable to find pixel description.");
63 
64  size_t maxNbBitsInChannels = 0;
65  for(unsigned int channelIndex = 0; channelIndex < _pixelDesc->nb_components; ++channelIndex)
66  {
67  const size_t nbBits = _pixelDesc->comp[channelIndex].depth_minus1 + 1;
68  if(nbBits > maxNbBitsInChannels)
69  maxNbBitsInChannels = nbBits;
70  }
71  return maxNbBitsInChannels;
72 }
73 
75 {
76  if(!_pixelDesc)
77  throw std::runtime_error("unable to find pixel description.");
78  return _pixelDesc->nb_components;
79 }
80 
82 {
83  if(!_pixelDesc)
84  throw std::runtime_error("unable to find pixel description.");
85  return _pixelDesc->log2_chroma_w;
86 }
87 
89 {
90  if(!_pixelDesc)
91  throw std::runtime_error("unable to find pixel description.");
92  return _pixelDesc->log2_chroma_h;
93 }
94 
96 {
97  if(!_pixelDesc)
98  throw std::runtime_error("unable to find pixel description.");
99 
100  if(_pixelDesc->nb_components == 1 || _pixelDesc->nb_components == 2)
101  {
102  return eComponentGray;
103  }
104  else if(_pixelDesc->flags & PIX_FMT_PAL || _pixelDesc->flags & PIX_FMT_RGB)
105  {
106  return eComponentRgb;
107  }
108  else if(_pixelDesc->name && !strncmp(_pixelDesc->name, "yuvj", 4))
109  {
110  return eComponentYuvJPEG;
111  }
112  else if(_pixelDesc->name && !strncmp(_pixelDesc->name, "yuva", 4))
113  {
114  return eComponentYuvA;
115  }
116  else
117  {
118  return eComponentYuv;
119  }
120 }
121 
123 {
124  if(!_pixelDesc)
125  throw std::runtime_error("unable to find pixel description.");
126 
127  if((_pixelDesc->log2_chroma_w == 0) && (_pixelDesc->log2_chroma_h == 1))
128  {
129  return eSubsampling440;
130  }
131  else if((_pixelDesc->log2_chroma_w == 1) && (_pixelDesc->log2_chroma_h == 0))
132  {
133  return eSubsampling422;
134  }
135  else if((_pixelDesc->log2_chroma_w == 1) && (_pixelDesc->log2_chroma_h == 1))
136  {
137  return eSubsampling420;
138  }
139  else if((_pixelDesc->log2_chroma_w == 2) && (_pixelDesc->log2_chroma_h == 0))
140  {
141  return eSubsampling411;
142  }
143  else if((_pixelDesc->log2_chroma_w == 2) && (_pixelDesc->log2_chroma_h == 2))
144  {
145  return eSubsampling410;
146  }
147  else
148  {
149  return eSubsamplingNone;
150  }
151 }
152 
154 {
155  if(!_pixelDesc)
156  throw std::runtime_error("unable to find pixel description.");
157  return (_pixelDesc->flags & PIX_FMT_BE) == PIX_FMT_BE;
158 }
159 
161 {
162  if(!_pixelDesc)
163  throw std::runtime_error("unable to find pixel description.");
164 
165 #if LIBAVCODEC_VERSION_MAJOR > 53
166  return (_pixelDesc->flags & PIX_FMT_ALPHA) == PIX_FMT_ALPHA;
167 #else
168  return false;
169 #endif
170 }
171 
173 {
174  if(!_pixelDesc)
175  throw std::runtime_error("unable to find pixel description.");
176  return (_pixelDesc->flags & PIX_FMT_PLANAR) == PIX_FMT_PLANAR;
177 }
178 
180 {
181  if(!_pixelDesc)
182  throw std::runtime_error("unable to find pixel description.");
183  return (_pixelDesc->flags & PIX_FMT_PAL) == PIX_FMT_PAL;
184 }
185 
187 {
188  if(!_pixelDesc)
189  throw std::runtime_error("unable to find pixel description.");
190  return (_pixelDesc->flags & PIX_FMT_BITSTREAM) == PIX_FMT_BITSTREAM;
191 }
192 
194 {
195  if(!_pixelDesc)
196  throw std::runtime_error("unable to find pixel description.");
197  return (_pixelDesc->flags & PIX_FMT_HWACCEL) == PIX_FMT_HWACCEL;
198 }
199 
201 {
202  if(!_pixelDesc)
203  throw std::runtime_error("unable to find pixel description.");
204  return (_pixelDesc->flags & PIX_FMT_RGB) == PIX_FMT_RGB;
205 }
206 
208 {
209  if(!_pixelDesc)
210  throw std::runtime_error("unable to find pixel description.");
211 
212 #if LIBAVCODEC_VERSION_MAJOR > 53
213  return (_pixelDesc->flags & PIX_FMT_PSEUDOPAL) == PIX_FMT_PSEUDOPAL;
214 #else
215  return false;
216 #endif
217 }
218 
219 std::vector<Channel> PixelProperties::getChannels() const
220 {
221  if(!_pixelDesc)
222  throw std::runtime_error("unable to find pixel description.");
223 
224  std::vector<Channel> channels;
225  for(size_t channel = 0; channel < (size_t)_pixelDesc->nb_components; ++channel)
226  {
227  Channel c;
228  c.id = channel;
229  c.chromaHeight = (size_t)_pixelDesc->comp[channel].plane;
230  c.bitStep = (size_t)_pixelDesc->comp[channel].step_minus1;
231  channels.push_back(c);
232  }
233  return channels;
234 }
235 
237 {
238  PropertyVector propertyVector;
239  return fillVector(propertyVector);
240 }
241 
243 {
244  addProperty(data, "pixelName", &PixelProperties::getPixelName);
245  addProperty(data, "pixelFormatName", &PixelProperties::getPixelFormatName);
246  addProperty(data, "bitDepth", &PixelProperties::getBitsPerPixel);
247  addProperty(data, "maxNbBitsInChannels", &PixelProperties::getMaxNbBitsInChannels);
248  addProperty(data, "nbComponents", &PixelProperties::getNbComponents);
249  addProperty(data, "chromaWidth", &PixelProperties::getChromaWidth);
250  addProperty(data, "chromaHeight", &PixelProperties::getChromaHeight);
251 
252  try
253  {
254  std::string colorComponents;
255  switch(getColorComponents())
256  {
257  case eComponentGray:
258  colorComponents = "gray";
259  break;
260  case eComponentRgb:
261  colorComponents = "RGB";
262  break;
263  case eComponentYuvJPEG:
264  colorComponents = "YUVJPEG";
265  break;
266  case eComponentYuvA:
267  colorComponents = "YUVA";
268  break;
269  case eComponentYuv:
270  colorComponents = "YUV";
271  break;
272  }
273  detail::add(data, "colorComponents", colorComponents);
274  }
275  catch(const std::exception& e)
276  {
277  detail::add(data, "colorComponents", detail::propertyValueIfError);
278  }
279 
280  try
281  {
282  std::string subsampling;
283  switch(getSubsampling())
284  {
285  case eSubsampling440:
286  subsampling = "440";
287  break;
288  case eSubsampling422:
289  subsampling = "422";
290  break;
291  case eSubsampling420:
292  subsampling = "420";
293  break;
294  case eSubsampling411:
295  subsampling = "411";
296  break;
297  case eSubsampling410:
298  subsampling = "410";
299  break;
300  case eSubsamplingNone:
301  subsampling = "None";
302  break;
303  }
304  detail::add(data, "subsampling", subsampling);
305  }
306  catch(const std::exception& e)
307  {
308  detail::add(data, "subsampling", detail::propertyValueIfError);
309  }
310 
311  addProperty(data, "isBigEndian", &PixelProperties::isBigEndian);
312  addProperty(data, "hasAlpha", &PixelProperties::hasAlpha);
313  addProperty(data, "isPlanar", &PixelProperties::isPlanar);
314  addProperty(data, "isIndexedColors", &PixelProperties::isIndexedColors);
315  addProperty(data, "bitWiseAcked", &PixelProperties::isBitWisePacked);
316  addProperty(data, "isHardwareAccelerated", &PixelProperties::isHardwareAccelerated);
317  addProperty(data, "rgbPixel", &PixelProperties::isRgbPixelData);
318  addProperty(data, "isPseudoPaletted", &PixelProperties::isPseudoPaletted);
319 
320  try
321  {
322  std::vector<Channel> channels = getChannels();
323  for(size_t channelIndex = 0; channelIndex < channels.size(); ++channelIndex)
324  {
325  std::stringstream channelName;
326  channelName << "channel_" << channels.at(channelIndex).id;
327 
328  std::stringstream channelValue;
329  channelValue << "chromaHeight " << channels.at(channelIndex).chromaHeight;
330  channelValue << " - ";
331  channelValue << "bitStep " << channels.at(channelIndex).bitStep;
332 
333  detail::add(data, channelName.str(), channelValue.str());
334  }
335  }
336  catch(const std::exception& e)
337  {
338  detail::add(data, "channels", detail::propertyValueIfError);
339  }
340 
341  return data;
342 }
343 }
std::vector< std::pair< std::string, std::string > > PropertyVector
PropertyVector is a vector of pair, because the order of properties matters to us.
Definition: util.hpp:23
ESubsamplingType getSubsampling() const
std::string getPixelName() const
EComponentType getColorComponents() const
std::string getPixelFormatName() const
PropertyVector asVector() const
Return all pixel properties as a vector (name of property: value)
void add(PropertyVector &propertyVector, const std::string &key, const size_t &value)
Definition: util.cpp:16
YUV color space. 16 <= Y <= 235, 16 <= U, V <= 240.
YUV color space with transparency.
PixelProperties(const std::string &avPixelFormat="")
#define AV_PIX_FMT_NONE
Definition: common.hpp:44
YUV color space. 0 <= Y <= 255, 0 <= U, V <= 255.
const AVPixFmtDescriptor * _pixelDesc
Has link (no ownership)
void addProperty(PropertyVector &data, const std::string &key, T(PixelProperties::*getter)(void) const) const
std::vector< Channel > getChannels() const
size_t getMaxNbBitsInChannels() const
PropertyVector & fillVector(PropertyVector &data) const
To avoid copy of the vector.
void init(const AVPixelFormat avPixelFormat)
#define AVPixelFormat
Definition: common.hpp:43
size_t getBitsPerPixel() const
padding bits are not counted
const std::string propertyValueIfError
Definition: util.hpp:38