FFmpeg
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"
48 #include "libavutil/pixdesc.h"
50 #include "libavcodec/bytestream.h"
51 #include "libavcodec/dnxhddata.h"
52 #include "libavcodec/dv_profile.h"
53 #include "libavcodec/h264_ps.h"
54 #include "libavcodec/golomb.h"
55 #include "libavcodec/internal.h"
56 #include "avformat.h"
57 #include "avio_internal.h"
58 #include "internal.h"
59 #include "avc.h"
60 #include "mxf.h"
61 #include "config.h"
62 
65 
66 #define EDIT_UNITS_PER_BODY 250
67 #define KAG_SIZE 512
68 
69 typedef struct MXFIndexEntry {
70  uint64_t offset;
71  unsigned slice_offset; ///< offset of audio slice
72  uint16_t temporal_ref;
75 
76 typedef struct MXFStreamContext {
77  int64_t pkt_cnt; ///< pkt counter for muxed packets
79  int index; ///< index in mxf_essence_container_uls table
80  const UID *codec_ul;
81  const UID *container_ul;
82  int order; ///< interleaving order if dts are equal
83  int interlaced; ///< whether picture is interlaced
84  int field_dominance; ///< tff=1, bff=2
91  AVRational aspect_ratio; ///< display aspect ratio
92  int closed_gop; ///< gop is closed, used in mpeg-2 frame parsing
95  int frame_size; ///< frame size in bytes
96  int seq_closed_gop; ///< all gops in sequence are closed, used in mpeg-2 descriptor
97  int max_gop; ///< maximum gop size, used by mpeg-2 descriptor
98  int b_picture_count; ///< maximum number of consecutive b pictures, used in mpeg-2 descriptor
99  int low_delay; ///< low delay, used in mpeg-2 descriptor
102 
103 typedef struct MXFContainerEssenceEntry {
107  void (*write_desc)(AVFormatContext *, AVStream *);
109 
110 typedef struct MXFPackage {
111  char *name;
113  int instance;
114  struct MXFPackage *ref;
115 } MXFPackage;
116 
117 enum ULIndex {
129 };
130 
131 static const struct {
132  enum AVCodecID id;
134 } mxf_essence_mappings[] = {
137  { AV_CODEC_ID_PCM_S16LE, INDEX_AES3 },
143  { AV_CODEC_ID_NONE }
144 };
145 
146 static void mxf_write_wav_desc(AVFormatContext *s, AVStream *st);
147 static void mxf_write_aes3_desc(AVFormatContext *s, AVStream *st);
149 static void mxf_write_h264_desc(AVFormatContext *s, AVStream *st);
150 static void mxf_write_cdci_desc(AVFormatContext *s, AVStream *st);
153 
155  { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x02,0x0D,0x01,0x03,0x01,0x02,0x04,0x60,0x01 },
156  { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x15,0x01,0x05,0x00 },
157  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x00,0x00,0x00 },
159  { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x06,0x03,0x00 },
160  { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x16,0x01,0x03,0x00 },
161  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 },
163  { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x06,0x01,0x00 },
164  { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x16,0x01,0x01,0x00 },
165  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 },
167  // D-10 Video
168  { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x01,0x01 },
169  { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x05,0x01,0x01,0x00 },
170  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x01,0x02,0x01,0x01 },
172  // D-10 Audio
173  { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x01,0x01 },
174  { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x06,0x01,0x10,0x00 },
175  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 },
177  // DV
178  { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x02,0x7F,0x01 },
179  { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x18,0x01,0x01,0x00 },
180  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x02,0x00,0x00,0x00 },
182  // DNxHD
183  { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x11,0x01,0x00 },
184  { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x15,0x01,0x05,0x00 },
185  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0A,0x04,0x01,0x02,0x02,0x71,0x01,0x00,0x00 },
187  // JPEG2000
188  { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x07,0x0d,0x01,0x03,0x01,0x02,0x0c,0x01,0x00 },
189  { 0x06,0x0e,0x2b,0x34,0x01,0x02,0x01,0x01,0x0d,0x01,0x03,0x01,0x15,0x01,0x08,0x00 },
190  { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x07,0x04,0x01,0x02,0x02,0x03,0x01,0x01,0x00 },
192  // H.264
193  { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x0D,0x01,0x03,0x01,0x02,0x10,0x60,0x01 },
194  { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x15,0x01,0x05,0x00 },
195  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x00,0x00,0x00 },
197  // S436M ANC
198  { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x0D,0x01,0x03,0x01,0x02,0x0e,0x00,0x00 },
199  { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x17,0x01,0x02,0x00 },
200  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x01,0x5C,0x00 },
202  // ProRes
203  { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0d,0x0d,0x01,0x03,0x01,0x02,0x1c,0x01,0x00 },
204  { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0d,0x01,0x03,0x01,0x15,0x01,0x17,0x00 },
205  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0d,0x04,0x01,0x02,0x02,0x03,0x06,0x03,0x00 },
207  { { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 },
208  { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 },
209  { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 },
210  NULL },
211 };
212 
213 static const UID mxf_d10_codec_uls[] = {
214  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x01,0x02,0x01,0x01 }, // D-10 625/50 PAL 50mb/s
215  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x01,0x02,0x01,0x02 }, // D-10 525/50 NTSC 50mb/s
216  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x01,0x02,0x01,0x03 }, // D-10 625/50 PAL 40mb/s
217  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x01,0x02,0x01,0x04 }, // D-10 525/50 NTSC 40mb/s
218  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x01,0x02,0x01,0x05 }, // D-10 625/50 PAL 30mb/s
219  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x01,0x02,0x01,0x06 }, // D-10 525/50 NTSC 30mb/s
220 };
221 
222 static const UID mxf_d10_container_uls[] = {
223  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x01,0x01 }, // D-10 625/50 PAL 50mb/s
224  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x02,0x01 }, // D-10 525/50 NTSC 50mb/s
225  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x03,0x01 }, // D-10 625/50 PAL 40mb/s
226  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x04,0x01 }, // D-10 525/50 NTSC 40mb/s
227  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x05,0x01 }, // D-10 625/50 PAL 30mb/s
228  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x06,0x01 }, // D-10 525/50 NTSC 30mb/s
229 };
230 
231 static const uint8_t uuid_base[] = { 0xAD,0xAB,0x44,0x24,0x2f,0x25,0x4d,0xc7,0x92,0xff,0x29,0xbd };
232 static const uint8_t umid_ul[] = { 0x06,0x0A,0x2B,0x34,0x01,0x01,0x01,0x05,0x01,0x01,0x0D,0x00,0x13 };
233 
234 /**
235  * complete key for operation pattern, partitions, and primer pack
236  */
237 static const uint8_t op1a_ul[] = { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x02,0x01,0x01,0x01,0x09,0x00 };
238 static const uint8_t opatom_ul[] = { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x02,0x0D,0x01,0x02,0x01,0x10,0x03,0x00,0x00 };
239 static const uint8_t footer_partition_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0D,0x01,0x02,0x01,0x01,0x04,0x04,0x00 }; // ClosedComplete
240 static const uint8_t primer_pack_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0D,0x01,0x02,0x01,0x01,0x05,0x01,0x00 };
241 static const uint8_t index_table_segment_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x10,0x01,0x00 };
242 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
243 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
244 static const uint8_t klv_fill_key[] = { 0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x03,0x01,0x02,0x10,0x01,0x00,0x00,0x00 };
245 static const uint8_t body_partition_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0D,0x01,0x02,0x01,0x01,0x03,0x04,0x00 }; // ClosedComplete
246 
247 /**
248  * partial key for header metadata
249  */
250 static const uint8_t header_metadata_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0D,0x01,0x01,0x01,0x01 };
251 static const uint8_t multiple_desc_ul[] = { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x0D,0x01,0x03,0x01,0x02,0x7F,0x01,0x00 };
252 
253 /**
254  * SMPTE RP210 http://www.smpte-ra.org/mdd/index.html
255  * https://smpte-ra.org/sites/default/files/Labels.xml
256  */
258  // preface set
259  { 0x3C0A, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x01,0x01,0x15,0x02,0x00,0x00,0x00,0x00}}, /* Instance UID */
260  { 0x3B02, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x10,0x02,0x04,0x00,0x00}}, /* Last Modified Date */
261  { 0x3B05, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x03,0x01,0x02,0x01,0x05,0x00,0x00,0x00}}, /* Version */
262  { 0x3B07, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x03,0x01,0x02,0x01,0x04,0x00,0x00,0x00}}, /* Object Model Version */
263  { 0x3B06, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x06,0x04,0x00,0x00}}, /* Identifications reference */
264  { 0x3B03, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x02,0x01,0x00,0x00}}, /* Content Storage reference */
265  { 0x3B09, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x01,0x02,0x02,0x03,0x00,0x00,0x00,0x00}}, /* Operational Pattern UL */
266  { 0x3B0A, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x01,0x02,0x02,0x10,0x02,0x01,0x00,0x00}}, /* Essence Containers UL batch */
267  { 0x3B0B, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x01,0x02,0x02,0x10,0x02,0x02,0x00,0x00}}, /* DM Schemes UL batch */
268  // Identification
269  { 0x3C09, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x20,0x07,0x01,0x01,0x00,0x00,0x00}}, /* This Generation UID */
270  { 0x3C01, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x20,0x07,0x01,0x02,0x01,0x00,0x00}}, /* Company Name */
271  { 0x3C02, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x20,0x07,0x01,0x03,0x01,0x00,0x00}}, /* Product Name */
272  { 0x3C03, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x20,0x07,0x01,0x04,0x00,0x00,0x00}}, /* Product Version */
273  { 0x3C04, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x20,0x07,0x01,0x05,0x01,0x00,0x00}}, /* Version String */
274  { 0x3C05, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x20,0x07,0x01,0x07,0x00,0x00,0x00}}, /* Product ID */
275  { 0x3C06, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x10,0x02,0x03,0x00,0x00}}, /* Modification Date */
276  { 0x3C07, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x20,0x07,0x01,0x0A,0x00,0x00,0x00}}, /* Toolkit Version */
277  { 0x3C08, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x20,0x07,0x01,0x06,0x01,0x00,0x00}}, /* Platform */
278  // Content Storage
279  { 0x1901, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x05,0x01,0x00,0x00}}, /* Package strong reference batch */
280  { 0x1902, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x05,0x02,0x00,0x00}}, /* Package strong reference batch */
281  // Essence Container Data
282  { 0x2701, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x06,0x01,0x00,0x00,0x00}}, /* Linked Package UID */
283  { 0x3F07, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x01,0x03,0x04,0x04,0x00,0x00,0x00,0x00}}, /* BodySID */
284  // Package
285  { 0x4401, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x01,0x01,0x15,0x10,0x00,0x00,0x00,0x00}}, /* Package UID */
286  { 0x4405, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x10,0x01,0x03,0x00,0x00}}, /* Package Creation Date */
287  { 0x4404, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x10,0x02,0x05,0x00,0x00}}, /* Package Modified Date */
288  { 0x4402, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x01,0x03,0x03,0x02,0x01,0x00,0x00,0x00}}, /* Package Name */
289  { 0x4403, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x06,0x05,0x00,0x00}}, /* Tracks Strong reference array */
290  { 0x4701, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x02,0x03,0x00,0x00}}, /* Descriptor */
291  // Track
292  { 0x4801, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x01,0x07,0x01,0x01,0x00,0x00,0x00,0x00}}, /* Track ID */
293  { 0x4804, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x01,0x04,0x01,0x03,0x00,0x00,0x00,0x00}}, /* Track Number */
294  { 0x4B01, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x30,0x04,0x05,0x00,0x00,0x00,0x00}}, /* Edit Rate */
295  { 0x4B02, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x03,0x01,0x03,0x00,0x00}}, /* Origin */
296  { 0x4803, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x02,0x04,0x00,0x00}}, /* Sequence reference */
297  // Sequence
298  { 0x0201, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x07,0x01,0x00,0x00,0x00,0x00,0x00}}, /* Data Definition UL */
299  { 0x0202, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x02,0x01,0x01,0x03,0x00,0x00}}, /* Duration */
300  { 0x1001, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x06,0x09,0x00,0x00}}, /* Structural Components reference array */
301  // Source Clip
302  { 0x1201, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x03,0x01,0x04,0x00,0x00}}, /* Start position */
303  { 0x1101, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x03,0x01,0x00,0x00,0x00}}, /* SourcePackageID */
304  { 0x1102, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x03,0x02,0x00,0x00,0x00}}, /* SourceTrackID */
305  // Timecode Component
306  { 0x1501, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x03,0x01,0x05,0x00,0x00}}, /* Start Time Code */
307  { 0x1502, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x04,0x01,0x01,0x02,0x06,0x00,0x00}}, /* Rounded Time Code Base */
308  { 0x1503, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x04,0x01,0x01,0x05,0x00,0x00,0x00}}, /* Drop Frame */
309  // File Descriptor
310  { 0x3F01, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x06,0x01,0x01,0x04,0x06,0x0B,0x00,0x00}}, /* Sub Descriptors reference array */
311  { 0x3006, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x06,0x01,0x01,0x03,0x05,0x00,0x00,0x00}}, /* Linked Track ID */
312  { 0x3001, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x06,0x01,0x01,0x00,0x00,0x00,0x00}}, /* SampleRate */
313  { 0x3002, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x06,0x01,0x02,0x00,0x00,0x00,0x00}}, /* ContainerDuration */
314  { 0x3004, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x01,0x02,0x00,0x00}}, /* Essence Container */
315  // Generic Picture Essence Descriptor
316  { 0x320C, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x03,0x01,0x04,0x00,0x00,0x00}}, /* Frame Layout */
317  { 0x320D, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x01,0x03,0x02,0x05,0x00,0x00,0x00}}, /* Video Line Map */
318  { 0x3203, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x02,0x02,0x00,0x00,0x00}}, /* Stored Width */
319  { 0x3202, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x02,0x01,0x00,0x00,0x00}}, /* Stored Height */
320  { 0x3216, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x01,0x03,0x02,0x08,0x00,0x00,0x00}}, /* Stored F2 Offset */
321  { 0x3205, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x01,0x08,0x00,0x00,0x00}}, /* Sampled Width */
322  { 0x3204, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x01,0x07,0x00,0x00,0x00}}, /* Sampled Height */
323  { 0x3206, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x01,0x09,0x00,0x00,0x00}}, /* Sampled X Offset */
324  { 0x3207, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x01,0x0A,0x00,0x00,0x00}}, /* Sampled Y Offset */
325  { 0x3209, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x01,0x0C,0x00,0x00,0x00}}, /* Display Width */
326  { 0x3208, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x01,0x0B,0x00,0x00,0x00}}, /* Display Height */
327  { 0x320A, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x01,0x0D,0x00,0x00,0x00}}, /* Display X offset */
328  { 0x320B, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x01,0x0E,0x00,0x00,0x00}}, /* Presentation Y offset */
329  { 0x3217, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x01,0x03,0x02,0x07,0x00,0x00,0x00}}, /* Display F2 offset */
330  { 0x320E, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x01,0x01,0x01,0x00,0x00,0x00}}, /* Aspect Ratio */
331  { 0x3210, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x01,0x02,0x01,0x01,0x01,0x02,0x00}}, /* Transfer characteristic */
332  { 0x321A, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x01,0x02,0x01,0x01,0x03,0x01,0x00}}, /* Coding Equations (color space) */
333  { 0x3219, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x09,0x04,0x01,0x02,0x01,0x01,0x06,0x01,0x00}}, /* Color Primaries */
334  { 0x3213, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x18,0x01,0x02,0x00,0x00,0x00,0x00}}, /* Image Start Offset */
335  { 0x3214, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x18,0x01,0x03,0x00,0x00,0x00,0x00}}, /* Image End Offset */
336  { 0x3201, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x01,0x06,0x01,0x00,0x00,0x00,0x00}}, /* Picture Essence Coding */
337  { 0x3212, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x01,0x03,0x01,0x06,0x00,0x00,0x00}}, /* Field Dominance (Opt) */
338  { 0x3215, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x05,0x01,0x13,0x00,0x00,0x00,0x00}}, /* Signal Standard */
339  // CDCI Picture Essence Descriptor
340  { 0x3301, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x01,0x05,0x03,0x0A,0x00,0x00,0x00}}, /* Component Depth */
341  { 0x3302, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x01,0x05,0x00,0x00,0x00}}, /* Horizontal Subsampling */
342  { 0x3308, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x01,0x05,0x01,0x10,0x00,0x00,0x00}}, /* Vertical Subsampling */
343  { 0x3303, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x01,0x06,0x00,0x00,0x00}}, /* Color Siting */
344  { 0x3307, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x18,0x01,0x04,0x00,0x00,0x00,0x00}}, /* Padding Bits */
345  { 0x3304, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x03,0x03,0x00,0x00,0x00}}, /* Black Ref level */
346  { 0x3305, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x03,0x04,0x00,0x00,0x00}}, /* White Ref level */
347  { 0x3306, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x01,0x05,0x03,0x05,0x00,0x00,0x00}}, /* Color Range */
348  // Generic Sound Essence Descriptor
349  { 0x3D02, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x04,0x02,0x03,0x01,0x04,0x00,0x00,0x00}}, /* Locked/Unlocked */
350  { 0x3D03, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x02,0x03,0x01,0x01,0x01,0x00,0x00}}, /* Audio sampling rate */
351  { 0x3D04, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x02,0x01,0x01,0x03,0x00,0x00,0x00}}, /* Audio Ref Level */
352  { 0x3D07, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x02,0x01,0x01,0x04,0x00,0x00,0x00}}, /* ChannelCount */
353  { 0x3D01, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x04,0x02,0x03,0x03,0x04,0x00,0x00,0x00}}, /* Quantization bits */
354  { 0x3D06, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x02,0x04,0x02,0x00,0x00,0x00,0x00}}, /* Sound Essence Compression */
355  // Index Table Segment
356  { 0x3F0B, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x05,0x30,0x04,0x06,0x00,0x00,0x00,0x00}}, /* Index Edit Rate */
357  { 0x3F0C, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x07,0x02,0x01,0x03,0x01,0x0A,0x00,0x00}}, /* Index Start Position */
358  { 0x3F0D, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x07,0x02,0x02,0x01,0x01,0x02,0x00,0x00}}, /* Index Duration */
359  { 0x3F05, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x04,0x06,0x02,0x01,0x00,0x00,0x00,0x00}}, /* Edit Unit Byte Count */
360  { 0x3F06, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x01,0x03,0x04,0x05,0x00,0x00,0x00,0x00}}, /* IndexSID */
361  { 0x3F08, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x04,0x04,0x04,0x01,0x01,0x00,0x00,0x00}}, /* Slice Count */
362  { 0x3F09, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x04,0x04,0x01,0x06,0x00,0x00,0x00}}, /* Delta Entry Array */
363  { 0x3F0A, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x04,0x04,0x02,0x05,0x00,0x00,0x00}}, /* Index Entry Array */
364  // MPEG video Descriptor
365  { 0x8000, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x01,0x06,0x02,0x01,0x0B,0x00,0x00}}, /* BitRate */
366  { 0x8003, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x01,0x06,0x02,0x01,0x05,0x00,0x00}}, /* LowDelay */
367  { 0x8004, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x01,0x06,0x02,0x01,0x06,0x00,0x00}}, /* ClosedGOP */
368  { 0x8006, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x01,0x06,0x02,0x01,0x08,0x00,0x00}}, /* MaxGOP */
369  { 0x8007, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x01,0x06,0x02,0x01,0x0A,0x00,0x00}}, /* ProfileAndLevel */
370  { 0x8008, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x01,0x06,0x02,0x01,0x09,0x00,0x00}}, /* BPictureCount */
371  // Wave Audio Essence Descriptor
372  { 0x3D09, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x02,0x03,0x03,0x05,0x00,0x00,0x00}}, /* Average Bytes Per Second */
373  { 0x3D0A, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x02,0x03,0x02,0x01,0x00,0x00,0x00}}, /* Block Align */
374  // mxf_user_comments_local_tag
375  { 0x4406, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x03,0x02,0x01,0x02,0x0C,0x00,0x00,0x00}}, /* User Comments */
376  { 0x5001, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x03,0x02,0x01,0x02,0x09,0x01,0x00,0x00}}, /* Name */
377  { 0x5003, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x03,0x02,0x01,0x02,0x0A,0x01,0x00,0x00}}, /* Value */
378  // mxf_avc_subdescriptor_local_tags
379  { 0x8100, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x09,0x06,0x01,0x01,0x04,0x06,0x10,0x00,0x00}}, /* SubDescriptors */
380  { 0x8200, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x0E,0x04,0x01,0x06,0x06,0x01,0x0E,0x00,0x00}}, /* AVC Decoding Delay */
381  { 0x8201, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x0E,0x04,0x01,0x06,0x06,0x01,0x0A,0x00,0x00}}, /* AVC Profile */
382  { 0x8202, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x0E,0x04,0x01,0x06,0x06,0x01,0x0D,0x00,0x00}}, /* AVC Level */
383  // ff_mxf_mastering_display_local_tags
388 };
389 
390 #define MXF_NUM_TAGS FF_ARRAY_ELEMS(mxf_local_tag_batch)
391 
392 typedef struct MXFContext {
400  uint64_t timestamp; ///< timestamp, as year(16),month(8),day(8),hour(8),minutes(8),msec/4(8)
401  uint8_t slice_count; ///< index slice count minus 1 (1 if no audio, 0 otherwise)
405  int last_key_index; ///< index of last key frame
406  uint64_t duration;
407  AVTimecode tc; ///< timecode context
409  int timecode_base; ///< rounded time code base (25 or 30)
410  int edit_unit_byte_count; ///< fixed edit unit byte count
411  int content_package_rate; ///< content package rate in system element, see SMPTE 326M
412  uint64_t body_offset;
413  uint32_t instance_number;
414  uint8_t umid[16]; ///< unique material identifier
420  int track_instance_count; // used to generate MXFTrack uuids
421  int cbr_index; ///< use a constant bitrate index
422  uint8_t unused_tags[MXF_NUM_TAGS]; ///< local tags that we know will not be used
423 } MXFContext;
424 
426 {
427  avio_write(pb, uuid_base, 12);
428  avio_wb16(pb, type);
429  avio_wb16(pb, value);
430 }
431 
432 static void mxf_write_umid(AVFormatContext *s, int type)
433 {
434  MXFContext *mxf = s->priv_data;
435  avio_write(s->pb, umid_ul, 13);
436  avio_wb24(s->pb, mxf->instance_number);
437  avio_write(s->pb, mxf->umid, 15);
438  avio_w8(s->pb, type);
439 }
440 
441 static void mxf_write_refs_count(AVIOContext *pb, int ref_count)
442 {
443  avio_wb32(pb, ref_count);
444  avio_wb32(pb, 16);
445 }
446 
447 static int klv_ber_length(uint64_t len)
448 {
449  if (len < 128)
450  return 1;
451  else
452  return (av_log2(len) >> 3) + 2;
453 }
454 
455 static int klv_encode_ber_length(AVIOContext *pb, uint64_t len)
456 {
457  // Determine the best BER size
458  int size = klv_ber_length(len);
459  if (size == 1) {
460  //short form
461  avio_w8(pb, len);
462  return 1;
463  }
464 
465  size --;
466  // long form
467  avio_w8(pb, 0x80 + size);
468  while(size) {
469  size--;
470  avio_w8(pb, len >> 8 * size & 0xff);
471  }
472  return 0;
473 }
474 
476 {
477  avio_w8(pb, 0x80 + 3);
478  avio_wb24(pb, len);
479 }
480 
481 static void klv_encode_ber9_length(AVIOContext *pb, uint64_t len)
482 {
483  avio_w8(pb, 0x80 + 8);
484  avio_wb64(pb, len);
485 }
486 
487 /*
488  * Get essence container ul index
489  */
491 {
492  int i;
493  for (i = 0; mxf_essence_mappings[i].id; i++)
494  if (mxf_essence_mappings[i].id == id)
495  return mxf_essence_mappings[i].index;
496  return -1;
497 }
498 
500 {
501  for (int i = 0; i < MXF_NUM_TAGS; i++) {
502  if (mxf_local_tag_batch[i].local_tag == tag) {
503  return &mxf_local_tag_batch[i];
504  }
505  }
506 
507  // this assert can only be hit during development
508  av_assert0(0 && "you forgot to add your new tag to mxf_local_tag_batch");
509 }
510 
511 static void mxf_mark_tag_unused(MXFContext *mxf, int tag)
512 {
513  const MXFLocalTagPair *pair = mxf_lookup_local_tag(tag);
514  mxf->unused_tags[pair - mxf_local_tag_batch] = 1;
515 }
516 
518 {
519  MXFContext *mxf = s->priv_data;
520  AVIOContext *pb = s->pb;
521  int local_tag_number = MXF_NUM_TAGS, i;
522  int will_have_avc_tags = 0, will_have_mastering_tags = 0;
523 
524  for (i = 0; i < s->nb_streams; i++) {
525  MXFStreamContext *sc = s->streams[i]->priv_data;
526  if (s->streams[i]->codecpar->codec_id == AV_CODEC_ID_H264 && !sc->avc_intra) {
527  will_have_avc_tags = 1;
528  }
530  will_have_mastering_tags = 1;
531  }
532  }
533 
534  if (!mxf->store_user_comments) {
535  mxf_mark_tag_unused(mxf, 0x4406);
536  mxf_mark_tag_unused(mxf, 0x5001);
537  mxf_mark_tag_unused(mxf, 0x5003);
538  }
539 
540  if (!will_have_avc_tags) {
541  mxf_mark_tag_unused(mxf, 0x8100);
542  mxf_mark_tag_unused(mxf, 0x8200);
543  mxf_mark_tag_unused(mxf, 0x8201);
544  mxf_mark_tag_unused(mxf, 0x8202);
545  }
546 
547  if (!will_have_mastering_tags) {
548  mxf_mark_tag_unused(mxf, 0x8301);
549  mxf_mark_tag_unused(mxf, 0x8302);
550  mxf_mark_tag_unused(mxf, 0x8303);
551  mxf_mark_tag_unused(mxf, 0x8304);
552  }
553 
554  for (i = 0; i < MXF_NUM_TAGS; i++) {
555  if (mxf->unused_tags[i]) {
556  local_tag_number--;
557  }
558  }
559 
560  avio_write(pb, primer_pack_key, 16);
561  klv_encode_ber_length(pb, local_tag_number * 18 + 8);
562 
563  avio_wb32(pb, local_tag_number); // local_tag num
564  avio_wb32(pb, 18); // item size, always 18 according to the specs
565 
566  for (i = 0; i < MXF_NUM_TAGS; i++) {
567  if (mxf->unused_tags[i] == 0) {
568  avio_wb16(pb, mxf_local_tag_batch[i].local_tag);
569  avio_write(pb, mxf_local_tag_batch[i].uid, 16);
570  }
571  }
572 }
573 
574 static void mxf_write_local_tag(AVFormatContext *s, int size, int tag)
575 {
576  MXFContext *mxf = s->priv_data;
577  AVIOContext *pb = s->pb;
578  const MXFLocalTagPair *pair = mxf_lookup_local_tag(tag);
579 
580  // make sure the tag was not declared unnecessary upfront
581  av_assert0(mxf->unused_tags[pair - mxf_local_tag_batch] == 0);
582 
583  avio_wb16(pb, tag);
584  avio_wb16(pb, size);
585 }
586 
587 static void mxf_write_metadata_key(AVIOContext *pb, unsigned int value)
588 {
590  avio_wb24(pb, value);
591 }
592 
593 static const MXFCodecUL *mxf_get_codec_ul_by_id(const MXFCodecUL *uls, int id)
594 {
595  while (uls->uid[0]) {
596  if (id == uls->id)
597  break;
598  uls++;
599  }
600  return uls;
601 }
602 
603 //one EC -> one descriptor. N ECs -> MultipleDescriptor + N descriptors
604 #define DESCRIPTOR_COUNT(essence_container_count) \
605  (essence_container_count > 1 ? essence_container_count + 1 : essence_container_count)
606 
608 {
609  MXFContext *c = s->priv_data;
610  AVIOContext *pb = s->pb;
611  int i;
612 
614  av_log(s,AV_LOG_DEBUG, "essence container count:%d\n", c->essence_container_count);
615  for (i = 0; i < s->nb_streams; i++) {
616  MXFStreamContext *sc = s->streams[i]->priv_data;
617  // check first track of essence container type and only write it once
618  if (sc->track_essence_element_key[15] != 0)
619  continue;
620  avio_write(pb, *sc->container_ul, 16);
621  if (c->essence_container_count == 1)
622  break;
623  }
624 
625  if (c->essence_container_count > 1)
626  avio_write(pb, multiple_desc_ul, 16);
627 }
628 
630 {
631  MXFContext *mxf = s->priv_data;
632  AVIOContext *pb = s->pb;
633 
634  mxf_write_metadata_key(pb, 0x012f00);
635  PRINT_KEY(s, "preface key", pb->buf_ptr - 16);
637 
638  // write preface set uid
639  mxf_write_local_tag(s, 16, 0x3C0A);
640  mxf_write_uuid(pb, Preface, 0);
641  PRINT_KEY(s, "preface uid", pb->buf_ptr - 16);
642 
643  // last modified date
644  mxf_write_local_tag(s, 8, 0x3B02);
645  avio_wb64(pb, mxf->timestamp);
646 
647  // write version
648  mxf_write_local_tag(s, 2, 0x3B05);
649  avio_wb16(pb, 259); // v1.3
650 
651  // Object Model Version
652  mxf_write_local_tag(s, 4, 0x3B07);
653  avio_wb32(pb, 1);
654 
655  // write identification_refs
656  mxf_write_local_tag(s, 16 + 8, 0x3B06);
657  mxf_write_refs_count(pb, 1);
659 
660  // write content_storage_refs
661  mxf_write_local_tag(s, 16, 0x3B03);
663 
664  // operational pattern
665  mxf_write_local_tag(s, 16, 0x3B09);
666  if (s->oformat == &ff_mxf_opatom_muxer)
667  avio_write(pb, opatom_ul, 16);
668  else
669  avio_write(pb, op1a_ul, 16);
670 
671  // write essence_container_refs
672  mxf_write_local_tag(s, 8 + 16LL * DESCRIPTOR_COUNT(mxf->essence_container_count), 0x3B0A);
674 
675  // write dm_scheme_refs
676  mxf_write_local_tag(s, 8, 0x3B0B);
677  avio_wb64(pb, 0);
678 }
679 
680 /*
681  * Returns the length of the UTF-16 string, in 16-bit characters, that would result
682  * from decoding the utf-8 string.
683  */
684 static uint64_t mxf_utf16len(const char *utf8_str)
685 {
686  const uint8_t *q = utf8_str;
687  uint64_t size = 0;
688  while (*q) {
689  uint32_t ch;
690  GET_UTF8(ch, *q++, goto invalid;)
691  if (ch < 0x10000)
692  size++;
693  else
694  size += 2;
695  continue;
696 invalid:
697  av_log(NULL, AV_LOG_ERROR, "Invalid UTF8 sequence in mxf_utf16len\n\n");
698  }
699  size += 1;
700  return size;
701 }
702 
703 /*
704  * Returns the calculated length a local tag containing an utf-8 string as utf-16
705  */
706 static int mxf_utf16_local_tag_length(const char *utf8_str)
707 {
708  uint64_t size;
709 
710  if (!utf8_str)
711  return 0;
712 
713  size = mxf_utf16len(utf8_str);
714  if (size >= UINT16_MAX/2) {
715  av_log(NULL, AV_LOG_ERROR, "utf16 local tag size %"PRIx64" invalid (too large), ignoring\n", size);
716  return 0;
717  }
718 
719  return 4 + size * 2;
720 }
721 
722 /*
723  * Write a local tag containing an utf-8 string as utf-16
724  */
725 static void mxf_write_local_tag_utf16(AVFormatContext *s, int tag, const char *value)
726 {
727  AVIOContext *pb = s->pb;
728  uint64_t size = mxf_utf16len(value);
729 
730  if (size >= UINT16_MAX/2) {
731  av_log(NULL, AV_LOG_ERROR, "utf16 local tag size %"PRIx64" invalid (too large), ignoring\n", size);
732  return;
733  }
734 
735  mxf_write_local_tag(s, size*2, tag);
736  avio_put_str16be(pb, value);
737 }
738 
740  AVIOContext *pb = s->pb;
741 
742  if (s->flags & AVFMT_FLAG_BITEXACT) {
743  avio_wb16(pb, 0); // major
744  avio_wb16(pb, 0); // minor
745  avio_wb16(pb, 0); // tertiary
746  } else {
747  avio_wb16(pb, LIBAVFORMAT_VERSION_MAJOR); // major
748  avio_wb16(pb, LIBAVFORMAT_VERSION_MINOR); // minor
749  avio_wb16(pb, LIBAVFORMAT_VERSION_MICRO); // tertiary
750  }
751  avio_wb16(pb, 0); // patch
752  avio_wb16(pb, 0); // release
753 }
754 
755 #define PLATFORM_IDENT "Lavf " AV_STRINGIFY((OS_NAME))
757 {
758  MXFContext *mxf = s->priv_data;
759  AVIOContext *pb = s->pb;
760  AVDictionaryEntry *com_entry = av_dict_get(s->metadata, "company_name", NULL, 0);
761  AVDictionaryEntry *product_entry = av_dict_get(s->metadata, "product_name", NULL, 0);
762  AVDictionaryEntry *version_entry = av_dict_get(s->metadata, "product_version", NULL, 0);
763  const char *company = com_entry ? com_entry->value : "FFmpeg";
764  const char *product = product_entry ? product_entry->value : s->oformat != &ff_mxf_opatom_muxer ? "OP1a Muxer" : "OPAtom Muxer";
765  const char *platform = s->flags & AVFMT_FLAG_BITEXACT ? "Lavf" : PLATFORM_IDENT;
766  const char *version = version_entry ? version_entry->value :
767  s->flags & AVFMT_FLAG_BITEXACT ? "0.0.0" :
769  int length;
770 
771  mxf_write_metadata_key(pb, 0x013000);
772  PRINT_KEY(s, "identification key", pb->buf_ptr - 16);
773 
774  length = 100 +mxf_utf16_local_tag_length(company) +
775  mxf_utf16_local_tag_length(product) +
776  mxf_utf16_local_tag_length(platform) +
778  klv_encode_ber_length(pb, length);
779 
780  // write uid
781  mxf_write_local_tag(s, 16, 0x3C0A);
783  PRINT_KEY(s, "identification uid", pb->buf_ptr - 16);
784 
785  // write generation uid
786  mxf_write_local_tag(s, 16, 0x3C09);
788  mxf_write_local_tag_utf16(s, 0x3C01, company); // Company Name
789  mxf_write_local_tag_utf16(s, 0x3C02, product); // Product Name
790 
791  mxf_write_local_tag(s, 10, 0x3C03); // Product Version
792  store_version(s);
793 
794  mxf_write_local_tag_utf16(s, 0x3C04, version); // Version String
795  mxf_write_local_tag_utf16(s, 0x3C08, platform); // Platform
796 
797  // write product uid
798  mxf_write_local_tag(s, 16, 0x3C05);
800 
801  // modification date
802  mxf_write_local_tag(s, 8, 0x3C06);
803  avio_wb64(pb, mxf->timestamp);
804 
805  mxf_write_local_tag(s, 10, 0x3C07); // Toolkit Version
806  store_version(s);
807 }
808 
809 static void mxf_write_content_storage(AVFormatContext *s, MXFPackage *packages, int package_count)
810 {
811  AVIOContext *pb = s->pb;
812  int i;
813 
814  mxf_write_metadata_key(pb, 0x011800);
815  PRINT_KEY(s, "content storage key", pb->buf_ptr - 16);
816  klv_encode_ber_length(pb, 60 + (16 * package_count));
817 
818  // write uid
819  mxf_write_local_tag(s, 16, 0x3C0A);
821  PRINT_KEY(s, "content storage uid", pb->buf_ptr - 16);
822 
823  // write package reference
824  mxf_write_local_tag(s, 16 * package_count + 8, 0x1901);
825  mxf_write_refs_count(pb, package_count);
826  for (i = 0; i < package_count; i++) {
827  mxf_write_uuid(pb, packages[i].type, packages[i].instance);
828  }
829 
830  // write essence container data
831  mxf_write_local_tag(s, 8 + 16, 0x1902);
832  mxf_write_refs_count(pb, 1);
834 }
835 
836 static void mxf_write_track(AVFormatContext *s, AVStream *st, MXFPackage *package)
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(s, 16, 0x3C0A);
849  PRINT_KEY(s, "track uid", pb->buf_ptr - 16);
850 
851  // write track id
852  mxf_write_local_tag(s, 4, 0x4801);
853  avio_wb32(pb, st->index+2);
854 
855  // write track number
856  mxf_write_local_tag(s, 4, 0x4804);
857  if (package->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(s, 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(s, 8, 0x4B02);
875  avio_wb64(pb, 0);
876 
877  // write sequence refs
878  mxf_write_local_tag(s, 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(s, 16, 0x0201);
891  if (st == mxf->timecode_track)
893  else {
895  avio_write(pb, data_def_ul->uid, 16);
896  }
897 
898  // write duration
899  mxf_write_local_tag(s, 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(s, 16, 0x3C0A);
920 
921  PRINT_KEY(s, "sequence uid", pb->buf_ptr - 16);
923 
924  // write structural component
925  mxf_write_local_tag(s, 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 
932  mxf_write_uuid(pb, component, mxf->track_instance_count);
933 }
934 
936 {
937  MXFContext *mxf = s->priv_data;
938  AVIOContext *pb = s->pb;
939 
940  mxf_write_metadata_key(pb, 0x011400);
941  klv_encode_ber_length(pb, 75);
942 
943  // UID
944  mxf_write_local_tag(s, 16, 0x3C0A);
946 
948 
949  // Start Time Code
950  mxf_write_local_tag(s, 8, 0x1501);
951  avio_wb64(pb, mxf->tc.start);
952 
953  // Rounded Time Code Base
954  mxf_write_local_tag(s, 2, 0x1502);
955  avio_wb16(pb, mxf->timecode_base);
956 
957  // Drop Frame
958  mxf_write_local_tag(s, 1, 0x1503);
959  avio_w8(pb, !!(mxf->tc.flags & AV_TIMECODE_FLAG_DROPFRAME));
960 }
961 
963 {
964  MXFContext *mxf = s->priv_data;
965  AVIOContext *pb = s->pb;
966  int i;
967 
968  mxf_write_metadata_key(pb, 0x011100);
969  PRINT_KEY(s, "sturctural component key", pb->buf_ptr - 16);
970  klv_encode_ber_length(pb, 108);
971 
972  // write uid
973  mxf_write_local_tag(s, 16, 0x3C0A);
975 
976  PRINT_KEY(s, "structural component uid", pb->buf_ptr - 16);
978 
979  // write start_position
980  mxf_write_local_tag(s, 8, 0x1201);
981  avio_wb64(pb, 0);
982 
983  // write source package uid, end of the reference
984  mxf_write_local_tag(s, 32, 0x1101);
985  if (!package->ref) {
986  for (i = 0; i < 4; i++)
987  avio_wb64(pb, 0);
988  } else
989  mxf_write_umid(s, package->ref->instance);
990 
991  // write source track id
992  mxf_write_local_tag(s, 4, 0x1102);
993  if (package->type == SourcePackage && !package->ref)
994  avio_wb32(pb, 0);
995  else
996  avio_wb32(pb, st->index+2);
997 }
998 
1000 {
1001  AVIOContext *pb = s->pb;
1002 
1003  mxf_write_metadata_key(pb, 0x012e00);
1004  PRINT_KEY(s, "tape descriptor key", pb->buf_ptr - 16);
1005  klv_encode_ber_length(pb, 20);
1006  mxf_write_local_tag(s, 16, 0x3C0A);
1008  PRINT_KEY(s, "tape_desc uid", pb->buf_ptr - 16);
1009 }
1010 
1011 
1013 {
1014  MXFContext *mxf = s->priv_data;
1015  AVIOContext *pb = s->pb;
1016  const uint8_t *ul;
1017  int i;
1018 
1019  mxf_write_metadata_key(pb, 0x014400);
1020  PRINT_KEY(s, "multiple descriptor key", pb->buf_ptr - 16);
1021  klv_encode_ber_length(pb, 64 + 16LL * s->nb_streams);
1022 
1023  mxf_write_local_tag(s, 16, 0x3C0A);
1025  PRINT_KEY(s, "multi_desc uid", pb->buf_ptr - 16);
1026 
1027  // write sample rate
1028  mxf_write_local_tag(s, 8, 0x3001);
1029  avio_wb32(pb, mxf->time_base.den);
1030  avio_wb32(pb, mxf->time_base.num);
1031 
1032  // write essence container ul
1033  mxf_write_local_tag(s, 16, 0x3004);
1034  if (mxf->essence_container_count > 1)
1035  ul = multiple_desc_ul;
1036  else {
1037  MXFStreamContext *sc = s->streams[0]->priv_data;
1038  ul = *sc->container_ul;
1039  }
1040  avio_write(pb, ul, 16);
1041 
1042  // write sub descriptor refs
1043  mxf_write_local_tag(s, s->nb_streams * 16 + 8, 0x3F01);
1045  for (i = 0; i < s->nb_streams; i++)
1046  mxf_write_uuid(pb, SubDescriptor, i);
1047 }
1048 
1050 {
1051  MXFContext *mxf = s->priv_data;
1052  MXFStreamContext *sc = st->priv_data;
1053  AVIOContext *pb = s->pb;
1054  int64_t pos;
1055 
1056  avio_write(pb, key, 16);
1057  klv_encode_ber4_length(pb, 0);
1058  pos = avio_tell(pb);
1059 
1060  mxf_write_local_tag(s, 16, 0x3C0A);
1062 
1063  mxf_write_local_tag(s, 4, 0x3006);
1064  avio_wb32(pb, st->index+2);
1065 
1066  mxf_write_local_tag(s, 8, 0x3001);
1067  if (s->oformat == &ff_mxf_d10_muxer) {
1068  avio_wb32(pb, mxf->time_base.den);
1069  avio_wb32(pb, mxf->time_base.num);
1070  } else {
1071  if (st->codecpar->codec_id == AV_CODEC_ID_PCM_S16LE ||
1073  avio_wb32(pb, st->codecpar->sample_rate);
1074  avio_wb32(pb, 1);
1075  } else {
1076  avio_wb32(pb, mxf->time_base.den);
1077  avio_wb32(pb, mxf->time_base.num);
1078  }
1079  }
1080 
1081  mxf_write_local_tag(s, 16, 0x3004);
1082  avio_write(pb, *sc->container_ul, 16);
1083 
1084  return pos;
1085 }
1086 
1087 static const UID mxf_s436m_anc_descriptor_key = { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x5c,0x00 };
1088 static const UID mxf_mpegvideo_descriptor_key = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x51,0x00 };
1089 static const UID mxf_wav_descriptor_key = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x48,0x00 };
1090 static const UID mxf_aes3_descriptor_key = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x47,0x00 };
1091 static const UID mxf_cdci_descriptor_key = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0D,0x01,0x01,0x01,0x01,0x01,0x28,0x00 };
1092 static const UID mxf_generic_sound_descriptor_key = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0D,0x01,0x01,0x01,0x01,0x01,0x42,0x00 };
1093 
1094 static const UID mxf_avc_subdescriptor_key = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x6E,0x00 };
1095 
1096 static inline uint16_t rescale_mastering_chroma(AVRational q)
1097 {
1099 }
1100 
1101 static inline uint32_t rescale_mastering_luma(AVRational q)
1102 {
1104 }
1105 
1106 static int64_t mxf_write_cdci_common(AVFormatContext *s, AVStream *st, const UID key)
1107 {
1108  MXFStreamContext *sc = st->priv_data;
1109  AVIOContext *pb = s->pb;
1110  int stored_width = 0;
1111  int stored_height = (st->codecpar->height+15)/16*16;
1112  int display_height;
1113  int f1, f2;
1114  const MXFCodecUL *color_primaries_ul;
1115  const MXFCodecUL *color_trc_ul;
1116  const MXFCodecUL *color_space_ul;
1117  int64_t pos = mxf_write_generic_desc(s, st, key);
1118  uint8_t *side_data;
1119 
1123 
1124  if (st->codecpar->codec_id == AV_CODEC_ID_DVVIDEO) {
1125  if (st->codecpar->height == 1080)
1126  stored_width = 1920;
1127  else if (st->codecpar->height == 720)
1128  stored_width = 1280;
1129  }
1130  if (!stored_width)
1131  stored_width = (st->codecpar->width+15)/16*16;
1132 
1133  mxf_write_local_tag(s, 4, 0x3203);
1134  avio_wb32(pb, stored_width);
1135 
1136  mxf_write_local_tag(s, 4, 0x3202);
1137  avio_wb32(pb, stored_height>>sc->interlaced);
1138 
1139  if (s->oformat == &ff_mxf_d10_muxer) {
1140  //Stored F2 Offset
1141  mxf_write_local_tag(s, 4, 0x3216);
1142  avio_wb32(pb, 0);
1143 
1144  //Image Start Offset
1145  mxf_write_local_tag(s, 4, 0x3213);
1146  avio_wb32(pb, 0);
1147 
1148  //Image End Offset
1149  mxf_write_local_tag(s, 4, 0x3214);
1150  avio_wb32(pb, 0);
1151  }
1152 
1153  //Sampled width
1154  mxf_write_local_tag(s, 4, 0x3205);
1155  avio_wb32(pb, stored_width);
1156 
1157  //Samples height
1158  mxf_write_local_tag(s, 4, 0x3204);
1159  avio_wb32(pb, st->codecpar->height>>sc->interlaced);
1160 
1161  //Sampled X Offset
1162  mxf_write_local_tag(s, 4, 0x3206);
1163  avio_wb32(pb, 0);
1164 
1165  //Sampled Y Offset
1166  mxf_write_local_tag(s, 4, 0x3207);
1167  avio_wb32(pb, 0);
1168 
1169  mxf_write_local_tag(s, 4, 0x3209);
1170  avio_wb32(pb, stored_width);
1171 
1172  if (st->codecpar->height == 608) // PAL + VBI
1173  display_height = 576;
1174  else if (st->codecpar->height == 512) // NTSC + VBI
1175  display_height = 486;
1176  else
1177  display_height = st->codecpar->height;
1178 
1179  mxf_write_local_tag(s, 4, 0x3208);
1180  avio_wb32(pb, display_height>>sc->interlaced);
1181 
1182  // display X offset
1183  mxf_write_local_tag(s, 4, 0x320A);
1184  avio_wb32(pb, 0);
1185 
1186  // display Y offset
1187  mxf_write_local_tag(s, 4, 0x320B);
1188  avio_wb32(pb, (st->codecpar->height - display_height)>>sc->interlaced);
1189 
1190  if (sc->interlaced) {
1191  //Display F2 Offset
1192  mxf_write_local_tag(s, 4, 0x3217);
1193  avio_wb32(pb, -((st->codecpar->height - display_height)&1));
1194  }
1195 
1196  // component depth
1197  mxf_write_local_tag(s, 4, 0x3301);
1198  avio_wb32(pb, sc->component_depth);
1199 
1200  // horizontal subsampling
1201  mxf_write_local_tag(s, 4, 0x3302);
1202  avio_wb32(pb, sc->h_chroma_sub_sample);
1203 
1204  // vertical subsampling
1205  mxf_write_local_tag(s, 4, 0x3308);
1206  avio_wb32(pb, sc->v_chroma_sub_sample);
1207 
1208  // color siting
1209  mxf_write_local_tag(s, 1, 0x3303);
1210  avio_w8(pb, sc->color_siting);
1211 
1212  // Padding Bits
1213  mxf_write_local_tag(s, 2, 0x3307);
1214  avio_wb16(pb, 0);
1215 
1217  int black = 0,
1218  white = (1<<sc->component_depth) - 1,
1219  color = (1<<sc->component_depth);
1220  if (st->codecpar->color_range == AVCOL_RANGE_MPEG) {
1221  black = 1 << (sc->component_depth - 4);
1222  white = 235 << (sc->component_depth - 8);
1223  color = (14 << (sc->component_depth - 4)) + 1;
1224  }
1225  mxf_write_local_tag(s, 4, 0x3304);
1226  avio_wb32(pb, black);
1227  mxf_write_local_tag(s, 4, 0x3305);
1228  avio_wb32(pb, white);
1229  mxf_write_local_tag(s, 4, 0x3306);
1230  avio_wb32(pb, color);
1231  }
1232 
1233  if (sc->signal_standard) {
1234  mxf_write_local_tag(s, 1, 0x3215);
1235  avio_w8(pb, sc->signal_standard);
1236  }
1237 
1238  // frame layout
1239  mxf_write_local_tag(s, 1, 0x320C);
1240  avio_w8(pb, sc->interlaced);
1241 
1242  // video line map
1243  switch (st->codecpar->height) {
1244  case 576: f1 = 23; f2 = st->codecpar->codec_id == AV_CODEC_ID_DVVIDEO ? 335 : 336; break;
1245  case 608: f1 = 7; f2 = 320; break;
1246  case 480: f1 = 20; f2 = st->codecpar->codec_id == AV_CODEC_ID_DVVIDEO ? 285 : 283; break;
1247  case 512: f1 = 7; f2 = 270; break;
1248  case 720: f1 = 26; f2 = 0; break; // progressive
1249  case 1080: f1 = 21; f2 = 584; break;
1250  default: f1 = 0; f2 = 0; break;
1251  }
1252 
1253  if (!sc->interlaced && f2) {
1254  f2 = 0;
1255  f1 *= 2;
1256  }
1257 
1258 
1259  mxf_write_local_tag(s, 16, 0x320D);
1260  avio_wb32(pb, 2);
1261  avio_wb32(pb, 4);
1262  avio_wb32(pb, f1);
1263  avio_wb32(pb, f2);
1264 
1265  mxf_write_local_tag(s, 8, 0x320E);
1266  avio_wb32(pb, sc->aspect_ratio.num);
1267  avio_wb32(pb, sc->aspect_ratio.den);
1268 
1269  if (color_primaries_ul->uid[0]) {
1270  mxf_write_local_tag(s, 16, 0x3219);
1271  avio_write(pb, color_primaries_ul->uid, 16);
1272  };
1273 
1274  if (color_trc_ul->uid[0]) {
1275  mxf_write_local_tag(s, 16, 0x3210);
1276  avio_write(pb, color_trc_ul->uid, 16);
1277  };
1278 
1279  if (color_space_ul->uid[0]) {
1280  mxf_write_local_tag(s, 16, 0x321A);
1281  avio_write(pb, color_space_ul->uid, 16);
1282  };
1283 
1284  mxf_write_local_tag(s, 16, 0x3201);
1285  avio_write(pb, *sc->codec_ul, 16);
1286 
1287  // Mastering Display metadata
1289  if (side_data) {
1290  const AVMasteringDisplayMetadata *metadata = (const AVMasteringDisplayMetadata*)side_data;
1291  if (metadata->has_primaries) {
1292  mxf_write_local_tag(s, 12, 0x8301);
1299  mxf_write_local_tag(s, 4, 0x8302);
1300  avio_wb16(pb, rescale_mastering_chroma(metadata->white_point[0]));
1301  avio_wb16(pb, rescale_mastering_chroma(metadata->white_point[1]));
1302  } else {
1303  av_log(NULL, AV_LOG_VERBOSE, "Not writing mastering display primaries. Missing data.\n");
1304  }
1305  if (metadata->has_luminance) {
1306  mxf_write_local_tag(s, 4, 0x8303);
1308  mxf_write_local_tag(s, 4, 0x8304);
1310  } else {
1311  av_log(NULL, AV_LOG_VERBOSE, "Not writing mastering display luminances. Missing data.\n");
1312  }
1313  }
1314 
1315  if (sc->interlaced && sc->field_dominance) {
1316  mxf_write_local_tag(s, 1, 0x3212);
1317  avio_w8(pb, sc->field_dominance);
1318  }
1319 
1320  if (st->codecpar->codec_id == AV_CODEC_ID_H264 && !sc->avc_intra) {
1321  // write avc sub descriptor ref
1322  mxf_write_local_tag(s, 8 + 16, 0x8100);
1323  mxf_write_refs_count(pb, 1);
1325  }
1326 
1327  return pos;
1328 }
1329 
1330 static void mxf_update_klv_size(AVIOContext *pb, int64_t pos)
1331 {
1332  int64_t cur_pos = avio_tell(pb);
1333  int size = cur_pos - pos;
1334  avio_seek(pb, pos - 4, SEEK_SET);
1335  klv_encode_ber4_length(pb, size);
1336  avio_seek(pb, cur_pos, SEEK_SET);
1337 }
1338 
1340 {
1341  AVIOContext *pb = s->pb;
1342  int64_t pos;
1343 
1345  klv_encode_ber4_length(pb, 0);
1346  pos = avio_tell(pb);
1347 
1348  mxf_write_local_tag(s, 16, 0x3C0A);
1350 
1351  mxf_write_local_tag(s, 1, 0x8200);
1352  avio_w8(pb, 0xFF); // AVC Decoding Delay, unknown
1353 
1354  mxf_write_local_tag(s, 1, 0x8201);
1355  avio_w8(pb, st->codecpar->profile); // AVC Profile
1356 
1357  mxf_write_local_tag(s, 1, 0x8202);
1358  avio_w8(pb, st->codecpar->level); // AVC Level
1359 
1360  mxf_update_klv_size(s->pb, pos);
1361 }
1362 
1364 {
1366  mxf_update_klv_size(s->pb, pos);
1367 
1368  if (st->codecpar->codec_id == AV_CODEC_ID_H264) {
1369  mxf_write_avc_subdesc(s, st);
1370  }
1371 }
1372 
1374 {
1375  MXFStreamContext *sc = st->priv_data;
1376  if (sc->avc_intra) {
1377  mxf_write_mpegvideo_desc(s, st);
1378  } else {
1380  mxf_update_klv_size(s->pb, pos);
1381  mxf_write_avc_subdesc(s, st);
1382  }
1383 }
1384 
1386 {
1388  mxf_update_klv_size(s->pb, pos);
1389 }
1390 
1392 {
1393  AVIOContext *pb = s->pb;
1394  MXFStreamContext *sc = st->priv_data;
1395  int profile_and_level = (st->codecpar->profile<<4) | st->codecpar->level;
1397 
1398  if (st->codecpar->codec_id != AV_CODEC_ID_H264) {
1399  // bit rate
1400  mxf_write_local_tag(s, 4, 0x8000);
1401  avio_wb32(pb, sc->video_bit_rate);
1402 
1403  // profile and level
1404  mxf_write_local_tag(s, 1, 0x8007);
1405  if (!st->codecpar->profile)
1406  profile_and_level |= 0x80; // escape bit
1407  avio_w8(pb, profile_and_level);
1408 
1409  // low delay
1410  mxf_write_local_tag(s, 1, 0x8003);
1411  avio_w8(pb, sc->low_delay);
1412 
1413  // closed gop
1414  mxf_write_local_tag(s, 1, 0x8004);
1415  avio_w8(pb, sc->seq_closed_gop);
1416 
1417  // max gop
1418  mxf_write_local_tag(s, 2, 0x8006);
1419  avio_wb16(pb, sc->max_gop);
1420 
1421  // b picture count
1422  mxf_write_local_tag(s, 2, 0x8008);
1423  avio_wb16(pb, sc->b_picture_count);
1424  }
1425 
1426  mxf_update_klv_size(pb, pos);
1427 }
1428 
1430 {
1431  AVIOContext *pb = s->pb;
1432  MXFContext *mxf = s->priv_data;
1433  int show_warnings = !mxf->footer_partition_offset;
1434  int64_t pos = mxf_write_generic_desc(s, st, key);
1435 
1436  if (s->oformat == &ff_mxf_opatom_muxer) {
1437  mxf_write_local_tag(s, 8, 0x3002);
1438  avio_wb64(pb, mxf->body_offset / mxf->edit_unit_byte_count);
1439  }
1440 
1441  // audio locked
1442  mxf_write_local_tag(s, 1, 0x3D02);
1443  avio_w8(pb, 1);
1444 
1445  // write audio sampling rate
1446  mxf_write_local_tag(s, 8, 0x3D03);
1447  avio_wb32(pb, st->codecpar->sample_rate);
1448  avio_wb32(pb, 1);
1449 
1450  if (s->oformat == &ff_mxf_d10_muxer) {
1451  mxf_write_local_tag(s, 1, 0x3D04);
1452  avio_w8(pb, 0);
1453  }
1454 
1455  mxf_write_local_tag(s, 4, 0x3D07);
1456  if (mxf->channel_count == -1) {
1457  if (show_warnings && (s->oformat == &ff_mxf_d10_muxer) && (st->codecpar->channels != 4) && (st->codecpar->channels != 8))
1458  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");
1459  avio_wb32(pb, st->codecpar->channels);
1460  } else if (s->oformat == &ff_mxf_d10_muxer) {
1461  if (show_warnings && (mxf->channel_count < st->codecpar->channels))
1462  av_log(s, AV_LOG_WARNING, "d10_channelcount < actual number of audio channels : some channels will be discarded\n");
1463  if (show_warnings && (mxf->channel_count != 4) && (mxf->channel_count != 8))
1464  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");
1465  avio_wb32(pb, mxf->channel_count);
1466  } else {
1467  avio_wb32(pb, st->codecpar->channels);
1468  }
1469 
1470  mxf_write_local_tag(s, 4, 0x3D01);
1472 
1473  return pos;
1474 }
1475 
1476 static int64_t mxf_write_wav_common(AVFormatContext *s, AVStream *st, const UID key)
1477 {
1478  AVIOContext *pb = s->pb;
1479  int64_t pos = mxf_write_generic_sound_common(s, st, key);
1480 
1481  mxf_write_local_tag(s, 2, 0x3D0A);
1482  avio_wb16(pb, st->codecpar->block_align);
1483 
1484  // avg bytes per sec
1485  mxf_write_local_tag(s, 4, 0x3D09);
1487 
1488  return pos;
1489 }
1490 
1492 {
1494  mxf_update_klv_size(s->pb, pos);
1495 }
1496 
1498 {
1500  mxf_update_klv_size(s->pb, pos);
1501 }
1502 
1504 {
1506  mxf_update_klv_size(s->pb, pos);
1507 }
1508 
1509 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 };
1510 
1511 static int mxf_write_tagged_value(AVFormatContext *s, const char* name, const char* value)
1512 {
1513  MXFContext *mxf = s->priv_data;
1514  AVIOContext *pb = s->pb;
1515  int name_size = mxf_utf16_local_tag_length(name);
1516  int indirect_value_size = 13 + mxf_utf16_local_tag_length(value);
1517 
1518  if (!name_size || indirect_value_size == 13)
1519  return 1;
1520 
1521  mxf_write_metadata_key(pb, 0x013f00);
1522  klv_encode_ber_length(pb, 24 + name_size + indirect_value_size);
1523 
1524  // write instance UID
1525  mxf_write_local_tag(s, 16, 0x3C0A);
1527 
1528  // write name
1529  mxf_write_local_tag_utf16(s, 0x5001, name); // Name
1530 
1531  // write indirect value
1532  mxf_write_local_tag(s, indirect_value_size, 0x5003);
1534  avio_put_str16le(pb, value);
1535 
1536  mxf->tagged_value_count++;
1537  return 0;
1538 }
1539 
1541 {
1542  MXFContext *mxf = s->priv_data;
1543  AVDictionaryEntry *t = NULL;
1544  int count = 0;
1545 
1546  while ((t = av_dict_get(m, "comment_", t, AV_DICT_IGNORE_SUFFIX))) {
1547  if (mxf->tagged_value_count >= UINT16_MAX) {
1548  av_log(s, AV_LOG_ERROR, "too many tagged values, ignoring remaining\n");
1549  return count;
1550  }
1551 
1552  if (mxf_write_tagged_value(s, t->key + 8, t->value) == 0)
1553  count++;
1554  }
1555  return count;
1556 }
1557 
1559 {
1560  MXFContext *mxf = s->priv_data;
1561  AVIOContext *pb = s->pb;
1562  int i, track_count = s->nb_streams+1;
1563  int name_size = mxf_utf16_local_tag_length(package->name);
1564  int user_comment_count = 0;
1565 
1566  if (package->type == MaterialPackage) {
1567  if (mxf->store_user_comments)
1568  user_comment_count = mxf_write_user_comments(s, s->metadata);
1569  mxf_write_metadata_key(pb, 0x013600);
1570  PRINT_KEY(s, "Material Package key", pb->buf_ptr - 16);
1571  klv_encode_ber_length(pb, 92 + name_size + (16*track_count) + (16*user_comment_count) + 12LL*mxf->store_user_comments);
1572  } else {
1573  mxf_write_metadata_key(pb, 0x013700);
1574  PRINT_KEY(s, "Source Package key", pb->buf_ptr - 16);
1575  klv_encode_ber_length(pb, 112 + name_size + (16*track_count) + 12LL*mxf->store_user_comments); // 20 bytes length for descriptor reference
1576  }
1577 
1578  // write uid
1579  mxf_write_local_tag(s, 16, 0x3C0A);
1580  mxf_write_uuid(pb, package->type, package->instance);
1581  av_log(s, AV_LOG_DEBUG, "package type:%d\n", package->type);
1582  PRINT_KEY(s, "package uid", pb->buf_ptr - 16);
1583 
1584  // write package umid
1585  mxf_write_local_tag(s, 32, 0x4401);
1586  mxf_write_umid(s, package->instance);
1587  PRINT_KEY(s, "package umid second part", pb->buf_ptr - 16);
1588 
1589  // package name
1590  if (name_size)
1591  mxf_write_local_tag_utf16(s, 0x4402, package->name);
1592 
1593  // package creation date
1594  mxf_write_local_tag(s, 8, 0x4405);
1595  avio_wb64(pb, mxf->timestamp);
1596 
1597  // package modified date
1598  mxf_write_local_tag(s, 8, 0x4404);
1599  avio_wb64(pb, mxf->timestamp);
1600 
1601  // write track refs
1602  mxf_write_local_tag(s, track_count*16 + 8, 0x4403);
1603  mxf_write_refs_count(pb, track_count);
1604  // these are the uuids of the tracks the will be written in mxf_write_track
1605  for (i = 0; i < track_count; i++)
1606  mxf_write_uuid(pb, Track, mxf->track_instance_count + i);
1607 
1608  // write user comment refs
1609  if (mxf->store_user_comments) {
1610  mxf_write_local_tag(s, user_comment_count*16 + 8, 0x4406);
1611  mxf_write_refs_count(pb, user_comment_count);
1612  for (i = 0; i < user_comment_count; i++)
1613  mxf_write_uuid(pb, TaggedValue, mxf->tagged_value_count - user_comment_count + i);
1614  }
1615 
1616  // write multiple descriptor reference
1617  if (package->type == SourcePackage && package->instance == 1) {
1618  mxf_write_local_tag(s, 16, 0x4701);
1619  if (s->nb_streams > 1) {
1622  } else
1623  mxf_write_uuid(pb, SubDescriptor, 0);
1624  } else if (package->type == SourcePackage && package->instance == 2) {
1625  mxf_write_local_tag(s, 16, 0x4701);
1628  }
1629 
1630  /*
1631  * for every 1 track in a package there is 1 sequence and 1 component.
1632  * all 3 of these elements share the same instance number for generating
1633  * there instance uuids. mxf->track_instance_count stores this value.
1634  * mxf->track_instance_count is incremented after a group of all 3 of
1635  * these elements are written.
1636  */
1637 
1638  // write timecode track
1639  mxf_write_track(s, mxf->timecode_track, package);
1640  mxf_write_sequence(s, mxf->timecode_track, package);
1641  mxf_write_timecode_component(s, mxf->timecode_track, package);
1642  mxf->track_instance_count++;
1643 
1644  for (i = 0; i < s->nb_streams; i++) {
1645  AVStream *st = s->streams[i];
1646  mxf_write_track(s, st, package);
1647  mxf_write_sequence(s, st, package);
1648  mxf_write_structural_component(s, st, package);
1649  mxf->track_instance_count++;
1650 
1651  if (package->type == SourcePackage && package->instance == 1) {
1652  MXFStreamContext *sc = st->priv_data;
1653  mxf_essence_container_uls[sc->index].write_desc(s, st);
1654  }
1655  }
1656 }
1657 
1659 {
1660  AVIOContext *pb = s->pb;
1661 
1662  mxf_write_metadata_key(pb, 0x012300);
1663  klv_encode_ber_length(pb, 72);
1664 
1665  mxf_write_local_tag(s, 16, 0x3C0A); // Instance UID
1667 
1668  mxf_write_local_tag(s, 32, 0x2701); // Linked Package UID
1669  mxf_write_umid(s, 1);
1670 
1671  mxf_write_local_tag(s, 4, 0x3F07); // BodySID
1672  avio_wb32(pb, 1);
1673 
1674  mxf_write_local_tag(s, 4, 0x3F06); // IndexSID
1675  avio_wb32(pb, 2);
1676 
1677  return 0;
1678 }
1679 
1681 {
1682  MXFContext *mxf = s->priv_data;
1683  AVDictionaryEntry *entry = NULL;
1684  AVStream *st = NULL;
1685  int i;
1686  MXFPackage packages[3] = {{0}};
1687  int package_count = 2;
1688  packages[0].type = MaterialPackage;
1689  packages[1].type = SourcePackage;
1690  packages[1].instance = 1;
1691  packages[0].ref = &packages[1];
1692 
1693 
1694  if (entry = av_dict_get(s->metadata, "material_package_name", NULL, 0))
1695  packages[0].name = entry->value;
1696 
1697  if (entry = av_dict_get(s->metadata, "file_package_name", NULL, 0)) {
1698  packages[1].name = entry->value;
1699  } else {
1700  /* check if any of the streams contain a file_package_name */
1701  for (i = 0; i < s->nb_streams; i++) {
1702  st = s->streams[i];
1703  if (entry = av_dict_get(st->metadata, "file_package_name", NULL, 0)) {
1704  packages[1].name = entry->value;
1705  break;
1706  }
1707  }
1708  }
1709 
1710  entry = av_dict_get(s->metadata, "reel_name", NULL, 0);
1711  if (entry) {
1712  packages[2].name = entry->value;
1713  packages[2].type = SourcePackage;
1714  packages[2].instance = 2;
1715  packages[1].ref = &packages[2];
1716  package_count = 3;
1717  }
1718 
1719  mxf_write_preface(s);
1721  mxf_write_content_storage(s, packages, package_count);
1722  mxf->track_instance_count = 0;
1723  for (i = 0; i < package_count; i++)
1724  mxf_write_package(s, &packages[i]);
1726  return 0;
1727 }
1728 
1729 static unsigned klv_fill_size(uint64_t size)
1730 {
1731  unsigned pad = KAG_SIZE - (size & (KAG_SIZE-1));
1732  if (pad < 20) // smallest fill item possible
1733  return pad + KAG_SIZE;
1734  else
1735  return pad & (KAG_SIZE-1);
1736 }
1737 
1739 {
1740  MXFContext *mxf = s->priv_data;
1741  AVIOContext *pb = s->pb;
1742  int i, j, temporal_reordering = 0;
1743  int key_index = mxf->last_key_index;
1744  int prev_non_b_picture = 0;
1745  int audio_frame_size = 0;
1746  int64_t pos;
1747 
1748  av_log(s, AV_LOG_DEBUG, "edit units count %d\n", mxf->edit_units_count);
1749 
1750  if (!mxf->edit_units_count && !mxf->edit_unit_byte_count)
1751  return;
1752 
1754 
1755  klv_encode_ber4_length(pb, 0);
1756  pos = avio_tell(pb);
1757 
1758  // instance id
1759  mxf_write_local_tag(s, 16, 0x3C0A);
1761 
1762  // index edit rate
1763  mxf_write_local_tag(s, 8, 0x3F0B);
1764  avio_wb32(pb, mxf->time_base.den);
1765  avio_wb32(pb, mxf->time_base.num);
1766 
1767  // index start position
1768  mxf_write_local_tag(s, 8, 0x3F0C);
1770 
1771  // index duration
1772  mxf_write_local_tag(s, 8, 0x3F0D);
1773  if (mxf->edit_unit_byte_count)
1774  avio_wb64(pb, 0); // index table covers whole container
1775  else
1776  avio_wb64(pb, mxf->edit_units_count);
1777 
1778  // edit unit byte count
1779  mxf_write_local_tag(s, 4, 0x3F05);
1780  avio_wb32(pb, mxf->edit_unit_byte_count);
1781 
1782  // index sid
1783  mxf_write_local_tag(s, 4, 0x3F06);
1784  avio_wb32(pb, 2);
1785 
1786  // body sid
1787  mxf_write_local_tag(s, 4, 0x3F07);
1788  avio_wb32(pb, 1);
1789 
1790  // real slice count - 1
1791  mxf_write_local_tag(s, 1, 0x3F08);
1792  avio_w8(pb, !mxf->edit_unit_byte_count); // only one slice for CBR
1793 
1794  // delta entry array
1795  mxf_write_local_tag(s, 8 + (s->nb_streams+1)*6, 0x3F09);
1796  avio_wb32(pb, s->nb_streams+1); // num of entries
1797  avio_wb32(pb, 6); // size of one entry
1798  // write system item delta entry
1799  avio_w8(pb, 0);
1800  avio_w8(pb, 0); // slice entry
1801  avio_wb32(pb, 0); // element delta
1802  // write each stream delta entry
1803  for (i = 0; i < s->nb_streams; i++) {
1804  AVStream *st = s->streams[i];
1805  MXFStreamContext *sc = st->priv_data;
1806  avio_w8(pb, sc->temporal_reordering);
1807  if (sc->temporal_reordering)
1808  temporal_reordering = 1;
1809  if (mxf->edit_unit_byte_count) {
1810  avio_w8(pb, 0); // slice number
1811  avio_wb32(pb, sc->slice_offset);
1812  } else if (i == 0) { // video track
1813  avio_w8(pb, 0); // slice number
1814  avio_wb32(pb, KAG_SIZE); // system item size including klv fill
1815  } else { // audio or data track
1816  if (!audio_frame_size) {
1817  audio_frame_size = sc->frame_size;
1818  audio_frame_size += klv_fill_size(audio_frame_size);
1819  }
1820  avio_w8(pb, 1);
1821  avio_wb32(pb, (i-1)*audio_frame_size); // element delta
1822  }
1823  }
1824 
1825  if (!mxf->edit_unit_byte_count) {
1826  MXFStreamContext *sc = s->streams[0]->priv_data;
1827  mxf_write_local_tag(s, 8 + mxf->edit_units_count*15, 0x3F0A);
1828  avio_wb32(pb, mxf->edit_units_count); // num of entries
1829  avio_wb32(pb, 15); // size of one entry
1830 
1831  for (i = 0; i < mxf->edit_units_count; i++) {
1832  int temporal_offset = 0;
1833 
1834  if (!(mxf->index_entries[i].flags & 0x33)) { // I-frame
1835  sc->max_gop = FFMAX(sc->max_gop, i - mxf->last_key_index);
1836  mxf->last_key_index = key_index;
1837  key_index = i;
1838  }
1839 
1840  if (temporal_reordering) {
1841  int pic_num_in_gop = i - key_index;
1842  if (pic_num_in_gop != mxf->index_entries[i].temporal_ref) {
1843  for (j = key_index; j < mxf->edit_units_count; j++) {
1844  if (pic_num_in_gop == mxf->index_entries[j].temporal_ref)
1845  break;
1846  }
1847  if (j == mxf->edit_units_count)
1848  av_log(s, AV_LOG_WARNING, "missing frames\n");
1849  temporal_offset = j - key_index - pic_num_in_gop;
1850  }
1851  }
1852  avio_w8(pb, temporal_offset);
1853 
1854  if ((mxf->index_entries[i].flags & 0x30) == 0x30) { // back and forward prediction
1855  sc->b_picture_count = FFMAX(sc->b_picture_count, i - prev_non_b_picture);
1856  avio_w8(pb, mxf->last_key_index - i);
1857  } else {
1858  avio_w8(pb, key_index - i); // key frame offset
1859  if ((mxf->index_entries[i].flags & 0x20) == 0x20) // only forward
1860  mxf->last_key_index = key_index;
1861  prev_non_b_picture = i;
1862  }
1863 
1864  if (!(mxf->index_entries[i].flags & 0x33) && // I-frame
1865  mxf->index_entries[i].flags & 0x40 && !temporal_offset)
1866  mxf->index_entries[i].flags |= 0x80; // random access
1867  avio_w8(pb, mxf->index_entries[i].flags);
1868  // stream offset
1869  avio_wb64(pb, mxf->index_entries[i].offset);
1870  if (s->nb_streams > 1)
1871  avio_wb32(pb, mxf->index_entries[i].slice_offset);
1872  else
1873  avio_wb32(pb, 0);
1874  }
1875 
1876  mxf->last_key_index = key_index - mxf->edit_units_count;
1878  mxf->edit_units_count = 0;
1879  }
1880 
1881  mxf_update_klv_size(pb, pos);
1882 }
1883 
1885 {
1886  unsigned pad = klv_fill_size(avio_tell(s->pb));
1887  if (pad) {
1888  avio_write(s->pb, klv_fill_key, 16);
1889  pad -= 16 + 4;
1890  klv_encode_ber4_length(s->pb, pad);
1891  ffio_fill(s->pb, 0, pad);
1892  av_assert1(!(avio_tell(s->pb) & (KAG_SIZE-1)));
1893  }
1894 }
1895 
1896 static int mxf_write_partition(AVFormatContext *s, int bodysid,
1897  int indexsid,
1898  const uint8_t *key, int write_metadata)
1899 {
1900  MXFContext *mxf = s->priv_data;
1901  AVIOContext *pb = s->pb;
1902  int64_t header_byte_count_offset;
1903  unsigned index_byte_count = 0;
1904  uint64_t partition_offset = avio_tell(pb);
1905  int err;
1906 
1907  if (!mxf->edit_unit_byte_count && mxf->edit_units_count)
1908  index_byte_count = 85 + 12+(s->nb_streams+1)*6 +
1909  12+mxf->edit_units_count*15;
1910  else if (mxf->edit_unit_byte_count && indexsid)
1911  index_byte_count = 80;
1912 
1913  if (index_byte_count) {
1914  index_byte_count += 16 + 4; // add encoded ber4 length
1915  index_byte_count += klv_fill_size(index_byte_count);
1916  }
1917 
1918  if (key && !memcmp(key, body_partition_key, 16)) {
1920  sizeof(*mxf->body_partition_offset))) < 0) {
1921  mxf->body_partitions_count = 0;
1922  return err;
1923  }
1924  mxf->body_partition_offset[mxf->body_partitions_count++] = partition_offset;
1925  }
1926 
1927  // write klv
1928  if (key)
1929  avio_write(pb, key, 16);
1930  else
1931  avio_write(pb, body_partition_key, 16);
1932 
1934 
1935  // write partition value
1936  avio_wb16(pb, 1); // majorVersion
1937  avio_wb16(pb, 3); // minorVersion
1938  avio_wb32(pb, KAG_SIZE); // KAGSize
1939 
1940  avio_wb64(pb, partition_offset); // ThisPartition
1941 
1942  if (key && !memcmp(key, body_partition_key, 16) && mxf->body_partitions_count > 1)
1943  avio_wb64(pb, mxf->body_partition_offset[mxf->body_partitions_count-2]); // PreviousPartition
1944  else if (key && !memcmp(key, footer_partition_key, 16) && mxf->body_partitions_count)
1945  avio_wb64(pb, mxf->body_partition_offset[mxf->body_partitions_count-1]); // PreviousPartition
1946  else
1947  avio_wb64(pb, 0);
1948 
1949  avio_wb64(pb, mxf->footer_partition_offset); // footerPartition
1950 
1951  // set offset
1952  header_byte_count_offset = avio_tell(pb);
1953  avio_wb64(pb, 0); // headerByteCount, update later
1954 
1955  // indexTable
1956  avio_wb64(pb, index_byte_count); // indexByteCount
1957  avio_wb32(pb, index_byte_count ? indexsid : 0); // indexSID
1958 
1959  // BodyOffset
1960  if (bodysid && mxf->edit_units_count && mxf->body_partitions_count && s->oformat != &ff_mxf_opatom_muxer)
1961  avio_wb64(pb, mxf->body_offset);
1962  else
1963  avio_wb64(pb, 0);
1964 
1965  avio_wb32(pb, bodysid); // bodySID
1966 
1967  // operational pattern
1968  if (s->oformat == &ff_mxf_opatom_muxer)
1969  avio_write(pb, opatom_ul, 16);
1970  else
1971  avio_write(pb, op1a_ul, 16);
1972 
1973  // essence container
1975 
1976  if (write_metadata) {
1977  // mark the start of the headermetadata and calculate metadata size
1978  int64_t pos, start;
1979  unsigned header_byte_count;
1980 
1981  mxf_write_klv_fill(s);
1982  start = avio_tell(s->pb);
1984  mxf_write_klv_fill(s);
1986  pos = avio_tell(s->pb);
1987  header_byte_count = pos - start + klv_fill_size(pos);
1988 
1989  // update header_byte_count
1990  avio_seek(pb, header_byte_count_offset, SEEK_SET);
1991  avio_wb64(pb, header_byte_count);
1992  avio_seek(pb, pos, SEEK_SET);
1993  }
1994 
1995  if(key)
1997 
1998  return 0;
1999 }
2000 
2001 static const struct {
2002  int profile;
2004 } mxf_prores_codec_uls[] = {
2005  { FF_PROFILE_PRORES_PROXY, { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0d,0x04,0x01,0x02,0x02,0x03,0x06,0x01,0x00 } },
2006  { FF_PROFILE_PRORES_LT, { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0d,0x04,0x01,0x02,0x02,0x03,0x06,0x02,0x00 } },
2007  { FF_PROFILE_PRORES_STANDARD, { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0d,0x04,0x01,0x02,0x02,0x03,0x06,0x03,0x00 } },
2008  { FF_PROFILE_PRORES_HQ, { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0d,0x04,0x01,0x02,0x02,0x03,0x06,0x04,0x00 } },
2009  { FF_PROFILE_PRORES_4444, { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0d,0x04,0x01,0x02,0x02,0x03,0x06,0x05,0x00 } },
2010  { FF_PROFILE_PRORES_XQ, { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0d,0x04,0x01,0x02,0x02,0x03,0x06,0x06,0x00 } },
2011 };
2012 
2014 {
2015  MXFContext *mxf = s->priv_data;
2016  MXFStreamContext *sc = st->priv_data;
2017  int i, profile;
2018 
2019  if (mxf->header_written)
2020  return 1;
2021 
2022  profile = st->codecpar->profile;
2023  for (i = 0; i < FF_ARRAY_ELEMS(mxf_prores_codec_uls); i++) {
2024  if (profile == mxf_prores_codec_uls[i].profile) {
2025  sc->codec_ul = &mxf_prores_codec_uls[i].codec_ul;
2026  break;
2027  }
2028  }
2030  return 0;
2031 
2032  sc->frame_size = pkt->size;
2033 
2034  return 1;
2035 }
2036 
2037 static const struct {
2038  int cid;
2039  UID codec_ul;
2040 } mxf_dnxhd_codec_uls[] = {
2041  { 1235, { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x71,0x01,0x00,0x00 } }, // 1080p 10bit HIGH
2042  { 1237, { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x71,0x03,0x00,0x00 } }, // 1080p 8bit MED
2043  { 1238, { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x71,0x04,0x00,0x00 } }, // 1080p 8bit HIGH
2044  { 1241, { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x71,0x07,0x00,0x00 } }, // 1080i 10bit HIGH
2045  { 1242, { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x71,0x08,0x00,0x00 } }, // 1080i 8bit MED
2046  { 1243, { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x71,0x09,0x00,0x00 } }, // 1080i 8bit HIGH
2047  { 1244, { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x71,0x0a,0x00,0x00 } }, // 1080i 8bit TR
2048  { 1250, { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x71,0x10,0x00,0x00 } }, // 720p 10bit
2049  { 1251, { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x71,0x11,0x00,0x00 } }, // 720p 8bit HIGH
2050  { 1252, { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x71,0x12,0x00,0x00 } }, // 720p 8bit MED
2051  { 1253, { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x71,0x13,0x00,0x00 } }, // 720p 8bit LOW
2052  { 1256, { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x71,0x16,0x00,0x00 } }, // 1080p 10bit 444
2053  { 1258, { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x71,0x18,0x00,0x00 } }, // 720p 8bit TR
2054  { 1259, { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x71,0x19,0x00,0x00 } }, // 1080p 8bit TR
2055  { 1260, { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x71,0x1a,0x00,0x00 } }, // 1080i 8bit TR MBAFF
2056  { 1270, { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x71,0x24,0x00,0x00 } }, // DNXHR 444
2057  { 1271, { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x71,0x25,0x00,0x00 } }, // DNXHR HQX
2058  { 1272, { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x71,0x26,0x00,0x00 } }, // DNXHR HQ
2059  { 1273, { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x71,0x27,0x00,0x00 } }, // DNXHR SQ
2060  { 1274, { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x71,0x28,0x00,0x00 } }, // DNXHR LB
2061 };
2062 
2064 {
2065  MXFContext *mxf = s->priv_data;
2066  MXFStreamContext *sc = st->priv_data;
2067  int i, cid, frame_size = 0;
2068 
2069  if (mxf->header_written)
2070  return 1;
2071 
2072  if (pkt->size < 43)
2073  return 0;
2074 
2075  cid = AV_RB32(pkt->data + 0x28);
2076  for (i = 0; i < FF_ARRAY_ELEMS(mxf_dnxhd_codec_uls); i++) {
2077  if (cid == mxf_dnxhd_codec_uls[i].cid) {
2078  sc->codec_ul = &mxf_dnxhd_codec_uls[i].codec_ul;
2079  break;
2080  }
2081  }
2083  return 0;
2084 
2085  sc->component_depth = 0;
2086  switch (pkt->data[0x21] >> 5) {
2087  case 1: sc->component_depth = 8; break;
2088  case 2: sc->component_depth = 10; break;
2089  case 3: sc->component_depth = 12; break;
2090  }
2091  if (!sc->component_depth)
2092  return 0;
2093 
2094  if ((frame_size = avpriv_dnxhd_get_frame_size(cid)) == DNXHD_VARIABLE) {
2095  frame_size = avpriv_dnxhd_get_hr_frame_size(cid, st->codecpar->width, st->codecpar->height);
2096  }
2097  if (frame_size < 0)
2098  return 0;
2099 
2100  if ((sc->interlaced = avpriv_dnxhd_get_interlaced(cid)) < 0)
2101  return 0;
2102 
2103  if (cid >= 1270) { // RI raster
2105  st->codecpar->width, st->codecpar->height,
2106  INT_MAX);
2107  } else {
2108  sc->aspect_ratio = (AVRational){ 16, 9 };
2109  }
2110 
2111  sc->frame_size = pkt->size;
2112 
2113  return 1;
2114 }
2115 
2116 static const struct {
2118  const UID codec_ul;
2119 } mxf_dv_uls[] = {
2120  { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x02,0x01,0x01 }, // IEC DV25 525/60
2121  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x02,0x01,0x01,0x00 } },
2122  { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x02,0x02,0x01 }, // IEC DV25 626/50
2123  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x02,0x01,0x02,0x00 } },
2124  { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x02,0x40,0x01 }, // DV25 525/60
2125  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x02,0x02,0x01,0x00 }, },
2126  { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x02,0x41,0x01 }, // DV25 625/50
2127  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x02,0x02,0x02,0x00 }, },
2128  { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x02,0x50,0x01 }, // DV50 525/60
2129  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x02,0x02,0x03,0x00 }, },
2130  { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x02,0x51,0x01 }, // DV50 625/50
2131  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x02,0x02,0x04,0x00 }, },
2132  { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x02,0x60,0x01 }, // DV100 1080/60
2133  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x02,0x02,0x05,0x00 }, },
2134  { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x02,0x61,0x01 }, // DV100 1080/50
2135  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x02,0x02,0x06,0x00 }, },
2136  { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x02,0x62,0x01 }, // DV100 720/60
2137  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x02,0x02,0x07,0x00 }, },
2138  { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x02,0x63,0x01 }, // DV100 720/50
2139  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x02,0x02,0x08,0x00 }, },
2140 };
2141 
2143 {
2144  MXFContext *mxf = s->priv_data;
2145  MXFStreamContext *sc = st->priv_data;
2146  uint8_t *vs_pack, *vsc_pack;
2147  int apt, ul_index, stype, pal;
2148 
2149  if (mxf->header_written)
2150  return 1;
2151 
2152  // Check for minimal frame size
2153  if (pkt->size < 120000)
2154  return -1;
2155 
2156  apt = pkt->data[4] & 0x7;
2157  vs_pack = pkt->data + 80*5 + 48;
2158  vsc_pack = pkt->data + 80*5 + 53;
2159  stype = vs_pack[3] & 0x1f;
2160  pal = (vs_pack[3] >> 5) & 0x1;
2161 
2162  if ((vsc_pack[2] & 0x07) == 0x02) {
2163  sc->aspect_ratio = (AVRational){ 16, 9 };
2164  } else {
2165  sc->aspect_ratio = (AVRational){ 4, 3 };
2166  }
2167 
2168  sc->interlaced = (vsc_pack[3] >> 4) & 0x01;
2169  // TODO: fix dv encoder to set proper FF/FS value in VSC pack
2170  // and set field dominance accordingly
2171  // av_log(s, AV_LOG_DEBUG, "DV vsc pack ff/ss = %x\n", vsc_pack[2] >> 6);
2172 
2173  switch (stype) {
2174  case 0x18: // DV100 720p
2175  ul_index = 8+pal;
2176  if (sc->interlaced) {
2177  av_log(s, AV_LOG_ERROR, "source marked as interlaced but codec profile is progressive\n");
2178  sc->interlaced = 0;
2179  }
2180  break;
2181  case 0x14: // DV100 1080i
2182  ul_index = 6+pal;
2183  break;
2184  case 0x04: // DV50
2185  ul_index = 4+pal;
2186  break;
2187  default: // DV25
2188  if (!apt) { // IEC
2189  ul_index = 0+pal;
2190  } else {
2191  ul_index = 2+pal;
2192  }
2193  }
2194 
2195  sc->container_ul = &mxf_dv_uls[ul_index].container_ul;
2196  sc->codec_ul = &mxf_dv_uls[ul_index].codec_ul;
2197 
2198  sc->frame_size = pkt->size;
2199 
2200  return 1;
2201 }
2202 
2203 static const struct {
2206  int profile;
2208  int intra_only; // 1 or 0 when there are separate UIDs for Long GOP and Intra, -1 when Intra/LGOP detection can be ignored
2209 } mxf_h264_codec_uls[] = {
2210  {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x31,0x11,0x01 }, 0, 66, 0, -1 }, // AVC Baseline
2211  {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x31,0x20,0x01 }, 0, 77, 0, -1 }, // AVC Main
2212  {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x31,0x30,0x01 }, 0, 88, 0, -1 }, // AVC Extended
2213  {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x31,0x40,0x01 }, 0, 100, 0, -1 }, // AVC High
2214  {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x31,0x50,0x01 }, 0, 110, 0, 0 }, // AVC High 10
2215  {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x31,0x60,0x01 }, 0, 122, 0, 0 }, // AVC High 422
2216  {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x31,0x70,0x01 }, 0, 244, 0, 0 }, // AVC High 444
2217  {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x32,0x20,0x01 }, 0, 110, 0, 1 }, // AVC High 10 Intra
2218  {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x32,0x21,0x01 }, 232960, 110, 1, 1 }, // AVC High 10 Intra RP2027 Class 50 1080/59.94i
2219  {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x32,0x21,0x02 }, 281088, 110, 1, 1 }, // AVC High 10 Intra RP2027 Class 50 1080/50i
2220  {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x32,0x21,0x03 }, 232960, 110, 0, 1 }, // AVC High 10 Intra RP2027 Class 50 1080/29.97p
2221  {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x32,0x21,0x04 }, 281088, 110, 0, 1 }, // AVC High 10 Intra RP2027 Class 50 1080/25p
2222  {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x32,0x21,0x08 }, 116736, 110, 0, 1 }, // AVC High 10 Intra RP2027 Class 50 720/59.94p
2223  {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x32,0x21,0x09 }, 140800, 110, 0, 1 }, // AVC High 10 Intra RP2027 Class 50 720/50p
2224  {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x32,0x30,0x01 }, 0, 122, 0, 1 }, // AVC High 422 Intra
2225  {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x32,0x31,0x01 }, 472576, 122, 1, 1 }, // AVC High 422 Intra RP2027 Class 100 1080/59.94i
2226  {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x32,0x31,0x02 }, 568832, 122, 1, 1 }, // AVC High 422 Intra RP2027 Class 100 1080/50i
2227  {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x32,0x31,0x03 }, 472576, 122, 0, 1 }, // AVC High 422 Intra RP2027 Class 100 1080/29.97p
2228  {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x32,0x31,0x04 }, 568832, 122, 0, 1 }, // AVC High 422 Intra RP2027 Class 100 1080/25p
2229  {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x32,0x31,0x08 }, 236544, 122, 0, 1 }, // AVC High 422 Intra RP2027 Class 100 720/59.94p
2230  {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x32,0x31,0x09 }, 284672, 122, 0, 1 }, // AVC High 422 Intra RP2027 Class 100 720/50p
2231  {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0d,0x04,0x01,0x02,0x02,0x01,0x32,0x40,0x01 }, 0, 244, 0, 1 }, // AVC High 444 Intra
2232  {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0d,0x04,0x01,0x02,0x02,0x01,0x32,0x50,0x01 }, 0, 44, 0, -1 }, // AVC CAVLC 444
2233 };
2234 
2236  AVPacket *pkt, MXFIndexEntry *e)
2237 {
2238  MXFContext *mxf = s->priv_data;
2239  MXFStreamContext *sc = st->priv_data;
2240  H264SPS seq, *const sps = &seq;
2241  GetBitContext gb;
2242  const uint8_t *buf = pkt->data;
2243  const uint8_t *buf_end = pkt->data + pkt->size;
2244  const uint8_t *nal_end;
2245  const UID *codec_ul = NULL;
2246  uint32_t state = -1;
2247  int extra_size = 512; // support AVC Intra files without SPS/PPS header
2248  int i, frame_size, slice_type, has_sps = 0, intra_only = 0, ret;
2249 
2250  for (;;) {
2251  buf = avpriv_find_start_code(buf, buf_end, &state);
2252  if (buf >= buf_end)
2253  break;
2254 
2255  switch (state & 0x1f) {
2256  case H264_NAL_SPS:
2257  e->flags |= 0x40;
2258 
2259  if (mxf->header_written)
2260  break;
2261 
2262  nal_end = ff_avc_find_startcode(buf, buf_end);
2263  ret = ff_avc_decode_sps(sps, buf, nal_end - buf);
2264  if (ret < 0) {
2265  av_log(s, AV_LOG_ERROR, "error parsing sps\n");
2266  return 0;
2267  }
2268  has_sps = 1;
2269 
2270  sc->aspect_ratio.num = st->codecpar->width * sps->sar.num;
2271  sc->aspect_ratio.den = st->codecpar->height * sps->sar.den;
2273  sc->aspect_ratio.num, sc->aspect_ratio.den, 1024*1024);
2274  intra_only = (sps->constraint_set_flags >> 3) & 1;
2275  sc->interlaced = !sps->frame_mbs_only_flag;
2276  sc->component_depth = sps->bit_depth_luma;
2277 
2278  buf = nal_end;
2279  break;
2280  case H264_NAL_PPS:
2281  if (e->flags & 0x40) { // sequence header present
2282  e->flags |= 0x80; // random access
2283  extra_size = 0;
2284  }
2285  break;
2286  case H264_NAL_IDR_SLICE:
2287  e->flags |= 0x04; // IDR Picture
2288  buf = buf_end;
2289  break;
2290  case H264_NAL_SLICE:
2291  init_get_bits8(&gb, buf, buf_end - buf);
2292  get_ue_golomb_long(&gb); // skip first_mb_in_slice
2293  slice_type = get_ue_golomb_31(&gb);
2294  switch (slice_type % 5) {
2295  case 0:
2296  e->flags |= 0x20; // P Picture
2297  e->flags |= 0x06; // P Picture
2298  break;
2299  case 1:
2300  e->flags |= 0x30; // B Picture
2301  e->flags |= 0x03; // non-referenced B Picture
2302  break;
2303  }
2304  buf = buf_end;
2305  break;
2306  default:
2307  break;
2308  }
2309  }
2310 
2311  if (mxf->header_written)
2312  return 1;
2313 
2314  if (!has_sps)
2315  sc->interlaced = st->codecpar->field_order != AV_FIELD_PROGRESSIVE ? 1 : 0;
2316  frame_size = pkt->size + extra_size;
2317 
2318  for (i = 0; i < FF_ARRAY_ELEMS(mxf_h264_codec_uls); i++) {
2319  if (frame_size == mxf_h264_codec_uls[i].frame_size && sc->interlaced == mxf_h264_codec_uls[i].interlaced) {
2320  codec_ul = &mxf_h264_codec_uls[i].uid;
2321  sc->component_depth = 10; // AVC Intra is always 10 Bit
2322  sc->aspect_ratio = (AVRational){ 16, 9 }; // 16:9 is mandatory for broadcast HD
2323  st->codecpar->profile = mxf_h264_codec_uls[i].profile;
2324  sc->avc_intra = 1;
2325  mxf->cbr_index = 1;
2326  sc->frame_size = pkt->size;
2327  if (sc->interlaced)
2328  sc->field_dominance = 1; // top field first is mandatory for AVC Intra
2329  break;
2330  } else if (has_sps && mxf_h264_codec_uls[i].frame_size == 0 &&
2331  mxf_h264_codec_uls[i].profile == sps->profile_idc &&
2332  (mxf_h264_codec_uls[i].intra_only < 0 ||
2333  mxf_h264_codec_uls[i].intra_only == intra_only)) {
2334  codec_ul = &mxf_h264_codec_uls[i].uid;
2335  st->codecpar->profile = sps->profile_idc;
2336  st->codecpar->level = sps->level_idc;
2337  // continue to check for avc intra
2338  }
2339  }
2340 
2341  if (!codec_ul) {
2342  av_log(s, AV_LOG_ERROR, "h264 profile not supported\n");
2343  return 0;
2344  }
2345  sc->codec_ul = codec_ul;
2346 
2347  return 1;
2348 }
2349 
2350 static const UID mxf_mpeg2_codec_uls[] = {
2351  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x01,0x10,0x00 }, // MP-ML I-Frame
2352  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x01,0x11,0x00 }, // MP-ML Long GOP
2353  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x02,0x02,0x00 }, // 422P-ML I-Frame
2354  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x02,0x03,0x00 }, // 422P-ML Long GOP
2355  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x03,0x02,0x00 }, // MP-HL I-Frame
2356  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x03,0x03,0x00 }, // MP-HL Long GOP
2357  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x04,0x02,0x00 }, // 422P-HL I-Frame
2358  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x04,0x03,0x00 }, // 422P-HL Long GOP
2359  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x05,0x02,0x00 }, // MP@H-14 I-Frame
2360  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x05,0x03,0x00 }, // MP@H-14 Long GOP
2361 };
2362 
2364 {
2365  int long_gop = 1;
2366 
2367  if (par->profile == 4) { // Main
2368  if (par->level == 8) // Main
2369  return &mxf_mpeg2_codec_uls[0+long_gop];
2370  else if (par->level == 4) // High
2371  return &mxf_mpeg2_codec_uls[4+long_gop];
2372  else if (par->level == 6) // High 14
2373  return &mxf_mpeg2_codec_uls[8+long_gop];
2374  } else if (par->profile == 0) { // 422
2375  if (par->level == 5) // Main
2376  return &mxf_mpeg2_codec_uls[2+long_gop];
2377  else if (par->level == 2) // High
2378  return &mxf_mpeg2_codec_uls[6+long_gop];
2379  }
2380  return NULL;
2381 }
2382 
2384  AVPacket *pkt, MXFIndexEntry *e)
2385 {
2386  MXFStreamContext *sc = st->priv_data;
2387  uint32_t c = -1;
2388  int i;
2389 
2390  for(i = 0; i < pkt->size - 4; i++) {
2391  c = (c<<8) + pkt->data[i];
2392  if (c == 0x1b5) {
2393  if ((pkt->data[i+1] & 0xf0) == 0x10) { // seq ext
2394  st->codecpar->profile = pkt->data[i+1] & 0x07;
2395  st->codecpar->level = pkt->data[i+2] >> 4;
2396  sc->low_delay = pkt->data[i+6] >> 7;
2397  } else if (i + 5 < pkt->size && (pkt->data[i+1] & 0xf0) == 0x80) { // pict coding ext
2398  sc->interlaced = !(pkt->data[i+5] & 0x80); // progressive frame
2399  if (sc->interlaced)
2400  sc->field_dominance = 1 + !(pkt->data[i+4] & 0x80); // top field first
2401  break;
2402  }
2403  } else if (c == 0x1b8) { // gop
2404  if (pkt->data[i+4]>>6 & 0x01) { // closed
2405  if (sc->seq_closed_gop == -1)
2406  sc->seq_closed_gop = 1;
2407  sc->closed_gop = 1;
2408  if (e->flags & 0x40) // sequence header present
2409  e->flags |= 0x80; // random access
2410  } else {
2411  sc->seq_closed_gop = 0;
2412  sc->closed_gop = 0;
2413  }
2414  } else if (c == 0x1b3) { // seq
2415  e->flags |= 0x40;
2416  switch ((pkt->data[i+4]>>4) & 0xf) {
2417  case 2: sc->aspect_ratio = (AVRational){ 4, 3}; break;
2418  case 3: sc->aspect_ratio = (AVRational){ 16, 9}; break;
2419  case 4: sc->aspect_ratio = (AVRational){221,100}; break;
2420  default:
2422  st->codecpar->width, st->codecpar->height, 1024*1024);
2423  }
2424  } else if (c == 0x100) { // pic
2425  int pict_type = (pkt->data[i+2]>>3) & 0x07;
2426  e->temporal_ref = (pkt->data[i+1]<<2) | (pkt->data[i+2]>>6);
2427  if (pict_type == 2) { // P-frame
2428  e->flags |= 0x22;
2429  sc->closed_gop = 0; // reset closed GOP, don't matter anymore
2430  } else if (pict_type == 3) { // B-frame
2431  if (sc->closed_gop)
2432  e->flags |= 0x13; // only backward prediction
2433  else
2434  e->flags |= 0x33;
2435  sc->temporal_reordering = -1;
2436  } else if (!pict_type) {
2437  av_log(s, AV_LOG_ERROR, "error parsing mpeg2 frame\n");
2438  return 0;
2439  }
2440  }
2441  }
2442  if (s->oformat != &ff_mxf_d10_muxer) {
2444  if (!codec_ul)
2445  return 0;
2446  sc->codec_ul = codec_ul;
2447  }
2448  return 1;
2449 }
2450 
2451 static uint64_t mxf_parse_timestamp(int64_t timestamp64)
2452 {
2453  time_t timestamp = timestamp64 / 1000000;
2454  struct tm tmbuf;
2455  struct tm *time = gmtime_r(&timestamp, &tmbuf);
2456  if (!time)
2457  return 0;
2458  return (uint64_t)(time->tm_year+1900) << 48 |
2459  (uint64_t)(time->tm_mon+1) << 40 |
2460  (uint64_t) time->tm_mday << 32 |
2461  time->tm_hour << 24 |
2462  time->tm_min << 16 |
2463  time->tm_sec << 8 |
2464  (timestamp64 % 1000000) / 4000;
2465 }
2466 
2468 {
2469  MXFContext *mxf = s->priv_data;
2470  uint32_t seed = av_get_random_seed();
2471  uint64_t umid = seed + 0x5294713400000000LL;
2472 
2473  AV_WB64(mxf->umid , umid);
2474  AV_WB64(mxf->umid+8, umid>>8);
2475 
2476  mxf->instance_number = seed & 0xFFFFFF;
2477 }
2478 
2480 {
2481  MXFContext *mxf = s->priv_data;
2482  AVDictionaryEntry *tcr = av_dict_get(s->metadata, "timecode", NULL, 0);
2483 
2484  if (!ff_mxf_get_content_package_rate(tbc)) {
2486  av_log(s, AV_LOG_ERROR, "Unsupported frame rate %d/%d. Set -strict option to 'unofficial' or lower in order to allow it!\n", tbc.den, tbc.num);
2487  return AVERROR(EINVAL);
2488  } else {
2489  av_log(s, AV_LOG_WARNING, "Unofficial frame rate %d/%d.\n", tbc.den, tbc.num);
2490  }
2491  }
2492 
2493  mxf->timecode_base = (tbc.den + tbc.num/2) / tbc.num;
2494  if (!tcr)
2495  tcr = av_dict_get(st->metadata, "timecode", NULL, 0);
2496 
2497  if (tcr)
2498  return av_timecode_init_from_string(&mxf->tc, av_inv_q(tbc), tcr->value, s);
2499  else
2500  return av_timecode_init(&mxf->tc, av_inv_q(tbc), 0, 0, s);
2501 }
2502 
2504 {
2505  MXFContext *mxf = s->priv_data;
2506  int i, ret;
2507  uint8_t present[FF_ARRAY_ELEMS(mxf_essence_container_uls)] = {0};
2508  int64_t timestamp = 0;
2509 
2510  if (!s->nb_streams)
2511  return -1;
2512 
2513  if (s->oformat == &ff_mxf_opatom_muxer && s->nb_streams !=1) {
2514  av_log(s, AV_LOG_ERROR, "there must be exactly one stream for mxf opatom\n");
2515  return -1;
2516  }
2517 
2518  if (!av_dict_get(s->metadata, "comment_", NULL, AV_DICT_IGNORE_SUFFIX))
2519  mxf->store_user_comments = 0;
2520 
2521  for (i = 0; i < s->nb_streams; i++) {
2522  AVStream *st = s->streams[i];
2523  MXFStreamContext *sc = av_mallocz(sizeof(*sc));
2524  if (!sc)
2525  return AVERROR(ENOMEM);
2526  st->priv_data = sc;
2527  sc->index = -1;
2528 
2529  if (((i == 0) ^ (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)) && s->oformat != &ff_mxf_opatom_muxer) {
2530  av_log(s, AV_LOG_ERROR, "there must be exactly one video stream and it must be the first one\n");
2531  return -1;
2532  }
2533 
2534  if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
2535  const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(st->codecpar->format);
2536  // TODO: should be avg_frame_rate
2537  AVRational tbc = st->time_base;
2538  // Default component depth to 8
2539  sc->component_depth = 8;
2540  sc->h_chroma_sub_sample = 2;
2541  sc->v_chroma_sub_sample = 2;
2542  sc->color_siting = 0xFF;
2543 
2546  av_make_q(st->codecpar->width, st->codecpar->height));
2547  }
2548 
2549  if (pix_desc) {
2550  sc->component_depth = pix_desc->comp[0].depth;
2551  sc->h_chroma_sub_sample = 1 << pix_desc->log2_chroma_w;
2552  sc->v_chroma_sub_sample = 1 << pix_desc->log2_chroma_h;
2553  }
2554  switch (ff_choose_chroma_location(s, st)) {
2555  case AVCHROMA_LOC_TOPLEFT: sc->color_siting = 0; break;
2556  case AVCHROMA_LOC_LEFT: sc->color_siting = 6; break;
2557  case AVCHROMA_LOC_TOP: sc->color_siting = 1; break;
2558  case AVCHROMA_LOC_CENTER: sc->color_siting = 3; break;
2559  }
2560 
2562  mxf->time_base = tbc;
2563  avpriv_set_pts_info(st, 64, mxf->time_base.num, mxf->time_base.den);
2564  if((ret = mxf_init_timecode(s, st, tbc)) < 0)
2565  return ret;
2566 
2568  sc->seq_closed_gop = -1; // unknown yet
2569  }
2570 
2571  sc->video_bit_rate = st->codecpar->bit_rate;
2572 
2573  if (s->oformat == &ff_mxf_d10_muxer ||
2576  mxf->cbr_index = 1;
2577 
2578  if (s->oformat == &ff_mxf_d10_muxer) {
2579  int ntsc = mxf->time_base.den != 25;
2580  int ul_index;
2581 
2583  av_log(s, AV_LOG_ERROR, "error MXF D-10 only support MPEG-2 Video\n");
2584  return AVERROR(EINVAL);
2585  }
2586  if ((sc->video_bit_rate == 50000000) && (mxf->time_base.den == 25)) {
2587  ul_index = 0;
2588  } else if ((sc->video_bit_rate == 49999840 || sc->video_bit_rate == 50000000) && ntsc) {
2589  ul_index = 1;
2590  } else if (sc->video_bit_rate == 40000000) {
2591  ul_index = 2+ntsc;
2592  } else if (sc->video_bit_rate == 30000000) {
2593  ul_index = 4+ntsc;
2594  } else {
2595  av_log(s, AV_LOG_ERROR, "error MXF D-10 only support 30/40/50 mbit/s\n");
2596  return -1;
2597  }
2598 
2599  sc->codec_ul = &mxf_d10_codec_uls[ul_index];
2600  sc->container_ul = &mxf_d10_container_uls[ul_index];
2601  sc->index = INDEX_D10_VIDEO;
2602  sc->signal_standard = 1;
2603  sc->color_siting = 0;
2604  sc->frame_size = (int64_t)sc->video_bit_rate *
2605  mxf->time_base.num / (8*mxf->time_base.den);
2606  }
2607  if (mxf->signal_standard >= 0)
2608  sc->signal_standard = mxf->signal_standard;
2609  } else if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
2610  char bsf_arg[32];
2611  if (st->codecpar->sample_rate != 48000) {
2612  av_log(s, AV_LOG_ERROR, "only 48khz is implemented\n");
2613  return -1;
2614  }
2615  avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
2616  if (s->oformat == &ff_mxf_d10_muxer) {
2617  if (st->index != 1) {
2618  av_log(s, AV_LOG_ERROR, "MXF D-10 only support one audio track\n");
2619  return -1;
2620  }
2621  if (st->codecpar->codec_id != AV_CODEC_ID_PCM_S16LE &&
2623  av_log(s, AV_LOG_ERROR, "MXF D-10 only support 16 or 24 bits le audio\n");
2624  }
2625  sc->index = INDEX_D10_AUDIO;
2626  sc->container_ul = ((MXFStreamContext*)s->streams[0]->priv_data)->container_ul;
2627  sc->frame_size = 4 + 8 * av_rescale_rnd(st->codecpar->sample_rate, mxf->time_base.num, mxf->time_base.den, AV_ROUND_UP) * 4;
2628  } else if (s->oformat == &ff_mxf_opatom_muxer) {
2629  AVRational tbc = av_inv_q(mxf->audio_edit_rate);
2630 
2631  if (st->codecpar->codec_id != AV_CODEC_ID_PCM_S16LE &&
2633  av_log(s, AV_LOG_ERROR, "Only pcm_s16le and pcm_s24le audio codecs are implemented\n");
2634  return AVERROR_PATCHWELCOME;
2635  }
2636  if (st->codecpar->channels != 1) {
2637  av_log(s, AV_LOG_ERROR, "MXF OPAtom only supports single channel audio\n");
2638  return AVERROR(EINVAL);
2639  }
2640 
2641  mxf->time_base = st->time_base;
2642  if((ret = mxf_init_timecode(s, st, tbc)) < 0)
2643  return ret;
2644 
2646  sc->index = INDEX_WAV;
2647  } else {
2648  mxf->slice_count = 1;
2649  sc->frame_size = st->codecpar->channels *
2652  }
2653  snprintf(bsf_arg, sizeof(bsf_arg), "r=%d/%d", mxf->tc.rate.num, mxf->tc.rate.den);
2654  ret = ff_stream_add_bitstream_filter(st, "pcm_rechunk", bsf_arg);
2655  if (ret < 0)
2656  return ret;
2657  } else if (st->codecpar->codec_type == AVMEDIA_TYPE_DATA) {
2658  AVDictionaryEntry *e = av_dict_get(st->metadata, "data_type", NULL, 0);
2659  if (e && !strcmp(e->value, "vbi_vanc_smpte_436M")) {
2660  sc->index = INDEX_S436M;
2661  } else {
2662  av_log(s, AV_LOG_ERROR, "track %d: unsupported data type\n", i);
2663  return -1;
2664  }
2665  if (st->index != s->nb_streams - 1) {
2666  av_log(s, AV_LOG_ERROR, "data track must be placed last\n");
2667  return -1;
2668  }
2669  }
2670 
2671  if (sc->index == -1) {
2673  if (sc->index == -1) {
2674  av_log(s, AV_LOG_ERROR, "track %d: could not find essence container ul, "
2675  "codec not currently supported in container\n", i);
2676  return -1;
2677  }
2678  }
2679 
2680  if (!sc->codec_ul)
2681  sc->codec_ul = &mxf_essence_container_uls[sc->index].codec_ul;
2682  if (!sc->container_ul)
2683  sc->container_ul = &mxf_essence_container_uls[sc->index].container_ul;
2684 
2685  memcpy(sc->track_essence_element_key, mxf_essence_container_uls[sc->index].element_ul, 15);
2686  sc->track_essence_element_key[15] = present[sc->index];
2687  PRINT_KEY(s, "track essence element key", sc->track_essence_element_key);
2688 
2689  if (!present[sc->index])
2690  mxf->essence_container_count++;
2691  present[sc->index]++;
2692  }
2693 
2694  if (s->oformat == &ff_mxf_d10_muxer || s->oformat == &ff_mxf_opatom_muxer) {
2695  mxf->essence_container_count = 1;
2696  }
2697 
2698  if (!(s->flags & AVFMT_FLAG_BITEXACT))
2699  mxf_gen_umid(s);
2700 
2701  for (i = 0; i < s->nb_streams; i++) {
2702  MXFStreamContext *sc = s->streams[i]->priv_data;
2703  // update element count
2704  sc->track_essence_element_key[13] = present[sc->index];
2705  if (!memcmp(sc->track_essence_element_key, mxf_essence_container_uls[INDEX_DV].element_ul, 13)) // DV
2706  sc->order = (0x15 << 24) | AV_RB32(sc->track_essence_element_key+13);
2707  else
2708  sc->order = AV_RB32(sc->track_essence_element_key+12);
2709  }
2710 
2711  if (ff_parse_creation_time_metadata(s, &timestamp, 0) > 0)
2712  mxf->timestamp = mxf_parse_timestamp(timestamp);
2713  mxf->duration = -1;
2714 
2715  mxf->timecode_track = av_mallocz(sizeof(*mxf->timecode_track));
2716  if (!mxf->timecode_track)
2717  return AVERROR(ENOMEM);
2719  if (!mxf->timecode_track->priv_data)
2720  return AVERROR(ENOMEM);
2721  mxf->timecode_track->index = -1;
2722 
2723  return 0;
2724 }
2725 
2726 static const uint8_t system_metadata_pack_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0D,0x01,0x03,0x01,0x04,0x01,0x01,0x00 };
2727 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 };
2728 
2730 {
2731  MXFContext *mxf = s->priv_data;
2732  AVIOContext *pb = s->pb;
2733  unsigned frame;
2734  uint32_t time_code;
2735  int i, system_item_bitmap = 0x58; // UL, user date/time stamp, picture present
2736 
2737  frame = mxf->last_indexed_edit_unit + mxf->edit_units_count;
2738 
2739  // write system metadata pack
2741  klv_encode_ber4_length(pb, 57);
2742 
2743  for (i = 0; i < s->nb_streams; i++) {
2745  system_item_bitmap |= 0x4;
2746  else if (s->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_DATA)
2747  system_item_bitmap |= 0x2;
2748  }
2749  avio_w8(pb, system_item_bitmap);
2750  avio_w8(pb, mxf->content_package_rate); // content package rate
2751  avio_w8(pb, 0x00); // content package type
2752  avio_wb16(pb, 0x00); // channel handle
2753  avio_wb16(pb, frame & 0xFFFF); // continuity count, supposed to overflow
2754  if (mxf->essence_container_count > 1)
2755  avio_write(pb, multiple_desc_ul, 16);
2756  else {
2757  MXFStreamContext *sc = s->streams[0]->priv_data;
2758  avio_write(pb, *sc->container_ul, 16);
2759  }
2760  avio_w8(pb, 0);
2761  avio_wb64(pb, 0);
2762  avio_wb64(pb, 0); // creation date/time stamp
2763 
2764  avio_w8(pb, 0x81); // SMPTE 12M time code
2765  time_code = av_timecode_get_smpte_from_framenum(&mxf->tc, frame);
2766  avio_wb32(pb, time_code);
2767  avio_wb32(pb, 0); // binary group data
2768  avio_wb64(pb, 0);
2769 
2770  // write system metadata package set
2772  klv_encode_ber4_length(pb, 35);
2773  avio_w8(pb, 0x83); // UMID
2774  avio_wb16(pb, 0x20);
2775  mxf_write_umid(s, 1);
2776 }
2777 
2779 {
2780  MXFContext *mxf = s->priv_data;
2781  AVIOContext *pb = s->pb;
2782  int frame_size = pkt->size / st->codecpar->block_align;
2783  uint8_t *samples = pkt->data;
2784  uint8_t *end = pkt->data + pkt->size;
2785  int i;
2786 
2787  klv_encode_ber4_length(pb, 4 + frame_size*4*8);
2788 
2789  avio_w8(pb, (frame_size == 1920 ? 0 : (mxf->edit_units_count-1) % 5 + 1));
2790  avio_wl16(pb, frame_size);
2791  avio_w8(pb, (1<<st->codecpar->channels)-1);
2792 
2793  while (samples < end) {
2794  for (i = 0; i < st->codecpar->channels; i++) {
2795  uint32_t sample;
2796  if (st->codecpar->codec_id == AV_CODEC_ID_PCM_S24LE) {
2797  sample = AV_RL24(samples)<< 4;
2798  samples += 3;
2799  } else {
2800  sample = AV_RL16(samples)<<12;
2801  samples += 2;
2802  }
2803  avio_wl32(pb, sample | i);
2804  }
2805  for (; i < 8; i++)
2806  avio_wl32(pb, i);
2807  }
2808 }
2809 
2811 {
2812  MXFContext *mxf = s->priv_data;
2813  AVIOContext *pb = s->pb;
2814  AVStream *st = s->streams[0];
2815  MXFStreamContext *sc = st->priv_data;
2816  const uint8_t *key = NULL;
2817 
2818  int err;
2819 
2820  if (!mxf->header_written)
2821  key = body_partition_key;
2822 
2823  if ((err = mxf_write_partition(s, 1, 0, key, 0)) < 0)
2824  return err;
2825  mxf_write_klv_fill(s);
2828  return 0;
2829 }
2830 
2832 {
2833  MXFContext *mxf = s->priv_data;
2834  AVIOContext *pb = s->pb;
2835 
2836  int err;
2837 
2838  if (!mxf->header_written) {
2839  if ((err = mxf_write_partition(s, 0, 0, header_open_partition_key, 1)) < 0)
2840  return err;
2841  mxf_write_klv_fill(s);
2842 
2843  if ((err = mxf_write_opatom_body_partition(s)) < 0)
2844  return err;
2845  mxf->header_written = 1;
2846  }
2847 
2848  if (!mxf->edit_unit_byte_count) {
2850  mxf->index_entries[mxf->edit_units_count].flags = ie->flags;
2852  }
2853  mxf->edit_units_count++;
2854  avio_write(pb, pkt->data, pkt->size);
2855  mxf->body_offset += pkt->size;
2856 
2857  return 0;
2858 }
2859 
2861 {
2862  MXFContext *mxf = s->priv_data;
2863  int i;
2864 
2865  if (s->oformat == &ff_mxf_opatom_muxer) {
2866  MXFStreamContext *sc = s->streams[0]->priv_data;
2867  mxf->edit_unit_byte_count = sc->frame_size;
2868  return;
2869  }
2870 
2871  mxf->edit_unit_byte_count = KAG_SIZE; // system element
2872  for (i = 0; i < s->nb_streams; i++) {
2873  AVStream *st = s->streams[i];
2874  MXFStreamContext *sc = st->priv_data;
2876  mxf->edit_unit_byte_count += 16 + 4 + sc->frame_size;
2878  }
2879 }
2880 
2882 {
2883  MXFContext *mxf = s->priv_data;
2884  AVIOContext *pb = s->pb;
2885  AVStream *st = s->streams[pkt->stream_index];
2886  MXFStreamContext *sc = st->priv_data;
2887  MXFIndexEntry ie = {0};
2888  int err;
2889 
2890  if (!mxf->header_written && pkt->stream_index != 0 &&
2891  s->oformat != &ff_mxf_opatom_muxer) {
2892  av_log(s, AV_LOG_ERROR, "Received non-video packet before "
2893  "header has been written\n");
2894  return AVERROR_INVALIDDATA;
2895  }
2896 
2897  if (!mxf->cbr_index && !mxf->edit_unit_byte_count && !(mxf->edit_units_count % EDIT_UNITS_PER_BODY)) {
2898  if ((err = av_reallocp_array(&mxf->index_entries, mxf->edit_units_count
2899  + EDIT_UNITS_PER_BODY, sizeof(*mxf->index_entries))) < 0) {
2900  mxf->edit_units_count = 0;
2901  av_log(s, AV_LOG_ERROR, "could not allocate index entries\n");
2902  return err;
2903  }
2904  }
2905 
2907  if (!mxf_parse_mpeg2_frame(s, st, pkt, &ie)) {
2908  av_log(s, AV_LOG_ERROR, "could not get mpeg2 profile and level\n");
2909  return -1;
2910  }
2911  } else if (st->codecpar->codec_id == AV_CODEC_ID_DNXHD) {
2912  if (!mxf_parse_dnxhd_frame(s, st, pkt)) {
2913  av_log(s, AV_LOG_ERROR, "could not get dnxhd profile\n");
2914  return -1;
2915  }
2916  } else if (st->codecpar->codec_id == AV_CODEC_ID_PRORES) {
2917  if (!mxf_parse_prores_frame(s, st, pkt)) {
2918  av_log(s, AV_LOG_ERROR, "could not get prores profile\n");
2919  return -1;
2920  }
2921  } else if (st->codecpar->codec_id == AV_CODEC_ID_DVVIDEO) {
2922  if (!mxf_parse_dv_frame(s, st, pkt)) {
2923  av_log(s, AV_LOG_ERROR, "could not get dv profile\n");
2924  return -1;
2925  }
2926  } else if (st->codecpar->codec_id == AV_CODEC_ID_H264) {
2927  if (!mxf_parse_h264_frame(s, st, pkt, &ie)) {
2928  av_log(s, AV_LOG_ERROR, "could not get h264 profile\n");
2929  return -1;
2930  }
2931  }
2932 
2933  if (mxf->cbr_index) {
2934  if (pkt->size != sc->frame_size && st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
2935  av_log(s, AV_LOG_ERROR, "track %d: frame size does not match index unit size, %d != %d\n",
2936  st->index, pkt->size, sc->frame_size);
2937  return -1;
2938  }
2939  if (!mxf->header_written)
2941  }
2942 
2943  if (s->oformat == &ff_mxf_opatom_muxer)
2944  return mxf_write_opatom_packet(s, pkt, &ie);
2945 
2946  if (!mxf->header_written) {
2947  if (mxf->edit_unit_byte_count) {
2948  if ((err = mxf_write_partition(s, 1, 2, header_open_partition_key, 1)) < 0)
2949  return err;
2950  mxf_write_klv_fill(s);
2952  } else {
2953  if ((err = mxf_write_partition(s, 0, 0, header_open_partition_key, 1)) < 0)
2954  return err;
2955  }
2956  mxf->header_written = 1;
2957  }
2958 
2959  if (st->index == 0) {
2960  if (!mxf->edit_unit_byte_count &&
2962  !(ie.flags & 0x33)) { // I-frame, GOP start
2963  mxf_write_klv_fill(s);
2964  if ((err = mxf_write_partition(s, 1, 2, body_partition_key, 0)) < 0)
2965  return err;
2966  mxf_write_klv_fill(s);
2968  }
2969 
2970  mxf_write_klv_fill(s);
2972 
2973  if (!mxf->edit_unit_byte_count) {
2975  mxf->index_entries[mxf->edit_units_count].flags = ie.flags;
2977  mxf->body_offset += KAG_SIZE; // size of system element
2978  }
2979  mxf->edit_units_count++;
2980  } else if (!mxf->edit_unit_byte_count && st->index == 1) {
2981  if (!mxf->edit_units_count) {
2982  av_log(s, AV_LOG_ERROR, "No packets in first stream\n");
2983  return AVERROR_PATCHWELCOME;
2984  }
2986  mxf->body_offset - mxf->index_entries[mxf->edit_units_count-1].offset;
2987  }
2988 
2989  mxf_write_klv_fill(s);
2990  avio_write(pb, sc->track_essence_element_key, 16); // write key
2991  if (s->oformat == &ff_mxf_d10_muxer &&
2993  mxf_write_d10_audio_packet(s, st, pkt);
2994  } else {
2995  klv_encode_ber4_length(pb, pkt->size); // write length
2996  avio_write(pb, pkt->data, pkt->size);
2997  mxf->body_offset += 16+4+pkt->size + klv_fill_size(16+4+pkt->size);
2998  }
2999 
3000  return 0;
3001 }
3002 
3004 {
3005  MXFContext *mxf = s->priv_data;
3006  AVIOContext *pb = s->pb;
3007  uint64_t pos = avio_tell(pb);
3008  int i;
3009 
3011  klv_encode_ber_length(pb, 28 + 12LL*mxf->body_partitions_count);
3012 
3013  if (mxf->edit_unit_byte_count && s->oformat != &ff_mxf_opatom_muxer)
3014  avio_wb32(pb, 1); // BodySID of header partition
3015  else
3016  avio_wb32(pb, 0);
3017  avio_wb64(pb, 0); // offset of header partition
3018 
3019  for (i = 0; i < mxf->body_partitions_count; i++) {
3020  avio_wb32(pb, 1); // BodySID
3021  avio_wb64(pb, mxf->body_partition_offset[i]);
3022  }
3023 
3024  avio_wb32(pb, 0); // BodySID of footer partition
3026 
3027  avio_wb32(pb, avio_tell(pb) - pos + 4);
3028 }
3029 
3031 {
3032  MXFContext *mxf = s->priv_data;
3033  AVIOContext *pb = s->pb;
3034  int i, err;
3035 
3036  if (!mxf->header_written ||
3037  (s->oformat == &ff_mxf_opatom_muxer && !mxf->body_partition_offset)) {
3038  /* reason could be invalid options/not supported codec/out of memory */
3039  return AVERROR_UNKNOWN;
3040  }
3041 
3043 
3044  mxf_write_klv_fill(s);
3046  if (mxf->edit_unit_byte_count && s->oformat != &ff_mxf_opatom_muxer) { // no need to repeat index
3047  if ((err = mxf_write_partition(s, 0, 0, footer_partition_key, 0)) < 0)
3048  return err;
3049  } else {
3050  if ((err = mxf_write_partition(s, 0, 2, footer_partition_key, 0)) < 0)
3051  return err;
3052  mxf_write_klv_fill(s);
3054  }
3055 
3056  mxf_write_klv_fill(s);
3058 
3059  if (s->pb->seekable & AVIO_SEEKABLE_NORMAL) {
3060  if (s->oformat == &ff_mxf_opatom_muxer) {
3061  /* rewrite body partition to update lengths */
3062  avio_seek(pb, mxf->body_partition_offset[0], SEEK_SET);
3063  if ((err = mxf_write_opatom_body_partition(s)) < 0)
3064  return err;
3065  }
3066 
3067  avio_seek(pb, 0, SEEK_SET);
3068  if (mxf->edit_unit_byte_count && s->oformat != &ff_mxf_opatom_muxer) {
3069  if ((err = mxf_write_partition(s, 1, 2, header_closed_partition_key, 1)) < 0)
3070  return err;
3071  mxf_write_klv_fill(s);
3073  } else {
3074  if ((err = mxf_write_partition(s, 0, 0, header_closed_partition_key, 1)) < 0)
3075  return err;
3076  }
3077  // update footer partition offset
3078  for (i = 0; i < mxf->body_partitions_count; i++) {
3079  avio_seek(pb, mxf->body_partition_offset[i]+44, SEEK_SET);
3081  }
3082  }
3083 
3084  return 0;
3085 }
3086 
3088 {
3089  MXFContext *mxf = s->priv_data;
3090 
3091  av_freep(&mxf->index_entries);
3093  if (mxf->timecode_track) {
3095  av_freep(&mxf->timecode_track);
3096  }
3097 }
3098 
3100 {
3101  int i, stream_count = 0;
3102 
3103  for (i = 0; i < s->nb_streams; i++)
3104  stream_count += !!s->streams[i]->internal->last_in_packet_buffer;
3105 
3106  if (stream_count && (s->nb_streams == stream_count || flush)) {
3107  AVPacketList *pktl = s->internal->packet_buffer;
3108  if (s->nb_streams != stream_count) {
3109  AVPacketList *last = NULL;
3110  // find last packet in edit unit
3111  while (pktl) {
3112  if (!stream_count || pktl->pkt.stream_index == 0)
3113  break;
3114  // update last packet in packet buffer
3115  if (s->streams[pktl->pkt.stream_index]->internal->last_in_packet_buffer != pktl)
3117  last = pktl;
3118  pktl = pktl->next;
3119  stream_count--;
3120  }
3121  // purge packet queue
3122  while (pktl) {
3123  AVPacketList *next = pktl->next;
3124  av_packet_unref(&pktl->pkt);
3125  av_freep(&pktl);
3126  pktl = next;
3127  }
3128  if (last)
3129  last->next = NULL;
3130  else {
3131  s->internal->packet_buffer = NULL;
3133  goto out;
3134  }
3135  pktl = s->internal->packet_buffer;
3136  }
3137 
3138  *out = pktl->pkt;
3139  av_log(s, AV_LOG_TRACE, "out st:%d dts:%"PRId64"\n", (*out).stream_index, (*out).dts);
3140  s->internal->packet_buffer = pktl->next;
3141  if(s->streams[pktl->pkt.stream_index]->internal->last_in_packet_buffer == pktl)
3143  if(!s->internal->packet_buffer)
3145  av_freep(&pktl);
3146  return 1;
3147  } else {
3148  out:
3149  return 0;
3150  }
3151 }
3152 
3154  const AVPacket *pkt)
3155 {
3157  MXFStreamContext *sc2 = s->streams[next->stream_index]->priv_data;
3158 
3159  return next->dts > pkt->dts ||
3160  (next->dts == pkt->dts && sc->order < sc2->order);
3161 }
3162 
3164 {
3165  int ret;
3166  if (pkt) {
3168  pkt->pts = pkt->dts = sc->pkt_cnt++;
3169  if ((ret = ff_interleave_add_packet(s, pkt, mxf_compare_timestamps)) < 0)
3170  return ret;
3171  }
3172  return mxf_interleave_get_packet(s, out, NULL, flush);
3173 }
3174 
3175 #define MXF_COMMON_OPTIONS \
3176  { "signal_standard", "Force/set Signal Standard",\
3177  offsetof(MXFContext, signal_standard), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 7, AV_OPT_FLAG_ENCODING_PARAM, "signal_standard"},\
3178  { "bt601", "ITU-R BT.601 and BT.656, also SMPTE 125M (525 and 625 line interlaced)",\
3179  0, AV_OPT_TYPE_CONST, {.i64 = 1}, -1, 7, AV_OPT_FLAG_ENCODING_PARAM, "signal_standard"},\
3180  { "bt1358", "ITU-R BT.1358 and ITU-R BT.799-3, also SMPTE 293M (525 and 625 line progressive)",\
3181  0, AV_OPT_TYPE_CONST, {.i64 = 2}, -1, 7, AV_OPT_FLAG_ENCODING_PARAM, "signal_standard"},\
3182  { "smpte347m", "SMPTE 347M (540 Mbps mappings)",\
3183  0, AV_OPT_TYPE_CONST, {.i64 = 3}, -1, 7, AV_OPT_FLAG_ENCODING_PARAM, "signal_standard"},\
3184  { "smpte274m", "SMPTE 274M (1125 line)",\
3185  0, AV_OPT_TYPE_CONST, {.i64 = 4}, -1, 7, AV_OPT_FLAG_ENCODING_PARAM, "signal_standard"},\
3186  { "smpte296m", "SMPTE 296M (750 line progressive)",\
3187  0, AV_OPT_TYPE_CONST, {.i64 = 5}, -1, 7, AV_OPT_FLAG_ENCODING_PARAM, "signal_standard"},\
3188  { "smpte349m", "SMPTE 349M (1485 Mbps mappings)",\
3189  0, AV_OPT_TYPE_CONST, {.i64 = 6}, -1, 7, AV_OPT_FLAG_ENCODING_PARAM, "signal_standard"},\
3190  { "smpte428", "SMPTE 428-1 DCDM",\
3191  0, AV_OPT_TYPE_CONST, {.i64 = 7}, -1, 7, AV_OPT_FLAG_ENCODING_PARAM, "signal_standard"},
3192 
3193 
3194 
3195 static const AVOption mxf_options[] = {
3197  { "store_user_comments", "",
3198  offsetof(MXFContext, store_user_comments), AV_OPT_TYPE_BOOL, {.i64 = 1}, 0, 1, AV_OPT_FLAG_ENCODING_PARAM},
3199  { NULL },
3200 };
3201 
3202 static const AVClass mxf_muxer_class = {
3203  .class_name = "MXF muxer",
3204  .item_name = av_default_item_name,
3205  .option = mxf_options,
3206  .version = LIBAVUTIL_VERSION_INT,
3207 };
3208 
3209 static const AVOption d10_options[] = {
3210  { "d10_channelcount", "Force/set channelcount in generic sound essence descriptor",
3211  offsetof(MXFContext, channel_count), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 8, AV_OPT_FLAG_ENCODING_PARAM},
3213  { "store_user_comments", "",
3214  offsetof(MXFContext, store_user_comments), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, AV_OPT_FLAG_ENCODING_PARAM},
3215  { NULL },
3216 };
3217 
3219  .class_name = "MXF-D10 muxer",
3220  .item_name = av_default_item_name,
3221  .option = d10_options,
3222  .version = LIBAVUTIL_VERSION_INT,
3223 };
3224 
3225 static const AVOption opatom_options[] = {
3226  { "mxf_audio_edit_rate", "Audio edit rate for timecode",
3227  offsetof(MXFContext, audio_edit_rate), AV_OPT_TYPE_RATIONAL, {.dbl=25}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
3229  { "store_user_comments", "",
3230  offsetof(MXFContext, store_user_comments), AV_OPT_TYPE_BOOL, {.i64 = 1}, 0, 1, AV_OPT_FLAG_ENCODING_PARAM},
3231  { NULL },
3232 };
3233 
3235  .class_name = "MXF-OPAtom muxer",
3236  .item_name = av_default_item_name,
3237  .option = opatom_options,
3238  .version = LIBAVUTIL_VERSION_INT,
3239 };
3240 
3242  .name = "mxf",
3243  .long_name = NULL_IF_CONFIG_SMALL("MXF (Material eXchange Format)"),
3244  .mime_type = "application/mxf",
3245  .extensions = "mxf",
3246  .priv_data_size = sizeof(MXFContext),
3247  .audio_codec = AV_CODEC_ID_PCM_S16LE,
3248  .video_codec = AV_CODEC_ID_MPEG2VIDEO,
3252  .deinit = mxf_deinit,
3255  .priv_class = &mxf_muxer_class,
3256 };
3257 
3258 AVOutputFormat ff_mxf_d10_muxer = {
3259  .name = "mxf_d10",
3260  .long_name = NULL_IF_CONFIG_SMALL("MXF (Material eXchange Format) D-10 Mapping"),
3261  .mime_type = "application/mxf",
3262  .priv_data_size = sizeof(MXFContext),
3263  .audio_codec = AV_CODEC_ID_PCM_S16LE,
3264  .video_codec = AV_CODEC_ID_MPEG2VIDEO,
3268  .deinit = mxf_deinit,
3271  .priv_class = &mxf_d10_muxer_class,
3272 };
3273 
3274 AVOutputFormat ff_mxf_opatom_muxer = {
3275  .name = "mxf_opatom",
3276  .long_name = NULL_IF_CONFIG_SMALL("MXF (Material eXchange Format) Operational Pattern Atom"),
3277  .mime_type = "application/mxf",
3278  .extensions = "mxf",
3279  .priv_data_size = sizeof(MXFContext),
3280  .audio_codec = AV_CODEC_ID_PCM_S16LE,
3281  .video_codec = AV_CODEC_ID_DNXHD,
3285  .deinit = mxf_deinit,
3288  .priv_class = &mxf_opatom_muxer_class,
3289 };
uint8_t profile_idc
Definition: avc.h:41
uint8_t bit_depth_luma
Definition: avc.h:45
static const struct @276 mxf_dv_uls[]
int cid
Definition: mxfenc.c:2038
static void write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, int unqueue)
Definition: ffmpeg.c:726
void avio_wb64(AVIOContext *s, uint64_t val)
Definition: aviobuf.c:453
uint32_t tagged_value_count
Definition: mxfenc.c:417
#define NULL
Definition: coverity.c:32
enum AVFieldOrder field_order
Video only.
Definition: codec_par.h:141
void avio_wl16(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:459
MXFMetadataSetType
Definition: mxf.h:30
Bytestream IO Context.
Definition: avio.h:161
enum AVColorTransferCharacteristic color_trc
Definition: codec_par.h:148
#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:706
uint8_t constraint_set_flags
Definition: avc.h:43
version
Definition: libkvazaar.c:320
int component_depth
Definition: mxfenc.c:85
static const uint8_t header_closed_partition_key[]
Definition: mxfenc.c:243
#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:499
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2573
MPEG-2/4 4:2:0, H.264 default for 4:2:0.
Definition: pixfmt.h:607
int signal_standard
Definition: mxfenc.c:87
const uint8_t * ff_avc_find_startcode(const uint8_t *p, const uint8_t *end)
Definition: avc.c:67
int avio_put_str16be(AVIOContext *s, const char *str)
Convert an UTF-8 string to UTF-16BE and write it.
uint8_t unused_tags[MXF_NUM_TAGS]
local tags that we know will not be used
Definition: mxfenc.c:422
AVOption.
Definition: opt.h:248
static void flush(AVCodecContext *avctx)
static void mxf_write_index_table_segment(AVFormatContext *s)
Definition: mxfenc.c:1738
enum AVCodecID id
Definition: mxfenc.c:132
uint64_t body_offset
Definition: mxfenc.c:412
static void mxf_write_timecode_component(AVFormatContext *s, AVStream *st, MXFPackage *package)
Definition: mxfenc.c:935
#define PRINT_KEY(pc, s, x)
Definition: mxf.h:147
static const uint8_t klv_fill_key[]
Definition: mxfenc.c:244
static const UID mxf_mpegvideo_descriptor_key
Definition: mxfenc.c:1088
static const UID mxf_d10_codec_uls[]
Definition: mxfenc.c:213
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:200
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
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:481
void(* write_desc)(AVFormatContext *, AVStream *)
Definition: mxfenc.c:107
int index
index in mxf_essence_container_uls table
Definition: mxfenc.c:79
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:4905
AVRational sample_aspect_ratio
Video only.
Definition: codec_par.h:136
unsigned body_partitions_count
Definition: mxfenc.c:404
uint8_t level_idc
Definition: avc.h:42
AVRational white_point[2]
CIE 1931 xy chromaticity coords of white point.
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:60
int num
Numerator.
Definition: rational.h:59
int index
stream index in AVFormatContext
Definition: avformat.h:885
int size
Definition: packet.h:364
Definition: mxf.h:34
static const UID mxf_cdci_descriptor_key
Definition: mxfenc.c:1091
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:253
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:235
AVFormatInternal * internal
An opaque field for libavformat internal usage.
Definition: avformat.h:1708
int interlaced
whether picture is interlaced
Definition: mxfenc.c:83
int av_log2(unsigned v)
Definition: intmath.c:26
static const uint8_t body_partition_key[]
Definition: mxfenc.c:245
static int64_t mxf_write_generic_sound_common(AVFormatContext *s, AVStream *st, const UID key)
Definition: mxfenc.c:1429
AVOutputFormat ff_mxf_muxer
Definition: mxfenc.c:3241
static void mxf_update_klv_size(AVIOContext *pb, int64_t pos)
Definition: mxfenc.c:1330
#define FF_PROFILE_PRORES_LT
Definition: avcodec.h:1958
GLint GLenum type
Definition: opengl_enc.c:104
int avpriv_dnxhd_get_interlaced(int cid)
Definition: dnxhddata.c:1108
static void mxf_write_s436m_anc_desc(AVFormatContext *s, AVStream *st)
Definition: mxfenc.c:1385
void * priv_data
Definition: avformat.h:899
int has_primaries
Flag indicating whether the display primaries (and white point) are set.
#define FF_ARRAY_ELEMS(a)
const char * key
#define FF_MXF_MasteringDisplayWhitePointChromaticity
Definition: mxf.h:90
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:237
static void mxf_write_sequence(AVFormatContext *s, AVStream *st, MXFPackage *package)
Definition: mxfenc.c:908
static AVPacket pkt
static void mxf_write_uuid(AVIOContext *pb, enum MXFMetadataSetType type, int value)
Definition: mxfenc.c:425
#define sample
static int mxf_init_timecode(AVFormatContext *s, AVStream *st, AVRational tbc)
Definition: mxfenc.c:2479
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_RL16
Definition: bytestream.h:91
UID track_essence_element_key
Definition: mxfenc.c:78
static void mxf_write_common_fields(AVFormatContext *s, AVStream *st)
Definition: mxfenc.c:884
int strict_std_compliance
Allow non-standard and experimental extension.
Definition: avformat.h:1551
const MXFCodecUL ff_mxf_color_space_uls[]
Definition: mxf.c:127
#define FF_PROFILE_PRORES_XQ
Definition: avcodec.h:1962
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:100
This struct describes the properties of an encoded stream.
Definition: codec_par.h:52
static const UID mxf_s436m_anc_descriptor_key
Definition: mxfenc.c:1087
enum AVColorSpace color_space
Definition: codec_par.h:149
static int mxf_write_header(AVFormatContext *s)
Definition: mxfenc.c:2503
Mastering display metadata (based on SMPTE-2086:2014).
Definition: packet.h:222
static void klv_encode_ber4_length(AVIOContext *pb, int len)
Definition: mxfenc.c:475
static const uint8_t multiple_desc_ul[]
Definition: mxfenc.c:251
Format I/O context.
Definition: avformat.h:1243
int h_chroma_sub_sample
Definition: mxfenc.c:88
uint8_t UID[16]
Definition: mxf.h:28
UID uid
Definition: mxfenc.c:2204
int temporal_reordering
Definition: mxfenc.c:90
#define gmtime_r
Definition: time_internal.h:34
uint8_t slice_count
index slice count minus 1 (1 if no audio, 0 otherwise)
Definition: mxfenc.c:401
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:1092
const UID container_ul
Definition: mxfenc.c:2117
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
static int klv_encode_ber_length(AVIOContext *pb, uint64_t len)
Definition: mxfenc.c:455
static void mxf_write_klv_fill(AVFormatContext *s)
Definition: mxfenc.c:1884
static void mxf_write_refs_count(AVIOContext *pb, int ref_count)
Definition: mxfenc.c:441
int timecode_base
rounded time code base (25 or 30)
Definition: mxfenc.c:409
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:250
static void mxf_write_structural_component(AVFormatContext *s, AVStream *st, MXFPackage *package)
Definition: mxfenc.c:962
static int mxf_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: mxfenc.c:2881
static void mxf_write_avc_subdesc(AVFormatContext *s, AVStream *st)
Definition: mxfenc.c:1339
AVOutputFormat ff_mxf_d10_muxer
Definition: mxfenc.c:3258
void avio_wl32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:379
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:1729
uint8_t
Round toward +infinity.
Definition: mathematics.h:83
Opaque data information usually continuous.
Definition: avutil.h:203
int width
Video only.
Definition: codec_par.h:126
AVOptions.
A point in the output bytestream where the underlying AVIOContext might flush the buffer depending on...
Definition: avio.h:146
static const UID mxf_mpeg2_codec_uls[]
Definition: mxfenc.c:2350
#define AV_LOG_TRACE
Extremely verbose debugging, useful for libav* development.
Definition: log.h:220
AVPacket pkt
Definition: packet.h:397
timecode is drop frame
Definition: timecode.h:36
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
GLsizei GLboolean const GLfloat * value
Definition: opengl_enc.c:108
int frame_size
frame size in bytes
Definition: mxfenc.c:95
struct AVPacketList * last_in_packet_buffer
last packet in packet_buffer for this stream when muxing.
Definition: internal.h:350
#define LIBAVFORMAT_VERSION_MAJOR
Definition: version.h:34
static int64_t mxf_write_generic_desc(AVFormatContext *s, AVStream *st, const UID key)
Definition: mxfenc.c:1049
void avio_write_marker(AVIOContext *s, int64_t time, enum AVIODataMarkerType type)
Mark the written bytestream as a specific type.
Definition: aviobuf.c:483
uint8_t * av_stream_get_side_data(const AVStream *stream, enum AVPacketSideDataType type, int *size)
Get side information from stream.
Definition: utils.c:5465
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:91
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1311
int avpriv_dnxhd_get_frame_size(int cid)
Definition: dnxhddata.c:1086
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:1374
uint8_t * data
Definition: packet.h:363
uint64_t duration
Definition: mxfenc.c:406
int start
timecode frame start (first base frame number)
Definition: timecode.h:42
uint32_t tag
Definition: movenc.c:1597
uint64_t offset
Definition: mxfenc.c:70
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:210
Definition: ismindex.c:69
int instance
Definition: mxfenc.c:113
ptrdiff_t size
Definition: opengl_enc.c:100
uint8_t flags
Definition: mxfenc.c:73
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:557
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
int has_luminance
Flag indicating whether the luminance (min_ and max_) have been set.
static const struct @277 mxf_h264_codec_uls[]
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:76
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:225
static int interleave_packet(AVFormatContext *s, AVPacket *out, AVPacket *in, int flush)
Interleave an AVPacket correctly so it can be muxed.
Definition: mux.c:1086
#define AVFMT_FLAG_BITEXACT
When muxing, try to avoid writing any random/volatile data to the output.
Definition: avformat.h:1391
AVRational audio_edit_rate
Definition: mxfenc.c:418
unsigned slice_offset
offset of audio slice
Definition: mxfenc.c:71
enum ULIndex index
Definition: mxfenc.c:133
static struct @321 state
static const uint8_t index_table_segment_key[]
Definition: mxfenc.c:241
#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:278
const uint8_t * avpriv_find_start_code(const uint8_t *p, const uint8_t *end, uint32_t *state)
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
Definition: codec_par.h:89
#define FF_COMPLIANCE_UNOFFICIAL
Allow unofficial extensions.
Definition: avcodec.h:1600
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:219
int store_user_comments
Definition: mxfenc.c:419
static const AVClass mxf_opatom_muxer_class
Definition: mxfenc.c:3234
H.264 parameter set handling.
static const uint8_t umid_ul[]
Definition: mxfenc.c:232
static void mxf_write_tape_descriptor(AVFormatContext *s)
Definition: mxfenc.c:999
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:5664
const MXFCodecUL ff_mxf_color_primaries_uls[]
Definition: mxf.c:91
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: codec_id.h:46
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
int av_get_bits_per_sample(enum AVCodecID codec_id)
Return codec bits per sample.
Definition: utils.c:1610
AVDictionary * metadata
Metadata that applies to the whole file.
Definition: avformat.h:1483
uint8_t log2_chroma_h
Amount to shift the luma height right to find the chroma height.
Definition: pixdesc.h:101
static void mxf_write_system_item(AVFormatContext *s)
Definition: mxfenc.c:2729
#define EDIT_UNITS_PER_BODY
Definition: mxfenc.c:66
#define MXF_NUM_TAGS
Definition: mxfenc.c:390
static const uint8_t header_metadata_key[]
partial key for header metadata
Definition: mxfenc.c:250
static const uint8_t mxf_indirect_value_utf16le[]
Definition: mxfenc.c:1509
int profile
Definition: mxfenc.c:2002
int ff_avc_decode_sps(H264SPS *sps, const uint8_t *buf, int buf_size)
Definition: avc.c:334
Definition: mxf.h:42
static const struct @274 mxf_prores_codec_uls[]
static int64_t mxf_write_wav_common(AVFormatContext *s, AVStream *st, const UID key)
Definition: mxfenc.c:1476
int v_chroma_sub_sample
Definition: mxfenc.c:89
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:115
enum AVColorPrimaries color_primaries
Definition: codec_par.h:147
static const uint8_t header_open_partition_key[]
Definition: mxfenc.c:242
static void mxf_write_preface(AVFormatContext *s)
Definition: mxfenc.c:629
unsigned int pos
Definition: spdifenc.c:410
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:215
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:56
int header_written
Definition: mxfenc.c:397
static void mxf_write_random_index_pack(AVFormatContext *s)
Definition: mxfenc.c:3003
static void mxf_write_cdci_desc(AVFormatContext *s, AVStream *st)
Definition: mxfenc.c:1363
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:2810
GLsizei GLsizei * length
Definition: opengl_enc.c:114
int cbr_index
use a constant bitrate index
Definition: mxfenc.c:421
static void mxf_write_content_storage(AVFormatContext *s, MXFPackage *packages, int package_count)
Definition: mxfenc.c:809
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_RL24
Definition: bytestream.h:91
static void mxf_write_metadata_key(AVIOContext *pb, unsigned int value)
Definition: mxfenc.c:587
uint8_t frame_mbs_only_flag
Definition: avc.h:47
static const struct @275 mxf_dnxhd_codec_uls[]
GLsizei count
Definition: opengl_enc.c:108
#define FF_MXF_MasteringDisplayPrimaries
Definition: mxf.h:89
#define MXF_COMMON_OPTIONS
Definition: mxfenc.c:3175
#define FFMAX(a, b)
Definition: common.h:103
int color_siting
Definition: mxfenc.c:86
AVRational max_luminance
Max luminance of mastering display (cd/m^2).
int64_t pkt_cnt
pkt counter for muxed packets
Definition: mxfenc.c:77
#define LIBAVFORMAT_VERSION_MINOR
Definition: version.h:35
int order
interleaving order if dts are equal
Definition: mxfenc.c:82
const uint8_t ff_mxf_random_index_pack_key[16]
Definition: mxf.c:25
static void mxf_write_d10_audio_packet(AVFormatContext *s, AVStream *st, AVPacket *pkt)
Definition: mxfenc.c:2778
static const uint8_t primer_pack_key[]
Definition: mxfenc.c:240
static const uint8_t system_metadata_pack_key[]
Definition: mxfenc.c:2726
static int mxf_get_essence_container_ul_index(enum AVCodecID id)
Definition: mxfenc.c:490
int av_reallocp_array(void *ptr, size_t nmemb, size_t size)
Allocate, reallocate, or free an array through a pointer to a pointer.
Definition: mem.c:206
int intra_only
Definition: mxfenc.c:2208
static int klv_ber_length(uint64_t len)
Definition: mxfenc.c:447
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:1299
int block_align
Audio only.
Definition: codec_par.h:177
int seekable
A combination of AVIO_SEEKABLE_ flags or 0 when the stream is not seekable.
Definition: avio.h:260
static int mxf_parse_h264_frame(AVFormatContext *s, AVStream *st, AVPacket *pkt, MXFIndexEntry *e)
Definition: mxfenc.c:2235
static const UID mxf_wav_descriptor_key
Definition: mxfenc.c:1089
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition: avassert.h:53
int64_t av_rescale(int64_t a, int64_t b, int64_t c)
Rescale a 64-bit integer with rounding to nearest.
Definition: mathematics.c:129
void ffio_fill(AVIOContext *s, int b, int count)
Definition: aviobuf.c:211
#define KAG_SIZE
Definition: mxfenc.c:67
#define av_clip_uint16
Definition: common.h:134
static const uint8_t op1a_ul[]
complete key for operation pattern, partitions, and primer pack
Definition: mxfenc.c:237
uint8_t interlaced
Definition: mxfenc.c:2207
AVRational min_luminance
Min luminance of mastering display (cd/m^2).
static int mxf_parse_dnxhd_frame(AVFormatContext *s, AVStream *st, AVPacket *pkt)
Definition: mxfenc.c:2063
#define LIBAVFORMAT_VERSION_MICRO
Definition: version.h:36
#define FF_PROFILE_PRORES_STANDARD
Definition: avcodec.h:1959
uint64_t * body_partition_offset
Definition: mxfenc.c:403
struct MXFPackage * ref
Definition: mxfenc.c:114
typedef void(APIENTRY *FF_PFNGLACTIVETEXTUREPROC)(GLenum texture)
static int write_trailer(AVFormatContext *s1)
Definition: v4l2enc.c:98
ITU-R 601, SMPTE 274M 296M S314M(DV 4:1:1), mpeg2 4:2:2.
Definition: pixfmt.h:609
static const AVOption mxf_options[]
Definition: mxfenc.c:3195
ULIndex
Definition: mxfenc.c:117
void avio_wb24(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:477
static const AVClass mxf_muxer_class
Definition: mxfenc.c:3202
const char * name
Definition: avformat.h:500
static int mxf_write_partition(AVFormatContext *s, int bodysid, int indexsid, const uint8_t *key, int write_metadata)
Definition: mxfenc.c:1896
static uint16_t rescale_mastering_chroma(AVRational q)
Definition: mxfenc.c:1096
static void mxf_write_local_tag_utf16(AVFormatContext *s, int tag, const char *value)
Definition: mxfenc.c:725
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
#define s(width, name)
Definition: cbs_vp9.c:257
static int mxf_write_header_metadata_sets(AVFormatContext *s)
Definition: mxfenc.c:1680
AVDictionary * metadata
Definition: avformat.h:948
AVStream * timecode_track
Definition: mxfenc.c:408
#define FF_MXF_MasteringDisplayMinimumLuminance
Definition: mxf.h:92
int slice_offset
Definition: mxfenc.c:94
static const UID mxf_aes3_descriptor_key
Definition: mxfenc.c:1090
enum AVColorRange color_range
Video only.
Definition: codec_par.h:146
const MXFCodecUL ff_mxf_data_definition_uls[]
SMPTE RP224 http://www.smpte-ra.org/mdd/index.html.
Definition: mxf.c:30
static const uint8_t system_metadata_package_set_key[]
Definition: mxfenc.c:2727
int edit_unit_byte_count
fixed edit unit byte count
Definition: mxfenc.c:410
int64_t av_rescale_rnd(int64_t a, int64_t b, int64_t c, enum AVRounding rnd)
Rescale a 64-bit integer with specified rounding.
Definition: mathematics.c:58
#define FF_MXF_MASTERING_LUMA_DEN
Definition: mxf.h:95
preferred ID for MPEG-1/2 video decoding
Definition: codec_id.h:51
static void mxf_mark_tag_unused(MXFContext *mxf, int tag)
Definition: mxfenc.c:511
ff_const59 struct AVOutputFormat * oformat
The output container format.
Definition: avformat.h:1262
static uint64_t mxf_parse_timestamp(int64_t timestamp64)
Definition: mxfenc.c:2451
if(ret)
Stream structure.
Definition: avformat.h:884
static unsigned get_ue_golomb_long(GetBitContext *gb)
Read an unsigned Exp-Golomb code in the range 0 to UINT32_MAX-1.
Definition: golomb.h:106
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
int content_package_rate
content package rate in system element, see SMPTE 326M
Definition: mxfenc.c:411
#define AVFMT_NOTIMESTAMPS
Format does not need / have any timestamps.
Definition: avformat.h:462
uint32_t instance_number
Definition: mxfenc.c:413
static int mxf_write_user_comments(AVFormatContext *s, const AVDictionary *m)
Definition: mxfenc.c:1540
#define FF_PROFILE_PRORES_4444
Definition: avcodec.h:1961
int b_picture_count
maximum number of consecutive b pictures, used in mpeg-2 descriptor
Definition: mxfenc.c:98
int signal_standard
Definition: mxfenc.c:416
#define AVIO_SEEKABLE_NORMAL
Seeking works like for a local file.
Definition: avio.h:40
static void mxf_write_aes3_desc(AVFormatContext *s, AVStream *st)
Definition: mxfenc.c:1497
int max_gop
maximum gop size, used by mpeg-2 descriptor
Definition: mxfenc.c:97
int frame_size
Definition: mxfenc.c:2205
int last_indexed_edit_unit
Definition: mxfenc.c:402
static const AVClass mxf_d10_muxer_class
Definition: mxfenc.c:3218
static int mxf_parse_mpeg2_frame(AVFormatContext *s, AVStream *st, AVPacket *pkt, MXFIndexEntry *e)
Definition: mxfenc.c:2383
AVStreamInternal * internal
An opaque field for libavformat internal usage.
Definition: avformat.h:1123
const UID * codec_ul
Definition: mxfenc.c:80
static int get_ue_golomb_31(GetBitContext *gb)
read unsigned exp golomb code, constraint to a max of 31.
Definition: golomb.h:122
Timecode helpers header.
AVIOContext * pb
I/O context.
Definition: avformat.h:1285
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:677
static AVRational av_make_q(int num, int den)
Create an AVRational.
Definition: rational.h:71
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:203
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:606
static void mxf_write_essence_container_refs(AVFormatContext *s)
Definition: mxfenc.c:607
static unsigned int seed
Definition: videogen.c:78
static const UID * mxf_get_mpeg2_codec_ul(AVCodecParameters *par)
Definition: mxfenc.c:2363
static void mxf_write_package(AVFormatContext *s, MXFPackage *package)
Definition: mxfenc.c:1558
AVRational rate
frame rate in rational form
Definition: timecode.h:44
int seq_closed_gop
all gops in sequence are closed, used in mpeg-2 descriptor
Definition: mxfenc.c:96
int closed_gop
gop is closed, used in mpeg-2 frame parsing
Definition: mxfenc.c:92
#define PLATFORM_IDENT
Definition: mxfenc.c:755
uint64_t timestamp
timestamp, as year(16),month(8),day(8),hour(8),minutes(8),msec/4(8)
Definition: mxfenc.c:400
int low_delay
low delay, used in mpeg-2 descriptor
Definition: mxfenc.c:99
#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:91
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:257
int track_instance_count
Definition: mxfenc.c:420
Rational number (pair of numerator and denominator).
Definition: rational.h:58
Mastering display metadata capable of representing the color volume of the display used to master the...
static int64_t mxf_write_cdci_common(AVFormatContext *s, AVStream *st, const UID key)
Definition: mxfenc.c:1106
uint16_t temporal_ref
Definition: mxfenc.c:72
static uint64_t mxf_utf16len(const char *utf8_str)
Definition: mxfenc.c:684
static void mxf_write_primer_pack(AVFormatContext *s)
Definition: mxfenc.c:517
#define FF_PROFILE_PRORES_HQ
Definition: avcodec.h:1960
int avio_put_str16le(AVIOContext *s, const char *str)
Convert an UTF-8 string to UTF-16LE and write it.
static int FUNC() sps(CodedBitstreamContext *ctx, RWContext *rw, H264RawSPS *current)
static uint32_t rescale_mastering_luma(AVRational q)
Definition: mxfenc.c:1101
#define snprintf
Definition: snprintf.h:34
static void mxf_write_track(AVFormatContext *s, AVStream *st, MXFPackage *package)
Definition: mxfenc.c:836
Definition: avc.h:39
MXFIndexEntry * index_entries
Definition: mxfenc.c:398
AVRational time_base
Definition: mxfenc.c:396
AVClass * av_class
Definition: mxfenc.c:393
Definition: mxf.h:97
int video_bit_rate
Definition: mxfenc.c:93
static const uint8_t uuid_base[]
Definition: mxfenc.c:231
AVRational display_primaries[3][2]
CIE 1931 xy chromaticity coords of color primaries (r, g, b order).
int channel_count
Definition: mxfenc.c:415
static void mxf_write_h264_desc(AVFormatContext *s, AVStream *st)
Definition: mxfenc.c:1373
#define LIBAVFORMAT_VERSION
Definition: version.h:41
void avio_wb16(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:465
static int mxf_parse_prores_frame(AVFormatContext *s, AVStream *st, AVPacket *pkt)
Definition: mxfenc.c:2013
static const MXFContainerEssenceEntry mxf_essence_container_uls[]
Definition: mxfenc.c:154
enum AVChromaLocation ff_choose_chroma_location(AVFormatContext *s, AVStream *st)
Chooses a timebase for muxing the specified stream.
Definition: mux.c:105
const UID * container_ul
Definition: mxfenc.c:81
Narrow or limited range content.
Definition: pixfmt.h:569
static av_always_inline AVRational av_inv_q(AVRational q)
Invert a rational.
Definition: rational.h:159
static int mxf_interleave_get_packet(AVFormatContext *s, AVPacket *out, AVPacket *pkt, int flush)
Definition: mxfenc.c:3099
int sample_rate
Audio only.
Definition: codec_par.h:170
#define DNXHD_VARIABLE
Indicate that a CIDEntry value must be read in the bitstream.
Definition: dnxhddata.h:40
static const UID mxf_d10_container_uls[]
Definition: mxfenc.c:222
int64_t footer_partition_offset
Definition: mxfenc.c:394
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:756
Definition: mxf.h:37
common internal api header.
struct AVPacketList * next
Definition: packet.h:398
static const uint8_t footer_partition_key[]
Definition: mxfenc.c:239
static int mxf_write_essence_container_data(AVFormatContext *s)
Definition: mxfenc.c:1658
static void write_metadata(AVFormatContext *s, unsigned int ts)
Definition: flvenc.c:274
int profile
Codec-specific bitstream restrictions that the stream conforms to.
Definition: codec_par.h:120
static const struct @273 mxf_essence_mappings[]
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
#define FF_MXF_MasteringDisplayMaximumLuminance
Definition: mxf.h:91
int field_dominance
tff=1, bff=2
Definition: mxfenc.c:84
static const UID mxf_avc_subdescriptor_key
Definition: mxfenc.c:1094
static const AVOption d10_options[]
Definition: mxfenc.c:3209
static int mxf_compare_timestamps(AVFormatContext *s, const AVPacket *next, const AVPacket *pkt)
Definition: mxfenc.c:3153
UID codec_ul
Definition: mxfenc.c:2003
struct AVPacketList * packet_buffer_end
Definition: internal.h:77
int last_key_index
index of last key frame
Definition: mxfenc.c:405
char * value
Definition: dict.h:87
int len
int ff_stream_add_bitstream_filter(AVStream *st, const char *name, const char *args)
Add a bitstream filter to a stream.
Definition: utils.c:5536
void * priv_data
Format private data.
Definition: avformat.h:1271
int ff_mxf_get_content_package_rate(AVRational time_base)
Definition: mxf.c:213
static void mxf_gen_umid(AVFormatContext *s)
Definition: mxfenc.c:2467
static void mxf_deinit(AVFormatContext *s)
Definition: mxfenc.c:3087
static void write_header(FFV1Context *f)
Definition: ffv1enc.c:346
uint8_t umid[16]
unique material identifier
Definition: mxfenc.c:414
static void mxf_write_multi_descriptor(AVFormatContext *s)
Definition: mxfenc.c:1012
static void mxf_compute_edit_unit_byte_count(AVFormatContext *s)
Definition: mxfenc.c:2860
static void mxf_write_wav_desc(AVFormatContext *s, AVStream *st)
Definition: mxfenc.c:1491
int channels
Audio only.
Definition: codec_par.h:166
static void mxf_write_local_tag(AVFormatContext *s, int size, int tag)
Definition: mxfenc.c:574
#define FF_MXF_MASTERING_CHROMA_DEN
Definition: mxf.h:94
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
Definition: packet.h:362
AVRational av_mul_q(AVRational b, AVRational c)
Multiply two rationals.
Definition: rational.c:80
void avio_wb32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:387
#define FF_PROFILE_PRORES_PROXY
Definition: avcodec.h:1957
FILE * out
Definition: movenc.c:54
Filter the word “frame” indicates either a video frame or a group of audio samples
static const MXFCodecUL * mxf_get_codec_ul_by_id(const MXFCodecUL *uls, int id)
Definition: mxfenc.c:593
#define av_freep(p)
static const AVOption opatom_options[]
Definition: mxfenc.c:3225
#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:399
uint32_t av_get_random_seed(void)
Get a seed to use in conjunction with random functions.
Definition: random_seed.c:120
MPEG-1 4:2:0, JPEG 4:2:0, H.263 4:2:0.
Definition: pixfmt.h:608
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1049
static const MXFLocalTagPair * mxf_lookup_local_tag(int tag)
Definition: mxfenc.c:499
UID uid
Definition: mxf.h:98
int stream_index
Definition: packet.h:365
static const uint8_t opatom_ul[]
Definition: mxfenc.c:238
#define DESCRIPTOR_COUNT(essence_container_count)
Definition: mxfenc.c:604
const MXFCodecUL ff_mxf_color_trc_uls[]
Definition: mxf.c:106
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avformat.h:913
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later.That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another.Frame references ownership and permissions
int depth
Number of bits in the component.
Definition: pixdesc.h:58
char * name
Definition: mxfdec.c:247
static int mxf_write_opatom_packet(AVFormatContext *s, AVPacket *pkt, MXFIndexEntry *ie)
Definition: mxfenc.c:2831
uint32_t flags
flags such as drop frame, +24 hours support, ...
Definition: timecode.h:43
exp golomb vlc stuff
AVRational sar
Definition: avc.h:48
static void mxf_write_mpegvideo_desc(AVFormatContext *s, AVStream *st)
Definition: mxfenc.c:1391
static void mxf_write_umid(AVFormatContext *s, int type)
Definition: mxfenc.c:432
This structure stores compressed data.
Definition: packet.h:340
static int mxf_parse_dv_frame(AVFormatContext *s, AVStream *st, AVPacket *pkt)
Definition: mxfenc.c:2142
int essence_container_count
Definition: mxfenc.c:395
static void store_version(AVFormatContext *s)
Definition: mxfenc.c:739
static int mxf_write_tagged_value(AVFormatContext *s, const char *name, const char *value)
Definition: mxfenc.c:1511
int avpriv_dnxhd_get_hr_frame_size(int cid, int w, int h)
Definition: dnxhddata.c:1094
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:356
static void mxf_write_generic_sound_desc(AVFormatContext *s, AVStream *st)
Definition: mxfenc.c:1503
enum MXFMetadataSetType type
Definition: mxfdec.c:240
static int mxf_interleave(AVFormatContext *s, AVPacket *out, AVPacket *pkt, int flush)
Definition: mxfenc.c:3163
int i
Definition: input.c:407
int ff_interleave_add_packet(AVFormatContext *s, AVPacket *pkt, int(*compare)(AVFormatContext *, const AVPacket *, const AVPacket *))
Add packet to an AVFormatContext&#39;s packet_buffer list, determining its interleaved position using com...
Definition: mux.c:829
AVTimecode tc
timecode context
Definition: mxfenc.c:407
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
static int mxf_write_footer(AVFormatContext *s)
Definition: mxfenc.c:3030
const char * name
Definition: opengl_enc.c:102
uint32_t av_timecode_get_smpte_from_framenum(const AVTimecode *tc, int framenum)
Convert frame number to SMPTE 12M binary representation.
Definition: timecode.c:52
AVOutputFormat ff_mxf_opatom_muxer
Definition: mxfenc.c:3274