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