| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355 |
- // SPDX-License-Identifier: GPL-2.0
- /*
- * Copyright 2020-2021 NXP
- */
- #include <linux/init.h>
- #include <linux/interconnect.h>
- #include <linux/ioctl.h>
- #include <linux/list.h>
- #include <linux/kernel.h>
- #include <linux/module.h>
- #include <linux/delay.h>
- #include <linux/videodev2.h>
- #include <linux/ktime.h>
- #include <linux/rational.h>
- #include <media/v4l2-device.h>
- #include <media/v4l2-event.h>
- #include <media/v4l2-mem2mem.h>
- #include <media/v4l2-ioctl.h>
- #include <media/videobuf2-v4l2.h>
- #include <media/videobuf2-dma-contig.h>
- #include "vpu.h"
- #include "vpu_defs.h"
- #include "vpu_core.h"
- #include "vpu_helpers.h"
- #include "vpu_v4l2.h"
- #include "vpu_cmds.h"
- #include "vpu_rpc.h"
- #define VENC_OUTPUT_ENABLE BIT(0)
- #define VENC_CAPTURE_ENABLE BIT(1)
- #define VENC_ENABLE_MASK (VENC_OUTPUT_ENABLE | VENC_CAPTURE_ENABLE)
- #define VENC_MAX_BUF_CNT 8
- #define VENC_MIN_BUFFER_OUT 6
- #define VENC_MIN_BUFFER_CAP 6
- struct venc_t {
- struct vpu_encode_params params;
- u32 request_key_frame;
- u32 input_ready;
- u32 cpb_size;
- bool bitrate_change;
- struct vpu_buffer enc[VENC_MAX_BUF_CNT];
- struct vpu_buffer ref[VENC_MAX_BUF_CNT];
- struct vpu_buffer act[VENC_MAX_BUF_CNT];
- struct list_head frames;
- u32 frame_count;
- u32 encode_count;
- u32 ready_count;
- u32 enable;
- u32 stopped;
- u32 memory_resource_configured;
- u32 skipped_count;
- u32 skipped_bytes;
- wait_queue_head_t wq;
- };
- struct venc_frame_t {
- struct list_head list;
- struct vpu_enc_pic_info info;
- u32 bytesused;
- s64 timestamp;
- };
- static const struct vpu_format venc_formats[] = {
- {
- .pixfmt = V4L2_PIX_FMT_NV12M,
- .mem_planes = 2,
- .comp_planes = 2,
- .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
- .sibling = V4L2_PIX_FMT_NV12,
- },
- {
- .pixfmt = V4L2_PIX_FMT_NV12,
- .mem_planes = 1,
- .comp_planes = 2,
- .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
- .sibling = V4L2_PIX_FMT_NV12M,
- },
- {
- .pixfmt = V4L2_PIX_FMT_H264,
- .mem_planes = 1,
- .comp_planes = 1,
- .type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
- .flags = V4L2_FMT_FLAG_COMPRESSED
- },
- {0, 0, 0, 0},
- };
- static int venc_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
- {
- strscpy(cap->driver, "amphion-vpu", sizeof(cap->driver));
- strscpy(cap->card, "amphion vpu encoder", sizeof(cap->card));
- strscpy(cap->bus_info, "platform: amphion-vpu", sizeof(cap->bus_info));
- return 0;
- }
- static int venc_enum_fmt(struct file *file, void *fh, struct v4l2_fmtdesc *f)
- {
- struct vpu_inst *inst = to_inst(file);
- const struct vpu_format *fmt;
- memset(f->reserved, 0, sizeof(f->reserved));
- fmt = vpu_helper_enum_format(inst, f->type, f->index);
- if (!fmt)
- return -EINVAL;
- f->pixelformat = fmt->pixfmt;
- f->flags = fmt->flags;
- return 0;
- }
- static int venc_enum_framesizes(struct file *file, void *fh, struct v4l2_frmsizeenum *fsize)
- {
- struct vpu_inst *inst = to_inst(file);
- const struct vpu_core_resources *res;
- if (!fsize || fsize->index)
- return -EINVAL;
- if (!vpu_helper_find_format(inst, 0, fsize->pixel_format))
- return -EINVAL;
- res = vpu_get_resource(inst);
- if (!res)
- return -EINVAL;
- fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
- fsize->stepwise.max_width = res->max_width;
- fsize->stepwise.max_height = res->max_height;
- fsize->stepwise.min_width = res->min_width;
- fsize->stepwise.min_height = res->min_height;
- fsize->stepwise.step_width = res->step_width;
- fsize->stepwise.step_height = res->step_height;
- return 0;
- }
- static int venc_enum_frameintervals(struct file *file, void *fh, struct v4l2_frmivalenum *fival)
- {
- struct vpu_inst *inst = to_inst(file);
- const struct vpu_core_resources *res;
- if (!fival || fival->index)
- return -EINVAL;
- if (!vpu_helper_find_format(inst, 0, fival->pixel_format))
- return -EINVAL;
- if (!fival->width || !fival->height)
- return -EINVAL;
- res = vpu_get_resource(inst);
- if (!res)
- return -EINVAL;
- if (fival->width < res->min_width || fival->width > res->max_width ||
- fival->height < res->min_height || fival->height > res->max_height)
- return -EINVAL;
- fival->type = V4L2_FRMIVAL_TYPE_CONTINUOUS;
- fival->stepwise.min.numerator = 1;
- fival->stepwise.min.denominator = USHRT_MAX;
- fival->stepwise.max.numerator = USHRT_MAX;
- fival->stepwise.max.denominator = 1;
- fival->stepwise.step.numerator = 1;
- fival->stepwise.step.denominator = 1;
- return 0;
- }
- static int venc_g_fmt(struct file *file, void *fh, struct v4l2_format *f)
- {
- struct vpu_inst *inst = to_inst(file);
- struct venc_t *venc = inst->priv;
- struct v4l2_pix_format_mplane *pixmp = &f->fmt.pix_mp;
- struct vpu_format *cur_fmt;
- int i;
- cur_fmt = vpu_get_format(inst, f->type);
- pixmp->pixelformat = cur_fmt->pixfmt;
- pixmp->num_planes = cur_fmt->mem_planes;
- pixmp->width = cur_fmt->width;
- pixmp->height = cur_fmt->height;
- pixmp->field = cur_fmt->field;
- pixmp->flags = cur_fmt->flags;
- for (i = 0; i < pixmp->num_planes; i++) {
- pixmp->plane_fmt[i].bytesperline = cur_fmt->bytesperline[i];
- pixmp->plane_fmt[i].sizeimage = vpu_get_fmt_plane_size(cur_fmt, i);
- }
- f->fmt.pix_mp.colorspace = venc->params.color.primaries;
- f->fmt.pix_mp.xfer_func = venc->params.color.transfer;
- f->fmt.pix_mp.ycbcr_enc = venc->params.color.matrix;
- f->fmt.pix_mp.quantization = venc->params.color.full_range;
- return 0;
- }
- static int venc_try_fmt(struct file *file, void *fh, struct v4l2_format *f)
- {
- struct vpu_inst *inst = to_inst(file);
- struct vpu_format fmt;
- vpu_try_fmt_common(inst, f, &fmt);
- return 0;
- }
- static int venc_s_fmt(struct file *file, void *fh, struct v4l2_format *f)
- {
- struct vpu_inst *inst = to_inst(file);
- struct vpu_format fmt;
- struct vpu_format *cur_fmt;
- struct vb2_queue *q;
- struct venc_t *venc = inst->priv;
- struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
- q = v4l2_m2m_get_vq(inst->fh.m2m_ctx, f->type);
- if (vb2_is_busy(q))
- return -EBUSY;
- if (vpu_try_fmt_common(inst, f, &fmt))
- return -EINVAL;
- cur_fmt = vpu_get_format(inst, f->type);
- memcpy(cur_fmt, &fmt, sizeof(*cur_fmt));
- if (V4L2_TYPE_IS_OUTPUT(f->type)) {
- venc->params.input_format = cur_fmt->pixfmt;
- venc->params.src_stride = cur_fmt->bytesperline[0];
- venc->params.src_width = cur_fmt->width;
- venc->params.src_height = cur_fmt->height;
- venc->params.crop.left = 0;
- venc->params.crop.top = 0;
- venc->params.crop.width = cur_fmt->width;
- venc->params.crop.height = cur_fmt->height;
- } else {
- venc->params.codec_format = cur_fmt->pixfmt;
- venc->params.out_width = cur_fmt->width;
- venc->params.out_height = cur_fmt->height;
- }
- if (V4L2_TYPE_IS_OUTPUT(f->type)) {
- venc->params.color.primaries = pix_mp->colorspace;
- venc->params.color.transfer = pix_mp->xfer_func;
- venc->params.color.matrix = pix_mp->ycbcr_enc;
- venc->params.color.full_range = pix_mp->quantization;
- }
- pix_mp->colorspace = venc->params.color.primaries;
- pix_mp->xfer_func = venc->params.color.transfer;
- pix_mp->ycbcr_enc = venc->params.color.matrix;
- pix_mp->quantization = venc->params.color.full_range;
- return 0;
- }
- static int venc_g_parm(struct file *file, void *fh, struct v4l2_streamparm *parm)
- {
- struct vpu_inst *inst = to_inst(file);
- struct venc_t *venc = inst->priv;
- struct v4l2_fract *timeperframe;
- if (!parm)
- return -EINVAL;
- if (!V4L2_TYPE_IS_OUTPUT(parm->type))
- return -EINVAL;
- if (!vpu_helper_check_type(inst, parm->type))
- return -EINVAL;
- timeperframe = &parm->parm.capture.timeperframe;
- parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
- parm->parm.capture.readbuffers = 0;
- timeperframe->numerator = venc->params.frame_rate.numerator;
- timeperframe->denominator = venc->params.frame_rate.denominator;
- return 0;
- }
- static int venc_s_parm(struct file *file, void *fh, struct v4l2_streamparm *parm)
- {
- struct vpu_inst *inst = to_inst(file);
- struct venc_t *venc = inst->priv;
- struct v4l2_fract *timeperframe;
- unsigned long n, d;
- if (!parm)
- return -EINVAL;
- if (!V4L2_TYPE_IS_OUTPUT(parm->type))
- return -EINVAL;
- if (!vpu_helper_check_type(inst, parm->type))
- return -EINVAL;
- timeperframe = &parm->parm.capture.timeperframe;
- if (!timeperframe->numerator)
- timeperframe->numerator = venc->params.frame_rate.numerator;
- if (!timeperframe->denominator)
- timeperframe->denominator = venc->params.frame_rate.denominator;
- venc->params.frame_rate.numerator = timeperframe->numerator;
- venc->params.frame_rate.denominator = timeperframe->denominator;
- rational_best_approximation(venc->params.frame_rate.numerator,
- venc->params.frame_rate.denominator,
- venc->params.frame_rate.numerator,
- venc->params.frame_rate.denominator,
- &n, &d);
- venc->params.frame_rate.numerator = n;
- venc->params.frame_rate.denominator = d;
- parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
- memset(parm->parm.capture.reserved, 0, sizeof(parm->parm.capture.reserved));
- return 0;
- }
- static int venc_g_selection(struct file *file, void *fh, struct v4l2_selection *s)
- {
- struct vpu_inst *inst = to_inst(file);
- struct venc_t *venc = inst->priv;
- if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT && s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
- return -EINVAL;
- switch (s->target) {
- case V4L2_SEL_TGT_CROP_DEFAULT:
- case V4L2_SEL_TGT_CROP_BOUNDS:
- s->r.left = 0;
- s->r.top = 0;
- s->r.width = inst->out_format.width;
- s->r.height = inst->out_format.height;
- break;
- case V4L2_SEL_TGT_CROP:
- s->r = venc->params.crop;
- break;
- default:
- return -EINVAL;
- }
- return 0;
- }
- static int venc_valid_crop(struct venc_t *venc, const struct vpu_core_resources *res)
- {
- struct v4l2_rect *rect = NULL;
- u32 min_width;
- u32 min_height;
- u32 src_width;
- u32 src_height;
- rect = &venc->params.crop;
- min_width = res->min_width;
- min_height = res->min_height;
- src_width = venc->params.src_width;
- src_height = venc->params.src_height;
- if (rect->width == 0 || rect->height == 0)
- return -EINVAL;
- if (rect->left > src_width - min_width || rect->top > src_height - min_height)
- return -EINVAL;
- rect->width = min(rect->width, src_width - rect->left);
- rect->width = max_t(u32, rect->width, min_width);
- rect->height = min(rect->height, src_height - rect->top);
- rect->height = max_t(u32, rect->height, min_height);
- return 0;
- }
- static int venc_s_selection(struct file *file, void *fh, struct v4l2_selection *s)
- {
- struct vpu_inst *inst = to_inst(file);
- const struct vpu_core_resources *res;
- struct venc_t *venc = inst->priv;
- res = vpu_get_resource(inst);
- if (!res)
- return -EINVAL;
- if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT && s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
- return -EINVAL;
- if (s->target != V4L2_SEL_TGT_CROP)
- return -EINVAL;
- venc->params.crop.left = ALIGN(s->r.left, res->step_width);
- venc->params.crop.top = ALIGN(s->r.top, res->step_height);
- venc->params.crop.width = ALIGN(s->r.width, res->step_width);
- venc->params.crop.height = ALIGN(s->r.height, res->step_height);
- if (venc_valid_crop(venc, res)) {
- venc->params.crop.left = 0;
- venc->params.crop.top = 0;
- venc->params.crop.width = venc->params.src_width;
- venc->params.crop.height = venc->params.src_height;
- }
- inst->crop = venc->params.crop;
- return 0;
- }
- static int venc_drain(struct vpu_inst *inst)
- {
- struct venc_t *venc = inst->priv;
- int ret;
- if (!inst->fh.m2m_ctx)
- return 0;
- if (inst->state != VPU_CODEC_STATE_DRAIN)
- return 0;
- if (!vpu_is_source_empty(inst))
- return 0;
- if (!venc->input_ready)
- return 0;
- venc->input_ready = false;
- vpu_trace(inst->dev, "[%d]\n", inst->id);
- ret = vpu_session_stop(inst);
- if (ret)
- return ret;
- inst->state = VPU_CODEC_STATE_STOP;
- wake_up_all(&venc->wq);
- return 0;
- }
- static int venc_request_eos(struct vpu_inst *inst)
- {
- inst->state = VPU_CODEC_STATE_DRAIN;
- venc_drain(inst);
- return 0;
- }
- static int venc_encoder_cmd(struct file *file, void *fh, struct v4l2_encoder_cmd *cmd)
- {
- struct vpu_inst *inst = to_inst(file);
- int ret;
- ret = v4l2_m2m_ioctl_try_encoder_cmd(file, fh, cmd);
- if (ret)
- return ret;
- vpu_inst_lock(inst);
- if (cmd->cmd == V4L2_ENC_CMD_STOP) {
- if (inst->state == VPU_CODEC_STATE_DEINIT)
- vpu_set_last_buffer_dequeued(inst, true);
- else
- venc_request_eos(inst);
- }
- vpu_inst_unlock(inst);
- return 0;
- }
- static int venc_subscribe_event(struct v4l2_fh *fh, const struct v4l2_event_subscription *sub)
- {
- switch (sub->type) {
- case V4L2_EVENT_EOS:
- return v4l2_event_subscribe(fh, sub, 0, NULL);
- case V4L2_EVENT_CTRL:
- return v4l2_ctrl_subscribe_event(fh, sub);
- default:
- return -EINVAL;
- }
- }
- static const struct v4l2_ioctl_ops venc_ioctl_ops = {
- .vidioc_querycap = venc_querycap,
- .vidioc_enum_fmt_vid_cap = venc_enum_fmt,
- .vidioc_enum_fmt_vid_out = venc_enum_fmt,
- .vidioc_enum_framesizes = venc_enum_framesizes,
- .vidioc_enum_frameintervals = venc_enum_frameintervals,
- .vidioc_g_fmt_vid_cap_mplane = venc_g_fmt,
- .vidioc_g_fmt_vid_out_mplane = venc_g_fmt,
- .vidioc_try_fmt_vid_cap_mplane = venc_try_fmt,
- .vidioc_try_fmt_vid_out_mplane = venc_try_fmt,
- .vidioc_s_fmt_vid_cap_mplane = venc_s_fmt,
- .vidioc_s_fmt_vid_out_mplane = venc_s_fmt,
- .vidioc_g_parm = venc_g_parm,
- .vidioc_s_parm = venc_s_parm,
- .vidioc_g_selection = venc_g_selection,
- .vidioc_s_selection = venc_s_selection,
- .vidioc_try_encoder_cmd = v4l2_m2m_ioctl_try_encoder_cmd,
- .vidioc_encoder_cmd = venc_encoder_cmd,
- .vidioc_subscribe_event = venc_subscribe_event,
- .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
- .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
- .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
- .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
- .vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf,
- .vidioc_qbuf = v4l2_m2m_ioctl_qbuf,
- .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
- .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf,
- .vidioc_streamon = v4l2_m2m_ioctl_streamon,
- .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
- };
- static int venc_op_s_ctrl(struct v4l2_ctrl *ctrl)
- {
- struct vpu_inst *inst = ctrl_to_inst(ctrl);
- struct venc_t *venc = inst->priv;
- int ret = 0;
- switch (ctrl->id) {
- case V4L2_CID_MPEG_VIDEO_H264_PROFILE:
- venc->params.profile = ctrl->val;
- break;
- case V4L2_CID_MPEG_VIDEO_H264_LEVEL:
- venc->params.level = ctrl->val;
- break;
- case V4L2_CID_MPEG_VIDEO_FRAME_RC_ENABLE:
- venc->params.rc_enable = ctrl->val;
- break;
- case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
- venc->params.rc_mode = ctrl->val;
- break;
- case V4L2_CID_MPEG_VIDEO_BITRATE:
- if (ctrl->val != venc->params.bitrate)
- venc->bitrate_change = true;
- venc->params.bitrate = ctrl->val;
- break;
- case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:
- venc->params.bitrate_max = ctrl->val;
- break;
- case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
- venc->params.gop_length = ctrl->val;
- break;
- case V4L2_CID_MPEG_VIDEO_B_FRAMES:
- venc->params.bframes = ctrl->val;
- break;
- case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP:
- venc->params.i_frame_qp = ctrl->val;
- break;
- case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP:
- venc->params.p_frame_qp = ctrl->val;
- break;
- case V4L2_CID_MPEG_VIDEO_H264_B_FRAME_QP:
- venc->params.b_frame_qp = ctrl->val;
- break;
- case V4L2_CID_MPEG_VIDEO_FORCE_KEY_FRAME:
- venc->request_key_frame = 1;
- break;
- case V4L2_CID_MPEG_VIDEO_H264_CPB_SIZE:
- venc->cpb_size = ctrl->val * 1024;
- break;
- case V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_ENABLE:
- venc->params.sar.enable = ctrl->val;
- break;
- case V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_IDC:
- venc->params.sar.idc = ctrl->val;
- break;
- case V4L2_CID_MPEG_VIDEO_H264_VUI_EXT_SAR_WIDTH:
- venc->params.sar.width = ctrl->val;
- break;
- case V4L2_CID_MPEG_VIDEO_H264_VUI_EXT_SAR_HEIGHT:
- venc->params.sar.height = ctrl->val;
- break;
- case V4L2_CID_MPEG_VIDEO_HEADER_MODE:
- break;
- default:
- ret = -EINVAL;
- break;
- }
- return ret;
- }
- static const struct v4l2_ctrl_ops venc_ctrl_ops = {
- .s_ctrl = venc_op_s_ctrl,
- .g_volatile_ctrl = vpu_helper_g_volatile_ctrl,
- };
- static int venc_ctrl_init(struct vpu_inst *inst)
- {
- struct v4l2_ctrl *ctrl;
- int ret;
- ret = v4l2_ctrl_handler_init(&inst->ctrl_handler, 20);
- if (ret)
- return ret;
- v4l2_ctrl_new_std_menu(&inst->ctrl_handler, &venc_ctrl_ops,
- V4L2_CID_MPEG_VIDEO_H264_PROFILE,
- V4L2_MPEG_VIDEO_H264_PROFILE_HIGH,
- ~((1 << V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE) |
- (1 << V4L2_MPEG_VIDEO_H264_PROFILE_MAIN) |
- (1 << V4L2_MPEG_VIDEO_H264_PROFILE_HIGH)),
- V4L2_MPEG_VIDEO_H264_PROFILE_HIGH);
- v4l2_ctrl_new_std_menu(&inst->ctrl_handler, &venc_ctrl_ops,
- V4L2_CID_MPEG_VIDEO_H264_LEVEL,
- V4L2_MPEG_VIDEO_H264_LEVEL_5_1,
- 0x0,
- V4L2_MPEG_VIDEO_H264_LEVEL_4_0);
- v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
- V4L2_CID_MPEG_VIDEO_FRAME_RC_ENABLE, 0, 1, 1, 1);
- v4l2_ctrl_new_std_menu(&inst->ctrl_handler, &venc_ctrl_ops,
- V4L2_CID_MPEG_VIDEO_BITRATE_MODE,
- V4L2_MPEG_VIDEO_BITRATE_MODE_CBR,
- ~((1 << V4L2_MPEG_VIDEO_BITRATE_MODE_VBR) |
- (1 << V4L2_MPEG_VIDEO_BITRATE_MODE_CBR)),
- V4L2_MPEG_VIDEO_BITRATE_MODE_CBR);
- v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
- V4L2_CID_MPEG_VIDEO_BITRATE,
- BITRATE_MIN,
- BITRATE_MAX,
- BITRATE_STEP,
- BITRATE_DEFAULT);
- v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
- V4L2_CID_MPEG_VIDEO_BITRATE_PEAK,
- BITRATE_MIN, BITRATE_MAX,
- BITRATE_STEP,
- BITRATE_DEFAULT_PEAK);
- v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
- V4L2_CID_MPEG_VIDEO_GOP_SIZE, 1, 8000, 1, 30);
- v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
- V4L2_CID_MPEG_VIDEO_B_FRAMES, 0, 4, 1, 0);
- v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
- V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP, 1, 51, 1, 26);
- v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
- V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP, 1, 51, 1, 28);
- v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
- V4L2_CID_MPEG_VIDEO_H264_B_FRAME_QP, 1, 51, 1, 30);
- v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
- V4L2_CID_MPEG_VIDEO_FORCE_KEY_FRAME, 0, 0, 0, 0);
- ctrl = v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
- V4L2_CID_MIN_BUFFERS_FOR_CAPTURE, 1, 32, 1, 2);
- if (ctrl)
- ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE;
- ctrl = v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
- V4L2_CID_MIN_BUFFERS_FOR_OUTPUT, 1, 32, 1, 2);
- if (ctrl)
- ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE;
- v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
- V4L2_CID_MPEG_VIDEO_H264_CPB_SIZE, 64, 10240, 1, 1024);
- v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
- V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_ENABLE, 0, 1, 1, 1);
- v4l2_ctrl_new_std_menu(&inst->ctrl_handler, &venc_ctrl_ops,
- V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_IDC,
- V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_EXTENDED,
- 0x0,
- V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_1x1);
- v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
- V4L2_CID_MPEG_VIDEO_H264_VUI_EXT_SAR_WIDTH,
- 0, USHRT_MAX, 1, 1);
- v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
- V4L2_CID_MPEG_VIDEO_H264_VUI_EXT_SAR_HEIGHT,
- 0, USHRT_MAX, 1, 1);
- v4l2_ctrl_new_std_menu(&inst->ctrl_handler, &venc_ctrl_ops,
- V4L2_CID_MPEG_VIDEO_HEADER_MODE,
- V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME,
- ~(1 << V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME),
- V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME);
- v4l2_ctrl_new_std(&inst->ctrl_handler, NULL,
- V4L2_CID_MPEG_VIDEO_AVERAGE_QP, 0, 51, 1, 0);
- if (inst->ctrl_handler.error) {
- ret = inst->ctrl_handler.error;
- v4l2_ctrl_handler_free(&inst->ctrl_handler);
- return ret;
- }
- ret = v4l2_ctrl_handler_setup(&inst->ctrl_handler);
- if (ret) {
- dev_err(inst->dev, "[%d] setup ctrls fail, ret = %d\n", inst->id, ret);
- v4l2_ctrl_handler_free(&inst->ctrl_handler);
- return ret;
- }
- return 0;
- }
- static bool venc_check_ready(struct vpu_inst *inst, unsigned int type)
- {
- struct venc_t *venc = inst->priv;
- if (V4L2_TYPE_IS_OUTPUT(type)) {
- if (vpu_helper_get_free_space(inst) < venc->cpb_size)
- return false;
- return venc->input_ready;
- }
- if (list_empty(&venc->frames))
- return false;
- return true;
- }
- static u32 venc_get_enable_mask(u32 type)
- {
- if (V4L2_TYPE_IS_OUTPUT(type))
- return VENC_OUTPUT_ENABLE;
- else
- return VENC_CAPTURE_ENABLE;
- }
- static void venc_set_enable(struct venc_t *venc, u32 type, int enable)
- {
- u32 mask = venc_get_enable_mask(type);
- if (enable)
- venc->enable |= mask;
- else
- venc->enable &= ~mask;
- }
- static u32 venc_get_enable(struct venc_t *venc, u32 type)
- {
- return venc->enable & venc_get_enable_mask(type);
- }
- static void venc_input_done(struct vpu_inst *inst)
- {
- struct venc_t *venc = inst->priv;
- vpu_inst_lock(inst);
- venc->input_ready = true;
- vpu_process_output_buffer(inst);
- if (inst->state == VPU_CODEC_STATE_DRAIN)
- venc_drain(inst);
- vpu_inst_unlock(inst);
- }
- /*
- * It's hardware limitation, that there may be several bytes
- * redundant data at the beginning of frame.
- * For android platform, the redundant data may cause cts test fail
- * So driver will strip them
- */
- static int venc_precheck_encoded_frame(struct vpu_inst *inst, struct venc_frame_t *frame)
- {
- struct venc_t *venc;
- int skipped;
- if (!frame || !frame->bytesused)
- return -EINVAL;
- venc = inst->priv;
- skipped = vpu_helper_find_startcode(&inst->stream_buffer,
- inst->cap_format.pixfmt,
- frame->info.wptr - inst->stream_buffer.phys,
- frame->bytesused);
- if (skipped > 0) {
- frame->bytesused -= skipped;
- frame->info.wptr = vpu_helper_step_walk(&inst->stream_buffer,
- frame->info.wptr, skipped);
- venc->skipped_bytes += skipped;
- venc->skipped_count++;
- }
- return 0;
- }
- static int venc_get_one_encoded_frame(struct vpu_inst *inst,
- struct venc_frame_t *frame,
- struct vb2_v4l2_buffer *vbuf)
- {
- struct venc_t *venc = inst->priv;
- struct vb2_v4l2_buffer *src_buf;
- if (!vbuf)
- return -EAGAIN;
- src_buf = vpu_find_buf_by_sequence(inst, inst->out_format.type, frame->info.frame_id);
- if (src_buf) {
- v4l2_m2m_buf_copy_metadata(src_buf, vbuf);
- vpu_set_buffer_state(src_buf, VPU_BUF_STATE_IDLE);
- v4l2_m2m_src_buf_remove_by_buf(inst->fh.m2m_ctx, src_buf);
- v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
- } else {
- vbuf->vb2_buf.timestamp = frame->info.timestamp;
- }
- if (!venc_get_enable(inst->priv, vbuf->vb2_buf.type)) {
- v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_ERROR);
- return 0;
- }
- if (frame->bytesused > vbuf->vb2_buf.planes[0].length) {
- v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_ERROR);
- return -ENOMEM;
- }
- venc_precheck_encoded_frame(inst, frame);
- if (frame->bytesused) {
- u32 rptr = frame->info.wptr;
- void *dst = vb2_plane_vaddr(&vbuf->vb2_buf, 0);
- vpu_helper_copy_from_stream_buffer(&inst->stream_buffer,
- &rptr, frame->bytesused, dst);
- vpu_iface_update_stream_buffer(inst, rptr, 0);
- }
- vb2_set_plane_payload(&vbuf->vb2_buf, 0, frame->bytesused);
- vbuf->sequence = frame->info.frame_id;
- vbuf->field = inst->cap_format.field;
- vbuf->flags |= frame->info.pic_type;
- vpu_set_buffer_state(vbuf, VPU_BUF_STATE_IDLE);
- vpu_set_buffer_average_qp(vbuf, frame->info.average_qp);
- dev_dbg(inst->dev, "[%d][OUTPUT TS]%32lld\n", inst->id, vbuf->vb2_buf.timestamp);
- v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_DONE);
- venc->ready_count++;
- if (vbuf->flags & V4L2_BUF_FLAG_KEYFRAME)
- dev_dbg(inst->dev, "[%d][%d]key frame\n", inst->id, frame->info.frame_id);
- return 0;
- }
- static int venc_get_encoded_frames(struct vpu_inst *inst)
- {
- struct venc_t *venc;
- struct venc_frame_t *frame;
- struct venc_frame_t *tmp;
- if (!inst->fh.m2m_ctx)
- return 0;
- venc = inst->priv;
- list_for_each_entry_safe(frame, tmp, &venc->frames, list) {
- if (venc_get_one_encoded_frame(inst, frame,
- v4l2_m2m_dst_buf_remove(inst->fh.m2m_ctx)))
- break;
- list_del_init(&frame->list);
- kfree(frame);
- }
- return 0;
- }
- static int venc_frame_encoded(struct vpu_inst *inst, void *arg)
- {
- struct vpu_enc_pic_info *info = arg;
- struct venc_frame_t *frame;
- struct venc_t *venc;
- int ret = 0;
- if (!info)
- return -EINVAL;
- venc = inst->priv;
- frame = kzalloc_obj(*frame);
- if (!frame)
- return -ENOMEM;
- memcpy(&frame->info, info, sizeof(frame->info));
- frame->bytesused = info->frame_size;
- vpu_inst_lock(inst);
- list_add_tail(&frame->list, &venc->frames);
- venc->encode_count++;
- venc_get_encoded_frames(inst);
- vpu_inst_unlock(inst);
- return ret;
- }
- static void venc_set_last_buffer_dequeued(struct vpu_inst *inst)
- {
- struct venc_t *venc = inst->priv;
- if (venc->stopped && list_empty(&venc->frames))
- vpu_set_last_buffer_dequeued(inst, true);
- }
- static void venc_stop_done(struct vpu_inst *inst)
- {
- struct venc_t *venc = inst->priv;
- vpu_inst_lock(inst);
- venc->stopped = true;
- venc_set_last_buffer_dequeued(inst);
- vpu_inst_unlock(inst);
- wake_up_all(&venc->wq);
- }
- static void venc_event_notify(struct vpu_inst *inst, u32 event, void *data)
- {
- }
- static void venc_release(struct vpu_inst *inst)
- {
- }
- static void venc_cleanup(struct vpu_inst *inst)
- {
- struct venc_t *venc;
- if (!inst)
- return;
- venc = inst->priv;
- kfree(venc);
- inst->priv = NULL;
- kfree(inst);
- }
- static int venc_start_session(struct vpu_inst *inst, u32 type)
- {
- struct venc_t *venc = inst->priv;
- int stream_buffer_size;
- int ret;
- venc_set_enable(venc, type, 1);
- if ((venc->enable & VENC_ENABLE_MASK) != VENC_ENABLE_MASK)
- return 0;
- vpu_iface_init_instance(inst);
- stream_buffer_size = vpu_iface_get_stream_buffer_size(inst->core);
- if (stream_buffer_size > 0) {
- inst->stream_buffer.length = max_t(u32, stream_buffer_size, venc->cpb_size * 3);
- ret = vpu_alloc_dma(inst->core, &inst->stream_buffer);
- if (ret)
- goto error;
- inst->use_stream_buffer = true;
- vpu_iface_config_stream_buffer(inst, &inst->stream_buffer);
- }
- ret = vpu_iface_set_encode_params(inst, &venc->params, 0);
- if (ret)
- goto error;
- venc->memory_resource_configured = false;
- ret = vpu_session_configure_codec(inst);
- if (ret)
- goto error;
- if (!venc->memory_resource_configured) {
- vb2_queue_error(v4l2_m2m_get_src_vq(inst->fh.m2m_ctx));
- vb2_queue_error(v4l2_m2m_get_dst_vq(inst->fh.m2m_ctx));
- ret = -ENOMEM;
- goto error;
- }
- inst->state = VPU_CODEC_STATE_CONFIGURED;
- /*vpu_iface_config_memory_resource*/
- /*config enc expert mode parameter*/
- ret = vpu_iface_set_encode_params(inst, &venc->params, 1);
- if (ret)
- goto error;
- ret = vpu_session_start(inst);
- if (ret)
- goto error;
- inst->state = VPU_CODEC_STATE_STARTED;
- venc->bitrate_change = false;
- venc->input_ready = true;
- venc->frame_count = 0;
- venc->encode_count = 0;
- venc->ready_count = 0;
- venc->stopped = false;
- vpu_process_output_buffer(inst);
- if (venc->frame_count == 0)
- dev_err(inst->dev, "[%d] there is no input when starting\n", inst->id);
- return 0;
- error:
- venc_set_enable(venc, type, 0);
- inst->state = VPU_CODEC_STATE_DEINIT;
- vpu_free_dma(&inst->stream_buffer);
- return ret;
- }
- static void venc_cleanup_mem_resource(struct vpu_inst *inst)
- {
- struct venc_t *venc;
- u32 i;
- venc = inst->priv;
- venc->memory_resource_configured = false;
- for (i = 0; i < ARRAY_SIZE(venc->enc); i++)
- vpu_free_dma(&venc->enc[i]);
- for (i = 0; i < ARRAY_SIZE(venc->ref); i++)
- vpu_free_dma(&venc->ref[i]);
- }
- static void venc_request_mem_resource(struct vpu_inst *inst,
- u32 enc_frame_size,
- u32 enc_frame_num,
- u32 ref_frame_size,
- u32 ref_frame_num,
- u32 act_frame_size,
- u32 act_frame_num)
- {
- struct venc_t *venc;
- u32 i;
- int ret;
- venc = inst->priv;
- if (enc_frame_num > ARRAY_SIZE(venc->enc)) {
- dev_err(inst->dev, "[%d] enc num(%d) is out of range\n", inst->id, enc_frame_num);
- return;
- }
- if (ref_frame_num > ARRAY_SIZE(venc->ref)) {
- dev_err(inst->dev, "[%d] ref num(%d) is out of range\n", inst->id, ref_frame_num);
- return;
- }
- if (act_frame_num > ARRAY_SIZE(venc->act)) {
- dev_err(inst->dev, "[%d] act num(%d) is out of range\n", inst->id, act_frame_num);
- return;
- }
- for (i = 0; i < enc_frame_num; i++) {
- venc->enc[i].length = enc_frame_size;
- ret = vpu_alloc_dma(inst->core, &venc->enc[i]);
- if (ret) {
- venc_cleanup_mem_resource(inst);
- return;
- }
- }
- for (i = 0; i < ref_frame_num; i++) {
- venc->ref[i].length = ref_frame_size;
- ret = vpu_alloc_dma(inst->core, &venc->ref[i]);
- if (ret) {
- venc_cleanup_mem_resource(inst);
- return;
- }
- }
- if (act_frame_num != 1 || act_frame_size > inst->act.length) {
- venc_cleanup_mem_resource(inst);
- return;
- }
- venc->act[0].length = act_frame_size;
- venc->act[0].phys = inst->act.phys;
- venc->act[0].virt = inst->act.virt;
- for (i = 0; i < enc_frame_num; i++)
- vpu_iface_config_memory_resource(inst, MEM_RES_ENC, i, &venc->enc[i]);
- for (i = 0; i < ref_frame_num; i++)
- vpu_iface_config_memory_resource(inst, MEM_RES_REF, i, &venc->ref[i]);
- for (i = 0; i < act_frame_num; i++)
- vpu_iface_config_memory_resource(inst, MEM_RES_ACT, i, &venc->act[i]);
- venc->memory_resource_configured = true;
- }
- static void venc_cleanup_frames(struct venc_t *venc)
- {
- struct venc_frame_t *frame;
- struct venc_frame_t *tmp;
- list_for_each_entry_safe(frame, tmp, &venc->frames, list) {
- list_del_init(&frame->list);
- kfree(frame);
- }
- }
- static int venc_stop_session(struct vpu_inst *inst, u32 type)
- {
- struct venc_t *venc = inst->priv;
- venc_set_enable(venc, type, 0);
- if (venc->enable & VENC_ENABLE_MASK)
- return 0;
- if (inst->state == VPU_CODEC_STATE_DEINIT)
- return 0;
- if (inst->state != VPU_CODEC_STATE_STOP)
- venc_request_eos(inst);
- call_void_vop(inst, wait_prepare);
- if (!wait_event_timeout(venc->wq, venc->stopped, VPU_TIMEOUT)) {
- set_bit(inst->id, &inst->core->hang_mask);
- vpu_session_debug(inst);
- }
- call_void_vop(inst, wait_finish);
- inst->state = VPU_CODEC_STATE_DEINIT;
- venc_cleanup_frames(inst->priv);
- vpu_free_dma(&inst->stream_buffer);
- venc_cleanup_mem_resource(inst);
- return 0;
- }
- static int venc_process_output(struct vpu_inst *inst, struct vb2_buffer *vb)
- {
- struct venc_t *venc = inst->priv;
- struct vb2_v4l2_buffer *vbuf;
- u32 flags;
- if (inst->state == VPU_CODEC_STATE_DEINIT)
- return -EINVAL;
- vbuf = to_vb2_v4l2_buffer(vb);
- if (inst->state == VPU_CODEC_STATE_STARTED)
- inst->state = VPU_CODEC_STATE_ACTIVE;
- flags = vbuf->flags;
- if (venc->request_key_frame) {
- vbuf->flags |= V4L2_BUF_FLAG_KEYFRAME;
- venc->request_key_frame = 0;
- }
- if (venc->bitrate_change) {
- vpu_session_update_parameters(inst, &venc->params);
- venc->bitrate_change = false;
- }
- dev_dbg(inst->dev, "[%d][INPUT TS]%32lld\n", inst->id, vb->timestamp);
- vpu_iface_input_frame(inst, vb);
- vbuf->flags = flags;
- venc->input_ready = false;
- venc->frame_count++;
- vpu_set_buffer_state(vbuf, VPU_BUF_STATE_INUSE);
- return 0;
- }
- static int venc_process_capture(struct vpu_inst *inst, struct vb2_buffer *vb)
- {
- struct venc_t *venc;
- struct venc_frame_t *frame = NULL;
- struct vb2_v4l2_buffer *vbuf;
- int ret;
- venc = inst->priv;
- if (list_empty(&venc->frames))
- return -EINVAL;
- frame = list_first_entry(&venc->frames, struct venc_frame_t, list);
- vbuf = to_vb2_v4l2_buffer(vb);
- v4l2_m2m_dst_buf_remove_by_buf(inst->fh.m2m_ctx, vbuf);
- ret = venc_get_one_encoded_frame(inst, frame, vbuf);
- if (ret)
- return ret;
- list_del_init(&frame->list);
- kfree(frame);
- return 0;
- }
- static void venc_on_queue_empty(struct vpu_inst *inst, u32 type)
- {
- struct venc_t *venc = inst->priv;
- if (V4L2_TYPE_IS_OUTPUT(type))
- return;
- if (venc->stopped)
- venc_set_last_buffer_dequeued(inst);
- }
- static int venc_get_debug_info(struct vpu_inst *inst, char *str, u32 size, u32 i)
- {
- struct venc_t *venc = inst->priv;
- int num = -1;
- switch (i) {
- case 0:
- num = scnprintf(str, size, "profile = %d\n", venc->params.profile);
- break;
- case 1:
- num = scnprintf(str, size, "level = %d\n", venc->params.level);
- break;
- case 2:
- num = scnprintf(str, size, "fps = %d/%d\n",
- venc->params.frame_rate.numerator,
- venc->params.frame_rate.denominator);
- break;
- case 3:
- num = scnprintf(str, size, "%d x %d -> %d x %d\n",
- venc->params.src_width,
- venc->params.src_height,
- venc->params.out_width,
- venc->params.out_height);
- break;
- case 4:
- num = scnprintf(str, size, "(%d, %d) %d x %d\n",
- venc->params.crop.left,
- venc->params.crop.top,
- venc->params.crop.width,
- venc->params.crop.height);
- break;
- case 5:
- num = scnprintf(str, size,
- "enable = 0x%x, input = %d, encode = %d, ready = %d, stopped = %d\n",
- venc->enable,
- venc->frame_count, venc->encode_count,
- venc->ready_count,
- venc->stopped);
- break;
- case 6:
- num = scnprintf(str, size, "gop = %d\n", venc->params.gop_length);
- break;
- case 7:
- num = scnprintf(str, size, "bframes = %d\n", venc->params.bframes);
- break;
- case 8:
- num = scnprintf(str, size, "rc: %s, mode = %d, bitrate = %d(%d), qp = %d\n",
- venc->params.rc_enable ? "enable" : "disable",
- venc->params.rc_mode,
- venc->params.bitrate,
- venc->params.bitrate_max,
- venc->params.i_frame_qp);
- break;
- case 9:
- num = scnprintf(str, size, "sar: enable = %d, idc = %d, %d x %d\n",
- venc->params.sar.enable,
- venc->params.sar.idc,
- venc->params.sar.width,
- venc->params.sar.height);
- break;
- case 10:
- num = scnprintf(str, size,
- "colorspace: primaries = %d, transfer = %d, matrix = %d, full_range = %d\n",
- venc->params.color.primaries,
- venc->params.color.transfer,
- venc->params.color.matrix,
- venc->params.color.full_range);
- break;
- case 11:
- num = scnprintf(str, size, "skipped: count = %d, bytes = %d\n",
- venc->skipped_count, venc->skipped_bytes);
- break;
- default:
- break;
- }
- return num;
- }
- static struct vpu_inst_ops venc_inst_ops = {
- .ctrl_init = venc_ctrl_init,
- .check_ready = venc_check_ready,
- .input_done = venc_input_done,
- .get_one_frame = venc_frame_encoded,
- .stop_done = venc_stop_done,
- .event_notify = venc_event_notify,
- .release = venc_release,
- .cleanup = venc_cleanup,
- .start = venc_start_session,
- .mem_request = venc_request_mem_resource,
- .stop = venc_stop_session,
- .process_output = venc_process_output,
- .process_capture = venc_process_capture,
- .on_queue_empty = venc_on_queue_empty,
- .get_debug_info = venc_get_debug_info,
- .wait_prepare = vpu_inst_unlock,
- .wait_finish = vpu_inst_lock,
- };
- static void venc_init(struct file *file)
- {
- struct vpu_inst *inst = to_inst(file);
- struct venc_t *venc;
- struct v4l2_format f;
- struct v4l2_streamparm parm;
- venc = inst->priv;
- venc->params.qp_min = 1;
- venc->params.qp_max = 51;
- venc->params.qp_min_i = 1;
- venc->params.qp_max_i = 51;
- venc->params.bitrate_min = BITRATE_MIN;
- memset(&f, 0, sizeof(f));
- f.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
- f.fmt.pix_mp.pixelformat = V4L2_PIX_FMT_NV12M;
- f.fmt.pix_mp.width = 1280;
- f.fmt.pix_mp.height = 720;
- f.fmt.pix_mp.field = V4L2_FIELD_NONE;
- venc_s_fmt(file, &inst->fh, &f);
- memset(&f, 0, sizeof(f));
- f.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
- f.fmt.pix_mp.pixelformat = V4L2_PIX_FMT_H264;
- f.fmt.pix_mp.width = 1280;
- f.fmt.pix_mp.height = 720;
- f.fmt.pix_mp.field = V4L2_FIELD_NONE;
- venc_s_fmt(file, &inst->fh, &f);
- memset(&parm, 0, sizeof(parm));
- parm.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
- parm.parm.capture.timeperframe.numerator = 1;
- parm.parm.capture.timeperframe.denominator = 30;
- venc_s_parm(file, &inst->fh, &parm);
- }
- static int venc_open(struct file *file)
- {
- struct vpu_inst *inst;
- struct venc_t *venc;
- int ret;
- inst = kzalloc_obj(*inst);
- if (!inst)
- return -ENOMEM;
- venc = kzalloc_obj(*venc);
- if (!venc) {
- kfree(inst);
- return -ENOMEM;
- }
- inst->ops = &venc_inst_ops;
- inst->formats = venc_formats;
- inst->type = VPU_CORE_TYPE_ENC;
- inst->priv = venc;
- INIT_LIST_HEAD(&venc->frames);
- init_waitqueue_head(&venc->wq);
- ret = vpu_v4l2_open(file, inst);
- if (ret)
- return ret;
- inst->min_buffer_out = VENC_MIN_BUFFER_OUT;
- inst->min_buffer_cap = VENC_MIN_BUFFER_CAP;
- venc_init(file);
- return 0;
- }
- static const struct v4l2_file_operations venc_fops = {
- .owner = THIS_MODULE,
- .open = venc_open,
- .release = vpu_v4l2_close,
- .unlocked_ioctl = video_ioctl2,
- .poll = v4l2_m2m_fop_poll,
- .mmap = v4l2_m2m_fop_mmap,
- };
- const struct v4l2_ioctl_ops *venc_get_ioctl_ops(void)
- {
- return &venc_ioctl_ops;
- }
- const struct v4l2_file_operations *venc_get_fops(void)
- {
- return &venc_fops;
- }
|