home *** CD-ROM | disk | FTP | other *** search
/ Chip 2005 June / ccd0605.iso / LINUX / gopchop-1.1.7.tar.tar / gopchop-1.1.7.tar / gopchop-1.1.7 / libvo / video_out_sdl.c < prev    next >
C/C++ Source or Header  |  2005-04-28  |  5KB  |  172 lines

  1. /*
  2.  * video_out_sdl.c
  3.  *
  4.  * Copyright (C) 2000-2003 Ryan C. Gordon <icculus@lokigames.com> and
  5.  *                         Dominik Schnitzer <aeneas@linuxvideo.org>
  6.  *
  7.  * SDL info, source, and binaries can be found at http://www.libsdl.org/
  8.  *
  9.  * This file is part of mpeg2dec, a free MPEG-2 video stream decoder.
  10.  * See http://libmpeg2.sourceforge.net/ for updates.
  11.  *
  12.  * mpeg2dec is free software; you can redistribute it and/or modify
  13.  * it under the terms of the GNU General Public License as published by
  14.  * the Free Software Foundation; either version 2 of the License, or
  15.  * (at your option) any later version.
  16.  *
  17.  * mpeg2dec is distributed in the hope that it will be useful,
  18.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20.  * GNU General Public License for more details.
  21.  *
  22.  * You should have received a copy of the GNU General Public License
  23.  * along with this program; if not, write to the Free Software
  24.  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  25.  */
  26.  
  27. #include "config.h"
  28.  
  29. #ifdef LIBVO_SDL
  30.  
  31. #include <stdio.h>
  32. #include <stdlib.h>
  33. #include <string.h>
  34. #include <SDL/SDL.h>
  35. #include <inttypes.h>
  36.  
  37. #include "video_out.h"
  38.  
  39. typedef struct {
  40.     vo_instance_t vo;
  41.     int width;
  42.     int height;
  43.     SDL_Surface * surface;
  44.     Uint32 sdlflags;
  45.     Uint8 bpp;
  46. } sdl_instance_t;
  47.  
  48. static void sdl_setup_fbuf (vo_instance_t * _instance,
  49.                 uint8_t ** buf, void ** id)
  50. {
  51.     sdl_instance_t * instance = (sdl_instance_t *) _instance;
  52.     SDL_Overlay * overlay;
  53.  
  54.     *id = overlay = SDL_CreateYUVOverlay (instance->width, instance->height,
  55.                       SDL_YV12_OVERLAY, instance->surface);
  56.     buf[0] = overlay->pixels[0];
  57.     buf[1] = overlay->pixels[2];
  58.     buf[2] = overlay->pixels[1];
  59.     if (((long)buf[0] & 15) || ((long)buf[1] & 15) || ((long)buf[2] & 15)) {
  60.     fprintf (stderr, "Unaligned buffers. Anyone know how to fix this ?\n");
  61.     exit (1);
  62.     }
  63. }
  64.  
  65. static void sdl_start_fbuf (vo_instance_t * instance,
  66.                 uint8_t * const * buf, void * id)
  67. {
  68.     SDL_LockYUVOverlay ((SDL_Overlay *) id);
  69. }
  70.  
  71. static void sdl_draw_frame (vo_instance_t * _instance,
  72.                 uint8_t * const * buf, void * id)
  73. {
  74.     sdl_instance_t * instance = (sdl_instance_t *) _instance;
  75.     SDL_Overlay * overlay = (SDL_Overlay *) id;
  76.     SDL_Event event;
  77.  
  78.     while (SDL_PollEvent (&event))
  79.     if (event.type == SDL_VIDEORESIZE)
  80.         instance->surface =
  81.         SDL_SetVideoMode (event.resize.w, event.resize.h,
  82.                   instance->bpp, instance->sdlflags);
  83.     SDL_DisplayYUVOverlay (overlay, &(instance->surface->clip_rect));
  84. }
  85.  
  86. static void sdl_discard (vo_instance_t * _instance,
  87.              uint8_t * const * buf, void * id)
  88. {
  89.     SDL_UnlockYUVOverlay ((SDL_Overlay *) id);
  90. }
  91.  
  92. #if 0
  93. static void sdl_close (vo_instance_t * _instance)
  94. {
  95.     sdl_instance_t * instance;
  96.     int i;
  97.  
  98.     instance = (sdl_instance_t *) _instance;
  99.     for (i = 0; i < 3; i++)
  100.     SDL_FreeYUVOverlay (instance->frame[i].overlay);
  101.     SDL_FreeSurface (instance->surface);
  102.     SDL_QuitSubSystem (SDL_INIT_VIDEO);
  103. }
  104. #endif
  105.  
  106. static int sdl_setup (vo_instance_t * _instance, unsigned int width,
  107.               unsigned int height,
  108.               unsigned int display_width, unsigned int display_height,
  109.               unsigned int chroma_width,
  110.               unsigned int chroma_height, vo_setup_result_t * result)
  111. {
  112.     sdl_instance_t * instance;
  113.  
  114.     instance = (sdl_instance_t *) _instance;
  115.  
  116.     instance->width = width;
  117.     instance->height = height;
  118.     instance->surface = SDL_SetVideoMode (width, height, instance->bpp,
  119.                       instance->sdlflags);
  120.     if (! (instance->surface)) {
  121.     fprintf (stderr, "sdl could not set the desired video mode\n");
  122.     return 1;
  123.     }
  124.  
  125.     result->convert = NULL;
  126.     return 0;
  127. }
  128.  
  129. vo_instance_t * vo_sdl_open (void)
  130. {
  131.     sdl_instance_t * instance;
  132.     const SDL_VideoInfo * vidInfo;
  133.  
  134.     instance = (sdl_instance_t *) calloc (1, sizeof (sdl_instance_t));
  135.     if (instance == NULL)
  136.     return NULL;
  137.  
  138.     instance->vo.setup = sdl_setup;
  139.     instance->vo.setup_fbuf = sdl_setup_fbuf;
  140.     instance->vo.start_fbuf = sdl_start_fbuf;
  141.     instance->vo.discard = sdl_discard;
  142.     instance->vo.draw = sdl_draw_frame;
  143.     instance->vo.close = NULL; /* sdl_close; */
  144.     instance->sdlflags = SDL_HWSURFACE | SDL_RESIZABLE;
  145.  
  146.     putenv("SDL_VIDEO_YUV_HWACCEL=1");
  147.     putenv("SDL_VIDEO_X11_NODIRECTCOLOR=1");
  148.  
  149.     if (SDL_Init (SDL_INIT_VIDEO)) {
  150.     fprintf (stderr, "sdl video initialization failed.\n");
  151.     return NULL;
  152.     }
  153.  
  154.     vidInfo = SDL_GetVideoInfo ();
  155.     if (!SDL_ListModes (vidInfo->vfmt, SDL_HWSURFACE | SDL_RESIZABLE)) {
  156.     instance->sdlflags = SDL_RESIZABLE;
  157.     if (!SDL_ListModes (vidInfo->vfmt, SDL_RESIZABLE)) {
  158.         fprintf (stderr, "sdl couldn't get any acceptable video mode\n");
  159.         return NULL;
  160.     }
  161.     }
  162.     instance->bpp = vidInfo->vfmt->BitsPerPixel;
  163.     if (instance->bpp < 16) {
  164.     fprintf(stderr, "sdl has to emulate a 16 bit surfaces, "
  165.         "that will slow things down.\n");
  166.     instance->bpp = 16;
  167.     }
  168.  
  169.     return (vo_instance_t *) instance;
  170. }
  171. #endif
  172.