-
Notifications
You must be signed in to change notification settings - Fork 0
[WIP] Adding encoding/decoding capabilities of FFmpeg #125
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
asubb
wants to merge
2
commits into
develop
Choose a base branch
from
ffmpeg
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| FFMPEG plugin | ||
| ======== | ||
|
|
||
| The projects injects [ffmpeg](http://ffmpeg.org/) capabilities into the project |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| dependencies { | ||
| implementation(project(":lib")) | ||
| api("org.bytedeco:ffmpeg-platform:5.0-1.5.7") | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,186 @@ | ||
| package io.wavebeans.ffmpeg | ||
|
|
||
| import io.wavebeans.lib.BitDepth | ||
| import io.wavebeans.lib.io.WavHeader | ||
| import org.bytedeco.ffmpeg.avcodec.AVPacket | ||
| import org.bytedeco.ffmpeg.avformat.AVProbeData | ||
| import org.bytedeco.ffmpeg.avformat.Read_packet_Pointer_BytePointer_int | ||
| import org.bytedeco.ffmpeg.avformat.Seek_Pointer_long_int | ||
| import org.bytedeco.ffmpeg.global.avcodec.av_packet_unref | ||
| import org.bytedeco.ffmpeg.global.avcodec.avcodec_alloc_context3 | ||
| import org.bytedeco.ffmpeg.global.avcodec.avcodec_find_decoder | ||
| import org.bytedeco.ffmpeg.global.avcodec.avcodec_open2 | ||
| import org.bytedeco.ffmpeg.global.avcodec.avcodec_parameters_to_context | ||
| import org.bytedeco.ffmpeg.global.avcodec.avcodec_receive_frame | ||
| import org.bytedeco.ffmpeg.global.avcodec.avcodec_send_packet | ||
| import org.bytedeco.ffmpeg.global.avformat.AVSEEK_SIZE | ||
| import org.bytedeco.ffmpeg.global.avformat.av_probe_input_format | ||
| import org.bytedeco.ffmpeg.global.avformat.av_read_frame | ||
| import org.bytedeco.ffmpeg.global.avformat.avformat_alloc_context | ||
| import org.bytedeco.ffmpeg.global.avformat.avformat_close_input | ||
| import org.bytedeco.ffmpeg.global.avformat.avformat_find_stream_info | ||
| import org.bytedeco.ffmpeg.global.avformat.avformat_open_input | ||
| import org.bytedeco.ffmpeg.global.avformat.avio_alloc_context | ||
| import org.bytedeco.ffmpeg.global.avutil.AVERROR_EOF | ||
| import org.bytedeco.ffmpeg.global.avutil.AVERROR_UNKNOWN | ||
| import org.bytedeco.ffmpeg.global.avutil.AVMEDIA_TYPE_AUDIO | ||
| import org.bytedeco.ffmpeg.global.avutil.av_frame_alloc | ||
| import org.bytedeco.ffmpeg.global.avutil.av_get_bytes_per_sample | ||
| import org.bytedeco.ffmpeg.global.avutil.av_malloc | ||
| import org.bytedeco.ffmpeg.global.avutil.av_strerror | ||
| import org.bytedeco.javacpp.BytePointer | ||
| import org.bytedeco.javacpp.Pointer | ||
| import org.bytedeco.javacpp.PointerPointer | ||
| import java.io.File | ||
| import java.nio.ByteBuffer | ||
|
|
||
| class Ffmpeg { | ||
| } | ||
|
|
||
|
|
||
| const val SEEK_SET = 0 | ||
| const val SEEK_CUR = 1 | ||
| const val SEEK_END = 2 | ||
| fun main() { | ||
| val file = "${System.getProperty("user.home")}/tmp/guitar.wav" | ||
|
|
||
| val buffer = ByteBuffer.wrap(File(file).readBytes()) | ||
|
|
||
| println("buffer=$buffer") | ||
| val inputStream = BytePointer(buffer) | ||
| val bufferSize = 512 | ||
| val ioContext = avio_alloc_context( | ||
| BytePointer(av_malloc(bufferSize.toLong())), | ||
| bufferSize, | ||
| 0, | ||
| inputStream, | ||
| object : Read_packet_Pointer_BytePointer_int() { | ||
| override fun call(opaque: Pointer, buf: BytePointer, buf_size: Int): Int { | ||
| try { | ||
| // println("read(opaque=$opaque, buf=${buf}, buf_size=$buf_size) buffer=$buffer") | ||
| val length = minOf(buffer.remaining(), buf_size/*, buf.capacity().toInt()*/) | ||
| if (length <= 0) { | ||
| return AVERROR_EOF | ||
| } | ||
| repeat(length) { | ||
| val b = buffer.get() | ||
| buf.put(it.toLong(), b) | ||
| } | ||
| // println("Read $length bytes") | ||
| return length | ||
| } catch (e: Exception) { | ||
| e.printStackTrace() | ||
| return AVERROR_UNKNOWN | ||
| } | ||
| } | ||
| }, | ||
| null, | ||
| object : Seek_Pointer_long_int() { | ||
| override fun call(opaque: Pointer, offset: Long, whence: Int): Long { | ||
| // println("seek(opaque=$opaque, offset=$offset, whence=$whence") | ||
| try { | ||
| if (whence == AVSEEK_SIZE) | ||
| return buffer.capacity().toLong() | ||
| when (whence) { | ||
| SEEK_SET -> buffer.position(offset.toInt()) | ||
| SEEK_CUR -> buffer.position(buffer.position() + offset.toInt()) | ||
| SEEK_END -> buffer.position(buffer.limit()) | ||
| else -> throw IllegalArgumentException("Whence $whence is not recognized") | ||
| } | ||
| // println("Set to ${buffer.position()} position:$buffer") | ||
| return buffer.position().toLong() | ||
| } catch (e: Exception) { | ||
asubb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| e.printStackTrace() | ||
| return -1 | ||
| } | ||
| } | ||
| } | ||
| ) | ||
|
|
||
| val fmtCtx = avformat_alloc_context() | ||
| fmtCtx.pb(ioContext) | ||
| // val fmtCtx = AVFormatContext(null) | ||
| // avformat_open_input(fmtCtx, file, null, null).throwIfError() | ||
| // av_dump_format(fmtCtx, 0, file, 0) | ||
|
|
||
| val probeData = AVProbeData() | ||
| probeData.buf(BytePointer(buffer)) | ||
| probeData.buf_size(1234) | ||
| probeData.filename(BytePointer("")) | ||
| val format = av_probe_input_format(probeData, 1) | ||
| println(">>>> format=${format.long_name().stringBytes.decodeToString()}") | ||
|
|
||
| fmtCtx.iformat(format) | ||
| fmtCtx.flags(128) // AVFMT_FLAG_CUSTOM_IO | ||
|
|
||
| buffer.rewind() | ||
| avformat_open_input(fmtCtx, "", format, null).throwIfError() | ||
|
|
||
| println("streams ${fmtCtx.streams()}") | ||
| println("nb_stream=${fmtCtx.nb_streams()}") | ||
|
|
||
| avformat_find_stream_info(fmtCtx, null as PointerPointer<*>?).throwIfError() | ||
| var streamIdx: Int = -1 | ||
| for (it in 0 until fmtCtx.nb_streams()) { | ||
| val codecType = fmtCtx.streams(it).codecpar().codec_type() | ||
| if (codecType == AVMEDIA_TYPE_AUDIO) { | ||
| streamIdx = it | ||
| break | ||
| } | ||
| } | ||
| require(streamIdx >= 0) { "audio stream is not found" } | ||
| println("Picked stream #$streamIdx") | ||
| fmtCtx.streams(streamIdx).codecpar().use { | ||
| println("Sample rate: ${it.sample_rate()}") | ||
| println("Bit depth: ${it.bits_per_coded_sample()}") | ||
| } | ||
|
|
||
| val codecCtx = avcodec_alloc_context3(null) | ||
| avcodec_parameters_to_context(codecCtx, fmtCtx.streams(streamIdx).codecpar()) | ||
| val codec = avcodec_find_decoder(codecCtx.codec_id()) | ||
| requireNotNull(codec) { "Unsupported codec $codecCtx" } | ||
| avcodec_open2(codecCtx, codec, null as PointerPointer<*>?).throwIfError() | ||
| println("Context=$codecCtx codec=$codec") | ||
|
|
||
| av_frame_alloc().use { frame -> | ||
| val wav = WavHeader(BitDepth.BIT_32, 44100.0f, 1, 0x1FFFFFFF); | ||
| val wavFile = File(File(file).parentFile, "output.wav") | ||
| wavFile.createNewFile() | ||
| wavFile.outputStream().buffered().use { fos -> | ||
| fos.write(wav.header()) | ||
| val pkt = AVPacket() | ||
| val dataSize = av_get_bytes_per_sample(codecCtx.sample_fmt()) | ||
| while (av_read_frame(fmtCtx, pkt) >= 0) { | ||
| if (pkt.stream_index() == streamIdx) { | ||
| avcodec_send_packet(codecCtx, pkt).throwIfError() | ||
| avcodec_receive_frame(codecCtx, frame).throwIfError() | ||
|
|
||
| val data = frame.data(0) | ||
| val buf = ByteArray(dataSize * frame.nb_samples()) | ||
| data.get(buf) | ||
| fos.write(buf) | ||
|
|
||
| } | ||
| av_packet_unref(pkt) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| avformat_close_input(fmtCtx) | ||
| } | ||
|
|
||
| fun Int.throwIfError() { | ||
| if (this < 0) { | ||
| println("ERROR") | ||
| val buf = ByteArray(1024) | ||
| val r = av_strerror(this, buf, 1024L) | ||
| val end = buf.indexOfFirst { it.toInt() == 0 }.takeIf { it >= 0 } | ||
| throw IllegalStateException( | ||
| if (r >= 0) { | ||
| buf.decodeToString(0, end ?: buf.size) | ||
| } else { | ||
| "unknown error $this" | ||
| } | ||
| ) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.