From 0aba92d42d5194aee335f623bcb70831cd71b0af Mon Sep 17 00:00:00 2001 From: Martin Vignali Date: Sun, 28 Oct 2018 12:53:25 +0100 Subject: [PATCH] avcodec : add prores_metadata bsf for set the color property of each prores frame --- doc/bitstream_filters.texi | 66 ++++++++++++ libavcodec/Makefile | 1 + libavcodec/bitstream_filters.c | 1 + libavcodec/prores_metadata_bsf.c | 172 +++++++++++++++++++++++++++++++ 4 files changed, 240 insertions(+) create mode 100644 libavcodec/prores_metadata_bsf.c diff --git a/doc/bitstream_filters.texi b/doc/bitstream_filters.texi index 53470c01ec..655a2c1e63 100644 --- a/doc/bitstream_filters.texi +++ b/doc/bitstream_filters.texi @@ -530,6 +530,72 @@ ffmpeg -i INPUT -c copy -bsf noise[=1] output.mkv @section null This bitstream filter passes the packets through unchanged. +@section prores_metadata + +Modify color property metadata embedded in prores stream. + +@table @option +@item color_primaries +Set the color primaries. +Available values are: + +@table @samp +@item auto +Keep the same color primaries property (default). + +@item unknown +@item bt709 +@item bt470bg +BT601 625 + +@item smpte170m +BT601 525 + +@item bt2020 +@item smpte431 +DCI P3 + +@item smpte432 +P3 D65 + +@end table + +@item transfer_characteristics +Set the color transfert. +Available values are: + +@table @samp +@item auto +Keep the same transfer characteristics property (default). + +@item unknown +@item bt709 +BT 601, BT 709, BT 2020 +@end table + + +@item matrix_coefficients +Set the matrix coefficient. +Available values are: + +@table @samp +@item auto +Keep the same transfer characteristics property (default). + +@item unknown +@item bt709 +@item smpte170m +BT 601 + +@item bt2020nc +@end table +@end table + +Set Rec709 colorspace for each frame of the file +@example +ffmpeg -i INPUT -c copy -bsf:v prores_metadata=color_primaries=bt709:color_trc=bt709:colorspace=bt709 output.mov +@end example + @section remove_extra Remove extradata from packets. diff --git a/libavcodec/Makefile b/libavcodec/Makefile index 8643da8f2b..05be02ec7d 100644 --- a/libavcodec/Makefile +++ b/libavcodec/Makefile @@ -1081,6 +1081,7 @@ OBJS-$(CONFIG_MP3_HEADER_DECOMPRESS_BSF) += mp3_header_decompress_bsf.o \ OBJS-$(CONFIG_MPEG2_METADATA_BSF) += mpeg2_metadata_bsf.o OBJS-$(CONFIG_NOISE_BSF) += noise_bsf.o OBJS-$(CONFIG_NULL_BSF) += null_bsf.o +OBJS-$(CONFIG_PRORES_METADATA_BSF) += prores_metadata_bsf.o OBJS-$(CONFIG_REMOVE_EXTRADATA_BSF) += remove_extradata_bsf.o OBJS-$(CONFIG_TEXT2MOVSUB_BSF) += movsub_bsf.o OBJS-$(CONFIG_TRACE_HEADERS_BSF) += trace_headers_bsf.o diff --git a/libavcodec/bitstream_filters.c b/libavcodec/bitstream_filters.c index 96b1746a75..2a8598bac2 100644 --- a/libavcodec/bitstream_filters.c +++ b/libavcodec/bitstream_filters.c @@ -47,6 +47,7 @@ extern const AVBitStreamFilter ff_mpeg4_unpack_bframes_bsf; extern const AVBitStreamFilter ff_mov2textsub_bsf; extern const AVBitStreamFilter ff_noise_bsf; extern const AVBitStreamFilter ff_null_bsf; +extern const AVBitStreamFilter ff_prores_metadata_bsf; extern const AVBitStreamFilter ff_remove_extradata_bsf; extern const AVBitStreamFilter ff_text2movsub_bsf; extern const AVBitStreamFilter ff_trace_headers_bsf; diff --git a/libavcodec/prores_metadata_bsf.c b/libavcodec/prores_metadata_bsf.c new file mode 100644 index 0000000000..cc7a2b89f4 --- /dev/null +++ b/libavcodec/prores_metadata_bsf.c @@ -0,0 +1,172 @@ +/* + * Prores Metadata bitstream filter + * Copyright (c) 2018 Jokyo Images + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +/** + * @file + * Prores Metadata bitstream filter + * set frame colorspace property + */ + +#include "libavutil/common.h" +#include "libavutil/intreadwrite.h" +#include "libavutil/opt.h" +#include "bsf.h" + +typedef struct ProresMetadataContext { + const AVClass *class; + + int color_primaries; + int transfer_characteristics; + int matrix_coefficients; +} ProresMetadataContext; + +static int prores_metadata(AVBSFContext *bsf, AVPacket *pkt) +{ + ProresMetadataContext *ctx = bsf->priv_data; + int ret = 0; + int buf_size; + uint8_t *buf; + + ret = ff_bsf_get_packet_ref(bsf, pkt); + if (ret < 0) + return ret; + + ret = av_packet_make_writable(pkt); + if (ret < 0) + goto fail; + + buf = pkt->data; + buf_size = pkt->size; + + /* check start of the prores frame */ + if (buf_size < 28) { + av_log(bsf, AV_LOG_ERROR, "not enough data in prores frame\n"); + ret = AVERROR_INVALIDDATA; + goto fail; + } + + if (AV_RL32(buf + 4) != AV_RL32("icpf")) { + av_log(bsf, AV_LOG_ERROR, "invalid frame header\n"); + ret = AVERROR_INVALIDDATA; + goto fail; + } + + if (AV_RB16(buf + 8) < 28) { + av_log(bsf, AV_LOG_ERROR, "invalid frame header size\n"); + ret = AVERROR_INVALIDDATA; + goto fail; + } + + /* set the new values */ + if (ctx->color_primaries != -1) + buf[8+14] = ctx->color_primaries; + if (ctx->transfer_characteristics != -1) + buf[8+15] = ctx->transfer_characteristics; + if (ctx->matrix_coefficients != -1) + buf[8+16] = ctx->matrix_coefficients; + +fail: + if (ret < 0) + av_packet_unref(pkt); + return ret; +} + +static const enum AVCodecID codec_ids[] = { + AV_CODEC_ID_PRORES, AV_CODEC_ID_NONE, +}; + +static int prores_metadata_init(AVBSFContext *bsf) +{ + ProresMetadataContext *ctx = bsf->priv_data; + /*! check options */ + switch (ctx->color_primaries) { + case -1: + case 0: + case AVCOL_PRI_BT709: + case AVCOL_PRI_BT470BG: + case AVCOL_PRI_SMPTE170M: + case AVCOL_PRI_BT2020: + case AVCOL_PRI_SMPTE431: + case AVCOL_PRI_SMPTE432: + break; + default: + av_log(bsf, AV_LOG_ERROR, "Color primaries %d is not a valid value\n", ctx->color_primaries); + return AVERROR(EINVAL); + } + + switch (ctx->matrix_coefficients) { + case -1: + case 0: + case AVCOL_PRI_BT709: + case AVCOL_SPC_SMPTE170M: + case AVCOL_SPC_BT2020_NCL: + break; + default: + av_log(bsf, AV_LOG_ERROR, "Colorspace %d is not a valid value\n", ctx->matrix_coefficients); + return AVERROR(EINVAL); + } + + return 0; +} + +#define OFFSET(x) offsetof(ProresMetadataContext, x) +#define FLAGS (AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_BSF_PARAM) +static const AVOption options[] = { + {"color_primaries", "select color primaries", OFFSET(color_primaries), AV_OPT_TYPE_INT, {.i64=-1}, -1, AVCOL_PRI_SMPTE432, FLAGS, "color_primaries"}, + {"auto", "keep the same color primaries", 0, AV_OPT_TYPE_CONST, {.i64=-1}, INT_MIN, INT_MAX, FLAGS, "color_primaries"}, + {"unknown", NULL, 0, AV_OPT_TYPE_CONST, {.i64=0}, INT_MIN, INT_MAX, FLAGS, "color_primaries"}, + {"bt709", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_PRI_BT709}, INT_MIN, INT_MAX, FLAGS, "color_primaries"}, + {"bt470bg", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_PRI_BT470BG}, INT_MIN, INT_MAX, FLAGS, "color_primaries"}, + {"smpte170m", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_PRI_SMPTE170M}, INT_MIN, INT_MAX, FLAGS, "color_primaries"}, + {"bt2020", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_PRI_BT2020}, INT_MIN, INT_MAX, FLAGS, "color_primaries"}, + {"smpte431", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_PRI_SMPTE431}, INT_MIN, INT_MAX, FLAGS, "color_primaries"}, + {"smpte432", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_PRI_SMPTE432}, INT_MIN, INT_MAX, FLAGS, "color_primaries"}, + + {"color_trc", "select color transfer", OFFSET(transfer_characteristics), AV_OPT_TYPE_INT, {.i64=-1}, -1, AVCOL_TRC_BT709, FLAGS, "color_trc"}, + {"auto", "keep the same color transfer", 0, AV_OPT_TYPE_CONST, {.i64=-1}, INT_MIN, INT_MAX, FLAGS, "color_trc"}, + {"unknown", NULL, 0, AV_OPT_TYPE_CONST, {.i64=0}, INT_MIN, INT_MAX, FLAGS, "color_trc"}, + {"bt709", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_TRC_BT709}, INT_MIN, INT_MAX, FLAGS, "color_trc"}, + + {"colorspace", "select colorspace", OFFSET(matrix_coefficients), AV_OPT_TYPE_INT, {.i64=-1}, -1, AVCOL_SPC_BT2020_NCL, FLAGS, "colorspace"}, + {"auto", "keep the same colorspace", 0, AV_OPT_TYPE_CONST, {.i64=-1}, INT_MIN, INT_MAX, FLAGS, "colorspace"}, + {"unknown", NULL, 0, AV_OPT_TYPE_CONST, {.i64=0}, INT_MIN, INT_MAX, FLAGS, "colorspace"}, + {"bt709", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_SPC_BT709}, INT_MIN, INT_MAX, FLAGS, "colorspace"}, + {"smpte170m", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_SPC_SMPTE170M}, INT_MIN, INT_MAX, FLAGS, "colorspace"}, + {"bt2020nc", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AVCOL_SPC_BT2020_NCL}, INT_MIN, INT_MAX, FLAGS, "colorspace"}, + + { NULL }, +}; + +static const AVClass prores_metadata_class = { + .class_name = "prores_metadata_bsf", + .item_name = av_default_item_name, + .option = options, + .version = LIBAVUTIL_VERSION_INT, +}; + +const AVBitStreamFilter ff_prores_metadata_bsf = { + .name = "prores_metadata", + .init = prores_metadata_init, + .filter = prores_metadata, + .priv_data_size = sizeof(ProresMetadataContext), + .priv_class = &prores_metadata_class, + .codec_ids = codec_ids, +};