annotate libmpcodecs/vf_blackframe.c @ 32455:5c9189c15fe0

Move find_sub.c to the sub directory.
author cigaes
date Wed, 27 Oct 2010 16:32:03 +0000
parents a972c1a4a012
children 8fa2f43cb760
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
26727
82601a38e2a7 Use standard license headers.
diego
parents: 24969
diff changeset
1 /*
82601a38e2a7 Use standard license headers.
diego
parents: 24969
diff changeset
2 * detect frames that are (almost) black
19094
e84360ee61c9 new black frame detection filter
ivo
parents:
diff changeset
3 * search for black frames to detect scene transitions
e84360ee61c9 new black frame detection filter
ivo
parents:
diff changeset
4 * (c) 2006 Julian Hall
e84360ee61c9 new black frame detection filter
ivo
parents:
diff changeset
5 *
e84360ee61c9 new black frame detection filter
ivo
parents:
diff changeset
6 * based on code designed for skipping commercials
e84360ee61c9 new black frame detection filter
ivo
parents:
diff changeset
7 * (c) 2002-2003 Brian J. Murrell
e84360ee61c9 new black frame detection filter
ivo
parents:
diff changeset
8 *
e84360ee61c9 new black frame detection filter
ivo
parents:
diff changeset
9 * cleanup, simplify, speedup (c) 2006 by Ivo van Poorten
e84360ee61c9 new black frame detection filter
ivo
parents:
diff changeset
10 *
26727
82601a38e2a7 Use standard license headers.
diego
parents: 24969
diff changeset
11 * This file is part of MPlayer.
82601a38e2a7 Use standard license headers.
diego
parents: 24969
diff changeset
12 *
82601a38e2a7 Use standard license headers.
diego
parents: 24969
diff changeset
13 * MPlayer is free software; you can redistribute it and/or modify
19094
e84360ee61c9 new black frame detection filter
ivo
parents:
diff changeset
14 * it under the terms of the GNU General Public License as published by
e84360ee61c9 new black frame detection filter
ivo
parents:
diff changeset
15 * the Free Software Foundation; either version 2 of the License, or
e84360ee61c9 new black frame detection filter
ivo
parents:
diff changeset
16 * (at your option) any later version.
e84360ee61c9 new black frame detection filter
ivo
parents:
diff changeset
17 *
26727
82601a38e2a7 Use standard license headers.
diego
parents: 24969
diff changeset
18 * MPlayer is distributed in the hope that it will be useful,
19094
e84360ee61c9 new black frame detection filter
ivo
parents:
diff changeset
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
e84360ee61c9 new black frame detection filter
ivo
parents:
diff changeset
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
e84360ee61c9 new black frame detection filter
ivo
parents:
diff changeset
21 * GNU General Public License for more details.
e84360ee61c9 new black frame detection filter
ivo
parents:
diff changeset
22 *
26727
82601a38e2a7 Use standard license headers.
diego
parents: 24969
diff changeset
23 * You should have received a copy of the GNU General Public License along
82601a38e2a7 Use standard license headers.
diego
parents: 24969
diff changeset
24 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
82601a38e2a7 Use standard license headers.
diego
parents: 24969
diff changeset
25 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19094
e84360ee61c9 new black frame detection filter
ivo
parents:
diff changeset
26 */
e84360ee61c9 new black frame detection filter
ivo
parents:
diff changeset
27
e84360ee61c9 new black frame detection filter
ivo
parents:
diff changeset
28 #include <stdio.h>
e84360ee61c9 new black frame detection filter
ivo
parents:
diff changeset
29 #include <stdlib.h>
e84360ee61c9 new black frame detection filter
ivo
parents:
diff changeset
30 #include <string.h>
e84360ee61c9 new black frame detection filter
ivo
parents:
diff changeset
31
e84360ee61c9 new black frame detection filter
ivo
parents:
diff changeset
32 #include "config.h"
e84360ee61c9 new black frame detection filter
ivo
parents:
diff changeset
33 #include "mp_msg.h"
e84360ee61c9 new black frame detection filter
ivo
parents:
diff changeset
34
e84360ee61c9 new black frame detection filter
ivo
parents:
diff changeset
35 #include "img_format.h"
e84360ee61c9 new black frame detection filter
ivo
parents:
diff changeset
36 #include "mp_image.h"
e84360ee61c9 new black frame detection filter
ivo
parents:
diff changeset
37 #include "vf.h"
e84360ee61c9 new black frame detection filter
ivo
parents:
diff changeset
38
e84360ee61c9 new black frame detection filter
ivo
parents:
diff changeset
39 struct vf_priv_s {
19096
30084f256717 print frame type and keep track of last encountered keyframe
ivo
parents: 19094
diff changeset
40 unsigned int bamount, bthresh, frame, lastkeyframe;
19094
e84360ee61c9 new black frame detection filter
ivo
parents:
diff changeset
41 };
e84360ee61c9 new black frame detection filter
ivo
parents:
diff changeset
42
30642
a972c1a4a012 cosmetics: Rename struct vf_instance_s --> vf_instance.
diego
parents: 30638
diff changeset
43 static int config(struct vf_instance *vf, int width, int height, int d_width,
19094
e84360ee61c9 new black frame detection filter
ivo
parents:
diff changeset
44 int d_height, unsigned int flags, unsigned int outfmt) {
e84360ee61c9 new black frame detection filter
ivo
parents:
diff changeset
45 return vf_next_config(vf,width,height,d_width,d_height,flags,outfmt);
e84360ee61c9 new black frame detection filter
ivo
parents:
diff changeset
46 }
e84360ee61c9 new black frame detection filter
ivo
parents:
diff changeset
47
30642
a972c1a4a012 cosmetics: Rename struct vf_instance_s --> vf_instance.
diego
parents: 30638
diff changeset
48 static int query_format(struct vf_instance *vf, unsigned fmt) {
19094
e84360ee61c9 new black frame detection filter
ivo
parents:
diff changeset
49 switch(fmt) {
e84360ee61c9 new black frame detection filter
ivo
parents:
diff changeset
50 case IMGFMT_YVU9:
e84360ee61c9 new black frame detection filter
ivo
parents:
diff changeset
51 case IMGFMT_IF09:
e84360ee61c9 new black frame detection filter
ivo
parents:
diff changeset
52 case IMGFMT_YV12:
e84360ee61c9 new black frame detection filter
ivo
parents:
diff changeset
53 case IMGFMT_I420:
e84360ee61c9 new black frame detection filter
ivo
parents:
diff changeset
54 case IMGFMT_IYUV:
e84360ee61c9 new black frame detection filter
ivo
parents:
diff changeset
55 case IMGFMT_CLPL:
e84360ee61c9 new black frame detection filter
ivo
parents:
diff changeset
56 case IMGFMT_Y800:
e84360ee61c9 new black frame detection filter
ivo
parents:
diff changeset
57 case IMGFMT_Y8:
e84360ee61c9 new black frame detection filter
ivo
parents:
diff changeset
58 case IMGFMT_NV12:
e84360ee61c9 new black frame detection filter
ivo
parents:
diff changeset
59 case IMGFMT_NV21:
e84360ee61c9 new black frame detection filter
ivo
parents:
diff changeset
60 case IMGFMT_444P:
e84360ee61c9 new black frame detection filter
ivo
parents:
diff changeset
61 case IMGFMT_422P:
e84360ee61c9 new black frame detection filter
ivo
parents:
diff changeset
62 case IMGFMT_411P:
e84360ee61c9 new black frame detection filter
ivo
parents:
diff changeset
63 case IMGFMT_HM12:
e84360ee61c9 new black frame detection filter
ivo
parents:
diff changeset
64 return vf_next_query_format(vf, fmt);
e84360ee61c9 new black frame detection filter
ivo
parents:
diff changeset
65 }
e84360ee61c9 new black frame detection filter
ivo
parents:
diff changeset
66 return 0;
e84360ee61c9 new black frame detection filter
ivo
parents:
diff changeset
67 }
e84360ee61c9 new black frame detection filter
ivo
parents:
diff changeset
68
30642
a972c1a4a012 cosmetics: Rename struct vf_instance_s --> vf_instance.
diego
parents: 30638
diff changeset
69 static int put_image(struct vf_instance *vf, mp_image_t *mpi, double pts){
19094
e84360ee61c9 new black frame detection filter
ivo
parents:
diff changeset
70 mp_image_t *dmpi;
e84360ee61c9 new black frame detection filter
ivo
parents:
diff changeset
71 int x, y;
e84360ee61c9 new black frame detection filter
ivo
parents:
diff changeset
72 int nblack=0, pblack=0;
e84360ee61c9 new black frame detection filter
ivo
parents:
diff changeset
73 unsigned char *yplane = mpi->planes[0];
e84360ee61c9 new black frame detection filter
ivo
parents:
diff changeset
74 unsigned int ystride = mpi->stride[0];
19096
30084f256717 print frame type and keep track of last encountered keyframe
ivo
parents: 19094
diff changeset
75 int pict_type = mpi->pict_type;
19094
e84360ee61c9 new black frame detection filter
ivo
parents:
diff changeset
76 int w = mpi->w, h = mpi->h;
e84360ee61c9 new black frame detection filter
ivo
parents:
diff changeset
77 int bthresh = vf->priv->bthresh;
e84360ee61c9 new black frame detection filter
ivo
parents:
diff changeset
78 int bamount = vf->priv->bamount;
24969
c2b7ba444ade begin moving const filter data to .text/.rodata sections
rfelker
parents: 19096
diff changeset
79 static const char *const picttypes[4] = { "unknown", "I", "P", "B" };
19094
e84360ee61c9 new black frame detection filter
ivo
parents:
diff changeset
80
e84360ee61c9 new black frame detection filter
ivo
parents:
diff changeset
81 for (y=1; y<=h; y++) {
e84360ee61c9 new black frame detection filter
ivo
parents:
diff changeset
82 for (x=0; x<w; x++)
e84360ee61c9 new black frame detection filter
ivo
parents:
diff changeset
83 nblack += yplane[x] < bthresh;
e84360ee61c9 new black frame detection filter
ivo
parents:
diff changeset
84 pblack = nblack*100/(w*y);
e84360ee61c9 new black frame detection filter
ivo
parents:
diff changeset
85 if (pblack < bamount) break;
e84360ee61c9 new black frame detection filter
ivo
parents:
diff changeset
86 yplane += ystride;
e84360ee61c9 new black frame detection filter
ivo
parents:
diff changeset
87 }
e84360ee61c9 new black frame detection filter
ivo
parents:
diff changeset
88
19096
30084f256717 print frame type and keep track of last encountered keyframe
ivo
parents: 19094
diff changeset
89 if (pict_type > 3 || pict_type < 0) pict_type = 0;
30084f256717 print frame type and keep track of last encountered keyframe
ivo
parents: 19094
diff changeset
90 if (pict_type == 1) vf->priv->lastkeyframe = vf->priv->frame;
30084f256717 print frame type and keep track of last encountered keyframe
ivo
parents: 19094
diff changeset
91
19094
e84360ee61c9 new black frame detection filter
ivo
parents:
diff changeset
92 if (pblack >= bamount)
19096
30084f256717 print frame type and keep track of last encountered keyframe
ivo
parents: 19094
diff changeset
93 mp_msg(MSGT_VFILTER, MSGL_INFO,"vf_blackframe: %u, %i%%, %s (I:%u)\n",
30084f256717 print frame type and keep track of last encountered keyframe
ivo
parents: 19094
diff changeset
94 vf->priv->frame, pblack, picttypes[pict_type],
30084f256717 print frame type and keep track of last encountered keyframe
ivo
parents: 19094
diff changeset
95 vf->priv->lastkeyframe);
19094
e84360ee61c9 new black frame detection filter
ivo
parents:
diff changeset
96
e84360ee61c9 new black frame detection filter
ivo
parents:
diff changeset
97 vf->priv->frame++;
e84360ee61c9 new black frame detection filter
ivo
parents:
diff changeset
98
e84360ee61c9 new black frame detection filter
ivo
parents:
diff changeset
99 dmpi = vf_get_image(vf->next, mpi->imgfmt, MP_IMGTYPE_EXPORT, 0,
e84360ee61c9 new black frame detection filter
ivo
parents:
diff changeset
100 mpi->width, mpi->height);
e84360ee61c9 new black frame detection filter
ivo
parents:
diff changeset
101 dmpi->planes[0] = mpi->planes[0];
e84360ee61c9 new black frame detection filter
ivo
parents:
diff changeset
102 dmpi->stride[0] = mpi->stride[0];
e84360ee61c9 new black frame detection filter
ivo
parents:
diff changeset
103 dmpi->planes[1] = mpi->planes[1];
e84360ee61c9 new black frame detection filter
ivo
parents:
diff changeset
104 dmpi->stride[1] = mpi->stride[1];
e84360ee61c9 new black frame detection filter
ivo
parents:
diff changeset
105 dmpi->planes[2] = mpi->planes[2];
e84360ee61c9 new black frame detection filter
ivo
parents:
diff changeset
106 dmpi->stride[2] = mpi->stride[2];
e84360ee61c9 new black frame detection filter
ivo
parents:
diff changeset
107
e84360ee61c9 new black frame detection filter
ivo
parents:
diff changeset
108 vf_clone_mpi_attributes(dmpi, mpi);
e84360ee61c9 new black frame detection filter
ivo
parents:
diff changeset
109
e84360ee61c9 new black frame detection filter
ivo
parents:
diff changeset
110 return vf_next_put_image(vf, dmpi, pts);
e84360ee61c9 new black frame detection filter
ivo
parents:
diff changeset
111 }
e84360ee61c9 new black frame detection filter
ivo
parents:
diff changeset
112
30642
a972c1a4a012 cosmetics: Rename struct vf_instance_s --> vf_instance.
diego
parents: 30638
diff changeset
113 static int control(struct vf_instance *vf, int request, void* data){
19094
e84360ee61c9 new black frame detection filter
ivo
parents:
diff changeset
114 return vf_next_control(vf,request,data);
e84360ee61c9 new black frame detection filter
ivo
parents:
diff changeset
115 }
e84360ee61c9 new black frame detection filter
ivo
parents:
diff changeset
116
30642
a972c1a4a012 cosmetics: Rename struct vf_instance_s --> vf_instance.
diego
parents: 30638
diff changeset
117 static void uninit(struct vf_instance *vf) {
19094
e84360ee61c9 new black frame detection filter
ivo
parents:
diff changeset
118 if (vf->priv) free(vf->priv);
e84360ee61c9 new black frame detection filter
ivo
parents:
diff changeset
119 }
e84360ee61c9 new black frame detection filter
ivo
parents:
diff changeset
120
30638
a7b908875c14 Rename open() vf initialization function to vf_open().
diego
parents: 26727
diff changeset
121 static int vf_open(vf_instance_t *vf, char *args){
19094
e84360ee61c9 new black frame detection filter
ivo
parents:
diff changeset
122 vf->priv = malloc(sizeof(struct vf_priv_s));
e84360ee61c9 new black frame detection filter
ivo
parents:
diff changeset
123 if (!vf->priv) return 0;
e84360ee61c9 new black frame detection filter
ivo
parents:
diff changeset
124
e84360ee61c9 new black frame detection filter
ivo
parents:
diff changeset
125 vf->config = config;
e84360ee61c9 new black frame detection filter
ivo
parents:
diff changeset
126 vf->put_image = put_image;
e84360ee61c9 new black frame detection filter
ivo
parents:
diff changeset
127 vf->control = control;
e84360ee61c9 new black frame detection filter
ivo
parents:
diff changeset
128 vf->uninit = uninit;
e84360ee61c9 new black frame detection filter
ivo
parents:
diff changeset
129 vf->query_format = query_format;
e84360ee61c9 new black frame detection filter
ivo
parents:
diff changeset
130
e84360ee61c9 new black frame detection filter
ivo
parents:
diff changeset
131 vf->priv->bamount = 98;
e84360ee61c9 new black frame detection filter
ivo
parents:
diff changeset
132 vf->priv->bthresh = 0x20;
e84360ee61c9 new black frame detection filter
ivo
parents:
diff changeset
133 vf->priv->frame = 0;
19096
30084f256717 print frame type and keep track of last encountered keyframe
ivo
parents: 19094
diff changeset
134 vf->priv->lastkeyframe = 0;
19094
e84360ee61c9 new black frame detection filter
ivo
parents:
diff changeset
135
e84360ee61c9 new black frame detection filter
ivo
parents:
diff changeset
136 if (args)
e84360ee61c9 new black frame detection filter
ivo
parents:
diff changeset
137 sscanf(args, "%u:%u", &vf->priv->bamount, &vf->priv->bthresh);
e84360ee61c9 new black frame detection filter
ivo
parents:
diff changeset
138 return 1;
e84360ee61c9 new black frame detection filter
ivo
parents:
diff changeset
139 }
e84360ee61c9 new black frame detection filter
ivo
parents:
diff changeset
140
24969
c2b7ba444ade begin moving const filter data to .text/.rodata sections
rfelker
parents: 19096
diff changeset
141 const vf_info_t vf_info_blackframe = {
19094
e84360ee61c9 new black frame detection filter
ivo
parents:
diff changeset
142 "detects black frames",
e84360ee61c9 new black frame detection filter
ivo
parents:
diff changeset
143 "blackframe",
e84360ee61c9 new black frame detection filter
ivo
parents:
diff changeset
144 "Brian J. Murrell, Julian Hall, Ivo van Poorten",
e84360ee61c9 new black frame detection filter
ivo
parents:
diff changeset
145 "Useful for detecting scene transitions",
30638
a7b908875c14 Rename open() vf initialization function to vf_open().
diego
parents: 26727
diff changeset
146 vf_open,
19094
e84360ee61c9 new black frame detection filter
ivo
parents:
diff changeset
147 NULL
e84360ee61c9 new black frame detection filter
ivo
parents:
diff changeset
148 };