s5p_mfc_enc.c 51 KB

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