s5p_mfc_enc.c 53 KB

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