在经历了很多痛苦和痛苦之后,我终于设法使它工作。
我需要提供一个至少为kCMFormatDescriptionExtension_SampleDescriptionExtensionAtoms
键值的CFDictionaryRef。这个键的值也必须是一个CFDictionaryRef。对于H264的类型,这是在CMVideoFormatDescriptionCreateFromH264ParameterSets
内部创建的,看起来像这样:
avcC = <014d401e ffe10016 674d401e 9a660a0f ff350101 01400000 fa000013 88010100 0468ee3c 80>
然而,对于MPEG4类型,您需要自己创建这个。最终的结果应该是这样的:
esds = <00000000 038081e6 00000003 8081e611 00000000 00000000 058081e5 060102>
我们创造这仍然是模糊的,以我的方式,但它在某种程度上工作。我受到this link的启发。这是代码:
- (CMFormatDescriptionRef)createFormatDescriptorFromMPEG4Message:(MessageContainer *)message {
CMVideoFormatDescriptionRef mediaDescriptor = NULL;
NSData *esdsData = [self newESDSFromData:message.frameData];
CFMutableDictionaryRef esdsDictionary = CFDictionaryCreateMutable(kCFAllocatorDefault, 1,
&kCFTypeDictionaryKeyCallBacks,
&kCFTypeDictionaryValueCallBacks);
CFDictionarySetValue(esdsDictionary, CFSTR("esds"), (__bridge const void *)(esdsData));
NSDictionary *dictionary = @{(__bridge NSString *)kCMFormatDescriptionExtension_SampleDescriptionExtensionAtoms : (__bridge NSDictionary *)esdsDictionary};
OSStatus status = CMVideoFormatDescriptionCreate(kCFAllocatorDefault,
self.mediaCodec,
message.frameSize.width,
message.frameSize.height,
(__bridge CFDictionaryRef)dictionary,
&mediaDescriptor);
if (status) {
NSLog(@"CMVideoFormatDesciprionCreate failed with %zd", status);
}
return mediaDescriptor;
}
- (NSData *)newESDSFromData:(NSData *)data {
NSInteger dataLength = data.length;
int full_size = 3 + 5 + 13 + 5 + dataLength + 3;
// ES_DescrTag data + DecoderConfigDescrTag + data + DecSpecificInfoTag + size + SLConfigDescriptor
int config_size = 13 + 5 + dataLength;
int padding = 12;
int8_t *esdsInfo = calloc(full_size + padding, sizeof(int8_t));
//Version
esdsInfo[0] = 0;
//Flags
esdsInfo[1] = 0;
esdsInfo[2] = 0;
esdsInfo[3] = 0;
//ES_DescrTag
esdsInfo[4] |= 0x03;
[self addMPEG4DescriptionLength:full_size
toPointer:esdsInfo + 5];
//esid
esdsInfo[8] = 0;
esdsInfo[9] = 0;
//Stream priority
esdsInfo[10] = 0;
//DecoderConfigDescrTag
esdsInfo[11] = 0x03;
[self addMPEG4DescriptionLength:config_size
toPointer:esdsInfo + 12];
//Stream Type
esdsInfo[15] = 0x11;
//Buffer Size
esdsInfo[16] = 0;
esdsInfo[17] = 0;
//Max bitrate
esdsInfo[18] = 0;
esdsInfo[19] = 0;
esdsInfo[20] = 0;
//Avg bitrate
esdsInfo[21] = 0;
esdsInfo[22] = 0;
esdsInfo[23] = 0;
//< DecSpecificInfoTag
esdsInfo[24] |= 0x05;
[self addMPEG4DescriptionLength:dataLength
toPointer:esdsInfo + 25];
//SLConfigDescrTag
esdsInfo[28] = 0x06;
//Length
esdsInfo[29] = 0x01;
esdsInfo[30] = 0x02;
NSData *esdsData = [NSData dataWithBytes:esdsInfo length:31 * sizeof(int8_t)];
free(esdsInfo);
return esdsData;
}
- (void)addMPEG4DescriptionLength:(NSInteger)length
toPointer:(int8_t *)ptr {
for (int i = 3; i >= 0; i--) {
uint8_t b = (length >> (i * 7)) & 0x7F;
if (i != 0) {
b |= 0x80;
}
ptr[3 - i] = b;
}
}
消息容器是一个简单的包装围绕从服务器接收到的数据:
@interface MessageContainer : NSObject
@property (nonatomic) CGSize frameSize;
@property (nonatomic) NSData *frameData;
@end
凡frameSize
是帧(从服务器分别接收到的)的尺寸和数据本身就是frameData
。
哦,它有助于我用快速解码ios上的mpeg4,但只有第一帧!成功解码第一帧后,我总是收到错误-12911 kVTVideoDecoderMalfunctionErr。你知道我有什么可能是错的吗? – JULIIncognito
我很高兴它有帮助。可悲的是,自从这个项目通过以来,我一直在努力,所以我不能真正帮助你。你可以尝试为每一帧创建'CFDictionaryRef',看看是否有效。然后,从那里开始,找出可能出错的地方。注意:我只需要在第一帧(这是一个关键帧)中执行此操作。 –
是的,这是要点:)我已经做到了每一帧,我的坏。现在它工作完美!谢谢 – JULIIncognito