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.

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