s5p_mfc_enc.c 51 KB

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