Beispiel #1
0
bool Capture::init()
{
	if (mVideoDevice.empty())
	{
		sgct::MessageHandler::instance()->print(sgct::MessageHandler::NOTIFY_ERROR, "No video device specified!\n");
		cleanup();
		return false;
	}

	initFFmpeg();
	setupOptions();

	// --------------------------------------------------
	//check out https://ffmpeg.org/ffmpeg-devices.html
	// --------------------------------------------------
	AVInputFormat * iformat;
	std::string inputName;

#ifdef __WIN32__
	iformat = av_find_input_format("dshow");
	inputName = "video=" + mVideoDevice;
#elif defined __APPLE__
	iformat = av_find_input_format("avfoundation");
	inputName = mVideoDevice;
#else  //linux NOT-Tested
	iformat = av_find_input_format("video4linux2");
	inputName = mVideoDevice;
#endif

	if (avformat_open_input(&mFMTContext, inputName.c_str(), iformat, &mOptions) < 0)
	{
		sgct::MessageHandler::instance()->print(sgct::MessageHandler::NOTIFY_ERROR, "Could not open capture input!\n");
		cleanup();
		return false;
	}

	/* retrieve stream information */
	if (avformat_find_stream_info(mFMTContext, nullptr) < 0)
	{
		sgct::MessageHandler::instance()->print(sgct::MessageHandler::NOTIFY_ERROR, "Could not find stream information!\n");
		cleanup();
		return false;
	}

	if (!initVideoStream())
	{
		cleanup();
		return false;
	}

	//dump format info to console
	av_dump_format(mFMTContext, 0, inputName.c_str(), 0);

	if (mVideoCodecContext)
	{
		if (!allocateVideoDecoderData(mVideoCodecContext->pix_fmt))
		{
			cleanup();
			return false;
		}
	}

	/* initialize packet, set data to nullptr, let the demuxer fill it */
	av_init_packet(&mPkt);
	mPkt.data = nullptr;
	mPkt.size = 0;

	//success
	mInited = true;
	return true;
}
status_t SoftFFmpegVideo::initDecoder() {
    status_t status;
    

    status = initFFmpeg();
    if (status != OK)
        return status;

    mCtx = avcodec_alloc_context3(NULL);
    if (!mCtx)
    {
        LOGE("avcodec_alloc_context failed.");
        return OMX_ErrorInsufficientResources;
    }

    mCtx->codec_type = AVMEDIA_TYPE_VIDEO;
    switch (mMode) {
    case MODE_H264:
        mCtx->codec_id = CODEC_ID_H264;
        break;
    case MODE_MPEG4:
        mCtx->codec_id = CODEC_ID_MPEG4;
        break;
    case MODE_MPEG2:
        mCtx->codec_id = CODEC_ID_MPEG2VIDEO;
        break;
    case MODE_H263:
        mCtx->codec_id = CODEC_ID_H263;
        // TODO, which?
        //mCtx->codec_id = CODEC_ID_H263P;
        //mCtx->codec_id = CODEC_ID_H263I;
        break;
    case MODE_VC1:
        mCtx->codec_id = CODEC_ID_VC1;
        break;
    default:
        CHECK(!"Should not be here. Unsupported codec");
        break;
    }

    mCtx->codec = avcodec_find_decoder(mCtx->codec_id);
    if (!mCtx->codec)
    {
        LOGE("find codec failed");
        return OMX_ErrorNotImplemented;
    }

    setAVCtxToDefault(mCtx, mCtx->codec);

    mCtx->extradata_size = 0;
    mCtx->extradata = NULL;
    mCtx->width = mWidth;
    mCtx->height = mHeight;

#if 0
    // FIXME, defer to open? ref: OMXCodec.cpp:setVideoOutputFormat
    err = avcodec_open2(mCtx, mCtx->codec, NULL);
    if (err < 0) {
        LOGE("ffmpeg video decoder failed to  initialize. (%d)", err);
        return OMX_ErrorUndefined;
    }
#endif

    return OMX_ErrorNone;
}