summaryrefslogtreecommitdiffstats
path: root/mpeglib/lib/frame/frameQueue.cpp
blob: fe524976b50e1abc6bfe204d5743e127699f3409 (plain)
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
/*
  queues frames
  Copyright (C) 2001  Martin Vogt

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU Library General Public License as published by
  the Free Software Foundation.

  For more information look at the file COPYRIGHT in this package

 */


#include "frameQueue.h"

#include <iostream>

using namespace std;

FrameQueue::FrameQueue(int maxsize) {
  this->size=maxsize;
  entries = new Frame*[size];
  int i;
  for(i=0;i<size;i++) {
    entries[i]=NULL;
  }
  fillgrade=0;
  writepos=0;
  readpos=0;
}


FrameQueue::~FrameQueue() {
  int i;
  for(i=0;i<size;i++) {
    if (entries[i] != NULL) {
      delete entries[i];
    }
  }
  delete entries;
}

int FrameQueue::canRead() {
  return (fillgrade > 0);
}


int FrameQueue::canWrite() {
  return (fillgrade < size);
}


int FrameQueue::getFillgrade() {
  return fillgrade;
}


void FrameQueue::enqueue(Frame* frame) {
  if(canWrite() == false) {
    cout << "FrameQueue full cannot enqueue"<<endl;
    exit(0);
  }
  fillgrade++;
  entries[writepos]=frame;
  writepos++;
  if (writepos == size) {
    writepos=0;
  }
}


Frame* FrameQueue::dequeue() {
  if(canRead() == false) {
    cout << "FrameQueue empty cannot dequeue"<<endl;
    exit(0);
  }
  Frame* back=entries[readpos];
  // invalide this frame, we do not longer own it!
  entries[readpos]=NULL;
  fillgrade--;
  readpos++;
  if (readpos == size) {
    readpos=0;
  }
  return back;
}

Frame* FrameQueue::peekqueue(int pos) {
  if(fillgrade-pos <= 0) {
    cout << "FrameQueue : cannot peek this positon"<<endl;
    cout << "fillgrade:"<<fillgrade<<endl;
    cout << "pos:"<<pos<<endl;
    exit(0);
  }
  int getpos=(readpos+pos) % size;

  
  Frame* back=entries[getpos];
  return back; 
}