|
| 1 | +/* -*- C++ -*- |
| 2 | + * |
| 3 | + * AVIWrapper.cpp - avifile library wrapper class to play AVI video & audio stream |
| 4 | + * |
| 5 | + * Copyright (c) 2001-2014 Ogapee. All rights reserved. |
| 6 | + * |
| 7 | + |
| 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 22 | + */ |
| 23 | + |
| 24 | +#include "AVIWrapper.h" |
| 25 | +#include <SDL_mixer.h> |
| 26 | +#include <audiodecoder.h> |
| 27 | +#include <avm_cpuinfo.h> |
| 28 | +#include <avm_output.h> |
| 29 | +#include <avifile.h> |
| 30 | +#include <videodecoder.h> |
| 31 | +#include <image.h> |
| 32 | +#include <stdlib.h> |
| 33 | +#include <string.h> |
| 34 | + |
| 35 | +#define DEFAULT_AUDIOBUF 4096 |
| 36 | +#define AVI_FINISH_EVENT 12345 |
| 37 | + |
| 38 | +#define AVIFILE_VERSION 747 |
| 39 | + |
| 40 | +AVIWrapper::AVIWrapper() |
| 41 | +{ |
| 42 | + screen_overlay = NULL; |
| 43 | + i_avi = NULL; |
| 44 | + v_stream = NULL; |
| 45 | + a_stream = NULL; |
| 46 | + remaining_buffer = new char[DEFAULT_AUDIOBUF*4]; |
| 47 | + remaining_count = 0; |
| 48 | +} |
| 49 | + |
| 50 | +AVIWrapper::~AVIWrapper() |
| 51 | +{ |
| 52 | + if ( v_stream ) |
| 53 | + v_stream->StopStreaming(); |
| 54 | + if ( a_stream ) |
| 55 | + a_stream->StopStreaming(); |
| 56 | + if ( i_avi ) delete i_avi; |
| 57 | + if ( screen_overlay ) SDL_FreeYUVOverlay( screen_overlay ); |
| 58 | + if ( remaining_buffer ) delete[] remaining_buffer; |
| 59 | +} |
| 60 | + |
| 61 | +int AVIWrapper::init( char *filename, bool debug_flag ) |
| 62 | +{ |
| 63 | + this->debug_flag = debug_flag; |
| 64 | +#if AVIFILE_VERSION >= 747 |
| 65 | + if ( !debug_flag ) avm::AvmOutput::singleton()->resetDebugLevels(-1); |
| 66 | +#else |
| 67 | + if ( !debug_flag ) avm::out.resetDebugLevels(-1); |
| 68 | +#endif |
| 69 | + |
| 70 | + i_avi = CreateIAviReadFile( filename ); |
| 71 | + if ( i_avi == NULL || i_avi->IsValid() == false ){ |
| 72 | + fprintf( stderr, "can't CreateIAviReadFile from %s\n", filename ); |
| 73 | + return -1; |
| 74 | + } |
| 75 | + |
| 76 | + v_stream = i_avi->GetStream(0, AviStream::Video ); |
| 77 | + if ( v_stream == NULL ){ |
| 78 | + fprintf( stderr, "Video Stream is NULL\n" ); |
| 79 | + return -1; |
| 80 | + } |
| 81 | + |
| 82 | + width = v_stream->GetStreamInfo()->GetVideoWidth(); |
| 83 | + height = v_stream->GetStreamInfo()->GetVideoHeight(); |
| 84 | + if ( debug_flag ) fprintf( stderr, "width %d height %d\n", width, height ); |
| 85 | + |
| 86 | + return 0; |
| 87 | +} |
| 88 | + |
| 89 | +int AVIWrapper::initAV( SDL_Surface *surface, bool audio_open_flag ) |
| 90 | +{ |
| 91 | + screen_rect.x = screen_rect.y = 0; |
| 92 | + screen_rect.w = surface->w; |
| 93 | + screen_rect.h = surface->h; |
| 94 | + |
| 95 | + v_stream->StartStreaming(); |
| 96 | + if ( v_stream->GetVideoDecoder() == NULL ){ |
| 97 | + if ( debug_flag ) fprintf( stderr, "GetVideoDecoder() return 0.\n" ); |
| 98 | + return -1; |
| 99 | + } |
| 100 | + avm::IVideoDecoder::CAPS cap = v_stream->GetVideoDecoder()->GetCapabilities(); |
| 101 | + if ( debug_flag ) printf("cap %x\n", cap ); |
| 102 | + |
| 103 | + if ( cap & avm::IVideoDecoder::CAP_YV12 ){ |
| 104 | + v_stream->GetVideoDecoder()->SetDestFmt( 0, fccYV12 ); |
| 105 | + screen_overlay = SDL_CreateYUVOverlay( width, height, SDL_YV12_OVERLAY, surface ); |
| 106 | + } |
| 107 | + else if ( cap & avm::IVideoDecoder::CAP_YUY2 ){ |
| 108 | + v_stream->GetVideoDecoder()->SetDestFmt( 0, fccYUY2 ); |
| 109 | + screen_overlay = SDL_CreateYUVOverlay( width, height, SDL_YUY2_OVERLAY, surface ); |
| 110 | + } |
| 111 | + else{ |
| 112 | + screen_overlay = SDL_CreateYUVOverlay( width, height, SDL_YV12_OVERLAY, surface ); |
| 113 | + } |
| 114 | + |
| 115 | + if ( !audio_open_flag ) return 0; |
| 116 | + a_stream = i_avi->GetStream(0, AviStream::Audio); |
| 117 | + if ( a_stream == NULL ){ |
| 118 | + if ( debug_flag ) fprintf( stderr, "Audio Stream is NULL\n" ); |
| 119 | + return 0; |
| 120 | + } |
| 121 | + |
| 122 | + a_stream->StartStreaming(); |
| 123 | + WAVEFORMATEX wave_fmt; |
| 124 | + a_stream->GetAudioDecoder()->GetOutputFormat( &wave_fmt ); |
| 125 | + if ( debug_flag ) printf(" format %d ch %d sample %d bit %d avg Bps %d\n", wave_fmt.wFormatTag, wave_fmt.nChannels, wave_fmt.nSamplesPerSec, wave_fmt.wBitsPerSample, wave_fmt.nAvgBytesPerSec ); |
| 126 | + |
| 127 | + if ( Mix_OpenAudio( wave_fmt.nSamplesPerSec, MIX_DEFAULT_FORMAT, wave_fmt.nChannels, DEFAULT_AUDIOBUF ) < 0 ){ |
| 128 | + fprintf( stderr, "can't open audio device\n" ); |
| 129 | + a_stream->StopStreaming(); |
| 130 | + delete a_stream; |
| 131 | + a_stream = NULL; |
| 132 | + } |
| 133 | + |
| 134 | + return 0; |
| 135 | +} |
| 136 | + |
| 137 | +static void audioCallback( void *userdata, Uint8 *stream, int len ) |
| 138 | +{ |
| 139 | + AVIWrapper &avi = *(AVIWrapper*)userdata; |
| 140 | + avi.audioCallback( userdata, stream, len ); |
| 141 | +} |
| 142 | + |
| 143 | +void AVIWrapper::audioCallback( void *userdata, Uint8 *stream, int len ) |
| 144 | +{ |
| 145 | + if ( len == 0 ) |
| 146 | + status = AVI_STOP; |
| 147 | + |
| 148 | + size_t ocnt; |
| 149 | + size_t samples; |
| 150 | + size_t count = 0; |
| 151 | + |
| 152 | + if ( remaining_count > 0 ){ |
| 153 | + if ( remaining_count <= len ){ |
| 154 | + memcpy( stream, remaining_buffer, remaining_count ); |
| 155 | + count = remaining_count; |
| 156 | + len -= remaining_count; |
| 157 | + remaining_count = 0; |
| 158 | + } |
| 159 | + else{ |
| 160 | + memmove( stream, remaining_buffer, len ); |
| 161 | + count = len; |
| 162 | + len = 0; |
| 163 | + remaining_count -= len; |
| 164 | + return; |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + while ( len > 0 && !a_stream->Eof() ){ |
| 169 | + a_stream->ReadFrames( remaining_buffer, (size_t)len, (size_t)len, samples, ocnt ); |
| 170 | + if ( (int)ocnt <= len ){ |
| 171 | + memcpy( stream+count, remaining_buffer, ocnt ); |
| 172 | + len -= ocnt; |
| 173 | + } |
| 174 | + else{ |
| 175 | + memcpy( stream+count, remaining_buffer, len ); |
| 176 | + if ( (int)ocnt-len < DEFAULT_AUDIOBUF*4 - len ){ |
| 177 | + memmove( remaining_buffer, remaining_buffer+len, ocnt-len ); |
| 178 | + remaining_count = ocnt-len; |
| 179 | + } |
| 180 | + else{ |
| 181 | + remaining_count = 0; |
| 182 | + } |
| 183 | + len = 0; |
| 184 | + } |
| 185 | + count += ocnt; |
| 186 | + } |
| 187 | +} |
| 188 | + |
| 189 | +double AVIWrapper::getAudioTime() |
| 190 | +{ |
| 191 | + if ( time_start == 0 ) |
| 192 | + { |
| 193 | + frame_start = (v_stream) ? v_stream->GetTime() : 0.; |
| 194 | +#if AVIFILE_VERSION >= 747 |
| 195 | + time_start = avm_get_time_us(); |
| 196 | +#else |
| 197 | + time_start = longcount(); |
| 198 | +#endif |
| 199 | + } |
| 200 | + |
| 201 | + if ( a_stream ) |
| 202 | + return a_stream->GetTime(); |
| 203 | + else |
| 204 | +#if AVIFILE_VERSION >= 747 |
| 205 | + return frame_start + to_float(avm_get_time_us(), time_start); |
| 206 | +#else |
| 207 | + return frame_start + to_float(longcount(), time_start); |
| 208 | +#endif |
| 209 | +} |
| 210 | + |
| 211 | +static int playVideo( void *userdata ) |
| 212 | +{ |
| 213 | + AVIWrapper &avi = *(AVIWrapper*)userdata; |
| 214 | + return avi.playVideo( userdata ); |
| 215 | +} |
| 216 | + |
| 217 | +#define NUM_CACHES 3 |
| 218 | +int AVIWrapper::playVideo( void *userdata ) |
| 219 | +{ |
| 220 | + int i; |
| 221 | + |
| 222 | + struct{ |
| 223 | + bool valid; |
| 224 | + avm::CImage *image; |
| 225 | + double time; |
| 226 | + } cache[NUM_CACHES]; |
| 227 | + for ( i=0 ; i<NUM_CACHES ; i++ ){ |
| 228 | + cache[i].valid = false; |
| 229 | + } |
| 230 | + int remaining_cache = NUM_CACHES; |
| 231 | + |
| 232 | + while ( status == AVI_PLAYING && !v_stream->Eof() ){ |
| 233 | + avm::CImage *image = v_stream->GetFrame( true ); |
| 234 | + if ( image == NULL ) break; |
| 235 | + |
| 236 | + double current_time = v_stream->GetTime(); |
| 237 | + double minimum_time = current_time; |
| 238 | + |
| 239 | + // look for the nearest in the cache |
| 240 | + int nearest_cache=-1; |
| 241 | + for ( i=0 ; i<NUM_CACHES ; i++ ){ |
| 242 | + if ( cache[i].valid ){ |
| 243 | + if ( nearest_cache == -1 || |
| 244 | + (nearest_cache >= 0 && |
| 245 | + cache[i].time < cache[nearest_cache].time) ){ |
| 246 | + nearest_cache = i; |
| 247 | + if ( minimum_time > cache[nearest_cache].time ) |
| 248 | + minimum_time = cache[nearest_cache].time; |
| 249 | + } |
| 250 | + } |
| 251 | + } |
| 252 | + |
| 253 | + double async = getAudioTime() - minimum_time; |
| 254 | + //printf("audio %f (%f - %f) minimum %d %f cur %f\n", async, getAudioTime(), minimum_time, nearest_cache, minimum_time, current_time ); |
| 255 | + if ( async < -0.01 ){ |
| 256 | + if ( remaining_cache == 0 ){ |
| 257 | + //printf("sync0 %f %f %f %f\n", async, (a_stream)?a_stream->GetTime():0.0, v_stream->GetTime(), minimum_time ); |
| 258 | + SDL_Delay( (int)(-async*1000) ); |
| 259 | + } |
| 260 | + } |
| 261 | + if ( (async < -0.01 && remaining_cache > 0) || |
| 262 | + nearest_cache >= 0 ){ |
| 263 | + // add cache |
| 264 | + for ( i=0 ; i<NUM_CACHES ; i++ ){ |
| 265 | + if ( cache[i].valid == false ){ |
| 266 | + cache[i].valid = true; |
| 267 | + cache[i].image = new avm::CImage(image); |
| 268 | + cache[i].time = current_time; |
| 269 | + remaining_cache--; |
| 270 | + break; |
| 271 | + } |
| 272 | + } |
| 273 | + if ( async < -0.01 ){ |
| 274 | + image->Release(); |
| 275 | + continue; |
| 276 | + } |
| 277 | + } |
| 278 | + |
| 279 | + if ( nearest_cache >= 0 && minimum_time == cache[nearest_cache].time ){ |
| 280 | + //printf("draw cache %d %f\n", nearest_cache, cache[nearest_cache].time ); |
| 281 | + //if ( async <= 0.033 ) // drop frame if necessary |
| 282 | + drawFrame( cache[nearest_cache].image ); |
| 283 | + cache[nearest_cache].image->Release(); |
| 284 | + cache[nearest_cache].valid = false; |
| 285 | + remaining_cache++; |
| 286 | + } |
| 287 | + else{ |
| 288 | + //printf("draw real %f\n", current_time ); |
| 289 | + //if ( async <= 0.033 ) // drop frame if necessary |
| 290 | + drawFrame( image ); |
| 291 | + } |
| 292 | + image->Release(); |
| 293 | + } |
| 294 | + status = AVI_STOP; |
| 295 | + |
| 296 | + for ( i=0 ; i<NUM_CACHES ; i++ ) |
| 297 | + if ( cache[i].valid ) cache[i].image->Release(); |
| 298 | + |
| 299 | + return 0; |
| 300 | +} |
| 301 | + |
| 302 | +int AVIWrapper::play( bool click_flag ) |
| 303 | +{ |
| 304 | + int ret = 0; |
| 305 | + time_start = 0; |
| 306 | + status = AVI_PLAYING; |
| 307 | + if ( v_stream ) |
| 308 | + thread_id = SDL_CreateThread( ::playVideo, this ); |
| 309 | + if ( a_stream ) |
| 310 | + Mix_HookMusic( ::audioCallback, this ); |
| 311 | + |
| 312 | + bool done_flag = false; |
| 313 | + while( !(done_flag & click_flag) && status == AVI_PLAYING ){ |
| 314 | + SDL_Event event; |
| 315 | + |
| 316 | + while( SDL_PollEvent( &event ) ){ |
| 317 | + switch (event.type){ |
| 318 | + case SDL_KEYUP: |
| 319 | + if ( ((SDL_KeyboardEvent *)&event)->keysym.sym == SDLK_RETURN || |
| 320 | + ((SDL_KeyboardEvent *)&event)->keysym.sym == SDLK_KP_ENTER || |
| 321 | + ((SDL_KeyboardEvent *)&event)->keysym.sym == SDLK_SPACE || |
| 322 | + ((SDL_KeyboardEvent *)&event)->keysym.sym == SDLK_ESCAPE ) |
| 323 | + done_flag = true; |
| 324 | + break; |
| 325 | + case SDL_QUIT: |
| 326 | + ret = 1; |
| 327 | + case SDL_MOUSEBUTTONUP: |
| 328 | + done_flag = true; |
| 329 | + break; |
| 330 | + default: |
| 331 | + break; |
| 332 | + } |
| 333 | + } |
| 334 | + SDL_Delay( 10 ); |
| 335 | + } |
| 336 | + |
| 337 | + status = AVI_STOP; |
| 338 | + if ( v_stream ) |
| 339 | + SDL_WaitThread( thread_id, NULL ); |
| 340 | + if ( a_stream ) |
| 341 | + Mix_HookMusic( NULL, NULL ); |
| 342 | + |
| 343 | + return ret; |
| 344 | +} |
| 345 | + |
| 346 | +int AVIWrapper::drawFrame( avm::CImage *image ) |
| 347 | +{ |
| 348 | + if ( image == NULL ) return -1; |
| 349 | + |
| 350 | + unsigned int i, j; |
| 351 | + uint32_t comp = image->GetFmt()->biCompression; |
| 352 | + |
| 353 | + unsigned char *buf = image->Data(); |
| 354 | + SDL_LockYUVOverlay( screen_overlay ); |
| 355 | + Uint8 *dst_y = screen_overlay->pixels[0]; |
| 356 | + Uint8 *dst_u = screen_overlay->pixels[1]; |
| 357 | + Uint8 *dst_v = screen_overlay->pixels[2]; |
| 358 | + |
| 359 | + if ( comp == 0 ){ // BGR |
| 360 | + image->ToYUV(); |
| 361 | + for ( i=0 ; i<width*height ; i++ ) |
| 362 | + *dst_y++ = buf[i*3]; |
| 363 | + |
| 364 | + for ( i=0 ; i<height/2 ; i++ ){ |
| 365 | + for ( j=0 ; j<width/2 ; j++ ){ |
| 366 | + *dst_v++ = buf[(i*width*2+j*2)*3+1]; |
| 367 | + *dst_u++ = buf[(i*width*2+j*2)*3+2]; |
| 368 | + } |
| 369 | + } |
| 370 | + } |
| 371 | + else if ( comp == IMG_FMT_YUY2 ){ |
| 372 | + memcpy( dst_y, buf, width*height*2 ); |
| 373 | + } |
| 374 | + else if ( comp == IMG_FMT_YV12 ){ |
| 375 | + memcpy( dst_y, buf, width*height+width*height/2 ); |
| 376 | + } |
| 377 | + SDL_UnlockYUVOverlay( screen_overlay ); |
| 378 | + SDL_DisplayYUVOverlay( screen_overlay, &screen_rect ); |
| 379 | + |
| 380 | + return 0; |
| 381 | +} |
0 commit comments