venc.c 35 KB

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