LCOV - code coverage report
Current view: top level - libavformat - qtpalette.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 46 49 93.9 %
Date: 2017-12-16 01:21:47 Functions: 1 1 100.0 %

          Line data    Source code
       1             : /*
       2             :  * QuickTime palette handling
       3             :  * Copyright (c) 2001 Fabrice Bellard
       4             :  * Copyright (c) 2009 Baptiste Coudurier <baptiste dot coudurier at gmail dot com>
       5             :  * Copyright (c) 2015 Mats Peterson
       6             :  *
       7             :  * This file is part of FFmpeg.
       8             :  *
       9             :  * FFmpeg is free software; you can redistribute it and/or
      10             :  * modify it under the terms of the GNU Lesser General Public
      11             :  * License as published by the Free Software Foundation; either
      12             :  * version 2.1 of the License, or (at your option) any later version.
      13             :  *
      14             :  * FFmpeg is distributed in the hope that it will be useful,
      15             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      16             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      17             :  * Lesser General Public License for more details.
      18             :  *
      19             :  * You should have received a copy of the GNU Lesser General Public
      20             :  * License along with FFmpeg; if not, write to the Free Software
      21             :  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
      22             :  */
      23             : 
      24             : #include <stdio.h>
      25             : #include <stdint.h>
      26             : 
      27             : #include "avformat.h"
      28             : #include "libavutil/intreadwrite.h"
      29             : #include "qtpalette.h"
      30             : 
      31         208 : int ff_get_qtpalette(int codec_id, AVIOContext *pb, uint32_t *palette)
      32             : {
      33             :     int tmp, bit_depth, color_table_id, greyscale, i;
      34             : 
      35         208 :     avio_seek(pb, 82, SEEK_CUR);
      36             : 
      37             :     /* Get the bit depth and greyscale state */
      38         208 :     tmp = avio_rb16(pb);
      39         208 :     bit_depth = tmp & 0x1F;
      40         208 :     greyscale = tmp & 0x20;
      41             : 
      42             :     /* Get the color table ID */
      43         208 :     color_table_id = avio_rb16(pb);
      44             : 
      45             :     /* Do not create a greyscale palette for Cinepak */
      46         208 :     if (greyscale && codec_id == AV_CODEC_ID_CINEPAK)
      47           0 :         return 0;
      48             : 
      49             :     /* If the depth is 1, 2, 4, or 8 bpp, file is palettized. */
      50         208 :     if ((bit_depth == 1 || bit_depth == 2 || bit_depth == 4 || bit_depth == 8)) {
      51             :         uint32_t color_count, color_start, color_end;
      52             :         uint32_t a, r, g, b;
      53             : 
      54             :         /* Ignore the greyscale bit for 1-bit video and sample
      55             :          * descriptions containing a color table. */
      56          19 :         if (greyscale && bit_depth > 1 && color_table_id) {
      57             :             int color_index, color_dec;
      58             :             /* compute the greyscale palette */
      59           6 :             color_count = 1 << bit_depth;
      60           6 :             color_index = 255;
      61           6 :             color_dec   = 256 / (color_count - 1);
      62         810 :             for (i = 0; i < color_count; i++) {
      63         804 :                 r = g = b = color_index;
      64         804 :                 palette[i] = (0xFFU << 24) | (r << 16) | (g << 8) | (b);
      65         804 :                 color_index -= color_dec;
      66         804 :                 if (color_index < 0)
      67           6 :                     color_index = 0;
      68             :             }
      69           7 :         } else if (color_table_id) {
      70             :             /* The color table ID is non-zero. Interpret this as
      71             :              * being -1, which means use the default Macintosh
      72             :              * color table */
      73             :             const uint8_t *color_table;
      74           4 :             color_count = 1 << bit_depth;
      75           4 :             if (bit_depth == 1)
      76           1 :                 color_table = ff_qt_default_palette_2;
      77           3 :             else if (bit_depth == 2)
      78           0 :                 color_table = ff_qt_default_palette_4;
      79           3 :             else if (bit_depth == 4)
      80           0 :                 color_table = ff_qt_default_palette_16;
      81             :             else
      82           3 :                 color_table = ff_qt_default_palette_256;
      83         774 :             for (i = 0; i < color_count; i++) {
      84         770 :                 r = color_table[i * 3 + 0];
      85         770 :                 g = color_table[i * 3 + 1];
      86         770 :                 b = color_table[i * 3 + 2];
      87         770 :                 palette[i] = (0xFFU << 24) | (r << 16) | (g << 8) | (b);
      88             :             }
      89             :         } else {
      90             :             /* The color table ID is 0; the color table is in the sample
      91             :              * description */
      92           3 :             color_start = avio_rb32(pb);
      93           3 :             avio_rb16(pb); /* color table flags */
      94           3 :             color_end = avio_rb16(pb);
      95           3 :             if ((color_start <= 255) && (color_end <= 255)) {
      96         531 :                 for (i = color_start; i <= color_end; i++) {
      97             :                     /* each A, R, G, or B component is 16 bits;
      98             :                      * only use the top 8 bits */
      99         528 :                     a = avio_r8(pb);
     100         528 :                     avio_r8(pb);
     101         528 :                     r = avio_r8(pb);
     102         528 :                     avio_r8(pb);
     103         528 :                     g = avio_r8(pb);
     104         528 :                     avio_r8(pb);
     105         528 :                     b = avio_r8(pb);
     106         528 :                     avio_r8(pb);
     107         528 :                     palette[i] = (a << 24 ) | (r << 16) | (g << 8) | (b);
     108             :                 }
     109             :             }
     110             :         }
     111             : 
     112          13 :         return 1;
     113             :     }
     114             : 
     115         195 :     return 0;
     116             : }

Generated by: LCOV version 1.13