Пример #1
0
// ---------------------------------------------------------------------------------
static PyObject *
Decoder_Decode( PyACodecObject* obj, PyObject *args)
{
	unsigned char* sData;
	void* pBuf;
	PyAFrameObject *cFrame= NULL;
	int iLen, out_size, len, iBufSize, iPos= 0;
	if (!PyArg_ParseTuple(args, "s#:decode", &sData, &iLen ))
		return NULL;

	// Get the header data first
	//need to add padding to buffer for libavcodec
	if( !Codec_AdjustPadBuffer( obj, iLen ) )
	{
		PyErr_NoMemory();
		return NULL;
	}
	memcpy( obj->pPaddedBuf, sData, iLen);
	sData=(uint8_t*)obj->pPaddedBuf;

	// Realloc memory
	iBufSize= AVCODEC_MAX_AUDIO_FRAME_SIZE* 2;
	pBuf= av_malloc( iBufSize );
	if( !pBuf )
	{
		PyErr_NoMemory();
		return NULL;
	}

	while( iLen> 0 )
	{
		if( iBufSize- iPos< AVCODEC_MAX_AUDIO_FRAME_SIZE )
		{
			pBuf= av_realloc( pBuf, iBufSize+ AVCODEC_MAX_AUDIO_FRAME_SIZE* 2 );
			if( !pBuf )
			{
				PyErr_NoMemory();
				return NULL;
			}
			iBufSize+= AVCODEC_MAX_AUDIO_FRAME_SIZE* 2;
		}
		out_size= 0;
		len= obj->cCodec->codec->decode( obj->cCodec, (char*)pBuf+ iPos, &out_size, sData, iLen );
		if( len < 0 )
		{
			// Need to report out the error( it should be in the error list )
			/*while( g_AvilibErr[ i ].iErrCode )
				if( g_AvilibErr[ i ].iErrCode== len )
				{
					PyErr_SetString(g_cErr, g_AvilibErr[ i ].sErrDesc );
					return NULL;
				}
				else
					i++;
			*/
			PyErr_Format(g_cErr, "Unspecified error %d. Cannot find any help text for it.", len );
			av_free( pBuf );
			return NULL;
		}
		else
		{
			iLen-= len;
			sData+= len;
			if( out_size> 0 )
			{
				iPos+= out_size;
				if( cFrame  )
				{
					cFrame->cData->pData= pBuf;
					cFrame->cData->iLen= iPos;
				}
				else
				{
					PyACStringObject* cRes;
					cFrame= (PyAFrameObject*)PyObject_New( PyAFrameObject, &FrameType );
					if( !cFrame )
						return NULL;

					cRes= ACString_New( pBuf, out_size );
					if( !cRes )
						return NULL;

					cFrame->bit_rate= obj->cCodec->bit_rate; 
					cFrame->sample_rate=	obj->cCodec->sample_rate;
					cFrame->bits_per_sample= obj->cCodec->bits_per_sample;
					cFrame->channels= obj->cCodec->channels;
					cFrame->cData= cRes;
				}
			}
		}
	}
#ifdef HAVE_MMX
    emms();
#endif

	if( !cFrame )
		free( pBuf );
	else
		return (PyObject*)cFrame;

	if( out_size )
		// Raise an error if data was found but no frames created
		return NULL;

	RETURN_NONE
}
Пример #2
0
// ---------------------------------------------------------------------------------
PyObject *
Codec_Decode( PyCodecObject* obj, PyObject *args)
{
  PyObject* cRes= NULL;
  uint8_t *sData=NULL,*sBuf=NULL;
  int iLen=0, out_size=0, i=0, len=0, hurry= 0;
  int64_t pts = 0;
  // Add by Vadim Grigoriev to save pts
  if (!PyArg_ParseTuple(args, "s#|Li:decode", &sBuf, &iLen, &pts, &hurry ))
    return NULL;
  //need to add padding to buffer for libavcodec
  if( !Codec_AdjustPadBuffer( obj, iLen ) )
    {
      PyErr_NoMemory();
      return NULL;
    }
  memcpy( obj->pPaddedBuf, sBuf, iLen);
  sData=(uint8_t*)obj->pPaddedBuf;

  while( iLen> 0 )
    {
      out_size= 0;
      obj->cCodec->hurry_up= hurry;
      // Add By Vadim Grigoriev to save pts
      obj->frame.pts = pts;
      //	if (obj->cCodec->coded_frame.pts > 0 )
      //
      len= obj->cCodec->codec->decode( obj->cCodec, &obj->frame, &out_size, sData, iLen );
      if( len < 0 )
			{
				// Need to report out the error( it should be in the error list )
				while( g_AvilibErr[ i ].iErrCode )
					if( g_AvilibErr[ i ].iErrCode== len )
						{
				PyErr_SetString(g_cErr, g_AvilibErr[ i ].sErrDesc );
				return NULL;
						}
					else
						i++;

				PyErr_Format(g_cErr, "Unspecified error %d. Cannot find any help text for it.", len );
				return NULL;
			}
      else
			{
				iLen-= len;
				sData+= len;
				if( !cRes && out_size> 0 )
					cRes= Frame_New_LAVC( obj );
			}
    }
#ifdef HAVE_MMX
  emms();
#endif

  if( cRes )
    return cRes;

  if( out_size )
    // Raise an error if data was found but no frames created
    return NULL;

  Py_INCREF( Py_None );
  return Py_None;
}