Lines Matching +full:gain +full:- +full:scaling +full:- +full:p
1 // SPDX-License-Identifier: GPL-2.0-only
3 * vivid-kthread-cap.h - video/vbi capture thread support functions.
20 #include <linux/v4l2-dv-timings.h>
23 #include <media/videobuf2-vmalloc.h>
24 #include <media/v4l2-dv-timings.h>
25 #include <media/v4l2-ioctl.h>
26 #include <media/v4l2-fh.h>
27 #include <media/v4l2-event.h>
28 #include <media/v4l2-rect.h>
30 #include "vivid-core.h"
31 #include "vivid-vid-common.h"
32 #include "vivid-vid-cap.h"
33 #include "vivid-vid-out.h"
34 #include "vivid-radio-common.h"
35 #include "vivid-radio-rx.h"
36 #include "vivid-radio-tx.h"
37 #include "vivid-sdr-cap.h"
38 #include "vivid-vbi-cap.h"
39 #include "vivid-vbi-out.h"
40 #include "vivid-osd.h"
41 #include "vivid-ctrls.h"
42 #include "vivid-kthread-cap.h"
43 #include "vivid-meta-cap.h"
48 return dev->std_cap[dev->input];
60 if ((dev->fbuf_out_flags & V4L2_FBUF_FLAG_CHROMAKEY) &&
61 *osd != dev->chromakey_out)
63 if ((dev->fbuf_out_flags & V4L2_FBUF_FLAG_SRC_CHROMAKEY) &&
64 out == dev->chromakey_out)
66 if (dev->fmt_cap->alpha_mask) {
67 if ((dev->fbuf_out_flags & V4L2_FBUF_FLAG_GLOBAL_ALPHA) &&
68 dev->global_alpha_out)
70 if ((dev->fbuf_out_flags & V4L2_FBUF_FLAG_LOCAL_ALPHA) &&
71 *cap & dev->fmt_cap->alpha_mask)
73 if ((dev->fbuf_out_flags & V4L2_FBUF_FLAG_LOCAL_INV_ALPHA) &&
74 !(*cap & dev->fmt_cap->alpha_mask))
94 /* Coarse scaling with Bresenham */
114 error -= dstw;
149 0, 0, dev->display_width, dev->display_height
153 out_dev->overlay_out_left, out_dev->overlay_out_top,
154 out_dev->compose_out.width, out_dev->compose_out.height
157 v4l2_rect_intersect(&dev->loop_vid_copy, &dev->crop_cap, &out_dev->compose_out);
159 dev->loop_vid_out = dev->loop_vid_copy;
160 v4l2_rect_scale(&dev->loop_vid_out, &out_dev->compose_out, &out_dev->crop_out);
161 dev->loop_vid_out.left += out_dev->crop_out.left;
162 dev->loop_vid_out.top += out_dev->crop_out.top;
164 dev->loop_vid_cap = dev->loop_vid_copy;
165 v4l2_rect_scale(&dev->loop_vid_cap, &dev->crop_cap, &dev->compose_cap);
169 dev->loop_vid_copy.left, dev->loop_vid_copy.top,
170 dev->loop_vid_copy.width, dev->loop_vid_copy.height,
171 dev->loop_vid_out.left, dev->loop_vid_out.top,
172 dev->loop_vid_out.width, dev->loop_vid_out.height,
173 dev->loop_vid_cap.left, dev->loop_vid_cap.top,
174 dev->loop_vid_cap.width, dev->loop_vid_cap.height);
179 r_overlay.left += out_dev->compose_out.left - out_dev->overlay_out_left;
180 r_overlay.top += out_dev->compose_out.top - out_dev->overlay_out_top;
182 v4l2_rect_intersect(&dev->loop_vid_overlay, &r_overlay, &dev->loop_vid_copy);
183 dev->loop_fb_copy = dev->loop_vid_overlay;
185 /* shift dev->loop_fb_copy back again to the fb origin */
186 dev->loop_fb_copy.left -= out_dev->compose_out.left - out_dev->overlay_out_left;
187 dev->loop_fb_copy.top -= out_dev->compose_out.top - out_dev->overlay_out_top;
189 dev->loop_vid_overlay_cap = dev->loop_vid_overlay;
190 v4l2_rect_scale(&dev->loop_vid_overlay_cap, &dev->crop_cap, &dev->compose_cap);
194 dev->loop_fb_copy.left, dev->loop_fb_copy.top,
195 dev->loop_fb_copy.width, dev->loop_fb_copy.height,
196 dev->loop_vid_overlay.left, dev->loop_vid_overlay.top,
197 dev->loop_vid_overlay.width, dev->loop_vid_overlay.height,
198 dev->loop_vid_overlay_cap.left, dev->loop_vid_overlay_cap.top,
199 dev->loop_vid_overlay_cap.width, dev->loop_vid_overlay_cap.height);
203 unsigned p, unsigned bpl[TPG_MAX_PLANES], unsigned h)
208 if (p == 0 || tpg_g_buffers(tpg) > 1)
209 return vb2_plane_vaddr(&buf->vb.vb2_buf, p);
210 vbuf = vb2_plane_vaddr(&buf->vb.vb2_buf, 0);
211 for (i = 0; i < p; i++)
212 vbuf += bpl[i] * h / tpg->vdownsampling[i];
217 struct vivid_dev *out_dev, unsigned p,
220 bool blank = dev->must_blank[vid_cap_buf->vb.vb2_buf.index];
221 struct tpg_data *tpg = &dev->tpg;
223 unsigned vdiv = out_dev->fmt_out->vdownsampling[p];
224 unsigned twopixsize = tpg_g_twopixelsize(tpg, p);
225 unsigned img_width = tpg_hdiv(tpg, p, dev->compose_cap.width);
226 unsigned img_height = dev->compose_cap.height;
227 unsigned stride_cap = tpg->bytesperline[p];
228 unsigned stride_out = out_dev->bytesperline_out[p];
229 unsigned stride_osd = dev->display_byte_stride;
230 unsigned hmax = (img_height * tpg->perc_fill) / 100;
234 bool blend = out_dev->fbuf_out_flags;
235 /* Coarse scaling with Bresenham */
244 unsigned vid_cap_left = tpg_hdiv(tpg, p, dev->loop_vid_cap.left);
248 vid_out_int_part = dev->loop_vid_out.height / dev->loop_vid_cap.height;
249 vid_out_fract_part = dev->loop_vid_out.height % dev->loop_vid_cap.height;
251 if (!list_empty(&out_dev->vid_out_active))
252 vid_out_buf = list_entry(out_dev->vid_out_active.next,
255 return -ENODATA;
257 vid_cap_buf->vb.field = vid_out_buf->vb.field;
259 voutbuf = plane_vaddr(tpg, vid_out_buf, p,
260 out_dev->bytesperline_out, out_dev->fmt_out_rect.height);
261 if (p < out_dev->fmt_out->buffers)
262 voutbuf += vid_out_buf->vb.vb2_buf.planes[p].data_offset;
263 voutbuf += tpg_hdiv(tpg, p, dev->loop_vid_out.left) +
264 (dev->loop_vid_out.top / vdiv) * stride_out;
265 vcapbuf += tpg_hdiv(tpg, p, dev->compose_cap.left) +
266 (dev->compose_cap.top / vdiv) * stride_cap;
268 if (dev->loop_vid_copy.width == 0 || dev->loop_vid_copy.height == 0) {
274 memcpy(vcapbuf, tpg->black_line[p], img_width);
278 if (out_dev->overlay_out_enabled &&
279 dev->loop_vid_overlay.width && dev->loop_vid_overlay.height) {
280 vosdbuf = dev->video_vbase;
281 vosdbuf += (dev->loop_fb_copy.left * twopixsize) / 2 +
282 dev->loop_fb_copy.top * stride_osd;
283 vid_overlay_int_part = dev->loop_vid_overlay.height /
284 dev->loop_vid_overlay_cap.height;
285 vid_overlay_fract_part = dev->loop_vid_overlay.height %
286 dev->loop_vid_overlay_cap.height;
289 vid_cap_right = tpg_hdiv(tpg, p, dev->loop_vid_cap.left + dev->loop_vid_cap.width);
290 /* quick is true if no video scaling is needed */
291 quick = dev->loop_vid_out.width == dev->loop_vid_cap.width;
293 dev->cur_scaled_line = dev->loop_vid_out.height;
296 bool osdline = vosdbuf && y >= dev->loop_vid_overlay_cap.top &&
297 y < dev->loop_vid_overlay_cap.top + dev->loop_vid_overlay_cap.height;
303 if (y < dev->loop_vid_cap.top ||
304 y >= dev->loop_vid_cap.top + dev->loop_vid_cap.height) {
305 memcpy(vcapbuf, tpg->black_line[p], img_width);
310 if (dev->loop_vid_cap.left)
311 memcpy(vcapbuf, tpg->black_line[p], vid_cap_left);
315 memcpy(vcapbuf + vid_cap_right, tpg->black_line[p],
316 img_width - vid_cap_right);
321 tpg_hdiv(tpg, p, dev->loop_vid_cap.width));
324 if (dev->cur_scaled_line == vid_out_y) {
325 memcpy(vcapbuf + vid_cap_left, dev->scaled_line,
326 tpg_hdiv(tpg, p, dev->loop_vid_cap.width));
330 scale_line(voutbuf + vid_out_y * stride_out, dev->scaled_line,
331 tpg_hdiv(tpg, p, dev->loop_vid_out.width),
332 tpg_hdiv(tpg, p, dev->loop_vid_cap.width),
333 tpg_g_twopixelsize(tpg, p));
340 ((dev->loop_vid_overlay.left - dev->loop_vid_copy.left) *
344 scale_line(voutbuf + vid_out_y * stride_out, dev->blended_line,
345 dev->loop_vid_out.width, dev->loop_vid_copy.width,
346 tpg_g_twopixelsize(tpg, p));
348 blend_line(dev, vid_overlay_y + dev->loop_vid_overlay.top,
349 dev->loop_vid_overlay.left,
350 dev->blended_line + offset, osd,
351 dev->loop_vid_overlay.width, twopixsize / 2);
353 memcpy(dev->blended_line + offset,
354 osd, (dev->loop_vid_overlay.width * twopixsize) / 2);
355 scale_line(dev->blended_line, dev->scaled_line,
356 dev->loop_vid_copy.width, dev->loop_vid_cap.width,
357 tpg_g_twopixelsize(tpg, p));
359 dev->cur_scaled_line = vid_out_y;
360 memcpy(vcapbuf + vid_cap_left, dev->scaled_line,
361 tpg_hdiv(tpg, p, dev->loop_vid_cap.width));
367 if (vid_overlay_error >= dev->loop_vid_overlay_cap.height) {
368 vid_overlay_error -= dev->loop_vid_overlay_cap.height;
374 if (vid_out_error >= dev->loop_vid_cap.height / vdiv) {
375 vid_out_error -= dev->loop_vid_cap.height / vdiv;
383 memcpy(vcapbuf, tpg->contrast_line[p], img_width);
390 struct tpg_data *tpg = &dev->tpg;
391 unsigned factor = V4L2_FIELD_HAS_T_OR_B(dev->field_cap) ? 2 : 1;
394 bool is_60hz = is_tv && (dev->std_cap[dev->input] & V4L2_STD_525_60);
395 unsigned p;
400 s32 gain;
402 buf->vb.sequence = dev->vid_cap_seq_count;
403 v4l2_ctrl_s_ctrl(dev->ro_int32, buf->vb.sequence & 0xff);
404 if (dev->field_cap == V4L2_FIELD_ALTERNATE) {
407 * with the top field. So if the 0-based seq_count is even,
411 buf->vb.field = ((dev->vid_cap_seq_count & 1) ^ is_60hz) ?
417 buf->vb.sequence /= 2;
419 buf->vb.field = dev->field_cap;
421 tpg_s_field(tpg, buf->vb.field,
422 dev->field_cap == V4L2_FIELD_ALTERNATE);
423 tpg_s_perc_fill_blank(tpg, dev->must_blank[buf->vb.vb2_buf.index]);
427 !VIVID_INVALID_SIGNAL(dev->std_signal_mode[dev->input])) ||
429 !VIVID_INVALID_SIGNAL(dev->dv_timings_signal_mode[dev->input])))) {
447 if (out_dev && dev != out_dev && !mutex_trylock(&out_dev->mutex))
454 for (p = 0; p < tpg_g_planes(tpg); p++) {
455 void *vbuf = plane_vaddr(tpg, buf, p,
456 tpg->bytesperline, tpg->buf_height);
459 * The first plane of a multiplanar format has a non-zero
461 * correctly supports non-zero data offsets.
463 if (p < tpg_g_buffers(tpg) && dev->fmt_cap->data_offset[p]) {
464 memset(vbuf, dev->fmt_cap->data_offset[p] & 0xff,
465 dev->fmt_cap->data_offset[p]);
466 vbuf += dev->fmt_cap->data_offset[p];
468 tpg_calc_text_basep(tpg, basep, p, vbuf);
469 if (!out_dev || vivid_copy_buffer(dev, out_dev, p, vbuf, buf))
471 p, vbuf);
474 mutex_unlock(&out_dev->mutex);
476 dev->must_blank[buf->vb.vb2_buf.index] = false;
479 if (dev->field_cap != V4L2_FIELD_ALTERNATE ||
480 (dev->vid_cap_seq_count & 1) == 0)
481 dev->ms_vid_cap =
482 jiffies_to_msecs(jiffies - dev->jiffies_vid_cap);
484 ms = dev->ms_vid_cap;
485 if (dev->osd_mode <= 1) {
491 buf->vb.sequence,
492 (dev->field_cap == V4L2_FIELD_ALTERNATE) ?
493 (buf->vb.field == V4L2_FIELD_TOP ?
497 if (dev->osd_mode == 0) {
499 dev->src_rect.width, dev->src_rect.height, dev->input);
502 gain = v4l2_ctrl_g_ctrl(dev->gain);
503 mutex_lock(dev->ctrl_hdl_user_vid.lock);
506 dev->brightness->cur.val,
507 dev->contrast->cur.val,
508 dev->saturation->cur.val,
509 dev->hue->cur.val);
512 " autogain %d, gain %3d, alpha 0x%02x ",
513 dev->autogain->cur.val, gain, dev->alpha->cur.val);
514 mutex_unlock(dev->ctrl_hdl_user_vid.lock);
516 mutex_lock(dev->ctrl_hdl_user_aud.lock);
519 dev->volume->cur.val, dev->mute->cur.val);
520 mutex_unlock(dev->ctrl_hdl_user_aud.lock);
522 mutex_lock(dev->ctrl_hdl_user_gen.lock);
524 dev->int32->cur.val,
525 dev->ro_int32->cur.val,
526 *dev->int64->p_cur.p_s64,
527 dev->bitmask->cur.val);
530 dev->boolean->cur.val,
531 dev->menu->qmenu[dev->menu->cur.val],
532 dev->string->p_cur.p_char);
535 dev->int_menu->qmenu_int[dev->int_menu->cur.val],
536 dev->int_menu->cur.val);
537 mutex_unlock(dev->ctrl_hdl_user_gen.lock);
539 if (dev->button_pressed) {
540 dev->button_pressed--;
544 if (dev->osd[0]) {
547 " OSD \"%s\"", dev->osd);
551 if (dev->osd_jiffies &&
552 time_is_before_jiffies(dev->osd_jiffies + 5 * HZ)) {
553 dev->osd[0] = 0;
554 dev->osd_jiffies = 0;
564 f_period = (u64)dev->timeperframe_vid_cap.numerator * 1000000000;
565 if (WARN_ON(dev->timeperframe_vid_cap.denominator == 0))
566 dev->timeperframe_vid_cap.denominator = 1;
567 do_div(f_period, dev->timeperframe_vid_cap.denominator);
568 if (dev->field_cap == V4L2_FIELD_ALTERNATE)
574 dev->cap_frame_eof_offset = f_period * 9;
575 do_div(dev->cap_frame_eof_offset, 10);
576 dev->cap_frame_period = f_period;
589 while (dropped_bufs-- > 1)
590 tpg_update_mv_count(&dev->tpg,
591 dev->field_cap == V4L2_FIELD_NONE ||
592 dev->field_cap == V4L2_FIELD_ALTERNATE);
595 if (dev->perc_dropped_buffers &&
596 get_random_u32_below(100) < dev->perc_dropped_buffers)
599 spin_lock(&dev->slock);
600 if (!list_empty(&dev->vid_cap_active)) {
601 vid_cap_buf = list_entry(dev->vid_cap_active.next, struct vivid_buffer, list);
602 list_del(&vid_cap_buf->list);
604 if (!list_empty(&dev->vbi_cap_active)) {
605 if (dev->field_cap != V4L2_FIELD_ALTERNATE ||
606 (dev->vbi_cap_seq_count & 1)) {
607 vbi_cap_buf = list_entry(dev->vbi_cap_active.next,
609 list_del(&vbi_cap_buf->list);
612 if (!list_empty(&dev->meta_cap_active)) {
613 meta_cap_buf = list_entry(dev->meta_cap_active.next,
615 list_del(&meta_cap_buf->list);
618 spin_unlock(&dev->slock);
623 f_time = ktime_get_ns() + dev->time_wrap_offset;
626 v4l2_ctrl_request_setup(vid_cap_buf->vb.vb2_buf.req_obj.req,
627 &dev->ctrl_hdl_vid_cap);
631 vid_cap_buf->vb.vb2_buf.index);
633 v4l2_ctrl_request_complete(vid_cap_buf->vb.vb2_buf.req_obj.req,
634 &dev->ctrl_hdl_vid_cap);
635 vb2_buffer_done(&vid_cap_buf->vb.vb2_buf, dev->dqbuf_error ?
638 vid_cap_buf->vb.vb2_buf.index);
640 vid_cap_buf->vb.vb2_buf.timestamp = f_time;
641 if (!dev->tstamp_src_is_soe)
642 vid_cap_buf->vb.vb2_buf.timestamp += dev->cap_frame_eof_offset;
648 v4l2_ctrl_request_setup(vbi_cap_buf->vb.vb2_buf.req_obj.req,
649 &dev->ctrl_hdl_vbi_cap);
650 if (vbi_cap_buf->vb.vb2_buf.type == V4L2_BUF_TYPE_SLICED_VBI_CAPTURE)
654 v4l2_ctrl_request_complete(vbi_cap_buf->vb.vb2_buf.req_obj.req,
655 &dev->ctrl_hdl_vbi_cap);
656 vb2_buffer_done(&vbi_cap_buf->vb.vb2_buf, dev->dqbuf_error ?
659 vbi_cap_buf->vb.vb2_buf.index);
662 vbi_period = dev->cap_frame_period * 5;
664 vbi_cap_buf->vb.vb2_buf.timestamp = f_time + dev->cap_frame_eof_offset + vbi_period;
668 v4l2_ctrl_request_setup(meta_cap_buf->vb.vb2_buf.req_obj.req,
669 &dev->ctrl_hdl_meta_cap);
671 v4l2_ctrl_request_complete(meta_cap_buf->vb.vb2_buf.req_obj.req,
672 &dev->ctrl_hdl_meta_cap);
673 vb2_buffer_done(&meta_cap_buf->vb.vb2_buf, dev->dqbuf_error ?
676 meta_cap_buf->vb.vb2_buf.index);
677 meta_cap_buf->vb.vb2_buf.timestamp = f_time + dev->cap_frame_eof_offset;
680 dev->dqbuf_error = false;
684 tpg_update_mv_count(&dev->tpg, dev->field_cap == V4L2_FIELD_NONE ||
685 dev->field_cap == V4L2_FIELD_ALTERNATE);
706 dev->cap_seq_offset = 0;
707 dev->cap_seq_count = 0;
708 dev->cap_seq_resync = false;
709 dev->jiffies_vid_cap = jiffies;
710 dev->cap_stream_start = ktime_get_ns();
711 if (dev->time_wrap)
712 dev->time_wrap_offset = dev->time_wrap - dev->cap_stream_start;
714 dev->time_wrap_offset = 0;
722 if (!mutex_trylock(&dev->mutex)) {
728 if (dev->cap_seq_resync) {
729 dev->jiffies_vid_cap = cur_jiffies;
730 dev->cap_seq_offset = dev->cap_seq_count + 1;
731 dev->cap_seq_count = 0;
732 dev->cap_stream_start += dev->cap_frame_period *
733 dev->cap_seq_offset;
735 dev->cap_seq_resync = false;
737 numerator = dev->timeperframe_vid_cap.numerator;
738 denominator = dev->timeperframe_vid_cap.denominator;
740 if (dev->field_cap == V4L2_FIELD_ALTERNATE)
744 jiffies_since_start = cur_jiffies - dev->jiffies_vid_cap;
752 * 'jiffies-per-day' to ease jiffies_to_msecs calculation)
757 dev->jiffies_vid_cap = cur_jiffies;
758 dev->cap_seq_offset = buffers_since_start;
761 dropped_bufs = buffers_since_start + dev->cap_seq_offset - dev->cap_seq_count;
762 dev->cap_seq_count = buffers_since_start + dev->cap_seq_offset;
763 dev->vid_cap_seq_count = dev->cap_seq_count - dev->vid_cap_seq_start;
764 dev->vbi_cap_seq_count = dev->cap_seq_count - dev->vbi_cap_seq_start;
765 dev->meta_cap_seq_count = dev->cap_seq_count - dev->meta_cap_seq_start;
776 jiffies_since_start = jiffies - dev->jiffies_vid_cap;
778 mutex_unlock(&dev->mutex);
791 wait_jiffies = next_jiffies_since_start - jiffies_since_start;
799 cur_jiffies + wait_jiffies - jiffies);
807 v4l2_ctrl_grab(dev->ctrl_has_crop_cap, grab);
808 v4l2_ctrl_grab(dev->ctrl_has_compose_cap, grab);
809 v4l2_ctrl_grab(dev->ctrl_has_scaler_cap, grab);
816 if (dev->kthread_vid_cap) {
817 u32 seq_count = dev->cap_seq_count + dev->seq_wrap * 128;
819 if (pstreaming == &dev->vid_cap_streaming)
820 dev->vid_cap_seq_start = seq_count;
821 else if (pstreaming == &dev->vbi_cap_streaming)
822 dev->vbi_cap_seq_start = seq_count;
824 dev->meta_cap_seq_start = seq_count;
830 tpg_init_mv_count(&dev->tpg);
832 dev->vid_cap_seq_start = dev->seq_wrap * 128;
833 dev->vbi_cap_seq_start = dev->seq_wrap * 128;
834 dev->meta_cap_seq_start = dev->seq_wrap * 128;
836 dev->kthread_vid_cap = kthread_run(vivid_thread_vid_cap, dev,
837 "%s-vid-cap", dev->v4l2_dev.name);
839 if (IS_ERR(dev->kthread_vid_cap)) {
840 int err = PTR_ERR(dev->kthread_vid_cap);
842 dev->kthread_vid_cap = NULL;
843 v4l2_err(&dev->v4l2_dev, "kernel_thread() failed\n");
857 if (dev->kthread_vid_cap == NULL)
861 if (pstreaming == &dev->vid_cap_streaming) {
863 while (!list_empty(&dev->vid_cap_active)) {
866 buf = list_entry(dev->vid_cap_active.next,
868 list_del(&buf->list);
869 v4l2_ctrl_request_complete(buf->vb.vb2_buf.req_obj.req,
870 &dev->ctrl_hdl_vid_cap);
871 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
873 buf->vb.vb2_buf.index);
877 if (pstreaming == &dev->vbi_cap_streaming) {
878 while (!list_empty(&dev->vbi_cap_active)) {
881 buf = list_entry(dev->vbi_cap_active.next,
883 list_del(&buf->list);
884 v4l2_ctrl_request_complete(buf->vb.vb2_buf.req_obj.req,
885 &dev->ctrl_hdl_vbi_cap);
886 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
888 buf->vb.vb2_buf.index);
892 if (pstreaming == &dev->meta_cap_streaming) {
893 while (!list_empty(&dev->meta_cap_active)) {
896 buf = list_entry(dev->meta_cap_active.next,
898 list_del(&buf->list);
899 v4l2_ctrl_request_complete(buf->vb.vb2_buf.req_obj.req,
900 &dev->ctrl_hdl_meta_cap);
901 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
903 buf->vb.vb2_buf.index);
907 if (dev->vid_cap_streaming || dev->vbi_cap_streaming ||
908 dev->meta_cap_streaming)
913 kthread_stop(dev->kthread_vid_cap);
914 dev->kthread_vid_cap = NULL;