Statistics
| Branch: | Revision:

ffmpeg / libavfilter / libmpcodecs / vf_cropdetect.c @ e4852fb3

History | View | Annotate | Download (5.23 KB)

1
/*
2
 * This file is part of MPlayer.
3
 *
4
 * MPlayer is free software; you can redistribute it and/or modify
5
 * it under the terms of the GNU General Public License as published by
6
 * the Free Software Foundation; either version 2 of the License, or
7
 * (at your option) any later version.
8
 *
9
 * MPlayer 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
12
 * GNU General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU General Public License along
15
 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
16
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17
 */
18

    
19
#include <stdio.h>
20
#include <stdlib.h>
21
#include <string.h>
22
#include <inttypes.h>
23

    
24
#include "config.h"
25
#include "mp_msg.h"
26
#include "help_mp.h"
27

    
28
#include "img_format.h"
29
#include "mp_image.h"
30
#include "vf.h"
31

    
32
struct vf_priv_s {
33
    int x1,y1,x2,y2;
34
    int limit;
35
    int round;
36
    int reset_count;
37
    int fno;
38
};
39

    
40
static int checkline(unsigned char* src,int stride,int len,int bpp){
41
    int total=0;
42
    int div=len;
43
    switch(bpp){
44
    case 1:
45
        while(--len>=0){
46
            total+=src[0]; src+=stride;
47
        }
48
        break;
49
    case 3:
50
    case 4:
51
        while(--len>=0){
52
            total+=src[0]+src[1]+src[2]; src+=stride;
53
        }
54
        div*=3;
55
        break;
56
    }
57
    total/=div;
58
//    printf("total=%d\n",total);
59
    return total;
60
}
61

    
62
//===========================================================================//
63

    
64
static int config(struct vf_instance *vf,
65
        int width, int height, int d_width, int d_height,
66
        unsigned int flags, unsigned int outfmt){
67
    vf->priv->x1=width - 1;
68
    vf->priv->y1=height - 1;
69
    vf->priv->x2=0;
70
    vf->priv->y2=0;
71
    vf->priv->fno=-2;
72
    return vf_next_config(vf,width,height,d_width,d_height,flags,outfmt);
73
}
74

    
75
static int put_image(struct vf_instance *vf, mp_image_t *mpi, double pts){
76
    mp_image_t *dmpi;
77
    int bpp=mpi->bpp/8;
78
    int w,h,x,y,shrink_by;
79

    
80
    // hope we'll get DR buffer:
81
    dmpi=vf_get_image(vf->next,mpi->imgfmt,
82
        MP_IMGTYPE_EXPORT, 0,
83
        mpi->w, mpi->h);
84

    
85
    dmpi->planes[0]=mpi->planes[0];
86
    dmpi->planes[1]=mpi->planes[1];
87
    dmpi->planes[2]=mpi->planes[2];
88
    dmpi->stride[0]=mpi->stride[0];
89
    dmpi->stride[1]=mpi->stride[1];
90
    dmpi->stride[2]=mpi->stride[2];
91
    dmpi->width=mpi->width;
92
    dmpi->height=mpi->height;
93

    
94
if(++vf->priv->fno>0){        // ignore first 2 frames - they may be empty
95

    
96
    // Reset the crop area every reset_count frames, if reset_count is > 0
97
    if(vf->priv->reset_count > 0 && vf->priv->fno > vf->priv->reset_count){
98
        vf->priv->x1=mpi->w-1;
99
        vf->priv->y1=mpi->h-1;
100
        vf->priv->x2=0;
101
        vf->priv->y2=0;
102
        vf->priv->fno=1;
103
    }
104

    
105
    for(y=0;y<vf->priv->y1;y++){
106
        if(checkline(mpi->planes[0]+mpi->stride[0]*y,bpp,mpi->w,bpp)>vf->priv->limit){
107
            vf->priv->y1=y;
108
            break;
109
        }
110
    }
111

    
112
    for(y=mpi->h-1;y>vf->priv->y2;y--){
113
        if(checkline(mpi->planes[0]+mpi->stride[0]*y,bpp,mpi->w,bpp)>vf->priv->limit){
114
            vf->priv->y2=y;
115
            break;
116
        }
117
    }
118

    
119
    for(y=0;y<vf->priv->x1;y++){
120
        if(checkline(mpi->planes[0]+bpp*y,mpi->stride[0],mpi->h,bpp)>vf->priv->limit){
121
            vf->priv->x1=y;
122
            break;
123
        }
124
    }
125

    
126
    for(y=mpi->w-1;y>vf->priv->x2;y--){
127
        if(checkline(mpi->planes[0]+bpp*y,mpi->stride[0],mpi->h,bpp)>vf->priv->limit){
128
            vf->priv->x2=y;
129
            break;
130
        }
131
    }
132

    
133
    // round x and y (up), important for yuv colorspaces
134
    // make sure they stay rounded!
135
    x=(vf->priv->x1+1)&(~1);
136
    y=(vf->priv->y1+1)&(~1);
137

    
138
    w = vf->priv->x2 - x + 1;
139
    h = vf->priv->y2 - y + 1;
140

    
141
    // w and h must be divisible by 2 as well because of yuv
142
    // colorspace problems.
143
    if (vf->priv->round <= 1)
144
      vf->priv->round = 16;
145
    if (vf->priv->round % 2)
146
      vf->priv->round *= 2;
147

    
148
    shrink_by = w % vf->priv->round;
149
    w -= shrink_by;
150
    x += (shrink_by / 2 + 1) & ~1;
151

    
152
    shrink_by = h % vf->priv->round;
153
    h -= shrink_by;
154
    y += (shrink_by / 2 + 1) & ~1;
155

    
156
    mp_msg(MSGT_VFILTER, MSGL_INFO, MSGTR_MPCODECS_CropArea,
157
        vf->priv->x1,vf->priv->x2,
158
        vf->priv->y1,vf->priv->y2,
159
        w,h,x,y);
160

    
161

    
162
}
163

    
164
    return vf_next_put_image(vf,dmpi, pts);
165
}
166

    
167
static int query_format(struct vf_instance *vf, unsigned int fmt) {
168
  switch(fmt) {
169
    // the default limit value works only right with YV12 right now.
170
    case IMGFMT_YV12:
171
      return vf_next_query_format(vf, fmt);
172
  }
173
  return 0;
174
}
175
//===========================================================================//
176

    
177
static int vf_open(vf_instance_t *vf, char *args){
178
    vf->config=config;
179
    vf->put_image=put_image;
180
    vf->query_format=query_format;
181
    vf->priv=malloc(sizeof(struct vf_priv_s));
182
    vf->priv->limit=24; // should be option
183
    vf->priv->round = 0;
184
    vf->priv->reset_count = 0;
185
    if(args) sscanf(args, "%d:%d:%d",
186
    &vf->priv->limit,
187
    &vf->priv->round,
188
    &vf->priv->reset_count);
189
    return 1;
190
}
191

    
192
const vf_info_t vf_info_cropdetect = {
193
    "autodetect crop size",
194
    "cropdetect",
195
    "A'rpi",
196
    "",
197
    vf_open,
198
    NULL
199
};
200

    
201
//===========================================================================//