#ifndef BUFFER_CONVERTER_H
#define BUFFER_CONVERTER_H

#include <cstdint>
#include <cstddef>

// Buffer format conversion utilities for HAL compatibility.
// Converts between decoder output formats and HAL-expected formats.
// Uses ARM NEON intrinsics for performance on Pixel 9a (arm64-v8a).

namespace buffer_converter {

// Convert YUV420 planar (I420) to NV21 (semi-planar, VU interleaved)
// YUV420 planar: Y plane, U plane, V plane (separate)
// NV21: Y plane, VU interleaved plane
//
// src_y, src_u, src_v: source plane pointers
// src_y_stride, src_uv_stride: source plane strides (bytes per row)
// dst_nv21: destination buffer (Y plane followed by VU interleaved)
// dst_y_stride, dst_uv_stride: destination strides
// width, height: frame dimensions
//
// Returns true on success, false on error
bool yuv420_planar_to_nv21(
    const uint8_t* src_y, int src_y_stride,
    const uint8_t* src_u, int src_u_stride,
    const uint8_t* src_v, int src_v_stride,
    uint8_t* dst_nv21, int dst_y_stride, int dst_uv_stride,
    int width, int height);

// Convert YUV420 planar (I420) to YUV420 planar with different stride
// Handles stride mismatch between decoder output and HAL buffer.
// Copies Y plane with stride adjustment, then U and V planes.
//
// Returns true on success
bool yuv420_planar_copy_with_stride(
    const uint8_t* src_y, int src_y_stride,
    const uint8_t* src_u, int src_u_stride,
    const uint8_t* src_v, int src_v_stride,
    uint8_t* dst_y, int dst_y_stride,
    uint8_t* dst_u, int dst_u_stride,
    uint8_t* dst_v, int dst_v_stride,
    int width, int height);

// Convert NV21 (semi-planar) to YUV420 planar (I420)
// Used when decoder outputs NV21 but HAL expects planar.
//
// Returns true on success
bool nv21_to_yuv420_planar(
    const uint8_t* src_nv21, int src_y_stride, int src_uv_stride,
    uint8_t* dst_y, int dst_y_stride,
    uint8_t* dst_u, int dst_u_stride,
    uint8_t* dst_v, int dst_v_stride,
    int width, int height);

// Fast Y plane copy with NEON (handles stride mismatch)
// Copies width bytes per row, advancing by stride each row.
void copy_plane_neon(
    const uint8_t* src, int src_stride,
    uint8_t* dst, int dst_stride,
    int width, int height);

// Interleave U and V planes into NV21 format (VU order) using NEON
void interleave_uv_to_nv21_neon(
    const uint8_t* src_u, int src_u_stride,
    const uint8_t* src_v, int src_v_stride,
    uint8_t* dst_vu, int dst_vu_stride,
    int width, int height);

} // namespace buffer_converter

#endif // BUFFER_CONVERTER_H
