Codebase list smpeg / scrub-obsolete/main MPEG.cpp
scrub-obsolete/main

Tree @scrub-obsolete/main (Download .tar.gz)

MPEG.cpp @scrub-obsolete/mainraw · history · blame

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
#include "SDL.h"

#include "MPEG.h"

#ifdef WIN32
#include <io.h>
#else
#include <unistd.h>
#endif
#include <fcntl.h>
#include <string.h>
#include <errno.h>

#ifndef O_BINARY
#define O_BINARY 0
#endif

MPEG::MPEG(const char * name, bool SDLaudio) :
  MPEGerror()
{
  SDL_RWops *source;

  mpeg_mem = 0;

  source = SDL_RWFromFile(name, "rb");
  if (!source) {
    InitErrorState();
    SetError(SDL_GetError());
    return;
  }
  Init(source, SDLaudio);
}

MPEG::MPEG(int Mpeg_FD, bool SDLaudio) :
  MPEGerror()
{
  SDL_RWops *source;

  mpeg_mem = 0;

  // *** FIXME we're leaking a bit of memory for the FILE *
  // best solution would be to have SDL_RWFromFD
  FILE *file = fdopen(Mpeg_FD, "rb");
  if (!file) {
    InitErrorState();
    SetError(strerror(errno));
    return;
  }

  source = SDL_RWFromFP(file,false);
  if (!source) {
    InitErrorState();
    SetError(SDL_GetError());
    return;
  }
  Init(source, SDLaudio);
}

MPEG::MPEG(void *data, int size, bool SDLaudio) :
  MPEGerror()
{
  SDL_RWops *source;

  // The semantics are that the data passed in should be copied
  // (?)
  mpeg_mem = new char[size];
  memcpy(mpeg_mem, data, size);

  source = SDL_RWFromMem(mpeg_mem, size);
  if (!source) {
    InitErrorState();
    SetError(SDL_GetError());
    return;
  }
  Init(source, SDLaudio);
}

MPEG::MPEG(SDL_RWops *mpeg_source, bool SDLaudio) :
  MPEGerror()
{
  mpeg_mem = 0;
  Init(mpeg_source, SDLaudio);
}

void MPEG::Init(SDL_RWops *mpeg_source, bool SDLaudio)
{
    source = mpeg_source;
    sdlaudio = SDLaudio;

    /* Create the system that will parse the MPEG stream */
    system = new MPEGsystem(source);

    /* Initialize everything to invalid values for cleanup */
    error = NULL;

    audiostream = videostream = NULL;
    audioaction = NULL;
    videoaction = NULL;
    audio = NULL;
    video = NULL;
    audioaction_enabled = SDLaudio;
    videoaction_enabled = false;
    loop = false;
    pause = false;

    parse_stream_list();

    EnableAudio(audioaction_enabled);
    EnableVideo(videoaction_enabled);

    if ( ! audiostream && ! videostream ) {
      SetError("No audio/video stream found in MPEG");
    }

    if ( system && system->WasError() ) {
      SetError(system->TheError());
    }

    if ( audio && audio->WasError() ) {
      SetError(audio->TheError());
    }

    if ( video && video->WasError() ) {
      SetError(video->TheError());
    }

    if ( WasError() ) {
      SetError(TheError());
    }
}

void MPEG::InitErrorState() {
    audio = NULL;
    video = NULL;
    system = NULL;
    error = NULL;
    source = NULL;

    audiostream = videostream = NULL;
    audioaction = NULL;
    videoaction = NULL;
    audio = NULL;
    video = NULL;
    audioaction_enabled = videoaction_enabled = false;
    loop = false;
    pause = false;
}

MPEG::~MPEG()
{
  Stop();
  if(video) delete video;
  if(audio) delete audio;
  if(system) delete system;
  
  if(source) SDL_RWclose(source);
  if ( mpeg_mem )
    delete[] mpeg_mem;
}

bool MPEG::AudioEnabled(void) {
  return(audioaction_enabled);
}
void MPEG::EnableAudio(bool enabled) {
  if ( enabled && ! audioaction ) {
    enabled = false;
  }
  audioaction_enabled = enabled;

  /* Stop currently playing stream, if necessary */
  if ( audioaction && ! audioaction_enabled ) {
    audioaction->Stop();
  } 
  /* Set the video time source */
  if ( videoaction ) {
    if ( audioaction_enabled ) {
      videoaction->SetTimeSource(audioaction);
    } else {
      videoaction->SetTimeSource(NULL);
    }
  }
  if(audiostream)
    audiostream->enable(enabled);
}
bool MPEG::VideoEnabled(void) {
  return(videoaction_enabled);
}
void MPEG::EnableVideo(bool enabled) {
  if ( enabled && ! videoaction ) {
    enabled = false;
  }
  videoaction_enabled = enabled;

  /* Stop currently playing stream, if necessary */
  if ( videoaction && ! videoaction_enabled ) {
    videoaction->Stop();
  } 
  if(videostream)
    videostream->enable(enabled);
}

