/* NV12 测试彩条生成: Y 平面填彩条, UV 平面填色度 */
#include "test_pattern.h"
#include <string.h>

/* SMPTE 彩条对应的 Y 值 */
static const uint8_t bar_y[8] = { 235,173,162,84,84,162,173,235 };

void test_pattern_nv12(uint8_t *out, int hor_stride, int ver_stride, int height, int frame_cnt){
    int bar_w = hor_stride / 8;
    if(bar_w < 1) bar_w = 1;
    /* Y */
    for(int y = 0; y < height; y++){
        uint8_t *row = out + (size_t)y * hor_stride;
        for(int x = 0; x < hor_stride; x++){
            int b = (x / bar_w) & 7;
            /* 中部加渐变 + 帧号滚动, 让画面有运动 */
            if(y > height/2){
                row[x] = (uint8_t)((bar_y[b] + (frame_cnt & 63) + (x & 7)) >> 1);
            }else{
                row[x] = bar_y[b];
            }
        }
    }
    /* 顶部余白 stride 填 16 */
    for(int y = height; y < ver_stride; y++){
        memset(out + (size_t)y * hor_stride, 16, hor_stride);
    }
    /* UV (NV12: 交错, 整个 chroma 平面) */
    uint8_t *uv = out + (size_t)hor_stride * ver_stride;
    int ch = ver_stride / 2;
    size_t uv_len = (size_t)hor_stride * ch;
    for(size_t i = 0; i < uv_len; i += 2){
        uint8_t u = (uint8_t)((i >> 1) & 0xff);
        uint8_t v = (uint8_t)((frame_cnt * 4 + (i >> 3)) & 0xff);
        uv[i]   = u;
        uv[i+1] = v;
    }
}
