音视频入门-8-ffmpeg小实验-v4l2 ubuntu取图、格式转换、编码成H264,ffplay观看

 

1. getpic_transform_encode2h264.c

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <time.h>
#include <sys/time.h>
#include "avformat.h"
#include "avcodec.h"
#include "avdevice.h"
#include <libavutil/imgutils.h>  
#include <libswscale/swscale.h> 
 

/*  知识点: ffmpeg新版本中(封装流)AVStream的codec参数被codecpar参数所替代
    该知识点的讲解博客
    https://blog.csdn.net/weixin_34419326/article/details/91775446?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.control&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.control
*/

#define    LOOP_NUM            300
#define OUT_WIDTH            320
#define OUT_HEIGHT            240
 
char* input_name= "video4linux2";
char* file_name = "/dev/video0";
 
struct timeval  time_val;
float      time_start;
float      time_end;
 
float get_diff_time(struct timeval* start , int update)
{
    float dt;
    struct timeval now;
    gettimeofday(&now, NULL);
    dt = (float)(now.tv_sec  - start->tv_sec);
    dt += (float)(now.tv_usec - start->tv_usec) * 1e-6;
    
    if (update == 1) {
        start->tv_sec = now.tv_sec;
        start->tv_usec = now.tv_usec;
    }
    
    return dt;
}
 
int flush_encoder(AVFormatContext *fmt_ctx,unsigned int stream_index){
    int ret;
    int got_frame;
    AVPacket enc_pkt;
    if (!(fmt_ctx->streams[stream_index]->codec->codec->capabilities & AV_CODEC_CAP_DELAY))
        return 0;
 
    while (1) {
        enc_pkt.data = NULL;
        enc_pkt.size = 0;
        av_init_packet(&enc_pkt);
        ret = avcodec_encode_video2 (fmt_ctx->streams[stream_index]->codec, &enc_pkt,
            NULL, &got_frame);
        av_frame_free(NULL);
        if (ret < 0)
            break;
        if (!got_frame){
            ret=0;
            break;
        }
        printf("Flush Encoder: Succeed to encode 1 frame!\tsize:%5d\n",enc_pkt.size);
        ret = av_write_frame(fmt_ctx, &enc_pkt);
        if (ret < 0)
            break;
    }
    return ret;
}
 
 
void captureFrame(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 = OUT_WIDTH;   
    int dst_h = OUT_HEIGHT;
    
    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));  
 
    /* set out format */
    AVFormatContext        *outFormatCtx;
    AVOutputFormat        *outfmt;
    AVCodecContext        *outCodecCtx;  
    AVStream            *video_st;
    AVDictionary         *param = 0;
    AVCodec                *outCodec;
    AVFrame                *outFrame;
    AVPacket             outpkt;
 
    uint8_t *picture_buf; 
    char    *out_file  = "ds.h264";
    int     picture_size;
    int     y_size;
    int        got_picture;
    int     loop = 0;
 
    outFormatCtx = avformat_alloc_context();
    outfmt = av_guess_format(NULL, out_file, NULL);  
    outFormatCtx->oformat = outfmt; 
    if (avio_open(&outFormatCtx->pb, out_file, AVIO_FLAG_READ_WRITE) < 0){  
        printf("Failed to open output file! \n");  
        return -1;  
    }
    video_st = avformat_new_stream(outFormatCtx, 0); 
    if (video_st==NULL){
        printf(" creat new stream err \n ");  
        return -1;  
    }  
    
    outCodecCtx = video_st->codec;
    outCodecCtx->codec_id = outfmt->video_codec;
    outCodecCtx->codec_type = AVMEDIA_TYPE_VIDEO;
    outCodecCtx->pix_fmt = AV_PIX_FMT_YUV420P;
    outCodecCtx->width = dst_w;    
    outCodecCtx->height = dst_h;  
    outCodecCtx->bit_rate = 2000000;    
    outCodecCtx->gop_size=10;  
  
    outCodecCtx->time_base.num = 1;    
    outCodecCtx->time_base.den = 25;   
    outCodecCtx->qmin = 10;  
    outCodecCtx->qmax = 51;  
    outCodecCtx->max_b_frames=3; 
    
    if(pCodecCtx->codec_id == AV_CODEC_ID_H264) { 
        av_dict_set(&param,"preset", "faster", 0);
        //av_dict_set(&param,"preset", "slow", 0);
        av_dict_set(&param,"tune", "zerolatency", 0);
    }
 
    av_dump_format(outFormatCtx, 0, out_file, 1);
    
    outCodec = avcodec_find_encoder(outCodecCtx->codec_id);
    if (!outCodec){  
        printf("Can not find encoder! \n");  
        return -1;  
    } 
 
    if (avcodec_open2(outCodecCtx, outCodec, &param) < 0){  
        printf("Failed to open encoder! \n");  
        return -1;  
    }  
 
    outFrame = av_frame_alloc();
    picture_size = avpicture_get_size(outCodecCtx->pix_fmt, outCodecCtx->width, outCodecCtx->height);
    picture_buf = (uint8_t *)av_malloc(picture_size); 
    avpicture_fill((AVPicture *)outFrame, picture_buf, outCodecCtx->pix_fmt, outCodecCtx->width, outCodecCtx->height);
    outFrame->format = outCodecCtx->pix_fmt;
    outFrame->width  = outCodecCtx->width;
    outFrame->height = outCodecCtx->height;    
    avformat_write_header(outFormatCtx,NULL);
    av_new_packet(&outpkt,picture_size);
    y_size = outCodecCtx->width * outCodecCtx->height; 
    
    time_start = get_diff_time(&time_val, 1);
    while(loop++ < LOOP_NUM){
        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); 
        
 
        outFrame->data[0] = dst_data[0];
        outFrame->data[1] = dst_data[0] + y_size;
        outFrame->data[2] = dst_data[0] + y_size*5/4;
        
        outFrame->pts=(loop -1)*(video_st->time_base.den)/((video_st->time_base.num)*25);
        ret = avcodec_encode_video2(outCodecCtx, &outpkt, outFrame, &got_picture);
        if(ret < 0)
        {
            printf("Failed to encode! \n");
            return -1;
        } 
 
        if(got_picture==1){
            outpkt.stream_index = video_st->index;
            ret = av_write_frame(outFormatCtx, &outpkt);
            av_free_packet(&outpkt); 
        }
    }
 
    time_end = get_diff_time(&time_val, 0);
    printf("\n\nencoder %d frame  spend time = %f \n\n",loop, time_end);
    
    ret = flush_encoder(outFormatCtx,0);
    if(ret < 0){
        printf("Flushing encoder failed\n");  
        return -1;
    }
 
    av_write_trailer(outFormatCtx);  
    if (video_st){  
        avcodec_close(video_st->codec);  
        av_free(outFrame);  
        av_free(picture_buf);  
    }  
    avio_close(outFormatCtx->pb);  
    avformat_free_context(outFormatCtx); 
    
    av_free_packet(packet);    
    av_freep(&dst_data[0]);
    sws_freeContext(sws_ctx);
    avformat_close_input(&fmtCtx);
 } 
 
