view test/testoverlay2.c @ 942:41a59de7f2ed

Here are patches for SDL12 and SDL_mixer for 4 or 6 channel surround sound on Linux using the Alsa driver. To use them, naturally you need a sound card that will do 4 or 6 channels and probably also a recent version of the Alsa drivers and library. Since the only SDL output driver that knows about surround sound is the Alsa driver, you���ll want to choose it, using: export SDL_AUDIODRIVER=alsa There are no syntactic changes to the programming API. No new library calls, no differences in arguments. There are two semantic changes: (1) For library calls with number of channels as an argument, formerly you could use only 1 or 2 for the number of channels. Now you can also use 4 or 6. (2) The two "left" and "right" arguments to Mix_SetPanning, for the case of 4 or 6 channels, no longer simply control the volumes of the left and right channels. Now the "left" argument is converted to an angle and Mix_SetPosition is called, and the "right" argu- ment is ignored. With two exceptions, so far as I know, the modified SDL12 and SDL_mixer work the same way as the original versions, when opened for 1 or 2 channel output. The two exceptions are bugs which I fixed. Well, the first, anyway, is a bug for sure. When rate conversions up or down by a factor of two are applied (in src/audio/SDL_audiocvt.c), streams with different numbers of channels (that is, mono and stereo) are treated the same way: either each sample is copied or every other sample is omitted. This is ok for mono, but for stereo, it is frames that should be copied or omitted, where by "frame" I mean a portion of the stream containing one sample for each channel. (In the SDL source, confusingly, sometimes frames are called "samples".) So for these rate conversions, stereo streams have to be treated differently, and they are, in my modified version. The other problem that might be characterized as a bug arises when SDL_mixer is passed a multichannel chunk which does not have an integral number of frames. Due to the way the effect_position code loops over frames, when the chunk ends with a partial frame, memory outside the chunk buffer will be accessed. In the case of stereo, it���s possible that because malloc may give more memory than requested, this potential problem never actually causes a segment fault. I don���t know. For 6 channel chunks, I do know, and it does cause segment faults. If SDL_mixer is passed defective chunks and this causes a segment fault, arguably, that���s not a bug in SDL_mixer. Still, whether or not it counts as a bug, it���s easy to protect against, so why not? I added code in mixer.c to discard any partial frame at the end of a chunk. Then what about when SDL or SDL_mixer is opened for 4 or 6 chan- nel output? What happens with the parts of the current library designed for stereo? I don���t know whether I���ve covered all the bases, but I���ve tried: (1) For playing 2 channel waves, or other cases where SDL knows it has to match up a 2 channel source with a 4 or 6 channel output, I���ve added code in SDL_audiocvt.c to make the necessary conversions. (2) For playing midis using timidity, I���ve converted timidity to do 4 or 6 channel output, upon request. (3) For playing mods using mikmod, I put ad hoc code in music.c to convert the stereo output that mikmod produces to 4 or 6 chan- nels. Obviously it would be better to change the mikmod code to mix down into 4 or 6 channels, but I have a hard time following the code in mikmod, so I didn���t do that. (4) For playing mp3s, I put ad hoc code in smpeg to copy channels in the case when 4 or 6 channel output is needed. (5) There seems to be no problem with .ogg files - stereo .oggs can be up converted as .wavs are. (6) The effect_position code in SDL_mixer is now generalized to in- clude the cases of 4 and 6 channel streams. I���ve done a very limited amount of compatibility testing for some of the games using SDL I happen to have. For details, see the file TESTS. I���ve put into a separate archive, Surround-SDL-testfiles.tgz, a couple of 6 channel wave files for testing and a 6 channel ogg file. If you have the right hardware and version of Alsa, you should be able to play the wave files with the Alsa utility aplay (and hear all channels, except maybe lfe, for chan-id.wav, since it���s rather faint). Don���t expect aplay to give good sound, though. There���s something wrong with the current version of aplay. The canyon.ogg file is to test loading of 6 channel oggs. After patching and compiling, you can play it with playmus. (My version of ogg123 will not play it, and I had to patch mplayer to get it to play 6 channel oggs.) Greg Lee <greg@ling.lll.hawaii.edu> Thus, July 1, 2004
author Sam Lantinga <slouken@libsdl.org>
date Sat, 21 Aug 2004 12:27:02 +0000
parents 05c551e5bc64
children 6b8f0543337c
line wrap: on
line source

