You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

173 lines
4.9KB

  1. /*
  2. * Copyright (c) 2002 A'rpi
  3. * Copyright (C) 2012 Clément Bœsch
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with FFmpeg; if not, write to the Free Software Foundation, Inc.,
  19. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  20. */
  21. /**
  22. * @file
  23. * libpostproc filter, ported from MPlayer.
  24. */
  25. #include "libavutil/avassert.h"
  26. #include "libavutil/opt.h"
  27. #include "internal.h"
  28. #include "libpostproc/postprocess.h"
  29. typedef struct {
  30. int mode_id;
  31. pp_mode *modes[PP_QUALITY_MAX + 1];
  32. void *pp_ctx;
  33. } PPFilterContext;
  34. static av_cold int pp_init(AVFilterContext *ctx, const char *args)
  35. {
  36. int i;
  37. PPFilterContext *pp = ctx->priv;
  38. if (!args || !*args)
  39. args = "de";
  40. for (i = 0; i <= PP_QUALITY_MAX; i++) {
  41. pp->modes[i] = pp_get_mode_by_name_and_quality(args, i);
  42. if (!pp->modes[i])
  43. return AVERROR_EXTERNAL;
  44. }
  45. pp->mode_id = PP_QUALITY_MAX;
  46. return 0;
  47. }
  48. static int pp_process_command(AVFilterContext *ctx, const char *cmd, const char *args,
  49. char *res, int res_len, int flags)
  50. {
  51. PPFilterContext *pp = ctx->priv;
  52. if (!strcmp(cmd, "quality")) {
  53. pp->mode_id = av_clip(strtol(args, NULL, 10), 0, PP_QUALITY_MAX);
  54. return 0;
  55. }
  56. return AVERROR(ENOSYS);
  57. }
  58. static int pp_query_formats(AVFilterContext *ctx)
  59. {
  60. static const enum PixelFormat pix_fmts[] = {
  61. AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUVJ420P,
  62. AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVJ422P,
  63. AV_PIX_FMT_YUV411P,
  64. AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVJ444P,
  65. AV_PIX_FMT_NONE
  66. };
  67. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  68. return 0;
  69. }
  70. static int pp_config_props(AVFilterLink *inlink)
  71. {
  72. int flags = PP_CPU_CAPS_AUTO;
  73. PPFilterContext *pp = inlink->dst->priv;
  74. switch (inlink->format) {
  75. case AV_PIX_FMT_YUVJ420P:
  76. case AV_PIX_FMT_YUV420P: flags |= PP_FORMAT_420; break;
  77. case AV_PIX_FMT_YUVJ422P:
  78. case AV_PIX_FMT_YUV422P: flags |= PP_FORMAT_422; break;
  79. case AV_PIX_FMT_YUV411P: flags |= PP_FORMAT_411; break;
  80. case AV_PIX_FMT_YUVJ444P:
  81. case AV_PIX_FMT_YUV444P: flags |= PP_FORMAT_444; break;
  82. default: av_assert0(0);
  83. }
  84. pp->pp_ctx = pp_get_context(inlink->w, inlink->h, flags);
  85. if (!pp->pp_ctx)
  86. return AVERROR(ENOMEM);
  87. return 0;
  88. }
  89. static int pp_filter_frame(AVFilterLink *inlink, AVFilterBufferRef *inbuf)
  90. {
  91. AVFilterContext *ctx = inlink->dst;
  92. PPFilterContext *pp = ctx->priv;
  93. AVFilterLink *outlink = ctx->outputs[0];
  94. const int aligned_w = FFALIGN(outlink->w, 8);
  95. const int aligned_h = FFALIGN(outlink->h, 8);
  96. AVFilterBufferRef *outbuf;
  97. outbuf = ff_get_video_buffer(outlink, AV_PERM_WRITE, aligned_w, aligned_h);
  98. if (!outbuf) {
  99. avfilter_unref_buffer(inbuf);
  100. return AVERROR(ENOMEM);
  101. }
  102. avfilter_copy_buffer_ref_props(outbuf, inbuf);
  103. pp_postprocess((const uint8_t **)inbuf->data, inbuf->linesize,
  104. outbuf->data, outbuf->linesize,
  105. aligned_w, outlink->h,
  106. outbuf->video->qp_table,
  107. outbuf->video->qp_table_linesize,
  108. pp->modes[pp->mode_id],
  109. pp->pp_ctx,
  110. outbuf->video->pict_type);
  111. avfilter_unref_buffer(inbuf);
  112. return ff_filter_frame(outlink, outbuf);
  113. }
  114. static av_cold void pp_uninit(AVFilterContext *ctx)
  115. {
  116. int i;
  117. PPFilterContext *pp = ctx->priv;
  118. for (i = 0; i <= PP_QUALITY_MAX; i++)
  119. pp_free_mode(pp->modes[i]);
  120. if (pp->pp_ctx)
  121. pp_free_context(pp->pp_ctx);
  122. }
  123. static const AVFilterPad pp_inputs[] = {
  124. {
  125. .name = "default",
  126. .type = AVMEDIA_TYPE_VIDEO,
  127. .config_props = pp_config_props,
  128. .filter_frame = pp_filter_frame,
  129. .min_perms = AV_PERM_READ,
  130. },
  131. { NULL }
  132. };
  133. static const AVFilterPad pp_outputs[] = {
  134. {
  135. .name = "default",
  136. .type = AVMEDIA_TYPE_VIDEO,
  137. },
  138. { NULL }
  139. };
  140. AVFilter avfilter_vf_pp = {
  141. .name = "pp",
  142. .description = NULL_IF_CONFIG_SMALL("Filter video using libpostproc."),
  143. .priv_size = sizeof(PPFilterContext),
  144. .init = pp_init,
  145. .uninit = pp_uninit,
  146. .query_formats = pp_query_formats,
  147. .inputs = pp_inputs,
  148. .outputs = pp_outputs,
  149. .process_command = pp_process_command,
  150. };