avformat/mxfenc: automatically update descriptors klv size
[ffmpeg.git] / libavformat / mxfenc.c
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"
48 #include "libavutil/time_internal.h"
49 #include "libavcodec/bytestream.h"
50 #include "libavcodec/dnxhddata.h"
51 #include "libavcodec/dv_profile.h"
52 #include "libavcodec/h264.h"
53 #include "libavcodec/internal.h"
54 #include "audiointerleave.h"
55 #include "avformat.h"
56 #include "avio_internal.h"
57 #include "internal.h"
58 #include "mxf.h"
59 #include "config.h"
60
61 extern AVOutputFormat ff_mxf_d10_muxer;
62 extern AVOutputFormat ff_mxf_opatom_muxer;
63
64 #define EDIT_UNITS_PER_BODY 250
65 #define KAG_SIZE 512
66
67 typedef struct MXFLocalTagPair {
68     int local_tag;
69     UID uid;
70 } MXFLocalTagPair;
71
72 typedef struct MXFIndexEntry {
73     uint8_t flags;
74     uint64_t offset;
75     unsigned slice_offset; ///< offset of audio slice
76     uint16_t temporal_ref;
77 } MXFIndexEntry;
78
79 typedef struct MXFStreamContext {
80     AudioInterleaveContext aic;
81     UID track_essence_element_key;
82     int index;               ///< index in mxf_essence_container_uls table
83     const UID *codec_ul;
84     int order;               ///< interleaving order if dts are equal
85     int interlaced;          ///< whether picture is interlaced
86     int field_dominance;     ///< tff=1, bff=2
87     int component_depth;
88     int color_siting;
89     int signal_standard;
90     int h_chroma_sub_sample;
91     int v_chroma_sub_sample;
92     int temporal_reordering;
93     AVRational aspect_ratio; ///< display aspect ratio
94     int closed_gop;          ///< gop is closed, used in mpeg-2 frame parsing
95     int video_bit_rate;
96 } MXFStreamContext;
97
98 typedef struct MXFContainerEssenceEntry {
99     UID container_ul;
100     UID element_ul;
101     UID codec_ul;
102     void (*write_desc)(AVFormatContext *, AVStream *);
103 } MXFContainerEssenceEntry;
104
105 typedef struct MXFPackage {
106     char *name;
107     enum MXFMetadataSetType type;
108     int instance;
109     struct MXFPackage *ref;
110 } MXFPackage;
111
112 enum ULIndex {
113     INDEX_MPEG2 = 0,
114     INDEX_AES3,
115     INDEX_WAV,
116     INDEX_D10_625_50_50_VIDEO,
117     INDEX_D10_625_50_50_AUDIO,
118     INDEX_D10_525_60_50_VIDEO,
119     INDEX_D10_525_60_50_AUDIO,
120     INDEX_D10_625_50_40_VIDEO,
121     INDEX_D10_625_50_40_AUDIO,
122     INDEX_D10_525_60_40_VIDEO,
123     INDEX_D10_525_60_40_AUDIO,
124     INDEX_D10_625_50_30_VIDEO,
125     INDEX_D10_625_50_30_AUDIO,
126     INDEX_D10_525_60_30_VIDEO,
127     INDEX_D10_525_60_30_AUDIO,
128     INDEX_DV,
129     INDEX_DV25_525_60,
130     INDEX_DV25_625_50,
131     INDEX_DV25_525_60_IEC,
132     INDEX_DV25_625_50_IEC,
133     INDEX_DV50_525_60,
134     INDEX_DV50_625_50,
135     INDEX_DV100_1080_60,
136     INDEX_DV100_1080_50,
137     INDEX_DV100_720_60,
138     INDEX_DV100_720_50,
139     INDEX_DNXHD_1080p_10bit_HIGH,
140     INDEX_DNXHD_1080p_8bit_MEDIUM,
141     INDEX_DNXHD_1080p_8bit_HIGH,
142     INDEX_DNXHD_1080i_10bit_HIGH,
143     INDEX_DNXHD_1080i_8bit_MEDIUM,
144     INDEX_DNXHD_1080i_8bit_HIGH,
145     INDEX_DNXHD_720p_10bit,
146     INDEX_DNXHD_720p_8bit_HIGH,
147     INDEX_DNXHD_720p_8bit_MEDIUM,
148     INDEX_DNXHD_720p_8bit_LOW,
149     INDEX_JPEG2000,
150     INDEX_H264,
151 };
152
153 static const struct {
154     enum AVCodecID id;
155     enum ULIndex index;
156 } mxf_essence_mappings[] = {
157     { AV_CODEC_ID_MPEG2VIDEO, INDEX_MPEG2 },
158     { AV_CODEC_ID_PCM_S24LE,  INDEX_AES3 },
159     { AV_CODEC_ID_PCM_S16LE,  INDEX_AES3 },
160     { AV_CODEC_ID_DVVIDEO,    INDEX_DV },
161     { AV_CODEC_ID_DNXHD,      INDEX_DNXHD_1080p_10bit_HIGH },
162     { AV_CODEC_ID_JPEG2000,   INDEX_JPEG2000 },
163     { AV_CODEC_ID_H264,       INDEX_H264 },
164     { AV_CODEC_ID_NONE }
165 };
166
167 static void mxf_write_wav_desc(AVFormatContext *s, AVStream *st);
168 static void mxf_write_aes3_desc(AVFormatContext *s, AVStream *st);
169 static void mxf_write_mpegvideo_desc(AVFormatContext *s, AVStream *st);
170 static void mxf_write_cdci_desc(AVFormatContext *s, AVStream *st);
171 static void mxf_write_generic_sound_desc(AVFormatContext *s, AVStream *st);
172
173 static const MXFContainerEssenceEntry mxf_essence_container_uls[] = {
174     { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x02,0x0D,0x01,0x03,0x01,0x02,0x04,0x60,0x01 },
175       { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x15,0x01,0x05,0x00 },
176       { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x00,0x00,0x00 },
177       mxf_write_mpegvideo_desc },
178     { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x06,0x03,0x00 },
179       { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x16,0x01,0x03,0x00 },
180       { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 },
181       mxf_write_aes3_desc },
182     { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x06,0x01,0x00 },
183       { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x16,0x01,0x01,0x00 },
184       { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 },
185       mxf_write_wav_desc },
186     // D-10 625/50 PAL 50mb/s
187     { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x01,0x01 },
188       { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x05,0x01,0x01,0x00 },
189       { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x01,0x02,0x01,0x01 },
190       mxf_write_cdci_desc },
191     { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x01,0x01 },
192       { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x06,0x01,0x10,0x00 },
193       { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 },
194       mxf_write_generic_sound_desc },
195     // D-10 525/60 NTSC 50mb/s
196     { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x02,0x01 },
197       { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x05,0x01,0x01,0x00 },
198       { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x01,0x02,0x01,0x02 },
199       mxf_write_cdci_desc },
200     { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x02,0x01 },
201       { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x06,0x01,0x10,0x00 },
202       { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 },
203       mxf_write_generic_sound_desc },
204     // D-10 625/50 PAL 40mb/s
205     { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x03,0x01 },
206       { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x05,0x01,0x01,0x00 },
207       { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x01,0x02,0x01,0x03 },
208       mxf_write_cdci_desc },
209     { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x03,0x01 },
210       { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x06,0x01,0x10,0x00 },
211       { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 },
212       mxf_write_generic_sound_desc },
213     // D-10 525/60 NTSC 40mb/s
214     { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x04,0x01 },
215       { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x05,0x01,0x01,0x00 },
216       { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x01,0x02,0x01,0x04 },
217       mxf_write_cdci_desc },
218     { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x04,0x01 },
219       { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x06,0x01,0x10,0x00 },
220       { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 },
221       mxf_write_generic_sound_desc },
222     // D-10 625/50 PAL 30mb/s
223     { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x05,0x01 },
224       { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x05,0x01,0x01,0x00 },
225       { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x01,0x02,0x01,0x05 },
226       mxf_write_cdci_desc },
227     { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x05,0x01 },
228       { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x06,0x01,0x10,0x00 },
229       { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 },
230       mxf_write_generic_sound_desc },
231     // D-10 525/60 NTSC 30mb/s
232     { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x06,0x01 },
233       { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x05,0x01,0x01,0x00 },
234       { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x01,0x02,0x01,0x06 },
235       mxf_write_cdci_desc },
236     { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x06,0x01 },
237       { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x06,0x01,0x10,0x00 },
238       { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 },
239       mxf_write_generic_sound_desc },
240     // DV Unknown
241     { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x02,0x7F,0x01 },
242       { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x18,0x01,0x01,0x00 },
243       { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x02,0x00,0x00,0x00 },
244       mxf_write_cdci_desc },
245
246     // DV25 525/60
247     { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x02,0x40,0x01 },
248       { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x18,0x01,0x01,0x00 },
249       { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x02,0x02,0x01,0x00 },
250       mxf_write_cdci_desc },
251     // DV25 625/50
252     { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x02,0x41,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,0x02,0x00 },
255       mxf_write_cdci_desc },
256
257     // IEC DV25 525/60
258     { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x02,0x01,0x01 },
259       { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x18,0x01,0x01,0x00 },
260       { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x02,0x02,0x01,0x00 },
261       mxf_write_cdci_desc },
262     // IEC DV25 625/50
263     { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x02,0x02,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,0x01,0x02,0x00 },
266       mxf_write_cdci_desc },
267
268       // DV50 525/60
269     { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x02,0x50,0x01 },
270       { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x18,0x01,0x01,0x00 },
271       { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x02,0x02,0x03,0x00 },
272       mxf_write_cdci_desc },
273     // DV50 625/50
274     { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x02,0x51,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,0x04,0x00 },
277       mxf_write_cdci_desc },
278     // DV100 1080/60
279     { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x02,0x60,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,0x05,0x00 },
282       mxf_write_cdci_desc },
283     // DV100 1080/50
284     { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x02,0x61,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,0x06,0x00 },
287       mxf_write_cdci_desc },
288     // DV100 720/60
289     { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x02,0x62,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,0x07,0x00 },
292       mxf_write_cdci_desc },
293     // DV100 720/50
294     { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x02,0x63,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,0x08,0x00 },
297       mxf_write_cdci_desc },
298     // DNxHD 1080p 10bit high
299     { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x11,0x01,0x00 },
300       { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x15,0x01,0x05,0x00 },
301       { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0A,0x04,0x01,0x02,0x02,0x71,0x01,0x00,0x00 },
302       mxf_write_cdci_desc },
303     // DNxHD 1080p 8bit medium
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,0x03,0x00,0x00 },
307       mxf_write_cdci_desc },
308     // DNxHD 1080p 8bit high
309     { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x11,0x01,0x00 },
310       { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x15,0x01,0x05,0x00 },
311       { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0A,0x04,0x01,0x02,0x02,0x71,0x04,0x00,0x00 },
312       mxf_write_cdci_desc },
313     // DNxHD 1080i 10bit high
314     { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x11,0x01,0x00 },
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,0x71,0x07,0x00,0x00 },
317       mxf_write_cdci_desc },
318     // DNxHD 1080i 8bit medium
319     { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x11,0x01,0x00 },
320       { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x15,0x01,0x05,0x00 },
321       { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0A,0x04,0x01,0x02,0x02,0x71,0x08,0x00,0x00 },
322       mxf_write_cdci_desc },
323     // DNxHD 1080i 8bit high
324     { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x11,0x01,0x00 },
325       { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x15,0x01,0x05,0x00 },
326       { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0A,0x04,0x01,0x02,0x02,0x71,0x09,0x00,0x00 },
327       mxf_write_cdci_desc },
328     // DNxHD 720p 10bit
329     { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x01,0x0d,0x01,0x03,0x01,0x02,0x11,0x01,0x00 },
330       { 0x06,0x0e,0x2b,0x34,0x01,0x02,0x01,0x01,0x0d,0x01,0x03,0x01,0x15,0x01,0x05,0x00 },
331       { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x0A,0x04,0x01,0x02,0x02,0x71,0x10,0x00,0x00 },
332       mxf_write_cdci_desc },
333     // DNxHD 720p 8bit high
334     { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x01,0x0d,0x01,0x03,0x01,0x02,0x11,0x01,0x00 },
335       { 0x06,0x0e,0x2b,0x34,0x01,0x02,0x01,0x01,0x0d,0x01,0x03,0x01,0x15,0x01,0x05,0x00 },
336       { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x0A,0x04,0x01,0x02,0x02,0x71,0x11,0x00,0x00 },
337       mxf_write_cdci_desc },
338     // DNxHD 720p 8bit medium
339     { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x01,0x0d,0x01,0x03,0x01,0x02,0x11,0x01,0x00 },
340       { 0x06,0x0e,0x2b,0x34,0x01,0x02,0x01,0x01,0x0d,0x01,0x03,0x01,0x15,0x01,0x05,0x00 },
341       { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x0A,0x04,0x01,0x02,0x02,0x71,0x12,0x00,0x00 },
342       mxf_write_cdci_desc },
343     // DNxHD 720p 8bit low
344     { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x01,0x0d,0x01,0x03,0x01,0x02,0x11,0x01,0x00 },
345       { 0x06,0x0e,0x2b,0x34,0x01,0x02,0x01,0x01,0x0d,0x01,0x03,0x01,0x15,0x01,0x05,0x00 },
346       { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x0A,0x04,0x01,0x02,0x02,0x71,0x13,0x00,0x00 },
347       mxf_write_cdci_desc },
348     // JPEG2000
349     { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x07,0x0d,0x01,0x03,0x01,0x02,0x0c,0x01,0x00 },
350       { 0x06,0x0e,0x2b,0x34,0x01,0x02,0x01,0x01,0x0d,0x01,0x03,0x01,0x15,0x01,0x08,0x00 },
351       { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x07,0x04,0x01,0x02,0x02,0x03,0x01,0x01,0x00 },
352       mxf_write_cdci_desc },
353     // H.264
354     { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x0D,0x01,0x03,0x01,0x02,0x10,0x60,0x01 },
355       { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x15,0x01,0x05,0x00 },
356       { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x00,0x00,0x00 },
357       mxf_write_mpegvideo_desc },
358     { { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 },
359       { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 },
360       { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 },
361       NULL },
362 };
363
364 typedef struct MXFContext {
365     AVClass *av_class;
366     int64_t footer_partition_offset;
367     int essence_container_count;
368     AVRational time_base;
369     int header_written;
370     MXFIndexEntry *index_entries;
371     unsigned edit_units_count;
372     uint64_t timestamp;   ///< timestamp, as year(16),month(8),day(8),hour(8),minutes(8),msec/4(8)
373     uint8_t slice_count;  ///< index slice count minus 1 (1 if no audio, 0 otherwise)
374     int last_indexed_edit_unit;
375     uint64_t *body_partition_offset;
376     unsigned body_partitions_count;
377     int last_key_index;  ///< index of last key frame
378     uint64_t duration;
379     AVTimecode tc;       ///< timecode context
380     AVStream *timecode_track;
381     int timecode_base;       ///< rounded time code base (25 or 30)
382     int edit_unit_byte_count; ///< fixed edit unit byte count
383     uint64_t body_offset;
384     uint32_t instance_number;
385     uint8_t umid[16];        ///< unique material identifier
386     int channel_count;
387     int signal_standard;
388     uint32_t tagged_value_count;
389     AVRational audio_edit_rate;
390     int store_user_comments;
391     int track_instance_count; // used to generate MXFTrack uuids
392 } MXFContext;
393
394 static const uint8_t uuid_base[]            = { 0xAD,0xAB,0x44,0x24,0x2f,0x25,0x4d,0xc7,0x92,0xff,0x29,0xbd };
395 static const uint8_t umid_ul[]              = { 0x06,0x0A,0x2B,0x34,0x01,0x01,0x01,0x05,0x01,0x01,0x0D,0x00,0x13 };
396
397 /**
398  * complete key for operation pattern, partitions, and primer pack
399  */
400 static const uint8_t op1a_ul[]                     = { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x02,0x01,0x01,0x01,0x09,0x00 };
401 static const uint8_t opatom_ul[]                   = { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x02,0x0D,0x01,0x02,0x01,0x10,0x03,0x00,0x00 };
402 static const uint8_t footer_partition_key[]        = { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0D,0x01,0x02,0x01,0x01,0x04,0x04,0x00 }; // ClosedComplete
403 static const uint8_t primer_pack_key[]             = { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0D,0x01,0x02,0x01,0x01,0x05,0x01,0x00 };
404 static const uint8_t index_table_segment_key[]     = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x10,0x01,0x00 };
405 static const uint8_t random_index_pack_key[]       = { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0D,0x01,0x02,0x01,0x01,0x11,0x01,0x00 };
406 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
407 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
408 static const uint8_t klv_fill_key[]                = { 0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x03,0x01,0x02,0x10,0x01,0x00,0x00,0x00 };
409 static const uint8_t body_partition_key[]          = { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0D,0x01,0x02,0x01,0x01,0x03,0x04,0x00 }; // ClosedComplete
410
411 /**
412  * partial key for header metadata
413  */
414 static const uint8_t header_metadata_key[]  = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0D,0x01,0x01,0x01,0x01 };
415 static const uint8_t multiple_desc_ul[]     = { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x0D,0x01,0x03,0x01,0x02,0x7F,0x01,0x00 };
416
417 /**
418  * SMPTE RP210 http://www.smpte-ra.org/mdd/index.html
419  *             https://smpte-ra.org/sites/default/files/Labels.xml
420  */
421 static const MXFLocalTagPair mxf_local_tag_batch[] = {
422     // preface set
423     { 0x3C0A, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x01,0x01,0x15,0x02,0x00,0x00,0x00,0x00}}, /* Instance UID */
424     { 0x3B02, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x10,0x02,0x04,0x00,0x00}}, /* Last Modified Date */
425     { 0x3B05, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x03,0x01,0x02,0x01,0x05,0x00,0x00,0x00}}, /* Version */
426     { 0x3B07, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x03,0x01,0x02,0x01,0x04,0x00,0x00,0x00}}, /* Object Model Version */
427     { 0x3B06, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x06,0x04,0x00,0x00}}, /* Identifications reference */
428     { 0x3B03, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x02,0x01,0x00,0x00}}, /* Content Storage reference */
429     { 0x3B09, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x01,0x02,0x02,0x03,0x00,0x00,0x00,0x00}}, /* Operational Pattern UL */
430     { 0x3B0A, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x01,0x02,0x02,0x10,0x02,0x01,0x00,0x00}}, /* Essence Containers UL batch */
431     { 0x3B0B, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x01,0x02,0x02,0x10,0x02,0x02,0x00,0x00}}, /* DM Schemes UL batch */
432     // Identification
433     { 0x3C09, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x20,0x07,0x01,0x01,0x00,0x00,0x00}}, /* This Generation UID */
434     { 0x3C01, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x20,0x07,0x01,0x02,0x01,0x00,0x00}}, /* Company Name */
435     { 0x3C02, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x20,0x07,0x01,0x03,0x01,0x00,0x00}}, /* Product Name */
436     { 0x3C03, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x20,0x07,0x01,0x04,0x00,0x00,0x00}}, /* Product Version */
437     { 0x3C04, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x20,0x07,0x01,0x05,0x01,0x00,0x00}}, /* Version String */
438     { 0x3C05, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x20,0x07,0x01,0x07,0x00,0x00,0x00}}, /* Product ID */
439     { 0x3C06, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x10,0x02,0x03,0x00,0x00}}, /* Modification Date */
440     { 0x3C07, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x20,0x07,0x01,0x0A,0x00,0x00,0x00}}, /* Toolkit Version */
441     { 0x3C08, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x20,0x07,0x01,0x06,0x01,0x00,0x00}}, /* Platform */
442     // Content Storage
443     { 0x1901, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x05,0x01,0x00,0x00}}, /* Package strong reference batch */
444     { 0x1902, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x05,0x02,0x00,0x00}}, /* Package strong reference batch */
445     // Essence Container Data
446     { 0x2701, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x06,0x01,0x00,0x00,0x00}}, /* Linked Package UID */
447     { 0x3F07, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x01,0x03,0x04,0x04,0x00,0x00,0x00,0x00}}, /* BodySID */
448     // Package
449     { 0x4401, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x01,0x01,0x15,0x10,0x00,0x00,0x00,0x00}}, /* Package UID */
450     { 0x4405, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x10,0x01,0x03,0x00,0x00}}, /* Package Creation Date */
451     { 0x4404, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x10,0x02,0x05,0x00,0x00}}, /* Package Modified Date */
452     { 0x4402, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x01,0x03,0x03,0x02,0x01,0x00,0x00,0x00}}, /* Package Name */
453     { 0x4403, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x06,0x05,0x00,0x00}}, /* Tracks Strong reference array */
454     { 0x4701, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x02,0x03,0x00,0x00}}, /* Descriptor */
455     // Track
456     { 0x4801, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x01,0x07,0x01,0x01,0x00,0x00,0x00,0x00}}, /* Track ID */
457     { 0x4804, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x01,0x04,0x01,0x03,0x00,0x00,0x00,0x00}}, /* Track Number */
458     { 0x4B01, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x30,0x04,0x05,0x00,0x00,0x00,0x00}}, /* Edit Rate */
459     { 0x4B02, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x03,0x01,0x03,0x00,0x00}}, /* Origin */
460     { 0x4803, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x02,0x04,0x00,0x00}}, /* Sequence reference */
461     // Sequence
462     { 0x0201, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x07,0x01,0x00,0x00,0x00,0x00,0x00}}, /* Data Definition UL */
463     { 0x0202, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x02,0x01,0x01,0x03,0x00,0x00}}, /* Duration */
464     { 0x1001, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x06,0x09,0x00,0x00}}, /* Structural Components reference array */
465     // Source Clip
466     { 0x1201, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x03,0x01,0x04,0x00,0x00}}, /* Start position */
467     { 0x1101, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x03,0x01,0x00,0x00,0x00}}, /* SourcePackageID */
468     { 0x1102, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x03,0x02,0x00,0x00,0x00}}, /* SourceTrackID */
469     // Timecode Component
470     { 0x1501, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x03,0x01,0x05,0x00,0x00}}, /* Start Time Code */
471     { 0x1502, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x04,0x01,0x01,0x02,0x06,0x00,0x00}}, /* Rounded Time Code Base */
472     { 0x1503, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x04,0x01,0x01,0x05,0x00,0x00,0x00}}, /* Drop Frame */
473     // File Descriptor
474     { 0x3F01, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x06,0x01,0x01,0x04,0x06,0x0B,0x00,0x00}}, /* Sub Descriptors reference array */
475     { 0x3006, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x06,0x01,0x01,0x03,0x05,0x00,0x00,0x00}}, /* Linked Track ID */
476     { 0x3001, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x06,0x01,0x01,0x00,0x00,0x00,0x00}}, /* SampleRate */
477     { 0x3002, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x06,0x01,0x02,0x00,0x00,0x00,0x00}}, /* ContainerDuration */
478     { 0x3004, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x01,0x02,0x00,0x00}}, /* Essence Container */
479     // Generic Picture Essence Descriptor
480     { 0x320C, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x03,0x01,0x04,0x00,0x00,0x00}}, /* Frame Layout */
481     { 0x320D, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x01,0x03,0x02,0x05,0x00,0x00,0x00}}, /* Video Line Map */
482     { 0x3203, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x02,0x02,0x00,0x00,0x00}}, /* Stored Width */
483     { 0x3202, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x02,0x01,0x00,0x00,0x00}}, /* Stored Height */
484     { 0x3216, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x01,0x03,0x02,0x08,0x00,0x00,0x00}}, /* Stored F2 Offset */
485     { 0x3205, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x01,0x08,0x00,0x00,0x00}}, /* Sampled Width */
486     { 0x3204, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x01,0x07,0x00,0x00,0x00}}, /* Sampled Height */
487     { 0x3206, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x01,0x09,0x00,0x00,0x00}}, /* Sampled X Offset */
488     { 0x3207, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x01,0x0A,0x00,0x00,0x00}}, /* Sampled Y Offset */
489     { 0x3209, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x01,0x0C,0x00,0x00,0x00}}, /* Display Width */
490     { 0x3208, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x01,0x0B,0x00,0x00,0x00}}, /* Display Height */
491     { 0x320A, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x01,0x0D,0x00,0x00,0x00}}, /* Display X offset */
492     { 0x320B, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x01,0x0E,0x00,0x00,0x00}}, /* Presentation Y offset */
493     { 0x3217, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x01,0x03,0x02,0x07,0x00,0x00,0x00}}, /* Display F2 offset */
494     { 0x320E, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x01,0x01,0x01,0x00,0x00,0x00}}, /* Aspect Ratio */
495     { 0x3210, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x01,0x02,0x01,0x01,0x01,0x02,0x00}}, /* Transfer characteristic */
496     { 0x3213, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x18,0x01,0x02,0x00,0x00,0x00,0x00}}, /* Image Start Offset */
497     { 0x3214, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x18,0x01,0x03,0x00,0x00,0x00,0x00}}, /* Image End Offset */
498     { 0x3201, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x01,0x06,0x01,0x00,0x00,0x00,0x00}}, /* Picture Essence Coding */
499     { 0x3212, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x01,0x03,0x01,0x06,0x00,0x00,0x00}}, /* Field Dominance (Opt) */
500     { 0x3215, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x05,0x01,0x13,0x00,0x00,0x00,0x00}}, /* Signal Standard */
501     // CDCI Picture Essence Descriptor
502     { 0x3301, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x01,0x05,0x03,0x0A,0x00,0x00,0x00}}, /* Component Depth */
503     { 0x3302, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x01,0x05,0x00,0x00,0x00}}, /* Horizontal Subsampling */
504     { 0x3308, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x01,0x05,0x01,0x10,0x00,0x00,0x00}}, /* Vertical Subsampling */
505     { 0x3303, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x01,0x06,0x00,0x00,0x00}}, /* Color Siting */
506     { 0x3307, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x18,0x01,0x04,0x00,0x00,0x00,0x00}}, /* Padding Bits */
507     { 0x3304, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x03,0x03,0x00,0x00,0x00}}, /* Black Ref level */
508     { 0x3305, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x03,0x04,0x00,0x00,0x00}}, /* White Ref level */
509     { 0x3306, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x01,0x05,0x03,0x05,0x00,0x00,0x00}}, /* Color Range */
510     // Generic Sound Essence Descriptor
511     { 0x3D02, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x04,0x02,0x03,0x01,0x04,0x00,0x00,0x00}}, /* Locked/Unlocked */
512     { 0x3D03, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x02,0x03,0x01,0x01,0x01,0x00,0x00}}, /* Audio sampling rate */
513     { 0x3D04, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x02,0x01,0x01,0x03,0x00,0x00,0x00}}, /* Audio Ref Level */
514     { 0x3D07, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x02,0x01,0x01,0x04,0x00,0x00,0x00}}, /* ChannelCount */
515     { 0x3D01, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x04,0x02,0x03,0x03,0x04,0x00,0x00,0x00}}, /* Quantization bits */
516     { 0x3D06, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x02,0x04,0x02,0x00,0x00,0x00,0x00}}, /* Sound Essence Compression */
517     // Index Table Segment
518     { 0x3F0B, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x05,0x30,0x04,0x06,0x00,0x00,0x00,0x00}}, /* Index Edit Rate */
519     { 0x3F0C, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x07,0x02,0x01,0x03,0x01,0x0A,0x00,0x00}}, /* Index Start Position */
520     { 0x3F0D, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x07,0x02,0x02,0x01,0x01,0x02,0x00,0x00}}, /* Index Duration */
521     { 0x3F05, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x04,0x06,0x02,0x01,0x00,0x00,0x00,0x00}}, /* Edit Unit Byte Count */
522     { 0x3F06, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x01,0x03,0x04,0x05,0x00,0x00,0x00,0x00}}, /* IndexSID */
523     { 0x3F08, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x04,0x04,0x04,0x01,0x01,0x00,0x00,0x00}}, /* Slice Count */
524     { 0x3F09, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x04,0x04,0x01,0x06,0x00,0x00,0x00}}, /* Delta Entry Array */
525     { 0x3F0A, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x04,0x04,0x02,0x05,0x00,0x00,0x00}}, /* Index Entry Array */
526     // MPEG video Descriptor
527     { 0x8000, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x01,0x06,0x02,0x01,0x0B,0x00,0x00}}, /* BitRate */
528     { 0x8007, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x01,0x06,0x02,0x01,0x0A,0x00,0x00}}, /* ProfileAndLevel */
529     // Wave Audio Essence Descriptor
530     { 0x3D09, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x02,0x03,0x03,0x05,0x00,0x00,0x00}}, /* Average Bytes Per Second */
531     { 0x3D0A, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x02,0x03,0x02,0x01,0x00,0x00,0x00}}, /* Block Align */
532 };
533
534 static const MXFLocalTagPair mxf_user_comments_local_tag[] = {
535     { 0x4406, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x03,0x02,0x01,0x02,0x0C,0x00,0x00,0x00}}, /* User Comments */
536     { 0x5001, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x03,0x02,0x01,0x02,0x09,0x01,0x00,0x00}}, /* Name */
537     { 0x5003, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x03,0x02,0x01,0x02,0x0A,0x01,0x00,0x00}}, /* Value */
538 };
539
540 static void mxf_write_uuid(AVIOContext *pb, enum MXFMetadataSetType type, int value)
541 {
542     avio_write(pb, uuid_base, 12);
543     avio_wb16(pb, type);
544     avio_wb16(pb, value);
545 }
546
547 static void mxf_write_umid(AVFormatContext *s, int type)
548 {
549     MXFContext *mxf = s->priv_data;
550     avio_write(s->pb, umid_ul, 13);
551     avio_wb24(s->pb, mxf->instance_number);
552     avio_write(s->pb, mxf->umid, 15);
553     avio_w8(s->pb, type);
554 }
555
556 static void mxf_write_refs_count(AVIOContext *pb, int ref_count)
557 {
558     avio_wb32(pb, ref_count);
559     avio_wb32(pb, 16);
560 }
561
562 static int klv_ber_length(uint64_t len)
563 {
564     if (len < 128)
565         return 1;
566     else
567         return (av_log2(len) >> 3) + 2;
568 }
569
570 static int klv_encode_ber_length(AVIOContext *pb, uint64_t len)
571 {
572     // Determine the best BER size
573     int size;
574     if (len < 128) {
575         //short form
576         avio_w8(pb, len);
577         return 1;
578     }
579
580     size = (av_log2(len) >> 3) + 1;
581
582     // long form
583     avio_w8(pb, 0x80 + size);
584     while(size) {
585         size--;
586         avio_w8(pb, len >> 8 * size & 0xff);
587     }
588     return 0;
589 }
590
591 static void klv_encode_ber4_length(AVIOContext *pb, int len)
592 {
593     avio_w8(pb, 0x80 + 3);
594     avio_wb24(pb, len);
595 }
596
597 static void klv_encode_ber9_length(AVIOContext *pb, uint64_t len)
598 {
599     avio_w8(pb, 0x80 + 8);
600     avio_wb64(pb, len);
601 }
602
603 /*
604  * Get essence container ul index
605  */
606 static int mxf_get_essence_container_ul_index(enum AVCodecID id)
607 {
608     int i;
609     for (i = 0; mxf_essence_mappings[i].id; i++)
610         if (mxf_essence_mappings[i].id == id)
611             return mxf_essence_mappings[i].index;
612     return -1;
613 }
614
615 static void mxf_write_primer_pack(AVFormatContext *s)
616 {
617     MXFContext *mxf = s->priv_data;
618     AVIOContext *pb = s->pb;
619     int local_tag_number, i = 0;
620
621     local_tag_number = FF_ARRAY_ELEMS(mxf_local_tag_batch);
622     local_tag_number += mxf->store_user_comments * FF_ARRAY_ELEMS(mxf_user_comments_local_tag);
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 }
640
641 static void mxf_write_local_tag(AVIOContext *pb, int size, int tag)
642 {
643     avio_wb16(pb, tag);
644     avio_wb16(pb, size);
645 }
646
647 static void mxf_write_metadata_key(AVIOContext *pb, unsigned int value)
648 {
649     avio_write(pb, header_metadata_key, 13);
650     avio_wb24(pb, value);
651 }
652
653 static void mxf_free(AVFormatContext *s)
654 {
655     int i;
656
657     for (i = 0; i < s->nb_streams; i++) {
658         AVStream *st = s->streams[i];
659         av_freep(&st->priv_data);
660     }
661 }
662
663 static const MXFCodecUL *mxf_get_data_definition_ul(int type)
664 {
665     const MXFCodecUL *uls = ff_mxf_data_definition_uls;
666     while (uls->uid[0]) {
667         if (type == uls->id)
668             break;
669         uls++;
670     }
671     return uls;
672 }
673
674 //one EC -> one descriptor. N ECs -> MultipleDescriptor + N descriptors
675 #define DESCRIPTOR_COUNT(essence_container_count) \
676     (essence_container_count > 1 ? essence_container_count + 1 : essence_container_count)
677
678 static void mxf_write_essence_container_refs(AVFormatContext *s)
679 {
680     MXFContext *c = s->priv_data;
681     AVIOContext *pb = s->pb;
682     int i;
683
684     mxf_write_refs_count(pb, DESCRIPTOR_COUNT(c->essence_container_count));
685     av_log(s,AV_LOG_DEBUG, "essence container count:%d\n", c->essence_container_count);
686     for (i = 0; i < c->essence_container_count; i++) {
687         MXFStreamContext *sc = s->streams[i]->priv_data;
688         avio_write(pb, mxf_essence_container_uls[sc->index].container_ul, 16);
689     }
690
691     if (c->essence_container_count > 1)
692         avio_write(pb, multiple_desc_ul, 16);
693 }
694
695 static void mxf_write_preface(AVFormatContext *s)
696 {
697     MXFContext *mxf = s->priv_data;
698     AVIOContext *pb = s->pb;
699
700     mxf_write_metadata_key(pb, 0x012f00);
701     PRINT_KEY(s, "preface key", pb->buf_ptr - 16);
702     klv_encode_ber_length(pb, 138 + 16LL * DESCRIPTOR_COUNT(mxf->essence_container_count));
703
704     // write preface set uid
705     mxf_write_local_tag(pb, 16, 0x3C0A);
706     mxf_write_uuid(pb, Preface, 0);
707     PRINT_KEY(s, "preface uid", pb->buf_ptr - 16);
708
709     // last modified date
710     mxf_write_local_tag(pb, 8, 0x3B02);
711     avio_wb64(pb, mxf->timestamp);
712
713     // write version
714     mxf_write_local_tag(pb, 2, 0x3B05);
715     avio_wb16(pb, 259); // v1.3
716
717     // Object Model Version
718     mxf_write_local_tag(pb, 4, 0x3B07);
719     avio_wb32(pb, 1);
720
721     // write identification_refs
722     mxf_write_local_tag(pb, 16 + 8, 0x3B06);
723     mxf_write_refs_count(pb, 1);
724     mxf_write_uuid(pb, Identification, 0);
725
726     // write content_storage_refs
727     mxf_write_local_tag(pb, 16, 0x3B03);
728     mxf_write_uuid(pb, ContentStorage, 0);
729
730     // operational pattern
731     mxf_write_local_tag(pb, 16, 0x3B09);
732     if (s->oformat == &ff_mxf_opatom_muxer)
733         avio_write(pb, opatom_ul, 16);
734     else
735         avio_write(pb, op1a_ul, 16);
736
737     // write essence_container_refs
738     mxf_write_local_tag(pb, 8 + 16LL * DESCRIPTOR_COUNT(mxf->essence_container_count), 0x3B0A);
739     mxf_write_essence_container_refs(s);
740
741     // write dm_scheme_refs
742     mxf_write_local_tag(pb, 8, 0x3B0B);
743     avio_wb64(pb, 0);
744 }
745
746 /*
747  * Returns the length of the UTF-16 string, in 16-bit characters, that would result
748  * from decoding the utf-8 string.
749  */
750 static uint64_t mxf_utf16len(const char *utf8_str)
751 {
752     const uint8_t *q = utf8_str;
753     uint64_t size = 0;
754     while (*q) {
755         uint32_t ch;
756         GET_UTF8(ch, *q++, goto invalid;)
757         if (ch < 0x10000)
758             size++;
759         else
760             size += 2;
761         continue;
762 invalid:
763         av_log(NULL, AV_LOG_ERROR, "Invalid UTF8 sequence in mxf_utf16len\n\n");
764     }
765     size += 1;
766     return size;
767 }
768
769 /*
770  * Returns the calculated length a local tag containing an utf-8 string as utf-16
771  */
772 static int mxf_utf16_local_tag_length(const char *utf8_str)
773 {
774     uint64_t size;
775
776     if (!utf8_str)
777         return 0;
778
779     size = mxf_utf16len(utf8_str);
780     if (size >= UINT16_MAX/2) {
781         av_log(NULL, AV_LOG_ERROR, "utf16 local tag size %"PRIx64" invalid (too large), ignoring\n", size);
782         return 0;
783     }
784
785     return 4 + size * 2;
786 }
787
788 /*
789  * Write a local tag containing an utf-8 string as utf-16
790  */
791 static void mxf_write_local_tag_utf16(AVIOContext *pb, int tag, const char *value)
792 {
793     uint64_t size = mxf_utf16len(value);
794
795     if (size >= UINT16_MAX/2) {
796         av_log(NULL, AV_LOG_ERROR, "utf16 local tag size %"PRIx64" invalid (too large), ignoring\n", size);
797         return;
798     }
799
800     mxf_write_local_tag(pb, size*2, tag);
801     avio_put_str16be(pb, value);
802 }
803
804 static void store_version(AVFormatContext *s){
805     AVIOContext *pb = s->pb;
806
807     if (s->flags & AVFMT_FLAG_BITEXACT) {
808         avio_wb16(pb, 0); // major
809         avio_wb16(pb, 0); // minor
810         avio_wb16(pb, 0); // tertiary
811     } else {
812         avio_wb16(pb, LIBAVFORMAT_VERSION_MAJOR); // major
813         avio_wb16(pb, LIBAVFORMAT_VERSION_MINOR); // minor
814         avio_wb16(pb, LIBAVFORMAT_VERSION_MICRO); // tertiary
815     }
816     avio_wb16(pb, 0); // patch
817     avio_wb16(pb, 0); // release
818 }
819
820 static void mxf_write_identification(AVFormatContext *s)
821 {
822     MXFContext *mxf = s->priv_data;
823     AVIOContext *pb = s->pb;
824     const char *company = "FFmpeg";
825     const char *product = s->oformat != &ff_mxf_opatom_muxer ? "OP1a Muxer" : "OPAtom Muxer";
826     const char *version;
827     int length;
828
829     mxf_write_metadata_key(pb, 0x013000);
830     PRINT_KEY(s, "identification key", pb->buf_ptr - 16);
831
832     version = s->flags & AVFMT_FLAG_BITEXACT ?
833         "0.0.0" : AV_STRINGIFY(LIBAVFORMAT_VERSION);
834     length = 100 +mxf_utf16_local_tag_length(company) +
835                   mxf_utf16_local_tag_length(product) +
836                   mxf_utf16_local_tag_length(version);
837     klv_encode_ber_length(pb, length);
838
839     // write uid
840     mxf_write_local_tag(pb, 16, 0x3C0A);
841     mxf_write_uuid(pb, Identification, 0);
842     PRINT_KEY(s, "identification uid", pb->buf_ptr - 16);
843
844     // write generation uid
845     mxf_write_local_tag(pb, 16, 0x3C09);
846     mxf_write_uuid(pb, Identification, 1);
847     mxf_write_local_tag_utf16(pb, 0x3C01, company); // Company Name
848     mxf_write_local_tag_utf16(pb, 0x3C02, product); // Product Name
849
850     mxf_write_local_tag(pb, 10, 0x3C03); // Product Version
851     store_version(s);
852
853     mxf_write_local_tag_utf16(pb, 0x3C04, version); // Version String
854
855     // write product uid
856     mxf_write_local_tag(pb, 16, 0x3C05);
857     mxf_write_uuid(pb, Identification, 2);
858
859     // modification date
860     mxf_write_local_tag(pb, 8, 0x3C06);
861     avio_wb64(pb, mxf->timestamp);
862
863     mxf_write_local_tag(pb, 10, 0x3C07); // Toolkit Version
864     store_version(s);
865 }
866
867 static void mxf_write_content_storage(AVFormatContext *s, MXFPackage *packages, int package_count)
868 {
869     AVIOContext *pb = s->pb;
870     int i;
871
872     mxf_write_metadata_key(pb, 0x011800);
873     PRINT_KEY(s, "content storage key", pb->buf_ptr - 16);
874     klv_encode_ber_length(pb, 60 + (16 * package_count));
875
876     // write uid
877     mxf_write_local_tag(pb, 16, 0x3C0A);
878     mxf_write_uuid(pb, ContentStorage, 0);
879     PRINT_KEY(s, "content storage uid", pb->buf_ptr - 16);
880
881     // write package reference
882     mxf_write_local_tag(pb, 16 * package_count + 8, 0x1901);
883     mxf_write_refs_count(pb, package_count);
884     for (i = 0; i < package_count; i++) {
885         mxf_write_uuid(pb, packages[i].type, packages[i].instance);
886     }
887
888     // write essence container data
889     mxf_write_local_tag(pb, 8 + 16, 0x1902);
890     mxf_write_refs_count(pb, 1);
891     mxf_write_uuid(pb, EssenceContainerData, 0);
892 }
893
894 static void mxf_write_track(AVFormatContext *s, AVStream *st, MXFPackage *package)
895 {
896     MXFContext *mxf = s->priv_data;
897     AVIOContext *pb = s->pb;
898     MXFStreamContext *sc = st->priv_data;
899
900     mxf_write_metadata_key(pb, 0x013b00);
901     PRINT_KEY(s, "track key", pb->buf_ptr - 16);
902     klv_encode_ber_length(pb, 80);
903
904     // write track uid
905     mxf_write_local_tag(pb, 16, 0x3C0A);
906     mxf_write_uuid(pb, Track, mxf->track_instance_count);
907     PRINT_KEY(s, "track uid", pb->buf_ptr - 16);
908
909     // write track id
910     mxf_write_local_tag(pb, 4, 0x4801);
911     avio_wb32(pb, st->index+2);
912
913     // write track number
914     mxf_write_local_tag(pb, 4, 0x4804);
915     if (package->type == MaterialPackage)
916         avio_wb32(pb, 0); // track number of material package is 0
917     else
918         avio_write(pb, sc->track_essence_element_key + 12, 4);
919
920     // write edit rate
921     mxf_write_local_tag(pb, 8, 0x4B01);
922
923     if (st == mxf->timecode_track && s->oformat == &ff_mxf_opatom_muxer) {
924         avio_wb32(pb, mxf->tc.rate.num);
925         avio_wb32(pb, mxf->tc.rate.den);
926     } else {
927         avio_wb32(pb, mxf->time_base.den);
928         avio_wb32(pb, mxf->time_base.num);
929     }
930
931     // write origin
932     mxf_write_local_tag(pb, 8, 0x4B02);
933     avio_wb64(pb, 0);
934
935     // write sequence refs
936     mxf_write_local_tag(pb, 16, 0x4803);
937     mxf_write_uuid(pb, Sequence, mxf->track_instance_count);
938 }
939
940 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 };
941
942 static void mxf_write_common_fields(AVFormatContext *s, AVStream *st)
943 {
944     MXFContext *mxf = s->priv_data;
945     AVIOContext *pb = s->pb;
946
947     // find data define uls
948     mxf_write_local_tag(pb, 16, 0x0201);
949     if (st == mxf->timecode_track)
950         avio_write(pb, smpte_12m_timecode_track_data_ul, 16);
951     else {
952         const MXFCodecUL *data_def_ul = mxf_get_data_definition_ul(st->codecpar->codec_type);
953         avio_write(pb, data_def_ul->uid, 16);
954     }
955
956     // write duration
957     mxf_write_local_tag(pb, 8, 0x0202);
958
959     if (st != mxf->timecode_track && s->oformat == &ff_mxf_opatom_muxer && st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
960         avio_wb64(pb, mxf->body_offset / mxf->edit_unit_byte_count);
961     } else {
962         avio_wb64(pb, mxf->duration);
963     }
964 }
965
966 static void mxf_write_sequence(AVFormatContext *s, AVStream *st, MXFPackage *package)
967 {
968     MXFContext *mxf = s->priv_data;
969     AVIOContext *pb = s->pb;
970     enum MXFMetadataSetType component;
971
972     mxf_write_metadata_key(pb, 0x010f00);
973     PRINT_KEY(s, "sequence key", pb->buf_ptr - 16);
974     klv_encode_ber_length(pb, 80);
975
976     mxf_write_local_tag(pb, 16, 0x3C0A);
977     mxf_write_uuid(pb, Sequence, mxf->track_instance_count);
978
979     PRINT_KEY(s, "sequence uid", pb->buf_ptr - 16);
980     mxf_write_common_fields(s, st);
981
982     // write structural component
983     mxf_write_local_tag(pb, 16 + 8, 0x1001);
984     mxf_write_refs_count(pb, 1);
985     if (st == mxf->timecode_track)
986         component = TimecodeComponent;
987     else
988         component = SourceClip;
989
990     mxf_write_uuid(pb, component, mxf->track_instance_count);
991 }
992
993 static void mxf_write_timecode_component(AVFormatContext *s, AVStream *st, MXFPackage *package)
994 {
995     MXFContext *mxf = s->priv_data;
996     AVIOContext *pb = s->pb;
997
998     mxf_write_metadata_key(pb, 0x011400);
999     klv_encode_ber_length(pb, 75);
1000
1001     // UID
1002     mxf_write_local_tag(pb, 16, 0x3C0A);
1003     mxf_write_uuid(pb, TimecodeComponent, mxf->track_instance_count);
1004
1005     mxf_write_common_fields(s, st);
1006
1007     // Start Time Code
1008     mxf_write_local_tag(pb, 8, 0x1501);
1009     avio_wb64(pb, mxf->tc.start);
1010
1011     // Rounded Time Code Base
1012     mxf_write_local_tag(pb, 2, 0x1502);
1013     avio_wb16(pb, mxf->timecode_base);
1014
1015     // Drop Frame
1016     mxf_write_local_tag(pb, 1, 0x1503);
1017     avio_w8(pb, !!(mxf->tc.flags & AV_TIMECODE_FLAG_DROPFRAME));
1018 }
1019
1020 static void mxf_write_structural_component(AVFormatContext *s, AVStream *st, MXFPackage *package)
1021 {
1022     MXFContext *mxf = s->priv_data;
1023     AVIOContext *pb = s->pb;
1024     int i;
1025
1026     mxf_write_metadata_key(pb, 0x011100);
1027     PRINT_KEY(s, "sturctural component key", pb->buf_ptr - 16);
1028     klv_encode_ber_length(pb, 108);
1029
1030     // write uid
1031     mxf_write_local_tag(pb, 16, 0x3C0A);
1032     mxf_write_uuid(pb, SourceClip, mxf->track_instance_count);
1033
1034     PRINT_KEY(s, "structural component uid", pb->buf_ptr - 16);
1035     mxf_write_common_fields(s, st);
1036
1037     // write start_position
1038     mxf_write_local_tag(pb, 8, 0x1201);
1039     avio_wb64(pb, 0);
1040
1041     // write source package uid, end of the reference
1042     mxf_write_local_tag(pb, 32, 0x1101);
1043     if (!package->ref) {
1044         for (i = 0; i < 4; i++)
1045             avio_wb64(pb, 0);
1046     } else
1047         mxf_write_umid(s, package->ref->instance);
1048
1049     // write source track id
1050     mxf_write_local_tag(pb, 4, 0x1102);
1051     if (package->type == SourcePackage && !package->ref)
1052         avio_wb32(pb, 0);
1053     else
1054         avio_wb32(pb, st->index+2);
1055 }
1056
1057 static void mxf_write_tape_descriptor(AVFormatContext *s)
1058 {
1059     AVIOContext *pb = s->pb;
1060
1061     mxf_write_metadata_key(pb, 0x012e00);
1062     PRINT_KEY(s, "tape descriptor key", pb->buf_ptr - 16);
1063     klv_encode_ber_length(pb, 20);
1064     mxf_write_local_tag(pb, 16, 0x3C0A);
1065     mxf_write_uuid(pb, TapeDescriptor, 0);
1066     PRINT_KEY(s, "tape_desc uid", pb->buf_ptr - 16);
1067 }
1068
1069
1070 static void mxf_write_multi_descriptor(AVFormatContext *s)
1071 {
1072     MXFContext *mxf = s->priv_data;
1073     AVIOContext *pb = s->pb;
1074     const uint8_t *ul;
1075     int i;
1076
1077     mxf_write_metadata_key(pb, 0x014400);
1078     PRINT_KEY(s, "multiple descriptor key", pb->buf_ptr - 16);
1079     klv_encode_ber_length(pb, 64 + 16LL * s->nb_streams);
1080
1081     mxf_write_local_tag(pb, 16, 0x3C0A);
1082     mxf_write_uuid(pb, MultipleDescriptor, 0);
1083     PRINT_KEY(s, "multi_desc uid", pb->buf_ptr - 16);
1084
1085     // write sample rate
1086     mxf_write_local_tag(pb, 8, 0x3001);
1087     avio_wb32(pb, mxf->time_base.den);
1088     avio_wb32(pb, mxf->time_base.num);
1089
1090     // write essence container ul
1091     mxf_write_local_tag(pb, 16, 0x3004);
1092     if (mxf->essence_container_count > 1)
1093         ul = multiple_desc_ul;
1094     else {
1095         MXFStreamContext *sc = s->streams[0]->priv_data;
1096         ul = mxf_essence_container_uls[sc->index].container_ul;
1097     }
1098     avio_write(pb, ul, 16);
1099
1100     // write sub descriptor refs
1101     mxf_write_local_tag(pb, s->nb_streams * 16 + 8, 0x3F01);
1102     mxf_write_refs_count(pb, s->nb_streams);
1103     for (i = 0; i < s->nb_streams; i++)
1104         mxf_write_uuid(pb, SubDescriptor, i);
1105 }
1106
1107 static int64_t mxf_write_generic_desc(AVFormatContext *s, AVStream *st, const UID key)
1108 {
1109     MXFContext *mxf = s->priv_data;
1110     MXFStreamContext *sc = st->priv_data;
1111     AVIOContext *pb = s->pb;
1112     int64_t pos;
1113
1114     avio_write(pb, key, 16);
1115     klv_encode_ber4_length(pb, 0);
1116     pos = avio_tell(pb);
1117
1118     mxf_write_local_tag(pb, 16, 0x3C0A);
1119     mxf_write_uuid(pb, SubDescriptor, st->index);
1120
1121     mxf_write_local_tag(pb, 4, 0x3006);
1122     avio_wb32(pb, st->index+2);
1123
1124     mxf_write_local_tag(pb, 8, 0x3001);
1125     if (s->oformat == &ff_mxf_d10_muxer) {
1126         avio_wb32(pb, mxf->time_base.den);
1127         avio_wb32(pb, mxf->time_base.num);
1128     } else {
1129         if (st->codecpar->codec_id == AV_CODEC_ID_PCM_S16LE ||
1130             st->codecpar->codec_id == AV_CODEC_ID_PCM_S24LE) {
1131             avio_wb32(pb, st->codecpar->sample_rate);
1132             avio_wb32(pb, 1);
1133         } else {
1134             avio_wb32(pb, mxf->time_base.den);
1135             avio_wb32(pb, mxf->time_base.num);
1136         }
1137     }
1138
1139     mxf_write_local_tag(pb, 16, 0x3004);
1140     avio_write(pb, mxf_essence_container_uls[sc->index].container_ul, 16);
1141
1142     return pos;
1143 }
1144
1145 static const UID mxf_mpegvideo_descriptor_key = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x51,0x00 };
1146 static const UID mxf_wav_descriptor_key       = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x48,0x00 };
1147 static const UID mxf_aes3_descriptor_key      = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x47,0x00 };
1148 static const UID mxf_cdci_descriptor_key      = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0D,0x01,0x01,0x01,0x01,0x01,0x28,0x00 };
1149 static const UID mxf_generic_sound_descriptor_key = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0D,0x01,0x01,0x01,0x01,0x01,0x42,0x00 };
1150
1151 static int get_trc(UID ul, enum AVColorTransferCharacteristic trc)
1152 {
1153     switch (trc){
1154     case AVCOL_TRC_GAMMA28   :
1155     case AVCOL_TRC_GAMMA22   :
1156         memcpy(ul, ((UID){0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x01,0x01,0x01,0x01,0x00,0x00}), 16);
1157         return 0;
1158     case AVCOL_TRC_BT709     :
1159     case AVCOL_TRC_SMPTE170M :
1160         memcpy(ul, ((UID){0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x01,0x01,0x01,0x02,0x00,0x00}), 16);
1161         return 0;
1162     case AVCOL_TRC_SMPTE240M :
1163         memcpy(ul, ((UID){0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x01,0x01,0x01,0x03,0x00,0x00}), 16);
1164         return 0;
1165     case AVCOL_TRC_BT1361_ECG:
1166         memcpy(ul, ((UID){0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x06,0x04,0x01,0x01,0x01,0x01,0x05,0x00,0x00}), 16);
1167         return 0;
1168     case AVCOL_TRC_LINEAR    :
1169         memcpy(ul, ((UID){0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x06,0x04,0x01,0x01,0x01,0x01,0x06,0x00,0x00}), 16);
1170         return 0;
1171     case AVCOL_TRC_SMPTE428  :
1172         memcpy(ul, ((UID){0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x08,0x04,0x01,0x01,0x01,0x01,0x07,0x00,0x00}), 16);
1173         return 0;
1174     default:
1175         return -1;
1176     }
1177 }
1178
1179 static int64_t mxf_write_cdci_common(AVFormatContext *s, AVStream *st, const UID key)
1180 {
1181     MXFStreamContext *sc = st->priv_data;
1182     AVIOContext *pb = s->pb;
1183     int stored_width  = (st->codecpar->width +15)/16*16;
1184     int stored_height = (st->codecpar->height+15)/16*16;
1185     int display_height;
1186     int f1, f2;
1187     UID transfer_ul = {0};
1188     int64_t pos = mxf_write_generic_desc(s, st, key);
1189
1190     get_trc(transfer_ul, st->codecpar->color_trc);
1191
1192     mxf_write_local_tag(pb, 4, 0x3203);
1193     avio_wb32(pb, stored_width);
1194
1195     mxf_write_local_tag(pb, 4, 0x3202);
1196     avio_wb32(pb, stored_height>>sc->interlaced);
1197
1198     if (s->oformat == &ff_mxf_d10_muxer) {
1199         //Stored F2 Offset
1200         mxf_write_local_tag(pb, 4, 0x3216);
1201         avio_wb32(pb, 0);
1202
1203         //Image Start Offset
1204         mxf_write_local_tag(pb, 4, 0x3213);
1205         avio_wb32(pb, 0);
1206
1207         //Image End Offset
1208         mxf_write_local_tag(pb, 4, 0x3214);
1209         avio_wb32(pb, 0);
1210     }
1211
1212     //Sampled width
1213     mxf_write_local_tag(pb, 4, 0x3205);
1214     avio_wb32(pb, st->codecpar->width);
1215
1216     //Samples height
1217     mxf_write_local_tag(pb, 4, 0x3204);
1218     avio_wb32(pb, st->codecpar->height>>sc->interlaced);
1219
1220     //Sampled X Offset
1221     mxf_write_local_tag(pb, 4, 0x3206);
1222     avio_wb32(pb, 0);
1223
1224     //Sampled Y Offset
1225     mxf_write_local_tag(pb, 4, 0x3207);
1226     avio_wb32(pb, 0);
1227
1228     mxf_write_local_tag(pb, 4, 0x3209);
1229     avio_wb32(pb, st->codecpar->width);
1230
1231     if (st->codecpar->height == 608) // PAL + VBI
1232         display_height = 576;
1233     else if (st->codecpar->height == 512)  // NTSC + VBI
1234         display_height = 486;
1235     else
1236         display_height = st->codecpar->height;
1237
1238     mxf_write_local_tag(pb, 4, 0x3208);
1239     avio_wb32(pb, display_height>>sc->interlaced);
1240
1241     // display X offset
1242     mxf_write_local_tag(pb, 4, 0x320A);
1243     avio_wb32(pb, 0);
1244
1245     // display Y offset
1246     mxf_write_local_tag(pb, 4, 0x320B);
1247     avio_wb32(pb, (st->codecpar->height - display_height)>>sc->interlaced);
1248
1249     if (sc->interlaced) {
1250         //Display F2 Offset
1251         mxf_write_local_tag(pb, 4, 0x3217);
1252         avio_wb32(pb, -((st->codecpar->height - display_height)&1));
1253     }
1254
1255     // component depth
1256     mxf_write_local_tag(pb, 4, 0x3301);
1257     avio_wb32(pb, sc->component_depth);
1258
1259     // horizontal subsampling
1260     mxf_write_local_tag(pb, 4, 0x3302);
1261     avio_wb32(pb, sc->h_chroma_sub_sample);
1262
1263     // vertical subsampling
1264     if (sc->v_chroma_sub_sample) {
1265         mxf_write_local_tag(pb, 4, 0x3308);
1266         avio_wb32(pb, sc->v_chroma_sub_sample);
1267     }
1268
1269     // color siting
1270     mxf_write_local_tag(pb, 1, 0x3303);
1271     avio_w8(pb, sc->color_siting);
1272
1273     // Padding Bits
1274     mxf_write_local_tag(pb, 2, 0x3307);
1275     avio_wb16(pb, 0);
1276
1277     if (st->codecpar->color_range != AVCOL_RANGE_UNSPECIFIED) {
1278         int black = 0,
1279             white = (1<<sc->component_depth) - 1,
1280             color = (1<<sc->component_depth) - 1;
1281         if (st->codecpar->color_range == AVCOL_RANGE_MPEG) {
1282             black = 1   << (sc->component_depth - 4);
1283             white = 235 << (sc->component_depth - 8);
1284             color = (14 << (sc->component_depth - 4)) + 1;
1285         }
1286         mxf_write_local_tag(pb, 4, 0x3304);
1287         avio_wb32(pb, black);
1288         mxf_write_local_tag(pb, 4, 0x3305);
1289         avio_wb32(pb, white);
1290         mxf_write_local_tag(pb, 4, 0x3306);
1291         avio_wb32(pb, color);
1292     }
1293
1294     if (sc->signal_standard) {
1295         mxf_write_local_tag(pb, 1, 0x3215);
1296         avio_w8(pb, sc->signal_standard);
1297     }
1298
1299     // frame layout
1300     mxf_write_local_tag(pb, 1, 0x320C);
1301     avio_w8(pb, sc->interlaced);
1302
1303     // video line map
1304     switch (st->codecpar->height) {
1305     case  576: f1 = 23; f2 = st->codecpar->codec_id == AV_CODEC_ID_DVVIDEO ? 335 : 336; break;
1306     case  608: f1 =  7; f2 = 320; break;
1307     case  480: f1 = 20; f2 = st->codecpar->codec_id == AV_CODEC_ID_DVVIDEO ? 285 : 283; break;
1308     case  512: f1 =  7; f2 = 270; break;
1309     case  720: f1 = 26; f2 =   0; break; // progressive
1310     case 1080: f1 = 21; f2 = 584; break;
1311     default:   f1 =  0; f2 =   0; break;
1312     }
1313
1314     if (!sc->interlaced) {
1315         f2  = 0;
1316         f1 *= 2;
1317     }
1318
1319
1320     mxf_write_local_tag(pb, 16, 0x320D);
1321     avio_wb32(pb, 2);
1322     avio_wb32(pb, 4);
1323     avio_wb32(pb, f1);
1324     avio_wb32(pb, f2);
1325
1326     mxf_write_local_tag(pb, 8, 0x320E);
1327     avio_wb32(pb, sc->aspect_ratio.num);
1328     avio_wb32(pb, sc->aspect_ratio.den);
1329
1330     //Transfer characteristic
1331     if (transfer_ul[0]) {
1332         mxf_write_local_tag(pb, 16, 0x3210);
1333         avio_write(pb, transfer_ul, 16);
1334     };
1335
1336     mxf_write_local_tag(pb, 16, 0x3201);
1337     avio_write(pb, *sc->codec_ul, 16);
1338
1339     if (sc->interlaced && sc->field_dominance) {
1340         mxf_write_local_tag(pb, 1, 0x3212);
1341         avio_w8(pb, sc->field_dominance);
1342     }
1343
1344     return pos;
1345 }
1346
1347 static void mxf_update_klv_size(AVIOContext *pb, int64_t pos)
1348 {
1349     int64_t cur_pos = avio_tell(pb);
1350     int size = cur_pos - pos;
1351     avio_seek(pb, pos - 4, SEEK_SET);
1352     klv_encode_ber4_length(pb, size);
1353     avio_seek(pb, cur_pos, SEEK_SET);
1354 }
1355
1356 static void mxf_write_cdci_desc(AVFormatContext *s, AVStream *st)
1357 {
1358     int64_t pos = mxf_write_cdci_common(s, st, mxf_cdci_descriptor_key);
1359     mxf_update_klv_size(s->pb, pos);
1360 }
1361
1362 static void mxf_write_mpegvideo_desc(AVFormatContext *s, AVStream *st)
1363 {
1364     AVIOContext *pb = s->pb;
1365     MXFStreamContext *sc = st->priv_data;
1366     int profile_and_level = (st->codecpar->profile<<4) | st->codecpar->level;
1367     int64_t pos = mxf_write_cdci_common(s, st, mxf_mpegvideo_descriptor_key);
1368
1369     if (st->codecpar->codec_id != AV_CODEC_ID_H264) {
1370         // bit rate
1371         mxf_write_local_tag(pb, 4, 0x8000);
1372         avio_wb32(pb, sc->video_bit_rate);
1373
1374         // profile and level
1375         mxf_write_local_tag(pb, 1, 0x8007);
1376         if (!st->codecpar->profile)
1377             profile_and_level |= 0x80; // escape bit
1378         avio_w8(pb, profile_and_level);
1379     }
1380
1381     mxf_update_klv_size(pb, pos);
1382 }
1383
1384 static int64_t mxf_write_generic_sound_common(AVFormatContext *s, AVStream *st, const UID key)
1385 {
1386     AVIOContext *pb = s->pb;
1387     MXFContext *mxf = s->priv_data;
1388     int show_warnings = !mxf->footer_partition_offset;
1389     int64_t pos = mxf_write_generic_desc(s, st, key);
1390
1391     if (s->oformat == &ff_mxf_opatom_muxer) {
1392         mxf_write_local_tag(pb, 8, 0x3002);
1393         avio_wb64(pb, mxf->body_offset / mxf->edit_unit_byte_count);
1394     }
1395
1396     // audio locked
1397     mxf_write_local_tag(pb, 1, 0x3D02);
1398     avio_w8(pb, 1);
1399
1400     // write audio sampling rate
1401     mxf_write_local_tag(pb, 8, 0x3D03);
1402     avio_wb32(pb, st->codecpar->sample_rate);
1403     avio_wb32(pb, 1);
1404
1405     if (s->oformat == &ff_mxf_d10_muxer) {
1406         mxf_write_local_tag(pb, 1, 0x3D04);
1407         avio_w8(pb, 0);
1408     }
1409
1410     mxf_write_local_tag(pb, 4, 0x3D07);
1411     if (mxf->channel_count == -1) {
1412         if (show_warnings && (s->oformat == &ff_mxf_d10_muxer) && (st->codecpar->channels != 4) && (st->codecpar->channels != 8))
1413             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");
1414         avio_wb32(pb, st->codecpar->channels);
1415     } else if (s->oformat == &ff_mxf_d10_muxer) {
1416         if (show_warnings && (mxf->channel_count < st->codecpar->channels))
1417             av_log(s, AV_LOG_WARNING, "d10_channelcount < actual number of audio channels : some channels will be discarded\n");
1418         if (show_warnings && (mxf->channel_count != 4) && (mxf->channel_count != 8))
1419             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");
1420         avio_wb32(pb, mxf->channel_count);
1421     } else {
1422         avio_wb32(pb, st->codecpar->channels);
1423     }
1424
1425     mxf_write_local_tag(pb, 4, 0x3D01);
1426     avio_wb32(pb, av_get_bits_per_sample(st->codecpar->codec_id));
1427
1428     return pos;
1429 }
1430
1431 static int64_t mxf_write_wav_common(AVFormatContext *s, AVStream *st, const UID key)
1432 {
1433     AVIOContext *pb = s->pb;
1434     int64_t pos = mxf_write_generic_sound_common(s, st, key);
1435
1436     mxf_write_local_tag(pb, 2, 0x3D0A);
1437     avio_wb16(pb, st->codecpar->block_align);
1438
1439     // avg bytes per sec
1440     mxf_write_local_tag(pb, 4, 0x3D09);
1441     avio_wb32(pb, st->codecpar->block_align*st->codecpar->sample_rate);
1442
1443     return pos;
1444 }
1445
1446 static void mxf_write_wav_desc(AVFormatContext *s, AVStream *st)
1447 {
1448     int64_t pos = mxf_write_wav_common(s, st, mxf_wav_descriptor_key);
1449     mxf_update_klv_size(s->pb, pos);
1450 }
1451
1452 static void mxf_write_aes3_desc(AVFormatContext *s, AVStream *st)
1453 {
1454     int64_t pos = mxf_write_wav_common(s, st, mxf_aes3_descriptor_key);
1455     mxf_update_klv_size(s->pb, pos);
1456 }
1457
1458 static void mxf_write_generic_sound_desc(AVFormatContext *s, AVStream *st)
1459 {
1460     int64_t pos = mxf_write_generic_sound_common(s, st, mxf_generic_sound_descriptor_key);
1461     mxf_update_klv_size(s->pb, pos);
1462 }
1463
1464 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 };
1465
1466 static int mxf_write_tagged_value(AVFormatContext *s, const char* name, const char* value)
1467 {
1468     MXFContext *mxf = s->priv_data;
1469     AVIOContext *pb = s->pb;
1470     int name_size = mxf_utf16_local_tag_length(name);
1471     int indirect_value_size = 13 + mxf_utf16_local_tag_length(value);
1472
1473     if (!name_size || indirect_value_size == 13)
1474         return 1;
1475
1476     mxf_write_metadata_key(pb, 0x013f00);
1477     klv_encode_ber_length(pb, 24 + name_size + indirect_value_size);
1478
1479     // write instance UID
1480     mxf_write_local_tag(pb, 16, 0x3C0A);
1481     mxf_write_uuid(pb, TaggedValue, mxf->tagged_value_count);
1482
1483     // write name
1484     mxf_write_local_tag_utf16(pb, 0x5001, name); // Name
1485
1486     // write indirect value
1487     mxf_write_local_tag(pb, indirect_value_size, 0x5003);
1488     avio_write(pb, mxf_indirect_value_utf16le, 17);
1489     avio_put_str16le(pb, value);
1490
1491     mxf->tagged_value_count++;
1492     return 0;
1493 }
1494
1495 static int mxf_write_user_comments(AVFormatContext *s, const AVDictionary *m)
1496 {
1497     MXFContext *mxf = s->priv_data;
1498     AVDictionaryEntry *t = NULL;
1499     int count = 0;
1500
1501     while ((t = av_dict_get(m, "comment_", t, AV_DICT_IGNORE_SUFFIX))) {
1502         if (mxf->tagged_value_count >= UINT16_MAX) {
1503             av_log(s, AV_LOG_ERROR, "too many tagged values, ignoring remaining\n");
1504             return count;
1505         }
1506
1507         if (mxf_write_tagged_value(s, t->key + 8, t->value) == 0)
1508             count++;
1509     }
1510     return count;
1511 }
1512
1513 static void mxf_write_package(AVFormatContext *s, MXFPackage *package)
1514 {
1515     MXFContext *mxf = s->priv_data;
1516     AVIOContext *pb = s->pb;
1517     int i, track_count = s->nb_streams+1;
1518     int name_size = mxf_utf16_local_tag_length(package->name);
1519     int user_comment_count = 0;
1520
1521     if (package->type == MaterialPackage) {
1522         if (mxf->store_user_comments)
1523             user_comment_count = mxf_write_user_comments(s, s->metadata);
1524         mxf_write_metadata_key(pb, 0x013600);
1525         PRINT_KEY(s, "Material Package key", pb->buf_ptr - 16);
1526         klv_encode_ber_length(pb, 92 + name_size + (16*track_count) + (16*user_comment_count) + 12LL*mxf->store_user_comments);
1527     } else {
1528         mxf_write_metadata_key(pb, 0x013700);
1529         PRINT_KEY(s, "Source Package key", pb->buf_ptr - 16);
1530         klv_encode_ber_length(pb, 112 + name_size + (16*track_count) + 12LL*mxf->store_user_comments); // 20 bytes length for descriptor reference
1531     }
1532
1533     // write uid
1534     mxf_write_local_tag(pb, 16, 0x3C0A);
1535     mxf_write_uuid(pb, package->type, package->instance);
1536     av_log(s, AV_LOG_DEBUG, "package type:%d\n", package->type);
1537     PRINT_KEY(s, "package uid", pb->buf_ptr - 16);
1538
1539     // write package umid
1540     mxf_write_local_tag(pb, 32, 0x4401);
1541     mxf_write_umid(s, package->instance);
1542     PRINT_KEY(s, "package umid second part", pb->buf_ptr - 16);
1543
1544     // package name
1545     if (name_size)
1546         mxf_write_local_tag_utf16(pb, 0x4402, package->name);
1547
1548     // package creation date
1549     mxf_write_local_tag(pb, 8, 0x4405);
1550     avio_wb64(pb, mxf->timestamp);
1551
1552     // package modified date
1553     mxf_write_local_tag(pb, 8, 0x4404);
1554     avio_wb64(pb, mxf->timestamp);
1555
1556     // write track refs
1557     mxf_write_local_tag(pb, track_count*16 + 8, 0x4403);
1558     mxf_write_refs_count(pb, track_count);
1559     // these are the uuids of the tracks the will be written in mxf_write_track
1560     for (i = 0; i < track_count; i++)
1561         mxf_write_uuid(pb, Track,  mxf->track_instance_count + i);
1562
1563     // write user comment refs
1564     if (mxf->store_user_comments) {
1565         mxf_write_local_tag(pb, user_comment_count*16 + 8, 0x4406);
1566         mxf_write_refs_count(pb, user_comment_count);
1567         for (i = 0; i < user_comment_count; i++)
1568             mxf_write_uuid(pb, TaggedValue, mxf->tagged_value_count - user_comment_count + i);
1569     }
1570
1571     // write multiple descriptor reference
1572     if (package->type == SourcePackage && package->instance == 1) {
1573         mxf_write_local_tag(pb, 16, 0x4701);
1574         if (s->nb_streams > 1) {
1575             mxf_write_uuid(pb, MultipleDescriptor, 0);
1576             mxf_write_multi_descriptor(s);
1577         } else
1578             mxf_write_uuid(pb, SubDescriptor, 0);
1579     } else if (package->type == SourcePackage && package->instance == 2) {
1580         mxf_write_local_tag(pb, 16, 0x4701);
1581         mxf_write_uuid(pb, TapeDescriptor, 0);
1582         mxf_write_tape_descriptor(s);
1583     }
1584
1585     /*
1586      * for every 1 track in a package there is 1 sequence and 1 component.
1587      * all 3 of these elements share the same instance number for generating
1588      * there instance uuids. mxf->track_instance_count stores this value.
1589      * mxf->track_instance_count is incremented after a group of all 3 of
1590      * these elements are written.
1591      */
1592
1593     // write timecode track
1594     mxf_write_track(s, mxf->timecode_track, package);
1595     mxf_write_sequence(s, mxf->timecode_track, package);
1596     mxf_write_timecode_component(s, mxf->timecode_track, package);
1597     mxf->track_instance_count++;
1598
1599     for (i = 0; i < s->nb_streams; i++) {
1600         AVStream *st = s->streams[i];
1601         mxf_write_track(s, st, package);
1602         mxf_write_sequence(s, st, package);
1603         mxf_write_structural_component(s, st, package);
1604         mxf->track_instance_count++;
1605
1606         if (package->type == SourcePackage && package->instance == 1) {
1607             MXFStreamContext *sc = st->priv_data;
1608             mxf_essence_container_uls[sc->index].write_desc(s, st);
1609         }
1610     }
1611 }
1612
1613 static int mxf_write_essence_container_data(AVFormatContext *s)
1614 {
1615     AVIOContext *pb = s->pb;
1616
1617     mxf_write_metadata_key(pb, 0x012300);
1618     klv_encode_ber_length(pb, 72);
1619
1620     mxf_write_local_tag(pb, 16, 0x3C0A); // Instance UID
1621     mxf_write_uuid(pb, EssenceContainerData, 0);
1622
1623     mxf_write_local_tag(pb, 32, 0x2701); // Linked Package UID
1624     mxf_write_umid(s, 1);
1625
1626     mxf_write_local_tag(pb, 4, 0x3F07); // BodySID
1627     avio_wb32(pb, 1);
1628
1629     mxf_write_local_tag(pb, 4, 0x3F06); // IndexSID
1630     avio_wb32(pb, 2);
1631
1632     return 0;
1633 }
1634
1635 static int mxf_write_header_metadata_sets(AVFormatContext *s)
1636 {
1637     MXFContext *mxf = s->priv_data;
1638     AVDictionaryEntry *entry = NULL;
1639     AVStream *st = NULL;
1640     int i;
1641     MXFPackage packages[3] = {{0}};
1642     int package_count = 2;
1643     packages[0].type = MaterialPackage;
1644     packages[1].type = SourcePackage;
1645     packages[1].instance = 1;
1646     packages[0].ref = &packages[1];
1647
1648
1649     if (entry = av_dict_get(s->metadata, "material_package_name", NULL, 0))
1650        packages[0].name = entry->value;
1651
1652     if (entry = av_dict_get(s->metadata, "file_package_name", NULL, 0)) {
1653         packages[1].name = entry->value;
1654     } else {
1655         /* check if any of the streams contain a file_package_name */
1656         for (i = 0; i < s->nb_streams; i++) {
1657             st = s->streams[i];
1658             if (entry = av_dict_get(st->metadata, "file_package_name", NULL, 0)) {
1659                 packages[1].name = entry->value;
1660                 break;
1661             }
1662         }
1663     }
1664
1665     entry = av_dict_get(s->metadata, "reel_name", NULL, 0);
1666     if (entry) {
1667         packages[2].name = entry->value;
1668         packages[2].type = SourcePackage;
1669         packages[2].instance = 2;
1670         packages[1].ref = &packages[2];
1671         package_count = 3;
1672     }
1673
1674     mxf_write_preface(s);
1675     mxf_write_identification(s);
1676     mxf_write_content_storage(s, packages, package_count);
1677     mxf->track_instance_count = 0;
1678     for (i = 0; i < package_count; i++)
1679         mxf_write_package(s, &packages[i]);
1680     mxf_write_essence_container_data(s);
1681     return 0;
1682 }
1683
1684 static unsigned klv_fill_size(uint64_t size)
1685 {
1686     unsigned pad = KAG_SIZE - (size & (KAG_SIZE-1));
1687     if (pad < 20) // smallest fill item possible
1688         return pad + KAG_SIZE;
1689     else
1690         return pad & (KAG_SIZE-1);
1691 }
1692
1693 static void mxf_write_index_table_segment(AVFormatContext *s)
1694 {
1695     MXFContext *mxf = s->priv_data;
1696     AVIOContext *pb = s->pb;
1697     int i, j, temporal_reordering = 0;
1698     int key_index = mxf->last_key_index;
1699
1700     av_log(s, AV_LOG_DEBUG, "edit units count %d\n", mxf->edit_units_count);
1701
1702     if (!mxf->edit_units_count && !mxf->edit_unit_byte_count)
1703         return;
1704
1705     avio_write(pb, index_table_segment_key, 16);
1706
1707     if (mxf->edit_unit_byte_count) {
1708         klv_encode_ber_length(pb, 80);
1709     } else {
1710         klv_encode_ber_length(pb, 85 + 12+(s->nb_streams+1LL)*6 +
1711                               12+mxf->edit_units_count*(11+mxf->slice_count*4LL));
1712     }
1713
1714     // instance id
1715     mxf_write_local_tag(pb, 16, 0x3C0A);
1716     mxf_write_uuid(pb, IndexTableSegment, 0);
1717
1718     // index edit rate
1719     mxf_write_local_tag(pb, 8, 0x3F0B);
1720     avio_wb32(pb, mxf->time_base.den);
1721     avio_wb32(pb, mxf->time_base.num);
1722
1723     // index start position
1724     mxf_write_local_tag(pb, 8, 0x3F0C);
1725     avio_wb64(pb, mxf->last_indexed_edit_unit);
1726
1727     // index duration
1728     mxf_write_local_tag(pb, 8, 0x3F0D);
1729     if (mxf->edit_unit_byte_count)
1730         avio_wb64(pb, 0); // index table covers whole container
1731     else
1732         avio_wb64(pb, mxf->edit_units_count);
1733
1734     // edit unit byte count
1735     mxf_write_local_tag(pb, 4, 0x3F05);
1736     avio_wb32(pb, mxf->edit_unit_byte_count);
1737
1738     // index sid
1739     mxf_write_local_tag(pb, 4, 0x3F06);
1740     avio_wb32(pb, 2);
1741
1742     // body sid
1743     mxf_write_local_tag(pb, 4, 0x3F07);
1744     avio_wb32(pb, 1);
1745
1746     if (!mxf->edit_unit_byte_count) {
1747         // real slice count - 1
1748         mxf_write_local_tag(pb, 1, 0x3F08);
1749         avio_w8(pb, mxf->slice_count);
1750
1751         // delta entry array
1752         mxf_write_local_tag(pb, 8 + (s->nb_streams+1)*6, 0x3F09);
1753         avio_wb32(pb, s->nb_streams+1); // num of entries
1754         avio_wb32(pb, 6);               // size of one entry
1755         // write system item delta entry
1756         avio_w8(pb, 0);
1757         avio_w8(pb, 0); // slice entry
1758         avio_wb32(pb, 0); // element delta
1759         for (i = 0; i < s->nb_streams; i++) {
1760             AVStream *st = s->streams[i];
1761             MXFStreamContext *sc = st->priv_data;
1762             avio_w8(pb, sc->temporal_reordering);
1763             if (sc->temporal_reordering)
1764                 temporal_reordering = 1;
1765             if (i == 0) { // video track
1766                 avio_w8(pb, 0); // slice number
1767                 avio_wb32(pb, KAG_SIZE); // system item size including klv fill
1768             } else { // audio track
1769                 unsigned audio_frame_size = sc->aic.samples[0]*sc->aic.sample_size;
1770                 audio_frame_size += klv_fill_size(audio_frame_size);
1771                 avio_w8(pb, 1);
1772                 avio_wb32(pb, (i-1)*audio_frame_size); // element delta
1773             }
1774         }
1775
1776         mxf_write_local_tag(pb, 8 + mxf->edit_units_count*(11+mxf->slice_count*4), 0x3F0A);
1777         avio_wb32(pb, mxf->edit_units_count);  // num of entries
1778         avio_wb32(pb, 11+mxf->slice_count*4);  // size of one entry
1779
1780         for (i = 0; i < mxf->edit_units_count; i++) {
1781             int temporal_offset = 0;
1782
1783             if (!(mxf->index_entries[i].flags & 0x33)) { // I-frame
1784                 mxf->last_key_index = key_index;
1785                 key_index = i;
1786             }
1787
1788             if (temporal_reordering) {
1789                 int pic_num_in_gop = i - key_index;
1790                 if (pic_num_in_gop != mxf->index_entries[i].temporal_ref) {
1791                     for (j = key_index; j < mxf->edit_units_count; j++) {
1792                         if (pic_num_in_gop == mxf->index_entries[j].temporal_ref)
1793                             break;
1794                     }
1795                     if (j == mxf->edit_units_count)
1796                         av_log(s, AV_LOG_WARNING, "missing frames\n");
1797                     temporal_offset = j - key_index - pic_num_in_gop;
1798                 }
1799             }
1800             avio_w8(pb, temporal_offset);
1801
1802             if ((mxf->index_entries[i].flags & 0x30) == 0x30) { // back and forward prediction
1803                 avio_w8(pb, mxf->last_key_index - i);
1804             } else {
1805                 avio_w8(pb, key_index - i); // key frame offset
1806                 if ((mxf->index_entries[i].flags & 0x20) == 0x20) // only forward
1807                     mxf->last_key_index = key_index;
1808             }
1809
1810             if (!(mxf->index_entries[i].flags & 0x33) && // I-frame
1811                 mxf->index_entries[i].flags & 0x40 && !temporal_offset)
1812                 mxf->index_entries[i].flags |= 0x80; // random access
1813             avio_w8(pb, mxf->index_entries[i].flags);
1814             // stream offset
1815             avio_wb64(pb, mxf->index_entries[i].offset);
1816             if (s->nb_streams > 1)
1817                 avio_wb32(pb, mxf->index_entries[i].slice_offset);
1818         }
1819
1820         mxf->last_key_index = key_index - mxf->edit_units_count;
1821         mxf->last_indexed_edit_unit += mxf->edit_units_count;
1822         mxf->edit_units_count = 0;
1823     }
1824 }
1825
1826 static void mxf_write_klv_fill(AVFormatContext *s)
1827 {
1828     unsigned pad = klv_fill_size(avio_tell(s->pb));
1829     if (pad) {
1830         avio_write(s->pb, klv_fill_key, 16);
1831         pad -= 16 + 4;
1832         klv_encode_ber4_length(s->pb, pad);
1833         ffio_fill(s->pb, 0, pad);
1834         av_assert1(!(avio_tell(s->pb) & (KAG_SIZE-1)));
1835     }
1836 }
1837
1838 static int mxf_write_partition(AVFormatContext *s, int bodysid,
1839                                 int indexsid,
1840                                 const uint8_t *key, int write_metadata)
1841 {
1842     MXFContext *mxf = s->priv_data;
1843     AVIOContext *pb = s->pb;
1844     int64_t header_byte_count_offset;
1845     unsigned index_byte_count = 0;
1846     uint64_t partition_offset = avio_tell(pb);
1847     int err;
1848
1849     if (!mxf->edit_unit_byte_count && mxf->edit_units_count)
1850         index_byte_count = 85 + 12+(s->nb_streams+1)*6 +
1851             12+mxf->edit_units_count*(11+mxf->slice_count*4);
1852     else if (mxf->edit_unit_byte_count && indexsid)
1853         index_byte_count = 80;
1854
1855     if (index_byte_count) {
1856         // add encoded ber length
1857         index_byte_count += 16 + klv_ber_length(index_byte_count);
1858         index_byte_count += klv_fill_size(index_byte_count);
1859     }
1860
1861     if (key && !memcmp(key, body_partition_key, 16)) {
1862         if ((err = av_reallocp_array(&mxf->body_partition_offset, mxf->body_partitions_count + 1,
1863                                      sizeof(*mxf->body_partition_offset))) < 0) {
1864             mxf->body_partitions_count = 0;
1865             return err;
1866         }
1867         mxf->body_partition_offset[mxf->body_partitions_count++] = partition_offset;
1868     }
1869
1870     // write klv
1871     if (key)
1872         avio_write(pb, key, 16);
1873     else
1874         avio_write(pb, body_partition_key, 16);
1875
1876     klv_encode_ber_length(pb, 88 + 16LL * DESCRIPTOR_COUNT(mxf->essence_container_count));
1877
1878     // write partition value
1879     avio_wb16(pb, 1); // majorVersion
1880     avio_wb16(pb, 3); // minorVersion
1881     avio_wb32(pb, KAG_SIZE); // KAGSize
1882
1883     avio_wb64(pb, partition_offset); // ThisPartition
1884
1885     if (key && !memcmp(key, body_partition_key, 16) && mxf->body_partitions_count > 1)
1886         avio_wb64(pb, mxf->body_partition_offset[mxf->body_partitions_count-2]); // PreviousPartition
1887     else if (key && !memcmp(key, footer_partition_key, 16) && mxf->body_partitions_count)
1888         avio_wb64(pb, mxf->body_partition_offset[mxf->body_partitions_count-1]); // PreviousPartition
1889     else
1890         avio_wb64(pb, 0);
1891
1892     avio_wb64(pb, mxf->footer_partition_offset); // footerPartition
1893
1894     // set offset
1895     header_byte_count_offset = avio_tell(pb);
1896     avio_wb64(pb, 0); // headerByteCount, update later
1897
1898     // indexTable
1899     avio_wb64(pb, index_byte_count); // indexByteCount
1900     avio_wb32(pb, index_byte_count ? indexsid : 0); // indexSID
1901
1902     // BodyOffset
1903     if (bodysid && mxf->edit_units_count && mxf->body_partitions_count && s->oformat != &ff_mxf_opatom_muxer)
1904         avio_wb64(pb, mxf->body_offset);
1905     else
1906         avio_wb64(pb, 0);
1907
1908     avio_wb32(pb, bodysid); // bodySID
1909
1910     // operational pattern
1911     if (s->oformat == &ff_mxf_opatom_muxer)
1912         avio_write(pb, opatom_ul, 16);
1913     else
1914         avio_write(pb, op1a_ul, 16);
1915
1916     // essence container
1917     mxf_write_essence_container_refs(s);
1918
1919     if (write_metadata) {
1920         // mark the start of the headermetadata and calculate metadata size
1921         int64_t pos, start;
1922         unsigned header_byte_count;
1923
1924         mxf_write_klv_fill(s);
1925         start = avio_tell(s->pb);
1926         mxf_write_primer_pack(s);
1927         mxf_write_klv_fill(s);
1928         mxf_write_header_metadata_sets(s);
1929         pos = avio_tell(s->pb);
1930         header_byte_count = pos - start + klv_fill_size(pos);
1931
1932         // update header_byte_count
1933         avio_seek(pb, header_byte_count_offset, SEEK_SET);
1934         avio_wb64(pb, header_byte_count);
1935         avio_seek(pb, pos, SEEK_SET);
1936     }
1937
1938     if(key)
1939         avio_flush(pb);
1940
1941     return 0;
1942 }
1943
1944 static int mxf_parse_dnxhd_frame(AVFormatContext *s, AVStream *st,
1945 AVPacket *pkt)
1946 {
1947     MXFContext *mxf = s->priv_data;
1948     MXFStreamContext *sc = st->priv_data;
1949     int i, cid;
1950     uint8_t* header_cid;
1951     int frame_size = 0;
1952
1953     if (mxf->header_written)
1954         return 1;
1955
1956     if (pkt->size < 43)
1957         return -1;
1958
1959     header_cid = pkt->data + 0x28;
1960     cid = header_cid[0] << 24 | header_cid[1] << 16 | header_cid[2] << 8 | header_cid[3];
1961
1962     if ((frame_size = avpriv_dnxhd_get_frame_size(cid)) < 0)
1963         return -1;
1964     if ((sc->interlaced = avpriv_dnxhd_get_interlaced(cid)) < 0)
1965         return AVERROR_INVALIDDATA;
1966
1967     switch (cid) {
1968     case 1235:
1969         sc->index = INDEX_DNXHD_1080p_10bit_HIGH;
1970         sc->component_depth = 10;
1971         break;
1972     case 1237:
1973         sc->index = INDEX_DNXHD_1080p_8bit_MEDIUM;
1974         break;
1975     case 1238:
1976         sc->index = INDEX_DNXHD_1080p_8bit_HIGH;
1977         break;
1978     case 1241:
1979         sc->index = INDEX_DNXHD_1080i_10bit_HIGH;
1980         sc->component_depth = 10;
1981         break;
1982     case 1242:
1983         sc->index = INDEX_DNXHD_1080i_8bit_MEDIUM;
1984         break;
1985     case 1243:
1986         sc->index = INDEX_DNXHD_1080i_8bit_HIGH;
1987         break;
1988     case 1250:
1989         sc->index = INDEX_DNXHD_720p_10bit;
1990         sc->component_depth = 10;
1991         break;
1992     case 1251:
1993         sc->index = INDEX_DNXHD_720p_8bit_HIGH;
1994         break;
1995     case 1252:
1996         sc->index = INDEX_DNXHD_720p_8bit_MEDIUM;
1997         break;
1998     case 1253:
1999         sc->index = INDEX_DNXHD_720p_8bit_LOW;
2000         break;
2001     default:
2002         return -1;
2003     }
2004
2005     sc->codec_ul = &mxf_essence_container_uls[sc->index].codec_ul;
2006     sc->aspect_ratio = (AVRational){ 16, 9 };
2007
2008     if (s->oformat == &ff_mxf_opatom_muxer) {
2009         mxf->edit_unit_byte_count = frame_size;
2010         return 1;
2011     }
2012
2013     mxf->edit_unit_byte_count = KAG_SIZE;
2014     for (i = 0; i < s->nb_streams; i++) {
2015         AVStream *st = s->streams[i];
2016         MXFStreamContext *sc = st->priv_data;
2017         if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
2018             mxf->edit_unit_byte_count += 16 + 4 + sc->aic.samples[0]*sc->aic.sample_size;
2019             mxf->edit_unit_byte_count += klv_fill_size(mxf->edit_unit_byte_count);
2020         } else if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
2021             mxf->edit_unit_byte_count += 16 + 4 + frame_size;
2022             mxf->edit_unit_byte_count += klv_fill_size(mxf->edit_unit_byte_count);
2023         }
2024     }
2025
2026     return 1;
2027 }
2028
2029 static int mxf_parse_dv_frame(AVFormatContext *s, AVStream *st, AVPacket *pkt)
2030 {
2031     MXFContext *mxf = s->priv_data;
2032     MXFStreamContext *sc = st->priv_data;
2033     uint8_t *vs_pack, *vsc_pack;
2034     int i, ul_index, frame_size, stype, pal;
2035     const AVDVProfile *profile;
2036
2037     if (mxf->header_written)
2038         return 1;
2039
2040     // Check for minimal frame size
2041     if (pkt->size < 120000)
2042         return -1;
2043
2044     profile = av_dv_frame_profile(NULL, pkt->data, pkt->size);
2045
2046     vs_pack  = pkt->data + 80*5 + 48;
2047     vsc_pack = pkt->data + 80*5 + 53;
2048     stype    = vs_pack[3] & 0x1f;
2049     pal      = (vs_pack[3] >> 5) & 0x1;
2050
2051     if ((vsc_pack[2] & 0x07) == 0x02) {
2052         sc->aspect_ratio = (AVRational){ 16, 9 };
2053     } else {
2054         sc->aspect_ratio = (AVRational){ 4, 3 };
2055     }
2056
2057     sc->interlaced = (vsc_pack[3] >> 4) & 0x01;
2058     // TODO: fix dv encoder to set proper FF/FS value in VSC pack
2059     // and set field dominance accordingly
2060     // av_log(s, AV_LOG_DEBUG, "DV vsc pack ff/ss = %x\n", vsc_pack[2] >> 6);
2061
2062     switch (stype) {
2063     case 0x18: // DV100 720p
2064         ul_index = INDEX_DV100_720_50 + pal;
2065         frame_size = pal ? 288000 : 240000;
2066         if (sc->interlaced) {
2067             av_log(s, AV_LOG_ERROR, "source marked as interlaced but codec profile is progressive\n");
2068             sc->interlaced = 0;
2069         }
2070         break;
2071     case 0x14: // DV100 1080i
2072         ul_index = INDEX_DV100_1080_50 + pal;
2073         frame_size = pal ? 576000 : 480000;
2074         break;
2075     case 0x04: // DV50
2076         ul_index = INDEX_DV50_525_60 + pal;
2077         frame_size = pal ? 288000 : 240000;
2078         break;
2079     default: // DV25
2080         if (profile && profile->pix_fmt == AV_PIX_FMT_YUV420P && pal) {
2081             ul_index = INDEX_DV25_525_60_IEC + pal;
2082             frame_size = pal ? 144000 : 120000;
2083             break;
2084         }
2085         ul_index = INDEX_DV25_525_60 + pal;
2086         frame_size = pal ? 144000 : 120000;
2087     }
2088
2089     sc->index = ul_index;
2090     sc->codec_ul =  &mxf_essence_container_uls[sc->index].codec_ul;
2091
2092     if(s->oformat == &ff_mxf_opatom_muxer) {
2093         mxf->edit_unit_byte_count = frame_size;
2094         return 1;
2095     }
2096
2097     mxf->edit_unit_byte_count = KAG_SIZE;
2098     for (i = 0; i < s->nb_streams; i++) {
2099         AVStream *st = s->streams[i];
2100         MXFStreamContext *sc = st->priv_data;
2101         if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
2102             mxf->edit_unit_byte_count += 16 + 4 + sc->aic.samples[0]*sc->aic.sample_size;
2103             mxf->edit_unit_byte_count += klv_fill_size(mxf->edit_unit_byte_count);
2104         } else if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
2105             mxf->edit_unit_byte_count += 16 + 4 + frame_size;
2106             mxf->edit_unit_byte_count += klv_fill_size(mxf->edit_unit_byte_count);
2107         }
2108     }
2109
2110     return 1;
2111 }
2112
2113 static const struct {
2114     UID uid;
2115     int frame_size;
2116     int profile;
2117     uint8_t interlaced;
2118     int long_gop; // 1 or 0 when there are separate UIDs for Long GOP and Intra, -1 when Intra/LGOP detection can be ignored
2119 } mxf_h264_codec_uls[] = {
2120     {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x31,0x11,0x01 },      0,  66, 0, -1 }, // AVC Baseline, Unconstrained Coding
2121     {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x31,0x20,0x01 },      0,  77, 0, -1 }, // AVC Main
2122     {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x31,0x30,0x01 },      0,  88, 0, -1 }, // AVC Extended
2123     {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x31,0x40,0x01 },      0, 100, 0, -1 }, // AVC High
2124     {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x31,0x50,0x01 },      0, 110, 0,  1 }, // AVC High 10
2125     {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x31,0x60,0x01 },      0, 122, 0,  1 }, // AVC High 422
2126     {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x31,0x70,0x01 },      0, 244, 0,  1 }, // AVC High 444
2127     {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x32,0x20,0x01 },      0, 110, 0,  0 }, // AVC High 10 Intra
2128     {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x32,0x21,0x01 }, 232960,   0, 1,  0 }, // AVC Intra 50 1080i60
2129     {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x32,0x21,0x02 }, 281088,   0, 1,  0 }, // AVC Intra 50 1080i50
2130     {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x32,0x21,0x03 }, 232960,   0, 0,  0 }, // AVC Intra 50 1080p30
2131     {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x32,0x21,0x04 }, 281088,   0, 0,  0 }, // AVC Intra 50 1080p25
2132     {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x32,0x21,0x08 }, 116736,   0, 0,  0 }, // AVC Intra 50 720p60
2133     {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x32,0x21,0x09 }, 140800,   0, 0,  0 }, // AVC Intra 50 720p50
2134     {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x32,0x30,0x01 },      0, 122, 0,  0 }, // AVC High 422 Intra
2135     {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x32,0x31,0x01 }, 472576,   0, 1,  0 }, // AVC Intra 100 1080i60
2136     {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x32,0x31,0x02 }, 568832,   0, 1,  0 }, // AVC Intra 100 1080i50
2137     {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x32,0x31,0x03 }, 472576,   0, 0,  0 }, // AVC Intra 100 1080p30
2138     {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x32,0x31,0x04 }, 568832,   0, 0,  0 }, // AVC Intra 100 1080p25
2139     {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x32,0x31,0x08 }, 236544,   0, 0,  0 }, // AVC Intra 100 720p60
2140     {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x32,0x31,0x09 }, 284672,   0, 0,  0 }, // AVC Intra 100 720p50
2141     {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0d,0x04,0x01,0x02,0x02,0x01,0x32,0x40,0x01 },      0, 244, 0,  0 }, // AVC High 444 Intra
2142     {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0d,0x04,0x01,0x02,0x02,0x01,0x32,0x50,0x01 },      0,  44, 0, -1 }, // AVC CAVLC 444
2143 };
2144
2145 static int mxf_parse_h264_frame(AVFormatContext *s, AVStream *st,
2146                                 AVPacket *pkt, MXFIndexEntry *e)
2147 {
2148     MXFContext *mxf = s->priv_data;
2149     MXFStreamContext *sc = st->priv_data;
2150     AVCodecParameters *par = st->codecpar;
2151     static const int mxf_h264_num_codec_uls = sizeof(mxf_h264_codec_uls) / sizeof(mxf_h264_codec_uls[0]);
2152     const uint8_t *buf = pkt->data;
2153     const uint8_t *buf_end = pkt->data + pkt->size;
2154     uint32_t state = -1;
2155     int long_gop = 0; // assume intra when there is no SPS header
2156     int extra_size = 512; // support AVC Intra files without SPS/PPS header
2157     int i, frame_size;
2158     uint8_t uid_found;
2159
2160     if (pkt->size > extra_size)
2161         buf_end -= pkt->size - extra_size; // no need to parse beyond SPS/PPS header
2162
2163     for (;;) {
2164         buf = avpriv_find_start_code(buf, buf_end, &state);
2165         if (buf >= buf_end)
2166             break;
2167         --buf;
2168         switch (state & 0x1f) {
2169         case H264_NAL_SPS:
2170             par->profile = buf[1];
2171             long_gop = buf[2] & 0x10 ? 0 : 1; // constraint_set3_flag signals intra
2172             e->flags |= 0x40;
2173             break;
2174         case H264_NAL_PPS:
2175             if (e->flags & 0x40) { // sequence header present
2176                 e->flags |= 0x80; // random access
2177                 extra_size = 0;
2178                 buf = buf_end;
2179             }
2180             break;
2181         default:
2182             break;
2183         }
2184     }
2185
2186     if (mxf->header_written)
2187         return 1;
2188
2189     sc->aspect_ratio = (AVRational){ 16, 9 }; // 16:9 is mandatory for broadcast HD
2190     sc->interlaced = par->field_order != AV_FIELD_PROGRESSIVE ? 1 : 0;
2191
2192     uid_found = 0;
2193     frame_size = pkt->size + extra_size;
2194     for (i = 0; i < mxf_h264_num_codec_uls; i++) {
2195         if (frame_size == mxf_h264_codec_uls[i].frame_size && sc->interlaced == mxf_h264_codec_uls[i].interlaced) {
2196             sc->codec_ul = &mxf_h264_codec_uls[i].uid;
2197             sc->component_depth = 10; // AVC Intra is always 10 Bit
2198             if (sc->interlaced)
2199                 sc->field_dominance = 1; // top field first is mandatory for AVC Intra
2200             return 1;
2201         } else if ((mxf_h264_codec_uls[i].profile == par->profile) &&
2202                    ((mxf_h264_codec_uls[i].long_gop < 0) ||
2203                    (mxf_h264_codec_uls[i].long_gop == long_gop))) {
2204             sc->codec_ul = &mxf_h264_codec_uls[i].uid;
2205             uid_found = 1;
2206         }
2207     }
2208
2209     if (!uid_found) {
2210         av_log(s, AV_LOG_ERROR, "h264 profile not supported\n");
2211         return 0;
2212     }
2213
2214     return 1;
2215 }
2216
2217 static const UID mxf_mpeg2_codec_uls[] = {
2218     { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x01,0x10,0x00 }, // MP-ML I-Frame
2219     { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x01,0x11,0x00 }, // MP-ML Long GOP
2220     { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x02,0x02,0x00 }, // 422P-ML I-Frame
2221     { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x02,0x03,0x00 }, // 422P-ML Long GOP
2222     { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x03,0x02,0x00 }, // MP-HL I-Frame
2223     { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x03,0x03,0x00 }, // MP-HL Long GOP
2224     { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x04,0x02,0x00 }, // 422P-HL I-Frame
2225     { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x04,0x03,0x00 }, // 422P-HL Long GOP
2226     { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x05,0x02,0x00 }, // MP@H-14 I-Frame
2227     { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x05,0x03,0x00 }, // MP@H-14 Long GOP
2228 };
2229
2230 static const UID *mxf_get_mpeg2_codec_ul(AVCodecParameters *par)
2231 {
2232     int long_gop = 1;
2233
2234     if (par->profile == 4) { // Main
2235         if (par->level == 8) // Main
2236             return &mxf_mpeg2_codec_uls[0+long_gop];
2237         else if (par->level == 4) // High
2238             return &mxf_mpeg2_codec_uls[4+long_gop];
2239         else if (par->level == 6) // High 14
2240             return &mxf_mpeg2_codec_uls[8+long_gop];
2241     } else if (par->profile == 0) { // 422
2242         if (par->level == 5) // Main
2243             return &mxf_mpeg2_codec_uls[2+long_gop];
2244         else if (par->level == 2) // High
2245             return &mxf_mpeg2_codec_uls[6+long_gop];
2246     }
2247     return NULL;
2248 }
2249
2250 static int mxf_parse_mpeg2_frame(AVFormatContext *s, AVStream *st,
2251                                  AVPacket *pkt, MXFIndexEntry *e)
2252 {
2253     MXFStreamContext *sc = st->priv_data;
2254     uint32_t c = -1;
2255     int i;
2256
2257     for(i = 0; i < pkt->size - 4; i++) {
2258         c = (c<<8) + pkt->data[i];
2259         if (c == 0x1b5) {
2260             if ((pkt->data[i+1] & 0xf0) == 0x10) { // seq ext
2261                 st->codecpar->profile = pkt->data[i+1] & 0x07;
2262                 st->codecpar->level   = pkt->data[i+2] >> 4;
2263             } else if (i + 5 < pkt->size && (pkt->data[i+1] & 0xf0) == 0x80) { // pict coding ext
2264                 sc->interlaced = !(pkt->data[i+5] & 0x80); // progressive frame
2265                 if (sc->interlaced)
2266                     sc->field_dominance = 1 + !(pkt->data[i+4] & 0x80); // top field first
2267                 break;
2268             }
2269         } else if (c == 0x1b8) { // gop
2270             if (pkt->data[i+4]>>6 & 0x01) { // closed
2271                 sc->closed_gop = 1;
2272                 if (e->flags & 0x40) // sequence header present
2273                     e->flags |= 0x80; // random access
2274             }
2275         } else if (c == 0x1b3) { // seq
2276             e->flags |= 0x40;
2277             switch ((pkt->data[i+4]>>4) & 0xf) {
2278             case 2:  sc->aspect_ratio = (AVRational){  4,  3}; break;
2279             case 3:  sc->aspect_ratio = (AVRational){ 16,  9}; break;
2280             case 4:  sc->aspect_ratio = (AVRational){221,100}; break;
2281             default:
2282                 av_reduce(&sc->aspect_ratio.num, &sc->aspect_ratio.den,
2283                           st->codecpar->width, st->codecpar->height, 1024*1024);
2284             }
2285         } else if (c == 0x100) { // pic
2286             int pict_type = (pkt->data[i+2]>>3) & 0x07;
2287             e->temporal_ref = (pkt->data[i+1]<<2) | (pkt->data[i+2]>>6);
2288             if (pict_type == 2) { // P-frame
2289                 e->flags |= 0x22;
2290                 sc->closed_gop = 0; // reset closed GOP, don't matter anymore
2291             } else if (pict_type == 3) { // B-frame
2292                 if (sc->closed_gop)
2293                     e->flags |= 0x13; // only backward prediction
2294                 else
2295                     e->flags |= 0x33;
2296                 sc->temporal_reordering = -1;
2297             } else if (!pict_type) {
2298                 av_log(s, AV_LOG_ERROR, "error parsing mpeg2 frame\n");
2299                 return 0;
2300             }
2301         }
2302     }
2303     if (s->oformat != &ff_mxf_d10_muxer)
2304         sc->codec_ul = mxf_get_mpeg2_codec_ul(st->codecpar);
2305     return !!sc->codec_ul;
2306 }
2307
2308 static uint64_t mxf_parse_timestamp(time_t timestamp)
2309 {
2310     struct tm tmbuf;
2311     struct tm *time = gmtime_r(&timestamp, &tmbuf);
2312     if (!time)
2313         return 0;
2314     return (uint64_t)(time->tm_year+1900) << 48 |
2315            (uint64_t)(time->tm_mon+1)     << 40 |
2316            (uint64_t) time->tm_mday       << 32 |
2317                       time->tm_hour       << 24 |
2318                       time->tm_min        << 16 |
2319                       time->tm_sec        << 8;
2320 }
2321
2322 static void mxf_gen_umid(AVFormatContext *s)
2323 {
2324     MXFContext *mxf = s->priv_data;
2325     uint32_t seed = av_get_random_seed();
2326     uint64_t umid = seed + 0x5294713400000000LL;
2327
2328     AV_WB64(mxf->umid  , umid);
2329     AV_WB64(mxf->umid+8, umid>>8);
2330
2331     mxf->instance_number = seed & 0xFFFFFF;
2332 }
2333
2334 static int mxf_init_timecode(AVFormatContext *s, AVStream *st, AVRational rate)
2335 {
2336     MXFContext *mxf = s->priv_data;
2337     AVDictionaryEntry *tcr = av_dict_get(s->metadata, "timecode", NULL, 0);
2338     if (!tcr)
2339         tcr = av_dict_get(st->metadata, "timecode", NULL, 0);
2340
2341     if (tcr)
2342         return av_timecode_init_from_string(&mxf->tc, rate, tcr->value, s);
2343     else
2344         return av_timecode_init(&mxf->tc, rate, 0, 0, s);
2345 }
2346
2347 static int mxf_write_header(AVFormatContext *s)
2348 {
2349     MXFContext *mxf = s->priv_data;
2350     int i, ret;
2351     uint8_t present[FF_ARRAY_ELEMS(mxf_essence_container_uls)] = {0};
2352     const MXFSamplesPerFrame *spf = NULL;
2353     int64_t timestamp = 0;
2354
2355     if (!s->nb_streams)
2356         return -1;
2357
2358     if (s->oformat == &ff_mxf_opatom_muxer && s->nb_streams !=1) {
2359         av_log(s, AV_LOG_ERROR, "there must be exactly one stream for mxf opatom\n");
2360         return -1;
2361     }
2362
2363     if (!av_dict_get(s->metadata, "comment_", NULL, AV_DICT_IGNORE_SUFFIX))
2364         mxf->store_user_comments = 0;
2365
2366     for (i = 0; i < s->nb_streams; i++) {
2367         AVStream *st = s->streams[i];
2368         MXFStreamContext *sc = av_mallocz(sizeof(*sc));
2369         if (!sc)
2370             return AVERROR(ENOMEM);
2371         st->priv_data = sc;
2372
2373         if (((i == 0) ^ (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)) && s->oformat != &ff_mxf_opatom_muxer) {
2374             av_log(s, AV_LOG_ERROR, "there must be exactly one video stream and it must be the first one\n");
2375             return -1;
2376         }
2377
2378         if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
2379             const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(st->codecpar->format);
2380             // TODO: should be avg_frame_rate
2381             AVRational rate, tbc = st->time_base;
2382             // Default component depth to 8
2383             sc->component_depth = 8;
2384             sc->h_chroma_sub_sample = 2;
2385             sc->color_siting = 0xFF;
2386
2387             if (pix_desc) {
2388                 sc->component_depth     = pix_desc->comp[0].depth;
2389                 sc->h_chroma_sub_sample = 1 << pix_desc->log2_chroma_w;
2390                 sc->v_chroma_sub_sample = 1 << pix_desc->log2_chroma_h;
2391             }
2392             switch (ff_choose_chroma_location(s, st)) {
2393             case AVCHROMA_LOC_TOPLEFT: sc->color_siting = 0; break;
2394             case AVCHROMA_LOC_LEFT:    sc->color_siting = 6; break;
2395             case AVCHROMA_LOC_TOP:     sc->color_siting = 1; break;
2396             case AVCHROMA_LOC_CENTER:  sc->color_siting = 3; break;
2397             }
2398
2399             mxf->timecode_base = (tbc.den + tbc.num/2) / tbc.num;
2400             spf = ff_mxf_get_samples_per_frame(s, tbc);
2401             if (!spf) {
2402                 av_log(s, AV_LOG_ERROR, "Unsupported video frame rate %d/%d\n",
2403                        tbc.den, tbc.num);
2404                 return AVERROR(EINVAL);
2405             }
2406             mxf->time_base = spf->time_base;
2407             rate = av_inv_q(mxf->time_base);
2408             avpriv_set_pts_info(st, 64, mxf->time_base.num, mxf->time_base.den);
2409             if((ret = mxf_init_timecode(s, st, rate)) < 0)
2410                 return ret;
2411
2412             sc->video_bit_rate = st->codecpar->bit_rate;
2413             if (s->oformat == &ff_mxf_d10_muxer) {
2414                 if (st->codecpar->codec_id != AV_CODEC_ID_MPEG2VIDEO) {
2415                     av_log(s, AV_LOG_ERROR, "error MXF D-10 only support MPEG-2 Video\n");
2416                     return AVERROR(EINVAL);
2417                 }
2418                 if ((sc->video_bit_rate == 50000000) && (mxf->time_base.den == 25)) {
2419                     sc->index = INDEX_D10_625_50_50_VIDEO;
2420                 } else if ((sc->video_bit_rate == 49999840 || sc->video_bit_rate == 50000000) && (mxf->time_base.den != 25)) {
2421                     sc->index = INDEX_D10_525_60_50_VIDEO;
2422                 } else if (sc->video_bit_rate == 40000000) {
2423                     if (mxf->time_base.den == 25) sc->index = INDEX_D10_625_50_40_VIDEO;
2424                     else                          sc->index = INDEX_D10_525_60_40_VIDEO;
2425                 } else if (sc->video_bit_rate == 30000000) {
2426                     if (mxf->time_base.den == 25) sc->index = INDEX_D10_625_50_30_VIDEO;
2427                     else                          sc->index = INDEX_D10_525_60_30_VIDEO;
2428                 } else {
2429                     av_log(s, AV_LOG_ERROR, "error MXF D-10 only support 30/40/50 mbit/s\n");
2430                     return -1;
2431                 }
2432
2433                 mxf->edit_unit_byte_count = KAG_SIZE; // system element
2434                 mxf->edit_unit_byte_count += 16 + 4 + (uint64_t)sc->video_bit_rate *
2435                     mxf->time_base.num / (8*mxf->time_base.den);
2436                 mxf->edit_unit_byte_count += klv_fill_size(mxf->edit_unit_byte_count);
2437                 mxf->edit_unit_byte_count += 16 + 4 + 4 + spf->samples_per_frame[0]*8*4;
2438                 mxf->edit_unit_byte_count += klv_fill_size(mxf->edit_unit_byte_count);
2439
2440                 sc->signal_standard = 1;
2441                 sc->color_siting = 0;
2442             }
2443             if (mxf->signal_standard >= 0)
2444                 sc->signal_standard = mxf->signal_standard;
2445         } else if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
2446             if (st->codecpar->sample_rate != 48000) {
2447                 av_log(s, AV_LOG_ERROR, "only 48khz is implemented\n");
2448                 return -1;
2449             }
2450             avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
2451             if (s->oformat == &ff_mxf_d10_muxer) {
2452                 if (st->index != 1) {
2453                     av_log(s, AV_LOG_ERROR, "MXF D-10 only support one audio track\n");
2454                     return -1;
2455                 }
2456                 if (st->codecpar->codec_id != AV_CODEC_ID_PCM_S16LE &&
2457                     st->codecpar->codec_id != AV_CODEC_ID_PCM_S24LE) {
2458                     av_log(s, AV_LOG_ERROR, "MXF D-10 only support 16 or 24 bits le audio\n");
2459                 }
2460                 sc->index = ((MXFStreamContext*)s->streams[0]->priv_data)->index + 1;
2461             } else if (s->oformat == &ff_mxf_opatom_muxer) {
2462                 AVRational tbc = av_inv_q(mxf->audio_edit_rate);
2463
2464                 if (st->codecpar->codec_id != AV_CODEC_ID_PCM_S16LE &&
2465                     st->codecpar->codec_id != AV_CODEC_ID_PCM_S24LE) {
2466                     av_log(s, AV_LOG_ERROR, "Only pcm_s16le and pcm_s24le audio codecs are implemented\n");
2467                     return AVERROR_PATCHWELCOME;
2468                 }
2469                 if (st->codecpar->channels != 1) {
2470                     av_log(s, AV_LOG_ERROR, "MXF OPAtom only supports single channel audio\n");
2471                     return AVERROR(EINVAL);
2472                 }
2473
2474                 spf = ff_mxf_get_samples_per_frame(s, tbc);
2475                 if (!spf) {
2476                     av_log(s, AV_LOG_ERROR, "Unsupported timecode frame rate %d/%d\n", tbc.den, tbc.num);
2477                     return AVERROR(EINVAL);
2478                 }
2479
2480                 mxf->time_base = st->time_base;
2481                 if((ret = mxf_init_timecode(s, st, av_inv_q(spf->time_base))) < 0)
2482                     return ret;
2483
2484                 mxf->timecode_base = (tbc.den + tbc.num/2) / tbc.num;
2485                 mxf->edit_unit_byte_count = (av_get_bits_per_sample(st->codecpar->codec_id) * st->codecpar->channels) >> 3;
2486                 sc->index = INDEX_WAV;
2487             } else {
2488                 mxf->slice_count = 1;
2489             }
2490         }
2491
2492         if (!sc->index) {
2493             sc->index = mxf_get_essence_container_ul_index(st->codecpar->codec_id);
2494             if (sc->index == -1) {
2495                 av_log(s, AV_LOG_ERROR, "track %d: could not find essence container ul, "
2496                        "codec not currently supported in container\n", i);
2497                 return -1;
2498             }
2499         }
2500
2501         sc->codec_ul = &mxf_essence_container_uls[sc->index].codec_ul;
2502
2503         memcpy(sc->track_essence_element_key, mxf_essence_container_uls[sc->index].element_ul, 15);
2504         sc->track_essence_element_key[15] = present[sc->index];
2505         PRINT_KEY(s, "track essence element key", sc->track_essence_element_key);
2506
2507         if (!present[sc->index])
2508             mxf->essence_container_count++;
2509         present[sc->index]++;
2510     }
2511
2512     if (s->oformat == &ff_mxf_d10_muxer || s->oformat == &ff_mxf_opatom_muxer) {
2513         mxf->essence_container_count = 1;
2514     }
2515
2516     if (!(s->flags & AVFMT_FLAG_BITEXACT))
2517         mxf_gen_umid(s);
2518
2519     for (i = 0; i < s->nb_streams; i++) {
2520         MXFStreamContext *sc = s->streams[i]->priv_data;
2521         // update element count
2522         sc->track_essence_element_key[13] = present[sc->index];
2523         if (!memcmp(sc->track_essence_element_key, mxf_essence_container_uls[15].element_ul, 13)) // DV
2524             sc->order = (0x15 << 24) | AV_RB32(sc->track_essence_element_key+13);
2525         else
2526             sc->order = AV_RB32(sc->track_essence_element_key+12);
2527     }
2528
2529     if (ff_parse_creation_time_metadata(s, &timestamp, 1) > 0)
2530         mxf->timestamp = mxf_parse_timestamp(timestamp);
2531     mxf->duration = -1;
2532
2533     mxf->timecode_track = av_mallocz(sizeof(*mxf->timecode_track));
2534     if (!mxf->timecode_track)
2535         return AVERROR(ENOMEM);
2536     mxf->timecode_track->priv_data = av_mallocz(sizeof(MXFStreamContext));
2537     if (!mxf->timecode_track->priv_data)
2538         return AVERROR(ENOMEM);
2539     mxf->timecode_track->index = -1;
2540
2541     if (!spf)
2542         spf = ff_mxf_get_samples_per_frame(s, (AVRational){ 1, 25 });
2543
2544     if (ff_audio_interleave_init(s, spf->samples_per_frame, mxf->time_base) < 0)
2545         return -1;
2546
2547     return 0;
2548 }
2549
2550 static const uint8_t system_metadata_pack_key[]        = { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0D,0x01,0x03,0x01,0x04,0x01,0x01,0x00 };
2551 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 };
2552
2553 static void mxf_write_system_item(AVFormatContext *s)
2554 {
2555     MXFContext *mxf = s->priv_data;
2556     AVIOContext *pb = s->pb;
2557     unsigned frame;
2558     uint32_t time_code;
2559
2560     frame = mxf->last_indexed_edit_unit + mxf->edit_units_count;
2561
2562     // write system metadata pack
2563     avio_write(pb, system_metadata_pack_key, 16);
2564     klv_encode_ber4_length(pb, 57);
2565     avio_w8(pb, 0x5c); // UL, user date/time stamp, picture and sound item present
2566     avio_w8(pb, 0x04); // content package rate
2567     avio_w8(pb, 0x00); // content package type
2568     avio_wb16(pb, 0x00); // channel handle
2569     avio_wb16(pb, (mxf->tc.start + frame) & 0xFFFF); // continuity count, supposed to overflow
2570     if (mxf->essence_container_count > 1)
2571         avio_write(pb, multiple_desc_ul, 16);
2572     else {
2573         MXFStreamContext *sc = s->streams[0]->priv_data;
2574         avio_write(pb, mxf_essence_container_uls[sc->index].container_ul, 16);
2575     }
2576     avio_w8(pb, 0);
2577     avio_wb64(pb, 0);
2578     avio_wb64(pb, 0); // creation date/time stamp
2579
2580     avio_w8(pb, 0x81); // SMPTE 12M time code
2581     time_code = av_timecode_get_smpte_from_framenum(&mxf->tc, frame);
2582     avio_wb32(pb, time_code);
2583     avio_wb32(pb, 0); // binary group data
2584     avio_wb64(pb, 0);
2585
2586     // write system metadata package set
2587     avio_write(pb, system_metadata_package_set_key, 16);
2588     klv_encode_ber4_length(pb, 35);
2589     avio_w8(pb, 0x83); // UMID
2590     avio_wb16(pb, 0x20);
2591     mxf_write_umid(s, 1);
2592 }
2593
2594 static void mxf_write_d10_video_packet(AVFormatContext *s, AVStream *st, AVPacket *pkt)
2595 {
2596     MXFContext *mxf = s->priv_data;
2597     AVIOContext *pb = s->pb;
2598     MXFStreamContext *sc = st->priv_data;
2599     int packet_size = (uint64_t)sc->video_bit_rate*mxf->time_base.num /
2600         (8*mxf->time_base.den); // frame size
2601     int pad;
2602
2603     packet_size += 16 + 4;
2604     packet_size += klv_fill_size(packet_size);
2605
2606     klv_encode_ber4_length(pb, pkt->size);
2607     avio_write(pb, pkt->data, pkt->size);
2608
2609     // ensure CBR muxing by padding to correct video frame size
2610     pad = packet_size - pkt->size - 16 - 4;
2611     if (pad > 20) {
2612         avio_write(s->pb, klv_fill_key, 16);
2613         pad -= 16 + 4;
2614         klv_encode_ber4_length(s->pb, pad);
2615         ffio_fill(s->pb, 0, pad);
2616         av_assert1(!(avio_tell(s->pb) & (KAG_SIZE-1)));
2617     } else {
2618         av_log(s, AV_LOG_WARNING, "cannot fill d-10 video packet\n");
2619         ffio_fill(s->pb, 0, pad);
2620     }
2621 }
2622
2623 static void mxf_write_d10_audio_packet(AVFormatContext *s, AVStream *st, AVPacket *pkt)
2624 {
2625     MXFContext *mxf = s->priv_data;
2626     AVIOContext *pb = s->pb;
2627     int frame_size = pkt->size / st->codecpar->block_align;
2628     uint8_t *samples = pkt->data;
2629     uint8_t *end = pkt->data + pkt->size;
2630     int i;
2631
2632     klv_encode_ber4_length(pb, 4 + frame_size*4*8);
2633
2634     avio_w8(pb, (frame_size == 1920 ? 0 : (mxf->edit_units_count-1) % 5 + 1));
2635     avio_wl16(pb, frame_size);
2636     avio_w8(pb, (1<<st->codecpar->channels)-1);
2637
2638     while (samples < end) {
2639         for (i = 0; i < st->codecpar->channels; i++) {
2640             uint32_t sample;
2641             if (st->codecpar->codec_id == AV_CODEC_ID_PCM_S24LE) {
2642                 sample = AV_RL24(samples)<< 4;
2643                 samples += 3;
2644             } else {
2645                 sample = AV_RL16(samples)<<12;
2646                 samples += 2;
2647             }
2648             avio_wl32(pb, sample | i);
2649         }
2650         for (; i < 8; i++)
2651             avio_wl32(pb, i);
2652     }
2653 }
2654
2655 static int mxf_write_opatom_body_partition(AVFormatContext *s)
2656 {
2657     MXFContext *mxf = s->priv_data;
2658     AVIOContext *pb = s->pb;
2659     AVStream *st = s->streams[0];
2660     MXFStreamContext *sc = st->priv_data;
2661     const uint8_t *key = NULL;
2662
2663     int err;
2664
2665     if (!mxf->header_written)
2666         key = body_partition_key;
2667
2668     if ((err = mxf_write_partition(s, 1, 0, key, 0)) < 0)
2669         return err;
2670     mxf_write_klv_fill(s);
2671     avio_write(pb, sc->track_essence_element_key, 16);
2672     klv_encode_ber9_length(pb, mxf->body_offset);
2673     return 0;
2674 }
2675
2676 static int mxf_write_opatom_packet(AVFormatContext *s, AVPacket *pkt, MXFIndexEntry *ie)
2677 {
2678     MXFContext *mxf = s->priv_data;
2679     AVIOContext *pb = s->pb;
2680
2681     int err;
2682
2683     if (!mxf->header_written) {
2684         if ((err = mxf_write_partition(s, 0, 0, header_open_partition_key, 1)) < 0)
2685             return err;
2686         mxf_write_klv_fill(s);
2687
2688         if ((err = mxf_write_opatom_body_partition(s)) < 0)
2689             return err;
2690         mxf->header_written = 1;
2691     }
2692
2693     if (!mxf->edit_unit_byte_count) {
2694         mxf->index_entries[mxf->edit_units_count].offset = mxf->body_offset;
2695         mxf->index_entries[mxf->edit_units_count].flags = ie->flags;
2696         mxf->index_entries[mxf->edit_units_count].temporal_ref = ie->temporal_ref;
2697     }
2698     mxf->edit_units_count++;
2699     avio_write(pb, pkt->data, pkt->size);
2700     mxf->body_offset += pkt->size;
2701     avio_flush(pb);
2702
2703     return 0;
2704 }
2705
2706 static int mxf_write_packet(AVFormatContext *s, AVPacket *pkt)
2707 {
2708     MXFContext *mxf = s->priv_data;
2709     AVIOContext *pb = s->pb;
2710     AVStream *st = s->streams[pkt->stream_index];
2711     MXFStreamContext *sc = st->priv_data;
2712     MXFIndexEntry ie = {0};
2713     int err;
2714
2715     if (!mxf->edit_unit_byte_count && !(mxf->edit_units_count % EDIT_UNITS_PER_BODY)) {
2716         if ((err = av_reallocp_array(&mxf->index_entries, mxf->edit_units_count
2717                                      + EDIT_UNITS_PER_BODY, sizeof(*mxf->index_entries))) < 0) {
2718             mxf->edit_units_count = 0;
2719             av_log(s, AV_LOG_ERROR, "could not allocate index entries\n");
2720             return err;
2721         }
2722     }
2723
2724     if (st->codecpar->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
2725         if (!mxf_parse_mpeg2_frame(s, st, pkt, &ie)) {
2726             av_log(s, AV_LOG_ERROR, "could not get mpeg2 profile and level\n");
2727             return -1;
2728         }
2729     } else if (st->codecpar->codec_id == AV_CODEC_ID_DNXHD) {
2730         if (!mxf_parse_dnxhd_frame(s, st, pkt)) {
2731             av_log(s, AV_LOG_ERROR, "could not get dnxhd profile\n");
2732             return -1;
2733         }
2734     } else if (st->codecpar->codec_id == AV_CODEC_ID_DVVIDEO) {
2735         if (!mxf_parse_dv_frame(s, st, pkt)) {
2736             av_log(s, AV_LOG_ERROR, "could not get dv profile\n");
2737             return -1;
2738         }
2739     } else if (st->codecpar->codec_id == AV_CODEC_ID_H264) {
2740         if (!mxf_parse_h264_frame(s, st, pkt, &ie)) {
2741             av_log(s, AV_LOG_ERROR, "could not get h264 profile\n");
2742             return -1;
2743         }
2744     }
2745
2746     if (s->oformat == &ff_mxf_opatom_muxer)
2747         return mxf_write_opatom_packet(s, pkt, &ie);
2748
2749     if (!mxf->header_written) {
2750         if (mxf->edit_unit_byte_count) {
2751             if ((err = mxf_write_partition(s, 1, 2, header_open_partition_key, 1)) < 0)
2752                 return err;
2753             mxf_write_klv_fill(s);
2754             mxf_write_index_table_segment(s);
2755         } else {
2756             if ((err = mxf_write_partition(s, 0, 0, header_open_partition_key, 1)) < 0)
2757                 return err;
2758         }
2759         mxf->header_written = 1;
2760     }
2761
2762     if (st->index == 0) {
2763         if (!mxf->edit_unit_byte_count &&
2764             (!mxf->edit_units_count || mxf->edit_units_count > EDIT_UNITS_PER_BODY) &&
2765             !(ie.flags & 0x33)) { // I-frame, GOP start
2766             mxf_write_klv_fill(s);
2767             if ((err = mxf_write_partition(s, 1, 2, body_partition_key, 0)) < 0)
2768                 return err;
2769             mxf_write_klv_fill(s);
2770             mxf_write_index_table_segment(s);
2771         }
2772
2773         mxf_write_klv_fill(s);
2774         mxf_write_system_item(s);
2775
2776         if (!mxf->edit_unit_byte_count) {
2777             mxf->index_entries[mxf->edit_units_count].offset = mxf->body_offset;
2778             mxf->index_entries[mxf->edit_units_count].flags = ie.flags;
2779             mxf->index_entries[mxf->edit_units_count].temporal_ref = ie.temporal_ref;
2780             mxf->body_offset += KAG_SIZE; // size of system element
2781         }
2782         mxf->edit_units_count++;
2783     } else if (!mxf->edit_unit_byte_count && st->index == 1) {
2784         if (!mxf->edit_units_count) {
2785             av_log(s, AV_LOG_ERROR, "No packets in first stream\n");
2786             return AVERROR_PATCHWELCOME;
2787         }
2788         mxf->index_entries[mxf->edit_units_count-1].slice_offset =
2789             mxf->body_offset - mxf->index_entries[mxf->edit_units_count-1].offset;
2790     }
2791
2792     mxf_write_klv_fill(s);
2793     avio_write(pb, sc->track_essence_element_key, 16); // write key
2794     if (s->oformat == &ff_mxf_d10_muxer) {
2795         if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
2796             mxf_write_d10_video_packet(s, st, pkt);
2797         else
2798             mxf_write_d10_audio_packet(s, st, pkt);
2799     } else {
2800         klv_encode_ber4_length(pb, pkt->size); // write length
2801         avio_write(pb, pkt->data, pkt->size);
2802         mxf->body_offset += 16+4+pkt->size + klv_fill_size(16+4+pkt->size);
2803     }
2804
2805     avio_flush(pb);
2806
2807     return 0;
2808 }
2809
2810 static void mxf_write_random_index_pack(AVFormatContext *s)
2811 {
2812     MXFContext *mxf = s->priv_data;
2813     AVIOContext *pb = s->pb;
2814     uint64_t pos = avio_tell(pb);
2815     int i;
2816
2817     avio_write(pb, random_index_pack_key, 16);
2818     klv_encode_ber_length(pb, 28 + 12LL*mxf->body_partitions_count);
2819
2820     if (mxf->edit_unit_byte_count && s->oformat != &ff_mxf_opatom_muxer)
2821         avio_wb32(pb, 1); // BodySID of header partition
2822     else
2823         avio_wb32(pb, 0);
2824     avio_wb64(pb, 0); // offset of header partition
2825
2826     for (i = 0; i < mxf->body_partitions_count; i++) {
2827         avio_wb32(pb, 1); // BodySID
2828         avio_wb64(pb, mxf->body_partition_offset[i]);
2829     }
2830
2831     avio_wb32(pb, 0); // BodySID of footer partition
2832     avio_wb64(pb, mxf->footer_partition_offset);
2833
2834     avio_wb32(pb, avio_tell(pb) - pos + 4);
2835 }
2836
2837 static int mxf_write_footer(AVFormatContext *s)
2838 {
2839     MXFContext *mxf = s->priv_data;
2840     AVIOContext *pb = s->pb;
2841     int err = 0;
2842
2843     if (!mxf->header_written ||
2844         (s->oformat == &ff_mxf_opatom_muxer && !mxf->body_partition_offset)) {
2845         /* reason could be invalid options/not supported codec/out of memory */
2846         err = AVERROR_UNKNOWN;
2847         goto end;
2848     }
2849
2850     mxf->duration = mxf->last_indexed_edit_unit + mxf->edit_units_count;
2851
2852     mxf_write_klv_fill(s);
2853     mxf->footer_partition_offset = avio_tell(pb);
2854     if (mxf->edit_unit_byte_count && s->oformat != &ff_mxf_opatom_muxer) { // no need to repeat index
2855         if ((err = mxf_write_partition(s, 0, 0, footer_partition_key, 0)) < 0)
2856             goto end;
2857     } else {
2858         if ((err = mxf_write_partition(s, 0, 2, footer_partition_key, 0)) < 0)
2859             goto end;
2860         mxf_write_klv_fill(s);
2861         mxf_write_index_table_segment(s);
2862     }
2863
2864     mxf_write_klv_fill(s);
2865     mxf_write_random_index_pack(s);
2866
2867     if (s->pb->seekable & AVIO_SEEKABLE_NORMAL) {
2868         if (s->oformat == &ff_mxf_opatom_muxer) {
2869             /* rewrite body partition to update lengths */
2870             avio_seek(pb, mxf->body_partition_offset[0], SEEK_SET);
2871             if ((err = mxf_write_opatom_body_partition(s)) < 0)
2872                 goto end;
2873         }
2874
2875         avio_seek(pb, 0, SEEK_SET);
2876         if (mxf->edit_unit_byte_count && s->oformat != &ff_mxf_opatom_muxer) {
2877             if ((err = mxf_write_partition(s, 1, 2, header_closed_partition_key, 1)) < 0)
2878                 goto end;
2879             mxf_write_klv_fill(s);
2880             mxf_write_index_table_segment(s);
2881         } else {
2882             if ((err = mxf_write_partition(s, 0, 0, header_closed_partition_key, 1)) < 0)
2883                 goto end;
2884         }
2885     }
2886
2887 end:
2888     ff_audio_interleave_close(s);
2889
2890     av_freep(&mxf->index_entries);
2891     av_freep(&mxf->body_partition_offset);
2892     av_freep(&mxf->timecode_track->priv_data);
2893     av_freep(&mxf->timecode_track);
2894
2895     mxf_free(s);
2896
2897     return err < 0 ? err : 0;
2898 }
2899
2900 static int mxf_interleave_get_packet(AVFormatContext *s, AVPacket *out, AVPacket *pkt, int flush)
2901 {
2902     int i, stream_count = 0;
2903
2904     for (i = 0; i < s->nb_streams; i++)
2905         stream_count += !!s->streams[i]->last_in_packet_buffer;
2906
2907     if (stream_count && (s->nb_streams == stream_count || flush)) {
2908         AVPacketList *pktl = s->internal->packet_buffer;
2909         if (s->nb_streams != stream_count) {
2910             AVPacketList *last = NULL;
2911             // find last packet in edit unit
2912             while (pktl) {
2913                 if (!stream_count || pktl->pkt.stream_index == 0)
2914                     break;
2915                 // update last packet in packet buffer
2916                 if (s->streams[pktl->pkt.stream_index]->last_in_packet_buffer != pktl)
2917                     s->streams[pktl->pkt.stream_index]->last_in_packet_buffer = pktl;
2918                 last = pktl;
2919                 pktl = pktl->next;
2920                 stream_count--;
2921             }
2922             // purge packet queue
2923             while (pktl) {
2924                 AVPacketList *next = pktl->next;
2925                 av_packet_unref(&pktl->pkt);
2926                 av_freep(&pktl);
2927                 pktl = next;
2928             }
2929             if (last)
2930                 last->next = NULL;
2931             else {
2932                 s->internal->packet_buffer = NULL;
2933                 s->internal->packet_buffer_end= NULL;
2934                 goto out;
2935             }
2936             pktl = s->internal->packet_buffer;
2937         }
2938
2939         *out = pktl->pkt;
2940         av_log(s, AV_LOG_TRACE, "out st:%d dts:%"PRId64"\n", (*out).stream_index, (*out).dts);
2941         s->internal->packet_buffer = pktl->next;
2942         if(s->streams[pktl->pkt.stream_index]->last_in_packet_buffer == pktl)
2943             s->streams[pktl->pkt.stream_index]->last_in_packet_buffer= NULL;
2944         if(!s->internal->packet_buffer)
2945             s->internal->packet_buffer_end= NULL;
2946         av_freep(&pktl);
2947         return 1;
2948     } else {
2949     out:
2950         av_init_packet(out);
2951         return 0;
2952     }
2953 }
2954
2955 static int mxf_compare_timestamps(AVFormatContext *s, AVPacket *next, AVPacket *pkt)
2956 {
2957     MXFStreamContext *sc  = s->streams[pkt ->stream_index]->priv_data;
2958     MXFStreamContext *sc2 = s->streams[next->stream_index]->priv_data;
2959
2960     return next->dts > pkt->dts ||
2961         (next->dts == pkt->dts && sc->order < sc2->order);
2962 }
2963
2964 static int mxf_interleave(AVFormatContext *s, AVPacket *out, AVPacket *pkt, int flush)
2965 {
2966     return ff_audio_rechunk_interleave(s, out, pkt, flush,
2967                                mxf_interleave_get_packet, mxf_compare_timestamps);
2968 }
2969
2970 #define MXF_COMMON_OPTIONS \
2971     { "signal_standard", "Force/set Sigal Standard",\
2972       offsetof(MXFContext, signal_standard), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 7, AV_OPT_FLAG_ENCODING_PARAM, "signal_standard"},\
2973     { "bt601", "ITU-R BT.601 and BT.656, also SMPTE 125M (525 and 625 line interlaced)",\
2974       0, AV_OPT_TYPE_CONST, {.i64 = 1}, -1, 7, AV_OPT_FLAG_ENCODING_PARAM, "signal_standard"},\
2975     { "bt1358", "ITU-R BT.1358 and ITU-R BT.799-3, also SMPTE 293M (525 and 625 line progressive)",\
2976       0, AV_OPT_TYPE_CONST, {.i64 = 2}, -1, 7, AV_OPT_FLAG_ENCODING_PARAM, "signal_standard"},\
2977     { "smpte347m", "SMPTE 347M (540 Mbps mappings)",\
2978       0, AV_OPT_TYPE_CONST, {.i64 = 3}, -1, 7, AV_OPT_FLAG_ENCODING_PARAM, "signal_standard"},\
2979     { "smpte274m", "SMPTE 274M (1125 line)",\
2980       0, AV_OPT_TYPE_CONST, {.i64 = 4}, -1, 7, AV_OPT_FLAG_ENCODING_PARAM, "signal_standard"},\
2981     { "smpte296m", "SMPTE 296M (750 line progressive)",\
2982       0, AV_OPT_TYPE_CONST, {.i64 = 5}, -1, 7, AV_OPT_FLAG_ENCODING_PARAM, "signal_standard"},\
2983     { "smpte349m", "SMPTE 349M (1485 Mbps mappings)",\
2984       0, AV_OPT_TYPE_CONST, {.i64 = 6}, -1, 7, AV_OPT_FLAG_ENCODING_PARAM, "signal_standard"},\
2985     { "smpte428", "SMPTE 428-1 DCDM",\
2986       0, AV_OPT_TYPE_CONST, {.i64 = 7}, -1, 7, AV_OPT_FLAG_ENCODING_PARAM, "signal_standard"},
2987
2988
2989
2990 static const AVOption mxf_options[] = {
2991     MXF_COMMON_OPTIONS
2992     { "store_user_comments", "",
2993       offsetof(MXFContext, store_user_comments), AV_OPT_TYPE_BOOL, {.i64 = 1}, 0, 1, AV_OPT_FLAG_ENCODING_PARAM},
2994     { NULL },
2995 };
2996
2997 static const AVClass mxf_muxer_class = {
2998     .class_name     = "MXF muxer",
2999     .item_name      = av_default_item_name,
3000     .option         = mxf_options,
3001     .version        = LIBAVUTIL_VERSION_INT,
3002 };
3003
3004 static const AVOption d10_options[] = {
3005     { "d10_channelcount", "Force/set channelcount in generic sound essence descriptor",
3006       offsetof(MXFContext, channel_count), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 8, AV_OPT_FLAG_ENCODING_PARAM},
3007     MXF_COMMON_OPTIONS
3008     { "store_user_comments", "",
3009       offsetof(MXFContext, store_user_comments), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, AV_OPT_FLAG_ENCODING_PARAM},
3010     { NULL },
3011 };
3012
3013 static const AVClass mxf_d10_muxer_class = {
3014     .class_name     = "MXF-D10 muxer",
3015     .item_name      = av_default_item_name,
3016     .option         = d10_options,
3017     .version        = LIBAVUTIL_VERSION_INT,
3018 };
3019
3020 static const AVOption opatom_options[] = {
3021     { "mxf_audio_edit_rate", "Audio edit rate for timecode",
3022         offsetof(MXFContext, audio_edit_rate), AV_OPT_TYPE_RATIONAL, {.dbl=25}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
3023     MXF_COMMON_OPTIONS
3024     { NULL },
3025 };
3026
3027 static const AVClass mxf_opatom_muxer_class = {
3028     .class_name     = "MXF-OPAtom muxer",
3029     .item_name      = av_default_item_name,
3030     .option         = opatom_options,
3031     .version        = LIBAVUTIL_VERSION_INT,
3032 };
3033
3034 AVOutputFormat ff_mxf_muxer = {
3035     .name              = "mxf",
3036     .long_name         = NULL_IF_CONFIG_SMALL("MXF (Material eXchange Format)"),
3037     .mime_type         = "application/mxf",
3038     .extensions        = "mxf",
3039     .priv_data_size    = sizeof(MXFContext),
3040     .audio_codec       = AV_CODEC_ID_PCM_S16LE,
3041     .video_codec       = AV_CODEC_ID_MPEG2VIDEO,
3042     .write_header      = mxf_write_header,
3043     .write_packet      = mxf_write_packet,
3044     .write_trailer     = mxf_write_footer,
3045     .flags             = AVFMT_NOTIMESTAMPS,
3046     .interleave_packet = mxf_interleave,
3047     .priv_class        = &mxf_muxer_class,
3048 };
3049
3050 AVOutputFormat ff_mxf_d10_muxer = {
3051     .name              = "mxf_d10",
3052     .long_name         = NULL_IF_CONFIG_SMALL("MXF (Material eXchange Format) D-10 Mapping"),
3053     .mime_type         = "application/mxf",
3054     .priv_data_size    = sizeof(MXFContext),
3055     .audio_codec       = AV_CODEC_ID_PCM_S16LE,
3056     .video_codec       = AV_CODEC_ID_MPEG2VIDEO,
3057     .write_header      = mxf_write_header,
3058     .write_packet      = mxf_write_packet,
3059     .write_trailer     = mxf_write_footer,
3060     .flags             = AVFMT_NOTIMESTAMPS,
3061     .interleave_packet = mxf_interleave,
3062     .priv_class        = &mxf_d10_muxer_class,
3063 };
3064
3065 AVOutputFormat ff_mxf_opatom_muxer = {
3066     .name              = "mxf_opatom",
3067     .long_name         = NULL_IF_CONFIG_SMALL("MXF (Material eXchange Format) Operational Pattern Atom"),
3068     .mime_type         = "application/mxf",
3069     .extensions        = "mxf",
3070     .priv_data_size    = sizeof(MXFContext),
3071     .audio_codec       = AV_CODEC_ID_PCM_S16LE,
3072     .video_codec       = AV_CODEC_ID_DNXHD,
3073     .write_header      = mxf_write_header,
3074     .write_packet      = mxf_write_packet,
3075     .write_trailer     = mxf_write_footer,
3076     .flags             = AVFMT_NOTIMESTAMPS,
3077     .interleave_packet = mxf_interleave,
3078     .priv_class        = &mxf_opatom_muxer_class,
3079 };