moviecut: formatting code using astyle

Origin commit data
------------------
Branch: ni/coolstream
Commit: 426c5f41fe
Author: vanhofen <vanhofen@gmx.de>
Date: 2022-09-17 (Sat, 17 Sep 2022)

Origin message was:
------------------
- moviecut: formatting code using astyle

------------------
No further description and justification available within origin commit message!

------------------
This commit was generated by Migit
This commit is contained in:
vanhofen
2022-09-17 23:27:05 +02:00
parent 983f05da8e
commit 3bf3fdf369
2 changed files with 199 additions and 134 deletions

View File

@@ -55,11 +55,12 @@
typedef struct pvr_file_info typedef struct pvr_file_info
{ {
uint32_t uDuration; /* Time duration in Ms */ uint32_t uDuration; /* Time duration in Ms */
uint32_t uTSPacketSize; uint32_t uTSPacketSize;
} PVR_FILE_INFO; } PVR_FILE_INFO;
struct mybook { struct mybook
{
off64_t pos; off64_t pos;
off64_t len; off64_t len;
bool ok; bool ok;
@@ -71,7 +72,7 @@ CMovieCut::CMovieCut()
timescale = NULL; timescale = NULL;
percent = 0; percent = 0;
int dx = 256; int dx = 256;
x = (((g_settings.screen_EndX- g_settings.screen_StartX)- dx) / 2) + g_settings.screen_StartX; x = (((g_settings.screen_EndX - g_settings.screen_StartX) - dx) / 2) + g_settings.screen_StartX;
y = g_settings.screen_EndY - 50; y = g_settings.screen_EndY - 50;
} }
@@ -82,47 +83,54 @@ CMovieCut::~CMovieCut()
void CMovieCut::paintProgress(bool refresh) void CMovieCut::paintProgress(bool refresh)
{ {
if (!timescale) { if (!timescale)
{
timescale = new CProgressBar(); timescale = new CProgressBar();
timescale->setType(CProgressBar::PB_TIMESCALE); timescale->setType(CProgressBar::PB_TIMESCALE);
} }
if (refresh) { if (refresh)
frameBuffer->paintBoxRel(x + 40, y+12, 200, 15, COL_INFOBAR_PLUS_0);//TODO: remove unneeded box paints {
frameBuffer->paintBoxRel(x + 40, y + 12, 200, 15, COL_INFOBAR_PLUS_0); //TODO: remove unneeded box paints
timescale->reset(); timescale->reset();
} }
timescale->setProgress(x + 41, y + 12, 200, 15, percent, 100); timescale->setProgress(x + 41, y + 12, 200, 15, percent, 100);
timescale->paint(); timescale->paint();
} }
void CMovieCut::reset_atime(const char * path, time_t tt) void CMovieCut::reset_atime(const char *path, time_t tt)
{ {
struct utimbuf ut; struct utimbuf ut;
ut.actime = tt-1; ut.actime = tt - 1;
ut.modtime = tt-1; ut.modtime = tt - 1;
utime(path, &ut); utime(path, &ut);
} }
void CMovieCut::WriteHeader(const char * path, uint32_t duration) void CMovieCut::WriteHeader(const char *path, uint32_t duration)
{ {
int srcfd = open(path, O_WRONLY | O_LARGEFILE); int srcfd = open(path, O_WRONLY | O_LARGEFILE);
if (srcfd >= 0) { if (srcfd >= 0)
if (lseek64(srcfd, 188-sizeof(PVR_FILE_INFO), SEEK_SET) >= 0) { {
if (lseek64(srcfd, 188 - sizeof(PVR_FILE_INFO), SEEK_SET) >= 0)
{
PVR_FILE_INFO pinfo; PVR_FILE_INFO pinfo;
pinfo.uDuration = duration; pinfo.uDuration = duration;
pinfo.uTSPacketSize = 188; pinfo.uTSPacketSize = 188;
write(srcfd, (uint8_t *)&pinfo, sizeof(PVR_FILE_INFO)); write(srcfd, (uint8_t *)&pinfo, sizeof(PVR_FILE_INFO));
} }
close(srcfd); close(srcfd);
} else }
else
perror(path); perror(path);
} }
uint32_t CMovieCut::getHeaderDurationMS(MI_MOVIE_INFO * minfo) uint32_t CMovieCut::getHeaderDurationMS(MI_MOVIE_INFO *minfo)
{ {
uint32_t duration = 0; uint32_t duration = 0;
int srcfd = open(minfo->file.Name.c_str(), O_RDONLY | O_LARGEFILE); int srcfd = open(minfo->file.Name.c_str(), O_RDONLY | O_LARGEFILE);
if (srcfd >= 0) { if (srcfd >= 0)
if (lseek64(srcfd, 188-sizeof(PVR_FILE_INFO), SEEK_SET) >= 0) { {
if (lseek64(srcfd, 188 - sizeof(PVR_FILE_INFO), SEEK_SET) >= 0)
{
PVR_FILE_INFO pinfo; PVR_FILE_INFO pinfo;
memset(&pinfo, 0, sizeof(PVR_FILE_INFO)); memset(&pinfo, 0, sizeof(PVR_FILE_INFO));
read(srcfd, (uint8_t *)&pinfo, sizeof(PVR_FILE_INFO)); read(srcfd, (uint8_t *)&pinfo, sizeof(PVR_FILE_INFO));
@@ -131,16 +139,18 @@ uint32_t CMovieCut::getHeaderDurationMS(MI_MOVIE_INFO * minfo)
close(srcfd); close(srcfd);
printf("CMovieCut::%s: [%s] duration %d ms\n", __func__, minfo->file.Name.c_str(), duration); printf("CMovieCut::%s: [%s] duration %d ms\n", __func__, minfo->file.Name.c_str(), duration);
} }
} else }
else
perror(minfo->file.Name.c_str()); perror(minfo->file.Name.c_str());
return duration; return duration;
} }
off64_t CMovieCut::getSecondSize(MI_MOVIE_INFO * minfo) off64_t CMovieCut::getSecondSize(MI_MOVIE_INFO *minfo)
{ {
struct stat64 s; struct stat64 s;
if (stat64(minfo->file.Name.c_str(), &s)) { if (stat64(minfo->file.Name.c_str(), &s))
{
perror(minfo->file.Name.c_str()); perror(minfo->file.Name.c_str());
return 0; return 0;
} }
@@ -154,11 +164,11 @@ off64_t CMovieCut::getSecondSize(MI_MOVIE_INFO * minfo)
duration = len * 60 * 1000; duration = len * 60 * 1000;
off64_t mssize = ((float)s.st_size / (float)duration); off64_t mssize = ((float)s.st_size / (float)duration);
printf("CMovieCut::%s: [%s] bytes per second: %" PRId64 "\n", __func__, minfo->file.Name.c_str(), mssize*1000); printf("CMovieCut::%s: [%s] bytes per second: %" PRId64 "\n", __func__, minfo->file.Name.c_str(), mssize * 1000);
return mssize*1000; return mssize * 1000;
} }
bool CMovieCut::truncateMovie(MI_MOVIE_INFO * minfo) bool CMovieCut::truncateMovie(MI_MOVIE_INFO *minfo)
{ {
off64_t secsize = getSecondSize(minfo); off64_t secsize = getSecondSize(minfo);
if (minfo->bookmarks.end == 0 || secsize == 0) if (minfo->bookmarks.end == 0 || secsize == 0)
@@ -167,39 +177,41 @@ bool CMovieCut::truncateMovie(MI_MOVIE_INFO * minfo)
off64_t newsize = secsize * minfo->bookmarks.end; off64_t newsize = secsize * minfo->bookmarks.end;
printf("CMovieCut::%s: [%s] truncate to %d sec, new size %" PRId64 "\n", __func__, minfo->file.Name.c_str(), minfo->bookmarks.end, newsize); printf("CMovieCut::%s: [%s] truncate to %d sec, new size %" PRId64 "\n", __func__, minfo->file.Name.c_str(), minfo->bookmarks.end, newsize);
if (truncate(minfo->file.Name.c_str(), newsize)) { if (truncate(minfo->file.Name.c_str(), newsize))
{
perror(minfo->file.Name.c_str()); perror(minfo->file.Name.c_str());
return false; return false;
} }
minfo->file.Size = newsize; minfo->file.Size = newsize;
minfo->length = minfo->bookmarks.end/60; minfo->length = minfo->bookmarks.end / 60;
minfo->bookmarks.end = 0; minfo->bookmarks.end = 0;
reset_atime(minfo->file.Name.c_str(), minfo->file.Time); reset_atime(minfo->file.Name.c_str(), minfo->file.Time);
CMovieInfo cmovie; CMovieInfo cmovie;
cmovie.saveMovieInfo(*minfo); cmovie.saveMovieInfo(*minfo);
WriteHeader(minfo->file.Name.c_str(), newsize/secsize*1000); WriteHeader(minfo->file.Name.c_str(), newsize / secsize * 1000);
return true; return true;
} }
int CMovieCut::check_pes_start (unsigned char *packet) int CMovieCut::check_pes_start(unsigned char *packet)
{ {
// PCKT: 47 41 91 37 07 50 3F 14 BF 04 FE B9 00 00 01 EA 00 00 8C ... // PCKT: 47 41 91 37 07 50 3F 14 BF 04 FE B9 00 00 01 EA 00 00 8C ...
if (packet[0] == 0x47 && // sync byte 0x47 if (packet[0] == 0x47 /* sync byte 0x47*/ && (packet[1] & 0x40) /* pusi == 1*/)
(packet[1] & 0x40)) // pusi == 1
{ {
/* good, now we have to check if it is video stream */ /* good, now we have to check if it is video stream */
unsigned char *pes = packet + 4; unsigned char *pes = packet + 4;
if (packet[3] & 0x20) // adaptation field is present if (packet[3] & 0x20) // adaptation field is present
pes += packet[4] + 1; pes += packet[4] + 1;
if (!memcmp(pes, "\x00\x00\x01", 3) && (pes[3] & 0xF0) == 0xE0) // PES start & video type if (!memcmp(pes, "\x00\x00\x01", 3) && (pes[3] & 0xF0) == 0xE0) // PES start & video type
{ {
pes += 4; pes += 4;
while (pes < (packet + 188 - 4)) while (pes < (packet + 188 - 4))
{
if (!memcmp(pes, "\x00\x00\x01\xB8", 4)) // GOP detect if (!memcmp(pes, "\x00\x00\x01\xB8", 4)) // GOP detect
return 1; return 1;
else else
pes++; pes++;
}
} }
} }
return 0; return 0;
@@ -207,9 +219,10 @@ int CMovieCut::check_pes_start (unsigned char *packet)
int CMovieCut::find_gop(unsigned char *buf, int r) int CMovieCut::find_gop(unsigned char *buf, int r)
{ {
for (int j = 0; j < r/188; j++) { for (int j = 0; j < r / 188; j++)
if (check_pes_start(&buf[188*j])) {
return 188*j; if (check_pes_start(&buf[188 * j]))
return 188 * j;
} }
return -1; return -1;
} }
@@ -220,19 +233,21 @@ off64_t CMovieCut::fake_read(int fd, unsigned char *buf, size_t size, off64_t fs
buf[0] = 0x47; buf[0] = 0x47;
if ((cur + (off64_t)size) > fsize) if ((cur + (off64_t)size) > fsize)
return(fsize - cur); return (fsize - cur);
else else
return size; return size;
} }
int CMovieCut::read_psi(const char * spart, unsigned char * buf) int CMovieCut::read_psi(const char *spart, unsigned char *buf)
{ {
int srcfd = open(spart, O_RDONLY | O_LARGEFILE); int srcfd = open(spart, O_RDONLY | O_LARGEFILE);
if (srcfd >= 0) { if (srcfd >= 0)
{
/* read psi */ /* read psi */
int r = read(srcfd, buf, PSI_SIZE); int r = read(srcfd, buf, PSI_SIZE);
close(srcfd); close(srcfd);
if (r != PSI_SIZE) { if (r != PSI_SIZE)
{
perror("read psi"); perror("read psi");
return -1; return -1;
} }
@@ -241,32 +256,34 @@ int CMovieCut::read_psi(const char * spart, unsigned char * buf)
return -1; return -1;
} }
void CMovieCut::save_info(MI_MOVIE_INFO * minfo, char * dpart, off64_t spos, off64_t secsize) void CMovieCut::save_info(MI_MOVIE_INFO *minfo, char *dpart, off64_t spos, off64_t secsize)
{ {
CMovieInfo cmovie; CMovieInfo cmovie;
MI_MOVIE_INFO ninfo = *minfo; MI_MOVIE_INFO ninfo = *minfo;
ninfo.file.Name = dpart; ninfo.file.Name = dpart;
ninfo.file.Size = spos; ninfo.file.Size = spos;
ninfo.length = spos/secsize/60; ninfo.length = spos / secsize / 60;
ninfo.bookmarks.end = 0; ninfo.bookmarks.end = 0;
ninfo.bookmarks.start = 0; ninfo.bookmarks.start = 0;
ninfo.bookmarks.lastPlayStop = 0; ninfo.bookmarks.lastPlayStop = 0;
for (int book_nr = 0; book_nr < MI_MOVIE_BOOK_USER_MAX; book_nr++) { for (int book_nr = 0; book_nr < MI_MOVIE_BOOK_USER_MAX; book_nr++)
if (ninfo.bookmarks.user[book_nr].pos != 0 && ninfo.bookmarks.user[book_nr].length > 0) { {
if (ninfo.bookmarks.user[book_nr].pos != 0 && ninfo.bookmarks.user[book_nr].length > 0)
{
ninfo.bookmarks.user[book_nr].pos = 0; ninfo.bookmarks.user[book_nr].pos = 0;
ninfo.bookmarks.user[book_nr].length = 0; ninfo.bookmarks.user[book_nr].length = 0;
} }
} }
cmovie.saveMovieInfo(ninfo); cmovie.saveMovieInfo(ninfo);
WriteHeader(ninfo.file.Name.c_str(), spos/secsize*1000); WriteHeader(ninfo.file.Name.c_str(), spos / secsize * 1000);
reset_atime(dpart, minfo->file.Time); reset_atime(dpart, minfo->file.Time);
} }
void CMovieCut::findNewName(const char * fname, char * dpart, size_t dpart_len) void CMovieCut::findNewName(const char *fname, char *dpart, size_t dpart_len)
{ {
char npart[255]; char npart[255];
snprintf(npart, sizeof(npart), "%s", fname); snprintf(npart, sizeof(npart), "%s", fname);
char * ptr = strstr(npart+strlen(npart)-3, ".ts"); char *ptr = strstr(npart + strlen(npart) - 3, ".ts");
if (ptr) if (ptr)
*ptr = 0; *ptr = 0;
@@ -279,8 +296,8 @@ void CMovieCut::findNewName(const char * fname, char * dpart, size_t dpart_len)
int CMovieCut::compare_book(const void *x, const void *y) int CMovieCut::compare_book(const void *x, const void *y)
{ {
struct mybook *px = (struct mybook*) x; struct mybook *px = (struct mybook *) x;
struct mybook *py = (struct mybook*) y; struct mybook *py = (struct mybook *) y;
int dx = px->pos / (off64_t) 1024; int dx = px->pos / (off64_t) 1024;
int dy = py->pos / (off64_t) 1024; int dy = py->pos / (off64_t) 1024;
int res = dx - dy; int res = dx - dy;
@@ -303,13 +320,13 @@ int CMovieCut::getInput()
retval |= 1; retval |= 1;
if (CNeutrinoApp::getInstance()->handleMsg(msg, data) & messages_return::cancel_all) if (CNeutrinoApp::getInstance()->handleMsg(msg, data) & messages_return::cancel_all)
retval |= 2; retval |= 2;
//printf("input: msg %d (%x) ret %d\n", msg, msg, retval); //printf("input: msg %d (%x) ret %d\n", msg, msg, retval);
return retval; return retval;
} }
bool CMovieCut::cutMovie(MI_MOVIE_INFO * minfo) bool CMovieCut::cutMovie(MI_MOVIE_INFO *minfo)
{ {
struct mybook books[MI_MOVIE_BOOK_USER_MAX+2]; struct mybook books[MI_MOVIE_BOOK_USER_MAX + 2];
unsigned char psi[PSI_SIZE]; unsigned char psi[PSI_SIZE];
char dpart[255]; char dpart[255];
int bcount = 0; int bcount = 0;
@@ -326,28 +343,32 @@ bool CMovieCut::cutMovie(MI_MOVIE_INFO * minfo)
off64_t secsize = getSecondSize(minfo); off64_t secsize = getSecondSize(minfo);
off64_t newsize = size; off64_t newsize = size;
if (minfo->bookmarks.start != 0) { if (minfo->bookmarks.start != 0)
{
books[bcount].pos = 0; books[bcount].pos = 0;
books[bcount].len = (minfo->bookmarks.start * secsize)/188 * 188; books[bcount].len = (minfo->bookmarks.start * secsize) / 188 * 188;
if (books[bcount].len > SAFE_GOP) if (books[bcount].len > SAFE_GOP)
books[bcount].len -= SAFE_GOP; books[bcount].len -= SAFE_GOP;
books[bcount].ok = 1; books[bcount].ok = 1;
printf("CMovieCut::%s: start bookmark %d at %" PRId64 " len %" PRId64 "\n", __func__, bcount, books[bcount].pos, books[bcount].len); printf("CMovieCut::%s: start bookmark %d at %" PRId64 " len %" PRId64 "\n", __func__, bcount, books[bcount].pos, books[bcount].len);
bcount++; bcount++;
} }
for (int book_nr = 0; book_nr < MI_MOVIE_BOOK_USER_MAX; book_nr++) { for (int book_nr = 0; book_nr < MI_MOVIE_BOOK_USER_MAX; book_nr++)
if (minfo->bookmarks.user[book_nr].pos != 0 && minfo->bookmarks.user[book_nr].length > 0) { {
books[bcount].pos = (minfo->bookmarks.user[book_nr].pos * secsize)/188 * 188; if (minfo->bookmarks.user[book_nr].pos != 0 && minfo->bookmarks.user[book_nr].length > 0)
books[bcount].len = (minfo->bookmarks.user[book_nr].length * secsize)/188 * 188; {
books[bcount].pos = (minfo->bookmarks.user[book_nr].pos * secsize) / 188 * 188;
books[bcount].len = (minfo->bookmarks.user[book_nr].length * secsize) / 188 * 188;
if (books[bcount].len > SAFE_GOP) if (books[bcount].len > SAFE_GOP)
books[bcount].len -= SAFE_GOP; books[bcount].len -= SAFE_GOP;
books[bcount].ok = 1; books[bcount].ok = 1;
printf("CMovieCut::%s: jump bookmark %d at %" PRId64 " len %" PRId64 " -> skip to %" PRId64 "\n", __func__, bcount, books[bcount].pos, books[bcount].len, books[bcount].pos+books[bcount].len); printf("CMovieCut::%s: jump bookmark %d at %" PRId64 " len %" PRId64 " -> skip to %" PRId64 "\n", __func__, bcount, books[bcount].pos, books[bcount].len, books[bcount].pos + books[bcount].len);
bcount++; bcount++;
} }
} }
if (minfo->bookmarks.end != 0) { if (minfo->bookmarks.end != 0)
books[bcount].pos = ((off64_t) minfo->bookmarks.end * secsize)/188 * 188; {
books[bcount].pos = ((off64_t) minfo->bookmarks.end * secsize) / 188 * 188;
books[bcount].len = size - books[bcount].pos; books[bcount].len = size - books[bcount].pos;
books[bcount].ok = 1; books[bcount].ok = 1;
printf("CMovieCut::%s: end bookmark %d at %" PRId64 "\n", __func__, bcount, books[bcount].pos); printf("CMovieCut::%s: end bookmark %d at %" PRId64 "\n", __func__, bcount, books[bcount].pos);
@@ -357,8 +378,9 @@ bool CMovieCut::cutMovie(MI_MOVIE_INFO * minfo)
if (!bcount) if (!bcount)
return false; return false;
unsigned char * buf = new unsigned char[BUF_SIZE]; unsigned char *buf = new unsigned char[BUF_SIZE];
if (buf == 0) { if (buf == 0)
{
perror("new"); perror("new");
return false; return false;
} }
@@ -366,20 +388,27 @@ bool CMovieCut::cutMovie(MI_MOVIE_INFO * minfo)
paintProgress(true); paintProgress(true);
qsort(books, bcount, sizeof(struct mybook), compare_book); qsort(books, bcount, sizeof(struct mybook), compare_book);
for (int i = 0; i < bcount; i++) { for (int i = 0; i < bcount; i++)
if (books[i].ok) { {
if (books[i].ok)
{
//printf("cut: bookmark %d at %" PRId64 " len %" PRId64 " -> skip to %" PRId64 "\n", i, books[i].pos, books[i].len, books[i].pos+books[i].len); //printf("cut: bookmark %d at %" PRId64 " len %" PRId64 " -> skip to %" PRId64 "\n", i, books[i].pos, books[i].len, books[i].pos+books[i].len);
newsize -= books[i].len; newsize -= books[i].len;
off64_t curend = books[i].pos + books[i].len; off64_t curend = books[i].pos + books[i].len;
/* check for overlapping bookmarks */ /* check for overlapping bookmarks */
for (int j = i + 1; j < bcount; j++) { for (int j = i + 1; j < bcount; j++)
if ((books[j].pos > books[i].pos) && (books[j].pos < curend)) { {
if ((books[j].pos > books[i].pos) && (books[j].pos < curend))
{
off64_t newend = books[j].pos + books[j].len; off64_t newend = books[j].pos + books[j].len;
if (newend > curend) { if (newend > curend)
{
printf("CMovieCut::%s: bad bookmark %d, position %" PRId64 " len %" PRId64 ", adjusting..\n", __func__, j, books[j].pos, books[j].len); printf("CMovieCut::%s: bad bookmark %d, position %" PRId64 " len %" PRId64 ", adjusting..\n", __func__, j, books[j].pos, books[j].len);
books[j].pos = curend; books[j].pos = curend;
books[j].len = newend - curend; books[j].len = newend - curend;
} else { }
else
{
printf("CMovieCut::%s: bad bookmark %d, position %" PRId64 " len %" PRId64 ", skipping..\n", __func__, j, books[j].pos, books[j].len); printf("CMovieCut::%s: bad bookmark %d, position %" PRId64 " len %" PRId64 ", skipping..\n", __func__, j, books[j].pos, books[j].len);
books[j].ok = 0; books[j].ok = 0;
} }
@@ -395,12 +424,14 @@ bool CMovieCut::cutMovie(MI_MOVIE_INFO * minfo)
off64_t offset = 0; off64_t offset = 0;
printf("CMovieCut::%s: new file %s, expected size %" PRId64 ", start time %s", __func__, dpart, newsize, ctime(&tt)); printf("CMovieCut::%s: new file %s, expected size %" PRId64 ", start time %s", __func__, dpart, newsize, ctime(&tt));
dstfd = open(dpart, O_CREAT|O_WRONLY|O_TRUNC| O_LARGEFILE, 0644); dstfd = open(dpart, O_CREAT | O_WRONLY | O_TRUNC | O_LARGEFILE, 0644);
if (dstfd < 0) { if (dstfd < 0)
{
perror(dpart); perror(dpart);
goto ret_err; goto ret_err;
} }
if (read_psi(minfo->file.Name.c_str(), &psi[0])) { if (read_psi(minfo->file.Name.c_str(), &psi[0]))
{
perror(minfo->file.Name.c_str()); perror(minfo->file.Name.c_str());
goto ret_err; goto ret_err;
} }
@@ -409,53 +440,64 @@ bool CMovieCut::cutMovie(MI_MOVIE_INFO * minfo)
stat64(minfo->file.Name.c_str(), &s); stat64(minfo->file.Name.c_str(), &s);
srcfd = open(minfo->file.Name.c_str(), O_RDONLY | O_LARGEFILE); srcfd = open(minfo->file.Name.c_str(), O_RDONLY | O_LARGEFILE);
if (srcfd < 0) { if (srcfd < 0)
{
perror(minfo->file.Name.c_str()); perror(minfo->file.Name.c_str());
goto ret_err; goto ret_err;
} }
lseek64(srcfd, offset, SEEK_SET); lseek64(srcfd, offset, SEEK_SET);
/* process all bookmarks */ /* process all bookmarks */
while (true) { while (true)
{
off64_t until = bpos; off64_t until = bpos;
printf("CMovieCut::%s: bookmark #%d reading from %" PRId64 " to %" PRId64 " (%" PRId64 ") want gop %d\n", __func__, bindex, offset, until, until - offset, need_gop); printf("CMovieCut::%s: bookmark #%d reading from %" PRId64 " to %" PRId64 " (%" PRId64 ") want gop %d\n", __func__, bindex, offset, until, until - offset, need_gop);
/* read up to jump end */ /* read up to jump end */
while (offset < until) { while (offset < until)
{
int msg = getInput(); int msg = getInput();
was_cancel = msg & 2; was_cancel = msg & 2;
if (msg & 4) { if (msg & 4)
{
unlink(dpart); unlink(dpart);
retval = true; retval = true;
goto ret_err; goto ret_err;
} }
size_t toread = (until-offset) > BUF_SIZE ? BUF_SIZE : until - offset; size_t toread = (until - offset) > BUF_SIZE ? BUF_SIZE : until - offset;
size_t r = read(srcfd, buf, toread); size_t r = read(srcfd, buf, toread);
if (r > 0) { if (r > 0)
{
int wptr = 0; int wptr = 0;
if (r != toread) if (r != toread)
printf("CMovieCut::%s: short read at %" PRId64 ": %d\n", __func__, offset, (int)r); printf("CMovieCut::%s: short read at %" PRId64 ": %d\n", __func__, offset, (int)r);
if (buf[0] != 0x47) if (buf[0] != 0x47)
printf("CMovieCut::%s: buffer not aligned at %" PRId64 "\n", __func__, offset); printf("CMovieCut::%s: buffer not aligned at %" PRId64 "\n", __func__, offset);
if (need_gop) { if (need_gop)
{
int gop = find_gop(buf, r); int gop = find_gop(buf, r);
if (gop >= 0) { if (gop >= 0)
printf("CMovieCut::%s: GOP found at %" PRId64 " offset %d\n", __func__, (off64_t)(offset+gop), gop); {
printf("CMovieCut::%s: GOP found at %" PRId64 " offset %d\n", __func__, (off64_t)(offset + gop), gop);
newsize -= gop; newsize -= gop;
wptr = gop; wptr = gop;
} else }
else
printf("CMovieCut::%s: GOP not found\n", __func__); printf("CMovieCut::%s: GOP not found\n", __func__);
need_gop = 0; need_gop = 0;
} }
offset += r; offset += r;
spos += r - wptr; spos += r - wptr;
percent = (int) ((float)(spos)/(float)(newsize)*100.); percent = (int)((float)(spos) / (float)(newsize) * 100.);
paintProgress(msg != 0); paintProgress(msg != 0);
size_t wr = write(dstfd, &buf[wptr], r-wptr); size_t wr = write(dstfd, &buf[wptr], r - wptr);
if (wr < (r-wptr)) { if (wr < (r - wptr))
{
perror(dpart); perror(dpart);
goto ret_err; goto ret_err;
} }
} else if (offset < s.st_size) { }
else if (offset < s.st_size)
{
/* read error ? */ /* read error ? */
perror(minfo->file.Name.c_str()); perror(minfo->file.Name.c_str());
goto ret_err; goto ret_err;
@@ -466,26 +508,30 @@ bool CMovieCut::cutMovie(MI_MOVIE_INFO * minfo)
offset = bpos + bskip; offset = bpos + bskip;
bindex++; bindex++;
while(bindex < bcount) { while (bindex < bcount)
if(books[bindex].ok) {
if (books[bindex].ok)
break; break;
else else
bindex++; bindex++;
} }
if(bindex < bcount) { if (bindex < bcount)
{
bpos = books[bindex].pos; bpos = books[bindex].pos;
bskip = books[bindex].len; bskip = books[bindex].len;
} else }
else
bpos = size; bpos = size;
if (offset >= s.st_size) { if (offset >= s.st_size)
{
printf("CMovieCut::%s: offset behind EOF: %" PRId64 " from %" PRId64 "\n", __func__, offset, s.st_size); printf("CMovieCut::%s: offset behind EOF: %" PRId64 " from %" PRId64 "\n", __func__, offset, s.st_size);
break; break;
} }
lseek64(srcfd, offset, SEEK_SET); lseek64(srcfd, offset, SEEK_SET);
} }
tt1 = time(0); tt1 = time(0);
printf("CMovieCut::%s: total written %" PRId64 " tooks %ld secs end time %s", __func__, spos, tt1-tt, ctime(&tt1)); printf("CMovieCut::%s: total written %" PRId64 " tooks %ld secs end time %s", __func__, spos, tt1 - tt, ctime(&tt1));
save_info(minfo, dpart, spos, secsize); save_info(minfo, dpart, spos, secsize);
retval = true; retval = true;
@@ -499,13 +545,13 @@ ret_err:
if (was_cancel) if (was_cancel)
g_RCInput->postMsg(CRCInput::RC_home, 0); g_RCInput->postMsg(CRCInput::RC_home, 0);
frameBuffer->paintBoxRel(x + 40, y+12, 200, 15, COL_INFOBAR_PLUS_0);//TODO: remove unneeded box paints frameBuffer->paintBoxRel(x + 40, y + 12, 200, 15, COL_INFOBAR_PLUS_0); //TODO: remove unneeded box paints
return retval; return retval;
} }
bool CMovieCut::copyMovie(MI_MOVIE_INFO * minfo, bool onefile) bool CMovieCut::copyMovie(MI_MOVIE_INFO *minfo, bool onefile)
{ {
struct mybook books[MI_MOVIE_BOOK_USER_MAX+2]; struct mybook books[MI_MOVIE_BOOK_USER_MAX + 2];
struct stat64 s; struct stat64 s;
char dpart[255]; char dpart[255];
unsigned char psi[PSI_SIZE]; unsigned char psi[PSI_SIZE];
@@ -520,12 +566,14 @@ bool CMovieCut::copyMovie(MI_MOVIE_INFO * minfo, bool onefile)
off64_t newsize = 0; off64_t newsize = 0;
off64_t secsize = getSecondSize(minfo); off64_t secsize = getSecondSize(minfo);
for (int book_nr = 0; book_nr < MI_MOVIE_BOOK_USER_MAX; book_nr++) { for (int book_nr = 0; book_nr < MI_MOVIE_BOOK_USER_MAX; book_nr++)
if (minfo->bookmarks.user[book_nr].pos != 0 && minfo->bookmarks.user[book_nr].length > 0) { {
books[bcount].pos = (minfo->bookmarks.user[book_nr].pos * secsize)/188 * 188; if (minfo->bookmarks.user[book_nr].pos != 0 && minfo->bookmarks.user[book_nr].length > 0)
{
books[bcount].pos = (minfo->bookmarks.user[book_nr].pos * secsize) / 188 * 188;
if (books[bcount].pos > SAFE_GOP) if (books[bcount].pos > SAFE_GOP)
books[bcount].pos -= SAFE_GOP; books[bcount].pos -= SAFE_GOP;
books[bcount].len = (minfo->bookmarks.user[book_nr].length * secsize)/188 * 188; books[bcount].len = (minfo->bookmarks.user[book_nr].length * secsize) / 188 * 188;
books[bcount].ok = 1; books[bcount].ok = 1;
printf("copy: jump bookmark %d at %" PRId64 " len %" PRId64 "\n", bcount, books[bcount].pos, books[bcount].len); printf("copy: jump bookmark %d at %" PRId64 " len %" PRId64 "\n", bcount, books[bcount].pos, books[bcount].len);
newsize += books[bcount].len; newsize += books[bcount].len;
@@ -536,8 +584,9 @@ bool CMovieCut::copyMovie(MI_MOVIE_INFO * minfo, bool onefile)
if (!bcount) if (!bcount)
return false; return false;
unsigned char * buf = new unsigned char[BUF_SIZE]; unsigned char *buf = new unsigned char[BUF_SIZE];
if (buf == 0) { if (buf == 0)
{
perror("new"); perror("new");
return false; return false;
} }
@@ -545,24 +594,29 @@ bool CMovieCut::copyMovie(MI_MOVIE_INFO * minfo, bool onefile)
paintProgress(true); paintProgress(true);
printf("********* %d boormarks, to %s file(s), expected size to copy %" PRId64 ", start time %s", bcount, onefile ? "one" : "many", newsize, ctime(&tt)); printf("********* %d boormarks, to %s file(s), expected size to copy %" PRId64 ", start time %s", bcount, onefile ? "one" : "many", newsize, ctime(&tt));
if (read_psi(minfo->file.Name.c_str(), &psi[0])) { if (read_psi(minfo->file.Name.c_str(), &psi[0]))
{
perror(minfo->file.Name.c_str()); perror(minfo->file.Name.c_str());
goto ret_err; goto ret_err;
} }
srcfd = open(minfo->file.Name.c_str(), O_RDONLY | O_LARGEFILE); srcfd = open(minfo->file.Name.c_str(), O_RDONLY | O_LARGEFILE);
if (srcfd < 0) { if (srcfd < 0)
{
perror(minfo->file.Name.c_str()); perror(minfo->file.Name.c_str());
goto ret_err; goto ret_err;
} }
stat64(minfo->file.Name.c_str(), &s); stat64(minfo->file.Name.c_str(), &s);
for (int i = 0; i < bcount; i++) { for (int i = 0; i < bcount; i++)
{
printf("\ncopy: processing bookmark %d at %" PRId64 " len %" PRId64 "\n", i, books[i].pos, books[i].len); printf("\ncopy: processing bookmark %d at %" PRId64 " len %" PRId64 "\n", i, books[i].pos, books[i].len);
if (!dst_done || !onefile) { if (!dst_done || !onefile)
{
findNewName(minfo->file.Name.c_str(), dpart, sizeof(dpart)); findNewName(minfo->file.Name.c_str(), dpart, sizeof(dpart));
dstfd = open(dpart, O_CREAT|O_WRONLY|O_TRUNC| O_LARGEFILE, 0644); dstfd = open(dpart, O_CREAT | O_WRONLY | O_TRUNC | O_LARGEFILE, 0644);
printf("copy: new file %s fd %d\n", dpart, dstfd); printf("copy: new file %s fd %d\n", dpart, dstfd);
if (dstfd < 0) { if (dstfd < 0)
{
printf("failed to open %s\n", dpart); printf("failed to open %s\n", dpart);
goto ret_err; goto ret_err;
} }
@@ -576,63 +630,74 @@ bool CMovieCut::copyMovie(MI_MOVIE_INFO * minfo, bool onefile)
lseek64(srcfd, offset, SEEK_SET); lseek64(srcfd, offset, SEEK_SET);
off64_t until = books[i].pos + books[i].len; off64_t until = books[i].pos + books[i].len;
printf("copy: read from %" PRId64 " to %" PRId64 " read size %d want gop %d\n", offset, until, BUF_SIZE, need_gop); printf("copy: read from %" PRId64 " to %" PRId64 " read size %d want gop %d\n", offset, until, BUF_SIZE, need_gop);
while (offset < until) { while (offset < until)
size_t toread = (until-offset) > BUF_SIZE ? BUF_SIZE : until - offset; {
size_t toread = (until - offset) > BUF_SIZE ? BUF_SIZE : until - offset;
int msg = getInput(); int msg = getInput();
was_cancel = msg & 2; was_cancel = msg & 2;
if (msg & 4) { if (msg & 4)
{
unlink(dpart); unlink(dpart);
retval = true; retval = true;
goto ret_err; goto ret_err;
} }
size_t r = read(srcfd, buf, toread); size_t r = read(srcfd, buf, toread);
if (r > 0) { if (r > 0)
{
int wptr = 0; int wptr = 0;
if (r != toread) if (r != toread)
printf("****** short read ? %d\n", (int)r); printf("****** short read ? %d\n", (int)r);
if (buf[0] != 0x47) if (buf[0] != 0x47)
printf("copy: buffer not aligned at %" PRId64 "\n", offset); printf("copy: buffer not aligned at %" PRId64 "\n", offset);
if (need_gop) { if (need_gop)
{
int gop = find_gop(buf, r); int gop = find_gop(buf, r);
if (gop >= 0) { if (gop >= 0)
printf("cut: GOP found at %" PRId64 " offset %d\n", (off64_t)(offset+gop), gop); {
printf("cut: GOP found at %" PRId64 " offset %d\n", (off64_t)(offset + gop), gop);
newsize -= gop; newsize -= gop;
wptr = gop; wptr = gop;
} else }
else
printf("cut: GOP needed, but not found\n"); printf("cut: GOP needed, but not found\n");
need_gop = 0; need_gop = 0;
} }
offset += r; offset += r;
spos += r - wptr; spos += r - wptr;
btotal += r; btotal += r;
percent = (int) ((float)(btotal)/(float)(newsize)*100.); percent = (int)((float)(btotal) / (float)(newsize) * 100.);
paintProgress(msg != 0); paintProgress(msg != 0);
size_t wr = write(dstfd, &buf[wptr], r-wptr); size_t wr = write(dstfd, &buf[wptr], r - wptr);
if (wr < (r-wptr)) { if (wr < (r - wptr))
{
printf("write to %s failed\n", dpart); printf("write to %s failed\n", dpart);
unlink(dpart); unlink(dpart);
goto ret_err; goto ret_err;
} }
} else if (offset < s.st_size) { }
else if (offset < s.st_size)
{
/* read error ? */ /* read error ? */
perror(minfo->file.Name.c_str()); perror(minfo->file.Name.c_str());
break; break;
} }
} /* while(offset < until) */ } /* while(offset < until) */
if (!onefile) { if (!onefile)
{
close(dstfd); close(dstfd);
dstfd = -1; dstfd = -1;
save_info(minfo, dpart, spos, secsize); save_info(minfo, dpart, spos, secsize);
time_t tt1 = time(0); time_t tt1 = time(0);
printf("copy: ********* %s: total written %" PRId64 " took %ld secs\n", dpart, spos, tt1-tt); printf("copy: ********* %s: total written %" PRId64 " took %ld secs\n", dpart, spos, tt1 - tt);
} }
} /* for all books */ } /* for all books */
if (onefile) { if (onefile)
{
save_info(minfo, dpart, spos, secsize); save_info(minfo, dpart, spos, secsize);
time_t tt1 = time(0); time_t tt1 = time(0);
printf("copy: ********* %s: total written %" PRId64 " took %ld secs\n", dpart, spos, tt1-tt); printf("copy: ********* %s: total written %" PRId64 " took %ld secs\n", dpart, spos, tt1 - tt);
} }
retval = true; retval = true;
ret_err: ret_err:
@@ -643,6 +708,6 @@ ret_err:
delete [] buf; delete [] buf;
if (was_cancel) if (was_cancel)
g_RCInput->postMsg(CRCInput::RC_home, 0); g_RCInput->postMsg(CRCInput::RC_home, 0);
frameBuffer->paintBoxRel(x + 40, y+12, 200, 15, COL_INFOBAR_PLUS_0);//TODO: remove unneeded box paints frameBuffer->paintBoxRel(x + 40, y + 12, 200, 15, COL_INFOBAR_PLUS_0); //TODO: remove unneeded box paints
return retval; return retval;
} }

View File

@@ -28,21 +28,21 @@ class CMovieCut
{ {
private: private:
CProgressBar *timescale; CProgressBar *timescale;
CFrameBuffer * frameBuffer; CFrameBuffer *frameBuffer;
int x; int x;
int y; int y;
int percent; int percent;
void reset_atime(const char * path, time_t tt); void reset_atime(const char *path, time_t tt);
uint32_t getHeaderDurationMS(MI_MOVIE_INFO * minfo); uint32_t getHeaderDurationMS(MI_MOVIE_INFO *minfo);
off64_t getSecondSize(MI_MOVIE_INFO * minfo); off64_t getSecondSize(MI_MOVIE_INFO *minfo);
void WriteHeader(const char * path, uint32_t duration); void WriteHeader(const char *path, uint32_t duration);
int check_pes_start (unsigned char *packet); int check_pes_start(unsigned char *packet);
int find_gop(unsigned char *buf, int r); int find_gop(unsigned char *buf, int r);
off64_t fake_read(int fd, unsigned char *buf, size_t size, off64_t fsize); off64_t fake_read(int fd, unsigned char *buf, size_t size, off64_t fsize);
int read_psi(const char * spart, unsigned char * buf); int read_psi(const char *spart, unsigned char *buf);
void save_info(MI_MOVIE_INFO * minfo, char * dpart, off64_t spos, off64_t secsize); void save_info(MI_MOVIE_INFO *minfo, char *dpart, off64_t spos, off64_t secsize);
void findNewName(const char * fname, char * dpart,size_t dpart_len); void findNewName(const char *fname, char *dpart, size_t dpart_len);
static int compare_book(const void *x, const void *y); static int compare_book(const void *x, const void *y);
int getInput(); int getInput();
@@ -51,9 +51,9 @@ class CMovieCut
public: public:
CMovieCut(); CMovieCut();
~CMovieCut(); ~CMovieCut();
bool truncateMovie(MI_MOVIE_INFO * minfo); bool truncateMovie(MI_MOVIE_INFO *minfo);
bool cutMovie(MI_MOVIE_INFO * minfo); bool cutMovie(MI_MOVIE_INFO *minfo);
bool copyMovie(MI_MOVIE_INFO * minfo, bool onefile); bool copyMovie(MI_MOVIE_INFO *minfo, bool onefile);
//int handleMsg(const neutrino_msg_t _msg, neutrino_msg_data_t data); //int handleMsg(const neutrino_msg_t _msg, neutrino_msg_data_t data);
}; };