Advertisement
Guest User

Untitled

a guest
Jun 18th, 2012
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.28 KB | None | 0 0
  1. #include <jni.h>
  2. #include <string.h>
  3. #include <stdio.h>
  4. #include <android/log.h>
  5. #include <android/bitmap.h>
  6. #include <sys/queue.h>
  7.  
  8. #include <libavcodec/avcodec.h>
  9. #include <libavformat/avformat.h>
  10. #include <libswscale/swscale.h>
  11. #include <libavutil/fifo.h>
  12.  
  13. #define  LOG_TAG    "FFMPEGSample"
  14. #define  LOGI(...)  __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__)
  15. #define  LOGE(...)  __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__)
  16. #define BUFFER_SIZE (1024 * 64)
  17.  
  18. AVFormatContext *pFormatCtx;
  19. AVCodecContext *pCodecCtx;
  20. int videoStream;
  21. uint8_t *buffer;
  22. static struct SwsContext *img_convert_ctx;
  23. AVFifoBuffer *fifo_buffer;
  24.  
  25. void Java_com_churnlabs_ffmpegsample_MainActivity_openFile(JNIEnv * env, jobject this)
  26. {
  27.     int ret;
  28.     int err;
  29.     int i;
  30.     AVCodec *pCodec;
  31.     int numBytes;
  32.  
  33.     av_register_all();
  34.     LOGE("Registered formats");
  35.     err = av_open_input_file(&pFormatCtx, "/sdcard/alabala/vid_001.mp4", NULL, 0, NULL);
  36.     LOGE("Called open file");
  37.     if(err!=0) {
  38.         LOGE("Couldn't open file");
  39.         return;
  40.     }
  41.     LOGE("Opened file");
  42.    
  43.     if(av_find_stream_info(pFormatCtx)<0) {
  44.         LOGE("Unable to get stream info");
  45.         return;
  46.     }
  47.    
  48.     videoStream = -1;
  49.     for (i=0; i<pFormatCtx->nb_streams; i++) {
  50.         if(pFormatCtx->streams[i]->codec->codec_type==CODEC_TYPE_VIDEO) {
  51.             videoStream = i;
  52.             break;
  53.         }
  54.     }
  55.     if(videoStream==-1) {
  56.         LOGE("Unable to find video stream");
  57.         return;
  58.     }
  59.    
  60.     LOGI("Video stream is [%d]", videoStream);
  61.    
  62.     pCodecCtx=pFormatCtx->streams[videoStream]->codec;
  63.    
  64.     pCodec=avcodec_find_decoder(pCodecCtx->codec_id);
  65.     if(pCodec==NULL) {
  66.         LOGE("Unsupported codec");
  67.         return;
  68.     }
  69.    
  70.     if(avcodec_open(pCodecCtx, pCodec)<0) {
  71.         LOGE("Unable to open codec");
  72.         return;
  73.     }
  74.    
  75.     LOGI("Video size is [%d x %d]", pCodecCtx->width, pCodecCtx->height);
  76.  
  77.     numBytes=avpicture_get_size(PIX_FMT_RGB24, pCodecCtx->width, pCodecCtx->height);
  78.     buffer=(uint8_t *)av_malloc(numBytes*sizeof(uint8_t));
  79.  
  80.     fifo_buffer = av_fifo_alloc(BUFFER_SIZE);
  81.     if(!fifo_buffer) {
  82.         LOGE("FIFO buffer error");
  83.         return;
  84.     }
  85.  
  86.     int target_width = 1280;
  87.     int target_height = 720;
  88.     img_convert_ctx = sws_getContext(pCodecCtx->width, pCodecCtx->height,
  89.            pCodecCtx->pix_fmt,
  90.            target_width, target_height, PIX_FMT_RGB24, SWS_BICUBIC,
  91.            NULL, NULL, NULL);
  92.  
  93.     if(img_convert_ctx == NULL) {
  94.         LOGE("could not initialize conversion context\n");
  95.         return;
  96.     }
  97. }
  98.  
  99. void Java_com_churnlabs_ffmpegsample_MainActivity_startBuffering(JNIEnv * env, jobject this, jstring bitmap)
  100. {
  101.     int i = 0;
  102.     int frameFinished = 0;
  103.     AVPacket packet;
  104.  
  105.     AndroidBitmapInfo  info;
  106.     void*              pixels;
  107.     int                ret;
  108.  
  109.     AVFrame *pFrame = avcodec_alloc_frame();
  110.     while(av_read_frame(pFormatCtx, &packet)>=0) {
  111.         if(packet.stream_index == videoStream) {
  112.             avcodec_decode_video2(pCodecCtx, pFrame, &frameFinished, &packet);
  113.  
  114.             if(frameFinished) {
  115.                 AVFrame *pFrameRGB = avcodec_alloc_frame();
  116.                 avpicture_fill((AVPicture *)pFrameRGB, buffer, PIX_FMT_RGB24, pCodecCtx->width, pCodecCtx->height);
  117.  
  118.                 sws_scale(img_convert_ctx, (const uint8_t* const*)pFrame->data, pFrame->linesize, 0,
  119.                         pCodecCtx->height, pFrameRGB->data, pFrameRGB->linesize);
  120.  
  121.                 av_fifo_generic_write(fifo_buffer, pFrameRGB, sizeof(AVFrame), NULL);
  122.             }
  123.         }
  124.         av_free_packet(&packet);
  125.     }
  126. }
  127.  
  128. void Java_com_churnlabs_ffmpegsample_MainActivity_readFromBuffer(JNIEnv * env, jobject this, jstring bitmap) {
  129.     AndroidBitmapInfo  info;
  130.     void*              pixels;
  131.     int                ret;
  132.     struct entry *item;
  133.     AVFrame frame;
  134.  
  135.     if(av_fifo_size(fifo_buffer) > 0) {
  136.         av_fifo_generic_read(fifo_buffer, &frame, sizeof(AVFrame), NULL);
  137.  
  138.         if ((ret = AndroidBitmap_getInfo(env, bitmap, &info)) < 0) {
  139.             LOGE("AndroidBitmap_getInfo() failed ! error=%d", ret);
  140.             return;
  141.         }
  142.  
  143.         if ((ret = AndroidBitmap_lockPixels(env, bitmap, &pixels)) < 0) {
  144.             LOGE("AndroidBitmap_lockPixels() failed ! error=%d", ret);
  145.         }
  146.  
  147.         fill_bitmap(&info, pixels, &frame);
  148.  
  149.         AndroidBitmap_unlockPixels(env, bitmap);
  150.     }
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement