专家
公告
财富商城
电子网
旗下网站
首页
问题库
专栏
标签库
话题
专家
NEW
门户
发布
提问题
发文章
嵌入式
嵌入式linux------ffmpeg移植 编码H264(am335x编码H264)
2019-07-12 15:12
发布
生成海报
站内文章
/
嵌入式Linux
11837
0
1350
[cpp]
view plain
copy
"code"
class
=
"cpp"
>
"code"
class
=
"cpp"
>
/*
arm-linux-gcc -o yuv2264 yuv2264.c -I/usr/local/ffmpeg_arm/include/ -L/usr/local/ffmpeg_arm/lib/ -lswresample -lavformat -lavutil -lavcodec -lswscale -lx264 libSDL.a
*/
#include "stdio.h"
#include "stdlib.h"
#include "libavformat/avformat.h"
#include "libavdevice/avdevice.h"
#include "libswresample/swresample.h"
#include "libavutil/opt.h"
#include "libavutil/channel_layout.h"
#include "libavutil/parseutils.h"
#include "libavutil/samplefmt.h"
#include "libavutil/fifo.h"
#include "libavutil/intreadwrite.h"
#include "libavutil/dict.h"
#include "libavutil/mathematics.h"
#include "libavutil/pixdesc.h"
#include "libavutil/avstring.h"
#include "libavutil/imgutils.h"
#include "libavutil/timestamp.h"
#include "libavutil/bprint.h"
#include "libavutil/time.h"
#include "libavutil/threadmessage.h"
#include "libavfilter/avcodec.h"
#include "libavcodec/avcodec.h"
#if HAVE_SYS_RESOURCE_H
#include
#include
#include
#elif HAVE_GETPROCESSTIMES
#include
#endif
#if HAVE_GETPROCESSMEMORYINFO
#include
#include
#endif
#if HAVE_SYS_SELECT_H
#include
#endif
#if HAVE_TERMIOS_H
#include
#include
#include
#include
#elif HAVE_KBHIT
#include
#endif
#if HAVE_PTHREADS
#include
#endif
#include
#include "libavutil/avassert.h"
#define MAX_LEN 1024 * 50
int
main()
{
//下面初始化h264解码库
//avcodec_init();
av_register_all();
avcodec_register_all();
/* find the video encoder */
AVCodec *videoCodec = avcodec_find_encoder(CODEC_ID_H264);
//得到264的编码器类
if
(!videoCodec)
{
printf(
"avcodec_find_decoder error "
);
return
-1;
}
/*AVCodecParserContext *avParserContext = av_parser_init(CODEC_ID_H264);//得到解析帧类,主要用于后面的帧头查找
if(!avParserContext)
{
printf("av_parser_init error ");
return -1;
}*/
AVCodecContext *codec_ = avcodec_alloc_context3(videoCodec);
//编码会话层
if
(!codec_)
{
printf(
"avcodec_alloc_context3 error "
);
return
-1;
}
//初始化参数,下面的参数应该由具体的业务决定
codec_->time_base.num = 1;
codec_->time_base.den = 25;
//帧率
codec_->gop_size = 1;
codec_->max_b_frames = 1;
codec_->thread_count = 1;
codec_->pix_fmt = PIX_FMT_YUV420P;
//codec_->frame_number = 1; //每包一个视频帧
//codec_->codec_type = AVMEDIA_TYPE_VIDEO;
codec_->bit_rate = 1000000;
codec_->width = 720;
//视频宽
codec_->height = 576;
//视频高
if
(avcodec_open2(codec_, videoCodec, NULL) < 0)
//打开编码器
{
printf(
"avcodec_open2 error "
);
return
-1;
}
FILE
*myH264 = fopen(
"t.264"
,
"wb"
);
if
(myH264 == NULL)
{
perror(
"cant open 264 file "
);
return
-1;
}
FILE
*yuvfile = fopen(
"my264.yuv"
,
"rb"
);
if
(yuvfile == NULL)
{
perror(
"cant open YUV file "
);
return
-1;
}
int
readFileLen = 1;
char
readBuf[MAX_LEN];
printf(
"readBuf address is %x "
, readBuf);
//
int
outbuf_size=100000;
unsigned
char
* outbuf= malloc(outbuf_size);
int
u_size=0;
AVPacket avpkt;
unsigned
char
* yuv_buf= malloc(720*576*3/2);
AVFrame *m_pYUVFrame=malloc(
sizeof
(AVFrame));
int
flag=0;
while
(1)
{
int
len = fread(yuv_buf,720*576*3/2,1,yuvfile);
if
(len<=0)
{
printf(
"read over "
);
break
;
}
else
{
avpicture_fill((AVPicture*)m_pYUVFrame,(unsigned
char
*)yuv_buf,PIX_FMT_YUV420P,720,576);
int
got_packet_ptr =0;
av_init_packet(&avpkt);
avpkt.data=outbuf;
avpkt.size=outbuf_size;
while
(1)
{
u_size = avcodec_encode_video(codec_,outbuf,outbuf_size,m_pYUVFrame);
if
(u_size>0 && u_size<100000)
{
m_pYUVFrame->pts++;
fwrite(avpkt.data,1,u_size,myH264);
flag++;
break
;
}
}
}
// if(flag>20)break;
}
avcodec_close(codec_);
av_free(codec_);
fclose(yuvfile);
fclose(myH264);
}
Ta的文章
更多
>>
嵌入式linux------ffmpeg移植 编码H264(am335x编码H264)
0 个评论
热门文章
×
关闭
举报内容
检举类型
检举内容
检举用户
检举原因
广告推广
恶意灌水
回答内容与提问无关
抄袭答案
其他
检举说明(必填)
提交
关闭
×
打开微信“扫一扫”,打开网页后点击屏幕右上角分享按钮