s5p_mfc_enc.c 51 KB

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