void sound_free( void ) { int i; for( i = 0 ; i < NUM_SOUNDS ; i++ ) { if( sounds[i] ) { Mix_FreeChunk( sounds[i] ); sounds[i] = NULL; } } sound_close_mixer(); }
int video_open( const char *filename ) { int i = -1; format_context = NULL; video_codec_context = NULL; video_stream = -1; video_clock = 0; audio_codec_context = NULL; audio_stream = -1; audio_buffer_size = 0; audio_buffer_index = 0; audio_packet_size = 0; audio_packet_data = NULL; if( !filename ) return -1; if( av_open_input_file( &format_context, filename, NULL, 0, NULL ) != 0 ) { fprintf( stderr, "Warning: Error opening video file '%s'\n", filename ); return -1; } if( av_find_stream_info( format_context ) < 0 ) { fprintf( stderr, "Warning: Error reading stream info from '%s'\n", filename ); return -1; } for( i = 0 ; i < format_context->nb_streams ; i++ ) { if( format_context->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO ) { video_stream = i; video_codec_context = format_context->streams[video_stream]->codec; } else if( format_context->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO ) { audio_stream = i; audio_codec_context = format_context->streams[audio_stream]->codec; } } if( !video_codec_context ) { fprintf( stderr, "Warning: Couldn't find video stream in '%s'\n", filename ); return -1; } video_codec_context->release_buffer = video_release_buffer; video_codec = avcodec_find_decoder( video_codec_context->codec_id ); if( !video_codec ) { fprintf( stderr, "Warning: Video codec in video '%s' not supported\n", filename ); return -1; } if( avcodec_open( video_codec_context, video_codec ) != 0 ) { fprintf( stderr, "Warning: Couldn't open video codec '%s' for '%s'\n", video_codec->name, filename ); return -1; } else { int bytes = avpicture_get_size( CONV_FORMAT, VIDEO_SIZE, VIDEO_SIZE ); video_buffer = (uint8_t*)av_malloc( bytes *sizeof(uint8_t) ); } if( !video_buffer ) { fprintf( stderr, "Warning: Couldn't allocate buffer for video '%s'\n", filename ); return -1; } avpicture_fill( (AVPicture*)conv_frame, video_buffer, CONV_FORMAT, VIDEO_SIZE, VIDEO_SIZE ); if( audio_codec_context ) { audio_codec = avcodec_find_decoder( audio_codec_context->codec_id ); if( !audio_codec ) { fprintf( stderr, "Warning: Audio codec in video '%s' not supported\n", filename ); audio_codec_context = NULL; } else { if( avcodec_open( audio_codec_context, audio_codec ) != 0 ) { fprintf( stderr, "Warning: Couldn't open audio codec '%s' for '%s'\n", audio_codec->name, filename ); audio_codec_context = NULL; } else { SDL_AudioSpec desired; desired.freq = audio_codec_context->sample_rate; desired.format = AUDIO_S16SYS; desired.channels = audio_codec_context->channels; desired.silence = 0; desired.samples = SAMPLES; desired.callback = video_audio_callback; desired.userdata = audio_codec_context; sound_close_mixer(); if( SDL_OpenAudio( &desired, &audio_spec ) < 0 ) { fprintf( stderr, "Warning: Couldn't open audio for video: %s\n", SDL_GetError() ); audio_codec_context = NULL; } else { packet_queue_flush( &audio_queue ); audio_open = 1; audio_running = 1; SDL_PauseAudio( 0 ); } } } } texture = ogl_create_empty_texture(); if( !texture ) return -1; texture->width = video_codec_context->width; texture->height = video_codec_context->height; got_texture = 0; stop = 0; reader_thread = SDL_CreateThread( video_reader_thread, NULL ); if( !reader_thread ) { fprintf( stderr, "Warning: Couldn't start video reader thread\n" ); return -1; } return 0; }