obs-encoder.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /******************************************************************************
  2. Copyright (C) 2013-2014 by Hugh 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. #define OBS_ENCODER_CAP_DEPRECATED (1 << 0)
  26. #define OBS_ENCODER_CAP_PASS_TEXTURE (1 << 1)
  27. #define OBS_ENCODER_CAP_DYN_BITRATE (1 << 2)
  28. /** Specifies the encoder type */
  29. enum obs_encoder_type {
  30. OBS_ENCODER_AUDIO, /**< The encoder provides an audio codec */
  31. OBS_ENCODER_VIDEO /**< The encoder provides a video codec */
  32. };
  33. /** Encoder output packet */
  34. struct encoder_packet {
  35. uint8_t *data; /**< Packet data */
  36. size_t size; /**< Packet size */
  37. int64_t pts; /**< Presentation timestamp */
  38. int64_t dts; /**< Decode timestamp */
  39. int32_t timebase_num; /**< Timebase numerator */
  40. int32_t timebase_den; /**< Timebase denominator */
  41. enum obs_encoder_type type; /**< Encoder type */
  42. bool keyframe; /**< Is a keyframe */
  43. /* ---------------------------------------------------------------- */
  44. /* Internal video variables (will be parsed automatically) */
  45. /* DTS in microseconds */
  46. int64_t dts_usec;
  47. /* System DTS in microseconds */
  48. int64_t sys_dts_usec;
  49. /**
  50. * Packet priority
  51. *
  52. * This is generally use by video encoders to specify the priority
  53. * of the packet.
  54. */
  55. int priority;
  56. /**
  57. * Dropped packet priority
  58. *
  59. * If this packet needs to be dropped, the next packet must be of this
  60. * priority or higher to continue transmission.
  61. */
  62. int drop_priority;
  63. /** Audio track index (used with outputs) */
  64. size_t track_idx;
  65. /** Encoder from which the track originated from */
  66. obs_encoder_t *encoder;
  67. };
  68. /** Encoder input frame */
  69. struct encoder_frame {
  70. /** Data for the frame/audio */
  71. uint8_t *data[MAX_AV_PLANES];
  72. /** size of each plane */
  73. uint32_t linesize[MAX_AV_PLANES];
  74. /** Number of frames (audio only) */
  75. uint32_t frames;
  76. /** Presentation timestamp */
  77. int64_t pts;
  78. };
  79. /**
  80. * Encoder interface
  81. *
  82. * Encoders have a limited usage with OBS. You are not generally supposed to
  83. * implement every encoder out there. Generally, these are limited or specific
  84. * encoders for h264/aac for streaming and recording. It doesn't have to be
  85. * *just* h264 or aac of course, but generally those are the expected encoders.
  86. *
  87. * That being said, other encoders will be kept in mind for future use.
  88. */
  89. struct obs_encoder_info {
  90. /* ----------------------------------------------------------------- */
  91. /* Required implementation*/
  92. /** Specifies the named identifier of this encoder */
  93. const char *id;
  94. /** Specifies the encoder type (video or audio) */
  95. enum obs_encoder_type type;
  96. /** Specifies the codec */
  97. const char *codec;
  98. /**
  99. * Gets the full translated name of this encoder
  100. *
  101. * @param type_data The type_data variable of this structure
  102. * @return Translated name of the encoder
  103. */
  104. const char *(*get_name)(void *type_data);
  105. /**
  106. * Creates the encoder with the specified settings
  107. *
  108. * @param settings Settings for the encoder
  109. * @param encoder OBS encoder context
  110. * @return Data associated with this encoder context, or
  111. * NULL if initialization failed.
  112. */
  113. void *(*create)(obs_data_t *settings, obs_encoder_t *encoder);
  114. /**
  115. * Destroys the encoder data
  116. *
  117. * @param data Data associated with this encoder context
  118. */
  119. void (*destroy)(void *data);
  120. /**
  121. * Encodes frame(s), and outputs encoded packets as they become
  122. * available.
  123. *
  124. * @param data Data associated with this encoder
  125. * context
  126. * @param[in] frame Raw audio/video data to encode
  127. * @param[out] packet Encoder packet output, if any
  128. * @param[out] received_packet Set to true if a packet was received,
  129. * false otherwise
  130. * @return true if successful, false otherwise.
  131. */
  132. bool (*encode)(void *data, struct encoder_frame *frame,
  133. struct encoder_packet *packet, bool *received_packet);
  134. /** Audio encoder only: Returns the frame size for this encoder */
  135. size_t (*get_frame_size)(void *data);
  136. /* ----------------------------------------------------------------- */
  137. /* Optional implementation */
  138. /**
  139. * Gets the default settings for this encoder
  140. *
  141. * @param[out] settings Data to assign default settings to
  142. */
  143. void (*get_defaults)(obs_data_t *settings);
  144. /**
  145. * Gets the property information of this encoder
  146. *
  147. * @return The properties data
  148. */
  149. obs_properties_t *(*get_properties)(void *data);
  150. /**
  151. * Updates the settings for this encoder (usually used for things like
  152. * changing bitrate while active)
  153. *
  154. * @param data Data associated with this encoder context
  155. * @param settings New settings for this encoder
  156. * @return true if successful, false otherwise
  157. */
  158. bool (*update)(void *data, obs_data_t *settings);
  159. /**
  160. * Returns extra data associated with this encoder (usually header)
  161. *
  162. * @param data Data associated with this encoder context
  163. * @param[out] extra_data Pointer to receive the extra data
  164. * @param[out] size Pointer to receive the size of the extra
  165. * data
  166. * @return true if extra data available, false
  167. * otherwise
  168. */
  169. bool (*get_extra_data)(void *data, uint8_t **extra_data, size_t *size);
  170. /**
  171. * Gets the SEI data, if any
  172. *
  173. * @param data Data associated with this encoder context
  174. * @param[out] sei_data Pointer to receive the SEI data
  175. * @param[out] size Pointer to receive the SEI data size
  176. * @return true if SEI data available, false otherwise
  177. */
  178. bool (*get_sei_data)(void *data, uint8_t **sei_data, size_t *size);
  179. /**
  180. * Returns desired audio format and sample information
  181. *
  182. * @param data Data associated with this encoder context
  183. * @param[in/out] info Audio format information
  184. */
  185. void (*get_audio_info)(void *data, struct audio_convert_info *info);
  186. /**
  187. * Returns desired video format information
  188. *
  189. * @param data Data associated with this encoder context
  190. * @param[in/out] info Video format information
  191. */
  192. void (*get_video_info)(void *data, struct video_scale_info *info);
  193. void *type_data;
  194. void (*free_type_data)(void *type_data);
  195. uint32_t caps;
  196. /**
  197. * Gets the default settings for this encoder
  198. *
  199. * If get_defaults is also defined both will be called, and the first
  200. * call will be to get_defaults, then to get_defaults2.
  201. *
  202. * @param[out] settings Data to assign default settings to
  203. * @param[in] typedata Type Data
  204. */
  205. void (*get_defaults2)(obs_data_t *settings, void *type_data);
  206. /**
  207. * Gets the property information of this encoder
  208. *
  209. * @param[in] data Pointer from create (or null)
  210. * @param[in] typedata Type Data
  211. * @return The properties data
  212. */
  213. obs_properties_t *(*get_properties2)(void *data, void *type_data);
  214. bool (*encode_texture)(void *data, uint32_t handle, int64_t pts,
  215. uint64_t lock_key, uint64_t *next_key,
  216. struct encoder_packet *packet,
  217. bool *received_packet);
  218. };
  219. EXPORT void obs_register_encoder_s(const struct obs_encoder_info *info,
  220. size_t size);
  221. /**
  222. * Register an encoder definition to the current obs context. This should be
  223. * used in obs_module_load.
  224. *
  225. * @param info Pointer to the source definition structure.
  226. */
  227. #define obs_register_encoder(info) \
  228. obs_register_encoder_s(info, sizeof(struct obs_encoder_info))
  229. #ifdef __cplusplus
  230. }
  231. #endif