1
0

reference-encoders.rst 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  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. - **OBS_ENCODER_CAP_SCALING** - Encoder implements its own scaling logic,
  105. desiring to receive unscaled frames
  106. Encoder Packet Structure (encoder_packet)
  107. -----------------------------------------
  108. .. struct:: encoder_packet
  109. Encoder packet structure.
  110. .. member:: uint8_t *encoder_packet.data
  111. Packet data.
  112. .. member:: size_t encoder_packet.size
  113. Packet size.
  114. .. member:: int64_t encoder_packet.pts
  115. int64_t encoder_packet.dts
  116. Packet presentation and decode timestamps.
  117. .. member:: int32_t encoder_packet.timebase_num
  118. int32_t encoder_packet.timebase_den
  119. Packet time base.
  120. .. member:: enum obs_encoder_type encoder_packet.type
  121. Can be one of the following values:
  122. - **OBS_ENCODER_VIDEO** - Video data
  123. - **OBS_ENCODER_AUDIO** - Audio data
  124. .. member:: bool encoder_packet.keyframe
  125. Packet is a keyframe.
  126. .. member:: int64_t encoder_packet.dts_usec
  127. The DTS in microseconds.
  128. (This should not be set by the encoder implementation)
  129. .. member:: int64_t encoder_packet.sys_dts_usec
  130. The system time of this packet in microseconds.
  131. (This should not be set by the encoder implementation)
  132. .. member:: int encoder_packet.priority
  133. Packet priority. This is no longer used.
  134. (This should not be set by the encoder implementation)
  135. .. member:: int encoder_packet.drop_priority
  136. Packet drop priority.
  137. If this packet needs to be dropped, the next packet must be of this
  138. priority or higher to continue transmission.
  139. (This should not be set by the encoder implementation)
  140. .. member:: size_t encoder_packet.track_idx
  141. Audio track index.
  142. (This should not be set by the encoder implementation)
  143. .. member:: obs_encoder_t *encoder_packet.encoder
  144. Encoder object associated with this packet.
  145. (This should not be set by the encoder implementation)
  146. Raw Frame Data Structure (encoder_frame)
  147. ----------------------------------------
  148. .. struct:: encoder_frame
  149. Raw frame data structure.
  150. .. member:: uint8_t *encoder_frame.data[MAX_AV_PLANES]
  151. Raw video/audio data.
  152. .. member:: uint32_t encoder_frame.linesize[MAX_AV_PLANES]
  153. Line size of each plane.
  154. .. member:: uint32_t encoder_frame.frames
  155. Number of audio frames (if audio).
  156. .. member:: int64_t encoder_frame.pts
  157. Presentation timestamp.
  158. Encoder Region of Interest Structure (obs_encoder_roi)
  159. ------------------------------------------------------
  160. .. struct:: obs_encoder_roi
  161. Encoder region of interest structure.
  162. .. versionadded:: 30.1
  163. .. member:: uint32_t top
  164. uint32_t bottom
  165. uint32_t left
  166. uint32_t right
  167. 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).
  168. .. member:: float priority
  169. Priority is specified as a float value between *-1.0f* and *1*.
  170. These are converted to encoder-specific values by the encoder.
  171. Values above 0 tell the encoder to increase quality for that region, values below tell it to worsen it.
  172. Not all encoders support negative values and they may be ignored.
  173. General Encoder Functions
  174. -------------------------
  175. .. function:: void obs_register_encoder(struct obs_encoder_info *info)
  176. Registers an encoder type. Typically used in
  177. :c:func:`obs_module_load()` or in the program's initialization phase.
  178. ---------------------
  179. .. function:: const char *obs_encoder_get_display_name(const char *id)
  180. Calls the :c:member:`obs_encoder_info.get_name` callback to get the
  181. translated display name of an encoder type.
  182. :param id: The encoder type string identifier
  183. :return: The translated display name of an encoder type
  184. ---------------------
  185. .. function:: obs_encoder_t *obs_video_encoder_create(const char *id, const char *name, obs_data_t *settings, obs_data_t *hotkey_data)
  186. Creates a video encoder with the specified settings.
  187. The "encoder" context is used for encoding video/audio data. Use
  188. obs_encoder_release to release it.
  189. :param id: The encoder type string identifier
  190. :param name: The desired name of the encoder. If this is
  191. not unique, it will be made to be unique
  192. :param settings: The settings for the encoder, or *NULL* if
  193. none
  194. :param hotkey_data: Saved hotkey data for the encoder, or *NULL*
  195. if none
  196. :return: A reference to the newly created encoder, or
  197. *NULL* if failed
  198. ---------------------
  199. .. 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)
  200. Creates an audio encoder with the specified settings.
  201. The "encoder" context is used for encoding video/audio data. Use
  202. :c:func:`obs_encoder_release()` to release it.
  203. :param id: The encoder type string identifier
  204. :param name: The desired name of the encoder. If this is
  205. not unique, it will be made to be unique
  206. :param settings: The settings for the encoder, or *NULL* if
  207. none
  208. :param mixer_idx: The audio mixer index this audio encoder
  209. will capture audio from
  210. :param hotkey_data: Saved hotkey data for the encoder, or *NULL*
  211. if none
  212. :return: A reference to the newly created encoder, or
  213. *NULL* if failed
  214. ---------------------
  215. .. function:: obs_encoder_t *obs_encoder_get_ref(obs_encoder_t *encoder)
  216. Returns an incremented reference if still valid, otherwise returns
  217. *NULL*. Release with :c:func:`obs_encoder_release()`.
  218. ---------------------
  219. .. function:: void obs_encoder_release(obs_encoder_t *encoder)
  220. Releases a reference to an encoder. When the last reference is released,
  221. the encoder is destroyed.
  222. ---------------------
  223. .. function:: obs_weak_encoder_t *obs_encoder_get_weak_encoder(obs_encoder_t *encoder)
  224. obs_encoder_t *obs_weak_encoder_get_encoder(obs_weak_encoder_t *weak)
  225. These functions are used to get a weak reference from a strong encoder
  226. reference, or a strong encoder reference from a weak reference. If
  227. the encoder is destroyed, *obs_weak_encoder_get_encoder* will return
  228. *NULL*.
  229. ---------------------
  230. .. function:: void obs_weak_encoder_addref(obs_weak_encoder_t *weak)
  231. void obs_weak_encoder_release(obs_weak_encoder_t *weak)
  232. Adds/releases a weak reference to an encoder.
  233. ---------------------
  234. .. function:: void obs_encoder_set_name(obs_encoder_t *encoder, const char *name)
  235. Sets the name of an encoder. If the encoder is not private and the
  236. name is not unique, it will automatically be given a unique name.
  237. ---------------------
  238. .. function:: const char *obs_encoder_get_name(const obs_encoder_t *encoder)
  239. :return: The name of the encoder
  240. ---------------------
  241. .. function:: const char *obs_encoder_get_codec(const obs_encoder_t *encoder)
  242. const char *obs_get_encoder_codec(const char *id)
  243. :return: The codec identifier of the encoder
  244. ---------------------
  245. .. function:: enum obs_encoder_type obs_encoder_get_type(const obs_encoder_t *encoder)
  246. enum obs_encoder_type obs_get_encoder_type(const char *id)
  247. :return: The encoder type: OBS_ENCODER_VIDEO or OBS_ENCODER_AUDIO
  248. ---------------------
  249. .. function:: void obs_encoder_set_scaled_size(obs_encoder_t *encoder, uint32_t width, uint32_t height)
  250. Sets the scaled resolution for a video encoder. Set width and height to 0
  251. to disable scaling. If the encoder is active, this function will trigger
  252. a warning, and do nothing.
  253. ---------------------
  254. .. function:: bool obs_encoder_scaling_enabled(const obs_encoder_t *encoder)
  255. :return: *true* if pre-encode (CPU) scaling enabled, *false*
  256. otherwise.
  257. ---------------------
  258. .. function:: uint32_t obs_encoder_get_width(const obs_encoder_t *encoder)
  259. uint32_t obs_encoder_get_height(const obs_encoder_t *encoder)
  260. :return: The width/height of a video encoder's encoded image
  261. ---------------------
  262. .. function:: uint32_t obs_encoder_get_sample_rate(const obs_encoder_t *encoder)
  263. :return: The sample rate of an audio encoder's audio data
  264. ---------------------
  265. .. function:: size_t obs_encoder_get_frame_size(const obs_encoder_t *encoder)
  266. :return: The frame size of the audio packet
  267. ---------------------
  268. .. function:: size_t obs_encoder_get_mixer_index(const obs_encoder_t *encoder)
  269. :return: The mixer index for the audio track which is encoded by the encoder
  270. ---------------------
  271. .. function:: void obs_encoder_set_preferred_video_format(obs_encoder_t *encoder, enum video_format format)
  272. enum video_format obs_encoder_get_preferred_video_format(const obs_encoder_t *encoder)
  273. Sets the preferred video format for a video encoder. If the encoder can use
  274. the format specified, it will force a conversion to that format if the
  275. obs output format does not match the preferred format.
  276. If the format is set to VIDEO_FORMAT_NONE, will revert to the default
  277. functionality of converting only when absolutely necessary.
  278. ---------------------
  279. .. function:: obs_data_t *obs_encoder_defaults(const char *id)
  280. obs_data_t *obs_encoder_get_defaults(const obs_encoder_t *encoder)
  281. :return: An incremented reference to the encoder's default settings.
  282. Release with :c:func:`obs_data_release()`.
  283. ---------------------
  284. .. function:: obs_properties_t *obs_encoder_properties(const obs_encoder_t *encoder)
  285. obs_properties_t *obs_get_encoder_properties(const char *id)
  286. Use these functions to get the properties of an encoder or encoder
  287. type. Properties are optionally used (if desired) to automatically
  288. generate user interface widgets to allow users to update settings.
  289. :return: The properties list for a specific existing encoder. Free
  290. with :c:func:`obs_properties_destroy()`
  291. ---------------------
  292. .. function:: void obs_encoder_update(obs_encoder_t *encoder, obs_data_t *settings)
  293. Updates the settings for this encoder context.
  294. ---------------------
  295. .. function:: obs_data_t *obs_encoder_get_settings(const obs_encoder_t *encoder)
  296. :return: An incremented reference to the encoder's settings. Release with
  297. :c:func:`obs_data_release()`.
  298. ---------------------
  299. .. function:: signal_handler_t *obs_encoder_get_signal_handler(const obs_encoder_t *encoder)
  300. :return: The signal handler of the encoder. Should not be manually freed,
  301. as its lifecycle is managed by libobs.
  302. ---------------------
  303. .. function:: proc_handler_t *obs_encoder_get_proc_handler(const obs_encoder_t *encoder)
  304. :return: The procedure handler of the encoder. Should not be manually freed,
  305. as its lifecycle is managed by libobs.
  306. ---------------------
  307. .. function:: bool obs_encoder_get_extra_data(const obs_encoder_t *encoder, uint8_t **extra_data, size_t *size)
  308. Gets extra data (headers) associated with this encoder.
  309. :return: *true* if successful, *false* if no extra data associated
  310. with this encoder
  311. ---------------------
  312. .. function:: void obs_encoder_set_video(obs_encoder_t *encoder, video_t *video)
  313. void obs_encoder_set_audio(obs_encoder_t *encoder, audio_t *audio)
  314. Sets the video/audio handler to use with this video/audio encoder.
  315. This is used to capture the raw video/audio data.
  316. ---------------------
  317. .. function:: video_t *obs_encoder_video(const obs_encoder_t *encoder)
  318. video_t *obs_encoder_parent_video(const obs_encoder_t *encoder)
  319. audio_t *obs_encoder_audio(const obs_encoder_t *encoder)
  320. :return: The video/audio handler associated with this encoder, or
  321. *NULL* if none or not a matching encoder type.
  322. *parent_video* returns the "original" video handler
  323. associated with this encoder, regardless of whether an FPS
  324. divisor is set.
  325. ---------------------
  326. .. function:: bool obs_encoder_active(const obs_encoder_t *encoder)
  327. :return: *true* if the encoder is active, *false* otherwise
  328. ---------------------
  329. .. function:: bool obs_encoder_add_roi(obs_encoder_t *encoder, const struct obs_encoder_roi *roi)
  330. Adds a new region of interest to the encoder if ROI feature is supported.
  331. :return: *true* if adding succeeded, *false* otherwise.
  332. .. versionadded:: 30.1
  333. ---------------------
  334. .. function:: bool obs_encoder_has_roi(obs_encoder_t *encoder)
  335. :return: *true* if encoder has ROI regions set, *false* otherwise.
  336. .. versionadded:: 30.1
  337. ---------------------
  338. .. function:: void obs_encoder_clear_roi(obs_encoder_t *encoder)
  339. Clear region of interest list, if any.
  340. .. versionadded:: 30.1
  341. ---------------------
  342. .. function:: void obs_encoder_enum_roi(obs_encoder_t *encoder, void (*enum_proc)(void *, struct obs_encoder_roi *), void *param)
  343. Enumerate currently configured ROIs by invoking callback for each entry, in reverse order of addition (i.e. most recent to oldest).
  344. **Note:** If the encoder has scaling enabled the struct passed to the callback will be scaled accordingly.
  345. .. versionadded:: 30.1
  346. ---------------------
  347. .. function:: uint32_t obs_encoder_get_roi_increment(const obs_encoder_t *encoder)
  348. Encoders shall refresh their ROI configuration if the increment value changes.
  349. :return: Increment/revision of ROI list
  350. .. versionadded:: 30.1
  351. ---------------------
  352. Functions used by encoders
  353. --------------------------
  354. .. function:: void obs_encoder_packet_ref(struct encoder_packet *dst, struct encoder_packet *src)
  355. void obs_encoder_packet_release(struct encoder_packet *packet)
  356. Adds or releases a reference to an encoder packet.
  357. .. ---------------------------------------------------------------------------
  358. .. _libobs/obs-encoder.h: https://github.com/obsproject/obs-studio/blob/master/libobs/obs-encoder.h