s5p_mfc_enc.c 56 KB

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