1 /*****************************************************************************
2 * video filter: video filter doing chroma conversion and resizing
3 * using the ffmpeg library
4 *****************************************************************************
5 * Copyright (C) 1999-2001 the VideoLAN team
8 * Authors: Gildas Bazin <gbazin@videolan.org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
33 #include <vlc_codec.h>
35 #include <vlc_filter.h>
38 #ifdef HAVE_FFMPEG_AVCODEC_H
39 # include <ffmpeg/avcodec.h>
46 #if !defined(HAVE_FFMPEG_SWSCALE_H) && !defined(HAVE_LIBSWSCALE_TREE)
47 void E_(InitLibavcodec) ( vlc_object_t *p_object );
48 static int CheckInit( filter_t *p_filter );
49 static picture_t *Process( filter_t *p_filter, picture_t *p_pic );
51 /*****************************************************************************
52 * filter_sys_t : filter descriptor
53 *****************************************************************************/
58 vlc_bool_t b_resize_first;
59 vlc_bool_t b_enable_croppadd;
62 int i_src_ffmpeg_chroma;
64 int i_dst_ffmpeg_chroma;
68 ImgReSampleContext *p_rsc;
71 /*****************************************************************************
72 * OpenFilterEx: common code to OpenFilter and OpenCropPadd
73 *****************************************************************************/
74 static int OpenFilterEx( vlc_object_t *p_this, vlc_bool_t b_enable_croppadd )
76 filter_t *p_filter = (filter_t*)p_this;
78 vlc_bool_t b_convert, b_resize;
80 /* Check if we can handle that formats */
81 if( E_(GetFfmpegChroma)( p_filter->fmt_in.video.i_chroma ) < 0 ||
82 E_(GetFfmpegChroma)( p_filter->fmt_out.video.i_chroma ) < 0 )
88 ( p_filter->fmt_in.video.i_width != p_filter->fmt_out.video.i_width ) ||
89 ( p_filter->fmt_in.video.i_height != p_filter->fmt_out.video.i_height );
91 if ( b_enable_croppadd )
93 b_resize = b_resize ||
94 ( p_filter->fmt_in.video.i_visible_width != p_filter->fmt_in.video.i_width ) ||
95 ( p_filter->fmt_in.video.i_visible_height != p_filter->fmt_in.video.i_height ) ||
96 ( p_filter->fmt_in.video.i_x_offset != 0 ) ||
97 ( p_filter->fmt_in.video.i_y_offset != 0 ) ||
98 ( p_filter->fmt_out.video.i_visible_width != p_filter->fmt_out.video.i_width ) ||
99 ( p_filter->fmt_out.video.i_visible_height != p_filter->fmt_out.video.i_height ) ||
100 ( p_filter->fmt_out.video.i_x_offset != 0 ) ||
101 ( p_filter->fmt_out.video.i_y_offset != 0 );
105 ( p_filter->fmt_in.video.i_chroma != p_filter->fmt_out.video.i_chroma );
107 if( !b_resize && !b_convert )
113 /* Allocate the memory needed to store the decoder's structure */
114 if( ( p_filter->p_sys = p_sys =
115 (filter_sys_t *)malloc(sizeof(filter_sys_t)) ) == NULL )
117 msg_Err( p_filter, "out of memory" );
123 p_sys->b_enable_croppadd = b_enable_croppadd;
124 p_sys->i_src_ffmpeg_chroma =
125 E_(GetFfmpegChroma)( p_filter->fmt_in.video.i_chroma );
126 p_sys->i_dst_ffmpeg_chroma =
127 E_(GetFfmpegChroma)( p_filter->fmt_out.video.i_chroma );
128 p_filter->pf_video_filter = Process;
129 es_format_Init( &p_sys->fmt_in, 0, 0 );
130 es_format_Init( &p_sys->fmt_out, 0, 0 );
132 /* Dummy alloc, will be reallocated in CheckInit */
133 avpicture_alloc( &p_sys->tmp_pic, p_sys->i_src_ffmpeg_chroma,
134 p_filter->fmt_out.video.i_width,
135 p_filter->fmt_out.video.i_height );
137 if( CheckInit( p_filter ) != VLC_SUCCESS )
143 msg_Dbg( p_filter, "input: %ix%i %4.4s -> %ix%i %4.4s",
144 p_filter->fmt_in.video.i_width, p_filter->fmt_in.video.i_height,
145 (char *)&p_filter->fmt_in.video.i_chroma,
146 p_filter->fmt_out.video.i_width, p_filter->fmt_out.video.i_height,
147 (char *)&p_filter->fmt_out.video.i_chroma );
149 /* libavcodec needs to be initialized for some chroma conversions */
150 E_(InitLibavcodec)(p_this);
155 /*****************************************************************************
156 * OpenFilter: probe the filter and return score
157 *****************************************************************************/
158 int E_(OpenFilter)( vlc_object_t *p_this )
160 return OpenFilterEx( p_this, VLC_FALSE );
163 /*****************************************************************************
164 * OpenCropPadd: probe the filter and return score
165 *****************************************************************************/
166 int E_(OpenCropPadd)( vlc_object_t *p_this )
168 return OpenFilterEx( p_this, VLC_TRUE );
172 /*****************************************************************************
173 * CloseFilter: clean up the filter
174 *****************************************************************************/
175 void E_(CloseFilter)( vlc_object_t *p_this )
177 filter_t *p_filter = (filter_t*)p_this;
178 filter_sys_t *p_sys = p_filter->p_sys;
180 if( p_sys->p_rsc ) img_resample_close( p_sys->p_rsc );
181 avpicture_free( &p_sys->tmp_pic );
186 /*****************************************************************************
187 * CheckInit: Initialise filter when necessary
188 *****************************************************************************/
189 static int CheckInit( filter_t *p_filter )
191 filter_sys_t *p_sys = p_filter->p_sys;
202 b_change = ( p_filter->fmt_in.video.i_width != p_sys->fmt_in.video.i_width ) ||
203 ( p_filter->fmt_in.video.i_height != p_sys->fmt_in.video.i_height ) ||
204 ( p_filter->fmt_out.video.i_width != p_sys->fmt_out.video.i_width ) ||
205 ( p_filter->fmt_out.video.i_height != p_sys->fmt_out.video.i_height );
207 if ( p_sys->b_enable_croppadd )
209 b_change = b_change ||
210 ( p_filter->fmt_in.video.i_y_offset != p_sys->fmt_in.video.i_y_offset ) ||
211 ( p_filter->fmt_in.video.i_x_offset != p_sys->fmt_in.video.i_x_offset ) ||
212 ( p_filter->fmt_in.video.i_visible_width != p_sys->fmt_in.video.i_visible_width ) ||
213 ( p_filter->fmt_in.video.i_visible_height != p_sys->fmt_in.video.i_visible_height ) ||
214 ( p_filter->fmt_out.video.i_y_offset != p_sys->fmt_out.video.i_y_offset ) ||
215 ( p_filter->fmt_out.video.i_x_offset != p_sys->fmt_out.video.i_x_offset ) ||
216 ( p_filter->fmt_out.video.i_visible_width != p_sys->fmt_out.video.i_visible_width ) ||
217 ( p_filter->fmt_out.video.i_visible_height != p_sys->fmt_out.video.i_visible_height );
222 if( p_sys->p_rsc ) img_resample_close( p_sys->p_rsc );
226 ( p_filter->fmt_in.video.i_chroma != p_filter->fmt_out.video.i_chroma );
229 ( p_filter->fmt_in.video.i_width != p_filter->fmt_out.video.i_width ) ||
230 ( p_filter->fmt_in.video.i_height != p_filter->fmt_out.video.i_height );
232 p_sys->b_resize_first =
233 ( p_filter->fmt_in.video.i_width * p_filter->fmt_in.video.i_height ) >
234 ( p_filter->fmt_out.video.i_width * p_filter->fmt_out.video.i_height );
236 if( p_sys->b_resize &&
237 ( p_sys->i_src_ffmpeg_chroma != PIX_FMT_YUV420P ) &&
238 ( p_sys->i_src_ffmpeg_chroma != PIX_FMT_YUVJ420P ) &&
239 ( p_sys->i_dst_ffmpeg_chroma != PIX_FMT_YUV420P ) &&
240 ( p_sys->i_dst_ffmpeg_chroma != PIX_FMT_YUVJ420P ) )
242 msg_Err( p_filter, "img_resample_init only deals with I420" );
245 else if( ( p_sys->i_src_ffmpeg_chroma != PIX_FMT_YUV420P ) &&
246 ( p_sys->i_src_ffmpeg_chroma != PIX_FMT_YUVJ420P ) )
248 p_sys->b_resize_first = VLC_FALSE;
250 else if( ( p_sys->i_dst_ffmpeg_chroma != PIX_FMT_YUV420P ) &&
251 ( p_sys->i_dst_ffmpeg_chroma != PIX_FMT_YUVJ420P ) )
253 p_sys->b_resize_first = VLC_TRUE;
256 if ( p_sys->b_enable_croppadd )
258 p_sys->b_resize = p_sys->b_resize ||
259 ( p_filter->fmt_in.video.i_visible_width != p_filter->fmt_in.video.i_width ) ||
260 ( p_filter->fmt_in.video.i_visible_height != p_filter->fmt_in.video.i_height ) ||
261 ( p_filter->fmt_in.video.i_x_offset != 0 ) ||
262 ( p_filter->fmt_in.video.i_y_offset != 0 ) ||
263 ( p_filter->fmt_out.video.i_visible_width != p_filter->fmt_out.video.i_width ) ||
264 ( p_filter->fmt_out.video.i_visible_height != p_filter->fmt_out.video.i_height ) ||
265 ( p_filter->fmt_out.video.i_x_offset != 0 ) ||
266 ( p_filter->fmt_out.video.i_y_offset != 0 );
269 if( p_sys->b_resize )
271 if ( p_sys->b_enable_croppadd )
273 i_croptop = p_filter->fmt_in.video.i_y_offset;
274 i_cropbottom = p_filter->fmt_in.video.i_height
275 - p_filter->fmt_in.video.i_visible_height
276 - p_filter->fmt_in.video.i_y_offset;
277 i_cropleft = p_filter->fmt_in.video.i_x_offset;
278 i_cropright = p_filter->fmt_in.video.i_width
279 - p_filter->fmt_in.video.i_visible_width
280 - p_filter->fmt_in.video.i_x_offset;
283 i_paddtop = p_filter->fmt_out.video.i_y_offset;
284 i_paddbottom = p_filter->fmt_out.video.i_height
285 - p_filter->fmt_out.video.i_visible_height
286 - p_filter->fmt_out.video.i_y_offset;
287 i_paddleft = p_filter->fmt_out.video.i_x_offset;
288 i_paddright = p_filter->fmt_out.video.i_width
289 - p_filter->fmt_out.video.i_visible_width
290 - p_filter->fmt_out.video.i_x_offset;
293 p_sys->p_rsc = img_resample_full_init(
294 p_filter->fmt_out.video.i_width,
295 p_filter->fmt_out.video.i_height,
296 p_filter->fmt_in.video.i_width,
297 p_filter->fmt_in.video.i_height,
298 i_croptop,i_cropbottom,
299 i_cropleft,i_cropright,
300 i_paddtop,i_paddbottom,
301 i_paddleft,i_paddright );
305 msg_Err( p_filter, "img_resample_init failed" );
309 msg_Dbg( p_filter, "input: %ix%i -> %ix%i",
310 p_filter->fmt_out.video.i_width,
311 p_filter->fmt_out.video.i_height,
312 p_filter->fmt_in.video.i_width,
313 p_filter->fmt_in.video.i_height);
317 avpicture_free( &p_sys->tmp_pic );
319 if( p_sys->b_resize_first )
321 /* Resizing then conversion */
322 avpicture_alloc( &p_sys->tmp_pic, p_sys->i_src_ffmpeg_chroma,
323 p_filter->fmt_out.video.i_width,
324 p_filter->fmt_out.video.i_height );
328 /* Conversion then resizing */
329 avpicture_alloc( &p_sys->tmp_pic, p_sys->i_dst_ffmpeg_chroma,
330 p_filter->fmt_in.video.i_width,
331 p_filter->fmt_in.video.i_height );
334 p_sys->fmt_in = p_filter->fmt_in;
335 p_sys->fmt_out = p_filter->fmt_out;
341 /* fill padd code from ffmpeg */
342 static int padcolor[3] = { 16, 128, 128 };
344 /* Expects img to be yuv420 */
345 static void fill_pad_region( AVPicture* img, int height, int width,
346 int padtop, int padbottom, int padleft, int padright, int *color )
351 for ( i = 0; i < 3; i++ )
353 shift = ( i == 0 ) ? 0 : 1;
355 if ( padtop || padleft )
357 memset( img->data[i], color[i], ( ( ( img->linesize[i] * padtop ) +
358 padleft ) >> shift) );
361 if ( padleft || padright )
363 optr = img->data[i] + ( img->linesize[i] * ( padtop >> shift ) ) +
364 ( img->linesize[i] - ( padright >> shift ) );
366 for ( y = 0; y < ( ( height - ( padtop + padbottom ) ) >> shift ); y++ )
368 memset( optr, color[i], ( padleft + padright ) >> shift );
369 optr += img->linesize[i];
375 optr = img->data[i] + ( img->linesize[i] * ( ( height - padbottom ) >> shift ) );
376 memset( optr, color[i], ( ( img->linesize[i] * padbottom ) >> shift ) );
381 /* Workaround, because old libavcodec doesnt know how to padd */
382 static void img_resample_padd( ImgReSampleContext *s, AVPicture *output,
383 const AVPicture *input, int padtop, int padleft )
385 AVPicture nopadd_pic = *output;
387 /* shift out top and left padding for old ffmpeg */
388 nopadd_pic.data[0] += ( nopadd_pic.linesize[0] * padtop + padleft );
389 nopadd_pic.data[1] += ( nopadd_pic.linesize[1] * padtop + padleft ) >> 1;
390 nopadd_pic.data[2] += ( nopadd_pic.linesize[2] * padtop + padleft ) >> 1;
391 img_resample( s, &nopadd_pic, input );
395 /*****************************************************************************
396 * Do the processing here
397 *****************************************************************************/
398 static picture_t *Process( filter_t *p_filter, picture_t *p_pic )
400 filter_sys_t *p_sys = p_filter->p_sys;
401 AVPicture src_pic, dest_pic;
402 AVPicture *p_src, *p_dst;
403 picture_t *p_pic_dst;
406 /* Check if format properties changed */
407 if( CheckInit( p_filter ) != VLC_SUCCESS ) return 0;
409 /* Request output picture */
410 p_pic_dst = p_filter->pf_vout_buffer_new( p_filter );
413 msg_Warn( p_filter, "can't get output picture" );
414 p_pic->pf_release( p_pic );
418 /* Prepare the AVPictures for the conversion */
419 for( i = 0; i < p_pic->i_planes; i++ )
421 src_pic.data[i] = p_pic->p[i].p_pixels;
422 src_pic.linesize[i] = p_pic->p[i].i_pitch;
424 for( i = 0; i < p_pic_dst->i_planes; i++ )
426 dest_pic.data[i] = p_pic_dst->p[i].p_pixels;
427 dest_pic.linesize[i] = p_pic_dst->p[i].i_pitch;
431 if( p_filter->fmt_in.video.i_chroma == VLC_FOURCC('Y','V','1','2') ||
432 p_filter->fmt_in.video.i_chroma == VLC_FOURCC('Y','V','U','9') )
435 src_pic.data[1] = p_pic->p[2].p_pixels;
436 src_pic.data[2] = p_pic->p[1].p_pixels;
438 if( p_filter->fmt_out.video.i_chroma == VLC_FOURCC('Y','V','1','2') ||
439 p_filter->fmt_out.video.i_chroma == VLC_FOURCC('Y','V','U','9') )
442 dest_pic.data[1] = p_pic_dst->p[2].p_pixels;
443 dest_pic.data[2] = p_pic_dst->p[1].p_pixels;
445 if( p_sys->i_src_ffmpeg_chroma == PIX_FMT_RGB24 )
446 if( p_filter->fmt_in.video.i_bmask == 0x00ff0000 )
447 p_sys->i_src_ffmpeg_chroma = PIX_FMT_BGR24;
451 if( p_sys->b_resize && p_sys->p_rsc )
454 if( p_sys->b_resize_first )
456 if( p_sys->b_convert ) p_dst = &p_sys->tmp_pic;
458 img_resample( p_sys->p_rsc, p_dst, p_src );
460 if (p_sys->b_enable_croppadd)
462 if (p_filter->fmt_out.video.i_visible_width != p_filter->fmt_out.video.i_width ||
463 p_filter->fmt_out.video.i_visible_height != p_filter->fmt_out.video.i_height ||
464 p_filter->fmt_out.video.i_x_offset != 0 ||
465 p_filter->fmt_out.video.i_y_offset != 0)
467 fill_pad_region(p_dst, p_filter->fmt_out.video.i_height,
468 p_filter->fmt_out.video.i_width,
469 p_filter->fmt_out.video.i_y_offset,
470 p_filter->fmt_out.video.i_height
471 - p_filter->fmt_out.video.i_visible_height
472 - p_filter->fmt_out.video.i_y_offset,
473 p_filter->fmt_out.video.i_x_offset,
474 p_filter->fmt_out.video.i_width
475 - p_filter->fmt_out.video.i_visible_width
476 - p_filter->fmt_out.video.i_x_offset,
485 if( p_sys->b_convert )
487 video_format_t *p_fmt = &p_filter->fmt_out.video;
489 if( p_sys->b_resize && !p_sys->b_resize_first )
491 p_dst = &p_sys->tmp_pic;
492 p_fmt = &p_filter->fmt_in.video;
495 img_convert( p_dst, p_sys->i_dst_ffmpeg_chroma,
496 p_src, p_sys->i_src_ffmpeg_chroma,
497 p_fmt->i_width, p_fmt->i_height );
502 if( p_sys->b_resize && !p_sys->b_resize_first && p_sys->p_rsc )
506 img_resample( p_sys->p_rsc, p_dst, p_src );
508 if (p_sys->b_enable_croppadd)
510 if (p_filter->fmt_out.video.i_visible_width != p_filter->fmt_out.video.i_width ||
511 p_filter->fmt_out.video.i_visible_height != p_filter->fmt_out.video.i_height ||
512 p_filter->fmt_out.video.i_x_offset != 0 ||
513 p_filter->fmt_out.video.i_y_offset != 0)
515 fill_pad_region(p_dst, p_filter->fmt_out.video.i_height,
516 p_filter->fmt_out.video.i_width,
517 p_filter->fmt_out.video.i_y_offset,
518 p_filter->fmt_out.video.i_height
519 - p_filter->fmt_out.video.i_visible_height
520 - p_filter->fmt_out.video.i_y_offset,
521 p_filter->fmt_out.video.i_x_offset,
522 p_filter->fmt_out.video.i_width
523 - p_filter->fmt_out.video.i_visible_width
524 - p_filter->fmt_out.video.i_x_offset,
530 /* Special case for RV32 -> YUVA */
531 if( !p_sys->b_resize &&
532 p_filter->fmt_in.video.i_chroma == VLC_FOURCC('R','V','3','2') &&
533 p_filter->fmt_out.video.i_chroma == VLC_FOURCC('Y','U','V','A') )
535 uint8_t *p_src = p_pic->p[0].p_pixels;
536 int i_src_pitch = p_pic->p[0].i_pitch;
537 uint8_t *p_dst = p_pic_dst->p[3].p_pixels;
538 int i_dst_pitch = p_pic_dst->p[3].i_pitch;
541 for( l = 0; l < p_filter->fmt_out.video.i_height; l++ )
543 for( j = 0; j < p_filter->fmt_out.video.i_width; j++ )
545 p_dst[j] = p_src[j*4+3];
547 p_src += i_src_pitch;
548 p_dst += i_dst_pitch;
551 else if( p_filter->fmt_out.video.i_chroma == VLC_FOURCC('Y','U','V','A') )
553 /* Special case for YUVA */
554 memset( p_pic_dst->p[3].p_pixels, 0xFF,
555 p_pic_dst->p[3].i_pitch * p_pic_dst->p[3].i_lines );
558 p_pic_dst->date = p_pic->date;
559 p_pic_dst->b_force = p_pic->b_force;
560 p_pic_dst->i_nb_fields = p_pic->i_nb_fields;
561 p_pic_dst->b_progressive = p_pic->b_progressive;
562 p_pic_dst->b_top_field_first = p_pic->b_top_field_first;
564 p_pic->pf_release( p_pic );
567 #endif /* ( (defined(HAVE_FFMPEG_SWSCALE_H) || defined(HAVE_LIBSWSCALE_TREE)) */