liveMedia/DVVideoStreamFramer.cpp

Go to the documentation of this file.
00001 /**********
00002 This library is free software; you can redistribute it and/or modify it under
00003 the terms of the GNU Lesser General Public License as published by the
00004 Free Software Foundation; either version 2.1 of the License, or (at your
00005 option) any later version. (See <http://www.gnu.org/copyleft/lesser.html>.)
00006 
00007 This library is distributed in the hope that it will be useful, but WITHOUT
00008 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00009 FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for
00010 more details.
00011 
00012 You should have received a copy of the GNU Lesser General Public License
00013 along with this library; if not, write to the Free Software Foundation, Inc.,
00014 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
00015 **********/
00016 // "liveMedia"
00017 // Copyright (c) 1996-2012 Live Networks, Inc.  All rights reserved.
00018 // A filter that parses a DV input stream into DV frames to deliver to the downstream object
00019 // Implementation
00020 // (Thanks to Ben Hutchings for his help, including a prototype implementation.)
00021 
00022 #include "DVVideoStreamFramer.hh"
00023 #include "GroupsockHelper.hh"
00024 
00026 
00027 DVVideoStreamFramer::DVVideoStreamFramer(UsageEnvironment& env, FramedSource* inputSource, Boolean sourceIsSeekable)
00028   : FramedFilter(env, inputSource),
00029     fOurProfile(NULL), fInitialBlocksPresent(False), fSourceIsSeekable(sourceIsSeekable) {
00030   fTo = NULL; // hack used when reading "fSavedInitialBlocks"
00031   // Use the current wallclock time as the initial 'presentation time':
00032   gettimeofday(&fNextFramePresentationTime, NULL);
00033 }
00034 
00035 DVVideoStreamFramer::~DVVideoStreamFramer() {
00036 }
00037 
00038 DVVideoStreamFramer*
00039 DVVideoStreamFramer::createNew(UsageEnvironment& env, FramedSource* inputSource, Boolean sourceIsSeekable) {
00040   return new DVVideoStreamFramer(env, inputSource, sourceIsSeekable);
00041 }
00042 
00043 // Define the parameters for the profiles that we understand:
00044 struct DVVideoProfile {
00045   char const* name;
00046   unsigned apt;
00047   unsigned sType;
00048   unsigned sequenceCount;
00049   unsigned channelCount;
00050   unsigned dvFrameSize; // in bytes (== sequenceCount*channelCount*(DV_NUM_BLOCKS_PER_SEQUENCE*DV_DIF_BLOCK_SIZE i.e. 12000))
00051   double frameDuration; // duration of the above, in microseconds.  (1000000/this == frame rate)
00052 };
00053 
00054 static DVVideoProfile const profiles[] = {
00055    { "SD-VCR/525-60",  0, 0x00, 10, 1, 120000, (1000000*1001)/30000.0 },
00056    { "SD-VCR/625-50",  0, 0x00, 12, 1, 144000, 1000000/25.0 },
00057    { "314M-25/525-60", 1, 0x00, 10, 1, 120000, (1000000*1001)/30000.0 },
00058    { "314M-25/625-50", 1, 0x00, 12, 1, 144000, 1000000/25.0 },
00059    { "314M-50/525-60", 1, 0x04, 10, 2, 240000, (1000000*1001)/30000.0 },
00060    { "314M-50/625-50", 1, 0x04, 12, 2, 288000, 1000000/25.0 },
00061    { "370M/1080-60i",  1, 0x14, 10, 4, 480000, (1000000*1001)/30000.0 },
00062    { "370M/1080-50i",  1, 0x14, 12, 4, 576000, 1000000/25.0 },
00063    { "370M/720-60p",   1, 0x18, 10, 2, 240000, (1000000*1001)/60000.0 },
00064    { "370M/720-50p",   1, 0x18, 12, 2, 288000, 1000000/50.0 },
00065    { NULL, 0, 0, 0, 0, 0, 0.0 }
00066   };
00067 
00068 
00069 char const* DVVideoStreamFramer::profileName() {
00070   if (fOurProfile == NULL) getProfile();
00071 
00072   return fOurProfile != NULL ? ((DVVideoProfile const*)fOurProfile)->name : NULL;
00073 }
00074 
00075 Boolean DVVideoStreamFramer::getFrameParameters(unsigned& frameSize, double& frameDuration) {
00076   if (fOurProfile == NULL) getProfile();
00077   if (fOurProfile == NULL) return False;
00078 
00079   frameSize = ((DVVideoProfile const*)fOurProfile)->dvFrameSize;
00080   frameDuration = ((DVVideoProfile const*)fOurProfile)->frameDuration;
00081   return True;
00082 }
00083 
00084 void DVVideoStreamFramer::getProfile() {
00085   // To determine the stream's profile, we need to first read a chunk of data that we can parse:
00086   fInputSource->getNextFrame(fSavedInitialBlocks, DV_SAVED_INITIAL_BLOCKS_SIZE,
00087                              afterGettingFrame, this, FramedSource::handleClosure, this);
00088   
00089   // Handle events until the requested data arrives:
00090   envir().taskScheduler().doEventLoop(&fInitialBlocksPresent);
00091 }
00092 
00093 Boolean DVVideoStreamFramer::isDVVideoStreamFramer() const {
00094   return True;
00095 }
00096 
00097 void DVVideoStreamFramer::doGetNextFrame() {
00098   fFrameSize = 0; // initially, until we deliver data
00099 
00100   // If we have saved initial blocks (and won't be seeking back to re-read this data), so use this data first.
00101   if (fInitialBlocksPresent && !fSourceIsSeekable) {
00102     // For simplicity, we require the downstream object's buffer to be >= this data's size:
00103     if (fMaxSize < DV_SAVED_INITIAL_BLOCKS_SIZE) {
00104       fNumTruncatedBytes = fMaxSize;
00105       afterGetting(this);
00106       return;
00107     }
00108 
00109     memmove(fTo, fSavedInitialBlocks, DV_SAVED_INITIAL_BLOCKS_SIZE);
00110     fFrameSize = DV_SAVED_INITIAL_BLOCKS_SIZE;
00111     fTo += DV_SAVED_INITIAL_BLOCKS_SIZE;
00112     fInitialBlocksPresent = False; // for the future
00113   }
00114     
00115   // Arrange to read the (rest of the) requested data.
00116   // (But first, make sure that we read an integral multiple of the DV block size.)
00117   fMaxSize -= fMaxSize%DV_DIF_BLOCK_SIZE;
00118   getAndDeliverData();
00119 }
00120 
00121 #define DV_SMALLEST_POSSIBLE_FRAME_SIZE 120000
00122 
00123 void DVVideoStreamFramer::getAndDeliverData() {
00124   unsigned const totFrameSize
00125     = fOurProfile != NULL ? ((DVVideoProfile const*)fOurProfile)->dvFrameSize : DV_SMALLEST_POSSIBLE_FRAME_SIZE;
00126   unsigned totBytesToDeliver = totFrameSize < fMaxSize ? totFrameSize : fMaxSize;
00127   unsigned numBytesToRead = totBytesToDeliver - fFrameSize;
00128 
00129   fInputSource->getNextFrame(fTo, numBytesToRead, afterGettingFrame, this, FramedSource::handleClosure, this);
00130 }
00131 
00132 void DVVideoStreamFramer::afterGettingFrame(void* clientData, unsigned frameSize,
00133                                             unsigned numTruncatedBytes,
00134                                             struct timeval /*presentationTime*/, unsigned /*durationInMicroseconds*/) {
00135   DVVideoStreamFramer* source = (DVVideoStreamFramer*)clientData;
00136   source->afterGettingFrame1(frameSize, numTruncatedBytes);
00137 }
00138 
00139 #define DVSectionId(n) ptr[(n)*DV_DIF_BLOCK_SIZE + 0]
00140 #define DVData(n,i) ptr[(n)*DV_DIF_BLOCK_SIZE + 3+(i)]
00141 
00142 #define DV_SECTION_HEADER 0x1F
00143 #define DV_PACK_HEADER_10 0x3F
00144 #define DV_PACK_HEADER_12 0xBF
00145 #define DV_SECTION_VAUX_MIN 0x50
00146 #define DV_SECTION_VAUX_MAX 0x5F
00147 #define DV_PACK_VIDEO_SOURCE 60
00148 #ifndef MILLION
00149 #define MILLION 1000000
00150 #endif
00151 
00152 void DVVideoStreamFramer::afterGettingFrame1(unsigned frameSize, unsigned numTruncatedBytes) {
00153   if (fOurProfile == NULL && frameSize >= DV_SAVED_INITIAL_BLOCKS_SIZE) {
00154     // (Try to) parse this data enough to figure out its profile.
00155     // We assume that the data begins on a (80-byte) block boundary, but not necessarily on a (150-block) sequence boundary.
00156     // We therefore scan each 80-byte block, until we find the 6-block header that begins a sequence:
00157     u_int8_t const* data = (fTo == NULL) ? fSavedInitialBlocks : fTo;
00158     for (u_int8_t const* ptr = data; ptr + 6*DV_DIF_BLOCK_SIZE <= &data[DV_SAVED_INITIAL_BLOCKS_SIZE]; ptr += DV_DIF_BLOCK_SIZE) {
00159       // Check whether "ptr" points to an appropriate header:
00160       u_int8_t const sectionHeader = DVSectionId(0);
00161       u_int8_t const sectionVAUX = DVSectionId(5);
00162       u_int8_t const packHeaderNum = DVData(0,0);
00163 
00164       if (sectionHeader == DV_SECTION_HEADER
00165           && (packHeaderNum == DV_PACK_HEADER_10 || packHeaderNum == DV_PACK_HEADER_12)
00166           && (sectionVAUX >= DV_SECTION_VAUX_MIN && sectionVAUX <= DV_SECTION_VAUX_MAX)) {
00167         // This data begins a sequence; look up the DV profile from this:
00168         u_int8_t const apt = DVData(0,1)&0x07;
00169         u_int8_t const sType = DVData(5,48)&0x1F;
00170         u_int8_t const sequenceCount = (packHeaderNum == DV_PACK_HEADER_10) ? 10 : 12;
00171 
00172         // Use these three parameters (apt, sType, sequenceCount) to look up the DV profile:
00173         for (DVVideoProfile const* profile = profiles; profile->name != NULL; ++profile) {
00174           if (profile->apt == apt && profile->sType == sType && profile->sequenceCount == sequenceCount) {
00175             fOurProfile = profile;
00176             break;
00177           }
00178         }
00179         break; // because we found a correct sequence header (even if we don't happen to define a profile for it)
00180       }
00181     }
00182   }
00183 
00184   if (fTo != NULL) { // There is a downstream object; complete delivery to it (or read more data, if necessary)
00185     unsigned const totFrameSize
00186       = fOurProfile != NULL ? ((DVVideoProfile const*)fOurProfile)->dvFrameSize : DV_SMALLEST_POSSIBLE_FRAME_SIZE;
00187     fFrameSize += frameSize;
00188     fTo += frameSize;
00189 
00190     if (fFrameSize < totFrameSize && fFrameSize < fMaxSize && numTruncatedBytes == 0) {
00191       // We have more data to deliver; get it now:
00192       getAndDeliverData();
00193     } else {
00194       // We're done delivering this DV frame (but check for truncation):
00195       fNumTruncatedBytes = totFrameSize - fFrameSize;
00196 
00197       if (fOurProfile != NULL) {
00198         // Also set the presentation time, and increment it for next time,
00199         // based on the length of this frame:
00200         fPresentationTime = fNextFramePresentationTime;
00201 
00202         DVVideoProfile const* ourProfile =(DVVideoProfile const*)fOurProfile;
00203         double durationInMicroseconds = (fFrameSize*ourProfile->frameDuration)/ourProfile->dvFrameSize;
00204         fDurationInMicroseconds = (unsigned)durationInMicroseconds;
00205         fNextFramePresentationTime.tv_usec += fDurationInMicroseconds;
00206         fNextFramePresentationTime.tv_sec += fNextFramePresentationTime.tv_usec/MILLION;
00207         fNextFramePresentationTime.tv_usec %= MILLION;
00208       }
00209 
00210       afterGetting(this);
00211     }
00212   } else {
00213     // We read data into our special buffer; signal that it has arrived:
00214     fInitialBlocksPresent = True;
00215   }
00216 }

Generated on Thu Feb 2 23:51:29 2012 for live by  doxygen 1.5.2