vf_deinterlace_vaapi.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. /*
  2. * This file is part of FFmpeg.
  3. *
  4. * FFmpeg is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * FFmpeg is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with FFmpeg; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include <string.h>
  19. #include "libavutil/avassert.h"
  20. #include "libavutil/common.h"
  21. #include "libavutil/mem.h"
  22. #include "libavutil/opt.h"
  23. #include "libavutil/pixdesc.h"
  24. #include "avfilter.h"
  25. #include "formats.h"
  26. #include "internal.h"
  27. #include "video.h"
  28. #include "vaapi_vpp.h"
  29. #define MAX_REFERENCES 8
  30. typedef struct DeintVAAPIContext {
  31. VAAPIVPPContext vpp_ctx; // must be the first field
  32. int mode;
  33. int field_rate;
  34. int auto_enable;
  35. VAProcFilterCapDeinterlacing
  36. deint_caps[VAProcDeinterlacingCount];
  37. int nb_deint_caps;
  38. VAProcPipelineCaps pipeline_caps;
  39. int queue_depth;
  40. int queue_count;
  41. AVFrame *frame_queue[MAX_REFERENCES];
  42. int extra_delay_for_timestamps;
  43. } DeintVAAPIContext;
  44. static const char *deint_vaapi_mode_name(int mode)
  45. {
  46. switch (mode) {
  47. #define D(name) case VAProcDeinterlacing ## name: return #name
  48. D(Bob);
  49. D(Weave);
  50. D(MotionAdaptive);
  51. D(MotionCompensated);
  52. #undef D
  53. default:
  54. return "Invalid";
  55. }
  56. }
  57. static void deint_vaapi_pipeline_uninit(AVFilterContext *avctx)
  58. {
  59. DeintVAAPIContext *ctx = avctx->priv;
  60. int i;
  61. for (i = 0; i < ctx->queue_count; i++)
  62. av_frame_free(&ctx->frame_queue[i]);
  63. ctx->queue_count = 0;
  64. ff_vaapi_vpp_pipeline_uninit(avctx);
  65. }
  66. static int deint_vaapi_build_filter_params(AVFilterContext *avctx)
  67. {
  68. VAAPIVPPContext *vpp_ctx = avctx->priv;
  69. DeintVAAPIContext *ctx = avctx->priv;
  70. VAStatus vas;
  71. VAProcFilterParameterBufferDeinterlacing params;
  72. int i;
  73. ctx->nb_deint_caps = VAProcDeinterlacingCount;
  74. vas = vaQueryVideoProcFilterCaps(vpp_ctx->hwctx->display,
  75. vpp_ctx->va_context,
  76. VAProcFilterDeinterlacing,
  77. &ctx->deint_caps,
  78. &ctx->nb_deint_caps);
  79. if (vas != VA_STATUS_SUCCESS) {
  80. av_log(avctx, AV_LOG_ERROR, "Failed to query deinterlacing "
  81. "caps: %d (%s).\n", vas, vaErrorStr(vas));
  82. return AVERROR(EIO);
  83. }
  84. if (ctx->mode == VAProcDeinterlacingNone) {
  85. for (i = 0; i < ctx->nb_deint_caps; i++) {
  86. if (ctx->deint_caps[i].type > ctx->mode)
  87. ctx->mode = ctx->deint_caps[i].type;
  88. }
  89. av_log(avctx, AV_LOG_VERBOSE, "Picking %d (%s) as default "
  90. "deinterlacing mode.\n", ctx->mode,
  91. deint_vaapi_mode_name(ctx->mode));
  92. } else {
  93. for (i = 0; i < ctx->nb_deint_caps; i++) {
  94. if (ctx->deint_caps[i].type == ctx->mode)
  95. break;
  96. }
  97. if (i >= ctx->nb_deint_caps) {
  98. av_log(avctx, AV_LOG_ERROR, "Deinterlacing mode %d (%s) is "
  99. "not supported.\n", ctx->mode,
  100. deint_vaapi_mode_name(ctx->mode));
  101. return AVERROR(EINVAL);
  102. }
  103. }
  104. params.type = VAProcFilterDeinterlacing;
  105. params.algorithm = ctx->mode;
  106. params.flags = 0;
  107. vas = ff_vaapi_vpp_make_param_buffers(avctx,
  108. VAProcFilterParameterBufferType,
  109. &params,
  110. sizeof(params),
  111. 1);
  112. if (vas)
  113. return vas;
  114. vas = vaQueryVideoProcPipelineCaps(vpp_ctx->hwctx->display,
  115. vpp_ctx->va_context,
  116. &vpp_ctx->filter_buffers[0], 1,
  117. &ctx->pipeline_caps);
  118. if (vas != VA_STATUS_SUCCESS) {
  119. av_log(avctx, AV_LOG_ERROR, "Failed to query pipeline "
  120. "caps: %d (%s).\n", vas, vaErrorStr(vas));
  121. return AVERROR(EIO);
  122. }
  123. ctx->extra_delay_for_timestamps = ctx->field_rate == 2 &&
  124. ctx->pipeline_caps.num_backward_references == 0;
  125. ctx->queue_depth = ctx->pipeline_caps.num_backward_references +
  126. ctx->pipeline_caps.num_forward_references +
  127. ctx->extra_delay_for_timestamps + 1;
  128. if (ctx->queue_depth > MAX_REFERENCES) {
  129. av_log(avctx, AV_LOG_ERROR, "Pipeline requires too many "
  130. "references (%u forward, %u back).\n",
  131. ctx->pipeline_caps.num_forward_references,
  132. ctx->pipeline_caps.num_backward_references);
  133. return AVERROR(ENOSYS);
  134. }
  135. return 0;
  136. }
  137. static int deint_vaapi_config_output(AVFilterLink *outlink)
  138. {
  139. AVFilterLink *inlink = outlink->src->inputs[0];
  140. AVFilterContext *avctx = outlink->src;
  141. DeintVAAPIContext *ctx = avctx->priv;
  142. int err;
  143. err = ff_vaapi_vpp_config_output(outlink);
  144. if (err < 0)
  145. return err;
  146. outlink->time_base = av_mul_q(inlink->time_base,
  147. (AVRational) { 1, ctx->field_rate });
  148. outlink->frame_rate = av_mul_q(inlink->frame_rate,
  149. (AVRational) { ctx->field_rate, 1 });
  150. return 0;
  151. }
  152. static int deint_vaapi_filter_frame(AVFilterLink *inlink, AVFrame *input_frame)
  153. {
  154. AVFilterContext *avctx = inlink->dst;
  155. AVFilterLink *outlink = avctx->outputs[0];
  156. VAAPIVPPContext *vpp_ctx = avctx->priv;
  157. DeintVAAPIContext *ctx = avctx->priv;
  158. AVFrame *output_frame = NULL;
  159. VASurfaceID input_surface;
  160. VASurfaceID backward_references[MAX_REFERENCES];
  161. VASurfaceID forward_references[MAX_REFERENCES];
  162. VAProcPipelineParameterBuffer params;
  163. VAProcFilterParameterBufferDeinterlacing *filter_params;
  164. VAStatus vas;
  165. void *filter_params_addr = NULL;
  166. int err, i, field, current_frame_index;
  167. av_log(avctx, AV_LOG_DEBUG, "Filter input: %s, %ux%u (%"PRId64").\n",
  168. av_get_pix_fmt_name(input_frame->format),
  169. input_frame->width, input_frame->height, input_frame->pts);
  170. if (ctx->queue_count < ctx->queue_depth) {
  171. ctx->frame_queue[ctx->queue_count++] = input_frame;
  172. if (ctx->queue_count < ctx->queue_depth) {
  173. // Need more reference surfaces before we can continue.
  174. return 0;
  175. }
  176. } else {
  177. av_frame_free(&ctx->frame_queue[0]);
  178. for (i = 0; i + 1 < ctx->queue_count; i++)
  179. ctx->frame_queue[i] = ctx->frame_queue[i + 1];
  180. ctx->frame_queue[i] = input_frame;
  181. }
  182. current_frame_index = ctx->pipeline_caps.num_forward_references;
  183. input_frame = ctx->frame_queue[current_frame_index];
  184. input_surface = (VASurfaceID)(uintptr_t)input_frame->data[3];
  185. for (i = 0; i < ctx->pipeline_caps.num_forward_references; i++)
  186. forward_references[i] = (VASurfaceID)(uintptr_t)
  187. ctx->frame_queue[current_frame_index - i - 1]->data[3];
  188. for (i = 0; i < ctx->pipeline_caps.num_backward_references; i++)
  189. backward_references[i] = (VASurfaceID)(uintptr_t)
  190. ctx->frame_queue[current_frame_index + i + 1]->data[3];
  191. av_log(avctx, AV_LOG_DEBUG, "Using surface %#x for "
  192. "deinterlace input.\n", input_surface);
  193. av_log(avctx, AV_LOG_DEBUG, "Backward references:");
  194. for (i = 0; i < ctx->pipeline_caps.num_backward_references; i++)
  195. av_log(avctx, AV_LOG_DEBUG, " %#x", backward_references[i]);
  196. av_log(avctx, AV_LOG_DEBUG, "\n");
  197. av_log(avctx, AV_LOG_DEBUG, "Forward references:");
  198. for (i = 0; i < ctx->pipeline_caps.num_forward_references; i++)
  199. av_log(avctx, AV_LOG_DEBUG, " %#x", forward_references[i]);
  200. av_log(avctx, AV_LOG_DEBUG, "\n");
  201. for (field = 0; field < ctx->field_rate; field++) {
  202. output_frame = ff_get_video_buffer(outlink, vpp_ctx->output_width,
  203. vpp_ctx->output_height);
  204. if (!output_frame) {
  205. err = AVERROR(ENOMEM);
  206. goto fail;
  207. }
  208. err = av_frame_copy_props(output_frame, input_frame);
  209. if (err < 0)
  210. return err;
  211. err = ff_vaapi_vpp_init_params(avctx, &params,
  212. input_frame, output_frame);
  213. if (err < 0)
  214. goto fail;
  215. if (!ctx->auto_enable || input_frame->interlaced_frame) {
  216. vas = vaMapBuffer(vpp_ctx->hwctx->display, vpp_ctx->filter_buffers[0],
  217. &filter_params_addr);
  218. if (vas != VA_STATUS_SUCCESS) {
  219. av_log(avctx, AV_LOG_ERROR, "Failed to map filter parameter "
  220. "buffer: %d (%s).\n", vas, vaErrorStr(vas));
  221. err = AVERROR(EIO);
  222. goto fail;
  223. }
  224. filter_params = filter_params_addr;
  225. filter_params->flags = 0;
  226. if (input_frame->top_field_first) {
  227. filter_params->flags |= field ? VA_DEINTERLACING_BOTTOM_FIELD : 0;
  228. } else {
  229. filter_params->flags |= VA_DEINTERLACING_BOTTOM_FIELD_FIRST;
  230. filter_params->flags |= field ? 0 : VA_DEINTERLACING_BOTTOM_FIELD;
  231. }
  232. filter_params_addr = NULL;
  233. vas = vaUnmapBuffer(vpp_ctx->hwctx->display, vpp_ctx->filter_buffers[0]);
  234. if (vas != VA_STATUS_SUCCESS)
  235. av_log(avctx, AV_LOG_ERROR, "Failed to unmap filter parameter "
  236. "buffer: %d (%s).\n", vas, vaErrorStr(vas));
  237. params.filters = &vpp_ctx->filter_buffers[0];
  238. params.num_filters = 1;
  239. params.forward_references = forward_references;
  240. params.num_forward_references =
  241. ctx->pipeline_caps.num_forward_references;
  242. params.backward_references = backward_references;
  243. params.num_backward_references =
  244. ctx->pipeline_caps.num_backward_references;
  245. } else {
  246. params.filters = NULL;
  247. params.num_filters = 0;
  248. }
  249. err = ff_vaapi_vpp_render_picture(avctx, &params, output_frame);
  250. if (err < 0)
  251. goto fail;
  252. if (ctx->field_rate == 2) {
  253. if (field == 0)
  254. output_frame->pts = 2 * input_frame->pts;
  255. else
  256. output_frame->pts = input_frame->pts +
  257. ctx->frame_queue[current_frame_index + 1]->pts;
  258. }
  259. output_frame->interlaced_frame = 0;
  260. av_log(avctx, AV_LOG_DEBUG, "Filter output: %s, %ux%u (%"PRId64").\n",
  261. av_get_pix_fmt_name(output_frame->format),
  262. output_frame->width, output_frame->height, output_frame->pts);
  263. err = ff_filter_frame(outlink, output_frame);
  264. if (err < 0)
  265. break;
  266. }
  267. return err;
  268. fail:
  269. if (filter_params_addr)
  270. vaUnmapBuffer(vpp_ctx->hwctx->display, vpp_ctx->filter_buffers[0]);
  271. av_frame_free(&output_frame);
  272. return err;
  273. }
  274. static av_cold int deint_vaapi_init(AVFilterContext *avctx)
  275. {
  276. VAAPIVPPContext *vpp_ctx = avctx->priv;
  277. ff_vaapi_vpp_ctx_init(avctx);
  278. vpp_ctx->pipeline_uninit = deint_vaapi_pipeline_uninit;
  279. vpp_ctx->build_filter_params = deint_vaapi_build_filter_params;
  280. vpp_ctx->output_format = AV_PIX_FMT_NONE;
  281. return 0;
  282. }
  283. #define OFFSET(x) offsetof(DeintVAAPIContext, x)
  284. #define FLAGS (AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM)
  285. static const AVOption deint_vaapi_options[] = {
  286. { "mode", "Deinterlacing mode",
  287. OFFSET(mode), AV_OPT_TYPE_INT, { .i64 = VAProcDeinterlacingNone },
  288. VAProcDeinterlacingNone, VAProcDeinterlacingCount - 1, FLAGS, "mode" },
  289. { "default", "Use the highest-numbered (and therefore possibly most advanced) deinterlacing algorithm",
  290. 0, AV_OPT_TYPE_CONST, { .i64 = VAProcDeinterlacingNone }, 0, 0, FLAGS, "mode" },
  291. { "bob", "Use the bob deinterlacing algorithm",
  292. 0, AV_OPT_TYPE_CONST, { .i64 = VAProcDeinterlacingBob }, 0, 0, FLAGS, "mode" },
  293. { "weave", "Use the weave deinterlacing algorithm",
  294. 0, AV_OPT_TYPE_CONST, { .i64 = VAProcDeinterlacingWeave }, 0, 0, FLAGS, "mode" },
  295. { "motion_adaptive", "Use the motion adaptive deinterlacing algorithm",
  296. 0, AV_OPT_TYPE_CONST, { .i64 = VAProcDeinterlacingMotionAdaptive }, 0, 0, FLAGS, "mode" },
  297. { "motion_compensated", "Use the motion compensated deinterlacing algorithm",
  298. 0, AV_OPT_TYPE_CONST, { .i64 = VAProcDeinterlacingMotionCompensated }, 0, 0, FLAGS, "mode" },
  299. { "rate", "Generate output at frame rate or field rate",
  300. OFFSET(field_rate), AV_OPT_TYPE_INT, { .i64 = 1 }, 1, 2, FLAGS, "rate" },
  301. { "frame", "Output at frame rate (one frame of output for each field-pair)",
  302. 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, 0, 0, FLAGS, "rate" },
  303. { "field", "Output at field rate (one frame of output for each field)",
  304. 0, AV_OPT_TYPE_CONST, { .i64 = 2 }, 0, 0, FLAGS, "rate" },
  305. { "auto", "Only deinterlace fields, passing frames through unchanged",
  306. OFFSET(auto_enable), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, FLAGS },
  307. { NULL },
  308. };
  309. static const AVClass deint_vaapi_class = {
  310. .class_name = "deinterlace_vaapi",
  311. .item_name = av_default_item_name,
  312. .option = deint_vaapi_options,
  313. .version = LIBAVUTIL_VERSION_INT,
  314. };
  315. static const AVFilterPad deint_vaapi_inputs[] = {
  316. {
  317. .name = "default",
  318. .type = AVMEDIA_TYPE_VIDEO,
  319. .filter_frame = &deint_vaapi_filter_frame,
  320. .config_props = &ff_vaapi_vpp_config_input,
  321. },
  322. { NULL }
  323. };
  324. static const AVFilterPad deint_vaapi_outputs[] = {
  325. {
  326. .name = "default",
  327. .type = AVMEDIA_TYPE_VIDEO,
  328. .config_props = &deint_vaapi_config_output,
  329. },
  330. { NULL }
  331. };
  332. AVFilter ff_vf_deinterlace_vaapi = {
  333. .name = "deinterlace_vaapi",
  334. .description = NULL_IF_CONFIG_SMALL("Deinterlacing of VAAPI surfaces"),
  335. .priv_size = sizeof(DeintVAAPIContext),
  336. .init = &deint_vaapi_init,
  337. .uninit = &ff_vaapi_vpp_ctx_uninit,
  338. .query_formats = &ff_vaapi_vpp_query_formats,
  339. .inputs = deint_vaapi_inputs,
  340. .outputs = deint_vaapi_outputs,
  341. .priv_class = &deint_vaapi_class,
  342. .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,
  343. };