FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/mxfenc.c
Date: 2025-08-19 23:55:23
Exec Total Coverage
Lines: 1494 1901 78.6%
Functions: 76 84 90.5%
Branches: 656 962 68.2%

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