obs-encoder.h 7.1 KB

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