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 RP210: SMPTE Metadata Dictionary
30 * SMPTE RP224: Registry of SMPTE Universal Labels
37 #include "libavutil/opt.h"
38 #include "libavutil/random_seed.h"
39 #include "libavutil/timecode.h"
40 #include "libavutil/avassert.h"
41 #include "libavcodec/bytestream.h"
42 #include "libavcodec/dnxhddata.h"
43 #include "audiointerleave.h"
45 #include "avio_internal.h"
50 extern AVOutputFormat ff_mxf_d10_muxer
;
52 #define EDIT_UNITS_PER_BODY 250
63 unsigned slice_offset
; ///< offset of audio slice
64 uint16_t temporal_ref
;
68 AudioInterleaveContext aic
;
69 UID track_essence_element_key
;
70 int index
; ///< index in mxf_essence_container_uls table
72 int order
; ///< interleaving order if dts are equal
73 int interlaced
; ///< whether picture is interlaced
74 int field_dominance
; ///< tff=1, bff=2
76 int temporal_reordering
;
77 AVRational aspect_ratio
; ///< display aspect ratio
78 int closed_gop
; ///< gop is closed, used in mpeg-2 frame parsing
86 void (*write_desc
)(AVFormatContext
*, AVStream
*);
87 } MXFContainerEssenceEntry
;
92 } mxf_essence_mappings
[] = {
93 { AV_CODEC_ID_MPEG2VIDEO
, 0 },
94 { AV_CODEC_ID_PCM_S24LE
, 1 },
95 { AV_CODEC_ID_PCM_S16LE
, 1 },
96 { AV_CODEC_ID_DVVIDEO
, 15 },
97 { AV_CODEC_ID_DNXHD
, 24 },
101 static void mxf_write_wav_desc(AVFormatContext
*s
, AVStream
*st
);
102 static void mxf_write_aes3_desc(AVFormatContext
*s
, AVStream
*st
);
103 static void mxf_write_mpegvideo_desc(AVFormatContext
*s
, AVStream
*st
);
104 static void mxf_write_cdci_desc(AVFormatContext
*s
, AVStream
*st
);
105 static void mxf_write_generic_sound_desc(AVFormatContext
*s
, AVStream
*st
);
107 static const MXFContainerEssenceEntry mxf_essence_container_uls
[] = {
108 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x02,0x0D,0x01,0x03,0x01,0x02,0x04,0x60,0x01 },
109 { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x15,0x01,0x05,0x00 },
110 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x00,0x00,0x00 },
111 mxf_write_mpegvideo_desc
},
112 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x06,0x03,0x00 },
113 { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x16,0x01,0x03,0x00 },
114 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 },
115 mxf_write_aes3_desc
},
116 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x06,0x01,0x00 },
117 { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x16,0x01,0x01,0x00 },
118 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 },
119 mxf_write_wav_desc
},
120 // D-10 625/50 PAL 50mb/s
121 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x01,0x01 },
122 { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x05,0x01,0x01,0x00 },
123 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x01,0x02,0x01,0x01 },
124 mxf_write_cdci_desc
},
125 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x01,0x01 },
126 { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x06,0x01,0x10,0x00 },
127 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 },
128 mxf_write_generic_sound_desc
},
129 // D-10 525/60 NTSC 50mb/s
130 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x02,0x01 },
131 { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x05,0x01,0x01,0x00 },
132 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x01,0x02,0x01,0x02 },
133 mxf_write_cdci_desc
},
134 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x02,0x01 },
135 { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x06,0x01,0x10,0x00 },
136 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 },
137 mxf_write_generic_sound_desc
},
138 // D-10 625/50 PAL 40mb/s
139 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x03,0x01 },
140 { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x05,0x01,0x01,0x00 },
141 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x01,0x02,0x01,0x03 },
142 mxf_write_cdci_desc
},
143 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x03,0x01 },
144 { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x06,0x01,0x10,0x00 },
145 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 },
146 mxf_write_generic_sound_desc
},
147 // D-10 525/60 NTSC 40mb/s
148 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x04,0x01 },
149 { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x05,0x01,0x01,0x00 },
150 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x01,0x02,0x01,0x04 },
151 mxf_write_cdci_desc
},
152 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x04,0x01 },
153 { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x06,0x01,0x10,0x00 },
154 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 },
155 mxf_write_generic_sound_desc
},
156 // D-10 625/50 PAL 30mb/s
157 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x05,0x01 },
158 { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x05,0x01,0x01,0x00 },
159 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x01,0x02,0x01,0x05 },
160 mxf_write_cdci_desc
},
161 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x05,0x01 },
162 { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x06,0x01,0x10,0x00 },
163 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 },
164 mxf_write_generic_sound_desc
},
165 // D-10 525/60 NTSC 30mb/s
166 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x06,0x01 },
167 { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x05,0x01,0x01,0x00 },
168 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x01,0x02,0x01,0x06 },
169 mxf_write_cdci_desc
},
170 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x06,0x01 },
171 { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x06,0x01,0x10,0x00 },
172 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 },
173 mxf_write_generic_sound_desc
},
175 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x02,0x7F,0x01 },
176 { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x18,0x01,0x01,0x00 },
177 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x02,0x00,0x00,0x00 },
178 mxf_write_cdci_desc
},
180 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x02,0x40,0x01 },
181 { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x18,0x01,0x01,0x00 },
182 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x02,0x02,0x01,0x00 },
183 mxf_write_cdci_desc
},
185 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x02,0x41,0x01 },
186 { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x18,0x01,0x01,0x00 },
187 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x02,0x02,0x02,0x00 },
188 mxf_write_cdci_desc
},
190 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x02,0x50,0x01 },
191 { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x18,0x01,0x01,0x00 },
192 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x02,0x02,0x03,0x00 },
193 mxf_write_cdci_desc
},
195 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x02,0x51,0x01 },
196 { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x18,0x01,0x01,0x00 },
197 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x02,0x02,0x04,0x00 },
198 mxf_write_cdci_desc
},
200 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x02,0x60,0x01 },
201 { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x18,0x01,0x01,0x00 },
202 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x02,0x02,0x05,0x00 },
203 mxf_write_cdci_desc
},
205 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x02,0x61,0x01 },
206 { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x18,0x01,0x01,0x00 },
207 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x02,0x02,0x06,0x00 },
208 mxf_write_cdci_desc
},
210 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x02,0x62,0x01 },
211 { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x18,0x01,0x01,0x00 },
212 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x02,0x02,0x07,0x00 },
213 mxf_write_cdci_desc
},
215 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x02,0x63,0x01 },
216 { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x18,0x01,0x01,0x00 },
217 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x02,0x02,0x08,0x00 },
218 mxf_write_cdci_desc
},
219 // DNxHD 1080p 10bit high
220 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x11,0x01,0x00 },
221 { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x15,0x01,0x05,0x00 },
222 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0A,0x04,0x01,0x02,0x02,0x71,0x01,0x00,0x00 },
223 mxf_write_cdci_desc
},
224 // DNxHD 1080p 8bit medium
225 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x11,0x01,0x00 },
226 { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x15,0x01,0x05,0x00 },
227 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0A,0x04,0x01,0x02,0x02,0x71,0x03,0x00,0x00 },
228 mxf_write_cdci_desc
},
229 // DNxHD 1080p 8bit high
230 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x11,0x01,0x00 },
231 { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x15,0x01,0x05,0x00 },
232 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0A,0x04,0x01,0x02,0x02,0x71,0x04,0x00,0x00 },
233 mxf_write_cdci_desc
},
234 // DNxHD 1080i 10bit high
235 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x11,0x01,0x00 },
236 { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x15,0x01,0x05,0x00 },
237 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0A,0x04,0x01,0x02,0x02,0x71,0x07,0x00,0x00 },
238 mxf_write_cdci_desc
},
239 // DNxHD 1080i 8bit medium
240 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x11,0x01,0x00 },
241 { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x15,0x01,0x05,0x00 },
242 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0A,0x04,0x01,0x02,0x02,0x71,0x08,0x00,0x00 },
243 mxf_write_cdci_desc
},
244 // DNxHD 1080i 8bit high
245 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x11,0x01,0x00 },
246 { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x15,0x01,0x05,0x00 },
247 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0A,0x04,0x01,0x02,0x02,0x71,0x09,0x00,0x00 },
248 mxf_write_cdci_desc
},
250 { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x01,0x0d,0x01,0x03,0x01,0x02,0x11,0x01,0x00 },
251 { 0x06,0x0e,0x2b,0x34,0x01,0x02,0x01,0x01,0x0d,0x01,0x03,0x01,0x15,0x01,0x05,0x00 },
252 { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x0A,0x04,0x01,0x02,0x02,0x71,0x10,0x00,0x00 },
253 mxf_write_cdci_desc
},
254 // DNxHD 720p 8bit high
255 { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x01,0x0d,0x01,0x03,0x01,0x02,0x11,0x01,0x00 },
256 { 0x06,0x0e,0x2b,0x34,0x01,0x02,0x01,0x01,0x0d,0x01,0x03,0x01,0x15,0x01,0x05,0x00 },
257 { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x0A,0x04,0x01,0x02,0x02,0x71,0x11,0x00,0x00 },
258 mxf_write_cdci_desc
},
259 // DNxHD 720p 8bit medium
260 { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x01,0x0d,0x01,0x03,0x01,0x02,0x11,0x01,0x00 },
261 { 0x06,0x0e,0x2b,0x34,0x01,0x02,0x01,0x01,0x0d,0x01,0x03,0x01,0x15,0x01,0x05,0x00 },
262 { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x0A,0x04,0x01,0x02,0x02,0x71,0x12,0x00,0x00 },
263 mxf_write_cdci_desc
},
264 // DNxHD 720p 8bit low
265 { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x01,0x0d,0x01,0x03,0x01,0x02,0x11,0x01,0x00 },
266 { 0x06,0x0e,0x2b,0x34,0x01,0x02,0x01,0x01,0x0d,0x01,0x03,0x01,0x15,0x01,0x05,0x00 },
267 { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x0A,0x04,0x01,0x02,0x02,0x71,0x13,0x00,0x00 },
268 mxf_write_cdci_desc
},
269 { { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 },
270 { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 },
271 { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 },
275 typedef struct MXFContext
{
277 int64_t footer_partition_offset
;
278 int essence_container_count
;
279 AVRational time_base
;
281 MXFIndexEntry
*index_entries
;
282 unsigned edit_units_count
;
283 uint64_t timestamp
; ///< timestamp, as year(16),month(8),day(8),hour(8),minutes(8),msec/4(8)
284 uint8_t slice_count
; ///< index slice count minus 1 (1 if no audio, 0 otherwise)
285 int last_indexed_edit_unit
;
286 uint64_t *body_partition_offset
;
287 unsigned body_partitions_count
;
288 int last_key_index
; ///< index of last key frame
290 AVTimecode tc
; ///< timecode context
291 AVStream
*timecode_track
;
292 int timecode_base
; ///< rounded time code base (25 or 30)
293 int edit_unit_byte_count
; ///< fixed edit unit byte count
294 uint64_t body_offset
;
295 uint32_t instance_number
;
296 uint8_t umid
[16]; ///< unique material identifier
300 static const uint8_t uuid_base
[] = { 0xAD,0xAB,0x44,0x24,0x2f,0x25,0x4d,0xc7,0x92,0xff,0x29,0xbd };
301 static const uint8_t umid_ul
[] = { 0x06,0x0A,0x2B,0x34,0x01,0x01,0x01,0x05,0x01,0x01,0x0D,0x00,0x13 };
304 * complete key for operation pattern, partitions, and primer pack
306 static const uint8_t op1a_ul
[] = { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x02,0x01,0x01,0x01,0x09,0x00 };
307 static const uint8_t footer_partition_key
[] = { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0D,0x01,0x02,0x01,0x01,0x04,0x04,0x00 }; // ClosedComplete
308 static const uint8_t primer_pack_key
[] = { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0D,0x01,0x02,0x01,0x01,0x05,0x01,0x00 };
309 static const uint8_t index_table_segment_key
[] = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x10,0x01,0x00 };
310 static const uint8_t random_index_pack_key
[] = { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0D,0x01,0x02,0x01,0x01,0x11,0x01,0x00 };
311 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
312 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
313 static const uint8_t klv_fill_key
[] = { 0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x03,0x01,0x02,0x10,0x01,0x00,0x00,0x00 };
314 static const uint8_t body_partition_key
[] = { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0D,0x01,0x02,0x01,0x01,0x03,0x04,0x00 }; // ClosedComplete
317 * partial key for header metadata
319 static const uint8_t header_metadata_key
[] = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0D,0x01,0x01,0x01,0x01 };
320 static const uint8_t multiple_desc_ul
[] = { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x0D,0x01,0x03,0x01,0x02,0x7F,0x01,0x00 };
323 * SMPTE RP210 http://www.smpte-ra.org/mdd/index.html
325 static const MXFLocalTagPair mxf_local_tag_batch
[] = {
327 { 0x3C0A, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x01,0x01,0x15,0x02,0x00,0x00,0x00,0x00}}, /* Instance UID */
328 { 0x3B02, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x10,0x02,0x04,0x00,0x00}}, /* Last Modified Date */
329 { 0x3B05, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x03,0x01,0x02,0x01,0x05,0x00,0x00,0x00}}, /* Version */
330 { 0x3B06, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x06,0x04,0x00,0x00}}, /* Identifications reference */
331 { 0x3B03, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x02,0x01,0x00,0x00}}, /* Content Storage reference */
332 { 0x3B09, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x01,0x02,0x02,0x03,0x00,0x00,0x00,0x00}}, /* Operational Pattern UL */
333 { 0x3B0A, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x01,0x02,0x02,0x10,0x02,0x01,0x00,0x00}}, /* Essence Containers UL batch */
334 { 0x3B0B, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x01,0x02,0x02,0x10,0x02,0x02,0x00,0x00}}, /* DM Schemes UL batch */
336 { 0x3C09, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x20,0x07,0x01,0x01,0x00,0x00,0x00}}, /* This Generation UID */
337 { 0x3C01, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x20,0x07,0x01,0x02,0x01,0x00,0x00}}, /* Company Name */
338 { 0x3C02, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x20,0x07,0x01,0x03,0x01,0x00,0x00}}, /* Product Name */
339 { 0x3C04, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x20,0x07,0x01,0x05,0x01,0x00,0x00}}, /* Version String */
340 { 0x3C05, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x20,0x07,0x01,0x07,0x00,0x00,0x00}}, /* Product ID */
341 { 0x3C06, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x10,0x02,0x03,0x00,0x00}}, /* Modification Date */
343 { 0x1901, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x05,0x01,0x00,0x00}}, /* Package strong reference batch */
344 { 0x1902, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x05,0x02,0x00,0x00}}, /* Package strong reference batch */
345 // Essence Container Data
346 { 0x2701, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x06,0x01,0x00,0x00,0x00}}, /* Linked Package UID */
347 { 0x3F07, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x01,0x03,0x04,0x04,0x00,0x00,0x00,0x00}}, /* BodySID */
349 { 0x4401, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x01,0x01,0x15,0x10,0x00,0x00,0x00,0x00}}, /* Package UID */
350 { 0x4405, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x10,0x01,0x03,0x00,0x00}}, /* Package Creation Date */
351 { 0x4404, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x10,0x02,0x05,0x00,0x00}}, /* Package Modified Date */
352 { 0x4403, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x06,0x05,0x00,0x00}}, /* Tracks Strong reference array */
353 { 0x4701, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x02,0x03,0x00,0x00}}, /* Descriptor */
355 { 0x4801, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x01,0x07,0x01,0x01,0x00,0x00,0x00,0x00}}, /* Track ID */
356 { 0x4804, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x01,0x04,0x01,0x03,0x00,0x00,0x00,0x00}}, /* Track Number */
357 { 0x4B01, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x30,0x04,0x05,0x00,0x00,0x00,0x00}}, /* Edit Rate */
358 { 0x4B02, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x03,0x01,0x03,0x00,0x00}}, /* Origin */
359 { 0x4803, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x02,0x04,0x00,0x00}}, /* Sequence reference */
361 { 0x0201, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x07,0x01,0x00,0x00,0x00,0x00,0x00}}, /* Data Definition UL */
362 { 0x0202, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x02,0x01,0x01,0x03,0x00,0x00}}, /* Duration */
363 { 0x1001, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x06,0x09,0x00,0x00}}, /* Structural Components reference array */
365 { 0x1201, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x03,0x01,0x04,0x00,0x00}}, /* Start position */
366 { 0x1101, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x03,0x01,0x00,0x00,0x00}}, /* SourcePackageID */
367 { 0x1102, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x03,0x02,0x00,0x00,0x00}}, /* SourceTrackID */
368 // Timecode Component
369 { 0x1501, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x03,0x01,0x05,0x00,0x00}}, /* Start Time Code */
370 { 0x1502, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x04,0x01,0x01,0x02,0x06,0x00,0x00}}, /* Rounded Time Code Base */
371 { 0x1503, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x04,0x01,0x01,0x05,0x00,0x00,0x00}}, /* Drop Frame */
373 { 0x3F01, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x06,0x01,0x01,0x04,0x06,0x0B,0x00,0x00}}, /* Sub Descriptors reference array */
374 { 0x3006, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x06,0x01,0x01,0x03,0x05,0x00,0x00,0x00}}, /* Linked Track ID */
375 { 0x3001, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x06,0x01,0x01,0x00,0x00,0x00,0x00}}, /* SampleRate */
376 { 0x3004, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x01,0x02,0x00,0x00}}, /* Essence Container */
377 // Generic Picture Essence Descriptor
378 { 0x320C, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x03,0x01,0x04,0x00,0x00,0x00}}, /* Frame Layout */
379 { 0x320D, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x01,0x03,0x02,0x05,0x00,0x00,0x00}}, /* Video Line Map */
380 { 0x3203, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x02,0x02,0x00,0x00,0x00}}, /* Stored Width */
381 { 0x3202, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x02,0x01,0x00,0x00,0x00}}, /* Stored Height */
382 { 0x3209, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x01,0x0C,0x00,0x00,0x00}}, /* Display Width */
383 { 0x3208, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x01,0x0B,0x00,0x00,0x00}}, /* Display Height */
384 { 0x320E, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x01,0x01,0x01,0x00,0x00,0x00}}, /* Aspect Ratio */
385 { 0x3201, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x01,0x06,0x01,0x00,0x00,0x00,0x00}}, /* Picture Essence Coding */
386 { 0x3212, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x01,0x03,0x01,0x06,0x00,0x00,0x00}}, /* Field Dominance (Opt) */
387 // CDCI Picture Essence Descriptor
388 { 0x3301, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x01,0x05,0x03,0x0A,0x00,0x00,0x00}}, /* Component Depth */
389 { 0x3302, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x01,0x05,0x00,0x00,0x00}}, /* Horizontal Subsampling */
390 // Generic Sound Essence Descriptor
391 { 0x3D02, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x04,0x02,0x03,0x01,0x04,0x00,0x00,0x00}}, /* Locked/Unlocked */
392 { 0x3D03, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x02,0x03,0x01,0x01,0x01,0x00,0x00}}, /* Audio sampling rate */
393 { 0x3D07, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x02,0x01,0x01,0x04,0x00,0x00,0x00}}, /* ChannelCount */
394 { 0x3D01, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x04,0x02,0x03,0x03,0x04,0x00,0x00,0x00}}, /* Quantization bits */
395 { 0x3D06, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x02,0x04,0x02,0x00,0x00,0x00,0x00}}, /* Sound Essence Compression */
396 // Index Table Segment
397 { 0x3F0B, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x05,0x30,0x04,0x06,0x00,0x00,0x00,0x00}}, /* Index Edit Rate */
398 { 0x3F0C, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x07,0x02,0x01,0x03,0x01,0x0A,0x00,0x00}}, /* Index Start Position */
399 { 0x3F0D, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x07,0x02,0x02,0x01,0x01,0x02,0x00,0x00}}, /* Index Duration */
400 { 0x3F05, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x04,0x06,0x02,0x01,0x00,0x00,0x00,0x00}}, /* Edit Unit Byte Count */
401 { 0x3F06, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x01,0x03,0x04,0x05,0x00,0x00,0x00,0x00}}, /* IndexSID */
402 { 0x3F08, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x04,0x04,0x04,0x01,0x01,0x00,0x00,0x00}}, /* Slice Count */
403 { 0x3F09, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x04,0x04,0x01,0x06,0x00,0x00,0x00}}, /* Delta Entry Array */
404 { 0x3F0A, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x04,0x04,0x02,0x05,0x00,0x00,0x00}}, /* Index Entry Array */
405 // MPEG video Descriptor
406 { 0x8000, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x01,0x06,0x02,0x01,0x0B,0x00,0x00}}, /* BitRate */
407 { 0x8007, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x01,0x06,0x02,0x01,0x0A,0x00,0x00}}, /* ProfileAndLevel */
408 // Wave Audio Essence Descriptor
409 { 0x3D09, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x02,0x03,0x03,0x05,0x00,0x00,0x00}}, /* Average Bytes Per Second */
410 { 0x3D0A, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x02,0x03,0x02,0x01,0x00,0x00,0x00}}, /* Block Align */
413 static void mxf_write_uuid(AVIOContext
*pb
, enum MXFMetadataSetType type
, int value
)
415 avio_write(pb
, uuid_base
, 12);
417 avio_wb16(pb
, value
);
420 static void mxf_write_umid(AVFormatContext
*s
, int type
)
422 MXFContext
*mxf
= s
->priv_data
;
423 avio_write(s
->pb
, umid_ul
, 13);
424 avio_wb24(s
->pb
, mxf
->instance_number
);
425 avio_write(s
->pb
, mxf
->umid
, 15);
426 avio_w8(s
->pb
, type
);
429 static void mxf_write_refs_count(AVIOContext
*pb
, int ref_count
)
431 avio_wb32(pb
, ref_count
);
435 static int klv_ber_length(uint64_t len
)
440 return (av_log2(len
) >> 3) + 2;
443 static int klv_encode_ber_length(AVIOContext
*pb
, uint64_t len
)
445 // Determine the best BER size
453 size
= (av_log2(len
) >> 3) + 1;
456 avio_w8(pb
, 0x80 + size
);
459 avio_w8(pb
, len
>> 8 * size
& 0xff);
464 static void klv_encode_ber4_length(AVIOContext
*pb
, int len
)
466 avio_w8(pb
, 0x80 + 3);
471 * Get essence container ul index
473 static int mxf_get_essence_container_ul_index(enum AVCodecID id
)
476 for (i
= 0; mxf_essence_mappings
[i
].id
; i
++)
477 if (mxf_essence_mappings
[i
].id
== id
)
478 return mxf_essence_mappings
[i
].index
;
482 static void mxf_write_primer_pack(AVFormatContext
*s
)
484 AVIOContext
*pb
= s
->pb
;
485 int local_tag_number
, i
= 0;
487 local_tag_number
= FF_ARRAY_ELEMS(mxf_local_tag_batch
);
489 avio_write(pb
, primer_pack_key
, 16);
490 klv_encode_ber_length(pb
, local_tag_number
* 18 + 8);
492 avio_wb32(pb
, local_tag_number
); // local_tag num
493 avio_wb32(pb
, 18); // item size, always 18 according to the specs
495 for (i
= 0; i
< local_tag_number
; i
++) {
496 avio_wb16(pb
, mxf_local_tag_batch
[i
].local_tag
);
497 avio_write(pb
, mxf_local_tag_batch
[i
].uid
, 16);
501 static void mxf_write_local_tag(AVIOContext
*pb
, int size
, int tag
)
507 static void mxf_write_metadata_key(AVIOContext
*pb
, unsigned int value
)
509 avio_write(pb
, header_metadata_key
, 13);
510 avio_wb24(pb
, value
);
513 static void mxf_free(AVFormatContext
*s
)
517 for (i
= 0; i
< s
->nb_streams
; i
++) {
518 AVStream
*st
= s
->streams
[i
];
519 av_freep(&st
->priv_data
);
523 static const MXFCodecUL
*mxf_get_data_definition_ul(int type
)
525 const MXFCodecUL
*uls
= ff_mxf_data_definition_uls
;
526 while (uls
->uid
[0]) {
534 //one EC -> one descriptor. N ECs -> MultipleDescriptor + N descriptors
535 #define DESCRIPTOR_COUNT(essence_container_count) \
536 (essence_container_count > 1 ? essence_container_count + 1 : essence_container_count)
538 static void mxf_write_essence_container_refs(AVFormatContext
*s
)
540 MXFContext
*c
= s
->priv_data
;
541 AVIOContext
*pb
= s
->pb
;
544 mxf_write_refs_count(pb
, DESCRIPTOR_COUNT(c
->essence_container_count
));
545 av_log(s
,AV_LOG_DEBUG
, "essence container count:%d\n", c
->essence_container_count
);
546 for (i
= 0; i
< c
->essence_container_count
; i
++) {
547 MXFStreamContext
*sc
= s
->streams
[i
]->priv_data
;
548 avio_write(pb
, mxf_essence_container_uls
[sc
->index
].container_ul
, 16);
551 if (c
->essence_container_count
> 1)
552 avio_write(pb
, multiple_desc_ul
, 16);
555 static void mxf_write_preface(AVFormatContext
*s
)
557 MXFContext
*mxf
= s
->priv_data
;
558 AVIOContext
*pb
= s
->pb
;
560 mxf_write_metadata_key(pb
, 0x012f00);
561 PRINT_KEY(s
, "preface key", pb
->buf_ptr
- 16);
562 klv_encode_ber_length(pb
, 130 + 16LL * DESCRIPTOR_COUNT(mxf
->essence_container_count
));
564 // write preface set uid
565 mxf_write_local_tag(pb
, 16, 0x3C0A);
566 mxf_write_uuid(pb
, Preface
, 0);
567 PRINT_KEY(s
, "preface uid", pb
->buf_ptr
- 16);
569 // last modified date
570 mxf_write_local_tag(pb
, 8, 0x3B02);
571 avio_wb64(pb
, mxf
->timestamp
);
574 mxf_write_local_tag(pb
, 2, 0x3B05);
575 avio_wb16(pb
, 258); // v1.2
577 // write identification_refs
578 mxf_write_local_tag(pb
, 16 + 8, 0x3B06);
579 mxf_write_refs_count(pb
, 1);
580 mxf_write_uuid(pb
, Identification
, 0);
582 // write content_storage_refs
583 mxf_write_local_tag(pb
, 16, 0x3B03);
584 mxf_write_uuid(pb
, ContentStorage
, 0);
586 // operational pattern
587 mxf_write_local_tag(pb
, 16, 0x3B09);
588 avio_write(pb
, op1a_ul
, 16);
590 // write essence_container_refs
591 mxf_write_local_tag(pb
, 8 + 16LL * DESCRIPTOR_COUNT(mxf
->essence_container_count
), 0x3B0A);
592 mxf_write_essence_container_refs(s
);
594 // write dm_scheme_refs
595 mxf_write_local_tag(pb
, 8, 0x3B0B);
600 * Write a local tag containing an ascii string as utf-16
602 static void mxf_write_local_tag_utf16(AVIOContext
*pb
, int tag
, const char *value
)
604 int i
, size
= strlen(value
);
605 mxf_write_local_tag(pb
, size
*2, tag
);
606 for (i
= 0; i
< size
; i
++)
607 avio_wb16(pb
, value
[i
]);
610 static void mxf_write_identification(AVFormatContext
*s
)
612 MXFContext
*mxf
= s
->priv_data
;
613 AVIOContext
*pb
= s
->pb
;
614 const char *company
= "FFmpeg";
615 const char *product
= "OP1a Muxer";
619 mxf_write_metadata_key(pb
, 0x013000);
620 PRINT_KEY(s
, "identification key", pb
->buf_ptr
- 16);
622 version
= s
->flags
& AVFMT_FLAG_BITEXACT
?
623 "0.0.0" : AV_STRINGIFY(LIBAVFORMAT_VERSION
);
624 length
= 84 + (strlen(company
)+strlen(product
)+strlen(version
))*2; // utf-16
625 klv_encode_ber_length(pb
, length
);
628 mxf_write_local_tag(pb
, 16, 0x3C0A);
629 mxf_write_uuid(pb
, Identification
, 0);
630 PRINT_KEY(s
, "identification uid", pb
->buf_ptr
- 16);
632 // write generation uid
633 mxf_write_local_tag(pb
, 16, 0x3C09);
634 mxf_write_uuid(pb
, Identification
, 1);
636 mxf_write_local_tag_utf16(pb
, 0x3C01, company
); // Company Name
637 mxf_write_local_tag_utf16(pb
, 0x3C02, product
); // Product Name
638 mxf_write_local_tag_utf16(pb
, 0x3C04, version
); // Version String
641 mxf_write_local_tag(pb
, 16, 0x3C05);
642 mxf_write_uuid(pb
, Identification
, 2);
645 mxf_write_local_tag(pb
, 8, 0x3C06);
646 avio_wb64(pb
, mxf
->timestamp
);
649 static void mxf_write_content_storage(AVFormatContext
*s
)
651 AVIOContext
*pb
= s
->pb
;
653 mxf_write_metadata_key(pb
, 0x011800);
654 PRINT_KEY(s
, "content storage key", pb
->buf_ptr
- 16);
655 klv_encode_ber_length(pb
, 92);
658 mxf_write_local_tag(pb
, 16, 0x3C0A);
659 mxf_write_uuid(pb
, ContentStorage
, 0);
660 PRINT_KEY(s
, "content storage uid", pb
->buf_ptr
- 16);
662 // write package reference
663 mxf_write_local_tag(pb
, 16 * 2 + 8, 0x1901);
664 mxf_write_refs_count(pb
, 2);
665 mxf_write_uuid(pb
, MaterialPackage
, 0);
666 mxf_write_uuid(pb
, SourcePackage
, 0);
668 // write essence container data
669 mxf_write_local_tag(pb
, 8 + 16, 0x1902);
670 mxf_write_refs_count(pb
, 1);
671 mxf_write_uuid(pb
, EssenceContainerData
, 0);
674 static void mxf_write_track(AVFormatContext
*s
, AVStream
*st
, enum MXFMetadataSetType type
)
676 MXFContext
*mxf
= s
->priv_data
;
677 AVIOContext
*pb
= s
->pb
;
678 MXFStreamContext
*sc
= st
->priv_data
;
680 mxf_write_metadata_key(pb
, 0x013b00);
681 PRINT_KEY(s
, "track key", pb
->buf_ptr
- 16);
682 klv_encode_ber_length(pb
, 80);
685 mxf_write_local_tag(pb
, 16, 0x3C0A);
686 mxf_write_uuid(pb
, type
== MaterialPackage
? Track
: Track
+ TypeBottom
, st
->index
);
687 PRINT_KEY(s
, "track uid", pb
->buf_ptr
- 16);
690 mxf_write_local_tag(pb
, 4, 0x4801);
691 avio_wb32(pb
, st
->index
+2);
693 // write track number
694 mxf_write_local_tag(pb
, 4, 0x4804);
695 if (type
== MaterialPackage
)
696 avio_wb32(pb
, 0); // track number of material package is 0
698 avio_write(pb
, sc
->track_essence_element_key
+ 12, 4);
700 mxf_write_local_tag(pb
, 8, 0x4B01);
701 avio_wb32(pb
, mxf
->time_base
.den
);
702 avio_wb32(pb
, mxf
->time_base
.num
);
705 mxf_write_local_tag(pb
, 8, 0x4B02);
708 // write sequence refs
709 mxf_write_local_tag(pb
, 16, 0x4803);
710 mxf_write_uuid(pb
, type
== MaterialPackage
? Sequence
: Sequence
+ TypeBottom
, st
->index
);
713 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 };
715 static void mxf_write_common_fields(AVFormatContext
*s
, AVStream
*st
)
717 MXFContext
*mxf
= s
->priv_data
;
718 AVIOContext
*pb
= s
->pb
;
720 // find data define uls
721 mxf_write_local_tag(pb
, 16, 0x0201);
722 if (st
== mxf
->timecode_track
)
723 avio_write(pb
, smpte_12m_timecode_track_data_ul
, 16);
725 const MXFCodecUL
*data_def_ul
= mxf_get_data_definition_ul(st
->codec
->codec_type
);
726 avio_write(pb
, data_def_ul
->uid
, 16);
730 mxf_write_local_tag(pb
, 8, 0x0202);
731 avio_wb64(pb
, mxf
->duration
);
734 static void mxf_write_sequence(AVFormatContext
*s
, AVStream
*st
, enum MXFMetadataSetType type
)
736 MXFContext
*mxf
= s
->priv_data
;
737 AVIOContext
*pb
= s
->pb
;
738 enum MXFMetadataSetType component
;
740 mxf_write_metadata_key(pb
, 0x010f00);
741 PRINT_KEY(s
, "sequence key", pb
->buf_ptr
- 16);
742 klv_encode_ber_length(pb
, 80);
744 mxf_write_local_tag(pb
, 16, 0x3C0A);
745 mxf_write_uuid(pb
, type
== MaterialPackage
? Sequence
: Sequence
+ TypeBottom
, st
->index
);
747 PRINT_KEY(s
, "sequence uid", pb
->buf_ptr
- 16);
748 mxf_write_common_fields(s
, st
);
750 // write structural component
751 mxf_write_local_tag(pb
, 16 + 8, 0x1001);
752 mxf_write_refs_count(pb
, 1);
753 if (st
== mxf
->timecode_track
)
754 component
= TimecodeComponent
;
756 component
= SourceClip
;
757 if (type
== SourcePackage
)
758 component
+= TypeBottom
;
759 mxf_write_uuid(pb
, component
, st
->index
);
762 static void mxf_write_timecode_component(AVFormatContext
*s
, AVStream
*st
, enum MXFMetadataSetType type
)
764 MXFContext
*mxf
= s
->priv_data
;
765 AVIOContext
*pb
= s
->pb
;
767 mxf_write_metadata_key(pb
, 0x011400);
768 klv_encode_ber_length(pb
, 75);
771 mxf_write_local_tag(pb
, 16, 0x3C0A);
772 mxf_write_uuid(pb
, type
== MaterialPackage
? TimecodeComponent
:
773 TimecodeComponent
+ TypeBottom
, st
->index
);
775 mxf_write_common_fields(s
, st
);
778 mxf_write_local_tag(pb
, 8, 0x1501);
779 avio_wb64(pb
, mxf
->tc
.start
);
781 // Rounded Time Code Base
782 mxf_write_local_tag(pb
, 2, 0x1502);
783 avio_wb16(pb
, mxf
->timecode_base
);
786 mxf_write_local_tag(pb
, 1, 0x1503);
787 avio_w8(pb
, !!(mxf
->tc
.flags
& AV_TIMECODE_FLAG_DROPFRAME
));
790 static void mxf_write_structural_component(AVFormatContext
*s
, AVStream
*st
, enum MXFMetadataSetType type
)
792 AVIOContext
*pb
= s
->pb
;
795 mxf_write_metadata_key(pb
, 0x011100);
796 PRINT_KEY(s
, "sturctural component key", pb
->buf_ptr
- 16);
797 klv_encode_ber_length(pb
, 108);
800 mxf_write_local_tag(pb
, 16, 0x3C0A);
801 mxf_write_uuid(pb
, type
== MaterialPackage
? SourceClip
: SourceClip
+ TypeBottom
, st
->index
);
803 PRINT_KEY(s
, "structural component uid", pb
->buf_ptr
- 16);
804 mxf_write_common_fields(s
, st
);
806 // write start_position
807 mxf_write_local_tag(pb
, 8, 0x1201);
810 // write source package uid, end of the reference
811 mxf_write_local_tag(pb
, 32, 0x1101);
812 if (type
== SourcePackage
) {
813 for (i
= 0; i
< 4; i
++)
816 mxf_write_umid(s
, 1);
818 // write source track id
819 mxf_write_local_tag(pb
, 4, 0x1102);
820 if (type
== SourcePackage
)
823 avio_wb32(pb
, st
->index
+2);
826 static void mxf_write_multi_descriptor(AVFormatContext
*s
)
828 MXFContext
*mxf
= s
->priv_data
;
829 AVIOContext
*pb
= s
->pb
;
833 mxf_write_metadata_key(pb
, 0x014400);
834 PRINT_KEY(s
, "multiple descriptor key", pb
->buf_ptr
- 16);
835 klv_encode_ber_length(pb
, 64 + 16LL * s
->nb_streams
);
837 mxf_write_local_tag(pb
, 16, 0x3C0A);
838 mxf_write_uuid(pb
, MultipleDescriptor
, 0);
839 PRINT_KEY(s
, "multi_desc uid", pb
->buf_ptr
- 16);
842 mxf_write_local_tag(pb
, 8, 0x3001);
843 avio_wb32(pb
, mxf
->time_base
.den
);
844 avio_wb32(pb
, mxf
->time_base
.num
);
846 // write essence container ul
847 mxf_write_local_tag(pb
, 16, 0x3004);
848 if (mxf
->essence_container_count
> 1)
849 ul
= multiple_desc_ul
;
851 MXFStreamContext
*sc
= s
->streams
[0]->priv_data
;
852 ul
= mxf_essence_container_uls
[sc
->index
].container_ul
;
854 avio_write(pb
, ul
, 16);
856 // write sub descriptor refs
857 mxf_write_local_tag(pb
, s
->nb_streams
* 16 + 8, 0x3F01);
858 mxf_write_refs_count(pb
, s
->nb_streams
);
859 for (i
= 0; i
< s
->nb_streams
; i
++)
860 mxf_write_uuid(pb
, SubDescriptor
, i
);
863 static void mxf_write_generic_desc(AVFormatContext
*s
, AVStream
*st
, const UID key
, unsigned size
)
865 MXFContext
*mxf
= s
->priv_data
;
866 MXFStreamContext
*sc
= st
->priv_data
;
867 AVIOContext
*pb
= s
->pb
;
869 avio_write(pb
, key
, 16);
870 klv_encode_ber4_length(pb
, size
+20+8+12+20);
872 mxf_write_local_tag(pb
, 16, 0x3C0A);
873 mxf_write_uuid(pb
, SubDescriptor
, st
->index
);
875 mxf_write_local_tag(pb
, 4, 0x3006);
876 avio_wb32(pb
, st
->index
+2);
878 mxf_write_local_tag(pb
, 8, 0x3001);
879 avio_wb32(pb
, mxf
->time_base
.den
);
880 avio_wb32(pb
, mxf
->time_base
.num
);
882 mxf_write_local_tag(pb
, 16, 0x3004);
883 avio_write(pb
, mxf_essence_container_uls
[sc
->index
].container_ul
, 16);
886 static const UID mxf_mpegvideo_descriptor_key
= { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x51,0x00 };
887 static const UID mxf_wav_descriptor_key
= { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x48,0x00 };
888 static const UID mxf_aes3_descriptor_key
= { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x47,0x00 };
889 static const UID mxf_cdci_descriptor_key
= { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0D,0x01,0x01,0x01,0x01,0x01,0x28,0x00 };
890 static const UID mxf_generic_sound_descriptor_key
= { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0D,0x01,0x01,0x01,0x01,0x01,0x42,0x00 };
892 static void mxf_write_cdci_common(AVFormatContext
*s
, AVStream
*st
, const UID key
, unsigned size
)
894 MXFStreamContext
*sc
= st
->priv_data
;
895 AVIOContext
*pb
= s
->pb
;
896 int stored_height
= (st
->codec
->height
+15)/16*16;
899 unsigned desc_size
= size
+8+8+8+8+8+8+5+16+sc
->interlaced
*4+12+20;
900 if (sc
->interlaced
&& sc
->field_dominance
)
903 mxf_write_generic_desc(s
, st
, key
, desc_size
);
905 mxf_write_local_tag(pb
, 4, 0x3203);
906 avio_wb32(pb
, st
->codec
->width
);
908 mxf_write_local_tag(pb
, 4, 0x3202);
909 avio_wb32(pb
, stored_height
>>sc
->interlaced
);
911 mxf_write_local_tag(pb
, 4, 0x3209);
912 avio_wb32(pb
, st
->codec
->width
);
914 if (st
->codec
->height
== 608) // PAL + VBI
915 display_height
= 576;
916 else if (st
->codec
->height
== 512) // NTSC + VBI
917 display_height
= 486;
919 display_height
= st
->codec
->height
;
921 mxf_write_local_tag(pb
, 4, 0x3208);
922 avio_wb32(pb
, display_height
>>sc
->interlaced
);
925 mxf_write_local_tag(pb
, 4, 0x3301);
926 avio_wb32(pb
, sc
->component_depth
);
928 // horizontal subsampling
929 mxf_write_local_tag(pb
, 4, 0x3302);
933 mxf_write_local_tag(pb
, 1, 0x320C);
934 avio_w8(pb
, sc
->interlaced
);
937 switch (st
->codec
->height
) {
938 case 576: f1
= 23; f2
= st
->codec
->codec_id
== AV_CODEC_ID_DVVIDEO
? 335 : 336; break;
939 case 608: f1
= 7; f2
= 320; break;
940 case 480: f1
= 20; f2
= st
->codec
->codec_id
== AV_CODEC_ID_DVVIDEO
? 285 : 283; break;
941 case 512: f1
= 7; f2
= 270; break;
942 case 720: f1
= 26; f2
= 0; break; // progressive
943 case 1080: f1
= 21; f2
= 584; break;
944 default: f1
= 0; f2
= 0; break;
947 if (!sc
->interlaced
) {
952 mxf_write_local_tag(pb
, 12+sc
->interlaced
*4, 0x320D);
953 avio_wb32(pb
, sc
->interlaced
? 2 : 1);
959 mxf_write_local_tag(pb
, 8, 0x320E);
960 avio_wb32(pb
, sc
->aspect_ratio
.num
);
961 avio_wb32(pb
, sc
->aspect_ratio
.den
);
963 mxf_write_local_tag(pb
, 16, 0x3201);
964 avio_write(pb
, *sc
->codec_ul
, 16);
966 if (sc
->interlaced
&& sc
->field_dominance
) {
967 mxf_write_local_tag(pb
, 1, 0x3212);
968 avio_w8(pb
, sc
->field_dominance
);
973 static void mxf_write_cdci_desc(AVFormatContext
*s
, AVStream
*st
)
975 mxf_write_cdci_common(s
, st
, mxf_cdci_descriptor_key
, 0);
978 static void mxf_write_mpegvideo_desc(AVFormatContext
*s
, AVStream
*st
)
980 AVIOContext
*pb
= s
->pb
;
981 MXFStreamContext
*sc
= st
->priv_data
;
982 int profile_and_level
= (st
->codec
->profile
<<4) | st
->codec
->level
;
984 mxf_write_cdci_common(s
, st
, mxf_mpegvideo_descriptor_key
, 8+5);
987 mxf_write_local_tag(pb
, 4, 0x8000);
988 avio_wb32(pb
, sc
->video_bit_rate
);
991 mxf_write_local_tag(pb
, 1, 0x8007);
992 if (!st
->codec
->profile
)
993 profile_and_level
|= 0x80; // escape bit
994 avio_w8(pb
, profile_and_level
);
997 static void mxf_write_generic_sound_common(AVFormatContext
*s
, AVStream
*st
, const UID key
, unsigned size
)
999 AVIOContext
*pb
= s
->pb
;
1000 MXFContext
*mxf
= s
->priv_data
;
1001 int show_warnings
= !mxf
->footer_partition_offset
;
1003 mxf_write_generic_desc(s
, st
, key
, size
+5+12+8+8);
1006 mxf_write_local_tag(pb
, 1, 0x3D02);
1009 // write audio sampling rate
1010 mxf_write_local_tag(pb
, 8, 0x3D03);
1011 avio_wb32(pb
, st
->codec
->sample_rate
);
1014 mxf_write_local_tag(pb
, 4, 0x3D07);
1015 if (mxf
->channel_count
== -1) {
1016 if (show_warnings
&& (s
->oformat
== &ff_mxf_d10_muxer
) && (st
->codec
->channels
!= 4) && (st
->codec
->channels
!= 8))
1017 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");
1018 avio_wb32(pb
, st
->codec
->channels
);
1019 } else if (s
->oformat
== &ff_mxf_d10_muxer
) {
1020 if (show_warnings
&& (mxf
->channel_count
< st
->codec
->channels
))
1021 av_log(s
, AV_LOG_WARNING
, "d10_channelcount < actual number of audio channels : some channels will be discarded\n");
1022 if (show_warnings
&& (mxf
->channel_count
!= 4) && (mxf
->channel_count
!= 8))
1023 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");
1024 avio_wb32(pb
, mxf
->channel_count
);
1026 if (show_warnings
&& mxf
->channel_count
!= -1)
1027 av_log(s
, AV_LOG_ERROR
, "-d10_channelcount requires MXF D-10 and will be ignored\n");
1028 avio_wb32(pb
, st
->codec
->channels
);
1031 mxf_write_local_tag(pb
, 4, 0x3D01);
1032 avio_wb32(pb
, av_get_bits_per_sample(st
->codec
->codec_id
));
1035 static void mxf_write_wav_common(AVFormatContext
*s
, AVStream
*st
, const UID key
, unsigned size
)
1037 AVIOContext
*pb
= s
->pb
;
1039 mxf_write_generic_sound_common(s
, st
, key
, size
+6+8);
1041 mxf_write_local_tag(pb
, 2, 0x3D0A);
1042 avio_wb16(pb
, st
->codec
->block_align
);
1044 // avg bytes per sec
1045 mxf_write_local_tag(pb
, 4, 0x3D09);
1046 avio_wb32(pb
, st
->codec
->block_align
*st
->codec
->sample_rate
);
1049 static void mxf_write_wav_desc(AVFormatContext
*s
, AVStream
*st
)
1051 mxf_write_wav_common(s
, st
, mxf_wav_descriptor_key
, 0);
1054 static void mxf_write_aes3_desc(AVFormatContext
*s
, AVStream
*st
)
1056 mxf_write_wav_common(s
, st
, mxf_aes3_descriptor_key
, 0);
1059 static void mxf_write_generic_sound_desc(AVFormatContext
*s
, AVStream
*st
)
1061 mxf_write_generic_sound_common(s
, st
, mxf_generic_sound_descriptor_key
, 0);
1064 static void mxf_write_package(AVFormatContext
*s
, enum MXFMetadataSetType type
)
1066 MXFContext
*mxf
= s
->priv_data
;
1067 AVIOContext
*pb
= s
->pb
;
1068 int i
, track_count
= s
->nb_streams
+1;
1070 if (type
== MaterialPackage
) {
1071 mxf_write_metadata_key(pb
, 0x013600);
1072 PRINT_KEY(s
, "Material Package key", pb
->buf_ptr
- 16);
1073 klv_encode_ber_length(pb
, 92 + 16*track_count
);
1075 mxf_write_metadata_key(pb
, 0x013700);
1076 PRINT_KEY(s
, "Source Package key", pb
->buf_ptr
- 16);
1077 klv_encode_ber_length(pb
, 112 + 16*track_count
); // 20 bytes length for descriptor reference
1081 mxf_write_local_tag(pb
, 16, 0x3C0A);
1082 mxf_write_uuid(pb
, type
, 0);
1083 av_log(s
,AV_LOG_DEBUG
, "package type:%d\n", type
);
1084 PRINT_KEY(s
, "package uid", pb
->buf_ptr
- 16);
1086 // write package umid
1087 mxf_write_local_tag(pb
, 32, 0x4401);
1088 mxf_write_umid(s
, type
== SourcePackage
);
1089 PRINT_KEY(s
, "package umid second part", pb
->buf_ptr
- 16);
1091 // package creation date
1092 mxf_write_local_tag(pb
, 8, 0x4405);
1093 avio_wb64(pb
, mxf
->timestamp
);
1095 // package modified date
1096 mxf_write_local_tag(pb
, 8, 0x4404);
1097 avio_wb64(pb
, mxf
->timestamp
);
1100 mxf_write_local_tag(pb
, track_count
*16 + 8, 0x4403);
1101 mxf_write_refs_count(pb
, track_count
);
1102 mxf_write_uuid(pb
, type
== MaterialPackage
? Track
:
1103 Track
+ TypeBottom
, -1); // timecode track
1104 for (i
= 0; i
< s
->nb_streams
; i
++)
1105 mxf_write_uuid(pb
, type
== MaterialPackage
? Track
: Track
+ TypeBottom
, i
);
1107 // write multiple descriptor reference
1108 if (type
== SourcePackage
) {
1109 mxf_write_local_tag(pb
, 16, 0x4701);
1110 if (s
->nb_streams
> 1) {
1111 mxf_write_uuid(pb
, MultipleDescriptor
, 0);
1112 mxf_write_multi_descriptor(s
);
1114 mxf_write_uuid(pb
, SubDescriptor
, 0);
1117 // write timecode track
1118 mxf_write_track(s
, mxf
->timecode_track
, type
);
1119 mxf_write_sequence(s
, mxf
->timecode_track
, type
);
1120 mxf_write_timecode_component(s
, mxf
->timecode_track
, type
);
1122 for (i
= 0; i
< s
->nb_streams
; i
++) {
1123 AVStream
*st
= s
->streams
[i
];
1124 mxf_write_track(s
, st
, type
);
1125 mxf_write_sequence(s
, st
, type
);
1126 mxf_write_structural_component(s
, st
, type
);
1128 if (type
== SourcePackage
) {
1129 MXFStreamContext
*sc
= st
->priv_data
;
1130 mxf_essence_container_uls
[sc
->index
].write_desc(s
, st
);
1135 static int mxf_write_essence_container_data(AVFormatContext
*s
)
1137 AVIOContext
*pb
= s
->pb
;
1139 mxf_write_metadata_key(pb
, 0x012300);
1140 klv_encode_ber_length(pb
, 72);
1142 mxf_write_local_tag(pb
, 16, 0x3C0A); // Instance UID
1143 mxf_write_uuid(pb
, EssenceContainerData
, 0);
1145 mxf_write_local_tag(pb
, 32, 0x2701); // Linked Package UID
1146 mxf_write_umid(s
, 1);
1148 mxf_write_local_tag(pb
, 4, 0x3F07); // BodySID
1151 mxf_write_local_tag(pb
, 4, 0x3F06); // IndexSID
1157 static int mxf_write_header_metadata_sets(AVFormatContext
*s
)
1159 mxf_write_preface(s
);
1160 mxf_write_identification(s
);
1161 mxf_write_content_storage(s
);
1162 mxf_write_package(s
, MaterialPackage
);
1163 mxf_write_package(s
, SourcePackage
);
1164 mxf_write_essence_container_data(s
);
1168 static unsigned klv_fill_size(uint64_t size
)
1170 unsigned pad
= KAG_SIZE
- (size
& (KAG_SIZE
-1));
1171 if (pad
< 20) // smallest fill item possible
1172 return pad
+ KAG_SIZE
;
1174 return pad
& (KAG_SIZE
-1);
1177 static void mxf_write_index_table_segment(AVFormatContext
*s
)
1179 MXFContext
*mxf
= s
->priv_data
;
1180 AVIOContext
*pb
= s
->pb
;
1181 int i
, j
, temporal_reordering
= 0;
1182 int key_index
= mxf
->last_key_index
;
1184 av_log(s
, AV_LOG_DEBUG
, "edit units count %d\n", mxf
->edit_units_count
);
1186 if (!mxf
->edit_units_count
&& !mxf
->edit_unit_byte_count
)
1189 avio_write(pb
, index_table_segment_key
, 16);
1191 if (mxf
->edit_unit_byte_count
) {
1192 klv_encode_ber_length(pb
, 80);
1194 klv_encode_ber_length(pb
, 85 + 12+(s
->nb_streams
+1LL)*6 +
1195 12+mxf
->edit_units_count
*(11+mxf
->slice_count
*4LL));
1199 mxf_write_local_tag(pb
, 16, 0x3C0A);
1200 mxf_write_uuid(pb
, IndexTableSegment
, 0);
1203 mxf_write_local_tag(pb
, 8, 0x3F0B);
1204 avio_wb32(pb
, mxf
->time_base
.den
);
1205 avio_wb32(pb
, mxf
->time_base
.num
);
1207 // index start position
1208 mxf_write_local_tag(pb
, 8, 0x3F0C);
1209 avio_wb64(pb
, mxf
->last_indexed_edit_unit
);
1212 mxf_write_local_tag(pb
, 8, 0x3F0D);
1213 if (mxf
->edit_unit_byte_count
)
1214 avio_wb64(pb
, 0); // index table covers whole container
1216 avio_wb64(pb
, mxf
->edit_units_count
);
1218 // edit unit byte count
1219 mxf_write_local_tag(pb
, 4, 0x3F05);
1220 avio_wb32(pb
, mxf
->edit_unit_byte_count
);
1223 mxf_write_local_tag(pb
, 4, 0x3F06);
1227 mxf_write_local_tag(pb
, 4, 0x3F07);
1230 if (!mxf
->edit_unit_byte_count
) {
1231 // real slice count - 1
1232 mxf_write_local_tag(pb
, 1, 0x3F08);
1233 avio_w8(pb
, mxf
->slice_count
);
1235 // delta entry array
1236 mxf_write_local_tag(pb
, 8 + (s
->nb_streams
+1)*6, 0x3F09);
1237 avio_wb32(pb
, s
->nb_streams
+1); // num of entries
1238 avio_wb32(pb
, 6); // size of one entry
1239 // write system item delta entry
1241 avio_w8(pb
, 0); // slice entry
1242 avio_wb32(pb
, 0); // element delta
1243 for (i
= 0; i
< s
->nb_streams
; i
++) {
1244 AVStream
*st
= s
->streams
[i
];
1245 MXFStreamContext
*sc
= st
->priv_data
;
1246 avio_w8(pb
, sc
->temporal_reordering
);
1247 if (sc
->temporal_reordering
)
1248 temporal_reordering
= 1;
1249 if (i
== 0) { // video track
1250 avio_w8(pb
, 0); // slice number
1251 avio_wb32(pb
, KAG_SIZE
); // system item size including klv fill
1252 } else { // audio track
1253 unsigned audio_frame_size
= sc
->aic
.samples
[0]*sc
->aic
.sample_size
;
1254 audio_frame_size
+= klv_fill_size(audio_frame_size
);
1256 avio_wb32(pb
, (i
-1)*audio_frame_size
); // element delta
1260 mxf_write_local_tag(pb
, 8 + mxf
->edit_units_count
*(11+mxf
->slice_count
*4), 0x3F0A);
1261 avio_wb32(pb
, mxf
->edit_units_count
); // num of entries
1262 avio_wb32(pb
, 11+mxf
->slice_count
*4); // size of one entry
1264 for (i
= 0; i
< mxf
->edit_units_count
; i
++) {
1265 int temporal_offset
= 0;
1267 if (!(mxf
->index_entries
[i
].flags
& 0x33)) { // I frame
1268 mxf
->last_key_index
= key_index
;
1272 if (temporal_reordering
) {
1273 int pic_num_in_gop
= i
- key_index
;
1274 if (pic_num_in_gop
!= mxf
->index_entries
[i
].temporal_ref
) {
1275 for (j
= key_index
; j
< mxf
->edit_units_count
; j
++) {
1276 if (pic_num_in_gop
== mxf
->index_entries
[j
].temporal_ref
)
1279 if (j
== mxf
->edit_units_count
)
1280 av_log(s
, AV_LOG_WARNING
, "missing frames\n");
1281 temporal_offset
= j
- key_index
- pic_num_in_gop
;
1284 avio_w8(pb
, temporal_offset
);
1286 if ((mxf
->index_entries
[i
].flags
& 0x30) == 0x30) { // back and forward prediction
1287 avio_w8(pb
, mxf
->last_key_index
- i
);
1289 avio_w8(pb
, key_index
- i
); // key frame offset
1290 if ((mxf
->index_entries
[i
].flags
& 0x20) == 0x20) // only forward
1291 mxf
->last_key_index
= key_index
;
1294 if (!(mxf
->index_entries
[i
].flags
& 0x33) && // I frame
1295 mxf
->index_entries
[i
].flags
& 0x40 && !temporal_offset
)
1296 mxf
->index_entries
[i
].flags
|= 0x80; // random access
1297 avio_w8(pb
, mxf
->index_entries
[i
].flags
);
1299 avio_wb64(pb
, mxf
->index_entries
[i
].offset
);
1300 if (s
->nb_streams
> 1)
1301 avio_wb32(pb
, mxf
->index_entries
[i
].slice_offset
);
1304 mxf
->last_key_index
= key_index
- mxf
->edit_units_count
;
1305 mxf
->last_indexed_edit_unit
+= mxf
->edit_units_count
;
1306 mxf
->edit_units_count
= 0;
1310 static void mxf_write_klv_fill(AVFormatContext
*s
)
1312 unsigned pad
= klv_fill_size(avio_tell(s
->pb
));
1314 avio_write(s
->pb
, klv_fill_key
, 16);
1316 klv_encode_ber4_length(s
->pb
, pad
);
1317 ffio_fill(s
->pb
, 0, pad
);
1318 av_assert1(!(avio_tell(s
->pb
) & (KAG_SIZE
-1)));
1322 static int mxf_write_partition(AVFormatContext
*s
, int bodysid
,
1324 const uint8_t *key
, int write_metadata
)
1326 MXFContext
*mxf
= s
->priv_data
;
1327 AVIOContext
*pb
= s
->pb
;
1328 int64_t header_byte_count_offset
;
1329 unsigned index_byte_count
= 0;
1330 uint64_t partition_offset
= avio_tell(pb
);
1333 if (!mxf
->edit_unit_byte_count
&& mxf
->edit_units_count
)
1334 index_byte_count
= 85 + 12+(s
->nb_streams
+1)*6 +
1335 12+mxf
->edit_units_count
*(11+mxf
->slice_count
*4);
1336 else if (mxf
->edit_unit_byte_count
&& indexsid
)
1337 index_byte_count
= 80;
1339 if (index_byte_count
) {
1340 // add encoded ber length
1341 index_byte_count
+= 16 + klv_ber_length(index_byte_count
);
1342 index_byte_count
+= klv_fill_size(index_byte_count
);
1345 if (!memcmp(key
, body_partition_key
, 16)) {
1346 if ((err
= av_reallocp_array(&mxf
->body_partition_offset
, mxf
->body_partitions_count
+ 1,
1347 sizeof(*mxf
->body_partition_offset
))) < 0) {
1348 mxf
->body_partitions_count
= 0;
1351 mxf
->body_partition_offset
[mxf
->body_partitions_count
++] = partition_offset
;
1355 avio_write(pb
, key
, 16);
1356 klv_encode_ber_length(pb
, 88 + 16LL * DESCRIPTOR_COUNT(mxf
->essence_container_count
));
1358 // write partition value
1359 avio_wb16(pb
, 1); // majorVersion
1360 avio_wb16(pb
, 2); // minorVersion
1361 avio_wb32(pb
, KAG_SIZE
); // KAGSize
1363 avio_wb64(pb
, partition_offset
); // ThisPartition
1365 if (!memcmp(key
, body_partition_key
, 16) && mxf
->body_partitions_count
> 1)
1366 avio_wb64(pb
, mxf
->body_partition_offset
[mxf
->body_partitions_count
-2]); // PreviousPartition
1367 else if (!memcmp(key
, footer_partition_key
, 16) && mxf
->body_partitions_count
)
1368 avio_wb64(pb
, mxf
->body_partition_offset
[mxf
->body_partitions_count
-1]); // PreviousPartition
1372 avio_wb64(pb
, mxf
->footer_partition_offset
); // footerPartition
1375 header_byte_count_offset
= avio_tell(pb
);
1376 avio_wb64(pb
, 0); // headerByteCount, update later
1379 avio_wb64(pb
, index_byte_count
); // indexByteCount
1380 avio_wb32(pb
, index_byte_count
? indexsid
: 0); // indexSID
1383 if (bodysid
&& mxf
->edit_units_count
&& mxf
->body_partitions_count
) {
1384 avio_wb64(pb
, mxf
->body_offset
);
1388 avio_wb32(pb
, bodysid
); // bodySID
1390 // operational pattern
1391 avio_write(pb
, op1a_ul
, 16);
1393 // essence container
1394 mxf_write_essence_container_refs(s
);
1396 if (write_metadata
) {
1397 // mark the start of the headermetadata and calculate metadata size
1399 unsigned header_byte_count
;
1401 mxf_write_klv_fill(s
);
1402 start
= avio_tell(s
->pb
);
1403 mxf_write_primer_pack(s
);
1404 mxf_write_header_metadata_sets(s
);
1405 pos
= avio_tell(s
->pb
);
1406 header_byte_count
= pos
- start
+ klv_fill_size(pos
);
1408 // update header_byte_count
1409 avio_seek(pb
, header_byte_count_offset
, SEEK_SET
);
1410 avio_wb64(pb
, header_byte_count
);
1411 avio_seek(pb
, pos
, SEEK_SET
);
1419 static int mxf_parse_dnxhd_frame(AVFormatContext
*s
, AVStream
*st
,
1422 MXFContext
*mxf
= s
->priv_data
;
1423 MXFStreamContext
*sc
= st
->priv_data
;
1425 uint8_t* header_cid
;
1428 if (mxf
->header_written
)
1434 header_cid
= pkt
->data
+ 0x28;
1435 cid
= header_cid
[0] << 24 | header_cid
[1] << 16 | header_cid
[2] << 8 | header_cid
[3];
1437 if ((frame_size
= avpriv_dnxhd_get_frame_size(cid
)) < 0)
1443 sc
->component_depth
= 10;
1453 sc
->component_depth
= 10;
1463 sc
->component_depth
= 10;
1478 sc
->codec_ul
= &mxf_essence_container_uls
[sc
->index
].codec_ul
;
1479 sc
->aspect_ratio
= (AVRational
){ 16, 9 };
1481 mxf
->edit_unit_byte_count
= KAG_SIZE
;
1482 for (i
= 0; i
< s
->nb_streams
; i
++) {
1483 AVStream
*st
= s
->streams
[i
];
1484 MXFStreamContext
*sc
= st
->priv_data
;
1485 if (st
->codec
->codec_type
== AVMEDIA_TYPE_AUDIO
) {
1486 mxf
->edit_unit_byte_count
+= 16 + 4 + sc
->aic
.samples
[0]*sc
->aic
.sample_size
;
1487 mxf
->edit_unit_byte_count
+= klv_fill_size(mxf
->edit_unit_byte_count
);
1488 } else if (st
->codec
->codec_type
== AVMEDIA_TYPE_VIDEO
) {
1489 mxf
->edit_unit_byte_count
+= 16 + 4 + frame_size
;
1490 mxf
->edit_unit_byte_count
+= klv_fill_size(mxf
->edit_unit_byte_count
);
1497 static int mxf_parse_dv_frame(AVFormatContext
*s
, AVStream
*st
, AVPacket
*pkt
)
1499 MXFContext
*mxf
= s
->priv_data
;
1500 MXFStreamContext
*sc
= st
->priv_data
;
1501 uint8_t *vs_pack
, *vsc_pack
;
1502 int i
, ul_index
, frame_size
, stype
, pal
;
1504 if (mxf
->header_written
)
1507 // Check for minimal frame size
1508 if (pkt
->size
< 120000)
1511 vs_pack
= pkt
->data
+ 80*5 + 48;
1512 vsc_pack
= pkt
->data
+ 80*5 + 53;
1513 stype
= vs_pack
[3] & 0x1f;
1514 pal
= (vs_pack
[3] >> 5) & 0x1;
1516 if ((vs_pack
[2] & 0x07) == 0x02)
1517 sc
->aspect_ratio
= (AVRational
){ 16, 9 };
1519 sc
->aspect_ratio
= (AVRational
){ 4, 3 };
1521 sc
->interlaced
= (vsc_pack
[3] >> 4) & 0x01;
1522 // TODO: fix dv encoder to set proper FF/FS value in VSC pack
1523 // and set field dominance accordingly
1524 // av_log(s, AV_LOG_DEBUG, "DV vsc pack ff/ss = %x\n", vsc_pack[2] >> 6);
1527 case 0x18: // DV100 720p
1529 frame_size
= pal
? 288000 : 240000;
1530 if (sc
->interlaced
) {
1531 av_log(s
, AV_LOG_ERROR
, "source marked as interlaced but codec profile is progressive\n");
1535 case 0x14: // DV100 1080i
1537 frame_size
= pal
? 576000 : 480000;
1541 frame_size
= pal
? 288000 : 240000;
1545 frame_size
= pal
? 144000 : 120000;
1548 sc
->index
= ul_index
+ 16;
1549 sc
->codec_ul
= &mxf_essence_container_uls
[sc
->index
].codec_ul
;
1551 mxf
->edit_unit_byte_count
= KAG_SIZE
;
1552 for (i
= 0; i
< s
->nb_streams
; i
++) {
1553 AVStream
*st
= s
->streams
[i
];
1554 MXFStreamContext
*sc
= st
->priv_data
;
1555 if (st
->codec
->codec_type
== AVMEDIA_TYPE_AUDIO
) {
1556 mxf
->edit_unit_byte_count
+= 16 + 4 + sc
->aic
.samples
[0]*sc
->aic
.sample_size
;
1557 mxf
->edit_unit_byte_count
+= klv_fill_size(mxf
->edit_unit_byte_count
);
1558 } else if (st
->codec
->codec_type
== AVMEDIA_TYPE_VIDEO
) {
1559 mxf
->edit_unit_byte_count
+= 16 + 4 + frame_size
;
1560 mxf
->edit_unit_byte_count
+= klv_fill_size(mxf
->edit_unit_byte_count
);
1567 static const UID mxf_mpeg2_codec_uls
[] = {
1568 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x01,0x10,0x00 }, // MP-ML I-Frame
1569 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x01,0x11,0x00 }, // MP-ML Long GOP
1570 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x02,0x02,0x00 }, // 422P-ML I-Frame
1571 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x02,0x03,0x00 }, // 422P-ML Long GOP
1572 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x03,0x02,0x00 }, // MP-HL I-Frame
1573 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x03,0x03,0x00 }, // MP-HL Long GOP
1574 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x04,0x02,0x00 }, // 422P-HL I-Frame
1575 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x04,0x03,0x00 }, // 422P-HL Long GOP
1576 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x05,0x02,0x00 }, // MP@H-14 I-Frame
1577 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x05,0x03,0x00 }, // MP@H-14 Long GOP
1580 static const UID
*mxf_get_mpeg2_codec_ul(AVCodecContext
*avctx
)
1582 int long_gop
= avctx
->gop_size
> 1 || avctx
->has_b_frames
;
1584 if (avctx
->profile
== 4) { // Main
1585 if (avctx
->level
== 8) // Main
1586 return &mxf_mpeg2_codec_uls
[0+long_gop
];
1587 else if (avctx
->level
== 4) // High
1588 return &mxf_mpeg2_codec_uls
[4+long_gop
];
1589 else if (avctx
->level
== 6) // High 14
1590 return &mxf_mpeg2_codec_uls
[8+long_gop
];
1591 } else if (avctx
->profile
== 0) { // 422
1592 if (avctx
->level
== 5) // Main
1593 return &mxf_mpeg2_codec_uls
[2+long_gop
];
1594 else if (avctx
->level
== 2) // High
1595 return &mxf_mpeg2_codec_uls
[6+long_gop
];
1600 static int mxf_parse_mpeg2_frame(AVFormatContext
*s
, AVStream
*st
,
1601 AVPacket
*pkt
, MXFIndexEntry
*e
)
1603 MXFStreamContext
*sc
= st
->priv_data
;
1607 for(i
= 0; i
< pkt
->size
- 4; i
++) {
1608 c
= (c
<<8) + pkt
->data
[i
];
1610 if ((pkt
->data
[i
+1] & 0xf0) == 0x10) { // seq ext
1611 st
->codec
->profile
= pkt
->data
[i
+1] & 0x07;
1612 st
->codec
->level
= pkt
->data
[i
+2] >> 4;
1613 } else if (i
+ 5 < pkt
->size
&& (pkt
->data
[i
+1] & 0xf0) == 0x80) { // pict coding ext
1614 sc
->interlaced
= !(pkt
->data
[i
+5] & 0x80); // progressive frame
1616 sc
->field_dominance
= 1 + !(pkt
->data
[i
+4] & 0x80); // top field first
1619 } else if (c
== 0x1b8) { // gop
1620 if (pkt
->data
[i
+4]>>6 & 0x01) { // closed
1622 if (e
->flags
& 0x40) // sequence header present
1623 e
->flags
|= 0x80; // random access
1625 } else if (c
== 0x1b3) { // seq
1627 switch ((pkt
->data
[i
+4]>>4) & 0xf) {
1628 case 2: sc
->aspect_ratio
= (AVRational
){ 4, 3}; break;
1629 case 3: sc
->aspect_ratio
= (AVRational
){ 16, 9}; break;
1630 case 4: sc
->aspect_ratio
= (AVRational
){221,100}; break;
1632 av_reduce(&sc
->aspect_ratio
.num
, &sc
->aspect_ratio
.den
,
1633 st
->codec
->width
, st
->codec
->height
, 1024*1024);
1635 } else if (c
== 0x100) { // pic
1636 int pict_type
= (pkt
->data
[i
+2]>>3) & 0x07;
1637 e
->temporal_ref
= (pkt
->data
[i
+1]<<2) | (pkt
->data
[i
+2]>>6);
1638 if (pict_type
== 2) { // P frame
1640 sc
->closed_gop
= 0; // reset closed gop, don't matter anymore
1641 } else if (pict_type
== 3) { // B frame
1643 e
->flags
|= 0x13; // only backward prediction
1646 sc
->temporal_reordering
= -1;
1647 } else if (!pict_type
) {
1648 av_log(s
, AV_LOG_ERROR
, "error parsing mpeg2 frame\n");
1653 if (s
->oformat
!= &ff_mxf_d10_muxer
)
1654 sc
->codec_ul
= mxf_get_mpeg2_codec_ul(st
->codec
);
1655 return !!sc
->codec_ul
;
1658 static uint64_t mxf_parse_timestamp(time_t timestamp
)
1660 struct tm
*time
= gmtime(×tamp
);
1663 return (uint64_t)(time
->tm_year
+1900) << 48 |
1664 (uint64_t)(time
->tm_mon
+1) << 40 |
1665 (uint64_t) time
->tm_mday
<< 32 |
1666 time
->tm_hour
<< 24 |
1667 time
->tm_min
<< 16 |
1671 static void mxf_gen_umid(AVFormatContext
*s
)
1673 MXFContext
*mxf
= s
->priv_data
;
1674 uint32_t seed
= av_get_random_seed();
1675 uint64_t umid
= seed
+ 0x5294713400000000LL
;
1677 AV_WB64(mxf
->umid
, umid
);
1678 AV_WB64(mxf
->umid
+8, umid
>>8);
1680 mxf
->instance_number
= seed
& 0xFFFFFF;
1683 static int mxf_write_header(AVFormatContext
*s
)
1685 MXFContext
*mxf
= s
->priv_data
;
1687 uint8_t present
[FF_ARRAY_ELEMS(mxf_essence_container_uls
)] = {0};
1688 const MXFSamplesPerFrame
*spf
= NULL
;
1689 AVDictionaryEntry
*t
;
1690 int64_t timestamp
= 0;
1691 AVDictionaryEntry
*tcr
= av_dict_get(s
->metadata
, "timecode", NULL
, 0);
1696 for (i
= 0; i
< s
->nb_streams
; i
++) {
1697 AVStream
*st
= s
->streams
[i
];
1698 MXFStreamContext
*sc
= av_mallocz(sizeof(*sc
));
1700 return AVERROR(ENOMEM
);
1703 if ((i
== 0) ^ (st
->codec
->codec_type
== AVMEDIA_TYPE_VIDEO
)) {
1704 av_log(s
, AV_LOG_ERROR
, "there must be exactly one video stream and it must be the first one\n");
1708 if (st
->codec
->codec_type
== AVMEDIA_TYPE_VIDEO
) {
1709 // TODO: should be avg_frame_rate
1710 AVRational rate
, tbc
= st
->time_base
;
1711 // Default component depth to 8
1712 sc
->component_depth
= 8;
1713 mxf
->timecode_base
= (tbc
.den
+ tbc
.num
/2) / tbc
.num
;
1714 spf
= ff_mxf_get_samples_per_frame(s
, tbc
);
1716 av_log(s
, AV_LOG_ERROR
, "Unsupported video frame rate %d/%d\n",
1718 return AVERROR(EINVAL
);
1720 mxf
->time_base
= spf
->time_base
;
1721 rate
= av_inv_q(mxf
->time_base
);
1722 avpriv_set_pts_info(st
, 64, mxf
->time_base
.num
, mxf
->time_base
.den
);
1724 tcr
= av_dict_get(st
->metadata
, "timecode", NULL
, 0);
1726 ret
= av_timecode_init_from_string(&mxf
->tc
, rate
, tcr
->value
, s
);
1728 ret
= av_timecode_init(&mxf
->tc
, rate
, 0, 0, s
);
1731 sc
->video_bit_rate
= st
->codec
->bit_rate
? st
->codec
->bit_rate
: st
->codec
->rc_max_rate
;
1732 if (s
->oformat
== &ff_mxf_d10_muxer
) {
1733 if (sc
->video_bit_rate
== 50000000) {
1734 if (mxf
->time_base
.den
== 25) sc
->index
= 3;
1736 } else if (sc
->video_bit_rate
== 40000000) {
1737 if (mxf
->time_base
.den
== 25) sc
->index
= 7;
1739 } else if (sc
->video_bit_rate
== 30000000) {
1740 if (mxf
->time_base
.den
== 25) sc
->index
= 11;
1741 else sc
->index
= 13;
1743 av_log(s
, AV_LOG_ERROR
, "error MXF D-10 only support 30/40/50 mbit/s\n");
1747 mxf
->edit_unit_byte_count
= KAG_SIZE
; // system element
1748 mxf
->edit_unit_byte_count
+= 16 + 4 + (uint64_t)sc
->video_bit_rate
*
1749 mxf
->time_base
.num
/ (8*mxf
->time_base
.den
);
1750 mxf
->edit_unit_byte_count
+= klv_fill_size(mxf
->edit_unit_byte_count
);
1751 mxf
->edit_unit_byte_count
+= 16 + 4 + 4 + spf
->samples_per_frame
[0]*8*4;
1752 mxf
->edit_unit_byte_count
+= klv_fill_size(mxf
->edit_unit_byte_count
);
1754 } else if (st
->codec
->codec_type
== AVMEDIA_TYPE_AUDIO
) {
1755 if (st
->codec
->sample_rate
!= 48000) {
1756 av_log(s
, AV_LOG_ERROR
, "only 48khz is implemented\n");
1759 avpriv_set_pts_info(st
, 64, 1, st
->codec
->sample_rate
);
1760 if (s
->oformat
== &ff_mxf_d10_muxer
) {
1761 if (st
->index
!= 1) {
1762 av_log(s
, AV_LOG_ERROR
, "MXF D-10 only support one audio track\n");
1765 if (st
->codec
->codec_id
!= AV_CODEC_ID_PCM_S16LE
&&
1766 st
->codec
->codec_id
!= AV_CODEC_ID_PCM_S24LE
) {
1767 av_log(s
, AV_LOG_ERROR
, "MXF D-10 only support 16 or 24 bits le audio\n");
1769 sc
->index
= ((MXFStreamContext
*)s
->streams
[0]->priv_data
)->index
+ 1;
1771 mxf
->slice_count
= 1;
1775 sc
->index
= mxf_get_essence_container_ul_index(st
->codec
->codec_id
);
1776 if (sc
->index
== -1) {
1777 av_log(s
, AV_LOG_ERROR
, "track %d: could not find essence container ul, "
1778 "codec not currently supported in container\n", i
);
1783 sc
->codec_ul
= &mxf_essence_container_uls
[sc
->index
].codec_ul
;
1785 memcpy(sc
->track_essence_element_key
, mxf_essence_container_uls
[sc
->index
].element_ul
, 15);
1786 sc
->track_essence_element_key
[15] = present
[sc
->index
];
1787 PRINT_KEY(s
, "track essence element key", sc
->track_essence_element_key
);
1789 if (!present
[sc
->index
])
1790 mxf
->essence_container_count
++;
1791 present
[sc
->index
]++;
1794 if (s
->oformat
== &ff_mxf_d10_muxer
) {
1795 mxf
->essence_container_count
= 1;
1798 if (!(s
->flags
& AVFMT_FLAG_BITEXACT
))
1801 for (i
= 0; i
< s
->nb_streams
; i
++) {
1802 MXFStreamContext
*sc
= s
->streams
[i
]->priv_data
;
1803 // update element count
1804 sc
->track_essence_element_key
[13] = present
[sc
->index
];
1805 if (!memcmp(sc
->track_essence_element_key
, mxf_essence_container_uls
[15].element_ul
, 13)) // DV
1806 sc
->order
= (0x15 << 24) | AV_RB32(sc
->track_essence_element_key
+13);
1808 sc
->order
= AV_RB32(sc
->track_essence_element_key
+12);
1811 if (t
= av_dict_get(s
->metadata
, "creation_time", NULL
, 0))
1812 timestamp
= ff_iso8601_to_unix_time(t
->value
);
1814 mxf
->timestamp
= mxf_parse_timestamp(timestamp
);
1817 mxf
->timecode_track
= av_mallocz(sizeof(*mxf
->timecode_track
));
1818 if (!mxf
->timecode_track
)
1819 return AVERROR(ENOMEM
);
1820 mxf
->timecode_track
->priv_data
= av_mallocz(sizeof(MXFStreamContext
));
1821 if (!mxf
->timecode_track
->priv_data
)
1822 return AVERROR(ENOMEM
);
1823 mxf
->timecode_track
->index
= -1;
1826 spf
= ff_mxf_get_samples_per_frame(s
, (AVRational
){ 1, 25 });
1828 if (ff_audio_interleave_init(s
, spf
->samples_per_frame
, mxf
->time_base
) < 0)
1834 static const uint8_t system_metadata_pack_key
[] = { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0D,0x01,0x03,0x01,0x04,0x01,0x01,0x00 };
1835 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 };
1837 static void mxf_write_system_item(AVFormatContext
*s
)
1839 MXFContext
*mxf
= s
->priv_data
;
1840 AVIOContext
*pb
= s
->pb
;
1844 frame
= mxf
->last_indexed_edit_unit
+ mxf
->edit_units_count
;
1846 // write system metadata pack
1847 avio_write(pb
, system_metadata_pack_key
, 16);
1848 klv_encode_ber4_length(pb
, 57);
1849 avio_w8(pb
, 0x5c); // UL, user date/time stamp, picture and sound item present
1850 avio_w8(pb
, 0x04); // content package rate
1851 avio_w8(pb
, 0x00); // content package type
1852 avio_wb16(pb
, 0x00); // channel handle
1853 avio_wb16(pb
, (mxf
->tc
.start
+ frame
) & 0xFFFF); // continuity count, supposed to overflow
1854 if (mxf
->essence_container_count
> 1)
1855 avio_write(pb
, multiple_desc_ul
, 16);
1857 MXFStreamContext
*sc
= s
->streams
[0]->priv_data
;
1858 avio_write(pb
, mxf_essence_container_uls
[sc
->index
].container_ul
, 16);
1862 avio_wb64(pb
, 0); // creation date/time stamp
1864 avio_w8(pb
, 0x81); // SMPTE 12M time code
1865 time_code
= av_timecode_get_smpte_from_framenum(&mxf
->tc
, frame
);
1866 avio_wb32(pb
, time_code
);
1867 avio_wb32(pb
, 0); // binary group data
1870 // write system metadata package set
1871 avio_write(pb
, system_metadata_package_set_key
, 16);
1872 klv_encode_ber4_length(pb
, 35);
1873 avio_w8(pb
, 0x83); // UMID
1874 avio_wb16(pb
, 0x20);
1875 mxf_write_umid(s
, 1);
1878 static void mxf_write_d10_video_packet(AVFormatContext
*s
, AVStream
*st
, AVPacket
*pkt
)
1880 MXFContext
*mxf
= s
->priv_data
;
1881 AVIOContext
*pb
= s
->pb
;
1882 MXFStreamContext
*sc
= st
->priv_data
;
1883 int packet_size
= (uint64_t)sc
->video_bit_rate
*mxf
->time_base
.num
/
1884 (8*mxf
->time_base
.den
); // frame size
1887 packet_size
+= 16 + 4;
1888 packet_size
+= klv_fill_size(packet_size
);
1890 klv_encode_ber4_length(pb
, pkt
->size
);
1891 avio_write(pb
, pkt
->data
, pkt
->size
);
1893 // ensure CBR muxing by padding to correct video frame size
1894 pad
= packet_size
- pkt
->size
- 16 - 4;
1896 avio_write(s
->pb
, klv_fill_key
, 16);
1898 klv_encode_ber4_length(s
->pb
, pad
);
1899 ffio_fill(s
->pb
, 0, pad
);
1900 av_assert1(!(avio_tell(s
->pb
) & (KAG_SIZE
-1)));
1902 av_log(s
, AV_LOG_WARNING
, "cannot fill d-10 video packet\n");
1903 ffio_fill(s
->pb
, 0, pad
);
1907 static void mxf_write_d10_audio_packet(AVFormatContext
*s
, AVStream
*st
, AVPacket
*pkt
)
1909 MXFContext
*mxf
= s
->priv_data
;
1910 AVIOContext
*pb
= s
->pb
;
1911 int frame_size
= pkt
->size
/ st
->codec
->block_align
;
1912 uint8_t *samples
= pkt
->data
;
1913 uint8_t *end
= pkt
->data
+ pkt
->size
;
1916 klv_encode_ber4_length(pb
, 4 + frame_size
*4*8);
1918 avio_w8(pb
, (frame_size
== 1920 ? 0 : (mxf
->edit_units_count
-1) % 5 + 1));
1919 avio_wl16(pb
, frame_size
);
1920 avio_w8(pb
, (1<<st
->codec
->channels
)-1);
1922 while (samples
< end
) {
1923 for (i
= 0; i
< st
->codec
->channels
; i
++) {
1925 if (st
->codec
->codec_id
== AV_CODEC_ID_PCM_S24LE
) {
1926 sample
= AV_RL24(samples
)<< 4;
1929 sample
= AV_RL16(samples
)<<12;
1932 avio_wl32(pb
, sample
| i
);
1939 static int mxf_write_packet(AVFormatContext
*s
, AVPacket
*pkt
)
1941 MXFContext
*mxf
= s
->priv_data
;
1942 AVIOContext
*pb
= s
->pb
;
1943 AVStream
*st
= s
->streams
[pkt
->stream_index
];
1944 MXFStreamContext
*sc
= st
->priv_data
;
1945 MXFIndexEntry ie
= {0};
1948 if (!mxf
->edit_unit_byte_count
&& !(mxf
->edit_units_count
% EDIT_UNITS_PER_BODY
)) {
1949 if ((err
= av_reallocp_array(&mxf
->index_entries
, mxf
->edit_units_count
1950 + EDIT_UNITS_PER_BODY
, sizeof(*mxf
->index_entries
))) < 0) {
1951 mxf
->edit_units_count
= 0;
1952 av_log(s
, AV_LOG_ERROR
, "could not allocate index entries\n");
1957 if (st
->codec
->codec_id
== AV_CODEC_ID_MPEG2VIDEO
) {
1958 if (!mxf_parse_mpeg2_frame(s
, st
, pkt
, &ie
)) {
1959 av_log(s
, AV_LOG_ERROR
, "could not get mpeg2 profile and level\n");
1962 } else if (st
->codec
->codec_id
== AV_CODEC_ID_DNXHD
) {
1963 if (!mxf_parse_dnxhd_frame(s
, st
, pkt
)) {
1964 av_log(s
, AV_LOG_ERROR
, "could not get dnxhd profile\n");
1967 } else if (st
->codec
->codec_id
== AV_CODEC_ID_DVVIDEO
) {
1968 if (!mxf_parse_dv_frame(s
, st
, pkt
)) {
1969 av_log(s
, AV_LOG_ERROR
, "could not get dv profile\n");
1974 if (!mxf
->header_written
) {
1975 if (mxf
->edit_unit_byte_count
) {
1976 if ((err
= mxf_write_partition(s
, 1, 2, header_open_partition_key
, 1)) < 0)
1978 mxf_write_klv_fill(s
);
1979 mxf_write_index_table_segment(s
);
1981 if ((err
= mxf_write_partition(s
, 0, 0, header_open_partition_key
, 1)) < 0)
1984 mxf
->header_written
= 1;
1987 if (st
->index
== 0) {
1988 if (!mxf
->edit_unit_byte_count
&&
1989 (!mxf
->edit_units_count
|| mxf
->edit_units_count
> EDIT_UNITS_PER_BODY
) &&
1990 !(ie
.flags
& 0x33)) { // I frame, Gop start
1991 mxf_write_klv_fill(s
);
1992 if ((err
= mxf_write_partition(s
, 1, 2, body_partition_key
, 0)) < 0)
1994 mxf_write_klv_fill(s
);
1995 mxf_write_index_table_segment(s
);
1998 mxf_write_klv_fill(s
);
1999 mxf_write_system_item(s
);
2001 if (!mxf
->edit_unit_byte_count
) {
2002 mxf
->index_entries
[mxf
->edit_units_count
].offset
= mxf
->body_offset
;
2003 mxf
->index_entries
[mxf
->edit_units_count
].flags
= ie
.flags
;
2004 mxf
->index_entries
[mxf
->edit_units_count
].temporal_ref
= ie
.temporal_ref
;
2005 mxf
->body_offset
+= KAG_SIZE
; // size of system element
2007 mxf
->edit_units_count
++;
2008 } else if (!mxf
->edit_unit_byte_count
&& st
->index
== 1) {
2009 mxf
->index_entries
[mxf
->edit_units_count
-1].slice_offset
=
2010 mxf
->body_offset
- mxf
->index_entries
[mxf
->edit_units_count
-1].offset
;
2013 mxf_write_klv_fill(s
);
2014 avio_write(pb
, sc
->track_essence_element_key
, 16); // write key
2015 if (s
->oformat
== &ff_mxf_d10_muxer
) {
2016 if (st
->codec
->codec_type
== AVMEDIA_TYPE_VIDEO
)
2017 mxf_write_d10_video_packet(s
, st
, pkt
);
2019 mxf_write_d10_audio_packet(s
, st
, pkt
);
2021 klv_encode_ber4_length(pb
, pkt
->size
); // write length
2022 avio_write(pb
, pkt
->data
, pkt
->size
);
2023 mxf
->body_offset
+= 16+4+pkt
->size
+ klv_fill_size(16+4+pkt
->size
);
2031 static void mxf_write_random_index_pack(AVFormatContext
*s
)
2033 MXFContext
*mxf
= s
->priv_data
;
2034 AVIOContext
*pb
= s
->pb
;
2035 uint64_t pos
= avio_tell(pb
);
2038 avio_write(pb
, random_index_pack_key
, 16);
2039 klv_encode_ber_length(pb
, 28 + 12LL*mxf
->body_partitions_count
);
2041 if (mxf
->edit_unit_byte_count
)
2042 avio_wb32(pb
, 1); // BodySID of header partition
2045 avio_wb64(pb
, 0); // offset of header partition
2047 for (i
= 0; i
< mxf
->body_partitions_count
; i
++) {
2048 avio_wb32(pb
, 1); // BodySID
2049 avio_wb64(pb
, mxf
->body_partition_offset
[i
]);
2052 avio_wb32(pb
, 0); // BodySID of footer partition
2053 avio_wb64(pb
, mxf
->footer_partition_offset
);
2055 avio_wb32(pb
, avio_tell(pb
) - pos
+ 4);
2058 static int mxf_write_footer(AVFormatContext
*s
)
2060 MXFContext
*mxf
= s
->priv_data
;
2061 AVIOContext
*pb
= s
->pb
;
2064 mxf
->duration
= mxf
->last_indexed_edit_unit
+ mxf
->edit_units_count
;
2066 mxf_write_klv_fill(s
);
2067 mxf
->footer_partition_offset
= avio_tell(pb
);
2068 if (mxf
->edit_unit_byte_count
) { // no need to repeat index
2069 if ((err
= mxf_write_partition(s
, 0, 0, footer_partition_key
, 0)) < 0)
2072 if ((err
= mxf_write_partition(s
, 0, 2, footer_partition_key
, 0)) < 0)
2074 mxf_write_klv_fill(s
);
2075 mxf_write_index_table_segment(s
);
2078 mxf_write_klv_fill(s
);
2079 mxf_write_random_index_pack(s
);
2081 if (s
->pb
->seekable
) {
2082 avio_seek(pb
, 0, SEEK_SET
);
2083 if (mxf
->edit_unit_byte_count
) {
2084 if ((err
= mxf_write_partition(s
, 1, 2, header_closed_partition_key
, 1)) < 0)
2086 mxf_write_klv_fill(s
);
2087 mxf_write_index_table_segment(s
);
2089 if ((err
= mxf_write_partition(s
, 0, 0, header_closed_partition_key
, 1)) < 0)
2094 ff_audio_interleave_close(s
);
2096 av_freep(&mxf
->index_entries
);
2097 av_freep(&mxf
->body_partition_offset
);
2098 av_freep(&mxf
->timecode_track
->priv_data
);
2099 av_freep(&mxf
->timecode_track
);
2106 static int mxf_interleave_get_packet(AVFormatContext
*s
, AVPacket
*out
, AVPacket
*pkt
, int flush
)
2108 int i
, stream_count
= 0;
2110 for (i
= 0; i
< s
->nb_streams
; i
++)
2111 stream_count
+= !!s
->streams
[i
]->last_in_packet_buffer
;
2113 if (stream_count
&& (s
->nb_streams
== stream_count
|| flush
)) {
2114 AVPacketList
*pktl
= s
->packet_buffer
;
2115 if (s
->nb_streams
!= stream_count
) {
2116 AVPacketList
*last
= NULL
;
2117 // find last packet in edit unit
2119 if (!stream_count
|| pktl
->pkt
.stream_index
== 0)
2125 // purge packet queue
2127 AVPacketList
*next
= pktl
->next
;
2129 if(s
->streams
[pktl
->pkt
.stream_index
]->last_in_packet_buffer
== pktl
)
2130 s
->streams
[pktl
->pkt
.stream_index
]->last_in_packet_buffer
= NULL
;
2131 av_free_packet(&pktl
->pkt
);
2138 s
->packet_buffer
= NULL
;
2139 s
->packet_buffer_end
= NULL
;
2142 pktl
= s
->packet_buffer
;
2146 av_dlog(s
, "out st:%d dts:%"PRId64
"\n", (*out
).stream_index
, (*out
).dts
);
2147 s
->packet_buffer
= pktl
->next
;
2148 if(s
->streams
[pktl
->pkt
.stream_index
]->last_in_packet_buffer
== pktl
)
2149 s
->streams
[pktl
->pkt
.stream_index
]->last_in_packet_buffer
= NULL
;
2150 if(!s
->packet_buffer
)
2151 s
->packet_buffer_end
= NULL
;
2156 av_init_packet(out
);
2161 static int mxf_compare_timestamps(AVFormatContext
*s
, AVPacket
*next
, AVPacket
*pkt
)
2163 MXFStreamContext
*sc
= s
->streams
[pkt
->stream_index
]->priv_data
;
2164 MXFStreamContext
*sc2
= s
->streams
[next
->stream_index
]->priv_data
;
2166 return next
->dts
> pkt
->dts
||
2167 (next
->dts
== pkt
->dts
&& sc
->order
< sc2
->order
);
2170 static int mxf_interleave(AVFormatContext
*s
, AVPacket
*out
, AVPacket
*pkt
, int flush
)
2172 return ff_audio_rechunk_interleave(s
, out
, pkt
, flush
,
2173 mxf_interleave_get_packet
, mxf_compare_timestamps
);
2176 static const AVOption d10_options
[] = {
2177 { "d10_channelcount", "Force/set channelcount in generic sound essence descriptor",
2178 offsetof(MXFContext
, channel_count
), AV_OPT_TYPE_INT
, {.i64
= -1}, -1, 8, AV_OPT_FLAG_ENCODING_PARAM
},
2182 static const AVClass mxf_d10_muxer_class
= {
2183 .class_name
= "MXF-D10 muxer",
2184 .item_name
= av_default_item_name
,
2185 .option
= d10_options
,
2186 .version
= LIBAVUTIL_VERSION_INT
,
2189 AVOutputFormat ff_mxf_muxer
= {
2191 .long_name
= NULL_IF_CONFIG_SMALL("MXF (Material eXchange Format)"),
2192 .mime_type
= "application/mxf",
2193 .extensions
= "mxf",
2194 .priv_data_size
= sizeof(MXFContext
),
2195 .audio_codec
= AV_CODEC_ID_PCM_S16LE
,
2196 .video_codec
= AV_CODEC_ID_MPEG2VIDEO
,
2197 .write_header
= mxf_write_header
,
2198 .write_packet
= mxf_write_packet
,
2199 .write_trailer
= mxf_write_footer
,
2200 .flags
= AVFMT_NOTIMESTAMPS
,
2201 .interleave_packet
= mxf_interleave
,
2204 AVOutputFormat ff_mxf_d10_muxer
= {
2206 .long_name
= NULL_IF_CONFIG_SMALL("MXF (Material eXchange Format) D-10 Mapping"),
2207 .mime_type
= "application/mxf",
2208 .priv_data_size
= sizeof(MXFContext
),
2209 .audio_codec
= AV_CODEC_ID_PCM_S16LE
,
2210 .video_codec
= AV_CODEC_ID_MPEG2VIDEO
,
2211 .write_header
= mxf_write_header
,
2212 .write_packet
= mxf_write_packet
,
2213 .write_trailer
= mxf_write_footer
,
2214 .flags
= AVFMT_NOTIMESTAMPS
,
2215 .interleave_packet
= mxf_interleave
,
2216 .priv_class
= &mxf_d10_muxer_class
,