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