video_filter.c 13.1 KB
Newer Older
1 2 3 4 5
/*****************************************************************************
 * video filter: video filter doing chroma conversion and resizing
 *               using the ffmpeg library
 *****************************************************************************
 * Copyright (C) 1999-2001 VideoLAN
6
 * $Id$
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
 *
 * Authors: Gildas Bazin <gbazin@videolan.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
#include <vlc/vlc.h>
#include <vlc/decoder.h>
#include "vlc_filter.h"

/* ffmpeg header */
#ifdef HAVE_FFMPEG_AVCODEC_H
#   include <ffmpeg/avcodec.h>
#else
#   include <avcodec.h>
#endif

#include "ffmpeg.h"

void E_(InitLibavcodec) ( vlc_object_t *p_object );
42
static int CheckInit( filter_t *p_filter );
43
static picture_t *Process( filter_t *p_filter, picture_t *p_pic );
44
static picture_t *Deinterlace( filter_t *p_filter, picture_t *p_pic );
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99

/*****************************************************************************
 * filter_sys_t : filter descriptor
 *****************************************************************************/
struct filter_sys_t
{
    vlc_bool_t b_resize;
    vlc_bool_t b_convert;

    es_format_t fmt_in;
    int i_src_ffmpeg_chroma;
    es_format_t fmt_out;
    int i_dst_ffmpeg_chroma;

    AVPicture tmp_pic;
    ImgReSampleContext *p_rsc;
};

/*****************************************************************************
 * OpenFilter: probe the filter and return score
 *****************************************************************************/
int E_(OpenFilter)( vlc_object_t *p_this )
{
    filter_t *p_filter = (filter_t*)p_this;
    filter_sys_t *p_sys;
    vlc_bool_t b_convert, b_resize;

    /* Check if we can handle that formats */
    if( E_(GetFfmpegChroma)( p_filter->fmt_in.video.i_chroma ) < 0 ||
        E_(GetFfmpegChroma)( p_filter->fmt_out.video.i_chroma ) < 0 )
    {
        return VLC_EGENERIC;
    }

    b_resize =
        p_filter->fmt_in.video.i_width != p_filter->fmt_out.video.i_width ||
        p_filter->fmt_in.video.i_height != p_filter->fmt_out.video.i_height;
    b_convert =
        p_filter->fmt_in.video.i_chroma != p_filter->fmt_out.video.i_chroma;

    if( !b_resize && !b_convert )
    {
        /* Nothing to do */
        return VLC_EGENERIC;
    }

    /* Allocate the memory needed to store the decoder's structure */
    if( ( p_filter->p_sys = p_sys =
          (filter_sys_t *)malloc(sizeof(filter_sys_t)) ) == NULL )
    {
        msg_Err( p_filter, "out of memory" );
        return VLC_EGENERIC;
    }

    /* Misc init */
100
    p_sys->p_rsc = NULL;
101 102 103 104 105 106
    p_sys->b_convert = b_convert;
    p_sys->i_src_ffmpeg_chroma =
        E_(GetFfmpegChroma)( p_filter->fmt_in.video.i_chroma );
    p_sys->i_dst_ffmpeg_chroma =
        E_(GetFfmpegChroma)( p_filter->fmt_out.video.i_chroma );
    p_filter->pf_video_filter = Process;
107 108 109 110 111 112 113 114
    es_format_Init( &p_sys->fmt_in, 0, 0 );
    es_format_Init( &p_sys->fmt_out, 0, 0 );

    if( CheckInit( p_filter ) != VLC_SUCCESS )
    {
        free( p_sys );
        return VLC_EGENERIC;
    }
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146

    avpicture_alloc( &p_sys->tmp_pic, p_sys->i_dst_ffmpeg_chroma,
                     p_filter->fmt_in.video.i_width,
                     p_filter->fmt_in.video.i_height );

    msg_Dbg( p_filter, "input: %ix%i %4.4s -> %ix%i %4.4s",
             p_filter->fmt_in.video.i_width, p_filter->fmt_in.video.i_height,
             (char *)&p_filter->fmt_in.video.i_chroma,
             p_filter->fmt_out.video.i_width, p_filter->fmt_out.video.i_height,
             (char *)&p_filter->fmt_out.video.i_chroma );

    /* libavcodec needs to be initialized for some chroma conversions */
    E_(InitLibavcodec)(p_this);

    return VLC_SUCCESS;
}

/*****************************************************************************
 * CloseFilter: clean up the filter
 *****************************************************************************/
void E_(CloseFilter)( vlc_object_t *p_this )
{
    filter_t *p_filter = (filter_t*)p_this;
    filter_sys_t *p_sys = p_filter->p_sys;

    if( p_sys->p_rsc ) img_resample_close( p_sys->p_rsc );

    avpicture_free( &p_sys->tmp_pic );

    free( p_sys );
}

147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
/*****************************************************************************
 * CheckInit: Initialise filter when necessary
 *****************************************************************************/
