
Recherche avancée
Autres articles (63)
-
Les autorisations surchargées par les plugins
27 avril 2010, parMediaspip core
autoriser_auteur_modifier() afin que les visiteurs soient capables de modifier leurs informations sur la page d’auteurs -
Ajouter notes et légendes aux images
7 février 2011, parPour pouvoir ajouter notes et légendes aux images, la première étape est d’installer le plugin "Légendes".
Une fois le plugin activé, vous pouvez le configurer dans l’espace de configuration afin de modifier les droits de création / modification et de suppression des notes. Par défaut seuls les administrateurs du site peuvent ajouter des notes aux images.
Modification lors de l’ajout d’un média
Lors de l’ajout d’un média de type "image" un nouveau bouton apparait au dessus de la prévisualisation (...) -
Contribute to translation
13 avril 2011You can help us to improve the language used in the software interface to make MediaSPIP more accessible and user-friendly. You can also translate the interface into any language that allows it to spread to new linguistic communities.
To do this, we use the translation interface of SPIP where the all the language modules of MediaSPIP are available. Just subscribe to the mailing list and request further informantion on translation.
MediaSPIP is currently available in French and English (...)
Sur d’autres sites (9333)
-
Vulkan image data to AVFrames and to video
12 avril 2024, par W4zab1I am trying to encode Vulkan image data into video with MPEG4 format. For some reason the output videofile is corrupted. FFProbe shows discontinuity in timestamps, and the frames are corrupted.
First I prepare my video encoder

Then I get FrameEnded events from my engine where I can get the image data from the vulkan swapchain.

I then convert the image data from vulkan to AVFrames (RGBA to YUV420P), then I pass the frames into queue.

This queue is then handled in another thread, where the frames are processed, and written into video.

I am bit of a noob with ffmpeg, so there can be some code that does not make sense.

This seems right straight forward logic, but there is probably some problems with codec params, way I am converting the imagedata to AVFrame, or something of that sort.

The videofile still gets created, and has some data in it (it is > 0 bytes, and longer the recording, bigger the filesize).

There is no errors from ffmpeg with log_level set to DEBUG.

struct FrameData {
 AVFrame* frame;
 int frame_index;
};

class EventListenerVideoCapture : public VEEventListenerGLFW {
private:
 AVFormatContext* format_ctx = nullptr;
 AVCodec* video_codec = nullptr;
 AVCodecContext* codec_context = nullptr;
 AVStream* video_stream = nullptr;
 AVDictionary* muxer_opts = nullptr;
 int frame_index = 0;

 std::queue frame_queue;
 std::mutex queue_mtx;
 std::condition_variable queue_cv;
 std::atomic<bool> stop_processing{ false };
 std::thread video_processing_thread;
 int prepare_video_encoder()
 {
 av_log_set_level(AV_LOG_DEBUG);
 // Add video stream to format context
 avformat_alloc_output_context2(&format_ctx, nullptr, nullptr, "video.mpg");
 video_stream = avformat_new_stream(format_ctx, NULL);
 video_codec = (AVCodec*)avcodec_find_encoder(AV_CODEC_ID_MPEG4);
 codec_context = avcodec_alloc_context3(video_codec);
 if (!format_ctx) { std::cerr << "Error: Failed to allocate format context" << std::endl; system("pause"); }
 if (!video_stream) { std::cerr << "Error: Failed to create new stream" << std::endl; system("pause"); }
 if (!video_codec) { std::cerr << "Error: Failed to find video codec" << std::endl; system("pause"); }
 if (!codec_context) { std::cerr << "Error: Failed to allocate codec context" << std::endl; system("pause"); }

 if (avio_open(&format_ctx->pb, "video.mpg", AVIO_FLAG_WRITE) < 0) { std::cerr << "Error: Failed to open file for writing!" << std::endl; return -1; }

 av_opt_set(codec_context->priv_data, "preset", "fast", 0);

 codec_context->codec_id = AV_CODEC_ID_MPEG4;
 codec_context->codec_type = AVMEDIA_TYPE_VIDEO;
 codec_context->pix_fmt = AV_PIX_FMT_YUV420P;
 codec_context->width = getWindowPointer()->getExtent().width;
 codec_context->height = getWindowPointer()->getExtent().height;
 codec_context->bit_rate = 1000 * 1000; // Bitrate
 codec_context->time_base = { 1, 30 }; // 30 FPS
 codec_context->gop_size = 10;

 av_dict_set(&muxer_opts, "movflags", "faststart", 0);

 //Unecessary? Since the params are copied anyways
 video_stream->time_base = codec_context->time_base;

 //Try to open codec after changes
 //copy codec_context params to videostream
 //and write headers to format_context
 if (avcodec_open2(codec_context, video_codec, NULL) < 0) { std::cerr << "Error: Could not open codec!" << std::endl; return -1; }
 if (avcodec_parameters_from_context(video_stream->codecpar, codec_context) < 0) { std::cerr << "Error: Could not copy params from context to stream!" << std::endl; return -1; };
 if (avformat_write_header(format_ctx, &muxer_opts) < 0) { std::cerr << "Error: Failed to write output file headers!" << std::endl; return -1; }
 return 0;
 }

