reference-encoders.rst 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  1. Encoder API Reference (obs_encoder_t)
  2. =====================================
  3. Encoders are OBS-specific implementations of video/audio encoders, which
  4. are used with outputs that use encoders. x264, NVENC, Quicksync are
  5. examples of encoder implementations. The `libobs/obs-encoder.h`_ file
  6. is the dedicated header for implementing encoders
  7. .. type:: obs_encoder_t
  8. A reference-counted encoder object.
  9. .. type:: obs_weak_encoder_t
  10. A weak reference to an encoder object.
  11. .. code:: cpp
  12. #include <obs.h>
  13. Encoder Definition Structure (obs_encoder_info)
  14. -----------------------------------------------
  15. .. struct:: obs_encoder_info
  16. Encoder definition structure.
  17. .. member:: const char *obs_encoder_info.id
  18. Unique string identifier for the encoder (required).
  19. .. member:: enum obs_encoder_type obs_encoder_info.type
  20. Type of encoder.
  21. - **OBS_ENCODER_VIDEO** - Video encoder
  22. - **OBS_ENCODER_AUDIO** - Audio encoder
  23. .. member:: const char *obs_encoder_info.codec
  24. The codec, in string form. For example, "h264" for an H.264 encoder.
  25. .. member:: const char *(*obs_encoder_info.get_name)(void *type_data)
  26. Get the translated name of the encoder type.
  27. :param type_data: The type_data variable of this structure
  28. :return: The translated name of the encoder type
  29. .. member:: void *(*obs_encoder_info.create)(obs_data_t *settings, obs_encoder_t *encoder)
  30. Creates the implementation data for the encoder.
  31. :param settings: Settings to initialize the encoder with
  32. :param encoder: Encoder that this data is associated with
  33. :return: The implementation data associated with this encoder
  34. .. member:: void (*obs_encoder_info.destroy)(void *data)
  35. Destroys the implementation data for the encoder.
  36. .. member:: bool (*encode)(void *data, struct encoder_frame *frame, struct encoder_packet *packet, bool *received_packet)
  37. Called to encode video or audio and outputs packets as they become
  38. available.
  39. :param frame: Raw audio/video data to encode
  40. :param packet: Encoder packet output, if any
  41. :param received_packet: Set to *true* if a packet was received,
  42. *false* otherwise
  43. :return: true if successful, false on critical failure
  44. .. member:: size_t (*get_frame_size)(void *data)
  45. :return: An audio encoder's frame size. For example, for AAC this
  46. number would be 1024
  47. .. member:: void (*obs_encoder_info.get_defaults)(obs_data_t *settings)
  48. void (*obs_encoder_info.get_defaults2)(obs_data_t *settings, void *type_data)
  49. Sets the default settings for this encoder.
  50. :param settings: Default settings. Call obs_data_set_default*
  51. functions on this object to set default setting
  52. values
  53. .. member:: obs_properties_t *(*obs_encoder_info.get_properties)(void *data)
  54. obs_properties_t *(*obs_encoder_info.get_properties2)(void *data, void *type_data)
  55. Gets the property information of this encoder.
  56. :param data: The implementation data associated with this encoder.
  57. This value can be null (e.g., when
  58. :c:func:`obs_get_encoder_properties()` is called on the
  59. encoder type), make sure to handle this gracefully.
  60. (Optional)
  61. :return: The properties of the encoder
  62. .. member:: void (*obs_encoder_info.update)(void *data, obs_data_t *settings)
  63. Updates the settings for this encoder.
  64. (Optional)
  65. :param settings: New settings for this encoder
  66. .. member:: bool (*obs_encoder_info.get_extra_data)(void *data, uint8_t **extra_data, size_t *size)
  67. Returns extra data associated with this encoder (usually header).
  68. (Optional)
  69. :param extra_data: Pointer to receive the extra data
  70. :param size: Pointer to receive the size of the extra
  71. data
  72. :return: true if extra data available, false
  73. otherwise
  74. .. member:: bool (*obs_encoder_info.get_sei_data)(void *data, uint8_t **sei_data, size_t *size)
  75. Gets the SEI data of a video encoder that has SEI data.
  76. (Optional)
  77. :param sei_data: Pointer to receive the SEI data
  78. :param size: Pointer to receive the SEI data size
  79. :return: true if SEI data available, false otherwise
  80. .. member:: void (*obs_encoder_info.get_audio_info)(void *data, struct audio_convert_info *info)
  81. Returns desired audio format and sample information. This callback
  82. can be used to tell the back-end that the audio data needs to be
  83. automatically converted to a different sample rate or audio format
  84. before being sent to the encoder.
  85. (Optional)
  86. :param info: Audio format information
  87. .. member:: void (*obs_encoder_info.get_video_info)(void *data, struct video_scale_info *info)
  88. Returns desired video format information. This callback can be used
  89. to tell the back-end that the video data needs to be automatically
  90. converted to a different video format or specific size before being
  91. sent to the encoder.
  92. :param info: Video format information
  93. .. member:: void *obs_encoder_info.type_data
  94. void (*obs_encoder_info.free_type_data)(void *type_data)
  95. Private data associated with this entry. Note that this is not the
  96. same as the implementation data; this is used to differentiate
  97. between two different types if the same callbacks are used for more
  98. than one different type.
  99. .. member:: uint32_t obs_encoder_info.caps
  100. Can be 0 or a bitwise OR combination of one or more of the following
  101. values:
  102. - **OBS_ENCODER_CAP_DEPRECATED** - Encoder is deprecated
  103. - **OBS_ENCODER_CAP_ROI** - Encoder supports region of interest feature
  104. .. versionadded:: 30.1
  105. Encoder Packet Structure (encoder_packet)
  106. -----------------------------------------
  107. .. struct:: encoder_packet
  108. Encoder packet structure.
  109. .. member:: uint8_t *encoder_packet.data
  110. Packet data.
  111. .. member:: size_t encoder_packet.size
  112. Packet size.
  113. .. member:: int64_t encoder_packet.pts
  114. int64_t encoder_packet.dts
  115. Packet presentation and decode timestamps.
  116. .. member:: int32_t encoder_packet.timebase_num
  117. int32_t encoder_packet.timebase_den
  118. Packet time base.
  119. .. member:: enum obs_encoder_type encoder_packet.type
  120. Can be one of the following values:
  121. - **OBS_ENCODER_VIDEO** - Video data
  122. - **OBS_ENCODER_AUDIO** - Audio data
  123. .. member:: bool encoder_packet.keyframe
  124. Packet is a keyframe.
  125. .. member:: int64_t encoder_packet.dts_usec
  126. The DTS in microseconds.
  127. (This should not be set by the encoder implementation)
  128. .. member:: int64_t encoder_packet.sys_dts_usec
  129. The system time of this packet in microseconds.
  130. (This should not be set by the encoder implementation)
  131. .. member:: int encoder_packet.priority
  132. Packet priority. This is no longer used.
  133. (This should not be set by the encoder implementation)
  134. .. member:: int encoder_packet.drop_priority
  135. Packet drop priority.
  136. If this packet needs to be dropped, the next packet must be of this
  137. priority or higher to continue transmission.
  138. (This should not be set by the encoder implementation)
  139. .. member:: size_t encoder_packet.track_idx
  140. Audio track index.
  141. (This should not be set by the encoder implementation)
  142. .. member:: obs_encoder_t *encoder_packet.encoder
  143. Encoder object associated with this packet.
  144. (This should not be set by the encoder implementation)
  145. Raw Frame Data Structure (encoder_frame)
  146. ----------------------------------------
  147. .. struct:: encoder_frame
  148. Raw frame data structure.
  149. .. member:: uint8_t *encoder_frame.data[MAX_AV_PLANES]
  150. Raw video/audio data.
  151. .. member:: uint32_t encoder_frame.linesize[MAX_AV_PLANES]
  152. Line size of each plane.
  153. .. member:: uint32_t encoder_frame.frames
  154. Number of audio frames (if audio).
  155. .. member:: int64_t encoder_frame.pts
  156. Presentation timestamp.
  157. Encoder Region of Interest Structure (obs_encoder_roi)
  158. ------------------------------------------------------
  159. .. struct:: obs_encoder_roi
  160. Encoder region of interest structure.
  161. .. versionadded:: 30.1
  162. .. member:: uint32_t top
  163. uint32_t bottom
  164. uint32_t left
  165. uint32_t right
  166. The rectangle edges of the region are specified as number of pixels from the input video's top and left edges (i.e. row/column 0).
  167. .. member:: float priority
  168. Priority is specified as a float value between *-1.0f* and *1*.
  169. These are converted to encoder-specific values by the encoder.
  170. Values above 0 tell the encoder to increase quality for that region, values below tell it to worsen it.
  171. Not all encoders support negative values and they may be ignored.
  172. General Encoder Functions
  173. -------------------------
  174. .. function:: void obs_register_encoder(struct obs_encoder_info *info)
  175. Registers an encoder type. Typically used in
  176. :c:func:`obs_module_load()` or in the program's initialization phase.
  177. ---------------------
  178. .. function:: const char *obs_encoder_get_display_name(const char *id)
  179. Calls the :c:member:`obs_encoder_info.get_name` callback to get the
  180. translated display name of an encoder type.
  181. :param id: The encoder type string identifier
  182. :return: The translated display name of an encoder type
  183. ---------------------
  184. .. function:: obs_encoder_t *obs_video_encoder_create(const char *id, const char *name, obs_data_t *settings, obs_data_t *hotkey_data)
  185. Creates a video encoder with the specified settings.
  186. The "encoder" context is used for encoding video/audio data. Use
  187. obs_encoder_release to release it.
  188. :param id: The encoder type string identifier
  189. :param name: The desired name of the encoder. If this is
  190. not unique, it will be made to be unique
  191. :param settings: The settings for the encoder, or *NULL* if
  192. none
  193. :param hotkey_data: Saved hotkey data for the encoder, or *NULL*
  194. if none
  195. :return: A reference to the newly created encoder, or
  196. *NULL* if failed
  197. ---------------------
  198. .. function:: obs_encoder_t *obs_audio_encoder_create(const char *id, const char *name, obs_data_t *settings, size_t mixer_idx, obs_data_t *hotkey_data)
  199. Creates an audio encoder with the specified settings.
  200. The "encoder" context is used for encoding video/audio data. Use
  201. :c:func:`obs_encoder_release()` to release it.
  202. :param id: The encoder type string identifier
  203. :param name: The desired name of the encoder. If this is
  204. not unique, it will be made to be unique
  205. :param settings: The settings for the encoder, or *NULL* if
  206. none
  207. :param mixer_idx: The audio mixer index this audio encoder
  208. will capture audio from
  209. :param hotkey_data: Saved hotkey data for the encoder, or *NULL*
  210. if none
  211. :return: A reference to the newly created encoder, or
  212. *NULL* if failed
  213. ---------------------
  214. .. function:: void obs_encoder_addref(obs_encoder_t *encoder)
  215. Adds a reference to an encoder.
  216. .. deprecated:: 27.2.0
  217. Use :c:func:`obs_encoder_get_ref()` instead.
  218. ---------------------
  219. .. function:: obs_encoder_t *obs_encoder_get_ref(obs_encoder_t *encoder)
  220. Returns an incremented reference if still valid, otherwise returns
  221. *NULL*. Release with :c:func:`obs_encoder_release()`.
  222. ---------------------
  223. .. function:: void obs_encoder_release(obs_encoder_t *encoder)
  224. Releases a reference to an encoder. When the last reference is released,
  225. the encoder is destroyed.
  226. ---------------------
  227. .. function:: obs_weak_encoder_t *obs_encoder_get_weak_encoder(obs_encoder_t *encoder)
  228. obs_encoder_t *obs_weak_encoder_get_encoder(obs_weak_encoder_t *weak)
  229. These functions are used to get a weak reference from a strong encoder
  230. reference, or a strong encoder reference from a weak reference. If
  231. the encoder is destroyed, *obs_weak_encoder_get_encoder* will return
  232. *NULL*.
  233. ---------------------
  234. .. function:: void obs_weak_encoder_addref(obs_weak_encoder_t *weak)
  235. void obs_weak_encoder_release(obs_weak_encoder_t *weak)
  236. Adds/releases a weak reference to an encoder.
  237. ---------------------
  238. .. function:: void obs_encoder_set_name(obs_encoder_t *encoder, const char *name)
  239. Sets the name of an encoder. If the encoder is not private and the
  240. name is not unique, it will automatically be given a unique name.
  241. ---------------------
  242. .. function:: const char *obs_encoder_get_name(const obs_encoder_t *encoder)
  243. :return: The name of the encoder
  244. ---------------------
  245. .. function:: const char *obs_encoder_get_codec(const obs_encoder_t *encoder)
  246. const char *obs_get_encoder_codec(const char *id)
  247. :return: The codec identifier of the encoder
  248. ---------------------
  249. .. function:: enum obs_encoder_type obs_encoder_get_type(const obs_encoder_t *encoder)
  250. enum obs_encoder_type obs_get_encoder_type(const char *id)
  251. :return: The encoder type: OBS_ENCODER_VIDEO or OBS_ENCODER_AUDIO
  252. ---------------------
  253. .. function:: void obs_encoder_set_scaled_size(obs_encoder_t *encoder, uint32_t width, uint32_t height)
  254. Sets the scaled resolution for a video encoder. Set width and height to 0
  255. to disable scaling. If the encoder is active, this function will trigger
  256. a warning, and do nothing.
  257. ---------------------
  258. .. function:: bool obs_encoder_scaling_enabled(const obs_encoder_t *encoder)
  259. :return: *true* if pre-encode (CPU) scaling enabled, *false*
  260. otherwise.
  261. ---------------------
  262. .. function:: uint32_t obs_encoder_get_width(const obs_encoder_t *encoder)
  263. uint32_t obs_encoder_get_height(const obs_encoder_t *encoder)
  264. :return: The width/height of a video encoder's encoded image
  265. ---------------------
  266. .. function:: uint32_t obs_encoder_get_sample_rate(const obs_encoder_t *encoder)
  267. :return: The sample rate of an audio encoder's audio data
  268. ---------------------
  269. .. function:: size_t obs_encoder_get_frame_size(const obs_encoder_t *encoder)
  270. :return: The frame size of the audio packet
  271. ---------------------
  272. .. function:: void obs_encoder_set_preferred_video_format(obs_encoder_t *encoder, enum video_format format)
  273. enum video_format obs_encoder_get_preferred_video_format(const obs_encoder_t *encoder)
  274. Sets the preferred video format for a video encoder. If the encoder can use
  275. the format specified, it will force a conversion to that format if the
  276. obs output format does not match the preferred format.
  277. If the format is set to VIDEO_FORMAT_NONE, will revert to the default
  278. functionality of converting only when absolutely necessary.
  279. ---------------------
  280. .. function:: obs_data_t *obs_encoder_defaults(const char *id)
  281. obs_data_t *obs_encoder_get_defaults(const obs_encoder_t *encoder)
  282. :return: An incremented reference to the encoder's default settings.
  283. Release with :c:func:`obs_data_release()`.
  284. ---------------------
  285. .. function:: obs_properties_t *obs_encoder_properties(const obs_encoder_t *encoder)
  286. obs_properties_t *obs_get_encoder_properties(const char *id)
  287. Use these functions to get the properties of an encoder or encoder
  288. type. Properties are optionally used (if desired) to automatically
  289. generate user interface widgets to allow users to update settings.
  290. :return: The properties list for a specific existing encoder. Free
  291. with :c:func:`obs_properties_destroy()`
  292. ---------------------
  293. .. function:: void obs_encoder_update(obs_encoder_t *encoder, obs_data_t *settings)
  294. Updates the settings for this encoder context.
  295. ---------------------
  296. .. function:: obs_data_t *obs_encoder_get_settings(const obs_encoder_t *encoder)
  297. :return: An incremented reference to the encoder's settings. Release with
  298. :c:func:`obs_data_release()`.
  299. ---------------------
  300. .. function:: signal_handler_t *obs_encoder_get_signal_handler(const obs_encoder_t *encoder)
  301. :return: The signal handler of the encoder. Should not be manually freed,
  302. as its lifecycle is managed by libobs.
  303. ---------------------
  304. .. function:: proc_handler_t *obs_encoder_get_proc_handler(const obs_encoder_t *encoder)
  305. :return: The procedure handler of the encoder. Should not be manually freed,
  306. as its lifecycle is managed by libobs.
  307. ---------------------
  308. .. function:: bool obs_encoder_get_extra_data(const obs_encoder_t *encoder, uint8_t **extra_data, size_t *size)
  309. Gets extra data (headers) associated with this encoder.
  310. :return: *true* if successful, *false* if no extra data associated
  311. with this encoder
  312. ---------------------
  313. .. function:: void obs_encoder_set_video(obs_encoder_t *encoder, video_t *video)
  314. void obs_encoder_set_audio(obs_encoder_t *encoder, audio_t *audio)
  315. Sets the video/audio handler to use with this video/audio encoder.
  316. This is used to capture the raw video/audio data.
  317. ---------------------
  318. .. function:: video_t *obs_encoder_video(const obs_encoder_t *encoder)
  319. video_t *obs_encoder_parent_video(const obs_encoder_t *encoder)
  320. audio_t *obs_encoder_audio(const obs_encoder_t *encoder)
  321. :return: The video/audio handler associated with this encoder, or
  322. *NULL* if none or not a matching encoder type.
  323. *parent_video* returns the "original" video handler
  324. associated with this encoder, regardless of whether an FPS
  325. divisor is set.
  326. ---------------------
  327. .. function:: bool obs_encoder_active(const obs_encoder_t *encoder)
  328. :return: *true* if the encoder is active, *false* otherwise
  329. ---------------------
  330. .. function:: bool obs_encoder_add_roi(obs_encoder_t *encoder, const struct obs_encoder_roi *roi)
  331. Adds a new region of interest to the encoder if ROI feature is supported.
  332. :return: *true* if adding succeeded, *false* otherwise.
  333. .. versionadded:: 30.1
  334. ---------------------
  335. .. function:: bool obs_encoder_has_roi(obs_encoder_t *encoder)
  336. :return: *true* if encoder has ROI regions set, *false* otherwise.
  337. .. versionadded:: 30.1
  338. ---------------------
  339. .. function:: void obs_encoder_clear_roi(obs_encoder_t *encoder)
  340. Clear region of interest list, if any.
  341. .. versionadded:: 30.1
  342. ---------------------
  343. .. function:: void obs_encoder_enum_roi(obs_encoder_t *encoder, void (*enum_proc)(void *, struct obs_encoder_roi *), void *param)
  344. Enumerate currently configured ROIs by invoking callback for each entry, in reverse order of addition (i.e. most recent to oldest).
  345. **Note:** If the encoder has scaling enabled the struct passed to the callback will be scaled accordingly.
  346. .. versionadded:: 30.1
  347. ---------------------
  348. .. function:: uint32_t obs_encoder_get_roi_increment(const obs_encoder_t *encoder)
  349. Encoders shall refresh their ROI configuration if the increment value changes.
  350. :return: Increment/revision of ROI list
  351. .. versionadded:: 30.1
  352. ---------------------
  353. Functions used by encoders
  354. --------------------------
  355. .. function:: void obs_encoder_packet_ref(struct encoder_packet *dst, struct encoder_packet *src)
  356. void obs_encoder_packet_release(struct encoder_packet *packet)
  357. Adds or releases a reference to an encoder packet.
  358. .. ---------------------------------------------------------------------------
  359. .. _libobs/obs-encoder.h: https://github.com/obsproject/obs-studio/blob/master/libobs/obs-encoder.h