int main(void){    
    av_register_all(); 
    avcodec_register_all();    
    avdevice_register_all();    
    captureFrame();    
    return 0;
}
 

 

2. makefile

OUT_APP         = test
INCLUDE_PATH = /usr/local/ffmpeg/include/
INCLUDE = -I$(INCLUDE_PATH) -I$(INCLUDE_PATH)libavutil/ -I$(INCLUDE_PATH)libavdevice/ \
            -I$(INCLUDE_PATH)libavcodec/ -I$(INCLUDE_PATH)libswresample \
            -I$(INCLUDE_PATH)libavfilter/ -I$(INCLUDE_PATH)libavformat \
            -I$(INCLUDE_PATH)libswscale/
 
LIB_PATH = /usr/local/ffmpeg/lib/
FFMPEG_LIBS = -L$(LIB_PATH) -lavformat -lavutil -lavdevice -lavcodec -lswresample -lavfilter -lswscale
SDL_LIBS    = 
LIBS        = $(FFMPEG_LIBS)$(SDL_LIBS)
 
COMPILE_OPTS = $(INCLUDE)
C              = c
OBJ          = o
C_COMPILER   = cc
C_FLAGS      = $(COMPILE_OPTS) $(CPPFLAGS) $(CFLAGS)
 
LINK          = cc -o 
LINK_OPTS    = -lz -lm  -lpthread
LINK_OBJ     = getpic_transform_encode2h264.o 
 
.$(C).$(OBJ):
    $(C_COMPILER) -c -g $(C_FLAGS) $<
 
 
$(OUT_APP): $(LINK_OBJ)
    $(LINK)$@  $(LINK_OBJ)  $(LIBS) $(LINK_OPTS)
 
clean:
        -rm -rf *.$(OBJ) $(OUT_APP) core *.core *~  picture

 

实测可能是编码的时间过长,视频分辨率又高,所以导致播放出来跟快进一样了。 以后再分析吧。

 

 

 

.

posted @ 2021-01-13 16:39  一匹夫  阅读(323)  评论(0编辑  收藏  举报