1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
| do{ if(avformat_open_input(&fmtCtx,NULL,NULL,NULL)<0){ qDebug("Cannot open input file."); return -1; } if(avformat_find_stream_info(fmtCtx,NULL)<0){ qDebug("Cannot find any stream in file."); return -1; }
for(size_t i=0;i<fmtCtx->nb_streams;i++){ if(fmtCtx->streams[i]->codecpar->codec_type==AVMEDIA_TYPE_AUDIO){ aStreamIndex=(int)i; break; } } if(aStreamIndex==-1){ qDebug("Cannot find audio stream."); return -1; }
AVCodecParameters *aCodecPara = fmtCtx->streams[aStreamIndex]->codecpar; AVCodec *codec = avcodec_find_decoder(aCodecPara->codec_id); if(!codec){ qDebug("Cannot find any codec for audio."); return -1; } codecCtx = avcodec_alloc_context3(codec); if(avcodec_parameters_to_context(codecCtx,aCodecPara)<0){ qDebug("Cannot alloc codec context."); return -1; } codecCtx->pkt_timebase = fmtCtx->streams[aStreamIndex]->time_base;
if(avcodec_open2(codecCtx,codec,NULL)<0){ qDebug("Cannot open audio codec."); return -1; }
uint64_t out_channel_layout = codecCtx->channel_layout; enum AVSampleFormat out_sample_fmt = AV_SAMPLE_FMT_S16; int out_sample_rate = codecCtx->sample_rate; int out_channels = av_get_channel_layout_nb_channels(out_channel_layout);
uint8_t *audio_out_buffer = (uint8_t*)av_malloc(MAX_AUDIO_FRAME_SIZE*2);
SwrContext *swr_ctx = swr_alloc_set_opts(NULL, out_channel_layout, out_sample_fmt, out_sample_rate, codecCtx->channel_layout, codecCtx->sample_fmt, codecCtx->sample_rate, 0,NULL); swr_init(swr_ctx);
QAudioFormat audioFmt; audioFmt.setSampleRate(codecCtx->sample_rate); audioFmt.setChannelCount(out_channels); audioFmt.setSampleSize(16); audioFmt.setCodec("audio/pcm"); audioFmt.setByteOrder(QAudioFormat::LittleEndian); audioFmt.setSampleType(QAudioFormat::SignedInt);
QAudioDeviceInfo info = QAudioDeviceInfo::defaultOutputDevice(); if(!info.isFormatSupported(audioFmt)){ audioFmt = info.nearestFormat(audioFmt); } QAudioOutput *audioOutput = new QAudioOutput(audioFmt); audioOutput->setVolume(100);
QIODevice *streamOut = audioOutput->start();
QByteArray pcmData;
while(av_read_frame(fmtCtx,pkt)>=0){ if(pkt->stream_index==aStreamIndex){ if(avcodec_send_packet(codecCtx,pkt)>=0){ while(avcodec_receive_frame(codecCtx,frame)>=0){ if(av_sample_fmt_is_planar(codecCtx->sample_fmt)){ int len = swr_convert(swr_ctx, &audio_out_buffer, MAX_AUDIO_FRAME_SIZE*2, (const uint8_t**)frame->data, frame->nb_samples); if(len<=0){ continue; } int out_size = av_samples_get_buffer_size(0, out_channels, len, out_sample_fmt, 1); pcmData.append((char*)audio_out_buffer,out_size); } } } } av_packet_unref(pkt); }
bool isStop = false; QByteArray tempBuf = pcmData; int chunks=0; while(audioOutput && audioOutput->state()!=QAudio::StoppedState && audioOutput->state()!=QAudio::SuspendedState && !isStop){ if(tempBuf.isEmpty()){ tempBuf = pcmData; Sleep(1000); } chunks = audioOutput->bytesFree()/audioOutput->periodSize(); if(chunks){ if(tempBuf.length() >= audioOutput->periodSize()){ streamOut->write(tempBuf.data(),audioOutput->periodSize()); tempBuf = tempBuf.mid(audioOutput->periodSize()); }else{ streamOut->write(tempBuf); tempBuf.clear(); } } } }while(0);
av_frame_free(&frame); av_packet_free(&pkt); avcodec_close(codecCtx); avcodec_free_context(&codecCtx); avformat_free_context(fmtCtx);
|