From 26a6468ced382ec460bb42860c7120aab990c7e7 Mon Sep 17 00:00:00 2001 From: WYY <1062587448@qq.com> Date: 星期三, 26 二月 2025 14:57:00 +0800 Subject: [PATCH] Merge branch 'master' of ssh://115.28.86.8:29418/~admin/FaceX_AI_智能守卫 --- Client/董澎韬/code/qffmpeg.cpp | 96 +++++++++++++++++++++++++++++++++++++----------- 1 files changed, 74 insertions(+), 22 deletions(-) diff --git "a/Client/\350\221\243\346\276\216\351\237\254/code/qffmpeg.cpp" "b/Client/\350\221\243\346\276\216\351\237\254/code/qffmpeg.cpp" index 756314e..851792c 100644 --- "a/Client/\350\221\243\346\276\216\351\237\254/code/qffmpeg.cpp" +++ "b/Client/\350\221\243\346\276\216\351\237\254/code/qffmpeg.cpp" @@ -24,7 +24,6 @@ if (pSwsContext) { sws_freeContext(pSwsContext); } - avpicture_free(&pAVPicture); } bool QFFmpeg::Init() @@ -46,9 +45,6 @@ //鑾峰彇瑙嗛娴佺储寮� videoStreamIndex = -1; -// qDebug()<<"nb:"<<pAVFormatContext->nb_streams; -// qDebug()<<"type:"<<pAVFormatContext->streams[0]->codec->codec_type; -// qDebug()<<"AVMEDIA_TYPE_VIDEO:"<<AVMEDIA_TYPE_VIDEO; for (uint i = 0; i < pAVFormatContext->nb_streams; i++) { if (pAVFormatContext->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO) { videoStreamIndex = i; @@ -67,8 +63,6 @@ videoWidth=pAVCodecContext->width; videoHeight=pAVCodecContext->height; - avpicture_alloc(&pAVPicture,AV_PIX_FMT_RGB24,videoWidth,videoHeight); - AVCodec *pAVCodec; //鑾峰彇瑙嗛娴佽В鐮佸櫒 @@ -79,7 +73,6 @@ result=avcodec_open2(pAVCodecContext,pAVCodec,NULL); if (result<0){ qDebug()<<"鎵撳紑瑙g爜鍣ㄥけ璐�"; - avpicture_free(&pAVPicture); sws_freeContext(pSwsContext); avformat_close_input(&pAVFormatContext); return false; @@ -91,23 +84,82 @@ void QFFmpeg::Play() { - //涓�甯т竴甯ц鍙栬棰� - int frameFinished = 0; - while (av_read_frame(pAVFormatContext, &pAVPacket) >= 0) { - if (pAVPacket.stream_index == videoStreamIndex) { - qDebug() << "寮�濮嬭В鐮�" << QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss"); - avcodec_decode_video2(pAVCodecContext, pAVFrame, &frameFinished, &pAVPacket); - if (frameFinished) { - mutex.lock(); - sws_scale(pSwsContext, (const uint8_t* const *)pAVFrame->data, pAVFrame->linesize, 0, videoHeight, pAVPicture.data, pAVPicture.linesize); - QImage image(pAVPicture.data[0], videoWidth, videoHeight, QImage::Format_RGB888); - QImage copyImage = image.copy(); // 娣辨嫹璐� - emit GetImage(copyImage, this->index); - qDebug() << "瑙g爜鎴愬姛锛屽彂閫佸浘鍍忎俊鍙�"; - mutex.unlock(); + AVPacket packet; + int frameFinished; + int videoStreamIndex = -1; + + // 鎵撳紑瑙嗛鏂囦欢 + if (avformat_open_input(&pAVFormatContext, url.toStdString().c_str(), NULL, NULL) != 0) + return; + + // 鑾峰彇娴佷俊鎭� + avformat_find_stream_info(pAVFormatContext, NULL); + + // 鎵惧埌瑙嗛娴佺储寮� + for (uint i = 0; i < pAVFormatContext->nb_streams; i++) { + if (pAVFormatContext->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO) { + videoStreamIndex = i; + break; + } + } + + // 鑾峰彇瑙g爜鍣� + AVCodec *codec = avcodec_find_decoder(pAVFormatContext->streams[videoStreamIndex]->codec->codec_id); + if (!codec) { + qDebug() << "鏈壘鍒拌В鐮佸櫒"; + return; + } + + if (avcodec_open2(pAVCodecContext, codec, NULL) < 0) { + qDebug() << "鎵撳紑瑙g爜鍣ㄥけ璐�"; + return; + } + + // 鍒濆鍖栧抚 + pAVFrame = av_frame_alloc(); + AVFrame *pFrameRGB = av_frame_alloc(); + uint8_t *buffer = (uint8_t *)av_malloc(avpicture_get_size(AV_PIX_FMT_RGB24, pAVCodecContext->width, pAVCodecContext->height)); + avpicture_fill((AVPicture *)pFrameRGB, buffer, AV_PIX_FMT_RGB24, pAVCodecContext->width, pAVCodecContext->height); + + // 鍒濆鍖栫缉鏀句笂涓嬫枃 + struct SwsContext *sws_ctx = sws_getContext(pAVCodecContext->width, + pAVCodecContext->height, + pAVCodecContext->pix_fmt, + pAVCodecContext->width, + pAVCodecContext->height, + AV_PIX_FMT_RGB24, + SWS_BILINEAR, + NULL, + NULL, + NULL); + + while (av_read_frame(pAVFormatContext, &packet) >= 0) { + if (packet.stream_index == videoStreamIndex) { + avcodec_send_packet(pAVCodecContext, &packet); + + while (avcodec_receive_frame(pAVCodecContext, pAVFrame) == 0) { + sws_scale(sws_ctx, + (const uint8_t **)pAVFrame->data, + pAVFrame->linesize, + 0, + pAVCodecContext->height, + pFrameRGB->data, + pFrameRGB->linesize); + + QImage img(pFrameRGB->data[0], + pAVCodecContext->width, + pAVCodecContext->height, + QImage::Format_RGB888); + + emit GetImage(img, 0); } } - av_packet_unref(&pAVPacket); + av_packet_unref(&packet); } + avformat_close_input(&pAVFormatContext); + av_frame_free(&pAVFrame); + av_frame_free(&pFrameRGB); + av_free(buffer); + sws_freeContext(sws_ctx); } -- Gitblit v1.8.0