LCOV - code coverage report
Current view: top level - libswscale - slice.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 201 218 92.2 %
Date: 2017-10-24 00:14:43 Functions: 10 10 100.0 %

          Line data    Source code
       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        2650 : static void free_lines(SwsSlice *s)
      24             : {
      25             :     int i;
      26        7950 :     for (i = 0; i < 2; ++i) {
      27        5300 :         int n = s->plane[i].available_lines;
      28             :         int j;
      29       49877 :         for (j = 0; j < n; ++j) {
      30       44577 :             av_freep(&s->plane[i].line[j]);
      31       44577 :             if (s->is_ring)
      32       39253 :                s->plane[i].line[j+n] = NULL;
      33             :         }
      34             :     }
      35             : 
      36       13250 :     for (i = 0; i < 4; ++i)
      37       10600 :         memset(s->plane[i].line, 0, sizeof(uint8_t*) * s->plane[i].available_lines * (s->is_ring ? 3 : 1));
      38        2650 :     s->should_free_lines = 0;
      39        2650 : }
      40             : 
      41             : /*
      42             :  slice lines contains extra bytes for vectorial code thus @size
      43             :  is the allocated memory size and @width is the number of pixels
      44             : */
      45        2650 : static int alloc_lines(SwsSlice *s, int size, int width)
      46             : {
      47             :     int i;
      48        2650 :     int idx[2] = {3, 2};
      49             : 
      50        2650 :     s->should_free_lines = 1;
      51        2650 :     s->width = width;
      52             : 
      53        7950 :     for (i = 0; i < 2; ++i) {
      54        5300 :         int n = s->plane[i].available_lines;
      55             :         int j;
      56        5300 :         int ii = idx[i];
      57             : 
      58        5300 :         av_assert0(n == s->plane[ii].available_lines);
      59       49877 :         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       44577 :             s->plane[i].line[j] = av_malloc(size * 2 + 32);
      63       44577 :             if (!s->plane[i].line[j]) {
      64           0 :                 free_lines(s);
      65           0 :                 return AVERROR(ENOMEM);
      66             :             }
      67       44577 :             s->plane[ii].line[j] = s->plane[i].line[j] + size + 16;
      68       44577 :             if (s->is_ring) {
      69       39253 :                s->plane[i].line[j+n] = s->plane[i].line[j];
      70       39253 :                s->plane[ii].line[j+n] = s->plane[ii].line[j];
      71             :             }
      72             :         }
      73             :     }
      74             : 
      75        2650 :     return 0;
      76             : }
      77             : 
      78        7462 : 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        7462 :     int size[4] = { lumLines,
      82             :                     chrLines,
      83             :                     chrLines,
      84             :                     lumLines };
      85             : 
      86        7462 :     s->h_chr_sub_sample = h_sub_sample;
      87        7462 :     s->v_chr_sub_sample = v_sub_sample;
      88        7462 :     s->fmt = fmt;
      89        7462 :     s->is_ring = ring;
      90        7462 :     s->should_free_lines = 0;
      91             : 
      92       37310 :     for (i = 0; i < 4; ++i) {
      93       29848 :         int n = size[i] * ( ring == 0 ? 1 : 3);
      94       29848 :         s->plane[i].line = av_mallocz_array(sizeof(uint8_t*), n);
      95       29848 :         if (!s->plane[i].line)
      96           0 :             return AVERROR(ENOMEM);
      97             : 
      98       29848 :         s->plane[i].tmp = ring ? s->plane[i].line + size[i] * 2 : NULL;
      99       29848 :         s->plane[i].available_lines = size[i];
     100       29848 :         s->plane[i].sliceY = 0;
     101       29848 :         s->plane[i].sliceH = 0;
     102             :     }
     103        7462 :     return 0;
     104             : }
     105             : 
     106        7462 : static void free_slice(SwsSlice *s)
     107             : {
     108             :     int i;
     109        7462 :     if (s) {
     110        7462 :         if (s->should_free_lines)
     111        2650 :             free_lines(s);
     112       37310 :         for (i = 0; i < 4; ++i) {
     113       29848 :             av_freep(&s->plane[i].line);
     114       29848 :             s->plane[i].tmp = NULL;
     115             :         }
     116             :     }
     117        7462 : }
     118             : 
     119     9931546 : int ff_rotate_slice(SwsSlice *s, int lum, int chr)
     120             : {
     121             :     int i;
     122     9931546 :     if (lum) {
     123    29791488 :         for (i = 0; i < 4; i+=3) {
     124    19860992 :             int n = s->plane[i].available_lines;
     125    19860992 :             int l = lum - s->plane[i].sliceY;
     126             : 
     127    19860992 :             if (l >= n * 2) {
     128     2287918 :                 s->plane[i].sliceY += n;
     129     2287918 :                 s->plane[i].sliceH -= n;
     130             :             }
     131             :         }
     132             :     }
     133     9931546 :     if (chr) {
     134    29775849 :         for (i = 1; i < 3; ++i) {
     135    19850566 :             int n = s->plane[i].available_lines;
     136    19850566 :             int l = chr - s->plane[i].sliceY;
     137             : 
     138    19850566 :             if (l >= n * 2) {
     139     1011816 :                 s->plane[i].sliceY += n;
     140     1011816 :                 s->plane[i].sliceH -= n;
     141             :             }
     142             :         }
     143             :     }
     144     9931546 :     return 0;
     145             : }
     146             : 
     147       61058 : 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, int relative)
     148             : {
     149       61058 :     int i = 0;
     150             : 
     151       61058 :     const int start[4] = {lumY,
     152             :                           chrY,
     153             :                           chrY,
     154             :                           lumY};
     155             : 
     156      244232 :     const int end[4] = {lumY +lumH,
     157       61058 :                         chrY + chrH,
     158       61058 :                         chrY + chrH,
     159       61058 :                         lumY + lumH};
     160             : 
     161      244232 :     uint8_t *const src_[4] = {src[0] + (relative ? 0 : start[0]) * stride[0],
     162       61058 :                               src[1] + (relative ? 0 : start[1]) * stride[1],
     163       61058 :                               src[2] + (relative ? 0 : start[2]) * stride[2],
     164       61058 :                               src[3] + (relative ? 0 : start[3]) * stride[3]};
     165             : 
     166       61058 :     s->width = srcW;
     167             : 
     168      305290 :     for (i = 0; i < 4; ++i) {
     169             :         int j;
     170      244232 :         int first = s->plane[i].sliceY;
     171      244232 :         int n = s->plane[i].available_lines;
     172      244232 :         int lines = end[i] - start[i];
     173      244232 :         int tot_lines = end[i] - first;
     174             : 
     175      488464 :         if (start[i] >= first && n >= tot_lines) {
     176      244232 :             s->plane[i].sliceH = FFMAX(tot_lines, s->plane[i].sliceH);
     177    60733780 :             for (j = 0; j < lines; j+= 1)
     178    60489548 :                 s->plane[i].line[start[i] - first + j] = src_[i] +  j * stride[i];
     179             :         } else {
     180           0 :             s->plane[i].sliceY = start[i];
     181           0 :             lines = lines > n ? n : lines;
     182           0 :             s->plane[i].sliceH = lines;
     183           0 :             for (j = 0; j < lines; j+= 1)
     184           0 :                 s->plane[i].line[j] = src_[i] +  j * stride[i];
     185             :         }
     186             : 
     187             :     }
     188             : 
     189       61058 :     return 0;
     190             : }
     191             : 
     192        2406 : static void fill_ones(SwsSlice *s, int n, int is16bit)
     193             : {
     194             :     int i;
     195       12030 :     for (i = 0; i < 4; ++i) {
     196             :         int j;
     197        9624 :         int size = s->plane[i].available_lines;
     198       88130 :         for (j = 0; j < size; ++j) {
     199             :             int k;
     200       78506 :             int end = is16bit ? n>>1: n;
     201             :             // fill also one extra element
     202       78506 :             end += 1;
     203       78506 :             if (is16bit)
     204     3743336 :                 for (k = 0; k < end; ++k)
     205     3732980 :                     ((int32_t*)(s->plane[i].line[j]))[k] = 1<<18;
     206             :             else
     207    26565212 :                 for (k = 0; k < end; ++k)
     208    26497062 :                     ((int16_t*)(s->plane[i].line[j]))[k] = 1<<14;
     209             :         }
     210             :     }
     211        2406 : }
     212             : 
     213             : /*
     214             :  Calculates the minimum ring buffer size, it should be able to store vFilterSize
     215             :  more n lines where n is the max difference between each adjacent slice which
     216             :  outputs a line.
     217             :  The n lines are needed only when there is not enough src lines to output a single
     218             :  dst line, then we should buffer these lines to process them on the next call to scale.
     219             : */
     220        2406 : static void get_min_buffer_size(SwsContext *c, int *out_lum_size, int *out_chr_size)
     221             : {
     222             :     int lumY;
     223        2406 :     int dstH = c->dstH;
     224        2406 :     int chrDstH = c->chrDstH;
     225        2406 :     int *lumFilterPos = c->vLumFilterPos;
     226        2406 :     int *chrFilterPos = c->vChrFilterPos;
     227        2406 :     int lumFilterSize = c->vLumFilterSize;
     228        2406 :     int chrFilterSize = c->vChrFilterSize;
     229        2406 :     int chrSubSample = c->chrSrcVSubSample;
     230             : 
     231        2406 :     *out_lum_size = lumFilterSize;
     232        2406 :     *out_chr_size = chrFilterSize;
     233             : 
     234      694358 :     for (lumY = 0; lumY < dstH; lumY++) {
     235      691952 :         int chrY      = (int64_t)lumY * chrDstH / dstH;
     236      691952 :         int nextSlice = FFMAX(lumFilterPos[lumY] + lumFilterSize - 1,
     237             :                               ((chrFilterPos[chrY] + chrFilterSize - 1)
     238             :                                << chrSubSample));
     239             : 
     240      691952 :         nextSlice >>= chrSubSample;
     241      691952 :         nextSlice <<= chrSubSample;
     242      691952 :         (*out_lum_size) = FFMAX((*out_lum_size), nextSlice - lumFilterPos[lumY]);
     243      691952 :         (*out_chr_size) = FFMAX((*out_chr_size), (nextSlice >> chrSubSample) - chrFilterPos[chrY]);
     244             :     }
     245        2406 : }
     246             : 
     247             : 
     248             : 
     249        2406 : int ff_init_filters(SwsContext * c)
     250             : {
     251             :     int i;
     252             :     int index;
     253             :     int num_ydesc;
     254             :     int num_cdesc;
     255        2406 :     int num_vdesc = isPlanarYUV(c->dstFormat) && !isGray(c->dstFormat) ? 2 : 1;
     256        2406 :     int need_lum_conv = c->lumToYV12 || c->readLumPlanar || c->alpToYV12 || c->readAlpPlanar;
     257        2406 :     int need_chr_conv = c->chrToYV12 || c->readChrPlanar;
     258        2406 :     int need_gamma = c->is_internal_gamma;
     259             :     int srcIdx, dstIdx;
     260        2406 :     int dst_stride = FFALIGN(c->dstW * sizeof(int16_t) + 66, 16);
     261             : 
     262        2406 :     uint32_t * pal = usePal(c->srcFormat) ? c->pal_yuv : (uint32_t*)c->input_rgb2yuv_table;
     263        2406 :     int res = 0;
     264             : 
     265             :     int lumBufSize;
     266             :     int chrBufSize;
     267             : 
     268        2406 :     get_min_buffer_size(c, &lumBufSize, &chrBufSize);
     269        2406 :     lumBufSize = FFMAX(lumBufSize, c->vLumFilterSize + MAX_LINES_AHEAD);
     270        2406 :     chrBufSize = FFMAX(chrBufSize, c->vChrFilterSize + MAX_LINES_AHEAD);
     271             : 
     272        2406 :     if (c->dstBpc == 16)
     273         334 :         dst_stride <<= 1;
     274             : 
     275        2406 :     num_ydesc = need_lum_conv ? 2 : 1;
     276        2406 :     num_cdesc = need_chr_conv ? 2 : 1;
     277             : 
     278        2406 :     c->numSlice = FFMAX(num_ydesc, num_cdesc) + 2;
     279        2406 :     c->numDesc = num_ydesc + num_cdesc + num_vdesc + (need_gamma ? 2 : 0);
     280        2406 :     c->descIndex[0] = num_ydesc + (need_gamma ? 1 : 0);
     281        2406 :     c->descIndex[1] = num_ydesc + num_cdesc + (need_gamma ? 1 : 0);
     282             : 
     283             : 
     284             : 
     285        2406 :     c->desc = av_mallocz_array(sizeof(SwsFilterDescriptor), c->numDesc);
     286        2406 :     if (!c->desc)
     287           0 :         return AVERROR(ENOMEM);
     288        2406 :     c->slice = av_mallocz_array(sizeof(SwsSlice), c->numSlice);
     289             : 
     290             : 
     291        2406 :     res = alloc_slice(&c->slice[0], c->srcFormat, c->srcH, c->chrSrcH, c->chrSrcHSubSample, c->chrSrcVSubSample, 0);
     292        2406 :     if (res < 0) goto cleanup;
     293        2650 :     for (i = 1; i < c->numSlice-2; ++i) {
     294         244 :         res = alloc_slice(&c->slice[i], c->srcFormat, lumBufSize, chrBufSize, c->chrSrcHSubSample, c->chrSrcVSubSample, 0);
     295         244 :         if (res < 0) goto cleanup;
     296         244 :         res = alloc_lines(&c->slice[i], FFALIGN(c->srcW*2+78, 16), c->srcW);
     297         244 :         if (res < 0) goto cleanup;
     298             :     }
     299             :     // horizontal scaler output
     300        2406 :     res = alloc_slice(&c->slice[i], c->srcFormat, lumBufSize, chrBufSize, c->chrDstHSubSample, c->chrDstVSubSample, 1);
     301        2406 :     if (res < 0) goto cleanup;
     302        2406 :     res = alloc_lines(&c->slice[i], dst_stride, c->dstW);
     303        2406 :     if (res < 0) goto cleanup;
     304             : 
     305        2406 :     fill_ones(&c->slice[i], dst_stride>>1, c->dstBpc == 16);
     306             : 
     307             :     // vertical scaler output
     308        2406 :     ++i;
     309        2406 :     res = alloc_slice(&c->slice[i], c->dstFormat, c->dstH, c->chrDstH, c->chrDstHSubSample, c->chrDstVSubSample, 0);
     310        2406 :     if (res < 0) goto cleanup;
     311             : 
     312        2406 :     index = 0;
     313        2406 :     srcIdx = 0;
     314        2406 :     dstIdx = 1;
     315             : 
     316        2406 :     if (need_gamma) {
     317           0 :         res = ff_init_gamma_convert(c->desc + index, c->slice + srcIdx, c->inv_gamma);
     318           0 :         if (res < 0) goto cleanup;
     319           0 :         ++index;
     320             :     }
     321             : 
     322        2406 :     if (need_lum_conv) {
     323         242 :         res = ff_init_desc_fmt_convert(&c->desc[index], &c->slice[srcIdx], &c->slice[dstIdx], pal);
     324         242 :         if (res < 0) goto cleanup;
     325         242 :         c->desc[index].alpha = c->needAlpha;
     326         242 :         ++index;
     327         242 :         srcIdx = dstIdx;
     328             :     }
     329             : 
     330             : 
     331        2406 :     dstIdx = FFMAX(num_ydesc, num_cdesc);
     332        2406 :     res = ff_init_desc_hscale(&c->desc[index], &c->slice[srcIdx], &c->slice[dstIdx], c->hLumFilter, c->hLumFilterPos, c->hLumFilterSize, c->lumXInc);
     333        2406 :     if (res < 0) goto cleanup;
     334        2406 :     c->desc[index].alpha = c->needAlpha;
     335             : 
     336             : 
     337        2406 :     ++index;
     338             :     {
     339        2406 :         srcIdx = 0;
     340        2406 :         dstIdx = 1;
     341        2406 :         if (need_chr_conv) {
     342         225 :             res = ff_init_desc_cfmt_convert(&c->desc[index], &c->slice[srcIdx], &c->slice[dstIdx], pal);
     343         225 :             if (res < 0) goto cleanup;
     344         225 :             ++index;
     345         225 :             srcIdx = dstIdx;
     346             :         }
     347             : 
     348        2406 :         dstIdx = FFMAX(num_ydesc, num_cdesc);
     349        2406 :         if (c->needs_hcscale)
     350        2250 :             res = ff_init_desc_chscale(&c->desc[index], &c->slice[srcIdx], &c->slice[dstIdx], c->hChrFilter, c->hChrFilterPos, c->hChrFilterSize, c->chrXInc);
     351             :         else
     352         156 :             res = ff_init_desc_no_chr(&c->desc[index], &c->slice[srcIdx], &c->slice[dstIdx]);
     353        2406 :         if (res < 0) goto cleanup;
     354             :     }
     355             : 
     356        2406 :     ++index;
     357             :     {
     358        2406 :         srcIdx = c->numSlice - 2;
     359        2406 :         dstIdx = c->numSlice - 1;
     360        2406 :         res = ff_init_vscale(c, c->desc + index, c->slice + srcIdx, c->slice + dstIdx);
     361        2406 :         if (res < 0) goto cleanup;
     362             :     }
     363             : 
     364        2406 :     ++index;
     365        2406 :     if (need_gamma) {
     366           0 :         res = ff_init_gamma_convert(c->desc + index, c->slice + dstIdx, c->gamma);
     367           0 :         if (res < 0) goto cleanup;
     368             :     }
     369             : 
     370        2406 :     return 0;
     371             : 
     372           0 : cleanup:
     373           0 :     ff_free_filters(c);
     374           0 :     return res;
     375             : }
     376             : 
     377        9078 : int ff_free_filters(SwsContext *c)
     378             : {
     379             :     int i;
     380        9078 :     if (c->desc) {
     381       11427 :         for (i = 0; i < c->numDesc; ++i)
     382        9021 :             av_freep(&c->desc[i].instance);
     383        2406 :         av_freep(&c->desc);
     384             :     }
     385             : 
     386        9078 :     if (c->slice) {
     387        9868 :         for (i = 0; i < c->numSlice; ++i)
     388        7462 :             free_slice(&c->slice[i]);
     389        2406 :         av_freep(&c->slice);
     390             :     }
     391        9078 :     return 0;
     392             : }

Generated by: LCOV version 1.13