File indexing completed on 2025-04-27 03:58:10

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2016-04-21
0007  * Description : video thumbnails extraction based on ffmpeg
0008  *
0009  * SPDX-FileCopyrightText: 2010      by Dirk Vanden Boer <dirk dot vdb at gmail dot com>
0010  * SPDX-FileCopyrightText: 2016-2018 by Maik Qualmann <metzpinguin at gmail dot com>
0011  * SPDX-FileCopyrightText: 2016-2024 by Gilles Caulier <caulier dot gilles at gmail dot com>
0012  *
0013  * SPDX-License-Identifier: GPL-2.0-or-later
0014  *
0015  * ============================================================ */
0016 
0017 #ifndef DIGIKAM_VIDEO_THUMB_DECODER_PRIVATE_H
0018 #define DIGIKAM_VIDEO_THUMB_DECODER_PRIVATE_H
0019 
0020 #include "videothumbdecoder.h"
0021 
0022 // FFMpeg includes
0023 
0024 extern "C"
0025 {
0026 #include <libswscale/swscale.h>
0027 #include <libavcodec/avcodec.h>
0028 #include <libavutil/imgutils.h>
0029 #include <libavformat/avformat.h>
0030 #include <libavfilter/avfilter.h>
0031 #include <libavfilter/buffersrc.h>
0032 #include <libavfilter/buffersink.h>
0033 }
0034 
0035 namespace Digikam
0036 {
0037 
0038 class Q_DECL_HIDDEN VideoThumbDecoder::Private
0039 {
0040 public:
0041 
0042     explicit Private();
0043     ~Private();
0044 
0045 public:
0046 
0047     int                videoStream;
0048     AVFormatContext*   pFormatContext;
0049     AVCodecContext*    pVideoCodecContext;
0050     AVCodecParameters* pVideoCodecParameters;
0051 
0052 #ifndef HAVE_FFMPEG_VERSION5
0053 
0054     AVCodec*           pVideoCodec;
0055 
0056 #else // ffmpeg >= 5
0057 
0058     const AVCodec*     pVideoCodec;
0059 
0060 #endif
0061 
0062     AVStream*          pVideoStream;
0063     AVFrame*           pFrame;
0064     quint8*            pFrameBuffer;
0065     AVPacket*          pPacket;
0066     bool               allowSeek;
0067     bool               initialized;
0068     AVFilterContext*   bufferSinkContext;
0069     AVFilterContext*   bufferSourceContext;
0070     AVFilterGraph*     filterGraph;
0071     AVFrame*           filterFrame;
0072     int                lastWidth;
0073     int                lastHeight;
0074     enum AVPixelFormat lastPixfmt;
0075 
0076 public:
0077 
0078     bool initializeVideo();
0079     bool getVideoPacket();
0080     bool decodeVideoPacket() const;
0081 
0082     void convertAndScaleFrame(AVPixelFormat format,
0083                               int scaledSize,
0084                               bool maintainAspectRatio,
0085                               int& scaledWidth,
0086                               int& scaledHeight);
0087 
0088     bool processFilterGraph(AVFrame* const dst,
0089                             const AVFrame* const src,
0090                             enum AVPixelFormat pixfmt,
0091                             int width,
0092                             int height);
0093 
0094     void deleteFilterGraph();
0095 
0096 private:
0097 
0098     bool initFilterGraph(enum AVPixelFormat pixfmt, int width, int height);
0099 
0100     void calculateDimensions(int squareSize,
0101                              bool maintainAspectRatio,
0102                              int& destWidth,
0103                              int& destHeight);
0104 
0105     void createAVFrame(AVFrame** const avFrame,
0106                        quint8** const frameBuffer,
0107                        int width,
0108                        int height,
0109                        AVPixelFormat format);
0110 
0111     // cppcheck-suppress unusedPrivateFunction
0112     int  decodeVideoNew(AVCodecContext* const avContext,
0113                         AVFrame* const avFrame,
0114                         int* gotFrame,
0115                         AVPacket* const avPacket) const;
0116 };
0117 
0118 } // namespace Digikam
0119 
0120 #endif // DIGIKAM_VIDEO_THUMB_DECODER_PRIVATE_H