s5p_mfc_enc.c 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942
  1. /*
  2. * linux/drivers/media/platform/s5p-mfc/s5p_mfc_enc.c
  3. *
  4. * Copyright (c) 2010-2011 Samsung Electronics Co., Ltd.
  5. * http://www.samsung.com/
  6. *
  7. * Jeongtae Park <jtp.park@samsung.com>
  8. * Kamil Debski <k.debski@samsung.com>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. */
  15. #include <linux/clk.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/io.h>
  18. #include <linux/module.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/sched.h>
  21. #include <linux/version.h>
  22. #include <linux/videodev2.h>
  23. #include <media/v4l2-event.h>
  24. #include <linux/workqueue.h>
  25. #include <media/v4l2-ctrls.h>
  26. #include <media/videobuf2-core.h>
  27. #include "s5p_mfc_common.h"
  28. #include "s5p_mfc_debug.h"
  29. #include "s5p_mfc_enc.h"
  30. #include "s5p_mfc_intr.h"
  31. #include "s5p_mfc_opr.h"
  32. #define DEF_SRC_FMT_ENC V4L2_PIX_FMT_NV12MT
  33. #define DEF_DST_FMT_ENC V4L2_PIX_FMT_H264
  34. static struct s5p_mfc_fmt formats[] = {
  35. {
  36. .name = "4:2:0 2 Planes 16x16 Tiles",
  37. .fourcc = V4L2_PIX_FMT_NV12MT_16X16,
  38. .codec_mode = S5P_MFC_CODEC_NONE,
  39. .type = MFC_FMT_RAW,
  40. .num_planes = 2,
  41. },
  42. {
  43. .name = "4:2:0 2 Planes 64x32 Tiles",
  44. .fourcc = V4L2_PIX_FMT_NV12MT,
  45. .codec_mode = S5P_MFC_CODEC_NONE,
  46. .type = MFC_FMT_RAW,
  47. .num_planes = 2,
  48. },
  49. {
  50. .name = "4:2:0 2 Planes Y/CbCr",
  51. .fourcc = V4L2_PIX_FMT_NV12M,
  52. .codec_mode = S5P_MFC_CODEC_NONE,
  53. .type = MFC_FMT_RAW,
  54. .num_planes = 2,
  55. },
  56. {
  57. .name = "4:2:0 2 Planes Y/CrCb",
  58. .fourcc = V4L2_PIX_FMT_NV21M,
  59. .codec_mode = S5P_MFC_CODEC_NONE,
  60. .type = MFC_FMT_RAW,
  61. .num_planes = 2,
  62. },
  63. {
  64. .name = "H264 Encoded Stream",
  65. .fourcc = V4L2_PIX_FMT_H264,
  66. .codec_mode = S5P_MFC_CODEC_H264_ENC,
  67. .type = MFC_FMT_ENC,
  68. .num_planes = 1,
  69. },
  70. {
  71. .name = "MPEG4 Encoded Stream",
  72. .fourcc = V4L2_PIX_FMT_MPEG4,
  73. .codec_mode = S5P_MFC_CODEC_MPEG4_ENC,
  74. .type = MFC_FMT_ENC,
  75. .num_planes = 1,
  76. },
  77. {
  78. .name = "H263 Encoded Stream",
  79. .fourcc = V4L2_PIX_FMT_H263,
  80. .codec_mode = S5P_MFC_CODEC_H263_ENC,
  81. .type = MFC_FMT_ENC,
  82. .num_planes = 1,
  83. },
  84. };
  85. #define NUM_FORMATS ARRAY_SIZE(formats)
  86. static struct s5p_mfc_fmt *find_format(struct v4l2_format *f, unsigned int t)
  87. {
  88. unsigned int i;
  89. for (i = 0; i < NUM_FORMATS; i++) {
  90. if (formats[i].fourcc == f->fmt.pix_mp.pixelformat &&
  91. formats[i].type == t)
  92. return &formats[i];
  93. }
  94. return NULL;
  95. }
  96. static struct mfc_control controls[] = {
  97. {
  98. .id = V4L2_CID_MPEG_VIDEO_GOP_SIZE,
  99. .type = V4L2_CTRL_TYPE_INTEGER,
  100. .minimum = 0,
  101. .maximum = (1 << 16) - 1,
  102. .step = 1,
  103. .default_value = 0,
  104. },
  105. {
  106. .id = V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE,
  107. .type = V4L2_CTRL_TYPE_MENU,
  108. .minimum = V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE,
  109. .maximum = V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES,
  110. .default_value = V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE,
  111. .menu_skip_mask = 0,
  112. },
  113. {
  114. .id = V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB,
  115. .type = V4L2_CTRL_TYPE_INTEGER,
  116. .minimum = 1,
  117. .maximum = (1 << 16) - 1,
  118. .step = 1,
  119. .default_value = 1,
  120. },
  121. {
  122. .id = V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES,
  123. .type = V4L2_CTRL_TYPE_INTEGER,
  124. .minimum = 1900,
  125. .maximum = (1 << 30) - 1,
  126. .step = 1,
  127. .default_value = 1900,
  128. },
  129. {
  130. .id = V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB,
  131. .type = V4L2_CTRL_TYPE_INTEGER,
  132. .minimum = 0,
  133. .maximum = (1 << 16) - 1,
  134. .step = 1,
  135. .default_value = 0,
  136. },
  137. {
  138. .id = V4L2_CID_MPEG_MFC51_VIDEO_PADDING,
  139. .type = V4L2_CTRL_TYPE_BOOLEAN,
  140. .name = "Padding Control Enable",
  141. .minimum = 0,
  142. .maximum = 1,
  143. .step = 1,
  144. .default_value = 0,
  145. },
  146. {
  147. .id = V4L2_CID_MPEG_MFC51_VIDEO_PADDING_YUV,
  148. .type = V4L2_CTRL_TYPE_INTEGER,
  149. .name = "Padding Color YUV Value",
  150. .minimum = 0,
  151. .maximum = (1 << 25) - 1,
  152. .step = 1,
  153. .default_value = 0,
  154. },
  155. {
  156. .id = V4L2_CID_MPEG_VIDEO_FRAME_RC_ENABLE,
  157. .type = V4L2_CTRL_TYPE_BOOLEAN,
  158. .minimum = 0,
  159. .maximum = 1,
  160. .step = 1,
  161. .default_value = 0,
  162. },
  163. {
  164. .id = V4L2_CID_MPEG_VIDEO_BITRATE,
  165. .type = V4L2_CTRL_TYPE_INTEGER,
  166. .minimum = 1,
  167. .maximum = (1 << 30) - 1,
  168. .step = 1,
  169. .default_value = 1,
  170. },
  171. {
  172. .id = V4L2_CID_MPEG_MFC51_VIDEO_RC_REACTION_COEFF,
  173. .type = V4L2_CTRL_TYPE_INTEGER,
  174. .name = "Rate Control Reaction Coeff.",
  175. .minimum = 1,
  176. .maximum = (1 << 16) - 1,
  177. .step = 1,
  178. .default_value = 1,
  179. },
  180. {
  181. .id = V4L2_CID_MPEG_MFC51_VIDEO_FORCE_FRAME_TYPE,
  182. .type = V4L2_CTRL_TYPE_MENU,
  183. .name = "Force frame type",
  184. .minimum = V4L2_MPEG_MFC51_VIDEO_FORCE_FRAME_TYPE_DISABLED,
  185. .maximum = V4L2_MPEG_MFC51_VIDEO_FORCE_FRAME_TYPE_NOT_CODED,
  186. .default_value = V4L2_MPEG_MFC51_VIDEO_FORCE_FRAME_TYPE_DISABLED,
  187. .menu_skip_mask = 0,
  188. },
  189. {
  190. .id = V4L2_CID_MPEG_VIDEO_VBV_SIZE,
  191. .type = V4L2_CTRL_TYPE_INTEGER,
  192. .minimum = 0,
  193. .maximum = (1 << 16) - 1,
  194. .step = 1,
  195. .default_value = 0,
  196. },
  197. {
  198. .id = V4L2_CID_MPEG_VIDEO_H264_CPB_SIZE,
  199. .type = V4L2_CTRL_TYPE_INTEGER,
  200. .minimum = 0,
  201. .maximum = (1 << 16) - 1,
  202. .step = 1,
  203. .default_value = 0,
  204. },
  205. {
  206. .id = V4L2_CID_MPEG_VIDEO_HEADER_MODE,
  207. .type = V4L2_CTRL_TYPE_MENU,
  208. .minimum = V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE,
  209. .maximum = V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME,
  210. .default_value = V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE,
  211. .menu_skip_mask = 0,
  212. },
  213. {
  214. .id = V4L2_CID_MPEG_MFC51_VIDEO_FRAME_SKIP_MODE,
  215. .type = V4L2_CTRL_TYPE_MENU,
  216. .name = "Frame Skip Enable",
  217. .minimum = V4L2_MPEG_MFC51_VIDEO_FRAME_SKIP_MODE_DISABLED,
  218. .maximum = V4L2_MPEG_MFC51_VIDEO_FRAME_SKIP_MODE_BUF_LIMIT,
  219. .menu_skip_mask = 0,
  220. .default_value = V4L2_MPEG_MFC51_VIDEO_FRAME_SKIP_MODE_DISABLED,
  221. },
  222. {
  223. .id = V4L2_CID_MPEG_MFC51_VIDEO_RC_FIXED_TARGET_BIT,
  224. .type = V4L2_CTRL_TYPE_BOOLEAN,
  225. .name = "Fixed Target Bit Enable",
  226. .minimum = 0,
  227. .maximum = 1,
  228. .default_value = 0,
  229. .menu_skip_mask = 0,
  230. },
  231. {
  232. .id = V4L2_CID_MPEG_VIDEO_B_FRAMES,
  233. .type = V4L2_CTRL_TYPE_INTEGER,
  234. .minimum = 0,
  235. .maximum = 2,
  236. .step = 1,
  237. .default_value = 0,
  238. },
  239. {
  240. .id = V4L2_CID_MPEG_VIDEO_H264_PROFILE,
  241. .type = V4L2_CTRL_TYPE_MENU,
  242. .minimum = V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE,
  243. .maximum = V4L2_MPEG_VIDEO_H264_PROFILE_MULTIVIEW_HIGH,
  244. .default_value = V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE,
  245. .menu_skip_mask = ~(
  246. (1 << V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE) |
  247. (1 << V4L2_MPEG_VIDEO_H264_PROFILE_MAIN) |
  248. (1 << V4L2_MPEG_VIDEO_H264_PROFILE_HIGH)
  249. ),
  250. },
  251. {
  252. .id = V4L2_CID_MPEG_VIDEO_H264_LEVEL,
  253. .type = V4L2_CTRL_TYPE_MENU,
  254. .minimum = V4L2_MPEG_VIDEO_H264_LEVEL_1_0,
  255. .maximum = V4L2_MPEG_VIDEO_H264_LEVEL_4_0,
  256. .default_value = V4L2_MPEG_VIDEO_H264_LEVEL_1_0,
  257. },
  258. {
  259. .id = V4L2_CID_MPEG_VIDEO_MPEG4_LEVEL,
  260. .type = V4L2_CTRL_TYPE_MENU,
  261. .minimum = V4L2_MPEG_VIDEO_MPEG4_LEVEL_0,
  262. .maximum = V4L2_MPEG_VIDEO_MPEG4_LEVEL_5,
  263. .default_value = V4L2_MPEG_VIDEO_MPEG4_LEVEL_0,
  264. .menu_skip_mask = 0,
  265. },
  266. {
  267. .id = V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE,
  268. .type = V4L2_CTRL_TYPE_MENU,
  269. .minimum = V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED,
  270. .maximum = V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_DISABLED_AT_SLICE_BOUNDARY,
  271. .default_value = V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED,
  272. .menu_skip_mask = 0,
  273. },
  274. {
  275. .id = V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA,
  276. .type = V4L2_CTRL_TYPE_INTEGER,
  277. .minimum = -6,
  278. .maximum = 6,
  279. .step = 1,
  280. .default_value = 0,
  281. },
  282. {
  283. .id = V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA,
  284. .type = V4L2_CTRL_TYPE_INTEGER,
  285. .minimum = -6,
  286. .maximum = 6,
  287. .step = 1,
  288. .default_value = 0,
  289. },
  290. {
  291. .id = V4L2_CID_MPEG_VIDEO_H264_ENTROPY_MODE,
  292. .type = V4L2_CTRL_TYPE_MENU,
  293. .minimum = V4L2_MPEG_VIDEO_H264_ENTROPY_MODE_CAVLC,
  294. .maximum = V4L2_MPEG_VIDEO_H264_ENTROPY_MODE_CABAC,
  295. .default_value = V4L2_MPEG_VIDEO_H264_ENTROPY_MODE_CAVLC,
  296. .menu_skip_mask = 0,
  297. },
  298. {
  299. .id = V4L2_CID_MPEG_MFC51_VIDEO_H264_NUM_REF_PIC_FOR_P,
  300. .type = V4L2_CTRL_TYPE_INTEGER,
  301. .name = "The Number of Ref. Pic for P",
  302. .minimum = 1,
  303. .maximum = 2,
  304. .step = 1,
  305. .default_value = 1,
  306. },
  307. {
  308. .id = V4L2_CID_MPEG_VIDEO_H264_8X8_TRANSFORM,
  309. .type = V4L2_CTRL_TYPE_BOOLEAN,
  310. .minimum = 0,
  311. .maximum = 1,
  312. .step = 1,
  313. .default_value = 0,
  314. },
  315. {
  316. .id = V4L2_CID_MPEG_VIDEO_MB_RC_ENABLE,
  317. .type = V4L2_CTRL_TYPE_BOOLEAN,
  318. .minimum = 0,
  319. .maximum = 1,
  320. .step = 1,
  321. .default_value = 0,
  322. },
  323. {
  324. .id = V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP,
  325. .type = V4L2_CTRL_TYPE_INTEGER,
  326. .minimum = 0,
  327. .maximum = 51,
  328. .step = 1,
  329. .default_value = 1,
  330. },
  331. {
  332. .id = V4L2_CID_MPEG_VIDEO_H264_MIN_QP,
  333. .type = V4L2_CTRL_TYPE_INTEGER,
  334. .minimum = 0,
  335. .maximum = 51,
  336. .step = 1,
  337. .default_value = 1,
  338. },
  339. {
  340. .id = V4L2_CID_MPEG_VIDEO_H264_MAX_QP,
  341. .type = V4L2_CTRL_TYPE_INTEGER,
  342. .minimum = 0,
  343. .maximum = 51,
  344. .step = 1,
  345. .default_value = 1,
  346. },
  347. {
  348. .id = V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP,
  349. .type = V4L2_CTRL_TYPE_INTEGER,
  350. .minimum = 0,
  351. .maximum = 51,
  352. .step = 1,
  353. .default_value = 1,
  354. },
  355. {
  356. .id = V4L2_CID_MPEG_VIDEO_H264_B_FRAME_QP,
  357. .type = V4L2_CTRL_TYPE_INTEGER,
  358. .minimum = 0,
  359. .maximum = 51,
  360. .step = 1,
  361. .default_value = 1,
  362. },
  363. {
  364. .id = V4L2_CID_MPEG_VIDEO_H263_I_FRAME_QP,
  365. .type = V4L2_CTRL_TYPE_INTEGER,
  366. .name = "H263 I-Frame QP value",
  367. .minimum = 1,
  368. .maximum = 31,
  369. .step = 1,
  370. .default_value = 1,
  371. },
  372. {
  373. .id = V4L2_CID_MPEG_VIDEO_H263_MIN_QP,
  374. .type = V4L2_CTRL_TYPE_INTEGER,
  375. .name = "H263 Minimum QP value",
  376. .minimum = 1,
  377. .maximum = 31,
  378. .step = 1,
  379. .default_value = 1,
  380. },
  381. {
  382. .id = V4L2_CID_MPEG_VIDEO_H263_MAX_QP,
  383. .type = V4L2_CTRL_TYPE_INTEGER,
  384. .name = "H263 Maximum QP value",
  385. .minimum = 1,
  386. .maximum = 31,
  387. .step = 1,
  388. .default_value = 1,
  389. },
  390. {
  391. .id = V4L2_CID_MPEG_VIDEO_H263_P_FRAME_QP,
  392. .type = V4L2_CTRL_TYPE_INTEGER,
  393. .name = "H263 P frame QP value",
  394. .minimum = 1,
  395. .maximum = 31,
  396. .step = 1,
  397. .default_value = 1,
  398. },
  399. {
  400. .id = V4L2_CID_MPEG_VIDEO_H263_B_FRAME_QP,
  401. .type = V4L2_CTRL_TYPE_INTEGER,
  402. .name = "H263 B frame QP value",
  403. .minimum = 1,
  404. .maximum = 31,
  405. .step = 1,
  406. .default_value = 1,
  407. },
  408. {
  409. .id = V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP,
  410. .type = V4L2_CTRL_TYPE_INTEGER,
  411. .name = "MPEG4 I-Frame QP value",
  412. .minimum = 1,
  413. .maximum = 31,
  414. .step = 1,
  415. .default_value = 1,
  416. },
  417. {
  418. .id = V4L2_CID_MPEG_VIDEO_MPEG4_MIN_QP,
  419. .type = V4L2_CTRL_TYPE_INTEGER,
  420. .name = "MPEG4 Minimum QP value",
  421. .minimum = 1,
  422. .maximum = 31,
  423. .step = 1,
  424. .default_value = 1,
  425. },
  426. {
  427. .id = V4L2_CID_MPEG_VIDEO_MPEG4_MAX_QP,
  428. .type = V4L2_CTRL_TYPE_INTEGER,
  429. .name = "MPEG4 Maximum QP value",
  430. .minimum = 0,
  431. .maximum = 51,
  432. .step = 1,
  433. .default_value = 1,
  434. },
  435. {
  436. .id = V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP,
  437. .type = V4L2_CTRL_TYPE_INTEGER,
  438. .name = "MPEG4 P frame QP value",
  439. .minimum = 1,
  440. .maximum = 31,
  441. .step = 1,
  442. .default_value = 1,
  443. },
  444. {
  445. .id = V4L2_CID_MPEG_VIDEO_MPEG4_B_FRAME_QP,
  446. .type = V4L2_CTRL_TYPE_INTEGER,
  447. .name = "MPEG4 B frame QP value",
  448. .minimum = 1,
  449. .maximum = 31,
  450. .step = 1,
  451. .default_value = 1,
  452. },
  453. {
  454. .id = V4L2_CID_MPEG_MFC51_VIDEO_H264_ADAPTIVE_RC_DARK,
  455. .type = V4L2_CTRL_TYPE_BOOLEAN,
  456. .name = "H264 Dark Reg Adaptive RC",
  457. .minimum = 0,
  458. .maximum = 1,
  459. .step = 1,
  460. .default_value = 0,
  461. },
  462. {
  463. .id = V4L2_CID_MPEG_MFC51_VIDEO_H264_ADAPTIVE_RC_SMOOTH,
  464. .type = V4L2_CTRL_TYPE_BOOLEAN,
  465. .name = "H264 Smooth Reg Adaptive RC",
  466. .minimum = 0,
  467. .maximum = 1,
  468. .step = 1,
  469. .default_value = 0,
  470. },
  471. {
  472. .id = V4L2_CID_MPEG_MFC51_VIDEO_H264_ADAPTIVE_RC_STATIC,
  473. .type = V4L2_CTRL_TYPE_BOOLEAN,
  474. .name = "H264 Static Reg Adaptive RC",
  475. .minimum = 0,
  476. .maximum = 1,
  477. .step = 1,
  478. .default_value = 0,
  479. },
  480. {
  481. .id = V4L2_CID_MPEG_MFC51_VIDEO_H264_ADAPTIVE_RC_ACTIVITY,
  482. .type = V4L2_CTRL_TYPE_BOOLEAN,
  483. .name = "H264 Activity Reg Adaptive RC",
  484. .minimum = 0,
  485. .maximum = 1,
  486. .step = 1,
  487. .default_value = 0,
  488. },
  489. {
  490. .id = V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_ENABLE,
  491. .type = V4L2_CTRL_TYPE_BOOLEAN,
  492. .minimum = 0,
  493. .maximum = 1,
  494. .step = 1,
  495. .default_value = 0,
  496. },
  497. {
  498. .id = V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_IDC,
  499. .type = V4L2_CTRL_TYPE_MENU,
  500. .minimum = V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_UNSPECIFIED,
  501. .maximum = V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_EXTENDED,
  502. .default_value = V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_UNSPECIFIED,
  503. .menu_skip_mask = 0,
  504. },
  505. {
  506. .id = V4L2_CID_MPEG_VIDEO_H264_VUI_EXT_SAR_WIDTH,
  507. .type = V4L2_CTRL_TYPE_INTEGER,
  508. .minimum = 0,
  509. .maximum = (1 << 16) - 1,
  510. .step = 1,
  511. .default_value = 0,
  512. },
  513. {
  514. .id = V4L2_CID_MPEG_VIDEO_H264_VUI_EXT_SAR_HEIGHT,
  515. .type = V4L2_CTRL_TYPE_INTEGER,
  516. .minimum = 0,
  517. .maximum = (1 << 16) - 1,
  518. .step = 1,
  519. .default_value = 0,
  520. },
  521. {
  522. .id = V4L2_CID_MPEG_VIDEO_GOP_CLOSURE,
  523. .type = V4L2_CTRL_TYPE_BOOLEAN,
  524. .minimum = 0,
  525. .maximum = 1,
  526. .step = 1,
  527. .default_value = 1,
  528. },
  529. {
  530. .id = V4L2_CID_MPEG_VIDEO_H264_I_PERIOD,
  531. .type = V4L2_CTRL_TYPE_INTEGER,
  532. .minimum = 0,
  533. .maximum = (1 << 16) - 1,
  534. .step = 1,
  535. .default_value = 0,
  536. },
  537. {
  538. .id = V4L2_CID_MPEG_VIDEO_MPEG4_PROFILE,
  539. .type = V4L2_CTRL_TYPE_MENU,
  540. .minimum = V4L2_MPEG_VIDEO_MPEG4_PROFILE_SIMPLE,
  541. .maximum = V4L2_MPEG_VIDEO_MPEG4_PROFILE_ADVANCED_SIMPLE,
  542. .default_value = V4L2_MPEG_VIDEO_MPEG4_PROFILE_SIMPLE,
  543. .menu_skip_mask = 0,
  544. },
  545. {
  546. .id = V4L2_CID_MPEG_VIDEO_MPEG4_QPEL,
  547. .type = V4L2_CTRL_TYPE_BOOLEAN,
  548. .minimum = 0,
  549. .maximum = 1,
  550. .step = 1,
  551. .default_value = 0,
  552. },
  553. };
  554. #define NUM_CTRLS ARRAY_SIZE(controls)
  555. static const char * const *mfc51_get_menu(u32 id)
  556. {
  557. static const char * const mfc51_video_frame_skip[] = {
  558. "Disabled",
  559. "Level Limit",
  560. "VBV/CPB Limit",
  561. NULL,
  562. };
  563. static const char * const mfc51_video_force_frame[] = {
  564. "Disabled",
  565. "I Frame",
  566. "Not Coded",
  567. NULL,
  568. };
  569. switch (id) {
  570. case V4L2_CID_MPEG_MFC51_VIDEO_FRAME_SKIP_MODE:
  571. return mfc51_video_frame_skip;
  572. case V4L2_CID_MPEG_MFC51_VIDEO_FORCE_FRAME_TYPE:
  573. return mfc51_video_force_frame;
  574. }
  575. return NULL;
  576. }
  577. static int s5p_mfc_ctx_ready(struct s5p_mfc_ctx *ctx)
  578. {
  579. mfc_debug(2, "src=%d, dst=%d, state=%d\n",
  580. ctx->src_queue_cnt, ctx->dst_queue_cnt, ctx->state);
  581. /* context is ready to make header */
  582. if (ctx->state == MFCINST_GOT_INST && ctx->dst_queue_cnt >= 1)
  583. return 1;
  584. /* context is ready to encode a frame */
  585. if ((ctx->state == MFCINST_RUNNING ||
  586. ctx->state == MFCINST_HEAD_PARSED) &&
  587. ctx->src_queue_cnt >= 1 && ctx->dst_queue_cnt >= 1)
  588. return 1;
  589. /* context is ready to encode remaining frames */
  590. if (ctx->state == MFCINST_FINISHING &&
  591. ctx->dst_queue_cnt >= 1)
  592. return 1;
  593. mfc_debug(2, "ctx is not ready\n");
  594. return 0;
  595. }
  596. static void cleanup_ref_queue(struct s5p_mfc_ctx *ctx)
  597. {
  598. struct s5p_mfc_buf *mb_entry;
  599. unsigned long mb_y_addr, mb_c_addr;
  600. /* move buffers in ref queue to src queue */
  601. while (!list_empty(&ctx->ref_queue)) {
  602. mb_entry = list_entry((&ctx->ref_queue)->next,
  603. struct s5p_mfc_buf, list);
  604. mb_y_addr = vb2_dma_contig_plane_dma_addr(mb_entry->b, 0);
  605. mb_c_addr = vb2_dma_contig_plane_dma_addr(mb_entry->b, 1);
  606. list_del(&mb_entry->list);
  607. ctx->ref_queue_cnt--;
  608. list_add_tail(&mb_entry->list, &ctx->src_queue);
  609. ctx->src_queue_cnt++;
  610. }
  611. mfc_debug(2, "enc src count: %d, enc ref count: %d\n",
  612. ctx->src_queue_cnt, ctx->ref_queue_cnt);
  613. INIT_LIST_HEAD(&ctx->ref_queue);
  614. ctx->ref_queue_cnt = 0;
  615. }
  616. static int enc_pre_seq_start(struct s5p_mfc_ctx *ctx)
  617. {
  618. struct s5p_mfc_dev *dev = ctx->dev;
  619. struct s5p_mfc_buf *dst_mb;
  620. unsigned long dst_addr;
  621. unsigned int dst_size;
  622. unsigned long flags;
  623. spin_lock_irqsave(&dev->irqlock, flags);
  624. dst_mb = list_entry(ctx->dst_queue.next, struct s5p_mfc_buf, list);
  625. dst_addr = vb2_dma_contig_plane_dma_addr(dst_mb->b, 0);
  626. dst_size = vb2_plane_size(dst_mb->b, 0);
  627. s5p_mfc_hw_call(dev->mfc_ops, set_enc_stream_buffer, ctx, dst_addr,
  628. dst_size);
  629. spin_unlock_irqrestore(&dev->irqlock, flags);
  630. return 0;
  631. }
  632. static int enc_post_seq_start(struct s5p_mfc_ctx *ctx)
  633. {
  634. struct s5p_mfc_dev *dev = ctx->dev;
  635. struct s5p_mfc_enc_params *p = &ctx->enc_params;
  636. struct s5p_mfc_buf *dst_mb;
  637. unsigned long flags;
  638. if (p->seq_hdr_mode == V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE) {
  639. spin_lock_irqsave(&dev->irqlock, flags);
  640. dst_mb = list_entry(ctx->dst_queue.next,
  641. struct s5p_mfc_buf, list);
  642. list_del(&dst_mb->list);
  643. ctx->dst_queue_cnt--;
  644. vb2_set_plane_payload(dst_mb->b, 0,
  645. s5p_mfc_hw_call(dev->mfc_ops, get_enc_strm_size, dev));
  646. vb2_buffer_done(dst_mb->b, VB2_BUF_STATE_DONE);
  647. spin_unlock_irqrestore(&dev->irqlock, flags);
  648. }
  649. if (IS_MFCV6(dev)) {
  650. ctx->state = MFCINST_HEAD_PARSED; /* for INIT_BUFFER cmd */
  651. } else {
  652. ctx->state = MFCINST_RUNNING;
  653. if (s5p_mfc_ctx_ready(ctx))
  654. set_work_bit_irqsave(ctx);
  655. s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
  656. }
  657. if (IS_MFCV6(dev))
  658. ctx->dpb_count = s5p_mfc_hw_call(dev->mfc_ops,
  659. get_enc_dpb_count, dev);
  660. return 0;
  661. }
  662. static int enc_pre_frame_start(struct s5p_mfc_ctx *ctx)
  663. {
  664. struct s5p_mfc_dev *dev = ctx->dev;
  665. struct s5p_mfc_buf *dst_mb;
  666. struct s5p_mfc_buf *src_mb;
  667. unsigned long flags;
  668. unsigned long src_y_addr, src_c_addr, dst_addr;
  669. unsigned int dst_size;
  670. spin_lock_irqsave(&dev->irqlock, flags);
  671. src_mb = list_entry(ctx->src_queue.next, struct s5p_mfc_buf, list);
  672. src_y_addr = vb2_dma_contig_plane_dma_addr(src_mb->b, 0);
  673. src_c_addr = vb2_dma_contig_plane_dma_addr(src_mb->b, 1);
  674. s5p_mfc_hw_call(dev->mfc_ops, set_enc_frame_buffer, ctx, src_y_addr,
  675. src_c_addr);
  676. spin_unlock_irqrestore(&dev->irqlock, flags);
  677. spin_lock_irqsave(&dev->irqlock, flags);
  678. dst_mb = list_entry(ctx->dst_queue.next, struct s5p_mfc_buf, list);
  679. dst_addr = vb2_dma_contig_plane_dma_addr(dst_mb->b, 0);
  680. dst_size = vb2_plane_size(dst_mb->b, 0);
  681. s5p_mfc_hw_call(dev->mfc_ops, set_enc_stream_buffer, ctx, dst_addr,
  682. dst_size);
  683. spin_unlock_irqrestore(&dev->irqlock, flags);
  684. return 0;
  685. }
  686. static int enc_post_frame_start(struct s5p_mfc_ctx *ctx)
  687. {
  688. struct s5p_mfc_dev *dev = ctx->dev;
  689. struct s5p_mfc_buf *mb_entry;
  690. unsigned long enc_y_addr, enc_c_addr;
  691. unsigned long mb_y_addr, mb_c_addr;
  692. int slice_type;
  693. unsigned int strm_size;
  694. unsigned long flags;
  695. slice_type = s5p_mfc_hw_call(dev->mfc_ops, get_enc_slice_type, dev);
  696. strm_size = s5p_mfc_hw_call(dev->mfc_ops, get_enc_strm_size, dev);
  697. mfc_debug(2, "Encoded slice type: %d", slice_type);
  698. mfc_debug(2, "Encoded stream size: %d", strm_size);
  699. mfc_debug(2, "Display order: %d",
  700. mfc_read(dev, S5P_FIMV_ENC_SI_PIC_CNT));
  701. spin_lock_irqsave(&dev->irqlock, flags);
  702. if (slice_type >= 0) {
  703. s5p_mfc_hw_call(dev->mfc_ops, get_enc_frame_buffer, ctx,
  704. &enc_y_addr, &enc_c_addr);
  705. list_for_each_entry(mb_entry, &ctx->src_queue, list) {
  706. mb_y_addr = vb2_dma_contig_plane_dma_addr(mb_entry->b, 0);
  707. mb_c_addr = vb2_dma_contig_plane_dma_addr(mb_entry->b, 1);
  708. if ((enc_y_addr == mb_y_addr) &&
  709. (enc_c_addr == mb_c_addr)) {
  710. list_del(&mb_entry->list);
  711. ctx->src_queue_cnt--;
  712. vb2_buffer_done(mb_entry->b,
  713. VB2_BUF_STATE_DONE);
  714. break;
  715. }
  716. }
  717. list_for_each_entry(mb_entry, &ctx->ref_queue, list) {
  718. mb_y_addr = vb2_dma_contig_plane_dma_addr(mb_entry->b, 0);
  719. mb_c_addr = vb2_dma_contig_plane_dma_addr(mb_entry->b, 1);
  720. if ((enc_y_addr == mb_y_addr) &&
  721. (enc_c_addr == mb_c_addr)) {
  722. list_del(&mb_entry->list);
  723. ctx->ref_queue_cnt--;
  724. vb2_buffer_done(mb_entry->b,
  725. VB2_BUF_STATE_DONE);
  726. break;
  727. }
  728. }
  729. }
  730. if ((ctx->src_queue_cnt > 0) && (ctx->state == MFCINST_RUNNING)) {
  731. mb_entry = list_entry(ctx->src_queue.next, struct s5p_mfc_buf,
  732. list);
  733. if (mb_entry->flags & MFC_BUF_FLAG_USED) {
  734. list_del(&mb_entry->list);
  735. ctx->src_queue_cnt--;
  736. list_add_tail(&mb_entry->list, &ctx->ref_queue);
  737. ctx->ref_queue_cnt++;
  738. }
  739. mfc_debug(2, "enc src count: %d, enc ref count: %d\n",
  740. ctx->src_queue_cnt, ctx->ref_queue_cnt);
  741. }
  742. if (strm_size > 0) {
  743. /* at least one more dest. buffers exist always */
  744. mb_entry = list_entry(ctx->dst_queue.next, struct s5p_mfc_buf,
  745. list);
  746. list_del(&mb_entry->list);
  747. ctx->dst_queue_cnt--;
  748. switch (slice_type) {
  749. case S5P_FIMV_ENC_SI_SLICE_TYPE_I:
  750. mb_entry->b->v4l2_buf.flags |= V4L2_BUF_FLAG_KEYFRAME;
  751. break;
  752. case S5P_FIMV_ENC_SI_SLICE_TYPE_P:
  753. mb_entry->b->v4l2_buf.flags |= V4L2_BUF_FLAG_PFRAME;
  754. break;
  755. case S5P_FIMV_ENC_SI_SLICE_TYPE_B:
  756. mb_entry->b->v4l2_buf.flags |= V4L2_BUF_FLAG_BFRAME;
  757. break;
  758. }
  759. vb2_set_plane_payload(mb_entry->b, 0, strm_size);
  760. vb2_buffer_done(mb_entry->b, VB2_BUF_STATE_DONE);
  761. }
  762. spin_unlock_irqrestore(&dev->irqlock, flags);
  763. if ((ctx->src_queue_cnt == 0) || (ctx->dst_queue_cnt == 0))
  764. clear_work_bit(ctx);
  765. return 0;
  766. }
  767. static struct s5p_mfc_codec_ops encoder_codec_ops = {
  768. .pre_seq_start = enc_pre_seq_start,
  769. .post_seq_start = enc_post_seq_start,
  770. .pre_frame_start = enc_pre_frame_start,
  771. .post_frame_start = enc_post_frame_start,
  772. };
  773. /* Query capabilities of the device */
  774. static int vidioc_querycap(struct file *file, void *priv,
  775. struct v4l2_capability *cap)
  776. {
  777. struct s5p_mfc_dev *dev = video_drvdata(file);
  778. strncpy(cap->driver, dev->plat_dev->name, sizeof(cap->driver) - 1);
  779. strncpy(cap->card, dev->plat_dev->name, sizeof(cap->card) - 1);
  780. cap->bus_info[0] = 0;
  781. cap->version = KERNEL_VERSION(1, 0, 0);
  782. /*
  783. * This is only a mem-to-mem video device. The capture and output
  784. * device capability flags are left only for backward compatibility
  785. * and are scheduled for removal.
  786. */
  787. cap->capabilities = V4L2_CAP_VIDEO_M2M_MPLANE | V4L2_CAP_STREAMING |
  788. V4L2_CAP_VIDEO_CAPTURE_MPLANE |
  789. V4L2_CAP_VIDEO_OUTPUT_MPLANE;
  790. return 0;
  791. }
  792. static int vidioc_enum_fmt(struct v4l2_fmtdesc *f, bool mplane, bool out)
  793. {
  794. struct s5p_mfc_fmt *fmt;
  795. int i, j = 0;
  796. for (i = 0; i < ARRAY_SIZE(formats); ++i) {
  797. if (mplane && formats[i].num_planes == 1)
  798. continue;
  799. else if (!mplane && formats[i].num_planes > 1)
  800. continue;
  801. if (out && formats[i].type != MFC_FMT_RAW)
  802. continue;
  803. else if (!out && formats[i].type != MFC_FMT_ENC)
  804. continue;
  805. if (j == f->index) {
  806. fmt = &formats[i];
  807. strlcpy(f->description, fmt->name,
  808. sizeof(f->description));
  809. f->pixelformat = fmt->fourcc;
  810. return 0;
  811. }
  812. ++j;
  813. }
  814. return -EINVAL;
  815. }
  816. static int vidioc_enum_fmt_vid_cap(struct file *file, void *pirv,
  817. struct v4l2_fmtdesc *f)
  818. {
  819. return vidioc_enum_fmt(f, false, false);
  820. }
  821. static int vidioc_enum_fmt_vid_cap_mplane(struct file *file, void *pirv,
  822. struct v4l2_fmtdesc *f)
  823. {
  824. return vidioc_enum_fmt(f, true, false);
  825. }
  826. static int vidioc_enum_fmt_vid_out(struct file *file, void *prov,
  827. struct v4l2_fmtdesc *f)
  828. {
  829. return vidioc_enum_fmt(f, false, true);
  830. }
  831. static int vidioc_enum_fmt_vid_out_mplane(struct file *file, void *prov,
  832. struct v4l2_fmtdesc *f)
  833. {
  834. return vidioc_enum_fmt(f, true, true);
  835. }
  836. static int vidioc_g_fmt(struct file *file, void *priv, struct v4l2_format *f)
  837. {
  838. struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
  839. struct v4l2_pix_format_mplane *pix_fmt_mp = &f->fmt.pix_mp;
  840. mfc_debug(2, "f->type = %d ctx->state = %d\n", f->type, ctx->state);
  841. if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
  842. /* This is run on output (encoder dest) */
  843. pix_fmt_mp->width = 0;
  844. pix_fmt_mp->height = 0;
  845. pix_fmt_mp->field = V4L2_FIELD_NONE;
  846. pix_fmt_mp->pixelformat = ctx->dst_fmt->fourcc;
  847. pix_fmt_mp->num_planes = ctx->dst_fmt->num_planes;
  848. pix_fmt_mp->plane_fmt[0].bytesperline = ctx->enc_dst_buf_size;
  849. pix_fmt_mp->plane_fmt[0].sizeimage = ctx->enc_dst_buf_size;
  850. } else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
  851. /* This is run on capture (encoder src) */
  852. pix_fmt_mp->width = ctx->img_width;
  853. pix_fmt_mp->height = ctx->img_height;
  854. pix_fmt_mp->field = V4L2_FIELD_NONE;
  855. pix_fmt_mp->pixelformat = ctx->src_fmt->fourcc;
  856. pix_fmt_mp->num_planes = ctx->src_fmt->num_planes;
  857. pix_fmt_mp->plane_fmt[0].bytesperline = ctx->buf_width;
  858. pix_fmt_mp->plane_fmt[0].sizeimage = ctx->luma_size;
  859. pix_fmt_mp->plane_fmt[1].bytesperline = ctx->buf_width;
  860. pix_fmt_mp->plane_fmt[1].sizeimage = ctx->chroma_size;
  861. } else {
  862. mfc_err("invalid buf type\n");
  863. return -EINVAL;
  864. }
  865. return 0;
  866. }
  867. static int vidioc_try_fmt(struct file *file, void *priv, struct v4l2_format *f)
  868. {
  869. struct s5p_mfc_fmt *fmt;
  870. struct v4l2_pix_format_mplane *pix_fmt_mp = &f->fmt.pix_mp;
  871. if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
  872. fmt = find_format(f, MFC_FMT_ENC);
  873. if (!fmt) {
  874. mfc_err("failed to try output format\n");
  875. return -EINVAL;
  876. }
  877. if (pix_fmt_mp->plane_fmt[0].sizeimage == 0) {
  878. mfc_err("must be set encoding output size\n");
  879. return -EINVAL;
  880. }
  881. pix_fmt_mp->plane_fmt[0].bytesperline =
  882. pix_fmt_mp->plane_fmt[0].sizeimage;
  883. } else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
  884. fmt = find_format(f, MFC_FMT_RAW);
  885. if (!fmt) {
  886. mfc_err("failed to try output format\n");
  887. return -EINVAL;
  888. }
  889. if (fmt->num_planes != pix_fmt_mp->num_planes) {
  890. mfc_err("failed to try output format\n");
  891. return -EINVAL;
  892. }
  893. v4l_bound_align_image(&pix_fmt_mp->width, 8, 1920, 1,
  894. &pix_fmt_mp->height, 4, 1080, 1, 0);
  895. } else {
  896. mfc_err("invalid buf type\n");
  897. return -EINVAL;
  898. }
  899. return 0;
  900. }
  901. static int vidioc_s_fmt(struct file *file, void *priv, struct v4l2_format *f)
  902. {
  903. struct s5p_mfc_dev *dev = video_drvdata(file);
  904. struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
  905. struct s5p_mfc_fmt *fmt;
  906. struct v4l2_pix_format_mplane *pix_fmt_mp = &f->fmt.pix_mp;
  907. int ret = 0;
  908. ret = vidioc_try_fmt(file, priv, f);
  909. if (ret)
  910. return ret;
  911. if (ctx->vq_src.streaming || ctx->vq_dst.streaming) {
  912. v4l2_err(&dev->v4l2_dev, "%s queue busy\n", __func__);
  913. ret = -EBUSY;
  914. goto out;
  915. }
  916. if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
  917. fmt = find_format(f, MFC_FMT_ENC);
  918. if (!fmt) {
  919. mfc_err("failed to set capture format\n");
  920. return -EINVAL;
  921. }
  922. ctx->state = MFCINST_INIT;
  923. ctx->dst_fmt = fmt;
  924. ctx->codec_mode = ctx->dst_fmt->codec_mode;
  925. ctx->enc_dst_buf_size = pix_fmt_mp->plane_fmt[0].sizeimage;
  926. pix_fmt_mp->plane_fmt[0].bytesperline = 0;
  927. ctx->dst_bufs_cnt = 0;
  928. ctx->capture_state = QUEUE_FREE;
  929. s5p_mfc_hw_call(dev->mfc_ops, alloc_instance_buffer, ctx);
  930. set_work_bit_irqsave(ctx);
  931. s5p_mfc_clean_ctx_int_flags(ctx);
  932. s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
  933. if (s5p_mfc_wait_for_done_ctx(ctx, \
  934. S5P_MFC_R2H_CMD_OPEN_INSTANCE_RET, 1)) {
  935. /* Error or timeout */
  936. mfc_err("Error getting instance from hardware\n");
  937. s5p_mfc_hw_call(dev->mfc_ops, release_instance_buffer,
  938. ctx);
  939. ret = -EIO;
  940. goto out;
  941. }
  942. mfc_debug(2, "Got instance number: %d\n", ctx->inst_no);
  943. } else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
  944. fmt = find_format(f, MFC_FMT_RAW);
  945. if (!fmt) {
  946. mfc_err("failed to set output format\n");
  947. return -EINVAL;
  948. }
  949. if (!IS_MFCV6(dev) &&
  950. (fmt->fourcc == V4L2_PIX_FMT_NV12MT_16X16)) {
  951. mfc_err("Not supported format.\n");
  952. return -EINVAL;
  953. } else if (IS_MFCV6(dev) &&
  954. (fmt->fourcc == V4L2_PIX_FMT_NV12MT)) {
  955. mfc_err("Not supported format.\n");
  956. return -EINVAL;
  957. }
  958. if (fmt->num_planes != pix_fmt_mp->num_planes) {
  959. mfc_err("failed to set output format\n");
  960. ret = -EINVAL;
  961. goto out;
  962. }
  963. ctx->src_fmt = fmt;
  964. ctx->img_width = pix_fmt_mp->width;
  965. ctx->img_height = pix_fmt_mp->height;
  966. mfc_debug(2, "codec number: %d\n", ctx->src_fmt->codec_mode);
  967. mfc_debug(2, "fmt - w: %d, h: %d, ctx - w: %d, h: %d\n",
  968. pix_fmt_mp->width, pix_fmt_mp->height,
  969. ctx->img_width, ctx->img_height);
  970. s5p_mfc_hw_call(dev->mfc_ops, enc_calc_src_size, ctx);
  971. pix_fmt_mp->plane_fmt[0].sizeimage = ctx->luma_size;
  972. pix_fmt_mp->plane_fmt[0].bytesperline = ctx->buf_width;
  973. pix_fmt_mp->plane_fmt[1].sizeimage = ctx->chroma_size;
  974. pix_fmt_mp->plane_fmt[1].bytesperline = ctx->buf_width;
  975. ctx->src_bufs_cnt = 0;
  976. ctx->output_state = QUEUE_FREE;
  977. } else {
  978. mfc_err("invalid buf type\n");
  979. return -EINVAL;
  980. }
  981. out:
  982. mfc_debug_leave();
  983. return ret;
  984. }
  985. static int vidioc_reqbufs(struct file *file, void *priv,
  986. struct v4l2_requestbuffers *reqbufs)
  987. {
  988. struct s5p_mfc_dev *dev = video_drvdata(file);
  989. struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
  990. int ret = 0;
  991. /* if memory is not mmp or userptr return error */
  992. if ((reqbufs->memory != V4L2_MEMORY_MMAP) &&
  993. (reqbufs->memory != V4L2_MEMORY_USERPTR))
  994. return -EINVAL;
  995. if (reqbufs->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
  996. if (ctx->capture_state != QUEUE_FREE) {
  997. mfc_err("invalid capture state: %d\n",
  998. ctx->capture_state);
  999. return -EINVAL;
  1000. }
  1001. ret = vb2_reqbufs(&ctx->vq_dst, reqbufs);
  1002. if (ret != 0) {
  1003. mfc_err("error in vb2_reqbufs() for E(D)\n");
  1004. return ret;
  1005. }
  1006. ctx->capture_state = QUEUE_BUFS_REQUESTED;
  1007. if (!IS_MFCV6(dev)) {
  1008. ret = s5p_mfc_hw_call(ctx->dev->mfc_ops,
  1009. alloc_codec_buffers, ctx);
  1010. if (ret) {
  1011. mfc_err("Failed to allocate encoding buffers\n");
  1012. reqbufs->count = 0;
  1013. ret = vb2_reqbufs(&ctx->vq_dst, reqbufs);
  1014. return -ENOMEM;
  1015. }
  1016. }
  1017. } else if (reqbufs->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
  1018. if (ctx->output_state != QUEUE_FREE) {
  1019. mfc_err("invalid output state: %d\n",
  1020. ctx->output_state);
  1021. return -EINVAL;
  1022. }
  1023. ret = vb2_reqbufs(&ctx->vq_src, reqbufs);
  1024. if (ret != 0) {
  1025. mfc_err("error in vb2_reqbufs() for E(S)\n");
  1026. return ret;
  1027. }
  1028. ctx->output_state = QUEUE_BUFS_REQUESTED;
  1029. } else {
  1030. mfc_err("invalid buf type\n");
  1031. return -EINVAL;
  1032. }
  1033. return ret;
  1034. }
  1035. static int vidioc_querybuf(struct file *file, void *priv,
  1036. struct v4l2_buffer *buf)
  1037. {
  1038. struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
  1039. int ret = 0;
  1040. /* if memory is not mmp or userptr return error */
  1041. if ((buf->memory != V4L2_MEMORY_MMAP) &&
  1042. (buf->memory != V4L2_MEMORY_USERPTR))
  1043. return -EINVAL;
  1044. if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
  1045. if (ctx->state != MFCINST_GOT_INST) {
  1046. mfc_err("invalid context state: %d\n", ctx->state);
  1047. return -EINVAL;
  1048. }
  1049. ret = vb2_querybuf(&ctx->vq_dst, buf);
  1050. if (ret != 0) {
  1051. mfc_err("error in vb2_querybuf() for E(D)\n");
  1052. return ret;
  1053. }
  1054. buf->m.planes[0].m.mem_offset += DST_QUEUE_OFF_BASE;
  1055. } else if (buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
  1056. ret = vb2_querybuf(&ctx->vq_src, buf);
  1057. if (ret != 0) {
  1058. mfc_err("error in vb2_querybuf() for E(S)\n");
  1059. return ret;
  1060. }
  1061. } else {
  1062. mfc_err("invalid buf type\n");
  1063. return -EINVAL;
  1064. }
  1065. return ret;
  1066. }
  1067. /* Queue a buffer */
  1068. static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
  1069. {
  1070. struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
  1071. if (ctx->state == MFCINST_ERROR) {
  1072. mfc_err("Call on QBUF after unrecoverable error\n");
  1073. return -EIO;
  1074. }
  1075. if (buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
  1076. if (ctx->state == MFCINST_FINISHING) {
  1077. mfc_err("Call on QBUF after EOS command\n");
  1078. return -EIO;
  1079. }
  1080. return vb2_qbuf(&ctx->vq_src, buf);
  1081. } else if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
  1082. return vb2_qbuf(&ctx->vq_dst, buf);
  1083. }
  1084. return -EINVAL;
  1085. }
  1086. /* Dequeue a buffer */
  1087. static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
  1088. {
  1089. const struct v4l2_event ev = {
  1090. .type = V4L2_EVENT_EOS
  1091. };
  1092. struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
  1093. int ret;
  1094. if (ctx->state == MFCINST_ERROR) {
  1095. mfc_err("Call on DQBUF after unrecoverable error\n");
  1096. return -EIO;
  1097. }
  1098. if (buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
  1099. ret = vb2_dqbuf(&ctx->vq_src, buf, file->f_flags & O_NONBLOCK);
  1100. } else if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
  1101. ret = vb2_dqbuf(&ctx->vq_dst, buf, file->f_flags & O_NONBLOCK);
  1102. if (ret == 0 && ctx->state == MFCINST_FINISHED
  1103. && list_empty(&ctx->vq_dst.done_list))
  1104. v4l2_event_queue_fh(&ctx->fh, &ev);
  1105. } else {
  1106. ret = -EINVAL;
  1107. }
  1108. return ret;
  1109. }
  1110. /* Export DMA buffer */
  1111. static int vidioc_expbuf(struct file *file, void *priv,
  1112. struct v4l2_exportbuffer *eb)
  1113. {
  1114. struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
  1115. if (eb->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
  1116. return vb2_expbuf(&ctx->vq_src, eb);
  1117. if (eb->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
  1118. return vb2_expbuf(&ctx->vq_dst, eb);
  1119. return -EINVAL;
  1120. }
  1121. /* Stream on */
  1122. static int vidioc_streamon(struct file *file, void *priv,
  1123. enum v4l2_buf_type type)
  1124. {
  1125. struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
  1126. if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
  1127. return vb2_streamon(&ctx->vq_src, type);
  1128. else if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
  1129. return vb2_streamon(&ctx->vq_dst, type);
  1130. return -EINVAL;
  1131. }
  1132. /* Stream off, which equals to a pause */
  1133. static int vidioc_streamoff(struct file *file, void *priv,
  1134. enum v4l2_buf_type type)
  1135. {
  1136. struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
  1137. if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
  1138. return vb2_streamoff(&ctx->vq_src, type);
  1139. else if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
  1140. return vb2_streamoff(&ctx->vq_dst, type);
  1141. return -EINVAL;
  1142. }
  1143. static inline int h264_level(enum v4l2_mpeg_video_h264_level lvl)
  1144. {
  1145. static unsigned int t[V4L2_MPEG_VIDEO_H264_LEVEL_4_0 + 1] = {
  1146. /* V4L2_MPEG_VIDEO_H264_LEVEL_1_0 */ 10,
  1147. /* V4L2_MPEG_VIDEO_H264_LEVEL_1B */ 9,
  1148. /* V4L2_MPEG_VIDEO_H264_LEVEL_1_1 */ 11,
  1149. /* V4L2_MPEG_VIDEO_H264_LEVEL_1_2 */ 12,
  1150. /* V4L2_MPEG_VIDEO_H264_LEVEL_1_3 */ 13,
  1151. /* V4L2_MPEG_VIDEO_H264_LEVEL_2_0 */ 20,
  1152. /* V4L2_MPEG_VIDEO_H264_LEVEL_2_1 */ 21,
  1153. /* V4L2_MPEG_VIDEO_H264_LEVEL_2_2 */ 22,
  1154. /* V4L2_MPEG_VIDEO_H264_LEVEL_3_0 */ 30,
  1155. /* V4L2_MPEG_VIDEO_H264_LEVEL_3_1 */ 31,
  1156. /* V4L2_MPEG_VIDEO_H264_LEVEL_3_2 */ 32,
  1157. /* V4L2_MPEG_VIDEO_H264_LEVEL_4_0 */ 40,
  1158. };
  1159. return t[lvl];
  1160. }
  1161. static inline int mpeg4_level(enum v4l2_mpeg_video_mpeg4_level lvl)
  1162. {
  1163. static unsigned int t[V4L2_MPEG_VIDEO_MPEG4_LEVEL_5 + 1] = {
  1164. /* V4L2_MPEG_VIDEO_MPEG4_LEVEL_0 */ 0,
  1165. /* V4L2_MPEG_VIDEO_MPEG4_LEVEL_0B */ 9,
  1166. /* V4L2_MPEG_VIDEO_MPEG4_LEVEL_1 */ 1,
  1167. /* V4L2_MPEG_VIDEO_MPEG4_LEVEL_2 */ 2,
  1168. /* V4L2_MPEG_VIDEO_MPEG4_LEVEL_3 */ 3,
  1169. /* V4L2_MPEG_VIDEO_MPEG4_LEVEL_3B */ 7,
  1170. /* V4L2_MPEG_VIDEO_MPEG4_LEVEL_4 */ 4,
  1171. /* V4L2_MPEG_VIDEO_MPEG4_LEVEL_5 */ 5,
  1172. };
  1173. return t[lvl];
  1174. }
  1175. static inline int vui_sar_idc(enum v4l2_mpeg_video_h264_vui_sar_idc sar)
  1176. {
  1177. static unsigned int t[V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_EXTENDED + 1] = {
  1178. /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_UNSPECIFIED */ 0,
  1179. /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_1x1 */ 1,
  1180. /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_12x11 */ 2,
  1181. /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_10x11 */ 3,
  1182. /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_16x11 */ 4,
  1183. /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_40x33 */ 5,
  1184. /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_24x11 */ 6,
  1185. /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_20x11 */ 7,
  1186. /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_32x11 */ 8,
  1187. /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_80x33 */ 9,
  1188. /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_18x11 */ 10,
  1189. /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_15x11 */ 11,
  1190. /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_64x33 */ 12,
  1191. /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_160x99 */ 13,
  1192. /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_4x3 */ 14,
  1193. /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_3x2 */ 15,
  1194. /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_2x1 */ 16,
  1195. /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_EXTENDED */ 255,
  1196. };
  1197. return t[sar];
  1198. }
  1199. static int s5p_mfc_enc_s_ctrl(struct v4l2_ctrl *ctrl)
  1200. {
  1201. struct s5p_mfc_ctx *ctx = ctrl_to_ctx(ctrl);
  1202. struct s5p_mfc_dev *dev = ctx->dev;
  1203. struct s5p_mfc_enc_params *p = &ctx->enc_params;
  1204. int ret = 0;
  1205. switch (ctrl->id) {
  1206. case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
  1207. p->gop_size = ctrl->val;
  1208. break;
  1209. case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE:
  1210. p->slice_mode = ctrl->val;
  1211. break;
  1212. case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB:
  1213. p->slice_mb = ctrl->val;
  1214. break;
  1215. case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES:
  1216. p->slice_bit = ctrl->val * 8;
  1217. break;
  1218. case V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB:
  1219. p->intra_refresh_mb = ctrl->val;
  1220. break;
  1221. case V4L2_CID_MPEG_MFC51_VIDEO_PADDING:
  1222. p->pad = ctrl->val;
  1223. break;
  1224. case V4L2_CID_MPEG_MFC51_VIDEO_PADDING_YUV:
  1225. p->pad_luma = (ctrl->val >> 16) & 0xff;
  1226. p->pad_cb = (ctrl->val >> 8) & 0xff;
  1227. p->pad_cr = (ctrl->val >> 0) & 0xff;
  1228. break;
  1229. case V4L2_CID_MPEG_VIDEO_FRAME_RC_ENABLE:
  1230. p->rc_frame = ctrl->val;
  1231. break;
  1232. case V4L2_CID_MPEG_VIDEO_BITRATE:
  1233. p->rc_bitrate = ctrl->val;
  1234. break;
  1235. case V4L2_CID_MPEG_MFC51_VIDEO_RC_REACTION_COEFF:
  1236. p->rc_reaction_coeff = ctrl->val;
  1237. break;
  1238. case V4L2_CID_MPEG_MFC51_VIDEO_FORCE_FRAME_TYPE:
  1239. ctx->force_frame_type = ctrl->val;
  1240. break;
  1241. case V4L2_CID_MPEG_VIDEO_VBV_SIZE:
  1242. p->vbv_size = ctrl->val;
  1243. break;
  1244. case V4L2_CID_MPEG_VIDEO_H264_CPB_SIZE:
  1245. p->codec.h264.cpb_size = ctrl->val;
  1246. break;
  1247. case V4L2_CID_MPEG_VIDEO_HEADER_MODE:
  1248. p->seq_hdr_mode = ctrl->val;
  1249. break;
  1250. case V4L2_CID_MPEG_MFC51_VIDEO_FRAME_SKIP_MODE:
  1251. p->frame_skip_mode = ctrl->val;
  1252. break;
  1253. case V4L2_CID_MPEG_MFC51_VIDEO_RC_FIXED_TARGET_BIT:
  1254. p->fixed_target_bit = ctrl->val;
  1255. break;
  1256. case V4L2_CID_MPEG_VIDEO_B_FRAMES:
  1257. p->num_b_frame = ctrl->val;
  1258. break;
  1259. case V4L2_CID_MPEG_VIDEO_H264_PROFILE:
  1260. switch (ctrl->val) {
  1261. case V4L2_MPEG_VIDEO_H264_PROFILE_MAIN:
  1262. p->codec.h264.profile =
  1263. S5P_FIMV_ENC_PROFILE_H264_MAIN;
  1264. break;
  1265. case V4L2_MPEG_VIDEO_H264_PROFILE_HIGH:
  1266. p->codec.h264.profile =
  1267. S5P_FIMV_ENC_PROFILE_H264_HIGH;
  1268. break;
  1269. case V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE:
  1270. p->codec.h264.profile =
  1271. S5P_FIMV_ENC_PROFILE_H264_BASELINE;
  1272. break;
  1273. case V4L2_MPEG_VIDEO_H264_PROFILE_CONSTRAINED_BASELINE:
  1274. if (IS_MFCV6(dev))
  1275. p->codec.h264.profile =
  1276. S5P_FIMV_ENC_PROFILE_H264_CONSTRAINED_BASELINE;
  1277. else
  1278. ret = -EINVAL;
  1279. break;
  1280. default:
  1281. ret = -EINVAL;
  1282. }
  1283. break;
  1284. case V4L2_CID_MPEG_VIDEO_H264_LEVEL:
  1285. p->codec.h264.level_v4l2 = ctrl->val;
  1286. p->codec.h264.level = h264_level(ctrl->val);
  1287. if (p->codec.h264.level < 0) {
  1288. mfc_err("Level number is wrong\n");
  1289. ret = p->codec.h264.level;
  1290. }
  1291. break;
  1292. case V4L2_CID_MPEG_VIDEO_MPEG4_LEVEL:
  1293. p->codec.mpeg4.level_v4l2 = ctrl->val;
  1294. p->codec.mpeg4.level = mpeg4_level(ctrl->val);
  1295. if (p->codec.mpeg4.level < 0) {
  1296. mfc_err("Level number is wrong\n");
  1297. ret = p->codec.mpeg4.level;
  1298. }
  1299. break;
  1300. case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE:
  1301. p->codec.h264.loop_filter_mode = ctrl->val;
  1302. break;
  1303. case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA:
  1304. p->codec.h264.loop_filter_alpha = ctrl->val;
  1305. break;
  1306. case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA:
  1307. p->codec.h264.loop_filter_beta = ctrl->val;
  1308. break;
  1309. case V4L2_CID_MPEG_VIDEO_H264_ENTROPY_MODE:
  1310. p->codec.h264.entropy_mode = ctrl->val;
  1311. break;
  1312. case V4L2_CID_MPEG_MFC51_VIDEO_H264_NUM_REF_PIC_FOR_P:
  1313. p->codec.h264.num_ref_pic_4p = ctrl->val;
  1314. break;
  1315. case V4L2_CID_MPEG_VIDEO_H264_8X8_TRANSFORM:
  1316. p->codec.h264._8x8_transform = ctrl->val;
  1317. break;
  1318. case V4L2_CID_MPEG_VIDEO_MB_RC_ENABLE:
  1319. p->rc_mb = ctrl->val;
  1320. break;
  1321. case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP:
  1322. p->codec.h264.rc_frame_qp = ctrl->val;
  1323. break;
  1324. case V4L2_CID_MPEG_VIDEO_H264_MIN_QP:
  1325. p->codec.h264.rc_min_qp = ctrl->val;
  1326. break;
  1327. case V4L2_CID_MPEG_VIDEO_H264_MAX_QP:
  1328. p->codec.h264.rc_max_qp = ctrl->val;
  1329. break;
  1330. case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP:
  1331. p->codec.h264.rc_p_frame_qp = ctrl->val;
  1332. break;
  1333. case V4L2_CID_MPEG_VIDEO_H264_B_FRAME_QP:
  1334. p->codec.h264.rc_b_frame_qp = ctrl->val;
  1335. break;
  1336. case V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP:
  1337. case V4L2_CID_MPEG_VIDEO_H263_I_FRAME_QP:
  1338. p->codec.mpeg4.rc_frame_qp = ctrl->val;
  1339. break;
  1340. case V4L2_CID_MPEG_VIDEO_MPEG4_MIN_QP:
  1341. case V4L2_CID_MPEG_VIDEO_H263_MIN_QP:
  1342. p->codec.mpeg4.rc_min_qp = ctrl->val;
  1343. break;
  1344. case V4L2_CID_MPEG_VIDEO_MPEG4_MAX_QP:
  1345. case V4L2_CID_MPEG_VIDEO_H263_MAX_QP:
  1346. p->codec.mpeg4.rc_max_qp = ctrl->val;
  1347. break;
  1348. case V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP:
  1349. case V4L2_CID_MPEG_VIDEO_H263_P_FRAME_QP:
  1350. p->codec.mpeg4.rc_p_frame_qp = ctrl->val;
  1351. break;
  1352. case V4L2_CID_MPEG_VIDEO_MPEG4_B_FRAME_QP:
  1353. case V4L2_CID_MPEG_VIDEO_H263_B_FRAME_QP:
  1354. p->codec.mpeg4.rc_b_frame_qp = ctrl->val;
  1355. break;
  1356. case V4L2_CID_MPEG_MFC51_VIDEO_H264_ADAPTIVE_RC_DARK:
  1357. p->codec.h264.rc_mb_dark = ctrl->val;
  1358. break;
  1359. case V4L2_CID_MPEG_MFC51_VIDEO_H264_ADAPTIVE_RC_SMOOTH:
  1360. p->codec.h264.rc_mb_smooth = ctrl->val;
  1361. break;
  1362. case V4L2_CID_MPEG_MFC51_VIDEO_H264_ADAPTIVE_RC_STATIC:
  1363. p->codec.h264.rc_mb_static = ctrl->val;
  1364. break;
  1365. case V4L2_CID_MPEG_MFC51_VIDEO_H264_ADAPTIVE_RC_ACTIVITY:
  1366. p->codec.h264.rc_mb_activity = ctrl->val;
  1367. break;
  1368. case V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_ENABLE:
  1369. p->codec.h264.vui_sar = ctrl->val;
  1370. break;
  1371. case V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_IDC:
  1372. p->codec.h264.vui_sar_idc = vui_sar_idc(ctrl->val);
  1373. break;
  1374. case V4L2_CID_MPEG_VIDEO_H264_VUI_EXT_SAR_WIDTH:
  1375. p->codec.h264.vui_ext_sar_width = ctrl->val;
  1376. break;
  1377. case V4L2_CID_MPEG_VIDEO_H264_VUI_EXT_SAR_HEIGHT:
  1378. p->codec.h264.vui_ext_sar_height = ctrl->val;
  1379. break;
  1380. case V4L2_CID_MPEG_VIDEO_GOP_CLOSURE:
  1381. p->codec.h264.open_gop = !ctrl->val;
  1382. break;
  1383. case V4L2_CID_MPEG_VIDEO_H264_I_PERIOD:
  1384. p->codec.h264.open_gop_size = ctrl->val;
  1385. break;
  1386. case V4L2_CID_MPEG_VIDEO_MPEG4_PROFILE:
  1387. switch (ctrl->val) {
  1388. case V4L2_MPEG_VIDEO_MPEG4_PROFILE_SIMPLE:
  1389. p->codec.mpeg4.profile =
  1390. S5P_FIMV_ENC_PROFILE_MPEG4_SIMPLE;
  1391. break;
  1392. case V4L2_MPEG_VIDEO_MPEG4_PROFILE_ADVANCED_SIMPLE:
  1393. p->codec.mpeg4.profile =
  1394. S5P_FIMV_ENC_PROFILE_MPEG4_ADVANCED_SIMPLE;
  1395. break;
  1396. default:
  1397. ret = -EINVAL;
  1398. }
  1399. break;
  1400. case V4L2_CID_MPEG_VIDEO_MPEG4_QPEL:
  1401. p->codec.mpeg4.quarter_pixel = ctrl->val;
  1402. break;
  1403. default:
  1404. v4l2_err(&dev->v4l2_dev, "Invalid control, id=%d, val=%d\n",
  1405. ctrl->id, ctrl->val);
  1406. ret = -EINVAL;
  1407. }
  1408. return ret;
  1409. }
  1410. static const struct v4l2_ctrl_ops s5p_mfc_enc_ctrl_ops = {
  1411. .s_ctrl = s5p_mfc_enc_s_ctrl,
  1412. };
  1413. static int vidioc_s_parm(struct file *file, void *priv,
  1414. struct v4l2_streamparm *a)
  1415. {
  1416. struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
  1417. if (a->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
  1418. ctx->enc_params.rc_framerate_num =
  1419. a->parm.output.timeperframe.denominator;
  1420. ctx->enc_params.rc_framerate_denom =
  1421. a->parm.output.timeperframe.numerator;
  1422. } else {
  1423. mfc_err("Setting FPS is only possible for the output queue\n");
  1424. return -EINVAL;
  1425. }
  1426. return 0;
  1427. }
  1428. static int vidioc_g_parm(struct file *file, void *priv,
  1429. struct v4l2_streamparm *a)
  1430. {
  1431. struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
  1432. if (a->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
  1433. a->parm.output.timeperframe.denominator =
  1434. ctx->enc_params.rc_framerate_num;
  1435. a->parm.output.timeperframe.numerator =
  1436. ctx->enc_params.rc_framerate_denom;
  1437. } else {
  1438. mfc_err("Setting FPS is only possible for the output queue\n");
  1439. return -EINVAL;
  1440. }
  1441. return 0;
  1442. }
  1443. int vidioc_encoder_cmd(struct file *file, void *priv,
  1444. struct v4l2_encoder_cmd *cmd)
  1445. {
  1446. struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
  1447. struct s5p_mfc_dev *dev = ctx->dev;
  1448. struct s5p_mfc_buf *buf;
  1449. unsigned long flags;
  1450. switch (cmd->cmd) {
  1451. case V4L2_ENC_CMD_STOP:
  1452. if (cmd->flags != 0)
  1453. return -EINVAL;
  1454. if (!ctx->vq_src.streaming)
  1455. return -EINVAL;
  1456. spin_lock_irqsave(&dev->irqlock, flags);
  1457. if (list_empty(&ctx->src_queue)) {
  1458. mfc_debug(2, "EOS: empty src queue, entering finishing state");
  1459. ctx->state = MFCINST_FINISHING;
  1460. if (s5p_mfc_ctx_ready(ctx))
  1461. set_work_bit_irqsave(ctx);
  1462. spin_unlock_irqrestore(&dev->irqlock, flags);
  1463. s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
  1464. } else {
  1465. mfc_debug(2, "EOS: marking last buffer of stream");
  1466. buf = list_entry(ctx->src_queue.prev,
  1467. struct s5p_mfc_buf, list);
  1468. if (buf->flags & MFC_BUF_FLAG_USED)
  1469. ctx->state = MFCINST_FINISHING;
  1470. else
  1471. buf->flags |= MFC_BUF_FLAG_EOS;
  1472. spin_unlock_irqrestore(&dev->irqlock, flags);
  1473. }
  1474. break;
  1475. default:
  1476. return -EINVAL;
  1477. }
  1478. return 0;
  1479. }
  1480. static int vidioc_subscribe_event(struct v4l2_fh *fh,
  1481. const struct v4l2_event_subscription *sub)
  1482. {
  1483. switch (sub->type) {
  1484. case V4L2_EVENT_EOS:
  1485. return v4l2_event_subscribe(fh, sub, 2, NULL);
  1486. default:
  1487. return -EINVAL;
  1488. }
  1489. }
  1490. static const struct v4l2_ioctl_ops s5p_mfc_enc_ioctl_ops = {
  1491. .vidioc_querycap = vidioc_querycap,
  1492. .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  1493. .vidioc_enum_fmt_vid_cap_mplane = vidioc_enum_fmt_vid_cap_mplane,
  1494. .vidioc_enum_fmt_vid_out = vidioc_enum_fmt_vid_out,
  1495. .vidioc_enum_fmt_vid_out_mplane = vidioc_enum_fmt_vid_out_mplane,
  1496. .vidioc_g_fmt_vid_cap_mplane = vidioc_g_fmt,
  1497. .vidioc_g_fmt_vid_out_mplane = vidioc_g_fmt,
  1498. .vidioc_try_fmt_vid_cap_mplane = vidioc_try_fmt,
  1499. .vidioc_try_fmt_vid_out_mplane = vidioc_try_fmt,
  1500. .vidioc_s_fmt_vid_cap_mplane = vidioc_s_fmt,
  1501. .vidioc_s_fmt_vid_out_mplane = vidioc_s_fmt,
  1502. .vidioc_reqbufs = vidioc_reqbufs,
  1503. .vidioc_querybuf = vidioc_querybuf,
  1504. .vidioc_qbuf = vidioc_qbuf,
  1505. .vidioc_dqbuf = vidioc_dqbuf,
  1506. .vidioc_expbuf = vidioc_expbuf,
  1507. .vidioc_streamon = vidioc_streamon,
  1508. .vidioc_streamoff = vidioc_streamoff,
  1509. .vidioc_s_parm = vidioc_s_parm,
  1510. .vidioc_g_parm = vidioc_g_parm,
  1511. .vidioc_encoder_cmd = vidioc_encoder_cmd,
  1512. .vidioc_subscribe_event = vidioc_subscribe_event,
  1513. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  1514. };
  1515. static int check_vb_with_fmt(struct s5p_mfc_fmt *fmt, struct vb2_buffer *vb)
  1516. {
  1517. int i;
  1518. if (!fmt)
  1519. return -EINVAL;
  1520. if (fmt->num_planes != vb->num_planes) {
  1521. mfc_err("invalid plane number for the format\n");
  1522. return -EINVAL;
  1523. }
  1524. for (i = 0; i < fmt->num_planes; i++) {
  1525. if (!vb2_dma_contig_plane_dma_addr(vb, i)) {
  1526. mfc_err("failed to get plane cookie\n");
  1527. return -EINVAL;
  1528. }
  1529. mfc_debug(2, "index: %d, plane[%d] cookie: 0x%08zx",
  1530. vb->v4l2_buf.index, i,
  1531. vb2_dma_contig_plane_dma_addr(vb, i));
  1532. }
  1533. return 0;
  1534. }
  1535. static int s5p_mfc_queue_setup(struct vb2_queue *vq,
  1536. const struct v4l2_format *fmt,
  1537. unsigned int *buf_count, unsigned int *plane_count,
  1538. unsigned int psize[], void *allocators[])
  1539. {
  1540. struct s5p_mfc_ctx *ctx = fh_to_ctx(vq->drv_priv);
  1541. struct s5p_mfc_dev *dev = ctx->dev;
  1542. if (ctx->state != MFCINST_GOT_INST) {
  1543. mfc_err("inavlid state: %d\n", ctx->state);
  1544. return -EINVAL;
  1545. }
  1546. if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
  1547. if (ctx->dst_fmt)
  1548. *plane_count = ctx->dst_fmt->num_planes;
  1549. else
  1550. *plane_count = MFC_ENC_CAP_PLANE_COUNT;
  1551. if (*buf_count < 1)
  1552. *buf_count = 1;
  1553. if (*buf_count > MFC_MAX_BUFFERS)
  1554. *buf_count = MFC_MAX_BUFFERS;
  1555. psize[0] = ctx->enc_dst_buf_size;
  1556. allocators[0] = ctx->dev->alloc_ctx[MFC_BANK1_ALLOC_CTX];
  1557. } else if (vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
  1558. if (ctx->src_fmt)
  1559. *plane_count = ctx->src_fmt->num_planes;
  1560. else
  1561. *plane_count = MFC_ENC_OUT_PLANE_COUNT;
  1562. if (*buf_count < 1)
  1563. *buf_count = 1;
  1564. if (*buf_count > MFC_MAX_BUFFERS)
  1565. *buf_count = MFC_MAX_BUFFERS;
  1566. psize[0] = ctx->luma_size;
  1567. psize[1] = ctx->chroma_size;
  1568. if (IS_MFCV6(dev)) {
  1569. allocators[0] =
  1570. ctx->dev->alloc_ctx[MFC_BANK1_ALLOC_CTX];
  1571. allocators[1] =
  1572. ctx->dev->alloc_ctx[MFC_BANK1_ALLOC_CTX];
  1573. } else {
  1574. allocators[0] =
  1575. ctx->dev->alloc_ctx[MFC_BANK2_ALLOC_CTX];
  1576. allocators[1] =
  1577. ctx->dev->alloc_ctx[MFC_BANK2_ALLOC_CTX];
  1578. }
  1579. } else {
  1580. mfc_err("inavlid queue type: %d\n", vq->type);
  1581. return -EINVAL;
  1582. }
  1583. return 0;
  1584. }
  1585. static void s5p_mfc_unlock(struct vb2_queue *q)
  1586. {
  1587. struct s5p_mfc_ctx *ctx = fh_to_ctx(q->drv_priv);
  1588. struct s5p_mfc_dev *dev = ctx->dev;
  1589. mutex_unlock(&dev->mfc_mutex);
  1590. }
  1591. static void s5p_mfc_lock(struct vb2_queue *q)
  1592. {
  1593. struct s5p_mfc_ctx *ctx = fh_to_ctx(q->drv_priv);
  1594. struct s5p_mfc_dev *dev = ctx->dev;
  1595. mutex_lock(&dev->mfc_mutex);
  1596. }
  1597. static int s5p_mfc_buf_init(struct vb2_buffer *vb)
  1598. {
  1599. struct vb2_queue *vq = vb->vb2_queue;
  1600. struct s5p_mfc_ctx *ctx = fh_to_ctx(vq->drv_priv);
  1601. unsigned int i;
  1602. int ret;
  1603. if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
  1604. ret = check_vb_with_fmt(ctx->dst_fmt, vb);
  1605. if (ret < 0)
  1606. return ret;
  1607. i = vb->v4l2_buf.index;
  1608. ctx->dst_bufs[i].b = vb;
  1609. ctx->dst_bufs[i].cookie.stream =
  1610. vb2_dma_contig_plane_dma_addr(vb, 0);
  1611. ctx->dst_bufs_cnt++;
  1612. } else if (vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
  1613. ret = check_vb_with_fmt(ctx->src_fmt, vb);
  1614. if (ret < 0)
  1615. return ret;
  1616. i = vb->v4l2_buf.index;
  1617. ctx->src_bufs[i].b = vb;
  1618. ctx->src_bufs[i].cookie.raw.luma =
  1619. vb2_dma_contig_plane_dma_addr(vb, 0);
  1620. ctx->src_bufs[i].cookie.raw.chroma =
  1621. vb2_dma_contig_plane_dma_addr(vb, 1);
  1622. ctx->src_bufs_cnt++;
  1623. } else {
  1624. mfc_err("inavlid queue type: %d\n", vq->type);
  1625. return -EINVAL;
  1626. }
  1627. return 0;
  1628. }
  1629. static int s5p_mfc_buf_prepare(struct vb2_buffer *vb)
  1630. {
  1631. struct vb2_queue *vq = vb->vb2_queue;
  1632. struct s5p_mfc_ctx *ctx = fh_to_ctx(vq->drv_priv);
  1633. int ret;
  1634. if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
  1635. ret = check_vb_with_fmt(ctx->dst_fmt, vb);
  1636. if (ret < 0)
  1637. return ret;
  1638. mfc_debug(2, "plane size: %ld, dst size: %d\n",
  1639. vb2_plane_size(vb, 0), ctx->enc_dst_buf_size);
  1640. if (vb2_plane_size(vb, 0) < ctx->enc_dst_buf_size) {
  1641. mfc_err("plane size is too small for capture\n");
  1642. return -EINVAL;
  1643. }
  1644. } else if (vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
  1645. ret = check_vb_with_fmt(ctx->src_fmt, vb);
  1646. if (ret < 0)
  1647. return ret;
  1648. mfc_debug(2, "plane size: %ld, luma size: %d\n",
  1649. vb2_plane_size(vb, 0), ctx->luma_size);
  1650. mfc_debug(2, "plane size: %ld, chroma size: %d\n",
  1651. vb2_plane_size(vb, 1), ctx->chroma_size);
  1652. if (vb2_plane_size(vb, 0) < ctx->luma_size ||
  1653. vb2_plane_size(vb, 1) < ctx->chroma_size) {
  1654. mfc_err("plane size is too small for output\n");
  1655. return -EINVAL;
  1656. }
  1657. } else {
  1658. mfc_err("inavlid queue type: %d\n", vq->type);
  1659. return -EINVAL;
  1660. }
  1661. return 0;
  1662. }
  1663. static int s5p_mfc_start_streaming(struct vb2_queue *q, unsigned int count)
  1664. {
  1665. struct s5p_mfc_ctx *ctx = fh_to_ctx(q->drv_priv);
  1666. struct s5p_mfc_dev *dev = ctx->dev;
  1667. v4l2_ctrl_handler_setup(&ctx->ctrl_handler);
  1668. /* If context is ready then dev = work->data;schedule it to run */
  1669. if (s5p_mfc_ctx_ready(ctx))
  1670. set_work_bit_irqsave(ctx);
  1671. s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
  1672. return 0;
  1673. }
  1674. static int s5p_mfc_stop_streaming(struct vb2_queue *q)
  1675. {
  1676. unsigned long flags;
  1677. struct s5p_mfc_ctx *ctx = fh_to_ctx(q->drv_priv);
  1678. struct s5p_mfc_dev *dev = ctx->dev;
  1679. if ((ctx->state == MFCINST_FINISHING ||
  1680. ctx->state == MFCINST_RUNNING) &&
  1681. dev->curr_ctx == ctx->num && dev->hw_lock) {
  1682. ctx->state = MFCINST_ABORT;
  1683. s5p_mfc_wait_for_done_ctx(ctx, S5P_MFC_R2H_CMD_FRAME_DONE_RET,
  1684. 0);
  1685. }
  1686. ctx->state = MFCINST_FINISHED;
  1687. spin_lock_irqsave(&dev->irqlock, flags);
  1688. if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
  1689. s5p_mfc_hw_call(dev->mfc_ops, cleanup_queue, &ctx->dst_queue,
  1690. &ctx->vq_dst);
  1691. INIT_LIST_HEAD(&ctx->dst_queue);
  1692. ctx->dst_queue_cnt = 0;
  1693. }
  1694. if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
  1695. cleanup_ref_queue(ctx);
  1696. s5p_mfc_hw_call(dev->mfc_ops, cleanup_queue, &ctx->src_queue,
  1697. &ctx->vq_src);
  1698. INIT_LIST_HEAD(&ctx->src_queue);
  1699. ctx->src_queue_cnt = 0;
  1700. }
  1701. spin_unlock_irqrestore(&dev->irqlock, flags);
  1702. return 0;
  1703. }
  1704. static void s5p_mfc_buf_queue(struct vb2_buffer *vb)
  1705. {
  1706. struct vb2_queue *vq = vb->vb2_queue;
  1707. struct s5p_mfc_ctx *ctx = fh_to_ctx(vq->drv_priv);
  1708. struct s5p_mfc_dev *dev = ctx->dev;
  1709. unsigned long flags;
  1710. struct s5p_mfc_buf *mfc_buf;
  1711. if (ctx->state == MFCINST_ERROR) {
  1712. vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
  1713. cleanup_ref_queue(ctx);
  1714. return;
  1715. }
  1716. if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
  1717. mfc_buf = &ctx->dst_bufs[vb->v4l2_buf.index];
  1718. mfc_buf->flags &= ~MFC_BUF_FLAG_USED;
  1719. /* Mark destination as available for use by MFC */
  1720. spin_lock_irqsave(&dev->irqlock, flags);
  1721. list_add_tail(&mfc_buf->list, &ctx->dst_queue);
  1722. ctx->dst_queue_cnt++;
  1723. spin_unlock_irqrestore(&dev->irqlock, flags);
  1724. } else if (vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
  1725. mfc_buf = &ctx->src_bufs[vb->v4l2_buf.index];
  1726. mfc_buf->flags &= ~MFC_BUF_FLAG_USED;
  1727. spin_lock_irqsave(&dev->irqlock, flags);
  1728. list_add_tail(&mfc_buf->list, &ctx->src_queue);
  1729. ctx->src_queue_cnt++;
  1730. spin_unlock_irqrestore(&dev->irqlock, flags);
  1731. } else {
  1732. mfc_err("unsupported buffer type (%d)\n", vq->type);
  1733. }
  1734. if (s5p_mfc_ctx_ready(ctx))
  1735. set_work_bit_irqsave(ctx);
  1736. s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
  1737. }
  1738. static struct vb2_ops s5p_mfc_enc_qops = {
  1739. .queue_setup = s5p_mfc_queue_setup,
  1740. .wait_prepare = s5p_mfc_unlock,
  1741. .wait_finish = s5p_mfc_lock,
  1742. .buf_init = s5p_mfc_buf_init,
  1743. .buf_prepare = s5p_mfc_buf_prepare,
  1744. .start_streaming = s5p_mfc_start_streaming,
  1745. .stop_streaming = s5p_mfc_stop_streaming,
  1746. .buf_queue = s5p_mfc_buf_queue,
  1747. };
  1748. struct s5p_mfc_codec_ops *get_enc_codec_ops(void)
  1749. {
  1750. return &encoder_codec_ops;
  1751. }
  1752. struct vb2_ops *get_enc_queue_ops(void)
  1753. {
  1754. return &s5p_mfc_enc_qops;
  1755. }
  1756. const struct v4l2_ioctl_ops *get_enc_v4l2_ioctl_ops(void)
  1757. {
  1758. return &s5p_mfc_enc_ioctl_ops;
  1759. }
  1760. #define IS_MFC51_PRIV(x) ((V4L2_CTRL_ID2CLASS(x) == V4L2_CTRL_CLASS_MPEG) \
  1761. && V4L2_CTRL_DRIVER_PRIV(x))
  1762. int s5p_mfc_enc_ctrls_setup(struct s5p_mfc_ctx *ctx)
  1763. {
  1764. struct v4l2_ctrl_config cfg;
  1765. int i;
  1766. v4l2_ctrl_handler_init(&ctx->ctrl_handler, NUM_CTRLS);
  1767. if (ctx->ctrl_handler.error) {
  1768. mfc_err("v4l2_ctrl_handler_init failed\n");
  1769. return ctx->ctrl_handler.error;
  1770. }
  1771. for (i = 0; i < NUM_CTRLS; i++) {
  1772. if (IS_MFC51_PRIV(controls[i].id)) {
  1773. memset(&cfg, 0, sizeof(struct v4l2_ctrl_config));
  1774. cfg.ops = &s5p_mfc_enc_ctrl_ops;
  1775. cfg.id = controls[i].id;
  1776. cfg.min = controls[i].minimum;
  1777. cfg.max = controls[i].maximum;
  1778. cfg.def = controls[i].default_value;
  1779. cfg.name = controls[i].name;
  1780. cfg.type = controls[i].type;
  1781. cfg.flags = 0;
  1782. if (cfg.type == V4L2_CTRL_TYPE_MENU) {
  1783. cfg.step = 0;
  1784. cfg.menu_skip_mask = cfg.menu_skip_mask;
  1785. cfg.qmenu = mfc51_get_menu(cfg.id);
  1786. } else {
  1787. cfg.step = controls[i].step;
  1788. cfg.menu_skip_mask = 0;
  1789. }
  1790. ctx->ctrls[i] = v4l2_ctrl_new_custom(&ctx->ctrl_handler,
  1791. &cfg, NULL);
  1792. } else {
  1793. if (controls[i].type == V4L2_CTRL_TYPE_MENU) {
  1794. ctx->ctrls[i] = v4l2_ctrl_new_std_menu(
  1795. &ctx->ctrl_handler,
  1796. &s5p_mfc_enc_ctrl_ops, controls[i].id,
  1797. controls[i].maximum, 0,
  1798. controls[i].default_value);
  1799. } else {
  1800. ctx->ctrls[i] = v4l2_ctrl_new_std(
  1801. &ctx->ctrl_handler,
  1802. &s5p_mfc_enc_ctrl_ops, controls[i].id,
  1803. controls[i].minimum,
  1804. controls[i].maximum, controls[i].step,
  1805. controls[i].default_value);
  1806. }
  1807. }
  1808. if (ctx->ctrl_handler.error) {
  1809. mfc_err("Adding control (%d) failed\n", i);
  1810. return ctx->ctrl_handler.error;
  1811. }
  1812. if (controls[i].is_volatile && ctx->ctrls[i])
  1813. ctx->ctrls[i]->flags |= V4L2_CTRL_FLAG_VOLATILE;
  1814. }
  1815. return 0;
  1816. }
  1817. void s5p_mfc_enc_ctrls_delete(struct s5p_mfc_ctx *ctx)
  1818. {
  1819. int i;
  1820. v4l2_ctrl_handler_free(&ctx->ctrl_handler);
  1821. for (i = 0; i < NUM_CTRLS; i++)
  1822. ctx->ctrls[i] = NULL;
  1823. }
  1824. void s5p_mfc_enc_init(struct s5p_mfc_ctx *ctx)
  1825. {
  1826. struct v4l2_format f;
  1827. f.fmt.pix_mp.pixelformat = DEF_SRC_FMT_ENC;
  1828. ctx->src_fmt = find_format(&f, MFC_FMT_RAW);
  1829. f.fmt.pix_mp.pixelformat = DEF_DST_FMT_ENC;
  1830. ctx->dst_fmt = find_format(&f, MFC_FMT_ENC);
  1831. }