2 * This file is part of FFmpeg.
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.
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.
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
19 #include "libavutil/avstring.h"
20 #include "libavutil/common.h"
21 #include "libavutil/opt.h"
26 #include "cbs_mpeg2.h"
29 typedef struct MPEG2MetadataContext {
32 MPEG2RawExtensionData sequence_display_extension;
34 AVRational display_aspect_ratio;
36 AVRational frame_rate;
40 int transfer_characteristics;
41 int matrix_coefficients;
44 } MPEG2MetadataContext;
47 static int mpeg2_metadata_update_fragment(AVBSFContext *bsf,
49 CodedBitstreamFragment *frag)
51 MPEG2MetadataContext *ctx = bsf->priv_data;
52 MPEG2RawSequenceHeader *sh = NULL;
53 MPEG2RawSequenceExtension *se = NULL;
54 MPEG2RawSequenceDisplayExtension *sde = NULL;
57 for (i = 0; i < frag->nb_units; i++) {
58 if (frag->units[i].type == MPEG2_START_SEQUENCE_HEADER) {
59 sh = frag->units[i].content;
60 } else if (frag->units[i].type == MPEG2_START_EXTENSION) {
61 MPEG2RawExtensionData *ext = frag->units[i].content;
62 if (ext->extension_start_code_identifier ==
63 MPEG2_EXTENSION_SEQUENCE) {
64 se = &ext->data.sequence;
66 } else if (ext->extension_start_code_identifier ==
67 MPEG2_EXTENSION_SEQUENCE_DISPLAY) {
68 sde = &ext->data.sequence_display;
74 // No sequence header and sequence extension: not an MPEG-2 video
76 if (sh && !ctx->mpeg1_warned) {
77 av_log(bsf, AV_LOG_WARNING, "Stream contains a sequence "
78 "header but not a sequence extension: maybe it's "
79 "actually MPEG-1?\n");
80 ctx->mpeg1_warned = 1;
85 if (ctx->display_aspect_ratio.num && ctx->display_aspect_ratio.den) {
88 av_reduce(&num, &den, ctx->display_aspect_ratio.num,
89 ctx->display_aspect_ratio.den, 65535);
91 if (num == 4 && den == 3)
92 sh->aspect_ratio_information = 2;
93 else if (num == 16 && den == 9)
94 sh->aspect_ratio_information = 3;
95 else if (num == 221 && den == 100)
96 sh->aspect_ratio_information = 4;
98 sh->aspect_ratio_information = 1;
101 if (ctx->frame_rate.num && ctx->frame_rate.den) {
102 int code, ext_n, ext_d;
104 ff_mpeg12_find_best_frame_rate(ctx->frame_rate,
105 &code, &ext_n, &ext_d, 0);
107 sh->frame_rate_code = code;
108 se->frame_rate_extension_n = ext_n;
109 se->frame_rate_extension_d = ext_d;
112 if (ctx->video_format >= 0 ||
113 ctx->colour_primaries >= 0 ||
114 ctx->transfer_characteristics >= 0 ||
115 ctx->matrix_coefficients >= 0) {
118 ctx->sequence_display_extension.extension_start_code =
119 MPEG2_START_EXTENSION;
120 ctx->sequence_display_extension.extension_start_code_identifier =
121 MPEG2_EXTENSION_SEQUENCE_DISPLAY;
122 sde = &ctx->sequence_display_extension.data.sequence_display;
124 *sde = (MPEG2RawSequenceDisplayExtension) {
127 .colour_description = 0,
128 .colour_primaries = 2,
129 .transfer_characteristics = 2,
130 .matrix_coefficients = 2,
132 .display_horizontal_size =
133 se->horizontal_size_extension << 12 | sh->horizontal_size_value,
134 .display_vertical_size =
135 se->vertical_size_extension << 12 | sh->vertical_size_value,
138 err = ff_cbs_insert_unit_content(frag, se_pos + 1,
139 MPEG2_START_EXTENSION,
140 &ctx->sequence_display_extension,
143 av_log(bsf, AV_LOG_ERROR, "Failed to insert new sequence "
144 "display extension.\n");
149 if (ctx->video_format >= 0)
150 sde->video_format = ctx->video_format;
152 if (ctx->colour_primaries >= 0 ||
153 ctx->transfer_characteristics >= 0 ||
154 ctx->matrix_coefficients >= 0) {
155 sde->colour_description = 1;
157 if (ctx->colour_primaries >= 0)
158 sde->colour_primaries = ctx->colour_primaries;
160 if (ctx->transfer_characteristics >= 0)
161 sde->transfer_characteristics = ctx->transfer_characteristics;
163 if (ctx->matrix_coefficients >= 0)
164 sde->matrix_coefficients = ctx->matrix_coefficients;
171 static const CBSBSFType mpeg2_metadata_type = {
172 .codec_id = AV_CODEC_ID_MPEG2VIDEO,
173 .fragment_name = "frame",
174 .unit_name = "start code",
175 .update_fragment = &mpeg2_metadata_update_fragment,
178 static int mpeg2_metadata_init(AVBSFContext *bsf)
180 MPEG2MetadataContext *ctx = bsf->priv_data;
182 #define VALIDITY_CHECK(name) do { \
184 av_log(bsf, AV_LOG_ERROR, "The value 0 for %s is " \
185 "forbidden.\n", #name); \
186 return AVERROR(EINVAL); \
189 VALIDITY_CHECK(colour_primaries);
190 VALIDITY_CHECK(transfer_characteristics);
191 VALIDITY_CHECK(matrix_coefficients);
192 #undef VALIDITY_CHECK
194 return ff_cbs_bsf_generic_init(bsf, &mpeg2_metadata_type);
197 #define OFFSET(x) offsetof(MPEG2MetadataContext, x)
198 #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_BSF_PARAM)
199 static const AVOption mpeg2_metadata_options[] = {
200 { "display_aspect_ratio", "Set display aspect ratio (table 6-3)",
201 OFFSET(display_aspect_ratio), AV_OPT_TYPE_RATIONAL,
202 { .dbl = 0.0 }, 0, 65535, FLAGS },
204 { "frame_rate", "Set frame rate",
205 OFFSET(frame_rate), AV_OPT_TYPE_RATIONAL,
206 { .dbl = 0.0 }, 0, UINT_MAX, FLAGS },
208 { "video_format", "Set video format (table 6-6)",
209 OFFSET(video_format), AV_OPT_TYPE_INT,
210 { .i64 = -1 }, -1, 7, FLAGS },
211 { "colour_primaries", "Set colour primaries (table 6-7)",
212 OFFSET(colour_primaries), AV_OPT_TYPE_INT,
213 { .i64 = -1 }, -1, 255, FLAGS },
214 { "transfer_characteristics", "Set transfer characteristics (table 6-8)",
215 OFFSET(transfer_characteristics), AV_OPT_TYPE_INT,
216 { .i64 = -1 }, -1, 255, FLAGS },
217 { "matrix_coefficients", "Set matrix coefficients (table 6-9)",
218 OFFSET(matrix_coefficients), AV_OPT_TYPE_INT,
219 { .i64 = -1 }, -1, 255, FLAGS },
224 static const AVClass mpeg2_metadata_class = {
225 .class_name = "mpeg2_metadata_bsf",
226 .item_name = av_default_item_name,
227 .option = mpeg2_metadata_options,
228 .version = LIBAVUTIL_VERSION_INT,
231 static const enum AVCodecID mpeg2_metadata_codec_ids[] = {
232 AV_CODEC_ID_MPEG2VIDEO, AV_CODEC_ID_NONE,
235 const AVBitStreamFilter ff_mpeg2_metadata_bsf = {
236 .name = "mpeg2_metadata",
237 .priv_data_size = sizeof(MPEG2MetadataContext),
238 .priv_class = &mpeg2_metadata_class,
239 .init = &mpeg2_metadata_init,
240 .close = &ff_cbs_bsf_generic_close,
241 .filter = &ff_cbs_bsf_generic_filter,
242 .codec_ids = mpeg2_metadata_codec_ids,