GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavcodec/prores_metadata_bsf.c Lines: 30 48 62.5 %
Date: 2019-11-18 18:00:01 Branches: 12 22 54.5 %

Line Branch Exec Source
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
36
    int color_primaries;
37
    int transfer_characteristics;
38
    int matrix_coefficients;
39
} ProresMetadataContext;
40
41
4
static int prores_metadata(AVBSFContext *bsf, AVPacket *pkt)
42
{
43
4
    ProresMetadataContext *ctx = bsf->priv_data;
44
4
    int ret = 0;
45
    int buf_size;
46
    uint8_t *buf;
47
48
4
    ret = ff_bsf_get_packet_ref(bsf, pkt);
49
4
    if (ret < 0)
50
2
        return ret;
51
52
2
    ret = av_packet_make_writable(pkt);
53
2
    if (ret < 0)
54
        goto fail;
55
56
2
    buf = pkt->data;
57
2
    buf_size = pkt->size;
58
59
    /* check start of the prores frame */
60
2
    if (buf_size < 28) {
61
        av_log(bsf, AV_LOG_ERROR, "not enough data in prores frame\n");
62
        ret = AVERROR_INVALIDDATA;
63
        goto fail;
64
    }
65
66
2
    if (AV_RL32(buf + 4) != AV_RL32("icpf")) {
67
        av_log(bsf, AV_LOG_ERROR, "invalid frame header\n");
68
        ret = AVERROR_INVALIDDATA;
69
        goto fail;
70
    }
71
72
2
    if (AV_RB16(buf + 8) < 28) {
73
        av_log(bsf, AV_LOG_ERROR, "invalid frame header size\n");
74
        ret = AVERROR_INVALIDDATA;
75
        goto fail;
76
    }
77
78
    /* set the new values */
79
2
    if (ctx->color_primaries != -1)
80
2
        buf[8+14] = ctx->color_primaries;
81
2
    if (ctx->transfer_characteristics != -1)
82
2
        buf[8+15] = ctx->transfer_characteristics;
83
2
    if (ctx->matrix_coefficients != -1)
84
2
        buf[8+16] = ctx->matrix_coefficients;
85
86
fail:
87
2
    if (ret < 0)
88
        av_packet_unref(pkt);
89
2
    return ret;
90
}
91
92
static const enum AVCodecID codec_ids[] = {
93
    AV_CODEC_ID_PRORES, AV_CODEC_ID_NONE,
94
};
95
96
1
static int prores_metadata_init(AVBSFContext *bsf)
97
{
98
1
    ProresMetadataContext *ctx = bsf->priv_data;
99
    /*! check options */
100
1
    switch (ctx->color_primaries) {
101
1
    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
1
        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
1
    switch (ctx->matrix_coefficients) {
116
1
    case -1:
117
    case 0:
118
    case AVCOL_SPC_BT709:
119
    case AVCOL_SPC_SMPTE170M:
120
    case AVCOL_SPC_BT2020_NCL:
121
1
        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
1
    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
158
static const AVClass prores_metadata_class = {
159
    .class_name = "prores_metadata_bsf",
160
    .item_name  = av_default_item_name,
161
    .option     = options,
162
    .version    = LIBAVUTIL_VERSION_INT,
163
};
164
165
const AVBitStreamFilter ff_prores_metadata_bsf = {
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
};