vdec.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright 2020-2021 NXP
  4. */
  5. #include <linux/init.h>
  6. #include <linux/interconnect.h>
  7. #include <linux/ioctl.h>
  8. #include <linux/list.h>
  9. #include <linux/kernel.h>
  10. #include <linux/module.h>
  11. #include <linux/vmalloc.h>
  12. #include <linux/videodev2.h>
  13. #include <media/v4l2-device.h>
  14. #include <media/v4l2-event.h>
  15. #include <media/v4l2-mem2mem.h>
  16. #include <media/v4l2-ioctl.h>
  17. #include <media/videobuf2-v4l2.h>
  18. #include <media/videobuf2-dma-contig.h>
  19. #include <media/videobuf2-vmalloc.h>
  20. #include "vpu.h"
  21. #include "vpu_defs.h"
  22. #include "vpu_core.h"
  23. #include "vpu_helpers.h"
  24. #include "vpu_v4l2.h"
  25. #include "vpu_cmds.h"
  26. #include "vpu_rpc.h"
  27. #define VDEC_MIN_BUFFER_CAP 8
  28. #define VDEC_MIN_BUFFER_OUT 8
  29. struct vdec_fs_info {
  30. char name[8];
  31. u32 type;
  32. u32 max_count;
  33. u32 req_count;
  34. u32 count;
  35. u32 index;
  36. u32 size;
  37. struct vpu_buffer buffer[32];
  38. u32 tag;
  39. };
  40. struct vdec_t {
  41. u32 seq_hdr_found;
  42. struct vpu_buffer udata;
  43. struct vpu_decode_params params;
  44. struct vpu_dec_codec_info codec_info;
  45. enum vpu_codec_state state;
  46. struct vpu_vb2_buffer *slots[VB2_MAX_FRAME];
  47. u32 req_frame_count;
  48. struct vdec_fs_info mbi;
  49. struct vdec_fs_info dcp;
  50. u32 seq_tag;
  51. bool reset_codec;
  52. bool fixed_fmt;
  53. u32 decoded_frame_count;
  54. u32 display_frame_count;
  55. u32 sequence;
  56. u32 eos_received;
  57. bool is_source_changed;
  58. u32 source_change;
  59. u32 drain;
  60. bool aborting;
  61. };
  62. static const struct vpu_format vdec_formats[] = {
  63. {
  64. .pixfmt = V4L2_PIX_FMT_NV12M_8L128,
  65. .num_planes = 2,
  66. .type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
  67. },
  68. {
  69. .pixfmt = V4L2_PIX_FMT_NV12M_10BE_8L128,
  70. .num_planes = 2,
  71. .type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
  72. },
  73. {
  74. .pixfmt = V4L2_PIX_FMT_H264,
  75. .num_planes = 1,
  76. .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
  77. .flags = V4L2_FMT_FLAG_DYN_RESOLUTION
  78. },
  79. {
  80. .pixfmt = V4L2_PIX_FMT_H264_MVC,
  81. .num_planes = 1,
  82. .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
  83. .flags = V4L2_FMT_FLAG_DYN_RESOLUTION
  84. },
  85. {
  86. .pixfmt = V4L2_PIX_FMT_HEVC,
  87. .num_planes = 1,
  88. .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
  89. .flags = V4L2_FMT_FLAG_DYN_RESOLUTION
  90. },
  91. {
  92. .pixfmt = V4L2_PIX_FMT_VC1_ANNEX_G,
  93. .num_planes = 1,
  94. .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
  95. .flags = V4L2_FMT_FLAG_DYN_RESOLUTION
  96. },
  97. {
  98. .pixfmt = V4L2_PIX_FMT_VC1_ANNEX_L,
  99. .num_planes = 1,
  100. .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
  101. },
  102. {
  103. .pixfmt = V4L2_PIX_FMT_MPEG2,
  104. .num_planes = 1,
  105. .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
  106. .flags = V4L2_FMT_FLAG_DYN_RESOLUTION
  107. },
  108. {
  109. .pixfmt = V4L2_PIX_FMT_MPEG4,
  110. .num_planes = 1,
  111. .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
  112. .flags = V4L2_FMT_FLAG_DYN_RESOLUTION
  113. },
  114. {
  115. .pixfmt = V4L2_PIX_FMT_XVID,
  116. .num_planes = 1,
  117. .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
  118. .flags = V4L2_FMT_FLAG_DYN_RESOLUTION
  119. },
  120. {
  121. .pixfmt = V4L2_PIX_FMT_VP8,
  122. .num_planes = 1,
  123. .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
  124. .flags = V4L2_FMT_FLAG_DYN_RESOLUTION
  125. },
  126. {
  127. .pixfmt = V4L2_PIX_FMT_H263,
  128. .num_planes = 1,
  129. .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
  130. .flags = V4L2_FMT_FLAG_DYN_RESOLUTION
  131. },
  132. {0, 0, 0, 0},
  133. };
  134. static int vdec_op_s_ctrl(struct v4l2_ctrl *ctrl)
  135. {
  136. struct vpu_inst *inst = ctrl_to_inst(ctrl);
  137. struct vdec_t *vdec = inst->priv;
  138. int ret = 0;
  139. vpu_inst_lock(inst);
  140. switch (ctrl->id) {
  141. case V4L2_CID_MPEG_VIDEO_DEC_DISPLAY_DELAY_ENABLE:
  142. vdec->params.display_delay_enable = ctrl->val;
  143. break;
  144. case V4L2_CID_MPEG_VIDEO_DEC_DISPLAY_DELAY:
  145. vdec->params.display_delay = ctrl->val;
  146. break;
  147. default:
  148. ret = -EINVAL;
  149. break;
  150. }
  151. vpu_inst_unlock(inst);
  152. return ret;
  153. }
  154. static const struct v4l2_ctrl_ops vdec_ctrl_ops = {
  155. .s_ctrl = vdec_op_s_ctrl,
  156. .g_volatile_ctrl = vpu_helper_g_volatile_ctrl,
  157. };
  158. static int vdec_ctrl_init(struct vpu_inst *inst)
  159. {
  160. struct v4l2_ctrl *ctrl;
  161. int ret;
  162. ret = v4l2_ctrl_handler_init(&inst->ctrl_handler, 20);
  163. if (ret)
  164. return ret;
  165. v4l2_ctrl_new_std(&inst->ctrl_handler, &vdec_ctrl_ops,
  166. V4L2_CID_MPEG_VIDEO_DEC_DISPLAY_DELAY,
  167. 0, 0, 1, 0);
  168. v4l2_ctrl_new_std(&inst->ctrl_handler, &vdec_ctrl_ops,
  169. V4L2_CID_MPEG_VIDEO_DEC_DISPLAY_DELAY_ENABLE,
  170. 0, 1, 1, 0);
  171. ctrl = v4l2_ctrl_new_std(&inst->ctrl_handler, &vdec_ctrl_ops,
  172. V4L2_CID_MIN_BUFFERS_FOR_CAPTURE, 1, 32, 1, 2);
  173. if (ctrl)
  174. ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE;
  175. ctrl = v4l2_ctrl_new_std(&inst->ctrl_handler, &vdec_ctrl_ops,
  176. V4L2_CID_MIN_BUFFERS_FOR_OUTPUT, 1, 32, 1, 2);
  177. if (ctrl)
  178. ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE;
  179. if (inst->ctrl_handler.error) {
  180. ret = inst->ctrl_handler.error;
  181. v4l2_ctrl_handler_free(&inst->ctrl_handler);
  182. return ret;
  183. }
  184. ret = v4l2_ctrl_handler_setup(&inst->ctrl_handler);
  185. if (ret) {
  186. dev_err(inst->dev, "[%d] setup ctrls fail, ret = %d\n", inst->id, ret);
  187. v4l2_ctrl_handler_free(&inst->ctrl_handler);
  188. return ret;
  189. }
  190. return 0;
  191. }
  192. static void vdec_handle_resolution_change(struct vpu_inst *inst)
  193. {
  194. struct vdec_t *vdec = inst->priv;
  195. struct vb2_queue *q;
  196. if (!inst->fh.m2m_ctx)
  197. return;
  198. if (inst->state != VPU_CODEC_STATE_DYAMIC_RESOLUTION_CHANGE)
  199. return;
  200. if (!vdec->source_change)
  201. return;
  202. q = v4l2_m2m_get_dst_vq(inst->fh.m2m_ctx);
  203. if (!list_empty(&q->done_list))
  204. return;
  205. vdec->source_change--;
  206. vpu_notify_source_change(inst);
  207. vpu_set_last_buffer_dequeued(inst, false);
  208. }
  209. static int vdec_update_state(struct vpu_inst *inst, enum vpu_codec_state state, u32 force)
  210. {
  211. struct vdec_t *vdec = inst->priv;
  212. enum vpu_codec_state pre_state = inst->state;
  213. if (state == VPU_CODEC_STATE_SEEK) {
  214. if (inst->state == VPU_CODEC_STATE_DYAMIC_RESOLUTION_CHANGE)
  215. vdec->state = inst->state;
  216. else
  217. vdec->state = VPU_CODEC_STATE_ACTIVE;
  218. }
  219. if (inst->state != VPU_CODEC_STATE_SEEK || force)
  220. inst->state = state;
  221. else if (state == VPU_CODEC_STATE_DYAMIC_RESOLUTION_CHANGE)
  222. vdec->state = VPU_CODEC_STATE_DYAMIC_RESOLUTION_CHANGE;
  223. if (inst->state != pre_state)
  224. vpu_trace(inst->dev, "[%d] %s -> %s\n", inst->id,
  225. vpu_codec_state_name(pre_state), vpu_codec_state_name(inst->state));
  226. if (inst->state == VPU_CODEC_STATE_DYAMIC_RESOLUTION_CHANGE)
  227. vdec_handle_resolution_change(inst);
  228. return 0;
  229. }
  230. static void vdec_set_last_buffer_dequeued(struct vpu_inst *inst)
  231. {
  232. struct vdec_t *vdec = inst->priv;
  233. if (inst->state == VPU_CODEC_STATE_DYAMIC_RESOLUTION_CHANGE)
  234. return;
  235. if (vdec->eos_received) {
  236. if (!vpu_set_last_buffer_dequeued(inst, true)) {
  237. vdec->eos_received--;
  238. vdec_update_state(inst, VPU_CODEC_STATE_DRAIN, 0);
  239. }
  240. }
  241. }
  242. static int vdec_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
  243. {
  244. strscpy(cap->driver, "amphion-vpu", sizeof(cap->driver));
  245. strscpy(cap->card, "amphion vpu decoder", sizeof(cap->card));
  246. strscpy(cap->bus_info, "platform: amphion-vpu", sizeof(cap->bus_info));
  247. return 0;
  248. }
  249. static int vdec_enum_fmt(struct file *file, void *fh, struct v4l2_fmtdesc *f)
  250. {
  251. struct vpu_inst *inst = to_inst(file);
  252. struct vdec_t *vdec = inst->priv;
  253. const struct vpu_format *fmt;
  254. int ret = -EINVAL;
  255. vpu_inst_lock(inst);
  256. if (!V4L2_TYPE_IS_OUTPUT(f->type) && vdec->fixed_fmt) {
  257. if (f->index == 0) {
  258. f->pixelformat = inst->cap_format.pixfmt;
  259. f->flags = inst->cap_format.flags;
  260. ret = 0;
  261. }
  262. } else {
  263. fmt = vpu_helper_enum_format(inst, f->type, f->index);
  264. memset(f->reserved, 0, sizeof(f->reserved));
  265. if (!fmt)
  266. goto exit;
  267. f->pixelformat = fmt->pixfmt;
  268. f->flags = fmt->flags;
  269. ret = 0;
  270. }
  271. exit:
  272. vpu_inst_unlock(inst);
  273. return ret;
  274. }
  275. static int vdec_g_fmt(struct file *file, void *fh, struct v4l2_format *f)
  276. {
  277. struct vpu_inst *inst = to_inst(file);
  278. struct vdec_t *vdec = inst->priv;
  279. struct v4l2_pix_format_mplane *pixmp = &f->fmt.pix_mp;
  280. struct vpu_format *cur_fmt;
  281. int i;
  282. vpu_inst_lock(inst);
  283. cur_fmt = vpu_get_format(inst, f->type);
  284. pixmp->pixelformat = cur_fmt->pixfmt;
  285. pixmp->num_planes = cur_fmt->num_planes;
  286. pixmp->width = cur_fmt->width;
  287. pixmp->height = cur_fmt->height;
  288. pixmp->field = cur_fmt->field;
  289. pixmp->flags = cur_fmt->flags;
  290. for (i = 0; i < pixmp->num_planes; i++) {
  291. pixmp->plane_fmt[i].bytesperline = cur_fmt->bytesperline[i];
  292. pixmp->plane_fmt[i].sizeimage = cur_fmt->sizeimage[i];
  293. }
  294. f->fmt.pix_mp.colorspace = vdec->codec_info.color_primaries;
  295. f->fmt.pix_mp.xfer_func = vdec->codec_info.transfer_chars;
  296. f->fmt.pix_mp.ycbcr_enc = vdec->codec_info.matrix_coeffs;
  297. f->fmt.pix_mp.quantization = vdec->codec_info.full_range;
  298. vpu_inst_unlock(inst);
  299. return 0;
  300. }
  301. static int vdec_try_fmt(struct file *file, void *fh, struct v4l2_format *f)
  302. {
  303. struct vpu_inst *inst = to_inst(file);
  304. struct vdec_t *vdec = inst->priv;
  305. vpu_try_fmt_common(inst, f);
  306. vpu_inst_lock(inst);
  307. if (vdec->fixed_fmt) {
  308. f->fmt.pix_mp.colorspace = vdec->codec_info.color_primaries;
  309. f->fmt.pix_mp.xfer_func = vdec->codec_info.transfer_chars;
  310. f->fmt.pix_mp.ycbcr_enc = vdec->codec_info.matrix_coeffs;
  311. f->fmt.pix_mp.quantization = vdec->codec_info.full_range;
  312. } else {
  313. f->fmt.pix_mp.colorspace = V4L2_COLORSPACE_DEFAULT;
  314. f->fmt.pix_mp.xfer_func = V4L2_XFER_FUNC_DEFAULT;
  315. f->fmt.pix_mp.ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
  316. f->fmt.pix_mp.quantization = V4L2_QUANTIZATION_DEFAULT;
  317. }
  318. vpu_inst_unlock(inst);
  319. return 0;
  320. }
  321. static int vdec_s_fmt_common(struct vpu_inst *inst, struct v4l2_format *f)
  322. {
  323. struct v4l2_pix_format_mplane *pixmp = &f->fmt.pix_mp;
  324. const struct vpu_format *fmt;
  325. struct vpu_format *cur_fmt;
  326. struct vb2_queue *q;
  327. struct vdec_t *vdec = inst->priv;
  328. int i;
  329. if (!inst->fh.m2m_ctx)
  330. return -EINVAL;
  331. q = v4l2_m2m_get_vq(inst->fh.m2m_ctx, f->type);
  332. if (!q)
  333. return -EINVAL;
  334. if (vb2_is_busy(q))
  335. return -EBUSY;
  336. fmt = vpu_try_fmt_common(inst, f);
  337. if (!fmt)
  338. return -EINVAL;
  339. cur_fmt = vpu_get_format(inst, f->type);
  340. if (V4L2_TYPE_IS_OUTPUT(f->type) && inst->state != VPU_CODEC_STATE_DEINIT) {
  341. if (cur_fmt->pixfmt != fmt->pixfmt) {
  342. vdec->reset_codec = true;
  343. vdec->fixed_fmt = false;
  344. }
  345. }
  346. cur_fmt->pixfmt = fmt->pixfmt;
  347. if (V4L2_TYPE_IS_OUTPUT(f->type) || !vdec->fixed_fmt) {
  348. cur_fmt->num_planes = fmt->num_planes;
  349. cur_fmt->flags = fmt->flags;
  350. cur_fmt->width = pixmp->width;
  351. cur_fmt->height = pixmp->height;
  352. for (i = 0; i < fmt->num_planes; i++) {
  353. cur_fmt->sizeimage[i] = pixmp->plane_fmt[i].sizeimage;
  354. cur_fmt->bytesperline[i] = pixmp->plane_fmt[i].bytesperline;
  355. }
  356. if (pixmp->field != V4L2_FIELD_ANY)
  357. cur_fmt->field = pixmp->field;
  358. } else {
  359. pixmp->num_planes = cur_fmt->num_planes;
  360. pixmp->width = cur_fmt->width;
  361. pixmp->height = cur_fmt->height;
  362. for (i = 0; i < pixmp->num_planes; i++) {
  363. pixmp->plane_fmt[i].bytesperline = cur_fmt->bytesperline[i];
  364. pixmp->plane_fmt[i].sizeimage = cur_fmt->sizeimage[i];
  365. }
  366. pixmp->field = cur_fmt->field;
  367. }
  368. if (!vdec->fixed_fmt) {
  369. if (V4L2_TYPE_IS_OUTPUT(f->type)) {
  370. vdec->params.codec_format = cur_fmt->pixfmt;
  371. vdec->codec_info.color_primaries = f->fmt.pix_mp.colorspace;
  372. vdec->codec_info.transfer_chars = f->fmt.pix_mp.xfer_func;
  373. vdec->codec_info.matrix_coeffs = f->fmt.pix_mp.ycbcr_enc;
  374. vdec->codec_info.full_range = f->fmt.pix_mp.quantization;
  375. } else {
  376. vdec->params.output_format = cur_fmt->pixfmt;
  377. inst->crop.left = 0;
  378. inst->crop.top = 0;
  379. inst->crop.width = cur_fmt->width;
  380. inst->crop.height = cur_fmt->height;
  381. }
  382. }
  383. vpu_trace(inst->dev, "[%d] %c%c%c%c %dx%d\n", inst->id,
  384. f->fmt.pix_mp.pixelformat,
  385. f->fmt.pix_mp.pixelformat >> 8,
  386. f->fmt.pix_mp.pixelformat >> 16,
  387. f->fmt.pix_mp.pixelformat >> 24,
  388. f->fmt.pix_mp.width,
  389. f->fmt.pix_mp.height);
  390. return 0;
  391. }
  392. static int vdec_s_fmt(struct file *file, void *fh, struct v4l2_format *f)
  393. {
  394. struct vpu_inst *inst = to_inst(file);
  395. struct v4l2_pix_format_mplane *pixmp = &f->fmt.pix_mp;
  396. struct vdec_t *vdec = inst->priv;
  397. int ret = 0;
  398. vpu_inst_lock(inst);
  399. ret = vdec_s_fmt_common(inst, f);
  400. if (ret)
  401. goto exit;
  402. if (V4L2_TYPE_IS_OUTPUT(f->type) && !vdec->fixed_fmt) {
  403. struct v4l2_format fc;
  404. memset(&fc, 0, sizeof(fc));
  405. fc.type = inst->cap_format.type;
  406. fc.fmt.pix_mp.pixelformat = inst->cap_format.pixfmt;
  407. fc.fmt.pix_mp.width = pixmp->width;
  408. fc.fmt.pix_mp.height = pixmp->height;
  409. vdec_s_fmt_common(inst, &fc);
  410. }
  411. f->fmt.pix_mp.colorspace = vdec->codec_info.color_primaries;
  412. f->fmt.pix_mp.xfer_func = vdec->codec_info.transfer_chars;
  413. f->fmt.pix_mp.ycbcr_enc = vdec->codec_info.matrix_coeffs;
  414. f->fmt.pix_mp.quantization = vdec->codec_info.full_range;
  415. exit:
  416. vpu_inst_unlock(inst);
  417. return ret;
  418. }
  419. static int vdec_g_selection(struct file *file, void *fh, struct v4l2_selection *s)
  420. {
  421. struct vpu_inst *inst = to_inst(file);
  422. if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE && s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
  423. return -EINVAL;
  424. switch (s->target) {
  425. case V4L2_SEL_TGT_COMPOSE:
  426. case V4L2_SEL_TGT_COMPOSE_DEFAULT:
  427. case V4L2_SEL_TGT_COMPOSE_PADDED:
  428. s->r = inst->crop;
  429. break;
  430. case V4L2_SEL_TGT_COMPOSE_BOUNDS:
  431. s->r.left = 0;
  432. s->r.top = 0;
  433. s->r.width = inst->cap_format.width;
  434. s->r.height = inst->cap_format.height;
  435. break;
  436. default:
  437. return -EINVAL;
  438. }
  439. return 0;
  440. }
  441. static int vdec_drain(struct vpu_inst *inst)
  442. {
  443. struct vdec_t *vdec = inst->priv;
  444. if (!inst->fh.m2m_ctx)
  445. return 0;
  446. if (!vdec->drain)
  447. return 0;
  448. if (!vpu_is_source_empty(inst))
  449. return 0;
  450. if (!vdec->params.frame_count) {
  451. vpu_set_last_buffer_dequeued(inst, true);
  452. return 0;
  453. }
  454. vpu_iface_add_scode(inst, SCODE_PADDING_EOS);
  455. vdec->params.end_flag = 1;
  456. vpu_iface_set_decode_params(inst, &vdec->params, 1);
  457. vdec->drain = 0;
  458. vpu_trace(inst->dev, "[%d] frame_count = %d\n", inst->id, vdec->params.frame_count);
  459. return 0;
  460. }
  461. static int vdec_cmd_start(struct vpu_inst *inst)
  462. {
  463. struct vdec_t *vdec = inst->priv;
  464. switch (inst->state) {
  465. case VPU_CODEC_STATE_STARTED:
  466. case VPU_CODEC_STATE_DRAIN:
  467. case VPU_CODEC_STATE_DYAMIC_RESOLUTION_CHANGE:
  468. vdec_update_state(inst, VPU_CODEC_STATE_ACTIVE, 0);
  469. break;
  470. default:
  471. break;
  472. }
  473. vpu_process_capture_buffer(inst);
  474. if (vdec->eos_received)
  475. vdec_set_last_buffer_dequeued(inst);
  476. return 0;
  477. }
  478. static int vdec_cmd_stop(struct vpu_inst *inst)
  479. {
  480. struct vdec_t *vdec = inst->priv;
  481. vpu_trace(inst->dev, "[%d]\n", inst->id);
  482. if (inst->state == VPU_CODEC_STATE_DEINIT) {
  483. vpu_set_last_buffer_dequeued(inst, true);
  484. } else {
  485. vdec->drain = 1;
  486. vdec_drain(inst);
  487. }
  488. return 0;
  489. }
  490. static int vdec_decoder_cmd(struct file *file, void *fh, struct v4l2_decoder_cmd *cmd)
  491. {
  492. struct vpu_inst *inst = to_inst(file);
  493. int ret;
  494. ret = v4l2_m2m_ioctl_try_decoder_cmd(file, fh, cmd);
  495. if (ret)
  496. return ret;
  497. vpu_inst_lock(inst);
  498. switch (cmd->cmd) {
  499. case V4L2_DEC_CMD_START:
  500. vdec_cmd_start(inst);
  501. break;
  502. case V4L2_DEC_CMD_STOP:
  503. vdec_cmd_stop(inst);
  504. break;
  505. default:
  506. break;
  507. }
  508. vpu_inst_unlock(inst);
  509. return 0;
  510. }
  511. static int vdec_subscribe_event(struct v4l2_fh *fh, const struct v4l2_event_subscription *sub)
  512. {
  513. switch (sub->type) {
  514. case V4L2_EVENT_EOS:
  515. return v4l2_event_subscribe(fh, sub, 0, NULL);
  516. case V4L2_EVENT_SOURCE_CHANGE:
  517. return v4l2_src_change_event_subscribe(fh, sub);
  518. case V4L2_EVENT_CTRL:
  519. return v4l2_ctrl_subscribe_event(fh, sub);
  520. default:
  521. return -EINVAL;
  522. }
  523. return 0;
  524. }
  525. static const struct v4l2_ioctl_ops vdec_ioctl_ops = {
  526. .vidioc_querycap = vdec_querycap,
  527. .vidioc_enum_fmt_vid_cap = vdec_enum_fmt,
  528. .vidioc_enum_fmt_vid_out = vdec_enum_fmt,
  529. .vidioc_g_fmt_vid_cap_mplane = vdec_g_fmt,
  530. .vidioc_g_fmt_vid_out_mplane = vdec_g_fmt,
  531. .vidioc_try_fmt_vid_cap_mplane = vdec_try_fmt,
  532. .vidioc_try_fmt_vid_out_mplane = vdec_try_fmt,
  533. .vidioc_s_fmt_vid_cap_mplane = vdec_s_fmt,
  534. .vidioc_s_fmt_vid_out_mplane = vdec_s_fmt,
  535. .vidioc_g_selection = vdec_g_selection,
  536. .vidioc_try_decoder_cmd = v4l2_m2m_ioctl_try_decoder_cmd,
  537. .vidioc_decoder_cmd = vdec_decoder_cmd,
  538. .vidioc_subscribe_event = vdec_subscribe_event,
  539. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  540. .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
  541. .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
  542. .vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf,
  543. .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
  544. .vidioc_qbuf = v4l2_m2m_ioctl_qbuf,
  545. .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
  546. .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf,
  547. .vidioc_streamon = v4l2_m2m_ioctl_streamon,
  548. .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
  549. };
  550. static bool vdec_check_ready(struct vpu_inst *inst, unsigned int type)
  551. {
  552. struct vdec_t *vdec = inst->priv;
  553. if (V4L2_TYPE_IS_OUTPUT(type))
  554. return true;
  555. if (vdec->req_frame_count)
  556. return true;
  557. return false;
  558. }
  559. static struct vb2_v4l2_buffer *vdec_get_src_buffer(struct vpu_inst *inst, u32 count)
  560. {
  561. if (count > 1)
  562. vpu_skip_frame(inst, count - 1);
  563. return vpu_next_src_buf(inst);
  564. }
  565. static int vdec_frame_decoded(struct vpu_inst *inst, void *arg)
  566. {
  567. struct vdec_t *vdec = inst->priv;
  568. struct vpu_dec_pic_info *info = arg;
  569. struct vpu_vb2_buffer *vpu_buf;
  570. struct vb2_v4l2_buffer *vbuf;
  571. struct vb2_v4l2_buffer *src_buf;
  572. int ret = 0;
  573. if (!info || info->id >= ARRAY_SIZE(vdec->slots))
  574. return -EINVAL;
  575. vpu_inst_lock(inst);
  576. vpu_buf = vdec->slots[info->id];
  577. if (!vpu_buf) {
  578. dev_err(inst->dev, "[%d] decoded invalid frame[%d]\n", inst->id, info->id);
  579. ret = -EINVAL;
  580. goto exit;
  581. }
  582. vbuf = &vpu_buf->m2m_buf.vb;
  583. src_buf = vdec_get_src_buffer(inst, info->consumed_count);
  584. if (src_buf) {
  585. v4l2_m2m_buf_copy_metadata(src_buf, vbuf, true);
  586. if (info->consumed_count) {
  587. v4l2_m2m_src_buf_remove(inst->fh.m2m_ctx);
  588. vpu_set_buffer_state(src_buf, VPU_BUF_STATE_IDLE);
  589. v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
  590. } else {
  591. vpu_set_buffer_state(src_buf, VPU_BUF_STATE_DECODED);
  592. }
  593. }
  594. if (vpu_get_buffer_state(vbuf) == VPU_BUF_STATE_DECODED)
  595. dev_info(inst->dev, "[%d] buf[%d] has been decoded\n", inst->id, info->id);
  596. vpu_set_buffer_state(vbuf, VPU_BUF_STATE_DECODED);
  597. vdec->decoded_frame_count++;
  598. exit:
  599. vpu_inst_unlock(inst);
  600. return ret;
  601. }
  602. static struct vpu_vb2_buffer *vdec_find_buffer(struct vpu_inst *inst, u32 luma)
  603. {
  604. struct vdec_t *vdec = inst->priv;
  605. int i;
  606. for (i = 0; i < ARRAY_SIZE(vdec->slots); i++) {
  607. if (!vdec->slots[i])
  608. continue;
  609. if (luma == vdec->slots[i]->luma)
  610. return vdec->slots[i];
  611. }
  612. return NULL;
  613. }
  614. static void vdec_buf_done(struct vpu_inst *inst, struct vpu_frame_info *frame)
  615. {
  616. struct vdec_t *vdec = inst->priv;
  617. struct vpu_vb2_buffer *vpu_buf;
  618. struct vb2_v4l2_buffer *vbuf;
  619. u32 sequence;
  620. if (!frame)
  621. return;
  622. vpu_inst_lock(inst);
  623. sequence = vdec->sequence++;
  624. vpu_buf = vdec_find_buffer(inst, frame->luma);
  625. vpu_inst_unlock(inst);
  626. if (!vpu_buf) {
  627. dev_err(inst->dev, "[%d] can't find buffer, id = %d, addr = 0x%x\n",
  628. inst->id, frame->id, frame->luma);
  629. return;
  630. }
  631. if (frame->skipped) {
  632. dev_dbg(inst->dev, "[%d] frame skip\n", inst->id);
  633. return;
  634. }
  635. vbuf = &vpu_buf->m2m_buf.vb;
  636. if (vbuf->vb2_buf.index != frame->id)
  637. dev_err(inst->dev, "[%d] buffer id(%d, %d) dismatch\n",
  638. inst->id, vbuf->vb2_buf.index, frame->id);
  639. if (vpu_get_buffer_state(vbuf) != VPU_BUF_STATE_DECODED)
  640. dev_err(inst->dev, "[%d] buffer(%d) ready without decoded\n", inst->id, frame->id);
  641. vpu_set_buffer_state(vbuf, VPU_BUF_STATE_READY);
  642. vb2_set_plane_payload(&vbuf->vb2_buf, 0, inst->cap_format.sizeimage[0]);
  643. vb2_set_plane_payload(&vbuf->vb2_buf, 1, inst->cap_format.sizeimage[1]);
  644. vbuf->field = inst->cap_format.field;
  645. vbuf->sequence = sequence;
  646. dev_dbg(inst->dev, "[%d][OUTPUT TS]%32lld\n", inst->id, vbuf->vb2_buf.timestamp);
  647. v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_DONE);
  648. vpu_inst_lock(inst);
  649. vdec->display_frame_count++;
  650. vpu_inst_unlock(inst);
  651. dev_dbg(inst->dev, "[%d] decoded : %d, display : %d, sequence : %d\n",
  652. inst->id, vdec->decoded_frame_count, vdec->display_frame_count, vdec->sequence);
  653. }
  654. static void vdec_stop_done(struct vpu_inst *inst)
  655. {
  656. struct vdec_t *vdec = inst->priv;
  657. vpu_inst_lock(inst);
  658. vdec_update_state(inst, VPU_CODEC_STATE_DEINIT, 0);
  659. vdec->seq_hdr_found = 0;
  660. vdec->req_frame_count = 0;
  661. vdec->reset_codec = false;
  662. vdec->fixed_fmt = false;
  663. vdec->params.end_flag = 0;
  664. vdec->drain = 0;
  665. vdec->params.frame_count = 0;
  666. vdec->decoded_frame_count = 0;
  667. vdec->display_frame_count = 0;
  668. vdec->sequence = 0;
  669. vdec->eos_received = 0;
  670. vdec->is_source_changed = false;
  671. vdec->source_change = 0;
  672. inst->total_input_count = 0;
  673. vpu_inst_unlock(inst);
  674. }
  675. static bool vdec_check_source_change(struct vpu_inst *inst)
  676. {
  677. struct vdec_t *vdec = inst->priv;
  678. const struct vpu_format *fmt;
  679. int i;
  680. if (!inst->fh.m2m_ctx)
  681. return false;
  682. if (vdec->reset_codec)
  683. return false;
  684. if (!vb2_is_streaming(v4l2_m2m_get_dst_vq(inst->fh.m2m_ctx)))
  685. return true;
  686. fmt = vpu_helper_find_format(inst, inst->cap_format.type, vdec->codec_info.pixfmt);
  687. if (inst->cap_format.pixfmt != vdec->codec_info.pixfmt)
  688. return true;
  689. if (inst->cap_format.width != vdec->codec_info.decoded_width)
  690. return true;
  691. if (inst->cap_format.height != vdec->codec_info.decoded_height)
  692. return true;
  693. if (vpu_get_num_buffers(inst, inst->cap_format.type) < inst->min_buffer_cap)
  694. return true;
  695. if (inst->crop.left != vdec->codec_info.offset_x)
  696. return true;
  697. if (inst->crop.top != vdec->codec_info.offset_y)
  698. return true;
  699. if (inst->crop.width != vdec->codec_info.width)
  700. return true;
  701. if (inst->crop.height != vdec->codec_info.height)
  702. return true;
  703. if (fmt && inst->cap_format.num_planes != fmt->num_planes)
  704. return true;
  705. for (i = 0; i < inst->cap_format.num_planes; i++) {
  706. if (inst->cap_format.bytesperline[i] != vdec->codec_info.bytesperline[i])
  707. return true;
  708. if (inst->cap_format.sizeimage[i] != vdec->codec_info.sizeimage[i])
  709. return true;
  710. }
  711. return false;
  712. }
  713. static void vdec_init_fmt(struct vpu_inst *inst)
  714. {
  715. struct vdec_t *vdec = inst->priv;
  716. const struct vpu_format *fmt;
  717. int i;
  718. fmt = vpu_helper_find_format(inst, inst->cap_format.type, vdec->codec_info.pixfmt);
  719. inst->out_format.width = vdec->codec_info.width;
  720. inst->out_format.height = vdec->codec_info.height;
  721. inst->cap_format.width = vdec->codec_info.decoded_width;
  722. inst->cap_format.height = vdec->codec_info.decoded_height;
  723. inst->cap_format.pixfmt = vdec->codec_info.pixfmt;
  724. if (fmt) {
  725. inst->cap_format.num_planes = fmt->num_planes;
  726. inst->cap_format.flags = fmt->flags;
  727. }
  728. for (i = 0; i < inst->cap_format.num_planes; i++) {
  729. inst->cap_format.bytesperline[i] = vdec->codec_info.bytesperline[i];
  730. inst->cap_format.sizeimage[i] = vdec->codec_info.sizeimage[i];
  731. }
  732. if (vdec->codec_info.progressive)
  733. inst->cap_format.field = V4L2_FIELD_NONE;
  734. else
  735. inst->cap_format.field = V4L2_FIELD_SEQ_TB;
  736. }
  737. static void vdec_init_crop(struct vpu_inst *inst)
  738. {
  739. struct vdec_t *vdec = inst->priv;
  740. inst->crop.left = vdec->codec_info.offset_x;
  741. inst->crop.top = vdec->codec_info.offset_y;
  742. inst->crop.width = vdec->codec_info.width;
  743. inst->crop.height = vdec->codec_info.height;
  744. }
  745. static void vdec_init_mbi(struct vpu_inst *inst)
  746. {
  747. struct vdec_t *vdec = inst->priv;
  748. vdec->mbi.size = vdec->codec_info.mbi_size;
  749. vdec->mbi.max_count = ARRAY_SIZE(vdec->mbi.buffer);
  750. scnprintf(vdec->mbi.name, sizeof(vdec->mbi.name), "mbi");
  751. vdec->mbi.type = MEM_RES_MBI;
  752. vdec->mbi.tag = vdec->seq_tag;
  753. }
  754. static void vdec_init_dcp(struct vpu_inst *inst)
  755. {
  756. struct vdec_t *vdec = inst->priv;
  757. vdec->dcp.size = vdec->codec_info.dcp_size;
  758. vdec->dcp.max_count = ARRAY_SIZE(vdec->dcp.buffer);
  759. scnprintf(vdec->dcp.name, sizeof(vdec->dcp.name), "dcp");
  760. vdec->dcp.type = MEM_RES_DCP;
  761. vdec->dcp.tag = vdec->seq_tag;
  762. }
  763. static void vdec_request_one_fs(struct vdec_fs_info *fs)
  764. {
  765. fs->req_count++;
  766. if (fs->req_count > fs->max_count)
  767. fs->req_count = fs->max_count;
  768. }
  769. static int vdec_alloc_fs_buffer(struct vpu_inst *inst, struct vdec_fs_info *fs)
  770. {
  771. struct vpu_buffer *buffer;
  772. if (!fs->size)
  773. return -EINVAL;
  774. if (fs->count >= fs->req_count)
  775. return -EINVAL;
  776. buffer = &fs->buffer[fs->count];
  777. if (buffer->virt && buffer->length >= fs->size)
  778. return 0;
  779. vpu_free_dma(buffer);
  780. buffer->length = fs->size;
  781. return vpu_alloc_dma(inst->core, buffer);
  782. }
  783. static void vdec_alloc_fs(struct vpu_inst *inst, struct vdec_fs_info *fs)
  784. {
  785. int ret;
  786. while (fs->count < fs->req_count) {
  787. ret = vdec_alloc_fs_buffer(inst, fs);
  788. if (ret)
  789. break;
  790. fs->count++;
  791. }
  792. }
  793. static void vdec_clear_fs(struct vdec_fs_info *fs)
  794. {
  795. u32 i;
  796. if (!fs)
  797. return;
  798. for (i = 0; i < ARRAY_SIZE(fs->buffer); i++)
  799. vpu_free_dma(&fs->buffer[i]);
  800. memset(fs, 0, sizeof(*fs));
  801. }
  802. static int vdec_response_fs(struct vpu_inst *inst, struct vdec_fs_info *fs)
  803. {
  804. struct vpu_fs_info info;
  805. int ret;
  806. if (fs->index >= fs->count)
  807. return 0;
  808. memset(&info, 0, sizeof(info));
  809. info.id = fs->index;
  810. info.type = fs->type;
  811. info.tag = fs->tag;
  812. info.luma_addr = fs->buffer[fs->index].phys;
  813. info.luma_size = fs->buffer[fs->index].length;
  814. ret = vpu_session_alloc_fs(inst, &info);
  815. if (ret)
  816. return ret;
  817. fs->index++;
  818. return 0;
  819. }
  820. static int vdec_response_frame_abnormal(struct vpu_inst *inst)
  821. {
  822. struct vdec_t *vdec = inst->priv;
  823. struct vpu_fs_info info;
  824. int ret;
  825. if (!vdec->req_frame_count)
  826. return 0;
  827. memset(&info, 0, sizeof(info));
  828. info.type = MEM_RES_FRAME;
  829. info.tag = vdec->seq_tag + 0xf0;
  830. ret = vpu_session_alloc_fs(inst, &info);
  831. if (ret)
  832. return ret;
  833. vdec->req_frame_count--;
  834. return 0;
  835. }
  836. static int vdec_response_frame(struct vpu_inst *inst, struct vb2_v4l2_buffer *vbuf)
  837. {
  838. struct vdec_t *vdec = inst->priv;
  839. struct vpu_vb2_buffer *vpu_buf;
  840. struct vpu_fs_info info;
  841. int ret;
  842. if (inst->state != VPU_CODEC_STATE_ACTIVE)
  843. return -EINVAL;
  844. if (vdec->aborting)
  845. return -EINVAL;
  846. if (!vdec->req_frame_count)
  847. return -EINVAL;
  848. if (!vbuf)
  849. return -EINVAL;
  850. if (vdec->slots[vbuf->vb2_buf.index]) {
  851. dev_err(inst->dev, "[%d] repeat alloc fs %d\n",
  852. inst->id, vbuf->vb2_buf.index);
  853. return -EINVAL;
  854. }
  855. dev_dbg(inst->dev, "[%d] state = %s, alloc fs %d, tag = 0x%x\n",
  856. inst->id, vpu_codec_state_name(inst->state), vbuf->vb2_buf.index, vdec->seq_tag);
  857. vpu_buf = to_vpu_vb2_buffer(vbuf);
  858. memset(&info, 0, sizeof(info));
  859. info.id = vbuf->vb2_buf.index;
  860. info.type = MEM_RES_FRAME;
  861. info.tag = vdec->seq_tag;
  862. info.luma_addr = vpu_get_vb_phy_addr(&vbuf->vb2_buf, 0);
  863. info.luma_size = inst->cap_format.sizeimage[0];
  864. info.chroma_addr = vpu_get_vb_phy_addr(&vbuf->vb2_buf, 1);
  865. info.chromau_size = inst->cap_format.sizeimage[1];
  866. info.bytesperline = inst->cap_format.bytesperline[0];
  867. ret = vpu_session_alloc_fs(inst, &info);
  868. if (ret)
  869. return ret;
  870. vpu_buf->tag = info.tag;
  871. vpu_buf->luma = info.luma_addr;
  872. vpu_buf->chroma_u = info.chromau_size;
  873. vpu_buf->chroma_v = 0;
  874. vpu_set_buffer_state(vbuf, VPU_BUF_STATE_INUSE);
  875. vdec->slots[info.id] = vpu_buf;
  876. vdec->req_frame_count--;
  877. return 0;
  878. }
  879. static void vdec_response_fs_request(struct vpu_inst *inst, bool force)
  880. {
  881. struct vdec_t *vdec = inst->priv;
  882. int i;
  883. int ret;
  884. if (force) {
  885. for (i = vdec->req_frame_count; i > 0; i--)
  886. vdec_response_frame_abnormal(inst);
  887. return;
  888. }
  889. for (i = vdec->req_frame_count; i > 0; i--) {
  890. ret = vpu_process_capture_buffer(inst);
  891. if (ret)
  892. break;
  893. if (vdec->eos_received)
  894. break;
  895. }
  896. for (i = vdec->mbi.index; i < vdec->mbi.count; i++) {
  897. if (vdec_response_fs(inst, &vdec->mbi))
  898. break;
  899. if (vdec->eos_received)
  900. break;
  901. }
  902. for (i = vdec->dcp.index; i < vdec->dcp.count; i++) {
  903. if (vdec_response_fs(inst, &vdec->dcp))
  904. break;
  905. if (vdec->eos_received)
  906. break;
  907. }
  908. }
  909. static void vdec_response_fs_release(struct vpu_inst *inst, u32 id, u32 tag)
  910. {
  911. struct vpu_fs_info info;
  912. memset(&info, 0, sizeof(info));
  913. info.id = id;
  914. info.tag = tag;
  915. vpu_session_release_fs(inst, &info);
  916. }
  917. static void vdec_recycle_buffer(struct vpu_inst *inst, struct vb2_v4l2_buffer *vbuf)
  918. {
  919. if (!inst->fh.m2m_ctx)
  920. return;
  921. if (vbuf->vb2_buf.state != VB2_BUF_STATE_ACTIVE)
  922. return;
  923. if (vpu_find_buf_by_idx(inst, vbuf->vb2_buf.type, vbuf->vb2_buf.index))
  924. return;
  925. v4l2_m2m_buf_queue(inst->fh.m2m_ctx, vbuf);
  926. }
  927. static void vdec_clear_slots(struct vpu_inst *inst)
  928. {
  929. struct vdec_t *vdec = inst->priv;
  930. struct vpu_vb2_buffer *vpu_buf;
  931. struct vb2_v4l2_buffer *vbuf;
  932. int i;
  933. for (i = 0; i < ARRAY_SIZE(vdec->slots); i++) {
  934. if (!vdec->slots[i])
  935. continue;
  936. vpu_buf = vdec->slots[i];
  937. vbuf = &vpu_buf->m2m_buf.vb;
  938. vpu_trace(inst->dev, "clear slot %d\n", i);
  939. vdec_response_fs_release(inst, i, vpu_buf->tag);
  940. vdec_recycle_buffer(inst, vbuf);
  941. vdec->slots[i]->state = VPU_BUF_STATE_IDLE;
  942. vdec->slots[i] = NULL;
  943. }
  944. }
  945. static void vdec_event_seq_hdr(struct vpu_inst *inst, struct vpu_dec_codec_info *hdr)
  946. {
  947. struct vdec_t *vdec = inst->priv;
  948. vpu_inst_lock(inst);
  949. memcpy(&vdec->codec_info, hdr, sizeof(vdec->codec_info));
  950. vpu_trace(inst->dev, "[%d] %d x %d, crop : (%d, %d) %d x %d, %d, %d\n",
  951. inst->id,
  952. vdec->codec_info.decoded_width,
  953. vdec->codec_info.decoded_height,
  954. vdec->codec_info.offset_x,
  955. vdec->codec_info.offset_y,
  956. vdec->codec_info.width,
  957. vdec->codec_info.height,
  958. hdr->num_ref_frms,
  959. hdr->num_dpb_frms);
  960. inst->min_buffer_cap = hdr->num_ref_frms + hdr->num_dpb_frms;
  961. vdec->is_source_changed = vdec_check_source_change(inst);
  962. vdec_init_fmt(inst);
  963. vdec_init_crop(inst);
  964. vdec_init_mbi(inst);
  965. vdec_init_dcp(inst);
  966. if (!vdec->seq_hdr_found) {
  967. vdec->seq_tag = vdec->codec_info.tag;
  968. if (vdec->is_source_changed) {
  969. vdec_update_state(inst, VPU_CODEC_STATE_DYAMIC_RESOLUTION_CHANGE, 0);
  970. vdec->source_change++;
  971. vdec_handle_resolution_change(inst);
  972. vdec->is_source_changed = false;
  973. }
  974. }
  975. if (vdec->seq_tag != vdec->codec_info.tag) {
  976. vdec_response_fs_request(inst, true);
  977. vpu_trace(inst->dev, "[%d] seq tag change: %d -> %d\n",
  978. inst->id, vdec->seq_tag, vdec->codec_info.tag);
  979. }
  980. vdec->seq_hdr_found++;
  981. vdec->fixed_fmt = true;
  982. vpu_inst_unlock(inst);
  983. }
  984. static void vdec_event_resolution_change(struct vpu_inst *inst)
  985. {
  986. struct vdec_t *vdec = inst->priv;
  987. vpu_trace(inst->dev, "[%d]\n", inst->id);
  988. vpu_inst_lock(inst);
  989. vdec->seq_tag = vdec->codec_info.tag;
  990. vdec_clear_fs(&vdec->mbi);
  991. vdec_clear_fs(&vdec->dcp);
  992. vdec_clear_slots(inst);
  993. vdec_init_mbi(inst);
  994. vdec_init_dcp(inst);
  995. if (vdec->is_source_changed) {
  996. vdec_update_state(inst, VPU_CODEC_STATE_DYAMIC_RESOLUTION_CHANGE, 0);
  997. vdec->source_change++;
  998. vdec_handle_resolution_change(inst);
  999. vdec->is_source_changed = false;
  1000. }
  1001. vpu_inst_unlock(inst);
  1002. }
  1003. static void vdec_event_req_fs(struct vpu_inst *inst, struct vpu_fs_info *fs)
  1004. {
  1005. struct vdec_t *vdec = inst->priv;
  1006. if (!fs)
  1007. return;
  1008. vpu_inst_lock(inst);
  1009. switch (fs->type) {
  1010. case MEM_RES_FRAME:
  1011. vdec->req_frame_count++;
  1012. break;
  1013. case MEM_RES_MBI:
  1014. vdec_request_one_fs(&vdec->mbi);
  1015. break;
  1016. case MEM_RES_DCP:
  1017. vdec_request_one_fs(&vdec->dcp);
  1018. break;
  1019. default:
  1020. break;
  1021. }
  1022. vdec_alloc_fs(inst, &vdec->mbi);
  1023. vdec_alloc_fs(inst, &vdec->dcp);
  1024. vdec_response_fs_request(inst, false);
  1025. vpu_inst_unlock(inst);
  1026. }
  1027. static void vdec_evnet_rel_fs(struct vpu_inst *inst, struct vpu_fs_info *fs)
  1028. {
  1029. struct vdec_t *vdec = inst->priv;
  1030. struct vpu_vb2_buffer *vpu_buf;
  1031. struct vb2_v4l2_buffer *vbuf;
  1032. if (!fs || fs->id >= ARRAY_SIZE(vdec->slots))
  1033. return;
  1034. if (fs->type != MEM_RES_FRAME)
  1035. return;
  1036. if (fs->id >= vpu_get_num_buffers(inst, inst->cap_format.type)) {
  1037. dev_err(inst->dev, "[%d] invalid fs(%d) to release\n", inst->id, fs->id);
  1038. return;
  1039. }
  1040. vpu_inst_lock(inst);
  1041. vpu_buf = vdec->slots[fs->id];
  1042. vdec->slots[fs->id] = NULL;
  1043. if (!vpu_buf) {
  1044. dev_dbg(inst->dev, "[%d] fs[%d] has bee released\n", inst->id, fs->id);
  1045. goto exit;
  1046. }
  1047. vbuf = &vpu_buf->m2m_buf.vb;
  1048. if (vpu_get_buffer_state(vbuf) == VPU_BUF_STATE_DECODED) {
  1049. dev_dbg(inst->dev, "[%d] frame skip\n", inst->id);
  1050. vdec->sequence++;
  1051. }
  1052. vdec_response_fs_release(inst, fs->id, vpu_buf->tag);
  1053. if (vpu_get_buffer_state(vbuf) != VPU_BUF_STATE_READY)
  1054. vdec_recycle_buffer(inst, vbuf);
  1055. vpu_set_buffer_state(vbuf, VPU_BUF_STATE_IDLE);
  1056. vpu_process_capture_buffer(inst);
  1057. exit:
  1058. vpu_inst_unlock(inst);
  1059. }
  1060. static void vdec_event_eos(struct vpu_inst *inst)
  1061. {
  1062. struct vdec_t *vdec = inst->priv;
  1063. vpu_trace(inst->dev, "[%d] input : %d, decoded : %d, display : %d, sequence : %d\n",
  1064. inst->id,
  1065. vdec->params.frame_count,
  1066. vdec->decoded_frame_count,
  1067. vdec->display_frame_count,
  1068. vdec->sequence);
  1069. vpu_inst_lock(inst);
  1070. vdec->eos_received++;
  1071. vdec->fixed_fmt = false;
  1072. inst->min_buffer_cap = VDEC_MIN_BUFFER_CAP;
  1073. vdec_set_last_buffer_dequeued(inst);
  1074. vpu_inst_unlock(inst);
  1075. }
  1076. static void vdec_event_notify(struct vpu_inst *inst, u32 event, void *data)
  1077. {
  1078. switch (event) {
  1079. case VPU_MSG_ID_SEQ_HDR_FOUND:
  1080. vdec_event_seq_hdr(inst, data);
  1081. break;
  1082. case VPU_MSG_ID_RES_CHANGE:
  1083. vdec_event_resolution_change(inst);
  1084. break;
  1085. case VPU_MSG_ID_FRAME_REQ:
  1086. vdec_event_req_fs(inst, data);
  1087. break;
  1088. case VPU_MSG_ID_FRAME_RELEASE:
  1089. vdec_evnet_rel_fs(inst, data);
  1090. break;
  1091. case VPU_MSG_ID_PIC_EOS:
  1092. vdec_event_eos(inst);
  1093. break;
  1094. default:
  1095. break;
  1096. }
  1097. }
  1098. static int vdec_process_output(struct vpu_inst *inst, struct vb2_buffer *vb)
  1099. {
  1100. struct vdec_t *vdec = inst->priv;
  1101. struct vb2_v4l2_buffer *vbuf;
  1102. struct vpu_rpc_buffer_desc desc;
  1103. u32 free_space;
  1104. int ret;
  1105. vbuf = to_vb2_v4l2_buffer(vb);
  1106. dev_dbg(inst->dev, "[%d] dec output [%d] %d : %ld\n",
  1107. inst->id, vbuf->sequence, vb->index, vb2_get_plane_payload(vb, 0));
  1108. if (inst->state == VPU_CODEC_STATE_DEINIT)
  1109. return -EINVAL;
  1110. if (vdec->reset_codec)
  1111. return -EINVAL;
  1112. if (inst->state == VPU_CODEC_STATE_STARTED)
  1113. vdec_update_state(inst, VPU_CODEC_STATE_ACTIVE, 0);
  1114. ret = vpu_iface_get_stream_buffer_desc(inst, &desc);
  1115. if (ret)
  1116. return ret;
  1117. free_space = vpu_helper_get_free_space(inst);
  1118. if (free_space < vb2_get_plane_payload(vb, 0) + 0x40000)
  1119. return -ENOMEM;
  1120. vpu_set_buffer_state(vbuf, VPU_BUF_STATE_INUSE);
  1121. ret = vpu_iface_input_frame(inst, vb);
  1122. if (ret < 0)
  1123. return -ENOMEM;
  1124. dev_dbg(inst->dev, "[%d][INPUT TS]%32lld\n", inst->id, vb->timestamp);
  1125. vdec->params.frame_count++;
  1126. if (vdec->drain)
  1127. vdec_drain(inst);
  1128. return 0;
  1129. }
  1130. static int vdec_process_capture(struct vpu_inst *inst, struct vb2_buffer *vb)
  1131. {
  1132. struct vdec_t *vdec = inst->priv;
  1133. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  1134. int ret;
  1135. if (inst->state == VPU_CODEC_STATE_DYAMIC_RESOLUTION_CHANGE)
  1136. return -EINVAL;
  1137. if (vdec->reset_codec)
  1138. return -EINVAL;
  1139. ret = vdec_response_frame(inst, vbuf);
  1140. if (ret)
  1141. return ret;
  1142. v4l2_m2m_dst_buf_remove_by_buf(inst->fh.m2m_ctx, vbuf);
  1143. return 0;
  1144. }
  1145. static void vdec_on_queue_empty(struct vpu_inst *inst, u32 type)
  1146. {
  1147. struct vdec_t *vdec = inst->priv;
  1148. if (V4L2_TYPE_IS_OUTPUT(type))
  1149. return;
  1150. vdec_handle_resolution_change(inst);
  1151. if (vdec->eos_received)
  1152. vdec_set_last_buffer_dequeued(inst);
  1153. }
  1154. static void vdec_abort(struct vpu_inst *inst)
  1155. {
  1156. struct vdec_t *vdec = inst->priv;
  1157. struct vpu_rpc_buffer_desc desc;
  1158. int ret;
  1159. vpu_trace(inst->dev, "[%d] state = %s\n", inst->id, vpu_codec_state_name(inst->state));
  1160. vdec->aborting = true;
  1161. vpu_iface_add_scode(inst, SCODE_PADDING_ABORT);
  1162. vdec->params.end_flag = 1;
  1163. vpu_iface_set_decode_params(inst, &vdec->params, 1);
  1164. vpu_session_abort(inst);
  1165. ret = vpu_iface_get_stream_buffer_desc(inst, &desc);
  1166. if (!ret)
  1167. vpu_iface_update_stream_buffer(inst, desc.rptr, 1);
  1168. vpu_session_rst_buf(inst);
  1169. vpu_trace(inst->dev, "[%d] input : %d, decoded : %d, display : %d, sequence : %d\n",
  1170. inst->id,
  1171. vdec->params.frame_count,
  1172. vdec->decoded_frame_count,
  1173. vdec->display_frame_count,
  1174. vdec->sequence);
  1175. if (!vdec->seq_hdr_found)
  1176. vdec->reset_codec = true;
  1177. vdec->params.end_flag = 0;
  1178. vdec->drain = 0;
  1179. vdec->params.frame_count = 0;
  1180. vdec->decoded_frame_count = 0;
  1181. vdec->display_frame_count = 0;
  1182. vdec->sequence = 0;
  1183. vdec->aborting = false;
  1184. inst->extra_size = 0;
  1185. }
  1186. static void vdec_stop(struct vpu_inst *inst, bool free)
  1187. {
  1188. struct vdec_t *vdec = inst->priv;
  1189. vdec_clear_slots(inst);
  1190. if (inst->state != VPU_CODEC_STATE_DEINIT)
  1191. vpu_session_stop(inst);
  1192. vdec_clear_fs(&vdec->mbi);
  1193. vdec_clear_fs(&vdec->dcp);
  1194. if (free) {
  1195. vpu_free_dma(&vdec->udata);
  1196. vpu_free_dma(&inst->stream_buffer);
  1197. }
  1198. vdec_update_state(inst, VPU_CODEC_STATE_DEINIT, 1);
  1199. vdec->reset_codec = false;
  1200. }
  1201. static void vdec_release(struct vpu_inst *inst)
  1202. {
  1203. if (inst->id != VPU_INST_NULL_ID)
  1204. vpu_trace(inst->dev, "[%d]\n", inst->id);
  1205. vdec_stop(inst, true);
  1206. }
  1207. static void vdec_cleanup(struct vpu_inst *inst)
  1208. {
  1209. struct vdec_t *vdec;
  1210. if (!inst)
  1211. return;
  1212. vdec = inst->priv;
  1213. vfree(vdec);
  1214. inst->priv = NULL;
  1215. vfree(inst);
  1216. }
  1217. static void vdec_init_params(struct vdec_t *vdec)
  1218. {
  1219. vdec->params.frame_count = 0;
  1220. vdec->params.end_flag = 0;
  1221. }
  1222. static int vdec_start(struct vpu_inst *inst)
  1223. {
  1224. struct vdec_t *vdec = inst->priv;
  1225. int stream_buffer_size;
  1226. int ret;
  1227. if (inst->state != VPU_CODEC_STATE_DEINIT)
  1228. return 0;
  1229. vpu_trace(inst->dev, "[%d]\n", inst->id);
  1230. if (!vdec->udata.virt) {
  1231. vdec->udata.length = 0x1000;
  1232. ret = vpu_alloc_dma(inst->core, &vdec->udata);
  1233. if (ret) {
  1234. dev_err(inst->dev, "[%d] alloc udata fail\n", inst->id);
  1235. goto error;
  1236. }
  1237. }
  1238. if (!inst->stream_buffer.virt) {
  1239. stream_buffer_size = vpu_iface_get_stream_buffer_size(inst->core);
  1240. if (stream_buffer_size > 0) {
  1241. inst->stream_buffer.length = stream_buffer_size;
  1242. ret = vpu_alloc_dma(inst->core, &inst->stream_buffer);
  1243. if (ret) {
  1244. dev_err(inst->dev, "[%d] alloc stream buffer fail\n", inst->id);
  1245. goto error;
  1246. }
  1247. inst->use_stream_buffer = true;
  1248. }
  1249. }
  1250. if (inst->use_stream_buffer)
  1251. vpu_iface_config_stream_buffer(inst, &inst->stream_buffer);
  1252. vpu_iface_init_instance(inst);
  1253. vdec->params.udata.base = vdec->udata.phys;
  1254. vdec->params.udata.size = vdec->udata.length;
  1255. ret = vpu_iface_set_decode_params(inst, &vdec->params, 0);
  1256. if (ret) {
  1257. dev_err(inst->dev, "[%d] set decode params fail\n", inst->id);
  1258. goto error;
  1259. }
  1260. vdec_init_params(vdec);
  1261. ret = vpu_session_start(inst);
  1262. if (ret) {
  1263. dev_err(inst->dev, "[%d] start fail\n", inst->id);
  1264. goto error;
  1265. }
  1266. vdec_update_state(inst, VPU_CODEC_STATE_STARTED, 0);
  1267. return 0;
  1268. error:
  1269. vpu_free_dma(&vdec->udata);
  1270. vpu_free_dma(&inst->stream_buffer);
  1271. return ret;
  1272. }
  1273. static int vdec_start_session(struct vpu_inst *inst, u32 type)
  1274. {
  1275. struct vdec_t *vdec = inst->priv;
  1276. int ret = 0;
  1277. if (V4L2_TYPE_IS_OUTPUT(type)) {
  1278. if (vdec->reset_codec)
  1279. vdec_stop(inst, false);
  1280. if (inst->state == VPU_CODEC_STATE_DEINIT) {
  1281. ret = vdec_start(inst);
  1282. if (ret)
  1283. return ret;
  1284. }
  1285. }
  1286. if (V4L2_TYPE_IS_OUTPUT(type)) {
  1287. vdec_update_state(inst, vdec->state, 1);
  1288. vdec->eos_received = 0;
  1289. vpu_process_output_buffer(inst);
  1290. } else {
  1291. vdec_cmd_start(inst);
  1292. }
  1293. if (inst->state == VPU_CODEC_STATE_ACTIVE)
  1294. vdec_response_fs_request(inst, false);
  1295. return ret;
  1296. }
  1297. static int vdec_stop_session(struct vpu_inst *inst, u32 type)
  1298. {
  1299. struct vdec_t *vdec = inst->priv;
  1300. if (inst->state == VPU_CODEC_STATE_DEINIT)
  1301. return 0;
  1302. if (V4L2_TYPE_IS_OUTPUT(type)) {
  1303. vdec_update_state(inst, VPU_CODEC_STATE_SEEK, 0);
  1304. vdec->drain = 0;
  1305. } else {
  1306. if (inst->state != VPU_CODEC_STATE_DYAMIC_RESOLUTION_CHANGE) {
  1307. vdec_abort(inst);
  1308. vdec->eos_received = 0;
  1309. }
  1310. vdec_clear_slots(inst);
  1311. }
  1312. return 0;
  1313. }
  1314. static int vdec_get_debug_info(struct vpu_inst *inst, char *str, u32 size, u32 i)
  1315. {
  1316. struct vdec_t *vdec = inst->priv;
  1317. int num = -1;
  1318. switch (i) {
  1319. case 0:
  1320. num = scnprintf(str, size,
  1321. "req_frame_count = %d\ninterlaced = %d\n",
  1322. vdec->req_frame_count,
  1323. vdec->codec_info.progressive ? 0 : 1);
  1324. break;
  1325. case 1:
  1326. num = scnprintf(str, size,
  1327. "mbi: size = 0x%x request = %d, alloc = %d, response = %d\n",
  1328. vdec->mbi.size,
  1329. vdec->mbi.req_count,
  1330. vdec->mbi.count,
  1331. vdec->mbi.index);
  1332. break;
  1333. case 2:
  1334. num = scnprintf(str, size,
  1335. "dcp: size = 0x%x request = %d, alloc = %d, response = %d\n",
  1336. vdec->dcp.size,
  1337. vdec->dcp.req_count,
  1338. vdec->dcp.count,
  1339. vdec->dcp.index);
  1340. break;
  1341. case 3:
  1342. num = scnprintf(str, size, "input_frame_count = %d\n", vdec->params.frame_count);
  1343. break;
  1344. case 4:
  1345. num = scnprintf(str, size, "decoded_frame_count = %d\n", vdec->decoded_frame_count);
  1346. break;
  1347. case 5:
  1348. num = scnprintf(str, size, "display_frame_count = %d\n", vdec->display_frame_count);
  1349. break;
  1350. case 6:
  1351. num = scnprintf(str, size, "sequence = %d\n", vdec->sequence);
  1352. break;
  1353. case 7:
  1354. num = scnprintf(str, size, "drain = %d, eos = %d, source_change = %d\n",
  1355. vdec->drain, vdec->eos_received, vdec->source_change);
  1356. break;
  1357. case 8:
  1358. num = scnprintf(str, size, "fps = %d/%d\n",
  1359. vdec->codec_info.frame_rate.numerator,
  1360. vdec->codec_info.frame_rate.denominator);
  1361. break;
  1362. case 9:
  1363. num = scnprintf(str, size, "colorspace: %d, %d, %d, %d (%d)\n",
  1364. vdec->codec_info.color_primaries,
  1365. vdec->codec_info.transfer_chars,
  1366. vdec->codec_info.matrix_coeffs,
  1367. vdec->codec_info.full_range,
  1368. vdec->codec_info.vui_present);
  1369. break;
  1370. default:
  1371. break;
  1372. }
  1373. return num;
  1374. }
  1375. static struct vpu_inst_ops vdec_inst_ops = {
  1376. .ctrl_init = vdec_ctrl_init,
  1377. .check_ready = vdec_check_ready,
  1378. .buf_done = vdec_buf_done,
  1379. .get_one_frame = vdec_frame_decoded,
  1380. .stop_done = vdec_stop_done,
  1381. .event_notify = vdec_event_notify,
  1382. .release = vdec_release,
  1383. .cleanup = vdec_cleanup,
  1384. .start = vdec_start_session,
  1385. .stop = vdec_stop_session,
  1386. .process_output = vdec_process_output,
  1387. .process_capture = vdec_process_capture,
  1388. .on_queue_empty = vdec_on_queue_empty,
  1389. .get_debug_info = vdec_get_debug_info,
  1390. .wait_prepare = vpu_inst_unlock,
  1391. .wait_finish = vpu_inst_lock,
  1392. };
  1393. static void vdec_init(struct file *file)
  1394. {
  1395. struct vpu_inst *inst = to_inst(file);
  1396. struct v4l2_format f;
  1397. memset(&f, 0, sizeof(f));
  1398. f.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
  1399. f.fmt.pix_mp.pixelformat = V4L2_PIX_FMT_H264;
  1400. f.fmt.pix_mp.width = 1280;
  1401. f.fmt.pix_mp.height = 720;
  1402. f.fmt.pix_mp.field = V4L2_FIELD_NONE;
  1403. vdec_s_fmt(file, &inst->fh, &f);
  1404. memset(&f, 0, sizeof(f));
  1405. f.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
  1406. f.fmt.pix_mp.pixelformat = V4L2_PIX_FMT_NV12M_8L128;
  1407. f.fmt.pix_mp.width = 1280;
  1408. f.fmt.pix_mp.height = 720;
  1409. f.fmt.pix_mp.field = V4L2_FIELD_NONE;
  1410. vdec_s_fmt(file, &inst->fh, &f);
  1411. }
  1412. static int vdec_open(struct file *file)
  1413. {
  1414. struct vpu_inst *inst;
  1415. struct vdec_t *vdec;
  1416. int ret;
  1417. inst = vzalloc(sizeof(*inst));
  1418. if (!inst)
  1419. return -ENOMEM;
  1420. vdec = vzalloc(sizeof(*vdec));
  1421. if (!vdec) {
  1422. vfree(inst);
  1423. return -ENOMEM;
  1424. }
  1425. inst->ops = &vdec_inst_ops;
  1426. inst->formats = vdec_formats;
  1427. inst->type = VPU_CORE_TYPE_DEC;
  1428. inst->priv = vdec;
  1429. ret = vpu_v4l2_open(file, inst);
  1430. if (ret)
  1431. return ret;
  1432. vdec->fixed_fmt = false;
  1433. vdec->state = VPU_CODEC_STATE_ACTIVE;
  1434. inst->min_buffer_cap = VDEC_MIN_BUFFER_CAP;
  1435. inst->min_buffer_out = VDEC_MIN_BUFFER_OUT;
  1436. vdec_init(file);
  1437. return 0;
  1438. }
  1439. static const struct v4l2_file_operations vdec_fops = {
  1440. .owner = THIS_MODULE,
  1441. .open = vdec_open,
  1442. .release = vpu_v4l2_close,
  1443. .unlocked_ioctl = video_ioctl2,
  1444. .poll = v4l2_m2m_fop_poll,
  1445. .mmap = v4l2_m2m_fop_mmap,
  1446. };
  1447. const struct v4l2_ioctl_ops *vdec_get_ioctl_ops(void)
  1448. {
  1449. return &vdec_ioctl_ops;
  1450. }
  1451. const struct v4l2_file_operations *vdec_get_fops(void)
  1452. {
  1453. return &vdec_fops;
  1454. }