obs-encoder.h 9.5 KB

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