• 嵌入式linux------ffmpeg移植 编码H264(am335x编码H264) 分类: TI-AM335X ffmpeg-SDL-VLC-Live555 arm-linux-Ubuntu 2015-08-04 09:34 5人阅读 评论(0) 收藏


    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 ");  
    84.         return -1;  
    85.     }  
    86.   
    87.     /*AVCodecParserContext *avParserContext = av_parser_init(CODEC_ID_H264);//得到解析帧类,主要用于后面的帧头查找 
    88.     if(!avParserContext) 
    89.     { 
    90.         printf("av_parser_init  error "); 
    91.         return -1; 
    92.     }*/  
    93.     AVCodecContext *codec_ = avcodec_alloc_context3(videoCodec);//编码会话层  
    94.     if(!codec_)  
    95.     {  
    96.         printf("avcodec_alloc_context3  error ");  
    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 ");  
    118.         return -1;  
    119.     }  
    120.   
    121.   
    122.     FILE *myH264 = fopen("t.264""wb");  
    123.     if(myH264 == NULL)  
    124.     {  
    125.         perror("cant open 264 file ");  
    126.         return -1;  
    127.     }  
    128.   
    129.     FILE *yuvfile = fopen("my264.yuv""rb");  
    130.     if(yuvfile == NULL)  
    131.     {  
    132.         perror("cant open YUV file ");  
    133.         return -1;  
    134.     }  
    135.           
    136.     int readFileLen = 1;  
    137.     char readBuf[MAX_LEN];  
    138.   
    139.     printf("readBuf address  is %x ", 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 ");  
    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>  

    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    Linux下命令行安装weblogic10.3.6
    11g新特性:Health Monitor Checks
    Oracle/PLSQL: ORA-06550
    DBMS_NETWORK_ACL_ADMIN
    【RDA】使用RDA(Remote Diagnostic Agent)工具对数据库进行健康检查
    ORA-39242 错误
    Yii2 中常用的增删改查操作总结
    PHP递归函数return返回null的问题
    PHP中生成随机字符串,数字+大小写字母随机组合
    使用layer.msg 时间设置不起作用
  • 原文地址:https://www.cnblogs.com/mao0504/p/4705494.html
Copyright © 2020-2023  润新知