在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.没有音视频同步

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