 void processFrames() {
 while (!stop_processing) {
 FrameData* frameData = nullptr;
 {
 std::unique_lock lock(queue_mtx);
 queue_cv.wait(lock, [&]() { return !frame_queue.empty() || stop_processing; });

 if (stop_processing && frame_queue.empty())
 break;

 frameData = frame_queue.front();
 frame_queue.pop();
 }

 if (frameData) {
 encodeAndWriteFrame(frameData);
 AVFrame* frame = frameData->frame;
 av_frame_free(&frame); // Free the processed frame
 delete frameData;
 }
 }
 }

 void encodeAndWriteFrame(FrameData* frameData) {

 // Validation
 if (!frameData->frame) { std::cerr << "Error: Frame was null! " << std::endl; return; }
 if (frameData->frame->format != codec_context->pix_fmt) { std::cerr << "Error: Frame format mismatch!" << std::endl; return; }
 if ( av_frame_get_buffer(frameData->frame, 0) < 0) { std::cerr << "Error allocating frame buffer: " << std::endl; return; }
 if (!codec_context) return;

 AVPacket* pkt = av_packet_alloc();
 if (!pkt) { std::cerr << "Error: Failed to allocate AVPacket" << std::endl; system("pause"); }

 int ret = avcodec_send_frame(codec_context, frameData->frame);
 if (ret < 0) { 
 std::cerr << "Error receiving packet from codec: " << ret << std::endl;
 delete frameData;
 av_packet_free(&pkt); return; 
 }

 while (ret >= 0) {
 ret = avcodec_receive_packet(codec_context, pkt);

 //Error checks
 if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) { break; }
 else if (ret < 0) { std::cerr << "Error receiving packet from codec: " << ret << std::endl; av_packet_free(&pkt); return; }
 if (!video_stream) { std::cerr << "Error: video stream is null!" << std::endl; av_packet_free(&pkt); return; }
 
 int64_t frame_duration = codec_context->time_base.den / codec_context->time_base.num;
 pkt->stream_index = video_stream->index;
 pkt->duration = frame_duration;
 pkt->pts = frameData->frame_index * frame_duration;

 int write_ret = av_interleaved_write_frame(format_ctx, pkt);
 if (write_ret < 0) { std::cerr << "Error: failed to write a frame! " << write_ret << std::endl;}

 av_packet_unref(pkt);
 }

 av_packet_free(&pkt);

 }

