s5p_mfc_enc.c 53 KB

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