LCOV - code coverage report
Current view: top level - libavcodec - rl.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 61 79 77.2 %
Date: 2017-12-18 20:14:19 Functions: 2 3 66.7 %

          Line data    Source code
       1             : /*
       2             :  * This file is part of FFmpeg.
       3             :  *
       4             :  * FFmpeg is free software; you can redistribute it and/or
       5             :  * modify it under the terms of the GNU Lesser General Public
       6             :  * License as published by the Free Software Foundation; either
       7             :  * version 2.1 of the License, or (at your option) any later version.
       8             :  *
       9             :  * FFmpeg is distributed in the hope that it will be useful,
      10             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      12             :  * Lesser General Public License for more details.
      13             :  *
      14             :  * You should have received a copy of the GNU Lesser General Public
      15             :  * License along with FFmpeg; if not, write to the Free Software
      16             :  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
      17             :  */
      18             : 
      19             : #include <stdint.h>
      20             : #include <string.h>
      21             : 
      22             : #include "libavutil/attributes.h"
      23             : #include "libavutil/avassert.h"
      24             : #include "libavutil/mem.h"
      25             : 
      26             : #include "rl.h"
      27             : 
      28           0 : void ff_rl_free(RLTable *rl)
      29             : {
      30             :     int i;
      31             : 
      32           0 :     for (i = 0; i < 2; i++) {
      33           0 :         av_freep(&rl->max_run[i]);
      34           0 :         av_freep(&rl->max_level[i]);
      35           0 :         av_freep(&rl->index_run[i]);
      36             :     }
      37           0 : }
      38             : 
      39        1586 : av_cold int ff_rl_init(RLTable *rl,
      40             :                        uint8_t static_store[2][2 * MAX_RUN + MAX_LEVEL + 3])
      41             : {
      42             :     int8_t  max_level[MAX_RUN + 1], max_run[MAX_LEVEL + 1];
      43             :     uint8_t index_run[MAX_RUN + 1];
      44             :     int last, run, level, start, end, i;
      45             : 
      46             :     /* If table is static, we can quit if rl->max_level[0] is not NULL */
      47        1586 :     if (static_store && rl->max_level[0])
      48           3 :         return 0;
      49             : 
      50             :     /* compute max_level[], max_run[] and index_run[] */
      51        4749 :     for (last = 0; last < 2; last++) {
      52        3166 :         if (last == 0) {
      53        1583 :             start = 0;
      54        1583 :             end = rl->last;
      55             :         } else {
      56        1583 :             start = rl->last;
      57        1583 :             end = rl->n;
      58             :         }
      59             : 
      60        3166 :         memset(max_level, 0, MAX_RUN + 1);
      61        3166 :         memset(max_run, 0, MAX_LEVEL + 1);
      62        3166 :         memset(index_run, rl->n, MAX_RUN + 1);
      63      192640 :         for (i = start; i < end; i++) {
      64      189474 :             run   = rl->table_run[i];
      65      189474 :             level = rl->table_level[i];
      66      189474 :             if (index_run[run] == rl->n)
      67       80492 :                 index_run[run] = i;
      68      189474 :             if (level > max_level[run])
      69      189461 :                 max_level[run] = level;
      70      189474 :             if (run > max_run[level])
      71      143260 :                 max_run[level] = run;
      72             :         }
      73        3166 :         if (static_store)
      74        3166 :             rl->max_level[last] = static_store[last];
      75             :         else {
      76           0 :             rl->max_level[last] = av_malloc(MAX_RUN + 1);
      77           0 :             if (!rl->max_level[last])
      78           0 :                 goto fail;
      79             :         }
      80        3166 :         memcpy(rl->max_level[last], max_level, MAX_RUN + 1);
      81        3166 :         if (static_store)
      82        3166 :             rl->max_run[last]   = static_store[last] + MAX_RUN + 1;
      83             :         else {
      84           0 :             rl->max_run[last]   = av_malloc(MAX_LEVEL + 1);
      85           0 :             if (!rl->max_run[last])
      86           0 :                 goto fail;
      87             :         }
      88        3166 :         memcpy(rl->max_run[last], max_run, MAX_LEVEL + 1);
      89        3166 :         if (static_store)
      90        3166 :             rl->index_run[last] = static_store[last] + MAX_RUN + MAX_LEVEL + 2;
      91             :         else {
      92           0 :             rl->index_run[last] = av_malloc(MAX_RUN + 1);
      93           0 :             if (!rl->index_run[last])
      94           0 :                 goto fail;
      95             :         }
      96        3166 :         memcpy(rl->index_run[last], index_run, MAX_RUN + 1);
      97             :     }
      98        1583 :     return 0;
      99             : 
     100           0 : fail:
     101           0 :     ff_rl_free(rl);
     102           0 :     return AVERROR(ENOMEM);
     103             : }
     104             : 
     105         864 : av_cold void ff_rl_init_vlc(RLTable *rl, unsigned static_size)
     106             : {
     107             :     int i, q;
     108         864 :     VLC_TYPE table[1500][2] = {{0}};
     109         864 :     VLC vlc = { .table = table, .table_allocated = static_size };
     110         864 :     av_assert0(static_size <= FF_ARRAY_ELEMS(table));
     111         864 :     init_vlc(&vlc, 9, rl->n + 1, &rl->table_vlc[0][1], 4, 2, &rl->table_vlc[0][0], 4, 2, INIT_VLC_USE_NEW_STATIC);
     112             : 
     113       28512 :     for (q = 0; q < 32; q++) {
     114       27648 :         int qmul = q * 2;
     115       27648 :         int qadd = (q - 1) | 1;
     116             : 
     117       27648 :         if (q == 0) {
     118         864 :             qmul = 1;
     119         864 :             qadd = 0;
     120             :         }
     121    20306112 :         for (i = 0; i < vlc.table_size; i++) {
     122    20278464 :             int code = vlc.table[i][0];
     123    20278464 :             int len  = vlc.table[i][1];
     124             :             int level, run;
     125             : 
     126    20278464 :             if (len == 0) { // illegal code
     127      183104 :                 run   = 66;
     128      183104 :                 level = MAX_LEVEL;
     129    20095360 :             } else if (len < 0) { // more bits needed
     130      396544 :                 run   = 0;
     131      396544 :                 level = code;
     132             :             } else {
     133    19698816 :                 if (code == rl->n) { // esc
     134      266336 :                     run   = 66;
     135      266336 :                     level =  0;
     136             :                 } else {
     137    19432480 :                     run   = rl->table_run[code] + 1;
     138    19432480 :                     level = rl->table_level[code] * qmul + qadd;
     139    19432480 :                     if (code >= rl->last) run += 192;
     140             :                 }
     141             :             }
     142    20278464 :             rl->rl_vlc[q][i].len   = len;
     143    20278464 :             rl->rl_vlc[q][i].level = level;
     144    20278464 :             rl->rl_vlc[q][i].run   = run;
     145             :         }
     146             :     }
     147         864 : }

Generated by: LCOV version 1.13