It's attached. Guess I never did change the main file yet (I was
writing some of the threads in separate files, but I thought I had
changed the main file as well - guess I didn't yet).

Should do exactly what you want.

Follow the wave header info on the link I provided previously to
figure out what's happening with writing the header, and right at the
end where I go back and fill  in the length fields.

Andrew

On Sat, Mar 13, 2010 at 3:54 PM, Yusuke Kenta <[email protected]> wrote:
> Hi Andrew,
>
> Thank you for the reply. Now I understand why I am getting wav files but has
> no information about it. It will be great if you can send some bits of the
> code to help me understand how to write the header.
>
> Regards,
>
> Yusuke Kenta
>
> On Sat, Mar 13, 2010 at 10:11 PM, Andrew Hakman 
> <[email protected]>wrote:
>
>> Don't re-encode to "wav", just write out the header yourself, and dump
>> out the samples you get from decode_audio_3.
>> For a good explanation of the header specifics, see
>> https://ccrma.stanford.edu/courses/422/projects/WaveFormat/
>>
>> I had code that did just that a couple weeks ago, but it's in a
>> non-functional state right now as I'm breaking it into multiple
>> threads (and writing wav files wasn't the end goal, just an
>> intermediate step to see that things are working to that point).
>> Haven't had much time to work on that program recently, but I could
>> probably extract some bits of it and send it to you if you thought it
>> would help.
>>
>> Andrew
>>
>> On Sat, Mar 13, 2010 at 9:09 AM, Edward Hau <[email protected]> wrote:
>> > Hi,
>> >
>> > I am using Martin Böhme example from:
>> > http://www.inb.uni-luebeck.de/~boehme/using_libavcodec.html<http://www.inb.uni-luebeck.de/%7Eboehme/using_libavcodec.html>to
>> >  open the
>> > video file and read it then implement libavcodec codec to get audio
>> stream.
>> >
>> > I have several attempts so far but no luck and keeping errors due to my
>> lack
>> > of understand how to process and save audio as wav (using defaults from
>> > libavcodec) from video.
>> >
>> > I am using Microsoft Visual studio 2008 and here the coding so far:
>> >
>> > #include <libavcodec\avcodec.h>
>> > #include <libavformat\avformat.h>
>> >
>> > #define INBUF_SIZE 4096
>> >
>> > int main(int argc, char *argv[]) {
>> >    FILE            *outputFile;
>> >    unsigned int    i, audioStream;
>> >    int                frame_size, out_size, enout_size, enoutbuf_size,
>> len;
>> >    short            *samples;
>> >    AVCodecContext    *pCodecCtx, *audioEn;
>> >    AVCodec            *pCodec, *aCodec;
>> >    AVFormatContext    *pFormatCtx;
>> >    CHAR            filename[1027];
>> >    uint8_t            *outbuf, *enoutbuf;
>> >    AVPacket        packet;
>> >
>> >    av_register_all();
>> >
>> > if(av_open_input_file(&pFormatCtx, filename, NULL, 0, NULL)!=0) return
>> -1;
>> > if(av_find_stream_info(pFormatCtx)<0)return -1;
>> > dump_format(pFormatCtx, 0, filename, false);
>> >
>> >    //Find audio stream
>> >    for(i=0; i < pFormatCtx->nb_streams; i++){
>> >        if(pFormatCtx->streams[i]->codec->codec_type==CODEC_TYPE_AUDIO)
>> >        {
>> >            audioStream=i;
>> >            break;
>> >        }
>> >    }
>> >
>> >    pCodecCtx=pFormatCtx->streams[audioStream]->codec;
>> >
>> > // Find the decoder for the audio stream
>> >    pCodec=avcodec_find_decoder(pCodecCtx->codec_id);
>> >    if(pCodec==NULL){
>> >       fprintf(stderr, "Cannot find decoder");
>> >        return -1; // Codec not found
>> >    }
>> >
>> >    if(avcodec_open(pCodecCtx,pCodec)<0) return -1;
>> >
>> >    outbuf = (uint8_t*)malloc(AVCODEC_MAX_AUDIO_FRAME_SIZE);
>> >
>> >    aCodec=avcodec_find_encoder(CODEC_ID_PCM_S16LE);
>> >    if(pCodec==NULL){
>> >        fprintf(stderr, "Cannot find decoder");
>> >        return -1; // Codec not found
>> >    }
>> >
>> >    //setting up encoder
>> >    audioEn = avcodec_alloc_context();
>> >    audioEn->bit_rate = 1411000;
>> >    audioEn->sample_rate = 44100;
>> >    audioEn->channels = 2;
>> >    audioEn->sample_fmt = SAMPLE_FMT_S16;
>> >
>> >    if(avcodec_open(audioEn,aCodec)<0){
>> >        fprintf(stderr, "Could not open encoder codec");
>> >        std::cin.get();
>> >    }
>> >
>> >    frame_size = audioEn->frame_size;
>> >    samples = (short*)malloc(frame_size * 2 * audioEn->channels);
>> >    enoutbuf_size = 10000;
>> >    enoutbuf = (uint8_t*)malloc(enoutbuf_size);
>> >
>> >    outputFile = fopen(".\\temp1.wav", "wb");
>> >    if (!outputFile) {
>> >        fprintf(stderr, "could not open %s\n", outputFile);
>> >        std::cin.get();
>> >        exit(1);
>> >    }
>> >
>> >    while(av_read_frame(pFormatCtx, &packet)>=0){
>> >        if(packet.stream_index==audioStream){
>> >            for(;;){
>> >                if (packet.size == 0)
>> >                    break;
>> >
>> >                while(packet.size >0){
>> >                    out_size = AVCODEC_MAX_AUDIO_FRAME_SIZE;
>> >                    // Decode audio frame
>> >                    len = avcodec_decode_audio3(pCodecCtx, (short
>> *)outbuf,
>> > &out_size, &packet);
>> >                    if (len < 0) {
>> >                        fprintf(stderr, "Error while decoding\n");
>> >                        exit(1);
>> >                    }
>> >                    if (out_size > 0) {
>> >                        enout_size=avcodec_encode_audio(audioEn, enoutbuf,
>> > enoutbuf_size, samples);
>> >                        fwrite(enoutbuf, 1, enout_size, outputFile);
>> >                    }
>> >                    packet.size -= len;
>> >                    packet.data += len;
>> >                }
>> >            }
>> >        av_free_packet(&packet);
>> >        }
>> >    }
>> >    fclose(outputFile);
>> >    free(outbuf);
>> >
>> >    // Close the codec
>> >    avcodec_close(pCodecCtx);
>> >
>> >    // Close the video file
>> >    av_close_input_file(pFormatCtx);
>> > }
>> >
>> > Regards,
>> >
>> > Yusuke Kenta
>> > _______________________________________________
>> > libav-user mailing list
>> > [email protected]
>> > https://lists.mplayerhq.hu/mailman/listinfo/libav-user
>> >
>> _______________________________________________
>> libav-user mailing list
>> [email protected]
>> https://lists.mplayerhq.hu/mailman/listinfo/libav-user
>>
> _______________________________________________
> libav-user mailing list
> [email protected]
> https://lists.mplayerhq.hu/mailman/listinfo/libav-user
>
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>

