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/fifo.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 AVOutputFormat mxf_d10_muxer;
49 #define EDIT_UNITS_PER_BODY 250
60 unsigned slice_offset; ///< offset of audio slice
64 AudioInterleaveContext aic;
65 UID track_essence_element_key;
66 int index; ///< index in mxf_essence_container_uls table
68 int order; ///< interleaving order if dts are equal
69 int interlaced; ///< wether picture is interlaced
70 int temporal_reordering;
71 AVRational aspect_ratio; ///< display aspect ratio
79 } MXFContainerEssenceEntry;
84 } mxf_essence_mappings[] = {
85 { CODEC_ID_MPEG2VIDEO, 0 },
86 { CODEC_ID_PCM_S24LE, 1 },
87 { CODEC_ID_PCM_S16LE, 1 },
91 static void mxf_write_wav_desc(AVFormatContext *s, AVStream *st);
92 static void mxf_write_aes3_desc(AVFormatContext *s, AVStream *st);
93 static void mxf_write_mpegvideo_desc(AVFormatContext *s, AVStream *st);
94 static void mxf_write_cdci_desc(AVFormatContext *s, AVStream *st);
95 static void mxf_write_generic_sound_desc(AVFormatContext *s, AVStream *st);
97 static const MXFContainerEssenceEntry mxf_essence_container_uls[] = {
98 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x02,0x0D,0x01,0x03,0x01,0x02,0x04,0x60,0x01 },
99 { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x15,0x01,0x05,0x00 },
100 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x00,0x00,0x00 },
101 mxf_write_mpegvideo_desc },
102 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x06,0x03,0x00 },
103 { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x16,0x01,0x03,0x00 },
104 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 },
105 mxf_write_aes3_desc },
106 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x06,0x01,0x00 },
107 { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x16,0x01,0x01,0x00 },
108 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 },
109 mxf_write_wav_desc },
110 // D-10 625/50 PAL 50mb/s
111 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x01,0x01 },
112 { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x05,0x01,0x01,0x00 },
113 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x01,0x02,0x01,0x01 },
114 mxf_write_cdci_desc },
115 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x01,0x01 },
116 { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x06,0x01,0x10,0x00 },
117 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 },
118 mxf_write_generic_sound_desc },
119 // D-10 525/60 NTSC 50mb/s
120 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x02,0x01 },
121 { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x05,0x01,0x01,0x00 },
122 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x01,0x02,0x01,0x02 },
123 mxf_write_cdci_desc },
124 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x02,0x01 },
125 { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x06,0x01,0x10,0x00 },
126 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 },
127 mxf_write_generic_sound_desc },
128 // D-10 625/50 PAL 40mb/s
129 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x03,0x01 },
130 { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x05,0x01,0x01,0x00 },
131 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x01,0x02,0x01,0x03 },
132 mxf_write_cdci_desc },
133 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x03,0x01 },
134 { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x06,0x01,0x10,0x00 },
135 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 },
136 mxf_write_generic_sound_desc },
137 // D-10 525/60 NTSC 40mb/s
138 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x04,0x01 },
139 { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x05,0x01,0x01,0x00 },
140 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x01,0x02,0x01,0x04 },
141 mxf_write_cdci_desc },
142 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x04,0x01 },
143 { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x06,0x01,0x10,0x00 },
144 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 },
145 mxf_write_generic_sound_desc },
146 // D-10 625/50 PAL 30mb/s
147 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x05,0x01 },
148 { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x05,0x01,0x01,0x00 },
149 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x01,0x02,0x01,0x05 },
150 mxf_write_cdci_desc },
151 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x05,0x01 },
152 { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x06,0x01,0x10,0x00 },
153 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 },
154 mxf_write_generic_sound_desc },
155 // D-10 525/60 NTSC 30mb/s
156 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x06,0x01 },
157 { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x05,0x01,0x01,0x00 },
158 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x01,0x02,0x01,0x06 },
159 mxf_write_cdci_desc },
160 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x06,0x01 },
161 { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x06,0x01,0x10,0x00 },
162 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 },
163 mxf_write_generic_sound_desc },
164 { { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 },
165 { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 },
166 { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 },
170 typedef struct MXFContext {
171 int64_t footer_partition_offset;
172 int essence_container_count;
173 AVRational time_base;
175 MXFIndexEntry *index_entries;
176 unsigned edit_units_count;
177 uint64_t timestamp; ///< timestamp, as year(16),month(8),day(8),hour(8),minutes(8),msec/4(8)
178 uint8_t slice_count; ///< index slice count minus 1 (1 if no audio, 0 otherwise)
179 int last_indexed_edit_unit;
180 uint64_t *body_partition_offset;
181 unsigned body_partitions_count;
182 int last_key_index; ///< index of last key frame
184 AVStream *timecode_track;
185 int timecode_base; ///< rounded time code base (25 or 30)
186 int timecode_start; ///< frame number computed from mpeg-2 gop header timecode
187 int timecode_drop_frame; ///< time code use drop frame method frop mpeg-2 essence gop header
188 int edit_unit_byte_count; ///< fixed edit unit byte count
189 uint64_t body_offset;
192 static const uint8_t uuid_base[] = { 0xAD,0xAB,0x44,0x24,0x2f,0x25,0x4d,0xc7,0x92,0xff,0x29,0xbd };
193 static const uint8_t umid_base[] = { 0x06,0x0A,0x2B,0x34,0x01,0x01,0x01,0x05,0x01,0x01,0x0D,0x00,0x13,0x00,0x00,0x00 };
196 * complete key for operation pattern, partitions, and primer pack
198 static const uint8_t op1a_ul[] = { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x02,0x01,0x01,0x01,0x01,0x00 };
199 static const uint8_t footer_partition_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0D,0x01,0x02,0x01,0x01,0x04,0x04,0x00 }; // ClosedComplete
200 static const uint8_t primer_pack_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0D,0x01,0x02,0x01,0x01,0x05,0x01,0x00 };
201 static const uint8_t index_table_segment_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x10,0x01,0x00 };
202 static const uint8_t random_index_pack_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0D,0x01,0x02,0x01,0x01,0x11,0x01,0x00 };
203 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
204 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
205 static const uint8_t klv_fill_key[] = { 0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x03,0x01,0x02,0x10,0x01,0x00,0x00,0x00 };
206 static const uint8_t body_partition_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0D,0x01,0x02,0x01,0x01,0x03,0x04,0x00 }; // ClosedComplete
209 * partial key for header metadata
211 static const uint8_t header_metadata_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0D,0x01,0x01,0x01,0x01 };
212 static const uint8_t multiple_desc_ul[] = { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x0D,0x01,0x03,0x01,0x02,0x7F,0x01,0x00 };
215 * SMPTE RP210 http://www.smpte-ra.org/mdd/index.html
217 static const MXFLocalTagPair mxf_local_tag_batch[] = {
219 { 0x3C0A, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x01,0x01,0x15,0x02,0x00,0x00,0x00,0x00}}, /* Instance UID */
220 { 0x3B02, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x10,0x02,0x04,0x00,0x00}}, /* Last Modified Date */
221 { 0x3B05, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x03,0x01,0x02,0x01,0x05,0x00,0x00,0x00}}, /* Version */
222 { 0x3B06, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x06,0x04,0x00,0x00}}, /* Identifications reference */
223 { 0x3B03, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x02,0x01,0x00,0x00}}, /* Content Storage reference */
224 { 0x3B09, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x01,0x02,0x02,0x03,0x00,0x00,0x00,0x00}}, /* Operational Pattern UL */
225 { 0x3B0A, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x01,0x02,0x02,0x10,0x02,0x01,0x00,0x00}}, /* Essence Containers UL batch */
226 { 0x3B0B, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x01,0x02,0x02,0x10,0x02,0x02,0x00,0x00}}, /* DM Schemes UL batch */
228 { 0x3C09, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x20,0x07,0x01,0x01,0x00,0x00,0x00}}, /* This Generation UID */
229 { 0x3C01, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x20,0x07,0x01,0x02,0x01,0x00,0x00}}, /* Company Name */
230 { 0x3C02, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x20,0x07,0x01,0x03,0x01,0x00,0x00}}, /* Product Name */
231 { 0x3C04, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x20,0x07,0x01,0x05,0x01,0x00,0x00}}, /* Version String */
232 { 0x3C05, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x20,0x07,0x01,0x07,0x00,0x00,0x00}}, /* Product ID */
233 { 0x3C06, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x10,0x02,0x03,0x00,0x00}}, /* Modification Date */
235 { 0x1901, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x05,0x01,0x00,0x00}}, /* Package strong reference batch */
236 { 0x1902, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x05,0x02,0x00,0x00}}, /* Package strong reference batch */
237 // Essence Container Data
238 { 0x2701, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x06,0x01,0x00,0x00,0x00}}, /* Linked Package UID */
239 { 0x3F07, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x01,0x03,0x04,0x04,0x00,0x00,0x00,0x00}}, /* BodySID */
241 { 0x4401, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x01,0x01,0x15,0x10,0x00,0x00,0x00,0x00}}, /* Package UID */
242 { 0x4405, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x10,0x01,0x03,0x00,0x00}}, /* Package Creation Date */
243 { 0x4404, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x10,0x02,0x05,0x00,0x00}}, /* Package Modified Date */
244 { 0x4403, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x06,0x05,0x00,0x00}}, /* Tracks Strong reference array */
245 { 0x4701, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x02,0x03,0x00,0x00}}, /* Descriptor */
247 { 0x4801, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x01,0x07,0x01,0x01,0x00,0x00,0x00,0x00}}, /* Track ID */
248 { 0x4804, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x01,0x04,0x01,0x03,0x00,0x00,0x00,0x00}}, /* Track Number */
249 { 0x4B01, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x30,0x04,0x05,0x00,0x00,0x00,0x00}}, /* Edit Rate */
250 { 0x4B02, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x03,0x01,0x03,0x00,0x00}}, /* Origin */
251 { 0x4803, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x02,0x04,0x00,0x00}}, /* Sequence reference */
253 { 0x0201, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x07,0x01,0x00,0x00,0x00,0x00,0x00}}, /* Data Definition UL */
254 { 0x0202, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x02,0x01,0x01,0x03,0x00,0x00}}, /* Duration */
255 { 0x1001, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x06,0x09,0x00,0x00}}, /* Structural Components reference array */
257 { 0x1201, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x03,0x01,0x04,0x00,0x00}}, /* Start position */
258 { 0x1101, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x03,0x01,0x00,0x00,0x00}}, /* SourcePackageID */
259 { 0x1102, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x03,0x02,0x00,0x00,0x00}}, /* SourceTrackID */
260 // Timecode Component
261 { 0x1501, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x03,0x01,0x05,0x00,0x00}}, /* Start Time Code */
262 { 0x1502, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x04,0x01,0x01,0x02,0x06,0x00,0x00}}, /* Rounded Time Code Base */
263 { 0x1503, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x04,0x01,0x01,0x05,0x00,0x00,0x00}}, /* Drop Frame */
265 { 0x3F01, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x06,0x01,0x01,0x04,0x06,0x0B,0x00,0x00}}, /* Sub Descriptors reference array */
266 { 0x3006, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x06,0x01,0x01,0x03,0x05,0x00,0x00,0x00}}, /* Linked Track ID */
267 { 0x3001, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x06,0x01,0x01,0x00,0x00,0x00,0x00}}, /* SampleRate */
268 { 0x3004, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x01,0x02,0x00,0x00}}, /* Essence Container */
269 // Generic Picture Essence Descriptor
270 { 0x320C, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x03,0x01,0x04,0x00,0x00,0x00}}, /* Frame Layout */
271 { 0x320D, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x01,0x03,0x02,0x05,0x00,0x00,0x00}}, /* Video Line Map */
272 { 0x3203, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x02,0x02,0x00,0x00,0x00}}, /* Stored Width */
273 { 0x3202, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x02,0x01,0x00,0x00,0x00}}, /* Stored Height */
274 { 0x3209, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x01,0x0C,0x00,0x00,0x00}}, /* Display Width */
275 { 0x3208, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x01,0x0B,0x00,0x00,0x00}}, /* Display Height */
276 { 0x320E, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x01,0x01,0x01,0x00,0x00,0x00}}, /* Aspect Ratio */
277 { 0x3201, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x01,0x06,0x01,0x00,0x00,0x00,0x00}}, /* Picture Essence Coding */
278 // CDCI Picture Essence Descriptor
279 { 0x3301, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x01,0x05,0x03,0x0A,0x00,0x00,0x00}}, /* Component Depth */
280 { 0x3302, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x01,0x05,0x00,0x00,0x00}}, /* Horizontal Subsampling */
281 // Generic Sound Essence Descriptor
282 { 0x3D02, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x04,0x02,0x03,0x01,0x04,0x00,0x00,0x00}}, /* Locked/Unlocked */
283 { 0x3D03, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x02,0x03,0x01,0x01,0x01,0x00,0x00}}, /* Audio sampling rate */
284 { 0x3D07, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x02,0x01,0x01,0x04,0x00,0x00,0x00}}, /* ChannelCount */
285 { 0x3D01, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x04,0x02,0x03,0x03,0x04,0x00,0x00,0x00}}, /* Quantization bits */
286 { 0x3D06, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x02,0x04,0x02,0x00,0x00,0x00,0x00}}, /* Sound Essence Compression */
287 // Index Table Segment
288 { 0x3F0B, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x05,0x30,0x04,0x06,0x00,0x00,0x00,0x00}}, /* Index Edit Rate */
289 { 0x3F0C, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x07,0x02,0x01,0x03,0x01,0x0A,0x00,0x00}}, /* Index Start Position */
290 { 0x3F0D, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x07,0x02,0x02,0x01,0x01,0x02,0x00,0x00}}, /* Index Duration */
291 { 0x3F05, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x04,0x06,0x02,0x01,0x00,0x00,0x00,0x00}}, /* Edit Unit Byte Count */
292 { 0x3F06, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x01,0x03,0x04,0x05,0x00,0x00,0x00,0x00}}, /* IndexSID */
293 { 0x3F08, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x04,0x04,0x04,0x01,0x01,0x00,0x00,0x00}}, /* Slice Count */
294 { 0x3F09, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x04,0x04,0x01,0x06,0x00,0x00,0x00}}, /* Delta Entry Array */
295 { 0x3F0A, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x04,0x04,0x02,0x05,0x00,0x00,0x00}}, /* Index Entry Array */
296 // MPEG video Descriptor
297 { 0x8000, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x01,0x06,0x02,0x01,0x0B,0x00,0x00}}, /* BitRate */
298 // Wave Audio Essence Descriptor
299 { 0x3D09, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x02,0x03,0x03,0x05,0x00,0x00,0x00}}, /* Average Bytes Per Second */
300 { 0x3D0A, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x02,0x03,0x02,0x01,0x00,0x00,0x00}}, /* Block Align */
303 static void mxf_write_uuid(ByteIOContext *pb, enum MXFMetadataSetType type, int value)
305 put_buffer(pb, uuid_base, 12);
310 static void mxf_write_umid(ByteIOContext *pb, enum MXFMetadataSetType type, int value)
312 put_buffer(pb, umid_base, 16);
313 mxf_write_uuid(pb, type, value);
316 static void mxf_write_refs_count(ByteIOContext *pb, int ref_count)
318 put_be32(pb, ref_count);
322 static int klv_ber_length(uint64_t len)
327 return (av_log2(len) >> 3) + 2;
330 static int klv_encode_ber_length(ByteIOContext *pb, uint64_t len)
332 // Determine the best BER size
340 size = (av_log2(len) >> 3) + 1;
343 put_byte(pb, 0x80 + size);
346 put_byte(pb, len >> 8 * size & 0xff);
351 static void klv_encode_ber4_length(ByteIOContext *pb, int len)
353 put_byte(pb, 0x80 + 3);
358 * Get essence container ul index
360 static int mxf_get_essence_container_ul_index(enum CodecID id)
363 for (i = 0; mxf_essence_mappings[i].id; i++)
364 if (mxf_essence_mappings[i].id == id)
365 return mxf_essence_mappings[i].index;
369 static void mxf_write_primer_pack(AVFormatContext *s)
371 ByteIOContext *pb = s->pb;
372 int local_tag_number, i = 0;
374 local_tag_number = FF_ARRAY_ELEMS(mxf_local_tag_batch);
376 put_buffer(pb, primer_pack_key, 16);
377 klv_encode_ber_length(pb, local_tag_number * 18 + 8);
379 put_be32(pb, local_tag_number); // local_tag num
380 put_be32(pb, 18); // item size, always 18 according to the specs
382 for (i = 0; i < local_tag_number; i++) {
383 put_be16(pb, mxf_local_tag_batch[i].local_tag);
384 put_buffer(pb, mxf_local_tag_batch[i].uid, 16);
388 static void mxf_write_local_tag(ByteIOContext *pb, int size, int tag)
394 static void mxf_write_metadata_key(ByteIOContext *pb, unsigned int value)
396 put_buffer(pb, header_metadata_key, 13);
400 static void mxf_free(AVFormatContext *s)
404 for (i = 0; i < s->nb_streams; i++) {
405 AVStream *st = s->streams[i];
406 av_freep(&st->priv_data);
410 static const MXFCodecUL *mxf_get_data_definition_ul(int type)
412 const MXFCodecUL *uls = ff_mxf_data_definition_uls;
413 while (uls->uid[0]) {
421 static void mxf_write_essence_container_refs(AVFormatContext *s)
423 MXFContext *c = s->priv_data;
424 ByteIOContext *pb = s->pb;
427 mxf_write_refs_count(pb, c->essence_container_count);
428 av_log(s,AV_LOG_DEBUG, "essence container count:%d\n", c->essence_container_count);
429 for (i = 0; i < c->essence_container_count; i++) {
430 MXFStreamContext *sc = s->streams[i]->priv_data;
431 put_buffer(pb, mxf_essence_container_uls[sc->index].container_ul, 16);
435 static void mxf_write_preface(AVFormatContext *s)
437 MXFContext *mxf = s->priv_data;
438 ByteIOContext *pb = s->pb;
440 mxf_write_metadata_key(pb, 0x012f00);
441 PRINT_KEY(s, "preface key", pb->buf_ptr - 16);
442 klv_encode_ber_length(pb, 130 + 16 * mxf->essence_container_count);
444 // write preface set uid
445 mxf_write_local_tag(pb, 16, 0x3C0A);
446 mxf_write_uuid(pb, Preface, 0);
447 PRINT_KEY(s, "preface uid", pb->buf_ptr - 16);
449 // last modified date
450 mxf_write_local_tag(pb, 8, 0x3B02);
451 put_be64(pb, mxf->timestamp);
454 mxf_write_local_tag(pb, 2, 0x3B05);
457 // write identification_refs
458 mxf_write_local_tag(pb, 16 + 8, 0x3B06);
459 mxf_write_refs_count(pb, 1);
460 mxf_write_uuid(pb, Identification, 0);
462 // write content_storage_refs
463 mxf_write_local_tag(pb, 16, 0x3B03);
464 mxf_write_uuid(pb, ContentStorage, 0);
466 // operational pattern
467 mxf_write_local_tag(pb, 16, 0x3B09);
468 if (s->nb_streams > 1) {
469 put_buffer(pb, op1a_ul, 14);
470 put_be16(pb, 0x0900); // multi track
472 put_buffer(pb, op1a_ul, 16);
475 // write essence_container_refs
476 mxf_write_local_tag(pb, 8 + 16 * mxf->essence_container_count, 0x3B0A);
477 mxf_write_essence_container_refs(s);
479 // write dm_scheme_refs
480 mxf_write_local_tag(pb, 8, 0x3B0B);
485 * Write a local tag containing an ascii string as utf-16
487 static void mxf_write_local_tag_utf16(ByteIOContext *pb, int tag, const char *value)
489 int i, size = strlen(value);
490 mxf_write_local_tag(pb, size*2, tag);
491 for (i = 0; i < size; i++)
492 put_be16(pb, value[i]);
495 static void mxf_write_identification(AVFormatContext *s)
497 MXFContext *mxf = s->priv_data;
498 ByteIOContext *pb = s->pb;
499 const char *company = "FFmpeg";
500 const char *product = "OP1a Muxer";
504 mxf_write_metadata_key(pb, 0x013000);
505 PRINT_KEY(s, "identification key", pb->buf_ptr - 16);
507 version = s->streams[0]->codec->flags & CODEC_FLAG_BITEXACT ?
508 "0.0.0" : AV_STRINGIFY(LIBAVFORMAT_VERSION);
509 length = 84 + (strlen(company)+strlen(product)+strlen(version))*2; // utf-16
510 klv_encode_ber_length(pb, length);
513 mxf_write_local_tag(pb, 16, 0x3C0A);
514 mxf_write_uuid(pb, Identification, 0);
515 PRINT_KEY(s, "identification uid", pb->buf_ptr - 16);
517 // write generation uid
518 mxf_write_local_tag(pb, 16, 0x3C09);
519 mxf_write_uuid(pb, Identification, 1);
521 mxf_write_local_tag_utf16(pb, 0x3C01, company); // Company Name
522 mxf_write_local_tag_utf16(pb, 0x3C02, product); // Product Name
523 mxf_write_local_tag_utf16(pb, 0x3C04, version); // Version String
526 mxf_write_local_tag(pb, 16, 0x3C05);
527 mxf_write_uuid(pb, Identification, 2);
530 mxf_write_local_tag(pb, 8, 0x3C06);
531 put_be64(pb, mxf->timestamp);
534 static void mxf_write_content_storage(AVFormatContext *s)
536 ByteIOContext *pb = s->pb;
538 mxf_write_metadata_key(pb, 0x011800);
539 PRINT_KEY(s, "content storage key", pb->buf_ptr - 16);
540 klv_encode_ber_length(pb, 92);
543 mxf_write_local_tag(pb, 16, 0x3C0A);
544 mxf_write_uuid(pb, ContentStorage, 0);
545 PRINT_KEY(s, "content storage uid", pb->buf_ptr - 16);
547 // write package reference
548 mxf_write_local_tag(pb, 16 * 2 + 8, 0x1901);
549 mxf_write_refs_count(pb, 2);
550 mxf_write_uuid(pb, MaterialPackage, 0);
551 mxf_write_uuid(pb, SourcePackage, 0);
553 // write essence container data
554 mxf_write_local_tag(pb, 8 + 16, 0x1902);
555 mxf_write_refs_count(pb, 1);
556 mxf_write_uuid(pb, EssenceContainerData, 0);
559 static void mxf_write_track(AVFormatContext *s, AVStream *st, enum MXFMetadataSetType type)
561 MXFContext *mxf = s->priv_data;
562 ByteIOContext *pb = s->pb;
563 MXFStreamContext *sc = st->priv_data;
565 mxf_write_metadata_key(pb, 0x013b00);
566 PRINT_KEY(s, "track key", pb->buf_ptr - 16);
567 klv_encode_ber_length(pb, 80);
570 mxf_write_local_tag(pb, 16, 0x3C0A);
571 mxf_write_uuid(pb, type == MaterialPackage ? Track : Track + TypeBottom, st->index);
572 PRINT_KEY(s, "track uid", pb->buf_ptr - 16);
575 mxf_write_local_tag(pb, 4, 0x4801);
576 put_be32(pb, st->index);
578 // write track number
579 mxf_write_local_tag(pb, 4, 0x4804);
580 if (type == MaterialPackage)
581 put_be32(pb, 0); // track number of material package is 0
583 put_buffer(pb, sc->track_essence_element_key + 12, 4);
585 mxf_write_local_tag(pb, 8, 0x4B01);
586 put_be32(pb, mxf->time_base.den);
587 put_be32(pb, mxf->time_base.num);
590 mxf_write_local_tag(pb, 8, 0x4B02);
593 // write sequence refs
594 mxf_write_local_tag(pb, 16, 0x4803);
595 mxf_write_uuid(pb, type == MaterialPackage ? Sequence: Sequence + TypeBottom, st->index);
598 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 };
600 static void mxf_write_common_fields(AVFormatContext *s, AVStream *st)
602 MXFContext *mxf = s->priv_data;
603 ByteIOContext *pb = s->pb;
605 // find data define uls
606 mxf_write_local_tag(pb, 16, 0x0201);
607 if (st == mxf->timecode_track)
608 put_buffer(pb, smpte_12m_timecode_track_data_ul, 16);
610 const MXFCodecUL *data_def_ul = mxf_get_data_definition_ul(st->codec->codec_type);
611 put_buffer(pb, data_def_ul->uid, 16);
615 mxf_write_local_tag(pb, 8, 0x0202);
616 put_be64(pb, mxf->duration);
619 static void mxf_write_sequence(AVFormatContext *s, AVStream *st, enum MXFMetadataSetType type)
621 MXFContext *mxf = s->priv_data;
622 ByteIOContext *pb = s->pb;
623 enum MXFMetadataSetType component;
625 mxf_write_metadata_key(pb, 0x010f00);
626 PRINT_KEY(s, "sequence key", pb->buf_ptr - 16);
627 klv_encode_ber_length(pb, 80);
629 mxf_write_local_tag(pb, 16, 0x3C0A);
630 mxf_write_uuid(pb, type == MaterialPackage ? Sequence: Sequence + TypeBottom, st->index);
632 PRINT_KEY(s, "sequence uid", pb->buf_ptr - 16);
633 mxf_write_common_fields(s, st);
635 // write structural component
636 mxf_write_local_tag(pb, 16 + 8, 0x1001);
637 mxf_write_refs_count(pb, 1);
638 if (st == mxf->timecode_track)
639 component = TimecodeComponent;
640 else if (type == MaterialPackage)
641 component = SourceClip;
643 component = SourceClip+TypeBottom;
644 mxf_write_uuid(pb, component, st->index);
647 static void mxf_write_timecode_component(AVFormatContext *s, AVStream *st)
649 MXFContext *mxf = s->priv_data;
650 ByteIOContext *pb = s->pb;
652 mxf_write_metadata_key(pb, 0x011400);
653 klv_encode_ber_length(pb, 75);
656 mxf_write_local_tag(pb, 16, 0x3C0A);
657 mxf_write_uuid(pb, TimecodeComponent, st->index);
659 mxf_write_common_fields(s, st);
662 mxf_write_local_tag(pb, 8, 0x1501);
663 put_be64(pb, mxf->timecode_start);
665 // Rounded Time Code Base
666 mxf_write_local_tag(pb, 2, 0x1502);
667 put_be16(pb, mxf->timecode_base);
670 mxf_write_local_tag(pb, 1, 0x1503);
671 put_byte(pb, mxf->timecode_drop_frame);
674 static void mxf_write_structural_component(AVFormatContext *s, AVStream *st, enum MXFMetadataSetType type)
676 ByteIOContext *pb = s->pb;
679 mxf_write_metadata_key(pb, 0x011100);
680 PRINT_KEY(s, "sturctural component key", pb->buf_ptr - 16);
681 klv_encode_ber_length(pb, 108);
684 mxf_write_local_tag(pb, 16, 0x3C0A);
685 mxf_write_uuid(pb, type == MaterialPackage ? SourceClip: SourceClip + TypeBottom, st->index);
687 PRINT_KEY(s, "structural component uid", pb->buf_ptr - 16);
688 mxf_write_common_fields(s, st);
690 // write start_position
691 mxf_write_local_tag(pb, 8, 0x1201);
694 // write source package uid, end of the reference
695 mxf_write_local_tag(pb, 32, 0x1101);
696 if (type == SourcePackage) {
697 for (i = 0; i < 4; i++)
700 mxf_write_umid(pb, SourcePackage, 0);
702 // write source track id
703 mxf_write_local_tag(pb, 4, 0x1102);
704 if (type == SourcePackage)
707 put_be32(pb, st->index);
710 static void mxf_write_multi_descriptor(AVFormatContext *s)
712 MXFContext *mxf = s->priv_data;
713 ByteIOContext *pb = s->pb;
717 mxf_write_metadata_key(pb, 0x014400);
718 PRINT_KEY(s, "multiple descriptor key", pb->buf_ptr - 16);
719 klv_encode_ber_length(pb, 64 + 16 * s->nb_streams);
721 mxf_write_local_tag(pb, 16, 0x3C0A);
722 mxf_write_uuid(pb, MultipleDescriptor, 0);
723 PRINT_KEY(s, "multi_desc uid", pb->buf_ptr - 16);
726 mxf_write_local_tag(pb, 8, 0x3001);
727 put_be32(pb, mxf->time_base.den);
728 put_be32(pb, mxf->time_base.num);
730 // write essence container ul
731 mxf_write_local_tag(pb, 16, 0x3004);
732 if (mxf->essence_container_count > 1)
733 ul = multiple_desc_ul;
735 MXFStreamContext *sc = s->streams[0]->priv_data;
736 ul = mxf_essence_container_uls[sc->index].container_ul;
738 put_buffer(pb, ul, 16);
740 // write sub descriptor refs
741 mxf_write_local_tag(pb, s->nb_streams * 16 + 8, 0x3F01);
742 mxf_write_refs_count(pb, s->nb_streams);
743 for (i = 0; i < s->nb_streams; i++)
744 mxf_write_uuid(pb, SubDescriptor, i);
747 static void mxf_write_generic_desc(AVFormatContext *s, AVStream *st, const UID key, unsigned size)
749 MXFContext *mxf = s->priv_data;
750 MXFStreamContext *sc = st->priv_data;
751 ByteIOContext *pb = s->pb;
753 put_buffer(pb, key, 16);
754 klv_encode_ber_length(pb, size);
756 mxf_write_local_tag(pb, 16, 0x3C0A);
757 mxf_write_uuid(pb, SubDescriptor, st->index);
759 mxf_write_local_tag(pb, 4, 0x3006);
760 put_be32(pb, st->index);
762 mxf_write_local_tag(pb, 8, 0x3001);
763 put_be32(pb, mxf->time_base.den);
764 put_be32(pb, mxf->time_base.num);
766 mxf_write_local_tag(pb, 16, 0x3004);
767 put_buffer(pb, mxf_essence_container_uls[sc->index].container_ul, 16);
770 static const UID mxf_mpegvideo_descriptor_key = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x51,0x00 };
771 static const UID mxf_wav_descriptor_key = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x48,0x00 };
772 static const UID mxf_aes3_descriptor_key = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x47,0x00 };
773 static const UID mxf_cdci_descriptor_key = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0D,0x01,0x01,0x01,0x01,0x01,0x28,0x00 };
774 static const UID mxf_generic_sound_descriptor_key = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0D,0x01,0x01,0x01,0x01,0x01,0x42,0x00 };
776 static void mxf_write_cdci_common(AVFormatContext *s, AVStream *st, const UID key, unsigned size)
778 MXFStreamContext *sc = st->priv_data;
779 ByteIOContext *pb = s->pb;
780 int stored_height = (st->codec->height+15)/16*16;
784 mxf_write_generic_desc(s, st, key, size);
786 mxf_write_local_tag(pb, 4, 0x3203);
787 put_be32(pb, st->codec->width);
789 mxf_write_local_tag(pb, 4, 0x3202);
790 put_be32(pb, stored_height>>sc->interlaced);
792 mxf_write_local_tag(pb, 4, 0x3209);
793 put_be32(pb, st->codec->width);
795 if (st->codec->height == 608) // PAL + VBI
796 display_height = 576;
797 else if (st->codec->height == 512) // NTSC + VBI
798 display_height = 486;
800 display_height = st->codec->height;
802 mxf_write_local_tag(pb, 4, 0x3208);
803 put_be32(pb, display_height>>sc->interlaced);
806 mxf_write_local_tag(pb, 4, 0x3301);
809 // horizontal subsampling
810 mxf_write_local_tag(pb, 4, 0x3302);
814 mxf_write_local_tag(pb, 1, 0x320C);
815 put_byte(pb, sc->interlaced);
818 switch (st->codec->height) {
819 case 576: f1 = 23; f2 = 336; break;
820 case 608: f1 = 7; f2 = 320; break;
821 case 480: f1 = 20; f2 = 283; break;
822 case 512: f1 = 7; f2 = 270; break;
823 case 720: f1 = 26; f2 = 0; break; // progressive
824 case 1080: f1 = 21; f2 = 584; break;
825 default: f1 = 0; f2 = 0; break;
828 if (!sc->interlaced) {
833 mxf_write_local_tag(pb, 12+sc->interlaced*4, 0x320D);
834 put_be32(pb, sc->interlaced ? 2 : 1);
840 mxf_write_local_tag(pb, 8, 0x320E);
841 put_be32(pb, sc->aspect_ratio.num);
842 put_be32(pb, sc->aspect_ratio.den);
844 mxf_write_local_tag(pb, 16, 0x3201);
845 put_buffer(pb, *sc->codec_ul, 16);
848 static void mxf_write_cdci_desc(AVFormatContext *s, AVStream *st)
850 MXFStreamContext *sc = st->priv_data;
851 mxf_write_cdci_common(s, st, mxf_cdci_descriptor_key, 161+sc->interlaced*4);
854 static void mxf_write_mpegvideo_desc(AVFormatContext *s, AVStream *st)
856 MXFStreamContext *sc = st->priv_data;
857 ByteIOContext *pb = s->pb;
859 mxf_write_cdci_common(s, st, mxf_mpegvideo_descriptor_key, 169+sc->interlaced*4);
862 mxf_write_local_tag(pb, 4, 0x8000);
863 put_be32(pb, st->codec->bit_rate);
866 static void mxf_write_generic_sound_common(AVFormatContext *s, AVStream *st, const UID key, unsigned size)
868 ByteIOContext *pb = s->pb;
870 mxf_write_generic_desc(s, st, key, size);
873 mxf_write_local_tag(pb, 1, 0x3D02);
876 // write audio sampling rate
877 mxf_write_local_tag(pb, 8, 0x3D03);
878 put_be32(pb, st->codec->sample_rate);
881 mxf_write_local_tag(pb, 4, 0x3D07);
882 put_be32(pb, st->codec->channels);
884 mxf_write_local_tag(pb, 4, 0x3D01);
885 put_be32(pb, av_get_bits_per_sample(st->codec->codec_id));
888 static void mxf_write_wav_common(AVFormatContext *s, AVStream *st, const UID key, unsigned size)
890 ByteIOContext *pb = s->pb;
892 mxf_write_generic_sound_common(s, st, key, size);
894 mxf_write_local_tag(pb, 2, 0x3D0A);
895 put_be16(pb, st->codec->block_align);
898 mxf_write_local_tag(pb, 4, 0x3D09);
899 put_be32(pb, st->codec->block_align*st->codec->sample_rate);
902 static void mxf_write_wav_desc(AVFormatContext *s, AVStream *st)
904 mxf_write_wav_common(s, st, mxf_wav_descriptor_key, 107);
907 static void mxf_write_aes3_desc(AVFormatContext *s, AVStream *st)
909 mxf_write_wav_common(s, st, mxf_aes3_descriptor_key, 107);
912 static void mxf_write_generic_sound_desc(AVFormatContext *s, AVStream *st)
914 mxf_write_generic_sound_common(s, st, mxf_generic_sound_descriptor_key, 93);
917 static void mxf_write_package(AVFormatContext *s, enum MXFMetadataSetType type)
919 MXFContext *mxf = s->priv_data;
920 ByteIOContext *pb = s->pb;
923 if (type == MaterialPackage) {
924 track_count = s->nb_streams + 1; // add timecode track
925 mxf_write_metadata_key(pb, 0x013600);
926 PRINT_KEY(s, "Material Package key", pb->buf_ptr - 16);
927 klv_encode_ber_length(pb, 92 + 16*track_count);
929 track_count = s->nb_streams;
930 mxf_write_metadata_key(pb, 0x013700);
931 PRINT_KEY(s, "Source Package key", pb->buf_ptr - 16);
932 klv_encode_ber_length(pb, 112 + 16*track_count); // 20 bytes length for descriptor reference
936 mxf_write_local_tag(pb, 16, 0x3C0A);
937 mxf_write_uuid(pb, type, 0);
938 av_log(s,AV_LOG_DEBUG, "package type:%d\n", type);
939 PRINT_KEY(s, "package uid", pb->buf_ptr - 16);
941 // write package umid
942 mxf_write_local_tag(pb, 32, 0x4401);
943 mxf_write_umid(pb, type, 0);
944 PRINT_KEY(s, "package umid second part", pb->buf_ptr - 16);
946 // package creation date
947 mxf_write_local_tag(pb, 8, 0x4405);
948 put_be64(pb, mxf->timestamp);
950 // package modified date
951 mxf_write_local_tag(pb, 8, 0x4404);
952 put_be64(pb, mxf->timestamp);
955 mxf_write_local_tag(pb, track_count*16 + 8, 0x4403);
956 mxf_write_refs_count(pb, track_count);
957 for (i = 0; i < s->nb_streams; i++)
958 mxf_write_uuid(pb, type == MaterialPackage ? Track : Track + TypeBottom, i);
959 if (type == MaterialPackage)
960 mxf_write_uuid(pb, Track, s->nb_streams); // timecode track
962 // write multiple descriptor reference
963 if (type == SourcePackage) {
964 mxf_write_local_tag(pb, 16, 0x4701);
965 if (s->nb_streams > 1) {
966 mxf_write_uuid(pb, MultipleDescriptor, 0);
967 mxf_write_multi_descriptor(s);
969 mxf_write_uuid(pb, SubDescriptor, 0);
971 // write timecode track
972 mxf_write_track(s, mxf->timecode_track, type);
973 mxf_write_sequence(s, mxf->timecode_track, type);
974 mxf_write_timecode_component(s, mxf->timecode_track);
977 for (i = 0; i < s->nb_streams; i++) {
978 AVStream *st = s->streams[i];
979 mxf_write_track(s, st, type);
980 mxf_write_sequence(s, st, type);
981 mxf_write_structural_component(s, st, type);
983 if (type == SourcePackage) {
984 MXFStreamContext *sc = st->priv_data;
985 mxf_essence_container_uls[sc->index].write_desc(s, st);
990 static int mxf_write_essence_container_data(AVFormatContext *s)
992 ByteIOContext *pb = s->pb;
994 mxf_write_metadata_key(pb, 0x012300);
995 klv_encode_ber_length(pb, 72);
997 mxf_write_local_tag(pb, 16, 0x3C0A); // Instance UID
998 mxf_write_uuid(pb, EssenceContainerData, 0);
1000 mxf_write_local_tag(pb, 32, 0x2701); // Linked Package UID
1001 mxf_write_umid(pb, SourcePackage, 0);
1003 mxf_write_local_tag(pb, 4, 0x3F07); // BodySID
1006 mxf_write_local_tag(pb, 4, 0x3F06); // IndexSID
1012 static int mxf_write_header_metadata_sets(AVFormatContext *s)
1014 mxf_write_preface(s);
1015 mxf_write_identification(s);
1016 mxf_write_content_storage(s);
1017 mxf_write_package(s, MaterialPackage);
1018 mxf_write_package(s, SourcePackage);
1019 mxf_write_essence_container_data(s);
1023 static unsigned klv_fill_size(uint64_t size)
1025 unsigned pad = KAG_SIZE - (size & (KAG_SIZE-1));
1026 if (pad < 20) // smallest fill item possible
1027 return pad + KAG_SIZE;
1029 return pad & (KAG_SIZE-1);
1032 static void mxf_write_index_table_segment(AVFormatContext *s)
1034 MXFContext *mxf = s->priv_data;
1035 ByteIOContext *pb = s->pb;
1037 int temporal_reordering = 0;
1038 int key_index = mxf->last_key_index;
1040 av_log(s, AV_LOG_DEBUG, "edit units count %d\n", mxf->edit_units_count);
1042 if (!mxf->edit_units_count && !mxf->edit_unit_byte_count)
1045 put_buffer(pb, index_table_segment_key, 16);
1047 if (mxf->edit_unit_byte_count) {
1048 klv_encode_ber_length(pb, 85);
1050 klv_encode_ber_length(pb, 85 + 12+(s->nb_streams+1)*6 +
1051 12+mxf->edit_units_count*(11+mxf->slice_count*4));
1055 mxf_write_local_tag(pb, 16, 0x3C0A);
1056 mxf_write_uuid(pb, IndexTableSegment, 0);
1059 mxf_write_local_tag(pb, 8, 0x3F0B);
1060 put_be32(pb, mxf->time_base.num);
1061 put_be32(pb, mxf->time_base.den);
1063 // index start position
1064 mxf_write_local_tag(pb, 8, 0x3F0C);
1065 put_be64(pb, mxf->last_indexed_edit_unit);
1068 mxf_write_local_tag(pb, 8, 0x3F0D);
1069 put_be64(pb, mxf->edit_units_count);
1071 // edit unit byte count
1072 mxf_write_local_tag(pb, 4, 0x3F05);
1073 put_be32(pb, mxf->edit_unit_byte_count);
1076 mxf_write_local_tag(pb, 4, 0x3F06);
1080 mxf_write_local_tag(pb, 4, 0x3F07);
1083 // real slice count - 1
1084 mxf_write_local_tag(pb, 1, 0x3F08);
1085 put_byte(pb, mxf->slice_count);
1087 if (!mxf->edit_unit_byte_count) {
1088 // delta entry array
1089 mxf_write_local_tag(pb, 8 + (s->nb_streams+1)*6, 0x3F09);
1090 put_be32(pb, s->nb_streams+1); // num of entries
1091 put_be32(pb, 6); // size of one entry
1092 // write system item delta entry
1094 put_byte(pb, 0); // slice entry
1095 put_be32(pb, 0); // element delta
1096 for (i = 0; i < s->nb_streams; i++) {
1097 AVStream *st = s->streams[i];
1098 MXFStreamContext *sc = st->priv_data;
1099 put_byte(pb, sc->temporal_reordering);
1100 if (sc->temporal_reordering)
1101 temporal_reordering = 1;
1102 if (i == 0) { // video track
1103 put_byte(pb, 0); // slice number
1104 put_be32(pb, KAG_SIZE); // system item size including klv fill
1105 } else { // audio track
1106 unsigned audio_frame_size = sc->aic.samples[0]*sc->aic.sample_size;
1107 audio_frame_size += klv_fill_size(audio_frame_size);
1109 put_be32(pb, (i-1)*audio_frame_size); // element delta
1113 mxf_write_local_tag(pb, 8 + mxf->edit_units_count*(11+mxf->slice_count*4), 0x3F0A);
1114 put_be32(pb, mxf->edit_units_count); // num of entries
1115 put_be32(pb, 11+mxf->slice_count*4); // size of one entry
1116 for (i = 0; i < mxf->edit_units_count; i++) {
1117 if (temporal_reordering) {
1118 int temporal_offset = 0;
1119 for (j = i+1; j < mxf->edit_units_count; j++) {
1121 if (mxf->index_entries[j].flags & 0x10) { // backward prediction
1122 // next is not b, so is reordered
1123 if (!(mxf->index_entries[i+1].flags & 0x10)) {
1124 if ((mxf->index_entries[i].flags & 0x11) == 0) // i frame
1125 temporal_offset = 0;
1127 temporal_offset = -temporal_offset;
1132 put_byte(pb, temporal_offset);
1135 if (!(mxf->index_entries[i].flags & 0x33)) { // I frame
1136 mxf->last_key_index = key_index;
1139 if (mxf->index_entries[i].flags & 0x10 && // backward prediction
1140 !(mxf->index_entries[key_index].flags & 0x80)) { // open gop
1141 put_byte(pb, mxf->last_key_index - i);
1143 put_byte(pb, key_index - i); // key frame offset
1144 if ((mxf->index_entries[i].flags & 0x20) == 0x20) // only forward
1145 mxf->last_key_index = key_index;
1147 put_byte(pb, mxf->index_entries[i].flags);
1149 put_be64(pb, mxf->index_entries[i].offset);
1150 if (s->nb_streams > 1)
1151 put_be32(pb, mxf->index_entries[i].slice_offset);
1154 mxf->last_key_index = key_index - mxf->edit_units_count;
1155 mxf->last_indexed_edit_unit += mxf->edit_units_count;
1156 mxf->edit_units_count = 0;
1160 static void mxf_write_klv_fill(AVFormatContext *s)
1162 unsigned pad = klv_fill_size(url_ftell(s->pb));
1164 put_buffer(s->pb, klv_fill_key, 16);
1166 klv_encode_ber4_length(s->pb, pad);
1169 assert(!(url_ftell(s->pb) & (KAG_SIZE-1)));
1173 static void mxf_write_partition(AVFormatContext *s, int bodysid,
1175 const uint8_t *key, int write_metadata)
1177 MXFContext *mxf = s->priv_data;
1178 ByteIOContext *pb = s->pb;
1179 int64_t header_byte_count_offset;
1180 unsigned index_byte_count = 0;
1181 uint64_t partition_offset = url_ftell(pb);
1183 if (!mxf->edit_unit_byte_count && mxf->edit_units_count)
1184 index_byte_count = 85 + 12+(s->nb_streams+1)*6 +
1185 12+mxf->edit_units_count*(11+mxf->slice_count*4);
1186 else if (mxf->edit_unit_byte_count && indexsid)
1187 index_byte_count = 85;
1189 if (index_byte_count) {
1190 // add encoded ber length
1191 index_byte_count += 16 + klv_ber_length(index_byte_count);
1192 index_byte_count += klv_fill_size(index_byte_count);
1195 if (!memcmp(key, body_partition_key, 16)) {
1196 mxf->body_partition_offset =
1197 av_realloc(mxf->body_partition_offset,
1198 (mxf->body_partitions_count+1)*
1199 sizeof(*mxf->body_partition_offset));
1200 mxf->body_partition_offset[mxf->body_partitions_count++] = partition_offset;
1204 put_buffer(pb, key, 16);
1205 klv_encode_ber_length(pb, 88 + 16 * mxf->essence_container_count);
1207 // write partition value
1208 put_be16(pb, 1); // majorVersion
1209 put_be16(pb, 2); // minorVersion
1210 put_be32(pb, KAG_SIZE); // KAGSize
1212 put_be64(pb, partition_offset); // ThisPartition
1214 if (!memcmp(key, body_partition_key, 16) && mxf->body_partitions_count > 1)
1215 put_be64(pb, mxf->body_partition_offset[mxf->body_partitions_count-2]); // PreviousPartition
1216 else if (!memcmp(key, footer_partition_key, 16) && mxf->body_partitions_count)
1217 put_be64(pb, mxf->body_partition_offset[mxf->body_partitions_count-1]); // PreviousPartition
1221 put_be64(pb, mxf->footer_partition_offset); // footerPartition
1224 header_byte_count_offset = url_ftell(pb);
1225 put_be64(pb, 0); // headerByteCount, update later
1228 put_be64(pb, index_byte_count); // indexByteCount
1229 put_be32(pb, index_byte_count ? indexsid : 0); // indexSID
1232 if (bodysid && mxf->edit_units_count && mxf->body_partitions_count) {
1233 put_be64(pb, mxf->body_offset);
1237 put_be32(pb, bodysid); // bodySID
1239 // operational pattern
1240 if (s->nb_streams > 1) {
1241 put_buffer(pb, op1a_ul, 14);
1242 put_be16(pb, 0x0900); // multi track
1244 put_buffer(pb, op1a_ul, 16);
1247 // essence container
1248 mxf_write_essence_container_refs(s);
1250 if (write_metadata) {
1251 // mark the start of the headermetadata and calculate metadata size
1253 unsigned header_byte_count;
1255 mxf_write_klv_fill(s);
1256 start = url_ftell(s->pb);
1257 mxf_write_primer_pack(s);
1258 mxf_write_header_metadata_sets(s);
1259 pos = url_ftell(s->pb);
1260 header_byte_count = pos - start + klv_fill_size(pos);
1262 // update header_byte_count
1263 url_fseek(pb, header_byte_count_offset, SEEK_SET);
1264 put_be64(pb, header_byte_count);
1265 url_fseek(pb, pos, SEEK_SET);
1268 put_flush_packet(pb);
1271 static const UID mxf_mpeg2_codec_uls[] = {
1272 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x01,0x10,0x00 }, // MP-ML I-Frame
1273 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x01,0x11,0x00 }, // MP-ML Long GOP
1274 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x02,0x02,0x00 }, // 422P-ML I-Frame
1275 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x02,0x03,0x00 }, // 422P-ML Long GOP
1276 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x03,0x02,0x00 }, // MP-HL I-Frame
1277 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x03,0x03,0x00 }, // MP-HL Long GOP
1278 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x04,0x02,0x00 }, // 422P-HL I-Frame
1279 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x04,0x03,0x00 }, // 422P-HL Long GOP
1282 static const UID *mxf_get_mpeg2_codec_ul(AVCodecContext *avctx)
1284 if (avctx->profile == 4) { // Main
1285 if (avctx->level == 8) // Main
1286 return avctx->gop_size ?
1287 &mxf_mpeg2_codec_uls[1] :
1288 &mxf_mpeg2_codec_uls[0];
1289 else if (avctx->level == 4) // High
1290 return avctx->gop_size ?
1291 &mxf_mpeg2_codec_uls[5] :
1292 &mxf_mpeg2_codec_uls[4];
1293 } else if (avctx->profile == 0) { // 422
1294 if (avctx->level == 5) // Main
1295 return avctx->gop_size ?
1296 &mxf_mpeg2_codec_uls[3] :
1297 &mxf_mpeg2_codec_uls[2];
1298 else if (avctx->level == 2) // High
1299 return avctx->gop_size ?
1300 &mxf_mpeg2_codec_uls[7] :
1301 &mxf_mpeg2_codec_uls[6];
1306 static int mxf_parse_mpeg2_frame(AVFormatContext *s, AVStream *st, AVPacket *pkt, int *flags)
1308 MXFStreamContext *sc = st->priv_data;
1309 MXFContext *mxf = s->priv_data;
1315 for(i = 0; i < pkt->size - 4; i++) {
1316 c = (c<<8) + pkt->data[i];
1318 if (i + 2 < pkt->size && (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 (i + 4 < pkt->size) {
1327 if (pkt->data[i+4]>>6 & 0x01) // closed
1328 *flags |= 0x80; // random access
1329 if (!mxf->header_written) {
1330 unsigned hours = (pkt->data[i+1]>>2) & 0x1f;
1331 unsigned minutes = ((pkt->data[i+1] & 0x03) << 4) | (pkt->data[i+2]>>4);
1332 unsigned seconds = ((pkt->data[i+2] & 0x07) << 3) | (pkt->data[i+3]>>5);
1333 unsigned frames = ((pkt->data[i+3] & 0x1f) << 1) | (pkt->data[i+4]>>7);
1334 mxf->timecode_drop_frame = !!(pkt->data[i+1] & 0x80);
1335 mxf->timecode_start = (hours*3600 + minutes*60 + seconds) *
1336 mxf->timecode_base + frames;
1337 if (mxf->timecode_drop_frame) {
1338 unsigned tminutes = 60 * hours + minutes;
1339 mxf->timecode_start -= 2 * (tminutes - tminutes / 10);
1341 av_log(s, AV_LOG_DEBUG, "frame %d %d:%d:%d%c%d\n", mxf->timecode_start,
1342 hours, minutes, seconds, mxf->timecode_drop_frame ? ';':':', frames);
1345 } else if (c == 0x1b3) { // seq
1347 if (i + 4 < pkt->size) {
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);
1357 } else if (c == 0x100) { // pic
1358 int pict_type = (pkt->data[i+2]>>3) & 0x07;
1359 if (pict_type == 2) { // P frame
1361 st->codec->gop_size = 1;
1362 } else if (pict_type == 3) { // B frame
1364 sc->temporal_reordering = -1;
1365 } else if (!pict_type) {
1366 av_log(s, AV_LOG_ERROR, "error parsing mpeg2 frame\n");
1371 if (s->oformat != &mxf_d10_muxer)
1372 sc->codec_ul = mxf_get_mpeg2_codec_ul(st->codec);
1373 return !!sc->codec_ul;
1376 static uint64_t mxf_parse_timestamp(time_t timestamp)
1378 struct tm *time = localtime(×tamp);
1379 return (uint64_t)(time->tm_year+1900) << 48 |
1380 (uint64_t)(time->tm_mon+1) << 40 |
1381 (uint64_t) time->tm_mday << 32 |
1382 time->tm_hour << 24 |
1383 time->tm_min << 16 |
1387 static int mxf_write_header(AVFormatContext *s)
1389 MXFContext *mxf = s->priv_data;
1391 uint8_t present[FF_ARRAY_ELEMS(mxf_essence_container_uls)] = {0};
1392 const int *samples_per_frame = NULL;
1394 for (i = 0; i < s->nb_streams; i++) {
1395 AVStream *st = s->streams[i];
1396 MXFStreamContext *sc = av_mallocz(sizeof(*sc));
1398 return AVERROR(ENOMEM);
1401 if (st->codec->codec_type == CODEC_TYPE_VIDEO) {
1403 av_log(s, AV_LOG_ERROR, "video stream must be first track\n");
1406 if (fabs(av_q2d(st->codec->time_base) - 1/25.0) < 0.0001) {
1407 samples_per_frame = PAL_samples_per_frame;
1408 mxf->time_base = (AVRational){ 1, 25 };
1409 mxf->timecode_base = 25;
1410 } else if (fabs(av_q2d(st->codec->time_base) - 1001/30000.0) < 0.0001) {
1411 samples_per_frame = NTSC_samples_per_frame;
1412 mxf->time_base = (AVRational){ 1001, 30000 };
1413 mxf->timecode_base = 30;
1415 av_log(s, AV_LOG_ERROR, "unsupported video frame rate\n");
1418 av_set_pts_info(st, 64, mxf->time_base.num, mxf->time_base.den);
1419 if (s->oformat == &mxf_d10_muxer) {
1420 if (st->codec->bit_rate == 50000000)
1421 if (mxf->time_base.den == 25) sc->index = 3;
1423 else if (st->codec->bit_rate == 40000000)
1424 if (mxf->time_base.den == 25) sc->index = 7;
1426 else if (st->codec->bit_rate == 30000000)
1427 if (mxf->time_base.den == 25) sc->index = 11;
1428 else sc->index = 13;
1430 av_log(s, AV_LOG_ERROR, "error MXF D-10 only support 30/40/50 mbit/s\n");
1434 mxf->edit_unit_byte_count = KAG_SIZE; // system element
1435 mxf->edit_unit_byte_count += 16 + 4 + (uint64_t)st->codec->bit_rate *
1436 mxf->time_base.num / (8*mxf->time_base.den);
1437 mxf->edit_unit_byte_count += klv_fill_size(mxf->edit_unit_byte_count);
1438 mxf->edit_unit_byte_count += 16 + 4 + 4 + samples_per_frame[0]*8*4;
1439 mxf->edit_unit_byte_count += klv_fill_size(mxf->edit_unit_byte_count);
1441 } else if (st->codec->codec_type == CODEC_TYPE_AUDIO) {
1442 if (st->codec->sample_rate != 48000) {
1443 av_log(s, AV_LOG_ERROR, "only 48khz is implemented\n");
1446 av_set_pts_info(st, 64, 1, st->codec->sample_rate);
1447 if (s->oformat == &mxf_d10_muxer) {
1448 if (st->index != 1) {
1449 av_log(s, AV_LOG_ERROR, "MXF D-10 only support one audio track\n");
1452 if (st->codec->codec_id != CODEC_ID_PCM_S16LE &&
1453 st->codec->codec_id != CODEC_ID_PCM_S24LE) {
1454 av_log(s, AV_LOG_ERROR, "MXF D-10 only support 16 or 24 bits le audio\n");
1456 sc->index = ((MXFStreamContext*)s->streams[0]->priv_data)->index + 1;
1458 mxf->slice_count = 1;
1462 sc->index = mxf_get_essence_container_ul_index(st->codec->codec_id);
1463 if (sc->index == -1) {
1464 av_log(s, AV_LOG_ERROR, "track %d: could not find essence container ul, "
1465 "codec not currently supported in container\n", i);
1470 sc->codec_ul = &mxf_essence_container_uls[sc->index].codec_ul;
1472 if (!present[sc->index]) {
1473 present[sc->index] = 1;
1474 mxf->essence_container_count++;
1476 present[sc->index]++;
1478 memcpy(sc->track_essence_element_key, mxf_essence_container_uls[sc->index].element_ul, 15);
1479 sc->track_essence_element_key[15] = present[sc->index];
1480 PRINT_KEY(s, "track essence element key", sc->track_essence_element_key);
1483 if (s->oformat == &mxf_d10_muxer) {
1484 mxf->essence_container_count = 1;
1487 for (i = 0; i < s->nb_streams; i++) {
1488 MXFStreamContext *sc = s->streams[i]->priv_data;
1489 // update element count
1490 sc->track_essence_element_key[13] = present[sc->index];
1491 sc->order = AV_RB32(sc->track_essence_element_key+12);
1494 mxf->timestamp = mxf_parse_timestamp(s->timestamp);
1497 mxf->timecode_track = av_mallocz(sizeof(*mxf->timecode_track));
1498 if (!mxf->timecode_track)
1499 return AVERROR(ENOMEM);
1500 mxf->timecode_track->priv_data = av_mallocz(sizeof(MXFStreamContext));
1501 if (!mxf->timecode_track->priv_data)
1502 return AVERROR(ENOMEM);
1503 mxf->timecode_track->index = s->nb_streams;
1505 if (!samples_per_frame)
1506 samples_per_frame = PAL_samples_per_frame;
1508 if (ff_audio_interleave_init(s, samples_per_frame, mxf->time_base) < 0)
1514 static const uint8_t system_metadata_pack_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0D,0x01,0x03,0x01,0x04,0x01,0x01,0x00 };
1515 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 };
1517 static uint32_t ff_framenum_to_12m_time_code(unsigned frame, int drop, int fps)
1519 return (0 << 31) | // color frame flag
1520 (0 << 30) | // drop frame flag
1521 ( ((frame % fps) / 10) << 28) | // tens of frames
1522 ( ((frame % fps) % 10) << 24) | // units of frames
1523 (0 << 23) | // field phase (NTSC), b0 (PAL)
1524 ((((frame / fps) % 60) / 10) << 20) | // tens of seconds
1525 ((((frame / fps) % 60) % 10) << 16) | // units of seconds
1526 (0 << 15) | // b0 (NTSC), b2 (PAL)
1527 ((((frame / (fps * 60)) % 60) / 10) << 12) | // tens of minutes
1528 ((((frame / (fps * 60)) % 60) % 10) << 8) | // units of minutes
1530 (0 << 6) | // b2 (NSC), field phase (PAL)
1531 ((((frame / (fps * 3600) % 24)) / 10) << 4) | // tens of hours
1532 ( (frame / (fps * 3600) % 24)) % 10; // units of hours
1535 static void mxf_write_system_item(AVFormatContext *s)
1537 MXFContext *mxf = s->priv_data;
1538 ByteIOContext *pb = s->pb;
1542 frame = mxf->timecode_start + mxf->last_indexed_edit_unit + mxf->edit_units_count;
1544 // write system metadata pack
1545 put_buffer(pb, system_metadata_pack_key, 16);
1546 klv_encode_ber4_length(pb, 57);
1547 put_byte(pb, 0x5c); // UL, user date/time stamp, picture and sound item present
1548 put_byte(pb, 0x04); // content package rate
1549 put_byte(pb, 0x00); // content package type
1550 put_be16(pb, 0x00); // channel handle
1551 put_be16(pb, frame); // continuity count
1552 if (mxf->essence_container_count > 1)
1553 put_buffer(pb, multiple_desc_ul, 16);
1555 MXFStreamContext *sc = s->streams[0]->priv_data;
1556 put_buffer(pb, mxf_essence_container_uls[sc->index].container_ul, 16);
1560 put_be64(pb, 0); // creation date/time stamp
1562 put_byte(pb, 0x81); // SMPTE 12M time code
1563 time_code = ff_framenum_to_12m_time_code(frame, mxf->timecode_drop_frame, mxf->timecode_base);
1564 put_be32(pb, time_code);
1565 put_be32(pb, 0); // binary group data
1568 // write system metadata package set
1569 put_buffer(pb, system_metadata_package_set_key, 16);
1570 klv_encode_ber4_length(pb, 35);
1571 put_byte(pb, 0x83); // UMID
1573 mxf_write_umid(pb, SourcePackage, 0);
1576 static void mxf_write_d10_video_packet(AVFormatContext *s, AVStream *st, AVPacket *pkt)
1578 MXFContext *mxf = s->priv_data;
1579 ByteIOContext *pb = s->pb;
1580 int packet_size = (uint64_t)st->codec->bit_rate*mxf->time_base.num /
1581 (8*mxf->time_base.den); // frame size
1584 packet_size += 16 + 4;
1585 packet_size += klv_fill_size(packet_size);
1587 klv_encode_ber4_length(pb, pkt->size);
1588 put_buffer(pb, pkt->data, pkt->size);
1590 // ensure CBR muxing by padding to correct video frame size
1591 pad = packet_size - pkt->size - 16 - 4;
1593 put_buffer(s->pb, klv_fill_key, 16);
1595 klv_encode_ber4_length(s->pb, pad);
1598 assert(!(url_ftell(s->pb) & (KAG_SIZE-1)));
1600 av_log(s, AV_LOG_WARNING, "cannot fill d-10 video packet\n");
1601 for (; pad > 0; pad--)
1606 static void mxf_write_d10_audio_packet(AVFormatContext *s, AVStream *st, AVPacket *pkt)
1608 MXFContext *mxf = s->priv_data;
1609 ByteIOContext *pb = s->pb;
1610 int frame_size = (pkt->size<<3) /
1611 (st->codec->channels*av_get_bits_per_sample(st->codec->codec_id));
1612 uint8_t *samples = pkt->data;
1613 uint8_t *end = pkt->data + pkt->size;
1616 klv_encode_ber4_length(pb, 4 + frame_size*4*8);
1618 put_byte(pb, (frame_size == 1920 ? 0 : (mxf->edit_units_count-1) % 5 + 1));
1619 put_le16(pb, frame_size);
1620 put_byte(pb, (1<<st->codec->channels)-1);
1622 while (samples < end) {
1623 for (i = 0; i < st->codec->channels; i++) {
1625 if (st->codec->codec_id == CODEC_ID_PCM_S24LE) {
1626 sample = (AV_RL24(samples)<< 4)|((samples==pkt->data)<<3) | i;
1629 sample = (AV_RL16(samples)<<12)|((samples==pkt->data)<<3) | i;
1632 put_le32(pb, sample);
1639 static int mxf_write_packet(AVFormatContext *s, AVPacket *pkt)
1641 MXFContext *mxf = s->priv_data;
1642 ByteIOContext *pb = s->pb;
1643 AVStream *st = s->streams[pkt->stream_index];
1644 MXFStreamContext *sc = st->priv_data;
1647 if (!mxf->edit_unit_byte_count && !(mxf->edit_units_count % EDIT_UNITS_PER_BODY)) {
1648 mxf->index_entries = av_realloc(mxf->index_entries,
1649 (mxf->edit_units_count + EDIT_UNITS_PER_BODY)*sizeof(*mxf->index_entries));
1650 if (!mxf->index_entries) {
1651 av_log(s, AV_LOG_ERROR, "could not allocate index entries\n");
1656 if (st->codec->codec_id == CODEC_ID_MPEG2VIDEO) {
1657 if (!mxf_parse_mpeg2_frame(s, st, pkt, &flags)) {
1658 av_log(s, AV_LOG_ERROR, "could not get mpeg2 profile and level\n");
1663 if (!mxf->header_written) {
1664 if (mxf->edit_unit_byte_count) {
1665 mxf_write_partition(s, 1, 2, header_open_partition_key, 1);
1666 mxf_write_klv_fill(s);
1667 mxf_write_index_table_segment(s);
1669 mxf_write_partition(s, 0, 0, header_open_partition_key, 1);
1671 mxf->header_written = 1;
1674 if (st->index == 0) {
1675 if (!mxf->edit_unit_byte_count &&
1676 (!mxf->edit_units_count || mxf->edit_units_count > EDIT_UNITS_PER_BODY) &&
1677 !(flags & 0x33)) { // I frame, Gop start
1678 mxf_write_klv_fill(s);
1679 mxf_write_partition(s, 1, 2, body_partition_key, 0);
1681 mxf_write_klv_fill(s);
1682 mxf_write_index_table_segment(s);
1685 mxf_write_klv_fill(s);
1686 mxf_write_system_item(s);
1688 if (!mxf->edit_unit_byte_count) {
1689 mxf->index_entries[mxf->edit_units_count].offset = mxf->body_offset;
1690 mxf->index_entries[mxf->edit_units_count].flags = flags;
1691 mxf->body_offset += KAG_SIZE; // size of system element
1693 mxf->edit_units_count++;
1694 } else if (!mxf->edit_unit_byte_count && st->index == 1) {
1695 mxf->index_entries[mxf->edit_units_count-1].slice_offset =
1696 mxf->body_offset - mxf->index_entries[mxf->edit_units_count-1].offset;
1699 mxf_write_klv_fill(s);
1700 put_buffer(pb, sc->track_essence_element_key, 16); // write key
1701 if (s->oformat == &mxf_d10_muxer) {
1702 if (st->codec->codec_type == CODEC_TYPE_VIDEO)
1703 mxf_write_d10_video_packet(s, st, pkt);
1705 mxf_write_d10_audio_packet(s, st, pkt);
1707 klv_encode_ber4_length(pb, pkt->size); // write length
1708 put_buffer(pb, pkt->data, pkt->size);
1709 mxf->body_offset += 16+4+pkt->size + klv_fill_size(16+4+pkt->size);
1712 put_flush_packet(pb);
1717 static void mxf_write_random_index_pack(AVFormatContext *s)
1719 MXFContext *mxf = s->priv_data;
1720 ByteIOContext *pb = s->pb;
1721 uint64_t pos = url_ftell(pb);
1724 put_buffer(pb, random_index_pack_key, 16);
1725 klv_encode_ber_length(pb, 28 + 12*mxf->body_partitions_count);
1727 put_be32(pb, 0); // BodySID of header partition
1728 put_be64(pb, 0); // offset of header partition
1730 for (i = 0; i < mxf->body_partitions_count; i++) {
1731 put_be32(pb, 1); // BodySID
1732 put_be64(pb, mxf->body_partition_offset[i]);
1735 put_be32(pb, 0); // BodySID of footer partition
1736 put_be64(pb, mxf->footer_partition_offset);
1738 put_be32(pb, url_ftell(pb) - pos + 4);
1741 static int mxf_write_footer(AVFormatContext *s)
1743 MXFContext *mxf = s->priv_data;
1744 ByteIOContext *pb = s->pb;
1746 mxf->duration = mxf->last_indexed_edit_unit + mxf->edit_units_count;
1748 mxf_write_klv_fill(s);
1749 mxf->footer_partition_offset = url_ftell(pb);
1750 if (mxf->edit_unit_byte_count) { // no need to repeat index
1751 mxf_write_partition(s, 0, 0, footer_partition_key, 0);
1753 mxf_write_partition(s, 0, 2, footer_partition_key, 0);
1755 mxf_write_klv_fill(s);
1756 mxf_write_index_table_segment(s);
1759 mxf_write_klv_fill(s);
1760 mxf_write_random_index_pack(s);
1762 if (!url_is_streamed(s->pb)) {
1763 url_fseek(pb, 0, SEEK_SET);
1764 if (mxf->edit_unit_byte_count) {
1765 mxf_write_partition(s, 1, 2, header_closed_partition_key, 1);
1766 mxf_write_klv_fill(s);
1767 mxf_write_index_table_segment(s);
1769 mxf_write_partition(s, 0, 0, header_closed_partition_key, 1);
1773 ff_audio_interleave_close(s);
1775 av_freep(&mxf->index_entries);
1776 av_freep(&mxf->body_partition_offset);
1777 av_freep(&mxf->timecode_track->priv_data);
1778 av_freep(&mxf->timecode_track);
1785 static int mxf_interleave_get_packet(AVFormatContext *s, AVPacket *out, AVPacket *pkt, int flush)
1788 int stream_count = 0;
1789 int streams[MAX_STREAMS];
1791 memset(streams, 0, sizeof(streams));
1792 pktl = s->packet_buffer;
1794 //av_log(s, AV_LOG_DEBUG, "show st:%d dts:%lld\n", pktl->pkt.stream_index, pktl->pkt.dts);
1795 if (!streams[pktl->pkt.stream_index])
1797 streams[pktl->pkt.stream_index]++;
1801 if (stream_count && (s->nb_streams == stream_count || flush)) {
1802 pktl = s->packet_buffer;
1803 if (s->nb_streams != stream_count) {
1804 AVPacketList *last = NULL;
1805 // find last packet in edit unit
1807 if (!stream_count || pktl->pkt.stream_index == 0)
1813 // purge packet queue
1815 AVPacketList *next = pktl->next;
1816 av_free_packet(&pktl->pkt);
1823 s->packet_buffer = NULL;
1826 pktl = s->packet_buffer;
1830 //av_log(s, AV_LOG_DEBUG, "out st:%d dts:%lld\n", (*out).stream_index, (*out).dts);
1831 s->packet_buffer = pktl->next;
1836 av_init_packet(out);
1841 static int mxf_compare_timestamps(AVFormatContext *s, AVPacket *next, AVPacket *pkt)
1843 MXFStreamContext *sc = s->streams[pkt ->stream_index]->priv_data;
1844 MXFStreamContext *sc2 = s->streams[next->stream_index]->priv_data;
1846 return next->dts > pkt->dts ||
1847 (next->dts == pkt->dts && sc->order < sc2->order);
1850 static int mxf_interleave(AVFormatContext *s, AVPacket *out, AVPacket *pkt, int flush)
1852 return ff_audio_rechunk_interleave(s, out, pkt, flush,
1853 mxf_interleave_get_packet, mxf_compare_timestamps);
1856 AVOutputFormat mxf_muxer = {
1858 NULL_IF_CONFIG_SMALL("Material eXchange Format"),
1863 CODEC_ID_MPEG2VIDEO,
1872 AVOutputFormat mxf_d10_muxer = {
1874 NULL_IF_CONFIG_SMALL("Material eXchange Format, D-10 Mapping"),
1879 CODEC_ID_MPEG2VIDEO,