s5p_mfc_enc.c 59 KB

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