Advertisement
Guest User

Untitled

a guest
Jan 13th, 2013
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.57 KB | None | 0 0
  1. static void encodeAac( const char *infilename,const char *filename)
  2. {
  3.     AVCodec *codec;
  4.     AVCodecContext *c = NULL;
  5.     int frame_size, i, j, out_size, outbuf_size;
  6.     FILE *f,*fin;
  7.     SAMPLE *samples;
  8.     float t, tincr;
  9.     uint8_t *outbuf;
  10.        
  11.     avcodec_register_all();                         //Load all codecs
  12.     av_register_all();
  13.     codec = avcodec_find_encoder(AV_CODEC_ID_AAC);  //Search for AAC codec
  14.     if (!codec) {
  15.         error("Codec not found");
  16.     }
  17.     c = avcodec_alloc_context();
  18.     c->bit_rate = 64000;
  19.     c->sample_fmt = AV_SAMPLE_FMT_S16;
  20.     c->sample_rate = SAMPLE_RATE;
  21.     c->channels = NUM_CHANNELS;
  22.     c->time_base.num= 1;
  23.     c->time_base.den= SAMPLE_RATE;
  24.     c->profile= FF_PROFILE_AAC_MAIN;
  25.     if (avcodec_open(c, codec) < 0) {
  26.         error(add("couldn","",avcodec_open(c, codec)).c_str());
  27.         exit(1);
  28.     }
  29.     f = fopen(filename, "wb");
  30.     fin=fopen(infilename,"rb");
  31.     if (!fin) {
  32.         error("could not open temporary file");
  33.     }
  34.     if (!f) {
  35.         error("could not open output file");
  36.     }
  37.     std::cout << c->frame_size*c->channels << std::endl;
  38.     samples = new SAMPLE[c->frame_size*c->channels];
  39.     outbuf = new uint8_t[FRAMES_PER_BUFFER * NUM_CHANNELS];
  40.     while(fread(samples,sizeof(SAMPLE),c->frame_size*c->channels,fin)){
  41.         out_size=avcodec_encode_audio(c,outbuf,FRAMES_PER_BUFFER * NUM_CHANNELS,samples);
  42.         fwrite(outbuf,sizeof(uint8_t),out_size,f);
  43.     }
  44.     for(int i=1;i<=4;i++){              //For buffer flushing
  45.     out_size=avcodec_encode_audio(c,outbuf,FRAMES_PER_BUFFER * NUM_CHANNELS,NULL);
  46.     fwrite(outbuf,sizeof(uint8_t),out_size,f);
  47.     }
  48.    
  49.     fclose(f);
  50.     delete outbuf;
  51.     delete samples;
  52.  
  53.     avcodec_close(c);
  54.     av_free(c);
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement