How to Convert AVFrame from NSImage or UIImage

1190 ワード

AVFrame *frame = avcodec_alloc_frame();
      int numBytes = avpicture_get_size(PIX_FMT_YUV420P, outputWidth, outputHeight);
      uint8_t *buffer = (uint8_t *)av_malloc(numBytes*sizeof(uint8_t));
      avpicture_fill((AVPicture *)frame, buffer, PIX_FMT_YUV420P, outputWidth, outputHeight);
      //UIImage *image = ? smothing ? ;
      NSImage *image = ? smothing ? ;
      //CGImageRef newCgImage = image.CGImage;
      CGImageRef newCgImage = [image CGImageForProposedRect:nil context:nil hints:nil];
      //NSBitmapImageRep* bm = [NSBitmapImageRep imageRepWithData:[image TIFFRepresentation]];
      //CGImageRef newCgImage = [bm CGImage];
      size_t w = CGImageGetWidth(newCgImage);
      size_t h = CGImageGetHeight(cgImage);
      CGDataProviderRef dataProvider = CGImageGetDataProvider(newCgImage);
      CFDataRef bitmapData = CGDataProviderCopyData(dataProvider);
      uint8_t *buffer = (uint8_t *)CFDataGetBytePtr(bitmapData);
      frame->linesize[0] = w;
      int y, x;
      for (y = 0; y < h; y++) {
        for (x = 0; x < w; x++) {
          int z = y * w + x;
          frame->data[0][z] = buffer[z];
        }
      }