FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/mxfenc.c
Date: 2022-11-26 13:19:19
Exec Total Coverage
Lines: 1368 1661 82.4%
Branches: 600 826 72.6%

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