|
| 1 | +/* Copyright 2020 The TensorFlow Authors. All Rights Reserved. |
| 2 | +
|
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | +
|
| 7 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +
|
| 9 | +Unless required by applicable law or agreed to in writing, software |
| 10 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +See the License for the specific language governing permissions and |
| 13 | +limitations under the License. |
| 14 | +==============================================================================*/ |
| 15 | + |
| 16 | +#include "tensorflow/core/framework/resource_mgr.h" |
| 17 | +#include "tensorflow/core/framework/resource_op_kernel.h" |
| 18 | + |
| 19 | +extern "C" { |
| 20 | +#if defined(__APPLE__) |
| 21 | +void* VideoCaptureInitFunction(int64_t* bytes, int64_t* width, int64_t* height); |
| 22 | +void VideoCaptureNextFunction(void* context, void* data, int64_t size); |
| 23 | +void VideoCaptureFiniFunction(void* context); |
| 24 | +#else |
| 25 | +void* VideoCaptureInitFunction(int64_t* bytes, int64_t* width, |
| 26 | + int64_t* height) { |
| 27 | + return NULL; |
| 28 | +} |
| 29 | +void VideoCaptureNextFunction(void* context, void* data, int64_t size) {} |
| 30 | +void VideoCaptureFiniFunction(void* context) {} |
| 31 | +#endif |
| 32 | +} |
| 33 | +namespace tensorflow { |
| 34 | +namespace data { |
| 35 | +namespace { |
| 36 | + |
| 37 | +class VideoCaptureReadableResource : public ResourceBase { |
| 38 | + public: |
| 39 | + VideoCaptureReadableResource(Env* env) |
| 40 | + : env_(env), context_(nullptr, [](void* p) { |
| 41 | + if (p != nullptr) { |
| 42 | + VideoCaptureFiniFunction(p); |
| 43 | + } |
| 44 | + }) {} |
| 45 | + ~VideoCaptureReadableResource() {} |
| 46 | + |
| 47 | + Status Init(const string& input) { |
| 48 | + mutex_lock l(mu_); |
| 49 | + |
| 50 | + int64_t bytes, width, height; |
| 51 | + context_.reset(VideoCaptureInitFunction(&bytes, &width, &height)); |
| 52 | + if (context_.get() == nullptr) { |
| 53 | + return errors::InvalidArgument("unable to open device ", input); |
| 54 | + } |
| 55 | + bytes_ = static_cast<int64>(bytes); |
| 56 | + width_ = static_cast<int64>(width); |
| 57 | + height_ = static_cast<int64>(height); |
| 58 | + return Status::OK(); |
| 59 | + } |
| 60 | + Status Read( |
| 61 | + std::function<Status(const TensorShape& shape, Tensor** value_tensor)> |
| 62 | + allocate_func) { |
| 63 | + mutex_lock l(mu_); |
| 64 | + |
| 65 | + Tensor* value_tensor; |
| 66 | + TF_RETURN_IF_ERROR(allocate_func(TensorShape({1}), &value_tensor)); |
| 67 | + |
| 68 | + string buffer; |
| 69 | + buffer.resize(bytes_); |
| 70 | + VideoCaptureNextFunction(context_.get(), (void*)&buffer[0], |
| 71 | + static_cast<int64_t>(bytes_)); |
| 72 | + value_tensor->flat<string>()(0) = buffer; |
| 73 | + |
| 74 | + return Status::OK(); |
| 75 | + } |
| 76 | + string DebugString() const override { |
| 77 | + mutex_lock l(mu_); |
| 78 | + return "VideoCaptureReadableResource"; |
| 79 | + } |
| 80 | + |
| 81 | + protected: |
| 82 | + mutable mutex mu_; |
| 83 | + Env* env_ GUARDED_BY(mu_); |
| 84 | + |
| 85 | + std::unique_ptr<void, void (*)(void*)> context_; |
| 86 | + int64 bytes_; |
| 87 | + int64 width_; |
| 88 | + int64 height_; |
| 89 | +}; |
| 90 | + |
| 91 | +class VideoCaptureReadableInitOp |
| 92 | + : public ResourceOpKernel<VideoCaptureReadableResource> { |
| 93 | + public: |
| 94 | + explicit VideoCaptureReadableInitOp(OpKernelConstruction* context) |
| 95 | + : ResourceOpKernel<VideoCaptureReadableResource>(context) { |
| 96 | + env_ = context->env(); |
| 97 | + } |
| 98 | + |
| 99 | + private: |
| 100 | + void Compute(OpKernelContext* context) override { |
| 101 | + ResourceOpKernel<VideoCaptureReadableResource>::Compute(context); |
| 102 | + |
| 103 | + const Tensor* input_tensor; |
| 104 | + OP_REQUIRES_OK(context, context->input("input", &input_tensor)); |
| 105 | + const string& input = input_tensor->scalar<string>()(); |
| 106 | + |
| 107 | + OP_REQUIRES_OK(context, resource_->Init(input)); |
| 108 | + } |
| 109 | + Status CreateResource(VideoCaptureReadableResource** resource) |
| 110 | + EXCLUSIVE_LOCKS_REQUIRED(mu_) override { |
| 111 | + *resource = new VideoCaptureReadableResource(env_); |
| 112 | + return Status::OK(); |
| 113 | + } |
| 114 | + |
| 115 | + private: |
| 116 | + mutable mutex mu_; |
| 117 | + Env* env_ GUARDED_BY(mu_); |
| 118 | +}; |
| 119 | + |
| 120 | +class VideoCaptureReadableReadOp : public OpKernel { |
| 121 | + public: |
| 122 | + explicit VideoCaptureReadableReadOp(OpKernelConstruction* context) |
| 123 | + : OpKernel(context) { |
| 124 | + env_ = context->env(); |
| 125 | + } |
| 126 | + |
| 127 | + void Compute(OpKernelContext* context) override { |
| 128 | + VideoCaptureReadableResource* resource; |
| 129 | + OP_REQUIRES_OK(context, |
| 130 | + GetResourceFromContext(context, "input", &resource)); |
| 131 | + core::ScopedUnref unref(resource); |
| 132 | + |
| 133 | + OP_REQUIRES_OK( |
| 134 | + context, resource->Read([&](const TensorShape& shape, |
| 135 | + Tensor** value_tensor) -> Status { |
| 136 | + TF_RETURN_IF_ERROR(context->allocate_output(0, shape, value_tensor)); |
| 137 | + return Status::OK(); |
| 138 | + })); |
| 139 | + } |
| 140 | + |
| 141 | + private: |
| 142 | + mutable mutex mu_; |
| 143 | + Env* env_ GUARDED_BY(mu_); |
| 144 | +}; |
| 145 | +REGISTER_KERNEL_BUILDER(Name("IO>VideoCaptureReadableInit").Device(DEVICE_CPU), |
| 146 | + VideoCaptureReadableInitOp); |
| 147 | +REGISTER_KERNEL_BUILDER(Name("IO>VideoCaptureReadableRead").Device(DEVICE_CPU), |
| 148 | + VideoCaptureReadableReadOp); |
| 149 | + |
| 150 | +} // namespace |
| 151 | +} // namespace data |
| 152 | +} // namespace tensorflow |
0 commit comments