1 /*
2 * Copyright (c) 2019 Rockchip Corporation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 */
17
18 #include "TnrStatsStream.h"
19 #include "SPStreamProcUnit.h"
20 #include "rkispp-config.h"
21 #include "CamHwIsp20.h"
22 namespace RkCam {
23
24
TnrStatsStream(SmartPtr<V4l2Device> dev,int type)25 TnrStatsStream::TnrStatsStream (SmartPtr<V4l2Device> dev, int type)
26 : RKStream(dev, type)
27 , _fd_init_flag(true)
28 {
29 setPollCallback (this);
30 }
31
~TnrStatsStream()32 TnrStatsStream::~TnrStatsStream()
33 {
34 }
35
36 void
start()37 TnrStatsStream::start()
38 {
39 RKStream::start();
40 }
41
42 void
stop()43 TnrStatsStream::stop()
44 {
45 RKStream::stopThreadOnly();
46 deinit_tnrbuf();
47 RKStream::stopDeviceOnly();
48 }
49
set_device(CamHwIsp20 * camHw,SmartPtr<V4l2SubDevice> dev)50 void TnrStatsStream::set_device(CamHwIsp20* camHw, SmartPtr<V4l2SubDevice> dev)
51 {
52 _ispp_dev = dev;
53 _camHw = camHw;
54 }
55
56 SmartPtr<VideoBuffer>
new_video_buffer(SmartPtr<V4l2Buffer> buf,SmartPtr<V4l2Device> dev)57 TnrStatsStream::new_video_buffer(SmartPtr<V4l2Buffer> buf, SmartPtr<V4l2Device> dev)
58 {
59 if (_fd_init_flag) {
60 init_tnrbuf();
61 _fd_init_flag = false;
62 }
63
64 struct rkispp_stats_tnrbuf *stats;
65 //gainkg
66 SmartPtr<SubV4l2BufferProxy> gainkg = new SubV4l2BufferProxy(buf, dev);
67 stats = (struct rkispp_stats_tnrbuf *)(gainkg->get_v4l2_userptr());
68 LOGD("%s:kg_indx=%d,g_indx=%d,frame_id=%d\n",__FUNCTION__, stats->gainkg.index, stats->gain.index, stats->frame_id);
69 gainkg->set_buff_info(get_fd_by_index(stats->gainkg.index), stats->gainkg.size);
70 gainkg->_buf_type = ISPP_GAIN_KG;
71
72 return gainkg;
73 }
74
75 XCamReturn
poll_buffer_ready(SmartPtr<VideoBuffer> & buf)76 TnrStatsStream::poll_buffer_ready (SmartPtr<VideoBuffer> &buf)
77 {
78 if (_camHw->mHwResLintener) {
79 struct rkispp_stats_tnrbuf *stats;
80 SmartPtr<SubV4l2BufferProxy> gainkg = buf.dynamic_cast_ptr<SubV4l2BufferProxy>();
81 stats = (struct rkispp_stats_tnrbuf *)(gainkg->get_v4l2_userptr());
82 //gainwr
83 SmartPtr<V4l2BufferProxy> v4l2buf = gainkg.dynamic_cast_ptr<V4l2BufferProxy>();
84 SmartPtr<SubVideoBuffer> gainwr = new SubVideoBuffer(v4l2buf);
85 gainwr->_buf_type = ISPP_GAIN_WR;
86 gainwr->set_buff_info(get_fd_by_index(stats->gain.index), stats->gain.size);
87 gainwr->set_sequence(stats->frame_id);
88 SmartPtr<VideoBuffer> vbuf = gainwr.dynamic_cast_ptr<VideoBuffer>();
89 _camHw->mHwResLintener->hwResCb(vbuf); //send gainwr
90 _camHw->mHwResLintener->hwResCb(buf); //send gainkg
91
92 }
93
94 return XCAM_RETURN_NO_ERROR;
95 }
96
get_fd_by_index(uint32_t index)97 int TnrStatsStream::get_fd_by_index(uint32_t index)
98 {
99 #if 0
100 if (index < 0)
101 return -1;
102 for (int i=0; i<_buf_num; i++) {
103 if (index == (int)_idx_array[i]) {
104 return _fd_array[i];
105 }
106 }
107 return -1;
108 #else
109 int fd = -1;
110 std::map<uint32_t, int>::iterator it;
111 it = _idx_fd_map.find(index);
112 if (it == _idx_fd_map.end())
113 fd = -1;
114 else
115 fd = it->second;
116 return fd;
117 #endif
118 }
119
init_tnrbuf()120 bool TnrStatsStream::init_tnrbuf()
121 {
122 struct rkispp_buf_idxfd isppbuf_fd;
123 int res = -1;
124
125 memset(&isppbuf_fd, 0, sizeof(isppbuf_fd));
126 res = _ispp_dev->io_control(RKISPP_CMD_GET_TNRBUF_FD , &isppbuf_fd);
127 if (res)
128 return false;
129 LOGD("tnr buf_num=%d",isppbuf_fd.buf_num);
130 for (uint32_t i=0; i<isppbuf_fd.buf_num; i++) {
131 if (isppbuf_fd.dmafd[i] < 0) {
132 LOGE("tnrbuf_fd[%u]:%d is illegal!",isppbuf_fd.index[i],isppbuf_fd.dmafd[i]);
133 LOGE("\n*** ASSERT: In File %s,line %d ***\n", __FILE__, __LINE__);
134 assert(0);
135 }
136 _fd_array[i] = isppbuf_fd.dmafd[i];
137 _idx_array[i] = isppbuf_fd.index[i];
138 _idx_fd_map[isppbuf_fd.index[i]] = isppbuf_fd.dmafd[i];
139 LOGD("tnrbuf_fd[%u]:%d",isppbuf_fd.index[i],isppbuf_fd.dmafd[i]);
140 }
141 _buf_num = isppbuf_fd.buf_num;
142 return true;
143 }
144
145 void
deinit_tnrbuf()146 TnrStatsStream::deinit_tnrbuf()
147 {
148 LOGD("%s enter", __FUNCTION__);
149 std::map<uint32_t,int>::iterator it;
150 for (it=_idx_fd_map.begin(); it!=_idx_fd_map.end(); ++it)
151 ::close(it->second);
152
153 _idx_fd_map.clear();
154 LOGD("%s exit", __FUNCTION__);
155 }
156
157 //void TnrStatsStream::connect_nr_stream(SmartPtr<NrStatsStream> stream)
158 //{
159 // _nr_stream_unit = stream;
160 //}
161
162 } //namspace RkCam
163