../../../ffmpeg/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
50 / 70
71.4%
Branches:
32 / 70
45.7%
Functions:
2 / 2
100.0%

libavcodec/bsf/mpeg2_metadata.c
Line Branch Exec Source
1 /*
2 * This file is part of FFmpeg.
3 *
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.
8 *
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.
13 *
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
17 */
18
19 #include "libavutil/common.h"
20 #include "libavutil/opt.h"
21
22 #include "libavcodec/bsf.h"
23 #include "libavcodec/bsf_internal.h"
24 #include "libavcodec/cbs.h"
25 #include "libavcodec/cbs_bsf.h"
26 #include "libavcodec/cbs_mpeg2.h"
27 #include "libavcodec/mpeg12.h"
28
29 typedef struct MPEG2MetadataContext {
30 CBSBSFContext common;
31
32 MPEG2RawExtensionData sequence_display_extension;
33
34 AVRational display_aspect_ratio;
35
36 AVRational frame_rate;
37
38 int video_format;
39 int colour_primaries;
40 int transfer_characteristics;
41 int matrix_coefficients;
42
43 int mpeg1_warned;
44 } MPEG2MetadataContext;
45
46
47 22 static int mpeg2_metadata_update_fragment(AVBSFContext *bsf,
48 AVPacket *pkt,
49 CodedBitstreamFragment *frag)
50 {
51 22 MPEG2MetadataContext *ctx = bsf->priv_data;
52 22 MPEG2RawSequenceHeader *sh = NULL;
53 22 MPEG2RawSequenceExtension *se = NULL;
54 22 MPEG2RawSequenceDisplayExtension *sde = NULL;
55 int i, se_pos;
56
57
2/2
✓ Branch 11 → 3 taken 12909 times.
✓ Branch 11 → 12 taken 22 times.
12931 for (i = 0; i < frag->nb_units; i++) {
58
2/2
✓ Branch 3 → 4 taken 8 times.
✓ Branch 3 → 5 taken 12901 times.
12909 if (frag->units[i].type == MPEG2_START_SEQUENCE_HEADER) {
59 8 sh = frag->units[i].content;
60
2/2
✓ Branch 5 → 6 taken 62 times.
✓ Branch 5 → 10 taken 12839 times.
12901 } else if (frag->units[i].type == MPEG2_START_EXTENSION) {
61 62 MPEG2RawExtensionData *ext = frag->units[i].content;
62
2/2
✓ Branch 6 → 7 taken 8 times.
✓ Branch 6 → 8 taken 54 times.
62 if (ext->extension_start_code_identifier ==
63 MPEG2_EXTENSION_SEQUENCE) {
64 8 se = &ext->data.sequence;
65 8 se_pos = i;
66
2/2
✓ Branch 8 → 9 taken 4 times.
✓ Branch 8 → 10 taken 50 times.
54 } else if (ext->extension_start_code_identifier ==
67 MPEG2_EXTENSION_SEQUENCE_DISPLAY) {
68 4 sde = &ext->data.sequence_display;
69 }
70 }
71 }
72
73
3/4
✓ Branch 12 → 13 taken 8 times.
✓ Branch 12 → 14 taken 14 times.
✗ Branch 13 → 14 not taken.
✓ Branch 13 → 19 taken 8 times.
22 if (!sh || !se) {
74 // No sequence header and sequence extension: not an MPEG-2 video
75 // sequence.
76
1/4
✗ Branch 14 → 15 not taken.
✓ Branch 14 → 18 taken 14 times.
✗ Branch 15 → 16 not taken.
✗ Branch 15 → 18 not taken.
14 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;
81 }
82 14 return 0;
83 }
84
85
1/4
✗ Branch 19 → 20 not taken.
✓ Branch 19 → 33 taken 8 times.
✗ Branch 20 → 21 not taken.
✗ Branch 20 → 33 not taken.
8 if (ctx->display_aspect_ratio.num && ctx->display_aspect_ratio.den) {
86 int num, den;
87
88 ✗ av_reduce(&num, &den, ctx->display_aspect_ratio.num,
89 ✗ ctx->display_aspect_ratio.den, 65535);
90
91
0/4
✗ Branch 22 → 23 not taken.
✗ Branch 22 → 25 not taken.
✗ Branch 23 → 24 not taken.
✗ Branch 23 → 25 not taken.
✗ if (num == 4 && den == 3)
92 ✗ sh->aspect_ratio_information = 2;
93
0/4
✗ Branch 25 → 26 not taken.
✗ Branch 25 → 28 not taken.
✗ Branch 26 → 27 not taken.
✗ Branch 26 → 28 not taken.
✗ else if (num == 16 && den == 9)
94 ✗ sh->aspect_ratio_information = 3;
95
0/4
✗ Branch 28 → 29 not taken.
✗ Branch 28 → 31 not taken.
✗ Branch 29 → 30 not taken.
✗ Branch 29 → 31 not taken.
✗ else if (num == 221 && den == 100)
96 ✗ sh->aspect_ratio_information = 4;
97 else
98 ✗ sh->aspect_ratio_information = 1;
99 }
100
101
1/4
✗ Branch 33 → 34 not taken.
✓ Branch 33 → 37 taken 8 times.
✗ Branch 34 → 35 not taken.
✗ Branch 34 → 37 not taken.
8 if (ctx->frame_rate.num && ctx->frame_rate.den) {
102 int code, ext_n, ext_d;
103
104 ✗ ff_mpeg12_find_best_frame_rate(ctx->frame_rate,
105 &code, &ext_n, &ext_d, 0);
106
107 ✗ sh->frame_rate_code = code;
108 ✗ se->frame_rate_extension_n = ext_n;
109 ✗ se->frame_rate_extension_d = ext_d;
110 }
111
112
1/2
✓ Branch 37 → 38 taken 8 times.
✗ Branch 37 → 41 not taken.
8 if (ctx->video_format >= 0 ||
113
2/2
✓ Branch 38 → 39 taken 6 times.
✓ Branch 38 → 41 taken 2 times.
8 ctx->colour_primaries >= 0 ||
114
1/2
✓ Branch 39 → 40 taken 6 times.
✗ Branch 39 → 41 not taken.
6 ctx->transfer_characteristics >= 0 ||
115
1/2
✗ Branch 40 → 41 not taken.
✓ Branch 40 → 58 taken 6 times.
6 ctx->matrix_coefficients >= 0) {
116
2/2
✓ Branch 41 → 42 taken 1 time.
✓ Branch 41 → 47 taken 1 time.
2 if (!sde) {
117 int err;
118 1 ctx->sequence_display_extension.extension_start_code =
119 MPEG2_START_EXTENSION;
120 1 ctx->sequence_display_extension.extension_start_code_identifier =
121 MPEG2_EXTENSION_SEQUENCE_DISPLAY;
122 1 sde = &ctx->sequence_display_extension.data.sequence_display;
123
124 1 *sde = (MPEG2RawSequenceDisplayExtension) {
125 .video_format = 5,
126
127 .colour_description = 0,
128 .colour_primaries = 2,
129 .transfer_characteristics = 2,
130 .matrix_coefficients = 2,
131
132 .display_horizontal_size =
133 1 se->horizontal_size_extension << 12 | sh->horizontal_size_value,
134 .display_vertical_size =
135 1 se->vertical_size_extension << 12 | sh->vertical_size_value,
136 };
137
138 1 err = ff_cbs_insert_unit_content(frag, se_pos + 1,
139 MPEG2_START_EXTENSION,
140 1 &ctx->sequence_display_extension,
141 NULL);
142
1/2
✗ Branch 43 → 44 not taken.
✓ Branch 43 → 46 taken 1 time.
1 if (err < 0) {
143 ✗ av_log(bsf, AV_LOG_ERROR, "Failed to insert new sequence "
144 "display extension.\n");
145 ✗ return err;
146 }
147 }
148
149
1/2
✗ Branch 47 → 48 not taken.
✓ Branch 47 → 49 taken 2 times.
2 if (ctx->video_format >= 0)
150 ✗ sde->video_format = ctx->video_format;
151
152
1/2
✗ Branch 49 → 50 not taken.
✓ Branch 49 → 52 taken 2 times.
2 if (ctx->colour_primaries >= 0 ||
153
0/2
✗ Branch 50 → 51 not taken.
✗ Branch 50 → 52 not taken.
✗ ctx->transfer_characteristics >= 0 ||
154
0/2
✗ Branch 51 → 52 not taken.
✗ Branch 51 → 58 not taken.
✗ ctx->matrix_coefficients >= 0) {
155 2 sde->colour_description = 1;
156
157
1/2
✓ Branch 52 → 53 taken 2 times.
✗ Branch 52 → 54 not taken.
2 if (ctx->colour_primaries >= 0)
158 2 sde->colour_primaries = ctx->colour_primaries;
159
160
1/2
✓ Branch 54 → 55 taken 2 times.
✗ Branch 54 → 56 not taken.
2 if (ctx->transfer_characteristics >= 0)
161 2 sde->transfer_characteristics = ctx->transfer_characteristics;
162
163
1/2
✓ Branch 56 → 57 taken 2 times.
✗ Branch 56 → 58 not taken.
2 if (ctx->matrix_coefficients >= 0)
164 2 sde->matrix_coefficients = ctx->matrix_coefficients;
165 }
166 }
167
168 8 return 0;
169 }
170
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,
176 };
177
178 4 static int mpeg2_metadata_init(AVBSFContext *bsf)
179 {
180 4 MPEG2MetadataContext *ctx = bsf->priv_data;
181
182 #define VALIDITY_CHECK(name) do { \
183 if (!ctx->name) { \
184 av_log(bsf, AV_LOG_ERROR, "The value 0 for %s is " \
185 "forbidden.\n", #name); \
186 return AVERROR(EINVAL); \
187 } \
188 } while (0)
189
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 5 taken 4 times.
4 VALIDITY_CHECK(colour_primaries);
190
1/2
✗ Branch 5 → 6 not taken.
✓ Branch 5 → 8 taken 4 times.
4 VALIDITY_CHECK(transfer_characteristics);
191
1/2
✗ Branch 8 → 9 not taken.
✓ Branch 8 → 11 taken 4 times.
4 VALIDITY_CHECK(matrix_coefficients);
192 #undef VALIDITY_CHECK
193
194 4 return ff_cbs_bsf_generic_init(bsf, &mpeg2_metadata_type);
195 }
196
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 },
203
204 { "frame_rate", "Set frame rate",
205 OFFSET(frame_rate), AV_OPT_TYPE_RATIONAL,
206 { .dbl = 0.0 }, 0, UINT_MAX, FLAGS },
207
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 },
220
221 { NULL }
222 };
223
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,
229 };
230
231 static const enum AVCodecID mpeg2_metadata_codec_ids[] = {
232 AV_CODEC_ID_MPEG2VIDEO, AV_CODEC_ID_NONE,
233 };
234
235 const FFBitStreamFilter ff_mpeg2_metadata_bsf = {
236 .p.name = "mpeg2_metadata",
237 .p.codec_ids = mpeg2_metadata_codec_ids,
238 .p.priv_class = &mpeg2_metadata_class,
239 .priv_data_size = sizeof(MPEG2MetadataContext),
240 .init = &mpeg2_metadata_init,
241 .close = &ff_cbs_bsf_generic_close,
242 .filter = &ff_cbs_bsf_generic_filter,
243 };
244