static int CheckInit( filter_t *p_filter )
{
    filter_sys_t *p_sys = p_filter->p_sys;

    if( p_filter->fmt_in.video.i_width != p_sys->fmt_in.video.i_width ||
        p_filter->fmt_in.video.i_height != p_sys->fmt_in.video.i_height ||
        p_filter->fmt_out.video.i_width != p_sys->fmt_out.video.i_width ||
        p_filter->fmt_out.video.i_height != p_sys->fmt_out.video.i_height )
    {
        if( p_sys->p_rsc ) img_resample_close( p_sys->p_rsc );
        p_sys->p_rsc = 0;

        p_sys->b_resize =
          p_filter->fmt_in.video.i_width != p_filter->fmt_out.video.i_width ||
          p_filter->fmt_in.video.i_height != p_filter->fmt_out.video.i_height;

        if( p_sys->b_resize )
        {
            p_sys->p_rsc = img_resample_init( p_filter->fmt_out.video.i_width,
                               p_filter->fmt_out.video.i_height,
                               p_filter->fmt_in.video.i_width,
                               p_filter->fmt_in.video.i_height );

            if( !p_sys->p_rsc )
            {
                msg_Err( p_filter, "img_resample_init failed" );
                return VLC_EGENERIC;
            }
        }

        p_sys->fmt_in = p_filter->fmt_in;
        p_sys->fmt_out = p_filter->fmt_out;
    }

    return VLC_SUCCESS;
}

187 188 189 190 191 192 193 194 195 196 197
/*****************************************************************************
 * Do the processing here
 *****************************************************************************/
static picture_t *Process( filter_t *p_filter, picture_t *p_pic )
{
    filter_sys_t *p_sys = p_filter->p_sys;
    AVPicture src_pic, dest_pic, inter_pic;
    picture_t *p_pic_dst;
    vlc_bool_t b_resize = p_sys->b_resize;
    int i;

198 199 200
    /* Check if format properties changed */
    if( CheckInit( p_filter ) != VLC_SUCCESS ) return 0;

201 202 203 204 205
    /* Request output picture */
    p_pic_dst = p_filter->pf_vout_buffer_new( p_filter );
    if( !p_pic_dst )
    {
        msg_Warn( p_filter, "can't get output picture" );
206
        p_pic->pf_release( p_pic );
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
        return NULL;
    }

    /* Prepare the AVPictures for the conversion */
    for( i = 0; i < p_pic->i_planes; i++ )
    {
        src_pic.data[i] = p_pic->p[i].p_pixels;
        src_pic.linesize[i] = p_pic->p[i].i_pitch;
    }
    for( i = 0; i < p_pic_dst->i_planes; i++ )
    {
        dest_pic.data[i] = p_pic_dst->p[i].p_pixels;
        dest_pic.linesize[i] = p_pic_dst->p[i].i_pitch;
    }

    /* Special cases */
    if( p_filter->fmt_in.video.i_chroma == VLC_FOURCC('Y','V','1','2') ||
        p_filter->fmt_in.video.i_chroma == VLC_FOURCC('Y','V','U','9') )
    {
        /* Invert U and V */
        src_pic.data[1] = p_pic->p[2].p_pixels;
        src_pic.data[2] = p_pic->p[1].p_pixels;
    }
    if( p_filter->fmt_out.video.i_chroma == VLC_FOURCC('Y','V','1','2') ||
        p_filter->fmt_out.video.i_chroma == VLC_FOURCC('Y','V','U','9') )
    {
        /* Invert U and V */
        dest_pic.data[1] = p_pic_dst->p[2].p_pixels;
        dest_pic.data[2] = p_pic_dst->p[1].p_pixels;
    }
237 238 239
    if( p_sys->i_src_ffmpeg_chroma == PIX_FMT_RGB24 )
        if( p_filter->fmt_in.video.i_bmask == 0x00ff0000 )
            p_sys->i_src_ffmpeg_chroma = PIX_FMT_BGR24;
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268

#if 0
    if( p_sys->b_resize &&
        p_filter->fmt_in.video.i_width * p_filter->fmt_in.video.i_height >
        p_filter->fmt_out.video.i_width * p_filter->fmt_out.video.i_height )
    {
        img_resample( p_sys->p_rsc, &dest_pic, &p_sys->tmp_pic );
        b_resize = 0;
    }
#endif

    if( p_sys->b_convert )
    {
        if( p_sys->b_resize ) inter_pic = p_sys->tmp_pic;
        else inter_pic = dest_pic;

        img_convert( &inter_pic, p_sys->i_dst_ffmpeg_chroma,
                     &src_pic, p_sys->i_src_ffmpeg_chroma,
                     p_filter->fmt_in.video.i_width,
                     p_filter->fmt_in.video.i_height );

        src_pic = inter_pic;
    }

    if( p_sys->b_resize && p_sys->p_rsc )
    {
        img_resample( p_sys->p_rsc, &dest_pic, &src_pic );
    }

269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
    /* Special case for RV32 -> YUVA */
    if( !p_sys->b_resize &&
        p_filter->fmt_in.video.i_chroma == VLC_FOURCC('R','V','3','2') &&
        p_filter->fmt_out.video.i_chroma == VLC_FOURCC('Y','U','V','A') )
    {
        uint8_t *p_src = p_pic->p[0].p_pixels;
        int i_src_pitch = p_pic->p[0].i_pitch;
        uint8_t *p_dst = p_pic_dst->p[3].p_pixels;
        int i_dst_pitch = p_pic_dst->p[3].i_pitch;
        int j;

        for( i = 0; i < p_filter->fmt_out.video.i_height; i++ )
        {
            for( j = 0; j < p_filter->fmt_out.video.i_width; j++ )
            {
              p_dst[j] = p_src[j*4+3];
            }
            p_src += i_src_pitch;
            p_dst += i_dst_pitch;
        }
    }
290 291 292 293 294 295
    else if( p_filter->fmt_out.video.i_chroma == VLC_FOURCC('Y','U','V','A') )
    {
        /* Special case for YUVA */
        memset( p_pic_dst->p[3].p_pixels, 0xFF,
                p_pic_dst->p[3].i_pitch * p_pic_dst->p[3].i_lines );
    }
296

297 298 299 300 301 302 303 304 305
    p_pic_dst->date = p_pic->date;
    p_pic_dst->b_force = p_pic->b_force;
    p_pic_dst->i_nb_fields = p_pic->i_nb_fields;
    p_pic_dst->b_progressive = p_pic->b_progressive;
    p_pic_dst->b_top_field_first = p_pic->b_top_field_first;

    p_pic->pf_release( p_pic );
    return p_pic_dst;
}
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383

