s5p_mfc_enc.c 56 KB

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