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>
6 * This file is part of FFmpeg.
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.
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.
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
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
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"
49 #include "avio_internal.h"
54 extern AVOutputFormat ff_mxf_d10_muxer;
55 extern AVOutputFormat ff_mxf_opatom_muxer;
57 #define EDIT_UNITS_PER_BODY 250
60 typedef struct MXFLocalTagPair {
65 typedef struct MXFIndexEntry {
68 unsigned slice_offset; ///< offset of audio slice
69 uint16_t temporal_ref;
72 typedef struct MXFStreamContext {
73 AudioInterleaveContext aic;
74 UID track_essence_element_key;
75 int index; ///< index in mxf_essence_container_uls table
77 int order; ///< interleaving order if dts are equal
78 int interlaced; ///< whether picture is interlaced
79 int field_dominance; ///< tff=1, bff=2
81 int temporal_reordering;
82 AVRational aspect_ratio; ///< display aspect ratio
83 int closed_gop; ///< gop is closed, used in mpeg-2 frame parsing
87 typedef struct MXFContainerEssenceEntry {
91 void (*write_desc)(AVFormatContext *, AVStream *);
92 } MXFContainerEssenceEntry;
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 },
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);
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 },
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 },
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 },
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 },
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 },
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 },
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 },
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 },
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 },
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 },
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 },
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 },
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 },
292 typedef struct MXFContext {
294 int64_t footer_partition_offset;
295 int essence_container_count;
296 AVRational time_base;
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
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
315 uint32_t tagged_value_count;
316 AVRational audio_edit_rate;
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 };
323 * complete key for operation pattern, partitions, and primer pack
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
337 * partial key for header metadata
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 };
343 * SMPTE RP210 http://www.smpte-ra.org/mdd/index.html
345 static const MXFLocalTagPair mxf_local_tag_batch[] = {
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 */
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 */
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 */
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 */
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 */
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 */
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 */
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 */
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 */
440 static void mxf_write_uuid(AVIOContext *pb, enum MXFMetadataSetType type, int value)
442 avio_write(pb, uuid_base, 12);
444 avio_wb16(pb, value);
447 static void mxf_write_umid(AVFormatContext *s, int type)
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);
456 static void mxf_write_refs_count(AVIOContext *pb, int ref_count)
458 avio_wb32(pb, ref_count);
462 static int klv_ber_length(uint64_t len)
467 return (av_log2(len) >> 3) + 2;
470 static int klv_encode_ber_length(AVIOContext *pb, uint64_t len)
472 // Determine the best BER size
480 size = (av_log2(len) >> 3) + 1;
483 avio_w8(pb, 0x80 + size);
486 avio_w8(pb, len >> 8 * size & 0xff);
491 static void klv_encode_ber4_length(AVIOContext *pb, int len)
493 avio_w8(pb, 0x80 + 3);
497 static void klv_encode_ber9_length(AVIOContext *pb, uint64_t len)
499 avio_w8(pb, 0x80 + 8);
504 * Get essence container ul index
506 static int mxf_get_essence_container_ul_index(enum AVCodecID id)
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;
515 static void mxf_write_primer_pack(AVFormatContext *s)
517 AVIOContext *pb = s->pb;
518 int local_tag_number, i = 0;
520 local_tag_number = FF_ARRAY_ELEMS(mxf_local_tag_batch);
522 avio_write(pb, primer_pack_key, 16);
523 klv_encode_ber_length(pb, local_tag_number * 18 + 8);
525 avio_wb32(pb, local_tag_number); // local_tag num
526 avio_wb32(pb, 18); // item size, always 18 according to the specs
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);
534 static void mxf_write_local_tag(AVIOContext *pb, int size, int tag)
540 static void mxf_write_metadata_key(AVIOContext *pb, unsigned int value)
542 avio_write(pb, header_metadata_key, 13);
543 avio_wb24(pb, value);
546 static void mxf_free(AVFormatContext *s)
550 for (i = 0; i < s->nb_streams; i++) {
551 AVStream *st = s->streams[i];
552 av_freep(&st->priv_data);
556 static const MXFCodecUL *mxf_get_data_definition_ul(int type)
558 const MXFCodecUL *uls = ff_mxf_data_definition_uls;
559 while (uls->uid[0]) {
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)
571 static void mxf_write_essence_container_refs(AVFormatContext *s)
573 MXFContext *c = s->priv_data;
574 AVIOContext *pb = s->pb;
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);
584 if (c->essence_container_count > 1)
585 avio_write(pb, multiple_desc_ul, 16);
588 static void mxf_write_preface(AVFormatContext *s)
590 MXFContext *mxf = s->priv_data;
591 AVIOContext *pb = s->pb;
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));
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);
602 // last modified date
603 mxf_write_local_tag(pb, 8, 0x3B02);
604 avio_wb64(pb, mxf->timestamp);
607 mxf_write_local_tag(pb, 2, 0x3B05);
608 avio_wb16(pb, 258); // v1.2
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);
615 // write content_storage_refs
616 mxf_write_local_tag(pb, 16, 0x3B03);
617 mxf_write_uuid(pb, ContentStorage, 0);
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);
624 avio_write(pb, op1a_ul, 16);
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);
630 // write dm_scheme_refs
631 mxf_write_local_tag(pb, 8, 0x3B0B);
636 * Returns the length of the UTF-16 string, in 16-bit characters, that would result
637 * from decoding the utf-8 string.
639 static uint64_t mxf_utf16len(const char *utf8_str)
641 const uint8_t *q = utf8_str;
645 GET_UTF8(ch, *q++, goto invalid;)
652 av_log(NULL, AV_LOG_ERROR, "Invaid UTF8 sequence in mxf_utf16len\n\n");
659 * Returns the calculated length a local tag containing an utf-8 string as utf-16
661 static int mxf_utf16_local_tag_length(const char *utf8_str)
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);
678 * Write a local tag containing an utf-8 string as utf-16
680 static void mxf_write_local_tag_utf16(AVIOContext *pb, int tag, const char *value)
682 uint64_t size = mxf_utf16len(value);
684 if (size >= UINT16_MAX/2) {
685 av_log(NULL, AV_LOG_ERROR, "utf16 local tag size %"PRIx64" invalid (too large), ignoring\n", size);
689 mxf_write_local_tag(pb, size*2, tag);
690 avio_put_str16be(pb, value);
693 static void mxf_write_identification(AVFormatContext *s)
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";
702 mxf_write_metadata_key(pb, 0x013000);
703 PRINT_KEY(s, "identification key", pb->buf_ptr - 16);
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);
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);
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
725 mxf_write_local_tag(pb, 16, 0x3C05);
726 mxf_write_uuid(pb, Identification, 2);
729 mxf_write_local_tag(pb, 8, 0x3C06);
730 avio_wb64(pb, mxf->timestamp);
733 static void mxf_write_content_storage(AVFormatContext *s)
735 AVIOContext *pb = s->pb;
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);
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);
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);
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);
758 static void mxf_write_track(AVFormatContext *s, AVStream *st, enum MXFMetadataSetType type)
760 MXFContext *mxf = s->priv_data;
761 AVIOContext *pb = s->pb;
762 MXFStreamContext *sc = st->priv_data;
764 mxf_write_metadata_key(pb, 0x013b00);
765 PRINT_KEY(s, "track key", pb->buf_ptr - 16);
766 klv_encode_ber_length(pb, 80);
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);
774 mxf_write_local_tag(pb, 4, 0x4801);
775 avio_wb32(pb, st->index+2);
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
782 avio_write(pb, sc->track_essence_element_key + 12, 4);
784 mxf_write_local_tag(pb, 8, 0x4B01);
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);
790 avio_wb32(pb, mxf->time_base.den);
791 avio_wb32(pb, mxf->time_base.num);
795 mxf_write_local_tag(pb, 8, 0x4B02);
798 // write sequence refs
799 mxf_write_local_tag(pb, 16, 0x4803);
800 mxf_write_uuid(pb, type == MaterialPackage ? Sequence: Sequence + TypeBottom, st->index);
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 };
805 static void mxf_write_common_fields(AVFormatContext *s, AVStream *st)
807 MXFContext *mxf = s->priv_data;
808 AVIOContext *pb = s->pb;
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);
815 const MXFCodecUL *data_def_ul = mxf_get_data_definition_ul(st->codec->codec_type);
816 avio_write(pb, data_def_ul->uid, 16);
820 mxf_write_local_tag(pb, 8, 0x0202);
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);
825 avio_wb64(pb, mxf->duration);
829 static void mxf_write_sequence(AVFormatContext *s, AVStream *st, enum MXFMetadataSetType type)
831 MXFContext *mxf = s->priv_data;
832 AVIOContext *pb = s->pb;
833 enum MXFMetadataSetType component;
835 mxf_write_metadata_key(pb, 0x010f00);
836 PRINT_KEY(s, "sequence key", pb->buf_ptr - 16);
837 klv_encode_ber_length(pb, 80);
839 mxf_write_local_tag(pb, 16, 0x3C0A);
840 mxf_write_uuid(pb, type == MaterialPackage ? Sequence: Sequence + TypeBottom, st->index);
842 PRINT_KEY(s, "sequence uid", pb->buf_ptr - 16);
843 mxf_write_common_fields(s, st);
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;
851 component = SourceClip;
852 if (type == SourcePackage)
853 component += TypeBottom;
854 mxf_write_uuid(pb, component, st->index);
857 static void mxf_write_timecode_component(AVFormatContext *s, AVStream *st, enum MXFMetadataSetType type)
859 MXFContext *mxf = s->priv_data;
860 AVIOContext *pb = s->pb;
862 mxf_write_metadata_key(pb, 0x011400);
863 klv_encode_ber_length(pb, 75);
866 mxf_write_local_tag(pb, 16, 0x3C0A);
867 mxf_write_uuid(pb, type == MaterialPackage ? TimecodeComponent :
868 TimecodeComponent + TypeBottom, st->index);
870 mxf_write_common_fields(s, st);
873 mxf_write_local_tag(pb, 8, 0x1501);
874 avio_wb64(pb, mxf->tc.start);
876 // Rounded Time Code Base
877 mxf_write_local_tag(pb, 2, 0x1502);
878 avio_wb16(pb, mxf->timecode_base);
881 mxf_write_local_tag(pb, 1, 0x1503);
882 avio_w8(pb, !!(mxf->tc.flags & AV_TIMECODE_FLAG_DROPFRAME));
885 static void mxf_write_structural_component(AVFormatContext *s, AVStream *st, enum MXFMetadataSetType type)
887 AVIOContext *pb = s->pb;
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);
895 mxf_write_local_tag(pb, 16, 0x3C0A);
896 mxf_write_uuid(pb, type == MaterialPackage ? SourceClip: SourceClip + TypeBottom, st->index);
898 PRINT_KEY(s, "structural component uid", pb->buf_ptr - 16);
899 mxf_write_common_fields(s, st);
901 // write start_position
902 mxf_write_local_tag(pb, 8, 0x1201);
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++)
911 mxf_write_umid(s, 1);
913 // write source track id
914 mxf_write_local_tag(pb, 4, 0x1102);
915 if (type == SourcePackage)
918 avio_wb32(pb, st->index+2);
921 static void mxf_write_multi_descriptor(AVFormatContext *s)
923 MXFContext *mxf = s->priv_data;
924 AVIOContext *pb = s->pb;
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);
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);
937 mxf_write_local_tag(pb, 8, 0x3001);
938 avio_wb32(pb, mxf->time_base.den);
939 avio_wb32(pb, mxf->time_base.num);
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;
946 MXFStreamContext *sc = s->streams[0]->priv_data;
947 ul = mxf_essence_container_uls[sc->index].container_ul;
949 avio_write(pb, ul, 16);
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);
958 static void mxf_write_generic_desc(AVFormatContext *s, AVStream *st, const UID key, unsigned size)
960 MXFContext *mxf = s->priv_data;
961 MXFStreamContext *sc = st->priv_data;
962 AVIOContext *pb = s->pb;
964 avio_write(pb, key, 16);
965 klv_encode_ber4_length(pb, size+20+8+12+20);
967 mxf_write_local_tag(pb, 16, 0x3C0A);
968 mxf_write_uuid(pb, SubDescriptor, st->index);
970 mxf_write_local_tag(pb, 4, 0x3006);
971 avio_wb32(pb, st->index+2);
973 mxf_write_local_tag(pb, 8, 0x3001);
974 avio_wb32(pb, mxf->time_base.den);
975 avio_wb32(pb, mxf->time_base.num);
977 mxf_write_local_tag(pb, 16, 0x3004);
978 avio_write(pb, mxf_essence_container_uls[sc->index].container_ul, 16);
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 };
987 static void mxf_write_cdci_common(AVFormatContext *s, AVStream *st, const UID key, unsigned size)
989 MXFStreamContext *sc = st->priv_data;
990 AVIOContext *pb = s->pb;
991 int stored_height = (st->codec->height+15)/16*16;
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)
998 mxf_write_generic_desc(s, st, key, desc_size);
1000 mxf_write_local_tag(pb, 4, 0x3203);
1001 avio_wb32(pb, st->codec->width);
1003 mxf_write_local_tag(pb, 4, 0x3202);
1004 avio_wb32(pb, stored_height>>sc->interlaced);
1006 mxf_write_local_tag(pb, 4, 0x3209);
1007 avio_wb32(pb, st->codec->width);
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;
1014 display_height = st->codec->height;
1016 mxf_write_local_tag(pb, 4, 0x3208);
1017 avio_wb32(pb, display_height>>sc->interlaced);
1019 // presentation Y offset
1020 mxf_write_local_tag(pb, 4, 0x320B);
1021 avio_wb32(pb, (st->codec->height - display_height)>>sc->interlaced);
1024 mxf_write_local_tag(pb, 4, 0x3301);
1025 avio_wb32(pb, sc->component_depth);
1027 // horizontal subsampling
1028 mxf_write_local_tag(pb, 4, 0x3302);
1032 mxf_write_local_tag(pb, 1, 0x320C);
1033 avio_w8(pb, sc->interlaced);
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;
1046 if (!sc->interlaced) {
1051 mxf_write_local_tag(pb, 12+sc->interlaced*4, 0x320D);
1052 avio_wb32(pb, sc->interlaced ? 2 : 1);
1058 mxf_write_local_tag(pb, 8, 0x320E);
1059 avio_wb32(pb, sc->aspect_ratio.num);
1060 avio_wb32(pb, sc->aspect_ratio.den);
1062 mxf_write_local_tag(pb, 16, 0x3201);
1063 avio_write(pb, *sc->codec_ul, 16);
1065 if (sc->interlaced && sc->field_dominance) {
1066 mxf_write_local_tag(pb, 1, 0x3212);
1067 avio_w8(pb, sc->field_dominance);
1072 static void mxf_write_cdci_desc(AVFormatContext *s, AVStream *st)
1074 mxf_write_cdci_common(s, st, mxf_cdci_descriptor_key, 0);
1077 static void mxf_write_mpegvideo_desc(AVFormatContext *s, AVStream *st)
1079 AVIOContext *pb = s->pb;
1080 MXFStreamContext *sc = st->priv_data;
1081 int profile_and_level = (st->codec->profile<<4) | st->codec->level;
1083 if (st->codec->codec_id != AV_CODEC_ID_H264) {
1084 mxf_write_cdci_common(s, st, mxf_mpegvideo_descriptor_key, 8+5);
1087 mxf_write_local_tag(pb, 4, 0x8000);
1088 avio_wb32(pb, sc->video_bit_rate);
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);
1096 mxf_write_cdci_common(s, st, mxf_mpegvideo_descriptor_key, 0);
1100 static void mxf_write_generic_sound_common(AVFormatContext *s, AVStream *st, const UID key, unsigned size)
1102 AVIOContext *pb = s->pb;
1103 MXFContext *mxf = s->priv_data;
1104 int show_warnings = !mxf->footer_partition_offset;
1105 int duration_size = 0;
1107 if (s->oformat == &ff_mxf_opatom_muxer)
1110 mxf_write_generic_desc(s, st, key, size+duration_size+5+12+8+8);
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);
1118 mxf_write_local_tag(pb, 1, 0x3D02);
1121 // write audio sampling rate
1122 mxf_write_local_tag(pb, 8, 0x3D03);
1123 avio_wb32(pb, st->codec->sample_rate);
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);
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);
1143 mxf_write_local_tag(pb, 4, 0x3D01);
1144 avio_wb32(pb, av_get_bits_per_sample(st->codec->codec_id));
1147 static void mxf_write_wav_common(AVFormatContext *s, AVStream *st, const UID key, unsigned size)
1149 AVIOContext *pb = s->pb;
1151 mxf_write_generic_sound_common(s, st, key, size+6+8);
1153 mxf_write_local_tag(pb, 2, 0x3D0A);
1154 avio_wb16(pb, st->codec->block_align);
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);
1161 static void mxf_write_wav_desc(AVFormatContext *s, AVStream *st)
1163 mxf_write_wav_common(s, st, mxf_wav_descriptor_key, 0);
1166 static void mxf_write_aes3_desc(AVFormatContext *s, AVStream *st)
1168 mxf_write_wav_common(s, st, mxf_aes3_descriptor_key, 0);
1171 static void mxf_write_generic_sound_desc(AVFormatContext *s, AVStream *st)
1173 mxf_write_generic_sound_common(s, st, mxf_generic_sound_descriptor_key, 0);
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 };
1178 static int mxf_write_tagged_value(AVFormatContext *s, const char* name, const char* value)
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);
1185 if (!name_size || indirect_value_size == 13)
1188 mxf_write_metadata_key(pb, 0x013f00);
1189 klv_encode_ber_length(pb, 24 + name_size + indirect_value_size);
1191 // write instance UID
1192 mxf_write_local_tag(pb, 16, 0x3C0A);
1193 mxf_write_uuid(pb, TaggedValue, mxf->tagged_value_count);
1196 mxf_write_local_tag_utf16(pb, 0x5001, name); // Name
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);
1203 mxf->tagged_value_count++;
1207 static int mxf_write_user_comments(AVFormatContext *s, const AVDictionary *m)
1209 MXFContext *mxf = s->priv_data;
1210 AVDictionaryEntry *t = NULL;
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");
1219 if (mxf_write_tagged_value(s, t->key + 8, t->value) == 0)
1225 static void mxf_write_package(AVFormatContext *s, enum MXFMetadataSetType type, const char *package_name)
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;
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));
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
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);
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);
1257 mxf_write_local_tag_utf16(pb, 0x4402, package_name);
1259 // package creation date
1260 mxf_write_local_tag(pb, 8, 0x4405);
1261 avio_wb64(pb, mxf->timestamp);
1263 // package modified date
1264 mxf_write_local_tag(pb, 8, 0x4404);
1265 avio_wb64(pb, mxf->timestamp);
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);
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);
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);
1288 mxf_write_uuid(pb, SubDescriptor, 0);
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);
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);
1302 if (type == SourcePackage) {
1303 MXFStreamContext *sc = st->priv_data;
1304 mxf_essence_container_uls[sc->index].write_desc(s, st);
1309 static int mxf_write_essence_container_data(AVFormatContext *s)
1311 AVIOContext *pb = s->pb;
1313 mxf_write_metadata_key(pb, 0x012300);
1314 klv_encode_ber_length(pb, 72);
1316 mxf_write_local_tag(pb, 16, 0x3C0A); // Instance UID
1317 mxf_write_uuid(pb, EssenceContainerData, 0);
1319 mxf_write_local_tag(pb, 32, 0x2701); // Linked Package UID
1320 mxf_write_umid(s, 1);
1322 mxf_write_local_tag(pb, 4, 0x3F07); // BodySID
1325 mxf_write_local_tag(pb, 4, 0x3F06); // IndexSID
1331 static int mxf_write_header_metadata_sets(AVFormatContext *s)
1333 const char *material_package_name = NULL;
1334 const char *file_package_name = NULL;
1335 AVDictionaryEntry *entry = NULL;
1336 AVStream *st = NULL;
1339 if (entry = av_dict_get(s->metadata, "material_package_name", NULL, 0))
1340 material_package_name = entry->value;
1342 if (entry = av_dict_get(s->metadata, "file_package_name", NULL, 0)) {
1343 file_package_name = entry->value;
1345 /* check if any of the streams contain a file_package_name */
1346 for (i = 0; i < s->nb_streams; i++) {
1348 if (entry = av_dict_get(st->metadata, "file_package_name", NULL, 0)) {
1349 file_package_name = entry->value;
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);
1364 static unsigned klv_fill_size(uint64_t size)
1366 unsigned pad = KAG_SIZE - (size & (KAG_SIZE-1));
1367 if (pad < 20) // smallest fill item possible
1368 return pad + KAG_SIZE;
1370 return pad & (KAG_SIZE-1);
1373 static void mxf_write_index_table_segment(AVFormatContext *s)
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;
1380 av_log(s, AV_LOG_DEBUG, "edit units count %d\n", mxf->edit_units_count);
1382 if (!mxf->edit_units_count && !mxf->edit_unit_byte_count)
1385 avio_write(pb, index_table_segment_key, 16);
1387 if (mxf->edit_unit_byte_count) {
1388 klv_encode_ber_length(pb, 80);
1390 klv_encode_ber_length(pb, 85 + 12+(s->nb_streams+1LL)*6 +
1391 12+mxf->edit_units_count*(11+mxf->slice_count*4LL));
1395 mxf_write_local_tag(pb, 16, 0x3C0A);
1396 mxf_write_uuid(pb, IndexTableSegment, 0);
1399 mxf_write_local_tag(pb, 8, 0x3F0B);
1400 avio_wb32(pb, mxf->time_base.den);
1401 avio_wb32(pb, mxf->time_base.num);
1403 // index start position
1404 mxf_write_local_tag(pb, 8, 0x3F0C);
1405 avio_wb64(pb, mxf->last_indexed_edit_unit);
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
1412 avio_wb64(pb, mxf->edit_units_count);
1414 // edit unit byte count
1415 mxf_write_local_tag(pb, 4, 0x3F05);
1416 avio_wb32(pb, mxf->edit_unit_byte_count);
1419 mxf_write_local_tag(pb, 4, 0x3F06);
1423 mxf_write_local_tag(pb, 4, 0x3F07);
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);
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
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);
1452 avio_wb32(pb, (i-1)*audio_frame_size); // element delta
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
1460 for (i = 0; i < mxf->edit_units_count; i++) {
1461 int temporal_offset = 0;
1463 if (!(mxf->index_entries[i].flags & 0x33)) { // I frame
1464 mxf->last_key_index = key_index;
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)
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;
1480 avio_w8(pb, temporal_offset);
1482 if ((mxf->index_entries[i].flags & 0x30) == 0x30) { // back and forward prediction
1483 avio_w8(pb, mxf->last_key_index - i);
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;
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);
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);
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;
1506 static void mxf_write_klv_fill(AVFormatContext *s)
1508 unsigned pad = klv_fill_size(avio_tell(s->pb));
1510 avio_write(s->pb, klv_fill_key, 16);
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)));
1518 static int mxf_write_partition(AVFormatContext *s, int bodysid,
1520 const uint8_t *key, int write_metadata)
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);
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;
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);
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;
1547 mxf->body_partition_offset[mxf->body_partitions_count++] = partition_offset;
1552 avio_write(pb, key, 16);
1554 avio_write(pb, body_partition_key, 16);
1556 klv_encode_ber_length(pb, 88 + 16LL * DESCRIPTOR_COUNT(mxf->essence_container_count));
1558 // write partition value
1559 avio_wb16(pb, 1); // majorVersion
1560 avio_wb16(pb, 2); // minorVersion
1561 avio_wb32(pb, KAG_SIZE); // KAGSize
1563 avio_wb64(pb, partition_offset); // ThisPartition
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
1572 avio_wb64(pb, mxf->footer_partition_offset); // footerPartition
1575 header_byte_count_offset = avio_tell(pb);
1576 avio_wb64(pb, 0); // headerByteCount, update later
1579 avio_wb64(pb, index_byte_count); // indexByteCount
1580 avio_wb32(pb, index_byte_count ? indexsid : 0); // indexSID
1583 if (bodysid && mxf->edit_units_count && mxf->body_partitions_count && s->oformat != &ff_mxf_opatom_muxer)
1584 avio_wb64(pb, mxf->body_offset);
1588 avio_wb32(pb, bodysid); // bodySID
1590 // operational pattern
1591 if (s->oformat == &ff_mxf_opatom_muxer)
1592 avio_write(pb, opatom_ul, 16);
1594 avio_write(pb, op1a_ul, 16);
1596 // essence container
1597 mxf_write_essence_container_refs(s);
1599 if (write_metadata) {
1600 // mark the start of the headermetadata and calculate metadata size
1602 unsigned header_byte_count;
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);
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);
1623 static int mxf_parse_dnxhd_frame(AVFormatContext *s, AVStream *st,
1626 MXFContext *mxf = s->priv_data;
1627 MXFStreamContext *sc = st->priv_data;
1629 uint8_t* header_cid;
1632 if (mxf->header_written)
1638 header_cid = pkt->data + 0x28;
1639 cid = header_cid[0] << 24 | header_cid[1] << 16 | header_cid[2] << 8 | header_cid[3];
1641 if ((frame_size = avpriv_dnxhd_get_frame_size(cid)) < 0)
1647 sc->component_depth = 10;
1657 sc->component_depth = 10;
1667 sc->component_depth = 10;
1682 sc->codec_ul = &mxf_essence_container_uls[sc->index].codec_ul;
1683 sc->aspect_ratio = (AVRational){ 16, 9 };
1685 if(s->oformat == &ff_mxf_opatom_muxer){
1686 mxf->edit_unit_byte_count = frame_size;
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);
1706 static int mxf_parse_dv_frame(AVFormatContext *s, AVStream *st, AVPacket *pkt)
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;
1713 if (mxf->header_written)
1716 // Check for minimal frame size
1717 if (pkt->size < 120000)
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;
1725 if ((vs_pack[2] & 0x07) == 0x02)
1726 sc->aspect_ratio = (AVRational){ 16, 9 };
1728 sc->aspect_ratio = (AVRational){ 4, 3 };
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);
1736 case 0x18: // DV100 720p
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");
1744 case 0x14: // DV100 1080i
1746 frame_size = pal ? 576000 : 480000;
1750 frame_size = pal ? 288000 : 240000;
1754 frame_size = pal ? 144000 : 120000;
1757 sc->index = ul_index + 16;
1758 sc->codec_ul = &mxf_essence_container_uls[sc->index].codec_ul;
1760 if(s->oformat == &ff_mxf_opatom_muxer) {
1761 mxf->edit_unit_byte_count = frame_size;
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);
1781 static const struct {
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
1803 static int mxf_parse_h264_frame(AVFormatContext *s, AVStream *st,
1804 AVPacket *pkt, MXFIndexEntry *e)
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
1816 if (pkt->size > extra_size)
1817 buf_end -= pkt->size - extra_size; // no need to parse beyond SPS/PPS header
1820 buf = avpriv_find_start_code(buf, buf_end, &state);
1824 switch (state & 0x1f) {
1826 st->codec->profile = buf[1];
1830 if (e->flags & 0x40) { // sequence header present
1831 e->flags |= 0x80; // random access
1841 if (mxf->header_written)
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;
1848 sc->field_dominance = 1; // top field first is mandatory for AVC Intra
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;
1856 } else if (st->codec->profile == mxf_h264_codec_uls[i].profile) {
1857 sc->codec_ul = &mxf_h264_codec_uls[i].uid;
1863 av_log(s, AV_LOG_ERROR, "AVC Intra 50/100 supported only\n");
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
1883 static const UID *mxf_get_mpeg2_codec_ul(AVCodecContext *avctx)
1885 int long_gop = avctx->gop_size > 1 || avctx->has_b_frames;
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];
1903 static int mxf_parse_mpeg2_frame(AVFormatContext *s, AVStream *st,
1904 AVPacket *pkt, MXFIndexEntry *e)
1906 MXFStreamContext *sc = st->priv_data;
1910 for(i = 0; i < pkt->size - 4; i++) {
1911 c = (c<<8) + pkt->data[i];
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
1919 sc->field_dominance = 1 + !(pkt->data[i+4] & 0x80); // top field first
1922 } else if (c == 0x1b8) { // gop
1923 if (pkt->data[i+4]>>6 & 0x01) { // closed
1925 if (e->flags & 0x40) // sequence header present
1926 e->flags |= 0x80; // random access
1928 } else if (c == 0x1b3) { // seq
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;
1935 av_reduce(&sc->aspect_ratio.num, &sc->aspect_ratio.den,
1936 st->codec->width, st->codec->height, 1024*1024);
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
1943 sc->closed_gop = 0; // reset closed gop, don't matter anymore
1944 } else if (pict_type == 3) { // B frame
1946 e->flags |= 0x13; // only backward prediction
1949 sc->temporal_reordering = -1;
1950 } else if (!pict_type) {
1951 av_log(s, AV_LOG_ERROR, "error parsing mpeg2 frame\n");
1956 if (s->oformat != &ff_mxf_d10_muxer)
1957 sc->codec_ul = mxf_get_mpeg2_codec_ul(st->codec);
1958 return !!sc->codec_ul;
1961 static uint64_t mxf_parse_timestamp(time_t timestamp)
1964 struct tm *time = gmtime_r(×tamp, &tmbuf);
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 |
1975 static void mxf_gen_umid(AVFormatContext *s)
1977 MXFContext *mxf = s->priv_data;
1978 uint32_t seed = av_get_random_seed();
1979 uint64_t umid = seed + 0x5294713400000000LL;
1981 AV_WB64(mxf->umid , umid);
1982 AV_WB64(mxf->umid+8, umid>>8);
1984 mxf->instance_number = seed & 0xFFFFFF;
1987 static int mxf_init_timecode(AVFormatContext *s, AVStream *st, AVRational rate)
1989 MXFContext *mxf = s->priv_data;
1990 AVDictionaryEntry *tcr = av_dict_get(s->metadata, "timecode", NULL, 0);
1992 tcr = av_dict_get(st->metadata, "timecode", NULL, 0);
1995 return av_timecode_init_from_string(&mxf->tc, rate, tcr->value, s);
1997 return av_timecode_init(&mxf->tc, rate, 0, 0, s);
2000 static int mxf_write_header(AVFormatContext *s)
2002 MXFContext *mxf = s->priv_data;
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;
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");
2017 for (i = 0; i < s->nb_streams; i++) {
2018 AVStream *st = s->streams[i];
2019 MXFStreamContext *sc = av_mallocz(sizeof(*sc));
2021 return AVERROR(ENOMEM);
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");
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);
2037 av_log(s, AV_LOG_ERROR, "Unsupported video frame rate %d/%d\n",
2039 return AVERROR(EINVAL);
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)
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;
2052 } else if (sc->video_bit_rate == 40000000) {
2053 if (mxf->time_base.den == 25) sc->index = 7;
2055 } else if (sc->video_bit_rate == 30000000) {
2056 if (mxf->time_base.den == 25) sc->index = 11;
2057 else sc->index = 13;
2059 av_log(s, AV_LOG_ERROR, "error MXF D-10 only support 30/40/50 mbit/s\n");
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);
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");
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");
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");
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);
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;
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);
2099 spf = ff_mxf_get_samples_per_frame(s, tbc);
2101 av_log(s, AV_LOG_ERROR, "Unsupported timecode frame rate %d/%d\n", tbc.den, tbc.num);
2102 return AVERROR(EINVAL);
2105 mxf->time_base = st->time_base;
2106 if((ret = mxf_init_timecode(s, st, av_inv_q(spf->time_base))) < 0)
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;
2113 mxf->slice_count = 1;
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);
2126 sc->codec_ul = &mxf_essence_container_uls[sc->index].codec_ul;
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);
2132 if (!present[sc->index])
2133 mxf->essence_container_count++;
2134 present[sc->index]++;
2137 if (s->oformat == &ff_mxf_d10_muxer || s->oformat == &ff_mxf_opatom_muxer) {
2138 mxf->essence_container_count = 1;
2141 if (!(s->flags & AVFMT_FLAG_BITEXACT))
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);
2151 sc->order = AV_RB32(sc->track_essence_element_key+12);
2154 if (t = av_dict_get(s->metadata, "creation_time", NULL, 0))
2155 timestamp = ff_iso8601_to_unix_time(t->value);
2157 mxf->timestamp = mxf_parse_timestamp(timestamp);
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;
2169 spf = ff_mxf_get_samples_per_frame(s, (AVRational){ 1, 25 });
2171 if (ff_audio_interleave_init(s, spf->samples_per_frame, mxf->time_base) < 0)
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 };
2180 static void mxf_write_system_item(AVFormatContext *s)
2182 MXFContext *mxf = s->priv_data;
2183 AVIOContext *pb = s->pb;
2187 frame = mxf->last_indexed_edit_unit + mxf->edit_units_count;
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);
2200 MXFStreamContext *sc = s->streams[0]->priv_data;
2201 avio_write(pb, mxf_essence_container_uls[sc->index].container_ul, 16);
2205 avio_wb64(pb, 0); // creation date/time stamp
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
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);
2221 static void mxf_write_d10_video_packet(AVFormatContext *s, AVStream *st, AVPacket *pkt)
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
2230 packet_size += 16 + 4;
2231 packet_size += klv_fill_size(packet_size);
2233 klv_encode_ber4_length(pb, pkt->size);
2234 avio_write(pb, pkt->data, pkt->size);
2236 // ensure CBR muxing by padding to correct video frame size
2237 pad = packet_size - pkt->size - 16 - 4;
2239 avio_write(s->pb, klv_fill_key, 16);
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)));
2245 av_log(s, AV_LOG_WARNING, "cannot fill d-10 video packet\n");
2246 ffio_fill(s->pb, 0, pad);
2250 static void mxf_write_d10_audio_packet(AVFormatContext *s, AVStream *st, AVPacket *pkt)
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;
2259 klv_encode_ber4_length(pb, 4 + frame_size*4*8);
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);
2265 while (samples < end) {
2266 for (i = 0; i < st->codec->channels; i++) {
2268 if (st->codec->codec_id == AV_CODEC_ID_PCM_S24LE) {
2269 sample = AV_RL24(samples)<< 4;
2272 sample = AV_RL16(samples)<<12;
2275 avio_wl32(pb, sample | i);
2282 static int mxf_write_opatom_body_partition(AVFormatContext *s)
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;
2292 if (!mxf->header_written)
2293 key = body_partition_key;
2295 if ((err = mxf_write_partition(s, 1, 0, key, 0)) < 0)
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);
2303 static int mxf_write_opatom_packet(AVFormatContext *s, AVPacket *pkt, MXFIndexEntry *ie)
2305 MXFContext *mxf = s->priv_data;
2306 AVIOContext *pb = s->pb;
2310 if (!mxf->header_written) {
2311 if ((err = mxf_write_partition(s, 0, 0, header_open_partition_key, 1)) < 0)
2313 mxf_write_klv_fill(s);
2315 if ((err = mxf_write_opatom_body_partition(s)) < 0)
2317 mxf->header_written = 1;
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;
2325 mxf->edit_units_count++;
2326 avio_write(pb, pkt->data, pkt->size);
2327 mxf->body_offset += pkt->size;
2333 static int mxf_write_packet(AVFormatContext *s, AVPacket *pkt)
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};
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");
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");
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");
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");
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");
2373 if (s->oformat == &ff_mxf_opatom_muxer)
2374 return mxf_write_opatom_packet(s, pkt, &ie);
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)
2380 mxf_write_klv_fill(s);
2381 mxf_write_index_table_segment(s);
2383 if ((err = mxf_write_partition(s, 0, 0, header_open_partition_key, 1)) < 0)
2386 mxf->header_written = 1;
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)
2396 mxf_write_klv_fill(s);
2397 mxf_write_index_table_segment(s);
2400 mxf_write_klv_fill(s);
2401 mxf_write_system_item(s);
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
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;
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);
2421 mxf_write_d10_audio_packet(s, st, pkt);
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);
2433 static void mxf_write_random_index_pack(AVFormatContext *s)
2435 MXFContext *mxf = s->priv_data;
2436 AVIOContext *pb = s->pb;
2437 uint64_t pos = avio_tell(pb);
2440 avio_write(pb, random_index_pack_key, 16);
2441 klv_encode_ber_length(pb, 28 + 12LL*mxf->body_partitions_count);
2443 if (mxf->edit_unit_byte_count && s->oformat != &ff_mxf_opatom_muxer)
2444 avio_wb32(pb, 1); // BodySID of header partition
2447 avio_wb64(pb, 0); // offset of header partition
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]);
2454 avio_wb32(pb, 0); // BodySID of footer partition
2455 avio_wb64(pb, mxf->footer_partition_offset);
2457 avio_wb32(pb, avio_tell(pb) - pos + 4);
2460 static int mxf_write_footer(AVFormatContext *s)
2462 MXFContext *mxf = s->priv_data;
2463 AVIOContext *pb = s->pb;
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;
2473 mxf->duration = mxf->last_indexed_edit_unit + mxf->edit_units_count;
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)
2481 if ((err = mxf_write_partition(s, 0, 2, footer_partition_key, 0)) < 0)
2483 mxf_write_klv_fill(s);
2484 mxf_write_index_table_segment(s);
2487 mxf_write_klv_fill(s);
2488 mxf_write_random_index_pack(s);
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)
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)
2502 mxf_write_klv_fill(s);
2503 mxf_write_index_table_segment(s);
2505 if ((err = mxf_write_partition(s, 0, 0, header_closed_partition_key, 1)) < 0)
2511 ff_audio_interleave_close(s);
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);
2520 return err < 0 ? err : 0;
2523 static int mxf_interleave_get_packet(AVFormatContext *s, AVPacket *out, AVPacket *pkt, int flush)
2525 int i, stream_count = 0;
2527 for (i = 0; i < s->nb_streams; i++)
2528 stream_count += !!s->streams[i]->last_in_packet_buffer;
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
2536 if (!stream_count || pktl->pkt.stream_index == 0)
2542 // purge packet queue
2544 AVPacketList *next = pktl->next;
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);
2555 s->internal->packet_buffer = NULL;
2556 s->internal->packet_buffer_end= NULL;
2559 pktl = s->internal->packet_buffer;
2563 av_log(s, AV_LOG_TRACE, "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;
2573 av_init_packet(out);
2578 static int mxf_compare_timestamps(AVFormatContext *s, AVPacket *next, AVPacket *pkt)
2580 MXFStreamContext *sc = s->streams[pkt ->stream_index]->priv_data;
2581 MXFStreamContext *sc2 = s->streams[next->stream_index]->priv_data;
2583 return next->dts > pkt->dts ||
2584 (next->dts == pkt->dts && sc->order < sc2->order);
2587 static int mxf_interleave(AVFormatContext *s, AVPacket *out, AVPacket *pkt, int flush)
2589 return ff_audio_rechunk_interleave(s, out, pkt, flush,
2590 mxf_interleave_get_packet, mxf_compare_timestamps);
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},
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,
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 },
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,
2619 AVOutputFormat ff_mxf_muxer = {
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,
2634 AVOutputFormat ff_mxf_d10_muxer = {
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,
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,