1 /******************************************************************************
2     QtAV:  Multimedia framework based on Qt and FFmpeg
3     Copyright (C) 2012-2017 Wang Bin <wbsecg1@gmail.com>
4 
5 *   This file is part of QtAV (from 2013)
6 
7     This library is free software; you can redistribute it and/or
8     modify it under the terms of the GNU Lesser General Public
9     License as published by the Free Software Foundation; either
10     version 2.1 of the License, or (at your option) any later version.
11 
12     This library is distributed in the hope that it will be useful,
13     but WITHOUT ANY WARRANTY; without even the implied warranty of
14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15     Lesser General Public License for more details.
16 
17     You should have received a copy of the GNU Lesser General Public
18     License along with this library; if not, write to the Free Software
19     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
20 ******************************************************************************/
21 
22 #ifndef QTAV_VIDEODECODERFFMPEGHW_P_H
23 #define QTAV_VIDEODECODERFFMPEGHW_P_H
24 
25 #include "VideoDecoderFFmpegHW.h"
26 #include "utils/GPUMemCopy.h"
27 
28 /*!
29    QTAV_HAVE(AVBUFREF): use AVCodecContext.get_buffer2 instead of old callbacks. In order to avoid compile warnings, now disable old
30    callbacks if possible. maybe we can also do a runtime check and enable old callbacks
31  */
32 
33 namespace QtAV {
34 
35 class VideoDecoderFFmpegHWPrivate : public VideoDecoderFFmpegBasePrivate
36 {
37 public:
VideoDecoderFFmpegHWPrivate()38     VideoDecoderFFmpegHWPrivate()
39         : VideoDecoderFFmpegBasePrivate()
40         , get_format(NULL)
41         , get_buffer(NULL)
42         , release_buffer(NULL)
43         , reget_buffer(NULL)
44         , get_buffer2(NULL)
45         , threads(0)
46         , copy_mode(VideoDecoderFFmpegHW::OptimizedCopy)
47         , hw_w(0)
48         , hw_h(0)
49         , hw_profile(0)
50     {}
~VideoDecoderFFmpegHWPrivate()51     virtual ~VideoDecoderFFmpegHWPrivate() {} //ctx is 0 now
enableFrameRef()52     bool enableFrameRef() const Q_DECL_OVERRIDE { return false;} //because of ffmpeg_get_va_buffer2?
53     bool prepare();
restore()54     void restore() {
55         codec_ctx->pix_fmt = pixfmt;
56         codec_ctx->opaque = 0;
57         codec_ctx->get_format = get_format;
58 #if QTAV_HAVE(AVBUFREF)
59         codec_ctx->get_buffer2 = get_buffer2;
60 #else
61         codec_ctx->get_buffer = get_buffer;
62         codec_ctx->release_buffer = release_buffer;
63         codec_ctx->reget_buffer = reget_buffer;
64 #endif //QTAV_HAVE(AVBUFREF)
65     }
66 
open()67     virtual bool open() Q_DECL_OVERRIDE { return prepare();}
close()68     virtual void close() Q_DECL_OVERRIDE {restore();}
69     // return hwaccel_context or null
70     virtual void* setup(AVCodecContext* avctx) = 0;
71 
72     AVPixelFormat getFormat(struct AVCodecContext *p_context, const AVPixelFormat *pi_fmt);
73     //TODO: remove opaque
74     virtual bool getBuffer(void **opaque, uint8_t **data) = 0;
75     virtual void releaseBuffer(void *opaque, uint8_t *data) = 0;
76     virtual AVPixelFormat vaPixelFormat() const = 0;
77 
78     int codedWidth(AVCodecContext *avctx) const;  //TODO: virtual int surfaceWidth(AVCodecContext*) const;
79     int codedHeight(AVCodecContext *avctx) const;
80     bool initUSWC(int lineSize);
81     void releaseUSWC();
82 
83     AVPixelFormat pixfmt; //store old one
84     //store old values because it does not own AVCodecContext
85     AVPixelFormat (*get_format)(struct AVCodecContext *s, const AVPixelFormat * fmt);
86     int (*get_buffer)(struct AVCodecContext *c, AVFrame *pic);
87     void (*release_buffer)(struct AVCodecContext *c, AVFrame *pic);
88     int (*reget_buffer)(struct AVCodecContext *c, AVFrame *pic);
89     int (*get_buffer2)(struct AVCodecContext *s, AVFrame *frame, int flags);
90 
91     QString description;
92     int threads; // multithread decoding may crash for some decoders (dxva, videotoolbox)
93     // false for not intel gpu. my test result is intel gpu is supper fast and lower cpu usage if use optimized uswc copy. but nv is worse.
94     // TODO: flag enable, disable, auto
95     VideoDecoderFFmpegHW::CopyMode copy_mode;
96     GPUMemCopy gpu_mem;
97 
98 private:
99     int hw_w, hw_h, hw_profile;
100 };
101 
102 } //namespace QtAV
103 
104 #endif // QTAV_VideoDecoderFFmpegHW_P_H
105