reference-encoders.rst 17 KB

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