FFmpeg
vpcc.c
Go to the documentation of this file.
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 "libavcodec/avcodec.h"
25 #include "vpcc.h"
26 
28 {
33 };
34 
36  enum AVPixelFormat pixel_format,
37  enum AVChromaLocation chroma_location)
38 {
39  int chroma_w, chroma_h;
40  if (av_pix_fmt_get_chroma_sub_sample(pixel_format, &chroma_w, &chroma_h) == 0) {
41  if (chroma_w == 1 && chroma_h == 1) {
42  return (chroma_location == AVCHROMA_LOC_LEFT)
45  } else if (chroma_w == 1 && chroma_h == 0) {
46  return VPX_SUBSAMPLING_422;
47  } else if (chroma_w == 0 && chroma_h == 0) {
48  return VPX_SUBSAMPLING_444;
49  }
50  }
51  av_log(s, AV_LOG_ERROR, "Unsupported pixel format (%d)\n", pixel_format);
52  return -1;
53 }
54 
55 static int get_bit_depth(AVFormatContext *s, enum AVPixelFormat pixel_format)
56 {
57  const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pixel_format);
58  if (desc == NULL) {
59  av_log(s, AV_LOG_ERROR, "Unsupported pixel format (%d)\n",
60  pixel_format);
61  return -1;
62  }
63  return desc->comp[0].depth;
64 }
65 
67 {
68  return color_range == AVCOL_RANGE_JPEG;
69 }
70 
71 // Find approximate VP9 level based on the Luma's Sample rate and Picture size.
72 static int get_vp9_level(AVCodecParameters *par, AVRational *frame_rate) {
73  int picture_size = par->width * par->height;
74  int64_t sample_rate;
75 
76  // All decisions will be based on picture_size, if frame rate is missing/invalid
77  if (!frame_rate || !frame_rate->den)
78  sample_rate = 0;
79  else
80  sample_rate = ((int64_t)picture_size * frame_rate->num) / frame_rate->den;
81 
82  if (picture_size <= 0) {
83  return 0;
84  } else if (sample_rate <= 829440 && picture_size <= 36864) {
85  return 10;
86  } else if (sample_rate <= 2764800 && picture_size <= 73728) {
87  return 11;
88  } else if (sample_rate <= 4608000 && picture_size <= 122880) {
89  return 20;
90  } else if (sample_rate <= 9216000 && picture_size <= 245760) {
91  return 21;
92  } else if (sample_rate <= 20736000 && picture_size <= 552960) {
93  return 30;
94  } else if (sample_rate <= 36864000 && picture_size <= 983040) {
95  return 31;
96  } else if (sample_rate <= 83558400 && picture_size <= 2228224) {
97  return 40;
98  } else if (sample_rate <= 160432128 && picture_size <= 2228224) {
99  return 41;
100  } else if (sample_rate <= 311951360 && picture_size <= 8912896) {
101  return 50;
102  } else if (sample_rate <= 588251136 && picture_size <= 8912896) {
103  return 51;
104  } else if (sample_rate <= 1176502272 && picture_size <= 8912896) {
105  return 52;
106  } else if (sample_rate <= 1176502272 && picture_size <= 35651584) {
107  return 60;
108  } else if (sample_rate <= 2353004544 && picture_size <= 35651584) {
109  return 61;
110  } else if (sample_rate <= 4706009088 && picture_size <= 35651584) {
111  return 62;
112  } else {
113  return 0;
114  }
115 }
116 
118  AVRational *frame_rate, VPCC *vpcc)
119 {
120  int profile = par->profile;
121  int level = par->level == FF_LEVEL_UNKNOWN ?
122  get_vp9_level(par, frame_rate) : par->level;
123  int bit_depth = get_bit_depth(s, par->format);
124  int vpx_chroma_subsampling =
126  int vpx_video_full_range_flag =
128 
129  if (bit_depth < 0 || vpx_chroma_subsampling < 0)
130  return AVERROR_INVALIDDATA;
131 
132  if (profile == FF_PROFILE_UNKNOWN) {
133  if (vpx_chroma_subsampling == VPX_SUBSAMPLING_420_VERTICAL ||
134  vpx_chroma_subsampling == VPX_SUBSAMPLING_420_COLLOCATED_WITH_LUMA) {
136  } else {
138  }
139  }
140 
141  vpcc->profile = profile;
142  vpcc->level = level;
143  vpcc->bitdepth = bit_depth;
144  vpcc->chroma_subsampling = vpx_chroma_subsampling;
145  vpcc->full_range_flag = vpx_video_full_range_flag;
146 
147  return 0;
148 }
149 
151  AVCodecParameters *par)
152 {
153  VPCC vpcc;
154  int ret;
155 
156  ret = ff_isom_get_vpcc_features(s, par, NULL, &vpcc);
157  if (ret < 0)
158  return ret;
159 
160  avio_w8(pb, vpcc.profile);
161  avio_w8(pb, vpcc.level);
162  avio_w8(pb, (vpcc.bitdepth << 4) | (vpcc.chroma_subsampling << 1) | vpcc.full_range_flag);
163  avio_w8(pb, par->color_primaries);
164  avio_w8(pb, par->color_trc);
165  avio_w8(pb, par->color_space);
166 
167  // vp9 does not have codec initialization data.
168  avio_wb16(pb, 0);
169  return 0;
170 }
bit_depth
static void bit_depth(AudioStatsContext *s, uint64_t mask, uint64_t imask, AVRational *depth)
Definition: af_astats.c:226
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
level
uint8_t level
Definition: svq3.c:204
FF_PROFILE_VP9_0
#define FF_PROFILE_VP9_0
Definition: avcodec.h:1608
AVCodecParameters
This struct describes the properties of an encoded stream.
Definition: codec_par.h:52
ff_isom_get_vpcc_features
int ff_isom_get_vpcc_features(AVFormatContext *s, AVCodecParameters *par, AVRational *frame_rate, VPCC *vpcc)
Definition: vpcc.c:117
AVCodecParameters::color_space
enum AVColorSpace color_space
Definition: codec_par.h:149
get_vpx_chroma_subsampling
static int get_vpx_chroma_subsampling(AVFormatContext *s, enum AVPixelFormat pixel_format, enum AVChromaLocation chroma_location)
Definition: vpcc.c:35
av_pix_fmt_desc_get
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2660
VPCC::full_range_flag
int full_range_flag
Definition: vpcc.h:40
pixdesc.h
AVCOL_RANGE_JPEG
@ AVCOL_RANGE_JPEG
Full range content.
Definition: pixfmt.h:597
VPX_SUBSAMPLING_422
@ VPX_SUBSAMPLING_422
Definition: vpcc.c:31
FF_LEVEL_UNKNOWN
#define FF_LEVEL_UNKNOWN
Definition: avcodec.h:1652
VPCC::chroma_subsampling
int chroma_subsampling
Definition: vpcc.h:39
sample_rate
sample_rate
Definition: ffmpeg_filter.c:153
AVCodecParameters::color_primaries
enum AVColorPrimaries color_primaries
Definition: codec_par.h:147
av_pix_fmt_get_chroma_sub_sample
int av_pix_fmt_get_chroma_sub_sample(enum AVPixelFormat pix_fmt, int *h_shift, int *v_shift)
Utility function to access log2_chroma_w log2_chroma_h from the pixel format AVPixFmtDescriptor.
Definition: pixdesc.c:2688
AVRational::num
int num
Numerator.
Definition: rational.h:59
vpcc.h
AVCodecParameters::color_trc
enum AVColorTransferCharacteristic color_trc
Definition: codec_par.h:148
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
s
#define s(width, name)
Definition: cbs_vp9.c:257
FF_PROFILE_VP9_3
#define FF_PROFILE_VP9_3
Definition: avcodec.h:1611
AVCodecParameters::width
int width
Video only.
Definition: codec_par.h:126
FF_PROFILE_UNKNOWN
#define FF_PROFILE_UNKNOWN
Definition: avcodec.h:1526
color_range
color_range
Definition: vf_selectivecolor.c:44
FF_PROFILE_VP9_2
#define FF_PROFILE_VP9_2
Definition: avcodec.h:1610
AVFormatContext
Format I/O context.
Definition: avformat.h:1200
NULL
#define NULL
Definition: coverity.c:32
VPCC::bitdepth
int bitdepth
Definition: vpcc.h:38
AVCHROMA_LOC_LEFT
@ AVCHROMA_LOC_LEFT
MPEG-2/4 4:2:0, H.264 default for 4:2:0.
Definition: pixfmt.h:618
AVRational
Rational number (pair of numerator and denominator).
Definition: rational.h:58
avio_w8
void avio_w8(AVIOContext *s, int b)
Definition: aviobuf.c:210
AVCodecParameters::level
int level
Definition: codec_par.h:121
VPX_CHROMA_SUBSAMPLING
VPX_CHROMA_SUBSAMPLING
Definition: videotoolbox_vp9.c:36
AVIOContext
Bytestream IO Context.
Definition: avio.h:161
ff_isom_write_vpcc
int ff_isom_write_vpcc(AVFormatContext *s, AVIOContext *pb, AVCodecParameters *par)
Writes VP codec configuration to the provided AVIOContext.
Definition: vpcc.c:150
get_vpx_video_full_range_flag
static int get_vpx_video_full_range_flag(enum AVColorRange color_range)
Definition: vpcc.c:66
VPX_SUBSAMPLING_420_COLLOCATED_WITH_LUMA
@ VPX_SUBSAMPLING_420_COLLOCATED_WITH_LUMA
Definition: vpcc.c:30
AVCodecParameters::profile
int profile
Codec-specific bitstream restrictions that the stream conforms to.
Definition: codec_par.h:120
AVChromaLocation
AVChromaLocation
Location of chroma samples.
Definition: pixfmt.h:616
AVCodecParameters::height
int height
Definition: codec_par.h:127
VPX_SUBSAMPLING_420_VERTICAL
@ VPX_SUBSAMPLING_420_VERTICAL
Definition: vpcc.c:29
AVCodecParameters::color_range
enum AVColorRange color_range
Video only.
Definition: codec_par.h:146
profile
int profile
Definition: mxfenc.c:2003
avcodec.h
get_bit_depth
static int get_bit_depth(AVFormatContext *s, enum AVPixelFormat pixel_format)
Definition: vpcc.c:55
ret
ret
Definition: filter_design.txt:187
pixfmt.h
FF_PROFILE_VP9_1
#define FF_PROFILE_VP9_1
Definition: avcodec.h:1609
VPCC::level
int level
Definition: vpcc.h:37
AVCodecParameters::chroma_location
enum AVChromaLocation chroma_location
Definition: codec_par.h:150
VPCC
Definition: vpcc.h:35
AVRational::den
int den
Denominator.
Definition: rational.h:60
desc
const char * desc
Definition: libsvtav1.c:79
AVCodecParameters::format
int format
Definition: codec_par.h:84
AVPixFmtDescriptor
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:69
avio_wb16
void avio_wb16(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:472
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
VPX_SUBSAMPLING_444
@ VPX_SUBSAMPLING_444
Definition: vpcc.c:32
VPCC::profile
int profile
Definition: vpcc.h:36
AVColorRange
AVColorRange
Visual content value range.
Definition: pixfmt.h:562
get_vp9_level
static int get_vp9_level(AVCodecParameters *par, AVRational *frame_rate)
Definition: vpcc.c:72