Advertisement
mpoullet

CMVideoFormatDescriptionCreateFromH264ParameterSets

Mar 3rd, 2017
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // clang CMVideoFormatDescriptionCreateFromH264ParameterSets.m -std=c11 -Wall -Wextra -fobjc-arc -fmodules -o CMVideoFormatDescriptionCreateFromH264ParameterSets
  2.  
  3. @import CoreMedia;
  4.  
  5. #define PARAMETERSETCOUNT 2
  6. #define SPSIDX 0
  7. #define PPSIDX 1
  8.  
  9. OSStatus formatCreate(const uint8_t sps[], size_t sps_size, const uint8_t pps[], size_t pps_size, CMVideoFormatDescriptionRef *formatDescription)
  10. {
  11.     const uint8_t* parameterSetPointers[PARAMETERSETCOUNT];
  12.     parameterSetPointers[SPSIDX] = sps;
  13.     parameterSetPointers[PPSIDX] = pps;
  14.     size_t parameterSetSizes[PARAMETERSETCOUNT];
  15.     parameterSetSizes[SPSIDX] = sps_size;
  16.     parameterSetSizes[PPSIDX] = pps_size;
  17.  
  18.     printf("SPS length = %zu\n", parameterSetSizes[SPSIDX]);
  19.     for(size_t i=0;i<parameterSetSizes[SPSIDX];i++) printf("0x%02x\n", *(parameterSetPointers[SPSIDX] + i));
  20.  
  21.     printf("PPS length = %zu\n", parameterSetSizes[PPSIDX]);
  22.     for(size_t i=0;i<parameterSetSizes[PPSIDX];i++) printf("0x%02x\n", *(parameterSetPointers[PPSIDX] + i));
  23.  
  24.     const size_t parameterSetCount = PARAMETERSETCOUNT;
  25.     const int NALUnitHeaderLength = 4;
  26.     return CMVideoFormatDescriptionCreateFromH264ParameterSets(NULL,
  27.                                                                parameterSetCount,
  28.                                                                parameterSetPointers,
  29.                                                                parameterSetSizes,
  30.                                                                NALUnitHeaderLength,
  31.                                                                formatDescription);
  32. }
  33.  
  34. void printFormatDescription(CMVideoFormatDescriptionRef formatDescription)
  35. {
  36.     CMVideoDimensions dimensions = CMVideoFormatDescriptionGetDimensions(formatDescription);
  37.     CGRect cleanAperture = CMVideoFormatDescriptionGetCleanAperture(formatDescription, true);
  38.     CGSize presentationDimensions = CMVideoFormatDescriptionGetPresentationDimensions(formatDescription, true, true);
  39.     printf("dimensions=%dx%d\ncleanAperture=%fx%f\npresentationDimensions=%fx%f\n",
  40.             dimensions.width,
  41.             dimensions.height,
  42.             cleanAperture.size.width,
  43.             cleanAperture.size.height,
  44.             presentationDimensions.width,
  45.             presentationDimensions.height);
  46. }
  47.  
  48. int main(void)
  49. {
  50.     // SD 576i
  51.     const uint8_t sps_576i[] = {
  52.                                0x67,
  53.                                0x64,
  54.                                0x00,
  55.                                0x1e,
  56.                                0xac,
  57.                                0x34,
  58.                                0xa5,
  59.                                0x02,
  60.                                0xd0,
  61.                                0x91,
  62.                                0x60,
  63.                                0x88,
  64.                                0x41,
  65.                                0x41,
  66.                                0x41,
  67.                                0x50,
  68.                                0x00,
  69.                                0x00,
  70.                                0x00,
  71.                                0x10,
  72.                                0x00,
  73.                                0x00,
  74.                                0x03,
  75.                                0x29,
  76.                                0xda,
  77.                                0x1a,
  78.                                0x2d,
  79.                                0x96
  80.     };
  81.     const uint8_t pps_576i[] = {
  82.                                0x68,
  83.                                0xe8,
  84.                                0x81,
  85.                                0x17,
  86.                                0x52,
  87.                                0x50
  88.     };
  89.  
  90.     // HD 720p
  91.     const uint8_t sps_720p[] = {
  92.                                0x67,
  93.                                0x64,
  94.                                0x00,
  95.                                0x28,
  96.                                0xac,
  97.                                0x34,
  98.                                0xa5,
  99.                                0x01,
  100.                                0x40,
  101.                                0x16,
  102.                                0xec,
  103.                                0x05,
  104.                                0x08,
  105.                                0x08,
  106.                                0x08,
  107.                                0x0a,
  108.                                0x00,
  109.                                0x00,
  110.                                0x00,
  111.                                0x02,
  112.                                0x00,
  113.                                0x00,
  114.                                0x00,
  115.                                0xc9,
  116.                                0x3b,
  117.                                0x43,
  118.                                0x86,
  119.                                0x14,
  120.                                0xb0
  121.     };
  122.     const uint8_t pps_720p[] = {
  123.                                0x68,
  124.                                0xe9,
  125.                                0x09,
  126.                                0x75,
  127.                                0x25
  128.     };
  129.  
  130.  
  131.     CMVideoFormatDescriptionRef formatDescription720p;
  132.     OSStatus formatCreateResult720p = formatCreate(sps_720p, sizeof(sps_720p), pps_720p, sizeof(pps_720p), &formatDescription720p);
  133.     printf("formatCreateResult720p = %d\n", formatCreateResult720p);
  134.     if (formatCreateResult720p == noErr) printFormatDescription(formatDescription720p);
  135.  
  136.     CMVideoFormatDescriptionRef formatDescription576i;
  137.     OSStatus formatCreateResult576i = formatCreate(sps_576i, sizeof(sps_576i), pps_576i, sizeof(pps_576i), &formatDescription576i);
  138.     printf("formatCreateResult576i = %d\n", formatCreateResult576i);
  139.     if (formatCreateResult576i == noErr) printFormatDescription(formatDescription576i);
  140.  
  141.     return 0;
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement