| 1 | /*****************************************************************************/ |
| 2 | /* |
| 3 | * NTSC/CRT - integer-only NTSC video signal encoding / decoding emulation |
| 4 | * |
| 5 | * by EMMIR 2018-2023 |
| 6 | * |
| 7 | * YouTube: https://www.youtube.com/@EMMIR_KC/videos |
| 8 | * Discord: https://discord.com/invite/hdYctSmyQJ |
| 9 | */ |
| 10 | /*****************************************************************************/ |
| 11 | #ifndef _CRT_NTSC_H_ |
| 12 | #define _CRT_NTSC_H_ |
| 13 | |
| 14 | #ifdef __cplusplus |
| 15 | extern "C" { |
| 16 | #endif |
| 17 | |
| 18 | /* crt_ntsc.h |
| 19 | * |
| 20 | * An interface to convert a digital image to an analog NTSC signal. |
| 21 | * |
| 22 | */ |
| 23 | /* 0 = vertical chroma (228 chroma clocks per line) */ |
| 24 | /* 1 = checkered chroma (227.5 chroma clocks per line) */ |
| 25 | #define CRT_CHROMA_PATTERN 1 |
| 26 | |
| 27 | /* chroma clocks (subcarrier cycles) per line */ |
| 28 | #if (CRT_CHROMA_PATTERN == 1) |
| 29 | #define CRT_CC_LINE 2275 |
| 30 | #else |
| 31 | /* this will give the 'rainbow' effect in the famous waterfall scene */ |
| 32 | #define CRT_CC_LINE 2280 |
| 33 | #endif |
| 34 | |
| 35 | /* NOTE, in general, increasing CRT_CB_FREQ reduces blur and bleed */ |
| 36 | #define CRT_CB_FREQ 4 /* carrier frequency relative to sample rate */ |
| 37 | #define CRT_HRES (CRT_CC_LINE * CRT_CB_FREQ / 10) /* horizontal res */ |
| 38 | #define CRT_VRES 262 /* vertical resolution */ |
| 39 | #define CRT_INPUT_SIZE (CRT_HRES * CRT_VRES) |
| 40 | |
| 41 | #define CRT_TOP 21 /* first line with active video */ |
| 42 | #define CRT_BOT 261 /* final line with active video */ |
| 43 | #define CRT_LINES (CRT_BOT - CRT_TOP) /* number of active video lines */ |
| 44 | |
| 45 | #define CRT_CC_SAMPLES 4 /* samples per chroma period (samples per 360 deg) */ |
| 46 | #define CRT_CC_VPER 1 /* vertical period in which the artifacts repeat */ |
| 47 | |
| 48 | /* search windows, in samples */ |
| 49 | #define CRT_HSYNC_WINDOW 8 |
| 50 | #define CRT_VSYNC_WINDOW 8 |
| 51 | |
| 52 | /* accumulated signal threshold required for sync detection. |
| 53 | * Larger = more stable, until it's so large that it is never reached in which |
| 54 | * case the CRT won't be able to sync |
| 55 | */ |
| 56 | #define CRT_HSYNC_THRESH 4 |
| 57 | #define CRT_VSYNC_THRESH 94 |
| 58 | |
| 59 | /* |
| 60 | * FULL HORIZONTAL LINE SIGNAL (~63500 ns) |
| 61 | * |---------------------------------------------------------------------------| |
| 62 | * HBLANK (~10900 ns) ACTIVE VIDEO (~52600 ns) |
| 63 | * |-------------------||------------------------------------------------------| |
| 64 | * |
| 65 | * |
| 66 | * WITHIN HBLANK PERIOD: |
| 67 | * |
| 68 | * FP (~1500 ns) SYNC (~4700 ns) BW (~600 ns) CB (~2500 ns) BP (~1600 ns) |
| 69 | * |--------------||---------------||------------||-------------||-------------| |
| 70 | * BLANK SYNC BLANK BLANK BLANK |
| 71 | * |
| 72 | */ |
| 73 | #define LINE_BEG 0 |
| 74 | #define FP_ns 1500 /* front porch */ |
| 75 | #define SYNC_ns 4700 /* sync tip */ |
| 76 | #define BW_ns 600 /* breezeway */ |
| 77 | #define CB_ns 2500 /* color burst */ |
| 78 | #define BP_ns 1600 /* back porch */ |
| 79 | #define AV_ns 52600 /* active video */ |
| 80 | #define HB_ns (FP_ns + SYNC_ns + BW_ns + CB_ns + BP_ns) /* h blank */ |
| 81 | /* line duration should be ~63500 ns */ |
| 82 | #define LINE_ns (FP_ns + SYNC_ns + BW_ns + CB_ns + BP_ns + AV_ns) |
| 83 | |
| 84 | /* convert nanosecond offset to its corresponding point on the sampled line */ |
| 85 | #define ns2pos(ns) ((ns) * CRT_HRES / LINE_ns) |
| 86 | /* starting points for all the different pulses */ |
| 87 | #define FP_BEG ns2pos(0) |
| 88 | #define SYNC_BEG ns2pos(FP_ns) |
| 89 | #define BW_BEG ns2pos(FP_ns + SYNC_ns) |
| 90 | #define CB_BEG ns2pos(FP_ns + SYNC_ns + BW_ns) |
| 91 | #define BP_BEG ns2pos(FP_ns + SYNC_ns + BW_ns + CB_ns) |
| 92 | #define AV_BEG ns2pos(HB_ns) |
| 93 | #define AV_LEN ns2pos(AV_ns) |
| 94 | |
| 95 | /* somewhere between 7 and 12 cycles */ |
| 96 | #define CB_CYCLES 10 |
| 97 | |
| 98 | /* frequencies for bandlimiting */ |
| 99 | #define L_FREQ 1431818 /* full line */ |
| 100 | #define Y_FREQ 420000 /* Luma (Y) 4.2 MHz of the 14.31818 MHz */ |
| 101 | #define I_FREQ 150000 /* Chroma (I) 1.5 MHz of the 14.31818 MHz */ |
| 102 | #define Q_FREQ 55000 /* Chroma (Q) 0.55 MHz of the 14.31818 MHz */ |
| 103 | |
| 104 | /* IRE units (100 = 1.0V, -40 = 0.0V) */ |
| 105 | #define WHITE_LEVEL 100 |
| 106 | #define BURST_LEVEL 20 |
| 107 | #define BLACK_LEVEL 7 |
| 108 | #define BLANK_LEVEL 0 |
| 109 | #define SYNC_LEVEL -40 |
| 110 | |
| 111 | #define NTSC_RAW 0 /* 0 = scale image to fit monitor, 1 = don't scale */ |
| 112 | #define NTSC_AS_COLOR 1 /* 0 = monochrome, 1 = full color */ |
| 113 | #define NTSC_HUE 0 /* 0-359 */ |
| 114 | #define NTSC_X_OFFSET 0 /* x offset in sample space. 0 is minimum value */ |
| 115 | #define NTSC_Y_OFFSET 0 /* y offset in # of lines. 0 is minimum value */ |
| 116 | |
| 117 | struct NTSC_SETTINGS { |
| 118 | const unsigned char *data; /* image data */ |
| 119 | int w, h; /* width and height of image */ |
| 120 | int field; /* 0 = even, 1 = odd */ |
| 121 | int frame; /* 0 = even, 1 = odd */ |
| 122 | /* make sure your NTSC_SETTINGS struct is zeroed out before you do anything */ |
| 123 | int iirs_initialized; /* internal state */ |
| 124 | }; |
| 125 | |
| 126 | #ifdef __cplusplus |
| 127 | } |
| 128 | #endif |
| 129 | |
| 130 | #endif |