yuv420sp2Uint8List method

Uint8List yuv420sp2Uint8List({
  1. required Uint8List y,
  2. required Uint8List u,
  3. required Uint8List v,
})

Converts YUV bytes to a Uint8List of YUV420sp(NV12).

The use case for this method is when using the camera plugin. https://pub.dev/packages/camera See example.

Implementation

Uint8List yuv420sp2Uint8List({
  required Uint8List y,
  required Uint8List u,
  required Uint8List v,
}) {
  assert(y.isNotEmpty, 'y is empty');
  assert(u.isNotEmpty, 'u is empty');
  assert(v.isNotEmpty, 'v is empty');

  final yuv420sp = Uint8List(
    y.length + u.length + v.length,
  );

  /// https://wiki.videolan.org/YUV#Semi-planar
  for (var i = 0; i < y.length; i++) {
    yuv420sp[i] = y[i];
  }

  for (var i = 0; i < u.length; i += 2) {
    yuv420sp[y.length + i] = u[i];
    yuv420sp[y.length + i + 1] = v[i];
  }
  return yuv420sp;
}