00001 #ifndef Impala_Core_VideoSet_SegmentationDocument_h
00002 #define Impala_Core_VideoSet_SegmentationDocument_h
00003
00004 #include "Core/Database/DataDocument.h"
00005 #include "Core/VideoSet/TableShots.h"
00006 #include "Core/VideoSet/Segmentation.h"
00007 #include "Core/VideoSet/Keyframes.h"
00008 #include "Core/ImageSet/ImageSet.h"
00009 #include "Core/Table/RemoveRow.h"
00010 #include "Core/Column/Copy.h"
00011 #include "Core/Column/Find.h"
00012 #include "Core/Column/Contains.h"
00013
00014 namespace Impala
00015 {
00016 namespace Core
00017 {
00018 namespace VideoSet
00019 {
00020
00021
00022 class SegmentationDocument : public Database::DataDocument
00023 {
00024 public:
00025 typedef TableShots BookmarkedType;
00026 typedef Core::ImageSet::ImageSet ImageSet;
00027
00028 SegmentationDocument(VideoSet* vidSet, Segmentation* shots,
00029 Keyframes* keyframes, ImageSet* imSetKeyfr,
00030 ImageSet* imSetThumb, ImageSet* imSetStill) :
00031 Database::DataDocument(vidSet)
00032 {
00033 mVidSet = vidSet;
00034 mShots = shots;
00035 mKeyframes = keyframes;
00036 mImSetKeyfr = imSetKeyfr;
00037 mImSetThumb = imSetThumb;
00038 mImSetStill = imSetStill;
00039
00040 mCurrentKeyfrId = -1;
00041 mCurrentShotId = -1;
00042 mCurrentKeyframes = new TableIds(50);
00043
00044 mBookmarked = new TableShots();
00045 mBookmarkedIds = new TableIds(0);
00046 }
00047
00048 VideoSet*
00049 GetVideoSet()
00050 {
00051 return mVidSet;
00052 }
00053
00054 Segmentation*
00055 GetSegmentation()
00056 {
00057 return mShots;
00058 }
00059
00060 Keyframes*
00061 GetKeyframes()
00062 {
00063 return mKeyframes;
00064 }
00065
00066 ImageSet*
00067 GetImSetKeyframes(bool thumbnails)
00068 {
00069 if (thumbnails)
00070 return mImSetThumb;
00071 return mImSetKeyfr;
00072 }
00073
00074 ImageSet*
00075 GetImSetStills()
00076 {
00077 return mImSetStill;
00078 }
00079
00080
00081
00082 virtual bool
00083 HasCursor()
00084 {
00085 return HasCurShot();
00086 }
00087
00088 bool
00089 GotoPrevVideo()
00090 {
00091 return TryVideoMoveCursor(-1);
00092 }
00093
00094 bool
00095 GotoNextVideo()
00096 {
00097 return TryVideoMoveCursor(1);
00098 }
00099
00100 virtual Database::DocLevel
00101 CursorToDir(int dirId)
00102 {
00103 mCurDirId = dirId;
00104 return CursorToFile(mVidSet->GetFirstFileId(dirId));
00105 }
00106
00107 virtual Database::DocLevel
00108 CursorPrevFile()
00109 {
00110 if (GotoPrevVideo())
00111 return Database::LEVEL_FILE;
00112 return Database::LEVEL_NONE;
00113 }
00114
00115 virtual Database::DocLevel
00116 CursorNextFile()
00117 {
00118 if (GotoNextVideo())
00119 return Database::LEVEL_FILE;
00120 return Database::LEVEL_NONE;
00121 }
00122
00123
00124 virtual Database::DocLevel
00125 CursorToFile(int fileId)
00126 {
00127 ILOG_DEBUG("fileId = " << fileId);
00128 if (TryVideoMove(fileId))
00129 return Database::LEVEL_FILE;
00130 return Database::LEVEL_NONE;
00131 }
00132
00133 virtual Database::DocLevel
00134 CursorPrevShot()
00135 {
00136 if (GotoPrevShot(1))
00137 return Database::LEVEL_SHOT;
00138 return Database::LEVEL_NONE;
00139 }
00140
00141 virtual Database::DocLevel
00142 CursorNextShot()
00143 {
00144 if (GotoNextShot(1))
00145 return Database::LEVEL_SHOT;
00146 return Database::LEVEL_NONE;
00147 }
00148
00149 Database::DocLevel
00150 CursorToBookmark(int idx)
00151 {
00152 if (mBookmarked->SetCursor(idx))
00153 return CursorToCurBookmark();
00154 return Database::LEVEL_NONE;
00155 }
00156
00157 Database::DocLevel
00158 CursorNextBookmark()
00159 {
00160 if (mBookmarked->MoveCursor(1))
00161 return CursorToCurBookmark();
00162 return Database::LEVEL_NONE;
00163 }
00164
00165 Database::DocLevel
00166 CursorPrevBookmark()
00167 {
00168 if (mBookmarked->MoveCursor(-1))
00169 return CursorToCurBookmark();
00170 return Database::LEVEL_NONE;
00171 }
00172
00173 virtual Database::DocLevel
00174 CursorToCurBookmark()
00175 {
00176 if (!mBookmarked->HasCursor())
00177 return Database::LEVEL_NONE;
00178 GotoKeyfr(mBookmarked->Get2AtCursor());
00179 SetRect(mBookmarked->Get4AtCursor());
00180 SetConcept(mBookmarked->Get3AtCursor());
00181 return Database::LEVEL_FILE;
00182 }
00183
00184
00185
00186 virtual Array::Array2dVec3UInt8*
00187 VisualAtCursor(bool thumbnail)
00188 {
00189 if (!HasCursor())
00190 return 0;
00191 if (thumbnail && mImSetThumb)
00192 return mImSetThumb->GetImage(mCurrentKeyfrId);
00193 if (!thumbnail && mImSetKeyfr)
00194 return mImSetKeyfr->GetImage(mCurrentKeyfrId);
00195 return 0;
00196 }
00197
00198 bool
00199 HasCurShot()
00200 {
00201 return mCurrentKeyfrId != -1;
00202 }
00203
00204 int
00205 CurShot()
00206 {
00207 return mCurrentShotId;
00208 }
00209
00210 int
00211 CurKeyfr()
00212 {
00213 return mCurrentKeyfrId;
00214 }
00215
00216 TableIds*
00217 CurKeyframes()
00218 {
00219 return mCurrentKeyframes;
00220 }
00221
00222 int
00223 CurFrame()
00224 {
00225 if (mCurrentKeyfrId == -1)
00226 return -1;
00227 return mKeyframes->GetFrameNr(mCurrentKeyfrId);
00228 }
00229
00230 void
00231 GotoKeyfr(int keyfrId)
00232 {
00233 mCurrentKeyfrId = keyfrId;
00234 ILOG_DEBUG("mCurrentKeyfrId = " << mCurrentKeyfrId);
00235 mCurrentShotId = -1;
00236 mCurrentKeyframes->SetEmpty();
00237 if (mCurrentKeyfrId != -1)
00238 {
00239 mCurrentShotId = mKeyframes->GetShotId(mCurrentKeyfrId);
00240 int nrKey = mKeyframes->GetNrKeyframesShot(mCurrentShotId);
00241 int firstKey = mKeyframes->GetFirstKeyframeShot(mCurrentShotId);
00242 for (int i=0 ; i<nrKey ; i++)
00243 mCurrentKeyframes->Add(firstKey + i);
00244 }
00245 ILOG_DEBUG("mCurrentShotId = " << mCurrentShotId);
00246
00247 if (mCurrentShotId == -1)
00248 GotoFile(-1);
00249 else
00250 GotoFile(mShots->GetVideoId(mCurrentShotId));
00251 }
00252
00253 bool
00254 GotoShotKeyfr(int idx)
00255 {
00256 if (!HasCurShot())
00257 return false;
00258 mCurrentKeyfrId = mCurrentKeyframes->Get1(idx);
00259 GotoFile(mShots->GetVideoId(mCurrentShotId));
00260 return true;
00261 }
00262
00263 void
00264 GotoShot(int shot)
00265 {
00266 GotoKeyfr(mKeyframes->GetFirstKeyframeShot(shot));
00267 }
00268
00269 bool
00270 GotoPrevShot(int steps = 1, bool withinVideo = true)
00271 {
00272 return TryShotMove(mCurrentShotId - steps, withinVideo);
00273 }
00274
00275 bool
00276 GotoNextShot(int steps = 1, bool withinVideo = true)
00277 {
00278 return TryShotMove(mCurrentShotId + steps, withinVideo);
00279 }
00280
00281
00282 int
00283 GetPrevShot(int steps = 1)
00284 {
00285 return GetShotMove(mCurrentShotId, -1, steps);
00286 }
00287
00288
00289 int
00290 GetNextShot(int steps = 1)
00291 {
00292 return GetShotMove(mCurrentShotId, 1, steps);
00293 }
00294
00295
00296
00297 BookmarkedType*
00298 GetBookmarked()
00299 {
00300 return mBookmarked;
00301 }
00302
00303
00304 TableIds*
00305 GetBookmarkedIds()
00306 {
00307 mBookmarkedIds->ReserveMin(mBookmarked->Size(), false);
00308 Column::Copy(mBookmarkedIds->GetColumn1(), mBookmarked->GetColumn2(),
00309 mBookmarked->Size(), 0, 0);
00310 mBookmarkedIds->SetSize(mBookmarked->Size());
00311 return mBookmarkedIds;
00312 }
00313
00314 virtual bool
00315 HasBookmarks()
00316 {
00317 return mBookmarked->Size() > 0;
00318 }
00319
00320 bool
00321 HasCurBookmark()
00322 {
00323 return mBookmarked->HasCursor();
00324 }
00325
00326 int
00327 CurBookmark()
00328 {
00329 return mBookmarked->GetCursor();
00330 }
00331
00332 std::string
00333 CurBookmarkAnno()
00334 {
00335 if (!HasCurBookmark())
00336 return std::string("");
00337 return mBookmarked->Get3AtCursor();
00338 }
00339
00340 virtual std::string
00341 DescribeBookmark(int idx)
00342 {
00343 int shot = mBookmarked->Get1(idx);
00344 return mShots->GetName(shot) + " " + mBookmarked->Get3(idx);
00345 }
00346
00347 virtual bool
00348 AddCursorToBookmarked()
00349 {
00350 if (! HasCursor())
00351 return false;
00352 if (Column::Contains(mBookmarked->GetColumn1(), CurShot(),
00353 0, mBookmarked->Size()))
00354 return false;
00355 return AddShotToBookmarked(CurShot(), CurKeyfr());
00356 }
00357
00358 virtual bool
00359 RemoveCursorFromBookmarked()
00360 {
00361 if (! HasCurBookmark())
00362 return false;
00363 int cursor = mBookmarked->GetCursor();
00364 Table::RemoveRow(mBookmarked, mBookmarked->GetCursor());
00365 CursorToBookmark(cursor);
00366 return true;
00367 }
00368
00369 virtual void
00370 ClearBookmarked()
00371 {
00372 mBookmarked->SetEmpty();
00373 }
00374
00375 virtual void
00376 LoadBookmarked(std::string fileName)
00377 {
00378 mBookmarkedFileName = fileName;
00379 mBookmarked->ReadVxsFile(fileName, mKeyframes);
00380 }
00381
00382
00383 virtual void
00384 SaveBookmarked(std::string fileName, std::string conceptName)
00385 {
00386 mBookmarked->WriteVxsFile(fileName, conceptName, mShots, mKeyframes);
00387 }
00388
00389 std::string
00390 GetBookmarkedFileName()
00391 {
00392 return mBookmarkedFileName;
00393 }
00394
00395 void
00396 LoadConceptsFromBookmarks()
00397 {
00398 for (int i=0 ; i<mBookmarked->Size() ; i++)
00399 AddConcept(mBookmarked->Get3(i));
00400 }
00401
00402 bool
00403 AddShotToBookmarked(int shot, int keyfr)
00404 {
00405 return mBookmarked->Add(shot, keyfr, GetConcept(), GetRect());
00406 }
00407
00408 int
00409 GetKeyOfBookmarked(int idx)
00410 {
00411 return mBookmarked->Get2(idx);
00412 }
00413
00414 int
00415 GetKeyOfBookmarkedShot(int shot)
00416 {
00417 int bookIdx = Column::Find(mBookmarked->GetColumn1(), shot,
00418 0, mBookmarked->Size());
00419 if (bookIdx != mBookmarked->Size())
00420 return GetKeyOfBookmarked(bookIdx);
00421 return -1;
00422 }
00423
00424
00425
00426 virtual void
00427 Clear()
00428 {
00429 Database::DataDocument::Clear();
00430 mBookmarked->SetEmpty();
00431 GotoKeyfr(-1);
00432 }
00433
00434 int
00435 GetShotRepresentative(int shot)
00436 {
00437 if (shot == -1)
00438 return -1;
00439 int bookIdx = Column::Find(mBookmarked->GetColumn1(), shot,
00440 0, mBookmarked->Size());
00441 if (bookIdx != mBookmarked->Size())
00442 return GetKeyOfBookmarked(bookIdx);
00443 return mKeyframes->GetShotRKF(shot);
00444 }
00445
00446 private:
00447
00448 bool
00449 TryVideoMoveCursor(int inc)
00450 {
00451 if (!HasCursor())
00452 return false;
00453 return TryVideoMove(CurFileId() + inc);
00454 }
00455
00456 bool
00457 TryVideoMove(int vid)
00458 {
00459 ILOG_DEBUG("vid = " << vid);
00460 if ((vid < 0) || (vid >= mVidSet->NrFiles()))
00461 return false;
00462 int newShot = mShots->GetFirstShotVideo(vid);
00463 ILOG_DEBUG("newShot = " << newShot);
00464 GotoKeyfr(GetShotRepresentative(newShot));
00465 return true;
00466 }
00467
00468 bool
00469 TryShotMove(int newShot, bool withinVideo)
00470 {
00471 if ((newShot < 0) || (newShot >= mShots->GetNrShots()))
00472 return false;
00473 if (!HasCurShot())
00474 return false;
00475 if (withinVideo && (! SameVideo(mCurrentShotId, newShot)))
00476 return false;
00477 GotoKeyfr(GetShotRepresentative(newShot));
00478 return true;
00479 }
00480
00481 int
00482 GetShotMove(int oldShot, int dir, int steps)
00483 {
00484 int newShot = oldShot + dir;
00485 if ((newShot < 0) || (newShot >= mShots->GetNrShots()))
00486 return -1;
00487 if (! SameVideo(mCurrentShotId, newShot))
00488 return -1;
00489 if (--steps <= 0)
00490 return newShot;
00491 return GetShotMove(newShot, dir, steps);
00492 }
00493
00494 bool
00495 SameVideo(int shotId1, int shotId2)
00496 {
00497 return mShots->GetVideoId(shotId1) == mShots->GetVideoId(shotId2);
00498 }
00499
00500 bool
00501 SameShot(int keyfrId1, int keyfrId2)
00502 {
00503 return mKeyframes->GetShotId(keyfrId1) == mKeyframes->GetShotId(keyfrId2);
00504 }
00505
00506 VideoSet* mVidSet;
00507 Segmentation* mShots;
00508 Keyframes* mKeyframes;
00509 ImageSet* mImSetThumb;
00510 ImageSet* mImSetKeyfr;
00511 ImageSet* mImSetStill;
00512
00513 int mCurrentShotId;
00514 int mCurrentKeyfrId;
00515 TableIds* mCurrentKeyframes;
00516
00517 TableShots* mBookmarked;
00518 TableIds* mBookmarkedIds;
00519 std::string mBookmarkedFileName;
00520
00521 ILOG_VAR_DEC;
00522 };
00523
00524 ILOG_VAR_INIT(SegmentationDocument, Impala.Core.VideoSet);
00525
00526 }
00527 }
00528 }
00529
00530 #endif