protected:
 virtual void onFrameEnded(veEvent event) override {
 // Get the image data from vulkan
 VkExtent2D extent = getWindowPointer()->getExtent();
 uint32_t imageSize = extent.width * extent.height * 4;
 VkImage image = getEnginePointer()->getRenderer()->getSwapChainImage();

 uint8_t *dataImage = new uint8_t[imageSize];
 
 vh::vhBufCopySwapChainImageToHost(getEnginePointer()->getRenderer()->getDevice(),
 getEnginePointer()->getRenderer()->getVmaAllocator(),
 getEnginePointer()->getRenderer()->getGraphicsQueue(),
 getEnginePointer()->getRenderer()->getCommandPool(),
 image, VK_FORMAT_R8G8B8A8_UNORM,
 VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
 dataImage, extent.width, extent.height, imageSize);
 
 // Create AVFrame for the converted image data
 AVFrame* frame = av_frame_alloc();
 if (!frame) { std::cout << "Could not allocate memory for frame!" << std::endl; return; }

 frame->format = AV_PIX_FMT_YUV420P;
 frame->width = extent.width;
 frame->height = extent.height;
 if (av_frame_get_buffer(frame, 0) < 0) { std::cerr << "Failed to allocate frame buffer! " << std::endl; return;} ;

 // Prepare context for converting from RGBA to YUV420P
 SwsContext* sws_ctx = sws_getContext(
 extent.width, extent.height, AV_PIX_FMT_RGBA,
 extent.width, extent.height, AV_PIX_FMT_YUV420P,
 SWS_BILINEAR, nullptr, nullptr, nullptr);

 // Convert the vulkan image data to AVFrame
 uint8_t* src_data[1] = { dataImage };
 int src_linesize[1] = { extent.width * 4 };
 int scale_ret = sws_scale(sws_ctx, src_data, src_linesize, 0, extent.height,
 frame->data, frame->linesize);

 if (scale_ret <= 0) { std::cerr << "Failed to scale the image to frame" << std::endl; return; }

 sws_freeContext(sws_ctx);
 delete[] dataImage;

 // Add frame to the queue
 {
 std::lock_guard lock(queue_mtx);

 FrameData* frameData = new FrameData;
 frameData->frame = frame;
 frameData->frame_index = frame_index;
 frame_queue.push(frameData);

 frame_index++;
 }

 // Notify processing thread
 queue_cv.notify_one();
 }

public:
 EventListenerVideoCapture(std::string name) : VEEventListenerGLFW(name) {
 //Prepare the video encoder
 int ret = prepare_video_encoder();
 if (ret < 0)
 {
 std::cerr << "Failed to prepare video encoder! " << std::endl;
 exit(-1);
 }
 else
 {
 // Start video processing thread
 video_processing_thread = std::thread(&EventListenerVideoCapture::processFrames, this);
 }
 }

 ~EventListenerVideoCapture() {
 // Stop video processing thread
 stop_processing = true;
 queue_cv.notify_one(); // Notify processing thread to stop

 if (video_processing_thread.joinable()) {
 video_processing_thread.join();
 }

 // Flush codec and close output file
 avcodec_send_frame(codec_context, nullptr);
 av_write_trailer(format_ctx);

 av_dict_free(&muxer_opts);
 avio_closep(&format_ctx->pb);
 avcodec_free_context(&codec_context);
 avformat_free_context(format_ctx);
 }
};

</bool>


I have tried changing the codec params, debugging and printing the videoframe data with no success.


-
FFMPEG & ytdl-core | Muxing highestaudio and highestvideo results in mp4 with incorrect time
17 mars 2023, par Niek PetersI want to combine a file with only video and a file with only audio from the ytdl-core package into a mp4 and stream it in the response. I get the files using the
highestvideo
andhighestaudio
quality settings. I tried to modify a part of the ytdl-core-muxer module to change the output format to mp4, for which I also needed to add-movflags frag_keyframe+empty_moov
, to allow the output mp4 to be seekable. This all seems to work and it produces a mp4 file without errors. When I open it with the Windows Photos app however, I noticed that even though video plays normally in perfect quality, the time is completely wrong.



