s5p_mfc_enc.c 51 KB

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