]> git.sesse.net Git - ffmpeg/blob - libswscale/slice.c
Merge commit '2c32eace5ec4d1d7ca4e0220856cd2815ccc71b2'
[ffmpeg] / libswscale / slice.c
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
21 #include "swscale_internal.h"
22
23 static void free_lines(SwsSlice *s)
24 {
25     int i;
26     for (i = 0; i < 2; ++i) {
27         int n = s->plane[i].available_lines;
28         int j;
29         for (j = 0; j < n; ++j) {
30             av_freep(&s->plane[i].line[j]);
31             if (s->is_ring)
32                s->plane[i].line[j+n] = NULL;
33         }
34     }
35
36     for (i = 0; i < 4; ++i)
37         memset(s->plane[i].line, 0, sizeof(uint8_t*) * s->plane[i].available_lines * (s->is_ring ? 3 : 1));
38     s->should_free_lines = 0;
39 }
40
41 /*
42  slice lines contains extra bytes for vetorial code thus @size
43  is the allocated memory size and @width is the number of pixels
44 */
45 static int alloc_lines(SwsSlice *s, int size, int width)
46 {
47     int i;
48     int idx[2] = {3, 2};
49
50     s->should_free_lines = 1;
51     s->width = width;
52
53     for (i = 0; i < 2; ++i) {
54         int n = s->plane[i].available_lines;
55         int j;
56         int ii = idx[i];
57
58         av_assert0(n == s->plane[ii].available_lines);
59         for (j = 0; j < n; ++j) {
60             // chroma plane line U and V are expected to be contiguous in memory
61             // by mmx vertical scaler code
62             s->plane[i].line[j] = av_malloc(size * 2 + 32);
63             if (!s->plane[i].line[j]) {
64                 free_lines(s);
65                 return AVERROR(ENOMEM);
66             }
67             s->plane[ii].line[j] = s->plane[i].line[j] + size + 16;
68             if (s->is_ring) {
69                s->plane[i].line[j+n] = s->plane[i].line[j];
70                s->plane[ii].line[j+n] = s->plane[ii].line[j];
71             }
72         }
73     }
74
75     return 0;
76 }
77
78 static int alloc_slice(SwsSlice *s, enum AVPixelFormat fmt, int lumLines, int chrLines, int h_sub_sample, int v_sub_sample, int ring)
79 {
80     int i;
81     int size[4] = { lumLines,
82                     chrLines,
83                     chrLines,
84                     lumLines };
85
86     s->h_chr_sub_sample = h_sub_sample;
87     s->v_chr_sub_sample = v_sub_sample;
88     s->fmt = fmt;
89     s->is_ring = ring;
90     s->should_free_lines = 0;
91
92     for (i = 0; i < 4; ++i) {
93         int n = size[i] * ( ring == 0 ? 1 : 3);
94         s->plane[i].line = av_mallocz_array(sizeof(uint8_t*), n);
95         if (!s->plane[i].line)
96             return AVERROR(ENOMEM);
97
98         s->plane[i].tmp = ring ? s->plane[i].line + size[i] * 2 : NULL;
99         s->plane[i].available_lines = size[i];
100         s->plane[i].sliceY = 0;
101         s->plane[i].sliceH = 0;
102     }
103     return 0;
104 }
105
106 static void free_slice(SwsSlice *s)
107 {
108     int i;
109     if (s) {
110         if (s->should_free_lines)
111             free_lines(s);
112         for (i = 0; i < 4; ++i) {
113             av_freep(&s->plane[i].line);
114             s->plane[i].tmp = NULL;
115         }
116     }
117 }
118
119 int ff_rotate_slice(SwsSlice *s, int lum, int chr)
120 {
121     int i;
122     if (lum) {
123         for (i = 0; i < 4; i+=3) {
124             int n = s->plane[i].available_lines;
125             int l = lum - s->plane[i].sliceY;
126
127             if (l >= n * 2) {
128                 s->plane[i].sliceY += n;
129                 s->plane[i].sliceH -= n;
130             }
131         }
132     }
133     if (chr) {
134         for (i = 1; i < 3; ++i) {
135             int n = s->plane[i].available_lines;
136             int l = chr - s->plane[i].sliceY;
137
138             if (l >= n * 2) {
139                 s->plane[i].sliceY += n;
140                 s->plane[i].sliceH -= n;
141             }
142         }
143     }
144     return 0;
145 }
146
147 int ff_init_slice_from_src(SwsSlice * s, uint8_t *src[4], int stride[4], int srcW, int lumY, int lumH, int chrY, int chrH)
148 {
149     int i = 0;
150
151     const int start[4] = {lumY,
152                     chrY,
153                     chrY,
154                     lumY};
155
156     const int end[4] = {lumY +lumH,
157                         chrY + chrH,
158                         chrY + chrH,
159                         lumY + lumH};
160
161     s->width = srcW;
162
163     for (i = 0; i < 4; ++i) {
164         int j;
165         int lines = end[i];
166         lines = s->plane[i].available_lines < lines ? s->plane[i].available_lines : lines;
167
168         if (end[i] > s->plane[i].sliceY+s->plane[i].sliceH) {
169             if (start[i] <= s->plane[i].sliceY+1)
170                 s->plane[i].sliceY = FFMIN(start[i], s->plane[i].sliceY);
171             else
172                 s->plane[i].sliceY = start[i];
173             s->plane[i].sliceH = end[i] - s->plane[i].sliceY;
174         } else {
175             if (end[i] >= s->plane[i].sliceY)
176                 s->plane[i].sliceH = s->plane[i].sliceY + s->plane[i].sliceH - start[i];
177             else
178                 s->plane[i].sliceH = end[i] - start[i];
179             s->plane[i].sliceY = start[i];
180         }
181
182         for (j = start[i]; j < lines; j+= 1)
183             s->plane[i].line[j] = src[i] + (start[i] + j) * stride[i];
184
185     }
186
187     return 0;
188 }
189
190 static void fill_ones(SwsSlice *s, int n, int is16bit)
191 {
192     int i;
193     for (i = 0; i < 4; ++i) {
194         int j;
195         int size = s->plane[i].available_lines;
196         for (j = 0; j < size; ++j) {
197             int k;
198             int end = is16bit ? n>>1: n;
199             // fill also one extra element
200             end += 1;
201             if (is16bit)
202                 for (k = 0; k < end; ++k)
203                     ((int32_t*)(s->plane[i].line[j]))[k] = 1<<18;
204             else
205                 for (k = 0; k < end; ++k)
206                     ((int16_t*)(s->plane[i].line[j]))[k] = 1<<14;
207         }
208     }
209 }
210
211 int ff_init_filters(SwsContext * c)
212 {
213     int i;
214     int index;
215     int num_ydesc;
216     int num_cdesc;
217     int num_vdesc = isPlanarYUV(c->dstFormat) && !isGray(c->dstFormat) ? 2 : 1;
218     int need_lum_conv = c->lumToYV12 || c->readLumPlanar || c->alpToYV12 || c->readAlpPlanar;
219     int need_chr_conv = c->chrToYV12 || c->readChrPlanar;
220     int need_gamma = c->is_internal_gamma;
221     int srcIdx, dstIdx;
222     int dst_stride = FFALIGN(c->dstW * sizeof(int16_t) + 66, 16);
223
224     uint32_t * pal = usePal(c->srcFormat) ? c->pal_yuv : (uint32_t*)c->input_rgb2yuv_table;
225     int res = 0;
226
227     if (c->dstBpc == 16)
228         dst_stride <<= 1;
229
230     num_ydesc = need_lum_conv ? 2 : 1;
231     num_cdesc = need_chr_conv ? 2 : 1;
232
233     c->numSlice = FFMAX(num_ydesc, num_cdesc) + 2;
234     c->numDesc = num_ydesc + num_cdesc + num_vdesc + (need_gamma ? 2 : 0);
235     c->descIndex[0] = num_ydesc + (need_gamma ? 1 : 0);
236     c->descIndex[1] = num_ydesc + num_cdesc + (need_gamma ? 1 : 0);
237
238
239
240     c->desc = av_mallocz_array(sizeof(SwsFilterDescriptor), c->numDesc);
241     if (!c->desc)
242         return AVERROR(ENOMEM);
243     c->slice = av_mallocz_array(sizeof(SwsSlice), c->numSlice);
244
245
246     res = alloc_slice(&c->slice[0], c->srcFormat, c->srcH, c->chrSrcH, c->chrSrcHSubSample, c->chrSrcVSubSample, 0);
247     if (res < 0) goto cleanup;
248     for (i = 1; i < c->numSlice-2; ++i) {
249         res = alloc_slice(&c->slice[i], c->srcFormat, c->vLumFilterSize + MAX_LINES_AHEAD, c->vChrFilterSize + MAX_LINES_AHEAD, c->chrSrcHSubSample, c->chrSrcVSubSample, 0);
250         if (res < 0) goto cleanup;
251         res = alloc_lines(&c->slice[i], FFALIGN(c->srcW*2+78, 16), c->srcW);
252         if (res < 0) goto cleanup;
253     }
254     // horizontal scaler output
255     res = alloc_slice(&c->slice[i], c->srcFormat, c->vLumFilterSize + MAX_LINES_AHEAD, c->vChrFilterSize + MAX_LINES_AHEAD, c->chrDstHSubSample, c->chrDstVSubSample, 1);
256     if (res < 0) goto cleanup;
257     res = alloc_lines(&c->slice[i], dst_stride, c->dstW);
258     if (res < 0) goto cleanup;
259
260     fill_ones(&c->slice[i], dst_stride>>1, c->dstBpc == 16);
261
262     // vertical scaler output
263     ++i;
264     res = alloc_slice(&c->slice[i], c->dstFormat, c->dstH, c->chrDstH, c->chrDstHSubSample, c->chrDstVSubSample, 0);
265     if (res < 0) goto cleanup;
266
267     index = 0;
268     srcIdx = 0;
269     dstIdx = 1;
270
271     if (need_gamma) {
272         res = ff_init_gamma_convert(c->desc + index, c->slice + srcIdx, c->inv_gamma);
273         if (res < 0) goto cleanup;
274         ++index;
275     }
276
277     if (need_lum_conv) {
278         res = ff_init_desc_fmt_convert(&c->desc[index], &c->slice[srcIdx], &c->slice[dstIdx], pal);
279         if (res < 0) goto cleanup;
280         c->desc[index].alpha = c->alpPixBuf != 0;
281         ++index;
282         srcIdx = dstIdx;
283     }
284
285
286     dstIdx = FFMAX(num_ydesc, num_cdesc);
287     res = ff_init_desc_hscale(&c->desc[index], &c->slice[srcIdx], &c->slice[dstIdx], c->hLumFilter, c->hLumFilterPos, c->hLumFilterSize, c->lumXInc);
288     if (res < 0) goto cleanup;
289     c->desc[index].alpha = c->alpPixBuf != 0;
290
291
292     ++index;
293     {
294         srcIdx = 0;
295         dstIdx = 1;
296         if (need_chr_conv) {
297             res = ff_init_desc_cfmt_convert(&c->desc[index], &c->slice[srcIdx], &c->slice[dstIdx], pal);
298             if (res < 0) goto cleanup;
299             ++index;
300             srcIdx = dstIdx;
301         }
302
303         dstIdx = FFMAX(num_ydesc, num_cdesc);
304         if (c->needs_hcscale)
305             res = ff_init_desc_chscale(&c->desc[index], &c->slice[srcIdx], &c->slice[dstIdx], c->hChrFilter, c->hChrFilterPos, c->hChrFilterSize, c->chrXInc);
306         else
307             res = ff_init_desc_no_chr(&c->desc[index], &c->slice[srcIdx], &c->slice[dstIdx]);
308         if (res < 0) goto cleanup;
309     }
310
311     ++index;
312     {
313         srcIdx = c->numSlice - 2;
314         dstIdx = c->numSlice - 1;
315         res = ff_init_vscale(c, c->desc + index, c->slice + srcIdx, c->slice + dstIdx);
316         if (res < 0) goto cleanup;
317     }
318
319     ++index;
320     if (need_gamma) {
321         res = ff_init_gamma_convert(c->desc + index, c->slice + dstIdx, c->gamma);
322         if (res < 0) goto cleanup;
323     }
324
325     return 0;
326
327 cleanup:
328     ff_free_filters(c);
329     return res;
330 }
331
332 int ff_free_filters(SwsContext *c)
333 {
334     int i;
335     if (c->desc) {
336         for (i = 0; i < c->numDesc; ++i)
337             av_freep(&c->desc[i].instance);
338         av_freep(&c->desc);
339     }
340
341     if (c->slice) {
342         for (i = 0; i < c->numSlice; ++i)
343             free_slice(&c->slice[i]);
344         av_freep(&c->slice);
345     }
346     return 0;
347 }