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

简介: <div class="dp-highlighter bg_cpp" style="font-family: Consolas, 'Courier New', Courier, mono, serif; background-color: rgb(231, 229, 220); width: 938.515625px; overflow: hidden; padding-top: 1px; b
[cpp]  view plain copy
  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.   
  5. #include "stdio.h"  
  6. #include "stdlib.h"  
  7.   
  8.   
  9. #include "libavformat/avformat.h"  
  10. #include "libavdevice/avdevice.h"  
  11. #include "libswresample/swresample.h"  
  12. #include "libavutil/opt.h"  
  13. #include "libavutil/channel_layout.h"  
  14. #include "libavutil/parseutils.h"  
  15. #include "libavutil/samplefmt.h"  
  16. #include "libavutil/fifo.h"  
  17. #include "libavutil/intreadwrite.h"  
  18. #include "libavutil/dict.h"  
  19. #include "libavutil/mathematics.h"  
  20. #include "libavutil/pixdesc.h"  
  21. #include "libavutil/avstring.h"  
  22. #include "libavutil/imgutils.h"  
  23. #include "libavutil/timestamp.h"  
  24. #include "libavutil/bprint.h"  
  25. #include "libavutil/time.h"  
  26. #include "libavutil/threadmessage.h"  
  27.   
  28.   
  29. #include "libavfilter/avcodec.h"  
  30. #include "libavcodec/avcodec.h"  
  31.   
  32.   
  33.   
  34.   
  35. #if HAVE_SYS_RESOURCE_H  
  36. #include <sys/time.h>  
  37. #include <sys/types.h>  
  38. #include <sys/resource.h>  
  39. #elif HAVE_GETPROCESSTIMES  
  40. #include <windows.h>  
  41. #endif  
  42. #if HAVE_GETPROCESSMEMORYINFO  
  43. #include <windows.h>  
  44. #include <psapi.h>  
  45. #endif  
  46.   
  47. #if HAVE_SYS_SELECT_H  
  48. #include <sys/select.h>  
  49. #endif  
  50.   
  51. #if HAVE_TERMIOS_H  
  52. #include <fcntl.h>  
  53. #include <sys/ioctl.h>  
  54. #include <sys/time.h>  
  55. #include <termios.h>  
  56. #elif HAVE_KBHIT  
  57. #include <conio.h>  
  58. #endif  
  59.   
  60. #if HAVE_PTHREADS  
  61. #include <pthread.h>  
  62. #endif  
  63.   
  64. #include <time.h>  
  65.   
  66. #include "libavutil/avassert.h"  
  67.   
  68. #define MAX_LEN  1024 * 50  
  69.   
  70.   
  71. int main()  
  72. {  
  73.     //下面初始化h264解码库  
  74.     //avcodec_init();  
  75.     av_register_all();  
  76.         avcodec_register_all();  
  77.   
  78.   
  79.     /* find the video encoder */  
  80.     AVCodec *videoCodec = avcodec_find_encoder(CODEC_ID_H264);//得到264的编码器类  
  81.     if(!videoCodec)  
  82.     {  
  83.         printf("avcodec_find_decoder error\n");  
  84.         return -1;  
  85.     }  
  86.   
  87.     /*AVCodecParserContext *avParserContext = av_parser_init(CODEC_ID_H264);//得到解析帧类,主要用于后面的帧头查找 
  88.     if(!avParserContext) 
  89.     { 
  90.         printf("av_parser_init  error\n"); 
  91.         return -1; 
  92.     }*/  
  93.     AVCodecContext *codec_ = avcodec_alloc_context3(videoCodec);//编码会话层  
  94.     if(!codec_)  
  95.     {  
  96.         printf("avcodec_alloc_context3  error\n");  
  97.         return -1;  
  98.     }  
  99.   
  100.   
  101.     //初始化参数,下面的参数应该由具体的业务决定  
  102.     codec_->time_base.num = 1;  
  103.         codec_->time_base.den = 25;//帧率  
  104.         codec_->gop_size = 1;  
  105.         codec_->max_b_frames = 1;  
  106.         codec_->thread_count = 1;  
  107.         codec_->pix_fmt = PIX_FMT_YUV420P;  
  108.     //codec_->frame_number = 1; //每包一个视频帧  
  109.     //codec_->codec_type = AVMEDIA_TYPE_VIDEO;  
  110.     codec_->bit_rate = 1000000;  
  111.       
  112.     codec_->width = 720;//视频宽  
  113.     codec_->height = 576;//视频高  
  114.   
  115.     if(avcodec_open2(codec_, videoCodec, NULL) < 0)//打开编码器  
  116.     {  
  117.         printf("avcodec_open2 error\n");  
  118.         return -1;  
  119.     }  
  120.   
  121.   
  122.     FILE *myH264 = fopen("t.264""wb");  
  123.     if(myH264 == NULL)  
  124.     {  
  125.         perror("cant open 264 file\n");  
  126.         return -1;  
  127.     }  
  128.   
  129.     FILE *yuvfile = fopen("my264.yuv""rb");  
  130.     if(yuvfile == NULL)  
  131.     {  
  132.         perror("cant open YUV file\n");  
  133.         return -1;  
  134.     }  
  135.           
  136.     int readFileLen = 1;  
  137.     char readBuf[MAX_LEN];  
  138.   
  139.     printf("readBuf address  is %x\n", readBuf);  
  140.       
  141.     //  
  142.     int outbuf_size=100000;  
  143.     unsigned char * outbuf= malloc(outbuf_size);  
  144.     int u_size=0;  
  145.     AVPacket avpkt;  
  146.     unsigned char * yuv_buf= malloc(720*576*3/2);  
  147.     AVFrame *m_pYUVFrame=malloc(sizeof(AVFrame));  
  148.     int flag=0;  
  149.     while(1)  
  150.     {  
  151.         int len = fread(yuv_buf,720*576*3/2,1,yuvfile);  
  152.         if(len<=0)  
  153.          {  
  154.              printf("read over\n");  
  155.          break;  
  156.          }  
  157.          else  
  158.          {  
  159.              avpicture_fill((AVPicture*)m_pYUVFrame,(unsigned char *)yuv_buf,PIX_FMT_YUV420P,720,576);  
  160.              int got_packet_ptr =0;  
  161.              av_init_packet(&avpkt);  
  162.              avpkt.data=outbuf;  
  163.              avpkt.size=outbuf_size;  
  164.              while(1)  
  165.             {  
  166.                u_size = avcodec_encode_video(codec_,outbuf,outbuf_size,m_pYUVFrame);  
  167.                if(u_size>0 && u_size<100000)  
  168.            {  
  169.                     m_pYUVFrame->pts++;  
  170.                     fwrite(avpkt.data,1,u_size,myH264);  
  171.                     flag++;  
  172.                     break;  
  173.            }  
  174.                  
  175.             }  
  176.          }  
  177.         // if(flag>20)break;  
  178.     }  
  179.     avcodec_close(codec_);  
  180.     av_free(codec_);  
  181.   
  182.     fclose(yuvfile);  
  183.     fclose(myH264);  
  184.       
  185. }  
  186.   
  187.   
  188. </pre><br><br></pre>  
