FFmpeg
Loading...
Searching...
No Matches
codecstring.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2014 Martin Storsjo
3 * Copyright (c) 2018 Akamai Technologies, Inc.
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/avstring.h"
23#include "libavutil/bprint.h"
25#include "libavutil/rational.h"
26
27#include "av1.h"
28#include "avc.h"
29#include "avformat.h"
30#include "hevc.h"
31#include "internal.h"
32#include "lcevc.h"
33#include "vpcc.h"
34
35static const struct codec_string {
37 char str[8];
38} codecs[] = {
39 { AV_CODEC_ID_VP8, "vp8" },
40 { AV_CODEC_ID_VP9, "vp9" },
41 { AV_CODEC_ID_VORBIS, "vorbis" },
42 { AV_CODEC_ID_OPUS, "opus" },
43 { AV_CODEC_ID_FLAC, "flac" },
45};
46
47static void set_vp9_codec_str(void *logctx, const AVCodecParameters *par,
48 const AVRational *frame_rate, AVBPrint *out)
49{
50 VPCC vpcc;
51 int ret = ff_isom_get_vpcc_features(logctx, par, NULL, 0, frame_rate, &vpcc);
52 if (ret == 0) {
53 av_bprintf(out, "vp09.%02d.%02d.%02d",
54 vpcc.profile, vpcc.level, vpcc.bitdepth);
55 } else {
56 // Default to just vp9 in case of error while finding out profile or level
57 if (logctx)
58 av_log(logctx, AV_LOG_WARNING, "Could not find VP9 profile and/or level\n");
59 av_bprintf(out, "vp9");
60 }
61}
62
63/** Byte i of the 48-bit general_constraint_indicator_flags, 0 first. */
64static unsigned hevc_constraint_byte(const HVCCProfileTierLevel *ptl, int i)
65{
66 return (ptl->constraint_indicator_flags >> (40 - 8 * i)) & 0xff;
67}
68
69/* 3GPP TS 26.244 and ISO/IEC 14496-15 */
70static int set_hevc_codec_str(const AVCodecParameters *par, AVBPrint *out)
71{
73 uint32_t compatibility;
74 int constraint_bytes = 6;
75 int ret;
76
77 if (par->codec_tag != MKTAG('h','v','c','1') &&
78 par->codec_tag != MKTAG('h','e','v','1'))
79 return AVERROR(EINVAL);
80 if (!par->extradata)
81 return AVERROR(EINVAL);
82
84 par->extradata_size);
85 if (ret < 0)
86 return ret;
87 /* No parameter set, or a zeroed hvcC record as some muxers write */
88 if (!ptl.profile_idc || !ptl.level_idc)
89 return AVERROR(EINVAL);
90
91 /* general_profile_compatibility_flags in reverse bit order */
92 compatibility = ptl.profile_compatibility_flags;
93 compatibility = ((compatibility & 0x55555555U) << 1) | ((compatibility >> 1) & 0x55555555U);
94 compatibility = ((compatibility & 0x33333333U) << 2) | ((compatibility >> 2) & 0x33333333U);
95 compatibility = ((compatibility & 0x0F0F0F0FU) << 4) | ((compatibility >> 4) & 0x0F0F0F0FU);
96 compatibility = ((compatibility & 0x00FF00FFU) << 8) | ((compatibility >> 8) & 0x00FF00FFU);
97 compatibility = (compatibility << 16) | (compatibility >> 16);
98
100 /* general_profile_space 1 to 3 is a letter A to C before the profile */
101 if (ptl.profile_space)
102 av_bprint_chars(out, 'A' + ptl.profile_space - 1, 1);
103 av_bprintf(out, "%d.%"PRIx32".%c%d", ptl.profile_idc, compatibility,
104 ptl.tier_flag ? 'H' : 'L', ptl.level_idc);
105
106 /* The six constraint bytes, trailing zero bytes omitted */
107 while (constraint_bytes > 1 &&
108 !hevc_constraint_byte(&ptl, constraint_bytes - 1))
109 constraint_bytes--;
110 for (int i = 0; i < constraint_bytes; i++)
112
113 return 0;
114}
115
116int ff_make_codec_str(void *logctx, const AVCodecParameters *par,
117 const AVRational *frame_rate, struct AVBPrint *out)
118{
119 int i;
120
121 // common Webm codecs are not part of RFC 6381
122 for (i = 0; codecs[i].id != AV_CODEC_ID_NONE; i++)
123 if (codecs[i].id == par->codec_id) {
124 if (codecs[i].id == AV_CODEC_ID_VP9) {
125 set_vp9_codec_str(logctx, par, frame_rate, out);
126 } else {
127 av_bprintf(out, "%s", codecs[i].str);
128 }
129 return 0;
130 }
131
132 if (par->codec_id == AV_CODEC_ID_H264) {
133 // RFC 6381
134 uint8_t *data = par->extradata;
135 if (data) {
136 const uint8_t *p;
137
138 if (AV_RB32(data) == 0x01 && (data[4] & 0x1F) == 7)
139 p = &data[5];
140 else if (AV_RB24(data) == 0x01 && (data[3] & 0x1F) == 7)
141 p = &data[4];
142 else if (data[0] == 0x01) /* avcC */
143 p = &data[1];
144 else
145 return AVERROR(EINVAL);
146 /* avc3 differs from avc1 by where the parameter sets are
147 * stored; the profile and level part is the same
148 * (ISO/IEC 14496-15) */
149 av_bprintf(out, "%s.%02x%02x%02x",
150 par->codec_tag == MKTAG('a','v','c','3') ? "avc3" : "avc1",
151 p[0], p[1], p[2]);
152 } else {
153 return AVERROR(EINVAL);
154 }
155 } else if (par->codec_id == AV_CODEC_ID_HEVC) {
156 return set_hevc_codec_str(par, out);
157 } else if (par->codec_id == AV_CODEC_ID_LCEVC) {
159 int err;
160 if (!par->extradata_size)
161 return AVERROR(EINVAL);
162 if ((err = ff_lcvec_parse_config_record(&lvcc, par->extradata, par->extradata_size, logctx)) < 0)
163 return err;
164 av_bprintf(out, "lvc1.vprf%u.vlev%u", lvcc.profile_idc, lvcc.level_idc);
165 } else if (par->codec_id == AV_CODEC_ID_AV1) {
166 // https://aomediacodec.github.io/av1-isobmff/#codecsparam
168 int err;
169 if (!par->extradata_size)
170 return AVERROR(EINVAL);
171 if ((err = ff_av1_parse_seq_header(&seq, par->extradata, par->extradata_size)) < 0)
172 return err;
173
174 av_bprintf(out, "av01.%01u.%02u%s.%02u",
175 seq.profile, seq.level, seq.tier ? "H" : "M", seq.bitdepth);
177 av_bprintf(out, ".%01u.%01u%01u%01u.%02u.%02u.%02u.%01u",
178 seq.monochrome,
181 seq.color_range);
182 } else if (par->codec_id == AV_CODEC_ID_MPEG4) {
183 // RFC 6381
184 av_bprintf(out, "mp4v.20");
185 // Unimplemented, should output ProfileLevelIndication as a decimal number
186 if (logctx)
187 av_log(logctx, AV_LOG_WARNING, "Incomplete RFC 6381 codec string for mp4v\n");
188 } else if (par->codec_id == AV_CODEC_ID_MP2) {
189 av_bprintf(out, "mp4a.40.33");
190 } else if (par->codec_id == AV_CODEC_ID_MP3) {
191 av_bprintf(out, "mp4a.40.34");
192 } else if (par->codec_id == AV_CODEC_ID_AAC) {
193 // RFC 6381
194 int aot = 2;
195 if (par->extradata_size >= 2) {
196 aot = par->extradata[0] >> 3;
197 if (aot == 31)
198 aot = ((AV_RB16(par->extradata) >> 5) & 0x3f) + 32;
199 } else if (par->profile != AV_PROFILE_UNKNOWN)
200 aot = par->profile + 1;
201 av_bprintf(out, "mp4a.40.%d", aot);
202 } else if (par->codec_id == AV_CODEC_ID_AC3) {
203 av_bprintf(out, "ac-3");
204 } else if (par->codec_id == AV_CODEC_ID_EAC3) {
205 av_bprintf(out, "ec-3");
206 } else {
207 return AVERROR(EINVAL);
208 }
209 return 0;
210}
211
213 AVRational frame_rate, struct AVBPrint *out)
214{
215 return ff_make_codec_str(NULL, par, &frame_rate, out);
216}
static FILE * out
int ff_av1_parse_seq_header(AV1SequenceParameters *seq, const uint8_t *buf, int size)
Parses a Sequence Header from the the provided buffer.
Definition av1.c:336
Main libavformat public API header.
void av_bprintf(AVBPrint *buf, const char *fmt,...)
Definition bprint.c:121
AVBPrint public header.
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
static unsigned hevc_constraint_byte(const HVCCProfileTierLevel *ptl, int i)
Byte i of the 48-bit general_constraint_indicator_flags, 0 first.
Definition codecstring.c:64
static const struct codec_string codecs[]
static int set_hevc_codec_str(const AVCodecParameters *par, AVBPrint *out)
Definition codecstring.c:70
int ff_make_codec_str(void *logctx, const AVCodecParameters *par, const AVRational *frame_rate, struct AVBPrint *out)
Make a RFC 4281/6381 like string describing a codec.
static void set_vp9_codec_str(void *logctx, const AVCodecParameters *par, const AVRational *frame_rate, AVBPrint *out)
Definition codecstring.c:47
#define NULL
Definition coverity.c:32
#define AV_PROFILE_UNKNOWN
Definition defs.h:65
int ret
Definition ffplay.c:391
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition codec_id.h:47
@ AV_CODEC_ID_VORBIS
Definition codec_id.h:464
@ AV_CODEC_ID_EAC3
Definition codec_id.h:499
@ AV_CODEC_ID_LCEVC
Definition codec_id.h:615
@ AV_CODEC_ID_H264
Definition codec_id.h:77
@ AV_CODEC_ID_NONE
Definition codec_id.h:48
@ AV_CODEC_ID_AV1
Definition codec_id.h:275
@ AV_CODEC_ID_VP8
Definition codec_id.h:190
@ AV_CODEC_ID_MP2
Definition codec_id.h:459
@ AV_CODEC_ID_HEVC
Definition codec_id.h:223
@ AV_CODEC_ID_AAC
Definition codec_id.h:461
@ AV_CODEC_ID_FLAC
Definition codec_id.h:471
@ AV_CODEC_ID_AC3
Definition codec_id.h:462
@ AV_CODEC_ID_MPEG4
Definition codec_id.h:62
@ AV_CODEC_ID_MP3
preferred ID for decoding MPEG audio layer 1, 2 or 3
Definition codec_id.h:460
@ AV_CODEC_ID_VP9
Definition codec_id.h:217
@ AV_CODEC_ID_OPUS
Definition codec_id.h:519
int av_mime_codec_str(const AVCodecParameters *par, AVRational frame_rate, struct AVBPrint *out)
Make a RFC 4281/6381 like string describing a codec for MIME types.
void av_bprint_chars(AVBPrint *buf, char c, unsigned n)
Append char c n times to a print buffer.
Definition bprint.c:129
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition log.h:216
#define av_fourcc2str(fourcc)
Definition avutil.h:323
const H265RawProfileTierLevel * ptl
int ff_hevc_get_profile_tier_level(HVCCProfileTierLevel *ptl, const uint8_t *data, int size)
Get the general profile, tier and level an hvcC record written from this extradata would carry.
Definition hevc.c:1411
#define AV_RB32(p)
#define AV_RB24(x)
#define AV_RB16(p)
int ff_lcvec_parse_config_record(LCEVCDecoderConfigurationRecord *lvcc, const uint8_t *buf, int size, void *logctx)
Definition lcevc.c:193
internal header for HEVC (de)muxer utilities
#define MKTAG(a, b, c, d)
Definition macros.h:55
const char data[16]
Definition mxf.c:155
Utilities for rational number calculation.
uint8_t monochrome
Definition av1.h:33
uint8_t color_description_present_flag
Definition av1.h:37
uint8_t chroma_sample_position
Definition av1.h:36
uint8_t color_range
Definition av1.h:41
uint8_t transfer_characteristics
Definition av1.h:39
uint8_t level
Definition av1.h:30
uint8_t color_primaries
Definition av1.h:38
uint8_t chroma_subsampling_x
Definition av1.h:34
uint8_t profile
Definition av1.h:29
uint8_t chroma_subsampling_y
Definition av1.h:35
uint8_t matrix_coefficients
Definition av1.h:40
uint8_t bitdepth
Definition av1.h:32
This struct describes the properties of an encoded stream.
Definition codec_par.h:49
int extradata_size
Size of the extradata content in bytes.
Definition codec_par.h:75
int profile
Codec-specific bitstream restrictions that the stream conforms to.
Definition codec_par.h:135
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition codec_par.h:61
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition codec_par.h:71
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition codec_par.h:57
Rational number (pair of numerator and denominator).
Definition rational.h:58
The general profile_tier_level() fields, as an hvcC record carries them.
Definition hevc.h:33
Definition vpcc.h:34
int profile
Definition vpcc.h:35
int bitdepth
Definition vpcc.h:37
int level
Definition vpcc.h:36
char str[8]
Definition codecstring.c:37
enum AVCodecID id
Definition codecstring.c:36
#define av_log(a,...)
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:157
internal header for VPx codec configuration utilities.