Exemplo n.º 1
0
AVFrame* FFMpegDecoder::decodeVideo(AVPacket *encodedPacket)
{
	int gotPic = 0;
	int bytesDecoded = -1;
	
	my_video_pkt_pts = encodedPacket->pts;
	bytesDecoded = avcodec_decode_video2(pVideoCodecCtx,pDecodedFrame,&gotPic,encodedPacket);
	


	if (encodedPacket->dts == AV_NOPTS_VALUE && pDecodedFrame->opaque && *(uint64_t*)pDecodedFrame->opaque != AV_NOPTS_VALUE)
		videoPTS = *(uint64_t *)pDecodedFrame->opaque;
	else if(encodedPacket->dts != AV_NOPTS_VALUE)
		videoPTS = encodedPacket->dts;
	else
		videoPTS = 0;

	//videoPTS *= pVideoCodecCtx->time_base;
	videoPTS *= av_q2d(pFormatCtx->streams[videoStream]->time_base);
    
	if (bytesDecoded < 0)
	{
		errinfo("decode error");
		return NULL;
	}	
	if (gotPic)
	{
		syncVideoPTS();
		return pDecodedFrame;
	}	
	else
		return NULL;
}
Exemplo n.º 2
0
AVFrame *FFMpegDecoder::getFrameAtSec(double sec)
{
	if (videoStream < 0)
	{
		errinfo("no video stream!");
		return NULL;
	}

	AVFrame *pic = NULL;
	seekToSec(sec);
	while (true)
	{
		AVPacket *pkt = readPacket();
		if (pkt == NULL)
			break;

		if (pkt->stream_index != videoStream)
			continue;

		pic = decodeVideo();
		if (pic != NULL)
			break;
	}
	return pic;
}
Exemplo n.º 3
0
      inline int tuntap_open(const Layer& layer, std::string& name)
      {
 	for (int i = 0; i < 256; ++i)
	  {
	    const char *tuntap;
	    if (layer() == Layer::OSI_LAYER_3)
	      tuntap = "tun";
	    else if (layer() == Layer::OSI_LAYER_2)
	      tuntap = "tap";
	    else
	      throw tun_mac_util("unknown OSI layer");
	    const std::string node_str = tuntap + to_string(i);
	    const std::string node_fn = "/dev/" + node_str;

	    ScopedFD fd(open(node_fn.c_str(), O_RDWR));
	    if (fd.defined())
	      {
		// got it
		if (fcntl(fd(), F_SETFL, O_NONBLOCK) < 0)
		  throw tun_mac_util("fcntl error on " + node_fn + " : " + errinfo(errno));

		name = node_str;
		return fd.release();
	      }
	  }
	throw tun_mac_util(std::string("error opening Mac ") + layer.dev_type() + " device");
      }
Exemplo n.º 4
0
/**
 * @brief Builds a lattice with a basis
 *
 * @param offset
 */
void SCSystemBuilder::buildLattice(const Vector4d offset)
{
    // TODO: Validate input
    Vector4d position=zero4;

    for(int i=0; i<=2; ++i)
    {
        if( fmod(boxSize_[i], latticeData_.latticeConstant)/latticeData_.latticeConstant > relativeUnitCellTolerance)
        {
            if ((latticeData_.latticeConstant - fmod(boxSize_[i], latticeData_.latticeConstant))>relativeUnitCellTolerance)
            {
            BOOST_THROW_EXCEPTION(InvalidConfigurationException()
                                  << errinfo("System dimensions must be a multiple of the lattice constant when building periodic lattices"));
            }
        }
    }

    if(     (latticeData_.cells[0] < 1) ||
            (latticeData_.cells[1] < 1) ||
            (latticeData_.cells[2] < 1))
    {
        BOOST_THROW_EXCEPTION(InvalidConfigurationException()
                              << errinfo("Invalid boxSize and latticeConstant combination"));
    }

    // TODO: bounds checking for offset vs. boxSize

    // Loop over all dimensions
    for(int i = 0; i < latticeData_.cells[0]; ++i)
    {
        for(int j = 0; j < latticeData_.cells[1]; ++j)
        {
            for(int k = 0; k < latticeData_.cells[2]; ++k)
            {
                // Calculate the positions of the atoms
                position[0] = i * latticeData_.latticeConstant + offset[0];
                position[1] = j * latticeData_.latticeConstant + offset[1];
                position[2] = k * latticeData_.latticeConstant + offset[2];

                // Create the atom and push it to the stack
                buildAtom(position);
            }
        }
    }
}
Exemplo n.º 5
0
/**
 * @brief Constructs the Simple Cubic System Builder
 *
 * @param data Lattice data
 */
