video_coding: Refatoring I420 wrapper. No functional updates.

BUG=
TEST=

Review URL: https://webrtc-codereview.appspot.com/673009

git-svn-id: http://webrtc.googlecode.com/svn/trunk@2504 4adac7df-926f-26a2-2b94-8c16560cd09d
This commit is contained in:
mikhal@webrtc.org 2012-07-10 20:58:08 +00:00
parent 0bb817dab0
commit b95e9ca865
2 changed files with 195 additions and 240 deletions

View File

@ -14,43 +14,42 @@
#include "video_codec_interface.h"
#include "typedefs.h"
namespace webrtc
{
namespace webrtc {
class I420Encoder : public VideoEncoder
{
class I420Encoder : public VideoEncoder {
public:
I420Encoder();
I420Encoder();
virtual ~I420Encoder();
virtual ~I420Encoder();
// Initialize the encoder with the information from the VideoCodec
// Initialize the encoder with the information from the VideoCodec.
//
// Input:
// - codecSettings : Codec settings
// - numberOfCores : Number of cores available for the encoder
// - codecSettings : Codec settings.
// - numberOfCores : Number of cores available for the encoder.
// - maxPayloadSize : The maximum size each payload is allowed
// to have. Usually MTU - overhead.
//
// Return value : WEBRTC_VIDEO_CODEC_OK if OK
// Return value : WEBRTC_VIDEO_CODEC_OK if OK.
// <0 - Error
virtual WebRtc_Word32 InitEncode(const VideoCodec* codecSettings, WebRtc_Word32 /*numberOfCores*/, WebRtc_UWord32 /*maxPayloadSize*/);
virtual int InitEncode(const VideoCodec* codecSettings,
int /*numberOfCores*/,
uint32_t /*maxPayloadSize*/);
// "Encode" an I420 image (as a part of a video stream). The encoded image
// will be returned to the user via the encode complete callback.
//
// Input:
// - inputImage : Image to be encoded
// - codecSpecificInfo : Pointer to codec specific data
// - frameType : Frame type to be sent (Key /Delta) .
// - inputImage : Image to be encoded.
// - codecSpecificInfo : Pointer to codec specific data.
// - frameType : Frame type to be sent (Key /Delta).
//
// Return value : WEBRTC_VIDEO_CODEC_OK if OK
// Return value : WEBRTC_VIDEO_CODEC_OK if OK.
// <0 - Error
virtual WebRtc_Word32
Encode(const RawImage& inputImage,
const CodecSpecificInfo* /*codecSpecificInfo*/,
const VideoFrameType /*frameTypes*/);
virtual int Encode(const RawImage& inputImage,
const CodecSpecificInfo* /*codecSpecificInfo*/,
const VideoFrameType /*frameTypes*/);
// Register an encode complete callback object.
//
@ -58,48 +57,46 @@ public:
// - callback : Callback object which handles encoded images.
//
// Return value : WEBRTC_VIDEO_CODEC_OK if OK, < 0 otherwise.
virtual WebRtc_Word32 RegisterEncodeCompleteCallback(EncodedImageCallback* callback);
virtual int RegisterEncodeCompleteCallback(EncodedImageCallback* callback);
// Free encoder memory.
//
// Return value : WEBRTC_VIDEO_CODEC_OK if OK, < 0 otherwise.
virtual WebRtc_Word32 Release();
virtual int Release();
virtual WebRtc_Word32 SetRates(WebRtc_UWord32 /*newBitRate*/,
WebRtc_UWord32 /*frameRate*/)
virtual int SetRates(uint32_t /*newBitRate*/, uint32_t /*frameRate*/)
{return WEBRTC_VIDEO_CODEC_OK;}
virtual WebRtc_Word32 SetChannelParameters(WebRtc_UWord32 /*packetLoss*/,
int /*rtt*/)
virtual int SetChannelParameters(uint32_t /*packetLoss*/, int /*rtt*/)
{return WEBRTC_VIDEO_CODEC_OK;}
virtual WebRtc_Word32 CodecConfigParameters(WebRtc_UWord8* /*buffer*/,
WebRtc_Word32 /*size*/)
virtual int CodecConfigParameters(uint8_t* /*buffer*/, int /*size*/)
{return WEBRTC_VIDEO_CODEC_OK;}
private:
bool _inited;
EncodedImage _encodedImage;
EncodedImageCallback* _encodedCompleteCallback;
bool _inited;
EncodedImage _encodedImage;
EncodedImageCallback* _encodedCompleteCallback;
}; // end of WebRtcI420DEncoder class
class I420Decoder : public VideoDecoder
{
class I420Decoder : public VideoDecoder {
public:
I420Decoder();
I420Decoder();
virtual ~I420Decoder();
virtual ~I420Decoder();
// Initialize the decoder.
// The user must notify the codec of width and height values.
//
// Return value : WEBRTC_VIDEO_CODEC_OK.
// <0 - Errors
virtual WebRtc_Word32 InitDecode(const VideoCodec* codecSettings, WebRtc_Word32 /*numberOfCores*/);
virtual int InitDecode(const VideoCodec* codecSettings,
int /*numberOfCores*/);
virtual WebRtc_Word32 SetCodecConfigParameters(const WebRtc_UWord8* /*buffer*/, WebRtc_Word32 /*size*/){return WEBRTC_VIDEO_CODEC_OK;};
virtual int SetCodecConfigParameters(const uint8_t* /*buffer*/, int /*size*/)
{return WEBRTC_VIDEO_CODEC_OK;};
// Decode encoded image (as a part of a video stream). The decoded image
// will be returned to the user through the decode complete callback.
@ -113,12 +110,11 @@ public:
//
// Return value : WEBRTC_VIDEO_CODEC_OK if OK
// <0 - Error
virtual WebRtc_Word32 Decode(
const EncodedImage& inputImage,
bool missingFrames,
const RTPFragmentationHeader* /*fragmentation*/,
const CodecSpecificInfo* /*codecSpecificInfo*/,
WebRtc_Word64 /*renderTimeMs*/);
virtual int Decode(const EncodedImage& inputImage,
bool missingFrames,
const RTPFragmentationHeader* /*fragmentation*/,
const CodecSpecificInfo* /*codecSpecificInfo*/,
int64_t /*renderTimeMs*/);
// Register a decode complete callback object.
//
@ -126,30 +122,29 @@ public:
// - callback : Callback object which handles decoded images.
//
// Return value : WEBRTC_VIDEO_CODEC_OK if OK, < 0 otherwise.
virtual WebRtc_Word32 RegisterDecodeCompleteCallback(DecodedImageCallback* callback);
virtual int RegisterDecodeCompleteCallback(DecodedImageCallback* callback);
// Free decoder memory.
//
// Return value : WEBRTC_VIDEO_CODEC_OK if OK
// Return value : WEBRTC_VIDEO_CODEC_OK if OK.
// <0 - Error
virtual WebRtc_Word32 Release();
virtual int Release();
// Reset decoder state and prepare for a new call.
//
// Return value : WEBRTC_VIDEO_CODEC_OK.
// <0 - Error
virtual WebRtc_Word32 Reset();
virtual int Reset();
private:
RawImage _decodedImage;
WebRtc_Word32 _width;
WebRtc_Word32 _height;
bool _inited;
DecodedImageCallback* _decodeCompleteCallback;
RawImage _decodedImage;
int _width;
int _height;
bool _inited;
DecodedImageCallback* _decodeCompleteCallback;
}; // end of WebRtcI420Decoder class
}; // End of WebRtcI420Decoder class.
} // namespace webrtc

View File

@ -18,121 +18,101 @@ I420Encoder::I420Encoder():
_inited(false),
_encodedImage(),
_encodedCompleteCallback(NULL)
{
//
{}
I420Encoder::~I420Encoder() {
_inited = false;
if (_encodedImage._buffer != NULL) {
delete [] _encodedImage._buffer;
_encodedImage._buffer = NULL;
}
}
I420Encoder::~I420Encoder()
{
_inited = false;
if (_encodedImage._buffer != NULL)
{
delete [] _encodedImage._buffer;
_encodedImage._buffer = NULL;
}
int I420Encoder::Release() {
// Should allocate an encoded frame and then release it here, for that we
// actually need an init flag.
if (_encodedImage._buffer != NULL) {
delete [] _encodedImage._buffer;
_encodedImage._buffer = NULL;
}
_inited = false;
return WEBRTC_VIDEO_CODEC_OK;
}
WebRtc_Word32
I420Encoder::Release()
{
// should allocate an encoded frame and then release it here, for that we actaully need an init flag
if (_encodedImage._buffer != NULL)
{
delete [] _encodedImage._buffer;
_encodedImage._buffer = NULL;
}
_inited = false;
return WEBRTC_VIDEO_CODEC_OK;
int I420Encoder::InitEncode(const VideoCodec* codecSettings,
int /*numberOfCores*/,
uint32_t /*maxPayloadSize */) {
if (codecSettings == NULL) {
return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
}
if (codecSettings->width < 1 || codecSettings->height < 1) {
return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
}
// Allocating encoded memory.
if (_encodedImage._buffer != NULL) {
delete [] _encodedImage._buffer;
_encodedImage._buffer = NULL;
_encodedImage._size = 0;
}
const uint32_t newSize = (3 * codecSettings->width *
codecSettings->height) >> 1;
uint8_t* newBuffer = new uint8_t[newSize];
if (newBuffer == NULL) {
return WEBRTC_VIDEO_CODEC_MEMORY;
}
_encodedImage._size = newSize;
_encodedImage._buffer = newBuffer;
// If no memory allocation, no point to init.
_inited = true;
return WEBRTC_VIDEO_CODEC_OK;
}
WebRtc_Word32
I420Encoder::InitEncode(const VideoCodec* codecSettings,
WebRtc_Word32 /*numberOfCores*/,
WebRtc_UWord32 /*maxPayloadSize */)
{
if (codecSettings == NULL)
{
return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
}
if (codecSettings->width < 1 || codecSettings->height < 1)
{
return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
}
// allocating encoded memory
if (_encodedImage._buffer != NULL)
{
delete [] _encodedImage._buffer;
_encodedImage._buffer = NULL;
_encodedImage._size = 0;
int I420Encoder::Encode(const RawImage& inputImage,
const CodecSpecificInfo* /*codecSpecificInfo*/,
const VideoFrameType /*frameType*/) {
if (!_inited) {
return WEBRTC_VIDEO_CODEC_UNINITIALIZED;
}
if (_encodedCompleteCallback == NULL) {
return WEBRTC_VIDEO_CODEC_UNINITIALIZED;
}
_encodedImage._frameType = kKeyFrame; // No coding.
_encodedImage._timeStamp = inputImage._timeStamp;
_encodedImage._encodedHeight = inputImage._height;
_encodedImage._encodedWidth = inputImage._width;
if (inputImage._length > _encodedImage._size) {
// Allocating encoded memory.
if (_encodedImage._buffer != NULL) {
delete [] _encodedImage._buffer;
_encodedImage._buffer = NULL;
_encodedImage._size = 0;
}
const WebRtc_UWord32 newSize = (3 * codecSettings->width *
codecSettings->height) >> 1;
WebRtc_UWord8* newBuffer = new WebRtc_UWord8[newSize];
if (newBuffer == NULL)
{
return WEBRTC_VIDEO_CODEC_MEMORY;
const uint32_t newSize = (3 * _encodedImage._encodedWidth *
_encodedImage._encodedHeight) >> 1;
uint8_t* newBuffer = new uint8_t[newSize];
if (newBuffer == NULL) {
return WEBRTC_VIDEO_CODEC_MEMORY;
}
_encodedImage._size = newSize;
_encodedImage._buffer = newBuffer;
// if no memory allocation, no point to init
_inited = true;
return WEBRTC_VIDEO_CODEC_OK;
}
memcpy(_encodedImage._buffer, inputImage._buffer, inputImage._length);
_encodedImage._length = inputImage._length;
_encodedCompleteCallback->Encoded(_encodedImage);
return WEBRTC_VIDEO_CODEC_OK;
}
WebRtc_Word32
I420Encoder::Encode(const RawImage& inputImage,
const CodecSpecificInfo* /*codecSpecificInfo*/,
const VideoFrameType /*frameType*/)
{
if (!_inited)
{
return WEBRTC_VIDEO_CODEC_UNINITIALIZED;
}
if (_encodedCompleteCallback == NULL)
{
return WEBRTC_VIDEO_CODEC_UNINITIALIZED;
}
_encodedImage._frameType = kKeyFrame; // no coding
_encodedImage._timeStamp = inputImage._timeStamp;
_encodedImage._encodedHeight = inputImage._height;
_encodedImage._encodedWidth = inputImage._width;
if (inputImage._length > _encodedImage._size)
{
// allocating encoded memory
if (_encodedImage._buffer != NULL)
{
delete [] _encodedImage._buffer;
_encodedImage._buffer = NULL;
_encodedImage._size = 0;
}
const WebRtc_UWord32 newSize = (3 * _encodedImage._encodedWidth * _encodedImage._encodedHeight) >> 1;
WebRtc_UWord8* newBuffer = new WebRtc_UWord8[newSize];
if (newBuffer == NULL)
{
return WEBRTC_VIDEO_CODEC_MEMORY;
}
_encodedImage._size = newSize;
_encodedImage._buffer = newBuffer;
}
memcpy(_encodedImage._buffer, inputImage._buffer, inputImage._length);
_encodedImage._length = inputImage._length;
_encodedCompleteCallback->Encoded(_encodedImage);
return WEBRTC_VIDEO_CODEC_OK;
}
WebRtc_Word32
I420Encoder::RegisterEncodeCompleteCallback(EncodedImageCallback* callback)
{
_encodedCompleteCallback = callback;
return WEBRTC_VIDEO_CODEC_OK;
int
I420Encoder::RegisterEncodeCompleteCallback(EncodedImageCallback* callback) {
_encodedCompleteCallback = callback;
return WEBRTC_VIDEO_CODEC_OK;
}
@ -142,112 +122,92 @@ _width(0),
_height(0),
_inited(false),
_decodeCompleteCallback(NULL)
{
//
{}
I420Decoder::~I420Decoder() {
Release();
}
I420Decoder::~I420Decoder()
{
Release();
}
WebRtc_Word32
I420Decoder::Reset()
{
return WEBRTC_VIDEO_CODEC_OK;
int
I420Decoder::Reset() {
return WEBRTC_VIDEO_CODEC_OK;
}
WebRtc_Word32
I420Decoder::InitDecode(const VideoCodec* codecSettings, WebRtc_Word32 /*numberOfCores */)
{
if (codecSettings == NULL)
{
return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
}
else if (codecSettings->width < 1 || codecSettings->height < 1)
{
return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
}
_width = codecSettings->width;
_height = codecSettings->height;
_inited = true;
return WEBRTC_VIDEO_CODEC_OK;
int
I420Decoder::InitDecode(const VideoCodec* codecSettings,
int /*numberOfCores */) {
if (codecSettings == NULL) {
return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
} else if (codecSettings->width < 1 || codecSettings->height < 1) {
return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
}
_width = codecSettings->width;
_height = codecSettings->height;
_inited = true;
return WEBRTC_VIDEO_CODEC_OK;
}
WebRtc_Word32
int
I420Decoder::Decode(const EncodedImage& inputImage,
bool /*missingFrames*/,
const RTPFragmentationHeader* /*fragmentation*/,
const CodecSpecificInfo* /*codecSpecificInfo*/,
WebRtc_Word64 /*renderTimeMs*/)
{
if (inputImage._buffer == NULL)
{
return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
}
if (_decodeCompleteCallback == NULL)
{
return WEBRTC_VIDEO_CODEC_UNINITIALIZED;
}
if (inputImage._length <= 0)
{
return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
}
if (!_inited)
{
return WEBRTC_VIDEO_CODEC_UNINITIALIZED;
}
int64_t /*renderTimeMs*/) {
if (inputImage._buffer == NULL) {
return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
}
if (_decodeCompleteCallback == NULL) {
return WEBRTC_VIDEO_CODEC_UNINITIALIZED;
}
if (inputImage._length <= 0) {
return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
}
if (!_inited) {
return WEBRTC_VIDEO_CODEC_UNINITIALIZED;
}
//Allocate memory for decoded image
if (_decodedImage._buffer != NULL)
{
delete [] _decodedImage._buffer;
_decodedImage._buffer = NULL;
_decodedImage._size = 0;
}
if (_decodedImage._buffer == NULL)
{
const WebRtc_UWord32 newSize = (3*_width*_height) >> 1;
WebRtc_UWord8* newBuffer = new WebRtc_UWord8[newSize];
if (newBuffer == NULL)
{
return WEBRTC_VIDEO_CODEC_MEMORY;
}
_decodedImage._size = newSize;
_decodedImage._buffer = newBuffer;
// Allocate memory for decoded image.
if (_decodedImage._buffer != NULL) {
delete [] _decodedImage._buffer;
_decodedImage._buffer = NULL;
_decodedImage._size = 0;
}
if (_decodedImage._buffer == NULL) {
const uint32_t newSize = (3*_width*_height) >> 1;
uint8_t* newBuffer = new uint8_t[newSize];
if (newBuffer == NULL) {
return WEBRTC_VIDEO_CODEC_MEMORY;
}
_decodedImage._size = newSize;
_decodedImage._buffer = newBuffer;
}
// Set decoded image parameters
_decodedImage._height = _height;
_decodedImage._width = _width;
_decodedImage._timeStamp = inputImage._timeStamp;
memcpy(_decodedImage._buffer, inputImage._buffer, inputImage._length);
_decodedImage._length = inputImage._length;
//_decodedImage._buffer = inputImage._buffer;
// Set decoded image parameters.
_decodedImage._height = _height;
_decodedImage._width = _width;
_decodedImage._timeStamp = inputImage._timeStamp;
memcpy(_decodedImage._buffer, inputImage._buffer, inputImage._length);
_decodedImage._length = inputImage._length;
_decodeCompleteCallback->Decoded(_decodedImage);
return WEBRTC_VIDEO_CODEC_OK;
_decodeCompleteCallback->Decoded(_decodedImage);
return WEBRTC_VIDEO_CODEC_OK;
}
WebRtc_Word32
I420Decoder::RegisterDecodeCompleteCallback(DecodedImageCallback* callback)
{
_decodeCompleteCallback = callback;
return WEBRTC_VIDEO_CODEC_OK;
int
I420Decoder::RegisterDecodeCompleteCallback(DecodedImageCallback* callback) {
_decodeCompleteCallback = callback;
return WEBRTC_VIDEO_CODEC_OK;
}
WebRtc_Word32
I420Decoder::Release()
{
if (_decodedImage._buffer != NULL)
{
delete [] _decodedImage._buffer;
_decodedImage._buffer = NULL;
}
_inited = false;
return WEBRTC_VIDEO_CODEC_OK;
int
I420Decoder::Release() {
if (_decodedImage._buffer != NULL) {
delete [] _decodedImage._buffer;
_decodedImage._buffer = NULL;
}
_inited = false;
return WEBRTC_VIDEO_CODEC_OK;
}
}