MPEG4流的CMVideoFormatDescription扩展

时间:2015-10-15 10:31:21

标签: ios video-streaming mpeg-4

我设法解码并播放H264视频,但是我在使用MPEG4视频时遇到了困难。

它需要哪些CMVideoFormatDescription扩展?尝试创建VTDecompressionSession时,我收到-8971错误(codecExtensionNotFoundErr)。

这就是我创建VideoFormatDescription

的方法
OSStatus success = CMVideoFormatDescriptionCreate(kCFAllocatorDefault,
                                                  self.mediaCodec,
                                                  message.frameSize.width,
                                                  message.frameSize.height,
                                                  NULL,
                                                  &mediaDescriptor);

而不是那个NULL,我假设我需要指定一个CFDictionaryRef,但是我不知道它应该包含什么。有什么想法吗?

1 个答案:

答案 0 :(得分:4)

经过多次痛苦和痛苦之后,我终于成功了。

我需要提供一个CFDictionaryRef,其中至少包含kCMFormatDescriptionExtension_SampleDescriptionExtensionAtoms键的值。此键的值也必须是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是数据本身。