obs-encoder.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. /******************************************************************************
  2. Copyright (C) 2023 by Lain Bailey <lain@obsproject.com>
  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_packet_time is used for timestamping events associated
  39. * with each video frame. This is useful for deriving absolute
  40. * timestamps (i.e. wall-clock based formats) and measuring latency.
  41. *
  42. * For each frame, there are four events of interest, described in
  43. * the encoder_packet_time struct, namely cts, fer, ferc, and pir.
  44. * The timebase of these four events is os_gettime_ns(), which provides
  45. * very high resolution timestamping, and the ability to convert the
  46. * timing to any other time format.
  47. *
  48. * Each frame follows a timeline in the following temporal order:
  49. * CTS, FER, FERC, PIR
  50. *
  51. * PTS is the integer-based monotonically increasing value that is used
  52. * to associate an encoder_packet_time entry with a specific encoder_packet.
  53. */
  54. struct encoder_packet_time {
  55. /* PTS used to associate uncompressed frames with encoded packets. */
  56. int64_t pts;
  57. /* Composition timestamp is when the frame was rendered,
  58. * captured via os_gettime_ns().
  59. */
  60. uint64_t cts;
  61. /* FERC (Frame Encode Request) is when the frame was
  62. * submitted to the encoder for encoding via the encode
  63. * callback (e.g. encode_texture2()), captured via os_gettime_ns().
  64. */
  65. uint64_t fer;
  66. /* FERC (Frame Encode Request Complete) is when
  67. * the associated FER event completed. If the encode
  68. * is synchronous with the call, this means FERC - FEC
  69. * measures the actual encode time, otherwise if the
  70. * encode is asynchronous, it measures the pipeline
  71. * delay between encode request and encode complete.
  72. * FERC is also captured via os_gettime_ns().
  73. */
  74. uint64_t ferc;
  75. /* PIR (Packet Interleave Request) is when the encoded packet
  76. * is interleaved with the stream. PIR is captured via
  77. * os_gettime_ns(). The difference between PIR and CTS gives
  78. * the total latency between frame rendering
  79. * and packet interleaving.
  80. */
  81. uint64_t pir;
  82. };
  83. /** Encoder output packet */
  84. struct encoder_packet {
  85. uint8_t *data; /**< Packet data */
  86. size_t size; /**< Packet size */
  87. int64_t pts; /**< Presentation timestamp */
  88. int64_t dts; /**< Decode timestamp */
  89. int32_t timebase_num; /**< Timebase numerator */
  90. int32_t timebase_den; /**< Timebase denominator */
  91. enum obs_encoder_type type; /**< Encoder type */
  92. bool keyframe; /**< Is a keyframe */
  93. /* ---------------------------------------------------------------- */
  94. /* Internal video variables (will be parsed automatically) */
  95. /* DTS in microseconds */
  96. int64_t dts_usec;
  97. /* System DTS in microseconds */
  98. int64_t sys_dts_usec;
  99. /**
  100. * Packet priority
  101. *
  102. * This is generally use by video encoders to specify the priority
  103. * of the packet.
  104. */
  105. int priority;
  106. /**
  107. * Dropped packet priority
  108. *
  109. * If this packet needs to be dropped, the next packet must be of this
  110. * priority or higher to continue transmission.
  111. */
  112. int drop_priority;
  113. /** Audio track index (used with outputs) */
  114. size_t track_idx;
  115. /** Encoder from which the track originated from */
  116. obs_encoder_t *encoder;
  117. };
  118. /** Encoder input frame */
  119. struct encoder_frame {
  120. /** Data for the frame/audio */
  121. uint8_t *data[MAX_AV_PLANES];
  122. /** size of each plane */
  123. uint32_t linesize[MAX_AV_PLANES];
  124. /** Number of frames (audio only) */
  125. uint32_t frames;
  126. /** Presentation timestamp */
  127. int64_t pts;
  128. };
  129. /** Encoder region of interest */
  130. struct obs_encoder_roi {
  131. /* The rectangle edges of the region are specified as number of pixels
  132. * from the input video's top and left edges (i.e. row/column 0). */
  133. uint32_t top;
  134. uint32_t bottom;
  135. uint32_t left;
  136. uint32_t right;
  137. /* Priority is specified as a float value between -1 and 1.
  138. * These are converted to encoder-specific values by the encoder.
  139. * Values above 0 tell the encoder to increase quality for that region,
  140. * values below tell it to worsen it.
  141. * Not all encoders support negative values and they may be ignored. */
  142. float priority;
  143. };
  144. struct gs_texture;
  145. /** Encoder input texture */
  146. struct encoder_texture {
  147. /** Shared texture handle, only set on Windows */
  148. uint32_t handle;
  149. /** Textures, length determined by format */
  150. struct gs_texture *tex[4];
  151. };
  152. /**
  153. * Encoder interface
  154. *
  155. * Encoders have a limited usage with OBS. You are not generally supposed to
  156. * implement every encoder out there. Generally, these are limited or specific
  157. * encoders for h264/aac for streaming and recording. It doesn't have to be
  158. * *just* h264 or aac of course, but generally those are the expected encoders.
  159. *
  160. * That being said, other encoders will be kept in mind for future use.
  161. */
  162. struct obs_encoder_info {
  163. /* ----------------------------------------------------------------- */
  164. /* Required implementation*/
  165. /** Specifies the named identifier of this encoder */
  166. const char *id;
  167. /** Specifies the encoder type (video or audio) */
  168. enum obs_encoder_type type;
  169. /** Specifies the codec */
  170. const char *codec;
  171. /**
  172. * Gets the full translated name of this encoder
  173. *
  174. * @param type_data The type_data variable of this structure
  175. * @return Translated name of the encoder
  176. */
  177. const char *(*get_name)(void *type_data);
  178. /**
  179. * Creates the encoder with the specified settings
  180. *
  181. * @param settings Settings for the encoder
  182. * @param encoder OBS encoder context
  183. * @return Data associated with this encoder context, or
  184. * NULL if initialization failed.
  185. */
  186. void *(*create)(obs_data_t *settings, obs_encoder_t *encoder);
  187. /**
  188. * Destroys the encoder data
  189. *
  190. * @param data Data associated with this encoder context
  191. */
  192. void (*destroy)(void *data);
  193. /**
  194. * Encodes frame(s), and outputs encoded packets as they become
  195. * available.
  196. *
  197. * @param data Data associated with this encoder
  198. * context
  199. * @param[in] frame Raw audio/video data to encode
  200. * @param[out] packet Encoder packet output, if any
  201. * @param[out] received_packet Set to true if a packet was received,
  202. * false otherwise
  203. * @return true if successful, false otherwise.
  204. */
  205. bool (*encode)(void *data, struct encoder_frame *frame, struct encoder_packet *packet, bool *received_packet);
  206. /** Audio encoder only: Returns the frame size for this encoder */
  207. size_t (*get_frame_size)(void *data);
  208. /* ----------------------------------------------------------------- */
  209. /* Optional implementation */
  210. /**
  211. * Gets the default settings for this encoder
  212. *
  213. * @param[out] settings Data to assign default settings to
  214. */
  215. void (*get_defaults)(obs_data_t *settings);
  216. /**
  217. * Gets the property information of this encoder
  218. *
  219. * @return The properties data
  220. */
  221. obs_properties_t *(*get_properties)(void *data);
  222. /**
  223. * Updates the settings for this encoder (usually used for things like
  224. * changing bitrate while active)
  225. *
  226. * @param data Data associated with this encoder context
  227. * @param settings New settings for this encoder
  228. * @return true if successful, false otherwise
  229. */
  230. bool (*update)(void *data, obs_data_t *settings);
  231. /**
  232. * Returns extra data associated with this encoder (usually header)
  233. *
  234. * @param data Data associated with this encoder context
  235. * @param[out] extra_data Pointer to receive the extra data
  236. * @param[out] size Pointer to receive the size of the extra
  237. * data
  238. * @return true if extra data available, false
  239. * otherwise
  240. */
  241. bool (*get_extra_data)(void *data, uint8_t **extra_data, size_t *size);
  242. /**
  243. * Gets the SEI data, if any
  244. *
  245. * @param data Data associated with this encoder context
  246. * @param[out] sei_data Pointer to receive the SEI data
  247. * @param[out] size Pointer to receive the SEI data size
  248. * @return true if SEI data available, false otherwise
  249. */
  250. bool (*get_sei_data)(void *data, uint8_t **sei_data, size_t *size);
  251. /**
  252. * Returns desired audio format and sample information
  253. *
  254. * @param data Data associated with this encoder context
  255. * @param[in/out] info Audio format information
  256. */
  257. void (*get_audio_info)(void *data, struct audio_convert_info *info);
  258. /**
  259. * Returns desired video format information
  260. *
  261. * @param data Data associated with this encoder context
  262. * @param[in/out] info Video format information
  263. */
  264. void (*get_video_info)(void *data, struct video_scale_info *info);
  265. void *type_data;
  266. void (*free_type_data)(void *type_data);
  267. uint32_t caps;
  268. /**
  269. * Gets the default settings for this encoder
  270. *
  271. * If get_defaults is also defined both will be called, and the first
  272. * call will be to get_defaults, then to get_defaults2.
  273. *
  274. * @param[out] settings Data to assign default settings to
  275. * @param[in] typedata Type Data
  276. */
  277. void (*get_defaults2)(obs_data_t *settings, void *type_data);
  278. /**
  279. * Gets the property information of this encoder
  280. *
  281. * @param[in] data Pointer from create (or null)
  282. * @param[in] typedata Type Data
  283. * @return The properties data
  284. */
  285. obs_properties_t *(*get_properties2)(void *data, void *type_data);
  286. bool (*encode_texture)(void *data, uint32_t handle, int64_t pts, uint64_t lock_key, uint64_t *next_key,
  287. struct encoder_packet *packet, bool *received_packet);
  288. bool (*encode_texture2)(void *data, struct encoder_texture *texture, int64_t pts, uint64_t lock_key,
  289. uint64_t *next_key, struct encoder_packet *packet, bool *received_packet);
  290. };
  291. EXPORT void obs_register_encoder_s(const struct obs_encoder_info *info, size_t size);
  292. /**
  293. * Register an encoder definition to the current obs context. This should be
  294. * used in obs_module_load.
  295. *
  296. * @param info Pointer to the source definition structure.
  297. */
  298. #define obs_register_encoder(info) obs_register_encoder_s(info, sizeof(struct obs_encoder_info))
  299. #ifdef __cplusplus
  300. }
  301. #endif