[FFmpeg-devel] transcoding between audio formats

Steve Jiekak devaureshy
Fri Aug 14 12:42:15 CEST 2009


Hello everyone, I am trying to write a transcoder from an audio stream
  to an adts  stream, and I want to know what steps I should
go to , in particular between decoding  and encoding a frame.
For example, for video you sometimes need to scale the picture...

Actually when I get the audio raw data from decode, I encode directly it,
and the resulting sound is terrible.
There is how my code look:
(for the moment I just get the context options from the source)

Thanks for your help,
Steve Jiekak

  av_write_header(dest->fctx);


while(av_read_frame(src->fctx, &src->packet)>=0) {
    // Is this a packet from the audio stream?
  if(src->packet.stream_index==astream ){

      uint8_t *ptr;
      int len, ret;

      len = src->packet.size;
      ptr = src->packet.data;
    while(len>0){ // there may be more than one frame in a packet
    if(len && len != src->packet.size ){
            fprintf(stderr, "Multiple frames in a packet from stream %d\n",
src->packet.stream_index);
     }
    memset(samples,0,sample_size);
    int data_size = sample_size;

    ret  = avcodec_decode_audio2(src->cctx,samples,&data_size,ptr,len);
        if(ret<0){
        fprintf(stderr,"failing decoding frame...\n");
        return -1;
    }
    ptr+= ret;
    len-=ret;
    if(data_size <=0 || ret == 0){
        av_free_packet(&src->packet);
        fprintf(stderr,"skipping...\n");
        continue;
    }

    //NOW WE ENCODE
     av_init_packet(&dest->packet);

     if( (dest->packet.size =
avcodec_encode_audio(dest->cctx,audio_outbuf,AUDIO_BUF_SIZE,samples)) < 0 ){
          fprintf(stderr,"encoding failed, return %d\n",dest->packet.size );
         exit(dest->packet.size);
      }
      else{
       // fprintf(stderr,"# bytes used, input buffer = %d, decompressed
frame size=%d,# bytes used decoding = %d
\n",dest->packet.size,data_size,ret);
            /*  dest->packet.pts = src->packet.pts;
          if (dest->cctx->coded_frame->pts != AV_NOPTS_VALUE)
        dest->packet.pts= av_rescale_q(dest->cctx->coded_frame->pts,
dest->cctx->time_base, dest->stream->time_base);*/
          dest->packet.data = audio_outbuf;
          dest->packet.stream_index = dest->stream->index;
          av_interleaved_write_frame(dest->fctx, &dest->packet);

      }
    }

  }
    av_free_packet(&src->packet);
}
    av_write_trailer(dest->fctx);



More information about the ffmpeg-devel mailing list