win-dshow-encoder.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. #include <obs-module.h>
  2. #include <obs-avc.h>
  3. #include <util/darray.h>
  4. #include <util/dstr.hpp>
  5. #ifdef OBS_LEGACY
  6. #include "libdshowcapture/dshowcapture.hpp"
  7. #else
  8. #include <dshowcapture.hpp>
  9. #endif
  10. using namespace DShow;
  11. using namespace std;
  12. struct DShowEncoder {
  13. obs_encoder_t *context;
  14. VideoEncoder encoder;
  15. VideoEncoderConfig config;
  16. const wchar_t *device;
  17. video_format format;
  18. long long frameInterval;
  19. bool first = true;
  20. DARRAY(uint8_t) firstPacket;
  21. DARRAY(uint8_t) header;
  22. inline DShowEncoder(obs_encoder_t *context_, const wchar_t *device_) : context(context_), device(device_)
  23. {
  24. da_init(firstPacket);
  25. da_init(header);
  26. }
  27. inline ~DShowEncoder()
  28. {
  29. da_free(firstPacket);
  30. da_free(header);
  31. }
  32. inline void ParseFirstPacket(const uint8_t *data, size_t size);
  33. inline bool Update(obs_data_t *settings);
  34. inline bool Encode(struct encoder_frame *frame, struct encoder_packet *packet, bool *received_packet);
  35. };
  36. static const char *GetC985EncoderName(void *)
  37. {
  38. return obs_module_text("Encoder.C985");
  39. }
  40. static const char *GetC353EncoderName(void *)
  41. {
  42. return obs_module_text("Encoder.C353");
  43. }
  44. static inline void FindDevice(DeviceId &id, const wchar_t *name)
  45. {
  46. vector<DeviceId> devices;
  47. DShow::VideoEncoder::EnumEncoders(devices);
  48. for (const DeviceId &device : devices) {
  49. if (device.name.find(name) != string::npos) {
  50. id = device;
  51. break;
  52. }
  53. }
  54. }
  55. /*
  56. * As far as I can tell the devices only seem to support 1024x768 and 1280x720
  57. * resolutions, so I'm just going to cap it to those resolutions by aspect.
  58. *
  59. * XXX: This should really not be hard-coded. Problem is I don't know how to
  60. * properly query the encoding capabilities of the devices.
  61. */
  62. static const double standardAspect = 1024.0 / 768.0;
  63. static const double wideAspect = 1280.0 / 720.0;
  64. inline bool DShowEncoder::Update(obs_data_t *settings)
  65. {
  66. DStr deviceName;
  67. DeviceId id;
  68. FindDevice(id, device);
  69. video_t *video = obs_encoder_video(context);
  70. const struct video_output_info *voi = video_output_get_info(video);
  71. int bitrate = (int)obs_data_get_int(settings, "bitrate");
  72. int keyint_sec = (int)obs_data_get_int(settings, "keyint_sec");
  73. int width = (int)obs_encoder_get_width(context);
  74. int height = (int)obs_encoder_get_height(context);
  75. double aspect = double(width) / double(height);
  76. if (keyint_sec == 0)
  77. keyint_sec = 2;
  78. if (fabs(aspect - standardAspect) < fabs(aspect - wideAspect)) {
  79. width = 1024;
  80. height = 768;
  81. } else {
  82. width = 1280;
  83. height = 720;
  84. }
  85. int keyint = keyint_sec * voi->fps_num / voi->fps_den;
  86. frameInterval = voi->fps_den * 10000000 / voi->fps_num;
  87. config.fpsNumerator = voi->fps_num;
  88. config.fpsDenominator = voi->fps_den;
  89. config.bitrate = bitrate;
  90. config.keyframeInterval = keyint;
  91. config.cx = width;
  92. config.cy = height;
  93. config.name = id.name;
  94. config.path = id.path;
  95. first = true;
  96. da_resize(firstPacket, 0);
  97. da_resize(header, 0);
  98. dstr_from_wcs(deviceName, id.name.c_str());
  99. DStr encoder_name;
  100. dstr_from_wcs(encoder_name, config.name.c_str());
  101. blog(LOG_DEBUG,
  102. "win-dshow-encoder:\n"
  103. "\tencoder: %s\n"
  104. "\twidth: %d\n"
  105. "\theight: %d\n"
  106. "\tfps_num: %d\n"
  107. "\tfps_den: %d",
  108. deviceName->array, (int)width, (int)height, (int)voi->fps_num, (int)voi->fps_den);
  109. return encoder.SetConfig(config);
  110. }
  111. static bool UpdateDShowEncoder(void *data, obs_data_t *settings)
  112. {
  113. DShowEncoder *encoder = reinterpret_cast<DShowEncoder *>(data);
  114. if (!obs_encoder_active(encoder->context))
  115. return encoder->Update(settings);
  116. return true;
  117. }
  118. static inline void *CreateDShowEncoder(obs_data_t *settings, obs_encoder_t *context, const wchar_t *device)
  119. {
  120. DShowEncoder *encoder = nullptr;
  121. try {
  122. encoder = new DShowEncoder(context, device);
  123. UpdateDShowEncoder(encoder, settings);
  124. } catch (const char *error) {
  125. blog(LOG_ERROR, "Could not create DirectShow encoder '%s': %s", obs_encoder_get_name(context), error);
  126. }
  127. return encoder;
  128. }
  129. static void *CreateC985Encoder(obs_data_t *settings, obs_encoder_t *context)
  130. {
  131. return CreateDShowEncoder(settings, context, L"C985");
  132. }
  133. static void *CreateC353Encoder(obs_data_t *settings, obs_encoder_t *context)
  134. {
  135. return CreateDShowEncoder(settings, context, L"C353");
  136. }
  137. static void DestroyDShowEncoder(void *data)
  138. {
  139. delete reinterpret_cast<DShowEncoder *>(data);
  140. }
  141. /* the first packet contains the SPS/PPS (header) NALs, so parse the first
  142. * packet and separate the NALs */
  143. inline void DShowEncoder::ParseFirstPacket(const uint8_t *data, size_t size)
  144. {
  145. const uint8_t *nal_start, *nal_end, *nal_codestart;
  146. const uint8_t *end = data + size;
  147. int type;
  148. nal_start = obs_avc_find_startcode(data, end);
  149. nal_end = nullptr;
  150. while (nal_end != end) {
  151. nal_codestart = nal_start;
  152. while (nal_start < end && !*(nal_start++))
  153. ;
  154. if (nal_start == end)
  155. break;
  156. type = nal_start[0] & 0x1F;
  157. nal_end = obs_avc_find_startcode(nal_start, end);
  158. if (!nal_end)
  159. nal_end = end;
  160. if (type == OBS_NAL_SPS || type == OBS_NAL_PPS) {
  161. da_push_back_array(header, nal_codestart, nal_end - nal_codestart);
  162. } else {
  163. da_push_back_array(firstPacket, nal_codestart, nal_end - nal_codestart);
  164. }
  165. nal_start = nal_end;
  166. }
  167. }
  168. inline bool DShowEncoder::Encode(struct encoder_frame *frame, struct encoder_packet *packet, bool *received_packet)
  169. {
  170. unsigned char *frame_data[DSHOW_MAX_PLANES] = {};
  171. size_t frame_sizes[DSHOW_MAX_PLANES] = {};
  172. EncoderPacket dshowPacket;
  173. bool new_packet = false;
  174. /* The encoders expect YV12, so swap the chroma planes for encoding */
  175. if (format == VIDEO_FORMAT_I420) {
  176. frame_data[0] = frame->data[0];
  177. frame_data[1] = frame->data[2];
  178. frame_data[2] = frame->data[1];
  179. frame_sizes[0] = frame->linesize[0] * config.cy;
  180. frame_sizes[1] = frame->linesize[2] * config.cy / 2;
  181. frame_sizes[2] = frame->linesize[1] * config.cy / 2;
  182. }
  183. long long actualPTS = frame->pts * frameInterval;
  184. bool success =
  185. encoder.Encode(frame_data, frame_sizes, actualPTS, actualPTS + frameInterval, dshowPacket, new_packet);
  186. if (!success)
  187. return false;
  188. if (new_packet && !!dshowPacket.data && !!dshowPacket.size) {
  189. packet->data = dshowPacket.data;
  190. packet->size = dshowPacket.size;
  191. packet->type = OBS_ENCODER_VIDEO;
  192. packet->pts = dshowPacket.pts / frameInterval;
  193. packet->dts = dshowPacket.dts / frameInterval;
  194. packet->keyframe = obs_avc_keyframe(packet->data, packet->size);
  195. /* first packet must be parsed in order to retrieve header */
  196. if (first) {
  197. first = false;
  198. ParseFirstPacket(packet->data, packet->size);
  199. packet->data = firstPacket.array;
  200. packet->size = firstPacket.num;
  201. }
  202. *received_packet = true;
  203. }
  204. return true;
  205. }
  206. static bool DShowEncode(void *data, struct encoder_frame *frame, struct encoder_packet *packet, bool *received_packet)
  207. {
  208. return reinterpret_cast<DShowEncoder *>(data)->Encode(frame, packet, received_packet);
  209. }
  210. static bool GetDShowExtraData(void *data, uint8_t **extra_data, size_t *size)
  211. {
  212. DShowEncoder *encoder = reinterpret_cast<DShowEncoder *>(data);
  213. *extra_data = encoder->header.array;
  214. *size = encoder->header.num;
  215. return *size > 0;
  216. }
  217. static inline bool ValidResolution(uint32_t width, uint32_t height)
  218. {
  219. return (width == 1280 && height == 720) || (width == 1024 && height == 768);
  220. }
  221. static void GetDShowVideoInfo(void *data, struct video_scale_info *info)
  222. {
  223. DShowEncoder *encoder = reinterpret_cast<DShowEncoder *>(data);
  224. encoder->format = VIDEO_FORMAT_I420;
  225. if (info->format == VIDEO_FORMAT_I420 && ValidResolution(info->width, info->height))
  226. return;
  227. info->format = VIDEO_FORMAT_I420;
  228. info->width = info->width;
  229. info->height = info->height;
  230. info->range = VIDEO_RANGE_DEFAULT;
  231. info->colorspace = VIDEO_CS_DEFAULT;
  232. double aspect = double(info->width) / double(info->height);
  233. if (fabs(aspect - standardAspect) < fabs(aspect - wideAspect)) {
  234. info->width = 1024;
  235. info->height = 768;
  236. } else {
  237. info->width = 1280;
  238. info->height = 720;
  239. }
  240. }
  241. static void GetDShowEncoderDefauts(obs_data_t *settings)
  242. {
  243. obs_data_set_default_int(settings, "bitrate", 1000);
  244. }
  245. static obs_properties_t *GetDShowEncoderProperties(void *data)
  246. {
  247. obs_properties_t *ppts = obs_properties_create();
  248. obs_properties_add_int(ppts, "bitrate", obs_module_text("Bitrate"), 1000, 60000, 1);
  249. UNUSED_PARAMETER(data);
  250. return ppts;
  251. }
  252. void RegisterDShowEncoders()
  253. {
  254. obs_encoder_info info = {};
  255. info.type = OBS_ENCODER_VIDEO;
  256. info.codec = "h264";
  257. info.destroy = DestroyDShowEncoder;
  258. info.encode = DShowEncode;
  259. info.update = UpdateDShowEncoder;
  260. info.get_defaults = GetDShowEncoderDefauts;
  261. info.get_properties = GetDShowEncoderProperties;
  262. info.get_extra_data = GetDShowExtraData;
  263. info.get_video_info = GetDShowVideoInfo;
  264. vector<DeviceId> devices;
  265. DShow::VideoEncoder::EnumEncoders(devices);
  266. bool foundC985 = false;
  267. for (const DeviceId &device : devices) {
  268. if (!foundC985 && device.name.find(L"C985") != string::npos) {
  269. info.id = "dshow_c985_h264";
  270. info.get_name = GetC985EncoderName;
  271. info.create = CreateC985Encoder;
  272. obs_register_encoder(&info);
  273. foundC985 = true;
  274. } else if (device.name.find(L"C353") != string::npos) {
  275. info.id = "dshow_c353_h264";
  276. info.get_name = GetC353EncoderName;
  277. info.create = CreateC353Encoder;
  278. obs_register_encoder(&info);
  279. }
  280. }
  281. }