99bool FFmpegDecoder::OpenInput (string &inputFile) {
1010 CloseInput ();
1111
12- if (isHwDecoderEnable) {
12+ if (! isHwDecoderEnable) {
1313 hwDecoderType = av_hwdevice_find_type_by_name (" d3d11va" );
1414 if (hwDecoderType != AV_HWDEVICE_TYPE_NONE) {
1515 isHwDecoderEnable = true ;
@@ -32,9 +32,9 @@ bool FFmpegDecoder::OpenInput(string &inputFile) {
3232 // 超时机制
3333 static const int timeout = 10 ;
3434 auto startTime = std::make_shared<uint64_t >();
35- *startTime = QDateTime::currentDateTime (). toSecsSinceEpoch ();
35+ *startTime = QDateTime::currentSecsSinceEpoch ();
3636 pFormatCtx->interrupt_callback .callback = [](void *ctx) -> int {
37- uint64_t now = QDateTime::currentDateTime (). toSecsSinceEpoch ();
37+ uint64_t now = QDateTime::currentSecsSinceEpoch ();
3838 return now - *(uint64_t *)ctx > timeout;
3939 };
4040 pFormatCtx->interrupt_callback .opaque = startTime.get ();
@@ -45,7 +45,7 @@ bool FFmpegDecoder::OpenInput(string &inputFile) {
4545 }
4646
4747 // 分析超时,退出,可能格式不正确
48- if (QDateTime::currentDateTime (). toSecsSinceEpoch () - *startTime > timeout) {
48+ if (QDateTime::currentSecsSinceEpoch () - *startTime > timeout) {
4949 CloseInput ();
5050 return false ;
5151 }
@@ -69,8 +69,8 @@ bool FFmpegDecoder::OpenInput(string &inputFile) {
6969
7070 // 创建音频解码缓存
7171 if (hasAudioStream) {
72- audioFifoBuffer = shared_ptr<AVFifoBuffer >(
73- av_fifo_alloc ( GetAudioFrameSamples () * GetAudioChannelCount () * 10 ), &av_fifo_free );
72+ audioFifoBuffer = shared_ptr<AVFifo >(
73+ av_fifo_alloc2 ( 0 , GetAudioFrameSamples () * GetAudioChannelCount () * 10 , AV_FIFO_FLAG_AUTO_GROW) );
7474 }
7575 return true ;
7676}
@@ -182,7 +182,6 @@ shared_ptr<AVFrame> FFmpegDecoder::GetNextFrame() {
182182}
183183
184184bool FFmpegDecoder::hwDecoderInit (AVCodecContext *ctx, const enum AVHWDeviceType type) {
185-
186185 if (av_hwdevice_ctx_create (&hwDeviceCtx, type, nullptr , nullptr , 0 ) < 0 ) {
187186 return false ;
188187 }
@@ -213,7 +212,6 @@ bool FFmpegDecoder::OpenVideo() {
213212 if (config->methods & AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX
214213 && config->device_type == hwDecoderType) {
215214 hwPixFmt = config->pix_fmt ;
216- isHwDecoderEnable = true ;
217215 break ;
218216 }
219217 }
@@ -225,6 +223,7 @@ bool FFmpegDecoder::OpenVideo() {
225223 if (isHwDecoderEnable) {
226224 isHwDecoderEnable = hwDecoderInit (pVideoCodecCtx, hwDecoderType);
227225 }
226+
228227 if (avcodec_parameters_to_context (pVideoCodecCtx, pFormatCtx->streams [i]->codecpar ) >= 0 ) {
229228 res = !(avcodec_open2 (pVideoCodecCtx, codec, nullptr ) < 0 );
230229 if (res) {
@@ -247,25 +246,28 @@ bool FFmpegDecoder::OpenVideo() {
247246 return res;
248247}
249248
250- bool FFmpegDecoder::DecodeVideo (const AVPacket *avpkt , shared_ptr<AVFrame> &pOutFrame) {
249+ bool FFmpegDecoder::DecodeVideo (const AVPacket *av_pkt , shared_ptr<AVFrame> &pOutFrame) {
251250 bool res = false ;
252251
253- if (pVideoCodecCtx && avpkt && pOutFrame) {
254- int ret = avcodec_send_packet (pVideoCodecCtx, avpkt );
252+ if (pVideoCodecCtx && av_pkt && pOutFrame) {
253+ int ret = avcodec_send_packet (pVideoCodecCtx, av_pkt );
255254 if (ret < 0 ) {
256255 char errStr[AV_ERROR_MAX_STRING_SIZE];
257256 av_strerror (ret, errStr, AV_ERROR_MAX_STRING_SIZE);
258257 throw runtime_error (" 发送视频包出错 " + string (errStr));
259258 }
260- if (isHwDecoderEnable && !hwFrame) {
261- hwFrame = shared_ptr<AVFrame>(av_frame_alloc (), &freeFrame);
262- }
263259
264260 if (isHwDecoderEnable) {
261+ // Initialize the hardware frame.
262+ if (!hwFrame) {
263+ hwFrame = shared_ptr<AVFrame>(av_frame_alloc (), &freeFrame);
264+ }
265+
265266 ret = avcodec_receive_frame (pVideoCodecCtx, hwFrame.get ());
266267 } else {
267268 ret = avcodec_receive_frame (pVideoCodecCtx, pOutFrame.get ());
268269 }
270+
269271 if (ret == AVERROR (EAGAIN) || ret == AVERROR_EOF) {
270272 // No output available right now or end of stream
271273 res = false ;
@@ -277,17 +279,20 @@ bool FFmpegDecoder::DecodeVideo(const AVPacket *avpkt, shared_ptr<AVFrame> &pOut
277279 // Successfully decoded a frame
278280 res = true ;
279281 }
282+
280283 if (isHwDecoderEnable) {
281284 if (dropCurrentVideoFrame) {
282285 pOutFrame.reset ();
283286 return false ;
284287 }
285- if ((ret = av_hwframe_transfer_data (pOutFrame.get (), hwFrame.get (), 0 )) < 0 ) {
286- if (ret < 0 ) {
287- char errStr[AV_ERROR_MAX_STRING_SIZE];
288- av_strerror (ret, errStr, AV_ERROR_MAX_STRING_SIZE);
289- throw runtime_error (" Decode video frame error. " + string (errStr));
290- }
288+
289+ // Copy data from the hw surface to the out frame.
290+ ret = av_hwframe_transfer_data (pOutFrame.get (), hwFrame.get (), 0 );
291+
292+ if (ret < 0 ) {
293+ char errStr[AV_ERROR_MAX_STRING_SIZE];
294+ av_strerror (ret, errStr, AV_ERROR_MAX_STRING_SIZE);
295+ throw runtime_error (" Decode video frame error. " + string (errStr));
291296 }
292297 }
293298 }
@@ -364,9 +369,10 @@ int FFmpegDecoder::DecodeAudio(int nStreamIndex, const AVPacket *avpkt, uint8_t
364369 if (audioFrame->format != AV_SAMPLE_FMT_S16) {
365370 // Convert frame to AV_SAMPLE_FMT_S16 if needed
366371 if (!swrCtx) {
367- auto ptr = swr_alloc_set_opts (
368- nullptr , pAudioCodecCtx->channel_layout , AV_SAMPLE_FMT_S16, pAudioCodecCtx->sample_rate ,
369- pAudioCodecCtx->channel_layout , static_cast <AVSampleFormat>(audioFrame->format ),
372+ SwrContext *ptr = nullptr ;
373+ swr_alloc_set_opts2 (
374+ &ptr, &pAudioCodecCtx->ch_layout , AV_SAMPLE_FMT_S16, pAudioCodecCtx->sample_rate ,
375+ &pAudioCodecCtx->ch_layout , static_cast <AVSampleFormat>(audioFrame->format ),
370376 pAudioCodecCtx->sample_rate , 0 , nullptr );
371377
372378 auto ret = swr_init (ptr);
@@ -383,11 +389,12 @@ int FFmpegDecoder::DecodeAudio(int nStreamIndex, const AVPacket *avpkt, uint8_t
383389 int samples = swr_convert (
384390 swrCtx.get (), &pDest, audioFrame->nb_samples , (const uint8_t **)audioFrame->data ,
385391 audioFrame->nb_samples );
386- sizeToDecode = samples * pAudioCodecCtx->channels * av_get_bytes_per_sample (AV_SAMPLE_FMT_S16);
392+ sizeToDecode
393+ = samples * pAudioCodecCtx->ch_layout .nb_channels * av_get_bytes_per_sample (AV_SAMPLE_FMT_S16);
387394 } else {
388395 // Copy S16 audio data directly
389396 sizeToDecode = av_samples_get_buffer_size (
390- nullptr , pAudioCodecCtx->channels , audioFrame->nb_samples , AV_SAMPLE_FMT_S16, 1 );
397+ nullptr , pAudioCodecCtx->ch_layout . nb_channels , audioFrame->nb_samples , AV_SAMPLE_FMT_S16, 1 );
391398 memcpy (pDest, audioFrame->data [0 ], sizeToDecode);
392399 }
393400 }
@@ -414,23 +421,23 @@ int FFmpegDecoder::DecodeAudio(int nStreamIndex, const AVPacket *avpkt, uint8_t
414421
415422void FFmpegDecoder::writeAudioBuff (uint8_t *aSample, size_t aSize) {
416423 lock_guard<mutex> lck (abBuffMtx);
417- if (av_fifo_space (audioFifoBuffer.get ()) < aSize) {
424+ if (av_fifo_can_write (audioFifoBuffer.get ()) < aSize) {
418425 std::vector<uint8_t > tmp;
419426 tmp.resize (aSize);
420- av_fifo_generic_read (audioFifoBuffer.get (), tmp.data (), aSize, nullptr );
427+ av_fifo_read (audioFifoBuffer.get (), tmp.data (), aSize);
421428 }
422- av_fifo_generic_write (audioFifoBuffer.get (), aSample, aSize, nullptr );
429+ av_fifo_write (audioFifoBuffer.get (), aSample, aSize);
423430}
424431
425432size_t FFmpegDecoder::ReadAudioBuff (uint8_t *aSample, size_t aSize) {
426433 lock_guard<mutex> lck (abBuffMtx);
427- if (av_fifo_size (audioFifoBuffer.get ()) < aSize) {
434+ if (av_fifo_elem_size (audioFifoBuffer.get ()) < aSize) {
428435 return 0 ;
429436 }
430- av_fifo_generic_read (audioFifoBuffer.get (), aSample, aSize, nullptr );
437+ av_fifo_read (audioFifoBuffer.get (), aSample, aSize);
431438 return aSize;
432439}
433440void FFmpegDecoder::ClearAudioBuff () {
434441 lock_guard<mutex> lck (abBuffMtx);
435- av_fifo_reset (audioFifoBuffer.get ());
442+ av_fifo_reset2 (audioFifoBuffer.get ());
436443}
0 commit comments