s5p_mfc_enc.c 52 KB

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