1 /******************************************************************************
2     Copyright (C) 2013-2014 by Hugh Bailey <obs.jim@gmail.com>
3 
4     This program is free software: you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation, either version 2 of the License, or
7     (at your option) any later version.
8 
9     This program 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
12     GNU General Public License for more details.
13 
14     You should have received a copy of the GNU General Public License
15     along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 ******************************************************************************/
17 
18 #pragma once
19 
20 /**
21  * @file
22  * @brief header for modules implementing encoders.
23  *
24  * Encoders are modules that implement some codec that can be used by libobs
25  * to process output data.
26  */
27 
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31 
32 #define OBS_ENCODER_CAP_DEPRECATED (1 << 0)
33 #define OBS_ENCODER_CAP_PASS_TEXTURE (1 << 1)
34 #define OBS_ENCODER_CAP_DYN_BITRATE (1 << 2)
35 #define OBS_ENCODER_CAP_INTERNAL (1 << 3)
36 
37 /** Specifies the encoder type */
38 enum obs_encoder_type {
39 	OBS_ENCODER_AUDIO, /**< The encoder provides an audio codec */
40 	OBS_ENCODER_VIDEO  /**< The encoder provides a video codec */
41 };
42 
43 /** Encoder output packet */
44 struct encoder_packet {
45 	uint8_t *data; /**< Packet data */
46 	size_t size;   /**< Packet size */
47 
48 	int64_t pts; /**< Presentation timestamp */
49 	int64_t dts; /**< Decode timestamp */
50 
51 	int32_t timebase_num; /**< Timebase numerator */
52 	int32_t timebase_den; /**< Timebase denominator */
53 
54 	enum obs_encoder_type type; /**< Encoder type */
55 
56 	bool keyframe; /**< Is a keyframe */
57 
58 	/* ---------------------------------------------------------------- */
59 	/* Internal video variables (will be parsed automatically) */
60 
61 	/* DTS in microseconds */
62 	int64_t dts_usec;
63 
64 	/* System DTS in microseconds */
65 	int64_t sys_dts_usec;
66 
67 	/**
68 	 * Packet priority
69 	 *
70 	 * This is generally use by video encoders to specify the priority
71 	 * of the packet.
72 	 */
73 	int priority;
74 
75 	/**
76 	 * Dropped packet priority
77 	 *
78 	 * If this packet needs to be dropped, the next packet must be of this
79 	 * priority or higher to continue transmission.
80 	 */
81 	int drop_priority;
82 
83 	/** Audio track index (used with outputs) */
84 	size_t track_idx;
85 
86 	/** Encoder from which the track originated from */
87 	obs_encoder_t *encoder;
88 };
89 
90 /** Encoder input frame */
91 struct encoder_frame {
92 	/** Data for the frame/audio */
93 	uint8_t *data[MAX_AV_PLANES];
94 
95 	/** size of each plane */
96 	uint32_t linesize[MAX_AV_PLANES];
97 
98 	/** Number of frames (audio only) */
99 	uint32_t frames;
100 
101 	/** Presentation timestamp */
102 	int64_t pts;
103 };
104 
105 /**
106  * Encoder interface
107  *
108  * Encoders have a limited usage with OBS.  You are not generally supposed to
109  * implement every encoder out there.  Generally, these are limited or specific
110  * encoders for h264/aac for streaming and recording.  It doesn't have to be
111  * *just* h264 or aac of course, but generally those are the expected encoders.
112  *
113  * That being said, other encoders will be kept in mind for future use.
114  */
115 struct obs_encoder_info {
116 	/* ----------------------------------------------------------------- */
117 	/* Required implementation*/
118 
119 	/** Specifies the named identifier of this encoder */
120 	const char *id;
121 
122 	/** Specifies the encoder type (video or audio) */
123 	enum obs_encoder_type type;
124 
125 	/** Specifies the codec */
126 	const char *codec;
127 
128 	/**
129 	 * Gets the full translated name of this encoder
130 	 *
131 	 * @param  type_data  The type_data variable of this structure
132 	 * @return            Translated name of the encoder
133 	 */
134 	const char *(*get_name)(void *type_data);
135 
136 	/**
137 	 * Creates the encoder with the specified settings
138 	 *
139 	 * @param  settings  Settings for the encoder
140 	 * @param  encoder   OBS encoder context
141 	 * @return           Data associated with this encoder context, or
142 	 *                   NULL if initialization failed.
143 	 */
144 	void *(*create)(obs_data_t *settings, obs_encoder_t *encoder);
145 
146 	/**
147 	 * Destroys the encoder data
148 	 *
149 	 * @param  data  Data associated with this encoder context
150 	 */
151 	void (*destroy)(void *data);
152 
153 	/**
154 	 * Encodes frame(s), and outputs encoded packets as they become
155 	 * available.
156 	 *
157 	 * @param       data             Data associated with this encoder
158 	 *                               context
159 	 * @param[in]   frame            Raw audio/video data to encode
160 	 * @param[out]  packet           Encoder packet output, if any
161 	 * @param[out]  received_packet  Set to true if a packet was received,
162 	 *                               false otherwise
163 	 * @return                       true if successful, false otherwise.
164 	 */
165 	bool (*encode)(void *data, struct encoder_frame *frame,
166 		       struct encoder_packet *packet, bool *received_packet);
167 
168 	/** Audio encoder only:  Returns the frame size for this encoder */
169 	size_t (*get_frame_size)(void *data);
170 
171 	/* ----------------------------------------------------------------- */
172 	/* Optional implementation */
173 
174 	/**
175 	 * Gets the default settings for this encoder
176 	 *
177 	 * @param[out]  settings  Data to assign default settings to
178 	 */
179 	void (*get_defaults)(obs_data_t *settings);
180 
181 	/**
182 	 * Gets the property information of this encoder
183 	 *
184 	 * @return         The properties data
185 	 */
186 	obs_properties_t *(*get_properties)(void *data);
187 
188 	/**
189 	 * Updates the settings for this encoder (usually used for things like
190 	 * changing bitrate while active)
191 	 *
192 	 * @param  data      Data associated with this encoder context
193 	 * @param  settings  New settings for this encoder
194 	 * @return           true if successful, false otherwise
195 	 */
196 	bool (*update)(void *data, obs_data_t *settings);
197 
198 	/**
199 	 * Returns extra data associated with this encoder (usually header)
200 	 *
201 	 * @param  data             Data associated with this encoder context
202 	 * @param[out]  extra_data  Pointer to receive the extra data
203 	 * @param[out]  size        Pointer to receive the size of the extra
204 	 *                          data
205 	 * @return                  true if extra data available, false
206 	 *                          otherwise
207 	 */
208 	bool (*get_extra_data)(void *data, uint8_t **extra_data, size_t *size);
209 
210 	/**
211 	 * Gets the SEI data, if any
212 	 *
213 	 * @param       data      Data associated with this encoder context
214 	 * @param[out]  sei_data  Pointer to receive the SEI data
215 	 * @param[out]  size      Pointer to receive the SEI data size
216 	 * @return                true if SEI data available, false otherwise
217 	 */
218 	bool (*get_sei_data)(void *data, uint8_t **sei_data, size_t *size);
219 
220 	/**
221 	 * Returns desired audio format and sample information
222 	 *
223 	 * @param          data  Data associated with this encoder context
224 	 * @param[in/out]  info  Audio format information
225 	 */
226 	void (*get_audio_info)(void *data, struct audio_convert_info *info);
227 
228 	/**
229 	 * Returns desired video format information
230 	 *
231 	 * @param          data  Data associated with this encoder context
232 	 * @param[in/out]  info  Video format information
233 	 */
234 	void (*get_video_info)(void *data, struct video_scale_info *info);
235 
236 	void *type_data;
237 	void (*free_type_data)(void *type_data);
238 
239 	uint32_t caps;
240 
241 	/**
242 	 * Gets the default settings for this encoder
243 	 *
244 	 * If get_defaults is also defined both will be called, and the first
245 	 * call will be to get_defaults, then to get_defaults2.
246 	 *
247 	 * @param[out]  settings  Data to assign default settings to
248 	 * @param[in]   typedata  Type Data
249 	 */
250 	void (*get_defaults2)(obs_data_t *settings, void *type_data);
251 
252 	/**
253 	 * Gets the property information of this encoder
254 	 *
255 	 * @param[in]   data      Pointer from create (or null)
256 	 * @param[in]   typedata  Type Data
257 	 * @return                The properties data
258 	 */
259 	obs_properties_t *(*get_properties2)(void *data, void *type_data);
260 
261 	bool (*encode_texture)(void *data, uint32_t handle, int64_t pts,
262 			       uint64_t lock_key, uint64_t *next_key,
263 			       struct encoder_packet *packet,
264 			       bool *received_packet);
265 };
266 
267 EXPORT void obs_register_encoder_s(const struct obs_encoder_info *info,
268 				   size_t size);
269 
270 /**
271  * Register an encoder definition to the current obs context.  This should be
272  * used in obs_module_load.
273  *
274  * @param  info  Pointer to the source definition structure.
275  */
276 #define obs_register_encoder(info) \
277 	obs_register_encoder_s(info, sizeof(struct obs_encoder_info))
278 
279 #ifdef __cplusplus
280 }
281 #endif
282