From 08d85387ef30e9e12edc6c216a5dd3160f3ed0bb Mon Sep 17 00:00:00 2001 From: wanggaoMagicsky <80475837@qq.com> Date: Wed, 10 Apr 2024 14:36:34 +0800 Subject: [PATCH] add support for FFMPEG 5.x --- .../liveview/dji_camera_stream_decoder.cpp | 32 ++++++++----------- .../liveview/dji_camera_stream_decoder.hpp | 10 ------ .../platform/linux/manifold2/CMakeLists.txt | 14 -------- 3 files changed, 14 insertions(+), 42 deletions(-) diff --git a/samples/sample_c++/module_sample/liveview/dji_camera_stream_decoder.cpp b/samples/sample_c++/module_sample/liveview/dji_camera_stream_decoder.cpp index 87e894eb..3dc3480a 100644 --- a/samples/sample_c++/module_sample/liveview/dji_camera_stream_decoder.cpp +++ b/samples/sample_c++/module_sample/liveview/dji_camera_stream_decoder.cpp @@ -50,7 +50,6 @@ DJICameraStreamDecoder::DJICameraStreamDecoder() pSwsCtx(nullptr), pFrameYUV(nullptr), pFrameRGB(nullptr), - rgbBuf(nullptr), #endif bufSize(0) { @@ -78,14 +77,15 @@ bool DJICameraStreamDecoder::init() } #ifdef FFMPEG_INSTALLED - avcodec_register_all(); + // avcodec_register_all(); pCodecCtx = avcodec_alloc_context3(nullptr); if (!pCodecCtx) { return false; } pCodecCtx->thread_count = 4; - pCodec = avcodec_find_decoder(AV_CODEC_ID_H264); + // pCodec = avcodec_find_decoder(AV_CODEC_ID_H264); + pCodec = const_cast(avcodec_find_decoder(AV_CODEC_ID_H264)); if (!pCodec || avcodec_open2(pCodecCtx, pCodec, nullptr) < 0) { return false; } @@ -147,11 +147,6 @@ void DJICameraStreamDecoder::cleanup() pCodecCtx = nullptr; } - if (nullptr != rgbBuf) { - av_free(rgbBuf); - rgbBuf = nullptr; - } - if (nullptr != pFrameRGB) { av_free(pFrameRGB); pFrameRGB = nullptr; @@ -206,11 +201,10 @@ void DJICameraStreamDecoder::decodeBuffer(const uint8_t *buf, int bufLen) pData += processedLen; if (pkt.size > 0) { - int gotPicture = 0; - avcodec_decode_video2(pCodecCtx, pFrameYUV, &gotPicture, &pkt); + int ret = avcodec_send_packet(pCodecCtx, &pkt); + ret = avcodec_receive_frame(pCodecCtx, pFrameYUV); - if (!gotPicture) { - ////DSTATUS_PRIVATE("Got Frame, but no picture\n"); + if(0 != ret) { continue; } else { int w = pFrameYUV->width; @@ -223,13 +217,15 @@ void DJICameraStreamDecoder::decodeBuffer(const uint8_t *buf, int bufLen) 4, nullptr, nullptr, nullptr); } - if (nullptr == rgbBuf) { - bufSize = avpicture_get_size(AV_PIX_FMT_RGB24, w, h); - rgbBuf = (uint8_t *) av_malloc(bufSize); - avpicture_fill((AVPicture *) pFrameRGB, rgbBuf, AV_PIX_FMT_RGB24, w, h); + if( 0 == bufSize) { + bufSize = w * h * 3; + pFrameRGB->width = w; + pFrameRGB->height = h; + pFrameRGB->format = AV_PIX_FMT_RGB24; + av_frame_get_buffer(pFrameRGB, 1); } - if (nullptr != pSwsCtx && nullptr != rgbBuf) { + if (nullptr != pSwsCtx && 0 != bufSize) { sws_scale(pSwsCtx, (uint8_t const *const *) pFrameYUV->data, pFrameYUV->linesize, 0, pFrameYUV->height, pFrameRGB->data, pFrameRGB->linesize); @@ -243,7 +239,7 @@ void DJICameraStreamDecoder::decodeBuffer(const uint8_t *buf, int bufLen) } } pthread_mutex_unlock(&decodemutex); - av_free_packet(&pkt); + av_packet_unref(&pkt); #endif } diff --git a/samples/sample_c++/module_sample/liveview/dji_camera_stream_decoder.hpp b/samples/sample_c++/module_sample/liveview/dji_camera_stream_decoder.hpp index b3db589b..0c2aa03c 100644 --- a/samples/sample_c++/module_sample/liveview/dji_camera_stream_decoder.hpp +++ b/samples/sample_c++/module_sample/liveview/dji_camera_stream_decoder.hpp @@ -78,7 +78,6 @@ class DJICameraStreamDecoder { AVFrame *pFrameYUV; AVFrame *pFrameRGB; #endif - uint8_t *rgbBuf; size_t bufSize; }; @@ -90,12 +89,3 @@ class DJICameraStreamDecoder { #endif // DJI_CAMERA_STREAM_DECCODER_H /************************ (C) COPYRIGHT DJI Innovations *******END OF FILE******/ - - - - - - - - - diff --git a/samples/sample_c++/platform/linux/manifold2/CMakeLists.txt b/samples/sample_c++/platform/linux/manifold2/CMakeLists.txt index 5677abff..7095c730 100644 --- a/samples/sample_c++/platform/linux/manifold2/CMakeLists.txt +++ b/samples/sample_c++/platform/linux/manifold2/CMakeLists.txt @@ -81,20 +81,6 @@ if (FFMPEG_FOUND) message(STATUS " - Includes: ${FFMPEG_INCLUDE_DIR}") message(STATUS " - Libraries: ${FFMPEG_LIBRARIES}") - EXECUTE_PROCESS(COMMAND ffmpeg -version - OUTPUT_VARIABLE ffmpeg_version_output - OUTPUT_STRIP_TRAILING_WHITESPACE - ) - string(REGEX MATCH "version.*Copyright" ffmpeg_version_line ${ffmpeg_version_output}) - string(REGEX MATCH " .* " ffmpeg_version ${ffmpeg_version_line}) - string(REGEX MATCH "^ 5.*$" ffmpeg_major_version ${ffmpeg_version}) - - if (HEAD${ffmpeg_major_version} STREQUAL "HEAD") - message(STATUS " - Version: ${ffmpeg_version}") - else () - message(FATAL_ERROR " - Not support FFMPEG version: ${ffmpeg_major_version}, please install 4.x.x instead.") - endif () - target_link_libraries(${PROJECT_NAME} ${FFMPEG_LIBRARIES}) include_directories(${FFMPEG_INCLUDE_DIR}) add_definitions(-DFFMPEG_INSTALLED)