I have tried adding a fixed framerate with
-r 60
and regenerating the timestamps with-fflags +genpts
, but to no avail. I have also tried encoding the video and audio formats toh264
(usinglibx264
) andacc
, but that didn't solve it either.



// create the ffmpeg process for muxing
const ffmpegProcess = cp.spawn(
 ffmpeg,
 [
 // input audio by pipe
 "-thread_queue_size", "4096",
 "-i", "pipe:3",

 // input video by pipe
 "-thread_queue_size", "4096",
 "-r", "60",
 "-i", "pipe:4",

 // map audio and video correspondingly
 "-map", "0:a",
 "-map", "1:v",

 // change the codec
 "-c:v", "libx264", 
 "-c:a", "aac",
 
 "-preset", "ultrafast",

 // Allow output to be seekable, which is needed for mp4 output
 "-movflags", "frag_keyframe+empty_moov",

 // output mp4 and pipe
 "-r", "60",
 "-fflags", "+genpts",
 "-f", "mp4", "pipe:5",
 ],
 {
 // no popup window for Windows users
 windowsHide: true,
 stdio: [
 // silence stdin/out, forward stderr,
 "inherit", "inherit", "inherit",
 // and pipe audio, video, output
 "pipe", "pipe", "pipe",
 ],
 }
);
audioStream.pipe(ffmpegProcess.stdio[3]);
videoStream.pipe(ffmpegProcess.stdio[4]);
ffmpegProcess.stdio[5].pipe(result);







Full console output :


ffmpeg version 5.0.1-essentials_build-www.gyan.dev Copyright (c) 2000-2022 the FFmpeg developers 
 built with gcc 11.2.0 (Rev7, Built by MSYS2 project)
 configuration: --enable-gpl --enable-version3 --enable-static --disable-w32threads --disable-autodetect --enable-fontconfig --enable-iconv --enable-gnutls --enable-libxml2 --enable-gmp --enable-lzma --enable-zlib --enable-libsrt --enable-libssh --enable-libzmq --enable-avisynth --enable-sdl2 --enable-libwebp 
--enable-libx264 --enable-libx265 --enable-libxvid --enable-libaom --enable-libopenjpeg --enable-libvpx 
--enable-libass --enable-libfreetype --enable-libfribidi --enable-libvidstab --enable-libvmaf --enable-libzimg --enable-amf --enable-cuda-llvm --enable-cuvid --enable-ffnvcodec --enable-nvdec --enable-nvenc --enable-d3d11va --enable-dxva2 --enable-libmfx --enable-libgme --enable-libopenmpt --enable-libopencore-amrwb --enable-libmp3lame --enable-libtheora --enable-libvo-amrwbenc --enable-libgsm --enable-libopencore-amrnb --enable-libopus --enable-libspeex --enable-libvorbis --enable-librubberband
 libavutil 57. 17.100 / 57. 17.100
 libavcodec 59. 18.100 / 59. 18.100
 libavformat 59. 16.100 / 59. 16.100
 libavdevice 59. 4.100 / 59. 4.100
 libavfilter 8. 24.100 / 8. 24.100
 libswscale 6. 4.100 / 6. 4.100
 libswresample 4. 3.100 / 4. 3.100
 libpostproc 56. 3.100 / 56. 3.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'pipe:3':
 Metadata:
 major_brand : mp42
 minor_version : 0
 compatible_brands: isommp42
 creation_time : 2022-08-21T19:56:49.000000Z
 Duration: 00:01:32.11, start: 0.000000, bitrate: N/A
 Stream #0:0[0x1](und): Video: h264 (High) (avc1 / 0x31637661), yuv420p(tv, bt709, progressive), 1280x720 [SAR 1:1 DAR 16:9], 1892 kb/s, 30 fps, 30 tbr, 15360 tbn (default)
 Metadata:
 creation_time : 2022-08-21T19:56:49.000000Z
 handler_name : ISO Media file produced by Google Inc. Created on: 08/21/2022.
 vendor_id : [0][0][0][0]
 Stream #0:1[0x2](eng): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 128 kb/s (default) Metadata:
 creation_time : 2022-08-21T19:56:49.000000Z
 handler_name : ISO Media file produced by Google Inc. Created on: 08/21/2022.
 vendor_id : [0][0][0][0]
