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 && p_pic->i_status == DISPLAYED_PICTURE )
315 DestroyPicture( p_vout, p_pic );
317 vlc_mutex_unlock( &p_vout->picture_lock );
323 * This function chooses whether the current picture needs to be copied
324 * before rendering, does the subpicture magic, and tells the video output
325 * thread which direct buffer needs to be displayed.
327 picture_t *vout_RenderPicture( vout_thread_t *p_vout, picture_t *p_pic,
328 subpicture_t *p_subpic, mtime_t render_date )
333 if( p_pic->i_type == DIRECT_PICTURE )
335 /* Picture is in a direct buffer. */
337 if( p_subpic != NULL )
339 /* We have subtitles. First copy the picture to
340 * the spare direct buffer, then render the
342 picture_Copy( PP_OUTPUTPICTURE[0], p_pic );
344 spu_RenderSubpictures( p_vout->p_spu,
345 PP_OUTPUTPICTURE[0], &p_vout->fmt_out,
346 p_subpic, &p_vout->fmt_in, render_date );
348 return PP_OUTPUTPICTURE[0];
351 /* No subtitles, picture is in a directbuffer so
352 * we can display it directly (even if it is still
357 /* Not a direct buffer. We either need to copy it to a direct buffer,
358 * or render it if the chroma isn't the same. */
359 if( p_vout->p->b_direct )
361 /* Picture is not in a direct buffer, but is exactly the
362 * same size as the direct buffers. A memcpy() is enough,
363 * then render the subtitles. */
364 picture_Copy( PP_OUTPUTPICTURE[0], p_pic );
365 spu_RenderSubpictures( p_vout->p_spu,
366 PP_OUTPUTPICTURE[0], &p_vout->fmt_out,
367 p_subpic, &p_vout->fmt_in, render_date );
369 return PP_OUTPUTPICTURE[0];
372 /* Picture is not in a direct buffer, and needs to be converted to
373 * another size/chroma. Then the subtitles need to be rendered as
374 * well. This usually means software YUV, or hardware YUV with a
375 * different chroma. */
377 if( p_subpic != NULL && p_vout->p_picture[0].b_slow )
379 /* The picture buffer is in slow memory. We'll use
380 * the "2 * VOUT_MAX_PICTURES + 1" picture as a temporary
381 * one for subpictures rendering. */
382 picture_t *p_tmp_pic = &p_vout->p_picture[2 * VOUT_MAX_PICTURES];
383 if( p_tmp_pic->i_status == FREE_PICTURE )
385 vout_AllocatePicture( VLC_OBJECT(p_vout),
386 p_tmp_pic, p_vout->fmt_out.i_chroma,
387 p_vout->fmt_out.i_width,
388 p_vout->fmt_out.i_height,
389 p_vout->fmt_out.i_sar_num,
390 p_vout->fmt_out.i_sar_den );
391 p_tmp_pic->i_type = MEMORY_PICTURE;
392 p_tmp_pic->i_status = RESERVED_PICTURE;
395 /* Convert image to the first direct buffer */
396 p_vout->p->p_chroma->p_owner = (filter_owner_sys_t *)p_tmp_pic;
397 p_vout->p->p_chroma->pf_video_filter( p_vout->p->p_chroma, p_pic );
399 /* Render subpictures on the first direct buffer */
400 spu_RenderSubpictures( p_vout->p_spu,
401 p_tmp_pic, &p_vout->fmt_out,
402 p_subpic, &p_vout->fmt_in, render_date );
404 picture_Copy( &p_vout->p_picture[0], p_tmp_pic );
408 /* Convert image to the first direct buffer */
409 p_vout->p->p_chroma->p_owner = (filter_owner_sys_t *)&p_vout->p_picture[0];
410 p_vout->p->p_chroma->pf_video_filter( p_vout->p->p_chroma, p_pic );
412 /* Render subpictures on the first direct buffer */
413 spu_RenderSubpictures( p_vout->p_spu,
414 &p_vout->p_picture[0], &p_vout->fmt_out,
415 p_subpic, &p_vout->fmt_in, render_date );
418 return &p_vout->p_picture[0];
422 * Calculate image window coordinates
424 * This function will be accessed by plugins. It calculates the relative
425 * position of the output window and the image window.
427 void vout_PlacePicture( const vout_thread_t *p_vout,
428 unsigned int i_width, unsigned int i_height,
429 unsigned int *restrict pi_x,
430 unsigned int *restrict pi_y,
431 unsigned int *restrict pi_width,
432 unsigned int *restrict pi_height )
434 if( i_width <= 0 || i_height <= 0 )
436 *pi_width = *pi_height = *pi_x = *pi_y = 0;
440 if( p_vout->b_autoscale )
443 *pi_height = i_height;
447 int i_zoom = p_vout->i_zoom;
448 /* be realistic, scaling factor confined between .2 and 10. */
449 if( i_zoom > 10 * ZOOM_FP_FACTOR ) i_zoom = 10 * ZOOM_FP_FACTOR;
450 else if( i_zoom < ZOOM_FP_FACTOR / 5 ) i_zoom = ZOOM_FP_FACTOR / 5;
452 unsigned int i_original_width, i_original_height;
454 if( p_vout->fmt_in.i_sar_num >= p_vout->fmt_in.i_sar_den )
456 i_original_width = p_vout->fmt_in.i_visible_width *
457 p_vout->fmt_in.i_sar_num / p_vout->fmt_in.i_sar_den;
458 i_original_height = p_vout->fmt_in.i_visible_height;
462 i_original_width = p_vout->fmt_in.i_visible_width;
463 i_original_height = p_vout->fmt_in.i_visible_height *
464 p_vout->fmt_in.i_sar_den / p_vout->fmt_in.i_sar_num;
467 /* On windows, inner video window exceeding container leads to black screen */
468 *pi_width = __MIN( i_width, i_original_width * i_zoom / ZOOM_FP_FACTOR );
469 *pi_height = __MIN( i_height, i_original_height * i_zoom / ZOOM_FP_FACTOR );
471 *pi_width = i_original_width * i_zoom / ZOOM_FP_FACTOR ;
472 *pi_height = i_original_height * i_zoom / ZOOM_FP_FACTOR ;
476 int64_t i_scaled_width = p_vout->fmt_in.i_visible_width * (int64_t)p_vout->fmt_in.i_sar_num *
477 *pi_height / p_vout->fmt_in.i_visible_height / p_vout->fmt_in.i_sar_den;
478 int64_t i_scaled_height = p_vout->fmt_in.i_visible_height * (int64_t)p_vout->fmt_in.i_sar_den *
479 *pi_width / p_vout->fmt_in.i_visible_width / p_vout->fmt_in.i_sar_num;
481 if( i_scaled_width <= 0 || i_scaled_height <= 0 )
483 msg_Warn( p_vout, "ignoring broken aspect ratio" );
484 i_scaled_width = *pi_width;
485 i_scaled_height = *pi_height;
488 if( i_scaled_width > *pi_width )
489 *pi_height = i_scaled_height;
491 *pi_width = i_scaled_width;
493 switch( p_vout->i_alignment & VOUT_ALIGN_HMASK )
495 case VOUT_ALIGN_LEFT:
498 case VOUT_ALIGN_RIGHT:
499 *pi_x = i_width - *pi_width;
502 *pi_x = ( i_width - *pi_width ) / 2;
505 switch( p_vout->i_alignment & VOUT_ALIGN_VMASK )
510 case VOUT_ALIGN_BOTTOM:
511 *pi_y = i_height - *pi_height;
514 *pi_y = ( i_height - *pi_height ) / 2;
518 #undef vout_AllocatePicture
520 * Allocate a new picture in the heap.
522 * This function allocates a fake direct buffer in memory, which can be
523 * used exactly like a video buffer. The video output thread then manages
524 * how it gets displayed.
526 int vout_AllocatePicture( vlc_object_t *p_this, picture_t *p_pic,
527 vlc_fourcc_t i_chroma,
528 int i_width, int i_height,
529 int i_sar_num, int i_sar_den )
532 int i_index, i_width_aligned, i_height_aligned;
534 /* Make sure the real dimensions are a multiple of 16 */
535 i_width_aligned = (i_width + 15) >> 4 << 4;
536 i_height_aligned = (i_height + 15) >> 4 << 4;
538 if( picture_Setup( p_pic, i_chroma, i_width, i_height,
539 i_sar_num, i_sar_den ) != VLC_SUCCESS )
545 /* Calculate how big the new image should be */
548 * bytes = width_aligned * height_aligned * bpp / 8
549 * We need to check for an integer overflow at each multiplication since
550 * height & width (and bpp?) could be arbitrary large
554 /* i_width_aligned is a multiple of 16, so we can divide by 8 now */
555 size_t i_width_aligned_divided = i_width_aligned / 8;
556 if( i_width_aligned_divided <= (SIZE_MAX/i_height_aligned) )
558 size_t i_pixels_divided = i_width_aligned_divided * i_height_aligned;
559 size_t i_bpp = p_pic->format.i_bits_per_pixel;
560 if( i_pixels_divided <= (SIZE_MAX/i_bpp) )
562 i_bytes = i_pixels_divided * i_bpp;
572 p_pic->p_data = vlc_memalign( &p_pic->p_data_orig, 16, i_bytes );
574 if( p_pic->p_data == NULL )
580 /* Fill the p_pixels field for each plane */
581 p_pic->p[ 0 ].p_pixels = p_pic->p_data;
583 for( i_index = 1; i_index < p_pic->i_planes; i_index++ )
585 p_pic->p[i_index].p_pixels = p_pic->p[i_index-1].p_pixels +
586 p_pic->p[i_index-1].i_lines * p_pic->p[i_index-1].i_pitch;
593 * Compare two chroma values
595 * This function returns 1 if the two fourcc values given as argument are
596 * the same format (eg. UYVY/UYNV) or almost the same format (eg. I420/YV12)
598 int vout_ChromaCmp( vlc_fourcc_t i_chroma, vlc_fourcc_t i_amorhc )
600 static const vlc_fourcc_t p_I420[] = {
601 VLC_CODEC_I420, VLC_CODEC_YV12, VLC_CODEC_J420, 0
603 static const vlc_fourcc_t p_I422[] = {
604 VLC_CODEC_I422, VLC_CODEC_J422, 0
606 static const vlc_fourcc_t p_I440[] = {
607 VLC_CODEC_I440, VLC_CODEC_J440, 0
609 static const vlc_fourcc_t p_I444[] = {
610 VLC_CODEC_I444, VLC_CODEC_J444, 0
612 static const vlc_fourcc_t *pp_fcc[] = {
613 p_I420, p_I422, p_I440, p_I444, NULL
617 i_chroma = vlc_fourcc_GetCodec( VIDEO_ES, i_chroma );
618 i_amorhc = vlc_fourcc_GetCodec( VIDEO_ES, i_amorhc );
620 /* If they are the same, they are the same ! */
621 if( i_chroma == i_amorhc )
624 /* Check for equivalence classes */
625 for( int i = 0; pp_fcc[i] != NULL; i++ )
629 for( int j = 0; pp_fcc[i][j] != 0; j++ )
631 if( i_chroma == pp_fcc[i][j] )
633 if( i_amorhc == pp_fcc[i][j] )
636 if( b_fcc1 && b_fcc2 )
642 /*****************************************************************************
644 *****************************************************************************/
645 static void PictureReleaseCallback( picture_t *p_picture )
647 if( --p_picture->i_refcount > 0 )
649 picture_Delete( p_picture );
652 /*****************************************************************************
654 *****************************************************************************/
655 void picture_Reset( picture_t *p_picture )
658 p_picture->date = VLC_TS_INVALID;
659 p_picture->b_force = false;
660 p_picture->b_progressive = false;
661 p_picture->i_nb_fields = 0;
662 p_picture->b_top_field_first = false;
663 picture_CleanupQuant( p_picture );
666 /*****************************************************************************
668 *****************************************************************************/
669 int picture_Setup( picture_t *p_picture, vlc_fourcc_t i_chroma,
670 int i_width, int i_height, int i_sar_num, int i_sar_den )
672 int i_index, i_width_aligned, i_height_aligned;
674 /* Store default values */
675 for( i_index = 0; i_index < VOUT_MAX_PLANES; i_index++ )
677 p_picture->p[i_index].p_pixels = NULL;
678 p_picture->p[i_index].i_pixel_pitch = 1;
681 p_picture->pf_release = NULL;
682 p_picture->p_release_sys = NULL;
683 p_picture->i_refcount = 0;
685 p_picture->i_qtype = QTYPE_NONE;
686 p_picture->i_qstride = 0;
687 p_picture->p_q = NULL;
689 video_format_Setup( &p_picture->format, i_chroma, i_width, i_height,
690 i_sar_num, i_sar_den );
692 /* Make sure the real dimensions are a multiple of 16 */
693 i_width_aligned = (i_width + 15) >> 4 << 4;
694 i_height_aligned = (i_height + 15) >> 4 << 4;
696 /* Calculate coordinates */
697 switch( vlc_fourcc_GetCodec( VIDEO_ES, i_chroma ) )
700 p_picture->p[ Y_PLANE ].i_lines = i_height_aligned;
701 p_picture->p[ Y_PLANE ].i_visible_lines = i_height;
702 p_picture->p[ Y_PLANE ].i_pitch = i_width_aligned;
703 p_picture->p[ Y_PLANE ].i_visible_pitch = i_width;
704 p_picture->p[ U_PLANE ].i_lines = i_height_aligned;
705 p_picture->p[ U_PLANE ].i_visible_lines = i_height;
706 p_picture->p[ U_PLANE ].i_pitch = i_width_aligned / 4;
707 p_picture->p[ U_PLANE ].i_visible_pitch = i_width / 4;
708 p_picture->p[ V_PLANE ].i_lines = i_height_aligned;
709 p_picture->p[ V_PLANE ].i_visible_lines = i_height;
710 p_picture->p[ V_PLANE ].i_pitch = i_width_aligned / 4;
711 p_picture->p[ V_PLANE ].i_visible_pitch = i_width / 4;
712 p_picture->i_planes = 3;
717 p_picture->p[ Y_PLANE ].i_lines = i_height_aligned;
718 p_picture->p[ Y_PLANE ].i_visible_lines = i_height;
719 p_picture->p[ Y_PLANE ].i_pitch = i_width_aligned;
720 p_picture->p[ Y_PLANE ].i_visible_pitch = i_width;
721 p_picture->p[ U_PLANE ].i_lines = i_height_aligned / 4;
722 p_picture->p[ U_PLANE ].i_visible_lines = i_height / 4;
723 p_picture->p[ U_PLANE ].i_pitch = i_width_aligned / 4;
724 p_picture->p[ U_PLANE ].i_visible_pitch = i_width / 4;
725 p_picture->p[ V_PLANE ].i_lines = i_height_aligned / 4;
726 p_picture->p[ V_PLANE ].i_visible_lines = i_height / 4;
727 p_picture->p[ V_PLANE ].i_pitch = i_width_aligned / 4;
728 p_picture->p[ V_PLANE ].i_visible_pitch = i_width / 4;
729 p_picture->i_planes = 3;
735 p_picture->p[ Y_PLANE ].i_lines = i_height_aligned;
736 p_picture->p[ Y_PLANE ].i_visible_lines = i_height;
737 p_picture->p[ Y_PLANE ].i_pitch = i_width_aligned;
738 p_picture->p[ Y_PLANE ].i_visible_pitch = i_width;
739 p_picture->p[ U_PLANE ].i_lines = i_height_aligned / 2;
740 p_picture->p[ U_PLANE ].i_visible_lines = i_height / 2;
741 p_picture->p[ U_PLANE ].i_pitch = i_width_aligned / 2;
742 p_picture->p[ U_PLANE ].i_visible_pitch = i_width / 2;
743 p_picture->p[ V_PLANE ].i_lines = i_height_aligned / 2;
744 p_picture->p[ V_PLANE ].i_visible_lines = i_height / 2;
745 p_picture->p[ V_PLANE ].i_pitch = i_width_aligned / 2;
746 p_picture->p[ V_PLANE ].i_visible_pitch = i_width / 2;
747 p_picture->i_planes = 3;
752 p_picture->p[ Y_PLANE ].i_lines = i_height_aligned;
753 p_picture->p[ Y_PLANE ].i_visible_lines = i_height;
754 p_picture->p[ Y_PLANE ].i_pitch = i_width_aligned;
755 p_picture->p[ Y_PLANE ].i_visible_pitch = i_width;
756 p_picture->p[ U_PLANE ].i_lines = i_height_aligned;
757 p_picture->p[ U_PLANE ].i_visible_lines = i_height;
758 p_picture->p[ U_PLANE ].i_pitch = i_width_aligned / 2;
759 p_picture->p[ U_PLANE ].i_visible_pitch = i_width / 2;
760 p_picture->p[ V_PLANE ].i_lines = i_height_aligned;
761 p_picture->p[ V_PLANE ].i_visible_lines = i_height;
762 p_picture->p[ V_PLANE ].i_pitch = i_width_aligned / 2;
763 p_picture->p[ V_PLANE ].i_visible_pitch = i_width / 2;
764 p_picture->i_planes = 3;
769 p_picture->p[ Y_PLANE ].i_lines = i_height_aligned;
770 p_picture->p[ Y_PLANE ].i_visible_lines = i_height;
771 p_picture->p[ Y_PLANE ].i_pitch = i_width_aligned;
772 p_picture->p[ Y_PLANE ].i_visible_pitch = i_width;
773 p_picture->p[ U_PLANE ].i_lines = i_height_aligned / 2;
774 p_picture->p[ U_PLANE ].i_visible_lines = i_height / 2;
775 p_picture->p[ U_PLANE ].i_pitch = i_width_aligned;
776 p_picture->p[ U_PLANE ].i_visible_pitch = i_width;
777 p_picture->p[ V_PLANE ].i_lines = i_height_aligned / 2;
778 p_picture->p[ V_PLANE ].i_visible_lines = i_height / 2;
779 p_picture->p[ V_PLANE ].i_pitch = i_width_aligned;
780 p_picture->p[ V_PLANE ].i_visible_pitch = i_width;
781 p_picture->i_planes = 3;
786 p_picture->p[ Y_PLANE ].i_lines = i_height_aligned;
787 p_picture->p[ Y_PLANE ].i_visible_lines = i_height;
788 p_picture->p[ Y_PLANE ].i_pitch = i_width_aligned;
789 p_picture->p[ Y_PLANE ].i_visible_pitch = i_width;
790 p_picture->p[ U_PLANE ].i_lines = i_height_aligned;
791 p_picture->p[ U_PLANE ].i_visible_lines = i_height;
792 p_picture->p[ U_PLANE ].i_pitch = i_width_aligned;
793 p_picture->p[ U_PLANE ].i_visible_pitch = i_width;
794 p_picture->p[ V_PLANE ].i_lines = i_height_aligned;
795 p_picture->p[ V_PLANE ].i_visible_lines = i_height;
796 p_picture->p[ V_PLANE ].i_pitch = i_width_aligned;
797 p_picture->p[ V_PLANE ].i_visible_pitch = i_width;
798 p_picture->i_planes = 3;
802 p_picture->p[ Y_PLANE ].i_lines = i_height_aligned;
803 p_picture->p[ Y_PLANE ].i_visible_lines = i_height;
804 p_picture->p[ Y_PLANE ].i_pitch = i_width_aligned;
805 p_picture->p[ Y_PLANE ].i_visible_pitch = i_width;
806 p_picture->p[ U_PLANE ].i_lines = i_height_aligned;
807 p_picture->p[ U_PLANE ].i_visible_lines = i_height;
808 p_picture->p[ U_PLANE ].i_pitch = i_width_aligned;
809 p_picture->p[ U_PLANE ].i_visible_pitch = i_width;
810 p_picture->p[ V_PLANE ].i_lines = i_height_aligned;
811 p_picture->p[ V_PLANE ].i_visible_lines = i_height;
812 p_picture->p[ V_PLANE ].i_pitch = i_width_aligned;
813 p_picture->p[ V_PLANE ].i_visible_pitch = i_width;
814 p_picture->p[ A_PLANE ].i_lines = i_height_aligned;
815 p_picture->p[ A_PLANE ].i_visible_lines = i_height;
816 p_picture->p[ A_PLANE ].i_pitch = i_width_aligned;
817 p_picture->p[ A_PLANE ].i_visible_pitch = i_width;
818 p_picture->i_planes = 4;
822 p_picture->p->i_lines = i_height_aligned;
823 p_picture->p->i_visible_lines = i_height;
824 p_picture->p->i_pitch = i_width_aligned;
825 p_picture->p->i_visible_pitch = i_width;
826 p_picture->p->i_pixel_pitch = 1;
827 p_picture->i_planes = 1;
831 p_picture->p->i_lines = i_height_aligned;
832 p_picture->p->i_visible_lines = i_height;
833 p_picture->p->i_pitch = i_width_aligned;
834 p_picture->p->i_visible_pitch = i_width;
835 p_picture->p->i_pixel_pitch = 4;
836 p_picture->i_planes = 1;
843 p_picture->p->i_lines = i_height_aligned;
844 p_picture->p->i_visible_lines = i_height;
845 p_picture->p->i_pitch = i_width_aligned * 2;
846 p_picture->p->i_visible_pitch = i_width * 2;
847 p_picture->p->i_pixel_pitch = 2;
848 p_picture->i_planes = 1;
852 p_picture->p->i_lines = i_height_aligned;
853 p_picture->p->i_visible_lines = i_height;
854 p_picture->p->i_pitch = i_width_aligned;
855 p_picture->p->i_visible_pitch = i_width;
856 p_picture->p->i_pixel_pitch = 1;
857 p_picture->i_planes = 1;
860 case VLC_CODEC_RGB15:
861 p_picture->p->i_lines = i_height_aligned;
862 p_picture->p->i_visible_lines = i_height;
863 p_picture->p->i_pitch = i_width_aligned * 2;
864 p_picture->p->i_visible_pitch = i_width * 2;
865 p_picture->p->i_pixel_pitch = 2;
866 p_picture->i_planes = 1;
869 case VLC_CODEC_RGB16:
870 p_picture->p->i_lines = i_height_aligned;
871 p_picture->p->i_visible_lines = i_height;
872 p_picture->p->i_pitch = i_width_aligned * 2;
873 p_picture->p->i_visible_pitch = i_width * 2;
874 p_picture->p->i_pixel_pitch = 2;
875 p_picture->i_planes = 1;
878 case VLC_CODEC_RGB24:
879 p_picture->p->i_lines = i_height_aligned;
880 p_picture->p->i_visible_lines = i_height;
881 p_picture->p->i_pitch = i_width_aligned * 3;
882 p_picture->p->i_visible_pitch = i_width * 3;
883 p_picture->p->i_pixel_pitch = 3;
884 p_picture->i_planes = 1;
887 case VLC_CODEC_RGB32:
889 p_picture->p->i_lines = i_height_aligned;
890 p_picture->p->i_visible_lines = i_height;
891 p_picture->p->i_pitch = i_width_aligned * 4;
892 p_picture->p->i_visible_pitch = i_width * 4;
893 p_picture->p->i_pixel_pitch = 4;
894 p_picture->i_planes = 1;
899 p_picture->p->i_lines = i_height_aligned;
900 p_picture->p->i_visible_lines = i_height;
901 p_picture->p->i_pitch = i_width_aligned;
902 p_picture->p->i_visible_pitch = i_width;
903 p_picture->p->i_pixel_pitch = 1;
904 p_picture->i_planes = 1;
908 p_picture->i_planes = 0;
915 /*****************************************************************************
917 *****************************************************************************/
918 picture_t *picture_NewFromResource( const video_format_t *p_fmt, const picture_resource_t *p_resource )
920 video_format_t fmt = *p_fmt;
922 /* It is needed to be sure all informations are filled */
923 video_format_Setup( &fmt, p_fmt->i_chroma,
924 p_fmt->i_width, p_fmt->i_height,
925 p_fmt->i_sar_num, p_fmt->i_sar_den );
928 picture_t *p_picture = calloc( 1, sizeof(*p_picture) );
934 if( picture_Setup( p_picture, fmt.i_chroma, fmt.i_width, fmt.i_height,
935 fmt.i_sar_num, fmt.i_sar_den ) )
940 p_picture->p_sys = p_resource->p_sys;
942 for( int i = 0; i < p_picture->i_planes; i++ )
944 p_picture->p[i].p_pixels = p_resource->p[i].p_pixels;
945 p_picture->p[i].i_lines = p_resource->p[i].i_lines;
946 p_picture->p[i].i_pitch = p_resource->p[i].i_pitch;
951 if( vout_AllocatePicture( (vlc_object_t *)NULL, p_picture,
952 fmt.i_chroma, fmt.i_width, fmt.i_height,
953 fmt.i_sar_num, fmt.i_sar_den ) )
960 p_picture->format = fmt;
961 p_picture->i_refcount = 1;
962 p_picture->pf_release = PictureReleaseCallback;
963 p_picture->i_status = RESERVED_PICTURE;
967 picture_t *picture_NewFromFormat( const video_format_t *p_fmt )
969 return picture_NewFromResource( p_fmt, NULL );
971 picture_t *picture_New( vlc_fourcc_t i_chroma, int i_width, int i_height, int i_sar_num, int i_sar_den )
975 memset( &fmt, 0, sizeof(fmt) );
976 video_format_Setup( &fmt, i_chroma, i_width, i_height,
977 i_sar_num, i_sar_den );
979 return picture_NewFromFormat( &fmt );
982 /*****************************************************************************
984 *****************************************************************************/
985 void picture_Delete( picture_t *p_picture )
987 assert( p_picture && p_picture->i_refcount == 0 );
988 assert( p_picture->p_release_sys == NULL );
990 free( p_picture->p_q );
991 free( p_picture->p_data_orig );
992 free( p_picture->p_sys );
996 /*****************************************************************************
998 *****************************************************************************/
999 void picture_CopyPixels( picture_t *p_dst, const picture_t *p_src )
1003 for( i = 0; i < p_src->i_planes ; i++ )
1004 plane_CopyPixels( p_dst->p+i, p_src->p+i );
1007 void plane_CopyPixels( plane_t *p_dst, const plane_t *p_src )
1009 const unsigned i_width = __MIN( p_dst->i_visible_pitch,
1010 p_src->i_visible_pitch );
1011 const unsigned i_height = __MIN( p_dst->i_visible_lines,
1012 p_src->i_visible_lines );
1014 if( p_src->i_pitch == p_dst->i_pitch )
1016 /* There are margins, but with the same width : perfect ! */
1017 vlc_memcpy( p_dst->p_pixels, p_src->p_pixels,
1018 p_src->i_pitch * i_height );
1022 /* We need to proceed line by line */
1023 uint8_t *p_in = p_src->p_pixels;
1024 uint8_t *p_out = p_dst->p_pixels;
1030 for( i_line = i_height; i_line--; )
1032 vlc_memcpy( p_out, p_in, i_width );
1033 p_in += p_src->i_pitch;
1034 p_out += p_dst->i_pitch;
1039 /*****************************************************************************
1041 *****************************************************************************/
1042 int picture_Export( vlc_object_t *p_obj,
1044 video_format_t *p_fmt,
1045 picture_t *p_picture,
1046 vlc_fourcc_t i_format,
1047 int i_override_width, int i_override_height )
1050 video_format_t fmt_in = p_picture->format;
1051 if( fmt_in.i_sar_num <= 0 || fmt_in.i_sar_den <= 0 )
1054 fmt_in.i_sar_den = 1;
1058 video_format_t fmt_out;
1059 memset( &fmt_out, 0, sizeof(fmt_out) );
1061 fmt_out.i_sar_den = 1;
1062 fmt_out.i_chroma = i_format;
1064 /* compute original width/height */
1065 unsigned int i_original_width;
1066 unsigned int i_original_height;
1067 if( fmt_in.i_sar_num >= fmt_in.i_sar_den )
1069 i_original_width = fmt_in.i_width * fmt_in.i_sar_num / fmt_in.i_sar_den;
1070 i_original_height = fmt_in.i_height;
1074 i_original_width = fmt_in.i_width;
1075 i_original_height = fmt_in.i_height * fmt_in.i_sar_den / fmt_in.i_sar_num;
1079 fmt_out.i_width = ( i_override_width < 0 ) ?
1080 i_original_width : i_override_width;
1081 fmt_out.i_height = ( i_override_height < 0 ) ?
1082 i_original_height : i_override_height;
1084 /* scale if only one direction is provided */
1085 if( fmt_out.i_height == 0 && fmt_out.i_width > 0 )
1087 fmt_out.i_height = fmt_in.i_height * fmt_out.i_width
1088 * fmt_in.i_sar_den / fmt_in.i_width / fmt_in.i_sar_num;
1090 else if( fmt_out.i_width == 0 && fmt_out.i_height > 0 )
1092 fmt_out.i_width = fmt_in.i_width * fmt_out.i_height
1093 * fmt_in.i_sar_num / fmt_in.i_height / fmt_in.i_sar_den;
1096 image_handler_t *p_image = image_HandlerCreate( p_obj );
1098 block_t *p_block = image_Write( p_image, p_picture, &fmt_in, &fmt_out );
1100 image_HandlerDelete( p_image );
1103 return VLC_EGENERIC;
1106 p_block->i_dts = p_picture->date;
1110 *pp_image = p_block;