obs-encoder.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. * @param locale Locale to use for translation
  83. * @return Translated name of the encoder
  84. */
  85. const char *(*getname)(const char *locale);
  86. /**
  87. * Creates the encoder with the specified settings
  88. *
  89. * @param settings Settings for the encoder
  90. * @param encoder OBS encoder context
  91. * @return Data associated with this encoder context, or
  92. * NULL if initialization failed.
  93. */
  94. void *(*create)(obs_data_t settings, obs_encoder_t encoder);
  95. /**
  96. * Destroys the encoder data
  97. *
  98. * @param data Data associated with this encoder context
  99. */
  100. void (*destroy)(void *data);
  101. /**
  102. * Encodes frame(s), and outputs encoded packets as they become
  103. * available.
  104. *
  105. * @param data Data associated with this encoder
  106. * context
  107. * @param[in] frame Raw audio/video data to encode
  108. * @param[out] packet Encoder packet output, if any
  109. * @param[out] received_packet Set to true if a packet was received,
  110. * false otherwise
  111. * @return true if successful, false otherwise.
  112. */
  113. bool (*encode)(void *data, struct encoder_frame *frame,
  114. struct encoder_packet *packet, bool *received_packet);
  115. /** Audio encoder only: Returns the frame size for this encoder */
  116. size_t (*frame_size)(void *data);
  117. /* ----------------------------------------------------------------- */
  118. /* Optional implementation */
  119. /**
  120. * Gets the default settings for this encoder
  121. *
  122. * @param[out] settings Data to assign default settings to
  123. */
  124. void (*defaults)(obs_data_t settings);
  125. /**
  126. * Gets the property information of this encoder
  127. *
  128. * @param locale The locale to translate with
  129. * @return The properties data
  130. */
  131. obs_properties_t (*properties)(const char *locale);
  132. /**
  133. * Updates the settings for this encoder (usually used for things like
  134. * changeing birate while active)
  135. *
  136. * @param data Data associated with this encoder context
  137. * @param settings New settings for this encoder
  138. * @return true if successful, false otherwise
  139. */
  140. bool (*update)(void *data, obs_data_t settings);
  141. /**
  142. * Returns extra data associated with this encoder (usually header)
  143. *
  144. * @param data Data associated with this encoder context
  145. * @param[out] extra_data Pointer to receive the extra data
  146. * @param[out] size Pointer to receive the size of the extra
  147. * data
  148. * @return true if extra data available, false
  149. * otherwise
  150. */
  151. bool (*extra_data)(void *data, uint8_t **extra_data, size_t *size);
  152. /**
  153. * Gets the SEI data, if any
  154. *
  155. * @param data Data associated with this encoder context
  156. * @param[out] sei_data Pointer to receive the SEI data
  157. * @param[out] size Pointer to receive the SEI data size
  158. * @return true if SEI data available, false otherwise
  159. */
  160. bool (*sei_data)(void *data, uint8_t **sei_data, size_t *size);
  161. /**
  162. * Returns desired audio format and sample information
  163. *
  164. * @param data Data associated with this encoder context
  165. * @param[out] info Audio format information
  166. * @return true if specific format is desired, false
  167. * otherwise
  168. */
  169. bool (*audio_info)(void *data, struct audio_convert_info *info);
  170. /**
  171. * Returns desired video format information
  172. *
  173. * @param data Data associated with this encoder context
  174. * @param[out] info Video format information
  175. * @return true if specific format is desired, false
  176. * otherwise
  177. */
  178. bool (*video_info)(void *data, struct video_scale_info *info);
  179. };
  180. EXPORT void obs_register_encoder_s(const struct obs_encoder_info *info,
  181. size_t size);
  182. /**
  183. * Register an encoder definition to the current obs context. This should be
  184. * used in obs_module_load.
  185. *
  186. * @param info Pointer to the source definition structure.
  187. */
  188. #define obs_register_encoder(info) \
  189. obs_register_encoder_s(info, sizeof(struct obs_encoder_info))