Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <jni.h>
- #include <string.h>
- #include <stdio.h>
- #include <android/log.h>
- #include <android/bitmap.h>
- #include <sys/queue.h>
- #include <libavcodec/avcodec.h>
- #include <libavformat/avformat.h>
- #include <libswscale/swscale.h>
- #include <libavutil/fifo.h>
- #define LOG_TAG "FFMPEGSample"
- #define LOGI(...) __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__)
- #define LOGE(...) __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__)
- #define BUFFER_SIZE (1024 * 64)
- AVFormatContext *pFormatCtx;
- AVCodecContext *pCodecCtx;
- int videoStream;
- uint8_t *buffer;
- static struct SwsContext *img_convert_ctx;
- AVFifoBuffer *fifo_buffer;
- void Java_com_churnlabs_ffmpegsample_MainActivity_openFile(JNIEnv * env, jobject this)
- {
- int ret;
- int err;
- int i;
- AVCodec *pCodec;
- int numBytes;
- av_register_all();
- LOGE("Registered formats");
- err = av_open_input_file(&pFormatCtx, "/sdcard/alabala/vid_001.mp4", NULL, 0, NULL);
- LOGE("Called open file");
- if(err!=0) {
- LOGE("Couldn't open file");
- return;
- }
- LOGE("Opened file");
- if(av_find_stream_info(pFormatCtx)<0) {
- LOGE("Unable to get stream info");
- return;
- }
- videoStream = -1;
- for (i=0; i<pFormatCtx->nb_streams; i++) {
- if(pFormatCtx->streams[i]->codec->codec_type==CODEC_TYPE_VIDEO) {
- videoStream = i;
- break;
- }
- }
- if(videoStream==-1) {
- LOGE("Unable to find video stream");
- return;
- }
- LOGI("Video stream is [%d]", videoStream);
- pCodecCtx=pFormatCtx->streams[videoStream]->codec;
- pCodec=avcodec_find_decoder(pCodecCtx->codec_id);
- if(pCodec==NULL) {
- LOGE("Unsupported codec");
- return;
- }
- if(avcodec_open(pCodecCtx, pCodec)<0) {
- LOGE("Unable to open codec");
- return;
- }
- LOGI("Video size is [%d x %d]", pCodecCtx->width, pCodecCtx->height);
- numBytes=avpicture_get_size(PIX_FMT_RGB24, pCodecCtx->width, pCodecCtx->height);
- buffer=(uint8_t *)av_malloc(numBytes*sizeof(uint8_t));
- fifo_buffer = av_fifo_alloc(BUFFER_SIZE);
- if(!fifo_buffer) {
- LOGE("FIFO buffer error");
- return;
- }
- int target_width = 1280;
- int target_height = 720;
- img_convert_ctx = sws_getContext(pCodecCtx->width, pCodecCtx->height,
- pCodecCtx->pix_fmt,
- target_width, target_height, PIX_FMT_RGB24, SWS_BICUBIC,
- NULL, NULL, NULL);
- if(img_convert_ctx == NULL) {
- LOGE("could not initialize conversion context\n");
- return;
- }
- }
- void Java_com_churnlabs_ffmpegsample_MainActivity_startBuffering(JNIEnv * env, jobject this, jstring bitmap)
- {
- int i = 0;
- int frameFinished = 0;
- AVPacket packet;
- AndroidBitmapInfo info;
- void* pixels;
- int ret;
- AVFrame *pFrame = avcodec_alloc_frame();
- while(av_read_frame(pFormatCtx, &packet)>=0) {
- if(packet.stream_index == videoStream) {
- avcodec_decode_video2(pCodecCtx, pFrame, &frameFinished, &packet);
- if(frameFinished) {
- AVFrame *pFrameRGB = avcodec_alloc_frame();
- avpicture_fill((AVPicture *)pFrameRGB, buffer, PIX_FMT_RGB24, pCodecCtx->width, pCodecCtx->height);
- sws_scale(img_convert_ctx, (const uint8_t* const*)pFrame->data, pFrame->linesize, 0,
- pCodecCtx->height, pFrameRGB->data, pFrameRGB->linesize);
- av_fifo_generic_write(fifo_buffer, pFrameRGB, sizeof(AVFrame), NULL);
- }
- }
- av_free_packet(&packet);
- }
- }
- void Java_com_churnlabs_ffmpegsample_MainActivity_readFromBuffer(JNIEnv * env, jobject this, jstring bitmap) {
- AndroidBitmapInfo info;
- void* pixels;
- int ret;
- struct entry *item;
- AVFrame frame;
- if(av_fifo_size(fifo_buffer) > 0) {
- av_fifo_generic_read(fifo_buffer, &frame, sizeof(AVFrame), NULL);
- if ((ret = AndroidBitmap_getInfo(env, bitmap, &info)) < 0) {
- LOGE("AndroidBitmap_getInfo() failed ! error=%d", ret);
- return;
- }
- if ((ret = AndroidBitmap_lockPixels(env, bitmap, &pixels)) < 0) {
- LOGE("AndroidBitmap_lockPixels() failed ! error=%d", ret);
- }
- fill_bitmap(&info, pixels, &frame);
- AndroidBitmap_unlockPixels(env, bitmap);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement