sdl_image.c
上传用户:kjfoods
上传日期:2020-07-06
资源大小:29949k
文件大小:10k
源码类别:

midi

开发平台:

Unix_Linux

  1. /*****************************************************************************
  2.  * sdl_image.c: sdl decoder module making use of libsdl_image.
  3.  *****************************************************************************
  4.  * Copyright (C) 2005 the VideoLAN team
  5.  * $Id: d4eb387ab17cf7a65ad8be146747b01bbdde7af5 $
  6.  *
  7.  * Authors: Christophe Massiot <massiot@via.ecp.fr>
  8.  *
  9.  * This program is free software; you can redistribute it and/or modify
  10.  * it under the terms of the GNU General Public License as published by
  11.  * the Free Software Foundation; either version 2 of the License, or
  12.  * (at your option) any later version.
  13.  *
  14.  * This program is distributed in the hope that it will be useful,
  15.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17.  * GNU General Public License for more details.
  18.  *
  19.  * You should have received a copy of the GNU General Public License
  20.  * along with this program; if not, write to the Free Software
  21.  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  22.  *****************************************************************************/
  23. /*****************************************************************************
  24.  * Preamble
  25.  *****************************************************************************/
  26. #ifdef HAVE_CONFIG_H
  27. # include "config.h"
  28. #endif
  29. #include <vlc_common.h>
  30. #include <vlc_plugin.h>
  31. #include <vlc_codec.h>
  32. #include <vlc_vout.h>
  33. #include SDL_IMAGE_INCLUDE_FILE
  34. /*****************************************************************************
  35.  * decoder_sys_t : sdl decoder descriptor
  36.  *****************************************************************************/
  37. struct decoder_sys_t
  38. {
  39.     const char *psz_sdl_type;
  40. };
  41. /*****************************************************************************
  42.  * Local prototypes
  43.  *****************************************************************************/
  44. static int  OpenDecoder   ( vlc_object_t * );
  45. static void CloseDecoder  ( vlc_object_t * );
  46. static picture_t *DecodeBlock  ( decoder_t *, block_t ** );
  47. /*****************************************************************************
  48.  * Module descriptor
  49.  *****************************************************************************/
  50. vlc_module_begin ()
  51.     set_category( CAT_INPUT )
  52.     set_subcategory( SUBCAT_INPUT_VCODEC )
  53.     set_shortname( N_("SDL Image decoder"))
  54.     set_description( N_("SDL_image video decoder") )
  55.     set_capability( "decoder", 60 )
  56.     set_callbacks( OpenDecoder, CloseDecoder )
  57.     add_shortcut( "sdl_image" )
  58. vlc_module_end ()
  59. static const struct supported_fmt_t
  60. {
  61.     vlc_fourcc_t i_fourcc;
  62.     const char *psz_sdl_type;
  63. } p_supported_fmt[] =
  64. {
  65.     { VLC_FOURCC('t','g','a',' '), "TGA" },
  66.     { VLC_FOURCC('b','m','p',' '), "BMP" },
  67.     { VLC_FOURCC('p','n','m',' '), "PNM" },
  68.     { VLC_FOURCC('x','p','m',' '), "XPM" },
  69.     { VLC_FOURCC('x','c','f',' '), "XCF" },
  70.     { VLC_FOURCC('p','c','x',' '), "PCX" },
  71.     { VLC_FOURCC('g','i','f',' '), "GIF" },
  72.     { VLC_FOURCC('j','p','e','g'), "JPG" },
  73.     { VLC_FOURCC('t','i','f','f'), "TIF" },
  74.     { VLC_FOURCC('l','b','m',' '), "LBM" },
  75.     { VLC_FOURCC('p','n','g',' '), "PNG" }
  76. };
  77. /*****************************************************************************
  78.  * OpenDecoder: probe the decoder and return score
  79.  *****************************************************************************/
  80. static int OpenDecoder( vlc_object_t *p_this )
  81. {
  82.     decoder_t *p_dec = (decoder_t *)p_this;
  83.     decoder_sys_t *p_sys;
  84.     int i;
  85.     /* Find codec. */
  86.     for ( i = 0;
  87.           i < (int)(sizeof(p_supported_fmt)/sizeof(struct supported_fmt_t));
  88.           i++ )
  89.     {
  90.         if ( p_supported_fmt[i].i_fourcc == p_dec->fmt_in.i_codec )
  91.             break;
  92.     }
  93.     if ( i == (int)(sizeof(p_supported_fmt)/sizeof(struct supported_fmt_t)) )
  94.     {
  95.         return VLC_EGENERIC;
  96.     }
  97.     /* Allocate the memory needed to store the decoder's structure */
  98.     if( ( p_dec->p_sys = p_sys =
  99.           (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
  100.         return VLC_ENOMEM;
  101.     p_sys->psz_sdl_type = p_supported_fmt[i].psz_sdl_type;
  102.     /* Set output properties - this is a decoy and isn't used anywhere */
  103.     p_dec->fmt_out.i_cat = VIDEO_ES;
  104.     p_dec->fmt_out.i_codec = VLC_FOURCC('R','V','3','2');
  105.     /* Set callbacks */
  106.     p_dec->pf_decode_video = DecodeBlock;
  107.     return VLC_SUCCESS;
  108. }
  109. /****************************************************************************
  110.  * DecodeBlock: the whole thing
  111.  ****************************************************************************
  112.  * This function must be fed with a complete compressed frame.
  113.  ****************************************************************************/
  114. static picture_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
  115. {
  116.     decoder_sys_t *p_sys = p_dec->p_sys;
  117.     block_t *p_block;
  118.     picture_t *p_pic = NULL;
  119.     SDL_Surface *p_surface;
  120.     SDL_RWops *p_rw;
  121.     if( pp_block == NULL || *pp_block == NULL ) return NULL;
  122.     p_block = *pp_block;
  123.     if( p_block->i_flags & BLOCK_FLAG_DISCONTINUITY )
  124.     {
  125.         block_Release( p_block ); *pp_block = NULL;
  126.         return NULL;
  127.     }
  128.     p_rw = SDL_RWFromConstMem( p_block->p_buffer, p_block->i_buffer );
  129.     /* Decode picture. */
  130.     p_surface = IMG_LoadTyped_RW( p_rw, 1, (char*)p_sys->psz_sdl_type );
  131.     if ( p_surface == NULL )
  132.     {
  133.         msg_Warn( p_dec, "SDL_image couldn't load the image (%s)",
  134.                   IMG_GetError() );
  135.         goto error;
  136.     }
  137.     switch ( p_surface->format->BitsPerPixel )
  138.     {
  139.     case 16:
  140.         p_dec->fmt_out.i_codec = VLC_FOURCC('R','V','1','6');
  141.         break;
  142.     case 8:
  143.     case 24:
  144.         p_dec->fmt_out.i_codec = VLC_FOURCC('R','V','2','4');
  145.         break;
  146.     case 32:
  147.         p_dec->fmt_out.i_codec = VLC_FOURCC('R','V','3','2');
  148.         break;
  149.     default:
  150.         msg_Warn( p_dec, "unknown bits/pixel format (%d)",
  151.                   p_surface->format->BitsPerPixel );
  152.         goto error;
  153.     }
  154.     p_dec->fmt_out.video.i_width = p_surface->w;
  155.     p_dec->fmt_out.video.i_height = p_surface->h;
  156.     p_dec->fmt_out.video.i_aspect = VOUT_ASPECT_FACTOR * p_surface->w
  157.                                      / p_surface->h;
  158.     /* Get a new picture. */
  159.     p_pic = decoder_NewPicture( p_dec );
  160.     if ( p_pic == NULL ) goto error;
  161.     switch ( p_surface->format->BitsPerPixel )
  162.     {
  163.         case 8:
  164.         {
  165.             int i, j;
  166.             uint8_t *p_src, *p_dst;
  167.             uint8_t r, g, b;
  168.             for ( i = 0; i < p_surface->h; i++ )
  169.             {
  170.                 p_src = (uint8_t*)p_surface->pixels + i * p_surface->pitch;
  171.                 p_dst = p_pic->p[0].p_pixels + i * p_pic->p[0].i_pitch;
  172.                 for ( j = 0; j < p_surface->w; j++ )
  173.                 {
  174.                     SDL_GetRGB( *(p_src++), p_surface->format,
  175.                                 &r, &g, &b );
  176.                     *(p_dst++) = r;
  177.                     *(p_dst++) = g;
  178.                     *(p_dst++) = b;
  179.                 }
  180.             }
  181.             break;
  182.         }
  183.         case 16:
  184.         {
  185.             int i;
  186.             uint8_t *p_src = p_surface->pixels;
  187.             uint8_t *p_dst = p_pic->p[0].p_pixels;
  188.             int i_pitch = p_pic->p[0].i_pitch < p_surface->pitch ?
  189.                 p_pic->p[0].i_pitch : p_surface->pitch;
  190.             for ( i = 0; i < p_surface->h; i++ )
  191.             {
  192.                 vlc_memcpy( p_dst, p_src, i_pitch );
  193.                 p_src += p_surface->pitch;
  194.                 p_dst += p_pic->p[0].i_pitch;
  195.             }
  196.             break;
  197.         }
  198.         case 24:
  199.         {
  200.             int i, j;
  201.             uint8_t *p_src, *p_dst;
  202.             uint8_t r, g, b;
  203.             for ( i = 0; i < p_surface->h; i++ )
  204.             {
  205.                 p_src = (uint8_t*)p_surface->pixels + i * p_surface->pitch;
  206.                 p_dst = p_pic->p[0].p_pixels + i * p_pic->p[0].i_pitch;
  207.                 for ( j = 0; j < p_surface->w; j++ )
  208.                 {
  209.                     SDL_GetRGB( *(uint32_t*)p_src, p_surface->format,
  210.                                 &r, &g, &b );
  211.                     *(p_dst++) = r;
  212.                     *(p_dst++) = g;
  213.                     *(p_dst++) = b;
  214.                     p_src += 3;
  215.                 }
  216.             }
  217.             break;
  218.         }
  219.         case 32:
  220.         {
  221.             int i, j;
  222.             uint8_t *p_src, *p_dst;
  223.             uint8_t r, g, b, a;
  224.             for ( i = 0; i < p_surface->h; i++ )
  225.             {
  226.                 p_src = (uint8_t*)p_surface->pixels + i * p_surface->pitch;
  227.                 p_dst = p_pic->p[0].p_pixels + i * p_pic->p[0].i_pitch;
  228.                 for ( j = 0; j < p_surface->w; j++ )
  229.                 {
  230.                     SDL_GetRGBA( *(uint32_t*)p_src, p_surface->format,
  231.                                 &r, &g, &b, &a );
  232.                     *(p_dst++) = b;
  233.                     *(p_dst++) = g;
  234.                     *(p_dst++) = r;
  235.                     *(p_dst++) = a;
  236.                     p_src += 4;
  237.                 }
  238.             }
  239.             break;
  240.         }
  241.     }
  242.     p_pic->date = p_block->i_pts > 0 ? p_block->i_pts : p_block->i_dts;
  243.     SDL_FreeSurface( p_surface );
  244.     block_Release( p_block ); *pp_block = NULL;
  245.     return p_pic;
  246. error:
  247.     if ( p_surface != NULL ) SDL_FreeSurface( p_surface );
  248.     block_Release( p_block ); *pp_block = NULL;
  249.     return NULL;
  250. }
  251. /*****************************************************************************
  252.  * CloseDecoder: sdl decoder destruction
  253.  *****************************************************************************/
  254. static void CloseDecoder( vlc_object_t *p_this )
  255. {
  256.     decoder_t *p_dec = (decoder_t *)p_this;
  257.     decoder_sys_t *p_sys = p_dec->p_sys;
  258.     free( p_sys );
  259. }