ffmpeg Android

FFmpeg 视频解码 YUV

2017-11-05  本文已影响116人  苍天霸气诀

1,获取所有的组件
av_register_all();
2.获取上下文
AVFormatContext *pContext=avformat_alloc_context();
把pContext 赋值
if(avformat_open_input(&pContext, inputStr, NULL, NULL)<0) {
LOGE("ERROR_CODE %d inputStr %s",avformat_open_input(&pContext, inputStr, NULL, NULL),inputStr)
LOGE("打开失败 ");
return;
}
2.获取到解码器上下文

int vedio_stream_idx=-1;
// 找到视频流
for (int i = 0; i < pContext->nb_streams; ++i) {
LOGE("循环 %d", i);
// codec 每一个流 对应的解码上下文 codec_type 流的类型
if (pContext->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
vedio_stream_idx = i;
}
}

// 获取到解码器上下文
AVCodecContext* pCodecCtx=pContext->streams[vedio_stream_idx]->codec;
3.读数据
av_read_frame(pContext, packet)
注释:
// 分配内存 malloc AVPacket 1 2
AVPacket *packet = (AVPacket *) av_malloc(sizeof(AVPacket));
// 初始化结构体
av_init_packet(packet);
4视频节封装 解码
// 分配内存 malloc AVPacket 1 2
AVPacket *packet = (AVPacket *) av_malloc(sizeof(AVPacket));
// 初始化结构体
av_init_packet(packet);
//还是不够
AVFrame *frame = av_frame_alloc();
// 声明一个yuvframe
AVFrame *yuvFrame = av_frame_alloc();
// 给yuvframe 的缓冲区 初始化

uint8_t  *out_buffer= (uint8_t *) av_malloc
        (avpicture_get_size(AV_PIX_FMT_YUV420P,
                            pCodecCtx->width, pCodecCtx->height));

int re=avpicture_fill((AVPicture *) yuvFrame,
                      out_buffer, AV_PIX_FMT_YUV420P,
                      pCodecCtx->width, pCodecCtx->height);
LOGE("宽 %d  高 %d",pCodecCtx->width,pCodecCtx->height);

// mp4 的上下文pCodecCtx->pix_fmt
SwsContext *swsContext=sws_getContext(pCodecCtx->width,pCodecCtx->height,
pCodecCtx->pix_fmt,
pCodecCtx->width,
pCodecCtx->height,AV_PIX_FMT_YUV420P
,SWS_BILINEAR,NULL,NULL,NULL
);
int frameCount = 0;
FILE *fp_yuv = fopen(outStr, "wb");

//packet入参 出参对象 转换上下文
int got_frame;
while (av_read_frame(pContext, packet) >= 0) {
// 节封装

// 根据frame 进行原生绘制 bitmap window
avcodec_decode_video2(pCodecCtx, frame, &got_frame, packet);
// frame 的数据拿到 视频像素数据 yuv 三个rgb r g b 数据量大 三个通道
LOGE("解码%d ",frameCount++);
if (got_frame > 0) {
sws_scale(swsContext, (const uint8_t *const *)
frame->data, frame->linesize,
0, frame->height, yuvFrame->data,
yuvFrame->linesize
);
int y_size = pCodecCtx->width * pCodecCtx->height;
// y 亮度信息写完了
fwrite(yuvFrame->data[0], 1, y_size, fp_yuv);
fwrite(yuvFrame->data[1], 1, y_size/4, fp_yuv);
fwrite(yuvFrame->data[2], 1, y_size/4, fp_yuv);
}
av_free_packet(packet);
}
方法的解释

  1. int avformat_open_input(AVFormatContext **ps, const char *filename, AVInputFormat *fmt, AVDictionary **options);
    ps:函数调用成功之后处理过的AVFormatContext结构体。
    file:打开的视音频流的URL。
    fmt:强制指定AVFormatContext中AVInputFormat的。这个参数一般情况下可以设置为NULL,这样FFmpeg可以自动检测AVInputFormat。
    dictionay:附加的一些选项,一般情况下可以设置为NULL。
    函数执行成功的话,其返回值大于等于0。

2.AVCodec *avcodec_find_decoder(enum AVCodecID id);
函数的参数是一个解码器的ID,返回查找到的解码器(没有找到就返回NULL)。

3.int av_read_frame(AVFormatContext *s, AVPacket *pkt);
av_read_frame()使用方法在注释中写得很详细,用中文简单描述一下它的两个参数:
s:输入的AVFormatContext
pkt:输出的AVPacket
如果返回0则说明读取正常。

4 .

利用ffmpeg进行图像数据格式的转换以及图片的缩放应用中,主要用到了swscale.h文件中的三个函数,分别是:

  struct SwsContext *sws_getContext(int srcW, int srcH, enum AVPixelFormat srcFormat,
                           int dstW, int dstH, enum AVPixelFormat dstFormat,
                           int flags, SwsFilter *srcFilter,
                           SwsFilter *dstFilter, const double *param);
  int sws_scale(struct SwsContext *c, const uint8_t *const srcSlice[],
                 const int srcStride[], int srcSliceY, int srcSliceH,
               uint8_t *const dst[], const int dstStride[]);
  void sws_freeContext(struct SwsContext *swsContext);

sws_getContext函数可以看做是初始化函数,它的参数定义分别为:
int srcW,int srcH 为原始图像数据的高和宽;
int dstW,int dstH 为输出图像数据的高和宽;
enum AVPixelFormat srcFormat 为输入和输出图片数据的类型;eg:AV_PIX_FMT_YUV420、PAV_PIX_FMT_RGB24;
int flags 为scale算法种类;eg:SWS_BICUBIC、SWS_BICUBLIN、SWS_POINT、SWS_SINC;
SwsFilter *srcFilter ,SwsFilter *dstFilter,const double *param 可以不用管,全为NULL即可;

sws_scale函数则为执行函数,它的参数定义分别为:
struct SwsContext *c 为sws_getContext函数返回的值;
const uint8_t *const srcSlice[],uint8_t *const dst[] 为输入输出图像数据各颜色通道的buffer指针数组;
const int srcStride[],const int dstStride[] 为输入输出图像数据各颜色通道每行存储的字节数数组;
int srcSliceY 为从输入图像数据的第多少列开始逐行扫描,通常设为0;
int srcSliceH 为需要扫描多少行,通常为输入图像数据的高度;
sws_freeContext函数为结束函数,它的参数即为sws_getContext函数返回的值;
参考雷神的微博
http://blog.csdn.net/leixiaohua1020/article/category/1360795

上一篇下一篇

猜你喜欢

热点阅读