s5p_mfc_enc.c 58 KB

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