2016-06-15 22:53:01 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2016 Google Inc.
|
|
|
|
* Copyright (c) 2016 KongQun Yang (kqyang@google.com)
|
|
|
|
*
|
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "libavutil/pixdesc.h"
|
|
|
|
#include "libavutil/pixfmt.h"
|
2023-09-02 14:57:41 +02:00
|
|
|
#include "libavcodec/defs.h"
|
2022-11-11 17:44:41 +02:00
|
|
|
#include "libavcodec/get_bits.h"
|
2016-06-15 22:53:01 +02:00
|
|
|
#include "vpcc.h"
|
|
|
|
|
2022-11-11 17:44:41 +02:00
|
|
|
#define VP9_SYNCCODE 0x498342
|
|
|
|
|
2016-06-15 22:53:01 +02:00
|
|
|
enum VPX_CHROMA_SUBSAMPLING
|
|
|
|
{
|
|
|
|
VPX_SUBSAMPLING_420_VERTICAL = 0,
|
|
|
|
VPX_SUBSAMPLING_420_COLLOCATED_WITH_LUMA = 1,
|
|
|
|
VPX_SUBSAMPLING_422 = 2,
|
|
|
|
VPX_SUBSAMPLING_444 = 3,
|
|
|
|
};
|
|
|
|
|
|
|
|
static int get_vpx_chroma_subsampling(AVFormatContext *s,
|
|
|
|
enum AVPixelFormat pixel_format,
|
|
|
|
enum AVChromaLocation chroma_location)
|
|
|
|
{
|
|
|
|
int chroma_w, chroma_h;
|
|
|
|
if (av_pix_fmt_get_chroma_sub_sample(pixel_format, &chroma_w, &chroma_h) == 0) {
|
|
|
|
if (chroma_w == 1 && chroma_h == 1) {
|
|
|
|
return (chroma_location == AVCHROMA_LOC_LEFT)
|
|
|
|
? VPX_SUBSAMPLING_420_VERTICAL
|
|
|
|
: VPX_SUBSAMPLING_420_COLLOCATED_WITH_LUMA;
|
|
|
|
} else if (chroma_w == 1 && chroma_h == 0) {
|
|
|
|
return VPX_SUBSAMPLING_422;
|
|
|
|
} else if (chroma_w == 0 && chroma_h == 0) {
|
|
|
|
return VPX_SUBSAMPLING_444;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
av_log(s, AV_LOG_ERROR, "Unsupported pixel format (%d)\n", pixel_format);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int get_bit_depth(AVFormatContext *s, enum AVPixelFormat pixel_format)
|
|
|
|
{
|
|
|
|
const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pixel_format);
|
|
|
|
if (desc == NULL) {
|
|
|
|
av_log(s, AV_LOG_ERROR, "Unsupported pixel format (%d)\n",
|
|
|
|
pixel_format);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return desc->comp[0].depth;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int get_vpx_video_full_range_flag(enum AVColorRange color_range)
|
|
|
|
{
|
|
|
|
return color_range == AVCOL_RANGE_JPEG;
|
|
|
|
}
|
|
|
|
|
2018-04-23 08:10:31 +02:00
|
|
|
// Find approximate VP9 level based on the Luma's Sample rate and Picture size.
|
|
|
|
static int get_vp9_level(AVCodecParameters *par, AVRational *frame_rate) {
|
|
|
|
int picture_size = par->width * par->height;
|
|
|
|
int64_t sample_rate;
|
|
|
|
|
|
|
|
// All decisions will be based on picture_size, if frame rate is missing/invalid
|
|
|
|
if (!frame_rate || !frame_rate->den)
|
|
|
|
sample_rate = 0;
|
|
|
|
else
|
|
|
|
sample_rate = ((int64_t)picture_size * frame_rate->num) / frame_rate->den;
|
|
|
|
|
|
|
|
if (picture_size <= 0) {
|
|
|
|
return 0;
|
|
|
|
} else if (sample_rate <= 829440 && picture_size <= 36864) {
|
2018-08-28 03:56:14 +02:00
|
|
|
return 10;
|
2018-04-23 08:10:31 +02:00
|
|
|
} else if (sample_rate <= 2764800 && picture_size <= 73728) {
|
2018-08-28 03:56:14 +02:00
|
|
|
return 11;
|
2018-04-23 08:10:31 +02:00
|
|
|
} else if (sample_rate <= 4608000 && picture_size <= 122880) {
|
2018-08-28 03:56:14 +02:00
|
|
|
return 20;
|
2018-04-23 08:10:31 +02:00
|
|
|
} else if (sample_rate <= 9216000 && picture_size <= 245760) {
|
2018-08-28 03:56:14 +02:00
|
|
|
return 21;
|
2018-04-23 08:10:31 +02:00
|
|
|
} else if (sample_rate <= 20736000 && picture_size <= 552960) {
|
2018-08-28 03:56:14 +02:00
|
|
|
return 30;
|
2018-04-23 08:10:31 +02:00
|
|
|
} else if (sample_rate <= 36864000 && picture_size <= 983040) {
|
2018-08-28 03:56:14 +02:00
|
|
|
return 31;
|
2018-04-23 08:10:31 +02:00
|
|
|
} else if (sample_rate <= 83558400 && picture_size <= 2228224) {
|
2018-08-28 03:56:14 +02:00
|
|
|
return 40;
|
2018-04-23 08:10:31 +02:00
|
|
|
} else if (sample_rate <= 160432128 && picture_size <= 2228224) {
|
2018-08-28 03:56:14 +02:00
|
|
|
return 41;
|
2018-04-23 08:10:31 +02:00
|
|
|
} else if (sample_rate <= 311951360 && picture_size <= 8912896) {
|
2018-08-28 03:56:14 +02:00
|
|
|
return 50;
|
2018-04-23 08:10:31 +02:00
|
|
|
} else if (sample_rate <= 588251136 && picture_size <= 8912896) {
|
2018-08-28 03:56:14 +02:00
|
|
|
return 51;
|
2018-04-23 08:10:31 +02:00
|
|
|
} else if (sample_rate <= 1176502272 && picture_size <= 8912896) {
|
2018-08-28 03:56:14 +02:00
|
|
|
return 52;
|
2018-04-23 08:10:31 +02:00
|
|
|
} else if (sample_rate <= 1176502272 && picture_size <= 35651584) {
|
2018-08-28 03:56:14 +02:00
|
|
|
return 60;
|
2018-04-23 08:10:31 +02:00
|
|
|
} else if (sample_rate <= 2353004544 && picture_size <= 35651584) {
|
2018-08-28 03:56:14 +02:00
|
|
|
return 61;
|
2018-04-23 08:10:31 +02:00
|
|
|
} else if (sample_rate <= 4706009088 && picture_size <= 35651584) {
|
2018-08-28 03:56:14 +02:00
|
|
|
return 62;
|
2018-04-23 08:10:31 +02:00
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-11 17:44:41 +02:00
|
|
|
static void parse_bitstream(GetBitContext *gb, int *profile, int *bit_depth) {
|
|
|
|
int keyframe, invisible;
|
|
|
|
|
|
|
|
if (get_bits(gb, 2) != 0x2) // frame marker
|
|
|
|
return;
|
|
|
|
*profile = get_bits1(gb);
|
|
|
|
*profile |= get_bits1(gb) << 1;
|
|
|
|
if (*profile == 3)
|
|
|
|
*profile += get_bits1(gb);
|
|
|
|
|
|
|
|
if (get_bits1(gb))
|
|
|
|
return;
|
|
|
|
|
|
|
|
keyframe = !get_bits1(gb);
|
|
|
|
invisible = !get_bits1(gb);
|
|
|
|
get_bits1(gb);
|
|
|
|
|
|
|
|
if (keyframe) {
|
|
|
|
if (get_bits(gb, 24) != VP9_SYNCCODE)
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
int intraonly = invisible ? get_bits1(gb) : 0;
|
|
|
|
if (!intraonly || get_bits(gb, 24) != VP9_SYNCCODE)
|
|
|
|
return;
|
|
|
|
if (*profile < 1) {
|
|
|
|
*bit_depth = 8;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*bit_depth = *profile <= 1 ? 8 : 10 + get_bits1(gb) * 2;
|
|
|
|
}
|
|
|
|
|
2018-01-13 21:04:55 +02:00
|
|
|
int ff_isom_get_vpcc_features(AVFormatContext *s, AVCodecParameters *par,
|
2022-11-11 17:44:41 +02:00
|
|
|
const uint8_t *data, int len,
|
2018-04-23 08:10:31 +02:00
|
|
|
AVRational *frame_rate, VPCC *vpcc)
|
2016-06-15 22:53:01 +02:00
|
|
|
{
|
|
|
|
int profile = par->profile;
|
2023-09-02 14:57:41 +02:00
|
|
|
int level = par->level == AV_LEVEL_UNKNOWN ?
|
2018-04-23 08:10:31 +02:00
|
|
|
get_vp9_level(par, frame_rate) : par->level;
|
2016-06-15 22:53:01 +02:00
|
|
|
int bit_depth = get_bit_depth(s, par->format);
|
|
|
|
int vpx_chroma_subsampling =
|
|
|
|
get_vpx_chroma_subsampling(s, par->format, par->chroma_location);
|
|
|
|
int vpx_video_full_range_flag =
|
|
|
|
get_vpx_video_full_range_flag(par->color_range);
|
|
|
|
|
2017-04-22 01:25:40 +02:00
|
|
|
if (bit_depth < 0 || vpx_chroma_subsampling < 0)
|
2016-06-15 22:53:01 +02:00
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
|
2023-09-02 14:57:41 +02:00
|
|
|
if (len && (profile == AV_PROFILE_UNKNOWN || !bit_depth)) {
|
2022-11-11 17:44:41 +02:00
|
|
|
GetBitContext gb;
|
|
|
|
|
|
|
|
int ret = init_get_bits8(&gb, data, len);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
parse_bitstream(&gb, &profile, &bit_depth);
|
|
|
|
}
|
|
|
|
|
2023-09-02 14:57:41 +02:00
|
|
|
if (profile == AV_PROFILE_UNKNOWN && bit_depth) {
|
2016-06-15 22:53:01 +02:00
|
|
|
if (vpx_chroma_subsampling == VPX_SUBSAMPLING_420_VERTICAL ||
|
|
|
|
vpx_chroma_subsampling == VPX_SUBSAMPLING_420_COLLOCATED_WITH_LUMA) {
|
2023-09-02 14:57:41 +02:00
|
|
|
profile = (bit_depth == 8) ? AV_PROFILE_VP9_0 : AV_PROFILE_VP9_2;
|
2016-06-15 22:53:01 +02:00
|
|
|
} else {
|
2023-09-02 14:57:41 +02:00
|
|
|
profile = (bit_depth == 8) ? AV_PROFILE_VP9_1 : AV_PROFILE_VP9_3;
|
2016-06-15 22:53:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-02 14:57:41 +02:00
|
|
|
if (profile == AV_PROFILE_UNKNOWN || !bit_depth)
|
2022-11-11 17:44:41 +02:00
|
|
|
av_log(s, AV_LOG_WARNING, "VP9 profile and/or bit depth not set or could not be derived\n");
|
|
|
|
|
2018-01-13 21:04:55 +02:00
|
|
|
vpcc->profile = profile;
|
|
|
|
vpcc->level = level;
|
|
|
|
vpcc->bitdepth = bit_depth;
|
|
|
|
vpcc->chroma_subsampling = vpx_chroma_subsampling;
|
|
|
|
vpcc->full_range_flag = vpx_video_full_range_flag;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ff_isom_write_vpcc(AVFormatContext *s, AVIOContext *pb,
|
2022-11-11 17:44:41 +02:00
|
|
|
const uint8_t *data, int len,
|
2018-01-13 21:04:55 +02:00
|
|
|
AVCodecParameters *par)
|
|
|
|
{
|
|
|
|
VPCC vpcc;
|
|
|
|
int ret;
|
|
|
|
|
2022-11-11 17:44:41 +02:00
|
|
|
ret = ff_isom_get_vpcc_features(s, par, data, len, NULL, &vpcc);
|
2018-01-13 21:04:55 +02:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
2023-09-03 18:00:13 +02:00
|
|
|
avio_w8(pb, 1); /* version */
|
|
|
|
avio_wb24(pb, 0); /* flags */
|
2018-01-13 21:04:55 +02:00
|
|
|
avio_w8(pb, vpcc.profile);
|
|
|
|
avio_w8(pb, vpcc.level);
|
|
|
|
avio_w8(pb, (vpcc.bitdepth << 4) | (vpcc.chroma_subsampling << 1) | vpcc.full_range_flag);
|
2017-04-22 01:25:40 +02:00
|
|
|
avio_w8(pb, par->color_primaries);
|
|
|
|
avio_w8(pb, par->color_trc);
|
|
|
|
avio_w8(pb, par->color_space);
|
2016-06-15 22:53:01 +02:00
|
|
|
|
|
|
|
// vp9 does not have codec initialization data.
|
|
|
|
avio_wb16(pb, 0);
|
|
|
|
return 0;
|
|
|
|
}
|