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 RP210: SMPTE Metadata Dictionary
36  * SMPTE RP224: Registry of SMPTE Universal Labels
37  */
38 
39 #include <inttypes.h>
40 #include <math.h>
41 #include <time.h>
42 
43 #include "libavutil/opt.h"
44 #include "libavutil/random_seed.h"
45 #include "libavutil/timecode.h"
46 #include "libavutil/avassert.h"
47 #include "libavutil/pixdesc.h"
49 #include "libavcodec/bytestream.h"
50 #include "libavcodec/dnxhddata.h"
51 #include "libavcodec/dv_profile.h"
52 #include "libavcodec/h264_ps.h"
53 #include "libavcodec/golomb.h"
54 #include "libavcodec/internal.h"
55 #include "audiointerleave.h"
56 #include "avformat.h"
57 #include "avio_internal.h"
58 #include "internal.h"
59 #include "avc.h"
60 #include "mxf.h"
61 #include "config.h"
62 
65 
66 #define EDIT_UNITS_PER_BODY 250
67 #define KAG_SIZE 512
68 
69 typedef struct MXFLocalTagPair {
70  int local_tag;
73 
74 typedef struct MXFIndexEntry {
76  uint64_t offset;
77  unsigned slice_offset; ///< offset of audio slice
78  uint16_t temporal_ref;
80 
81 typedef struct MXFStreamContext {
84  int index; ///< index in mxf_essence_container_uls table
85  const UID *codec_ul;
86  int order; ///< interleaving order if dts are equal
87  int interlaced; ///< whether picture is interlaced
88  int field_dominance; ///< tff=1, bff=2
95  AVRational aspect_ratio; ///< display aspect ratio
96  int closed_gop; ///< gop is closed, used in mpeg-2 frame parsing
99  int frame_size; ///< frame size in bytes
100  int seq_closed_gop; ///< all gops in sequence are closed, used in mpeg-2 descriptor
101  int max_gop; ///< maximum gop size, used by mpeg-2 descriptor
102  int b_picture_count; ///< maximum number of consecutive b pictures, used in mpeg-2 descriptor
103  int low_delay; ///< low delay, used in mpeg-2 descriptor
106 
107 typedef struct MXFContainerEssenceEntry {
111  void (*write_desc)(AVFormatContext *, AVStream *);
113 
114 typedef struct MXFPackage {
115  char *name;
117  int instance;
118  struct MXFPackage *ref;
119 } MXFPackage;
120 
121 enum ULIndex {
153 };
154 
155 static const struct {
156  enum AVCodecID id;
158 } mxf_essence_mappings[] = {
161  { AV_CODEC_ID_PCM_S16LE, INDEX_AES3 },
167  { AV_CODEC_ID_NONE }
168 };
169 
170 static void mxf_write_wav_desc(AVFormatContext *s, AVStream *st);
171 static void mxf_write_aes3_desc(AVFormatContext *s, AVStream *st);
173 static void mxf_write_h264_desc(AVFormatContext *s, AVStream *st);
174 static void mxf_write_cdci_desc(AVFormatContext *s, AVStream *st);
177 
179  { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x02,0x0D,0x01,0x03,0x01,0x02,0x04,0x60,0x01 },
180  { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x15,0x01,0x05,0x00 },
181  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x00,0x00,0x00 },
183  { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x06,0x03,0x00 },
184  { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x16,0x01,0x03,0x00 },
185  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 },
187  { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x06,0x01,0x00 },
188  { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x16,0x01,0x01,0x00 },
189  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 },
191  // D-10 625/50 PAL 50mb/s
192  { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x01,0x01 },
193  { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x05,0x01,0x01,0x00 },
194  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x01,0x02,0x01,0x01 },
196  { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x01,0x01 },
197  { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x06,0x01,0x10,0x00 },
198  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 },
200  // D-10 525/60 NTSC 50mb/s
201  { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x02,0x01 },
202  { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x05,0x01,0x01,0x00 },
203  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x01,0x02,0x01,0x02 },
205  { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x02,0x01 },
206  { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x06,0x01,0x10,0x00 },
207  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 },
209  // D-10 625/50 PAL 40mb/s
210  { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x03,0x01 },
211  { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x05,0x01,0x01,0x00 },
212  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x01,0x02,0x01,0x03 },
214  { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x03,0x01 },
215  { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x06,0x01,0x10,0x00 },
216  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 },
218  // D-10 525/60 NTSC 40mb/s
219  { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x04,0x01 },
220  { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x05,0x01,0x01,0x00 },
221  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x01,0x02,0x01,0x04 },
223  { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x04,0x01 },
224  { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x06,0x01,0x10,0x00 },
225  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 },
227  // D-10 625/50 PAL 30mb/s
228  { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x05,0x01 },
229  { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x05,0x01,0x01,0x00 },
230  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x01,0x02,0x01,0x05 },
232  { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x05,0x01 },
233  { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x06,0x01,0x10,0x00 },
234  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 },
236  // D-10 525/60 NTSC 30mb/s
237  { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x06,0x01 },
238  { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x05,0x01,0x01,0x00 },
239  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x01,0x02,0x01,0x06 },
241  { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x06,0x01 },
242  { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x06,0x01,0x10,0x00 },
243  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 },
245  // DV Unknown
246  { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x02,0x7F,0x01 },
247  { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x18,0x01,0x01,0x00 },
248  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x02,0x00,0x00,0x00 },
250 
251  // DV25 525/60
252  { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x02,0x40,0x01 },
253  { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x18,0x01,0x01,0x00 },
254  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x02,0x02,0x01,0x00 },
256  // DV25 625/50
257  { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x02,0x41,0x01 },
258  { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x18,0x01,0x01,0x00 },
259  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x02,0x02,0x02,0x00 },
261 
262  // IEC DV25 525/60
263  { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x02,0x01,0x01 },
264  { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x18,0x01,0x01,0x00 },
265  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x02,0x02,0x01,0x00 },
267  // IEC DV25 625/50
268  { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x02,0x02,0x01 },
269  { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x18,0x01,0x01,0x00 },
270  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x02,0x01,0x02,0x00 },
272 
273  // DV50 525/60
274  { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x02,0x50,0x01 },
275  { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x18,0x01,0x01,0x00 },
276  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x02,0x02,0x03,0x00 },
278  // DV50 625/50
279  { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x02,0x51,0x01 },
280  { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x18,0x01,0x01,0x00 },
281  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x02,0x02,0x04,0x00 },
283  // DV100 1080/60
284  { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x02,0x60,0x01 },
285  { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x18,0x01,0x01,0x00 },
286  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x02,0x02,0x05,0x00 },
288  // DV100 1080/50
289  { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x02,0x61,0x01 },
290  { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x18,0x01,0x01,0x00 },
291  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x02,0x02,0x06,0x00 },
293  // DV100 720/60
294  { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x02,0x62,0x01 },
295  { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x18,0x01,0x01,0x00 },
296  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x02,0x02,0x07,0x00 },
298  // DV100 720/50
299  { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x02,0x63,0x01 },
300  { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x18,0x01,0x01,0x00 },
301  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x02,0x02,0x08,0x00 },
303  // DNxHD
304  { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x11,0x01,0x00 },
305  { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x15,0x01,0x05,0x00 },
306  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0A,0x04,0x01,0x02,0x02,0x71,0x01,0x00,0x00 },
308  // JPEG2000
309  { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x07,0x0d,0x01,0x03,0x01,0x02,0x0c,0x01,0x00 },
310  { 0x06,0x0e,0x2b,0x34,0x01,0x02,0x01,0x01,0x0d,0x01,0x03,0x01,0x15,0x01,0x08,0x00 },
311  { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x07,0x04,0x01,0x02,0x02,0x03,0x01,0x01,0x00 },
313  // H.264
314  { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x0D,0x01,0x03,0x01,0x02,0x10,0x60,0x01 },
315  { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x15,0x01,0x05,0x00 },
316  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x00,0x00,0x00 },
318  // S436M ANC
319  { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x0D,0x01,0x03,0x01,0x02,0x0e,0x00,0x00 },
320  { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x17,0x01,0x02,0x00 },
321  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x01,0x5C,0x00 },
323  // ProRes
324  { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0d,0x0d,0x01,0x03,0x01,0x02,0x1c,0x01,0x00 },
325  { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0d,0x01,0x03,0x01,0x15,0x01,0x17,0x00 },
326  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0d,0x04,0x01,0x02,0x02,0x03,0x06,0x03,0x00 },
328  { { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 },
329  { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 },
330  { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 },
331  NULL },
332 };
333 
334 typedef struct MXFContext {
342  uint64_t timestamp; ///< timestamp, as year(16),month(8),day(8),hour(8),minutes(8),msec/4(8)
343  uint8_t slice_count; ///< index slice count minus 1 (1 if no audio, 0 otherwise)
347  int last_key_index; ///< index of last key frame
348  uint64_t duration;
349  AVTimecode tc; ///< timecode context
351  int timecode_base; ///< rounded time code base (25 or 30)
352  int edit_unit_byte_count; ///< fixed edit unit byte count
353  int content_package_rate; ///< content package rate in system element, see SMPTE 326M
354  uint64_t body_offset;
355  uint32_t instance_number;
356  uint8_t umid[16]; ///< unique material identifier
362  int track_instance_count; // used to generate MXFTrack uuids
363  int cbr_index; ///< use a constant bitrate index
364 } MXFContext;
365 
366 static const uint8_t uuid_base[] = { 0xAD,0xAB,0x44,0x24,0x2f,0x25,0x4d,0xc7,0x92,0xff,0x29,0xbd };
367 static const uint8_t umid_ul[] = { 0x06,0x0A,0x2B,0x34,0x01,0x01,0x01,0x05,0x01,0x01,0x0D,0x00,0x13 };
368 
369 /**
370  * complete key for operation pattern, partitions, and primer pack
371  */
372 static const uint8_t op1a_ul[] = { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x02,0x01,0x01,0x01,0x09,0x00 };
373 static const uint8_t opatom_ul[] = { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x02,0x0D,0x01,0x02,0x01,0x10,0x03,0x00,0x00 };
374 static const uint8_t footer_partition_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0D,0x01,0x02,0x01,0x01,0x04,0x04,0x00 }; // ClosedComplete
375 static const uint8_t primer_pack_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0D,0x01,0x02,0x01,0x01,0x05,0x01,0x00 };
376 static const uint8_t index_table_segment_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x10,0x01,0x00 };
377 static const uint8_t random_index_pack_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0D,0x01,0x02,0x01,0x01,0x11,0x01,0x00 };
378 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
379 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
380 static const uint8_t klv_fill_key[] = { 0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x03,0x01,0x02,0x10,0x01,0x00,0x00,0x00 };
381 static const uint8_t body_partition_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0D,0x01,0x02,0x01,0x01,0x03,0x04,0x00 }; // ClosedComplete
382 
383 /**
384  * partial key for header metadata
385  */
386 static const uint8_t header_metadata_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0D,0x01,0x01,0x01,0x01 };
387 static const uint8_t multiple_desc_ul[] = { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x0D,0x01,0x03,0x01,0x02,0x7F,0x01,0x00 };
388 
389 /**
390  * SMPTE RP210 http://www.smpte-ra.org/mdd/index.html
391  * https://smpte-ra.org/sites/default/files/Labels.xml
392  */
394  // preface set
395  { 0x3C0A, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x01,0x01,0x15,0x02,0x00,0x00,0x00,0x00}}, /* Instance UID */
396  { 0x3B02, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x10,0x02,0x04,0x00,0x00}}, /* Last Modified Date */
397  { 0x3B05, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x03,0x01,0x02,0x01,0x05,0x00,0x00,0x00}}, /* Version */
398  { 0x3B07, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x03,0x01,0x02,0x01,0x04,0x00,0x00,0x00}}, /* Object Model Version */
399  { 0x3B06, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x06,0x04,0x00,0x00}}, /* Identifications reference */
400  { 0x3B03, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x02,0x01,0x00,0x00}}, /* Content Storage reference */
401  { 0x3B09, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x01,0x02,0x02,0x03,0x00,0x00,0x00,0x00}}, /* Operational Pattern UL */
402  { 0x3B0A, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x01,0x02,0x02,0x10,0x02,0x01,0x00,0x00}}, /* Essence Containers UL batch */
403  { 0x3B0B, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x01,0x02,0x02,0x10,0x02,0x02,0x00,0x00}}, /* DM Schemes UL batch */
404  // Identification
405  { 0x3C09, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x20,0x07,0x01,0x01,0x00,0x00,0x00}}, /* This Generation UID */
406  { 0x3C01, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x20,0x07,0x01,0x02,0x01,0x00,0x00}}, /* Company Name */
407  { 0x3C02, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x20,0x07,0x01,0x03,0x01,0x00,0x00}}, /* Product Name */
408  { 0x3C03, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x20,0x07,0x01,0x04,0x00,0x00,0x00}}, /* Product Version */
409  { 0x3C04, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x20,0x07,0x01,0x05,0x01,0x00,0x00}}, /* Version String */
410  { 0x3C05, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x20,0x07,0x01,0x07,0x00,0x00,0x00}}, /* Product ID */
411  { 0x3C06, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x10,0x02,0x03,0x00,0x00}}, /* Modification Date */
412  { 0x3C07, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x20,0x07,0x01,0x0A,0x00,0x00,0x00}}, /* Toolkit Version */
413  { 0x3C08, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x20,0x07,0x01,0x06,0x01,0x00,0x00}}, /* Platform */
414  // Content Storage
415  { 0x1901, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x05,0x01,0x00,0x00}}, /* Package strong reference batch */
416  { 0x1902, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x05,0x02,0x00,0x00}}, /* Package strong reference batch */
417  // Essence Container Data
418  { 0x2701, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x06,0x01,0x00,0x00,0x00}}, /* Linked Package UID */
419  { 0x3F07, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x01,0x03,0x04,0x04,0x00,0x00,0x00,0x00}}, /* BodySID */
420  // Package
421  { 0x4401, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x01,0x01,0x15,0x10,0x00,0x00,0x00,0x00}}, /* Package UID */
422  { 0x4405, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x10,0x01,0x03,0x00,0x00}}, /* Package Creation Date */
423  { 0x4404, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x10,0x02,0x05,0x00,0x00}}, /* Package Modified Date */
424  { 0x4402, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x01,0x03,0x03,0x02,0x01,0x00,0x00,0x00}}, /* Package Name */
425  { 0x4403, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x06,0x05,0x00,0x00}}, /* Tracks Strong reference array */
426  { 0x4701, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x02,0x03,0x00,0x00}}, /* Descriptor */
427  // Track
428  { 0x4801, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x01,0x07,0x01,0x01,0x00,0x00,0x00,0x00}}, /* Track ID */
429  { 0x4804, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x01,0x04,0x01,0x03,0x00,0x00,0x00,0x00}}, /* Track Number */
430  { 0x4B01, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x30,0x04,0x05,0x00,0x00,0x00,0x00}}, /* Edit Rate */
431  { 0x4B02, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x03,0x01,0x03,0x00,0x00}}, /* Origin */
432  { 0x4803, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x02,0x04,0x00,0x00}}, /* Sequence reference */
433  // Sequence
434  { 0x0201, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x07,0x01,0x00,0x00,0x00,0x00,0x00}}, /* Data Definition UL */
435  { 0x0202, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x02,0x01,0x01,0x03,0x00,0x00}}, /* Duration */
436  { 0x1001, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x06,0x09,0x00,0x00}}, /* Structural Components reference array */
437  // Source Clip
438  { 0x1201, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x03,0x01,0x04,0x00,0x00}}, /* Start position */
439  { 0x1101, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x03,0x01,0x00,0x00,0x00}}, /* SourcePackageID */
440  { 0x1102, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x03,0x02,0x00,0x00,0x00}}, /* SourceTrackID */
441  // Timecode Component
442  { 0x1501, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x03,0x01,0x05,0x00,0x00}}, /* Start Time Code */
443  { 0x1502, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x04,0x01,0x01,0x02,0x06,0x00,0x00}}, /* Rounded Time Code Base */
444  { 0x1503, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x04,0x01,0x01,0x05,0x00,0x00,0x00}}, /* Drop Frame */
445  // File Descriptor
446  { 0x3F01, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x06,0x01,0x01,0x04,0x06,0x0B,0x00,0x00}}, /* Sub Descriptors reference array */
447  { 0x3006, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x06,0x01,0x01,0x03,0x05,0x00,0x00,0x00}}, /* Linked Track ID */
448  { 0x3001, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x06,0x01,0x01,0x00,0x00,0x00,0x00}}, /* SampleRate */
449  { 0x3002, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x06,0x01,0x02,0x00,0x00,0x00,0x00}}, /* ContainerDuration */
450  { 0x3004, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x01,0x02,0x00,0x00}}, /* Essence Container */
451  // Generic Picture Essence Descriptor
452  { 0x320C, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x03,0x01,0x04,0x00,0x00,0x00}}, /* Frame Layout */
453  { 0x320D, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x01,0x03,0x02,0x05,0x00,0x00,0x00}}, /* Video Line Map */
454  { 0x3203, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x02,0x02,0x00,0x00,0x00}}, /* Stored Width */
455  { 0x3202, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x02,0x01,0x00,0x00,0x00}}, /* Stored Height */
456  { 0x3216, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x01,0x03,0x02,0x08,0x00,0x00,0x00}}, /* Stored F2 Offset */
457  { 0x3205, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x01,0x08,0x00,0x00,0x00}}, /* Sampled Width */
458  { 0x3204, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x01,0x07,0x00,0x00,0x00}}, /* Sampled Height */
459  { 0x3206, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x01,0x09,0x00,0x00,0x00}}, /* Sampled X Offset */
460  { 0x3207, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x01,0x0A,0x00,0x00,0x00}}, /* Sampled Y Offset */
461  { 0x3209, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x01,0x0C,0x00,0x00,0x00}}, /* Display Width */
462  { 0x3208, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x01,0x0B,0x00,0x00,0x00}}, /* Display Height */
463  { 0x320A, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x01,0x0D,0x00,0x00,0x00}}, /* Display X offset */
464  { 0x320B, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x01,0x0E,0x00,0x00,0x00}}, /* Presentation Y offset */
465  { 0x3217, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x01,0x03,0x02,0x07,0x00,0x00,0x00}}, /* Display F2 offset */
466  { 0x320E, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x01,0x01,0x01,0x00,0x00,0x00}}, /* Aspect Ratio */
467  { 0x3210, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x01,0x02,0x01,0x01,0x01,0x02,0x00}}, /* Transfer characteristic */
468  { 0x3213, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x18,0x01,0x02,0x00,0x00,0x00,0x00}}, /* Image Start Offset */
469  { 0x3214, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x18,0x01,0x03,0x00,0x00,0x00,0x00}}, /* Image End Offset */
470  { 0x3201, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x01,0x06,0x01,0x00,0x00,0x00,0x00}}, /* Picture Essence Coding */
471  { 0x3212, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x01,0x03,0x01,0x06,0x00,0x00,0x00}}, /* Field Dominance (Opt) */
472  { 0x3215, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x05,0x01,0x13,0x00,0x00,0x00,0x00}}, /* Signal Standard */
473  // CDCI Picture Essence Descriptor
474  { 0x3301, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x01,0x05,0x03,0x0A,0x00,0x00,0x00}}, /* Component Depth */
475  { 0x3302, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x01,0x05,0x00,0x00,0x00}}, /* Horizontal Subsampling */
476  { 0x3308, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x01,0x05,0x01,0x10,0x00,0x00,0x00}}, /* Vertical Subsampling */
477  { 0x3303, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x01,0x06,0x00,0x00,0x00}}, /* Color Siting */
478  { 0x3307, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x18,0x01,0x04,0x00,0x00,0x00,0x00}}, /* Padding Bits */
479  { 0x3304, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x03,0x03,0x00,0x00,0x00}}, /* Black Ref level */
480  { 0x3305, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x03,0x04,0x00,0x00,0x00}}, /* White Ref level */
481  { 0x3306, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x01,0x05,0x03,0x05,0x00,0x00,0x00}}, /* Color Range */
482  // Generic Sound Essence Descriptor
483  { 0x3D02, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x04,0x02,0x03,0x01,0x04,0x00,0x00,0x00}}, /* Locked/Unlocked */
484  { 0x3D03, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x02,0x03,0x01,0x01,0x01,0x00,0x00}}, /* Audio sampling rate */
485  { 0x3D04, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x02,0x01,0x01,0x03,0x00,0x00,0x00}}, /* Audio Ref Level */
486  { 0x3D07, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x02,0x01,0x01,0x04,0x00,0x00,0x00}}, /* ChannelCount */
487  { 0x3D01, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x04,0x02,0x03,0x03,0x04,0x00,0x00,0x00}}, /* Quantization bits */
488  { 0x3D06, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x02,0x04,0x02,0x00,0x00,0x00,0x00}}, /* Sound Essence Compression */
489  // Index Table Segment
490  { 0x3F0B, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x05,0x30,0x04,0x06,0x00,0x00,0x00,0x00}}, /* Index Edit Rate */
491  { 0x3F0C, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x07,0x02,0x01,0x03,0x01,0x0A,0x00,0x00}}, /* Index Start Position */
492  { 0x3F0D, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x07,0x02,0x02,0x01,0x01,0x02,0x00,0x00}}, /* Index Duration */
493  { 0x3F05, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x04,0x06,0x02,0x01,0x00,0x00,0x00,0x00}}, /* Edit Unit Byte Count */
494  { 0x3F06, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x01,0x03,0x04,0x05,0x00,0x00,0x00,0x00}}, /* IndexSID */
495  { 0x3F08, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x04,0x04,0x04,0x01,0x01,0x00,0x00,0x00}}, /* Slice Count */
496  { 0x3F09, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x04,0x04,0x01,0x06,0x00,0x00,0x00}}, /* Delta Entry Array */
497  { 0x3F0A, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x04,0x04,0x02,0x05,0x00,0x00,0x00}}, /* Index Entry Array */
498  // MPEG video Descriptor
499  { 0x8000, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x01,0x06,0x02,0x01,0x0B,0x00,0x00}}, /* BitRate */
500  { 0x8003, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x01,0x06,0x02,0x01,0x05,0x00,0x00}}, /* LowDelay */
501  { 0x8004, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x01,0x06,0x02,0x01,0x06,0x00,0x00}}, /* ClosedGOP */
502  { 0x8006, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x01,0x06,0x02,0x01,0x08,0x00,0x00}}, /* MaxGOP */
503  { 0x8007, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x01,0x06,0x02,0x01,0x0A,0x00,0x00}}, /* ProfileAndLevel */
504  { 0x8008, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x01,0x06,0x02,0x01,0x09,0x00,0x00}}, /* BPictureCount */
505  // Wave Audio Essence Descriptor
506  { 0x3D09, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x02,0x03,0x03,0x05,0x00,0x00,0x00}}, /* Average Bytes Per Second */
507  { 0x3D0A, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x02,0x03,0x02,0x01,0x00,0x00,0x00}}, /* Block Align */
508 };
509 
511  { 0x8100, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x09,0x06,0x01,0x01,0x04,0x06,0x10,0x00,0x00}}, /* SubDescriptors */
512  { 0x8200, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x0E,0x04,0x01,0x06,0x06,0x01,0x0E,0x00,0x00}}, /* AVC Decoding Delay */
513  { 0x8201, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x0E,0x04,0x01,0x06,0x06,0x01,0x0A,0x00,0x00}}, /* AVC Profile */
514  { 0x8202, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x0E,0x04,0x01,0x06,0x06,0x01,0x0D,0x00,0x00}}, /* AVC Level */
515 };
516 
518  { 0x4406, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x03,0x02,0x01,0x02,0x0C,0x00,0x00,0x00}}, /* User Comments */
519  { 0x5001, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x03,0x02,0x01,0x02,0x09,0x01,0x00,0x00}}, /* Name */
520  { 0x5003, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x03,0x02,0x01,0x02,0x0A,0x01,0x00,0x00}}, /* Value */
521 };
522 
524 {
525  avio_write(pb, uuid_base, 12);
526  avio_wb16(pb, type);
527  avio_wb16(pb, value);
528 }
529 
530 static void mxf_write_umid(AVFormatContext *s, int type)
531 {
532  MXFContext *mxf = s->priv_data;
533  avio_write(s->pb, umid_ul, 13);
534  avio_wb24(s->pb, mxf->instance_number);
535  avio_write(s->pb, mxf->umid, 15);
536  avio_w8(s->pb, type);
537 }
538 
539 static void mxf_write_refs_count(AVIOContext *pb, int ref_count)
540 {
541  avio_wb32(pb, ref_count);
542  avio_wb32(pb, 16);
543 }
544 
545 static int klv_ber_length(uint64_t len)
546 {
547  if (len < 128)
548  return 1;
549  else
550  return (av_log2(len) >> 3) + 2;
551 }
552 
553 static int klv_encode_ber_length(AVIOContext *pb, uint64_t len)
554 {
555  // Determine the best BER size
556  int size = klv_ber_length(len);
557  if (size == 1) {
558  //short form
559  avio_w8(pb, len);
560  return 1;
561  }
562 
563  size --;
564  // long form
565  avio_w8(pb, 0x80 + size);
566  while(size) {
567  size--;
568  avio_w8(pb, len >> 8 * size & 0xff);
569  }
570  return 0;
571 }
572 
574 {
575  avio_w8(pb, 0x80 + 3);
576  avio_wb24(pb, len);
577 }
578 
579 static void klv_encode_ber9_length(AVIOContext *pb, uint64_t len)
580 {
581  avio_w8(pb, 0x80 + 8);
582  avio_wb64(pb, len);
583 }
584 
585 /*
586  * Get essence container ul index
587  */
589 {
590  int i;
591  for (i = 0; mxf_essence_mappings[i].id; i++)
592  if (mxf_essence_mappings[i].id == id)
593  return mxf_essence_mappings[i].index;
594  return -1;
595 }
596 
597 static void mxf_write_local_tags(AVIOContext *pb, const MXFLocalTagPair *local_tags, int count)
598 {
599  int i;
600  for (i = 0; i < count; i++) {
601  avio_wb16(pb, local_tags[i].local_tag);
602  avio_write(pb, local_tags[i].uid, 16);
603  }
604 }
605 
607 {
608  MXFContext *mxf = s->priv_data;
609  AVIOContext *pb = s->pb;
610  int local_tag_number, i = 0;
611  int avc_tags_count = 0;
612 
613  local_tag_number = FF_ARRAY_ELEMS(mxf_local_tag_batch);
614  local_tag_number += mxf->store_user_comments * FF_ARRAY_ELEMS(mxf_user_comments_local_tag);
615 
616  for (i = 0; i < s->nb_streams; i++) {
617  MXFStreamContext *sc = s->streams[i]->priv_data;
618  if (s->streams[i]->codecpar->codec_id == AV_CODEC_ID_H264 && !sc->avc_intra) {
619  avc_tags_count = FF_ARRAY_ELEMS(mxf_avc_subdescriptor_local_tags);
620  local_tag_number += avc_tags_count;
621  }
622  }
623 
624  avio_write(pb, primer_pack_key, 16);
625  klv_encode_ber_length(pb, local_tag_number * 18 + 8);
626 
627  avio_wb32(pb, local_tag_number); // local_tag num
628  avio_wb32(pb, 18); // item size, always 18 according to the specs
629 
630  for (i = 0; i < FF_ARRAY_ELEMS(mxf_local_tag_batch); i++) {
631  avio_wb16(pb, mxf_local_tag_batch[i].local_tag);
632  avio_write(pb, mxf_local_tag_batch[i].uid, 16);
633  }
634  if (mxf->store_user_comments)
635  for (i = 0; i < FF_ARRAY_ELEMS(mxf_user_comments_local_tag); i++) {
636  avio_wb16(pb, mxf_user_comments_local_tag[i].local_tag);
637  avio_write(pb, mxf_user_comments_local_tag[i].uid, 16);
638  }
639  if (avc_tags_count > 0)
640  mxf_write_local_tags(pb, mxf_avc_subdescriptor_local_tags, avc_tags_count);
641 }
642 
643 static void mxf_write_local_tag(AVIOContext *pb, int size, int tag)
644 {
645  avio_wb16(pb, tag);
646  avio_wb16(pb, size);
647 }
648 
649 static void mxf_write_metadata_key(AVIOContext *pb, unsigned int value)
650 {
652  avio_wb24(pb, value);
653 }
654 
655 static void mxf_free(AVFormatContext *s)
656 {
657  int i;
658 
659  for (i = 0; i < s->nb_streams; i++) {
660  AVStream *st = s->streams[i];
661  av_freep(&st->priv_data);
662  }
663 }
664 
666 {
668  while (uls->uid[0]) {
669  if (type == uls->id)
670  break;
671  uls++;
672  }
673  return uls;
674 }
675 
676 //one EC -> one descriptor. N ECs -> MultipleDescriptor + N descriptors
677 #define DESCRIPTOR_COUNT(essence_container_count) \
678  (essence_container_count > 1 ? essence_container_count + 1 : essence_container_count)
679 
681 {
682  MXFContext *c = s->priv_data;
683  AVIOContext *pb = s->pb;
684  int i;
685 
687  av_log(s,AV_LOG_DEBUG, "essence container count:%d\n", c->essence_container_count);
688  for (i = 0; i < s->nb_streams; i++) {
689  MXFStreamContext *sc = s->streams[i]->priv_data;
690  // check first track of essence container type and only write it once
691  if (sc->track_essence_element_key[15] != 0)
692  continue;
693  avio_write(pb, mxf_essence_container_uls[sc->index].container_ul, 16);
694  if (c->essence_container_count == 1)
695  break;
696  }
697 
698  if (c->essence_container_count > 1)
699  avio_write(pb, multiple_desc_ul, 16);
700 }
701 
703 {
704  MXFContext *mxf = s->priv_data;
705  AVIOContext *pb = s->pb;
706 
707  mxf_write_metadata_key(pb, 0x012f00);
708  PRINT_KEY(s, "preface key", pb->buf_ptr - 16);
710 
711  // write preface set uid
712  mxf_write_local_tag(pb, 16, 0x3C0A);
713  mxf_write_uuid(pb, Preface, 0);
714  PRINT_KEY(s, "preface uid", pb->buf_ptr - 16);
715 
716  // last modified date
717  mxf_write_local_tag(pb, 8, 0x3B02);
718  avio_wb64(pb, mxf->timestamp);
719 
720  // write version
721  mxf_write_local_tag(pb, 2, 0x3B05);
722  avio_wb16(pb, 259); // v1.3
723 
724  // Object Model Version
725  mxf_write_local_tag(pb, 4, 0x3B07);
726  avio_wb32(pb, 1);
727 
728  // write identification_refs
729  mxf_write_local_tag(pb, 16 + 8, 0x3B06);
730  mxf_write_refs_count(pb, 1);
732 
733  // write content_storage_refs
734  mxf_write_local_tag(pb, 16, 0x3B03);
736 
737  // operational pattern
738  mxf_write_local_tag(pb, 16, 0x3B09);
739  if (s->oformat == &ff_mxf_opatom_muxer)
740  avio_write(pb, opatom_ul, 16);
741  else
742  avio_write(pb, op1a_ul, 16);
743 
744  // write essence_container_refs
745  mxf_write_local_tag(pb, 8 + 16LL * DESCRIPTOR_COUNT(mxf->essence_container_count), 0x3B0A);
747 
748  // write dm_scheme_refs
749  mxf_write_local_tag(pb, 8, 0x3B0B);
750  avio_wb64(pb, 0);
751 }
752 
753 /*
754  * Returns the length of the UTF-16 string, in 16-bit characters, that would result
755  * from decoding the utf-8 string.
756  */
757 static uint64_t mxf_utf16len(const char *utf8_str)
758 {
759  const uint8_t *q = utf8_str;
760  uint64_t size = 0;
761  while (*q) {
762  uint32_t ch;
763  GET_UTF8(ch, *q++, goto invalid;)
764  if (ch < 0x10000)
765  size++;
766  else
767  size += 2;
768  continue;
769 invalid:
770  av_log(NULL, AV_LOG_ERROR, "Invalid UTF8 sequence in mxf_utf16len\n\n");
771  }
772  size += 1;
773  return size;
774 }
775 
776 /*
777  * Returns the calculated length a local tag containing an utf-8 string as utf-16
778  */
779 static int mxf_utf16_local_tag_length(const char *utf8_str)
780 {
781  uint64_t size;
782 
783  if (!utf8_str)
784  return 0;
785 
786  size = mxf_utf16len(utf8_str);
787  if (size >= UINT16_MAX/2) {
788  av_log(NULL, AV_LOG_ERROR, "utf16 local tag size %"PRIx64" invalid (too large), ignoring\n", size);
789  return 0;
790  }
791 
792  return 4 + size * 2;
793 }
794 
795 /*
796  * Write a local tag containing an utf-8 string as utf-16
797  */
798 static void mxf_write_local_tag_utf16(AVIOContext *pb, int tag, const char *value)
799 {
800  uint64_t size = mxf_utf16len(value);
801 
802  if (size >= UINT16_MAX/2) {
803  av_log(NULL, AV_LOG_ERROR, "utf16 local tag size %"PRIx64" invalid (too large), ignoring\n", size);
804  return;
805  }
806 
807  mxf_write_local_tag(pb, size*2, tag);
808  avio_put_str16be(pb, value);
809 }
810 
812  AVIOContext *pb = s->pb;
813 
814  if (s->flags & AVFMT_FLAG_BITEXACT) {
815  avio_wb16(pb, 0); // major
816  avio_wb16(pb, 0); // minor
817  avio_wb16(pb, 0); // tertiary
818  } else {
819  avio_wb16(pb, LIBAVFORMAT_VERSION_MAJOR); // major
820  avio_wb16(pb, LIBAVFORMAT_VERSION_MINOR); // minor
821  avio_wb16(pb, LIBAVFORMAT_VERSION_MICRO); // tertiary
822  }
823  avio_wb16(pb, 0); // patch
824  avio_wb16(pb, 0); // release
825 }
826 
828 {
829  MXFContext *mxf = s->priv_data;
830  AVIOContext *pb = s->pb;
831  const char *company = "FFmpeg";
832  const char *product = s->oformat != &ff_mxf_opatom_muxer ? "OP1a Muxer" : "OPAtom Muxer";
833  const char *version;
834  int length;
835 
836  mxf_write_metadata_key(pb, 0x013000);
837  PRINT_KEY(s, "identification key", pb->buf_ptr - 16);
838 
839  version = s->flags & AVFMT_FLAG_BITEXACT ?
841  length = 100 +mxf_utf16_local_tag_length(company) +
842  mxf_utf16_local_tag_length(product) +
844  klv_encode_ber_length(pb, length);
845 
846  // write uid
847  mxf_write_local_tag(pb, 16, 0x3C0A);
849  PRINT_KEY(s, "identification uid", pb->buf_ptr - 16);
850 
851  // write generation uid
852  mxf_write_local_tag(pb, 16, 0x3C09);
854  mxf_write_local_tag_utf16(pb, 0x3C01, company); // Company Name
855  mxf_write_local_tag_utf16(pb, 0x3C02, product); // Product Name
856 
857  mxf_write_local_tag(pb, 10, 0x3C03); // Product Version
858  store_version(s);
859 
860  mxf_write_local_tag_utf16(pb, 0x3C04, version); // Version String
861 
862  // write product uid
863  mxf_write_local_tag(pb, 16, 0x3C05);
865 
866  // modification date
867  mxf_write_local_tag(pb, 8, 0x3C06);
868  avio_wb64(pb, mxf->timestamp);
869 
870  mxf_write_local_tag(pb, 10, 0x3C07); // Toolkit Version
871  store_version(s);
872 }
873 
874 static void mxf_write_content_storage(AVFormatContext *s, MXFPackage *packages, int package_count)
875 {
876  AVIOContext *pb = s->pb;
877  int i;
878 
879  mxf_write_metadata_key(pb, 0x011800);
880  PRINT_KEY(s, "content storage key", pb->buf_ptr - 16);
881  klv_encode_ber_length(pb, 60 + (16 * package_count));
882 
883  // write uid
884  mxf_write_local_tag(pb, 16, 0x3C0A);
886  PRINT_KEY(s, "content storage uid", pb->buf_ptr - 16);
887 
888  // write package reference
889  mxf_write_local_tag(pb, 16 * package_count + 8, 0x1901);
890  mxf_write_refs_count(pb, package_count);
891  for (i = 0; i < package_count; i++) {
892  mxf_write_uuid(pb, packages[i].type, packages[i].instance);
893  }
894 
895  // write essence container data
896  mxf_write_local_tag(pb, 8 + 16, 0x1902);
897  mxf_write_refs_count(pb, 1);
899 }
900 
901 static void mxf_write_track(AVFormatContext *s, AVStream *st, MXFPackage *package)
902 {
903  MXFContext *mxf = s->priv_data;
904  AVIOContext *pb = s->pb;
905  MXFStreamContext *sc = st->priv_data;
906 
907  mxf_write_metadata_key(pb, 0x013b00);
908  PRINT_KEY(s, "track key", pb->buf_ptr - 16);
909  klv_encode_ber_length(pb, 80);
910 
911  // write track uid
912  mxf_write_local_tag(pb, 16, 0x3C0A);
914  PRINT_KEY(s, "track uid", pb->buf_ptr - 16);
915 
916  // write track id
917  mxf_write_local_tag(pb, 4, 0x4801);
918  avio_wb32(pb, st->index+2);
919 
920  // write track number
921  mxf_write_local_tag(pb, 4, 0x4804);
922  if (package->type == MaterialPackage)
923  avio_wb32(pb, 0); // track number of material package is 0
924  else
925  avio_write(pb, sc->track_essence_element_key + 12, 4);
926 
927  // write edit rate
928  mxf_write_local_tag(pb, 8, 0x4B01);
929 
930  if (st == mxf->timecode_track && s->oformat == &ff_mxf_opatom_muxer) {
931  avio_wb32(pb, mxf->tc.rate.num);
932  avio_wb32(pb, mxf->tc.rate.den);
933  } else {
934  avio_wb32(pb, mxf->time_base.den);
935  avio_wb32(pb, mxf->time_base.num);
936  }
937 
938  // write origin
939  mxf_write_local_tag(pb, 8, 0x4B02);
940  avio_wb64(pb, 0);
941 
942  // write sequence refs
943  mxf_write_local_tag(pb, 16, 0x4803);
945 }
946 
947 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 };
948 
950 {
951  MXFContext *mxf = s->priv_data;
952  AVIOContext *pb = s->pb;
953 
954  // find data define uls
955  mxf_write_local_tag(pb, 16, 0x0201);
956  if (st == mxf->timecode_track)
958  else {
959  const MXFCodecUL *data_def_ul = mxf_get_data_definition_ul(st->codecpar->codec_type);
960  avio_write(pb, data_def_ul->uid, 16);
961  }
962 
963  // write duration
964  mxf_write_local_tag(pb, 8, 0x0202);
965 
966  if (st != mxf->timecode_track && s->oformat == &ff_mxf_opatom_muxer && st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
967  avio_wb64(pb, mxf->body_offset / mxf->edit_unit_byte_count);
968  } else {
969  avio_wb64(pb, mxf->duration);
970  }
971 }
972 
974 {
975  MXFContext *mxf = s->priv_data;
976  AVIOContext *pb = s->pb;
977  enum MXFMetadataSetType component;
978 
979  mxf_write_metadata_key(pb, 0x010f00);
980  PRINT_KEY(s, "sequence key", pb->buf_ptr - 16);
981  klv_encode_ber_length(pb, 80);
982 
983  mxf_write_local_tag(pb, 16, 0x3C0A);
985 
986  PRINT_KEY(s, "sequence uid", pb->buf_ptr - 16);
988 
989  // write structural component
990  mxf_write_local_tag(pb, 16 + 8, 0x1001);
991  mxf_write_refs_count(pb, 1);
992  if (st == mxf->timecode_track)
993  component = TimecodeComponent;
994  else
995  component = SourceClip;
996 
997  mxf_write_uuid(pb, component, mxf->track_instance_count);
998 }
999 
1001 {
1002  MXFContext *mxf = s->priv_data;
1003  AVIOContext *pb = s->pb;
1004 
1005  mxf_write_metadata_key(pb, 0x011400);
1006  klv_encode_ber_length(pb, 75);
1007 
1008  // UID
1009  mxf_write_local_tag(pb, 16, 0x3C0A);
1011 
1012  mxf_write_common_fields(s, st);
1013 
1014  // Start Time Code
1015  mxf_write_local_tag(pb, 8, 0x1501);
1016  avio_wb64(pb, mxf->tc.start);
1017 
1018  // Rounded Time Code Base
1019  mxf_write_local_tag(pb, 2, 0x1502);
1020  avio_wb16(pb, mxf->timecode_base);
1021 
1022  // Drop Frame
1023  mxf_write_local_tag(pb, 1, 0x1503);
1024  avio_w8(pb, !!(mxf->tc.flags & AV_TIMECODE_FLAG_DROPFRAME));
1025 }
1026 
1028 {
1029  MXFContext *mxf = s->priv_data;
1030  AVIOContext *pb = s->pb;
1031  int i;
1032 
1033  mxf_write_metadata_key(pb, 0x011100);
1034  PRINT_KEY(s, "sturctural component key", pb->buf_ptr - 16);
1035  klv_encode_ber_length(pb, 108);
1036 
1037  // write uid
1038  mxf_write_local_tag(pb, 16, 0x3C0A);
1040 
1041  PRINT_KEY(s, "structural component uid", pb->buf_ptr - 16);
1042  mxf_write_common_fields(s, st);
1043 
1044  // write start_position
1045  mxf_write_local_tag(pb, 8, 0x1201);
1046  avio_wb64(pb, 0);
1047 
1048  // write source package uid, end of the reference
1049  mxf_write_local_tag(pb, 32, 0x1101);
1050  if (!package->ref) {
1051  for (i = 0; i < 4; i++)
1052  avio_wb64(pb, 0);
1053  } else
1054  mxf_write_umid(s, package->ref->instance);
1055 
1056  // write source track id
1057  mxf_write_local_tag(pb, 4, 0x1102);
1058  if (package->type == SourcePackage && !package->ref)
1059  avio_wb32(pb, 0);
1060  else
1061  avio_wb32(pb, st->index+2);
1062 }
1063 
1065 {
1066  AVIOContext *pb = s->pb;
1067 
1068  mxf_write_metadata_key(pb, 0x012e00);
1069  PRINT_KEY(s, "tape descriptor key", pb->buf_ptr - 16);
1070  klv_encode_ber_length(pb, 20);
1071  mxf_write_local_tag(pb, 16, 0x3C0A);
1073  PRINT_KEY(s, "tape_desc uid", pb->buf_ptr - 16);
1074 }
1075 
1076 
1078 {
1079  MXFContext *mxf = s->priv_data;
1080  AVIOContext *pb = s->pb;
1081  const uint8_t *ul;
1082  int i;
1083 
1084  mxf_write_metadata_key(pb, 0x014400);
1085  PRINT_KEY(s, "multiple descriptor key", pb->buf_ptr - 16);
1086  klv_encode_ber_length(pb, 64 + 16LL * s->nb_streams);
1087 
1088  mxf_write_local_tag(pb, 16, 0x3C0A);
1090  PRINT_KEY(s, "multi_desc uid", pb->buf_ptr - 16);
1091 
1092  // write sample rate
1093  mxf_write_local_tag(pb, 8, 0x3001);
1094  avio_wb32(pb, mxf->time_base.den);
1095  avio_wb32(pb, mxf->time_base.num);
1096 
1097  // write essence container ul
1098  mxf_write_local_tag(pb, 16, 0x3004);
1099  if (mxf->essence_container_count > 1)
1100  ul = multiple_desc_ul;
1101  else {
1102  MXFStreamContext *sc = s->streams[0]->priv_data;
1103  ul = mxf_essence_container_uls[sc->index].container_ul;
1104  }
1105  avio_write(pb, ul, 16);
1106 
1107  // write sub descriptor refs
1108  mxf_write_local_tag(pb, s->nb_streams * 16 + 8, 0x3F01);
1110  for (i = 0; i < s->nb_streams; i++)
1111  mxf_write_uuid(pb, SubDescriptor, i);
1112 }
1113 
1115 {
1116  MXFContext *mxf = s->priv_data;
1117  MXFStreamContext *sc = st->priv_data;
1118  AVIOContext *pb = s->pb;
1119  int64_t pos;
1120 
1121  avio_write(pb, key, 16);
1122  klv_encode_ber4_length(pb, 0);
1123  pos = avio_tell(pb);
1124 
1125  mxf_write_local_tag(pb, 16, 0x3C0A);
1127 
1128  mxf_write_local_tag(pb, 4, 0x3006);
1129  avio_wb32(pb, st->index+2);
1130 
1131  mxf_write_local_tag(pb, 8, 0x3001);
1132  if (s->oformat == &ff_mxf_d10_muxer) {
1133  avio_wb32(pb, mxf->time_base.den);
1134  avio_wb32(pb, mxf->time_base.num);
1135  } else {
1136  if (st->codecpar->codec_id == AV_CODEC_ID_PCM_S16LE ||
1138  avio_wb32(pb, st->codecpar->sample_rate);
1139  avio_wb32(pb, 1);
1140  } else {
1141  avio_wb32(pb, mxf->time_base.den);
1142  avio_wb32(pb, mxf->time_base.num);
1143  }
1144  }
1145 
1146  mxf_write_local_tag(pb, 16, 0x3004);
1147  avio_write(pb, mxf_essence_container_uls[sc->index].container_ul, 16);
1148 
1149  return pos;
1150 }
1151 
1152 static const UID mxf_s436m_anc_descriptor_key = { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x5c,0x00 };
1153 static const UID mxf_mpegvideo_descriptor_key = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x51,0x00 };
1154 static const UID mxf_wav_descriptor_key = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x48,0x00 };
1155 static const UID mxf_aes3_descriptor_key = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x47,0x00 };
1156 static const UID mxf_cdci_descriptor_key = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0D,0x01,0x01,0x01,0x01,0x01,0x28,0x00 };
1157 static const UID mxf_generic_sound_descriptor_key = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0D,0x01,0x01,0x01,0x01,0x01,0x42,0x00 };
1158 
1159 static const UID mxf_avc_subdescriptor_key = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x6E,0x00 };
1160 
1162 {
1163  switch (trc){
1164  case AVCOL_TRC_GAMMA28 :
1165  case AVCOL_TRC_GAMMA22 :
1166  memcpy(ul, ((UID){0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x01,0x01,0x01,0x01,0x00,0x00}), 16);
1167  return 0;
1168  case AVCOL_TRC_BT709 :
1169  case AVCOL_TRC_SMPTE170M :
1170  memcpy(ul, ((UID){0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x01,0x01,0x01,0x02,0x00,0x00}), 16);
1171  return 0;
1172  case AVCOL_TRC_SMPTE240M :
1173  memcpy(ul, ((UID){0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x01,0x01,0x01,0x03,0x00,0x00}), 16);
1174  return 0;
1175  case AVCOL_TRC_BT1361_ECG:
1176  memcpy(ul, ((UID){0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x06,0x04,0x01,0x01,0x01,0x01,0x05,0x00,0x00}), 16);
1177  return 0;
1178  case AVCOL_TRC_LINEAR :
1179  memcpy(ul, ((UID){0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x06,0x04,0x01,0x01,0x01,0x01,0x06,0x00,0x00}), 16);
1180  return 0;
1181  case AVCOL_TRC_SMPTE428 :
1182  memcpy(ul, ((UID){0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x08,0x04,0x01,0x01,0x01,0x01,0x07,0x00,0x00}), 16);
1183  return 0;
1184  default:
1185  return -1;
1186  }
1187 }
1188 
1189 static int64_t mxf_write_cdci_common(AVFormatContext *s, AVStream *st, const UID key)
1190 {
1191  MXFStreamContext *sc = st->priv_data;
1192  AVIOContext *pb = s->pb;
1193  int stored_width = 0;
1194  int stored_height = (st->codecpar->height+15)/16*16;
1195  int display_height;
1196  int f1, f2;
1197  UID transfer_ul = {0};
1198  int64_t pos = mxf_write_generic_desc(s, st, key);
1199 
1200  get_trc(transfer_ul, st->codecpar->color_trc);
1201 
1202  if (st->codecpar->codec_id == AV_CODEC_ID_DVVIDEO) {
1203  if (st->codecpar->height == 1080)
1204  stored_width = 1920;
1205  else if (st->codecpar->height == 720)
1206  stored_width = 1280;
1207  }
1208  if (!stored_width)
1209  stored_width = (st->codecpar->width+15)/16*16;
1210 
1211  mxf_write_local_tag(pb, 4, 0x3203);
1212  avio_wb32(pb, stored_width);
1213 
1214  mxf_write_local_tag(pb, 4, 0x3202);
1215  avio_wb32(pb, stored_height>>sc->interlaced);
1216 
1217  if (s->oformat == &ff_mxf_d10_muxer) {
1218  //Stored F2 Offset
1219  mxf_write_local_tag(pb, 4, 0x3216);
1220  avio_wb32(pb, 0);
1221 
1222  //Image Start Offset
1223  mxf_write_local_tag(pb, 4, 0x3213);
1224  avio_wb32(pb, 0);
1225 
1226  //Image End Offset
1227  mxf_write_local_tag(pb, 4, 0x3214);
1228  avio_wb32(pb, 0);
1229  }
1230 
1231  //Sampled width
1232  mxf_write_local_tag(pb, 4, 0x3205);
1233  avio_wb32(pb, stored_width);
1234 
1235  //Samples height
1236  mxf_write_local_tag(pb, 4, 0x3204);
1237  avio_wb32(pb, st->codecpar->height>>sc->interlaced);
1238 
1239  //Sampled X Offset
1240  mxf_write_local_tag(pb, 4, 0x3206);
1241  avio_wb32(pb, 0);
1242 
1243  //Sampled Y Offset
1244  mxf_write_local_tag(pb, 4, 0x3207);
1245  avio_wb32(pb, 0);
1246 
1247  mxf_write_local_tag(pb, 4, 0x3209);
1248  avio_wb32(pb, stored_width);
1249 
1250  if (st->codecpar->height == 608) // PAL + VBI
1251  display_height = 576;
1252  else if (st->codecpar->height == 512) // NTSC + VBI
1253  display_height = 486;
1254  else
1255  display_height = st->codecpar->height;
1256 
1257  mxf_write_local_tag(pb, 4, 0x3208);
1258  avio_wb32(pb, display_height>>sc->interlaced);
1259 
1260  // display X offset
1261  mxf_write_local_tag(pb, 4, 0x320A);
1262  avio_wb32(pb, 0);
1263 
1264  // display Y offset
1265  mxf_write_local_tag(pb, 4, 0x320B);
1266  avio_wb32(pb, (st->codecpar->height - display_height)>>sc->interlaced);
1267 
1268  if (sc->interlaced) {
1269  //Display F2 Offset
1270  mxf_write_local_tag(pb, 4, 0x3217);
1271  avio_wb32(pb, -((st->codecpar->height - display_height)&1));
1272  }
1273 
1274  // component depth
1275  mxf_write_local_tag(pb, 4, 0x3301);
1276  avio_wb32(pb, sc->component_depth);
1277 
1278  // horizontal subsampling
1279  mxf_write_local_tag(pb, 4, 0x3302);
1280  avio_wb32(pb, sc->h_chroma_sub_sample);
1281 
1282  // vertical subsampling
1283  mxf_write_local_tag(pb, 4, 0x3308);
1284  avio_wb32(pb, sc->v_chroma_sub_sample);
1285 
1286  // color siting
1287  mxf_write_local_tag(pb, 1, 0x3303);
1288  avio_w8(pb, sc->color_siting);
1289 
1290  // Padding Bits
1291  mxf_write_local_tag(pb, 2, 0x3307);
1292  avio_wb16(pb, 0);
1293 
1295  int black = 0,
1296  white = (1<<sc->component_depth) - 1,
1297  color = (1<<sc->component_depth) - 1;
1298  if (st->codecpar->color_range == AVCOL_RANGE_MPEG) {
1299  black = 1 << (sc->component_depth - 4);
1300  white = 235 << (sc->component_depth - 8);
1301  color = (14 << (sc->component_depth - 4)) + 1;
1302  }
1303  mxf_write_local_tag(pb, 4, 0x3304);
1304  avio_wb32(pb, black);
1305  mxf_write_local_tag(pb, 4, 0x3305);
1306  avio_wb32(pb, white);
1307  mxf_write_local_tag(pb, 4, 0x3306);
1308  avio_wb32(pb, color);
1309  }
1310 
1311  if (sc->signal_standard) {
1312  mxf_write_local_tag(pb, 1, 0x3215);
1313  avio_w8(pb, sc->signal_standard);
1314  }
1315 
1316  // frame layout
1317  mxf_write_local_tag(pb, 1, 0x320C);
1318  avio_w8(pb, sc->interlaced);
1319 
1320  // video line map
1321  switch (st->codecpar->height) {
1322  case 576: f1 = 23; f2 = st->codecpar->codec_id == AV_CODEC_ID_DVVIDEO ? 335 : 336; break;
1323  case 608: f1 = 7; f2 = 320; break;
1324  case 480: f1 = 20; f2 = st->codecpar->codec_id == AV_CODEC_ID_DVVIDEO ? 285 : 283; break;
1325  case 512: f1 = 7; f2 = 270; break;
1326  case 720: f1 = 26; f2 = 0; break; // progressive
1327  case 1080: f1 = 21; f2 = 584; break;
1328  default: f1 = 0; f2 = 0; break;
1329  }
1330 
1331  if (!sc->interlaced && f2) {
1332  f2 = 0;
1333  f1 *= 2;
1334  }
1335 
1336 
1337  mxf_write_local_tag(pb, 16, 0x320D);
1338  avio_wb32(pb, 2);
1339  avio_wb32(pb, 4);
1340  avio_wb32(pb, f1);
1341  avio_wb32(pb, f2);
1342 
1343  mxf_write_local_tag(pb, 8, 0x320E);
1344  avio_wb32(pb, sc->aspect_ratio.num);
1345  avio_wb32(pb, sc->aspect_ratio.den);
1346 
1347  //Transfer characteristic
1348  if (transfer_ul[0]) {
1349  mxf_write_local_tag(pb, 16, 0x3210);
1350  avio_write(pb, transfer_ul, 16);
1351  };
1352 
1353  mxf_write_local_tag(pb, 16, 0x3201);
1354  avio_write(pb, *sc->codec_ul, 16);
1355 
1356  if (sc->interlaced && sc->field_dominance) {
1357  mxf_write_local_tag(pb, 1, 0x3212);
1358  avio_w8(pb, sc->field_dominance);
1359  }
1360 
1361  if (st->codecpar->codec_id == AV_CODEC_ID_H264 && !sc->avc_intra) {
1362  // write avc sub descriptor ref
1363  mxf_write_local_tag(pb, 8 + 16, 0x8100);
1364  mxf_write_refs_count(pb, 1);
1366  }
1367 
1368  return pos;
1369 }
1370 
1371 static void mxf_update_klv_size(AVIOContext *pb, int64_t pos)
1372 {
1373  int64_t cur_pos = avio_tell(pb);
1374  int size = cur_pos - pos;
1375  avio_seek(pb, pos - 4, SEEK_SET);
1376  klv_encode_ber4_length(pb, size);
1377  avio_seek(pb, cur_pos, SEEK_SET);
1378 }
1379 
1381 {
1382  AVIOContext *pb = s->pb;
1383  int64_t pos;
1384 
1386  klv_encode_ber4_length(pb, 0);
1387  pos = avio_tell(pb);
1388 
1389  mxf_write_local_tag(pb, 16, 0x3C0A);
1391 
1392  mxf_write_local_tag(pb, 1, 0x8200);
1393  avio_w8(pb, 0xFF); // AVC Decoding Delay, unknown
1394 
1395  mxf_write_local_tag(pb, 1, 0x8201);
1396  avio_w8(pb, st->codecpar->profile); // AVC Profile
1397 
1398  mxf_write_local_tag(pb, 1, 0x8202);
1399  avio_w8(pb, st->codecpar->level); // AVC Level
1400 
1401  mxf_update_klv_size(s->pb, pos);
1402 }
1403 
1405 {
1406  int64_t pos = mxf_write_cdci_common(s, st, mxf_cdci_descriptor_key);
1407  mxf_update_klv_size(s->pb, pos);
1408 
1409  if (st->codecpar->codec_id == AV_CODEC_ID_H264) {
1410  mxf_write_avc_subdesc(s, st);
1411  }
1412 }
1413 
1415 {
1416  MXFStreamContext *sc = st->priv_data;
1417  if (sc->avc_intra) {
1418  mxf_write_mpegvideo_desc(s, st);
1419  } else {
1420  int64_t pos = mxf_write_cdci_common(s, st, mxf_cdci_descriptor_key);
1421  mxf_update_klv_size(s->pb, pos);
1422  mxf_write_avc_subdesc(s, st);
1423  }
1424 }
1425 
1427 {
1429  mxf_update_klv_size(s->pb, pos);
1430 }
1431 
1433 {
1434  AVIOContext *pb = s->pb;
1435  MXFStreamContext *sc = st->priv_data;
1436  int profile_and_level = (st->codecpar->profile<<4) | st->codecpar->level;
1438 
1439  if (st->codecpar->codec_id != AV_CODEC_ID_H264) {
1440  // bit rate
1441  mxf_write_local_tag(pb, 4, 0x8000);
1442  avio_wb32(pb, sc->video_bit_rate);
1443 
1444  // profile and level
1445  mxf_write_local_tag(pb, 1, 0x8007);
1446  if (!st->codecpar->profile)
1447  profile_and_level |= 0x80; // escape bit
1448  avio_w8(pb, profile_and_level);
1449 
1450  // low delay
1451  mxf_write_local_tag(pb, 1, 0x8003);
1452  avio_w8(pb, sc->low_delay);
1453 
1454  // closed gop
1455  mxf_write_local_tag(pb, 1, 0x8004);
1456  avio_w8(pb, sc->seq_closed_gop);
1457 
1458  // max gop
1459  mxf_write_local_tag(pb, 2, 0x8006);
1460  avio_wb16(pb, sc->max_gop);
1461 
1462  // b picture count
1463  mxf_write_local_tag(pb, 2, 0x8008);
1464  avio_wb16(pb, sc->b_picture_count);
1465  }
1466 
1467  mxf_update_klv_size(pb, pos);
1468 }
1469 
1471 {
1472  AVIOContext *pb = s->pb;
1473  MXFContext *mxf = s->priv_data;
1474  int show_warnings = !mxf->footer_partition_offset;
1475  int64_t pos = mxf_write_generic_desc(s, st, key);
1476 
1477  if (s->oformat == &ff_mxf_opatom_muxer) {
1478  mxf_write_local_tag(pb, 8, 0x3002);
1479  avio_wb64(pb, mxf->body_offset / mxf->edit_unit_byte_count);
1480  }
1481 
1482  // audio locked
1483  mxf_write_local_tag(pb, 1, 0x3D02);
1484  avio_w8(pb, 1);
1485 
1486  // write audio sampling rate
1487  mxf_write_local_tag(pb, 8, 0x3D03);
1488  avio_wb32(pb, st->codecpar->sample_rate);
1489  avio_wb32(pb, 1);
1490 
1491  if (s->oformat == &ff_mxf_d10_muxer) {
1492  mxf_write_local_tag(pb, 1, 0x3D04);
1493  avio_w8(pb, 0);
1494  }
1495 
1496  mxf_write_local_tag(pb, 4, 0x3D07);
1497  if (mxf->channel_count == -1) {
1498  if (show_warnings && (s->oformat == &ff_mxf_d10_muxer) && (st->codecpar->channels != 4) && (st->codecpar->channels != 8))
1499  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");
1500  avio_wb32(pb, st->codecpar->channels);
1501  } else if (s->oformat == &ff_mxf_d10_muxer) {
1502  if (show_warnings && (mxf->channel_count < st->codecpar->channels))
1503  av_log(s, AV_LOG_WARNING, "d10_channelcount < actual number of audio channels : some channels will be discarded\n");
1504  if (show_warnings && (mxf->channel_count != 4) && (mxf->channel_count != 8))
1505  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");
1506  avio_wb32(pb, mxf->channel_count);
1507  } else {
1508  avio_wb32(pb, st->codecpar->channels);
1509  }
1510 
1511  mxf_write_local_tag(pb, 4, 0x3D01);
1513 
1514  return pos;
1515 }
1516 
1517 static int64_t mxf_write_wav_common(AVFormatContext *s, AVStream *st, const UID key)
1518 {
1519  AVIOContext *pb = s->pb;
1520  int64_t pos = mxf_write_generic_sound_common(s, st, key);
1521 
1522  mxf_write_local_tag(pb, 2, 0x3D0A);
1523  avio_wb16(pb, st->codecpar->block_align);
1524 
1525  // avg bytes per sec
1526  mxf_write_local_tag(pb, 4, 0x3D09);
1528 
1529  return pos;
1530 }
1531 
1533 {
1534  int64_t pos = mxf_write_wav_common(s, st, mxf_wav_descriptor_key);
1535  mxf_update_klv_size(s->pb, pos);
1536 }
1537 
1539 {
1540  int64_t pos = mxf_write_wav_common(s, st, mxf_aes3_descriptor_key);
1541  mxf_update_klv_size(s->pb, pos);
1542 }
1543 
1545 {
1547  mxf_update_klv_size(s->pb, pos);
1548 }
1549 
1550 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 };
1551 
1552 static int mxf_write_tagged_value(AVFormatContext *s, const char* name, const char* value)
1553 {
1554  MXFContext *mxf = s->priv_data;
1555  AVIOContext *pb = s->pb;
1556  int name_size = mxf_utf16_local_tag_length(name);
1557  int indirect_value_size = 13 + mxf_utf16_local_tag_length(value);
1558 
1559  if (!name_size || indirect_value_size == 13)
1560  return 1;
1561 
1562  mxf_write_metadata_key(pb, 0x013f00);
1563  klv_encode_ber_length(pb, 24 + name_size + indirect_value_size);
1564 
1565  // write instance UID
1566  mxf_write_local_tag(pb, 16, 0x3C0A);
1568 
1569  // write name
1570  mxf_write_local_tag_utf16(pb, 0x5001, name); // Name
1571 
1572  // write indirect value
1573  mxf_write_local_tag(pb, indirect_value_size, 0x5003);
1575  avio_put_str16le(pb, value);
1576 
1577  mxf->tagged_value_count++;
1578  return 0;
1579 }
1580 
1582 {
1583  MXFContext *mxf = s->priv_data;
1584  AVDictionaryEntry *t = NULL;
1585  int count = 0;
1586 
1587  while ((t = av_dict_get(m, "comment_", t, AV_DICT_IGNORE_SUFFIX))) {
1588  if (mxf->tagged_value_count >= UINT16_MAX) {
1589  av_log(s, AV_LOG_ERROR, "too many tagged values, ignoring remaining\n");
1590  return count;
1591  }
1592 
1593  if (mxf_write_tagged_value(s, t->key + 8, t->value) == 0)
1594  count++;
1595  }
1596  return count;
1597 }
1598 
1600 {
1601  MXFContext *mxf = s->priv_data;
1602  AVIOContext *pb = s->pb;
1603  int i, track_count = s->nb_streams+1;
1604  int name_size = mxf_utf16_local_tag_length(package->name);
1605  int user_comment_count = 0;
1606 
1607  if (package->type == MaterialPackage) {
1608  if (mxf->store_user_comments)
1609  user_comment_count = mxf_write_user_comments(s, s->metadata);
1610  mxf_write_metadata_key(pb, 0x013600);
1611  PRINT_KEY(s, "Material Package key", pb->buf_ptr - 16);
1612  klv_encode_ber_length(pb, 92 + name_size + (16*track_count) + (16*user_comment_count) + 12LL*mxf->store_user_comments);
1613  } else {
1614  mxf_write_metadata_key(pb, 0x013700);
1615  PRINT_KEY(s, "Source Package key", pb->buf_ptr - 16);
1616  klv_encode_ber_length(pb, 112 + name_size + (16*track_count) + 12LL*mxf->store_user_comments); // 20 bytes length for descriptor reference
1617  }
1618 
1619  // write uid
1620  mxf_write_local_tag(pb, 16, 0x3C0A);
1621  mxf_write_uuid(pb, package->type, package->instance);
1622  av_log(s, AV_LOG_DEBUG, "package type:%d\n", package->type);
1623  PRINT_KEY(s, "package uid", pb->buf_ptr - 16);
1624 
1625  // write package umid
1626  mxf_write_local_tag(pb, 32, 0x4401);
1627  mxf_write_umid(s, package->instance);
1628  PRINT_KEY(s, "package umid second part", pb->buf_ptr - 16);
1629 
1630  // package name
1631  if (name_size)
1632  mxf_write_local_tag_utf16(pb, 0x4402, package->name);
1633 
1634  // package creation date
1635  mxf_write_local_tag(pb, 8, 0x4405);
1636  avio_wb64(pb, mxf->timestamp);
1637 
1638  // package modified date
1639  mxf_write_local_tag(pb, 8, 0x4404);
1640  avio_wb64(pb, mxf->timestamp);
1641 
1642  // write track refs
1643  mxf_write_local_tag(pb, track_count*16 + 8, 0x4403);
1644  mxf_write_refs_count(pb, track_count);
1645  // these are the uuids of the tracks the will be written in mxf_write_track
1646  for (i = 0; i < track_count; i++)
1647  mxf_write_uuid(pb, Track, mxf->track_instance_count + i);
1648 
1649  // write user comment refs
1650  if (mxf->store_user_comments) {
1651  mxf_write_local_tag(pb, user_comment_count*16 + 8, 0x4406);
1652  mxf_write_refs_count(pb, user_comment_count);
1653  for (i = 0; i < user_comment_count; i++)
1654  mxf_write_uuid(pb, TaggedValue, mxf->tagged_value_count - user_comment_count + i);
1655  }
1656 
1657  // write multiple descriptor reference
1658  if (package->type == SourcePackage && package->instance == 1) {
1659  mxf_write_local_tag(pb, 16, 0x4701);
1660  if (s->nb_streams > 1) {
1663  } else
1664  mxf_write_uuid(pb, SubDescriptor, 0);
1665  } else if (package->type == SourcePackage && package->instance == 2) {
1666  mxf_write_local_tag(pb, 16, 0x4701);
1669  }
1670 
1671  /*
1672  * for every 1 track in a package there is 1 sequence and 1 component.
1673  * all 3 of these elements share the same instance number for generating
1674  * there instance uuids. mxf->track_instance_count stores this value.
1675  * mxf->track_instance_count is incremented after a group of all 3 of
1676  * these elements are written.
1677  */
1678 
1679  // write timecode track
1680  mxf_write_track(s, mxf->timecode_track, package);
1681  mxf_write_sequence(s, mxf->timecode_track, package);
1682  mxf_write_timecode_component(s, mxf->timecode_track, package);
1683  mxf->track_instance_count++;
1684 
1685  for (i = 0; i < s->nb_streams; i++) {
1686  AVStream *st = s->streams[i];
1687  mxf_write_track(s, st, package);
1688  mxf_write_sequence(s, st, package);
1689  mxf_write_structural_component(s, st, package);
1690  mxf->track_instance_count++;
1691 
1692  if (package->type == SourcePackage && package->instance == 1) {
1693  MXFStreamContext *sc = st->priv_data;
1694  mxf_essence_container_uls[sc->index].write_desc(s, st);
1695  }
1696  }
1697 }
1698 
1700 {
1701  AVIOContext *pb = s->pb;
1702 
1703  mxf_write_metadata_key(pb, 0x012300);
1704  klv_encode_ber_length(pb, 72);
1705 
1706  mxf_write_local_tag(pb, 16, 0x3C0A); // Instance UID
1708 
1709  mxf_write_local_tag(pb, 32, 0x2701); // Linked Package UID
1710  mxf_write_umid(s, 1);
1711 
1712  mxf_write_local_tag(pb, 4, 0x3F07); // BodySID
1713  avio_wb32(pb, 1);
1714 
1715  mxf_write_local_tag(pb, 4, 0x3F06); // IndexSID
1716  avio_wb32(pb, 2);
1717 
1718  return 0;
1719 }
1720 
1722 {
1723  MXFContext *mxf = s->priv_data;
1724  AVDictionaryEntry *entry = NULL;
1725  AVStream *st = NULL;
1726  int i;
1727  MXFPackage packages[3] = {{0}};
1728  int package_count = 2;
1729  packages[0].type = MaterialPackage;
1730  packages[1].type = SourcePackage;
1731  packages[1].instance = 1;
1732  packages[0].ref = &packages[1];
1733 
1734 
1735  if (entry = av_dict_get(s->metadata, "material_package_name", NULL, 0))
1736  packages[0].name = entry->value;
1737 
1738  if (entry = av_dict_get(s->metadata, "file_package_name", NULL, 0)) {
1739  packages[1].name = entry->value;
1740  } else {
1741  /* check if any of the streams contain a file_package_name */
1742  for (i = 0; i < s->nb_streams; i++) {
1743  st = s->streams[i];
1744  if (entry = av_dict_get(st->metadata, "file_package_name", NULL, 0)) {
1745  packages[1].name = entry->value;
1746  break;
1747  }
1748  }
1749  }
1750 
1751  entry = av_dict_get(s->metadata, "reel_name", NULL, 0);
1752  if (entry) {
1753  packages[2].name = entry->value;
1754  packages[2].type = SourcePackage;
1755  packages[2].instance = 2;
1756  packages[1].ref = &packages[2];
1757  package_count = 3;
1758  }
1759 
1760  mxf_write_preface(s);
1762  mxf_write_content_storage(s, packages, package_count);
1763  mxf->track_instance_count = 0;
1764  for (i = 0; i < package_count; i++)
1765  mxf_write_package(s, &packages[i]);
1767  return 0;
1768 }
1769 
1770 static unsigned klv_fill_size(uint64_t size)
1771 {
1772  unsigned pad = KAG_SIZE - (size & (KAG_SIZE-1));
1773  if (pad < 20) // smallest fill item possible
1774  return pad + KAG_SIZE;
1775  else
1776  return pad & (KAG_SIZE-1);
1777 }
1778 
1780 {
1781  MXFContext *mxf = s->priv_data;
1782  AVIOContext *pb = s->pb;
1783  int i, j, temporal_reordering = 0;
1784  int key_index = mxf->last_key_index;
1785  int prev_non_b_picture = 0;
1786  int audio_frame_size = 0;
1787  int64_t pos;
1788 
1789  av_log(s, AV_LOG_DEBUG, "edit units count %d\n", mxf->edit_units_count);
1790 
1791  if (!mxf->edit_units_count && !mxf->edit_unit_byte_count)
1792  return;
1793 
1795 
1796  klv_encode_ber4_length(pb, 0);
1797  pos = avio_tell(pb);
1798 
1799  // instance id
1800  mxf_write_local_tag(pb, 16, 0x3C0A);
1802 
1803  // index edit rate
1804  mxf_write_local_tag(pb, 8, 0x3F0B);
1805  avio_wb32(pb, mxf->time_base.den);
1806  avio_wb32(pb, mxf->time_base.num);
1807 
1808  // index start position
1809  mxf_write_local_tag(pb, 8, 0x3F0C);
1811 
1812  // index duration
1813  mxf_write_local_tag(pb, 8, 0x3F0D);
1814  if (mxf->edit_unit_byte_count)
1815  avio_wb64(pb, 0); // index table covers whole container
1816  else
1817  avio_wb64(pb, mxf->edit_units_count);
1818 
1819  // edit unit byte count
1820  mxf_write_local_tag(pb, 4, 0x3F05);
1821  avio_wb32(pb, mxf->edit_unit_byte_count);
1822 
1823  // index sid
1824  mxf_write_local_tag(pb, 4, 0x3F06);
1825  avio_wb32(pb, 2);
1826 
1827  // body sid
1828  mxf_write_local_tag(pb, 4, 0x3F07);
1829  avio_wb32(pb, 1);
1830 
1831  // real slice count - 1
1832  mxf_write_local_tag(pb, 1, 0x3F08);
1833  avio_w8(pb, !mxf->edit_unit_byte_count); // only one slice for CBR
1834 
1835  // delta entry array
1836  mxf_write_local_tag(pb, 8 + (s->nb_streams+1)*6, 0x3F09);
1837  avio_wb32(pb, s->nb_streams+1); // num of entries
1838  avio_wb32(pb, 6); // size of one entry
1839  // write system item delta entry
1840  avio_w8(pb, 0);
1841  avio_w8(pb, 0); // slice entry
1842  avio_wb32(pb, 0); // element delta
1843  // write each stream delta entry
1844  for (i = 0; i < s->nb_streams; i++) {
1845  AVStream *st = s->streams[i];
1846  MXFStreamContext *sc = st->priv_data;
1847  avio_w8(pb, sc->temporal_reordering);
1848  if (sc->temporal_reordering)
1849  temporal_reordering = 1;
1850  if (mxf->edit_unit_byte_count) {
1851  avio_w8(pb, 0); // slice number
1852  avio_wb32(pb, sc->slice_offset);
1853  } else if (i == 0) { // video track
1854  avio_w8(pb, 0); // slice number
1855  avio_wb32(pb, KAG_SIZE); // system item size including klv fill
1856  } else { // audio or data track
1857  if (!audio_frame_size) {
1858  audio_frame_size = sc->aic.samples[0]*sc->aic.sample_size;
1859  audio_frame_size += klv_fill_size(audio_frame_size);
1860  }
1861  avio_w8(pb, 1);
1862  avio_wb32(pb, (i-1)*audio_frame_size); // element delta
1863  }
1864  }
1865 
1866  if (!mxf->edit_unit_byte_count) {
1867  MXFStreamContext *sc = s->streams[0]->priv_data;
1868  mxf_write_local_tag(pb, 8 + mxf->edit_units_count*15, 0x3F0A);
1869  avio_wb32(pb, mxf->edit_units_count); // num of entries
1870  avio_wb32(pb, 15); // size of one entry
1871 
1872  for (i = 0; i < mxf->edit_units_count; i++) {
1873  int temporal_offset = 0;
1874 
1875  if (!(mxf->index_entries[i].flags & 0x33)) { // I-frame
1876  sc->max_gop = FFMAX(sc->max_gop, i - mxf->last_key_index);
1877  mxf->last_key_index = key_index;
1878  key_index = i;
1879  }
1880 
1881  if (temporal_reordering) {
1882  int pic_num_in_gop = i - key_index;
1883  if (pic_num_in_gop != mxf->index_entries[i].temporal_ref) {
1884  for (j = key_index; j < mxf->edit_units_count; j++) {
1885  if (pic_num_in_gop == mxf->index_entries[j].temporal_ref)
1886  break;
1887  }
1888  if (j == mxf->edit_units_count)
1889  av_log(s, AV_LOG_WARNING, "missing frames\n");
1890  temporal_offset = j - key_index - pic_num_in_gop;
1891  }
1892  }
1893  avio_w8(pb, temporal_offset);
1894 
1895  if ((mxf->index_entries[i].flags & 0x30) == 0x30) { // back and forward prediction
1896  sc->b_picture_count = FFMAX(sc->b_picture_count, i - prev_non_b_picture);
1897  avio_w8(pb, mxf->last_key_index - i);
1898  } else {
1899  avio_w8(pb, key_index - i); // key frame offset
1900  if ((mxf->index_entries[i].flags & 0x20) == 0x20) // only forward
1901  mxf->last_key_index = key_index;
1902  prev_non_b_picture = i;
1903  }
1904 
1905  if (!(mxf->index_entries[i].flags & 0x33) && // I-frame
1906  mxf->index_entries[i].flags & 0x40 && !temporal_offset)
1907  mxf->index_entries[i].flags |= 0x80; // random access
1908  avio_w8(pb, mxf->index_entries[i].flags);
1909  // stream offset
1910  avio_wb64(pb, mxf->index_entries[i].offset);
1911  if (s->nb_streams > 1)
1912  avio_wb32(pb, mxf->index_entries[i].slice_offset);
1913  else
1914  avio_wb32(pb, 0);
1915  }
1916 
1917  mxf->last_key_index = key_index - mxf->edit_units_count;
1919  mxf->edit_units_count = 0;
1920  }
1921 
1922  mxf_update_klv_size(pb, pos);
1923 }
1924 
1926 {
1927  unsigned pad = klv_fill_size(avio_tell(s->pb));
1928  if (pad) {
1929  avio_write(s->pb, klv_fill_key, 16);
1930  pad -= 16 + 4;
1931  klv_encode_ber4_length(s->pb, pad);
1932  ffio_fill(s->pb, 0, pad);
1933  av_assert1(!(avio_tell(s->pb) & (KAG_SIZE-1)));
1934  }
1935 }
1936 
1937 static int mxf_write_partition(AVFormatContext *s, int bodysid,
1938  int indexsid,
1939  const uint8_t *key, int write_metadata)
1940 {
1941  MXFContext *mxf = s->priv_data;
1942  AVIOContext *pb = s->pb;
1943  int64_t header_byte_count_offset;
1944  unsigned index_byte_count = 0;
1945  uint64_t partition_offset = avio_tell(pb);
1946  int err;
1947 
1948  if (!mxf->edit_unit_byte_count && mxf->edit_units_count)
1949  index_byte_count = 85 + 12+(s->nb_streams+1)*6 +
1950  12+mxf->edit_units_count*15;
1951  else if (mxf->edit_unit_byte_count && indexsid)
1952  index_byte_count = 80;
1953 
1954  if (index_byte_count) {
1955  index_byte_count += 16 + 4; // add encoded ber4 length
1956  index_byte_count += klv_fill_size(index_byte_count);
1957  }
1958 
1959  if (key && !memcmp(key, body_partition_key, 16)) {
1961  sizeof(*mxf->body_partition_offset))) < 0) {
1962  mxf->body_partitions_count = 0;
1963  return err;
1964  }
1965  mxf->body_partition_offset[mxf->body_partitions_count++] = partition_offset;
1966  }
1967 
1968  // write klv
1969  if (key)
1970  avio_write(pb, key, 16);
1971  else
1972  avio_write(pb, body_partition_key, 16);
1973 
1975 
1976  // write partition value
1977  avio_wb16(pb, 1); // majorVersion
1978  avio_wb16(pb, 3); // minorVersion
1979  avio_wb32(pb, KAG_SIZE); // KAGSize
1980 
1981  avio_wb64(pb, partition_offset); // ThisPartition
1982 
1983  if (key && !memcmp(key, body_partition_key, 16) && mxf->body_partitions_count > 1)
1984  avio_wb64(pb, mxf->body_partition_offset[mxf->body_partitions_count-2]); // PreviousPartition
1985  else if (key && !memcmp(key, footer_partition_key, 16) && mxf->body_partitions_count)
1986  avio_wb64(pb, mxf->body_partition_offset[mxf->body_partitions_count-1]); // PreviousPartition
1987  else
1988  avio_wb64(pb, 0);
1989 
1990  avio_wb64(pb, mxf->footer_partition_offset); // footerPartition
1991 
1992  // set offset
1993  header_byte_count_offset = avio_tell(pb);
1994  avio_wb64(pb, 0); // headerByteCount, update later
1995 
1996  // indexTable
1997  avio_wb64(pb, index_byte_count); // indexByteCount
1998  avio_wb32(pb, index_byte_count ? indexsid : 0); // indexSID
1999 
2000  // BodyOffset
2001  if (bodysid && mxf->edit_units_count && mxf->body_partitions_count && s->oformat != &ff_mxf_opatom_muxer)
2002  avio_wb64(pb, mxf->body_offset);
2003  else
2004  avio_wb64(pb, 0);
2005 
2006  avio_wb32(pb, bodysid); // bodySID
2007 
2008  // operational pattern
2009  if (s->oformat == &ff_mxf_opatom_muxer)
2010  avio_write(pb, opatom_ul, 16);
2011  else
2012  avio_write(pb, op1a_ul, 16);
2013 
2014  // essence container
2016 
2017  if (write_metadata) {
2018  // mark the start of the headermetadata and calculate metadata size
2019  int64_t pos, start;
2020  unsigned header_byte_count;
2021 
2022  mxf_write_klv_fill(s);
2023  start = avio_tell(s->pb);
2025  mxf_write_klv_fill(s);
2027  pos = avio_tell(s->pb);
2028  header_byte_count = pos - start + klv_fill_size(pos);
2029 
2030  // update header_byte_count
2031  avio_seek(pb, header_byte_count_offset, SEEK_SET);
2032  avio_wb64(pb, header_byte_count);
2033  avio_seek(pb, pos, SEEK_SET);
2034  }
2035 
2036  if(key)
2037  avio_flush(pb);
2038 
2039  return 0;
2040 }
2041 
2042 static const struct {
2043  int profile;
2045 } mxf_prores_codec_uls[] = {
2046  { FF_PROFILE_PRORES_PROXY, { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0d,0x04,0x01,0x02,0x02,0x03,0x06,0x01,0x00 } },
2047  { FF_PROFILE_PRORES_LT, { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0d,0x04,0x01,0x02,0x02,0x03,0x06,0x02,0x00 } },
2048  { FF_PROFILE_PRORES_STANDARD, { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0d,0x04,0x01,0x02,0x02,0x03,0x06,0x03,0x00 } },
2049  { FF_PROFILE_PRORES_HQ, { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0d,0x04,0x01,0x02,0x02,0x03,0x06,0x04,0x00 } },
2050  { FF_PROFILE_PRORES_4444, { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0d,0x04,0x01,0x02,0x02,0x03,0x06,0x05,0x00 } },
2051  { FF_PROFILE_PRORES_XQ, { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0d,0x04,0x01,0x02,0x02,0x03,0x06,0x06,0x00 } },
2052 };
2053 
2055 {
2056  MXFContext *mxf = s->priv_data;
2057  MXFStreamContext *sc = st->priv_data;
2058  int i, profile;
2059 
2060  if (mxf->header_written)
2061  return 1;
2062 
2063  sc->codec_ul = NULL;
2064  profile = st->codecpar->profile;
2065  for (i = 0; i < FF_ARRAY_ELEMS(mxf_prores_codec_uls); i++) {
2066  if (profile == mxf_prores_codec_uls[i].profile) {
2067  sc->codec_ul = &mxf_prores_codec_uls[i].codec_ul;
2068  break;
2069  }
2070  }
2071  if (!sc->codec_ul)
2072  return 0;
2073 
2074  sc->frame_size = pkt->size;
2075 
2076  return 1;
2077 }
2078 
2079 static const struct {
2080  int cid;
2081  UID codec_ul;
2082 } mxf_dnxhd_codec_uls[] = {
2083  { 1235, { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x71,0x01,0x00,0x00 } }, // 1080p 10bit HIGH
2084  { 1237, { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x71,0x03,0x00,0x00 } }, // 1080p 8bit MED
2085  { 1238, { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x71,0x04,0x00,0x00 } }, // 1080p 8bit HIGH
2086  { 1241, { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x71,0x07,0x00,0x00 } }, // 1080i 10bit HIGH
2087  { 1242, { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x71,0x08,0x00,0x00 } }, // 1080i 8bit MED
2088  { 1243, { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x71,0x09,0x00,0x00 } }, // 1080i 8bit HIGH
2089  { 1244, { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x71,0x0a,0x00,0x00 } }, // 1080i 8bit TR
2090  { 1250, { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x71,0x10,0x00,0x00 } }, // 720p 10bit
2091  { 1251, { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x71,0x11,0x00,0x00 } }, // 720p 8bit HIGH
2092  { 1252, { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x71,0x12,0x00,0x00 } }, // 720p 8bit MED
2093  { 1253, { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x71,0x13,0x00,0x00 } }, // 720p 8bit LOW
2094  { 1256, { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x71,0x16,0x00,0x00 } }, // 1080p 10bit 444
2095  { 1258, { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x71,0x18,0x00,0x00 } }, // 720p 8bit TR
2096  { 1259, { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x71,0x19,0x00,0x00 } }, // 1080p 8bit TR
2097  { 1260, { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x71,0x1a,0x00,0x00 } }, // 1080i 8bit TR MBAFF
2098  { 1270, { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x71,0x24,0x00,0x00 } }, // DNXHR 444
2099  { 1271, { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x71,0x25,0x00,0x00 } }, // DNXHR HQX
2100  { 1272, { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x71,0x26,0x00,0x00 } }, // DNXHR HQ
2101  { 1273, { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x71,0x27,0x00,0x00 } }, // DNXHR SQ
2102  { 1274, { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x71,0x28,0x00,0x00 } }, // DNXHR LB
2103 };
2104 
2106 {
2107  MXFContext *mxf = s->priv_data;
2108  MXFStreamContext *sc = st->priv_data;
2109  int i, cid, frame_size = 0;
2110 
2111  if (mxf->header_written)
2112  return 1;
2113 
2114  if (pkt->size < 43)
2115  return 0;
2116 
2117  sc->codec_ul = NULL;
2118  cid = AV_RB32(pkt->data + 0x28);
2119  for (i = 0; i < FF_ARRAY_ELEMS(mxf_dnxhd_codec_uls); i++) {
2120  if (cid == mxf_dnxhd_codec_uls[i].cid) {
2121  sc->codec_ul = &mxf_dnxhd_codec_uls[i].codec_ul;
2122  break;
2123  }
2124  }
2125  if (!sc->codec_ul)
2126  return 0;
2127 
2128  sc->component_depth = 0;
2129  switch (pkt->data[0x21] >> 5) {
2130  case 1: sc->component_depth = 8; break;
2131  case 2: sc->component_depth = 10; break;
2132  case 3: sc->component_depth = 12; break;
2133  }
2134  if (!sc->component_depth)
2135  return 0;
2136 
2137  if ((frame_size = avpriv_dnxhd_get_frame_size(cid)) == DNXHD_VARIABLE) {
2138  frame_size = avpriv_dnxhd_get_hr_frame_size(cid, st->codecpar->width, st->codecpar->height);
2139  }
2140  if (frame_size < 0)
2141  return 0;
2142 
2143  if ((sc->interlaced = avpriv_dnxhd_get_interlaced(cid)) < 0)
2144  return 0;
2145 
2146  if (cid >= 1270) { // RI raster
2148  st->codecpar->width, st->codecpar->height,
2149  INT_MAX);
2150  } else {
2151  sc->aspect_ratio = (AVRational){ 16, 9 };
2152  }
2153 
2154  sc->frame_size = pkt->size;
2155 
2156  return 1;
2157 }
2158 
2160 {
2161  MXFContext *mxf = s->priv_data;
2162  MXFStreamContext *sc = st->priv_data;
2163  uint8_t *vs_pack, *vsc_pack;
2164  int ul_index, stype, pal;
2165  const AVDVProfile *profile;
2166 
2167  if (mxf->header_written)
2168  return 1;
2169 
2170  // Check for minimal frame size
2171  if (pkt->size < 120000)
2172  return -1;
2173 
2174  profile = av_dv_frame_profile(NULL, pkt->data, pkt->size);
2175 
2176  vs_pack = pkt->data + 80*5 + 48;
2177  vsc_pack = pkt->data + 80*5 + 53;
2178  stype = vs_pack[3] & 0x1f;
2179  pal = (vs_pack[3] >> 5) & 0x1;
2180 
2181  if ((vsc_pack[2] & 0x07) == 0x02) {
2182  sc->aspect_ratio = (AVRational){ 16, 9 };
2183  } else {
2184  sc->aspect_ratio = (AVRational){ 4, 3 };
2185  }
2186 
2187  sc->interlaced = (vsc_pack[3] >> 4) & 0x01;
2188  // TODO: fix dv encoder to set proper FF/FS value in VSC pack
2189  // and set field dominance accordingly
2190  // av_log(s, AV_LOG_DEBUG, "DV vsc pack ff/ss = %x\n", vsc_pack[2] >> 6);
2191 
2192  switch (stype) {
2193  case 0x18: // DV100 720p
2194  ul_index = INDEX_DV100_720_50 + pal;
2195  if (sc->interlaced) {
2196  av_log(s, AV_LOG_ERROR, "source marked as interlaced but codec profile is progressive\n");
2197  sc->interlaced = 0;
2198  }
2199  break;
2200  case 0x14: // DV100 1080i
2201  ul_index = INDEX_DV100_1080_50 + pal;
2202  break;
2203  case 0x04: // DV50
2204  ul_index = INDEX_DV50_525_60 + pal;
2205  break;
2206  default: // DV25
2207  if (profile && profile->pix_fmt == AV_PIX_FMT_YUV420P && pal) {
2208  ul_index = INDEX_DV25_525_60_IEC + pal;
2209  break;
2210  }
2211  ul_index = INDEX_DV25_525_60 + pal;
2212  }
2213 
2214  sc->index = ul_index;
2215  sc->codec_ul = &mxf_essence_container_uls[sc->index].codec_ul;
2216  sc->frame_size = pkt->size;
2217 
2218  return 1;
2219 }
2220 
2221 static const struct {
2224  int profile;
2226  int intra_only; // 1 or 0 when there are separate UIDs for Long GOP and Intra, -1 when Intra/LGOP detection can be ignored
2227 } mxf_h264_codec_uls[] = {
2228  {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x31,0x11,0x01 }, 0, 66, 0, -1 }, // AVC Baseline
2229  {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x31,0x20,0x01 }, 0, 77, 0, -1 }, // AVC Main
2230  {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x31,0x30,0x01 }, 0, 88, 0, -1 }, // AVC Extended
2231  {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x31,0x40,0x01 }, 0, 100, 0, -1 }, // AVC High
2232  {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x31,0x50,0x01 }, 0, 110, 0, 0 }, // AVC High 10
2233  {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x31,0x60,0x01 }, 0, 122, 0, 0 }, // AVC High 422
2234  {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x31,0x70,0x01 }, 0, 244, 0, 0 }, // AVC High 444
2235  {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x32,0x20,0x01 }, 0, 110, 0, 1 }, // AVC High 10 Intra
2236  {{ 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
2237  {{ 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
2238  {{ 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
2239  {{ 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
2240  {{ 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
2241  {{ 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
2242  {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x32,0x30,0x01 }, 0, 122, 0, 1 }, // AVC High 422 Intra
2243  {{ 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
2244  {{ 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
2245  {{ 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
2246  {{ 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
2247  {{ 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
2248  {{ 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
2249  {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0d,0x04,0x01,0x02,0x02,0x01,0x32,0x40,0x01 }, 0, 244, 0, 1 }, // AVC High 444 Intra
2250  {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0d,0x04,0x01,0x02,0x02,0x01,0x32,0x50,0x01 }, 0, 44, 0, -1 }, // AVC CAVLC 444
2251 };
2252 
2254  AVPacket *pkt, MXFIndexEntry *e)
2255 {
2256  MXFContext *mxf = s->priv_data;
2257  MXFStreamContext *sc = st->priv_data;
2259  GetBitContext gb;
2260  const uint8_t *buf = pkt->data;
2261  const uint8_t *buf_end = pkt->data + pkt->size;
2262  const uint8_t *nal_end;
2263  uint32_t state = -1;
2264  int extra_size = 512; // support AVC Intra files without SPS/PPS header
2265  int i, frame_size, slice_type, intra_only = 0;
2266 
2267  for (;;) {
2268  buf = avpriv_find_start_code(buf, buf_end, &state);
2269  if (buf >= buf_end)
2270  break;
2271 
2272  switch (state & 0x1f) {
2273  case H264_NAL_SPS:
2274  e->flags |= 0x40;
2275 
2276  if (mxf->header_written)
2277  break;
2278 
2279  nal_end = ff_avc_find_startcode(buf, buf_end);
2280  sps = ff_avc_decode_sps(buf, nal_end - buf);
2281  if (!sps) {
2282  av_log(s, AV_LOG_ERROR, "error parsing sps\n");
2283  return 0;
2284  }
2285 
2286  sc->aspect_ratio.num = st->codecpar->width * sps->sar.num;
2287  sc->aspect_ratio.den = st->codecpar->height * sps->sar.den;
2289  sc->aspect_ratio.num, sc->aspect_ratio.den, 1024*1024);
2290  intra_only = (sps->constraint_set_flags >> 3) & 1;
2291  sc->interlaced = !sps->frame_mbs_only_flag;
2292  sc->component_depth = sps->bit_depth_luma;
2293 
2294  buf = nal_end;
2295  break;
2296  case H264_NAL_PPS:
2297  if (e->flags & 0x40) { // sequence header present
2298  e->flags |= 0x80; // random access
2299  extra_size = 0;
2300  }
2301  break;
2302  case H264_NAL_IDR_SLICE:
2303  e->flags |= 0x04; // IDR Picture
2304  buf = buf_end;
2305  break;
2306  case H264_NAL_SLICE:
2307  init_get_bits8(&gb, buf, buf_end - buf);
2308  get_ue_golomb_long(&gb); // skip first_mb_in_slice
2309  slice_type = get_ue_golomb_31(&gb);
2310  switch (slice_type % 5) {
2311  case 0:
2312  e->flags |= 0x20; // P Picture
2313  e->flags |= 0x06; // P Picture
2314  break;
2315  case 1:
2316  e->flags |= 0x30; // B Picture
2317  e->flags |= 0x03; // non-referenced B Picture
2318  break;
2319  }
2320  buf = buf_end;
2321  break;
2322  default:
2323  break;
2324  }
2325  }
2326 
2327  if (mxf->header_written)
2328  return 1;
2329 
2330  if (!sps)
2331  sc->interlaced = st->codecpar->field_order != AV_FIELD_PROGRESSIVE ? 1 : 0;
2332  sc->codec_ul = NULL;
2333  frame_size = pkt->size + extra_size;
2334 
2335  for (i = 0; i < FF_ARRAY_ELEMS(mxf_h264_codec_uls); i++) {
2336  if (frame_size == mxf_h264_codec_uls[i].frame_size && sc->interlaced == mxf_h264_codec_uls[i].interlaced) {
2337  sc->codec_ul = &mxf_h264_codec_uls[i].uid;
2338  sc->component_depth = 10; // AVC Intra is always 10 Bit
2339  sc->aspect_ratio = (AVRational){ 16, 9 }; // 16:9 is mandatory for broadcast HD
2340  st->codecpar->profile = mxf_h264_codec_uls[i].profile;
2341  sc->avc_intra = 1;
2342  mxf->cbr_index = 1;
2343  sc->frame_size = pkt->size;
2344  if (sc->interlaced)
2345  sc->field_dominance = 1; // top field first is mandatory for AVC Intra
2346  break;
2347  } else if (sps && mxf_h264_codec_uls[i].frame_size == 0 &&
2348  mxf_h264_codec_uls[i].profile == sps->profile_idc &&
2349  (mxf_h264_codec_uls[i].intra_only < 0 ||
2350  mxf_h264_codec_uls[i].intra_only == intra_only)) {
2351  sc->codec_ul = &mxf_h264_codec_uls[i].uid;
2352  st->codecpar->profile = sps->profile_idc;
2353  st->codecpar->level = sps->level_idc;
2354  // continue to check for avc intra
2355  }
2356  }
2357 
2358  av_free(sps);
2359 
2360  if (!sc->codec_ul) {
2361  av_log(s, AV_LOG_ERROR, "h264 profile not supported\n");
2362  return 0;
2363  }
2364 
2365  return 1;
2366 }
2367 
2368 static const UID mxf_mpeg2_codec_uls[] = {
2369  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x01,0x10,0x00 }, // MP-ML I-Frame
2370  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x01,0x11,0x00 }, // MP-ML Long GOP
2371  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x02,0x02,0x00 }, // 422P-ML I-Frame
2372  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x02,0x03,0x00 }, // 422P-ML Long GOP
2373  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x03,0x02,0x00 }, // MP-HL I-Frame
2374  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x03,0x03,0x00 }, // MP-HL Long GOP
2375  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x04,0x02,0x00 }, // 422P-HL I-Frame
2376  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x04,0x03,0x00 }, // 422P-HL Long GOP
2377  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x05,0x02,0x00 }, // MP@H-14 I-Frame
2378  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x05,0x03,0x00 }, // MP@H-14 Long GOP
2379 };
2380 
2382 {
2383  int long_gop = 1;
2384 
2385  if (par->profile == 4) { // Main
2386  if (par->level == 8) // Main
2387  return &mxf_mpeg2_codec_uls[0+long_gop];
2388  else if (par->level == 4) // High
2389  return &mxf_mpeg2_codec_uls[4+long_gop];
2390  else if (par->level == 6) // High 14
2391  return &mxf_mpeg2_codec_uls[8+long_gop];
2392  } else if (par->profile == 0) { // 422
2393  if (par->level == 5) // Main
2394  return &mxf_mpeg2_codec_uls[2+long_gop];
2395  else if (par->level == 2) // High
2396  return &mxf_mpeg2_codec_uls[6+long_gop];
2397  }
2398  return NULL;
2399 }
2400 
2402  AVPacket *pkt, MXFIndexEntry *e)
2403 {
2404  MXFStreamContext *sc = st->priv_data;
2405  uint32_t c = -1;
2406  int i;
2407 
2408  for(i = 0; i < pkt->size - 4; i++) {
2409  c = (c<<8) + pkt->data[i];
2410  if (c == 0x1b5) {
2411  if ((pkt->data[i+1] & 0xf0) == 0x10) { // seq ext
2412  st->codecpar->profile = pkt->data[i+1] & 0x07;
2413  st->codecpar->level = pkt->data[i+2] >> 4;
2414  sc->low_delay = pkt->data[i+6] >> 7;
2415  } else if (i + 5 < pkt->size && (pkt->data[i+1] & 0xf0) == 0x80) { // pict coding ext
2416  sc->interlaced = !(pkt->data[i+5] & 0x80); // progressive frame
2417  if (sc->interlaced)
2418  sc->field_dominance = 1 + !(pkt->data[i+4] & 0x80); // top field first
2419  break;
2420  }
2421  } else if (c == 0x1b8) { // gop
2422  if (pkt->data[i+4]>>6 & 0x01) { // closed
2423  if (sc->seq_closed_gop == -1)
2424  sc->seq_closed_gop = 1;
2425  sc->closed_gop = 1;
2426  if (e->flags & 0x40) // sequence header present
2427  e->flags |= 0x80; // random access
2428  } else {
2429  sc->seq_closed_gop = 0;
2430  sc->closed_gop = 0;
2431  }
2432  } else if (c == 0x1b3) { // seq
2433  e->flags |= 0x40;
2434  switch ((pkt->data[i+4]>>4) & 0xf) {
2435  case 2: sc->aspect_ratio = (AVRational){ 4, 3}; break;
2436  case 3: sc->aspect_ratio = (AVRational){ 16, 9}; break;
2437  case 4: sc->aspect_ratio = (AVRational){221,100}; break;
2438  default:
2440  st->codecpar->width, st->codecpar->height, 1024*1024);
2441  }
2442  } else if (c == 0x100) { // pic
2443  int pict_type = (pkt->data[i+2]>>3) & 0x07;
2444  e->temporal_ref = (pkt->data[i+1]<<2) | (pkt->data[i+2]>>6);
2445  if (pict_type == 2) { // P-frame
2446  e->flags |= 0x22;
2447  sc->closed_gop = 0; // reset closed GOP, don't matter anymore
2448  } else if (pict_type == 3) { // B-frame
2449  if (sc->closed_gop)
2450  e->flags |= 0x13; // only backward prediction
2451  else
2452  e->flags |= 0x33;
2453  sc->temporal_reordering = -1;
2454  } else if (!pict_type) {
2455  av_log(s, AV_LOG_ERROR, "error parsing mpeg2 frame\n");
2456  return 0;
2457  }
2458  }
2459  }
2460  if (s->oformat != &ff_mxf_d10_muxer)
2462  return !!sc->codec_ul;
2463 }
2464 
2465 static uint64_t mxf_parse_timestamp(int64_t timestamp64)
2466 {
2467  time_t timestamp = timestamp64 / 1000000;
2468  struct tm tmbuf;
2469  struct tm *time = gmtime_r(&timestamp, &tmbuf);
2470  if (!time)
2471  return 0;
2472  return (uint64_t)(time->tm_year+1900) << 48 |
2473  (uint64_t)(time->tm_mon+1) << 40 |
2474  (uint64_t) time->tm_mday << 32 |
2475  time->tm_hour << 24 |
2476  time->tm_min << 16 |
2477  time->tm_sec << 8 |
2478  (timestamp64 % 1000000) / 4000;
2479 }
2480 
2482 {
2483  MXFContext *mxf = s->priv_data;
2484  uint32_t seed = av_get_random_seed();
2485  uint64_t umid = seed + 0x5294713400000000LL;
2486 
2487  AV_WB64(mxf->umid , umid);
2488  AV_WB64(mxf->umid+8, umid>>8);
2489 
2490  mxf->instance_number = seed & 0xFFFFFF;
2491 }
2492 
2494 {
2495  MXFContext *mxf = s->priv_data;
2496  AVDictionaryEntry *tcr = av_dict_get(s->metadata, "timecode", NULL, 0);
2497  if (!tcr)
2498  tcr = av_dict_get(st->metadata, "timecode", NULL, 0);
2499 
2500  if (tcr)
2501  return av_timecode_init_from_string(&mxf->tc, rate, tcr->value, s);
2502  else
2503  return av_timecode_init(&mxf->tc, rate, 0, 0, s);
2504 }
2505 
2507 {
2508  MXFContext *mxf = s->priv_data;
2509  int i, ret;
2510  uint8_t present[FF_ARRAY_ELEMS(mxf_essence_container_uls)] = {0};
2511  const MXFSamplesPerFrame *spf = NULL;
2512  int64_t timestamp = 0;
2513 
2514  if (!s->nb_streams)
2515  return -1;
2516 
2517  if (s->oformat == &ff_mxf_opatom_muxer && s->nb_streams !=1) {
2518  av_log(s, AV_LOG_ERROR, "there must be exactly one stream for mxf opatom\n");
2519  return -1;
2520  }
2521 
2522  if (!av_dict_get(s->metadata, "comment_", NULL, AV_DICT_IGNORE_SUFFIX))
2523  mxf->store_user_comments = 0;
2524 
2525  for (i = 0; i < s->nb_streams; i++) {
2526  AVStream *st = s->streams[i];
2527  MXFStreamContext *sc = av_mallocz(sizeof(*sc));
2528  if (!sc)
2529  return AVERROR(ENOMEM);
2530  st->priv_data = sc;
2531 
2532  if (((i == 0) ^ (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)) && s->oformat != &ff_mxf_opatom_muxer) {
2533  av_log(s, AV_LOG_ERROR, "there must be exactly one video stream and it must be the first one\n");
2534  return -1;
2535  }
2536 
2537  if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
2538  const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(st->codecpar->format);
2539  // TODO: should be avg_frame_rate
2540  AVRational rate, tbc = st->time_base;
2541  // Default component depth to 8
2542  sc->component_depth = 8;
2543  sc->h_chroma_sub_sample = 2;
2544  sc->v_chroma_sub_sample = 2;
2545  sc->color_siting = 0xFF;
2546 
2549  av_make_q(st->codecpar->width, st->codecpar->height));
2550  }
2551 
2552  if (pix_desc) {
2553  sc->component_depth = pix_desc->comp[0].depth;
2554  sc->h_chroma_sub_sample = 1 << pix_desc->log2_chroma_w;
2555  sc->v_chroma_sub_sample = 1 << pix_desc->log2_chroma_h;
2556  }
2557  switch (ff_choose_chroma_location(s, st)) {
2558  case AVCHROMA_LOC_TOPLEFT: sc->color_siting = 0; break;
2559  case AVCHROMA_LOC_LEFT: sc->color_siting = 6; break;
2560  case AVCHROMA_LOC_TOP: sc->color_siting = 1; break;
2561  case AVCHROMA_LOC_CENTER: sc->color_siting = 3; break;
2562  }
2563 
2564  mxf->timecode_base = (tbc.den + tbc.num/2) / tbc.num;
2565  spf = ff_mxf_get_samples_per_frame(s, tbc);
2566  if (!spf) {
2567  av_log(s, AV_LOG_ERROR, "Unsupported video frame rate %d/%d\n",
2568  tbc.den, tbc.num);
2569  return AVERROR(EINVAL);
2570  }
2572  mxf->time_base = spf->time_base;
2573  rate = av_inv_q(mxf->time_base);
2574  avpriv_set_pts_info(st, 64, mxf->time_base.num, mxf->time_base.den);
2575  if((ret = mxf_init_timecode(s, st, rate)) < 0)
2576  return ret;
2577 
2579  sc->seq_closed_gop = -1; // unknown yet
2580  }
2581 
2582  sc->video_bit_rate = st->codecpar->bit_rate;
2583 
2584  if (s->oformat == &ff_mxf_d10_muxer ||
2587  mxf->cbr_index = 1;
2588 
2589  if (s->oformat == &ff_mxf_d10_muxer) {
2591  av_log(s, AV_LOG_ERROR, "error MXF D-10 only support MPEG-2 Video\n");
2592  return AVERROR(EINVAL);
2593  }
2594  if ((sc->video_bit_rate == 50000000) && (mxf->time_base.den == 25)) {
2596  } else if ((sc->video_bit_rate == 49999840 || sc->video_bit_rate == 50000000) && (mxf->time_base.den != 25)) {
2598  } else if (sc->video_bit_rate == 40000000) {
2599  if (mxf->time_base.den == 25) sc->index = INDEX_D10_625_50_40_VIDEO;
2600  else sc->index = INDEX_D10_525_60_40_VIDEO;
2601  } else if (sc->video_bit_rate == 30000000) {
2602  if (mxf->time_base.den == 25) sc->index = INDEX_D10_625_50_30_VIDEO;
2603  else sc->index = INDEX_D10_525_60_30_VIDEO;
2604  } else {
2605  av_log(s, AV_LOG_ERROR, "error MXF D-10 only support 30/40/50 mbit/s\n");
2606  return -1;
2607  }
2608 
2609  sc->signal_standard = 1;
2610  sc->color_siting = 0;
2611  sc->frame_size = (int64_t)sc->video_bit_rate *
2612  mxf->time_base.num / (8*mxf->time_base.den);
2613  }
2614  if (mxf->signal_standard >= 0)
2615  sc->signal_standard = mxf->signal_standard;
2616  } else if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
2617  if (st->codecpar->sample_rate != 48000) {
2618  av_log(s, AV_LOG_ERROR, "only 48khz is implemented\n");
2619  return -1;
2620  }
2621  avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
2622  if (s->oformat == &ff_mxf_d10_muxer) {
2623  if (st->index != 1) {
2624  av_log(s, AV_LOG_ERROR, "MXF D-10 only support one audio track\n");
2625  return -1;
2626  }
2627  if (st->codecpar->codec_id != AV_CODEC_ID_PCM_S16LE &&
2629  av_log(s, AV_LOG_ERROR, "MXF D-10 only support 16 or 24 bits le audio\n");
2630  }
2631  sc->index = ((MXFStreamContext*)s->streams[0]->priv_data)->index + 1;
2632  sc->frame_size = 4 + 8 * spf[0].samples_per_frame[0] * 4;
2633  } else if (s->oformat == &ff_mxf_opatom_muxer) {
2634  AVRational tbc = av_inv_q(mxf->audio_edit_rate);
2635 
2636  if (st->codecpar->codec_id != AV_CODEC_ID_PCM_S16LE &&
2638  av_log(s, AV_LOG_ERROR, "Only pcm_s16le and pcm_s24le audio codecs are implemented\n");
2639  return AVERROR_PATCHWELCOME;
2640  }
2641  if (st->codecpar->channels != 1) {
2642  av_log(s, AV_LOG_ERROR, "MXF OPAtom only supports single channel audio\n");
2643  return AVERROR(EINVAL);
2644  }
2645 
2646  spf = ff_mxf_get_samples_per_frame(s, tbc);
2647  if (!spf) {
2648  av_log(s, AV_LOG_ERROR, "Unsupported timecode frame rate %d/%d\n", tbc.den, tbc.num);
2649  return AVERROR(EINVAL);
2650  }
2651 
2652  mxf->time_base = st->time_base;
2653  if((ret = mxf_init_timecode(s, st, av_inv_q(spf->time_base))) < 0)
2654  return ret;
2655 
2656  mxf->timecode_base = (tbc.den + tbc.num/2) / tbc.num;
2658  sc->index = INDEX_WAV;
2659  } else {
2660  mxf->slice_count = 1;
2661  sc->frame_size = (st->codecpar->channels * spf[0].samples_per_frame[0] *
2663  }
2664  } else if (st->codecpar->codec_type == AVMEDIA_TYPE_DATA) {
2665  AVDictionaryEntry *e = av_dict_get(st->metadata, "data_type", NULL, 0);
2666  if (e && !strcmp(e->value, "vbi_vanc_smpte_436M")) {
2667  sc->index = INDEX_S436M;
2668  } else {
2669  av_log(s, AV_LOG_ERROR, "track %d: unsupported data type\n", i);
2670  return -1;
2671  }
2672  if (st->index != s->nb_streams - 1) {
2673  av_log(s, AV_LOG_ERROR, "data track must be placed last\n");
2674  return -1;
2675  }
2676  }
2677 
2678  if (!sc->index) {
2680  if (sc->index == -1) {
2681  av_log(s, AV_LOG_ERROR, "track %d: could not find essence container ul, "
2682  "codec not currently supported in container\n", i);
2683  return -1;
2684  }
2685  }
2686 
2687  sc->codec_ul = &mxf_essence_container_uls[sc->index].codec_ul;
2688 
2689  memcpy(sc->track_essence_element_key, mxf_essence_container_uls[sc->index].element_ul, 15);
2690  sc->track_essence_element_key[15] = present[sc->index];
2691  PRINT_KEY(s, "track essence element key", sc->track_essence_element_key);
2692 
2693  if (!present[sc->index])
2694  mxf->essence_container_count++;
2695  present[sc->index]++;
2696  }
2697 
2698  if (s->oformat == &ff_mxf_d10_muxer || s->oformat == &ff_mxf_opatom_muxer) {
2699  mxf->essence_container_count = 1;
2700  }
2701 
2702  if (!(s->flags & AVFMT_FLAG_BITEXACT))
2703  mxf_gen_umid(s);
2704 
2705  for (i = 0; i < s->nb_streams; i++) {
2706  MXFStreamContext *sc = s->streams[i]->priv_data;
2707  // update element count
2708  sc->track_essence_element_key[13] = present[sc->index];
2709  if (!memcmp(sc->track_essence_element_key, mxf_essence_container_uls[15].element_ul, 13)) // DV
2710  sc->order = (0x15 << 24) | AV_RB32(sc->track_essence_element_key+13);
2711  else
2712  sc->order = AV_RB32(sc->track_essence_element_key+12);
2713  }
2714 
2715  if (ff_parse_creation_time_metadata(s, &timestamp, 0) > 0)
2716  mxf->timestamp = mxf_parse_timestamp(timestamp);
2717  mxf->duration = -1;
2718 
2719  mxf->timecode_track = av_mallocz(sizeof(*mxf->timecode_track));
2720  if (!mxf->timecode_track)
2721  return AVERROR(ENOMEM);
2723  if (!mxf->timecode_track->priv_data)
2724  return AVERROR(ENOMEM);
2725  mxf->timecode_track->index = -1;
2726 
2727  if (!spf)
2728  spf = ff_mxf_get_samples_per_frame(s, (AVRational){ 1, 25 });
2729 
2730  if (ff_audio_interleave_init(s, spf->samples_per_frame, mxf->time_base) < 0)
2731  return -1;
2732 
2733  return 0;
2734 }
2735 
2736 static const uint8_t system_metadata_pack_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0D,0x01,0x03,0x01,0x04,0x01,0x01,0x00 };
2737 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 };
2738 
2740 {
2741  MXFContext *mxf = s->priv_data;
2742  AVIOContext *pb = s->pb;
2743  unsigned frame;
2744  uint32_t time_code;
2745  int i, system_item_bitmap = 0x58; // UL, user date/time stamp, picture present
2746 
2747  frame = mxf->last_indexed_edit_unit + mxf->edit_units_count;
2748 
2749  // write system metadata pack
2751  klv_encode_ber4_length(pb, 57);
2752 
2753  for (i = 0; i < s->nb_streams; i++) {
2755  system_item_bitmap |= 0x4;
2756  else if (s->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_DATA)
2757  system_item_bitmap |= 0x2;
2758  }
2759  avio_w8(pb, system_item_bitmap);
2760  avio_w8(pb, mxf->content_package_rate); // content package rate
2761  avio_w8(pb, 0x00); // content package type
2762  avio_wb16(pb, 0x00); // channel handle
2763  avio_wb16(pb, (mxf->tc.start + frame) & 0xFFFF); // continuity count, supposed to overflow
2764  if (mxf->essence_container_count > 1)
2765  avio_write(pb, multiple_desc_ul, 16);
2766  else {
2767  MXFStreamContext *sc = s->streams[0]->priv_data;
2768  avio_write(pb, mxf_essence_container_uls[sc->index].container_ul, 16);
2769  }
2770  avio_w8(pb, 0);
2771  avio_wb64(pb, 0);
2772  avio_wb64(pb, 0); // creation date/time stamp
2773 
2774  avio_w8(pb, 0x81); // SMPTE 12M time code
2775  time_code = av_timecode_get_smpte_from_framenum(&mxf->tc, frame);
2776  avio_wb32(pb, time_code);
2777  avio_wb32(pb, 0); // binary group data
2778  avio_wb64(pb, 0);
2779 
2780  // write system metadata package set
2782  klv_encode_ber4_length(pb, 35);
2783  avio_w8(pb, 0x83); // UMID
2784  avio_wb16(pb, 0x20);
2785  mxf_write_umid(s, 1);
2786 }
2787 
2789 {
2790  MXFContext *mxf = s->priv_data;
2791  AVIOContext *pb = s->pb;
2792  int frame_size = pkt->size / st->codecpar->block_align;
2793  uint8_t *samples = pkt->data;
2794  uint8_t *end = pkt->data + pkt->size;
2795  int i;
2796 
2797  klv_encode_ber4_length(pb, 4 + frame_size*4*8);
2798 
2799  avio_w8(pb, (frame_size == 1920 ? 0 : (mxf->edit_units_count-1) % 5 + 1));
2800  avio_wl16(pb, frame_size);
2801  avio_w8(pb, (1<<st->codecpar->channels)-1);
2802 
2803  while (samples < end) {
2804  for (i = 0; i < st->codecpar->channels; i++) {
2805  uint32_t sample;
2806  if (st->codecpar->codec_id == AV_CODEC_ID_PCM_S24LE) {
2807  sample = AV_RL24(samples)<< 4;
2808  samples += 3;
2809  } else {
2810  sample = AV_RL16(samples)<<12;
2811  samples += 2;
2812  }
2813  avio_wl32(pb, sample | i);
2814  }
2815  for (; i < 8; i++)
2816  avio_wl32(pb, i);
2817  }
2818 }
2819 
2821 {
2822  MXFContext *mxf = s->priv_data;
2823  AVIOContext *pb = s->pb;
2824  AVStream *st = s->streams[0];
2825  MXFStreamContext *sc = st->priv_data;
2826  const uint8_t *key = NULL;
2827 
2828  int err;
2829 
2830  if (!mxf->header_written)
2831  key = body_partition_key;
2832 
2833  if ((err = mxf_write_partition(s, 1, 0, key, 0)) < 0)
2834  return err;
2835  mxf_write_klv_fill(s);
2838  return 0;
2839 }
2840 
2842 {
2843  MXFContext *mxf = s->priv_data;
2844  AVIOContext *pb = s->pb;
2845 
2846  int err;
2847 
2848  if (!mxf->header_written) {
2849  if ((err = mxf_write_partition(s, 0, 0, header_open_partition_key, 1)) < 0)
2850  return err;
2851  mxf_write_klv_fill(s);
2852 
2853  if ((err = mxf_write_opatom_body_partition(s)) < 0)
2854  return err;
2855  mxf->header_written = 1;
2856  }
2857 
2858  if (!mxf->edit_unit_byte_count) {
2860  mxf->index_entries[mxf->edit_units_count].flags = ie->flags;
2862  }
2863  mxf->edit_units_count++;
2864  avio_write(pb, pkt->data, pkt->size);
2865  mxf->body_offset += pkt->size;
2866  avio_flush(pb);
2867 
2868  return 0;
2869 }
2870 
2872 {
2873  MXFContext *mxf = s->priv_data;
2874  int i;
2875 
2876  if (s->oformat == &ff_mxf_opatom_muxer) {
2877  MXFStreamContext *sc = s->streams[0]->priv_data;
2878  mxf->edit_unit_byte_count = sc->frame_size;
2879  return;
2880  }
2881 
2882  mxf->edit_unit_byte_count = KAG_SIZE; // system element
2883  for (i = 0; i < s->nb_streams; i++) {
2884  AVStream *st = s->streams[i];
2885  MXFStreamContext *sc = st->priv_data;
2887  mxf->edit_unit_byte_count += 16 + 4 + sc->frame_size;
2889  }
2890 }
2891 
2893 {
2894  MXFContext *mxf = s->priv_data;
2895  AVIOContext *pb = s->pb;
2896  AVStream *st = s->streams[pkt->stream_index];
2897  MXFStreamContext *sc = st->priv_data;
2898  MXFIndexEntry ie = {0};
2899  int err;
2900 
2901  if (!mxf->cbr_index && !mxf->edit_unit_byte_count && !(mxf->edit_units_count % EDIT_UNITS_PER_BODY)) {
2902  if ((err = av_reallocp_array(&mxf->index_entries, mxf->edit_units_count
2903  + EDIT_UNITS_PER_BODY, sizeof(*mxf->index_entries))) < 0) {
2904  mxf->edit_units_count = 0;
2905  av_log(s, AV_LOG_ERROR, "could not allocate index entries\n");
2906  return err;
2907  }
2908  }
2909 
2911  if (!mxf_parse_mpeg2_frame(s, st, pkt, &ie)) {
2912  av_log(s, AV_LOG_ERROR, "could not get mpeg2 profile and level\n");
2913  return -1;
2914  }
2915  } else if (st->codecpar->codec_id == AV_CODEC_ID_DNXHD) {
2916  if (!mxf_parse_dnxhd_frame(s, st, pkt)) {
2917  av_log(s, AV_LOG_ERROR, "could not get dnxhd profile\n");
2918  return -1;
2919  }
2920  } else if (st->codecpar->codec_id == AV_CODEC_ID_PRORES) {
2921  if (!mxf_parse_prores_frame(s, st, pkt)) {
2922  av_log(s, AV_LOG_ERROR, "could not get prores profile\n");
2923  return -1;
2924  }
2925  } else if (st->codecpar->codec_id == AV_CODEC_ID_DVVIDEO) {
2926  if (!mxf_parse_dv_frame(s, st, pkt)) {
2927  av_log(s, AV_LOG_ERROR, "could not get dv profile\n");
2928  return -1;
2929  }
2930  } else if (st->codecpar->codec_id == AV_CODEC_ID_H264) {
2931  if (!mxf_parse_h264_frame(s, st, pkt, &ie)) {
2932  av_log(s, AV_LOG_ERROR, "could not get h264 profile\n");
2933  return -1;
2934  }
2935  }
2936 
2937  if (mxf->cbr_index) {
2938  if (pkt->size != sc->frame_size && st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
2939  av_log(s, AV_LOG_ERROR, "track %d: frame size does not match index unit size, %d != %d\n",
2940  st->index, pkt->size, sc->frame_size);
2941  return -1;
2942  }
2943  if (!mxf->header_written)
2945  }
2946 
2947  if (s->oformat == &ff_mxf_opatom_muxer)
2948  return mxf_write_opatom_packet(s, pkt, &ie);
2949 
2950  if (!mxf->header_written) {
2951  if (mxf->edit_unit_byte_count) {
2952  if ((err = mxf_write_partition(s, 1, 2, header_open_partition_key, 1)) < 0)
2953  return err;
2954  mxf_write_klv_fill(s);
2956  } else {
2957  if ((err = mxf_write_partition(s, 0, 0, header_open_partition_key, 1)) < 0)
2958  return err;
2959  }
2960  mxf->header_written = 1;
2961  }
2962 
2963  if (st->index == 0) {
2964  if (!mxf->edit_unit_byte_count &&
2966  !(ie.flags & 0x33)) { // I-frame, GOP start
2967  mxf_write_klv_fill(s);
2968  if ((err = mxf_write_partition(s, 1, 2, body_partition_key, 0)) < 0)
2969  return err;
2970  mxf_write_klv_fill(s);
2972  }
2973 
2974  mxf_write_klv_fill(s);
2976 
2977  if (!mxf->edit_unit_byte_count) {
2979  mxf->index_entries[mxf->edit_units_count].flags = ie.flags;
2981  mxf->body_offset += KAG_SIZE; // size of system element
2982  }
2983  mxf->edit_units_count++;
2984  } else if (!mxf->edit_unit_byte_count && st->index == 1) {
2985  if (!mxf->edit_units_count) {
2986  av_log(s, AV_LOG_ERROR, "No packets in first stream\n");
2987  return AVERROR_PATCHWELCOME;
2988  }
2990  mxf->body_offset - mxf->index_entries[mxf->edit_units_count-1].offset;
2991  }
2992 
2993  mxf_write_klv_fill(s);
2994  avio_write(pb, sc->track_essence_element_key, 16); // write key
2995  if (s->oformat == &ff_mxf_d10_muxer &&
2997  mxf_write_d10_audio_packet(s, st, pkt);
2998  } else {
2999  klv_encode_ber4_length(pb, pkt->size); // write length
3000  avio_write(pb, pkt->data, pkt->size);
3001  mxf->body_offset += 16+4+pkt->size + klv_fill_size(16+4+pkt->size);
3002  }
3003 
3004  avio_flush(pb);
3005 
3006  return 0;
3007 }
3008 
3010 {
3011  MXFContext *mxf = s->priv_data;
3012  AVIOContext *pb = s->pb;
3013  uint64_t pos = avio_tell(pb);
3014  int i;
3015 
3017  klv_encode_ber_length(pb, 28 + 12LL*mxf->body_partitions_count);
3018 
3019  if (mxf->edit_unit_byte_count && s->oformat != &ff_mxf_opatom_muxer)
3020  avio_wb32(pb, 1); // BodySID of header partition
3021  else
3022  avio_wb32(pb, 0);
3023  avio_wb64(pb, 0); // offset of header partition
3024 
3025  for (i = 0; i < mxf->body_partitions_count; i++) {
3026  avio_wb32(pb, 1); // BodySID
3027  avio_wb64(pb, mxf->body_partition_offset[i]);
3028  }
3029 
3030  avio_wb32(pb, 0); // BodySID of footer partition
3032 
3033  avio_wb32(pb, avio_tell(pb) - pos + 4);
3034 }
3035 
3037 {
3038  MXFContext *mxf = s->priv_data;
3039  AVIOContext *pb = s->pb;
3040  int i, err = 0;
3041 
3042  if (!mxf->header_written ||
3043  (s->oformat == &ff_mxf_opatom_muxer && !mxf->body_partition_offset)) {
3044  /* reason could be invalid options/not supported codec/out of memory */
3045  err = AVERROR_UNKNOWN;
3046  goto end;
3047  }
3048 
3050 
3051  mxf_write_klv_fill(s);
3053  if (mxf->edit_unit_byte_count && s->oformat != &ff_mxf_opatom_muxer) { // no need to repeat index
3054  if ((err = mxf_write_partition(s, 0, 0, footer_partition_key, 0)) < 0)
3055  goto end;
3056  } else {
3057  if ((err = mxf_write_partition(s, 0, 2, footer_partition_key, 0)) < 0)
3058  goto end;
3059  mxf_write_klv_fill(s);
3061  }
3062 
3063  mxf_write_klv_fill(s);
3065 
3066  if (s->pb->seekable & AVIO_SEEKABLE_NORMAL) {
3067  if (s->oformat == &ff_mxf_opatom_muxer) {
3068  /* rewrite body partition to update lengths */
3069  avio_seek(pb, mxf->body_partition_offset[0], SEEK_SET);
3070  if ((err = mxf_write_opatom_body_partition(s)) < 0)
3071  goto end;
3072  }
3073 
3074  avio_seek(pb, 0, SEEK_SET);
3075  if (mxf->edit_unit_byte_count && s->oformat != &ff_mxf_opatom_muxer) {
3076  if ((err = mxf_write_partition(s, 1, 2, header_closed_partition_key, 1)) < 0)
3077  goto end;
3078  mxf_write_klv_fill(s);
3080  } else {
3081  if ((err = mxf_write_partition(s, 0, 0, header_closed_partition_key, 1)) < 0)
3082  goto end;
3083  }
3084  // update footer partition offset
3085  for (i = 0; i < mxf->body_partitions_count; i++) {
3086  avio_seek(pb, mxf->body_partition_offset[i]+44, SEEK_SET);
3088  }
3089  }
3090 
3091 end:
3093 
3094  av_freep(&mxf->index_entries);
3097  av_freep(&mxf->timecode_track);
3098 
3099  mxf_free(s);
3100 
3101  return err < 0 ? err : 0;
3102 }
3103 
3105 {
3106  int i, stream_count = 0;
3107 
3108  for (i = 0; i < s->nb_streams; i++)
3109  stream_count += !!s->streams[i]->last_in_packet_buffer;
3110 
3111  if (stream_count && (s->nb_streams == stream_count || flush)) {
3112  AVPacketList *pktl = s->internal->packet_buffer;
3113  if (s->nb_streams != stream_count) {
3114  AVPacketList *last = NULL;
3115  // find last packet in edit unit
3116  while (pktl) {
3117  if (!stream_count || pktl->pkt.stream_index == 0)
3118  break;
3119  // update last packet in packet buffer
3120  if (s->streams[pktl->pkt.stream_index]->last_in_packet_buffer != pktl)
3121  s->streams[pktl->pkt.stream_index]->last_in_packet_buffer = pktl;
3122  last = pktl;
3123  pktl = pktl->next;
3124  stream_count--;
3125  }
3126  // purge packet queue
3127  while (pktl) {
3128  AVPacketList *next = pktl->next;
3129  av_packet_unref(&pktl->pkt);
3130  av_freep(&pktl);
3131  pktl = next;
3132  }
3133  if (last)
3134  last->next = NULL;
3135  else {
3136  s->internal->packet_buffer = NULL;
3138  goto out;
3139  }
3140  pktl = s->internal->packet_buffer;
3141  }
3142 
3143  *out = pktl->pkt;
3144  av_log(s, AV_LOG_TRACE, "out st:%d dts:%"PRId64"\n", (*out).stream_index, (*out).dts);
3145  s->internal->packet_buffer = pktl->next;
3146  if(s->streams[pktl->pkt.stream_index]->last_in_packet_buffer == pktl)
3148  if(!s->internal->packet_buffer)
3150  av_freep(&pktl);
3151  return 1;
3152  } else {
3153  out:
3154  av_init_packet(out);
3155  return 0;
3156  }
3157 }
3158 
3160  const AVPacket *pkt)
3161 {
3163  MXFStreamContext *sc2 = s->streams[next->stream_index]->priv_data;
3164 
3165  return next->dts > pkt->dts ||
3166  (next->dts == pkt->dts && sc->order < sc2->order);
3167 }
3168 
3170 {
3171  return ff_audio_rechunk_interleave(s, out, pkt, flush,
3173 }
3174 
3175 #define MXF_COMMON_OPTIONS \
3176  { "signal_standard", "Force/set Signal Standard",\
3177  offsetof(MXFContext, signal_standard), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 7, AV_OPT_FLAG_ENCODING_PARAM, "signal_standard"},\
3178  { "bt601", "ITU-R BT.601 and BT.656, also SMPTE 125M (525 and 625 line interlaced)",\
3179  0, AV_OPT_TYPE_CONST, {.i64 = 1}, -1, 7, AV_OPT_FLAG_ENCODING_PARAM, "signal_standard"},\
3180  { "bt1358", "ITU-R BT.1358 and ITU-R BT.799-3, also SMPTE 293M (525 and 625 line progressive)",\
3181  0, AV_OPT_TYPE_CONST, {.i64 = 2}, -1, 7, AV_OPT_FLAG_ENCODING_PARAM, "signal_standard"},\
3182  { "smpte347m", "SMPTE 347M (540 Mbps mappings)",\
3183  0, AV_OPT_TYPE_CONST, {.i64 = 3}, -1, 7, AV_OPT_FLAG_ENCODING_PARAM, "signal_standard"},\
3184  { "smpte274m", "SMPTE 274M (1125 line)",\
3185  0, AV_OPT_TYPE_CONST, {.i64 = 4}, -1, 7, AV_OPT_FLAG_ENCODING_PARAM, "signal_standard"},\
3186  { "smpte296m", "SMPTE 296M (750 line progressive)",\
3187  0, AV_OPT_TYPE_CONST, {.i64 = 5}, -1, 7, AV_OPT_FLAG_ENCODING_PARAM, "signal_standard"},\
3188  { "smpte349m", "SMPTE 349M (1485 Mbps mappings)",\
3189  0, AV_OPT_TYPE_CONST, {.i64 = 6}, -1, 7, AV_OPT_FLAG_ENCODING_PARAM, "signal_standard"},\
3190  { "smpte428", "SMPTE 428-1 DCDM",\
3191  0, AV_OPT_TYPE_CONST, {.i64 = 7}, -1, 7, AV_OPT_FLAG_ENCODING_PARAM, "signal_standard"},
3192 
3193 
3194 
3195 static const AVOption mxf_options[] = {
3197  { "store_user_comments", "",
3198  offsetof(MXFContext, store_user_comments), AV_OPT_TYPE_BOOL, {.i64 = 1}, 0, 1, AV_OPT_FLAG_ENCODING_PARAM},
3199  { NULL },
3200 };
3201 
3202 static const AVClass mxf_muxer_class = {
3203  .class_name = "MXF muxer",
3204  .item_name = av_default_item_name,
3205  .option = mxf_options,
3206  .version = LIBAVUTIL_VERSION_INT,
3207 };
3208 
3209 static const AVOption d10_options[] = {
3210  { "d10_channelcount", "Force/set channelcount in generic sound essence descriptor",
3211  offsetof(MXFContext, channel_count), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 8, AV_OPT_FLAG_ENCODING_PARAM},
3213  { "store_user_comments", "",
3214  offsetof(MXFContext, store_user_comments), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, AV_OPT_FLAG_ENCODING_PARAM},
3215  { NULL },
3216 };
3217 
3219  .class_name = "MXF-D10 muxer",
3220  .item_name = av_default_item_name,
3221  .option = d10_options,
3222  .version = LIBAVUTIL_VERSION_INT,
3223 };
3224 
3225 static const AVOption opatom_options[] = {
3226  { "mxf_audio_edit_rate", "Audio edit rate for timecode",
3227  offsetof(MXFContext, audio_edit_rate), AV_OPT_TYPE_RATIONAL, {.dbl=25}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
3229  { "store_user_comments", "",
3230  offsetof(MXFContext, store_user_comments), AV_OPT_TYPE_BOOL, {.i64 = 1}, 0, 1, AV_OPT_FLAG_ENCODING_PARAM},
3231  { NULL },
3232 };
3233 
3235  .class_name = "MXF-OPAtom muxer",
3236  .item_name = av_default_item_name,
3237  .option = opatom_options,
3238  .version = LIBAVUTIL_VERSION_INT,
3239 };
3240 
3242  .name = "mxf",
3243  .long_name = NULL_IF_CONFIG_SMALL("MXF (Material eXchange Format)"),
3244  .mime_type = "application/mxf",
3245  .extensions = "mxf",
3246  .priv_data_size = sizeof(MXFContext),
3247  .audio_codec = AV_CODEC_ID_PCM_S16LE,
3248  .video_codec = AV_CODEC_ID_MPEG2VIDEO,
3254  .priv_class = &mxf_muxer_class,
3255 };
3256 
3257 AVOutputFormat ff_mxf_d10_muxer = {
3258  .name = "mxf_d10",
3259  .long_name = NULL_IF_CONFIG_SMALL("MXF (Material eXchange Format) D-10 Mapping"),
3260  .mime_type = "application/mxf",
3261  .priv_data_size = sizeof(MXFContext),
3262  .audio_codec = AV_CODEC_ID_PCM_S16LE,
3263  .video_codec = AV_CODEC_ID_MPEG2VIDEO,
3269  .priv_class = &mxf_d10_muxer_class,
3270 };
3271 
3272 AVOutputFormat ff_mxf_opatom_muxer = {
3273  .name = "mxf_opatom",
3274  .long_name = NULL_IF_CONFIG_SMALL("MXF (Material eXchange Format) Operational Pattern Atom"),
3275  .mime_type = "application/mxf",
3276  .extensions = "mxf",
3277  .priv_data_size = sizeof(MXFContext),
3278  .audio_codec = AV_CODEC_ID_PCM_S16LE,
3279  .video_codec = AV_CODEC_ID_DNXHD,
3285  .priv_class = &mxf_opatom_muxer_class,
3286 };
int cid
Definition: mxfenc.c:2080
static void write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, int unqueue)
Definition: ffmpeg.c:690
void avio_wb64(AVIOContext *s, uint64_t val)
Definition: aviobuf.c:466
uint32_t tagged_value_count
Definition: mxfenc.c:359
#define NULL
Definition: coverity.c:32
enum AVFieldOrder field_order
Video only.
Definition: avcodec.h:4049
void avio_wl16(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:472
MXFMetadataSetType
Definition: mxf.h:30
Bytestream IO Context.
Definition: avio.h:161
enum AVColorTransferCharacteristic color_trc
Definition: avcodec.h:4056
static int mxf_utf16_local_tag_length(const char *utf8_str)
Definition: mxfenc.c:779
int component_depth
Definition: mxfenc.c:89
static const uint8_t header_closed_partition_key[]
Definition: mxfenc.c:379
#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:385
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2522
MPEG-2/4 4:2:0, H.264 default for 4:2:0.
Definition: pixfmt.h:544
int signal_standard
Definition: mxfenc.c:91
const uint8_t * ff_avc_find_startcode(const uint8_t *p, const uint8_t *end)
Definition: avc.c:66
int avio_put_str16be(AVIOContext *s, const char *str)
Convert an UTF-8 string to UTF-16BE and write it.
AVOption.
Definition: opt.h:246
static void flush(AVCodecContext *avctx)
"Linear transfer characteristics"
Definition: pixfmt.h:477
static void mxf_write_index_table_segment(AVFormatContext *s)
Definition: mxfenc.c:1779
enum AVCodecID id
Definition: mxfenc.c:156
uint64_t body_offset
Definition: mxfenc.c:354
static void mxf_write_timecode_component(AVFormatContext *s, AVStream *st, MXFPackage *package)
Definition: mxfenc.c:1000
#define PRINT_KEY(pc, s, x)
Definition: mxf.h:129
static const uint8_t klv_fill_key[]
Definition: mxfenc.c:380
static const UID mxf_mpegvideo_descriptor_key
Definition: mxfenc.c:1153
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
unsigned char * buf_ptr
Current position in the buffer.
Definition: avio.h:228
static void klv_encode_ber9_length(AVIOContext *pb, uint64_t len)
Definition: mxfenc.c:579
void(* write_desc)(AVFormatContext *, AVStream *)
Definition: mxfenc.c:111
int index
index in mxf_essence_container_uls table
Definition: mxfenc.c:84
void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition: utils.c:4929
UID codec_ul
Definition: mxfenc.c:2044
AVRational sample_aspect_ratio
Video only.
Definition: avcodec.h:4044
unsigned body_partitions_count
Definition: mxfenc.c:346
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:3968
const int * samples
current samples per frame, pointer to samples_per_frame
int num
Numerator.
Definition: rational.h:59
static int mxf_init_timecode(AVFormatContext *s, AVStream *st, AVRational rate)
Definition: mxfenc.c:2493
int index
stream index in AVFormatContext
Definition: avformat.h:882
int size
Definition: avcodec.h:1481
Definition: mxf.h:34
static const UID mxf_cdci_descriptor_key
Definition: mxfenc.c:1156
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:246
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:191
static const struct @279 mxf_essence_mappings[]
AVFormatInternal * internal
An opaque field for libavformat internal usage.
Definition: avformat.h:1811
int interlaced
whether picture is interlaced
Definition: mxfenc.c:87
int av_log2(unsigned v)
Definition: intmath.c:26
static const uint8_t body_partition_key[]
Definition: mxfenc.c:381
static int get_trc(UID ul, enum AVColorTransferCharacteristic trc)
Definition: mxfenc.c:1161
static int64_t mxf_write_generic_sound_common(AVFormatContext *s, AVStream *st, const UID key)
Definition: mxfenc.c:1470
AVOutputFormat ff_mxf_muxer
Definition: mxfenc.c:3241
static void mxf_update_klv_size(AVIOContext *pb, int64_t pos)
Definition: mxfenc.c:1371
#define FF_PROFILE_PRORES_LT
Definition: avcodec.h:3007
GLint GLenum type
Definition: opengl_enc.c:104
int avpriv_dnxhd_get_interlaced(int cid)
Definition: dnxhddata.c:1108
static void mxf_write_s436m_anc_desc(AVFormatContext *s, AVStream *st)
Definition: mxfenc.c:1426
void * priv_data
Definition: avformat.h:896
const char * key
int version
Definition: avisynth_c.h:858
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:236
static void mxf_write_sequence(AVFormatContext *s, AVStream *st, MXFPackage *package)
Definition: mxfenc.c:973
static AVPacket pkt
struct AVRational time_base
Definition: mxf.h:86
uint8_t level_idc
Definition: avc.h:42
static void mxf_free(AVFormatContext *s)
Definition: mxfenc.c:655
static void mxf_write_uuid(AVIOContext *pb, enum MXFMetadataSetType type, int value)
Definition: mxfenc.c:523
#define sample
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_RL16
Definition: bytestream.h:87
UID track_essence_element_key
Definition: mxfenc.c:83
static void mxf_write_common_fields(AVFormatContext *s, AVStream *st)
Definition: mxfenc.c:949
uint8_t bit_depth_luma
Definition: avc.h:45
#define FF_PROFILE_PRORES_XQ
Definition: avcodec.h:3011
uint8_t log2_chroma_w
Amount to shift the luma width right to find the chroma width.
Definition: pixdesc.h:92
int id
Definition: mxf.h:79
This struct describes the properties of an encoded stream.
Definition: avcodec.h:3960
static const UID mxf_s436m_anc_descriptor_key
Definition: mxfenc.c:1152
AVColorTransferCharacteristic
Color Transfer Characteristic.
Definition: pixfmt.h:468
static int mxf_write_header(AVFormatContext *s)
Definition: mxfenc.c:2506
static void klv_encode_ber4_length(AVIOContext *pb, int len)
Definition: mxfenc.c:573
static const uint8_t multiple_desc_ul[]
Definition: mxfenc.c:387
Format I/O context.
Definition: avformat.h:1358
int h_chroma_sub_sample
Definition: mxfenc.c:92
uint8_t UID[16]
Definition: mxf.h:28
int temporal_reordering
Definition: mxfenc.c:94
#define gmtime_r
Definition: time_internal.h:34
uint8_t slice_count
index slice count minus 1 (1 if no audio, 0 otherwise)
Definition: mxfenc.c:343
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:72
#define AV_WB64(p, v)
Definition: intreadwrite.h:433
static const UID mxf_generic_sound_descriptor_key
Definition: mxfenc.c:1157
static int klv_encode_ber_length(AVIOContext *pb, uint64_t len)
Definition: mxfenc.c:553
static void mxf_write_klv_fill(AVFormatContext *s)
Definition: mxfenc.c:1925
static void mxf_write_refs_count(AVIOContext *pb, int ref_count)
Definition: mxfenc.c:539
int timecode_base
rounded time code base (25 or 30)
Definition: mxfenc.c:351
int av_timecode_init_from_string(AVTimecode *tc, AVRational rate, const char *str, void *log_ctx)
Parse timecode representation (hh:mm:ss[:;.
Definition: timecode.c:194
static void mxf_write_structural_component(AVFormatContext *s, AVStream *st, MXFPackage *package)
Definition: mxfenc.c:1027
static int mxf_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: mxfenc.c:2892
static void mxf_write_avc_subdesc(AVFormatContext *s, AVStream *st)
Definition: mxfenc.c:1380
AVOutputFormat ff_mxf_d10_muxer
Definition: mxfenc.c:3257
void avio_wl32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:372
AVComponentDescriptor comp[4]
Parameters that describe how pixels are packed.
Definition: pixdesc.h:117
static unsigned klv_fill_size(uint64_t size)
Definition: mxfenc.c:1770
uint8_t
const MXFSamplesPerFrame * ff_mxf_get_samples_per_frame(AVFormatContext *s, AVRational time_base)
Definition: mxf.c:155
static const struct @282 mxf_h264_codec_uls[]
Opaque data information usually continuous.
Definition: avutil.h:203
int width
Video only.
Definition: avcodec.h:4034
AVOptions.
static const UID mxf_mpeg2_codec_uls[]
Definition: mxfenc.c:2368
#define AV_LOG_TRACE
Extremely verbose debugging, useful for libav* development.
Definition: log.h:202
AVPacket pkt
Definition: avformat.h:2032
timecode is drop frame
Definition: timecode.h:36
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:90
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
GLsizei GLboolean const GLfloat * value
Definition: opengl_enc.c:108
int frame_size
frame size in bytes
Definition: mxfenc.c:99
also ITU-R BT470M / ITU-R BT1700 625 PAL & SECAM
Definition: pixfmt.h:473
int ff_audio_interleave_init(AVFormatContext *s, const int *samples_per_frame, AVRational time_base)
#define LIBAVFORMAT_VERSION_MAJOR
Definition: version.h:34
static int64_t mxf_write_generic_desc(AVFormatContext *s, AVStream *st, const UID key)
Definition: mxfenc.c:1114
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:87
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1426
int avpriv_dnxhd_get_frame_size(int cid)
Definition: dnxhddata.c:1087
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:40
int flags
Flags modifying the (de)muxer behaviour.
Definition: avformat.h:1489
uint8_t * data
Definition: avcodec.h:1480
uint64_t duration
Definition: mxfenc.c:348
int start
timecode frame start (first base frame number)
Definition: timecode.h:42
uint32_t tag
Definition: movenc.c:1531
uint64_t offset
Definition: mxfenc.c:76
Definition: ismindex.c:69
enum AVPixelFormat pix_fmt
Definition: dv_profile.h:50
int instance
Definition: mxfenc.c:117
ptrdiff_t size
Definition: opengl_enc.c:100
uint8_t flags
Definition: mxfenc.c:75
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:557
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
struct AVPacketList * packet_buffer
This buffer is only needed when packets were already buffered but not decoded, for example to get the...
Definition: internal.h:76
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:218
static int interleave_packet(AVFormatContext *s, AVPacket *out, AVPacket *in, int flush)
Interleave an AVPacket correctly so it can be muxed.
Definition: mux.c:1188
#define AVFMT_FLAG_BITEXACT
When muxing, try to avoid writing any random/volatile data to the output.
Definition: avformat.h:1506
AVRational audio_edit_rate
Definition: mxfenc.c:360
unsigned slice_offset
offset of audio slice
Definition: mxfenc.c:77
enum ULIndex index
Definition: mxfenc.c:157
static const uint8_t index_table_segment_key[]
Definition: mxfenc.c:376
#define av_log(a,...)
#define AV_OPT_FLAG_ENCODING_PARAM
a generic parameter which can be set by the user for muxing or encoding
Definition: opt.h:276
const uint8_t * avpriv_find_start_code(const uint8_t *p, const uint8_t *end, uint32_t *state)
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
Definition: avcodec.h:3997
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:184
int store_user_comments
Definition: mxfenc.c:361
AudioInterleaveContext aic
Definition: mxfenc.c:82
int local_tag
Definition: mxfenc.c:70
static const AVClass mxf_opatom_muxer_class
Definition: mxfenc.c:3234
H.264 parameter set handling.
static const uint8_t umid_ul[]
Definition: mxfenc.c:367
static void mxf_write_tape_descriptor(AVFormatContext *s)
Definition: mxfenc.c:1064
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: utils.c:5696
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:259
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: avcodec.h:215
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
AVDictionary * metadata
Metadata that applies to the whole file.
Definition: avformat.h:1598
int av_get_bits_per_sample(enum AVCodecID codec_id)
Return codec bits per sample.
Definition: utils.c:1533
ITU-R BT1361 Extended Colour Gamut.
Definition: pixfmt.h:481
uint8_t log2_chroma_h
Amount to shift the luma height right to find the chroma height.
Definition: pixdesc.h:101
static void mxf_write_system_item(AVFormatContext *s)
Definition: mxfenc.c:2739
#define EDIT_UNITS_PER_BODY
Definition: mxfenc.c:66
static const uint8_t header_metadata_key[]
partial key for header metadata
Definition: mxfenc.c:386
static const uint8_t mxf_indirect_value_utf16le[]
Definition: mxfenc.c:1550
int profile
Definition: mxfenc.c:2043
Definition: mxf.h:42
static int64_t mxf_write_wav_common(AVFormatContext *s, AVStream *st, const UID key)
Definition: mxfenc.c:1517
int v_chroma_sub_sample
Definition: mxfenc.c:93
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:186
static const uint8_t header_open_partition_key[]
Definition: mxfenc.c:378
static void mxf_write_preface(AVFormatContext *s)
Definition: mxfenc.c:702
const AVDVProfile * av_dv_frame_profile(const AVDVProfile *sys, const uint8_t *frame, unsigned buf_size)
Get a DV profile for the provided compressed frame.
Definition: dv_profile.c:300
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
static const struct @280 mxf_prores_codec_uls[]
enum AVMediaType codec_type
General type of the encoded data.
Definition: avcodec.h:3964
int header_written
Definition: mxfenc.c:339
static void mxf_write_random_index_pack(AVFormatContext *s)
Definition: mxfenc.c:3009
static void mxf_write_cdci_desc(AVFormatContext *s, AVStream *st)
Definition: mxfenc.c:1404
simple assert() macros that are a bit more flexible than ISO C assert().
static int mxf_write_opatom_body_partition(AVFormatContext *s)
Definition: mxfenc.c:2820
GLsizei GLsizei * length
Definition: opengl_enc.c:114
int cbr_index
use a constant bitrate index
Definition: mxfenc.c:363
static void mxf_write_content_storage(AVFormatContext *s, MXFPackage *packages, int package_count)
Definition: mxfenc.c:874
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_RL24
Definition: bytestream.h:87
static void mxf_write_metadata_key(AVIOContext *pb, unsigned int value)
Definition: mxfenc.c:649
GLsizei count
Definition: opengl_enc.c:108
#define MXF_COMMON_OPTIONS
Definition: mxfenc.c:3175
#define FFMAX(a, b)
Definition: common.h:94
int color_siting
Definition: mxfenc.c:90
#define LIBAVFORMAT_VERSION_MINOR
Definition: version.h:35
int order
interleaving order if dts are equal
Definition: mxfenc.c:86
static void mxf_write_d10_audio_packet(AVFormatContext *s, AVStream *st, AVPacket *pkt)
Definition: mxfenc.c:2788
static const uint8_t primer_pack_key[]
Definition: mxfenc.c:375
static const uint8_t system_metadata_pack_key[]
Definition: mxfenc.c:2736
static int mxf_get_essence_container_ul_index(enum AVCodecID id)
Definition: mxfenc.c:588
int av_reallocp_array(void *ptr, size_t nmemb, size_t size)
Allocate, reallocate, or free an array through a pointer to a pointer.
Definition: mem.c:205
int intra_only
Definition: mxfenc.c:2226
static int klv_ber_length(uint64_t len)
Definition: mxfenc.c:545
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:1414
int block_align
Audio only.
Definition: avcodec.h:4085
int seekable
A combination of AVIO_SEEKABLE_ flags or 0 when the stream is not seekable.
Definition: avio.h:260
static int mxf_parse_h264_frame(AVFormatContext *s, AVStream *st, AVPacket *pkt, MXFIndexEntry *e)
Definition: mxfenc.c:2253
static const UID mxf_wav_descriptor_key
Definition: mxfenc.c:1154
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition: avassert.h:53
void ffio_fill(AVIOContext *s, int b, int count)
Definition: aviobuf.c:204
#define KAG_SIZE
Definition: mxfenc.c:67
static const uint8_t op1a_ul[]
complete key for operation pattern, partitions, and primer pack
Definition: mxfenc.c:372
uint8_t frame_mbs_only_flag
Definition: avc.h:46
uint8_t interlaced
Definition: mxfenc.c:2225
static int mxf_parse_dnxhd_frame(AVFormatContext *s, AVStream *st, AVPacket *pkt)
Definition: mxfenc.c:2105
#define LIBAVFORMAT_VERSION_MICRO
Definition: version.h:36
#define FF_PROFILE_PRORES_STANDARD
Definition: avcodec.h:3008
uint64_t * body_partition_offset
Definition: mxfenc.c:345
struct MXFPackage * ref
Definition: mxfenc.c:118
typedef void(APIENTRY *FF_PFNGLACTIVETEXTUREPROC)(GLenum texture)
static int write_trailer(AVFormatContext *s1)
Definition: v4l2enc.c:94
ITU-R 601, SMPTE 274M 296M S314M(DV 4:1:1), mpeg2 4:2:2.
Definition: pixfmt.h:546
static const AVOption mxf_options[]
Definition: mxfenc.c:3195
ULIndex
Definition: mxfenc.c:121
void avio_wb24(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:490
static const AVClass mxf_muxer_class
Definition: mxfenc.c:3202
const char * name
Definition: avformat.h:505
static int mxf_write_partition(AVFormatContext *s, int bodysid, int indexsid, const uint8_t *key, int write_metadata)
Definition: mxfenc.c:1937
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
static void mxf_write_local_tag_utf16(AVIOContext *pb, int tag, const char *value)
Definition: mxfenc.c:798
#define s(width, name)
Definition: cbs_vp9.c:257
static int mxf_write_header_metadata_sets(AVFormatContext *s)
Definition: mxfenc.c:1721
static const struct @281 mxf_dnxhd_codec_uls[]
uint8_t constraint_set_flags
Definition: avc.h:43
AVDictionary * metadata
Definition: avformat.h:945
AVStream * timecode_track
Definition: mxfenc.c:350
int slice_offset
Definition: mxfenc.c:98
static const UID mxf_aes3_descriptor_key
Definition: mxfenc.c:1155
enum AVColorRange color_range
Video only.
Definition: avcodec.h:4054
const MXFCodecUL ff_mxf_data_definition_uls[]
SMPTE RP224 http://www.smpte-ra.org/mdd/index.html.
Definition: mxf.c:28
static const uint8_t system_metadata_package_set_key[]
Definition: mxfenc.c:2737
int edit_unit_byte_count
fixed edit unit byte count
Definition: mxfenc.c:352
preferred ID for MPEG-1/2 video decoding
Definition: avcodec.h:220
#define FF_ARRAY_ELEMS(a)
ff_const59 struct AVOutputFormat * oformat
The output container format.
Definition: avformat.h:1377
static uint64_t mxf_parse_timestamp(int64_t timestamp64)
Definition: mxfenc.c:2465
if(ret)
AVRational sar
Definition: avc.h:47
Stream structure.
Definition: avformat.h:881
static unsigned get_ue_golomb_long(GetBitContext *gb)
Read an unsigned Exp-Golomb code in the range 0 to UINT32_MAX-1.
Definition: golomb.h:105
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
int content_package_rate
content package rate in system element, see SMPTE 326M
Definition: mxfenc.c:353
#define AVFMT_NOTIMESTAMPS
Format does not need / have any timestamps.
Definition: avformat.h:467
uint32_t instance_number
Definition: mxfenc.c:355
static int mxf_write_user_comments(AVFormatContext *s, const AVDictionary *m)
Definition: mxfenc.c:1581
#define FF_PROFILE_PRORES_4444
Definition: avcodec.h:3010
int b_picture_count
maximum number of consecutive b pictures, used in mpeg-2 descriptor
Definition: mxfenc.c:102
int signal_standard
Definition: mxfenc.c:358
also ITU-R BT1361
Definition: pixfmt.h:470
#define AVIO_SEEKABLE_NORMAL
Seeking works like for a local file.
Definition: avio.h:40
static void mxf_write_local_tag(AVIOContext *pb, int size, int tag)
Definition: mxfenc.c:643
static void mxf_write_aes3_desc(AVFormatContext *s, AVStream *st)
Definition: mxfenc.c:1538
int max_gop
maximum gop size, used by mpeg-2 descriptor
Definition: mxfenc.c:101
int frame_size
Definition: mxfenc.c:2223
int last_indexed_edit_unit
Definition: mxfenc.c:344
also ITU-R BT601-6 525 or 625 / ITU-R BT1358 525 or 625 / ITU-R BT1700 NTSC
Definition: pixfmt.h:475
static const AVClass mxf_d10_muxer_class
Definition: mxfenc.c:3218
static int mxf_parse_mpeg2_frame(AVFormatContext *s, AVStream *st, AVPacket *pkt, MXFIndexEntry *e)
Definition: mxfenc.c:2401
static struct @320 state
uint8_t profile_idc
Definition: avc.h:41
const UID * codec_ul
Definition: mxfenc.c:85
static int get_ue_golomb_31(GetBitContext *gb)
read unsigned exp golomb code, constraint to a max of 31.
Definition: golomb.h:120
Timecode helpers header.
AVIOContext * pb
I/O context.
Definition: avformat.h:1400
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:677
static AVRational av_make_q(int num, int den)
Create an AVRational.
Definition: rational.h:71
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:81
void avio_w8(AVIOContext *s, int b)
Definition: aviobuf.c:196
int samples_per_frame[6]
Definition: mxf.h:87
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:599
static void mxf_write_essence_container_refs(AVFormatContext *s)
Definition: mxfenc.c:680
static unsigned int seed
Definition: videogen.c:78
static const UID * mxf_get_mpeg2_codec_ul(AVCodecParameters *par)
Definition: mxfenc.c:2381
static void mxf_write_package(AVFormatContext *s, MXFPackage *package)
Definition: mxfenc.c:1599
AVRational rate
frame rate in rational form
Definition: timecode.h:44
int seq_closed_gop
all gops in sequence are closed, used in mpeg-2 descriptor
Definition: mxfenc.c:100
void * buf
Definition: avisynth_c.h:766
int closed_gop
gop is closed, used in mpeg-2 frame parsing
Definition: mxfenc.c:96
uint64_t timestamp
timestamp, as year(16),month(8),day(8),hour(8),minutes(8),msec/4(8)
Definition: mxfenc.c:342
int low_delay
low delay, used in mpeg-2 descriptor
Definition: mxfenc.c:103
#define AV_STRINGIFY(s)
Definition: macros.h:36
Describe the class of an AVClass context structure.
Definition: log.h:67
AVRational aspect_ratio
display aspect ratio
Definition: mxfenc.c:95
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.xml.
Definition: mxfenc.c:393
int track_instance_count
Definition: mxfenc.c:362
Rational number (pair of numerator and denominator).
Definition: rational.h:58
static int64_t mxf_write_cdci_common(AVFormatContext *s, AVStream *st, const UID key)
Definition: mxfenc.c:1189
int sample_size
size of one sample all channels included
uint16_t temporal_ref
Definition: mxfenc.c:78
static uint64_t mxf_utf16len(const char *utf8_str)
Definition: mxfenc.c:757
static void mxf_write_primer_pack(AVFormatContext *s)
Definition: mxfenc.c:606
#define FF_PROFILE_PRORES_HQ
Definition: avcodec.h:3009
void avio_flush(AVIOContext *s)
Force flushing of buffered data.
Definition: aviobuf.c:238
int avio_put_str16le(AVIOContext *s, const char *str)
Convert an UTF-8 string to UTF-16LE and write it.
static int FUNC() sps(CodedBitstreamContext *ctx, RWContext *rw, H264RawSPS *current)
SMPTE ST 428-1.
Definition: pixfmt.h:487
static void mxf_write_track(AVFormatContext *s, AVStream *st, MXFPackage *package)
Definition: mxfenc.c:901
MXFIndexEntry * index_entries
Definition: mxfenc.c:340
AVRational time_base
Definition: mxfenc.c:338
AVClass * av_class
Definition: mxfenc.c:335
Definition: mxf.h:76
int video_bit_rate
Definition: mxfenc.c:97
static const uint8_t uuid_base[]
Definition: mxfenc.c:366
uint8_t pi<< 24) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_U8,(uint64_t)((*(const uint8_t *) pi-0x80U))<< 56) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8,(*(const uint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8,(*(const uint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16,(*(const int16_t *) pi >>8)+0x80) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_S16,(uint64_t)(*(const int16_t *) pi)<< 48) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16,*(const int16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16,*(const int16_t *) pi *(1.0/(1<< 15))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32,(*(const int32_t *) pi >>24)+0x80) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_S32,(uint64_t)(*(const int32_t *) pi)<< 32) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32,*(const int32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32,*(const int32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S64,(*(const int64_t *) pi >>56)+0x80) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S64,*(const int64_t *) pi *(1.0f/(UINT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S64,*(const int64_t *) pi *(1.0/(UINT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, av_clip_uint8(lrintf(*(const float *) pi *(1<< 7))+0x80)) CONV_FUNC(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, av_clip_int16(lrintf(*(const float *) pi *(1<< 15)))) CONV_FUNC(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, av_clipl_int32(llrintf(*(const float *) pi *(1U<< 31)))) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_FLT, llrintf(*(const float *) pi *(UINT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, av_clip_uint8(lrint(*(const double *) pi *(1<< 7))+0x80)) CONV_FUNC(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, av_clip_int16(lrint(*(const double *) pi *(1<< 15)))) CONV_FUNC(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, av_clipl_int32(llrint(*(const double *) pi *(1U<< 31)))) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_DBL, llrint(*(const double *) pi *(UINT64_C(1)<< 63)))#define FMT_PAIR_FUNC(out, in) static conv_func_type *const fmt_pair_to_conv_functions[AV_SAMPLE_FMT_NB *AV_SAMPLE_FMT_NB]={FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S64),};static void cpy1(uint8_t **dst, const uint8_t **src, int len){memcpy(*dst,*src, len);}static void cpy2(uint8_t **dst, const uint8_t **src, int len){memcpy(*dst,*src, 2 *len);}static void cpy4(uint8_t **dst, const uint8_t **src, int len){memcpy(*dst,*src, 4 *len);}static void cpy8(uint8_t **dst, const uint8_t **src, int len){memcpy(*dst,*src, 8 *len);}AudioConvert *swri_audio_convert_alloc(enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt, int channels, const int *ch_map, int flags){AudioConvert *ctx;conv_func_type *f=fmt_pair_to_conv_functions[av_get_packed_sample_fmt(out_fmt)+AV_SAMPLE_FMT_NB *av_get_packed_sample_fmt(in_fmt)];if(!f) return NULL;ctx=av_mallocz(sizeof(*ctx));if(!ctx) return NULL;if(channels==1){in_fmt=av_get_planar_sample_fmt(in_fmt);out_fmt=av_get_planar_sample_fmt(out_fmt);}ctx->channels=channels;ctx->conv_f=f;ctx->ch_map=ch_map;if(in_fmt==AV_SAMPLE_FMT_U8||in_fmt==AV_SAMPLE_FMT_U8P) memset(ctx->silence, 0x80, sizeof(ctx->silence));if(out_fmt==in_fmt &&!ch_map){switch(av_get_bytes_per_sample(in_fmt)){case 1:ctx->simd_f=cpy1;break;case 2:ctx->simd_f=cpy2;break;case 4:ctx->simd_f=cpy4;break;case 8:ctx->simd_f=cpy8;break;}}if(HAVE_X86ASM &&1) swri_audio_convert_init_x86(ctx, out_fmt, in_fmt, channels);if(ARCH_ARM) swri_audio_convert_init_arm(ctx, out_fmt, in_fmt, channels);if(ARCH_AARCH64) swri_audio_convert_init_aarch64(ctx, out_fmt, in_fmt, channels);return ctx;}void swri_audio_convert_free(AudioConvert **ctx){av_freep(ctx);}int swri_audio_convert(AudioConvert *ctx, AudioData *out, AudioData *in, int len){int ch;int off=0;const int os=(out->planar?1:out->ch_count)*out->bps;unsigned misaligned=0;av_assert0(ctx->channels==out->ch_count);if(ctx->in_simd_align_mask){int planes=in->planar?in->ch_count:1;unsigned m=0;for(ch=0;ch< planes;ch++) m|=(intptr_t) in->ch[ch];misaligned|=m &ctx->in_simd_align_mask;}if(ctx->out_simd_align_mask){int planes=out->planar?out->ch_count:1;unsigned m=0;for(ch=0;ch< planes;ch++) m|=(intptr_t) out->ch[ch];misaligned|=m &ctx->out_simd_align_mask;}if(ctx->simd_f &&!ctx->ch_map &&!misaligned){off=len &~15;av_assert1(off >=0);av_assert1(off<=len);av_assert2(ctx->channels==SWR_CH_MAX||!in->ch[ctx->channels]);if(off >0){if(out->planar==in->planar){int planes=out->planar?out->ch_count:1;for(ch=0;ch< planes;ch++){ctx->simd_f(out-> ch ch
Definition: audioconvert.c:56
int channel_count
Definition: mxfenc.c:357
static void mxf_write_h264_desc(AVFormatContext *s, AVStream *st)
Definition: mxfenc.c:1414
#define LIBAVFORMAT_VERSION
Definition: version.h:41
void avio_wb16(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:478
#define flags(name, subs,...)
Definition: cbs_av1.c:561
static int mxf_parse_prores_frame(AVFormatContext *s, AVStream *st, AVPacket *pkt)
Definition: mxfenc.c:2054
static const MXFContainerEssenceEntry mxf_essence_container_uls[]
Definition: mxfenc.c:178
enum AVChromaLocation ff_choose_chroma_location(AVFormatContext *s, AVStream *st)
Chooses a timebase for muxing the specified stream.
Definition: mux.c:118
the normal 219*2^(n-8) "MPEG" YUV ranges
Definition: pixfmt.h:522
static av_always_inline AVRational av_inv_q(AVRational q)
Invert a rational.
Definition: rational.h:159
static const MXFLocalTagPair mxf_user_comments_local_tag[]
Definition: mxfenc.c:517
static int mxf_interleave_get_packet(AVFormatContext *s, AVPacket *out, AVPacket *pkt, int flush)
Definition: mxfenc.c:3104
int sample_rate
Audio only.
Definition: avcodec.h:4078
#define DNXHD_VARIABLE
Indicate that a CIDEntry value must be read in the bitstream.
Definition: dnxhddata.h:40
int64_t footer_partition_offset
Definition: mxfenc.c:336
static const uint8_t smpte_12m_timecode_track_data_ul[]
Definition: mxfenc.c:947
Main libavformat public API header.
static void mxf_write_identification(AVFormatContext *s)
Definition: mxfenc.c:827
void ff_audio_interleave_close(AVFormatContext *s)
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:66
Definition: mxf.h:37
H264SequenceParameterSet * ff_avc_decode_sps(const uint8_t *buf, int buf_size)
Definition: avc.c:311
common internal api header.
struct AVPacketList * next
Definition: avformat.h:2033
static const uint8_t footer_partition_key[]
Definition: mxfenc.c:374
static int mxf_write_essence_container_data(AVFormatContext *s)
Definition: mxfenc.c:1699
static void write_metadata(AVFormatContext *s, unsigned int ts)
Definition: flvenc.c:274
int profile
Codec-specific bitstream restrictions that the stream conforms to.
Definition: avcodec.h:4028
also ITU-R BT470BG
Definition: pixfmt.h:474
void av_init_packet(AVPacket *pkt)
Initialize optional fields of a packet with default values.
Definition: avpacket.c:33
char * key
Definition: dict.h:86
int den
Denominator.
Definition: rational.h:60
#define AVERROR_UNKNOWN
Unknown error, typically from an external library.
Definition: error.h:71
static const MXFCodecUL * mxf_get_data_definition_ul(int type)
Definition: mxfenc.c:665
int field_dominance
tff=1, bff=2
Definition: mxfenc.c:88
static const UID mxf_avc_subdescriptor_key
Definition: mxfenc.c:1159
static const AVOption d10_options[]
Definition: mxfenc.c:3209
static int mxf_compare_timestamps(AVFormatContext *s, const AVPacket *next, const AVPacket *pkt)
Definition: mxfenc.c:3159
struct AVPacketList * packet_buffer_end
Definition: internal.h:77
#define av_free(p)
int last_key_index
index of last key frame
Definition: mxfenc.c:347
int ff_audio_rechunk_interleave(AVFormatContext *s, AVPacket *out, AVPacket *pkt, int flush, int(*get_packet)(AVFormatContext *, AVPacket *, AVPacket *, int), int(*compare_ts)(AVFormatContext *, const AVPacket *, const AVPacket *))
Rechunk audio PCM packets per AudioInterleaveContext->samples_per_frame and interleave them correctly...
char * value
Definition: dict.h:87
int len
void * priv_data
Format private data.
Definition: avformat.h:1386
int ff_mxf_get_content_package_rate(AVRational time_base)
Definition: mxf.c:180
static void mxf_gen_umid(AVFormatContext *s)
Definition: mxfenc.c:2481
static void write_header(FFV1Context *f)
Definition: ffv1enc.c:349
uint8_t umid[16]
unique material identifier
Definition: mxfenc.c:356
static void mxf_write_multi_descriptor(AVFormatContext *s)
Definition: mxfenc.c:1077
static void mxf_compute_edit_unit_byte_count(AVFormatContext *s)
Definition: mxfenc.c:2871
static void mxf_write_wav_desc(AVFormatContext *s, AVStream *st)
Definition: mxfenc.c:1532
int channels
Audio only.
Definition: avcodec.h:4074
static void mxf_write_local_tags(AVIOContext *pb, const MXFLocalTagPair *local_tags, int count)
Definition: mxfenc.c:597
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
Definition: avcodec.h:1479
AVRational av_mul_q(AVRational b, AVRational c)
Multiply two rationals.
Definition: rational.c:80
void avio_wb32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:380
#define FF_PROFILE_PRORES_PROXY
Definition: avcodec.h:3006
FILE * out
Definition: movenc.c:54
Filter the word “frame” indicates either a video frame or a group of audio samples
#define av_freep(p)
static const AVOption opatom_options[]
Definition: mxfenc.c:3225
void INT64 start
Definition: avisynth_c.h:766
#define AV_DICT_IGNORE_SUFFIX
Return first entry in a dictionary whose first part corresponds to the search key, ignoring the suffix of the found key string.
Definition: dict.h:70
unsigned edit_units_count
Definition: mxfenc.c:341
uint32_t av_get_random_seed(void)
Get a seed to use in conjunction with random functions.
Definition: random_seed.c:120
MPEG-1 4:2:0, JPEG 4:2:0, H.263 4:2:0.
Definition: pixfmt.h:545
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1028
UID uid
Definition: mxf.h:77
int stream_index
Definition: avcodec.h:1482
static const uint8_t opatom_ul[]
Definition: mxfenc.c:373
#define DESCRIPTOR_COUNT(essence_container_count)
Definition: mxfenc.c:677
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avformat.h:910
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
int depth
Number of bits in the component.
Definition: pixdesc.h:58
char * name
Definition: mxfdec.c:236
static int mxf_write_opatom_packet(AVFormatContext *s, AVPacket *pkt, MXFIndexEntry *ie)
Definition: mxfenc.c:2841
uint32_t flags
flags such as drop frame, +24 hours support, ...
Definition: timecode.h:43
exp golomb vlc stuff
static void mxf_write_mpegvideo_desc(AVFormatContext *s, AVStream *st)
Definition: mxfenc.c:1432
static void mxf_write_umid(AVFormatContext *s, int type)
Definition: mxfenc.c:530
This structure stores compressed data.
Definition: avcodec.h:1457
static int mxf_parse_dv_frame(AVFormatContext *s, AVStream *st, AVPacket *pkt)
Definition: mxfenc.c:2159
int essence_container_count
Definition: mxfenc.c:337
static void store_version(AVFormatContext *s)
Definition: mxfenc.c:811
static int mxf_write_tagged_value(AVFormatContext *s, const char *name, const char *value)
Definition: mxfenc.c:1552
static const uint8_t random_index_pack_key[]
Definition: mxfenc.c:377
int avpriv_dnxhd_get_hr_frame_size(int cid, int w, int h)
Definition: dnxhddata.c:1095
static void mxf_write_generic_sound_desc(AVFormatContext *s, AVStream *st)
Definition: mxfenc.c:1544
enum MXFMetadataSetType type
Definition: mxfdec.c:229
static int mxf_interleave(AVFormatContext *s, AVPacket *out, AVPacket *pkt, int flush)
Definition: mxfenc.c:3169
struct AVPacketList * last_in_packet_buffer
last packet in packet_buffer for this stream when muxing.
Definition: avformat.h:1105
AVTimecode tc
timecode context
Definition: mxfenc.c:349
static int mxf_write_footer(AVFormatContext *s)
Definition: mxfenc.c:3036
static const MXFLocalTagPair mxf_avc_subdescriptor_local_tags[]
Definition: mxfenc.c:510
const char * name
Definition: opengl_enc.c:102
uint32_t av_timecode_get_smpte_from_framenum(const AVTimecode *tc, int framenum)
Convert frame number to SMPTE 12M binary representation.
Definition: timecode.c:55
AVOutputFormat ff_mxf_opatom_muxer
Definition: mxfenc.c:3272