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. return reinterpret_cast<DShowEncoder*>(data)->Update(settings);
  116. }
  117. static inline void *CreateDShowEncoder(obs_data_t *settings,
  118. 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",
  126. obs_encoder_get_name(context), error);
  127. }
  128. UNUSED_PARAMETER(settings);
  129. return encoder;
  130. }
  131. static void *CreateC985Encoder(obs_data_t *settings, obs_encoder_t *context)
  132. {
  133. return CreateDShowEncoder(settings, context, L"C985");
  134. }
  135. static void *CreateC353Encoder(obs_data_t *settings, obs_encoder_t *context)
  136. {
  137. return CreateDShowEncoder(settings, context, L"C353");
  138. }
  139. static void DestroyDShowEncoder(void *data)
  140. {
  141. delete reinterpret_cast<DShowEncoder*>(data);
  142. }
  143. /* the first packet contains the SPS/PPS (header) NALs, so parse the first
  144. * packet and separate the NALs */
  145. inline void DShowEncoder::ParseFirstPacket(const uint8_t *data, size_t size)
  146. {
  147. const uint8_t *nal_start, *nal_end, *nal_codestart;
  148. const uint8_t *end = data + size;
  149. int type;
  150. nal_start = obs_avc_find_startcode(data, end);
  151. nal_end = nullptr;
  152. while (nal_end != end) {
  153. nal_codestart = nal_start;
  154. while (nal_start < end && !*(nal_start++));
  155. if (nal_start == end)
  156. break;
  157. type = nal_start[0] & 0x1F;
  158. nal_end = obs_avc_find_startcode(nal_start, end);
  159. if (!nal_end)
  160. nal_end = end;
  161. if (type == OBS_NAL_SPS || type == OBS_NAL_PPS) {
  162. da_push_back_array(header, nal_codestart,
  163. nal_end - nal_codestart);
  164. } else {
  165. da_push_back_array(firstPacket, nal_codestart,
  166. nal_end - nal_codestart);
  167. }
  168. nal_start = nal_end;
  169. }
  170. }
  171. inline bool DShowEncoder::Encode(struct encoder_frame *frame,
  172. struct encoder_packet *packet, bool *received_packet)
  173. {
  174. unsigned char *frame_data[DSHOW_MAX_PLANES] = {};
  175. size_t frame_sizes[DSHOW_MAX_PLANES] = {};
  176. EncoderPacket dshowPacket;
  177. bool new_packet = false;
  178. /* The encoders expect YV12, so swap the chroma planes for encoding */
  179. if (format == VIDEO_FORMAT_I420) {
  180. frame_data[0] = frame->data[0];
  181. frame_data[1] = frame->data[2];
  182. frame_data[2] = frame->data[1];
  183. frame_sizes[0] = frame->linesize[0] * config.cy;
  184. frame_sizes[1] = frame->linesize[2] * config.cy / 2;
  185. frame_sizes[2] = frame->linesize[1] * config.cy / 2;
  186. }
  187. long long actualPTS = frame->pts * frameInterval;
  188. bool success = encoder.Encode(frame_data, frame_sizes,
  189. actualPTS, actualPTS + frameInterval,
  190. dshowPacket, new_packet);
  191. if (!success)
  192. return false;
  193. if (new_packet && !!dshowPacket.data && !!dshowPacket.size) {
  194. packet->data = dshowPacket.data;
  195. packet->size = dshowPacket.size;
  196. packet->type = OBS_ENCODER_VIDEO;
  197. packet->pts = dshowPacket.pts / frameInterval;
  198. packet->dts = dshowPacket.dts / frameInterval;
  199. packet->keyframe = obs_avc_keyframe(packet->data, packet->size);
  200. /* first packet must be parsed in order to retrieve header */
  201. if (first) {
  202. first = false;
  203. ParseFirstPacket(packet->data, packet->size);
  204. packet->data = firstPacket.array;
  205. packet->size = firstPacket.num;
  206. }
  207. *received_packet = true;
  208. }
  209. return true;
  210. }
  211. static bool DShowEncode(void *data, struct encoder_frame *frame,
  212. struct encoder_packet *packet, bool *received_packet)
  213. {
  214. return reinterpret_cast<DShowEncoder*>(data)->Encode(frame, packet,
  215. received_packet);
  216. }
  217. static bool GetDShowExtraData(void *data, uint8_t **extra_data, size_t *size)
  218. {
  219. DShowEncoder *encoder = reinterpret_cast<DShowEncoder*>(data);
  220. *extra_data = encoder->header.array;
  221. *size = encoder->header.num;
  222. return *size > 0;
  223. }
  224. static inline bool ValidResolution(uint32_t width, uint32_t height)
  225. {
  226. return (width == 1280 && height == 720) ||
  227. (width == 1024 && height == 768);
  228. }
  229. static bool GetDShowVideoInfo(void *data, struct video_scale_info *info)
  230. {
  231. DShowEncoder *encoder = reinterpret_cast<DShowEncoder*>(data);
  232. video_t *video = obs_encoder_video(encoder->context);
  233. const struct video_output_info *vid_info = video_output_get_info(video);
  234. encoder->format = VIDEO_FORMAT_I420;
  235. if (vid_info->format == VIDEO_FORMAT_I420 &&
  236. ValidResolution(vid_info->width, vid_info->height))
  237. return false;
  238. info->format = VIDEO_FORMAT_I420;
  239. info->width = vid_info->width;
  240. info->height = vid_info->height;
  241. info->range = VIDEO_RANGE_DEFAULT;
  242. info->colorspace = VIDEO_CS_DEFAULT;
  243. double aspect = double(info->width) / double(info->height);
  244. if (fabs(aspect - standardAspect) < fabs(aspect - wideAspect)) {
  245. info->width = 1024;
  246. info->height = 768;
  247. } else {
  248. info->width = 1280;
  249. info->height = 720;
  250. }
  251. return true;
  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. }