With the completion of my graduation thesis, I finally made room for free these two days, and I had time to study FFMPEG. I thought that I had been engaged in FFMPEG decoding and seldom involved in FFMPEG encoding. So I plan to study FFMPEG encoding. I read some examples on the Internet and found that it would be slightly more difficult, or the class library would be outdated, so I decided to develop an encoding example for later study.
The encoder in this article implements YUV420P data encoding as JPEG images. Based on the simple principle, the code is basically reduced to the limit. The latest FFMPEG class library compiled in May 6, 2014 is used.
The program is very simple. After opening the project, you can directly run it to encode YUV data as JPEG. This program is flexible and can be modified to encode encoder of various image formats as needed, such as PNG and GIF.
The platform uses VC2010.
The following code is used directly. Notes are provided for important parts.
/** The Simplest FFmpeg-based image Encoder * Simplest FFmpeg Picture Encoder ** leixiao Lei Xiaohua * leixiaohua1020@126.com * China Media University/Digital TV technology * Communication University of China/Digital TV Technology * http://blog.csdn.net/leixiaohua1020 ** this program realizes the YUV420P pixel data encoding as JPEG image. Is the simplest FFmpeg encoding tutorial. * By studying this example, you can understand the FFmpeg encoding process. * This software encode YUV420P data to JPEG format file. it's the simplest encode software based on FFmpeg. * Suitable for beginner of FFmpeg */# include "stdafx. h "extern" C "{# include <libavcodec \ avcodec. h> # include <libavformat \ avformat. h> # include <libswscale \ swscale. h >}; int _ tmain (int argc, _ TCHAR * argv []) {AVFormatContext * pFormatCtx; AVOutputFormat * fmt; AVStream * video_st; AVCodecContext * pCodecCtx; AV Codec * pCodec; uint8_t * picture_buf; AVFrame * picture; int size; FILE * in_file = fopen ("cuc_view_480x272.yuv", "rb"); // video YUV source FILE int in_w = 480, in_h = 272; // const char * out_file = "cuc_view_encode.jpg"; // output file path av_register_all (); // method 1. combine several functions, pFormatCtx = avformat_alloc_context (); // guess the format. Use MJPEG encoding fmt = av_guess_format ("mjpeg", NULL, NULL); pFormatCtx-> oformat = fmt; // note: the output path if (avio_open (& pFormatCtx-> pb, out_file, AVIO_FLAG_READ_WRITE) <0) {printf ("failed to open the output file"); return-1 ;}// method 2. more automated // avformat_alloc_output_context2 (& pFormatCtx, NULL, NULL, out_file); // fmt = pFormatCtx-> oformat; video_st = av_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 = bytes; pCodecCtx-> width = in_w; pCodecCtx-> height = in_h; pCodecCtx-> time_base.num = 1; pCodecCtx-> time_base.den = 25; // output format information av_dump_format (pFormatCtx, 0, out_file, 1); pCodec = encode (pCodecCtx-> codec_id ); if (! PCodec) {printf ("no suitable encoder found! "); Return-1;} if (avcodec_open2 (pCodecCtx, pCodec, NULL) <0) {printf (" encoder opening failed! "); Return-1;} picture = avcodec_alloc_frame (); size = bytes (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 the file header avformat_write_header (pFormatCtx, NULL); AVPacket pkt; int y_size = pCodecCtx-> width * pCodecCtx-> height; av_new_packet (& pkt, y_size * 3 ); // read YUVif (fread (picture_buf, 1, y_size * 3/2, in_file) <0) {printf ("file read error"); return-1 ;} picture-> data [0] = picture_buf; // brightness Ypicture-> Data [1] = picture_buf + y_size; // U picture-> data [2] = picture_buf + y_size * 5/4; // Vint got_picture = 0; // encode int ret = avcodec_encode_video2 (pCodecCtx, & pkt, picture, & got_picture); if (ret <0) {printf ("Encoding Error! \ N "); return-1;} if (got_picture = 1) {pkt. stream_index = video_st-> index; ret = av_write_frame! \ 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 data before encoding:
Encoded JPEG:
Complete Project:
Http://download.csdn.net/detail/leixiaohua1020/7319265
SourceForge Project address:
Https://sourceforge.net/projects/simplestffmpegpictureencoder/