FFmpeg
mxfenc.c
Go to the documentation of this file.
1 /*
2  * MXF muxer
3  * Copyright (c) 2008 GUCAS, Zhentan Feng <spyfeng at gmail dot com>
4  * Copyright (c) 2008 Baptiste Coudurier <baptiste dot coudurier at gmail dot com>
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
23 /*
24  * signal_standard, color_siting, store_user_comments, sample rate and klv_fill_key version
25  * fixes sponsored by NOA GmbH
26  */
27 
28 /*
29  * References
30  * SMPTE 336M KLV Data Encoding Protocol Using Key-Length-Value
31  * SMPTE 377M MXF File Format Specifications
32  * SMPTE 379M MXF Generic Container
33  * SMPTE 381M Mapping MPEG Streams into the MXF Generic Container
34  * SMPTE 422M Mapping JPEG 2000 Codestreams into the MXF Generic Container
35  * SMPTE ST2019-4 (2009 or later) Mapping VC-3 Coding Units into the MXF Generic Container
36  * SMPTE RP210: SMPTE Metadata Dictionary
37  * SMPTE RP224: Registry of SMPTE Universal Labels
38  */
39 
40 #include <inttypes.h>
41 #include <time.h>
42 
43 #include "libavutil/opt.h"
44 #include "libavutil/random_seed.h"
45 #include "libavutil/timecode.h"
46 #include "libavutil/avassert.h"
48 #include "libavutil/pixdesc.h"
50 #include "libavcodec/defs.h"
51 #include "libavcodec/golomb.h"
52 #include "libavcodec/h264.h"
54 #include "libavcodec/rangecoder.h"
55 #include "libavcodec/startcode.h"
56 #include "avformat.h"
57 #include "avio_internal.h"
58 #include "internal.h"
59 #include "avc.h"
60 #include "mux.h"
61 #include "mxf.h"
62 #include "config.h"
63 #include "version.h"
64 
65 extern const FFOutputFormat ff_mxf_d10_muxer;
67 
68 #define IS_D10(s) ((s)->oformat == &ff_mxf_d10_muxer.p)
69 #define IS_OPATOM(s) ((s)->oformat == &ff_mxf_opatom_muxer.p)
70 
71 #define EDIT_UNITS_PER_BODY 250
72 #define KAG_SIZE 512
73 
74 typedef struct MXFIndexEntry {
75  uint64_t offset;
76  unsigned slice_offset; ///< offset of audio slice
77  uint16_t temporal_ref;
78  uint8_t flags;
80 
81 typedef struct MXFStreamContext {
82  int64_t pkt_cnt; ///< pkt counter for muxed packets
84  int index; ///< index in mxf_essence_container_uls table
85  const UID *codec_ul;
86  const UID *container_ul;
87  int order; ///< interleaving order if dts are equal
88  int interlaced; ///< whether picture is interlaced
89  int field_dominance; ///< tff=1, bff=2
96  AVRational aspect_ratio; ///< display aspect ratio
97  int closed_gop; ///< gop is closed, used in mpeg-2 frame parsing
100  int frame_size; ///< frame size in bytes
101  int seq_closed_gop; ///< all gops in sequence are closed, used in mpeg-2 descriptor
102  int max_gop; ///< maximum gop size, used by mpeg-2 descriptor
103  int b_picture_count; ///< maximum number of consecutive b pictures, used in mpeg-2 descriptor
104  int low_delay; ///< low delay, used in mpeg-2 descriptor
106  int micro_version; ///< format micro_version, used in ffv1 descriptor
108 
109 typedef struct MXFContainerEssenceEntry {
115 
116 typedef struct MXFPackage {
117  char *name;
119  int instance;
120  struct MXFPackage *ref;
121 } MXFPackage;
122 
123 enum ULIndex {
136 };
137 
138 static const struct {
139  enum AVCodecID id;
141 } mxf_essence_mappings[] = {
151  { AV_CODEC_ID_NONE }
152 };
153 
154 static void mxf_write_wav_desc(AVFormatContext *s, AVStream *st);
155 static void mxf_write_aes3_desc(AVFormatContext *s, AVStream *st);
157 static void mxf_write_h264_desc(AVFormatContext *s, AVStream *st);
158 static void mxf_write_ffv1_desc(AVFormatContext *s, AVStream *st);
159 static void mxf_write_cdci_desc(AVFormatContext *s, AVStream *st);
162 
164  { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x02,0x0D,0x01,0x03,0x01,0x02,0x04,0x60,0x01 },
165  { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x15,0x01,0x05,0x00 },
166  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x00,0x00,0x00 },
168  { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x06,0x03,0x00 },
169  { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x16,0x01,0x03,0x00 },
170  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 },
172  { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x06,0x01,0x00 },
173  { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x16,0x01,0x01,0x00 },
174  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 },
176  // D-10 Video
177  { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x01,0x01 },
178  { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x05,0x01,0x01,0x00 },
179  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x01,0x02,0x01,0x01 },
181  // D-10 Audio
182  { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x01,0x01 },
183  { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x06,0x01,0x10,0x00 },
184  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 },
186  // DV
187  { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x02,0x7F,0x01 },
188  { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x18,0x01,0x01,0x00 },
189  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x02,0x00,0x00,0x00 },
191  // DNxHD
192  { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0A,0x0D,0x01,0x03,0x01,0x02,0x11,0x01,0x00 },
193  { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x15,0x01,0x0C,0x00 },
194  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0A,0x04,0x01,0x02,0x02,0x71,0x01,0x00,0x00 },
196  // JPEG2000
197  { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x07,0x0d,0x01,0x03,0x01,0x02,0x0c,0x01,0x00 },
198  { 0x06,0x0e,0x2b,0x34,0x01,0x02,0x01,0x01,0x0d,0x01,0x03,0x01,0x15,0x01,0x08,0x00 },
199  { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x07,0x04,0x01,0x02,0x02,0x03,0x01,0x01,0x00 },
201  // H.264
202  { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x0D,0x01,0x03,0x01,0x02,0x10,0x60,0x01 },
203  { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x15,0x01,0x05,0x00 },
204  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x00,0x00,0x00 },
206  // S436M ANC
207  { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x0D,0x01,0x03,0x01,0x02,0x0e,0x00,0x00 },
208  { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x17,0x01,0x02,0x00 },
209  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x01,0x5C,0x00 },
211  // ProRes
212  { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0d,0x0d,0x01,0x03,0x01,0x02,0x1c,0x01,0x00 },
213  { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0d,0x01,0x03,0x01,0x15,0x01,0x17,0x00 },
214  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0d,0x04,0x01,0x02,0x02,0x03,0x06,0x03,0x00 },
216  // FFV1
217  { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0d,0x0d,0x01,0x03,0x01,0x02,0x23,0x01,0x00 },
218  { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0d,0x01,0x03,0x01,0x15,0x01,0x1d,0x00 },
219  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0d,0x04,0x01,0x02,0x02,0x03,0x09,0x00,0x00 },
221  { { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 },
222  { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 },
223  { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 },
224  NULL },
225 };
226 
227 static const UID mxf_d10_codec_uls[] = {
228  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x01,0x02,0x01,0x01 }, // D-10 625/50 PAL 50mb/s
229  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x01,0x02,0x01,0x02 }, // D-10 525/50 NTSC 50mb/s
230  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x01,0x02,0x01,0x03 }, // D-10 625/50 PAL 40mb/s
231  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x01,0x02,0x01,0x04 }, // D-10 525/50 NTSC 40mb/s
232  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x01,0x02,0x01,0x05 }, // D-10 625/50 PAL 30mb/s
233  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x01,0x02,0x01,0x06 }, // D-10 525/50 NTSC 30mb/s
234 };
235 
236 static const UID mxf_d10_container_uls[] = {
237  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x01,0x01 }, // D-10 625/50 PAL 50mb/s
238  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x02,0x01 }, // D-10 525/50 NTSC 50mb/s
239  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x03,0x01 }, // D-10 625/50 PAL 40mb/s
240  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x04,0x01 }, // D-10 525/50 NTSC 40mb/s
241  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x05,0x01 }, // D-10 625/50 PAL 30mb/s
242  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x06,0x01 }, // D-10 525/50 NTSC 30mb/s
243 };
244 
245 
246 static const UID mxf_ffv1_codec_uls[] = {
247  { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x0D,0x04,0x01,0x02,0x02,0x03,0x09,0x01,0x00 }, // FFV1 version 0
248  { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x0D,0x04,0x01,0x02,0x02,0x03,0x09,0x02,0x00 }, // FFV1 version 1
249  { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x0D,0x04,0x01,0x02,0x02,0x03,0x09,0x03,0x00 }, // FFV1 version 2 (was only experimental)
250  { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x0D,0x04,0x01,0x02,0x02,0x03,0x09,0x04,0x00 }, // FFV1 version 3
251  { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x0D,0x04,0x01,0x02,0x02,0x03,0x09,0x05,0x00 }, // FFV1 version 4
252 };
253 
254 static const uint8_t product_uid[] = { 0xAD,0xAB,0x44,0x24,0x2f,0x25,0x4d,0xc7,0x92,0xff,0x29,0xbd,0x00,0x0c,0x00,0x02};
255 static const uint8_t uuid_base[] = { 0xAD,0xAB,0x44,0x24,0x2f,0x25,0x4d,0xc7,0x92,0xff };
256 static const uint8_t umid_ul[] = { 0x06,0x0A,0x2B,0x34,0x01,0x01,0x01,0x05,0x01,0x01,0x0D,0x00,0x13 };
257 
258 /**
259  * complete key for operation pattern, partitions, and primer pack
260  */
261 static const uint8_t op1a_ul[] = { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x02,0x01,0x01,0x01,0x09,0x00 };
262 static const uint8_t opatom_ul[] = { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x02,0x0D,0x01,0x02,0x01,0x10,0x03,0x00,0x00 };
263 static const uint8_t footer_partition_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0D,0x01,0x02,0x01,0x01,0x04,0x04,0x00 }; // ClosedComplete
264 static const uint8_t primer_pack_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0D,0x01,0x02,0x01,0x01,0x05,0x01,0x00 };
265 static const uint8_t index_table_segment_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x10,0x01,0x00 };
266 static const uint8_t header_open_partition_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0D,0x01,0x02,0x01,0x01,0x02,0x01,0x00 }; // OpenIncomplete
267 static const uint8_t header_closed_partition_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0D,0x01,0x02,0x01,0x01,0x02,0x04,0x00 }; // ClosedComplete
268 static const uint8_t klv_fill_key[] = { 0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x03,0x01,0x02,0x10,0x01,0x00,0x00,0x00 };
269 static const uint8_t body_partition_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0D,0x01,0x02,0x01,0x01,0x03,0x04,0x00 }; // ClosedComplete
270 
271 /**
272  * partial key for header metadata
273  */
274 static const uint8_t header_metadata_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0D,0x01,0x01,0x01,0x01 };
275 static const uint8_t multiple_desc_ul[] = { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x0D,0x01,0x03,0x01,0x02,0x7F,0x01,0x00 };
276 
277 /**
278  * SMPTE RP210 http://www.smpte-ra.org/mdd/index.html
279  * https://smpte-ra.org/sites/default/files/Labels.xml
280  */
282  // preface set
283  { 0x3C0A, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x01,0x01,0x15,0x02,0x00,0x00,0x00,0x00}}, /* Instance UID */
284  { 0x3B02, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x10,0x02,0x04,0x00,0x00}}, /* Last Modified Date */
285  { 0x3B05, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x03,0x01,0x02,0x01,0x05,0x00,0x00,0x00}}, /* Version */
286  { 0x3B07, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x03,0x01,0x02,0x01,0x04,0x00,0x00,0x00}}, /* Object Model Version */
287  { 0x3B06, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x06,0x04,0x00,0x00}}, /* Identifications reference */
288  { 0x3B03, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x02,0x01,0x00,0x00}}, /* Content Storage reference */
289  { 0x3B09, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x01,0x02,0x02,0x03,0x00,0x00,0x00,0x00}}, /* Operational Pattern UL */
290  { 0x3B0A, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x01,0x02,0x02,0x10,0x02,0x01,0x00,0x00}}, /* Essence Containers UL batch */
291  { 0x3B0B, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x01,0x02,0x02,0x10,0x02,0x02,0x00,0x00}}, /* DM Schemes UL batch */
292  // Identification
293  { 0x3C09, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x20,0x07,0x01,0x01,0x00,0x00,0x00}}, /* This Generation UID */
294  { 0x3C01, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x20,0x07,0x01,0x02,0x01,0x00,0x00}}, /* Company Name */
295  { 0x3C02, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x20,0x07,0x01,0x03,0x01,0x00,0x00}}, /* Product Name */
296  { 0x3C03, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x20,0x07,0x01,0x04,0x00,0x00,0x00}}, /* Product Version */
297  { 0x3C04, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x20,0x07,0x01,0x05,0x01,0x00,0x00}}, /* Version String */
298  { 0x3C05, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x20,0x07,0x01,0x07,0x00,0x00,0x00}}, /* Product ID */
299  { 0x3C06, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x10,0x02,0x03,0x00,0x00}}, /* Modification Date */
300  { 0x3C07, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x20,0x07,0x01,0x0A,0x00,0x00,0x00}}, /* Toolkit Version */
301  { 0x3C08, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x20,0x07,0x01,0x06,0x01,0x00,0x00}}, /* Platform */
302  // Content Storage
303  { 0x1901, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x05,0x01,0x00,0x00}}, /* Package strong reference batch */
304  { 0x1902, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x05,0x02,0x00,0x00}}, /* Package strong reference batch */
305  // Essence Container Data
306  { 0x2701, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x06,0x01,0x00,0x00,0x00}}, /* Linked Package UID */
307  { 0x3F07, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x01,0x03,0x04,0x04,0x00,0x00,0x00,0x00}}, /* BodySID */
308  // Package
309  { 0x4401, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x01,0x01,0x15,0x10,0x00,0x00,0x00,0x00}}, /* Package UID */
310  { 0x4405, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x10,0x01,0x03,0x00,0x00}}, /* Package Creation Date */
311  { 0x4404, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x10,0x02,0x05,0x00,0x00}}, /* Package Modified Date */
312  { 0x4402, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x01,0x03,0x03,0x02,0x01,0x00,0x00,0x00}}, /* Package Name */
313  { 0x4403, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x06,0x05,0x00,0x00}}, /* Tracks Strong reference array */
314  { 0x4701, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x02,0x03,0x00,0x00}}, /* Descriptor */
315  // Track
316  { 0x4801, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x01,0x07,0x01,0x01,0x00,0x00,0x00,0x00}}, /* Track ID */
317  { 0x4804, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x01,0x04,0x01,0x03,0x00,0x00,0x00,0x00}}, /* Track Number */
318  { 0x4B01, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x30,0x04,0x05,0x00,0x00,0x00,0x00}}, /* Edit Rate */
319  { 0x4B02, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x03,0x01,0x03,0x00,0x00}}, /* Origin */
320  { 0x4803, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x02,0x04,0x00,0x00}}, /* Sequence reference */
321  // Sequence
322  { 0x0201, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x07,0x01,0x00,0x00,0x00,0x00,0x00}}, /* Data Definition UL */
323  { 0x0202, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x02,0x01,0x01,0x03,0x00,0x00}}, /* Duration */
324  { 0x1001, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x06,0x09,0x00,0x00}}, /* Structural Components reference array */
325  // Source Clip
326  { 0x1201, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x03,0x01,0x04,0x00,0x00}}, /* Start position */
327  { 0x1101, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x03,0x01,0x00,0x00,0x00}}, /* SourcePackageID */
328  { 0x1102, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x03,0x02,0x00,0x00,0x00}}, /* SourceTrackID */
329  // Timecode Component
330  { 0x1501, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x03,0x01,0x05,0x00,0x00}}, /* Start Time Code */
331  { 0x1502, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x04,0x01,0x01,0x02,0x06,0x00,0x00}}, /* Rounded Time Code Base */
332  { 0x1503, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x04,0x01,0x01,0x05,0x00,0x00,0x00}}, /* Drop Frame */
333  // File Descriptor
334  { 0x3F01, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x06,0x01,0x01,0x04,0x06,0x0B,0x00,0x00}}, /* Sub Descriptors reference array */
335  { 0x3006, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x06,0x01,0x01,0x03,0x05,0x00,0x00,0x00}}, /* Linked Track ID */
336  { 0x3001, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x06,0x01,0x01,0x00,0x00,0x00,0x00}}, /* SampleRate */
337  { 0x3002, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x06,0x01,0x02,0x00,0x00,0x00,0x00}}, /* ContainerDuration */
338  { 0x3004, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x01,0x02,0x00,0x00}}, /* Essence Container */
339  // Generic Picture Essence Descriptor
340  { 0x320C, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x03,0x01,0x04,0x00,0x00,0x00}}, /* Frame Layout */
341  { 0x320D, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x01,0x03,0x02,0x05,0x00,0x00,0x00}}, /* Video Line Map */
342  { 0x3203, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x02,0x02,0x00,0x00,0x00}}, /* Stored Width */
343  { 0x3202, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x02,0x01,0x00,0x00,0x00}}, /* Stored Height */
344  { 0x3216, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x01,0x03,0x02,0x08,0x00,0x00,0x00}}, /* Stored F2 Offset */
345  { 0x3205, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x01,0x08,0x00,0x00,0x00}}, /* Sampled Width */
346  { 0x3204, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x01,0x07,0x00,0x00,0x00}}, /* Sampled Height */
347  { 0x3206, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x01,0x09,0x00,0x00,0x00}}, /* Sampled X Offset */
348  { 0x3207, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x01,0x0A,0x00,0x00,0x00}}, /* Sampled Y Offset */
349  { 0x3209, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x01,0x0C,0x00,0x00,0x00}}, /* Display Width */
350  { 0x3208, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x01,0x0B,0x00,0x00,0x00}}, /* Display Height */
351  { 0x320A, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x01,0x0D,0x00,0x00,0x00}}, /* Display X offset */
352  { 0x320B, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x01,0x0E,0x00,0x00,0x00}}, /* Presentation Y offset */
353  { 0x3217, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x01,0x03,0x02,0x07,0x00,0x00,0x00}}, /* Display F2 offset */
354  { 0x320E, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x01,0x01,0x01,0x00,0x00,0x00}}, /* Aspect Ratio */
355  { 0x3210, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x01,0x02,0x01,0x01,0x01,0x02,0x00}}, /* Transfer characteristic */
356  { 0x321A, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x01,0x02,0x01,0x01,0x03,0x01,0x00}}, /* Coding Equations (color space) */
357  { 0x3219, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x09,0x04,0x01,0x02,0x01,0x01,0x06,0x01,0x00}}, /* Color Primaries */
358  { 0x3213, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x18,0x01,0x02,0x00,0x00,0x00,0x00}}, /* Image Start Offset */
359  { 0x3214, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x18,0x01,0x03,0x00,0x00,0x00,0x00}}, /* Image End Offset */
360  { 0x3201, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x01,0x06,0x01,0x00,0x00,0x00,0x00}}, /* Picture Essence Coding */
361  { 0x3212, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x01,0x03,0x01,0x06,0x00,0x00,0x00}}, /* Field Dominance (Opt) */
362  { 0x3215, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x05,0x01,0x13,0x00,0x00,0x00,0x00}}, /* Signal Standard */
363  // CDCI Picture Essence Descriptor
364  { 0x3301, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x01,0x05,0x03,0x0A,0x00,0x00,0x00}}, /* Component Depth */
365  { 0x3302, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x01,0x05,0x00,0x00,0x00}}, /* Horizontal Subsampling */
366  { 0x3308, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x01,0x05,0x01,0x10,0x00,0x00,0x00}}, /* Vertical Subsampling */
367  { 0x3303, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x01,0x06,0x00,0x00,0x00}}, /* Color Siting */
368  { 0x3307, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x18,0x01,0x04,0x00,0x00,0x00,0x00}}, /* Padding Bits */
369  { 0x3304, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x03,0x03,0x00,0x00,0x00}}, /* Black Ref level */
370  { 0x3305, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x03,0x04,0x00,0x00,0x00}}, /* White Ref level */
371  { 0x3306, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x01,0x05,0x03,0x05,0x00,0x00,0x00}}, /* Color Range */
372  // Generic Sound Essence Descriptor
373  { 0x3D02, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x04,0x02,0x03,0x01,0x04,0x00,0x00,0x00}}, /* Locked/Unlocked */
374  { 0x3D03, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x02,0x03,0x01,0x01,0x01,0x00,0x00}}, /* Audio sampling rate */
375  { 0x3D04, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x02,0x01,0x01,0x03,0x00,0x00,0x00}}, /* Audio Ref Level */
376  { 0x3D07, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x02,0x01,0x01,0x04,0x00,0x00,0x00}}, /* ChannelCount */
377  { 0x3D01, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x04,0x02,0x03,0x03,0x04,0x00,0x00,0x00}}, /* Quantization bits */
378  { 0x3D06, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x02,0x04,0x02,0x00,0x00,0x00,0x00}}, /* Sound Essence Compression */
379  // Index Table Segment
380  { 0x3F0B, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x05,0x30,0x04,0x06,0x00,0x00,0x00,0x00}}, /* Index Edit Rate */
381  { 0x3F0C, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x07,0x02,0x01,0x03,0x01,0x0A,0x00,0x00}}, /* Index Start Position */
382  { 0x3F0D, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x07,0x02,0x02,0x01,0x01,0x02,0x00,0x00}}, /* Index Duration */
383  { 0x3F05, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x04,0x06,0x02,0x01,0x00,0x00,0x00,0x00}}, /* Edit Unit Byte Count */
384  { 0x3F06, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x01,0x03,0x04,0x05,0x00,0x00,0x00,0x00}}, /* IndexSID */
385  { 0x3F08, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x04,0x04,0x04,0x01,0x01,0x00,0x00,0x00}}, /* Slice Count */
386  { 0x3F09, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x04,0x04,0x01,0x06,0x00,0x00,0x00}}, /* Delta Entry Array */
387  { 0x3F0A, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x04,0x04,0x02,0x05,0x00,0x00,0x00}}, /* Index Entry Array */
388  // MPEG video Descriptor
389  { 0x8000, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x01,0x06,0x02,0x01,0x0B,0x00,0x00}}, /* BitRate */
390  { 0x8003, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x01,0x06,0x02,0x01,0x05,0x00,0x00}}, /* LowDelay */
391  { 0x8004, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x01,0x06,0x02,0x01,0x06,0x00,0x00}}, /* ClosedGOP */
392  { 0x8006, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x01,0x06,0x02,0x01,0x08,0x00,0x00}}, /* MaxGOP */
393  { 0x8007, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x01,0x06,0x02,0x01,0x0A,0x00,0x00}}, /* ProfileAndLevel */
394  { 0x8008, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x01,0x06,0x02,0x01,0x09,0x00,0x00}}, /* BPictureCount */
395  // Wave Audio Essence Descriptor
396  { 0x3D09, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x02,0x03,0x03,0x05,0x00,0x00,0x00}}, /* Average Bytes Per Second */
397  { 0x3D0A, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x02,0x03,0x02,0x01,0x00,0x00,0x00}}, /* Block Align */
398  // mxf_user_comments_local_tag
399  { 0x4406, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x03,0x02,0x01,0x02,0x0C,0x00,0x00,0x00}}, /* User Comments */
400  { 0x5001, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x03,0x02,0x01,0x02,0x09,0x01,0x00,0x00}}, /* Name */
401  { 0x5003, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x03,0x02,0x01,0x02,0x0A,0x01,0x00,0x00}}, /* Value */
402  // mxf_avc_subdescriptor_local_tags
403  { 0x8100, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x09,0x06,0x01,0x01,0x04,0x06,0x10,0x00,0x00}}, /* SubDescriptors */
404  { 0x8200, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x0E,0x04,0x01,0x06,0x06,0x01,0x0E,0x00,0x00}}, /* AVC Decoding Delay */
405  { 0x8201, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x0E,0x04,0x01,0x06,0x06,0x01,0x0A,0x00,0x00}}, /* AVC Profile */
406  { 0x8202, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x0E,0x04,0x01,0x06,0x06,0x01,0x0D,0x00,0x00}}, /* AVC Level */
407  // ff_mxf_mastering_display_local_tags
412  // FFV1
413  { 0xDFD9, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x0E,0x04,0x01,0x06,0x0C,0x06,0x00,0x00,0x00}}, /* FFV1 Micro-version */
414  { 0xDFDA, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x0E,0x04,0x01,0x06,0x0C,0x05,0x00,0x00,0x00}}, /* FFV1 Version */
415  { 0xDFDB, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x0E,0x04,0x01,0x06,0x0C,0x01,0x00,0x00,0x00}}, /* FFV1 Initialization Metadata */
416 };
417 
418 #define MXF_NUM_TAGS FF_ARRAY_ELEMS(mxf_local_tag_batch)
419 
420 typedef struct MXFContext {
428  uint64_t timestamp; ///< timestamp, as year(16),month(8),day(8),hour(8),minutes(8),msec/4(8)
429  uint8_t slice_count; ///< index slice count minus 1 (1 if no audio, 0 otherwise)
433  int last_key_index; ///< index of last key frame
434  uint64_t duration;
435  AVTimecode tc; ///< timecode context
437  int timecode_base; ///< rounded time code base (25 or 30)
438  int edit_unit_byte_count; ///< fixed edit unit byte count
439  int content_package_rate; ///< content package rate in system element, see SMPTE 326M
440  uint64_t body_offset;
441  uint32_t instance_number;
442  uint8_t umid[16]; ///< unique material identifier
448  int track_instance_count; // used to generate MXFTrack uuids
449  int cbr_index; ///< use a constant bitrate index
450  uint8_t unused_tags[MXF_NUM_TAGS]; ///< local tags that we know will not be used
452 } MXFContext;
453 
455 {
456  avio_write(pb, uuid_base, 10);
457  avio_wb16(pb, type);
458  avio_wb32(pb, value);
459 }
460 
462 {
463  MXFContext *mxf = s->priv_data;
464  avio_write(s->pb, umid_ul, 13);
465  avio_wb24(s->pb, mxf->instance_number);
466  avio_write(s->pb, mxf->umid, 15);
467  avio_w8(s->pb, type);
468 }
469 
470 static void mxf_write_refs_count(AVIOContext *pb, int ref_count)
471 {
472  avio_wb32(pb, ref_count);
473  avio_wb32(pb, 16);
474 }
475 
476 static int klv_ber_length(uint64_t len)
477 {
478  if (len < 128)
479  return 1;
480  else
481  return (av_log2(len) >> 3) + 2;
482 }
483 
484 static int klv_encode_ber_length(AVIOContext *pb, uint64_t len)
485 {
486  // Determine the best BER size
487  int size = klv_ber_length(len);
488  if (size == 1) {
489  //short form
490  avio_w8(pb, len);
491  return 1;
492  }
493 
494  size --;
495  // long form
496  avio_w8(pb, 0x80 + size);
497  while(size) {
498  size--;
499  avio_w8(pb, len >> 8 * size & 0xff);
500  }
501  return 0;
502 }
503 
505 {
506  avio_w8(pb, 0x80 + 3);
507  avio_wb24(pb, len);
508 }
509 
510 static void klv_encode_ber9_length(AVIOContext *pb, uint64_t len)
511 {
512  avio_w8(pb, 0x80 + 8);
513  avio_wb64(pb, len);
514 }
515 
516 /*
517  * Get essence container ul index
518  */
520 {
521  int i;
522  for (i = 0; mxf_essence_mappings[i].id; i++)
523  if (mxf_essence_mappings[i].id == id)
524  return mxf_essence_mappings[i].index;
525  return -1;
526 }
527 
529 {
530  for (int i = 0; i < MXF_NUM_TAGS; i++) {
531  if (mxf_local_tag_batch[i].local_tag == tag) {
532  return &mxf_local_tag_batch[i];
533  }
534  }
535 
536  // this assert can only be hit during development
537  av_assert0(0 && "you forgot to add your new tag to mxf_local_tag_batch");
538  return NULL;
539 }
540 
541 static void mxf_mark_tag_unused(MXFContext *mxf, int tag)
542 {
544  mxf->unused_tags[pair - mxf_local_tag_batch] = 1;
545 }
546 
548 {
549  MXFContext *mxf = s->priv_data;
550  AVIOContext *pb = s->pb;
551  int local_tag_number = MXF_NUM_TAGS, i;
552  int will_have_avc_tags = 0, will_have_mastering_tags = 0, will_have_ffv1_tags = 0;
553 
554  for (i = 0; i < s->nb_streams; i++) {
555  MXFStreamContext *sc = s->streams[i]->priv_data;
556  if (s->streams[i]->codecpar->codec_id == AV_CODEC_ID_H264 && !sc->avc_intra) {
557  will_have_avc_tags = 1;
558  }
559  if (av_packet_side_data_get(s->streams[i]->codecpar->coded_side_data,
560  s->streams[i]->codecpar->nb_coded_side_data,
562  will_have_mastering_tags = 1;
563  }
564  if (s->streams[i]->codecpar->codec_id == AV_CODEC_ID_FFV1) {
565  will_have_ffv1_tags = 1;
566  }
567  }
568 
569  if (!mxf->store_user_comments) {
570  mxf_mark_tag_unused(mxf, 0x4406);
571  mxf_mark_tag_unused(mxf, 0x5001);
572  mxf_mark_tag_unused(mxf, 0x5003);
573  }
574 
575  if (!will_have_avc_tags && !will_have_ffv1_tags) {
576  mxf_mark_tag_unused(mxf, 0x8100);
577  }
578 
579  if (!will_have_avc_tags) {
580  mxf_mark_tag_unused(mxf, 0x8200);
581  mxf_mark_tag_unused(mxf, 0x8201);
582  mxf_mark_tag_unused(mxf, 0x8202);
583  }
584 
585  if (!will_have_mastering_tags) {
586  mxf_mark_tag_unused(mxf, 0x8301);
587  mxf_mark_tag_unused(mxf, 0x8302);
588  mxf_mark_tag_unused(mxf, 0x8303);
589  mxf_mark_tag_unused(mxf, 0x8304);
590  }
591 
592  if (!will_have_ffv1_tags) {
593  mxf_mark_tag_unused(mxf, 0xDFD9);
594  mxf_mark_tag_unused(mxf, 0xDFDA);
595  mxf_mark_tag_unused(mxf, 0xDFDB);
596  }
597 
598  for (i = 0; i < MXF_NUM_TAGS; i++) {
599  if (mxf->unused_tags[i]) {
600  local_tag_number--;
601  }
602  }
603 
604  avio_write(pb, primer_pack_key, 16);
605  klv_encode_ber_length(pb, local_tag_number * 18 + 8);
606 
607  avio_wb32(pb, local_tag_number); // local_tag num
608  avio_wb32(pb, 18); // item size, always 18 according to the specs
609 
610  for (i = 0; i < MXF_NUM_TAGS; i++) {
611  if (mxf->unused_tags[i] == 0) {
612  avio_wb16(pb, mxf_local_tag_batch[i].local_tag);
614  }
615  }
616 }
617 
619 {
620  MXFContext *mxf = s->priv_data;
621  AVIOContext *pb = s->pb;
623 
624  // make sure the tag was not declared unnecessary upfront
625  av_assert0(mxf->unused_tags[pair - mxf_local_tag_batch] == 0);
626 
627  avio_wb16(pb, tag);
628  avio_wb16(pb, size);
629 }
630 
631 static void mxf_write_metadata_key(AVIOContext *pb, unsigned int value)
632 {
634  avio_wb24(pb, value);
635 }
636 
637 static const MXFCodecUL *mxf_get_codec_ul_by_id(const MXFCodecUL *uls, int id)
638 {
639  while (uls->uid[0]) {
640  if (id == uls->id)
641  break;
642  uls++;
643  }
644  return uls;
645 }
646 
647 //one EC -> one descriptor. N ECs -> MultipleDescriptor + N descriptors
648 #define DESCRIPTOR_COUNT(essence_container_count) \
649  (essence_container_count > 1 ? essence_container_count + 1 : essence_container_count)
650 
652 {
653  MXFContext *c = s->priv_data;
654  AVIOContext *pb = s->pb;
655  int i;
656 
657  mxf_write_refs_count(pb, DESCRIPTOR_COUNT(c->essence_container_count));
658  av_log(s,AV_LOG_DEBUG, "essence container count:%d\n", c->essence_container_count);
659  for (i = 0; i < s->nb_streams; i++) {
660  MXFStreamContext *sc = s->streams[i]->priv_data;
661  // check first track of essence container type and only write it once
662  if (sc->track_essence_element_key[15] != 0)
663  continue;
664  avio_write(pb, *sc->container_ul, 16);
665  if (c->essence_container_count == 1)
666  break;
667  }
668 
669  if (c->essence_container_count > 1)
670  avio_write(pb, multiple_desc_ul, 16);
671 }
672 
674 {
675  MXFContext *mxf = s->priv_data;
676  AVIOContext *pb = s->pb;
677 
678  mxf_write_metadata_key(pb, 0x012f00);
679  PRINT_KEY(s, "preface key", pb->buf_ptr - 16);
681 
682  // write preface set uid
683  mxf_write_local_tag(s, 16, 0x3C0A);
684  mxf_write_uuid(pb, Preface, 0);
685  PRINT_KEY(s, "preface uid", pb->buf_ptr - 16);
686 
687  // last modified date
688  mxf_write_local_tag(s, 8, 0x3B02);
689  avio_wb64(pb, mxf->timestamp);
690 
691  // write version
692  mxf_write_local_tag(s, 2, 0x3B05);
693  avio_wb16(pb, 259); // v1.3
694 
695  // Object Model Version
696  mxf_write_local_tag(s, 4, 0x3B07);
697  avio_wb32(pb, 1);
698 
699  // write identification_refs
700  mxf_write_local_tag(s, 16 + 8, 0x3B06);
701  mxf_write_refs_count(pb, 1);
703 
704  // write content_storage_refs
705  mxf_write_local_tag(s, 16, 0x3B03);
707 
708  // operational pattern
709  mxf_write_local_tag(s, 16, 0x3B09);
710  if (IS_OPATOM(s))
711  avio_write(pb, opatom_ul, 16);
712  else
713  avio_write(pb, op1a_ul, 16);
714 
715  // write essence_container_refs
718 
719  // write dm_scheme_refs
720  mxf_write_local_tag(s, 8, 0x3B0B);
721  avio_wb64(pb, 0);
722 }
723 
724 /*
725  * Returns the length of the UTF-16 string, in 16-bit characters, that would result
726  * from decoding the utf-8 string.
727  */
728 static uint64_t mxf_utf16len(const char *utf8_str)
729 {
730  const uint8_t *q = utf8_str;
731  uint64_t size = 0;
732  while (*q) {
733  uint32_t ch;
734  GET_UTF8(ch, *q++, goto invalid;)
735  if (ch < 0x10000)
736  size++;
737  else
738  size += 2;
739  continue;
740 invalid:
741  av_log(NULL, AV_LOG_ERROR, "Invalid UTF8 sequence in mxf_utf16len\n\n");
742  }
743  size += 1;
744  return size;
745 }
746 
747 /*
748  * Returns the calculated length a local tag containing an utf-8 string as utf-16
749  */
750 static int mxf_utf16_local_tag_length(const char *utf8_str)
751 {
752  uint64_t size;
753 
754  if (!utf8_str)
755  return 0;
756 
757  size = mxf_utf16len(utf8_str);
758  if (size >= UINT16_MAX/2) {
759  av_log(NULL, AV_LOG_ERROR, "utf16 local tag size %"PRIx64" invalid (too large), ignoring\n", size);
760  return 0;
761  }
762 
763  return 4 + size * 2;
764 }
765 
766 /*
767  * Write a local tag containing an utf-8 string as utf-16
768  */
769 static void mxf_write_local_tag_utf16(AVFormatContext *s, int tag, const char *value)
770 {
771  AVIOContext *pb = s->pb;
772  uint64_t size = mxf_utf16len(value);
773 
774  if (size >= UINT16_MAX/2) {
775  av_log(NULL, AV_LOG_ERROR, "utf16 local tag size %"PRIx64" invalid (too large), ignoring\n", size);
776  return;
777  }
778 
780  avio_put_str16be(pb, value);
781 }
782 
784  AVIOContext *pb = s->pb;
785 
786  if (s->flags & AVFMT_FLAG_BITEXACT) {
787  avio_wb16(pb, 0); // major
788  avio_wb16(pb, 0); // minor
789  avio_wb16(pb, 0); // tertiary
790  } else {
791  avio_wb16(pb, LIBAVFORMAT_VERSION_MAJOR); // major
792  avio_wb16(pb, LIBAVFORMAT_VERSION_MINOR); // minor
793  avio_wb16(pb, LIBAVFORMAT_VERSION_MICRO); // tertiary
794  }
795  avio_wb16(pb, 0); // patch
796  avio_wb16(pb, 0); // release
797 }
798 
799 #define PLATFORM_IDENT "Lavf " AV_STRINGIFY((OS_NAME))
801 {
802  MXFContext *mxf = s->priv_data;
803  AVIOContext *pb = s->pb;
804  AVDictionaryEntry *com_entry = av_dict_get(s->metadata, "company_name", NULL, 0);
805  AVDictionaryEntry *product_entry = av_dict_get(s->metadata, "product_name", NULL, 0);
806  AVDictionaryEntry *version_entry = av_dict_get(s->metadata, "product_version", NULL, 0);
807  const char *company = com_entry ? com_entry->value : "FFmpeg";
808  const char *product = product_entry ? product_entry->value : !IS_OPATOM(s) ? "OP1a Muxer" : "OPAtom Muxer";
809  const char *platform = s->flags & AVFMT_FLAG_BITEXACT ? "Lavf" : PLATFORM_IDENT;
810  const char *version = version_entry ? version_entry->value :
811  s->flags & AVFMT_FLAG_BITEXACT ? "0.0.0" :
813  int length;
814 
815  mxf_write_metadata_key(pb, 0x013000);
816  PRINT_KEY(s, "identification key", pb->buf_ptr - 16);
817 
818  length = 100 +mxf_utf16_local_tag_length(company) +
819  mxf_utf16_local_tag_length(product) +
820  mxf_utf16_local_tag_length(platform) +
822  klv_encode_ber_length(pb, length);
823 
824  // write uid
825  mxf_write_local_tag(s, 16, 0x3C0A);
827  PRINT_KEY(s, "identification uid", pb->buf_ptr - 16);
828 
829  // write generation uid
830  mxf_write_local_tag(s, 16, 0x3C09);
832  mxf_write_local_tag_utf16(s, 0x3C01, company); // Company Name
833  mxf_write_local_tag_utf16(s, 0x3C02, product); // Product Name
834 
835  mxf_write_local_tag(s, 10, 0x3C03); // Product Version
836  store_version(s);
837 
838  mxf_write_local_tag_utf16(s, 0x3C04, version); // Version String
839  mxf_write_local_tag_utf16(s, 0x3C08, platform); // Platform
840 
841  // write product uid
842  mxf_write_local_tag(s, 16, 0x3C05);
843  avio_write(pb, product_uid, 16);
844 
845  // modification date
846  mxf_write_local_tag(s, 8, 0x3C06);
847  avio_wb64(pb, mxf->timestamp);
848 
849  mxf_write_local_tag(s, 10, 0x3C07); // Toolkit Version
850  store_version(s);
851 }
852 
853 static void mxf_write_content_storage(AVFormatContext *s, MXFPackage *packages, int package_count)
854 {
855  AVIOContext *pb = s->pb;
856  int i;
857 
858  mxf_write_metadata_key(pb, 0x011800);
859  PRINT_KEY(s, "content storage key", pb->buf_ptr - 16);
860  klv_encode_ber_length(pb, 60 + (16 * package_count));
861 
862  // write uid
863  mxf_write_local_tag(s, 16, 0x3C0A);
865  PRINT_KEY(s, "content storage uid", pb->buf_ptr - 16);
866 
867  // write package reference
868  mxf_write_local_tag(s, 16 * package_count + 8, 0x1901);
869  mxf_write_refs_count(pb, package_count);
870  for (i = 0; i < package_count; i++) {
871  mxf_write_uuid(pb, packages[i].type, packages[i].instance);
872  }
873 
874  // write essence container data
875  mxf_write_local_tag(s, 8 + 16, 0x1902);
876  mxf_write_refs_count(pb, 1);
878 }
879 
881 {
882  MXFContext *mxf = s->priv_data;
883  AVIOContext *pb = s->pb;
884  MXFStreamContext *sc = st->priv_data;
885 
886  mxf_write_metadata_key(pb, 0x013b00);
887  PRINT_KEY(s, "track key", pb->buf_ptr - 16);
888  klv_encode_ber_length(pb, 80);
889 
890  // write track uid
891  mxf_write_local_tag(s, 16, 0x3C0A);
893  PRINT_KEY(s, "track uid", pb->buf_ptr - 16);
894 
895  // write track id
896  mxf_write_local_tag(s, 4, 0x4801);
897  avio_wb32(pb, st->index+2);
898 
899  // write track number
900  mxf_write_local_tag(s, 4, 0x4804);
901  if (package->type == MaterialPackage)
902  avio_wb32(pb, 0); // track number of material package is 0
903  else
904  avio_write(pb, sc->track_essence_element_key + 12, 4);
905 
906  // write edit rate
907  mxf_write_local_tag(s, 8, 0x4B01);
908 
909  if (st == mxf->timecode_track && IS_OPATOM(s)) {
910  avio_wb32(pb, mxf->tc.rate.num);
911  avio_wb32(pb, mxf->tc.rate.den);
912  } else {
913  avio_wb32(pb, mxf->time_base.den);
914  avio_wb32(pb, mxf->time_base.num);
915  }
916 
917  // write origin
918  mxf_write_local_tag(s, 8, 0x4B02);
919  avio_wb64(pb, 0);
920 
921  // write sequence refs
922  mxf_write_local_tag(s, 16, 0x4803);
924 }
925 
926 static const uint8_t smpte_12m_timecode_track_data_ul[] = { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x01,0x03,0x02,0x01,0x01,0x00,0x00,0x00 };
927 
929 {
930  MXFContext *mxf = s->priv_data;
931  AVIOContext *pb = s->pb;
932 
933  // find data define uls
934  mxf_write_local_tag(s, 16, 0x0201);
935  if (st == mxf->timecode_track)
937  else {
939  avio_write(pb, data_def_ul->uid, 16);
940  }
941 
942  // write duration
943  mxf_write_local_tag(s, 8, 0x0202);
944 
945  if (st != mxf->timecode_track && IS_OPATOM(s) && st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
946  avio_wb64(pb, mxf->body_offset / mxf->edit_unit_byte_count);
947  } else {
948  avio_wb64(pb, mxf->duration);
949  }
950 }
951 
953 {
954  MXFContext *mxf = s->priv_data;
955  AVIOContext *pb = s->pb;
956  enum MXFMetadataSetType component;
957 
958  mxf_write_metadata_key(pb, 0x010f00);
959  PRINT_KEY(s, "sequence key", pb->buf_ptr - 16);
960  klv_encode_ber_length(pb, 80);
961 
962  mxf_write_local_tag(s, 16, 0x3C0A);
964 
965  PRINT_KEY(s, "sequence uid", pb->buf_ptr - 16);
967 
968  // write structural component
969  mxf_write_local_tag(s, 16 + 8, 0x1001);
970  mxf_write_refs_count(pb, 1);
971  if (st == mxf->timecode_track)
972  component = TimecodeComponent;
973  else
974  component = SourceClip;
975 
976  mxf_write_uuid(pb, component, mxf->track_instance_count);
977 }
978 
980 {
981  MXFContext *mxf = s->priv_data;
982  AVIOContext *pb = s->pb;
983 
984  mxf_write_metadata_key(pb, 0x011400);
985  klv_encode_ber_length(pb, 75);
986 
987  // UID
988  mxf_write_local_tag(s, 16, 0x3C0A);
990 
992 
993  // Start Time Code
994  mxf_write_local_tag(s, 8, 0x1501);
995  avio_wb64(pb, mxf->tc.start);
996 
997  // Rounded Time Code Base
998  mxf_write_local_tag(s, 2, 0x1502);
999  avio_wb16(pb, mxf->timecode_base);
1000 
1001  // Drop Frame
1002  mxf_write_local_tag(s, 1, 0x1503);
1003  avio_w8(pb, !!(mxf->tc.flags & AV_TIMECODE_FLAG_DROPFRAME));
1004 }
1005 
1007 {
1008  MXFContext *mxf = s->priv_data;
1009  AVIOContext *pb = s->pb;
1010 
1011  mxf_write_metadata_key(pb, 0x011100);
1012  PRINT_KEY(s, "sturctural component key", pb->buf_ptr - 16);
1013  klv_encode_ber_length(pb, 108);
1014 
1015  // write uid
1016  mxf_write_local_tag(s, 16, 0x3C0A);
1018 
1019  PRINT_KEY(s, "structural component uid", pb->buf_ptr - 16);
1021 
1022  // write start_position
1023  mxf_write_local_tag(s, 8, 0x1201);
1024  avio_wb64(pb, 0);
1025 
1026  // write source package uid, end of the reference
1027  mxf_write_local_tag(s, 32, 0x1101);
1028  if (!package->ref) {
1029  ffio_fill(pb, 0, 32);
1030  } else
1031  mxf_write_umid(s, package->ref->instance);
1032 
1033  // write source track id
1034  mxf_write_local_tag(s, 4, 0x1102);
1035  if (package->type == SourcePackage && !package->ref)
1036  avio_wb32(pb, 0);
1037  else
1038  avio_wb32(pb, st->index+2);
1039 }
1040 
1042 {
1043  AVIOContext *pb = s->pb;
1044 
1045  mxf_write_metadata_key(pb, 0x012e00);
1046  PRINT_KEY(s, "tape descriptor key", pb->buf_ptr - 16);
1047  klv_encode_ber_length(pb, 20);
1048  mxf_write_local_tag(s, 16, 0x3C0A);
1050  PRINT_KEY(s, "tape_desc uid", pb->buf_ptr - 16);
1051 }
1052 
1053 
1055 {
1056  MXFContext *mxf = s->priv_data;
1057  AVIOContext *pb = s->pb;
1058  const uint8_t *ul;
1059  int i;
1060 
1061  mxf_write_metadata_key(pb, 0x014400);
1062  PRINT_KEY(s, "multiple descriptor key", pb->buf_ptr - 16);
1063  klv_encode_ber_length(pb, 64 + 16LL * s->nb_streams);
1064 
1065  mxf_write_local_tag(s, 16, 0x3C0A);
1067  PRINT_KEY(s, "multi_desc uid", pb->buf_ptr - 16);
1068 
1069  // write sample rate
1070  mxf_write_local_tag(s, 8, 0x3001);
1071  avio_wb32(pb, mxf->time_base.den);
1072  avio_wb32(pb, mxf->time_base.num);
1073 
1074  // write essence container ul
1075  mxf_write_local_tag(s, 16, 0x3004);
1076  if (mxf->essence_container_count > 1)
1077  ul = multiple_desc_ul;
1078  else {
1079  MXFStreamContext *sc = s->streams[0]->priv_data;
1080  ul = *sc->container_ul;
1081  }
1082  avio_write(pb, ul, 16);
1083 
1084  // write sub descriptor refs
1085  mxf_write_local_tag(s, s->nb_streams * 16 + 8, 0x3F01);
1086  mxf_write_refs_count(pb, s->nb_streams);
1087  for (i = 0; i < s->nb_streams; i++)
1089 }
1090 
1092 {
1093  MXFContext *mxf = s->priv_data;
1094  MXFStreamContext *sc = st->priv_data;
1095  AVIOContext *pb = s->pb;
1096  int64_t pos;
1097 
1098  avio_write(pb, key, 16);
1099  klv_encode_ber4_length(pb, 0);
1100  pos = avio_tell(pb);
1101 
1102  mxf_write_local_tag(s, 16, 0x3C0A);
1104 
1105  mxf_write_local_tag(s, 4, 0x3006);
1106  avio_wb32(pb, st->index+2);
1107 
1108  mxf_write_local_tag(s, 8, 0x3001);
1109  if (IS_D10(s)) {
1110  avio_wb32(pb, mxf->time_base.den);
1111  avio_wb32(pb, mxf->time_base.num);
1112  } else {
1113  if (st->codecpar->codec_id == AV_CODEC_ID_PCM_S16LE ||
1115  avio_wb32(pb, st->codecpar->sample_rate);
1116  avio_wb32(pb, 1);
1117  } else {
1118  avio_wb32(pb, mxf->time_base.den);
1119  avio_wb32(pb, mxf->time_base.num);
1120  }
1121  }
1122 
1123  mxf_write_local_tag(s, 16, 0x3004);
1124  avio_write(pb, *sc->container_ul, 16);
1125 
1126  return pos;
1127 }
1128 
1129 static const UID mxf_s436m_anc_descriptor_key = { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x5c,0x00 };
1130 static const UID mxf_mpegvideo_descriptor_key = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x51,0x00 };
1131 static const UID mxf_wav_descriptor_key = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x48,0x00 };
1132 static const UID mxf_aes3_descriptor_key = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x47,0x00 };
1133 static const UID mxf_cdci_descriptor_key = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0D,0x01,0x01,0x01,0x01,0x01,0x28,0x00 };
1134 static const UID mxf_rgba_descriptor_key = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0D,0x01,0x01,0x01,0x01,0x01,0x29,0x00 };
1135 static const UID mxf_generic_sound_descriptor_key = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0D,0x01,0x01,0x01,0x01,0x01,0x42,0x00 };
1136 
1137 static const UID mxf_avc_subdescriptor_key = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x6E,0x00 };
1138 static const UID mxf_ffv1_subdescriptor_key = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x81,0x03 };
1139 
1140 static inline uint16_t rescale_mastering_chroma(AVRational q)
1141 {
1143 }
1144 
1145 static inline uint32_t rescale_mastering_luma(AVRational q)
1146 {
1148 }
1149 
1151 {
1152  MXFStreamContext *sc = st->priv_data;
1153  AVIOContext *pb = s->pb;
1154  int stored_width = st->codecpar->width;
1155  int stored_height = st->codecpar->height;
1156  int display_width;
1157  int display_height;
1158  int f1, f2;
1159  const MXFCodecUL *color_primaries_ul;
1160  const MXFCodecUL *color_trc_ul;
1161  const MXFCodecUL *color_space_ul;
1162  int64_t pos = mxf_write_generic_desc(s, st, key);
1163  const AVPacketSideData *side_data;
1164 
1168 
1169  if (st->codecpar->codec_id == AV_CODEC_ID_DVVIDEO) {
1170  if (st->codecpar->height == 1080)
1171  stored_width = 1920;
1172  else if (st->codecpar->height == 720)
1173  stored_width = 1280;
1174  }
1175  display_width = stored_width;
1176 
1177  switch (st->codecpar->codec_id) {
1179  case AV_CODEC_ID_H264:
1180  //Based on 16x16 macroblocks
1181  stored_width = (stored_width+15)/16*16;
1182  stored_height = (stored_height+15)/16*16;
1183  break;
1184  default:
1185  break;
1186  }
1187 
1188  //Stored width
1189  mxf_write_local_tag(s, 4, 0x3203);
1190  avio_wb32(pb, stored_width);
1191 
1192  //Stored height
1193  mxf_write_local_tag(s, 4, 0x3202);
1194  avio_wb32(pb, stored_height>>sc->interlaced);
1195 
1196  if (IS_D10(s)) {
1197  //Stored F2 Offset
1198  mxf_write_local_tag(s, 4, 0x3216);
1199  avio_wb32(pb, 0);
1200 
1201  //Image Start Offset
1202  mxf_write_local_tag(s, 4, 0x3213);
1203  avio_wb32(pb, 0);
1204 
1205  //Image End Offset
1206  mxf_write_local_tag(s, 4, 0x3214);
1207  avio_wb32(pb, 0);
1208  }
1209 
1210  //Sampled width
1211  mxf_write_local_tag(s, 4, 0x3205);
1212  avio_wb32(pb, display_width);
1213 
1214  //Samples height
1215  mxf_write_local_tag(s, 4, 0x3204);
1216  avio_wb32(pb, st->codecpar->height>>sc->interlaced);
1217 
1218  //Sampled X Offset
1219  mxf_write_local_tag(s, 4, 0x3206);
1220  avio_wb32(pb, 0);
1221 
1222  //Sampled Y Offset
1223  mxf_write_local_tag(s, 4, 0x3207);
1224  avio_wb32(pb, 0);
1225 
1226  //Display width
1227  mxf_write_local_tag(s, 4, 0x3209);
1228  avio_wb32(pb, display_width);
1229 
1230  if (st->codecpar->height == 608) // PAL + VBI
1231  display_height = 576;
1232  else if (st->codecpar->height == 512) // NTSC + VBI
1233  display_height = 486;
1234  else
1235  display_height = st->codecpar->height;
1236 
1237  //Display height
1238  mxf_write_local_tag(s, 4, 0x3208);
1239  avio_wb32(pb, display_height>>sc->interlaced);
1240 
1241  // display X offset
1242  mxf_write_local_tag(s, 4, 0x320A);
1243  avio_wb32(pb, 0);
1244 
1245  // display Y offset
1246  mxf_write_local_tag(s, 4, 0x320B);
1247  avio_wb32(pb, (st->codecpar->height - display_height)>>sc->interlaced);
1248 
1249  if (sc->interlaced) {
1250  //Display F2 Offset
1251  mxf_write_local_tag(s, 4, 0x3217);
1252  avio_wb32(pb, -((st->codecpar->height - display_height)&1));
1253  }
1254 
1255  if (key != mxf_rgba_descriptor_key) {
1256  // component depth
1257  mxf_write_local_tag(s, 4, 0x3301);
1258  avio_wb32(pb, sc->component_depth);
1259 
1260  // horizontal subsampling
1261  mxf_write_local_tag(s, 4, 0x3302);
1262  avio_wb32(pb, sc->h_chroma_sub_sample);
1263 
1264  // vertical subsampling
1265  mxf_write_local_tag(s, 4, 0x3308);
1266  avio_wb32(pb, sc->v_chroma_sub_sample);
1267 
1268  // color siting
1269  mxf_write_local_tag(s, 1, 0x3303);
1270  avio_w8(pb, sc->color_siting);
1271 
1272  // Padding Bits
1273  mxf_write_local_tag(s, 2, 0x3307);
1274  avio_wb16(pb, 0);
1275 
1277  int black = 0,
1278  white = (1<<sc->component_depth) - 1,
1279  color = (1<<sc->component_depth);
1280  if (st->codecpar->color_range == AVCOL_RANGE_MPEG) {
1281  black = 1 << (sc->component_depth - 4);
1282  white = 235 << (sc->component_depth - 8);
1283  color = (14 << (sc->component_depth - 4)) + 1;
1284  }
1285  mxf_write_local_tag(s, 4, 0x3304);
1286  avio_wb32(pb, black);
1287  mxf_write_local_tag(s, 4, 0x3305);
1288  avio_wb32(pb, white);
1289  mxf_write_local_tag(s, 4, 0x3306);
1290  avio_wb32(pb, color);
1291  }
1292  }
1293 
1294  if (sc->signal_standard) {
1295  mxf_write_local_tag(s, 1, 0x3215);
1296  avio_w8(pb, sc->signal_standard);
1297  }
1298 
1299  // frame layout
1300  mxf_write_local_tag(s, 1, 0x320C);
1301  avio_w8(pb, sc->interlaced);
1302 
1303  // video line map
1304  switch (st->codecpar->height) {
1305  case 576: f1 = 23; f2 = st->codecpar->codec_id == AV_CODEC_ID_DVVIDEO ? 335 : 336; break;
1306  case 608: f1 = 7; f2 = 320; break;
1307  case 480: f1 = 20; f2 = st->codecpar->codec_id == AV_CODEC_ID_DVVIDEO ? 285 : 283; break;
1308  case 512: f1 = 7; f2 = 270; break;
1309  case 720: f1 = 26; f2 = 0; break; // progressive
1310  case 1080: f1 = 21; f2 = 584; break;
1311  default: f1 = 0; f2 = 0; break;
1312  }
1313 
1314  if (!sc->interlaced && f2) {
1315  f2 = 0;
1316  f1 *= 2;
1317  }
1318 
1319 
1320  mxf_write_local_tag(s, 16, 0x320D);
1321  avio_wb32(pb, 2);
1322  avio_wb32(pb, 4);
1323  avio_wb32(pb, f1);
1324  avio_wb32(pb, f2);
1325 
1326  mxf_write_local_tag(s, 8, 0x320E);
1327  avio_wb32(pb, sc->aspect_ratio.num);
1328  avio_wb32(pb, sc->aspect_ratio.den);
1329 
1330  if (color_primaries_ul->uid[0]) {
1331  mxf_write_local_tag(s, 16, 0x3219);
1332  avio_write(pb, color_primaries_ul->uid, 16);
1333  };
1334 
1335  if (color_trc_ul->uid[0]) {
1336  mxf_write_local_tag(s, 16, 0x3210);
1337  avio_write(pb, color_trc_ul->uid, 16);
1338  };
1339 
1340  if (color_space_ul->uid[0]) {
1341  mxf_write_local_tag(s, 16, 0x321A);
1342  avio_write(pb, color_space_ul->uid, 16);
1343  };
1344 
1345  mxf_write_local_tag(s, 16, 0x3201);
1346  avio_write(pb, *sc->codec_ul, 16);
1347 
1348  // Mastering Display metadata
1352  if (side_data) {
1353  const AVMasteringDisplayMetadata *metadata = (const AVMasteringDisplayMetadata*)side_data->data;
1354  if (metadata->has_primaries) {
1355  mxf_write_local_tag(s, 12, 0x8301);
1362  mxf_write_local_tag(s, 4, 0x8302);
1363  avio_wb16(pb, rescale_mastering_chroma(metadata->white_point[0]));
1364  avio_wb16(pb, rescale_mastering_chroma(metadata->white_point[1]));
1365  } else {
1366  av_log(NULL, AV_LOG_VERBOSE, "Not writing mastering display primaries. Missing data.\n");
1367  }
1368  if (metadata->has_luminance) {
1369  mxf_write_local_tag(s, 4, 0x8303);
1371  mxf_write_local_tag(s, 4, 0x8304);
1373  } else {
1374  av_log(NULL, AV_LOG_VERBOSE, "Not writing mastering display luminances. Missing data.\n");
1375  }
1376  }
1377 
1378  if (sc->interlaced && sc->field_dominance) {
1379  mxf_write_local_tag(s, 1, 0x3212);
1380  avio_w8(pb, sc->field_dominance);
1381  }
1382 
1383  if (st->codecpar->codec_id == AV_CODEC_ID_H264 && !sc->avc_intra) {
1384  // write avc sub descriptor ref
1385  mxf_write_local_tag(s, 8 + 16, 0x8100);
1386  mxf_write_refs_count(pb, 1);
1388  }
1389 
1390  if (st->codecpar->codec_id == AV_CODEC_ID_FFV1) {
1391  // write ffv1 sub descriptor ref
1392  mxf_write_local_tag(s, 8 + 16, 0x8100);
1393  mxf_write_refs_count(pb, 1);
1395  }
1396 
1397  return pos;
1398 }
1399 
1400 static void mxf_update_klv_size(AVIOContext *pb, int64_t pos)
1401 {
1402  int64_t cur_pos = avio_tell(pb);
1403  int size = cur_pos - pos;
1404  avio_seek(pb, pos - 4, SEEK_SET);
1406  avio_seek(pb, cur_pos, SEEK_SET);
1407 }
1408 
1410 {
1411  AVIOContext *pb = s->pb;
1412  int64_t pos;
1413 
1415  klv_encode_ber4_length(pb, 0);
1416  pos = avio_tell(pb);
1417 
1418  mxf_write_local_tag(s, 16, 0x3C0A);
1420 
1421  mxf_write_local_tag(s, 1, 0x8200);
1422  avio_w8(pb, 0xFF); // AVC Decoding Delay, unknown
1423 
1424  mxf_write_local_tag(s, 1, 0x8201);
1425  avio_w8(pb, st->codecpar->profile); // AVC Profile
1426 
1427  mxf_write_local_tag(s, 1, 0x8202);
1428  avio_w8(pb, st->codecpar->level); // AVC Level
1429 
1430  mxf_update_klv_size(s->pb, pos);
1431 }
1432 
1434 {
1436  mxf_update_klv_size(s->pb, pos);
1437 
1438  if (st->codecpar->codec_id == AV_CODEC_ID_H264) {
1439  mxf_write_avc_subdesc(s, st);
1440  }
1441 }
1442 
1444 {
1445  MXFStreamContext *sc = st->priv_data;
1446  if (sc->avc_intra) {
1448  } else {
1450  mxf_update_klv_size(s->pb, pos);
1451  mxf_write_avc_subdesc(s, st);
1452  }
1453 }
1454 
1456 {
1457  AVIOContext *pb = s->pb;
1458  MXFStreamContext *sc = st->priv_data;
1459  int64_t pos;
1460 
1462  klv_encode_ber4_length(pb, 0);
1463  pos = avio_tell(pb);
1464 
1465  mxf_write_local_tag(s, 16, 0x3C0A);
1467 
1468  if (st->codecpar->extradata_size) {
1470  avio_write(pb, st->codecpar->extradata, st->codecpar->extradata_size); // FFV1InitializationMetadata
1471  }
1472 
1473  mxf_write_local_tag(s, 2, 0xDFDA);
1474  avio_wb16(pb, (*sc->codec_ul)[14]); // FFV1Version
1475 
1476  if (st->codecpar->extradata_size) {
1477  mxf_write_local_tag(s, 2, 0xDFD9);
1478  avio_wb16(pb, sc->micro_version); // FFV1MicroVersion
1479  }
1480 
1481  mxf_update_klv_size(s->pb, pos);
1482 }
1483 
1485 {
1486  int is_rgb, pos;
1488  av_assert0(desc);
1489  is_rgb = desc->flags & AV_PIX_FMT_FLAG_RGB;
1490 
1492  mxf_update_klv_size(s->pb, pos);
1494 }
1495 
1497 {
1499  mxf_update_klv_size(s->pb, pos);
1500 }
1501 
1503 {
1504  AVIOContext *pb = s->pb;
1505  MXFStreamContext *sc = st->priv_data;
1506  int profile_and_level = (st->codecpar->profile<<4) | st->codecpar->level;
1508 
1509  if (st->codecpar->codec_id != AV_CODEC_ID_H264) {
1510  // bit rate
1511  mxf_write_local_tag(s, 4, 0x8000);
1512  avio_wb32(pb, sc->video_bit_rate);
1513 
1514  // profile and level
1515  mxf_write_local_tag(s, 1, 0x8007);
1516  if (!st->codecpar->profile)
1517  profile_and_level |= 0x80; // escape bit
1518  avio_w8(pb, profile_and_level);
1519 
1520  // low delay
1521  mxf_write_local_tag(s, 1, 0x8003);
1522  avio_w8(pb, sc->low_delay);
1523 
1524  // closed gop
1525  mxf_write_local_tag(s, 1, 0x8004);
1526  avio_w8(pb, sc->seq_closed_gop);
1527 
1528  // max gop
1529  mxf_write_local_tag(s, 2, 0x8006);
1530  avio_wb16(pb, sc->max_gop);
1531 
1532  // b picture count
1533  mxf_write_local_tag(s, 2, 0x8008);
1534  avio_wb16(pb, sc->b_picture_count);
1535  }
1536 
1537  mxf_update_klv_size(pb, pos);
1538 }
1539 
1541 {
1542  AVIOContext *pb = s->pb;
1543  MXFContext *mxf = s->priv_data;
1544  int show_warnings = !mxf->footer_partition_offset;
1545  int64_t pos = mxf_write_generic_desc(s, st, key);
1546 
1547  if (IS_OPATOM(s)) {
1548  mxf_write_local_tag(s, 8, 0x3002);
1549  avio_wb64(pb, mxf->body_offset / mxf->edit_unit_byte_count);
1550  }
1551 
1552  // audio locked
1553  mxf_write_local_tag(s, 1, 0x3D02);
1554  avio_w8(pb, 1);
1555 
1556  // write audio sampling rate
1557  mxf_write_local_tag(s, 8, 0x3D03);
1558  avio_wb32(pb, st->codecpar->sample_rate);
1559  avio_wb32(pb, 1);
1560 
1561  if (IS_D10(s)) {
1562  mxf_write_local_tag(s, 1, 0x3D04);
1563  avio_w8(pb, 0);
1564  }
1565 
1566  mxf_write_local_tag(s, 4, 0x3D07);
1567  if (mxf->channel_count == -1) {
1568  if (show_warnings && IS_D10(s) &&
1569  (st->codecpar->ch_layout.nb_channels != 4) &&
1570  (st->codecpar->ch_layout.nb_channels != 8))
1571  av_log(s, AV_LOG_WARNING, "the number of audio channels shall be 4 or 8 : the output will not comply to MXF D-10 specs, use -d10_channelcount to fix this\n");
1573  } else if (IS_D10(s)) {
1574  if (show_warnings && (mxf->channel_count < st->codecpar->ch_layout.nb_channels))
1575  av_log(s, AV_LOG_WARNING, "d10_channelcount < actual number of audio channels : some channels will be discarded\n");
1576  if (show_warnings && (mxf->channel_count != 4) && (mxf->channel_count != 8))
1577  av_log(s, AV_LOG_WARNING, "d10_channelcount shall be set to 4 or 8 : the output will not comply to MXF D-10 specs\n");
1578  avio_wb32(pb, mxf->channel_count);
1579  } else {
1581  }
1582 
1583  mxf_write_local_tag(s, 4, 0x3D01);
1585 
1586  return pos;
1587 }
1588 
1590 {
1591  AVIOContext *pb = s->pb;
1592  int64_t pos = mxf_write_generic_sound_common(s, st, key);
1593 
1594  mxf_write_local_tag(s, 2, 0x3D0A);
1595  avio_wb16(pb, st->codecpar->block_align);
1596 
1597  // avg bytes per sec
1598  mxf_write_local_tag(s, 4, 0x3D09);
1600 
1601  return pos;
1602 }
1603 
1605 {
1607  mxf_update_klv_size(s->pb, pos);
1608 }
1609 
1611 {
1613  mxf_update_klv_size(s->pb, pos);
1614 }
1615 
1617 {
1619  mxf_update_klv_size(s->pb, pos);
1620 }
1621 
1622 static const uint8_t mxf_indirect_value_utf16le[] = { 0x4c,0x00,0x02,0x10,0x01,0x00,0x00,0x00,0x00,0x06,0x0e,0x2b,0x34,0x01,0x04,0x01,0x01 };
1623 
1624 static int mxf_write_tagged_value(AVFormatContext *s, const char* name, const char* value)
1625 {
1626  MXFContext *mxf = s->priv_data;
1627  AVIOContext *pb = s->pb;
1628  int name_size = mxf_utf16_local_tag_length(name);
1629  int indirect_value_size = 13 + mxf_utf16_local_tag_length(value);
1630 
1631  if (!name_size || indirect_value_size == 13)
1632  return 1;
1633 
1634  mxf_write_metadata_key(pb, 0x013f00);
1635  klv_encode_ber_length(pb, 24 + name_size + indirect_value_size);
1636 
1637  // write instance UID
1638  mxf_write_local_tag(s, 16, 0x3C0A);
1640 
1641  // write name
1642  mxf_write_local_tag_utf16(s, 0x5001, name); // Name
1643 
1644  // write indirect value
1645  mxf_write_local_tag(s, indirect_value_size, 0x5003);
1647  avio_put_str16le(pb, value);
1648 
1649  mxf->tagged_value_count++;
1650  return 0;
1651 }
1652 
1654 {
1655  MXFContext *mxf = s->priv_data;
1656  AVDictionaryEntry *t = NULL;
1657  int count = 0;
1658 
1659  while ((t = av_dict_get(m, "comment_", t, AV_DICT_IGNORE_SUFFIX))) {
1660  if (mxf->tagged_value_count >= UINT16_MAX) {
1661  av_log(s, AV_LOG_ERROR, "too many tagged values, ignoring remaining\n");
1662  return count;
1663  }
1664 
1665  if (mxf_write_tagged_value(s, t->key + 8, t->value) == 0)
1666  count++;
1667  }
1668  return count;
1669 }
1670 
1672 {
1673  MXFContext *mxf = s->priv_data;
1674  AVIOContext *pb = s->pb;
1675  int i, track_count = s->nb_streams+1;
1676  int name_size = mxf_utf16_local_tag_length(package->name);
1677  int user_comment_count = 0;
1678 
1679  if (package->type == MaterialPackage) {
1680  if (mxf->store_user_comments)
1681  user_comment_count = mxf_write_user_comments(s, s->metadata);
1682  mxf_write_metadata_key(pb, 0x013600);
1683  PRINT_KEY(s, "Material Package key", pb->buf_ptr - 16);
1684  klv_encode_ber_length(pb, 92 + name_size + (16*track_count) + (16*user_comment_count) + 12LL*mxf->store_user_comments);
1685  } else {
1686  mxf_write_metadata_key(pb, 0x013700);
1687  PRINT_KEY(s, "Source Package key", pb->buf_ptr - 16);
1688  klv_encode_ber_length(pb, 112 + name_size + (16*track_count) + 12LL*mxf->store_user_comments); // 20 bytes length for descriptor reference
1689  }
1690 
1691  // write uid
1692  mxf_write_local_tag(s, 16, 0x3C0A);
1693  mxf_write_uuid(pb, package->type, package->instance);
1694  av_log(s, AV_LOG_DEBUG, "package type:%d\n", package->type);
1695  PRINT_KEY(s, "package uid", pb->buf_ptr - 16);
1696 
1697  // write package umid
1698  mxf_write_local_tag(s, 32, 0x4401);
1699  mxf_write_umid(s, package->instance);
1700  PRINT_KEY(s, "package umid second part", pb->buf_ptr - 16);
1701 
1702  // package name
1703  if (name_size)
1704  mxf_write_local_tag_utf16(s, 0x4402, package->name);
1705 
1706  // package creation date
1707  mxf_write_local_tag(s, 8, 0x4405);
1708  avio_wb64(pb, mxf->timestamp);
1709 
1710  // package modified date
1711  mxf_write_local_tag(s, 8, 0x4404);
1712  avio_wb64(pb, mxf->timestamp);
1713 
1714  // write track refs
1715  mxf_write_local_tag(s, track_count*16 + 8, 0x4403);
1716  mxf_write_refs_count(pb, track_count);
1717  // these are the uuids of the tracks the will be written in mxf_write_track
1718  for (i = 0; i < track_count; i++)
1720 
1721  // write user comment refs
1722  if (mxf->store_user_comments) {
1723  mxf_write_local_tag(s, user_comment_count*16 + 8, 0x4406);
1724  mxf_write_refs_count(pb, user_comment_count);
1725  for (i = 0; i < user_comment_count; i++)
1726  mxf_write_uuid(pb, TaggedValue, mxf->tagged_value_count - user_comment_count + i);
1727  }
1728 
1729  // write multiple descriptor reference
1730  if (package->type == SourcePackage && package->instance == 1) {
1731  mxf_write_local_tag(s, 16, 0x4701);
1732  if (s->nb_streams > 1) {
1735  } else
1736  mxf_write_uuid(pb, SubDescriptor, 0);
1737  } else if (package->type == SourcePackage && package->instance == 2) {
1738  mxf_write_local_tag(s, 16, 0x4701);
1741  }
1742 
1743  /*
1744  * for every 1 track in a package there is 1 sequence and 1 component.
1745  * all 3 of these elements share the same instance number for generating
1746  * there instance uuids. mxf->track_instance_count stores this value.
1747  * mxf->track_instance_count is incremented after a group of all 3 of
1748  * these elements are written.
1749  */
1750 
1751  // write timecode track
1752  mxf_write_track(s, mxf->timecode_track, package);
1753  mxf_write_sequence(s, mxf->timecode_track, package);
1755  mxf->track_instance_count++;
1756 
1757  for (i = 0; i < s->nb_streams; i++) {
1758  AVStream *st = s->streams[i];
1759  mxf_write_track(s, st, package);
1760  mxf_write_sequence(s, st, package);
1761  mxf_write_structural_component(s, st, package);
1762  mxf->track_instance_count++;
1763 
1764  if (package->type == SourcePackage && package->instance == 1) {
1765  MXFStreamContext *sc = st->priv_data;
1767  }
1768  }
1769 }
1770 
1772 {
1773  AVIOContext *pb = s->pb;
1774 
1775  mxf_write_metadata_key(pb, 0x012300);
1776  klv_encode_ber_length(pb, 72);
1777 
1778  mxf_write_local_tag(s, 16, 0x3C0A); // Instance UID
1780 
1781  mxf_write_local_tag(s, 32, 0x2701); // Linked Package UID
1782  mxf_write_umid(s, 1);
1783 
1784  mxf_write_local_tag(s, 4, 0x3F07); // BodySID
1785  avio_wb32(pb, 1);
1786 
1787  mxf_write_local_tag(s, 4, 0x3F06); // IndexSID
1788  avio_wb32(pb, 2);
1789 
1790  return 0;
1791 }
1792 
1794 {
1795  MXFContext *mxf = s->priv_data;
1796  AVDictionaryEntry *entry = NULL;
1797  AVStream *st = NULL;
1798  int i;
1799  MXFPackage packages[3] = {{0}};
1800  int package_count = 2;
1801  packages[0].type = MaterialPackage;
1802  packages[1].type = SourcePackage;
1803  packages[1].instance = 1;
1804  packages[0].ref = &packages[1];
1805 
1806 
1807  if (entry = av_dict_get(s->metadata, "material_package_name", NULL, 0))
1808  packages[0].name = entry->value;
1809 
1810  if (entry = av_dict_get(s->metadata, "file_package_name", NULL, 0)) {
1811  packages[1].name = entry->value;
1812  } else {
1813  /* check if any of the streams contain a file_package_name */
1814  for (i = 0; i < s->nb_streams; i++) {
1815  st = s->streams[i];
1816  if (entry = av_dict_get(st->metadata, "file_package_name", NULL, 0)) {
1817  packages[1].name = entry->value;
1818  break;
1819  }
1820  }
1821  }
1822 
1823  entry = av_dict_get(s->metadata, "reel_name", NULL, 0);
1824  if (entry) {
1825  packages[2].name = entry->value;
1826  packages[2].type = SourcePackage;
1827  packages[2].instance = 2;
1828  packages[1].ref = &packages[2];
1829  package_count = 3;
1830  }
1831 
1834  mxf_write_content_storage(s, packages, package_count);
1835  mxf->track_instance_count = 0;
1836  for (i = 0; i < package_count; i++)
1837  mxf_write_package(s, &packages[i]);
1839  return 0;
1840 }
1841 
1842 static unsigned klv_fill_size(uint64_t size)
1843 {
1844  unsigned pad = KAG_SIZE - (size & (KAG_SIZE-1));
1845  if (pad < 20) // smallest fill item possible
1846  return pad + KAG_SIZE;
1847  else
1848  return pad & (KAG_SIZE-1);
1849 }
1850 
1852 {
1853  MXFContext *mxf = s->priv_data;
1854  AVIOContext *pb = s->pb;
1855  int i, j, temporal_reordering = 0;
1856  int key_index = mxf->last_key_index;
1857  int prev_non_b_picture = 0;
1858  int audio_frame_size = 0;
1859  int64_t pos;
1860 
1861  av_log(s, AV_LOG_DEBUG, "edit units count %d\n", mxf->edit_units_count);
1862 
1863  if (!mxf->edit_units_count && !mxf->edit_unit_byte_count)
1864  return;
1865 
1867 
1868  klv_encode_ber4_length(pb, 0);
1869  pos = avio_tell(pb);
1870 
1871  // instance id
1872  mxf_write_local_tag(s, 16, 0x3C0A);
1874 
1875  // index edit rate
1876  mxf_write_local_tag(s, 8, 0x3F0B);
1877  avio_wb32(pb, mxf->time_base.den);
1878  avio_wb32(pb, mxf->time_base.num);
1879 
1880  // index start position
1881  mxf_write_local_tag(s, 8, 0x3F0C);
1883 
1884  // index duration
1885  mxf_write_local_tag(s, 8, 0x3F0D);
1886  if (mxf->edit_unit_byte_count)
1887  avio_wb64(pb, 0); // index table covers whole container
1888  else
1889  avio_wb64(pb, mxf->edit_units_count);
1890 
1891  // edit unit byte count
1892  mxf_write_local_tag(s, 4, 0x3F05);
1893  avio_wb32(pb, mxf->edit_unit_byte_count);
1894 
1895  // index sid
1896  mxf_write_local_tag(s, 4, 0x3F06);
1897  avio_wb32(pb, 2);
1898 
1899  // body sid
1900  mxf_write_local_tag(s, 4, 0x3F07);
1901  avio_wb32(pb, 1);
1902 
1903  // real slice count - 1
1904  mxf_write_local_tag(s, 1, 0x3F08);
1905  avio_w8(pb, !mxf->edit_unit_byte_count); // only one slice for CBR
1906 
1907  // delta entry array
1908  mxf_write_local_tag(s, 8 + (s->nb_streams+1)*6, 0x3F09);
1909  avio_wb32(pb, s->nb_streams+1); // num of entries
1910  avio_wb32(pb, 6); // size of one entry
1911  // write system item delta entry
1912  avio_w8(pb, 0);
1913  avio_w8(pb, 0); // slice entry
1914  avio_wb32(pb, 0); // element delta
1915  // write each stream delta entry
1916  for (i = 0; i < s->nb_streams; i++) {
1917  AVStream *st = s->streams[i];
1918  MXFStreamContext *sc = st->priv_data;
1919  avio_w8(pb, sc->temporal_reordering);
1920  if (sc->temporal_reordering)
1921  temporal_reordering = 1;
1922  if (mxf->edit_unit_byte_count) {
1923  avio_w8(pb, 0); // slice number
1924  avio_wb32(pb, sc->slice_offset);
1925  } else if (i == 0) { // video track
1926  avio_w8(pb, 0); // slice number
1927  avio_wb32(pb, KAG_SIZE); // system item size including klv fill
1928  } else { // audio or data track
1929  if (!audio_frame_size) {
1930  audio_frame_size = sc->frame_size;
1931  audio_frame_size += klv_fill_size(audio_frame_size);
1932  }
1933  avio_w8(pb, 1);
1934  avio_wb32(pb, (i-1)*audio_frame_size); // element delta
1935  }
1936  }
1937 
1938  if (!mxf->edit_unit_byte_count) {
1939  MXFStreamContext *sc = s->streams[0]->priv_data;
1940  mxf_write_local_tag(s, 8 + mxf->edit_units_count*15, 0x3F0A);
1941  avio_wb32(pb, mxf->edit_units_count); // num of entries
1942  avio_wb32(pb, 15); // size of one entry
1943 
1944  for (i = 0; i < mxf->edit_units_count; i++) {
1945  int temporal_offset = 0;
1946 
1947  if (!(mxf->index_entries[i].flags & 0x33)) { // I-frame
1948  sc->max_gop = FFMAX(sc->max_gop, i - mxf->last_key_index);
1949  mxf->last_key_index = key_index;
1950  key_index = i;
1951  }
1952 
1953  if (temporal_reordering) {
1954  int pic_num_in_gop = i - key_index;
1955  if (pic_num_in_gop != mxf->index_entries[i].temporal_ref) {
1956  for (j = key_index; j < mxf->edit_units_count; j++) {
1957  if (pic_num_in_gop == mxf->index_entries[j].temporal_ref)
1958  break;
1959  }
1960  if (j == mxf->edit_units_count)
1961  av_log(s, AV_LOG_WARNING, "missing frames\n");
1962  temporal_offset = j - key_index - pic_num_in_gop;
1963  }
1964  }
1965  avio_w8(pb, temporal_offset);
1966 
1967  if ((mxf->index_entries[i].flags & 0x30) == 0x30) { // back and forward prediction
1968  sc->b_picture_count = FFMAX(sc->b_picture_count, i - prev_non_b_picture);
1969  avio_w8(pb, mxf->last_key_index - i);
1970  } else {
1971  avio_w8(pb, key_index - i); // key frame offset
1972  if ((mxf->index_entries[i].flags & 0x20) == 0x20) // only forward
1973  mxf->last_key_index = key_index;
1974  prev_non_b_picture = i;
1975  }
1976 
1977  if (!(mxf->index_entries[i].flags & 0x33) && // I-frame
1978  mxf->index_entries[i].flags & 0x40 && !temporal_offset)
1979  mxf->index_entries[i].flags |= 0x80; // random access
1980  avio_w8(pb, mxf->index_entries[i].flags);
1981  // stream offset
1982  avio_wb64(pb, mxf->index_entries[i].offset);
1983  if (s->nb_streams > 1)
1985  else
1986  avio_wb32(pb, 0);
1987  }
1988 
1989  mxf->last_key_index = key_index - mxf->edit_units_count;
1991  mxf->edit_units_count = 0;
1992  }
1993 
1994  mxf_update_klv_size(pb, pos);
1995 }
1996 
1998 {
1999  unsigned pad = klv_fill_size(avio_tell(s->pb));
2000  if (pad) {
2001  avio_write(s->pb, klv_fill_key, 16);
2002  pad -= 16 + 4;
2003  klv_encode_ber4_length(s->pb, pad);
2004  ffio_fill(s->pb, 0, pad);
2005  av_assert1(!(avio_tell(s->pb) & (KAG_SIZE-1)));
2006  }
2007 }
2008 
2009 static int mxf_write_partition(AVFormatContext *s, int bodysid,
2010  int indexsid,
2011  const uint8_t *key, int write_metadata)
2012 {
2013  MXFContext *mxf = s->priv_data;
2014  AVIOContext *pb = s->pb;
2015  int64_t header_byte_count_offset;
2016  unsigned index_byte_count = 0;
2017  uint64_t partition_offset = avio_tell(pb);
2018  int err;
2019 
2020  if (!mxf->edit_unit_byte_count && mxf->edit_units_count)
2021  index_byte_count = 85 + 12+(s->nb_streams+1)*6 +
2022  12+mxf->edit_units_count*15;
2023  else if (mxf->edit_unit_byte_count && indexsid)
2024  index_byte_count = 80;
2025 
2026  if (index_byte_count) {
2027  index_byte_count += 16 + 4; // add encoded ber4 length
2028  index_byte_count += klv_fill_size(index_byte_count);
2029  }
2030 
2031  if (key && !memcmp(key, body_partition_key, 16)) {
2033  sizeof(*mxf->body_partition_offset))) < 0) {
2034  mxf->body_partitions_count = 0;
2035  return err;
2036  }
2037  mxf->body_partition_offset[mxf->body_partitions_count++] = partition_offset;
2038  }
2039 
2040  // write klv
2041  if (key)
2042  avio_write(pb, key, 16);
2043  else
2044  avio_write(pb, body_partition_key, 16);
2045 
2047 
2048  // write partition value
2049  avio_wb16(pb, 1); // majorVersion
2050  avio_wb16(pb, 3); // minorVersion
2051  avio_wb32(pb, KAG_SIZE); // KAGSize
2052 
2053  avio_wb64(pb, partition_offset); // ThisPartition
2054 
2055  if (key && !memcmp(key, body_partition_key, 16) && mxf->body_partitions_count > 1)
2056  avio_wb64(pb, mxf->body_partition_offset[mxf->body_partitions_count-2]); // PreviousPartition
2057  else if (key && !memcmp(key, footer_partition_key, 16) && mxf->body_partitions_count)
2058  avio_wb64(pb, mxf->body_partition_offset[mxf->body_partitions_count-1]); // PreviousPartition
2059  else
2060  avio_wb64(pb, 0);
2061 
2062  avio_wb64(pb, mxf->footer_partition_offset); // footerPartition
2063 
2064  // set offset
2065  header_byte_count_offset = avio_tell(pb);
2066  avio_wb64(pb, 0); // headerByteCount, update later
2067 
2068  // indexTable
2069  avio_wb64(pb, index_byte_count); // indexByteCount
2070  avio_wb32(pb, index_byte_count ? indexsid : 0); // indexSID
2071 
2072  // BodyOffset
2073  if (bodysid && mxf->edit_units_count && mxf->body_partitions_count && !IS_OPATOM(s))
2074  avio_wb64(pb, mxf->body_offset);
2075  else
2076  avio_wb64(pb, 0);
2077 
2078  avio_wb32(pb, bodysid); // bodySID
2079 
2080  // operational pattern
2081  if (IS_OPATOM(s))
2082  avio_write(pb, opatom_ul, 16);
2083  else
2084  avio_write(pb, op1a_ul, 16);
2085 
2086  // essence container
2088 
2089  if (write_metadata) {
2090  // mark the start of the headermetadata and calculate metadata size
2091  int64_t pos, start;
2092  unsigned header_byte_count;
2093 
2095  start = avio_tell(s->pb);
2099  pos = avio_tell(s->pb);
2100  header_byte_count = pos - start + klv_fill_size(pos);
2101 
2102  // update header_byte_count
2103  avio_seek(pb, header_byte_count_offset, SEEK_SET);
2104  avio_wb64(pb, header_byte_count);
2105  avio_seek(pb, pos, SEEK_SET);
2106  }
2107 
2108  if(key)
2110 
2111  return 0;
2112 }
2113 
2114 static const struct {
2115  int profile;
2117 } mxf_prores_codec_uls[] = {
2118  { AV_PROFILE_PRORES_PROXY, { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0d,0x04,0x01,0x02,0x02,0x03,0x06,0x01,0x00 } },
2119  { AV_PROFILE_PRORES_LT, { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0d,0x04,0x01,0x02,0x02,0x03,0x06,0x02,0x00 } },
2120  { AV_PROFILE_PRORES_STANDARD, { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0d,0x04,0x01,0x02,0x02,0x03,0x06,0x03,0x00 } },
2121  { AV_PROFILE_PRORES_HQ, { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0d,0x04,0x01,0x02,0x02,0x03,0x06,0x04,0x00 } },
2122  { AV_PROFILE_PRORES_4444, { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0d,0x04,0x01,0x02,0x02,0x03,0x06,0x05,0x00 } },
2123  { AV_PROFILE_PRORES_XQ, { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0d,0x04,0x01,0x02,0x02,0x03,0x06,0x06,0x00 } },
2124 };
2125 
2127 {
2128  MXFContext *mxf = s->priv_data;
2129  MXFStreamContext *sc = st->priv_data;
2130  int i, profile;
2131 
2132  if (mxf->header_written)
2133  return 1;
2134 
2135  profile = st->codecpar->profile;
2136  for (i = 0; i < FF_ARRAY_ELEMS(mxf_prores_codec_uls); i++) {
2138  sc->codec_ul = &mxf_prores_codec_uls[i].codec_ul;
2139  break;
2140  }
2141  }
2143  return 0;
2144 
2145  sc->frame_size = pkt->size;
2146 
2147  return 1;
2148 }
2149 
2150 static const struct {
2151  uint16_t cid;
2152  uint8_t interlaced;
2153  UID codec_ul;
2154 } mxf_dnxhd_codec_uls[] = {
2155  { 1235, 0, { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x71,0x01,0x00,0x00 } }, // 1080p 10bit HIGH
2156  { 1237, 0, { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x71,0x03,0x00,0x00 } }, // 1080p 8bit MED
2157  { 1238, 0, { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x71,0x04,0x00,0x00 } }, // 1080p 8bit HIGH
2158  { 1241, 1, { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x71,0x07,0x00,0x00 } }, // 1080i 10bit HIGH
2159  { 1242, 1, { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x71,0x08,0x00,0x00 } }, // 1080i 8bit MED
2160  { 1243, 1, { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x71,0x09,0x00,0x00 } }, // 1080i 8bit HIGH
2161  { 1244, 1, { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x71,0x0a,0x00,0x00 } }, // 1080i 8bit TR
2162  { 1250, 0, { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x71,0x10,0x00,0x00 } }, // 720p 10bit
2163  { 1251, 0, { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x71,0x11,0x00,0x00 } }, // 720p 8bit HIGH
2164  { 1252, 0, { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x71,0x12,0x00,0x00 } }, // 720p 8bit MED
2165  { 1253, 0, { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x71,0x13,0x00,0x00 } }, // 720p 8bit LOW
2166  { 1256, 0, { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x71,0x16,0x00,0x00 } }, // 1080p 10bit 444
2167  { 1258, 0, { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x71,0x18,0x00,0x00 } }, // 720p 8bit TR
2168  { 1259, 0, { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x71,0x19,0x00,0x00 } }, // 1080p 8bit TR
2169  { 1260, 1, { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x71,0x1a,0x00,0x00 } }, // 1080i 8bit TR MBAFF
2170  { 1270, 0, { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x71,0x24,0x00,0x00 } }, // DNXHR 444
2171  { 1271, 0, { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x71,0x25,0x00,0x00 } }, // DNXHR HQX
2172  { 1272, 0, { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x71,0x26,0x00,0x00 } }, // DNXHR HQ
2173  { 1273, 0, { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x71,0x27,0x00,0x00 } }, // DNXHR SQ
2174  { 1274, 0, { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x71,0x28,0x00,0x00 } }, // DNXHR LB
2175 };
2176 
2178 {
2179  MXFContext *mxf = s->priv_data;
2180  MXFStreamContext *sc = st->priv_data;
2181  int i, cid;
2182 
2183  if (mxf->header_written)
2184  return 1;
2185 
2186  if (pkt->size < 43)
2187  return 0;
2188 
2189  cid = AV_RB32(pkt->data + 0x28);
2190  for (i = 0; i < FF_ARRAY_ELEMS(mxf_dnxhd_codec_uls); i++) {
2191  if (cid == mxf_dnxhd_codec_uls[i].cid) {
2192  sc->codec_ul = &mxf_dnxhd_codec_uls[i].codec_ul;
2193  sc->interlaced = mxf_dnxhd_codec_uls[i].interlaced;
2194  break;
2195  }
2196  }
2198  return 0;
2199 
2200  sc->component_depth = 0;
2201  switch (pkt->data[0x21] >> 5) {
2202  case 1: sc->component_depth = 8; break;
2203  case 2: sc->component_depth = 10; break;
2204  case 3: sc->component_depth = 12; break;
2205  }
2206  if (!sc->component_depth)
2207  return 0;
2208 
2209  if (cid >= 1270) { // RI raster
2211  st->codecpar->width, st->codecpar->height,
2212  INT_MAX);
2213  } else {
2214  sc->aspect_ratio = (AVRational){ 16, 9 };
2215  }
2216 
2217  sc->frame_size = pkt->size;
2218 
2219  return 1;
2220 }
2221 
2222 static const struct {
2224  const UID codec_ul;
2225 } mxf_dv_uls[] = {
2226  { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x02,0x01,0x01 }, // IEC DV25 525/60
2227  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x02,0x01,0x01,0x00 } },
2228  { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x02,0x02,0x01 }, // IEC DV25 626/50
2229  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x02,0x01,0x02,0x00 } },
2230  { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x02,0x40,0x01 }, // DV25 525/60
2231  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x02,0x02,0x01,0x00 }, },
2232  { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x02,0x41,0x01 }, // DV25 625/50
2233  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x02,0x02,0x02,0x00 }, },
2234  { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x02,0x50,0x01 }, // DV50 525/60
2235  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x02,0x02,0x03,0x00 }, },
2236  { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x02,0x51,0x01 }, // DV50 625/50
2237  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x02,0x02,0x04,0x00 }, },
2238  { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x02,0x60,0x01 }, // DV100 1080/60
2239  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x02,0x02,0x05,0x00 }, },
2240  { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x02,0x61,0x01 }, // DV100 1080/50
2241  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x02,0x02,0x06,0x00 }, },
2242  { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x02,0x62,0x01 }, // DV100 720/60
2243  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x02,0x02,0x07,0x00 }, },
2244  { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x02,0x63,0x01 }, // DV100 720/50
2245  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x02,0x02,0x08,0x00 }, },
2246 };
2247 
2249 {
2250  MXFContext *mxf = s->priv_data;
2251  MXFStreamContext *sc = st->priv_data;
2252  const uint8_t *vs_pack, *vsc_pack;
2253  int apt, ul_index, stype, pal;
2254 
2255  if (mxf->header_written)
2256  return 1;
2257 
2258  // Check for minimal frame size
2259  if (pkt->size < 120000)
2260  return -1;
2261 
2262  apt = pkt->data[4] & 0x7;
2263  vs_pack = pkt->data + 80*5 + 48;
2264  vsc_pack = pkt->data + 80*5 + 53;
2265  stype = vs_pack[3] & 0x1f;
2266  pal = (vs_pack[3] >> 5) & 0x1;
2267 
2268  if ((vsc_pack[2] & 0x07) == 0x02) {
2269  sc->aspect_ratio = (AVRational){ 16, 9 };
2270  } else {
2271  sc->aspect_ratio = (AVRational){ 4, 3 };
2272  }
2273 
2274  sc->interlaced = (vsc_pack[3] >> 4) & 0x01;
2275  // TODO: fix dv encoder to set proper FF/FS value in VSC pack
2276  // and set field dominance accordingly
2277  // av_log(s, AV_LOG_DEBUG, "DV vsc pack ff/ss = %x\n", vsc_pack[2] >> 6);
2278 
2279  switch (stype) {
2280  case 0x18: // DV100 720p
2281  ul_index = 8+pal;
2282  if (sc->interlaced) {
2283  av_log(s, AV_LOG_ERROR, "source marked as interlaced but codec profile is progressive\n");
2284  sc->interlaced = 0;
2285  }
2286  break;
2287  case 0x14: // DV100 1080i
2288  ul_index = 6+pal;
2289  break;
2290  case 0x04: // DV50
2291  ul_index = 4+pal;
2292  break;
2293  default: // DV25
2294  if (!apt) { // IEC
2295  ul_index = 0+pal;
2296  } else {
2297  ul_index = 2+pal;
2298  }
2299  }
2300 
2301  sc->container_ul = &mxf_dv_uls[ul_index].container_ul;
2302  sc->codec_ul = &mxf_dv_uls[ul_index].codec_ul;
2303 
2304  sc->frame_size = pkt->size;
2305 
2306  return 1;
2307 }
2308 
2309 static const struct {
2312  uint8_t profile;
2313  uint8_t interlaced;
2314  int8_t intra_only; // 1 or 0 when there are separate UIDs for Long GOP and Intra, -1 when Intra/LGOP detection can be ignored
2315 } mxf_h264_codec_uls[] = {
2316  {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x31,0x11,0x01 }, 0, 66, 0, -1 }, // AVC Baseline
2317  {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x31,0x20,0x01 }, 0, 77, 0, -1 }, // AVC Main
2318  {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x31,0x30,0x01 }, 0, 88, 0, -1 }, // AVC Extended
2319  {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x31,0x40,0x01 }, 0, 100, 0, -1 }, // AVC High
2320  {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x31,0x50,0x01 }, 0, 110, 0, 0 }, // AVC High 10
2321  {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x31,0x60,0x01 }, 0, 122, 0, 0 }, // AVC High 422
2322  {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x31,0x70,0x01 }, 0, 244, 0, 0 }, // AVC High 444
2323  {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x32,0x20,0x01 }, 0, 110, 0, 1 }, // AVC High 10 Intra
2324  {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x32,0x21,0x01 }, 232960, 110, 1, 1 }, // AVC High 10 Intra RP2027 Class 50 1080/59.94i
2325  {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x32,0x21,0x02 }, 281088, 110, 1, 1 }, // AVC High 10 Intra RP2027 Class 50 1080/50i
2326  {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x32,0x21,0x03 }, 232960, 110, 0, 1 }, // AVC High 10 Intra RP2027 Class 50 1080/29.97p
2327  {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x32,0x21,0x04 }, 281088, 110, 0, 1 }, // AVC High 10 Intra RP2027 Class 50 1080/25p
2328  {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x32,0x21,0x08 }, 116736, 110, 0, 1 }, // AVC High 10 Intra RP2027 Class 50 720/59.94p
2329  {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x32,0x21,0x09 }, 140800, 110, 0, 1 }, // AVC High 10 Intra RP2027 Class 50 720/50p
2330  {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x32,0x30,0x01 }, 0, 122, 0, 1 }, // AVC High 422 Intra
2331  {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x32,0x31,0x01 }, 472576, 122, 1, 1 }, // AVC High 422 Intra RP2027 Class 100 1080/59.94i
2332  {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x32,0x31,0x02 }, 568832, 122, 1, 1 }, // AVC High 422 Intra RP2027 Class 100 1080/50i
2333  {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x32,0x31,0x03 }, 472576, 122, 0, 1 }, // AVC High 422 Intra RP2027 Class 100 1080/29.97p
2334  {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x32,0x31,0x04 }, 568832, 122, 0, 1 }, // AVC High 422 Intra RP2027 Class 100 1080/25p
2335  {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x32,0x31,0x08 }, 236544, 122, 0, 1 }, // AVC High 422 Intra RP2027 Class 100 720/59.94p
2336  {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x32,0x31,0x09 }, 284672, 122, 0, 1 }, // AVC High 422 Intra RP2027 Class 100 720/50p
2337  {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0d,0x04,0x01,0x02,0x02,0x01,0x32,0x40,0x01 }, 0, 244, 0, 1 }, // AVC High 444 Intra
2338  {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0d,0x04,0x01,0x02,0x02,0x01,0x32,0x50,0x01 }, 0, 44, 0, -1 }, // AVC CAVLC 444
2339 };
2340 
2342  AVPacket *pkt, MXFIndexEntry *e)
2343 {
2344  MXFContext *mxf = s->priv_data;
2345  MXFStreamContext *sc = st->priv_data;
2346  H264SPS seq, *const sps = &seq;
2347  GetBitContext gb;
2348  const uint8_t *buf = pkt->data;
2349  const uint8_t *buf_end = pkt->data + pkt->size;
2350  const uint8_t *nal_end;
2351  const UID *codec_ul = NULL;
2352  uint32_t state = -1;
2353  int extra_size = 512; // support AVC Intra files without SPS/PPS header
2354  int i, frame_size, slice_type, has_sps = 0, intra_only = 0, ret;
2355 
2356  for (;;) {
2357  buf = avpriv_find_start_code(buf, buf_end, &state);
2358  if (buf >= buf_end)
2359  break;
2360 
2361  switch (state & 0x1f) {
2362  case H264_NAL_SPS:
2363  e->flags |= 0x40;
2364 
2365  if (mxf->header_written)
2366  break;
2367 
2368  nal_end = ff_avc_find_startcode(buf, buf_end);
2369  ret = ff_avc_decode_sps(sps, buf, nal_end - buf);
2370  if (ret < 0) {
2371  av_log(s, AV_LOG_ERROR, "error parsing sps\n");
2372  return 0;
2373  }
2374  has_sps = 1;
2375 
2376  sc->aspect_ratio.num = st->codecpar->width * sps->sar.num;
2377  sc->aspect_ratio.den = st->codecpar->height * sps->sar.den;
2379  sc->aspect_ratio.num, sc->aspect_ratio.den, 1024*1024);
2380  intra_only = (sps->constraint_set_flags >> 3) & 1;
2381  sc->interlaced = !sps->frame_mbs_only_flag;
2382  sc->component_depth = sps->bit_depth_luma;
2383 
2384  buf = nal_end;
2385  break;
2386  case H264_NAL_PPS:
2387  if (e->flags & 0x40) { // sequence header present
2388  e->flags |= 0x80; // random access
2389  extra_size = 0;
2390  }
2391  break;
2392  case H264_NAL_IDR_SLICE:
2393  e->flags |= 0x04; // IDR Picture
2394  buf = buf_end;
2395  break;
2396  case H264_NAL_SLICE:
2397  init_get_bits8(&gb, buf, buf_end - buf);
2398  get_ue_golomb_long(&gb); // skip first_mb_in_slice
2399  slice_type = get_ue_golomb_31(&gb);
2400  switch (slice_type % 5) {
2401  case 0:
2402  e->flags |= 0x20; // P Picture
2403  e->flags |= 0x06; // P Picture
2404  break;
2405  case 1:
2406  e->flags |= 0x30; // B Picture
2407  e->flags |= 0x03; // non-referenced B Picture
2408  break;
2409  }
2410  buf = buf_end;
2411  break;
2412  default:
2413  break;
2414  }
2415  }
2416 
2417  if (mxf->header_written)
2418  return 1;
2419 
2420  if (!has_sps)
2421  sc->interlaced = st->codecpar->field_order != AV_FIELD_PROGRESSIVE ? 1 : 0;
2422  frame_size = pkt->size + extra_size;
2423 
2424  for (i = 0; i < FF_ARRAY_ELEMS(mxf_h264_codec_uls); i++) {
2425  if (frame_size == mxf_h264_codec_uls[i].frame_size && sc->interlaced == mxf_h264_codec_uls[i].interlaced) {
2426  codec_ul = &mxf_h264_codec_uls[i].uid;
2427  sc->component_depth = 10; // AVC Intra is always 10 Bit
2428  sc->aspect_ratio = (AVRational){ 16, 9 }; // 16:9 is mandatory for broadcast HD
2429  st->codecpar->profile = mxf_h264_codec_uls[i].profile;
2430  sc->avc_intra = 1;
2431  mxf->cbr_index = 1;
2432  sc->frame_size = pkt->size;
2433  if (sc->interlaced)
2434  sc->field_dominance = 1; // top field first is mandatory for AVC Intra
2435  break;
2436  } else if (has_sps && mxf_h264_codec_uls[i].frame_size == 0 &&
2437  mxf_h264_codec_uls[i].profile == sps->profile_idc &&
2438  (mxf_h264_codec_uls[i].intra_only < 0 ||
2439  mxf_h264_codec_uls[i].intra_only == intra_only)) {
2440  codec_ul = &mxf_h264_codec_uls[i].uid;
2441  st->codecpar->profile = sps->profile_idc;
2442  st->codecpar->level = sps->level_idc;
2443  // continue to check for avc intra
2444  }
2445  }
2446 
2447  if (!codec_ul) {
2448  av_log(s, AV_LOG_ERROR, "h264 profile not supported\n");
2449  return 0;
2450  }
2451  sc->codec_ul = codec_ul;
2452 
2453  return 1;
2454 }
2455 
2456 static inline int get_ffv1_unsigned_symbol(RangeCoder *c, uint8_t *state) {
2457  if(get_rac(c, state+0))
2458  return 0;
2459  else{
2460  int i, e;
2461  unsigned a;
2462  e= 0;
2463  while(get_rac(c, state+1 + FFMIN(e,9))){ //1..10
2464  e++;
2465  if (e > 31)
2466  return AVERROR_INVALIDDATA;
2467  }
2468 
2469  a= 1;
2470  for(i=e-1; i>=0; i--){
2471  a += a + get_rac(c, state+22 + FFMIN(i,9)); //22..31
2472  }
2473 
2474  return a;
2475  }
2476 }
2477 #define FFV1_CONTEXT_SIZE 32
2479 {
2480  MXFContext *mxf = s->priv_data;
2481  MXFStreamContext *sc = st->priv_data;
2482  uint8_t state[FFV1_CONTEXT_SIZE];
2483  RangeCoder c;
2484  unsigned v;
2485 
2486  sc->frame_size = pkt->size;
2487 
2488  if (mxf->header_written)
2489  return 1;
2490 
2491  memset(state, 128, sizeof(state));
2492  if (st->codecpar->extradata) {
2494  ff_build_rac_states(&c, 0.05 * (1LL << 32), 256 - 8);
2496  av_assert0(v >= 2);
2497  if (v > 4) {
2498  return 0;
2499  }
2500  if (v > 4) {
2501  av_log(s, AV_LOG_ERROR, "unsupported ffv1 version %d\n", v);
2502  return 0;
2503  }
2505  } else {
2506  uint8_t keystate = 128;
2508  ff_build_rac_states(&c, 0.05 * (1LL << 32), 256 - 8);
2509  get_rac(&c, &keystate); // keyframe
2511  av_assert0(v < 2);
2512  }
2513  sc->codec_ul = &mxf_ffv1_codec_uls[v];
2514 
2516  sc->interlaced = 1;
2518  }
2522  sc->aspect_ratio.num, sc->aspect_ratio.den, INT_MAX);
2523 
2524  return 1;
2525 }
2526 
2527 static const UID mxf_mpeg2_codec_uls[] = {
2528  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x01,0x10,0x00 }, // MP-ML I-Frame
2529  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x01,0x11,0x00 }, // MP-ML Long GOP
2530  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x02,0x02,0x00 }, // 422P-ML I-Frame
2531  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x02,0x03,0x00 }, // 422P-ML Long GOP
2532  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x03,0x02,0x00 }, // MP-HL I-Frame
2533  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x03,0x03,0x00 }, // MP-HL Long GOP
2534  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x04,0x02,0x00 }, // 422P-HL I-Frame
2535  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x04,0x03,0x00 }, // 422P-HL Long GOP
2536  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x05,0x02,0x00 }, // MP@H-14 I-Frame
2537  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x05,0x03,0x00 }, // MP@H-14 Long GOP
2538 };
2539 
2541 {
2542  int long_gop = 1;
2543 
2544  if (par->profile == 4) { // Main
2545  if (par->level == 8) // Main
2546  return &mxf_mpeg2_codec_uls[0+long_gop];
2547  else if (par->level == 4) // High
2548  return &mxf_mpeg2_codec_uls[4+long_gop];
2549  else if (par->level == 6) // High 14
2550  return &mxf_mpeg2_codec_uls[8+long_gop];
2551  } else if (par->profile == 0) { // 422
2552  if (par->level == 5) // Main
2553  return &mxf_mpeg2_codec_uls[2+long_gop];
2554  else if (par->level == 2) // High
2555  return &mxf_mpeg2_codec_uls[6+long_gop];
2556  }
2557  return NULL;
2558 }
2559 
2561  AVPacket *pkt, MXFIndexEntry *e)
2562 {
2563  MXFStreamContext *sc = st->priv_data;
2564  uint32_t c = -1;
2565  int i;
2566 
2567  for(i = 0; i < pkt->size - 4; i++) {
2568  c = (c<<8) + pkt->data[i];
2569  if (c == 0x1b5) {
2570  if ((pkt->data[i+1] & 0xf0) == 0x10) { // seq ext
2571  st->codecpar->profile = pkt->data[i+1] & 0x07;
2572  st->codecpar->level = pkt->data[i+2] >> 4;
2573  sc->low_delay = pkt->data[i+6] >> 7;
2574  } else if (i + 5 < pkt->size && (pkt->data[i+1] & 0xf0) == 0x80) { // pict coding ext
2575  sc->interlaced = !(pkt->data[i+5] & 0x80); // progressive frame
2576  if (sc->interlaced)
2577  sc->field_dominance = 1 + !(pkt->data[i+4] & 0x80); // top field first
2578  break;
2579  }
2580  } else if (c == 0x1b8) { // gop
2581  if (pkt->data[i+4]>>6 & 0x01) { // closed
2582  if (sc->seq_closed_gop == -1)
2583  sc->seq_closed_gop = 1;
2584  sc->closed_gop = 1;
2585  if (e->flags & 0x40) // sequence header present
2586  e->flags |= 0x80; // random access
2587  } else {
2588  sc->seq_closed_gop = 0;
2589  sc->closed_gop = 0;
2590  }
2591  } else if (c == 0x1b3) { // seq
2592  e->flags |= 0x40;
2593  switch ((pkt->data[i+4]>>4) & 0xf) {
2594  case 2: sc->aspect_ratio = (AVRational){ 4, 3}; break;
2595  case 3: sc->aspect_ratio = (AVRational){ 16, 9}; break;
2596  case 4: sc->aspect_ratio = (AVRational){221,100}; break;
2597  default:
2599  st->codecpar->width, st->codecpar->height, 1024*1024);
2600  }
2601  } else if (c == 0x100) { // pic
2602  int pict_type = (pkt->data[i+2]>>3) & 0x07;
2603  e->temporal_ref = (pkt->data[i+1]<<2) | (pkt->data[i+2]>>6);
2604  if (pict_type == 2) { // P-frame
2605  e->flags |= 0x22;
2606  sc->closed_gop = 0; // reset closed GOP, don't matter anymore
2607  } else if (pict_type == 3) { // B-frame
2608  if (sc->closed_gop)
2609  e->flags |= 0x13; // only backward prediction
2610  else
2611  e->flags |= 0x33;
2612  sc->temporal_reordering = -1;
2613  } else if (!pict_type) {
2614  av_log(s, AV_LOG_ERROR, "error parsing mpeg2 frame\n");
2615  return 0;
2616  }
2617  }
2618  }
2619  if (!IS_D10(s)) {
2621  if (!codec_ul)
2622  return 0;
2623  sc->codec_ul = codec_ul;
2624  }
2625  return 1;
2626 }
2627 
2628 static uint64_t mxf_parse_timestamp(int64_t timestamp64)
2629 {
2630  time_t timestamp = timestamp64 / 1000000;
2631  struct tm tmbuf;
2632  struct tm *time = gmtime_r(&timestamp, &tmbuf);
2633  if (!time)
2634  return 0;
2635  return (uint64_t)(time->tm_year+1900) << 48 |
2636  (uint64_t)(time->tm_mon+1) << 40 |
2637  (uint64_t) time->tm_mday << 32 |
2638  time->tm_hour << 24 |
2639  time->tm_min << 16 |
2640  time->tm_sec << 8 |
2641  (timestamp64 % 1000000) / 4000;
2642 }
2643 
2645 {
2646  MXFContext *mxf = s->priv_data;
2647  uint32_t seed = av_get_random_seed();
2648  uint64_t umid = seed + 0x5294713400000000LL;
2649 
2650  AV_WB64(mxf->umid , umid);
2651  AV_WB64(mxf->umid+8, umid>>8);
2652 
2653  mxf->instance_number = seed & 0xFFFFFF;
2654 }
2655 
2657 {
2658  MXFContext *mxf = s->priv_data;
2659  AVDictionaryEntry *tcr = av_dict_get(s->metadata, "timecode", NULL, 0);
2660 
2661  if (!ff_mxf_get_content_package_rate(tbc)) {
2662  if (s->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL) {
2663  av_log(s, AV_LOG_ERROR, "Unsupported frame rate %d/%d. Set -strict option to 'unofficial' or lower in order to allow it!\n", tbc.den, tbc.num);
2664  return AVERROR(EINVAL);
2665  } else {
2666  av_log(s, AV_LOG_WARNING, "Unofficial frame rate %d/%d.\n", tbc.den, tbc.num);
2667  }
2668  }
2669 
2670  mxf->timecode_base = (tbc.den + tbc.num/2) / tbc.num;
2671  if (!tcr)
2672  tcr = av_dict_get(st->metadata, "timecode", NULL, 0);
2673 
2674  if (tcr)
2675  return av_timecode_init_from_string(&mxf->tc, av_inv_q(tbc), tcr->value, s);
2676  else
2677  return av_timecode_init(&mxf->tc, av_inv_q(tbc), 0, 0, s);
2678 }
2679 
2681 {
2682  AVCodecParameters *par = st->codecpar;
2683  const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(par->format);
2684 
2686  return par->chroma_location;
2687 
2688  if (pix_desc) {
2689  if (pix_desc->log2_chroma_h == 0) {
2690  return AVCHROMA_LOC_TOPLEFT;
2691  } else if (pix_desc->log2_chroma_w == 1 && pix_desc->log2_chroma_h == 1) {
2693  switch (par->codec_id) {
2694  case AV_CODEC_ID_MJPEG:
2696  }
2697  }
2699  switch (par->codec_id) {
2701  }
2702  }
2703  }
2704  }
2705 
2706  return AVCHROMA_LOC_UNSPECIFIED;
2707 }
2708 
2710 {
2711  MXFContext *mxf = s->priv_data;
2712  int i, ret;
2713  uint8_t present[FF_ARRAY_ELEMS(mxf_essence_container_uls)] = {0};
2714  int64_t timestamp = 0;
2715 
2716  if (IS_OPATOM(s) && s->nb_streams != 1) {
2717  av_log(s, AV_LOG_ERROR, "there must be exactly one stream for mxf opatom\n");
2718  return -1;
2719  }
2720 
2721  if (!av_dict_get(s->metadata, "comment_", NULL, AV_DICT_IGNORE_SUFFIX))
2722  mxf->store_user_comments = 0;
2723 
2724  for (i = 0; i < s->nb_streams; i++) {
2725  AVStream *st = s->streams[i];
2726  MXFStreamContext *sc = av_mallocz(sizeof(*sc));
2727  if (!sc)
2728  return AVERROR(ENOMEM);
2729  st->priv_data = sc;
2730  sc->index = -1;
2731 
2732  if (((i == 0) ^ (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)) && !IS_OPATOM(s)) {
2733  av_log(s, AV_LOG_ERROR, "there must be exactly one video stream and it must be the first one\n");
2734  return -1;
2735  }
2736 
2737  if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
2738  const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(st->codecpar->format);
2739  // TODO: should be avg_frame_rate
2740  AVRational tbc = st->time_base;
2741  // Default component depth to 8
2742  sc->component_depth = 8;
2743  sc->h_chroma_sub_sample = 2;
2744  sc->v_chroma_sub_sample = 2;
2745  sc->color_siting = 0xFF;
2746 
2749  av_make_q(st->codecpar->width, st->codecpar->height));
2750  }
2751 
2752  if (pix_desc) {
2753  sc->component_depth = pix_desc->comp[0].depth;
2754  sc->h_chroma_sub_sample = 1 << pix_desc->log2_chroma_w;
2755  sc->v_chroma_sub_sample = 1 << pix_desc->log2_chroma_h;
2756  }
2757  switch (choose_chroma_location(s, st)) {
2758  case AVCHROMA_LOC_TOPLEFT: sc->color_siting = 0; break;
2759  case AVCHROMA_LOC_LEFT: sc->color_siting = 6; break;
2760  case AVCHROMA_LOC_TOP: sc->color_siting = 1; break;
2761  case AVCHROMA_LOC_CENTER: sc->color_siting = 3; break;
2762  }
2763 
2765  mxf->time_base = tbc;
2766  avpriv_set_pts_info(st, 64, mxf->time_base.num, mxf->time_base.den);
2767  if((ret = mxf_init_timecode(s, st, tbc)) < 0)
2768  return ret;
2769 
2771  sc->seq_closed_gop = -1; // unknown yet
2772  }
2773 
2774  sc->video_bit_rate = st->codecpar->bit_rate;
2775 
2776  if (IS_D10(s) ||
2779  mxf->cbr_index = 1;
2780 
2781  if (IS_D10(s)) {
2782  int ntsc = mxf->time_base.den != 25;
2783  int ul_index;
2784 
2786  av_log(s, AV_LOG_ERROR, "error MXF D-10 only support MPEG-2 Video\n");
2787  return AVERROR(EINVAL);
2788  }
2789  if ((sc->video_bit_rate == 50000000) && (mxf->time_base.den == 25)) {
2790  ul_index = 0;
2791  } else if ((sc->video_bit_rate == 49999840 || sc->video_bit_rate == 50000000) && ntsc) {
2792  ul_index = 1;
2793  } else if (sc->video_bit_rate == 40000000) {
2794  ul_index = 2+ntsc;
2795  } else if (sc->video_bit_rate == 30000000) {
2796  ul_index = 4+ntsc;
2797  } else {
2798  av_log(s, AV_LOG_ERROR, "error MXF D-10 only support 30/40/50 mbit/s\n");
2799  return -1;
2800  }
2801 
2802  sc->codec_ul = &mxf_d10_codec_uls[ul_index];
2803  sc->container_ul = &mxf_d10_container_uls[ul_index];
2804  sc->index = INDEX_D10_VIDEO;
2805  sc->signal_standard = 1;
2806  sc->color_siting = 0;
2807  sc->frame_size = (int64_t)sc->video_bit_rate *
2808  mxf->time_base.num / (8*mxf->time_base.den);
2809  }
2810  if (mxf->signal_standard >= 0)
2811  sc->signal_standard = mxf->signal_standard;
2812  } else if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
2813  char bsf_arg[32];
2814  if (st->codecpar->sample_rate != 48000) {
2815  av_log(s, AV_LOG_ERROR, "only 48khz is implemented\n");
2816  return -1;
2817  }
2818  avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
2819  if (IS_D10(s)) {
2820  if (st->index != 1) {
2821  av_log(s, AV_LOG_ERROR, "MXF D-10 only support one audio track\n");
2822  return -1;
2823  }
2824  if (st->codecpar->codec_id != AV_CODEC_ID_PCM_S16LE &&
2826  av_log(s, AV_LOG_ERROR, "MXF D-10 only support 16 or 24 bits le audio\n");
2827  }
2828  sc->index = INDEX_D10_AUDIO;
2829  sc->container_ul = ((MXFStreamContext*)s->streams[0]->priv_data)->container_ul;
2830  sc->frame_size = 4 + 8 * av_rescale_rnd(st->codecpar->sample_rate, mxf->time_base.num, mxf->time_base.den, AV_ROUND_UP) * 4;
2831  } else if (IS_OPATOM(s)) {
2832  AVRational tbc = av_inv_q(mxf->audio_edit_rate);
2833 
2834  if (st->codecpar->codec_id != AV_CODEC_ID_PCM_S16LE &&
2836  av_log(s, AV_LOG_ERROR, "Only pcm_s16le and pcm_s24le audio codecs are implemented\n");
2837  return AVERROR_PATCHWELCOME;
2838  }
2839  if (st->codecpar->ch_layout.nb_channels != 1) {
2840  av_log(s, AV_LOG_ERROR, "MXF OPAtom only supports single channel audio\n");
2841  return AVERROR(EINVAL);
2842  }
2843 
2844  mxf->time_base = st->time_base;
2845  if((ret = mxf_init_timecode(s, st, tbc)) < 0)
2846  return ret;
2847 
2849  sc->index = INDEX_WAV;
2850  } else {
2851  mxf->slice_count = 1;
2855  }
2856  snprintf(bsf_arg, sizeof(bsf_arg), "r=%d/%d", mxf->tc.rate.num, mxf->tc.rate.den);
2857  ret = ff_stream_add_bitstream_filter(st, "pcm_rechunk", bsf_arg);
2858  if (ret < 0)
2859  return ret;
2860  } else if (st->codecpar->codec_type == AVMEDIA_TYPE_DATA) {
2861  AVDictionaryEntry *e = av_dict_get(st->metadata, "data_type", NULL, 0);
2862  if (e && !strcmp(e->value, "vbi_vanc_smpte_436M")) {
2863  sc->index = INDEX_S436M;
2864  } else {
2865  av_log(s, AV_LOG_ERROR, "track %d: unsupported data type\n", i);
2866  return -1;
2867  }
2868  if (st->index != s->nb_streams - 1) {
2869  av_log(s, AV_LOG_ERROR, "data track must be placed last\n");
2870  return -1;
2871  }
2872  }
2873 
2874  if (sc->index == -1) {
2876  if (sc->index == -1) {
2877  av_log(s, AV_LOG_ERROR, "track %d: could not find essence container ul, "
2878  "codec not currently supported in container\n", i);
2879  return -1;
2880  }
2881  }
2882 
2883  if (!sc->codec_ul)
2885  if (!sc->container_ul)
2887 
2889  sc->track_essence_element_key[15] = present[sc->index];
2890  if (IS_OPATOM(s) && st->codecpar->codec_id == AV_CODEC_ID_DNXHD) {
2891  // clip-wrapping requires 0x0D per ST2019-4:2009 or 0x06 per previous version ST2019-4:2008
2892  // we choose to use 0x06 instead 0x0D to be compatible with AVID systems
2893  // and produce mxf files with the most relevant flavour for opatom
2894  sc->track_essence_element_key[14] = 0x06;
2895  }
2896  PRINT_KEY(s, "track essence element key", sc->track_essence_element_key);
2897 
2898  if (!present[sc->index])
2899  mxf->essence_container_count++;
2900  present[sc->index]++;
2901  }
2902 
2903  if (IS_D10(s) || IS_OPATOM(s)) {
2904  mxf->essence_container_count = 1;
2905  }
2906 
2907  if (!(s->flags & AVFMT_FLAG_BITEXACT))
2908  mxf_gen_umid(s);
2909 
2910  for (i = 0; i < s->nb_streams; i++) {
2911  MXFStreamContext *sc = s->streams[i]->priv_data;
2912  // update element count
2913  sc->track_essence_element_key[13] = present[sc->index];
2915  sc->order = (0x15 << 24) | AV_RB32(sc->track_essence_element_key+13);
2916  else
2917  sc->order = AV_RB32(sc->track_essence_element_key+12);
2918  }
2919 
2920  if (ff_parse_creation_time_metadata(s, &timestamp, 0) > 0)
2921  mxf->timestamp = mxf_parse_timestamp(timestamp);
2922  mxf->duration = -1;
2923 
2924  mxf->timecode_track = av_mallocz(sizeof(*mxf->timecode_track));
2925  if (!mxf->timecode_track)
2926  return AVERROR(ENOMEM);
2928  mxf->timecode_track->index = -1;
2929 
2930  return 0;
2931 }
2932 
2933 static const uint8_t system_metadata_pack_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0D,0x01,0x03,0x01,0x04,0x01,0x01,0x00 };
2934 static const uint8_t system_metadata_package_set_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x43,0x01,0x01,0x0D,0x01,0x03,0x01,0x04,0x01,0x02,0x01 };
2935 
2937 {
2938  MXFContext *mxf = s->priv_data;
2939  AVIOContext *pb = s->pb;
2940  unsigned frame;
2941  uint32_t time_code;
2942  int i, system_item_bitmap = 0x58; // UL, user date/time stamp, picture present
2943 
2945 
2946  // write system metadata pack
2948  klv_encode_ber4_length(pb, 57);
2949 
2950  for (i = 0; i < s->nb_streams; i++) {
2951  if (s->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_AUDIO)
2952  system_item_bitmap |= 0x4;
2953  else if (s->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_DATA)
2954  system_item_bitmap |= 0x2;
2955  }
2956  avio_w8(pb, system_item_bitmap);
2957  avio_w8(pb, mxf->content_package_rate); // content package rate
2958  avio_w8(pb, 0x00); // content package type
2959  avio_wb16(pb, 0x00); // channel handle
2960  avio_wb16(pb, frame & 0xFFFF); // continuity count, supposed to overflow
2961  if (mxf->essence_container_count > 1)
2962  avio_write(pb, multiple_desc_ul, 16);
2963  else {
2964  MXFStreamContext *sc = s->streams[0]->priv_data;
2965  avio_write(pb, *sc->container_ul, 16);
2966  }
2967  avio_w8(pb, 0);
2968  avio_wb64(pb, 0);
2969  avio_wb64(pb, 0); // creation date/time stamp
2970 
2971  avio_w8(pb, 0x81); // SMPTE 12M time code
2972  time_code = av_timecode_get_smpte_from_framenum(&mxf->tc, frame);
2973  avio_wb32(pb, time_code);
2974  avio_wb32(pb, 0); // binary group data
2975  avio_wb64(pb, 0);
2976 
2977  // write system metadata package set
2979  klv_encode_ber4_length(pb, 35);
2980  avio_w8(pb, 0x83); // UMID
2981  avio_wb16(pb, 0x20);
2982  mxf_write_umid(s, 1);
2983 }
2984 
2986 {
2987  MXFContext *mxf = s->priv_data;
2988  AVIOContext *pb = s->pb;
2989  int frame_size = pkt->size / st->codecpar->block_align;
2990  const uint8_t *samples = pkt->data;
2991  const uint8_t *const end = pkt->data + pkt->size;
2992  int i;
2993 
2994  klv_encode_ber4_length(pb, 4 + frame_size*4*8);
2995 
2996  avio_w8(pb, (frame_size == 1920 ? 0 : (mxf->edit_units_count-1) % 5 + 1));
2997  avio_wl16(pb, frame_size);
2998  avio_w8(pb, (1 << st->codecpar->ch_layout.nb_channels)-1);
2999 
3000  while (samples < end) {
3001  for (i = 0; i < st->codecpar->ch_layout.nb_channels; i++) {
3002  uint32_t sample;
3003  if (st->codecpar->codec_id == AV_CODEC_ID_PCM_S24LE) {
3004  sample = AV_RL24(samples)<< 4;
3005  samples += 3;
3006  } else {
3007  sample = AV_RL16(samples)<<12;
3008  samples += 2;
3009  }
3010  avio_wl32(pb, sample | i);
3011  }
3012  for (; i < 8; i++)
3013  avio_wl32(pb, i);
3014  }
3015 }
3016 
3018 {
3019  MXFContext *mxf = s->priv_data;
3020  AVIOContext *pb = s->pb;
3021  AVStream *st = s->streams[0];
3022  MXFStreamContext *sc = st->priv_data;
3023  const uint8_t *key = NULL;
3024 
3025  int err;
3026 
3027  if (!mxf->header_written)
3029 
3030  if ((err = mxf_write_partition(s, 1, 0, key, 0)) < 0)
3031  return err;
3035  return 0;
3036 }
3037 
3039 {
3040  MXFContext *mxf = s->priv_data;
3041  AVIOContext *pb = s->pb;
3042 
3043  int err;
3044 
3045  if (!mxf->header_written) {
3046  if ((err = mxf_write_partition(s, 0, 0, header_open_partition_key, 1)) < 0)
3047  return err;
3049 
3050  if ((err = mxf_write_opatom_body_partition(s)) < 0)
3051  return err;
3052  mxf->header_written = 1;
3053  }
3054 
3055  if (!mxf->edit_unit_byte_count) {
3057  mxf->index_entries[mxf->edit_units_count].flags = ie->flags;
3059  }
3060  mxf->edit_units_count++;
3061  avio_write(pb, pkt->data, pkt->size);
3062  mxf->body_offset += pkt->size;
3063 
3064  return 0;
3065 }
3066 
3068 {
3069  MXFContext *mxf = s->priv_data;
3070  int i;
3071 
3072  if (IS_OPATOM(s)) {
3073  MXFStreamContext *sc = s->streams[0]->priv_data;
3074  mxf->edit_unit_byte_count = sc->frame_size;
3075  return;
3076  }
3077 
3078  mxf->edit_unit_byte_count = KAG_SIZE; // system element
3079  for (i = 0; i < s->nb_streams; i++) {
3080  AVStream *st = s->streams[i];
3081  MXFStreamContext *sc = st->priv_data;
3083  mxf->edit_unit_byte_count += 16 + 4 + sc->frame_size;
3085  }
3086 }
3087 
3089 {
3090  MXFContext *mxf = s->priv_data;
3091  AVIOContext *pb = s->pb;
3092  AVStream *st = s->streams[pkt->stream_index];
3093  MXFStreamContext *sc = st->priv_data;
3094  MXFIndexEntry ie = {0};
3095  int err;
3096 
3097  if (!mxf->header_written && pkt->stream_index != 0 &&
3098  !IS_OPATOM(s)) {
3099  av_log(s, AV_LOG_ERROR, "Received non-video packet before "
3100  "header has been written\n");
3101  return AVERROR_INVALIDDATA;
3102  }
3103 
3104  if (!mxf->cbr_index && !mxf->edit_unit_byte_count && !(mxf->edit_units_count % EDIT_UNITS_PER_BODY)) {
3105  if ((err = av_reallocp_array(&mxf->index_entries, mxf->edit_units_count
3106  + EDIT_UNITS_PER_BODY, sizeof(*mxf->index_entries))) < 0) {
3107  mxf->edit_units_count = 0;
3108  av_log(s, AV_LOG_ERROR, "could not allocate index entries\n");
3109  return err;
3110  }
3111  }
3112 
3114  if (!mxf_parse_mpeg2_frame(s, st, pkt, &ie)) {
3115  av_log(s, AV_LOG_ERROR, "could not get mpeg2 profile and level\n");
3116  return -1;
3117  }
3118  } else if (st->codecpar->codec_id == AV_CODEC_ID_DNXHD) {
3119  if (!mxf_parse_dnxhd_frame(s, st, pkt)) {
3120  av_log(s, AV_LOG_ERROR, "could not get dnxhd profile\n");
3121  return -1;
3122  }
3123  } else if (st->codecpar->codec_id == AV_CODEC_ID_PRORES) {
3124  if (!mxf_parse_prores_frame(s, st, pkt)) {
3125  av_log(s, AV_LOG_ERROR, "could not get prores profile\n");
3126  return -1;
3127  }
3128  } else if (st->codecpar->codec_id == AV_CODEC_ID_DVVIDEO) {
3129  if (!mxf_parse_dv_frame(s, st, pkt)) {
3130  av_log(s, AV_LOG_ERROR, "could not get dv profile\n");
3131  return -1;
3132  }
3133  } else if (st->codecpar->codec_id == AV_CODEC_ID_H264) {
3134  if (!mxf_parse_h264_frame(s, st, pkt, &ie)) {
3135  av_log(s, AV_LOG_ERROR, "could not get h264 profile\n");
3136  return -1;
3137  }
3138  } else if (st->codecpar->codec_id == AV_CODEC_ID_FFV1) {
3139  if (!mxf_parse_ffv1_frame(s, st, pkt)) {
3140  av_log(s, AV_LOG_ERROR, "could not get ffv1 version\n");
3141  return -1;
3142  }
3143  }
3144 
3145  if (mxf->cbr_index) {
3146  if (pkt->size != sc->frame_size && st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
3147  av_log(s, AV_LOG_ERROR, "track %d: frame size does not match index unit size, %d != %d\n",
3148  st->index, pkt->size, sc->frame_size);
3149  return -1;
3150  }
3151  if (!mxf->header_written)
3153  }
3154 
3155  if (IS_OPATOM(s))
3156  return mxf_write_opatom_packet(s, pkt, &ie);
3157 
3158  if (!mxf->header_written) {
3159  if (mxf->edit_unit_byte_count) {
3160  if ((err = mxf_write_partition(s, 1, 2, header_open_partition_key, 1)) < 0)
3161  return err;
3164  } else {
3165  if ((err = mxf_write_partition(s, 0, 0, header_open_partition_key, 1)) < 0)
3166  return err;
3167  }
3168  mxf->header_written = 1;
3169  }
3170 
3171  if (st->index == 0) {
3172  if (!mxf->edit_unit_byte_count &&
3174  !(ie.flags & 0x33)) { // I-frame, GOP start
3176  if ((err = mxf_write_partition(s, 1, 2, body_partition_key, 0)) < 0)
3177  return err;
3180  }
3181 
3184 
3185  if (!mxf->edit_unit_byte_count) {
3187  mxf->index_entries[mxf->edit_units_count].flags = ie.flags;
3189  mxf->body_offset += KAG_SIZE; // size of system element
3190  }
3191  mxf->edit_units_count++;
3192  } else if (!mxf->edit_unit_byte_count && st->index == 1) {
3193  if (!mxf->edit_units_count) {
3194  av_log(s, AV_LOG_ERROR, "No packets in first stream\n");
3195  return AVERROR_PATCHWELCOME;
3196  }
3198  mxf->body_offset - mxf->index_entries[mxf->edit_units_count-1].offset;
3199  }
3200 
3202  avio_write(pb, sc->track_essence_element_key, 16); // write key
3203  if (IS_D10(s) && st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
3205  } else {
3206  klv_encode_ber4_length(pb, pkt->size); // write length
3207  avio_write(pb, pkt->data, pkt->size);
3208  mxf->body_offset += 16+4+pkt->size + klv_fill_size(16+4+pkt->size);
3209  }
3210 
3211  return 0;
3212 }
3213 
3215 {
3216  MXFContext *mxf = s->priv_data;
3217  AVIOContext *pb = s->pb;
3218  uint64_t pos = avio_tell(pb);
3219  int i;
3220 
3222  klv_encode_ber_length(pb, 28 + 12LL*mxf->body_partitions_count);
3223 
3224  if (mxf->edit_unit_byte_count && !IS_OPATOM(s))
3225  avio_wb32(pb, 1); // BodySID of header partition
3226  else
3227  avio_wb32(pb, 0);
3228  avio_wb64(pb, 0); // offset of header partition
3229 
3230  for (i = 0; i < mxf->body_partitions_count; i++) {
3231  avio_wb32(pb, 1); // BodySID
3232  avio_wb64(pb, mxf->body_partition_offset[i]);
3233  }
3234 
3235  avio_wb32(pb, 0); // BodySID of footer partition
3237 
3238  avio_wb32(pb, avio_tell(pb) - pos + 4);
3239 }
3240 
3242 {
3243  MXFContext *mxf = s->priv_data;
3244  AVIOContext *pb = s->pb;
3245  int i, err;
3246 
3247  if (!mxf->header_written ||
3248  (IS_OPATOM(s) && !mxf->body_partition_offset)) {
3249  /* reason could be invalid options/not supported codec/out of memory */
3250  return AVERROR_UNKNOWN;
3251  }
3252 
3254 
3257  if (mxf->edit_unit_byte_count && !IS_OPATOM(s)) { // no need to repeat index
3258  if ((err = mxf_write_partition(s, 0, 0, footer_partition_key, 0)) < 0)
3259  return err;
3260  } else {
3261  if ((err = mxf_write_partition(s, 0, 2, footer_partition_key, 0)) < 0)
3262  return err;
3265  }
3266 
3269 
3270  if (s->pb->seekable & AVIO_SEEKABLE_NORMAL) {
3271  if (IS_OPATOM(s)) {
3272  /* rewrite body partition to update lengths */
3273  avio_seek(pb, mxf->body_partition_offset[0], SEEK_SET);
3274  if ((err = mxf_write_opatom_body_partition(s)) < 0)
3275  return err;
3276  }
3277 
3278  avio_seek(pb, 0, SEEK_SET);
3279  if (mxf->edit_unit_byte_count && !IS_OPATOM(s)) {
3280  if ((err = mxf_write_partition(s, 1, 2, header_closed_partition_key, 1)) < 0)
3281  return err;
3284  } else {
3285  if ((err = mxf_write_partition(s, 0, 0, header_closed_partition_key, 1)) < 0)
3286  return err;
3287  }
3288  // update footer partition offset
3289  for (i = 0; i < mxf->body_partitions_count; i++) {
3290  avio_seek(pb, mxf->body_partition_offset[i]+44, SEEK_SET);
3292  }
3293  }
3294 
3295  return 0;
3296 }
3297 
3299 {
3300  MXFContext *mxf = s->priv_data;
3301 
3302  av_freep(&mxf->index_entries);
3304  av_freep(&mxf->timecode_track);
3305 }
3306 
3308 {
3309  FFFormatContext *const si = ffformatcontext(s);
3310  int i, stream_count = 0;
3311 
3312  for (i = 0; i < s->nb_streams; i++)
3313  stream_count += !!ffstream(s->streams[i])->last_in_packet_buffer;
3314 
3315  if (stream_count && (s->nb_streams == stream_count || flush)) {
3316  PacketListEntry *pktl = si->packet_buffer.head;
3317  if (s->nb_streams != stream_count) {
3318  PacketListEntry *last = NULL;
3319  // find last packet in edit unit
3320  while (pktl) {
3321  if (!stream_count || pktl->pkt.stream_index == 0)
3322  break;
3323  // update last packet in packet buffer
3324  if (ffstream(s->streams[pktl->pkt.stream_index])->last_in_packet_buffer != pktl)
3325  ffstream(s->streams[pktl->pkt.stream_index])->last_in_packet_buffer = pktl;
3326  last = pktl;
3327  pktl = pktl->next;
3328  stream_count--;
3329  }
3330  // purge packet queue
3331  while (pktl) {
3332  PacketListEntry *next = pktl->next;
3333  av_packet_unref(&pktl->pkt);
3334  av_freep(&pktl);
3335  pktl = next;
3336  }
3337  if (last)
3338  last->next = NULL;
3339  else {
3340  si->packet_buffer.head = NULL;
3341  si->packet_buffer.tail = NULL;
3342  goto out;
3343  }
3344  pktl = si->packet_buffer.head;
3345  }
3346 
3347  if (ffstream(s->streams[pktl->pkt.stream_index])->last_in_packet_buffer == pktl)
3348  ffstream(s->streams[pktl->pkt.stream_index])->last_in_packet_buffer = NULL;
3350  av_log(s, AV_LOG_TRACE, "out st:%d dts:%"PRId64"\n", out->stream_index, out->dts);
3351  return 1;
3352  } else {
3353  out:
3354  return 0;
3355  }
3356 }
3357 
3359  const AVPacket *pkt)
3360 {
3361  MXFStreamContext *sc = s->streams[pkt ->stream_index]->priv_data;
3362  MXFStreamContext *sc2 = s->streams[next->stream_index]->priv_data;
3363 
3364  return next->dts > pkt->dts ||
3365  (next->dts == pkt->dts && sc->order < sc2->order);
3366 }
3367 
3369  int flush, int has_packet)
3370 {
3371  int ret;
3372  if (has_packet) {
3373  MXFStreamContext *sc = s->streams[pkt->stream_index]->priv_data;
3374  pkt->pts = pkt->dts = sc->pkt_cnt++;
3376  return ret;
3377  }
3379 }
3380 
3381 #define MXF_COMMON_OPTIONS \
3382  { "signal_standard", "Force/set Signal Standard",\
3383  offsetof(MXFContext, signal_standard), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 7, AV_OPT_FLAG_ENCODING_PARAM, "signal_standard"},\
3384  { "bt601", "ITU-R BT.601 and BT.656, also SMPTE 125M (525 and 625 line interlaced)",\
3385  0, AV_OPT_TYPE_CONST, {.i64 = 1}, -1, 7, AV_OPT_FLAG_ENCODING_PARAM, "signal_standard"},\
3386  { "bt1358", "ITU-R BT.1358 and ITU-R BT.799-3, also SMPTE 293M (525 and 625 line progressive)",\
3387  0, AV_OPT_TYPE_CONST, {.i64 = 2}, -1, 7, AV_OPT_FLAG_ENCODING_PARAM, "signal_standard"},\
3388  { "smpte347m", "SMPTE 347M (540 Mbps mappings)",\
3389  0, AV_OPT_TYPE_CONST, {.i64 = 3}, -1, 7, AV_OPT_FLAG_ENCODING_PARAM, "signal_standard"},\
3390  { "smpte274m", "SMPTE 274M (1125 line)",\
3391  0, AV_OPT_TYPE_CONST, {.i64 = 4}, -1, 7, AV_OPT_FLAG_ENCODING_PARAM, "signal_standard"},\
3392  { "smpte296m", "SMPTE 296M (750 line progressive)",\
3393  0, AV_OPT_TYPE_CONST, {.i64 = 5}, -1, 7, AV_OPT_FLAG_ENCODING_PARAM, "signal_standard"},\
3394  { "smpte349m", "SMPTE 349M (1485 Mbps mappings)",\
3395  0, AV_OPT_TYPE_CONST, {.i64 = 6}, -1, 7, AV_OPT_FLAG_ENCODING_PARAM, "signal_standard"},\
3396  { "smpte428", "SMPTE 428-1 DCDM",\
3397  0, AV_OPT_TYPE_CONST, {.i64 = 7}, -1, 7, AV_OPT_FLAG_ENCODING_PARAM, "signal_standard"},
3398 
3399 
3400 
3401 static const AVOption mxf_options[] = {
3403  { "store_user_comments", "",
3404  offsetof(MXFContext, store_user_comments), AV_OPT_TYPE_BOOL, {.i64 = 1}, 0, 1, AV_OPT_FLAG_ENCODING_PARAM},
3405  { NULL },
3406 };
3407 
3408 static const AVClass mxf_muxer_class = {
3409  .class_name = "MXF muxer",
3410  .item_name = av_default_item_name,
3411  .option = mxf_options,
3412  .version = LIBAVUTIL_VERSION_INT,
3413 };
3414 
3415 static const AVOption d10_options[] = {
3416  { "d10_channelcount", "Force/set channelcount in generic sound essence descriptor",
3417  offsetof(MXFContext, channel_count), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 8, AV_OPT_FLAG_ENCODING_PARAM},
3419  { "store_user_comments", "",
3420  offsetof(MXFContext, store_user_comments), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, AV_OPT_FLAG_ENCODING_PARAM},
3421  { NULL },
3422 };
3423 
3425  .class_name = "MXF-D10 muxer",
3426  .item_name = av_default_item_name,
3427  .option = d10_options,
3428  .version = LIBAVUTIL_VERSION_INT,
3429 };
3430 
3431 static const AVOption opatom_options[] = {
3432  { "mxf_audio_edit_rate", "Audio edit rate for timecode",
3433  offsetof(MXFContext, audio_edit_rate), AV_OPT_TYPE_RATIONAL, {.dbl=25}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
3435  { "store_user_comments", "",
3436  offsetof(MXFContext, store_user_comments), AV_OPT_TYPE_BOOL, {.i64 = 1}, 0, 1, AV_OPT_FLAG_ENCODING_PARAM},
3437  { NULL },
3438 };
3439 
3441  .class_name = "MXF-OPAtom muxer",
3442  .item_name = av_default_item_name,
3443  .option = opatom_options,
3444  .version = LIBAVUTIL_VERSION_INT,
3445 };
3446 
3448  .p.name = "mxf",
3449  .p.long_name = NULL_IF_CONFIG_SMALL("MXF (Material eXchange Format)"),
3450  .p.mime_type = "application/mxf",
3451  .p.extensions = "mxf",
3452  .priv_data_size = sizeof(MXFContext),
3453  .p.audio_codec = AV_CODEC_ID_PCM_S16LE,
3454  .p.video_codec = AV_CODEC_ID_MPEG2VIDEO,
3455  .init = mxf_init,
3456  .write_packet = mxf_write_packet,
3457  .write_trailer = mxf_write_footer,
3458  .deinit = mxf_deinit,
3459  .p.flags = AVFMT_NOTIMESTAMPS,
3460  .interleave_packet = mxf_interleave,
3461  .p.priv_class = &mxf_muxer_class,
3462 };
3463 
3465  .p.name = "mxf_d10",
3466  .p.long_name = NULL_IF_CONFIG_SMALL("MXF (Material eXchange Format) D-10 Mapping"),
3467  .p.mime_type = "application/mxf",
3468  .priv_data_size = sizeof(MXFContext),
3469  .p.audio_codec = AV_CODEC_ID_PCM_S16LE,
3470  .p.video_codec = AV_CODEC_ID_MPEG2VIDEO,
3471  .init = mxf_init,
3472  .write_packet = mxf_write_packet,
3473  .write_trailer = mxf_write_footer,
3474  .deinit = mxf_deinit,
3475  .p.flags = AVFMT_NOTIMESTAMPS,
3476  .interleave_packet = mxf_interleave,
3477  .p.priv_class = &mxf_d10_muxer_class,
3478 };
3479 
3481  .p.name = "mxf_opatom",
3482  .p.long_name = NULL_IF_CONFIG_SMALL("MXF (Material eXchange Format) Operational Pattern Atom"),
3483  .p.mime_type = "application/mxf",
3484  .p.extensions = "mxf",
3485  .priv_data_size = sizeof(MXFContext),
3486  .p.audio_codec = AV_CODEC_ID_PCM_S16LE,
3487  .p.video_codec = AV_CODEC_ID_DNXHD,
3488  .init = mxf_init,
3489  .write_packet = mxf_write_packet,
3490  .write_trailer = mxf_write_footer,
3491  .deinit = mxf_deinit,
3492  .p.flags = AVFMT_NOTIMESTAMPS,
3493  .interleave_packet = mxf_interleave,
3494  .p.priv_class = &mxf_opatom_muxer_class,
3495 };
AV_CODEC_ID_PCM_S16LE
@ AV_CODEC_ID_PCM_S16LE
Definition: codec_id.h:330
H264SPS
Definition: avc.h:69
AVMasteringDisplayMetadata::has_primaries
int has_primaries
Flag indicating whether the display primaries (and white point) are set.
Definition: mastering_display_metadata.h:62
MXFContext::edit_units_count
unsigned edit_units_count
Definition: mxfenc.c:427
primer_pack_key
static const uint8_t primer_pack_key[]
Definition: mxfenc.c:264
MXFStreamContext
Definition: mxfenc.c:81
IS_OPATOM
#define IS_OPATOM(s)
Definition: mxfenc.c:69
av_packet_unref
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:423
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:186
AVMasteringDisplayMetadata::max_luminance
AVRational max_luminance
Max luminance of mastering display (cd/m^2).
Definition: mastering_display_metadata.h:57
AVCodecParameters::extradata
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition: codec_par.h:69
name
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf default minimum maximum flags name is the option name
Definition: writing_filters.txt:88
PacketList::head
PacketListEntry * head
Definition: packet_internal.h:34
avpriv_packet_list_get
int avpriv_packet_list_get(PacketList *pkt_buffer, AVPacket *pkt)
Remove the oldest AVPacket in the list and return it.
Definition: avpacket.c:576
ff_mxf_color_primaries_uls
const MXFCodecUL ff_mxf_color_primaries_uls[]
Definition: mxf.c:97
AVOutputFormat::name
const char * name
Definition: avformat.h:511
d10_options
static const AVOption d10_options[]
Definition: mxfenc.c:3415
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
uid
UID uid
Definition: mxfenc.c:2310
opt.h
Track
Definition: ismindex.c:69
AVCodecParameters::codec_type
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:51
out
FILE * out
Definition: movenc.c:54
mxf_write_random_index_pack
static void mxf_write_random_index_pack(AVFormatContext *s)
Definition: mxfenc.c:3214
mxf_utf16len
static uint64_t mxf_utf16len(const char *utf8_str)
Definition: mxfenc.c:728
color
Definition: vf_paletteuse.c:511
MXFContext::tc
AVTimecode tc
timecode context
Definition: mxfenc.c:435
FF_MXF_MasteringDisplayMinimumLuminance
#define FF_MXF_MasteringDisplayMinimumLuminance
Definition: mxf.h:98
AVCodecParameters
This struct describes the properties of an encoded stream.
Definition: codec_par.h:47
mxf_prores_codec_uls
static const struct @317 mxf_prores_codec_uls[]
INDEX_H264
@ INDEX_H264
Definition: mxfenc.c:132
ffformatcontext
static av_always_inline FFFormatContext * ffformatcontext(AVFormatContext *s)
Definition: internal.h:194
klv_encode_ber_length
static int klv_encode_ber_length(AVIOContext *pb, uint64_t len)
Definition: mxfenc.c:484
AVCodecParameters::color_space
enum AVColorSpace color_space
Definition: codec_par.h:144
mxf_dv_uls
static const struct @319 mxf_dv_uls[]
AVStream::priv_data
void * priv_data
Definition: avformat.h:866
av_pix_fmt_desc_get
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2964
mxf_write_opatom_body_partition
static int mxf_write_opatom_body_partition(AVFormatContext *s)
Definition: mxfenc.c:3017
AV_FIELD_PROGRESSIVE
@ AV_FIELD_PROGRESSIVE
Definition: defs.h:200
AV_PKT_DATA_MASTERING_DISPLAY_METADATA
@ AV_PKT_DATA_MASTERING_DISPLAY_METADATA
Mastering display metadata (based on SMPTE-2086:2014).
Definition: packet.h:223
AVMasteringDisplayMetadata::display_primaries
AVRational display_primaries[3][2]
CIE 1931 xy chromaticity coords of color primaries (r, g, b order).
Definition: mastering_display_metadata.h:42
mxf_write_local_tag
static void mxf_write_local_tag(AVFormatContext *s, int size, int tag)
Definition: mxfenc.c:618
TaggedValue
@ TaggedValue
Definition: mxf.h:50
AVMasteringDisplayMetadata::has_luminance
int has_luminance
Flag indicating whether the luminance (min_ and max_) have been set.
Definition: mastering_display_metadata.h:67
MXFContext::tagged_value_count
uint32_t tagged_value_count
Definition: mxfenc.c:445
AVFMT_NOTIMESTAMPS
#define AVFMT_NOTIMESTAMPS
Format does not need / have any timestamps.
Definition: avformat.h:480
MXF_COMMON_OPTIONS
#define MXF_COMMON_OPTIONS
Definition: mxfenc.c:3381
mxf_write_d10_audio_packet
static void mxf_write_d10_audio_packet(AVFormatContext *s, AVStream *st, AVPacket *pkt)
Definition: mxfenc.c:2985
FF_MXF_MasteringDisplayPrimaries
#define FF_MXF_MasteringDisplayPrimaries
Definition: mxf.h:95
MXFPackage::type
enum MXFMetadataSetType type
Definition: mxfenc.c:118
DESCRIPTOR_COUNT
#define DESCRIPTOR_COUNT(essence_container_count)
Definition: mxfenc.c:648
pixdesc.h
AVPacketSideData
This structure stores auxiliary information for decoding, presenting, or otherwise processing the cod...
Definition: packet.h:342
AVPacket::data
uint8_t * data
Definition: packet.h:491
header_metadata_key
static const uint8_t header_metadata_key[]
partial key for header metadata
Definition: mxfenc.c:274
ContentStorage
@ ContentStorage
Definition: mxf.h:45
AVComponentDescriptor::depth
int depth
Number of bits in the component.
Definition: pixdesc.h:57
SourceClip
@ SourceClip
Definition: mxf.h:35
AVOption
AVOption.
Definition: opt.h:251
MXFPackage::instance
int instance
Definition: mxfenc.c:119
AVTimecode::flags
uint32_t flags
flags such as drop frame, +24 hours support, ...
Definition: timecode.h:43
rangecoder.h
MXFContext::body_offset
uint64_t body_offset
Definition: mxfenc.c:440
MXFStreamContext::frame_size
int frame_size
frame size in bytes
Definition: mxfenc.c:100
AV_DICT_IGNORE_SUFFIX
#define AV_DICT_IGNORE_SUFFIX
Return first entry in a dictionary whose first part corresponds to the search key,...
Definition: dict.h:75
MXFIndexEntry
Definition: mxfenc.c:74
mxf_parse_dnxhd_frame
static int mxf_parse_dnxhd_frame(AVFormatContext *s, AVStream *st, AVPacket *pkt)
Definition: mxfenc.c:2177
ff_parse_creation_time_metadata
int ff_parse_creation_time_metadata(AVFormatContext *s, int64_t *timestamp, int return_seconds)
Parse creation_time in AVFormatContext metadata if exists and warn if the parsing fails.
Definition: mux_utils.c:126
AV_LOG_VERBOSE
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:196
LIBAVFORMAT_VERSION_MICRO
#define LIBAVFORMAT_VERSION_MICRO
Definition: version.h:35
MXFStreamContext::index
int index
index in mxf_essence_container_uls table
Definition: mxfenc.c:84
AVDictionary
Definition: dict.c:34
product_uid
static const uint8_t product_uid[]
Definition: mxfenc.c:254
FFMAX
#define FFMAX(a, b)
Definition: macros.h:47
MXFStreamContext::signal_standard
int signal_standard
Definition: mxfenc.c:92
mxf_write_content_storage
static void mxf_write_content_storage(AVFormatContext *s, MXFPackage *packages, int package_count)
Definition: mxfenc.c:853
mxf_rgba_descriptor_key
static const UID mxf_rgba_descriptor_key
Definition: mxfenc.c:1134
AVERROR_UNKNOWN
#define AVERROR_UNKNOWN
Unknown error, typically from an external library.
Definition: error.h:73
AV_OPT_TYPE_RATIONAL
@ AV_OPT_TYPE_RATIONAL
Definition: opt.h:230
rescale_mastering_luma
static uint32_t rescale_mastering_luma(AVRational q)
Definition: mxfenc.c:1145
AVChannelLayout::nb_channels
int nb_channels
Number of channels in this layout.
Definition: channel_layout.h:317
ff_mxf_d10_muxer
const FFOutputFormat ff_mxf_d10_muxer
Definition: mxfenc.c:3464
mxf_muxer_class
static const AVClass mxf_muxer_class
Definition: mxfenc.c:3408
MXFContext
Definition: mxfdec.c:296
ff_mxf_color_trc_uls
const MXFCodecUL ff_mxf_color_trc_uls[]
Definition: mxf.c:112
TimecodeComponent
@ TimecodeComponent
Definition: mxf.h:36
MXFContainerEssenceEntry::codec_ul
UID codec_ul
Definition: mxfenc.c:112
INDEX_MPEG2
@ INDEX_MPEG2
Definition: mxfenc.c:124
AV_WB64
#define AV_WB64(p, v)
Definition: intreadwrite.h:431
MXFStreamContext::interlaced
int interlaced
whether picture is interlaced
Definition: mxfenc.c:88
mxf_get_essence_container_ul_index
static int mxf_get_essence_container_ul_index(enum AVCodecID id)
Definition: mxfenc.c:519
FFOutputFormat::p
AVOutputFormat p
The public AVOutputFormat.
Definition: mux.h:36
mxf_avc_subdescriptor_key
static const UID mxf_avc_subdescriptor_key
Definition: mxfenc.c:1137
AV_PROFILE_PRORES_STANDARD
#define AV_PROFILE_PRORES_STANDARD
Definition: defs.h:181
av_get_random_seed
uint32_t av_get_random_seed(void)
Get a seed to use in conjunction with random functions.
Definition: random_seed.c:167
INDEX_DNXHD
@ INDEX_DNXHD
Definition: mxfenc.c:130
MXFContainerEssenceEntry::write_desc
void(* write_desc)(AVFormatContext *, AVStream *)
Definition: mxfenc.c:113
MXF_NUM_TAGS
#define MXF_NUM_TAGS
Definition: mxfenc.c:418
mxf_ffv1_codec_uls
static const UID mxf_ffv1_codec_uls[]
Definition: mxfenc.c:246
avio_wl16
void avio_wl16(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:484
AVCodecParameters::color_primaries
enum AVColorPrimaries color_primaries
Definition: codec_par.h:142
avio_write_marker
void avio_write_marker(AVIOContext *s, int64_t time, enum AVIODataMarkerType type)
Mark the written bytestream as a specific type.
Definition: aviobuf.c:508
golomb.h
exp golomb vlc stuff
TapeDescriptor
@ TapeDescriptor
Definition: mxf.h:51
ff_mxf_get_content_package_rate
int ff_mxf_get_content_package_rate(AVRational time_base)
Definition: mxf.c:219
AV_FIELD_TT
@ AV_FIELD_TT
Top coded_first, top displayed first.
Definition: defs.h:201
intra_only
int8_t intra_only
Definition: mxfenc.c:2314
INDEX_JPEG2000
@ INDEX_JPEG2000
Definition: mxfenc.c:131
mxf.h
AV_PROFILE_PRORES_HQ
#define AV_PROFILE_PRORES_HQ
Definition: defs.h:182
avpriv_set_pts_info
void avpriv_set_pts_info(AVStream *st, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition: avformat.c:761
store_version
static void store_version(AVFormatContext *s)
Definition: mxfenc.c:783
KAG_SIZE
#define KAG_SIZE
Definition: mxfenc.c:72
PLATFORM_IDENT
#define PLATFORM_IDENT
Definition: mxfenc.c:799
smpte_12m_timecode_track_data_ul
static const uint8_t smpte_12m_timecode_track_data_ul[]
Definition: mxfenc.c:926
mxf_write_generic_sound_desc
static void mxf_write_generic_sound_desc(AVFormatContext *s, AVStream *st)
Definition: mxfenc.c:1616
ffstream
static av_always_inline FFStream * ffstream(AVStream *st)
Definition: internal.h:420
header_open_partition_key
static const uint8_t header_open_partition_key[]
Definition: mxfenc.c:266
LIBAVFORMAT_VERSION_MAJOR
#define LIBAVFORMAT_VERSION_MAJOR
Definition: version_major.h:32
FFV1_CONTEXT_SIZE
#define FFV1_CONTEXT_SIZE
Definition: mxfenc.c:2477
mxf_write_footer
static int mxf_write_footer(AVFormatContext *s)
Definition: mxfenc.c:3241
mxf_mark_tag_unused
static void mxf_mark_tag_unused(MXFContext *mxf, int tag)
Definition: mxfenc.c:541
H264_NAL_PPS
@ H264_NAL_PPS
Definition: h264.h:42
timecode.h
MXFStreamContext::track_essence_element_key
UID track_essence_element_key
Definition: mxfenc.c:83
mxf_local_tag_batch
static const MXFLocalTagPair mxf_local_tag_batch[]
SMPTE RP210 http://www.smpte-ra.org/mdd/index.html https://smpte-ra.org/sites/default/files/Labels....
Definition: mxfenc.c:281
GetBitContext
Definition: get_bits.h:108
ff_avc_decode_sps
int ff_avc_decode_sps(H264SPS *sps, const uint8_t *buf, int buf_size)
Definition: avc.c:368
MXFCodecUL::id
int id
Definition: mxf.h:106
AVTimecode::start
int start
timecode frame start (first base frame number)
Definition: timecode.h:42
gmtime_r
#define gmtime_r
Definition: time_internal.h:34
mxf_write_multi_descriptor
static void mxf_write_multi_descriptor(AVFormatContext *s)
Definition: mxfenc.c:1054
avio_tell
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:513
type
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf type
Definition: writing_filters.txt:86
MXFStreamContext::v_chroma_sub_sample
int v_chroma_sub_sample
Definition: mxfenc.c:94
AV_ROUND_UP
@ AV_ROUND_UP
Round toward +infinity.
Definition: mathematics.h:134
H264_NAL_SLICE
@ H264_NAL_SLICE
Definition: h264.h:35
mxf_ffv1_subdescriptor_key
static const UID mxf_ffv1_subdescriptor_key
Definition: mxfenc.c:1138
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
AVRational::num
int num
Numerator.
Definition: rational.h:59
AV_FIELD_TB
@ AV_FIELD_TB
Top coded first, bottom displayed first.
Definition: defs.h:203
body_partition_key
static const uint8_t body_partition_key[]
Definition: mxfenc.c:269
MXFContext::header_written
int header_written
Definition: mxfenc.c:425
mxf_write_header_metadata_sets
static int mxf_write_header_metadata_sets(AVFormatContext *s)
Definition: mxfenc.c:1793
av_get_bits_per_sample
int av_get_bits_per_sample(enum AVCodecID codec_id)
Return codec bits per sample.
Definition: utils.c:558
GET_UTF8
#define GET_UTF8(val, GET_BYTE, ERROR)
Convert a UTF-8 character (up to 4 bytes) to its 32-bit UCS-4 encoded form.
Definition: common.h:470
AVCodecParameters::color_trc
enum AVColorTransferCharacteristic color_trc
Definition: codec_par.h:143
avassert.h
mxf_write_package
static void mxf_write_package(AVFormatContext *s, MXFPackage *package)
Definition: mxfenc.c:1671
system_metadata_package_set_key
static const uint8_t system_metadata_package_set_key[]
Definition: mxfenc.c:2934
mxf_write_sequence
static void mxf_write_sequence(AVFormatContext *s, AVStream *st, MXFPackage *package)
Definition: mxfenc.c:952
AV_LOG_TRACE
#define AV_LOG_TRACE
Extremely verbose debugging, useful for libav* development.
Definition: log.h:206
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
FF_ARRAY_ELEMS
#define FF_ARRAY_ELEMS(a)
Definition: sinewin_tablegen.c:29
mxf_parse_prores_frame
static int mxf_parse_prores_frame(AVFormatContext *s, AVStream *st, AVPacket *pkt)
Definition: mxfenc.c:2126
init_get_bits8
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:545
PRINT_KEY
#define PRINT_KEY(pc, s, x)
Definition: mxf.h:153
AV_FIELD_UNKNOWN
@ AV_FIELD_UNKNOWN
Definition: defs.h:199
av_timecode_init
int av_timecode_init(AVTimecode *tc, AVRational rate, int flags, int frame_start, void *log_ctx)
Init a timecode struct with the passed parameters.
Definition: timecode.c:221
MXFStreamContext::h_chroma_sub_sample
int h_chroma_sub_sample
Definition: mxfenc.c:93
av_dict_get
AVDictionaryEntry * av_dict_get(const AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags)
Get a dictionary entry with matching key.
Definition: dict.c:62
mxf_h264_codec_uls
static const struct @320 mxf_h264_codec_uls[]
ff_mxf_data_definition_uls
const MXFCodecUL ff_mxf_data_definition_uls[]
SMPTE RP224 http://www.smpte-ra.org/mdd/index.html.
Definition: mxf.c:31
mxf_write_user_comments
static int mxf_write_user_comments(AVFormatContext *s, const AVDictionary *m)
Definition: mxfenc.c:1653
MXFStreamContext::temporal_reordering
int temporal_reordering
Definition: mxfenc.c:95
mxf_parse_h264_frame
static int mxf_parse_h264_frame(AVFormatContext *s, AVStream *st, AVPacket *pkt, MXFIndexEntry *e)
Definition: mxfenc.c:2341
mxf_write_cdci_common
static int64_t mxf_write_cdci_common(AVFormatContext *s, AVStream *st, const UID key)
Definition: mxfenc.c:1150
mxf_write_common_fields
static void mxf_write_common_fields(AVFormatContext *s, AVStream *st)
Definition: mxfenc.c:928
mxf_write_identification
static void mxf_write_identification(AVFormatContext *s)
Definition: mxfenc.c:800
MXFContext::slice_count
uint8_t slice_count
index slice count minus 1 (1 if no audio, 0 otherwise)
Definition: mxfenc.c:429
AVMasteringDisplayMetadata::white_point
AVRational white_point[2]
CIE 1931 xy chromaticity coords of white point.
Definition: mastering_display_metadata.h:47
MXFPackage::ref
struct MXFPackage * ref
Definition: mxfenc.c:120
s
#define s(width, name)
Definition: cbs_vp9.c:198
MXFMetadataSetType
MXFMetadataSetType
Definition: mxf.h:32
AVCHROMA_LOC_TOP
@ AVCHROMA_LOC_TOP
Definition: pixfmt.h:697
AV_OPT_FLAG_ENCODING_PARAM
#define AV_OPT_FLAG_ENCODING_PARAM
a generic parameter which can be set by the user for muxing or encoding
Definition: opt.h:281
MultipleDescriptor
@ MultipleDescriptor
Definition: mxf.h:39
MXFContext::timecode_base
int timecode_base
rounded time code base (25 or 30)
Definition: mxfenc.c:437
klv_encode_ber9_length
static void klv_encode_ber9_length(AVIOContext *pb, uint64_t len)
Definition: mxfenc.c:510
INDEX_S436M
@ INDEX_S436M
Definition: mxfenc.c:133
FFFormatContext::packet_buffer
PacketList packet_buffer
This buffer is only needed when packets were already buffered but not decoded, for example to get the...
Definition: internal.h:104
AVCodecParameters::sample_aspect_ratio
AVRational sample_aspect_ratio
Video only.
Definition: codec_par.h:131
choose_chroma_location
static enum AVChromaLocation choose_chroma_location(AVFormatContext *s, AVStream *st)
Definition: mxfenc.c:2680
AVDictionaryEntry::key
char * key
Definition: dict.h:90
frame_size
int frame_size
Definition: mxfenc.c:2311
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
AVCodecParameters::width
int width
Video only.
Definition: codec_par.h:121
ULIndex
ULIndex
Definition: mxfenc.c:123
AVCSubDescriptor
@ AVCSubDescriptor
Definition: mxf.h:52
mxf_parse_mpeg2_frame
static int mxf_parse_mpeg2_frame(AVFormatContext *s, AVStream *st, AVPacket *pkt, MXFIndexEntry *e)
Definition: mxfenc.c:2560
mxf_write_wav_desc
static void mxf_write_wav_desc(AVFormatContext *s, AVStream *st)
Definition: mxfenc.c:1604
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:40
mxf_write_track
static void mxf_write_track(AVFormatContext *s, AVStream *st, MXFPackage *package)
Definition: mxfenc.c:880
UID
AVUUID UID
Definition: mxf.h:30
mxf_d10_muxer_class
static const AVClass mxf_d10_muxer_class
Definition: mxfenc.c:3424
mxf_cdci_descriptor_key
static const UID mxf_cdci_descriptor_key
Definition: mxfenc.c:1133
AV_LOG_DEBUG
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:201
AVPacketSideData::data
uint8_t * data
Definition: packet.h:343
mxf_write_klv_fill
static void mxf_write_klv_fill(AVFormatContext *s)
Definition: mxfenc.c:1997
INDEX_WAV
@ INDEX_WAV
Definition: mxfenc.c:126
AV_RL16
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_RL16
Definition: bytestream.h:94
INDEX_DV
@ INDEX_DV
Definition: mxfenc.c:129
AVPixFmtDescriptor::log2_chroma_w
uint8_t log2_chroma_w
Amount to shift the luma width right to find the chroma width.
Definition: pixdesc.h:80
mxf_compare_timestamps
static int mxf_compare_timestamps(AVFormatContext *s, const AVPacket *next, const AVPacket *pkt)
Definition: mxfenc.c:3358
mxf_write_opatom_packet
static int mxf_write_opatom_packet(AVFormatContext *s, AVPacket *pkt, MXFIndexEntry *ie)
Definition: mxfenc.c:3038
key
const char * key
Definition: hwcontext_opencl.c:174
MXFContext::audio_edit_rate
AVRational audio_edit_rate
Definition: mxfenc.c:446
AVCodecParameters::nb_coded_side_data
int nb_coded_side_data
Amount of entries in coded_side_data.
Definition: codec_par.h:228
AVMEDIA_TYPE_DATA
@ AVMEDIA_TYPE_DATA
Opaque data information usually continuous.
Definition: avutil.h:203
Preface
@ Preface
Definition: mxf.h:43
AV_CODEC_ID_H264
@ AV_CODEC_ID_H264
Definition: codec_id.h:79
mxf_essence_container_uls
static const MXFContainerEssenceEntry mxf_essence_container_uls[]
Definition: mxfenc.c:163
ff_mxf_color_space_uls
const MXFCodecUL ff_mxf_color_space_uls[]
Definition: mxf.c:133
PacketList::tail
PacketListEntry * tail
Definition: packet_internal.h:34
frame
static AVFrame * frame
Definition: demux_decode.c:54
time_internal.h
if
if(ret)
Definition: filter_design.txt:179
FFFormatContext
Definition: internal.h:70
MXFContext::duration
uint64_t duration
Definition: mxfenc.c:434
MXFIndexEntry::slice_offset
unsigned slice_offset
offset of audio slice
Definition: mxfenc.c:76
SubDescriptor
@ SubDescriptor
Definition: mxf.h:46
AVFormatContext
Format I/O context.
Definition: avformat.h:1115
mxf_write_tagged_value
static int mxf_write_tagged_value(AVFormatContext *s, const char *name, const char *value)
Definition: mxfenc.c:1624
internal.h
MXFContext::store_user_comments
int store_user_comments
Definition: mxfenc.c:447
FF_MXF_MASTERING_LUMA_DEN
#define FF_MXF_MASTERING_LUMA_DEN
Definition: mxf.h:101
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:864
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
mxf_d10_container_uls
static const UID mxf_d10_container_uls[]
Definition: mxfenc.c:236
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
mxf_write_aes3_desc
static void mxf_write_aes3_desc(AVFormatContext *s, AVStream *st)
Definition: mxfenc.c:1610
AVStream::time_base
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented.
Definition: avformat.h:880
NULL
#define NULL
Definition: coverity.c:32
MaterialPackage
@ MaterialPackage
Definition: mxf.h:33
AVERROR_PATCHWELCOME
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:64
MXFContext::edit_unit_byte_count
int edit_unit_byte_count
fixed edit unit byte count
Definition: mxfenc.c:438
AVCHROMA_LOC_LEFT
@ AVCHROMA_LOC_LEFT
MPEG-2/4 4:2:0, H.264 default for 4:2:0.
Definition: pixfmt.h:694
mxf_write_essence_container_data
static int mxf_write_essence_container_data(AVFormatContext *s)
Definition: mxfenc.c:1771
AVRational
Rational number (pair of numerator and denominator).
Definition: rational.h:58
AVCHROMA_LOC_TOPLEFT
@ AVCHROMA_LOC_TOPLEFT
ITU-R 601, SMPTE 274M 296M S314M(DV 4:1:1), mpeg2 4:2:2.
Definition: pixfmt.h:696
FF_MXF_MASTERING_CHROMA_DEN
#define FF_MXF_MASTERING_CHROMA_DEN
Definition: mxf.h:100
mxf_interleave_get_packet
static int mxf_interleave_get_packet(AVFormatContext *s, AVPacket *out, int flush)
Definition: mxfenc.c:3307
MXFContainerEssenceEntry
Definition: mxfenc.c:109
uuid_base
static const uint8_t uuid_base[]
Definition: mxfenc.c:255
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:237
avc.h
MXFStreamContext::b_picture_count
int b_picture_count
maximum number of consecutive b pictures, used in mpeg-2 descriptor
Definition: mxfenc.c:103
AVStream::metadata
AVDictionary * metadata
Definition: avformat.h:921
mxf_utf16_local_tag_length
static int mxf_utf16_local_tag_length(const char *utf8_str)
Definition: mxfenc.c:750
MXFContext::signal_standard
int signal_standard
Definition: mxfenc.c:444
avpriv_find_start_code
const uint8_t * avpriv_find_start_code(const uint8_t *p, const uint8_t *end, uint32_t *state)
FFOutputFormat
Definition: mux.h:32
mxf_parse_ffv1_frame
static int mxf_parse_ffv1_frame(AVFormatContext *s, AVStream *st, AVPacket *pkt)
Definition: mxfenc.c:2478
mxf_write_structural_component
static void mxf_write_structural_component(AVFormatContext *s, AVStream *st, MXFPackage *package)
Definition: mxfenc.c:1006
MXFContext::last_indexed_edit_unit
int last_indexed_edit_unit
Definition: mxfenc.c:430
time.h
MXFStreamContext::max_gop
int max_gop
maximum gop size, used by mpeg-2 descriptor
Definition: mxfenc.c:102
avio_w8
void avio_w8(AVIOContext *s, int b)
Definition: aviobuf.c:226
ffio_fill
void ffio_fill(AVIOContext *s, int b, int64_t count)
Definition: aviobuf.c:234
mxf_init
static int mxf_init(AVFormatContext *s)
Definition: mxfenc.c:2709
mxf_mpeg2_codec_uls
static const UID mxf_mpeg2_codec_uls[]
Definition: mxfenc.c:2527
AVCodecParameters::ch_layout
AVChannelLayout ch_layout
Audio only.
Definition: codec_par.h:206
PacketListEntry::next
struct PacketListEntry * next
Definition: packet_internal.h:29
mxf_write_index_table_segment
static void mxf_write_index_table_segment(AVFormatContext *s)
Definition: mxfenc.c:1851
MXFStreamContext::codec_ul
const UID * codec_ul
Definition: mxfenc.c:85
seed
static unsigned int seed
Definition: videogen.c:78
AVCOL_RANGE_UNSPECIFIED
@ AVCOL_RANGE_UNSPECIFIED
Definition: pixfmt.h:639
AVCodecParameters::level
int level
Definition: codec_par.h:116
INDEX_D10_AUDIO
@ INDEX_D10_AUDIO
Definition: mxfenc.c:128
AVTimecode::rate
AVRational rate
frame rate in rational form
Definition: timecode.h:44
c
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
AV_PROFILE_PRORES_LT
#define AV_PROFILE_PRORES_LT
Definition: defs.h:180
AVCodecParameters::sample_rate
int sample_rate
Audio only.
Definition: codec_par.h:171
cid
uint16_t cid
Definition: mxfenc.c:2151
AV_CODEC_ID_MPEG1VIDEO
@ AV_CODEC_ID_MPEG1VIDEO
Definition: codec_id.h:53
mxf_dnxhd_codec_uls
static const struct @318 mxf_dnxhd_codec_uls[]
AVCodecID
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: codec_id.h:49
MXFStreamContext::order
int order
interleaving order if dts are equal
Definition: mxfenc.c:87
av_packet_side_data_get
const AVPacketSideData * av_packet_side_data_get(const AVPacketSideData *sd, int nb_sd, enum AVPacketSideDataType type)
Get side information from a side data array.
Definition: avpacket.c:650
AVCodecParameters::extradata_size
int extradata_size
Size of the extradata content in bytes.
Definition: codec_par.h:73
mxf_compute_edit_unit_byte_count
static void mxf_compute_edit_unit_byte_count(AVFormatContext *s)
Definition: mxfenc.c:3067
startcode.h
mxf_deinit
static void mxf_deinit(AVFormatContext *s)
Definition: mxfenc.c:3298
av_rescale_rnd
int64_t av_rescale_rnd(int64_t a, int64_t b, int64_t c, enum AVRounding rnd)
Rescale a 64-bit integer with specified rounding.
Definition: mathematics.c:58
mxf_indirect_value_utf16le
static const uint8_t mxf_indirect_value_utf16le[]
Definition: mxfenc.c:1622
INDEX_FFV1
@ INDEX_FFV1
Definition: mxfenc.c:135
ff_init_range_decoder
av_cold void ff_init_range_decoder(RangeCoder *c, const uint8_t *buf, int buf_size)
Definition: rangecoder.c:53
FF_MXF_MasteringDisplayWhitePointChromaticity
#define FF_MXF_MasteringDisplayWhitePointChromaticity
Definition: mxf.h:96
AV_CODEC_ID_FFV1
@ AV_CODEC_ID_FFV1
Definition: codec_id.h:85
ff_mxf_muxer
const FFOutputFormat ff_mxf_muxer
Definition: mxfenc.c:3447
MXFStreamContext::closed_gop
int closed_gop
gop is closed, used in mpeg-2 frame parsing
Definition: mxfenc.c:97
klv_encode_ber4_length
static void klv_encode_ber4_length(AVIOContext *pb, int len)
Definition: mxfenc.c:504
AVIOContext
Bytestream IO Context.
Definition: avio.h:166
AV_CODEC_ID_PCM_S24LE
@ AV_CODEC_ID_PCM_S24LE
Definition: codec_id.h:342
MXFContext::cbr_index
int cbr_index
use a constant bitrate index
Definition: mxfenc.c:449
AVPacket::size
int size
Definition: packet.h:492
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:106
AV_PIX_FMT_FLAG_RGB
#define AV_PIX_FMT_FLAG_RGB
The pixel format contains RGB-like data (as opposed to YUV/grayscale).
Definition: pixdesc.h:136
MXFStreamContext::color_siting
int color_siting
Definition: mxfenc.c:91
MXFStreamContext::pkt_cnt
int64_t pkt_cnt
pkt counter for muxed packets
Definition: mxfenc.c:82
sample
#define sample
Definition: flacdsp_template.c:44
mxf_write_packet
static int mxf_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: mxfenc.c:3088
size
int size
Definition: twinvq_data.h:10344
Identification
@ Identification
Definition: mxf.h:44
INDEX_AES3
@ INDEX_AES3
Definition: mxfenc.c:125
av_make_q
static AVRational av_make_q(int num, int den)
Create an AVRational.
Definition: rational.h:71
ff_build_rac_states
void ff_build_rac_states(RangeCoder *c, int factor, int max_p)
Definition: rangecoder.c:68
AV_NOPTS_VALUE
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
mxf_update_klv_size
static void mxf_update_klv_size(AVIOContext *pb, int64_t pos)
Definition: mxfenc.c:1400
AV_RB32
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_RB32
Definition: bytestream.h:96
PacketListEntry::pkt
AVPacket pkt
Definition: packet_internal.h:30
AVCodecParameters::profile
int profile
Codec-specific bitstream restrictions that the stream conforms to.
Definition: codec_par.h:115
AVCHROMA_LOC_UNSPECIFIED
@ AVCHROMA_LOC_UNSPECIFIED
Definition: pixfmt.h:693
MXFContext::time_base
AVRational time_base
Definition: mxfenc.c:424
get_ffv1_unsigned_symbol
static int get_ffv1_unsigned_symbol(RangeCoder *c, uint8_t *state)
Definition: mxfenc.c:2456
AV_RL24
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_RL24
Definition: bytestream.h:93
MXFContext::av_class
AVClass * av_class
Definition: mxfenc.c:421
MXFContext::timestamp
uint64_t timestamp
timestamp, as year(16),month(8),day(8),hour(8),minutes(8),msec/4(8)
Definition: mxfenc.c:428
mxf_write_umid
static void mxf_write_umid(AVFormatContext *s, int type)
Definition: mxfenc.c:461
mxf_essence_mappings
static const struct @316 mxf_essence_mappings[]
AVPacket::dts
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed.
Definition: packet.h:490
avio_write
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:248
avio_wb32
void avio_wb32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:412
AV_PROFILE_PRORES_4444
#define AV_PROFILE_PRORES_4444
Definition: defs.h:183
a
The reader does not expect b to be semantically here and if the code is changed by maybe adding a a division or other the signedness will almost certainly be mistaken To avoid this confusion a new type was SUINT is the C unsigned type but it holds a signed int to use the same example SUINT a
Definition: undefined.txt:41
av_reallocp_array
int av_reallocp_array(void *ptr, size_t nmemb, size_t size)
Allocate, reallocate an array through a pointer to a pointer.
Definition: mem.c:223
mxf_opatom_muxer_class
static const AVClass mxf_opatom_muxer_class
Definition: mxfenc.c:3440
avio_wl32
void avio_wl32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:404
IndexTableSegment
@ IndexTableSegment
Definition: mxf.h:47
mxf_gen_umid
static void mxf_gen_umid(AVFormatContext *s)
Definition: mxfenc.c:2644
mxf_write_system_item
static void mxf_write_system_item(AVFormatContext *s)
Definition: mxfenc.c:2936
FF_COMPLIANCE_UNOFFICIAL
#define FF_COMPLIANCE_UNOFFICIAL
Allow unofficial extensions.
Definition: defs.h:61
MXFStreamContext::micro_version
int micro_version
format micro_version, used in ffv1 descriptor
Definition: mxfenc.c:106
version
version
Definition: libkvazaar.c:321
MXFContext::timecode_track_priv
MXFStreamContext timecode_track_priv
Definition: mxfenc.c:451
AV_PROFILE_PRORES_PROXY
#define AV_PROFILE_PRORES_PROXY
Definition: defs.h:179
MXFContext::body_partition_offset
uint64_t * body_partition_offset
Definition: mxfenc.c:431
multiple_desc_ul
static const uint8_t multiple_desc_ul[]
Definition: mxfenc.c:275
opatom_options
static const AVOption opatom_options[]
Definition: mxfenc.c:3431
AVChromaLocation
AVChromaLocation
Location of chroma samples.
Definition: pixfmt.h:692
footer_partition_key
static const uint8_t footer_partition_key[]
Definition: mxfenc.c:263
av_timecode_get_smpte_from_framenum
uint32_t av_timecode_get_smpte_from_framenum(const AVTimecode *tc, int framenum)
Convert frame number to SMPTE 12M binary representation.
Definition: timecode.c:53
MXFContext::footer_partition_offset
int64_t footer_partition_offset
Definition: mxfenc.c:422
AV_CODEC_ID_MJPEG
@ AV_CODEC_ID_MJPEG
Definition: codec_id.h:59
container_ul
const UID container_ul
Definition: mxfenc.c:2223
mxf_get_codec_ul_by_id
static const MXFCodecUL * mxf_get_codec_ul_by_id(const MXFCodecUL *uls, int id)
Definition: mxfenc.c:637
interlaced
uint8_t interlaced
Definition: mxfenc.c:2152
mxf_write_avc_subdesc
static void mxf_write_avc_subdesc(AVFormatContext *s, AVStream *st)
Definition: mxfenc.c:1409
mxf_lookup_local_tag
static const MXFLocalTagPair * mxf_lookup_local_tag(int tag)
Definition: mxfenc.c:528
PacketListEntry
Definition: packet_internal.h:28
AV_CODEC_ID_NONE
@ AV_CODEC_ID_NONE
Definition: codec_id.h:50
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:255
AVPacket::pts
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:484
MXFContext::content_package_rate
int content_package_rate
content package rate in system element, see SMPTE 326M
Definition: mxfenc.c:439
avio_internal.h
klv_fill_size
static unsigned klv_fill_size(uint64_t size)
Definition: mxfenc.c:1842
mxf_write_cdci_desc
static void mxf_write_cdci_desc(AVFormatContext *s, AVStream *st)
Definition: mxfenc.c:1433
MXFContext::timecode_track
AVStream * timecode_track
Definition: mxfenc.c:436
codec_ul
UID codec_ul
Definition: mxfenc.c:2116
MXFStreamContext::slice_offset
int slice_offset
Definition: mxfenc.c:99
index
enum ULIndex index
Definition: mxfenc.c:140
AVCodecParameters::height
int height
Definition: codec_par.h:122
mxf_write_s436m_anc_desc
static void mxf_write_s436m_anc_desc(AVFormatContext *s, AVStream *st)
Definition: mxfenc.c:1496
AVCodecParameters::block_align
int block_align
Audio only.
Definition: codec_par.h:178
MXFStreamContext::container_ul
const UID * container_ul
Definition: mxfenc.c:86
ff_interleave_add_packet
int ff_interleave_add_packet(AVFormatContext *s, AVPacket *pkt, int(*compare)(AVFormatContext *, const AVPacket *, const AVPacket *))
Add packet to an AVFormatContext's packet_buffer list, determining its interleaved position using com...
Definition: mux.c:845
av_assert1
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition: avassert.h:56
mxf_parse_dv_frame
static int mxf_parse_dv_frame(AVFormatContext *s, AVStream *st, AVPacket *pkt)
Definition: mxfenc.c:2248
MXFContainerEssenceEntry::element_ul
UID element_ul
Definition: mxfenc.c:111
umid_ul
static const uint8_t umid_ul[]
Definition: mxfenc.c:256
value
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf default value
Definition: writing_filters.txt:86
MXFIndexEntry::flags
uint8_t flags
Definition: mxfenc.c:78
mxf_d10_codec_uls
static const UID mxf_d10_codec_uls[]
Definition: mxfenc.c:227
mxf_write_timecode_component
static void mxf_write_timecode_component(AVFormatContext *s, AVStream *st, MXFPackage *package)
Definition: mxfenc.c:979
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
system_metadata_pack_key
static const uint8_t system_metadata_pack_key[]
Definition: mxfenc.c:2933
AV_STRINGIFY
#define AV_STRINGIFY(s)
Definition: macros.h:66
AV_CODEC_ID_DVVIDEO
@ AV_CODEC_ID_DVVIDEO
Definition: codec_id.h:76
MXFIndexEntry::offset
uint64_t offset
Definition: mxfenc.c:75
ff_mxf_opatom_muxer
const FFOutputFormat ff_mxf_opatom_muxer
Definition: mxfenc.c:3480
EssenceContainerData
@ EssenceContainerData
Definition: mxf.h:48
av_mallocz
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:254
mxf_write_uuid
static void mxf_write_uuid(AVIOContext *pb, enum MXFMetadataSetType type, int value)
Definition: mxfenc.c:454
mxf_write_ffv1_subdesc
static void mxf_write_ffv1_subdesc(AVFormatContext *s, AVStream *st)
Definition: mxfenc.c:1455
av_inv_q
static av_always_inline AVRational av_inv_q(AVRational q)
Invert a rational.
Definition: rational.h:159
AVCodecParameters::color_range
enum AVColorRange color_range
Video only.
Definition: codec_par.h:141
AVMasteringDisplayMetadata
Mastering display metadata capable of representing the color volume of the display used to master the...
Definition: mastering_display_metadata.h:38
len
int len
Definition: vorbis_enc_data.h:426
AV_CODEC_ID_JPEG2000
@ AV_CODEC_ID_JPEG2000
Definition: codec_id.h:140
get_rac
static int get_rac(RangeCoder *c, uint8_t *const state)
Definition: rangecoder.h:127
profile
int profile
Definition: mxfenc.c:2115
av_rescale
int64_t av_rescale(int64_t a, int64_t b, int64_t c)
Rescale a 64-bit integer with rounding to nearest.
Definition: mathematics.c:129
AVCodecParameters::coded_side_data
AVPacketSideData * coded_side_data
Additional data associated with the entire stream.
Definition: codec_par.h:223
FFStream::last_in_packet_buffer
PacketListEntry * last_in_packet_buffer
last packet in packet_buffer for this stream when muxing.
Definition: internal.h:380
AVCOL_RANGE_MPEG
@ AVCOL_RANGE_MPEG
Narrow or limited range content.
Definition: pixfmt.h:656
MXFContext::instance_number
uint32_t instance_number
Definition: mxfenc.c:441
MXFContext::last_key_index
int last_key_index
index of last key frame
Definition: mxfenc.c:433
AVCodecParameters::field_order
enum AVFieldOrder field_order
Video only.
Definition: codec_par.h:136
MXFPackage
Definition: mxfdec.c:263
mxf_interleave
static int mxf_interleave(AVFormatContext *s, AVPacket *pkt, int flush, int has_packet)
Definition: mxfenc.c:3368
version.h
AV_TIMECODE_FLAG_DROPFRAME
@ AV_TIMECODE_FLAG_DROPFRAME
timecode is drop frame
Definition: timecode.h:36
MXFStreamContext::seq_closed_gop
int seq_closed_gop
all gops in sequence are closed, used in mpeg-2 descriptor
Definition: mxfenc.c:101
tag
uint32_t tag
Definition: movenc.c:1737
LIBAVFORMAT_VERSION
#define LIBAVFORMAT_VERSION
Definition: version.h:40
ret
ret
Definition: filter_design.txt:187
mxf_write_wav_common
static int64_t mxf_write_wav_common(AVFormatContext *s, AVStream *st, const UID key)
Definition: mxfenc.c:1589
AVFMT_FLAG_BITEXACT
#define AVFMT_FLAG_BITEXACT
When muxing, try to avoid writing any random/volatile data to the output.
Definition: avformat.h:1250
AVStream
Stream structure.
Definition: avformat.h:841
LIBAVFORMAT_VERSION_MINOR
#define LIBAVFORMAT_VERSION_MINOR
Definition: version.h:34
mxf_wav_descriptor_key
static const UID mxf_wav_descriptor_key
Definition: mxfenc.c:1131
op1a_ul
static const uint8_t op1a_ul[]
complete key for operation pattern, partitions, and primer pack
Definition: mxfenc.c:261
avio_seek
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:278
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
MXFContext::umid
uint8_t umid[16]
unique material identifier
Definition: mxfenc.c:442
sps
static int FUNC() sps(CodedBitstreamContext *ctx, RWContext *rw, H264RawSPS *current)
Definition: cbs_h264_syntax_template.c:260
pos
unsigned int pos
Definition: spdifenc.c:413
avformat.h
MXFLocalTagPair
Definition: mxf.h:87
mxf_write_partition
static int mxf_write_partition(AVFormatContext *s, int bodysid, int indexsid, const uint8_t *key, int write_metadata)
Definition: mxfenc.c:2009
ff_mxf_random_index_pack_key
const uint8_t ff_mxf_random_index_pack_key[16]
Definition: mxf.c:26
AVCodecParameters::chroma_location
enum AVChromaLocation chroma_location
Definition: codec_par.h:145
opatom_ul
static const uint8_t opatom_ul[]
Definition: mxfenc.c:262
avio_put_str16be
int avio_put_str16be(AVIOContext *s, const char *str)
Convert an UTF-8 string to UTF-16BE and write it.
id
enum AVCodecID id
Definition: mxfenc.c:139
mxf_write_mpegvideo_desc
static void mxf_write_mpegvideo_desc(AVFormatContext *s, AVStream *st)
Definition: mxfenc.c:1502
AVStream::index
int index
stream index in AVFormatContext
Definition: avformat.h:847
get_ue_golomb_31
static int get_ue_golomb_31(GetBitContext *gb)
read unsigned exp golomb code, constraint to a max of 31.
Definition: golomb.h:120
rescale_mastering_chroma
static uint16_t rescale_mastering_chroma(AVRational q)
Definition: mxfenc.c:1140
klv_fill_key
static const uint8_t klv_fill_key[]
Definition: mxfenc.c:268
random_seed.h
mxf_init_timecode
static int mxf_init_timecode(AVFormatContext *s, AVStream *st, AVRational tbc)
Definition: mxfenc.c:2656
MXFIndexEntry::temporal_ref
uint16_t temporal_ref
Definition: mxfenc.c:77
AVIO_SEEKABLE_NORMAL
#define AVIO_SEEKABLE_NORMAL
Seeking works like for a local file.
Definition: avio.h:41
mxf_write_local_tag_utf16
static void mxf_write_local_tag_utf16(AVFormatContext *s, int tag, const char *value)
Definition: mxfenc.c:769
MXFContext::index_entries
MXFIndexEntry * index_entries
Definition: mxfenc.c:426
AVCHROMA_LOC_CENTER
@ AVCHROMA_LOC_CENTER
MPEG-1 4:2:0, JPEG 4:2:0, H.263 4:2:0.
Definition: pixfmt.h:695
MXFStreamContext::low_delay
int low_delay
low delay, used in mpeg-2 descriptor
Definition: mxfenc.c:104
FF_MXF_MasteringDisplayMaximumLuminance
#define FF_MXF_MasteringDisplayMaximumLuminance
Definition: mxf.h:97
AVRational::den
int den
Denominator.
Definition: rational.h:60
MXFStreamContext::video_bit_rate
int video_bit_rate
Definition: mxfenc.c:98
mxf_mpegvideo_descriptor_key
static const UID mxf_mpegvideo_descriptor_key
Definition: mxfenc.c:1130
MXFStreamContext::aspect_ratio
AVRational aspect_ratio
display aspect ratio
Definition: mxfenc.c:96
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Definition: opt.h:225
defs.h
MXFContext::track_instance_count
int track_instance_count
Definition: mxfenc.c:448
MXFContext::channel_count
int channel_count
Definition: mxfenc.c:443
klv_ber_length
static int klv_ber_length(uint64_t len)
Definition: mxfenc.c:476
AVPixFmtDescriptor::comp
AVComponentDescriptor comp[4]
Parameters that describe how pixels are packed.
Definition: pixdesc.h:105
MXFContext::essence_container_count
int essence_container_count
Definition: mxfenc.c:423
mxf_get_mpeg2_codec_ul
static const UID * mxf_get_mpeg2_codec_ul(AVCodecParameters *par)
Definition: mxfenc.c:2540
samples
Filter the word “frame” indicates either a video frame or a group of audio samples
Definition: filter_design.txt:8
SourcePackage
@ SourcePackage
Definition: mxf.h:34
AVMasteringDisplayMetadata::min_luminance
AVRational min_luminance
Min luminance of mastering display (cd/m^2).
Definition: mastering_display_metadata.h:52
av_mul_q
AVRational av_mul_q(AVRational b, AVRational c)
Multiply two rationals.
Definition: rational.c:80
AVPacket::stream_index
int stream_index
Definition: packet.h:493
state
static struct @362 state
MXFStreamContext::avc_intra
int avc_intra
Definition: mxfenc.c:105
av_clip_uint16
#define av_clip_uint16
Definition: common.h:108
avio_wb64
void avio_wb64(AVIOContext *s, uint64_t val)
Definition: aviobuf.c:478
INDEX_D10_VIDEO
@ INDEX_D10_VIDEO
Definition: mxfenc.c:127
mxf_generic_sound_descriptor_key
static const UID mxf_generic_sound_descriptor_key
Definition: mxfenc.c:1135
mxf_parse_timestamp
static uint64_t mxf_parse_timestamp(int64_t timestamp64)
Definition: mxfenc.c:2628
mxf_write_generic_sound_common
static int64_t mxf_write_generic_sound_common(AVFormatContext *s, AVStream *st, const UID key)
Definition: mxfenc.c:1540
desc
const char * desc
Definition: libsvtav1.c:83
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
H264_NAL_IDR_SLICE
@ H264_NAL_IDR_SLICE
Definition: h264.h:39
flush
void(* flush)(AVBSFContext *ctx)
Definition: dts2pts_bsf.c:367
index_table_segment_key
static const uint8_t index_table_segment_key[]
Definition: mxfenc.c:265
header_closed_partition_key
static const uint8_t header_closed_partition_key[]
Definition: mxfenc.c:267
mxf_write_preface
static void mxf_write_preface(AVFormatContext *s)
Definition: mxfenc.c:673
packet_internal.h
INDEX_PRORES
@ INDEX_PRORES
Definition: mxfenc.c:134
mastering_display_metadata.h
get_ue_golomb_long
static unsigned get_ue_golomb_long(GetBitContext *gb)
Read an unsigned Exp-Golomb code in the range 0 to UINT32_MAX-1.
Definition: golomb.h:104
H264_NAL_SPS
@ H264_NAL_SPS
Definition: h264.h:41
AVCodecParameters::format
int format
Definition: codec_par.h:79
MXFCodecUL
Definition: mxf.h:103
AVPixFmtDescriptor
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:69
avio_wb24
void avio_wb24(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:502
mxf_options
static const AVOption mxf_options[]
Definition: mxfenc.c:3401
MXFContainerEssenceEntry::container_ul
UID container_ul
Definition: mxfenc.c:110
AVDictionaryEntry
Definition: dict.h:89
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:55
AVPacket
This structure stores compressed data.
Definition: packet.h:468
AV_OPT_TYPE_BOOL
@ AV_OPT_TYPE_BOOL
Definition: opt.h:244
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
write_metadata
static void write_metadata(AVFormatContext *s, unsigned int ts)
Definition: flvenc.c:279
mxf_write_ffv1_desc
static void mxf_write_ffv1_desc(AVFormatContext *s, AVStream *st)
Definition: mxfenc.c:1484
mxf_write_generic_desc
static int64_t mxf_write_generic_desc(AVFormatContext *s, AVStream *st, const UID key)
Definition: mxfenc.c:1091
avio_put_str16le
int avio_put_str16le(AVIOContext *s, const char *str)
Convert an UTF-8 string to UTF-16LE and write it.
AV_PROFILE_PRORES_XQ
#define AV_PROFILE_PRORES_XQ
Definition: defs.h:184
h264.h
mxf_s436m_anc_descriptor_key
static const UID mxf_s436m_anc_descriptor_key
Definition: mxfenc.c:1129
avio_wb16
void avio_wb16(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:490
EDIT_UNITS_PER_BODY
#define EDIT_UNITS_PER_BODY
Definition: mxfenc.c:71
AVCodecParameters::bit_rate
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
Definition: codec_par.h:84
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
mxf_write_tape_descriptor
static void mxf_write_tape_descriptor(AVFormatContext *s)
Definition: mxfenc.c:1041
mxf_write_h264_desc
static void mxf_write_h264_desc(AVFormatContext *s, AVStream *st)
Definition: mxfenc.c:1443
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
mxf_aes3_descriptor_key
static const UID mxf_aes3_descriptor_key
Definition: mxfenc.c:1132
mxf_write_essence_container_refs
static void mxf_write_essence_container_refs(AVFormatContext *s)
Definition: mxfenc.c:651
av_timecode_init_from_string
int av_timecode_init_from_string(AVTimecode *tc, AVRational rate, const char *str, void *log_ctx)
Parse timecode representation (hh:mm:ss[:;.
Definition: timecode.c:252
RangeCoder
Definition: mss3.c:62
Sequence
@ Sequence
Definition: mxf.h:38
AVDictionaryEntry::value
char * value
Definition: dict.h:91
IS_D10
#define IS_D10(s)
Definition: mxfenc.c:68
AVTimecode
Definition: timecode.h:41
AVIOContext::buf_ptr
unsigned char * buf_ptr
Current position in the buffer.
Definition: avio.h:233
AV_CODEC_ID_MPEG2VIDEO
@ AV_CODEC_ID_MPEG2VIDEO
preferred ID for MPEG-1/2 video decoding
Definition: codec_id.h:54
mxf_write_refs_count
static void mxf_write_refs_count(AVIOContext *pb, int ref_count)
Definition: mxfenc.c:470
MXFStreamContext::component_depth
int component_depth
Definition: mxfenc.c:90
snprintf
#define snprintf
Definition: snprintf.h:34
ff_stream_add_bitstream_filter
int ff_stream_add_bitstream_filter(AVStream *st, const char *name, const char *args)
Add a bitstream filter to a stream.
Definition: mux.c:1347
av_log2
int av_log2(unsigned v)
Definition: intmath.c:26
mxf_write_primer_pack
static void mxf_write_primer_pack(AVFormatContext *s)
Definition: mxfenc.c:547
MXFCodecUL::uid
UID uid
Definition: mxf.h:104
MXFContext::body_partitions_count
unsigned body_partitions_count
Definition: mxfenc.c:432
MXFStreamContext::field_dominance
int field_dominance
tff=1, bff=2
Definition: mxfenc.c:89
MXFContext::unused_tags
uint8_t unused_tags[MXF_NUM_TAGS]
local tags that we know will not be used
Definition: mxfenc.c:450
AVPixFmtDescriptor::log2_chroma_h
uint8_t log2_chroma_h
Amount to shift the luma height right to find the chroma height.
Definition: pixdesc.h:89
AV_CODEC_ID_DNXHD
@ AV_CODEC_ID_DNXHD
Definition: codec_id.h:151
FFV1SubDescriptor
@ FFV1SubDescriptor
Definition: mxf.h:56
MXFPackage::name
char * name
Definition: mxfdec.c:270
AV_CODEC_ID_PRORES
@ AV_CODEC_ID_PRORES
Definition: codec_id.h:200
mxf_write_metadata_key
static void mxf_write_metadata_key(AVIOContext *pb, unsigned int value)
Definition: mxfenc.c:631
ff_avc_find_startcode
const uint8_t * ff_avc_find_startcode(const uint8_t *p, const uint8_t *end)
Definition: avc.c:67
AVIO_DATA_MARKER_FLUSH_POINT
@ AVIO_DATA_MARKER_FLUSH_POINT
A point in the output bytestream where the underlying AVIOContext might flush the buffer depending on...
Definition: avio.h:151
mux.h