/********************************************************************************
 *                                                                              *
 * Test of the overlay used for moved pictures, test more closed to real life.  *
 * Running trojan moose :) Coded by Mike Gorchak.                               *
 *                                                                              *
 ********************************************************************************/

#include "SDL.h"

#define MOOSEPIC_W 64
#define MOOSEPIC_H 88

#define MOOSEFRAME_SIZE (MOOSEPIC_W * MOOSEPIC_H)
#define MOOSEFRAMES_COUNT 10

SDL_Color MooseColors[84]={
    { 49,  49,  49}, { 66,  24,   0}, { 66,  33,   0}, { 66,  66,  66},
    { 66, 115,  49}, { 74,  33,   0}, { 74,  41,  16}, { 82,  33,   8},
    { 82,  41,   8}, { 82,  49,  16}, { 82,  82,  82}, { 90,  41,   8},
    { 90,  41,  16}, { 90,  57,  24}, { 99,  49,  16}, { 99,  66,  24},
    { 99,  66,  33}, { 99,  74,  33}, {107,  57,  24}, {107,  82,  41},
    {115,  57,  33}, {115,  66,  33}, {115,  66,  41}, {115,  74,   0},
    {115,  90,  49}, {115, 115, 115}, {123,  82,   0}, {123,  99,  57},
    {132,  66,  41}, {132,  74,  41}, {132,  90,   8}, {132,  99,  33},
    {132,  99,  66}, {132, 107,  66}, {140,  74,  49}, {140,  99,  16},
    {140, 107,  74}, {140, 115,  74}, {148, 107,  24}, {148, 115,  82},
    {148, 123,  74}, {148, 123,  90}, {156, 115,  33}, {156, 115,  90},
    {156, 123,  82}, {156, 132,  82}, {156, 132,  99}, {156, 156, 156},
    {165, 123,  49}, {165, 123,  90}, {165, 132,  82}, {165, 132,  90},
    {165, 132,  99}, {165, 140,  90}, {173, 132,  57}, {173, 132,  99},
    {173, 140, 107}, {173, 140, 115}, {173, 148,  99}, {173, 173, 173},
    {181, 140,  74}, {181, 148, 115}, {181, 148, 123}, {181, 156, 107},
    {189, 148, 123}, {189, 156,  82}, {189, 156, 123}, {189, 156, 132},
    {189, 189, 189}, {198, 156, 123}, {198, 165, 132}, {206, 165,  99},
    {206, 165, 132}, {206, 173, 140}, {206, 206, 206}, {214, 173, 115},
    {214, 173, 140}, {222, 181, 148}, {222, 189, 132}, {222, 189, 156},
    {222, 222, 222}, {231, 198, 165}, {231, 231, 231}, {239, 206, 173}
};

/* All RGB2YUV conversion code and some other parts of code has been taken from testoverlay.c */

/* NOTE: These RGB conversion functions are not intended for speed,
         only as examples.
*/

void RGBtoYUV(Uint8 *rgb, int *yuv, int monochrome, int luminance)
{
    int i;

    if (monochrome)
    {
#if 1 /* these are the two formulas that I found on the FourCC site... */
        yuv[0] = 0.299*rgb[0] + 0.587*rgb[1] + 0.114*rgb[2];
        yuv[1] = 128;
        yuv[2] = 128;
#else
        yuv[0] = (0.257 * rgb[0]) + (0.504 * rgb[1]) + (0.098 * rgb[2]) + 16;
        yuv[1] = 128;
        yuv[2] = 128;
#endif
    }
    else
    {
#if 1 /* these are the two formulas that I found on the FourCC site... */
        yuv[0] = 0.299*rgb[0] + 0.587*rgb[1] + 0.114*rgb[2];
        yuv[1] = (rgb[2]-yuv[0])*0.565 + 128;
        yuv[2] = (rgb[0]-yuv[0])*0.713 + 128;
#else
        yuv[0] = (0.257 * rgb[0]) + (0.504 * rgb[1]) + (0.098 * rgb[2]) + 16;
        yuv[1] = 128 - (0.148 * rgb[0]) - (0.291 * rgb[1]) + (0.439 * rgb[2]);
        yuv[2] = 128 + (0.439 * rgb[0]) - (0.368 * rgb[1]) - (0.071 * rgb[2]);
#endif
    }

    if (luminance!=100)
    {
        yuv[0]=yuv[0]*luminance/100;
        if (yuv[0]>255)
            yuv[0]=255;
    }
}

