Search in sources :

Example 1 with ShortPointer

use of com.googlecode.javacpp.ShortPointer in project VideoRecorder by qdrzwd.

the class NewFFmpegFrameRecorder method record.

@Override
public boolean record(int sampleRate, Buffer... samples) throws Exception {
    if (audioSt == null) {
        throw new Exception("No audio output stream (Is audioChannels > 0 and has start() been called?)");
    }
    int inputSize = samples[0].limit() - samples[0].position();
    int inputDepth;
    if (sampleRate <= 0) {
        sampleRate = audioC.sample_rate();
    }
    int inputFormat;
    if (samples[0] instanceof ByteBuffer) {
        inputFormat = samples.length > 1 ? AV_SAMPLE_FMT_U8P : AV_SAMPLE_FMT_U8;
        inputDepth = 1;
        for (int i = 0; i < samples.length; i++) {
            ByteBuffer b = (ByteBuffer) samples[i];
            if (samplesIn[i] instanceof BytePointer && samplesIn[i].capacity() >= inputSize && b.hasArray()) {
                ((BytePointer) samplesIn[i]).position(0).put(b.array(), b.position(), inputSize);
            } else {
                samplesIn[i] = new BytePointer(b);
            }
        }
    } else if (samples[0] instanceof ShortBuffer) {
        inputFormat = samples.length > 1 ? AV_SAMPLE_FMT_S16P : AV_SAMPLE_FMT_S16;
        inputDepth = 2;
        for (int i = 0; i < samples.length; i++) {
            ShortBuffer b = (ShortBuffer) samples[i];
            if (samplesIn[i] instanceof ShortPointer && samplesIn[i].capacity() >= inputSize && b.hasArray()) {
                ((ShortPointer) samplesIn[i]).position(0).put(b.array(), samples[i].position(), inputSize);
            } else {
                samplesIn[i] = new ShortPointer(b);
            }
        }
    } else if (samples[0] instanceof IntBuffer) {
        inputFormat = samples.length > 1 ? AV_SAMPLE_FMT_S32P : AV_SAMPLE_FMT_S32;
        inputDepth = 4;
        for (int i = 0; i < samples.length; i++) {
            IntBuffer b = (IntBuffer) samples[i];
            if (samplesIn[i] instanceof IntPointer && samplesIn[i].capacity() >= inputSize && b.hasArray()) {
                ((IntPointer) samplesIn[i]).position(0).put(b.array(), samples[i].position(), inputSize);
            } else {
                samplesIn[i] = new IntPointer(b);
            }
        }
    } else if (samples[0] instanceof FloatBuffer) {
        inputFormat = samples.length > 1 ? AV_SAMPLE_FMT_FLTP : AV_SAMPLE_FMT_FLT;
        inputDepth = 4;
        for (int i = 0; i < samples.length; i++) {
            FloatBuffer b = (FloatBuffer) samples[i];
            if (samplesIn[i] instanceof FloatPointer && samplesIn[i].capacity() >= inputSize && b.hasArray()) {
                ((FloatPointer) samplesIn[i]).position(0).put(b.array(), b.position(), inputSize);
            } else {
                samplesIn[i] = new FloatPointer(b);
            }
        }
    } else if (samples[0] instanceof DoubleBuffer) {
        inputFormat = samples.length > 1 ? AV_SAMPLE_FMT_DBLP : AV_SAMPLE_FMT_DBL;
        inputDepth = 8;
        for (int i = 0; i < samples.length; i++) {
            DoubleBuffer b = (DoubleBuffer) samples[i];
            if (samplesIn[i] instanceof DoublePointer && samplesIn[i].capacity() >= inputSize && b.hasArray()) {
                ((DoublePointer) samplesIn[i]).position(0).put(b.array(), b.position(), inputSize);
            } else {
                samplesIn[i] = new DoublePointer(b);
            }
        }
    } else {
        throw new Exception("Audio samples Buffer has unsupported type: " + samples);
    }
    int ret;
    int outputFormat = audioC.sample_fmt();
    if (samplesConvertCtx == null) {
        samplesConvertCtx = swr_alloc_set_opts(null, audioC.channel_layout(), outputFormat, audioC.sample_rate(), audioC.channel_layout(), inputFormat, sampleRate, 0, null);
        if (samplesConvertCtx == null) {
            throw new Exception("swr_alloc_set_opts() error: Cannot allocate the conversion context.");
        } else if ((ret = swr_init(samplesConvertCtx)) < 0) {
            throw new Exception("swr_init() error " + ret + ": Cannot initialize the conversion context.");
        }
    }
    for (int i = 0; i < samples.length; i++) {
        samplesIn[i].position(samplesIn[i].position() * inputDepth).limit((samplesIn[i].position() + inputSize) * inputDepth);
    }
    int outputChannels = samplesOut.length > 1 ? 1 : audioChannels;
    int outputDepth = av_get_bytes_per_sample(outputFormat);
    int inputChannels = samples.length > 1 ? 1 : audioChannels;
    while (true) {
        int inputCount = (samplesIn[0].limit() - samplesIn[0].position()) / (inputChannels * inputDepth);
        int outputCount = (samplesOut[0].limit() - samplesOut[0].position()) / (outputChannels * outputDepth);
        inputCount = Math.min(inputCount, 2 * (outputCount * sampleRate) / audioC.sample_rate());
        for (int i = 0; i < samples.length; i++) {
            samplesInPtr.put(i, samplesIn[i]);
        }
        for (int i = 0; i < samplesOut.length; i++) {
            samplesOutPtr.put(i, samplesOut[i]);
        }
        if ((ret = swr_convert(samplesConvertCtx, samplesOutPtr, outputCount, samplesInPtr, inputCount)) < 0) {
            throw new Exception("swr_convert() error " + ret + ": Cannot convert audio samples.");
        } else if (ret == 0) {
            break;
        }
        for (int i = 0; i < samples.length; i++) {
            samplesIn[i].position(samplesIn[i].position() + inputCount * inputChannels * inputDepth);
        }
        for (int i = 0; i < samplesOut.length; i++) {
            samplesOut[i].position(samplesOut[i].position() + ret * outputChannels * outputDepth);
        }
        if (samplesOut[0].position() >= samplesOut[0].limit()) {
            frame.nb_samples(audioInputFrameSize);
            avcodec_fill_audio_frame(frame, audioC.channels(), outputFormat, samplesOut[0], samplesOut[0].limit(), 0);
            for (int i = 0; i < samplesOut.length; i++) {
                frame.data(i, samplesOut[i].position(0));
                frame.linesize(i, samplesOut[i].limit());
            }
            frame.quality(audioC.global_quality());
            record(frame);
        }
    }
    return frame.key_frame() != 0;
}
Also used : DoubleBuffer(java.nio.DoubleBuffer) ShortPointer(com.googlecode.javacpp.ShortPointer) FloatPointer(com.googlecode.javacpp.FloatPointer) IntBuffer(java.nio.IntBuffer) IntPointer(com.googlecode.javacpp.IntPointer) BytePointer(com.googlecode.javacpp.BytePointer) DoublePointer(com.googlecode.javacpp.DoublePointer) FloatBuffer(java.nio.FloatBuffer) ByteBuffer(java.nio.ByteBuffer) ShortBuffer(java.nio.ShortBuffer)

