FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/mxfenc.c
Date: 2025-10-28 01:11:23
Exec Total Coverage
Lines: 1496 1897 78.9%
Functions: 75 82 91.5%
Branches: 661 957 69.1%

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