Results 1 to 5 of 5

Thread: QAudioOutput + FFMPEG

  1. #1
    Join Date
    Sep 2010
    Posts
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default QAudioOutput + FFMPEG

    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?

  2. #2
    Join Date
    Sep 2010
    Posts
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QAudioOutput + FFMPEG

    Ok solved the problem by myself:

    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. while( av_read_frame(formatContext,&packet) >= 0 )
    37. {
    38. qint64 written=0;
    39. if( packet.stream_index == audioStream )
    40. {
    41. qDebug() << "size= " << packet.size;
    42. while(packet.size > 0)
    43. {
    44. int len, data_size = AVCODEC_MAX_AUDIO_FRAME_SIZE;
    45. //decode
    46. len = avcodec_decode_audio3(codecContext, (int16_t*)med_buffer, &data_size, &packet);
    47. if( len <= 0 )
    48. qWarning() << "no data read to buffer";
    49. if( data_size > 0 )
    50. written = buffer.write((const char*)med_buffer, data_size);
    51.  
    52. packet.size -= len;
    53. packet.data += len;
    54.  
    55. if( buffer.size() > 10000000 )
    56. {
    57. qDebug() << "buffer.size()=" << buffer.size();
    58. break;
    59. }
    60. }
    61.  
    62. }
    63.  
    64. }
    65.  
    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.  
    76. if (!info.isFormatSupported(format)) {
    77. qWarning()<<"raw audio format not supported by backend, cannot play audio.";
    78. format = info.nearestFormat(format);
    79. }
    80.  
    81. audio = new QAudioOutput(format, this);
    82.  
    83. connect(audio,SIGNAL(stateChanged(QAudio::State)),SLOT(stateChanged(QAudio::State)));
    84.  
    85. if( !buffer.open(QBuffer::ReadWrite) )
    86. qWarning() << "Couldnt open Buffer";
    87.  
    88. qDebug() << "buffer.size()=" << buffer.size();
    89.  
    90. audio->start(&buffer);
    91. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jun 2014
    Posts
    3
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: QAudioOutput + FFMPEG

    Hello,

    I'm trying to use FFMPEG for an audio converter but i can't import the libraries. Can you attach the code to see how you did it?

    Thank you very much!

  4. #4
    Join Date
    Mar 2013
    Posts
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Android

    Default Re: QAudioOutput + FFMPEG

    I would like to create sample audio player...Could you pls send me the full code....


    Quote Originally Posted by Biberbaer View Post
    Ok solved the problem by myself:

    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. while( av_read_frame(formatContext,&packet) >= 0 )
    37. {
    38. qint64 written=0;
    39. if( packet.stream_index == audioStream )
    40. {
    41. qDebug() << "size= " << packet.size;
    42. while(packet.size > 0)
    43. {
    44. int len, data_size = AVCODEC_MAX_AUDIO_FRAME_SIZE;
    45. //decode
    46. len = avcodec_decode_audio3(codecContext, (int16_t*)med_buffer, &data_size, &packet);
    47. if( len <= 0 )
    48. qWarning() << "no data read to buffer";
    49. if( data_size > 0 )
    50. written = buffer.write((const char*)med_buffer, data_size);
    51.  
    52. packet.size -= len;
    53. packet.data += len;
    54.  
    55. if( buffer.size() > 10000000 )
    56. {
    57. qDebug() << "buffer.size()=" << buffer.size();
    58. break;
    59. }
    60. }
    61.  
    62. }
    63.  
    64. }
    65.  
    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.  
    76. if (!info.isFormatSupported(format)) {
    77. qWarning()<<"raw audio format not supported by backend, cannot play audio.";
    78. format = info.nearestFormat(format);
    79. }
    80.  
    81. audio = new QAudioOutput(format, this);
    82.  
    83. connect(audio,SIGNAL(stateChanged(QAudio::State)),SLOT(stateChanged(QAudio::State)));
    84.  
    85. if( !buffer.open(QBuffer::ReadWrite) )
    86. qWarning() << "Couldnt open Buffer";
    87.  
    88. qDebug() << "buffer.size()=" << buffer.size();
    89.  
    90. audio->start(&buffer);
    91. }
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Oct 2014
    Posts
    81
    Thanks
    20
    Thanked 9 Times in 9 Posts
    Qt products
    Qt5
    Platforms
    Windows
    Wiki edits
    7

    Default Re: QAudioOutput + FFMPEG

    His post is from 2011, it's not likely that he'll read your request.

    You may want to look at the following:

    - Qt Phonon (4.8) music player example.
    http://qt-project.org/doc/qt-4.8/pho...sicplayer.html

    - Amarok, an advanced open source audio player built with Qt.
    https://amarok.kde.org/en/contribute

    - MLT Framework, a multimedia framework that can be used along with Qt.
    http://www.mltframework.org/bin/view/MLT/Features
    https://github.com/mltframework/BuildOnMe (simple Qt example)
    https://github.com/mltframework/shotcut

Similar Threads

  1. QAudioOutput in a QThread - problem.
    By _franko_ in forum Qt Programming
    Replies: 14
    Last Post: 17th June 2012, 02:05
  2. QAudioOutput + not UI thread
    By medved6 in forum Qt Programming
    Replies: 1
    Last Post: 27th December 2010, 19:11
  3. QAudioOutput buffer underrun
    By BartBlackMagic in forum Qt Programming
    Replies: 1
    Last Post: 16th September 2010, 17:44
  4. Ffmpeg?
    By nthung in forum Qt Programming
    Replies: 1
    Last Post: 10th June 2010, 11:17
  5. QAudioOutput help
    By XavierQT in forum Qt Programming
    Replies: 1
    Last Post: 6th February 2010, 10:35

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.