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.

270 lines
7.9KB

  1. /*
  2. * This file is part of FFmpeg.
  3. *
  4. * FFmpeg is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * FFmpeg is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with FFmpeg; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "libavutil/avassert.h"
  19. #include "libavutil/eval.h"
  20. #include "libavutil/opt.h"
  21. #include "avfilter.h"
  22. #include "internal.h"
  23. enum {
  24. X, Y, W, H,
  25. NB_PARAMS,
  26. };
  27. static const char addroi_param_names[] = {
  28. 'x', 'y', 'w', 'h',
  29. };
  30. enum {
  31. VAR_IW,
  32. VAR_IH,
  33. NB_VARS,
  34. };
  35. static const char *const addroi_var_names[] = {
  36. "iw",
  37. "ih",
  38. };
  39. typedef struct AddROIContext {
  40. const AVClass *class;
  41. char *region_str[NB_PARAMS];
  42. AVExpr *region_expr[NB_PARAMS];
  43. int region[NB_PARAMS];
  44. AVRational qoffset;
  45. int clear;
  46. } AddROIContext;
  47. static int addroi_config_input(AVFilterLink *inlink)
  48. {
  49. AVFilterContext *avctx = inlink->dst;
  50. AddROIContext *ctx = avctx->priv;
  51. int i;
  52. double vars[NB_VARS];
  53. double val;
  54. vars[VAR_IW] = inlink->w;
  55. vars[VAR_IH] = inlink->h;
  56. for (i = 0; i < NB_PARAMS; i++) {
  57. int max_value;
  58. switch (i) {
  59. case X: max_value = inlink->w; break;
  60. case Y: max_value = inlink->h; break;
  61. case W: max_value = inlink->w - ctx->region[X]; break;
  62. case H: max_value = inlink->h - ctx->region[Y]; break;
  63. }
  64. val = av_expr_eval(ctx->region_expr[i], vars, NULL);
  65. if (val < 0.0) {
  66. av_log(avctx, AV_LOG_WARNING, "Calculated value %g for %c is "
  67. "less than zero - using zero instead.\n", val,
  68. addroi_param_names[i]);
  69. val = 0.0;
  70. } else if (val > max_value) {
  71. av_log(avctx, AV_LOG_WARNING, "Calculated value %g for %c is "
  72. "greater than maximum allowed value %d - "
  73. "using %d instead.\n", val, addroi_param_names[i],
  74. max_value, max_value);
  75. val = max_value;
  76. }
  77. ctx->region[i] = val;
  78. }
  79. return 0;
  80. }
  81. static int addroi_filter_frame(AVFilterLink *inlink, AVFrame *frame)
  82. {
  83. AVFilterContext *avctx = inlink->dst;
  84. AVFilterLink *outlink = avctx->outputs[0];
  85. AddROIContext *ctx = avctx->priv;
  86. AVRegionOfInterest *roi;
  87. AVFrameSideData *sd;
  88. int err;
  89. if (ctx->clear) {
  90. av_frame_remove_side_data(frame, AV_FRAME_DATA_REGIONS_OF_INTEREST);
  91. sd = NULL;
  92. } else {
  93. sd = av_frame_get_side_data(frame, AV_FRAME_DATA_REGIONS_OF_INTEREST);
  94. }
  95. if (sd) {
  96. const AVRegionOfInterest *old_roi;
  97. uint32_t old_roi_size;
  98. AVBufferRef *roi_ref;
  99. int nb_roi, i;
  100. old_roi = (const AVRegionOfInterest*)sd->data;
  101. old_roi_size = old_roi->self_size;
  102. av_assert0(old_roi_size && sd->size % old_roi_size == 0);
  103. nb_roi = sd->size / old_roi_size + 1;
  104. roi_ref = av_buffer_alloc(sizeof(*roi) * nb_roi);
  105. if (!roi_ref) {
  106. err = AVERROR(ENOMEM);
  107. goto fail;
  108. }
  109. roi = (AVRegionOfInterest*)roi_ref->data;
  110. for (i = 0; i < nb_roi - 1; i++) {
  111. old_roi = (const AVRegionOfInterest*)
  112. (sd->data + old_roi_size * i);
  113. roi[i] = (AVRegionOfInterest) {
  114. .self_size = sizeof(*roi),
  115. .top = old_roi->top,
  116. .bottom = old_roi->bottom,
  117. .left = old_roi->left,
  118. .right = old_roi->right,
  119. .qoffset = old_roi->qoffset,
  120. };
  121. }
  122. roi[nb_roi - 1] = (AVRegionOfInterest) {
  123. .self_size = sizeof(*roi),
  124. .top = ctx->region[Y],
  125. .bottom = ctx->region[Y] + ctx->region[H],
  126. .left = ctx->region[X],
  127. .right = ctx->region[X] + ctx->region[W],
  128. .qoffset = ctx->qoffset,
  129. };
  130. av_frame_remove_side_data(frame, AV_FRAME_DATA_REGIONS_OF_INTEREST);
  131. sd = av_frame_new_side_data_from_buf(frame,
  132. AV_FRAME_DATA_REGIONS_OF_INTEREST,
  133. roi_ref);
  134. if (!sd) {
  135. av_buffer_unref(&roi_ref);
  136. err = AVERROR(ENOMEM);
  137. goto fail;
  138. }
  139. } else {
  140. sd = av_frame_new_side_data(frame, AV_FRAME_DATA_REGIONS_OF_INTEREST,
  141. sizeof(AVRegionOfInterest));
  142. if (!sd) {
  143. err = AVERROR(ENOMEM);
  144. goto fail;
  145. }
  146. roi = (AVRegionOfInterest*)sd->data;
  147. *roi = (AVRegionOfInterest) {
  148. .self_size = sizeof(*roi),
  149. .top = ctx->region[Y],
  150. .bottom = ctx->region[Y] + ctx->region[H],
  151. .left = ctx->region[X],
  152. .right = ctx->region[X] + ctx->region[W],
  153. .qoffset = ctx->qoffset,
  154. };
  155. }
  156. return ff_filter_frame(outlink, frame);
  157. fail:
  158. av_frame_free(&frame);
  159. return err;
  160. }
  161. static av_cold int addroi_init(AVFilterContext *avctx)
  162. {
  163. AddROIContext *ctx = avctx->priv;
  164. int i, err;
  165. for (i = 0; i < NB_PARAMS; i++) {
  166. err = av_expr_parse(&ctx->region_expr[i], ctx->region_str[i],
  167. addroi_var_names, NULL, NULL, NULL, NULL,
  168. 0, avctx);
  169. if (err < 0) {
  170. av_log(ctx, AV_LOG_ERROR,
  171. "Error parsing %c expression '%s'.\n",
  172. addroi_param_names[i], ctx->region_str[i]);
  173. return err;
  174. }
  175. }
  176. return 0;
  177. }
  178. static av_cold void addroi_uninit(AVFilterContext *avctx)
  179. {
  180. AddROIContext *ctx = avctx->priv;
  181. int i;
  182. for (i = 0; i < NB_PARAMS; i++) {
  183. av_expr_free(ctx->region_expr[i]);
  184. ctx->region_expr[i] = NULL;
  185. }
  186. }
  187. #define OFFSET(x) offsetof(AddROIContext, x)
  188. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
  189. static const AVOption addroi_options[] = {
  190. { "x", "Region distance from left edge of frame.",
  191. OFFSET(region_str[X]), AV_OPT_TYPE_STRING, { .str = "0" }, .flags = FLAGS },
  192. { "y", "Region distance from top edge of frame.",
  193. OFFSET(region_str[Y]), AV_OPT_TYPE_STRING, { .str = "0" }, .flags = FLAGS },
  194. { "w", "Region width.",
  195. OFFSET(region_str[W]), AV_OPT_TYPE_STRING, { .str = "0" }, .flags = FLAGS },
  196. { "h", "Region height.",
  197. OFFSET(region_str[H]), AV_OPT_TYPE_STRING, { .str = "0" }, .flags = FLAGS },
  198. { "qoffset", "Quantisation offset to apply in the region.",
  199. OFFSET(qoffset), AV_OPT_TYPE_RATIONAL, { .dbl = -0.1 }, -1, +1, FLAGS },
  200. { "clear", "Remove any existing regions of interest before adding the new one.",
  201. OFFSET(clear), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS },
  202. { NULL }
  203. };
  204. AVFILTER_DEFINE_CLASS(addroi);
  205. static const AVFilterPad addroi_inputs[] = {
  206. {
  207. .name = "default",
  208. .type = AVMEDIA_TYPE_VIDEO,
  209. .config_props = addroi_config_input,
  210. .filter_frame = addroi_filter_frame,
  211. },
  212. { NULL }
  213. };
  214. static const AVFilterPad addroi_outputs[] = {
  215. {
  216. .name = "default",
  217. .type = AVMEDIA_TYPE_VIDEO,
  218. },
  219. { NULL }
  220. };
  221. AVFilter ff_vf_addroi = {
  222. .name = "addroi",
  223. .description = NULL_IF_CONFIG_SMALL("Add region of interest to frame."),
  224. .init = addroi_init,
  225. .uninit = addroi_uninit,
  226. .priv_size = sizeof(AddROIContext),
  227. .priv_class = &addroi_class,
  228. .inputs = addroi_inputs,
  229. .outputs = addroi_outputs,
  230. };