/*****************************************************************************
 * OpenDeinterlace: probe the filter and return score
 *****************************************************************************/
int E_(OpenDeinterlace)( vlc_object_t *p_this )
{
    filter_t *p_filter = (filter_t*)p_this;
    filter_sys_t *p_sys;

    /* Check if we can handle that formats */
    if( E_(GetFfmpegChroma)( p_filter->fmt_in.video.i_chroma ) < 0 )
    {
        return VLC_EGENERIC;
    }

    /* Allocate the memory needed to store the decoder's structure */
    if( ( p_filter->p_sys = p_sys =
          (filter_sys_t *)malloc(sizeof(filter_sys_t)) ) == NULL )
    {
        msg_Err( p_filter, "out of memory" );
        return VLC_EGENERIC;
    }

    /* Misc init */
    p_sys->i_src_ffmpeg_chroma =
        E_(GetFfmpegChroma)( p_filter->fmt_in.video.i_chroma );
    p_filter->pf_video_filter = Deinterlace;

    msg_Dbg( p_filter, "deinterlacing" );

    /* libavcodec needs to be initialized for some chroma conversions */
    E_(InitLibavcodec)(p_this);

    return VLC_SUCCESS;
}

/*****************************************************************************
 * CloseDeinterlace: clean up the filter
 *****************************************************************************/
void E_(CloseDeinterlace)( vlc_object_t *p_this )
{
    filter_t *p_filter = (filter_t*)p_this;
    filter_sys_t *p_sys = p_filter->p_sys;

    free( p_sys );
}

/*****************************************************************************
 * Do the processing here
 *****************************************************************************/
static picture_t *Deinterlace( filter_t *p_filter, picture_t *p_pic )
{
    filter_sys_t *p_sys = p_filter->p_sys;
    AVPicture src_pic, dest_pic;
    picture_t *p_pic_dst;
    int i;

    /* Request output picture */
    p_pic_dst = p_filter->pf_vout_buffer_new( p_filter );
    if( !p_pic_dst )
    {
        msg_Warn( p_filter, "can't get output picture" );
        return NULL;
    }

    /* Prepare the AVPictures for the conversion */
    for( i = 0; i < p_pic->i_planes; i++ )
    {
        src_pic.data[i] = p_pic->p[i].p_pixels;
        src_pic.linesize[i] = p_pic->p[i].i_pitch;
    }
    for( i = 0; i < p_pic_dst->i_planes; i++ )
    {
        dest_pic.data[i] = p_pic_dst->p[i].p_pixels;
        dest_pic.linesize[i] = p_pic_dst->p[i].i_pitch;
    }

    avpicture_deinterlace( &dest_pic, &src_pic, p_sys->i_src_ffmpeg_chroma,
384 385
                           p_filter->fmt_in.video.i_width,
                           p_filter->fmt_in.video.i_height );
386 387 388 389 390 391 392 393 394 395

    p_pic_dst->date = p_pic->date;
    p_pic_dst->b_force = p_pic->b_force;
    p_pic_dst->i_nb_fields = p_pic->i_nb_fields;
    p_pic_dst->b_progressive = VLC_TRUE;
    p_pic_dst->b_top_field_first = p_pic->b_top_field_first;

    p_pic->pf_release( p_pic );
    return p_pic_dst;
}