vout_pictures.c
上传用户:riyaled888
上传日期:2009-03-27
资源大小:7338k
文件大小:32k
源码类别:

多媒体

开发平台:

MultiPlatform

  1. /*****************************************************************************
  2.  * vout_pictures.c : picture management functions
  3.  *****************************************************************************
  4.  * Copyright (C) 2000-2004 VideoLAN
  5.  * $Id: vout_pictures.c 9268 2004-11-10 13:01:48Z gbazin $
  6.  *
  7.  * Authors: Vincent Seguin <seguin@via.ecp.fr>
  8.  *          Samuel Hocevar <sam@zoy.org>
  9.  *
  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.
  14.  *
  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.
  19.  *
  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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
  23.  *****************************************************************************/
  24. /*****************************************************************************
  25.  * Preamble
  26.  *****************************************************************************/
  27. #include <stdlib.h>                                                /* free() */
  28. #include <stdio.h>                                              /* sprintf() */
  29. #include <string.h>                                            /* strerror() */
  30. #include <vlc/vlc.h>
  31. #include "vlc_video.h"
  32. #include "video_output.h"
  33. #include "vlc_spu.h"
  34. #include "vout_pictures.h"
  35. /**
  36.  * Display a picture
  37.  *
  38.  * Remove the reservation flag of a picture, which will cause it to be ready
  39.  * for display. The picture won't be displayed until vout_DatePicture has been
  40.  * called.
  41.  */
  42. void vout_DisplayPicture( vout_thread_t *p_vout, picture_t *p_pic )
  43. {
  44.     vlc_mutex_lock( &p_vout->picture_lock );
  45.     switch( p_pic->i_status )
  46.     {
  47.     case RESERVED_PICTURE:
  48.         p_pic->i_status = RESERVED_DISP_PICTURE;
  49.         break;
  50.     case RESERVED_DATED_PICTURE:
  51.         p_pic->i_status = READY_PICTURE;
  52.         break;
  53.     default:
  54.         msg_Err( p_vout, "picture to display %p has invalid status %d",
  55.                          p_pic, p_pic->i_status );
  56.         break;
  57.     }
  58.     vlc_mutex_unlock( &p_vout->picture_lock );
  59. }
  60. /**
  61.  * Date a picture
  62.  *
  63.  * Remove the reservation flag of a picture, which will cause it to be ready
  64.  * for display. The picture won't be displayed until vout_DisplayPicture has
  65.  * been called.
  66.  * param p_vout The vout in question
  67.  * param p_pic The picture to date
  68.  * param date The date to display the picture
  69.  */
  70. void vout_DatePicture( vout_thread_t *p_vout,
  71.                        picture_t *p_pic, mtime_t date )
  72. {
  73.     vlc_mutex_lock( &p_vout->picture_lock );
  74.     p_pic->date = date;
  75.     switch( p_pic->i_status )
  76.     {
  77.     case RESERVED_PICTURE:
  78.         p_pic->i_status = RESERVED_DATED_PICTURE;
  79.         break;
  80.     case RESERVED_DISP_PICTURE:
  81.         p_pic->i_status = READY_PICTURE;
  82.         break;
  83.     default:
  84.         msg_Err( p_vout, "picture to date %p has invalid status %d",
  85.                          p_pic, p_pic->i_status );
  86.         break;
  87.     }
  88.     vlc_mutex_unlock( &p_vout->picture_lock );
  89. }
  90. /**
  91.  * Allocate a picture in the video output heap.
  92.  *
  93.  * This function creates a reserved image in the video output heap.
  94.  * A null pointer is returned if the function fails. This method provides an
  95.  * already allocated zone of memory in the picture data fields.
  96.  * It needs locking since several pictures can be created by several producers
  97.  * threads.
  98.  */
  99. picture_t *vout_CreatePicture( vout_thread_t *p_vout,
  100.                                vlc_bool_t b_progressive,
  101.                                vlc_bool_t b_top_field_first,
  102.                                unsigned int i_nb_fields )
  103. {
  104.     int         i_pic;                                      /* picture index */
  105.     picture_t * p_pic;
  106.     picture_t * p_freepic = NULL;                      /* first free picture */
  107.     /* Get lock */
  108.     vlc_mutex_lock( &p_vout->picture_lock );
  109.     /*
  110.      * Look for an empty place in the picture heap.
  111.      */
  112.     for( i_pic = 0; i_pic < I_RENDERPICTURES; i_pic++ )
  113.     {
  114.         p_pic = PP_RENDERPICTURE[(p_vout->render.i_last_used_pic + i_pic + 1)
  115.                                  % I_RENDERPICTURES];
  116.         switch( p_pic->i_status )
  117.         {
  118.             case DESTROYED_PICTURE:
  119.                 /* Memory will not be reallocated, and function can end
  120.                  * immediately - this is the best possible case, since no
  121.                  * memory allocation needs to be done */
  122.                 p_pic->i_status   = RESERVED_PICTURE;
  123.                 p_pic->i_refcount = 0;
  124.                 p_pic->b_force    = 0;
  125.                 p_pic->b_progressive        = b_progressive;
  126.                 p_pic->i_nb_fields          = i_nb_fields;
  127.                 p_pic->b_top_field_first    = b_top_field_first;
  128.                 p_vout->i_heap_size++;
  129.                 p_vout->render.i_last_used_pic =
  130.                     ( p_vout->render.i_last_used_pic + i_pic + 1 )
  131.                     % I_RENDERPICTURES;
  132.                 vlc_mutex_unlock( &p_vout->picture_lock );
  133.                 return( p_pic );
  134.             case FREE_PICTURE:
  135.                 /* Picture is empty and ready for allocation */
  136.                 p_vout->render.i_last_used_pic =
  137.                     ( p_vout->render.i_last_used_pic + i_pic + 1 )
  138.                     % I_RENDERPICTURES;
  139.                 p_freepic = p_pic;
  140.                 break;
  141.             default:
  142.                 break;
  143.         }
  144.     }
  145.     /*
  146.      * Prepare picture
  147.      */
  148.     if( p_freepic != NULL )
  149.     {
  150.         vout_AllocatePicture( VLC_OBJECT(p_vout),
  151.                               p_freepic, p_vout->render.i_chroma,
  152.                               p_vout->render.i_width, p_vout->render.i_height,
  153.                               p_vout->render.i_aspect );
  154.         if( p_freepic->i_planes )
  155.         {
  156.             /* Copy picture information, set some default values */
  157.             p_freepic->i_status   = RESERVED_PICTURE;
  158.             p_freepic->i_type     = MEMORY_PICTURE;
  159.             p_freepic->b_slow     = 0;
  160.             p_freepic->i_refcount = 0;
  161.             p_freepic->b_force = 0;
  162.             p_freepic->b_progressive        = b_progressive;
  163.             p_freepic->i_nb_fields          = i_nb_fields;
  164.             p_freepic->b_top_field_first    = b_top_field_first;
  165.             p_freepic->i_matrix_coefficients = 1;
  166.             p_vout->i_heap_size++;
  167.         }
  168.         else
  169.         {
  170.             /* Memory allocation failed : set picture as empty */
  171.             p_freepic->i_status = FREE_PICTURE;
  172.             p_freepic = NULL;
  173.             msg_Err( p_vout, "picture allocation failed" );
  174.         }
  175.         vlc_mutex_unlock( &p_vout->picture_lock );
  176.         return( p_freepic );
  177.     }
  178.     /* No free or destroyed picture could be found, but the decoder
  179.      * will try again in a while. */
  180.     vlc_mutex_unlock( &p_vout->picture_lock );
  181.     return( NULL );
  182. }
  183. /**
  184.  * Remove a permanent or reserved picture from the heap
  185.  *
  186.  * This function frees a previously reserved picture or a permanent
  187.  * picture. It is meant to be used when the construction of a picture aborted.
  188.  * Note that the picture will be destroyed even if it is linked !
  189.  */
  190. void vout_DestroyPicture( vout_thread_t *p_vout, picture_t *p_pic )
  191. {
  192.     vlc_mutex_lock( &p_vout->picture_lock );
  193. #ifdef DEBUG
  194.     /* Check if picture status is valid */
  195.     if( (p_pic->i_status != RESERVED_PICTURE) &&
  196.         (p_pic->i_status != RESERVED_DATED_PICTURE) &&
  197.         (p_pic->i_status != RESERVED_DISP_PICTURE) )
  198.     {
  199.         msg_Err( p_vout, "picture to destroy %p has invalid status %d",
  200.                          p_pic, p_pic->i_status );
  201.     }
  202. #endif
  203.     p_pic->i_status = DESTROYED_PICTURE;
  204.     p_vout->i_heap_size--;
  205.     vlc_mutex_unlock( &p_vout->picture_lock );
  206. }
  207. /**
  208.  * Increment reference counter of a picture
  209.  *
  210.  * This function increments the reference counter of a picture in the video
  211.  * heap. It needs a lock since several producer threads can access the picture.
  212.  */
  213. void vout_LinkPicture( vout_thread_t *p_vout, picture_t *p_pic )
  214. {
  215.     vlc_mutex_lock( &p_vout->picture_lock );
  216.     p_pic->i_refcount++;
  217.     vlc_mutex_unlock( &p_vout->picture_lock );
  218. }
  219. /**
  220.  * Decrement reference counter of a picture
  221.  *
  222.  * This function decrement the reference counter of a picture in the video heap
  223.  */
  224. void vout_UnlinkPicture( vout_thread_t *p_vout, picture_t *p_pic )
  225. {
  226.     vlc_mutex_lock( &p_vout->picture_lock );
  227.     p_pic->i_refcount--;
  228.     if( p_pic->i_refcount < 0 )
  229.     {
  230.         msg_Err( p_vout, "picture %p refcount is %i",
  231.                  p_pic, p_pic->i_refcount );
  232.         p_pic->i_refcount = 0;
  233.     }
  234.     if( ( p_pic->i_refcount == 0 ) &&
  235.         ( p_pic->i_status == DISPLAYED_PICTURE ) )
  236.     {
  237.         p_pic->i_status = DESTROYED_PICTURE;
  238.         p_vout->i_heap_size--;
  239.     }
  240.     vlc_mutex_unlock( &p_vout->picture_lock );
  241. }
  242. /**
  243.  * Render a picture
  244.  *
  245.  * This function chooses whether the current picture needs to be copied
  246.  * before rendering, does the subpicture magic, and tells the video output
  247.  * thread which direct buffer needs to be displayed.
  248.  */
  249. picture_t * vout_RenderPicture( vout_thread_t *p_vout, picture_t *p_pic,
  250.                                                        subpicture_t *p_subpic )
  251. {
  252.     video_format_t fmt;
  253.     int i_scale_width, i_scale_height;
  254.     if( p_pic == NULL )
  255.     {
  256.         /* XXX: subtitles */
  257.         return NULL;
  258.     }
  259.     fmt.i_aspect = p_vout->output.i_aspect;
  260.     fmt.i_chroma = p_vout->output.i_chroma;
  261.     fmt.i_width = p_vout->output.i_width;
  262.     fmt.i_height = p_vout->output.i_height;
  263.     fmt.i_sar_num = p_vout->output.i_aspect * fmt.i_height / fmt.i_width;
  264.     fmt.i_sar_den = VOUT_ASPECT_FACTOR;
  265.     i_scale_width = p_vout->output.i_width * 1000 / p_vout->render.i_width;
  266.     i_scale_height = p_vout->output.i_height * 1000 / p_vout->render.i_height;
  267.     if( p_pic->i_type == DIRECT_PICTURE )
  268.     {
  269.         if( !p_vout->render.b_allow_modify_pics || p_pic->i_refcount ||
  270.             p_pic->b_force )
  271.         {
  272.             /* Picture is in a direct buffer and is still in use,
  273.              * we need to copy it to another direct buffer before
  274.              * displaying it if there are subtitles. */
  275.             if( p_subpic != NULL )
  276.             {
  277.                 /* We have subtitles. First copy the picture to
  278.                  * the spare direct buffer, then render the
  279.                  * subtitles. */
  280.                 vout_CopyPicture( p_vout, PP_OUTPUTPICTURE[0], p_pic );
  281.                 spu_RenderSubpictures( p_vout->p_spu, &fmt,
  282.                                        PP_OUTPUTPICTURE[0], p_pic, p_subpic,
  283.                                        i_scale_width, i_scale_height );
  284.                 return PP_OUTPUTPICTURE[0];
  285.             }
  286.             /* No subtitles, picture is in a directbuffer so
  287.              * we can display it directly even if it is still
  288.              * in use. */
  289.             return p_pic;
  290.         }
  291.         /* Picture is in a direct buffer but isn't used by the
  292.          * decoder. We can safely render subtitles on it and
  293.          * display it. */
  294.         spu_RenderSubpictures( p_vout->p_spu, &fmt, p_pic, p_pic, p_subpic,
  295.                                i_scale_width, i_scale_height );
  296.         return p_pic;
  297.     }
  298.     /* Not a direct buffer. We either need to copy it to a direct buffer,
  299.      * or render it if the chroma isn't the same. */
  300.     if( p_vout->b_direct )
  301.     {
  302.         /* Picture is not in a direct buffer, but is exactly the
  303.          * same size as the direct buffers. A memcpy() is enough,
  304.          * then render the subtitles. */
  305.         if( PP_OUTPUTPICTURE[0]->pf_lock )
  306.             if( PP_OUTPUTPICTURE[0]->pf_lock( p_vout, PP_OUTPUTPICTURE[0] ) )
  307.                 return NULL;
  308.         vout_CopyPicture( p_vout, PP_OUTPUTPICTURE[0], p_pic );
  309.         spu_RenderSubpictures( p_vout->p_spu, &fmt, PP_OUTPUTPICTURE[0],
  310.                                p_pic, p_subpic, i_scale_width, i_scale_height);
  311.         if( PP_OUTPUTPICTURE[0]->pf_unlock )
  312.             PP_OUTPUTPICTURE[0]->pf_unlock( p_vout, PP_OUTPUTPICTURE[0] );
  313.         return PP_OUTPUTPICTURE[0];
  314.     }
  315.     /* Picture is not in a direct buffer, and needs to be converted to
  316.      * another size/chroma. Then the subtitles need to be rendered as
  317.      * well. This usually means software YUV, or hardware YUV with a
  318.      * different chroma. */
  319.     if( p_subpic != NULL && p_vout->p_picture[0].b_slow )
  320.     {
  321.         /* The picture buffer is in slow memory. We'll use
  322.          * the "2 * VOUT_MAX_PICTURES + 1" picture as a temporary
  323.          * one for subpictures rendering. */
  324.         picture_t *p_tmp_pic = &p_vout->p_picture[2 * VOUT_MAX_PICTURES];
  325.         if( p_tmp_pic->i_status == FREE_PICTURE )
  326.         {
  327.             vout_AllocatePicture( VLC_OBJECT(p_vout),
  328.                                   p_tmp_pic, p_vout->output.i_chroma,
  329.                                   p_vout->output.i_width,
  330.                                   p_vout->output.i_height,
  331.                                   p_vout->output.i_aspect );
  332.             p_tmp_pic->i_type = MEMORY_PICTURE;
  333.             p_tmp_pic->i_status = RESERVED_PICTURE;
  334.         }
  335.         /* Convert image to the first direct buffer */
  336.         p_vout->chroma.pf_convert( p_vout, p_pic, p_tmp_pic );
  337.         /* Render subpictures on the first direct buffer */
  338.         spu_RenderSubpictures( p_vout->p_spu, &fmt, p_tmp_pic,
  339.                                p_tmp_pic, p_subpic,
  340.                                i_scale_width, i_scale_height );
  341.         if( p_vout->p_picture[0].pf_lock )
  342.             if( p_vout->p_picture[0].pf_lock( p_vout, &p_vout->p_picture[0] ) )
  343.                 return NULL;
  344.         vout_CopyPicture( p_vout, &p_vout->p_picture[0], p_tmp_pic );
  345.     }
  346.     else
  347.     {
  348.         if( p_vout->p_picture[0].pf_lock )
  349.             if( p_vout->p_picture[0].pf_lock( p_vout, &p_vout->p_picture[0] ) )
  350.                 return NULL;
  351.         /* Convert image to the first direct buffer */
  352.         p_vout->chroma.pf_convert( p_vout, p_pic, &p_vout->p_picture[0] );
  353.         /* Render subpictures on the first direct buffer */
  354.         spu_RenderSubpictures( p_vout->p_spu, &fmt, &p_vout->p_picture[0],
  355.                                &p_vout->p_picture[0], p_subpic,
  356.                                i_scale_width, i_scale_height );
  357.     }
  358.     if( p_vout->p_picture[0].pf_unlock )
  359.         p_vout->p_picture[0].pf_unlock( p_vout, &p_vout->p_picture[0] );
  360.     return &p_vout->p_picture[0];
  361. }
  362. /**
  363.  * Calculate image window coordinates
  364.  *
  365.  * This function will be accessed by plugins. It calculates the relative
  366.  * position of the output window and the image window.
  367.  */
  368. void vout_PlacePicture( vout_thread_t *p_vout,
  369.                         unsigned int i_width, unsigned int i_height,
  370.                         unsigned int *pi_x, unsigned int *pi_y,
  371.                         unsigned int *pi_width, unsigned int *pi_height )
  372. {
  373.     if( (i_width <= 0) || (i_height <=0) )
  374.     {
  375.         *pi_width = *pi_height = *pi_x = *pi_y = 0;
  376.         return;
  377.     }
  378.     if( p_vout->b_scale )
  379.     {
  380.         *pi_width = i_width;
  381.         *pi_height = i_height;
  382.     }
  383.     else
  384.     {
  385.         *pi_width = __MIN( i_width, p_vout->render.i_width );
  386.         *pi_height = __MIN( i_height, p_vout->render.i_height );
  387.     }
  388.     if( VOUT_ASPECT_FACTOR * *pi_width / *pi_height < p_vout->render.i_aspect )
  389.     {
  390.         *pi_width = *pi_height * p_vout->render.i_aspect / VOUT_ASPECT_FACTOR;
  391.     }
  392.     else
  393.     {
  394.         *pi_height = *pi_width * VOUT_ASPECT_FACTOR / p_vout->render.i_aspect;
  395.     }
  396.     if( *pi_width > i_width )
  397.     {
  398.         *pi_width = i_width;
  399.         *pi_height = VOUT_ASPECT_FACTOR * *pi_width / p_vout->render.i_aspect;
  400.     }
  401.     if( *pi_height > i_height )
  402.     {
  403.         *pi_height = i_height;
  404.         *pi_width = *pi_height * p_vout->render.i_aspect / VOUT_ASPECT_FACTOR;
  405.     }
  406.     switch( p_vout->i_alignment & VOUT_ALIGN_HMASK )
  407.     {
  408.     case VOUT_ALIGN_LEFT:
  409.         *pi_x = 0;
  410.         break;
  411.     case VOUT_ALIGN_RIGHT:
  412.         *pi_x = i_width - *pi_width;
  413.         break;
  414.     default:
  415.         *pi_x = ( i_width - *pi_width ) / 2;
  416.     }
  417.     switch( p_vout->i_alignment & VOUT_ALIGN_VMASK )
  418.     {
  419.     case VOUT_ALIGN_TOP:
  420.         *pi_y = 0;
  421.         break;
  422.     case VOUT_ALIGN_BOTTOM:
  423.         *pi_y = i_height - *pi_height;
  424.         break;
  425.     default:
  426.         *pi_y = ( i_height - *pi_height ) / 2;
  427.     }
  428. }
  429. /**
  430.  * Allocate a new picture in the heap.
  431.  *
  432.  * This function allocates a fake direct buffer in memory, which can be
  433.  * used exactly like a video buffer. The video output thread then manages
  434.  * how it gets displayed.
  435.  */
  436. int __vout_AllocatePicture( vlc_object_t *p_this, picture_t *p_pic,
  437.                             vlc_fourcc_t i_chroma,
  438.                             int i_width, int i_height, int i_aspect )
  439. {
  440.     int i_bytes, i_index, i_width_aligned, i_height_aligned;
  441.     /* Make sure the real dimensions are a multiple of 16 */
  442.     i_width_aligned = (i_width + 15) >> 4 << 4;
  443.     i_height_aligned = (i_height + 15) >> 4 << 4;
  444.     if( vout_InitPicture( p_this, p_pic, i_chroma,
  445.                           i_width, i_height, i_aspect ) != VLC_SUCCESS )
  446.     {
  447.         p_pic->i_planes = 0;
  448.         return VLC_EGENERIC;
  449.     }
  450.     /* Calculate how big the new image should be */
  451.     i_bytes = p_pic->format.i_bits_per_pixel *
  452.         i_width_aligned * i_height_aligned / 8;
  453.     p_pic->p_data = vlc_memalign( &p_pic->p_data_orig, 16, i_bytes );
  454.     if( p_pic->p_data == NULL )
  455.     {
  456.         p_pic->i_planes = 0;
  457.         return VLC_EGENERIC;
  458.     }
  459.     /* Fill the p_pixels field for each plane */
  460.     p_pic->p[ 0 ].p_pixels = p_pic->p_data;
  461.     for( i_index = 1; i_index < p_pic->i_planes; i_index++ )
  462.     {
  463.         p_pic->p[i_index].p_pixels = p_pic->p[i_index-1].p_pixels +
  464.             p_pic->p[i_index-1].i_lines * p_pic->p[i_index-1].i_pitch;
  465.     }
  466.     return VLC_SUCCESS;
  467. }
  468. /**
  469.  * Initialise the video format fields given chroma/size.
  470.  *
  471.  * This function initializes all the video_frame_format_t fields given the
  472.  * static properties of a picture (chroma and size).
  473.  * param p_format Pointer to the format structure to initialize
  474.  * param i_chroma Chroma to set
  475.  * param i_width Width to set
  476.  * param i_height Height to set
  477.  * param i_aspect Aspect ratio
  478.  */
  479. void vout_InitFormat( video_frame_format_t *p_format, vlc_fourcc_t i_chroma,
  480.                       int i_width, int i_height, int i_aspect )
  481. {
  482.     p_format->i_chroma   = i_chroma;
  483.     p_format->i_width    = p_format->i_visible_width  = i_width;
  484.     p_format->i_height   = p_format->i_visible_height = i_height;
  485.     p_format->i_x_offset = p_format->i_y_offset = 0;
  486.     p_format->i_aspect   = i_aspect;
  487. #if 0
  488.     /* Assume we have square pixels */
  489.     if( i_width && i_height )
  490.         p_format->i_aspect = i_width * VOUT_ASPECT_FACTOR / i_height;
  491.     else
  492.         p_format->i_aspect = 0;
  493. #endif
  494.     switch( i_chroma )
  495.     {
  496.         case FOURCC_YUVA:
  497.             p_format->i_bits_per_pixel = 32;
  498.             break;
  499.         case FOURCC_I444:
  500.             p_format->i_bits_per_pixel = 24;
  501.             break;
  502.         case FOURCC_I422:
  503.         case FOURCC_YUY2:
  504.         case FOURCC_UYVY:
  505.             p_format->i_bits_per_pixel = 16;
  506.             p_format->i_bits_per_pixel = 16;
  507.             break;
  508.         case FOURCC_I411:
  509.         case FOURCC_YV12:
  510.         case FOURCC_I420:
  511.         case FOURCC_IYUV:
  512.             p_format->i_bits_per_pixel = 12;
  513.             break;
  514.         case FOURCC_I410:
  515.         case FOURCC_YVU9:
  516.             p_format->i_bits_per_pixel = 9;
  517.             break;
  518.         case FOURCC_Y211:
  519.             p_format->i_bits_per_pixel = 8;
  520.             break;
  521.         case FOURCC_YUVP:
  522.             p_format->i_bits_per_pixel = 8;
  523.             break;
  524.         case FOURCC_RV32:
  525.             p_format->i_bits_per_pixel = 32;
  526.             break;
  527.         case FOURCC_RV24:
  528.             p_format->i_bits_per_pixel = 24;
  529.             break;
  530.         case FOURCC_RV15:
  531.         case FOURCC_RV16:
  532.             p_format->i_bits_per_pixel = 16;
  533.             break;
  534.         case FOURCC_RGB2:
  535.             p_format->i_bits_per_pixel = 8;
  536.             break;
  537.         default:
  538.             p_format->i_bits_per_pixel = 0;
  539.             break;
  540.     }
  541. }
  542. /**
  543.  * Initialise the picture_t fields given chroma/size.
  544.  *
  545.  * This function initializes most of the picture_t fields given a chroma and
  546.  * size. It makes the assumption that stride == width.
  547.  * param p_this The calling object
  548.  * param p_pic Pointer to the picture to initialize
  549.  * param i_chroma The chroma fourcc to set
  550.  * param i_width The width of the picture
  551.  * param i_height The height of the picture
  552.  * param i_aspect The aspect ratio of the picture
  553.  */
  554. int __vout_InitPicture( vlc_object_t *p_this, picture_t *p_pic,
  555.                         vlc_fourcc_t i_chroma,
  556.                         int i_width, int i_height, int i_aspect )
  557. {
  558.     int i_index, i_width_aligned, i_height_aligned;
  559.     /* Store default values */
  560.     for( i_index = 0; i_index < VOUT_MAX_PLANES; i_index++ )
  561.     {
  562.         p_pic->p[i_index].p_pixels = NULL;
  563.         p_pic->p[i_index].i_pixel_pitch = 1;
  564.     }
  565.     p_pic->pf_release = 0;
  566.     p_pic->pf_lock = 0;
  567.     p_pic->pf_unlock = 0;
  568.     p_pic->i_refcount = 0;
  569.     vout_InitFormat( &p_pic->format, i_chroma, i_width, i_height, i_aspect );
  570.     /* Make sure the real dimensions are a multiple of 16 */
  571.     i_width_aligned = (i_width + 15) >> 4 << 4;
  572.     i_height_aligned = (i_height + 15) >> 4 << 4;
  573.     /* Calculate coordinates */
  574.     switch( i_chroma )
  575.     {
  576.         case FOURCC_I411:
  577.             p_pic->p[ Y_PLANE ].i_lines = i_height_aligned;
  578.             p_pic->p[ Y_PLANE ].i_visible_lines = i_height;
  579.             p_pic->p[ Y_PLANE ].i_pitch = i_width_aligned;
  580.             p_pic->p[ Y_PLANE ].i_visible_pitch = i_width;
  581.             p_pic->p[ U_PLANE ].i_lines = i_height_aligned;
  582.             p_pic->p[ U_PLANE ].i_visible_lines = i_height;
  583.             p_pic->p[ U_PLANE ].i_pitch = i_width_aligned / 4;
  584.             p_pic->p[ U_PLANE ].i_visible_pitch = i_width / 4;
  585.             p_pic->p[ V_PLANE ].i_lines = i_height_aligned;
  586.             p_pic->p[ V_PLANE ].i_visible_lines = i_height;
  587.             p_pic->p[ V_PLANE ].i_pitch = i_width_aligned / 4;
  588.             p_pic->p[ V_PLANE ].i_visible_pitch = i_width / 4;
  589.             p_pic->i_planes = 3;
  590.             break;
  591.         case FOURCC_I410:
  592.         case FOURCC_YVU9:
  593.             p_pic->p[ Y_PLANE ].i_lines = i_height_aligned;
  594.             p_pic->p[ Y_PLANE ].i_visible_lines = i_height;
  595.             p_pic->p[ Y_PLANE ].i_pitch = i_width_aligned;
  596.             p_pic->p[ Y_PLANE ].i_visible_pitch = i_width;
  597.             p_pic->p[ U_PLANE ].i_lines = i_height_aligned / 4;
  598.             p_pic->p[ U_PLANE ].i_visible_lines = i_height / 4;
  599.             p_pic->p[ U_PLANE ].i_pitch = i_width_aligned / 4;
  600.             p_pic->p[ U_PLANE ].i_visible_pitch = i_width / 4;
  601.             p_pic->p[ V_PLANE ].i_lines = i_height_aligned / 4;
  602.             p_pic->p[ V_PLANE ].i_visible_lines = i_height / 4;
  603.             p_pic->p[ V_PLANE ].i_pitch = i_width_aligned / 4;
  604.             p_pic->p[ V_PLANE ].i_visible_pitch = i_width / 4;
  605.             p_pic->i_planes = 3;
  606.             break;
  607.         case FOURCC_YV12:
  608.         case FOURCC_I420:
  609.         case FOURCC_IYUV:
  610.             p_pic->p[ Y_PLANE ].i_lines = i_height_aligned;
  611.             p_pic->p[ Y_PLANE ].i_visible_lines = i_height;
  612.             p_pic->p[ Y_PLANE ].i_pitch = i_width_aligned;
  613.             p_pic->p[ Y_PLANE ].i_visible_pitch = i_width;
  614.             p_pic->p[ U_PLANE ].i_lines = i_height_aligned / 2;
  615.             p_pic->p[ U_PLANE ].i_visible_lines = i_height / 2;
  616.             p_pic->p[ U_PLANE ].i_pitch = i_width_aligned / 2;
  617.             p_pic->p[ U_PLANE ].i_visible_pitch = i_width / 2;
  618.             p_pic->p[ V_PLANE ].i_lines = i_height_aligned / 2;
  619.             p_pic->p[ V_PLANE ].i_visible_lines = i_height / 2;
  620.             p_pic->p[ V_PLANE ].i_pitch = i_width_aligned / 2;
  621.             p_pic->p[ V_PLANE ].i_visible_pitch = i_width / 2;
  622.             p_pic->i_planes = 3;
  623.             break;
  624.         case FOURCC_I422:
  625.             p_pic->p[ Y_PLANE ].i_lines = i_height_aligned;
  626.             p_pic->p[ Y_PLANE ].i_visible_lines = i_height;
  627.             p_pic->p[ Y_PLANE ].i_pitch = i_width_aligned;
  628.             p_pic->p[ Y_PLANE ].i_visible_pitch = i_width;
  629.             p_pic->p[ U_PLANE ].i_lines = i_height_aligned;
  630.             p_pic->p[ U_PLANE ].i_visible_lines = i_height;
  631.             p_pic->p[ U_PLANE ].i_pitch = i_width_aligned / 2;
  632.             p_pic->p[ U_PLANE ].i_visible_pitch = i_width / 2;
  633.             p_pic->p[ V_PLANE ].i_lines = i_height_aligned;
  634.             p_pic->p[ V_PLANE ].i_visible_lines = i_height;
  635.             p_pic->p[ V_PLANE ].i_pitch = i_width_aligned / 2;
  636.             p_pic->p[ V_PLANE ].i_visible_pitch = i_width / 2;
  637.             p_pic->i_planes = 3;
  638.             break;
  639.         case FOURCC_I444:
  640.             p_pic->p[ Y_PLANE ].i_lines = i_height_aligned;
  641.             p_pic->p[ Y_PLANE ].i_visible_lines = i_height;
  642.             p_pic->p[ Y_PLANE ].i_pitch = i_width_aligned;
  643.             p_pic->p[ Y_PLANE ].i_visible_pitch = i_width;
  644.             p_pic->p[ U_PLANE ].i_lines = i_height_aligned;
  645.             p_pic->p[ U_PLANE ].i_visible_lines = i_height;
  646.             p_pic->p[ U_PLANE ].i_pitch = i_width_aligned;
  647.             p_pic->p[ U_PLANE ].i_visible_pitch = i_width;
  648.             p_pic->p[ V_PLANE ].i_lines = i_height_aligned;
  649.             p_pic->p[ V_PLANE ].i_visible_lines = i_height;
  650.             p_pic->p[ V_PLANE ].i_pitch = i_width_aligned;
  651.             p_pic->p[ V_PLANE ].i_visible_pitch = i_width;
  652.             p_pic->i_planes = 3;
  653.             break;
  654.         case FOURCC_YUVA:
  655.             p_pic->p[ Y_PLANE ].i_lines = i_height_aligned;
  656.             p_pic->p[ Y_PLANE ].i_visible_lines = i_height;
  657.             p_pic->p[ Y_PLANE ].i_pitch = i_width_aligned;
  658.             p_pic->p[ Y_PLANE ].i_visible_pitch = i_width;
  659.             p_pic->p[ U_PLANE ].i_lines = i_height_aligned;
  660.             p_pic->p[ U_PLANE ].i_visible_lines = i_height;
  661.             p_pic->p[ U_PLANE ].i_pitch = i_width_aligned;
  662.             p_pic->p[ U_PLANE ].i_visible_pitch = i_width;
  663.             p_pic->p[ V_PLANE ].i_lines = i_height_aligned;
  664.             p_pic->p[ V_PLANE ].i_visible_lines = i_height;
  665.             p_pic->p[ V_PLANE ].i_pitch = i_width_aligned;
  666.             p_pic->p[ V_PLANE ].i_visible_pitch = i_width;
  667.             p_pic->p[ A_PLANE ].i_lines = i_height_aligned;
  668.             p_pic->p[ A_PLANE ].i_visible_lines = i_height;
  669.             p_pic->p[ A_PLANE ].i_pitch = i_width_aligned;
  670.             p_pic->p[ A_PLANE ].i_visible_pitch = i_width;
  671.             p_pic->i_planes = 4;
  672.             break;
  673.         case FOURCC_YUVP:
  674.             p_pic->p->i_lines = i_height_aligned;
  675.             p_pic->p->i_visible_lines = i_height;
  676.             p_pic->p->i_pitch = i_width_aligned;
  677.             p_pic->p->i_visible_pitch = i_width;
  678.             p_pic->p->i_pixel_pitch = 8;
  679.             p_pic->i_planes = 1;
  680.             break;
  681.         case FOURCC_Y211:
  682.             p_pic->p->i_lines = i_height_aligned;
  683.             p_pic->p->i_visible_lines = i_height;
  684.             p_pic->p->i_pitch = i_width_aligned;
  685.             p_pic->p->i_visible_pitch = i_width;
  686.             p_pic->p->i_pixel_pitch = 4;
  687.             p_pic->i_planes = 1;
  688.             break;
  689.         case FOURCC_UYVY:
  690.         case FOURCC_YUY2:
  691.             p_pic->p->i_lines = i_height_aligned;
  692.             p_pic->p->i_visible_lines = i_height;
  693.             p_pic->p->i_pitch = i_width_aligned * 2;
  694.             p_pic->p->i_visible_pitch = i_width * 2;
  695.             p_pic->p->i_pixel_pitch = 4;
  696.             p_pic->i_planes = 1;
  697.             break;
  698.         case FOURCC_RGB2:
  699.             p_pic->p->i_lines = i_height_aligned;
  700.             p_pic->p->i_visible_lines = i_height;
  701.             p_pic->p->i_pitch = i_width_aligned;
  702.             p_pic->p->i_visible_pitch = i_width;
  703.             p_pic->p->i_pixel_pitch = 1;
  704.             p_pic->i_planes = 1;
  705.             break;
  706.         case FOURCC_RV15:
  707.             p_pic->p->i_lines = i_height_aligned;
  708.             p_pic->p->i_visible_lines = i_height;
  709.             p_pic->p->i_pitch = i_width_aligned * 2;
  710.             p_pic->p->i_visible_pitch = i_width * 2;
  711.             p_pic->p->i_pixel_pitch = 2;
  712.             p_pic->i_planes = 1;
  713.             break;
  714.         case FOURCC_RV16:
  715.             p_pic->p->i_lines = i_height_aligned;
  716.             p_pic->p->i_visible_lines = i_height;
  717.             p_pic->p->i_pitch = i_width_aligned * 2;
  718.             p_pic->p->i_visible_pitch = i_width * 2;
  719.             p_pic->p->i_pixel_pitch = 2;
  720.             p_pic->i_planes = 1;
  721.             break;
  722.         case FOURCC_RV24:
  723.             p_pic->p->i_lines = i_height_aligned;
  724.             p_pic->p->i_visible_lines = i_height;
  725.             p_pic->p->i_pitch = i_width_aligned * 3;
  726.             p_pic->p->i_visible_pitch = i_width * 3;
  727.             p_pic->p->i_pixel_pitch = 3;
  728.             p_pic->i_planes = 1;
  729.             break;
  730.         case FOURCC_RV32:
  731.             p_pic->p->i_lines = i_height_aligned;
  732.             p_pic->p->i_visible_lines = i_height;
  733.             p_pic->p->i_pitch = i_width_aligned * 4;
  734.             p_pic->p->i_visible_pitch = i_width * 4;
  735.             p_pic->p->i_pixel_pitch = 4;
  736.             p_pic->i_planes = 1;
  737.             break;
  738.         default:
  739.             msg_Err( p_this, "unknown chroma type 0x%.8x (%4.4s)",
  740.                              i_chroma, (char*)&i_chroma );
  741.             p_pic->i_planes = 0;
  742.             return VLC_EGENERIC;
  743.     }
  744.     return VLC_SUCCESS;
  745. }
  746. /**
  747.  * Compare two chroma values
  748.  *
  749.  * This function returns 1 if the two fourcc values given as argument are
  750.  * the same format (eg. UYVY/UYNV) or almost the same format (eg. I420/YV12)
  751.  */
  752. int vout_ChromaCmp( vlc_fourcc_t i_chroma, vlc_fourcc_t i_amorhc )
  753. {
  754.     /* If they are the same, they are the same ! */
  755.     if( i_chroma == i_amorhc )
  756.     {
  757.         return 1;
  758.     }
  759.     /* Check for equivalence classes */
  760.     switch( i_chroma )
  761.     {
  762.         case FOURCC_I420:
  763.         case FOURCC_IYUV:
  764.         case FOURCC_YV12:
  765.             switch( i_amorhc )
  766.             {
  767.                 case FOURCC_I420:
  768.                 case FOURCC_IYUV:
  769.                 case FOURCC_YV12:
  770.                     return 1;
  771.                 default:
  772.                     return 0;
  773.             }
  774.         case FOURCC_UYVY:
  775.         case FOURCC_UYNV:
  776.         case FOURCC_Y422:
  777.             switch( i_amorhc )
  778.             {
  779.                 case FOURCC_UYVY:
  780.                 case FOURCC_UYNV:
  781.                 case FOURCC_Y422:
  782.                     return 1;
  783.                 default:
  784.                     return 0;
  785.             }
  786.         case FOURCC_YUY2:
  787.         case FOURCC_YUNV:
  788.             switch( i_amorhc )
  789.             {
  790.                 case FOURCC_YUY2:
  791.                 case FOURCC_YUNV:
  792.                     return 1;
  793.                 default:
  794.                     return 0;
  795.             }
  796.         default:
  797.             return 0;
  798.     }
  799. }
  800. /*****************************************************************************
  801.  * vout_CopyPicture: copy a picture to another one
  802.  *****************************************************************************
  803.  * This function takes advantage of the image format, and reduces the
  804.  * number of calls to memcpy() to the minimum. Source and destination
  805.  * images must have same width (hence i_visible_pitch), height, and chroma.
  806.  *****************************************************************************/
  807. void __vout_CopyPicture( vlc_object_t *p_this,
  808.                          picture_t *p_dest, picture_t *p_src )
  809. {
  810.     int i;
  811.     for( i = 0; i < p_src->i_planes ; i++ )
  812.     {
  813.         if( p_src->p[i].i_pitch == p_dest->p[i].i_pitch )
  814.         {
  815.             /* There are margins, but with the same width : perfect ! */
  816.             p_this->p_vlc->pf_memcpy(
  817.                          p_dest->p[i].p_pixels, p_src->p[i].p_pixels,
  818.                          p_src->p[i].i_pitch * p_src->p[i].i_visible_lines );
  819.         }
  820.         else
  821.         {
  822.             /* We need to proceed line by line */
  823.             uint8_t *p_in = p_src->p[i].p_pixels;
  824.             uint8_t *p_out = p_dest->p[i].p_pixels;
  825.             int i_line;
  826.             for( i_line = p_src->p[i].i_visible_lines; i_line--; )
  827.             {
  828.                 p_this->p_vlc->pf_memcpy( p_out, p_in,
  829.                                           p_src->p[i].i_visible_pitch );
  830.                 p_in += p_src->p[i].i_pitch;
  831.                 p_out += p_dest->p[i].i_pitch;
  832.             }
  833.         }
  834.     }
  835.     p_dest->date = p_src->date;
  836.     p_dest->b_force = p_src->b_force;
  837.     p_dest->i_nb_fields = p_src->i_nb_fields;
  838.     p_dest->b_progressive = p_src->b_progressive;
  839.     p_dest->b_top_field_first = p_src->b_top_field_first;
  840. }