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