1 /*****************************************************************************
2 * vout_pictures.c : picture management functions
3 *****************************************************************************
4 * Copyright (C) 2000-2004 the VideoLAN team
7 * Authors: Vincent Seguin <seguin@via.ecp.fr>
8 * Samuel Hocevar <sam@zoy.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 *****************************************************************************/
34 #include <vlc_common.h>
38 #include <vlc_filter.h>
39 #include <vlc_image.h>
40 #include <vlc_block.h>
41 #include <vlc_picture_fifo.h>
42 #include <vlc_picture_pool.h>
44 #include "vout_pictures.h"
45 #include "vout_internal.h"
50 * Remove the reservation flag of a picture, which will cause it to be ready
53 void vout_DisplayPicture( vout_thread_t *p_vout, picture_t *p_pic )
55 vlc_mutex_lock( &p_vout->picture_lock );
57 if( p_pic->i_status == RESERVED_PICTURE )
59 p_pic->i_status = READY_PICTURE;
60 vlc_cond_signal( &p_vout->p->picture_wait );
64 msg_Err( p_vout, "picture to display %p has invalid status %d",
65 p_pic, p_pic->i_status );
67 p_vout->p->i_picture_qtype = p_pic->i_qtype;
68 p_vout->p->b_picture_interlaced = !p_pic->b_progressive;
70 vlc_mutex_unlock( &p_vout->picture_lock );
74 * Allocate a picture in the video output heap.
76 * This function creates a reserved image in the video output heap.
77 * A null pointer is returned if the function fails. This method provides an
78 * already allocated zone of memory in the picture data fields.
79 * It needs locking since several pictures can be created by several producers
82 int vout_CountPictureAvailable( vout_thread_t *p_vout )
87 vlc_mutex_lock( &p_vout->picture_lock );
88 for( i_pic = 0; i_pic < I_RENDERPICTURES; i_pic++ )
90 picture_t *p_pic = PP_RENDERPICTURE[(p_vout->render.i_last_used_pic + i_pic + 1) % I_RENDERPICTURES];
92 switch( p_pic->i_status )
94 case DESTROYED_PICTURE:
106 vlc_mutex_unlock( &p_vout->picture_lock );
111 picture_t *vout_CreatePicture( vout_thread_t *p_vout,
113 bool b_top_field_first,
114 unsigned int i_nb_fields )
116 int i_pic; /* picture index */
118 picture_t * p_freepic = NULL; /* first free picture */
121 vlc_mutex_lock( &p_vout->picture_lock );
124 * Look for an empty place in the picture heap.
126 for( i_pic = 0; i_pic < I_RENDERPICTURES; i_pic++ )
128 p_pic = PP_RENDERPICTURE[(p_vout->render.i_last_used_pic + i_pic + 1)
131 switch( p_pic->i_status )
133 case DESTROYED_PICTURE:
134 /* Memory will not be reallocated, and function can end
135 * immediately - this is the best possible case, since no
136 * memory allocation needs to be done */
137 p_pic->i_status = RESERVED_PICTURE;
138 p_pic->i_refcount = 0;
141 p_pic->b_progressive = b_progressive;
142 p_pic->i_nb_fields = i_nb_fields;
143 p_pic->b_top_field_first = b_top_field_first;
145 p_vout->i_heap_size++;
146 p_vout->render.i_last_used_pic =
147 ( p_vout->render.i_last_used_pic + i_pic + 1 )
149 vlc_mutex_unlock( &p_vout->picture_lock );
153 /* Picture is empty and ready for allocation */
154 p_vout->render.i_last_used_pic =
155 ( p_vout->render.i_last_used_pic + i_pic + 1 )
168 if( p_freepic != NULL )
170 vout_AllocatePicture( VLC_OBJECT(p_vout),
171 p_freepic, p_vout->render.i_chroma,
172 p_vout->render.i_width, p_vout->render.i_height,
173 p_vout->render.i_aspect * p_vout->render.i_height,
174 VOUT_ASPECT_FACTOR * p_vout->render.i_width);
176 if( p_freepic->i_planes )
178 /* Copy picture information, set some default values */
179 p_freepic->i_status = RESERVED_PICTURE;
180 p_freepic->i_type = MEMORY_PICTURE;
181 p_freepic->b_slow = 0;
183 p_freepic->i_refcount = 0;
184 p_freepic->b_force = 0;
186 p_freepic->b_progressive = b_progressive;
187 p_freepic->i_nb_fields = i_nb_fields;
188 p_freepic->b_top_field_first = b_top_field_first;
190 p_vout->i_heap_size++;
194 /* Memory allocation failed : set picture as empty */
195 p_freepic->i_status = FREE_PICTURE;
198 msg_Err( p_vout, "picture allocation failed" );
201 vlc_mutex_unlock( &p_vout->picture_lock );
206 /* No free or destroyed picture could be found, but the decoder
207 * will try again in a while. */
208 vlc_mutex_unlock( &p_vout->picture_lock );
214 static void DestroyPicture( vout_thread_t *p_vout, picture_t *p_picture )
216 vlc_assert_locked( &p_vout->picture_lock );
218 p_picture->i_status = DESTROYED_PICTURE;
219 p_vout->i_heap_size--;
220 picture_CleanupQuant( p_picture );
222 vlc_cond_signal( &p_vout->p->picture_wait );
226 * Remove a permanent or reserved picture from the heap
228 * This function frees a previously reserved picture or a permanent
229 * picture. It is meant to be used when the construction of a picture aborted.
230 * Note that the picture will be destroyed even if it is linked !
232 * TODO remove it, vout_DropPicture should be used instead
234 void vout_DestroyPicture( vout_thread_t *p_vout, picture_t *p_pic )
237 /* Check if picture status is valid */
238 vlc_mutex_lock( &p_vout->picture_lock );
239 if( p_pic->i_status != RESERVED_PICTURE )
241 msg_Err( p_vout, "picture to destroy %p has invalid status %d",
242 p_pic, p_pic->i_status );
244 vlc_mutex_unlock( &p_vout->picture_lock );
247 vout_DropPicture( p_vout, p_pic );
251 void vout_UsePictureLocked( vout_thread_t *p_vout, picture_t *p_picture )
253 vlc_assert_locked( &p_vout->picture_lock );
254 if( p_picture->i_refcount > 0 )
256 /* Pretend we displayed the picture, but don't destroy
257 * it since the decoder might still need it. */
258 p_picture->i_status = DISPLAYED_PICTURE;
262 /* Destroy the picture without displaying it */
263 DestroyPicture( p_vout, p_picture );
268 void vout_DropPicture( vout_thread_t *p_vout, picture_t *p_pic )
270 vlc_mutex_lock( &p_vout->picture_lock );
272 if( p_pic->i_status == READY_PICTURE )
274 /* Grr cannot destroy ready picture by myself so be sure vout won't like it */
276 vlc_cond_signal( &p_vout->p->picture_wait );
280 vout_UsePictureLocked( p_vout, p_pic );
283 vlc_mutex_unlock( &p_vout->picture_lock );
287 * Increment reference counter of a picture
289 * This function increments the reference counter of a picture in the video
290 * heap. It needs a lock since several producer threads can access the picture.
292 void vout_LinkPicture( vout_thread_t *p_vout, picture_t *p_pic )
294 vlc_mutex_lock( &p_vout->picture_lock );
296 vlc_mutex_unlock( &p_vout->picture_lock );
300 * Decrement reference counter of a picture
302 * This function decrement the reference counter of a picture in the video heap
304 void vout_UnlinkPicture( vout_thread_t *p_vout, picture_t *p_pic )
306 vlc_mutex_lock( &p_vout->picture_lock );
308 if( p_pic->i_refcount > 0 )
311 msg_Err( p_vout, "Invalid picture reference count (%p, %d)",
312 p_pic, p_pic->i_refcount );
314 if( p_pic->i_refcount == 0 &&
315 ( p_pic->i_status == DISPLAYED_PICTURE || p_pic->i_status == RESERVED_PICTURE ) )
316 DestroyPicture( p_vout, p_pic );
318 vlc_mutex_unlock( &p_vout->picture_lock );
324 * This function chooses whether the current picture needs to be copied
325 * before rendering, does the subpicture magic, and tells the video output
326 * thread which direct buffer needs to be displayed.
328 picture_t *vout_RenderPicture( vout_thread_t *p_vout, picture_t *p_pic,
329 subpicture_t *p_subpic, mtime_t render_date )
334 if( p_pic->i_type == DIRECT_PICTURE && !p_subpic )
336 /* No subtitles, picture is in a directbuffer so
337 * we can display it directly (even if it is still
342 /* It is either because:
343 * - the picture is not a direct buffer
344 * - we have to render subtitles (we can never do it on the given
345 * picture even if not referenced).
348 if( p_subpic != NULL && PP_OUTPUTPICTURE[0]->b_slow )
350 /* The picture buffer is in slow memory. We'll use
351 * the "2 * VOUT_MAX_PICTURES + 1" picture as a temporary
352 * one for subpictures rendering. */
353 p_render = &p_vout->p_picture[2 * VOUT_MAX_PICTURES];
354 if( p_render->i_status == FREE_PICTURE )
356 vout_AllocatePicture( VLC_OBJECT(p_vout),
357 p_render, p_vout->fmt_out.i_chroma,
358 p_vout->fmt_out.i_width,
359 p_vout->fmt_out.i_height,
360 p_vout->fmt_out.i_sar_num,
361 p_vout->fmt_out.i_sar_den );
362 p_render->i_type = MEMORY_PICTURE;
363 p_render->i_status = RESERVED_PICTURE;
368 /* We can directly render into a direct buffer */
369 p_render = PP_OUTPUTPICTURE[0];
373 picture_Copy( p_render, p_pic );
375 /* Render the subtitles if present */
377 spu_RenderSubpictures( p_vout->p->p_spu,
378 p_render, &p_vout->fmt_out,
379 p_subpic, &p_vout->fmt_in, render_date );
380 /* Copy in case we used a temporary fast buffer */
381 if( p_render != PP_OUTPUTPICTURE[0] )
382 picture_Copy( PP_OUTPUTPICTURE[0], p_render );
384 return PP_OUTPUTPICTURE[0];
387 #undef vout_AllocatePicture
389 * Allocate a new picture in the heap.
391 * This function allocates a fake direct buffer in memory, which can be
392 * used exactly like a video buffer. The video output thread then manages
393 * how it gets displayed.
395 int vout_AllocatePicture( vlc_object_t *p_this, picture_t *p_pic,
396 vlc_fourcc_t i_chroma,
397 int i_width, int i_height,
398 int i_sar_num, int i_sar_den )
402 /* Make sure the real dimensions are a multiple of 16 */
403 if( picture_Setup( p_pic, i_chroma, i_width, i_height,
404 i_sar_num, i_sar_den ) != VLC_SUCCESS )
407 /* Calculate how big the new image should be */
409 for( int i = 0; i < p_pic->i_planes; i++ )
411 const plane_t *p = &p_pic->p[i];
413 if( p->i_pitch <= 0 || p->i_lines <= 0 ||
414 p->i_pitch > (SIZE_MAX - i_bytes)/p->i_lines )
419 i_bytes += p->i_pitch * p->i_lines;
422 p_pic->p_data = vlc_memalign( &p_pic->p_data_orig, 16, i_bytes );
423 if( p_pic->p_data == NULL )
429 /* Fill the p_pixels field for each plane */
430 p_pic->p[0].p_pixels = p_pic->p_data;
431 for( int i = 1; i < p_pic->i_planes; i++ )
433 p_pic->p[i].p_pixels = &p_pic->p[i-1].p_pixels[ p_pic->p[i-1].i_lines *
434 p_pic->p[i-1].i_pitch ];
440 /*****************************************************************************
442 *****************************************************************************/
443 static void PictureReleaseCallback( picture_t *p_picture )
445 if( --p_picture->i_refcount > 0 )
447 picture_Delete( p_picture );
450 /*****************************************************************************
452 *****************************************************************************/
453 void picture_Reset( picture_t *p_picture )
456 p_picture->date = VLC_TS_INVALID;
457 p_picture->b_force = false;
458 p_picture->b_progressive = false;
459 p_picture->i_nb_fields = 0;
460 p_picture->b_top_field_first = false;
461 picture_CleanupQuant( p_picture );
464 /*****************************************************************************
466 *****************************************************************************/
469 unsigned i_plane_count;
482 } p[VOUT_MAX_PLANES];
483 unsigned i_pixel_size;
485 } chroma_description_t;
487 #define PLANAR(n, w_den, h_den) \
488 { n, { {{1,1}, {1,1}}, {{1,w_den}, {1,h_den}}, {{1,w_den}, {1,h_den}}, {{1,1}, {1,1}} }, 1 }
489 #define PACKED(size) \
490 { 1, { {{1,1}, {1,1}} }, size }
494 vlc_fourcc_t p_fourcc[5];
495 chroma_description_t description;
497 { { VLC_CODEC_I411, 0 }, PLANAR(3, 4, 1) },
498 { { VLC_CODEC_I410, VLC_CODEC_YV9, 0 }, PLANAR(3, 4, 4) },
499 { { VLC_CODEC_YV12, VLC_CODEC_I420, VLC_CODEC_J420, 0 }, PLANAR(3, 2, 2) },
500 { { VLC_CODEC_I422, VLC_CODEC_J422, 0 }, PLANAR(3, 2, 1) },
501 { { VLC_CODEC_I440, VLC_CODEC_J440, 0 }, PLANAR(3, 1, 2) },
502 { { VLC_CODEC_I444, VLC_CODEC_J444, 0 }, PLANAR(3, 1, 1) },
503 { { VLC_CODEC_YUVA, 0 }, PLANAR(4, 1, 1) },
505 { { VLC_CODEC_UYVY, VLC_CODEC_VYUY, VLC_CODEC_YUYV, VLC_CODEC_YVYU, 0 }, PACKED(2) },
506 { { VLC_CODEC_RGB8, VLC_CODEC_GREY, VLC_CODEC_YUVP, VLC_CODEC_RGBP, 0 }, PACKED(1) },
507 { { VLC_CODEC_RGB16, VLC_CODEC_RGB15, 0 }, PACKED(2) },
508 { { VLC_CODEC_RGB24, 0 }, PACKED(3) },
509 { { VLC_CODEC_RGB32, VLC_CODEC_RGBA, 0 }, PACKED(4) },
511 { { VLC_CODEC_Y211, 0 }, { 1, { {{1,4}, {1,1}} }, 4 } },
513 { {0}, { 0, {}, 0 } }
519 static const chroma_description_t *vlc_fourcc_GetChromaDescription( vlc_fourcc_t i_fourcc )
521 for( unsigned i = 0; p_chromas[i].p_fourcc[0]; i++ )
523 const vlc_fourcc_t *p_fourcc = p_chromas[i].p_fourcc;
524 for( unsigned j = 0; p_fourcc[j]; j++ )
526 if( p_fourcc[j] == i_fourcc )
527 return &p_chromas[i].description;
533 static int LCM( int a, int b )
535 return a * b / GCD( a, b );
538 int picture_Setup( picture_t *p_picture, vlc_fourcc_t i_chroma,
539 int i_width, int i_height, int i_sar_num, int i_sar_den )
541 /* Store default values */
542 p_picture->i_planes = 0;
543 for( unsigned i = 0; i < VOUT_MAX_PLANES; i++ )
545 plane_t *p = &p_picture->p[i];
547 p->i_pixel_pitch = 0;
550 p_picture->pf_release = NULL;
551 p_picture->p_release_sys = NULL;
552 p_picture->i_refcount = 0;
554 p_picture->i_qtype = QTYPE_NONE;
555 p_picture->i_qstride = 0;
556 p_picture->p_q = NULL;
558 video_format_Setup( &p_picture->format, i_chroma, i_width, i_height,
559 i_sar_num, i_sar_den );
561 const chroma_description_t *p_dsc =
562 vlc_fourcc_GetChromaDescription( p_picture->format.i_chroma );
566 /* We want V (width/height) to respect:
567 (V * p_dsc->p[i].w.i_num) % p_dsc->p[i].w.i_den == 0
568 (V * p_dsc->p[i].w.i_num/p_dsc->p[i].w.i_den * p_dsc->i_pixel_size) % 16 == 0
569 Which is respected if you have
570 V % lcm( p_dsc->p[0..planes].w.i_den * 16) == 0
575 for( unsigned i = 0; i < p_dsc->i_plane_count; i++ )
577 i_modulo_w = LCM( i_modulo_w, 16 * p_dsc->p[i].w.i_den );
578 i_modulo_h = LCM( i_modulo_h, 16 * p_dsc->p[i].h.i_den );
579 if( i_ratio_h < p_dsc->p[i].h.i_den )
580 i_ratio_h = p_dsc->p[i].h.i_den;
583 const int i_width_aligned = ( i_width + i_modulo_w - 1 ) / i_modulo_w * i_modulo_w;
584 const int i_height_aligned = ( i_height + i_modulo_h - 1 ) / i_modulo_h * i_modulo_h;
585 const int i_height_extra = 2 * i_ratio_h; /* This one is a hack for some ASM functions */
586 for( unsigned i = 0; i < p_dsc->i_plane_count; i++ )
588 plane_t *p = &p_picture->p[i];
590 p->i_lines = (i_height_aligned + i_height_extra ) * p_dsc->p[i].h.i_num / p_dsc->p[i].h.i_den;
591 p->i_visible_lines = i_height * p_dsc->p[i].h.i_num / p_dsc->p[i].h.i_den;
592 p->i_pitch = i_width_aligned * p_dsc->p[i].w.i_num / p_dsc->p[i].w.i_den * p_dsc->i_pixel_size;
593 p->i_visible_pitch = i_width * p_dsc->p[i].w.i_num / p_dsc->p[i].w.i_den * p_dsc->i_pixel_size;
594 p->i_pixel_pitch = p_dsc->i_pixel_size;
596 assert( (p->i_pitch % 16) == 0 );
598 p_picture->i_planes = p_dsc->i_plane_count;
603 /*****************************************************************************
605 *****************************************************************************/
606 picture_t *picture_NewFromResource( const video_format_t *p_fmt, const picture_resource_t *p_resource )
608 video_format_t fmt = *p_fmt;
610 /* It is needed to be sure all informations are filled */
611 video_format_Setup( &fmt, p_fmt->i_chroma,
612 p_fmt->i_width, p_fmt->i_height,
613 p_fmt->i_sar_num, p_fmt->i_sar_den );
616 picture_t *p_picture = calloc( 1, sizeof(*p_picture) );
622 if( picture_Setup( p_picture, fmt.i_chroma, fmt.i_width, fmt.i_height,
623 fmt.i_sar_num, fmt.i_sar_den ) )
628 p_picture->p_sys = p_resource->p_sys;
630 for( int i = 0; i < p_picture->i_planes; i++ )
632 p_picture->p[i].p_pixels = p_resource->p[i].p_pixels;
633 p_picture->p[i].i_lines = p_resource->p[i].i_lines;
634 p_picture->p[i].i_pitch = p_resource->p[i].i_pitch;
639 if( vout_AllocatePicture( (vlc_object_t *)NULL, p_picture,
640 fmt.i_chroma, fmt.i_width, fmt.i_height,
641 fmt.i_sar_num, fmt.i_sar_den ) )
648 p_picture->format = fmt;
649 p_picture->i_refcount = 1;
650 p_picture->pf_release = PictureReleaseCallback;
651 p_picture->i_status = RESERVED_PICTURE;
655 picture_t *picture_NewFromFormat( const video_format_t *p_fmt )
657 return picture_NewFromResource( p_fmt, NULL );
659 picture_t *picture_New( vlc_fourcc_t i_chroma, int i_width, int i_height, int i_sar_num, int i_sar_den )
663 memset( &fmt, 0, sizeof(fmt) );
664 video_format_Setup( &fmt, i_chroma, i_width, i_height,
665 i_sar_num, i_sar_den );
667 return picture_NewFromFormat( &fmt );
670 /*****************************************************************************
672 *****************************************************************************/
673 void picture_Delete( picture_t *p_picture )
675 assert( p_picture && p_picture->i_refcount == 0 );
676 assert( p_picture->p_release_sys == NULL );
678 free( p_picture->p_q );
679 free( p_picture->p_data_orig );
680 free( p_picture->p_sys );
684 /*****************************************************************************
686 *****************************************************************************/
687 void picture_CopyPixels( picture_t *p_dst, const picture_t *p_src )
691 for( i = 0; i < p_src->i_planes ; i++ )
692 plane_CopyPixels( p_dst->p+i, p_src->p+i );
695 void plane_CopyPixels( plane_t *p_dst, const plane_t *p_src )
697 const unsigned i_width = __MIN( p_dst->i_visible_pitch,
698 p_src->i_visible_pitch );
699 const unsigned i_height = __MIN( p_dst->i_visible_lines,
700 p_src->i_visible_lines );
702 if( p_src->i_pitch == p_dst->i_pitch )
704 /* There are margins, but with the same width : perfect ! */
705 vlc_memcpy( p_dst->p_pixels, p_src->p_pixels,
706 p_src->i_pitch * i_height );
710 /* We need to proceed line by line */
711 uint8_t *p_in = p_src->p_pixels;
712 uint8_t *p_out = p_dst->p_pixels;
718 for( i_line = i_height; i_line--; )
720 vlc_memcpy( p_out, p_in, i_width );
721 p_in += p_src->i_pitch;
722 p_out += p_dst->i_pitch;
727 /*****************************************************************************
729 *****************************************************************************/
730 int picture_Export( vlc_object_t *p_obj,
732 video_format_t *p_fmt,
733 picture_t *p_picture,
734 vlc_fourcc_t i_format,
735 int i_override_width, int i_override_height )
738 video_format_t fmt_in = p_picture->format;
739 if( fmt_in.i_sar_num <= 0 || fmt_in.i_sar_den <= 0 )
742 fmt_in.i_sar_den = 1;
746 video_format_t fmt_out;
747 memset( &fmt_out, 0, sizeof(fmt_out) );
749 fmt_out.i_sar_den = 1;
750 fmt_out.i_chroma = i_format;
752 /* compute original width/height */
753 unsigned int i_original_width;
754 unsigned int i_original_height;
755 if( fmt_in.i_sar_num >= fmt_in.i_sar_den )
757 i_original_width = fmt_in.i_width * fmt_in.i_sar_num / fmt_in.i_sar_den;
758 i_original_height = fmt_in.i_height;
762 i_original_width = fmt_in.i_width;
763 i_original_height = fmt_in.i_height * fmt_in.i_sar_den / fmt_in.i_sar_num;
767 fmt_out.i_width = ( i_override_width < 0 ) ?
768 i_original_width : i_override_width;
769 fmt_out.i_height = ( i_override_height < 0 ) ?
770 i_original_height : i_override_height;
772 /* scale if only one direction is provided */
773 if( fmt_out.i_height == 0 && fmt_out.i_width > 0 )
775 fmt_out.i_height = fmt_in.i_height * fmt_out.i_width
776 * fmt_in.i_sar_den / fmt_in.i_width / fmt_in.i_sar_num;
778 else if( fmt_out.i_width == 0 && fmt_out.i_height > 0 )
780 fmt_out.i_width = fmt_in.i_width * fmt_out.i_height
781 * fmt_in.i_sar_num / fmt_in.i_height / fmt_in.i_sar_den;
784 image_handler_t *p_image = image_HandlerCreate( p_obj );
786 block_t *p_block = image_Write( p_image, p_picture, &fmt_in, &fmt_out );
788 image_HandlerDelete( p_image );
794 p_block->i_dts = p_picture->date;