From fb54fd0a6918b3ced1708c4858b662ed6b7ca6c3 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Wed, 4 Mar 2009 11:05:55 +0000 Subject: [PATCH] Added support for using libswscale --- CMakeModules/FindFFmpeg.cmake | 1 + src/osgPlugins/ffmpeg/CMakeLists.txt | 39 +++++++++++---- src/osgPlugins/ffmpeg/FFmpegDecoderVideo.cpp | 34 +++++++++++-- src/osgPlugins/ffmpeg/FFmpegDecoderVideo.hpp | 50 ++++++++++++-------- src/osgPlugins/ffmpeg/FFmpegHeaders.hpp | 5 ++ src/osgPlugins/ffmpeg/ReaderWriterFFmpeg.cpp | 4 +- 6 files changed, 99 insertions(+), 34 deletions(-) diff --git a/CMakeModules/FindFFmpeg.cmake b/CMakeModules/FindFFmpeg.cmake index 3cc91d8b5..266b59c55 100644 --- a/CMakeModules/FindFFmpeg.cmake +++ b/CMakeModules/FindFFmpeg.cmake @@ -20,6 +20,7 @@ IF(PKG_CONFIG_FOUND) pkg_check_modules(FFMPEG_LIBAVDEVICE libavdevice) pkg_check_modules(FFMPEG_LIBAVCODEC libavcodec) pkg_check_modules(FFMPEG_LIBAVUTIL libavutil) + pkg_check_modules(FFMPEG_LIBSWSCALE libswscale) ENDIF(PKG_CONFIG_FOUND) diff --git a/src/osgPlugins/ffmpeg/CMakeLists.txt b/src/osgPlugins/ffmpeg/CMakeLists.txt index 9d6fedd9a..577b5afdf 100644 --- a/src/osgPlugins/ffmpeg/CMakeLists.txt +++ b/src/osgPlugins/ffmpeg/CMakeLists.txt @@ -1,11 +1,35 @@ # INCLUDE_DIRECTORIES( ${FFMPEG_INCLUDE_DIRS} ) -INCLUDE_DIRECTORIES( - ${FFMPEG_LIBAVFORMAT_INCLUDE_DIRS} ${FFMPEG_LIBAVFORMAT_INCLUDE_DIRS}/libavformat ${FFMPEG_LIBAVFORMAT_INCLUDE_DIRS}/ffmpeg - ${FFMPEG_LIBAVDEVICE_INCLUDE_DIRS} ${FFMPEG_LIBAVDEVICE_INCLUDE_DIRS}/libavdevice ${FFMPEG_LIBAVDEVICE_INCLUDE_DIRS}/ffmpeg - ${FFMPEG_LIBAVCODEC_INCLUDE_DIRS} ${FFMPEG_LIBAVCODEC_INCLUDE_DIRS}/libavcodec ${FFMPEG_LIBAVCODEC_INCLUDE_DIRS}/ffmpeg - ${FFMPEG_LIBAVUTIL_INCLUDE_DIRS} ${FFMPEG_LIBAVUTIL_INCLUDE_DIRS}/libavcodec ${FFMPEG_LIBAVUTIL_INCLUDE_DIRS}/ffmpeg -) +IF (FFMPEG_LIBSWSCALE_FOUND) + + INCLUDE_DIRECTORIES( + ${FFMPEG_LIBAVFORMAT_INCLUDE_DIRS} ${FFMPEG_LIBAVFORMAT_INCLUDE_DIRS}/libavformat ${FFMPEG_LIBAVFORMAT_INCLUDE_DIRS}/ffmpeg + ${FFMPEG_LIBAVDEVICE_INCLUDE_DIRS} ${FFMPEG_LIBAVDEVICE_INCLUDE_DIRS}/libavdevice ${FFMPEG_LIBAVDEVICE_INCLUDE_DIRS}/ffmpeg + ${FFMPEG_LIBAVCODEC_INCLUDE_DIRS} ${FFMPEG_LIBAVCODEC_INCLUDE_DIRS}/libavcodec ${FFMPEG_LIBAVCODEC_INCLUDE_DIRS}/ffmpeg + ${FFMPEG_LIBAVUTIL_INCLUDE_DIRS} ${FFMPEG_LIBAVUTIL_INCLUDE_DIRS}/libavcodec ${FFMPEG_LIBAVUTIL_INCLUDE_DIRS}/ffmpeg + ${FFMPEG_LIBSWSCALE_INCLUDE_DIRS} ${FFMPEG_LIBSWSCALE_INCLUDE_DIRS}/libswscale ${FFMPEG_LIBSWSCALE_INCLUDE_DIRS}/ffmpeg + ) + + ADD_DEFINITIONS(-DUSE_SWSCALE) + + LINK_DIRECTORIES(${FFMPEG_LIBRARY_DIRS}) + + SET(TARGET_EXTERNAL_LIBRARIES ${FFMPEG_LIBRARIES} ${FFMPEG_LIBSWSCALE_LIBRARIES}) + +ELSE(FFMPEG_LIBSWSCALE_FOUND) + + INCLUDE_DIRECTORIES( + ${FFMPEG_LIBAVFORMAT_INCLUDE_DIRS} ${FFMPEG_LIBAVFORMAT_INCLUDE_DIRS}/libavformat ${FFMPEG_LIBAVFORMAT_INCLUDE_DIRS}/ffmpeg + ${FFMPEG_LIBAVDEVICE_INCLUDE_DIRS} ${FFMPEG_LIBAVDEVICE_INCLUDE_DIRS}/libavdevice ${FFMPEG_LIBAVDEVICE_INCLUDE_DIRS}/ffmpeg + ${FFMPEG_LIBAVCODEC_INCLUDE_DIRS} ${FFMPEG_LIBAVCODEC_INCLUDE_DIRS}/libavcodec ${FFMPEG_LIBAVCODEC_INCLUDE_DIRS}/ffmpeg + ${FFMPEG_LIBAVUTIL_INCLUDE_DIRS} ${FFMPEG_LIBAVUTIL_INCLUDE_DIRS}/libavcodec ${FFMPEG_LIBAVUTIL_INCLUDE_DIRS}/ffmpeg + ) + + LINK_DIRECTORIES(${FFMPEG_LIBRARY_DIRS}) + + SET(TARGET_EXTERNAL_LIBRARIES ${FFMPEG_LIBRARIES} ) + +ENDIF() # MESSAGE("FFMPEG_LIBAVFORMAT_INCLUDE_DIRS = " ${FFMPEG_LIBAVFORMAT_INCLUDE_DIRS} ) # MESSAGE("FFMPEG_LIBAVDEVICE_INCLUDE_DIRS = " ${FFMPEG_LIBAVDEVICE_INCLUDE_DIRS} ) @@ -36,9 +60,6 @@ SET(TARGET_H MessageQueue.hpp ) -LINK_DIRECTORIES(${FFMPEG_LIBRARY_DIRS}) - -SET(TARGET_EXTERNAL_LIBRARIES ${FFMPEG_LIBRARIES} ) #### end var setup ### diff --git a/src/osgPlugins/ffmpeg/FFmpegDecoderVideo.cpp b/src/osgPlugins/ffmpeg/FFmpegDecoderVideo.cpp index 2f4c0efaf..9fef131ce 100644 --- a/src/osgPlugins/ffmpeg/FFmpegDecoderVideo.cpp +++ b/src/osgPlugins/ffmpeg/FFmpegDecoderVideo.cpp @@ -5,12 +5,14 @@ #include #include +#if 0 extern "C" { int img_convert(AVPicture *dst, int dst_pix_fmt, const AVPicture *src, int src_pix_fmt, int src_width, int src_height); }; +#endif namespace osgFFmpeg { @@ -27,7 +29,8 @@ FFmpegDecoderVideo::FFmpegDecoderVideo(PacketQueue & packets, FFmpegClocks & clo m_packet_pts(AV_NOPTS_VALUE), m_user_data(0), m_publish_func(0), - m_exit(false) + m_exit(false), + m_swscale_ctx(0) { } @@ -41,6 +44,12 @@ FFmpegDecoderVideo::~FFmpegDecoderVideo() m_exit = true; join(); } + + if (m_swscale_ctx) + { + sws_freeContext(m_swscale_ctx); + m_swscale_ctx = 0; + } } @@ -210,6 +219,25 @@ void FFmpegDecoderVideo::findAspectRatio() m_aspect_ratio = ratio; } +int FFmpegDecoderVideo::convert(AVPicture *dst, int dst_pix_fmt, const AVPicture *src, + int src_pix_fmt, int src_width, int src_height) +{ +#ifdef USE_SWSCALE + if (m_swscale_ctx==0) + { + m_swscale_ctx = sws_getContext(src_width, src_height, src_pix_fmt, + src_width, src_height, dst_pix_fmt, + SWS_BILINEAR, NULL, NULL, NULL); + } + + return sws_scale(m_swscale_ctx, + src->data, src->linesize, 0, src_height, + dst->data, dst->linesize); +#else + return convert(dst, dst_pix_fmt, src, + src_pix_fmt, src_width, src_height) +#endif +} void FFmpegDecoderVideo::publishFrame(const double delay) @@ -230,7 +258,7 @@ void FFmpegDecoderVideo::publishFrame(const double delay) if (m_context->pix_fmt == PIX_FMT_YUVA420P) yuva420pToRgba(dst, src, width(), height()); else - img_convert(dst, PIX_FMT_RGB32, src, m_context->pix_fmt, width(), height()); + convert(dst, PIX_FMT_RGB32, src, m_context->pix_fmt, width(), height()); // Flip and swap buffer swapBuffers(); @@ -266,7 +294,7 @@ void FFmpegDecoderVideo::swapBuffers() void FFmpegDecoderVideo::yuva420pToRgba(AVPicture * const dst, const AVPicture * const src, int width, int height) { - img_convert(dst, PIX_FMT_RGB32, src, m_context->pix_fmt, width, height); + convert(dst, PIX_FMT_RGB32, src, m_context->pix_fmt, width, height); const size_t bpp = 4; diff --git a/src/osgPlugins/ffmpeg/FFmpegDecoderVideo.hpp b/src/osgPlugins/ffmpeg/FFmpegDecoderVideo.hpp index 33fcafad7..716588aed 100644 --- a/src/osgPlugins/ffmpeg/FFmpegDecoderVideo.hpp +++ b/src/osgPlugins/ffmpeg/FFmpegDecoderVideo.hpp @@ -89,34 +89,42 @@ private: double synchronizeVideo(double pts); void yuva420pToRgba(AVPicture *dst, const AVPicture *src, int width, int height); + int convert(AVPicture *dst, int dst_pix_fmt, const AVPicture *src, + int src_pix_fmt, int src_width, int src_height); + + static int getBuffer(AVCodecContext * context, AVFrame * picture); static void releaseBuffer(AVCodecContext * context, AVFrame * picture); - PacketQueue & m_packets; - FFmpegClocks & m_clocks; - AVStream * m_stream; - AVCodecContext * m_context; - AVCodec * m_codec; - const uint8_t * m_packet_data; - int m_bytes_remaining; - int64_t m_packet_pts; + PacketQueue & m_packets; + FFmpegClocks & m_clocks; + AVStream * m_stream; + AVCodecContext * m_context; + AVCodec * m_codec; + const uint8_t * m_packet_data; + int m_bytes_remaining; + int64_t m_packet_pts; - FramePtr m_frame; - FramePtr m_frame_rgba; - Buffer m_buffer_rgba; - Buffer m_buffer_rgba_public; + FramePtr m_frame; + FramePtr m_frame_rgba; + Buffer m_buffer_rgba; + Buffer m_buffer_rgba_public; - void * m_user_data; - PublishFunc m_publish_func; + void * m_user_data; + PublishFunc m_publish_func; - double m_frame_rate; - double m_aspect_ratio; - int m_width; - int m_height; - size_t m_next_frame_index; - bool m_alpha_channel; + double m_frame_rate; + double m_aspect_ratio; + int m_width; + int m_height; + size_t m_next_frame_index; + bool m_alpha_channel; - volatile bool m_exit; + volatile bool m_exit; + +#if USE_SWSCALE + struct SwsContext * m_swscale_ctx; +#endif }; diff --git a/src/osgPlugins/ffmpeg/FFmpegHeaders.hpp b/src/osgPlugins/ffmpeg/FFmpegHeaders.hpp index 412eda91a..132e6c728 100644 --- a/src/osgPlugins/ffmpeg/FFmpegHeaders.hpp +++ b/src/osgPlugins/ffmpeg/FFmpegHeaders.hpp @@ -9,6 +9,11 @@ extern "C" #include #include #include + +#ifdef USE_SWSCALE + #include +#endif + } diff --git a/src/osgPlugins/ffmpeg/ReaderWriterFFmpeg.cpp b/src/osgPlugins/ffmpeg/ReaderWriterFFmpeg.cpp index 5ca960875..794cb16d2 100644 --- a/src/osgPlugins/ffmpeg/ReaderWriterFFmpeg.cpp +++ b/src/osgPlugins/ffmpeg/ReaderWriterFFmpeg.cpp @@ -45,7 +45,9 @@ public: if (! acceptsExtension(ext)) return ReadResult::FILE_NOT_HANDLED; - const std::string path = osgDB::findDataFile(filename, options); + const std::string path = osgDB::containsServerAddress(filename) ? + filename : + osgDB::findDataFile(filename, options); if (path.empty()) return ReadResult::FILE_NOT_FOUND;