ConvertRGBtoYV12(SDL_Surface *s, SDL_Overlay *o, int monochrome, int luminance)
{
	int x,y;
	int yuv[3];
	Uint8 *p,*op[3];

	SDL_LockSurface(s);
	SDL_LockYUVOverlay(o);

	/* Convert */
	for(y=0; y<s->h && y<o->h; y++)
	{
		p=((Uint8 *) s->pixels)+s->pitch*y;
		op[0]=o->pixels[0]+o->pitches[0]*y;
		op[1]=o->pixels[1]+o->pitches[1]*(y/2);
		op[2]=o->pixels[2]+o->pitches[2]*(y/2);
		for(x=0; x<s->w && x<o->w; x++)
		{
			RGBtoYUV(p, yuv, monochrome, luminance);
			*(op[0]++)=yuv[0];
			if(x%2==0 && y%2==0)
			{
				*(op[1]++)=yuv[2];
				*(op[2]++)=yuv[1];
			}
			p+=s->format->BytesPerPixel;
		}
	}

	SDL_UnlockYUVOverlay(o);
	SDL_UnlockSurface(s);
}

ConvertRGBtoIYUV(SDL_Surface *s, SDL_Overlay *o, int monochrome, int luminance)
{
	int x,y;
	int yuv[3];
	Uint8 *p,*op[3];

	SDL_LockSurface(s);
	SDL_LockYUVOverlay(o);

	/* Convert */
	for(y=0; y<s->h && y<o->h; y++)
	{
		p=((Uint8 *) s->pixels)+s->pitch*y;
		op[0]=o->pixels[0]+o->pitches[0]*y;
		op[1]=o->pixels[1]+o->pitches[1]*(y/2);
		op[2]=o->pixels[2]+o->pitches[2]*(y/2);
		for(x=0; x<s->w && x<o->w; x++)
		{
			RGBtoYUV(p,yuv, monochrome, luminance);
			*(op[0]++)=yuv[0];
			if(x%2==0 && y%2==0)
			{
				*(op[1]++)=yuv[1];
				*(op[2]++)=yuv[2];
			}
			p+=s->format->BytesPerPixel;
		}
	}

	SDL_UnlockYUVOverlay(o);
	SDL_UnlockSurface(s);
}

ConvertRGBtoUYVY(SDL_Surface *s, SDL_Overlay *o, int monochrome, int luminance)
{
	int x,y;
	int yuv[3];
	Uint8 *p,*op;

	SDL_LockSurface(s);
	SDL_LockYUVOverlay(o);

	for(y=0; y<s->h && y<o->h; y++)
	{
		p=((Uint8 *) s->pixels)+s->pitch*y;
		op=o->pixels[0]+o->pitches[0]*y;
		for(x=0; x<s->w && x<o->w; x++)
		{
			RGBtoYUV(p, yuv, monochrome, luminance);
			if(x%2==0)
			{
				*(op++)=yuv[1];
				*(op++)=yuv[0];
				*(op++)=yuv[2];
			}
			else
				*(op++)=yuv[0];

			p+=s->format->BytesPerPixel;
		}
	}

	SDL_UnlockYUVOverlay(o);
	SDL_UnlockSurface(s);
}

