Libav
mxfenc.c
Go to the documentation of this file.
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 Libav.
7  *
8  * Libav 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  * Libav 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 Libav; 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/random_seed.h"
38 #include "libavcodec/bytestream.h"
39 #include "audiointerleave.h"
40 #include "avformat.h"
41 #include "internal.h"
42 #include "mxf.h"
43 
44 static const int NTSC_samples_per_frame[] = { 1602, 1601, 1602, 1601, 1602, 0 };
45 static const int PAL_samples_per_frame[] = { 1920, 0 };
46 
48 
49 #define EDIT_UNITS_PER_BODY 250
50 #define KAG_SIZE 512
51 
52 typedef struct {
53  int local_tag;
56 
57 typedef struct {
59  uint64_t offset;
60  unsigned slice_offset;
61  uint16_t temporal_ref;
63 
64 typedef struct {
67  int index;
68  const UID *codec_ul;
69  int order;
70  int interlaced;
73  int closed_gop;
75 
76 typedef struct {
80  void (*write_desc)(AVFormatContext *, AVStream *);
82 
83 static const struct {
84  enum AVCodecID id;
85  int index;
88  { AV_CODEC_ID_PCM_S24LE, 1 },
89  { AV_CODEC_ID_PCM_S16LE, 1 },
91 };
92 
93 static void mxf_write_wav_desc(AVFormatContext *s, AVStream *st);
94 static void mxf_write_aes3_desc(AVFormatContext *s, AVStream *st);
96 static void mxf_write_cdci_desc(AVFormatContext *s, AVStream *st);
98 
100  { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x02,0x0D,0x01,0x03,0x01,0x02,0x04,0x60,0x01 },
101  { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x15,0x01,0x05,0x00 },
102  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x00,0x00,0x00 },
104  { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x06,0x03,0x00 },
105  { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x16,0x01,0x03,0x00 },
106  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 },
108  { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x06,0x01,0x00 },
109  { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x16,0x01,0x01,0x00 },
110  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 },
112  // D-10 625/50 PAL 50mb/s
113  { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x01,0x01 },
114  { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x05,0x01,0x01,0x00 },
115  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x01,0x02,0x01,0x01 },
117  { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x01,0x01 },
118  { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x06,0x01,0x10,0x00 },
119  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 },
121  // D-10 525/60 NTSC 50mb/s
122  { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x02,0x01 },
123  { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x05,0x01,0x01,0x00 },
124  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x01,0x02,0x01,0x02 },
126  { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x02,0x01 },
127  { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x06,0x01,0x10,0x00 },
128  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 },
130  // D-10 625/50 PAL 40mb/s
131  { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x03,0x01 },
132  { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x05,0x01,0x01,0x00 },
133  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x01,0x02,0x01,0x03 },
135  { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x03,0x01 },
136  { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x06,0x01,0x10,0x00 },
137  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 },
139  // D-10 525/60 NTSC 40mb/s
140  { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x04,0x01 },
141  { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x05,0x01,0x01,0x00 },
142  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x01,0x02,0x01,0x04 },
144  { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x04,0x01 },
145  { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x06,0x01,0x10,0x00 },
146  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 },
148  // D-10 625/50 PAL 30mb/s
149  { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x05,0x01 },
150  { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x05,0x01,0x01,0x00 },
151  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x01,0x02,0x01,0x05 },
153  { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x05,0x01 },
154  { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x06,0x01,0x10,0x00 },
155  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 },
157  // D-10 525/60 NTSC 30mb/s
158  { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x06,0x01 },
159  { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x05,0x01,0x01,0x00 },
160  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x01,0x02,0x01,0x06 },
162  { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x06,0x01 },
163  { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x06,0x01,0x10,0x00 },
164  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 },
166  { { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 },
167  { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 },
168  { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 },
169  NULL },
170 };
171 
172 typedef struct MXFContext {
179  uint64_t timestamp;
185  uint64_t duration;
191  uint64_t body_offset;
192  uint32_t instance_number;
193  uint8_t umid[16];
194 } MXFContext;
195 
196 static const uint8_t uuid_base[] = { 0xAD,0xAB,0x44,0x24,0x2f,0x25,0x4d,0xc7,0x92,0xff,0x29,0xbd };
197 static const uint8_t umid_ul[] = { 0x06,0x0A,0x2B,0x34,0x01,0x01,0x01,0x05,0x01,0x01,0x0D,0x00,0x13 };
198 
202 static const uint8_t op1a_ul[] = { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x02,0x01,0x01,0x01,0x09,0x00 };
203 static const uint8_t footer_partition_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0D,0x01,0x02,0x01,0x01,0x04,0x04,0x00 }; // ClosedComplete
204 static const uint8_t primer_pack_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0D,0x01,0x02,0x01,0x01,0x05,0x01,0x00 };
205 static const uint8_t index_table_segment_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x10,0x01,0x00 };
206 static const uint8_t random_index_pack_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0D,0x01,0x02,0x01,0x01,0x11,0x01,0x00 };
207 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
208 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
209 static const uint8_t klv_fill_key[] = { 0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x03,0x01,0x02,0x10,0x01,0x00,0x00,0x00 };
210 static const uint8_t body_partition_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0D,0x01,0x02,0x01,0x01,0x03,0x04,0x00 }; // ClosedComplete
211 
215 static const uint8_t header_metadata_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0D,0x01,0x01,0x01,0x01 };
216 static const uint8_t multiple_desc_ul[] = { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x0D,0x01,0x03,0x01,0x02,0x7F,0x01,0x00 };
217 
222  // preface set
223  { 0x3C0A, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x01,0x01,0x15,0x02,0x00,0x00,0x00,0x00}}, /* Instance UID */
224  { 0x3B02, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x10,0x02,0x04,0x00,0x00}}, /* Last Modified Date */
225  { 0x3B05, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x03,0x01,0x02,0x01,0x05,0x00,0x00,0x00}}, /* Version */
226  { 0x3B06, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x06,0x04,0x00,0x00}}, /* Identifications reference */
227  { 0x3B03, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x02,0x01,0x00,0x00}}, /* Content Storage reference */
228  { 0x3B09, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x01,0x02,0x02,0x03,0x00,0x00,0x00,0x00}}, /* Operational Pattern UL */
229  { 0x3B0A, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x01,0x02,0x02,0x10,0x02,0x01,0x00,0x00}}, /* Essence Containers UL batch */
230  { 0x3B0B, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x01,0x02,0x02,0x10,0x02,0x02,0x00,0x00}}, /* DM Schemes UL batch */
231  // Identification
232  { 0x3C09, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x20,0x07,0x01,0x01,0x00,0x00,0x00}}, /* This Generation UID */
233  { 0x3C01, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x20,0x07,0x01,0x02,0x01,0x00,0x00}}, /* Company Name */
234  { 0x3C02, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x20,0x07,0x01,0x03,0x01,0x00,0x00}}, /* Product Name */
235  { 0x3C04, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x20,0x07,0x01,0x05,0x01,0x00,0x00}}, /* Version String */
236  { 0x3C05, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x20,0x07,0x01,0x07,0x00,0x00,0x00}}, /* Product ID */
237  { 0x3C06, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x10,0x02,0x03,0x00,0x00}}, /* Modification Date */
238  // Content Storage
239  { 0x1901, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x05,0x01,0x00,0x00}}, /* Package strong reference batch */
240  { 0x1902, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x05,0x02,0x00,0x00}}, /* Package strong reference batch */
241  // Essence Container Data
242  { 0x2701, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x06,0x01,0x00,0x00,0x00}}, /* Linked Package UID */
243  { 0x3F07, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x01,0x03,0x04,0x04,0x00,0x00,0x00,0x00}}, /* BodySID */
244  // Package
245  { 0x4401, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x01,0x01,0x15,0x10,0x00,0x00,0x00,0x00}}, /* Package UID */
246  { 0x4405, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x10,0x01,0x03,0x00,0x00}}, /* Package Creation Date */
247  { 0x4404, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x10,0x02,0x05,0x00,0x00}}, /* Package Modified Date */
248  { 0x4403, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x06,0x05,0x00,0x00}}, /* Tracks Strong reference array */
249  { 0x4701, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x02,0x03,0x00,0x00}}, /* Descriptor */
250  // Track
251  { 0x4801, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x01,0x07,0x01,0x01,0x00,0x00,0x00,0x00}}, /* Track ID */
252  { 0x4804, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x01,0x04,0x01,0x03,0x00,0x00,0x00,0x00}}, /* Track Number */
253  { 0x4B01, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x30,0x04,0x05,0x00,0x00,0x00,0x00}}, /* Edit Rate */
254  { 0x4B02, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x03,0x01,0x03,0x00,0x00}}, /* Origin */
255  { 0x4803, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x02,0x04,0x00,0x00}}, /* Sequence reference */
256  // Sequence
257  { 0x0201, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x07,0x01,0x00,0x00,0x00,0x00,0x00}}, /* Data Definition UL */
258  { 0x0202, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x02,0x01,0x01,0x03,0x00,0x00}}, /* Duration */
259  { 0x1001, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x06,0x09,0x00,0x00}}, /* Structural Components reference array */
260  // Source Clip
261  { 0x1201, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x03,0x01,0x04,0x00,0x00}}, /* Start position */
262  { 0x1101, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x03,0x01,0x00,0x00,0x00}}, /* SourcePackageID */
263  { 0x1102, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x03,0x02,0x00,0x00,0x00}}, /* SourceTrackID */
264  // Timecode Component
265  { 0x1501, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x03,0x01,0x05,0x00,0x00}}, /* Start Time Code */
266  { 0x1502, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x04,0x01,0x01,0x02,0x06,0x00,0x00}}, /* Rounded Time Code Base */
267  { 0x1503, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x04,0x01,0x01,0x05,0x00,0x00,0x00}}, /* Drop Frame */
268  // File Descriptor
269  { 0x3F01, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x06,0x01,0x01,0x04,0x06,0x0B,0x00,0x00}}, /* Sub Descriptors reference array */
270  { 0x3006, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x06,0x01,0x01,0x03,0x05,0x00,0x00,0x00}}, /* Linked Track ID */
271  { 0x3001, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x06,0x01,0x01,0x00,0x00,0x00,0x00}}, /* SampleRate */
272  { 0x3004, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x01,0x02,0x00,0x00}}, /* Essence Container */
273  // Generic Picture Essence Descriptor
274  { 0x320C, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x03,0x01,0x04,0x00,0x00,0x00}}, /* Frame Layout */
275  { 0x320D, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x01,0x03,0x02,0x05,0x00,0x00,0x00}}, /* Video Line Map */
276  { 0x3203, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x02,0x02,0x00,0x00,0x00}}, /* Stored Width */
277  { 0x3202, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x02,0x01,0x00,0x00,0x00}}, /* Stored Height */
278  { 0x3209, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x01,0x0C,0x00,0x00,0x00}}, /* Display Width */
279  { 0x3208, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x01,0x0B,0x00,0x00,0x00}}, /* Display Height */
280  { 0x320E, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x01,0x01,0x01,0x00,0x00,0x00}}, /* Aspect Ratio */
281  { 0x3201, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x01,0x06,0x01,0x00,0x00,0x00,0x00}}, /* Picture Essence Coding */
282  // CDCI Picture Essence Descriptor
283  { 0x3301, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x01,0x05,0x03,0x0A,0x00,0x00,0x00}}, /* Component Depth */
284  { 0x3302, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x01,0x05,0x00,0x00,0x00}}, /* Horizontal Subsampling */
285  // Generic Sound Essence Descriptor
286  { 0x3D02, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x04,0x02,0x03,0x01,0x04,0x00,0x00,0x00}}, /* Locked/Unlocked */
287  { 0x3D03, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x02,0x03,0x01,0x01,0x01,0x00,0x00}}, /* Audio sampling rate */
288  { 0x3D07, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x02,0x01,0x01,0x04,0x00,0x00,0x00}}, /* ChannelCount */
289  { 0x3D01, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x04,0x02,0x03,0x03,0x04,0x00,0x00,0x00}}, /* Quantization bits */
290  { 0x3D06, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x02,0x04,0x02,0x00,0x00,0x00,0x00}}, /* Sound Essence Compression */
291  // Index Table Segment
292  { 0x3F0B, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x05,0x30,0x04,0x06,0x00,0x00,0x00,0x00}}, /* Index Edit Rate */
293  { 0x3F0C, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x07,0x02,0x01,0x03,0x01,0x0A,0x00,0x00}}, /* Index Start Position */
294  { 0x3F0D, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x07,0x02,0x02,0x01,0x01,0x02,0x00,0x00}}, /* Index Duration */
295  { 0x3F05, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x04,0x06,0x02,0x01,0x00,0x00,0x00,0x00}}, /* Edit Unit Byte Count */
296  { 0x3F06, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x01,0x03,0x04,0x05,0x00,0x00,0x00,0x00}}, /* IndexSID */
297  { 0x3F08, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x04,0x04,0x04,0x01,0x01,0x00,0x00,0x00}}, /* Slice Count */
298  { 0x3F09, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x04,0x04,0x01,0x06,0x00,0x00,0x00}}, /* Delta Entry Array */
299  { 0x3F0A, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x04,0x04,0x02,0x05,0x00,0x00,0x00}}, /* Index Entry Array */
300  // MPEG video Descriptor
301  { 0x8000, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x01,0x06,0x02,0x01,0x0B,0x00,0x00}}, /* BitRate */
302  { 0x8007, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x01,0x06,0x02,0x01,0x0A,0x00,0x00}}, /* ProfileAndLevel */
303  // Wave Audio Essence Descriptor
304  { 0x3D09, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x02,0x03,0x03,0x05,0x00,0x00,0x00}}, /* Average Bytes Per Second */
305  { 0x3D0A, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x02,0x03,0x02,0x01,0x00,0x00,0x00}}, /* Block Align */
306 };
307 
308 static void mxf_write_uuid(AVIOContext *pb, enum MXFMetadataSetType type, int value)
309 {
310  avio_write(pb, uuid_base, 12);
311  avio_wb16(pb, type);
312  avio_wb16(pb, value);
313 }
314 
315 static void mxf_write_umid(AVFormatContext *s, int type)
316 {
317  MXFContext *mxf = s->priv_data;
318  avio_write(s->pb, umid_ul, 13);
319  avio_wb24(s->pb, mxf->instance_number);
320  avio_write(s->pb, mxf->umid, 15);
321  avio_w8(s->pb, type);
322 }
323 
324 static void mxf_write_refs_count(AVIOContext *pb, int ref_count)
325 {
326  avio_wb32(pb, ref_count);
327  avio_wb32(pb, 16);
328 }
329 
330 static int klv_ber_length(uint64_t len)
331 {
332  if (len < 128)
333  return 1;
334  else
335  return (av_log2(len) >> 3) + 2;
336 }
337 
338 static int klv_encode_ber_length(AVIOContext *pb, uint64_t len)
339 {
340  // Determine the best BER size
341  int size;
342  if (len < 128) {
343  //short form
344  avio_w8(pb, len);
345  return 1;
346  }
347 
348  size = (av_log2(len) >> 3) + 1;
349 
350  // long form
351  avio_w8(pb, 0x80 + size);
352  while(size) {
353  size--;
354  avio_w8(pb, len >> 8 * size & 0xff);
355  }
356  return 0;
357 }
358 
360 {
361  avio_w8(pb, 0x80 + 3);
362  avio_wb24(pb, len);
363 }
364 
365 /*
366  * Get essence container ul index
367  */
369 {
370  int i;
371  for (i = 0; mxf_essence_mappings[i].id; i++)
372  if (mxf_essence_mappings[i].id == id)
373  return mxf_essence_mappings[i].index;
374  return -1;
375 }
376 
378 {
379  AVIOContext *pb = s->pb;
380  int local_tag_number, i = 0;
381 
382  local_tag_number = FF_ARRAY_ELEMS(mxf_local_tag_batch);
383 
384  avio_write(pb, primer_pack_key, 16);
385  klv_encode_ber_length(pb, local_tag_number * 18 + 8);
386 
387  avio_wb32(pb, local_tag_number); // local_tag num
388  avio_wb32(pb, 18); // item size, always 18 according to the specs
389 
390  for (i = 0; i < local_tag_number; i++) {
391  avio_wb16(pb, mxf_local_tag_batch[i].local_tag);
392  avio_write(pb, mxf_local_tag_batch[i].uid, 16);
393  }
394 }
395 
396 static void mxf_write_local_tag(AVIOContext *pb, int size, int tag)
397 {
398  avio_wb16(pb, tag);
399  avio_wb16(pb, size);
400 }
401 
402 static void mxf_write_metadata_key(AVIOContext *pb, unsigned int value)
403 {
405  avio_wb24(pb, value);
406 }
407 
408 static void mxf_free(AVFormatContext *s)
409 {
410  int i;
411 
412  for (i = 0; i < s->nb_streams; i++) {
413  AVStream *st = s->streams[i];
414  av_freep(&st->priv_data);
415  }
416 }
417 
418 static const MXFCodecUL *mxf_get_data_definition_ul(int type)
419 {
421  while (uls->uid[0]) {
422  if (type == uls->id)
423  break;
424  uls++;
425  }
426  return uls;
427 }
428 
430 {
431  MXFContext *c = s->priv_data;
432  AVIOContext *pb = s->pb;
433  int i;
434 
436  av_log(s,AV_LOG_DEBUG, "essence container count:%d\n", c->essence_container_count);
437  for (i = 0; i < c->essence_container_count; i++) {
438  MXFStreamContext *sc = s->streams[i]->priv_data;
439  avio_write(pb, mxf_essence_container_uls[sc->index].container_ul, 16);
440  }
441 }
442 
444 {
445  MXFContext *mxf = s->priv_data;
446  AVIOContext *pb = s->pb;
447 
448  mxf_write_metadata_key(pb, 0x012f00);
449  PRINT_KEY(s, "preface key", pb->buf_ptr - 16);
450  klv_encode_ber_length(pb, 130 + 16 * mxf->essence_container_count);
451 
452  // write preface set uid
453  mxf_write_local_tag(pb, 16, 0x3C0A);
454  mxf_write_uuid(pb, Preface, 0);
455  PRINT_KEY(s, "preface uid", pb->buf_ptr - 16);
456 
457  // last modified date
458  mxf_write_local_tag(pb, 8, 0x3B02);
459  avio_wb64(pb, mxf->timestamp);
460 
461  // write version
462  mxf_write_local_tag(pb, 2, 0x3B05);
463  avio_wb16(pb, 258); // v1.2
464 
465  // write identification_refs
466  mxf_write_local_tag(pb, 16 + 8, 0x3B06);
467  mxf_write_refs_count(pb, 1);
469 
470  // write content_storage_refs
471  mxf_write_local_tag(pb, 16, 0x3B03);
473 
474  // operational pattern
475  mxf_write_local_tag(pb, 16, 0x3B09);
476  avio_write(pb, op1a_ul, 16);
477 
478  // write essence_container_refs
479  mxf_write_local_tag(pb, 8 + 16 * mxf->essence_container_count, 0x3B0A);
481 
482  // write dm_scheme_refs
483  mxf_write_local_tag(pb, 8, 0x3B0B);
484  avio_wb64(pb, 0);
485 }
486 
487 /*
488  * Write a local tag containing an ascii string as utf-16
489  */
490 static void mxf_write_local_tag_utf16(AVIOContext *pb, int tag, const char *value)
491 {
492  int i, size = strlen(value);
493  mxf_write_local_tag(pb, size*2, tag);
494  for (i = 0; i < size; i++)
495  avio_wb16(pb, value[i]);
496 }
497 
499 {
500  MXFContext *mxf = s->priv_data;
501  AVIOContext *pb = s->pb;
502  const char *company = "Libav";
503  const char *product = "OP1a Muxer";
504  const char *version;
505  int length;
506 
507  mxf_write_metadata_key(pb, 0x013000);
508  PRINT_KEY(s, "identification key", pb->buf_ptr - 16);
509 
510  version = s->flags & AVFMT_FLAG_BITEXACT ?
512  length = 84 + (strlen(company)+strlen(product)+strlen(version))*2; // utf-16
513  klv_encode_ber_length(pb, length);
514 
515  // write uid
516  mxf_write_local_tag(pb, 16, 0x3C0A);
518  PRINT_KEY(s, "identification uid", pb->buf_ptr - 16);
519 
520  // write generation uid
521  mxf_write_local_tag(pb, 16, 0x3C09);
523 
524  mxf_write_local_tag_utf16(pb, 0x3C01, company); // Company Name
525  mxf_write_local_tag_utf16(pb, 0x3C02, product); // Product Name
526  mxf_write_local_tag_utf16(pb, 0x3C04, version); // Version String
527 
528  // write product uid
529  mxf_write_local_tag(pb, 16, 0x3C05);
531 
532  // modification date
533  mxf_write_local_tag(pb, 8, 0x3C06);
534  avio_wb64(pb, mxf->timestamp);
535 }
536 
538 {
539  AVIOContext *pb = s->pb;
540 
541  mxf_write_metadata_key(pb, 0x011800);
542  PRINT_KEY(s, "content storage key", pb->buf_ptr - 16);
543  klv_encode_ber_length(pb, 92);
544 
545  // write uid
546  mxf_write_local_tag(pb, 16, 0x3C0A);
548  PRINT_KEY(s, "content storage uid", pb->buf_ptr - 16);
549 
550  // write package reference
551  mxf_write_local_tag(pb, 16 * 2 + 8, 0x1901);
552  mxf_write_refs_count(pb, 2);
555 
556  // write essence container data
557  mxf_write_local_tag(pb, 8 + 16, 0x1902);
558  mxf_write_refs_count(pb, 1);
560 }
561 
563 {
564  MXFContext *mxf = s->priv_data;
565  AVIOContext *pb = s->pb;
566  MXFStreamContext *sc = st->priv_data;
567 
568  mxf_write_metadata_key(pb, 0x013b00);
569  PRINT_KEY(s, "track key", pb->buf_ptr - 16);
570  klv_encode_ber_length(pb, 80);
571 
572  // write track uid
573  mxf_write_local_tag(pb, 16, 0x3C0A);
574  mxf_write_uuid(pb, type == MaterialPackage ? Track : Track + TypeBottom, st->index);
575  PRINT_KEY(s, "track uid", pb->buf_ptr - 16);
576 
577  // write track id
578  mxf_write_local_tag(pb, 4, 0x4801);
579  avio_wb32(pb, st->index+2);
580 
581  // write track number
582  mxf_write_local_tag(pb, 4, 0x4804);
583  if (type == MaterialPackage)
584  avio_wb32(pb, 0); // track number of material package is 0
585  else
586  avio_write(pb, sc->track_essence_element_key + 12, 4);
587 
588  mxf_write_local_tag(pb, 8, 0x4B01);
589  avio_wb32(pb, mxf->time_base.den);
590  avio_wb32(pb, mxf->time_base.num);
591 
592  // write origin
593  mxf_write_local_tag(pb, 8, 0x4B02);
594  avio_wb64(pb, 0);
595 
596  // write sequence refs
597  mxf_write_local_tag(pb, 16, 0x4803);
599 }
600 
601 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 };
602 
604 {
605  MXFContext *mxf = s->priv_data;
606  AVIOContext *pb = s->pb;
607 
608  // find data define uls
609  mxf_write_local_tag(pb, 16, 0x0201);
610  if (st == mxf->timecode_track)
612  else {
613  const MXFCodecUL *data_def_ul = mxf_get_data_definition_ul(st->codec->codec_type);
614  avio_write(pb, data_def_ul->uid, 16);
615  }
616 
617  // write duration
618  mxf_write_local_tag(pb, 8, 0x0202);
619  avio_wb64(pb, mxf->duration);
620 }
621 
623 {
624  MXFContext *mxf = s->priv_data;
625  AVIOContext *pb = s->pb;
626  enum MXFMetadataSetType component;
627 
628  mxf_write_metadata_key(pb, 0x010f00);
629  PRINT_KEY(s, "sequence key", pb->buf_ptr - 16);
630  klv_encode_ber_length(pb, 80);
631 
632  mxf_write_local_tag(pb, 16, 0x3C0A);
634 
635  PRINT_KEY(s, "sequence uid", pb->buf_ptr - 16);
637 
638  // write structural component
639  mxf_write_local_tag(pb, 16 + 8, 0x1001);
640  mxf_write_refs_count(pb, 1);
641  if (st == mxf->timecode_track)
642  component = TimecodeComponent;
643  else
644  component = SourceClip;
645  if (type == SourcePackage)
646  component += TypeBottom;
647  mxf_write_uuid(pb, component, st->index);
648 }
649 
651 {
652  MXFContext *mxf = s->priv_data;
653  AVIOContext *pb = s->pb;
654 
655  mxf_write_metadata_key(pb, 0x011400);
656  klv_encode_ber_length(pb, 75);
657 
658  // UID
659  mxf_write_local_tag(pb, 16, 0x3C0A);
662 
664 
665  // Start Time Code
666  mxf_write_local_tag(pb, 8, 0x1501);
667  avio_wb64(pb, mxf->timecode_start);
668 
669  // Rounded Time Code Base
670  mxf_write_local_tag(pb, 2, 0x1502);
671  avio_wb16(pb, mxf->timecode_base);
672 
673  // Drop Frame
674  mxf_write_local_tag(pb, 1, 0x1503);
675  avio_w8(pb, mxf->timecode_drop_frame);
676 }
677 
679 {
680  AVIOContext *pb = s->pb;
681  int i;
682 
683  mxf_write_metadata_key(pb, 0x011100);
684  PRINT_KEY(s, "sturctural component key", pb->buf_ptr - 16);
685  klv_encode_ber_length(pb, 108);
686 
687  // write uid
688  mxf_write_local_tag(pb, 16, 0x3C0A);
690 
691  PRINT_KEY(s, "structural component uid", pb->buf_ptr - 16);
693 
694  // write start_position
695  mxf_write_local_tag(pb, 8, 0x1201);
696  avio_wb64(pb, 0);
697 
698  // write source package uid, end of the reference
699  mxf_write_local_tag(pb, 32, 0x1101);
700  if (type == SourcePackage) {
701  for (i = 0; i < 4; i++)
702  avio_wb64(pb, 0);
703  } else
704  mxf_write_umid(s, 1);
705 
706  // write source track id
707  mxf_write_local_tag(pb, 4, 0x1102);
708  if (type == SourcePackage)
709  avio_wb32(pb, 0);
710  else
711  avio_wb32(pb, st->index+2);
712 }
713 
715 {
716  MXFContext *mxf = s->priv_data;
717  AVIOContext *pb = s->pb;
718  const uint8_t *ul;
719  int i;
720 
721  mxf_write_metadata_key(pb, 0x014400);
722  PRINT_KEY(s, "multiple descriptor key", pb->buf_ptr - 16);
723  klv_encode_ber_length(pb, 64 + 16 * s->nb_streams);
724 
725  mxf_write_local_tag(pb, 16, 0x3C0A);
727  PRINT_KEY(s, "multi_desc uid", pb->buf_ptr - 16);
728 
729  // write sample rate
730  mxf_write_local_tag(pb, 8, 0x3001);
731  avio_wb32(pb, mxf->time_base.den);
732  avio_wb32(pb, mxf->time_base.num);
733 
734  // write essence container ul
735  mxf_write_local_tag(pb, 16, 0x3004);
736  if (mxf->essence_container_count > 1)
737  ul = multiple_desc_ul;
738  else {
739  MXFStreamContext *sc = s->streams[0]->priv_data;
740  ul = mxf_essence_container_uls[sc->index].container_ul;
741  }
742  avio_write(pb, ul, 16);
743 
744  // write sub descriptor refs
745  mxf_write_local_tag(pb, s->nb_streams * 16 + 8, 0x3F01);
747  for (i = 0; i < s->nb_streams; i++)
749 }
750 
751 static void mxf_write_generic_desc(AVFormatContext *s, AVStream *st, const UID key, unsigned size)
752 {
753  MXFContext *mxf = s->priv_data;
754  MXFStreamContext *sc = st->priv_data;
755  AVIOContext *pb = s->pb;
756 
757  avio_write(pb, key, 16);
758  klv_encode_ber4_length(pb, size+20+8+12+20);
759 
760  mxf_write_local_tag(pb, 16, 0x3C0A);
762 
763  mxf_write_local_tag(pb, 4, 0x3006);
764  avio_wb32(pb, st->index+2);
765 
766  mxf_write_local_tag(pb, 8, 0x3001);
767  avio_wb32(pb, mxf->time_base.den);
768  avio_wb32(pb, mxf->time_base.num);
769 
770  mxf_write_local_tag(pb, 16, 0x3004);
771  avio_write(pb, mxf_essence_container_uls[sc->index].container_ul, 16);
772 }
773 
774 static const UID mxf_mpegvideo_descriptor_key = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x51,0x00 };
775 static const UID mxf_wav_descriptor_key = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x48,0x00 };
776 static const UID mxf_aes3_descriptor_key = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x47,0x00 };
777 static const UID mxf_cdci_descriptor_key = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0D,0x01,0x01,0x01,0x01,0x01,0x28,0x00 };
778 static const UID mxf_generic_sound_descriptor_key = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0D,0x01,0x01,0x01,0x01,0x01,0x42,0x00 };
779 
780 static void mxf_write_cdci_common(AVFormatContext *s, AVStream *st, const UID key, unsigned size)
781 {
782  MXFStreamContext *sc = st->priv_data;
783  AVIOContext *pb = s->pb;
784  int stored_height = (st->codec->height+15)/16*16;
785  int display_height;
786  int f1, f2;
787 
788  mxf_write_generic_desc(s, st, key, size+8+8+8+8+8+8+5+16+sc->interlaced*4+12+20);
789 
790  mxf_write_local_tag(pb, 4, 0x3203);
791  avio_wb32(pb, st->codec->width);
792 
793  mxf_write_local_tag(pb, 4, 0x3202);
794  avio_wb32(pb, stored_height>>sc->interlaced);
795 
796  mxf_write_local_tag(pb, 4, 0x3209);
797  avio_wb32(pb, st->codec->width);
798 
799  if (st->codec->height == 608) // PAL + VBI
800  display_height = 576;
801  else if (st->codec->height == 512) // NTSC + VBI
802  display_height = 486;
803  else
804  display_height = st->codec->height;
805 
806  mxf_write_local_tag(pb, 4, 0x3208);
807  avio_wb32(pb, display_height>>sc->interlaced);
808 
809  // component depth
810  mxf_write_local_tag(pb, 4, 0x3301);
811  avio_wb32(pb, 8);
812 
813  // horizontal subsampling
814  mxf_write_local_tag(pb, 4, 0x3302);
815  avio_wb32(pb, 2);
816 
817  // frame layout
818  mxf_write_local_tag(pb, 1, 0x320C);
819  avio_w8(pb, sc->interlaced);
820 
821  // video line map
822  switch (st->codec->height) {
823  case 576: f1 = 23; f2 = 336; break;
824  case 608: f1 = 7; f2 = 320; break;
825  case 480: f1 = 20; f2 = 283; break;
826  case 512: f1 = 7; f2 = 270; break;
827  case 720: f1 = 26; f2 = 0; break; // progressive
828  case 1080: f1 = 21; f2 = 584; break;
829  default: f1 = 0; f2 = 0; break;
830  }
831 
832  if (!sc->interlaced) {
833  f2 = 0;
834  f1 *= 2;
835  }
836 
837  mxf_write_local_tag(pb, 12+sc->interlaced*4, 0x320D);
838  avio_wb32(pb, sc->interlaced ? 2 : 1);
839  avio_wb32(pb, 4);
840  avio_wb32(pb, f1);
841  if (sc->interlaced)
842  avio_wb32(pb, f2);
843 
844  mxf_write_local_tag(pb, 8, 0x320E);
845  avio_wb32(pb, sc->aspect_ratio.num);
846  avio_wb32(pb, sc->aspect_ratio.den);
847 
848  mxf_write_local_tag(pb, 16, 0x3201);
849  avio_write(pb, *sc->codec_ul, 16);
850 }
851 
853 {
855 }
856 
858 {
859  AVIOContext *pb = s->pb;
860  int profile_and_level = (st->codec->profile<<4) | st->codec->level;
861 
863 
864  // bit rate
865  mxf_write_local_tag(pb, 4, 0x8000);
866  avio_wb32(pb, st->codec->bit_rate);
867 
868  // profile and level
869  mxf_write_local_tag(pb, 1, 0x8007);
870  if (!st->codec->profile)
871  profile_and_level |= 0x80; // escape bit
872  avio_w8(pb, profile_and_level);
873 }
874 
875 static void mxf_write_generic_sound_common(AVFormatContext *s, AVStream *st, const UID key, unsigned size)
876 {
877  AVIOContext *pb = s->pb;
878 
879  mxf_write_generic_desc(s, st, key, size+5+12+8+8);
880 
881  // audio locked
882  mxf_write_local_tag(pb, 1, 0x3D02);
883  avio_w8(pb, 1);
884 
885  // write audio sampling rate
886  mxf_write_local_tag(pb, 8, 0x3D03);
887  avio_wb32(pb, st->codec->sample_rate);
888  avio_wb32(pb, 1);
889 
890  mxf_write_local_tag(pb, 4, 0x3D07);
891  avio_wb32(pb, st->codec->channels);
892 
893  mxf_write_local_tag(pb, 4, 0x3D01);
895 }
896 
897 static void mxf_write_wav_common(AVFormatContext *s, AVStream *st, const UID key, unsigned size)
898 {
899  AVIOContext *pb = s->pb;
900 
901  mxf_write_generic_sound_common(s, st, key, size+6+8);
902 
903  mxf_write_local_tag(pb, 2, 0x3D0A);
904  avio_wb16(pb, st->codec->block_align);
905 
906  // avg bytes per sec
907  mxf_write_local_tag(pb, 4, 0x3D09);
909 }
910 
912 {
914 }
915 
917 {
919 }
920 
922 {
924 }
925 
927 {
928  MXFContext *mxf = s->priv_data;
929  AVIOContext *pb = s->pb;
930  int i, track_count = s->nb_streams+1;
931 
932  if (type == MaterialPackage) {
933  mxf_write_metadata_key(pb, 0x013600);
934  PRINT_KEY(s, "Material Package key", pb->buf_ptr - 16);
935  klv_encode_ber_length(pb, 92 + 16*track_count);
936  } else {
937  mxf_write_metadata_key(pb, 0x013700);
938  PRINT_KEY(s, "Source Package key", pb->buf_ptr - 16);
939  klv_encode_ber_length(pb, 112 + 16*track_count); // 20 bytes length for descriptor reference
940  }
941 
942  // write uid
943  mxf_write_local_tag(pb, 16, 0x3C0A);
944  mxf_write_uuid(pb, type, 0);
945  av_log(s,AV_LOG_DEBUG, "package type:%d\n", type);
946  PRINT_KEY(s, "package uid", pb->buf_ptr - 16);
947 
948  // write package umid
949  mxf_write_local_tag(pb, 32, 0x4401);
950  mxf_write_umid(s, type == SourcePackage);
951  PRINT_KEY(s, "package umid second part", pb->buf_ptr - 16);
952 
953  // package creation date
954  mxf_write_local_tag(pb, 8, 0x4405);
955  avio_wb64(pb, mxf->timestamp);
956 
957  // package modified date
958  mxf_write_local_tag(pb, 8, 0x4404);
959  avio_wb64(pb, mxf->timestamp);
960 
961  // write track refs
962  mxf_write_local_tag(pb, track_count*16 + 8, 0x4403);
963  mxf_write_refs_count(pb, track_count);
964  mxf_write_uuid(pb, type == MaterialPackage ? Track :
965  Track + TypeBottom, -1); // timecode track
966  for (i = 0; i < s->nb_streams; i++)
967  mxf_write_uuid(pb, type == MaterialPackage ? Track : Track + TypeBottom, i);
968 
969  // write multiple descriptor reference
970  if (type == SourcePackage) {
971  mxf_write_local_tag(pb, 16, 0x4701);
972  if (s->nb_streams > 1) {
975  } else
977  }
978 
979  // write timecode track
980  mxf_write_track(s, mxf->timecode_track, type);
981  mxf_write_sequence(s, mxf->timecode_track, type);
983 
984  for (i = 0; i < s->nb_streams; i++) {
985  AVStream *st = s->streams[i];
986  mxf_write_track(s, st, type);
987  mxf_write_sequence(s, st, type);
988  mxf_write_structural_component(s, st, type);
989 
990  if (type == SourcePackage) {
991  MXFStreamContext *sc = st->priv_data;
992  mxf_essence_container_uls[sc->index].write_desc(s, st);
993  }
994  }
995 }
996 
998 {
999  AVIOContext *pb = s->pb;
1000 
1001  mxf_write_metadata_key(pb, 0x012300);
1002  klv_encode_ber_length(pb, 72);
1003 
1004  mxf_write_local_tag(pb, 16, 0x3C0A); // Instance UID
1006 
1007  mxf_write_local_tag(pb, 32, 0x2701); // Linked Package UID
1008  mxf_write_umid(s, 1);
1009 
1010  mxf_write_local_tag(pb, 4, 0x3F07); // BodySID
1011  avio_wb32(pb, 1);
1012 
1013  mxf_write_local_tag(pb, 4, 0x3F06); // IndexSID
1014  avio_wb32(pb, 2);
1015 
1016  return 0;
1017 }
1018 
1020 {
1021  mxf_write_preface(s);
1027  return 0;
1028 }
1029 
1030 static unsigned klv_fill_size(uint64_t size)
1031 {
1032  unsigned pad = KAG_SIZE - (size & (KAG_SIZE-1));
1033  if (pad < 20) // smallest fill item possible
1034  return pad + KAG_SIZE;
1035  else
1036  return pad & (KAG_SIZE-1);
1037 }
1038 
1040 {
1041  MXFContext *mxf = s->priv_data;
1042  AVIOContext *pb = s->pb;
1043  int i, j, temporal_reordering = 0;
1044  int key_index = mxf->last_key_index;
1045 
1046  av_log(s, AV_LOG_DEBUG, "edit units count %d\n", mxf->edit_units_count);
1047 
1048  if (!mxf->edit_units_count && !mxf->edit_unit_byte_count)
1049  return;
1050 
1052 
1053  if (mxf->edit_unit_byte_count) {
1054  klv_encode_ber_length(pb, 80);
1055  } else {
1056  klv_encode_ber_length(pb, 85 + 12+(s->nb_streams+1)*6 +
1057  12+mxf->edit_units_count*(11+mxf->slice_count*4));
1058  }
1059 
1060  // instance id
1061  mxf_write_local_tag(pb, 16, 0x3C0A);
1063 
1064  // index edit rate
1065  mxf_write_local_tag(pb, 8, 0x3F0B);
1066  avio_wb32(pb, mxf->time_base.den);
1067  avio_wb32(pb, mxf->time_base.num);
1068 
1069  // index start position
1070  mxf_write_local_tag(pb, 8, 0x3F0C);
1072 
1073  // index duration
1074  mxf_write_local_tag(pb, 8, 0x3F0D);
1075  if (mxf->edit_unit_byte_count)
1076  avio_wb64(pb, 0); // index table covers whole container
1077  else
1078  avio_wb64(pb, mxf->edit_units_count);
1079 
1080  // edit unit byte count
1081  mxf_write_local_tag(pb, 4, 0x3F05);
1082  avio_wb32(pb, mxf->edit_unit_byte_count);
1083 
1084  // index sid
1085  mxf_write_local_tag(pb, 4, 0x3F06);
1086  avio_wb32(pb, 2);
1087 
1088  // body sid
1089  mxf_write_local_tag(pb, 4, 0x3F07);
1090  avio_wb32(pb, 1);
1091 
1092  if (!mxf->edit_unit_byte_count) {
1093  // real slice count - 1
1094  mxf_write_local_tag(pb, 1, 0x3F08);
1095  avio_w8(pb, mxf->slice_count);
1096 
1097  // delta entry array
1098  mxf_write_local_tag(pb, 8 + (s->nb_streams+1)*6, 0x3F09);
1099  avio_wb32(pb, s->nb_streams+1); // num of entries
1100  avio_wb32(pb, 6); // size of one entry
1101  // write system item delta entry
1102  avio_w8(pb, 0);
1103  avio_w8(pb, 0); // slice entry
1104  avio_wb32(pb, 0); // element delta
1105  for (i = 0; i < s->nb_streams; i++) {
1106  AVStream *st = s->streams[i];
1107  MXFStreamContext *sc = st->priv_data;
1108  avio_w8(pb, sc->temporal_reordering);
1109  if (sc->temporal_reordering)
1110  temporal_reordering = 1;
1111  if (i == 0) { // video track
1112  avio_w8(pb, 0); // slice number
1113  avio_wb32(pb, KAG_SIZE); // system item size including klv fill
1114  } else { // audio track
1115  unsigned audio_frame_size = sc->aic.samples[0]*sc->aic.sample_size;
1116  audio_frame_size += klv_fill_size(audio_frame_size);
1117  avio_w8(pb, 1);
1118  avio_wb32(pb, (i-1)*audio_frame_size); // element delta
1119  }
1120  }
1121 
1122  mxf_write_local_tag(pb, 8 + mxf->edit_units_count*(11+mxf->slice_count*4), 0x3F0A);
1123  avio_wb32(pb, mxf->edit_units_count); // num of entries
1124  avio_wb32(pb, 11+mxf->slice_count*4); // size of one entry
1125 
1126  for (i = 0; i < mxf->edit_units_count; i++) {
1127  int temporal_offset = 0;
1128 
1129  if (!(mxf->index_entries[i].flags & 0x33)) { // I frame
1130  mxf->last_key_index = key_index;
1131  key_index = i;
1132  }
1133 
1134  if (temporal_reordering) {
1135  int pic_num_in_gop = i - key_index;
1136  if (pic_num_in_gop != mxf->index_entries[i].temporal_ref) {
1137  for (j = key_index; j < mxf->edit_units_count; j++) {
1138  if (pic_num_in_gop == mxf->index_entries[j].temporal_ref)
1139  break;
1140  }
1141  if (j == mxf->edit_units_count)
1142  av_log(s, AV_LOG_WARNING, "missing frames\n");
1143  temporal_offset = j - key_index - pic_num_in_gop;
1144  }
1145  }
1146  avio_w8(pb, temporal_offset);
1147 
1148  if ((mxf->index_entries[i].flags & 0x30) == 0x30) { // back and forward prediction
1149  avio_w8(pb, mxf->last_key_index - i);
1150  } else {
1151  avio_w8(pb, key_index - i); // key frame offset
1152  if ((mxf->index_entries[i].flags & 0x20) == 0x20) // only forward
1153  mxf->last_key_index = key_index;
1154  }
1155 
1156  if (!(mxf->index_entries[i].flags & 0x33) && // I frame
1157  mxf->index_entries[i].flags & 0x40 && !temporal_offset)
1158  mxf->index_entries[i].flags |= 0x80; // random access
1159  avio_w8(pb, mxf->index_entries[i].flags);
1160  // stream offset
1161  avio_wb64(pb, mxf->index_entries[i].offset);
1162  if (s->nb_streams > 1)
1163  avio_wb32(pb, mxf->index_entries[i].slice_offset);
1164  }
1165 
1166  mxf->last_key_index = key_index - mxf->edit_units_count;
1168  mxf->edit_units_count = 0;
1169  }
1170 }
1171 
1173 {
1174  unsigned pad = klv_fill_size(avio_tell(s->pb));
1175  if (pad) {
1176  avio_write(s->pb, klv_fill_key, 16);
1177  pad -= 16 + 4;
1178  klv_encode_ber4_length(s->pb, pad);
1179  for (; pad; pad--)
1180  avio_w8(s->pb, 0);
1181  assert(!(avio_tell(s->pb) & (KAG_SIZE-1)));
1182  }
1183 }
1184 
1185 static int mxf_write_partition(AVFormatContext *s, int bodysid,
1186  int indexsid,
1187  const uint8_t *key, int write_metadata)
1188 {
1189  MXFContext *mxf = s->priv_data;
1190  AVIOContext *pb = s->pb;
1191  int64_t header_byte_count_offset;
1192  unsigned index_byte_count = 0;
1193  uint64_t partition_offset = avio_tell(pb);
1194  int err;
1195 
1196  if (!mxf->edit_unit_byte_count && mxf->edit_units_count)
1197  index_byte_count = 85 + 12+(s->nb_streams+1)*6 +
1198  12+mxf->edit_units_count*(11+mxf->slice_count*4);
1199  else if (mxf->edit_unit_byte_count && indexsid)
1200  index_byte_count = 80;
1201 
1202  if (index_byte_count) {
1203  // add encoded ber length
1204  index_byte_count += 16 + klv_ber_length(index_byte_count);
1205  index_byte_count += klv_fill_size(index_byte_count);
1206  }
1207 
1208  if (!memcmp(key, body_partition_key, 16)) {
1210  sizeof(*mxf->body_partition_offset))) < 0) {
1211  mxf->body_partitions_count = 0;
1212  return err;
1213  }
1214  mxf->body_partition_offset[mxf->body_partitions_count++] = partition_offset;
1215  }
1216 
1217  // write klv
1218  avio_write(pb, key, 16);
1219  klv_encode_ber_length(pb, 88 + 16 * mxf->essence_container_count);
1220 
1221  // write partition value
1222  avio_wb16(pb, 1); // majorVersion
1223  avio_wb16(pb, 2); // minorVersion
1224  avio_wb32(pb, KAG_SIZE); // KAGSize
1225 
1226  avio_wb64(pb, partition_offset); // ThisPartition
1227 
1228  if (!memcmp(key, body_partition_key, 16) && mxf->body_partitions_count > 1)
1229  avio_wb64(pb, mxf->body_partition_offset[mxf->body_partitions_count-2]); // PreviousPartition
1230  else if (!memcmp(key, footer_partition_key, 16) && mxf->body_partitions_count)
1231  avio_wb64(pb, mxf->body_partition_offset[mxf->body_partitions_count-1]); // PreviousPartition
1232  else
1233  avio_wb64(pb, 0);
1234 
1235  avio_wb64(pb, mxf->footer_partition_offset); // footerPartition
1236 
1237  // set offset
1238  header_byte_count_offset = avio_tell(pb);
1239  avio_wb64(pb, 0); // headerByteCount, update later
1240 
1241  // indexTable
1242  avio_wb64(pb, index_byte_count); // indexByteCount
1243  avio_wb32(pb, index_byte_count ? indexsid : 0); // indexSID
1244 
1245  // BodyOffset
1246  if (bodysid && mxf->edit_units_count && mxf->body_partitions_count) {
1247  avio_wb64(pb, mxf->body_offset);
1248  } else
1249  avio_wb64(pb, 0);
1250 
1251  avio_wb32(pb, bodysid); // bodySID
1252 
1253  // operational pattern
1254  avio_write(pb, op1a_ul, 16);
1255 
1256  // essence container
1258 
1259  if (write_metadata) {
1260  // mark the start of the headermetadata and calculate metadata size
1261  int64_t pos, start;
1262  unsigned header_byte_count;
1263 
1264  mxf_write_klv_fill(s);
1265  start = avio_tell(s->pb);
1268  pos = avio_tell(s->pb);
1269  header_byte_count = pos - start + klv_fill_size(pos);
1270 
1271  // update header_byte_count
1272  avio_seek(pb, header_byte_count_offset, SEEK_SET);
1273  avio_wb64(pb, header_byte_count);
1274  avio_seek(pb, pos, SEEK_SET);
1275  }
1276 
1277  avio_flush(pb);
1278 
1279  return 0;
1280 }
1281 
1282 static const UID mxf_mpeg2_codec_uls[] = {
1283  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x01,0x10,0x00 }, // MP-ML I-Frame
1284  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x01,0x11,0x00 }, // MP-ML Long GOP
1285  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x02,0x02,0x00 }, // 422P-ML I-Frame
1286  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x02,0x03,0x00 }, // 422P-ML Long GOP
1287  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x03,0x02,0x00 }, // MP-HL I-Frame
1288  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x03,0x03,0x00 }, // MP-HL Long GOP
1289  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x04,0x02,0x00 }, // 422P-HL I-Frame
1290  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x04,0x03,0x00 }, // 422P-HL Long GOP
1291  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x05,0x02,0x00 }, // MP@H-14 I-Frame
1292  { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x05,0x03,0x00 }, // MP@H-14 Long GOP
1293 };
1294 
1296 {
1297  int long_gop = avctx->gop_size > 1 || avctx->has_b_frames;
1298 
1299  if (avctx->profile == 4) { // Main
1300  if (avctx->level == 8) // Main
1301  return &mxf_mpeg2_codec_uls[0+long_gop];
1302  else if (avctx->level == 4) // High
1303  return &mxf_mpeg2_codec_uls[4+long_gop];
1304  else if (avctx->level == 6) // High 14
1305  return &mxf_mpeg2_codec_uls[8+long_gop];
1306  } else if (avctx->profile == 0) { // 422
1307  if (avctx->level == 5) // Main
1308  return &mxf_mpeg2_codec_uls[2+long_gop];
1309  else if (avctx->level == 2) // High
1310  return &mxf_mpeg2_codec_uls[6+long_gop];
1311  }
1312  return NULL;
1313 }
1314 
1316  AVPacket *pkt, MXFIndexEntry *e)
1317 {
1318  MXFStreamContext *sc = st->priv_data;
1319  MXFContext *mxf = s->priv_data;
1320  uint32_t c = -1;
1321  int i;
1322 
1323  for(i = 0; i < pkt->size - 4; i++) {
1324  c = (c<<8) + pkt->data[i];
1325  if (c == 0x1b5) {
1326  if ((pkt->data[i+1] & 0xf0) == 0x10) { // seq ext
1327  st->codec->profile = pkt->data[i+1] & 0x07;
1328  st->codec->level = pkt->data[i+2] >> 4;
1329  } else if (i + 5 < pkt->size && (pkt->data[i+1] & 0xf0) == 0x80) { // pict coding ext
1330  sc->interlaced = !(pkt->data[i+5] & 0x80); // progressive frame
1331  break;
1332  }
1333  } else if (c == 0x1b8) { // gop
1334  if (pkt->data[i+4]>>6 & 0x01) { // closed
1335  sc->closed_gop = 1;
1336  if (e->flags & 0x40) // sequence header present
1337  e->flags |= 0x80; // random access
1338  }
1339  if (!mxf->header_written) {
1340  unsigned hours = (pkt->data[i+1]>>2) & 0x1f;
1341  unsigned minutes = ((pkt->data[i+1] & 0x03) << 4) | (pkt->data[i+2]>>4);
1342  unsigned seconds = ((pkt->data[i+2] & 0x07) << 3) | (pkt->data[i+3]>>5);
1343  unsigned frames = ((pkt->data[i+3] & 0x1f) << 1) | (pkt->data[i+4]>>7);
1344  mxf->timecode_drop_frame = !!(pkt->data[i+1] & 0x80);
1345  mxf->timecode_start = (hours*3600 + minutes*60 + seconds) *
1346  mxf->timecode_base + frames;
1347  if (mxf->timecode_drop_frame) {
1348  unsigned tminutes = 60 * hours + minutes;
1349  mxf->timecode_start -= 2 * (tminutes - tminutes / 10);
1350  }
1351  av_log(s, AV_LOG_DEBUG, "frame %d %d:%d:%d%c%d\n", mxf->timecode_start,
1352  hours, minutes, seconds, mxf->timecode_drop_frame ? ';':':', frames);
1353  }
1354  } else if (c == 0x1b3) { // seq
1355  e->flags |= 0x40;
1356  switch ((pkt->data[i+4]>>4) & 0xf) {
1357  case 2: sc->aspect_ratio = (AVRational){ 4, 3}; break;
1358  case 3: sc->aspect_ratio = (AVRational){ 16, 9}; break;
1359  case 4: sc->aspect_ratio = (AVRational){221,100}; break;
1360  default:
1362  st->codec->width, st->codec->height, 1024*1024);
1363  }
1364  } else if (c == 0x100) { // pic
1365  int pict_type = (pkt->data[i+2]>>3) & 0x07;
1366  e->temporal_ref = (pkt->data[i+1]<<2) | (pkt->data[i+2]>>6);
1367  if (pict_type == 2) { // P frame
1368  e->flags |= 0x22;
1369  sc->closed_gop = 0; // reset closed gop, don't matter anymore
1370  } else if (pict_type == 3) { // B frame
1371  if (sc->closed_gop)
1372  e->flags |= 0x13; // only backward prediction
1373  else
1374  e->flags |= 0x33;
1375  sc->temporal_reordering = -1;
1376  } else if (!pict_type) {
1377  av_log(s, AV_LOG_ERROR, "error parsing mpeg2 frame\n");
1378  return 0;
1379  }
1380  }
1381  }
1382  if (s->oformat != &ff_mxf_d10_muxer)
1384  return !!sc->codec_ul;
1385 }
1386 
1387 static uint64_t mxf_parse_timestamp(time_t timestamp)
1388 {
1389  struct tm *time = gmtime(&timestamp);
1390  if (!time)
1391  return 0;
1392  return (uint64_t)(time->tm_year+1900) << 48 |
1393  (uint64_t)(time->tm_mon+1) << 40 |
1394  (uint64_t) time->tm_mday << 32 |
1395  time->tm_hour << 24 |
1396  time->tm_min << 16 |
1397  time->tm_sec << 8;
1398 }
1399 
1401 {
1402  MXFContext *mxf = s->priv_data;
1403  uint32_t seed = av_get_random_seed();
1404  uint64_t umid = seed + 0x5294713400000000LL;
1405 
1406  AV_WB64(mxf->umid , umid);
1407  AV_WB64(mxf->umid+8, umid>>8);
1408 
1409  mxf->instance_number = seed;
1410 }
1411 
1413 {
1414  MXFContext *mxf = s->priv_data;
1415  int i;
1416  uint8_t present[FF_ARRAY_ELEMS(mxf_essence_container_uls)] = {0};
1417  const int *samples_per_frame = NULL;
1418  AVDictionaryEntry *t;
1419  int64_t timestamp = 0;
1420 
1421  if (!s->nb_streams)
1422  return -1;
1423 
1424  for (i = 0; i < s->nb_streams; i++) {
1425  AVStream *st = s->streams[i];
1426  MXFStreamContext *sc = av_mallocz(sizeof(*sc));
1427  if (!sc)
1428  return AVERROR(ENOMEM);
1429  st->priv_data = sc;
1430 
1431  if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
1432  if (i != 0) {
1433  av_log(s, AV_LOG_ERROR, "video stream must be first track\n");
1434  return -1;
1435  }
1436  // TODO: should be avg_frame_rate
1437  if (fabs(av_q2d(st->time_base) - 1/25.0) < 0.0001) {
1438  samples_per_frame = PAL_samples_per_frame;
1439  mxf->time_base = (AVRational){ 1, 25 };
1440  mxf->timecode_base = 25;
1441  } else if (fabs(av_q2d(st->time_base) - 1001/30000.0) < 0.0001) {
1442  samples_per_frame = NTSC_samples_per_frame;
1443  mxf->time_base = (AVRational){ 1001, 30000 };
1444  mxf->timecode_base = 30;
1445  } else {
1446  av_log(s, AV_LOG_ERROR, "unsupported video frame rate\n");
1447  return -1;
1448  }
1449  avpriv_set_pts_info(st, 64, mxf->time_base.num, mxf->time_base.den);
1450  if (s->oformat == &ff_mxf_d10_muxer) {
1451  if (st->codec->bit_rate == 50000000)
1452  if (mxf->time_base.den == 25) sc->index = 3;
1453  else sc->index = 5;
1454  else if (st->codec->bit_rate == 40000000)
1455  if (mxf->time_base.den == 25) sc->index = 7;
1456  else sc->index = 9;
1457  else if (st->codec->bit_rate == 30000000)
1458  if (mxf->time_base.den == 25) sc->index = 11;
1459  else sc->index = 13;
1460  else {
1461  av_log(s, AV_LOG_ERROR, "error MXF D-10 only support 30/40/50 mbit/s\n");
1462  return -1;
1463  }
1464 
1465  mxf->edit_unit_byte_count = KAG_SIZE; // system element
1466  mxf->edit_unit_byte_count += 16 + 4 + (uint64_t)st->codec->bit_rate *
1467  mxf->time_base.num / (8*mxf->time_base.den);
1469  mxf->edit_unit_byte_count += 16 + 4 + 4 + samples_per_frame[0]*8*4;
1471  }
1472  } else if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
1473  if (st->codec->sample_rate != 48000) {
1474  av_log(s, AV_LOG_ERROR, "only 48khz is implemented\n");
1475  return -1;
1476  }
1477  avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
1478  if (s->oformat == &ff_mxf_d10_muxer) {
1479  if (st->index != 1) {
1480  av_log(s, AV_LOG_ERROR, "MXF D-10 only support one audio track\n");
1481  return -1;
1482  }
1483  if (st->codec->codec_id != AV_CODEC_ID_PCM_S16LE &&
1485  av_log(s, AV_LOG_ERROR, "MXF D-10 only support 16 or 24 bits le audio\n");
1486  }
1487  sc->index = ((MXFStreamContext*)s->streams[0]->priv_data)->index + 1;
1488  } else
1489  mxf->slice_count = 1;
1490  }
1491 
1492  if (!sc->index) {
1494  if (sc->index == -1) {
1495  av_log(s, AV_LOG_ERROR, "track %d: could not find essence container ul, "
1496  "codec not currently supported in container\n", i);
1497  return -1;
1498  }
1499  }
1500 
1501  sc->codec_ul = &mxf_essence_container_uls[sc->index].codec_ul;
1502 
1503  memcpy(sc->track_essence_element_key, mxf_essence_container_uls[sc->index].element_ul, 15);
1504  sc->track_essence_element_key[15] = present[sc->index];
1505  PRINT_KEY(s, "track essence element key", sc->track_essence_element_key);
1506 
1507  if (!present[sc->index])
1508  mxf->essence_container_count++;
1509  present[sc->index]++;
1510  }
1511 
1512  if (s->oformat == &ff_mxf_d10_muxer) {
1513  mxf->essence_container_count = 1;
1514  }
1515 
1516  if (!(s->flags & AVFMT_FLAG_BITEXACT))
1517  mxf_gen_umid(s);
1518 
1519  for (i = 0; i < s->nb_streams; i++) {
1520  MXFStreamContext *sc = s->streams[i]->priv_data;
1521  // update element count
1522  sc->track_essence_element_key[13] = present[sc->index];
1523  sc->order = AV_RB32(sc->track_essence_element_key+12);
1524  }
1525 
1526  if (t = av_dict_get(s->metadata, "creation_time", NULL, 0))
1527  timestamp = ff_iso8601_to_unix_time(t->value);
1528  if (timestamp)
1529  mxf->timestamp = mxf_parse_timestamp(timestamp);
1530  mxf->duration = -1;
1531 
1532  mxf->timecode_track = av_mallocz(sizeof(*mxf->timecode_track));
1533  if (!mxf->timecode_track)
1534  return AVERROR(ENOMEM);
1536  if (!mxf->timecode_track->priv_data)
1537  return AVERROR(ENOMEM);
1538  mxf->timecode_track->index = -1;
1539 
1540  if (!samples_per_frame)
1541  samples_per_frame = PAL_samples_per_frame;
1542 
1543  if (ff_audio_interleave_init(s, samples_per_frame, mxf->time_base) < 0)
1544  return -1;
1545 
1546  return 0;
1547 }
1548 
1549 static const uint8_t system_metadata_pack_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0D,0x01,0x03,0x01,0x04,0x01,0x01,0x00 };
1550 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 };
1551 
1552 static uint32_t framenum_to_12m_time_code(unsigned frame, int drop, int fps)
1553 {
1554  return (0 << 31) | // color frame flag
1555  (drop << 30) | // drop frame flag
1556  ( ((frame % fps) / 10) << 28) | // tens of frames
1557  ( ((frame % fps) % 10) << 24) | // units of frames
1558  (0 << 23) | // field phase (NTSC), b0 (PAL)
1559  ((((frame / fps) % 60) / 10) << 20) | // tens of seconds
1560  ((((frame / fps) % 60) % 10) << 16) | // units of seconds
1561  (0 << 15) | // b0 (NTSC), b2 (PAL)
1562  ((((frame / (fps * 60)) % 60) / 10) << 12) | // tens of minutes
1563  ((((frame / (fps * 60)) % 60) % 10) << 8) | // units of minutes
1564  (0 << 7) | // b1
1565  (0 << 6) | // b2 (NTSC), field phase (PAL)
1566  ((((frame / (fps * 3600) % 24)) / 10) << 4) | // tens of hours
1567  ( (frame / (fps * 3600) % 24)) % 10; // units of hours
1568 }
1569 
1571 {
1572  MXFContext *mxf = s->priv_data;
1573  AVIOContext *pb = s->pb;
1574  unsigned frame;
1575  uint32_t time_code;
1576 
1577  frame = mxf->timecode_start + mxf->last_indexed_edit_unit + mxf->edit_units_count;
1578 
1579  // write system metadata pack
1581  klv_encode_ber4_length(pb, 57);
1582  avio_w8(pb, 0x5c); // UL, user date/time stamp, picture and sound item present
1583  avio_w8(pb, 0x04); // content package rate
1584  avio_w8(pb, 0x00); // content package type
1585  avio_wb16(pb, 0x00); // channel handle
1586  avio_wb16(pb, frame); // continuity count
1587  if (mxf->essence_container_count > 1)
1588  avio_write(pb, multiple_desc_ul, 16);
1589  else {
1590  MXFStreamContext *sc = s->streams[0]->priv_data;
1591  avio_write(pb, mxf_essence_container_uls[sc->index].container_ul, 16);
1592  }
1593  avio_w8(pb, 0);
1594  avio_wb64(pb, 0);
1595  avio_wb64(pb, 0); // creation date/time stamp
1596 
1597  avio_w8(pb, 0x81); // SMPTE 12M time code
1598  time_code = framenum_to_12m_time_code(frame, mxf->timecode_drop_frame,
1599  mxf->timecode_base);
1600  avio_wb32(pb, time_code);
1601  avio_wb32(pb, 0); // binary group data
1602  avio_wb64(pb, 0);
1603 
1604  // write system metadata package set
1606  klv_encode_ber4_length(pb, 35);
1607  avio_w8(pb, 0x83); // UMID
1608  avio_wb16(pb, 0x20);
1609  mxf_write_umid(s, 1);
1610 }
1611 
1613 {
1614  MXFContext *mxf = s->priv_data;
1615  AVIOContext *pb = s->pb;
1616  int packet_size = (uint64_t)st->codec->bit_rate*mxf->time_base.num /
1617  (8*mxf->time_base.den); // frame size
1618  int pad;
1619 
1620  packet_size += 16 + 4;
1621  packet_size += klv_fill_size(packet_size);
1622 
1623  klv_encode_ber4_length(pb, pkt->size);
1624  avio_write(pb, pkt->data, pkt->size);
1625 
1626  // ensure CBR muxing by padding to correct video frame size
1627  pad = packet_size - pkt->size - 16 - 4;
1628  if (pad > 20) {
1629  avio_write(s->pb, klv_fill_key, 16);
1630  pad -= 16 + 4;
1631  klv_encode_ber4_length(s->pb, pad);
1632  for (; pad; pad--)
1633  avio_w8(s->pb, 0);
1634  assert(!(avio_tell(s->pb) & (KAG_SIZE-1)));
1635  } else {
1636  av_log(s, AV_LOG_WARNING, "cannot fill d-10 video packet\n");
1637  for (; pad > 0; pad--)
1638  avio_w8(s->pb, 0);
1639  }
1640 }
1641 
1643 {
1644  MXFContext *mxf = s->priv_data;
1645  AVIOContext *pb = s->pb;
1646  int frame_size = pkt->size / st->codec->block_align;
1647  uint8_t *samples = pkt->data;
1648  uint8_t *end = pkt->data + pkt->size;
1649  int i;
1650 
1651  klv_encode_ber4_length(pb, 4 + frame_size*4*8);
1652 
1653  avio_w8(pb, (frame_size == 1920 ? 0 : (mxf->edit_units_count-1) % 5 + 1));
1654  avio_wl16(pb, frame_size);
1655  avio_w8(pb, (1<<st->codec->channels)-1);
1656 
1657  while (samples < end) {
1658  for (i = 0; i < st->codec->channels; i++) {
1659  uint32_t sample;
1660  if (st->codec->codec_id == AV_CODEC_ID_PCM_S24LE) {
1661  sample = AV_RL24(samples)<< 4;
1662  samples += 3;
1663  } else {
1664  sample = AV_RL16(samples)<<12;
1665  samples += 2;
1666  }
1667  avio_wl32(pb, sample | i);
1668  }
1669  for (; i < 8; i++)
1670  avio_wl32(pb, i);
1671  }
1672 }
1673 
1675 {
1676  MXFContext *mxf = s->priv_data;
1677  AVIOContext *pb = s->pb;
1678  AVStream *st = s->streams[pkt->stream_index];
1679  MXFStreamContext *sc = st->priv_data;
1680  MXFIndexEntry ie = {0};
1681  int err;
1682 
1684  if ((err = av_reallocp_array(&mxf->index_entries, mxf->edit_units_count
1685  + EDIT_UNITS_PER_BODY, sizeof(*mxf->index_entries))) < 0) {
1686  mxf->edit_units_count = 0;
1687  av_log(s, AV_LOG_ERROR, "could not allocate index entries\n");
1688  return err;
1689  }
1690  }
1691 
1692  if (st->codec->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
1693  if (!mxf_parse_mpeg2_frame(s, st, pkt, &ie)) {
1694  av_log(s, AV_LOG_ERROR, "could not get mpeg2 profile and level\n");
1695  return -1;
1696  }
1697  }
1698 
1699  if (!mxf->header_written) {
1700  if (mxf->edit_unit_byte_count) {
1701  if ((err = mxf_write_partition(s, 1, 2, header_open_partition_key, 1)) < 0)
1702  return err;
1703  mxf_write_klv_fill(s);
1705  } else {
1706  if ((err = mxf_write_partition(s, 0, 0, header_open_partition_key, 1)) < 0)
1707  return err;
1708  }
1709  mxf->header_written = 1;
1710  }
1711 
1712  if (st->index == 0) {
1713  if (!mxf->edit_unit_byte_count &&
1715  !(ie.flags & 0x33)) { // I frame, Gop start
1716  mxf_write_klv_fill(s);
1717  if ((err = mxf_write_partition(s, 1, 2, body_partition_key, 0)) < 0)
1718  return err;
1719  mxf_write_klv_fill(s);
1721  }
1722 
1723  mxf_write_klv_fill(s);
1725 
1726  if (!mxf->edit_unit_byte_count) {
1728  mxf->index_entries[mxf->edit_units_count].flags = ie.flags;
1730  mxf->body_offset += KAG_SIZE; // size of system element
1731  }
1732  mxf->edit_units_count++;
1733  } else if (!mxf->edit_unit_byte_count && st->index == 1) {
1735  mxf->body_offset - mxf->index_entries[mxf->edit_units_count-1].offset;
1736  }
1737 
1738  mxf_write_klv_fill(s);
1739  avio_write(pb, sc->track_essence_element_key, 16); // write key
1740  if (s->oformat == &ff_mxf_d10_muxer) {
1741  if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO)
1742  mxf_write_d10_video_packet(s, st, pkt);
1743  else
1744  mxf_write_d10_audio_packet(s, st, pkt);
1745  } else {
1746  klv_encode_ber4_length(pb, pkt->size); // write length
1747  avio_write(pb, pkt->data, pkt->size);
1748  mxf->body_offset += 16+4+pkt->size + klv_fill_size(16+4+pkt->size);
1749  }
1750 
1751  avio_flush(pb);
1752 
1753  return 0;
1754 }
1755 
1757 {
1758  MXFContext *mxf = s->priv_data;
1759  AVIOContext *pb = s->pb;
1760  uint64_t pos = avio_tell(pb);
1761  int i;
1762 
1764  klv_encode_ber_length(pb, 28 + 12*mxf->body_partitions_count);
1765 
1766  if (mxf->edit_unit_byte_count)
1767  avio_wb32(pb, 1); // BodySID of header partition
1768  else
1769  avio_wb32(pb, 0);
1770  avio_wb64(pb, 0); // offset of header partition
1771 
1772  for (i = 0; i < mxf->body_partitions_count; i++) {
1773  avio_wb32(pb, 1); // BodySID
1774  avio_wb64(pb, mxf->body_partition_offset[i]);
1775  }
1776 
1777  avio_wb32(pb, 0); // BodySID of footer partition
1779 
1780  avio_wb32(pb, avio_tell(pb) - pos + 4);
1781 }
1782 
1784 {
1785  MXFContext *mxf = s->priv_data;
1786  AVIOContext *pb = s->pb;
1787  int err;
1788 
1790 
1791  mxf_write_klv_fill(s);
1793  if (mxf->edit_unit_byte_count) { // no need to repeat index
1794  if ((err = mxf_write_partition(s, 0, 0, footer_partition_key, 0)) < 0)
1795  return err;
1796  } else {
1797  if ((err = mxf_write_partition(s, 0, 2, footer_partition_key, 0)) < 0)
1798  return err;
1799  mxf_write_klv_fill(s);
1801  }
1802 
1803  mxf_write_klv_fill(s);
1805 
1806  if (s->pb->seekable) {
1807  avio_seek(pb, 0, SEEK_SET);
1808  if (mxf->edit_unit_byte_count) {
1809  if ((err = mxf_write_partition(s, 1, 2, header_closed_partition_key, 1)) < 0)
1810  return err;
1811  mxf_write_klv_fill(s);
1813  } else {
1814  if ((err = mxf_write_partition(s, 0, 0, header_closed_partition_key, 1)) < 0)
1815  return err;
1816  }
1817  }
1818 
1820 
1821  av_freep(&mxf->index_entries);
1824  av_freep(&mxf->timecode_track);
1825 
1826  mxf_free(s);
1827 
1828  return 0;
1829 }
1830 
1832 {
1833  int i, stream_count = 0;
1834 
1835  for (i = 0; i < s->nb_streams; i++)
1836  stream_count += !!s->streams[i]->last_in_packet_buffer;
1837 
1838  if (stream_count && (s->nb_streams == stream_count || flush)) {
1839  AVPacketList *pktl = s->packet_buffer;
1840  if (s->nb_streams != stream_count) {
1841  AVPacketList *last = NULL;
1842  // find last packet in edit unit
1843  while (pktl) {
1844  if (!stream_count || pktl->pkt.stream_index == 0)
1845  break;
1846  last = pktl;
1847  pktl = pktl->next;
1848  stream_count--;
1849  }
1850  // purge packet queue
1851  while (pktl) {
1852  AVPacketList *next = pktl->next;
1853 
1854  if(s->streams[pktl->pkt.stream_index]->last_in_packet_buffer == pktl)
1856  av_free_packet(&pktl->pkt);
1857  av_freep(&pktl);
1858  pktl = next;
1859  }
1860  if (last)
1861  last->next = NULL;
1862  else {
1863  s->packet_buffer = NULL;
1864  s->packet_buffer_end= NULL;
1865  goto out;
1866  }
1867  pktl = s->packet_buffer;
1868  }
1869 
1870  *out = pktl->pkt;
1871  av_dlog(s, "out st:%d dts:%"PRId64"\n", (*out).stream_index, (*out).dts);
1872  s->packet_buffer = pktl->next;
1873  if(s->streams[pktl->pkt.stream_index]->last_in_packet_buffer == pktl)
1875  if(!s->packet_buffer)
1876  s->packet_buffer_end= NULL;
1877  av_freep(&pktl);
1878  return 1;
1879  } else {
1880  out:
1881  av_init_packet(out);
1882  return 0;
1883  }
1884 }
1885 
1887 {
1889  MXFStreamContext *sc2 = s->streams[next->stream_index]->priv_data;
1890 
1891  return next->dts > pkt->dts ||
1892  (next->dts == pkt->dts && sc->order < sc2->order);
1893 }
1894 
1896 {
1897  return ff_audio_rechunk_interleave(s, out, pkt, flush,
1899 }
1900 
1902  .name = "mxf",
1903  .long_name = NULL_IF_CONFIG_SMALL("MXF (Material eXchange Format)"),
1904  .mime_type = "application/mxf",
1905  .extensions = "mxf",
1906  .priv_data_size = sizeof(MXFContext),
1907  .audio_codec = AV_CODEC_ID_PCM_S16LE,
1908  .video_codec = AV_CODEC_ID_MPEG2VIDEO,
1914 };
1915 
1916 AVOutputFormat ff_mxf_d10_muxer = {
1917  .name = "mxf_d10",
1918  .long_name = NULL_IF_CONFIG_SMALL("MXF (Material eXchange Format) D-10 Mapping"),
1919  .mime_type = "application/mxf",
1920  .priv_data_size = sizeof(MXFContext),
1921  .audio_codec = AV_CODEC_ID_PCM_S16LE,
1922  .video_codec = AV_CODEC_ID_MPEG2VIDEO,
1928 };
static uint64_t mxf_parse_timestamp(time_t timestamp)
Definition: mxfenc.c:1387
static void mxf_write_package(AVFormatContext *s, enum MXFMetadataSetType type)
Definition: mxfenc.c:926
MXFMetadataSetType
Definition: mxf.h:30
Bytestream IO Context.
Definition: avio.h:68
static void mxf_write_d10_video_packet(AVFormatContext *s, AVStream *st, AVPacket *pkt)
Definition: mxfenc.c:1612
static const uint8_t header_closed_partition_key[]
Definition: mxfenc.c:208
int size
void av_free_packet(AVPacket *pkt)
Free a packet.
Definition: avpacket.c:243
static void mxf_write_index_table_segment(AVFormatContext *s)
Definition: mxfenc.c:1039
struct AVPacketList * packet_buffer_end
Definition: avformat.h:1217
enum AVCodecID id
Definition: mxfenc.c:84
uint64_t body_offset
Definition: mxfenc.c:191
#define PRINT_KEY(pc, s, x)
Definition: mxf.h:119
static const uint8_t klv_fill_key[]
Definition: mxfenc.c:209
static const UID mxf_mpegvideo_descriptor_key
Definition: mxfenc.c:774
#define AV_RL24(x)
Definition: intreadwrite.h:420
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:129
void(* write_desc)(AVFormatContext *, AVStream *)
Definition: mxfenc.c:80
int index
index in mxf_essence_container_uls table
Definition: mxfenc.c:67
void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition: utils.c:2821
static void mxf_write_structural_component(AVFormatContext *s, AVStream *st, enum MXFMetadataSetType type)
Definition: mxfenc.c:678
static int write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: assenc.c:58
#define AV_RL16(x)
Definition: intreadwrite.h:220
unsigned body_partitions_count
Definition: mxfenc.c:183
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:718
const int * samples
current samples per frame, pointer to samples_per_frame
int num
numerator
Definition: rational.h:44
int index
stream index in AVFormatContext
Definition: avformat.h:700
int size
Definition: avcodec.h:974
Definition: mxf.h:34
static const UID mxf_cdci_descriptor_key
Definition: mxfenc.c:777
int interlaced
whether picture is interlaced
Definition: mxfenc.c:70
static const uint8_t body_partition_key[]
Definition: mxfenc.c:210
AVOutputFormat ff_mxf_muxer
Definition: mxfenc.c:1901
void av_log(void *avcl, int level, const char *fmt,...) av_printf_format(3
Send the specified message to the log if the level is less than or equal to the current av_log_level...
av_dlog(ac->avr,"%d samples - audio_convert: %s to %s (%s)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt), use_generic?ac->func_descr_generic:ac->func_descr)
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:165
int void avio_flush(AVIOContext *s)
Definition: aviobuf.c:180
static void mxf_free(AVFormatContext *s)
Definition: mxfenc.c:408
static void mxf_write_uuid(AVIOContext *pb, enum MXFMetadataSetType type, int value)
Definition: mxfenc.c:308
static void mxf_write_generic_desc(AVFormatContext *s, AVStream *st, const UID key, unsigned size)
Definition: mxfenc.c:751
int profile
profile
Definition: avcodec.h:2622
#define sample
UID track_essence_element_key
Definition: mxfenc.c:66
static void mxf_write_common_fields(AVFormatContext *s, AVStream *st)
Definition: mxfenc.c:603
int block_align
number of bytes per packet if constant and known or 0 Used by some WAV based audio codecs...
Definition: avcodec.h:1828
int id
Definition: mxf.h:67
int index
Definition: mxfenc.c:85
static int mxf_write_header(AVFormatContext *s)
Definition: mxfenc.c:1412
static void klv_encode_ber4_length(AVIOContext *pb, int len)
Definition: mxfenc.c:359
void av_freep(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:198
static const uint8_t multiple_desc_ul[]
Definition: mxfenc.c:216
Format I/O context.
Definition: avformat.h:922
uint8_t UID[16]
Definition: mxf.h:28
int temporal_reordering
Definition: mxfenc.c:71
uint8_t slice_count
index slice count minus 1 (1 if no audio, 0 otherwise)
Definition: mxfenc.c:180
static const UID mxf_generic_sound_descriptor_key
Definition: mxfenc.c:778
#define AVFMT_NOTIMESTAMPS
Format does not need / have any timestamps.
Definition: avformat.h:416
static int klv_encode_ber_length(AVIOContext *pb, uint64_t len)
Definition: mxfenc.c:338
static void mxf_write_klv_fill(AVFormatContext *s)
Definition: mxfenc.c:1172
static void mxf_write_refs_count(AVIOContext *pb, int ref_count)
Definition: mxfenc.c:324
int timecode_base
rounded time code base (25 or 30)
Definition: mxfenc.c:187
static int mxf_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: mxfenc.c:1674
AVOutputFormat ff_mxf_d10_muxer
Definition: mxfenc.c:1916
static unsigned klv_fill_size(uint64_t size)
Definition: mxfenc.c:1030
uint8_t
static void mxf_write_cdci_common(AVFormatContext *s, AVStream *st, const UID key, unsigned size)
Definition: mxfenc.c:780
struct AVPacketList * last_in_packet_buffer
last packet in packet_buffer for this stream when muxing.
Definition: avformat.h:873
static const UID mxf_mpeg2_codec_uls[]
Definition: mxfenc.c:1282
AVPacket pkt
Definition: avformat.h:1260
int ff_audio_interleave_init(AVFormatContext *s, const int *samples_per_frame, AVRational time_base)
void avio_wl32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:260
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:219
static double av_q2d(AVRational a)
Convert rational to double.
Definition: rational.h:69
int flags
Flags modifying the (de)muxer behaviour.
Definition: avformat.h:1033
uint64_t duration
Definition: mxfenc.c:185
static int flags
Definition: log.c:44
uint32_t tag
Definition: movenc.c:844
void * priv_data
Definition: avformat.h:719
uint64_t offset
Definition: mxfenc.c:59
Definition: ismindex.c:65
uint8_t flags
Definition: mxfenc.c:58
static void mxf_write_track(AVFormatContext *s, AVStream *st, enum MXFMetadataSetType type)
Definition: mxfenc.c:562
static int interleave_packet(AVFormatContext *s, AVPacket *out, AVPacket *in, int flush)
Interleave an AVPacket correctly so it can be muxed.
Definition: mux.c:522
unsigned slice_offset
offset of audio slice
Definition: mxfenc.c:60
static const uint8_t index_table_segment_key[]
Definition: mxfenc.c:205
static int write_trailer(AVFormatContext *s)
Definition: assenc.c:64
static const uint8_t frame_size[4]
Definition: g723_1_data.h:47
AudioInterleaveContext aic
Definition: mxfenc.c:65
int local_tag
Definition: mxfenc.c:53
static const uint8_t umid_ul[]
Definition: mxfenc.c:197
int av_reallocp_array(void *ptr, size_t nmemb, size_t size)
Definition: mem.c:167
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: avcodec.h:105
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:123
int has_b_frames
Size of the frame reordering buffer in the decoder.
Definition: avcodec.h:1339
int av_get_bits_per_sample(enum AVCodecID codec_id)
Return codec bits per sample.
Definition: utils.c:2043
static void mxf_write_system_item(AVFormatContext *s)
Definition: mxfenc.c:1570
#define EDIT_UNITS_PER_BODY
Definition: mxfenc.c:49
static const uint8_t header_metadata_key[]
partial key for header metadata
Definition: mxfenc.c:215
void * priv_data
Format private data.
Definition: avformat.h:950
Definition: mxf.h:41
#define AVERROR(e)
Definition: error.h:43
int ff_audio_rechunk_interleave(AVFormatContext *s, AVPacket *out, AVPacket *pkt, int flush, int(*get_packet)(AVFormatContext *, AVPacket *, AVPacket *, int), int(*compare_ts)(AVFormatContext *, AVPacket *, AVPacket *))
Rechunk audio PCM packets per AudioInterleaveContext->samples_per_frame and interleave them correctly...
#define AV_WB64(p, d)
Definition: intreadwrite.h:275
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:150
static const uint8_t header_open_partition_key[]
Definition: mxfenc.c:207
static void mxf_write_preface(AVFormatContext *s)
Definition: mxfenc.c:443
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:144
int header_written
Definition: mxfenc.c:176
static void mxf_write_random_index_pack(AVFormatContext *s)
Definition: mxfenc.c:1756
static const struct @117 mxf_essence_mappings[]
static void mxf_write_cdci_desc(AVFormatContext *s, AVStream *st)
Definition: mxfenc.c:852
static void mxf_write_metadata_key(AVIOContext *pb, unsigned int value)
Definition: mxfenc.c:402
int order
interleaving order if dts are equal
Definition: mxfenc.c:69
static void mxf_write_d10_audio_packet(AVFormatContext *s, AVStream *st, AVPacket *pkt)
Definition: mxfenc.c:1642
#define LIBAVFORMAT_VERSION
Definition: version.h:39
static const uint8_t primer_pack_key[]
Definition: mxfenc.c:204
static const uint8_t system_metadata_pack_key[]
Definition: mxfenc.c:1549
static int mxf_get_essence_container_ul_index(enum AVCodecID id)
Definition: mxfenc.c:368
static void mxf_write_content_storage(AVFormatContext *s)
Definition: mxfenc.c:537
int av_reduce(int *dst_num, int *dst_den, int64_t num, int64_t den, int64_t max)
Reduce a fraction.
Definition: rational.c:35
static int klv_ber_length(uint64_t len)
Definition: mxfenc.c:330
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:978
int seekable
A combination of AVIO_SEEKABLE_ flags or 0 when the stream is not seekable.
Definition: avio.h:117
int bit_rate
the average bitrate
Definition: avcodec.h:1114
static const UID mxf_wav_descriptor_key
Definition: mxfenc.c:775
static const int PAL_samples_per_frame[]
Definition: mxfenc.c:45
#define KAG_SIZE
Definition: mxfenc.c:50
static const uint8_t op1a_ul[]
complete key for operation pattern, partitions, and primer pack
Definition: mxfenc.c:202
int width
picture width / height.
Definition: avcodec.h:1224
int timecode_drop_frame
time code use drop frame method frop mpeg-2 essence gop header
Definition: mxfenc.c:189
uint64_t * body_partition_offset
Definition: mxfenc.c:182
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:990
int64_t ff_iso8601_to_unix_time(const char *datestr)
Convert a date string in ISO8601 format to Unix timestamp.
Definition: utils.c:2907
static int mxf_write_partition(AVFormatContext *s, int bodysid, int indexsid, const uint8_t *key, int write_metadata)
Definition: mxfenc.c:1185
char * value
Definition: dict.h:76
#define FF_ARRAY_ELEMS(a)
Definition: common.h:61
const char * name
Definition: avformat.h:446
static void mxf_write_local_tag_utf16(AVIOContext *pb, int tag, const char *value)
Definition: mxfenc.c:490
static int mxf_write_header_metadata_sets(AVFormatContext *s)
Definition: mxfenc.c:1019
int level
level
Definition: avcodec.h:2705
unsigned char * buf_ptr
Current position in the buffer.
Definition: avio.h:84
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:186
AVStream * timecode_track
Definition: mxfenc.c:186
static const UID mxf_aes3_descriptor_key
Definition: mxfenc.c:776
const MXFCodecUL ff_mxf_data_definition_uls[]
SMPTE RP224 http://www.smpte-ra.org/mdd/index.html.
Definition: mxf.c:28
static const uint8_t system_metadata_package_set_key[]
Definition: mxfenc.c:1550
int edit_unit_byte_count
fixed edit unit byte count
Definition: mxfenc.c:190
void avio_w8(AVIOContext *s, int b)
Definition: aviobuf.c:144
preferred ID for MPEG-1/2 video decoding
Definition: avcodec.h:110
if(ac->has_optimized_func)
Stream structure.
Definition: avformat.h:699
uint32_t instance_number
Definition: mxfenc.c:192
static void mxf_write_wav_common(AVFormatContext *s, AVStream *st, const UID key, unsigned size)
Definition: mxfenc.c:897
static void mxf_write_local_tag(AVIOContext *pb, int size, int tag)
Definition: mxfenc.c:396
NULL
Definition: eval.c:55
static void mxf_write_aes3_desc(AVFormatContext *s, AVStream *st)
Definition: mxfenc.c:916
int last_indexed_edit_unit
Definition: mxfenc.c:181
static int mxf_parse_mpeg2_frame(AVFormatContext *s, AVStream *st, AVPacket *pkt, MXFIndexEntry *e)
Definition: mxfenc.c:1315
enum AVMediaType codec_type
Definition: avcodec.h:1058
version
Definition: ffv1enc.c:1080
const UID * codec_ul
Definition: mxfenc.c:68
enum AVCodecID codec_id
Definition: avcodec.h:1067
int sample_rate
samples per second
Definition: avcodec.h:1791
static const UID * mxf_get_mpeg2_codec_ul(AVCodecContext *avctx)
Definition: mxfenc.c:1295
main external API structure.
Definition: avcodec.h:1050
static void(WINAPI *cond_broadcast)(pthread_cond_t *cond)
static void mxf_write_essence_container_refs(AVFormatContext *s)
Definition: mxfenc.c:429
static unsigned int seed
Definition: videogen.c:78
void avio_wb64(AVIOContext *s, uint64_t val)
Definition: aviobuf.c:330
AVIOContext * pb
I/O context.
Definition: avformat.h:964
int closed_gop
gop is closed, used in mpeg-2 frame parsing
Definition: mxfenc.c:73
uint64_t timestamp
timestamp, as year(16),month(8),day(8),hour(8),minutes(8),msec/4(8)
Definition: mxfenc.c:179
#define AV_STRINGIFY(s)
Definition: macros.h:36
AVRational aspect_ratio
display aspect ratio
Definition: mxfenc.c:72
static const MXFLocalTagPair mxf_local_tag_batch[]
SMPTE RP210 http://www.smpte-ra.org/mdd/index.html.
Definition: mxfenc.c:221
int timecode_start
frame number computed from mpeg-2 gop header timecode
Definition: mxfenc.c:188
rational number numerator/denominator
Definition: rational.h:43
uint8_t * data
Definition: avcodec.h:973
int sample_size
size of one sample all channels included
uint16_t temporal_ref
Definition: mxfenc.c:61
AVDictionary * metadata
Metadata that applies to the whole file.
Definition: avformat.h:1130
static void mxf_write_primer_pack(AVFormatContext *s)
Definition: mxfenc.c:377
#define AVFMT_FLAG_BITEXACT
When muxing, try to avoid writing any random/volatile data to the output.
Definition: avformat.h:1050
void avio_wb32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:268
void avio_wb16(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:342
MXFIndexEntry * index_entries
Definition: mxfenc.c:177
AVRational time_base
Definition: mxfenc.c:175
Definition: mxf.h:64
static void mxf_write_timecode_component(AVFormatContext *s, AVStream *st, enum MXFMetadataSetType type)
Definition: mxfenc.c:650
static const uint8_t uuid_base[]
Definition: mxfenc.c:196
Definition: mxf.h:47
static const MXFContainerEssenceEntry mxf_essence_container_uls[]
Definition: mxfenc.c:99
int gop_size
the number of pictures in a group of pictures, or 0 for intra_only
Definition: avcodec.h:1245
static int mxf_interleave_get_packet(AVFormatContext *s, AVPacket *out, AVPacket *pkt, int flush)
Definition: mxfenc.c:1831
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31))))#defineSET_CONV_FUNC_GROUP(ofmt, ifmt) staticvoidset_generic_function(AudioConvert *ac){}voidff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, intsample_rate, intapply_map){AudioConvert *ac;intin_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) returnNULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method!=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt)>2){ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc){av_free(ac);returnNULL;}returnac;}in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}elseif(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;elseac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);returnac;}intff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){intuse_generic=1;intlen=in->nb_samples;intp;if(ac->dc){av_dlog(ac->avr,"%dsamples-audio_convert:%sto%s(dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));returnff_convert_dither(ac-> out
int64_t footer_partition_offset
Definition: mxfenc.c:173
static const uint8_t smpte_12m_timecode_track_data_ul[]
Definition: mxfenc.c:601
static void mxf_write_identification(AVFormatContext *s)
Definition: mxfenc.c:498
void ff_audio_interleave_close(AVFormatContext *s)
Definition: mxf.h:36
static int mxf_compare_timestamps(AVFormatContext *s, AVPacket *next, AVPacket *pkt)
Definition: mxfenc.c:1886
static const uint8_t footer_partition_key[]
Definition: mxfenc.c:203
static int mxf_write_essence_container_data(AVFormatContext *s)
Definition: mxfenc.c:997
static av_cold void flush(AVCodecContext *avctx)
Flush (reset) the frame ID after seeking.
Definition: alsdec.c:1771
Main libavformat public API header.
AVDictionaryEntry * av_dict_get(const AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags)
Get a dictionary entry with matching key.
Definition: dict.c:38
void av_init_packet(AVPacket *pkt)
Initialize optional fields of a packet with default values.
Definition: avpacket.c:47
int den
denominator
Definition: rational.h:45
static const MXFCodecUL * mxf_get_data_definition_ul(int type)
Definition: mxfenc.c:418
struct AVPacketList * packet_buffer
This buffer is only needed when packets were already buffered but not decoded, for example to get the...
Definition: avformat.h:1216
#define AV_RB32(x)
Definition: intreadwrite.h:232
struct AVOutputFormat * oformat
The output container format.
Definition: avformat.h:941
int last_key_index
index of last key frame
Definition: mxfenc.c:184
int len
int channels
number of audio channels
Definition: avcodec.h:1792
#define av_log2
Definition: intmath.h:85
static void mxf_write_generic_sound_common(AVFormatContext *s, AVStream *st, const UID key, unsigned size)
Definition: mxfenc.c:875
void avio_wb24(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:354
static uint32_t framenum_to_12m_time_code(unsigned frame, int drop, int fps)
Definition: mxfenc.c:1552
static void mxf_gen_umid(AVFormatContext *s)
Definition: mxfenc.c:1400
static void write_header(FFV1Context *f)
Definition: ffv1enc.c:380
uint8_t umid[16]
unique material identifier
Definition: mxfenc.c:193
static void mxf_write_multi_descriptor(AVFormatContext *s)
Definition: mxfenc.c:714
static void mxf_write_wav_desc(AVFormatContext *s, AVStream *st)
Definition: mxfenc.c:911
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
Definition: avcodec.h:972
unsigned edit_units_count
Definition: mxfenc.c:178
uint32_t av_get_random_seed(void)
Get random data.
Definition: random_seed.c:95
static void mxf_write_sequence(AVFormatContext *s, AVStream *st, enum MXFMetadataSetType type)
Definition: mxfenc.c:622
UID uid
Definition: mxf.h:65
int stream_index
Definition: avcodec.h:975
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avformat.h:741
static void mxf_write_mpegvideo_desc(AVFormatContext *s, AVStream *st)
Definition: mxfenc.c:857
static void mxf_write_umid(AVFormatContext *s, int type)
Definition: mxfenc.c:315
This structure stores compressed data.
Definition: avcodec.h:950
int essence_container_count
Definition: mxfenc.c:174
static const uint8_t random_index_pack_key[]
Definition: mxfenc.c:206
static void mxf_write_generic_sound_desc(AVFormatContext *s, AVStream *st)
Definition: mxfenc.c:921
static int mxf_interleave(AVFormatContext *s, AVPacket *out, AVPacket *pkt, int flush)
Definition: mxfenc.c:1895
static const int NTSC_samples_per_frame[]
Definition: mxfenc.c:44
void avio_wl16(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:336
void * av_mallocz(size_t size) av_malloc_attrib 1(1)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:205
static int mxf_write_footer(AVFormatContext *s)
Definition: mxfenc.c:1783
struct AVPacketList * next
Definition: avformat.h:1261