Input #1, matroska,webm, from 'pipe:4':
 Metadata:
 encoder : google/video-file
 Duration: 00:01:32.12, start: 0.000000, bitrate: N/A
 Stream #1:0(eng): Video: vp9 (Profile 0), yuv420p(tv, bt709), 3840x2160, SAR 1:1 DAR 16:9, 60 fps, 60 
tbr, 1k tbn (default)
Stream mapping:
 Stream #0:1 -> #0:0 (aac (native) -> aac (native))
 Stream #1:0 -> #0:1 (vp9 (native) -> h264 (libx264))
[libx264 @ 0000022aa059be80] using SAR=1/1
[libx264 @ 0000022aa059be80] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX FMA3 BMI2 AVX2 
[libx264 @ 0000022aa059be80] profile Constrained Baseline, level 5.2, 4:2:0, 8-bit
[libx264 @ 0000022aa059be80] 264 - core 164 r3094 bfc87b7 - H.264/MPEG-4 AVC codec - Copyleft 2003-2022 
- http://www.videolan.org/x264.html - options: cabac=0 ref=1 deblock=0:0:0 analyse=0:0 me=dia subme=0 psy=1 psy_rd=1.00:0.00 mixed_ref=0 me_range=16 chroma_me=1 trellis=0 8x8dct=0 cqm=0 deadzone=21,11 fast_pskip=1 chroma_qp_offset=0 threads=12 lookahead_threads=2 sliced_threads=0 nr=0 decimate=1 interlaced=0 bluray_compat=0 constrained_intra=0 bframes=0 weightp=0 keyint=250 keyint_min=25 scenecut=0 intra_refresh=0 rc=crf mbtree=0 crf=23.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 ip_ratio=1.40 aq=0
Output #0, mp4, to 'pipe:5':
 Metadata:
 major_brand : mp42
 minor_version : 0
 compatible_brands: isommp42
 encoder : Lavf59.16.100
 Stream #0:0(eng): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 128 kb/s (default) 
 Metadata:
 creation_time : 2022-08-21T19:56:49.000000Z
 handler_name : ISO Media file produced by Google Inc. Created on: 08/21/2022.
 vendor_id : [0][0][0][0]
 encoder : Lavc59.18.100 aac
 Stream #0:1(eng): Video: h264 (avc1 / 0x31637661), yuv420p(tv, bt709, progressive), 3840x2160 [SAR 1:1 DAR 16:9], q=2-31, 60 fps, 15360 tbn (default)
 Metadata:
 encoder : Lavc59.18.100 libx264
 Side data:
 cpb: bitrate max/min/avg: 0/0/0 buffer size: 0 vbv_delay: N/A
[matroska,webm @ 0000022aa056cd00] Thread message queue blocking; consider raising the thread_queue_size option (current value: 4096)
frame= 5527 fps= 77 q=-1.0 Lsize= 424599kB time=00:01:32.11 bitrate=37761.2kbits/s speed=1.29x 
video:423226kB audio:1329kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.010570%[aac @ 0000022aa05ca240] Qavg: 17828.035
[libx264 @ 0000022aa059be80] frame I:23 Avg QP:19.83 size:292044
[libx264 @ 0000022aa059be80] frame P:5504 Avg QP:22.92 size: 77519
[libx264 @ 0000022aa059be80] mb I I16..4: 100.0% 0.0% 0.0%
[libx264 @ 0000022aa059be80] mb P I16..4: 26.5% 0.0% 0.0% P16..4: 14.1% 0.0% 0.0% 0.0% 0.0% skip:59.3%
[libx264 @ 0000022aa059be80] coded y,uvDC,uvAC intra: 10.6% 27.6% 7.0% inter: 6.5% 12.8% 1.4%
[libx264 @ 0000022aa059be80] i16 v,h,dc,p: 57% 26% 8% 9%
[libx264 @ 0000022aa059be80] i8c dc,h,v,p: 43% 24% 25% 7%
[libx264 @ 0000022aa059be80] kb/s:37637.70



