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