Пример #1
0
bool AVInfo::loadFile(const QString &fileName)
{
	_file_name = fileName;
	//deprecated
	// Open an input stream and read the header. The codecs are not opened.
	//if(av_open_input_file(&_format_context, _file_name.toLocal8Bit().constData(), NULL, 0, NULL)) {
	if (avformat_open_input(&_format_context, qPrintable(_file_name), NULL, NULL)) {
	//if (avformat_open_input(&formatCtx, qPrintable(filename), NULL, NULL)) {
		qDebug("Can't open video");
		return false;
	}
    _format_context->flags |= AVFMT_FLAG_GENPTS;
	//deprecated
	//if(av_find_stream_info(_format_context)<0) {
	if (avformat_find_stream_info(_format_context, NULL)<0) {
		qDebug("Can't find stream info");
		return false;
	}

	//a_codec_context = _format_context->streams[audioStream()]->codec;
	//v_codec_context = _format_context->streams[videoStream()]->codec;
	findAVCodec();

	AVCodec *aCodec = avcodec_find_decoder(a_codec_context->codec_id);
	if(aCodec) {
		if(avcodec_open2(a_codec_context, aCodec, NULL)<0) {
			qDebug("open audio codec failed");
		}
	} else {
		qDebug("Unsupported audio codec. id=%d.", v_codec_context->codec_id);
	}
	AVCodec *vCodec = avcodec_find_decoder(v_codec_context->codec_id);
	if(!vCodec) {
		qDebug("Unsupported video codec. id=%d.", v_codec_context->codec_id);
		return false;
	}
	////v_codec_context->time_base = (AVRational){1,30};
	//avcodec_open(v_codec_context, vCodec) //deprecated
	if(avcodec_open2(v_codec_context, vCodec, NULL)<0) {
		qDebug("open video codec failed");
		return false;
	}
	return true;
}
Пример #2
0
bool AVDemuxer::loadFile(const QString &fileName)
{
    close();
    qDebug("all closed and reseted");
    _file_name = fileName;
    //deprecated
    // Open an input stream and read the header. The codecs are not opened.
    //if(av_open_input_file(&format_context, _file_name.toLocal8Bit().constData(), NULL, 0, NULL)) {

    //alloc av format context
    if(!format_context)
        format_context = avformat_alloc_context();

    //install interrupt callback
    format_context->interrupt_callback.callback = __interrupt_cb;
    format_context->interrupt_callback.opaque = this;

    qDebug("avformat_open_input: format_context:'%p', url:'%s'...",format_context, qPrintable(_file_name));

    //start timeout timer and timeout
    __interrupt_timer.start();

    int ret = avformat_open_input(&format_context, qPrintable(_file_name), NULL, NULL);

    //invalidate the timer
    __interrupt_timer.invalidate();

    qDebug("avformat_open_input: url:'%s' ret:%d",qPrintable(_file_name), ret);

    if (ret < 0) {
    //if (avformat_open_input(&format_context, qPrintable(filename), NULL, NULL)) {
        qWarning("Can't open media: %s", av_err2str(ret));
        return false;
    }
    format_context->flags |= AVFMT_FLAG_GENPTS;
    //deprecated
    //if(av_find_stream_info(format_context)<0) {
    //TODO: avformat_find_stream_info is too slow, only useful for some video format
    ret = avformat_find_stream_info(format_context, NULL);
    if (ret < 0) {
        qWarning("Can't find stream info: %s", av_err2str(ret));
        return false;
    }

    //a_codec_context = format_context->streams[audioStream()]->codec;
    //v_codec_context = format_context->streams[videoStream()]->codec;
    findAVCodec();

    bool _has_audio = a_codec_context != 0;
    if (a_codec_context) {
        AVCodec *aCodec = avcodec_find_decoder(a_codec_context->codec_id);
        if (aCodec) {
            ret = avcodec_open2(a_codec_context, aCodec, NULL);
            if (ret < 0) {
                _has_audio = false;
                qWarning("open audio codec failed: %s", av_err2str(ret));
            }
        } else {
            _has_audio = false;
            qDebug("Unsupported audio codec. id=%d.", a_codec_context->codec_id);
        }
    }
    if (master_clock->isClockAuto()) {
        qDebug("auto select clock: audio > external");
        if (!_has_audio) {
            qWarning("No audio found or audio not supported. Using ExternalClock");
            master_clock->setClockType(AVClock::ExternalClock);
        } else {
            qDebug("Using AudioClock");
            master_clock->setClockType(AVClock::AudioClock);
        }
    }

    bool _has_vedio = v_codec_context != 0;
    if (v_codec_context) {
        AVCodec *vCodec = avcodec_find_decoder(v_codec_context->codec_id);
        if(!vCodec) {
            qWarning("Unsupported video codec. id=%d.", v_codec_context->codec_id);
            _has_vedio = false;
        }
        ////v_codec_context->time_base = (AVRational){1,30};
        //avcodec_open(v_codec_context, vCodec) //deprecated
        ret = avcodec_open2(v_codec_context, vCodec, NULL);
        if (ret < 0) {
            qWarning("open video codec failed: %s", av_err2str(ret));
            _has_vedio = false;
        } else {
            if (vCodec->capabilities & CODEC_CAP_DR1)
                v_codec_context->flags |= CODEC_FLAG_EMU_EDGE;
        }
        //if (hurry_up) {
// 					codec_ctx->skip_frame = AVDISCARD_NONREF;
            //codec_ctx->skip_loop_filter = codec_ctx->skip_idct = AVDISCARD_ALL;
            //codec_ctx->flags2 |= CODEC_FLAG2_FAST;
        //}
        //else {
            /*codec_ctx->skip_frame =*/ v_codec_context->skip_loop_filter = v_codec_context->skip_idct = AVDISCARD_DEFAULT;
            v_codec_context->flags2 &= ~CODEC_FLAG2_FAST;
        //}
            bool skipframes = false;
            v_codec_context->skip_frame = skipframes ? AVDISCARD_NONREF : AVDISCARD_DEFAULT;
    }
    started_ = false;
    return _has_audio || _has_vedio;
}