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.

175 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, AVFrame *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. AVFrame *outbuf;
  97. int qstride, qp_type;
  98. int8_t *qp_table ;
  99. outbuf = ff_get_video_buffer(outlink, aligned_w, aligned_h);
  100. if (!outbuf) {
  101. av_frame_free(&inbuf);
  102. return AVERROR(ENOMEM);
  103. }
  104. av_frame_copy_props(outbuf, inbuf);
  105. qp_table = av_frame_get_qp_table(inbuf, &qstride, &qp_type);
  106. pp_postprocess((const uint8_t **)inbuf->data, inbuf->linesize,
  107. outbuf->data, outbuf->linesize,
  108. aligned_w, outlink->h,
  109. qp_table,
  110. qstride,
  111. pp->modes[pp->mode_id],
  112. pp->pp_ctx,
  113. outbuf->pict_type | (qp_type ? PP_PICT_TYPE_QP2 : 0));
  114. av_frame_free(&inbuf);
  115. return ff_filter_frame(outlink, outbuf);
  116. }
  117. static av_cold void pp_uninit(AVFilterContext *ctx)
  118. {
  119. int i;
  120. PPFilterContext *pp = ctx->priv;
  121. for (i = 0; i <= PP_QUALITY_MAX; i++)
  122. pp_free_mode(pp->modes[i]);
  123. if (pp->pp_ctx)
  124. pp_free_context(pp->pp_ctx);
  125. }
  126. static const AVFilterPad pp_inputs[] = {
  127. {
  128. .name = "default",
  129. .type = AVMEDIA_TYPE_VIDEO,
  130. .config_props = pp_config_props,
  131. .filter_frame = pp_filter_frame,
  132. },
  133. { NULL }
  134. };
  135. static const AVFilterPad pp_outputs[] = {
  136. {
  137. .name = "default",
  138. .type = AVMEDIA_TYPE_VIDEO,
  139. },
  140. { NULL }
  141. };
  142. AVFilter avfilter_vf_pp = {
  143. .name = "pp",
  144. .description = NULL_IF_CONFIG_SMALL("Filter video using libpostproc."),
  145. .priv_size = sizeof(PPFilterContext),
  146. .init = pp_init,
  147. .uninit = pp_uninit,
  148. .query_formats = pp_query_formats,
  149. .inputs = pp_inputs,
  150. .outputs = pp_outputs,
  151. .process_command = pp_process_command,
  152. };