org.bytedeco.javacpp.avutil.AVDictionary Java Examples

The following examples show how to use org.bytedeco.javacpp.avutil.AVDictionary. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: GrabberTmplate.java    From easyCV with Apache License 2.0 6 votes vote down vote up
/**
 * 查找并尝试打开解码器
 * @return 
 */
protected AVCodecContext findAndOpenCodec(AVCodecContext pCodecCtx) {
	// Find the decoder for the video stream
	AVCodec pCodec = avcodec_find_decoder(pCodecCtx.codec_id());
	if (pCodec == null) {
		System.err.println("Codec not found");
		throw new CodecNotFoundExpception("Codec not found");
	}
	AVDictionary optionsDict = null;
	// Open codec
	if (avcodec_open2(pCodecCtx, pCodec, optionsDict) < 0) {
		System.err.println("Could not open codec");
		throw new CodecNotFoundExpception("Could not open codec"); // Could not open codec
	}
	return pCodecCtx;
}
 
Example #2
Source File: FFmpegRecorder.java    From easyCV with Apache License 2.0 6 votes vote down vote up
/**
 * 查找并尝试打开解码器
 * @return 
 */
protected AVCodecContext findAndOpenCodec(AVCodecContext pCodecCtx) {
	// Find the decoder for the video stream
	AVCodec pCodec = avcodec_find_decoder(pCodecCtx.codec_id());
	if (pCodec == null) {
		System.err.println("Codec not found");
		throw new CodecNotFoundExpception("Codec not found");
	}
	AVDictionary optionsDict = null;
	// Open codec
	if (avcodec_open2(pCodecCtx, pCodec, optionsDict) < 0) {
		System.err.println("Could not open codec");
		throw new CodecNotFoundExpception("Could not open codec"); // Could not open codec
	}
	return pCodecCtx;
}
 
Example #3
Source File: TestPusher.java    From easyCV with Apache License 2.0 6 votes vote down vote up
/**
 * 查找并尝试打开解码器
 * @return 
 */
protected AVCodecContext findAndOpenCodec(AVCodecContext pCodecCtx) {
	// Find the decoder for the video stream
	AVCodec pCodec = avcodec_find_decoder(pCodecCtx.codec_id());
	if (pCodec == null) {
		System.err.println("Codec not found");
		throw new CodecNotFoundExpception("Codec not found");
	}
	AVDictionary optionsDict = null;
	// Open codec
	if (avcodec_open2(pCodecCtx, pCodec, optionsDict) < 0) {
		System.err.println("Could not open codec");
		throw new CodecNotFoundExpception("Could not open codec"); // Could not open codec
	}
	return pCodecCtx;
}
 
Example #4
Source File: GrabberTemplate4.java    From easyCV with Apache License 2.0 6 votes vote down vote up
/**
 * 查找并尝试打开解码器
 * @return 
 */
protected AVCodecContext findAndOpenCodec(AVCodecContext codecCtx) {
	// Find the decoder for the video stream
	AVCodec pCodec = avcodec_find_decoder(codecCtx.codec_id());
	if (pCodec == null) {
		Console.err("Codec not found!");
		throw new CodecNotFoundExpception("Codec not found!");
	}
	AVDictionary optionsDict = null;
	// Open codec
	if (avcodec_open2(codecCtx, pCodec, optionsDict) < 0) {
		Console.err("Could not open codec!");
		throw new CodecNotFoundExpception("Could not open codec!"); // Could not open codec
	}
	return codecCtx;
}
 
Example #5
Source File: TestPusher.java    From easyCV with Apache License 2.0 5 votes vote down vote up
/**
 * 检索流信息
 * 
 * @param pFormatCtx
 * @return
 */
protected AVFormatContext findStreamInfo(AVFormatContext pFormatCtx) throws StreamInfoNotFoundException {
	// 解决rtsp默认udp丢帧导致检索时间过长问题
	AVDictionary options = new AVDictionary();
	av_dict_set(options, "rtsp_transport", "tcp", 0);
	// 解决rtmp检索时间过长问题
	// 限制最大读取缓存
	pFormatCtx.probesize(500 * 1024);// 设置500k能保证高清视频也能读取到关键帧
	// 限制avformat_find_stream_info最大持续时长,设置成3秒
	pFormatCtx.max_analyze_duration(3 * AV_TIME_BASE);
	if (avformat_find_stream_info(pFormatCtx, options) >= 0) {
		return pFormatCtx;
	}
	throw new StreamInfoNotFoundException("Didn't retrieve stream information");
}
 
Example #6
Source File: GrabberTemplate4.java    From easyCV with Apache License 2.0 5 votes vote down vote up
/**
 * 检索流信息(rtsp/rtmp检索时间过长问题解决)
 * @param pFormatCtx
 * @return
 */
