win-dshow-encoder.cpp 9.2 KB

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