26e240b7b7cc9fcd17a6e186bb6c2e718e192ec7
[platform/upstream/gstreamer.git] / sys / nvcodec / gstnvbaseenc.h
1 /* GStreamer NVENC plugin
2  * Copyright (C) 2015 Centricular Ltd
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19
20 #ifndef __GST_NV_BASE_ENC_H_INCLUDED__
21 #define __GST_NV_BASE_ENC_H_INCLUDED__
22
23 #include "gstnvenc.h"
24
25 #include <gst/video/gstvideoencoder.h>
26 #include "gstcudacontext.h"
27
28 #define GST_TYPE_NV_BASE_ENC \
29   (gst_nv_base_enc_get_type())
30 #define GST_NV_BASE_ENC(obj) \
31   (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_NV_BASE_ENC,GstNvBaseEnc))
32 #define GST_NV_BASE_ENC_CLASS(klass) \
33   (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_NV_BASE_ENC,GstNvBaseEncClass))
34 #define GST_NV_BASE_ENC_GET_CLASS(obj) \
35   (G_TYPE_INSTANCE_GET_CLASS((obj),GST_TYPE_NV_BASE_ENC,GstNvBaseEncClass))
36 #define GST_IS_NV_BASE_ENC(obj) \
37   (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_NV_BASE_ENC))
38 #define GST_IS_NV_BASE_ENC_CLASS(obj) \
39   (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_NV_BASE_ENC))
40
41 typedef enum {
42   GST_NV_PRESET_DEFAULT,
43   GST_NV_PRESET_HP,
44   GST_NV_PRESET_HQ,
45 /* FIXME: problematic GST_NV_PRESET_BD, */
46   GST_NV_PRESET_LOW_LATENCY_DEFAULT,
47   GST_NV_PRESET_LOW_LATENCY_HQ,
48   GST_NV_PRESET_LOW_LATENCY_HP,
49   GST_NV_PRESET_LOSSLESS_DEFAULT,
50   GST_NV_PRESET_LOSSLESS_HP,
51 } GstNvPreset;
52
53 typedef enum {
54   GST_NV_RC_MODE_DEFAULT,
55   GST_NV_RC_MODE_CONSTQP,
56   GST_NV_RC_MODE_CBR,
57   GST_NV_RC_MODE_VBR,
58   GST_NV_RC_MODE_VBR_MINQP,
59   GST_NV_RC_MODE_CBR_LOWDELAY_HQ,
60   GST_NV_RC_MODE_CBR_HQ,
61   GST_NV_RC_MODE_VBR_HQ,
62 } GstNvRCMode;
63
64 typedef enum
65 {
66   GST_NVENC_MEM_TYPE_SYSTEM = 0,
67   GST_NVENC_MEM_TYPE_GL,
68   GST_NVENC_MEM_TYPE_CUDA,
69   /* FIXME: add support D3D11 memory */
70 } GstNvEncMemType;
71
72 typedef struct {
73   gboolean weighted_prediction;
74   gint rc_modes;
75   gboolean custom_vbv_bufsize;
76   gboolean lookahead;
77   gboolean temporal_aq;
78   gint bframes;
79 } GstNvEncDeviceCaps;
80
81 typedef struct {
82   gint qp_i;
83   gint qp_p;
84   gint qp_b;
85 } GstNvEncQP;
86
87 typedef struct {
88   GstVideoEncoder video_encoder;
89
90   /* properties */
91   GstNvPreset     preset_enum;
92   GUID            selected_preset;
93   GstNvRCMode     rate_control_mode;
94   gint            qp_min;
95   GstNvEncQP      qp_min_detail;
96   gint            qp_max;
97   GstNvEncQP      qp_max_detail;
98   gint            qp_const;
99   GstNvEncQP      qp_const_detail;
100   guint           bitrate;
101   gint            gop_size;
102   guint           max_bitrate;
103   gboolean        spatial_aq;
104   guint           aq_strength;
105   gboolean        non_refp;
106   /* zero reorder delay (consistent naming with x264) */
107   gboolean        zerolatency;
108   gboolean        strict_gop;
109   gdouble         const_quality;
110   gboolean        i_adapt;
111
112   GstCudaContext * cuda_ctx;
113   CUstream         cuda_stream;
114   void          * encoder;
115   NV_ENC_INITIALIZE_PARAMS init_params;
116   NV_ENC_CONFIG            config;
117
118   /* the supported input formats */
119   GValue        * input_formats;                  /* OBJECT LOCK */
120
121   GstVideoCodecState *input_state;
122   volatile gint       reconfig;                   /* ATOMIC */
123   GstNvEncMemType     mem_type;
124
125   /* array of allocated input/output buffers (GstNvEncFrameState),
126    * and hold the ownership of the GstNvEncFrameState. */
127   GArray            *items;
128
129   /* (GstNvEncFrameState) available empty items which could be submitted
130    * to encoder */
131   GAsyncQueue       *available_queue;
132
133   /* (GstNvEncFrameState) submitted to encoder but not ready to finish
134    * (due to bframe or lookhead operation) */
135   GAsyncQueue       *pending_queue;
136
137   /* (GstNvEncFrameState) submitted to encoder and ready to finish.
138    * finished items will go back to available item queue */
139   GAsyncQueue       *bitstream_queue;
140
141   /* we spawn a thread that does the (blocking) waits for output buffers
142    * to become available, so we can continue to feed data to the encoder
143    * while we wait */
144   GThread        *bitstream_thread;
145
146   GstObject      *display;            /* GstGLDisplay */
147   GstObject      *other_context;      /* GstGLContext */
148   GstObject      *gl_context;         /* GstGLContext */
149
150   GstVideoInfo        input_info;     /* buffer configuration for buffers sent to NVENC */
151
152   GstFlowReturn   last_flow;          /* ATOMIC */
153
154   /* the first frame when bframe was enabled */
155   GstVideoCodecFrame *first_frame;
156   GstClockTime dts_offset;
157
158   /*< protected >*/
159   /* device capability dependent properties, set by subclass */
160   gboolean        weighted_pred;
161   guint           vbv_buffersize;
162   guint           rc_lookahead;
163   gboolean        temporal_aq;
164   guint           bframes;
165   gboolean        b_adapt;
166 } GstNvBaseEnc;
167
168 typedef struct {
169   GstVideoEncoderClass video_encoder_class;
170
171   GUID codec_id;
172   guint cuda_device_id;
173   GstNvEncDeviceCaps device_caps;
174
175   gboolean (*set_src_caps)       (GstNvBaseEnc * nvenc,
176                                   GstVideoCodecState * state);
177   gboolean (*set_pic_params)     (GstNvBaseEnc * nvenc,
178                                   GstVideoCodecFrame * frame,
179                                   NV_ENC_PIC_PARAMS * pic_params);
180   gboolean (*set_encoder_config) (GstNvBaseEnc * nvenc,
181                                   GstVideoCodecState * state,
182                                   NV_ENC_CONFIG * config);
183 } GstNvBaseEncClass;
184
185 G_GNUC_INTERNAL
186 GType gst_nv_base_enc_get_type (void);
187
188 GType gst_nv_base_enc_register                (const char * codec,
189                                                guint device_id,
190                                                GstNvEncDeviceCaps * device_caps);
191
192 void gst_nv_base_enc_schedule_reconfig        (GstNvBaseEnc * nvenc);
193
194
195 #endif /* __GST_NV_BASE_ENC_H_INCLUDED__ */