s5p_mfc_enc.c 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973
  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_PRODUCED) &&
  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. unsigned int enc_pb_count;
  640. if (p->seq_hdr_mode == V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE) {
  641. spin_lock_irqsave(&dev->irqlock, flags);
  642. dst_mb = list_entry(ctx->dst_queue.next,
  643. struct s5p_mfc_buf, list);
  644. list_del(&dst_mb->list);
  645. ctx->dst_queue_cnt--;
  646. vb2_set_plane_payload(dst_mb->b, 0,
  647. s5p_mfc_hw_call(dev->mfc_ops, get_enc_strm_size, dev));
  648. vb2_buffer_done(dst_mb->b, VB2_BUF_STATE_DONE);
  649. spin_unlock_irqrestore(&dev->irqlock, flags);
  650. }
  651. if (!IS_MFCV6(dev)) {
  652. ctx->state = MFCINST_RUNNING;
  653. if (s5p_mfc_ctx_ready(ctx))
  654. set_work_bit_irqsave(ctx);
  655. s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
  656. } else {
  657. enc_pb_count = s5p_mfc_hw_call(dev->mfc_ops,
  658. get_enc_dpb_count, dev);
  659. if (ctx->pb_count < enc_pb_count)
  660. ctx->pb_count = enc_pb_count;
  661. ctx->state = MFCINST_HEAD_PRODUCED;
  662. }
  663. return 0;
  664. }
  665. static int enc_pre_frame_start(struct s5p_mfc_ctx *ctx)
  666. {
  667. struct s5p_mfc_dev *dev = ctx->dev;
  668. struct s5p_mfc_buf *dst_mb;
  669. struct s5p_mfc_buf *src_mb;
  670. unsigned long flags;
  671. unsigned long src_y_addr, src_c_addr, dst_addr;
  672. unsigned int dst_size;
  673. spin_lock_irqsave(&dev->irqlock, flags);
  674. src_mb = list_entry(ctx->src_queue.next, struct s5p_mfc_buf, list);
  675. src_y_addr = vb2_dma_contig_plane_dma_addr(src_mb->b, 0);
  676. src_c_addr = vb2_dma_contig_plane_dma_addr(src_mb->b, 1);
  677. s5p_mfc_hw_call(dev->mfc_ops, set_enc_frame_buffer, ctx, src_y_addr,
  678. src_c_addr);
  679. spin_unlock_irqrestore(&dev->irqlock, flags);
  680. spin_lock_irqsave(&dev->irqlock, flags);
  681. dst_mb = list_entry(ctx->dst_queue.next, struct s5p_mfc_buf, list);
  682. dst_addr = vb2_dma_contig_plane_dma_addr(dst_mb->b, 0);
  683. dst_size = vb2_plane_size(dst_mb->b, 0);
  684. s5p_mfc_hw_call(dev->mfc_ops, set_enc_stream_buffer, ctx, dst_addr,
  685. dst_size);
  686. spin_unlock_irqrestore(&dev->irqlock, flags);
  687. return 0;
  688. }
  689. static int enc_post_frame_start(struct s5p_mfc_ctx *ctx)
  690. {
  691. struct s5p_mfc_dev *dev = ctx->dev;
  692. struct s5p_mfc_buf *mb_entry;
  693. unsigned long enc_y_addr, enc_c_addr;
  694. unsigned long mb_y_addr, mb_c_addr;
  695. int slice_type;
  696. unsigned int strm_size;
  697. unsigned long flags;
  698. slice_type = s5p_mfc_hw_call(dev->mfc_ops, get_enc_slice_type, dev);
  699. strm_size = s5p_mfc_hw_call(dev->mfc_ops, get_enc_strm_size, dev);
  700. mfc_debug(2, "Encoded slice type: %d\n", slice_type);
  701. mfc_debug(2, "Encoded stream size: %d\n", strm_size);
  702. mfc_debug(2, "Display order: %d\n",
  703. mfc_read(dev, S5P_FIMV_ENC_SI_PIC_CNT));
  704. spin_lock_irqsave(&dev->irqlock, flags);
  705. if (slice_type >= 0) {
  706. s5p_mfc_hw_call(dev->mfc_ops, get_enc_frame_buffer, ctx,
  707. &enc_y_addr, &enc_c_addr);
  708. list_for_each_entry(mb_entry, &ctx->src_queue, list) {
  709. mb_y_addr = vb2_dma_contig_plane_dma_addr(mb_entry->b, 0);
  710. mb_c_addr = vb2_dma_contig_plane_dma_addr(mb_entry->b, 1);
  711. if ((enc_y_addr == mb_y_addr) &&
  712. (enc_c_addr == mb_c_addr)) {
  713. list_del(&mb_entry->list);
  714. ctx->src_queue_cnt--;
  715. vb2_buffer_done(mb_entry->b,
  716. VB2_BUF_STATE_DONE);
  717. break;
  718. }
  719. }
  720. list_for_each_entry(mb_entry, &ctx->ref_queue, list) {
  721. mb_y_addr = vb2_dma_contig_plane_dma_addr(mb_entry->b, 0);
  722. mb_c_addr = vb2_dma_contig_plane_dma_addr(mb_entry->b, 1);
  723. if ((enc_y_addr == mb_y_addr) &&
  724. (enc_c_addr == mb_c_addr)) {
  725. list_del(&mb_entry->list);
  726. ctx->ref_queue_cnt--;
  727. vb2_buffer_done(mb_entry->b,
  728. VB2_BUF_STATE_DONE);
  729. break;
  730. }
  731. }
  732. }
  733. if ((ctx->src_queue_cnt > 0) && (ctx->state == MFCINST_RUNNING)) {
  734. mb_entry = list_entry(ctx->src_queue.next, struct s5p_mfc_buf,
  735. list);
  736. if (mb_entry->flags & MFC_BUF_FLAG_USED) {
  737. list_del(&mb_entry->list);
  738. ctx->src_queue_cnt--;
  739. list_add_tail(&mb_entry->list, &ctx->ref_queue);
  740. ctx->ref_queue_cnt++;
  741. }
  742. mfc_debug(2, "enc src count: %d, enc ref count: %d\n",
  743. ctx->src_queue_cnt, ctx->ref_queue_cnt);
  744. }
  745. if (strm_size > 0) {
  746. /* at least one more dest. buffers exist always */
  747. mb_entry = list_entry(ctx->dst_queue.next, struct s5p_mfc_buf,
  748. list);
  749. list_del(&mb_entry->list);
  750. ctx->dst_queue_cnt--;
  751. switch (slice_type) {
  752. case S5P_FIMV_ENC_SI_SLICE_TYPE_I:
  753. mb_entry->b->v4l2_buf.flags |= V4L2_BUF_FLAG_KEYFRAME;
  754. break;
  755. case S5P_FIMV_ENC_SI_SLICE_TYPE_P:
  756. mb_entry->b->v4l2_buf.flags |= V4L2_BUF_FLAG_PFRAME;
  757. break;
  758. case S5P_FIMV_ENC_SI_SLICE_TYPE_B:
  759. mb_entry->b->v4l2_buf.flags |= V4L2_BUF_FLAG_BFRAME;
  760. break;
  761. }
  762. vb2_set_plane_payload(mb_entry->b, 0, strm_size);
  763. vb2_buffer_done(mb_entry->b, VB2_BUF_STATE_DONE);
  764. }
  765. spin_unlock_irqrestore(&dev->irqlock, flags);
  766. if ((ctx->src_queue_cnt == 0) || (ctx->dst_queue_cnt == 0))
  767. clear_work_bit(ctx);
  768. return 0;
  769. }
  770. static struct s5p_mfc_codec_ops encoder_codec_ops = {
  771. .pre_seq_start = enc_pre_seq_start,
  772. .post_seq_start = enc_post_seq_start,
  773. .pre_frame_start = enc_pre_frame_start,
  774. .post_frame_start = enc_post_frame_start,
  775. };
  776. /* Query capabilities of the device */
  777. static int vidioc_querycap(struct file *file, void *priv,
  778. struct v4l2_capability *cap)
  779. {
  780. struct s5p_mfc_dev *dev = video_drvdata(file);
  781. strncpy(cap->driver, dev->plat_dev->name, sizeof(cap->driver) - 1);
  782. strncpy(cap->card, dev->plat_dev->name, sizeof(cap->card) - 1);
  783. cap->bus_info[0] = 0;
  784. cap->version = KERNEL_VERSION(1, 0, 0);
  785. /*
  786. * This is only a mem-to-mem video device. The capture and output
  787. * device capability flags are left only for backward compatibility
  788. * and are scheduled for removal.
  789. */
  790. cap->capabilities = V4L2_CAP_VIDEO_M2M_MPLANE | V4L2_CAP_STREAMING |
  791. V4L2_CAP_VIDEO_CAPTURE_MPLANE |
  792. V4L2_CAP_VIDEO_OUTPUT_MPLANE;
  793. return 0;
  794. }
  795. static int vidioc_enum_fmt(struct v4l2_fmtdesc *f, bool mplane, bool out)
  796. {
  797. struct s5p_mfc_fmt *fmt;
  798. int i, j = 0;
  799. for (i = 0; i < ARRAY_SIZE(formats); ++i) {
  800. if (mplane && formats[i].num_planes == 1)
  801. continue;
  802. else if (!mplane && formats[i].num_planes > 1)
  803. continue;
  804. if (out && formats[i].type != MFC_FMT_RAW)
  805. continue;
  806. else if (!out && formats[i].type != MFC_FMT_ENC)
  807. continue;
  808. if (j == f->index) {
  809. fmt = &formats[i];
  810. strlcpy(f->description, fmt->name,
  811. sizeof(f->description));
  812. f->pixelformat = fmt->fourcc;
  813. return 0;
  814. }
  815. ++j;
  816. }
  817. return -EINVAL;
  818. }
  819. static int vidioc_enum_fmt_vid_cap(struct file *file, void *pirv,
  820. struct v4l2_fmtdesc *f)
  821. {
  822. return vidioc_enum_fmt(f, false, false);
  823. }
  824. static int vidioc_enum_fmt_vid_cap_mplane(struct file *file, void *pirv,
  825. struct v4l2_fmtdesc *f)
  826. {
  827. return vidioc_enum_fmt(f, true, false);
  828. }
  829. static int vidioc_enum_fmt_vid_out(struct file *file, void *prov,
  830. struct v4l2_fmtdesc *f)
  831. {
  832. return vidioc_enum_fmt(f, false, true);
  833. }
  834. static int vidioc_enum_fmt_vid_out_mplane(struct file *file, void *prov,
  835. struct v4l2_fmtdesc *f)
  836. {
  837. return vidioc_enum_fmt(f, true, true);
  838. }
  839. static int vidioc_g_fmt(struct file *file, void *priv, struct v4l2_format *f)
  840. {
  841. struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
  842. struct v4l2_pix_format_mplane *pix_fmt_mp = &f->fmt.pix_mp;
  843. mfc_debug(2, "f->type = %d ctx->state = %d\n", f->type, ctx->state);
  844. if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
  845. /* This is run on output (encoder dest) */
  846. pix_fmt_mp->width = 0;
  847. pix_fmt_mp->height = 0;
  848. pix_fmt_mp->field = V4L2_FIELD_NONE;
  849. pix_fmt_mp->pixelformat = ctx->dst_fmt->fourcc;
  850. pix_fmt_mp->num_planes = ctx->dst_fmt->num_planes;
  851. pix_fmt_mp->plane_fmt[0].bytesperline = ctx->enc_dst_buf_size;
  852. pix_fmt_mp->plane_fmt[0].sizeimage = ctx->enc_dst_buf_size;
  853. } else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
  854. /* This is run on capture (encoder src) */
  855. pix_fmt_mp->width = ctx->img_width;
  856. pix_fmt_mp->height = ctx->img_height;
  857. pix_fmt_mp->field = V4L2_FIELD_NONE;
  858. pix_fmt_mp->pixelformat = ctx->src_fmt->fourcc;
  859. pix_fmt_mp->num_planes = ctx->src_fmt->num_planes;
  860. pix_fmt_mp->plane_fmt[0].bytesperline = ctx->buf_width;
  861. pix_fmt_mp->plane_fmt[0].sizeimage = ctx->luma_size;
  862. pix_fmt_mp->plane_fmt[1].bytesperline = ctx->buf_width;
  863. pix_fmt_mp->plane_fmt[1].sizeimage = ctx->chroma_size;
  864. } else {
  865. mfc_err("invalid buf type\n");
  866. return -EINVAL;
  867. }
  868. return 0;
  869. }
  870. static int vidioc_try_fmt(struct file *file, void *priv, struct v4l2_format *f)
  871. {
  872. struct s5p_mfc_fmt *fmt;
  873. struct v4l2_pix_format_mplane *pix_fmt_mp = &f->fmt.pix_mp;
  874. if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
  875. fmt = find_format(f, MFC_FMT_ENC);
  876. if (!fmt) {
  877. mfc_err("failed to try output format\n");
  878. return -EINVAL;
  879. }
  880. if (pix_fmt_mp->plane_fmt[0].sizeimage == 0) {
  881. mfc_err("must be set encoding output size\n");
  882. return -EINVAL;
  883. }
  884. pix_fmt_mp->plane_fmt[0].bytesperline =
  885. pix_fmt_mp->plane_fmt[0].sizeimage;
  886. } else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
  887. fmt = find_format(f, MFC_FMT_RAW);
  888. if (!fmt) {
  889. mfc_err("failed to try output format\n");
  890. return -EINVAL;
  891. }
  892. if (fmt->num_planes != pix_fmt_mp->num_planes) {
  893. mfc_err("failed to try output format\n");
  894. return -EINVAL;
  895. }
  896. v4l_bound_align_image(&pix_fmt_mp->width, 8, 1920, 1,
  897. &pix_fmt_mp->height, 4, 1080, 1, 0);
  898. } else {
  899. mfc_err("invalid buf type\n");
  900. return -EINVAL;
  901. }
  902. return 0;
  903. }
  904. static int vidioc_s_fmt(struct file *file, void *priv, struct v4l2_format *f)
  905. {
  906. struct s5p_mfc_dev *dev = video_drvdata(file);
  907. struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
  908. struct s5p_mfc_fmt *fmt;
  909. struct v4l2_pix_format_mplane *pix_fmt_mp = &f->fmt.pix_mp;
  910. int ret = 0;
  911. ret = vidioc_try_fmt(file, priv, f);
  912. if (ret)
  913. return ret;
  914. if (ctx->vq_src.streaming || ctx->vq_dst.streaming) {
  915. v4l2_err(&dev->v4l2_dev, "%s queue busy\n", __func__);
  916. ret = -EBUSY;
  917. goto out;
  918. }
  919. if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
  920. fmt = find_format(f, MFC_FMT_ENC);
  921. if (!fmt) {
  922. mfc_err("failed to set capture format\n");
  923. return -EINVAL;
  924. }
  925. ctx->state = MFCINST_INIT;
  926. ctx->dst_fmt = fmt;
  927. ctx->codec_mode = ctx->dst_fmt->codec_mode;
  928. ctx->enc_dst_buf_size = pix_fmt_mp->plane_fmt[0].sizeimage;
  929. pix_fmt_mp->plane_fmt[0].bytesperline = 0;
  930. ctx->dst_bufs_cnt = 0;
  931. ctx->capture_state = QUEUE_FREE;
  932. s5p_mfc_hw_call(dev->mfc_ops, alloc_instance_buffer, ctx);
  933. set_work_bit_irqsave(ctx);
  934. s5p_mfc_clean_ctx_int_flags(ctx);
  935. s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
  936. if (s5p_mfc_wait_for_done_ctx(ctx, \
  937. S5P_MFC_R2H_CMD_OPEN_INSTANCE_RET, 1)) {
  938. /* Error or timeout */
  939. mfc_err("Error getting instance from hardware\n");
  940. s5p_mfc_hw_call(dev->mfc_ops, release_instance_buffer,
  941. ctx);
  942. ret = -EIO;
  943. goto out;
  944. }
  945. mfc_debug(2, "Got instance number: %d\n", ctx->inst_no);
  946. } else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
  947. fmt = find_format(f, MFC_FMT_RAW);
  948. if (!fmt) {
  949. mfc_err("failed to set output format\n");
  950. return -EINVAL;
  951. }
  952. if (!IS_MFCV6(dev) &&
  953. (fmt->fourcc == V4L2_PIX_FMT_NV12MT_16X16)) {
  954. mfc_err("Not supported format.\n");
  955. return -EINVAL;
  956. } else if (IS_MFCV6(dev) &&
  957. (fmt->fourcc == V4L2_PIX_FMT_NV12MT)) {
  958. mfc_err("Not supported format.\n");
  959. return -EINVAL;
  960. }
  961. if (fmt->num_planes != pix_fmt_mp->num_planes) {
  962. mfc_err("failed to set output format\n");
  963. ret = -EINVAL;
  964. goto out;
  965. }
  966. ctx->src_fmt = fmt;
  967. ctx->img_width = pix_fmt_mp->width;
  968. ctx->img_height = pix_fmt_mp->height;
  969. mfc_debug(2, "codec number: %d\n", ctx->src_fmt->codec_mode);
  970. mfc_debug(2, "fmt - w: %d, h: %d, ctx - w: %d, h: %d\n",
  971. pix_fmt_mp->width, pix_fmt_mp->height,
  972. ctx->img_width, ctx->img_height);
  973. s5p_mfc_hw_call(dev->mfc_ops, enc_calc_src_size, ctx);
  974. pix_fmt_mp->plane_fmt[0].sizeimage = ctx->luma_size;
  975. pix_fmt_mp->plane_fmt[0].bytesperline = ctx->buf_width;
  976. pix_fmt_mp->plane_fmt[1].sizeimage = ctx->chroma_size;
  977. pix_fmt_mp->plane_fmt[1].bytesperline = ctx->buf_width;
  978. ctx->src_bufs_cnt = 0;
  979. ctx->output_state = QUEUE_FREE;
  980. } else {
  981. mfc_err("invalid buf type\n");
  982. return -EINVAL;
  983. }
  984. out:
  985. mfc_debug_leave();
  986. return ret;
  987. }
  988. static int vidioc_reqbufs(struct file *file, void *priv,
  989. struct v4l2_requestbuffers *reqbufs)
  990. {
  991. struct s5p_mfc_dev *dev = video_drvdata(file);
  992. struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
  993. int ret = 0;
  994. /* if memory is not mmp or userptr return error */
  995. if ((reqbufs->memory != V4L2_MEMORY_MMAP) &&
  996. (reqbufs->memory != V4L2_MEMORY_USERPTR))
  997. return -EINVAL;
  998. if (reqbufs->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
  999. if (ctx->capture_state != QUEUE_FREE) {
  1000. mfc_err("invalid capture state: %d\n",
  1001. ctx->capture_state);
  1002. return -EINVAL;
  1003. }
  1004. ret = vb2_reqbufs(&ctx->vq_dst, reqbufs);
  1005. if (ret != 0) {
  1006. mfc_err("error in vb2_reqbufs() for E(D)\n");
  1007. return ret;
  1008. }
  1009. ctx->capture_state = QUEUE_BUFS_REQUESTED;
  1010. ret = s5p_mfc_hw_call(ctx->dev->mfc_ops,
  1011. alloc_codec_buffers, ctx);
  1012. if (ret) {
  1013. mfc_err("Failed to allocate encoding buffers\n");
  1014. reqbufs->count = 0;
  1015. ret = vb2_reqbufs(&ctx->vq_dst, reqbufs);
  1016. return -ENOMEM;
  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. if (IS_MFCV6(dev)) {
  1025. /* Check for min encoder buffers */
  1026. if (ctx->pb_count &&
  1027. (reqbufs->count < ctx->pb_count)) {
  1028. reqbufs->count = ctx->pb_count;
  1029. mfc_debug(2, "Minimum %d output buffers needed\n",
  1030. ctx->pb_count);
  1031. } else {
  1032. ctx->pb_count = reqbufs->count;
  1033. }
  1034. }
  1035. ret = vb2_reqbufs(&ctx->vq_src, reqbufs);
  1036. if (ret != 0) {
  1037. mfc_err("error in vb2_reqbufs() for E(S)\n");
  1038. return ret;
  1039. }
  1040. ctx->output_state = QUEUE_BUFS_REQUESTED;
  1041. } else {
  1042. mfc_err("invalid buf type\n");
  1043. return -EINVAL;
  1044. }
  1045. return ret;
  1046. }
  1047. static int vidioc_querybuf(struct file *file, void *priv,
  1048. struct v4l2_buffer *buf)
  1049. {
  1050. struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
  1051. int ret = 0;
  1052. /* if memory is not mmp or userptr return error */
  1053. if ((buf->memory != V4L2_MEMORY_MMAP) &&
  1054. (buf->memory != V4L2_MEMORY_USERPTR))
  1055. return -EINVAL;
  1056. if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
  1057. if (ctx->state != MFCINST_GOT_INST) {
  1058. mfc_err("invalid context state: %d\n", ctx->state);
  1059. return -EINVAL;
  1060. }
  1061. ret = vb2_querybuf(&ctx->vq_dst, buf);
  1062. if (ret != 0) {
  1063. mfc_err("error in vb2_querybuf() for E(D)\n");
  1064. return ret;
  1065. }
  1066. buf->m.planes[0].m.mem_offset += DST_QUEUE_OFF_BASE;
  1067. } else if (buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
  1068. ret = vb2_querybuf(&ctx->vq_src, buf);
  1069. if (ret != 0) {
  1070. mfc_err("error in vb2_querybuf() for E(S)\n");
  1071. return ret;
  1072. }
  1073. } else {
  1074. mfc_err("invalid buf type\n");
  1075. return -EINVAL;
  1076. }
  1077. return ret;
  1078. }
  1079. /* Queue a buffer */
  1080. static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
  1081. {
  1082. struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
  1083. if (ctx->state == MFCINST_ERROR) {
  1084. mfc_err("Call on QBUF after unrecoverable error\n");
  1085. return -EIO;
  1086. }
  1087. if (buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
  1088. if (ctx->state == MFCINST_FINISHING) {
  1089. mfc_err("Call on QBUF after EOS command\n");
  1090. return -EIO;
  1091. }
  1092. return vb2_qbuf(&ctx->vq_src, buf);
  1093. } else if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
  1094. return vb2_qbuf(&ctx->vq_dst, buf);
  1095. }
  1096. return -EINVAL;
  1097. }
  1098. /* Dequeue a buffer */
  1099. static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
  1100. {
  1101. const struct v4l2_event ev = {
  1102. .type = V4L2_EVENT_EOS
  1103. };
  1104. struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
  1105. int ret;
  1106. if (ctx->state == MFCINST_ERROR) {
  1107. mfc_err("Call on DQBUF after unrecoverable error\n");
  1108. return -EIO;
  1109. }
  1110. if (buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
  1111. ret = vb2_dqbuf(&ctx->vq_src, buf, file->f_flags & O_NONBLOCK);
  1112. } else if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
  1113. ret = vb2_dqbuf(&ctx->vq_dst, buf, file->f_flags & O_NONBLOCK);
  1114. if (ret == 0 && ctx->state == MFCINST_FINISHED
  1115. && list_empty(&ctx->vq_dst.done_list))
  1116. v4l2_event_queue_fh(&ctx->fh, &ev);
  1117. } else {
  1118. ret = -EINVAL;
  1119. }
  1120. return ret;
  1121. }
  1122. /* Export DMA buffer */
  1123. static int vidioc_expbuf(struct file *file, void *priv,
  1124. struct v4l2_exportbuffer *eb)
  1125. {
  1126. struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
  1127. if (eb->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
  1128. return vb2_expbuf(&ctx->vq_src, eb);
  1129. if (eb->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
  1130. return vb2_expbuf(&ctx->vq_dst, eb);
  1131. return -EINVAL;
  1132. }
  1133. /* Stream on */
  1134. static int vidioc_streamon(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_streamon(&ctx->vq_src, type);
  1140. else if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
  1141. return vb2_streamon(&ctx->vq_dst, type);
  1142. return -EINVAL;
  1143. }
  1144. /* Stream off, which equals to a pause */
  1145. static int vidioc_streamoff(struct file *file, void *priv,
  1146. enum v4l2_buf_type type)
  1147. {
  1148. struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
  1149. if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
  1150. return vb2_streamoff(&ctx->vq_src, type);
  1151. else if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
  1152. return vb2_streamoff(&ctx->vq_dst, type);
  1153. return -EINVAL;
  1154. }
  1155. static inline int h264_level(enum v4l2_mpeg_video_h264_level lvl)
  1156. {
  1157. static unsigned int t[V4L2_MPEG_VIDEO_H264_LEVEL_4_0 + 1] = {
  1158. /* V4L2_MPEG_VIDEO_H264_LEVEL_1_0 */ 10,
  1159. /* V4L2_MPEG_VIDEO_H264_LEVEL_1B */ 9,
  1160. /* V4L2_MPEG_VIDEO_H264_LEVEL_1_1 */ 11,
  1161. /* V4L2_MPEG_VIDEO_H264_LEVEL_1_2 */ 12,
  1162. /* V4L2_MPEG_VIDEO_H264_LEVEL_1_3 */ 13,
  1163. /* V4L2_MPEG_VIDEO_H264_LEVEL_2_0 */ 20,
  1164. /* V4L2_MPEG_VIDEO_H264_LEVEL_2_1 */ 21,
  1165. /* V4L2_MPEG_VIDEO_H264_LEVEL_2_2 */ 22,
  1166. /* V4L2_MPEG_VIDEO_H264_LEVEL_3_0 */ 30,
  1167. /* V4L2_MPEG_VIDEO_H264_LEVEL_3_1 */ 31,
  1168. /* V4L2_MPEG_VIDEO_H264_LEVEL_3_2 */ 32,
  1169. /* V4L2_MPEG_VIDEO_H264_LEVEL_4_0 */ 40,
  1170. };
  1171. return t[lvl];
  1172. }
  1173. static inline int mpeg4_level(enum v4l2_mpeg_video_mpeg4_level lvl)
  1174. {
  1175. static unsigned int t[V4L2_MPEG_VIDEO_MPEG4_LEVEL_5 + 1] = {
  1176. /* V4L2_MPEG_VIDEO_MPEG4_LEVEL_0 */ 0,
  1177. /* V4L2_MPEG_VIDEO_MPEG4_LEVEL_0B */ 9,
  1178. /* V4L2_MPEG_VIDEO_MPEG4_LEVEL_1 */ 1,
  1179. /* V4L2_MPEG_VIDEO_MPEG4_LEVEL_2 */ 2,
  1180. /* V4L2_MPEG_VIDEO_MPEG4_LEVEL_3 */ 3,
  1181. /* V4L2_MPEG_VIDEO_MPEG4_LEVEL_3B */ 7,
  1182. /* V4L2_MPEG_VIDEO_MPEG4_LEVEL_4 */ 4,
  1183. /* V4L2_MPEG_VIDEO_MPEG4_LEVEL_5 */ 5,
  1184. };
  1185. return t[lvl];
  1186. }
  1187. static inline int vui_sar_idc(enum v4l2_mpeg_video_h264_vui_sar_idc sar)
  1188. {
  1189. static unsigned int t[V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_EXTENDED + 1] = {
  1190. /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_UNSPECIFIED */ 0,
  1191. /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_1x1 */ 1,
  1192. /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_12x11 */ 2,
  1193. /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_10x11 */ 3,
  1194. /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_16x11 */ 4,
  1195. /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_40x33 */ 5,
  1196. /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_24x11 */ 6,
  1197. /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_20x11 */ 7,
  1198. /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_32x11 */ 8,
  1199. /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_80x33 */ 9,
  1200. /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_18x11 */ 10,
  1201. /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_15x11 */ 11,
  1202. /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_64x33 */ 12,
  1203. /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_160x99 */ 13,
  1204. /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_4x3 */ 14,
  1205. /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_3x2 */ 15,
  1206. /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_2x1 */ 16,
  1207. /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_EXTENDED */ 255,
  1208. };
  1209. return t[sar];
  1210. }
  1211. static int s5p_mfc_enc_s_ctrl(struct v4l2_ctrl *ctrl)
  1212. {
  1213. struct s5p_mfc_ctx *ctx = ctrl_to_ctx(ctrl);
  1214. struct s5p_mfc_dev *dev = ctx->dev;
  1215. struct s5p_mfc_enc_params *p = &ctx->enc_params;
  1216. int ret = 0;
  1217. switch (ctrl->id) {
  1218. case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
  1219. p->gop_size = ctrl->val;
  1220. break;
  1221. case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE:
  1222. p->slice_mode = ctrl->val;
  1223. break;
  1224. case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB:
  1225. p->slice_mb = ctrl->val;
  1226. break;
  1227. case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES:
  1228. p->slice_bit = ctrl->val * 8;
  1229. break;
  1230. case V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB:
  1231. p->intra_refresh_mb = ctrl->val;
  1232. break;
  1233. case V4L2_CID_MPEG_MFC51_VIDEO_PADDING:
  1234. p->pad = ctrl->val;
  1235. break;
  1236. case V4L2_CID_MPEG_MFC51_VIDEO_PADDING_YUV:
  1237. p->pad_luma = (ctrl->val >> 16) & 0xff;
  1238. p->pad_cb = (ctrl->val >> 8) & 0xff;
  1239. p->pad_cr = (ctrl->val >> 0) & 0xff;
  1240. break;
  1241. case V4L2_CID_MPEG_VIDEO_FRAME_RC_ENABLE:
  1242. p->rc_frame = ctrl->val;
  1243. break;
  1244. case V4L2_CID_MPEG_VIDEO_BITRATE:
  1245. p->rc_bitrate = ctrl->val;
  1246. break;
  1247. case V4L2_CID_MPEG_MFC51_VIDEO_RC_REACTION_COEFF:
  1248. p->rc_reaction_coeff = ctrl->val;
  1249. break;
  1250. case V4L2_CID_MPEG_MFC51_VIDEO_FORCE_FRAME_TYPE:
  1251. ctx->force_frame_type = ctrl->val;
  1252. break;
  1253. case V4L2_CID_MPEG_VIDEO_VBV_SIZE:
  1254. p->vbv_size = ctrl->val;
  1255. break;
  1256. case V4L2_CID_MPEG_VIDEO_H264_CPB_SIZE:
  1257. p->codec.h264.cpb_size = ctrl->val;
  1258. break;
  1259. case V4L2_CID_MPEG_VIDEO_HEADER_MODE:
  1260. p->seq_hdr_mode = ctrl->val;
  1261. break;
  1262. case V4L2_CID_MPEG_MFC51_VIDEO_FRAME_SKIP_MODE:
  1263. p->frame_skip_mode = ctrl->val;
  1264. break;
  1265. case V4L2_CID_MPEG_MFC51_VIDEO_RC_FIXED_TARGET_BIT:
  1266. p->fixed_target_bit = ctrl->val;
  1267. break;
  1268. case V4L2_CID_MPEG_VIDEO_B_FRAMES:
  1269. p->num_b_frame = ctrl->val;
  1270. break;
  1271. case V4L2_CID_MPEG_VIDEO_H264_PROFILE:
  1272. switch (ctrl->val) {
  1273. case V4L2_MPEG_VIDEO_H264_PROFILE_MAIN:
  1274. p->codec.h264.profile =
  1275. S5P_FIMV_ENC_PROFILE_H264_MAIN;
  1276. break;
  1277. case V4L2_MPEG_VIDEO_H264_PROFILE_HIGH:
  1278. p->codec.h264.profile =
  1279. S5P_FIMV_ENC_PROFILE_H264_HIGH;
  1280. break;
  1281. case V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE:
  1282. p->codec.h264.profile =
  1283. S5P_FIMV_ENC_PROFILE_H264_BASELINE;
  1284. break;
  1285. case V4L2_MPEG_VIDEO_H264_PROFILE_CONSTRAINED_BASELINE:
  1286. if (IS_MFCV6(dev))
  1287. p->codec.h264.profile =
  1288. S5P_FIMV_ENC_PROFILE_H264_CONSTRAINED_BASELINE;
  1289. else
  1290. ret = -EINVAL;
  1291. break;
  1292. default:
  1293. ret = -EINVAL;
  1294. }
  1295. break;
  1296. case V4L2_CID_MPEG_VIDEO_H264_LEVEL:
  1297. p->codec.h264.level_v4l2 = ctrl->val;
  1298. p->codec.h264.level = h264_level(ctrl->val);
  1299. if (p->codec.h264.level < 0) {
  1300. mfc_err("Level number is wrong\n");
  1301. ret = p->codec.h264.level;
  1302. }
  1303. break;
  1304. case V4L2_CID_MPEG_VIDEO_MPEG4_LEVEL:
  1305. p->codec.mpeg4.level_v4l2 = ctrl->val;
  1306. p->codec.mpeg4.level = mpeg4_level(ctrl->val);
  1307. if (p->codec.mpeg4.level < 0) {
  1308. mfc_err("Level number is wrong\n");
  1309. ret = p->codec.mpeg4.level;
  1310. }
  1311. break;
  1312. case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE:
  1313. p->codec.h264.loop_filter_mode = ctrl->val;
  1314. break;
  1315. case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA:
  1316. p->codec.h264.loop_filter_alpha = ctrl->val;
  1317. break;
  1318. case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA:
  1319. p->codec.h264.loop_filter_beta = ctrl->val;
  1320. break;
  1321. case V4L2_CID_MPEG_VIDEO_H264_ENTROPY_MODE:
  1322. p->codec.h264.entropy_mode = ctrl->val;
  1323. break;
  1324. case V4L2_CID_MPEG_MFC51_VIDEO_H264_NUM_REF_PIC_FOR_P:
  1325. p->codec.h264.num_ref_pic_4p = ctrl->val;
  1326. break;
  1327. case V4L2_CID_MPEG_VIDEO_H264_8X8_TRANSFORM:
  1328. p->codec.h264._8x8_transform = ctrl->val;
  1329. break;
  1330. case V4L2_CID_MPEG_VIDEO_MB_RC_ENABLE:
  1331. p->rc_mb = ctrl->val;
  1332. break;
  1333. case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP:
  1334. p->codec.h264.rc_frame_qp = ctrl->val;
  1335. break;
  1336. case V4L2_CID_MPEG_VIDEO_H264_MIN_QP:
  1337. p->codec.h264.rc_min_qp = ctrl->val;
  1338. break;
  1339. case V4L2_CID_MPEG_VIDEO_H264_MAX_QP:
  1340. p->codec.h264.rc_max_qp = ctrl->val;
  1341. break;
  1342. case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP:
  1343. p->codec.h264.rc_p_frame_qp = ctrl->val;
  1344. break;
  1345. case V4L2_CID_MPEG_VIDEO_H264_B_FRAME_QP:
  1346. p->codec.h264.rc_b_frame_qp = ctrl->val;
  1347. break;
  1348. case V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP:
  1349. case V4L2_CID_MPEG_VIDEO_H263_I_FRAME_QP:
  1350. p->codec.mpeg4.rc_frame_qp = ctrl->val;
  1351. break;
  1352. case V4L2_CID_MPEG_VIDEO_MPEG4_MIN_QP:
  1353. case V4L2_CID_MPEG_VIDEO_H263_MIN_QP:
  1354. p->codec.mpeg4.rc_min_qp = ctrl->val;
  1355. break;
  1356. case V4L2_CID_MPEG_VIDEO_MPEG4_MAX_QP:
  1357. case V4L2_CID_MPEG_VIDEO_H263_MAX_QP:
  1358. p->codec.mpeg4.rc_max_qp = ctrl->val;
  1359. break;
  1360. case V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP:
  1361. case V4L2_CID_MPEG_VIDEO_H263_P_FRAME_QP:
  1362. p->codec.mpeg4.rc_p_frame_qp = ctrl->val;
  1363. break;
  1364. case V4L2_CID_MPEG_VIDEO_MPEG4_B_FRAME_QP:
  1365. case V4L2_CID_MPEG_VIDEO_H263_B_FRAME_QP:
  1366. p->codec.mpeg4.rc_b_frame_qp = ctrl->val;
  1367. break;
  1368. case V4L2_CID_MPEG_MFC51_VIDEO_H264_ADAPTIVE_RC_DARK:
  1369. p->codec.h264.rc_mb_dark = ctrl->val;
  1370. break;
  1371. case V4L2_CID_MPEG_MFC51_VIDEO_H264_ADAPTIVE_RC_SMOOTH:
  1372. p->codec.h264.rc_mb_smooth = ctrl->val;
  1373. break;
  1374. case V4L2_CID_MPEG_MFC51_VIDEO_H264_ADAPTIVE_RC_STATIC:
  1375. p->codec.h264.rc_mb_static = ctrl->val;
  1376. break;
  1377. case V4L2_CID_MPEG_MFC51_VIDEO_H264_ADAPTIVE_RC_ACTIVITY:
  1378. p->codec.h264.rc_mb_activity = ctrl->val;
  1379. break;
  1380. case V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_ENABLE:
  1381. p->codec.h264.vui_sar = ctrl->val;
  1382. break;
  1383. case V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_IDC:
  1384. p->codec.h264.vui_sar_idc = vui_sar_idc(ctrl->val);
  1385. break;
  1386. case V4L2_CID_MPEG_VIDEO_H264_VUI_EXT_SAR_WIDTH:
  1387. p->codec.h264.vui_ext_sar_width = ctrl->val;
  1388. break;
  1389. case V4L2_CID_MPEG_VIDEO_H264_VUI_EXT_SAR_HEIGHT:
  1390. p->codec.h264.vui_ext_sar_height = ctrl->val;
  1391. break;
  1392. case V4L2_CID_MPEG_VIDEO_GOP_CLOSURE:
  1393. p->codec.h264.open_gop = !ctrl->val;
  1394. break;
  1395. case V4L2_CID_MPEG_VIDEO_H264_I_PERIOD:
  1396. p->codec.h264.open_gop_size = ctrl->val;
  1397. break;
  1398. case V4L2_CID_MPEG_VIDEO_MPEG4_PROFILE:
  1399. switch (ctrl->val) {
  1400. case V4L2_MPEG_VIDEO_MPEG4_PROFILE_SIMPLE:
  1401. p->codec.mpeg4.profile =
  1402. S5P_FIMV_ENC_PROFILE_MPEG4_SIMPLE;
  1403. break;
  1404. case V4L2_MPEG_VIDEO_MPEG4_PROFILE_ADVANCED_SIMPLE:
  1405. p->codec.mpeg4.profile =
  1406. S5P_FIMV_ENC_PROFILE_MPEG4_ADVANCED_SIMPLE;
  1407. break;
  1408. default:
  1409. ret = -EINVAL;
  1410. }
  1411. break;
  1412. case V4L2_CID_MPEG_VIDEO_MPEG4_QPEL:
  1413. p->codec.mpeg4.quarter_pixel = ctrl->val;
  1414. break;
  1415. default:
  1416. v4l2_err(&dev->v4l2_dev, "Invalid control, id=%d, val=%d\n",
  1417. ctrl->id, ctrl->val);
  1418. ret = -EINVAL;
  1419. }
  1420. return ret;
  1421. }
  1422. static const struct v4l2_ctrl_ops s5p_mfc_enc_ctrl_ops = {
  1423. .s_ctrl = s5p_mfc_enc_s_ctrl,
  1424. };
  1425. static int vidioc_s_parm(struct file *file, void *priv,
  1426. struct v4l2_streamparm *a)
  1427. {
  1428. struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
  1429. if (a->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
  1430. ctx->enc_params.rc_framerate_num =
  1431. a->parm.output.timeperframe.denominator;
  1432. ctx->enc_params.rc_framerate_denom =
  1433. a->parm.output.timeperframe.numerator;
  1434. } else {
  1435. mfc_err("Setting FPS is only possible for the output queue\n");
  1436. return -EINVAL;
  1437. }
  1438. return 0;
  1439. }
  1440. static int vidioc_g_parm(struct file *file, void *priv,
  1441. struct v4l2_streamparm *a)
  1442. {
  1443. struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
  1444. if (a->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
  1445. a->parm.output.timeperframe.denominator =
  1446. ctx->enc_params.rc_framerate_num;
  1447. a->parm.output.timeperframe.numerator =
  1448. ctx->enc_params.rc_framerate_denom;
  1449. } else {
  1450. mfc_err("Setting FPS is only possible for the output queue\n");
  1451. return -EINVAL;
  1452. }
  1453. return 0;
  1454. }
  1455. int vidioc_encoder_cmd(struct file *file, void *priv,
  1456. struct v4l2_encoder_cmd *cmd)
  1457. {
  1458. struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
  1459. struct s5p_mfc_dev *dev = ctx->dev;
  1460. struct s5p_mfc_buf *buf;
  1461. unsigned long flags;
  1462. switch (cmd->cmd) {
  1463. case V4L2_ENC_CMD_STOP:
  1464. if (cmd->flags != 0)
  1465. return -EINVAL;
  1466. if (!ctx->vq_src.streaming)
  1467. return -EINVAL;
  1468. spin_lock_irqsave(&dev->irqlock, flags);
  1469. if (list_empty(&ctx->src_queue)) {
  1470. mfc_debug(2, "EOS: empty src queue, entering finishing state\n");
  1471. ctx->state = MFCINST_FINISHING;
  1472. if (s5p_mfc_ctx_ready(ctx))
  1473. set_work_bit_irqsave(ctx);
  1474. spin_unlock_irqrestore(&dev->irqlock, flags);
  1475. s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
  1476. } else {
  1477. mfc_debug(2, "EOS: marking last buffer of stream\n");
  1478. buf = list_entry(ctx->src_queue.prev,
  1479. struct s5p_mfc_buf, list);
  1480. if (buf->flags & MFC_BUF_FLAG_USED)
  1481. ctx->state = MFCINST_FINISHING;
  1482. else
  1483. buf->flags |= MFC_BUF_FLAG_EOS;
  1484. spin_unlock_irqrestore(&dev->irqlock, flags);
  1485. }
  1486. break;
  1487. default:
  1488. return -EINVAL;
  1489. }
  1490. return 0;
  1491. }
  1492. static int vidioc_subscribe_event(struct v4l2_fh *fh,
  1493. const struct v4l2_event_subscription *sub)
  1494. {
  1495. switch (sub->type) {
  1496. case V4L2_EVENT_EOS:
  1497. return v4l2_event_subscribe(fh, sub, 2, NULL);
  1498. default:
  1499. return -EINVAL;
  1500. }
  1501. }
  1502. static const struct v4l2_ioctl_ops s5p_mfc_enc_ioctl_ops = {
  1503. .vidioc_querycap = vidioc_querycap,
  1504. .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  1505. .vidioc_enum_fmt_vid_cap_mplane = vidioc_enum_fmt_vid_cap_mplane,
  1506. .vidioc_enum_fmt_vid_out = vidioc_enum_fmt_vid_out,
  1507. .vidioc_enum_fmt_vid_out_mplane = vidioc_enum_fmt_vid_out_mplane,
  1508. .vidioc_g_fmt_vid_cap_mplane = vidioc_g_fmt,
  1509. .vidioc_g_fmt_vid_out_mplane = vidioc_g_fmt,
  1510. .vidioc_try_fmt_vid_cap_mplane = vidioc_try_fmt,
  1511. .vidioc_try_fmt_vid_out_mplane = vidioc_try_fmt,
  1512. .vidioc_s_fmt_vid_cap_mplane = vidioc_s_fmt,
  1513. .vidioc_s_fmt_vid_out_mplane = vidioc_s_fmt,
  1514. .vidioc_reqbufs = vidioc_reqbufs,
  1515. .vidioc_querybuf = vidioc_querybuf,
  1516. .vidioc_qbuf = vidioc_qbuf,
  1517. .vidioc_dqbuf = vidioc_dqbuf,
  1518. .vidioc_expbuf = vidioc_expbuf,
  1519. .vidioc_streamon = vidioc_streamon,
  1520. .vidioc_streamoff = vidioc_streamoff,
  1521. .vidioc_s_parm = vidioc_s_parm,
  1522. .vidioc_g_parm = vidioc_g_parm,
  1523. .vidioc_encoder_cmd = vidioc_encoder_cmd,
  1524. .vidioc_subscribe_event = vidioc_subscribe_event,
  1525. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  1526. };
  1527. static int check_vb_with_fmt(struct s5p_mfc_fmt *fmt, struct vb2_buffer *vb)
  1528. {
  1529. int i;
  1530. if (!fmt)
  1531. return -EINVAL;
  1532. if (fmt->num_planes != vb->num_planes) {
  1533. mfc_err("invalid plane number for the format\n");
  1534. return -EINVAL;
  1535. }
  1536. for (i = 0; i < fmt->num_planes; i++) {
  1537. if (!vb2_dma_contig_plane_dma_addr(vb, i)) {
  1538. mfc_err("failed to get plane cookie\n");
  1539. return -EINVAL;
  1540. }
  1541. mfc_debug(2, "index: %d, plane[%d] cookie: 0x%08zx\n",
  1542. vb->v4l2_buf.index, i,
  1543. vb2_dma_contig_plane_dma_addr(vb, i));
  1544. }
  1545. return 0;
  1546. }
  1547. static int s5p_mfc_queue_setup(struct vb2_queue *vq,
  1548. const struct v4l2_format *fmt,
  1549. unsigned int *buf_count, unsigned int *plane_count,
  1550. unsigned int psize[], void *allocators[])
  1551. {
  1552. struct s5p_mfc_ctx *ctx = fh_to_ctx(vq->drv_priv);
  1553. struct s5p_mfc_dev *dev = ctx->dev;
  1554. if (ctx->state != MFCINST_GOT_INST) {
  1555. mfc_err("inavlid state: %d\n", ctx->state);
  1556. return -EINVAL;
  1557. }
  1558. if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
  1559. if (ctx->dst_fmt)
  1560. *plane_count = ctx->dst_fmt->num_planes;
  1561. else
  1562. *plane_count = MFC_ENC_CAP_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->enc_dst_buf_size;
  1568. allocators[0] = ctx->dev->alloc_ctx[MFC_BANK1_ALLOC_CTX];
  1569. } else if (vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
  1570. if (ctx->src_fmt)
  1571. *plane_count = ctx->src_fmt->num_planes;
  1572. else
  1573. *plane_count = MFC_ENC_OUT_PLANE_COUNT;
  1574. if (*buf_count < 1)
  1575. *buf_count = 1;
  1576. if (*buf_count > MFC_MAX_BUFFERS)
  1577. *buf_count = MFC_MAX_BUFFERS;
  1578. psize[0] = ctx->luma_size;
  1579. psize[1] = ctx->chroma_size;
  1580. if (IS_MFCV6(dev)) {
  1581. allocators[0] =
  1582. ctx->dev->alloc_ctx[MFC_BANK1_ALLOC_CTX];
  1583. allocators[1] =
  1584. ctx->dev->alloc_ctx[MFC_BANK1_ALLOC_CTX];
  1585. } else {
  1586. allocators[0] =
  1587. ctx->dev->alloc_ctx[MFC_BANK2_ALLOC_CTX];
  1588. allocators[1] =
  1589. ctx->dev->alloc_ctx[MFC_BANK2_ALLOC_CTX];
  1590. }
  1591. } else {
  1592. mfc_err("inavlid queue type: %d\n", vq->type);
  1593. return -EINVAL;
  1594. }
  1595. return 0;
  1596. }
  1597. static void s5p_mfc_unlock(struct vb2_queue *q)
  1598. {
  1599. struct s5p_mfc_ctx *ctx = fh_to_ctx(q->drv_priv);
  1600. struct s5p_mfc_dev *dev = ctx->dev;
  1601. mutex_unlock(&dev->mfc_mutex);
  1602. }
  1603. static void s5p_mfc_lock(struct vb2_queue *q)
  1604. {
  1605. struct s5p_mfc_ctx *ctx = fh_to_ctx(q->drv_priv);
  1606. struct s5p_mfc_dev *dev = ctx->dev;
  1607. mutex_lock(&dev->mfc_mutex);
  1608. }
  1609. static int s5p_mfc_buf_init(struct vb2_buffer *vb)
  1610. {
  1611. struct vb2_queue *vq = vb->vb2_queue;
  1612. struct s5p_mfc_ctx *ctx = fh_to_ctx(vq->drv_priv);
  1613. unsigned int i;
  1614. int ret;
  1615. if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
  1616. ret = check_vb_with_fmt(ctx->dst_fmt, vb);
  1617. if (ret < 0)
  1618. return ret;
  1619. i = vb->v4l2_buf.index;
  1620. ctx->dst_bufs[i].b = vb;
  1621. ctx->dst_bufs[i].cookie.stream =
  1622. vb2_dma_contig_plane_dma_addr(vb, 0);
  1623. ctx->dst_bufs_cnt++;
  1624. } else if (vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
  1625. ret = check_vb_with_fmt(ctx->src_fmt, vb);
  1626. if (ret < 0)
  1627. return ret;
  1628. i = vb->v4l2_buf.index;
  1629. ctx->src_bufs[i].b = vb;
  1630. ctx->src_bufs[i].cookie.raw.luma =
  1631. vb2_dma_contig_plane_dma_addr(vb, 0);
  1632. ctx->src_bufs[i].cookie.raw.chroma =
  1633. vb2_dma_contig_plane_dma_addr(vb, 1);
  1634. ctx->src_bufs_cnt++;
  1635. } else {
  1636. mfc_err("inavlid queue type: %d\n", vq->type);
  1637. return -EINVAL;
  1638. }
  1639. return 0;
  1640. }
  1641. static int s5p_mfc_buf_prepare(struct vb2_buffer *vb)
  1642. {
  1643. struct vb2_queue *vq = vb->vb2_queue;
  1644. struct s5p_mfc_ctx *ctx = fh_to_ctx(vq->drv_priv);
  1645. int ret;
  1646. if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
  1647. ret = check_vb_with_fmt(ctx->dst_fmt, vb);
  1648. if (ret < 0)
  1649. return ret;
  1650. mfc_debug(2, "plane size: %ld, dst size: %d\n",
  1651. vb2_plane_size(vb, 0), ctx->enc_dst_buf_size);
  1652. if (vb2_plane_size(vb, 0) < ctx->enc_dst_buf_size) {
  1653. mfc_err("plane size is too small for capture\n");
  1654. return -EINVAL;
  1655. }
  1656. } else if (vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
  1657. ret = check_vb_with_fmt(ctx->src_fmt, vb);
  1658. if (ret < 0)
  1659. return ret;
  1660. mfc_debug(2, "plane size: %ld, luma size: %d\n",
  1661. vb2_plane_size(vb, 0), ctx->luma_size);
  1662. mfc_debug(2, "plane size: %ld, chroma size: %d\n",
  1663. vb2_plane_size(vb, 1), ctx->chroma_size);
  1664. if (vb2_plane_size(vb, 0) < ctx->luma_size ||
  1665. vb2_plane_size(vb, 1) < ctx->chroma_size) {
  1666. mfc_err("plane size is too small for output\n");
  1667. return -EINVAL;
  1668. }
  1669. } else {
  1670. mfc_err("inavlid queue type: %d\n", vq->type);
  1671. return -EINVAL;
  1672. }
  1673. return 0;
  1674. }
  1675. static int s5p_mfc_start_streaming(struct vb2_queue *q, unsigned int count)
  1676. {
  1677. struct s5p_mfc_ctx *ctx = fh_to_ctx(q->drv_priv);
  1678. struct s5p_mfc_dev *dev = ctx->dev;
  1679. if (IS_MFCV6(dev) && (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)) {
  1680. if ((ctx->state == MFCINST_GOT_INST) &&
  1681. (dev->curr_ctx == ctx->num) && dev->hw_lock) {
  1682. s5p_mfc_wait_for_done_ctx(ctx,
  1683. S5P_MFC_R2H_CMD_SEQ_DONE_RET,
  1684. 0);
  1685. }
  1686. if (ctx->src_bufs_cnt < ctx->pb_count) {
  1687. mfc_err("Need minimum %d OUTPUT buffers\n",
  1688. ctx->pb_count);
  1689. return -EINVAL;
  1690. }
  1691. }
  1692. /* If context is ready then dev = work->data;schedule it to run */
  1693. if (s5p_mfc_ctx_ready(ctx))
  1694. set_work_bit_irqsave(ctx);
  1695. s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
  1696. return 0;
  1697. }
  1698. static int s5p_mfc_stop_streaming(struct vb2_queue *q)
  1699. {
  1700. unsigned long flags;
  1701. struct s5p_mfc_ctx *ctx = fh_to_ctx(q->drv_priv);
  1702. struct s5p_mfc_dev *dev = ctx->dev;
  1703. if ((ctx->state == MFCINST_FINISHING ||
  1704. ctx->state == MFCINST_RUNNING) &&
  1705. dev->curr_ctx == ctx->num && dev->hw_lock) {
  1706. ctx->state = MFCINST_ABORT;
  1707. s5p_mfc_wait_for_done_ctx(ctx, S5P_MFC_R2H_CMD_FRAME_DONE_RET,
  1708. 0);
  1709. }
  1710. ctx->state = MFCINST_FINISHED;
  1711. spin_lock_irqsave(&dev->irqlock, flags);
  1712. if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
  1713. s5p_mfc_hw_call(dev->mfc_ops, cleanup_queue, &ctx->dst_queue,
  1714. &ctx->vq_dst);
  1715. INIT_LIST_HEAD(&ctx->dst_queue);
  1716. ctx->dst_queue_cnt = 0;
  1717. }
  1718. if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
  1719. cleanup_ref_queue(ctx);
  1720. s5p_mfc_hw_call(dev->mfc_ops, cleanup_queue, &ctx->src_queue,
  1721. &ctx->vq_src);
  1722. INIT_LIST_HEAD(&ctx->src_queue);
  1723. ctx->src_queue_cnt = 0;
  1724. }
  1725. spin_unlock_irqrestore(&dev->irqlock, flags);
  1726. return 0;
  1727. }
  1728. static void s5p_mfc_buf_queue(struct vb2_buffer *vb)
  1729. {
  1730. struct vb2_queue *vq = vb->vb2_queue;
  1731. struct s5p_mfc_ctx *ctx = fh_to_ctx(vq->drv_priv);
  1732. struct s5p_mfc_dev *dev = ctx->dev;
  1733. unsigned long flags;
  1734. struct s5p_mfc_buf *mfc_buf;
  1735. if (ctx->state == MFCINST_ERROR) {
  1736. vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
  1737. cleanup_ref_queue(ctx);
  1738. return;
  1739. }
  1740. if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
  1741. mfc_buf = &ctx->dst_bufs[vb->v4l2_buf.index];
  1742. mfc_buf->flags &= ~MFC_BUF_FLAG_USED;
  1743. /* Mark destination as available for use by MFC */
  1744. spin_lock_irqsave(&dev->irqlock, flags);
  1745. list_add_tail(&mfc_buf->list, &ctx->dst_queue);
  1746. ctx->dst_queue_cnt++;
  1747. spin_unlock_irqrestore(&dev->irqlock, flags);
  1748. } else if (vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
  1749. mfc_buf = &ctx->src_bufs[vb->v4l2_buf.index];
  1750. mfc_buf->flags &= ~MFC_BUF_FLAG_USED;
  1751. spin_lock_irqsave(&dev->irqlock, flags);
  1752. list_add_tail(&mfc_buf->list, &ctx->src_queue);
  1753. ctx->src_queue_cnt++;
  1754. spin_unlock_irqrestore(&dev->irqlock, flags);
  1755. } else {
  1756. mfc_err("unsupported buffer type (%d)\n", vq->type);
  1757. }
  1758. if (s5p_mfc_ctx_ready(ctx))
  1759. set_work_bit_irqsave(ctx);
  1760. s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
  1761. }
  1762. static struct vb2_ops s5p_mfc_enc_qops = {
  1763. .queue_setup = s5p_mfc_queue_setup,
  1764. .wait_prepare = s5p_mfc_unlock,
  1765. .wait_finish = s5p_mfc_lock,
  1766. .buf_init = s5p_mfc_buf_init,
  1767. .buf_prepare = s5p_mfc_buf_prepare,
  1768. .start_streaming = s5p_mfc_start_streaming,
  1769. .stop_streaming = s5p_mfc_stop_streaming,
  1770. .buf_queue = s5p_mfc_buf_queue,
  1771. };
  1772. struct s5p_mfc_codec_ops *get_enc_codec_ops(void)
  1773. {
  1774. return &encoder_codec_ops;
  1775. }
  1776. struct vb2_ops *get_enc_queue_ops(void)
  1777. {
  1778. return &s5p_mfc_enc_qops;
  1779. }
  1780. const struct v4l2_ioctl_ops *get_enc_v4l2_ioctl_ops(void)
  1781. {
  1782. return &s5p_mfc_enc_ioctl_ops;
  1783. }
  1784. #define IS_MFC51_PRIV(x) ((V4L2_CTRL_ID2CLASS(x) == V4L2_CTRL_CLASS_MPEG) \
  1785. && V4L2_CTRL_DRIVER_PRIV(x))
  1786. int s5p_mfc_enc_ctrls_setup(struct s5p_mfc_ctx *ctx)
  1787. {
  1788. struct v4l2_ctrl_config cfg;
  1789. int i;
  1790. v4l2_ctrl_handler_init(&ctx->ctrl_handler, NUM_CTRLS);
  1791. if (ctx->ctrl_handler.error) {
  1792. mfc_err("v4l2_ctrl_handler_init failed\n");
  1793. return ctx->ctrl_handler.error;
  1794. }
  1795. for (i = 0; i < NUM_CTRLS; i++) {
  1796. if (IS_MFC51_PRIV(controls[i].id)) {
  1797. memset(&cfg, 0, sizeof(struct v4l2_ctrl_config));
  1798. cfg.ops = &s5p_mfc_enc_ctrl_ops;
  1799. cfg.id = controls[i].id;
  1800. cfg.min = controls[i].minimum;
  1801. cfg.max = controls[i].maximum;
  1802. cfg.def = controls[i].default_value;
  1803. cfg.name = controls[i].name;
  1804. cfg.type = controls[i].type;
  1805. cfg.flags = 0;
  1806. if (cfg.type == V4L2_CTRL_TYPE_MENU) {
  1807. cfg.step = 0;
  1808. cfg.menu_skip_mask = cfg.menu_skip_mask;
  1809. cfg.qmenu = mfc51_get_menu(cfg.id);
  1810. } else {
  1811. cfg.step = controls[i].step;
  1812. cfg.menu_skip_mask = 0;
  1813. }
  1814. ctx->ctrls[i] = v4l2_ctrl_new_custom(&ctx->ctrl_handler,
  1815. &cfg, NULL);
  1816. } else {
  1817. if (controls[i].type == V4L2_CTRL_TYPE_MENU) {
  1818. ctx->ctrls[i] = v4l2_ctrl_new_std_menu(
  1819. &ctx->ctrl_handler,
  1820. &s5p_mfc_enc_ctrl_ops, controls[i].id,
  1821. controls[i].maximum, 0,
  1822. controls[i].default_value);
  1823. } else {
  1824. ctx->ctrls[i] = v4l2_ctrl_new_std(
  1825. &ctx->ctrl_handler,
  1826. &s5p_mfc_enc_ctrl_ops, controls[i].id,
  1827. controls[i].minimum,
  1828. controls[i].maximum, controls[i].step,
  1829. controls[i].default_value);
  1830. }
  1831. }
  1832. if (ctx->ctrl_handler.error) {
  1833. mfc_err("Adding control (%d) failed\n", i);
  1834. return ctx->ctrl_handler.error;
  1835. }
  1836. if (controls[i].is_volatile && ctx->ctrls[i])
  1837. ctx->ctrls[i]->flags |= V4L2_CTRL_FLAG_VOLATILE;
  1838. }
  1839. v4l2_ctrl_handler_setup(&ctx->ctrl_handler);
  1840. return 0;
  1841. }
  1842. void s5p_mfc_enc_ctrls_delete(struct s5p_mfc_ctx *ctx)
  1843. {
  1844. int i;
  1845. v4l2_ctrl_handler_free(&ctx->ctrl_handler);
  1846. for (i = 0; i < NUM_CTRLS; i++)
  1847. ctx->ctrls[i] = NULL;
  1848. }
  1849. void s5p_mfc_enc_init(struct s5p_mfc_ctx *ctx)
  1850. {
  1851. struct v4l2_format f;
  1852. f.fmt.pix_mp.pixelformat = DEF_SRC_FMT_ENC;
  1853. ctx->src_fmt = find_format(&f, MFC_FMT_RAW);
  1854. f.fmt.pix_mp.pixelformat = DEF_DST_FMT_ENC;
  1855. ctx->dst_fmt = find_format(&f, MFC_FMT_ENC);
  1856. }