0
# Scaling and Format Conversion
1
2
Video scaling, pixel format conversion, and color space transformations using FFmpeg's libswscale with optimized implementations for performance.
3
4
## Capabilities
5
6
### Scaling Context Management
7
8
#### Context Operations
9
10
```java { .api }
11
/**
12
* Allocate and initialize scaling context
13
* @param srcW Source width
14
* @param srcH Source height
15
* @param srcFormat Source pixel format
16
* @param dstW Destination width
17
* @param dstH Destination height
18
* @param dstFormat Destination pixel format
19
* @param flags Scaling algorithm flags
20
* @param srcFilter Source filter (optional)
21
* @param dstFilter Destination filter (optional)
22
* @param param Additional parameters (optional)
23
* @return Scaling context or null on failure
24
*/
25
SwsContext sws_getContext(int srcW, int srcH, int srcFormat,
26
int dstW, int dstH, int dstFormat, int flags,
27
SwsFilter srcFilter, SwsFilter dstFilter, DoublePointer param);
28
29
/**
30
* Free scaling context
31
* @param swsContext Context to free
32
*/
33
void sws_freeContext(SwsContext swsContext);
34
35
/**
36
* Scale image data
37
* @param c Scaling context
38
* @param srcSlice Source image planes
39
* @param srcStride Source line strides
40
* @param srcSliceY Starting Y position
41
* @param srcSliceH Height to process
42
* @param dst Destination image planes
43
* @param dstStride Destination line strides
44
* @return Height of output slice
45
*/
46
int sws_scale(SwsContext c, PointerPointer srcSlice, IntPointer srcStride,
47
int srcSliceY, int srcSliceH, PointerPointer dst, IntPointer dstStride);
48
```
49
50
**Usage Example:**
51
52
```java
53
import org.bytedeco.ffmpeg.swscale.*;
54
import static org.bytedeco.ffmpeg.global.swscale.*;
55
import static org.bytedeco.ffmpeg.global.avutil.*;
56
57
// Create scaling context: 1920x1080 -> 640x480, YUV420P -> RGB24
58
SwsContext swsContext = sws_getContext(
59
1920, 1080, AV_PIX_FMT_YUV420P, // Source
60
640, 480, AV_PIX_FMT_RGB24, // Destination
61
SWS_BILINEAR, // Scaling algorithm
62
null, null, (DoublePointer)null // Optional filters/params
63
);
64
65
if (swsContext == null) {
66
throw new RuntimeException("Cannot create scaling context");
67
}
68
69
// Allocate destination frame
70
AVFrame dstFrame = av_frame_alloc();
71
dstFrame.width(640);
72
dstFrame.height(480);
73
dstFrame.format(AV_PIX_FMT_RGB24);
74
av_frame_get_buffer(dstFrame, 32);
75
76
// Scale source frame to destination
77
AVFrame srcFrame = /* ... source frame ... */;
78
int result = sws_scale(
79
swsContext,
80
srcFrame.data(), srcFrame.linesize(),
81
0, srcFrame.height(),
82
dstFrame.data(), dstFrame.linesize()
83
);
84
85
System.out.println("Scaled " + result + " lines");
86
87
// Cleanup
88
av_frame_free(dstFrame);
89
sws_freeContext(swsContext);
90
```
91
92
### Advanced Scaling
93
94
#### Cached Context Operations
95
96
```java { .api }
97
/**
98
* Scale with cached context (allocates if needed)
99
* @param context Pointer to context (null initially)
100
* @param srcW Source width
101
* @param srcH Source height
102
* @param srcFormat Source format
103
* @param dstW Destination width
104
* @param dstH Destination height
105
* @param dstFormat Destination format
106
* @param flags Scaling flags
107
* @param srcFilter Source filter
108
* @param dstFilter Destination filter
109
* @param param Parameters
110
* @return Scaling context
111
*/
112
SwsContext sws_getCachedContext(SwsContext context,
113
int srcW, int srcH, int srcFormat,
114
int dstW, int dstH, int dstFormat, int flags,
115
SwsFilter srcFilter, SwsFilter dstFilter, DoublePointer param);
116
117
/**
118
* Check if scaling is supported
119
* @param pix_fmt Pixel format
120
* @return 1 if supported for input, 2 if supported for output
121
*/
122
int sws_isSupportedInput(int pix_fmt);
123
int sws_isSupportedOutput(int pix_fmt);
124
```
125
126
### Vector Operations
127
128
#### SIMD Optimizations
129
130
```java { .api }
131
/**
132
* Get CPU flags for optimization
133
* @return CPU capability flags
134
*/
135
int av_get_cpu_flags();
136
137
/**
138
* Initialize scaling context with CPU optimizations
139
* @param c Scaling context
140
* @param srcFilter Source filter
141
* @param dstFilter Destination filter
142
* @return 0 on success
143
*/
144
int sws_init_context(SwsContext c, SwsFilter srcFilter, SwsFilter dstFilter);
145
```
146
147
### Color Space Operations
148
149
#### Color Space Information
150
151
```java { .api }
152
/**
153
* Get coefficients for color space conversion
154
* @param colorspace Color space (BT.709, BT.601, etc.)
155
* @return Color space coefficients
156
*/
157
IntPointer sws_getCoefficients(int colorspace);
158
159
/**
160
* Set color space conversion parameters
161
* @param c Scaling context
162
* @param inv_table Inverse color table
163
* @param fullRange Full range flag
164
* @param table Color conversion table
165
* @param srcRange Source range
166
* @param dstRange Destination range
167
* @param brightness Brightness adjustment
168
* @param contrast Contrast adjustment
169
* @param saturation Saturation adjustment
170
* @return 0 on success
171
*/
172
int sws_setColorspaceDetails(SwsContext c, IntPointer inv_table, int fullRange,
173
IntPointer table, int srcRange, int dstRange, int brightness, int contrast, int saturation);
174
```
175
176
## Constants
177
178
### Scaling Algorithms
179
180
```java { .api }
181
// Scaling algorithm flags
182
int SWS_FAST_BILINEAR = 1; // Fast bilinear
183
int SWS_BILINEAR = 2; // Bilinear
184
int SWS_BICUBIC = 4; // Bicubic
185
int SWS_X = 8; // Experimental
186
int SWS_POINT = 0x10; // Nearest neighbor
187
int SWS_AREA = 0x20; // Area averaging
188
int SWS_BICUBLIN = 0x40; // Bicubic for luma, bilinear for chroma
189
int SWS_GAUSS = 0x80; // Gaussian
190
int SWS_SINC = 0x100; // Sinc
191
int SWS_LANCZOS = 0x200; // Lanczos
192
int SWS_SPLINE = 0x400; // Spline
193
```
194
195
### Pixel Formats
196
197
```java { .api }
198
// Common pixel formats for scaling
199
int AV_PIX_FMT_YUV420P = 0; // Planar YUV 4:2:0
200
int AV_PIX_FMT_RGB24 = 2; // Packed RGB 8:8:8
201
int AV_PIX_FMT_BGR24 = 3; // Packed BGR 8:8:8
202
int AV_PIX_FMT_YUV422P = 4; // Planar YUV 4:2:2
203
int AV_PIX_FMT_YUV444P = 5; // Planar YUV 4:4:4
204
int AV_PIX_FMT_RGBA = 26; // Packed RGBA 8:8:8:8
205
int AV_PIX_FMT_BGRA = 27; // Packed BGRA 8:8:8:8
206
int AV_PIX_FMT_NV12 = 23; // Semi-planar YUV 4:2:0
207
int AV_PIX_FMT_NV21 = 24; // Semi-planar YUV 4:2:0
208
```
209
210
### Color Space Constants
211
212
```java { .api }
213
// Color space standards
214
int SWS_CS_ITU709 = 1; // ITU-R BT.709
215
int SWS_CS_FCC = 4; // FCC
216
int SWS_CS_ITU601 = 5; // ITU-R BT.601
217
int SWS_CS_SMPTE170M = 6; // SMPTE-170M
218
int SWS_CS_SMPTE240M = 7; // SMPTE-240M
219
int SWS_CS_DEFAULT = 5; // Default (BT.601)
220
```