zoukankan      html  css  js  c++  java
  • 最简单的基于FFMPEG的图像编码器(YUV编码为JPEG)

    伴随着毕业论文的完毕,这两天最终腾出了空暇,又有时间搞搞FFMPEG的研究了。想着之前一直搞的都是FFMPEG解码方面的工作,非常少涉及到FFMPEG编码方面的东西,于是打算研究一下FFMPEG的编码。在网上看了一些样例,发现要不然是难度稍微有些大,要不然就是类库比較陈旧,于是就决定自己做一个编码方面的样例,方便以后学习。

    本文的编码器实现了YUV420P的数据编码为JPEG图片。本着简单的原则,代码基本上精简到了极限。使用了2014年5月6号编译的最新的FFMPEG类库。

    程序非常easy,打开project后直接执行就可以将YUV数据编码为JPEG。本程序十分灵活,能够依据须要改动成编码各种图像格式的编码器,比方PNG,GIF等等。

    平台使用VC2010。

    以下直接上代码。比較重要的地方都有凝视。

    /* 
     *最简单的基于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的编码流程。
     *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 <libavcodecavcodec.h>
    #include <libavformatavformat.h>
    #include <libswscaleswscale.h>
    };
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	AVFormatContext* pFormatCtx;
    	AVOutputFormat* fmt;
    	AVStream* video_st;
    	AVCodecContext* pCodecCtx;
    	AVCodec* pCodec;
    
    	uint8_t* picture_buf;
    	AVFrame* picture;
    	int size;
    
    	FILE *in_file = fopen("cuc_view_480x272.yuv", "rb");	//视频YUV源文件 
    	int in_w=480,in_h=272;									//宽高
    	const char* out_file = "cuc_view_encode.jpg";					//输出文件路径
    
    	av_register_all();
    
    	//方法1.组合使用几个函数
    	pFormatCtx = avformat_alloc_context();
    	//猜格式。用MJPEG编码
    	fmt = av_guess_format("mjpeg", NULL, NULL);
    	pFormatCtx->oformat = fmt;
    	//注意:输出路径
    	if (avio_open(&pFormatCtx->pb,out_file, AVIO_FLAG_READ_WRITE) < 0)
    	{
    		printf("输出文件打开失败");
    		return -1;
    	}
    
    	//方法2.更加自己主动化一些
    	//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 = PIX_FMT_YUVJ420P;
    
    	pCodecCtx->width = in_w;  
    	pCodecCtx->height = in_h;
    
    	pCodecCtx->time_base.num = 1;  
    	pCodecCtx->time_base.den = 25;   
    	//输出格式信息
    	av_dump_format(pFormatCtx, 0, out_file, 1);
    
    	pCodec = avcodec_find_encoder(pCodecCtx->codec_id);
    	if (!pCodec)
    	{
    		printf("没有找到合适的编码器!");
    		return -1;
    	}
    	if (avcodec_open2(pCodecCtx, pCodec,NULL) < 0)
    	{
    		printf("编码器打开失败!");
    		return -1;
    	}
    	picture = avcodec_alloc_frame();
    	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);
    
    	//写文件头
    	avformat_write_header(pFormatCtx,NULL);
    
    	AVPacket pkt;
    	int y_size = pCodecCtx->width * pCodecCtx->height;
    	av_new_packet(&pkt,y_size*3);
    	//读入YUV
    	if (fread(picture_buf, 1, y_size*3/2, in_file) < 0)
    	{
    		printf("文件读取错误");
    		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
    	int got_picture=0;
    	//编码
    	int ret = avcodec_encode_video2(pCodecCtx, &pkt,picture, &got_picture);
    	if(ret < 0)
    	{
    		printf("编码错误!
    ");
    		return -1;
    	}
    	if (got_picture==1)
    	{
    		pkt.stream_index = video_st->index;
    		ret = av_write_frame(pFormatCtx, &pkt);
    	}
    
    	av_free_packet(&pkt);
    	//写文件尾
    	av_write_trailer(pFormatCtx);
    
    	printf("编码成功!
    ");
    
    	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数据:


    编码后的JPEG:


    完整project下载地址:

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

    SourceForge项目地址:

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

  • 相关阅读:
    django1.8升级1.9的几个问题
    App免费推广途径概要
    Django Channels 入门指南
    小谈业务应用架构
    比技术债更可怕的人债
    js数据结构与算法--递归
    常见react面试题汇总
    如何使用koa实现socket.io官网的例子
    Vue插槽
    10分钟了解 react 引入的 Hooks
  • 原文地址:https://www.cnblogs.com/mfrbuaa/p/3765160.html
Copyright © 2011-2022 走看看