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 
27 #define GST_TYPE_NV_BASE_ENC \
28   (gst_nv_base_enc_get_type())
29 #define GST_NV_BASE_ENC(obj) \
30   (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_NV_BASE_ENC,GstNvBaseEnc))
31 #define GST_NV_BASE_ENC_CLASS(klass) \
32   (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_NV_BASE_ENC,GstNvBaseEncClass))
33 #define GST_NV_BASE_ENC_GET_CLASS(obj) \
34   (G_TYPE_INSTANCE_GET_CLASS((obj),GST_TYPE_NV_BASE_ENC,GstNvBaseEncClass))
35 #define GST_IS_NV_BASE_ENC(obj) \
36   (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_NV_BASE_ENC))
37 #define GST_IS_NV_BASE_ENC_CLASS(obj) \
38   (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_NV_BASE_ENC))
39 
40 typedef enum {
41   GST_NV_PRESET_DEFAULT,
42   GST_NV_PRESET_HP,
43   GST_NV_PRESET_HQ,
44 /* FIXME: problematic GST_NV_PRESET_BD, */
45   GST_NV_PRESET_LOW_LATENCY_DEFAULT,
46   GST_NV_PRESET_LOW_LATENCY_HQ,
47   GST_NV_PRESET_LOW_LATENCY_HP,
48   GST_NV_PRESET_LOSSLESS_DEFAULT,
49   GST_NV_PRESET_LOSSLESS_HP,
50 } GstNvPreset;
51 
52 typedef enum {
53   GST_NV_RC_MODE_DEFAULT,
54   GST_NV_RC_MODE_CONSTQP,
55   GST_NV_RC_MODE_CBR,
56   GST_NV_RC_MODE_VBR,
57   GST_NV_RC_MODE_VBR_MINQP,
58 } GstNvRCMode;
59 
60 typedef struct {
61   GstVideoEncoder video_encoder;
62 
63   /* properties */
64   guint           cuda_device_id;
65   GstNvPreset     preset_enum;
66   GUID            selected_preset;
67   GstNvRCMode     rate_control_mode;
68   gint            qp_min;
69   gint            qp_max;
70   gint            qp_const;
71   guint           bitrate;
72   gint            gop_size;
73 
74   CUcontext       cuda_ctx;
75   void          * encoder;
76 
77   /* the supported input formats */
78   GValue        * input_formats;                  /* OBJECT LOCK */
79 
80   GstVideoCodecState *input_state;
81   volatile gint       reconfig;                   /* ATOMIC */
82   gboolean            gl_input;
83 
84   /* allocated buffers */
85   gpointer          *input_bufs;   /* array of n_allocs input buffers  */
86   NV_ENC_OUTPUT_PTR *output_bufs;  /* array of n_allocs output buffers */
87   guint              n_bufs;
88 
89   /* input and output buffers currently available */
90   GAsyncQueue    *in_bufs_pool;
91   GAsyncQueue    *bitstream_pool;
92 
93   /* output bufs in use (input bufs in use are tracked via the codec frames) */
94   GAsyncQueue    *bitstream_queue;
95 
96   /* we spawn a thread that does the (blocking) waits for output buffers
97    * to become available, so we can continue to feed data to the encoder
98    * while we wait */
99   GThread        *bitstream_thread;
100 
101   /* supported interlacing input modes.
102    * 0 = none, 1 = fields, 2 = interleaved */
103   gint            interlace_modes;
104 
105   void           *display;            /* GstGLDisplay */
106   void           *other_context;      /* GstGLContext */
107 
108   /* the maximum buffer size the encoder is configured for */
109   guint               max_encode_width;
110   guint               max_encode_height;
111 
112   GstVideoInfo        input_info;     /* buffer configuration for buffers sent to NVENC */
113 
114   GstFlowReturn   last_flow;          /* ATOMIC */
115 } GstNvBaseEnc;
116 
117 typedef struct {
118   GstVideoEncoderClass video_encoder_class;
119 
120   GUID codec_id;
121 
122   gboolean (*set_src_caps)       (GstNvBaseEnc * nvenc,
123                                   GstVideoCodecState * state);
124   gboolean (*set_pic_params)     (GstNvBaseEnc * nvenc,
125                                   GstVideoCodecFrame * frame,
126                                   NV_ENC_PIC_PARAMS * pic_params);
127   gboolean (*set_encoder_config) (GstNvBaseEnc * nvenc,
128                                   GstVideoCodecState * state,
129                                   NV_ENC_CONFIG * config);
130 } GstNvBaseEncClass;
131 
132 G_GNUC_INTERNAL
133 GType gst_nv_base_enc_get_type (void);
134 
135 
136 void gst_nv_base_enc_get_max_encode_size      (GstNvBaseEnc * nvenc,
137                                                guint * max_width,
138                                                guint * max_height);
139 void gst_nv_base_enc_set_max_encode_size      (GstNvBaseEnc * nvenc,
140                                                guint max_width,
141                                                guint max_height);
142 
143 #endif /* __GST_NV_BASE_ENC_H_INCLUDED__ */
144