FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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  AVPacket *in = NULL;
44  CodedBitstreamFragment *frag = &ctx->fragment;
45  int err, i;
46 
47  err = ff_bsf_get_packet(bsf, &in);
48  if (err < 0)
49  return err;
50 
51  err = ff_cbs_read_packet(ctx->cbc, frag, in);
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 
61  if (ctx->color_space >= 0) {
62  header->color_space = ctx->color_space;
63  }
64  if (ctx->color_range >= 0) {
65  if (ctx->color_range == 0 &&
66  header->color_space == VP9_CS_RGB &&
67  !ctx->color_range_rgb_warned) {
68  av_log(bsf, AV_LOG_WARNING, "Warning: color_range cannot "
69  "be set to limited in RGB streams.\n");
70  ctx->color_range_rgb_warned = 1;
71  } else {
72  header->color_range = ctx->color_range;
73  }
74  }
75  }
76 
77  err = ff_cbs_write_packet(ctx->cbc, out, frag);
78  if (err < 0) {
79  av_log(bsf, AV_LOG_ERROR, "Failed to write packet.\n");
80  goto fail;
81  }
82 
83  err = av_packet_copy_props(out, in);
84  if (err < 0)
85  goto fail;
86 
87  err = 0;
88 fail:
89  ff_cbs_fragment_uninit(ctx->cbc, frag);
90 
91  if (err < 0)
92  av_packet_unref(out);
93  av_packet_free(&in);
94 
95  return err;
96 }
97 
99 {
101 
102  return ff_cbs_init(&ctx->cbc, AV_CODEC_ID_VP9, bsf);
103 }
104 
106 {
108  ff_cbs_close(&ctx->cbc);
109 }
110 
111 #define OFFSET(x) offsetof(VP9MetadataContext, x)
112 #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_BSF_PARAM)
113 static const AVOption vp9_metadata_options[] = {
114  { "color_space", "Set colour space (section 7.2.2)",
115  OFFSET(color_space), AV_OPT_TYPE_INT,
116  { .i64 = -1 }, -1, VP9_CS_RGB, FLAGS, "cs" },
117  { "unknown", "Unknown/unspecified", 0, AV_OPT_TYPE_CONST,
118  { .i64 = VP9_CS_UNKNOWN }, .flags = FLAGS, .unit = "cs" },
119  { "bt601", "ITU-R BT.601-7", 0, AV_OPT_TYPE_CONST,
120  { .i64 = VP9_CS_BT_601 }, .flags = FLAGS, .unit = "cs" },
121  { "bt709", "ITU-R BT.709-6", 0, AV_OPT_TYPE_CONST,
122  { .i64 = VP9_CS_BT_709 }, .flags = FLAGS, .unit = "cs" },
123  { "smpte170", "SMPTE-170", 0, AV_OPT_TYPE_CONST,
124  { .i64 = VP9_CS_SMPTE_170 }, .flags = FLAGS, .unit = "cs" },
125  { "smpte240", "SMPTE-240", 0, AV_OPT_TYPE_CONST,
126  { .i64 = VP9_CS_SMPTE_240 }, .flags = FLAGS, .unit = "cs" },
127  { "bt2020", "ITU-R BT.2020-2", 0, AV_OPT_TYPE_CONST,
128  { .i64 = VP9_CS_BT_2020 }, .flags = FLAGS, .unit = "cs" },
129  { "rgb", "sRGB / IEC 61966-2-1", 0, AV_OPT_TYPE_CONST,
130  { .i64 = VP9_CS_RGB }, .flags = FLAGS, .unit = "cs" },
131 
132  { "color_range", "Set colour range (section 7.2.2)",
134  { .i64 = -1 }, -1, 1, FLAGS, "cr" },
135  { "tv", "TV (limited) range", 0, AV_OPT_TYPE_CONST,
136  { .i64 = 0 }, .flags = FLAGS, .unit = "cr" },
137  { "pc", "PC (full) range", 0, AV_OPT_TYPE_CONST,
138  { .i64 = 1 }, .flags = FLAGS, .unit = "cr" },
139 
140  { NULL }
141 };
142 
143 static const AVClass vp9_metadata_class = {
144  .class_name = "vp9_metadata_bsf",
145  .item_name = av_default_item_name,
146  .option = vp9_metadata_options,
147  .version = LIBAVUTIL_VERSION_INT,
148 };
149 
150 static const enum AVCodecID vp9_metadata_codec_ids[] = {
152 };
153 
155  .name = "vp9_metadata",
156  .priv_data_size = sizeof(VP9MetadataContext),
157  .priv_class = &vp9_metadata_class,
159  .close = &vp9_metadata_close,
162 };
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 *out)
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:343
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:5703
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:191
color_range
void * priv_data
Opaque filter-specific private data.
Definition: avcodec.h:5724
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
void av_packet_free(AVPacket **pkt)
Free the packet, if the packet is reference counted, it will be unreferenced first.
Definition: avpacket.c:62
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:233
const char * name
Definition: avcodec.h:5753
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.
Definition: cbs.h:153
#define av_log(a,...)
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
void ff_cbs_fragment_uninit(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag)
Free all allocated memory in a fragment.
Definition: cbs.c:139
#define fail()
Definition: checkasm.h:117
int av_packet_copy_props(AVPacket *dst, const AVPacket *src)
Copy only "properties" fields from src to dst.
Definition: avpacket.c:564
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:598
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31))))#defineSET_CONV_FUNC_GROUP(ofmt, ifmt) staticvoidset_generic_function(AudioConvert *ac){}voidff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, intsample_rate, intapply_map){AudioConvert *ac;intin_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) returnNULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method!=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt)>2){ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc){av_free(ac);returnNULL;}returnac;}in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}elseif(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;elseac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);returnac;}intff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){intuse_generic=1;intlen=in->nb_samples;intp;if(ac->dc){av_log(ac->avr, AV_LOG_TRACE,"%dsamples-audio_convert:%sto%s(dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));returnff_convert_dither(ac-> in
Describe the class of an AVClass context structure.
Definition: log.h:67
Context structure for coded bitstream operations.
Definition: cbs.h:159
#define FLAGS
void ff_cbs_close(CodedBitstreamContext **ctx_ptr)
Close a context and free all internal state.
Definition: cbs.c:113
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[]
int ff_bsf_get_packet(AVBSFContext *ctx, AVPacket **pkt)
Called by the bitstream filters to get the next packet for filtering.
Definition: bsf.c:216
FILE * out
Definition: movenc.c:54
This structure stores compressed data.
Definition: avcodec.h:1422