forked from samiamwork/Movist
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMSubtitleParser_SUB.m
213 lines (190 loc) · 6.9 KB
/
MSubtitleParser_SUB.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
//
// Movist
//
// Copyright 2006 ~ 2008 Yong-Hoe Kim. All rights reserved.
// Yong-Hoe Kim <[email protected]>
//
// This file is part of Movist.
//
// Movist is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.
//
// Movist is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
#import "MSubtitleParser_SUB.h"
#import "vobsub.h"
#import "spudec.h"
@implementation MSubtitleParser_SUB
- (id)initWithURL:(NSURL*)subtitleURL
{
if (self = [super initWithURL:subtitleURL]) {
_subtitles = [[NSMutableArray alloc] initWithCapacity:2];
_tracks = [[NSMutableDictionary alloc] initWithCapacity:2];
}
return self;
}
- (void)dealloc
{
int i, count = [_subtitles count];
for (i = 0; i < count; i++) {
if (_spudec[i]) {
spudec_free(_spudec[i]);
}
}
if (_vobsub) {
vobsub_close(_vobsub);
_vobsub = 0;
}
[_tracks release];
[_subtitles release];
[super dealloc];
}
- (NSImage*)imageWithSpudec:(spudec_handle_t*)spudec
{
int x, y, width;
int sx = spudec->width - 1, ex = 0;
unsigned char* pa = spudec->aimage;
for (y = 0; y < spudec->height; y++) {
for (x = 0; x < spudec->width; x++) {
if (pa[x] && x < sx) {
sx = x;
}
}
for (x = spudec->width - 1; 0 <= x; x--) {
if (pa[x] && ex < x) {
ex = x;
}
}
pa += spudec->stride;
}
width = ex - sx + 1;
NSBitmapImageRep* bmp;
bmp = [[NSBitmapImageRep alloc]
initWithBitmapDataPlanes:0
pixelsWide:width pixelsHigh:spudec->height
bitsPerSample:8 samplesPerPixel:4 hasAlpha:TRUE
isPlanar:FALSE colorSpaceName:NSCalibratedRGBColorSpace
bitmapFormat:0 bytesPerRow:width * 4 bitsPerPixel:32];
pa = spudec->aimage;
unsigned char* pi = spudec->image;
unsigned char* pb = [bmp bitmapData];
for (y = 0; y < spudec->height; y++) {
for (x = sx; x <= ex; x++) {
*pb++ = pi[x]; // red
*pb++ = pi[x]; // green
*pb++ = pi[x]; // blue
*pb++ = (pa[x]) ? 255 : 0; // alpha: 255 is opaque
}
pi += spudec->stride;
pa += spudec->stride;
}
NSImage* image = [[NSImage alloc] initWithSize:[bmp size]];
[image addRepresentation:bmp];
[image setCacheMode:NSImageCacheNever];
[image setCachedSeparately:TRUE]; // for thread safety
[bmp release];
return [image autorelease];
}
- (void)parseSubtitle:(MSubtitle*)subtitle atIndex:(int)index
{
TRACE(@"subtitle[%d] parse... (0x%x)", index, _spudec[index]);
vobsub_init_spudec(_vobsub, index);
NSAutoreleasePool* pool;
int size, pts100;
spudec_packet_t* packet;
spudec_handle_t* spudec = (spudec_handle_t*)_spudec[index];
while (0 <= (size = vobsub_get_next_packet(_vobsub, index, (void*)&packet, &pts100))) {
spudec_assemble(spudec, (unsigned char*)packet, size, pts100);
while (spudec->queue_head) {
spudec_heartbeat(spudec, spudec->queue_head->start_pts);
if (spudec->end_pts == UINT_MAX) {
spudec->end_pts = spudec->start_pts + (2 * 90 * 1000);
}
if (spudec_changed(spudec)) {
if (0 < spudec->width && 0 < spudec->height) {
pool = [[NSAutoreleasePool alloc] init];
[subtitle addImage:[self imageWithSpudec:spudec]
beginTime:spudec->start_pts / 90 / 1000.f
endTime:spudec->end_pts / 90 / 1000.f];
[pool release];
}
if (spudec->start_pts <= spudec->now_pts &&
spudec->now_pts < spudec->end_pts && spudec->image) {
spudec->spu_changed = 0;
}
}
}
}
spudec_reset(_spudec[index]);
TRACE(@"subtitle[%d] parse...done", index);
}
- (void)parseThread:(id)object
{
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
int index = [(NSNumber*)object intValue];
MSubtitle* subtitle = [_subtitles objectAtIndex:index];
[self parseSubtitle:subtitle atIndex:index];
[pool release];
}
- (NSArray*)parseWithOptions:(NSDictionary*)options error:(NSError**)error
{
NSString* path = [[_subtitleURL path] stringByDeletingPathExtension];
_vobsub = vobsub_open([path UTF8String], 0, 0, &_spudec[0]);
if (!_vobsub) {
return nil;
}
const char* s;
MSubtitle* subtitle;
int i, count = vobsub_get_indexes_count(_vobsub);
for (i = 0; i < count; i++) {
if (0 < i) { // _spudec[0] is already init in vobsub_open().
_spudec[i] = calloc(1, sizeof(spudec_handle_t));
memcpy(_spudec[i], _spudec[0], sizeof(spudec_handle_t));
}
s = vobsub_get_id(_vobsub, i);
subtitle = [[[MSubtitle alloc] initWithURL:_subtitleURL] autorelease];
[subtitle setType:@"VOBSUB"];
[subtitle setName:(!s) ? NSLocalizedString(@"Unnamed", 0) :
[NSString stringWithCString:s encoding:NSASCIIStringEncoding]];
[subtitle setTrackName:NSLocalizedString(@"External Subtitle", nil)];
[subtitle setEmbedded:FALSE];
[_subtitles addObject:subtitle];
[NSThread detachNewThreadSelector:@selector(parseThread:) toTarget:self
withObject:[NSNumber numberWithInt:i]];
}
// _spudec, _vobsub will be released in -dealloc.
return _subtitles;
}
/*
// this is called by MSubtitleParser_MKV for each subtitle-idx.
- (void)mkvTrackNumber:(int)trackNumber parseIdx:(const char*)s
{
int index = [_tracks count];
if (!_vobsub) {
_vobsub = vobsub_make(s, &_spudec[0]);
}
if (0 < index) {
_spudec[index] = calloc(1, sizeof(spudec_handle_t));
memcpy(_spudec[index], _spudec[0], sizeof(spudec_handle_t));
}
[_tracks setObject:[NSNumber numberWithInt:index]
forKey:[NSNumber numberWithInt:trackNumber]];
//TRACE(@"idx: %d => %d", trackNumber, index);
}
// this is called by MSubtitleParser_MKV for each subtitle-image-item.
- (void)mkvTrackNumber:(int)trackNumber
parseSubtitleImage:(unsigned char*)data size:(int)dataSize time:(float)time
{
int index = [[_tracks objectForKey:[NSNumber numberWithInt:trackNumber]] intValue];
vobsub_add_sub(_vobsub, index, data, dataSize, (int)(time * 90 * 1000));
}
*/
@end