FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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_mpeg2.h"
26 #include "mpeg12.h"
27 
28 typedef struct MPEG2MetadataContext {
29  const AVClass *class;
30 
33 
35 
37 
39 
44 
47 
48 
51 {
56  int i, se_pos, add_sde = 0;
57 
58  for (i = 0; i < frag->nb_units; i++) {
59  if (frag->units[i].type == MPEG2_START_SEQUENCE_HEADER) {
60  sh = frag->units[i].content;
61  } else if (frag->units[i].type == MPEG2_START_EXTENSION) {
62  MPEG2RawExtensionData *ext = frag->units[i].content;
65  se = &ext->data.sequence;
66  se_pos = i;
67  } else if (ext->extension_start_code_identifier ==
69  sde = &ext->data.sequence_display;
70  }
71  }
72  }
73 
74  if (!sh || !se) {
75  // No sequence header and sequence extension: not an MPEG-2 video
76  // sequence.
77  if (sh && !ctx->mpeg1_warned) {
78  av_log(bsf, AV_LOG_WARNING, "Stream contains a sequence "
79  "header but not a sequence extension: maybe it's "
80  "actually MPEG-1?\n");
81  ctx->mpeg1_warned = 1;
82  }
83  return 0;
84  }
85 
87  int num, den;
88 
89  av_reduce(&num, &den, ctx->display_aspect_ratio.num,
90  ctx->display_aspect_ratio.den, 65535);
91 
92  if (num == 4 && den == 3)
94  else if (num == 16 && den == 9)
96  else if (num == 221 && den == 100)
98  else
100  }
101 
102  if (ctx->frame_rate.num && ctx->frame_rate.den) {
103  int code, ext_n, ext_d;
104 
106  &code, &ext_n, &ext_d, 0);
107 
108  sh->frame_rate_code = code;
109  se->frame_rate_extension_n = ext_n;
110  se->frame_rate_extension_d = ext_d;
111  }
112 
113  if (ctx->video_format >= 0 ||
114  ctx->colour_primaries >= 0 ||
115  ctx->transfer_characteristics >= 0 ||
116  ctx->matrix_coefficients >= 0) {
117  if (!sde) {
118  add_sde = 1;
124 
126  .video_format = 5,
127 
128  .colour_description = 0,
129  .colour_primaries = 2,
130  .transfer_characteristics = 2,
131  .matrix_coefficients = 2,
132 
133  .display_horizontal_size =
135  .display_vertical_size =
137  };
138  }
139 
140  if (ctx->video_format >= 0)
141  sde->video_format = ctx->video_format;
142 
143  if (ctx->colour_primaries >= 0 ||
144  ctx->transfer_characteristics >= 0 ||
145  ctx->matrix_coefficients >= 0) {
146  sde->colour_description = 1;
147 
148  if (ctx->colour_primaries >= 0)
150  else if (add_sde)
151  sde->colour_primaries = 2;
152 
153  if (ctx->transfer_characteristics >= 0)
155  else if (add_sde)
156  sde->transfer_characteristics = 2;
157 
158  if (ctx->matrix_coefficients >= 0)
160  else if (add_sde)
161  sde->matrix_coefficients = 2;
162  }
163  }
164 
165  if (add_sde) {
166  int err;
167 
168  err = ff_cbs_insert_unit_content(ctx->cbc, frag, se_pos + 1,
171  if (err < 0) {
172  av_log(bsf, AV_LOG_ERROR, "Failed to insert new sequence "
173  "display extension.\n");
174  return err;
175  }
176  }
177 
178  return 0;
179 }
180 
182 {
184  AVPacket *in = NULL;
185  CodedBitstreamFragment *frag = &ctx->fragment;
186  int err;
187 
188  err = ff_bsf_get_packet(bsf, &in);
189  if (err < 0)
190  goto fail;
191 
192  err = ff_cbs_read_packet(ctx->cbc, frag, in);
193  if (err < 0) {
194  av_log(bsf, AV_LOG_ERROR, "Failed to read packet.\n");
195  goto fail;
196  }
197 
198  err = mpeg2_metadata_update_fragment(bsf, frag);
199  if (err < 0) {
200  av_log(bsf, AV_LOG_ERROR, "Failed to update frame fragment.\n");
201  goto fail;
202  }
203 
204  err = ff_cbs_write_packet(ctx->cbc, out, frag);
205  if (err < 0) {
206  av_log(bsf, AV_LOG_ERROR, "Failed to write packet.\n");
207  goto fail;
208  }
209 
210  err = av_packet_copy_props(out, in);
211  if (err < 0) {
212  av_packet_unref(out);
213  goto fail;
214  }
215 
216  err = 0;
217 fail:
218  ff_cbs_fragment_uninit(ctx->cbc, frag);
219 
220  av_packet_free(&in);
221 
222  return err;
223 }
224 
226 {
228  CodedBitstreamFragment *frag = &ctx->fragment;
229  int err;
230 
231  err = ff_cbs_init(&ctx->cbc, AV_CODEC_ID_MPEG2VIDEO, bsf);
232  if (err < 0)
233  return err;
234 
235  if (bsf->par_in->extradata) {
236  err = ff_cbs_read_extradata(ctx->cbc, frag, bsf->par_in);
237  if (err < 0) {
238  av_log(bsf, AV_LOG_ERROR, "Failed to read extradata.\n");
239  goto fail;
240  }
241 
242  err = mpeg2_metadata_update_fragment(bsf, frag);
243  if (err < 0) {
244  av_log(bsf, AV_LOG_ERROR, "Failed to update metadata fragment.\n");
245  goto fail;
246  }
247 
248  err = ff_cbs_write_extradata(ctx->cbc, bsf->par_out, frag);
249  if (err < 0) {
250  av_log(bsf, AV_LOG_ERROR, "Failed to write extradata.\n");
251  goto fail;
252  }
253  }
254 
255  err = 0;
256 fail:
257  ff_cbs_fragment_uninit(ctx->cbc, frag);
258  return err;
259 }
260 
262 {
264  ff_cbs_close(&ctx->cbc);
265 }
266 
267 #define OFFSET(x) offsetof(MPEG2MetadataContext, x)
269  { "display_aspect_ratio", "Set display aspect ratio (table 6-3)",
270  OFFSET(display_aspect_ratio), AV_OPT_TYPE_RATIONAL,
271  { .dbl = 0.0 }, 0, 65535 },
272 
273  { "frame_rate", "Set frame rate",
274  OFFSET(frame_rate), AV_OPT_TYPE_RATIONAL,
275  { .dbl = 0.0 }, 0, UINT_MAX },
276 
277  { "video_format", "Set video format (table 6-6)",
278  OFFSET(video_format), AV_OPT_TYPE_INT,
279  { .i64 = -1 }, -1, 7 },
280  { "colour_primaries", "Set colour primaries (table 6-7)",
281  OFFSET(colour_primaries), AV_OPT_TYPE_INT,
282  { .i64 = -1 }, -1, 255 },
283  { "transfer_characteristics", "Set transfer characteristics (table 6-8)",
285  { .i64 = -1 }, -1, 255 },
286  { "matrix_coefficients", "Set matrix coefficients (table 6-9)",
287  OFFSET(matrix_coefficients), AV_OPT_TYPE_INT,
288  { .i64 = -1 }, -1, 255 },
289 
290  { NULL }
291 };
292 
294  .class_name = "mpeg2_metadata_bsf",
295  .item_name = av_default_item_name,
296  .option = mpeg2_metadata_options,
297  .version = LIBAVUTIL_VERSION_INT,
298 };
299 
300 static const enum AVCodecID mpeg2_metadata_codec_ids[] = {
302 };
303 
305  .name = "mpeg2_metadata",
306  .priv_data_size = sizeof(MPEG2MetadataContext),
307  .priv_class = &mpeg2_metadata_class,
309  .close = &mpeg2_metadata_close,
312 };
#define NULL
Definition: coverity.c:32
int nb_units
Number of units in this fragment.
Definition: cbs.h:133
uint8_t extension_start_code_identifier
Definition: cbs_mpeg2.h:170
AVCodecParameters * par_out
Parameters of the output stream.
Definition: avcodec.h:5591
#define se(name, range_min, range_max)
Definition: cbs_h2645.c:348
AVOption.
Definition: opt.h:246
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
int ff_cbs_write_packet(CodedBitstreamContext *ctx, AVPacket *pkt, CodedBitstreamFragment *frag)
Write the bitstream of a fragment to a packet.
Definition: cbs.c:267
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
MPEG2RawSequenceExtension sequence
Definition: cbs_mpeg2.h:173
int ff_cbs_init(CodedBitstreamContext **ctx_ptr, enum AVCodecID codec_id, void *log_ctx)
Create and initialise a new context for the given codec.
Definition: cbs.c:42
CodedBitstreamUnitType type
Codec-specific type of this unit.
Definition: cbs.h:65
int num
Numerator.
Definition: rational.h:59
The bitstream filter state.
Definition: avcodec.h:5557
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:193
void * priv_data
Opaque filter-specific private data.
Definition: avcodec.h:5578
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:72
uint16_t vertical_size_value
Definition: cbs_mpeg2.h:61
static void filter(int16_t *output, ptrdiff_t out_stride, int16_t *low, ptrdiff_t low_stride, int16_t *high, ptrdiff_t high_stride, int len, int clip)
Definition: cfhd.c:97
void av_packet_free(AVPacket **pkt)
Free the packet, if the packet is reference counted, it will be unreferenced first.
Definition: avpacket.c:62
AVOptions.
int ff_cbs_read_packet(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, const AVPacket *pkt)
Read the data bitstream from a packet into a fragment, then split into units and decompose.
Definition: cbs.c:172
const char * name
Definition: avcodec.h:5607
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
void * content
Pointer to the decomposed form of this unit.
Definition: cbs.h:92
CodedBitstreamUnit * units
Pointer to an array of units of length nb_units.
Definition: cbs.h:139
int ff_cbs_insert_unit_content(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, int position, CodedBitstreamUnitType type, void *content)
Insert a new unit into a fragment with the given content.
Definition: cbs.c:422
#define av_log(a,...)
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: avcodec.h:215
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
void ff_cbs_fragment_uninit(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag)
Free all allocated memory in a fragment.
Definition: cbs.c:106
void ff_mpeg12_find_best_frame_rate(AVRational frame_rate, int *code, int *ext_n, int *ext_d, int nonstandard)
#define fail()
Definition: checkasm.h:112
static void mpeg2_metadata_close(AVBSFContext *bsf)
static const struct TransferCharacteristics transfer_characteristics[AVCOL_TRC_NB]
int av_packet_copy_props(AVPacket *dst, const AVPacket *src)
Copy only "properties" fields from src to dst.
Definition: avpacket.c:558
const AVBitStreamFilter ff_mpeg2_metadata_bsf
int ff_cbs_write_extradata(CodedBitstreamContext *ctx, AVCodecParameters *par, CodedBitstreamFragment *frag)
Write the bitstream of a fragment to the extradata in codec parameters.
Definition: cbs.c:242
MPEG2RawExtensionData sequence_display_extension
AVFormatContext * ctx
Definition: movenc.c:48
uint8_t horizontal_size_extension
Definition: cbs_mpeg2.h:85
preferred ID for MPEG-1/2 video decoding
Definition: avcodec.h:220
union MPEG2RawExtensionData::@62 data
MPEG2RawSequenceDisplayExtension sequence_display
Definition: cbs_mpeg2.h:174
Coded bitstream fragment structure, combining one or more units.
Definition: cbs.h:108
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:590
static const AVClass mpeg2_metadata_class
uint8_t frame_rate_extension_n
Definition: cbs_mpeg2.h:90
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31))))#defineSET_CONV_FUNC_GROUP(ofmt, ifmt) staticvoidset_generic_function(AudioConvert *ac){}voidff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, intsample_rate, intapply_map){AudioConvert *ac;intin_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) returnNULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method!=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt)>2){ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc){av_free(ac);returnNULL;}returnac;}in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}elseif(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;elseac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);returnac;}intff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){intuse_generic=1;intlen=in->nb_samples;intp;if(ac->dc){av_log(ac->avr, AV_LOG_TRACE,"%dsamples-audio_convert:%sto%s(dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));returnff_convert_dither(ac-> in
uint8_t frame_rate_extension_d
Definition: cbs_mpeg2.h:91
static enum AVCodecID mpeg2_metadata_codec_ids[]
Describe the class of an AVClass context structure.
Definition: log.h:67
Context structure for coded bitstream operations.
Definition: cbs.h:145
Rational number (pair of numerator and denominator).
Definition: rational.h:58
uint8_t extension_start_code
Definition: cbs_mpeg2.h:169
uint16_t horizontal_size_value
Definition: cbs_mpeg2.h:60
void ff_cbs_close(CodedBitstreamContext **ctx_ptr)
Close a context and free all internal state.
Definition: cbs.c:81
uint8_t aspect_ratio_information
Definition: cbs_mpeg2.h:62
int ff_cbs_read_extradata(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, const AVCodecParameters *par)
Read the extradata bitstream found in codec parameters into a fragment, then split into units and dec...
Definition: cbs.c:151
CodedBitstreamContext * cbc
static int mpeg2_metadata_filter(AVBSFContext *bsf, AVPacket *out)
CodedBitstreamFragment fragment
common internal and external API header
static enum AVCodecID codec_ids[]
int ff_bsf_get_packet(AVBSFContext *ctx, AVPacket **pkt)
Called by the bitstream filters to get the next packet for filtering.
Definition: bsf.c:201
int den
Denominator.
Definition: rational.h:60
static const AVOption mpeg2_metadata_options[]
uint8_t vertical_size_extension
Definition: cbs_mpeg2.h:86
static int mpeg2_metadata_update_fragment(AVBSFContext *bsf, CodedBitstreamFragment *frag)
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition: avcodec.h:3826
FILE * out
Definition: movenc.c:54
#define OFFSET(x)
static int mpeg2_metadata_init(AVBSFContext *bsf)
This structure stores compressed data.
Definition: avcodec.h:1377
AVCodecParameters * par_in
Parameters of the input stream.
Definition: avcodec.h:5585