ConvertRGBtoYVYU(SDL_Surface *s, SDL_Overlay *o, int monochrome, int luminance)
{
	int x,y;
	int yuv[3];
	Uint8 *p,*op;

	SDL_LockSurface(s);
	SDL_LockYUVOverlay(o);

	for(y=0; y<s->h && y<o->h; y++)
	{
		p=((Uint8 *) s->pixels)+s->pitch*y;
		op=o->pixels[0]+o->pitches[0]*y;
		for(x=0; x<s->w && x<o->w; x++)
		{
			RGBtoYUV(p,yuv, monochrome, luminance);
			if(x%2==0)
			{
				*(op++)=yuv[0];
				*(op++)=yuv[2];
				op[1]=yuv[1];
			}
			else
			{
				*op=yuv[0];
				op+=2;
			}

			p+=s->format->BytesPerPixel;
		}
	}

	SDL_UnlockYUVOverlay(o);
	SDL_UnlockSurface(s);
}

ConvertRGBtoYUY2(SDL_Surface *s, SDL_Overlay *o, int monochrome, int luminance)
{
	int x,y;
	int yuv[3];
	Uint8 *p,*op;

	SDL_LockSurface(s);
	SDL_LockYUVOverlay(o);
        
	for(y=0; y<s->h && y<o->h; y++)
	{
		p=((Uint8 *) s->pixels)+s->pitch*y;
		op=o->pixels[0]+o->pitches[0]*y;
		for(x=0; x<s->w && x<o->w; x++)
		{
			RGBtoYUV(p,yuv, monochrome, luminance);
			if(x%2==0)
			{
				*(op++)=yuv[0];
				*(op++)=yuv[1];
				op[1]=yuv[2];
			}
			else
			{
				*op=yuv[0];
				op+=2;
			}

			p+=s->format->BytesPerPixel;
		}
	}

	SDL_UnlockYUVOverlay(o);
	SDL_UnlockSurface(s);
}

static void PrintUsage(char *argv0)
{
    fprintf(stderr, "Usage: %s [arg] [arg] [arg] ...\n", argv0);
    fprintf(stderr, "\n");
    fprintf(stderr, "Where 'arg' is any of the following options:\n");
    fprintf(stderr, "\n");
    fprintf(stderr, "	-fps <frames per second>\n");
    fprintf(stderr, "	-format <fmt> (one of the: YV12, IYUV, YUY2, UYVY, YVYU)\n");
    fprintf(stderr, "	-scale <scale factor> (initial scale of the overlay)\n");
    fprintf(stderr, "	-help (shows this help)\n");
    fprintf(stderr, "\n");
    fprintf(stderr, "Press ESC to exit, or SPACE to freeze the movie while application running.\n");
    fprintf(stderr, "\n");
}

