reference-encoders.rst 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  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. Encoder Packet Structure (encoder_packet)
  104. -----------------------------------------
  105. .. struct:: encoder_packet
  106. Encoder packet structure.
  107. .. member:: uint8_t *encoder_packet.data
  108. Packet data.
  109. .. member:: size_t encoder_packet.size
  110. Packet size.
  111. .. member:: int64_t encoder_packet.pts
  112. int64_t encoder_packet.dts
  113. Packet presentation and decode timestamps.
  114. .. member:: int32_t encoder_packet.timebase_num
  115. int32_t encoder_packet.timebase_den
  116. Packet time base.
  117. .. member:: enum obs_encoder_type encoder_packet.type
  118. Can be one of the following values:
  119. - **OBS_ENCODER_VIDEO** - Video data
  120. - **OBS_ENCODER_AUDIO** - Audio data
  121. .. member:: bool encoder_packet.keyframe
  122. Packet is a keyframe.
  123. .. member:: int64_t encoder_packet.dts_usec
  124. The DTS in microseconds.
  125. (This should not be set by the encoder implementation)
  126. .. member:: int64_t encoder_packet.sys_dts_usec
  127. The system time of this packet in microseconds.
  128. (This should not be set by the encoder implementation)
  129. .. member:: int encoder_packet.priority
  130. Packet priority. This is no longer used.
  131. (This should not be set by the encoder implementation)
  132. .. member:: int encoder_packet.drop_priority
  133. Packet drop priority.
  134. If this packet needs to be dropped, the next packet must be of this
  135. priority or higher to continue transmission.
  136. (This should not be set by the encoder implementation)
  137. .. member:: size_t encoder_packet.track_idx
  138. Audio track index.
  139. (This should not be set by the encoder implementation)
  140. .. member:: obs_encoder_t *encoder_packet.encoder
  141. Encoder object associated with this packet.
  142. (This should not be set by the encoder implementation)
  143. Raw Frame Data Structure (encoder_frame)
  144. ----------------------------------------
  145. .. struct:: encoder_frame
  146. Raw frame data structure.
  147. .. member:: uint8_t *encoder_frame.data[MAX_AV_PLANES]
  148. Raw video/audio data.
  149. .. member:: uint32_t encoder_frame.linesize[MAX_AV_PLANES]
  150. Line size of each plane.
  151. .. member:: uint32_t encoder_frame.frames
  152. Number of audio frames (if audio).
  153. .. member:: int64_t encoder_frame.pts
  154. Presentation timestamp.
  155. General Encoder Functions
  156. -------------------------
  157. .. function:: void obs_register_encoder(struct obs_encoder_info *info)
  158. Registers an encoder type. Typically used in
  159. :c:func:`obs_module_load()` or in the program's initialization phase.
  160. ---------------------
  161. .. function:: const char *obs_encoder_get_display_name(const char *id)
  162. Calls the :c:member:`obs_encoder_info.get_name` callback to get the
  163. translated display name of an encoder type.
  164. :param id: The encoder type string identifier
  165. :return: The translated display name of an encoder type
  166. ---------------------
  167. .. function:: obs_encoder_t *obs_video_encoder_create(const char *id, const char *name, obs_data_t *settings, obs_data_t *hotkey_data)
  168. Creates a video encoder with the specified settings.
  169. The "encoder" context is used for encoding video/audio data. Use
  170. obs_encoder_release to release it.
  171. :param id: The encoder type string identifier
  172. :param name: The desired name of the encoder. If this is
  173. not unique, it will be made to be unique
  174. :param settings: The settings for the encoder, or *NULL* if
  175. none
  176. :param hotkey_data: Saved hotkey data for the encoder, or *NULL*
  177. if none
  178. :return: A reference to the newly created encoder, or
  179. *NULL* if failed
  180. ---------------------
  181. .. 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)
  182. Creates an audio encoder with the specified settings.
  183. The "encoder" context is used for encoding video/audio data. Use
  184. :c:func:`obs_encoder_release()` to release it.
  185. :param id: The encoder type string identifier
  186. :param name: The desired name of the encoder. If this is
  187. not unique, it will be made to be unique
  188. :param settings: The settings for the encoder, or *NULL* if
  189. none
  190. :param mixer_idx: The audio mixer index this audio encoder
  191. will capture audio from
  192. :param hotkey_data: Saved hotkey data for the encoder, or *NULL*
  193. if none
  194. :return: A reference to the newly created encoder, or
  195. *NULL* if failed
  196. ---------------------
  197. .. function:: void obs_encoder_addref(obs_encoder_t *encoder)
  198. Adds a reference to an encoder.
  199. .. deprecated:: 27.2.0
  200. Use :c:func:`obs_encoder_get_ref()` instead.
  201. ---------------------
  202. .. function:: obs_encoder_t *obs_encoder_get_ref(obs_encoder_t *encoder)
  203. Returns an incremented reference if still valid, otherwise returns
  204. *NULL*. Release with :c:func:`obs_encoder_release()`.
  205. ---------------------
  206. .. function:: void obs_encoder_release(obs_encoder_t *encoder)
  207. Releases a reference to an encoder. When the last reference is released,
  208. the encoder is destroyed.
  209. ---------------------
  210. .. function:: obs_weak_encoder_t *obs_encoder_get_weak_encoder(obs_encoder_t *encoder)
  211. obs_encoder_t *obs_weak_encoder_get_encoder(obs_weak_encoder_t *weak)
  212. These functions are used to get a weak reference from a strong encoder
  213. reference, or a strong encoder reference from a weak reference. If
  214. the encoder is destroyed, *obs_weak_encoder_get_encoder* will return
  215. *NULL*.
  216. ---------------------
  217. .. function:: void obs_weak_encoder_addref(obs_weak_encoder_t *weak)
  218. void obs_weak_encoder_release(obs_weak_encoder_t *weak)
  219. Adds/releases a weak reference to an encoder.
  220. ---------------------
  221. .. function:: void obs_encoder_set_name(obs_encoder_t *encoder, const char *name)
  222. Sets the name of an encoder. If the encoder is not private and the
  223. name is not unique, it will automatically be given a unique name.
  224. ---------------------
  225. .. function:: const char *obs_encoder_get_name(const obs_encoder_t *encoder)
  226. :return: The name of the encoder
  227. ---------------------
  228. .. function:: const char *obs_encoder_get_codec(const obs_encoder_t *encoder)
  229. const char *obs_get_encoder_codec(const char *id)
  230. :return: The codec identifier of the encoder
  231. ---------------------
  232. .. function:: enum obs_encoder_type obs_encoder_get_type(const obs_encoder_t *encoder)
  233. enum obs_encoder_type obs_get_encoder_type(const char *id)
  234. :return: The encoder type: OBS_ENCODER_VIDEO or OBS_ENCODER_AUDIO
  235. ---------------------
  236. .. function:: void obs_encoder_set_scaled_size(obs_encoder_t *encoder, uint32_t width, uint32_t height)
  237. Sets the scaled resolution for a video encoder. Set width and height to 0
  238. to disable scaling. If the encoder is active, this function will trigger
  239. a warning, and do nothing.
  240. ---------------------
  241. .. function:: bool obs_encoder_scaling_enabled(const obs_encoder_t *encoder)
  242. :return: *true* if pre-encode (CPU) scaling enabled, *false*
  243. otherwise.
  244. ---------------------
  245. .. function:: uint32_t obs_encoder_get_width(const obs_encoder_t *encoder)
  246. uint32_t obs_encoder_get_height(const obs_encoder_t *encoder)
  247. :return: The width/height of a video encoder's encoded image
  248. ---------------------
  249. .. function:: uint32_t obs_encoder_get_sample_rate(const obs_encoder_t *encoder)
  250. :return: The sample rate of an audio encoder's audio data
  251. ---------------------
  252. .. function:: size_t obs_encoder_get_frame_size(const obs_encoder_t *encoder)
  253. :return: The frame size of the audio packet
  254. ---------------------
  255. .. function:: void obs_encoder_set_preferred_video_format(obs_encoder_t *encoder, enum video_format format)
  256. enum video_format obs_encoder_get_preferred_video_format(const obs_encoder_t *encoder)
  257. Sets the preferred video format for a video encoder. If the encoder can use
  258. the format specified, it will force a conversion to that format if the
  259. obs output format does not match the preferred format.
  260. If the format is set to VIDEO_FORMAT_NONE, will revert to the default
  261. functionality of converting only when absolutely necessary.
  262. ---------------------
  263. .. function:: obs_data_t *obs_encoder_defaults(const char *id)
  264. obs_data_t *obs_encoder_get_defaults(const obs_encoder_t *encoder)
  265. :return: An incremented reference to the encoder's default settings.
  266. Release with :c:func:`obs_data_release()`.
  267. ---------------------
  268. .. function:: obs_properties_t *obs_encoder_properties(const obs_encoder_t *encoder)
  269. obs_properties_t *obs_get_encoder_properties(const char *id)
  270. Use these functions to get the properties of an encoder or encoder
  271. type. Properties are optionally used (if desired) to automatically
  272. generate user interface widgets to allow users to update settings.
  273. :return: The properties list for a specific existing encoder. Free
  274. with :c:func:`obs_properties_destroy()`
  275. ---------------------
  276. .. function:: void obs_encoder_update(obs_encoder_t *encoder, obs_data_t *settings)
  277. Updates the settings for this encoder context.
  278. ---------------------
  279. .. function:: obs_data_t *obs_encoder_get_settings(const obs_encoder_t *encoder)
  280. :return: An incremented reference to the encoder's settings. Release with
  281. :c:func:`obs_data_release()`.
  282. ---------------------
  283. .. function:: signal_handler_t *obs_encoder_get_signal_handler(const obs_encoder_t *encoder)
  284. :return: The signal handler of the encoder. Should not be manually freed,
  285. as its lifecycle is managed by libobs.
  286. ---------------------
  287. .. function:: proc_handler_t *obs_encoder_get_proc_handler(const obs_encoder_t *encoder)
  288. :return: The procedure handler of the encoder. Should not be manually freed,
  289. as its lifecycle is managed by libobs.
  290. ---------------------
  291. .. function:: bool obs_encoder_get_extra_data(const obs_encoder_t *encoder, uint8_t **extra_data, size_t *size)
  292. Gets extra data (headers) associated with this encoder.
  293. :return: *true* if successful, *false* if no extra data associated
  294. with this encoder
  295. ---------------------
  296. .. function:: void obs_encoder_set_video(obs_encoder_t *encoder, video_t *video)
  297. void obs_encoder_set_audio(obs_encoder_t *encoder, audio_t *audio)
  298. Sets the video/audio handler to use with this video/audio encoder.
  299. This is used to capture the raw video/audio data.
  300. ---------------------
  301. .. function:: video_t *obs_encoder_video(const obs_encoder_t *encoder)
  302. audio_t *obs_encoder_audio(const obs_encoder_t *encoder)
  303. :return: The video/audio handler associated with this encoder, or
  304. *NULL* if none or not a matching encoder type
  305. ---------------------
  306. .. function:: bool obs_encoder_active(const obs_encoder_t *encoder)
  307. :return: *true* if the encoder is active, *false* otherwise
  308. ---------------------
  309. Functions used by encoders
  310. --------------------------
  311. .. function:: void obs_encoder_packet_ref(struct encoder_packet *dst, struct encoder_packet *src)
  312. void obs_encoder_packet_release(struct encoder_packet *packet)
  313. Adds or releases a reference to an encoder packet.
  314. .. ---------------------------------------------------------------------------
  315. .. _libobs/obs-encoder.h: https://github.com/obsproject/obs-studio/blob/master/libobs/obs-encoder.h