s5p_mfc_enc.c 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828
  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. cap->capabilities = V4L2_CAP_VIDEO_CAPTURE_MPLANE
  761. | V4L2_CAP_VIDEO_OUTPUT_MPLANE
  762. | V4L2_CAP_STREAMING;
  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. unsigned long flags;
  881. int ret = 0;
  882. ret = vidioc_try_fmt(file, priv, f);
  883. if (ret)
  884. return ret;
  885. if (ctx->vq_src.streaming || ctx->vq_dst.streaming) {
  886. v4l2_err(&dev->v4l2_dev, "%s queue busy\n", __func__);
  887. ret = -EBUSY;
  888. goto out;
  889. }
  890. if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
  891. fmt = find_format(f, MFC_FMT_ENC);
  892. if (!fmt) {
  893. mfc_err("failed to set capture format\n");
  894. return -EINVAL;
  895. }
  896. ctx->state = MFCINST_INIT;
  897. ctx->dst_fmt = fmt;
  898. ctx->codec_mode = ctx->dst_fmt->codec_mode;
  899. ctx->enc_dst_buf_size = pix_fmt_mp->plane_fmt[0].sizeimage;
  900. pix_fmt_mp->plane_fmt[0].bytesperline = 0;
  901. ctx->dst_bufs_cnt = 0;
  902. ctx->capture_state = QUEUE_FREE;
  903. s5p_mfc_alloc_instance_buffer(ctx);
  904. spin_lock_irqsave(&dev->condlock, flags);
  905. set_bit(ctx->num, &dev->ctx_work_bits);
  906. spin_unlock_irqrestore(&dev->condlock, flags);
  907. s5p_mfc_clean_ctx_int_flags(ctx);
  908. s5p_mfc_try_run(dev);
  909. if (s5p_mfc_wait_for_done_ctx(ctx, \
  910. S5P_FIMV_R2H_CMD_OPEN_INSTANCE_RET, 1)) {
  911. /* Error or timeout */
  912. mfc_err("Error getting instance from hardware\n");
  913. s5p_mfc_release_instance_buffer(ctx);
  914. ret = -EIO;
  915. goto out;
  916. }
  917. mfc_debug(2, "Got instance number: %d\n", ctx->inst_no);
  918. } else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
  919. fmt = find_format(f, MFC_FMT_RAW);
  920. if (!fmt) {
  921. mfc_err("failed to set output format\n");
  922. return -EINVAL;
  923. }
  924. if (fmt->num_planes != pix_fmt_mp->num_planes) {
  925. mfc_err("failed to set output format\n");
  926. ret = -EINVAL;
  927. goto out;
  928. }
  929. ctx->src_fmt = fmt;
  930. ctx->img_width = pix_fmt_mp->width;
  931. ctx->img_height = pix_fmt_mp->height;
  932. mfc_debug(2, "codec number: %d\n", ctx->src_fmt->codec_mode);
  933. mfc_debug(2, "fmt - w: %d, h: %d, ctx - w: %d, h: %d\n",
  934. pix_fmt_mp->width, pix_fmt_mp->height,
  935. ctx->img_width, ctx->img_height);
  936. if (ctx->src_fmt->fourcc == V4L2_PIX_FMT_NV12M) {
  937. ctx->buf_width = ALIGN(ctx->img_width,
  938. S5P_FIMV_NV12M_HALIGN);
  939. ctx->luma_size = ALIGN(ctx->img_width,
  940. S5P_FIMV_NV12M_HALIGN) * ALIGN(ctx->img_height,
  941. S5P_FIMV_NV12M_LVALIGN);
  942. ctx->chroma_size = ALIGN(ctx->img_width,
  943. S5P_FIMV_NV12M_HALIGN) * ALIGN((ctx->img_height
  944. >> 1), S5P_FIMV_NV12M_CVALIGN);
  945. ctx->luma_size = ALIGN(ctx->luma_size,
  946. S5P_FIMV_NV12M_SALIGN);
  947. ctx->chroma_size = ALIGN(ctx->chroma_size,
  948. S5P_FIMV_NV12M_SALIGN);
  949. pix_fmt_mp->plane_fmt[0].sizeimage = ctx->luma_size;
  950. pix_fmt_mp->plane_fmt[0].bytesperline = ctx->buf_width;
  951. pix_fmt_mp->plane_fmt[1].sizeimage = ctx->chroma_size;
  952. pix_fmt_mp->plane_fmt[1].bytesperline = ctx->buf_width;
  953. } else if (ctx->src_fmt->fourcc == V4L2_PIX_FMT_NV12MT) {
  954. ctx->buf_width = ALIGN(ctx->img_width,
  955. S5P_FIMV_NV12MT_HALIGN);
  956. ctx->luma_size = ALIGN(ctx->img_width,
  957. S5P_FIMV_NV12MT_HALIGN) * ALIGN(ctx->img_height,
  958. S5P_FIMV_NV12MT_VALIGN);
  959. ctx->chroma_size = ALIGN(ctx->img_width,
  960. S5P_FIMV_NV12MT_HALIGN) * ALIGN((ctx->img_height
  961. >> 1), S5P_FIMV_NV12MT_VALIGN);
  962. ctx->luma_size = ALIGN(ctx->luma_size,
  963. S5P_FIMV_NV12MT_SALIGN);
  964. ctx->chroma_size = ALIGN(ctx->chroma_size,
  965. S5P_FIMV_NV12MT_SALIGN);
  966. pix_fmt_mp->plane_fmt[0].sizeimage = ctx->luma_size;
  967. pix_fmt_mp->plane_fmt[0].bytesperline = ctx->buf_width;
  968. pix_fmt_mp->plane_fmt[1].sizeimage = ctx->chroma_size;
  969. pix_fmt_mp->plane_fmt[1].bytesperline = ctx->buf_width;
  970. }
  971. ctx->src_bufs_cnt = 0;
  972. ctx->output_state = QUEUE_FREE;
  973. } else {
  974. mfc_err("invalid buf type\n");
  975. return -EINVAL;
  976. }
  977. out:
  978. mfc_debug_leave();
  979. return ret;
  980. }
  981. static int vidioc_reqbufs(struct file *file, void *priv,
  982. struct v4l2_requestbuffers *reqbufs)
  983. {
  984. struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
  985. int ret = 0;
  986. /* if memory is not mmp or userptr return error */
  987. if ((reqbufs->memory != V4L2_MEMORY_MMAP) &&
  988. (reqbufs->memory != V4L2_MEMORY_USERPTR))
  989. return -EINVAL;
  990. if (reqbufs->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
  991. if (ctx->capture_state != QUEUE_FREE) {
  992. mfc_err("invalid capture state: %d\n",
  993. ctx->capture_state);
  994. return -EINVAL;
  995. }
  996. ret = vb2_reqbufs(&ctx->vq_dst, reqbufs);
  997. if (ret != 0) {
  998. mfc_err("error in vb2_reqbufs() for E(D)\n");
  999. return ret;
  1000. }
  1001. ctx->capture_state = QUEUE_BUFS_REQUESTED;
  1002. ret = s5p_mfc_alloc_codec_buffers(ctx);
  1003. if (ret) {
  1004. mfc_err("Failed to allocate encoding buffers\n");
  1005. reqbufs->count = 0;
  1006. ret = vb2_reqbufs(&ctx->vq_dst, reqbufs);
  1007. return -ENOMEM;
  1008. }
  1009. } else if (reqbufs->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
  1010. if (ctx->output_state != QUEUE_FREE) {
  1011. mfc_err("invalid output state: %d\n",
  1012. ctx->output_state);
  1013. return -EINVAL;
  1014. }
  1015. ret = vb2_reqbufs(&ctx->vq_src, reqbufs);
  1016. if (ret != 0) {
  1017. mfc_err("error in vb2_reqbufs() for E(S)\n");
  1018. return ret;
  1019. }
  1020. ctx->output_state = QUEUE_BUFS_REQUESTED;
  1021. } else {
  1022. mfc_err("invalid buf type\n");
  1023. return -EINVAL;
  1024. }
  1025. return ret;
  1026. }
  1027. static int vidioc_querybuf(struct file *file, void *priv,
  1028. struct v4l2_buffer *buf)
  1029. {
  1030. struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
  1031. int ret = 0;
  1032. /* if memory is not mmp or userptr return error */
  1033. if ((buf->memory != V4L2_MEMORY_MMAP) &&
  1034. (buf->memory != V4L2_MEMORY_USERPTR))
  1035. return -EINVAL;
  1036. if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
  1037. if (ctx->state != MFCINST_GOT_INST) {
  1038. mfc_err("invalid context state: %d\n", ctx->state);
  1039. return -EINVAL;
  1040. }
  1041. ret = vb2_querybuf(&ctx->vq_dst, buf);
  1042. if (ret != 0) {
  1043. mfc_err("error in vb2_querybuf() for E(D)\n");
  1044. return ret;
  1045. }
  1046. buf->m.planes[0].m.mem_offset += DST_QUEUE_OFF_BASE;
  1047. } else if (buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
  1048. ret = vb2_querybuf(&ctx->vq_src, buf);
  1049. if (ret != 0) {
  1050. mfc_err("error in vb2_querybuf() for E(S)\n");
  1051. return ret;
  1052. }
  1053. } else {
  1054. mfc_err("invalid buf type\n");
  1055. return -EINVAL;
  1056. }
  1057. return ret;
  1058. }
  1059. /* Queue a buffer */
  1060. static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
  1061. {
  1062. struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
  1063. if (ctx->state == MFCINST_ERROR) {
  1064. mfc_err("Call on QBUF after unrecoverable error\n");
  1065. return -EIO;
  1066. }
  1067. if (buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
  1068. return vb2_qbuf(&ctx->vq_src, buf);
  1069. else if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
  1070. return vb2_qbuf(&ctx->vq_dst, buf);
  1071. return -EINVAL;
  1072. }
  1073. /* Dequeue a buffer */
  1074. static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
  1075. {
  1076. struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
  1077. if (ctx->state == MFCINST_ERROR) {
  1078. mfc_err("Call on DQBUF after unrecoverable error\n");
  1079. return -EIO;
  1080. }
  1081. if (buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
  1082. return vb2_dqbuf(&ctx->vq_src, buf, file->f_flags & O_NONBLOCK);
  1083. else if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
  1084. return vb2_dqbuf(&ctx->vq_dst, buf, file->f_flags & O_NONBLOCK);
  1085. return -EINVAL;
  1086. }
  1087. /* Stream on */
  1088. static int vidioc_streamon(struct file *file, void *priv,
  1089. enum v4l2_buf_type type)
  1090. {
  1091. struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
  1092. if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
  1093. return vb2_streamon(&ctx->vq_src, type);
  1094. else if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
  1095. return vb2_streamon(&ctx->vq_dst, type);
  1096. return -EINVAL;
  1097. }
  1098. /* Stream off, which equals to a pause */
  1099. static int vidioc_streamoff(struct file *file, void *priv,
  1100. enum v4l2_buf_type type)
  1101. {
  1102. struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
  1103. if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
  1104. return vb2_streamoff(&ctx->vq_src, type);
  1105. else if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
  1106. return vb2_streamoff(&ctx->vq_dst, type);
  1107. return -EINVAL;
  1108. }
  1109. static inline int h264_level(enum v4l2_mpeg_video_h264_level lvl)
  1110. {
  1111. static unsigned int t[V4L2_MPEG_VIDEO_H264_LEVEL_4_0 + 1] = {
  1112. /* V4L2_MPEG_VIDEO_H264_LEVEL_1_0 */ 10,
  1113. /* V4L2_MPEG_VIDEO_H264_LEVEL_1B */ 9,
  1114. /* V4L2_MPEG_VIDEO_H264_LEVEL_1_1 */ 11,
  1115. /* V4L2_MPEG_VIDEO_H264_LEVEL_1_2 */ 12,
  1116. /* V4L2_MPEG_VIDEO_H264_LEVEL_1_3 */ 13,
  1117. /* V4L2_MPEG_VIDEO_H264_LEVEL_2_0 */ 20,
  1118. /* V4L2_MPEG_VIDEO_H264_LEVEL_2_1 */ 21,
  1119. /* V4L2_MPEG_VIDEO_H264_LEVEL_2_2 */ 22,
  1120. /* V4L2_MPEG_VIDEO_H264_LEVEL_3_0 */ 30,
  1121. /* V4L2_MPEG_VIDEO_H264_LEVEL_3_1 */ 31,
  1122. /* V4L2_MPEG_VIDEO_H264_LEVEL_3_2 */ 32,
  1123. /* V4L2_MPEG_VIDEO_H264_LEVEL_4_0 */ 40,
  1124. };
  1125. return t[lvl];
  1126. }
  1127. static inline int mpeg4_level(enum v4l2_mpeg_video_mpeg4_level lvl)
  1128. {
  1129. static unsigned int t[V4L2_MPEG_VIDEO_MPEG4_LEVEL_5 + 1] = {
  1130. /* V4L2_MPEG_VIDEO_MPEG4_LEVEL_0 */ 0,
  1131. /* V4L2_MPEG_VIDEO_MPEG4_LEVEL_0B */ 9,
  1132. /* V4L2_MPEG_VIDEO_MPEG4_LEVEL_1 */ 1,
  1133. /* V4L2_MPEG_VIDEO_MPEG4_LEVEL_2 */ 2,
  1134. /* V4L2_MPEG_VIDEO_MPEG4_LEVEL_3 */ 3,
  1135. /* V4L2_MPEG_VIDEO_MPEG4_LEVEL_3B */ 7,
  1136. /* V4L2_MPEG_VIDEO_MPEG4_LEVEL_4 */ 4,
  1137. /* V4L2_MPEG_VIDEO_MPEG4_LEVEL_5 */ 5,
  1138. };
  1139. return t[lvl];
  1140. }
  1141. static inline int vui_sar_idc(enum v4l2_mpeg_video_h264_vui_sar_idc sar)
  1142. {
  1143. static unsigned int t[V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_EXTENDED + 1] = {
  1144. /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_UNSPECIFIED */ 0,
  1145. /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_1x1 */ 1,
  1146. /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_12x11 */ 2,
  1147. /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_10x11 */ 3,
  1148. /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_16x11 */ 4,
  1149. /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_40x33 */ 5,
  1150. /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_24x11 */ 6,
  1151. /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_20x11 */ 7,
  1152. /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_32x11 */ 8,
  1153. /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_80x33 */ 9,
  1154. /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_18x11 */ 10,
  1155. /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_15x11 */ 11,
  1156. /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_64x33 */ 12,
  1157. /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_160x99 */ 13,
  1158. /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_4x3 */ 14,
  1159. /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_3x2 */ 15,
  1160. /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_2x1 */ 16,
  1161. /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_EXTENDED */ 255,
  1162. };
  1163. return t[sar];
  1164. }
  1165. static int s5p_mfc_enc_s_ctrl(struct v4l2_ctrl *ctrl)
  1166. {
  1167. struct s5p_mfc_ctx *ctx = ctrl_to_ctx(ctrl);
  1168. struct s5p_mfc_dev *dev = ctx->dev;
  1169. struct s5p_mfc_enc_params *p = &ctx->enc_params;
  1170. int ret = 0;
  1171. switch (ctrl->id) {
  1172. case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
  1173. p->gop_size = ctrl->val;
  1174. break;
  1175. case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE:
  1176. p->slice_mode = ctrl->val;
  1177. break;
  1178. case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB:
  1179. p->slice_mb = ctrl->val;
  1180. break;
  1181. case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES:
  1182. p->slice_bit = ctrl->val * 8;
  1183. break;
  1184. case V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB:
  1185. p->intra_refresh_mb = ctrl->val;
  1186. break;
  1187. case V4L2_CID_MPEG_MFC51_VIDEO_PADDING:
  1188. p->pad = ctrl->val;
  1189. break;
  1190. case V4L2_CID_MPEG_MFC51_VIDEO_PADDING_YUV:
  1191. p->pad_luma = (ctrl->val >> 16) & 0xff;
  1192. p->pad_cb = (ctrl->val >> 8) & 0xff;
  1193. p->pad_cr = (ctrl->val >> 0) & 0xff;
  1194. break;
  1195. case V4L2_CID_MPEG_VIDEO_FRAME_RC_ENABLE:
  1196. p->rc_frame = ctrl->val;
  1197. break;
  1198. case V4L2_CID_MPEG_VIDEO_BITRATE:
  1199. p->rc_bitrate = ctrl->val;
  1200. break;
  1201. case V4L2_CID_MPEG_MFC51_VIDEO_RC_REACTION_COEFF:
  1202. p->rc_reaction_coeff = ctrl->val;
  1203. break;
  1204. case V4L2_CID_MPEG_MFC51_VIDEO_FORCE_FRAME_TYPE:
  1205. ctx->force_frame_type = ctrl->val;
  1206. break;
  1207. case V4L2_CID_MPEG_VIDEO_VBV_SIZE:
  1208. p->vbv_size = ctrl->val;
  1209. break;
  1210. case V4L2_CID_MPEG_VIDEO_H264_CPB_SIZE:
  1211. p->codec.h264.cpb_size = ctrl->val;
  1212. break;
  1213. case V4L2_CID_MPEG_VIDEO_HEADER_MODE:
  1214. p->seq_hdr_mode = ctrl->val;
  1215. break;
  1216. case V4L2_CID_MPEG_MFC51_VIDEO_FRAME_SKIP_MODE:
  1217. p->frame_skip_mode = ctrl->val;
  1218. break;
  1219. case V4L2_CID_MPEG_MFC51_VIDEO_RC_FIXED_TARGET_BIT:
  1220. p->fixed_target_bit = ctrl->val;
  1221. break;
  1222. case V4L2_CID_MPEG_VIDEO_B_FRAMES:
  1223. p->num_b_frame = ctrl->val;
  1224. break;
  1225. case V4L2_CID_MPEG_VIDEO_H264_PROFILE:
  1226. switch (ctrl->val) {
  1227. case V4L2_MPEG_VIDEO_H264_PROFILE_MAIN:
  1228. p->codec.h264.profile =
  1229. S5P_FIMV_ENC_PROFILE_H264_MAIN;
  1230. break;
  1231. case V4L2_MPEG_VIDEO_H264_PROFILE_HIGH:
  1232. p->codec.h264.profile =
  1233. S5P_FIMV_ENC_PROFILE_H264_HIGH;
  1234. break;
  1235. case V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE:
  1236. p->codec.h264.profile =
  1237. S5P_FIMV_ENC_PROFILE_H264_BASELINE;
  1238. break;
  1239. default:
  1240. ret = -EINVAL;
  1241. }
  1242. break;
  1243. case V4L2_CID_MPEG_VIDEO_H264_LEVEL:
  1244. p->codec.h264.level_v4l2 = ctrl->val;
  1245. p->codec.h264.level = h264_level(ctrl->val);
  1246. if (p->codec.h264.level < 0) {
  1247. mfc_err("Level number is wrong\n");
  1248. ret = p->codec.h264.level;
  1249. }
  1250. break;
  1251. case V4L2_CID_MPEG_VIDEO_MPEG4_LEVEL:
  1252. p->codec.mpeg4.level_v4l2 = ctrl->val;
  1253. p->codec.mpeg4.level = mpeg4_level(ctrl->val);
  1254. if (p->codec.mpeg4.level < 0) {
  1255. mfc_err("Level number is wrong\n");
  1256. ret = p->codec.mpeg4.level;
  1257. }
  1258. break;
  1259. case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE:
  1260. p->codec.h264.loop_filter_mode = ctrl->val;
  1261. break;
  1262. case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA:
  1263. p->codec.h264.loop_filter_alpha = ctrl->val;
  1264. break;
  1265. case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA:
  1266. p->codec.h264.loop_filter_beta = ctrl->val;
  1267. break;
  1268. case V4L2_CID_MPEG_VIDEO_H264_ENTROPY_MODE:
  1269. p->codec.h264.entropy_mode = ctrl->val;
  1270. break;
  1271. case V4L2_CID_MPEG_MFC51_VIDEO_H264_NUM_REF_PIC_FOR_P:
  1272. p->codec.h264.num_ref_pic_4p = ctrl->val;
  1273. break;
  1274. case V4L2_CID_MPEG_VIDEO_H264_8X8_TRANSFORM:
  1275. p->codec.h264._8x8_transform = ctrl->val;
  1276. break;
  1277. case V4L2_CID_MPEG_VIDEO_MB_RC_ENABLE:
  1278. p->codec.h264.rc_mb = ctrl->val;
  1279. break;
  1280. case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP:
  1281. p->codec.h264.rc_frame_qp = ctrl->val;
  1282. break;
  1283. case V4L2_CID_MPEG_VIDEO_H264_MIN_QP:
  1284. p->codec.h264.rc_min_qp = ctrl->val;
  1285. break;
  1286. case V4L2_CID_MPEG_VIDEO_H264_MAX_QP:
  1287. p->codec.h264.rc_max_qp = ctrl->val;
  1288. break;
  1289. case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP:
  1290. p->codec.h264.rc_p_frame_qp = ctrl->val;
  1291. break;
  1292. case V4L2_CID_MPEG_VIDEO_H264_B_FRAME_QP:
  1293. p->codec.h264.rc_b_frame_qp = ctrl->val;
  1294. break;
  1295. case V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP:
  1296. case V4L2_CID_MPEG_VIDEO_H263_I_FRAME_QP:
  1297. p->codec.mpeg4.rc_frame_qp = ctrl->val;
  1298. break;
  1299. case V4L2_CID_MPEG_VIDEO_MPEG4_MIN_QP:
  1300. case V4L2_CID_MPEG_VIDEO_H263_MIN_QP:
  1301. p->codec.mpeg4.rc_min_qp = ctrl->val;
  1302. break;
  1303. case V4L2_CID_MPEG_VIDEO_MPEG4_MAX_QP:
  1304. case V4L2_CID_MPEG_VIDEO_H263_MAX_QP:
  1305. p->codec.mpeg4.rc_max_qp = ctrl->val;
  1306. break;
  1307. case V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP:
  1308. case V4L2_CID_MPEG_VIDEO_H263_P_FRAME_QP:
  1309. p->codec.mpeg4.rc_p_frame_qp = ctrl->val;
  1310. break;
  1311. case V4L2_CID_MPEG_VIDEO_MPEG4_B_FRAME_QP:
  1312. case V4L2_CID_MPEG_VIDEO_H263_B_FRAME_QP:
  1313. p->codec.mpeg4.rc_b_frame_qp = ctrl->val;
  1314. break;
  1315. case V4L2_CID_MPEG_MFC51_VIDEO_H264_ADAPTIVE_RC_DARK:
  1316. p->codec.h264.rc_mb_dark = ctrl->val;
  1317. break;
  1318. case V4L2_CID_MPEG_MFC51_VIDEO_H264_ADAPTIVE_RC_SMOOTH:
  1319. p->codec.h264.rc_mb_smooth = ctrl->val;
  1320. break;
  1321. case V4L2_CID_MPEG_MFC51_VIDEO_H264_ADAPTIVE_RC_STATIC:
  1322. p->codec.h264.rc_mb_static = ctrl->val;
  1323. break;
  1324. case V4L2_CID_MPEG_MFC51_VIDEO_H264_ADAPTIVE_RC_ACTIVITY:
  1325. p->codec.h264.rc_mb_activity = ctrl->val;
  1326. break;
  1327. case V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_ENABLE:
  1328. p->codec.h264.vui_sar = ctrl->val;
  1329. break;
  1330. case V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_IDC:
  1331. p->codec.h264.vui_sar_idc = vui_sar_idc(ctrl->val);
  1332. break;
  1333. case V4L2_CID_MPEG_VIDEO_H264_VUI_EXT_SAR_WIDTH:
  1334. p->codec.h264.vui_ext_sar_width = ctrl->val;
  1335. break;
  1336. case V4L2_CID_MPEG_VIDEO_H264_VUI_EXT_SAR_HEIGHT:
  1337. p->codec.h264.vui_ext_sar_height = ctrl->val;
  1338. break;
  1339. case V4L2_CID_MPEG_VIDEO_GOP_CLOSURE:
  1340. p->codec.h264.open_gop = !ctrl->val;
  1341. break;
  1342. case V4L2_CID_MPEG_VIDEO_H264_I_PERIOD:
  1343. p->codec.h264.open_gop_size = ctrl->val;
  1344. break;
  1345. case V4L2_CID_MPEG_VIDEO_MPEG4_PROFILE:
  1346. switch (ctrl->val) {
  1347. case V4L2_MPEG_VIDEO_MPEG4_PROFILE_SIMPLE:
  1348. p->codec.mpeg4.profile =
  1349. S5P_FIMV_ENC_PROFILE_MPEG4_SIMPLE;
  1350. break;
  1351. case V4L2_MPEG_VIDEO_MPEG4_PROFILE_ADVANCED_SIMPLE:
  1352. p->codec.mpeg4.profile =
  1353. S5P_FIMV_ENC_PROFILE_MPEG4_ADVANCED_SIMPLE;
  1354. break;
  1355. default:
  1356. ret = -EINVAL;
  1357. }
  1358. break;
  1359. case V4L2_CID_MPEG_VIDEO_MPEG4_QPEL:
  1360. p->codec.mpeg4.quarter_pixel = ctrl->val;
  1361. break;
  1362. default:
  1363. v4l2_err(&dev->v4l2_dev, "Invalid control, id=%d, val=%d\n",
  1364. ctrl->id, ctrl->val);
  1365. ret = -EINVAL;
  1366. }
  1367. return ret;
  1368. }
  1369. static const struct v4l2_ctrl_ops s5p_mfc_enc_ctrl_ops = {
  1370. .s_ctrl = s5p_mfc_enc_s_ctrl,
  1371. };
  1372. static int vidioc_s_parm(struct file *file, void *priv,
  1373. struct v4l2_streamparm *a)
  1374. {
  1375. struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
  1376. if (a->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
  1377. ctx->enc_params.rc_framerate_num =
  1378. a->parm.output.timeperframe.denominator;
  1379. ctx->enc_params.rc_framerate_denom =
  1380. a->parm.output.timeperframe.numerator;
  1381. } else {
  1382. mfc_err("Setting FPS is only possible for the output queue\n");
  1383. return -EINVAL;
  1384. }
  1385. return 0;
  1386. }
  1387. static int vidioc_g_parm(struct file *file, void *priv,
  1388. struct v4l2_streamparm *a)
  1389. {
  1390. struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
  1391. if (a->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
  1392. a->parm.output.timeperframe.denominator =
  1393. ctx->enc_params.rc_framerate_num;
  1394. a->parm.output.timeperframe.numerator =
  1395. ctx->enc_params.rc_framerate_denom;
  1396. } else {
  1397. mfc_err("Setting FPS is only possible for the output queue\n");
  1398. return -EINVAL;
  1399. }
  1400. return 0;
  1401. }
  1402. static const struct v4l2_ioctl_ops s5p_mfc_enc_ioctl_ops = {
  1403. .vidioc_querycap = vidioc_querycap,
  1404. .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  1405. .vidioc_enum_fmt_vid_cap_mplane = vidioc_enum_fmt_vid_cap_mplane,
  1406. .vidioc_enum_fmt_vid_out = vidioc_enum_fmt_vid_out,
  1407. .vidioc_enum_fmt_vid_out_mplane = vidioc_enum_fmt_vid_out_mplane,
  1408. .vidioc_g_fmt_vid_cap_mplane = vidioc_g_fmt,
  1409. .vidioc_g_fmt_vid_out_mplane = vidioc_g_fmt,
  1410. .vidioc_try_fmt_vid_cap_mplane = vidioc_try_fmt,
  1411. .vidioc_try_fmt_vid_out_mplane = vidioc_try_fmt,
  1412. .vidioc_s_fmt_vid_cap_mplane = vidioc_s_fmt,
  1413. .vidioc_s_fmt_vid_out_mplane = vidioc_s_fmt,
  1414. .vidioc_reqbufs = vidioc_reqbufs,
  1415. .vidioc_querybuf = vidioc_querybuf,
  1416. .vidioc_qbuf = vidioc_qbuf,
  1417. .vidioc_dqbuf = vidioc_dqbuf,
  1418. .vidioc_streamon = vidioc_streamon,
  1419. .vidioc_streamoff = vidioc_streamoff,
  1420. .vidioc_s_parm = vidioc_s_parm,
  1421. .vidioc_g_parm = vidioc_g_parm,
  1422. };
  1423. static int check_vb_with_fmt(struct s5p_mfc_fmt *fmt, struct vb2_buffer *vb)
  1424. {
  1425. int i;
  1426. if (!fmt)
  1427. return -EINVAL;
  1428. if (fmt->num_planes != vb->num_planes) {
  1429. mfc_err("invalid plane number for the format\n");
  1430. return -EINVAL;
  1431. }
  1432. for (i = 0; i < fmt->num_planes; i++) {
  1433. if (!vb2_dma_contig_plane_dma_addr(vb, i)) {
  1434. mfc_err("failed to get plane cookie\n");
  1435. return -EINVAL;
  1436. }
  1437. mfc_debug(2, "index: %d, plane[%d] cookie: 0x%08zx",
  1438. vb->v4l2_buf.index, i,
  1439. vb2_dma_contig_plane_dma_addr(vb, i));
  1440. }
  1441. return 0;
  1442. }
  1443. static int s5p_mfc_queue_setup(struct vb2_queue *vq,
  1444. const struct v4l2_format *fmt,
  1445. unsigned int *buf_count, unsigned int *plane_count,
  1446. unsigned int psize[], void *allocators[])
  1447. {
  1448. struct s5p_mfc_ctx *ctx = fh_to_ctx(vq->drv_priv);
  1449. if (ctx->state != MFCINST_GOT_INST) {
  1450. mfc_err("inavlid state: %d\n", ctx->state);
  1451. return -EINVAL;
  1452. }
  1453. if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
  1454. if (ctx->dst_fmt)
  1455. *plane_count = ctx->dst_fmt->num_planes;
  1456. else
  1457. *plane_count = MFC_ENC_CAP_PLANE_COUNT;
  1458. if (*buf_count < 1)
  1459. *buf_count = 1;
  1460. if (*buf_count > MFC_MAX_BUFFERS)
  1461. *buf_count = MFC_MAX_BUFFERS;
  1462. psize[0] = ctx->enc_dst_buf_size;
  1463. allocators[0] = ctx->dev->alloc_ctx[MFC_BANK1_ALLOC_CTX];
  1464. } else if (vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
  1465. if (ctx->src_fmt)
  1466. *plane_count = ctx->src_fmt->num_planes;
  1467. else
  1468. *plane_count = MFC_ENC_OUT_PLANE_COUNT;
  1469. if (*buf_count < 1)
  1470. *buf_count = 1;
  1471. if (*buf_count > MFC_MAX_BUFFERS)
  1472. *buf_count = MFC_MAX_BUFFERS;
  1473. psize[0] = ctx->luma_size;
  1474. psize[1] = ctx->chroma_size;
  1475. allocators[0] = ctx->dev->alloc_ctx[MFC_BANK2_ALLOC_CTX];
  1476. allocators[1] = ctx->dev->alloc_ctx[MFC_BANK2_ALLOC_CTX];
  1477. } else {
  1478. mfc_err("inavlid queue type: %d\n", vq->type);
  1479. return -EINVAL;
  1480. }
  1481. return 0;
  1482. }
  1483. static void s5p_mfc_unlock(struct vb2_queue *q)
  1484. {
  1485. struct s5p_mfc_ctx *ctx = fh_to_ctx(q->drv_priv);
  1486. struct s5p_mfc_dev *dev = ctx->dev;
  1487. mutex_unlock(&dev->mfc_mutex);
  1488. }
  1489. static void s5p_mfc_lock(struct vb2_queue *q)
  1490. {
  1491. struct s5p_mfc_ctx *ctx = fh_to_ctx(q->drv_priv);
  1492. struct s5p_mfc_dev *dev = ctx->dev;
  1493. mutex_lock(&dev->mfc_mutex);
  1494. }
  1495. static int s5p_mfc_buf_init(struct vb2_buffer *vb)
  1496. {
  1497. struct vb2_queue *vq = vb->vb2_queue;
  1498. struct s5p_mfc_ctx *ctx = fh_to_ctx(vq->drv_priv);
  1499. unsigned int i;
  1500. int ret;
  1501. if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
  1502. ret = check_vb_with_fmt(ctx->dst_fmt, vb);
  1503. if (ret < 0)
  1504. return ret;
  1505. i = vb->v4l2_buf.index;
  1506. ctx->dst_bufs[i].b = vb;
  1507. ctx->dst_bufs[i].cookie.stream =
  1508. vb2_dma_contig_plane_dma_addr(vb, 0);
  1509. ctx->dst_bufs_cnt++;
  1510. } else if (vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
  1511. ret = check_vb_with_fmt(ctx->src_fmt, vb);
  1512. if (ret < 0)
  1513. return ret;
  1514. i = vb->v4l2_buf.index;
  1515. ctx->src_bufs[i].b = vb;
  1516. ctx->src_bufs[i].cookie.raw.luma =
  1517. vb2_dma_contig_plane_dma_addr(vb, 0);
  1518. ctx->src_bufs[i].cookie.raw.chroma =
  1519. vb2_dma_contig_plane_dma_addr(vb, 1);
  1520. ctx->src_bufs_cnt++;
  1521. } else {
  1522. mfc_err("inavlid queue type: %d\n", vq->type);
  1523. return -EINVAL;
  1524. }
  1525. return 0;
  1526. }
  1527. static int s5p_mfc_buf_prepare(struct vb2_buffer *vb)
  1528. {
  1529. struct vb2_queue *vq = vb->vb2_queue;
  1530. struct s5p_mfc_ctx *ctx = fh_to_ctx(vq->drv_priv);
  1531. int ret;
  1532. if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
  1533. ret = check_vb_with_fmt(ctx->dst_fmt, vb);
  1534. if (ret < 0)
  1535. return ret;
  1536. mfc_debug(2, "plane size: %ld, dst size: %d\n",
  1537. vb2_plane_size(vb, 0), ctx->enc_dst_buf_size);
  1538. if (vb2_plane_size(vb, 0) < ctx->enc_dst_buf_size) {
  1539. mfc_err("plane size is too small for capture\n");
  1540. return -EINVAL;
  1541. }
  1542. } else if (vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
  1543. ret = check_vb_with_fmt(ctx->src_fmt, vb);
  1544. if (ret < 0)
  1545. return ret;
  1546. mfc_debug(2, "plane size: %ld, luma size: %d\n",
  1547. vb2_plane_size(vb, 0), ctx->luma_size);
  1548. mfc_debug(2, "plane size: %ld, chroma size: %d\n",
  1549. vb2_plane_size(vb, 1), ctx->chroma_size);
  1550. if (vb2_plane_size(vb, 0) < ctx->luma_size ||
  1551. vb2_plane_size(vb, 1) < ctx->chroma_size) {
  1552. mfc_err("plane size is too small for output\n");
  1553. return -EINVAL;
  1554. }
  1555. } else {
  1556. mfc_err("inavlid queue type: %d\n", vq->type);
  1557. return -EINVAL;
  1558. }
  1559. return 0;
  1560. }
  1561. static int s5p_mfc_start_streaming(struct vb2_queue *q, unsigned int count)
  1562. {
  1563. struct s5p_mfc_ctx *ctx = fh_to_ctx(q->drv_priv);
  1564. struct s5p_mfc_dev *dev = ctx->dev;
  1565. unsigned long flags;
  1566. v4l2_ctrl_handler_setup(&ctx->ctrl_handler);
  1567. /* If context is ready then dev = work->data;schedule it to run */
  1568. if (s5p_mfc_ctx_ready(ctx)) {
  1569. spin_lock_irqsave(&dev->condlock, flags);
  1570. set_bit(ctx->num, &dev->ctx_work_bits);
  1571. spin_unlock_irqrestore(&dev->condlock, flags);
  1572. }
  1573. s5p_mfc_try_run(dev);
  1574. return 0;
  1575. }
  1576. static int s5p_mfc_stop_streaming(struct vb2_queue *q)
  1577. {
  1578. unsigned long flags;
  1579. struct s5p_mfc_ctx *ctx = fh_to_ctx(q->drv_priv);
  1580. struct s5p_mfc_dev *dev = ctx->dev;
  1581. if ((ctx->state == MFCINST_FINISHING ||
  1582. ctx->state == MFCINST_RUNNING) &&
  1583. dev->curr_ctx == ctx->num && dev->hw_lock) {
  1584. ctx->state = MFCINST_ABORT;
  1585. s5p_mfc_wait_for_done_ctx(ctx, S5P_FIMV_R2H_CMD_FRAME_DONE_RET,
  1586. 0);
  1587. }
  1588. ctx->state = MFCINST_FINISHED;
  1589. spin_lock_irqsave(&dev->irqlock, flags);
  1590. if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
  1591. s5p_mfc_cleanup_queue(&ctx->dst_queue, &ctx->vq_dst);
  1592. INIT_LIST_HEAD(&ctx->dst_queue);
  1593. ctx->dst_queue_cnt = 0;
  1594. }
  1595. if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
  1596. cleanup_ref_queue(ctx);
  1597. s5p_mfc_cleanup_queue(&ctx->src_queue, &ctx->vq_src);
  1598. INIT_LIST_HEAD(&ctx->src_queue);
  1599. ctx->src_queue_cnt = 0;
  1600. }
  1601. spin_unlock_irqrestore(&dev->irqlock, flags);
  1602. return 0;
  1603. }
  1604. static void s5p_mfc_buf_queue(struct vb2_buffer *vb)
  1605. {
  1606. struct vb2_queue *vq = vb->vb2_queue;
  1607. struct s5p_mfc_ctx *ctx = fh_to_ctx(vq->drv_priv);
  1608. struct s5p_mfc_dev *dev = ctx->dev;
  1609. unsigned long flags;
  1610. struct s5p_mfc_buf *mfc_buf;
  1611. if (ctx->state == MFCINST_ERROR) {
  1612. vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
  1613. cleanup_ref_queue(ctx);
  1614. return;
  1615. }
  1616. if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
  1617. mfc_buf = &ctx->dst_bufs[vb->v4l2_buf.index];
  1618. mfc_buf->used = 0;
  1619. /* Mark destination as available for use by MFC */
  1620. spin_lock_irqsave(&dev->irqlock, flags);
  1621. list_add_tail(&mfc_buf->list, &ctx->dst_queue);
  1622. ctx->dst_queue_cnt++;
  1623. spin_unlock_irqrestore(&dev->irqlock, flags);
  1624. } else if (vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
  1625. mfc_buf = &ctx->src_bufs[vb->v4l2_buf.index];
  1626. mfc_buf->used = 0;
  1627. spin_lock_irqsave(&dev->irqlock, flags);
  1628. if (vb->v4l2_planes[0].bytesused == 0) {
  1629. mfc_debug(1, "change state to FINISHING\n");
  1630. ctx->state = MFCINST_FINISHING;
  1631. vb2_buffer_done(vb, VB2_BUF_STATE_DONE);
  1632. cleanup_ref_queue(ctx);
  1633. } else {
  1634. list_add_tail(&mfc_buf->list, &ctx->src_queue);
  1635. ctx->src_queue_cnt++;
  1636. }
  1637. spin_unlock_irqrestore(&dev->irqlock, flags);
  1638. } else {
  1639. mfc_err("unsupported buffer type (%d)\n", vq->type);
  1640. }
  1641. if (s5p_mfc_ctx_ready(ctx)) {
  1642. spin_lock_irqsave(&dev->condlock, flags);
  1643. set_bit(ctx->num, &dev->ctx_work_bits);
  1644. spin_unlock_irqrestore(&dev->condlock, flags);
  1645. }
  1646. s5p_mfc_try_run(dev);
  1647. }
  1648. static struct vb2_ops s5p_mfc_enc_qops = {
  1649. .queue_setup = s5p_mfc_queue_setup,
  1650. .wait_prepare = s5p_mfc_unlock,
  1651. .wait_finish = s5p_mfc_lock,
  1652. .buf_init = s5p_mfc_buf_init,
  1653. .buf_prepare = s5p_mfc_buf_prepare,
  1654. .start_streaming = s5p_mfc_start_streaming,
  1655. .stop_streaming = s5p_mfc_stop_streaming,
  1656. .buf_queue = s5p_mfc_buf_queue,
  1657. };
  1658. struct s5p_mfc_codec_ops *get_enc_codec_ops(void)
  1659. {
  1660. return &encoder_codec_ops;
  1661. }
  1662. struct vb2_ops *get_enc_queue_ops(void)
  1663. {
  1664. return &s5p_mfc_enc_qops;
  1665. }
  1666. const struct v4l2_ioctl_ops *get_enc_v4l2_ioctl_ops(void)
  1667. {
  1668. return &s5p_mfc_enc_ioctl_ops;
  1669. }
  1670. #define IS_MFC51_PRIV(x) ((V4L2_CTRL_ID2CLASS(x) == V4L2_CTRL_CLASS_MPEG) \
  1671. && V4L2_CTRL_DRIVER_PRIV(x))
  1672. int s5p_mfc_enc_ctrls_setup(struct s5p_mfc_ctx *ctx)
  1673. {
  1674. struct v4l2_ctrl_config cfg;
  1675. int i;
  1676. v4l2_ctrl_handler_init(&ctx->ctrl_handler, NUM_CTRLS);
  1677. if (ctx->ctrl_handler.error) {
  1678. mfc_err("v4l2_ctrl_handler_init failed\n");
  1679. return ctx->ctrl_handler.error;
  1680. }
  1681. for (i = 0; i < NUM_CTRLS; i++) {
  1682. if (IS_MFC51_PRIV(controls[i].id)) {
  1683. cfg.ops = &s5p_mfc_enc_ctrl_ops;
  1684. cfg.id = controls[i].id;
  1685. cfg.min = controls[i].minimum;
  1686. cfg.max = controls[i].maximum;
  1687. cfg.def = controls[i].default_value;
  1688. cfg.name = controls[i].name;
  1689. cfg.type = controls[i].type;
  1690. cfg.flags = 0;
  1691. if (cfg.type == V4L2_CTRL_TYPE_MENU) {
  1692. cfg.step = 0;
  1693. cfg.menu_skip_mask = cfg.menu_skip_mask;
  1694. cfg.qmenu = mfc51_get_menu(cfg.id);
  1695. } else {
  1696. cfg.step = controls[i].step;
  1697. cfg.menu_skip_mask = 0;
  1698. }
  1699. ctx->ctrls[i] = v4l2_ctrl_new_custom(&ctx->ctrl_handler,
  1700. &cfg, NULL);
  1701. } else {
  1702. if (controls[i].type == V4L2_CTRL_TYPE_MENU) {
  1703. ctx->ctrls[i] = v4l2_ctrl_new_std_menu(
  1704. &ctx->ctrl_handler,
  1705. &s5p_mfc_enc_ctrl_ops, controls[i].id,
  1706. controls[i].maximum, 0,
  1707. controls[i].default_value);
  1708. } else {
  1709. ctx->ctrls[i] = v4l2_ctrl_new_std(
  1710. &ctx->ctrl_handler,
  1711. &s5p_mfc_enc_ctrl_ops, controls[i].id,
  1712. controls[i].minimum,
  1713. controls[i].maximum, controls[i].step,
  1714. controls[i].default_value);
  1715. }
  1716. }
  1717. if (ctx->ctrl_handler.error) {
  1718. mfc_err("Adding control (%d) failed\n", i);
  1719. return ctx->ctrl_handler.error;
  1720. }
  1721. if (controls[i].is_volatile && ctx->ctrls[i])
  1722. ctx->ctrls[i]->flags |= V4L2_CTRL_FLAG_VOLATILE;
  1723. }
  1724. return 0;
  1725. }
  1726. void s5p_mfc_enc_ctrls_delete(struct s5p_mfc_ctx *ctx)
  1727. {
  1728. int i;
  1729. v4l2_ctrl_handler_free(&ctx->ctrl_handler);
  1730. for (i = 0; i < NUM_CTRLS; i++)
  1731. ctx->ctrls[i] = NULL;
  1732. }