1 /*
2 * MXF muxer
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>
5 *
6 * This file is part of FFmpeg.
7 *
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.
12 *
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.
17 *
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
21 */
22
23 /*
24 * References
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
31 */
32
33 #include <inttypes.h>
34 #include <math.h>
35 #include <time.h>
36
37 #include "libavutil/opt.h"
38 #include "libavutil/random_seed.h"
39 #include "libavutil/timecode.h"
40 #include "libavutil/avassert.h"
41 #include "libavcodec/bytestream.h"
42 #include "libavcodec/dnxhddata.h"
43 #include "audiointerleave.h"
44 #include "avformat.h"
45 #include "avio_internal.h"
46 #include "internal.h"
47 #include "mxf.h"
48
49 #include "config.h"
50
51 extern AVOutputFormat ff_mxf_d10_muxer;
52
53 #define EDIT_UNITS_PER_BODY 250
54 #define KAG_SIZE 512
55
56 typedef struct {
57 int local_tag;
58 UID uid;
59 } MXFLocalTagPair;
60
61 typedef struct {
62 uint8_t flags;
63 uint64_t offset;
64 unsigned slice_offset; ///< offset of audio slice
65 uint16_t temporal_ref;
66 } MXFIndexEntry;
67
68 typedef struct {
69 AudioInterleaveContext aic;
70 UID track_essence_element_key;
71 int index; ///< index in mxf_essence_container_uls table
72 const UID *codec_ul;
73 int order; ///< interleaving order if dts are equal
74 int interlaced; ///< whether picture is interlaced
75 int field_dominance; ///< tff=1, bff=2
76 int component_depth;
77 int temporal_reordering;
78 AVRational aspect_ratio; ///< display aspect ratio
79 int closed_gop; ///< gop is closed, used in mpeg-2 frame parsing
80 int video_bit_rate;
81 } MXFStreamContext;
82
83 typedef struct {
84 UID container_ul;
85 UID element_ul;
86 UID codec_ul;
87 void (*write_desc)(AVFormatContext *, AVStream *);
88 } MXFContainerEssenceEntry;
89
90 static const struct {
91 enum AVCodecID id;
92 int index;
93 } mxf_essence_mappings[] = {
94 { AV_CODEC_ID_MPEG2VIDEO, 0 },
95 { AV_CODEC_ID_PCM_S24LE, 1 },
96 { AV_CODEC_ID_PCM_S16LE, 1 },
97 { AV_CODEC_ID_DVVIDEO, 15 },
98 { AV_CODEC_ID_DNXHD, 24 },
99 { AV_CODEC_ID_NONE }
100 };
101
102 static void mxf_write_wav_desc(AVFormatContext *s, AVStream *st);
103 static void mxf_write_aes3_desc(AVFormatContext *s, AVStream *st);
104 static void mxf_write_mpegvideo_desc(AVFormatContext *s, AVStream *st);
105 static void mxf_write_cdci_desc(AVFormatContext *s, AVStream *st);
106 static void mxf_write_generic_sound_desc(AVFormatContext *s, AVStream *st);
107
108 static const MXFContainerEssenceEntry mxf_essence_container_uls[] = {
109 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x02,0x0D,0x01,0x03,0x01,0x02,0x04,0x60,0x01 },
110 { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x15,0x01,0x05,0x00 },
111 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x00,0x00,0x00 },
112 mxf_write_mpegvideo_desc },
113 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x06,0x03,0x00 },
114 { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x16,0x01,0x03,0x00 },
115 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 },
116 mxf_write_aes3_desc },
117 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x06,0x01,0x00 },
118 { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x16,0x01,0x01,0x00 },
119 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 },
120 mxf_write_wav_desc },
121 // D-10 625/50 PAL 50mb/s
122 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x01,0x01 },
123 { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x05,0x01,0x01,0x00 },
124 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x01,0x02,0x01,0x01 },
125 mxf_write_cdci_desc },
126 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x01,0x01 },
127 { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x06,0x01,0x10,0x00 },
128 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 },
129 mxf_write_generic_sound_desc },
130 // D-10 525/60 NTSC 50mb/s
131 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x02,0x01 },
132 { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x05,0x01,0x01,0x00 },
133 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x01,0x02,0x01,0x02 },
134 mxf_write_cdci_desc },
135 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x02,0x01 },
136 { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x06,0x01,0x10,0x00 },
137 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 },
138 mxf_write_generic_sound_desc },
139 // D-10 625/50 PAL 40mb/s
140 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x03,0x01 },
141 { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x05,0x01,0x01,0x00 },
142 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x01,0x02,0x01,0x03 },
143 mxf_write_cdci_desc },
144 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x03,0x01 },
145 { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x06,0x01,0x10,0x00 },
146 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 },
147 mxf_write_generic_sound_desc },
148 // D-10 525/60 NTSC 40mb/s
149 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x04,0x01 },
150 { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x05,0x01,0x01,0x00 },
151 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x01,0x02,0x01,0x04 },
152 mxf_write_cdci_desc },
153 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x04,0x01 },
154 { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x06,0x01,0x10,0x00 },
155 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 },
156 mxf_write_generic_sound_desc },
157 // D-10 625/50 PAL 30mb/s
158 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x05,0x01 },
159 { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x05,0x01,0x01,0x00 },
160 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x01,0x02,0x01,0x05 },
161 mxf_write_cdci_desc },
162 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x05,0x01 },
163 { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x06,0x01,0x10,0x00 },
164 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 },
165 mxf_write_generic_sound_desc },
166 // D-10 525/60 NTSC 30mb/s
167 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x06,0x01 },
168 { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x05,0x01,0x01,0x00 },
169 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x01,0x02,0x01,0x06 },
170 mxf_write_cdci_desc },
171 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x06,0x01 },
172 { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x06,0x01,0x10,0x00 },
173 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 },
174 mxf_write_generic_sound_desc },
175 // DV Unknown
176 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x02,0x7F,0x01 },
177 { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x18,0x01,0x01,0x00 },
178 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x02,0x00,0x00,0x00 },
179 mxf_write_cdci_desc },
180 // DV25 525/60
181 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x02,0x40,0x01 },
182 { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x18,0x01,0x01,0x00 },
183 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x02,0x02,0x01,0x00 },
184 mxf_write_cdci_desc },
185 // DV25 625/50
186 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x02,0x41,0x01 },
187 { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x18,0x01,0x01,0x00 },
188 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x02,0x02,0x02,0x00 },
189 mxf_write_cdci_desc },
190 // DV50 525/60
191 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x02,0x50,0x01 },
192 { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x18,0x01,0x01,0x00 },
193 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x02,0x02,0x03,0x00 },
194 mxf_write_cdci_desc },
195 // DV50 625/50
196 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x02,0x51,0x01 },
197 { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x18,0x01,0x01,0x00 },
198 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x02,0x02,0x04,0x00 },
199 mxf_write_cdci_desc },
200 // DV100 1080/60
201 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x02,0x60,0x01 },
202 { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x18,0x01,0x01,0x00 },
203 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x02,0x02,0x05,0x00 },
204 mxf_write_cdci_desc },
205 // DV100 1080/50
206 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x02,0x61,0x01 },
207 { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x18,0x01,0x01,0x00 },
208 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x02,0x02,0x06,0x00 },
209 mxf_write_cdci_desc },
210 // DV100 720/60
211 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x02,0x62,0x01 },
212 { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x18,0x01,0x01,0x00 },
213 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x02,0x02,0x07,0x00 },
214 mxf_write_cdci_desc },
215 // DV100 720/50
216 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x02,0x63,0x01 },
217 { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x18,0x01,0x01,0x00 },
218 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x02,0x02,0x08,0x00 },
219 mxf_write_cdci_desc },
220 // DNxHD 1080p 10bit high
221 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x11,0x01,0x00 },
222 { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x15,0x01,0x05,0x00 },
223 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0A,0x04,0x01,0x02,0x02,0x71,0x01,0x00,0x00 },
224 mxf_write_cdci_desc },
225 // DNxHD 1080p 8bit medium
226 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x11,0x01,0x00 },
227 { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x15,0x01,0x05,0x00 },
228 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0A,0x04,0x01,0x02,0x02,0x71,0x03,0x00,0x00 },
229 mxf_write_cdci_desc },
230 // DNxHD 1080p 8bit high
231 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x11,0x01,0x00 },
232 { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x15,0x01,0x05,0x00 },
233 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0A,0x04,0x01,0x02,0x02,0x71,0x04,0x00,0x00 },
234 mxf_write_cdci_desc },
235 // DNxHD 1080i 10bit high
236 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x11,0x01,0x00 },
237 { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x15,0x01,0x05,0x00 },
238 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0A,0x04,0x01,0x02,0x02,0x71,0x07,0x00,0x00 },
239 mxf_write_cdci_desc },
240 // DNxHD 1080i 8bit medium
241 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x11,0x01,0x00 },
242 { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x15,0x01,0x05,0x00 },
243 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0A,0x04,0x01,0x02,0x02,0x71,0x08,0x00,0x00 },
244 mxf_write_cdci_desc },
245 // DNxHD 1080i 8bit high
246 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x11,0x01,0x00 },
247 { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x15,0x01,0x05,0x00 },
248 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0A,0x04,0x01,0x02,0x02,0x71,0x09,0x00,0x00 },
249 mxf_write_cdci_desc },
250 // DNxHD 720p 10bit
251 { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x01,0x0d,0x01,0x03,0x01,0x02,0x11,0x01,0x00 },
252 { 0x06,0x0e,0x2b,0x34,0x01,0x02,0x01,0x01,0x0d,0x01,0x03,0x01,0x15,0x01,0x05,0x00 },
253 { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x0A,0x04,0x01,0x02,0x02,0x71,0x10,0x00,0x00 },
254 mxf_write_cdci_desc },
255 // DNxHD 720p 8bit high
256 { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x01,0x0d,0x01,0x03,0x01,0x02,0x11,0x01,0x00 },
257 { 0x06,0x0e,0x2b,0x34,0x01,0x02,0x01,0x01,0x0d,0x01,0x03,0x01,0x15,0x01,0x05,0x00 },
258 { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x0A,0x04,0x01,0x02,0x02,0x71,0x11,0x00,0x00 },
259 mxf_write_cdci_desc },
260 // DNxHD 720p 8bit medium
261 { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x01,0x0d,0x01,0x03,0x01,0x02,0x11,0x01,0x00 },
262 { 0x06,0x0e,0x2b,0x34,0x01,0x02,0x01,0x01,0x0d,0x01,0x03,0x01,0x15,0x01,0x05,0x00 },
263 { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x0A,0x04,0x01,0x02,0x02,0x71,0x12,0x00,0x00 },
264 mxf_write_cdci_desc },
265 // DNxHD 720p 8bit low
266 { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x01,0x0d,0x01,0x03,0x01,0x02,0x11,0x01,0x00 },
267 { 0x06,0x0e,0x2b,0x34,0x01,0x02,0x01,0x01,0x0d,0x01,0x03,0x01,0x15,0x01,0x05,0x00 },
268 { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x0A,0x04,0x01,0x02,0x02,0x71,0x13,0x00,0x00 },
269 mxf_write_cdci_desc },
270 { { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 },
271 { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 },
272 { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 },
273 NULL },
274 };
275
276 typedef struct MXFContext {
277 AVClass *av_class;
278 int64_t footer_partition_offset;
279 int essence_container_count;
280 AVRational time_base;
281 int header_written;
282 MXFIndexEntry *index_entries;
283 unsigned edit_units_count;
284 uint64_t timestamp; ///< timestamp, as year(16),month(8),day(8),hour(8),minutes(8),msec/4(8)
285 uint8_t slice_count; ///< index slice count minus 1 (1 if no audio, 0 otherwise)
286 int last_indexed_edit_unit;
287 uint64_t *body_partition_offset;
288 unsigned body_partitions_count;
289 int last_key_index; ///< index of last key frame
290 uint64_t duration;
291 AVTimecode tc; ///< timecode context
292 AVStream *timecode_track;
293 int timecode_base; ///< rounded time code base (25 or 30)
294 int edit_unit_byte_count; ///< fixed edit unit byte count
295 uint64_t body_offset;
296 uint32_t instance_number;
297 uint8_t umid[16]; ///< unique material identifier
298 int channel_count;
299 } MXFContext;
300
301 static const uint8_t uuid_base[] = { 0xAD,0xAB,0x44,0x24,0x2f,0x25,0x4d,0xc7,0x92,0xff,0x29,0xbd };
302 static const uint8_t umid_ul[] = { 0x06,0x0A,0x2B,0x34,0x01,0x01,0x01,0x05,0x01,0x01,0x0D,0x00,0x13 };
303
304 /**
305 * complete key for operation pattern, partitions, and primer pack
306 */
307 static const uint8_t op1a_ul[] = { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x02,0x01,0x01,0x01,0x09,0x00 };
308 static const uint8_t footer_partition_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0D,0x01,0x02,0x01,0x01,0x04,0x04,0x00 }; // ClosedComplete
309 static const uint8_t primer_pack_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0D,0x01,0x02,0x01,0x01,0x05,0x01,0x00 };
310 static const uint8_t index_table_segment_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x10,0x01,0x00 };
311 static const uint8_t random_index_pack_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0D,0x01,0x02,0x01,0x01,0x11,0x01,0x00 };
312 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
313 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
314 static const uint8_t klv_fill_key[] = { 0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x03,0x01,0x02,0x10,0x01,0x00,0x00,0x00 };
315 static const uint8_t body_partition_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0D,0x01,0x02,0x01,0x01,0x03,0x04,0x00 }; // ClosedComplete
316
317 /**
318 * partial key for header metadata
319 */
320 static const uint8_t header_metadata_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0D,0x01,0x01,0x01,0x01 };
321 static const uint8_t multiple_desc_ul[] = { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x0D,0x01,0x03,0x01,0x02,0x7F,0x01,0x00 };
322
323 /**
324 * SMPTE RP210 http://www.smpte-ra.org/mdd/index.html
325 */
326 static const MXFLocalTagPair mxf_local_tag_batch[] = {
327 // preface set
328 { 0x3C0A, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x01,0x01,0x15,0x02,0x00,0x00,0x00,0x00}}, /* Instance UID */
329 { 0x3B02, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x10,0x02,0x04,0x00,0x00}}, /* Last Modified Date */
330 { 0x3B05, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x03,0x01,0x02,0x01,0x05,0x00,0x00,0x00}}, /* Version */
331 { 0x3B06, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x06,0x04,0x00,0x00}}, /* Identifications reference */
332 { 0x3B03, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x02,0x01,0x00,0x00}}, /* Content Storage reference */
333 { 0x3B09, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x01,0x02,0x02,0x03,0x00,0x00,0x00,0x00}}, /* Operational Pattern UL */
334 { 0x3B0A, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x01,0x02,0x02,0x10,0x02,0x01,0x00,0x00}}, /* Essence Containers UL batch */
335 { 0x3B0B, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x01,0x02,0x02,0x10,0x02,0x02,0x00,0x00}}, /* DM Schemes UL batch */
336 // Identification
337 { 0x3C09, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x20,0x07,0x01,0x01,0x00,0x00,0x00}}, /* This Generation UID */
338 { 0x3C01, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x20,0x07,0x01,0x02,0x01,0x00,0x00}}, /* Company Name */
339 { 0x3C02, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x20,0x07,0x01,0x03,0x01,0x00,0x00}}, /* Product Name */
340 { 0x3C04, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x20,0x07,0x01,0x05,0x01,0x00,0x00}}, /* Version String */
341 { 0x3C05, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x20,0x07,0x01,0x07,0x00,0x00,0x00}}, /* Product ID */
342 { 0x3C06, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x10,0x02,0x03,0x00,0x00}}, /* Modification Date */
343 // Content Storage
344 { 0x1901, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x05,0x01,0x00,0x00}}, /* Package strong reference batch */
345 { 0x1902, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x05,0x02,0x00,0x00}}, /* Package strong reference batch */
346 // Essence Container Data
347 { 0x2701, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x06,0x01,0x00,0x00,0x00}}, /* Linked Package UID */
348 { 0x3F07, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x01,0x03,0x04,0x04,0x00,0x00,0x00,0x00}}, /* BodySID */
349 // Package
350 { 0x4401, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x01,0x01,0x15,0x10,0x00,0x00,0x00,0x00}}, /* Package UID */
351 { 0x4405, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x10,0x01,0x03,0x00,0x00}}, /* Package Creation Date */
352 { 0x4404, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x10,0x02,0x05,0x00,0x00}}, /* Package Modified Date */
353 { 0x4403, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x06,0x05,0x00,0x00}}, /* Tracks Strong reference array */
354 { 0x4701, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x02,0x03,0x00,0x00}}, /* Descriptor */
355 // Track
356 { 0x4801, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x01,0x07,0x01,0x01,0x00,0x00,0x00,0x00}}, /* Track ID */
357 { 0x4804, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x01,0x04,0x01,0x03,0x00,0x00,0x00,0x00}}, /* Track Number */
358 { 0x4B01, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x30,0x04,0x05,0x00,0x00,0x00,0x00}}, /* Edit Rate */
359 { 0x4B02, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x03,0x01,0x03,0x00,0x00}}, /* Origin */
360 { 0x4803, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x02,0x04,0x00,0x00}}, /* Sequence reference */
361 // Sequence
362 { 0x0201, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x07,0x01,0x00,0x00,0x00,0x00,0x00}}, /* Data Definition UL */
363 { 0x0202, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x02,0x01,0x01,0x03,0x00,0x00}}, /* Duration */
364 { 0x1001, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x06,0x09,0x00,0x00}}, /* Structural Components reference array */
365 // Source Clip
366 { 0x1201, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x03,0x01,0x04,0x00,0x00}}, /* Start position */
367 { 0x1101, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x03,0x01,0x00,0x00,0x00}}, /* SourcePackageID */
368 { 0x1102, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x03,0x02,0x00,0x00,0x00}}, /* SourceTrackID */
369 // Timecode Component
370 { 0x1501, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x03,0x01,0x05,0x00,0x00}}, /* Start Time Code */
371 { 0x1502, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x04,0x01,0x01,0x02,0x06,0x00,0x00}}, /* Rounded Time Code Base */
372 { 0x1503, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x04,0x01,0x01,0x05,0x00,0x00,0x00}}, /* Drop Frame */
373 // File Descriptor
374 { 0x3F01, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x06,0x01,0x01,0x04,0x06,0x0B,0x00,0x00}}, /* Sub Descriptors reference array */
375 { 0x3006, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x06,0x01,0x01,0x03,0x05,0x00,0x00,0x00}}, /* Linked Track ID */
376 { 0x3001, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x06,0x01,0x01,0x00,0x00,0x00,0x00}}, /* SampleRate */
377 { 0x3004, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x01,0x02,0x00,0x00}}, /* Essence Container */
378 // Generic Picture Essence Descriptor
379 { 0x320C, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x03,0x01,0x04,0x00,0x00,0x00}}, /* Frame Layout */
380 { 0x320D, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x01,0x03,0x02,0x05,0x00,0x00,0x00}}, /* Video Line Map */
381 { 0x3203, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x02,0x02,0x00,0x00,0x00}}, /* Stored Width */
382 { 0x3202, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x02,0x01,0x00,0x00,0x00}}, /* Stored Height */
383 { 0x3209, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x01,0x0C,0x00,0x00,0x00}}, /* Display Width */
384 { 0x3208, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x01,0x0B,0x00,0x00,0x00}}, /* Display Height */
385 { 0x320E, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x01,0x01,0x01,0x00,0x00,0x00}}, /* Aspect Ratio */
386 { 0x3201, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x01,0x06,0x01,0x00,0x00,0x00,0x00}}, /* Picture Essence Coding */
387 { 0x3212, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x01,0x03,0x01,0x06,0x00,0x00,0x00}}, /* Field Dominance (Opt) */
388 // CDCI Picture Essence Descriptor
389 { 0x3301, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x01,0x05,0x03,0x0A,0x00,0x00,0x00}}, /* Component Depth */
390 { 0x3302, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x01,0x05,0x00,0x00,0x00}}, /* Horizontal Subsampling */
391 // Generic Sound Essence Descriptor
392 { 0x3D02, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x04,0x02,0x03,0x01,0x04,0x00,0x00,0x00}}, /* Locked/Unlocked */
393 { 0x3D03, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x02,0x03,0x01,0x01,0x01,0x00,0x00}}, /* Audio sampling rate */
394 { 0x3D07, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x02,0x01,0x01,0x04,0x00,0x00,0x00}}, /* ChannelCount */
395 { 0x3D01, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x04,0x02,0x03,0x03,0x04,0x00,0x00,0x00}}, /* Quantization bits */
396 { 0x3D06, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x02,0x04,0x02,0x00,0x00,0x00,0x00}}, /* Sound Essence Compression */
397 // Index Table Segment
398 { 0x3F0B, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x05,0x30,0x04,0x06,0x00,0x00,0x00,0x00}}, /* Index Edit Rate */
399 { 0x3F0C, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x07,0x02,0x01,0x03,0x01,0x0A,0x00,0x00}}, /* Index Start Position */
400 { 0x3F0D, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x07,0x02,0x02,0x01,0x01,0x02,0x00,0x00}}, /* Index Duration */
401 { 0x3F05, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x04,0x06,0x02,0x01,0x00,0x00,0x00,0x00}}, /* Edit Unit Byte Count */
402 { 0x3F06, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x01,0x03,0x04,0x05,0x00,0x00,0x00,0x00}}, /* IndexSID */
403 { 0x3F08, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x04,0x04,0x04,0x01,0x01,0x00,0x00,0x00}}, /* Slice Count */
404 { 0x3F09, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x04,0x04,0x01,0x06,0x00,0x00,0x00}}, /* Delta Entry Array */
405 { 0x3F0A, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x04,0x04,0x02,0x05,0x00,0x00,0x00}}, /* Index Entry Array */
406 // MPEG video Descriptor
407 { 0x8000, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x01,0x06,0x02,0x01,0x0B,0x00,0x00}}, /* BitRate */
408 { 0x8007, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x01,0x06,0x02,0x01,0x0A,0x00,0x00}}, /* ProfileAndLevel */
409 // Wave Audio Essence Descriptor
410 { 0x3D09, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x02,0x03,0x03,0x05,0x00,0x00,0x00}}, /* Average Bytes Per Second */
411 { 0x3D0A, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x02,0x03,0x02,0x01,0x00,0x00,0x00}}, /* Block Align */
412 };
413
mxf_write_uuid(AVIOContext * pb,enum MXFMetadataSetType type,int value)414 static void mxf_write_uuid(AVIOContext *pb, enum MXFMetadataSetType type, int value)
415 {
416 avio_write(pb, uuid_base, 12);
417 avio_wb16(pb, type);
418 avio_wb16(pb, value);
419 }
420
mxf_write_umid(AVFormatContext * s,int type)421 static void mxf_write_umid(AVFormatContext *s, int type)
422 {
423 MXFContext *mxf = s->priv_data;
424 avio_write(s->pb, umid_ul, 13);
425 avio_wb24(s->pb, mxf->instance_number);
426 avio_write(s->pb, mxf->umid, 15);
427 avio_w8(s->pb, type);
428 }
429
mxf_write_refs_count(AVIOContext * pb,int ref_count)430 static void mxf_write_refs_count(AVIOContext *pb, int ref_count)
431 {
432 avio_wb32(pb, ref_count);
433 avio_wb32(pb, 16);
434 }
435
klv_ber_length(uint64_t len)436 static int klv_ber_length(uint64_t len)
437 {
438 if (len < 128)
439 return 1;
440 else
441 return (av_log2(len) >> 3) + 2;
442 }
443
klv_encode_ber_length(AVIOContext * pb,uint64_t len)444 static int klv_encode_ber_length(AVIOContext *pb, uint64_t len)
445 {
446 // Determine the best BER size
447 int size;
448 if (len < 128) {
449 //short form
450 avio_w8(pb, len);
451 return 1;
452 }
453
454 size = (av_log2(len) >> 3) + 1;
455
456 // long form
457 avio_w8(pb, 0x80 + size);
458 while(size) {
459 size--;
460 avio_w8(pb, len >> 8 * size & 0xff);
461 }
462 return 0;
463 }
464
klv_encode_ber4_length(AVIOContext * pb,int len)465 static void klv_encode_ber4_length(AVIOContext *pb, int len)
466 {
467 avio_w8(pb, 0x80 + 3);
468 avio_wb24(pb, len);
469 }
470
471 /*
472 * Get essence container ul index
473 */
mxf_get_essence_container_ul_index(enum AVCodecID id)474 static int mxf_get_essence_container_ul_index(enum AVCodecID id)
475 {
476 int i;
477 for (i = 0; mxf_essence_mappings[i].id; i++)
478 if (mxf_essence_mappings[i].id == id)
479 return mxf_essence_mappings[i].index;
480 return -1;
481 }
482
mxf_write_primer_pack(AVFormatContext * s)483 static void mxf_write_primer_pack(AVFormatContext *s)
484 {
485 AVIOContext *pb = s->pb;
486 int local_tag_number, i = 0;
487
488 local_tag_number = FF_ARRAY_ELEMS(mxf_local_tag_batch);
489
490 avio_write(pb, primer_pack_key, 16);
491 klv_encode_ber_length(pb, local_tag_number * 18 + 8);
492
493 avio_wb32(pb, local_tag_number); // local_tag num
494 avio_wb32(pb, 18); // item size, always 18 according to the specs
495
496 for (i = 0; i < local_tag_number; i++) {
497 avio_wb16(pb, mxf_local_tag_batch[i].local_tag);
498 avio_write(pb, mxf_local_tag_batch[i].uid, 16);
499 }
500 }
501
mxf_write_local_tag(AVIOContext * pb,int size,int tag)502 static void mxf_write_local_tag(AVIOContext *pb, int size, int tag)
503 {
504 avio_wb16(pb, tag);
505 avio_wb16(pb, size);
506 }
507
mxf_write_metadata_key(AVIOContext * pb,unsigned int value)508 static void mxf_write_metadata_key(AVIOContext *pb, unsigned int value)
509 {
510 avio_write(pb, header_metadata_key, 13);
511 avio_wb24(pb, value);
512 }
513
mxf_free(AVFormatContext * s)514 static void mxf_free(AVFormatContext *s)
515 {
516 int i;
517
518 for (i = 0; i < s->nb_streams; i++) {
519 AVStream *st = s->streams[i];
520 av_freep(&st->priv_data);
521 }
522 }
523
mxf_get_data_definition_ul(int type)524 static const MXFCodecUL *mxf_get_data_definition_ul(int type)
525 {
526 const MXFCodecUL *uls = ff_mxf_data_definition_uls;
527 while (uls->uid[0]) {
528 if (type == uls->id)
529 break;
530 uls++;
531 }
532 return uls;
533 }
534
535 //one EC -> one descriptor. N ECs -> MultipleDescriptor + N descriptors
536 #define DESCRIPTOR_COUNT(essence_container_count) \
537 (essence_container_count > 1 ? essence_container_count + 1 : essence_container_count)
538
mxf_write_essence_container_refs(AVFormatContext * s)539 static void mxf_write_essence_container_refs(AVFormatContext *s)
540 {
541 MXFContext *c = s->priv_data;
542 AVIOContext *pb = s->pb;
543 int i;
544
545 mxf_write_refs_count(pb, DESCRIPTOR_COUNT(c->essence_container_count));
546 av_log(s,AV_LOG_DEBUG, "essence container count:%d\n", c->essence_container_count);
547 for (i = 0; i < c->essence_container_count; i++) {
548 MXFStreamContext *sc = s->streams[i]->priv_data;
549 avio_write(pb, mxf_essence_container_uls[sc->index].container_ul, 16);
550 }
551
552 if (c->essence_container_count > 1)
553 avio_write(pb, multiple_desc_ul, 16);
554 }
555
mxf_write_preface(AVFormatContext * s)556 static void mxf_write_preface(AVFormatContext *s)
557 {
558 MXFContext *mxf = s->priv_data;
559 AVIOContext *pb = s->pb;
560
561 mxf_write_metadata_key(pb, 0x012f00);
562 PRINT_KEY(s, "preface key", pb->buf_ptr - 16);
563 klv_encode_ber_length(pb, 130 + LLN(16) * DESCRIPTOR_COUNT(mxf->essence_container_count));
564
565 // write preface set uid
566 mxf_write_local_tag(pb, 16, 0x3C0A);
567 mxf_write_uuid(pb, Preface, 0);
568 PRINT_KEY(s, "preface uid", pb->buf_ptr - 16);
569
570 // last modified date
571 mxf_write_local_tag(pb, 8, 0x3B02);
572 avio_wb64(pb, mxf->timestamp);
573
574 // write version
575 mxf_write_local_tag(pb, 2, 0x3B05);
576 avio_wb16(pb, 258); // v1.2
577
578 // write identification_refs
579 mxf_write_local_tag(pb, 16 + 8, 0x3B06);
580 mxf_write_refs_count(pb, 1);
581 mxf_write_uuid(pb, Identification, 0);
582
583 // write content_storage_refs
584 mxf_write_local_tag(pb, 16, 0x3B03);
585 mxf_write_uuid(pb, ContentStorage, 0);
586
587 // operational pattern
588 mxf_write_local_tag(pb, 16, 0x3B09);
589 avio_write(pb, op1a_ul, 16);
590
591 // write essence_container_refs
592 mxf_write_local_tag(pb, 8 + LLN(16) * DESCRIPTOR_COUNT(mxf->essence_container_count), 0x3B0A);
593 mxf_write_essence_container_refs(s);
594
595 // write dm_scheme_refs
596 mxf_write_local_tag(pb, 8, 0x3B0B);
597 avio_wb64(pb, 0);
598 }
599
600 /*
601 * Write a local tag containing an ascii string as utf-16
602 */
mxf_write_local_tag_utf16(AVIOContext * pb,int tag,const char * value)603 static void mxf_write_local_tag_utf16(AVIOContext *pb, int tag, const char *value)
604 {
605 int i, size = strlen(value);
606 mxf_write_local_tag(pb, size*2, tag);
607 for (i = 0; i < size; i++)
608 avio_wb16(pb, value[i]);
609 }
610
mxf_write_identification(AVFormatContext * s)611 static void mxf_write_identification(AVFormatContext *s)
612 {
613 MXFContext *mxf = s->priv_data;
614 AVIOContext *pb = s->pb;
615 const char *company = "FFmpeg";
616 const char *product = "OP1a Muxer";
617 const char *version;
618 int length;
619
620 mxf_write_metadata_key(pb, 0x013000);
621 PRINT_KEY(s, "identification key", pb->buf_ptr - 16);
622
623 version = s->flags & AVFMT_FLAG_BITEXACT ?
624 "0.0.0" : AV_STRINGIFY(LIBAVFORMAT_VERSION);
625 length = 84 + (strlen(company)+strlen(product)+strlen(version))*2; // utf-16
626 klv_encode_ber_length(pb, length);
627
628 // write uid
629 mxf_write_local_tag(pb, 16, 0x3C0A);
630 mxf_write_uuid(pb, Identification, 0);
631 PRINT_KEY(s, "identification uid", pb->buf_ptr - 16);
632
633 // write generation uid
634 mxf_write_local_tag(pb, 16, 0x3C09);
635 mxf_write_uuid(pb, Identification, 1);
636
637 mxf_write_local_tag_utf16(pb, 0x3C01, company); // Company Name
638 mxf_write_local_tag_utf16(pb, 0x3C02, product); // Product Name
639 mxf_write_local_tag_utf16(pb, 0x3C04, version); // Version String
640
641 // write product uid
642 mxf_write_local_tag(pb, 16, 0x3C05);
643 mxf_write_uuid(pb, Identification, 2);
644
645 // modification date
646 mxf_write_local_tag(pb, 8, 0x3C06);
647 avio_wb64(pb, mxf->timestamp);
648 }
649
mxf_write_content_storage(AVFormatContext * s)650 static void mxf_write_content_storage(AVFormatContext *s)
651 {
652 AVIOContext *pb = s->pb;
653
654 mxf_write_metadata_key(pb, 0x011800);
655 PRINT_KEY(s, "content storage key", pb->buf_ptr - 16);
656 klv_encode_ber_length(pb, 92);
657
658 // write uid
659 mxf_write_local_tag(pb, 16, 0x3C0A);
660 mxf_write_uuid(pb, ContentStorage, 0);
661 PRINT_KEY(s, "content storage uid", pb->buf_ptr - 16);
662
663 // write package reference
664 mxf_write_local_tag(pb, 16 * 2 + 8, 0x1901);
665 mxf_write_refs_count(pb, 2);
666 mxf_write_uuid(pb, MaterialPackage, 0);
667 mxf_write_uuid(pb, SourcePackage, 0);
668
669 // write essence container data
670 mxf_write_local_tag(pb, 8 + 16, 0x1902);
671 mxf_write_refs_count(pb, 1);
672 mxf_write_uuid(pb, EssenceContainerData, 0);
673 }
674
mxf_write_track(AVFormatContext * s,AVStream * st,enum MXFMetadataSetType type)675 static void mxf_write_track(AVFormatContext *s, AVStream *st, enum MXFMetadataSetType type)
676 {
677 MXFContext *mxf = s->priv_data;
678 AVIOContext *pb = s->pb;
679 MXFStreamContext *sc = st->priv_data;
680
681 mxf_write_metadata_key(pb, 0x013b00);
682 PRINT_KEY(s, "track key", pb->buf_ptr - 16);
683 klv_encode_ber_length(pb, 80);
684
685 // write track uid
686 mxf_write_local_tag(pb, 16, 0x3C0A);
687 mxf_write_uuid(pb, type == MaterialPackage ? Track : Track + TypeBottom, st->index);
688 PRINT_KEY(s, "track uid", pb->buf_ptr - 16);
689
690 // write track id
691 mxf_write_local_tag(pb, 4, 0x4801);
692 avio_wb32(pb, st->index+2);
693
694 // write track number
695 mxf_write_local_tag(pb, 4, 0x4804);
696 if (type == MaterialPackage)
697 avio_wb32(pb, 0); // track number of material package is 0
698 else
699 avio_write(pb, sc->track_essence_element_key + 12, 4);
700
701 mxf_write_local_tag(pb, 8, 0x4B01);
702 avio_wb32(pb, mxf->time_base.den);
703 avio_wb32(pb, mxf->time_base.num);
704
705 // write origin
706 mxf_write_local_tag(pb, 8, 0x4B02);
707 avio_wb64(pb, 0);
708
709 // write sequence refs
710 mxf_write_local_tag(pb, 16, 0x4803);
711 mxf_write_uuid(pb, type == MaterialPackage ? Sequence: Sequence + TypeBottom, st->index);
712 }
713
714 static const uint8_t smpte_12m_timecode_track_data_ul[] = { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x01,0x03,0x02,0x01,0x01,0x00,0x00,0x00 };
715
mxf_write_common_fields(AVFormatContext * s,AVStream * st)716 static void mxf_write_common_fields(AVFormatContext *s, AVStream *st)
717 {
718 MXFContext *mxf = s->priv_data;
719 AVIOContext *pb = s->pb;
720
721 // find data define uls
722 mxf_write_local_tag(pb, 16, 0x0201);
723 if (st == mxf->timecode_track)
724 avio_write(pb, smpte_12m_timecode_track_data_ul, 16);
725 else {
726 const MXFCodecUL *data_def_ul = mxf_get_data_definition_ul(st->codec->codec_type);
727 avio_write(pb, data_def_ul->uid, 16);
728 }
729
730 // write duration
731 mxf_write_local_tag(pb, 8, 0x0202);
732 avio_wb64(pb, mxf->duration);
733 }
734
mxf_write_sequence(AVFormatContext * s,AVStream * st,enum MXFMetadataSetType type)735 static void mxf_write_sequence(AVFormatContext *s, AVStream *st, enum MXFMetadataSetType type)
736 {
737 MXFContext *mxf = s->priv_data;
738 AVIOContext *pb = s->pb;
739 enum MXFMetadataSetType component;
740
741 mxf_write_metadata_key(pb, 0x010f00);
742 PRINT_KEY(s, "sequence key", pb->buf_ptr - 16);
743 klv_encode_ber_length(pb, 80);
744
745 mxf_write_local_tag(pb, 16, 0x3C0A);
746 mxf_write_uuid(pb, type == MaterialPackage ? Sequence: Sequence + TypeBottom, st->index);
747
748 PRINT_KEY(s, "sequence uid", pb->buf_ptr - 16);
749 mxf_write_common_fields(s, st);
750
751 // write structural component
752 mxf_write_local_tag(pb, 16 + 8, 0x1001);
753 mxf_write_refs_count(pb, 1);
754 if (st == mxf->timecode_track)
755 component = TimecodeComponent;
756 else
757 component = SourceClip;
758 if (type == SourcePackage)
759 component += TypeBottom;
760 mxf_write_uuid(pb, component, st->index);
761 }
762
mxf_write_timecode_component(AVFormatContext * s,AVStream * st,enum MXFMetadataSetType type)763 static void mxf_write_timecode_component(AVFormatContext *s, AVStream *st, enum MXFMetadataSetType type)
764 {
765 MXFContext *mxf = s->priv_data;
766 AVIOContext *pb = s->pb;
767
768 mxf_write_metadata_key(pb, 0x011400);
769 klv_encode_ber_length(pb, 75);
770
771 // UID
772 mxf_write_local_tag(pb, 16, 0x3C0A);
773 mxf_write_uuid(pb, type == MaterialPackage ? TimecodeComponent :
774 TimecodeComponent + TypeBottom, st->index);
775
776 mxf_write_common_fields(s, st);
777
778 // Start Time Code
779 mxf_write_local_tag(pb, 8, 0x1501);
780 avio_wb64(pb, mxf->tc.start);
781
782 // Rounded Time Code Base
783 mxf_write_local_tag(pb, 2, 0x1502);
784 avio_wb16(pb, mxf->timecode_base);
785
786 // Drop Frame
787 mxf_write_local_tag(pb, 1, 0x1503);
788 avio_w8(pb, !!(mxf->tc.flags & AV_TIMECODE_FLAG_DROPFRAME));
789 }
790
mxf_write_structural_component(AVFormatContext * s,AVStream * st,enum MXFMetadataSetType type)791 static void mxf_write_structural_component(AVFormatContext *s, AVStream *st, enum MXFMetadataSetType type)
792 {
793 AVIOContext *pb = s->pb;
794 int i;
795
796 mxf_write_metadata_key(pb, 0x011100);
797 PRINT_KEY(s, "sturctural component key", pb->buf_ptr - 16);
798 klv_encode_ber_length(pb, 108);
799
800 // write uid
801 mxf_write_local_tag(pb, 16, 0x3C0A);
802 mxf_write_uuid(pb, type == MaterialPackage ? SourceClip: SourceClip + TypeBottom, st->index);
803
804 PRINT_KEY(s, "structural component uid", pb->buf_ptr - 16);
805 mxf_write_common_fields(s, st);
806
807 // write start_position
808 mxf_write_local_tag(pb, 8, 0x1201);
809 avio_wb64(pb, 0);
810
811 // write source package uid, end of the reference
812 mxf_write_local_tag(pb, 32, 0x1101);
813 if (type == SourcePackage) {
814 for (i = 0; i < 4; i++)
815 avio_wb64(pb, 0);
816 } else
817 mxf_write_umid(s, 1);
818
819 // write source track id
820 mxf_write_local_tag(pb, 4, 0x1102);
821 if (type == SourcePackage)
822 avio_wb32(pb, 0);
823 else
824 avio_wb32(pb, st->index+2);
825 }
826
mxf_write_multi_descriptor(AVFormatContext * s)827 static void mxf_write_multi_descriptor(AVFormatContext *s)
828 {
829 MXFContext *mxf = s->priv_data;
830 AVIOContext *pb = s->pb;
831 const uint8_t *ul;
832 int i;
833
834 mxf_write_metadata_key(pb, 0x014400);
835 PRINT_KEY(s, "multiple descriptor key", pb->buf_ptr - 16);
836 klv_encode_ber_length(pb, 64 + LLN(16) * s->nb_streams);
837
838 mxf_write_local_tag(pb, 16, 0x3C0A);
839 mxf_write_uuid(pb, MultipleDescriptor, 0);
840 PRINT_KEY(s, "multi_desc uid", pb->buf_ptr - 16);
841
842 // write sample rate
843 mxf_write_local_tag(pb, 8, 0x3001);
844 avio_wb32(pb, mxf->time_base.den);
845 avio_wb32(pb, mxf->time_base.num);
846
847 // write essence container ul
848 mxf_write_local_tag(pb, 16, 0x3004);
849 if (mxf->essence_container_count > 1)
850 ul = multiple_desc_ul;
851 else {
852 MXFStreamContext *sc = s->streams[0]->priv_data;
853 ul = mxf_essence_container_uls[sc->index].container_ul;
854 }
855 avio_write(pb, ul, 16);
856
857 // write sub descriptor refs
858 mxf_write_local_tag(pb, s->nb_streams * 16 + 8, 0x3F01);
859 mxf_write_refs_count(pb, s->nb_streams);
860 for (i = 0; i < s->nb_streams; i++)
861 mxf_write_uuid(pb, SubDescriptor, i);
862 }
863
mxf_write_generic_desc(AVFormatContext * s,AVStream * st,const UID key,unsigned size)864 static void mxf_write_generic_desc(AVFormatContext *s, AVStream *st, const UID key, unsigned size)
865 {
866 MXFContext *mxf = s->priv_data;
867 MXFStreamContext *sc = st->priv_data;
868 AVIOContext *pb = s->pb;
869
870 avio_write(pb, key, 16);
871 klv_encode_ber4_length(pb, size+20+8+12+20);
872
873 mxf_write_local_tag(pb, 16, 0x3C0A);
874 mxf_write_uuid(pb, SubDescriptor, st->index);
875
876 mxf_write_local_tag(pb, 4, 0x3006);
877 avio_wb32(pb, st->index+2);
878
879 mxf_write_local_tag(pb, 8, 0x3001);
880 avio_wb32(pb, mxf->time_base.den);
881 avio_wb32(pb, mxf->time_base.num);
882
883 mxf_write_local_tag(pb, 16, 0x3004);
884 avio_write(pb, mxf_essence_container_uls[sc->index].container_ul, 16);
885 }
886
887 static const UID mxf_mpegvideo_descriptor_key = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x51,0x00 };
888 static const UID mxf_wav_descriptor_key = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x48,0x00 };
889 static const UID mxf_aes3_descriptor_key = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x47,0x00 };
890 static const UID mxf_cdci_descriptor_key = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0D,0x01,0x01,0x01,0x01,0x01,0x28,0x00 };
891 static const UID mxf_generic_sound_descriptor_key = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0D,0x01,0x01,0x01,0x01,0x01,0x42,0x00 };
892
mxf_write_cdci_common(AVFormatContext * s,AVStream * st,const UID key,unsigned size)893 static void mxf_write_cdci_common(AVFormatContext *s, AVStream *st, const UID key, unsigned size)
894 {
895 MXFStreamContext *sc = st->priv_data;
896 AVIOContext *pb = s->pb;
897 int stored_height = (st->codec->height+15)/16*16;
898 int display_height;
899 int f1, f2;
900 unsigned desc_size = size+8+8+8+8+8+8+5+16+sc->interlaced*4+12+20;
901 if (sc->interlaced && sc->field_dominance)
902 desc_size += 5;
903
904 mxf_write_generic_desc(s, st, key, desc_size);
905
906 mxf_write_local_tag(pb, 4, 0x3203);
907 avio_wb32(pb, st->codec->width);
908
909 mxf_write_local_tag(pb, 4, 0x3202);
910 avio_wb32(pb, stored_height>>sc->interlaced);
911
912 mxf_write_local_tag(pb, 4, 0x3209);
913 avio_wb32(pb, st->codec->width);
914
915 if (st->codec->height == 608) // PAL + VBI
916 display_height = 576;
917 else if (st->codec->height == 512) // NTSC + VBI
918 display_height = 486;
919 else
920 display_height = st->codec->height;
921
922 mxf_write_local_tag(pb, 4, 0x3208);
923 avio_wb32(pb, display_height>>sc->interlaced);
924
925 // component depth
926 mxf_write_local_tag(pb, 4, 0x3301);
927 avio_wb32(pb, sc->component_depth);
928
929 // horizontal subsampling
930 mxf_write_local_tag(pb, 4, 0x3302);
931 avio_wb32(pb, 2);
932
933 // frame layout
934 mxf_write_local_tag(pb, 1, 0x320C);
935 avio_w8(pb, sc->interlaced);
936
937 // video line map
938 switch (st->codec->height) {
939 case 576: f1 = 23; f2 = st->codec->codec_id == AV_CODEC_ID_DVVIDEO ? 335 : 336; break;
940 case 608: f1 = 7; f2 = 320; break;
941 case 480: f1 = 20; f2 = st->codec->codec_id == AV_CODEC_ID_DVVIDEO ? 285 : 283; break;
942 case 512: f1 = 7; f2 = 270; break;
943 case 720: f1 = 26; f2 = 0; break; // progressive
944 case 1080: f1 = 21; f2 = 584; break;
945 default: f1 = 0; f2 = 0; break;
946 }
947
948 if (!sc->interlaced) {
949 f2 = 0;
950 f1 *= 2;
951 }
952
953 mxf_write_local_tag(pb, 12+sc->interlaced*4, 0x320D);
954 avio_wb32(pb, sc->interlaced ? 2 : 1);
955 avio_wb32(pb, 4);
956 avio_wb32(pb, f1);
957 if (sc->interlaced)
958 avio_wb32(pb, f2);
959
960 mxf_write_local_tag(pb, 8, 0x320E);
961 avio_wb32(pb, sc->aspect_ratio.num);
962 avio_wb32(pb, sc->aspect_ratio.den);
963
964 mxf_write_local_tag(pb, 16, 0x3201);
965 avio_write(pb, *sc->codec_ul, 16);
966
967 if (sc->interlaced && sc->field_dominance) {
968 mxf_write_local_tag(pb, 1, 0x3212);
969 avio_w8(pb, sc->field_dominance);
970 }
971
972 }
973
mxf_write_cdci_desc(AVFormatContext * s,AVStream * st)974 static void mxf_write_cdci_desc(AVFormatContext *s, AVStream *st)
975 {
976 mxf_write_cdci_common(s, st, mxf_cdci_descriptor_key, 0);
977 }
978
mxf_write_mpegvideo_desc(AVFormatContext * s,AVStream * st)979 static void mxf_write_mpegvideo_desc(AVFormatContext *s, AVStream *st)
980 {
981 AVIOContext *pb = s->pb;
982 MXFStreamContext *sc = st->priv_data;
983 int profile_and_level = (st->codec->profile<<4) | st->codec->level;
984
985 mxf_write_cdci_common(s, st, mxf_mpegvideo_descriptor_key, 8+5);
986
987 // bit rate
988 mxf_write_local_tag(pb, 4, 0x8000);
989 avio_wb32(pb, sc->video_bit_rate);
990
991 // profile and level
992 mxf_write_local_tag(pb, 1, 0x8007);
993 if (!st->codec->profile)
994 profile_and_level |= 0x80; // escape bit
995 avio_w8(pb, profile_and_level);
996 }
997
mxf_write_generic_sound_common(AVFormatContext * s,AVStream * st,const UID key,unsigned size)998 static void mxf_write_generic_sound_common(AVFormatContext *s, AVStream *st, const UID key, unsigned size)
999 {
1000 AVIOContext *pb = s->pb;
1001 MXFContext *mxf = s->priv_data;
1002 int show_warnings = !mxf->footer_partition_offset;
1003
1004 mxf_write_generic_desc(s, st, key, size+5+12+8+8);
1005
1006 // audio locked
1007 mxf_write_local_tag(pb, 1, 0x3D02);
1008 avio_w8(pb, 1);
1009
1010 // write audio sampling rate
1011 mxf_write_local_tag(pb, 8, 0x3D03);
1012 avio_wb32(pb, st->codec->sample_rate);
1013 avio_wb32(pb, 1);
1014
1015 mxf_write_local_tag(pb, 4, 0x3D07);
1016 if (mxf->channel_count == -1) {
1017 if (show_warnings && (s->oformat == &ff_mxf_d10_muxer) && (st->codec->channels != 4) && (st->codec->channels != 8))
1018 av_log(s, AV_LOG_WARNING, "the number of audio channels shall be 4 or 8 : the output will not comply to MXF D-10 specs, use -d10_channelcount to fix this\n");
1019 avio_wb32(pb, st->codec->channels);
1020 } else if (s->oformat == &ff_mxf_d10_muxer) {
1021 if (show_warnings && (mxf->channel_count < st->codec->channels))
1022 av_log(s, AV_LOG_WARNING, "d10_channelcount < actual number of audio channels : some channels will be discarded\n");
1023 if (show_warnings && (mxf->channel_count != 4) && (mxf->channel_count != 8))
1024 av_log(s, AV_LOG_WARNING, "d10_channelcount shall be set to 4 or 8 : the output will not comply to MXF D-10 specs\n");
1025 avio_wb32(pb, mxf->channel_count);
1026 } else {
1027 if (show_warnings && mxf->channel_count != -1)
1028 av_log(s, AV_LOG_ERROR, "-d10_channelcount requires MXF D-10 and will be ignored\n");
1029 avio_wb32(pb, st->codec->channels);
1030 }
1031
1032 mxf_write_local_tag(pb, 4, 0x3D01);
1033 avio_wb32(pb, av_get_bits_per_sample(st->codec->codec_id));
1034 }
1035
mxf_write_wav_common(AVFormatContext * s,AVStream * st,const UID key,unsigned size)1036 static void mxf_write_wav_common(AVFormatContext *s, AVStream *st, const UID key, unsigned size)
1037 {
1038 AVIOContext *pb = s->pb;
1039
1040 mxf_write_generic_sound_common(s, st, key, size+6+8);
1041
1042 mxf_write_local_tag(pb, 2, 0x3D0A);
1043 avio_wb16(pb, st->codec->block_align);
1044
1045 // avg bytes per sec
1046 mxf_write_local_tag(pb, 4, 0x3D09);
1047 avio_wb32(pb, st->codec->block_align*st->codec->sample_rate);
1048 }
1049
mxf_write_wav_desc(AVFormatContext * s,AVStream * st)1050 static void mxf_write_wav_desc(AVFormatContext *s, AVStream *st)
1051 {
1052 mxf_write_wav_common(s, st, mxf_wav_descriptor_key, 0);
1053 }
1054
mxf_write_aes3_desc(AVFormatContext * s,AVStream * st)1055 static void mxf_write_aes3_desc(AVFormatContext *s, AVStream *st)
1056 {
1057 mxf_write_wav_common(s, st, mxf_aes3_descriptor_key, 0);
1058 }
1059
mxf_write_generic_sound_desc(AVFormatContext * s,AVStream * st)1060 static void mxf_write_generic_sound_desc(AVFormatContext *s, AVStream *st)
1061 {
1062 mxf_write_generic_sound_common(s, st, mxf_generic_sound_descriptor_key, 0);
1063 }
1064
mxf_write_package(AVFormatContext * s,enum MXFMetadataSetType type)1065 static void mxf_write_package(AVFormatContext *s, enum MXFMetadataSetType type)
1066 {
1067 MXFContext *mxf = s->priv_data;
1068 AVIOContext *pb = s->pb;
1069 int i, track_count = s->nb_streams+1;
1070
1071 if (type == MaterialPackage) {
1072 mxf_write_metadata_key(pb, 0x013600);
1073 PRINT_KEY(s, "Material Package key", pb->buf_ptr - 16);
1074 klv_encode_ber_length(pb, 92 + 16*track_count);
1075 } else {
1076 mxf_write_metadata_key(pb, 0x013700);
1077 PRINT_KEY(s, "Source Package key", pb->buf_ptr - 16);
1078 klv_encode_ber_length(pb, 112 + 16*track_count); // 20 bytes length for descriptor reference
1079 }
1080
1081 // write uid
1082 mxf_write_local_tag(pb, 16, 0x3C0A);
1083 mxf_write_uuid(pb, type, 0);
1084 av_log(s,AV_LOG_DEBUG, "package type:%d\n", type);
1085 PRINT_KEY(s, "package uid", pb->buf_ptr - 16);
1086
1087 // write package umid
1088 mxf_write_local_tag(pb, 32, 0x4401);
1089 mxf_write_umid(s, type == SourcePackage);
1090 PRINT_KEY(s, "package umid second part", pb->buf_ptr - 16);
1091
1092 // package creation date
1093 mxf_write_local_tag(pb, 8, 0x4405);
1094 avio_wb64(pb, mxf->timestamp);
1095
1096 // package modified date
1097 mxf_write_local_tag(pb, 8, 0x4404);
1098 avio_wb64(pb, mxf->timestamp);
1099
1100 // write track refs
1101 mxf_write_local_tag(pb, track_count*16 + 8, 0x4403);
1102 mxf_write_refs_count(pb, track_count);
1103 mxf_write_uuid(pb, type == MaterialPackage ? Track :
1104 Track + TypeBottom, -1); // timecode track
1105 for (i = 0; i < s->nb_streams; i++)
1106 mxf_write_uuid(pb, type == MaterialPackage ? Track : Track + TypeBottom, i);
1107
1108 // write multiple descriptor reference
1109 if (type == SourcePackage) {
1110 mxf_write_local_tag(pb, 16, 0x4701);
1111 if (s->nb_streams > 1) {
1112 mxf_write_uuid(pb, MultipleDescriptor, 0);
1113 mxf_write_multi_descriptor(s);
1114 } else
1115 mxf_write_uuid(pb, SubDescriptor, 0);
1116 }
1117
1118 // write timecode track
1119 mxf_write_track(s, mxf->timecode_track, type);
1120 mxf_write_sequence(s, mxf->timecode_track, type);
1121 mxf_write_timecode_component(s, mxf->timecode_track, type);
1122
1123 for (i = 0; i < s->nb_streams; i++) {
1124 AVStream *st = s->streams[i];
1125 mxf_write_track(s, st, type);
1126 mxf_write_sequence(s, st, type);
1127 mxf_write_structural_component(s, st, type);
1128
1129 if (type == SourcePackage) {
1130 MXFStreamContext *sc = st->priv_data;
1131 mxf_essence_container_uls[sc->index].write_desc(s, st);
1132 }
1133 }
1134 }
1135
mxf_write_essence_container_data(AVFormatContext * s)1136 static int mxf_write_essence_container_data(AVFormatContext *s)
1137 {
1138 AVIOContext *pb = s->pb;
1139
1140 mxf_write_metadata_key(pb, 0x012300);
1141 klv_encode_ber_length(pb, 72);
1142
1143 mxf_write_local_tag(pb, 16, 0x3C0A); // Instance UID
1144 mxf_write_uuid(pb, EssenceContainerData, 0);
1145
1146 mxf_write_local_tag(pb, 32, 0x2701); // Linked Package UID
1147 mxf_write_umid(s, 1);
1148
1149 mxf_write_local_tag(pb, 4, 0x3F07); // BodySID
1150 avio_wb32(pb, 1);
1151
1152 mxf_write_local_tag(pb, 4, 0x3F06); // IndexSID
1153 avio_wb32(pb, 2);
1154
1155 return 0;
1156 }
1157
mxf_write_header_metadata_sets(AVFormatContext * s)1158 static int mxf_write_header_metadata_sets(AVFormatContext *s)
1159 {
1160 mxf_write_preface(s);
1161 mxf_write_identification(s);
1162 mxf_write_content_storage(s);
1163 mxf_write_package(s, MaterialPackage);
1164 mxf_write_package(s, SourcePackage);
1165 mxf_write_essence_container_data(s);
1166 return 0;
1167 }
1168
klv_fill_size(uint64_t size)1169 static unsigned klv_fill_size(uint64_t size)
1170 {
1171 unsigned pad = KAG_SIZE - (size & (KAG_SIZE-1));
1172 if (pad < 20) // smallest fill item possible
1173 return pad + KAG_SIZE;
1174 else
1175 return pad & (KAG_SIZE-1);
1176 }
1177
mxf_write_index_table_segment(AVFormatContext * s)1178 static void mxf_write_index_table_segment(AVFormatContext *s)
1179 {
1180 MXFContext *mxf = s->priv_data;
1181 AVIOContext *pb = s->pb;
1182 int i, j, temporal_reordering = 0;
1183 int key_index = mxf->last_key_index;
1184
1185 av_log(s, AV_LOG_DEBUG, "edit units count %d\n", mxf->edit_units_count);
1186
1187 if (!mxf->edit_units_count && !mxf->edit_unit_byte_count)
1188 return;
1189
1190 avio_write(pb, index_table_segment_key, 16);
1191
1192 if (mxf->edit_unit_byte_count) {
1193 klv_encode_ber_length(pb, 80);
1194 } else {
1195 klv_encode_ber_length(pb, 85 + 12+(s->nb_streams+LLN(1))*6 +
1196 12+mxf->edit_units_count*(11+mxf->slice_count*LLN(4)));
1197 }
1198
1199 // instance id
1200 mxf_write_local_tag(pb, 16, 0x3C0A);
1201 mxf_write_uuid(pb, IndexTableSegment, 0);
1202
1203 // index edit rate
1204 mxf_write_local_tag(pb, 8, 0x3F0B);
1205 avio_wb32(pb, mxf->time_base.den);
1206 avio_wb32(pb, mxf->time_base.num);
1207
1208 // index start position
1209 mxf_write_local_tag(pb, 8, 0x3F0C);
1210 avio_wb64(pb, mxf->last_indexed_edit_unit);
1211
1212 // index duration
1213 mxf_write_local_tag(pb, 8, 0x3F0D);
1214 if (mxf->edit_unit_byte_count)
1215 avio_wb64(pb, 0); // index table covers whole container
1216 else
1217 avio_wb64(pb, mxf->edit_units_count);
1218
1219 // edit unit byte count
1220 mxf_write_local_tag(pb, 4, 0x3F05);
1221 avio_wb32(pb, mxf->edit_unit_byte_count);
1222
1223 // index sid
1224 mxf_write_local_tag(pb, 4, 0x3F06);
1225 avio_wb32(pb, 2);
1226
1227 // body sid
1228 mxf_write_local_tag(pb, 4, 0x3F07);
1229 avio_wb32(pb, 1);
1230
1231 if (!mxf->edit_unit_byte_count) {
1232 // real slice count - 1
1233 mxf_write_local_tag(pb, 1, 0x3F08);
1234 avio_w8(pb, mxf->slice_count);
1235
1236 // delta entry array
1237 mxf_write_local_tag(pb, 8 + (s->nb_streams+1)*6, 0x3F09);
1238 avio_wb32(pb, s->nb_streams+1); // num of entries
1239 avio_wb32(pb, 6); // size of one entry
1240 // write system item delta entry
1241 avio_w8(pb, 0);
1242 avio_w8(pb, 0); // slice entry
1243 avio_wb32(pb, 0); // element delta
1244 for (i = 0; i < s->nb_streams; i++) {
1245 AVStream *st = s->streams[i];
1246 MXFStreamContext *sc = st->priv_data;
1247 avio_w8(pb, sc->temporal_reordering);
1248 if (sc->temporal_reordering)
1249 temporal_reordering = 1;
1250 if (i == 0) { // video track
1251 avio_w8(pb, 0); // slice number
1252 avio_wb32(pb, KAG_SIZE); // system item size including klv fill
1253 } else { // audio track
1254 unsigned audio_frame_size = sc->aic.samples[0]*sc->aic.sample_size;
1255 audio_frame_size += klv_fill_size(audio_frame_size);
1256 avio_w8(pb, 1);
1257 avio_wb32(pb, (i-1)*audio_frame_size); // element delta
1258 }
1259 }
1260
1261 mxf_write_local_tag(pb, 8 + mxf->edit_units_count*(11+mxf->slice_count*4), 0x3F0A);
1262 avio_wb32(pb, mxf->edit_units_count); // num of entries
1263 avio_wb32(pb, 11+mxf->slice_count*4); // size of one entry
1264
1265 for (i = 0; i < mxf->edit_units_count; i++) {
1266 int temporal_offset = 0;
1267
1268 if (!(mxf->index_entries[i].flags & 0x33)) { // I frame
1269 mxf->last_key_index = key_index;
1270 key_index = i;
1271 }
1272
1273 if (temporal_reordering) {
1274 int pic_num_in_gop = i - key_index;
1275 if (pic_num_in_gop != mxf->index_entries[i].temporal_ref) {
1276 for (j = key_index; j < mxf->edit_units_count; j++) {
1277 if (pic_num_in_gop == mxf->index_entries[j].temporal_ref)
1278 break;
1279 }
1280 if (j == mxf->edit_units_count)
1281 av_log(s, AV_LOG_WARNING, "missing frames\n");
1282 temporal_offset = j - key_index - pic_num_in_gop;
1283 }
1284 }
1285 avio_w8(pb, temporal_offset);
1286
1287 if ((mxf->index_entries[i].flags & 0x30) == 0x30) { // back and forward prediction
1288 avio_w8(pb, mxf->last_key_index - i);
1289 } else {
1290 avio_w8(pb, key_index - i); // key frame offset
1291 if ((mxf->index_entries[i].flags & 0x20) == 0x20) // only forward
1292 mxf->last_key_index = key_index;
1293 }
1294
1295 if (!(mxf->index_entries[i].flags & 0x33) && // I frame
1296 mxf->index_entries[i].flags & 0x40 && !temporal_offset)
1297 mxf->index_entries[i].flags |= 0x80; // random access
1298 avio_w8(pb, mxf->index_entries[i].flags);
1299 // stream offset
1300 avio_wb64(pb, mxf->index_entries[i].offset);
1301 if (s->nb_streams > 1)
1302 avio_wb32(pb, mxf->index_entries[i].slice_offset);
1303 }
1304
1305 mxf->last_key_index = key_index - mxf->edit_units_count;
1306 mxf->last_indexed_edit_unit += mxf->edit_units_count;
1307 mxf->edit_units_count = 0;
1308 }
1309 }
1310
mxf_write_klv_fill(AVFormatContext * s)1311 static void mxf_write_klv_fill(AVFormatContext *s)
1312 {
1313 unsigned pad = klv_fill_size(avio_tell(s->pb));
1314 if (pad) {
1315 avio_write(s->pb, klv_fill_key, 16);
1316 pad -= 16 + 4;
1317 klv_encode_ber4_length(s->pb, pad);
1318 ffio_fill(s->pb, 0, pad);
1319 av_assert1(!(avio_tell(s->pb) & (KAG_SIZE-1)));
1320 }
1321 }
1322
mxf_write_partition(AVFormatContext * s,int bodysid,int indexsid,const uint8_t * key,int write_metadata)1323 static int mxf_write_partition(AVFormatContext *s, int bodysid,
1324 int indexsid,
1325 const uint8_t *key, int write_metadata)
1326 {
1327 MXFContext *mxf = s->priv_data;
1328 AVIOContext *pb = s->pb;
1329 int64_t header_byte_count_offset;
1330 unsigned index_byte_count = 0;
1331 uint64_t partition_offset = avio_tell(pb);
1332 int err;
1333
1334 if (!mxf->edit_unit_byte_count && mxf->edit_units_count)
1335 index_byte_count = 85 + 12+(s->nb_streams+1)*6 +
1336 12+mxf->edit_units_count*(11+mxf->slice_count*4);
1337 else if (mxf->edit_unit_byte_count && indexsid)
1338 index_byte_count = 80;
1339
1340 if (index_byte_count) {
1341 // add encoded ber length
1342 index_byte_count += 16 + klv_ber_length(index_byte_count);
1343 index_byte_count += klv_fill_size(index_byte_count);
1344 }
1345
1346 if (!memcmp(key, body_partition_key, 16)) {
1347 if ((err = av_reallocp_array(&mxf->body_partition_offset, mxf->body_partitions_count + 1,
1348 sizeof(*mxf->body_partition_offset))) < 0) {
1349 mxf->body_partitions_count = 0;
1350 return err;
1351 }
1352 mxf->body_partition_offset[mxf->body_partitions_count++] = partition_offset;
1353 }
1354
1355 // write klv
1356 avio_write(pb, key, 16);
1357 klv_encode_ber_length(pb, 88 + LLN(16) * DESCRIPTOR_COUNT(mxf->essence_container_count));
1358
1359 // write partition value
1360 avio_wb16(pb, 1); // majorVersion
1361 avio_wb16(pb, 2); // minorVersion
1362 avio_wb32(pb, KAG_SIZE); // KAGSize
1363
1364 avio_wb64(pb, partition_offset); // ThisPartition
1365
1366 if (!memcmp(key, body_partition_key, 16) && mxf->body_partitions_count > 1)
1367 avio_wb64(pb, mxf->body_partition_offset[mxf->body_partitions_count-2]); // PreviousPartition
1368 else if (!memcmp(key, footer_partition_key, 16) && mxf->body_partitions_count)
1369 avio_wb64(pb, mxf->body_partition_offset[mxf->body_partitions_count-1]); // PreviousPartition
1370 else
1371 avio_wb64(pb, 0);
1372
1373 avio_wb64(pb, mxf->footer_partition_offset); // footerPartition
1374
1375 // set offset
1376 header_byte_count_offset = avio_tell(pb);
1377 avio_wb64(pb, 0); // headerByteCount, update later
1378
1379 // indexTable
1380 avio_wb64(pb, index_byte_count); // indexByteCount
1381 avio_wb32(pb, index_byte_count ? indexsid : 0); // indexSID
1382
1383 // BodyOffset
1384 if (bodysid && mxf->edit_units_count && mxf->body_partitions_count) {
1385 avio_wb64(pb, mxf->body_offset);
1386 } else
1387 avio_wb64(pb, 0);
1388
1389 avio_wb32(pb, bodysid); // bodySID
1390
1391 // operational pattern
1392 avio_write(pb, op1a_ul, 16);
1393
1394 // essence container
1395 mxf_write_essence_container_refs(s);
1396
1397 if (write_metadata) {
1398 // mark the start of the headermetadata and calculate metadata size
1399 int64_t pos, start;
1400 unsigned header_byte_count;
1401
1402 mxf_write_klv_fill(s);
1403 start = avio_tell(s->pb);
1404 mxf_write_primer_pack(s);
1405 mxf_write_header_metadata_sets(s);
1406 pos = avio_tell(s->pb);
1407 header_byte_count = pos - start + klv_fill_size(pos);
1408
1409 // update header_byte_count
1410 avio_seek(pb, header_byte_count_offset, SEEK_SET);
1411 avio_wb64(pb, header_byte_count);
1412 avio_seek(pb, pos, SEEK_SET);
1413 }
1414
1415 avio_flush(pb);
1416
1417 return 0;
1418 }
1419
mxf_parse_dnxhd_frame(AVFormatContext * s,AVStream * st,AVPacket * pkt)1420 static int mxf_parse_dnxhd_frame(AVFormatContext *s, AVStream *st,
1421 AVPacket *pkt)
1422 {
1423 MXFContext *mxf = s->priv_data;
1424 MXFStreamContext *sc = st->priv_data;
1425 int i, cid;
1426 uint8_t* header_cid;
1427 int frame_size = 0;
1428
1429 if (mxf->header_written)
1430 return 1;
1431
1432 if (pkt->size < 43)
1433 return -1;
1434
1435 header_cid = pkt->data + 0x28;
1436 cid = header_cid[0] << 24 | header_cid[1] << 16 | header_cid[2] << 8 | header_cid[3];
1437
1438 if ((frame_size = avpriv_dnxhd_get_frame_size(cid)) < 0)
1439 return -1;
1440
1441 switch (cid) {
1442 case 1235:
1443 sc->index = 24;
1444 sc->component_depth = 10;
1445 break;
1446 case 1237:
1447 sc->index = 25;
1448 break;
1449 case 1238:
1450 sc->index = 26;
1451 break;
1452 case 1241:
1453 sc->index = 27;
1454 sc->component_depth = 10;
1455 break;
1456 case 1242:
1457 sc->index = 28;
1458 break;
1459 case 1243:
1460 sc->index = 29;
1461 break;
1462 case 1250:
1463 sc->index = 30;
1464 sc->component_depth = 10;
1465 break;
1466 case 1251:
1467 sc->index = 31;
1468 break;
1469 case 1252:
1470 sc->index = 32;
1471 break;
1472 case 1253:
1473 sc->index = 33;
1474 break;
1475 default:
1476 return -1;
1477 }
1478
1479 sc->codec_ul = &mxf_essence_container_uls[sc->index].codec_ul;
1480 sc->aspect_ratio = (AVRational){ 16, 9 };
1481
1482 mxf->edit_unit_byte_count = KAG_SIZE;
1483 for (i = 0; i < s->nb_streams; i++) {
1484 AVStream *st = s->streams[i];
1485 MXFStreamContext *sc = st->priv_data;
1486 if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
1487 mxf->edit_unit_byte_count += 16 + 4 + sc->aic.samples[0]*sc->aic.sample_size;
1488 mxf->edit_unit_byte_count += klv_fill_size(mxf->edit_unit_byte_count);
1489 } else if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
1490 mxf->edit_unit_byte_count += 16 + 4 + frame_size;
1491 mxf->edit_unit_byte_count += klv_fill_size(mxf->edit_unit_byte_count);
1492 }
1493 }
1494
1495 return 1;
1496 }
1497
mxf_parse_dv_frame(AVFormatContext * s,AVStream * st,AVPacket * pkt)1498 static int mxf_parse_dv_frame(AVFormatContext *s, AVStream *st, AVPacket *pkt)
1499 {
1500 MXFContext *mxf = s->priv_data;
1501 MXFStreamContext *sc = st->priv_data;
1502 uint8_t *vs_pack, *vsc_pack;
1503 int i, ul_index, frame_size, stype, pal;
1504
1505 if (mxf->header_written)
1506 return 1;
1507
1508 // Check for minimal frame size
1509 if (pkt->size < 120000)
1510 return -1;
1511
1512 vs_pack = pkt->data + 80*5 + 48;
1513 vsc_pack = pkt->data + 80*5 + 53;
1514 stype = vs_pack[3] & 0x1f;
1515 pal = (vs_pack[3] >> 5) & 0x1;
1516
1517 if ((vs_pack[2] & 0x07) == 0x02) {
1518 sc->aspect_ratio = (AVRational){ 16, 9 };
1519 } else {
1520 sc->aspect_ratio = (AVRational){ 4, 3 };
1521 }
1522 sc->interlaced = (vsc_pack[3] >> 4) & 0x01;
1523 // TODO: fix dv encoder to set proper FF/FS value in VSC pack
1524 // and set field dominance accordingly
1525 // av_log(s, AV_LOG_DEBUG, "DV vsc pack ff/ss = %x\n", vsc_pack[2] >> 6);
1526
1527 switch (stype) {
1528 case 0x18: // DV100 720p
1529 ul_index = 6 + pal;
1530 frame_size = pal ? 288000 : 240000;
1531 if (sc->interlaced) {
1532 av_log(s, AV_LOG_ERROR, "source marked as interlaced but codec profile is progressive\n");
1533 sc->interlaced = 0;
1534 }
1535 break;
1536 case 0x14: // DV100 1080i
1537 ul_index = 4 + pal;
1538 frame_size = pal ? 576000 : 480000;
1539 break;
1540 case 0x04: // DV50
1541 ul_index = 2 + pal;
1542 frame_size = pal ? 288000 : 240000;
1543 break;
1544 default: // DV25
1545 ul_index = 0 + pal;
1546 frame_size = pal ? 144000 : 120000;
1547 }
1548
1549 sc->index = ul_index + 16;
1550 sc->codec_ul = &mxf_essence_container_uls[sc->index].codec_ul;
1551
1552 mxf->edit_unit_byte_count = KAG_SIZE;
1553 for (i = 0; i < s->nb_streams; i++) {
1554 AVStream *st = s->streams[i];
1555 MXFStreamContext *sc = st->priv_data;
1556 if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
1557 mxf->edit_unit_byte_count += 16 + 4 + sc->aic.samples[0]*sc->aic.sample_size;
1558 mxf->edit_unit_byte_count += klv_fill_size(mxf->edit_unit_byte_count);
1559 } else if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
1560 mxf->edit_unit_byte_count += 16 + 4 + frame_size;
1561 mxf->edit_unit_byte_count += klv_fill_size(mxf->edit_unit_byte_count);
1562 }
1563 }
1564
1565 return 1;
1566 }
1567
1568 static const UID mxf_mpeg2_codec_uls[] = {
1569 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x01,0x10,0x00 }, // MP-ML I-Frame
1570 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x01,0x11,0x00 }, // MP-ML Long GOP
1571 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x02,0x02,0x00 }, // 422P-ML I-Frame
1572 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x02,0x03,0x00 }, // 422P-ML Long GOP
1573 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x03,0x02,0x00 }, // MP-HL I-Frame
1574 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x03,0x03,0x00 }, // MP-HL Long GOP
1575 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x04,0x02,0x00 }, // 422P-HL I-Frame
1576 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x04,0x03,0x00 }, // 422P-HL Long GOP
1577 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x05,0x02,0x00 }, // MP@H-14 I-Frame
1578 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x05,0x03,0x00 }, // MP@H-14 Long GOP
1579 };
1580
mxf_get_mpeg2_codec_ul(AVCodecContext * avctx)1581 static const UID *mxf_get_mpeg2_codec_ul(AVCodecContext *avctx)
1582 {
1583 int long_gop = avctx->gop_size > 1 || avctx->has_b_frames;
1584
1585 if (avctx->profile == 4) { // Main
1586 if (avctx->level == 8) // Main
1587 return &mxf_mpeg2_codec_uls[0+long_gop];
1588 else if (avctx->level == 4) // High
1589 return &mxf_mpeg2_codec_uls[4+long_gop];
1590 else if (avctx->level == 6) // High 14
1591 return &mxf_mpeg2_codec_uls[8+long_gop];
1592 } else if (avctx->profile == 0) { // 422
1593 if (avctx->level == 5) // Main
1594 return &mxf_mpeg2_codec_uls[2+long_gop];
1595 else if (avctx->level == 2) // High
1596 return &mxf_mpeg2_codec_uls[6+long_gop];
1597 }
1598 return NULL;
1599 }
1600
mxf_parse_mpeg2_frame(AVFormatContext * s,AVStream * st,AVPacket * pkt,MXFIndexEntry * e)1601 static int mxf_parse_mpeg2_frame(AVFormatContext *s, AVStream *st,
1602 AVPacket *pkt, MXFIndexEntry *e)
1603 {
1604 MXFStreamContext *sc = st->priv_data;
1605 uint32_t c = -1;
1606 int i;
1607
1608 for(i = 0; i < pkt->size - 4; i++) {
1609 c = (c<<8) + pkt->data[i];
1610 if (c == 0x1b5) {
1611 if ((pkt->data[i+1] & 0xf0) == 0x10) { // seq ext
1612 st->codec->profile = pkt->data[i+1] & 0x07;
1613 st->codec->level = pkt->data[i+2] >> 4;
1614 } else if (i + 5 < pkt->size && (pkt->data[i+1] & 0xf0) == 0x80) { // pict coding ext
1615 sc->interlaced = !(pkt->data[i+5] & 0x80); // progressive frame
1616 if (sc->interlaced)
1617 sc->field_dominance = 1 + !(pkt->data[i+4] & 0x80); // top field first
1618 break;
1619 }
1620 } else if (c == 0x1b8) { // gop
1621 if (pkt->data[i+4]>>6 & 0x01) { // closed
1622 sc->closed_gop = 1;
1623 if (e->flags & 0x40) // sequence header present
1624 e->flags |= 0x80; // random access
1625 }
1626 } else if (c == 0x1b3) { // seq
1627 e->flags |= 0x40;
1628 switch ((pkt->data[i+4]>>4) & 0xf) {
1629 case 2:
1630 sc->aspect_ratio = (AVRational){ 4, 3};
1631 break;
1632 case 3:
1633 sc->aspect_ratio = (AVRational){ 16, 9};
1634 break;
1635 case 4:
1636 sc->aspect_ratio = (AVRational){221,100};
1637 break;
1638 default:
1639 av_reduce(&sc->aspect_ratio.num, &sc->aspect_ratio.den,
1640 st->codec->width, st->codec->height, 1024*1024);
1641 }
1642 } else if (c == 0x100) { // pic
1643 int pict_type = (pkt->data[i+2]>>3) & 0x07;
1644 e->temporal_ref = (pkt->data[i+1]<<2) | (pkt->data[i+2]>>6);
1645 if (pict_type == 2) { // P frame
1646 e->flags |= 0x22;
1647 sc->closed_gop = 0; // reset closed gop, don't matter anymore
1648 } else if (pict_type == 3) { // B frame
1649 if (sc->closed_gop)
1650 e->flags |= 0x13; // only backward prediction
1651 else
1652 e->flags |= 0x33;
1653 sc->temporal_reordering = -1;
1654 } else if (!pict_type) {
1655 av_log(s, AV_LOG_ERROR, "error parsing mpeg2 frame\n");
1656 return 0;
1657 }
1658 }
1659 }
1660 if (s->oformat != &ff_mxf_d10_muxer)
1661 sc->codec_ul = mxf_get_mpeg2_codec_ul(st->codec);
1662 return !!sc->codec_ul;
1663 }
1664
mxf_parse_timestamp(time_t timestamp)1665 static uint64_t mxf_parse_timestamp(time_t timestamp)
1666 {
1667 struct tm *time = gmtime(×tamp);
1668 if (!time)
1669 return 0;
1670 return (uint64_t)(time->tm_year+1900) << 48 |
1671 (uint64_t)(time->tm_mon+1) << 40 |
1672 (uint64_t) time->tm_mday << 32 |
1673 time->tm_hour << 24 |
1674 time->tm_min << 16 |
1675 time->tm_sec << 8;
1676 }
1677
mxf_gen_umid(AVFormatContext * s)1678 static void mxf_gen_umid(AVFormatContext *s)
1679 {
1680 MXFContext *mxf = s->priv_data;
1681 uint32_t seed = av_get_random_seed();
1682 uint64_t umid = seed + LLN(0x5294713400000000);
1683
1684 AV_WB64(mxf->umid , umid);
1685 AV_WB64(mxf->umid+8, umid>>8);
1686
1687 mxf->instance_number = seed & 0xFFFFFF;
1688 }
1689
mxf_write_header(AVFormatContext * s)1690 static int mxf_write_header(AVFormatContext *s)
1691 {
1692 MXFContext *mxf = s->priv_data;
1693 int i, ret;
1694 uint8_t present[FF_ARRAY_ELEMS(mxf_essence_container_uls)] = {0};
1695 const MXFSamplesPerFrame *spf = NULL;
1696 AVDictionaryEntry *t;
1697 int64_t timestamp = 0;
1698 AVDictionaryEntry *tcr = av_dict_get(s->metadata, "timecode", NULL, 0);
1699
1700 if (!s->nb_streams)
1701 return -1;
1702
1703 for (i = 0; i < s->nb_streams; i++) {
1704 AVStream *st = s->streams[i];
1705 MXFStreamContext *sc = av_mallocz(sizeof(*sc));
1706 if (!sc)
1707 return AVERROR(ENOMEM);
1708 st->priv_data = sc;
1709
1710 if ((i == 0) ^ (st->codec->codec_type == AVMEDIA_TYPE_VIDEO)) {
1711 av_log(s, AV_LOG_ERROR, "there must be exactly one video stream and it must be the first one\n");
1712 return -1;
1713 }
1714
1715 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
1716 // TODO: should be avg_frame_rate
1717 AVRational rate, tbc = st->time_base;
1718 // Default component depth to 8
1719 sc->component_depth = 8;
1720 mxf->timecode_base = (tbc.den + tbc.num/2) / tbc.num;
1721 spf = ff_mxf_get_samples_per_frame(s, tbc);
1722 if (!spf) {
1723 av_log(s, AV_LOG_ERROR, "Unsupported video frame rate %d/%d\n",
1724 tbc.den, tbc.num);
1725 return AVERROR(EINVAL);
1726 }
1727 mxf->time_base = spf->time_base;
1728 rate = av_inv_q(mxf->time_base);
1729 avpriv_set_pts_info(st, 64, mxf->time_base.num, mxf->time_base.den);
1730 if (!tcr)
1731 tcr = av_dict_get(st->metadata, "timecode", NULL, 0);
1732 if (tcr)
1733 ret = av_timecode_init_from_string(&mxf->tc, rate, tcr->value, s);
1734 else
1735 ret = av_timecode_init(&mxf->tc, rate, 0, 0, s);
1736 if (ret < 0)
1737 return ret;
1738 sc->video_bit_rate = st->codec->bit_rate ? st->codec->bit_rate : st->codec->rc_max_rate;
1739 if (s->oformat == &ff_mxf_d10_muxer) {
1740 if (sc->video_bit_rate == 50000000) {
1741 if (mxf->time_base.den == 25) sc->index = 3;
1742 else sc->index = 5;
1743 } else if (sc->video_bit_rate == 40000000) {
1744 if (mxf->time_base.den == 25) sc->index = 7;
1745 else sc->index = 9;
1746 } else if (sc->video_bit_rate == 30000000) {
1747 if (mxf->time_base.den == 25) sc->index = 11;
1748 else sc->index = 13;
1749 } else {
1750 av_log(s, AV_LOG_ERROR, "error MXF D-10 only support 30/40/50 mbit/s\n");
1751 return -1;
1752 }
1753
1754 mxf->edit_unit_byte_count = KAG_SIZE; // system element
1755 mxf->edit_unit_byte_count += 16 + 4 + (uint64_t)sc->video_bit_rate *
1756 mxf->time_base.num / (8*mxf->time_base.den);
1757 mxf->edit_unit_byte_count += klv_fill_size(mxf->edit_unit_byte_count);
1758 mxf->edit_unit_byte_count += 16 + 4 + 4 + spf->samples_per_frame[0]*8*4;
1759 mxf->edit_unit_byte_count += klv_fill_size(mxf->edit_unit_byte_count);
1760 }
1761 } else if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
1762 if (st->codec->sample_rate != 48000) {
1763 av_log(s, AV_LOG_ERROR, "only 48khz is implemented\n");
1764 return -1;
1765 }
1766 avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
1767 if (s->oformat == &ff_mxf_d10_muxer) {
1768 if (st->index != 1) {
1769 av_log(s, AV_LOG_ERROR, "MXF D-10 only support one audio track\n");
1770 return -1;
1771 }
1772 if (st->codec->codec_id != AV_CODEC_ID_PCM_S16LE &&
1773 st->codec->codec_id != AV_CODEC_ID_PCM_S24LE) {
1774 av_log(s, AV_LOG_ERROR, "MXF D-10 only support 16 or 24 bits le audio\n");
1775 }
1776 sc->index = ((MXFStreamContext*)s->streams[0]->priv_data)->index + 1;
1777 } else
1778 mxf->slice_count = 1;
1779 }
1780
1781 if (!sc->index) {
1782 sc->index = mxf_get_essence_container_ul_index(st->codec->codec_id);
1783 if (sc->index == -1) {
1784 av_log(s, AV_LOG_ERROR, "track %d: could not find essence container ul, "
1785 "codec not currently supported in container\n", i);
1786 return -1;
1787 }
1788 }
1789
1790 sc->codec_ul = &mxf_essence_container_uls[sc->index].codec_ul;
1791
1792 memcpy(sc->track_essence_element_key, mxf_essence_container_uls[sc->index].element_ul, 15);
1793 sc->track_essence_element_key[15] = present[sc->index];
1794 PRINT_KEY(s, "track essence element key", sc->track_essence_element_key);
1795
1796 if (!present[sc->index])
1797 mxf->essence_container_count++;
1798 present[sc->index]++;
1799 }
1800
1801 if (s->oformat == &ff_mxf_d10_muxer) {
1802 mxf->essence_container_count = 1;
1803 }
1804
1805 if (!(s->flags & AVFMT_FLAG_BITEXACT))
1806 mxf_gen_umid(s);
1807
1808 for (i = 0; i < s->nb_streams; i++) {
1809 MXFStreamContext *sc = s->streams[i]->priv_data;
1810 // update element count
1811 sc->track_essence_element_key[13] = present[sc->index];
1812 if (!memcmp(sc->track_essence_element_key, mxf_essence_container_uls[15].element_ul, 13)) // DV
1813 sc->order = (0x15 << 24) | AV_RB32(sc->track_essence_element_key+13);
1814 else
1815 sc->order = AV_RB32(sc->track_essence_element_key+12);
1816 }
1817
1818 if (t = av_dict_get(s->metadata, "creation_time", NULL, 0))
1819 timestamp = ff_iso8601_to_unix_time(t->value);
1820 if (timestamp)
1821 mxf->timestamp = mxf_parse_timestamp(timestamp);
1822 mxf->duration = -1;
1823
1824 mxf->timecode_track = av_mallocz(sizeof(*mxf->timecode_track));
1825 if (!mxf->timecode_track)
1826 return AVERROR(ENOMEM);
1827 mxf->timecode_track->priv_data = av_mallocz(sizeof(MXFStreamContext));
1828 if (!mxf->timecode_track->priv_data)
1829 return AVERROR(ENOMEM);
1830 mxf->timecode_track->index = -1;
1831
1832 if (!spf) {
1833 spf = ff_mxf_get_samples_per_frame(s, (AVRational){ 1, 25 });
1834 }
1835
1836 if (ff_audio_interleave_init(s, spf->samples_per_frame, mxf->time_base) < 0)
1837 return -1;
1838
1839 return 0;
1840 }
1841
1842 static const uint8_t system_metadata_pack_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0D,0x01,0x03,0x01,0x04,0x01,0x01,0x00 };
1843 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 };
1844
mxf_write_system_item(AVFormatContext * s)1845 static void mxf_write_system_item(AVFormatContext *s)
1846 {
1847 MXFContext *mxf = s->priv_data;
1848 AVIOContext *pb = s->pb;
1849 unsigned frame;
1850 uint32_t time_code;
1851
1852 frame = mxf->last_indexed_edit_unit + mxf->edit_units_count;
1853
1854 // write system metadata pack
1855 avio_write(pb, system_metadata_pack_key, 16);
1856 klv_encode_ber4_length(pb, 57);
1857 avio_w8(pb, 0x5c); // UL, user date/time stamp, picture and sound item present
1858 avio_w8(pb, 0x04); // content package rate
1859 avio_w8(pb, 0x00); // content package type
1860 avio_wb16(pb, 0x00); // channel handle
1861 avio_wb16(pb, (mxf->tc.start + frame) & 0xFFFF); // continuity count, supposed to overflow
1862 if (mxf->essence_container_count > 1)
1863 avio_write(pb, multiple_desc_ul, 16);
1864 else {
1865 MXFStreamContext *sc = s->streams[0]->priv_data;
1866 avio_write(pb, mxf_essence_container_uls[sc->index].container_ul, 16);
1867 }
1868 avio_w8(pb, 0);
1869 avio_wb64(pb, 0);
1870 avio_wb64(pb, 0); // creation date/time stamp
1871
1872 avio_w8(pb, 0x81); // SMPTE 12M time code
1873 time_code = av_timecode_get_smpte_from_framenum(&mxf->tc, frame);
1874 avio_wb32(pb, time_code);
1875 avio_wb32(pb, 0); // binary group data
1876 avio_wb64(pb, 0);
1877
1878 // write system metadata package set
1879 avio_write(pb, system_metadata_package_set_key, 16);
1880 klv_encode_ber4_length(pb, 35);
1881 avio_w8(pb, 0x83); // UMID
1882 avio_wb16(pb, 0x20);
1883 mxf_write_umid(s, 1);
1884 }
1885
mxf_write_d10_video_packet(AVFormatContext * s,AVStream * st,AVPacket * pkt)1886 static void mxf_write_d10_video_packet(AVFormatContext *s, AVStream *st, AVPacket *pkt)
1887 {
1888 MXFContext *mxf = s->priv_data;
1889 AVIOContext *pb = s->pb;
1890 MXFStreamContext *sc = st->priv_data;
1891 int packet_size = (uint64_t)sc->video_bit_rate*mxf->time_base.num /
1892 (8*mxf->time_base.den); // frame size
1893 int pad;
1894
1895 packet_size += 16 + 4;
1896 packet_size += klv_fill_size(packet_size);
1897
1898 klv_encode_ber4_length(pb, pkt->size);
1899 avio_write(pb, pkt->data, pkt->size);
1900
1901 // ensure CBR muxing by padding to correct video frame size
1902 pad = packet_size - pkt->size - 16 - 4;
1903 if (pad > 20) {
1904 avio_write(s->pb, klv_fill_key, 16);
1905 pad -= 16 + 4;
1906 klv_encode_ber4_length(s->pb, pad);
1907 ffio_fill(s->pb, 0, pad);
1908 av_assert1(!(avio_tell(s->pb) & (KAG_SIZE-1)));
1909 } else {
1910 av_log(s, AV_LOG_WARNING, "cannot fill d-10 video packet\n");
1911 ffio_fill(s->pb, 0, pad);
1912 }
1913 }
1914
mxf_write_d10_audio_packet(AVFormatContext * s,AVStream * st,AVPacket * pkt)1915 static void mxf_write_d10_audio_packet(AVFormatContext *s, AVStream *st, AVPacket *pkt)
1916 {
1917 MXFContext *mxf = s->priv_data;
1918 AVIOContext *pb = s->pb;
1919 int frame_size = pkt->size / st->codec->block_align;
1920 uint8_t *samples = pkt->data;
1921 uint8_t *end = pkt->data + pkt->size;
1922 int i;
1923
1924 klv_encode_ber4_length(pb, 4 + frame_size*4*8);
1925
1926 avio_w8(pb, (frame_size == 1920 ? 0 : (mxf->edit_units_count-1) % 5 + 1));
1927 avio_wl16(pb, frame_size);
1928 avio_w8(pb, (1<<st->codec->channels)-1);
1929
1930 while (samples < end) {
1931 for (i = 0; i < st->codec->channels; i++) {
1932 uint32_t sample;
1933 if (st->codec->codec_id == AV_CODEC_ID_PCM_S24LE) {
1934 sample = AV_RL24(samples)<< 4;
1935 samples += 3;
1936 } else {
1937 sample = AV_RL16(samples)<<12;
1938 samples += 2;
1939 }
1940 avio_wl32(pb, sample | i);
1941 }
1942 for (; i < 8; i++)
1943 avio_wl32(pb, i);
1944 }
1945 }
1946
mxf_write_packet(AVFormatContext * s,AVPacket * pkt)1947 static int mxf_write_packet(AVFormatContext *s, AVPacket *pkt)
1948 {
1949 MXFContext *mxf = s->priv_data;
1950 AVIOContext *pb = s->pb;
1951 AVStream *st = s->streams[pkt->stream_index];
1952 MXFStreamContext *sc = st->priv_data;
1953 MXFIndexEntry ie = {0};
1954 int err;
1955
1956 if (!mxf->edit_unit_byte_count && !(mxf->edit_units_count % EDIT_UNITS_PER_BODY)) {
1957 if ((err = av_reallocp_array(&mxf->index_entries, mxf->edit_units_count
1958 + EDIT_UNITS_PER_BODY, sizeof(*mxf->index_entries))) < 0) {
1959 mxf->edit_units_count = 0;
1960 av_log(s, AV_LOG_ERROR, "could not allocate index entries\n");
1961 return err;
1962 }
1963 }
1964
1965 if (st->codec->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
1966 if (!mxf_parse_mpeg2_frame(s, st, pkt, &ie)) {
1967 av_log(s, AV_LOG_ERROR, "could not get mpeg2 profile and level\n");
1968 return -1;
1969 }
1970 } else if (st->codec->codec_id == AV_CODEC_ID_DNXHD) {
1971 if (!mxf_parse_dnxhd_frame(s, st, pkt)) {
1972 av_log(s, AV_LOG_ERROR, "could not get dnxhd profile\n");
1973 return -1;
1974 }
1975 } else if (st->codec->codec_id == AV_CODEC_ID_DVVIDEO) {
1976 if (!mxf_parse_dv_frame(s, st, pkt)) {
1977 av_log(s, AV_LOG_ERROR, "could not get dv profile\n");
1978 return -1;
1979 }
1980 }
1981
1982 if (!mxf->header_written) {
1983 if (mxf->edit_unit_byte_count) {
1984 if ((err = mxf_write_partition(s, 1, 2, header_open_partition_key, 1)) < 0)
1985 return err;
1986 mxf_write_klv_fill(s);
1987 mxf_write_index_table_segment(s);
1988 } else {
1989 if ((err = mxf_write_partition(s, 0, 0, header_open_partition_key, 1)) < 0)
1990 return err;
1991 }
1992 mxf->header_written = 1;
1993 }
1994
1995 if (st->index == 0) {
1996 if (!mxf->edit_unit_byte_count &&
1997 (!mxf->edit_units_count || mxf->edit_units_count > EDIT_UNITS_PER_BODY) &&
1998 !(ie.flags & 0x33)) { // I frame, Gop start
1999 mxf_write_klv_fill(s);
2000 if ((err = mxf_write_partition(s, 1, 2, body_partition_key, 0)) < 0)
2001 return err;
2002 mxf_write_klv_fill(s);
2003 mxf_write_index_table_segment(s);
2004 }
2005
2006 mxf_write_klv_fill(s);
2007 mxf_write_system_item(s);
2008
2009 if (!mxf->edit_unit_byte_count) {
2010 mxf->index_entries[mxf->edit_units_count].offset = mxf->body_offset;
2011 mxf->index_entries[mxf->edit_units_count].flags = ie.flags;
2012 mxf->index_entries[mxf->edit_units_count].temporal_ref = ie.temporal_ref;
2013 mxf->body_offset += KAG_SIZE; // size of system element
2014 }
2015 mxf->edit_units_count++;
2016 } else if (!mxf->edit_unit_byte_count && st->index == 1) {
2017 mxf->index_entries[mxf->edit_units_count-1].slice_offset =
2018 mxf->body_offset - mxf->index_entries[mxf->edit_units_count-1].offset;
2019 }
2020
2021 mxf_write_klv_fill(s);
2022 avio_write(pb, sc->track_essence_element_key, 16); // write key
2023 if (s->oformat == &ff_mxf_d10_muxer) {
2024 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO)
2025 mxf_write_d10_video_packet(s, st, pkt);
2026 else
2027 mxf_write_d10_audio_packet(s, st, pkt);
2028 } else {
2029 klv_encode_ber4_length(pb, pkt->size); // write length
2030 avio_write(pb, pkt->data, pkt->size);
2031 mxf->body_offset += 16+4+pkt->size + klv_fill_size(16+4+pkt->size);
2032 }
2033
2034 avio_flush(pb);
2035
2036 return 0;
2037 }
2038
mxf_write_random_index_pack(AVFormatContext * s)2039 static void mxf_write_random_index_pack(AVFormatContext *s)
2040 {
2041 MXFContext *mxf = s->priv_data;
2042 AVIOContext *pb = s->pb;
2043 uint64_t pos = avio_tell(pb);
2044 int i;
2045
2046 avio_write(pb, random_index_pack_key, 16);
2047 klv_encode_ber_length(pb, 28 + LLN(12)*mxf->body_partitions_count);
2048
2049 if (mxf->edit_unit_byte_count)
2050 avio_wb32(pb, 1); // BodySID of header partition
2051 else
2052 avio_wb32(pb, 0);
2053 avio_wb64(pb, 0); // offset of header partition
2054
2055 for (i = 0; i < mxf->body_partitions_count; i++) {
2056 avio_wb32(pb, 1); // BodySID
2057 avio_wb64(pb, mxf->body_partition_offset[i]);
2058 }
2059
2060 avio_wb32(pb, 0); // BodySID of footer partition
2061 avio_wb64(pb, mxf->footer_partition_offset);
2062
2063 avio_wb32(pb, avio_tell(pb) - pos + 4);
2064 }
2065
mxf_write_footer(AVFormatContext * s)2066 static int mxf_write_footer(AVFormatContext *s)
2067 {
2068 MXFContext *mxf = s->priv_data;
2069 AVIOContext *pb = s->pb;
2070 int err;
2071
2072 mxf->duration = mxf->last_indexed_edit_unit + mxf->edit_units_count;
2073
2074 mxf_write_klv_fill(s);
2075 mxf->footer_partition_offset = avio_tell(pb);
2076 if (mxf->edit_unit_byte_count) { // no need to repeat index
2077 if ((err = mxf_write_partition(s, 0, 0, footer_partition_key, 0)) < 0)
2078 return err;
2079 } else {
2080 if ((err = mxf_write_partition(s, 0, 2, footer_partition_key, 0)) < 0)
2081 return err;
2082 mxf_write_klv_fill(s);
2083 mxf_write_index_table_segment(s);
2084 }
2085
2086 mxf_write_klv_fill(s);
2087 mxf_write_random_index_pack(s);
2088
2089 if (s->pb->seekable) {
2090 avio_seek(pb, 0, SEEK_SET);
2091 if (mxf->edit_unit_byte_count) {
2092 if ((err = mxf_write_partition(s, 1, 2, header_closed_partition_key, 1)) < 0)
2093 return err;
2094 mxf_write_klv_fill(s);
2095 mxf_write_index_table_segment(s);
2096 } else {
2097 if ((err = mxf_write_partition(s, 0, 0, header_closed_partition_key, 1)) < 0)
2098 return err;
2099 }
2100 }
2101
2102 ff_audio_interleave_close(s);
2103
2104 av_freep(&mxf->index_entries);
2105 av_freep(&mxf->body_partition_offset);
2106 av_freep(&mxf->timecode_track->priv_data);
2107 av_freep(&mxf->timecode_track);
2108
2109 mxf_free(s);
2110
2111 return 0;
2112 }
2113
mxf_interleave_get_packet(AVFormatContext * s,AVPacket * out,AVPacket * pkt,int flush)2114 static int mxf_interleave_get_packet(AVFormatContext *s, AVPacket *out, AVPacket *pkt, int flush)
2115 {
2116 int i, stream_count = 0;
2117
2118 for (i = 0; i < s->nb_streams; i++)
2119 stream_count += !!s->streams[i]->last_in_packet_buffer;
2120
2121 if (stream_count && (s->nb_streams == stream_count || flush)) {
2122 AVPacketList *pktl = s->packet_buffer;
2123 if (s->nb_streams != stream_count) {
2124 AVPacketList *last = NULL;
2125 // find last packet in edit unit
2126 while (pktl) {
2127 if (!stream_count || pktl->pkt.stream_index == 0)
2128 break;
2129 last = pktl;
2130 pktl = pktl->next;
2131 stream_count--;
2132 }
2133 // purge packet queue
2134 while (pktl) {
2135 AVPacketList *next = pktl->next;
2136
2137 if(s->streams[pktl->pkt.stream_index]->last_in_packet_buffer == pktl)
2138 s->streams[pktl->pkt.stream_index]->last_in_packet_buffer= NULL;
2139 av_free_packet(&pktl->pkt);
2140 av_freep(&pktl);
2141 pktl = next;
2142 }
2143 if (last)
2144 last->next = NULL;
2145 else {
2146 s->packet_buffer = NULL;
2147 s->packet_buffer_end= NULL;
2148 goto out;
2149 }
2150 pktl = s->packet_buffer;
2151 }
2152
2153 *out = pktl->pkt;
2154 av_dlog(s, "out st:%d dts:%"PRId64"\n", (*out).stream_index, (*out).dts);
2155 s->packet_buffer = pktl->next;
2156 if(s->streams[pktl->pkt.stream_index]->last_in_packet_buffer == pktl)
2157 s->streams[pktl->pkt.stream_index]->last_in_packet_buffer= NULL;
2158 if(!s->packet_buffer)
2159 s->packet_buffer_end= NULL;
2160 av_freep(&pktl);
2161 return 1;
2162 } else {
2163 out:
2164 av_init_packet(out);
2165 return 0;
2166 }
2167 }
2168
mxf_compare_timestamps(AVFormatContext * s,AVPacket * next,AVPacket * pkt)2169 static int mxf_compare_timestamps(AVFormatContext *s, AVPacket *next, AVPacket *pkt)
2170 {
2171 MXFStreamContext *sc = s->streams[pkt ->stream_index]->priv_data;
2172 MXFStreamContext *sc2 = s->streams[next->stream_index]->priv_data;
2173
2174 return next->dts > pkt->dts ||
2175 (next->dts == pkt->dts && sc->order < sc2->order);
2176 }
2177
mxf_interleave(AVFormatContext * s,AVPacket * out,AVPacket * pkt,int flush)2178 static int mxf_interleave(AVFormatContext *s, AVPacket *out, AVPacket *pkt, int flush)
2179 {
2180 return ff_audio_rechunk_interleave(s, out, pkt, flush,
2181 mxf_interleave_get_packet, mxf_compare_timestamps);
2182 }
2183
2184 static const AVOption d10_options[] = {
2185 { "d10_channelcount", "Force/set channelcount in generic sound essence descriptor",
2186 offsetof(MXFContext, channel_count), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 8, AV_OPT_FLAG_ENCODING_PARAM},
2187 { NULL },
2188 };
2189
2190 static const AVClass mxf_d10_muxer_class = {
2191 .class_name = "MXF-D10 muxer",
2192 .item_name = av_default_item_name,
2193 .option = d10_options,
2194 .version = LIBAVUTIL_VERSION_INT,
2195 };
2196
2197 AVOutputFormat ff_mxf_muxer = {
2198 .name = "mxf",
2199 .long_name = NULL_IF_CONFIG_SMALL("MXF (Material eXchange Format)"),
2200 .mime_type = "application/mxf",
2201 .extensions = "mxf",
2202 .priv_data_size = sizeof(MXFContext),
2203 .audio_codec = AV_CODEC_ID_PCM_S16LE,
2204 .video_codec = AV_CODEC_ID_MPEG2VIDEO,
2205 .write_header = mxf_write_header,
2206 .write_packet = mxf_write_packet,
2207 .write_trailer = mxf_write_footer,
2208 .flags = AVFMT_NOTIMESTAMPS,
2209 .interleave_packet = mxf_interleave,
2210 };
2211
2212 AVOutputFormat ff_mxf_d10_muxer = {
2213 .name = "mxf_d10",
2214 .long_name = NULL_IF_CONFIG_SMALL("MXF (Material eXchange Format) D-10 Mapping"),
2215 .mime_type = "application/mxf",
2216 .priv_data_size = sizeof(MXFContext),
2217 .audio_codec = AV_CODEC_ID_PCM_S16LE,
2218 .video_codec = AV_CODEC_ID_MPEG2VIDEO,
2219 .write_header = mxf_write_header,
2220 .write_packet = mxf_write_packet,
2221 .write_trailer = mxf_write_footer,
2222 .flags = AVFMT_NOTIMESTAMPS,
2223 .interleave_packet = mxf_interleave,
2224 .priv_class = &mxf_d10_muxer_class,
2225 };
2226