int main(int argc, char **argv)
{
    Uint8* RawMooseData;
    SDL_RWops* handle;
    SDL_Surface* screen;
    SDL_Surface* MooseFrame[MOOSEFRAMES_COUNT];
    SDL_Overlay* overlay;
    SDL_Rect overlayrect;
    SDL_Event event;
    Uint32 lastftick;
    int paused=0;
    int resized=0;
    int i;
    int fps=12;
    int fpsdelay;
    int overlay_format=SDL_YUY2_OVERLAY;
    int scale=5;

    while ( argc > 1 )
    {
        if (strcmp(argv[1], "-fps")== 0)
        {
            if (argv[2])
            {
                fps = atoi(argv[2]);
                if (fps==0)
                {
                    fprintf(stderr, "The -fps option requires an argument [from 1 to 1000], default is 12.\n");
                    return -1;
                }
                if ((fps<0) || (fps>1000))
                {
                    fprintf(stderr, "The -fps option must be in range from 1 to 1000, default is 12.\n");
                    return -1;
                }
                argv += 2;
                argc -= 2;
            }
            else
            {
                fprintf(stderr, "The -fps option requires an argument [from 1 to 1000], default is 12.\n");
                return -1;
            }
        } else
        if (strcmp(argv[1], "-format") == 0)
        {
            if (argv[2])
            {
                if (!strcmp(argv[2],"YV12"))
                    overlay_format = SDL_YV12_OVERLAY;
                else if(!strcmp(argv[2],"IYUV"))
                    overlay_format = SDL_IYUV_OVERLAY;
                else if(!strcmp(argv[2],"YUY2"))
                    overlay_format = SDL_YUY2_OVERLAY;
                else if(!strcmp(argv[2],"UYVY"))
                    overlay_format = SDL_UYVY_OVERLAY;
                else if(!strcmp(argv[2],"YVYU"))
                    overlay_format = SDL_YVYU_OVERLAY;
                else
                {
                    fprintf(stderr, "The -format option %s is not recognized, see help for info.\n", argv[2]);
                    return -1;
                }
                argv += 2;
                argc -= 2;
            }
            else
            {
                fprintf(stderr, "The -format option requires an argument, default is YUY2.\n");
                return -1;
            }
        } else
        if (strcmp(argv[1], "-scale") == 0)
        {
            if (argv[2])
            {
                scale = atoi(argv[2]);
                if (scale==0)
                {
                    fprintf(stderr, "The -scale option requires an argument [from 1 to 50], default is 5.\n");
                    return -1;
                }
                if ((scale<0) || (scale>50))
                {
                    fprintf(stderr, "The -scale option must be in range from 1 to 50, default is 5.\n");
                    return -1;
                }
                argv += 2;
                argc -= 2;
            }
            else
            {
                fprintf(stderr, "The -fps option requires an argument [from 1 to 1000], default is 12.\n");
                return -1;
            }
        } else
        if ((strcmp(argv[1], "-help") == 0 ) || (strcmp(argv[1], "-h") == 0))
        {
            PrintUsage(argv[0]);
            return 0;
        } else
        {
            fprintf(stderr, "Unrecognized option: %s.\n", argv[1]);
            return -1;
        }
        break;
    }
   
    RawMooseData=(Uint8*)malloc(MOOSEFRAME_SIZE * MOOSEFRAMES_COUNT);
    if (RawMooseData==NULL)
    {
        fprintf(stderr, "Can't allocate memory for movie !\n");
        free(RawMooseData);
        return 1;
    }

    /* load the trojan moose images */
    handle=SDL_RWFromFile("moose.dat", "rb");
    if (handle==NULL)
    {
        fprintf(stderr, "Can't find the file moose.dat !\n");
        free(RawMooseData);
        return 2;
    }
   
    SDL_RWread(handle, RawMooseData, MOOSEFRAME_SIZE, MOOSEFRAMES_COUNT);

    SDL_RWclose(handle);

    if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE) < 0)
    {
        fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
        free(RawMooseData);
        return 3;
    }
    atexit(SDL_Quit);

    /* Set video mode */
    if ( (screen=SDL_SetVideoMode(MOOSEPIC_W*scale, MOOSEPIC_H*scale, 0, SDL_RESIZABLE | SDL_SWSURFACE)) == NULL )
    {
        fprintf(stderr, "Couldn't set video mode: %s\n", 0, SDL_GetError());
        free(RawMooseData);
        return 4;
    }

    /* Set the window manager title bar */
    SDL_WM_SetCaption("SDL test overlay: running moose", "testoverlay2");

    for (i=0; i<MOOSEFRAMES_COUNT; i++)
    {
        MooseFrame[i]=SDL_CreateRGBSurfaceFrom(RawMooseData+i*MOOSEFRAME_SIZE, MOOSEPIC_W,
                                               MOOSEPIC_H, 8, MOOSEPIC_W, 0, 0, 0, 0);
        if (MooseFrame[i]==NULL)
        {
            fprintf(stderr, "Couldn't create SDL_Surfaces:%s\n", 0, SDL_GetError());
            free(RawMooseData);
            return 5;
        }
        SDL_SetColors(MooseFrame[i], MooseColors, 0, 84);

	{
		SDL_Surface *newsurf;
		SDL_PixelFormat format;

		format.palette=NULL;
		format.BitsPerPixel=32;
		format.BytesPerPixel=4;
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
		format.Rshift=0;
		format.Gshift=8;
		format.Bshift=16;
#else
		format.Rshift=24;
		format.Gshift=16;
		format.Bshift=8;
#endif
		format.Ashift=0;
		format.Rmask=0xff<<format.Rshift;
		format.Gmask=0xff<<format.Gshift;
		format.Bmask=0xff<<format.Bshift;
		format.Amask=0;
		format.Rloss=0;
		format.Gloss=0;
		format.Bloss=0;
		format.Aloss=8;
		format.colorkey=0;
		format.alpha=0;

		newsurf=SDL_ConvertSurface(MooseFrame[i], &format, SDL_SWSURFACE);
		if(!newsurf)
		{
                    fprintf(stderr, "Couldn't convert picture to 32bits RGB: %s\n", SDL_GetError());
                    return 6;
		}
		SDL_FreeSurface(MooseFrame[i]);
		MooseFrame[i]=newsurf;
	}
    }

    free(RawMooseData);

    overlay=SDL_CreateYUVOverlay(MOOSEPIC_W, MOOSEPIC_H, overlay_format, screen);
    if (!overlay)
    {
        fprintf(stderr, "Couldn't create overlay: %s\n", SDL_GetError());
        return 7;
    }

    printf("Created %dx%dx%d %s %s overlay\n",overlay->w,overlay->h,overlay->planes,
           overlay->hw_overlay?"hardware":"software",
           overlay->format==SDL_YV12_OVERLAY?"YV12":
           overlay->format==SDL_IYUV_OVERLAY?"IYUV":
           overlay->format==SDL_YUY2_OVERLAY?"YUY2":
           overlay->format==SDL_UYVY_OVERLAY?"UYVY":
           overlay->format==SDL_YVYU_OVERLAY?"YVYU":
           "Unknown");

    for(i=0; i<overlay->planes; i++)
    {
        printf("  plane %d: pitch=%d\n", i, overlay->pitches[i]);
    }

    overlayrect.x=0;
    overlayrect.y=0;
    overlayrect.w=MOOSEPIC_W*scale;
    overlayrect.h=MOOSEPIC_H*scale;

    /* set the start frame */
    i=0;
    fpsdelay=1000/fps;

    /* Ignore key up events, they don't even get filtered */
    SDL_EventState(SDL_KEYUP, SDL_IGNORE);

    lastftick=SDL_GetTicks();

    /* Loop, waiting for QUIT or RESIZE */
    while (1)
    {
        if (SDL_PollEvent(&event))
        {
            switch (event.type)
            {
                case SDL_VIDEORESIZE:
                     screen=SDL_SetVideoMode(event.resize.w, event.resize.h, 0, SDL_RESIZABLE | SDL_SWSURFACE);
                     overlayrect.w=event.resize.w;
                     overlayrect.h=event.resize.h;
                     if (paused)
                     {
                         resized=1;
                     }
                     break;
                case SDL_KEYDOWN:
                     if (event.key.keysym.sym == SDLK_SPACE)
                     {
                         paused=!paused;
                         break;
                     }
                     if (event.key.keysym.sym != SDLK_ESCAPE)
                     {
                         break;
                     }
                case SDL_QUIT:
                     SDL_FreeYUVOverlay(overlay);
                     for (i=0; i<MOOSEFRAMES_COUNT; i++)
                     {
                         SDL_FreeSurface(MooseFrame[i]);
                     }
                     return 0;
            }
        }

        if ((!paused)||(resized))
        {
            if (((SDL_GetTicks()-lastftick)>fpsdelay)||(resized))
            {
                lastftick=SDL_GetTicks();

                switch (overlay_format)
                {
                    case SDL_YUY2_OVERLAY:
                         ConvertRGBtoYUY2(MooseFrame[i], overlay, 0, 100);
                         break;
                    case SDL_YV12_OVERLAY:
                         ConvertRGBtoYV12(MooseFrame[i], overlay, 0, 100);
                         break;
                    case SDL_UYVY_OVERLAY:
                         ConvertRGBtoUYVY(MooseFrame[i], overlay, 0, 100);
                         break;
                    case SDL_YVYU_OVERLAY:
                         ConvertRGBtoYVYU(MooseFrame[i], overlay, 0, 100);
                         break;
                    case SDL_IYUV_OVERLAY:
                         ConvertRGBtoIYUV(MooseFrame[i], overlay, 0, 100);
                         break;
                }

                SDL_DisplayYUVOverlay(overlay, &overlayrect);
                if (!resized)
                {
                    i++;
                    if (i==10)
                    {
                        i=0;
                    }
                }
                else
                {
                    resized=0;
                }
            }
        }
        /* kind of timeslice to OS */
        SDL_Delay(1);
    }

    return 0;
}