FFmpeg  4.2.3
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 "cbs.h"
25 #include "cbs_vp9.h"
26 
27 typedef struct VP9MetadataContext {
28  const AVClass *class;
29 
32 
35 
38 
39 
41 {
43  CodedBitstreamFragment *frag = &ctx->fragment;
44  int err, i;
45 
46  err = ff_bsf_get_packet_ref(bsf, pkt);
47  if (err < 0)
48  return err;
49 
50  err = ff_cbs_read_packet(ctx->cbc, frag, pkt);
51  if (err < 0) {
52  av_log(bsf, AV_LOG_ERROR, "Failed to read packet.\n");
53  goto fail;
54  }
55 
56  for (i = 0; i < frag->nb_units; i++) {
57  VP9RawFrame *frame = frag->units[i].content;
58  VP9RawFrameHeader *header = &frame->header;
59 
60  if (ctx->color_space >= 0) {
61  header->color_space = ctx->color_space;
62  }
63  if (ctx->color_range >= 0) {
64  if (ctx->color_range == 0 &&
65  header->color_space == VP9_CS_RGB &&
66  !ctx->color_range_rgb_warned) {
67  av_log(bsf, AV_LOG_WARNING, "Warning: color_range cannot "
68  "be set to limited in RGB streams.\n");
69  ctx->color_range_rgb_warned = 1;
70  } else {
71  header->color_range = ctx->color_range;
72  }
73  }
74  }
75 
76  err = ff_cbs_write_packet(ctx->cbc, pkt, frag);
77  if (err < 0) {
78  av_log(bsf, AV_LOG_ERROR, "Failed to write packet.\n");
79  goto fail;
80  }
81 
82  err = 0;
83 fail:
84  ff_cbs_fragment_reset(ctx->cbc, frag);
85 
86  if (err < 0)
87  av_packet_unref(pkt);
88 
89  return err;
90 }
91 
93 {
95 
96  return ff_cbs_init(&ctx->cbc, AV_CODEC_ID_VP9, bsf);
97 }
98 
100 {
102 
103  ff_cbs_fragment_free(ctx->cbc, &ctx->fragment);
104  ff_cbs_close(&ctx->cbc);
105 }
106 
107 #define OFFSET(x) offsetof(VP9MetadataContext, x)
108 #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_BSF_PARAM)
109 static const AVOption vp9_metadata_options[] = {
110  { "color_space", "Set colour space (section 7.2.2)",
112  { .i64 = -1 }, -1, VP9_CS_RGB, FLAGS, "cs" },
113  { "unknown", "Unknown/unspecified", 0, AV_OPT_TYPE_CONST,
114  { .i64 = VP9_CS_UNKNOWN }, .flags = FLAGS, .unit = "cs" },
115  { "bt601", "ITU-R BT.601-7", 0, AV_OPT_TYPE_CONST,
116  { .i64 = VP9_CS_BT_601 }, .flags = FLAGS, .unit = "cs" },
117  { "bt709", "ITU-R BT.709-6", 0, AV_OPT_TYPE_CONST,
118  { .i64 = VP9_CS_BT_709 }, .flags = FLAGS, .unit = "cs" },
119  { "smpte170", "SMPTE-170", 0, AV_OPT_TYPE_CONST,
120  { .i64 = VP9_CS_SMPTE_170 }, .flags = FLAGS, .unit = "cs" },
121  { "smpte240", "SMPTE-240", 0, AV_OPT_TYPE_CONST,
122  { .i64 = VP9_CS_SMPTE_240 }, .flags = FLAGS, .unit = "cs" },
123  { "bt2020", "ITU-R BT.2020-2", 0, AV_OPT_TYPE_CONST,
124  { .i64 = VP9_CS_BT_2020 }, .flags = FLAGS, .unit = "cs" },
125  { "rgb", "sRGB / IEC 61966-2-1", 0, AV_OPT_TYPE_CONST,
126  { .i64 = VP9_CS_RGB }, .flags = FLAGS, .unit = "cs" },
127 
128  { "color_range", "Set colour range (section 7.2.2)",
130  { .i64 = -1 }, -1, 1, FLAGS, "cr" },
131  { "tv", "TV (limited) range", 0, AV_OPT_TYPE_CONST,
132  { .i64 = 0 }, .flags = FLAGS, .unit = "cr" },
133  { "pc", "PC (full) range", 0, AV_OPT_TYPE_CONST,
134  { .i64 = 1 }, .flags = FLAGS, .unit = "cr" },
135 
136  { NULL }
137 };
138 
139 static const AVClass vp9_metadata_class = {
140  .class_name = "vp9_metadata_bsf",
141  .item_name = av_default_item_name,
142  .option = vp9_metadata_options,
143  .version = LIBAVUTIL_VERSION_INT,
144 };
145 
146 static const enum AVCodecID vp9_metadata_codec_ids[] = {
148 };
149 
151  .name = "vp9_metadata",
152  .priv_data_size = sizeof(VP9MetadataContext),
153  .priv_class = &vp9_metadata_class,
155  .close = &vp9_metadata_close,
158 };
CodedBitstreamContext * cbc
#define NULL
Definition: coverity.c:32
int nb_units
Number of units in this fragment.
Definition: cbs.h:147
uint8_t color_space
Definition: cbs_vp9.h:97
static int vp9_metadata_filter(AVBSFContext *bsf, AVPacket *pkt)
AVOption.
Definition: opt.h:246
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
int ff_cbs_write_packet(CodedBitstreamContext *ctx, AVPacket *pkt, CodedBitstreamFragment *frag)
Write the bitstream of a fragment to a packet.
Definition: cbs.c:401
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
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
The bitstream filter state.
Definition: avcodec.h:5763
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:191
color_range
static AVPacket pkt
void * priv_data
Opaque filter-specific private data.
Definition: avcodec.h:5784
static enum AVCodecID vp9_metadata_codec_ids[]
static int vp9_metadata_init(AVBSFContext *bsf)
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
static const AVOption vp9_metadata_options[]
static void filter(int16_t *output, ptrdiff_t out_stride, int16_t *low, ptrdiff_t low_stride, int16_t *high, ptrdiff_t high_stride, int len, int clip)
Definition: cfhd.c:153
AVOptions.
static AVFrame * frame
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
const char * name
Definition: avcodec.h:5813
void * content
Pointer to the decomposed form of this unit.
Definition: cbs.h:101
static const uint8_t header[24]
Definition: sdr2.c:67
CodedBitstreamUnit * units
Pointer to an array of units of length nb_units_allocated.
Definition: cbs.h:162
#define av_log(a,...)
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
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:259
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: avcodec.h:215
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
#define fail()
Definition: checkasm.h:120
static const AVClass vp9_metadata_class
AVFormatContext * ctx
Definition: movenc.c:48
CodedBitstreamFragment fragment
const AVBitStreamFilter ff_vp9_metadata_bsf
#define OFFSET(x)
Coded bitstream fragment structure, combining one or more units.
Definition: cbs.h:116
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:599
Describe the class of an AVClass context structure.
Definition: log.h:67
void ff_cbs_fragment_reset(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag)
Free the units contained in a fragment as well as the fragment&#39;s own data buffer, but not the units a...
Definition: cbs.c:142
Context structure for coded bitstream operations.
Definition: cbs.h:168
#define FLAGS
void ff_cbs_close(CodedBitstreamContext **ctx_ptr)
Close a context and free all internal state.
Definition: cbs.c:115
static void vp9_metadata_close(AVBSFContext *bsf)
uint8_t color_range
Definition: cbs_vp9.h:98
VP9RawFrameHeader header
Definition: cbs_vp9.h:165
common internal and external API header
static enum AVCodecID codec_ids[]
This structure stores compressed data.
Definition: avcodec.h:1454
int ff_bsf_get_packet_ref(AVBSFContext *ctx, AVPacket *pkt)
Called by bitstream filters to get packet for filtering.
Definition: bsf.c:239