obs-encoder.h 7.6 KB

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