相关文章
|
1月前
|
传感器 数据采集 存储
ARM Linux摄像头传感器数据处理全景视野:从板端编码视频到高级应用(一)
ARM Linux摄像头传感器数据处理全景视野:从板端编码视频到高级应用
75 0
|
29天前
|
存储 编解码 数据处理
【FFmpeg 视频基本格式】深入理解FFmpeg:从YUV到PCM,解码到编码(三)
【FFmpeg 视频基本格式】深入理解FFmpeg:从YUV到PCM,解码到编码
29 0
|
29天前
|
存储 编解码 数据处理
【FFmpeg 视频基本格式】深入理解FFmpeg:从YUV到PCM,解码到编码(二)
【FFmpeg 视频基本格式】深入理解FFmpeg:从YUV到PCM,解码到编码
35 0
|
2月前
|
Linux 编译器 C语言
Linux应用开发基础知识——字符文字编码(五)
Linux应用开发基础知识——字符文字编码(五)
57 0
Linux应用开发基础知识——字符文字编码(五)
|
1月前
|
存储 Shell Linux
【Shell 命令集合 备份压缩 】Linux 解码uuencode编码的文件 uudecode 命令 使用指南
【Shell 命令集合 备份压缩 】Linux 解码uuencode编码的文件 uudecode 命令 使用指南
31 0
|
1月前
|
传感器 Linux 数据处理
ARM Linux摄像头传感器数据处理全景视野:从板端编码视频到高级应用(二)
ARM Linux摄像头传感器数据处理全景视野:从板端编码视频到高级应用
45 1
|
29天前
|
存储 缓存 编解码
【FFmpeg 视频基本格式】深入理解FFmpeg:从YUV到PCM,解码到编码(一)
【FFmpeg 视频基本格式】深入理解FFmpeg:从YUV到PCM,解码到编码
38 0
|
1月前
|
传感器 存储 编解码
ARM Linux摄像头传感器数据处理全景视野:从板端编码视频到高级应用(三)
ARM Linux摄像头传感器数据处理全景视野:从板端编码视频到高级应用
51 2
|
8月前
ffmpeg编码报错:more samples than frame size (avcodec_encode_audio2)
ffmpeg编码报错:more samples than frame size (avcodec_encode_audio2)
50 0
ffmpeg编码报错:more samples than frame size (avcodec_encode_audio2)
|
4月前
|
关系型数据库 MySQL Linux
【Linux 下 MySQL5.7 中文编码设置】
【Linux 下 MySQL5.7 中文编码设置】