File: MPEGVideoStreamFramer.cpp

package info (click to toggle)
liblivemedia 2006.03.17-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 2,928 kB
  • ctags: 4,588
  • sloc: cpp: 35,064; ansic: 979; makefile: 78; sh: 73
file content (180 lines) | stat: -rw-r--r-- 6,377 bytes parent folder | download
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
/**********
This library is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the
Free Software Foundation; either version 2.1 of the License, or (at your
option) any later version. (See <http://www.gnu.org/copyleft/lesser.html>.)

This library is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for
more details.

You should have received a copy of the GNU Lesser General Public License
along with this library; if not, write to the Free Software Foundation, Inc.,
59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
**********/
// "liveMedia"
// Copyright (c) 1996-2005 Live Networks, Inc.  All rights reserved.
// A filter that breaks up an MPEG video elementary stream into
//   headers and frames
// Implementation

#include "MPEGVideoStreamParser.hh"
#include <GroupsockHelper.hh>

////////// TimeCode implementation //////////

TimeCode::TimeCode()
  : days(0), hours(0), minutes(0), seconds(0), pictures(0) {
}

TimeCode::~TimeCode() {
}

int TimeCode::operator==(TimeCode const& arg2) {
  return pictures == arg2.pictures && seconds == arg2.seconds
    && minutes == arg2.minutes && hours == arg2.hours && days == arg2.days;
}

////////// MPEGVideoStreamFramer implementation //////////

MPEGVideoStreamFramer::MPEGVideoStreamFramer(UsageEnvironment& env,
					     FramedSource* inputSource)
  : FramedFilter(env, inputSource),
    fFrameRate(0.0) /* until we learn otherwise */,
    fParser(NULL) {
  reset();
}

MPEGVideoStreamFramer::~MPEGVideoStreamFramer() {
  delete fParser;
}

void MPEGVideoStreamFramer::flushInput() {
  reset();
  if (fParser != NULL) fParser->flushInput();
}

void MPEGVideoStreamFramer::reset() {
  fPictureCount = 0;
  fPictureEndMarker = False;
  fPicturesAdjustment = 0;
  fPictureTimeBase = 0.0;
  fTcSecsBase = 0;
  fHaveSeenFirstTimeCode = False;

  // Use the current wallclock time as the base 'presentation time':
  gettimeofday(&fPresentationTimeBase, NULL);
}

#ifdef DEBUG
static struct timeval firstPT;
#endif
void MPEGVideoStreamFramer
::computePresentationTime(unsigned numAdditionalPictures) {
  // Computes "fPresentationTime" from the most recent GOP's
  // time_code, along with the "numAdditionalPictures" parameter:
  TimeCode& tc = fCurGOPTimeCode;

  unsigned tcSecs
    = (((tc.days*24)+tc.hours)*60+tc.minutes)*60+tc.seconds - fTcSecsBase;
  double pictureTime = fFrameRate == 0.0 ? 0.0
    : (tc.pictures + fPicturesAdjustment + numAdditionalPictures)/fFrameRate;
  while (pictureTime < fPictureTimeBase) { // "if" should be enough, but just in case
    if (tcSecs > 0) tcSecs -= 1;
    pictureTime += 1.0;
  }
  pictureTime -= fPictureTimeBase;
  if (pictureTime < 0.0) pictureTime = 0.0; // sanity check
  unsigned pictureSeconds = (unsigned)pictureTime;
  double pictureFractionOfSecond = pictureTime - (double)pictureSeconds;

  fPresentationTime = fPresentationTimeBase;
  fPresentationTime.tv_sec += tcSecs + pictureSeconds;
  fPresentationTime.tv_usec += (long)(pictureFractionOfSecond*1000000.0);
  if (fPresentationTime.tv_usec >= 1000000) {
    fPresentationTime.tv_usec -= 1000000;
    ++fPresentationTime.tv_sec;
  }
#ifdef DEBUG
  if (firstPT.tv_sec == 0 && firstPT.tv_usec == 0) firstPT = fPresentationTime;
  struct timeval diffPT;
  diffPT.tv_sec = fPresentationTime.tv_sec - firstPT.tv_sec; 
  diffPT.tv_usec = fPresentationTime.tv_usec - firstPT.tv_usec; 
  if (fPresentationTime.tv_usec < firstPT.tv_usec) {
    --diffPT.tv_sec;
    diffPT.tv_usec += 1000000;
  }
  fprintf(stderr, "MPEGVideoStreamFramer::computePresentationTime(%d) -> %lu.%06ld [%lu.%06ld]\n", numAdditionalPictures, fPresentationTime.tv_sec, fPresentationTime.tv_usec, diffPT.tv_sec, diffPT.tv_usec);
#endif
}

void MPEGVideoStreamFramer
::setTimeCode(unsigned hours, unsigned minutes, unsigned seconds,
	      unsigned pictures, unsigned picturesSinceLastGOP) {
  TimeCode& tc = fCurGOPTimeCode; // abbrev
  unsigned days = tc.days;
  if (hours < tc.hours) {
    // Assume that the 'day' has wrapped around:
    ++days;
  }
  tc.days = days;
  tc.hours = hours;
  tc.minutes = minutes;
  tc.seconds = seconds;
  tc.pictures = pictures;
  if (!fHaveSeenFirstTimeCode) {
    fPictureTimeBase = fFrameRate == 0.0 ? 0.0 : tc.pictures/fFrameRate;
    fTcSecsBase = (((tc.days*24)+tc.hours)*60+tc.minutes)*60+tc.seconds;
    fHaveSeenFirstTimeCode = True;
  } else if (fCurGOPTimeCode == fPrevGOPTimeCode) {
    // The time code has not changed since last time.  Adjust for this:
    fPicturesAdjustment += picturesSinceLastGOP;
  } else {
    // Normal case: The time code changed since last time.
    fPrevGOPTimeCode = tc;
    fPicturesAdjustment = 0;
  }
}

void MPEGVideoStreamFramer::doGetNextFrame() {
  fParser->registerReadInterest(fTo, fMaxSize);
  continueReadProcessing();
}

void MPEGVideoStreamFramer
::continueReadProcessing(void* clientData,
			 unsigned char* /*ptr*/, unsigned /*size*/,
			 struct timeval /*presentationTime*/) {
  MPEGVideoStreamFramer* framer = (MPEGVideoStreamFramer*)clientData;
  framer->continueReadProcessing();
}

void MPEGVideoStreamFramer::continueReadProcessing() {
  unsigned acquiredFrameSize = fParser->parse();
  if (acquiredFrameSize > 0) {
    // We were able to acquire a frame from the input.
    // It has already been copied to the reader's space.
    fFrameSize = acquiredFrameSize;
    fNumTruncatedBytes = fParser->numTruncatedBytes();
    
    // "fPresentationTime" should have already been computed.

    // Compute "fDurationInMicroseconds" now:
    fDurationInMicroseconds
      = (fFrameRate == 0.0 || ((int)fPictureCount) < 0) ? 0
      : (unsigned)((fPictureCount*1000000)/fFrameRate);
#ifdef DEBUG
    fprintf(stderr, "fDurationInMicroseconds: %d ((%d*1000000)/%f)\n", fDurationInMicroseconds, fPictureCount, fFrameRate);
#endif
    fPictureCount = 0;

    // Call our own 'after getting' function.  Because we're not a 'leaf'
    // source, we can call this directly, without risking infinite recursion.
    afterGetting(this);
  } else {
    // We were unable to parse a complete frame from the input, because:
    // - we had to read more data from the source stream, or
    // - the source stream has ended.
  }
}