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
38 #include "libavutil/random_seed.h"
39 #include "libavcodec/bytestream.h"
40 #include "audiointerleave.h"
44 static const int NTSC_samples_per_frame[] = { 1602, 1601, 1602, 1601, 1602, 0 };
45 static const int PAL_samples_per_frame[] = { 1920, 0 };
47 extern AVOutputFormat mxf_d10_muxer;
49 #define EDIT_UNITS_PER_BODY 250
60 unsigned slice_offset; ///< offset of audio slice
61 uint16_t temporal_ref;
65 AudioInterleaveContext aic;
66 UID track_essence_element_key;
67 int index; ///< index in mxf_essence_container_uls table
69 int order; ///< interleaving order if dts are equal
70 int interlaced; ///< wether picture is interlaced
71 int temporal_reordering;
72 AVRational aspect_ratio; ///< display aspect ratio
73 int closed_gop; ///< gop is closed, used in mpeg-2 frame parsing
80 void (*write_desc)(AVFormatContext *, AVStream *);
81 } MXFContainerEssenceEntry;
86 } mxf_essence_mappings[] = {
87 { CODEC_ID_MPEG2VIDEO, 0 },
88 { CODEC_ID_PCM_S24LE, 1 },
89 { CODEC_ID_PCM_S16LE, 1 },
93 static void mxf_write_wav_desc(AVFormatContext *s, AVStream *st);
94 static void mxf_write_aes3_desc(AVFormatContext *s, AVStream *st);
95 static void mxf_write_mpegvideo_desc(AVFormatContext *s, AVStream *st);
96 static void mxf_write_cdci_desc(AVFormatContext *s, AVStream *st);
97 static void mxf_write_generic_sound_desc(AVFormatContext *s, AVStream *st);
99 static const MXFContainerEssenceEntry mxf_essence_container_uls[] = {
100 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x02,0x0D,0x01,0x03,0x01,0x02,0x04,0x60,0x01 },
101 { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x15,0x01,0x05,0x00 },
102 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x00,0x00,0x00 },
103 mxf_write_mpegvideo_desc },
104 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x06,0x03,0x00 },
105 { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x16,0x01,0x03,0x00 },
106 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 },
107 mxf_write_aes3_desc },
108 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x06,0x01,0x00 },
109 { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x16,0x01,0x01,0x00 },
110 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 },
111 mxf_write_wav_desc },
112 // D-10 625/50 PAL 50mb/s
113 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x01,0x01 },
114 { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x05,0x01,0x01,0x00 },
115 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x01,0x02,0x01,0x01 },
116 mxf_write_cdci_desc },
117 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x01,0x01 },
118 { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x06,0x01,0x10,0x00 },
119 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 },
120 mxf_write_generic_sound_desc },
121 // D-10 525/60 NTSC 50mb/s
122 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x02,0x01 },
123 { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x05,0x01,0x01,0x00 },
124 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x01,0x02,0x01,0x02 },
125 mxf_write_cdci_desc },
126 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x02,0x01 },
127 { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x06,0x01,0x10,0x00 },
128 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 },
129 mxf_write_generic_sound_desc },
130 // D-10 625/50 PAL 40mb/s
131 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x03,0x01 },
132 { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x05,0x01,0x01,0x00 },
133 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x01,0x02,0x01,0x03 },
134 mxf_write_cdci_desc },
135 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x03,0x01 },
136 { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x06,0x01,0x10,0x00 },
137 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 },
138 mxf_write_generic_sound_desc },
139 // D-10 525/60 NTSC 40mb/s
140 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x04,0x01 },
141 { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x05,0x01,0x01,0x00 },
142 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x01,0x02,0x01,0x04 },
143 mxf_write_cdci_desc },
144 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x04,0x01 },
145 { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x06,0x01,0x10,0x00 },
146 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 },
147 mxf_write_generic_sound_desc },
148 // D-10 625/50 PAL 30mb/s
149 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x05,0x01 },
150 { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x05,0x01,0x01,0x00 },
151 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x01,0x02,0x01,0x05 },
152 mxf_write_cdci_desc },
153 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x05,0x01 },
154 { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x06,0x01,0x10,0x00 },
155 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 },
156 mxf_write_generic_sound_desc },
157 // D-10 525/60 NTSC 30mb/s
158 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x06,0x01 },
159 { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x05,0x01,0x01,0x00 },
160 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x01,0x02,0x01,0x06 },
161 mxf_write_cdci_desc },
162 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x06,0x01 },
163 { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x06,0x01,0x10,0x00 },
164 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 },
165 mxf_write_generic_sound_desc },
166 { { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 },
167 { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 },
168 { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 },
172 typedef struct MXFContext {
173 int64_t footer_partition_offset;
174 int essence_container_count;
175 AVRational time_base;
177 MXFIndexEntry *index_entries;
178 unsigned edit_units_count;
179 uint64_t timestamp; ///< timestamp, as year(16),month(8),day(8),hour(8),minutes(8),msec/4(8)
180 uint8_t slice_count; ///< index slice count minus 1 (1 if no audio, 0 otherwise)
181 int last_indexed_edit_unit;
182 uint64_t *body_partition_offset;
183 unsigned body_partitions_count;
184 int last_key_index; ///< index of last key frame
186 AVStream *timecode_track;
187 int timecode_base; ///< rounded time code base (25 or 30)
188 int timecode_start; ///< frame number computed from mpeg-2 gop header timecode
189 int timecode_drop_frame; ///< time code use drop frame method frop mpeg-2 essence gop header
190 int edit_unit_byte_count; ///< fixed edit unit byte count
191 uint64_t body_offset;
192 uint32_t instance_number;
193 uint8_t umid[16]; ///< unique material identifier
196 static const uint8_t uuid_base[] = { 0xAD,0xAB,0x44,0x24,0x2f,0x25,0x4d,0xc7,0x92,0xff,0x29,0xbd };
197 static const uint8_t umid_ul[] = { 0x06,0x0A,0x2B,0x34,0x01,0x01,0x01,0x05,0x01,0x01,0x0D,0x00,0x13 };
200 * complete key for operation pattern, partitions, and primer pack
202 static const uint8_t op1a_ul[] = { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x02,0x01,0x01,0x01,0x09,0x00 };
203 static const uint8_t footer_partition_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0D,0x01,0x02,0x01,0x01,0x04,0x04,0x00 }; // ClosedComplete
204 static const uint8_t primer_pack_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0D,0x01,0x02,0x01,0x01,0x05,0x01,0x00 };
205 static const uint8_t index_table_segment_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x10,0x01,0x00 };
206 static const uint8_t random_index_pack_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0D,0x01,0x02,0x01,0x01,0x11,0x01,0x00 };
207 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
208 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
209 static const uint8_t klv_fill_key[] = { 0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x03,0x01,0x02,0x10,0x01,0x00,0x00,0x00 };
210 static const uint8_t body_partition_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0D,0x01,0x02,0x01,0x01,0x03,0x04,0x00 }; // ClosedComplete
213 * partial key for header metadata
215 static const uint8_t header_metadata_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0D,0x01,0x01,0x01,0x01 };
216 static const uint8_t multiple_desc_ul[] = { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x0D,0x01,0x03,0x01,0x02,0x7F,0x01,0x00 };
219 * SMPTE RP210 http://www.smpte-ra.org/mdd/index.html
221 static const MXFLocalTagPair mxf_local_tag_batch[] = {
223 { 0x3C0A, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x01,0x01,0x15,0x02,0x00,0x00,0x00,0x00}}, /* Instance UID */
224 { 0x3B02, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x10,0x02,0x04,0x00,0x00}}, /* Last Modified Date */
225 { 0x3B05, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x03,0x01,0x02,0x01,0x05,0x00,0x00,0x00}}, /* Version */
226 { 0x3B06, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x06,0x04,0x00,0x00}}, /* Identifications reference */
227 { 0x3B03, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x02,0x01,0x00,0x00}}, /* Content Storage reference */
228 { 0x3B09, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x01,0x02,0x02,0x03,0x00,0x00,0x00,0x00}}, /* Operational Pattern UL */
229 { 0x3B0A, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x01,0x02,0x02,0x10,0x02,0x01,0x00,0x00}}, /* Essence Containers UL batch */
230 { 0x3B0B, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x01,0x02,0x02,0x10,0x02,0x02,0x00,0x00}}, /* DM Schemes UL batch */
232 { 0x3C09, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x20,0x07,0x01,0x01,0x00,0x00,0x00}}, /* This Generation UID */
233 { 0x3C01, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x20,0x07,0x01,0x02,0x01,0x00,0x00}}, /* Company Name */
234 { 0x3C02, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x20,0x07,0x01,0x03,0x01,0x00,0x00}}, /* Product Name */
235 { 0x3C04, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x20,0x07,0x01,0x05,0x01,0x00,0x00}}, /* Version String */
236 { 0x3C05, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x20,0x07,0x01,0x07,0x00,0x00,0x00}}, /* Product ID */
237 { 0x3C06, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x10,0x02,0x03,0x00,0x00}}, /* Modification Date */
239 { 0x1901, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x05,0x01,0x00,0x00}}, /* Package strong reference batch */
240 { 0x1902, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x05,0x02,0x00,0x00}}, /* Package strong reference batch */
241 // Essence Container Data
242 { 0x2701, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x06,0x01,0x00,0x00,0x00}}, /* Linked Package UID */
243 { 0x3F07, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x01,0x03,0x04,0x04,0x00,0x00,0x00,0x00}}, /* BodySID */
245 { 0x4401, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x01,0x01,0x15,0x10,0x00,0x00,0x00,0x00}}, /* Package UID */
246 { 0x4405, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x10,0x01,0x03,0x00,0x00}}, /* Package Creation Date */
247 { 0x4404, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x10,0x02,0x05,0x00,0x00}}, /* Package Modified Date */
248 { 0x4403, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x06,0x05,0x00,0x00}}, /* Tracks Strong reference array */
249 { 0x4701, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x02,0x03,0x00,0x00}}, /* Descriptor */
251 { 0x4801, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x01,0x07,0x01,0x01,0x00,0x00,0x00,0x00}}, /* Track ID */
252 { 0x4804, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x01,0x04,0x01,0x03,0x00,0x00,0x00,0x00}}, /* Track Number */
253 { 0x4B01, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x30,0x04,0x05,0x00,0x00,0x00,0x00}}, /* Edit Rate */
254 { 0x4B02, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x03,0x01,0x03,0x00,0x00}}, /* Origin */
255 { 0x4803, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x02,0x04,0x00,0x00}}, /* Sequence reference */
257 { 0x0201, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x07,0x01,0x00,0x00,0x00,0x00,0x00}}, /* Data Definition UL */
258 { 0x0202, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x02,0x01,0x01,0x03,0x00,0x00}}, /* Duration */
259 { 0x1001, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x06,0x09,0x00,0x00}}, /* Structural Components reference array */
261 { 0x1201, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x03,0x01,0x04,0x00,0x00}}, /* Start position */
262 { 0x1101, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x03,0x01,0x00,0x00,0x00}}, /* SourcePackageID */
263 { 0x1102, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x03,0x02,0x00,0x00,0x00}}, /* SourceTrackID */
264 // Timecode Component
265 { 0x1501, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x03,0x01,0x05,0x00,0x00}}, /* Start Time Code */
266 { 0x1502, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x04,0x01,0x01,0x02,0x06,0x00,0x00}}, /* Rounded Time Code Base */
267 { 0x1503, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x04,0x01,0x01,0x05,0x00,0x00,0x00}}, /* Drop Frame */
269 { 0x3F01, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x06,0x01,0x01,0x04,0x06,0x0B,0x00,0x00}}, /* Sub Descriptors reference array */
270 { 0x3006, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x06,0x01,0x01,0x03,0x05,0x00,0x00,0x00}}, /* Linked Track ID */
271 { 0x3001, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x06,0x01,0x01,0x00,0x00,0x00,0x00}}, /* SampleRate */
272 { 0x3004, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x01,0x02,0x00,0x00}}, /* Essence Container */
273 // Generic Picture Essence Descriptor
274 { 0x320C, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x03,0x01,0x04,0x00,0x00,0x00}}, /* Frame Layout */
275 { 0x320D, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x01,0x03,0x02,0x05,0x00,0x00,0x00}}, /* Video Line Map */
276 { 0x3203, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x02,0x02,0x00,0x00,0x00}}, /* Stored Width */
277 { 0x3202, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x02,0x01,0x00,0x00,0x00}}, /* Stored Height */
278 { 0x3209, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x01,0x0C,0x00,0x00,0x00}}, /* Display Width */
279 { 0x3208, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x01,0x0B,0x00,0x00,0x00}}, /* Display Height */
280 { 0x320E, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x01,0x01,0x01,0x00,0x00,0x00}}, /* Aspect Ratio */
281 { 0x3201, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x01,0x06,0x01,0x00,0x00,0x00,0x00}}, /* Picture Essence Coding */
282 // CDCI Picture Essence Descriptor
283 { 0x3301, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x01,0x05,0x03,0x0A,0x00,0x00,0x00}}, /* Component Depth */
284 { 0x3302, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x01,0x05,0x00,0x00,0x00}}, /* Horizontal Subsampling */
285 // Generic Sound Essence Descriptor
286 { 0x3D02, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x04,0x02,0x03,0x01,0x04,0x00,0x00,0x00}}, /* Locked/Unlocked */
287 { 0x3D03, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x02,0x03,0x01,0x01,0x01,0x00,0x00}}, /* Audio sampling rate */
288 { 0x3D07, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x02,0x01,0x01,0x04,0x00,0x00,0x00}}, /* ChannelCount */
289 { 0x3D01, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x04,0x02,0x03,0x03,0x04,0x00,0x00,0x00}}, /* Quantization bits */
290 { 0x3D06, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x02,0x04,0x02,0x00,0x00,0x00,0x00}}, /* Sound Essence Compression */
291 // Index Table Segment
292 { 0x3F0B, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x05,0x30,0x04,0x06,0x00,0x00,0x00,0x00}}, /* Index Edit Rate */
293 { 0x3F0C, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x07,0x02,0x01,0x03,0x01,0x0A,0x00,0x00}}, /* Index Start Position */
294 { 0x3F0D, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x07,0x02,0x02,0x01,0x01,0x02,0x00,0x00}}, /* Index Duration */
295 { 0x3F05, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x04,0x06,0x02,0x01,0x00,0x00,0x00,0x00}}, /* Edit Unit Byte Count */
296 { 0x3F06, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x01,0x03,0x04,0x05,0x00,0x00,0x00,0x00}}, /* IndexSID */
297 { 0x3F08, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x04,0x04,0x04,0x01,0x01,0x00,0x00,0x00}}, /* Slice Count */
298 { 0x3F09, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x04,0x04,0x01,0x06,0x00,0x00,0x00}}, /* Delta Entry Array */
299 { 0x3F0A, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x04,0x04,0x02,0x05,0x00,0x00,0x00}}, /* Index Entry Array */
300 // MPEG video Descriptor
301 { 0x8000, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x01,0x06,0x02,0x01,0x0B,0x00,0x00}}, /* BitRate */
302 { 0x8007, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x01,0x06,0x02,0x01,0x0A,0x00,0x00}}, /* ProfileAndLevel */
303 // Wave Audio Essence Descriptor
304 { 0x3D09, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x02,0x03,0x03,0x05,0x00,0x00,0x00}}, /* Average Bytes Per Second */
305 { 0x3D0A, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x02,0x03,0x02,0x01,0x00,0x00,0x00}}, /* Block Align */
308 static void mxf_write_uuid(ByteIOContext *pb, enum MXFMetadataSetType type, int value)
310 put_buffer(pb, uuid_base, 12);
315 static void mxf_write_umid(AVFormatContext *s, int type)
317 MXFContext *mxf = s->priv_data;
318 put_buffer(s->pb, umid_ul, 13);
319 put_be24(s->pb, mxf->instance_number);
320 put_buffer(s->pb, mxf->umid, 15);
321 put_byte(s->pb, type);
324 static void mxf_write_refs_count(ByteIOContext *pb, int ref_count)
326 put_be32(pb, ref_count);
330 static int klv_ber_length(uint64_t len)
335 return (av_log2(len) >> 3) + 2;
338 static int klv_encode_ber_length(ByteIOContext *pb, uint64_t len)
340 // Determine the best BER size
348 size = (av_log2(len) >> 3) + 1;
351 put_byte(pb, 0x80 + size);
354 put_byte(pb, len >> 8 * size & 0xff);
359 static void klv_encode_ber4_length(ByteIOContext *pb, int len)
361 put_byte(pb, 0x80 + 3);
366 * Get essence container ul index
368 static int mxf_get_essence_container_ul_index(enum CodecID id)
371 for (i = 0; mxf_essence_mappings[i].id; i++)
372 if (mxf_essence_mappings[i].id == id)
373 return mxf_essence_mappings[i].index;
377 static void mxf_write_primer_pack(AVFormatContext *s)
379 ByteIOContext *pb = s->pb;
380 int local_tag_number, i = 0;
382 local_tag_number = FF_ARRAY_ELEMS(mxf_local_tag_batch);
384 put_buffer(pb, primer_pack_key, 16);
385 klv_encode_ber_length(pb, local_tag_number * 18 + 8);
387 put_be32(pb, local_tag_number); // local_tag num
388 put_be32(pb, 18); // item size, always 18 according to the specs
390 for (i = 0; i < local_tag_number; i++) {
391 put_be16(pb, mxf_local_tag_batch[i].local_tag);
392 put_buffer(pb, mxf_local_tag_batch[i].uid, 16);
396 static void mxf_write_local_tag(ByteIOContext *pb, int size, int tag)
402 static void mxf_write_metadata_key(ByteIOContext *pb, unsigned int value)
404 put_buffer(pb, header_metadata_key, 13);
408 static void mxf_free(AVFormatContext *s)
412 for (i = 0; i < s->nb_streams; i++) {
413 AVStream *st = s->streams[i];
414 av_freep(&st->priv_data);
418 static const MXFCodecUL *mxf_get_data_definition_ul(int type)
420 const MXFCodecUL *uls = ff_mxf_data_definition_uls;
421 while (uls->uid[0]) {
429 static void mxf_write_essence_container_refs(AVFormatContext *s)
431 MXFContext *c = s->priv_data;
432 ByteIOContext *pb = s->pb;
435 mxf_write_refs_count(pb, c->essence_container_count);
436 av_log(s,AV_LOG_DEBUG, "essence container count:%d\n", c->essence_container_count);
437 for (i = 0; i < c->essence_container_count; i++) {
438 MXFStreamContext *sc = s->streams[i]->priv_data;
439 put_buffer(pb, mxf_essence_container_uls[sc->index].container_ul, 16);
443 static void mxf_write_preface(AVFormatContext *s)
445 MXFContext *mxf = s->priv_data;
446 ByteIOContext *pb = s->pb;
448 mxf_write_metadata_key(pb, 0x012f00);
449 PRINT_KEY(s, "preface key", pb->buf_ptr - 16);
450 klv_encode_ber_length(pb, 130 + 16 * mxf->essence_container_count);
452 // write preface set uid
453 mxf_write_local_tag(pb, 16, 0x3C0A);
454 mxf_write_uuid(pb, Preface, 0);
455 PRINT_KEY(s, "preface uid", pb->buf_ptr - 16);
457 // last modified date
458 mxf_write_local_tag(pb, 8, 0x3B02);
459 put_be64(pb, mxf->timestamp);
462 mxf_write_local_tag(pb, 2, 0x3B05);
463 put_be16(pb, 258); // v1.2
465 // write identification_refs
466 mxf_write_local_tag(pb, 16 + 8, 0x3B06);
467 mxf_write_refs_count(pb, 1);
468 mxf_write_uuid(pb, Identification, 0);
470 // write content_storage_refs
471 mxf_write_local_tag(pb, 16, 0x3B03);
472 mxf_write_uuid(pb, ContentStorage, 0);
474 // operational pattern
475 mxf_write_local_tag(pb, 16, 0x3B09);
476 put_buffer(pb, op1a_ul, 16);
478 // write essence_container_refs
479 mxf_write_local_tag(pb, 8 + 16 * mxf->essence_container_count, 0x3B0A);
480 mxf_write_essence_container_refs(s);
482 // write dm_scheme_refs
483 mxf_write_local_tag(pb, 8, 0x3B0B);
488 * Write a local tag containing an ascii string as utf-16
490 static void mxf_write_local_tag_utf16(ByteIOContext *pb, int tag, const char *value)
492 int i, size = strlen(value);
493 mxf_write_local_tag(pb, size*2, tag);
494 for (i = 0; i < size; i++)
495 put_be16(pb, value[i]);
498 static void mxf_write_identification(AVFormatContext *s)
500 MXFContext *mxf = s->priv_data;
501 ByteIOContext *pb = s->pb;
502 const char *company = "FFmpeg";
503 const char *product = "OP1a Muxer";
507 mxf_write_metadata_key(pb, 0x013000);
508 PRINT_KEY(s, "identification key", pb->buf_ptr - 16);
510 version = s->streams[0]->codec->flags & CODEC_FLAG_BITEXACT ?
511 "0.0.0" : AV_STRINGIFY(LIBAVFORMAT_VERSION);
512 length = 84 + (strlen(company)+strlen(product)+strlen(version))*2; // utf-16
513 klv_encode_ber_length(pb, length);
516 mxf_write_local_tag(pb, 16, 0x3C0A);
517 mxf_write_uuid(pb, Identification, 0);
518 PRINT_KEY(s, "identification uid", pb->buf_ptr - 16);
520 // write generation uid
521 mxf_write_local_tag(pb, 16, 0x3C09);
522 mxf_write_uuid(pb, Identification, 1);
524 mxf_write_local_tag_utf16(pb, 0x3C01, company); // Company Name
525 mxf_write_local_tag_utf16(pb, 0x3C02, product); // Product Name
526 mxf_write_local_tag_utf16(pb, 0x3C04, version); // Version String
529 mxf_write_local_tag(pb, 16, 0x3C05);
530 mxf_write_uuid(pb, Identification, 2);
533 mxf_write_local_tag(pb, 8, 0x3C06);
534 put_be64(pb, mxf->timestamp);
537 static void mxf_write_content_storage(AVFormatContext *s)
539 ByteIOContext *pb = s->pb;
541 mxf_write_metadata_key(pb, 0x011800);
542 PRINT_KEY(s, "content storage key", pb->buf_ptr - 16);
543 klv_encode_ber_length(pb, 92);
546 mxf_write_local_tag(pb, 16, 0x3C0A);
547 mxf_write_uuid(pb, ContentStorage, 0);
548 PRINT_KEY(s, "content storage uid", pb->buf_ptr - 16);
550 // write package reference
551 mxf_write_local_tag(pb, 16 * 2 + 8, 0x1901);
552 mxf_write_refs_count(pb, 2);
553 mxf_write_uuid(pb, MaterialPackage, 0);
554 mxf_write_uuid(pb, SourcePackage, 0);
556 // write essence container data
557 mxf_write_local_tag(pb, 8 + 16, 0x1902);
558 mxf_write_refs_count(pb, 1);
559 mxf_write_uuid(pb, EssenceContainerData, 0);
562 static void mxf_write_track(AVFormatContext *s, AVStream *st, enum MXFMetadataSetType type)
564 MXFContext *mxf = s->priv_data;
565 ByteIOContext *pb = s->pb;
566 MXFStreamContext *sc = st->priv_data;
568 mxf_write_metadata_key(pb, 0x013b00);
569 PRINT_KEY(s, "track key", pb->buf_ptr - 16);
570 klv_encode_ber_length(pb, 80);
573 mxf_write_local_tag(pb, 16, 0x3C0A);
574 mxf_write_uuid(pb, type == MaterialPackage ? Track : Track + TypeBottom, st->index);
575 PRINT_KEY(s, "track uid", pb->buf_ptr - 16);
578 mxf_write_local_tag(pb, 4, 0x4801);
579 put_be32(pb, st->index+2);
581 // write track number
582 mxf_write_local_tag(pb, 4, 0x4804);
583 if (type == MaterialPackage)
584 put_be32(pb, 0); // track number of material package is 0
586 put_buffer(pb, sc->track_essence_element_key + 12, 4);
588 mxf_write_local_tag(pb, 8, 0x4B01);
589 put_be32(pb, mxf->time_base.den);
590 put_be32(pb, mxf->time_base.num);
593 mxf_write_local_tag(pb, 8, 0x4B02);
596 // write sequence refs
597 mxf_write_local_tag(pb, 16, 0x4803);
598 mxf_write_uuid(pb, type == MaterialPackage ? Sequence: Sequence + TypeBottom, st->index);
601 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 };
603 static void mxf_write_common_fields(AVFormatContext *s, AVStream *st)
605 MXFContext *mxf = s->priv_data;
606 ByteIOContext *pb = s->pb;
608 // find data define uls
609 mxf_write_local_tag(pb, 16, 0x0201);
610 if (st == mxf->timecode_track)
611 put_buffer(pb, smpte_12m_timecode_track_data_ul, 16);
613 const MXFCodecUL *data_def_ul = mxf_get_data_definition_ul(st->codec->codec_type);
614 put_buffer(pb, data_def_ul->uid, 16);
618 mxf_write_local_tag(pb, 8, 0x0202);
619 put_be64(pb, mxf->duration);
622 static void mxf_write_sequence(AVFormatContext *s, AVStream *st, enum MXFMetadataSetType type)
624 MXFContext *mxf = s->priv_data;
625 ByteIOContext *pb = s->pb;
626 enum MXFMetadataSetType component;
628 mxf_write_metadata_key(pb, 0x010f00);
629 PRINT_KEY(s, "sequence key", pb->buf_ptr - 16);
630 klv_encode_ber_length(pb, 80);
632 mxf_write_local_tag(pb, 16, 0x3C0A);
633 mxf_write_uuid(pb, type == MaterialPackage ? Sequence: Sequence + TypeBottom, st->index);
635 PRINT_KEY(s, "sequence uid", pb->buf_ptr - 16);
636 mxf_write_common_fields(s, st);
638 // write structural component
639 mxf_write_local_tag(pb, 16 + 8, 0x1001);
640 mxf_write_refs_count(pb, 1);
641 if (st == mxf->timecode_track)
642 component = TimecodeComponent;
644 component = SourceClip;
645 if (type == SourcePackage)
646 component += TypeBottom;
647 mxf_write_uuid(pb, component, st->index);
650 static void mxf_write_timecode_component(AVFormatContext *s, AVStream *st, enum MXFMetadataSetType type)
652 MXFContext *mxf = s->priv_data;
653 ByteIOContext *pb = s->pb;
655 mxf_write_metadata_key(pb, 0x011400);
656 klv_encode_ber_length(pb, 75);
659 mxf_write_local_tag(pb, 16, 0x3C0A);
660 mxf_write_uuid(pb, type == MaterialPackage ? TimecodeComponent :
661 TimecodeComponent + TypeBottom, st->index);
663 mxf_write_common_fields(s, st);
666 mxf_write_local_tag(pb, 8, 0x1501);
667 put_be64(pb, mxf->timecode_start);
669 // Rounded Time Code Base
670 mxf_write_local_tag(pb, 2, 0x1502);
671 put_be16(pb, mxf->timecode_base);
674 mxf_write_local_tag(pb, 1, 0x1503);
675 put_byte(pb, mxf->timecode_drop_frame);
678 static void mxf_write_structural_component(AVFormatContext *s, AVStream *st, enum MXFMetadataSetType type)
680 ByteIOContext *pb = s->pb;
683 mxf_write_metadata_key(pb, 0x011100);
684 PRINT_KEY(s, "sturctural component key", pb->buf_ptr - 16);
685 klv_encode_ber_length(pb, 108);
688 mxf_write_local_tag(pb, 16, 0x3C0A);
689 mxf_write_uuid(pb, type == MaterialPackage ? SourceClip: SourceClip + TypeBottom, st->index);
691 PRINT_KEY(s, "structural component uid", pb->buf_ptr - 16);
692 mxf_write_common_fields(s, st);
694 // write start_position
695 mxf_write_local_tag(pb, 8, 0x1201);
698 // write source package uid, end of the reference
699 mxf_write_local_tag(pb, 32, 0x1101);
700 if (type == SourcePackage) {
701 for (i = 0; i < 4; i++)
704 mxf_write_umid(s, 1);
706 // write source track id
707 mxf_write_local_tag(pb, 4, 0x1102);
708 if (type == SourcePackage)
711 put_be32(pb, st->index+2);
714 static void mxf_write_multi_descriptor(AVFormatContext *s)
716 MXFContext *mxf = s->priv_data;
717 ByteIOContext *pb = s->pb;
721 mxf_write_metadata_key(pb, 0x014400);
722 PRINT_KEY(s, "multiple descriptor key", pb->buf_ptr - 16);
723 klv_encode_ber_length(pb, 64 + 16 * s->nb_streams);
725 mxf_write_local_tag(pb, 16, 0x3C0A);
726 mxf_write_uuid(pb, MultipleDescriptor, 0);
727 PRINT_KEY(s, "multi_desc uid", pb->buf_ptr - 16);
730 mxf_write_local_tag(pb, 8, 0x3001);
731 put_be32(pb, mxf->time_base.den);
732 put_be32(pb, mxf->time_base.num);
734 // write essence container ul
735 mxf_write_local_tag(pb, 16, 0x3004);
736 if (mxf->essence_container_count > 1)
737 ul = multiple_desc_ul;
739 MXFStreamContext *sc = s->streams[0]->priv_data;
740 ul = mxf_essence_container_uls[sc->index].container_ul;
742 put_buffer(pb, ul, 16);
744 // write sub descriptor refs
745 mxf_write_local_tag(pb, s->nb_streams * 16 + 8, 0x3F01);
746 mxf_write_refs_count(pb, s->nb_streams);
747 for (i = 0; i < s->nb_streams; i++)
748 mxf_write_uuid(pb, SubDescriptor, i);
751 static void mxf_write_generic_desc(AVFormatContext *s, AVStream *st, const UID key, unsigned size)
753 MXFContext *mxf = s->priv_data;
754 MXFStreamContext *sc = st->priv_data;
755 ByteIOContext *pb = s->pb;
757 put_buffer(pb, key, 16);
758 klv_encode_ber4_length(pb, size+20+8+12+20);
760 mxf_write_local_tag(pb, 16, 0x3C0A);
761 mxf_write_uuid(pb, SubDescriptor, st->index);
763 mxf_write_local_tag(pb, 4, 0x3006);
764 put_be32(pb, st->index+2);
766 mxf_write_local_tag(pb, 8, 0x3001);
767 put_be32(pb, mxf->time_base.den);
768 put_be32(pb, mxf->time_base.num);
770 mxf_write_local_tag(pb, 16, 0x3004);
771 put_buffer(pb, mxf_essence_container_uls[sc->index].container_ul, 16);
774 static const UID mxf_mpegvideo_descriptor_key = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x51,0x00 };
775 static const UID mxf_wav_descriptor_key = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x48,0x00 };
776 static const UID mxf_aes3_descriptor_key = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x47,0x00 };
777 static const UID mxf_cdci_descriptor_key = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0D,0x01,0x01,0x01,0x01,0x01,0x28,0x00 };
778 static const UID mxf_generic_sound_descriptor_key = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0D,0x01,0x01,0x01,0x01,0x01,0x42,0x00 };
780 static void mxf_write_cdci_common(AVFormatContext *s, AVStream *st, const UID key, unsigned size)
782 MXFStreamContext *sc = st->priv_data;
783 ByteIOContext *pb = s->pb;
784 int stored_height = (st->codec->height+15)/16*16;
788 mxf_write_generic_desc(s, st, key, size+8+8+8+8+8+8+5+16+sc->interlaced*4+12+20);
790 mxf_write_local_tag(pb, 4, 0x3203);
791 put_be32(pb, st->codec->width);
793 mxf_write_local_tag(pb, 4, 0x3202);
794 put_be32(pb, stored_height>>sc->interlaced);
796 mxf_write_local_tag(pb, 4, 0x3209);
797 put_be32(pb, st->codec->width);
799 if (st->codec->height == 608) // PAL + VBI
800 display_height = 576;
801 else if (st->codec->height == 512) // NTSC + VBI
802 display_height = 486;
804 display_height = st->codec->height;
806 mxf_write_local_tag(pb, 4, 0x3208);
807 put_be32(pb, display_height>>sc->interlaced);
810 mxf_write_local_tag(pb, 4, 0x3301);
813 // horizontal subsampling
814 mxf_write_local_tag(pb, 4, 0x3302);
818 mxf_write_local_tag(pb, 1, 0x320C);
819 put_byte(pb, sc->interlaced);
822 switch (st->codec->height) {
823 case 576: f1 = 23; f2 = 336; break;
824 case 608: f1 = 7; f2 = 320; break;
825 case 480: f1 = 20; f2 = 283; break;
826 case 512: f1 = 7; f2 = 270; break;
827 case 720: f1 = 26; f2 = 0; break; // progressive
828 case 1080: f1 = 21; f2 = 584; break;
829 default: f1 = 0; f2 = 0; break;
832 if (!sc->interlaced) {
837 mxf_write_local_tag(pb, 12+sc->interlaced*4, 0x320D);
838 put_be32(pb, sc->interlaced ? 2 : 1);
844 mxf_write_local_tag(pb, 8, 0x320E);
845 put_be32(pb, sc->aspect_ratio.num);
846 put_be32(pb, sc->aspect_ratio.den);
848 mxf_write_local_tag(pb, 16, 0x3201);
849 put_buffer(pb, *sc->codec_ul, 16);
852 static void mxf_write_cdci_desc(AVFormatContext *s, AVStream *st)
854 mxf_write_cdci_common(s, st, mxf_cdci_descriptor_key, 0);
857 static void mxf_write_mpegvideo_desc(AVFormatContext *s, AVStream *st)
859 ByteIOContext *pb = s->pb;
860 int profile_and_level = (st->codec->profile<<4) | st->codec->level;
862 mxf_write_cdci_common(s, st, mxf_mpegvideo_descriptor_key, 8+5);
865 mxf_write_local_tag(pb, 4, 0x8000);
866 put_be32(pb, st->codec->bit_rate);
869 mxf_write_local_tag(pb, 1, 0x8007);
870 if (!st->codec->profile)
871 profile_and_level |= 0x80; // escape bit
872 put_byte(pb, profile_and_level);
875 static void mxf_write_generic_sound_common(AVFormatContext *s, AVStream *st, const UID key, unsigned size)
877 ByteIOContext *pb = s->pb;
879 mxf_write_generic_desc(s, st, key, size+5+12+8+8);
882 mxf_write_local_tag(pb, 1, 0x3D02);
885 // write audio sampling rate
886 mxf_write_local_tag(pb, 8, 0x3D03);
887 put_be32(pb, st->codec->sample_rate);
890 mxf_write_local_tag(pb, 4, 0x3D07);
891 put_be32(pb, st->codec->channels);
893 mxf_write_local_tag(pb, 4, 0x3D01);
894 put_be32(pb, av_get_bits_per_sample(st->codec->codec_id));
897 static void mxf_write_wav_common(AVFormatContext *s, AVStream *st, const UID key, unsigned size)
899 ByteIOContext *pb = s->pb;
901 mxf_write_generic_sound_common(s, st, key, size+6+8);
903 mxf_write_local_tag(pb, 2, 0x3D0A);
904 put_be16(pb, st->codec->block_align);
907 mxf_write_local_tag(pb, 4, 0x3D09);
908 put_be32(pb, st->codec->block_align*st->codec->sample_rate);
911 static void mxf_write_wav_desc(AVFormatContext *s, AVStream *st)
913 mxf_write_wav_common(s, st, mxf_wav_descriptor_key, 0);
916 static void mxf_write_aes3_desc(AVFormatContext *s, AVStream *st)
918 mxf_write_wav_common(s, st, mxf_aes3_descriptor_key, 0);
921 static void mxf_write_generic_sound_desc(AVFormatContext *s, AVStream *st)
923 mxf_write_generic_sound_common(s, st, mxf_generic_sound_descriptor_key, 0);
926 static void mxf_write_package(AVFormatContext *s, enum MXFMetadataSetType type)
928 MXFContext *mxf = s->priv_data;
929 ByteIOContext *pb = s->pb;
930 int i, track_count = s->nb_streams+1;
932 if (type == MaterialPackage) {
933 mxf_write_metadata_key(pb, 0x013600);
934 PRINT_KEY(s, "Material Package key", pb->buf_ptr - 16);
935 klv_encode_ber_length(pb, 92 + 16*track_count);
937 mxf_write_metadata_key(pb, 0x013700);
938 PRINT_KEY(s, "Source Package key", pb->buf_ptr - 16);
939 klv_encode_ber_length(pb, 112 + 16*track_count); // 20 bytes length for descriptor reference
943 mxf_write_local_tag(pb, 16, 0x3C0A);
944 mxf_write_uuid(pb, type, 0);
945 av_log(s,AV_LOG_DEBUG, "package type:%d\n", type);
946 PRINT_KEY(s, "package uid", pb->buf_ptr - 16);
948 // write package umid
949 mxf_write_local_tag(pb, 32, 0x4401);
950 mxf_write_umid(s, type == SourcePackage);
951 PRINT_KEY(s, "package umid second part", pb->buf_ptr - 16);
953 // package creation date
954 mxf_write_local_tag(pb, 8, 0x4405);
955 put_be64(pb, mxf->timestamp);
957 // package modified date
958 mxf_write_local_tag(pb, 8, 0x4404);
959 put_be64(pb, mxf->timestamp);
962 mxf_write_local_tag(pb, track_count*16 + 8, 0x4403);
963 mxf_write_refs_count(pb, track_count);
964 mxf_write_uuid(pb, type == MaterialPackage ? Track :
965 Track + TypeBottom, -1); // timecode track
966 for (i = 0; i < s->nb_streams; i++)
967 mxf_write_uuid(pb, type == MaterialPackage ? Track : Track + TypeBottom, i);
969 // write multiple descriptor reference
970 if (type == SourcePackage) {
971 mxf_write_local_tag(pb, 16, 0x4701);
972 if (s->nb_streams > 1) {
973 mxf_write_uuid(pb, MultipleDescriptor, 0);
974 mxf_write_multi_descriptor(s);
976 mxf_write_uuid(pb, SubDescriptor, 0);
979 // write timecode track
980 mxf_write_track(s, mxf->timecode_track, type);
981 mxf_write_sequence(s, mxf->timecode_track, type);
982 mxf_write_timecode_component(s, mxf->timecode_track, type);
984 for (i = 0; i < s->nb_streams; i++) {
985 AVStream *st = s->streams[i];
986 mxf_write_track(s, st, type);
987 mxf_write_sequence(s, st, type);
988 mxf_write_structural_component(s, st, type);
990 if (type == SourcePackage) {
991 MXFStreamContext *sc = st->priv_data;
992 mxf_essence_container_uls[sc->index].write_desc(s, st);
997 static int mxf_write_essence_container_data(AVFormatContext *s)
999 ByteIOContext *pb = s->pb;
1001 mxf_write_metadata_key(pb, 0x012300);
1002 klv_encode_ber_length(pb, 72);
1004 mxf_write_local_tag(pb, 16, 0x3C0A); // Instance UID
1005 mxf_write_uuid(pb, EssenceContainerData, 0);
1007 mxf_write_local_tag(pb, 32, 0x2701); // Linked Package UID
1008 mxf_write_umid(s, 1);
1010 mxf_write_local_tag(pb, 4, 0x3F07); // BodySID
1013 mxf_write_local_tag(pb, 4, 0x3F06); // IndexSID
1019 static int mxf_write_header_metadata_sets(AVFormatContext *s)
1021 mxf_write_preface(s);
1022 mxf_write_identification(s);
1023 mxf_write_content_storage(s);
1024 mxf_write_package(s, MaterialPackage);
1025 mxf_write_package(s, SourcePackage);
1026 mxf_write_essence_container_data(s);
1030 static unsigned klv_fill_size(uint64_t size)
1032 unsigned pad = KAG_SIZE - (size & (KAG_SIZE-1));
1033 if (pad < 20) // smallest fill item possible
1034 return pad + KAG_SIZE;
1036 return pad & (KAG_SIZE-1);
1039 static void mxf_write_index_table_segment(AVFormatContext *s)
1041 MXFContext *mxf = s->priv_data;
1042 ByteIOContext *pb = s->pb;
1043 int i, j, temporal_reordering = 0;
1044 int key_index = mxf->last_key_index;
1046 av_log(s, AV_LOG_DEBUG, "edit units count %d\n", mxf->edit_units_count);
1048 if (!mxf->edit_units_count && !mxf->edit_unit_byte_count)
1051 put_buffer(pb, index_table_segment_key, 16);
1053 if (mxf->edit_unit_byte_count) {
1054 klv_encode_ber_length(pb, 80);
1056 klv_encode_ber_length(pb, 85 + 12+(s->nb_streams+1)*6 +
1057 12+mxf->edit_units_count*(11+mxf->slice_count*4));
1061 mxf_write_local_tag(pb, 16, 0x3C0A);
1062 mxf_write_uuid(pb, IndexTableSegment, 0);
1065 mxf_write_local_tag(pb, 8, 0x3F0B);
1066 put_be32(pb, mxf->time_base.den);
1067 put_be32(pb, mxf->time_base.num);
1069 // index start position
1070 mxf_write_local_tag(pb, 8, 0x3F0C);
1071 put_be64(pb, mxf->last_indexed_edit_unit);
1074 mxf_write_local_tag(pb, 8, 0x3F0D);
1075 if (mxf->edit_unit_byte_count)
1076 put_be64(pb, 0); // index table covers whole container
1078 put_be64(pb, mxf->edit_units_count);
1080 // edit unit byte count
1081 mxf_write_local_tag(pb, 4, 0x3F05);
1082 put_be32(pb, mxf->edit_unit_byte_count);
1085 mxf_write_local_tag(pb, 4, 0x3F06);
1089 mxf_write_local_tag(pb, 4, 0x3F07);
1092 if (!mxf->edit_unit_byte_count) {
1093 // real slice count - 1
1094 mxf_write_local_tag(pb, 1, 0x3F08);
1095 put_byte(pb, mxf->slice_count);
1097 // delta entry array
1098 mxf_write_local_tag(pb, 8 + (s->nb_streams+1)*6, 0x3F09);
1099 put_be32(pb, s->nb_streams+1); // num of entries
1100 put_be32(pb, 6); // size of one entry
1101 // write system item delta entry
1103 put_byte(pb, 0); // slice entry
1104 put_be32(pb, 0); // element delta
1105 for (i = 0; i < s->nb_streams; i++) {
1106 AVStream *st = s->streams[i];
1107 MXFStreamContext *sc = st->priv_data;
1108 put_byte(pb, sc->temporal_reordering);
1109 if (sc->temporal_reordering)
1110 temporal_reordering = 1;
1111 if (i == 0) { // video track
1112 put_byte(pb, 0); // slice number
1113 put_be32(pb, KAG_SIZE); // system item size including klv fill
1114 } else { // audio track
1115 unsigned audio_frame_size = sc->aic.samples[0]*sc->aic.sample_size;
1116 audio_frame_size += klv_fill_size(audio_frame_size);
1118 put_be32(pb, (i-1)*audio_frame_size); // element delta
1122 mxf_write_local_tag(pb, 8 + mxf->edit_units_count*(11+mxf->slice_count*4), 0x3F0A);
1123 put_be32(pb, mxf->edit_units_count); // num of entries
1124 put_be32(pb, 11+mxf->slice_count*4); // size of one entry
1126 for (i = 0; i < mxf->edit_units_count; i++) {
1127 int temporal_offset = 0;
1129 if (!(mxf->index_entries[i].flags & 0x33)) { // I frame
1130 mxf->last_key_index = key_index;
1134 if (temporal_reordering) {
1135 int pic_num_in_gop = i - key_index;
1136 if (pic_num_in_gop != mxf->index_entries[i].temporal_ref) {
1137 for (j = key_index; j < mxf->edit_units_count; j++) {
1138 if (pic_num_in_gop == mxf->index_entries[j].temporal_ref)
1141 if (j == mxf->edit_units_count)
1142 av_log(s, AV_LOG_WARNING, "missing frames\n");
1143 temporal_offset = j - key_index - pic_num_in_gop;
1146 put_byte(pb, temporal_offset);
1148 if ((mxf->index_entries[i].flags & 0x30) == 0x30) { // back and forward prediction
1149 put_byte(pb, mxf->last_key_index - i);
1151 put_byte(pb, key_index - i); // key frame offset
1152 if ((mxf->index_entries[i].flags & 0x20) == 0x20) // only forward
1153 mxf->last_key_index = key_index;
1156 if (!(mxf->index_entries[i].flags & 0x33) && // I frame
1157 mxf->index_entries[i].flags & 0x40 && !temporal_offset)
1158 mxf->index_entries[i].flags |= 0x80; // random access
1159 put_byte(pb, mxf->index_entries[i].flags);
1161 put_be64(pb, mxf->index_entries[i].offset);
1162 if (s->nb_streams > 1)
1163 put_be32(pb, mxf->index_entries[i].slice_offset);
1166 mxf->last_key_index = key_index - mxf->edit_units_count;
1167 mxf->last_indexed_edit_unit += mxf->edit_units_count;
1168 mxf->edit_units_count = 0;
1172 static void mxf_write_klv_fill(AVFormatContext *s)
1174 unsigned pad = klv_fill_size(url_ftell(s->pb));
1176 put_buffer(s->pb, klv_fill_key, 16);
1178 klv_encode_ber4_length(s->pb, pad);
1181 assert(!(url_ftell(s->pb) & (KAG_SIZE-1)));
1185 static void mxf_write_partition(AVFormatContext *s, int bodysid,
1187 const uint8_t *key, int write_metadata)
1189 MXFContext *mxf = s->priv_data;
1190 ByteIOContext *pb = s->pb;
1191 int64_t header_byte_count_offset;
1192 unsigned index_byte_count = 0;
1193 uint64_t partition_offset = url_ftell(pb);
1195 if (!mxf->edit_unit_byte_count && mxf->edit_units_count)
1196 index_byte_count = 85 + 12+(s->nb_streams+1)*6 +
1197 12+mxf->edit_units_count*(11+mxf->slice_count*4);
1198 else if (mxf->edit_unit_byte_count && indexsid)
1199 index_byte_count = 80;
1201 if (index_byte_count) {
1202 // add encoded ber length
1203 index_byte_count += 16 + klv_ber_length(index_byte_count);
1204 index_byte_count += klv_fill_size(index_byte_count);
1207 if (!memcmp(key, body_partition_key, 16)) {
1208 mxf->body_partition_offset =
1209 av_realloc(mxf->body_partition_offset,
1210 (mxf->body_partitions_count+1)*
1211 sizeof(*mxf->body_partition_offset));
1212 mxf->body_partition_offset[mxf->body_partitions_count++] = partition_offset;
1216 put_buffer(pb, key, 16);
1217 klv_encode_ber_length(pb, 88 + 16 * mxf->essence_container_count);
1219 // write partition value
1220 put_be16(pb, 1); // majorVersion
1221 put_be16(pb, 2); // minorVersion
1222 put_be32(pb, KAG_SIZE); // KAGSize
1224 put_be64(pb, partition_offset); // ThisPartition
1226 if (!memcmp(key, body_partition_key, 16) && mxf->body_partitions_count > 1)
1227 put_be64(pb, mxf->body_partition_offset[mxf->body_partitions_count-2]); // PreviousPartition
1228 else if (!memcmp(key, footer_partition_key, 16) && mxf->body_partitions_count)
1229 put_be64(pb, mxf->body_partition_offset[mxf->body_partitions_count-1]); // PreviousPartition
1233 put_be64(pb, mxf->footer_partition_offset); // footerPartition
1236 header_byte_count_offset = url_ftell(pb);
1237 put_be64(pb, 0); // headerByteCount, update later
1240 put_be64(pb, index_byte_count); // indexByteCount
1241 put_be32(pb, index_byte_count ? indexsid : 0); // indexSID
1244 if (bodysid && mxf->edit_units_count && mxf->body_partitions_count) {
1245 put_be64(pb, mxf->body_offset);
1249 put_be32(pb, bodysid); // bodySID
1251 // operational pattern
1252 put_buffer(pb, op1a_ul, 16);
1254 // essence container
1255 mxf_write_essence_container_refs(s);
1257 if (write_metadata) {
1258 // mark the start of the headermetadata and calculate metadata size
1260 unsigned header_byte_count;
1262 mxf_write_klv_fill(s);
1263 start = url_ftell(s->pb);
1264 mxf_write_primer_pack(s);
1265 mxf_write_header_metadata_sets(s);
1266 pos = url_ftell(s->pb);
1267 header_byte_count = pos - start + klv_fill_size(pos);
1269 // update header_byte_count
1270 url_fseek(pb, header_byte_count_offset, SEEK_SET);
1271 put_be64(pb, header_byte_count);
1272 url_fseek(pb, pos, SEEK_SET);
1275 put_flush_packet(pb);
1278 static const UID mxf_mpeg2_codec_uls[] = {
1279 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x01,0x10,0x00 }, // MP-ML I-Frame
1280 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x01,0x11,0x00 }, // MP-ML Long GOP
1281 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x02,0x02,0x00 }, // 422P-ML I-Frame
1282 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x02,0x03,0x00 }, // 422P-ML Long GOP
1283 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x03,0x02,0x00 }, // MP-HL I-Frame
1284 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x03,0x03,0x00 }, // MP-HL Long GOP
1285 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x04,0x02,0x00 }, // 422P-HL I-Frame
1286 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x04,0x03,0x00 }, // 422P-HL Long GOP
1289 static const UID *mxf_get_mpeg2_codec_ul(AVCodecContext *avctx)
1291 int long_gop = avctx->gop_size > 1 || avctx->has_b_frames;
1293 if (avctx->profile == 4) { // Main
1294 if (avctx->level == 8) // Main
1295 return &mxf_mpeg2_codec_uls[0+long_gop];
1296 else if (avctx->level == 4) // High
1297 return &mxf_mpeg2_codec_uls[4+long_gop];
1298 } else if (avctx->profile == 0) { // 422
1299 if (avctx->level == 5) // Main
1300 return &mxf_mpeg2_codec_uls[2+long_gop];
1301 else if (avctx->level == 2) // High
1302 return &mxf_mpeg2_codec_uls[6+long_gop];
1307 static int mxf_parse_mpeg2_frame(AVFormatContext *s, AVStream *st,
1308 AVPacket *pkt, MXFIndexEntry *e)
1310 MXFStreamContext *sc = st->priv_data;
1311 MXFContext *mxf = s->priv_data;
1315 for(i = 0; i < pkt->size - 4; i++) {
1316 c = (c<<8) + pkt->data[i];
1318 if ((pkt->data[i+1] & 0xf0) == 0x10) { // seq ext
1319 st->codec->profile = pkt->data[i+1] & 0x07;
1320 st->codec->level = pkt->data[i+2] >> 4;
1321 } else if (i + 5 < pkt->size && (pkt->data[i+1] & 0xf0) == 0x80) { // pict coding ext
1322 sc->interlaced = !(pkt->data[i+5] & 0x80); // progressive frame
1325 } else if (c == 0x1b8) { // gop
1326 if (pkt->data[i+4]>>6 & 0x01) { // closed
1328 if (e->flags & 0x40) // sequence header present
1329 e->flags |= 0x80; // random access
1331 if (!mxf->header_written) {
1332 unsigned hours = (pkt->data[i+1]>>2) & 0x1f;
1333 unsigned minutes = ((pkt->data[i+1] & 0x03) << 4) | (pkt->data[i+2]>>4);
1334 unsigned seconds = ((pkt->data[i+2] & 0x07) << 3) | (pkt->data[i+3]>>5);
1335 unsigned frames = ((pkt->data[i+3] & 0x1f) << 1) | (pkt->data[i+4]>>7);
1336 mxf->timecode_drop_frame = !!(pkt->data[i+1] & 0x80);
1337 mxf->timecode_start = (hours*3600 + minutes*60 + seconds) *
1338 mxf->timecode_base + frames;
1339 if (mxf->timecode_drop_frame) {
1340 unsigned tminutes = 60 * hours + minutes;
1341 mxf->timecode_start -= 2 * (tminutes - tminutes / 10);
1343 av_log(s, AV_LOG_DEBUG, "frame %d %d:%d:%d%c%d\n", mxf->timecode_start,
1344 hours, minutes, seconds, mxf->timecode_drop_frame ? ';':':', frames);
1346 } else if (c == 0x1b3) { // seq
1348 switch ((pkt->data[i+4]>>4) & 0xf) {
1349 case 2: sc->aspect_ratio = (AVRational){ 4, 3}; break;
1350 case 3: sc->aspect_ratio = (AVRational){ 16, 9}; break;
1351 case 4: sc->aspect_ratio = (AVRational){221,100}; break;
1353 av_reduce(&sc->aspect_ratio.num, &sc->aspect_ratio.den,
1354 st->codec->width, st->codec->height, 1024*1024);
1356 } else if (c == 0x100) { // pic
1357 int pict_type = (pkt->data[i+2]>>3) & 0x07;
1358 e->temporal_ref = (pkt->data[i+1]<<2) | (pkt->data[i+2]>>6);
1359 if (pict_type == 2) { // P frame
1361 sc->closed_gop = 0; // reset closed gop, don't matter anymore
1362 } else if (pict_type == 3) { // B frame
1364 e->flags |= 0x13; // only backward prediction
1367 sc->temporal_reordering = -1;
1368 } else if (!pict_type) {
1369 av_log(s, AV_LOG_ERROR, "error parsing mpeg2 frame\n");
1374 if (s->oformat != &mxf_d10_muxer)
1375 sc->codec_ul = mxf_get_mpeg2_codec_ul(st->codec);
1376 return !!sc->codec_ul;
1379 static uint64_t mxf_parse_timestamp(time_t timestamp)
1381 struct tm *time = gmtime(×tamp);
1382 return (uint64_t)(time->tm_year+1900) << 48 |
1383 (uint64_t)(time->tm_mon+1) << 40 |
1384 (uint64_t) time->tm_mday << 32 |
1385 time->tm_hour << 24 |
1386 time->tm_min << 16 |
1390 static void mxf_gen_umid(AVFormatContext *s)
1392 MXFContext *mxf = s->priv_data;
1393 uint32_t seed = av_get_random_seed();
1394 uint64_t umid = seed + 0x5294713400000000LL;
1396 AV_WB64(mxf->umid , umid);
1397 AV_WB64(mxf->umid+8, umid>>8);
1399 mxf->instance_number = seed;
1402 static int mxf_write_header(AVFormatContext *s)
1404 MXFContext *mxf = s->priv_data;
1406 uint8_t present[FF_ARRAY_ELEMS(mxf_essence_container_uls)] = {0};
1407 const int *samples_per_frame = NULL;
1412 for (i = 0; i < s->nb_streams; i++) {
1413 AVStream *st = s->streams[i];
1414 MXFStreamContext *sc = av_mallocz(sizeof(*sc));
1416 return AVERROR(ENOMEM);
1419 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
1421 av_log(s, AV_LOG_ERROR, "video stream must be first track\n");
1424 if (fabs(av_q2d(st->codec->time_base) - 1/25.0) < 0.0001) {
1425 samples_per_frame = PAL_samples_per_frame;
1426 mxf->time_base = (AVRational){ 1, 25 };
1427 mxf->timecode_base = 25;
1428 } else if (fabs(av_q2d(st->codec->time_base) - 1001/30000.0) < 0.0001) {
1429 samples_per_frame = NTSC_samples_per_frame;
1430 mxf->time_base = (AVRational){ 1001, 30000 };
1431 mxf->timecode_base = 30;
1433 av_log(s, AV_LOG_ERROR, "unsupported video frame rate\n");
1436 av_set_pts_info(st, 64, mxf->time_base.num, mxf->time_base.den);
1437 if (s->oformat == &mxf_d10_muxer) {
1438 if (st->codec->bit_rate == 50000000)
1439 if (mxf->time_base.den == 25) sc->index = 3;
1441 else if (st->codec->bit_rate == 40000000)
1442 if (mxf->time_base.den == 25) sc->index = 7;
1444 else if (st->codec->bit_rate == 30000000)
1445 if (mxf->time_base.den == 25) sc->index = 11;
1446 else sc->index = 13;
1448 av_log(s, AV_LOG_ERROR, "error MXF D-10 only support 30/40/50 mbit/s\n");
1452 mxf->edit_unit_byte_count = KAG_SIZE; // system element
1453 mxf->edit_unit_byte_count += 16 + 4 + (uint64_t)st->codec->bit_rate *
1454 mxf->time_base.num / (8*mxf->time_base.den);
1455 mxf->edit_unit_byte_count += klv_fill_size(mxf->edit_unit_byte_count);
1456 mxf->edit_unit_byte_count += 16 + 4 + 4 + samples_per_frame[0]*8*4;
1457 mxf->edit_unit_byte_count += klv_fill_size(mxf->edit_unit_byte_count);
1459 } else if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
1460 if (st->codec->sample_rate != 48000) {
1461 av_log(s, AV_LOG_ERROR, "only 48khz is implemented\n");
1464 av_set_pts_info(st, 64, 1, st->codec->sample_rate);
1465 if (s->oformat == &mxf_d10_muxer) {
1466 if (st->index != 1) {
1467 av_log(s, AV_LOG_ERROR, "MXF D-10 only support one audio track\n");
1470 if (st->codec->codec_id != CODEC_ID_PCM_S16LE &&
1471 st->codec->codec_id != CODEC_ID_PCM_S24LE) {
1472 av_log(s, AV_LOG_ERROR, "MXF D-10 only support 16 or 24 bits le audio\n");
1474 sc->index = ((MXFStreamContext*)s->streams[0]->priv_data)->index + 1;
1476 mxf->slice_count = 1;
1480 sc->index = mxf_get_essence_container_ul_index(st->codec->codec_id);
1481 if (sc->index == -1) {
1482 av_log(s, AV_LOG_ERROR, "track %d: could not find essence container ul, "
1483 "codec not currently supported in container\n", i);
1488 sc->codec_ul = &mxf_essence_container_uls[sc->index].codec_ul;
1490 memcpy(sc->track_essence_element_key, mxf_essence_container_uls[sc->index].element_ul, 15);
1491 sc->track_essence_element_key[15] = present[sc->index];
1492 PRINT_KEY(s, "track essence element key", sc->track_essence_element_key);
1494 if (!present[sc->index])
1495 mxf->essence_container_count++;
1496 present[sc->index]++;
1499 if (s->oformat == &mxf_d10_muxer) {
1500 mxf->essence_container_count = 1;
1503 if (!(s->streams[0]->codec->flags & CODEC_FLAG_BITEXACT))
1506 for (i = 0; i < s->nb_streams; i++) {
1507 MXFStreamContext *sc = s->streams[i]->priv_data;
1508 // update element count
1509 sc->track_essence_element_key[13] = present[sc->index];
1510 sc->order = AV_RB32(sc->track_essence_element_key+12);
1514 mxf->timestamp = mxf_parse_timestamp(s->timestamp);
1517 mxf->timecode_track = av_mallocz(sizeof(*mxf->timecode_track));
1518 if (!mxf->timecode_track)
1519 return AVERROR(ENOMEM);
1520 mxf->timecode_track->priv_data = av_mallocz(sizeof(MXFStreamContext));
1521 if (!mxf->timecode_track->priv_data)
1522 return AVERROR(ENOMEM);
1523 mxf->timecode_track->index = -1;
1525 if (!samples_per_frame)
1526 samples_per_frame = PAL_samples_per_frame;
1528 if (ff_audio_interleave_init(s, samples_per_frame, mxf->time_base) < 0)
1534 static const uint8_t system_metadata_pack_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0D,0x01,0x03,0x01,0x04,0x01,0x01,0x00 };
1535 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 };
1537 static uint32_t ff_framenum_to_12m_time_code(unsigned frame, int drop, int fps)
1539 return (0 << 31) | // color frame flag
1540 (0 << 30) | // drop frame flag
1541 ( ((frame % fps) / 10) << 28) | // tens of frames
1542 ( ((frame % fps) % 10) << 24) | // units of frames
1543 (0 << 23) | // field phase (NTSC), b0 (PAL)
1544 ((((frame / fps) % 60) / 10) << 20) | // tens of seconds
1545 ((((frame / fps) % 60) % 10) << 16) | // units of seconds
1546 (0 << 15) | // b0 (NTSC), b2 (PAL)
1547 ((((frame / (fps * 60)) % 60) / 10) << 12) | // tens of minutes
1548 ((((frame / (fps * 60)) % 60) % 10) << 8) | // units of minutes
1550 (0 << 6) | // b2 (NSC), field phase (PAL)
1551 ((((frame / (fps * 3600) % 24)) / 10) << 4) | // tens of hours
1552 ( (frame / (fps * 3600) % 24)) % 10; // units of hours
1555 static void mxf_write_system_item(AVFormatContext *s)
1557 MXFContext *mxf = s->priv_data;
1558 ByteIOContext *pb = s->pb;
1562 frame = mxf->timecode_start + mxf->last_indexed_edit_unit + mxf->edit_units_count;
1564 // write system metadata pack
1565 put_buffer(pb, system_metadata_pack_key, 16);
1566 klv_encode_ber4_length(pb, 57);
1567 put_byte(pb, 0x5c); // UL, user date/time stamp, picture and sound item present
1568 put_byte(pb, 0x04); // content package rate
1569 put_byte(pb, 0x00); // content package type
1570 put_be16(pb, 0x00); // channel handle
1571 put_be16(pb, frame); // continuity count
1572 if (mxf->essence_container_count > 1)
1573 put_buffer(pb, multiple_desc_ul, 16);
1575 MXFStreamContext *sc = s->streams[0]->priv_data;
1576 put_buffer(pb, mxf_essence_container_uls[sc->index].container_ul, 16);
1580 put_be64(pb, 0); // creation date/time stamp
1582 put_byte(pb, 0x81); // SMPTE 12M time code
1583 time_code = ff_framenum_to_12m_time_code(frame, mxf->timecode_drop_frame, mxf->timecode_base);
1584 put_be32(pb, time_code);
1585 put_be32(pb, 0); // binary group data
1588 // write system metadata package set
1589 put_buffer(pb, system_metadata_package_set_key, 16);
1590 klv_encode_ber4_length(pb, 35);
1591 put_byte(pb, 0x83); // UMID
1593 mxf_write_umid(s, 1);
1596 static void mxf_write_d10_video_packet(AVFormatContext *s, AVStream *st, AVPacket *pkt)
1598 MXFContext *mxf = s->priv_data;
1599 ByteIOContext *pb = s->pb;
1600 int packet_size = (uint64_t)st->codec->bit_rate*mxf->time_base.num /
1601 (8*mxf->time_base.den); // frame size
1604 packet_size += 16 + 4;
1605 packet_size += klv_fill_size(packet_size);
1607 klv_encode_ber4_length(pb, pkt->size);
1608 put_buffer(pb, pkt->data, pkt->size);
1610 // ensure CBR muxing by padding to correct video frame size
1611 pad = packet_size - pkt->size - 16 - 4;
1613 put_buffer(s->pb, klv_fill_key, 16);
1615 klv_encode_ber4_length(s->pb, pad);
1618 assert(!(url_ftell(s->pb) & (KAG_SIZE-1)));
1620 av_log(s, AV_LOG_WARNING, "cannot fill d-10 video packet\n");
1621 for (; pad > 0; pad--)
1626 static void mxf_write_d10_audio_packet(AVFormatContext *s, AVStream *st, AVPacket *pkt)
1628 MXFContext *mxf = s->priv_data;
1629 ByteIOContext *pb = s->pb;
1630 int frame_size = pkt->size / st->codec->block_align;
1631 uint8_t *samples = pkt->data;
1632 uint8_t *end = pkt->data + pkt->size;
1635 klv_encode_ber4_length(pb, 4 + frame_size*4*8);
1637 put_byte(pb, (frame_size == 1920 ? 0 : (mxf->edit_units_count-1) % 5 + 1));
1638 put_le16(pb, frame_size);
1639 put_byte(pb, (1<<st->codec->channels)-1);
1641 while (samples < end) {
1642 for (i = 0; i < st->codec->channels; i++) {
1644 if (st->codec->codec_id == CODEC_ID_PCM_S24LE) {
1645 sample = AV_RL24(samples)<< 4;
1648 sample = AV_RL16(samples)<<12;
1651 put_le32(pb, sample | i);
1658 static int mxf_write_packet(AVFormatContext *s, AVPacket *pkt)
1660 MXFContext *mxf = s->priv_data;
1661 ByteIOContext *pb = s->pb;
1662 AVStream *st = s->streams[pkt->stream_index];
1663 MXFStreamContext *sc = st->priv_data;
1664 MXFIndexEntry ie = {0};
1666 if (!mxf->edit_unit_byte_count && !(mxf->edit_units_count % EDIT_UNITS_PER_BODY)) {
1667 mxf->index_entries = av_realloc(mxf->index_entries,
1668 (mxf->edit_units_count + EDIT_UNITS_PER_BODY)*sizeof(*mxf->index_entries));
1669 if (!mxf->index_entries) {
1670 av_log(s, AV_LOG_ERROR, "could not allocate index entries\n");
1675 if (st->codec->codec_id == CODEC_ID_MPEG2VIDEO) {
1676 if (!mxf_parse_mpeg2_frame(s, st, pkt, &ie)) {
1677 av_log(s, AV_LOG_ERROR, "could not get mpeg2 profile and level\n");
1682 if (!mxf->header_written) {
1683 if (mxf->edit_unit_byte_count) {
1684 mxf_write_partition(s, 1, 2, header_open_partition_key, 1);
1685 mxf_write_klv_fill(s);
1686 mxf_write_index_table_segment(s);
1688 mxf_write_partition(s, 0, 0, header_open_partition_key, 1);
1690 mxf->header_written = 1;
1693 if (st->index == 0) {
1694 if (!mxf->edit_unit_byte_count &&
1695 (!mxf->edit_units_count || mxf->edit_units_count > EDIT_UNITS_PER_BODY) &&
1696 !(ie.flags & 0x33)) { // I frame, Gop start
1697 mxf_write_klv_fill(s);
1698 mxf_write_partition(s, 1, 2, body_partition_key, 0);
1700 mxf_write_klv_fill(s);
1701 mxf_write_index_table_segment(s);
1704 mxf_write_klv_fill(s);
1705 mxf_write_system_item(s);
1707 if (!mxf->edit_unit_byte_count) {
1708 mxf->index_entries[mxf->edit_units_count].offset = mxf->body_offset;
1709 mxf->index_entries[mxf->edit_units_count].flags = ie.flags;
1710 mxf->index_entries[mxf->edit_units_count].temporal_ref = ie.temporal_ref;
1711 mxf->body_offset += KAG_SIZE; // size of system element
1713 mxf->edit_units_count++;
1714 } else if (!mxf->edit_unit_byte_count && st->index == 1) {
1715 mxf->index_entries[mxf->edit_units_count-1].slice_offset =
1716 mxf->body_offset - mxf->index_entries[mxf->edit_units_count-1].offset;
1719 mxf_write_klv_fill(s);
1720 put_buffer(pb, sc->track_essence_element_key, 16); // write key
1721 if (s->oformat == &mxf_d10_muxer) {
1722 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO)
1723 mxf_write_d10_video_packet(s, st, pkt);
1725 mxf_write_d10_audio_packet(s, st, pkt);
1727 klv_encode_ber4_length(pb, pkt->size); // write length
1728 put_buffer(pb, pkt->data, pkt->size);
1729 mxf->body_offset += 16+4+pkt->size + klv_fill_size(16+4+pkt->size);
1732 put_flush_packet(pb);
1737 static void mxf_write_random_index_pack(AVFormatContext *s)
1739 MXFContext *mxf = s->priv_data;
1740 ByteIOContext *pb = s->pb;
1741 uint64_t pos = url_ftell(pb);
1744 put_buffer(pb, random_index_pack_key, 16);
1745 klv_encode_ber_length(pb, 28 + 12*mxf->body_partitions_count);
1747 if (mxf->edit_unit_byte_count)
1748 put_be32(pb, 1); // BodySID of header partition
1751 put_be64(pb, 0); // offset of header partition
1753 for (i = 0; i < mxf->body_partitions_count; i++) {
1754 put_be32(pb, 1); // BodySID
1755 put_be64(pb, mxf->body_partition_offset[i]);
1758 put_be32(pb, 0); // BodySID of footer partition
1759 put_be64(pb, mxf->footer_partition_offset);
1761 put_be32(pb, url_ftell(pb) - pos + 4);
1764 static int mxf_write_footer(AVFormatContext *s)
1766 MXFContext *mxf = s->priv_data;
1767 ByteIOContext *pb = s->pb;
1769 mxf->duration = mxf->last_indexed_edit_unit + mxf->edit_units_count;
1771 mxf_write_klv_fill(s);
1772 mxf->footer_partition_offset = url_ftell(pb);
1773 if (mxf->edit_unit_byte_count) { // no need to repeat index
1774 mxf_write_partition(s, 0, 0, footer_partition_key, 0);
1776 mxf_write_partition(s, 0, 2, footer_partition_key, 0);
1778 mxf_write_klv_fill(s);
1779 mxf_write_index_table_segment(s);
1782 mxf_write_klv_fill(s);
1783 mxf_write_random_index_pack(s);
1785 if (!url_is_streamed(s->pb)) {
1786 url_fseek(pb, 0, SEEK_SET);
1787 if (mxf->edit_unit_byte_count) {
1788 mxf_write_partition(s, 1, 2, header_closed_partition_key, 1);
1789 mxf_write_klv_fill(s);
1790 mxf_write_index_table_segment(s);
1792 mxf_write_partition(s, 0, 0, header_closed_partition_key, 1);
1796 put_flush_packet(pb);
1798 ff_audio_interleave_close(s);
1800 av_freep(&mxf->index_entries);
1801 av_freep(&mxf->body_partition_offset);
1802 av_freep(&mxf->timecode_track->priv_data);
1803 av_freep(&mxf->timecode_track);
1810 static int mxf_interleave_get_packet(AVFormatContext *s, AVPacket *out, AVPacket *pkt, int flush)
1812 int i, stream_count = 0;
1814 for (i = 0; i < s->nb_streams; i++)
1815 stream_count += !!s->streams[i]->last_in_packet_buffer;
1817 if (stream_count && (s->nb_streams == stream_count || flush)) {
1818 AVPacketList *pktl = s->packet_buffer;
1819 if (s->nb_streams != stream_count) {
1820 AVPacketList *last = NULL;
1821 // find last packet in edit unit
1823 if (!stream_count || pktl->pkt.stream_index == 0)
1829 // purge packet queue
1831 AVPacketList *next = pktl->next;
1833 if(s->streams[pktl->pkt.stream_index]->last_in_packet_buffer == pktl)
1834 s->streams[pktl->pkt.stream_index]->last_in_packet_buffer= NULL;
1835 av_free_packet(&pktl->pkt);
1842 s->packet_buffer = NULL;
1843 s->packet_buffer_end= NULL;
1846 pktl = s->packet_buffer;
1850 //av_log(s, AV_LOG_DEBUG, "out st:%d dts:%lld\n", (*out).stream_index, (*out).dts);
1851 s->packet_buffer = pktl->next;
1852 if(s->streams[pktl->pkt.stream_index]->last_in_packet_buffer == pktl)
1853 s->streams[pktl->pkt.stream_index]->last_in_packet_buffer= NULL;
1854 if(!s->packet_buffer)
1855 s->packet_buffer_end= NULL;
1860 av_init_packet(out);
1865 static int mxf_compare_timestamps(AVFormatContext *s, AVPacket *next, AVPacket *pkt)
1867 MXFStreamContext *sc = s->streams[pkt ->stream_index]->priv_data;
1868 MXFStreamContext *sc2 = s->streams[next->stream_index]->priv_data;
1870 return next->dts > pkt->dts ||
1871 (next->dts == pkt->dts && sc->order < sc2->order);
1874 static int mxf_interleave(AVFormatContext *s, AVPacket *out, AVPacket *pkt, int flush)
1876 return ff_audio_rechunk_interleave(s, out, pkt, flush,
1877 mxf_interleave_get_packet, mxf_compare_timestamps);
1880 AVOutputFormat mxf_muxer = {
1882 NULL_IF_CONFIG_SMALL("Material eXchange Format"),
1887 CODEC_ID_MPEG2VIDEO,
1896 AVOutputFormat mxf_d10_muxer = {
1898 NULL_IF_CONFIG_SMALL("Material eXchange Format, D-10 Mapping"),
1903 CODEC_ID_MPEG2VIDEO,