/* MPEG actions */
void MPEG::Loop(bool toggle) {
  loop = toggle;
}
void MPEG::Play(void) {
  if ( AudioEnabled() ) {
    audioaction->Play();
  }
  if ( VideoEnabled() ) {
    videoaction->Play();
  }
}
void MPEG::Stop(void) {
  if ( VideoEnabled() ) {
    videoaction->Stop();
  }
  if ( AudioEnabled() ) {
    audioaction->Stop();
  }
}

void MPEG::Rewind(void) {
  seekIntoStream(0);
}

void MPEG::Pause(void) {
  pause = !pause;

  if ( VideoEnabled() ) {
    videoaction->Pause();
  }
  if ( AudioEnabled() ) {
    audioaction->Pause();
  }
}

/* Michel Darricau from eProcess <mdarricau@eprocess.fr> conflict name with popcorn */
MPEGstatus MPEG::GetStatus(void) {
  MPEGstatus status;

  status = MPEG_STOPPED;
  if ( VideoEnabled() ) {
		/* Michel Darricau from eProcess <mdarricau@eprocess.fr> conflict name with popcorn */
    switch (videoaction->GetStatus()) {
      case MPEG_PLAYING:
        status = MPEG_PLAYING;
      break;
      default:
      break;
    }
  }
  if ( AudioEnabled() ) {
		/* Michel Darricau from eProcess <mdarricau@eprocess.fr> conflict name with popcorn */
    switch (audioaction->GetStatus()) {
      case MPEG_PLAYING:
        status = MPEG_PLAYING;
      break;
      default:
      break;
    }
  }

  if(status == MPEG_STOPPED && loop && !pause)
  {
    /* Here we go again */
    Rewind();
    Play();

    if ( VideoEnabled() ) {
		/* Michel Darricau from eProcess <mdarricau@eprocess.fr> conflict name with popcorn */
      switch (videoaction->GetStatus()) {
      case MPEG_PLAYING:
        status = MPEG_PLAYING;
	break;
        default:
        break;
      }
    }
    if ( AudioEnabled() ) {
		/* Michel Darricau from eProcess <mdarricau@eprocess.fr> conflict name with popcorn */
      switch (audioaction->GetStatus()) {
      case MPEG_PLAYING:
        status = MPEG_PLAYING;
	break;
        default:
        break;
      }
    }
  }

  return(status);
}


/* MPEG audio actions */
bool MPEG::GetAudioInfo(MPEG_AudioInfo *info) {
  if ( AudioEnabled() ) {
    return(audioaction->GetAudioInfo(info));
  }
  return(false);
}
void MPEG::Volume(int vol) {
  if ( AudioEnabled() ) {
    audioaction->Volume(vol);
  }
}
bool MPEG::WantedSpec(SDL_AudioSpec *wanted) {
  if( audiostream ) {
    return(GetAudio()->WantedSpec(wanted));
  }
  return(false);
}
void MPEG::ActualSpec(const SDL_AudioSpec *actual) {
  if( audiostream ) {
    GetAudio()->ActualSpec(actual);
  }
}
MPEGaudio *MPEG::GetAudio(void) { // Simple accessor used in the C interface
  return audio;
}

/* MPEG video actions */
bool MPEG::GetVideoInfo(MPEG_VideoInfo *info) {
  if ( VideoEnabled() ) {
    return(videoaction->GetVideoInfo(info));
  }
  return(false);
}
bool MPEG::SetDisplay(SDL_Surface *dst, SDL_mutex *lock,
		MPEG_DisplayCallback callback) {
  if ( VideoEnabled() ) {
    return(videoaction->SetDisplay(dst, lock, callback));
  }
  return(false);
}
void MPEG::MoveDisplay(int x, int y) {
  if ( VideoEnabled() ) {
    videoaction->MoveDisplay(x, y);
  }
}
void MPEG::ScaleDisplayXY(int w, int h) {
  if ( VideoEnabled() ) {
    videoaction->ScaleDisplayXY(w, h);
  }
}
void MPEG::SetDisplayRegion(int x, int y, int w, int h) {
  if ( VideoEnabled() ) {
    videoaction->SetDisplayRegion(x, y, w, h);
  }
}
void MPEG::RenderFrame(int frame)
{
    if ( VideoEnabled() ) {
        videoaction->RenderFrame(frame);
    }
}
void MPEG::RenderFinal(SDL_Surface *dst, int x, int y)
{
    Stop();
    if ( VideoEnabled() ) {
        videoaction->RenderFinal(dst, x, y);
    }
    Rewind();
}

