Ticket #6759: mxfenc.c

File mxfenc.c, 111.3 KB (added by AxelTechnology, 9 years ago)

patched mxfenc.c

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