最簡單的基於FFMPEG的圖像編碼器(YUV編碼爲JPEG)(轉)

原文轉自 https://blog.csdn.net/leixiaohua1020/article/details/25346147/html

 

伴隨着畢業論文的完成,這兩天終於騰出了空閒,又有時間搞搞FFMPEG的研究了。想着以前一直搞的都是FFMPEG解碼方面的工做,不多涉及到FFMPEG編碼方面的東西,因而打算研究一下FFMPEG的編碼。在網上看了一些例子,發現要否則是難度略微有些大,要否則就是類庫比較陳舊,因而就決定本身作一個編碼方面的例子,方便之後學習。git

簡介

本文的編碼器實現了YUV420P的數據編碼爲JPEG圖片。本着簡單的原則,代碼基本上精簡到了極限。使用了2014年5月6號編譯的最新的FFMPEG類庫。github

程序很簡單,打開工程後直接運行便可將YUV數據編碼爲JPEG。本程序十分靈活,能夠根據須要修改爲編碼各類圖像格式的編碼器,好比PNG,GIF等等。平臺使用VC2010。ide

源代碼

/**
 * 最簡單的基於FFmpeg的圖像編碼器
 * Simplest FFmpeg Picture Encoder
 * 
 * 雷霄驊 Lei Xiaohua
 * leixiaohua1020@126.com
 * 中國傳媒大學/數字電視技術
 * Communication University of China / Digital TV Technology
 * http://blog.csdn.net/leixiaohua1020
 * 
 * 本程序實現了YUV420P像素數據編碼爲JPEG圖片。是最簡單的FFmpeg編碼方面的教程。
 * 經過學習本例子能夠了解FFmpeg的編碼流程。
 */

#include <stdio.h>

#define __STDC_CONSTANT_MACROS

#ifdef _WIN32
//Windows
extern "C"
{
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
};
#else
//Linux...
#ifdef __cplusplus
extern "C"
{
#endif
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#ifdef __cplusplus
};
#endif
#endif


int main(int argc, char* argv[])
{
    AVFormatContext* pFormatCtx;
    AVOutputFormat* fmt;
    AVStream* video_st;
    AVCodecContext* pCodecCtx;
    AVCodec* pCodec;

    uint8_t* picture_buf;
    AVFrame* picture;
    AVPacket pkt;
    int y_size;
    int got_picture=0;
    int size;

    int ret=0;

    FILE *in_file = NULL;                            //YUV source
    int in_w=480,in_h=272;                           //YUV's width and height
    const char* out_file = "cuc_view_encode.jpg";    //Output file

    in_file = fopen("cuc_view_480x272.yuv", "rb");

    av_register_all();

    //Method 1
    pFormatCtx = avformat_alloc_context();
    //Guess format
    fmt = av_guess_format("mjpeg", NULL, NULL);
    pFormatCtx->oformat = fmt;
    //Output URL
    if (avio_open(&pFormatCtx->pb,out_file, AVIO_FLAG_READ_WRITE) < 0){
        printf("Couldn't open output file.");
        return -1;
    }

    //Method 2. More simple
    //avformat_alloc_output_context2(&pFormatCtx, NULL, NULL, out_file);
    //fmt = pFormatCtx->oformat;

    video_st = avformat_new_stream(pFormatCtx, 0);
    if (video_st==NULL){
        return -1;
    }
    pCodecCtx = video_st->codec;
    pCodecCtx->codec_id = fmt->video_codec;
    pCodecCtx->codec_type = AVMEDIA_TYPE_VIDEO;
    pCodecCtx->pix_fmt = AV_PIX_FMT_YUVJ420P;

    pCodecCtx->width = in_w;  
    pCodecCtx->height = in_h;

    pCodecCtx->time_base.num = 1;  
    pCodecCtx->time_base.den = 25;   
    //Output some information
    av_dump_format(pFormatCtx, 0, out_file, 1);

    pCodec = avcodec_find_encoder(pCodecCtx->codec_id);
    if (!pCodec){
        printf("Codec not found.");
        return -1;
    }
    if (avcodec_open2(pCodecCtx, pCodec,NULL) < 0){
        printf("Could not open codec.");
        return -1;
    }
    picture = av_frame_alloc();
    size = avpicture_get_size(pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height);
    picture_buf = (uint8_t *)av_malloc(size);
    if (!picture_buf)
    {
        return -1;
    }
    avpicture_fill((AVPicture *)picture, picture_buf, pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height);

    //Write Header
    avformat_write_header(pFormatCtx,NULL);

    y_size = pCodecCtx->width * pCodecCtx->height;
    av_new_packet(&pkt,y_size*3);
    //Read YUV
    if (fread(picture_buf, 1, y_size*3/2, in_file) <=0)
    {
        printf("Could not read input file.");
        return -1;
    }
    picture->data[0] = picture_buf;              // Y
    picture->data[1] = picture_buf+ y_size;      // U 
    picture->data[2] = picture_buf+ y_size*5/4;  // V

    //Encode
    ret = avcodec_encode_video2(pCodecCtx, &pkt,picture, &got_picture);
    if(ret < 0){
        printf("Encode Error.\n");
        return -1;
    }
    if (got_picture==1){
        pkt.stream_index = video_st->index;
        ret = av_write_frame(pFormatCtx, &pkt);
    }

    av_free_packet(&pkt);
    //Write Trailer
    av_write_trailer(pFormatCtx);

    printf("Encode Successful.\n");

    if (video_st){
        avcodec_close(video_st->codec);
        av_free(picture);
        av_free(picture_buf);
    }
    avio_close(pFormatCtx->pb);
    avformat_free_context(pFormatCtx);

    fclose(in_file);

    return 0;
}

