vf_pp.c 5.5 KB

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