org.bytedeco.javacpp.avcodec.AVCodec Java Examples

The following examples show how to use org.bytedeco.javacpp.avcodec.AVCodec. 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: 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 #6
Source File: Codec.java    From JavaAV with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Create a new encoder with specified codec id.
 *
 * @param codecId the codec id.
 *
 * @return a new encoder.
 *
 * @throws JavaAVException if encoder could not be created.
 */
public static Codec getEncoderById(CodecID codecId) throws JavaAVException {
	if (codecId == null)
		throw new NullPointerException("CodecID is null.");

	AVCodec avCodec = avcodec_find_encoder(codecId.value());

	if (avCodec == null || avCodec.isNull())
		throw new JavaAVException("Encoder not found: " + codecId.toString());

	Codec codec = new Codec();
	codec.avCodec = avCodec;

	return codec;
}
 
Example #7
Source File: Codec.java    From JavaAV with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Create a new decoder with specified codec id.
 *
 * @param codecId the codec id.
 *
 * @return a new decoder.
 *
 * @throws JavaAVException if decoder could not be created.
 */
public static Codec getDecoderById(CodecID codecId) throws JavaAVException {
	if (codecId == null)
		throw new NullPointerException("CodecID is null.");

	AVCodec avCodec = avcodec_find_decoder(codecId.value());

	if (avCodec == null || avCodec.isNull())
		throw new JavaAVException("Decoder not found: " + codecId.toString());

	Codec codec = new Codec();
	codec.avCodec = avCodec;

	return codec;
}
 
Example #8
Source File: Codec.java    From JavaAV with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Create a new encoder with specified codec name.
 *
 * @param avCodecName the codec name.
 *
 * @return a new encoder.
 *
 * @throws JavaAVException if encoder could not be created.
 */
public static Codec getEncoderByName(String avCodecName) throws JavaAVException {
	if (avCodecName == null || avCodecName.isEmpty())
		throw new NullPointerException("Codec name is null or empty.");

	AVCodec avCodec = avcodec_find_encoder_by_name(avCodecName);

	if (avCodec == null || avCodec.isNull())
		throw new JavaAVException("Encoder not found: " + avCodecName);

	Codec codec = new Codec();
	codec.avCodec = avCodec;

	return codec;
}
 
Example #9
Source File: Codec.java    From JavaAV with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Create a new decoder with specified codec name.
 *
 * @param avCodecName the codec name.
 *
 * @return a new decoder.
 *
 * @throws JavaAVException if decoder could not be created.
 */
public static Codec getDecoderByName(String avCodecName) throws JavaAVException {
	if (avCodecName == null || avCodecName.isEmpty())
		throw new NullPointerException("Codec name is null or empty.");

	AVCodec avCodec = avcodec_find_decoder_by_name(avCodecName);

	if (avCodec == null || avCodec.isNull())
		throw new JavaAVException("Decoder not found: " + avCodecName);

	Codec codec = new Codec();
	codec.avCodec = avCodec;

	return codec;
}
 
Example #10
Source File: Codec.java    From JavaAV with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Get all names of codecs that are compiled into FFmpeg.
 *
 * @return short codec names that the current FFmpeg version supports.
 */
public static String[] getInstalledCodecs() {
	Set<String> names = new TreeSet<String>();

	AVCodec codec = null;
	while ((codec = av_codec_next(codec)) != null) {
		String shortName = codec.name().getString();
		String type = MediaType.byId(codec.type()).toString().substring(0, 1);
		String longName = codec.long_name().getString();

		names.add(String.format("%-17s [%s] %s", shortName, type, longName));
	}

	return names.toArray(new String[0]);
}
 
Example #11
Source File: TestFFmpeg.java    From easyCV with Apache License 2.0 4 votes vote down vote up
/**
	 * 把YUVJ420P数据编码保存成jpg图片
	 * @param pFrame -YUVJ420P数据
	 * @param index -序号
	 * @return
	 */
	private static int saveImg(AVFrame pFrame, int index,String out_file) {
		int width= pFrame.width(), height= pFrame.height();
		// 分配AVFormatContext对象
		AVFormatContext pFormatCtx = avformat_alloc_context();
		// 设置输出文件格式
		pFormatCtx.oformat(av_guess_format("PNG", null, null));
		if (pFormatCtx.oformat() == null) {
			return -1;
		}
		// 创建并初始化一个和该url相关的AVIOContext
		AVIOContext pb = new AVIOContext();
		if (avio_open(pb, out_file, AVIO_FLAG_READ_WRITE) < 0) {
			System.err.println("Couldn't open output file.");
			return -1;
		}
		pFormatCtx.pb(pb);
		// 构建一个新stream
		AVCodec codec = null;
		AVStream pAVStream = avformat_new_stream(pFormatCtx, codec);
		if (pAVStream == null) {
			return -1;
		}
		// 设置该stream的信息
		AVCodecContext pCodecCtx = pAVStream.codec();
		pCodecCtx.codec_id(pFormatCtx.oformat().video_codec());
		pCodecCtx.codec_type(AVMEDIA_TYPE_VIDEO);
		pCodecCtx.pix_fmt(pFrame.format());
		pCodecCtx.width(width);
		pCodecCtx.height(height);
		pCodecCtx.time_base().num(1);
		pCodecCtx.time_base().den(25);
		// Begin Output some information
		av_dump_format(pFormatCtx, 0, out_file, 1);
		// End Output some information
		// 查找解码器
		AVCodec pCodec = avcodec_find_encoder(pCodecCtx.codec_id());
		if (pCodec == null) {
			System.err.println("Codec not found.");
			return -1;
		}
		// 设置pCodecCtx的解码器为pCodec
		if (avcodec_open2(pCodecCtx, pCodec, (PointerPointer) null) < 0) {
			System.err.println("Could not open codec.");
			return -1;
		}

		// Write Header
		avformat_write_header(pFormatCtx, (PointerPointer) null);

		int y_size = width * height;

		// 给AVPacket分配足够大的空间
		AVPacket pkt = new AVPacket();
		av_new_packet(pkt, y_size * 3);
		//
		int[] got_picture_arr = { 0 };
//		IntPointer got_picture = new IntPointer(got_picture_arr);
		int ret = avcodec_encode_video2(pCodecCtx, pkt, pFrame, got_picture_arr);
		if (ret < 0) {
			System.err.println("Encode Error.\n");
			return -1;
		}
		if (pkt != null && !pkt.isNull()) {
			// pkt.stream_index = pAVStream->index;
			ret = av_write_frame(pFormatCtx, pkt);
		}
		// Write Trailer
		if (av_write_trailer(pFormatCtx) >= 0) {
			System.err.println("Encode Successful.");
		}

		av_free_packet(pkt);

		if (pAVStream != null) {
			avcodec_close(pAVStream.codec());
		}

		if (pFormatCtx != null) {
			avio_close(pFormatCtx.pb());
			avformat_free_context(pFormatCtx);
		}

		return 0;
	}
 
Example #12
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 #13
Source File: Codec.java    From JavaAV with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Get the codec.
 *
 * @return the codec.
 */
AVCodec getCodec() {
	return avCodec;
}