嵌入式linux------ffmpeg移植 编码H264(am335x编码H264)
来源:互联网 发布:c语言在线测试网站 编辑:程序博客网 时间:2024/04/28 04:03
- <pre name="code" class="cpp"><pre name="code" class="cpp">
-
-
-
- #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 <sys/time.h>
- #include <sys/types.h>
- #include <sys/resource.h>
- #elif HAVE_GETPROCESSTIMES
- #include <windows.h>
- #endif
- #if HAVE_GETPROCESSMEMORYINFO
- #include <windows.h>
- #include <psapi.h>
- #endif
-
- #if HAVE_SYS_SELECT_H
- #include <sys/select.h>
- #endif
-
- #if HAVE_TERMIOS_H
- #include <fcntl.h>
- #include <sys/ioctl.h>
- #include <sys/time.h>
- #include <termios.h>
- #elif HAVE_KBHIT
- #include <conio.h>
- #endif
-
- #if HAVE_PTHREADS
- #include <pthread.h>
- #endif
-
- #include <time.h>
-
- #include "libavutil/avassert.h"
-
- #define MAX_LEN 1024 * 50
-
-
- int main()
- {
-
-
- av_register_all();
- avcodec_register_all();
-
-
-
- AVCodec *videoCodec = avcodec_find_encoder(CODEC_ID_H264);
- if(!videoCodec)
- {
- printf("avcodec_find_decoder error\n");
- return -1;
- }
-
-
-
-
-
-
-
- AVCodecContext *codec_ = avcodec_alloc_context3(videoCodec);
- if(!codec_)
- {
- printf("avcodec_alloc_context3 error\n");
- 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_->bit_rate = 1000000;
-
- codec_->width = 720;
- codec_->height = 576;
-
- if(avcodec_open2(codec_, videoCodec, NULL) < 0)
- {
- printf("avcodec_open2 error\n");
- return -1;
- }
-
-
- FILE *myH264 = fopen("t.264", "wb");
- if(myH264 == NULL)
- {
- perror("cant open 264 file\n");
- return -1;
- }
-
- FILE *yuvfile = fopen("my264.yuv", "rb");
- if(yuvfile == NULL)
- {
- perror("cant open YUV file\n");
- return -1;
- }
-
- int readFileLen = 1;
- char readBuf[MAX_LEN];
-
- printf("readBuf address is %x\n", 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\n");
- 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;
- }
-
- }
- }
-
- }
- avcodec_close(codec_);
- av_free(codec_);
-
- fclose(yuvfile);
- fclose(myH264);
-
- }
-
-
- </pre><br><br></pre>
0 0