xoreos  0.0.5
vpx.cpp
Go to the documentation of this file.
1 /* xoreos - A reimplementation of BioWare's Aurora engine
2  *
3  * xoreos is the legal property of its developers, whose names
4  * can be found in the AUTHORS file distributed with this source
5  * distribution.
6  *
7  * xoreos is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 3
10  * of the License, or (at your option) any later version.
11  *
12  * xoreos is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with xoreos. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
25 #include <vpx/vpx_decoder.h>
26 #include <vpx/vp8dx.h>
27 
28 // Undefine UNUSED, which is defined by libvpx
29 #undef UNUSED
30 
31 #include "src/common/readstream.h"
32 #include "src/common/scopedptr.h"
35 #include "src/video/codecs/codec.h"
36 #include "src/video/codecs/vpx.h"
37 
38 namespace Video {
39 
40 class VPXDecoder : public Codec {
41 public:
42  VPXDecoder();
43  ~VPXDecoder();
44 
45  bool init(vpx_codec_iface_t *iface);
46 
47  void decodeFrame(Graphics::Surface &surface, Common::SeekableReadStream &dataStream);
48 
49 private:
51  vpx_codec_ctx _context;
52 
54 };
55 
56 VPXDecoder::VPXDecoder() : _initialized(false) {
57 }
58 
60  if (_initialized)
61  vpx_codec_destroy(&_context);
62 }
63 
64 bool VPXDecoder::init(vpx_codec_iface_t *iface) {
65  // Destroy any old context
66  if (_initialized) {
67  vpx_codec_destroy(&_context);
68  _initialized = false;
69  }
70 
71  // Set up the config params
72  // TODO: More threads?
73  vpx_codec_dec_cfg config;
74  memset(&config, 0, sizeof(config));
75  config.threads = 1;
76 
77  // Try to perform the initialization
78  vpx_codec_err_t result = vpx_codec_dec_init(&_context, iface, &config, 0);
79  if (result != VPX_CODEC_OK)
80  return false;
81 
82  // All good
83  _initialized = true;
84  return true;
85 }
86 
88  if (!_initialized)
89  return;
90 
91  // Read all the data from the stream
92  Common::ScopedArray<byte> data(new byte[dataStream.size()]);
93  dataStream.read(data.get(), dataStream.size());
94 
95  // Perform the actual decode
96  vpx_codec_err_t result = vpx_codec_decode(&_context, data.get(), dataStream.size(), 0, 0);
97  if (result != VPX_CODEC_OK)
98  return;
99 
100  // Try to get the image
101  vpx_codec_iter_t iter = 0;
102  vpx_image_t *image = vpx_codec_get_frame(&_context, &iter);
103  if (!image)
104  return;
105 
106  // Figure out the color range
108  switch (image->range) {
109  case VPX_CR_STUDIO_RANGE:
111  break;
112  case VPX_CR_FULL_RANGE:
114  break;
115  default:
116  return;
117  }
118 
119  // If we don't have it already, create our local surface
120  if (!_surface)
121  _surface.reset(new Graphics::Surface(image->w, image->h));
122 
123  // Do the conversion based on the color space
124  switch (image->fmt) {
125  case VPX_IMG_FMT_I420:
126  YUVToRGBMan.convert420(scale, _surface->getData(), _surface->getPitch(), image->planes[0], image->planes[1], image->planes[2], image->w, image->h, image->stride[0], image->stride[1]);
127  break;
128  default:
129  return;
130  }
131 
132  // Copy the subarea into the surface
133  for (int y = 0; y < surface.getHeight(); y++)
134  memcpy(surface.getData() + y * surface.getPitch(), _surface->getData() + (y * image->d_h) * image->d_w * 4, image->d_w * 4);
135 }
136 
139  if (!decoder->init(&vpx_codec_vp8_dx_algo))
140  return 0;
141 
142  return decoder.release();
143 }
144 
147  if (!decoder->init(&vpx_codec_vp9_dx_algo))
148  return 0;
149 
150  return decoder.release();
151 }
152 
153 } // End of namespace Video
154 
byte * getData()
Definition: surface.cpp:61
LuminanceScale
The scale of the luminance values.
Definition: yuv_to_rgb.h:40
Luminance values range from [0, 255].
Definition: yuv_to_rgb.h:42
VP8/VP9 codec class.
void reset(PointerType o=0)
Resets the pointer with the new value.
Definition: scopedptr.h:87
PointerType release()
Returns the plain pointer value and releases ScopedPtr.
Definition: scopedptr.h:103
Video codec base class.
A simple scoped smart pointer template.
bool init(vpx_codec_iface_t *iface)
Definition: vpx.cpp:64
Common::ScopedPtr< Graphics::Surface > _surface
Definition: vpx.cpp:53
void decodeFrame(Graphics::Surface &surface, Common::SeekableReadStream &dataStream)
Definition: vpx.cpp:87
bool _initialized
Definition: vpx.cpp:50
virtual size_t read(void *dataPtr, size_t dataSize)=0
Read data from the stream.
#define YUVToRGBMan
Definition: yuv_to_rgb.h:91
Efficient YUV to RGB conversion.
virtual size_t size() const =0
Obtains the total size of the stream, measured in bytes.
vpx_codec_ctx _context
Definition: vpx.cpp:51
Basic reading stream interfaces.
An image surface, in BGRA format.
Codec * makeVP8Decoder()
Create a codec capable of decoding VP8 frames.
Definition: vpx.cpp:137
int getHeight() const
Definition: surface.cpp:57
Codec * makeVP9Decoder()
Create a codec capable of decoding VP9 frames.
Definition: vpx.cpp:145
int getPitch() const
Definition: surface.h:39
Interface for a seekable & readable data stream.
Definition: readstream.h:265
uint8 byte
Definition: types.h:209