extract audio interleaving code from mxf muxer, will be used by gxf and dv
[ffmpeg.git] / libavformat / mxfenc.c
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 //#define DEBUG
34
35 #include <math.h>
36 #include <time.h>
37
38 #include "libavutil/fifo.h"
39 #include "audiointerleave.h"
40 #include "mxf.h"
41
42 static const int NTSC_samples_per_frame[] = { 1602, 1601, 1602, 1601, 1602, 0 };
43 static const int PAL_samples_per_frame[]  = { 1920, 0 };
44
45 #define MXF_INDEX_CLUSTER_SIZE 4096
46 #define KAG_SIZE 512
47
48 typedef struct {
49     int local_tag;
50     UID uid;
51 } MXFLocalTagPair;
52
53 typedef struct {
54     uint8_t flags;
55     uint64_t offset;
56     unsigned slice_offset; // offset of audio slice
57 } MXFIndexEntry;
58
59 typedef struct {
60     AudioInterleaveContext aic;
61     UID track_essence_element_key;
62     int index;            ///< index in mxf_essence_container_uls table
63     const UID *codec_ul;
64     int64_t duration;
65     int order;            ///< interleaving order if dts are equal
66     int interlaced;       ///< wether picture is interlaced
67     int temporal_reordering;
68 } MXFStreamContext;
69
70 typedef struct {
71     UID container_ul;
72     UID element_ul;
73     UID codec_ul;
74     void (*write_desc)();
75 } MXFContainerEssenceEntry;
76
77 static const struct {
78     enum CodecID id;
79     int index;
80 } mxf_essence_mappings[] = {
81     { CODEC_ID_MPEG2VIDEO, 0 },
82     { CODEC_ID_PCM_S24LE,  1 },
83     { CODEC_ID_PCM_S16LE,  1 },
84     { 0 }
85 };
86
87 static void mxf_write_wav_desc(AVFormatContext *s, AVStream *st);
88 static void mxf_write_aes3_desc(AVFormatContext *s, AVStream *st);
89 static void mxf_write_mpegvideo_desc(AVFormatContext *s, AVStream *st);
90
91 static const MXFContainerEssenceEntry mxf_essence_container_uls[] = {
92     { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x02,0x0D,0x01,0x03,0x01,0x02,0x04,0x60,0x01 },
93       { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x15,0x01,0x05,0x00 },
94       { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x00,0x00,0x00 },
95       mxf_write_mpegvideo_desc },
96     { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x06,0x03,0x00 },
97       { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x16,0x01,0x03,0x00 },
98       { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 },
99       mxf_write_aes3_desc },
100     { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x06,0x01,0x00 },
101       { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x16,0x01,0x01,0x00 },
102       { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 },
103       mxf_write_wav_desc },
104     { { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 },
105       { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 },
106       { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 },
107       NULL },
108 };
109
110 typedef struct MXFContext {
111     int64_t footer_partition_offset;
112     int essence_container_count;
113     uint8_t essence_containers_indices[FF_ARRAY_ELEMS(mxf_essence_container_uls)];
114     AVRational time_base;
115     int header_written;
116     MXFIndexEntry *index_entries;
117     unsigned edit_units_count;
118     uint64_t timestamp;   ///< timestamp, as year(16),month(8),day(8),hour(8),minutes(8),msec/4(8)
119     uint8_t slice_count;  /// index slice count minus 1 (1 if no audio, 0 otherwise)
120 } MXFContext;
121
122 static const uint8_t uuid_base[]            = { 0xAD,0xAB,0x44,0x24,0x2f,0x25,0x4d,0xc7,0x92,0xff,0x29,0xbd };
123 static const uint8_t umid_base[]            = { 0x06,0x0A,0x2B,0x34,0x01,0x01,0x01,0x05,0x01,0x01,0x0D,0x00,0x13,0x00,0x00,0x00 };
124
125 /**
126  * complete key for operation pattern, partitions, and primer pack
127  */
128 static const uint8_t op1a_ul[]                     = { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x02,0x01,0x01,0x01,0x01,0x00 };
129 static const uint8_t footer_partition_key[]        = { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0D,0x01,0x02,0x01,0x01,0x04,0x04,0x00 }; // ClosedComplete
130 static const uint8_t primer_pack_key[]             = { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0D,0x01,0x02,0x01,0x01,0x05,0x01,0x00 };
131 static const uint8_t index_table_segment_key[]     = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x10,0x01,0x00 };
132 static const uint8_t random_index_pack_key[]       = { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0D,0x01,0x02,0x01,0x01,0x11,0x01,0x00 };
133 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
134 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
135 static const uint8_t klv_fill_key[]                = { 0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x03,0x01,0x02,0x10,0x01,0x00,0x00,0x00 };
136
137 /**
138  * partial key for header metadata
139  */
140 static const uint8_t header_metadata_key[]  = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0D,0x01,0x01,0x01,0x01 };
141 static const uint8_t multiple_desc_ul[]     = { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x0D,0x01,0x03,0x01,0x02,0x7F,0x01,0x00 };
142
143 /**
144  * SMPTE RP210 http://www.smpte-ra.org/mdd/index.html
145  */
146 static const MXFLocalTagPair mxf_local_tag_batch[] = {
147     // preface set
148     { 0x3C0A, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x01,0x01,0x15,0x02,0x00,0x00,0x00,0x00}}, /* Instance UID */
149     { 0x3B02, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x10,0x02,0x04,0x00,0x00}}, /* Last Modified Date */
150     { 0x3B05, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x03,0x01,0x02,0x01,0x05,0x00,0x00,0x00}}, /* Version */
151     { 0x3B06, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x06,0x04,0x00,0x00}}, /* Identifications reference */
152     { 0x3B03, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x02,0x01,0x00,0x00}}, /* Content Storage reference */
153     { 0x3B09, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x01,0x02,0x02,0x03,0x00,0x00,0x00,0x00}}, /* Operational Pattern UL */
154     { 0x3B0A, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x01,0x02,0x02,0x10,0x02,0x01,0x00,0x00}}, /* Essence Containers UL batch */
155     { 0x3B0B, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x01,0x02,0x02,0x10,0x02,0x02,0x00,0x00}}, /* DM Schemes UL batch */
156     // Identification
157     { 0x3C09, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x20,0x07,0x01,0x01,0x00,0x00,0x00}}, /* This Generation UID */
158     { 0x3C01, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x20,0x07,0x01,0x02,0x01,0x00,0x00}}, /* Company Name */
159     { 0x3C02, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x20,0x07,0x01,0x03,0x01,0x00,0x00}}, /* Product Name */
160     { 0x3C04, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x20,0x07,0x01,0x05,0x01,0x00,0x00}}, /* Version String */
161     { 0x3C05, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x20,0x07,0x01,0x07,0x00,0x00,0x00}}, /* Product ID */
162     { 0x3C06, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x10,0x02,0x03,0x00,0x00}}, /* Modification Date */
163     // Content Storage
164     { 0x1901, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x05,0x01,0x00,0x00}}, /* Package strong reference batch */
165     { 0x1902, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x05,0x02,0x00,0x00}}, /* Package strong reference batch */
166     // Essence Container Data
167     { 0x2701, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x06,0x01,0x00,0x00,0x00}}, /* Linked Package UID */
168     { 0x3F07, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x01,0x03,0x04,0x04,0x00,0x00,0x00,0x00}}, /* BodySID */
169     // Package
170     { 0x4401, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x01,0x01,0x15,0x10,0x00,0x00,0x00,0x00}}, /* Package UID */
171     { 0x4405, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x10,0x01,0x03,0x00,0x00}}, /* Package Creation Date */
172     { 0x4404, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x10,0x02,0x05,0x00,0x00}}, /* Package Modified Date */
173     { 0x4403, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x06,0x05,0x00,0x00}}, /* Tracks Strong reference array */
174     { 0x4701, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x02,0x03,0x00,0x00}}, /* Descriptor */
175     // Track
176     { 0x4801, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x01,0x07,0x01,0x01,0x00,0x00,0x00,0x00}}, /* Track ID */
177     { 0x4804, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x01,0x04,0x01,0x03,0x00,0x00,0x00,0x00}}, /* Track Number */
178     { 0x4B01, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x30,0x04,0x05,0x00,0x00,0x00,0x00}}, /* Edit Rate */
179     { 0x4B02, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x03,0x01,0x03,0x00,0x00}}, /* Origin */
180     { 0x4803, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x02,0x04,0x00,0x00}}, /* Sequence reference */
181     // Sequence
182     { 0x0201, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x07,0x01,0x00,0x00,0x00,0x00,0x00}}, /* Data Definition UL */
183     { 0x0202, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x02,0x01,0x01,0x03,0x00,0x00}}, /* Duration */
184     { 0x1001, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x06,0x09,0x00,0x00}}, /* Structural Components reference array */
185     // Source Clip
186     { 0x1201, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x07,0x02,0x01,0x03,0x01,0x04,0x00,0x00}}, /* Start position */
187     { 0x1101, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x03,0x01,0x00,0x00,0x00}}, /* SourcePackageID */
188     { 0x1102, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x03,0x02,0x00,0x00,0x00}}, /* SourceTrackID */
189     // File Descriptor
190     { 0x3F01, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x06,0x01,0x01,0x04,0x06,0x0B,0x00,0x00}}, /* Sub Descriptors reference array */
191     { 0x3006, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x06,0x01,0x01,0x03,0x05,0x00,0x00,0x00}}, /* Linked Track ID */
192     { 0x3001, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x06,0x01,0x01,0x00,0x00,0x00,0x00}}, /* SampleRate */
193     { 0x3004, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x01,0x02,0x00,0x00}}, /* Essence Container */
194     // Generic Picture Essence Descriptor
195     { 0x320C, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x03,0x01,0x04,0x00,0x00,0x00}}, /* Frame Layout */
196     { 0x320D, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x03,0x02,0x05,0x00,0x00,0x00}}, /* Video Line Map */
197     { 0x3203, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x02,0x02,0x00,0x00,0x00}}, /* Stored Width */
198     { 0x3202, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x02,0x01,0x00,0x00,0x00}}, /* Stored Height */
199     { 0x3209, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x01,0x0C,0x00,0x00,0x00}}, /* Display Width */
200     { 0x3208, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x01,0x0B,0x00,0x00,0x00}}, /* Display Height */
201     { 0x320E, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x01,0x01,0x01,0x00,0x00,0x00}}, /* Aspect Ratio */
202     { 0x3201, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x01,0x06,0x01,0x00,0x00,0x00,0x00}}, /* Picture Essence Coding */
203     // Generic Sound Essence Descriptor
204     { 0x3D02, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x02,0x03,0x01,0x04,0x00,0x00,0x00}}, /* Locked/Unlocked */
205     { 0x3D03, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x02,0x03,0x01,0x01,0x01,0x00,0x00}}, /* Audio sampling rate */
206     { 0x3D07, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x02,0x01,0x01,0x04,0x00,0x00,0x00}}, /* ChannelCount */
207     { 0x3D01, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x04,0x02,0x03,0x03,0x04,0x00,0x00,0x00}}, /* Quantization bits */
208     { 0x3D06, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x02,0x04,0x02,0x00,0x00,0x00,0x00}}, /* Sound Essence Compression */
209     // Index Table Segment
210     { 0x3F0B, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x05,0x30,0x04,0x06,0x00,0x00,0x00,0x00}}, /* Index Edit Rate */
211     { 0x3F0C, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x07,0x02,0x01,0x03,0x01,0x0A,0x00,0x00}}, /* Index Start Position */
212     { 0x3F0D, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x07,0x02,0x02,0x01,0x01,0x02,0x00,0x00}}, /* Index Duration */
213     { 0x3F05, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x06,0x02,0x01,0x00,0x00,0x00,0x00,0x00}}, /* Edit Unit Byte Count */
214     { 0x3F06, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x01,0x03,0x04,0x05,0x00,0x00,0x00,0x00}}, /* IndexSID */
215     { 0x3F08, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x04,0x04,0x04,0x01,0x01,0x00,0x00,0x00}}, /* Slice Count */
216     { 0x3F09, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x04,0x04,0x01,0x06,0x00,0x00,0x00}}, /* Delta Entry Array */
217     { 0x3F0A, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x04,0x04,0x02,0x05,0x00,0x00,0x00}}, /* Index Entry Array */
218     // MPEG video Descriptor
219     { 0x8000, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x01,0x06,0x02,0x01,0x0B,0x00,0x00}}, /* BitRate */
220     // Wave Audio Essence Descriptor
221     { 0x3D09, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x02,0x03,0x03,0x05,0x00,0x00,0x00}}, /* Average Bytes Per Second */
222     { 0x3D0A, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x02,0x03,0x02,0x01,0x00,0x00,0x00}}, /* Block Align */
223 };
224
225 static void mxf_write_uuid(ByteIOContext *pb, enum MXFMetadataSetType type, int value)
226 {
227     put_buffer(pb, uuid_base, 12);
228     put_be16(pb, type);
229     put_be16(pb, value);
230 }
231
232 static void mxf_write_umid(ByteIOContext *pb, enum MXFMetadataSetType type, int value)
233 {
234     put_buffer(pb, umid_base, 16);
235     mxf_write_uuid(pb, type, value);
236 }
237
238 static void mxf_write_refs_count(ByteIOContext *pb, int ref_count)
239 {
240     put_be32(pb, ref_count);
241     put_be32(pb, 16);
242 }
243
244 static int klv_ber_length(uint64_t len)
245 {
246     if (len < 128)
247         return 1;
248     else
249         return (av_log2(len) >> 3) + 2;
250 }
251
252 static int klv_encode_ber_length(ByteIOContext *pb, uint64_t len)
253 {
254     // Determine the best BER size
255     int size;
256     if (len < 128) {
257         //short form
258         put_byte(pb, len);
259         return 1;
260     }
261
262     size = (av_log2(len) >> 3) + 1;
263
264     // long form
265     put_byte(pb, 0x80 + size);
266     while(size) {
267         size--;
268         put_byte(pb, len >> 8 * size & 0xff);
269     }
270     return 0;
271 }
272
273 /*
274  * Get essence container ul index
275  */
276 static int mxf_get_essence_container_ul_index(enum CodecID id)
277 {
278     int i;
279     for (i = 0; mxf_essence_mappings[i].id; i++)
280         if (mxf_essence_mappings[i].id == id)
281             return mxf_essence_mappings[i].index;
282     return -1;
283 }
284
285 static void mxf_write_primer_pack(AVFormatContext *s)
286 {
287     ByteIOContext *pb = s->pb;
288     int local_tag_number, i = 0;
289
290     local_tag_number = FF_ARRAY_ELEMS(mxf_local_tag_batch);
291
292     put_buffer(pb, primer_pack_key, 16);
293     klv_encode_ber_length(pb, local_tag_number * 18 + 8);
294
295     put_be32(pb, local_tag_number); // local_tag num
296     put_be32(pb, 18); // item size, always 18 according to the specs
297
298     for (i = 0; i < local_tag_number; i++) {
299         put_be16(pb, mxf_local_tag_batch[i].local_tag);
300         put_buffer(pb, mxf_local_tag_batch[i].uid, 16);
301     }
302 }
303
304 static void mxf_write_local_tag(ByteIOContext *pb, int size, int tag)
305 {
306     put_be16(pb, tag);
307     put_be16(pb, size);
308 }
309
310 static void mxf_write_metadata_key(ByteIOContext *pb, unsigned int value)
311 {
312     put_buffer(pb, header_metadata_key, 13);
313     put_be24(pb, value);
314 }
315
316 static void mxf_free(AVFormatContext *s)
317 {
318     int i;
319
320     for (i = 0; i < s->nb_streams; i++) {
321         AVStream *st = s->streams[i];
322         av_freep(&st->priv_data);
323     }
324 }
325
326 static const MXFDataDefinitionUL *mxf_get_data_definition_ul(enum CodecType type)
327 {
328     const MXFDataDefinitionUL *uls = ff_mxf_data_definition_uls;
329     while (uls->type != CODEC_TYPE_DATA) {
330         if (type == uls->type)
331             break;
332         uls++;
333     }
334     return uls;
335 }
336
337 static void mxf_write_essence_container_refs(AVFormatContext *s)
338 {
339     MXFContext *c = s->priv_data;
340     ByteIOContext *pb = s->pb;
341     int i;
342
343     mxf_write_refs_count(pb, c->essence_container_count);
344     av_log(s,AV_LOG_DEBUG, "essence container count:%d\n", c->essence_container_count);
345     for (i = 0; i < c->essence_container_count; i++) {
346         put_buffer(pb, mxf_essence_container_uls[c->essence_containers_indices[i]].container_ul, 16);
347         PRINT_KEY(s, "essence container ul:\n", mxf_essence_container_uls[c->essence_containers_indices[i]].container_ul);
348     }
349 }
350
351 static void mxf_write_preface(AVFormatContext *s)
352 {
353     MXFContext *mxf = s->priv_data;
354     ByteIOContext *pb = s->pb;
355
356     mxf_write_metadata_key(pb, 0x012f00);
357     PRINT_KEY(s, "preface key", pb->buf_ptr - 16);
358     klv_encode_ber_length(pb, 130 + 16 * mxf->essence_container_count);
359
360     // write preface set uid
361     mxf_write_local_tag(pb, 16, 0x3C0A);
362     mxf_write_uuid(pb, Preface, 0);
363     PRINT_KEY(s, "preface uid", pb->buf_ptr - 16);
364
365     // last modified date
366     mxf_write_local_tag(pb, 8, 0x3B02);
367     put_be64(pb, mxf->timestamp);
368
369     // write version
370     mxf_write_local_tag(pb, 2, 0x3B05);
371     put_be16(pb, 1);
372
373     // write identification_refs
374     mxf_write_local_tag(pb, 16 + 8, 0x3B06);
375     mxf_write_refs_count(pb, 1);
376     mxf_write_uuid(pb, Identification, 0);
377
378     // write content_storage_refs
379     mxf_write_local_tag(pb, 16, 0x3B03);
380     mxf_write_uuid(pb, ContentStorage, 0);
381
382     mxf_write_local_tag(pb, 16, 0x3B09);
383     put_buffer(pb, op1a_ul, 16);
384
385     // write essence_container_refs
386     mxf_write_local_tag(pb, 8 + 16 * mxf->essence_container_count, 0x3B0A);
387     mxf_write_essence_container_refs(s);
388
389     // write dm_scheme_refs
390     mxf_write_local_tag(pb, 8, 0x3B0B);
391     put_be64(pb, 0);
392 }
393
394 /*
395  * Write a local tag containing an ascii string as utf-16
396  */
397 static void mxf_write_local_tag_utf16(ByteIOContext *pb, int tag, const char *value)
398 {
399     int i, size = strlen(value);
400     mxf_write_local_tag(pb, size*2, tag);
401     for (i = 0; i < size; i++)
402         put_be16(pb, value[i]);
403 }
404
405 static void mxf_write_identification(AVFormatContext *s)
406 {
407     MXFContext *mxf = s->priv_data;
408     ByteIOContext *pb = s->pb;
409     const char *company = "FFmpeg";
410     const char *product = "OP1a Muxer";
411     const char *version;
412     int length;
413
414     mxf_write_metadata_key(pb, 0x013000);
415     PRINT_KEY(s, "identification key", pb->buf_ptr - 16);
416
417     version = s->streams[0]->codec->flags & CODEC_FLAG_BITEXACT ?
418         "0.0.0" : AV_STRINGIFY(LIBAVFORMAT_VERSION);
419     length = 84 + (strlen(company)+strlen(product)+strlen(version))*2; // utf-16
420     klv_encode_ber_length(pb, length);
421
422     // write uid
423     mxf_write_local_tag(pb, 16, 0x3C0A);
424     mxf_write_uuid(pb, Identification, 0);
425     PRINT_KEY(s, "identification uid", pb->buf_ptr - 16);
426
427     // write generation uid
428     mxf_write_local_tag(pb, 16, 0x3C09);
429     mxf_write_uuid(pb, Identification, 1);
430
431     mxf_write_local_tag_utf16(pb, 0x3C01, company); // Company Name
432     mxf_write_local_tag_utf16(pb, 0x3C02, product); // Product Name
433     mxf_write_local_tag_utf16(pb, 0x3C04, version); // Version String
434
435     // write product uid
436     mxf_write_local_tag(pb, 16, 0x3C05);
437     mxf_write_uuid(pb, Identification, 2);
438
439     // modification date
440     mxf_write_local_tag(pb, 8, 0x3C06);
441     put_be64(pb, mxf->timestamp);
442 }
443
444 static void mxf_write_content_storage(AVFormatContext *s)
445 {
446     ByteIOContext *pb = s->pb;
447
448     mxf_write_metadata_key(pb, 0x011800);
449     PRINT_KEY(s, "content storage key", pb->buf_ptr - 16);
450     klv_encode_ber_length(pb, 92);
451
452     // write uid
453     mxf_write_local_tag(pb, 16, 0x3C0A);
454     mxf_write_uuid(pb, ContentStorage, 0);
455     PRINT_KEY(s, "content storage uid", pb->buf_ptr - 16);
456
457     // write package reference
458     mxf_write_local_tag(pb, 16 * 2 + 8, 0x1901);
459     mxf_write_refs_count(pb, 2);
460     mxf_write_uuid(pb, MaterialPackage, 0);
461     mxf_write_uuid(pb, SourcePackage, 0);
462
463     // write essence container data
464     mxf_write_local_tag(pb, 8 + 16, 0x1902);
465     mxf_write_refs_count(pb, 1);
466     mxf_write_uuid(pb, EssenceContainerData, 0);
467 }
468
469 static void mxf_write_track(AVFormatContext *s, AVStream *st, enum MXFMetadataSetType type)
470 {
471     MXFContext *mxf = s->priv_data;
472     ByteIOContext *pb = s->pb;
473     MXFStreamContext *sc = st->priv_data;
474
475     mxf_write_metadata_key(pb, 0x013b00);
476     PRINT_KEY(s, "track key", pb->buf_ptr - 16);
477     klv_encode_ber_length(pb, 80);
478
479     // write track uid
480     mxf_write_local_tag(pb, 16, 0x3C0A);
481     mxf_write_uuid(pb, type == MaterialPackage ? Track : Track + TypeBottom, st->index);
482     PRINT_KEY(s, "track uid", pb->buf_ptr - 16);
483
484     // write track id
485     mxf_write_local_tag(pb, 4, 0x4801);
486     put_be32(pb, st->index);
487
488     // write track number
489     mxf_write_local_tag(pb, 4, 0x4804);
490     if (type == MaterialPackage)
491         put_be32(pb, 0); // track number of material package is 0
492     else
493         put_buffer(pb, sc->track_essence_element_key + 12, 4);
494
495     mxf_write_local_tag(pb, 8, 0x4B01);
496     put_be32(pb, mxf->time_base.den);
497     put_be32(pb, mxf->time_base.num);
498
499     // write origin
500     mxf_write_local_tag(pb, 8, 0x4B02);
501     put_be64(pb, 0);
502
503     // write sequence refs
504     mxf_write_local_tag(pb, 16, 0x4803);
505     mxf_write_uuid(pb, type == MaterialPackage ? Sequence: Sequence + TypeBottom, st->index);
506 }
507
508 static void mxf_write_common_fields(ByteIOContext *pb, AVStream *st)
509 {
510     const MXFDataDefinitionUL *data_def_ul = mxf_get_data_definition_ul(st->codec->codec_type);
511     MXFStreamContext *sc = st->priv_data;
512
513     // find data define uls
514     mxf_write_local_tag(pb, 16, 0x0201);
515     put_buffer(pb, data_def_ul->uid, 16);
516
517     // write duration
518     mxf_write_local_tag(pb, 8, 0x0202);
519     put_be64(pb, sc->duration);
520 }
521
522 static void mxf_write_sequence(AVFormatContext *s, AVStream *st, enum MXFMetadataSetType type)
523 {
524     ByteIOContext *pb = s->pb;
525
526     mxf_write_metadata_key(pb, 0x010f00);
527     PRINT_KEY(s, "sequence key", pb->buf_ptr - 16);
528     klv_encode_ber_length(pb, 80);
529
530     mxf_write_local_tag(pb, 16, 0x3C0A);
531     mxf_write_uuid(pb, type == MaterialPackage ? Sequence: Sequence + TypeBottom, st->index);
532
533     PRINT_KEY(s, "sequence uid", pb->buf_ptr - 16);
534     mxf_write_common_fields(pb, st);
535
536     // write structural component
537     mxf_write_local_tag(pb, 16 + 8, 0x1001);
538     mxf_write_refs_count(pb, 1);
539     mxf_write_uuid(pb, type == MaterialPackage ? SourceClip: SourceClip + TypeBottom, st->index);
540 }
541
542 static void mxf_write_structural_component(AVFormatContext *s, AVStream *st, enum MXFMetadataSetType type)
543 {
544     ByteIOContext *pb = s->pb;
545     int i;
546
547     mxf_write_metadata_key(pb, 0x011100);
548     PRINT_KEY(s, "sturctural component key", pb->buf_ptr - 16);
549     klv_encode_ber_length(pb, 108);
550
551     // write uid
552     mxf_write_local_tag(pb, 16, 0x3C0A);
553     mxf_write_uuid(pb, type == MaterialPackage ? SourceClip: SourceClip + TypeBottom, st->index);
554
555     PRINT_KEY(s, "structural component uid", pb->buf_ptr - 16);
556     mxf_write_common_fields(pb, st);
557
558     // write start_position
559     mxf_write_local_tag(pb, 8, 0x1201);
560     put_be64(pb, 0);
561
562     // write source package uid, end of the reference
563     mxf_write_local_tag(pb, 32, 0x1101);
564     if (type == SourcePackage) {
565         for (i = 0; i < 4; i++)
566             put_be64(pb, 0);
567     } else
568         mxf_write_umid(pb, SourcePackage, 0);
569
570     // write source track id
571     mxf_write_local_tag(pb, 4, 0x1102);
572     if (type == SourcePackage)
573         put_be32(pb, 0);
574     else
575         put_be32(pb, st->index);
576 }
577
578 static void mxf_write_multi_descriptor(AVFormatContext *s)
579 {
580     MXFContext *mxf = s->priv_data;
581     ByteIOContext *pb = s->pb;
582     int i;
583
584     mxf_write_metadata_key(pb, 0x014400);
585     PRINT_KEY(s, "multiple descriptor key", pb->buf_ptr - 16);
586     klv_encode_ber_length(pb, 64 + 16 * s->nb_streams);
587
588     mxf_write_local_tag(pb, 16, 0x3C0A);
589     mxf_write_uuid(pb, MultipleDescriptor, 0);
590     PRINT_KEY(s, "multi_desc uid", pb->buf_ptr - 16);
591
592     // write sample rate
593     mxf_write_local_tag(pb, 8, 0x3001);
594     put_be32(pb, mxf->time_base.den);
595     put_be32(pb, mxf->time_base.num);
596
597     // write essence container ul
598     mxf_write_local_tag(pb, 16, 0x3004);
599     put_buffer(pb, multiple_desc_ul, 16);
600
601     // write sub descriptor refs
602     mxf_write_local_tag(pb, s->nb_streams * 16 + 8, 0x3F01);
603     mxf_write_refs_count(pb, s->nb_streams);
604     for (i = 0; i < s->nb_streams; i++)
605         mxf_write_uuid(pb, SubDescriptor, i);
606 }
607
608 static void mxf_write_generic_desc(AVFormatContext *s, AVStream *st, const UID key, unsigned size)
609 {
610     MXFContext *mxf = s->priv_data;
611     MXFStreamContext *sc = st->priv_data;
612     ByteIOContext *pb = s->pb;
613
614     put_buffer(pb, key, 16);
615     klv_encode_ber_length(pb, size);
616
617     mxf_write_local_tag(pb, 16, 0x3C0A);
618     mxf_write_uuid(pb, SubDescriptor, st->index);
619
620     mxf_write_local_tag(pb, 4, 0x3006);
621     put_be32(pb, st->index);
622
623     mxf_write_local_tag(pb, 8, 0x3001);
624     put_be32(pb, mxf->time_base.den);
625     put_be32(pb, mxf->time_base.num);
626
627     mxf_write_local_tag(pb, 16, 0x3004);
628     put_buffer(pb, mxf_essence_container_uls[sc->index].container_ul, 16);
629 }
630
631 static const UID mxf_mpegvideo_descriptor_key = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x51,0x00 };
632 static const UID mxf_wav_descriptor_key       = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x48,0x00 };
633 static const UID mxf_aes3_descriptor_key      = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x47,0x00 };
634
635 static void mxf_write_mpegvideo_desc(AVFormatContext *s, AVStream *st)
636 {
637     MXFStreamContext *sc = st->priv_data;
638     ByteIOContext *pb = s->pb;
639     int stored_height = (st->codec->height+15)/16*16;
640     AVRational dar;
641     int f1, f2;
642
643     mxf_write_generic_desc(s, st, mxf_mpegvideo_descriptor_key, 153+sc->interlaced*4);
644
645     mxf_write_local_tag(pb, 4, 0x3203);
646     put_be32(pb, st->codec->width);
647
648     mxf_write_local_tag(pb, 4, 0x3202);
649     put_be32(pb, stored_height>>sc->interlaced);
650
651     mxf_write_local_tag(pb, 4, 0x3209);
652     put_be32(pb, st->codec->width);
653
654     mxf_write_local_tag(pb, 4, 0x3208);
655     put_be32(pb, st->codec->height>>sc->interlaced);
656
657     // bit rate
658     mxf_write_local_tag(pb, 4, 0x8000);
659     put_be32(pb, st->codec->bit_rate);
660
661     // frame layout
662     mxf_write_local_tag(pb, 1, 0x320C);
663     put_byte(pb, sc->interlaced);
664
665     // video line map
666     switch (st->codec->height) {
667     case  576: f1 = 23; f2 = 336; break;
668     case  608: f1 =  7; f2 = 320; break;
669     case  480: f1 = 20; f2 = 283; break;
670     case  512: f1 =  7; f2 = 270; break;
671     case  720: f1 = 26; f2 =   0; break; // progressive
672     case 1080: f1 = 21; f2 = 584; break;
673     default:   f1 =  0; f2 =   0; break;
674     }
675
676     if (!sc->interlaced) {
677         f2  = 0;
678         f1 *= 2;
679     }
680
681     mxf_write_local_tag(pb, 12+sc->interlaced*4, 0x320D);
682     put_be32(pb, sc->interlaced ? 2 : 1);
683     put_be32(pb, 4);
684     put_be32(pb, f1);
685     if (sc->interlaced)
686         put_be32(pb, f2);
687
688     av_reduce(&dar.num, &dar.den,
689               st->codec->width*st->codec->sample_aspect_ratio.num,
690               st->codec->height*st->codec->sample_aspect_ratio.den,
691               1024*1024);
692
693     mxf_write_local_tag(pb, 8, 0x320E);
694     put_be32(pb, dar.num);
695     put_be32(pb, dar.den);
696
697     mxf_write_local_tag(pb, 16, 0x3201);
698     put_buffer(pb, *sc->codec_ul, 16);
699 }
700
701 static void mxf_write_generic_sound_desc(AVFormatContext *s, AVStream *st, const UID key, unsigned size)
702 {
703     ByteIOContext *pb = s->pb;
704
705     mxf_write_generic_desc(s, st, key, size);
706
707     // audio locked
708     mxf_write_local_tag(pb, 1, 0x3D02);
709     put_byte(pb, 1);
710
711     // write audio sampling rate
712     mxf_write_local_tag(pb, 8, 0x3D03);
713     put_be32(pb, st->codec->sample_rate);
714     put_be32(pb, 1);
715
716     mxf_write_local_tag(pb, 4, 0x3D07);
717     put_be32(pb, st->codec->channels);
718
719     mxf_write_local_tag(pb, 4, 0x3D01);
720     put_be32(pb, av_get_bits_per_sample(st->codec->codec_id));
721 }
722
723 static void mxf_write_wav_common_desc(AVFormatContext *s, AVStream *st, const UID key, unsigned size)
724 {
725     ByteIOContext *pb = s->pb;
726
727     mxf_write_generic_sound_desc(s, st, key, size);
728
729     mxf_write_local_tag(pb, 2, 0x3D0A);
730     put_be16(pb, st->codec->block_align);
731
732     // avg bytes per sec
733     mxf_write_local_tag(pb, 4, 0x3D09);
734     put_be32(pb, st->codec->block_align*st->codec->sample_rate);
735 }
736
737 static void mxf_write_wav_desc(AVFormatContext *s, AVStream *st)
738 {
739     mxf_write_wav_common_desc(s, st, mxf_wav_descriptor_key, 107);
740 }
741
742 static void mxf_write_aes3_desc(AVFormatContext *s, AVStream *st)
743 {
744     mxf_write_wav_common_desc(s, st, mxf_aes3_descriptor_key, 107);
745 }
746
747 static void mxf_write_package(AVFormatContext *s, enum MXFMetadataSetType type)
748 {
749     MXFContext *mxf = s->priv_data;
750     ByteIOContext *pb = s->pb;
751     int i;
752
753     if (type == MaterialPackage) {
754         mxf_write_metadata_key(pb, 0x013600);
755         PRINT_KEY(s, "Material Package key", pb->buf_ptr - 16);
756         klv_encode_ber_length(pb, 92 + 16 * s->nb_streams);
757     } else {
758         mxf_write_metadata_key(pb, 0x013700);
759         PRINT_KEY(s, "Source Package key", pb->buf_ptr - 16);
760         klv_encode_ber_length(pb, 112 + 16 * s->nb_streams); // 20 bytes length for descriptor reference
761     }
762
763     // write uid
764     mxf_write_local_tag(pb, 16, 0x3C0A);
765     mxf_write_uuid(pb, type, 0);
766     av_log(s,AV_LOG_DEBUG, "package type:%d\n", type);
767     PRINT_KEY(s, "package uid", pb->buf_ptr - 16);
768
769     // write package umid
770     mxf_write_local_tag(pb, 32, 0x4401);
771     mxf_write_umid(pb, type, 0);
772     PRINT_KEY(s, "package umid second part", pb->buf_ptr - 16);
773
774     // package creation date
775     mxf_write_local_tag(pb, 8, 0x4405);
776     put_be64(pb, mxf->timestamp);
777
778     // package modified date
779     mxf_write_local_tag(pb, 8, 0x4404);
780     put_be64(pb, mxf->timestamp);
781
782     // write track refs
783     mxf_write_local_tag(pb, s->nb_streams * 16 + 8, 0x4403);
784     mxf_write_refs_count(pb, s->nb_streams);
785     for (i = 0; i < s->nb_streams; i++)
786         mxf_write_uuid(pb, type == MaterialPackage ? Track : Track + TypeBottom, i);
787
788     // write multiple descriptor reference
789     if (type == SourcePackage) {
790         mxf_write_local_tag(pb, 16, 0x4701);
791         if (s->nb_streams > 1) {
792             mxf_write_uuid(pb, MultipleDescriptor, 0);
793             mxf_write_multi_descriptor(s);
794         } else
795             mxf_write_uuid(pb, SubDescriptor, 0);
796     }
797
798     for (i = 0; i < s->nb_streams; i++) {
799         AVStream *st = s->streams[i];
800         mxf_write_track(s, st, type);
801         mxf_write_sequence(s, st, type);
802         mxf_write_structural_component(s, st, type);
803
804         if (type == SourcePackage) {
805             MXFStreamContext *sc = st->priv_data;
806             mxf_essence_container_uls[sc->index].write_desc(s, st);
807         }
808     }
809 }
810
811 static int mxf_write_essence_container_data(AVFormatContext *s)
812 {
813     ByteIOContext *pb = s->pb;
814
815     mxf_write_metadata_key(pb, 0x012300);
816     klv_encode_ber_length(pb, 72);
817
818     mxf_write_local_tag(pb, 16, 0x3C0A); // Instance UID
819     mxf_write_uuid(pb, EssenceContainerData, 0);
820
821     mxf_write_local_tag(pb, 32, 0x2701); // Linked Package UID
822     mxf_write_umid(pb, SourcePackage, 0);
823
824     mxf_write_local_tag(pb, 4, 0x3F07); // BodySID
825     put_be32(pb, 1);
826
827     mxf_write_local_tag(pb, 4, 0x3F06); // IndexSID
828     put_be32(pb, 2);
829
830     return 0;
831 }
832
833 static int mxf_write_header_metadata_sets(AVFormatContext *s)
834 {
835     mxf_write_preface(s);
836     mxf_write_identification(s);
837     mxf_write_content_storage(s);
838     mxf_write_package(s, MaterialPackage);
839     mxf_write_package(s, SourcePackage);
840     mxf_write_essence_container_data(s);
841     return 0;
842 }
843
844 static unsigned klv_fill_size(uint64_t size)
845 {
846     unsigned pad = KAG_SIZE - (size & (KAG_SIZE-1));
847     if (pad < 17) // smallest fill item possible
848         return pad + KAG_SIZE;
849     else
850         return pad & (KAG_SIZE-1);
851 }
852
853 static int mxf_write_index_table_segment(AVFormatContext *s)
854 {
855     MXFContext *mxf = s->priv_data;
856     ByteIOContext *pb = s->pb;
857     int i, j, ret;
858     int temporal_reordering = 0;
859     int last_key_index = 0, key_index = 0;
860
861     av_log(s, AV_LOG_DEBUG, "edit units count %d\n", mxf->edit_units_count);
862
863     put_buffer(pb, index_table_segment_key, 16);
864     ret = klv_encode_ber_length(pb, 109 + (s->nb_streams+1)*6 +
865                                 mxf->edit_units_count*(11+mxf->slice_count*4));
866
867     // instance id
868     mxf_write_local_tag(pb, 16, 0x3C0A);
869     mxf_write_uuid(pb, IndexTableSegment, 0);
870
871     // index edit rate
872     mxf_write_local_tag(pb, 8, 0x3F0B);
873     put_be32(pb, mxf->time_base.num);
874     put_be32(pb, mxf->time_base.den);
875
876     // index start position
877     mxf_write_local_tag(pb, 8, 0x3F0C);
878     put_be64(pb, 0);
879
880     // index duration
881     mxf_write_local_tag(pb, 8, 0x3F0D);
882     put_be64(pb, mxf->edit_units_count);
883
884     // edit unit byte count
885     mxf_write_local_tag(pb, 4, 0x3F05);
886     put_be32(pb, 0);
887
888     // index sid
889     mxf_write_local_tag(pb, 4, 0x3F06);
890     put_be32(pb, 2);
891
892     // body sid
893     mxf_write_local_tag(pb, 4, 0x3F07);
894     put_be32(pb, 1);
895
896     // real slice count - 1
897     mxf_write_local_tag(pb, 1, 0x3F08);
898     put_byte(pb, mxf->slice_count);
899
900     // delta entry array
901     mxf_write_local_tag(pb, 8 + (s->nb_streams+1)*6, 0x3F09);
902     put_be32(pb, s->nb_streams+1); // num of entries
903     put_be32(pb, 6);             // size of one entry
904     // write system item delta entry
905     put_byte(pb, 0);
906     put_byte(pb, 0); // slice entry
907     put_be32(pb, 0); // element delta
908     for (i = 0; i < s->nb_streams; i++) {
909         AVStream *st = s->streams[i];
910         MXFStreamContext *sc = st->priv_data;
911         put_byte(pb, sc->temporal_reordering);
912         if (sc->temporal_reordering)
913             temporal_reordering = 1;
914         // slice number
915         if (i == 0) { // video track
916             put_byte(pb, 0); // slice number
917             put_be32(pb, KAG_SIZE); // system item size including klv fill
918         } else { // audio track
919             unsigned audio_frame_size = sc->aic.samples[0]*sc->aic.sample_size;
920             audio_frame_size += klv_fill_size(audio_frame_size);
921             put_byte(pb, 1);
922             put_be32(pb, (i-1)*audio_frame_size); // element delta
923         }
924     }
925
926     mxf_write_local_tag(pb, 8 + mxf->edit_units_count*(11+mxf->slice_count*4), 0x3F0A);
927     put_be32(pb, mxf->edit_units_count);  // num of entries
928     put_be32(pb, 11+mxf->slice_count*4);  // size of one entry
929     for (i = 0; i < mxf->edit_units_count; i++) {
930         if (temporal_reordering) {
931             int temporal_offset = 0;
932             for (j = i+1; j < mxf->edit_units_count; j++) {
933                 temporal_offset++;
934                 if (mxf->index_entries[j].flags & 0x10) { // backward prediction
935                     // next is not b, so is reordered
936                     if (!(mxf->index_entries[i+1].flags & 0x10)) {
937                         if ((mxf->index_entries[i].flags & 0x11) == 0) // i frame
938                             temporal_offset = 0;
939                         else
940                             temporal_offset = -temporal_offset;
941                     }
942                     break;
943                 }
944             }
945             put_byte(pb, temporal_offset);
946         } else
947             put_byte(pb, 0);
948         if (!(mxf->index_entries[i].flags & 0x33)) { // I frame
949             last_key_index = key_index;
950             key_index = i;
951         }
952         if (mxf->index_entries[i].flags & 0x10 && // backward prediction
953             !(mxf->index_entries[key_index].flags & 0x80)) { // open gop
954             put_byte(pb, last_key_index - i);
955         } else {
956             put_byte(pb, key_index - i); // key frame offset
957             if ((mxf->index_entries[i].flags & 0x20) == 0x20) // only forward
958                 last_key_index = key_index;
959         }
960         put_byte(pb, mxf->index_entries[i].flags);
961         // stream offset
962         put_be64(pb, mxf->index_entries[i].offset - mxf->index_entries[0].offset);
963         if (s->nb_streams > 1)
964             put_be32(pb, mxf->index_entries[i].slice_offset);
965     }
966
967     return ret;
968 }
969
970 static void mxf_write_partition(AVFormatContext *s, int bodysid,
971                                 int indexsid, unsigned index_byte_count,
972                                 const uint8_t *key, int write_metadata)
973 {
974     MXFContext *mxf = s->priv_data;
975     ByteIOContext *pb = s->pb;
976     int64_t header_byte_count_offset;
977
978     // write klv
979     put_buffer(pb, key, 16);
980
981     klv_encode_ber_length(pb, 88 + 16 * mxf->essence_container_count);
982
983     // write partition value
984     put_be16(pb, 1); // majorVersion
985     put_be16(pb, 2); // minorVersion
986     put_be32(pb, KAG_SIZE); // KAGSize
987
988     put_be64(pb, url_ftell(pb) - 25); // thisPartition
989     put_be64(pb, 0); // previousPartition
990
991     put_be64(pb, mxf->footer_partition_offset); // footerPartition
992
993     // set offset
994     header_byte_count_offset = url_ftell(pb);
995     put_be64(pb, 0); // headerByteCount, update later
996
997     // indexTable
998     put_be64(pb, index_byte_count); // indexByteCount
999     put_be32(pb, indexsid); // indexSID
1000     put_be64(pb, 0); // bodyOffset
1001
1002     put_be32(pb, bodysid); // bodySID
1003     put_buffer(pb, op1a_ul, 16); // operational pattern
1004
1005     // essence container
1006     mxf_write_essence_container_refs(s);
1007
1008     if (write_metadata) {
1009         // mark the start of the headermetadata and calculate metadata size
1010         int64_t pos, start = url_ftell(s->pb);
1011         unsigned header_byte_count;
1012
1013         mxf_write_primer_pack(s);
1014         mxf_write_header_metadata_sets(s);
1015         pos = url_ftell(s->pb);
1016         header_byte_count = pos - start + klv_fill_size(pos);
1017
1018         // update header_byte_count
1019         url_fseek(pb, header_byte_count_offset, SEEK_SET);
1020         put_be64(pb, header_byte_count);
1021         url_fseek(pb, pos, SEEK_SET);
1022     }
1023
1024     put_flush_packet(pb);
1025 }
1026
1027 static const UID mxf_mpeg2_codec_uls[] = {
1028     { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x01,0x10,0x00 }, // MP-ML I-Frame
1029     { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x01,0x11,0x00 }, // MP-ML Long GOP
1030     { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x02,0x02,0x00 }, // 422P-ML I-Frame
1031     { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x02,0x03,0x00 }, // 422P-ML Long GOP
1032     { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x03,0x02,0x00 }, // MP-HL I-Frame
1033     { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x03,0x03,0x00 }, // MP-HL Long GOP
1034     { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x04,0x02,0x00 }, // 422P-HL I-Frame
1035     { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x04,0x03,0x00 }, // 422P-HL Long GOP
1036 };
1037
1038 static const UID *mxf_get_mpeg2_codec_ul(AVCodecContext *avctx)
1039 {
1040     if (avctx->profile == 4) { // Main
1041         if (avctx->level == 8) // Main
1042             return avctx->gop_size ?
1043                 &mxf_mpeg2_codec_uls[1] :
1044                 &mxf_mpeg2_codec_uls[0];
1045         else if (avctx->level == 4) // High
1046             return avctx->gop_size ?
1047                 &mxf_mpeg2_codec_uls[5] :
1048                 &mxf_mpeg2_codec_uls[4];
1049     } else if (avctx->profile == 0) { // 422
1050         if (avctx->level == 5) // Main
1051             return avctx->gop_size ?
1052                 &mxf_mpeg2_codec_uls[3] :
1053                 &mxf_mpeg2_codec_uls[2];
1054         else if (avctx->level == 2) // High
1055             return avctx->gop_size ?
1056                 &mxf_mpeg2_codec_uls[7] :
1057                 &mxf_mpeg2_codec_uls[6];
1058     }
1059     return NULL;
1060 }
1061
1062 static int mxf_parse_mpeg2_frame(AVFormatContext *s, AVStream *st, AVPacket *pkt)
1063 {
1064     MXFContext *mxf = s->priv_data;
1065     MXFStreamContext *sc = st->priv_data;
1066     uint32_t c = -1;
1067     int i;
1068
1069     mxf->index_entries[mxf->edit_units_count].flags = 0;
1070
1071     for(i = 0; i < pkt->size - 4; i++) {
1072         c = (c<<8) + pkt->data[i];
1073         if (c == 0x1B5) {
1074             if (i + 2 < pkt->size && (pkt->data[i+1] & 0xf0) == 0x10) { // seq ext
1075                 st->codec->profile = pkt->data[i+1] & 0x07;
1076                 st->codec->level   = pkt->data[i+2] >> 4;
1077             } else if (i + 5 < pkt->size && (pkt->data[i+1] & 0xf0) == 0x80) { // pict coding ext
1078                 sc->interlaced = !(pkt->data[i+5] & 0x80); // progressive frame
1079                 break;
1080             }
1081         } else if (c == 0x1b8) { // gop
1082             if (i + 4 < pkt->size && pkt->data[i+4]>>6 & 0x01) // closed
1083                 mxf->index_entries[mxf->edit_units_count].flags |= 0x80; // random access
1084         } else if (c == 0x1b3) { // seq
1085             mxf->index_entries[mxf->edit_units_count].flags |= 0x40;
1086         } else if (c == 0x100) { // pic
1087             int pict_type = (pkt->data[i+2]>>3) & 0x07;
1088             if (pict_type == 2) { // P frame
1089                 mxf->index_entries[mxf->edit_units_count].flags |= 0x22;
1090                 st->codec->gop_size = 1;
1091             } else if (pict_type == 3) { // B frame
1092                 mxf->index_entries[mxf->edit_units_count].flags |= 0x33;
1093                 sc->temporal_reordering = -1;
1094             } else if (!pict_type) {
1095                 av_log(s, AV_LOG_ERROR, "error parsing mpeg2 frame\n");
1096                 return 0;
1097             }
1098         }
1099     }
1100     sc->codec_ul = mxf_get_mpeg2_codec_ul(st->codec);
1101     return !!sc->codec_ul;
1102 }
1103
1104 static uint64_t mxf_parse_timestamp(time_t timestamp)
1105 {
1106     struct tm *time = localtime(&timestamp);
1107     return (uint64_t)(time->tm_year+1900) << 48 |
1108            (uint64_t)(time->tm_mon+1)     << 40 |
1109            (uint64_t) time->tm_mday       << 32 |
1110                       time->tm_hour       << 24 |
1111                       time->tm_min        << 16 |
1112                       time->tm_sec        << 8;
1113 }
1114
1115 static int mxf_write_header(AVFormatContext *s)
1116 {
1117     MXFContext *mxf = s->priv_data;
1118     int i;
1119     uint8_t present[FF_ARRAY_ELEMS(mxf_essence_container_uls)] = {0};
1120     const int *samples_per_frame = NULL;
1121
1122     if (s->nb_streams > 17) {
1123         av_log(s, AV_LOG_ERROR, "error, mxf muxer supports 17 tracks maximum\n");
1124         return -1;
1125     }
1126
1127     for (i = 0; i < s->nb_streams; i++) {
1128         AVStream *st = s->streams[i];
1129         MXFStreamContext *sc = av_mallocz(sizeof(*sc));
1130         if (!sc)
1131             return AVERROR(ENOMEM);
1132         st->priv_data = sc;
1133
1134         if (st->codec->codec_type == CODEC_TYPE_VIDEO) {
1135             if (i != 0) {
1136                 av_log(s, AV_LOG_ERROR, "video stream must be first track\n");
1137                 return -1;
1138             }
1139             if (fabs(av_q2d(st->codec->time_base) - 1/25.0) < 0.0001) {
1140                 samples_per_frame = PAL_samples_per_frame;
1141                 mxf->time_base = (AVRational){ 1, 25 };
1142             } else if (fabs(av_q2d(st->codec->time_base) - 1001/30000.0) < 0.0001) {
1143                 samples_per_frame = NTSC_samples_per_frame;
1144                 mxf->time_base = (AVRational){ 1001, 30000 };
1145             } else {
1146                 av_log(s, AV_LOG_ERROR, "unsupported video frame rate\n");
1147                 return -1;
1148             }
1149             av_set_pts_info(st, 64, mxf->time_base.num, mxf->time_base.den);
1150         } else if (st->codec->codec_type == CODEC_TYPE_AUDIO) {
1151             if (st->codec->sample_rate != 48000) {
1152                 av_log(s, AV_LOG_ERROR, "only 48khz is implemented\n");
1153                 return -1;
1154             }
1155             av_set_pts_info(st, 64, 1, st->codec->sample_rate);
1156             mxf->slice_count = 1;
1157         }
1158         sc->duration = -1;
1159
1160         sc->index = mxf_get_essence_container_ul_index(st->codec->codec_id);
1161         if (sc->index == -1) {
1162             av_log(s, AV_LOG_ERROR, "track %d: could not find essence container ul, "
1163                    "codec not currently supported in container\n", i);
1164             return -1;
1165         }
1166
1167         sc->codec_ul = &mxf_essence_container_uls[sc->index].codec_ul;
1168
1169         if (!present[sc->index]) {
1170             mxf->essence_containers_indices[mxf->essence_container_count++] = sc->index;
1171             present[sc->index] = 1;
1172         } else
1173             present[sc->index]++;
1174         memcpy(sc->track_essence_element_key, mxf_essence_container_uls[sc->index].element_ul, 15);
1175         sc->track_essence_element_key[15] = present[sc->index];
1176         PRINT_KEY(s, "track essence element key", sc->track_essence_element_key);
1177     }
1178
1179     for (i = 0; i < s->nb_streams; i++) {
1180         MXFStreamContext *sc = s->streams[i]->priv_data;
1181         // update element count
1182         sc->track_essence_element_key[13] = present[sc->index];
1183         sc->order = AV_RB32(sc->track_essence_element_key+12);
1184     }
1185
1186     mxf->timestamp = mxf_parse_timestamp(s->timestamp);
1187
1188     if (!samples_per_frame)
1189         samples_per_frame = PAL_samples_per_frame;
1190
1191     if (ff_audio_interleave_init(s, samples_per_frame, mxf->time_base) < 0)
1192         return -1;
1193
1194     return 0;
1195 }
1196
1197 static void mxf_write_klv_fill(AVFormatContext *s)
1198 {
1199     unsigned pad = klv_fill_size(url_ftell(s->pb));
1200     if (pad) {
1201         put_buffer(s->pb, klv_fill_key, 16);
1202         pad -= 16;
1203         pad -= klv_ber_length(pad);
1204         klv_encode_ber_length(s->pb, pad);
1205         for (; pad > 7; pad -= 8)
1206             put_be64(s->pb, 0);
1207         for (; pad; pad--)
1208             put_byte(s->pb, 0);
1209         assert(!(url_ftell(s->pb) & (KAG_SIZE-1)));
1210     }
1211 }
1212
1213 static const uint8_t system_metadata_pack_key[]        = { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0D,0x01,0x03,0x01,0x04,0x01,0x01,0x00 };
1214 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 };
1215
1216 static uint32_t ff_framenum_to_12m_time_code(unsigned frame, int drop, int fps)
1217 {
1218     return (0                                    << 31) | // color frame flag
1219            (0                                    << 30) | // drop  frame flag
1220            ( ((frame % fps) / 10)                << 28) | // tens  of frames
1221            ( ((frame % fps) % 10)                << 24) | // units of frames
1222            (0                                    << 23) | // field phase (NTSC), b0 (PAL)
1223            ((((frame / fps) % 60) / 10)          << 20) | // tens  of seconds
1224            ((((frame / fps) % 60) % 10)          << 16) | // units of seconds
1225            (0                                    << 15) | // b0 (NTSC), b2 (PAL)
1226            ((((frame / (fps * 60)) % 60) / 10)   << 12) | // tens  of minutes
1227            ((((frame / (fps * 60)) % 60) % 10)   <<  8) | // units of minutes
1228            (0                                    <<  7) | // b1
1229            (0                                    <<  6) | // b2 (NSC), field phase (PAL)
1230            ((((frame / (fps * 3600) % 24)) / 10) <<  4) | // tens  of hours
1231            (  (frame / (fps * 3600) % 24)) % 10;          // units of hours
1232 }
1233
1234 static void mxf_write_system_item(AVFormatContext *s)
1235 {
1236     MXFContext *mxf = s->priv_data;
1237     ByteIOContext *pb = s->pb;
1238     unsigned fps, frame;
1239     uint32_t time_code;
1240
1241     frame = mxf->edit_units_count;
1242
1243     // write system metadata pack
1244     put_buffer(pb, system_metadata_pack_key, 16);
1245     klv_encode_ber_length(pb, 57);
1246     put_byte(pb, 0x5c); // UL, user date/time stamp, picture and sound item present
1247     put_byte(pb, 0x04); // content package rate
1248     put_byte(pb, 0x00); // content package type
1249     put_be16(pb, 0x00); // channel handle
1250     put_be16(pb, frame); // continuity count
1251     if (mxf->essence_container_count > 1)
1252         put_buffer(pb, multiple_desc_ul, 16);
1253     else
1254         put_buffer(pb, mxf_essence_container_uls[mxf->essence_containers_indices[0]].container_ul, 16);
1255     put_byte(pb, 0);
1256     put_be64(pb, 0);
1257     put_be64(pb, 0); // creation date/time stamp
1258
1259     // XXX drop frame
1260     if (mxf->time_base.den == 30000) fps = 30;
1261     else                             fps = 25;
1262
1263     put_byte(pb, 0x81); // SMPTE 12M time code
1264     time_code = ff_framenum_to_12m_time_code(frame, 0, fps);
1265     put_be32(pb, time_code);
1266     put_be32(pb, 0); // binary group data
1267     put_be64(pb, 0);
1268
1269     // write system metadata package set
1270     put_buffer(pb, system_metadata_package_set_key, 16);
1271     klv_encode_ber_length(pb, 35);
1272     put_byte(pb, 0x83); // UMID
1273     put_be16(pb, 0x20);
1274     mxf_write_umid(pb, SourcePackage, 0);
1275 }
1276
1277 static int mxf_write_packet(AVFormatContext *s, AVPacket *pkt)
1278 {
1279     MXFContext *mxf = s->priv_data;
1280     ByteIOContext *pb = s->pb;
1281     AVStream *st = s->streams[pkt->stream_index];
1282     MXFStreamContext *sc = st->priv_data;
1283
1284     if (!(mxf->edit_units_count % MXF_INDEX_CLUSTER_SIZE)) {
1285         mxf->index_entries = av_realloc(mxf->index_entries,
1286             (mxf->edit_units_count + MXF_INDEX_CLUSTER_SIZE)*
1287              sizeof(*mxf->index_entries));
1288         if (!mxf->index_entries) {
1289             av_log(s, AV_LOG_ERROR, "could not allocate index entries\n");
1290             return -1;
1291         }
1292     }
1293
1294     if (st->codec->codec_id == CODEC_ID_MPEG2VIDEO) {
1295         if (!mxf_parse_mpeg2_frame(s, st, pkt)) {
1296             av_log(s, AV_LOG_ERROR, "could not get mpeg2 profile and level\n");
1297             return -1;
1298         }
1299     }
1300
1301     if (!mxf->header_written) {
1302         mxf_write_partition(s, 1, 0, 0, header_open_partition_key, 1);
1303         mxf->header_written = 1;
1304     }
1305
1306     mxf_write_klv_fill(s);
1307
1308     if (st->index == 0) {
1309         mxf->index_entries[mxf->edit_units_count].offset = url_ftell(pb);
1310
1311         mxf_write_system_item(s);
1312         mxf_write_klv_fill(s);
1313
1314         mxf->edit_units_count++;
1315     } else if (st->index == 1) {
1316         mxf->index_entries[mxf->edit_units_count-1].slice_offset =
1317             url_ftell(pb) - mxf->index_entries[mxf->edit_units_count-1].offset;
1318     }
1319
1320     put_buffer(pb, sc->track_essence_element_key, 16); // write key
1321     klv_encode_ber_length(pb, pkt->size); // write length
1322     put_buffer(pb, pkt->data, pkt->size); // write value
1323
1324     sc->duration = FFMAX(pkt->pts + pkt->duration, sc->duration);
1325
1326     put_flush_packet(pb);
1327     return 0;
1328 }
1329
1330 static void mxf_write_random_index_pack(AVFormatContext *s)
1331 {
1332     MXFContext *mxf = s->priv_data;
1333     ByteIOContext *pb = s->pb;
1334     uint64_t pos = url_ftell(pb);
1335
1336     put_buffer(pb, random_index_pack_key, 16);
1337     klv_encode_ber_length(pb, 28);
1338
1339     put_be32(pb, 1); // BodySID of header partition
1340     put_be64(pb, 0); // offset of header partition
1341
1342     put_be32(pb, 0); // BodySID of footer partition
1343     put_be64(pb, mxf->footer_partition_offset);
1344
1345     put_be32(pb, url_ftell(pb) - pos + 4);
1346 }
1347
1348 static int mxf_write_footer(AVFormatContext *s)
1349 {
1350     MXFContext *mxf = s->priv_data;
1351     ByteIOContext *pb = s->pb;
1352     unsigned index_byte_count =
1353         109 + (s->nb_streams+1)*6 +
1354         mxf->edit_units_count*(11+mxf->slice_count*4);
1355
1356     // add encoded ber length
1357     index_byte_count += 16 + klv_ber_length(index_byte_count);
1358
1359     mxf_write_klv_fill(s);
1360
1361     mxf->footer_partition_offset = url_ftell(pb);
1362     mxf_write_partition(s, 0, 2, index_byte_count, footer_partition_key, 0);
1363
1364     mxf_write_index_table_segment(s);
1365
1366     mxf_write_random_index_pack(s);
1367
1368     if (!url_is_streamed(s->pb)) {
1369         url_fseek(pb, 0, SEEK_SET);
1370         mxf_write_partition(s, 1, 0, 0, header_closed_partition_key, 1);
1371     }
1372
1373     ff_audio_interleave_close(s);
1374
1375     mxf_free(s);
1376     return 0;
1377 }
1378
1379 static int mxf_interleave_get_packet(AVFormatContext *s, AVPacket *out, AVPacket *pkt, int flush)
1380 {
1381     AVPacketList *pktl;
1382     int stream_count = 0;
1383     int streams[MAX_STREAMS];
1384
1385     memset(streams, 0, sizeof(streams));
1386     pktl = s->packet_buffer;
1387     while (pktl) {
1388         //av_log(s, AV_LOG_DEBUG, "show st:%d dts:%lld\n", pktl->pkt.stream_index, pktl->pkt.dts);
1389         if (!streams[pktl->pkt.stream_index])
1390             stream_count++;
1391         streams[pktl->pkt.stream_index]++;
1392         pktl = pktl->next;
1393     }
1394
1395     if (stream_count && (s->nb_streams == stream_count || flush)) {
1396         pktl = s->packet_buffer;
1397         if (s->nb_streams != stream_count) {
1398             AVPacketList *first = NULL;
1399             // find first packet in edit unit
1400             while (pktl) {
1401                 AVStream *st = s->streams[pktl->pkt.stream_index];
1402                 if (st->index == 0)
1403                     break;
1404                 else if (!first)
1405                     first = pktl;
1406                 pktl = pktl->next;
1407             }
1408             // purge packet queue
1409             while (pktl) {
1410                 AVPacketList *next = pktl->next;
1411                 av_free_packet(&pktl->pkt);
1412                 av_freep(&pktl);
1413                 pktl = next;
1414             }
1415             if (!first)
1416                 goto out;
1417             pktl = first;
1418         }
1419
1420         *out = pktl->pkt;
1421         //av_log(s, AV_LOG_DEBUG, "out st:%d dts:%lld\n", (*out).stream_index, (*out).dts);
1422         s->packet_buffer = pktl->next;
1423         av_freep(&pktl);
1424         return 1;
1425     } else {
1426     out:
1427         av_init_packet(out);
1428         return 0;
1429     }
1430 }
1431
1432 static int mxf_compare_timestamps(AVFormatContext *s, AVPacket *next, AVPacket *pkt)
1433 {
1434     MXFStreamContext *sc  = s->streams[pkt ->stream_index]->priv_data;
1435     MXFStreamContext *sc2 = s->streams[next->stream_index]->priv_data;
1436
1437     return next->dts > pkt->dts ||
1438         (next->dts == pkt->dts && sc->order < sc2->order);
1439 }
1440
1441 static int mxf_interleave(AVFormatContext *s, AVPacket *out, AVPacket *pkt, int flush)
1442 {
1443     return ff_audio_interleave(s, out, pkt, flush,
1444                                mxf_interleave_get_packet, mxf_compare_timestamps);
1445 }
1446
1447 AVOutputFormat mxf_muxer = {
1448     "mxf",
1449     NULL_IF_CONFIG_SMALL("Material eXchange Format"),
1450     NULL,
1451     "mxf",
1452     sizeof(MXFContext),
1453     CODEC_ID_PCM_S16LE,
1454     CODEC_ID_MPEG2VIDEO,
1455     mxf_write_header,
1456     mxf_write_packet,
1457     mxf_write_footer,
1458     AVFMT_NOTIMESTAMPS,
1459     NULL,
1460     mxf_interleave,
1461 };