]> git.sesse.net Git - ffmpeg/blob - libavcodec/vp9_metadata_bsf.c
vp9_metadata: Improve spec-compliance and warnings
[ffmpeg] / libavcodec / vp9_metadata_bsf.c
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
30     CodedBitstreamContext *cbc;
31     CodedBitstreamFragment fragment;
32
33     int color_space;
34     int color_range;
35
36     int color_warnings;
37 } VP9MetadataContext;
38
39
40 static int vp9_metadata_filter(AVBSFContext *bsf, AVPacket *pkt)
41 {
42     VP9MetadataContext *ctx = bsf->priv_data;
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         int profile = (header->profile_high_bit << 1) + header->profile_low_bit;
60
61         if (header->frame_type == VP9_KEY_FRAME ||
62             header->intra_only && profile > 0) {
63             if (ctx->color_space >= 0) {
64                 if (!(profile & 1) && ctx->color_space == VP9_CS_RGB) {
65                     if (!(ctx->color_warnings & 2)) {
66                         av_log(bsf, AV_LOG_WARNING, "Warning: RGB "
67                                "incompatible with profiles 0 and 2.\n");
68                         ctx->color_warnings |= 2;
69                     }
70                 } else
71                     header->color_space = ctx->color_space;
72             }
73
74             if (ctx->color_range >= 0)
75                 header->color_range = ctx->color_range;
76             if (header->color_space == VP9_CS_RGB) {
77                 if (!(ctx->color_warnings & 1) && !header->color_range) {
78                     av_log(bsf, AV_LOG_WARNING, "Warning: Color space RGB "
79                            "implicitly sets color range to PC range.\n");
80                     ctx->color_warnings |= 1;
81                 }
82                 header->color_range = 1;
83             }
84         } else if (!(ctx->color_warnings & 4) && header->intra_only && !profile &&
85                    ctx->color_space >= 0 && ctx->color_space != VP9_CS_BT_601) {
86             av_log(bsf, AV_LOG_WARNING, "Warning: Intra-only frames in "
87                    "profile 0 are automatically BT.601.\n");
88             ctx->color_warnings |= 4;
89         }
90     }
91
92     err = ff_cbs_write_packet(ctx->cbc, pkt, frag);
93     if (err < 0) {
94         av_log(bsf, AV_LOG_ERROR, "Failed to write packet.\n");
95         goto fail;
96     }
97
98     err = 0;
99 fail:
100     ff_cbs_fragment_reset(ctx->cbc, frag);
101
102     if (err < 0)
103         av_packet_unref(pkt);
104
105     return err;
106 }
107
108 static int vp9_metadata_init(AVBSFContext *bsf)
109 {
110     VP9MetadataContext *ctx = bsf->priv_data;
111
112     return ff_cbs_init(&ctx->cbc, AV_CODEC_ID_VP9, bsf);
113 }
114
115 static void vp9_metadata_close(AVBSFContext *bsf)
116 {
117     VP9MetadataContext *ctx = bsf->priv_data;
118
119     ff_cbs_fragment_free(ctx->cbc, &ctx->fragment);
120     ff_cbs_close(&ctx->cbc);
121 }
122
123 #define OFFSET(x) offsetof(VP9MetadataContext, x)
124 #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_BSF_PARAM)
125 static const AVOption vp9_metadata_options[] = {
126     { "color_space", "Set colour space (section 7.2.2)",
127         OFFSET(color_space), AV_OPT_TYPE_INT,
128         { .i64 = -1 }, -1, VP9_CS_RGB, FLAGS, "cs" },
129     { "unknown",  "Unknown/unspecified",  0, AV_OPT_TYPE_CONST,
130         { .i64 = VP9_CS_UNKNOWN   }, .flags = FLAGS, .unit = "cs" },
131     { "bt601",    "ITU-R BT.601-7",       0, AV_OPT_TYPE_CONST,
132         { .i64 = VP9_CS_BT_601    }, .flags = FLAGS, .unit = "cs" },
133     { "bt709",    "ITU-R BT.709-6",       0, AV_OPT_TYPE_CONST,
134         { .i64 = VP9_CS_BT_709    }, .flags = FLAGS, .unit = "cs" },
135     { "smpte170", "SMPTE-170",            0, AV_OPT_TYPE_CONST,
136         { .i64 = VP9_CS_SMPTE_170 }, .flags = FLAGS, .unit = "cs" },
137     { "smpte240", "SMPTE-240",            0, AV_OPT_TYPE_CONST,
138         { .i64 = VP9_CS_SMPTE_240 }, .flags = FLAGS, .unit = "cs" },
139     { "bt2020",   "ITU-R BT.2020-2",      0, AV_OPT_TYPE_CONST,
140         { .i64 = VP9_CS_BT_2020   }, .flags = FLAGS, .unit = "cs" },
141     { "rgb",      "sRGB / IEC 61966-2-1", 0, AV_OPT_TYPE_CONST,
142         { .i64 = VP9_CS_RGB       }, .flags = FLAGS, .unit = "cs" },
143
144     { "color_range", "Set colour range (section 7.2.2)",
145         OFFSET(color_range), AV_OPT_TYPE_INT,
146         { .i64 = -1 }, -1, 1, FLAGS, "cr" },
147     { "tv", "TV (limited) range", 0, AV_OPT_TYPE_CONST,
148         { .i64 = 0 }, .flags = FLAGS, .unit = "cr" },
149     { "pc", "PC (full) range",    0, AV_OPT_TYPE_CONST,
150         { .i64 = 1 }, .flags = FLAGS, .unit = "cr" },
151
152     { NULL }
153 };
154
155 static const AVClass vp9_metadata_class = {
156     .class_name = "vp9_metadata_bsf",
157     .item_name  = av_default_item_name,
158     .option     = vp9_metadata_options,
159     .version    = LIBAVUTIL_VERSION_INT,
160 };
161
162 static const enum AVCodecID vp9_metadata_codec_ids[] = {
163     AV_CODEC_ID_VP9, AV_CODEC_ID_NONE,
164 };
165
166 const AVBitStreamFilter ff_vp9_metadata_bsf = {
167     .name           = "vp9_metadata",
168     .priv_data_size = sizeof(VP9MetadataContext),
169     .priv_class     = &vp9_metadata_class,
170     .init           = &vp9_metadata_init,
171     .close          = &vp9_metadata_close,
172     .filter         = &vp9_metadata_filter,
173     .codec_ids      = vp9_metadata_codec_ids,
174 };