FFmpeg
vp9_metadata_bsf.c
Go to the documentation of this file.
1 /*
2  * This file is part of FFmpeg.
3  *
4  * FFmpeg is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * FFmpeg is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with FFmpeg; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 #include "libavutil/avstring.h"
20 #include "libavutil/common.h"
21 #include "libavutil/opt.h"
22 
23 #include "bsf.h"
24 #include "bsf_internal.h"
25 #include "cbs.h"
26 #include "cbs_vp9.h"
27 
28 typedef struct VP9MetadataContext {
29  const AVClass *class;
30 
33 
36 
39 
40 
42 {
44  CodedBitstreamFragment *frag = &ctx->fragment;
45  int err, i;
46 
47  err = ff_bsf_get_packet_ref(bsf, pkt);
48  if (err < 0)
49  return err;
50 
51  err = ff_cbs_read_packet(ctx->cbc, frag, pkt);
52  if (err < 0) {
53  av_log(bsf, AV_LOG_ERROR, "Failed to read packet.\n");
54  goto fail;
55  }
56 
57  for (i = 0; i < frag->nb_units; i++) {
58  VP9RawFrame *frame = frag->units[i].content;
59  VP9RawFrameHeader *header = &frame->header;
60  int profile = (header->profile_high_bit << 1) + header->profile_low_bit;
61 
62  if (header->frame_type == VP9_KEY_FRAME ||
63  header->intra_only && profile > 0) {
64  if (ctx->color_space >= 0) {
65  if (!(profile & 1) && ctx->color_space == VP9_CS_RGB) {
66  if (!(ctx->color_warnings & 2)) {
67  av_log(bsf, AV_LOG_WARNING, "Warning: RGB "
68  "incompatible with profiles 0 and 2.\n");
69  ctx->color_warnings |= 2;
70  }
71  } else
72  header->color_space = ctx->color_space;
73  }
74 
75  if (ctx->color_range >= 0)
76  header->color_range = ctx->color_range;
77  if (header->color_space == VP9_CS_RGB) {
78  if (!(ctx->color_warnings & 1) && !header->color_range) {
79  av_log(bsf, AV_LOG_WARNING, "Warning: Color space RGB "
80  "implicitly sets color range to PC range.\n");
81  ctx->color_warnings |= 1;
82  }
83  header->color_range = 1;
84  }
85  } else if (!(ctx->color_warnings & 4) && header->intra_only && !profile &&
86  ctx->color_space >= 0 && ctx->color_space != VP9_CS_BT_601) {
87  av_log(bsf, AV_LOG_WARNING, "Warning: Intra-only frames in "
88  "profile 0 are automatically BT.601.\n");
89  ctx->color_warnings |= 4;
90  }
91  }
92 
93  err = ff_cbs_write_packet(ctx->cbc, pkt, frag);
94  if (err < 0) {
95  av_log(bsf, AV_LOG_ERROR, "Failed to write packet.\n");
96  goto fail;
97  }
98 
99  err = 0;
100 fail:
101  ff_cbs_fragment_reset(ctx->cbc, frag);
102 
103  if (err < 0)
105 
106  return err;
107 }
108 
110 {
112 
113  return ff_cbs_init(&ctx->cbc, AV_CODEC_ID_VP9, bsf);
114 }
115 
117 {
119 
120  ff_cbs_fragment_free(ctx->cbc, &ctx->fragment);
121  ff_cbs_close(&ctx->cbc);
122 }
123 
124 #define OFFSET(x) offsetof(VP9MetadataContext, x)
125 #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_BSF_PARAM)
126 static const AVOption vp9_metadata_options[] = {
127  { "color_space", "Set colour space (section 7.2.2)",
128  OFFSET(color_space), AV_OPT_TYPE_INT,
129  { .i64 = -1 }, -1, VP9_CS_RGB, FLAGS, "cs" },
130  { "unknown", "Unknown/unspecified", 0, AV_OPT_TYPE_CONST,
131  { .i64 = VP9_CS_UNKNOWN }, .flags = FLAGS, .unit = "cs" },
132  { "bt601", "ITU-R BT.601-7", 0, AV_OPT_TYPE_CONST,
133  { .i64 = VP9_CS_BT_601 }, .flags = FLAGS, .unit = "cs" },
134  { "bt709", "ITU-R BT.709-6", 0, AV_OPT_TYPE_CONST,
135  { .i64 = VP9_CS_BT_709 }, .flags = FLAGS, .unit = "cs" },
136  { "smpte170", "SMPTE-170", 0, AV_OPT_TYPE_CONST,
137  { .i64 = VP9_CS_SMPTE_170 }, .flags = FLAGS, .unit = "cs" },
138  { "smpte240", "SMPTE-240", 0, AV_OPT_TYPE_CONST,
139  { .i64 = VP9_CS_SMPTE_240 }, .flags = FLAGS, .unit = "cs" },
140  { "bt2020", "ITU-R BT.2020-2", 0, AV_OPT_TYPE_CONST,
141  { .i64 = VP9_CS_BT_2020 }, .flags = FLAGS, .unit = "cs" },
142  { "rgb", "sRGB / IEC 61966-2-1", 0, AV_OPT_TYPE_CONST,
143  { .i64 = VP9_CS_RGB }, .flags = FLAGS, .unit = "cs" },
144 
145  { "color_range", "Set colour range (section 7.2.2)",
147  { .i64 = -1 }, -1, 1, FLAGS, "cr" },
148  { "tv", "TV (limited) range", 0, AV_OPT_TYPE_CONST,
149  { .i64 = 0 }, .flags = FLAGS, .unit = "cr" },
150  { "pc", "PC (full) range", 0, AV_OPT_TYPE_CONST,
151  { .i64 = 1 }, .flags = FLAGS, .unit = "cr" },
152 
153  { NULL }
154 };
155 
156 static const AVClass vp9_metadata_class = {
157  .class_name = "vp9_metadata_bsf",
158  .item_name = av_default_item_name,
159  .option = vp9_metadata_options,
160  .version = LIBAVUTIL_VERSION_INT,
161 };
162 
163 static const enum AVCodecID vp9_metadata_codec_ids[] = {
165 };
166 
168  .name = "vp9_metadata",
169  .priv_data_size = sizeof(VP9MetadataContext),
170  .priv_class = &vp9_metadata_class,
172  .close = &vp9_metadata_close,
175 };
av_packet_unref
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:605
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
init
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
bsf_internal.h
opt.h
VP9_CS_BT_601
@ VP9_CS_BT_601
Definition: cbs_vp9.h:58
VP9MetadataContext::fragment
CodedBitstreamFragment fragment
Definition: vp9_metadata_bsf.c:32
vp9_metadata_codec_ids
static enum AVCodecID vp9_metadata_codec_ids[]
Definition: vp9_metadata_bsf.c:163
CodedBitstreamUnit::content
void * content
Pointer to the decomposed form of this unit.
Definition: cbs.h:101
AVBitStreamFilter::name
const char * name
Definition: bsf.h:99
profile
mfxU16 profile
Definition: qsvenc.c:45
CodedBitstreamContext
Context structure for coded bitstream operations.
Definition: cbs.h:168
ff_cbs_close
void ff_cbs_close(CodedBitstreamContext **ctx_ptr)
Close a context and free all internal state.
Definition: cbs.c:115
AVOption
AVOption.
Definition: opt.h:246
ff_cbs_fragment_free
void ff_cbs_fragment_free(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag)
Free the units array of a fragment in addition to what ff_cbs_fragment_reset does.
Definition: cbs.c:157
vp9_metadata_filter
static int vp9_metadata_filter(AVBSFContext *bsf, AVPacket *pkt)
Definition: vp9_metadata_bsf.c:41
cbs.h
filter
filter_frame For filters that do not use the this method is called when a frame is pushed to the filter s input It can be called at any time except in a reentrant way If the input frame is enough to produce then the filter should push the output frames on the output link immediately As an exception to the previous rule if the input frame is enough to produce several output frames then the filter needs output only at least one per link The additional frames can be left buffered in the filter
Definition: filter_design.txt:228
VP9MetadataContext::color_range
int color_range
Definition: vp9_metadata_bsf.c:35
AVBSFContext
The bitstream filter state.
Definition: bsf.h:49
bsf.h
fail
#define fail()
Definition: checkasm.h:123
vp9_metadata_close
static void vp9_metadata_close(AVBSFContext *bsf)
Definition: vp9_metadata_bsf.c:116
CodedBitstreamFragment::units
CodedBitstreamUnit * units
Pointer to an array of units of length nb_units_allocated.
Definition: cbs.h:162
VP9MetadataContext
Definition: vp9_metadata_bsf.c:28
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
VP9_KEY_FRAME
@ VP9_KEY_FRAME
Definition: cbs_vp9.h:44
CodedBitstreamFragment
Coded bitstream fragment structure, combining one or more units.
Definition: cbs.h:116
ff_vp9_metadata_bsf
const AVBitStreamFilter ff_vp9_metadata_bsf
Definition: vp9_metadata_bsf.c:167
ff_cbs_write_packet
int ff_cbs_write_packet(CodedBitstreamContext *ctx, AVPacket *pkt, CodedBitstreamFragment *frag)
Write the bitstream of a fragment to a packet.
Definition: cbs.c:401
AV_CODEC_ID_VP9
@ AV_CODEC_ID_VP9
Definition: codec_id.h:217
cbs_vp9.h
ctx
AVFormatContext * ctx
Definition: movenc.c:48
VP9RawFrameHeader
Definition: cbs_vp9.h:83
vp9_metadata_init
static int vp9_metadata_init(AVBSFContext *bsf)
Definition: vp9_metadata_bsf.c:109
color_range
color_range
Definition: vf_selectivecolor.c:44
vp9_metadata_class
static const AVClass vp9_metadata_class
Definition: vp9_metadata_bsf.c:156
if
if(ret)
Definition: filter_design.txt:179
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:67
NULL
#define NULL
Definition: coverity.c:32
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:235
vp9_metadata_options
static const AVOption vp9_metadata_options[]
Definition: vp9_metadata_bsf.c:126
VP9MetadataContext::color_space
int color_space
Definition: vp9_metadata_bsf.c:34
VP9MetadataContext::cbc
CodedBitstreamContext * cbc
Definition: vp9_metadata_bsf.c:31
VP9RawFrame
Definition: cbs_vp9.h:164
AVCodecID
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: codec_id.h:46
VP9_CS_SMPTE_170
@ VP9_CS_SMPTE_170
Definition: cbs_vp9.h:60
VP9_CS_BT_2020
@ VP9_CS_BT_2020
Definition: cbs_vp9.h:62
header
static const uint8_t header[24]
Definition: sdr2.c:67
VP9_CS_RGB
@ VP9_CS_RGB
Definition: cbs_vp9.h:64
FLAGS
#define FLAGS
Definition: vp9_metadata_bsf.c:125
OFFSET
#define OFFSET(x)
Definition: vp9_metadata_bsf.c:124
AV_CODEC_ID_NONE
@ AV_CODEC_ID_NONE
Definition: codec_id.h:47
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
VP9MetadataContext::color_warnings
int color_warnings
Definition: vp9_metadata_bsf.c:37
common.h
AVBSFContext::priv_data
void * priv_data
Opaque filter-specific private data.
Definition: bsf.h:70
AVClass::class_name
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:72
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:264
ff_cbs_init
int ff_cbs_init(CodedBitstreamContext **ctx_ptr, enum AVCodecID codec_id, void *log_ctx)
Create and initialise a new context for the given codec.
Definition: cbs.c:74
VP9_CS_BT_709
@ VP9_CS_BT_709
Definition: cbs_vp9.h:59
pkt
static AVPacket pkt
Definition: demuxing_decoding.c:54
AVBitStreamFilter
Definition: bsf.h:98
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Definition: opt.h:223
ff_cbs_fragment_reset
void ff_cbs_fragment_reset(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag)
Free the units contained in a fragment as well as the fragment's own data buffer, but not the units a...
Definition: cbs.c:142
ff_cbs_read_packet
int ff_cbs_read_packet(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, const AVPacket *pkt)
Read the data bitstream from a packet into a fragment, then split into units and decompose.
Definition: cbs.c:242
codec_ids
static enum AVCodecID codec_ids[]
Definition: aac_adtstoasc_bsf.c:148
VP9_CS_SMPTE_240
@ VP9_CS_SMPTE_240
Definition: cbs_vp9.h:61
AVPacket
This structure stores compressed data.
Definition: packet.h:332
VP9_CS_UNKNOWN
@ VP9_CS_UNKNOWN
Definition: cbs_vp9.h:57
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
ff_bsf_get_packet_ref
int ff_bsf_get_packet_ref(AVBSFContext *ctx, AVPacket *pkt)
Called by bitstream filters to get packet for filtering.
Definition: bsf.c:249
avstring.h
AV_OPT_TYPE_CONST
@ AV_OPT_TYPE_CONST
Definition: opt.h:232
CodedBitstreamFragment::nb_units
int nb_units
Number of units in this fragment.
Definition: cbs.h:147