protected AVFormatContext findStreamInfo(AVFormatContext formatCtx,AVDictionary options) throws StreamInfoNotFoundException{
	if (avformat_find_stream_info(formatCtx, options==null?(AVDictionary)null:options)>= 0) {
		return formatCtx;
	}
	throw new StreamInfoNotFoundException("Didn't retrieve stream information");
}
 
Example #7
Source File: GrabberTemplate4.java    From easyCV with Apache License 2.0 5 votes vote down vote up
/**
 * 查找并尝试打开解码器
 * @return 
 */
protected AVCodecContext findAndOpenCodec(AVFormatContext formatCtx ,int videoStreamIndex) {
	// Find codec param
	AVCodecParameters codecParameters=findVideoParameters(formatCtx ,videoStreamIndex);

	// Find the decoder for the video stream
	AVCodec codec = avcodec_find_decoder(codecParameters.codec_id());

	if (codec == null) {
		Console.err("Codec not found!");
		throw new CodecNotFoundExpception("Codec not found!");
	}
	AVDictionary optionsDict = null;
	AVCodecContext codecCtx = avcodec_alloc_context3(codec);

	//convert to codecContext
	if(avcodec_parameters_to_context( codecCtx, codecParameters)<0) {
		Console.err("Could not convert parameter to codecContext!");
		throw new CodecNotFoundExpception("Could not convert parameter to codecContext!"); // Could not open codec
	}

	// Open codec
	if (avcodec_open2(codecCtx, codec, optionsDict) < 0) {
		Console.err("Could not open codec!");
		throw new CodecNotFoundExpception("Could not open codec!"); // Could not open codec
	}
	
	return codecCtx;
}
 
Example #8
Source File: FFMpegVideoDecoder.java    From cineast with MIT License 4 votes vote down vote up
/**
 * Initializes the audio decoding part of FFMPEG.
 *
 * @param config The {@link DecoderConfig} used for configuring the {@link FFMpegVideoDecoder}.
 * @return True if a) audio decoder was initialized, b) number of channels is smaller than zero (no audio) or c) audio is unavailable or unsupported, false if initialization failed due to technical reasons.
 */
private boolean initAudio(DecoderConfig config) {
    /* Read decoder configuration. */
    int samplerate = config.namedAsInt(CONFIG_SAMPLERATE_PROPERTY, CONFIG_SAMPLERATE_DEFAULT);
    int channels = config.namedAsInt(CONFIG_CHANNELS_PROPERTY, CONFIG_CHANNELS_DEFAULT);
    long channellayout = av_get_default_channel_layout(channels);

    /* If number of channels is smaller or equal than zero; return true (no audio decoded). */
    if (channels <= 0) {
        LOGGER.info("Channel setting is smaller than zero. Continuing without audio!");
        this.audioComplete.set(true);
        return true;
    }

    /* Find the best frames stream. */
    final AVCodec codec = av_codec_next((AVCodec)null);
    this.audioStream = av_find_best_stream(this.pFormatCtx, AVMEDIA_TYPE_AUDIO,-1, -1, codec, 0);
    if (this.audioStream < 0) {
        LOGGER.warn("Couldn't find a supported audio stream. Continuing without audio!");
        this.audioComplete.set(true);
        return true;
    }

    /* Allocate new codec-context. */
    this.pCodecCtxAudio = avcodec_alloc_context3(codec);
    avcodec_parameters_to_context(this.pCodecCtxAudio, this.pFormatCtx.streams(this.audioStream).codecpar());

    /* Open the code context. */
    if (avcodec_open2(this.pCodecCtxAudio, codec, (AVDictionary)null) < 0) {
        LOGGER.error("Could not open audio codec. Continuing without audio!");
        this.audioComplete.set(true);
        return true;
    }

    /* Allocate the re-sample context. */
    this.swr_ctx = swr_alloc_set_opts(null, channellayout, TARGET_FORMAT, samplerate, this.pCodecCtxAudio.channel_layout(), this.pCodecCtxAudio.sample_fmt(), this.pCodecCtxAudio.sample_rate(), 0, null);
    if(swr_init(this.swr_ctx) < 0) {
        this.swr_ctx = null;
        LOGGER.warn("Could not open re-sample context - original format will be kept!");
    }

    /* Initialize decoded and resampled frame. */
    this.resampledFrame = av_frame_alloc();
    if (this.resampledFrame == null) {
        LOGGER.error("Could not allocate frame data structure for re-sampled data.");
        return false;
    }

    /* Initialize out-frame. */
    this.resampledFrame = av_frame_alloc();
    this.resampledFrame.channel_layout(channellayout);
    this.resampledFrame.sample_rate(samplerate);
    this.resampledFrame.channels(channels);
    this.resampledFrame.format(TARGET_FORMAT);

    /* Initialize the AudioDescriptor. */
    final AVRational timebase = this.pFormatCtx.streams(this.audioStream).time_base();
    final long duration = (1000L * timebase.num() * this.pFormatCtx.streams(this.audioStream).duration()/timebase.den());
    if (this.swr_ctx == null) {
        this.audioDescriptor = new AudioDescriptor(this.pCodecCtxAudio.sample_rate(), this.pCodecCtxAudio.channels(), duration);
    } else {
        this.audioDescriptor = new AudioDescriptor(this.resampledFrame.sample_rate(), this.resampledFrame.channels(), duration);
    }

    /* Completed initialization. */
    return true;
}
 
