Changeset 1000
- Timestamp:
- 12/19/08 19:27:50 (1 year ago)
- Files:
-
- trunk/Subtitles/SubImport.mm (modified) (11 diffs)
- trunk/VobSubCodec.c (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/Subtitles/SubImport.mm
r982 r1000 18 18 19 19 extern "C" { 20 void ExtractVobSubPacket(UInt8 *dest, const UInt8 *framedSrc, int srcSize);20 int ExtractVobSubPacket(UInt8 *dest, const UInt8 *framedSrc, int srcSize, int *usedSrcBytes); 21 21 } 22 22 … … 753 753 { 754 754 uint8_t *controlSeq = packet + controlOffset; 755 uint32_t i = 4, end = length - controlOffset; 755 int32_t i = 4; 756 int32_t end = length - controlOffset; 756 757 uint16_t timestamp = (controlSeq[0] << 8) | controlSeq[1]; 757 758 uint16_t nextOffset = (controlSeq[2] << 8) + controlSeq[3]; … … 799 800 loop = false; 800 801 controlOffset = nextOffset; 801 i = end;802 i = 0x7fffffff; 802 803 break; 803 804 … … 808 809 } 809 810 } 811 if(i != 0x7fffffff) 812 { 813 //End of packet 814 loop = false; 815 } 810 816 } 811 817 } … … 830 836 831 837 VobSubState state = VOB_SUB_STATE_READING_PRIVATE; 832 VobSubTrack *currentTrack = nil, *lastTrack = nil; 833 long movieDuration = (GetMovieDuration(theMovie) * 1000 / GetMovieTimeScale(theMovie)); 838 VobSubTrack *currentTrack = nil; 834 839 int imageWidth = 0, imageHeight = 0; 835 840 long delay=0; … … 841 846 if(![idxContent length]) 842 847 goto bail; 848 849 int subFileSize = [[[[NSFileManager defaultManager] fileAttributesAtPath:subFileName traverseLink:NO] objectForKey:NSFileSize] intValue]; 843 850 844 851 NSArray *lines = [idxContent componentsSeparatedByString:@"\n"]; … … 889 896 NSString *language = [NSString stringWithUTF8String:langStr]; 890 897 891 lastTrack = currentTrack;892 898 currentTrack = [[VobSubTrack alloc] initWithPrivateData:privateData language:language andIndex:index]; 893 899 [tracks addObject:currentTrack]; … … 905 911 long time = scanTime(timeStr); 906 912 free(timeStr); 907 if(lastTrack)908 {909 [lastTrack addSampleTime:movieDuration offset:position];910 lastTrack = nil;911 }912 913 [currentTrack addSampleTime:time + delay offset:position]; 913 914 } … … 915 916 } 916 917 } 917 if(lastTrack) 918 { 919 int position = [[[[NSFileManager defaultManager] fileAttributesAtPath:subFileName traverseLink:NO] objectForKey:NSFileSize] intValue]; 920 [lastTrack addSampleTime:movieDuration offset:position]; 921 } 922 918 923 919 if([tracks count]) 924 920 { … … 947 943 } 948 944 949 int sampleCount = [track->samples count] - 1;945 int sampleCount = [track->samples count]; 950 946 int totalSamples = sampleCount; 951 947 SampleReference64Ptr samples = (SampleReference64Ptr)calloc(sampleCount*2, sizeof(SampleReference64Record)); 952 948 SampleReference64Ptr sample = samples; 953 VobSubSample *lastSample = [track->samples objectAtIndex:0];954 949 int i; 955 950 uint32_t lastTime = 0; 956 951 for(i=0; i<sampleCount; i++) 957 952 { 958 VobSubSample *nextSample = [track->samples objectAtIndex:i+1]; 959 int offset = lastSample->fileOffset; 960 int size = nextSample->fileOffset - offset; 953 VobSubSample *currentSample = [track->samples objectAtIndex:i]; 954 int offset = currentSample->fileOffset; 955 int nextOffset; 956 if(i == sampleCount - 1) 957 nextOffset = subFileSize; 958 else 959 nextOffset = ((VobSubSample *)[track->samples objectAtIndex:i+1])->fileOffset; 960 int size = nextOffset - offset; 961 if(size < 0) 962 //Skip samples for which we cannot determine size 963 continue; 961 964 962 965 NSData *subData = [subFileData subdataWithRange:NSMakeRange(offset, size)]; 963 966 uint8_t *extracted = (uint8_t *)malloc(size); 964 ExtractVobSubPacket(extracted, (const UInt8 *)[subData bytes],size);967 int extractedSize = ExtractVobSubPacket(extracted, (const UInt8 *)[subData bytes], size, &size); 965 968 966 969 uint16_t startTimestamp, endTimestamp; 967 ReadPacketTimes(extracted, size, &startTimestamp, &endTimestamp);970 ReadPacketTimes(extracted, extractedSize, &startTimestamp, &endTimestamp); 968 971 free(extracted); 969 uint32_t startTime = lastSample->timeStamp + startTimestamp; 970 uint32_t endTime = lastSample->timeStamp + endTimestamp; 972 uint32_t startTime = currentSample->timeStamp + startTimestamp; 973 uint32_t endTime = currentSample->timeStamp + endTimestamp; 974 int duration = endTimestamp - startTimestamp; 975 if(duration <= 0) 976 //Skip samples which are broken 977 continue; 971 978 if(i == 0) 972 lastSample->timeStamp = startTime;979 currentSample->timeStamp = startTime; 973 980 else if(lastTime != startTime) 974 981 { … … 984 991 sample->dataOffset.hi = 0; 985 992 sample->dataOffset.lo = offset; 986 sample->dataSize = nextSample->fileOffset - lastSample->fileOffset;993 sample->dataSize = size; 987 994 sample->sampleFlags = 0; 988 sample->durationPerSample = endTimestamp - startTimestamp;995 sample->durationPerSample = duration; 989 996 sample->numberOfSamples = 1; 990 997 lastTime = endTime; 991 998 992 lastSample = nextSample;993 999 sample++; 994 1000 } trunk/VobSubCodec.c
r982 r1000 62 62 63 63 // dest must be at least as large as src 64 void ExtractVobSubPacket(UInt8 *dest, UInt8 *framedSrc, int srcSize);64 int ExtractVobSubPacket(UInt8 *dest, UInt8 *framedSrc, int srcSize, int *usedSrcBytes); 65 65 static ComponentResult ReadPacketControls(UInt8 *packet, UInt32 palette[16], PacketControlData *controlDataOut); 66 66 extern void initLib(); … … 308 308 } else if (data[0] + data[1] == 0) { 309 309 // remove the MPEG framing 310 ExtractVobSubPacket(glob->codecData, data, myDrp->bufferSize);310 myDrp->bufferSize = ExtractVobSubPacket(glob->codecData, data, myDrp->bufferSize, NULL); 311 311 } else { 312 312 memcpy(glob->codecData, drp->codecData, myDrp->bufferSize); … … 414 414 } 415 415 416 void ExtractVobSubPacket(UInt8 *dest, UInt8 *framedSrc, int srcSize) {416 int ExtractVobSubPacket(UInt8 *dest, UInt8 *framedSrc, int srcSize, int *usedSrcBytes) { 417 417 int copiedBytes = 0; 418 418 UInt8 *currentPacket = framedSrc; 419 420 while (currentPacket - framedSrc < srcSize) { 419 int packetSize = 0x7fffffff; 420 421 while (currentPacket - framedSrc < srcSize && copiedBytes < packetSize) { 421 422 // 3-byte start code: 0x00 00 01 422 423 if (currentPacket[0] + currentPacket[1] != 0 || currentPacket[2] != 1) { 423 424 Codecprintf(NULL, "VobSub Codec: !! Unknown header: %02x %02x %02x\n", currentPacket[0], currentPacket[1], currentPacket[2]); 424 return ;425 return copiedBytes; 425 426 } 426 427 … … 461 462 packet_length - 1 - (header_data_length + 3)); 462 463 464 if(packetSize == 0x7fffffff) 465 { 466 packetSize = dest[0] << 8 | dest[1]; 467 } 463 468 copiedBytes += packet_length - 1 - (header_data_length + 3); 464 469 currentPacket += packet_length + 6; … … 468 473 // unknown packet, probably video, return for now 469 474 Codecprintf(NULL, "VobSubCodec - Unknown packet type %x, aborting\n", (int)currentPacket[3]); 470 return ;475 return copiedBytes; 471 476 } // switch (currentPacket[3]) 472 477 } // while (currentPacket - framedSrc < srcSize) 478 if(usedSrcBytes != NULL) 479 *usedSrcBytes = currentPacket - framedSrc; 480 481 return copiedBytes; 473 482 } 474 483
