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;
78 } MXFContainerEssenceEntry;
83 } mxf_essence_mappings[] = {
84 { CODEC_ID_MPEG2VIDEO, 0 },
85 { CODEC_ID_PCM_S24LE, 1 },
86 { CODEC_ID_PCM_S16LE, 1 },
90 static void mxf_write_wav_desc(AVFormatContext *s, AVStream *st);
91 static void mxf_write_aes3_desc(AVFormatContext *s, AVStream *st);
92 static void mxf_write_mpegvideo_desc(AVFormatContext *s, AVStream *st);
93 static void mxf_write_cdci_desc(AVFormatContext *s, AVStream *st);
94 static void mxf_write_generic_sound_desc(AVFormatContext *s, AVStream *st);
96 static const MXFContainerEssenceEntry mxf_essence_container_uls[] = {
97 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x02,0x0D,0x01,0x03,0x01,0x02,0x04,0x60,0x01 },
98 { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x15,0x01,0x05,0x00 },
99 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x00,0x00,0x00 },
100 mxf_write_mpegvideo_desc },
101 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x06,0x03,0x00 },
102 { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x16,0x01,0x03,0x00 },
103 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 },
104 mxf_write_aes3_desc },
105 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x06,0x01,0x00 },
106 { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x16,0x01,0x01,0x00 },
107 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 },
108 mxf_write_wav_desc },
109 // D-10 625/50 PAL 50mb/s
110 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x01,0x01 },
111 { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x05,0x01,0x01,0x00 },
112 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x01,0x02,0x01,0x01 },
113 mxf_write_cdci_desc },
114 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x01,0x01 },
115 { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x06,0x01,0x10,0x00 },
116 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 },
117 mxf_write_generic_sound_desc },
118 // D-10 525/60 NTSC 50mb/s
119 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x02,0x01 },
120 { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x05,0x01,0x01,0x00 },
121 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x01,0x02,0x01,0x02 },
122 mxf_write_cdci_desc },
123 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x02,0x01 },
124 { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x06,0x01,0x10,0x00 },
125 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 },
126 mxf_write_generic_sound_desc },
127 // D-10 625/50 PAL 40mb/s
128 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x03,0x01 },
129 { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x05,0x01,0x01,0x00 },
130 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x01,0x02,0x01,0x03 },
131 mxf_write_cdci_desc },
132 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x03,0x01 },
133 { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x06,0x01,0x10,0x00 },
134 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 },
135 mxf_write_generic_sound_desc },
136 // D-10 525/60 NTSC 40mb/s
137 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x04,0x01 },
138 { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x05,0x01,0x01,0x00 },
139 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x01,0x02,0x01,0x04 },
140 mxf_write_cdci_desc },
141 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x04,0x01 },
142 { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x06,0x01,0x10,0x00 },
143 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 },
144 mxf_write_generic_sound_desc },
145 // D-10 625/50 PAL 30mb/s
146 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x05,0x01 },
147 { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x05,0x01,0x01,0x00 },
148 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x01,0x02,0x01,0x05 },
149 mxf_write_cdci_desc },
150 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x05,0x01 },
151 { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x06,0x01,0x10,0x00 },
152 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 },
153 mxf_write_generic_sound_desc },
154 // D-10 525/60 NTSC 30mb/s
155 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x06,0x01 },
156 { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x05,0x01,0x01,0x00 },
157 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x01,0x02,0x01,0x06 },
158 mxf_write_cdci_desc },
159 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x06,0x01 },
160 { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x06,0x01,0x10,0x00 },
161 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 },
162 mxf_write_generic_sound_desc },
163 { { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 },
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 },
169 typedef struct MXFContext {
170 int64_t footer_partition_offset;
171 int essence_container_count;
172 AVRational time_base;
174 MXFIndexEntry *index_entries;
175 unsigned edit_units_count;
176 uint64_t timestamp; ///< timestamp, as year(16),month(8),day(8),hour(8),minutes(8),msec/4(8)
177 uint8_t slice_count; ///< index slice count minus 1 (1 if no audio, 0 otherwise)
178 int last_indexed_edit_unit;
179 uint64_t first_edit_unit_offset;
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
191 static const uint8_t uuid_base[] = { 0xAD,0xAB,0x44,0x24,0x2f,0x25,0x4d,0xc7,0x92,0xff,0x29,0xbd };
192 static const uint8_t umid_base[] = { 0x06,0x0A,0x2B,0x34,0x01,0x01,0x01,0x05,0x01,0x01,0x0D,0x00,0x13,0x00,0x00,0x00 };
195 * complete key for operation pattern, partitions, and primer pack
197 static const uint8_t op1a_ul[] = { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x02,0x01,0x01,0x01,0x01,0x00 };
198 static const uint8_t footer_partition_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0D,0x01,0x02,0x01,0x01,0x04,0x04,0x00 }; // ClosedComplete
199 static const uint8_t primer_pack_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0D,0x01,0x02,0x01,0x01,0x05,0x01,0x00 };
200 static const uint8_t index_table_segment_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x10,0x01,0x00 };
201 static const uint8_t random_index_pack_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0D,0x01,0x02,0x01,0x01,0x11,0x01,0x00 };
202 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
203 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
204 static const uint8_t klv_fill_key[] = { 0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x03,0x01,0x02,0x10,0x01,0x00,0x00,0x00 };
205 static const uint8_t body_partition_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0D,0x01,0x02,0x01,0x01,0x03,0x04,0x00 }; // ClosedComplete
208 * partial key for header metadata
210 static const uint8_t header_metadata_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0D,0x01,0x01,0x01,0x01 };
211 static const uint8_t multiple_desc_ul[] = { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x0D,0x01,0x03,0x01,0x02,0x7F,0x01,0x00 };
214 * SMPTE RP210 http://www.smpte-ra.org/mdd/index.html
216 static const MXFLocalTagPair mxf_local_tag_batch[] = {
218 { 0x3C0A, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x01,0x01,0x15,0x02,0x00,0x00,0x00,0x00}}, /* Instance UID */
219 { 0x3B02, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x10,0x02,0x04,0x00,0x00}}, /* Last Modified Date */
220 { 0x3B05, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x03,0x01,0x02,0x01,0x05,0x00,0x00,0x00}}, /* Version */
221 { 0x3B06, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x06,0x04,0x00,0x00}}, /* Identifications reference */
222 { 0x3B03, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x02,0x01,0x00,0x00}}, /* Content Storage reference */
223 { 0x3B09, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x01,0x02,0x02,0x03,0x00,0x00,0x00,0x00}}, /* Operational Pattern UL */
224 { 0x3B0A, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x01,0x02,0x02,0x10,0x02,0x01,0x00,0x00}}, /* Essence Containers UL batch */
225 { 0x3B0B, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x01,0x02,0x02,0x10,0x02,0x02,0x00,0x00}}, /* DM Schemes UL batch */
227 { 0x3C09, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x20,0x07,0x01,0x01,0x00,0x00,0x00}}, /* This Generation UID */
228 { 0x3C01, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x20,0x07,0x01,0x02,0x01,0x00,0x00}}, /* Company Name */
229 { 0x3C02, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x20,0x07,0x01,0x03,0x01,0x00,0x00}}, /* Product Name */
230 { 0x3C04, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x20,0x07,0x01,0x05,0x01,0x00,0x00}}, /* Version String */
231 { 0x3C05, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x20,0x07,0x01,0x07,0x00,0x00,0x00}}, /* Product ID */
232 { 0x3C06, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x10,0x02,0x03,0x00,0x00}}, /* Modification Date */
234 { 0x1901, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x05,0x01,0x00,0x00}}, /* Package strong reference batch */
235 { 0x1902, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x05,0x02,0x00,0x00}}, /* Package strong reference batch */
236 // Essence Container Data
237 { 0x2701, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x06,0x01,0x00,0x00,0x00}}, /* Linked Package UID */
238 { 0x3F07, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x01,0x03,0x04,0x04,0x00,0x00,0x00,0x00}}, /* BodySID */
240 { 0x4401, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x01,0x01,0x15,0x10,0x00,0x00,0x00,0x00}}, /* Package UID */
241 { 0x4405, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x10,0x01,0x03,0x00,0x00}}, /* Package Creation Date */
242 { 0x4404, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x10,0x02,0x05,0x00,0x00}}, /* Package Modified Date */
243 { 0x4403, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x06,0x05,0x00,0x00}}, /* Tracks Strong reference array */
244 { 0x4701, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x02,0x03,0x00,0x00}}, /* Descriptor */
246 { 0x4801, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x01,0x07,0x01,0x01,0x00,0x00,0x00,0x00}}, /* Track ID */
247 { 0x4804, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x01,0x04,0x01,0x03,0x00,0x00,0x00,0x00}}, /* Track Number */
248 { 0x4B01, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x30,0x04,0x05,0x00,0x00,0x00,0x00}}, /* Edit Rate */
249 { 0x4B02, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x03,0x01,0x03,0x00,0x00}}, /* Origin */
250 { 0x4803, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x02,0x04,0x00,0x00}}, /* Sequence reference */
252 { 0x0201, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x07,0x01,0x00,0x00,0x00,0x00,0x00}}, /* Data Definition UL */
253 { 0x0202, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x02,0x01,0x01,0x03,0x00,0x00}}, /* Duration */
254 { 0x1001, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x06,0x09,0x00,0x00}}, /* Structural Components reference array */
256 { 0x1201, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x03,0x01,0x04,0x00,0x00}}, /* Start position */
257 { 0x1101, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x03,0x01,0x00,0x00,0x00}}, /* SourcePackageID */
258 { 0x1102, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x03,0x02,0x00,0x00,0x00}}, /* SourceTrackID */
259 // Timecode Component
260 { 0x1501, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x03,0x01,0x05,0x00,0x00}}, /* Start Time Code */
261 { 0x1502, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x04,0x01,0x01,0x02,0x06,0x00,0x00}}, /* Rounded Time Code Base */
262 { 0x1503, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x04,0x01,0x01,0x05,0x00,0x00,0x00}}, /* Drop Frame */
264 { 0x3F01, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x06,0x01,0x01,0x04,0x06,0x0B,0x00,0x00}}, /* Sub Descriptors reference array */
265 { 0x3006, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x06,0x01,0x01,0x03,0x05,0x00,0x00,0x00}}, /* Linked Track ID */
266 { 0x3001, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x06,0x01,0x01,0x00,0x00,0x00,0x00}}, /* SampleRate */
267 { 0x3004, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x01,0x02,0x00,0x00}}, /* Essence Container */
268 // Generic Picture Essence Descriptor
269 { 0x320C, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x03,0x01,0x04,0x00,0x00,0x00}}, /* Frame Layout */
270 { 0x320D, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x01,0x03,0x02,0x05,0x00,0x00,0x00}}, /* Video Line Map */
271 { 0x3203, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x02,0x02,0x00,0x00,0x00}}, /* Stored Width */
272 { 0x3202, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x02,0x01,0x00,0x00,0x00}}, /* Stored Height */
273 { 0x3209, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x01,0x0C,0x00,0x00,0x00}}, /* Display Width */
274 { 0x3208, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x01,0x0B,0x00,0x00,0x00}}, /* Display Height */
275 { 0x320E, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x01,0x01,0x01,0x00,0x00,0x00}}, /* Aspect Ratio */
276 { 0x3201, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x01,0x06,0x01,0x00,0x00,0x00,0x00}}, /* Picture Essence Coding */
277 // CDCI Picture Essence Descriptor
278 { 0x3301, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x01,0x05,0x03,0x0A,0x00,0x00,0x00}}, /* Component Depth */
279 { 0x3302, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x01,0x05,0x00,0x00,0x00}}, /* Horizontal Subsampling */
280 // Generic Sound Essence Descriptor
281 { 0x3D02, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x04,0x02,0x03,0x01,0x04,0x00,0x00,0x00}}, /* Locked/Unlocked */
282 { 0x3D03, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x02,0x03,0x01,0x01,0x01,0x00,0x00}}, /* Audio sampling rate */
283 { 0x3D07, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x02,0x01,0x01,0x04,0x00,0x00,0x00}}, /* ChannelCount */
284 { 0x3D01, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x04,0x02,0x03,0x03,0x04,0x00,0x00,0x00}}, /* Quantization bits */
285 { 0x3D06, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x02,0x04,0x02,0x00,0x00,0x00,0x00}}, /* Sound Essence Compression */
286 // Index Table Segment
287 { 0x3F0B, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x05,0x30,0x04,0x06,0x00,0x00,0x00,0x00}}, /* Index Edit Rate */
288 { 0x3F0C, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x07,0x02,0x01,0x03,0x01,0x0A,0x00,0x00}}, /* Index Start Position */
289 { 0x3F0D, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x07,0x02,0x02,0x01,0x01,0x02,0x00,0x00}}, /* Index Duration */
290 { 0x3F05, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x04,0x06,0x02,0x01,0x00,0x00,0x00,0x00}}, /* Edit Unit Byte Count */
291 { 0x3F06, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x01,0x03,0x04,0x05,0x00,0x00,0x00,0x00}}, /* IndexSID */
292 { 0x3F08, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x04,0x04,0x04,0x01,0x01,0x00,0x00,0x00}}, /* Slice Count */
293 { 0x3F09, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x04,0x04,0x01,0x06,0x00,0x00,0x00}}, /* Delta Entry Array */
294 { 0x3F0A, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x04,0x04,0x02,0x05,0x00,0x00,0x00}}, /* Index Entry Array */
295 // MPEG video Descriptor
296 { 0x8000, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x01,0x06,0x02,0x01,0x0B,0x00,0x00}}, /* BitRate */
297 // Wave Audio Essence Descriptor
298 { 0x3D09, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x02,0x03,0x03,0x05,0x00,0x00,0x00}}, /* Average Bytes Per Second */
299 { 0x3D0A, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x02,0x03,0x02,0x01,0x00,0x00,0x00}}, /* Block Align */
302 static void mxf_write_uuid(ByteIOContext *pb, enum MXFMetadataSetType type, int value)
304 put_buffer(pb, uuid_base, 12);
309 static void mxf_write_umid(ByteIOContext *pb, enum MXFMetadataSetType type, int value)
311 put_buffer(pb, umid_base, 16);
312 mxf_write_uuid(pb, type, value);
315 static void mxf_write_refs_count(ByteIOContext *pb, int ref_count)
317 put_be32(pb, ref_count);
321 static int klv_ber_length(uint64_t len)
326 return (av_log2(len) >> 3) + 2;
329 static int klv_encode_ber_length(ByteIOContext *pb, uint64_t len)
331 // Determine the best BER size
339 size = (av_log2(len) >> 3) + 1;
342 put_byte(pb, 0x80 + size);
345 put_byte(pb, len >> 8 * size & 0xff);
350 static void klv_encode_ber4_length(ByteIOContext *pb, int len)
352 put_byte(pb, 0x80 + 3);
357 * Get essence container ul index
359 static int mxf_get_essence_container_ul_index(enum CodecID id)
362 for (i = 0; mxf_essence_mappings[i].id; i++)
363 if (mxf_essence_mappings[i].id == id)
364 return mxf_essence_mappings[i].index;
368 static void mxf_write_primer_pack(AVFormatContext *s)
370 ByteIOContext *pb = s->pb;
371 int local_tag_number, i = 0;
373 local_tag_number = FF_ARRAY_ELEMS(mxf_local_tag_batch);
375 put_buffer(pb, primer_pack_key, 16);
376 klv_encode_ber_length(pb, local_tag_number * 18 + 8);
378 put_be32(pb, local_tag_number); // local_tag num
379 put_be32(pb, 18); // item size, always 18 according to the specs
381 for (i = 0; i < local_tag_number; i++) {
382 put_be16(pb, mxf_local_tag_batch[i].local_tag);
383 put_buffer(pb, mxf_local_tag_batch[i].uid, 16);
387 static void mxf_write_local_tag(ByteIOContext *pb, int size, int tag)
393 static void mxf_write_metadata_key(ByteIOContext *pb, unsigned int value)
395 put_buffer(pb, header_metadata_key, 13);
399 static void mxf_free(AVFormatContext *s)
403 for (i = 0; i < s->nb_streams; i++) {
404 AVStream *st = s->streams[i];
405 av_freep(&st->priv_data);
409 static const MXFCodecUL *mxf_get_data_definition_ul(int type)
411 const MXFCodecUL *uls = ff_mxf_data_definition_uls;
412 while (uls->uid[0]) {
420 static void mxf_write_essence_container_refs(AVFormatContext *s)
422 MXFContext *c = s->priv_data;
423 ByteIOContext *pb = s->pb;
426 mxf_write_refs_count(pb, c->essence_container_count);
427 av_log(s,AV_LOG_DEBUG, "essence container count:%d\n", c->essence_container_count);
428 for (i = 0; i < c->essence_container_count; i++) {
429 MXFStreamContext *sc = s->streams[i]->priv_data;
430 put_buffer(pb, mxf_essence_container_uls[sc->index].container_ul, 16);
434 static void mxf_write_preface(AVFormatContext *s)
436 MXFContext *mxf = s->priv_data;
437 ByteIOContext *pb = s->pb;
439 mxf_write_metadata_key(pb, 0x012f00);
440 PRINT_KEY(s, "preface key", pb->buf_ptr - 16);
441 klv_encode_ber_length(pb, 130 + 16 * mxf->essence_container_count);
443 // write preface set uid
444 mxf_write_local_tag(pb, 16, 0x3C0A);
445 mxf_write_uuid(pb, Preface, 0);
446 PRINT_KEY(s, "preface uid", pb->buf_ptr - 16);
448 // last modified date
449 mxf_write_local_tag(pb, 8, 0x3B02);
450 put_be64(pb, mxf->timestamp);
453 mxf_write_local_tag(pb, 2, 0x3B05);
456 // write identification_refs
457 mxf_write_local_tag(pb, 16 + 8, 0x3B06);
458 mxf_write_refs_count(pb, 1);
459 mxf_write_uuid(pb, Identification, 0);
461 // write content_storage_refs
462 mxf_write_local_tag(pb, 16, 0x3B03);
463 mxf_write_uuid(pb, ContentStorage, 0);
465 // operational pattern
466 mxf_write_local_tag(pb, 16, 0x3B09);
467 if (s->nb_streams > 1) {
468 put_buffer(pb, op1a_ul, 14);
469 put_be16(pb, 0x0900); // multi track
471 put_buffer(pb, op1a_ul, 16);
474 // write essence_container_refs
475 mxf_write_local_tag(pb, 8 + 16 * mxf->essence_container_count, 0x3B0A);
476 mxf_write_essence_container_refs(s);
478 // write dm_scheme_refs
479 mxf_write_local_tag(pb, 8, 0x3B0B);
484 * Write a local tag containing an ascii string as utf-16
486 static void mxf_write_local_tag_utf16(ByteIOContext *pb, int tag, const char *value)
488 int i, size = strlen(value);
489 mxf_write_local_tag(pb, size*2, tag);
490 for (i = 0; i < size; i++)
491 put_be16(pb, value[i]);
494 static void mxf_write_identification(AVFormatContext *s)
496 MXFContext *mxf = s->priv_data;
497 ByteIOContext *pb = s->pb;
498 const char *company = "FFmpeg";
499 const char *product = "OP1a Muxer";
503 mxf_write_metadata_key(pb, 0x013000);
504 PRINT_KEY(s, "identification key", pb->buf_ptr - 16);
506 version = s->streams[0]->codec->flags & CODEC_FLAG_BITEXACT ?
507 "0.0.0" : AV_STRINGIFY(LIBAVFORMAT_VERSION);
508 length = 84 + (strlen(company)+strlen(product)+strlen(version))*2; // utf-16
509 klv_encode_ber_length(pb, length);
512 mxf_write_local_tag(pb, 16, 0x3C0A);
513 mxf_write_uuid(pb, Identification, 0);
514 PRINT_KEY(s, "identification uid", pb->buf_ptr - 16);
516 // write generation uid
517 mxf_write_local_tag(pb, 16, 0x3C09);
518 mxf_write_uuid(pb, Identification, 1);
520 mxf_write_local_tag_utf16(pb, 0x3C01, company); // Company Name
521 mxf_write_local_tag_utf16(pb, 0x3C02, product); // Product Name
522 mxf_write_local_tag_utf16(pb, 0x3C04, version); // Version String
525 mxf_write_local_tag(pb, 16, 0x3C05);
526 mxf_write_uuid(pb, Identification, 2);
529 mxf_write_local_tag(pb, 8, 0x3C06);
530 put_be64(pb, mxf->timestamp);
533 static void mxf_write_content_storage(AVFormatContext *s)
535 ByteIOContext *pb = s->pb;
537 mxf_write_metadata_key(pb, 0x011800);
538 PRINT_KEY(s, "content storage key", pb->buf_ptr - 16);
539 klv_encode_ber_length(pb, 92);
542 mxf_write_local_tag(pb, 16, 0x3C0A);
543 mxf_write_uuid(pb, ContentStorage, 0);
544 PRINT_KEY(s, "content storage uid", pb->buf_ptr - 16);
546 // write package reference
547 mxf_write_local_tag(pb, 16 * 2 + 8, 0x1901);
548 mxf_write_refs_count(pb, 2);
549 mxf_write_uuid(pb, MaterialPackage, 0);
550 mxf_write_uuid(pb, SourcePackage, 0);
552 // write essence container data
553 mxf_write_local_tag(pb, 8 + 16, 0x1902);
554 mxf_write_refs_count(pb, 1);
555 mxf_write_uuid(pb, EssenceContainerData, 0);
558 static void mxf_write_track(AVFormatContext *s, AVStream *st, enum MXFMetadataSetType type)
560 MXFContext *mxf = s->priv_data;
561 ByteIOContext *pb = s->pb;
562 MXFStreamContext *sc = st->priv_data;
564 mxf_write_metadata_key(pb, 0x013b00);
565 PRINT_KEY(s, "track key", pb->buf_ptr - 16);
566 klv_encode_ber_length(pb, 80);
569 mxf_write_local_tag(pb, 16, 0x3C0A);
570 mxf_write_uuid(pb, type == MaterialPackage ? Track : Track + TypeBottom, st->index);
571 PRINT_KEY(s, "track uid", pb->buf_ptr - 16);
574 mxf_write_local_tag(pb, 4, 0x4801);
575 put_be32(pb, st->index);
577 // write track number
578 mxf_write_local_tag(pb, 4, 0x4804);
579 if (type == MaterialPackage)
580 put_be32(pb, 0); // track number of material package is 0
582 put_buffer(pb, sc->track_essence_element_key + 12, 4);
584 mxf_write_local_tag(pb, 8, 0x4B01);
585 put_be32(pb, mxf->time_base.den);
586 put_be32(pb, mxf->time_base.num);
589 mxf_write_local_tag(pb, 8, 0x4B02);
592 // write sequence refs
593 mxf_write_local_tag(pb, 16, 0x4803);
594 mxf_write_uuid(pb, type == MaterialPackage ? Sequence: Sequence + TypeBottom, st->index);
597 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 };
599 static void mxf_write_common_fields(AVFormatContext *s, AVStream *st)
601 MXFContext *mxf = s->priv_data;
602 ByteIOContext *pb = s->pb;
604 // find data define uls
605 mxf_write_local_tag(pb, 16, 0x0201);
606 if (st == mxf->timecode_track)
607 put_buffer(pb, smpte_12m_timecode_track_data_ul, 16);
609 const MXFCodecUL *data_def_ul = mxf_get_data_definition_ul(st->codec->codec_type);
610 put_buffer(pb, data_def_ul->uid, 16);
614 mxf_write_local_tag(pb, 8, 0x0202);
615 put_be64(pb, mxf->duration);
618 static void mxf_write_sequence(AVFormatContext *s, AVStream *st, enum MXFMetadataSetType type)
620 MXFContext *mxf = s->priv_data;
621 ByteIOContext *pb = s->pb;
622 enum MXFMetadataSetType component;
624 mxf_write_metadata_key(pb, 0x010f00);
625 PRINT_KEY(s, "sequence key", pb->buf_ptr - 16);
626 klv_encode_ber_length(pb, 80);
628 mxf_write_local_tag(pb, 16, 0x3C0A);
629 mxf_write_uuid(pb, type == MaterialPackage ? Sequence: Sequence + TypeBottom, st->index);
631 PRINT_KEY(s, "sequence uid", pb->buf_ptr - 16);
632 mxf_write_common_fields(s, st);
634 // write structural component
635 mxf_write_local_tag(pb, 16 + 8, 0x1001);
636 mxf_write_refs_count(pb, 1);
637 if (st == mxf->timecode_track)
638 component = TimecodeComponent;
639 else if (type == MaterialPackage)
640 component = SourceClip;
642 component = SourceClip+TypeBottom;
643 mxf_write_uuid(pb, component, st->index);
646 static void mxf_write_timecode_component(AVFormatContext *s, AVStream *st)
648 MXFContext *mxf = s->priv_data;
649 ByteIOContext *pb = s->pb;
651 mxf_write_metadata_key(pb, 0x011400);
652 klv_encode_ber_length(pb, 75);
655 mxf_write_local_tag(pb, 16, 0x3C0A);
656 mxf_write_uuid(pb, TimecodeComponent, st->index);
658 mxf_write_common_fields(s, st);
661 mxf_write_local_tag(pb, 8, 0x1501);
662 put_be64(pb, mxf->timecode_start);
664 // Rounded Time Code Base
665 mxf_write_local_tag(pb, 2, 0x1502);
666 put_be16(pb, mxf->timecode_base);
669 mxf_write_local_tag(pb, 1, 0x1503);
670 put_byte(pb, mxf->timecode_drop_frame);
673 static void mxf_write_structural_component(AVFormatContext *s, AVStream *st, enum MXFMetadataSetType type)
675 ByteIOContext *pb = s->pb;
678 mxf_write_metadata_key(pb, 0x011100);
679 PRINT_KEY(s, "sturctural component key", pb->buf_ptr - 16);
680 klv_encode_ber_length(pb, 108);
683 mxf_write_local_tag(pb, 16, 0x3C0A);
684 mxf_write_uuid(pb, type == MaterialPackage ? SourceClip: SourceClip + TypeBottom, st->index);
686 PRINT_KEY(s, "structural component uid", pb->buf_ptr - 16);
687 mxf_write_common_fields(s, st);
689 // write start_position
690 mxf_write_local_tag(pb, 8, 0x1201);
693 // write source package uid, end of the reference
694 mxf_write_local_tag(pb, 32, 0x1101);
695 if (type == SourcePackage) {
696 for (i = 0; i < 4; i++)
699 mxf_write_umid(pb, SourcePackage, 0);
701 // write source track id
702 mxf_write_local_tag(pb, 4, 0x1102);
703 if (type == SourcePackage)
706 put_be32(pb, st->index);
709 static void mxf_write_multi_descriptor(AVFormatContext *s)
711 MXFContext *mxf = s->priv_data;
712 ByteIOContext *pb = s->pb;
716 mxf_write_metadata_key(pb, 0x014400);
717 PRINT_KEY(s, "multiple descriptor key", pb->buf_ptr - 16);
718 klv_encode_ber_length(pb, 64 + 16 * s->nb_streams);
720 mxf_write_local_tag(pb, 16, 0x3C0A);
721 mxf_write_uuid(pb, MultipleDescriptor, 0);
722 PRINT_KEY(s, "multi_desc uid", pb->buf_ptr - 16);
725 mxf_write_local_tag(pb, 8, 0x3001);
726 put_be32(pb, mxf->time_base.den);
727 put_be32(pb, mxf->time_base.num);
729 // write essence container ul
730 mxf_write_local_tag(pb, 16, 0x3004);
731 if (mxf->essence_container_count > 1)
732 ul = multiple_desc_ul;
734 MXFStreamContext *sc = s->streams[0]->priv_data;
735 ul = mxf_essence_container_uls[sc->index].container_ul;
737 put_buffer(pb, ul, 16);
739 // write sub descriptor refs
740 mxf_write_local_tag(pb, s->nb_streams * 16 + 8, 0x3F01);
741 mxf_write_refs_count(pb, s->nb_streams);
742 for (i = 0; i < s->nb_streams; i++)
743 mxf_write_uuid(pb, SubDescriptor, i);
746 static void mxf_write_generic_desc(AVFormatContext *s, AVStream *st, const UID key, unsigned size)
748 MXFContext *mxf = s->priv_data;
749 MXFStreamContext *sc = st->priv_data;
750 ByteIOContext *pb = s->pb;
752 put_buffer(pb, key, 16);
753 klv_encode_ber_length(pb, size);
755 mxf_write_local_tag(pb, 16, 0x3C0A);
756 mxf_write_uuid(pb, SubDescriptor, st->index);
758 mxf_write_local_tag(pb, 4, 0x3006);
759 put_be32(pb, st->index);
761 mxf_write_local_tag(pb, 8, 0x3001);
762 put_be32(pb, mxf->time_base.den);
763 put_be32(pb, mxf->time_base.num);
765 mxf_write_local_tag(pb, 16, 0x3004);
766 put_buffer(pb, mxf_essence_container_uls[sc->index].container_ul, 16);
769 static const UID mxf_mpegvideo_descriptor_key = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x51,0x00 };
770 static const UID mxf_wav_descriptor_key = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x48,0x00 };
771 static const UID mxf_aes3_descriptor_key = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x47,0x00 };
772 static const UID mxf_cdci_descriptor_key = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0D,0x01,0x01,0x01,0x01,0x01,0x28,0x00 };
773 static const UID mxf_generic_sound_descriptor_key = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0D,0x01,0x01,0x01,0x01,0x01,0x42,0x00 };
775 static void mxf_write_cdci_common(AVFormatContext *s, AVStream *st, const UID key, unsigned size)
777 MXFStreamContext *sc = st->priv_data;
778 ByteIOContext *pb = s->pb;
779 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 av_reduce(&dar.num, &dar.den,
841 st->codec->width*st->codec->sample_aspect_ratio.num,
842 st->codec->height*st->codec->sample_aspect_ratio.den,
845 mxf_write_local_tag(pb, 8, 0x320E);
846 put_be32(pb, dar.num);
847 put_be32(pb, dar.den);
849 mxf_write_local_tag(pb, 16, 0x3201);
850 put_buffer(pb, *sc->codec_ul, 16);
853 static void mxf_write_cdci_desc(AVFormatContext *s, AVStream *st)
855 MXFStreamContext *sc = st->priv_data;
856 mxf_write_cdci_common(s, st, mxf_cdci_descriptor_key, 161+sc->interlaced*4);
859 static void mxf_write_mpegvideo_desc(AVFormatContext *s, AVStream *st)
861 MXFStreamContext *sc = st->priv_data;
862 ByteIOContext *pb = s->pb;
864 mxf_write_cdci_common(s, st, mxf_mpegvideo_descriptor_key, 169+sc->interlaced*4);
867 mxf_write_local_tag(pb, 4, 0x8000);
868 put_be32(pb, st->codec->bit_rate);
871 static void mxf_write_generic_sound_common(AVFormatContext *s, AVStream *st, const UID key, unsigned size)
873 ByteIOContext *pb = s->pb;
875 mxf_write_generic_desc(s, st, key, size);
878 mxf_write_local_tag(pb, 1, 0x3D02);
881 // write audio sampling rate
882 mxf_write_local_tag(pb, 8, 0x3D03);
883 put_be32(pb, st->codec->sample_rate);
886 mxf_write_local_tag(pb, 4, 0x3D07);
887 put_be32(pb, st->codec->channels);
889 mxf_write_local_tag(pb, 4, 0x3D01);
890 put_be32(pb, av_get_bits_per_sample(st->codec->codec_id));
893 static void mxf_write_wav_common(AVFormatContext *s, AVStream *st, const UID key, unsigned size)
895 ByteIOContext *pb = s->pb;
897 mxf_write_generic_sound_common(s, st, key, size);
899 mxf_write_local_tag(pb, 2, 0x3D0A);
900 put_be16(pb, st->codec->block_align);
903 mxf_write_local_tag(pb, 4, 0x3D09);
904 put_be32(pb, st->codec->block_align*st->codec->sample_rate);
907 static void mxf_write_wav_desc(AVFormatContext *s, AVStream *st)
909 mxf_write_wav_common(s, st, mxf_wav_descriptor_key, 107);
912 static void mxf_write_aes3_desc(AVFormatContext *s, AVStream *st)
914 mxf_write_wav_common(s, st, mxf_aes3_descriptor_key, 107);
917 static void mxf_write_generic_sound_desc(AVFormatContext *s, AVStream *st)
919 mxf_write_generic_sound_common(s, st, mxf_generic_sound_descriptor_key, 93);
922 static void mxf_write_package(AVFormatContext *s, enum MXFMetadataSetType type)
924 MXFContext *mxf = s->priv_data;
925 ByteIOContext *pb = s->pb;
928 if (type == MaterialPackage) {
929 track_count = s->nb_streams + 1; // add timecode track
930 mxf_write_metadata_key(pb, 0x013600);
931 PRINT_KEY(s, "Material Package key", pb->buf_ptr - 16);
932 klv_encode_ber_length(pb, 92 + 16*track_count);
934 track_count = s->nb_streams;
935 mxf_write_metadata_key(pb, 0x013700);
936 PRINT_KEY(s, "Source Package key", pb->buf_ptr - 16);
937 klv_encode_ber_length(pb, 112 + 16*track_count); // 20 bytes length for descriptor reference
941 mxf_write_local_tag(pb, 16, 0x3C0A);
942 mxf_write_uuid(pb, type, 0);
943 av_log(s,AV_LOG_DEBUG, "package type:%d\n", type);
944 PRINT_KEY(s, "package uid", pb->buf_ptr - 16);
946 // write package umid
947 mxf_write_local_tag(pb, 32, 0x4401);
948 mxf_write_umid(pb, type, 0);
949 PRINT_KEY(s, "package umid second part", pb->buf_ptr - 16);
951 // package creation date
952 mxf_write_local_tag(pb, 8, 0x4405);
953 put_be64(pb, mxf->timestamp);
955 // package modified date
956 mxf_write_local_tag(pb, 8, 0x4404);
957 put_be64(pb, mxf->timestamp);
960 mxf_write_local_tag(pb, track_count*16 + 8, 0x4403);
961 mxf_write_refs_count(pb, track_count);
962 for (i = 0; i < s->nb_streams; i++)
963 mxf_write_uuid(pb, type == MaterialPackage ? Track : Track + TypeBottom, i);
964 if (type == MaterialPackage)
965 mxf_write_uuid(pb, Track, s->nb_streams); // timecode track
967 // write multiple descriptor reference
968 if (type == SourcePackage) {
969 mxf_write_local_tag(pb, 16, 0x4701);
970 if (s->nb_streams > 1) {
971 mxf_write_uuid(pb, MultipleDescriptor, 0);
972 mxf_write_multi_descriptor(s);
974 mxf_write_uuid(pb, SubDescriptor, 0);
976 // write timecode track
977 mxf_write_track(s, mxf->timecode_track, type);
978 mxf_write_sequence(s, mxf->timecode_track, type);
979 mxf_write_timecode_component(s, mxf->timecode_track);
982 for (i = 0; i < s->nb_streams; i++) {
983 AVStream *st = s->streams[i];
984 mxf_write_track(s, st, type);
985 mxf_write_sequence(s, st, type);
986 mxf_write_structural_component(s, st, type);
988 if (type == SourcePackage) {
989 MXFStreamContext *sc = st->priv_data;
990 mxf_essence_container_uls[sc->index].write_desc(s, st);
995 static int mxf_write_essence_container_data(AVFormatContext *s)
997 ByteIOContext *pb = s->pb;
999 mxf_write_metadata_key(pb, 0x012300);
1000 klv_encode_ber_length(pb, 72);
1002 mxf_write_local_tag(pb, 16, 0x3C0A); // Instance UID
1003 mxf_write_uuid(pb, EssenceContainerData, 0);
1005 mxf_write_local_tag(pb, 32, 0x2701); // Linked Package UID
1006 mxf_write_umid(pb, SourcePackage, 0);
1008 mxf_write_local_tag(pb, 4, 0x3F07); // BodySID
1011 mxf_write_local_tag(pb, 4, 0x3F06); // IndexSID
1017 static int mxf_write_header_metadata_sets(AVFormatContext *s)
1019 mxf_write_preface(s);
1020 mxf_write_identification(s);
1021 mxf_write_content_storage(s);
1022 mxf_write_package(s, MaterialPackage);
1023 mxf_write_package(s, SourcePackage);
1024 mxf_write_essence_container_data(s);
1028 static unsigned klv_fill_size(uint64_t size)
1030 unsigned pad = KAG_SIZE - (size & (KAG_SIZE-1));
1031 if (pad < 20) // smallest fill item possible
1032 return pad + KAG_SIZE;
1034 return pad & (KAG_SIZE-1);
1037 static void mxf_write_index_table_segment(AVFormatContext *s)
1039 MXFContext *mxf = s->priv_data;
1040 ByteIOContext *pb = s->pb;
1042 int temporal_reordering = 0;
1043 int key_index = mxf->last_key_index;
1045 av_log(s, AV_LOG_DEBUG, "edit units count %d\n", mxf->edit_units_count);
1047 if (!mxf->edit_units_count && !mxf->edit_unit_byte_count)
1050 put_buffer(pb, index_table_segment_key, 16);
1052 if (mxf->edit_unit_byte_count) {
1053 klv_encode_ber_length(pb, 85);
1055 klv_encode_ber_length(pb, 85 + 12+(s->nb_streams+1)*6 +
1056 12+mxf->edit_units_count*(11+mxf->slice_count*4));
1060 mxf_write_local_tag(pb, 16, 0x3C0A);
1061 mxf_write_uuid(pb, IndexTableSegment, 0);
1064 mxf_write_local_tag(pb, 8, 0x3F0B);
1065 put_be32(pb, mxf->time_base.num);
1066 put_be32(pb, mxf->time_base.den);
1068 // index start position
1069 mxf_write_local_tag(pb, 8, 0x3F0C);
1070 put_be64(pb, mxf->last_indexed_edit_unit);
1073 mxf_write_local_tag(pb, 8, 0x3F0D);
1074 put_be64(pb, mxf->edit_units_count);
1076 // edit unit byte count
1077 mxf_write_local_tag(pb, 4, 0x3F05);
1078 put_be32(pb, mxf->edit_unit_byte_count);
1081 mxf_write_local_tag(pb, 4, 0x3F06);
1085 mxf_write_local_tag(pb, 4, 0x3F07);
1088 // real slice count - 1
1089 mxf_write_local_tag(pb, 1, 0x3F08);
1090 put_byte(pb, mxf->slice_count);
1092 if (!mxf->edit_unit_byte_count) {
1093 // delta entry array
1094 mxf_write_local_tag(pb, 8 + (s->nb_streams+1)*6, 0x3F09);
1095 put_be32(pb, s->nb_streams+1); // num of entries
1096 put_be32(pb, 6); // size of one entry
1097 // write system item delta entry
1099 put_byte(pb, 0); // slice entry
1100 put_be32(pb, 0); // element delta
1101 for (i = 0; i < s->nb_streams; i++) {
1102 AVStream *st = s->streams[i];
1103 MXFStreamContext *sc = st->priv_data;
1104 put_byte(pb, sc->temporal_reordering);
1105 if (sc->temporal_reordering)
1106 temporal_reordering = 1;
1107 if (i == 0) { // video track
1108 put_byte(pb, 0); // slice number
1109 put_be32(pb, KAG_SIZE); // system item size including klv fill
1110 } else { // audio track
1111 unsigned audio_frame_size = sc->aic.samples[0]*sc->aic.sample_size;
1112 audio_frame_size += klv_fill_size(audio_frame_size);
1114 put_be32(pb, (i-1)*audio_frame_size); // element delta
1118 mxf_write_local_tag(pb, 8 + mxf->edit_units_count*(11+mxf->slice_count*4), 0x3F0A);
1119 put_be32(pb, mxf->edit_units_count); // num of entries
1120 put_be32(pb, 11+mxf->slice_count*4); // size of one entry
1121 for (i = 0; i < mxf->edit_units_count; i++) {
1122 if (temporal_reordering) {
1123 int temporal_offset = 0;
1124 for (j = i+1; j < mxf->edit_units_count; j++) {
1126 if (mxf->index_entries[j].flags & 0x10) { // backward prediction
1127 // next is not b, so is reordered
1128 if (!(mxf->index_entries[i+1].flags & 0x10)) {
1129 if ((mxf->index_entries[i].flags & 0x11) == 0) // i frame
1130 temporal_offset = 0;
1132 temporal_offset = -temporal_offset;
1137 put_byte(pb, temporal_offset);
1140 if (!(mxf->index_entries[i].flags & 0x33)) { // I frame
1141 mxf->last_key_index = key_index;
1144 if (mxf->index_entries[i].flags & 0x10 && // backward prediction
1145 !(mxf->index_entries[key_index].flags & 0x80)) { // open gop
1146 put_byte(pb, mxf->last_key_index - i);
1148 put_byte(pb, key_index - i); // key frame offset
1149 if ((mxf->index_entries[i].flags & 0x20) == 0x20) // only forward
1150 mxf->last_key_index = key_index;
1152 put_byte(pb, mxf->index_entries[i].flags);
1154 put_be64(pb, mxf->index_entries[i].offset - mxf->first_edit_unit_offset);
1155 if (s->nb_streams > 1)
1156 put_be32(pb, mxf->index_entries[i].slice_offset);
1159 mxf->last_key_index = key_index - mxf->edit_units_count;
1160 mxf->last_indexed_edit_unit += mxf->edit_units_count;
1161 mxf->edit_units_count = 0;
1165 static void mxf_write_klv_fill(AVFormatContext *s)
1167 unsigned pad = klv_fill_size(url_ftell(s->pb));
1169 put_buffer(s->pb, klv_fill_key, 16);
1171 klv_encode_ber4_length(s->pb, pad);
1174 assert(!(url_ftell(s->pb) & (KAG_SIZE-1)));
1178 static void mxf_write_partition(AVFormatContext *s, int bodysid,
1180 const uint8_t *key, int write_metadata)
1182 MXFContext *mxf = s->priv_data;
1183 ByteIOContext *pb = s->pb;
1184 int64_t header_byte_count_offset;
1185 unsigned index_byte_count = 0;
1186 uint64_t partition_offset = url_ftell(pb);
1188 if (!mxf->edit_unit_byte_count && mxf->edit_units_count)
1189 index_byte_count = 85 + 12+(s->nb_streams+1)*6 +
1190 12+mxf->edit_units_count*(11+mxf->slice_count*4);
1191 else if (mxf->edit_unit_byte_count && indexsid)
1192 index_byte_count = 85;
1194 if (index_byte_count) {
1195 // add encoded ber length
1196 index_byte_count += 16 + klv_ber_length(index_byte_count);
1197 index_byte_count += klv_fill_size(index_byte_count);
1200 if (!memcmp(key, body_partition_key, 16)) {
1201 mxf->body_partition_offset =
1202 av_realloc(mxf->body_partition_offset,
1203 (mxf->body_partitions_count+1)*
1204 sizeof(*mxf->body_partition_offset));
1205 mxf->body_partition_offset[mxf->body_partitions_count++] = partition_offset;
1209 put_buffer(pb, key, 16);
1210 klv_encode_ber_length(pb, 88 + 16 * mxf->essence_container_count);
1212 // write partition value
1213 put_be16(pb, 1); // majorVersion
1214 put_be16(pb, 2); // minorVersion
1215 put_be32(pb, KAG_SIZE); // KAGSize
1217 put_be64(pb, partition_offset); // ThisPartition
1219 if (!memcmp(key, body_partition_key, 16) && mxf->body_partitions_count > 1)
1220 put_be64(pb, mxf->body_partition_offset[mxf->body_partitions_count-2]); // PreviousPartition
1221 else if (!memcmp(key, footer_partition_key, 16) && mxf->body_partitions_count)
1222 put_be64(pb, mxf->body_partition_offset[mxf->body_partitions_count-1]); // PreviousPartition
1226 put_be64(pb, mxf->footer_partition_offset); // footerPartition
1229 header_byte_count_offset = url_ftell(pb);
1230 put_be64(pb, 0); // headerByteCount, update later
1233 put_be64(pb, index_byte_count); // indexByteCount
1234 put_be32(pb, index_byte_count ? indexsid : 0); // indexSID
1237 if (bodysid && mxf->edit_units_count && mxf->body_partitions_count) {
1238 uint64_t partition_end = url_ftell(pb) + 8 + 4 + 16 + 8 +
1239 16*mxf->essence_container_count;
1240 put_be64(pb, partition_end + klv_fill_size(partition_end) +
1241 index_byte_count - mxf->first_edit_unit_offset);
1245 put_be32(pb, bodysid); // bodySID
1247 // operational pattern
1248 if (s->nb_streams > 1) {
1249 put_buffer(pb, op1a_ul, 14);
1250 put_be16(pb, 0x0900); // multi track
1252 put_buffer(pb, op1a_ul, 16);
1255 // essence container
1256 mxf_write_essence_container_refs(s);
1258 if (write_metadata) {
1259 // mark the start of the headermetadata and calculate metadata size
1261 unsigned header_byte_count;
1263 mxf_write_klv_fill(s);
1264 start = url_ftell(s->pb);
1265 mxf_write_primer_pack(s);
1266 mxf_write_header_metadata_sets(s);
1267 pos = url_ftell(s->pb);
1268 header_byte_count = pos - start + klv_fill_size(pos);
1270 // update header_byte_count
1271 url_fseek(pb, header_byte_count_offset, SEEK_SET);
1272 put_be64(pb, header_byte_count);
1273 url_fseek(pb, pos, SEEK_SET);
1276 put_flush_packet(pb);
1279 static const UID mxf_mpeg2_codec_uls[] = {
1280 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x01,0x10,0x00 }, // MP-ML I-Frame
1281 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x01,0x11,0x00 }, // MP-ML Long GOP
1282 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x02,0x02,0x00 }, // 422P-ML I-Frame
1283 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x02,0x03,0x00 }, // 422P-ML Long GOP
1284 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x03,0x02,0x00 }, // MP-HL I-Frame
1285 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x03,0x03,0x00 }, // MP-HL Long GOP
1286 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x04,0x02,0x00 }, // 422P-HL I-Frame
1287 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x04,0x03,0x00 }, // 422P-HL Long GOP
1290 static const UID *mxf_get_mpeg2_codec_ul(AVCodecContext *avctx)
1292 if (avctx->profile == 4) { // Main
1293 if (avctx->level == 8) // Main
1294 return avctx->gop_size ?
1295 &mxf_mpeg2_codec_uls[1] :
1296 &mxf_mpeg2_codec_uls[0];
1297 else if (avctx->level == 4) // High
1298 return avctx->gop_size ?
1299 &mxf_mpeg2_codec_uls[5] :
1300 &mxf_mpeg2_codec_uls[4];
1301 } else if (avctx->profile == 0) { // 422
1302 if (avctx->level == 5) // Main
1303 return avctx->gop_size ?
1304 &mxf_mpeg2_codec_uls[3] :
1305 &mxf_mpeg2_codec_uls[2];
1306 else if (avctx->level == 2) // High
1307 return avctx->gop_size ?
1308 &mxf_mpeg2_codec_uls[7] :
1309 &mxf_mpeg2_codec_uls[6];
1314 static int mxf_parse_mpeg2_frame(AVFormatContext *s, AVStream *st, AVPacket *pkt, int *flags)
1316 MXFStreamContext *sc = st->priv_data;
1317 MXFContext *mxf = s->priv_data;
1323 for(i = 0; i < pkt->size - 4; i++) {
1324 c = (c<<8) + pkt->data[i];
1326 if (i + 2 < pkt->size && (pkt->data[i+1] & 0xf0) == 0x10) { // seq ext
1327 st->codec->profile = pkt->data[i+1] & 0x07;
1328 st->codec->level = pkt->data[i+2] >> 4;
1329 } else if (i + 5 < pkt->size && (pkt->data[i+1] & 0xf0) == 0x80) { // pict coding ext
1330 sc->interlaced = !(pkt->data[i+5] & 0x80); // progressive frame
1333 } else if (c == 0x1b8) { // gop
1334 if (i + 4 < pkt->size) {
1335 if (pkt->data[i+4]>>6 & 0x01) // closed
1336 *flags |= 0x80; // random access
1337 if (!mxf->header_written) {
1338 unsigned hours = (pkt->data[i+1]>>2) & 0x1f;
1339 unsigned minutes = ((pkt->data[i+1] & 0x03) << 4) | (pkt->data[i+2]>>4);
1340 unsigned seconds = ((pkt->data[i+2] & 0x07) << 3) | (pkt->data[i+3]>>5);
1341 unsigned frames = ((pkt->data[i+3] & 0x1f) << 1) | (pkt->data[i+4]>>7);
1342 mxf->timecode_drop_frame = !!(pkt->data[i+1] & 0x80);
1343 mxf->timecode_start = (hours*3600 + minutes*60 + seconds) *
1344 mxf->timecode_base + frames;
1345 if (mxf->timecode_drop_frame) {
1346 unsigned tminutes = 60 * hours + minutes;
1347 mxf->timecode_start -= 2 * (tminutes - tminutes / 10);
1349 av_log(s, AV_LOG_DEBUG, "frame %d %d:%d:%d%c%d\n", mxf->timecode_start,
1350 hours, minutes, seconds, mxf->timecode_drop_frame ? ';':':', frames);
1353 } else if (c == 0x1b3) { // seq
1355 } else if (c == 0x100) { // pic
1356 int pict_type = (pkt->data[i+2]>>3) & 0x07;
1357 if (pict_type == 2) { // P frame
1359 st->codec->gop_size = 1;
1360 } else if (pict_type == 3) { // B frame
1362 sc->temporal_reordering = -1;
1363 } else if (!pict_type) {
1364 av_log(s, AV_LOG_ERROR, "error parsing mpeg2 frame\n");
1369 if (s->oformat != &mxf_d10_muxer)
1370 sc->codec_ul = mxf_get_mpeg2_codec_ul(st->codec);
1371 return !!sc->codec_ul;
1374 static uint64_t mxf_parse_timestamp(time_t timestamp)
1376 struct tm *time = localtime(×tamp);
1377 return (uint64_t)(time->tm_year+1900) << 48 |
1378 (uint64_t)(time->tm_mon+1) << 40 |
1379 (uint64_t) time->tm_mday << 32 |
1380 time->tm_hour << 24 |
1381 time->tm_min << 16 |
1385 static int mxf_write_header(AVFormatContext *s)
1387 MXFContext *mxf = s->priv_data;
1389 uint8_t present[FF_ARRAY_ELEMS(mxf_essence_container_uls)] = {0};
1390 const int *samples_per_frame = NULL;
1392 for (i = 0; i < s->nb_streams; i++) {
1393 AVStream *st = s->streams[i];
1394 MXFStreamContext *sc = av_mallocz(sizeof(*sc));
1396 return AVERROR(ENOMEM);
1399 if (st->codec->codec_type == CODEC_TYPE_VIDEO) {
1401 av_log(s, AV_LOG_ERROR, "video stream must be first track\n");
1404 if (fabs(av_q2d(st->codec->time_base) - 1/25.0) < 0.0001) {
1405 samples_per_frame = PAL_samples_per_frame;
1406 mxf->time_base = (AVRational){ 1, 25 };
1407 mxf->timecode_base = 25;
1408 } else if (fabs(av_q2d(st->codec->time_base) - 1001/30000.0) < 0.0001) {
1409 samples_per_frame = NTSC_samples_per_frame;
1410 mxf->time_base = (AVRational){ 1001, 30000 };
1411 mxf->timecode_base = 30;
1413 av_log(s, AV_LOG_ERROR, "unsupported video frame rate\n");
1416 av_set_pts_info(st, 64, mxf->time_base.num, mxf->time_base.den);
1417 if (s->oformat == &mxf_d10_muxer) {
1418 if (st->codec->bit_rate == 50000000)
1419 if (mxf->time_base.den == 25) sc->index = 3;
1421 else if (st->codec->bit_rate == 40000000)
1422 if (mxf->time_base.den == 25) sc->index = 7;
1424 else if (st->codec->bit_rate == 30000000)
1425 if (mxf->time_base.den == 25) sc->index = 11;
1426 else sc->index = 13;
1428 av_log(s, AV_LOG_ERROR, "error MXF D-10 only support 30/40/50 mbit/s\n");
1432 mxf->edit_unit_byte_count = KAG_SIZE; // system element
1433 mxf->edit_unit_byte_count += 16 + 4 + (uint64_t)st->codec->bit_rate *
1434 mxf->time_base.num / (8*mxf->time_base.den);
1435 mxf->edit_unit_byte_count += klv_fill_size(mxf->edit_unit_byte_count);
1436 mxf->edit_unit_byte_count += 16 + 4 + 4 + samples_per_frame[0]*8*4;
1437 mxf->edit_unit_byte_count += klv_fill_size(mxf->edit_unit_byte_count);
1439 } else if (st->codec->codec_type == CODEC_TYPE_AUDIO) {
1440 if (st->codec->sample_rate != 48000) {
1441 av_log(s, AV_LOG_ERROR, "only 48khz is implemented\n");
1444 av_set_pts_info(st, 64, 1, st->codec->sample_rate);
1445 if (s->oformat == &mxf_d10_muxer) {
1446 if (st->index != 1) {
1447 av_log(s, AV_LOG_ERROR, "MXF D-10 only support one audio track\n");
1450 if (st->codec->codec_id != CODEC_ID_PCM_S16LE &&
1451 st->codec->codec_id != CODEC_ID_PCM_S24LE) {
1452 av_log(s, AV_LOG_ERROR, "MXF D-10 only support 16 or 24 bits le audio\n");
1454 sc->index = ((MXFStreamContext*)s->streams[0]->priv_data)->index + 1;
1456 mxf->slice_count = 1;
1460 sc->index = mxf_get_essence_container_ul_index(st->codec->codec_id);
1461 if (sc->index == -1) {
1462 av_log(s, AV_LOG_ERROR, "track %d: could not find essence container ul, "
1463 "codec not currently supported in container\n", i);
1468 sc->codec_ul = &mxf_essence_container_uls[sc->index].codec_ul;
1470 if (!present[sc->index]) {
1471 present[sc->index] = 1;
1472 mxf->essence_container_count++;
1474 present[sc->index]++;
1476 memcpy(sc->track_essence_element_key, mxf_essence_container_uls[sc->index].element_ul, 15);
1477 sc->track_essence_element_key[15] = present[sc->index];
1478 PRINT_KEY(s, "track essence element key", sc->track_essence_element_key);
1481 if (s->oformat == &mxf_d10_muxer) {
1482 mxf->essence_container_count = 1;
1485 for (i = 0; i < s->nb_streams; i++) {
1486 MXFStreamContext *sc = s->streams[i]->priv_data;
1487 // update element count
1488 sc->track_essence_element_key[13] = present[sc->index];
1489 sc->order = AV_RB32(sc->track_essence_element_key+12);
1492 mxf->timestamp = mxf_parse_timestamp(s->timestamp);
1495 mxf->timecode_track = av_mallocz(sizeof(*mxf->timecode_track));
1496 if (!mxf->timecode_track)
1497 return AVERROR(ENOMEM);
1498 mxf->timecode_track->priv_data = av_mallocz(sizeof(MXFStreamContext));
1499 if (!mxf->timecode_track->priv_data)
1500 return AVERROR(ENOMEM);
1501 mxf->timecode_track->index = s->nb_streams;
1503 if (!samples_per_frame)
1504 samples_per_frame = PAL_samples_per_frame;
1506 if (ff_audio_interleave_init(s, samples_per_frame, mxf->time_base) < 0)
1512 static const uint8_t system_metadata_pack_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0D,0x01,0x03,0x01,0x04,0x01,0x01,0x00 };
1513 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 };
1515 static uint32_t ff_framenum_to_12m_time_code(unsigned frame, int drop, int fps)
1517 return (0 << 31) | // color frame flag
1518 (0 << 30) | // drop frame flag
1519 ( ((frame % fps) / 10) << 28) | // tens of frames
1520 ( ((frame % fps) % 10) << 24) | // units of frames
1521 (0 << 23) | // field phase (NTSC), b0 (PAL)
1522 ((((frame / fps) % 60) / 10) << 20) | // tens of seconds
1523 ((((frame / fps) % 60) % 10) << 16) | // units of seconds
1524 (0 << 15) | // b0 (NTSC), b2 (PAL)
1525 ((((frame / (fps * 60)) % 60) / 10) << 12) | // tens of minutes
1526 ((((frame / (fps * 60)) % 60) % 10) << 8) | // units of minutes
1528 (0 << 6) | // b2 (NSC), field phase (PAL)
1529 ((((frame / (fps * 3600) % 24)) / 10) << 4) | // tens of hours
1530 ( (frame / (fps * 3600) % 24)) % 10; // units of hours
1533 static void mxf_write_system_item(AVFormatContext *s)
1535 MXFContext *mxf = s->priv_data;
1536 ByteIOContext *pb = s->pb;
1540 frame = mxf->timecode_start + mxf->last_indexed_edit_unit + mxf->edit_units_count;
1542 // write system metadata pack
1543 put_buffer(pb, system_metadata_pack_key, 16);
1544 klv_encode_ber4_length(pb, 57);
1545 put_byte(pb, 0x5c); // UL, user date/time stamp, picture and sound item present
1546 put_byte(pb, 0x04); // content package rate
1547 put_byte(pb, 0x00); // content package type
1548 put_be16(pb, 0x00); // channel handle
1549 put_be16(pb, frame); // continuity count
1550 if (mxf->essence_container_count > 1)
1551 put_buffer(pb, multiple_desc_ul, 16);
1553 MXFStreamContext *sc = s->streams[0]->priv_data;
1554 put_buffer(pb, mxf_essence_container_uls[sc->index].container_ul, 16);
1558 put_be64(pb, 0); // creation date/time stamp
1560 put_byte(pb, 0x81); // SMPTE 12M time code
1561 time_code = ff_framenum_to_12m_time_code(frame, mxf->timecode_drop_frame, mxf->timecode_base);
1562 put_be32(pb, time_code);
1563 put_be32(pb, 0); // binary group data
1566 // write system metadata package set
1567 put_buffer(pb, system_metadata_package_set_key, 16);
1568 klv_encode_ber4_length(pb, 35);
1569 put_byte(pb, 0x83); // UMID
1571 mxf_write_umid(pb, SourcePackage, 0);
1574 static void mxf_write_d10_video_packet(AVFormatContext *s, AVStream *st, AVPacket *pkt)
1576 MXFContext *mxf = s->priv_data;
1577 ByteIOContext *pb = s->pb;
1578 int packet_size = (uint64_t)st->codec->bit_rate*mxf->time_base.num /
1579 (8*mxf->time_base.den); // frame size
1582 packet_size += 16 + 4;
1583 packet_size += klv_fill_size(packet_size);
1585 klv_encode_ber4_length(pb, pkt->size);
1586 put_buffer(pb, pkt->data, pkt->size);
1588 // ensure CBR muxing by padding to correct video frame size
1589 pad = packet_size - pkt->size - 16 - 4;
1591 put_buffer(s->pb, klv_fill_key, 16);
1593 klv_encode_ber4_length(s->pb, pad);
1596 assert(!(url_ftell(s->pb) & (KAG_SIZE-1)));
1598 av_log(s, AV_LOG_WARNING, "cannot fill d-10 video packet\n");
1599 for (; pad > 0; pad--)
1604 static void mxf_write_d10_audio_packet(AVFormatContext *s, AVStream *st, AVPacket *pkt)
1606 MXFContext *mxf = s->priv_data;
1607 ByteIOContext *pb = s->pb;
1608 int frame_size = (pkt->size<<3) /
1609 (st->codec->channels*av_get_bits_per_sample(st->codec->codec_id));
1610 uint8_t *samples = pkt->data;
1611 uint8_t *end = pkt->data + pkt->size;
1614 klv_encode_ber4_length(pb, 4 + frame_size*4*8);
1616 put_byte(pb, (frame_size == 1920 ? 0 : (mxf->edit_units_count-1) % 5 + 1));
1617 put_le16(pb, frame_size);
1618 put_byte(pb, (1<<st->codec->channels)-1);
1620 while (samples < end) {
1621 for (i = 0; i < st->codec->channels; i++) {
1623 if (st->codec->codec_id == CODEC_ID_PCM_S24LE) {
1624 sample = (AV_RL24(samples)<< 4)|((samples==pkt->data)<<3) | i;
1627 sample = (AV_RL16(samples)<<12)|((samples==pkt->data)<<3) | i;
1630 put_le32(pb, sample);
1637 static int mxf_write_d10_packet(AVFormatContext *s, AVPacket *pkt)
1639 MXFContext *mxf = s->priv_data;
1640 ByteIOContext *pb = s->pb;
1641 AVStream *st = s->streams[pkt->stream_index];
1642 MXFStreamContext *sc = st->priv_data;
1645 if (st->codec->codec_id == CODEC_ID_MPEG2VIDEO) {
1646 if (!mxf_parse_mpeg2_frame(s, st, pkt, &flags)) {
1647 av_log(s, AV_LOG_ERROR, "could not get mpeg2 profile and level\n");
1652 if (!mxf->header_written) {
1653 mxf_write_partition(s, 1, 2, header_open_partition_key, 1);
1654 mxf->header_written = 1;
1655 mxf_write_klv_fill(s);
1656 mxf_write_index_table_segment(s);
1659 if (st->index == 0) {
1660 mxf_write_klv_fill(s);
1661 mxf_write_system_item(s);
1663 mxf->edit_units_count++;
1666 mxf_write_klv_fill(s);
1667 put_buffer(pb, sc->track_essence_element_key, 16); // write key
1668 if (st->codec->codec_type == CODEC_TYPE_VIDEO)
1669 mxf_write_d10_video_packet(s, st, pkt);
1671 mxf_write_d10_audio_packet(s, st, pkt);
1673 put_flush_packet(pb);
1678 static int mxf_write_packet(AVFormatContext *s, AVPacket *pkt)
1680 MXFContext *mxf = s->priv_data;
1681 ByteIOContext *pb = s->pb;
1682 AVStream *st = s->streams[pkt->stream_index];
1683 MXFStreamContext *sc = st->priv_data;
1686 if (!(mxf->edit_units_count % EDIT_UNITS_PER_BODY)) {
1687 mxf->index_entries = av_realloc(mxf->index_entries,
1688 (mxf->edit_units_count + EDIT_UNITS_PER_BODY)*sizeof(*mxf->index_entries));
1689 if (!mxf->index_entries) {
1690 av_log(s, AV_LOG_ERROR, "could not allocate index entries\n");
1695 if (st->codec->codec_id == CODEC_ID_MPEG2VIDEO) {
1696 if (!mxf_parse_mpeg2_frame(s, st, pkt, &flags)) {
1697 av_log(s, AV_LOG_ERROR, "could not get mpeg2 profile and level\n");
1702 if (!mxf->header_written) {
1703 mxf_write_partition(s, 0, 0, header_open_partition_key, 1);
1704 mxf->header_written = 1;
1707 if (st->index == 0) {
1708 if ((!mxf->edit_units_count || mxf->edit_units_count > EDIT_UNITS_PER_BODY) &&
1709 !(flags & 0x33)) { // I frame, Gop start
1710 mxf_write_klv_fill(s);
1711 mxf_write_partition(s, 1, 2, body_partition_key, 0);
1713 mxf_write_klv_fill(s);
1714 mxf_write_index_table_segment(s);
1717 mxf_write_klv_fill(s);
1718 mxf->index_entries[mxf->edit_units_count].offset = url_ftell(pb);
1719 mxf->index_entries[mxf->edit_units_count].flags = flags;
1720 if (!mxf->first_edit_unit_offset)
1721 mxf->first_edit_unit_offset = mxf->index_entries[0].offset;
1722 mxf_write_system_item(s);
1724 mxf->edit_units_count++;
1725 } else if (st->index == 1) {
1726 mxf->index_entries[mxf->edit_units_count-1].slice_offset =
1727 url_ftell(pb) - mxf->index_entries[mxf->edit_units_count-1].offset;
1730 mxf_write_klv_fill(s);
1731 put_buffer(pb, sc->track_essence_element_key, 16); // write key
1732 klv_encode_ber4_length(pb, pkt->size); // write length
1733 put_buffer(pb, pkt->data, pkt->size); // write value
1735 put_flush_packet(pb);
1739 static void mxf_write_random_index_pack(AVFormatContext *s)
1741 MXFContext *mxf = s->priv_data;
1742 ByteIOContext *pb = s->pb;
1743 uint64_t pos = url_ftell(pb);
1746 put_buffer(pb, random_index_pack_key, 16);
1747 klv_encode_ber_length(pb, 28 + 12*mxf->body_partitions_count);
1749 put_be32(pb, 0); // BodySID of header partition
1750 put_be64(pb, 0); // offset of header partition
1752 for (i = 0; i < mxf->body_partitions_count; i++) {
1753 put_be32(pb, 1); // BodySID
1754 put_be64(pb, mxf->body_partition_offset[i]);
1757 put_be32(pb, 0); // BodySID of footer partition
1758 put_be64(pb, mxf->footer_partition_offset);
1760 put_be32(pb, url_ftell(pb) - pos + 4);
1763 static int mxf_write_footer(AVFormatContext *s)
1765 MXFContext *mxf = s->priv_data;
1766 ByteIOContext *pb = s->pb;
1768 mxf->duration = mxf->last_indexed_edit_unit + mxf->edit_units_count;
1770 mxf_write_klv_fill(s);
1771 mxf->footer_partition_offset = url_ftell(pb);
1772 if (mxf->edit_unit_byte_count) { // no need to repeat index
1773 mxf_write_partition(s, 0, 0, footer_partition_key, 0);
1775 mxf_write_partition(s, 0, 2, footer_partition_key, 0);
1777 mxf_write_klv_fill(s);
1778 mxf_write_index_table_segment(s);
1781 mxf_write_klv_fill(s);
1782 mxf_write_random_index_pack(s);
1784 if (!url_is_streamed(s->pb)) {
1786 url_fseek(pb, 0, SEEK_SET);
1787 if (s->oformat == &mxf_d10_muxer) {
1788 mxf_write_partition(s, 1, 2, header_closed_partition_key, 1);
1790 } else if (mxf->edit_unit_byte_count) {
1791 mxf_write_partition(s, 0, 2, header_closed_partition_key, 1);
1794 mxf_write_partition(s, 0, 0, header_closed_partition_key, 1);
1798 mxf_write_klv_fill(s);
1799 mxf_write_index_table_segment(s);
1803 ff_audio_interleave_close(s);
1805 av_freep(&mxf->index_entries);
1806 av_freep(&mxf->body_partition_offset);
1807 av_freep(&mxf->timecode_track->priv_data);
1808 av_freep(&mxf->timecode_track);
1815 static int mxf_interleave_get_packet(AVFormatContext *s, AVPacket *out, AVPacket *pkt, int flush)
1818 int stream_count = 0;
1819 int streams[MAX_STREAMS];
1821 memset(streams, 0, sizeof(streams));
1822 pktl = s->packet_buffer;
1824 //av_log(s, AV_LOG_DEBUG, "show st:%d dts:%lld\n", pktl->pkt.stream_index, pktl->pkt.dts);
1825 if (!streams[pktl->pkt.stream_index])
1827 streams[pktl->pkt.stream_index]++;
1831 if (stream_count && (s->nb_streams == stream_count || flush)) {
1832 pktl = s->packet_buffer;
1833 if (s->nb_streams != stream_count) {
1834 AVPacketList *first = NULL;
1835 AVPacketList *last = NULL;
1836 // find first packet in edit unit
1838 AVStream *st = s->streams[pktl->pkt.stream_index];
1848 // purge packet queue
1850 AVPacketList *next = pktl->next;
1851 av_free_packet(&pktl->pkt);
1861 //av_log(s, AV_LOG_DEBUG, "out st:%d dts:%lld\n", (*out).stream_index, (*out).dts);
1862 s->packet_buffer = pktl->next;
1867 av_init_packet(out);
1872 static int mxf_compare_timestamps(AVFormatContext *s, AVPacket *next, AVPacket *pkt)
1874 MXFStreamContext *sc = s->streams[pkt ->stream_index]->priv_data;
1875 MXFStreamContext *sc2 = s->streams[next->stream_index]->priv_data;
1877 return next->dts > pkt->dts ||
1878 (next->dts == pkt->dts && sc->order < sc2->order);
1881 static int mxf_interleave(AVFormatContext *s, AVPacket *out, AVPacket *pkt, int flush)
1883 return ff_audio_rechunk_interleave(s, out, pkt, flush,
1884 mxf_interleave_get_packet, mxf_compare_timestamps);
1887 AVOutputFormat mxf_muxer = {
1889 NULL_IF_CONFIG_SMALL("Material eXchange Format"),
1894 CODEC_ID_MPEG2VIDEO,
1903 AVOutputFormat mxf_d10_muxer = {
1905 NULL_IF_CONFIG_SMALL("Material eXchange Format, D-10 Mapping"),
1910 CODEC_ID_MPEG2VIDEO,
1912 mxf_write_d10_packet,