-
Decoding audio via Android using FFMpeg
31 octobre 2013, par Rob HauptI can play Wav files using the below code without issues. When trying to play the exact same media in Mp3 format I only get garbled junk. I believe I am fundamentally misunderstanding how the avcodec_decode_audio3 function works.
Since the Wav file contains PCM data when it is decoded it can go straight to the AudioTrack.write function. There must be some additional step to get Mp3s to work like this. I don't know what I'm missing, but I've been pulling my hair out for a week now.
Java Code
package com.rohaupt.RRD2;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import android.app.Activity;
import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioTrack;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.SystemClock;
public class player extends Activity
{
private AudioTrack track;
private FileOutputStream os;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
createEngine();
MediaPlayer mp = new MediaPlayer();
mp.start();
int bufSize = AudioTrack.getMinBufferSize(32000,
AudioFormat.CHANNEL_CONFIGURATION_STEREO,
AudioFormat.ENCODING_PCM_16BIT);
track = new AudioTrack(AudioManager.STREAM_MUSIC,
32000,
AudioFormat.CHANNEL_CONFIGURATION_STEREO,
AudioFormat.ENCODING_PCM_16BIT,
bufSize,
AudioTrack.MODE_STREAM);
byte[] bytes = new byte[bufSize];
try {
os = new FileOutputStream("/sdcard/a.out",false);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String result = loadFile("/sdcard/a.mp3",bytes);
try {
os.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
void playSound(byte[] buf, int size) {
//android.util.Log.v("ROHAUPT", "RAH Playing");
if(track.getPlayState()!=AudioTrack.PLAYSTATE_PLAYING)
track.play();
track.write(buf, 0, size);
try {
os.write(buf,0,size);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private native void createEngine();
private native String loadFile(String file, byte[] array);
/** Load jni .so on initialization*/
static {
System.loadLibrary("avutil");
System.loadLibrary("avcore");
System.loadLibrary("avcodec");
System.loadLibrary("avformat");
System.loadLibrary("avdevice");
System.loadLibrary("swscale");
System.loadLibrary("avfilter");
System.loadLibrary("ffmpeg");
}
}C Code
#include
#include
#include
#include <android></android>log.h>
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
#define DEBUG_TAG "ROHAUPT"
void Java_com_rohaupt_RRD2_player_createEngine(JNIEnv* env, jclass clazz)
{
avcodec_init();
av_register_all();
}
jstring Java_com_rohaupt_RRD2_player_loadFile(JNIEnv* env, jobject obj,jstring file,jbyteArray array)
{
jboolean isCopy;
int i;
int audioStream=-1;
int res;
int decoded = 0;
int out_size;
AVFormatContext *pFormatCtx;
AVCodecContext *aCodecCtx;
AVCodecContext *c= NULL;
AVCodec *aCodec;
AVPacket packet;
jclass cls = (*env)->GetObjectClass(env, obj);
jmethodID play = (*env)->GetMethodID(env, cls, "playSound", "([BI)V");//At the begining of your main function
const char * szfile = (*env)->GetStringUTFChars(env, file, &isCopy);
int16_t * pAudioBuffer = (int16_t *) av_malloc (AVCODEC_MAX_AUDIO_FRAME_SIZE*2+FF_INPUT_BUFFER_PADDING_SIZE);
int16_t * outBuffer = (int16_t *) av_malloc (AVCODEC_MAX_AUDIO_FRAME_SIZE*2+FF_INPUT_BUFFER_PADDING_SIZE);
__android_log_print(ANDROID_LOG_INFO, DEBUG_TAG, "RAH28 Starting");
res = av_open_input_file(&pFormatCtx, szfile, NULL, 0, NULL);
if(res!=0)
{
__android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "RAH opening input failed with result: [%d]", res);
return file;
}
__android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "RAH getting stream info");
res = av_find_stream_info(pFormatCtx);
if(res<0)
{
__android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "RAH getting stream info failed with result: [%d]", res);
return file;
}
__android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "RAH getting audio stream");
for(i=0; i < pFormatCtx->nb_streams; i++) {
if(pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_AUDIO &&
audioStream < 0) {
audioStream=i;
}
}
if(audioStream==-1)
{
__android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "RAH couldn't find audio stream");
return file;
}
__android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "RAH audio stream found with result: [%d]", res);
aCodecCtx=pFormatCtx->streams[audioStream]->codec;
__android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "RAH audio codec info loaded");
__android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "RAH audio codec info [%d]", aCodecCtx->codec_id);
aCodec = avcodec_find_decoder(aCodecCtx->codec_id);
if(!aCodec) {
__android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "RAH audio codec unsupported");
return file;
}
__android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "RAH audio codec info found");
res = avcodec_open(aCodecCtx, aCodec);
__android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "RAH audio codec loaded [%d] [%d]",aCodecCtx->sample_fmt,res);
//c=avcodec_alloc_context();
av_init_packet(&packet);
__android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "RAH channels [%d] sample rate [%d] sample format [%d]",aCodecCtx->channels,aCodecCtx->sample_rate,aCodecCtx->sample_fmt);
int x,y;
x=0;y=0;
while (av_read_frame(pFormatCtx, &packet)>= 0) {
__android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "RAH frame read: [%d] [%d]",x++,y);
if (aCodecCtx->codec_type == AVMEDIA_TYPE_AUDIO) {
__android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "RAH audio ready");
int data_size = AVCODEC_MAX_AUDIO_FRAME_SIZE*2+FF_INPUT_BUFFER_PADDING_SIZE;
int size=packet.size;
y=0;
decoded = 0;
__android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "RAH packet size: [%d]", size);
while(size > 0) {
__android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "RAH decoding: [%d] [%d]",x,y++);
int len = avcodec_decode_audio3(aCodecCtx, pAudioBuffer, &data_size, &packet);
__android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "RAH 1 size [%d] len [%d] data_size [%d] out_size [%d]",size,len,data_size,out_size);
jbyte *bytes = (*env)->GetByteArrayElements(env, array, NULL);
memcpy(bytes + decoded, pAudioBuffer, len); //
__android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "RAH 2");
(*env)->ReleaseByteArrayElements(env, array, bytes, 0);
__android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "RAH 3");
(*env)->CallVoidMethod(env, obj, play, array, len);
__android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "RAH 4");
size -= len;
decoded += len;
}
av_free_packet(&packet);
}
}
// Close the video file
av_close_input_file(pFormatCtx);
//__android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "RAH Finished Running result: [%d]", res);
(*env)->ReleaseStringUTFChars(env, file, szfile);
return file;
}To add some detail. When Calling this function with a Wav File I get the following Log Data
I/ROHAUPT ( 227): RAH28 Starting
D/ROHAUPT ( 227): RAH getting stream info
D/ROHAUPT ( 227): RAH getting audio stream
D/ROHAUPT ( 227): RAH audio stream found with result: [0]
D/ROHAUPT ( 227): RAH audio codec info loaded
D/ROHAUPT ( 227): RAH audio codec info [65536]
D/ROHAUPT ( 227): RAH audio codec info found
D/ROHAUPT ( 227): RAH audio codec loaded [1] [0]
D/ROHAUPT ( 227): RAH channels [2] sample rate [32000] sample format [1]
D/ROHAUPT ( 227): RAH frame read: [0] [0]
D/ROHAUPT ( 227): RAH audio ready
D/ROHAUPT ( 227): RAH packet size: [4096]
D/ROHAUPT ( 227): RAH decoding: [1] [0]
D/ROHAUPT ( 227): RAH 1 size [4096] len [4096] data_size [4096] out_size [0]
D/ROHAUPT ( 227): RAH 2
D/ROHAUPT ( 227): RAH 3
D/ROHAUPT ( 227): RAH 4
D/ROHAUPT ( 227): RAH frame read: [1] [1]
D/ROHAUPT ( 227): RAH audio ready
...
D/ROHAUPT ( 227): RAH frame read: [924] [1]
D/ROHAUPT ( 227): RAH audio ready
D/ROHAUPT ( 227): RAH packet size: [4096]
D/ROHAUPT ( 227): RAH decoding: [925] [0]
D/ROHAUPT ( 227): RAH 1 size [4096] len [4096] data_size [4096] out_size [0]
D/ROHAUPT ( 227): RAH 2
D/ROHAUPT ( 227): RAH 3
D/ROHAUPT ( 227): RAH 4
D/ROHAUPT ( 227): RAH frame read: [925] [1]
D/ROHAUPT ( 227): RAH audio ready
D/ROHAUPT ( 227): RAH packet size: [3584]
D/ROHAUPT ( 227): RAH decoding: [926] [0]
D/ROHAUPT ( 227): RAH 1 size [3584] len [3584] data_size [3584] out_size [0]
D/ROHAUPT ( 227): RAH 2
D/ROHAUPT ( 227): RAH 3
D/ROHAUPT ( 227): RAH 4When calling with an Mp3 file I get the following
I/ROHAUPT ( 280): RAH28 Starting
D/ROHAUPT ( 280): RAH getting stream info
D/ROHAUPT ( 280): RAH getting audio stream
D/ROHAUPT ( 280): RAH audio stream found with result: [0]
D/ROHAUPT ( 280): RAH audio codec info loaded
D/ROHAUPT ( 280): RAH audio codec info [86017]
D/ROHAUPT ( 280): RAH audio codec info found
D/ROHAUPT ( 280): RAH audio codec loaded [1] [0]
D/ROHAUPT ( 280): RAH channels [2] sample rate [32000] sample format [1]
D/ROHAUPT ( 280): RAH frame read: [0] [0]
D/ROHAUPT ( 280): RAH audio ready
D/ROHAUPT ( 280): RAH packet size: [432]
D/ROHAUPT ( 280): RAH decoding: [1] [0]
D/ROHAUPT ( 280): RAH 1 size [432] len [432] data_size [4608] out_size [0]
D/ROHAUPT ( 280): RAH 2
...
D/ROHAUPT ( 280): RAH frame read: [822] [1]
D/ROHAUPT ( 280): RAH audio ready
D/ROHAUPT ( 280): RAH packet size: [432]
D/ROHAUPT ( 280): RAH decoding: [823] [0]
D/ROHAUPT ( 280): RAH 1 size [432] len [432] data_size [4608] out_size [0]
D/ROHAUPT ( 280): RAH 2
D/ROHAUPT ( 280): RAH 3
D/ROHAUPT ( 280): RAH 4
D/ROHAUPT ( 280): RAH frame read: [823] [1]
D/ROHAUPT ( 280): RAH audio ready
D/ROHAUPT ( 280): RAH packet size: [432]
D/ROHAUPT ( 280): RAH decoding: [824] [0]
D/ROHAUPT ( 280): RAH 1 size [432] len [432] data_size [4608] out_size [0]
D/ROHAUPT ( 280): RAH 2
D/ROHAUPT ( 280): RAH 3
D/ROHAUPT ( 280): RAH 4