#include <stdio.h>

int main(int argc, char *argv[]) {
  AVFormatContext *pFormatCtx;
  int             i, videoStream;
  int audioStreamCount = 0;
  int audioStreams[4];
  AVCodecContext  *pCodecCtx;
  AVCodecContext  *aCodecCtx[4];
  AVCodec	  *aCodec[4];
  AVCodec         *pCodec;
  AVFrame         *pFrame; 
  AVFrame         *pFrameRGB;
  AVPacket        packet;
  int             frameFinished;
  int             numBytes;
  uint8_t         *buffer;
  int		  wave_tot_byte_count=0;
  int16_t *decode_buffer;
  int packet_counter=0, frame_counter=0;
  

  char *wavebuffer;
  int ok;
  ok=posix_memalign(&wavebuffer, 16, 256);
  if (ok!=0)
  {
    printf("error, could not allocate wave output buffer\n");
    return -1;
  }
  ok=posix_memalign(&decode_buffer, 16, AVCODEC_MAX_AUDIO_FRAME_SIZE*5);
  if (ok!=0)
  {
    printf("error, could not allocate decode buffer\n");
    return -1;
  }

  FILE *waveFile;
  char waveFilename[32];
  
  // Open output wave file
  sprintf(waveFilename, "testout.wave");
  waveFile=fopen(waveFilename, "wb");
  if(waveFile==NULL)
  {
    printf("error, cannot open output wave file\n");
    return -1;
  }

  //write wave header
  i=0;
  sprintf(wavebuffer, "RIFF");
  fwrite(wavebuffer,1,4,waveFile);
  fwrite(&i,4,1,waveFile);
  sprintf(wavebuffer, "WAVEfmt ");
  fwrite(wavebuffer,1,8,waveFile);
  i=16;
  fwrite(&i,4,1,waveFile);
  i=1;
  fwrite(&i,2,1,waveFile);
  i=2; //number of channels
  fwrite(&i,2,1,waveFile);
  i=48000; //sample rate
  fwrite(&i,4,1,waveFile);
  i=192000; //byte rate = samplerate * num channels * bits per sample / 8
  fwrite(&i,4,1,waveFile);
  i=4; //block align - number of bytes for one sample including all channels
  fwrite(&i,2,1,waveFile);
  i=16; //bits per sample
  fwrite(&i,2,1,waveFile);
  sprintf(wavebuffer, "data");
  fwrite(wavebuffer,1,4,waveFile);
  i=0; //data subchunk size - go back and fill this in later
  fwrite(&i,4,1,waveFile);
  //now ready to write actual samples into wave file


  
  if(argc < 2) {
    printf("Please provide a movie file\n");
    return -1;
  }
  // Register all formats and codecs
  av_register_all();
  
  // Open video file
  if(av_open_input_file(&pFormatCtx, argv[1], NULL, 0, NULL)!=0)
    return -1; // Couldn't open file
  
  // Retrieve stream information
  if(av_find_stream_info(pFormatCtx)<0)
    return -1; // Couldn't find stream information
  
  // Dump information about file onto standard error
//  dump_format(pFormatCtx, 0, argv[1], 0);
  
  // Find the first video stream
  // and up to 4 audio streams
  videoStream=-1;
  for (i=0; i<4; ++i)
	  audioStreams[i]=0;
  for(i=0; i<pFormatCtx->nb_streams; i++)
  {
    if(pFormatCtx->streams[i]->codec->codec_type==CODEC_TYPE_VIDEO) {
      videoStream=i;
    }
    if (pFormatCtx->streams[i]->codec->codec_type==CODEC_TYPE_AUDIO)
    {
    	if (audioStreamCount<4)
	{
		audioStreams[audioStreamCount]=i;
		++audioStreamCount;
	}
    }
  }
  printf("found %d audio streams\n", audioStreamCount);
  // Get the pointers to the codec context for the audio streams
  for (i=0; i<audioStreamCount; ++i)
  {
	  aCodecCtx[i]=pFormatCtx->streams[audioStreams[i]]->codec;
	  aCodec[i]=avcodec_find_decoder(aCodecCtx[i]->codec_id);
	  if (aCodec[i]==NULL)
		  fprintf(stderr, "Unsupported codec in audio stream %d\n",i);
	  if (avcodec_open(aCodecCtx[i],aCodec[i])<0)
	    fprintf(stderr, "Can't open codec for audio stream %d\n",i);
  }
  
  // Read frames and save first audio stream to wave file
  i=0;
  while(av_read_frame(pFormatCtx, &packet)>=0)
  {
    uint8_t *audio_pkt_data = NULL;
    int audio_pkt_size = 0;
    int pkt_decoded_len = 0;
    int frame_decoded_len;
    int decode_buff_remain=AVCODEC_MAX_AUDIO_FRAME_SIZE * 5;
    
    // Is this a packet from the video stream?
    if(packet.stream_index==videoStream) 
    {
      printf("video packet, pts=%ld, dts=%ld\n",packet.pts, packet.dts);
    }
    
    //is this a packet from the first audio stream?
    else if(packet.stream_index==audioStreams[0])
    {
      printf("audio 0 packet, pts=%ld, dts=%ld\n",packet.pts, packet.dts);
      //decode the audio packet
      frame_decoded_len=decode_buff_remain;
      int16_t *decode_buff_ptr = decode_buffer;
      int decoded_tot_len=0;
      //++packet_counter;
      //printf("decoding audio packet %d which is %d bytes\n",packet_counter, audio_pkt_size);
      while (audio_pkt_size>0)
      {
	frame_decoded_len=decode_buff_remain;
	//++frame_counter;
	pkt_decoded_len = avcodec_decode_audio3(aCodecCtx[0], decode_buff_ptr, &frame_decoded_len, &packet);
	//printf("called decode_audio2 %d times now, decoded %d bytes of packet, output is %d bytes\n",frame_counter,pkt_decoded_len,frame_decoded_len);
	if (pkt_decoded_len <0) break;
	packet.size -= pkt_decoded_len;
	packet.data += pkt_decoded_len;
	decode_buff_ptr += frame_decoded_len;
	decode_buff_remain -= frame_decoded_len;
	decoded_tot_len += frame_decoded_len;
      }
      //write the decode_buffer out to the wave file
      fwrite(decode_buffer,1,decoded_tot_len,waveFile);
      wave_tot_byte_count+=decoded_tot_len;
    }
    else if(packet.stream_index==audioStreams[1]) printf("audio 1 packet, pts=%ld, dts=%ld\n",packet.pts, packet.dts);
    else if(packet.stream_index==audioStreams[2]) printf("audio 2 packet, pts=%ld, dts=%ld\n",packet.pts, packet.dts);
    else if(packet.stream_index==audioStreams[3]) printf("audio 3 packet, pts=%ld, dts=%ld\n",packet.pts, packet.dts);
    // Free the packet that was allocated by av_read_frame
    av_free_packet(&packet);
  }
  
  
  // Close the video and audio codec
//  avcodec_close(pCodecCtx);
  for (i=0; i<audioStreamCount; ++i)
  {
    avcodec_close(aCodecCtx[i]);
  }
  //go back and fill in the section sizes in the wave file
  fseek(waveFile,4,SEEK_SET);
  i=wave_tot_byte_count+36;
  fwrite(&i,4,1,waveFile);
  fseek(waveFile,40,SEEK_SET);
  fwrite(&wave_tot_byte_count,4,1,waveFile);
  fclose(waveFile);
  
  // Close the video file
  av_close_input_file(pFormatCtx);
  
  return 0;
}
_______________________________________________
libav-user mailing list
[email protected]
https://lists.mplayerhq.hu/mailman/listinfo/libav-user

Reply via email to