title: Which YCbCr matrix to use? BT.709 or BT.601
body: <p>I'm working on a video player project on iOS.</p>
  
  <p>It uses <a href="https://developer.apple.com/reference/avfoundation" rel="noreferrer">AVFoundation</a> to extract <a href="https://developer.apple.com/reference/corevideo/1665102-cvpixelbuffer" rel="noreferrer">CVPixelBuffer</a> from a video file and then send that buffer to OpenGL as textures.</p>
  
  <p>The proof-of-concept code is inspired by <a href="https://developer.apple.com/library/content/samplecode/AVBasicVideoOutput/Listings/AVBasicVideoOutput_APLEAGLView_m.html#//apple_ref/doc/uid/DTS40013109-AVBasicVideoOutput_APLEAGLView_m-DontLinkElementID_6" rel="noreferrer">Apple's sample code</a>.
  The AVFoundation provides each frame in YCbCr color space and it need to be transformed into RGB to render in OpenGL. This transform seems to have multiple transform matrix options depending on different YCbCr standard(e.g. <a href="https://en.wikipedia.org/wiki/YCbCr#ITU-R_BT.601_conversion" rel="noreferrer">ITU-R BT.709, ITU-R BT.601</a>). The sample code determines which one to use by following code:
  </p>
  
  <blockquote>
  <pre><code>CFTypeRef colorAttachments = CVBufferGetAttachment(pixelBuffer, kCVImageBufferYCbCrMatrixKey, NULL);
  if (colorAttachments == kCVImageBufferYCbCrMatrix_ITU_R_601_4) {
      _preferredConversion = kColorConversion601;
  }
  else {
      _preferredConversion = kColorConversion709;
  }
  </code></pre>
  </blockquote>
  
  <p>However, I'm using swift and the return <code>colorAttachment</code> is of type <code>Unmanaged&lt;CFTypeRef&gt;</code> while the constant <code>kCVImageBufferYCbCrMatrix_ITU_R_601_4</code> is of type <code>CFString</code> so they can't be equaled directly. I did some research and ended up with:
  </p>
  
  <pre><code>CFEqual(colorAttachments, kCVImageBufferYCbCrMatrix_ITU_R_601_4) // returns false
  CFEqual(colorAttachments, kCVImageBufferYCbCrMatrix_ITU_R_709_2) // returns false too!!
  //-----------------------------------------
  CFGetType(colorAttachments) // returns 1
  CFStringGetType() // returns 7, note kCVImageBufferYCbCrMatrix_ITU_R_601_4 is of type CFString
  // so I still can't check their equality 
  // because the retrieved  colorAttachments is not of type CFString at all
  </code></pre>
  
  <hr>
  
  <p>I tried two transforms one-by-one by hardcoding the matrix and the result (rendered scene) seems no difference to human eye which is predictable because the two transform matrixes are not differed to much.</p>
  
  <p>My questions:</p>
  
  <ol>
  <li>How to determine which transform to use?</li>
  <li>If it's impossible to solve [1.], can I hard code either one? what's the consequence of doing so?</li>
  </ol>