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"
24 #include "bsf_internal.h"
26 #include "cbs_mpeg2.h"
29 typedef struct MPEG2MetadataContext {
32 CodedBitstreamContext *cbc;
33 CodedBitstreamFragment fragment;
35 MPEG2RawExtensionData sequence_display_extension;
37 AVRational display_aspect_ratio;
39 AVRational frame_rate;
43 int transfer_characteristics;
44 int matrix_coefficients;
47 } MPEG2MetadataContext;
50 static int mpeg2_metadata_update_fragment(AVBSFContext *bsf,
51 CodedBitstreamFragment *frag)
53 MPEG2MetadataContext *ctx = bsf->priv_data;
54 MPEG2RawSequenceHeader *sh = NULL;
55 MPEG2RawSequenceExtension *se = NULL;
56 MPEG2RawSequenceDisplayExtension *sde = NULL;
59 for (i = 0; i < frag->nb_units; i++) {
60 if (frag->units[i].type == MPEG2_START_SEQUENCE_HEADER) {
61 sh = frag->units[i].content;
62 } else if (frag->units[i].type == MPEG2_START_EXTENSION) {
63 MPEG2RawExtensionData *ext = frag->units[i].content;
64 if (ext->extension_start_code_identifier ==
65 MPEG2_EXTENSION_SEQUENCE) {
66 se = &ext->data.sequence;
68 } else if (ext->extension_start_code_identifier ==
69 MPEG2_EXTENSION_SEQUENCE_DISPLAY) {
70 sde = &ext->data.sequence_display;
76 // No sequence header and sequence extension: not an MPEG-2 video
78 if (sh && !ctx->mpeg1_warned) {
79 av_log(bsf, AV_LOG_WARNING, "Stream contains a sequence "
80 "header but not a sequence extension: maybe it's "
81 "actually MPEG-1?\n");
82 ctx->mpeg1_warned = 1;
87 if (ctx->display_aspect_ratio.num && ctx->display_aspect_ratio.den) {
90 av_reduce(&num, &den, ctx->display_aspect_ratio.num,
91 ctx->display_aspect_ratio.den, 65535);
93 if (num == 4 && den == 3)
94 sh->aspect_ratio_information = 2;
95 else if (num == 16 && den == 9)
96 sh->aspect_ratio_information = 3;
97 else if (num == 221 && den == 100)
98 sh->aspect_ratio_information = 4;
100 sh->aspect_ratio_information = 1;
103 if (ctx->frame_rate.num && ctx->frame_rate.den) {
104 int code, ext_n, ext_d;
106 ff_mpeg12_find_best_frame_rate(ctx->frame_rate,
107 &code, &ext_n, &ext_d, 0);
109 sh->frame_rate_code = code;
110 se->frame_rate_extension_n = ext_n;
111 se->frame_rate_extension_d = ext_d;
114 if (ctx->video_format >= 0 ||
115 ctx->colour_primaries >= 0 ||
116 ctx->transfer_characteristics >= 0 ||
117 ctx->matrix_coefficients >= 0) {
120 ctx->sequence_display_extension.extension_start_code =
121 MPEG2_START_EXTENSION;
122 ctx->sequence_display_extension.extension_start_code_identifier =
123 MPEG2_EXTENSION_SEQUENCE_DISPLAY;
124 sde = &ctx->sequence_display_extension.data.sequence_display;
126 *sde = (MPEG2RawSequenceDisplayExtension) {
129 .colour_description = 0,
130 .colour_primaries = 2,
131 .transfer_characteristics = 2,
132 .matrix_coefficients = 2,
134 .display_horizontal_size =
135 se->horizontal_size_extension << 12 | sh->horizontal_size_value,
136 .display_vertical_size =
137 se->vertical_size_extension << 12 | sh->vertical_size_value,
140 err = ff_cbs_insert_unit_content(frag, se_pos + 1,
141 MPEG2_START_EXTENSION,
142 &ctx->sequence_display_extension,
145 av_log(bsf, AV_LOG_ERROR, "Failed to insert new sequence "
146 "display extension.\n");
151 if (ctx->video_format >= 0)
152 sde->video_format = ctx->video_format;
154 if (ctx->colour_primaries >= 0 ||
155 ctx->transfer_characteristics >= 0 ||
156 ctx->matrix_coefficients >= 0) {
157 sde->colour_description = 1;
159 if (ctx->colour_primaries >= 0)
160 sde->colour_primaries = ctx->colour_primaries;
162 if (ctx->transfer_characteristics >= 0)
163 sde->transfer_characteristics = ctx->transfer_characteristics;
165 if (ctx->matrix_coefficients >= 0)
166 sde->matrix_coefficients = ctx->matrix_coefficients;
173 static int mpeg2_metadata_filter(AVBSFContext *bsf, AVPacket *pkt)
175 MPEG2MetadataContext *ctx = bsf->priv_data;
176 CodedBitstreamFragment *frag = &ctx->fragment;
179 err = ff_bsf_get_packet_ref(bsf, pkt);
183 err = ff_cbs_read_packet(ctx->cbc, frag, pkt);
185 av_log(bsf, AV_LOG_ERROR, "Failed to read packet.\n");
189 err = mpeg2_metadata_update_fragment(bsf, frag);
191 av_log(bsf, AV_LOG_ERROR, "Failed to update frame fragment.\n");
195 err = ff_cbs_write_packet(ctx->cbc, pkt, frag);
197 av_log(bsf, AV_LOG_ERROR, "Failed to write packet.\n");
203 ff_cbs_fragment_reset(frag);
206 av_packet_unref(pkt);
211 static int mpeg2_metadata_init(AVBSFContext *bsf)
213 MPEG2MetadataContext *ctx = bsf->priv_data;
214 CodedBitstreamFragment *frag = &ctx->fragment;
217 #define VALIDITY_CHECK(name) do { \
219 av_log(bsf, AV_LOG_ERROR, "The value 0 for %s is " \
220 "forbidden.\n", #name); \
221 return AVERROR(EINVAL); \
224 VALIDITY_CHECK(colour_primaries);
225 VALIDITY_CHECK(transfer_characteristics);
226 VALIDITY_CHECK(matrix_coefficients);
227 #undef VALIDITY_CHECK
229 err = ff_cbs_init(&ctx->cbc, AV_CODEC_ID_MPEG2VIDEO, bsf);
233 if (bsf->par_in->extradata) {
234 err = ff_cbs_read_extradata(ctx->cbc, frag, bsf->par_in);
236 av_log(bsf, AV_LOG_ERROR, "Failed to read extradata.\n");
240 err = mpeg2_metadata_update_fragment(bsf, frag);
242 av_log(bsf, AV_LOG_ERROR, "Failed to update metadata fragment.\n");
246 err = ff_cbs_write_extradata(ctx->cbc, bsf->par_out, frag);
248 av_log(bsf, AV_LOG_ERROR, "Failed to write extradata.\n");
255 ff_cbs_fragment_reset(frag);
259 static void mpeg2_metadata_close(AVBSFContext *bsf)
261 MPEG2MetadataContext *ctx = bsf->priv_data;
263 ff_cbs_fragment_free(&ctx->fragment);
264 ff_cbs_close(&ctx->cbc);
267 #define OFFSET(x) offsetof(MPEG2MetadataContext, x)
268 #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_BSF_PARAM)
269 static const AVOption mpeg2_metadata_options[] = {
270 { "display_aspect_ratio", "Set display aspect ratio (table 6-3)",
271 OFFSET(display_aspect_ratio), AV_OPT_TYPE_RATIONAL,
272 { .dbl = 0.0 }, 0, 65535, FLAGS },
274 { "frame_rate", "Set frame rate",
275 OFFSET(frame_rate), AV_OPT_TYPE_RATIONAL,
276 { .dbl = 0.0 }, 0, UINT_MAX, FLAGS },
278 { "video_format", "Set video format (table 6-6)",
279 OFFSET(video_format), AV_OPT_TYPE_INT,
280 { .i64 = -1 }, -1, 7, FLAGS },
281 { "colour_primaries", "Set colour primaries (table 6-7)",
282 OFFSET(colour_primaries), AV_OPT_TYPE_INT,
283 { .i64 = -1 }, -1, 255, FLAGS },
284 { "transfer_characteristics", "Set transfer characteristics (table 6-8)",
285 OFFSET(transfer_characteristics), AV_OPT_TYPE_INT,
286 { .i64 = -1 }, -1, 255, FLAGS },
287 { "matrix_coefficients", "Set matrix coefficients (table 6-9)",
288 OFFSET(matrix_coefficients), AV_OPT_TYPE_INT,
289 { .i64 = -1 }, -1, 255, FLAGS },
294 static const AVClass mpeg2_metadata_class = {
295 .class_name = "mpeg2_metadata_bsf",
296 .item_name = av_default_item_name,
297 .option = mpeg2_metadata_options,
298 .version = LIBAVUTIL_VERSION_INT,
301 static const enum AVCodecID mpeg2_metadata_codec_ids[] = {
302 AV_CODEC_ID_MPEG2VIDEO, AV_CODEC_ID_NONE,
305 const AVBitStreamFilter ff_mpeg2_metadata_bsf = {
306 .name = "mpeg2_metadata",
307 .priv_data_size = sizeof(MPEG2MetadataContext),
308 .priv_class = &mpeg2_metadata_class,
309 .init = &mpeg2_metadata_init,
310 .close = &mpeg2_metadata_close,
311 .filter = &mpeg2_metadata_filter,
312 .codec_ids = mpeg2_metadata_codec_ids,