Hi,
I am a beginner in SDL programming. I am trying to
build a YUV 420 player in redhat Linux platform. I
found source code in Internet. But when I try to use
it on my system, it is giving following error on exit
from the routine.
“Fatal signal: Segmentation Fault (SDL Parachute
Deployed)”.
Please help me to avoid this error. It is very urgent.
The source is as follows.---------------------------------------------------------------------------------------------------
#ifdef HAVE_CONFIG_H
#include “config.h”
#endif
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <regex.h>
#include “SDL.h”
SDL_Surface *screen;
SDL_Event event;
SDL_Rect video_rect;
SDL_Overlay *my_overlay;
int width = 0;
int height = 0;
char *vfilename;
FILE *fpointer;
unsigned char *y_data, *cr_data, *cb_data;
int zoom = 1;
int frame = 0;
int quit = 0;
int grid = 0;
int load_frame(){
int cnt;
/* Fill in video data /
cnt = fread(y_data, 1, widthheight, fpointer);
if(cnt < width*height){
return 0;
} else {
cnt = fread(cb_data, 1, height * width / 4,
fpointer);
if(cnt < width * height / 4){
return 0;
} else {
cnt = fread(cr_data, 1, height * width / 4,
fpointer);
if(cnt < width * height / 4){
return 0;
}
}
}
return 1;
}
void draw_frame(){
int x, y;
/* Fill in pixel data - the pitches array contains
the length of a line in each plane*/
SDL_LockYUVOverlay(my_overlay);
memcpy(my_overlay->pixels[0], y_data, widthheight);
memcpy(my_overlay->pixels[1], cr_data,
widthheight/4);
memcpy(my_overlay->pixels[2], cb_data,
width*height/4);
if(grid){
// horizontal grid lines
for(y=0; y<height; y=y+16){
for(x=0; x<width; x+=8){
*(my_overlay->pixels[0] + y *
my_overlay->pitches[0] + x ) = 0xF0;
*(my_overlay->pixels[0] + y *
my_overlay->pitches[0] + x+4 ) = 0x20;
}
}
// vertical grid lines
for(x=0; x<width; x=x+16){
for(y=0; y<height; y+=8){
*(my_overlay->pixels[0] + y *
my_overlay->pitches[0] + x ) = 0xF0;
*(my_overlay->pixels[0] + (y+4) *
my_overlay->pitches[0] + x ) = 0x20;
}
}
}
SDL_UnlockYUVOverlay(my_overlay);
video_rect.x = 0;
video_rect.y = 0;
video_rect.w = widthzoom;
video_rect.h = heightzoom;
SDL_DisplayYUVOverlay(my_overlay, &video_rect);
}
void print_usage(){
fprintf(stdout, “Usage: yay [-s x]
filename.yuv\n”);
}
int main(int argc, char *argv[])
{
int opt;
char caption[32];
regex_t reg;
regmatch_t pm;
int result;
char picsize[32]="";
if(argc == 2){
// only one parameter, must be filename
vfilename = argv[1];
// try to find picture size from filename or path
if (regcomp(®, “_[0-9]+x[0-9]+”, REG_EXTENDED)
!= 0) return -1;
result = regexec(®, vfilename, 1, &pm,
REG_NOTBOL);
if(result == 0){
strncpy(picsize, (vfilename + pm.rm_so + 1),
(pm.rm_eo - pm.rm_so -1 ));
strcat(picsize, “\0”);
}
if (sscanf(picsize, “%dx%d”, &width, &height) !=
2) {
fprintf(stdout, “No geometry information found
in path/filename.\nPlease use -s x
paramter.\n”);
return 1;
}
} else if(argc == 4){
while((opt = getopt(argc, argv, “s:”)) != -1)
switch(opt){
case ‘s’:
if (sscanf(optarg, “%dx%d”, &width, &height) != 2) {
fprintf(stdout, “No geometry information found in
path/filename or provided by -s paramter.\n”);
return 1;
}
}
vfilename = argv[3];
} else {
print_usage();
return 1;
}
// SDL init
if(SDL_Init(SDL_INIT_VIDEO) < 0){
fprintf(stderr, “Unable to set video mode: %s\n”,
SDL_GetError());
exit(1);
}
atexit(SDL_Quit);
//Create a resizable SDL window
screen = SDL_SetVideoMode(widthzoom, heightzoom,
0,
SDL_SWSURFACE);
if(!screen){ //Couldn’t create window?
fprintf(stderr, “Couldn’t create screen\n”);
//Output to stderr and quit
exit(1);
}
SDL_EnableKeyRepeat(500, 10);
my_overlay = SDL_CreateYUVOverlay(width, height,
SDL_YV12_OVERLAY, screen);
if(!my_overlay){ //Couldn’t create overlay?
fprintf(stderr, “Couldn’t create overlay\n”);
//Output to stderr and quit
exit(1);
}
/* should allocate memory for y_data, cr_data,
cb_data here */
y_data = malloc(width * height * sizeof(unsigned
char ));
cb_data = malloc(width * height * sizeof(unsigned
char ));
cr_data = malloc(width * height * sizeof(unsigned
char ));
fpointer = fopen(vfilename, “r”);
if (fpointer == NULL){
fprintf(stderr, “Error opening %s\n”, vfilename);
return 1;
}
// send event to display first frame
event.type = SDL_KEYDOWN;
event.key.keysym.sym = SDLK_RIGHT;
SDL_PushEvent(&event);
// main loop
while (!quit){
sprintf(caption, "frame %d, zoom=%d", frame,
zoom);
SDL_WM_SetCaption( caption, NULL );
// wait for SDL event
SDL_WaitEvent(&event);
switch(event.type)
{
case SDL_KEYDOWN:
switch(event.key.keysym.sym)
{
case SDLK_SPACE:
case SDLK_RIGHT:
{
if(!feof(fpointer+1)){
// check for next frame existing
if(load_frame()){
draw_frame();
frame++;
}
}
break;
}
case SDLK_BACKSPACE:
case SDLK_LEFT:
{
if(frame>1){
frame--;
fseek(fpointer, (frame-1)*1.5*height*width ,
SEEK_SET);
//if(draw_frame())
load_frame();
draw_frame();
}
break;
}
case SDLK_UP:
{
zoom++;
screen = SDL_SetVideoMode(widthzoom,
heightzoom, 24,
SDL_HWSURFACE|SDL_HWACCEL);
video_rect.w = widthzoom;
video_rect.h = heightzoom;
SDL_DisplayYUVOverlay(my_overlay, &video_rect);
break;
}
case SDLK_DOWN:
{
if(zoom>1){
zoom–;
screen = SDL_SetVideoMode(widthzoom, heightzoom,
24,
SDL_HWSURFACE|SDL_HWACCEL);
video_rect.w = widthzoom;
video_rect.h = heightzoom;
SDL_DisplayYUVOverlay(my_overlay, &video_rect);
}
break;
}
case SDLK_g:
grid = ~grid;
draw_frame();
break;
case SDLK_q:
quit = 1;
break;
case SDLK_f:
SDL_WM_ToggleFullScreen(screen);
break;
default:
break;
} // switch key
break;
case SDL_QUIT:
quit = 1;
break;
case SDL_VIDEOEXPOSE:
SDL_DisplayYUVOverlay(my_overlay, &video_rect);
break;
default:
break;
} // switch event type
} // while
// clean up
SDL_FreeYUVOverlay(my_overlay);
free(y_data);
free(cb_data);
free(cr_data);
fclose(fpointer);
regfree(®);
return 0;
}
Thanks & regards,
Kishor.
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com