/* * MP42Utilities.c * Subler * * Created by Damiano Galassi on 30/01/09. * Copyright 2009 Damiano Galassi. All rights reserved. * */ #import "MP42PrivateUtilities.h" #import #import #import #include #include #import "MP42Languages.h" #import "MP42MediaFormat.h" #include "intreadwrite.h" #include "avcodec.h" NSString * SRTStringFromTime( long long time, long timeScale , const char separator) { NSString *SRT_string; int hour, minute, second, msecond; long long result; result = time / timeScale; // second msecond = (time % timeScale) / (timeScale / 1000.0f); second = result % 60; result = result / 60; // minute minute = result % 60; result = result / 60; // hour hour = result % 24; SRT_string = [NSString stringWithFormat:@"%02d:%02d:%02d%c%03d", hour, minute, second, separator, msecond]; // h:mm:ss:fff return SRT_string; } int MP4SetTrackEnabled(MP4FileHandle fileHandle, MP4TrackId trackId) { return MP4SetTrackIntegerProperty(fileHandle, trackId, "tkhd.flags", (TRACK_ENABLED | TRACK_IN_MOVIE)); } int MP4SetTrackDisabled(MP4FileHandle fileHandle, MP4TrackId trackId) { return MP4SetTrackIntegerProperty(fileHandle, trackId, "tkhd.flags", (TRACK_DISABLED | TRACK_IN_MOVIE)); } int updateTracksCount(MP4FileHandle fileHandle) { MP4TrackId maxTrackId = 0; unsigned int i; for (i = 0; i< MP4GetNumberOfTracks( fileHandle, 0, 0); i++ ) if (MP4FindTrackId(fileHandle, i, 0, 0) > maxTrackId) maxTrackId = MP4FindTrackId(fileHandle, i, 0, 0); return MP4SetIntegerProperty(fileHandle, "moov.mvhd.nextTrackId", maxTrackId + 1); } void updateMoovDuration(MP4FileHandle fileHandle) { MP4TrackId trackId = 0; MP4Duration maxTrackDuration = 0, trackDuration = 0; unsigned int i; for (i = 0; i< MP4GetNumberOfTracks( fileHandle, 0, 0); i++ ) { trackId = MP4FindTrackId(fileHandle, i, 0, 0); MP4GetTrackIntegerProperty(fileHandle, trackId, "tkhd.duration", &trackDuration); if (maxTrackDuration < trackDuration) maxTrackDuration = trackDuration; } MP4SetIntegerProperty(fileHandle, "moov.mvhd.duration", maxTrackDuration); } uint64_t getTrackSize(MP4FileHandle fileHandle, MP4TrackId trackId) { uint64_t i, sampleNum, dataLength; i = 1; sampleNum = MP4GetTrackNumberOfSamples(fileHandle, trackId); dataLength = 0; while (i <= sampleNum) { dataLength += MP4GetSampleSize(fileHandle, trackId, i); i++; } return dataLength; } MP4TrackId findChapterTrackId(MP4FileHandle fileHandle) { MP4TrackId trackId = 0; uint64_t trackRef; unsigned int i; for (i = 0; i< MP4GetNumberOfTracks( fileHandle, 0, 0); i++ ) { trackId = MP4FindTrackId(fileHandle, i, 0, 0); if (MP4HaveTrackAtom(fileHandle, trackId, "tref.chap")) { MP4GetTrackIntegerProperty(fileHandle, trackId, "tref.chap.entries.trackId", &trackRef); if (trackRef > 0) return (MP4TrackId) trackRef; } } return 0; } MP4TrackId findChapterPreviewTrackId(MP4FileHandle fileHandle) { MP4TrackId trackId = 0; uint64_t trackRef = 0; unsigned int i; for (i = 0; i< MP4GetNumberOfTracks( fileHandle, 0, 0); i++ ) { trackId = MP4FindTrackId(fileHandle, i, 0, 0); if (MP4HaveTrackAtom(fileHandle, trackId, "tref.chap")) { uint64_t entryCount = 0; MP4GetTrackIntegerProperty(fileHandle, trackId, "tref.chap.entryCount", &entryCount); if (entryCount > 1 && MP4GetTrackIntegerProperty(fileHandle, trackId, "tref.chap.entries[1].trackId", &trackRef)) if (trackRef > 0) return (MP4TrackId) trackRef; } } return 0; } void removeAllChapterTrackReferences(MP4FileHandle fileHandle) { MP4TrackId trackId = 0; unsigned int i; for (i = 0; i< MP4GetNumberOfTracks( fileHandle, 0, 0); i++ ) { trackId = MP4FindTrackId(fileHandle, i, 0, 0); if (MP4HaveTrackAtom(fileHandle, trackId, "tref.chap")) { MP4RemoveAllTrackReferences(fileHandle, "tref.chap", trackId); } } return; } MP4TrackId findFirstVideoTrack(MP4FileHandle fileHandle) { MP4TrackId videoTrack = 0; int i, trackNumber = MP4GetNumberOfTracks( fileHandle, 0, 0); if (!trackNumber) return 0; for (i = 0; i < trackNumber; i++) { videoTrack = MP4FindTrackId(fileHandle, i, 0, 0); const char *trackType = MP4GetTrackType(fileHandle, videoTrack); if (trackType) if (!strcmp(trackType, MP4_VIDEO_TRACK_TYPE)) return videoTrack; } return 0; } uint16_t getFixedVideoWidth(MP4FileHandle fileHandle, MP4TrackId Id) { uint16_t videoWidth = MP4GetTrackVideoWidth(fileHandle, Id); if (MP4HaveTrackAtom(fileHandle, Id, "mdia.minf.stbl.stsd.*.pasp")) { uint64_t hSpacing, vSpacing; MP4GetTrackIntegerProperty(fileHandle, Id, "mdia.minf.stbl.stsd.*.pasp.hSpacing", &hSpacing); MP4GetTrackIntegerProperty(fileHandle, Id, "mdia.minf.stbl.stsd.*.pasp.vSpacing", &vSpacing); if( hSpacing > 0 && vSpacing > 0) return (uint16_t) (videoWidth / (float) vSpacing * (float) hSpacing); else return videoWidth; } return videoWidth; } NSString * getTrackName(MP4FileHandle fileHandle, MP4TrackId Id) { char *trackName; if (MP4GetTrackName(fileHandle, Id, &trackName)) { NSString *name = @(trackName); free(trackName); return name; } return nil; } FourCharCode getTrackMediaType(MP4FileHandle fileHandle, MP4TrackId Id) { const char *type = MP4GetTrackType(fileHandle, Id); if (type && strlen(type) == 4) { FourCharCode code = Str2FourCC(type); return code; } return kMP42MediaType_Unknown; } FourCharCode getTrackMediaSubType(MP4FileHandle fileHandle, MP4TrackId Id) { const char *type = MP4GetTrackType(fileHandle, Id); const char *dataName = MP4GetTrackMediaDataName(fileHandle, Id); if (dataName && strlen(dataName) == 4) { if (!strcmp(dataName, "avc1")) { return kMP42VideoCodecType_H264; } else if (!strcmp(dataName, "hvc1")) { return kMP42VideoCodecType_HEVC; } else if (!strcmp(dataName, "hev1")) { return kMP42VideoCodecType_HEVC_PSinBitstream; } else if (!strcmp(dataName, "mp4a")) { uint8_t audiotype = MP4GetTrackEsdsObjectTypeId(fileHandle, Id); if (audiotype == MP4_MPEG4_AUDIO_TYPE) return kMP42AudioCodecType_MPEG4AAC; else if (audiotype == MP4_MPEG2_AUDIO_TYPE) return kMP42AudioCodecType_MPEGLayer3; else if (audiotype == 0xA9) return kMP42AudioCodecType_DTS; } else if (!strcmp(dataName, "alac")) return kMP42AudioCodecType_AppleLossless; else if (!strcmp(dataName, "ac-3")) return kMP42AudioCodecType_AC3; else if (!strcmp(dataName, "ec-3")) return kMP42AudioCodecType_EnhancedAC3; else if (!strcmp(dataName, "mp4v")) return kMP42VideoCodecType_MPEG4Video; else if (!strcmp(dataName, "text")) return kMP42SubtitleCodecType_Text; else if (!strcmp(dataName, "tx3g")) return kMP42SubtitleCodecType_3GText; else if (!strcmp(dataName, "wvtt")) return kMP42SubtitleCodecType_WebVTT; else if (!strcmp(dataName, "c608")) return kMP42ClosedCaptionCodecType_CEA608; else if (!strcmp(dataName, "c708")) return kMP42ClosedCaptionCodecType_CEA708; else if (!strcmp(dataName, "samr")) return kMP42AudioCodecType_AMR; else if (!strcmp(dataName, "jpeg")) return kMP42VideoCodecType_JPEG; else if (!strcmp(dataName, "rtp ")) return 'rtp '; else if (!strcmp(dataName, "drms")) return kMP42AudioCodecType_FairPlay; else if (!strcmp(dataName, "drmi")) return kMP42VideoCodecType_FairPlay; else if (!strcmp(dataName, "p608")) return kMP42ClosedCaptionCodecType_FairPlay; else if (!strcmp(dataName, "tmcd")) return kMP42TimeCodeFormatType_TimeCode32; else if (!strcmp(dataName, "mp4s") && !strcmp(type, "subp")) return kMP42SubtitleCodecType_VobSub; else { FourCharCode code = Str2FourCC(dataName); return code; } } return kMP42MediaType_Unknown; } NSString * getTrackLanguage(MP4FileHandle fileHandle, MP4TrackId Id) { char lang[4] = ""; MP4GetTrackLanguage(fileHandle, Id, lang); return @(lang); } // if the subtitle filename is something like title.en.srt or movie.fre.srt // this function detects it and returns the subtitle language NSString * getFilenameLanguage(CFStringRef filename) { CFRange findResult; CFStringRef baseName = NULL; CFStringRef langStr = NULL; NSString *lang = @"en"; // find and strip the extension findResult = CFStringFind(filename, CFSTR("."), kCFCompareBackwards); findResult.length = findResult.location; findResult.location = 0; baseName = CFStringCreateWithSubstring(NULL, filename, findResult); // then find the previous period findResult = CFStringFind(baseName, CFSTR("."), kCFCompareBackwards); findResult.location++; findResult.length = CFStringGetLength(baseName) - findResult.location; // check for 3 char language code if (findResult.length == 3) { char langCStr[4] = ""; langStr = CFStringCreateWithSubstring(NULL, baseName, findResult); CFStringGetCString(langStr, langCStr, 4, kCFStringEncodingASCII); lang = [MP42Languages.defaultManager extendedTagForISO_639_2:@(langCStr)]; CFRelease(langStr); // and for a 2 char language code } else if (findResult.length == 2) { char langCStr[3] = ""; langStr = CFStringCreateWithSubstring(NULL, baseName, findResult); CFStringGetCString(langStr, langCStr, 3, kCFStringEncodingASCII); lang = [MP42Languages.defaultManager extendedTagForISO_639_1:@(langCStr)]; CFRelease(langStr); } else if (findResult.length) { char langCStr[40] = ""; langStr = CFStringCreateWithSubstring(NULL, baseName, findResult); CFStringGetCString(langStr, langCStr, 40, kCFStringEncodingASCII); lang = [MP42Languages.defaultManager extendedTagForLang:@(langCStr)]; CFRelease(langStr); } CFRelease(baseName); return lang; } /* write the data to the target adress & then return a pointer which points after the written data */ uint8_t *write_data(uint8_t *target, uint8_t* data, int32_t data_size) { if(data_size > 0) memcpy(target, data, data_size); return (target + data_size); } /* write_data() */ /* write the int32_t data to target & then return a pointer which points after that data */ uint8_t *write_int32(uint8_t *target, int32_t data) { return write_data(target, (uint8_t*)&data, sizeof(data)); } /* write_int32() */ /* write the int16_t data to target & then return a pointer which points after that data */ uint8_t *write_int16(uint8_t *target, int16_t data) { return write_data(target, (uint8_t*)&data, sizeof(data)); } /* write_int16() */ #define MP4ESDescrTag 0x03 #define MP4DecConfigDescrTag 0x04 #define MP4DecSpecificDescrTag 0x05 // from perian // based off of mov_mp4_read_descr_len from mov.c in ffmpeg's libavformat static int readDescrLen(UInt8 **buffer) { int len = 0; int count = 4; while (count--) { int c = *(*buffer)++; len = (len << 7) | (c & 0x7f); if (!(c & 0x80)) break; } return len; } // based off of mov_mp4_read_descr from mov.c in ffmpeg's libavformat static int readDescr(UInt8 **buffer, int *tag) { *tag = *(*buffer)++; return readDescrLen(buffer); } // based off of mov_read_esds from mov.c in ffmpeg's libavformat ComponentResult ReadESDSDescExt(void* descExt, UInt8 **buffer, int *size, int versionFlags) { UInt8 *esds = (UInt8 *) descExt; int tag, len; *size = 0; if (versionFlags) esds += 4; // version + flags readDescr(&esds, &tag); esds += 2; // ID if (tag == MP4ESDescrTag) esds++; // priority readDescr(&esds, &tag); if (tag == MP4DecConfigDescrTag) { esds++; // object type id esds++; // stream type esds += 3; // buffer size db esds += 4; // max bitrate esds += 4; // average bitrate len = readDescr(&esds, &tag); if (tag == MP4DecSpecificDescrTag) { *buffer = calloc(1, len + 8); if (*buffer) { memcpy(*buffer, esds, len); *size = len; } } } return noErr; } // the esds atom creation is based off of the routines for it in ffmpeg's movenc.c static unsigned int descrLength(unsigned int len) { int i; for(i=1; len>>(7*i); i++); return len + 1 + i; } static uint8_t* putDescr(uint8_t *buffer, int tag, unsigned int size) { int i= descrLength(size) - size - 2; *buffer++ = tag; for(; i>0; i--) *buffer++ = (size>>(7*i)) | 0x80; *buffer++ = size & 0x7F; return buffer; } // ESDS layout: // + version (4 bytes) // + ES descriptor // + Track ID (2 bytes) // + Flags (1 byte) // + DecoderConfig descriptor // + Object Type (1 byte) // + Stream Type (1 byte) // + Buffersize DB (3 bytes) // + Max bitrate (4 bytes) // + VBR/Avg bitrate (4 bytes) // + DecoderSpecific info descriptor // + codecPrivate (codecPrivate->GetSize()) // + SL descriptor // + dunno (1 byte) uint8_t *CreateEsdsFromSetupData(uint8_t *codecPrivate, size_t vosLen, size_t *esdsLen, int trackID, bool audio, bool write_version) { int decoderSpecificInfoLen = vosLen ? descrLength(vosLen) : 0; int versionLen = write_version ? 4 : 0; *esdsLen = versionLen + descrLength(3 + descrLength(13 + decoderSpecificInfoLen) + descrLength(1)); uint8_t *esds = (uint8_t*)malloc(*esdsLen); UInt8 *pos = (UInt8 *) esds; // esds atom version (only needed for ImageDescription extension) if (write_version) pos = write_int32(pos, 0); // ES Descriptor pos = putDescr(pos, 0x03, 3 + descrLength(13 + decoderSpecificInfoLen) + descrLength(1)); pos = write_int16(pos, EndianS16_NtoB(trackID)); *pos++ = 0; // no flags // DecoderConfig descriptor pos = putDescr(pos, 0x04, 13 + decoderSpecificInfoLen); // Object type indication, see http://gpac.sourceforge.net/tutorial/mediatypes.htm if (audio) *pos++ = 0x40; // aac else *pos++ = 0x20; // mpeg4 part 2 // streamtype if (audio) *pos++ = 0x15; else *pos++ = 0x11; // 3 bytes: buffersize DB (not sure how to get easily) *pos++ = 0; pos = write_int16(pos, 0); // max bitrate, not sure how to get easily pos = write_int32(pos, 0); // vbr pos = write_int32(pos, 0); if (vosLen) { pos = putDescr(pos, 0x05, vosLen); pos = write_data(pos, codecPrivate, vosLen); } // SL descriptor pos = putDescr(pos, 0x06, 1); *pos++ = 0x02; return esds; } #pragma mark - NSTimeInterval getTrackStartOffset(MP4FileHandle fileHandle, MP4TrackId Id) { NSTimeInterval offset = 0; uint32_t i = 1, trackEditCount = MP4GetTrackNumberOfEdits(fileHandle, Id); while (i <= trackEditCount) { MP4Duration editDuration = MP4GetTrackEditDuration(fileHandle, Id, i); MP4Timestamp editMediaTime = MP4GetTrackEditMediaStart(fileHandle, Id, i); //int8_t editMediaRate = MP4GetTrackEditDwell(fileHandle, Id, i); uint64_t editListVersion = 0; MP4GetTrackIntegerProperty(fileHandle, Id, "edts.elst.version", &editListVersion); if (editListVersion == 0 && editMediaTime == ((uint32_t)-1)) offset += MP4ConvertFromMovieDuration(fileHandle, editDuration, MP4_NANOSECONDS_TIME_SCALE); else if (editListVersion == 1 && editMediaTime == ((uint64_t)-1)) offset += MP4ConvertFromMovieDuration(fileHandle, editDuration, MP4_NANOSECONDS_TIME_SCALE); else if (i == 1) offset -= MP4ConvertFromTrackDuration(fileHandle, Id, editMediaTime, MP4_NANOSECONDS_TIME_SCALE); //NSLog(@"Track %d, Media Time = %lld, Segment Duration: %qu Media Rate:%d", Id, editMediaTime, editDuration, editMediaRate); i++; } //NSLog(@"Track %d offset: %d ms", Id, offset); return offset / 1000000; } MP4Duration getTrackDuration(MP4FileHandle fileHandle, MP4TrackId trackId) { uint32_t trackEditsCount = MP4GetTrackNumberOfEdits(fileHandle, trackId); MP4Duration duration = 0; uint32_t i = 1; while (i <= trackEditsCount) { duration += MP4GetTrackEditDuration(fileHandle, trackId, i); i++; } if (duration == 0) { duration = MP4ConvertFromTrackDuration(fileHandle, trackId, MP4GetTrackDuration(fileHandle, trackId), MP4GetTimeScale(fileHandle)); } return duration; } void setTrackStartOffset(MP4FileHandle fileHandle, MP4TrackId Id, NSTimeInterval offset) { uint32_t trackEditsCount = MP4GetTrackNumberOfEdits(fileHandle, Id); CMTime time = CMTimeMake(offset * 1000000, MP4_NANOSECONDS_TIME_SCALE); CMTime trackTime = CMTimeConvertScale(time, MP4GetTrackTimeScale(fileHandle, Id), kCMTimeRoundingMethod_Default); int64_t offset_i = trackTime.value; // If there isn't an existing edit list, just add some new ones at the start and do the usual stuff. if (offset_i && !trackEditsCount) { MP4Duration editDuration = MP4ConvertFromTrackDuration(fileHandle, Id, MP4GetTrackDuration(fileHandle, Id), MP4GetTimeScale(fileHandle)); if (offset_i > 0) { MP4Duration delayDuration = MP4ConvertFromTrackDuration(fileHandle, Id, offset_i, MP4GetTimeScale(fileHandle)); MP4AddTrackEdit(fileHandle, Id, MP4_INVALID_EDIT_ID, -1, delayDuration, 0); MP4AddTrackEdit(fileHandle, Id, MP4_INVALID_EDIT_ID, 0, editDuration, 0); } else if (offset_i < 0) { MP4Duration delayDuration = MP4ConvertFromTrackDuration(fileHandle, Id, -offset_i, MP4GetTimeScale(fileHandle)); MP4AddTrackEdit(fileHandle, Id, MP4_INVALID_EDIT_ID, -offset_i, editDuration - delayDuration, 0); } } // If the mp4 contains already some edits list, try to reuse them else if (trackEditsCount) { if (offset_i >= 0) { // Remove all the empty edit lists while (MP4GetTrackNumberOfEdits(fileHandle, Id)) { uint64_t editListVersion = 0; MP4Timestamp editMediaTime = MP4GetTrackEditMediaStart(fileHandle, Id, 1); MP4GetTrackIntegerProperty(fileHandle, Id, "edts.elst.version", &editListVersion); if (editListVersion == 0 && editMediaTime == ((uint32_t)-1)) MP4DeleteTrackEdit(fileHandle, Id, 1); else if (editListVersion == 1 && editMediaTime == ((uint64_t)-1)) MP4DeleteTrackEdit(fileHandle, Id, 1); else { if (getTrackStartOffset(fileHandle, Id) < 0) { MP4Duration oldEditDuration = MP4GetTrackEditDuration(fileHandle, Id, 1); MP4Duration oldEditMediaStart = MP4GetTrackEditMediaStart(fileHandle, Id, 1); oldEditMediaStart = MP4ConvertFromTrackDuration(fileHandle, Id, oldEditMediaStart, MP4GetTimeScale(fileHandle)); MP4SetTrackEditDuration(fileHandle, Id, 1, oldEditDuration + oldEditMediaStart); MP4SetTrackEditMediaStart(fileHandle, Id, 1, 0); } break; } } MP4Duration delayDuration = MP4ConvertFromTrackDuration(fileHandle, Id, offset_i, MP4GetTimeScale(fileHandle)); if (offset_i != 0) MP4AddTrackEdit(fileHandle, Id, 1, -1, delayDuration, 0); } else if (offset_i < 0) { // First remove all the empty edit lists while (MP4GetTrackNumberOfEdits(fileHandle, Id)) { uint64_t editListVersion = 0; MP4Timestamp editMediaTime = MP4GetTrackEditMediaStart(fileHandle, Id, 1); MP4GetTrackIntegerProperty(fileHandle, Id, "edts.elst.version", &editListVersion); if (editListVersion == 0 && editMediaTime == ((uint32_t)-1)) MP4DeleteTrackEdit(fileHandle, Id, 1); else if (editListVersion == 1 && editMediaTime == ((uint64_t)-1)) MP4DeleteTrackEdit(fileHandle, Id, 1); else break; } // If there is already an edit list reuse it if (MP4GetTrackNumberOfEdits(fileHandle, Id)) { MP4Duration oldEditDuration = MP4GetTrackEditDuration(fileHandle, Id, 1); MP4Duration oldEditMediaStart = MP4GetTrackEditMediaStart(fileHandle, Id, 1); oldEditMediaStart = MP4ConvertFromTrackDuration(fileHandle, Id, oldEditMediaStart, MP4GetTimeScale(fileHandle)); MP4Duration newOffsetDuration = MP4ConvertFromTrackDuration(fileHandle, Id, -offset_i, MP4GetTimeScale(fileHandle)); MP4SetTrackEditDuration(fileHandle, Id, 1, oldEditDuration + oldEditMediaStart - newOffsetDuration); MP4SetTrackEditMediaStart(fileHandle, Id, 1, -offset_i); } // Else create a new one. else { MP4Duration delayDuration = MP4ConvertFromTrackDuration(fileHandle, Id, -offset_i, MP4GetTimeScale(fileHandle)); MP4AddTrackEdit(fileHandle, Id, 1, -1, delayDuration, 0); } } } // Update the duration in tkhd, the value must be the sum of the durations of all track's edits. MP4Duration totalDuration = getTrackDuration(fileHandle, Id); MP4SetTrackIntegerProperty(fileHandle, Id, "tkhd.duration", totalDuration); // Update the duration in mvhd. updateMoovDuration(fileHandle); } int copyTrackEditLists (MP4FileHandle fileHandle, MP4TrackId srcTrackId, MP4TrackId dstTrackId) { MP4Duration trackDuration = 0; uint32_t i = 1, trackEditCount = MP4GetTrackNumberOfEdits(fileHandle, srcTrackId); while (i <= trackEditCount) { MP4Timestamp editMediaStart = MP4GetTrackEditMediaStart(fileHandle, srcTrackId, i); MP4Duration editDuration = MP4ConvertFromMovieDuration(fileHandle, MP4GetTrackEditDuration(fileHandle, srcTrackId, i), MP4GetTimeScale(fileHandle)); trackDuration += editDuration; int8_t editDwell = MP4GetTrackEditDwell(fileHandle, srcTrackId, i); MP4AddTrackEdit(fileHandle, dstTrackId, i, editMediaStart, editDuration, editDwell); i++; } if (trackEditCount) MP4SetTrackIntegerProperty(fileHandle, dstTrackId, "tkhd.duration", trackDuration); else { uint32_t firstFrameOffset = MP4GetSampleRenderingOffset(fileHandle, dstTrackId, 1); MP4Duration editDuration = MP4ConvertFromTrackDuration(fileHandle, srcTrackId, MP4GetTrackDuration(fileHandle, srcTrackId), MP4GetTimeScale(fileHandle)); MP4AddTrackEdit(fileHandle, dstTrackId, MP4_INVALID_EDIT_ID, firstFrameOffset, editDuration, 0); } return 1; } NSError* MP42Error(NSString *description, NSString* recoverySuggestion, NSInteger code) { NSMutableDictionary *errorDetail = [NSMutableDictionary dictionary]; [errorDetail setValue:description forKey:NSLocalizedDescriptionKey]; [errorDetail setValue:recoverySuggestion forKey:NSLocalizedRecoverySuggestionErrorKey]; return [NSError errorWithDomain:@"MP42Error" code:code userInfo:errorDetail]; } // Taken from HandBrake common.c int yuv2rgb(int yuv) { double y, Cr, Cb; int r, g, b; y = (yuv >> 16) & 0xff; Cb = (yuv >> 8) & 0xff; Cr = (yuv ) & 0xff; r = 1.164 * (y - 16) + 2.018 * (Cb - 128); g = 1.164 * (y - 16) - 0.813 * (Cr - 128) - 0.391 * (Cb - 128); b = 1.164 * (y - 16) + 1.596 * (Cr - 128); r = (r < 0) ? 0 : r; g = (g < 0) ? 0 : g; b = (b < 0) ? 0 : b; r = (r > 255) ? 255 : r; g = (g > 255) ? 255 : g; b = (b > 255) ? 255 : b; return (r << 16) | (g << 8) | b; } int rgb2yuv(int rgb) { double r, g, b; int y, Cr, Cb; r = (rgb >> 16) & 0xff; g = (rgb >> 8) & 0xff; b = (rgb ) & 0xff; y = 16. + ( 0.257 * r) + (0.504 * g) + (0.098 * b); Cb = 128. + (-0.148 * r) - (0.291 * g) + (0.439 * b); Cr = 128. + ( 0.439 * r) - (0.368 * g) - (0.071 * b); y = (y < 0) ? 0 : y; Cb = (Cb < 0) ? 0 : Cb; Cr = (Cr < 0) ? 0 : Cr; y = (y > 255) ? 255 : y; Cb = (Cb > 255) ? 255 : Cb; Cr = (Cr > 255) ? 255 : Cr; return (y << 16) | (Cr << 8) | Cb; } void *fast_realloc_with_padding(void *ptr, unsigned int *size, unsigned int min_size) { void *res = ptr; av_fast_malloc(&res, size, min_size + FF_INPUT_BUFFER_PADDING_SIZE); if (res) memset(res + min_size, 0, FF_INPUT_BUFFER_PADDING_SIZE); return res; } int DecompressZlib(uint8_t **sampleData, uint32_t *sampleSize) { uint8_t* pkt_data = NULL; uint8_t av_unused *newpktdata; int pkt_size = *sampleSize; int result = 0; z_stream zstream = {0}; if (inflateInit(&zstream) != Z_OK) return 0; zstream.next_in = *sampleData; zstream.avail_in = *sampleSize; do { pkt_size *= 3; newpktdata = av_realloc(pkt_data, pkt_size); if (!newpktdata) { inflateEnd(&zstream); goto failed; } pkt_data = newpktdata; zstream.avail_out = pkt_size - zstream.total_out; zstream.next_out = pkt_data + zstream.total_out; if (pkt_data) { result = inflate(&zstream, Z_NO_FLUSH); } else result = Z_MEM_ERROR; } while (result==Z_OK && pkt_size<10000000); pkt_size = zstream.total_out; inflateEnd(&zstream); if (result != Z_STREAM_END) { if (result == Z_MEM_ERROR) result = 0; else result = 0; goto failed; } if (*sampleData) free(*sampleData); *sampleData = pkt_data; *sampleSize = pkt_size; return 1; failed: av_free(pkt_data); return result; } int DecompressBzlib(uint8_t **sampleData, uint32_t *sampleSize) { uint8_t* pkt_data = NULL; uint8_t av_unused *newpktdata; int pkt_size = *sampleSize; int result = 0; bz_stream bzstream = {0}; if (BZ2_bzDecompressInit(&bzstream, 0, 0) != BZ_OK) return 0; bzstream.next_in = (char *) *sampleData; bzstream.avail_in = *sampleSize; do { pkt_size *= 3; newpktdata = av_realloc(pkt_data, pkt_size); if (!newpktdata) { BZ2_bzDecompressEnd(&bzstream); goto failed; } pkt_data = newpktdata; bzstream.avail_out = pkt_size - bzstream.total_out_lo32; bzstream.next_out = (char *) pkt_data + bzstream.total_out_lo32; if (pkt_data) { result = BZ2_bzDecompress(&bzstream); } else result = BZ_MEM_ERROR; } while (result==BZ_OK && pkt_size<10000000); pkt_size = bzstream.total_out_lo32; BZ2_bzDecompressEnd(&bzstream); if (result != BZ_STREAM_END) { if (result == BZ_MEM_ERROR) result = 0; else result = 0; goto failed; } if (*sampleData) free(*sampleData); *sampleData = pkt_data; *sampleSize = pkt_size; return 1; failed: av_free(pkt_data); return result; }