Commit | Line | Data |
---|---|---|
2ba45a60 DM |
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 | * References | |
25 | * SMPTE 336M KLV Data Encoding Protocol Using Key-Length-Value | |
26 | * SMPTE 377M MXF File Format Specifications | |
27 | * SMPTE 379M MXF Generic Container | |
28 | * SMPTE 381M Mapping MPEG Streams into the MXF Generic Container | |
29 | * SMPTE RP210: SMPTE Metadata Dictionary | |
30 | * SMPTE RP224: Registry of SMPTE Universal Labels | |
31 | */ | |
32 | ||
33 | #include <inttypes.h> | |
34 | #include <math.h> | |
35 | #include <time.h> | |
36 | ||
37 | #include "libavutil/opt.h" | |
38 | #include "libavutil/random_seed.h" | |
39 | #include "libavutil/timecode.h" | |
40 | #include "libavutil/avassert.h" | |
41 | #include "libavcodec/bytestream.h" | |
42 | #include "libavcodec/dnxhddata.h" | |
43 | #include "audiointerleave.h" | |
44 | #include "avformat.h" | |
45 | #include "avio_internal.h" | |
46 | #include "internal.h" | |
47 | #include "mxf.h" | |
48 | #include "config.h" | |
49 | ||
50 | extern AVOutputFormat ff_mxf_d10_muxer; | |
51 | ||
52 | #define EDIT_UNITS_PER_BODY 250 | |
53 | #define KAG_SIZE 512 | |
54 | ||
55 | typedef struct { | |
56 | int local_tag; | |
57 | UID uid; | |
58 | } MXFLocalTagPair; | |
59 | ||
60 | typedef struct { | |
61 | uint8_t flags; | |
62 | uint64_t offset; | |
63 | unsigned slice_offset; ///< offset of audio slice | |
64 | uint16_t temporal_ref; | |
65 | } MXFIndexEntry; | |
66 | ||
67 | typedef struct { | |
68 | AudioInterleaveContext aic; | |
69 | UID track_essence_element_key; | |
70 | int index; ///< index in mxf_essence_container_uls table | |
71 | const UID *codec_ul; | |
72 | int order; ///< interleaving order if dts are equal | |
73 | int interlaced; ///< whether picture is interlaced | |
74 | int field_dominance; ///< tff=1, bff=2 | |
75 | int component_depth; | |
76 | int temporal_reordering; | |
77 | AVRational aspect_ratio; ///< display aspect ratio | |
78 | int closed_gop; ///< gop is closed, used in mpeg-2 frame parsing | |
79 | int video_bit_rate; | |
80 | } MXFStreamContext; | |
81 | ||
82 | typedef struct { | |
83 | UID container_ul; | |
84 | UID element_ul; | |
85 | UID codec_ul; | |
86 | void (*write_desc)(AVFormatContext *, AVStream *); | |
87 | } MXFContainerEssenceEntry; | |
88 | ||
89 | static const struct { | |
90 | enum AVCodecID id; | |
91 | int index; | |
92 | } mxf_essence_mappings[] = { | |
93 | { AV_CODEC_ID_MPEG2VIDEO, 0 }, | |
94 | { AV_CODEC_ID_PCM_S24LE, 1 }, | |
95 | { AV_CODEC_ID_PCM_S16LE, 1 }, | |
96 | { AV_CODEC_ID_DVVIDEO, 15 }, | |
97 | { AV_CODEC_ID_DNXHD, 24 }, | |
98 | { AV_CODEC_ID_NONE } | |
99 | }; | |
100 | ||
101 | static void mxf_write_wav_desc(AVFormatContext *s, AVStream *st); | |
102 | static void mxf_write_aes3_desc(AVFormatContext *s, AVStream *st); | |
103 | static void mxf_write_mpegvideo_desc(AVFormatContext *s, AVStream *st); | |
104 | static void mxf_write_cdci_desc(AVFormatContext *s, AVStream *st); | |
105 | static void mxf_write_generic_sound_desc(AVFormatContext *s, AVStream *st); | |
106 | ||
107 | static const MXFContainerEssenceEntry mxf_essence_container_uls[] = { | |
108 | { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x02,0x0D,0x01,0x03,0x01,0x02,0x04,0x60,0x01 }, | |
109 | { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x15,0x01,0x05,0x00 }, | |
110 | { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x00,0x00,0x00 }, | |
111 | mxf_write_mpegvideo_desc }, | |
112 | { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x06,0x03,0x00 }, | |
113 | { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x16,0x01,0x03,0x00 }, | |
114 | { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 }, | |
115 | mxf_write_aes3_desc }, | |
116 | { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x06,0x01,0x00 }, | |
117 | { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x16,0x01,0x01,0x00 }, | |
118 | { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 }, | |
119 | mxf_write_wav_desc }, | |
120 | // D-10 625/50 PAL 50mb/s | |
121 | { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x01,0x01 }, | |
122 | { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x05,0x01,0x01,0x00 }, | |
123 | { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x01,0x02,0x01,0x01 }, | |
124 | mxf_write_cdci_desc }, | |
125 | { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x01,0x01 }, | |
126 | { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x06,0x01,0x10,0x00 }, | |
127 | { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 }, | |
128 | mxf_write_generic_sound_desc }, | |
129 | // D-10 525/60 NTSC 50mb/s | |
130 | { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x02,0x01 }, | |
131 | { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x05,0x01,0x01,0x00 }, | |
132 | { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x01,0x02,0x01,0x02 }, | |
133 | mxf_write_cdci_desc }, | |
134 | { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x02,0x01 }, | |
135 | { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x06,0x01,0x10,0x00 }, | |
136 | { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 }, | |
137 | mxf_write_generic_sound_desc }, | |
138 | // D-10 625/50 PAL 40mb/s | |
139 | { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x03,0x01 }, | |
140 | { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x05,0x01,0x01,0x00 }, | |
141 | { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x01,0x02,0x01,0x03 }, | |
142 | mxf_write_cdci_desc }, | |
143 | { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x03,0x01 }, | |
144 | { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x06,0x01,0x10,0x00 }, | |
145 | { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 }, | |
146 | mxf_write_generic_sound_desc }, | |
147 | // D-10 525/60 NTSC 40mb/s | |
148 | { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x04,0x01 }, | |
149 | { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x05,0x01,0x01,0x00 }, | |
150 | { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x01,0x02,0x01,0x04 }, | |
151 | mxf_write_cdci_desc }, | |
152 | { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x04,0x01 }, | |
153 | { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x06,0x01,0x10,0x00 }, | |
154 | { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 }, | |
155 | mxf_write_generic_sound_desc }, | |
156 | // D-10 625/50 PAL 30mb/s | |
157 | { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x05,0x01 }, | |
158 | { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x05,0x01,0x01,0x00 }, | |
159 | { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x01,0x02,0x01,0x05 }, | |
160 | mxf_write_cdci_desc }, | |
161 | { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x05,0x01 }, | |
162 | { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x06,0x01,0x10,0x00 }, | |
163 | { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 }, | |
164 | mxf_write_generic_sound_desc }, | |
165 | // D-10 525/60 NTSC 30mb/s | |
166 | { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x06,0x01 }, | |
167 | { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x05,0x01,0x01,0x00 }, | |
168 | { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x01,0x02,0x01,0x06 }, | |
169 | mxf_write_cdci_desc }, | |
170 | { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x06,0x01 }, | |
171 | { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x06,0x01,0x10,0x00 }, | |
172 | { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 }, | |
173 | mxf_write_generic_sound_desc }, | |
174 | // DV Unknown | |
175 | { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x02,0x7F,0x01 }, | |
176 | { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x18,0x01,0x01,0x00 }, | |
177 | { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x02,0x00,0x00,0x00 }, | |
178 | mxf_write_cdci_desc }, | |
179 | // DV25 525/60 | |
180 | { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x02,0x40,0x01 }, | |
181 | { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x18,0x01,0x01,0x00 }, | |
182 | { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x02,0x02,0x01,0x00 }, | |
183 | mxf_write_cdci_desc }, | |
184 | // DV25 625/50 | |
185 | { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x02,0x41,0x01 }, | |
186 | { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x18,0x01,0x01,0x00 }, | |
187 | { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x02,0x02,0x02,0x00 }, | |
188 | mxf_write_cdci_desc }, | |
189 | // DV50 525/60 | |
190 | { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x02,0x50,0x01 }, | |
191 | { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x18,0x01,0x01,0x00 }, | |
192 | { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x02,0x02,0x03,0x00 }, | |
193 | mxf_write_cdci_desc }, | |
194 | // DV50 625/50 | |
195 | { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x02,0x51,0x01 }, | |
196 | { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x18,0x01,0x01,0x00 }, | |
197 | { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x02,0x02,0x04,0x00 }, | |
198 | mxf_write_cdci_desc }, | |
199 | // DV100 1080/60 | |
200 | { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x02,0x60,0x01 }, | |
201 | { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x18,0x01,0x01,0x00 }, | |
202 | { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x02,0x02,0x05,0x00 }, | |
203 | mxf_write_cdci_desc }, | |
204 | // DV100 1080/50 | |
205 | { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x02,0x61,0x01 }, | |
206 | { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x18,0x01,0x01,0x00 }, | |
207 | { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x02,0x02,0x06,0x00 }, | |
208 | mxf_write_cdci_desc }, | |
209 | // DV100 720/60 | |
210 | { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x02,0x62,0x01 }, | |
211 | { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x18,0x01,0x01,0x00 }, | |
212 | { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x02,0x02,0x07,0x00 }, | |
213 | mxf_write_cdci_desc }, | |
214 | // DV100 720/50 | |
215 | { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x02,0x63,0x01 }, | |
216 | { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x18,0x01,0x01,0x00 }, | |
217 | { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x02,0x02,0x08,0x00 }, | |
218 | mxf_write_cdci_desc }, | |
219 | // DNxHD 1080p 10bit high | |
220 | { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x11,0x01,0x00 }, | |
221 | { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x15,0x01,0x05,0x00 }, | |
222 | { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0A,0x04,0x01,0x02,0x02,0x71,0x01,0x00,0x00 }, | |
223 | mxf_write_cdci_desc }, | |
224 | // DNxHD 1080p 8bit medium | |
225 | { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x11,0x01,0x00 }, | |
226 | { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x15,0x01,0x05,0x00 }, | |
227 | { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0A,0x04,0x01,0x02,0x02,0x71,0x03,0x00,0x00 }, | |
228 | mxf_write_cdci_desc }, | |
229 | // DNxHD 1080p 8bit high | |
230 | { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x11,0x01,0x00 }, | |
231 | { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x15,0x01,0x05,0x00 }, | |
232 | { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0A,0x04,0x01,0x02,0x02,0x71,0x04,0x00,0x00 }, | |
233 | mxf_write_cdci_desc }, | |
234 | // DNxHD 1080i 10bit high | |
235 | { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x11,0x01,0x00 }, | |
236 | { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x15,0x01,0x05,0x00 }, | |
237 | { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0A,0x04,0x01,0x02,0x02,0x71,0x07,0x00,0x00 }, | |
238 | mxf_write_cdci_desc }, | |
239 | // DNxHD 1080i 8bit medium | |
240 | { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x11,0x01,0x00 }, | |
241 | { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x15,0x01,0x05,0x00 }, | |
242 | { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0A,0x04,0x01,0x02,0x02,0x71,0x08,0x00,0x00 }, | |
243 | mxf_write_cdci_desc }, | |
244 | // DNxHD 1080i 8bit high | |
245 | { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x11,0x01,0x00 }, | |
246 | { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x15,0x01,0x05,0x00 }, | |
247 | { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0A,0x04,0x01,0x02,0x02,0x71,0x09,0x00,0x00 }, | |
248 | mxf_write_cdci_desc }, | |
249 | // DNxHD 720p 10bit | |
250 | { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x01,0x0d,0x01,0x03,0x01,0x02,0x11,0x01,0x00 }, | |
251 | { 0x06,0x0e,0x2b,0x34,0x01,0x02,0x01,0x01,0x0d,0x01,0x03,0x01,0x15,0x01,0x05,0x00 }, | |
252 | { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x0A,0x04,0x01,0x02,0x02,0x71,0x10,0x00,0x00 }, | |
253 | mxf_write_cdci_desc }, | |
254 | // DNxHD 720p 8bit high | |
255 | { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x01,0x0d,0x01,0x03,0x01,0x02,0x11,0x01,0x00 }, | |
256 | { 0x06,0x0e,0x2b,0x34,0x01,0x02,0x01,0x01,0x0d,0x01,0x03,0x01,0x15,0x01,0x05,0x00 }, | |
257 | { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x0A,0x04,0x01,0x02,0x02,0x71,0x11,0x00,0x00 }, | |
258 | mxf_write_cdci_desc }, | |
259 | // DNxHD 720p 8bit medium | |
260 | { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x01,0x0d,0x01,0x03,0x01,0x02,0x11,0x01,0x00 }, | |
261 | { 0x06,0x0e,0x2b,0x34,0x01,0x02,0x01,0x01,0x0d,0x01,0x03,0x01,0x15,0x01,0x05,0x00 }, | |
262 | { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x0A,0x04,0x01,0x02,0x02,0x71,0x12,0x00,0x00 }, | |
263 | mxf_write_cdci_desc }, | |
264 | // DNxHD 720p 8bit low | |
265 | { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x01,0x0d,0x01,0x03,0x01,0x02,0x11,0x01,0x00 }, | |
266 | { 0x06,0x0e,0x2b,0x34,0x01,0x02,0x01,0x01,0x0d,0x01,0x03,0x01,0x15,0x01,0x05,0x00 }, | |
267 | { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x0A,0x04,0x01,0x02,0x02,0x71,0x13,0x00,0x00 }, | |
268 | mxf_write_cdci_desc }, | |
269 | { { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }, | |
270 | { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }, | |
271 | { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }, | |
272 | NULL }, | |
273 | }; | |
274 | ||
275 | typedef struct MXFContext { | |
276 | AVClass *av_class; | |
277 | int64_t footer_partition_offset; | |
278 | int essence_container_count; | |
279 | AVRational time_base; | |
280 | int header_written; | |
281 | MXFIndexEntry *index_entries; | |
282 | unsigned edit_units_count; | |
283 | uint64_t timestamp; ///< timestamp, as year(16),month(8),day(8),hour(8),minutes(8),msec/4(8) | |
284 | uint8_t slice_count; ///< index slice count minus 1 (1 if no audio, 0 otherwise) | |
285 | int last_indexed_edit_unit; | |
286 | uint64_t *body_partition_offset; | |
287 | unsigned body_partitions_count; | |
288 | int last_key_index; ///< index of last key frame | |
289 | uint64_t duration; | |
290 | AVTimecode tc; ///< timecode context | |
291 | AVStream *timecode_track; | |
292 | int timecode_base; ///< rounded time code base (25 or 30) | |
293 | int edit_unit_byte_count; ///< fixed edit unit byte count | |
294 | uint64_t body_offset; | |
295 | uint32_t instance_number; | |
296 | uint8_t umid[16]; ///< unique material identifier | |
297 | int channel_count; | |
298 | } MXFContext; | |
299 | ||
300 | static const uint8_t uuid_base[] = { 0xAD,0xAB,0x44,0x24,0x2f,0x25,0x4d,0xc7,0x92,0xff,0x29,0xbd }; | |
301 | static const uint8_t umid_ul[] = { 0x06,0x0A,0x2B,0x34,0x01,0x01,0x01,0x05,0x01,0x01,0x0D,0x00,0x13 }; | |
302 | ||
303 | /** | |
304 | * complete key for operation pattern, partitions, and primer pack | |
305 | */ | |
306 | static const uint8_t op1a_ul[] = { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x02,0x01,0x01,0x01,0x09,0x00 }; | |
307 | static const uint8_t footer_partition_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0D,0x01,0x02,0x01,0x01,0x04,0x04,0x00 }; // ClosedComplete | |
308 | static const uint8_t primer_pack_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0D,0x01,0x02,0x01,0x01,0x05,0x01,0x00 }; | |
309 | static const uint8_t index_table_segment_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x10,0x01,0x00 }; | |
310 | static const uint8_t random_index_pack_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0D,0x01,0x02,0x01,0x01,0x11,0x01,0x00 }; | |
311 | static const uint8_t header_open_partition_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0D,0x01,0x02,0x01,0x01,0x02,0x01,0x00 }; // OpenIncomplete | |
312 | static const uint8_t header_closed_partition_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0D,0x01,0x02,0x01,0x01,0x02,0x04,0x00 }; // ClosedComplete | |
313 | static const uint8_t klv_fill_key[] = { 0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x03,0x01,0x02,0x10,0x01,0x00,0x00,0x00 }; | |
314 | static const uint8_t body_partition_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0D,0x01,0x02,0x01,0x01,0x03,0x04,0x00 }; // ClosedComplete | |
315 | ||
316 | /** | |
317 | * partial key for header metadata | |
318 | */ | |
319 | static const uint8_t header_metadata_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0D,0x01,0x01,0x01,0x01 }; | |
320 | static const uint8_t multiple_desc_ul[] = { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x0D,0x01,0x03,0x01,0x02,0x7F,0x01,0x00 }; | |
321 | ||
322 | /** | |
323 | * SMPTE RP210 http://www.smpte-ra.org/mdd/index.html | |
324 | */ | |
325 | static const MXFLocalTagPair mxf_local_tag_batch[] = { | |
326 | // preface set | |
327 | { 0x3C0A, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x01,0x01,0x15,0x02,0x00,0x00,0x00,0x00}}, /* Instance UID */ | |
328 | { 0x3B02, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x10,0x02,0x04,0x00,0x00}}, /* Last Modified Date */ | |
329 | { 0x3B05, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x03,0x01,0x02,0x01,0x05,0x00,0x00,0x00}}, /* Version */ | |
330 | { 0x3B06, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x06,0x04,0x00,0x00}}, /* Identifications reference */ | |
331 | { 0x3B03, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x02,0x01,0x00,0x00}}, /* Content Storage reference */ | |
332 | { 0x3B09, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x01,0x02,0x02,0x03,0x00,0x00,0x00,0x00}}, /* Operational Pattern UL */ | |
333 | { 0x3B0A, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x01,0x02,0x02,0x10,0x02,0x01,0x00,0x00}}, /* Essence Containers UL batch */ | |
334 | { 0x3B0B, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x01,0x02,0x02,0x10,0x02,0x02,0x00,0x00}}, /* DM Schemes UL batch */ | |
335 | // Identification | |
336 | { 0x3C09, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x20,0x07,0x01,0x01,0x00,0x00,0x00}}, /* This Generation UID */ | |
337 | { 0x3C01, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x20,0x07,0x01,0x02,0x01,0x00,0x00}}, /* Company Name */ | |
338 | { 0x3C02, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x20,0x07,0x01,0x03,0x01,0x00,0x00}}, /* Product Name */ | |
339 | { 0x3C04, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x20,0x07,0x01,0x05,0x01,0x00,0x00}}, /* Version String */ | |
340 | { 0x3C05, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x20,0x07,0x01,0x07,0x00,0x00,0x00}}, /* Product ID */ | |
341 | { 0x3C06, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x10,0x02,0x03,0x00,0x00}}, /* Modification Date */ | |
342 | // Content Storage | |
343 | { 0x1901, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x05,0x01,0x00,0x00}}, /* Package strong reference batch */ | |
344 | { 0x1902, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x05,0x02,0x00,0x00}}, /* Package strong reference batch */ | |
345 | // Essence Container Data | |
346 | { 0x2701, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x06,0x01,0x00,0x00,0x00}}, /* Linked Package UID */ | |
347 | { 0x3F07, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x01,0x03,0x04,0x04,0x00,0x00,0x00,0x00}}, /* BodySID */ | |
348 | // Package | |
349 | { 0x4401, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x01,0x01,0x15,0x10,0x00,0x00,0x00,0x00}}, /* Package UID */ | |
350 | { 0x4405, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x10,0x01,0x03,0x00,0x00}}, /* Package Creation Date */ | |
351 | { 0x4404, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x10,0x02,0x05,0x00,0x00}}, /* Package Modified Date */ | |
352 | { 0x4403, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x06,0x05,0x00,0x00}}, /* Tracks Strong reference array */ | |
353 | { 0x4701, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x02,0x03,0x00,0x00}}, /* Descriptor */ | |
354 | // Track | |
355 | { 0x4801, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x01,0x07,0x01,0x01,0x00,0x00,0x00,0x00}}, /* Track ID */ | |
356 | { 0x4804, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x01,0x04,0x01,0x03,0x00,0x00,0x00,0x00}}, /* Track Number */ | |
357 | { 0x4B01, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x30,0x04,0x05,0x00,0x00,0x00,0x00}}, /* Edit Rate */ | |
358 | { 0x4B02, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x03,0x01,0x03,0x00,0x00}}, /* Origin */ | |
359 | { 0x4803, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x02,0x04,0x00,0x00}}, /* Sequence reference */ | |
360 | // Sequence | |
361 | { 0x0201, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x07,0x01,0x00,0x00,0x00,0x00,0x00}}, /* Data Definition UL */ | |
362 | { 0x0202, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x02,0x01,0x01,0x03,0x00,0x00}}, /* Duration */ | |
363 | { 0x1001, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x06,0x09,0x00,0x00}}, /* Structural Components reference array */ | |
364 | // Source Clip | |
365 | { 0x1201, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x03,0x01,0x04,0x00,0x00}}, /* Start position */ | |
366 | { 0x1101, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x03,0x01,0x00,0x00,0x00}}, /* SourcePackageID */ | |
367 | { 0x1102, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x03,0x02,0x00,0x00,0x00}}, /* SourceTrackID */ | |
368 | // Timecode Component | |
369 | { 0x1501, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x03,0x01,0x05,0x00,0x00}}, /* Start Time Code */ | |
370 | { 0x1502, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x04,0x01,0x01,0x02,0x06,0x00,0x00}}, /* Rounded Time Code Base */ | |
371 | { 0x1503, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x04,0x01,0x01,0x05,0x00,0x00,0x00}}, /* Drop Frame */ | |
372 | // File Descriptor | |
373 | { 0x3F01, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x06,0x01,0x01,0x04,0x06,0x0B,0x00,0x00}}, /* Sub Descriptors reference array */ | |
374 | { 0x3006, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x06,0x01,0x01,0x03,0x05,0x00,0x00,0x00}}, /* Linked Track ID */ | |
375 | { 0x3001, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x06,0x01,0x01,0x00,0x00,0x00,0x00}}, /* SampleRate */ | |
376 | { 0x3004, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x01,0x02,0x00,0x00}}, /* Essence Container */ | |
377 | // Generic Picture Essence Descriptor | |
378 | { 0x320C, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x03,0x01,0x04,0x00,0x00,0x00}}, /* Frame Layout */ | |
379 | { 0x320D, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x01,0x03,0x02,0x05,0x00,0x00,0x00}}, /* Video Line Map */ | |
380 | { 0x3203, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x02,0x02,0x00,0x00,0x00}}, /* Stored Width */ | |
381 | { 0x3202, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x02,0x01,0x00,0x00,0x00}}, /* Stored Height */ | |
382 | { 0x3209, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x01,0x0C,0x00,0x00,0x00}}, /* Display Width */ | |
383 | { 0x3208, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x01,0x0B,0x00,0x00,0x00}}, /* Display Height */ | |
384 | { 0x320E, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x01,0x01,0x01,0x00,0x00,0x00}}, /* Aspect Ratio */ | |
385 | { 0x3201, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x01,0x06,0x01,0x00,0x00,0x00,0x00}}, /* Picture Essence Coding */ | |
386 | { 0x3212, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x01,0x03,0x01,0x06,0x00,0x00,0x00}}, /* Field Dominance (Opt) */ | |
387 | // CDCI Picture Essence Descriptor | |
388 | { 0x3301, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x01,0x05,0x03,0x0A,0x00,0x00,0x00}}, /* Component Depth */ | |
389 | { 0x3302, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x01,0x05,0x00,0x00,0x00}}, /* Horizontal Subsampling */ | |
390 | // Generic Sound Essence Descriptor | |
391 | { 0x3D02, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x04,0x02,0x03,0x01,0x04,0x00,0x00,0x00}}, /* Locked/Unlocked */ | |
392 | { 0x3D03, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x02,0x03,0x01,0x01,0x01,0x00,0x00}}, /* Audio sampling rate */ | |
393 | { 0x3D07, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x02,0x01,0x01,0x04,0x00,0x00,0x00}}, /* ChannelCount */ | |
394 | { 0x3D01, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x04,0x02,0x03,0x03,0x04,0x00,0x00,0x00}}, /* Quantization bits */ | |
395 | { 0x3D06, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x02,0x04,0x02,0x00,0x00,0x00,0x00}}, /* Sound Essence Compression */ | |
396 | // Index Table Segment | |
397 | { 0x3F0B, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x05,0x30,0x04,0x06,0x00,0x00,0x00,0x00}}, /* Index Edit Rate */ | |
398 | { 0x3F0C, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x07,0x02,0x01,0x03,0x01,0x0A,0x00,0x00}}, /* Index Start Position */ | |
399 | { 0x3F0D, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x07,0x02,0x02,0x01,0x01,0x02,0x00,0x00}}, /* Index Duration */ | |
400 | { 0x3F05, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x04,0x06,0x02,0x01,0x00,0x00,0x00,0x00}}, /* Edit Unit Byte Count */ | |
401 | { 0x3F06, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x01,0x03,0x04,0x05,0x00,0x00,0x00,0x00}}, /* IndexSID */ | |
402 | { 0x3F08, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x04,0x04,0x04,0x01,0x01,0x00,0x00,0x00}}, /* Slice Count */ | |
403 | { 0x3F09, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x04,0x04,0x01,0x06,0x00,0x00,0x00}}, /* Delta Entry Array */ | |
404 | { 0x3F0A, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x04,0x04,0x02,0x05,0x00,0x00,0x00}}, /* Index Entry Array */ | |
405 | // MPEG video Descriptor | |
406 | { 0x8000, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x01,0x06,0x02,0x01,0x0B,0x00,0x00}}, /* BitRate */ | |
407 | { 0x8007, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x01,0x06,0x02,0x01,0x0A,0x00,0x00}}, /* ProfileAndLevel */ | |
408 | // Wave Audio Essence Descriptor | |
409 | { 0x3D09, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x02,0x03,0x03,0x05,0x00,0x00,0x00}}, /* Average Bytes Per Second */ | |
410 | { 0x3D0A, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x02,0x03,0x02,0x01,0x00,0x00,0x00}}, /* Block Align */ | |
411 | }; | |
412 | ||
413 | static void mxf_write_uuid(AVIOContext *pb, enum MXFMetadataSetType type, int value) | |
414 | { | |
415 | avio_write(pb, uuid_base, 12); | |
416 | avio_wb16(pb, type); | |
417 | avio_wb16(pb, value); | |
418 | } | |
419 | ||
420 | static void mxf_write_umid(AVFormatContext *s, int type) | |
421 | { | |
422 | MXFContext *mxf = s->priv_data; | |
423 | avio_write(s->pb, umid_ul, 13); | |
424 | avio_wb24(s->pb, mxf->instance_number); | |
425 | avio_write(s->pb, mxf->umid, 15); | |
426 | avio_w8(s->pb, type); | |
427 | } | |
428 | ||
429 | static void mxf_write_refs_count(AVIOContext *pb, int ref_count) | |
430 | { | |
431 | avio_wb32(pb, ref_count); | |
432 | avio_wb32(pb, 16); | |
433 | } | |
434 | ||
435 | static int klv_ber_length(uint64_t len) | |
436 | { | |
437 | if (len < 128) | |
438 | return 1; | |
439 | else | |
440 | return (av_log2(len) >> 3) + 2; | |
441 | } | |
442 | ||
443 | static int klv_encode_ber_length(AVIOContext *pb, uint64_t len) | |
444 | { | |
445 | // Determine the best BER size | |
446 | int size; | |
447 | if (len < 128) { | |
448 | //short form | |
449 | avio_w8(pb, len); | |
450 | return 1; | |
451 | } | |
452 | ||
453 | size = (av_log2(len) >> 3) + 1; | |
454 | ||
455 | // long form | |
456 | avio_w8(pb, 0x80 + size); | |
457 | while(size) { | |
458 | size--; | |
459 | avio_w8(pb, len >> 8 * size & 0xff); | |
460 | } | |
461 | return 0; | |
462 | } | |
463 | ||
464 | static void klv_encode_ber4_length(AVIOContext *pb, int len) | |
465 | { | |
466 | avio_w8(pb, 0x80 + 3); | |
467 | avio_wb24(pb, len); | |
468 | } | |
469 | ||
470 | /* | |
471 | * Get essence container ul index | |
472 | */ | |
473 | static int mxf_get_essence_container_ul_index(enum AVCodecID id) | |
474 | { | |
475 | int i; | |
476 | for (i = 0; mxf_essence_mappings[i].id; i++) | |
477 | if (mxf_essence_mappings[i].id == id) | |
478 | return mxf_essence_mappings[i].index; | |
479 | return -1; | |
480 | } | |
481 | ||
482 | static void mxf_write_primer_pack(AVFormatContext *s) | |
483 | { | |
484 | AVIOContext *pb = s->pb; | |
485 | int local_tag_number, i = 0; | |
486 | ||
487 | local_tag_number = FF_ARRAY_ELEMS(mxf_local_tag_batch); | |
488 | ||
489 | avio_write(pb, primer_pack_key, 16); | |
490 | klv_encode_ber_length(pb, local_tag_number * 18 + 8); | |
491 | ||
492 | avio_wb32(pb, local_tag_number); // local_tag num | |
493 | avio_wb32(pb, 18); // item size, always 18 according to the specs | |
494 | ||
495 | for (i = 0; i < local_tag_number; i++) { | |
496 | avio_wb16(pb, mxf_local_tag_batch[i].local_tag); | |
497 | avio_write(pb, mxf_local_tag_batch[i].uid, 16); | |
498 | } | |
499 | } | |
500 | ||
501 | static void mxf_write_local_tag(AVIOContext *pb, int size, int tag) | |
502 | { | |
503 | avio_wb16(pb, tag); | |
504 | avio_wb16(pb, size); | |
505 | } | |
506 | ||
507 | static void mxf_write_metadata_key(AVIOContext *pb, unsigned int value) | |
508 | { | |
509 | avio_write(pb, header_metadata_key, 13); | |
510 | avio_wb24(pb, value); | |
511 | } | |
512 | ||
513 | static void mxf_free(AVFormatContext *s) | |
514 | { | |
515 | int i; | |
516 | ||
517 | for (i = 0; i < s->nb_streams; i++) { | |
518 | AVStream *st = s->streams[i]; | |
519 | av_freep(&st->priv_data); | |
520 | } | |
521 | } | |
522 | ||
523 | static const MXFCodecUL *mxf_get_data_definition_ul(int type) | |
524 | { | |
525 | const MXFCodecUL *uls = ff_mxf_data_definition_uls; | |
526 | while (uls->uid[0]) { | |
527 | if (type == uls->id) | |
528 | break; | |
529 | uls++; | |
530 | } | |
531 | return uls; | |
532 | } | |
533 | ||
534 | //one EC -> one descriptor. N ECs -> MultipleDescriptor + N descriptors | |
535 | #define DESCRIPTOR_COUNT(essence_container_count) \ | |
536 | (essence_container_count > 1 ? essence_container_count + 1 : essence_container_count) | |
537 | ||
538 | static void mxf_write_essence_container_refs(AVFormatContext *s) | |
539 | { | |
540 | MXFContext *c = s->priv_data; | |
541 | AVIOContext *pb = s->pb; | |
542 | int i; | |
543 | ||
544 | mxf_write_refs_count(pb, DESCRIPTOR_COUNT(c->essence_container_count)); | |
545 | av_log(s,AV_LOG_DEBUG, "essence container count:%d\n", c->essence_container_count); | |
546 | for (i = 0; i < c->essence_container_count; i++) { | |
547 | MXFStreamContext *sc = s->streams[i]->priv_data; | |
548 | avio_write(pb, mxf_essence_container_uls[sc->index].container_ul, 16); | |
549 | } | |
550 | ||
551 | if (c->essence_container_count > 1) | |
552 | avio_write(pb, multiple_desc_ul, 16); | |
553 | } | |
554 | ||
555 | static void mxf_write_preface(AVFormatContext *s) | |
556 | { | |
557 | MXFContext *mxf = s->priv_data; | |
558 | AVIOContext *pb = s->pb; | |
559 | ||
560 | mxf_write_metadata_key(pb, 0x012f00); | |
561 | PRINT_KEY(s, "preface key", pb->buf_ptr - 16); | |
562 | klv_encode_ber_length(pb, 130 + 16LL * DESCRIPTOR_COUNT(mxf->essence_container_count)); | |
563 | ||
564 | // write preface set uid | |
565 | mxf_write_local_tag(pb, 16, 0x3C0A); | |
566 | mxf_write_uuid(pb, Preface, 0); | |
567 | PRINT_KEY(s, "preface uid", pb->buf_ptr - 16); | |
568 | ||
569 | // last modified date | |
570 | mxf_write_local_tag(pb, 8, 0x3B02); | |
571 | avio_wb64(pb, mxf->timestamp); | |
572 | ||
573 | // write version | |
574 | mxf_write_local_tag(pb, 2, 0x3B05); | |
575 | avio_wb16(pb, 258); // v1.2 | |
576 | ||
577 | // write identification_refs | |
578 | mxf_write_local_tag(pb, 16 + 8, 0x3B06); | |
579 | mxf_write_refs_count(pb, 1); | |
580 | mxf_write_uuid(pb, Identification, 0); | |
581 | ||
582 | // write content_storage_refs | |
583 | mxf_write_local_tag(pb, 16, 0x3B03); | |
584 | mxf_write_uuid(pb, ContentStorage, 0); | |
585 | ||
586 | // operational pattern | |
587 | mxf_write_local_tag(pb, 16, 0x3B09); | |
588 | avio_write(pb, op1a_ul, 16); | |
589 | ||
590 | // write essence_container_refs | |
591 | mxf_write_local_tag(pb, 8 + 16LL * DESCRIPTOR_COUNT(mxf->essence_container_count), 0x3B0A); | |
592 | mxf_write_essence_container_refs(s); | |
593 | ||
594 | // write dm_scheme_refs | |
595 | mxf_write_local_tag(pb, 8, 0x3B0B); | |
596 | avio_wb64(pb, 0); | |
597 | } | |
598 | ||
599 | /* | |
600 | * Write a local tag containing an ascii string as utf-16 | |
601 | */ | |
602 | static void mxf_write_local_tag_utf16(AVIOContext *pb, int tag, const char *value) | |
603 | { | |
604 | int i, size = strlen(value); | |
605 | mxf_write_local_tag(pb, size*2, tag); | |
606 | for (i = 0; i < size; i++) | |
607 | avio_wb16(pb, value[i]); | |
608 | } | |
609 | ||
610 | static void mxf_write_identification(AVFormatContext *s) | |
611 | { | |
612 | MXFContext *mxf = s->priv_data; | |
613 | AVIOContext *pb = s->pb; | |
614 | const char *company = "FFmpeg"; | |
615 | const char *product = "OP1a Muxer"; | |
616 | const char *version; | |
617 | int length; | |
618 | ||
619 | mxf_write_metadata_key(pb, 0x013000); | |
620 | PRINT_KEY(s, "identification key", pb->buf_ptr - 16); | |
621 | ||
622 | version = s->flags & AVFMT_FLAG_BITEXACT ? | |
623 | "0.0.0" : AV_STRINGIFY(LIBAVFORMAT_VERSION); | |
624 | length = 84 + (strlen(company)+strlen(product)+strlen(version))*2; // utf-16 | |
625 | klv_encode_ber_length(pb, length); | |
626 | ||
627 | // write uid | |
628 | mxf_write_local_tag(pb, 16, 0x3C0A); | |
629 | mxf_write_uuid(pb, Identification, 0); | |
630 | PRINT_KEY(s, "identification uid", pb->buf_ptr - 16); | |
631 | ||
632 | // write generation uid | |
633 | mxf_write_local_tag(pb, 16, 0x3C09); | |
634 | mxf_write_uuid(pb, Identification, 1); | |
635 | ||
636 | mxf_write_local_tag_utf16(pb, 0x3C01, company); // Company Name | |
637 | mxf_write_local_tag_utf16(pb, 0x3C02, product); // Product Name | |
638 | mxf_write_local_tag_utf16(pb, 0x3C04, version); // Version String | |
639 | ||
640 | // write product uid | |
641 | mxf_write_local_tag(pb, 16, 0x3C05); | |
642 | mxf_write_uuid(pb, Identification, 2); | |
643 | ||
644 | // modification date | |
645 | mxf_write_local_tag(pb, 8, 0x3C06); | |
646 | avio_wb64(pb, mxf->timestamp); | |
647 | } | |
648 | ||
649 | static void mxf_write_content_storage(AVFormatContext *s) | |
650 | { | |
651 | AVIOContext *pb = s->pb; | |
652 | ||
653 | mxf_write_metadata_key(pb, 0x011800); | |
654 | PRINT_KEY(s, "content storage key", pb->buf_ptr - 16); | |
655 | klv_encode_ber_length(pb, 92); | |
656 | ||
657 | // write uid | |
658 | mxf_write_local_tag(pb, 16, 0x3C0A); | |
659 | mxf_write_uuid(pb, ContentStorage, 0); | |
660 | PRINT_KEY(s, "content storage uid", pb->buf_ptr - 16); | |
661 | ||
662 | // write package reference | |
663 | mxf_write_local_tag(pb, 16 * 2 + 8, 0x1901); | |
664 | mxf_write_refs_count(pb, 2); | |
665 | mxf_write_uuid(pb, MaterialPackage, 0); | |
666 | mxf_write_uuid(pb, SourcePackage, 0); | |
667 | ||
668 | // write essence container data | |
669 | mxf_write_local_tag(pb, 8 + 16, 0x1902); | |
670 | mxf_write_refs_count(pb, 1); | |
671 | mxf_write_uuid(pb, EssenceContainerData, 0); | |
672 | } | |
673 | ||
674 | static void mxf_write_track(AVFormatContext *s, AVStream *st, enum MXFMetadataSetType type) | |
675 | { | |
676 | MXFContext *mxf = s->priv_data; | |
677 | AVIOContext *pb = s->pb; | |
678 | MXFStreamContext *sc = st->priv_data; | |
679 | ||
680 | mxf_write_metadata_key(pb, 0x013b00); | |
681 | PRINT_KEY(s, "track key", pb->buf_ptr - 16); | |
682 | klv_encode_ber_length(pb, 80); | |
683 | ||
684 | // write track uid | |
685 | mxf_write_local_tag(pb, 16, 0x3C0A); | |
686 | mxf_write_uuid(pb, type == MaterialPackage ? Track : Track + TypeBottom, st->index); | |
687 | PRINT_KEY(s, "track uid", pb->buf_ptr - 16); | |
688 | ||
689 | // write track id | |
690 | mxf_write_local_tag(pb, 4, 0x4801); | |
691 | avio_wb32(pb, st->index+2); | |
692 | ||
693 | // write track number | |
694 | mxf_write_local_tag(pb, 4, 0x4804); | |
695 | if (type == MaterialPackage) | |
696 | avio_wb32(pb, 0); // track number of material package is 0 | |
697 | else | |
698 | avio_write(pb, sc->track_essence_element_key + 12, 4); | |
699 | ||
700 | mxf_write_local_tag(pb, 8, 0x4B01); | |
701 | avio_wb32(pb, mxf->time_base.den); | |
702 | avio_wb32(pb, mxf->time_base.num); | |
703 | ||
704 | // write origin | |
705 | mxf_write_local_tag(pb, 8, 0x4B02); | |
706 | avio_wb64(pb, 0); | |
707 | ||
708 | // write sequence refs | |
709 | mxf_write_local_tag(pb, 16, 0x4803); | |
710 | mxf_write_uuid(pb, type == MaterialPackage ? Sequence: Sequence + TypeBottom, st->index); | |
711 | } | |
712 | ||
713 | static const uint8_t smpte_12m_timecode_track_data_ul[] = { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x01,0x03,0x02,0x01,0x01,0x00,0x00,0x00 }; | |
714 | ||
715 | static void mxf_write_common_fields(AVFormatContext *s, AVStream *st) | |
716 | { | |
717 | MXFContext *mxf = s->priv_data; | |
718 | AVIOContext *pb = s->pb; | |
719 | ||
720 | // find data define uls | |
721 | mxf_write_local_tag(pb, 16, 0x0201); | |
722 | if (st == mxf->timecode_track) | |
723 | avio_write(pb, smpte_12m_timecode_track_data_ul, 16); | |
724 | else { | |
725 | const MXFCodecUL *data_def_ul = mxf_get_data_definition_ul(st->codec->codec_type); | |
726 | avio_write(pb, data_def_ul->uid, 16); | |
727 | } | |
728 | ||
729 | // write duration | |
730 | mxf_write_local_tag(pb, 8, 0x0202); | |
731 | avio_wb64(pb, mxf->duration); | |
732 | } | |
733 | ||
734 | static void mxf_write_sequence(AVFormatContext *s, AVStream *st, enum MXFMetadataSetType type) | |
735 | { | |
736 | MXFContext *mxf = s->priv_data; | |
737 | AVIOContext *pb = s->pb; | |
738 | enum MXFMetadataSetType component; | |
739 | ||
740 | mxf_write_metadata_key(pb, 0x010f00); | |
741 | PRINT_KEY(s, "sequence key", pb->buf_ptr - 16); | |
742 | klv_encode_ber_length(pb, 80); | |
743 | ||
744 | mxf_write_local_tag(pb, 16, 0x3C0A); | |
745 | mxf_write_uuid(pb, type == MaterialPackage ? Sequence: Sequence + TypeBottom, st->index); | |
746 | ||
747 | PRINT_KEY(s, "sequence uid", pb->buf_ptr - 16); | |
748 | mxf_write_common_fields(s, st); | |
749 | ||
750 | // write structural component | |
751 | mxf_write_local_tag(pb, 16 + 8, 0x1001); | |
752 | mxf_write_refs_count(pb, 1); | |
753 | if (st == mxf->timecode_track) | |
754 | component = TimecodeComponent; | |
755 | else | |
756 | component = SourceClip; | |
757 | if (type == SourcePackage) | |
758 | component += TypeBottom; | |
759 | mxf_write_uuid(pb, component, st->index); | |
760 | } | |
761 | ||
762 | static void mxf_write_timecode_component(AVFormatContext *s, AVStream *st, enum MXFMetadataSetType type) | |
763 | { | |
764 | MXFContext *mxf = s->priv_data; | |
765 | AVIOContext *pb = s->pb; | |
766 | ||
767 | mxf_write_metadata_key(pb, 0x011400); | |
768 | klv_encode_ber_length(pb, 75); | |
769 | ||
770 | // UID | |
771 | mxf_write_local_tag(pb, 16, 0x3C0A); | |
772 | mxf_write_uuid(pb, type == MaterialPackage ? TimecodeComponent : | |
773 | TimecodeComponent + TypeBottom, st->index); | |
774 | ||
775 | mxf_write_common_fields(s, st); | |
776 | ||
777 | // Start Time Code | |
778 | mxf_write_local_tag(pb, 8, 0x1501); | |
779 | avio_wb64(pb, mxf->tc.start); | |
780 | ||
781 | // Rounded Time Code Base | |
782 | mxf_write_local_tag(pb, 2, 0x1502); | |
783 | avio_wb16(pb, mxf->timecode_base); | |
784 | ||
785 | // Drop Frame | |
786 | mxf_write_local_tag(pb, 1, 0x1503); | |
787 | avio_w8(pb, !!(mxf->tc.flags & AV_TIMECODE_FLAG_DROPFRAME)); | |
788 | } | |
789 | ||
790 | static void mxf_write_structural_component(AVFormatContext *s, AVStream *st, enum MXFMetadataSetType type) | |
791 | { | |
792 | AVIOContext *pb = s->pb; | |
793 | int i; | |
794 | ||
795 | mxf_write_metadata_key(pb, 0x011100); | |
796 | PRINT_KEY(s, "sturctural component key", pb->buf_ptr - 16); | |
797 | klv_encode_ber_length(pb, 108); | |
798 | ||
799 | // write uid | |
800 | mxf_write_local_tag(pb, 16, 0x3C0A); | |
801 | mxf_write_uuid(pb, type == MaterialPackage ? SourceClip: SourceClip + TypeBottom, st->index); | |
802 | ||
803 | PRINT_KEY(s, "structural component uid", pb->buf_ptr - 16); | |
804 | mxf_write_common_fields(s, st); | |
805 | ||
806 | // write start_position | |
807 | mxf_write_local_tag(pb, 8, 0x1201); | |
808 | avio_wb64(pb, 0); | |
809 | ||
810 | // write source package uid, end of the reference | |
811 | mxf_write_local_tag(pb, 32, 0x1101); | |
812 | if (type == SourcePackage) { | |
813 | for (i = 0; i < 4; i++) | |
814 | avio_wb64(pb, 0); | |
815 | } else | |
816 | mxf_write_umid(s, 1); | |
817 | ||
818 | // write source track id | |
819 | mxf_write_local_tag(pb, 4, 0x1102); | |
820 | if (type == SourcePackage) | |
821 | avio_wb32(pb, 0); | |
822 | else | |
823 | avio_wb32(pb, st->index+2); | |
824 | } | |
825 | ||
826 | static void mxf_write_multi_descriptor(AVFormatContext *s) | |
827 | { | |
828 | MXFContext *mxf = s->priv_data; | |
829 | AVIOContext *pb = s->pb; | |
830 | const uint8_t *ul; | |
831 | int i; | |
832 | ||
833 | mxf_write_metadata_key(pb, 0x014400); | |
834 | PRINT_KEY(s, "multiple descriptor key", pb->buf_ptr - 16); | |
835 | klv_encode_ber_length(pb, 64 + 16LL * s->nb_streams); | |
836 | ||
837 | mxf_write_local_tag(pb, 16, 0x3C0A); | |
838 | mxf_write_uuid(pb, MultipleDescriptor, 0); | |
839 | PRINT_KEY(s, "multi_desc uid", pb->buf_ptr - 16); | |
840 | ||
841 | // write sample rate | |
842 | mxf_write_local_tag(pb, 8, 0x3001); | |
843 | avio_wb32(pb, mxf->time_base.den); | |
844 | avio_wb32(pb, mxf->time_base.num); | |
845 | ||
846 | // write essence container ul | |
847 | mxf_write_local_tag(pb, 16, 0x3004); | |
848 | if (mxf->essence_container_count > 1) | |
849 | ul = multiple_desc_ul; | |
850 | else { | |
851 | MXFStreamContext *sc = s->streams[0]->priv_data; | |
852 | ul = mxf_essence_container_uls[sc->index].container_ul; | |
853 | } | |
854 | avio_write(pb, ul, 16); | |
855 | ||
856 | // write sub descriptor refs | |
857 | mxf_write_local_tag(pb, s->nb_streams * 16 + 8, 0x3F01); | |
858 | mxf_write_refs_count(pb, s->nb_streams); | |
859 | for (i = 0; i < s->nb_streams; i++) | |
860 | mxf_write_uuid(pb, SubDescriptor, i); | |
861 | } | |
862 | ||
863 | static void mxf_write_generic_desc(AVFormatContext *s, AVStream *st, const UID key, unsigned size) | |
864 | { | |
865 | MXFContext *mxf = s->priv_data; | |
866 | MXFStreamContext *sc = st->priv_data; | |
867 | AVIOContext *pb = s->pb; | |
868 | ||
869 | avio_write(pb, key, 16); | |
870 | klv_encode_ber4_length(pb, size+20+8+12+20); | |
871 | ||
872 | mxf_write_local_tag(pb, 16, 0x3C0A); | |
873 | mxf_write_uuid(pb, SubDescriptor, st->index); | |
874 | ||
875 | mxf_write_local_tag(pb, 4, 0x3006); | |
876 | avio_wb32(pb, st->index+2); | |
877 | ||
878 | mxf_write_local_tag(pb, 8, 0x3001); | |
879 | avio_wb32(pb, mxf->time_base.den); | |
880 | avio_wb32(pb, mxf->time_base.num); | |
881 | ||
882 | mxf_write_local_tag(pb, 16, 0x3004); | |
883 | avio_write(pb, mxf_essence_container_uls[sc->index].container_ul, 16); | |
884 | } | |
885 | ||
886 | static const UID mxf_mpegvideo_descriptor_key = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x51,0x00 }; | |
887 | static const UID mxf_wav_descriptor_key = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x48,0x00 }; | |
888 | static const UID mxf_aes3_descriptor_key = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x47,0x00 }; | |
889 | static const UID mxf_cdci_descriptor_key = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0D,0x01,0x01,0x01,0x01,0x01,0x28,0x00 }; | |
890 | static const UID mxf_generic_sound_descriptor_key = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0D,0x01,0x01,0x01,0x01,0x01,0x42,0x00 }; | |
891 | ||
892 | static void mxf_write_cdci_common(AVFormatContext *s, AVStream *st, const UID key, unsigned size) | |
893 | { | |
894 | MXFStreamContext *sc = st->priv_data; | |
895 | AVIOContext *pb = s->pb; | |
896 | int stored_height = (st->codec->height+15)/16*16; | |
897 | int display_height; | |
898 | int f1, f2; | |
899 | unsigned desc_size = size+8+8+8+8+8+8+5+16+sc->interlaced*4+12+20; | |
900 | if (sc->interlaced && sc->field_dominance) | |
901 | desc_size += 5; | |
902 | ||
903 | mxf_write_generic_desc(s, st, key, desc_size); | |
904 | ||
905 | mxf_write_local_tag(pb, 4, 0x3203); | |
906 | avio_wb32(pb, st->codec->width); | |
907 | ||
908 | mxf_write_local_tag(pb, 4, 0x3202); | |
909 | avio_wb32(pb, stored_height>>sc->interlaced); | |
910 | ||
911 | mxf_write_local_tag(pb, 4, 0x3209); | |
912 | avio_wb32(pb, st->codec->width); | |
913 | ||
914 | if (st->codec->height == 608) // PAL + VBI | |
915 | display_height = 576; | |
916 | else if (st->codec->height == 512) // NTSC + VBI | |
917 | display_height = 486; | |
918 | else | |
919 | display_height = st->codec->height; | |
920 | ||
921 | mxf_write_local_tag(pb, 4, 0x3208); | |
922 | avio_wb32(pb, display_height>>sc->interlaced); | |
923 | ||
924 | // component depth | |
925 | mxf_write_local_tag(pb, 4, 0x3301); | |
926 | avio_wb32(pb, sc->component_depth); | |
927 | ||
928 | // horizontal subsampling | |
929 | mxf_write_local_tag(pb, 4, 0x3302); | |
930 | avio_wb32(pb, 2); | |
931 | ||
932 | // frame layout | |
933 | mxf_write_local_tag(pb, 1, 0x320C); | |
934 | avio_w8(pb, sc->interlaced); | |
935 | ||
936 | // video line map | |
937 | switch (st->codec->height) { | |
938 | case 576: f1 = 23; f2 = st->codec->codec_id == AV_CODEC_ID_DVVIDEO ? 335 : 336; break; | |
939 | case 608: f1 = 7; f2 = 320; break; | |
940 | case 480: f1 = 20; f2 = st->codec->codec_id == AV_CODEC_ID_DVVIDEO ? 285 : 283; break; | |
941 | case 512: f1 = 7; f2 = 270; break; | |
942 | case 720: f1 = 26; f2 = 0; break; // progressive | |
943 | case 1080: f1 = 21; f2 = 584; break; | |
944 | default: f1 = 0; f2 = 0; break; | |
945 | } | |
946 | ||
947 | if (!sc->interlaced) { | |
948 | f2 = 0; | |
949 | f1 *= 2; | |
950 | } | |
951 | ||
952 | mxf_write_local_tag(pb, 12+sc->interlaced*4, 0x320D); | |
953 | avio_wb32(pb, sc->interlaced ? 2 : 1); | |
954 | avio_wb32(pb, 4); | |
955 | avio_wb32(pb, f1); | |
956 | if (sc->interlaced) | |
957 | avio_wb32(pb, f2); | |
958 | ||
959 | mxf_write_local_tag(pb, 8, 0x320E); | |
960 | avio_wb32(pb, sc->aspect_ratio.num); | |
961 | avio_wb32(pb, sc->aspect_ratio.den); | |
962 | ||
963 | mxf_write_local_tag(pb, 16, 0x3201); | |
964 | avio_write(pb, *sc->codec_ul, 16); | |
965 | ||
966 | if (sc->interlaced && sc->field_dominance) { | |
967 | mxf_write_local_tag(pb, 1, 0x3212); | |
968 | avio_w8(pb, sc->field_dominance); | |
969 | } | |
970 | ||
971 | } | |
972 | ||
973 | static void mxf_write_cdci_desc(AVFormatContext *s, AVStream *st) | |
974 | { | |
975 | mxf_write_cdci_common(s, st, mxf_cdci_descriptor_key, 0); | |
976 | } | |
977 | ||
978 | static void mxf_write_mpegvideo_desc(AVFormatContext *s, AVStream *st) | |
979 | { | |
980 | AVIOContext *pb = s->pb; | |
981 | MXFStreamContext *sc = st->priv_data; | |
982 | int profile_and_level = (st->codec->profile<<4) | st->codec->level; | |
983 | ||
984 | mxf_write_cdci_common(s, st, mxf_mpegvideo_descriptor_key, 8+5); | |
985 | ||
986 | // bit rate | |
987 | mxf_write_local_tag(pb, 4, 0x8000); | |
988 | avio_wb32(pb, sc->video_bit_rate); | |
989 | ||
990 | // profile and level | |
991 | mxf_write_local_tag(pb, 1, 0x8007); | |
992 | if (!st->codec->profile) | |
993 | profile_and_level |= 0x80; // escape bit | |
994 | avio_w8(pb, profile_and_level); | |
995 | } | |
996 | ||
997 | static void mxf_write_generic_sound_common(AVFormatContext *s, AVStream *st, const UID key, unsigned size) | |
998 | { | |
999 | AVIOContext *pb = s->pb; | |
1000 | MXFContext *mxf = s->priv_data; | |
1001 | int show_warnings = !mxf->footer_partition_offset; | |
1002 | ||
1003 | mxf_write_generic_desc(s, st, key, size+5+12+8+8); | |
1004 | ||
1005 | // audio locked | |
1006 | mxf_write_local_tag(pb, 1, 0x3D02); | |
1007 | avio_w8(pb, 1); | |
1008 | ||
1009 | // write audio sampling rate | |
1010 | mxf_write_local_tag(pb, 8, 0x3D03); | |
1011 | avio_wb32(pb, st->codec->sample_rate); | |
1012 | avio_wb32(pb, 1); | |
1013 | ||
1014 | mxf_write_local_tag(pb, 4, 0x3D07); | |
1015 | if (mxf->channel_count == -1) { | |
1016 | if (show_warnings && (s->oformat == &ff_mxf_d10_muxer) && (st->codec->channels != 4) && (st->codec->channels != 8)) | |
1017 | 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"); | |
1018 | avio_wb32(pb, st->codec->channels); | |
1019 | } else if (s->oformat == &ff_mxf_d10_muxer) { | |
1020 | if (show_warnings && (mxf->channel_count < st->codec->channels)) | |
1021 | av_log(s, AV_LOG_WARNING, "d10_channelcount < actual number of audio channels : some channels will be discarded\n"); | |
1022 | if (show_warnings && (mxf->channel_count != 4) && (mxf->channel_count != 8)) | |
1023 | 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"); | |
1024 | avio_wb32(pb, mxf->channel_count); | |
1025 | } else { | |
1026 | if (show_warnings && mxf->channel_count != -1) | |
1027 | av_log(s, AV_LOG_ERROR, "-d10_channelcount requires MXF D-10 and will be ignored\n"); | |
1028 | avio_wb32(pb, st->codec->channels); | |
1029 | } | |
1030 | ||
1031 | mxf_write_local_tag(pb, 4, 0x3D01); | |
1032 | avio_wb32(pb, av_get_bits_per_sample(st->codec->codec_id)); | |
1033 | } | |
1034 | ||
1035 | static void mxf_write_wav_common(AVFormatContext *s, AVStream *st, const UID key, unsigned size) | |
1036 | { | |
1037 | AVIOContext *pb = s->pb; | |
1038 | ||
1039 | mxf_write_generic_sound_common(s, st, key, size+6+8); | |
1040 | ||
1041 | mxf_write_local_tag(pb, 2, 0x3D0A); | |
1042 | avio_wb16(pb, st->codec->block_align); | |
1043 | ||
1044 | // avg bytes per sec | |
1045 | mxf_write_local_tag(pb, 4, 0x3D09); | |
1046 | avio_wb32(pb, st->codec->block_align*st->codec->sample_rate); | |
1047 | } | |
1048 | ||
1049 | static void mxf_write_wav_desc(AVFormatContext *s, AVStream *st) | |
1050 | { | |
1051 | mxf_write_wav_common(s, st, mxf_wav_descriptor_key, 0); | |
1052 | } | |
1053 | ||
1054 | static void mxf_write_aes3_desc(AVFormatContext *s, AVStream *st) | |
1055 | { | |
1056 | mxf_write_wav_common(s, st, mxf_aes3_descriptor_key, 0); | |
1057 | } | |
1058 | ||
1059 | static void mxf_write_generic_sound_desc(AVFormatContext *s, AVStream *st) | |
1060 | { | |
1061 | mxf_write_generic_sound_common(s, st, mxf_generic_sound_descriptor_key, 0); | |
1062 | } | |
1063 | ||
1064 | static void mxf_write_package(AVFormatContext *s, enum MXFMetadataSetType type) | |
1065 | { | |
1066 | MXFContext *mxf = s->priv_data; | |
1067 | AVIOContext *pb = s->pb; | |
1068 | int i, track_count = s->nb_streams+1; | |
1069 | ||
1070 | if (type == MaterialPackage) { | |
1071 | mxf_write_metadata_key(pb, 0x013600); | |
1072 | PRINT_KEY(s, "Material Package key", pb->buf_ptr - 16); | |
1073 | klv_encode_ber_length(pb, 92 + 16*track_count); | |
1074 | } else { | |
1075 | mxf_write_metadata_key(pb, 0x013700); | |
1076 | PRINT_KEY(s, "Source Package key", pb->buf_ptr - 16); | |
1077 | klv_encode_ber_length(pb, 112 + 16*track_count); // 20 bytes length for descriptor reference | |
1078 | } | |
1079 | ||
1080 | // write uid | |
1081 | mxf_write_local_tag(pb, 16, 0x3C0A); | |
1082 | mxf_write_uuid(pb, type, 0); | |
1083 | av_log(s,AV_LOG_DEBUG, "package type:%d\n", type); | |
1084 | PRINT_KEY(s, "package uid", pb->buf_ptr - 16); | |
1085 | ||
1086 | // write package umid | |
1087 | mxf_write_local_tag(pb, 32, 0x4401); | |
1088 | mxf_write_umid(s, type == SourcePackage); | |
1089 | PRINT_KEY(s, "package umid second part", pb->buf_ptr - 16); | |
1090 | ||
1091 | // package creation date | |
1092 | mxf_write_local_tag(pb, 8, 0x4405); | |
1093 | avio_wb64(pb, mxf->timestamp); | |
1094 | ||
1095 | // package modified date | |
1096 | mxf_write_local_tag(pb, 8, 0x4404); | |
1097 | avio_wb64(pb, mxf->timestamp); | |
1098 | ||
1099 | // write track refs | |
1100 | mxf_write_local_tag(pb, track_count*16 + 8, 0x4403); | |
1101 | mxf_write_refs_count(pb, track_count); | |
1102 | mxf_write_uuid(pb, type == MaterialPackage ? Track : | |
1103 | Track + TypeBottom, -1); // timecode track | |
1104 | for (i = 0; i < s->nb_streams; i++) | |
1105 | mxf_write_uuid(pb, type == MaterialPackage ? Track : Track + TypeBottom, i); | |
1106 | ||
1107 | // write multiple descriptor reference | |
1108 | if (type == SourcePackage) { | |
1109 | mxf_write_local_tag(pb, 16, 0x4701); | |
1110 | if (s->nb_streams > 1) { | |
1111 | mxf_write_uuid(pb, MultipleDescriptor, 0); | |
1112 | mxf_write_multi_descriptor(s); | |
1113 | } else | |
1114 | mxf_write_uuid(pb, SubDescriptor, 0); | |
1115 | } | |
1116 | ||
1117 | // write timecode track | |
1118 | mxf_write_track(s, mxf->timecode_track, type); | |
1119 | mxf_write_sequence(s, mxf->timecode_track, type); | |
1120 | mxf_write_timecode_component(s, mxf->timecode_track, type); | |
1121 | ||
1122 | for (i = 0; i < s->nb_streams; i++) { | |
1123 | AVStream *st = s->streams[i]; | |
1124 | mxf_write_track(s, st, type); | |
1125 | mxf_write_sequence(s, st, type); | |
1126 | mxf_write_structural_component(s, st, type); | |
1127 | ||
1128 | if (type == SourcePackage) { | |
1129 | MXFStreamContext *sc = st->priv_data; | |
1130 | mxf_essence_container_uls[sc->index].write_desc(s, st); | |
1131 | } | |
1132 | } | |
1133 | } | |
1134 | ||
1135 | static int mxf_write_essence_container_data(AVFormatContext *s) | |
1136 | { | |
1137 | AVIOContext *pb = s->pb; | |
1138 | ||
1139 | mxf_write_metadata_key(pb, 0x012300); | |
1140 | klv_encode_ber_length(pb, 72); | |
1141 | ||
1142 | mxf_write_local_tag(pb, 16, 0x3C0A); // Instance UID | |
1143 | mxf_write_uuid(pb, EssenceContainerData, 0); | |
1144 | ||
1145 | mxf_write_local_tag(pb, 32, 0x2701); // Linked Package UID | |
1146 | mxf_write_umid(s, 1); | |
1147 | ||
1148 | mxf_write_local_tag(pb, 4, 0x3F07); // BodySID | |
1149 | avio_wb32(pb, 1); | |
1150 | ||
1151 | mxf_write_local_tag(pb, 4, 0x3F06); // IndexSID | |
1152 | avio_wb32(pb, 2); | |
1153 | ||
1154 | return 0; | |
1155 | } | |
1156 | ||
1157 | static int mxf_write_header_metadata_sets(AVFormatContext *s) | |
1158 | { | |
1159 | mxf_write_preface(s); | |
1160 | mxf_write_identification(s); | |
1161 | mxf_write_content_storage(s); | |
1162 | mxf_write_package(s, MaterialPackage); | |
1163 | mxf_write_package(s, SourcePackage); | |
1164 | mxf_write_essence_container_data(s); | |
1165 | return 0; | |
1166 | } | |
1167 | ||
1168 | static unsigned klv_fill_size(uint64_t size) | |
1169 | { | |
1170 | unsigned pad = KAG_SIZE - (size & (KAG_SIZE-1)); | |
1171 | if (pad < 20) // smallest fill item possible | |
1172 | return pad + KAG_SIZE; | |
1173 | else | |
1174 | return pad & (KAG_SIZE-1); | |
1175 | } | |
1176 | ||
1177 | static void mxf_write_index_table_segment(AVFormatContext *s) | |
1178 | { | |
1179 | MXFContext *mxf = s->priv_data; | |
1180 | AVIOContext *pb = s->pb; | |
1181 | int i, j, temporal_reordering = 0; | |
1182 | int key_index = mxf->last_key_index; | |
1183 | ||
1184 | av_log(s, AV_LOG_DEBUG, "edit units count %d\n", mxf->edit_units_count); | |
1185 | ||
1186 | if (!mxf->edit_units_count && !mxf->edit_unit_byte_count) | |
1187 | return; | |
1188 | ||
1189 | avio_write(pb, index_table_segment_key, 16); | |
1190 | ||
1191 | if (mxf->edit_unit_byte_count) { | |
1192 | klv_encode_ber_length(pb, 80); | |
1193 | } else { | |
1194 | klv_encode_ber_length(pb, 85 + 12+(s->nb_streams+1LL)*6 + | |
1195 | 12+mxf->edit_units_count*(11+mxf->slice_count*4LL)); | |
1196 | } | |
1197 | ||
1198 | // instance id | |
1199 | mxf_write_local_tag(pb, 16, 0x3C0A); | |
1200 | mxf_write_uuid(pb, IndexTableSegment, 0); | |
1201 | ||
1202 | // index edit rate | |
1203 | mxf_write_local_tag(pb, 8, 0x3F0B); | |
1204 | avio_wb32(pb, mxf->time_base.den); | |
1205 | avio_wb32(pb, mxf->time_base.num); | |
1206 | ||
1207 | // index start position | |
1208 | mxf_write_local_tag(pb, 8, 0x3F0C); | |
1209 | avio_wb64(pb, mxf->last_indexed_edit_unit); | |
1210 | ||
1211 | // index duration | |
1212 | mxf_write_local_tag(pb, 8, 0x3F0D); | |
1213 | if (mxf->edit_unit_byte_count) | |
1214 | avio_wb64(pb, 0); // index table covers whole container | |
1215 | else | |
1216 | avio_wb64(pb, mxf->edit_units_count); | |
1217 | ||
1218 | // edit unit byte count | |
1219 | mxf_write_local_tag(pb, 4, 0x3F05); | |
1220 | avio_wb32(pb, mxf->edit_unit_byte_count); | |
1221 | ||
1222 | // index sid | |
1223 | mxf_write_local_tag(pb, 4, 0x3F06); | |
1224 | avio_wb32(pb, 2); | |
1225 | ||
1226 | // body sid | |
1227 | mxf_write_local_tag(pb, 4, 0x3F07); | |
1228 | avio_wb32(pb, 1); | |
1229 | ||
1230 | if (!mxf->edit_unit_byte_count) { | |
1231 | // real slice count - 1 | |
1232 | mxf_write_local_tag(pb, 1, 0x3F08); | |
1233 | avio_w8(pb, mxf->slice_count); | |
1234 | ||
1235 | // delta entry array | |
1236 | mxf_write_local_tag(pb, 8 + (s->nb_streams+1)*6, 0x3F09); | |
1237 | avio_wb32(pb, s->nb_streams+1); // num of entries | |
1238 | avio_wb32(pb, 6); // size of one entry | |
1239 | // write system item delta entry | |
1240 | avio_w8(pb, 0); | |
1241 | avio_w8(pb, 0); // slice entry | |
1242 | avio_wb32(pb, 0); // element delta | |
1243 | for (i = 0; i < s->nb_streams; i++) { | |
1244 | AVStream *st = s->streams[i]; | |
1245 | MXFStreamContext *sc = st->priv_data; | |
1246 | avio_w8(pb, sc->temporal_reordering); | |
1247 | if (sc->temporal_reordering) | |
1248 | temporal_reordering = 1; | |
1249 | if (i == 0) { // video track | |
1250 | avio_w8(pb, 0); // slice number | |
1251 | avio_wb32(pb, KAG_SIZE); // system item size including klv fill | |
1252 | } else { // audio track | |
1253 | unsigned audio_frame_size = sc->aic.samples[0]*sc->aic.sample_size; | |
1254 | audio_frame_size += klv_fill_size(audio_frame_size); | |
1255 | avio_w8(pb, 1); | |
1256 | avio_wb32(pb, (i-1)*audio_frame_size); // element delta | |
1257 | } | |
1258 | } | |
1259 | ||
1260 | mxf_write_local_tag(pb, 8 + mxf->edit_units_count*(11+mxf->slice_count*4), 0x3F0A); | |
1261 | avio_wb32(pb, mxf->edit_units_count); // num of entries | |
1262 | avio_wb32(pb, 11+mxf->slice_count*4); // size of one entry | |
1263 | ||
1264 | for (i = 0; i < mxf->edit_units_count; i++) { | |
1265 | int temporal_offset = 0; | |
1266 | ||
1267 | if (!(mxf->index_entries[i].flags & 0x33)) { // I frame | |
1268 | mxf->last_key_index = key_index; | |
1269 | key_index = i; | |
1270 | } | |
1271 | ||
1272 | if (temporal_reordering) { | |
1273 | int pic_num_in_gop = i - key_index; | |
1274 | if (pic_num_in_gop != mxf->index_entries[i].temporal_ref) { | |
1275 | for (j = key_index; j < mxf->edit_units_count; j++) { | |
1276 | if (pic_num_in_gop == mxf->index_entries[j].temporal_ref) | |
1277 | break; | |
1278 | } | |
1279 | if (j == mxf->edit_units_count) | |
1280 | av_log(s, AV_LOG_WARNING, "missing frames\n"); | |
1281 | temporal_offset = j - key_index - pic_num_in_gop; | |
1282 | } | |
1283 | } | |
1284 | avio_w8(pb, temporal_offset); | |
1285 | ||
1286 | if ((mxf->index_entries[i].flags & 0x30) == 0x30) { // back and forward prediction | |
1287 | avio_w8(pb, mxf->last_key_index - i); | |
1288 | } else { | |
1289 | avio_w8(pb, key_index - i); // key frame offset | |
1290 | if ((mxf->index_entries[i].flags & 0x20) == 0x20) // only forward | |
1291 | mxf->last_key_index = key_index; | |
1292 | } | |
1293 | ||
1294 | if (!(mxf->index_entries[i].flags & 0x33) && // I frame | |
1295 | mxf->index_entries[i].flags & 0x40 && !temporal_offset) | |
1296 | mxf->index_entries[i].flags |= 0x80; // random access | |
1297 | avio_w8(pb, mxf->index_entries[i].flags); | |
1298 | // stream offset | |
1299 | avio_wb64(pb, mxf->index_entries[i].offset); | |
1300 | if (s->nb_streams > 1) | |
1301 | avio_wb32(pb, mxf->index_entries[i].slice_offset); | |
1302 | } | |
1303 | ||
1304 | mxf->last_key_index = key_index - mxf->edit_units_count; | |
1305 | mxf->last_indexed_edit_unit += mxf->edit_units_count; | |
1306 | mxf->edit_units_count = 0; | |
1307 | } | |
1308 | } | |
1309 | ||
1310 | static void mxf_write_klv_fill(AVFormatContext *s) | |
1311 | { | |
1312 | unsigned pad = klv_fill_size(avio_tell(s->pb)); | |
1313 | if (pad) { | |
1314 | avio_write(s->pb, klv_fill_key, 16); | |
1315 | pad -= 16 + 4; | |
1316 | klv_encode_ber4_length(s->pb, pad); | |
1317 | ffio_fill(s->pb, 0, pad); | |
1318 | av_assert1(!(avio_tell(s->pb) & (KAG_SIZE-1))); | |
1319 | } | |
1320 | } | |
1321 | ||
1322 | static int mxf_write_partition(AVFormatContext *s, int bodysid, | |
1323 | int indexsid, | |
1324 | const uint8_t *key, int write_metadata) | |
1325 | { | |
1326 | MXFContext *mxf = s->priv_data; | |
1327 | AVIOContext *pb = s->pb; | |
1328 | int64_t header_byte_count_offset; | |
1329 | unsigned index_byte_count = 0; | |
1330 | uint64_t partition_offset = avio_tell(pb); | |
1331 | int err; | |
1332 | ||
1333 | if (!mxf->edit_unit_byte_count && mxf->edit_units_count) | |
1334 | index_byte_count = 85 + 12+(s->nb_streams+1)*6 + | |
1335 | 12+mxf->edit_units_count*(11+mxf->slice_count*4); | |
1336 | else if (mxf->edit_unit_byte_count && indexsid) | |
1337 | index_byte_count = 80; | |
1338 | ||
1339 | if (index_byte_count) { | |
1340 | // add encoded ber length | |
1341 | index_byte_count += 16 + klv_ber_length(index_byte_count); | |
1342 | index_byte_count += klv_fill_size(index_byte_count); | |
1343 | } | |
1344 | ||
1345 | if (!memcmp(key, body_partition_key, 16)) { | |
1346 | if ((err = av_reallocp_array(&mxf->body_partition_offset, mxf->body_partitions_count + 1, | |
1347 | sizeof(*mxf->body_partition_offset))) < 0) { | |
1348 | mxf->body_partitions_count = 0; | |
1349 | return err; | |
1350 | } | |
1351 | mxf->body_partition_offset[mxf->body_partitions_count++] = partition_offset; | |
1352 | } | |
1353 | ||
1354 | // write klv | |
1355 | avio_write(pb, key, 16); | |
1356 | klv_encode_ber_length(pb, 88 + 16LL * DESCRIPTOR_COUNT(mxf->essence_container_count)); | |
1357 | ||
1358 | // write partition value | |
1359 | avio_wb16(pb, 1); // majorVersion | |
1360 | avio_wb16(pb, 2); // minorVersion | |
1361 | avio_wb32(pb, KAG_SIZE); // KAGSize | |
1362 | ||
1363 | avio_wb64(pb, partition_offset); // ThisPartition | |
1364 | ||
1365 | if (!memcmp(key, body_partition_key, 16) && mxf->body_partitions_count > 1) | |
1366 | avio_wb64(pb, mxf->body_partition_offset[mxf->body_partitions_count-2]); // PreviousPartition | |
1367 | else if (!memcmp(key, footer_partition_key, 16) && mxf->body_partitions_count) | |
1368 | avio_wb64(pb, mxf->body_partition_offset[mxf->body_partitions_count-1]); // PreviousPartition | |
1369 | else | |
1370 | avio_wb64(pb, 0); | |
1371 | ||
1372 | avio_wb64(pb, mxf->footer_partition_offset); // footerPartition | |
1373 | ||
1374 | // set offset | |
1375 | header_byte_count_offset = avio_tell(pb); | |
1376 | avio_wb64(pb, 0); // headerByteCount, update later | |
1377 | ||
1378 | // indexTable | |
1379 | avio_wb64(pb, index_byte_count); // indexByteCount | |
1380 | avio_wb32(pb, index_byte_count ? indexsid : 0); // indexSID | |
1381 | ||
1382 | // BodyOffset | |
1383 | if (bodysid && mxf->edit_units_count && mxf->body_partitions_count) { | |
1384 | avio_wb64(pb, mxf->body_offset); | |
1385 | } else | |
1386 | avio_wb64(pb, 0); | |
1387 | ||
1388 | avio_wb32(pb, bodysid); // bodySID | |
1389 | ||
1390 | // operational pattern | |
1391 | avio_write(pb, op1a_ul, 16); | |
1392 | ||
1393 | // essence container | |
1394 | mxf_write_essence_container_refs(s); | |
1395 | ||
1396 | if (write_metadata) { | |
1397 | // mark the start of the headermetadata and calculate metadata size | |
1398 | int64_t pos, start; | |
1399 | unsigned header_byte_count; | |
1400 | ||
1401 | mxf_write_klv_fill(s); | |
1402 | start = avio_tell(s->pb); | |
1403 | mxf_write_primer_pack(s); | |
1404 | mxf_write_header_metadata_sets(s); | |
1405 | pos = avio_tell(s->pb); | |
1406 | header_byte_count = pos - start + klv_fill_size(pos); | |
1407 | ||
1408 | // update header_byte_count | |
1409 | avio_seek(pb, header_byte_count_offset, SEEK_SET); | |
1410 | avio_wb64(pb, header_byte_count); | |
1411 | avio_seek(pb, pos, SEEK_SET); | |
1412 | } | |
1413 | ||
1414 | avio_flush(pb); | |
1415 | ||
1416 | return 0; | |
1417 | } | |
1418 | ||
1419 | static int mxf_parse_dnxhd_frame(AVFormatContext *s, AVStream *st, | |
1420 | AVPacket *pkt) | |
1421 | { | |
1422 | MXFContext *mxf = s->priv_data; | |
1423 | MXFStreamContext *sc = st->priv_data; | |
1424 | int i, cid; | |
1425 | uint8_t* header_cid; | |
1426 | int frame_size = 0; | |
1427 | ||
1428 | if (mxf->header_written) | |
1429 | return 1; | |
1430 | ||
1431 | if (pkt->size < 43) | |
1432 | return -1; | |
1433 | ||
1434 | header_cid = pkt->data + 0x28; | |
1435 | cid = header_cid[0] << 24 | header_cid[1] << 16 | header_cid[2] << 8 | header_cid[3]; | |
1436 | ||
1437 | if ((frame_size = avpriv_dnxhd_get_frame_size(cid)) < 0) | |
1438 | return -1; | |
1439 | ||
1440 | switch (cid) { | |
1441 | case 1235: | |
1442 | sc->index = 24; | |
1443 | sc->component_depth = 10; | |
1444 | break; | |
1445 | case 1237: | |
1446 | sc->index = 25; | |
1447 | break; | |
1448 | case 1238: | |
1449 | sc->index = 26; | |
1450 | break; | |
1451 | case 1241: | |
1452 | sc->index = 27; | |
1453 | sc->component_depth = 10; | |
1454 | break; | |
1455 | case 1242: | |
1456 | sc->index = 28; | |
1457 | break; | |
1458 | case 1243: | |
1459 | sc->index = 29; | |
1460 | break; | |
1461 | case 1250: | |
1462 | sc->index = 30; | |
1463 | sc->component_depth = 10; | |
1464 | break; | |
1465 | case 1251: | |
1466 | sc->index = 31; | |
1467 | break; | |
1468 | case 1252: | |
1469 | sc->index = 32; | |
1470 | break; | |
1471 | case 1253: | |
1472 | sc->index = 33; | |
1473 | break; | |
1474 | default: | |
1475 | return -1; | |
1476 | } | |
1477 | ||
1478 | sc->codec_ul = &mxf_essence_container_uls[sc->index].codec_ul; | |
1479 | sc->aspect_ratio = (AVRational){ 16, 9 }; | |
1480 | ||
1481 | mxf->edit_unit_byte_count = KAG_SIZE; | |
1482 | for (i = 0; i < s->nb_streams; i++) { | |
1483 | AVStream *st = s->streams[i]; | |
1484 | MXFStreamContext *sc = st->priv_data; | |
1485 | if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) { | |
1486 | mxf->edit_unit_byte_count += 16 + 4 + sc->aic.samples[0]*sc->aic.sample_size; | |
1487 | mxf->edit_unit_byte_count += klv_fill_size(mxf->edit_unit_byte_count); | |
1488 | } else if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) { | |
1489 | mxf->edit_unit_byte_count += 16 + 4 + frame_size; | |
1490 | mxf->edit_unit_byte_count += klv_fill_size(mxf->edit_unit_byte_count); | |
1491 | } | |
1492 | } | |
1493 | ||
1494 | return 1; | |
1495 | } | |
1496 | ||
1497 | static int mxf_parse_dv_frame(AVFormatContext *s, AVStream *st, AVPacket *pkt) | |
1498 | { | |
1499 | MXFContext *mxf = s->priv_data; | |
1500 | MXFStreamContext *sc = st->priv_data; | |
1501 | uint8_t *vs_pack, *vsc_pack; | |
1502 | int i, ul_index, frame_size, stype, pal; | |
1503 | ||
1504 | if (mxf->header_written) | |
1505 | return 1; | |
1506 | ||
1507 | // Check for minimal frame size | |
1508 | if (pkt->size < 120000) | |
1509 | return -1; | |
1510 | ||
1511 | vs_pack = pkt->data + 80*5 + 48; | |
1512 | vsc_pack = pkt->data + 80*5 + 53; | |
1513 | stype = vs_pack[3] & 0x1f; | |
1514 | pal = (vs_pack[3] >> 5) & 0x1; | |
1515 | ||
1516 | if ((vs_pack[2] & 0x07) == 0x02) | |
1517 | sc->aspect_ratio = (AVRational){ 16, 9 }; | |
1518 | else | |
1519 | sc->aspect_ratio = (AVRational){ 4, 3 }; | |
1520 | ||
1521 | sc->interlaced = (vsc_pack[3] >> 4) & 0x01; | |
1522 | // TODO: fix dv encoder to set proper FF/FS value in VSC pack | |
1523 | // and set field dominance accordingly | |
1524 | // av_log(s, AV_LOG_DEBUG, "DV vsc pack ff/ss = %x\n", vsc_pack[2] >> 6); | |
1525 | ||
1526 | switch (stype) { | |
1527 | case 0x18: // DV100 720p | |
1528 | ul_index = 6 + pal; | |
1529 | frame_size = pal ? 288000 : 240000; | |
1530 | if (sc->interlaced) { | |
1531 | av_log(s, AV_LOG_ERROR, "source marked as interlaced but codec profile is progressive\n"); | |
1532 | sc->interlaced = 0; | |
1533 | } | |
1534 | break; | |
1535 | case 0x14: // DV100 1080i | |
1536 | ul_index = 4 + pal; | |
1537 | frame_size = pal ? 576000 : 480000; | |
1538 | break; | |
1539 | case 0x04: // DV50 | |
1540 | ul_index = 2 + pal; | |
1541 | frame_size = pal ? 288000 : 240000; | |
1542 | break; | |
1543 | default: // DV25 | |
1544 | ul_index = 0 + pal; | |
1545 | frame_size = pal ? 144000 : 120000; | |
1546 | } | |
1547 | ||
1548 | sc->index = ul_index + 16; | |
1549 | sc->codec_ul = &mxf_essence_container_uls[sc->index].codec_ul; | |
1550 | ||
1551 | mxf->edit_unit_byte_count = KAG_SIZE; | |
1552 | for (i = 0; i < s->nb_streams; i++) { | |
1553 | AVStream *st = s->streams[i]; | |
1554 | MXFStreamContext *sc = st->priv_data; | |
1555 | if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) { | |
1556 | mxf->edit_unit_byte_count += 16 + 4 + sc->aic.samples[0]*sc->aic.sample_size; | |
1557 | mxf->edit_unit_byte_count += klv_fill_size(mxf->edit_unit_byte_count); | |
1558 | } else if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) { | |
1559 | mxf->edit_unit_byte_count += 16 + 4 + frame_size; | |
1560 | mxf->edit_unit_byte_count += klv_fill_size(mxf->edit_unit_byte_count); | |
1561 | } | |
1562 | } | |
1563 | ||
1564 | return 1; | |
1565 | } | |
1566 | ||
1567 | static const UID mxf_mpeg2_codec_uls[] = { | |
1568 | { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x01,0x10,0x00 }, // MP-ML I-Frame | |
1569 | { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x01,0x11,0x00 }, // MP-ML Long GOP | |
1570 | { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x02,0x02,0x00 }, // 422P-ML I-Frame | |
1571 | { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x02,0x03,0x00 }, // 422P-ML Long GOP | |
1572 | { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x03,0x02,0x00 }, // MP-HL I-Frame | |
1573 | { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x03,0x03,0x00 }, // MP-HL Long GOP | |
1574 | { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x04,0x02,0x00 }, // 422P-HL I-Frame | |
1575 | { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x04,0x03,0x00 }, // 422P-HL Long GOP | |
1576 | { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x05,0x02,0x00 }, // MP@H-14 I-Frame | |
1577 | { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x05,0x03,0x00 }, // MP@H-14 Long GOP | |
1578 | }; | |
1579 | ||
1580 | static const UID *mxf_get_mpeg2_codec_ul(AVCodecContext *avctx) | |
1581 | { | |
1582 | int long_gop = avctx->gop_size > 1 || avctx->has_b_frames; | |
1583 | ||
1584 | if (avctx->profile == 4) { // Main | |
1585 | if (avctx->level == 8) // Main | |
1586 | return &mxf_mpeg2_codec_uls[0+long_gop]; | |
1587 | else if (avctx->level == 4) // High | |
1588 | return &mxf_mpeg2_codec_uls[4+long_gop]; | |
1589 | else if (avctx->level == 6) // High 14 | |
1590 | return &mxf_mpeg2_codec_uls[8+long_gop]; | |
1591 | } else if (avctx->profile == 0) { // 422 | |
1592 | if (avctx->level == 5) // Main | |
1593 | return &mxf_mpeg2_codec_uls[2+long_gop]; | |
1594 | else if (avctx->level == 2) // High | |
1595 | return &mxf_mpeg2_codec_uls[6+long_gop]; | |
1596 | } | |
1597 | return NULL; | |
1598 | } | |
1599 | ||
1600 | static int mxf_parse_mpeg2_frame(AVFormatContext *s, AVStream *st, | |
1601 | AVPacket *pkt, MXFIndexEntry *e) | |
1602 | { | |
1603 | MXFStreamContext *sc = st->priv_data; | |
1604 | uint32_t c = -1; | |
1605 | int i; | |
1606 | ||
1607 | for(i = 0; i < pkt->size - 4; i++) { | |
1608 | c = (c<<8) + pkt->data[i]; | |
1609 | if (c == 0x1b5) { | |
1610 | if ((pkt->data[i+1] & 0xf0) == 0x10) { // seq ext | |
1611 | st->codec->profile = pkt->data[i+1] & 0x07; | |
1612 | st->codec->level = pkt->data[i+2] >> 4; | |
1613 | } else if (i + 5 < pkt->size && (pkt->data[i+1] & 0xf0) == 0x80) { // pict coding ext | |
1614 | sc->interlaced = !(pkt->data[i+5] & 0x80); // progressive frame | |
1615 | if (sc->interlaced) | |
1616 | sc->field_dominance = 1 + !(pkt->data[i+4] & 0x80); // top field first | |
1617 | break; | |
1618 | } | |
1619 | } else if (c == 0x1b8) { // gop | |
1620 | if (pkt->data[i+4]>>6 & 0x01) { // closed | |
1621 | sc->closed_gop = 1; | |
1622 | if (e->flags & 0x40) // sequence header present | |
1623 | e->flags |= 0x80; // random access | |
1624 | } | |
1625 | } else if (c == 0x1b3) { // seq | |
1626 | e->flags |= 0x40; | |
1627 | switch ((pkt->data[i+4]>>4) & 0xf) { | |
1628 | case 2: sc->aspect_ratio = (AVRational){ 4, 3}; break; | |
1629 | case 3: sc->aspect_ratio = (AVRational){ 16, 9}; break; | |
1630 | case 4: sc->aspect_ratio = (AVRational){221,100}; break; | |
1631 | default: | |
1632 | av_reduce(&sc->aspect_ratio.num, &sc->aspect_ratio.den, | |
1633 | st->codec->width, st->codec->height, 1024*1024); | |
1634 | } | |
1635 | } else if (c == 0x100) { // pic | |
1636 | int pict_type = (pkt->data[i+2]>>3) & 0x07; | |
1637 | e->temporal_ref = (pkt->data[i+1]<<2) | (pkt->data[i+2]>>6); | |
1638 | if (pict_type == 2) { // P frame | |
1639 | e->flags |= 0x22; | |
1640 | sc->closed_gop = 0; // reset closed gop, don't matter anymore | |
1641 | } else if (pict_type == 3) { // B frame | |
1642 | if (sc->closed_gop) | |
1643 | e->flags |= 0x13; // only backward prediction | |
1644 | else | |
1645 | e->flags |= 0x33; | |
1646 | sc->temporal_reordering = -1; | |
1647 | } else if (!pict_type) { | |
1648 | av_log(s, AV_LOG_ERROR, "error parsing mpeg2 frame\n"); | |
1649 | return 0; | |
1650 | } | |
1651 | } | |
1652 | } | |
1653 | if (s->oformat != &ff_mxf_d10_muxer) | |
1654 | sc->codec_ul = mxf_get_mpeg2_codec_ul(st->codec); | |
1655 | return !!sc->codec_ul; | |
1656 | } | |
1657 | ||
1658 | static uint64_t mxf_parse_timestamp(time_t timestamp) | |
1659 | { | |
1660 | struct tm *time = gmtime(×tamp); | |
1661 | if (!time) | |
1662 | return 0; | |
1663 | return (uint64_t)(time->tm_year+1900) << 48 | | |
1664 | (uint64_t)(time->tm_mon+1) << 40 | | |
1665 | (uint64_t) time->tm_mday << 32 | | |
1666 | time->tm_hour << 24 | | |
1667 | time->tm_min << 16 | | |
1668 | time->tm_sec << 8; | |
1669 | } | |
1670 | ||
1671 | static void mxf_gen_umid(AVFormatContext *s) | |
1672 | { | |
1673 | MXFContext *mxf = s->priv_data; | |
1674 | uint32_t seed = av_get_random_seed(); | |
1675 | uint64_t umid = seed + 0x5294713400000000LL; | |
1676 | ||
1677 | AV_WB64(mxf->umid , umid); | |
1678 | AV_WB64(mxf->umid+8, umid>>8); | |
1679 | ||
1680 | mxf->instance_number = seed & 0xFFFFFF; | |
1681 | } | |
1682 | ||
1683 | static int mxf_write_header(AVFormatContext *s) | |
1684 | { | |
1685 | MXFContext *mxf = s->priv_data; | |
1686 | int i, ret; | |
1687 | uint8_t present[FF_ARRAY_ELEMS(mxf_essence_container_uls)] = {0}; | |
1688 | const MXFSamplesPerFrame *spf = NULL; | |
1689 | AVDictionaryEntry *t; | |
1690 | int64_t timestamp = 0; | |
1691 | AVDictionaryEntry *tcr = av_dict_get(s->metadata, "timecode", NULL, 0); | |
1692 | ||
1693 | if (!s->nb_streams) | |
1694 | return -1; | |
1695 | ||
1696 | for (i = 0; i < s->nb_streams; i++) { | |
1697 | AVStream *st = s->streams[i]; | |
1698 | MXFStreamContext *sc = av_mallocz(sizeof(*sc)); | |
1699 | if (!sc) | |
1700 | return AVERROR(ENOMEM); | |
1701 | st->priv_data = sc; | |
1702 | ||
1703 | if ((i == 0) ^ (st->codec->codec_type == AVMEDIA_TYPE_VIDEO)) { | |
1704 | av_log(s, AV_LOG_ERROR, "there must be exactly one video stream and it must be the first one\n"); | |
1705 | return -1; | |
1706 | } | |
1707 | ||
1708 | if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) { | |
1709 | // TODO: should be avg_frame_rate | |
1710 | AVRational rate, tbc = st->time_base; | |
1711 | // Default component depth to 8 | |
1712 | sc->component_depth = 8; | |
1713 | mxf->timecode_base = (tbc.den + tbc.num/2) / tbc.num; | |
1714 | spf = ff_mxf_get_samples_per_frame(s, tbc); | |
1715 | if (!spf) { | |
1716 | av_log(s, AV_LOG_ERROR, "Unsupported video frame rate %d/%d\n", | |
1717 | tbc.den, tbc.num); | |
1718 | return AVERROR(EINVAL); | |
1719 | } | |
1720 | mxf->time_base = spf->time_base; | |
1721 | rate = av_inv_q(mxf->time_base); | |
1722 | avpriv_set_pts_info(st, 64, mxf->time_base.num, mxf->time_base.den); | |
1723 | if (!tcr) | |
1724 | tcr = av_dict_get(st->metadata, "timecode", NULL, 0); | |
1725 | if (tcr) | |
1726 | ret = av_timecode_init_from_string(&mxf->tc, rate, tcr->value, s); | |
1727 | else | |
1728 | ret = av_timecode_init(&mxf->tc, rate, 0, 0, s); | |
1729 | if (ret < 0) | |
1730 | return ret; | |
1731 | sc->video_bit_rate = st->codec->bit_rate ? st->codec->bit_rate : st->codec->rc_max_rate; | |
1732 | if (s->oformat == &ff_mxf_d10_muxer) { | |
1733 | if (sc->video_bit_rate == 50000000) { | |
1734 | if (mxf->time_base.den == 25) sc->index = 3; | |
1735 | else sc->index = 5; | |
1736 | } else if (sc->video_bit_rate == 40000000) { | |
1737 | if (mxf->time_base.den == 25) sc->index = 7; | |
1738 | else sc->index = 9; | |
1739 | } else if (sc->video_bit_rate == 30000000) { | |
1740 | if (mxf->time_base.den == 25) sc->index = 11; | |
1741 | else sc->index = 13; | |
1742 | } else { | |
1743 | av_log(s, AV_LOG_ERROR, "error MXF D-10 only support 30/40/50 mbit/s\n"); | |
1744 | return -1; | |
1745 | } | |
1746 | ||
1747 | mxf->edit_unit_byte_count = KAG_SIZE; // system element | |
1748 | mxf->edit_unit_byte_count += 16 + 4 + (uint64_t)sc->video_bit_rate * | |
1749 | mxf->time_base.num / (8*mxf->time_base.den); | |
1750 | mxf->edit_unit_byte_count += klv_fill_size(mxf->edit_unit_byte_count); | |
1751 | mxf->edit_unit_byte_count += 16 + 4 + 4 + spf->samples_per_frame[0]*8*4; | |
1752 | mxf->edit_unit_byte_count += klv_fill_size(mxf->edit_unit_byte_count); | |
1753 | } | |
1754 | } else if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) { | |
1755 | if (st->codec->sample_rate != 48000) { | |
1756 | av_log(s, AV_LOG_ERROR, "only 48khz is implemented\n"); | |
1757 | return -1; | |
1758 | } | |
1759 | avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate); | |
1760 | if (s->oformat == &ff_mxf_d10_muxer) { | |
1761 | if (st->index != 1) { | |
1762 | av_log(s, AV_LOG_ERROR, "MXF D-10 only support one audio track\n"); | |
1763 | return -1; | |
1764 | } | |
1765 | if (st->codec->codec_id != AV_CODEC_ID_PCM_S16LE && | |
1766 | st->codec->codec_id != AV_CODEC_ID_PCM_S24LE) { | |
1767 | av_log(s, AV_LOG_ERROR, "MXF D-10 only support 16 or 24 bits le audio\n"); | |
1768 | } | |
1769 | sc->index = ((MXFStreamContext*)s->streams[0]->priv_data)->index + 1; | |
1770 | } else | |
1771 | mxf->slice_count = 1; | |
1772 | } | |
1773 | ||
1774 | if (!sc->index) { | |
1775 | sc->index = mxf_get_essence_container_ul_index(st->codec->codec_id); | |
1776 | if (sc->index == -1) { | |
1777 | av_log(s, AV_LOG_ERROR, "track %d: could not find essence container ul, " | |
1778 | "codec not currently supported in container\n", i); | |
1779 | return -1; | |
1780 | } | |
1781 | } | |
1782 | ||
1783 | sc->codec_ul = &mxf_essence_container_uls[sc->index].codec_ul; | |
1784 | ||
1785 | memcpy(sc->track_essence_element_key, mxf_essence_container_uls[sc->index].element_ul, 15); | |
1786 | sc->track_essence_element_key[15] = present[sc->index]; | |
1787 | PRINT_KEY(s, "track essence element key", sc->track_essence_element_key); | |
1788 | ||
1789 | if (!present[sc->index]) | |
1790 | mxf->essence_container_count++; | |
1791 | present[sc->index]++; | |
1792 | } | |
1793 | ||
1794 | if (s->oformat == &ff_mxf_d10_muxer) { | |
1795 | mxf->essence_container_count = 1; | |
1796 | } | |
1797 | ||
1798 | if (!(s->flags & AVFMT_FLAG_BITEXACT)) | |
1799 | mxf_gen_umid(s); | |
1800 | ||
1801 | for (i = 0; i < s->nb_streams; i++) { | |
1802 | MXFStreamContext *sc = s->streams[i]->priv_data; | |
1803 | // update element count | |
1804 | sc->track_essence_element_key[13] = present[sc->index]; | |
1805 | if (!memcmp(sc->track_essence_element_key, mxf_essence_container_uls[15].element_ul, 13)) // DV | |
1806 | sc->order = (0x15 << 24) | AV_RB32(sc->track_essence_element_key+13); | |
1807 | else | |
1808 | sc->order = AV_RB32(sc->track_essence_element_key+12); | |
1809 | } | |
1810 | ||
1811 | if (t = av_dict_get(s->metadata, "creation_time", NULL, 0)) | |
1812 | timestamp = ff_iso8601_to_unix_time(t->value); | |
1813 | if (timestamp) | |
1814 | mxf->timestamp = mxf_parse_timestamp(timestamp); | |
1815 | mxf->duration = -1; | |
1816 | ||
1817 | mxf->timecode_track = av_mallocz(sizeof(*mxf->timecode_track)); | |
1818 | if (!mxf->timecode_track) | |
1819 | return AVERROR(ENOMEM); | |
1820 | mxf->timecode_track->priv_data = av_mallocz(sizeof(MXFStreamContext)); | |
1821 | if (!mxf->timecode_track->priv_data) | |
1822 | return AVERROR(ENOMEM); | |
1823 | mxf->timecode_track->index = -1; | |
1824 | ||
1825 | if (!spf) | |
1826 | spf = ff_mxf_get_samples_per_frame(s, (AVRational){ 1, 25 }); | |
1827 | ||
1828 | if (ff_audio_interleave_init(s, spf->samples_per_frame, mxf->time_base) < 0) | |
1829 | return -1; | |
1830 | ||
1831 | return 0; | |
1832 | } | |
1833 | ||
1834 | static const uint8_t system_metadata_pack_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0D,0x01,0x03,0x01,0x04,0x01,0x01,0x00 }; | |
1835 | static const uint8_t system_metadata_package_set_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x43,0x01,0x01,0x0D,0x01,0x03,0x01,0x04,0x01,0x02,0x01 }; | |
1836 | ||
1837 | static void mxf_write_system_item(AVFormatContext *s) | |
1838 | { | |
1839 | MXFContext *mxf = s->priv_data; | |
1840 | AVIOContext *pb = s->pb; | |
1841 | unsigned frame; | |
1842 | uint32_t time_code; | |
1843 | ||
1844 | frame = mxf->last_indexed_edit_unit + mxf->edit_units_count; | |
1845 | ||
1846 | // write system metadata pack | |
1847 | avio_write(pb, system_metadata_pack_key, 16); | |
1848 | klv_encode_ber4_length(pb, 57); | |
1849 | avio_w8(pb, 0x5c); // UL, user date/time stamp, picture and sound item present | |
1850 | avio_w8(pb, 0x04); // content package rate | |
1851 | avio_w8(pb, 0x00); // content package type | |
1852 | avio_wb16(pb, 0x00); // channel handle | |
1853 | avio_wb16(pb, (mxf->tc.start + frame) & 0xFFFF); // continuity count, supposed to overflow | |
1854 | if (mxf->essence_container_count > 1) | |
1855 | avio_write(pb, multiple_desc_ul, 16); | |
1856 | else { | |
1857 | MXFStreamContext *sc = s->streams[0]->priv_data; | |
1858 | avio_write(pb, mxf_essence_container_uls[sc->index].container_ul, 16); | |
1859 | } | |
1860 | avio_w8(pb, 0); | |
1861 | avio_wb64(pb, 0); | |
1862 | avio_wb64(pb, 0); // creation date/time stamp | |
1863 | ||
1864 | avio_w8(pb, 0x81); // SMPTE 12M time code | |
1865 | time_code = av_timecode_get_smpte_from_framenum(&mxf->tc, frame); | |
1866 | avio_wb32(pb, time_code); | |
1867 | avio_wb32(pb, 0); // binary group data | |
1868 | avio_wb64(pb, 0); | |
1869 | ||
1870 | // write system metadata package set | |
1871 | avio_write(pb, system_metadata_package_set_key, 16); | |
1872 | klv_encode_ber4_length(pb, 35); | |
1873 | avio_w8(pb, 0x83); // UMID | |
1874 | avio_wb16(pb, 0x20); | |
1875 | mxf_write_umid(s, 1); | |
1876 | } | |
1877 | ||
1878 | static void mxf_write_d10_video_packet(AVFormatContext *s, AVStream *st, AVPacket *pkt) | |
1879 | { | |
1880 | MXFContext *mxf = s->priv_data; | |
1881 | AVIOContext *pb = s->pb; | |
1882 | MXFStreamContext *sc = st->priv_data; | |
1883 | int packet_size = (uint64_t)sc->video_bit_rate*mxf->time_base.num / | |
1884 | (8*mxf->time_base.den); // frame size | |
1885 | int pad; | |
1886 | ||
1887 | packet_size += 16 + 4; | |
1888 | packet_size += klv_fill_size(packet_size); | |
1889 | ||
1890 | klv_encode_ber4_length(pb, pkt->size); | |
1891 | avio_write(pb, pkt->data, pkt->size); | |
1892 | ||
1893 | // ensure CBR muxing by padding to correct video frame size | |
1894 | pad = packet_size - pkt->size - 16 - 4; | |
1895 | if (pad > 20) { | |
1896 | avio_write(s->pb, klv_fill_key, 16); | |
1897 | pad -= 16 + 4; | |
1898 | klv_encode_ber4_length(s->pb, pad); | |
1899 | ffio_fill(s->pb, 0, pad); | |
1900 | av_assert1(!(avio_tell(s->pb) & (KAG_SIZE-1))); | |
1901 | } else { | |
1902 | av_log(s, AV_LOG_WARNING, "cannot fill d-10 video packet\n"); | |
1903 | ffio_fill(s->pb, 0, pad); | |
1904 | } | |
1905 | } | |
1906 | ||
1907 | static void mxf_write_d10_audio_packet(AVFormatContext *s, AVStream *st, AVPacket *pkt) | |
1908 | { | |
1909 | MXFContext *mxf = s->priv_data; | |
1910 | AVIOContext *pb = s->pb; | |
1911 | int frame_size = pkt->size / st->codec->block_align; | |
1912 | uint8_t *samples = pkt->data; | |
1913 | uint8_t *end = pkt->data + pkt->size; | |
1914 | int i; | |
1915 | ||
1916 | klv_encode_ber4_length(pb, 4 + frame_size*4*8); | |
1917 | ||
1918 | avio_w8(pb, (frame_size == 1920 ? 0 : (mxf->edit_units_count-1) % 5 + 1)); | |
1919 | avio_wl16(pb, frame_size); | |
1920 | avio_w8(pb, (1<<st->codec->channels)-1); | |
1921 | ||
1922 | while (samples < end) { | |
1923 | for (i = 0; i < st->codec->channels; i++) { | |
1924 | uint32_t sample; | |
1925 | if (st->codec->codec_id == AV_CODEC_ID_PCM_S24LE) { | |
1926 | sample = AV_RL24(samples)<< 4; | |
1927 | samples += 3; | |
1928 | } else { | |
1929 | sample = AV_RL16(samples)<<12; | |
1930 | samples += 2; | |
1931 | } | |
1932 | avio_wl32(pb, sample | i); | |
1933 | } | |
1934 | for (; i < 8; i++) | |
1935 | avio_wl32(pb, i); | |
1936 | } | |
1937 | } | |
1938 | ||
1939 | static int mxf_write_packet(AVFormatContext *s, AVPacket *pkt) | |
1940 | { | |
1941 | MXFContext *mxf = s->priv_data; | |
1942 | AVIOContext *pb = s->pb; | |
1943 | AVStream *st = s->streams[pkt->stream_index]; | |
1944 | MXFStreamContext *sc = st->priv_data; | |
1945 | MXFIndexEntry ie = {0}; | |
1946 | int err; | |
1947 | ||
1948 | if (!mxf->edit_unit_byte_count && !(mxf->edit_units_count % EDIT_UNITS_PER_BODY)) { | |
1949 | if ((err = av_reallocp_array(&mxf->index_entries, mxf->edit_units_count | |
1950 | + EDIT_UNITS_PER_BODY, sizeof(*mxf->index_entries))) < 0) { | |
1951 | mxf->edit_units_count = 0; | |
1952 | av_log(s, AV_LOG_ERROR, "could not allocate index entries\n"); | |
1953 | return err; | |
1954 | } | |
1955 | } | |
1956 | ||
1957 | if (st->codec->codec_id == AV_CODEC_ID_MPEG2VIDEO) { | |
1958 | if (!mxf_parse_mpeg2_frame(s, st, pkt, &ie)) { | |
1959 | av_log(s, AV_LOG_ERROR, "could not get mpeg2 profile and level\n"); | |
1960 | return -1; | |
1961 | } | |
1962 | } else if (st->codec->codec_id == AV_CODEC_ID_DNXHD) { | |
1963 | if (!mxf_parse_dnxhd_frame(s, st, pkt)) { | |
1964 | av_log(s, AV_LOG_ERROR, "could not get dnxhd profile\n"); | |
1965 | return -1; | |
1966 | } | |
1967 | } else if (st->codec->codec_id == AV_CODEC_ID_DVVIDEO) { | |
1968 | if (!mxf_parse_dv_frame(s, st, pkt)) { | |
1969 | av_log(s, AV_LOG_ERROR, "could not get dv profile\n"); | |
1970 | return -1; | |
1971 | } | |
1972 | } | |
1973 | ||
1974 | if (!mxf->header_written) { | |
1975 | if (mxf->edit_unit_byte_count) { | |
1976 | if ((err = mxf_write_partition(s, 1, 2, header_open_partition_key, 1)) < 0) | |
1977 | return err; | |
1978 | mxf_write_klv_fill(s); | |
1979 | mxf_write_index_table_segment(s); | |
1980 | } else { | |
1981 | if ((err = mxf_write_partition(s, 0, 0, header_open_partition_key, 1)) < 0) | |
1982 | return err; | |
1983 | } | |
1984 | mxf->header_written = 1; | |
1985 | } | |
1986 | ||
1987 | if (st->index == 0) { | |
1988 | if (!mxf->edit_unit_byte_count && | |
1989 | (!mxf->edit_units_count || mxf->edit_units_count > EDIT_UNITS_PER_BODY) && | |
1990 | !(ie.flags & 0x33)) { // I frame, Gop start | |
1991 | mxf_write_klv_fill(s); | |
1992 | if ((err = mxf_write_partition(s, 1, 2, body_partition_key, 0)) < 0) | |
1993 | return err; | |
1994 | mxf_write_klv_fill(s); | |
1995 | mxf_write_index_table_segment(s); | |
1996 | } | |
1997 | ||
1998 | mxf_write_klv_fill(s); | |
1999 | mxf_write_system_item(s); | |
2000 | ||
2001 | if (!mxf->edit_unit_byte_count) { | |
2002 | mxf->index_entries[mxf->edit_units_count].offset = mxf->body_offset; | |
2003 | mxf->index_entries[mxf->edit_units_count].flags = ie.flags; | |
2004 | mxf->index_entries[mxf->edit_units_count].temporal_ref = ie.temporal_ref; | |
2005 | mxf->body_offset += KAG_SIZE; // size of system element | |
2006 | } | |
2007 | mxf->edit_units_count++; | |
2008 | } else if (!mxf->edit_unit_byte_count && st->index == 1) { | |
2009 | mxf->index_entries[mxf->edit_units_count-1].slice_offset = | |
2010 | mxf->body_offset - mxf->index_entries[mxf->edit_units_count-1].offset; | |
2011 | } | |
2012 | ||
2013 | mxf_write_klv_fill(s); | |
2014 | avio_write(pb, sc->track_essence_element_key, 16); // write key | |
2015 | if (s->oformat == &ff_mxf_d10_muxer) { | |
2016 | if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) | |
2017 | mxf_write_d10_video_packet(s, st, pkt); | |
2018 | else | |
2019 | mxf_write_d10_audio_packet(s, st, pkt); | |
2020 | } else { | |
2021 | klv_encode_ber4_length(pb, pkt->size); // write length | |
2022 | avio_write(pb, pkt->data, pkt->size); | |
2023 | mxf->body_offset += 16+4+pkt->size + klv_fill_size(16+4+pkt->size); | |
2024 | } | |
2025 | ||
2026 | avio_flush(pb); | |
2027 | ||
2028 | return 0; | |
2029 | } | |
2030 | ||
2031 | static void mxf_write_random_index_pack(AVFormatContext *s) | |
2032 | { | |
2033 | MXFContext *mxf = s->priv_data; | |
2034 | AVIOContext *pb = s->pb; | |
2035 | uint64_t pos = avio_tell(pb); | |
2036 | int i; | |
2037 | ||
2038 | avio_write(pb, random_index_pack_key, 16); | |
2039 | klv_encode_ber_length(pb, 28 + 12LL*mxf->body_partitions_count); | |
2040 | ||
2041 | if (mxf->edit_unit_byte_count) | |
2042 | avio_wb32(pb, 1); // BodySID of header partition | |
2043 | else | |
2044 | avio_wb32(pb, 0); | |
2045 | avio_wb64(pb, 0); // offset of header partition | |
2046 | ||
2047 | for (i = 0; i < mxf->body_partitions_count; i++) { | |
2048 | avio_wb32(pb, 1); // BodySID | |
2049 | avio_wb64(pb, mxf->body_partition_offset[i]); | |
2050 | } | |
2051 | ||
2052 | avio_wb32(pb, 0); // BodySID of footer partition | |
2053 | avio_wb64(pb, mxf->footer_partition_offset); | |
2054 | ||
2055 | avio_wb32(pb, avio_tell(pb) - pos + 4); | |
2056 | } | |
2057 | ||
2058 | static int mxf_write_footer(AVFormatContext *s) | |
2059 | { | |
2060 | MXFContext *mxf = s->priv_data; | |
2061 | AVIOContext *pb = s->pb; | |
2062 | int err; | |
2063 | ||
2064 | mxf->duration = mxf->last_indexed_edit_unit + mxf->edit_units_count; | |
2065 | ||
2066 | mxf_write_klv_fill(s); | |
2067 | mxf->footer_partition_offset = avio_tell(pb); | |
2068 | if (mxf->edit_unit_byte_count) { // no need to repeat index | |
2069 | if ((err = mxf_write_partition(s, 0, 0, footer_partition_key, 0)) < 0) | |
2070 | return err; | |
2071 | } else { | |
2072 | if ((err = mxf_write_partition(s, 0, 2, footer_partition_key, 0)) < 0) | |
2073 | return err; | |
2074 | mxf_write_klv_fill(s); | |
2075 | mxf_write_index_table_segment(s); | |
2076 | } | |
2077 | ||
2078 | mxf_write_klv_fill(s); | |
2079 | mxf_write_random_index_pack(s); | |
2080 | ||
2081 | if (s->pb->seekable) { | |
2082 | avio_seek(pb, 0, SEEK_SET); | |
2083 | if (mxf->edit_unit_byte_count) { | |
2084 | if ((err = mxf_write_partition(s, 1, 2, header_closed_partition_key, 1)) < 0) | |
2085 | return err; | |
2086 | mxf_write_klv_fill(s); | |
2087 | mxf_write_index_table_segment(s); | |
2088 | } else { | |
2089 | if ((err = mxf_write_partition(s, 0, 0, header_closed_partition_key, 1)) < 0) | |
2090 | return err; | |
2091 | } | |
2092 | } | |
2093 | ||
2094 | ff_audio_interleave_close(s); | |
2095 | ||
2096 | av_freep(&mxf->index_entries); | |
2097 | av_freep(&mxf->body_partition_offset); | |
2098 | av_freep(&mxf->timecode_track->priv_data); | |
2099 | av_freep(&mxf->timecode_track); | |
2100 | ||
2101 | mxf_free(s); | |
2102 | ||
2103 | return 0; | |
2104 | } | |
2105 | ||
2106 | static int mxf_interleave_get_packet(AVFormatContext *s, AVPacket *out, AVPacket *pkt, int flush) | |
2107 | { | |
2108 | int i, stream_count = 0; | |
2109 | ||
2110 | for (i = 0; i < s->nb_streams; i++) | |
2111 | stream_count += !!s->streams[i]->last_in_packet_buffer; | |
2112 | ||
2113 | if (stream_count && (s->nb_streams == stream_count || flush)) { | |
2114 | AVPacketList *pktl = s->packet_buffer; | |
2115 | if (s->nb_streams != stream_count) { | |
2116 | AVPacketList *last = NULL; | |
2117 | // find last packet in edit unit | |
2118 | while (pktl) { | |
2119 | if (!stream_count || pktl->pkt.stream_index == 0) | |
2120 | break; | |
2121 | last = pktl; | |
2122 | pktl = pktl->next; | |
2123 | stream_count--; | |
2124 | } | |
2125 | // purge packet queue | |
2126 | while (pktl) { | |
2127 | AVPacketList *next = pktl->next; | |
2128 | ||
2129 | if(s->streams[pktl->pkt.stream_index]->last_in_packet_buffer == pktl) | |
2130 | s->streams[pktl->pkt.stream_index]->last_in_packet_buffer= NULL; | |
2131 | av_free_packet(&pktl->pkt); | |
2132 | av_freep(&pktl); | |
2133 | pktl = next; | |
2134 | } | |
2135 | if (last) | |
2136 | last->next = NULL; | |
2137 | else { | |
2138 | s->packet_buffer = NULL; | |
2139 | s->packet_buffer_end= NULL; | |
2140 | goto out; | |
2141 | } | |
2142 | pktl = s->packet_buffer; | |
2143 | } | |
2144 | ||
2145 | *out = pktl->pkt; | |
2146 | av_dlog(s, "out st:%d dts:%"PRId64"\n", (*out).stream_index, (*out).dts); | |
2147 | s->packet_buffer = pktl->next; | |
2148 | if(s->streams[pktl->pkt.stream_index]->last_in_packet_buffer == pktl) | |
2149 | s->streams[pktl->pkt.stream_index]->last_in_packet_buffer= NULL; | |
2150 | if(!s->packet_buffer) | |
2151 | s->packet_buffer_end= NULL; | |
2152 | av_freep(&pktl); | |
2153 | return 1; | |
2154 | } else { | |
2155 | out: | |
2156 | av_init_packet(out); | |
2157 | return 0; | |
2158 | } | |
2159 | } | |
2160 | ||
2161 | static int mxf_compare_timestamps(AVFormatContext *s, AVPacket *next, AVPacket *pkt) | |
2162 | { | |
2163 | MXFStreamContext *sc = s->streams[pkt ->stream_index]->priv_data; | |
2164 | MXFStreamContext *sc2 = s->streams[next->stream_index]->priv_data; | |
2165 | ||
2166 | return next->dts > pkt->dts || | |
2167 | (next->dts == pkt->dts && sc->order < sc2->order); | |
2168 | } | |
2169 | ||
2170 | static int mxf_interleave(AVFormatContext *s, AVPacket *out, AVPacket *pkt, int flush) | |
2171 | { | |
2172 | return ff_audio_rechunk_interleave(s, out, pkt, flush, | |
2173 | mxf_interleave_get_packet, mxf_compare_timestamps); | |
2174 | } | |
2175 | ||
2176 | static const AVOption d10_options[] = { | |
2177 | { "d10_channelcount", "Force/set channelcount in generic sound essence descriptor", | |
2178 | offsetof(MXFContext, channel_count), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 8, AV_OPT_FLAG_ENCODING_PARAM}, | |
2179 | { NULL }, | |
2180 | }; | |
2181 | ||
2182 | static const AVClass mxf_d10_muxer_class = { | |
2183 | .class_name = "MXF-D10 muxer", | |
2184 | .item_name = av_default_item_name, | |
2185 | .option = d10_options, | |
2186 | .version = LIBAVUTIL_VERSION_INT, | |
2187 | }; | |
2188 | ||
2189 | AVOutputFormat ff_mxf_muxer = { | |
2190 | .name = "mxf", | |
2191 | .long_name = NULL_IF_CONFIG_SMALL("MXF (Material eXchange Format)"), | |
2192 | .mime_type = "application/mxf", | |
2193 | .extensions = "mxf", | |
2194 | .priv_data_size = sizeof(MXFContext), | |
2195 | .audio_codec = AV_CODEC_ID_PCM_S16LE, | |
2196 | .video_codec = AV_CODEC_ID_MPEG2VIDEO, | |
2197 | .write_header = mxf_write_header, | |
2198 | .write_packet = mxf_write_packet, | |
2199 | .write_trailer = mxf_write_footer, | |
2200 | .flags = AVFMT_NOTIMESTAMPS, | |
2201 | .interleave_packet = mxf_interleave, | |
2202 | }; | |
2203 | ||
2204 | AVOutputFormat ff_mxf_d10_muxer = { | |
2205 | .name = "mxf_d10", | |
2206 | .long_name = NULL_IF_CONFIG_SMALL("MXF (Material eXchange Format) D-10 Mapping"), | |
2207 | .mime_type = "application/mxf", | |
2208 | .priv_data_size = sizeof(MXFContext), | |
2209 | .audio_codec = AV_CODEC_ID_PCM_S16LE, | |
2210 | .video_codec = AV_CODEC_ID_MPEG2VIDEO, | |
2211 | .write_header = mxf_write_header, | |
2212 | .write_packet = mxf_write_packet, | |
2213 | .write_trailer = mxf_write_footer, | |
2214 | .flags = AVFMT_NOTIMESTAMPS, | |
2215 | .interleave_packet = mxf_interleave, | |
2216 | .priv_class = &mxf_d10_muxer_class, | |
2217 | }; |