LCOV - code coverage report
Current view: top level - libavformat - vpcc.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 87 0.0 %
Date: 2018-05-20 11:54:08 Functions: 0 6 0.0 %

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2016 Google Inc.
       3             :  * Copyright (c) 2016 KongQun Yang (kqyang@google.com)
       4             :  *
       5             :  * This file is part of FFmpeg.
       6             :  *
       7             :  * FFmpeg is free software; you can redistribute it and/or
       8             :  * modify it under the terms of the GNU Lesser General Public
       9             :  * License as published by the Free Software Foundation; either
      10             :  * version 2.1 of the License, or (at your option) any later version.
      11             :  *
      12             :  * FFmpeg is distributed in the hope that it will be useful,
      13             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      14             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      15             :  * Lesser General Public License for more details.
      16             :  *
      17             :  * You should have received a copy of the GNU Lesser General Public
      18             :  * License along with FFmpeg; if not, write to the Free Software
      19             :  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
      20             :  */
      21             : 
      22             : #include "libavutil/pixdesc.h"
      23             : #include "libavutil/pixfmt.h"
      24             : #include "vpcc.h"
      25             : 
      26             : enum VPX_CHROMA_SUBSAMPLING
      27             : {
      28             :     VPX_SUBSAMPLING_420_VERTICAL = 0,
      29             :     VPX_SUBSAMPLING_420_COLLOCATED_WITH_LUMA = 1,
      30             :     VPX_SUBSAMPLING_422 = 2,
      31             :     VPX_SUBSAMPLING_444 = 3,
      32             : };
      33             : 
      34           0 : static int get_vpx_chroma_subsampling(AVFormatContext *s,
      35             :                                       enum AVPixelFormat pixel_format,
      36             :                                       enum AVChromaLocation chroma_location)
      37             : {
      38             :     int chroma_w, chroma_h;
      39           0 :     if (av_pix_fmt_get_chroma_sub_sample(pixel_format, &chroma_w, &chroma_h) == 0) {
      40           0 :         if (chroma_w == 1 && chroma_h == 1) {
      41             :             return (chroma_location == AVCHROMA_LOC_LEFT)
      42             :                        ? VPX_SUBSAMPLING_420_VERTICAL
      43           0 :                        : VPX_SUBSAMPLING_420_COLLOCATED_WITH_LUMA;
      44           0 :         } else if (chroma_w == 1 && chroma_h == 0) {
      45           0 :             return VPX_SUBSAMPLING_422;
      46           0 :         } else if (chroma_w == 0 && chroma_h == 0) {
      47           0 :             return VPX_SUBSAMPLING_444;
      48             :         }
      49             :     }
      50           0 :     av_log(s, AV_LOG_ERROR, "Unsupported pixel format (%d)\n", pixel_format);
      51           0 :     return -1;
      52             : }
      53             : 
      54           0 : static int get_bit_depth(AVFormatContext *s, enum AVPixelFormat pixel_format)
      55             : {
      56           0 :     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pixel_format);
      57           0 :     if (desc == NULL) {
      58           0 :         av_log(s, AV_LOG_ERROR, "Unsupported pixel format (%d)\n",
      59             :                pixel_format);
      60           0 :         return -1;
      61             :     }
      62           0 :     return desc->comp[0].depth;
      63             : }
      64             : 
      65           0 : static int get_vpx_video_full_range_flag(enum AVColorRange color_range)
      66             : {
      67           0 :     return color_range == AVCOL_RANGE_JPEG;
      68             : }
      69             : 
      70             : // Find approximate VP9 level based on the Luma's Sample rate and Picture size.
      71           0 : static int get_vp9_level(AVCodecParameters *par, AVRational *frame_rate) {
      72           0 :     int picture_size = par->width * par->height;
      73             :     int64_t sample_rate;
      74             : 
      75             :     // All decisions will be based on picture_size, if frame rate is missing/invalid
      76           0 :     if (!frame_rate || !frame_rate->den)
      77           0 :         sample_rate = 0;
      78             :     else
      79           0 :         sample_rate = ((int64_t)picture_size * frame_rate->num) / frame_rate->den;
      80             : 
      81           0 :     if (picture_size <= 0) {
      82           0 :         return 0;
      83           0 :     } else if (sample_rate <= 829440     && picture_size <= 36864) {
      84           0 :         return 0x10;
      85           0 :     } else if (sample_rate <= 2764800    && picture_size <= 73728) {
      86           0 :         return 0x11;
      87           0 :     } else if (sample_rate <= 4608000    && picture_size <= 122880) {
      88           0 :         return 0x20;
      89           0 :     } else if (sample_rate <= 9216000    && picture_size <= 245760) {
      90           0 :         return 0x21;
      91           0 :     } else if (sample_rate <= 20736000   && picture_size <= 552960) {
      92           0 :         return 0x30;
      93           0 :     } else if (sample_rate <= 36864000   && picture_size <= 983040) {
      94           0 :         return 0x31;
      95           0 :     } else if (sample_rate <= 83558400   && picture_size <= 2228224) {
      96           0 :         return 0x40;
      97           0 :     } else if (sample_rate <= 160432128  && picture_size <= 2228224) {
      98           0 :         return 0x41;
      99           0 :     } else if (sample_rate <= 311951360  && picture_size <= 8912896) {
     100           0 :         return 0x50;
     101           0 :     } else if (sample_rate <= 588251136  && picture_size <= 8912896) {
     102           0 :         return 0x51;
     103           0 :     } else if (sample_rate <= 1176502272 && picture_size <= 8912896) {
     104           0 :         return 0x52;
     105           0 :     } else if (sample_rate <= 1176502272 && picture_size <= 35651584) {
     106           0 :         return 0x60;
     107           0 :     } else if (sample_rate <= 2353004544 && picture_size <= 35651584) {
     108           0 :         return 0x61;
     109           0 :     } else if (sample_rate <= 4706009088 && picture_size <= 35651584) {
     110           0 :         return 0x62;
     111             :     } else {
     112           0 :         return 0;
     113             :     }
     114             : }
     115             : 
     116           0 : int ff_isom_get_vpcc_features(AVFormatContext *s, AVCodecParameters *par,
     117             :                               AVRational *frame_rate, VPCC *vpcc)
     118             : {
     119           0 :     int profile = par->profile;
     120           0 :     int level = par->level == FF_LEVEL_UNKNOWN ?
     121           0 :         get_vp9_level(par, frame_rate) : par->level;
     122           0 :     int bit_depth = get_bit_depth(s, par->format);
     123           0 :     int vpx_chroma_subsampling =
     124           0 :         get_vpx_chroma_subsampling(s, par->format, par->chroma_location);
     125           0 :     int vpx_video_full_range_flag =
     126           0 :         get_vpx_video_full_range_flag(par->color_range);
     127             : 
     128           0 :     if (bit_depth < 0 || vpx_chroma_subsampling < 0)
     129           0 :         return AVERROR_INVALIDDATA;
     130             : 
     131           0 :     if (profile == FF_PROFILE_UNKNOWN) {
     132           0 :         if (vpx_chroma_subsampling == VPX_SUBSAMPLING_420_VERTICAL ||
     133             :             vpx_chroma_subsampling == VPX_SUBSAMPLING_420_COLLOCATED_WITH_LUMA) {
     134           0 :             profile = (bit_depth == 8) ? FF_PROFILE_VP9_0 : FF_PROFILE_VP9_2;
     135             :         } else {
     136           0 :             profile = (bit_depth == 8) ? FF_PROFILE_VP9_1 : FF_PROFILE_VP9_3;
     137             :         }
     138             :     }
     139             : 
     140           0 :     vpcc->profile            = profile;
     141           0 :     vpcc->level              = level;
     142           0 :     vpcc->bitdepth           = bit_depth;
     143           0 :     vpcc->chroma_subsampling = vpx_chroma_subsampling;
     144           0 :     vpcc->full_range_flag    = vpx_video_full_range_flag;
     145             : 
     146           0 :     return 0;
     147             : }
     148             : 
     149           0 : int ff_isom_write_vpcc(AVFormatContext *s, AVIOContext *pb,
     150             :                        AVCodecParameters *par)
     151             : {
     152             :     VPCC vpcc;
     153             :     int ret;
     154             : 
     155           0 :     ret = ff_isom_get_vpcc_features(s, par, NULL, &vpcc);
     156           0 :     if (ret < 0)
     157           0 :         return ret;
     158             : 
     159           0 :     avio_w8(pb, vpcc.profile);
     160           0 :     avio_w8(pb, vpcc.level);
     161           0 :     avio_w8(pb, (vpcc.bitdepth << 4) | (vpcc.chroma_subsampling << 1) | vpcc.full_range_flag);
     162           0 :     avio_w8(pb, par->color_primaries);
     163           0 :     avio_w8(pb, par->color_trc);
     164           0 :     avio_w8(pb, par->color_space);
     165             : 
     166             :     // vp9 does not have codec initialization data.
     167           0 :     avio_wb16(pb, 0);
     168           0 :     return 0;
     169             : }

Generated by: LCOV version 1.13