From 4a57e35ca794dcc5ac0db66fbdde2ce585a7aa8d Mon Sep 17 00:00:00 2001 From: Laentir Valetov Date: Mon, 16 Sep 2019 21:46:20 +0400 Subject: [PATCH] Support DELPHI-FFMPEG in components Signed-off-by: Laentir Valetov --- README.md | 9 +- .../cCameraCapture/cCameraCapture.dproj | 2 +- .../cFFmpegIPCamSource.dproj | 275 +++++++++++++++++- source/Delphi-FFMPEG | 2 +- source/component/ocv.comp.FFMSource.pas | 45 +-- 5 files changed, 304 insertions(+), 29 deletions(-) diff --git a/README.md b/README.md index 047640c..96c2136 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ or from the repository: (1) 32-bit in the "Delphi-OpenCV\redist\VC14\x86\" (2) 64-bit in the "Delphi-OpenCV\redist\VC14\x64\" ``` -* Shared library FFMPEG 4.0.2 for Windows can be downloaded from [here][5]
+* Shared library FFMPEG 4.2 for Windows can be downloaded from [here][5]
``` (3) FFmpeg 32-bit Shared (4) FFmpeg 64-bit Shared @@ -55,13 +55,13 @@ Unzip it to a convenient directory, thus get the following directory structure ``` -Download the [FFmpeg Delphi/Pascal Headers 4.0.2][6] and extract to \source\ffmpeg
+Download the [FFmpeg Delphi/Pascal Headers 4.0.2][6] and extract to \source\ffmpeg or use [DELPHI-FFMPEG][7] (as submodule)
Add the search path for the modules of the project in Delphi IDE (Tools-Options-Delphi Options-Library-Library path) ``` \source \source\classes \source\component -\source\ffmpeg\headers +\source\ffmpeg\headers or \source\Delphi-FFMPEG\source \source\opengl \source\sdl \source\sdl2 @@ -130,4 +130,5 @@ Examples of use of components [3]: https://www.libsdl.org/index.php [4]: https://github.com/opencv/opencv/releases/tag/2.4.13.3 [5]: http://ffmpeg.zeranoe.com/builds/ -[6]: http://www.delphiffmpeg.com/headers/ \ No newline at end of file +[6]: http://www.delphiffmpeg.com/headers/ +[7]: https://github.com/Laex/Delphi-FFMPEG \ No newline at end of file diff --git a/samples/Components/cCameraCapture/cCameraCapture.dproj b/samples/Components/cCameraCapture/cCameraCapture.dproj index 6aecabb..6ec1cb7 100644 --- a/samples/Components/cCameraCapture/cCameraCapture.dproj +++ b/samples/Components/cCameraCapture/cCameraCapture.dproj @@ -1,7 +1,7 @@  {9F27EBDE-3B7A-4B88-8E68-90DCA8A35F81} - 18.6 + 18.7 VCL cCameraCapture.dpr True diff --git a/samples/Components/cFFmpegIPCamSource/cFFmpegIPCamSource.dproj b/samples/Components/cFFmpegIPCamSource/cFFmpegIPCamSource.dproj index acc9f41..f0510f0 100644 --- a/samples/Components/cFFmpegIPCamSource/cFFmpegIPCamSource.dproj +++ b/samples/Components/cFFmpegIPCamSource/cFFmpegIPCamSource.dproj @@ -1,7 +1,7 @@  {08BFF34B-6FF9-468C-9AB2-3F76BBC9E351} - 18.6 + 18.7 VCL cFFmpegIPCamSource.dpr True @@ -214,6 +214,12 @@ 1 + + + res\values + 1 + + res\drawable @@ -250,6 +256,36 @@ 1 + + + res\drawable-mdpi + 1 + + + + + res\drawable-hdpi + 1 + + + + + res\drawable-xhdpi + 1 + + + + + res\drawable-xxhdpi + 1 + + + + + res\drawable-xxxhdpi + 1 + + res\drawable-small @@ -274,6 +310,12 @@ 1 + + + res\values + 1 + + 1 @@ -393,6 +435,17 @@ 1 + + + 1 + + + 1 + + + 1 + + 1 @@ -404,6 +457,39 @@ 1 + + + 1 + + + 1 + + + 1 + + + + + 1 + + + 1 + + + 1 + + + + + 1 + + + 1 + + + 1 + + 1 @@ -415,6 +501,61 @@ 1 + + + 1 + + + 1 + + + 1 + + + + + 1 + + + 1 + + + 1 + + + + + 1 + + + 1 + + + 1 + + + + + 1 + + + 1 + + + 1 + + + + + 1 + + + 1 + + + 1 + + 1 @@ -426,6 +567,116 @@ 1 + + + 1 + + + 1 + + + 1 + + + + + 1 + + + 1 + + + 1 + + + + + 1 + + + 1 + + + 1 + + + + + 1 + + + 1 + + + 1 + + + + + 1 + + + 1 + + + 1 + + + + + 1 + + + 1 + + + 1 + + + + + 1 + + + 1 + + + 1 + + + + + 1 + + + 1 + + + 1 + + + + + 1 + + + 1 + + + 1 + + + + + 1 + + + 1 + + + 1 + + 1 @@ -459,6 +710,28 @@ 1 + + + 1 + + + 1 + + + 1 + + + + + 1 + + + 1 + + + 1 + + 1 diff --git a/source/Delphi-FFMPEG b/source/Delphi-FFMPEG index 0be92a0..ed23a68 160000 --- a/source/Delphi-FFMPEG +++ b/source/Delphi-FFMPEG @@ -1 +1 @@ -Subproject commit 0be92a08278ddc61978d326621038eebf4015ecb +Subproject commit ed23a68bf58b5b2f19ad60673ed79a9389dd8b06 diff --git a/source/component/ocv.comp.FFMSource.pas b/source/component/ocv.comp.FFMSource.pas index 76da246..2e5731c 100644 --- a/source/component/ocv.comp.FFMSource.pas +++ b/source/component/ocv.comp.FFMSource.pas @@ -44,7 +44,7 @@ Uses libavcodec; Type - TOnNotifyFFMpegPacket = procedure(Sender: TObject; const packet: TAVPacket; const isKeyFrame: Boolean) of object; + TOnNotifyFFMpegPacket = procedure(Sender: TObject; const packet: AVPacket; const isKeyFrame: Boolean) of object; TocvFFMpegIPCamEvent = (ffocvTryConnect, ffocvConnected, ffocvLostConnection, ffocvReconnect, ffocvErrorGetStream); TOnocvFFMpegIPCamEvent = procedure(Sender: TObject; const Event: TocvFFMpegIPCamEvent) of object; @@ -66,7 +66,7 @@ Type function GetIPCamTarget: AnsiString; procedure SetEnabled(Value: Boolean); override; procedure Loaded; override; - procedure DoNotifyPacket(const packet: TAVPacket; const isKeyFrame: Boolean); + procedure DoNotifyPacket(const packet: AVPacket; const isKeyFrame: Boolean); procedure DoNotifyEvent(Event: TocvFFMpegIPCamEvent); public constructor Create(AOwner: TComponent); override; @@ -89,11 +89,12 @@ Uses ocv.core.types_c, ocv.comp.Types, libavformat, - libavutil_dict, +// libavutil_dict, libavutil, - libavutil_frame, - libswscale, - libavutil_pixfmt; +// libavutil_frame, + libswscale +// ,libavutil_pixfmt + ; Type TocvFFMpegIPCamSourceThread = class(TocvCustomSourceThread) @@ -139,7 +140,7 @@ begin OnIPCamEvent(Self, Event); end; -procedure TocvFFMpegIPCamSource.DoNotifyPacket(const packet: TAVPacket; const isKeyFrame: Boolean); +procedure TocvFFMpegIPCamSource.DoNotifyPacket(const packet: AVPacket; const isKeyFrame: Boolean); begin if Assigned(OnFFMpegPacket) then OnFFMpegPacket(Self, packet, isKeyFrame); @@ -232,7 +233,7 @@ Var pFormatCtx: pAVFormatContext; pCodecCtx: pAVCodecContext; pCodec: pAVCodec; - packet: TAVPacket; + packet: AVPacket; img_convert_context: pSwsContext; frame: pAVFrame; iplframe: pIplImage; @@ -246,7 +247,7 @@ Var end; if Assigned(pFormatCtx) then begin - avformat_close_input(@pFormatCtx); + avformat_close_input(pFormatCtx); pFormatCtx := nil; end; if Assigned(iplframe) then @@ -256,12 +257,12 @@ Var end; if Assigned(frame) then begin - av_frame_free(@frame); + av_frame_free(frame); frame := nil; end; if Assigned(optionsDict) then begin - av_dict_free(@optionsDict); + av_dict_free(optionsDict); optionsDict := nil; end; end; @@ -298,14 +299,14 @@ begin DoNotyfy(ffocvTryConnect); - av_dict_set(@optionsDict, 'rtsp_transport', 'tcp', 0); - av_dict_set(@optionsDict, 'rtsp_flags', 'prefer_tcp', 0); - av_dict_set(@optionsDict, 'allowed_media_types', 'video', 0); - av_dict_set(@optionsDict, 'reorder_queue_size', '10', 0); - av_dict_set(@optionsDict, 'max_delay', '500000', 0); - av_dict_set(@optionsDict, 'stimeout', '1000000', 0); + av_dict_set(optionsDict, 'rtsp_transport', 'tcp', 0); + av_dict_set(optionsDict, 'rtsp_flags', 'prefer_tcp', 0); + av_dict_set(optionsDict, 'allowed_media_types', 'video', 0); + av_dict_set(optionsDict, 'reorder_queue_size', '10', 0); + av_dict_set(optionsDict, 'max_delay', '500000', 0); + av_dict_set(optionsDict, 'stimeout', '1000000', 0); - ret := avformat_open_input(@pFormatCtx, PAnsiChar(FIPCamURL), nil, @optionsDict); // pFormatCtx + ret := avformat_open_input(pFormatCtx, PAnsiChar(FIPCamURL), nil, @optionsDict); // pFormatCtx if ret < 0 then begin DoNotyfy(ffocvErrorGetStream); @@ -313,7 +314,7 @@ begin Continue; end; - av_dict_free(@optionsDict); + av_dict_free(optionsDict); optionsDict := nil; if avformat_find_stream_info(pFormatCtx, nil) < 0 then begin @@ -368,8 +369,8 @@ begin Continue; end; - img_convert_context := sws_getCachedContext(nil, pCodecCtx^.Width, pCodecCtx^.Height, Integer(pCodecCtx^.pix_fmt), - pCodecCtx^.Width, pCodecCtx^.Height, Integer(AV_PIX_FMT_BGR24), SWS_BILINEAR, nil, nil, nil); + img_convert_context := sws_getCachedContext(nil, pCodecCtx^.Width, pCodecCtx^.Height, pCodecCtx^.pix_fmt, + pCodecCtx^.Width, pCodecCtx^.Height, AV_PIX_FMT_BGR24, SWS_BILINEAR, nil, nil, nil); if (img_convert_context = nil) then begin DoNotyfy(ffocvErrorGetStream); @@ -391,7 +392,7 @@ begin begin FOwner.DoNotifyPacket(packet, (packet.flags and AV_PKT_FLAG_KEY) <> 0); // Video stream packet - avcodec_decode_video2(pCodecCtx, frame, @frame_finished, @packet); + avcodec_decode_video2(pCodecCtx, frame, frame_finished, @packet); if (frame_finished <> 0) then begin sws_scale(img_convert_context, @frame^.data, @frame^.linesize, 0, pCodecCtx^.Height, @iplframe^.imageData,