*/
/**
- * @file libavfilter/vf_crop.c
+ * @file
* video crop filter
*/
#include "avfilter.h"
-#include "libavutil/pixdesc.h"
+#include "libavcore/imgutils.h"
typedef struct {
int x; ///< x offset of the non-cropped area with respect to the input area
int w; ///< width of the cropped area
int h; ///< height of the cropped area
- int bpp; ///< bits per pixel
+ int max_step[4]; ///< max pixel step for each plane, expressed as a number of bytes
int hsub, vsub; ///< chroma subsampling
} CropContext;
{
AVFilterContext *ctx = link->dst;
CropContext *crop = ctx->priv;
+ const AVPixFmtDescriptor *pix_desc = &av_pix_fmt_descriptors[link->format];
- switch (link->format) {
- case PIX_FMT_RGB48BE:
- case PIX_FMT_RGB48LE:
- crop->bpp = 48;
- break;
- case PIX_FMT_ARGB:
- case PIX_FMT_RGBA:
- case PIX_FMT_ABGR:
- case PIX_FMT_BGRA:
- crop->bpp = 32;
- break;
- case PIX_FMT_RGB24:
- case PIX_FMT_BGR24:
- crop->bpp = 24;
- break;
- case PIX_FMT_RGB565BE:
- case PIX_FMT_RGB565LE:
- case PIX_FMT_RGB555BE:
- case PIX_FMT_RGB555LE:
- case PIX_FMT_BGR565BE:
- case PIX_FMT_BGR565LE:
- case PIX_FMT_BGR555BE:
- case PIX_FMT_BGR555LE:
- case PIX_FMT_GRAY16BE:
- case PIX_FMT_GRAY16LE:
- case PIX_FMT_YUV420P16LE:
- case PIX_FMT_YUV420P16BE:
- case PIX_FMT_YUV422P16LE:
- case PIX_FMT_YUV422P16BE:
- case PIX_FMT_YUV444P16LE:
- case PIX_FMT_YUV444P16BE:
- crop->bpp = 16;
- break;
- default:
- crop->bpp = 8;
- }
-
- avcodec_get_chroma_sub_sample(link->format, &crop->hsub, &crop->vsub);
+ av_fill_image_max_pixsteps(crop->max_step, NULL, pix_desc);
+ crop->hsub = av_pix_fmt_descriptors[link->format].log2_chroma_w;
+ crop->vsub = av_pix_fmt_descriptors[link->format].log2_chroma_h;
if (crop->w == 0)
crop->w = link->w - crop->x;
av_log(ctx, AV_LOG_ERROR,
"Output area %d:%d:%d:%d not within the input area 0:0:%d:%d or zero-sized\n",
crop->x, crop->y, crop->w, crop->h, link->w, link->h);
- return -1;
+ return AVERROR(EINVAL);
}
return 0;
return 0;
}
-static AVFilterPicRef *get_video_buffer(AVFilterLink *link, int perms,
- int w, int h)
-{
- return avfilter_get_video_buffer(link->dst->outputs[0], perms, w, h);
-}
-
-static void start_frame(AVFilterLink *link, AVFilterPicRef *picref)
+static void start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
{
CropContext *crop = link->dst->priv;
- AVFilterPicRef *ref2 = avfilter_ref_pic(picref, ~0);
+ AVFilterBufferRef *ref2 = avfilter_ref_buffer(picref, ~0);
int i;
- ref2->w = crop->w;
- ref2->h = crop->h;
+ picref->video->w = crop->w;
+ picref->video->h = crop->h;
ref2->data[0] += crop->y * ref2->linesize[0];
- ref2->data[0] += (crop->x * crop->bpp) >> 3;
+ ref2->data[0] += (crop->x * crop->max_step[0]);
if (!(av_pix_fmt_descriptors[link->format].flags & PIX_FMT_PAL)) {
for (i = 1; i < 3; i ++) {
if (ref2->data[i]) {
ref2->data[i] += (crop->y >> crop->vsub) * ref2->linesize[i];
- ref2->data[i] += ((crop->x * crop->bpp) >> 3) >> crop->hsub;
+ ref2->data[i] += (crop->x * crop->max_step[i]) >> crop->hsub;
}
}
}
/* alpha plane */
if (ref2->data[3]) {
ref2->data[3] += crop->y * ref2->linesize[3];
- ref2->data[3] += (crop->x * crop->bpp) >> 3;
+ ref2->data[3] += (crop->x * crop->max_step[3]);
}
avfilter_start_frame(link->dst->outputs[0], ref2);
.init = init,
.inputs = (AVFilterPad[]) {{ .name = "default",
- .type = CODEC_TYPE_VIDEO,
+ .type = AVMEDIA_TYPE_VIDEO,
.start_frame = start_frame,
.draw_slice = draw_slice,
- .get_video_buffer = get_video_buffer,
+ .get_video_buffer = avfilter_null_get_video_buffer,
.config_props = config_input, },
{ .name = NULL}},
.outputs = (AVFilterPad[]) {{ .name = "default",
- .type = CODEC_TYPE_VIDEO,
+ .type = AVMEDIA_TYPE_VIDEO,
.config_props = config_output, },
{ .name = NULL}},
};