/src/ffmpeg/libswscale/vscale.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright (C) 2015 Pedro Arthur <bygrandao@gmail.com> |
3 | | * |
4 | | * This file is part of FFmpeg. |
5 | | * |
6 | | * FFmpeg is free software; you can redistribute it and/or |
7 | | * modify it under the terms of the GNU Lesser General Public |
8 | | * License as published by the Free Software Foundation; either |
9 | | * version 2.1 of the License, or (at your option) any later version. |
10 | | * |
11 | | * FFmpeg is distributed in the hope that it will be useful, |
12 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14 | | * Lesser General Public License for more details. |
15 | | * |
16 | | * You should have received a copy of the GNU Lesser General Public |
17 | | * License along with FFmpeg; if not, write to the Free Software |
18 | | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
19 | | */ |
20 | | #include "libavutil/mem.h" |
21 | | #include "swscale_internal.h" |
22 | | |
23 | | typedef struct VScalerContext |
24 | | { |
25 | | uint16_t *filter[2]; |
26 | | int32_t *filter_pos; |
27 | | int filter_size; |
28 | | int isMMX; |
29 | | union { |
30 | | yuv2planar1_fn yuv2planar1; |
31 | | yuv2planarX_fn yuv2planarX; |
32 | | yuv2interleavedX_fn yuv2interleavedX; |
33 | | yuv2packed1_fn yuv2packed1; |
34 | | yuv2packed2_fn yuv2packed2; |
35 | | yuv2anyX_fn yuv2anyX; |
36 | | } pfn; |
37 | | yuv2packedX_fn yuv2packedX; |
38 | | } VScalerContext; |
39 | | |
40 | | |
41 | | static int lum_planar_vscale(SwsInternal *c, SwsFilterDescriptor *desc, int sliceY, int sliceH) |
42 | 0 | { |
43 | 0 | VScalerContext *inst = desc->instance; |
44 | 0 | int dstW = desc->dst->width; |
45 | |
|
46 | 0 | int first = FFMAX(1-inst->filter_size, inst->filter_pos[sliceY]); |
47 | 0 | int sp = first - desc->src->plane[0].sliceY; |
48 | 0 | int dp = sliceY - desc->dst->plane[0].sliceY; |
49 | 0 | uint8_t **src = desc->src->plane[0].line + sp; |
50 | 0 | uint8_t **dst = desc->dst->plane[0].line + dp; |
51 | 0 | uint16_t *filter = inst->filter[0] + (inst->isMMX ? 0 : sliceY * inst->filter_size); |
52 | |
|
53 | 0 | if (inst->filter_size == 1) |
54 | 0 | inst->pfn.yuv2planar1((const int16_t*)src[0], dst[0], dstW, c->lumDither8, 0); |
55 | 0 | else |
56 | 0 | inst->pfn.yuv2planarX(filter, inst->filter_size, (const int16_t**)src, dst[0], dstW, c->lumDither8, 0); |
57 | |
|
58 | 0 | if (desc->alpha) { |
59 | 0 | int sp = first - desc->src->plane[3].sliceY; |
60 | 0 | int dp = sliceY - desc->dst->plane[3].sliceY; |
61 | 0 | uint8_t **src = desc->src->plane[3].line + sp; |
62 | 0 | uint8_t **dst = desc->dst->plane[3].line + dp; |
63 | 0 | uint16_t *filter = inst->filter[1] + (inst->isMMX ? 0 : sliceY * inst->filter_size); |
64 | |
|
65 | 0 | if (inst->filter_size == 1) |
66 | 0 | inst->pfn.yuv2planar1((const int16_t*)src[0], dst[0], dstW, c->lumDither8, 0); |
67 | 0 | else |
68 | 0 | inst->pfn.yuv2planarX(filter, inst->filter_size, (const int16_t**)src, dst[0], dstW, c->lumDither8, 0); |
69 | 0 | } |
70 | |
|
71 | 0 | return 1; |
72 | 0 | } |
73 | | |
74 | | static int chr_planar_vscale(SwsInternal *c, SwsFilterDescriptor *desc, int sliceY, int sliceH) |
75 | 0 | { |
76 | 0 | const int chrSkipMask = (1 << desc->dst->v_chr_sub_sample) - 1; |
77 | 0 | if (sliceY & chrSkipMask) |
78 | 0 | return 0; |
79 | 0 | else { |
80 | 0 | VScalerContext *inst = desc->instance; |
81 | 0 | int dstW = AV_CEIL_RSHIFT(desc->dst->width, desc->dst->h_chr_sub_sample); |
82 | 0 | int chrSliceY = sliceY >> desc->dst->v_chr_sub_sample; |
83 | |
|
84 | 0 | int first = FFMAX(1-inst->filter_size, inst->filter_pos[chrSliceY]); |
85 | 0 | int sp1 = first - desc->src->plane[1].sliceY; |
86 | 0 | int sp2 = first - desc->src->plane[2].sliceY; |
87 | 0 | int dp1 = chrSliceY - desc->dst->plane[1].sliceY; |
88 | 0 | int dp2 = chrSliceY - desc->dst->plane[2].sliceY; |
89 | 0 | uint8_t **src1 = desc->src->plane[1].line + sp1; |
90 | 0 | uint8_t **src2 = desc->src->plane[2].line + sp2; |
91 | 0 | uint8_t **dst1 = desc->dst->plane[1].line + dp1; |
92 | 0 | uint8_t **dst2 = desc->dst->plane[2].line + dp2; |
93 | 0 | uint16_t *filter = inst->filter[0] + (inst->isMMX ? 0 : chrSliceY * inst->filter_size); |
94 | |
|
95 | 0 | if (c->yuv2nv12cX) { |
96 | 0 | inst->pfn.yuv2interleavedX(c->opts.dst_format, c->chrDither8, filter, inst->filter_size, (const int16_t**)src1, (const int16_t**)src2, dst1[0], dstW); |
97 | 0 | } else if (inst->filter_size == 1) { |
98 | 0 | inst->pfn.yuv2planar1((const int16_t*)src1[0], dst1[0], dstW, c->chrDither8, 0); |
99 | 0 | inst->pfn.yuv2planar1((const int16_t*)src2[0], dst2[0], dstW, c->chrDither8, 3); |
100 | 0 | } else { |
101 | 0 | inst->pfn.yuv2planarX(filter, inst->filter_size, (const int16_t**)src1, dst1[0], dstW, c->chrDither8, 0); |
102 | 0 | inst->pfn.yuv2planarX(filter, inst->filter_size, (const int16_t**)src2, dst2[0], dstW, c->chrDither8, inst->isMMX ? (c->uv_offx2 >> 1) : 3); |
103 | 0 | } |
104 | 0 | } |
105 | | |
106 | 0 | return 1; |
107 | 0 | } |
108 | | |
109 | | static int packed_vscale(SwsInternal *c, SwsFilterDescriptor *desc, int sliceY, int sliceH) |
110 | 0 | { |
111 | 0 | VScalerContext *inst = desc->instance; |
112 | 0 | int dstW = desc->dst->width; |
113 | 0 | int chrSliceY = sliceY >> desc->dst->v_chr_sub_sample; |
114 | |
|
115 | 0 | int lum_fsize = inst[0].filter_size; |
116 | 0 | int chr_fsize = inst[1].filter_size; |
117 | 0 | uint16_t *lum_filter = inst[0].filter[0]; |
118 | 0 | uint16_t *chr_filter = inst[1].filter[0]; |
119 | |
|
120 | 0 | int firstLum = FFMAX(1-lum_fsize, inst[0].filter_pos[ sliceY]); |
121 | 0 | int firstChr = FFMAX(1-chr_fsize, inst[1].filter_pos[chrSliceY]); |
122 | |
|
123 | 0 | int sp0 = firstLum - desc->src->plane[0].sliceY; |
124 | 0 | int sp1 = firstChr - desc->src->plane[1].sliceY; |
125 | 0 | int sp2 = firstChr - desc->src->plane[2].sliceY; |
126 | 0 | int sp3 = firstLum - desc->src->plane[3].sliceY; |
127 | 0 | int dp = sliceY - desc->dst->plane[0].sliceY; |
128 | 0 | uint8_t **src0 = desc->src->plane[0].line + sp0; |
129 | 0 | uint8_t **src1 = desc->src->plane[1].line + sp1; |
130 | 0 | uint8_t **src2 = desc->src->plane[2].line + sp2; |
131 | 0 | uint8_t **src3 = desc->alpha ? desc->src->plane[3].line + sp3 : NULL; |
132 | 0 | uint8_t **dst = desc->dst->plane[0].line + dp; |
133 | | |
134 | |
|
135 | 0 | if (c->yuv2packed1 && lum_fsize == 1 && chr_fsize == 1) { // unscaled RGB |
136 | 0 | inst->pfn.yuv2packed1(c, (const int16_t*)*src0, (const int16_t**)src1, (const int16_t**)src2, |
137 | 0 | (const int16_t*)(desc->alpha ? *src3 : NULL), *dst, dstW, 0, sliceY); |
138 | 0 | } else if (c->yuv2packed1 && lum_fsize == 1 && chr_fsize == 2 && |
139 | 0 | chr_filter[2 * chrSliceY + 1] + chr_filter[2 * chrSliceY] == 4096 && |
140 | 0 | chr_filter[2 * chrSliceY + 1] <= 4096U) { // unscaled RGB |
141 | 0 | int chrAlpha = chr_filter[2 * chrSliceY + 1]; |
142 | 0 | inst->pfn.yuv2packed1(c, (const int16_t*)*src0, (const int16_t**)src1, (const int16_t**)src2, |
143 | 0 | (const int16_t*)(desc->alpha ? *src3 : NULL), *dst, dstW, chrAlpha, sliceY); |
144 | 0 | } else if (c->yuv2packed2 && lum_fsize == 2 && chr_fsize == 2 && |
145 | 0 | lum_filter[2 * sliceY + 1] + lum_filter[2 * sliceY] == 4096 && |
146 | 0 | lum_filter[2 * sliceY + 1] <= 4096U && |
147 | 0 | chr_filter[2 * chrSliceY + 1] + chr_filter[2 * chrSliceY] == 4096 && |
148 | 0 | chr_filter[2 * chrSliceY + 1] <= 4096U |
149 | 0 | ) { // bilinear upscale RGB |
150 | 0 | int lumAlpha = lum_filter[2 * sliceY + 1]; |
151 | 0 | int chrAlpha = chr_filter[2 * chrSliceY + 1]; |
152 | 0 | c->lumMmxFilter[2] = |
153 | 0 | c->lumMmxFilter[3] = lum_filter[2 * sliceY] * 0x10001; |
154 | 0 | c->chrMmxFilter[2] = |
155 | 0 | c->chrMmxFilter[3] = chr_filter[2 * chrSliceY] * 0x10001; |
156 | 0 | inst->pfn.yuv2packed2(c, (const int16_t**)src0, (const int16_t**)src1, (const int16_t**)src2, (const int16_t**)src3, |
157 | 0 | *dst, dstW, lumAlpha, chrAlpha, sliceY); |
158 | 0 | } else { // general RGB |
159 | 0 | if ((c->yuv2packed1 && lum_fsize == 1 && chr_fsize == 2) || |
160 | 0 | (c->yuv2packed2 && lum_fsize == 2 && chr_fsize == 2)) { |
161 | 0 | if (!c->warned_unuseable_bilinear) |
162 | 0 | av_log(c, AV_LOG_INFO, "Optimized 2 tap filter code cannot be used\n"); |
163 | 0 | c->warned_unuseable_bilinear = 1; |
164 | 0 | } |
165 | |
|
166 | 0 | inst->yuv2packedX(c, lum_filter + sliceY * lum_fsize, |
167 | 0 | (const int16_t**)src0, lum_fsize, chr_filter + chrSliceY * chr_fsize, |
168 | 0 | (const int16_t**)src1, (const int16_t**)src2, chr_fsize, (const int16_t**)src3, *dst, dstW, sliceY); |
169 | 0 | } |
170 | 0 | return 1; |
171 | 0 | } |
172 | | |
173 | | static int any_vscale(SwsInternal *c, SwsFilterDescriptor *desc, int sliceY, int sliceH) |
174 | 0 | { |
175 | 0 | VScalerContext *inst = desc->instance; |
176 | 0 | int dstW = desc->dst->width; |
177 | 0 | int chrSliceY = sliceY >> desc->dst->v_chr_sub_sample; |
178 | |
|
179 | 0 | int lum_fsize = inst[0].filter_size; |
180 | 0 | int chr_fsize = inst[1].filter_size; |
181 | 0 | uint16_t *lum_filter = inst[0].filter[0]; |
182 | 0 | uint16_t *chr_filter = inst[1].filter[0]; |
183 | |
|
184 | 0 | int firstLum = FFMAX(1-lum_fsize, inst[0].filter_pos[ sliceY]); |
185 | 0 | int firstChr = FFMAX(1-chr_fsize, inst[1].filter_pos[chrSliceY]); |
186 | |
|
187 | 0 | int sp0 = firstLum - desc->src->plane[0].sliceY; |
188 | 0 | int sp1 = firstChr - desc->src->plane[1].sliceY; |
189 | 0 | int sp2 = firstChr - desc->src->plane[2].sliceY; |
190 | 0 | int sp3 = firstLum - desc->src->plane[3].sliceY; |
191 | 0 | int dp0 = sliceY - desc->dst->plane[0].sliceY; |
192 | 0 | int dp1 = chrSliceY - desc->dst->plane[1].sliceY; |
193 | 0 | int dp2 = chrSliceY - desc->dst->plane[2].sliceY; |
194 | 0 | int dp3 = sliceY - desc->dst->plane[3].sliceY; |
195 | |
|
196 | 0 | uint8_t **src0 = desc->src->plane[0].line + sp0; |
197 | 0 | uint8_t **src1 = desc->src->plane[1].line + sp1; |
198 | 0 | uint8_t **src2 = desc->src->plane[2].line + sp2; |
199 | 0 | uint8_t **src3 = desc->alpha ? desc->src->plane[3].line + sp3 : NULL; |
200 | 0 | uint8_t *dst[4] = { desc->dst->plane[0].line[dp0], |
201 | 0 | desc->dst->plane[1].line[dp1], |
202 | 0 | desc->dst->plane[2].line[dp2], |
203 | 0 | desc->alpha ? desc->dst->plane[3].line[dp3] : NULL }; |
204 | |
|
205 | 0 | av_assert1(!c->yuv2packed1 && !c->yuv2packed2); |
206 | 0 | inst->pfn.yuv2anyX(c, lum_filter + sliceY * lum_fsize, |
207 | 0 | (const int16_t**)src0, lum_fsize, chr_filter + sliceY * chr_fsize, |
208 | 0 | (const int16_t**)src1, (const int16_t**)src2, chr_fsize, (const int16_t**)src3, dst, dstW, sliceY); |
209 | |
|
210 | 0 | return 1; |
211 | |
|
212 | 0 | } |
213 | | |
214 | | int ff_init_vscale(SwsInternal *c, SwsFilterDescriptor *desc, SwsSlice *src, SwsSlice *dst) |
215 | 0 | { |
216 | 0 | VScalerContext *lumCtx = NULL; |
217 | 0 | VScalerContext *chrCtx = NULL; |
218 | |
|
219 | 0 | if (isPlanarYUV(c->opts.dst_format) || (isGray(c->opts.dst_format) && !isALPHA(c->opts.dst_format))) { |
220 | 0 | lumCtx = av_mallocz(sizeof(VScalerContext)); |
221 | 0 | if (!lumCtx) |
222 | 0 | return AVERROR(ENOMEM); |
223 | | |
224 | | |
225 | 0 | desc[0].process = lum_planar_vscale; |
226 | 0 | desc[0].instance = lumCtx; |
227 | 0 | desc[0].src = src; |
228 | 0 | desc[0].dst = dst; |
229 | 0 | desc[0].alpha = c->needAlpha; |
230 | |
|
231 | 0 | if (!isGray(c->opts.dst_format)) { |
232 | 0 | chrCtx = av_mallocz(sizeof(VScalerContext)); |
233 | 0 | if (!chrCtx) |
234 | 0 | return AVERROR(ENOMEM); |
235 | 0 | desc[1].process = chr_planar_vscale; |
236 | 0 | desc[1].instance = chrCtx; |
237 | 0 | desc[1].src = src; |
238 | 0 | desc[1].dst = dst; |
239 | 0 | } |
240 | 0 | } else { |
241 | 0 | lumCtx = av_calloc(2, sizeof(*lumCtx)); |
242 | 0 | if (!lumCtx) |
243 | 0 | return AVERROR(ENOMEM); |
244 | 0 | chrCtx = &lumCtx[1]; |
245 | |
|
246 | 0 | desc[0].process = c->yuv2packedX ? packed_vscale : any_vscale; |
247 | 0 | desc[0].instance = lumCtx; |
248 | 0 | desc[0].src = src; |
249 | 0 | desc[0].dst = dst; |
250 | 0 | desc[0].alpha = c->needAlpha; |
251 | 0 | } |
252 | | |
253 | 0 | ff_init_vscale_pfn(c, c->yuv2plane1, c->yuv2planeX, c->yuv2nv12cX, |
254 | 0 | c->yuv2packed1, c->yuv2packed2, c->yuv2packedX, c->yuv2anyX, c->use_mmx_vfilter); |
255 | 0 | return 0; |
256 | 0 | } |
257 | | |
258 | | void ff_init_vscale_pfn(SwsInternal *c, |
259 | | yuv2planar1_fn yuv2plane1, |
260 | | yuv2planarX_fn yuv2planeX, |
261 | | yuv2interleavedX_fn yuv2nv12cX, |
262 | | yuv2packed1_fn yuv2packed1, |
263 | | yuv2packed2_fn yuv2packed2, |
264 | | yuv2packedX_fn yuv2packedX, |
265 | | yuv2anyX_fn yuv2anyX, int use_mmx) |
266 | 0 | { |
267 | 0 | VScalerContext *lumCtx = NULL; |
268 | 0 | VScalerContext *chrCtx = NULL; |
269 | 0 | int idx = c->numDesc - (c->is_internal_gamma ? 2 : 1); //FIXME avoid hardcoding indexes |
270 | |
|
271 | 0 | if (isPlanarYUV(c->opts.dst_format) || (isGray(c->opts.dst_format) && !isALPHA(c->opts.dst_format))) { |
272 | 0 | if (!isGray(c->opts.dst_format)) { |
273 | 0 | chrCtx = c->desc[idx].instance; |
274 | |
|
275 | 0 | chrCtx->filter[0] = use_mmx ? (int16_t*)c->chrMmxFilter : c->vChrFilter; |
276 | 0 | chrCtx->filter_size = c->vChrFilterSize; |
277 | 0 | chrCtx->filter_pos = c->vChrFilterPos; |
278 | 0 | chrCtx->isMMX = use_mmx; |
279 | |
|
280 | 0 | --idx; |
281 | 0 | if (yuv2nv12cX) chrCtx->pfn.yuv2interleavedX = yuv2nv12cX; |
282 | 0 | else if (c->vChrFilterSize == 1) chrCtx->pfn.yuv2planar1 = yuv2plane1; |
283 | 0 | else chrCtx->pfn.yuv2planarX = yuv2planeX; |
284 | 0 | } |
285 | |
|
286 | 0 | lumCtx = c->desc[idx].instance; |
287 | |
|
288 | 0 | lumCtx->filter[0] = use_mmx ? (int16_t*)c->lumMmxFilter : c->vLumFilter; |
289 | 0 | lumCtx->filter[1] = use_mmx ? (int16_t*)c->alpMmxFilter : c->vLumFilter; |
290 | 0 | lumCtx->filter_size = c->vLumFilterSize; |
291 | 0 | lumCtx->filter_pos = c->vLumFilterPos; |
292 | 0 | lumCtx->isMMX = use_mmx; |
293 | |
|
294 | 0 | if (c->vLumFilterSize == 1) lumCtx->pfn.yuv2planar1 = yuv2plane1; |
295 | 0 | else lumCtx->pfn.yuv2planarX = yuv2planeX; |
296 | |
|
297 | 0 | } else { |
298 | 0 | lumCtx = c->desc[idx].instance; |
299 | 0 | chrCtx = &lumCtx[1]; |
300 | |
|
301 | 0 | lumCtx->filter[0] = c->vLumFilter; |
302 | 0 | lumCtx->filter_size = c->vLumFilterSize; |
303 | 0 | lumCtx->filter_pos = c->vLumFilterPos; |
304 | |
|
305 | 0 | chrCtx->filter[0] = c->vChrFilter; |
306 | 0 | chrCtx->filter_size = c->vChrFilterSize; |
307 | 0 | chrCtx->filter_pos = c->vChrFilterPos; |
308 | |
|
309 | 0 | lumCtx->isMMX = use_mmx; |
310 | 0 | chrCtx->isMMX = use_mmx; |
311 | |
|
312 | 0 | if (yuv2packedX) { |
313 | 0 | if (c->yuv2packed1 && c->vLumFilterSize == 1 && c->vChrFilterSize <= 2) |
314 | 0 | lumCtx->pfn.yuv2packed1 = yuv2packed1; |
315 | 0 | else if (c->yuv2packed2 && c->vLumFilterSize == 2 && c->vChrFilterSize == 2) |
316 | 0 | lumCtx->pfn.yuv2packed2 = yuv2packed2; |
317 | 0 | lumCtx->yuv2packedX = yuv2packedX; |
318 | 0 | } else |
319 | 0 | lumCtx->pfn.yuv2anyX = yuv2anyX; |
320 | 0 | } |
321 | 0 | } |