在Android上使用FFMPEG和SDL 2.X播放视频

2 篇文章 0 订阅
1 篇文章 0 订阅

工程:
效果图:
前言:	

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值