ffmpeg使用vp8编解码

  1. /*
  2.  * copyright (c) 2001 Fabrice Bellard
  3.  *
  4.  * This file is part of FFmpeg.
  5.  *
  6.  * FFmpeg is free software; you can redistribute it and/or
  7.  * modify it under the terms of the GNU Lesser General Public
  8.  * License as published by the Free Software Foundation; either
  9.  * version 2.1 of the License, or (at your option) any later version.
  10.  *
  11.  * FFmpeg is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14.  * Lesser General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU Lesser General Public
  17.  * License along with FFmpeg; if not, write to the Free Software
  18.  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19.  */
  20. #include
  21. #include
  22. #include
  23.  
  24. #ifdef HAVE_AV_CONFIG_H
  25. #undef HAVE_AV_CONFIG_H
  26. #endif
  27.  
  28. #include "libavcodec/avcodec.h"
  29. #include "libavutil/mathematics.h"
  30.  
  31. #define INBUF_SIZE 4096
  32. #define AUDIO_INBUF_SIZE 20480
  33. #define AUDIO_REFILL_THRESH 4096
  34.  
  35. /*
  36.  * Video encoding example
  37.  */
  38. static  void video_encode_example ( const  char  *filename )
  39. {
  40.     AVCodec  *codec ;
  41.     AVCodecContext  *c = NULL ;
  42.      int i , out_size , size , x , y , outbuf_size ;
  43.     FILE  *f ;
  44.     AVFrame  *picture ;
  45.     uint8_t  *outbuf ,  *picture_buf ;
  46.  
  47.      printf ( "Video encoding\n" ) ;
  48.  
  49.      /* find the mpeg1 video encoder */
  50.     codec  = avcodec_find_encoder (CODEC_ID_VP8 ) ;
  51.      if  ( !codec )  {
  52.         fprintf (stderr ,  "codec not found\n" ) ;
  53.         exit (1 ) ;
  54.      }
  55.  
  56.     c = avcodec_alloc_context ( ) ;
  57.     picture = avcodec_alloc_frame ( ) ;
  58.  
  59.      /* put sample parameters */
  60.     c ->bit_rate  =  400000 ;
  61.      /* resolution must be a multiple of two */
  62.     c ->width  =  352 ;
  63.     c ->height  =  288 ;
  64.      /* frames per second */
  65.     c ->time_base =  (AVRational ) {1 ,25 } ;
  66.     c ->gop_size  =  10 ;  /* emit one intra frame every ten frames */
  67.     c ->max_b_frames = 1 ;
  68.     c ->pix_fmt  = PIX_FMT_YUV420P ;
  69.  
  70.      /* open it */
  71.      if  (avcodec_open (c , codec )  <  0 )  {
  72.         fprintf (stderr ,  "could not open codec\n" ) ;
  73.         exit (1 ) ;
  74.      }
  75.  
  76.     f  = fopen (filename ,  "wb" ) ;
  77.      if  ( !f )  {
  78.         fprintf (stderr ,  "could not open %s\n" , filename ) ;
  79.         exit (1 ) ;
  80.      }
  81.  
  82.      /* alloc image and output buffer */
  83.     outbuf_size  =  100000 ;
  84.     outbuf  = malloc (outbuf_size ) ;
  85.     size  = c ->width  * c ->height ;
  86.     picture_buf  = malloc ( (size  * 3 )  / 2 ) ;  /* size for YUV 420 */
  87.  
  88.     picture ->data [0 ]  = picture_buf ;
  89.     picture ->data [1 ]  = picture ->data [0 ]  + size ;
  90.     picture ->data [2 ]  = picture ->data [1 ]  + size  /  4 ;
  91.     picture ->linesize [0 ]  = c ->width ;
  92.     picture ->linesize [1 ]  = c ->width  /  2 ;
  93.     picture ->linesize [2 ]  = c ->width  /  2 ;
  94.  
  95.      /* encode 1 second of video */
  96.      for (i = 0 ;i < 25 ;i ++ )  {
  97.         fflush (stdout ) ;
  98.          /* prepare a dummy image */
  99.          /* Y */
  100.          for (y = 0 ;y <c ->height ;y ++ )  {
  101.              for (x = 0 ;x <c ->width ;x ++ )  {
  102.                 picture ->data [0 ] [* picture ->linesize [0 ]  + x ]  = x  + y  + i  *  3 ;
  103.              }
  104.          }
  105.  
  106.          /* Cb and Cr */
  107.          for (y = 0 ;y <c ->height / 2 ;y ++ )  {
  108.              for (x = 0 ;x <c ->width / 2 ;x ++ )  {
  109.                 picture ->data [1 ] [* picture ->linesize [1 ]  + x ]  = 128  + y  + i  *  2 ;
  110.                 picture ->data [2 ] [* picture ->linesize [2 ]  + x ]  = 64  + x  + i  *  5 ;
  111.              }
  112.          }
  113.  
  114.          /* encode the image */
  115.         out_size  = avcodec_encode_video (c , outbuf , outbuf_size , picture ) ;
  116.          printf ( "encoding frame %3d (size=%5d)\n" , i , out_size ) ;
  117.         fwrite (outbuf , 1 , out_size , f ) ;
  118.      }
  119.  
  120.      /* get the delayed frames */
  121.      /*for(; out_size; i++) {
  122.         fflush(stdout);
  123.  
  124.         out_size = avcodec_encode_video(c, outbuf, outbuf_size, NULL);
  125.         printf("write frame %3d (size=%5d)\n", i, out_size);
  126.         fwrite(outbuf, 1, out_size, f);
  127.     }*/
  128.  
  129.      /* add sequence end code to have a real mpeg file */
  130.      /*outbuf[0] = 0x00;
  131.     outbuf[1] = 0x00;
  132.     outbuf[2] = 0x01;
  133.     outbuf[3] = 0xb7;
  134.     fwrite(outbuf, 1, 4, f);*/
  135.    
  136.     fclose (f ) ;
  137.     free (picture_buf ) ;
  138.     free (outbuf ) ;
  139.  
  140.     avcodec_close (c ) ;
  141.     av_free (c ) ;
  142.     av_free (picture ) ;
  143.      printf ( "\n" ) ;
  144. }
  145.  
  146. 原文地址
  147.  
  148. int main ( int argc ,  char  **argv )
  149. {
  150.      const  char  *filename ;
  151.  
  152.      /* must be called before using avcodec lib */
  153.     avcodec_init ( ) ;
  154.  
  155.      /* register all the codecs */
  156.     avcodec_register_all ( ) ;
  157.  
  158.     video_encode_example ( "testa.webm" ) ;
  159.  
  160.      return  0 ;
  161. }

你可能感兴趣的:(Multimedia)