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