FFmpeg
prores_metadata_bsf.c
Go to the documentation of this file.
1 /*
2  * Prores Metadata bitstream filter
3  * Copyright (c) 2018 Jokyo Images
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 /**
23  * @file
24  * Prores Metadata bitstream filter
25  * set frame colorspace property
26  */
27 
28 #include "libavutil/common.h"
29 #include "libavutil/intreadwrite.h"
30 #include "libavutil/opt.h"
31 #include "bsf.h"
32 
33 typedef struct ProresMetadataContext {
34  const AVClass *class;
35 
40 
42 {
44  int ret = 0;
45  int buf_size;
46  uint8_t *buf;
47 
49  if (ret < 0)
50  return ret;
51 
53  if (ret < 0)
54  goto fail;
55 
56  buf = pkt->data;
57  buf_size = pkt->size;
58 
59  /* check start of the prores frame */
60  if (buf_size < 28) {
61  av_log(bsf, AV_LOG_ERROR, "not enough data in prores frame\n");
63  goto fail;
64  }
65 
66  if (AV_RL32(buf + 4) != AV_RL32("icpf")) {
67  av_log(bsf, AV_LOG_ERROR, "invalid frame header\n");
69  goto fail;
70  }
71 
72  if (AV_RB16(buf + 8) < 28) {
73  av_log(bsf, AV_LOG_ERROR, "invalid frame header size\n");
75  goto fail;
76  }
77 
78  /* set the new values */
79  if (ctx->color_primaries != -1)
80  buf[8+14] = ctx->color_primaries;
81  if (ctx->transfer_characteristics != -1)
82  buf[8+15] = ctx->transfer_characteristics;
83  if (ctx->matrix_coefficients != -1)
84  buf[8+16] = ctx->matrix_coefficients;
85 
86 fail:
87  if (ret < 0)
89  return ret;
90 }
91 
92 static const enum AVCodecID codec_ids[] = {
94 };
95 
97 {
99  /*! check options */
100  switch (ctx->color_primaries) {
101  case -1:
102  case 0:
103  case AVCOL_PRI_BT709:
104  case AVCOL_PRI_BT470BG:
105  case AVCOL_PRI_SMPTE170M:
106  case AVCOL_PRI_BT2020:
107  case AVCOL_PRI_SMPTE431:
108  case AVCOL_PRI_SMPTE432:
109  break;
110  default:
111  av_log(bsf, AV_LOG_ERROR, "Color primaries %d is not a valid value\n", ctx->color_primaries);
112  return AVERROR(EINVAL);
113  }
114 
115  switch (ctx->matrix_coefficients) {
116  case -1:
117  case 0:
118  case AVCOL_SPC_BT709:
119  case AVCOL_SPC_SMPTE170M:
121  break;
122  default:
123  av_log(bsf, AV_LOG_ERROR, "Colorspace %d is not a valid value\n", ctx->matrix_coefficients);
124  return AVERROR(EINVAL);
125  }
126 
127  return 0;
128 }
129 
130 #define OFFSET(x) offsetof(ProresMetadataContext, x)
131 #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_BSF_PARAM)
132 static const AVOption options[] = {
133  {"color_primaries", "select color primaries", OFFSET(color_primaries), AV_OPT_TYPE_INT, {.i64=-1}, -1, AVCOL_PRI_SMPTE432, FLAGS, "color_primaries"},
134  {"auto", "keep the same color primaries", 0, AV_OPT_TYPE_CONST, {.i64=-1}, INT_MIN, INT_MAX, FLAGS, "color_primaries"},
135  {"unknown", NULL, 0, AV_OPT_TYPE_CONST, {.i64=0}, INT_MIN, INT_MAX, FLAGS, "color_primaries"},
136  {"bt709", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_PRI_BT709}, INT_MIN, INT_MAX, FLAGS, "color_primaries"},
137  {"bt470bg", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_PRI_BT470BG}, INT_MIN, INT_MAX, FLAGS, "color_primaries"},
138  {"smpte170m", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_PRI_SMPTE170M}, INT_MIN, INT_MAX, FLAGS, "color_primaries"},
139  {"bt2020", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_PRI_BT2020}, INT_MIN, INT_MAX, FLAGS, "color_primaries"},
140  {"smpte431", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_PRI_SMPTE431}, INT_MIN, INT_MAX, FLAGS, "color_primaries"},
141  {"smpte432", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_PRI_SMPTE432}, INT_MIN, INT_MAX, FLAGS, "color_primaries"},
142 
143  {"color_trc", "select color transfer", OFFSET(transfer_characteristics), AV_OPT_TYPE_INT, {.i64=-1}, -1, AVCOL_TRC_BT709, FLAGS, "color_trc"},
144  {"auto", "keep the same color transfer", 0, AV_OPT_TYPE_CONST, {.i64=-1}, INT_MIN, INT_MAX, FLAGS, "color_trc"},
145  {"unknown", NULL, 0, AV_OPT_TYPE_CONST, {.i64=0}, INT_MIN, INT_MAX, FLAGS, "color_trc"},
146  {"bt709", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_TRC_BT709}, INT_MIN, INT_MAX, FLAGS, "color_trc"},
147 
148  {"colorspace", "select colorspace", OFFSET(matrix_coefficients), AV_OPT_TYPE_INT, {.i64=-1}, -1, AVCOL_SPC_BT2020_NCL, FLAGS, "colorspace"},
149  {"auto", "keep the same colorspace", 0, AV_OPT_TYPE_CONST, {.i64=-1}, INT_MIN, INT_MAX, FLAGS, "colorspace"},
150  {"unknown", NULL, 0, AV_OPT_TYPE_CONST, {.i64=0}, INT_MIN, INT_MAX, FLAGS, "colorspace"},
151  {"bt709", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_SPC_BT709}, INT_MIN, INT_MAX, FLAGS, "colorspace"},
152  {"smpte170m", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_SPC_SMPTE170M}, INT_MIN, INT_MAX, FLAGS, "colorspace"},
153  {"bt2020nc", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_SPC_BT2020_NCL}, INT_MIN, INT_MAX, FLAGS, "colorspace"},
154 
155  { NULL },
156 };
157 
159  .class_name = "prores_metadata_bsf",
160  .item_name = av_default_item_name,
161  .option = options,
162  .version = LIBAVUTIL_VERSION_INT,
163 };
164 
166  .name = "prores_metadata",
167  .init = prores_metadata_init,
168  .filter = prores_metadata,
169  .priv_data_size = sizeof(ProresMetadataContext),
170  .priv_class = &prores_metadata_class,
171  .codec_ids = codec_ids,
172 };
ProresMetadataContext::matrix_coefficients
int matrix_coefficients
Definition: prores_metadata_bsf.c:38
av_packet_unref
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:599
prores_metadata_class
static const AVClass prores_metadata_class
Definition: prores_metadata_bsf.c:158
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
opt.h
AVBitStreamFilter::name
const char * name
Definition: avcodec.h:5813
options
static const AVOption options[]
Definition: prores_metadata_bsf.c:132
AVPacket::data
uint8_t * data
Definition: avcodec.h:1477
AVOption
AVOption.
Definition: opt.h:246
AVBSFContext
The bitstream filter state.
Definition: avcodec.h:5763
bsf.h
fail
#define fail()
Definition: checkasm.h:120
prores_metadata_init
static int prores_metadata_init(AVBSFContext *bsf)
Definition: prores_metadata_bsf.c:96
ProresMetadataContext::transfer_characteristics
int transfer_characteristics
Definition: prores_metadata_bsf.c:37
ProresMetadataContext
Definition: prores_metadata_bsf.c:33
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
buf
void * buf
Definition: avisynth_c.h:766
intreadwrite.h
AVCOL_SPC_SMPTE170M
@ AVCOL_SPC_SMPTE170M
also ITU-R BT601-6 525 / ITU-R BT1358 525 / ITU-R BT1700 NTSC
Definition: pixfmt.h:503
ctx
AVFormatContext * ctx
Definition: movenc.c:48
ff_prores_metadata_bsf
const AVBitStreamFilter ff_prores_metadata_bsf
Definition: prores_metadata_bsf.c:165
AVCOL_PRI_BT470BG
@ AVCOL_PRI_BT470BG
also ITU-R BT601-6 625 / ITU-R BT1358 625 / ITU-R BT1700 625 PAL & SECAM
Definition: pixfmt.h:450
AVCOL_PRI_SMPTE170M
@ AVCOL_PRI_SMPTE170M
also ITU-R BT601-6 525 / ITU-R BT1358 525 / ITU-R BT1700 NTSC
Definition: pixfmt.h:451
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
AVCOL_PRI_BT709
@ AVCOL_PRI_BT709
also ITU-R BT1361 / IEC 61966-2-4 / SMPTE RP177 Annex B
Definition: pixfmt.h:445
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:191
AVCodecID
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: avcodec.h:215
AVCOL_PRI_BT2020
@ AVCOL_PRI_BT2020
ITU-R BT2020.
Definition: pixfmt.h:454
AVCOL_PRI_SMPTE431
@ AVCOL_PRI_SMPTE431
SMPTE ST 431-2 (2011) / DCI P3.
Definition: pixfmt.h:457
AVPacket::size
int size
Definition: avcodec.h:1478
OFFSET
#define OFFSET(x)
Definition: prores_metadata_bsf.c:130
transfer_characteristics
static const struct TransferCharacteristics transfer_characteristics[AVCOL_TRC_NB]
Definition: vf_colorspace.c:176
AVCOL_TRC_BT709
@ AVCOL_TRC_BT709
also ITU-R BT1361
Definition: pixfmt.h:469
AV_CODEC_ID_NONE
@ AV_CODEC_ID_NONE
Definition: avcodec.h:216
AVCOL_SPC_BT2020_NCL
@ AVCOL_SPC_BT2020_NCL
ITU-R BT2020 non-constant luminance system.
Definition: pixfmt.h:507
common.h
codec_ids
static enum AVCodecID codec_ids[]
Definition: prores_metadata_bsf.c:92
uint8_t
uint8_t
Definition: audio_convert.c:194
AVBSFContext::priv_data
void * priv_data
Opaque filter-specific private data.
Definition: avcodec.h:5784
ret
ret
Definition: filter_design.txt:187
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
FLAGS
#define FLAGS
Definition: prores_metadata_bsf.c:131
AV_RL32
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:88
pkt
static AVPacket pkt
Definition: demuxing_decoding.c:54
AVBitStreamFilter
Definition: avcodec.h:5812
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Definition: opt.h:223
av_packet_make_writable
int av_packet_make_writable(AVPacket *pkt)
Create a writable reference for the data described by a given packet, avoiding data copy if possible.
Definition: avpacket.c:682
AVCOL_PRI_SMPTE432
@ AVCOL_PRI_SMPTE432
SMPTE ST 432-1 (2010) / P3 D65 / Display P3.
Definition: pixfmt.h:458
AVPacket
This structure stores compressed data.
Definition: avcodec.h:1454
ProresMetadataContext::color_primaries
int color_primaries
Definition: prores_metadata_bsf.c:36
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
prores_metadata
static int prores_metadata(AVBSFContext *bsf, AVPacket *pkt)
Definition: prores_metadata_bsf.c:41
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:239
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
AVCOL_SPC_BT709
@ AVCOL_SPC_BT709
also ITU-R BT1361 / IEC 61966-2-4 xvYCC709 / SMPTE RP177 Annex B
Definition: pixfmt.h:498
AV_OPT_TYPE_CONST
@ AV_OPT_TYPE_CONST
Definition: opt.h:232
color_primaries
static const struct ColorPrimaries color_primaries[AVCOL_PRI_NB]
Definition: vf_colorspace.c:209
AV_CODEC_ID_PRORES
@ AV_CODEC_ID_PRORES
Definition: avcodec.h:366
AV_RB16
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_WB32 unsigned int_TMPL AV_WB24 unsigned int_TMPL AV_RB16
Definition: bytestream.h:94