Complete (but untested) .sup generation.

Originally committed to SVN as r1805.
This commit is contained in:
Rodrigo Braz Monteiro 2008-01-21 20:35:31 +00:00
parent f8441e703c
commit 6a811254e3
8 changed files with 116 additions and 22 deletions

View File

@ -365,7 +365,7 @@ void SubtitleFormat::ConvertTags(int format,wxString lineEnd) {
////////////////////////////////////////////
// Merge identical and/or overlapping lines
void SubtitleFormat::Merge(bool identical,bool overlaps,bool stripComments) {
void SubtitleFormat::Merge(bool identical,bool overlaps,bool stripComments,bool stripNonDialogue) {
using std::list;
list<AssEntry*>::iterator next;
list<AssEntry*>::iterator prev = Line->end();
@ -407,8 +407,10 @@ void SubtitleFormat::Merge(bool identical,bool overlaps,bool stripComments) {
// Other line, delete it
else {
delete *cur;
Line->erase(cur);
if (stripNonDialogue) {
delete *cur;
Line->erase(cur);
}
}
}
}

View File

@ -71,7 +71,7 @@ protected:
void ClearCopy();
void SortLines();
void ConvertTags(int format,wxString lineEnd);
void Merge(bool identical,bool overlaps,bool stripComments);
void Merge(bool identical,bool overlaps,bool stripComments,bool stripNonDialogue);
void Clear();
void LoadDefault(bool defline=true);

View File

@ -47,6 +47,7 @@
#ifdef _OPENMP
#include <omp.h>
#endif
#include <wx/file.h>
///////////////
@ -82,11 +83,6 @@ void DVDSubtitleFormat::GetSubPictureList(std::vector<SubPicture> &pics) {
AegiVideoFrame srcFrame = video->GetFrame(0);
delete video;
// Prepare subtitles
CreateCopy();
SortLines();
//Merge(true,true,true);
// Count and index lines
using std::list;
int count = 0;
@ -192,7 +188,7 @@ void DVDSubtitleFormat::GetSubPictureList(std::vector<SubPicture> &pics) {
}
}
// Save image
// Save image data
if (startX > endX) endX = startX;
if (startY > endY) endY = startY;
int sw = endX-startX+1;
@ -201,6 +197,8 @@ void DVDSubtitleFormat::GetSubPictureList(std::vector<SubPicture> &pics) {
pics[i].y = startY;
pics[i].w = sw;
pics[i].h = sh;
pics[i].start = current->Start.GetMS();
pics[i].end = current->End.GetMS();
// RLE to memory
for (int j=0;j<2;j++) {
@ -208,7 +206,9 @@ void DVDSubtitleFormat::GetSubPictureList(std::vector<SubPicture> &pics) {
int col;
int temp;
int len = 0;
//wxImage subPic = img.GetSubImage(wxRect(startX,startY,sw,sh));
dataRead = data + ((startY+j)*w+startX)*3;
//dataRead = subPic.GetData();
std::vector<RLEGroup> groups;
groups.reserve(1024);
@ -226,7 +226,7 @@ void DVDSubtitleFormat::GetSubPictureList(std::vector<SubPicture> &pics) {
if (col == curCol) {
len++;
if (len == 255) {
if (len) groups.push_back(RLEGroup(curCol,len,false));
groups.push_back(RLEGroup(curCol,len,false));
len = 0;
}
}
@ -257,21 +257,35 @@ void DVDSubtitleFormat::GetSubPictureList(std::vector<SubPicture> &pics) {
std::vector<unsigned char> &data = pics[i].data[j];
unsigned char last = 0;
for (size_t m=0;m<groups.size();m++) {
unsigned char len = groups[m].len;
int nibbles;
// End of line, write b000000cc
if (groups[m].eol) nibbles = 4;
else nibbles = groups[m].len >> 2;
nibble[0] = groups[m].col | ((groups[m].len & 0x3) << 2);
nibble[1] = (groups[m].len & 0x3C) >> 2;
nibble[2] = (groups[m].len & 0xC0) >> 6;
for (int n=0;n<nibbles;n++) {
else {
if (len < 4) nibbles = 1;
else if (len < 16) nibbles = 2;
else if (len < 64) nibbles = 3;
else nibbles = 4;
}
// Write nibbles
nibble[0] = groups[m].col | ((len & 0x3) << 2);
nibble[1] = (len & 0x3C) >> 2;
nibble[2] = (len & 0xC0) >> 6;
for (int n=nibbles;--n>=0;) {
wxASSERT(nibble[n] >= 0 && nibble[n] < 16);
wxASSERT(n >= 0 && n < 4);
if (!off) {
last = nibble[nibbles-n-1] << 4;
last = nibble[n] << 4;
data.push_back(last);
}
else data.back() = data.back() | last;
else data.back() = nibble[n] | last;
off = !off;
}
}
last = 0;
off = false;
data.resize(data.size());
}
}
@ -285,7 +299,84 @@ void DVDSubtitleFormat::GetSubPictureList(std::vector<SubPicture> &pics) {
///////////////////////
// Actually write them
void DVDSubtitleFormat::WriteFile(wxString filename,wxString encoding) {
// Prepare subtitles
CreateCopy();
SortLines();
Merge(true,true,true,false);
// Get subpictures
std::vector<SubPicture> pics;
GetSubPictureList(pics);
// Open file for writing
wxFile fp(filename,wxFile::write);
if (!fp.IsOpened()) throw _T("Could not open file for writing.");
// Write each subpicture
size_t pos = 0;
for (size_t i=0;i<pics.size();i++) {
// Write sup packet data
pos += fp.Write("SP",2);
wxUint32 temp = wxUINT32_SWAP_ON_BE(pics[i].start * 90);
pos += fp.Write(&temp,4);
temp = 0;
pos += fp.Write(&temp,4);
// Calculate lengths
size_t controlLen = 30;
size_t packetLen = 2 + pics[i].data[0].size() + pics[i].data[1].size() + controlLen;
size_t packetStart = pos;
// Write position of the next packet and control
unsigned short temp2;
temp2 = wxUINT16_SWAP_ON_LE(packetLen);
pos += fp.Write(&temp2,2);
temp2 = wxUINT16_SWAP_ON_LE(packetLen-controlLen);
pos += fp.Write(&temp2,2);
// Write RLE data
size_t line0pos = pos - packetStart;
pos += fp.Write(&pics[i].data[0][0],pics[i].data[0].size());
size_t line1pos = pos - packetStart;
pos += fp.Write(&pics[i].data[1][0],pics[i].data[1].size());
// Control group data
size_t comm2add = packetLen - 4;
unsigned char comm2_b1 = (comm2add & 0xFF00) >> 8;
unsigned char comm2_b2 = comm2add & 0xFF;
unsigned char pix0_b1 = (line0pos & 0xFF00) >> 8;
unsigned char pix0_b2 = line0pos & 0xFF;
unsigned char pix1_b1 = (line1pos & 0xFF00) >> 8;
unsigned char pix1_b2 = line1pos & 0xFF;
int delay = (pics[i].end - pics[i].start)/10;
unsigned char delay_b1 = (delay & 0xFF00) >> 8;
unsigned char delay_b2 = delay & 0xFF;
int sx = pics[i].x;
int sy = pics[i].y;
int ex = pics[i].w + sx;
int ey = pics[i].h + sy;
unsigned char dispx_b1 = (sx & 0xFF0) >> 4;
unsigned char dispx_b2 = ((sx & 0x0F) << 4) | ((ex & 0xF00) >> 8);
unsigned char dispx_b3 = (sx & 0xFF);
unsigned char dispy_b1 = (sy & 0xFF0) >> 4;
unsigned char dispy_b2 = ((sy & 0x0F) << 4) | ((ey & 0xF00) >> 8);
unsigned char dispy_b3 = (sy & 0xFF);
// Write control group
unsigned char control[] = {
0x00, 0x00, // Delay
comm2_b1, comm2_b2, // Next command
0x01, // Start display
0x03, 0x01, 0x23, // Set colours
0x04, 0x0F, 0xFF, // Alpha blend
0x05, dispx_b1, dispx_b2, dispx_b3, dispy_b1, dispy_b2, dispy_b3, // Display area
0x06, pix0_b1, pix0_b2, pix1_b1, pix1_b2, // Pixel pointers
0xFF, // End block 1
delay_b1, delay_b2, // Delay
comm2_b1, comm2_b2, // This command
0x02, // Stop display
0xFF // End
};
pos += fp.Write(control,controlLen);
}
}

View File

@ -49,6 +49,7 @@ struct SubPicture {
std::vector<unsigned char> data[2];
int x,y;
int w,h;
int start,end;
};
struct RLEGroup {

View File

@ -77,7 +77,7 @@ void EncoreSubtitleFormat::WriteFile(wxString _filename,wxString encoding) {
// Convert to encore
CreateCopy();
SortLines();
Merge(true,true,true);
Merge(true,true,true,false);
ConvertTags(1,_T("\r\n"));
// Write lines

View File

@ -178,7 +178,7 @@ void MicroDVDSubtitleFormat::WriteFile(wxString filename,wxString encoding) {
// Convert file
CreateCopy();
SortLines();
Merge(true,true,true);
Merge(true,true,true,false);
ConvertTags(1,_T("|"));
// Open file

View File

@ -182,7 +182,7 @@ void SRTSubtitleFormat::WriteFile(wxString _filename,wxString encoding) {
// Convert to SRT
CreateCopy();
SortLines();
Merge(true,true,true);
Merge(true,true,true,false);
ConvertTags(2,_T("\r\n"));
// Write lines

View File

@ -322,7 +322,7 @@ void TTXTSubtitleFormat::WriteLine(wxXmlNode *root, AssDialogue *line) {
void TTXTSubtitleFormat::ConvertToTTXT () {
// Convert
SortLines();
Merge(true,true,true);
Merge(true,true,true,false);
ConvertTags(1,_T("\r\n"));
// Find last line