venc.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright 2020-2021 NXP
  4. */
  5. #include <linux/init.h>
  6. #include <linux/interconnect.h>
  7. #include <linux/ioctl.h>
  8. #include <linux/list.h>
  9. #include <linux/kernel.h>
  10. #include <linux/module.h>
  11. #include <linux/delay.h>
  12. #include <linux/videodev2.h>
  13. #include <linux/ktime.h>
  14. #include <linux/rational.h>
  15. #include <media/v4l2-device.h>
  16. #include <media/v4l2-event.h>
  17. #include <media/v4l2-mem2mem.h>
  18. #include <media/v4l2-ioctl.h>
  19. #include <media/videobuf2-v4l2.h>
  20. #include <media/videobuf2-dma-contig.h>
  21. #include "vpu.h"
  22. #include "vpu_defs.h"
  23. #include "vpu_core.h"
  24. #include "vpu_helpers.h"
  25. #include "vpu_v4l2.h"
  26. #include "vpu_cmds.h"
  27. #include "vpu_rpc.h"
  28. #define VENC_OUTPUT_ENABLE BIT(0)
  29. #define VENC_CAPTURE_ENABLE BIT(1)
  30. #define VENC_ENABLE_MASK (VENC_OUTPUT_ENABLE | VENC_CAPTURE_ENABLE)
  31. #define VENC_MAX_BUF_CNT 8
  32. #define VENC_MIN_BUFFER_OUT 6
  33. #define VENC_MIN_BUFFER_CAP 6
  34. struct venc_t {
  35. struct vpu_encode_params params;
  36. u32 request_key_frame;
  37. u32 input_ready;
  38. u32 cpb_size;
  39. bool bitrate_change;
  40. struct vpu_buffer enc[VENC_MAX_BUF_CNT];
  41. struct vpu_buffer ref[VENC_MAX_BUF_CNT];
  42. struct vpu_buffer act[VENC_MAX_BUF_CNT];
  43. struct list_head frames;
  44. u32 frame_count;
  45. u32 encode_count;
  46. u32 ready_count;
  47. u32 enable;
  48. u32 stopped;
  49. u32 memory_resource_configured;
  50. u32 skipped_count;
  51. u32 skipped_bytes;
  52. wait_queue_head_t wq;
  53. };
  54. struct venc_frame_t {
  55. struct list_head list;
  56. struct vpu_enc_pic_info info;
  57. u32 bytesused;
  58. s64 timestamp;
  59. };
  60. static const struct vpu_format venc_formats[] = {
  61. {
  62. .pixfmt = V4L2_PIX_FMT_NV12M,
  63. .mem_planes = 2,
  64. .comp_planes = 2,
  65. .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
  66. .sibling = V4L2_PIX_FMT_NV12,
  67. },
  68. {
  69. .pixfmt = V4L2_PIX_FMT_NV12,
  70. .mem_planes = 1,
  71. .comp_planes = 2,
  72. .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
  73. .sibling = V4L2_PIX_FMT_NV12M,
  74. },
  75. {
  76. .pixfmt = V4L2_PIX_FMT_H264,
  77. .mem_planes = 1,
  78. .comp_planes = 1,
  79. .type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
  80. .flags = V4L2_FMT_FLAG_COMPRESSED
  81. },
  82. {0, 0, 0, 0},
  83. };
  84. static int venc_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
  85. {
  86. strscpy(cap->driver, "amphion-vpu", sizeof(cap->driver));
  87. strscpy(cap->card, "amphion vpu encoder", sizeof(cap->card));
  88. strscpy(cap->bus_info, "platform: amphion-vpu", sizeof(cap->bus_info));
  89. return 0;
  90. }
  91. static int venc_enum_fmt(struct file *file, void *fh, struct v4l2_fmtdesc *f)
  92. {
  93. struct vpu_inst *inst = to_inst(file);
  94. const struct vpu_format *fmt;
  95. memset(f->reserved, 0, sizeof(f->reserved));
  96. fmt = vpu_helper_enum_format(inst, f->type, f->index);
  97. if (!fmt)
  98. return -EINVAL;
  99. f->pixelformat = fmt->pixfmt;
  100. f->flags = fmt->flags;
  101. return 0;
  102. }
  103. static int venc_enum_framesizes(struct file *file, void *fh, struct v4l2_frmsizeenum *fsize)
  104. {
  105. struct vpu_inst *inst = to_inst(file);
  106. const struct vpu_core_resources *res;
  107. if (!fsize || fsize->index)
  108. return -EINVAL;
  109. if (!vpu_helper_find_format(inst, 0, fsize->pixel_format))
  110. return -EINVAL;
  111. res = vpu_get_resource(inst);
  112. if (!res)
  113. return -EINVAL;
  114. fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
  115. fsize->stepwise.max_width = res->max_width;
  116. fsize->stepwise.max_height = res->max_height;
  117. fsize->stepwise.min_width = res->min_width;
  118. fsize->stepwise.min_height = res->min_height;
  119. fsize->stepwise.step_width = res->step_width;
  120. fsize->stepwise.step_height = res->step_height;
  121. return 0;
  122. }
  123. static int venc_enum_frameintervals(struct file *file, void *fh, struct v4l2_frmivalenum *fival)
  124. {
  125. struct vpu_inst *inst = to_inst(file);
  126. const struct vpu_core_resources *res;
  127. if (!fival || fival->index)
  128. return -EINVAL;
  129. if (!vpu_helper_find_format(inst, 0, fival->pixel_format))
  130. return -EINVAL;
  131. if (!fival->width || !fival->height)
  132. return -EINVAL;
  133. res = vpu_get_resource(inst);
  134. if (!res)
  135. return -EINVAL;
  136. if (fival->width < res->min_width || fival->width > res->max_width ||
  137. fival->height < res->min_height || fival->height > res->max_height)
  138. return -EINVAL;
  139. fival->type = V4L2_FRMIVAL_TYPE_CONTINUOUS;
  140. fival->stepwise.min.numerator = 1;
  141. fival->stepwise.min.denominator = USHRT_MAX;
  142. fival->stepwise.max.numerator = USHRT_MAX;
  143. fival->stepwise.max.denominator = 1;
  144. fival->stepwise.step.numerator = 1;
  145. fival->stepwise.step.denominator = 1;
  146. return 0;
  147. }
  148. static int venc_g_fmt(struct file *file, void *fh, struct v4l2_format *f)
  149. {
  150. struct vpu_inst *inst = to_inst(file);
  151. struct venc_t *venc = inst->priv;
  152. struct v4l2_pix_format_mplane *pixmp = &f->fmt.pix_mp;
  153. struct vpu_format *cur_fmt;
  154. int i;
  155. cur_fmt = vpu_get_format(inst, f->type);
  156. pixmp->pixelformat = cur_fmt->pixfmt;
  157. pixmp->num_planes = cur_fmt->mem_planes;
  158. pixmp->width = cur_fmt->width;
  159. pixmp->height = cur_fmt->height;
  160. pixmp->field = cur_fmt->field;
  161. pixmp->flags = cur_fmt->flags;
  162. for (i = 0; i < pixmp->num_planes; i++) {
  163. pixmp->plane_fmt[i].bytesperline = cur_fmt->bytesperline[i];
  164. pixmp->plane_fmt[i].sizeimage = vpu_get_fmt_plane_size(cur_fmt, i);
  165. }
  166. f->fmt.pix_mp.colorspace = venc->params.color.primaries;
  167. f->fmt.pix_mp.xfer_func = venc->params.color.transfer;
  168. f->fmt.pix_mp.ycbcr_enc = venc->params.color.matrix;
  169. f->fmt.pix_mp.quantization = venc->params.color.full_range;
  170. return 0;
  171. }
  172. static int venc_try_fmt(struct file *file, void *fh, struct v4l2_format *f)
  173. {
  174. struct vpu_inst *inst = to_inst(file);
  175. struct vpu_format fmt;
  176. vpu_try_fmt_common(inst, f, &fmt);
  177. return 0;
  178. }
  179. static int venc_s_fmt(struct file *file, void *fh, struct v4l2_format *f)
  180. {
  181. struct vpu_inst *inst = to_inst(file);
  182. struct vpu_format fmt;
  183. struct vpu_format *cur_fmt;
  184. struct vb2_queue *q;
  185. struct venc_t *venc = inst->priv;
  186. struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
  187. q = v4l2_m2m_get_vq(inst->fh.m2m_ctx, f->type);
  188. if (vb2_is_busy(q))
  189. return -EBUSY;
  190. if (vpu_try_fmt_common(inst, f, &fmt))
  191. return -EINVAL;
  192. cur_fmt = vpu_get_format(inst, f->type);
  193. memcpy(cur_fmt, &fmt, sizeof(*cur_fmt));
  194. if (V4L2_TYPE_IS_OUTPUT(f->type)) {
  195. venc->params.input_format = cur_fmt->pixfmt;
  196. venc->params.src_stride = cur_fmt->bytesperline[0];
  197. venc->params.src_width = cur_fmt->width;
  198. venc->params.src_height = cur_fmt->height;
  199. venc->params.crop.left = 0;
  200. venc->params.crop.top = 0;
  201. venc->params.crop.width = cur_fmt->width;
  202. venc->params.crop.height = cur_fmt->height;
  203. } else {
  204. venc->params.codec_format = cur_fmt->pixfmt;
  205. venc->params.out_width = cur_fmt->width;
  206. venc->params.out_height = cur_fmt->height;
  207. }
  208. if (V4L2_TYPE_IS_OUTPUT(f->type)) {
  209. venc->params.color.primaries = pix_mp->colorspace;
  210. venc->params.color.transfer = pix_mp->xfer_func;
  211. venc->params.color.matrix = pix_mp->ycbcr_enc;
  212. venc->params.color.full_range = pix_mp->quantization;
  213. }
  214. pix_mp->colorspace = venc->params.color.primaries;
  215. pix_mp->xfer_func = venc->params.color.transfer;
  216. pix_mp->ycbcr_enc = venc->params.color.matrix;
  217. pix_mp->quantization = venc->params.color.full_range;
  218. return 0;
  219. }
  220. static int venc_g_parm(struct file *file, void *fh, struct v4l2_streamparm *parm)
  221. {
  222. struct vpu_inst *inst = to_inst(file);
  223. struct venc_t *venc = inst->priv;
  224. struct v4l2_fract *timeperframe;
  225. if (!parm)
  226. return -EINVAL;
  227. if (!V4L2_TYPE_IS_OUTPUT(parm->type))
  228. return -EINVAL;
  229. if (!vpu_helper_check_type(inst, parm->type))
  230. return -EINVAL;
  231. timeperframe = &parm->parm.capture.timeperframe;
  232. parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
  233. parm->parm.capture.readbuffers = 0;
  234. timeperframe->numerator = venc->params.frame_rate.numerator;
  235. timeperframe->denominator = venc->params.frame_rate.denominator;
  236. return 0;
  237. }
  238. static int venc_s_parm(struct file *file, void *fh, struct v4l2_streamparm *parm)
  239. {
  240. struct vpu_inst *inst = to_inst(file);
  241. struct venc_t *venc = inst->priv;
  242. struct v4l2_fract *timeperframe;
  243. unsigned long n, d;
  244. if (!parm)
  245. return -EINVAL;
  246. if (!V4L2_TYPE_IS_OUTPUT(parm->type))
  247. return -EINVAL;
  248. if (!vpu_helper_check_type(inst, parm->type))
  249. return -EINVAL;
  250. timeperframe = &parm->parm.capture.timeperframe;
  251. if (!timeperframe->numerator)
  252. timeperframe->numerator = venc->params.frame_rate.numerator;
  253. if (!timeperframe->denominator)
  254. timeperframe->denominator = venc->params.frame_rate.denominator;
  255. venc->params.frame_rate.numerator = timeperframe->numerator;
  256. venc->params.frame_rate.denominator = timeperframe->denominator;
  257. rational_best_approximation(venc->params.frame_rate.numerator,
  258. venc->params.frame_rate.denominator,
  259. venc->params.frame_rate.numerator,
  260. venc->params.frame_rate.denominator,
  261. &n, &d);
  262. venc->params.frame_rate.numerator = n;
  263. venc->params.frame_rate.denominator = d;
  264. parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
  265. memset(parm->parm.capture.reserved, 0, sizeof(parm->parm.capture.reserved));
  266. return 0;
  267. }
  268. static int venc_g_selection(struct file *file, void *fh, struct v4l2_selection *s)
  269. {
  270. struct vpu_inst *inst = to_inst(file);
  271. struct venc_t *venc = inst->priv;
  272. if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT && s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
  273. return -EINVAL;
  274. switch (s->target) {
  275. case V4L2_SEL_TGT_CROP_DEFAULT:
  276. case V4L2_SEL_TGT_CROP_BOUNDS:
  277. s->r.left = 0;
  278. s->r.top = 0;
  279. s->r.width = inst->out_format.width;
  280. s->r.height = inst->out_format.height;
  281. break;
  282. case V4L2_SEL_TGT_CROP:
  283. s->r = venc->params.crop;
  284. break;
  285. default:
  286. return -EINVAL;
  287. }
  288. return 0;
  289. }
  290. static int venc_valid_crop(struct venc_t *venc, const struct vpu_core_resources *res)
  291. {
  292. struct v4l2_rect *rect = NULL;
  293. u32 min_width;
  294. u32 min_height;
  295. u32 src_width;
  296. u32 src_height;
  297. rect = &venc->params.crop;
  298. min_width = res->min_width;
  299. min_height = res->min_height;
  300. src_width = venc->params.src_width;
  301. src_height = venc->params.src_height;
  302. if (rect->width == 0 || rect->height == 0)
  303. return -EINVAL;
  304. if (rect->left > src_width - min_width || rect->top > src_height - min_height)
  305. return -EINVAL;
  306. rect->width = min(rect->width, src_width - rect->left);
  307. rect->width = max_t(u32, rect->width, min_width);
  308. rect->height = min(rect->height, src_height - rect->top);
  309. rect->height = max_t(u32, rect->height, min_height);
  310. return 0;
  311. }
  312. static int venc_s_selection(struct file *file, void *fh, struct v4l2_selection *s)
  313. {
  314. struct vpu_inst *inst = to_inst(file);
  315. const struct vpu_core_resources *res;
  316. struct venc_t *venc = inst->priv;
  317. res = vpu_get_resource(inst);
  318. if (!res)
  319. return -EINVAL;
  320. if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT && s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
  321. return -EINVAL;
  322. if (s->target != V4L2_SEL_TGT_CROP)
  323. return -EINVAL;
  324. venc->params.crop.left = ALIGN(s->r.left, res->step_width);
  325. venc->params.crop.top = ALIGN(s->r.top, res->step_height);
  326. venc->params.crop.width = ALIGN(s->r.width, res->step_width);
  327. venc->params.crop.height = ALIGN(s->r.height, res->step_height);
  328. if (venc_valid_crop(venc, res)) {
  329. venc->params.crop.left = 0;
  330. venc->params.crop.top = 0;
  331. venc->params.crop.width = venc->params.src_width;
  332. venc->params.crop.height = venc->params.src_height;
  333. }
  334. inst->crop = venc->params.crop;
  335. return 0;
  336. }
  337. static int venc_drain(struct vpu_inst *inst)
  338. {
  339. struct venc_t *venc = inst->priv;
  340. int ret;
  341. if (!inst->fh.m2m_ctx)
  342. return 0;
  343. if (inst->state != VPU_CODEC_STATE_DRAIN)
  344. return 0;
  345. if (!vpu_is_source_empty(inst))
  346. return 0;
  347. if (!venc->input_ready)
  348. return 0;
  349. venc->input_ready = false;
  350. vpu_trace(inst->dev, "[%d]\n", inst->id);
  351. ret = vpu_session_stop(inst);
  352. if (ret)
  353. return ret;
  354. inst->state = VPU_CODEC_STATE_STOP;
  355. wake_up_all(&venc->wq);
  356. return 0;
  357. }
  358. static int venc_request_eos(struct vpu_inst *inst)
  359. {
  360. inst->state = VPU_CODEC_STATE_DRAIN;
  361. venc_drain(inst);
  362. return 0;
  363. }
  364. static int venc_encoder_cmd(struct file *file, void *fh, struct v4l2_encoder_cmd *cmd)
  365. {
  366. struct vpu_inst *inst = to_inst(file);
  367. int ret;
  368. ret = v4l2_m2m_ioctl_try_encoder_cmd(file, fh, cmd);
  369. if (ret)
  370. return ret;
  371. vpu_inst_lock(inst);
  372. if (cmd->cmd == V4L2_ENC_CMD_STOP) {
  373. if (inst->state == VPU_CODEC_STATE_DEINIT)
  374. vpu_set_last_buffer_dequeued(inst, true);
  375. else
  376. venc_request_eos(inst);
  377. }
  378. vpu_inst_unlock(inst);
  379. return 0;
  380. }
  381. static int venc_subscribe_event(struct v4l2_fh *fh, const struct v4l2_event_subscription *sub)
  382. {
  383. switch (sub->type) {
  384. case V4L2_EVENT_EOS:
  385. return v4l2_event_subscribe(fh, sub, 0, NULL);
  386. case V4L2_EVENT_CTRL:
  387. return v4l2_ctrl_subscribe_event(fh, sub);
  388. default:
  389. return -EINVAL;
  390. }
  391. }
  392. static const struct v4l2_ioctl_ops venc_ioctl_ops = {
  393. .vidioc_querycap = venc_querycap,
  394. .vidioc_enum_fmt_vid_cap = venc_enum_fmt,
  395. .vidioc_enum_fmt_vid_out = venc_enum_fmt,
  396. .vidioc_enum_framesizes = venc_enum_framesizes,
  397. .vidioc_enum_frameintervals = venc_enum_frameintervals,
  398. .vidioc_g_fmt_vid_cap_mplane = venc_g_fmt,
  399. .vidioc_g_fmt_vid_out_mplane = venc_g_fmt,
  400. .vidioc_try_fmt_vid_cap_mplane = venc_try_fmt,
  401. .vidioc_try_fmt_vid_out_mplane = venc_try_fmt,
  402. .vidioc_s_fmt_vid_cap_mplane = venc_s_fmt,
  403. .vidioc_s_fmt_vid_out_mplane = venc_s_fmt,
  404. .vidioc_g_parm = venc_g_parm,
  405. .vidioc_s_parm = venc_s_parm,
  406. .vidioc_g_selection = venc_g_selection,
  407. .vidioc_s_selection = venc_s_selection,
  408. .vidioc_try_encoder_cmd = v4l2_m2m_ioctl_try_encoder_cmd,
  409. .vidioc_encoder_cmd = venc_encoder_cmd,
  410. .vidioc_subscribe_event = venc_subscribe_event,
  411. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  412. .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
  413. .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
  414. .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
  415. .vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf,
  416. .vidioc_qbuf = v4l2_m2m_ioctl_qbuf,
  417. .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
  418. .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf,
  419. .vidioc_streamon = v4l2_m2m_ioctl_streamon,
  420. .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
  421. };
  422. static int venc_op_s_ctrl(struct v4l2_ctrl *ctrl)
  423. {
  424. struct vpu_inst *inst = ctrl_to_inst(ctrl);
  425. struct venc_t *venc = inst->priv;
  426. int ret = 0;
  427. switch (ctrl->id) {
  428. case V4L2_CID_MPEG_VIDEO_H264_PROFILE:
  429. venc->params.profile = ctrl->val;
  430. break;
  431. case V4L2_CID_MPEG_VIDEO_H264_LEVEL:
  432. venc->params.level = ctrl->val;
  433. break;
  434. case V4L2_CID_MPEG_VIDEO_FRAME_RC_ENABLE:
  435. venc->params.rc_enable = ctrl->val;
  436. break;
  437. case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
  438. venc->params.rc_mode = ctrl->val;
  439. break;
  440. case V4L2_CID_MPEG_VIDEO_BITRATE:
  441. if (ctrl->val != venc->params.bitrate)
  442. venc->bitrate_change = true;
  443. venc->params.bitrate = ctrl->val;
  444. break;
  445. case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:
  446. venc->params.bitrate_max = ctrl->val;
  447. break;
  448. case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
  449. venc->params.gop_length = ctrl->val;
  450. break;
  451. case V4L2_CID_MPEG_VIDEO_B_FRAMES:
  452. venc->params.bframes = ctrl->val;
  453. break;
  454. case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP:
  455. venc->params.i_frame_qp = ctrl->val;
  456. break;
  457. case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP:
  458. venc->params.p_frame_qp = ctrl->val;
  459. break;
  460. case V4L2_CID_MPEG_VIDEO_H264_B_FRAME_QP:
  461. venc->params.b_frame_qp = ctrl->val;
  462. break;
  463. case V4L2_CID_MPEG_VIDEO_FORCE_KEY_FRAME:
  464. venc->request_key_frame = 1;
  465. break;
  466. case V4L2_CID_MPEG_VIDEO_H264_CPB_SIZE:
  467. venc->cpb_size = ctrl->val * 1024;
  468. break;
  469. case V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_ENABLE:
  470. venc->params.sar.enable = ctrl->val;
  471. break;
  472. case V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_IDC:
  473. venc->params.sar.idc = ctrl->val;
  474. break;
  475. case V4L2_CID_MPEG_VIDEO_H264_VUI_EXT_SAR_WIDTH:
  476. venc->params.sar.width = ctrl->val;
  477. break;
  478. case V4L2_CID_MPEG_VIDEO_H264_VUI_EXT_SAR_HEIGHT:
  479. venc->params.sar.height = ctrl->val;
  480. break;
  481. case V4L2_CID_MPEG_VIDEO_HEADER_MODE:
  482. break;
  483. default:
  484. ret = -EINVAL;
  485. break;
  486. }
  487. return ret;
  488. }
  489. static const struct v4l2_ctrl_ops venc_ctrl_ops = {
  490. .s_ctrl = venc_op_s_ctrl,
  491. .g_volatile_ctrl = vpu_helper_g_volatile_ctrl,
  492. };
  493. static int venc_ctrl_init(struct vpu_inst *inst)
  494. {
  495. struct v4l2_ctrl *ctrl;
  496. int ret;
  497. ret = v4l2_ctrl_handler_init(&inst->ctrl_handler, 20);
  498. if (ret)
  499. return ret;
  500. v4l2_ctrl_new_std_menu(&inst->ctrl_handler, &venc_ctrl_ops,
  501. V4L2_CID_MPEG_VIDEO_H264_PROFILE,
  502. V4L2_MPEG_VIDEO_H264_PROFILE_HIGH,
  503. ~((1 << V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE) |
  504. (1 << V4L2_MPEG_VIDEO_H264_PROFILE_MAIN) |
  505. (1 << V4L2_MPEG_VIDEO_H264_PROFILE_HIGH)),
  506. V4L2_MPEG_VIDEO_H264_PROFILE_HIGH);
  507. v4l2_ctrl_new_std_menu(&inst->ctrl_handler, &venc_ctrl_ops,
  508. V4L2_CID_MPEG_VIDEO_H264_LEVEL,
  509. V4L2_MPEG_VIDEO_H264_LEVEL_5_1,
  510. 0x0,
  511. V4L2_MPEG_VIDEO_H264_LEVEL_4_0);
  512. v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
  513. V4L2_CID_MPEG_VIDEO_FRAME_RC_ENABLE, 0, 1, 1, 1);
  514. v4l2_ctrl_new_std_menu(&inst->ctrl_handler, &venc_ctrl_ops,
  515. V4L2_CID_MPEG_VIDEO_BITRATE_MODE,
  516. V4L2_MPEG_VIDEO_BITRATE_MODE_CBR,
  517. ~((1 << V4L2_MPEG_VIDEO_BITRATE_MODE_VBR) |
  518. (1 << V4L2_MPEG_VIDEO_BITRATE_MODE_CBR)),
  519. V4L2_MPEG_VIDEO_BITRATE_MODE_CBR);
  520. v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
  521. V4L2_CID_MPEG_VIDEO_BITRATE,
  522. BITRATE_MIN,
  523. BITRATE_MAX,
  524. BITRATE_STEP,
  525. BITRATE_DEFAULT);
  526. v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
  527. V4L2_CID_MPEG_VIDEO_BITRATE_PEAK,
  528. BITRATE_MIN, BITRATE_MAX,
  529. BITRATE_STEP,
  530. BITRATE_DEFAULT_PEAK);
  531. v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
  532. V4L2_CID_MPEG_VIDEO_GOP_SIZE, 1, 8000, 1, 30);
  533. v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
  534. V4L2_CID_MPEG_VIDEO_B_FRAMES, 0, 4, 1, 0);
  535. v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
  536. V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP, 1, 51, 1, 26);
  537. v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
  538. V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP, 1, 51, 1, 28);
  539. v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
  540. V4L2_CID_MPEG_VIDEO_H264_B_FRAME_QP, 1, 51, 1, 30);
  541. v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
  542. V4L2_CID_MPEG_VIDEO_FORCE_KEY_FRAME, 0, 0, 0, 0);
  543. ctrl = v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
  544. V4L2_CID_MIN_BUFFERS_FOR_CAPTURE, 1, 32, 1, 2);
  545. if (ctrl)
  546. ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE;
  547. ctrl = v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
  548. V4L2_CID_MIN_BUFFERS_FOR_OUTPUT, 1, 32, 1, 2);
  549. if (ctrl)
  550. ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE;
  551. v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
  552. V4L2_CID_MPEG_VIDEO_H264_CPB_SIZE, 64, 10240, 1, 1024);
  553. v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
  554. V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_ENABLE, 0, 1, 1, 1);
  555. v4l2_ctrl_new_std_menu(&inst->ctrl_handler, &venc_ctrl_ops,
  556. V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_IDC,
  557. V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_EXTENDED,
  558. 0x0,
  559. V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_1x1);
  560. v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
  561. V4L2_CID_MPEG_VIDEO_H264_VUI_EXT_SAR_WIDTH,
  562. 0, USHRT_MAX, 1, 1);
  563. v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
  564. V4L2_CID_MPEG_VIDEO_H264_VUI_EXT_SAR_HEIGHT,
  565. 0, USHRT_MAX, 1, 1);
  566. v4l2_ctrl_new_std_menu(&inst->ctrl_handler, &venc_ctrl_ops,
  567. V4L2_CID_MPEG_VIDEO_HEADER_MODE,
  568. V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME,
  569. ~(1 << V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME),
  570. V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME);
  571. v4l2_ctrl_new_std(&inst->ctrl_handler, NULL,
  572. V4L2_CID_MPEG_VIDEO_AVERAGE_QP, 0, 51, 1, 0);
  573. if (inst->ctrl_handler.error) {
  574. ret = inst->ctrl_handler.error;
  575. v4l2_ctrl_handler_free(&inst->ctrl_handler);
  576. return ret;
  577. }
  578. ret = v4l2_ctrl_handler_setup(&inst->ctrl_handler);
  579. if (ret) {
  580. dev_err(inst->dev, "[%d] setup ctrls fail, ret = %d\n", inst->id, ret);
  581. v4l2_ctrl_handler_free(&inst->ctrl_handler);
  582. return ret;
  583. }
  584. return 0;
  585. }
  586. static bool venc_check_ready(struct vpu_inst *inst, unsigned int type)
  587. {
  588. struct venc_t *venc = inst->priv;
  589. if (V4L2_TYPE_IS_OUTPUT(type)) {
  590. if (vpu_helper_get_free_space(inst) < venc->cpb_size)
  591. return false;
  592. return venc->input_ready;
  593. }
  594. if (list_empty(&venc->frames))
  595. return false;
  596. return true;
  597. }
  598. static u32 venc_get_enable_mask(u32 type)
  599. {
  600. if (V4L2_TYPE_IS_OUTPUT(type))
  601. return VENC_OUTPUT_ENABLE;
  602. else
  603. return VENC_CAPTURE_ENABLE;
  604. }
  605. static void venc_set_enable(struct venc_t *venc, u32 type, int enable)
  606. {
  607. u32 mask = venc_get_enable_mask(type);
  608. if (enable)
  609. venc->enable |= mask;
  610. else
  611. venc->enable &= ~mask;
  612. }
  613. static u32 venc_get_enable(struct venc_t *venc, u32 type)
  614. {
  615. return venc->enable & venc_get_enable_mask(type);
  616. }
  617. static void venc_input_done(struct vpu_inst *inst)
  618. {
  619. struct venc_t *venc = inst->priv;
  620. vpu_inst_lock(inst);
  621. venc->input_ready = true;
  622. vpu_process_output_buffer(inst);
  623. if (inst->state == VPU_CODEC_STATE_DRAIN)
  624. venc_drain(inst);
  625. vpu_inst_unlock(inst);
  626. }
  627. /*
  628. * It's hardware limitation, that there may be several bytes
  629. * redundant data at the beginning of frame.
  630. * For android platform, the redundant data may cause cts test fail
  631. * So driver will strip them
  632. */
  633. static int venc_precheck_encoded_frame(struct vpu_inst *inst, struct venc_frame_t *frame)
  634. {
  635. struct venc_t *venc;
  636. int skipped;
  637. if (!frame || !frame->bytesused)
  638. return -EINVAL;
  639. venc = inst->priv;
  640. skipped = vpu_helper_find_startcode(&inst->stream_buffer,
  641. inst->cap_format.pixfmt,
  642. frame->info.wptr - inst->stream_buffer.phys,
  643. frame->bytesused);
  644. if (skipped > 0) {
  645. frame->bytesused -= skipped;
  646. frame->info.wptr = vpu_helper_step_walk(&inst->stream_buffer,
  647. frame->info.wptr, skipped);
  648. venc->skipped_bytes += skipped;
  649. venc->skipped_count++;
  650. }
  651. return 0;
  652. }
  653. static int venc_get_one_encoded_frame(struct vpu_inst *inst,
  654. struct venc_frame_t *frame,
  655. struct vb2_v4l2_buffer *vbuf)
  656. {
  657. struct venc_t *venc = inst->priv;
  658. struct vb2_v4l2_buffer *src_buf;
  659. if (!vbuf)
  660. return -EAGAIN;
  661. src_buf = vpu_find_buf_by_sequence(inst, inst->out_format.type, frame->info.frame_id);
  662. if (src_buf) {
  663. v4l2_m2m_buf_copy_metadata(src_buf, vbuf);
  664. vpu_set_buffer_state(src_buf, VPU_BUF_STATE_IDLE);
  665. v4l2_m2m_src_buf_remove_by_buf(inst->fh.m2m_ctx, src_buf);
  666. v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
  667. } else {
  668. vbuf->vb2_buf.timestamp = frame->info.timestamp;
  669. }
  670. if (!venc_get_enable(inst->priv, vbuf->vb2_buf.type)) {
  671. v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_ERROR);
  672. return 0;
  673. }
  674. if (frame->bytesused > vbuf->vb2_buf.planes[0].length) {
  675. v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_ERROR);
  676. return -ENOMEM;
  677. }
  678. venc_precheck_encoded_frame(inst, frame);
  679. if (frame->bytesused) {
  680. u32 rptr = frame->info.wptr;
  681. void *dst = vb2_plane_vaddr(&vbuf->vb2_buf, 0);
  682. vpu_helper_copy_from_stream_buffer(&inst->stream_buffer,
  683. &rptr, frame->bytesused, dst);
  684. vpu_iface_update_stream_buffer(inst, rptr, 0);
  685. }
  686. vb2_set_plane_payload(&vbuf->vb2_buf, 0, frame->bytesused);
  687. vbuf->sequence = frame->info.frame_id;
  688. vbuf->field = inst->cap_format.field;
  689. vbuf->flags |= frame->info.pic_type;
  690. vpu_set_buffer_state(vbuf, VPU_BUF_STATE_IDLE);
  691. vpu_set_buffer_average_qp(vbuf, frame->info.average_qp);
  692. dev_dbg(inst->dev, "[%d][OUTPUT TS]%32lld\n", inst->id, vbuf->vb2_buf.timestamp);
  693. v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_DONE);
  694. venc->ready_count++;
  695. if (vbuf->flags & V4L2_BUF_FLAG_KEYFRAME)
  696. dev_dbg(inst->dev, "[%d][%d]key frame\n", inst->id, frame->info.frame_id);
  697. return 0;
  698. }
  699. static int venc_get_encoded_frames(struct vpu_inst *inst)
  700. {
  701. struct venc_t *venc;
  702. struct venc_frame_t *frame;
  703. struct venc_frame_t *tmp;
  704. if (!inst->fh.m2m_ctx)
  705. return 0;
  706. venc = inst->priv;
  707. list_for_each_entry_safe(frame, tmp, &venc->frames, list) {
  708. if (venc_get_one_encoded_frame(inst, frame,
  709. v4l2_m2m_dst_buf_remove(inst->fh.m2m_ctx)))
  710. break;
  711. list_del_init(&frame->list);
  712. kfree(frame);
  713. }
  714. return 0;
  715. }
  716. static int venc_frame_encoded(struct vpu_inst *inst, void *arg)
  717. {
  718. struct vpu_enc_pic_info *info = arg;
  719. struct venc_frame_t *frame;
  720. struct venc_t *venc;
  721. int ret = 0;
  722. if (!info)
  723. return -EINVAL;
  724. venc = inst->priv;
  725. frame = kzalloc_obj(*frame);
  726. if (!frame)
  727. return -ENOMEM;
  728. memcpy(&frame->info, info, sizeof(frame->info));
  729. frame->bytesused = info->frame_size;
  730. vpu_inst_lock(inst);
  731. list_add_tail(&frame->list, &venc->frames);
  732. venc->encode_count++;
  733. venc_get_encoded_frames(inst);
  734. vpu_inst_unlock(inst);
  735. return ret;
  736. }
  737. static void venc_set_last_buffer_dequeued(struct vpu_inst *inst)
  738. {
  739. struct venc_t *venc = inst->priv;
  740. if (venc->stopped && list_empty(&venc->frames))
  741. vpu_set_last_buffer_dequeued(inst, true);
  742. }
  743. static void venc_stop_done(struct vpu_inst *inst)
  744. {
  745. struct venc_t *venc = inst->priv;
  746. vpu_inst_lock(inst);
  747. venc->stopped = true;
  748. venc_set_last_buffer_dequeued(inst);
  749. vpu_inst_unlock(inst);
  750. wake_up_all(&venc->wq);
  751. }
  752. static void venc_event_notify(struct vpu_inst *inst, u32 event, void *data)
  753. {
  754. }
  755. static void venc_release(struct vpu_inst *inst)
  756. {
  757. }
  758. static void venc_cleanup(struct vpu_inst *inst)
  759. {
  760. struct venc_t *venc;
  761. if (!inst)
  762. return;
  763. venc = inst->priv;
  764. kfree(venc);
  765. inst->priv = NULL;
  766. kfree(inst);
  767. }
  768. static int venc_start_session(struct vpu_inst *inst, u32 type)
  769. {
  770. struct venc_t *venc = inst->priv;
  771. int stream_buffer_size;
  772. int ret;
  773. venc_set_enable(venc, type, 1);
  774. if ((venc->enable & VENC_ENABLE_MASK) != VENC_ENABLE_MASK)
  775. return 0;
  776. vpu_iface_init_instance(inst);
  777. stream_buffer_size = vpu_iface_get_stream_buffer_size(inst->core);
  778. if (stream_buffer_size > 0) {
  779. inst->stream_buffer.length = max_t(u32, stream_buffer_size, venc->cpb_size * 3);
  780. ret = vpu_alloc_dma(inst->core, &inst->stream_buffer);
  781. if (ret)
  782. goto error;
  783. inst->use_stream_buffer = true;
  784. vpu_iface_config_stream_buffer(inst, &inst->stream_buffer);
  785. }
  786. ret = vpu_iface_set_encode_params(inst, &venc->params, 0);
  787. if (ret)
  788. goto error;
  789. venc->memory_resource_configured = false;
  790. ret = vpu_session_configure_codec(inst);
  791. if (ret)
  792. goto error;
  793. if (!venc->memory_resource_configured) {
  794. vb2_queue_error(v4l2_m2m_get_src_vq(inst->fh.m2m_ctx));
  795. vb2_queue_error(v4l2_m2m_get_dst_vq(inst->fh.m2m_ctx));
  796. ret = -ENOMEM;
  797. goto error;
  798. }
  799. inst->state = VPU_CODEC_STATE_CONFIGURED;
  800. /*vpu_iface_config_memory_resource*/
  801. /*config enc expert mode parameter*/
  802. ret = vpu_iface_set_encode_params(inst, &venc->params, 1);
  803. if (ret)
  804. goto error;
  805. ret = vpu_session_start(inst);
  806. if (ret)
  807. goto error;
  808. inst->state = VPU_CODEC_STATE_STARTED;
  809. venc->bitrate_change = false;
  810. venc->input_ready = true;
  811. venc->frame_count = 0;
  812. venc->encode_count = 0;
  813. venc->ready_count = 0;
  814. venc->stopped = false;
  815. vpu_process_output_buffer(inst);
  816. if (venc->frame_count == 0)
  817. dev_err(inst->dev, "[%d] there is no input when starting\n", inst->id);
  818. return 0;
  819. error:
  820. venc_set_enable(venc, type, 0);
  821. inst->state = VPU_CODEC_STATE_DEINIT;
  822. vpu_free_dma(&inst->stream_buffer);
  823. return ret;
  824. }
  825. static void venc_cleanup_mem_resource(struct vpu_inst *inst)
  826. {
  827. struct venc_t *venc;
  828. u32 i;
  829. venc = inst->priv;
  830. venc->memory_resource_configured = false;
  831. for (i = 0; i < ARRAY_SIZE(venc->enc); i++)
  832. vpu_free_dma(&venc->enc[i]);
  833. for (i = 0; i < ARRAY_SIZE(venc->ref); i++)
  834. vpu_free_dma(&venc->ref[i]);
  835. }
  836. static void venc_request_mem_resource(struct vpu_inst *inst,
  837. u32 enc_frame_size,
  838. u32 enc_frame_num,
  839. u32 ref_frame_size,
  840. u32 ref_frame_num,
  841. u32 act_frame_size,
  842. u32 act_frame_num)
  843. {
  844. struct venc_t *venc;
  845. u32 i;
  846. int ret;
  847. venc = inst->priv;
  848. if (enc_frame_num > ARRAY_SIZE(venc->enc)) {
  849. dev_err(inst->dev, "[%d] enc num(%d) is out of range\n", inst->id, enc_frame_num);
  850. return;
  851. }
  852. if (ref_frame_num > ARRAY_SIZE(venc->ref)) {
  853. dev_err(inst->dev, "[%d] ref num(%d) is out of range\n", inst->id, ref_frame_num);
  854. return;
  855. }
  856. if (act_frame_num > ARRAY_SIZE(venc->act)) {
  857. dev_err(inst->dev, "[%d] act num(%d) is out of range\n", inst->id, act_frame_num);
  858. return;
  859. }
  860. for (i = 0; i < enc_frame_num; i++) {
  861. venc->enc[i].length = enc_frame_size;
  862. ret = vpu_alloc_dma(inst->core, &venc->enc[i]);
  863. if (ret) {
  864. venc_cleanup_mem_resource(inst);
  865. return;
  866. }
  867. }
  868. for (i = 0; i < ref_frame_num; i++) {
  869. venc->ref[i].length = ref_frame_size;
  870. ret = vpu_alloc_dma(inst->core, &venc->ref[i]);
  871. if (ret) {
  872. venc_cleanup_mem_resource(inst);
  873. return;
  874. }
  875. }
  876. if (act_frame_num != 1 || act_frame_size > inst->act.length) {
  877. venc_cleanup_mem_resource(inst);
  878. return;
  879. }
  880. venc->act[0].length = act_frame_size;
  881. venc->act[0].phys = inst->act.phys;
  882. venc->act[0].virt = inst->act.virt;
  883. for (i = 0; i < enc_frame_num; i++)
  884. vpu_iface_config_memory_resource(inst, MEM_RES_ENC, i, &venc->enc[i]);
  885. for (i = 0; i < ref_frame_num; i++)
  886. vpu_iface_config_memory_resource(inst, MEM_RES_REF, i, &venc->ref[i]);
  887. for (i = 0; i < act_frame_num; i++)
  888. vpu_iface_config_memory_resource(inst, MEM_RES_ACT, i, &venc->act[i]);
  889. venc->memory_resource_configured = true;
  890. }
  891. static void venc_cleanup_frames(struct venc_t *venc)
  892. {
  893. struct venc_frame_t *frame;
  894. struct venc_frame_t *tmp;
  895. list_for_each_entry_safe(frame, tmp, &venc->frames, list) {
  896. list_del_init(&frame->list);
  897. kfree(frame);
  898. }
  899. }
  900. static int venc_stop_session(struct vpu_inst *inst, u32 type)
  901. {
  902. struct venc_t *venc = inst->priv;
  903. venc_set_enable(venc, type, 0);
  904. if (venc->enable & VENC_ENABLE_MASK)
  905. return 0;
  906. if (inst->state == VPU_CODEC_STATE_DEINIT)
  907. return 0;
  908. if (inst->state != VPU_CODEC_STATE_STOP)
  909. venc_request_eos(inst);
  910. call_void_vop(inst, wait_prepare);
  911. if (!wait_event_timeout(venc->wq, venc->stopped, VPU_TIMEOUT)) {
  912. set_bit(inst->id, &inst->core->hang_mask);
  913. vpu_session_debug(inst);
  914. }
  915. call_void_vop(inst, wait_finish);
  916. inst->state = VPU_CODEC_STATE_DEINIT;
  917. venc_cleanup_frames(inst->priv);
  918. vpu_free_dma(&inst->stream_buffer);
  919. venc_cleanup_mem_resource(inst);
  920. return 0;
  921. }
  922. static int venc_process_output(struct vpu_inst *inst, struct vb2_buffer *vb)
  923. {
  924. struct venc_t *venc = inst->priv;
  925. struct vb2_v4l2_buffer *vbuf;
  926. u32 flags;
  927. if (inst->state == VPU_CODEC_STATE_DEINIT)
  928. return -EINVAL;
  929. vbuf = to_vb2_v4l2_buffer(vb);
  930. if (inst->state == VPU_CODEC_STATE_STARTED)
  931. inst->state = VPU_CODEC_STATE_ACTIVE;
  932. flags = vbuf->flags;
  933. if (venc->request_key_frame) {
  934. vbuf->flags |= V4L2_BUF_FLAG_KEYFRAME;
  935. venc->request_key_frame = 0;
  936. }
  937. if (venc->bitrate_change) {
  938. vpu_session_update_parameters(inst, &venc->params);
  939. venc->bitrate_change = false;
  940. }
  941. dev_dbg(inst->dev, "[%d][INPUT TS]%32lld\n", inst->id, vb->timestamp);
  942. vpu_iface_input_frame(inst, vb);
  943. vbuf->flags = flags;
  944. venc->input_ready = false;
  945. venc->frame_count++;
  946. vpu_set_buffer_state(vbuf, VPU_BUF_STATE_INUSE);
  947. return 0;
  948. }
  949. static int venc_process_capture(struct vpu_inst *inst, struct vb2_buffer *vb)
  950. {
  951. struct venc_t *venc;
  952. struct venc_frame_t *frame = NULL;
  953. struct vb2_v4l2_buffer *vbuf;
  954. int ret;
  955. venc = inst->priv;
  956. if (list_empty(&venc->frames))
  957. return -EINVAL;
  958. frame = list_first_entry(&venc->frames, struct venc_frame_t, list);
  959. vbuf = to_vb2_v4l2_buffer(vb);
  960. v4l2_m2m_dst_buf_remove_by_buf(inst->fh.m2m_ctx, vbuf);
  961. ret = venc_get_one_encoded_frame(inst, frame, vbuf);
  962. if (ret)
  963. return ret;
  964. list_del_init(&frame->list);
  965. kfree(frame);
  966. return 0;
  967. }
  968. static void venc_on_queue_empty(struct vpu_inst *inst, u32 type)
  969. {
  970. struct venc_t *venc = inst->priv;
  971. if (V4L2_TYPE_IS_OUTPUT(type))
  972. return;
  973. if (venc->stopped)
  974. venc_set_last_buffer_dequeued(inst);
  975. }
  976. static int venc_get_debug_info(struct vpu_inst *inst, char *str, u32 size, u32 i)
  977. {
  978. struct venc_t *venc = inst->priv;
  979. int num = -1;
  980. switch (i) {
  981. case 0:
  982. num = scnprintf(str, size, "profile = %d\n", venc->params.profile);
  983. break;
  984. case 1:
  985. num = scnprintf(str, size, "level = %d\n", venc->params.level);
  986. break;
  987. case 2:
  988. num = scnprintf(str, size, "fps = %d/%d\n",
  989. venc->params.frame_rate.numerator,
  990. venc->params.frame_rate.denominator);
  991. break;
  992. case 3:
  993. num = scnprintf(str, size, "%d x %d -> %d x %d\n",
  994. venc->params.src_width,
  995. venc->params.src_height,
  996. venc->params.out_width,
  997. venc->params.out_height);
  998. break;
  999. case 4:
  1000. num = scnprintf(str, size, "(%d, %d) %d x %d\n",
  1001. venc->params.crop.left,
  1002. venc->params.crop.top,
  1003. venc->params.crop.width,
  1004. venc->params.crop.height);
  1005. break;
  1006. case 5:
  1007. num = scnprintf(str, size,
  1008. "enable = 0x%x, input = %d, encode = %d, ready = %d, stopped = %d\n",
  1009. venc->enable,
  1010. venc->frame_count, venc->encode_count,
  1011. venc->ready_count,
  1012. venc->stopped);
  1013. break;
  1014. case 6:
  1015. num = scnprintf(str, size, "gop = %d\n", venc->params.gop_length);
  1016. break;
  1017. case 7:
  1018. num = scnprintf(str, size, "bframes = %d\n", venc->params.bframes);
  1019. break;
  1020. case 8:
  1021. num = scnprintf(str, size, "rc: %s, mode = %d, bitrate = %d(%d), qp = %d\n",
  1022. venc->params.rc_enable ? "enable" : "disable",
  1023. venc->params.rc_mode,
  1024. venc->params.bitrate,
  1025. venc->params.bitrate_max,
  1026. venc->params.i_frame_qp);
  1027. break;
  1028. case 9:
  1029. num = scnprintf(str, size, "sar: enable = %d, idc = %d, %d x %d\n",
  1030. venc->params.sar.enable,
  1031. venc->params.sar.idc,
  1032. venc->params.sar.width,
  1033. venc->params.sar.height);
  1034. break;
  1035. case 10:
  1036. num = scnprintf(str, size,
  1037. "colorspace: primaries = %d, transfer = %d, matrix = %d, full_range = %d\n",
  1038. venc->params.color.primaries,
  1039. venc->params.color.transfer,
  1040. venc->params.color.matrix,
  1041. venc->params.color.full_range);
  1042. break;
  1043. case 11:
  1044. num = scnprintf(str, size, "skipped: count = %d, bytes = %d\n",
  1045. venc->skipped_count, venc->skipped_bytes);
  1046. break;
  1047. default:
  1048. break;
  1049. }
  1050. return num;
  1051. }
  1052. static struct vpu_inst_ops venc_inst_ops = {
  1053. .ctrl_init = venc_ctrl_init,
  1054. .check_ready = venc_check_ready,
  1055. .input_done = venc_input_done,
  1056. .get_one_frame = venc_frame_encoded,
  1057. .stop_done = venc_stop_done,
  1058. .event_notify = venc_event_notify,
  1059. .release = venc_release,
  1060. .cleanup = venc_cleanup,
  1061. .start = venc_start_session,
  1062. .mem_request = venc_request_mem_resource,
  1063. .stop = venc_stop_session,
  1064. .process_output = venc_process_output,
  1065. .process_capture = venc_process_capture,
  1066. .on_queue_empty = venc_on_queue_empty,
  1067. .get_debug_info = venc_get_debug_info,
  1068. .wait_prepare = vpu_inst_unlock,
  1069. .wait_finish = vpu_inst_lock,
  1070. };
  1071. static void venc_init(struct file *file)
  1072. {
  1073. struct vpu_inst *inst = to_inst(file);
  1074. struct venc_t *venc;
  1075. struct v4l2_format f;
  1076. struct v4l2_streamparm parm;
  1077. venc = inst->priv;
  1078. venc->params.qp_min = 1;
  1079. venc->params.qp_max = 51;
  1080. venc->params.qp_min_i = 1;
  1081. venc->params.qp_max_i = 51;
  1082. venc->params.bitrate_min = BITRATE_MIN;
  1083. memset(&f, 0, sizeof(f));
  1084. f.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
  1085. f.fmt.pix_mp.pixelformat = V4L2_PIX_FMT_NV12M;
  1086. f.fmt.pix_mp.width = 1280;
  1087. f.fmt.pix_mp.height = 720;
  1088. f.fmt.pix_mp.field = V4L2_FIELD_NONE;
  1089. venc_s_fmt(file, &inst->fh, &f);
  1090. memset(&f, 0, sizeof(f));
  1091. f.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
  1092. f.fmt.pix_mp.pixelformat = V4L2_PIX_FMT_H264;
  1093. f.fmt.pix_mp.width = 1280;
  1094. f.fmt.pix_mp.height = 720;
  1095. f.fmt.pix_mp.field = V4L2_FIELD_NONE;
  1096. venc_s_fmt(file, &inst->fh, &f);
  1097. memset(&parm, 0, sizeof(parm));
  1098. parm.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
  1099. parm.parm.capture.timeperframe.numerator = 1;
  1100. parm.parm.capture.timeperframe.denominator = 30;
  1101. venc_s_parm(file, &inst->fh, &parm);
  1102. }
  1103. static int venc_open(struct file *file)
  1104. {
  1105. struct vpu_inst *inst;
  1106. struct venc_t *venc;
  1107. int ret;
  1108. inst = kzalloc_obj(*inst);
  1109. if (!inst)
  1110. return -ENOMEM;
  1111. venc = kzalloc_obj(*venc);
  1112. if (!venc) {
  1113. kfree(inst);
  1114. return -ENOMEM;
  1115. }
  1116. inst->ops = &venc_inst_ops;
  1117. inst->formats = venc_formats;
  1118. inst->type = VPU_CORE_TYPE_ENC;
  1119. inst->priv = venc;
  1120. INIT_LIST_HEAD(&venc->frames);
  1121. init_waitqueue_head(&venc->wq);
  1122. ret = vpu_v4l2_open(file, inst);
  1123. if (ret)
  1124. return ret;
  1125. inst->min_buffer_out = VENC_MIN_BUFFER_OUT;
  1126. inst->min_buffer_cap = VENC_MIN_BUFFER_CAP;
  1127. venc_init(file);
  1128. return 0;
  1129. }
  1130. static const struct v4l2_file_operations venc_fops = {
  1131. .owner = THIS_MODULE,
  1132. .open = venc_open,
  1133. .release = vpu_v4l2_close,
  1134. .unlocked_ioctl = video_ioctl2,
  1135. .poll = v4l2_m2m_fop_poll,
  1136. .mmap = v4l2_m2m_fop_mmap,
  1137. };
  1138. const struct v4l2_ioctl_ops *venc_get_ioctl_ops(void)
  1139. {
  1140. return &venc_ioctl_ops;
  1141. }
  1142. const struct v4l2_file_operations *venc_get_fops(void)
  1143. {
  1144. return &venc_fops;
  1145. }