• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

libavfilter/libmpcodecs/vf_blackframe.c

Go to the documentation of this file.
00001 /*
00002  * detect frames that are (almost) black
00003  * search for black frames to detect scene transitions
00004  * (c) 2006 Julian Hall
00005  *
00006  * based on code designed for skipping commercials
00007  * (c) 2002-2003 Brian J. Murrell
00008  *
00009  * cleanup, simplify, speedup (c) 2006 by Ivo van Poorten
00010  *
00011  * This file is part of MPlayer.
00012  *
00013  * MPlayer is free software; you can redistribute it and/or modify
00014  * it under the terms of the GNU General Public License as published by
00015  * the Free Software Foundation; either version 2 of the License, or
00016  * (at your option) any later version.
00017  *
00018  * MPlayer is distributed in the hope that it will be useful,
00019  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00020  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00021  * GNU General Public License for more details.
00022  *
00023  * You should have received a copy of the GNU General Public License along
00024  * with MPlayer; if not, write to the Free Software Foundation, Inc.,
00025  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
00026  */
00027 
00028 #include <stdio.h>
00029 #include <stdlib.h>
00030 #include <string.h>
00031 
00032 #include "config.h"
00033 #include "mp_msg.h"
00034 
00035 #include "img_format.h"
00036 #include "mp_image.h"
00037 #include "vf.h"
00038 
00039 struct vf_priv_s {
00040     unsigned int bamount, bthresh, frame, lastkeyframe;
00041 };
00042 
00043 static int config(struct vf_instance *vf, int width, int height, int d_width,
00044                     int d_height, unsigned int flags, unsigned int outfmt) {
00045     return vf_next_config(vf,width,height,d_width,d_height,flags,outfmt);
00046 }
00047 
00048 static int query_format(struct vf_instance *vf, unsigned fmt) {
00049     switch(fmt) {
00050     case IMGFMT_YVU9:
00051     case IMGFMT_IF09:
00052     case IMGFMT_YV12:
00053     case IMGFMT_I420:
00054     case IMGFMT_IYUV:
00055     case IMGFMT_CLPL:
00056     case IMGFMT_Y800:
00057     case IMGFMT_Y8:
00058     case IMGFMT_NV12:
00059     case IMGFMT_NV21:
00060     case IMGFMT_444P:
00061     case IMGFMT_422P:
00062     case IMGFMT_411P:
00063     case IMGFMT_HM12:
00064         return vf_next_query_format(vf, fmt);
00065     }
00066     return 0;
00067 }
00068 
00069 static int put_image(struct vf_instance *vf, mp_image_t *mpi, double pts){
00070     mp_image_t *dmpi;
00071     int x, y;
00072     int nblack=0, pblack=0;
00073     unsigned char *yplane = mpi->planes[0];
00074     unsigned int ystride = mpi->stride[0];
00075     int pict_type = mpi->pict_type;
00076     int w = mpi->w, h = mpi->h;
00077     int bthresh = vf->priv->bthresh;
00078     int bamount = vf->priv->bamount;
00079     static const char *const picttypes[4] = { "unknown", "I", "P", "B" };
00080 
00081     for (y=1; y<=h; y++) {
00082         for (x=0; x<w; x++)
00083             nblack += yplane[x] < bthresh;
00084         pblack = nblack*100/(w*y);
00085         if (pblack < bamount) break;
00086         yplane += ystride;
00087     }
00088 
00089     if (pict_type > 3 || pict_type < 0) pict_type = 0;
00090     if (pict_type == 1) vf->priv->lastkeyframe = vf->priv->frame;
00091 
00092     if (pblack >= bamount)
00093         mp_msg(MSGT_VFILTER, MSGL_INFO,"vf_blackframe: %u, %i%%, %s (I:%u)\n",
00094                                 vf->priv->frame, pblack, picttypes[pict_type],
00095                                 vf->priv->lastkeyframe);
00096 
00097     vf->priv->frame++;
00098 
00099     dmpi = vf_get_image(vf->next, mpi->imgfmt, MP_IMGTYPE_EXPORT, 0,
00100                                                     mpi->width, mpi->height);
00101     dmpi->planes[0] = mpi->planes[0];
00102     dmpi->stride[0] = mpi->stride[0];
00103     dmpi->planes[1] = mpi->planes[1];
00104     dmpi->stride[1] = mpi->stride[1];
00105     dmpi->planes[2] = mpi->planes[2];
00106     dmpi->stride[2] = mpi->stride[2];
00107 
00108     vf_clone_mpi_attributes(dmpi, mpi);
00109 
00110     return vf_next_put_image(vf, dmpi, pts);
00111 }
00112 
00113 static int control(struct vf_instance *vf, int request, void* data){
00114     return vf_next_control(vf,request,data);
00115 }
00116 
00117 static void uninit(struct vf_instance *vf) {
00118     free(vf->priv);
00119 }
00120 
00121 static int vf_open(vf_instance_t *vf, char *args){
00122     vf->priv = malloc(sizeof(struct vf_priv_s));
00123     if (!vf->priv) return 0;
00124 
00125     vf->config = config;
00126     vf->put_image = put_image;
00127     vf->control = control;
00128     vf->uninit = uninit;
00129     vf->query_format = query_format;
00130 
00131     vf->priv->bamount = 98;
00132     vf->priv->bthresh = 0x20;
00133     vf->priv->frame = 0;
00134     vf->priv->lastkeyframe = 0;
00135 
00136     if (args)
00137         sscanf(args, "%u:%u", &vf->priv->bamount, &vf->priv->bthresh);
00138     return 1;
00139 }
00140 
00141 const vf_info_t vf_info_blackframe = {
00142     "detects black frames",
00143     "blackframe",
00144     "Brian J. Murrell, Julian Hall, Ivo van Poorten",
00145     "Useful for detecting scene transitions",
00146     vf_open,
00147     NULL
00148 };

Generated on Fri Feb 22 2013 07:24:30 for FFmpeg by  doxygen 1.7.1