Example 2 with ShortPointer

use of com.googlecode.javacpp.ShortPointer in project VideoRecorder by qdrzwd.

the class FFmpegFrameRecorder method record.

@Override
public boolean record(int sampleRate, Buffer... samples) throws Exception {
    if (audioSt == null) {
        throw new Exception("No audio output stream (Is audioChannels > 0 and has start() been called?)");
    }
    int inputSize = samples[0].limit() - samples[0].position();
    int inputFormat;
    int inputDepth;
    if (sampleRate <= 0) {
        sampleRate = audioC.sample_rate();
    }
    if (samples[0] instanceof ByteBuffer) {
        inputFormat = samples.length > 1 ? AV_SAMPLE_FMT_U8P : AV_SAMPLE_FMT_U8;
        inputDepth = 1;
        for (int i = 0; i < samples.length; i++) {
            ByteBuffer b = (ByteBuffer) samples[i];
            if (samplesIn[i] instanceof BytePointer && samplesIn[i].capacity() >= inputSize && b.hasArray()) {
                ((BytePointer) samplesIn[i]).position(0).put(b.array(), b.position(), inputSize);
            } else {
                samplesIn[i] = new BytePointer(b);
            }
        }
    } else if (samples[0] instanceof ShortBuffer) {
        inputFormat = samples.length > 1 ? AV_SAMPLE_FMT_S16P : AV_SAMPLE_FMT_S16;
        inputDepth = 2;
        for (int i = 0; i < samples.length; i++) {
            ShortBuffer b = (ShortBuffer) samples[i];
            if (samplesIn[i] instanceof ShortPointer && samplesIn[i].capacity() >= inputSize && b.hasArray()) {
                ((ShortPointer) samplesIn[i]).position(0).put(b.array(), samples[i].position(), inputSize);
            } else {
                samplesIn[i] = new ShortPointer(b);
            }
        }
    } else if (samples[0] instanceof IntBuffer) {
        inputFormat = samples.length > 1 ? AV_SAMPLE_FMT_S32P : AV_SAMPLE_FMT_S32;
        inputDepth = 4;
        for (int i = 0; i < samples.length; i++) {
            IntBuffer b = (IntBuffer) samples[i];
            if (samplesIn[i] instanceof IntPointer && samplesIn[i].capacity() >= inputSize && b.hasArray()) {
                ((IntPointer) samplesIn[i]).position(0).put(b.array(), samples[i].position(), inputSize);
            } else {
                samplesIn[i] = new IntPointer(b);
            }
        }
    } else if (samples[0] instanceof FloatBuffer) {
        inputFormat = samples.length > 1 ? AV_SAMPLE_FMT_FLTP : AV_SAMPLE_FMT_FLT;
        inputDepth = 4;
        for (int i = 0; i < samples.length; i++) {
            FloatBuffer b = (FloatBuffer) samples[i];
            if (samplesIn[i] instanceof FloatPointer && samplesIn[i].capacity() >= inputSize && b.hasArray()) {
                ((FloatPointer) samplesIn[i]).position(0).put(b.array(), b.position(), inputSize);
            } else {
                samplesIn[i] = new FloatPointer(b);
            }
        }
    } else if (samples[0] instanceof DoubleBuffer) {
        inputFormat = samples.length > 1 ? AV_SAMPLE_FMT_DBLP : AV_SAMPLE_FMT_DBL;
        inputDepth = 8;
        for (int i = 0; i < samples.length; i++) {
            DoubleBuffer b = (DoubleBuffer) samples[i];
            if (samplesIn[i] instanceof DoublePointer && samplesIn[i].capacity() >= inputSize && b.hasArray()) {
                ((DoublePointer) samplesIn[i]).position(0).put(b.array(), b.position(), inputSize);
            } else {
                samplesIn[i] = new DoublePointer(b);
            }
        }
    } else {
        throw new Exception("Audio samples Buffer has unsupported type: " + samples);
    }
    int ret;
    int outputFormat = audioC.sample_fmt();
    if (samplesConvertCtx == null) {
        samplesConvertCtx = swr_alloc_set_opts(null, audioC.channel_layout(), outputFormat, audioC.sample_rate(), audioC.channel_layout(), inputFormat, sampleRate, 0, null);
        if (samplesConvertCtx == null) {
            throw new Exception("swr_alloc_set_opts() error: Cannot allocate the conversion context.");
        } else if ((ret = swr_init(samplesConvertCtx)) < 0) {
            throw new Exception("swr_init() error " + ret + ": Cannot initialize the conversion context.");
        }
    }
    for (int i = 0; i < samples.length; i++) {
        samplesIn[i].position(samplesIn[i].position() * inputDepth).limit((samplesIn[i].position() + inputSize) * inputDepth);
    }
    int outputChannels = samplesOut.length > 1 ? 1 : audioChannels;
    int inputChannels = samples.length > 1 ? 1 : audioChannels;
    int outputDepth = av_get_bytes_per_sample(outputFormat);
    while (true) {
        int inputCount = (samplesIn[0].limit() - samplesIn[0].position()) / (inputChannels * inputDepth);
        int outputCount = (samplesOut[0].limit() - samplesOut[0].position()) / (outputChannels * outputDepth);
        inputCount = Math.min(inputCount, 2 * (outputCount * sampleRate) / audioC.sample_rate());
        for (int i = 0; i < samples.length; i++) {
            samplesInPtr.put(i, samplesIn[i]);
        }
        for (int i = 0; i < samplesOut.length; i++) {
            samplesOutPtr.put(i, samplesOut[i]);
        }
        if ((ret = swr_convert(samplesConvertCtx, samplesOutPtr, outputCount, samplesInPtr, inputCount)) < 0) {
            throw new Exception("swr_convert() error " + ret + ": Cannot convert audio samples.");
        } else if (ret == 0) {
            break;
        }
        for (int i = 0; i < samples.length; i++) {
            samplesIn[i].position(samplesIn[i].position() + inputCount * inputChannels * inputDepth);
        }
        for (int i = 0; i < samplesOut.length; i++) {
            samplesOut[i].position(samplesOut[i].position() + ret * outputChannels * outputDepth);
        }
        if (samplesOut[0].position() >= samplesOut[0].limit()) {
            frame.nb_samples(audioInputFrameSize);
            avcodec_fill_audio_frame(frame, audioC.channels(), outputFormat, samplesOut[0], samplesOut[0].limit(), 0);
            for (int i = 0; i < samplesOut.length; i++) {
                frame.data(i, samplesOut[i].position(0));
                frame.linesize(i, samplesOut[i].limit());
            }
            frame.quality(audioC.global_quality());
            record(frame);
        }
    }
    return frame.key_frame() != 0;
}
Also used : DoubleBuffer(java.nio.DoubleBuffer) ShortPointer(com.googlecode.javacpp.ShortPointer) FloatPointer(com.googlecode.javacpp.FloatPointer) IntBuffer(java.nio.IntBuffer) IntPointer(com.googlecode.javacpp.IntPointer) BytePointer(com.googlecode.javacpp.BytePointer) DoublePointer(com.googlecode.javacpp.DoublePointer) FloatBuffer(java.nio.FloatBuffer) ByteBuffer(java.nio.ByteBuffer) ShortBuffer(java.nio.ShortBuffer)

Aggregations

BytePointer (com.googlecode.javacpp.BytePointer)2 DoublePointer (com.googlecode.javacpp.DoublePointer)2 FloatPointer (com.googlecode.javacpp.FloatPointer)2 IntPointer (com.googlecode.javacpp.IntPointer)2 ShortPointer (com.googlecode.javacpp.ShortPointer)2 ByteBuffer (java.nio.ByteBuffer)2 DoubleBuffer (java.nio.DoubleBuffer)2 FloatBuffer (java.nio.FloatBuffer)2 IntBuffer (java.nio.IntBuffer)2 ShortBuffer (java.nio.ShortBuffer)2