win-dshow-encoder.cpp 9.4 KB

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