venc.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371
  1. /*
  2. * Copyright (c) 2012-2016, The Linux Foundation. All rights reserved.
  3. * Copyright (C) 2017 Linaro Ltd.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 and
  7. * only version 2 as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. */
  15. #include <linux/clk.h>
  16. #include <linux/module.h>
  17. #include <linux/mod_devicetable.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/pm_runtime.h>
  20. #include <linux/slab.h>
  21. #include <media/v4l2-mem2mem.h>
  22. #include <media/videobuf2-dma-sg.h>
  23. #include <media/v4l2-ioctl.h>
  24. #include <media/v4l2-event.h>
  25. #include <media/v4l2-ctrls.h>
  26. #include "hfi_venus_io.h"
  27. #include "hfi_parser.h"
  28. #include "core.h"
  29. #include "helpers.h"
  30. #include "venc.h"
  31. #define NUM_B_FRAMES_MAX 4
  32. /*
  33. * Three resons to keep MPLANE formats (despite that the number of planes
  34. * currently is one):
  35. * - the MPLANE formats allow only one plane to be used
  36. * - the downstream driver use MPLANE formats too
  37. * - future firmware versions could add support for >1 planes
  38. */
  39. static const struct venus_format venc_formats[] = {
  40. {
  41. .pixfmt = V4L2_PIX_FMT_NV12,
  42. .num_planes = 1,
  43. .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
  44. }, {
  45. .pixfmt = V4L2_PIX_FMT_MPEG4,
  46. .num_planes = 1,
  47. .type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
  48. }, {
  49. .pixfmt = V4L2_PIX_FMT_H263,
  50. .num_planes = 1,
  51. .type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
  52. }, {
  53. .pixfmt = V4L2_PIX_FMT_H264,
  54. .num_planes = 1,
  55. .type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
  56. }, {
  57. .pixfmt = V4L2_PIX_FMT_VP8,
  58. .num_planes = 1,
  59. .type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
  60. }, {
  61. .pixfmt = V4L2_PIX_FMT_HEVC,
  62. .num_planes = 1,
  63. .type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
  64. },
  65. };
  66. static const struct venus_format *
  67. find_format(struct venus_inst *inst, u32 pixfmt, u32 type)
  68. {
  69. const struct venus_format *fmt = venc_formats;
  70. unsigned int size = ARRAY_SIZE(venc_formats);
  71. unsigned int i;
  72. for (i = 0; i < size; i++) {
  73. if (fmt[i].pixfmt == pixfmt)
  74. break;
  75. }
  76. if (i == size || fmt[i].type != type)
  77. return NULL;
  78. if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE &&
  79. !venus_helper_check_codec(inst, fmt[i].pixfmt))
  80. return NULL;
  81. return &fmt[i];
  82. }
  83. static const struct venus_format *
  84. find_format_by_index(struct venus_inst *inst, unsigned int index, u32 type)
  85. {
  86. const struct venus_format *fmt = venc_formats;
  87. unsigned int size = ARRAY_SIZE(venc_formats);
  88. unsigned int i, k = 0;
  89. if (index > size)
  90. return NULL;
  91. for (i = 0; i < size; i++) {
  92. bool valid;
  93. if (fmt[i].type != type)
  94. continue;
  95. valid = type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE ||
  96. venus_helper_check_codec(inst, fmt[i].pixfmt);
  97. if (k == index && valid)
  98. break;
  99. if (valid)
  100. k++;
  101. }
  102. if (i == size)
  103. return NULL;
  104. return &fmt[i];
  105. }
  106. static int venc_v4l2_to_hfi(int id, int value)
  107. {
  108. switch (id) {
  109. case V4L2_CID_MPEG_VIDEO_MPEG4_LEVEL:
  110. switch (value) {
  111. case V4L2_MPEG_VIDEO_MPEG4_LEVEL_0:
  112. default:
  113. return HFI_MPEG4_LEVEL_0;
  114. case V4L2_MPEG_VIDEO_MPEG4_LEVEL_0B:
  115. return HFI_MPEG4_LEVEL_0b;
  116. case V4L2_MPEG_VIDEO_MPEG4_LEVEL_1:
  117. return HFI_MPEG4_LEVEL_1;
  118. case V4L2_MPEG_VIDEO_MPEG4_LEVEL_2:
  119. return HFI_MPEG4_LEVEL_2;
  120. case V4L2_MPEG_VIDEO_MPEG4_LEVEL_3:
  121. return HFI_MPEG4_LEVEL_3;
  122. case V4L2_MPEG_VIDEO_MPEG4_LEVEL_4:
  123. return HFI_MPEG4_LEVEL_4;
  124. case V4L2_MPEG_VIDEO_MPEG4_LEVEL_5:
  125. return HFI_MPEG4_LEVEL_5;
  126. }
  127. case V4L2_CID_MPEG_VIDEO_MPEG4_PROFILE:
  128. switch (value) {
  129. case V4L2_MPEG_VIDEO_MPEG4_PROFILE_SIMPLE:
  130. default:
  131. return HFI_MPEG4_PROFILE_SIMPLE;
  132. case V4L2_MPEG_VIDEO_MPEG4_PROFILE_ADVANCED_SIMPLE:
  133. return HFI_MPEG4_PROFILE_ADVANCEDSIMPLE;
  134. }
  135. case V4L2_CID_MPEG_VIDEO_H264_PROFILE:
  136. switch (value) {
  137. case V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE:
  138. return HFI_H264_PROFILE_BASELINE;
  139. case V4L2_MPEG_VIDEO_H264_PROFILE_CONSTRAINED_BASELINE:
  140. return HFI_H264_PROFILE_CONSTRAINED_BASE;
  141. case V4L2_MPEG_VIDEO_H264_PROFILE_MAIN:
  142. return HFI_H264_PROFILE_MAIN;
  143. case V4L2_MPEG_VIDEO_H264_PROFILE_HIGH:
  144. default:
  145. return HFI_H264_PROFILE_HIGH;
  146. }
  147. case V4L2_CID_MPEG_VIDEO_H264_LEVEL:
  148. switch (value) {
  149. case V4L2_MPEG_VIDEO_H264_LEVEL_1_0:
  150. return HFI_H264_LEVEL_1;
  151. case V4L2_MPEG_VIDEO_H264_LEVEL_1B:
  152. return HFI_H264_LEVEL_1b;
  153. case V4L2_MPEG_VIDEO_H264_LEVEL_1_1:
  154. return HFI_H264_LEVEL_11;
  155. case V4L2_MPEG_VIDEO_H264_LEVEL_1_2:
  156. return HFI_H264_LEVEL_12;
  157. case V4L2_MPEG_VIDEO_H264_LEVEL_1_3:
  158. return HFI_H264_LEVEL_13;
  159. case V4L2_MPEG_VIDEO_H264_LEVEL_2_0:
  160. return HFI_H264_LEVEL_2;
  161. case V4L2_MPEG_VIDEO_H264_LEVEL_2_1:
  162. return HFI_H264_LEVEL_21;
  163. case V4L2_MPEG_VIDEO_H264_LEVEL_2_2:
  164. return HFI_H264_LEVEL_22;
  165. case V4L2_MPEG_VIDEO_H264_LEVEL_3_0:
  166. return HFI_H264_LEVEL_3;
  167. case V4L2_MPEG_VIDEO_H264_LEVEL_3_1:
  168. return HFI_H264_LEVEL_31;
  169. case V4L2_MPEG_VIDEO_H264_LEVEL_3_2:
  170. return HFI_H264_LEVEL_32;
  171. case V4L2_MPEG_VIDEO_H264_LEVEL_4_0:
  172. return HFI_H264_LEVEL_4;
  173. case V4L2_MPEG_VIDEO_H264_LEVEL_4_1:
  174. return HFI_H264_LEVEL_41;
  175. case V4L2_MPEG_VIDEO_H264_LEVEL_4_2:
  176. return HFI_H264_LEVEL_42;
  177. case V4L2_MPEG_VIDEO_H264_LEVEL_5_0:
  178. default:
  179. return HFI_H264_LEVEL_5;
  180. case V4L2_MPEG_VIDEO_H264_LEVEL_5_1:
  181. return HFI_H264_LEVEL_51;
  182. }
  183. case V4L2_CID_MPEG_VIDEO_H264_ENTROPY_MODE:
  184. switch (value) {
  185. case V4L2_MPEG_VIDEO_H264_ENTROPY_MODE_CAVLC:
  186. default:
  187. return HFI_H264_ENTROPY_CAVLC;
  188. case V4L2_MPEG_VIDEO_H264_ENTROPY_MODE_CABAC:
  189. return HFI_H264_ENTROPY_CABAC;
  190. }
  191. case V4L2_CID_MPEG_VIDEO_VP8_PROFILE:
  192. switch (value) {
  193. case 0:
  194. default:
  195. return HFI_VPX_PROFILE_VERSION_0;
  196. case 1:
  197. return HFI_VPX_PROFILE_VERSION_1;
  198. case 2:
  199. return HFI_VPX_PROFILE_VERSION_2;
  200. case 3:
  201. return HFI_VPX_PROFILE_VERSION_3;
  202. }
  203. case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE:
  204. switch (value) {
  205. case V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED:
  206. default:
  207. return HFI_H264_DB_MODE_ALL_BOUNDARY;
  208. case V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_DISABLED:
  209. return HFI_H264_DB_MODE_DISABLE;
  210. case V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_DISABLED_AT_SLICE_BOUNDARY:
  211. return HFI_H264_DB_MODE_SKIP_SLICE_BOUNDARY;
  212. }
  213. case V4L2_CID_MPEG_VIDEO_HEVC_PROFILE:
  214. switch (value) {
  215. case V4L2_MPEG_VIDEO_HEVC_PROFILE_MAIN:
  216. default:
  217. return HFI_HEVC_PROFILE_MAIN;
  218. case V4L2_MPEG_VIDEO_HEVC_PROFILE_MAIN_STILL_PICTURE:
  219. return HFI_HEVC_PROFILE_MAIN_STILL_PIC;
  220. case V4L2_MPEG_VIDEO_HEVC_PROFILE_MAIN_10:
  221. return HFI_HEVC_PROFILE_MAIN10;
  222. }
  223. case V4L2_CID_MPEG_VIDEO_HEVC_LEVEL:
  224. switch (value) {
  225. case V4L2_MPEG_VIDEO_HEVC_LEVEL_1:
  226. default:
  227. return HFI_HEVC_LEVEL_1;
  228. case V4L2_MPEG_VIDEO_HEVC_LEVEL_2:
  229. return HFI_HEVC_LEVEL_2;
  230. case V4L2_MPEG_VIDEO_HEVC_LEVEL_2_1:
  231. return HFI_HEVC_LEVEL_21;
  232. case V4L2_MPEG_VIDEO_HEVC_LEVEL_3:
  233. return HFI_HEVC_LEVEL_3;
  234. case V4L2_MPEG_VIDEO_HEVC_LEVEL_3_1:
  235. return HFI_HEVC_LEVEL_31;
  236. case V4L2_MPEG_VIDEO_HEVC_LEVEL_4:
  237. return HFI_HEVC_LEVEL_4;
  238. case V4L2_MPEG_VIDEO_HEVC_LEVEL_4_1:
  239. return HFI_HEVC_LEVEL_41;
  240. case V4L2_MPEG_VIDEO_HEVC_LEVEL_5:
  241. return HFI_HEVC_LEVEL_5;
  242. case V4L2_MPEG_VIDEO_HEVC_LEVEL_5_1:
  243. return HFI_HEVC_LEVEL_51;
  244. case V4L2_MPEG_VIDEO_HEVC_LEVEL_5_2:
  245. return HFI_HEVC_LEVEL_52;
  246. case V4L2_MPEG_VIDEO_HEVC_LEVEL_6:
  247. return HFI_HEVC_LEVEL_6;
  248. case V4L2_MPEG_VIDEO_HEVC_LEVEL_6_1:
  249. return HFI_HEVC_LEVEL_61;
  250. case V4L2_MPEG_VIDEO_HEVC_LEVEL_6_2:
  251. return HFI_HEVC_LEVEL_62;
  252. }
  253. }
  254. return 0;
  255. }
  256. static int
  257. venc_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
  258. {
  259. strlcpy(cap->driver, "qcom-venus", sizeof(cap->driver));
  260. strlcpy(cap->card, "Qualcomm Venus video encoder", sizeof(cap->card));
  261. strlcpy(cap->bus_info, "platform:qcom-venus", sizeof(cap->bus_info));
  262. return 0;
  263. }
  264. static int venc_enum_fmt(struct file *file, void *fh, struct v4l2_fmtdesc *f)
  265. {
  266. struct venus_inst *inst = to_inst(file);
  267. const struct venus_format *fmt;
  268. fmt = find_format_by_index(inst, f->index, f->type);
  269. memset(f->reserved, 0, sizeof(f->reserved));
  270. if (!fmt)
  271. return -EINVAL;
  272. f->pixelformat = fmt->pixfmt;
  273. return 0;
  274. }
  275. static const struct venus_format *
  276. venc_try_fmt_common(struct venus_inst *inst, struct v4l2_format *f)
  277. {
  278. struct v4l2_pix_format_mplane *pixmp = &f->fmt.pix_mp;
  279. struct v4l2_plane_pix_format *pfmt = pixmp->plane_fmt;
  280. const struct venus_format *fmt;
  281. memset(pfmt[0].reserved, 0, sizeof(pfmt[0].reserved));
  282. memset(pixmp->reserved, 0, sizeof(pixmp->reserved));
  283. fmt = find_format(inst, pixmp->pixelformat, f->type);
  284. if (!fmt) {
  285. if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
  286. pixmp->pixelformat = V4L2_PIX_FMT_H264;
  287. else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
  288. pixmp->pixelformat = V4L2_PIX_FMT_NV12;
  289. else
  290. return NULL;
  291. fmt = find_format(inst, pixmp->pixelformat, f->type);
  292. }
  293. pixmp->width = clamp(pixmp->width, frame_width_min(inst),
  294. frame_width_max(inst));
  295. pixmp->height = clamp(pixmp->height, frame_height_min(inst),
  296. frame_height_max(inst));
  297. if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
  298. pixmp->height = ALIGN(pixmp->height, 32);
  299. pixmp->width = ALIGN(pixmp->width, 2);
  300. pixmp->height = ALIGN(pixmp->height, 2);
  301. if (pixmp->field == V4L2_FIELD_ANY)
  302. pixmp->field = V4L2_FIELD_NONE;
  303. pixmp->num_planes = fmt->num_planes;
  304. pixmp->flags = 0;
  305. pfmt[0].sizeimage = venus_helper_get_framesz(pixmp->pixelformat,
  306. pixmp->width,
  307. pixmp->height);
  308. if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
  309. pfmt[0].bytesperline = ALIGN(pixmp->width, 128);
  310. else
  311. pfmt[0].bytesperline = 0;
  312. return fmt;
  313. }
  314. static int venc_try_fmt(struct file *file, void *fh, struct v4l2_format *f)
  315. {
  316. struct venus_inst *inst = to_inst(file);
  317. venc_try_fmt_common(inst, f);
  318. return 0;
  319. }
  320. static int venc_s_fmt(struct file *file, void *fh, struct v4l2_format *f)
  321. {
  322. struct venus_inst *inst = to_inst(file);
  323. struct v4l2_pix_format_mplane *pixmp = &f->fmt.pix_mp;
  324. struct v4l2_pix_format_mplane orig_pixmp;
  325. const struct venus_format *fmt;
  326. struct v4l2_format format;
  327. u32 pixfmt_out = 0, pixfmt_cap = 0;
  328. orig_pixmp = *pixmp;
  329. fmt = venc_try_fmt_common(inst, f);
  330. if (!fmt)
  331. return -EINVAL;
  332. if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
  333. pixfmt_out = pixmp->pixelformat;
  334. pixfmt_cap = inst->fmt_cap->pixfmt;
  335. } else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
  336. pixfmt_cap = pixmp->pixelformat;
  337. pixfmt_out = inst->fmt_out->pixfmt;
  338. }
  339. memset(&format, 0, sizeof(format));
  340. format.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
  341. format.fmt.pix_mp.pixelformat = pixfmt_out;
  342. format.fmt.pix_mp.width = orig_pixmp.width;
  343. format.fmt.pix_mp.height = orig_pixmp.height;
  344. venc_try_fmt_common(inst, &format);
  345. if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
  346. inst->out_width = format.fmt.pix_mp.width;
  347. inst->out_height = format.fmt.pix_mp.height;
  348. inst->colorspace = pixmp->colorspace;
  349. inst->ycbcr_enc = pixmp->ycbcr_enc;
  350. inst->quantization = pixmp->quantization;
  351. inst->xfer_func = pixmp->xfer_func;
  352. }
  353. memset(&format, 0, sizeof(format));
  354. format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
  355. format.fmt.pix_mp.pixelformat = pixfmt_cap;
  356. format.fmt.pix_mp.width = orig_pixmp.width;
  357. format.fmt.pix_mp.height = orig_pixmp.height;
  358. venc_try_fmt_common(inst, &format);
  359. inst->width = format.fmt.pix_mp.width;
  360. inst->height = format.fmt.pix_mp.height;
  361. if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
  362. inst->fmt_out = fmt;
  363. else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
  364. inst->fmt_cap = fmt;
  365. return 0;
  366. }
  367. static int venc_g_fmt(struct file *file, void *fh, struct v4l2_format *f)
  368. {
  369. struct v4l2_pix_format_mplane *pixmp = &f->fmt.pix_mp;
  370. struct venus_inst *inst = to_inst(file);
  371. const struct venus_format *fmt;
  372. if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
  373. fmt = inst->fmt_cap;
  374. else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
  375. fmt = inst->fmt_out;
  376. else
  377. return -EINVAL;
  378. pixmp->pixelformat = fmt->pixfmt;
  379. if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
  380. pixmp->width = inst->width;
  381. pixmp->height = inst->height;
  382. pixmp->colorspace = inst->colorspace;
  383. pixmp->ycbcr_enc = inst->ycbcr_enc;
  384. pixmp->quantization = inst->quantization;
  385. pixmp->xfer_func = inst->xfer_func;
  386. } else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
  387. pixmp->width = inst->out_width;
  388. pixmp->height = inst->out_height;
  389. }
  390. venc_try_fmt_common(inst, f);
  391. return 0;
  392. }
  393. static int
  394. venc_g_selection(struct file *file, void *fh, struct v4l2_selection *s)
  395. {
  396. struct venus_inst *inst = to_inst(file);
  397. if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
  398. return -EINVAL;
  399. switch (s->target) {
  400. case V4L2_SEL_TGT_CROP_DEFAULT:
  401. case V4L2_SEL_TGT_CROP_BOUNDS:
  402. s->r.width = inst->width;
  403. s->r.height = inst->height;
  404. break;
  405. case V4L2_SEL_TGT_CROP:
  406. s->r.width = inst->out_width;
  407. s->r.height = inst->out_height;
  408. break;
  409. default:
  410. return -EINVAL;
  411. }
  412. s->r.top = 0;
  413. s->r.left = 0;
  414. return 0;
  415. }
  416. static int
  417. venc_s_selection(struct file *file, void *fh, struct v4l2_selection *s)
  418. {
  419. struct venus_inst *inst = to_inst(file);
  420. if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
  421. return -EINVAL;
  422. switch (s->target) {
  423. case V4L2_SEL_TGT_CROP:
  424. if (s->r.width != inst->out_width ||
  425. s->r.height != inst->out_height ||
  426. s->r.top != 0 || s->r.left != 0)
  427. return -EINVAL;
  428. break;
  429. default:
  430. return -EINVAL;
  431. }
  432. return 0;
  433. }
  434. static int venc_s_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
  435. {
  436. struct venus_inst *inst = to_inst(file);
  437. struct v4l2_outputparm *out = &a->parm.output;
  438. struct v4l2_fract *timeperframe = &out->timeperframe;
  439. u64 us_per_frame, fps;
  440. if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE &&
  441. a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
  442. return -EINVAL;
  443. memset(out->reserved, 0, sizeof(out->reserved));
  444. if (!timeperframe->denominator)
  445. timeperframe->denominator = inst->timeperframe.denominator;
  446. if (!timeperframe->numerator)
  447. timeperframe->numerator = inst->timeperframe.numerator;
  448. out->capability = V4L2_CAP_TIMEPERFRAME;
  449. us_per_frame = timeperframe->numerator * (u64)USEC_PER_SEC;
  450. do_div(us_per_frame, timeperframe->denominator);
  451. if (!us_per_frame)
  452. return -EINVAL;
  453. fps = (u64)USEC_PER_SEC;
  454. do_div(fps, us_per_frame);
  455. inst->timeperframe = *timeperframe;
  456. inst->fps = fps;
  457. return 0;
  458. }
  459. static int venc_g_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
  460. {
  461. struct venus_inst *inst = to_inst(file);
  462. if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE &&
  463. a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
  464. return -EINVAL;
  465. a->parm.output.capability |= V4L2_CAP_TIMEPERFRAME;
  466. a->parm.output.timeperframe = inst->timeperframe;
  467. return 0;
  468. }
  469. static int venc_enum_framesizes(struct file *file, void *fh,
  470. struct v4l2_frmsizeenum *fsize)
  471. {
  472. struct venus_inst *inst = to_inst(file);
  473. const struct venus_format *fmt;
  474. fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
  475. fmt = find_format(inst, fsize->pixel_format,
  476. V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
  477. if (!fmt) {
  478. fmt = find_format(inst, fsize->pixel_format,
  479. V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
  480. if (!fmt)
  481. return -EINVAL;
  482. }
  483. if (fsize->index)
  484. return -EINVAL;
  485. fsize->stepwise.min_width = frame_width_min(inst);
  486. fsize->stepwise.max_width = frame_width_max(inst);
  487. fsize->stepwise.step_width = frame_width_step(inst);
  488. fsize->stepwise.min_height = frame_height_min(inst);
  489. fsize->stepwise.max_height = frame_height_max(inst);
  490. fsize->stepwise.step_height = frame_height_step(inst);
  491. return 0;
  492. }
  493. static int venc_enum_frameintervals(struct file *file, void *fh,
  494. struct v4l2_frmivalenum *fival)
  495. {
  496. struct venus_inst *inst = to_inst(file);
  497. const struct venus_format *fmt;
  498. fival->type = V4L2_FRMIVAL_TYPE_STEPWISE;
  499. fmt = find_format(inst, fival->pixel_format,
  500. V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
  501. if (!fmt) {
  502. fmt = find_format(inst, fival->pixel_format,
  503. V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
  504. if (!fmt)
  505. return -EINVAL;
  506. }
  507. if (fival->index)
  508. return -EINVAL;
  509. if (!fival->width || !fival->height)
  510. return -EINVAL;
  511. if (fival->width > frame_width_max(inst) ||
  512. fival->width < frame_width_min(inst) ||
  513. fival->height > frame_height_max(inst) ||
  514. fival->height < frame_height_min(inst))
  515. return -EINVAL;
  516. fival->stepwise.min.numerator = 1;
  517. fival->stepwise.min.denominator = frate_max(inst);
  518. fival->stepwise.max.numerator = 1;
  519. fival->stepwise.max.denominator = frate_min(inst);
  520. fival->stepwise.step.numerator = 1;
  521. fival->stepwise.step.denominator = frate_max(inst);
  522. return 0;
  523. }
  524. static const struct v4l2_ioctl_ops venc_ioctl_ops = {
  525. .vidioc_querycap = venc_querycap,
  526. .vidioc_enum_fmt_vid_cap_mplane = venc_enum_fmt,
  527. .vidioc_enum_fmt_vid_out_mplane = venc_enum_fmt,
  528. .vidioc_s_fmt_vid_cap_mplane = venc_s_fmt,
  529. .vidioc_s_fmt_vid_out_mplane = venc_s_fmt,
  530. .vidioc_g_fmt_vid_cap_mplane = venc_g_fmt,
  531. .vidioc_g_fmt_vid_out_mplane = venc_g_fmt,
  532. .vidioc_try_fmt_vid_cap_mplane = venc_try_fmt,
  533. .vidioc_try_fmt_vid_out_mplane = venc_try_fmt,
  534. .vidioc_g_selection = venc_g_selection,
  535. .vidioc_s_selection = venc_s_selection,
  536. .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
  537. .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
  538. .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
  539. .vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf,
  540. .vidioc_qbuf = v4l2_m2m_ioctl_qbuf,
  541. .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
  542. .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf,
  543. .vidioc_streamon = v4l2_m2m_ioctl_streamon,
  544. .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
  545. .vidioc_s_parm = venc_s_parm,
  546. .vidioc_g_parm = venc_g_parm,
  547. .vidioc_enum_framesizes = venc_enum_framesizes,
  548. .vidioc_enum_frameintervals = venc_enum_frameintervals,
  549. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  550. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  551. };
  552. static int venc_set_properties(struct venus_inst *inst)
  553. {
  554. struct venc_controls *ctr = &inst->controls.enc;
  555. struct hfi_intra_period intra_period;
  556. struct hfi_profile_level pl;
  557. struct hfi_framerate frate;
  558. struct hfi_bitrate brate;
  559. struct hfi_idr_period idrp;
  560. u32 ptype, rate_control, bitrate, profile = 0, level = 0;
  561. int ret;
  562. ret = venus_helper_set_work_mode(inst, VIDC_WORK_MODE_2);
  563. if (ret)
  564. return ret;
  565. ret = venus_helper_set_core_usage(inst, VIDC_CORE_ID_2);
  566. if (ret)
  567. return ret;
  568. ptype = HFI_PROPERTY_CONFIG_FRAME_RATE;
  569. frate.buffer_type = HFI_BUFFER_OUTPUT;
  570. frate.framerate = inst->fps * (1 << 16);
  571. ret = hfi_session_set_property(inst, ptype, &frate);
  572. if (ret)
  573. return ret;
  574. if (inst->fmt_cap->pixfmt == V4L2_PIX_FMT_H264) {
  575. struct hfi_h264_vui_timing_info info;
  576. struct hfi_h264_entropy_control entropy;
  577. struct hfi_h264_db_control deblock;
  578. ptype = HFI_PROPERTY_PARAM_VENC_H264_VUI_TIMING_INFO;
  579. info.enable = 1;
  580. info.fixed_framerate = 1;
  581. info.time_scale = NSEC_PER_SEC;
  582. ret = hfi_session_set_property(inst, ptype, &info);
  583. if (ret)
  584. return ret;
  585. ptype = HFI_PROPERTY_PARAM_VENC_H264_ENTROPY_CONTROL;
  586. entropy.entropy_mode = venc_v4l2_to_hfi(
  587. V4L2_CID_MPEG_VIDEO_H264_ENTROPY_MODE,
  588. ctr->h264_entropy_mode);
  589. entropy.cabac_model = HFI_H264_CABAC_MODEL_0;
  590. ret = hfi_session_set_property(inst, ptype, &entropy);
  591. if (ret)
  592. return ret;
  593. ptype = HFI_PROPERTY_PARAM_VENC_H264_DEBLOCK_CONTROL;
  594. deblock.mode = venc_v4l2_to_hfi(
  595. V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE,
  596. ctr->h264_loop_filter_mode);
  597. deblock.slice_alpha_offset = ctr->h264_loop_filter_alpha;
  598. deblock.slice_beta_offset = ctr->h264_loop_filter_beta;
  599. ret = hfi_session_set_property(inst, ptype, &deblock);
  600. if (ret)
  601. return ret;
  602. }
  603. /* IDR periodicity, n:
  604. * n = 0 - only the first I-frame is IDR frame
  605. * n = 1 - all I-frames will be IDR frames
  606. * n > 1 - every n-th I-frame will be IDR frame
  607. */
  608. ptype = HFI_PROPERTY_CONFIG_VENC_IDR_PERIOD;
  609. idrp.idr_period = 0;
  610. ret = hfi_session_set_property(inst, ptype, &idrp);
  611. if (ret)
  612. return ret;
  613. if (ctr->num_b_frames) {
  614. u32 max_num_b_frames = NUM_B_FRAMES_MAX;
  615. ptype = HFI_PROPERTY_PARAM_VENC_MAX_NUM_B_FRAMES;
  616. ret = hfi_session_set_property(inst, ptype, &max_num_b_frames);
  617. if (ret)
  618. return ret;
  619. }
  620. ptype = HFI_PROPERTY_CONFIG_VENC_INTRA_PERIOD;
  621. intra_period.pframes = ctr->num_p_frames;
  622. intra_period.bframes = ctr->num_b_frames;
  623. ret = hfi_session_set_property(inst, ptype, &intra_period);
  624. if (ret)
  625. return ret;
  626. if (ctr->bitrate_mode == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR)
  627. rate_control = HFI_RATE_CONTROL_VBR_CFR;
  628. else
  629. rate_control = HFI_RATE_CONTROL_CBR_CFR;
  630. ptype = HFI_PROPERTY_PARAM_VENC_RATE_CONTROL;
  631. ret = hfi_session_set_property(inst, ptype, &rate_control);
  632. if (ret)
  633. return ret;
  634. if (!ctr->bitrate)
  635. bitrate = 64000;
  636. else
  637. bitrate = ctr->bitrate;
  638. ptype = HFI_PROPERTY_CONFIG_VENC_TARGET_BITRATE;
  639. brate.bitrate = bitrate;
  640. brate.layer_id = 0;
  641. ret = hfi_session_set_property(inst, ptype, &brate);
  642. if (ret)
  643. return ret;
  644. if (!ctr->bitrate_peak)
  645. bitrate *= 2;
  646. else
  647. bitrate = ctr->bitrate_peak;
  648. ptype = HFI_PROPERTY_CONFIG_VENC_MAX_BITRATE;
  649. brate.bitrate = bitrate;
  650. brate.layer_id = 0;
  651. ret = hfi_session_set_property(inst, ptype, &brate);
  652. if (ret)
  653. return ret;
  654. if (inst->fmt_cap->pixfmt == V4L2_PIX_FMT_H264) {
  655. profile = venc_v4l2_to_hfi(V4L2_CID_MPEG_VIDEO_H264_PROFILE,
  656. ctr->profile.h264);
  657. level = venc_v4l2_to_hfi(V4L2_CID_MPEG_VIDEO_H264_LEVEL,
  658. ctr->level.h264);
  659. } else if (inst->fmt_cap->pixfmt == V4L2_PIX_FMT_VP8) {
  660. profile = venc_v4l2_to_hfi(V4L2_CID_MPEG_VIDEO_VP8_PROFILE,
  661. ctr->profile.vpx);
  662. level = 0;
  663. } else if (inst->fmt_cap->pixfmt == V4L2_PIX_FMT_MPEG4) {
  664. profile = venc_v4l2_to_hfi(V4L2_CID_MPEG_VIDEO_MPEG4_PROFILE,
  665. ctr->profile.mpeg4);
  666. level = venc_v4l2_to_hfi(V4L2_CID_MPEG_VIDEO_MPEG4_LEVEL,
  667. ctr->level.mpeg4);
  668. } else if (inst->fmt_cap->pixfmt == V4L2_PIX_FMT_H263) {
  669. profile = 0;
  670. level = 0;
  671. } else if (inst->fmt_cap->pixfmt == V4L2_PIX_FMT_HEVC) {
  672. profile = venc_v4l2_to_hfi(V4L2_CID_MPEG_VIDEO_HEVC_PROFILE,
  673. ctr->profile.hevc);
  674. level = venc_v4l2_to_hfi(V4L2_CID_MPEG_VIDEO_HEVC_LEVEL,
  675. ctr->level.hevc);
  676. }
  677. ptype = HFI_PROPERTY_PARAM_PROFILE_LEVEL_CURRENT;
  678. pl.profile = profile;
  679. pl.level = level;
  680. ret = hfi_session_set_property(inst, ptype, &pl);
  681. if (ret)
  682. return ret;
  683. return 0;
  684. }
  685. static int venc_init_session(struct venus_inst *inst)
  686. {
  687. int ret;
  688. ret = hfi_session_init(inst, inst->fmt_cap->pixfmt);
  689. if (ret)
  690. return ret;
  691. ret = venus_helper_set_input_resolution(inst, inst->width,
  692. inst->height);
  693. if (ret)
  694. goto deinit;
  695. ret = venus_helper_set_output_resolution(inst, inst->width,
  696. inst->height,
  697. HFI_BUFFER_OUTPUT);
  698. if (ret)
  699. goto deinit;
  700. ret = venus_helper_set_color_format(inst, inst->fmt_out->pixfmt);
  701. if (ret)
  702. goto deinit;
  703. ret = venc_set_properties(inst);
  704. if (ret)
  705. goto deinit;
  706. return 0;
  707. deinit:
  708. hfi_session_deinit(inst);
  709. return ret;
  710. }
  711. static int venc_out_num_buffers(struct venus_inst *inst, unsigned int *num)
  712. {
  713. struct hfi_buffer_requirements bufreq;
  714. int ret;
  715. ret = venc_init_session(inst);
  716. if (ret)
  717. return ret;
  718. ret = venus_helper_get_bufreq(inst, HFI_BUFFER_INPUT, &bufreq);
  719. *num = bufreq.count_actual;
  720. hfi_session_deinit(inst);
  721. return ret;
  722. }
  723. static int venc_queue_setup(struct vb2_queue *q,
  724. unsigned int *num_buffers, unsigned int *num_planes,
  725. unsigned int sizes[], struct device *alloc_devs[])
  726. {
  727. struct venus_inst *inst = vb2_get_drv_priv(q);
  728. unsigned int num, min = 4;
  729. int ret = 0;
  730. if (*num_planes) {
  731. if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE &&
  732. *num_planes != inst->fmt_out->num_planes)
  733. return -EINVAL;
  734. if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE &&
  735. *num_planes != inst->fmt_cap->num_planes)
  736. return -EINVAL;
  737. if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE &&
  738. sizes[0] < inst->input_buf_size)
  739. return -EINVAL;
  740. if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE &&
  741. sizes[0] < inst->output_buf_size)
  742. return -EINVAL;
  743. return 0;
  744. }
  745. switch (q->type) {
  746. case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
  747. *num_planes = inst->fmt_out->num_planes;
  748. ret = venc_out_num_buffers(inst, &num);
  749. if (ret)
  750. break;
  751. num = max(num, min);
  752. *num_buffers = max(*num_buffers, num);
  753. inst->num_input_bufs = *num_buffers;
  754. sizes[0] = venus_helper_get_framesz(inst->fmt_out->pixfmt,
  755. inst->width,
  756. inst->height);
  757. inst->input_buf_size = sizes[0];
  758. break;
  759. case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
  760. *num_planes = inst->fmt_cap->num_planes;
  761. *num_buffers = max(*num_buffers, min);
  762. inst->num_output_bufs = *num_buffers;
  763. sizes[0] = venus_helper_get_framesz(inst->fmt_cap->pixfmt,
  764. inst->width,
  765. inst->height);
  766. inst->output_buf_size = sizes[0];
  767. break;
  768. default:
  769. ret = -EINVAL;
  770. break;
  771. }
  772. return ret;
  773. }
  774. static int venc_verify_conf(struct venus_inst *inst)
  775. {
  776. enum hfi_version ver = inst->core->res->hfi_version;
  777. struct hfi_buffer_requirements bufreq;
  778. int ret;
  779. if (!inst->num_input_bufs || !inst->num_output_bufs)
  780. return -EINVAL;
  781. ret = venus_helper_get_bufreq(inst, HFI_BUFFER_OUTPUT, &bufreq);
  782. if (ret)
  783. return ret;
  784. if (inst->num_output_bufs < bufreq.count_actual ||
  785. inst->num_output_bufs < HFI_BUFREQ_COUNT_MIN(&bufreq, ver))
  786. return -EINVAL;
  787. ret = venus_helper_get_bufreq(inst, HFI_BUFFER_INPUT, &bufreq);
  788. if (ret)
  789. return ret;
  790. if (inst->num_input_bufs < bufreq.count_actual ||
  791. inst->num_input_bufs < HFI_BUFREQ_COUNT_MIN(&bufreq, ver))
  792. return -EINVAL;
  793. return 0;
  794. }
  795. static int venc_start_streaming(struct vb2_queue *q, unsigned int count)
  796. {
  797. struct venus_inst *inst = vb2_get_drv_priv(q);
  798. int ret;
  799. mutex_lock(&inst->lock);
  800. if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
  801. inst->streamon_out = 1;
  802. else
  803. inst->streamon_cap = 1;
  804. if (!(inst->streamon_out & inst->streamon_cap)) {
  805. mutex_unlock(&inst->lock);
  806. return 0;
  807. }
  808. venus_helper_init_instance(inst);
  809. inst->sequence_cap = 0;
  810. inst->sequence_out = 0;
  811. ret = venc_init_session(inst);
  812. if (ret)
  813. goto bufs_done;
  814. ret = venc_set_properties(inst);
  815. if (ret)
  816. goto deinit_sess;
  817. ret = venc_verify_conf(inst);
  818. if (ret)
  819. goto deinit_sess;
  820. ret = venus_helper_set_num_bufs(inst, inst->num_input_bufs,
  821. inst->num_output_bufs, 0);
  822. if (ret)
  823. goto deinit_sess;
  824. ret = venus_helper_vb2_start_streaming(inst);
  825. if (ret)
  826. goto deinit_sess;
  827. mutex_unlock(&inst->lock);
  828. return 0;
  829. deinit_sess:
  830. hfi_session_deinit(inst);
  831. bufs_done:
  832. venus_helper_buffers_done(inst, VB2_BUF_STATE_QUEUED);
  833. if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
  834. inst->streamon_out = 0;
  835. else
  836. inst->streamon_cap = 0;
  837. mutex_unlock(&inst->lock);
  838. return ret;
  839. }
  840. static const struct vb2_ops venc_vb2_ops = {
  841. .queue_setup = venc_queue_setup,
  842. .buf_init = venus_helper_vb2_buf_init,
  843. .buf_prepare = venus_helper_vb2_buf_prepare,
  844. .start_streaming = venc_start_streaming,
  845. .stop_streaming = venus_helper_vb2_stop_streaming,
  846. .buf_queue = venus_helper_vb2_buf_queue,
  847. };
  848. static void venc_buf_done(struct venus_inst *inst, unsigned int buf_type,
  849. u32 tag, u32 bytesused, u32 data_offset, u32 flags,
  850. u32 hfi_flags, u64 timestamp_us)
  851. {
  852. struct vb2_v4l2_buffer *vbuf;
  853. struct vb2_buffer *vb;
  854. unsigned int type;
  855. if (buf_type == HFI_BUFFER_INPUT)
  856. type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
  857. else
  858. type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
  859. vbuf = venus_helper_find_buf(inst, type, tag);
  860. if (!vbuf)
  861. return;
  862. vbuf->flags = flags;
  863. if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
  864. vb = &vbuf->vb2_buf;
  865. vb2_set_plane_payload(vb, 0, bytesused + data_offset);
  866. vb->planes[0].data_offset = data_offset;
  867. vb->timestamp = timestamp_us * NSEC_PER_USEC;
  868. vbuf->sequence = inst->sequence_cap++;
  869. } else {
  870. vbuf->sequence = inst->sequence_out++;
  871. }
  872. v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_DONE);
  873. }
  874. static void venc_event_notify(struct venus_inst *inst, u32 event,
  875. struct hfi_event_data *data)
  876. {
  877. struct device *dev = inst->core->dev_enc;
  878. if (event == EVT_SESSION_ERROR) {
  879. inst->session_error = true;
  880. dev_err(dev, "enc: event session error %x\n", inst->error);
  881. }
  882. }
  883. static const struct hfi_inst_ops venc_hfi_ops = {
  884. .buf_done = venc_buf_done,
  885. .event_notify = venc_event_notify,
  886. };
  887. static const struct v4l2_m2m_ops venc_m2m_ops = {
  888. .device_run = venus_helper_m2m_device_run,
  889. .job_abort = venus_helper_m2m_job_abort,
  890. };
  891. static int m2m_queue_init(void *priv, struct vb2_queue *src_vq,
  892. struct vb2_queue *dst_vq)
  893. {
  894. struct venus_inst *inst = priv;
  895. int ret;
  896. src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
  897. src_vq->io_modes = VB2_MMAP | VB2_DMABUF;
  898. src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
  899. src_vq->ops = &venc_vb2_ops;
  900. src_vq->mem_ops = &vb2_dma_sg_memops;
  901. src_vq->drv_priv = inst;
  902. src_vq->buf_struct_size = sizeof(struct venus_buffer);
  903. src_vq->allow_zero_bytesused = 1;
  904. src_vq->min_buffers_needed = 1;
  905. src_vq->dev = inst->core->dev;
  906. if (inst->core->res->hfi_version == HFI_VERSION_1XX)
  907. src_vq->bidirectional = 1;
  908. ret = vb2_queue_init(src_vq);
  909. if (ret)
  910. return ret;
  911. dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
  912. dst_vq->io_modes = VB2_MMAP | VB2_DMABUF;
  913. dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
  914. dst_vq->ops = &venc_vb2_ops;
  915. dst_vq->mem_ops = &vb2_dma_sg_memops;
  916. dst_vq->drv_priv = inst;
  917. dst_vq->buf_struct_size = sizeof(struct venus_buffer);
  918. dst_vq->allow_zero_bytesused = 1;
  919. dst_vq->min_buffers_needed = 1;
  920. dst_vq->dev = inst->core->dev;
  921. ret = vb2_queue_init(dst_vq);
  922. if (ret) {
  923. vb2_queue_release(src_vq);
  924. return ret;
  925. }
  926. return 0;
  927. }
  928. static void venc_inst_init(struct venus_inst *inst)
  929. {
  930. inst->fmt_cap = &venc_formats[2];
  931. inst->fmt_out = &venc_formats[0];
  932. inst->width = 1280;
  933. inst->height = ALIGN(720, 32);
  934. inst->out_width = 1280;
  935. inst->out_height = 720;
  936. inst->fps = 15;
  937. inst->timeperframe.numerator = 1;
  938. inst->timeperframe.denominator = 15;
  939. inst->hfi_codec = HFI_VIDEO_CODEC_H264;
  940. }
  941. static int venc_open(struct file *file)
  942. {
  943. struct venus_core *core = video_drvdata(file);
  944. struct venus_inst *inst;
  945. int ret;
  946. inst = kzalloc(sizeof(*inst), GFP_KERNEL);
  947. if (!inst)
  948. return -ENOMEM;
  949. INIT_LIST_HEAD(&inst->dpbbufs);
  950. INIT_LIST_HEAD(&inst->registeredbufs);
  951. INIT_LIST_HEAD(&inst->internalbufs);
  952. INIT_LIST_HEAD(&inst->list);
  953. mutex_init(&inst->lock);
  954. inst->core = core;
  955. inst->session_type = VIDC_SESSION_TYPE_ENC;
  956. venus_helper_init_instance(inst);
  957. ret = pm_runtime_get_sync(core->dev_enc);
  958. if (ret < 0)
  959. goto err_free_inst;
  960. ret = venc_ctrl_init(inst);
  961. if (ret)
  962. goto err_put_sync;
  963. ret = hfi_session_create(inst, &venc_hfi_ops);
  964. if (ret)
  965. goto err_ctrl_deinit;
  966. venc_inst_init(inst);
  967. /*
  968. * create m2m device for every instance, the m2m context scheduling
  969. * is made by firmware side so we do not need to care about.
  970. */
  971. inst->m2m_dev = v4l2_m2m_init(&venc_m2m_ops);
  972. if (IS_ERR(inst->m2m_dev)) {
  973. ret = PTR_ERR(inst->m2m_dev);
  974. goto err_session_destroy;
  975. }
  976. inst->m2m_ctx = v4l2_m2m_ctx_init(inst->m2m_dev, inst, m2m_queue_init);
  977. if (IS_ERR(inst->m2m_ctx)) {
  978. ret = PTR_ERR(inst->m2m_ctx);
  979. goto err_m2m_release;
  980. }
  981. v4l2_fh_init(&inst->fh, core->vdev_enc);
  982. inst->fh.ctrl_handler = &inst->ctrl_handler;
  983. v4l2_fh_add(&inst->fh);
  984. inst->fh.m2m_ctx = inst->m2m_ctx;
  985. file->private_data = &inst->fh;
  986. return 0;
  987. err_m2m_release:
  988. v4l2_m2m_release(inst->m2m_dev);
  989. err_session_destroy:
  990. hfi_session_destroy(inst);
  991. err_ctrl_deinit:
  992. venc_ctrl_deinit(inst);
  993. err_put_sync:
  994. pm_runtime_put_sync(core->dev_enc);
  995. err_free_inst:
  996. kfree(inst);
  997. return ret;
  998. }
  999. static int venc_close(struct file *file)
  1000. {
  1001. struct venus_inst *inst = to_inst(file);
  1002. v4l2_m2m_ctx_release(inst->m2m_ctx);
  1003. v4l2_m2m_release(inst->m2m_dev);
  1004. venc_ctrl_deinit(inst);
  1005. hfi_session_destroy(inst);
  1006. mutex_destroy(&inst->lock);
  1007. v4l2_fh_del(&inst->fh);
  1008. v4l2_fh_exit(&inst->fh);
  1009. pm_runtime_put_sync(inst->core->dev_enc);
  1010. kfree(inst);
  1011. return 0;
  1012. }
  1013. static const struct v4l2_file_operations venc_fops = {
  1014. .owner = THIS_MODULE,
  1015. .open = venc_open,
  1016. .release = venc_close,
  1017. .unlocked_ioctl = video_ioctl2,
  1018. .poll = v4l2_m2m_fop_poll,
  1019. .mmap = v4l2_m2m_fop_mmap,
  1020. #ifdef CONFIG_COMPAT
  1021. .compat_ioctl32 = v4l2_compat_ioctl32,
  1022. #endif
  1023. };
  1024. static int venc_probe(struct platform_device *pdev)
  1025. {
  1026. struct device *dev = &pdev->dev;
  1027. struct video_device *vdev;
  1028. struct venus_core *core;
  1029. int ret;
  1030. if (!dev->parent)
  1031. return -EPROBE_DEFER;
  1032. core = dev_get_drvdata(dev->parent);
  1033. if (!core)
  1034. return -EPROBE_DEFER;
  1035. if (IS_V3(core) || IS_V4(core)) {
  1036. core->core1_clk = devm_clk_get(dev, "core");
  1037. if (IS_ERR(core->core1_clk))
  1038. return PTR_ERR(core->core1_clk);
  1039. }
  1040. if (IS_V4(core)) {
  1041. core->core1_bus_clk = devm_clk_get(dev, "bus");
  1042. if (IS_ERR(core->core1_bus_clk))
  1043. return PTR_ERR(core->core1_bus_clk);
  1044. }
  1045. platform_set_drvdata(pdev, core);
  1046. vdev = video_device_alloc();
  1047. if (!vdev)
  1048. return -ENOMEM;
  1049. strlcpy(vdev->name, "qcom-venus-encoder", sizeof(vdev->name));
  1050. vdev->release = video_device_release;
  1051. vdev->fops = &venc_fops;
  1052. vdev->ioctl_ops = &venc_ioctl_ops;
  1053. vdev->vfl_dir = VFL_DIR_M2M;
  1054. vdev->v4l2_dev = &core->v4l2_dev;
  1055. vdev->device_caps = V4L2_CAP_VIDEO_M2M_MPLANE | V4L2_CAP_STREAMING;
  1056. ret = video_register_device(vdev, VFL_TYPE_GRABBER, -1);
  1057. if (ret)
  1058. goto err_vdev_release;
  1059. core->vdev_enc = vdev;
  1060. core->dev_enc = dev;
  1061. video_set_drvdata(vdev, core);
  1062. pm_runtime_enable(dev);
  1063. return 0;
  1064. err_vdev_release:
  1065. video_device_release(vdev);
  1066. return ret;
  1067. }
  1068. static int venc_remove(struct platform_device *pdev)
  1069. {
  1070. struct venus_core *core = dev_get_drvdata(pdev->dev.parent);
  1071. video_unregister_device(core->vdev_enc);
  1072. pm_runtime_disable(core->dev_enc);
  1073. return 0;
  1074. }
  1075. static __maybe_unused int venc_runtime_suspend(struct device *dev)
  1076. {
  1077. struct venus_core *core = dev_get_drvdata(dev);
  1078. int ret;
  1079. if (IS_V1(core))
  1080. return 0;
  1081. ret = venus_helper_power_enable(core, VIDC_SESSION_TYPE_ENC, true);
  1082. if (ret)
  1083. return ret;
  1084. if (IS_V4(core))
  1085. clk_disable_unprepare(core->core1_bus_clk);
  1086. clk_disable_unprepare(core->core1_clk);
  1087. return venus_helper_power_enable(core, VIDC_SESSION_TYPE_ENC, false);
  1088. }
  1089. static __maybe_unused int venc_runtime_resume(struct device *dev)
  1090. {
  1091. struct venus_core *core = dev_get_drvdata(dev);
  1092. int ret;
  1093. if (IS_V1(core))
  1094. return 0;
  1095. ret = venus_helper_power_enable(core, VIDC_SESSION_TYPE_ENC, true);
  1096. if (ret)
  1097. return ret;
  1098. ret = clk_prepare_enable(core->core1_clk);
  1099. if (ret)
  1100. goto err_power_disable;
  1101. if (IS_V4(core))
  1102. ret = clk_prepare_enable(core->core1_bus_clk);
  1103. if (ret)
  1104. goto err_unprepare_core1;
  1105. return venus_helper_power_enable(core, VIDC_SESSION_TYPE_ENC, false);
  1106. err_unprepare_core1:
  1107. clk_disable_unprepare(core->core1_clk);
  1108. err_power_disable:
  1109. venus_helper_power_enable(core, VIDC_SESSION_TYPE_ENC, false);
  1110. return ret;
  1111. }
  1112. static const struct dev_pm_ops venc_pm_ops = {
  1113. SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
  1114. pm_runtime_force_resume)
  1115. SET_RUNTIME_PM_OPS(venc_runtime_suspend, venc_runtime_resume, NULL)
  1116. };
  1117. static const struct of_device_id venc_dt_match[] = {
  1118. { .compatible = "venus-encoder" },
  1119. { }
  1120. };
  1121. MODULE_DEVICE_TABLE(of, venc_dt_match);
  1122. static struct platform_driver qcom_venus_enc_driver = {
  1123. .probe = venc_probe,
  1124. .remove = venc_remove,
  1125. .driver = {
  1126. .name = "qcom-venus-encoder",
  1127. .of_match_table = venc_dt_match,
  1128. .pm = &venc_pm_ops,
  1129. },
  1130. };
  1131. module_platform_driver(qcom_venus_enc_driver);
  1132. MODULE_ALIAS("platform:qcom-venus-encoder");
  1133. MODULE_DESCRIPTION("Qualcomm Venus video encoder driver");
  1134. MODULE_LICENSE("GPL v2");