bytedeco / javacpp-presets

The missing Java distribution of native C++ libraries
Other
2.68k stars 744 forks source link

Trouble with decode mp3 to PCM (AV_SAMPLE_FMT_S16) #1089

Open jershell opened 3 years ago

jershell commented 3 years ago

In my decode loop i have a frame that contains data

            println("pFrame: sample rate = ${pFrame.sample_rate()}") // 44100
            println("pFrame: nb_samples = ${pFrame.nb_samples()}") //1152
            println("pFrame: format = ${pFrame.format()}") // 8 -> AV_SAMPLE_FMT_FLTP
            println("pFrame: format = ${printFmt(pFrame.format())}") // 8 -> AV_SAMPLE_FMT_FLTP
            println("pFrame: channels = ${pFrame.channels()}") // 2
            println("pFrame: channel_layout = ${pFrame.channel_layout()}") // 3
            println("pFrame: linesize_0 = ${pFrame.linesize(0)}") // 4608

I need PCM in AV_SAMPLE_FMT_S16 format, but I can't do it. In stackoverflow told AV_SAMPLE_FMT_FLTP is float planar that sign pFrame.extended_data(channel) contains a float array in range from -1.0f to 1.0f. Next, for extract values i use DataInputStream,

            val linesize = pFrame.linesize(0)

           // allocate buffers
            val byteArrayCh0 = ByteArray(linesize)
            val byteArrayCh1 = ByteArray(linesize)

           // fill buffers
            pFrame.extended_data(0).get(byteArrayCh0, 0 , linesize)
            pFrame.extended_data(1).get(byteArrayCh1, 0 , linesize)

            // convert for read the buffers
            val dis0 = DataInputStream(byteArrayCh0.inputStream())
            val dis1 = DataInputStream(byteArrayCh1.inputStream())

            var k = 0L
            while (k<nb_samples) {
                val sampleFromCh0 = dis0.readFloat()
                val sampleFromCh1 = dis1.readFloat()
                outputBuffer2[k * channels + 0] = (sampleFromCh0 * 32767.0f).toInt()
                outputBuffer2[k * channels + 1] = (sampleFromCh1 * 32767.0f).toInt()
                k++
            }

But it does not work. I hear only noise. For the test, I convert some mp3 file by cli and different results by

ffplay  -f s16le -ar 44.1k -ac 2 ./output_pcm.raw

How I can read these float arrays?

saudet commented 3 years ago

Something like this sample code works: https://github.com/bytedeco/javacv/blob/master/samples/JavaFxPlayVideoAndAudio.java

jershell commented 3 years ago

Thx, it's help me.