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