123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354 |
- #include <obs-module.h>
- #include <obs-avc.h>
- #include <util/darray.h>
- #include <util/dstr.hpp>
- #ifdef OBS_LEGACY
- #include "libdshowcapture/dshowcapture.hpp"
- #else
- #include <dshowcapture.hpp>
- #endif
- using namespace DShow;
- using namespace std;
- struct DShowEncoder {
- obs_encoder_t *context;
- VideoEncoder encoder;
- VideoEncoderConfig config;
- const wchar_t *device;
- video_format format;
- long long frameInterval;
- bool first = true;
- DARRAY(uint8_t) firstPacket;
- DARRAY(uint8_t) header;
- inline DShowEncoder(obs_encoder_t *context_, const wchar_t *device_) : context(context_), device(device_)
- {
- da_init(firstPacket);
- da_init(header);
- }
- inline ~DShowEncoder()
- {
- da_free(firstPacket);
- da_free(header);
- }
- inline void ParseFirstPacket(const uint8_t *data, size_t size);
- inline bool Update(obs_data_t *settings);
- inline bool Encode(struct encoder_frame *frame, struct encoder_packet *packet, bool *received_packet);
- };
- static const char *GetC985EncoderName(void *)
- {
- return obs_module_text("Encoder.C985");
- }
- static const char *GetC353EncoderName(void *)
- {
- return obs_module_text("Encoder.C353");
- }
- static inline void FindDevice(DeviceId &id, const wchar_t *name)
- {
- vector<DeviceId> devices;
- DShow::VideoEncoder::EnumEncoders(devices);
- for (const DeviceId &device : devices) {
- if (device.name.find(name) != string::npos) {
- id = device;
- break;
- }
- }
- }
- /*
- * As far as I can tell the devices only seem to support 1024x768 and 1280x720
- * resolutions, so I'm just going to cap it to those resolutions by aspect.
- *
- * XXX: This should really not be hard-coded. Problem is I don't know how to
- * properly query the encoding capabilities of the devices.
- */
- static const double standardAspect = 1024.0 / 768.0;
- static const double wideAspect = 1280.0 / 720.0;
- inline bool DShowEncoder::Update(obs_data_t *settings)
- {
- DStr deviceName;
- DeviceId id;
- FindDevice(id, device);
- video_t *video = obs_encoder_video(context);
- const struct video_output_info *voi = video_output_get_info(video);
- int bitrate = (int)obs_data_get_int(settings, "bitrate");
- int keyint_sec = (int)obs_data_get_int(settings, "keyint_sec");
- int width = (int)obs_encoder_get_width(context);
- int height = (int)obs_encoder_get_height(context);
- double aspect = double(width) / double(height);
- if (keyint_sec == 0)
- keyint_sec = 2;
- if (fabs(aspect - standardAspect) < fabs(aspect - wideAspect)) {
- width = 1024;
- height = 768;
- } else {
- width = 1280;
- height = 720;
- }
- int keyint = keyint_sec * voi->fps_num / voi->fps_den;
- frameInterval = voi->fps_den * 10000000 / voi->fps_num;
- config.fpsNumerator = voi->fps_num;
- config.fpsDenominator = voi->fps_den;
- config.bitrate = bitrate;
- config.keyframeInterval = keyint;
- config.cx = width;
- config.cy = height;
- config.name = id.name;
- config.path = id.path;
- first = true;
- da_resize(firstPacket, 0);
- da_resize(header, 0);
- dstr_from_wcs(deviceName, id.name.c_str());
- DStr encoder_name;
- dstr_from_wcs(encoder_name, config.name.c_str());
- blog(LOG_DEBUG,
- "win-dshow-encoder:\n"
- "\tencoder: %s\n"
- "\twidth: %d\n"
- "\theight: %d\n"
- "\tfps_num: %d\n"
- "\tfps_den: %d",
- deviceName->array, (int)width, (int)height, (int)voi->fps_num, (int)voi->fps_den);
- return encoder.SetConfig(config);
- }
- static bool UpdateDShowEncoder(void *data, obs_data_t *settings)
- {
- DShowEncoder *encoder = reinterpret_cast<DShowEncoder *>(data);
- if (!obs_encoder_active(encoder->context))
- return encoder->Update(settings);
- return true;
- }
- static inline void *CreateDShowEncoder(obs_data_t *settings, obs_encoder_t *context, const wchar_t *device)
- {
- DShowEncoder *encoder = nullptr;
- try {
- encoder = new DShowEncoder(context, device);
- UpdateDShowEncoder(encoder, settings);
- } catch (const char *error) {
- blog(LOG_ERROR, "Could not create DirectShow encoder '%s': %s", obs_encoder_get_name(context), error);
- }
- return encoder;
- }
- static void *CreateC985Encoder(obs_data_t *settings, obs_encoder_t *context)
- {
- return CreateDShowEncoder(settings, context, L"C985");
- }
- static void *CreateC353Encoder(obs_data_t *settings, obs_encoder_t *context)
- {
- return CreateDShowEncoder(settings, context, L"C353");
- }
- static void DestroyDShowEncoder(void *data)
- {
- delete reinterpret_cast<DShowEncoder *>(data);
- }
- /* the first packet contains the SPS/PPS (header) NALs, so parse the first
- * packet and separate the NALs */
- inline void DShowEncoder::ParseFirstPacket(const uint8_t *data, size_t size)
- {
- const uint8_t *nal_start, *nal_end, *nal_codestart;
- const uint8_t *end = data + size;
- int type;
- nal_start = obs_avc_find_startcode(data, end);
- nal_end = nullptr;
- while (nal_end != end) {
- nal_codestart = nal_start;
- while (nal_start < end && !*(nal_start++))
- ;
- if (nal_start == end)
- break;
- type = nal_start[0] & 0x1F;
- nal_end = obs_avc_find_startcode(nal_start, end);
- if (!nal_end)
- nal_end = end;
- if (type == OBS_NAL_SPS || type == OBS_NAL_PPS) {
- da_push_back_array(header, nal_codestart, nal_end - nal_codestart);
- } else {
- da_push_back_array(firstPacket, nal_codestart, nal_end - nal_codestart);
- }
- nal_start = nal_end;
- }
- }
- inline bool DShowEncoder::Encode(struct encoder_frame *frame, struct encoder_packet *packet, bool *received_packet)
- {
- unsigned char *frame_data[DSHOW_MAX_PLANES] = {};
- size_t frame_sizes[DSHOW_MAX_PLANES] = {};
- EncoderPacket dshowPacket;
- bool new_packet = false;
- /* The encoders expect YV12, so swap the chroma planes for encoding */
- if (format == VIDEO_FORMAT_I420) {
- frame_data[0] = frame->data[0];
- frame_data[1] = frame->data[2];
- frame_data[2] = frame->data[1];
- frame_sizes[0] = frame->linesize[0] * config.cy;
- frame_sizes[1] = frame->linesize[2] * config.cy / 2;
- frame_sizes[2] = frame->linesize[1] * config.cy / 2;
- }
- long long actualPTS = frame->pts * frameInterval;
- bool success =
- encoder.Encode(frame_data, frame_sizes, actualPTS, actualPTS + frameInterval, dshowPacket, new_packet);
- if (!success)
- return false;
- if (new_packet && !!dshowPacket.data && !!dshowPacket.size) {
- packet->data = dshowPacket.data;
- packet->size = dshowPacket.size;
- packet->type = OBS_ENCODER_VIDEO;
- packet->pts = dshowPacket.pts / frameInterval;
- packet->dts = dshowPacket.dts / frameInterval;
- packet->keyframe = obs_avc_keyframe(packet->data, packet->size);
- /* first packet must be parsed in order to retrieve header */
- if (first) {
- first = false;
- ParseFirstPacket(packet->data, packet->size);
- packet->data = firstPacket.array;
- packet->size = firstPacket.num;
- }
- *received_packet = true;
- }
- return true;
- }
- static bool DShowEncode(void *data, struct encoder_frame *frame, struct encoder_packet *packet, bool *received_packet)
- {
- return reinterpret_cast<DShowEncoder *>(data)->Encode(frame, packet, received_packet);
- }
- static bool GetDShowExtraData(void *data, uint8_t **extra_data, size_t *size)
- {
- DShowEncoder *encoder = reinterpret_cast<DShowEncoder *>(data);
- *extra_data = encoder->header.array;
- *size = encoder->header.num;
- return *size > 0;
- }
- static inline bool ValidResolution(uint32_t width, uint32_t height)
- {
- return (width == 1280 && height == 720) || (width == 1024 && height == 768);
- }
- static void GetDShowVideoInfo(void *data, struct video_scale_info *info)
- {
- DShowEncoder *encoder = reinterpret_cast<DShowEncoder *>(data);
- encoder->format = VIDEO_FORMAT_I420;
- if (info->format == VIDEO_FORMAT_I420 && ValidResolution(info->width, info->height))
- return;
- info->format = VIDEO_FORMAT_I420;
- info->width = info->width;
- info->height = info->height;
- info->range = VIDEO_RANGE_DEFAULT;
- info->colorspace = VIDEO_CS_DEFAULT;
- double aspect = double(info->width) / double(info->height);
- if (fabs(aspect - standardAspect) < fabs(aspect - wideAspect)) {
- info->width = 1024;
- info->height = 768;
- } else {
- info->width = 1280;
- info->height = 720;
- }
- }
- static void GetDShowEncoderDefauts(obs_data_t *settings)
- {
- obs_data_set_default_int(settings, "bitrate", 1000);
- }
- static obs_properties_t *GetDShowEncoderProperties(void *data)
- {
- obs_properties_t *ppts = obs_properties_create();
- obs_properties_add_int(ppts, "bitrate", obs_module_text("Bitrate"), 1000, 60000, 1);
- UNUSED_PARAMETER(data);
- return ppts;
- }
- void RegisterDShowEncoders()
- {
- obs_encoder_info info = {};
- info.type = OBS_ENCODER_VIDEO;
- info.codec = "h264";
- info.destroy = DestroyDShowEncoder;
- info.encode = DShowEncode;
- info.update = UpdateDShowEncoder;
- info.get_defaults = GetDShowEncoderDefauts;
- info.get_properties = GetDShowEncoderProperties;
- info.get_extra_data = GetDShowExtraData;
- info.get_video_info = GetDShowVideoInfo;
- vector<DeviceId> devices;
- DShow::VideoEncoder::EnumEncoders(devices);
- bool foundC985 = false;
- for (const DeviceId &device : devices) {
- if (!foundC985 && device.name.find(L"C985") != string::npos) {
- info.id = "dshow_c985_h264";
- info.get_name = GetC985EncoderName;
- info.create = CreateC985Encoder;
- obs_register_encoder(&info);
- foundC985 = true;
- } else if (device.name.find(L"C353") != string::npos) {
- info.id = "dshow_c353_h264";
- info.get_name = GetC353EncoderName;
- info.create = CreateC353Encoder;
- obs_register_encoder(&info);
- }
- }
- }
|