s5p_mfc_enc.c 58 KB

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