Example #9
Source File: Coder.java    From JavaAV with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Initializes the {@code Coder} with codec options that may contain specific
 * codec parameters.
 *
 * @param options codec options.
 *
 * @throws JavaAVException if {@code Coder} could not be opened.
 */
public void open(Map<String, String> options) throws JavaAVException {
	if (state == State.Opened) {
		logger.warn("Trying to open an already opened Coder. Aborted.");
		return;
	}
	if (codec == null)
		throw new JavaAVException("Codec is null. Aborted.");

	if (avContext == null)
		avContext = avcodec_alloc_context3(codec.getCodec());

	if (avContext == null)
		throw new JavaAVException("No codec context available for codec " + codec.getName());

	// set configuration parameters
	if (pixelFormat != null) {
		avContext.pix_fmt(pixelFormat.value());
	}
	if (sampleFormat != null) {
		int sampleBitSize = av_get_bytes_per_sample(sampleFormat.value()) * 8;
		avContext.sample_fmt(sampleFormat.value());
		avContext.bits_per_raw_sample(sampleBitSize);
	}
	if (imageWidth > 0) {
		avContext.width(imageWidth);
	}
	if (imageHeight > 0) {
		avContext.height(imageHeight);
	}
	if (gopSize > 0) {
		avContext.gop_size(gopSize);
	}
	if (audioChannels > 0) {
		avContext.channels(audioChannels);
		avContext.channel_layout(av_get_default_channel_layout(audioChannels));
	}
	if (sampleRate > 0) {
		avContext.sample_rate(sampleRate);
		avContext.time_base().num(1).den(sampleRate);
	}
	if (frameRate > 0) {
		avContext.time_base(av_inv_q(av_d2q(frameRate, 1001000)));
	}
	if (bitrate > 0) {
		avContext.bit_rate(bitrate);
	}
	if (profile > 0) {
		avContext.profile(profile);
	}
	if (quality > -10) {
		avContext.flags(avContext.flags() | avcodec.CODEC_FLAG_QSCALE);
		avContext.global_quality((int) Math.round(FF_QP2LAMBDA * quality));
	}
	for (CodecFlag flag : flags)
		avContext.flags(avContext.flags() | flag.value());

	AVDictionary avDictionary = new AVDictionary(null);

	if (getQuality() >= 0)
		av_dict_set(avDictionary, "crf", getQuality() + "", 0);

	if (options != null) {
		for (Entry<String, String> e : options.entrySet()) {
			av_dict_set(avDictionary, e.getKey(), e.getValue(), 0);
		}
	}

	if (codec.open(avDictionary, avContext) < 0)
		throw new JavaAVException("Could not open codec.");

	av_dict_free(avDictionary);

	avFrame = avcodec_alloc_frame();

	if (avFrame == null)
		throw new JavaAVException("Could not allocate frame.");

	avPacket = new AVPacket();

	state = State.Opened;
}
 
Example #10
Source File: Codec.java    From JavaAV with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Initializes the {@code Codec} with a dictionary that may contain specific codec
 * parameters and a previously created codec context.
 *
 * @param avDictionary dictionary with codec parameters.
 * @param avContext    codec context.
 *
 * @return zero on success, a negative value on error.
 *
 * @throws JavaAVException if the codec could not be opened.
 */
int open(AVDictionary avDictionary, AVCodecContext avContext) throws JavaAVException {
	if (avContext == null)
		throw new JavaAVException("Could not open codec. Codec context is null.");

	this.avContext = avContext;

	return avcodec_open2(avContext, avCodec, avDictionary);
}
 
Example #11
Source File: Codec.java    From JavaAV with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Initializes the {@code Codec} with a dictionary that may contain specific
 * codec parameters.
 *
 * @param avDictionary dictionary with codec parameters.
 *
 * @return zero on success, a negative value on error.
 *
 * @throws JavaAVException if the codec could not be opened.
 */
int open(AVDictionary avDictionary) throws JavaAVException {
	AVCodecContext avContext = avcodec_alloc_context3(avCodec);

	return open(avDictionary, avContext);
}