SMPEG_Filter * MPEG::Filter(SMPEG_Filter * filter)
{
  if ( VideoEnabled() ) {
    return(videoaction->Filter(filter));
  }
  return 0;
}

void MPEG::Seek(int position)
{
  int was_playing = 0;

  /* Cannot seek past end of file */
  if((Uint32)position > system->TotalSize()) return;
  
	/* Michel Darricau from eProcess <mdarricau@eprocess.fr> conflict name with popcorn */
  /* get info whrether we need to restart playing at the end */
  if( GetStatus() == MPEG_PLAYING )
    was_playing = 1;

  if(!seekIntoStream(position)) return;

  /* If we were playing and not rewind then play again */
  if (was_playing)
    Play();

  if (VideoEnabled() && !was_playing) 
    videoaction->RenderFrame(0);

  if ( pause && VideoEnabled() ) {
    videoaction->Pause();
  }
  if ( pause && AudioEnabled() ) {
    audioaction->Pause();
  }
}

bool MPEG::seekIntoStream(int position)
{
  /* First we stop everything */
  Stop();

  /* Go to the desired position into file */
  if(!system->Seek(position)) return(false);

  /* Seek first aligned data */
  if(audiostream && audioaction_enabled)
    while(audiostream->time() == -1)
      if ( ! audiostream->next_packet() ) return false;
  if(videostream && videoaction_enabled)
    while(videostream->time() == -1)
      if ( ! videostream->next_packet() ) return false;

  /* Calculating current play time on audio only makes sense when there
     is no video */  
  if ( audioaction && !videoaction) {
    audioaction->Rewind();
    audioaction->ResetSynchro(system->TimeElapsedAudio(position));
  }
  /* And forget what we previouly buffered */
  else if ( audioaction ) {
    audioaction->Rewind();
    audioaction->ResetSynchro(audiostream->time());
  }
  if ( videoaction ) {
    videoaction->Rewind();
    videoaction->ResetSynchro(videostream->time());
  }

  return(true);
}

void MPEG::Skip(float seconds)
{
  if(system->get_stream(SYSTEM_STREAMID))
  {
    system->Skip(seconds);
  }
  else
  {
    /* No system information in MPEG */
    if( VideoEnabled() ) videoaction->Skip(seconds);
    if( AudioEnabled() ) audioaction->Skip(seconds);
  }
}

void MPEG::GetSystemInfo(MPEG_SystemInfo * sinfo)
{
  sinfo->total_size = system->TotalSize();
  sinfo->current_offset = system->Tell();
  sinfo->total_time = system->TotalTime();

  /* Get current time from audio or video decoder */
  /* TODO: move timing reference in MPEGsystem    */
  sinfo->current_time = 0;
  if( videoaction ) 
    sinfo->current_time = videoaction->Time();
  if( audioaction )
    sinfo->current_time = audioaction->Time();
}

void MPEG::parse_stream_list()
{
  MPEGstream ** stream_list;
  register int i;

  /* A new thread is created for each video and audio */
  /* stream                                           */ 
  /* TODO: support MPEG systems containing more than  */
  /*       one audio or video stream                  */
  i = 0;
  do
  {
    /* Retreive the list of streams */
    stream_list = system->GetStreamList();

    switch(stream_list[i]->streamid)
    {
      case SYSTEM_STREAMID:
      break;

      case AUDIO_STREAMID:
	audiostream = stream_list[i];
	audioaction_enabled = true;
	audiostream->next_packet();
	audio = new MPEGaudio(audiostream, sdlaudio);
	audioaction = audio;
      break;

      case VIDEO_STREAMID:
	videostream = stream_list[i];
	videoaction_enabled = true;
	videostream->next_packet();
	video = new MPEGvideo(videostream);
	videoaction = video;
      break;
    }

    i++;
  }
  while(stream_list[i]);
}