s5p_mfc_enc.c 58 KB

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