~ubuntu-branches/ubuntu/trusty/libav/trusty-proposed

« back to all changes in this revision

Viewing changes to libavfilter/vf_cropdetect.c

  • Committer: Package Import Robot
  • Author(s): Reinhard Tartler
  • Date: 2013-10-22 23:24:08 UTC
  • mfrom: (1.3.36 sid)
  • Revision ID: package-import@ubuntu.com-20131022232408-b8tvvn4pyzri9mi3
Tags: 6:9.10-1ubuntu1
* Build all -extra flavors from this source package, as libav got demoted
  from main to universe, cf LP: #1243235
* Simplify debian/rules to follow exactly the code that debian executes
* New upstream (LP: #1180288) fixes lots of security issues (LP: #1242802)
* Merge from unstable, remaining changes:
  - build-depend on libtiff5-dev rather than libtiff4-dev,
    avoids FTBFS caused by imlib
  - follow the regular debian codepaths

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
 * Ported from MPlayer libmpcodecs/vf_cropdetect.c.
24
24
 */
25
25
 
 
26
#include <stdio.h>
 
27
 
26
28
#include "libavutil/imgutils.h"
 
29
#include "libavutil/internal.h"
27
30
#include "avfilter.h"
 
31
#include "formats.h"
 
32
#include "internal.h"
 
33
#include "video.h"
28
34
 
29
35
typedef struct {
30
36
    int x1, y1, x2, y2;
37
43
 
38
44
static int query_formats(AVFilterContext *ctx)
39
45
{
40
 
    static const enum PixelFormat pix_fmts[] = {
41
 
        PIX_FMT_YUV420P, PIX_FMT_YUVJ420P,
42
 
        PIX_FMT_YUV422P, PIX_FMT_YUVJ422P,
43
 
        PIX_FMT_YUV444P, PIX_FMT_YUVJ444P,
44
 
        PIX_FMT_YUV411P, PIX_FMT_GRAY8,
45
 
        PIX_FMT_NV12,    PIX_FMT_NV21,
46
 
        PIX_FMT_NONE
 
46
    static const enum AVPixelFormat pix_fmts[] = {
 
47
        AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUVJ420P,
 
48
        AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVJ422P,
 
49
        AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVJ444P,
 
50
        AV_PIX_FMT_YUV411P, AV_PIX_FMT_GRAY8,
 
51
        AV_PIX_FMT_NV12,    AV_PIX_FMT_NV21,
 
52
        AV_PIX_FMT_NONE
47
53
    };
48
54
 
49
 
    avfilter_set_common_formats(ctx, avfilter_make_format_list(pix_fmts));
 
55
    ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
50
56
    return 0;
51
57
}
52
58
 
77
83
    return total;
78
84
}
79
85
 
80
 
static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
 
86
static av_cold int init(AVFilterContext *ctx, const char *args)
81
87
{
82
88
    CropDetectContext *cd = ctx->priv;
83
89
 
89
95
    if (args)
90
96
        sscanf(args, "%d:%d:%d", &cd->limit, &cd->round, &cd->reset_count);
91
97
 
92
 
    av_log(ctx, AV_LOG_INFO, "limit:%d round:%d reset_count:%d\n",
 
98
    av_log(ctx, AV_LOG_VERBOSE, "limit:%d round:%d reset_count:%d\n",
93
99
           cd->limit, cd->round, cd->reset_count);
94
100
 
95
101
    return 0;
101
107
    CropDetectContext *cd = ctx->priv;
102
108
 
103
109
    av_image_fill_max_pixsteps(cd->max_pixsteps, NULL,
104
 
                               &av_pix_fmt_descriptors[inlink->format]);
 
110
                               av_pix_fmt_desc_get(inlink->format));
105
111
 
106
112
    cd->x1 = inlink->w - 1;
107
113
    cd->y1 = inlink->h - 1;
111
117
    return 0;
112
118
}
113
119
 
114
 
static void end_frame(AVFilterLink *inlink)
 
120
static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *frame)
115
121
{
116
122
    AVFilterContext *ctx = inlink->dst;
117
123
    CropDetectContext *cd = ctx->priv;
118
 
    AVFilterBufferRef *picref = inlink->cur_buf;
119
124
    int bpp = cd->max_pixsteps[0];
120
125
    int w, h, x, y, shrink_by;
121
126
 
123
128
    if (++cd->frame_nb > 0) {
124
129
        // Reset the crop area every reset_count frames, if reset_count is > 0
125
130
        if (cd->reset_count > 0 && cd->frame_nb > cd->reset_count) {
126
 
            cd->x1 = picref->video->w-1;
127
 
            cd->y1 = picref->video->h-1;
 
131
            cd->x1 = frame->video->w-1;
 
132
            cd->y1 = frame->video->h-1;
128
133
            cd->x2 = 0;
129
134
            cd->y2 = 0;
130
135
            cd->frame_nb = 1;
131
136
        }
132
137
 
133
138
        for (y = 0; y < cd->y1; y++) {
134
 
            if (checkline(ctx, picref->data[0] + picref->linesize[0] * y, bpp, picref->video->w, bpp) > cd->limit) {
 
139
            if (checkline(ctx, frame->data[0] + frame->linesize[0] * y, bpp, frame->video->w, bpp) > cd->limit) {
135
140
                cd->y1 = y;
136
141
                break;
137
142
            }
138
143
        }
139
144
 
140
 
        for (y = picref->video->h-1; y > cd->y2; y--) {
141
 
            if (checkline(ctx, picref->data[0] + picref->linesize[0] * y, bpp, picref->video->w, bpp) > cd->limit) {
 
145
        for (y = frame->video->h-1; y > cd->y2; y--) {
 
146
            if (checkline(ctx, frame->data[0] + frame->linesize[0] * y, bpp, frame->video->w, bpp) > cd->limit) {
142
147
                cd->y2 = y;
143
148
                break;
144
149
            }
145
150
        }
146
151
 
147
152
        for (y = 0; y < cd->x1; y++) {
148
 
            if (checkline(ctx, picref->data[0] + bpp*y, picref->linesize[0], picref->video->h, bpp) > cd->limit) {
 
153
            if (checkline(ctx, frame->data[0] + bpp*y, frame->linesize[0], frame->video->h, bpp) > cd->limit) {
149
154
                cd->x1 = y;
150
155
                break;
151
156
            }
152
157
        }
153
158
 
154
 
        for (y = picref->video->w-1; y > cd->x2; y--) {
155
 
            if (checkline(ctx, picref->data[0] + bpp*y, picref->linesize[0], picref->video->h, bpp) > cd->limit) {
 
159
        for (y = frame->video->w-1; y > cd->x2; y--) {
 
160
            if (checkline(ctx, frame->data[0] + bpp*y, frame->linesize[0], frame->video->h, bpp) > cd->limit) {
156
161
                cd->x2 = y;
157
162
                break;
158
163
            }
183
188
 
184
189
        av_log(ctx, AV_LOG_INFO,
185
190
               "x1:%d x2:%d y1:%d y2:%d w:%d h:%d x:%d y:%d pos:%"PRId64" pts:%"PRId64" t:%f crop=%d:%d:%d:%d\n",
186
 
               cd->x1, cd->x2, cd->y1, cd->y2, w, h, x, y, picref->pos, picref->pts,
187
 
               picref->pts == AV_NOPTS_VALUE ? -1 : picref->pts * av_q2d(inlink->time_base),
 
191
               cd->x1, cd->x2, cd->y1, cd->y2, w, h, x, y, frame->pos, frame->pts,
 
192
               frame->pts == AV_NOPTS_VALUE ? -1 : frame->pts * av_q2d(inlink->time_base),
188
193
               w, h, x, y);
189
194
    }
190
195
 
191
 
    avfilter_end_frame(inlink->dst->outputs[0]);
 
196
    return ff_filter_frame(inlink->dst->outputs[0], frame);
192
197
}
193
198
 
 
199
static const AVFilterPad avfilter_vf_cropdetect_inputs[] = {
 
200
    {
 
201
        .name             = "default",
 
202
        .type             = AVMEDIA_TYPE_VIDEO,
 
203
        .config_props     = config_input,
 
204
        .get_video_buffer = ff_null_get_video_buffer,
 
205
        .filter_frame     = filter_frame,
 
206
    },
 
207
    { NULL }
 
208
};
 
209
 
 
210
static const AVFilterPad avfilter_vf_cropdetect_outputs[] = {
 
211
    {
 
212
        .name = "default",
 
213
        .type = AVMEDIA_TYPE_VIDEO
 
214
    },
 
215
    { NULL }
 
216
};
 
217
 
194
218
AVFilter avfilter_vf_cropdetect = {
195
219
    .name        = "cropdetect",
196
220
    .description = NULL_IF_CONFIG_SMALL("Auto-detect crop size."),
200
224
 
201
225
    .query_formats = query_formats,
202
226
 
203
 
    .inputs    = (AVFilterPad[]) {{ .name = "default",
204
 
                                    .type             = AVMEDIA_TYPE_VIDEO,
205
 
                                    .config_props     = config_input,
206
 
                                    .get_video_buffer = avfilter_null_get_video_buffer,
207
 
                                    .start_frame      = avfilter_null_start_frame,
208
 
                                    .end_frame        = end_frame, },
209
 
                                  { .name = NULL}},
 
227
    .inputs    = avfilter_vf_cropdetect_inputs,
210
228
 
211
 
    .outputs   = (AVFilterPad[]) {{ .name             = "default",
212
 
                                    .type             = AVMEDIA_TYPE_VIDEO },
213
 
                                  { .name = NULL}},
 
229
    .outputs   = avfilter_vf_cropdetect_outputs,
214
230
};