s5p_mfc_enc.c 58 KB

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