2002-06-22 10:11:59 +02:00
|
|
|
/*
|
|
|
|
* cutter.c: The video cutting facilities
|
|
|
|
*
|
|
|
|
* See the main source file 'vdr.c' for copyright information and
|
|
|
|
* how to reach the author.
|
|
|
|
*
|
2005-10-31 13:14:26 +01:00
|
|
|
* $Id: cutter.c 1.11 2005/10/31 12:26:44 kls Exp $
|
2002-06-22 10:11:59 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "cutter.h"
|
|
|
|
#include "recording.h"
|
|
|
|
#include "remux.h"
|
|
|
|
#include "thread.h"
|
|
|
|
#include "videodir.h"
|
|
|
|
|
|
|
|
// --- cCuttingThread --------------------------------------------------------
|
|
|
|
|
|
|
|
class cCuttingThread : public cThread {
|
|
|
|
private:
|
|
|
|
const char *error;
|
2005-10-31 13:14:26 +01:00
|
|
|
cUnbufferedFile *fromFile, *toFile;
|
2002-06-22 10:11:59 +02:00
|
|
|
cFileName *fromFileName, *toFileName;
|
|
|
|
cIndexFile *fromIndex, *toIndex;
|
|
|
|
cMarks fromMarks, toMarks;
|
|
|
|
protected:
|
|
|
|
virtual void Action(void);
|
|
|
|
public:
|
|
|
|
cCuttingThread(const char *FromFileName, const char *ToFileName);
|
|
|
|
virtual ~cCuttingThread();
|
|
|
|
const char *Error(void) { return error; }
|
|
|
|
};
|
|
|
|
|
|
|
|
cCuttingThread::cCuttingThread(const char *FromFileName, const char *ToFileName)
|
2003-10-18 12:29:08 +02:00
|
|
|
:cThread("video cutting")
|
2002-06-22 10:11:59 +02:00
|
|
|
{
|
|
|
|
error = NULL;
|
2005-10-31 13:14:26 +01:00
|
|
|
fromFile = toFile = NULL;
|
2002-06-22 10:11:59 +02:00
|
|
|
fromFileName = toFileName = NULL;
|
|
|
|
fromIndex = toIndex = NULL;
|
|
|
|
if (fromMarks.Load(FromFileName) && fromMarks.Count()) {
|
|
|
|
fromFileName = new cFileName(FromFileName, false, true);
|
|
|
|
toFileName = new cFileName(ToFileName, true, true);
|
|
|
|
fromIndex = new cIndexFile(FromFileName, false);
|
|
|
|
toIndex = new cIndexFile(ToFileName, true);
|
|
|
|
toMarks.Load(ToFileName); // doesn't actually load marks, just sets the file name
|
|
|
|
Start();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
esyslog("no editing marks found for %s", FromFileName);
|
|
|
|
}
|
|
|
|
|
|
|
|
cCuttingThread::~cCuttingThread()
|
|
|
|
{
|
|
|
|
Cancel(3);
|
|
|
|
delete fromFileName;
|
|
|
|
delete toFileName;
|
|
|
|
delete fromIndex;
|
|
|
|
delete toIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
void cCuttingThread::Action(void)
|
|
|
|
{
|
|
|
|
cMark *Mark = fromMarks.First();
|
|
|
|
if (Mark) {
|
|
|
|
fromFile = fromFileName->Open();
|
|
|
|
toFile = toFileName->Open();
|
2005-10-31 13:14:26 +01:00
|
|
|
if (!fromFile || !toFile)
|
2005-08-13 13:17:24 +02:00
|
|
|
return;
|
2002-06-22 10:11:59 +02:00
|
|
|
int Index = Mark->position;
|
|
|
|
Mark = fromMarks.Next(Mark);
|
|
|
|
int FileSize = 0;
|
|
|
|
int CurrentFileNumber = 0;
|
|
|
|
int LastIFrame = 0;
|
|
|
|
toMarks.Add(0);
|
|
|
|
toMarks.Save();
|
|
|
|
uchar buffer[MAXFRAMESIZE];
|
2003-05-24 12:01:52 +02:00
|
|
|
bool LastMark = false;
|
2003-04-26 15:11:17 +02:00
|
|
|
bool cutIn = true;
|
2005-08-14 11:24:57 +02:00
|
|
|
while (Running()) {
|
2002-06-22 10:11:59 +02:00
|
|
|
uchar FileNumber;
|
|
|
|
int FileOffset, Length;
|
|
|
|
uchar PictureType;
|
|
|
|
|
|
|
|
// Make sure there is enough disk space:
|
|
|
|
|
2003-08-17 09:18:40 +02:00
|
|
|
AssertFreeDiskSpace(-1);
|
2002-06-22 10:11:59 +02:00
|
|
|
|
|
|
|
// Read one frame:
|
|
|
|
|
|
|
|
if (fromIndex->Get(Index++, &FileNumber, &FileOffset, &PictureType, &Length)) {
|
|
|
|
if (FileNumber != CurrentFileNumber) {
|
|
|
|
fromFile = fromFileName->SetOffset(FileNumber, FileOffset);
|
|
|
|
CurrentFileNumber = FileNumber;
|
|
|
|
}
|
2005-10-31 13:14:26 +01:00
|
|
|
if (fromFile) {
|
2002-06-22 10:11:59 +02:00
|
|
|
int len = ReadFrame(fromFile, buffer, Length, sizeof(buffer));
|
|
|
|
if (len < 0) {
|
|
|
|
error = "ReadFrame";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (len != Length) {
|
|
|
|
CurrentFileNumber = 0; // this re-syncs in case the frame was larger than the buffer
|
|
|
|
Length = len;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
error = "fromFile";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
|
|
|
|
// Write one frame:
|
|
|
|
|
|
|
|
if (PictureType == I_FRAME) { // every file shall start with an I_FRAME
|
2003-05-24 12:01:52 +02:00
|
|
|
if (LastMark) // edited version shall end before next I-frame
|
2002-06-22 10:11:59 +02:00
|
|
|
break;
|
|
|
|
if (FileSize > MEGABYTE(Setup.MaxVideoFileSize)) {
|
|
|
|
toFile = toFileName->NextFile();
|
|
|
|
if (toFile < 0) {
|
|
|
|
error = "toFile 1";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
FileSize = 0;
|
|
|
|
}
|
|
|
|
LastIFrame = 0;
|
2003-04-26 15:11:17 +02:00
|
|
|
|
|
|
|
if (cutIn) {
|
|
|
|
cRemux::SetBrokenLink(buffer, Length);
|
|
|
|
cutIn = false;
|
|
|
|
}
|
2002-06-22 10:11:59 +02:00
|
|
|
}
|
2005-10-31 13:14:26 +01:00
|
|
|
if (toFile->Write(buffer, Length) < 0) {
|
2002-06-22 10:11:59 +02:00
|
|
|
error = "safe_write";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!toIndex->Write(PictureType, toFileName->Number(), FileSize)) {
|
|
|
|
error = "toIndex";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
FileSize += Length;
|
|
|
|
if (!LastIFrame)
|
|
|
|
LastIFrame = toIndex->Last();
|
|
|
|
|
|
|
|
// Check editing marks:
|
|
|
|
|
|
|
|
if (Mark && Index >= Mark->position) {
|
|
|
|
Mark = fromMarks.Next(Mark);
|
|
|
|
toMarks.Add(LastIFrame);
|
|
|
|
if (Mark)
|
|
|
|
toMarks.Add(toIndex->Last() + 1);
|
|
|
|
toMarks.Save();
|
|
|
|
if (Mark) {
|
|
|
|
Index = Mark->position;
|
|
|
|
Mark = fromMarks.Next(Mark);
|
|
|
|
CurrentFileNumber = 0; // triggers SetOffset before reading next frame
|
2003-04-26 15:11:17 +02:00
|
|
|
cutIn = true;
|
2002-06-22 10:11:59 +02:00
|
|
|
if (Setup.SplitEditedFiles) {
|
|
|
|
toFile = toFileName->NextFile();
|
|
|
|
if (toFile < 0) {
|
|
|
|
error = "toFile 2";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
FileSize = 0;
|
|
|
|
}
|
|
|
|
}
|
2003-05-24 12:01:52 +02:00
|
|
|
else
|
|
|
|
LastMark = true;
|
2002-06-22 10:11:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
esyslog("no editing marks found!");
|
|
|
|
}
|
|
|
|
|
|
|
|
// --- cCutter ---------------------------------------------------------------
|
|
|
|
|
|
|
|
char *cCutter::editedVersionName = NULL;
|
|
|
|
cCuttingThread *cCutter::cuttingThread = NULL;
|
|
|
|
bool cCutter::error = false;
|
|
|
|
bool cCutter::ended = false;
|
|
|
|
|
|
|
|
bool cCutter::Start(const char *FileName)
|
|
|
|
{
|
|
|
|
if (!cuttingThread) {
|
|
|
|
error = false;
|
|
|
|
ended = false;
|
|
|
|
cRecording Recording(FileName);
|
|
|
|
const char *evn = Recording.PrefixFileName('%');
|
|
|
|
if (evn && RemoveVideoFile(evn) && MakeDirs(evn, true)) {
|
|
|
|
// XXX this can be removed once RenameVideoFile() follows symlinks (see videodir.c)
|
|
|
|
// remove a possible deleted recording with the same name to avoid symlink mixups:
|
|
|
|
char *s = strdup(evn);
|
|
|
|
char *e = strrchr(s, '.');
|
|
|
|
if (e) {
|
|
|
|
if (strcmp(e, ".rec") == 0) {
|
|
|
|
strcpy(e, ".del");
|
|
|
|
RemoveVideoFile(s);
|
|
|
|
}
|
|
|
|
}
|
2002-08-11 13:32:23 +02:00
|
|
|
free(s);
|
2002-06-22 10:11:59 +02:00
|
|
|
// XXX
|
|
|
|
editedVersionName = strdup(evn);
|
2005-05-16 14:45:11 +02:00
|
|
|
Recording.WriteInfo();
|
2004-06-13 20:26:51 +02:00
|
|
|
Recordings.AddByName(editedVersionName);
|
2002-06-22 10:11:59 +02:00
|
|
|
cuttingThread = new cCuttingThread(FileName, editedVersionName);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void cCutter::Stop(void)
|
|
|
|
{
|
2005-08-14 11:24:57 +02:00
|
|
|
bool Interrupted = cuttingThread && cuttingThread->Active();
|
2002-06-22 10:11:59 +02:00
|
|
|
const char *Error = cuttingThread ? cuttingThread->Error() : NULL;
|
|
|
|
delete cuttingThread;
|
|
|
|
cuttingThread = NULL;
|
|
|
|
if ((Interrupted || Error) && editedVersionName) {
|
|
|
|
if (Interrupted)
|
|
|
|
isyslog("editing process has been interrupted");
|
|
|
|
if (Error)
|
|
|
|
esyslog("ERROR: '%s' during editing process", Error);
|
|
|
|
RemoveVideoFile(editedVersionName); //XXX what if this file is currently being replayed?
|
2004-06-13 20:26:51 +02:00
|
|
|
Recordings.DelByName(editedVersionName);
|
2002-06-22 10:11:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cCutter::Active(void)
|
|
|
|
{
|
|
|
|
if (cuttingThread) {
|
2005-08-14 11:24:57 +02:00
|
|
|
if (cuttingThread->Active())
|
2002-06-22 10:11:59 +02:00
|
|
|
return true;
|
|
|
|
error = cuttingThread->Error();
|
|
|
|
Stop();
|
|
|
|
if (!error)
|
|
|
|
cRecordingUserCommand::InvokeCommand(RUC_EDITEDRECORDING, editedVersionName);
|
2002-08-11 13:32:23 +02:00
|
|
|
free(editedVersionName);
|
2002-06-22 10:11:59 +02:00
|
|
|
editedVersionName = NULL;
|
|
|
|
ended = true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cCutter::Error(void)
|
|
|
|
{
|
|
|
|
bool result = error;
|
|
|
|
error = false;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cCutter::Ended(void)
|
|
|
|
{
|
|
|
|
bool result = ended;
|
|
|
|
ended = false;
|
|
|
|
return result;
|
|
|
|
}
|