File indexing completed on 2025-01-19 04:22:52

0001 /*
0002     SPDX-FileCopyrightText: 2010 Dirk Vanden Boer <dirk.vdb@gmail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #ifndef MOVIEDECODER_H
0008 #define MOVIEDECODER_H
0009 
0010 #include "videoframe.h"
0011 #include <QString>
0012 
0013 extern "C" {
0014 #include <libavcodec/avcodec.h>
0015 #include <libavformat/avformat.h>
0016 #include <libavfilter/avfilter.h>
0017 #include <libavfilter/buffersrc.h>
0018 #include <libavfilter/buffersink.h>
0019 }
0020 
0021 namespace ffmpegthumbnailer
0022 {
0023 
0024 class MovieDecoder
0025 {
0026 public:
0027     explicit MovieDecoder(const QString& filename, AVFormatContext* pavContext = nullptr);
0028     ~MovieDecoder();
0029 
0030     QString getCodec();
0031     void seek(int timeInSeconds);
0032     bool decodeVideoFrame();
0033     void getScaledVideoFrame(int scaledSize, bool maintainAspectRatio, VideoFrame& videoFrame);
0034 
0035     int getWidth();
0036     int getHeight();
0037     int getDuration();
0038 
0039     void initialize(const QString& filename);
0040     void destroy();
0041     bool getInitialized();
0042 
0043 private:
0044     bool initializeVideo();
0045 
0046     bool decodeVideoPacket();
0047     bool getVideoPacket();
0048     void convertAndScaleFrame(AVPixelFormat format, int scaledSize, bool maintainAspectRatio, int& scaledWidth, int& scaledHeight);
0049     void createAVFrame(AVFrame** avFrame, quint8** frameBuffer, int width, int height, AVPixelFormat format);
0050     void calculateDimensions(int squareSize, bool maintainAspectRatio, int& destWidth, int& destHeight);
0051 
0052     void deleteFilterGraph();
0053     bool initFilterGraph(enum AVPixelFormat pixfmt, int width, int height);
0054     bool processFilterGraph(AVFrame *dst, const AVFrame *src, enum AVPixelFormat pixfmt, int width, int height);
0055 
0056 private:
0057     int                     m_VideoStream;
0058     AVFormatContext*        m_pFormatContext;
0059     AVCodecContext*         m_pVideoCodecContext;
0060 #if LIBAVCODEC_VERSION_MAJOR < 59
0061     AVCodec*                m_pVideoCodec;
0062 #else
0063     const AVCodec*          m_pVideoCodec;
0064 #endif
0065 //    AVStream*               m_pVideoStream;
0066     AVFrame*                m_pFrame;
0067     quint8*                 m_pFrameBuffer;
0068     AVPacket*               m_pPacket;
0069     bool                    m_FormatContextWasGiven;
0070     bool                    m_AllowSeek;
0071     bool                    m_initialized;
0072     AVFilterContext*        m_bufferSinkContext;
0073     AVFilterContext*        m_bufferSourceContext;
0074     AVFilterGraph*          m_filterGraph;
0075     AVFrame*                m_filterFrame;
0076     int                     m_lastWidth;
0077     int                     m_lastHeight;
0078     enum AVPixelFormat      m_lastPixfmt;
0079 };
0080 
0081 }
0082 
0083 #endif