title: 420YpCbCr8 to RGB conversation
body: <p>I try to set texture with <code>CVPixelBuffer</code> that i get from video stream</p>
  
  <pre><code>NSDictionary* videoOutputOptions = @{ (id)kCVPixelBufferPixelFormatTypeKey : [NSNumber numberWithInt:kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange] };
          self.videoOutput = [[AVPlayerItemVideoOutput alloc] initWithPixelBufferAttributes:videoOutputOptions];
  </code></pre>
  
  <p>to texture that i want to use. For this purpose I use next fragment shader:</p>
  
  <pre><code>varying lowp vec2 v_texCoord;
  precision mediump float;
  
  uniform sampler2D SamplerUV;
  uniform sampler2D SamplerY;
  uniform mat3 colorConversionMatrix;
  
  void main()
  {
      mediump vec3 yuv;
      lowp vec3 rgb;
  
      // Subtract constants to map the video range start at 0
      yuv.x = (texture2D(SamplerY, v_texCoord).r - (16.0/255.0));
      yuv.yz = (texture2D(SamplerUV, v_texCoord).rg - vec2(0.5, 0.5));
  
      rgb = colorConversionMatrix * yuv;
  
      gl_FragColor = vec4(rgb,1);
  }
  </code></pre>
  
  <p>And conversation matrix is</p>
  
  <pre><code>// BT.709, which is the standard for HDTV.
  static const GLfloat kColorConversion709[] = {
      1.164,  1.164, 1.164,
      0.0, -0.213, 2.112,
      1.793, -0.533,   0.0,
  };
  </code></pre>
  
  <p>But as result i got <code>greened</code> texture - think this mean that I use incorrect conversation.
  My result:</p>
  
  <p><img src="https://i.stack.imgur.com/eY9UW.png" alt="greened image"></p>
  
  <p>So, also try to change konversation matrix to another one - <a href="https://github.com/BradLarson/GPUImage/blob/master/framework/Source/Mac/GPUImageAVCamera.m" rel="nofollow noreferrer">here</a>.
  Also try variants from <a href="http://www.equasys.de/colorconversion.html" rel="nofollow noreferrer">this</a> resource. But lookl like maybe it's problem not only in conversation, but in fragment shader also?</p>
  
  <p>Any suggestion, why i got greened image? (<strong>question</strong>).</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>This is mathod how I get textures (use apple sample <code>AVBasicVideoOutput</code> and <a href="https://developer.apple.com/library/prerelease/ios/documentation/CoreVideo/Reference/CVOpenGLESTextureCacheRef/index.html#//apple_ref/c/func/CVOpenGLESTextureCacheCreateTextureFromImage" rel="nofollow noreferrer">this</a>)</p>
  
  <pre><code>- (void)displayPixelBuffer:(CVPixelBufferRef)pixelBuffer frameSize:(CGSize)presentationSize
  {
      CVReturn err;
      if (pixelBuffer != NULL) {
          int frameWidth = (int)CVPixelBufferGetWidth(pixelBuffer);
          int frameHeight = (int)CVPixelBufferGetHeight(pixelBuffer);
  
          if (!_videoTextureCache) {
              NSLog(@"No video texture cache");
              return;
          }
          [self cleanUpTextures];
          //Use the color attachment of the pixel buffer to determine the appropriate color conversion matrix.
          CFTypeRef colorAttachments = CVBufferGetAttachment(pixelBuffer, kCVImageBufferYCbCrMatrixKey, NULL);
  
          if (colorAttachments == kCVImageBufferYCbCrMatrix_ITU_R_601_4) {
              _preferredConversion = kColorConversion601;
          }
          else {
              _preferredConversion = kColorConversion709;
          }
           //CVOpenGLESTextureCacheCreateTextureFromImage will create GLES texture optimally from CVPixelBufferRef.
           //Create Y and UV textures from the pixel buffer. These textures will be drawn on the frame buffer Y-plane.
          glActiveTexture(GL_TEXTURE0);
          err = CVOpenGLESTextureCacheCreateTextureFromImage(kCFAllocatorDefault, _videoTextureCache, pixelBuffer, NULL,  GL_TEXTURE_2D, GL_LUMINANCE, frameWidth, frameHeight, GL_LUMINANCE, GL_UNSIGNED_BYTE, 0, &amp;_lumaTexture);
          if (err) {
              NSLog(@"Error at CVOpenGLESTextureCacheCreateTextureFromImage %d", err);
          }
  
          glBindTexture(CVOpenGLESTextureGetTarget(_lumaTexture), CVOpenGLESTextureGetName(_lumaTexture));
          glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
          glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
          glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
          glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  
          // UV-plane.
          glActiveTexture(GL_TEXTURE1);
          err = CVOpenGLESTextureCacheCreateTextureFromImage(kCFAllocatorDefault, _videoTextureCache, pixelBuffer, NULL, GL_TEXTURE_2D, GL_LUMINANCE_ALPHA, frameWidth / 2, frameHeight / 2, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, 1, &amp;_chromaTexture);
          if (err) {
              NSLog(@"Error at CVOpenGLESTextureCacheCreateTextureFromImage %d", err);
          }
  
          glBindTexture(CVOpenGLESTextureGetTarget(_chromaTexture), CVOpenGLESTextureGetName(_chromaTexture));
          glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
          glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
          glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
          glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  
          glBindFramebuffer(GL_FRAMEBUFFER, _vertexBufferID);
  
          CFRelease(pixelBuffer);
      }
  }
  </code></pre>
  
  <p><strong>EDIT</strong></p>
  
  <p>If someone put <code>-1</code> - please add comment why u do this and whats wrong - maybe for someone problem is visible and simple, but for others - not</p>