vf_crop.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*
  2. * copyright (c) 2007 Bobby Bingham
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /**
  21. * @file
  22. * video crop filter
  23. */
  24. #include "avfilter.h"
  25. #include "libavutil/pixdesc.h"
  26. typedef struct {
  27. int x; ///< x offset of the non-cropped area with respect to the input area
  28. int y; ///< y offset of the non-cropped area with respect to the input area
  29. int w; ///< width of the cropped area
  30. int h; ///< height of the cropped area
  31. int bpp; ///< bits per pixel
  32. int hsub, vsub; ///< chroma subsampling
  33. } CropContext;
  34. static int query_formats(AVFilterContext *ctx)
  35. {
  36. static const enum PixelFormat pix_fmts[] = {
  37. PIX_FMT_RGB48BE, PIX_FMT_RGB48LE,
  38. PIX_FMT_ARGB, PIX_FMT_RGBA,
  39. PIX_FMT_ABGR, PIX_FMT_BGRA,
  40. PIX_FMT_RGB24, PIX_FMT_BGR24,
  41. PIX_FMT_RGB565BE, PIX_FMT_RGB565LE,
  42. PIX_FMT_RGB555BE, PIX_FMT_RGB555LE,
  43. PIX_FMT_BGR565BE, PIX_FMT_BGR565LE,
  44. PIX_FMT_BGR555BE, PIX_FMT_BGR555LE,
  45. PIX_FMT_GRAY16BE, PIX_FMT_GRAY16LE,
  46. PIX_FMT_YUV420P16LE, PIX_FMT_YUV420P16BE,
  47. PIX_FMT_YUV422P16LE, PIX_FMT_YUV422P16BE,
  48. PIX_FMT_YUV444P16LE, PIX_FMT_YUV444P16BE,
  49. PIX_FMT_YUV444P, PIX_FMT_YUV422P,
  50. PIX_FMT_YUV420P, PIX_FMT_YUV411P,
  51. PIX_FMT_YUV410P, PIX_FMT_YUV440P,
  52. PIX_FMT_YUVJ444P, PIX_FMT_YUVJ422P,
  53. PIX_FMT_YUVJ420P, PIX_FMT_YUVJ440P,
  54. PIX_FMT_YUVA420P,
  55. PIX_FMT_RGB8, PIX_FMT_BGR8,
  56. PIX_FMT_RGB4_BYTE, PIX_FMT_BGR4_BYTE,
  57. PIX_FMT_PAL8, PIX_FMT_GRAY8,
  58. PIX_FMT_NONE
  59. };
  60. avfilter_set_common_formats(ctx, avfilter_make_format_list(pix_fmts));
  61. return 0;
  62. }
  63. static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
  64. {
  65. CropContext *crop = ctx->priv;
  66. if (args)
  67. sscanf(args, "%d:%d:%d:%d", &crop->x, &crop->y, &crop->w, &crop->h);
  68. return 0;
  69. }
  70. static int config_input(AVFilterLink *link)
  71. {
  72. AVFilterContext *ctx = link->dst;
  73. CropContext *crop = ctx->priv;
  74. switch (link->format) {
  75. case PIX_FMT_RGB48BE:
  76. case PIX_FMT_RGB48LE:
  77. crop->bpp = 48;
  78. break;
  79. case PIX_FMT_ARGB:
  80. case PIX_FMT_RGBA:
  81. case PIX_FMT_ABGR:
  82. case PIX_FMT_BGRA:
  83. crop->bpp = 32;
  84. break;
  85. case PIX_FMT_RGB24:
  86. case PIX_FMT_BGR24:
  87. crop->bpp = 24;
  88. break;
  89. case PIX_FMT_RGB565BE:
  90. case PIX_FMT_RGB565LE:
  91. case PIX_FMT_RGB555BE:
  92. case PIX_FMT_RGB555LE:
  93. case PIX_FMT_BGR565BE:
  94. case PIX_FMT_BGR565LE:
  95. case PIX_FMT_BGR555BE:
  96. case PIX_FMT_BGR555LE:
  97. case PIX_FMT_GRAY16BE:
  98. case PIX_FMT_GRAY16LE:
  99. case PIX_FMT_YUV420P16LE:
  100. case PIX_FMT_YUV420P16BE:
  101. case PIX_FMT_YUV422P16LE:
  102. case PIX_FMT_YUV422P16BE:
  103. case PIX_FMT_YUV444P16LE:
  104. case PIX_FMT_YUV444P16BE:
  105. crop->bpp = 16;
  106. break;
  107. default:
  108. crop->bpp = 8;
  109. }
  110. avcodec_get_chroma_sub_sample(link->format, &crop->hsub, &crop->vsub);
  111. if (crop->w == 0)
  112. crop->w = link->w - crop->x;
  113. if (crop->h == 0)
  114. crop->h = link->h - crop->y;
  115. crop->x &= ~((1 << crop->hsub) - 1);
  116. crop->y &= ~((1 << crop->vsub) - 1);
  117. av_log(link->dst, AV_LOG_INFO, "x:%d y:%d w:%d h:%d\n",
  118. crop->x, crop->y, crop->w, crop->h);
  119. if (crop->x < 0 || crop->y < 0 ||
  120. crop->w <= 0 || crop->h <= 0 ||
  121. (unsigned)crop->x + (unsigned)crop->w > link->w ||
  122. (unsigned)crop->y + (unsigned)crop->h > link->h) {
  123. av_log(ctx, AV_LOG_ERROR,
  124. "Output area %d:%d:%d:%d not within the input area 0:0:%d:%d or zero-sized\n",
  125. crop->x, crop->y, crop->w, crop->h, link->w, link->h);
  126. return -1;
  127. }
  128. return 0;
  129. }
  130. static int config_output(AVFilterLink *link)
  131. {
  132. CropContext *crop = link->src->priv;
  133. link->w = crop->w;
  134. link->h = crop->h;
  135. return 0;
  136. }
  137. static void start_frame(AVFilterLink *link, AVFilterPicRef *picref)
  138. {
  139. CropContext *crop = link->dst->priv;
  140. AVFilterPicRef *ref2 = avfilter_ref_pic(picref, ~0);
  141. int i;
  142. ref2->w = crop->w;
  143. ref2->h = crop->h;
  144. ref2->data[0] += crop->y * ref2->linesize[0];
  145. ref2->data[0] += (crop->x * crop->bpp) >> 3;
  146. if (!(av_pix_fmt_descriptors[link->format].flags & PIX_FMT_PAL)) {
  147. for (i = 1; i < 3; i ++) {
  148. if (ref2->data[i]) {
  149. ref2->data[i] += (crop->y >> crop->vsub) * ref2->linesize[i];
  150. ref2->data[i] += ((crop->x * crop->bpp) >> 3) >> crop->hsub;
  151. }
  152. }
  153. }
  154. /* alpha plane */
  155. if (ref2->data[3]) {
  156. ref2->data[3] += crop->y * ref2->linesize[3];
  157. ref2->data[3] += (crop->x * crop->bpp) >> 3;
  158. }
  159. avfilter_start_frame(link->dst->outputs[0], ref2);
  160. }
  161. static void draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
  162. {
  163. AVFilterContext *ctx = link->dst;
  164. CropContext *crop = ctx->priv;
  165. if (y >= crop->y + crop->h || y + h <= crop->y)
  166. return;
  167. if (y < crop->y) {
  168. h -= crop->y - y;
  169. y = crop->y;
  170. }
  171. if (y + h > crop->y + crop->h)
  172. h = crop->y + crop->h - y;
  173. avfilter_draw_slice(ctx->outputs[0], y - crop->y, h, slice_dir);
  174. }
  175. AVFilter avfilter_vf_crop = {
  176. .name = "crop",
  177. .description = NULL_IF_CONFIG_SMALL("Crop the input video to x:y:width:height."),
  178. .priv_size = sizeof(CropContext),
  179. .query_formats = query_formats,
  180. .init = init,
  181. .inputs = (AVFilterPad[]) {{ .name = "default",
  182. .type = AVMEDIA_TYPE_VIDEO,
  183. .start_frame = start_frame,
  184. .draw_slice = draw_slice,
  185. .get_video_buffer = avfilter_null_get_video_buffer,
  186. .config_props = config_input, },
  187. { .name = NULL}},
  188. .outputs = (AVFilterPad[]) {{ .name = "default",
  189. .type = AVMEDIA_TYPE_VIDEO,
  190. .config_props = config_output, },
  191. { .name = NULL}},
  192. };