Hey all,
Im currently writing on a video player with Qt and I want to use QAudioOutput from the Multimedia module together with ffmpeg. However after decoding the audio data most of the time I just hear noise, below my code:

Qt Code:
  1. void MainWindow::try_with_ffmpeg()
  2. {
  3. av_register_all();
  4.  
  5. int audioStream = -1;
  6. AVCodecContext *codecContext;
  7. AVCodec *codec;
  8. AVPacket packet;
  9.  
  10. av_open_input_file(&formatContext, WMV, NULL,0,NULL);
  11. av_find_stream_info(formatContext);
  12. av_dump_format(formatContext, 0, WMV,0);
  13.  
  14. for( unsigned int i=0; i < formatContext->nb_streams; i++ )
  15. {
  16. if( formatContext->streams[i]->codec->codec_type == CODEC_TYPE_AUDIO )
  17. {
  18. audioStream = i;
  19. break;
  20. }
  21. }
  22. if( audioStream == -1 )
  23. {
  24. qWarning() << "Didnt find audio stream";
  25. return;
  26. }
  27.  
  28. codecContext = formatContext->streams[audioStream]->codec;
  29.  
  30. codec = avcodec_find_decoder(codecContext->codec_id);
  31. avcodec_open(codecContext,codec);
  32.  
  33. med_buffer = (uint8_t*)malloc(AVCODEC_MAX_AUDIO_FRAME_SIZE + FF_INPUT_BUFFER_PADDING_SIZE);
  34. buffer.open(QBuffer::WriteOnly);
  35.  
  36. int i=0;
  37.  
  38. while( av_read_frame(formatContext,&packet) >= 0 )
  39. {
  40. qint64 written=0;
  41. if( packet.stream_index == audioStream )
  42. {
  43. // qDebug() << "reading audio packet";
  44. int len, data_size = AVCODEC_MAX_AUDIO_FRAME_SIZE;
  45. len = avcodec_decode_audio3(codecContext, (int16_t*)med_buffer, &data_size, &packet);
  46. if( len <= 0 )
  47. qWarning() << "no data read to buffer";
  48. else
  49. {
  50. written = buffer.write((const char*)med_buffer, len);
  51.  
  52. qDebug()<< "i= " << ++i << "len = " << len << "\twritten =" << written << "\tbuffer.size() =" << buffer.size();
  53. }
  54.  
  55. if( buffer.size() > 10000000 )
  56. {
  57. qDebug() << "buffer.size()=" << buffer.size();
  58. break;
  59. }
  60.  
  61. }
  62.  
  63. }
  64. qDebug() << "bitRate= " << codecContext->bit_rate
  65. << "\nsampleRate = " << codecContext->sample_rate;
  66. QAudioFormat format;
  67. format.setFrequency(codecContext->sample_rate);
  68. format.setChannels(2);
  69. format.setSampleSize(16);
  70. format.setCodec("audio/pcm");
  71. format.setByteOrder(QAudioFormat::LittleEndian);
  72. format.setSampleType(QAudioFormat::SignedInt);
  73.  
  74. QAudioDeviceInfo info(QAudioDeviceInfo::defaultOutputDevice());
  75. if (!info.isFormatSupported(format)) {
  76. qWarning()<<"raw audio format not supported by backend, cannot play audio.";
  77. format = info.nearestFormat(format);
  78. }
  79.  
  80. audio = new QAudioOutput(format, this);
  81.  
  82. connect(audio,SIGNAL(stateChanged(QAudio::State)),SLOT(stateChanged(QAudio::State)));
  83.  
  84. if( !buffer.open(QBuffer::ReadWrite) )
  85. qWarning() << "Couldnt open Buffer";
  86.  
  87. qDebug() << "buffer.size()=" << buffer.size();
  88.  
  89. audio->start(&buffer);
  90. }
To copy to clipboard, switch view to plain text mode 

If I convert a file with ffmpeg -i bla.ogg -acodec pcm_s16le output.wav to a pcm file, the audio stream is played correctly, however, if I use another codec audio fails.

Any Ideas?