s5p_mfc_enc.c 57 KB

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