vf_pp.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 PPFilterContext {
  30. const AVClass *class;
  31. char *subfilters;
  32. int mode_id;
  33. pp_mode *modes[PP_QUALITY_MAX + 1];
  34. void *pp_ctx;
  35. } PPFilterContext;
  36. #define OFFSET(x) offsetof(PPFilterContext, x)
  37. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  38. static const AVOption pp_options[] = {
  39. { "subfilters", "set postprocess subfilters", OFFSET(subfilters), AV_OPT_TYPE_STRING, {.str="de"}, .flags = FLAGS },
  40. { NULL }
  41. };
  42. AVFILTER_DEFINE_CLASS(pp);
  43. static av_cold int pp_init(AVFilterContext *ctx)
  44. {
  45. int i;
  46. PPFilterContext *pp = ctx->priv;
  47. for (i = 0; i <= PP_QUALITY_MAX; i++) {
  48. pp->modes[i] = pp_get_mode_by_name_and_quality(pp->subfilters, i);
  49. if (!pp->modes[i])
  50. return AVERROR_EXTERNAL;
  51. }
  52. pp->mode_id = PP_QUALITY_MAX;
  53. return 0;
  54. }
  55. static int pp_process_command(AVFilterContext *ctx, const char *cmd, const char *args,
  56. char *res, int res_len, int flags)
  57. {
  58. PPFilterContext *pp = ctx->priv;
  59. if (!strcmp(cmd, "quality")) {
  60. pp->mode_id = av_clip(strtol(args, NULL, 10), 0, PP_QUALITY_MAX);
  61. return 0;
  62. }
  63. return AVERROR(ENOSYS);
  64. }
  65. static int pp_query_formats(AVFilterContext *ctx)
  66. {
  67. static const enum AVPixelFormat pix_fmts[] = {
  68. AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUVJ420P,
  69. AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVJ422P,
  70. AV_PIX_FMT_YUV411P,
  71. AV_PIX_FMT_GBRP,
  72. AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVJ444P,
  73. AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUVJ440P,
  74. AV_PIX_FMT_GRAY8,
  75. AV_PIX_FMT_NONE
  76. };
  77. AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
  78. if (!fmts_list)
  79. return AVERROR(ENOMEM);
  80. return ff_set_common_formats(ctx, fmts_list);
  81. }
  82. static int pp_config_props(AVFilterLink *inlink)
  83. {
  84. int flags = PP_CPU_CAPS_AUTO;
  85. PPFilterContext *pp = inlink->dst->priv;
  86. switch (inlink->format) {
  87. case AV_PIX_FMT_GRAY8:
  88. case AV_PIX_FMT_YUVJ420P:
  89. case AV_PIX_FMT_YUV420P: flags |= PP_FORMAT_420; break;
  90. case AV_PIX_FMT_YUVJ422P:
  91. case AV_PIX_FMT_YUV422P: flags |= PP_FORMAT_422; break;
  92. case AV_PIX_FMT_YUV411P: flags |= PP_FORMAT_411; break;
  93. case AV_PIX_FMT_GBRP:
  94. case AV_PIX_FMT_YUVJ444P:
  95. case AV_PIX_FMT_YUV444P: flags |= PP_FORMAT_444; break;
  96. case AV_PIX_FMT_YUVJ440P:
  97. case AV_PIX_FMT_YUV440P: flags |= PP_FORMAT_440; break;
  98. default: av_assert0(0);
  99. }
  100. pp->pp_ctx = pp_get_context(inlink->w, inlink->h, flags);
  101. if (!pp->pp_ctx)
  102. return AVERROR(ENOMEM);
  103. return 0;
  104. }
  105. static int pp_filter_frame(AVFilterLink *inlink, AVFrame *inbuf)
  106. {
  107. AVFilterContext *ctx = inlink->dst;
  108. PPFilterContext *pp = ctx->priv;
  109. AVFilterLink *outlink = ctx->outputs[0];
  110. const int aligned_w = FFALIGN(outlink->w, 8);
  111. const int aligned_h = FFALIGN(outlink->h, 8);
  112. AVFrame *outbuf;
  113. int qstride, qp_type;
  114. int8_t *qp_table ;
  115. outbuf = ff_get_video_buffer(outlink, aligned_w, aligned_h);
  116. if (!outbuf) {
  117. av_frame_free(&inbuf);
  118. return AVERROR(ENOMEM);
  119. }
  120. av_frame_copy_props(outbuf, inbuf);
  121. outbuf->width = inbuf->width;
  122. outbuf->height = inbuf->height;
  123. qp_table = av_frame_get_qp_table(inbuf, &qstride, &qp_type);
  124. pp_postprocess((const uint8_t **)inbuf->data, inbuf->linesize,
  125. outbuf->data, outbuf->linesize,
  126. aligned_w, outlink->h,
  127. qp_table,
  128. qstride,
  129. pp->modes[pp->mode_id],
  130. pp->pp_ctx,
  131. outbuf->pict_type | (qp_type ? PP_PICT_TYPE_QP2 : 0));
  132. av_frame_free(&inbuf);
  133. return ff_filter_frame(outlink, outbuf);
  134. }
  135. static av_cold void pp_uninit(AVFilterContext *ctx)
  136. {
  137. int i;
  138. PPFilterContext *pp = ctx->priv;
  139. for (i = 0; i <= PP_QUALITY_MAX; i++)
  140. pp_free_mode(pp->modes[i]);
  141. if (pp->pp_ctx)
  142. pp_free_context(pp->pp_ctx);
  143. }
  144. static const AVFilterPad pp_inputs[] = {
  145. {
  146. .name = "default",
  147. .type = AVMEDIA_TYPE_VIDEO,
  148. .config_props = pp_config_props,
  149. .filter_frame = pp_filter_frame,
  150. },
  151. { NULL }
  152. };
  153. static const AVFilterPad pp_outputs[] = {
  154. {
  155. .name = "default",
  156. .type = AVMEDIA_TYPE_VIDEO,
  157. },
  158. { NULL }
  159. };
  160. AVFilter ff_vf_pp = {
  161. .name = "pp",
  162. .description = NULL_IF_CONFIG_SMALL("Filter video using libpostproc."),
  163. .priv_size = sizeof(PPFilterContext),
  164. .init = pp_init,
  165. .uninit = pp_uninit,
  166. .query_formats = pp_query_formats,
  167. .inputs = pp_inputs,
  168. .outputs = pp_outputs,
  169. .process_command = pp_process_command,
  170. .priv_class = &pp_class,
  171. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
  172. };