嵌入式linux------ffmpeg移植 编码H264(am335x编码H264)

时间:2021-09-02 17:19:52
  1. <pre name="code" class="cpp"><pre name="code" class="cpp">/*
  2. 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
  3. */
  4. #include "stdio.h"
  5. #include "stdlib.h"
  6. #include "libavformat/avformat.h"
  7. #include "libavdevice/avdevice.h"
  8. #include "libswresample/swresample.h"
  9. #include "libavutil/opt.h"
  10. #include "libavutil/channel_layout.h"
  11. #include "libavutil/parseutils.h"
  12. #include "libavutil/samplefmt.h"
  13. #include "libavutil/fifo.h"
  14. #include "libavutil/intreadwrite.h"
  15. #include "libavutil/dict.h"
  16. #include "libavutil/mathematics.h"
  17. #include "libavutil/pixdesc.h"
  18. #include "libavutil/avstring.h"
  19. #include "libavutil/imgutils.h"
  20. #include "libavutil/timestamp.h"
  21. #include "libavutil/bprint.h"
  22. #include "libavutil/time.h"
  23. #include "libavutil/threadmessage.h"
  24. #include "libavfilter/avcodec.h"
  25. #include "libavcodec/avcodec.h"
  26. #if HAVE_SYS_RESOURCE_H
  27. #include <sys/time.h>
  28. #include <sys/types.h>
  29. #include <sys/resource.h>
  30. #elif HAVE_GETPROCESSTIMES
  31. #include <windows.h>
  32. #endif
  33. #if HAVE_GETPROCESSMEMORYINFO
  34. #include <windows.h>
  35. #include <psapi.h>
  36. #endif
  37. #if HAVE_SYS_SELECT_H
  38. #include <sys/select.h>
  39. #endif
  40. #if HAVE_TERMIOS_H
  41. #include <fcntl.h>
  42. #include <sys/ioctl.h>
  43. #include <sys/time.h>
  44. #include <termios.h>
  45. #elif HAVE_KBHIT
  46. #include <conio.h>
  47. #endif
  48. #if HAVE_PTHREADS
  49. #include <pthread.h>
  50. #endif
  51. #include <time.h>
  52. #include "libavutil/avassert.h"
  53. #define MAX_LEN  1024 * 50
  54. int main()
  55. {
  56. //下面初始化h264解码库
  57. //avcodec_init();
  58. av_register_all();
  59. avcodec_register_all();
  60. /* find the video encoder */
  61. AVCodec *videoCodec = avcodec_find_encoder(CODEC_ID_H264);//得到264的编码器类
  62. if(!videoCodec)
  63. {
  64. printf("avcodec_find_decoder error\n");
  65. return -1;
  66. }
  67. /*AVCodecParserContext *avParserContext = av_parser_init(CODEC_ID_H264);//得到解析帧类,主要用于后面的帧头查找
  68. if(!avParserContext)
  69. {
  70. printf("av_parser_init  error\n");
  71. return -1;
  72. }*/
  73. AVCodecContext *codec_ = avcodec_alloc_context3(videoCodec);//编码会话层
  74. if(!codec_)
  75. {
  76. printf("avcodec_alloc_context3  error\n");
  77. return -1;
  78. }
  79. //初始化参数,下面的参数应该由具体的业务决定
  80. codec_->time_base.num = 1;
  81. codec_->time_base.den = 25;//帧率
  82. codec_->gop_size = 1;
  83. codec_->max_b_frames = 1;
  84. codec_->thread_count = 1;
  85. codec_->pix_fmt = PIX_FMT_YUV420P;
  86. //codec_->frame_number = 1; //每包一个视频帧
  87. //codec_->codec_type = AVMEDIA_TYPE_VIDEO;
  88. codec_->bit_rate = 1000000;
  89. codec_->width = 720;//视频宽
  90. codec_->height = 576;//视频高
  91. if(avcodec_open2(codec_, videoCodec, NULL) < 0)//打开编码器
  92. {
  93. printf("avcodec_open2 error\n");
  94. return -1;
  95. }
  96. FILE *myH264 = fopen("t.264", "wb");
  97. if(myH264 == NULL)
  98. {
  99. perror("cant open 264 file\n");
  100. return -1;
  101. }
  102. FILE *yuvfile = fopen("my264.yuv", "rb");
  103. if(yuvfile == NULL)
  104. {
  105. perror("cant open YUV file\n");
  106. return -1;
  107. }
  108. int readFileLen = 1;
  109. char readBuf[MAX_LEN];
  110. printf("readBuf address  is %x\n", readBuf);
  111. //
  112. int outbuf_size=100000;
  113. unsigned char * outbuf= malloc(outbuf_size);
  114. int u_size=0;
  115. AVPacket avpkt;
  116. unsigned char * yuv_buf= malloc(720*576*3/2);
  117. AVFrame *m_pYUVFrame=malloc(sizeof(AVFrame));
  118. int flag=0;
  119. while(1)
  120. {
  121. int len = fread(yuv_buf,720*576*3/2,1,yuvfile);
  122. if(len<=0)
  123. {
  124. printf("read over\n");
  125. break;
  126. }
  127. else
  128. {
  129. avpicture_fill((AVPicture*)m_pYUVFrame,(unsigned char *)yuv_buf,PIX_FMT_YUV420P,720,576);
  130. int got_packet_ptr =0;
  131. av_init_packet(&avpkt);
  132. avpkt.data=outbuf;
  133. avpkt.size=outbuf_size;
  134. while(1)
  135. {
  136. u_size = avcodec_encode_video(codec_,outbuf,outbuf_size,m_pYUVFrame);
  137. if(u_size>0 && u_size<100000)
  138. {
  139. m_pYUVFrame->pts++;
  140. fwrite(avpkt.data,1,u_size,myH264);
  141. flag++;
  142. break;
  143. }
  144. }
  145. }
  146. // if(flag>20)break;
  147. }
  148. avcodec_close(codec_);
  149. av_free(codec_);
  150. fclose(yuvfile);
  151. fclose(myH264);
  152. }
  153. </pre><br><br></pre>