SCSystemBuilder::SCSystemBuilder(const LatticeData& data) :
    SystemBuilder(),
    latticeData_(data)
{
    if(data.latticeConstant <= 0)
    {
        BOOST_THROW_EXCEPTION(ArgumentOutOfRangeException()
                              << errinfo("Lattice constant must be strictly positive"));
    }
}
Exemplo n.º 6
0
void SslClient::socketError(QAbstractSocket::SocketError err)
{
    if( lastError().isEmpty() ) {
        QString output;
        QDebug errinfo(&output);
        errinfo << err;
        setIOError(output);
    }
    handleDisconnectError(err);
}
Exemplo n.º 7
0
int FFMpegDecoder::my_get_buffer(struct AVCodecContext *c, AVFrame *pic) 
{
  int ret = avcodec_default_get_buffer(c, pic);
  if (ret)
	  errinfo("avcodec_default_get_buffer failed!\n");

  uint64_t *pts = (uint64_t*)av_malloc(sizeof(uint64_t));
  *pts = my_video_pkt_pts;
  pic->opaque = pts;
  return ret;
}
Exemplo n.º 8
0
int FFMpegEncoder::encodeVideoFrame(AVFrame *decodedFrame,unsigned char* encodeBuf,int encodeBufSize)
{
	if (pVideoCodecCtx->me_threshold == 0)
		decodedFrame->pict_type = 0;

	videoEncodedSize = avcodec_encode_video(pVideoCodecCtx,encodeBuf,encodeBufSize, decodedFrame);
	if (videoEncodedSize < 0)
		errinfo("encode error!");

	return videoEncodedSize;
}
Exemplo n.º 9
0
unsigned char* FFMpegDecoder::getReSampledAudioSamples(int dstSize)
{
	if (resampler == NULL)
	{
		errinfo("resampler is NULL!");
		return NULL;
	}
		

	//write enough samples into fifo
	while (fifoResampleResult->getSize() < dstSize)
	{
		
		AVPacket *pkt = readPacket();
		if (pkt == NULL)
		{
			int fifoSize = fifoResampleResult->getSize();
			fifoResampleResult->read(resampleChunk,fifoSize);
			memset(resampleChunk+fifoSize,0,dstSize-fifoSize);
			break;
		}
		
		if (pkt->stream_index != audioStream)
			continue;
		
		//decode,resample,and write to fifo
		SimpleBuf *samples = decodeAudio();
		if (samples == NULL)	//decode error
			continue;

		int nb_resampled = resampler->resample((short*)samples->data,(short*)resampleChunk,samples->dataSize);
		fifoResampleResult->write(resampleChunk,nb_resampled);
	}	
	
	fifoResampleResult->read(resampleChunk,dstSize);
	return  resampleChunk;
}
Exemplo n.º 10
0
AVInfo* FFMpegDecoder::openFile(char* fileName)
{
	
	int ret = av_open_input_file(&pFormatCtx, fileName, NULL, 0, NULL);

	FILE *f = fopen(fileName,"rb");
	if (f != NULL)
	{
		fseek(f,0,SEEK_END);
		long offset = ftell(f);
		printf("file size=%d\n",offset);
		fclose(f);
	}
	if (ret != 0)
	{
		errinfo("av_open_input_file failed!");
		return NULL;
	}
		

	ret = av_find_stream_info(pFormatCtx);
	/*
    if( ret < 0 )
        return NULL;
	*/
	dump_format(pFormatCtx, 0, (char*)fileName, false);

    for(unsigned int i=0; i<pFormatCtx->nb_streams; i++)
	{
        if( pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO) 
            videoStream = i;
        else if( pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO) 
            audioStream = i;
	}
    if( videoStream == -1 && audioStream == -1 )
        return NULL;
	//
	

	if( videoStream >= 0 )
	{
		pVideoCodecCtx = pFormatCtx->streams[videoStream]->codec;
		pVideoCodec = avcodec_find_decoder(pVideoCodecCtx->codec_id);
		if( pVideoCodec == NULL )
			return NULL;
/*
		if( pVideoCodec->capabilities & CODEC_CAP_TRUNCATED )
			pVideoCodecCtx->flags |= CODEC_FLAG_TRUNCATED;
*/
		if(avcodec_open(pVideoCodecCtx, pVideoCodec)<0)
			return NULL;

		pVideoCodecCtx->get_buffer = &FFMpegDecoder::my_get_buffer;
		pVideoCodecCtx->release_buffer = &FFMpegDecoder::my_release_buffer;
	}
	if( audioStream >= 0 )
	{
		pAudioCodecCtx = pFormatCtx->streams[audioStream]->codec;
		pAudioCodec = avcodec_find_decoder(pAudioCodecCtx->codec_id);
		if( pAudioCodec == NULL )
		{
			errinfo("audio stream found but no decoder");
			audioStream = -1;
		}
		else
		{
			if(avcodec_open(pAudioCodecCtx, pAudioCodec)<0)
			return NULL;
		}
	}

	mediaInfo.videoStreamIdx = videoStream;
	mediaInfo.audioStreamIdx = audioStream;
	mediaInfo.fileSize = pFormatCtx->file_size;
	mediaInfo.durationSec = pFormatCtx->duration / 1000000;
	
	

	if (audioStream >= 0)
	{
		mediaInfo.audioBitrate = pAudioCodecCtx->bit_rate;
		mediaInfo.audioChannels = pAudioCodecCtx->channels;
		mediaInfo.audioSampleRate = pAudioCodecCtx->sample_rate;
		mediaInfo.audioTimeBase = av_q2d(pFormatCtx->streams[audioStream]->time_base);
		mediaInfo.audioBitsPerSample = 16;

		audioBytesPerSec = pAudioCodecCtx->sample_rate * pAudioCodecCtx->channels * 2;
		audioPtsSec = 0;
	}

	if (videoStream >= 0)
	{
		mediaInfo.videoBitrate = pFormatCtx->file_size / mediaInfo.durationSec * 8;
		mediaInfo.videoWidth = pVideoCodecCtx->width;
		mediaInfo.videoHeight = pVideoCodecCtx->height;
		//mediaInfo.videoFrameRate = av_q2d(pFormatCtx->streams[videoStream]->r_frame_rate);
		mediaInfo.videoTimeBase = av_q2d(pFormatCtx->streams[videoStream]->time_base);
		mediaInfo.videoFrameCount = pFormatCtx->streams[videoStream]->nb_frames;
		mediaInfo.videoPixFmt = pFormatCtx->streams[videoStream]->codec->pix_fmt;
	}

	
	return &mediaInfo;
}