FFmpeg
mpeg2_metadata_bsf.c
Go to the documentation of this file.
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/avstring.h"
20 #include "libavutil/common.h"
21 #include "libavutil/opt.h"
22 
23 #include "bsf.h"
24 #include "cbs.h"
25 #include "cbs_bsf.h"
26 #include "cbs_mpeg2.h"
27 #include "mpeg12.h"
28 
29 typedef struct MPEG2MetadataContext {
31 
33 
35 
37 
42 
45 
46 
48  AVPacket *pkt,
50 {
55  int i, se_pos;
56 
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;
64  se = &ext->data.sequence;
65  se_pos = i;
66  } else if (ext->extension_start_code_identifier ==
68  sde = &ext->data.sequence_display;
69  }
70  }
71  }
72 
73  if (!sh || !se) {
74  // No sequence header and sequence extension: not an MPEG-2 video
75  // sequence.
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;
81  }
82  return 0;
83  }
84 
85  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  if (num == 4 && den == 3)
93  else if (num == 16 && den == 9)
95  else if (num == 221 && den == 100)
97  else
99  }
100 
101  if (ctx->frame_rate.num && ctx->frame_rate.den) {
102  int code, ext_n, ext_d;
103 
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  if (ctx->video_format >= 0 ||
113  ctx->colour_primaries >= 0 ||
114  ctx->transfer_characteristics >= 0 ||
115  ctx->matrix_coefficients >= 0) {
116  if (!sde) {
117  int err;
118  ctx->sequence_display_extension.extension_start_code =
120  ctx->sequence_display_extension.extension_start_code_identifier =
122  sde = &ctx->sequence_display_extension.data.sequence_display;
123 
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  se->horizontal_size_extension << 12 | sh->horizontal_size_value,
134  .display_vertical_size =
135  se->vertical_size_extension << 12 | sh->vertical_size_value,
136  };
137 
138  err = ff_cbs_insert_unit_content(frag, se_pos + 1,
140  &ctx->sequence_display_extension,
141  NULL);
142  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  if (ctx->video_format >= 0)
150  sde->video_format = ctx->video_format;
151 
152  if (ctx->colour_primaries >= 0 ||
153  ctx->transfer_characteristics >= 0 ||
154  ctx->matrix_coefficients >= 0) {
155  sde->colour_description = 1;
156 
157  if (ctx->colour_primaries >= 0)
158  sde->colour_primaries = ctx->colour_primaries;
159 
160  if (ctx->transfer_characteristics >= 0)
161  sde->transfer_characteristics = ctx->transfer_characteristics;
162 
163  if (ctx->matrix_coefficients >= 0)
164  sde->matrix_coefficients = ctx->matrix_coefficients;
165  }
166  }
167 
168  return 0;
169 }
170 
173  .fragment_name = "frame",
174  .unit_name = "start code",
175  .update_fragment = &mpeg2_metadata_update_fragment,
176 };
177 
179 {
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  VALIDITY_CHECK(colour_primaries);
191  VALIDITY_CHECK(matrix_coefficients);
192 #undef VALIDITY_CHECK
193 
195 }
196 
197 #define OFFSET(x) offsetof(MPEG2MetadataContext, x)
198 #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_BSF_PARAM)
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)",
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 
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[] = {
233 };
234 
236  .name = "mpeg2_metadata",
237  .priv_data_size = sizeof(MPEG2MetadataContext),
238  .priv_class = &mpeg2_metadata_class,
240  .close = &ff_cbs_bsf_generic_close,
243 };
MPEG2MetadataContext::display_aspect_ratio
AVRational display_aspect_ratio
Definition: mpeg2_metadata_bsf.c:34
OFFSET
#define OFFSET(x)
Definition: mpeg2_metadata_bsf.c:197
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:186
ff_cbs_bsf_generic_init
int ff_cbs_bsf_generic_init(AVBSFContext *bsf, const CBSBSFType *type)
Initialise generic CBS BSF setup.
Definition: cbs_bsf.c:110
opt.h
CBSBSFType::codec_id
enum AVCodecID codec_id
Definition: cbs_bsf.h:32
CodedBitstreamUnit::content
void * content
Pointer to the decomposed form of this unit.
Definition: cbs.h:106
AVBitStreamFilter::name
const char * name
Definition: bsf.h:91
MPEG2MetadataContext::transfer_characteristics
int transfer_characteristics
Definition: mpeg2_metadata_bsf.c:40
se
#define se(name, range_min, range_max)
Definition: cbs_h2645.c:275
MPEG2RawExtensionData::sequence_display
MPEG2RawSequenceDisplayExtension sequence_display
Definition: cbs_mpeg2.h:183
AVOption
AVOption.
Definition: opt.h:247
CBSBSFContext
Definition: cbs_bsf.h:53
CodedBitstreamUnit::type
CodedBitstreamUnitType type
Codec-specific type of this unit.
Definition: cbs.h:73
cbs.h
filter
filter_frame For filters that do not use the this method is called when a frame is pushed to the filter s input It can be called at any time except in a reentrant way If the input frame is enough to produce then the filter should push the output frames on the output link immediately As an exception to the previous rule if the input frame is enough to produce several output frames then the filter needs output only at least one per link The additional frames can be left buffered in the filter
Definition: filter_design.txt:228
MPEG2RawExtensionData
Definition: cbs_mpeg2.h:177
MPEG2MetadataContext::video_format
int video_format
Definition: mpeg2_metadata_bsf.c:38
AV_OPT_TYPE_RATIONAL
@ AV_OPT_TYPE_RATIONAL
Definition: opt.h:229
MPEG2RawSequenceHeader::frame_rate_code
uint8_t frame_rate_code
Definition: cbs_mpeg2.h:65
AVBSFContext
The bitstream filter state.
Definition: bsf.h:47
ff_mpeg12_find_best_frame_rate
void ff_mpeg12_find_best_frame_rate(AVRational frame_rate, int *code, int *ext_n, int *ext_d, int nonstandard)
Definition: mpeg12framerate.c:44
init
static int init
Definition: av_tx.c:47
bsf.h
cbs_bsf.h
mpeg2_metadata_init
static int mpeg2_metadata_init(AVBSFContext *bsf)
Definition: mpeg2_metadata_bsf.c:178
av_reduce
int av_reduce(int *dst_num, int *dst_den, int64_t num, int64_t den, int64_t max)
Reduce a fraction.
Definition: rational.c:35
MPEG2RawSequenceDisplayExtension::colour_description
uint8_t colour_description
Definition: cbs_mpeg2.h:100
CodedBitstreamFragment::units
CodedBitstreamUnit * units
Pointer to an array of units of length nb_units_allocated.
Definition: cbs.h:167
mpeg12.h
pkt
AVPacket * pkt
Definition: movenc.c:59
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
MPEG2RawSequenceExtension
Definition: cbs_mpeg2.h:84
CodedBitstreamFragment
Coded bitstream fragment structure, combining one or more units.
Definition: cbs.h:121
MPEG2RawSequenceDisplayExtension
Definition: cbs_mpeg2.h:97
ctx
AVFormatContext * ctx
Definition: movenc.c:48
MPEG2RawSequenceDisplayExtension::video_format
uint8_t video_format
Definition: cbs_mpeg2.h:98
MPEG2RawSequenceHeader::vertical_size_value
uint16_t vertical_size_value
Definition: cbs_mpeg2.h:63
MPEG2MetadataContext
Definition: mpeg2_metadata_bsf.c:29
FLAGS
#define FLAGS
Definition: mpeg2_metadata_bsf.c:198
mpeg2_metadata_update_fragment
static int mpeg2_metadata_update_fragment(AVBSFContext *bsf, AVPacket *pkt, CodedBitstreamFragment *frag)
Definition: mpeg2_metadata_bsf.c:47
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
MPEG2RawSequenceDisplayExtension::colour_primaries
uint8_t colour_primaries
Definition: cbs_mpeg2.h:101
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
MPEG2MetadataContext::matrix_coefficients
int matrix_coefficients
Definition: mpeg2_metadata_bsf.c:41
NULL
#define NULL
Definition: coverity.c:32
mpeg2_metadata_codec_ids
static enum AVCodecID mpeg2_metadata_codec_ids[]
Definition: mpeg2_metadata_bsf.c:231
ff_cbs_insert_unit_content
int ff_cbs_insert_unit_content(CodedBitstreamFragment *frag, int position, CodedBitstreamUnitType type, void *content, AVBufferRef *content_buf)
Insert a new unit into a fragment with the given content.
Definition: cbs.c:756
AVRational
Rational number (pair of numerator and denominator).
Definition: rational.h:58
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:235
mpeg2_metadata_type
static const CBSBSFType mpeg2_metadata_type
Definition: mpeg2_metadata_bsf.c:171
cbs_mpeg2.h
MPEG2MetadataContext::mpeg1_warned
int mpeg1_warned
Definition: mpeg2_metadata_bsf.c:43
MPEG2_EXTENSION_SEQUENCE
@ MPEG2_EXTENSION_SEQUENCE
Definition: cbs_mpeg2.h:45
MPEG2RawSequenceDisplayExtension::matrix_coefficients
uint8_t matrix_coefficients
Definition: cbs_mpeg2.h:103
MPEG2MetadataContext::colour_primaries
int colour_primaries
Definition: mpeg2_metadata_bsf.c:39
MPEG2RawSequenceHeader::horizontal_size_value
uint16_t horizontal_size_value
Definition: cbs_mpeg2.h:62
AVCodecID
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: codec_id.h:47
MPEG2RawExtensionData::sequence
MPEG2RawSequenceExtension sequence
Definition: cbs_mpeg2.h:182
VALIDITY_CHECK
#define VALIDITY_CHECK(name)
MPEG2RawExtensionData::extension_start_code_identifier
uint8_t extension_start_code_identifier
Definition: cbs_mpeg2.h:179
mpeg2_metadata_options
static const AVOption mpeg2_metadata_options[]
Definition: mpeg2_metadata_bsf.c:199
CBSBSFType
Definition: cbs_bsf.h:31
MPEG2RawExtensionData::data
union MPEG2RawExtensionData::@35 data
ff_cbs_bsf_generic_close
void ff_cbs_bsf_generic_close(AVBSFContext *bsf)
Close a generic CBS BSF instance.
Definition: cbs_bsf.c:150
transfer_characteristics
static const struct TransferCharacteristics transfer_characteristics[AVCOL_TRC_NB]
Definition: vf_colorspace.c:177
MPEG2RawSequenceHeader
Definition: cbs_mpeg2.h:59
AV_CODEC_ID_NONE
@ AV_CODEC_ID_NONE
Definition: codec_id.h:48
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:271
code
and forward the test the status of outputs and forward it to the corresponding return FFERROR_NOT_READY If the filters stores internally one or a few frame for some it can consider them to be part of the FIFO and delay acknowledging a status change accordingly Example code
Definition: filter_design.txt:178
common.h
MPEG2RawSequenceHeader::aspect_ratio_information
uint8_t aspect_ratio_information
Definition: cbs_mpeg2.h:64
MPEG2MetadataContext::frame_rate
AVRational frame_rate
Definition: mpeg2_metadata_bsf.c:36
MPEG2MetadataContext::common
CBSBSFContext common
Definition: mpeg2_metadata_bsf.c:30
MPEG2RawSequenceDisplayExtension::transfer_characteristics
uint8_t transfer_characteristics
Definition: cbs_mpeg2.h:102
AVBSFContext::priv_data
void * priv_data
Opaque filter-specific private data.
Definition: bsf.h:62
ff_cbs_bsf_generic_filter
int ff_cbs_bsf_generic_filter(AVBSFContext *bsf, AVPacket *pkt)
Filter operation for CBS BSF.
Definition: cbs_bsf.c:61
AVClass::class_name
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:71
ff_mpeg2_metadata_bsf
const AVBitStreamFilter ff_mpeg2_metadata_bsf
Definition: mpeg2_metadata_bsf.c:235
MPEG2MetadataContext::sequence_display_extension
MPEG2RawExtensionData sequence_display_extension
Definition: mpeg2_metadata_bsf.c:32
MPEG2_EXTENSION_SEQUENCE_DISPLAY
@ MPEG2_EXTENSION_SEQUENCE_DISPLAY
Definition: cbs_mpeg2.h:46
AVBitStreamFilter
Definition: bsf.h:90
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Definition: opt.h:224
codec_ids
static enum AVCodecID codec_ids[]
Definition: aac_adtstoasc_bsf.c:148
MPEG2_START_EXTENSION
@ MPEG2_START_EXTENSION
Definition: cbs_mpeg2.h:35
MPEG2_START_SEQUENCE_HEADER
@ MPEG2_START_SEQUENCE_HEADER
Definition: cbs_mpeg2.h:33
AVPacket
This structure stores compressed data.
Definition: packet.h:350
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
avstring.h
AV_CODEC_ID_MPEG2VIDEO
@ AV_CODEC_ID_MPEG2VIDEO
preferred ID for MPEG-1/2 video decoding
Definition: codec_id.h:52
mpeg2_metadata_class
static const AVClass mpeg2_metadata_class
Definition: mpeg2_metadata_bsf.c:224
CodedBitstreamFragment::nb_units
int nb_units
Number of units in this fragment.
Definition: cbs.h:152