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