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  * signal_standard, color_siting, store_user_comments, sample rate and klv_fill_key version
25  * fixes sponsored by NOA GmbH
26  */
27 
28 /*
29  * References
30  * SMPTE 336M KLV Data Encoding Protocol Using Key-Length-Value
31  * SMPTE 377M MXF File Format Specifications
32  * SMPTE 379M MXF Generic Container
33  * SMPTE 381M Mapping MPEG Streams into the MXF Generic Container
34  * SMPTE 422M Mapping JPEG 2000 Codestreams into the MXF Generic Container
35  * SMPTE RP210: SMPTE Metadata Dictionary
36  * SMPTE RP224: Registry of SMPTE Universal Labels
37  */
38 
39 #include <inttypes.h>
40 #include <math.h>
41 #include <time.h>
42 
43 #include "libavutil/opt.h"
44 #include "libavutil/random_seed.h"
45 #include "libavutil/timecode.h"
46 #include "libavutil/avassert.h"
47 #include "libavutil/pixdesc.h"
49 #include "libavcodec/bytestream.h"
50 #include "libavcodec/dnxhddata.h"
51 #include "libavcodec/dv_profile.h"
52 #include "libavcodec/h264.h"
53 #include "libavcodec/internal.h"
54 #include "audiointerleave.h"
55 #include "avformat.h"
56 #include "avio_internal.h"
57 #include "internal.h"
58 #include "mxf.h"
59 #include "config.h"
60 
63 
64 #define EDIT_UNITS_PER_BODY 250
65 #define KAG_SIZE 512
66 
67 typedef struct MXFLocalTagPair {
68  int local_tag;
71 
72 typedef struct MXFIndexEntry {
74  uint64_t offset;
75  unsigned slice_offset; ///< offset of audio slice
76  uint16_t temporal_ref;
78 
79 typedef struct MXFStreamContext {
82  int index; ///< index in mxf_essence_container_uls table
83  const UID *codec_ul;
84  int order; ///< interleaving order if dts are equal
85  int interlaced; ///< whether picture is interlaced
86  int field_dominance; ///< tff=1, bff=2
92  AVRational aspect_ratio; ///< display aspect ratio
93  int closed_gop; ///< gop is closed, used in mpeg-2 frame parsing
96 
97 typedef struct MXFContainerEssenceEntry {
103 
104 enum ULIndex {
143 };
144 
145 static const struct {
146  enum AVCodecID id;
148 } mxf_essence_mappings[] = {
156  { AV_CODEC_ID_NONE }
157 };
158 
159 static void mxf_write_wav_desc(AVFormatContext *s, AVStream *st);
160 static void mxf_write_aes3_desc(AVFormatContext *s, AVStream *st);
162 static void mxf_write_cdci_desc(AVFormatContext *s, AVStream *st);
164 
166  { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x02,0x0D,0x01,0x03,0x01,0x02,0x04,0x60,0x01 },
167  { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x15,0x01,0x05,0x00 },
168  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x00,0x00,0x00 },
170  { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x06,0x03,0x00 },
171  { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x16,0x01,0x03,0x00 },
172  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 },
174  { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x06,0x01,0x00 },
175  { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x16,0x01,0x01,0x00 },
176  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 },
178  // D-10 625/50 PAL 50mb/s
179  { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x01,0x01 },
180  { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x05,0x01,0x01,0x00 },
181  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x01,0x02,0x01,0x01 },
183  { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x01,0x01 },
184  { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x06,0x01,0x10,0x00 },
185  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 },
187  // D-10 525/60 NTSC 50mb/s
188  { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x02,0x01 },
189  { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x05,0x01,0x01,0x00 },
190  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x01,0x02,0x01,0x02 },
192  { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x02,0x01 },
193  { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x06,0x01,0x10,0x00 },
194  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 },
196  // D-10 625/50 PAL 40mb/s
197  { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x03,0x01 },
198  { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x05,0x01,0x01,0x00 },
199  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x01,0x02,0x01,0x03 },
201  { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x03,0x01 },
202  { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x06,0x01,0x10,0x00 },
203  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 },
205  // D-10 525/60 NTSC 40mb/s
206  { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x04,0x01 },
207  { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x05,0x01,0x01,0x00 },
208  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x01,0x02,0x01,0x04 },
210  { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x04,0x01 },
211  { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x06,0x01,0x10,0x00 },
212  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 },
214  // D-10 625/50 PAL 30mb/s
215  { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x05,0x01 },
216  { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x05,0x01,0x01,0x00 },
217  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x01,0x02,0x01,0x05 },
219  { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x05,0x01 },
220  { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x06,0x01,0x10,0x00 },
221  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 },
223  // D-10 525/60 NTSC 30mb/s
224  { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x06,0x01 },
225  { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x05,0x01,0x01,0x00 },
226  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x01,0x02,0x01,0x06 },
228  { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x06,0x01 },
229  { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x06,0x01,0x10,0x00 },
230  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 },
232  // DV Unknown
233  { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x02,0x7F,0x01 },
234  { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x18,0x01,0x01,0x00 },
235  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x02,0x00,0x00,0x00 },
237 
238  // DV25 525/60
239  { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x02,0x40,0x01 },
240  { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x18,0x01,0x01,0x00 },
241  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x02,0x02,0x01,0x00 },
243  // DV25 625/50
244  { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x02,0x41,0x01 },
245  { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x18,0x01,0x01,0x00 },
246  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x02,0x02,0x02,0x00 },
248 
249  // IEC DV25 525/60
250  { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x02,0x01,0x01 },
251  { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x18,0x01,0x01,0x00 },
252  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x02,0x02,0x01,0x00 },
254  // IEC DV25 625/50
255  { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x02,0x02,0x01 },
256  { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x18,0x01,0x01,0x00 },
257  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x02,0x01,0x02,0x00 },
259 
260  // DV50 525/60
261  { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x02,0x50,0x01 },
262  { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x18,0x01,0x01,0x00 },
263  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x02,0x02,0x03,0x00 },
265  // DV50 625/50
266  { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x02,0x51,0x01 },
267  { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x18,0x01,0x01,0x00 },
268  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x02,0x02,0x04,0x00 },
270  // DV100 1080/60
271  { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x02,0x60,0x01 },
272  { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x18,0x01,0x01,0x00 },
273  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x02,0x02,0x05,0x00 },
275  // DV100 1080/50
276  { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x02,0x61,0x01 },
277  { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x18,0x01,0x01,0x00 },
278  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x02,0x02,0x06,0x00 },
280  // DV100 720/60
281  { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x02,0x62,0x01 },
282  { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x18,0x01,0x01,0x00 },
283  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x02,0x02,0x07,0x00 },
285  // DV100 720/50
286  { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x02,0x63,0x01 },
287  { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x18,0x01,0x01,0x00 },
288  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x02,0x02,0x08,0x00 },
290  // DNxHD 1080p 10bit high
291  { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x11,0x01,0x00 },
292  { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x15,0x01,0x05,0x00 },
293  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0A,0x04,0x01,0x02,0x02,0x71,0x01,0x00,0x00 },
295  // DNxHD 1080p 8bit medium
296  { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x11,0x01,0x00 },
297  { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x15,0x01,0x05,0x00 },
298  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0A,0x04,0x01,0x02,0x02,0x71,0x03,0x00,0x00 },
300  // DNxHD 1080p 8bit high
301  { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x11,0x01,0x00 },
302  { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x15,0x01,0x05,0x00 },
303  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0A,0x04,0x01,0x02,0x02,0x71,0x04,0x00,0x00 },
305  // DNxHD 1080i 10bit high
306  { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x11,0x01,0x00 },
307  { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x15,0x01,0x05,0x00 },
308  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0A,0x04,0x01,0x02,0x02,0x71,0x07,0x00,0x00 },
310  // DNxHD 1080i 8bit medium
311  { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x11,0x01,0x00 },
312  { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x15,0x01,0x05,0x00 },
313  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0A,0x04,0x01,0x02,0x02,0x71,0x08,0x00,0x00 },
315  // DNxHD 1080i 8bit high
316  { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x11,0x01,0x00 },
317  { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x15,0x01,0x05,0x00 },
318  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0A,0x04,0x01,0x02,0x02,0x71,0x09,0x00,0x00 },
320  // DNxHD 720p 10bit
321  { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x01,0x0d,0x01,0x03,0x01,0x02,0x11,0x01,0x00 },
322  { 0x06,0x0e,0x2b,0x34,0x01,0x02,0x01,0x01,0x0d,0x01,0x03,0x01,0x15,0x01,0x05,0x00 },
323  { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x0A,0x04,0x01,0x02,0x02,0x71,0x10,0x00,0x00 },
325  // DNxHD 720p 8bit high
326  { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x01,0x0d,0x01,0x03,0x01,0x02,0x11,0x01,0x00 },
327  { 0x06,0x0e,0x2b,0x34,0x01,0x02,0x01,0x01,0x0d,0x01,0x03,0x01,0x15,0x01,0x05,0x00 },
328  { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x0A,0x04,0x01,0x02,0x02,0x71,0x11,0x00,0x00 },
330  // DNxHD 720p 8bit medium
331  { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x01,0x0d,0x01,0x03,0x01,0x02,0x11,0x01,0x00 },
332  { 0x06,0x0e,0x2b,0x34,0x01,0x02,0x01,0x01,0x0d,0x01,0x03,0x01,0x15,0x01,0x05,0x00 },
333  { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x0A,0x04,0x01,0x02,0x02,0x71,0x12,0x00,0x00 },
335  // DNxHD 720p 8bit low
336  { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x01,0x0d,0x01,0x03,0x01,0x02,0x11,0x01,0x00 },
337  { 0x06,0x0e,0x2b,0x34,0x01,0x02,0x01,0x01,0x0d,0x01,0x03,0x01,0x15,0x01,0x05,0x00 },
338  { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x0A,0x04,0x01,0x02,0x02,0x71,0x13,0x00,0x00 },
340  // JPEG2000
341  { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x07,0x0d,0x01,0x03,0x01,0x02,0x0c,0x01,0x00 },
342  { 0x06,0x0e,0x2b,0x34,0x01,0x02,0x01,0x01,0x0d,0x01,0x03,0x01,0x15,0x01,0x08,0x00 },
343  { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x07,0x04,0x01,0x02,0x02,0x03,0x01,0x01,0x00 },
345  // H.264
346  { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x0D,0x01,0x03,0x01,0x02,0x10,0x60,0x01 },
347  { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x15,0x01,0x05,0x00 },
348  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x00,0x00,0x00 },
350  { { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 },
351  { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 },
352  { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 },
353  NULL },
354 };
355 
356 typedef struct MXFContext {
364  uint64_t timestamp; ///< timestamp, as year(16),month(8),day(8),hour(8),minutes(8),msec/4(8)
365  uint8_t slice_count; ///< index slice count minus 1 (1 if no audio, 0 otherwise)
369  int last_key_index; ///< index of last key frame
370  uint64_t duration;
371  AVTimecode tc; ///< timecode context
373  int timecode_base; ///< rounded time code base (25 or 30)
374  int edit_unit_byte_count; ///< fixed edit unit byte count
375  uint64_t body_offset;
376  uint32_t instance_number;
377  uint8_t umid[16]; ///< unique material identifier
383 } MXFContext;
384 
385 static const uint8_t uuid_base[] = { 0xAD,0xAB,0x44,0x24,0x2f,0x25,0x4d,0xc7,0x92,0xff,0x29,0xbd };
386 static const uint8_t umid_ul[] = { 0x06,0x0A,0x2B,0x34,0x01,0x01,0x01,0x05,0x01,0x01,0x0D,0x00,0x13 };
387 
388 /**
389  * complete key for operation pattern, partitions, and primer pack
390  */
391 static const uint8_t op1a_ul[] = { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x02,0x01,0x01,0x01,0x09,0x00 };
392 static const uint8_t opatom_ul[] = { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x02,0x0D,0x01,0x02,0x01,0x10,0x03,0x00,0x00 };
393 static const uint8_t footer_partition_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0D,0x01,0x02,0x01,0x01,0x04,0x04,0x00 }; // ClosedComplete
394 static const uint8_t primer_pack_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0D,0x01,0x02,0x01,0x01,0x05,0x01,0x00 };
395 static const uint8_t index_table_segment_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x10,0x01,0x00 };
396 static const uint8_t random_index_pack_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0D,0x01,0x02,0x01,0x01,0x11,0x01,0x00 };
397 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
398 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
399 static const uint8_t klv_fill_key[] = { 0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x03,0x01,0x02,0x10,0x01,0x00,0x00,0x00 };
400 static const uint8_t body_partition_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0D,0x01,0x02,0x01,0x01,0x03,0x04,0x00 }; // ClosedComplete
401 
402 /**
403  * partial key for header metadata
404  */
405 static const uint8_t header_metadata_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0D,0x01,0x01,0x01,0x01 };
406 static const uint8_t multiple_desc_ul[] = { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x0D,0x01,0x03,0x01,0x02,0x7F,0x01,0x00 };
407 
408 /**
409  * SMPTE RP210 http://www.smpte-ra.org/mdd/index.html
410  * https://smpte-ra.org/sites/default/files/Labels.xml
411  */
413  // preface set
414  { 0x3C0A, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x01,0x01,0x15,0x02,0x00,0x00,0x00,0x00}}, /* Instance UID */
415  { 0x3B02, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x10,0x02,0x04,0x00,0x00}}, /* Last Modified Date */
416  { 0x3B05, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x03,0x01,0x02,0x01,0x05,0x00,0x00,0x00}}, /* Version */
417  { 0x3B06, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x06,0x04,0x00,0x00}}, /* Identifications reference */
418  { 0x3B03, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x02,0x01,0x00,0x00}}, /* Content Storage reference */
419  { 0x3B09, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x01,0x02,0x02,0x03,0x00,0x00,0x00,0x00}}, /* Operational Pattern UL */
420  { 0x3B0A, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x01,0x02,0x02,0x10,0x02,0x01,0x00,0x00}}, /* Essence Containers UL batch */
421  { 0x3B0B, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x01,0x02,0x02,0x10,0x02,0x02,0x00,0x00}}, /* DM Schemes UL batch */
422  // Identification
423  { 0x3C09, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x20,0x07,0x01,0x01,0x00,0x00,0x00}}, /* This Generation UID */
424  { 0x3C01, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x20,0x07,0x01,0x02,0x01,0x00,0x00}}, /* Company Name */
425  { 0x3C02, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x20,0x07,0x01,0x03,0x01,0x00,0x00}}, /* Product Name */
426  { 0x3C04, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x20,0x07,0x01,0x05,0x01,0x00,0x00}}, /* Version String */
427  { 0x3C05, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x20,0x07,0x01,0x07,0x00,0x00,0x00}}, /* Product ID */
428  { 0x3C06, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x10,0x02,0x03,0x00,0x00}}, /* Modification Date */
429  // Content Storage
430  { 0x1901, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x05,0x01,0x00,0x00}}, /* Package strong reference batch */
431  { 0x1902, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x05,0x02,0x00,0x00}}, /* Package strong reference batch */
432  // Essence Container Data
433  { 0x2701, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x06,0x01,0x00,0x00,0x00}}, /* Linked Package UID */
434  { 0x3F07, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x01,0x03,0x04,0x04,0x00,0x00,0x00,0x00}}, /* BodySID */
435  // Package
436  { 0x4401, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x01,0x01,0x15,0x10,0x00,0x00,0x00,0x00}}, /* Package UID */
437  { 0x4405, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x10,0x01,0x03,0x00,0x00}}, /* Package Creation Date */
438  { 0x4404, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x10,0x02,0x05,0x00,0x00}}, /* Package Modified Date */
439  { 0x4402, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x01,0x03,0x03,0x02,0x01,0x00,0x00,0x00}}, /* Package Name */
440  { 0x4403, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x06,0x05,0x00,0x00}}, /* Tracks Strong reference array */
441  { 0x4701, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x02,0x03,0x00,0x00}}, /* Descriptor */
442  // Track
443  { 0x4801, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x01,0x07,0x01,0x01,0x00,0x00,0x00,0x00}}, /* Track ID */
444  { 0x4804, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x01,0x04,0x01,0x03,0x00,0x00,0x00,0x00}}, /* Track Number */
445  { 0x4B01, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x30,0x04,0x05,0x00,0x00,0x00,0x00}}, /* Edit Rate */
446  { 0x4B02, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x03,0x01,0x03,0x00,0x00}}, /* Origin */
447  { 0x4803, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x02,0x04,0x00,0x00}}, /* Sequence reference */
448  // Sequence
449  { 0x0201, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x07,0x01,0x00,0x00,0x00,0x00,0x00}}, /* Data Definition UL */
450  { 0x0202, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x02,0x01,0x01,0x03,0x00,0x00}}, /* Duration */
451  { 0x1001, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x06,0x09,0x00,0x00}}, /* Structural Components reference array */
452  // Source Clip
453  { 0x1201, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x03,0x01,0x04,0x00,0x00}}, /* Start position */
454  { 0x1101, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x03,0x01,0x00,0x00,0x00}}, /* SourcePackageID */
455  { 0x1102, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x03,0x02,0x00,0x00,0x00}}, /* SourceTrackID */
456  // Timecode Component
457  { 0x1501, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x03,0x01,0x05,0x00,0x00}}, /* Start Time Code */
458  { 0x1502, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x04,0x01,0x01,0x02,0x06,0x00,0x00}}, /* Rounded Time Code Base */
459  { 0x1503, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x04,0x01,0x01,0x05,0x00,0x00,0x00}}, /* Drop Frame */
460  // File Descriptor
461  { 0x3F01, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x06,0x01,0x01,0x04,0x06,0x0B,0x00,0x00}}, /* Sub Descriptors reference array */
462  { 0x3006, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x06,0x01,0x01,0x03,0x05,0x00,0x00,0x00}}, /* Linked Track ID */
463  { 0x3001, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x06,0x01,0x01,0x00,0x00,0x00,0x00}}, /* SampleRate */
464  { 0x3002, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x06,0x01,0x02,0x00,0x00,0x00,0x00}}, /* ContainerDuration */
465  { 0x3004, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x01,0x02,0x00,0x00}}, /* Essence Container */
466  // Generic Picture Essence Descriptor
467  { 0x320C, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x03,0x01,0x04,0x00,0x00,0x00}}, /* Frame Layout */
468  { 0x320D, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x01,0x03,0x02,0x05,0x00,0x00,0x00}}, /* Video Line Map */
469  { 0x3203, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x02,0x02,0x00,0x00,0x00}}, /* Stored Width */
470  { 0x3202, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x02,0x01,0x00,0x00,0x00}}, /* Stored Height */
471  { 0x3209, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x01,0x0C,0x00,0x00,0x00}}, /* Display Width */
472  { 0x3208, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x01,0x0B,0x00,0x00,0x00}}, /* Display Height */
473  { 0x320B, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x01,0x0E,0x00,0x00,0x00}}, /* Presentation Y offset */
474  { 0x320E, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x01,0x01,0x01,0x00,0x00,0x00}}, /* Aspect Ratio */
475  { 0x3201, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x01,0x06,0x01,0x00,0x00,0x00,0x00}}, /* Picture Essence Coding */
476  { 0x3212, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x01,0x03,0x01,0x06,0x00,0x00,0x00}}, /* Field Dominance (Opt) */
477  { 0x3215, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x05,0x01,0x13,0x00,0x00,0x00,0x00}}, /* Signal Standard */
478  // CDCI Picture Essence Descriptor
479  { 0x3301, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x01,0x05,0x03,0x0A,0x00,0x00,0x00}}, /* Component Depth */
480  { 0x3302, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x01,0x05,0x00,0x00,0x00}}, /* Horizontal Subsampling */
481  { 0x3303, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x01,0x06,0x00,0x00,0x00}}, /* Color Siting */
482  // Generic Sound Essence Descriptor
483  { 0x3D02, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x04,0x02,0x03,0x01,0x04,0x00,0x00,0x00}}, /* Locked/Unlocked */
484  { 0x3D03, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x02,0x03,0x01,0x01,0x01,0x00,0x00}}, /* Audio sampling rate */
485  { 0x3D07, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x02,0x01,0x01,0x04,0x00,0x00,0x00}}, /* ChannelCount */
486  { 0x3D01, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x04,0x02,0x03,0x03,0x04,0x00,0x00,0x00}}, /* Quantization bits */
487  { 0x3D06, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x02,0x04,0x02,0x00,0x00,0x00,0x00}}, /* Sound Essence Compression */
488  // Index Table Segment
489  { 0x3F0B, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x05,0x30,0x04,0x06,0x00,0x00,0x00,0x00}}, /* Index Edit Rate */
490  { 0x3F0C, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x07,0x02,0x01,0x03,0x01,0x0A,0x00,0x00}}, /* Index Start Position */
491  { 0x3F0D, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x07,0x02,0x02,0x01,0x01,0x02,0x00,0x00}}, /* Index Duration */
492  { 0x3F05, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x04,0x06,0x02,0x01,0x00,0x00,0x00,0x00}}, /* Edit Unit Byte Count */
493  { 0x3F06, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x01,0x03,0x04,0x05,0x00,0x00,0x00,0x00}}, /* IndexSID */
494  { 0x3F08, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x04,0x04,0x04,0x01,0x01,0x00,0x00,0x00}}, /* Slice Count */
495  { 0x3F09, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x04,0x04,0x01,0x06,0x00,0x00,0x00}}, /* Delta Entry Array */
496  { 0x3F0A, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x04,0x04,0x02,0x05,0x00,0x00,0x00}}, /* Index Entry Array */
497  // MPEG video Descriptor
498  { 0x8000, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x01,0x06,0x02,0x01,0x0B,0x00,0x00}}, /* BitRate */
499  { 0x8007, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x01,0x06,0x02,0x01,0x0A,0x00,0x00}}, /* ProfileAndLevel */
500  // Wave Audio Essence Descriptor
501  { 0x3D09, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x02,0x03,0x03,0x05,0x00,0x00,0x00}}, /* Average Bytes Per Second */
502  { 0x3D0A, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x02,0x03,0x02,0x01,0x00,0x00,0x00}}, /* Block Align */
503 };
504 
506  { 0x4406, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x03,0x02,0x01,0x02,0x0C,0x00,0x00,0x00}}, /* User Comments */
507  { 0x5001, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x03,0x02,0x01,0x02,0x09,0x01,0x00,0x00}}, /* Name */
508  { 0x5003, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x03,0x02,0x01,0x02,0x0A,0x01,0x00,0x00}}, /* Value */
509 };
510 
512 {
513  avio_write(pb, uuid_base, 12);
514  avio_wb16(pb, type);
515  avio_wb16(pb, value);
516 }
517 
519 {
520  MXFContext *mxf = s->priv_data;
521  avio_write(s->pb, umid_ul, 13);
522  avio_wb24(s->pb, mxf->instance_number);
523  avio_write(s->pb, mxf->umid, 15);
524  avio_w8(s->pb, type);
525 }
526 
527 static void mxf_write_refs_count(AVIOContext *pb, int ref_count)
528 {
529  avio_wb32(pb, ref_count);
530  avio_wb32(pb, 16);
531 }
532 
533 static int klv_ber_length(uint64_t len)
534 {
535  if (len < 128)
536  return 1;
537  else
538  return (av_log2(len) >> 3) + 2;
539 }
540 
541 static int klv_encode_ber_length(AVIOContext *pb, uint64_t len)
542 {
543  // Determine the best BER size
544  int size;
545  if (len < 128) {
546  //short form
547  avio_w8(pb, len);
548  return 1;
549  }
550 
551  size = (av_log2(len) >> 3) + 1;
552 
553  // long form
554  avio_w8(pb, 0x80 + size);
555  while(size) {
556  size--;
557  avio_w8(pb, len >> 8 * size & 0xff);
558  }
559  return 0;
560 }
561 
563 {
564  avio_w8(pb, 0x80 + 3);
565  avio_wb24(pb, len);
566 }
567 
568 static void klv_encode_ber9_length(AVIOContext *pb, uint64_t len)
569 {
570  avio_w8(pb, 0x80 + 8);
571  avio_wb64(pb, len);
572 }
573 
574 /*
575  * Get essence container ul index
576  */
578 {
579  int i;
580  for (i = 0; mxf_essence_mappings[i].id; i++)
581  if (mxf_essence_mappings[i].id == id)
582  return mxf_essence_mappings[i].index;
583  return -1;
584 }
585 
587 {
588  MXFContext *mxf = s->priv_data;
589  AVIOContext *pb = s->pb;
590  int local_tag_number, i = 0;
591 
592  local_tag_number = FF_ARRAY_ELEMS(mxf_local_tag_batch);
593  local_tag_number += mxf->store_user_comments * FF_ARRAY_ELEMS(mxf_user_comments_local_tag);
594 
595  avio_write(pb, primer_pack_key, 16);
596  klv_encode_ber_length(pb, local_tag_number * 18 + 8);
597 
598  avio_wb32(pb, local_tag_number); // local_tag num
599  avio_wb32(pb, 18); // item size, always 18 according to the specs
600 
601  for (i = 0; i < FF_ARRAY_ELEMS(mxf_local_tag_batch); i++) {
602  avio_wb16(pb, mxf_local_tag_batch[i].local_tag);
603  avio_write(pb, mxf_local_tag_batch[i].uid, 16);
604  }
605  if (mxf->store_user_comments)
606  for (i = 0; i < FF_ARRAY_ELEMS(mxf_user_comments_local_tag); i++) {
607  avio_wb16(pb, mxf_user_comments_local_tag[i].local_tag);
608  avio_write(pb, mxf_user_comments_local_tag[i].uid, 16);
609  }
610 }
611 
612 static void mxf_write_local_tag(AVIOContext *pb, int size, int tag)
613 {
614  avio_wb16(pb, tag);
615  avio_wb16(pb, size);
616 }
617 
618 static void mxf_write_metadata_key(AVIOContext *pb, unsigned int value)
619 {
621  avio_wb24(pb, value);
622 }
623 
625 {
626  int i;
627 
628  for (i = 0; i < s->nb_streams; i++) {
629  AVStream *st = s->streams[i];
630  av_freep(&st->priv_data);
631  }
632 }
633 
635 {
637  while (uls->uid[0]) {
638  if (type == uls->id)
639  break;
640  uls++;
641  }
642  return uls;
643 }
644 
645 //one EC -> one descriptor. N ECs -> MultipleDescriptor + N descriptors
646 #define DESCRIPTOR_COUNT(essence_container_count) \
647  (essence_container_count > 1 ? essence_container_count + 1 : essence_container_count)
648 
650 {
651  MXFContext *c = s->priv_data;
652  AVIOContext *pb = s->pb;
653  int i;
654 
656  av_log(s,AV_LOG_DEBUG, "essence container count:%d\n", c->essence_container_count);
657  for (i = 0; i < c->essence_container_count; i++) {
658  MXFStreamContext *sc = s->streams[i]->priv_data;
659  avio_write(pb, mxf_essence_container_uls[sc->index].container_ul, 16);
660  }
661 
662  if (c->essence_container_count > 1)
663  avio_write(pb, multiple_desc_ul, 16);
664 }
665 
667 {
668  MXFContext *mxf = s->priv_data;
669  AVIOContext *pb = s->pb;
670 
671  mxf_write_metadata_key(pb, 0x012f00);
672  PRINT_KEY(s, "preface key", pb->buf_ptr - 16);
674 
675  // write preface set uid
676  mxf_write_local_tag(pb, 16, 0x3C0A);
677  mxf_write_uuid(pb, Preface, 0);
678  PRINT_KEY(s, "preface uid", pb->buf_ptr - 16);
679 
680  // last modified date
681  mxf_write_local_tag(pb, 8, 0x3B02);
682  avio_wb64(pb, mxf->timestamp);
683 
684  // write version
685  mxf_write_local_tag(pb, 2, 0x3B05);
686  avio_wb16(pb, 258); // v1.2
687 
688  // write identification_refs
689  mxf_write_local_tag(pb, 16 + 8, 0x3B06);
690  mxf_write_refs_count(pb, 1);
692 
693  // write content_storage_refs
694  mxf_write_local_tag(pb, 16, 0x3B03);
696 
697  // operational pattern
698  mxf_write_local_tag(pb, 16, 0x3B09);
699  if (s->oformat == &ff_mxf_opatom_muxer)
700  avio_write(pb, opatom_ul, 16);
701  else
702  avio_write(pb, op1a_ul, 16);
703 
704  // write essence_container_refs
705  mxf_write_local_tag(pb, 8 + 16LL * DESCRIPTOR_COUNT(mxf->essence_container_count), 0x3B0A);
707 
708  // write dm_scheme_refs
709  mxf_write_local_tag(pb, 8, 0x3B0B);
710  avio_wb64(pb, 0);
711 }
712 
713 /*
714  * Returns the length of the UTF-16 string, in 16-bit characters, that would result
715  * from decoding the utf-8 string.
716  */
717 static uint64_t mxf_utf16len(const char *utf8_str)
718 {
719  const uint8_t *q = utf8_str;
720  uint64_t size = 0;
721  while (*q) {
722  uint32_t ch;
723  GET_UTF8(ch, *q++, goto invalid;)
724  if (ch < 0x10000)
725  size++;
726  else
727  size += 2;
728  continue;
729 invalid:
730  av_log(NULL, AV_LOG_ERROR, "Invalid UTF8 sequence in mxf_utf16len\n\n");
731  }
732  size += 1;
733  return size;
734 }
735 
736 /*
737  * Returns the calculated length a local tag containing an utf-8 string as utf-16
738  */
739 static int mxf_utf16_local_tag_length(const char *utf8_str)
740 {
741  uint64_t size;
742 
743  if (!utf8_str)
744  return 0;
745 
746  size = mxf_utf16len(utf8_str);
747  if (size >= UINT16_MAX/2) {
748  av_log(NULL, AV_LOG_ERROR, "utf16 local tag size %"PRIx64" invalid (too large), ignoring\n", size);
749  return 0;
750  }
751 
752  return 4 + size * 2;
753 }
754 
755 /*
756  * Write a local tag containing an utf-8 string as utf-16
757  */
758 static void mxf_write_local_tag_utf16(AVIOContext *pb, int tag, const char *value)
759 {
760  uint64_t size = mxf_utf16len(value);
761 
762  if (size >= UINT16_MAX/2) {
763  av_log(NULL, AV_LOG_ERROR, "utf16 local tag size %"PRIx64" invalid (too large), ignoring\n", size);
764  return;
765  }
766 
767  mxf_write_local_tag(pb, size*2, tag);
768  avio_put_str16be(pb, value);
769 }
770 
772 {
773  MXFContext *mxf = s->priv_data;
774  AVIOContext *pb = s->pb;
775  const char *company = "FFmpeg";
776  const char *product = s->oformat != &ff_mxf_opatom_muxer ? "OP1a Muxer" : "OPAtom Muxer";
777  const char *version;
778  int length;
779 
780  mxf_write_metadata_key(pb, 0x013000);
781  PRINT_KEY(s, "identification key", pb->buf_ptr - 16);
782 
783  version = s->flags & AVFMT_FLAG_BITEXACT ?
785  length = 72 + mxf_utf16_local_tag_length(company) +
786  mxf_utf16_local_tag_length(product) +
788  klv_encode_ber_length(pb, length);
789 
790  // write uid
791  mxf_write_local_tag(pb, 16, 0x3C0A);
793  PRINT_KEY(s, "identification uid", pb->buf_ptr - 16);
794 
795  // write generation uid
796  mxf_write_local_tag(pb, 16, 0x3C09);
798  mxf_write_local_tag_utf16(pb, 0x3C01, company); // Company Name
799  mxf_write_local_tag_utf16(pb, 0x3C02, product); // Product Name
800  mxf_write_local_tag_utf16(pb, 0x3C04, version); // Version String
801 
802  // write product uid
803  mxf_write_local_tag(pb, 16, 0x3C05);
805 
806  // modification date
807  mxf_write_local_tag(pb, 8, 0x3C06);
808  avio_wb64(pb, mxf->timestamp);
809 }
810 
812 {
813  AVIOContext *pb = s->pb;
814 
815  mxf_write_metadata_key(pb, 0x011800);
816  PRINT_KEY(s, "content storage key", pb->buf_ptr - 16);
817  klv_encode_ber_length(pb, 92);
818 
819  // write uid
820  mxf_write_local_tag(pb, 16, 0x3C0A);
822  PRINT_KEY(s, "content storage uid", pb->buf_ptr - 16);
823 
824  // write package reference
825  mxf_write_local_tag(pb, 16 * 2 + 8, 0x1901);
826  mxf_write_refs_count(pb, 2);
829 
830  // write essence container data
831  mxf_write_local_tag(pb, 8 + 16, 0x1902);
832  mxf_write_refs_count(pb, 1);
834 }
835 
837 {
838  MXFContext *mxf = s->priv_data;
839  AVIOContext *pb = s->pb;
840  MXFStreamContext *sc = st->priv_data;
841 
842  mxf_write_metadata_key(pb, 0x013b00);
843  PRINT_KEY(s, "track key", pb->buf_ptr - 16);
844  klv_encode_ber_length(pb, 80);
845 
846  // write track uid
847  mxf_write_local_tag(pb, 16, 0x3C0A);
848  mxf_write_uuid(pb, type == MaterialPackage ? Track : Track + TypeBottom, st->index);
849  PRINT_KEY(s, "track uid", pb->buf_ptr - 16);
850 
851  // write track id
852  mxf_write_local_tag(pb, 4, 0x4801);
853  avio_wb32(pb, st->index+2);
854 
855  // write track number
856  mxf_write_local_tag(pb, 4, 0x4804);
857  if (type == MaterialPackage)
858  avio_wb32(pb, 0); // track number of material package is 0
859  else
860  avio_write(pb, sc->track_essence_element_key + 12, 4);
861 
862  // write edit rate
863  mxf_write_local_tag(pb, 8, 0x4B01);
864 
865  if (st == mxf->timecode_track && s->oformat == &ff_mxf_opatom_muxer) {
866  avio_wb32(pb, mxf->tc.rate.num);
867  avio_wb32(pb, mxf->tc.rate.den);
868  } else {
869  avio_wb32(pb, mxf->time_base.den);
870  avio_wb32(pb, mxf->time_base.num);
871  }
872 
873  // write origin
874  mxf_write_local_tag(pb, 8, 0x4B02);
875  avio_wb64(pb, 0);
876 
877  // write sequence refs
878  mxf_write_local_tag(pb, 16, 0x4803);
880 }
881 
882 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 };
883 
885 {
886  MXFContext *mxf = s->priv_data;
887  AVIOContext *pb = s->pb;
888 
889  // find data define uls
890  mxf_write_local_tag(pb, 16, 0x0201);
891  if (st == mxf->timecode_track)
893  else {
894  const MXFCodecUL *data_def_ul = mxf_get_data_definition_ul(st->codecpar->codec_type);
895  avio_write(pb, data_def_ul->uid, 16);
896  }
897 
898  // write duration
899  mxf_write_local_tag(pb, 8, 0x0202);
900 
901  if (st != mxf->timecode_track && s->oformat == &ff_mxf_opatom_muxer && st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
902  avio_wb64(pb, mxf->body_offset / mxf->edit_unit_byte_count);
903  } else {
904  avio_wb64(pb, mxf->duration);
905  }
906 }
907 
909 {
910  MXFContext *mxf = s->priv_data;
911  AVIOContext *pb = s->pb;
912  enum MXFMetadataSetType component;
913 
914  mxf_write_metadata_key(pb, 0x010f00);
915  PRINT_KEY(s, "sequence key", pb->buf_ptr - 16);
916  klv_encode_ber_length(pb, 80);
917 
918  mxf_write_local_tag(pb, 16, 0x3C0A);
920 
921  PRINT_KEY(s, "sequence uid", pb->buf_ptr - 16);
923 
924  // write structural component
925  mxf_write_local_tag(pb, 16 + 8, 0x1001);
926  mxf_write_refs_count(pb, 1);
927  if (st == mxf->timecode_track)
928  component = TimecodeComponent;
929  else
930  component = SourceClip;
931  if (type == SourcePackage)
932  component += TypeBottom;
933  mxf_write_uuid(pb, component, st->index);
934 }
935 
937 {
938  MXFContext *mxf = s->priv_data;
939  AVIOContext *pb = s->pb;
940 
941  mxf_write_metadata_key(pb, 0x011400);
942  klv_encode_ber_length(pb, 75);
943 
944  // UID
945  mxf_write_local_tag(pb, 16, 0x3C0A);
948 
950 
951  // Start Time Code
952  mxf_write_local_tag(pb, 8, 0x1501);
953  avio_wb64(pb, mxf->tc.start);
954 
955  // Rounded Time Code Base
956  mxf_write_local_tag(pb, 2, 0x1502);
957  avio_wb16(pb, mxf->timecode_base);
958 
959  // Drop Frame
960  mxf_write_local_tag(pb, 1, 0x1503);
961  avio_w8(pb, !!(mxf->tc.flags & AV_TIMECODE_FLAG_DROPFRAME));
962 }
963 
965 {
966  AVIOContext *pb = s->pb;
967  int i;
968 
969  mxf_write_metadata_key(pb, 0x011100);
970  PRINT_KEY(s, "sturctural component key", pb->buf_ptr - 16);
971  klv_encode_ber_length(pb, 108);
972 
973  // write uid
974  mxf_write_local_tag(pb, 16, 0x3C0A);
976 
977  PRINT_KEY(s, "structural component uid", pb->buf_ptr - 16);
979 
980  // write start_position
981  mxf_write_local_tag(pb, 8, 0x1201);
982  avio_wb64(pb, 0);
983 
984  // write source package uid, end of the reference
985  mxf_write_local_tag(pb, 32, 0x1101);
986  if (type == SourcePackage) {
987  for (i = 0; i < 4; i++)
988  avio_wb64(pb, 0);
989  } else
990  mxf_write_umid(s, 1);
991 
992  // write source track id
993  mxf_write_local_tag(pb, 4, 0x1102);
994  if (type == SourcePackage)
995  avio_wb32(pb, 0);
996  else
997  avio_wb32(pb, st->index+2);
998 }
999 
1001 {
1002  MXFContext *mxf = s->priv_data;
1003  AVIOContext *pb = s->pb;
1004  const uint8_t *ul;
1005  int i;
1006 
1007  mxf_write_metadata_key(pb, 0x014400);
1008  PRINT_KEY(s, "multiple descriptor key", pb->buf_ptr - 16);
1009  klv_encode_ber_length(pb, 64 + 16LL * s->nb_streams);
1010 
1011  mxf_write_local_tag(pb, 16, 0x3C0A);
1013  PRINT_KEY(s, "multi_desc uid", pb->buf_ptr - 16);
1014 
1015  // write sample rate
1016  mxf_write_local_tag(pb, 8, 0x3001);
1017  avio_wb32(pb, mxf->time_base.den);
1018  avio_wb32(pb, mxf->time_base.num);
1019 
1020  // write essence container ul
1021  mxf_write_local_tag(pb, 16, 0x3004);
1022  if (mxf->essence_container_count > 1)
1023  ul = multiple_desc_ul;
1024  else {
1025  MXFStreamContext *sc = s->streams[0]->priv_data;
1026  ul = mxf_essence_container_uls[sc->index].container_ul;
1027  }
1028  avio_write(pb, ul, 16);
1029 
1030  // write sub descriptor refs
1031  mxf_write_local_tag(pb, s->nb_streams * 16 + 8, 0x3F01);
1033  for (i = 0; i < s->nb_streams; i++)
1034  mxf_write_uuid(pb, SubDescriptor, i);
1035 }
1036 
1037 static void mxf_write_generic_desc(AVFormatContext *s, AVStream *st, const UID key, unsigned size)
1038 {
1039  MXFContext *mxf = s->priv_data;
1040  MXFStreamContext *sc = st->priv_data;
1041  AVIOContext *pb = s->pb;
1042 
1043  avio_write(pb, key, 16);
1044  klv_encode_ber4_length(pb, size+20+8+12+20);
1045 
1046  mxf_write_local_tag(pb, 16, 0x3C0A);
1048 
1049  mxf_write_local_tag(pb, 4, 0x3006);
1050  avio_wb32(pb, st->index+2);
1051 
1052  mxf_write_local_tag(pb, 8, 0x3001);
1053  if (s->oformat == &ff_mxf_d10_muxer) {
1054  avio_wb32(pb, mxf->time_base.den);
1055  avio_wb32(pb, mxf->time_base.num);
1056  } else {
1057  if (st->codecpar->codec_id == AV_CODEC_ID_PCM_S16LE ||
1059  avio_wb32(pb, st->codecpar->sample_rate);
1060  avio_wb32(pb, 1);
1061  } else {
1062  avio_wb32(pb, mxf->time_base.den);
1063  avio_wb32(pb, mxf->time_base.num);
1064  }
1065  }
1066 
1067  mxf_write_local_tag(pb, 16, 0x3004);
1068  avio_write(pb, mxf_essence_container_uls[sc->index].container_ul, 16);
1069 }
1070 
1071 static const UID mxf_mpegvideo_descriptor_key = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x51,0x00 };
1072 static const UID mxf_wav_descriptor_key = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x48,0x00 };
1073 static const UID mxf_aes3_descriptor_key = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x47,0x00 };
1074 static const UID mxf_cdci_descriptor_key = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0D,0x01,0x01,0x01,0x01,0x01,0x28,0x00 };
1075 static const UID mxf_generic_sound_descriptor_key = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0D,0x01,0x01,0x01,0x01,0x01,0x42,0x00 };
1076 
1077 static void mxf_write_cdci_common(AVFormatContext *s, AVStream *st, const UID key, unsigned size)
1078 {
1079  MXFStreamContext *sc = st->priv_data;
1080  AVIOContext *pb = s->pb;
1081  int stored_height = (st->codecpar->height+15)/16*16;
1082  int display_height;
1083  int f1, f2;
1084  unsigned desc_size = size+8+8+8+8+8+8+8+5+16+4+12+20+5;
1085  if (sc->interlaced && sc->field_dominance)
1086  desc_size += 5;
1087  if (sc->signal_standard)
1088  desc_size += 5;
1089 
1090  mxf_write_generic_desc(s, st, key, desc_size);
1091 
1092  mxf_write_local_tag(pb, 4, 0x3203);
1093  avio_wb32(pb, st->codecpar->width);
1094 
1095  mxf_write_local_tag(pb, 4, 0x3202);
1096  avio_wb32(pb, stored_height>>sc->interlaced);
1097 
1098  mxf_write_local_tag(pb, 4, 0x3209);
1099  avio_wb32(pb, st->codecpar->width);
1100 
1101  if (st->codecpar->height == 608) // PAL + VBI
1102  display_height = 576;
1103  else if (st->codecpar->height == 512) // NTSC + VBI
1104  display_height = 486;
1105  else
1106  display_height = st->codecpar->height;
1107 
1108  mxf_write_local_tag(pb, 4, 0x3208);
1109  avio_wb32(pb, display_height>>sc->interlaced);
1110 
1111  // presentation Y offset
1112  mxf_write_local_tag(pb, 4, 0x320B);
1113  avio_wb32(pb, (st->codecpar->height - display_height)>>sc->interlaced);
1114 
1115  // component depth
1116  mxf_write_local_tag(pb, 4, 0x3301);
1117  avio_wb32(pb, sc->component_depth);
1118 
1119  // horizontal subsampling
1120  mxf_write_local_tag(pb, 4, 0x3302);
1121  avio_wb32(pb, sc->h_chroma_sub_sample);
1122 
1123  // color siting
1124  mxf_write_local_tag(pb, 1, 0x3303);
1125  avio_w8(pb, sc->color_siting);
1126 
1127  if (sc->signal_standard) {
1128  mxf_write_local_tag(pb, 1, 0x3215);
1129  avio_w8(pb, sc->signal_standard);
1130  }
1131 
1132  // frame layout
1133  mxf_write_local_tag(pb, 1, 0x320C);
1134  avio_w8(pb, sc->interlaced);
1135 
1136  // video line map
1137  switch (st->codecpar->height) {
1138  case 576: f1 = 23; f2 = st->codecpar->codec_id == AV_CODEC_ID_DVVIDEO ? 335 : 336; break;
1139  case 608: f1 = 7; f2 = 320; break;
1140  case 480: f1 = 20; f2 = st->codecpar->codec_id == AV_CODEC_ID_DVVIDEO ? 285 : 283; break;
1141  case 512: f1 = 7; f2 = 270; break;
1142  case 720: f1 = 26; f2 = 0; break; // progressive
1143  case 1080: f1 = 21; f2 = 584; break;
1144  default: f1 = 0; f2 = 0; break;
1145  }
1146 
1147  if (!sc->interlaced) {
1148  f2 = 0;
1149  f1 *= 2;
1150  }
1151 
1152 
1153  mxf_write_local_tag(pb, 16, 0x320D);
1154  avio_wb32(pb, 2);
1155  avio_wb32(pb, 4);
1156  avio_wb32(pb, f1);
1157  avio_wb32(pb, f2);
1158 
1159  mxf_write_local_tag(pb, 8, 0x320E);
1160  avio_wb32(pb, sc->aspect_ratio.num);
1161  avio_wb32(pb, sc->aspect_ratio.den);
1162 
1163  mxf_write_local_tag(pb, 16, 0x3201);
1164  avio_write(pb, *sc->codec_ul, 16);
1165 
1166  if (sc->interlaced && sc->field_dominance) {
1167  mxf_write_local_tag(pb, 1, 0x3212);
1168  avio_w8(pb, sc->field_dominance);
1169  }
1170 
1171 }
1172 
1174 {
1176 }
1177 
1179 {
1180  AVIOContext *pb = s->pb;
1181  MXFStreamContext *sc = st->priv_data;
1182  int profile_and_level = (st->codecpar->profile<<4) | st->codecpar->level;
1183 
1184  if (st->codecpar->codec_id != AV_CODEC_ID_H264) {
1186 
1187  // bit rate
1188  mxf_write_local_tag(pb, 4, 0x8000);
1189  avio_wb32(pb, sc->video_bit_rate);
1190 
1191  // profile and level
1192  mxf_write_local_tag(pb, 1, 0x8007);
1193  if (!st->codecpar->profile)
1194  profile_and_level |= 0x80; // escape bit
1195  avio_w8(pb, profile_and_level);
1196  } else {
1198  }
1199 }
1200 
1201 static void mxf_write_generic_sound_common(AVFormatContext *s, AVStream *st, const UID key, unsigned size)
1202 {
1203  AVIOContext *pb = s->pb;
1204  MXFContext *mxf = s->priv_data;
1205  int show_warnings = !mxf->footer_partition_offset;
1206  int duration_size = 0;
1207 
1208  if (s->oformat == &ff_mxf_opatom_muxer)
1209  duration_size = 12;
1210 
1211  mxf_write_generic_desc(s, st, key, size+duration_size+5+12+8+8);
1212 
1213  if (duration_size > 0) {
1214  mxf_write_local_tag(pb, 8, 0x3002);
1215  avio_wb64(pb, mxf->body_offset / mxf->edit_unit_byte_count);
1216  }
1217 
1218  // audio locked
1219  mxf_write_local_tag(pb, 1, 0x3D02);
1220  avio_w8(pb, 1);
1221 
1222  // write audio sampling rate
1223  mxf_write_local_tag(pb, 8, 0x3D03);
1224  avio_wb32(pb, st->codecpar->sample_rate);
1225  avio_wb32(pb, 1);
1226 
1227  mxf_write_local_tag(pb, 4, 0x3D07);
1228  if (mxf->channel_count == -1) {
1229  if (show_warnings && (s->oformat == &ff_mxf_d10_muxer) && (st->codecpar->channels != 4) && (st->codecpar->channels != 8))
1230  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");
1231  avio_wb32(pb, st->codecpar->channels);
1232  } else if (s->oformat == &ff_mxf_d10_muxer) {
1233  if (show_warnings && (mxf->channel_count < st->codecpar->channels))
1234  av_log(s, AV_LOG_WARNING, "d10_channelcount < actual number of audio channels : some channels will be discarded\n");
1235  if (show_warnings && (mxf->channel_count != 4) && (mxf->channel_count != 8))
1236  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");
1237  avio_wb32(pb, mxf->channel_count);
1238  } else {
1239  avio_wb32(pb, st->codecpar->channels);
1240  }
1241 
1242  mxf_write_local_tag(pb, 4, 0x3D01);
1244 }
1245 
1246 static void mxf_write_wav_common(AVFormatContext *s, AVStream *st, const UID key, unsigned size)
1247 {
1248  AVIOContext *pb = s->pb;
1249 
1250  mxf_write_generic_sound_common(s, st, key, size+6+8);
1251 
1252  mxf_write_local_tag(pb, 2, 0x3D0A);
1253  avio_wb16(pb, st->codecpar->block_align);
1254 
1255  // avg bytes per sec
1256  mxf_write_local_tag(pb, 4, 0x3D09);
1258 }
1259 
1261 {
1263 }
1264 
1266 {
1268 }
1269 
1271 {
1273 }
1274 
1275 static const uint8_t mxf_indirect_value_utf16le[] = { 0x4c,0x00,0x02,0x10,0x01,0x00,0x00,0x00,0x00,0x06,0x0e,0x2b,0x34,0x01,0x04,0x01,0x01 };
1276 
1277 static int mxf_write_tagged_value(AVFormatContext *s, const char* name, const char* value)
1278 {
1279  MXFContext *mxf = s->priv_data;
1280  AVIOContext *pb = s->pb;
1281  int name_size = mxf_utf16_local_tag_length(name);
1282  int indirect_value_size = 13 + mxf_utf16_local_tag_length(value);
1283 
1284  if (!name_size || indirect_value_size == 13)
1285  return 1;
1286 
1287  mxf_write_metadata_key(pb, 0x013f00);
1288  klv_encode_ber_length(pb, 24 + name_size + indirect_value_size);
1289 
1290  // write instance UID
1291  mxf_write_local_tag(pb, 16, 0x3C0A);
1293 
1294  // write name
1295  mxf_write_local_tag_utf16(pb, 0x5001, name); // Name
1296 
1297  // write indirect value
1298  mxf_write_local_tag(pb, indirect_value_size, 0x5003);
1300  avio_put_str16le(pb, value);
1301 
1302  mxf->tagged_value_count++;
1303  return 0;
1304 }
1305 
1307 {
1308  MXFContext *mxf = s->priv_data;
1309  AVDictionaryEntry *t = NULL;
1310  int count = 0;
1311 
1312  while ((t = av_dict_get(m, "comment_", t, AV_DICT_IGNORE_SUFFIX))) {
1313  if (mxf->tagged_value_count >= UINT16_MAX) {
1314  av_log(s, AV_LOG_ERROR, "too many tagged values, ignoring remaining\n");
1315  return count;
1316  }
1317 
1318  if (mxf_write_tagged_value(s, t->key + 8, t->value) == 0)
1319  count++;
1320  }
1321  return count;
1322 }
1323 
1324 static void mxf_write_package(AVFormatContext *s, enum MXFMetadataSetType type, const char *package_name)
1325 {
1326  MXFContext *mxf = s->priv_data;
1327  AVIOContext *pb = s->pb;
1328  int i, track_count = s->nb_streams+1;
1329  int name_size = mxf_utf16_local_tag_length(package_name);
1330  int user_comment_count = 0;
1331 
1332  if (type == MaterialPackage) {
1333  if (mxf->store_user_comments)
1334  user_comment_count = mxf_write_user_comments(s, s->metadata);
1335  mxf_write_metadata_key(pb, 0x013600);
1336  PRINT_KEY(s, "Material Package key", pb->buf_ptr - 16);
1337  klv_encode_ber_length(pb, 92 + name_size + (16*track_count) + (16*user_comment_count) + 12LL*mxf->store_user_comments);
1338  } else {
1339  mxf_write_metadata_key(pb, 0x013700);
1340  PRINT_KEY(s, "Source Package key", pb->buf_ptr - 16);
1341  klv_encode_ber_length(pb, 112 + name_size + (16*track_count) + 12LL*mxf->store_user_comments); // 20 bytes length for descriptor reference
1342  }
1343 
1344  // write uid
1345  mxf_write_local_tag(pb, 16, 0x3C0A);
1346  mxf_write_uuid(pb, type, 0);
1347  av_log(s, AV_LOG_DEBUG, "package type:%d\n", type);
1348  PRINT_KEY(s, "package uid", pb->buf_ptr - 16);
1349 
1350  // write package umid
1351  mxf_write_local_tag(pb, 32, 0x4401);
1352  mxf_write_umid(s, type == SourcePackage);
1353  PRINT_KEY(s, "package umid second part", pb->buf_ptr - 16);
1354 
1355  // package name
1356  if (name_size)
1357  mxf_write_local_tag_utf16(pb, 0x4402, package_name);
1358 
1359  // package creation date
1360  mxf_write_local_tag(pb, 8, 0x4405);
1361  avio_wb64(pb, mxf->timestamp);
1362 
1363  // package modified date
1364  mxf_write_local_tag(pb, 8, 0x4404);
1365  avio_wb64(pb, mxf->timestamp);
1366 
1367  // write track refs
1368  mxf_write_local_tag(pb, track_count*16 + 8, 0x4403);
1369  mxf_write_refs_count(pb, track_count);
1370  mxf_write_uuid(pb, type == MaterialPackage ? Track :
1371  Track + TypeBottom, -1); // timecode track
1372  for (i = 0; i < s->nb_streams; i++)
1373  mxf_write_uuid(pb, type == MaterialPackage ? Track : Track + TypeBottom, i);
1374 
1375  // write user comment refs
1376  if (mxf->store_user_comments) {
1377  mxf_write_local_tag(pb, user_comment_count*16 + 8, 0x4406);
1378  mxf_write_refs_count(pb, user_comment_count);
1379  for (i = 0; i < user_comment_count; i++)
1380  mxf_write_uuid(pb, TaggedValue, mxf->tagged_value_count - user_comment_count + i);
1381  }
1382 
1383  // write multiple descriptor reference
1384  if (type == SourcePackage) {
1385  mxf_write_local_tag(pb, 16, 0x4701);
1386  if (s->nb_streams > 1) {
1389  } else
1390  mxf_write_uuid(pb, SubDescriptor, 0);
1391  }
1392 
1393  // write timecode track
1394  mxf_write_track(s, mxf->timecode_track, type);
1395  mxf_write_sequence(s, mxf->timecode_track, type);
1397 
1398  for (i = 0; i < s->nb_streams; i++) {
1399  AVStream *st = s->streams[i];
1400  mxf_write_track(s, st, type);
1401  mxf_write_sequence(s, st, type);
1402  mxf_write_structural_component(s, st, type);
1403 
1404  if (type == SourcePackage) {
1405  MXFStreamContext *sc = st->priv_data;
1406  mxf_essence_container_uls[sc->index].write_desc(s, st);
1407  }
1408  }
1409 }
1410 
1412 {
1413  AVIOContext *pb = s->pb;
1414 
1415  mxf_write_metadata_key(pb, 0x012300);
1416  klv_encode_ber_length(pb, 72);
1417 
1418  mxf_write_local_tag(pb, 16, 0x3C0A); // Instance UID
1420 
1421  mxf_write_local_tag(pb, 32, 0x2701); // Linked Package UID
1422  mxf_write_umid(s, 1);
1423 
1424  mxf_write_local_tag(pb, 4, 0x3F07); // BodySID
1425  avio_wb32(pb, 1);
1426 
1427  mxf_write_local_tag(pb, 4, 0x3F06); // IndexSID
1428  avio_wb32(pb, 2);
1429 
1430  return 0;
1431 }
1432 
1434 {
1435  const char *material_package_name = NULL;
1436  const char *file_package_name = NULL;
1437  AVDictionaryEntry *entry = NULL;
1438  AVStream *st = NULL;
1439  int i;
1440 
1441  if (entry = av_dict_get(s->metadata, "material_package_name", NULL, 0))
1442  material_package_name = entry->value;
1443 
1444  if (entry = av_dict_get(s->metadata, "file_package_name", NULL, 0)) {
1445  file_package_name = entry->value;
1446  } else {
1447  /* check if any of the streams contain a file_package_name */
1448  for (i = 0; i < s->nb_streams; i++) {
1449  st = s->streams[i];
1450  if (entry = av_dict_get(st->metadata, "file_package_name", NULL, 0)) {
1451  file_package_name = entry->value;
1452  break;
1453  }
1454  }
1455  }
1456 
1457  mxf_write_preface(s);
1460  mxf_write_package(s, MaterialPackage, material_package_name);
1461  mxf_write_package(s, SourcePackage, file_package_name);
1463  return 0;
1464 }
1465 
1466 static unsigned klv_fill_size(uint64_t size)
1467 {
1468  unsigned pad = KAG_SIZE - (size & (KAG_SIZE-1));
1469  if (pad < 20) // smallest fill item possible
1470  return pad + KAG_SIZE;
1471  else
1472  return pad & (KAG_SIZE-1);
1473 }
1474 
1476 {
1477  MXFContext *mxf = s->priv_data;
1478  AVIOContext *pb = s->pb;
1479  int i, j, temporal_reordering = 0;
1480  int key_index = mxf->last_key_index;
1481 
1482  av_log(s, AV_LOG_DEBUG, "edit units count %d\n", mxf->edit_units_count);
1483 
1484  if (!mxf->edit_units_count && !mxf->edit_unit_byte_count)
1485  return;
1486 
1488 
1489  if (mxf->edit_unit_byte_count) {
1490  klv_encode_ber_length(pb, 80);
1491  } else {
1492  klv_encode_ber_length(pb, 85 + 12+(s->nb_streams+1LL)*6 +
1493  12+mxf->edit_units_count*(11+mxf->slice_count*4LL));
1494  }
1495 
1496  // instance id
1497  mxf_write_local_tag(pb, 16, 0x3C0A);
1499 
1500  // index edit rate
1501  mxf_write_local_tag(pb, 8, 0x3F0B);
1502  avio_wb32(pb, mxf->time_base.den);
1503  avio_wb32(pb, mxf->time_base.num);
1504 
1505  // index start position
1506  mxf_write_local_tag(pb, 8, 0x3F0C);
1508 
1509  // index duration
1510  mxf_write_local_tag(pb, 8, 0x3F0D);
1511  if (mxf->edit_unit_byte_count)
1512  avio_wb64(pb, 0); // index table covers whole container
1513  else
1514  avio_wb64(pb, mxf->edit_units_count);
1515 
1516  // edit unit byte count
1517  mxf_write_local_tag(pb, 4, 0x3F05);
1518  avio_wb32(pb, mxf->edit_unit_byte_count);
1519 
1520  // index sid
1521  mxf_write_local_tag(pb, 4, 0x3F06);
1522  avio_wb32(pb, 2);
1523 
1524  // body sid
1525  mxf_write_local_tag(pb, 4, 0x3F07);
1526  avio_wb32(pb, 1);
1527 
1528  if (!mxf->edit_unit_byte_count) {
1529  // real slice count - 1
1530  mxf_write_local_tag(pb, 1, 0x3F08);
1531  avio_w8(pb, mxf->slice_count);
1532 
1533  // delta entry array
1534  mxf_write_local_tag(pb, 8 + (s->nb_streams+1)*6, 0x3F09);
1535  avio_wb32(pb, s->nb_streams+1); // num of entries
1536  avio_wb32(pb, 6); // size of one entry
1537  // write system item delta entry
1538  avio_w8(pb, 0);
1539  avio_w8(pb, 0); // slice entry
1540  avio_wb32(pb, 0); // element delta
1541  for (i = 0; i < s->nb_streams; i++) {
1542  AVStream *st = s->streams[i];
1543  MXFStreamContext *sc = st->priv_data;
1544  avio_w8(pb, sc->temporal_reordering);
1545  if (sc->temporal_reordering)
1546  temporal_reordering = 1;
1547  if (i == 0) { // video track
1548  avio_w8(pb, 0); // slice number
1549  avio_wb32(pb, KAG_SIZE); // system item size including klv fill
1550  } else { // audio track
1551  unsigned audio_frame_size = sc->aic.samples[0]*sc->aic.sample_size;
1552  audio_frame_size += klv_fill_size(audio_frame_size);
1553  avio_w8(pb, 1);
1554  avio_wb32(pb, (i-1)*audio_frame_size); // element delta
1555  }
1556  }
1557 
1558  mxf_write_local_tag(pb, 8 + mxf->edit_units_count*(11+mxf->slice_count*4), 0x3F0A);
1559  avio_wb32(pb, mxf->edit_units_count); // num of entries
1560  avio_wb32(pb, 11+mxf->slice_count*4); // size of one entry
1561 
1562  for (i = 0; i < mxf->edit_units_count; i++) {
1563  int temporal_offset = 0;
1564 
1565  if (!(mxf->index_entries[i].flags & 0x33)) { // I-frame
1566  mxf->last_key_index = key_index;
1567  key_index = i;
1568  }
1569 
1570  if (temporal_reordering) {
1571  int pic_num_in_gop = i - key_index;
1572  if (pic_num_in_gop != mxf->index_entries[i].temporal_ref) {
1573  for (j = key_index; j < mxf->edit_units_count; j++) {
1574  if (pic_num_in_gop == mxf->index_entries[j].temporal_ref)
1575  break;
1576  }
1577  if (j == mxf->edit_units_count)
1578  av_log(s, AV_LOG_WARNING, "missing frames\n");
1579  temporal_offset = j - key_index - pic_num_in_gop;
1580  }
1581  }
1582  avio_w8(pb, temporal_offset);
1583 
1584  if ((mxf->index_entries[i].flags & 0x30) == 0x30) { // back and forward prediction
1585  avio_w8(pb, mxf->last_key_index - i);
1586  } else {
1587  avio_w8(pb, key_index - i); // key frame offset
1588  if ((mxf->index_entries[i].flags & 0x20) == 0x20) // only forward
1589  mxf->last_key_index = key_index;
1590  }
1591 
1592  if (!(mxf->index_entries[i].flags & 0x33) && // I-frame
1593  mxf->index_entries[i].flags & 0x40 && !temporal_offset)
1594  mxf->index_entries[i].flags |= 0x80; // random access
1595  avio_w8(pb, mxf->index_entries[i].flags);
1596  // stream offset
1597  avio_wb64(pb, mxf->index_entries[i].offset);
1598  if (s->nb_streams > 1)
1599  avio_wb32(pb, mxf->index_entries[i].slice_offset);
1600  }
1601 
1602  mxf->last_key_index = key_index - mxf->edit_units_count;
1604  mxf->edit_units_count = 0;
1605  }
1606 }
1607 
1609 {
1610  unsigned pad = klv_fill_size(avio_tell(s->pb));
1611  if (pad) {
1612  avio_write(s->pb, klv_fill_key, 16);
1613  pad -= 16 + 4;
1614  klv_encode_ber4_length(s->pb, pad);
1615  ffio_fill(s->pb, 0, pad);
1616  av_assert1(!(avio_tell(s->pb) & (KAG_SIZE-1)));
1617  }
1618 }
1619 
1620 static int mxf_write_partition(AVFormatContext *s, int bodysid,
1621  int indexsid,
1622  const uint8_t *key, int write_metadata)
1623 {
1624  MXFContext *mxf = s->priv_data;
1625  AVIOContext *pb = s->pb;
1626  int64_t header_byte_count_offset;
1627  unsigned index_byte_count = 0;
1628  uint64_t partition_offset = avio_tell(pb);
1629  int err;
1630 
1631  if (!mxf->edit_unit_byte_count && mxf->edit_units_count)
1632  index_byte_count = 85 + 12+(s->nb_streams+1)*6 +
1633  12+mxf->edit_units_count*(11+mxf->slice_count*4);
1634  else if (mxf->edit_unit_byte_count && indexsid)
1635  index_byte_count = 80;
1636 
1637  if (index_byte_count) {
1638  // add encoded ber length
1639  index_byte_count += 16 + klv_ber_length(index_byte_count);
1640  index_byte_count += klv_fill_size(index_byte_count);
1641  }
1642 
1643  if (key && !memcmp(key, body_partition_key, 16)) {
1645  sizeof(*mxf->body_partition_offset))) < 0) {
1646  mxf->body_partitions_count = 0;
1647  return err;
1648  }
1649  mxf->body_partition_offset[mxf->body_partitions_count++] = partition_offset;
1650  }
1651 
1652  // write klv
1653  if (key)
1654  avio_write(pb, key, 16);
1655  else
1656  avio_write(pb, body_partition_key, 16);
1657 
1659 
1660  // write partition value
1661  avio_wb16(pb, 1); // majorVersion
1662  avio_wb16(pb, 2); // minorVersion
1663  avio_wb32(pb, KAG_SIZE); // KAGSize
1664 
1665  avio_wb64(pb, partition_offset); // ThisPartition
1666 
1667  if (key && !memcmp(key, body_partition_key, 16) && mxf->body_partitions_count > 1)
1668  avio_wb64(pb, mxf->body_partition_offset[mxf->body_partitions_count-2]); // PreviousPartition
1669  else if (key && !memcmp(key, footer_partition_key, 16) && mxf->body_partitions_count)
1670  avio_wb64(pb, mxf->body_partition_offset[mxf->body_partitions_count-1]); // PreviousPartition
1671  else
1672  avio_wb64(pb, 0);
1673 
1674  avio_wb64(pb, mxf->footer_partition_offset); // footerPartition
1675 
1676  // set offset
1677  header_byte_count_offset = avio_tell(pb);
1678  avio_wb64(pb, 0); // headerByteCount, update later
1679 
1680  // indexTable
1681  avio_wb64(pb, index_byte_count); // indexByteCount
1682  avio_wb32(pb, index_byte_count ? indexsid : 0); // indexSID
1683 
1684  // BodyOffset
1685  if (bodysid && mxf->edit_units_count && mxf->body_partitions_count && s->oformat != &ff_mxf_opatom_muxer)
1686  avio_wb64(pb, mxf->body_offset);
1687  else
1688  avio_wb64(pb, 0);
1689 
1690  avio_wb32(pb, bodysid); // bodySID
1691 
1692  // operational pattern
1693  if (s->oformat == &ff_mxf_opatom_muxer)
1694  avio_write(pb, opatom_ul, 16);
1695  else
1696  avio_write(pb, op1a_ul, 16);
1697 
1698  // essence container
1700 
1701  if (write_metadata) {
1702  // mark the start of the headermetadata and calculate metadata size
1703  int64_t pos, start;
1704  unsigned header_byte_count;
1705 
1706  mxf_write_klv_fill(s);
1707  start = avio_tell(s->pb);
1710  pos = avio_tell(s->pb);
1711  header_byte_count = pos - start + klv_fill_size(pos);
1712 
1713  // update header_byte_count
1714  avio_seek(pb, header_byte_count_offset, SEEK_SET);
1715  avio_wb64(pb, header_byte_count);
1716  avio_seek(pb, pos, SEEK_SET);
1717  }
1718 
1719  if(key)
1720  avio_flush(pb);
1721 
1722  return 0;
1723 }
1724 
1726 AVPacket *pkt)
1727 {
1728  MXFContext *mxf = s->priv_data;
1729  MXFStreamContext *sc = st->priv_data;
1730  int i, cid;
1731  uint8_t* header_cid;
1732  int frame_size = 0;
1733 
1734  if (mxf->header_written)
1735  return 1;
1736 
1737  if (pkt->size < 43)
1738  return -1;
1739 
1740  header_cid = pkt->data + 0x28;
1741  cid = header_cid[0] << 24 | header_cid[1] << 16 | header_cid[2] << 8 | header_cid[3];
1742 
1743  if ((frame_size = avpriv_dnxhd_get_frame_size(cid)) < 0)
1744  return -1;
1745  if ((sc->interlaced = avpriv_dnxhd_get_interlaced(cid)) < 0)
1746  return AVERROR_INVALIDDATA;
1747 
1748  switch (cid) {
1749  case 1235:
1751  sc->component_depth = 10;
1752  break;
1753  case 1237:
1755  break;
1756  case 1238:
1758  break;
1759  case 1241:
1761  sc->component_depth = 10;
1762  break;
1763  case 1242:
1765  break;
1766  case 1243:
1768  break;
1769  case 1250:
1771  sc->component_depth = 10;
1772  break;
1773  case 1251:
1775  break;
1776  case 1252:
1778  break;
1779  case 1253:
1781  break;
1782  default:
1783  return -1;
1784  }
1785 
1786  sc->codec_ul = &mxf_essence_container_uls[sc->index].codec_ul;
1787  sc->aspect_ratio = (AVRational){ 16, 9 };
1788 
1789  if (s->oformat == &ff_mxf_opatom_muxer) {
1791  return 1;
1792  }
1793 
1795  for (i = 0; i < s->nb_streams; i++) {
1796  AVStream *st = s->streams[i];
1797  MXFStreamContext *sc = st->priv_data;
1798  if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
1799  mxf->edit_unit_byte_count += 16 + 4 + sc->aic.samples[0]*sc->aic.sample_size;
1801  } else if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
1802  mxf->edit_unit_byte_count += 16 + 4 + frame_size;
1804  }
1805  }
1806 
1807  return 1;
1808 }
1809 
1811 {
1812  MXFContext *mxf = s->priv_data;
1813  MXFStreamContext *sc = st->priv_data;
1814  uint8_t *vs_pack, *vsc_pack;
1815  int i, ul_index, frame_size, stype, pal;
1816  const AVDVProfile *profile;
1817 
1818  if (mxf->header_written)
1819  return 1;
1820 
1821  // Check for minimal frame size
1822  if (pkt->size < 120000)
1823  return -1;
1824 
1825  profile = av_dv_frame_profile(NULL, pkt->data, pkt->size);
1826 
1827  vs_pack = pkt->data + 80*5 + 48;
1828  vsc_pack = pkt->data + 80*5 + 53;
1829  stype = vs_pack[3] & 0x1f;
1830  pal = (vs_pack[3] >> 5) & 0x1;
1831 
1832  if ((vsc_pack[2] & 0x07) == 0x02) {
1833  sc->aspect_ratio = (AVRational){ 16, 9 };
1834  } else {
1835  sc->aspect_ratio = (AVRational){ 4, 3 };
1836  }
1837 
1838  sc->interlaced = (vsc_pack[3] >> 4) & 0x01;
1839  // TODO: fix dv encoder to set proper FF/FS value in VSC pack
1840  // and set field dominance accordingly
1841  // av_log(s, AV_LOG_DEBUG, "DV vsc pack ff/ss = %x\n", vsc_pack[2] >> 6);
1842 
1843  switch (stype) {
1844  case 0x18: // DV100 720p
1845  ul_index = INDEX_DV100_720_50 + pal;
1846  frame_size = pal ? 288000 : 240000;
1847  if (sc->interlaced) {
1848  av_log(s, AV_LOG_ERROR, "source marked as interlaced but codec profile is progressive\n");
1849  sc->interlaced = 0;
1850  }
1851  break;
1852  case 0x14: // DV100 1080i
1853  ul_index = INDEX_DV100_1080_50 + pal;
1854  frame_size = pal ? 576000 : 480000;
1855  break;
1856  case 0x04: // DV50
1857  ul_index = INDEX_DV50_525_60 + pal;
1858  frame_size = pal ? 288000 : 240000;
1859  break;
1860  default: // DV25
1861  if (profile && profile->pix_fmt == AV_PIX_FMT_YUV420P && pal) {
1862  ul_index = INDEX_DV25_525_60_IEC + pal;
1863  frame_size = pal ? 144000 : 120000;
1864  break;
1865  }
1866  ul_index = INDEX_DV25_525_60 + pal;
1867  frame_size = pal ? 144000 : 120000;
1868  }
1869 
1870  sc->index = ul_index;
1871  sc->codec_ul = &mxf_essence_container_uls[sc->index].codec_ul;
1872 
1873  if(s->oformat == &ff_mxf_opatom_muxer) {
1875  return 1;
1876  }
1877 
1879  for (i = 0; i < s->nb_streams; i++) {
1880  AVStream *st = s->streams[i];
1881  MXFStreamContext *sc = st->priv_data;
1882  if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
1883  mxf->edit_unit_byte_count += 16 + 4 + sc->aic.samples[0]*sc->aic.sample_size;
1885  } else if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
1886  mxf->edit_unit_byte_count += 16 + 4 + frame_size;
1888  }
1889  }
1890 
1891  return 1;
1892 }
1893 
1894 static const struct {
1897  int profile;
1899 } mxf_h264_codec_uls[] = {
1900  {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x31,0x11,0x01 }, 0, 66, 0 }, // AVC Baseline, Unconstrained Coding
1901  {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x32,0x20,0x01 }, 0, 110, 0 }, // AVC High 10 Intra
1902  {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x32,0x21,0x01 }, 232960, 0, 1 }, // AVC Intra 50 1080i60
1903  {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x32,0x21,0x02 }, 281088, 0, 1 }, // AVC Intra 50 1080i50
1904  {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x32,0x21,0x03 }, 232960, 0, 0 }, // AVC Intra 50 1080p30
1905  {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x32,0x21,0x04 }, 281088, 0, 0 }, // AVC Intra 50 1080p25
1906  {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x32,0x21,0x08 }, 116736, 0, 0 }, // AVC Intra 50 720p60
1907  {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x32,0x21,0x09 }, 140800, 0, 0 }, // AVC Intra 50 720p50
1908  {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x32,0x30,0x01 }, 0, 122, 0 }, // AVC High 422 Intra
1909  {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x32,0x31,0x01 }, 472576, 0, 1 }, // AVC Intra 100 1080i60
1910  {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x32,0x31,0x02 }, 568832, 0, 1 }, // AVC Intra 100 1080i50
1911  {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x32,0x31,0x03 }, 472576, 0, 0 }, // AVC Intra 100 1080p30
1912  {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x32,0x31,0x04 }, 568832, 0, 0 }, // AVC Intra 100 1080p25
1913  {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x32,0x31,0x08 }, 236544, 0, 0 }, // AVC Intra 100 720p60
1914  {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x32,0x31,0x09 }, 284672, 0, 0 }, // AVC Intra 100 720p50
1915 };
1916 
1918  AVPacket *pkt, MXFIndexEntry *e)
1919 {
1920  MXFContext *mxf = s->priv_data;
1921  MXFStreamContext *sc = st->priv_data;
1922  static const int mxf_h264_num_codec_uls = sizeof(mxf_h264_codec_uls) / sizeof(mxf_h264_codec_uls[0]);
1923  const uint8_t *buf = pkt->data;
1924  const uint8_t *buf_end = pkt->data + pkt->size;
1925  uint32_t state = -1;
1926  int extra_size = 512; // support AVC Intra files without SPS/PPS header
1927  int i, frame_size;
1928  uint8_t uid_found;
1929 
1930  if (pkt->size > extra_size)
1931  buf_end -= pkt->size - extra_size; // no need to parse beyond SPS/PPS header
1932 
1933  for (;;) {
1934  buf = avpriv_find_start_code(buf, buf_end, &state);
1935  if (buf >= buf_end)
1936  break;
1937  --buf;
1938  switch (state & 0x1f) {
1939  case H264_NAL_SPS:
1940  st->codecpar->profile = buf[1];
1941  e->flags |= 0x40;
1942  break;
1943  case H264_NAL_PPS:
1944  if (e->flags & 0x40) { // sequence header present
1945  e->flags |= 0x80; // random access
1946  extra_size = 0;
1947  buf = buf_end;
1948  }
1949  break;
1950  default:
1951  break;
1952  }
1953  }
1954 
1955  if (mxf->header_written)
1956  return 1;
1957 
1958  sc->aspect_ratio = (AVRational){ 16, 9 }; // 16:9 is mandatory for broadcast HD
1959  sc->component_depth = 10; // AVC Intra is always 10 Bit
1960  sc->interlaced = st->codecpar->field_order != AV_FIELD_PROGRESSIVE ? 1 : 0;
1961  if (sc->interlaced)
1962  sc->field_dominance = 1; // top field first is mandatory for AVC Intra
1963 
1964  uid_found = 0;
1965  frame_size = pkt->size + extra_size;
1966  for (i = 0; i < mxf_h264_num_codec_uls; i++) {
1967  if (frame_size == mxf_h264_codec_uls[i].frame_size && sc->interlaced == mxf_h264_codec_uls[i].interlaced) {
1968  sc->codec_ul = &mxf_h264_codec_uls[i].uid;
1969  return 1;
1970  } else if (st->codecpar->profile == mxf_h264_codec_uls[i].profile) {
1971  sc->codec_ul = &mxf_h264_codec_uls[i].uid;
1972  uid_found = 1;
1973  }
1974  }
1975 
1976  if (!uid_found) {
1977  av_log(s, AV_LOG_ERROR, "AVC Intra 50/100 supported only\n");
1978  return 0;
1979  }
1980 
1981  return 1;
1982 }
1983 
1984 static const UID mxf_mpeg2_codec_uls[] = {
1985  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x01,0x10,0x00 }, // MP-ML I-Frame
1986  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x01,0x11,0x00 }, // MP-ML Long GOP
1987  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x02,0x02,0x00 }, // 422P-ML I-Frame
1988  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x02,0x03,0x00 }, // 422P-ML Long GOP
1989  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x03,0x02,0x00 }, // MP-HL I-Frame
1990  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x03,0x03,0x00 }, // MP-HL Long GOP
1991  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x04,0x02,0x00 }, // 422P-HL I-Frame
1992  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x04,0x03,0x00 }, // 422P-HL Long GOP
1993  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x05,0x02,0x00 }, // MP@H-14 I-Frame
1994  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x05,0x03,0x00 }, // MP@H-14 Long GOP
1995 };
1996 
1998 {
1999  int long_gop = 1;
2000 
2001  if (par->profile == 4) { // Main
2002  if (par->level == 8) // Main
2003  return &mxf_mpeg2_codec_uls[0+long_gop];
2004  else if (par->level == 4) // High
2005  return &mxf_mpeg2_codec_uls[4+long_gop];
2006  else if (par->level == 6) // High 14
2007  return &mxf_mpeg2_codec_uls[8+long_gop];
2008  } else if (par->profile == 0) { // 422
2009  if (par->level == 5) // Main
2010  return &mxf_mpeg2_codec_uls[2+long_gop];
2011  else if (par->level == 2) // High
2012  return &mxf_mpeg2_codec_uls[6+long_gop];
2013  }
2014  return NULL;
2015 }
2016 
2018  AVPacket *pkt, MXFIndexEntry *e)
2019 {
2020  MXFStreamContext *sc = st->priv_data;
2021  uint32_t c = -1;
2022  int i;
2023 
2024  for(i = 0; i < pkt->size - 4; i++) {
2025  c = (c<<8) + pkt->data[i];
2026  if (c == 0x1b5) {
2027  if ((pkt->data[i+1] & 0xf0) == 0x10) { // seq ext
2028  st->codecpar->profile = pkt->data[i+1] & 0x07;
2029  st->codecpar->level = pkt->data[i+2] >> 4;
2030  } else if (i + 5 < pkt->size && (pkt->data[i+1] & 0xf0) == 0x80) { // pict coding ext
2031  sc->interlaced = !(pkt->data[i+5] & 0x80); // progressive frame
2032  if (sc->interlaced)
2033  sc->field_dominance = 1 + !(pkt->data[i+4] & 0x80); // top field first
2034  break;
2035  }
2036  } else if (c == 0x1b8) { // gop
2037  if (pkt->data[i+4]>>6 & 0x01) { // closed
2038  sc->closed_gop = 1;
2039  if (e->flags & 0x40) // sequence header present
2040  e->flags |= 0x80; // random access
2041  }
2042  } else if (c == 0x1b3) { // seq
2043  e->flags |= 0x40;
2044  switch ((pkt->data[i+4]>>4) & 0xf) {
2045  case 2: sc->aspect_ratio = (AVRational){ 4, 3}; break;
2046  case 3: sc->aspect_ratio = (AVRational){ 16, 9}; break;
2047  case 4: sc->aspect_ratio = (AVRational){221,100}; break;
2048  default:
2050  st->codecpar->width, st->codecpar->height, 1024*1024);
2051  }
2052  } else if (c == 0x100) { // pic
2053  int pict_type = (pkt->data[i+2]>>3) & 0x07;
2054  e->temporal_ref = (pkt->data[i+1]<<2) | (pkt->data[i+2]>>6);
2055  if (pict_type == 2) { // P-frame
2056  e->flags |= 0x22;
2057  sc->closed_gop = 0; // reset closed GOP, don't matter anymore
2058  } else if (pict_type == 3) { // B-frame
2059  if (sc->closed_gop)
2060  e->flags |= 0x13; // only backward prediction
2061  else
2062  e->flags |= 0x33;
2063  sc->temporal_reordering = -1;
2064  } else if (!pict_type) {
2065  av_log(s, AV_LOG_ERROR, "error parsing mpeg2 frame\n");
2066  return 0;
2067  }
2068  }
2069  }
2070  if (s->oformat != &ff_mxf_d10_muxer)
2072  return !!sc->codec_ul;
2073 }
2074 
2075 static uint64_t mxf_parse_timestamp(time_t timestamp)
2076 {
2077  struct tm tmbuf;
2078  struct tm *time = gmtime_r(&timestamp, &tmbuf);
2079  if (!time)
2080  return 0;
2081  return (uint64_t)(time->tm_year+1900) << 48 |
2082  (uint64_t)(time->tm_mon+1) << 40 |
2083  (uint64_t) time->tm_mday << 32 |
2084  time->tm_hour << 24 |
2085  time->tm_min << 16 |
2086  time->tm_sec << 8;
2087 }
2088 
2090 {
2091  MXFContext *mxf = s->priv_data;
2092  uint32_t seed = av_get_random_seed();
2093  uint64_t umid = seed + 0x5294713400000000LL;
2094 
2095  AV_WB64(mxf->umid , umid);
2096  AV_WB64(mxf->umid+8, umid>>8);
2097 
2098  mxf->instance_number = seed & 0xFFFFFF;
2099 }
2100 
2102 {
2103  MXFContext *mxf = s->priv_data;
2104  AVDictionaryEntry *tcr = av_dict_get(s->metadata, "timecode", NULL, 0);
2105  if (!tcr)
2106  tcr = av_dict_get(st->metadata, "timecode", NULL, 0);
2107 
2108  if (tcr)
2109  return av_timecode_init_from_string(&mxf->tc, rate, tcr->value, s);
2110  else
2111  return av_timecode_init(&mxf->tc, rate, 0, 0, s);
2112 }
2113 
2115 {
2116  MXFContext *mxf = s->priv_data;
2117  int i, ret;
2118  uint8_t present[FF_ARRAY_ELEMS(mxf_essence_container_uls)] = {0};
2119  const MXFSamplesPerFrame *spf = NULL;
2120  int64_t timestamp = 0;
2121 
2122  if (!s->nb_streams)
2123  return -1;
2124 
2125  if (s->oformat == &ff_mxf_opatom_muxer && s->nb_streams !=1) {
2126  av_log(s, AV_LOG_ERROR, "there must be exactly one stream for mxf opatom\n");
2127  return -1;
2128  }
2129 
2130  if (!av_dict_get(s->metadata, "comment_", NULL, AV_DICT_IGNORE_SUFFIX))
2131  mxf->store_user_comments = 0;
2132 
2133  for (i = 0; i < s->nb_streams; i++) {
2134  AVStream *st = s->streams[i];
2135  MXFStreamContext *sc = av_mallocz(sizeof(*sc));
2136  if (!sc)
2137  return AVERROR(ENOMEM);
2138  st->priv_data = sc;
2139 
2140  if (((i == 0) ^ (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)) && s->oformat != &ff_mxf_opatom_muxer) {
2141  av_log(s, AV_LOG_ERROR, "there must be exactly one video stream and it must be the first one\n");
2142  return -1;
2143  }
2144 
2145  if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
2146  const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(st->codecpar->format);
2147  // TODO: should be avg_frame_rate
2148  AVRational rate, tbc = st->time_base;
2149  // Default component depth to 8
2150  sc->component_depth = 8;
2151  sc->h_chroma_sub_sample = 2;
2152  sc->color_siting = 0xFF;
2153 
2154  if (pix_desc) {
2155  sc->component_depth = pix_desc->comp[0].depth;
2156  sc->h_chroma_sub_sample = 1 << pix_desc->log2_chroma_w;
2157  }
2158  switch (ff_choose_chroma_location(s, st)) {
2159  case AVCHROMA_LOC_TOPLEFT: sc->color_siting = 0; break;
2160  case AVCHROMA_LOC_LEFT: sc->color_siting = 6; break;
2161  case AVCHROMA_LOC_TOP: sc->color_siting = 1; break;
2162  case AVCHROMA_LOC_CENTER: sc->color_siting = 3; break;
2163  }
2164 
2165  mxf->timecode_base = (tbc.den + tbc.num/2) / tbc.num;
2166  spf = ff_mxf_get_samples_per_frame(s, tbc);
2167  if (!spf) {
2168  av_log(s, AV_LOG_ERROR, "Unsupported video frame rate %d/%d\n",
2169  tbc.den, tbc.num);
2170  return AVERROR(EINVAL);
2171  }
2172  mxf->time_base = spf->time_base;
2173  rate = av_inv_q(mxf->time_base);
2174  avpriv_set_pts_info(st, 64, mxf->time_base.num, mxf->time_base.den);
2175  if((ret = mxf_init_timecode(s, st, rate)) < 0)
2176  return ret;
2177 
2178  sc->video_bit_rate = st->codecpar->bit_rate;
2179  if (s->oformat == &ff_mxf_d10_muxer) {
2181  av_log(s, AV_LOG_ERROR, "error MXF D-10 only support MPEG-2 Video\n");
2182  return AVERROR(EINVAL);
2183  }
2184  if ((sc->video_bit_rate == 50000000) && (mxf->time_base.den == 25)) {
2186  } else if ((sc->video_bit_rate == 49999840 || sc->video_bit_rate == 50000000) && (mxf->time_base.den != 25)) {
2188  } else if (sc->video_bit_rate == 40000000) {
2189  if (mxf->time_base.den == 25) sc->index = INDEX_D10_625_50_40_VIDEO;
2190  else sc->index = INDEX_D10_525_60_40_VIDEO;
2191  } else if (sc->video_bit_rate == 30000000) {
2192  if (mxf->time_base.den == 25) sc->index = INDEX_D10_625_50_30_VIDEO;
2193  else sc->index = INDEX_D10_525_60_30_VIDEO;
2194  } else {
2195  av_log(s, AV_LOG_ERROR, "error MXF D-10 only support 30/40/50 mbit/s\n");
2196  return -1;
2197  }
2198 
2199  mxf->edit_unit_byte_count = KAG_SIZE; // system element
2200  mxf->edit_unit_byte_count += 16 + 4 + (uint64_t)sc->video_bit_rate *
2201  mxf->time_base.num / (8*mxf->time_base.den);
2203  mxf->edit_unit_byte_count += 16 + 4 + 4 + spf->samples_per_frame[0]*8*4;
2205 
2206  sc->signal_standard = 1;
2207  }
2208  if (mxf->signal_standard >= 0)
2209  sc->signal_standard = mxf->signal_standard;
2210  } else if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
2211  if (st->codecpar->sample_rate != 48000) {
2212  av_log(s, AV_LOG_ERROR, "only 48khz is implemented\n");
2213  return -1;
2214  }
2215  avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
2216  if (s->oformat == &ff_mxf_d10_muxer) {
2217  if (st->index != 1) {
2218  av_log(s, AV_LOG_ERROR, "MXF D-10 only support one audio track\n");
2219  return -1;
2220  }
2221  if (st->codecpar->codec_id != AV_CODEC_ID_PCM_S16LE &&
2223  av_log(s, AV_LOG_ERROR, "MXF D-10 only support 16 or 24 bits le audio\n");
2224  }
2225  sc->index = ((MXFStreamContext*)s->streams[0]->priv_data)->index + 1;
2226  } else if (s->oformat == &ff_mxf_opatom_muxer) {
2227  AVRational tbc = av_inv_q(mxf->audio_edit_rate);
2228 
2229  if (st->codecpar->codec_id != AV_CODEC_ID_PCM_S16LE &&
2231  av_log(s, AV_LOG_ERROR, "Only pcm_s16le and pcm_s24le audio codecs are implemented\n");
2232  return AVERROR_PATCHWELCOME;
2233  }
2234  if (st->codecpar->channels != 1) {
2235  av_log(s, AV_LOG_ERROR, "MXF OPAtom only supports single channel audio\n");
2236  return AVERROR(EINVAL);
2237  }
2238 
2239  spf = ff_mxf_get_samples_per_frame(s, tbc);
2240  if (!spf) {
2241  av_log(s, AV_LOG_ERROR, "Unsupported timecode frame rate %d/%d\n", tbc.den, tbc.num);
2242  return AVERROR(EINVAL);
2243  }
2244 
2245  mxf->time_base = st->time_base;
2246  if((ret = mxf_init_timecode(s, st, av_inv_q(spf->time_base))) < 0)
2247  return ret;
2248 
2249  mxf->timecode_base = (tbc.den + tbc.num/2) / tbc.num;
2251  sc->index = INDEX_WAV;
2252  } else {
2253  mxf->slice_count = 1;
2254  }
2255  }
2256 
2257  if (!sc->index) {
2259  if (sc->index == -1) {
2260  av_log(s, AV_LOG_ERROR, "track %d: could not find essence container ul, "
2261  "codec not currently supported in container\n", i);
2262  return -1;
2263  }
2264  }
2265 
2266  sc->codec_ul = &mxf_essence_container_uls[sc->index].codec_ul;
2267 
2268  memcpy(sc->track_essence_element_key, mxf_essence_container_uls[sc->index].element_ul, 15);
2269  sc->track_essence_element_key[15] = present[sc->index];
2270  PRINT_KEY(s, "track essence element key", sc->track_essence_element_key);
2271 
2272  if (!present[sc->index])
2273  mxf->essence_container_count++;
2274  present[sc->index]++;
2275  }
2276 
2277  if (s->oformat == &ff_mxf_d10_muxer || s->oformat == &ff_mxf_opatom_muxer) {
2278  mxf->essence_container_count = 1;
2279  }
2280 
2281  if (!(s->flags & AVFMT_FLAG_BITEXACT))
2282  mxf_gen_umid(s);
2283 
2284  for (i = 0; i < s->nb_streams; i++) {
2285  MXFStreamContext *sc = s->streams[i]->priv_data;
2286  // update element count
2287  sc->track_essence_element_key[13] = present[sc->index];
2288  if (!memcmp(sc->track_essence_element_key, mxf_essence_container_uls[15].element_ul, 13)) // DV
2289  sc->order = (0x15 << 24) | AV_RB32(sc->track_essence_element_key+13);
2290  else
2291  sc->order = AV_RB32(sc->track_essence_element_key+12);
2292  }
2293 
2294  if (ff_parse_creation_time_metadata(s, &timestamp, 1) > 0)
2295  mxf->timestamp = mxf_parse_timestamp(timestamp);
2296  mxf->duration = -1;
2297 
2298  mxf->timecode_track = av_mallocz(sizeof(*mxf->timecode_track));
2299  if (!mxf->timecode_track)
2300  return AVERROR(ENOMEM);
2302  if (!mxf->timecode_track->priv_data)
2303  return AVERROR(ENOMEM);
2304  mxf->timecode_track->index = -1;
2305 
2306  if (!spf)
2307  spf = ff_mxf_get_samples_per_frame(s, (AVRational){ 1, 25 });
2308 
2309  if (ff_audio_interleave_init(s, spf->samples_per_frame, mxf->time_base) < 0)
2310  return -1;
2311 
2312  return 0;
2313 }
2314 
2315 static const uint8_t system_metadata_pack_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0D,0x01,0x03,0x01,0x04,0x01,0x01,0x00 };
2316 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 };
2317 
2319 {
2320  MXFContext *mxf = s->priv_data;
2321  AVIOContext *pb = s->pb;
2322  unsigned frame;
2323  uint32_t time_code;
2324 
2325  frame = mxf->last_indexed_edit_unit + mxf->edit_units_count;
2326 
2327  // write system metadata pack
2329  klv_encode_ber4_length(pb, 57);
2330  avio_w8(pb, 0x5c); // UL, user date/time stamp, picture and sound item present
2331  avio_w8(pb, 0x04); // content package rate
2332  avio_w8(pb, 0x00); // content package type
2333  avio_wb16(pb, 0x00); // channel handle
2334  avio_wb16(pb, (mxf->tc.start + frame) & 0xFFFF); // continuity count, supposed to overflow
2335  if (mxf->essence_container_count > 1)
2336  avio_write(pb, multiple_desc_ul, 16);
2337  else {
2338  MXFStreamContext *sc = s->streams[0]->priv_data;
2339  avio_write(pb, mxf_essence_container_uls[sc->index].container_ul, 16);
2340  }
2341  avio_w8(pb, 0);
2342  avio_wb64(pb, 0);
2343  avio_wb64(pb, 0); // creation date/time stamp
2344 
2345  avio_w8(pb, 0x81); // SMPTE 12M time code
2346  time_code = av_timecode_get_smpte_from_framenum(&mxf->tc, frame);
2347  avio_wb32(pb, time_code);
2348  avio_wb32(pb, 0); // binary group data
2349  avio_wb64(pb, 0);
2350 
2351  // write system metadata package set
2353  klv_encode_ber4_length(pb, 35);
2354  avio_w8(pb, 0x83); // UMID
2355  avio_wb16(pb, 0x20);
2356  mxf_write_umid(s, 1);
2357 }
2358 
2360 {
2361  MXFContext *mxf = s->priv_data;
2362  AVIOContext *pb = s->pb;
2363  MXFStreamContext *sc = st->priv_data;
2364  int packet_size = (uint64_t)sc->video_bit_rate*mxf->time_base.num /
2365  (8*mxf->time_base.den); // frame size
2366  int pad;
2367 
2368  packet_size += 16 + 4;
2369  packet_size += klv_fill_size(packet_size);
2370 
2371  klv_encode_ber4_length(pb, pkt->size);
2372  avio_write(pb, pkt->data, pkt->size);
2373 
2374  // ensure CBR muxing by padding to correct video frame size
2375  pad = packet_size - pkt->size - 16 - 4;
2376  if (pad > 20) {
2377  avio_write(s->pb, klv_fill_key, 16);
2378  pad -= 16 + 4;
2379  klv_encode_ber4_length(s->pb, pad);
2380  ffio_fill(s->pb, 0, pad);
2381  av_assert1(!(avio_tell(s->pb) & (KAG_SIZE-1)));
2382  } else {
2383  av_log(s, AV_LOG_WARNING, "cannot fill d-10 video packet\n");
2384  ffio_fill(s->pb, 0, pad);
2385  }
2386 }
2387 
2389 {
2390  MXFContext *mxf = s->priv_data;
2391  AVIOContext *pb = s->pb;
2392  int frame_size = pkt->size / st->codecpar->block_align;
2393  uint8_t *samples = pkt->data;
2394  uint8_t *end = pkt->data + pkt->size;
2395  int i;
2396 
2397  klv_encode_ber4_length(pb, 4 + frame_size*4*8);
2398 
2399  avio_w8(pb, (frame_size == 1920 ? 0 : (mxf->edit_units_count-1) % 5 + 1));
2400  avio_wl16(pb, frame_size);
2401  avio_w8(pb, (1<<st->codecpar->channels)-1);
2402 
2403  while (samples < end) {
2404  for (i = 0; i < st->codecpar->channels; i++) {
2405  uint32_t sample;
2406  if (st->codecpar->codec_id == AV_CODEC_ID_PCM_S24LE) {
2407  sample = AV_RL24(samples)<< 4;
2408  samples += 3;
2409  } else {
2410  sample = AV_RL16(samples)<<12;
2411  samples += 2;
2412  }
2413  avio_wl32(pb, sample | i);
2414  }
2415  for (; i < 8; i++)
2416  avio_wl32(pb, i);
2417  }
2418 }
2419 
2421 {
2422  MXFContext *mxf = s->priv_data;
2423  AVIOContext *pb = s->pb;
2424  AVStream *st = s->streams[0];
2425  MXFStreamContext *sc = st->priv_data;
2426  const uint8_t *key = NULL;
2427 
2428  int err;
2429 
2430  if (!mxf->header_written)
2431  key = body_partition_key;
2432 
2433  if ((err = mxf_write_partition(s, 1, 0, key, 0)) < 0)
2434  return err;
2435  mxf_write_klv_fill(s);
2438  return 0;
2439 }
2440 
2442 {
2443  MXFContext *mxf = s->priv_data;
2444  AVIOContext *pb = s->pb;
2445 
2446  int err;
2447 
2448  if (!mxf->header_written) {
2449  if ((err = mxf_write_partition(s, 0, 0, header_open_partition_key, 1)) < 0)
2450  return err;
2451  mxf_write_klv_fill(s);
2452 
2453  if ((err = mxf_write_opatom_body_partition(s)) < 0)
2454  return err;
2455  mxf->header_written = 1;
2456  }
2457 
2458  if (!mxf->edit_unit_byte_count) {
2460  mxf->index_entries[mxf->edit_units_count].flags = ie->flags;
2462  }
2463  mxf->edit_units_count++;
2464  avio_write(pb, pkt->data, pkt->size);
2465  mxf->body_offset += pkt->size;
2466  avio_flush(pb);
2467 
2468  return 0;
2469 }
2470 
2472 {
2473  MXFContext *mxf = s->priv_data;
2474  AVIOContext *pb = s->pb;
2475  AVStream *st = s->streams[pkt->stream_index];
2476  MXFStreamContext *sc = st->priv_data;
2477  MXFIndexEntry ie = {0};
2478  int err;
2479 
2481  if ((err = av_reallocp_array(&mxf->index_entries, mxf->edit_units_count
2482  + EDIT_UNITS_PER_BODY, sizeof(*mxf->index_entries))) < 0) {
2483  mxf->edit_units_count = 0;
2484  av_log(s, AV_LOG_ERROR, "could not allocate index entries\n");
2485  return err;
2486  }
2487  }
2488 
2490  if (!mxf_parse_mpeg2_frame(s, st, pkt, &ie)) {
2491  av_log(s, AV_LOG_ERROR, "could not get mpeg2 profile and level\n");
2492  return -1;
2493  }
2494  } else if (st->codecpar->codec_id == AV_CODEC_ID_DNXHD) {
2495  if (!mxf_parse_dnxhd_frame(s, st, pkt)) {
2496  av_log(s, AV_LOG_ERROR, "could not get dnxhd profile\n");
2497  return -1;
2498  }
2499  } else if (st->codecpar->codec_id == AV_CODEC_ID_DVVIDEO) {
2500  if (!mxf_parse_dv_frame(s, st, pkt)) {
2501  av_log(s, AV_LOG_ERROR, "could not get dv profile\n");
2502  return -1;
2503  }
2504  } else if (st->codecpar->codec_id == AV_CODEC_ID_H264) {
2505  if (!mxf_parse_h264_frame(s, st, pkt, &ie)) {
2506  av_log(s, AV_LOG_ERROR, "could not get h264 profile\n");
2507  return -1;
2508  }
2509  }
2510 
2511  if (s->oformat == &ff_mxf_opatom_muxer)
2512  return mxf_write_opatom_packet(s, pkt, &ie);
2513 
2514  if (!mxf->header_written) {
2515  if (mxf->edit_unit_byte_count) {
2516  if ((err = mxf_write_partition(s, 1, 2, header_open_partition_key, 1)) < 0)
2517  return err;
2518  mxf_write_klv_fill(s);
2520  } else {
2521  if ((err = mxf_write_partition(s, 0, 0, header_open_partition_key, 1)) < 0)
2522  return err;
2523  }
2524  mxf->header_written = 1;
2525  }
2526 
2527  if (st->index == 0) {
2528  if (!mxf->edit_unit_byte_count &&
2530  !(ie.flags & 0x33)) { // I-frame, GOP start
2531  mxf_write_klv_fill(s);
2532  if ((err = mxf_write_partition(s, 1, 2, body_partition_key, 0)) < 0)
2533  return err;
2534  mxf_write_klv_fill(s);
2536  }
2537 
2538  mxf_write_klv_fill(s);
2540 
2541  if (!mxf->edit_unit_byte_count) {
2543  mxf->index_entries[mxf->edit_units_count].flags = ie.flags;
2545  mxf->body_offset += KAG_SIZE; // size of system element
2546  }
2547  mxf->edit_units_count++;
2548  } else if (!mxf->edit_unit_byte_count && st->index == 1) {
2549  if (!mxf->edit_units_count) {
2550  av_log(s, AV_LOG_ERROR, "No packets in first stream\n");
2551  return AVERROR_PATCHWELCOME;
2552  }
2554  mxf->body_offset - mxf->index_entries[mxf->edit_units_count-1].offset;
2555  }
2556 
2557  mxf_write_klv_fill(s);
2558  avio_write(pb, sc->track_essence_element_key, 16); // write key
2559  if (s->oformat == &ff_mxf_d10_muxer) {
2561  mxf_write_d10_video_packet(s, st, pkt);
2562  else
2563  mxf_write_d10_audio_packet(s, st, pkt);
2564  } else {
2565  klv_encode_ber4_length(pb, pkt->size); // write length
2566  avio_write(pb, pkt->data, pkt->size);
2567  mxf->body_offset += 16+4+pkt->size + klv_fill_size(16+4+pkt->size);
2568  }
2569 
2570  avio_flush(pb);
2571 
2572  return 0;
2573 }
2574 
2576 {
2577  MXFContext *mxf = s->priv_data;
2578  AVIOContext *pb = s->pb;
2579  uint64_t pos = avio_tell(pb);
2580  int i;
2581 
2583  klv_encode_ber_length(pb, 28 + 12LL*mxf->body_partitions_count);
2584 
2585  if (mxf->edit_unit_byte_count && s->oformat != &ff_mxf_opatom_muxer)
2586  avio_wb32(pb, 1); // BodySID of header partition
2587  else
2588  avio_wb32(pb, 0);
2589  avio_wb64(pb, 0); // offset of header partition
2590 
2591  for (i = 0; i < mxf->body_partitions_count; i++) {
2592  avio_wb32(pb, 1); // BodySID
2593  avio_wb64(pb, mxf->body_partition_offset[i]);
2594  }
2595 
2596  avio_wb32(pb, 0); // BodySID of footer partition
2598 
2599  avio_wb32(pb, avio_tell(pb) - pos + 4);
2600 }
2601 
2603 {
2604  MXFContext *mxf = s->priv_data;
2605  AVIOContext *pb = s->pb;
2606  int err = 0;
2607 
2608  if (!mxf->header_written ||
2609  (s->oformat == &ff_mxf_opatom_muxer && !mxf->body_partition_offset)) {
2610  /* reason could be invalid options/not supported codec/out of memory */
2611  err = AVERROR_UNKNOWN;
2612  goto end;
2613  }
2614 
2616 
2617  mxf_write_klv_fill(s);
2619  if (mxf->edit_unit_byte_count && s->oformat != &ff_mxf_opatom_muxer) { // no need to repeat index
2620  if ((err = mxf_write_partition(s, 0, 0, footer_partition_key, 0)) < 0)
2621  goto end;
2622  } else {
2623  if ((err = mxf_write_partition(s, 0, 2, footer_partition_key, 0)) < 0)
2624  goto end;
2625  mxf_write_klv_fill(s);
2627  }
2628 
2629  mxf_write_klv_fill(s);
2631 
2632  if (s->pb->seekable & AVIO_SEEKABLE_NORMAL) {
2633  if (s->oformat == &ff_mxf_opatom_muxer) {
2634  /* rewrite body partition to update lengths */
2635  avio_seek(pb, mxf->body_partition_offset[0], SEEK_SET);
2636  if ((err = mxf_write_opatom_body_partition(s)) < 0)
2637  goto end;
2638  }
2639 
2640  avio_seek(pb, 0, SEEK_SET);
2641  if (mxf->edit_unit_byte_count && s->oformat != &ff_mxf_opatom_muxer) {
2642  if ((err = mxf_write_partition(s, 1, 2, header_closed_partition_key, 1)) < 0)
2643  goto end;
2644  mxf_write_klv_fill(s);
2646  } else {
2647  if ((err = mxf_write_partition(s, 0, 0, header_closed_partition_key, 1)) < 0)
2648  goto end;
2649  }
2650  }
2651 
2652 end:
2654 
2655  av_freep(&mxf->index_entries);
2658  av_freep(&mxf->timecode_track);
2659 
2660  mxf_free(s);
2661 
2662  return err < 0 ? err : 0;
2663 }
2664 
2666 {
2667  int i, stream_count = 0;
2668 
2669  for (i = 0; i < s->nb_streams; i++)
2670  stream_count += !!s->streams[i]->last_in_packet_buffer;
2671 
2672  if (stream_count && (s->nb_streams == stream_count || flush)) {
2673  AVPacketList *pktl = s->internal->packet_buffer;
2674  if (s->nb_streams != stream_count) {
2675  AVPacketList *last = NULL;
2676  // find last packet in edit unit
2677  while (pktl) {
2678  if (!stream_count || pktl->pkt.stream_index == 0)
2679  break;
2680  last = pktl;
2681  pktl = pktl->next;
2682  stream_count--;
2683  }
2684  // purge packet queue
2685  while (pktl) {
2686  AVPacketList *next = pktl->next;
2687 
2688  if(s->streams[pktl->pkt.stream_index]->last_in_packet_buffer == pktl)
2690  av_packet_unref(&pktl->pkt);
2691  av_freep(&pktl);
2692  pktl = next;
2693  }
2694  if (last)
2695  last->next = NULL;
2696  else {
2697  s->internal->packet_buffer = NULL;
2699  goto out;
2700  }
2701  pktl = s->internal->packet_buffer;
2702  }
2703 
2704  *out = pktl->pkt;
2705  av_log(s, AV_LOG_TRACE, "out st:%d dts:%"PRId64"\n", (*out).stream_index, (*out).dts);
2706  s->internal->packet_buffer = pktl->next;
2707  if(s->streams[pktl->pkt.stream_index]->last_in_packet_buffer == pktl)
2709  if(!s->internal->packet_buffer)
2711  av_freep(&pktl);
2712  return 1;
2713  } else {
2714  out:
2715  av_init_packet(out);
2716  return 0;
2717  }
2718 }
2719 
2721 {
2723  MXFStreamContext *sc2 = s->streams[next->stream_index]->priv_data;
2724 
2725  return next->dts > pkt->dts ||
2726  (next->dts == pkt->dts && sc->order < sc2->order);
2727 }
2728 
2730 {
2731  return ff_audio_rechunk_interleave(s, out, pkt, flush,
2733 }
2734 
2735 #define MXF_COMMON_OPTIONS \
2736  { "signal_standard", "Force/set Sigal Standard",\
2737  offsetof(MXFContext, signal_standard), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 7, AV_OPT_FLAG_ENCODING_PARAM, "signal_standard"},\
2738  { "bt601", "ITU-R BT.601 and BT.656, also SMPTE 125M (525 and 625 line interlaced)",\
2739  0, AV_OPT_TYPE_CONST, {.i64 = 1}, -1, 7, AV_OPT_FLAG_ENCODING_PARAM, "signal_standard"},\
2740  { "bt1358", "ITU-R BT.1358 and ITU-R BT.799-3, also SMPTE 293M (525 and 625 line progressive)",\
2741  0, AV_OPT_TYPE_CONST, {.i64 = 2}, -1, 7, AV_OPT_FLAG_ENCODING_PARAM, "signal_standard"},\
2742  { "smpte347m", "SMPTE 347M (540 Mbps mappings)",\
2743  0, AV_OPT_TYPE_CONST, {.i64 = 3}, -1, 7, AV_OPT_FLAG_ENCODING_PARAM, "signal_standard"},\
2744  { "smpte274m", "SMPTE 274M (1125 line)",\
2745  0, AV_OPT_TYPE_CONST, {.i64 = 4}, -1, 7, AV_OPT_FLAG_ENCODING_PARAM, "signal_standard"},\
2746  { "smpte296m", "SMPTE 296M (750 line progressive)",\
2747  0, AV_OPT_TYPE_CONST, {.i64 = 5}, -1, 7, AV_OPT_FLAG_ENCODING_PARAM, "signal_standard"},\
2748  { "smpte349m", "SMPTE 349M (1485 Mbps mappings)",\
2749  0, AV_OPT_TYPE_CONST, {.i64 = 6}, -1, 7, AV_OPT_FLAG_ENCODING_PARAM, "signal_standard"},\
2750  { "smpte428", "SMPTE 428-1 DCDM",\
2751  0, AV_OPT_TYPE_CONST, {.i64 = 7}, -1, 7, AV_OPT_FLAG_ENCODING_PARAM, "signal_standard"},
2752 
2753 
2754 
2755 static const AVOption mxf_options[] = {
2757  { "store_user_comments", "",
2758  offsetof(MXFContext, store_user_comments), AV_OPT_TYPE_BOOL, {.i64 = 1}, 0, 1, AV_OPT_FLAG_ENCODING_PARAM},
2759  { NULL },
2760 };
2761 
2762 static const AVClass mxf_muxer_class = {
2763  .class_name = "MXF muxer",
2764  .item_name = av_default_item_name,
2765  .option = mxf_options,
2766  .version = LIBAVUTIL_VERSION_INT,
2767 };
2768 
2769 static const AVOption d10_options[] = {
2770  { "d10_channelcount", "Force/set channelcount in generic sound essence descriptor",
2771  offsetof(MXFContext, channel_count), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 8, AV_OPT_FLAG_ENCODING_PARAM},
2773  { "store_user_comments", "",
2774  offsetof(MXFContext, store_user_comments), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, AV_OPT_FLAG_ENCODING_PARAM},
2775  { NULL },
2776 };
2777 
2779  .class_name = "MXF-D10 muxer",
2780  .item_name = av_default_item_name,
2781  .option = d10_options,
2782  .version = LIBAVUTIL_VERSION_INT,
2783 };
2784 
2785 static const AVOption opatom_options[] = {
2786  { "mxf_audio_edit_rate", "Audio edit rate for timecode",
2787  offsetof(MXFContext, audio_edit_rate), AV_OPT_TYPE_RATIONAL, {.dbl=25}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
2789  { NULL },
2790 };
2791 
2793  .class_name = "MXF-OPAtom muxer",
2794  .item_name = av_default_item_name,
2795  .option = opatom_options,
2796  .version = LIBAVUTIL_VERSION_INT,
2797 };
2798 
2800  .name = "mxf",
2801  .long_name = NULL_IF_CONFIG_SMALL("MXF (Material eXchange Format)"),
2802  .mime_type = "application/mxf",
2803  .extensions = "mxf",
2804  .priv_data_size = sizeof(MXFContext),
2805  .audio_codec = AV_CODEC_ID_PCM_S16LE,
2806  .video_codec = AV_CODEC_ID_MPEG2VIDEO,
2812  .priv_class = &mxf_muxer_class,
2813 };
2814 
2815 AVOutputFormat ff_mxf_d10_muxer = {
2816  .name = "mxf_d10",
2817  .long_name = NULL_IF_CONFIG_SMALL("MXF (Material eXchange Format) D-10 Mapping"),
2818  .mime_type = "application/mxf",
2819  .priv_data_size = sizeof(MXFContext),
2820  .audio_codec = AV_CODEC_ID_PCM_S16LE,
2821  .video_codec = AV_CODEC_ID_MPEG2VIDEO,
2827  .priv_class = &mxf_d10_muxer_class,
2828 };
2829 
2830 AVOutputFormat ff_mxf_opatom_muxer = {
2831  .name = "mxf_opatom",
2832  .long_name = NULL_IF_CONFIG_SMALL("MXF (Material eXchange Format) Operational Pattern Atom"),
2833  .mime_type = "application/mxf",
2834  .extensions = "mxf",
2835  .priv_data_size = sizeof(MXFContext),
2836  .audio_codec = AV_CODEC_ID_PCM_S16LE,
2837  .video_codec = AV_CODEC_ID_DNXHD,
2843  .priv_class = &mxf_opatom_muxer_class,
2844 };
static uint64_t mxf_parse_timestamp(time_t timestamp)
Definition: mxfenc.c:2075
static void write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, int unqueue)
Definition: ffmpeg.c:671
void avio_wb64(AVIOContext *s, uint64_t val)
Definition: aviobuf.c:461
uint32_t tagged_value_count
Definition: mxfenc.c:380
#define NULL
Definition: coverity.c:32
enum AVFieldOrder field_order
Video only.
Definition: avcodec.h:3817
void avio_wl16(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:467
MXFMetadataSetType
Definition: mxf.h:30
const char * s
Definition: avisynth_c.h:768
Bytestream IO Context.
Definition: avio.h:161
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
static int mxf_utf16_local_tag_length(const char *utf8_str)
Definition: mxfenc.c:739
static void mxf_write_d10_video_packet(AVFormatContext *s, AVStream *st, AVPacket *pkt)
Definition: mxfenc.c:2359
int component_depth
Definition: mxfenc.c:87
static const uint8_t header_closed_partition_key[]
Definition: mxfenc.c:398
#define GET_UTF8(val, GET_BYTE, ERROR)
Convert a UTF-8 character (up to 4 bytes) to its 32-bit UCS-4 encoded form.
Definition: common.h:361
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2359
MPEG-2/4 4:2:0, H.264 default for 4:2:0.
Definition: pixfmt.h:513
int signal_standard
Definition: mxfenc.c:89
int avio_put_str16be(AVIOContext *s, const char *str)
Convert an UTF-8 string to UTF-16BE and write it.
AVOption.
Definition: opt.h:246
static void flush(AVCodecContext *avctx)
static void mxf_write_index_table_segment(AVFormatContext *s)
Definition: mxfenc.c:1475
enum AVCodecID id
Definition: mxfenc.c:146
uint64_t body_offset
Definition: mxfenc.c:375
#define PRINT_KEY(pc, s, x)
Definition: mxf.h:115
static const uint8_t klv_fill_key[]
Definition: mxfenc.c:399
static const UID mxf_mpegvideo_descriptor_key
Definition: mxfenc.c:1071
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
#define LIBAVUTIL_VERSION_INT
Definition: version.h:86
unsigned char * buf_ptr
Current position in the buffer.
Definition: avio.h:228
static void klv_encode_ber9_length(AVIOContext *pb, uint64_t len)
Definition: mxfenc.c:568
int index
index in mxf_essence_container_uls table
Definition: mxfenc.c:82
void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition: utils.c:4747
static void mxf_write_structural_component(AVFormatContext *s, AVStream *st, enum MXFMetadataSetType type)
Definition: mxfenc.c:964
unsigned body_partitions_count
Definition: mxfenc.c:368
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:3736
const int * samples
current samples per frame, pointer to samples_per_frame
int num
Numerator.
Definition: rational.h:59
static int mxf_init_timecode(AVFormatContext *s, AVStream *st, AVRational rate)
Definition: mxfenc.c:2101
int index
stream index in AVFormatContext
Definition: avformat.h:873
int size
Definition: avcodec.h:1400
Definition: mxf.h:34
static const UID mxf_cdci_descriptor_key
Definition: mxfenc.c:1074
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:244
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:193
AVFormatInternal * internal
An opaque field for libavformat internal usage.
Definition: avformat.h:1756
int interlaced
whether picture is interlaced
Definition: mxfenc.c:85
int av_log2(unsigned v)
Definition: intmath.c:26
static const uint8_t body_partition_key[]
Definition: mxfenc.c:400
AVOutputFormat ff_mxf_muxer
Definition: mxfenc.c:2799
int avpriv_dnxhd_get_interlaced(int cid)
Definition: dnxhddata.c:1095
void * priv_data
Definition: avformat.h:887
int version
Definition: avisynth_c.h:766
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:236
static AVPacket pkt
struct AVRational time_base
Definition: mxf.h:74
static void mxf_free(AVFormatContext *s)
Definition: mxfenc.c:624
static void mxf_write_uuid(AVIOContext *pb, enum MXFMetadataSetType type, int value)
Definition: mxfenc.c:511
static void mxf_write_generic_desc(AVFormatContext *s, AVStream *st, const UID key, unsigned size)
Definition: mxfenc.c:1037
#define sample
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_RL16
Definition: bytestream.h:87
UID track_essence_element_key
Definition: mxfenc.c:81
static void mxf_write_common_fields(AVFormatContext *s, AVStream *st)
Definition: mxfenc.c:884
uint8_t log2_chroma_w
Amount to shift the luma width right to find the chroma width.
Definition: pixdesc.h:92
int id
Definition: mxf.h:70
This struct describes the properties of an encoded stream.
Definition: avcodec.h:3728
static int mxf_write_header(AVFormatContext *s)
Definition: mxfenc.c:2114
static void klv_encode_ber4_length(AVIOContext *pb, int len)
Definition: mxfenc.c:562
static const uint8_t multiple_desc_ul[]
Definition: mxfenc.c:406
Format I/O context.
Definition: avformat.h:1325
int h_chroma_sub_sample
Definition: mxfenc.c:90
uint8_t UID[16]
Definition: mxf.h:28
UID uid
Definition: mxfenc.c:1895
int temporal_reordering
Definition: mxfenc.c:91
uint8_t slice_count
index slice count minus 1 (1 if no audio, 0 otherwise)
Definition: mxfenc.c:365
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:72
#define AV_WB64(p, v)
Definition: intreadwrite.h:433
static const UID mxf_generic_sound_descriptor_key
Definition: mxfenc.c:1075
static int klv_encode_ber_length(AVIOContext *pb, uint64_t len)
Definition: mxfenc.c:541
static void mxf_write_klv_fill(AVFormatContext *s)
Definition: mxfenc.c:1608
static void mxf_write_refs_count(AVIOContext *pb, int ref_count)
Definition: mxfenc.c:527
int timecode_base
rounded time code base (25 or 30)
Definition: mxfenc.c:373
int av_timecode_init_from_string(AVTimecode *tc, AVRational rate, const char *str, void *log_ctx)
Parse timecode representation (hh:mm:ss[:;.
Definition: timecode.c:194
static int mxf_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: mxfenc.c:2471
AVOutputFormat ff_mxf_d10_muxer
Definition: mxfenc.c:2815
void avio_wl32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:367
AVComponentDescriptor comp[4]
Parameters that describe how pixels are packed.
Definition: pixdesc.h:117
static unsigned klv_fill_size(uint64_t size)
Definition: mxfenc.c:1466
uint8_t
const MXFSamplesPerFrame * ff_mxf_get_samples_per_frame(AVFormatContext *s, AVRational time_base)
Definition: mxf.c:154
int width
Video only.
Definition: avcodec.h:3802
static void mxf_write_cdci_common(AVFormatContext *s, AVStream *st, const UID key, unsigned size)
Definition: mxfenc.c:1077
AVOptions.
static const UID mxf_mpeg2_codec_uls[]
Definition: mxfenc.c:1984
#define AV_LOG_TRACE
Extremely verbose debugging, useful for libav* development.
Definition: log.h:202
AVPacket pkt
Definition: avformat.h:1963
timecode is drop frame
Definition: timecode.h:36
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:90
int ff_audio_interleave_init(AVFormatContext *s, const int *samples_per_frame, AVRational time_base)
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_RB32
Definition: bytestream.h:87
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1393
static AVFrame * frame
int avpriv_dnxhd_get_frame_size(int cid)
Definition: dnxhddata.c:1087
AVDictionaryEntry * av_dict_get(const AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags)
Get a dictionary entry with matching key.
Definition: dict.c:40
int flags
Flags modifying the (de)muxer behaviour.
Definition: avformat.h:1436
uint8_t * data
Definition: avcodec.h:1399
uint64_t duration
Definition: mxfenc.c:370
int start
timecode frame start (first base frame number)
Definition: timecode.h:42
static int flags
Definition: log.c:57
uint32_t tag
Definition: movenc.c:1424
uint64_t offset
Definition: mxfenc.c:74
Definition: ismindex.c:69
enum AVPixelFormat pix_fmt
Definition: dv_profile.h:50
ptrdiff_t size
Definition: opengl_enc.c:101
uint8_t flags
Definition: mxfenc.c:73
static void mxf_write_track(AVFormatContext *s, AVStream *st, enum MXFMetadataSetType type)
Definition: mxfenc.c:836
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:558
int av_reduce(int *dst_num, int *dst_den, int64_t num, int64_t den, int64_t max)
Reduce a fraction.
Definition: rational.c:35
struct AVPacketList * packet_buffer
This buffer is only needed when packets were already buffered but not decoded, for example to get the...
Definition: internal.h:78
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:216
static int interleave_packet(AVFormatContext *s, AVPacket *out, AVPacket *in, int flush)
Interleave an AVPacket correctly so it can be muxed.
Definition: mux.c:1188
#define AVFMT_FLAG_BITEXACT
When muxing, try to avoid writing any random/volatile data to the output.
Definition: avformat.h:1453
AVRational audio_edit_rate
Definition: mxfenc.c:381
unsigned slice_offset
offset of audio slice
Definition: mxfenc.c:75
enum ULIndex index
Definition: mxfenc.c:147
static const uint8_t index_table_segment_key[]
Definition: mxfenc.c:395
#define av_log(a,...)
#define AV_OPT_FLAG_ENCODING_PARAM
a generic parameter which can be set by the user for muxing or encoding
Definition: opt.h:276
const uint8_t * avpriv_find_start_code(const uint8_t *p, const uint8_t *end, uint32_t *state)
struct AVOutputFormat * oformat
The output container format.
Definition: avformat.h:1344
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
Definition: avcodec.h:3765
int av_timecode_init(AVTimecode *tc, AVRational rate, int flags, int frame_start, void *log_ctx)
Init a timecode struct with the passed parameters.
Definition: timecode.c:184
H.264 common definitions.
int store_user_comments
Definition: mxfenc.c:382
AudioInterleaveContext aic
Definition: mxfenc.c:80
int local_tag
Definition: mxfenc.c:68
static const AVClass mxf_opatom_muxer_class
Definition: mxfenc.c:2792
static const uint8_t umid_ul[]
Definition: mxfenc.c:386
int ff_parse_creation_time_metadata(AVFormatContext *s, int64_t *timestamp, int return_seconds)
Parse creation_time in AVFormatContext metadata if exists and warn if the parsing fails...
Definition: utils.c:5461
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: avcodec.h:214
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
AVDictionary * metadata
Metadata that applies to the whole file.
Definition: avformat.h:1543
int av_get_bits_per_sample(enum AVCodecID codec_id)
Return codec bits per sample.
Definition: utils.c:1636
static void mxf_write_system_item(AVFormatContext *s)
Definition: mxfenc.c:2318
#define EDIT_UNITS_PER_BODY
Definition: mxfenc.c:64
void(* write_desc)(AVFormatContext *, AVStream *)
Definition: mxfenc.c:101
static const uint8_t header_metadata_key[]
partial key for header metadata
Definition: mxfenc.c:405
static const uint8_t mxf_indirect_value_utf16le[]
Definition: mxfenc.c:1275
int profile
Definition: mxfenc.c:1897
Definition: mxf.h:42
#define AVERROR(e)
Definition: error.h:43
int ff_audio_rechunk_interleave(AVFormatContext *s, AVPacket *out, AVPacket *pkt, int flush, int(*get_packet)(AVFormatContext *, AVPacket *, AVPacket *, int), int(*compare_ts)(AVFormatContext *, AVPacket *, AVPacket *))
Rechunk audio PCM packets per AudioInterleaveContext->samples_per_frame and interleave them correctly...
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:190
static const uint8_t header_open_partition_key[]
Definition: mxfenc.c:397
static void mxf_write_preface(AVFormatContext *s)
Definition: mxfenc.c:666
const AVDVProfile * av_dv_frame_profile(const AVDVProfile *sys, const uint8_t *frame, unsigned buf_size)
Get a DV profile for the provided compressed frame.
Definition: dv_profile.c:300
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
enum AVMediaType codec_type
General type of the encoded data.
Definition: avcodec.h:3732
int header_written
Definition: mxfenc.c:361
static void mxf_write_random_index_pack(AVFormatContext *s)
Definition: mxfenc.c:2575
static void mxf_write_cdci_desc(AVFormatContext *s, AVStream *st)
Definition: mxfenc.c:1173
simple assert() macros that are a bit more flexible than ISO C assert().
static int mxf_write_opatom_body_partition(AVFormatContext *s)
Definition: mxfenc.c:2420
GLsizei GLsizei * length
Definition: opengl_enc.c:115
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_RL24
Definition: bytestream.h:87
static void mxf_write_metadata_key(AVIOContext *pb, unsigned int value)
Definition: mxfenc.c:618
GLsizei count
Definition: opengl_enc.c:109
#define MXF_COMMON_OPTIONS
Definition: mxfenc.c:2735
int color_siting
Definition: mxfenc.c:88
int order
interleaving order if dts are equal
Definition: mxfenc.c:84
static void mxf_write_d10_audio_packet(AVFormatContext *s, AVStream *st, AVPacket *pkt)
Definition: mxfenc.c:2388
static const uint8_t primer_pack_key[]
Definition: mxfenc.c:394
static const uint8_t system_metadata_pack_key[]
Definition: mxfenc.c:2315
static int mxf_get_essence_container_ul_index(enum AVCodecID id)
Definition: mxfenc.c:577
static struct tm * gmtime_r(const time_t *clock, struct tm *result)
Definition: time_internal.h:26
int av_reallocp_array(void *ptr, size_t nmemb, size_t size)
Definition: mem.c:205
static void mxf_write_content_storage(AVFormatContext *s)
Definition: mxfenc.c:811
static int klv_ber_length(uint64_t len)
Definition: mxfenc.c:533
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:1381
int block_align
Audio only.
Definition: avcodec.h:3853
int seekable
A combination of AVIO_SEEKABLE_ flags or 0 when the stream is not seekable.
Definition: avio.h:261
static int mxf_parse_h264_frame(AVFormatContext *s, AVStream *st, AVPacket *pkt, MXFIndexEntry *e)
Definition: mxfenc.c:1917
int void avio_flush(AVIOContext *s)
Force flushing of buffered data.
Definition: aviobuf.c:236
static const UID mxf_wav_descriptor_key
Definition: mxfenc.c:1072
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition: avassert.h:53
void ffio_fill(AVIOContext *s, int b, int count)
Definition: aviobuf.c:202
#define KAG_SIZE
Definition: mxfenc.c:65
static const uint8_t op1a_ul[]
complete key for operation pattern, partitions, and primer pack
Definition: mxfenc.c:391
uint8_t interlaced
Definition: mxfenc.c:1898
static int mxf_parse_dnxhd_frame(AVFormatContext *s, AVStream *st, AVPacket *pkt)
Definition: mxfenc.c:1725
uint64_t * body_partition_offset
Definition: mxfenc.c:367
typedef void(APIENTRY *FF_PFNGLACTIVETEXTUREPROC)(GLenum texture)
GLsizei GLboolean const GLfloat * value
Definition: opengl_enc.c:109
static int write_trailer(AVFormatContext *s1)
Definition: v4l2enc.c:94
ITU-R 601, SMPTE 274M 296M S314M(DV 4:1:1), mpeg2 4:2:2.
Definition: pixfmt.h:515
static const AVOption mxf_options[]
Definition: mxfenc.c:2755
ULIndex
Definition: mxfenc.c:104
void avio_wb24(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:485
static const AVClass mxf_muxer_class
Definition: mxfenc.c:2762
const char * name
Definition: avformat.h:507
static int mxf_write_partition(AVFormatContext *s, int bodysid, int indexsid, const uint8_t *key, int write_metadata)
Definition: mxfenc.c:1620
static void mxf_write_local_tag_utf16(AVIOContext *pb, int tag, const char *value)
Definition: mxfenc.c:758
static int mxf_write_header_metadata_sets(AVFormatContext *s)
Definition: mxfenc.c:1433
AVDictionary * metadata
Definition: avformat.h:936
AVStream * timecode_track
Definition: mxfenc.c:372
static const UID mxf_aes3_descriptor_key
Definition: mxfenc.c:1073
static struct @271 state
const MXFCodecUL ff_mxf_data_definition_uls[]
SMPTE RP224 http://www.smpte-ra.org/mdd/index.html.
Definition: mxf.c:28
static const uint8_t system_metadata_package_set_key[]
Definition: mxfenc.c:2316
int edit_unit_byte_count
fixed edit unit byte count
Definition: mxfenc.c:374
preferred ID for MPEG-1/2 video decoding
Definition: avcodec.h:219
#define FF_ARRAY_ELEMS(a)
Stream structure.
Definition: avformat.h:872
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
#define AVFMT_NOTIMESTAMPS
Format does not need / have any timestamps.
Definition: avformat.h:469
uint32_t instance_number
Definition: mxfenc.c:376
static void mxf_write_wav_common(AVFormatContext *s, AVStream *st, const UID key, unsigned size)
Definition: mxfenc.c:1246
static int mxf_write_user_comments(AVFormatContext *s, const AVDictionary *m)
Definition: mxfenc.c:1306
int signal_standard
Definition: mxfenc.c:379
#define AVIO_SEEKABLE_NORMAL
Seeking works like for a local file.
Definition: avio.h:40
static void mxf_write_local_tag(AVIOContext *pb, int size, int tag)
Definition: mxfenc.c:612
static void mxf_write_aes3_desc(AVFormatContext *s, AVStream *st)
Definition: mxfenc.c:1265
int frame_size
Definition: mxfenc.c:1896
int last_indexed_edit_unit
Definition: mxfenc.c:366
static const AVClass mxf_d10_muxer_class
Definition: mxfenc.c:2778
static int mxf_parse_mpeg2_frame(AVFormatContext *s, AVStream *st, AVPacket *pkt, MXFIndexEntry *e)
Definition: mxfenc.c:2017
const UID * codec_ul
Definition: mxfenc.c:83
Timecode helpers header.
AVIOContext * pb
I/O context.
Definition: avformat.h:1367
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:81
void avio_w8(AVIOContext *s, int b)
Definition: aviobuf.c:194
int samples_per_frame[6]
Definition: mxf.h:75
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:590
static void mxf_write_essence_container_refs(AVFormatContext *s)
Definition: mxfenc.c:649
static unsigned int seed
Definition: videogen.c:78
static const UID * mxf_get_mpeg2_codec_ul(AVCodecParameters *par)
Definition: mxfenc.c:1997
AVRational rate
frame rate in rational form
Definition: timecode.h:44
void * buf
Definition: avisynth_c.h:690
int closed_gop
gop is closed, used in mpeg-2 frame parsing
Definition: mxfenc.c:93
GLint GLenum type
Definition: opengl_enc.c:105
uint64_t timestamp
timestamp, as year(16),month(8),day(8),hour(8),minutes(8),msec/4(8)
Definition: mxfenc.c:364
#define AV_STRINGIFY(s)
Definition: macros.h:36
Describe the class of an AVClass context structure.
Definition: log.h:67
AVRational aspect_ratio
display aspect ratio
Definition: mxfenc.c:92
static const MXFLocalTagPair mxf_local_tag_batch[]
SMPTE RP210 http://www.smpte-ra.org/mdd/index.html https://smpte-ra.org/sites/default/files/Labels.xml.
Definition: mxfenc.c:412
Rational number (pair of numerator and denominator).
Definition: rational.h:58
int sample_size
size of one sample all channels included
uint16_t temporal_ref
Definition: mxfenc.c:76
static uint64_t mxf_utf16len(const char *utf8_str)
Definition: mxfenc.c:717
static void mxf_write_primer_pack(AVFormatContext *s)
Definition: mxfenc.c:586
int avio_put_str16le(AVIOContext *s, const char *str)
Convert an UTF-8 string to UTF-16LE and write it.
MXFIndexEntry * index_entries
Definition: mxfenc.c:362
AVRational time_base
Definition: mxfenc.c:360
AVClass * av_class
Definition: mxfenc.c:357
Definition: mxf.h:67
int video_bit_rate
Definition: mxfenc.c:94
static void mxf_write_timecode_component(AVFormatContext *s, AVStream *st, enum MXFMetadataSetType type)
Definition: mxfenc.c:936
static const uint8_t uuid_base[]
Definition: mxfenc.c:385
int channel_count
Definition: mxfenc.c:378
Definition: mxf.h:48
#define LIBAVFORMAT_VERSION
Definition: version.h:41
void avio_wb16(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:473
static const MXFContainerEssenceEntry mxf_essence_container_uls[]
Definition: mxfenc.c:165
enum AVChromaLocation ff_choose_chroma_location(AVFormatContext *s, AVStream *st)
Chooses a timebase for muxing the specified stream.
Definition: mux.c:118
static av_always_inline AVRational av_inv_q(AVRational q)
Invert a rational.
Definition: rational.h:159
static const MXFLocalTagPair mxf_user_comments_local_tag[]
Definition: mxfenc.c:505
static int mxf_interleave_get_packet(AVFormatContext *s, AVPacket *out, AVPacket *pkt, int flush)
Definition: mxfenc.c:2665
int sample_rate
Audio only.
Definition: avcodec.h:3846
int64_t footer_partition_offset
Definition: mxfenc.c:358
static const uint8_t smpte_12m_timecode_track_data_ul[]
Definition: mxfenc.c:882
Main libavformat public API header.
static void mxf_write_identification(AVFormatContext *s)
Definition: mxfenc.c:771
void ff_audio_interleave_close(AVFormatContext *s)
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:62
Definition: mxf.h:37
static int mxf_compare_timestamps(AVFormatContext *s, AVPacket *next, AVPacket *pkt)
Definition: mxfenc.c:2720
static void mxf_write_package(AVFormatContext *s, enum MXFMetadataSetType type, const char *package_name)
Definition: mxfenc.c:1324
common internal api header.
static const struct @236 mxf_h264_codec_uls[]
static const struct @235 mxf_essence_mappings[]
struct AVPacketList * next
Definition: avformat.h:1964
if(ret< 0)
Definition: vf_mcdeint.c:279
static const uint8_t footer_partition_key[]
Definition: mxfenc.c:393
static int mxf_write_essence_container_data(AVFormatContext *s)
Definition: mxfenc.c:1411
static void write_metadata(AVFormatContext *s, unsigned int ts)
Definition: flvenc.c:269
static double c[64]
int profile
Codec-specific bitstream restrictions that the stream conforms to.
Definition: avcodec.h:3796
void av_init_packet(AVPacket *pkt)
Initialize optional fields of a packet with default values.
Definition: avpacket.c:33
char * key
Definition: dict.h:86
int den
Denominator.
Definition: rational.h:60
#define AVERROR_UNKNOWN
Unknown error, typically from an external library.
Definition: error.h:71
static const MXFCodecUL * mxf_get_data_definition_ul(int type)
Definition: mxfenc.c:634
int field_dominance
tff=1, bff=2
Definition: mxfenc.c:86
static const AVOption d10_options[]
Definition: mxfenc.c:2769
struct AVPacketList * packet_buffer_end
Definition: internal.h:79
int last_key_index
index of last key frame
Definition: mxfenc.c:369
char * value
Definition: dict.h:87
int len
static void mxf_write_generic_sound_common(AVFormatContext *s, AVStream *st, const UID key, unsigned size)
Definition: mxfenc.c:1201
void * priv_data
Format private data.
Definition: avformat.h:1353
static void mxf_gen_umid(AVFormatContext *s)
Definition: mxfenc.c:2089
static void write_header(FFV1Context *f)
Definition: ffv1enc.c:337
uint8_t umid[16]
unique material identifier
Definition: mxfenc.c:377
static void mxf_write_multi_descriptor(AVFormatContext *s)
Definition: mxfenc.c:1000
static void mxf_write_wav_desc(AVFormatContext *s, AVStream *st)
Definition: mxfenc.c:1260
int channels
Audio only.
Definition: avcodec.h:3842
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
Definition: avcodec.h:1398
void avio_wb32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:375
FILE * out
Definition: movenc.c:54
#define av_freep(p)
static const AVOption opatom_options[]
Definition: mxfenc.c:2785
void INT64 start
Definition: avisynth_c.h:690
#define AV_DICT_IGNORE_SUFFIX
Return first entry in a dictionary whose first part corresponds to the search key, ignoring the suffix of the found key string.
Definition: dict.h:70
unsigned edit_units_count
Definition: mxfenc.c:363
uint32_t av_get_random_seed(void)
Get a seed to use in conjunction with random functions.
Definition: random_seed.c:120
static void mxf_write_sequence(AVFormatContext *s, AVStream *st, enum MXFMetadataSetType type)
Definition: mxfenc.c:908
MPEG-1 4:2:0, JPEG 4:2:0, H.263 4:2:0.
Definition: pixfmt.h:514
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1014
UID uid
Definition: mxf.h:68
int stream_index
Definition: avcodec.h:1401
static const uint8_t opatom_ul[]
Definition: mxfenc.c:392
#define DESCRIPTOR_COUNT(essence_container_count)
Definition: mxfenc.c:646
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avformat.h:901
int depth
Number of bits in the component.
Definition: pixdesc.h:58
static int mxf_write_opatom_packet(AVFormatContext *s, AVPacket *pkt, MXFIndexEntry *ie)
Definition: mxfenc.c:2441
uint32_t flags
flags such as drop frame, +24 hours support, ...
Definition: timecode.h:43
static void mxf_write_mpegvideo_desc(AVFormatContext *s, AVStream *st)
Definition: mxfenc.c:1178
static void mxf_write_umid(AVFormatContext *s, int type)
Definition: mxfenc.c:518
This structure stores compressed data.
Definition: avcodec.h:1376
static int mxf_parse_dv_frame(AVFormatContext *s, AVStream *st, AVPacket *pkt)
Definition: mxfenc.c:1810
int essence_container_count
Definition: mxfenc.c:359
static int mxf_write_tagged_value(AVFormatContext *s, const char *name, const char *value)
Definition: mxfenc.c:1277
static const uint8_t random_index_pack_key[]
Definition: mxfenc.c:396
static void mxf_write_generic_sound_desc(AVFormatContext *s, AVStream *st)
Definition: mxfenc.c:1270
static int mxf_interleave(AVFormatContext *s, AVPacket *out, AVPacket *pkt, int flush)
Definition: mxfenc.c:2729
struct AVPacketList * last_in_packet_buffer
last packet in packet_buffer for this stream when muxing.
Definition: avformat.h:1091
AVTimecode tc
timecode context
Definition: mxfenc.c:371
static int mxf_write_footer(AVFormatContext *s)
Definition: mxfenc.c:2602
uint8_t pi<< 24) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_U8,(uint64_t)((*(constuint8_t *) pi-0x80U))<< 56) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_S16,(uint64_t)(*(constint16_t *) pi)<< 48) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_S32,(uint64_t)(*(constint32_t *) pi)<< 32) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S64,(*(constint64_t *) pi >>56)+0x80) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S64,*(constint64_t *) pi *(1.0f/(INT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S64,*(constint64_t *) pi *(1.0/(INT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_FLT, llrintf(*(constfloat *) pi *(INT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31)))) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_DBL, llrint(*(constdouble *) pi *(INT64_C(1)<< 63)))#defineFMT_PAIR_FUNC(out, in) staticconv_func_type *constfmt_pair_to_conv_functions[AV_SAMPLE_FMT_NB *AV_SAMPLE_FMT_NB]={FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S64),};staticvoidcpy1(uint8_t **dst, constuint8_t **src, intlen){memcpy(*dst,*src, len);}staticvoidcpy2(uint8_t **dst, constuint8_t **src, intlen){memcpy(*dst,*src, 2 *len);}staticvoidcpy4(uint8_t **dst, constuint8_t **src, intlen){memcpy(*dst,*src, 4 *len);}staticvoidcpy8(uint8_t **dst, constuint8_t **src, intlen){memcpy(*dst,*src, 8 *len);}AudioConvert *swri_audio_convert_alloc(enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, constint *ch_map, intflags){AudioConvert *ctx;conv_func_type *f=fmt_pair_to_conv_functions[av_get_packed_sample_fmt(out_fmt)+AV_SAMPLE_FMT_NB *av_get_packed_sample_fmt(in_fmt)];if(!f) returnNULL;ctx=av_mallocz(sizeof(*ctx));if(!ctx) returnNULL;if(channels==1){in_fmt=av_get_planar_sample_fmt(in_fmt);out_fmt=av_get_planar_sample_fmt(out_fmt);}ctx->channels=channels;ctx->conv_f=f;ctx->ch_map=ch_map;if(in_fmt==AV_SAMPLE_FMT_U8||in_fmt==AV_SAMPLE_FMT_U8P) memset(ctx->silence, 0x80, sizeof(ctx->silence));if(out_fmt==in_fmt &&!ch_map){switch(av_get_bytes_per_sample(in_fmt)){case1:ctx->simd_f=cpy1;break;case2:ctx->simd_f=cpy2;break;case4:ctx->simd_f=cpy4;break;case8:ctx->simd_f=cpy8;break;}}if(HAVE_X86ASM &&1) swri_audio_convert_init_x86(ctx, out_fmt, in_fmt, channels);if(ARCH_ARM) swri_audio_convert_init_arm(ctx, out_fmt, in_fmt, channels);if(ARCH_AARCH64) swri_audio_convert_init_aarch64(ctx, out_fmt, in_fmt, channels);returnctx;}voidswri_audio_convert_free(AudioConvert **ctx){av_freep(ctx);}intswri_audio_convert(AudioConvert *ctx, AudioData *out, AudioData *in, intlen){intch;intoff=0;constintos=(out->planar?1:out->ch_count)*out->bps;unsignedmisaligned=0;av_assert0(ctx->channels==out->ch_count);if(ctx->in_simd_align_mask){intplanes=in->planar?in->ch_count:1;unsignedm=0;for(ch=0;ch< planes;ch++) m|=(intptr_t) in->ch[ch];misaligned|=m &ctx->in_simd_align_mask;}if(ctx->out_simd_align_mask){intplanes=out->planar?out->ch_count:1;unsignedm=0;for(ch=0;ch< planes;ch++) m|=(intptr_t) out->ch[ch];misaligned|=m &ctx->out_simd_align_mask;}if(ctx->simd_f &&!ctx->ch_map &&!misaligned){off=len &~15;av_assert1(off >=0);av_assert1(off<=len);av_assert2(ctx->channels==SWR_CH_MAX||!in->ch[ctx->channels]);if(off >0){if(out->planar==in->planar){intplanes=out->planar?out->ch_count:1;for(ch=0;ch< planes;ch++){ctx->simd_f(out-> ch ch
Definition: audioconvert.c:56
const char * name
Definition: opengl_enc.c:103
uint32_t av_timecode_get_smpte_from_framenum(const AVTimecode *tc, int framenum)
Convert frame number to SMPTE 12M binary representation.
Definition: timecode.c:55
AVOutputFormat ff_mxf_opatom_muxer
Definition: mxfenc.c:2830