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