s5p_mfc_enc.c 51 KB

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