結果

編碼前的YUV420P數據:post

 

編碼後的JPEG:學習

 

下載

 

simplest ffmpeg picture encoderui

 

項目主頁編碼

SourceForge:https://sourceforge.net/projects/simplestffmpegpictureencoder/spa

Github:https://github.com/leixiaohua1020/simplest_ffmpeg_picture_encoder.net

開源中國:http://git.oschina.net/leixiaohua1020/simplest_ffmpeg_picture_encoder

 

 

CSDN工程下載地址:

http://download.csdn.net/detail/leixiaohua1020/7319265

PUDN工程下載地址:

http://www.pudn.com/downloads644/sourcecode/multimedia/detail2605252.html

 

本程序實現了YUV420P像素數據編碼爲JPEG圖片。是最簡單的FFmpeg編碼方面的教程。經過學習本例子能夠了解FFmpeg的編碼流程。

 

更新-1.1(2015.2.13)=========================================

此次考慮到了跨平臺的要求,調整了源代碼。通過此次調整以後,源代碼能夠在如下平臺編譯經過:

VC++:打開sln文件便可編譯,無需配置。

cl.exe:打開compile_cl.bat便可命令行下使用cl.exe進行編譯,注意可能須要按照VC的安裝路徑調整腳本里面的參數。編譯命令以下。

 

::VS2010 Environment
call "D:\Program Files\Microsoft Visual Studio 10.0\VC\vcvarsall.bat"
::include
@set INCLUDE=include;%INCLUDE%
::lib
@set LIB=lib;%LIB%
::compile and link
cl simplest_ffmpeg_picture_encoder.cpp /link avcodec.lib avformat.lib avutil.lib ^
avdevice.lib avfilter.lib postproc.lib swresample.lib swscale.lib /OPT:NOREF

 

MinGW:MinGW命令行下運行compile_mingw.sh便可使用MinGW的g++進行編譯。編譯命令以下。

 

g++ simplest_ffmpeg_picture_encoder.cpp -g -o simplest_ffmpeg_picture_encoder.exe \
-I /usr/local/include -L /usr/local/lib \
-lavformat -lavcodec -lavutil

 

GCC:Linux或者MacOS命令行下運行compile_gcc.sh便可使用GCC進行編譯。編譯命令以下。

 

gcc simplest_ffmpeg_picture_encoder.cpp -g -o simplest_ffmpeg_picture_encoder.out \
-I /usr/local/include -L /usr/local/lib -lavformat -lavcodec -lavutil

PS:相關的編譯命令已經保存到了工程文件夾中

 

CSDN下載地址:http://download.csdn.net/detail/leixiaohua1020/8444893

SourceForge上已經更新。