Embedded Linux------ffmpeg Migration encoding H264 (am335x encoding H264)

Source: Internet
Author: User

/*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 <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 * 50int        Main () {//below initializes the H264 decoding Library//avcodec_init (); Av_register_all (); Avcodec_register_all ();/* Find the video encoder */avcodec *videocodec = Avcodec_find_encoder (codec_id_h264);// Get 264 Encoder class if (!VIDEOCODEC) {printf ("Avcodec_find_decoder error\n"); return-1;} /*avcodecparsercontext *avparsercontext = Av_parser_init (codec_id_h264);//Gets the parsing frame class, mainly used for the rear frame header to find if (!avparsercontext) { printf ("Av_parser_init error\n"); return-1;}  */avcodeccontext *codec_ = Avcodec_alloc_context3 (VIDEOCODEC);//Encode Session layer if (!CODEC_) {printf ("Avcodec_alloc_context3 Error\n "); return-1;} EarlyTo initialize the parameters, the following parameters should be determined by the specific business codec_->time_base.num = 1;        Codec_->time_base.den = 25;//Frame rate codec_->gop_size = 1;        Codec_->max_b_frames = 1;        Codec_->thread_count = 1; CODEC_-&GT;PIX_FMT = Pix_fmt_yuv420p;//codec_->frame_number = 1; One video frame per package//codec_->codec_type = Avmedia_type_video;codec_->bit_rate = 1000000;codec_->width = 720;// Video width codec_->height = 576;//video High if (Avcodec_open2 (codec_, Videocodec, NULL) < 0)//Open Encoder {printf ("Avcodec_open2 error \ n "); return-1;} FILE *myh264 = fopen ("t.264", "WB"), if (myH264 = = NULL) {perror ("Cant open 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];p rintf ("Readbuf address is%x\n", readbuf);//int outbuf_size=100000;unsigned C Har * 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");         } else {Avpicture_fill ((avpicture*) m_pyuvframe, (unsigned char *) yuv_buf,pix_fmt_yuv420p,720,576);         int got_packet_ptr = 0;         Av_init_packet (&AMP;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);}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Embedded Linux------ffmpeg Migration encoding H264 (am335x encoding H264)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.