ffmpeg v4l2 数据格式装换

43 篇文章 0 订阅

/*=============================================================================
 * #     FileName: read_device.c
 * #         Desc: use ffmpeg read a frame data from v4l2, and convert
 * #               the output data format
 * #       Author: licaibiao
 * #   LastChange: 2017-03-28
 * =============================================================================

 在linux系统中,摄像头数据通过v4l2采集,由于摄像头的不同,摄像头所输出的数据格式各有不同。而在进行数据编码的时候,我们一般使用平面分布而不使用交叉分布。以YUV420P 的格式使用最广泛。为了提高软件的兼容性,所以在直播系统中添加数据格式的转换。另外,为了输出不同长宽比例的画面,图像的拉伸缩放自然也得支持。这里使用ffmpeg 提供的接口直接使用,将从v4l2采集到的摄像头数据转换为需要的尺寸和格式。代码如下:

上面的代码需要注意,输入和输出的数据格式可以是任意的YUV和RGB格式,也可以是任意的尺寸。但是,如果使用的摄像头只能输出MJPEG格式数据,那么在使用上面代码将会出错,因为上面的代码并不支持MJPEG格式的转换

*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include "avformat.h"
#include "avcodec.h"
#include "avdevice.h"
#include <libavutil/imgutils.h>  
#include <libswscale/swscale.h>
 
char* input_name= "video4linux2";
char* file_name = "/dev/video0";
char* out_file  = "yuv420.yuv";
 
void captureOneFrame(void){
    AVFormatContext *fmtCtx = NULL;    
    AVInputFormat     *inputFmt;
    AVPacket         *packet;
    AVCodecContext    *pCodecCtx;
    AVCodec         *pCodec;
    struct SwsContext *sws_ctx;  
    FILE *fp;
    int i;
    int ret;
    int videoindex;
    
    enum AVPixelFormat dst_pix_fmt = AV_PIX_FMT_YUV420P;
    const char *dst_size = NULL;  
    const char *src_size = NULL;  
    uint8_t *src_data[4];   
    uint8_t *dst_data[4];  
    int src_linesize[4];  
    int dst_linesize[4];  
    int src_bufsize;  
    int dst_bufsize;  
    int src_w ;  
    int src_h ;  
    int dst_w = 1280;   
    int dst_h = 960;
 
    fp = fopen(out_file, "wb");    
    if (fp < 0)    {        
        printf("open frame data file failed\n");        
        return ;    
    }    
 
    inputFmt = av_find_input_format (input_name);    
   
    if (inputFmt == NULL)    {        
        printf("can not find_input_format\n");        
        return;    
    }    
 
    if (avformat_open_input ( &fmtCtx, file_name, inputFmt, NULL) < 0){
        printf("can not open_input_file\n");         return;    
    }
    
    av_dump_format(fmtCtx, 0, file_name, 0);
 
    videoindex= -1;
    for(i=0; i<fmtCtx->nb_streams; i++)
        if(fmtCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO){
            videoindex=i;
            break;
        }
    if(videoindex==-1){
        printf("Didn't find a video stream.\n");
        return -1;
    }
 
    pCodecCtx = fmtCtx->streams[videoindex]->codec;
    pCodec    = avcodec_find_decoder(pCodecCtx->codec_id);
 
    printf("picture width   =  %d \n", pCodecCtx->width);
    printf("picture height  =  %d \n", pCodecCtx->height);
    printf("Pixel   Format  =  %d \n", pCodecCtx->pix_fmt);
        
    sws_ctx = sws_getContext( pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt, dst_w, dst_h, dst_pix_fmt,  
                             SWS_BILINEAR, NULL, NULL, NULL);
 
    src_bufsize = av_image_alloc(src_data, src_linesize, pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt, 16);
    dst_bufsize = av_image_alloc(dst_data, dst_linesize, dst_w, dst_h, dst_pix_fmt, 1);
    
    packet = (AVPacket *)av_malloc(sizeof(AVPacket));  
 
    int loop = 1000;
//    while(loop--){
        av_read_frame(fmtCtx, packet);
        memcpy(src_data[0], packet->data, packet->size);
        sws_scale(sws_ctx, src_data,  src_linesize, 0, pCodecCtx->height, dst_data, dst_linesize);
        fwrite(dst_data[0], 1, dst_bufsize, fp);
//    }
    
    fclose(fp);    
    av_free_packet(packet);    
    av_freep(&dst_data[0]);
    sws_freeContext(sws_ctx);
    avformat_close_input(&fmtCtx);
 }
 
int main(void){    
    avcodec_register_all();    
    avdevice_register_all();    
    captureOneFrame();    
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值