getNv21Uint8List method

Uint8List getNv21Uint8List()

Implementation

Uint8List getNv21Uint8List() {
  final width = this.width;
  final height = this.height;

  final yPlane = planes[0];
  final uPlane = planes[1];
  final vPlane = planes[2];

  final yBuffer = yPlane.bytes;
  final uBuffer = uPlane.bytes;
  final vBuffer = vPlane.bytes;

  final numPixels = (width * height * 1.5).toInt();
  final nv21 = Uint8List(numPixels);

  // Full size Y channel and quarter size U+V channels.
  int idY = 0;
  int idUV = width * height;
  final uvWidth = width ~/ 2;
  final uvHeight = height ~/ 2;
  // Copy Y & UV channel.
  // NV21 format is expected to have YYYYVU packaging.
  // The U/V planes are guaranteed to have the same row stride and pixel stride.
  // getRowStride analogue??
  final uvRowStride = uPlane.bytesPerRow;
  // getPixelStride analogue
  final uvPixelStride = uPlane.bytesPerPixel ?? 0;
  final yRowStride = yPlane.bytesPerRow;
  final yPixelStride = yPlane.bytesPerPixel ?? 0;

  for (int y = 0; y < height; ++y) {
    final uvOffset = y * uvRowStride;
    final yOffset = y * yRowStride;

    for (int x = 0; x < width; ++x) {
      nv21[idY++] = yBuffer[yOffset + x * yPixelStride];

      if (y < uvHeight && x < uvWidth) {
        final bufferIndex = uvOffset + (x * uvPixelStride);
        //V channel
        nv21[idUV++] = vBuffer[bufferIndex];
        //V channel
        nv21[idUV++] = uBuffer[bufferIndex];
      }
    }
  }
  return nv21;
}