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/defs.h"
25 #include "libavcodec/get_bits.h"
26 #include "vpcc.h"
27 
28 #define VP9_SYNCCODE 0x498342
29 
31 {
36 };
37 
38 static int get_vpx_chroma_subsampling(void *logctx,
39  enum AVPixelFormat pixel_format,
40  enum AVChromaLocation chroma_location)
41 {
42  int chroma_w, chroma_h;
43  if (av_pix_fmt_get_chroma_sub_sample(pixel_format, &chroma_w, &chroma_h) == 0) {
44  if (chroma_w == 1 && chroma_h == 1) {
45  return (chroma_location == AVCHROMA_LOC_LEFT)
48  } else if (chroma_w == 1 && chroma_h == 0) {
49  return VPX_SUBSAMPLING_422;
50  } else if (chroma_w == 0 && chroma_h == 0) {
51  return VPX_SUBSAMPLING_444;
52  }
53  }
54  av_log(logctx, AV_LOG_ERROR, "Unsupported pixel format (%d)\n", pixel_format);
55  return -1;
56 }
57 
58 static int get_bit_depth(void *logctx, enum AVPixelFormat pixel_format)
59 {
60  const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pixel_format);
61  if (desc == NULL) {
62  av_log(logctx, AV_LOG_ERROR, "Unsupported pixel format (%d)\n",
63  pixel_format);
64  return -1;
65  }
66  return desc->comp[0].depth;
67 }
68 
70 {
71  return color_range == AVCOL_RANGE_JPEG;
72 }
73 
74 // Find approximate VP9 level based on the Luma's Sample rate and Picture size.
75 static int get_vp9_level(const AVCodecParameters *par,
76  const AVRational *frame_rate)
77 {
78  int picture_size = par->width * par->height;
79  int64_t sample_rate;
80 
81  // All decisions will be based on picture_size, if frame rate is missing/invalid
82  if (!frame_rate || !frame_rate->den)
83  sample_rate = 0;
84  else
85  sample_rate = ((int64_t)picture_size * frame_rate->num) / frame_rate->den;
86 
87  if (picture_size <= 0) {
88  return 0;
89  } else if (sample_rate <= 829440 && picture_size <= 36864) {
90  return 10;
91  } else if (sample_rate <= 2764800 && picture_size <= 73728) {
92  return 11;
93  } else if (sample_rate <= 4608000 && picture_size <= 122880) {
94  return 20;
95  } else if (sample_rate <= 9216000 && picture_size <= 245760) {
96  return 21;
97  } else if (sample_rate <= 20736000 && picture_size <= 552960) {
98  return 30;
99  } else if (sample_rate <= 36864000 && picture_size <= 983040) {
100  return 31;
101  } else if (sample_rate <= 83558400 && picture_size <= 2228224) {
102  return 40;
103  } else if (sample_rate <= 160432128 && picture_size <= 2228224) {
104  return 41;
105  } else if (sample_rate <= 311951360 && picture_size <= 8912896) {
106  return 50;
107  } else if (sample_rate <= 588251136 && picture_size <= 8912896) {
108  return 51;
109  } else if (sample_rate <= 1176502272 && picture_size <= 8912896) {
110  return 52;
111  } else if (sample_rate <= 1176502272 && picture_size <= 35651584) {
112  return 60;
113  } else if (sample_rate <= 2353004544 && picture_size <= 35651584) {
114  return 61;
115  } else if (sample_rate <= 4706009088 && picture_size <= 35651584) {
116  return 62;
117  } else {
118  return 0;
119  }
120 }
121 
122 static void parse_bitstream(GetBitContext *gb, int *profile, int *bit_depth) {
123  int keyframe, invisible;
124 
125  if (get_bits(gb, 2) != 0x2) // frame marker
126  return;
127  *profile = get_bits1(gb);
128  *profile |= get_bits1(gb) << 1;
129  if (*profile == 3)
130  *profile += get_bits1(gb);
131 
132  if (get_bits1(gb))
133  return;
134 
135  keyframe = !get_bits1(gb);
136  invisible = !get_bits1(gb);
137  get_bits1(gb);
138 
139  if (keyframe) {
140  if (get_bits(gb, 24) != VP9_SYNCCODE)
141  return;
142  } else {
143  int intraonly = invisible ? get_bits1(gb) : 0;
144  if (!intraonly || get_bits(gb, 24) != VP9_SYNCCODE)
145  return;
146  if (*profile < 1) {
147  *bit_depth = 8;
148  return;
149  }
150  }
151 
152  *bit_depth = *profile <= 1 ? 8 : 10 + get_bits1(gb) * 2;
153 }
154 
155 int ff_isom_get_vpcc_features(void *logctx, const AVCodecParameters *par,
156  const uint8_t *data, int len,
157  const AVRational *frame_rate, VPCC *vpcc)
158 {
159  int profile = par->profile;
160  int level = par->level == AV_LEVEL_UNKNOWN ?
161  get_vp9_level(par, frame_rate) : par->level;
162  int bit_depth = get_bit_depth(logctx, par->format);
163  int vpx_chroma_subsampling =
165  int vpx_video_full_range_flag =
167 
168  if (bit_depth < 0 || vpx_chroma_subsampling < 0)
169  return AVERROR_INVALIDDATA;
170 
171  if (len && (profile == AV_PROFILE_UNKNOWN || !bit_depth)) {
172  GetBitContext gb;
173 
174  int ret = init_get_bits8(&gb, data, len);
175  if (ret < 0)
176  return ret;
177 
179  }
180 
182  if (vpx_chroma_subsampling == VPX_SUBSAMPLING_420_VERTICAL ||
183  vpx_chroma_subsampling == VPX_SUBSAMPLING_420_COLLOCATED_WITH_LUMA) {
185  } else {
187  }
188  }
189 
191  av_log(logctx, AV_LOG_WARNING, "VP9 profile and/or bit depth not set or could not be derived\n");
192 
193  vpcc->profile = profile;
194  vpcc->level = level;
195  vpcc->bitdepth = bit_depth;
196  vpcc->chroma_subsampling = vpx_chroma_subsampling;
197  vpcc->full_range_flag = vpx_video_full_range_flag;
198 
199  return 0;
200 }
201 
202 int ff_isom_write_vpcc(void *logctx, AVIOContext *pb,
203  const uint8_t *data, int len,
204  const AVCodecParameters *par)
205 {
206  VPCC vpcc;
207  int ret;
208 
209  ret = ff_isom_get_vpcc_features(logctx, par, data, len, NULL, &vpcc);
210  if (ret < 0)
211  return ret;
212 
213  avio_w8(pb, 1); /* version */
214  avio_wb24(pb, 0); /* flags */
215  avio_w8(pb, vpcc.profile);
216  avio_w8(pb, vpcc.level);
217  avio_w8(pb, (vpcc.bitdepth << 4) | (vpcc.chroma_subsampling << 1) | vpcc.full_range_flag);
218  avio_w8(pb, par->color_primaries);
219  avio_w8(pb, par->color_trc);
220  avio_w8(pb, par->color_space);
221 
222  // vp9 does not have codec initialization data.
223  avio_wb16(pb, 0);
224  return 0;
225 }
ff_isom_write_vpcc
int ff_isom_write_vpcc(void *logctx, AVIOContext *pb, const uint8_t *data, int len, const AVCodecParameters *par)
Writes VP codec configuration to the provided AVIOContext.
Definition: vpcc.c:202
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:216
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:71
level
uint8_t level
Definition: svq3.c:208
AVCodecParameters
This struct describes the properties of an encoded stream.
Definition: codec_par.h:47
AVCodecParameters::color_space
enum AVColorSpace color_space
Definition: codec_par.h:169
av_pix_fmt_desc_get
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:3456
VPCC::full_range_flag
int full_range_flag
Definition: vpcc.h:39
int64_t
long long int64_t
Definition: coverity.c:34
AV_PROFILE_VP9_1
#define AV_PROFILE_VP9_1
Definition: defs.h:155
pixdesc.h
AVCOL_RANGE_JPEG
@ AVCOL_RANGE_JPEG
Full range content.
Definition: pixfmt.h:777
data
const char data[16]
Definition: mxf.c:149
VPX_SUBSAMPLING_422
@ VPX_SUBSAMPLING_422
Definition: vpcc.c:34
VPCC::chroma_subsampling
int chroma_subsampling
Definition: vpcc.h:38
bit_depth
static void bit_depth(AudioStatsContext *s, const uint64_t *const mask, uint8_t *depth)
Definition: af_astats.c:246
AVCodecParameters::color_primaries
enum AVColorPrimaries color_primaries
Definition: codec_par.h:167
get_bits
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:337
parse_bitstream
static void parse_bitstream(GetBitContext *gb, int *profile, int *bit_depth)
Definition: vpcc.c:122
GetBitContext
Definition: get_bits.h:109
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:3484
AV_PROFILE_VP9_3
#define AV_PROFILE_VP9_3
Definition: defs.h:157
AVRational::num
int num
Numerator.
Definition: rational.h:59
vpcc.h
AVCodecParameters::color_trc
enum AVColorTransferCharacteristic color_trc
Definition: codec_par.h:168
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:210
init_get_bits8
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:544
AV_PROFILE_UNKNOWN
#define AV_PROFILE_UNKNOWN
Definition: defs.h:65
VP9_SYNCCODE
#define VP9_SYNCCODE
Definition: vpcc.c:28
AVCodecParameters::width
int width
Video only.
Definition: codec_par.h:134
get_bits.h
color_range
color_range
Definition: vf_selectivecolor.c:43
NULL
#define NULL
Definition: coverity.c:32
VPCC::bitdepth
int bitdepth
Definition: vpcc.h:37
AVCHROMA_LOC_LEFT
@ AVCHROMA_LOC_LEFT
MPEG-2/4 4:2:0, H.264 default for 4:2:0.
Definition: pixfmt.h:798
AV_LEVEL_UNKNOWN
#define AV_LEVEL_UNKNOWN
Definition: defs.h:209
AVRational
Rational number (pair of numerator and denominator).
Definition: rational.h:58
get_bits1
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:391
avio_w8
void avio_w8(AVIOContext *s, int b)
Definition: aviobuf.c:184
AVCodecParameters::level
int level
Definition: codec_par.h:129
VPX_CHROMA_SUBSAMPLING
VPX_CHROMA_SUBSAMPLING
Definition: videotoolbox_vp9.c:39
ff_isom_get_vpcc_features
int ff_isom_get_vpcc_features(void *logctx, const AVCodecParameters *par, const uint8_t *data, int len, const AVRational *frame_rate, VPCC *vpcc)
Definition: vpcc.c:155
AVIOContext
Bytestream IO Context.
Definition: avio.h:160
get_vpx_chroma_subsampling
static int get_vpx_chroma_subsampling(void *logctx, enum AVPixelFormat pixel_format, enum AVChromaLocation chroma_location)
Definition: vpcc.c:38
get_vpx_video_full_range_flag
static int get_vpx_video_full_range_flag(enum AVColorRange color_range)
Definition: vpcc.c:69
VPX_SUBSAMPLING_420_COLLOCATED_WITH_LUMA
@ VPX_SUBSAMPLING_420_COLLOCATED_WITH_LUMA
Definition: vpcc.c:33
AVCodecParameters::profile
int profile
Codec-specific bitstream restrictions that the stream conforms to.
Definition: codec_par.h:128
get_bit_depth
static int get_bit_depth(void *logctx, enum AVPixelFormat pixel_format)
Definition: vpcc.c:58
AVChromaLocation
AVChromaLocation
Location of chroma samples.
Definition: pixfmt.h:796
AVCodecParameters::height
int height
Definition: codec_par.h:135
VPX_SUBSAMPLING_420_VERTICAL
@ VPX_SUBSAMPLING_420_VERTICAL
Definition: vpcc.c:32
AVCodecParameters::color_range
enum AVColorRange color_range
Video only.
Definition: codec_par.h:166
len
int len
Definition: vorbis_enc_data.h:426
profile
int profile
Definition: mxfenc.c:2297
AV_PROFILE_VP9_2
#define AV_PROFILE_VP9_2
Definition: defs.h:156
ret
ret
Definition: filter_design.txt:187
pixfmt.h
VPCC::level
int level
Definition: vpcc.h:36
AVCodecParameters::chroma_location
enum AVChromaLocation chroma_location
Definition: codec_par.h:170
VPCC
Definition: vpcc.h:34
AVRational::den
int den
Denominator.
Definition: rational.h:60
defs.h
AV_PROFILE_VP9_0
#define AV_PROFILE_VP9_0
Definition: defs.h:154
desc
const char * desc
Definition: libsvtav1.c:78
AVCodecParameters::format
int format
Definition: codec_par.h:92
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_wb24
void avio_wb24(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:458
avio_wb16
void avio_wb16(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:446
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
get_vp9_level
static int get_vp9_level(const AVCodecParameters *par, const AVRational *frame_rate)
Definition: vpcc.c:75
VPX_SUBSAMPLING_444
@ VPX_SUBSAMPLING_444
Definition: vpcc.c:35
VPCC::profile
int profile
Definition: vpcc.h:35
AVColorRange
AVColorRange
Visual content value range.
Definition: pixfmt.h:742