linux/drivers/media/platform/vivid/vivid-kthread-cap.c
<<
>>
Prefs
   1/*
   2 * vivid-kthread-cap.h - video/vbi capture thread support functions.
   3 *
   4 * Copyright 2014 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
   5 *
   6 * This program is free software; you may redistribute it and/or modify
   7 * it under the terms of the GNU General Public License as published by
   8 * the Free Software Foundation; version 2 of the License.
   9 *
  10 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  11 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  12 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  13 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  14 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  15 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  16 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  17 * SOFTWARE.
  18 */
  19
  20#include <linux/module.h>
  21#include <linux/errno.h>
  22#include <linux/kernel.h>
  23#include <linux/init.h>
  24#include <linux/sched.h>
  25#include <linux/slab.h>
  26#include <linux/font.h>
  27#include <linux/mutex.h>
  28#include <linux/videodev2.h>
  29#include <linux/kthread.h>
  30#include <linux/freezer.h>
  31#include <linux/random.h>
  32#include <linux/v4l2-dv-timings.h>
  33#include <asm/div64.h>
  34#include <media/videobuf2-vmalloc.h>
  35#include <media/v4l2-dv-timings.h>
  36#include <media/v4l2-ioctl.h>
  37#include <media/v4l2-fh.h>
  38#include <media/v4l2-event.h>
  39
  40#include "vivid-core.h"
  41#include "vivid-vid-common.h"
  42#include "vivid-vid-cap.h"
  43#include "vivid-vid-out.h"
  44#include "vivid-radio-common.h"
  45#include "vivid-radio-rx.h"
  46#include "vivid-radio-tx.h"
  47#include "vivid-sdr-cap.h"
  48#include "vivid-vbi-cap.h"
  49#include "vivid-vbi-out.h"
  50#include "vivid-osd.h"
  51#include "vivid-ctrls.h"
  52#include "vivid-kthread-cap.h"
  53
  54static inline v4l2_std_id vivid_get_std_cap(const struct vivid_dev *dev)
  55{
  56        if (vivid_is_sdtv_cap(dev))
  57                return dev->std_cap;
  58        return 0;
  59}
  60
  61static void copy_pix(struct vivid_dev *dev, int win_y, int win_x,
  62                        u16 *cap, const u16 *osd)
  63{
  64        u16 out;
  65        int left = dev->overlay_out_left;
  66        int top = dev->overlay_out_top;
  67        int fb_x = win_x + left;
  68        int fb_y = win_y + top;
  69        int i;
  70
  71        out = *cap;
  72        *cap = *osd;
  73        if (dev->bitmap_out) {
  74                const u8 *p = dev->bitmap_out;
  75                unsigned stride = (dev->compose_out.width + 7) / 8;
  76
  77                win_x -= dev->compose_out.left;
  78                win_y -= dev->compose_out.top;
  79                if (!(p[stride * win_y + win_x / 8] & (1 << (win_x & 7))))
  80                        return;
  81        }
  82
  83        for (i = 0; i < dev->clipcount_out; i++) {
  84                struct v4l2_rect *r = &dev->clips_out[i].c;
  85
  86                if (fb_y >= r->top && fb_y < r->top + r->height &&
  87                    fb_x >= r->left && fb_x < r->left + r->width)
  88                        return;
  89        }
  90        if ((dev->fbuf_out_flags & V4L2_FBUF_FLAG_CHROMAKEY) &&
  91            *osd != dev->chromakey_out)
  92                return;
  93        if ((dev->fbuf_out_flags & V4L2_FBUF_FLAG_SRC_CHROMAKEY) &&
  94            out == dev->chromakey_out)
  95                return;
  96        if (dev->fmt_cap->alpha_mask) {
  97                if ((dev->fbuf_out_flags & V4L2_FBUF_FLAG_GLOBAL_ALPHA) &&
  98                    dev->global_alpha_out)
  99                        return;
 100                if ((dev->fbuf_out_flags & V4L2_FBUF_FLAG_LOCAL_ALPHA) &&
 101                    *cap & dev->fmt_cap->alpha_mask)
 102                        return;
 103                if ((dev->fbuf_out_flags & V4L2_FBUF_FLAG_LOCAL_INV_ALPHA) &&
 104                    !(*cap & dev->fmt_cap->alpha_mask))
 105                        return;
 106        }
 107        *cap = out;
 108}
 109
 110static void blend_line(struct vivid_dev *dev, unsigned y_offset, unsigned x_offset,
 111                u8 *vcapbuf, const u8 *vosdbuf,
 112                unsigned width, unsigned pixsize)
 113{
 114        unsigned x;
 115
 116        for (x = 0; x < width; x++, vcapbuf += pixsize, vosdbuf += pixsize) {
 117                copy_pix(dev, y_offset, x_offset + x,
 118                         (u16 *)vcapbuf, (const u16 *)vosdbuf);
 119        }
 120}
 121
 122static void scale_line(const u8 *src, u8 *dst, unsigned srcw, unsigned dstw, unsigned twopixsize)
 123{
 124        /* Coarse scaling with Bresenham */
 125        unsigned int_part;
 126        unsigned fract_part;
 127        unsigned src_x = 0;
 128        unsigned error = 0;
 129        unsigned x;
 130
 131        /*
 132         * We always combine two pixels to prevent color bleed in the packed
 133         * yuv case.
 134         */
 135        srcw /= 2;
 136        dstw /= 2;
 137        int_part = srcw / dstw;
 138        fract_part = srcw % dstw;
 139        for (x = 0; x < dstw; x++, dst += twopixsize) {
 140                memcpy(dst, src + src_x * twopixsize, twopixsize);
 141                src_x += int_part;
 142                error += fract_part;
 143                if (error >= dstw) {
 144                        error -= dstw;
 145                        src_x++;
 146                }
 147        }
 148}
 149
 150/*
 151 * Precalculate the rectangles needed to perform video looping:
 152 *
 153 * The nominal pipeline is that the video output buffer is cropped by
 154 * crop_out, scaled to compose_out, overlaid with the output overlay,
 155 * cropped on the capture side by crop_cap and scaled again to the video
 156 * capture buffer using compose_cap.
 157 *
 158 * To keep things efficient we calculate the intersection of compose_out
 159 * and crop_cap (since that's the only part of the video that will
 160 * actually end up in the capture buffer), determine which part of the
 161 * video output buffer that is and which part of the video capture buffer
 162 * so we can scale the video straight from the output buffer to the capture
 163 * buffer without any intermediate steps.
 164 *
 165 * If we need to deal with an output overlay, then there is no choice and
 166 * that intermediate step still has to be taken. For the output overlay
 167 * support we calculate the intersection of the framebuffer and the overlay
 168 * window (which may be partially or wholly outside of the framebuffer
 169 * itself) and the intersection of that with loop_vid_copy (i.e. the part of
 170 * the actual looped video that will be overlaid). The result is calculated
 171 * both in framebuffer coordinates (loop_fb_copy) and compose_out coordinates
 172 * (loop_vid_overlay). Finally calculate the part of the capture buffer that
 173 * will receive that overlaid video.
 174 */
 175static void vivid_precalc_copy_rects(struct vivid_dev *dev)
 176{
 177        /* Framebuffer rectangle */
 178        struct v4l2_rect r_fb = {
 179                0, 0, dev->display_width, dev->display_height
 180        };
 181        /* Overlay window rectangle in framebuffer coordinates */
 182        struct v4l2_rect r_overlay = {
 183                dev->overlay_out_left, dev->overlay_out_top,
 184                dev->compose_out.width, dev->compose_out.height
 185        };
 186
 187        dev->loop_vid_copy = rect_intersect(&dev->crop_cap, &dev->compose_out);
 188
 189        dev->loop_vid_out = dev->loop_vid_copy;
 190        rect_scale(&dev->loop_vid_out, &dev->compose_out, &dev->crop_out);
 191        dev->loop_vid_out.left += dev->crop_out.left;
 192        dev->loop_vid_out.top += dev->crop_out.top;
 193
 194        dev->loop_vid_cap = dev->loop_vid_copy;
 195        rect_scale(&dev->loop_vid_cap, &dev->crop_cap, &dev->compose_cap);
 196
 197        dprintk(dev, 1,
 198                "loop_vid_copy: %dx%d@%dx%d loop_vid_out: %dx%d@%dx%d loop_vid_cap: %dx%d@%dx%d\n",
 199                dev->loop_vid_copy.width, dev->loop_vid_copy.height,
 200                dev->loop_vid_copy.left, dev->loop_vid_copy.top,
 201                dev->loop_vid_out.width, dev->loop_vid_out.height,
 202                dev->loop_vid_out.left, dev->loop_vid_out.top,
 203                dev->loop_vid_cap.width, dev->loop_vid_cap.height,
 204                dev->loop_vid_cap.left, dev->loop_vid_cap.top);
 205
 206        r_overlay = rect_intersect(&r_fb, &r_overlay);
 207
 208        /* shift r_overlay to the same origin as compose_out */
 209        r_overlay.left += dev->compose_out.left - dev->overlay_out_left;
 210        r_overlay.top += dev->compose_out.top - dev->overlay_out_top;
 211
 212        dev->loop_vid_overlay = rect_intersect(&r_overlay, &dev->loop_vid_copy);
 213        dev->loop_fb_copy = dev->loop_vid_overlay;
 214
 215        /* shift dev->loop_fb_copy back again to the fb origin */
 216        dev->loop_fb_copy.left -= dev->compose_out.left - dev->overlay_out_left;
 217        dev->loop_fb_copy.top -= dev->compose_out.top - dev->overlay_out_top;
 218
 219        dev->loop_vid_overlay_cap = dev->loop_vid_overlay;
 220        rect_scale(&dev->loop_vid_overlay_cap, &dev->crop_cap, &dev->compose_cap);
 221
 222        dprintk(dev, 1,
 223                "loop_fb_copy: %dx%d@%dx%d loop_vid_overlay: %dx%d@%dx%d loop_vid_overlay_cap: %dx%d@%dx%d\n",
 224                dev->loop_fb_copy.width, dev->loop_fb_copy.height,
 225                dev->loop_fb_copy.left, dev->loop_fb_copy.top,
 226                dev->loop_vid_overlay.width, dev->loop_vid_overlay.height,
 227                dev->loop_vid_overlay.left, dev->loop_vid_overlay.top,
 228                dev->loop_vid_overlay_cap.width, dev->loop_vid_overlay_cap.height,
 229                dev->loop_vid_overlay_cap.left, dev->loop_vid_overlay_cap.top);
 230}
 231
 232static void *plane_vaddr(struct tpg_data *tpg, struct vivid_buffer *buf,
 233                         unsigned p, unsigned bpl[TPG_MAX_PLANES], unsigned h)
 234{
 235        unsigned i;
 236        void *vbuf;
 237
 238        if (p == 0 || tpg_g_buffers(tpg) > 1)
 239                return vb2_plane_vaddr(&buf->vb, p);
 240        vbuf = vb2_plane_vaddr(&buf->vb, 0);
 241        for (i = 0; i < p; i++)
 242                vbuf += bpl[i] * h / tpg->vdownsampling[i];
 243        return vbuf;
 244}
 245
 246static int vivid_copy_buffer(struct vivid_dev *dev, unsigned p, u8 *vcapbuf,
 247                struct vivid_buffer *vid_cap_buf)
 248{
 249        bool blank = dev->must_blank[vid_cap_buf->vb.v4l2_buf.index];
 250        struct tpg_data *tpg = &dev->tpg;
 251        struct vivid_buffer *vid_out_buf = NULL;
 252        unsigned vdiv = dev->fmt_out->vdownsampling[p];
 253        unsigned twopixsize = tpg_g_twopixelsize(tpg, p);
 254        unsigned img_width = tpg_hdiv(tpg, p, dev->compose_cap.width);
 255        unsigned img_height = dev->compose_cap.height;
 256        unsigned stride_cap = tpg->bytesperline[p];
 257        unsigned stride_out = dev->bytesperline_out[p];
 258        unsigned stride_osd = dev->display_byte_stride;
 259        unsigned hmax = (img_height * tpg->perc_fill) / 100;
 260        u8 *voutbuf;
 261        u8 *vosdbuf = NULL;
 262        unsigned y;
 263        bool blend = dev->bitmap_out || dev->clipcount_out || dev->fbuf_out_flags;
 264        /* Coarse scaling with Bresenham */
 265        unsigned vid_out_int_part;
 266        unsigned vid_out_fract_part;
 267        unsigned vid_out_y = 0;
 268        unsigned vid_out_error = 0;
 269        unsigned vid_overlay_int_part = 0;
 270        unsigned vid_overlay_fract_part = 0;
 271        unsigned vid_overlay_y = 0;
 272        unsigned vid_overlay_error = 0;
 273        unsigned vid_cap_left = tpg_hdiv(tpg, p, dev->loop_vid_cap.left);
 274        unsigned vid_cap_right;
 275        bool quick;
 276
 277        vid_out_int_part = dev->loop_vid_out.height / dev->loop_vid_cap.height;
 278        vid_out_fract_part = dev->loop_vid_out.height % dev->loop_vid_cap.height;
 279
 280        if (!list_empty(&dev->vid_out_active))
 281                vid_out_buf = list_entry(dev->vid_out_active.next,
 282                                         struct vivid_buffer, list);
 283        if (vid_out_buf == NULL)
 284                return -ENODATA;
 285
 286        vid_cap_buf->vb.v4l2_buf.field = vid_out_buf->vb.v4l2_buf.field;
 287
 288        voutbuf = plane_vaddr(tpg, vid_out_buf, p,
 289                              dev->bytesperline_out, dev->fmt_out_rect.height);
 290        if (p < dev->fmt_out->buffers)
 291                voutbuf += vid_out_buf->vb.v4l2_planes[p].data_offset;
 292        voutbuf += tpg_hdiv(tpg, p, dev->loop_vid_out.left) +
 293                (dev->loop_vid_out.top / vdiv) * stride_out;
 294        vcapbuf += tpg_hdiv(tpg, p, dev->compose_cap.left) +
 295                (dev->compose_cap.top / vdiv) * stride_cap;
 296
 297        if (dev->loop_vid_copy.width == 0 || dev->loop_vid_copy.height == 0) {
 298                /*
 299                 * If there is nothing to copy, then just fill the capture window
 300                 * with black.
 301                 */
 302                for (y = 0; y < hmax / vdiv; y++, vcapbuf += stride_cap)
 303                        memcpy(vcapbuf, tpg->black_line[p], img_width);
 304                return 0;
 305        }
 306
 307        if (dev->overlay_out_enabled &&
 308            dev->loop_vid_overlay.width && dev->loop_vid_overlay.height) {
 309                vosdbuf = dev->video_vbase;
 310                vosdbuf += (dev->loop_fb_copy.left * twopixsize) / 2 +
 311                           dev->loop_fb_copy.top * stride_osd;
 312                vid_overlay_int_part = dev->loop_vid_overlay.height /
 313                                       dev->loop_vid_overlay_cap.height;
 314                vid_overlay_fract_part = dev->loop_vid_overlay.height %
 315                                         dev->loop_vid_overlay_cap.height;
 316        }
 317
 318        vid_cap_right = tpg_hdiv(tpg, p, dev->loop_vid_cap.left + dev->loop_vid_cap.width);
 319        /* quick is true if no video scaling is needed */
 320        quick = dev->loop_vid_out.width == dev->loop_vid_cap.width;
 321
 322        dev->cur_scaled_line = dev->loop_vid_out.height;
 323        for (y = 0; y < hmax; y += vdiv, vcapbuf += stride_cap) {
 324                /* osdline is true if this line requires overlay blending */
 325                bool osdline = vosdbuf && y >= dev->loop_vid_overlay_cap.top &&
 326                          y < dev->loop_vid_overlay_cap.top + dev->loop_vid_overlay_cap.height;
 327
 328                /*
 329                 * If this line of the capture buffer doesn't get any video, then
 330                 * just fill with black.
 331                 */
 332                if (y < dev->loop_vid_cap.top ||
 333                    y >= dev->loop_vid_cap.top + dev->loop_vid_cap.height) {
 334                        memcpy(vcapbuf, tpg->black_line[p], img_width);
 335                        continue;
 336                }
 337
 338                /* fill the left border with black */
 339                if (dev->loop_vid_cap.left)
 340                        memcpy(vcapbuf, tpg->black_line[p], vid_cap_left);
 341
 342                /* fill the right border with black */
 343                if (vid_cap_right < img_width)
 344                        memcpy(vcapbuf + vid_cap_right, tpg->black_line[p],
 345                                img_width - vid_cap_right);
 346
 347                if (quick && !osdline) {
 348                        memcpy(vcapbuf + vid_cap_left,
 349                               voutbuf + vid_out_y * stride_out,
 350                               tpg_hdiv(tpg, p, dev->loop_vid_cap.width));
 351                        goto update_vid_out_y;
 352                }
 353                if (dev->cur_scaled_line == vid_out_y) {
 354                        memcpy(vcapbuf + vid_cap_left, dev->scaled_line,
 355                               tpg_hdiv(tpg, p, dev->loop_vid_cap.width));
 356                        goto update_vid_out_y;
 357                }
 358                if (!osdline) {
 359                        scale_line(voutbuf + vid_out_y * stride_out, dev->scaled_line,
 360                                tpg_hdiv(tpg, p, dev->loop_vid_out.width),
 361                                tpg_hdiv(tpg, p, dev->loop_vid_cap.width),
 362                                tpg_g_twopixelsize(tpg, p));
 363                } else {
 364                        /*
 365                         * Offset in bytes within loop_vid_copy to the start of the
 366                         * loop_vid_overlay rectangle.
 367                         */
 368                        unsigned offset =
 369                                ((dev->loop_vid_overlay.left - dev->loop_vid_copy.left) *
 370                                 twopixsize) / 2;
 371                        u8 *osd = vosdbuf + vid_overlay_y * stride_osd;
 372
 373                        scale_line(voutbuf + vid_out_y * stride_out, dev->blended_line,
 374                                dev->loop_vid_out.width, dev->loop_vid_copy.width,
 375                                tpg_g_twopixelsize(tpg, p));
 376                        if (blend)
 377                                blend_line(dev, vid_overlay_y + dev->loop_vid_overlay.top,
 378                                           dev->loop_vid_overlay.left,
 379                                           dev->blended_line + offset, osd,
 380                                           dev->loop_vid_overlay.width, twopixsize / 2);
 381                        else
 382                                memcpy(dev->blended_line + offset,
 383                                       osd, (dev->loop_vid_overlay.width * twopixsize) / 2);
 384                        scale_line(dev->blended_line, dev->scaled_line,
 385                                        dev->loop_vid_copy.width, dev->loop_vid_cap.width,
 386                                        tpg_g_twopixelsize(tpg, p));
 387                }
 388                dev->cur_scaled_line = vid_out_y;
 389                memcpy(vcapbuf + vid_cap_left, dev->scaled_line,
 390                       tpg_hdiv(tpg, p, dev->loop_vid_cap.width));
 391
 392update_vid_out_y:
 393                if (osdline) {
 394                        vid_overlay_y += vid_overlay_int_part;
 395                        vid_overlay_error += vid_overlay_fract_part;
 396                        if (vid_overlay_error >= dev->loop_vid_overlay_cap.height) {
 397                                vid_overlay_error -= dev->loop_vid_overlay_cap.height;
 398                                vid_overlay_y++;
 399                        }
 400                }
 401                vid_out_y += vid_out_int_part;
 402                vid_out_error += vid_out_fract_part;
 403                if (vid_out_error >= dev->loop_vid_cap.height / vdiv) {
 404                        vid_out_error -= dev->loop_vid_cap.height / vdiv;
 405                        vid_out_y++;
 406                }
 407        }
 408
 409        if (!blank)
 410                return 0;
 411        for (; y < img_height; y += vdiv, vcapbuf += stride_cap)
 412                memcpy(vcapbuf, tpg->contrast_line[p], img_width);
 413        return 0;
 414}
 415
 416static void vivid_fillbuff(struct vivid_dev *dev, struct vivid_buffer *buf)
 417{
 418        struct tpg_data *tpg = &dev->tpg;
 419        unsigned factor = V4L2_FIELD_HAS_T_OR_B(dev->field_cap) ? 2 : 1;
 420        unsigned line_height = 16 / factor;
 421        bool is_tv = vivid_is_sdtv_cap(dev);
 422        bool is_60hz = is_tv && (dev->std_cap & V4L2_STD_525_60);
 423        unsigned p;
 424        int line = 1;
 425        u8 *basep[TPG_MAX_PLANES][2];
 426        unsigned ms;
 427        char str[100];
 428        s32 gain;
 429        bool is_loop = false;
 430
 431        if (dev->loop_video && dev->can_loop_video &&
 432            ((vivid_is_svid_cap(dev) && !VIVID_INVALID_SIGNAL(dev->std_signal_mode)) ||
 433             (vivid_is_hdmi_cap(dev) && !VIVID_INVALID_SIGNAL(dev->dv_timings_signal_mode))))
 434                is_loop = true;
 435
 436        buf->vb.v4l2_buf.sequence = dev->vid_cap_seq_count;
 437        /*
 438         * Take the timestamp now if the timestamp source is set to
 439         * "Start of Exposure".
 440         */
 441        if (dev->tstamp_src_is_soe)
 442                v4l2_get_timestamp(&buf->vb.v4l2_buf.timestamp);
 443        if (dev->field_cap == V4L2_FIELD_ALTERNATE) {
 444                /*
 445                 * 60 Hz standards start with the bottom field, 50 Hz standards
 446                 * with the top field. So if the 0-based seq_count is even,
 447                 * then the field is TOP for 50 Hz and BOTTOM for 60 Hz
 448                 * standards.
 449                 */
 450                buf->vb.v4l2_buf.field = ((dev->vid_cap_seq_count & 1) ^ is_60hz) ?
 451                        V4L2_FIELD_BOTTOM : V4L2_FIELD_TOP;
 452                /*
 453                 * The sequence counter counts frames, not fields. So divide
 454                 * by two.
 455                 */
 456                buf->vb.v4l2_buf.sequence /= 2;
 457        } else {
 458                buf->vb.v4l2_buf.field = dev->field_cap;
 459        }
 460        tpg_s_field(tpg, buf->vb.v4l2_buf.field,
 461                    dev->field_cap == V4L2_FIELD_ALTERNATE);
 462        tpg_s_perc_fill_blank(tpg, dev->must_blank[buf->vb.v4l2_buf.index]);
 463
 464        vivid_precalc_copy_rects(dev);
 465
 466        for (p = 0; p < tpg_g_planes(tpg); p++) {
 467                void *vbuf = plane_vaddr(tpg, buf, p,
 468                                         tpg->bytesperline, tpg->buf_height);
 469
 470                /*
 471                 * The first plane of a multiplanar format has a non-zero
 472                 * data_offset. This helps testing whether the application
 473                 * correctly supports non-zero data offsets.
 474                 */
 475                if (p < tpg_g_buffers(tpg) && dev->fmt_cap->data_offset[p]) {
 476                        memset(vbuf, dev->fmt_cap->data_offset[p] & 0xff,
 477                               dev->fmt_cap->data_offset[p]);
 478                        vbuf += dev->fmt_cap->data_offset[p];
 479                }
 480                tpg_calc_text_basep(tpg, basep, p, vbuf);
 481                if (!is_loop || vivid_copy_buffer(dev, p, vbuf, buf))
 482                        tpg_fill_plane_buffer(tpg, vivid_get_std_cap(dev), p, vbuf);
 483        }
 484        dev->must_blank[buf->vb.v4l2_buf.index] = false;
 485
 486        /* Updates stream time, only update at the start of a new frame. */
 487        if (dev->field_cap != V4L2_FIELD_ALTERNATE || (buf->vb.v4l2_buf.sequence & 1) == 0)
 488                dev->ms_vid_cap = jiffies_to_msecs(jiffies - dev->jiffies_vid_cap);
 489
 490        ms = dev->ms_vid_cap;
 491        if (dev->osd_mode <= 1) {
 492                snprintf(str, sizeof(str), " %02d:%02d:%02d:%03d %u%s",
 493                                (ms / (60 * 60 * 1000)) % 24,
 494                                (ms / (60 * 1000)) % 60,
 495                                (ms / 1000) % 60,
 496                                ms % 1000,
 497                                buf->vb.v4l2_buf.sequence,
 498                                (dev->field_cap == V4L2_FIELD_ALTERNATE) ?
 499                                        (buf->vb.v4l2_buf.field == V4L2_FIELD_TOP ?
 500                                         " top" : " bottom") : "");
 501                tpg_gen_text(tpg, basep, line++ * line_height, 16, str);
 502        }
 503        if (dev->osd_mode == 0) {
 504                snprintf(str, sizeof(str), " %dx%d, input %d ",
 505                                dev->src_rect.width, dev->src_rect.height, dev->input);
 506                tpg_gen_text(tpg, basep, line++ * line_height, 16, str);
 507
 508                gain = v4l2_ctrl_g_ctrl(dev->gain);
 509                mutex_lock(dev->ctrl_hdl_user_vid.lock);
 510                snprintf(str, sizeof(str),
 511                        " brightness %3d, contrast %3d, saturation %3d, hue %d ",
 512                        dev->brightness->cur.val,
 513                        dev->contrast->cur.val,
 514                        dev->saturation->cur.val,
 515                        dev->hue->cur.val);
 516                tpg_gen_text(tpg, basep, line++ * line_height, 16, str);
 517                snprintf(str, sizeof(str),
 518                        " autogain %d, gain %3d, alpha 0x%02x ",
 519                        dev->autogain->cur.val, gain, dev->alpha->cur.val);
 520                mutex_unlock(dev->ctrl_hdl_user_vid.lock);
 521                tpg_gen_text(tpg, basep, line++ * line_height, 16, str);
 522                mutex_lock(dev->ctrl_hdl_user_aud.lock);
 523                snprintf(str, sizeof(str),
 524                        " volume %3d, mute %d ",
 525                        dev->volume->cur.val, dev->mute->cur.val);
 526                mutex_unlock(dev->ctrl_hdl_user_aud.lock);
 527                tpg_gen_text(tpg, basep, line++ * line_height, 16, str);
 528                mutex_lock(dev->ctrl_hdl_user_gen.lock);
 529                snprintf(str, sizeof(str), " int32 %d, int64 %lld, bitmask %08x ",
 530                        dev->int32->cur.val,
 531                        *dev->int64->p_cur.p_s64,
 532                        dev->bitmask->cur.val);
 533                tpg_gen_text(tpg, basep, line++ * line_height, 16, str);
 534                snprintf(str, sizeof(str), " boolean %d, menu %s, string \"%s\" ",
 535                        dev->boolean->cur.val,
 536                        dev->menu->qmenu[dev->menu->cur.val],
 537                        dev->string->p_cur.p_char);
 538                tpg_gen_text(tpg, basep, line++ * line_height, 16, str);
 539                snprintf(str, sizeof(str), " integer_menu %lld, value %d ",
 540                        dev->int_menu->qmenu_int[dev->int_menu->cur.val],
 541                        dev->int_menu->cur.val);
 542                mutex_unlock(dev->ctrl_hdl_user_gen.lock);
 543                tpg_gen_text(tpg, basep, line++ * line_height, 16, str);
 544                if (dev->button_pressed) {
 545                        dev->button_pressed--;
 546                        snprintf(str, sizeof(str), " button pressed!");
 547                        tpg_gen_text(tpg, basep, line++ * line_height, 16, str);
 548                }
 549        }
 550
 551        /*
 552         * If "End of Frame" is specified at the timestamp source, then take
 553         * the timestamp now.
 554         */
 555        if (!dev->tstamp_src_is_soe)
 556                v4l2_get_timestamp(&buf->vb.v4l2_buf.timestamp);
 557        buf->vb.v4l2_buf.timestamp.tv_sec += dev->time_wrap_offset;
 558}
 559
 560/*
 561 * Return true if this pixel coordinate is a valid video pixel.
 562 */
 563static bool valid_pix(struct vivid_dev *dev, int win_y, int win_x, int fb_y, int fb_x)
 564{
 565        int i;
 566
 567        if (dev->bitmap_cap) {
 568                /*
 569                 * Only if the corresponding bit in the bitmap is set can
 570                 * the video pixel be shown. Coordinates are relative to
 571                 * the overlay window set by VIDIOC_S_FMT.
 572                 */
 573                const u8 *p = dev->bitmap_cap;
 574                unsigned stride = (dev->compose_cap.width + 7) / 8;
 575
 576                if (!(p[stride * win_y + win_x / 8] & (1 << (win_x & 7))))
 577                        return false;
 578        }
 579
 580        for (i = 0; i < dev->clipcount_cap; i++) {
 581                /*
 582                 * Only if the framebuffer coordinate is not in any of the
 583                 * clip rectangles will be video pixel be shown.
 584                 */
 585                struct v4l2_rect *r = &dev->clips_cap[i].c;
 586
 587                if (fb_y >= r->top && fb_y < r->top + r->height &&
 588                    fb_x >= r->left && fb_x < r->left + r->width)
 589                        return false;
 590        }
 591        return true;
 592}
 593
 594/*
 595 * Draw the image into the overlay buffer.
 596 * Note that the combination of overlay and multiplanar is not supported.
 597 */
 598static void vivid_overlay(struct vivid_dev *dev, struct vivid_buffer *buf)
 599{
 600        struct tpg_data *tpg = &dev->tpg;
 601        unsigned pixsize = tpg_g_twopixelsize(tpg, 0) / 2;
 602        void *vbase = dev->fb_vbase_cap;
 603        void *vbuf = vb2_plane_vaddr(&buf->vb, 0);
 604        unsigned img_width = dev->compose_cap.width;
 605        unsigned img_height = dev->compose_cap.height;
 606        unsigned stride = tpg->bytesperline[0];
 607        /* if quick is true, then valid_pix() doesn't have to be called */
 608        bool quick = dev->bitmap_cap == NULL && dev->clipcount_cap == 0;
 609        int x, y, w, out_x = 0;
 610
 611        /*
 612         * Overlay support is only supported for formats that have a twopixelsize
 613         * that's >= 2. Warn and bail out if that's not the case.
 614         */
 615        if (WARN_ON(pixsize == 0))
 616                return;
 617        if ((dev->overlay_cap_field == V4L2_FIELD_TOP ||
 618             dev->overlay_cap_field == V4L2_FIELD_BOTTOM) &&
 619            dev->overlay_cap_field != buf->vb.v4l2_buf.field)
 620                return;
 621
 622        vbuf += dev->compose_cap.left * pixsize + dev->compose_cap.top * stride;
 623        x = dev->overlay_cap_left;
 624        w = img_width;
 625        if (x < 0) {
 626                out_x = -x;
 627                w = w - out_x;
 628                x = 0;
 629        } else {
 630                w = dev->fb_cap.fmt.width - x;
 631                if (w > img_width)
 632                        w = img_width;
 633        }
 634        if (w <= 0)
 635                return;
 636        if (dev->overlay_cap_top >= 0)
 637                vbase += dev->overlay_cap_top * dev->fb_cap.fmt.bytesperline;
 638        for (y = dev->overlay_cap_top;
 639             y < dev->overlay_cap_top + (int)img_height;
 640             y++, vbuf += stride) {
 641                int px;
 642
 643                if (y < 0 || y > dev->fb_cap.fmt.height)
 644                        continue;
 645                if (quick) {
 646                        memcpy(vbase + x * pixsize,
 647                               vbuf + out_x * pixsize, w * pixsize);
 648                        vbase += dev->fb_cap.fmt.bytesperline;
 649                        continue;
 650                }
 651                for (px = 0; px < w; px++) {
 652                        if (!valid_pix(dev, y - dev->overlay_cap_top,
 653                                       px + out_x, y, px + x))
 654                                continue;
 655                        memcpy(vbase + (px + x) * pixsize,
 656                               vbuf + (px + out_x) * pixsize,
 657                               pixsize);
 658                }
 659                vbase += dev->fb_cap.fmt.bytesperline;
 660        }
 661}
 662
 663static void vivid_thread_vid_cap_tick(struct vivid_dev *dev, int dropped_bufs)
 664{
 665        struct vivid_buffer *vid_cap_buf = NULL;
 666        struct vivid_buffer *vbi_cap_buf = NULL;
 667
 668        dprintk(dev, 1, "Video Capture Thread Tick\n");
 669
 670        while (dropped_bufs-- > 1)
 671                tpg_update_mv_count(&dev->tpg,
 672                                dev->field_cap == V4L2_FIELD_NONE ||
 673                                dev->field_cap == V4L2_FIELD_ALTERNATE);
 674
 675        /* Drop a certain percentage of buffers. */
 676        if (dev->perc_dropped_buffers &&
 677            prandom_u32_max(100) < dev->perc_dropped_buffers)
 678                goto update_mv;
 679
 680        spin_lock(&dev->slock);
 681        if (!list_empty(&dev->vid_cap_active)) {
 682                vid_cap_buf = list_entry(dev->vid_cap_active.next, struct vivid_buffer, list);
 683                list_del(&vid_cap_buf->list);
 684        }
 685        if (!list_empty(&dev->vbi_cap_active)) {
 686                if (dev->field_cap != V4L2_FIELD_ALTERNATE ||
 687                    (dev->vbi_cap_seq_count & 1)) {
 688                        vbi_cap_buf = list_entry(dev->vbi_cap_active.next,
 689                                                 struct vivid_buffer, list);
 690                        list_del(&vbi_cap_buf->list);
 691                }
 692        }
 693        spin_unlock(&dev->slock);
 694
 695        if (!vid_cap_buf && !vbi_cap_buf)
 696                goto update_mv;
 697
 698        if (vid_cap_buf) {
 699                /* Fill buffer */
 700                vivid_fillbuff(dev, vid_cap_buf);
 701                dprintk(dev, 1, "filled buffer %d\n",
 702                        vid_cap_buf->vb.v4l2_buf.index);
 703
 704                /* Handle overlay */
 705                if (dev->overlay_cap_owner && dev->fb_cap.base &&
 706                                dev->fb_cap.fmt.pixelformat == dev->fmt_cap->fourcc)
 707                        vivid_overlay(dev, vid_cap_buf);
 708
 709                vb2_buffer_done(&vid_cap_buf->vb, dev->dqbuf_error ?
 710                                VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE);
 711                dprintk(dev, 2, "vid_cap buffer %d done\n",
 712                                vid_cap_buf->vb.v4l2_buf.index);
 713        }
 714
 715        if (vbi_cap_buf) {
 716                if (dev->stream_sliced_vbi_cap)
 717                        vivid_sliced_vbi_cap_process(dev, vbi_cap_buf);
 718                else
 719                        vivid_raw_vbi_cap_process(dev, vbi_cap_buf);
 720                vb2_buffer_done(&vbi_cap_buf->vb, dev->dqbuf_error ?
 721                                VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE);
 722                dprintk(dev, 2, "vbi_cap %d done\n",
 723                                vbi_cap_buf->vb.v4l2_buf.index);
 724        }
 725        dev->dqbuf_error = false;
 726
 727update_mv:
 728        /* Update the test pattern movement counters */
 729        tpg_update_mv_count(&dev->tpg, dev->field_cap == V4L2_FIELD_NONE ||
 730                                       dev->field_cap == V4L2_FIELD_ALTERNATE);
 731}
 732
 733static int vivid_thread_vid_cap(void *data)
 734{
 735        struct vivid_dev *dev = data;
 736        u64 numerators_since_start;
 737        u64 buffers_since_start;
 738        u64 next_jiffies_since_start;
 739        unsigned long jiffies_since_start;
 740        unsigned long cur_jiffies;
 741        unsigned wait_jiffies;
 742        unsigned numerator;
 743        unsigned denominator;
 744        int dropped_bufs;
 745
 746        dprintk(dev, 1, "Video Capture Thread Start\n");
 747
 748        set_freezable();
 749
 750        /* Resets frame counters */
 751        dev->cap_seq_offset = 0;
 752        dev->cap_seq_count = 0;
 753        dev->cap_seq_resync = false;
 754        dev->jiffies_vid_cap = jiffies;
 755
 756        for (;;) {
 757                try_to_freeze();
 758                if (kthread_should_stop())
 759                        break;
 760
 761                mutex_lock(&dev->mutex);
 762                cur_jiffies = jiffies;
 763                if (dev->cap_seq_resync) {
 764                        dev->jiffies_vid_cap = cur_jiffies;
 765                        dev->cap_seq_offset = dev->cap_seq_count + 1;
 766                        dev->cap_seq_count = 0;
 767                        dev->cap_seq_resync = false;
 768                }
 769                numerator = dev->timeperframe_vid_cap.numerator;
 770                denominator = dev->timeperframe_vid_cap.denominator;
 771
 772                if (dev->field_cap == V4L2_FIELD_ALTERNATE)
 773                        denominator *= 2;
 774
 775                /* Calculate the number of jiffies since we started streaming */
 776                jiffies_since_start = cur_jiffies - dev->jiffies_vid_cap;
 777                /* Get the number of buffers streamed since the start */
 778                buffers_since_start = (u64)jiffies_since_start * denominator +
 779                                      (HZ * numerator) / 2;
 780                do_div(buffers_since_start, HZ * numerator);
 781
 782                /*
 783                 * After more than 0xf0000000 (rounded down to a multiple of
 784                 * 'jiffies-per-day' to ease jiffies_to_msecs calculation)
 785                 * jiffies have passed since we started streaming reset the
 786                 * counters and keep track of the sequence offset.
 787                 */
 788                if (jiffies_since_start > JIFFIES_RESYNC) {
 789                        dev->jiffies_vid_cap = cur_jiffies;
 790                        dev->cap_seq_offset = buffers_since_start;
 791                        buffers_since_start = 0;
 792                }
 793                dropped_bufs = buffers_since_start + dev->cap_seq_offset - dev->cap_seq_count;
 794                dev->cap_seq_count = buffers_since_start + dev->cap_seq_offset;
 795                dev->vid_cap_seq_count = dev->cap_seq_count - dev->vid_cap_seq_start;
 796                dev->vbi_cap_seq_count = dev->cap_seq_count - dev->vbi_cap_seq_start;
 797
 798                vivid_thread_vid_cap_tick(dev, dropped_bufs);
 799
 800                /*
 801                 * Calculate the number of 'numerators' streamed since we started,
 802                 * including the current buffer.
 803                 */
 804                numerators_since_start = ++buffers_since_start * numerator;
 805
 806                /* And the number of jiffies since we started */
 807                jiffies_since_start = jiffies - dev->jiffies_vid_cap;
 808
 809                mutex_unlock(&dev->mutex);
 810
 811                /*
 812                 * Calculate when that next buffer is supposed to start
 813                 * in jiffies since we started streaming.
 814                 */
 815                next_jiffies_since_start = numerators_since_start * HZ +
 816                                           denominator / 2;
 817                do_div(next_jiffies_since_start, denominator);
 818                /* If it is in the past, then just schedule asap */
 819                if (next_jiffies_since_start < jiffies_since_start)
 820                        next_jiffies_since_start = jiffies_since_start;
 821
 822                wait_jiffies = next_jiffies_since_start - jiffies_since_start;
 823                schedule_timeout_interruptible(wait_jiffies ? wait_jiffies : 1);
 824        }
 825        dprintk(dev, 1, "Video Capture Thread End\n");
 826        return 0;
 827}
 828
 829static void vivid_grab_controls(struct vivid_dev *dev, bool grab)
 830{
 831        v4l2_ctrl_grab(dev->ctrl_has_crop_cap, grab);
 832        v4l2_ctrl_grab(dev->ctrl_has_compose_cap, grab);
 833        v4l2_ctrl_grab(dev->ctrl_has_scaler_cap, grab);
 834}
 835
 836int vivid_start_generating_vid_cap(struct vivid_dev *dev, bool *pstreaming)
 837{
 838        dprintk(dev, 1, "%s\n", __func__);
 839
 840        if (dev->kthread_vid_cap) {
 841                u32 seq_count = dev->cap_seq_count + dev->seq_wrap * 128;
 842
 843                if (pstreaming == &dev->vid_cap_streaming)
 844                        dev->vid_cap_seq_start = seq_count;
 845                else
 846                        dev->vbi_cap_seq_start = seq_count;
 847                *pstreaming = true;
 848                return 0;
 849        }
 850
 851        /* Resets frame counters */
 852        tpg_init_mv_count(&dev->tpg);
 853
 854        dev->vid_cap_seq_start = dev->seq_wrap * 128;
 855        dev->vbi_cap_seq_start = dev->seq_wrap * 128;
 856
 857        dev->kthread_vid_cap = kthread_run(vivid_thread_vid_cap, dev,
 858                        "%s-vid-cap", dev->v4l2_dev.name);
 859
 860        if (IS_ERR(dev->kthread_vid_cap)) {
 861                v4l2_err(&dev->v4l2_dev, "kernel_thread() failed\n");
 862                return PTR_ERR(dev->kthread_vid_cap);
 863        }
 864        *pstreaming = true;
 865        vivid_grab_controls(dev, true);
 866
 867        dprintk(dev, 1, "returning from %s\n", __func__);
 868        return 0;
 869}
 870
 871void vivid_stop_generating_vid_cap(struct vivid_dev *dev, bool *pstreaming)
 872{
 873        dprintk(dev, 1, "%s\n", __func__);
 874
 875        if (dev->kthread_vid_cap == NULL)
 876                return;
 877
 878        *pstreaming = false;
 879        if (pstreaming == &dev->vid_cap_streaming) {
 880                /* Release all active buffers */
 881                while (!list_empty(&dev->vid_cap_active)) {
 882                        struct vivid_buffer *buf;
 883
 884                        buf = list_entry(dev->vid_cap_active.next,
 885                                         struct vivid_buffer, list);
 886                        list_del(&buf->list);
 887                        vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
 888                        dprintk(dev, 2, "vid_cap buffer %d done\n",
 889                                buf->vb.v4l2_buf.index);
 890                }
 891        }
 892
 893        if (pstreaming == &dev->vbi_cap_streaming) {
 894                while (!list_empty(&dev->vbi_cap_active)) {
 895                        struct vivid_buffer *buf;
 896
 897                        buf = list_entry(dev->vbi_cap_active.next,
 898                                         struct vivid_buffer, list);
 899                        list_del(&buf->list);
 900                        vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
 901                        dprintk(dev, 2, "vbi_cap buffer %d done\n",
 902                                buf->vb.v4l2_buf.index);
 903                }
 904        }
 905
 906        if (dev->vid_cap_streaming || dev->vbi_cap_streaming)
 907                return;
 908
 909        /* shutdown control thread */
 910        vivid_grab_controls(dev, false);
 911        mutex_unlock(&dev->mutex);
 912        kthread_stop(dev->kthread_vid_cap);
 913        dev->kthread_vid_cap = NULL;
 914        mutex_lock(&dev->mutex);
 915}
 916