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 *****************************************************************************/
29 #include <vlc_codec.h>
31 #include <vlc_filter.h>
34 #ifdef HAVE_FFMPEG_AVCODEC_H
35 # include <ffmpeg/avcodec.h>
43 #if !defined(HAVE_FFMPEG_SWSCALE_H) && !defined(HAVE_LIBSWSCALE_TREE)
44 void E_(InitLibavcodec) ( vlc_object_t *p_object );
45 static int CheckInit( filter_t *p_filter );
46 static picture_t *Process( filter_t *p_filter, picture_t *p_pic );
47 static picture_t *Deinterlace( filter_t *p_filter, picture_t *p_pic );
49 /*****************************************************************************
50 * filter_sys_t : filter descriptor
51 *****************************************************************************/
56 vlc_bool_t b_resize_first;
57 vlc_bool_t b_enable_croppadd;
60 int i_src_ffmpeg_chroma;
62 int i_dst_ffmpeg_chroma;
65 ImgReSampleContext *p_rsc;
69 /*****************************************************************************
70 * OpenFilterEx: common code to OpenFilter and OpenCropPadd
71 *****************************************************************************/
72 static int OpenFilterEx( vlc_object_t *p_this, vlc_bool_t b_enable_croppadd )
74 filter_t *p_filter = (filter_t*)p_this;
76 vlc_bool_t b_convert, b_resize;
78 /* Check if we can handle that formats */
79 if( E_(GetFfmpegChroma)( p_filter->fmt_in.video.i_chroma ) < 0 ||
80 E_(GetFfmpegChroma)( p_filter->fmt_out.video.i_chroma ) < 0 )
86 p_filter->fmt_in.video.i_width != p_filter->fmt_out.video.i_width ||
87 p_filter->fmt_in.video.i_height != p_filter->fmt_out.video.i_height;
89 if ( b_enable_croppadd )
91 b_resize = b_resize ||
92 p_filter->fmt_in.video.i_visible_width != p_filter->fmt_in.video.i_width ||
93 p_filter->fmt_in.video.i_visible_height != p_filter->fmt_in.video.i_height ||
94 p_filter->fmt_in.video.i_x_offset != 0 ||
95 p_filter->fmt_in.video.i_y_offset != 0 ||
96 p_filter->fmt_out.video.i_visible_width != p_filter->fmt_out.video.i_width ||
97 p_filter->fmt_out.video.i_visible_height != p_filter->fmt_out.video.i_height ||
98 p_filter->fmt_out.video.i_x_offset != 0 ||
99 p_filter->fmt_out.video.i_y_offset != 0;
103 p_filter->fmt_in.video.i_chroma != p_filter->fmt_out.video.i_chroma;
105 if( !b_resize && !b_convert )
111 /* Allocate the memory needed to store the decoder's structure */
112 if( ( p_filter->p_sys = p_sys =
113 (filter_sys_t *)malloc(sizeof(filter_sys_t)) ) == NULL )
115 msg_Err( p_filter, "out of memory" );
121 p_sys->b_enable_croppadd = b_enable_croppadd;
122 p_sys->i_src_ffmpeg_chroma =
123 E_(GetFfmpegChroma)( p_filter->fmt_in.video.i_chroma );
124 p_sys->i_dst_ffmpeg_chroma =
125 E_(GetFfmpegChroma)( p_filter->fmt_out.video.i_chroma );
126 p_filter->pf_video_filter = Process;
127 es_format_Init( &p_sys->fmt_in, 0, 0 );
128 es_format_Init( &p_sys->fmt_out, 0, 0 );
130 /* Dummy alloc, will be reallocated in CheckInit */
131 avpicture_alloc( &p_sys->tmp_pic, p_sys->i_src_ffmpeg_chroma,
132 p_filter->fmt_out.video.i_width,
133 p_filter->fmt_out.video.i_height );
135 if( CheckInit( p_filter ) != VLC_SUCCESS )
141 msg_Dbg( p_filter, "input: %ix%i %4.4s -> %ix%i %4.4s",
142 p_filter->fmt_in.video.i_width, p_filter->fmt_in.video.i_height,
143 (char *)&p_filter->fmt_in.video.i_chroma,
144 p_filter->fmt_out.video.i_width, p_filter->fmt_out.video.i_height,
145 (char *)&p_filter->fmt_out.video.i_chroma );
147 /* libavcodec needs to be initialized for some chroma conversions */
148 E_(InitLibavcodec)(p_this);
153 /*****************************************************************************
154 * OpenFilter: probe the filter and return score
155 *****************************************************************************/
156 int E_(OpenFilter)( vlc_object_t *p_this )
158 return OpenFilterEx( p_this, VLC_FALSE );
161 /*****************************************************************************
162 * OpenCropPadd: probe the filter and return score
163 *****************************************************************************/
164 int E_(OpenCropPadd)( vlc_object_t *p_this )
166 return OpenFilterEx( p_this, VLC_TRUE );
170 /*****************************************************************************
171 * CloseFilter: clean up the filter
172 *****************************************************************************/
173 void E_(CloseFilter)( vlc_object_t *p_this )
175 filter_t *p_filter = (filter_t*)p_this;
176 filter_sys_t *p_sys = p_filter->p_sys;
178 if( p_sys->p_rsc ) img_resample_close( p_sys->p_rsc );
180 avpicture_free( &p_sys->tmp_pic );
185 /*****************************************************************************
186 * CheckInit: Initialise filter when necessary
187 *****************************************************************************/
188 static int CheckInit( filter_t *p_filter )
190 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 );
303 msg_Dbg( p_filter, "input: %ix%i -> %ix%i",
304 p_filter->fmt_out.video.i_width,
305 p_filter->fmt_out.video.i_height,
306 p_filter->fmt_in.video.i_width,
307 p_filter->fmt_in.video.i_height);
311 msg_Err( p_filter, "img_resample_init failed" );
316 avpicture_free( &p_sys->tmp_pic );
318 if( p_sys->b_resize_first )
320 /* Resizing then conversion */
321 avpicture_alloc( &p_sys->tmp_pic, p_sys->i_src_ffmpeg_chroma,
322 p_filter->fmt_out.video.i_width,
323 p_filter->fmt_out.video.i_height );
327 /* Conversion then resizing */
328 avpicture_alloc( &p_sys->tmp_pic, p_sys->i_dst_ffmpeg_chroma,
329 p_filter->fmt_in.video.i_width,
330 p_filter->fmt_in.video.i_height );
333 p_sys->fmt_in = p_filter->fmt_in;
334 p_sys->fmt_out = p_filter->fmt_out;
340 /* fill padd code from ffmpeg */
341 static int padcolor[3] = { 16, 128, 128 };
343 /* Expects img to be yuv420 */
344 static void fill_pad_region( AVPicture* img, int height, int width,
345 int padtop, int padbottom, int padleft, int padright, int *color )
350 for ( i = 0; i < 3; i++ )
352 shift = ( i == 0 ) ? 0 : 1;
354 if ( padtop || padleft )
356 memset( img->data[i], color[i], ( ( ( img->linesize[i] * padtop ) +
357 padleft ) >> shift) );
360 if ( padleft || padright )
362 optr = img->data[i] + ( img->linesize[i] * ( padtop >> shift ) ) +
363 ( img->linesize[i] - ( padright >> shift ) );
365 for ( y = 0; y < ( ( height - ( padtop + padbottom ) ) >> shift ); y++ )
367 memset( optr, color[i], ( padleft + padright ) >> shift );
368 optr += img->linesize[i];
374 optr = img->data[i] + ( img->linesize[i] * ( ( height - padbottom ) >> shift ) );
375 memset( optr, color[i], ( ( img->linesize[i] * padbottom ) >> shift ) );
380 /* Workaround, because old libavcodec doesnt know how to padd */
381 static void img_resample_padd( ImgReSampleContext *s, AVPicture *output,
382 const AVPicture *input, int padtop, int padleft )
384 AVPicture nopadd_pic = *output;
386 /* shift out top and left padding for old ffmpeg */
387 nopadd_pic.data[0] += ( nopadd_pic.linesize[0] * padtop + padleft );
388 nopadd_pic.data[1] += ( nopadd_pic.linesize[1] * padtop + padleft ) >> 1;
389 nopadd_pic.data[2] += ( nopadd_pic.linesize[2] * padtop + padleft ) >> 1;
390 img_resample( s, &nopadd_pic, input );
394 /*****************************************************************************
395 * Do the processing here
396 *****************************************************************************/
397 static picture_t *Process( filter_t *p_filter, picture_t *p_pic )
399 filter_sys_t *p_sys = p_filter->p_sys;
400 AVPicture src_pic, dest_pic;
401 AVPicture *p_src, *p_dst;
402 picture_t *p_pic_dst;
405 /* Check if format properties changed */
406 if( CheckInit( p_filter ) != VLC_SUCCESS ) return 0;
408 /* Request output picture */
409 p_pic_dst = p_filter->pf_vout_buffer_new( p_filter );
412 msg_Warn( p_filter, "can't get output picture" );
413 p_pic->pf_release( p_pic );
417 /* Prepare the AVPictures for the conversion */
418 for( i = 0; i < p_pic->i_planes; i++ )
420 src_pic.data[i] = p_pic->p[i].p_pixels;
421 src_pic.linesize[i] = p_pic->p[i].i_pitch;
423 for( i = 0; i < p_pic_dst->i_planes; i++ )
425 dest_pic.data[i] = p_pic_dst->p[i].p_pixels;
426 dest_pic.linesize[i] = p_pic_dst->p[i].i_pitch;
430 if( p_filter->fmt_in.video.i_chroma == VLC_FOURCC('Y','V','1','2') ||
431 p_filter->fmt_in.video.i_chroma == VLC_FOURCC('Y','V','U','9') )
434 src_pic.data[1] = p_pic->p[2].p_pixels;
435 src_pic.data[2] = p_pic->p[1].p_pixels;
437 if( p_filter->fmt_out.video.i_chroma == VLC_FOURCC('Y','V','1','2') ||
438 p_filter->fmt_out.video.i_chroma == VLC_FOURCC('Y','V','U','9') )
441 dest_pic.data[1] = p_pic_dst->p[2].p_pixels;
442 dest_pic.data[2] = p_pic_dst->p[1].p_pixels;
444 if( p_sys->i_src_ffmpeg_chroma == PIX_FMT_RGB24 )
445 if( p_filter->fmt_in.video.i_bmask == 0x00ff0000 )
446 p_sys->i_src_ffmpeg_chroma = PIX_FMT_BGR24;
450 if( p_sys->b_resize && p_sys->p_rsc )
453 if( p_sys->b_resize_first )
455 if( p_sys->b_convert ) p_dst = &p_sys->tmp_pic;
457 img_resample( p_sys->p_rsc, p_dst, p_src );
459 if (p_sys->b_enable_croppadd)
461 if (p_filter->fmt_out.video.i_visible_width != p_filter->fmt_out.video.i_width ||
462 p_filter->fmt_out.video.i_visible_height != p_filter->fmt_out.video.i_height ||
463 p_filter->fmt_out.video.i_x_offset != 0 ||
464 p_filter->fmt_out.video.i_y_offset != 0)
466 fill_pad_region(p_dst, p_filter->fmt_out.video.i_height,
467 p_filter->fmt_out.video.i_width,
468 p_filter->fmt_out.video.i_y_offset,
469 p_filter->fmt_out.video.i_height
470 - p_filter->fmt_out.video.i_visible_height
471 - p_filter->fmt_out.video.i_y_offset,
472 p_filter->fmt_out.video.i_x_offset,
473 p_filter->fmt_out.video.i_width
474 - p_filter->fmt_out.video.i_visible_width
475 - p_filter->fmt_out.video.i_x_offset,
484 if( p_sys->b_convert )
486 video_format_t *p_fmt = &p_filter->fmt_out.video;
488 if( p_sys->b_resize && !p_sys->b_resize_first )
490 p_dst = &p_sys->tmp_pic;
491 p_fmt = &p_filter->fmt_in.video;
494 img_convert( p_dst, p_sys->i_dst_ffmpeg_chroma,
495 p_src, p_sys->i_src_ffmpeg_chroma,
496 p_fmt->i_width, p_fmt->i_height );
501 if( p_sys->b_resize && !p_sys->b_resize_first && p_sys->p_rsc )
505 img_resample( p_sys->p_rsc, p_dst, p_src );
507 if (p_sys->b_enable_croppadd)
509 if (p_filter->fmt_out.video.i_visible_width != p_filter->fmt_out.video.i_width ||
510 p_filter->fmt_out.video.i_visible_height != p_filter->fmt_out.video.i_height ||
511 p_filter->fmt_out.video.i_x_offset != 0 ||
512 p_filter->fmt_out.video.i_y_offset != 0)
514 fill_pad_region(p_dst, p_filter->fmt_out.video.i_height,
515 p_filter->fmt_out.video.i_width,
516 p_filter->fmt_out.video.i_y_offset,
517 p_filter->fmt_out.video.i_height
518 - p_filter->fmt_out.video.i_visible_height
519 - p_filter->fmt_out.video.i_y_offset,
520 p_filter->fmt_out.video.i_x_offset,
521 p_filter->fmt_out.video.i_width
522 - p_filter->fmt_out.video.i_visible_width
523 - p_filter->fmt_out.video.i_x_offset,
529 /* Special case for RV32 -> YUVA */
530 if( !p_sys->b_resize &&
531 p_filter->fmt_in.video.i_chroma == VLC_FOURCC('R','V','3','2') &&
532 p_filter->fmt_out.video.i_chroma == VLC_FOURCC('Y','U','V','A') )
534 uint8_t *p_src = p_pic->p[0].p_pixels;
535 int i_src_pitch = p_pic->p[0].i_pitch;
536 uint8_t *p_dst = p_pic_dst->p[3].p_pixels;
537 int i_dst_pitch = p_pic_dst->p[3].i_pitch;
540 for( l = 0; l < p_filter->fmt_out.video.i_height; l++ )
542 for( j = 0; j < p_filter->fmt_out.video.i_width; j++ )
544 p_dst[j] = p_src[j*4+3];
546 p_src += i_src_pitch;
547 p_dst += i_dst_pitch;
550 else if( p_filter->fmt_out.video.i_chroma == VLC_FOURCC('Y','U','V','A') )
552 /* Special case for YUVA */
553 memset( p_pic_dst->p[3].p_pixels, 0xFF,
554 p_pic_dst->p[3].i_pitch * p_pic_dst->p[3].i_lines );
557 p_pic_dst->date = p_pic->date;
558 p_pic_dst->b_force = p_pic->b_force;
559 p_pic_dst->i_nb_fields = p_pic->i_nb_fields;
560 p_pic_dst->b_progressive = p_pic->b_progressive;
561 p_pic_dst->b_top_field_first = p_pic->b_top_field_first;
563 p_pic->pf_release( p_pic );
567 /*****************************************************************************
568 * OpenDeinterlace: probe the filter and return score
569 *****************************************************************************/
570 int E_(OpenDeinterlace)( vlc_object_t *p_this )
572 filter_t *p_filter = (filter_t*)p_this;
575 /* Check if we can handle that formats */
576 if( E_(GetFfmpegChroma)( p_filter->fmt_in.video.i_chroma ) < 0 )
581 /* Allocate the memory needed to store the decoder's structure */
582 if( ( p_filter->p_sys = p_sys =
583 (filter_sys_t *)malloc(sizeof(filter_sys_t)) ) == NULL )
585 msg_Err( p_filter, "out of memory" );
590 p_sys->i_src_ffmpeg_chroma =
591 E_(GetFfmpegChroma)( p_filter->fmt_in.video.i_chroma );
592 p_filter->pf_video_filter = Deinterlace;
594 msg_Dbg( p_filter, "deinterlacing" );
596 /* libavcodec needs to be initialized for some chroma conversions */
597 E_(InitLibavcodec)(p_this);
602 /*****************************************************************************
603 * CloseDeinterlace: clean up the filter
604 *****************************************************************************/
605 void E_(CloseDeinterlace)( vlc_object_t *p_this )
607 filter_t *p_filter = (filter_t*)p_this;
608 filter_sys_t *p_sys = p_filter->p_sys;
613 /*****************************************************************************
614 * Do the processing here
615 *****************************************************************************/
616 static picture_t *Deinterlace( filter_t *p_filter, picture_t *p_pic )
618 filter_sys_t *p_sys = p_filter->p_sys;
619 AVPicture src_pic, dest_pic;
620 picture_t *p_pic_dst;
623 /* Request output picture */
624 p_pic_dst = p_filter->pf_vout_buffer_new( p_filter );
627 msg_Warn( p_filter, "can't get output picture" );
631 /* Prepare the AVPictures for the conversion */
632 for( i = 0; i < p_pic->i_planes; i++ )
634 src_pic.data[i] = p_pic->p[i].p_pixels;
635 src_pic.linesize[i] = p_pic->p[i].i_pitch;
637 for( i = 0; i < p_pic_dst->i_planes; i++ )
639 dest_pic.data[i] = p_pic_dst->p[i].p_pixels;
640 dest_pic.linesize[i] = p_pic_dst->p[i].i_pitch;
643 avpicture_deinterlace( &dest_pic, &src_pic, p_sys->i_src_ffmpeg_chroma,
644 p_filter->fmt_in.video.i_width,
645 p_filter->fmt_in.video.i_height );
647 p_pic_dst->date = p_pic->date;
648 p_pic_dst->b_force = p_pic->b_force;
649 p_pic_dst->i_nb_fields = p_pic->i_nb_fields;
650 p_pic_dst->b_progressive = VLC_TRUE;
651 p_pic_dst->b_top_field_first = p_pic->b_top_field_first;
653 p_pic->pf_release( p_pic );
657 #endif /* ( (defined(HAVE_FFMPEG_SWSCALE_H) || defined(HAVE_LIBSWSCALE_TREE)) */