在Android上使用FFMPEG和SDL 2.X播放視頻

工程:
效果圖:
前言:	

1.本代碼使用的SDL爲2.x,意思是使用的SDL_Texture來顯示YUV數據

2.大致的過程就是:使用FFMPEG解碼出YUV數據,然後使用SDL顯示


關鍵代碼:

1.轉成YUV

sws_ctx =
    sws_getContext
    (
        pCodecCtx->width,
        pCodecCtx->height,
        pCodecCtx->pix_fmt,
        1536,
        1952,
        PIX_FMT_YUV420P,
        SWS_BILINEAR,
        NULL,
        NULL,
        NULL
    );

sws_scale
    (
        sws_ctx, 
        (uint8_t const * const *)pFrame->data, 
        pFrame->linesize, 
        0,
        pCodecCtx->height,
        pict.data,
        pict.linesize
    );

2.SDL顯示YUV數據

a.首先得綁定AVFrame到Buffer

pFrameYUV420p = avcodec_alloc_frame();
  uint8_t *buffer;
  int numBytes;
  // Determine required buffer size and allocate buffer
  numBytes=avpicture_get_size(PIX_FMT_YUV420P, 1536,
                            1952);
  buffer=(uint8_t *)av_malloc(numBytes*sizeof(uint8_t));
  if(buffer==NULL)
  {
    LOGE("buffer is null");return -1;
  }
還有一句別忘記了

avpicture_fill((AVPicture *)pFrameYUV420p, buffer, PIX_FMT_YUV420P,
                1536, 1952);

b.其次得把Buffer數據送到SDL_Texture

int i = SDL_UpdateTexture( pTexture, NULL, buffer, iPitch );
    LOGE("after SDL_UpdateTexture");
        //SDL_RenderClear( renderer );
        //LOGE("after SDL_RenderClear");
        SDL_RenderCopy( renderer, pTexture, NULL, NULL );
        LOGE("after SDL_RenderCopy");
        SDL_RenderPresent( renderer );
        LOGE("after SDL_RenderPresent");

事已至此,已經完成了播放,不過非常簡陋。


總結:

本文寫的很簡陋,也存在很多問題:

1.沒有加入多線程

2.沒有播放速度控制

3.沒有音頻

4.沒有音視頻同步

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章