Statistics
| Branch: | Revision:

ffmpeg / libavformat / mxfenc.c @ 6fb625c4

History | View | Annotate | Download (50.5 KB)

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 "libavutil/fifo.h"
36
#include "mxf.h"
37

    
38
static const int NTSC_samples_per_frame[] = { 1602, 1601, 1602, 1601, 1602, 0 };
39
static const int PAL_samples_per_frame[]  = { 1920, 0 };
40

    
41
#define MXF_INDEX_CLUSTER_SIZE 4096
42

    
43
typedef struct {
44
    AVFifoBuffer fifo;
45
    unsigned fifo_size;           ///< current fifo size allocated
46
    uint64_t dts;                 ///< current dts
47
    int sample_size;              ///< size of one sample all channels included
48
    const int *samples_per_frame; ///< must be 0 terminated
49
    const int *samples;           ///< current samples per frame, pointer to samples_per_frame
50
} AudioInterleaveContext;
51

    
52
typedef struct {
53
    int local_tag;
54
    UID uid;
55
} MXFLocalTagPair;
56

    
57
typedef struct {
58
    uint8_t flags;
59
    uint64_t offset;
60
    unsigned slice_offset[17]; // one video, 16 audio
61
} MXFIndexEntry;
62

    
63
typedef struct {
64
    AudioInterleaveContext aic;
65
    UID track_essence_element_key;
66
    int index;            ///< index in mxf_essence_container_uls table
67
    const UID *codec_ul;
68
    int64_t duration;
69
    int order;            ///< interleaving order if dts are equal
70
    int interlaced;       ///< wether picture is interlaced
71
    int temporal_reordering;
72
} MXFStreamContext;
73

    
74
typedef struct {
75
    UID container_ul;
76
    UID element_ul;
77
    UID codec_ul;
78
    void (*write_desc)();
79
} MXFContainerEssenceEntry;
80

    
81
static const struct {
82
    enum CodecID id;
83
    int index;
84
} mxf_essence_mappings[] = {
85
    { CODEC_ID_MPEG2VIDEO, 0 },
86
    { CODEC_ID_PCM_S24LE,  1 },
87
    { CODEC_ID_PCM_S16LE,  1 },
88
    { 0 }
89
};
90

    
91
static void mxf_write_wav_desc(AVFormatContext *s, AVStream *st);
92
static void mxf_write_aes3_desc(AVFormatContext *s, AVStream *st);
93
static void mxf_write_mpegvideo_desc(AVFormatContext *s, AVStream *st);
94

    
95
static const MXFContainerEssenceEntry mxf_essence_container_uls[] = {
96
    { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x02,0x0D,0x01,0x03,0x01,0x02,0x04,0x60,0x01 },
97
      { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x15,0x01,0x05,0x00 },
98
      { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x00,0x00,0x00 },
99
      mxf_write_mpegvideo_desc },
100
    { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x06,0x03,0x00 },
101
      { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x16,0x01,0x03,0x00 },
102
      { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 },
103
      mxf_write_aes3_desc },
104
    { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x06,0x01,0x00 },
105
      { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x16,0x01,0x01,0x00 },
106
      { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 },
107
      mxf_write_wav_desc },
108
    { { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 },
109
      { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 },
110
      { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 },
111
      NULL },
112
};
113

    
114
typedef struct MXFContext {
115
    int64_t footer_partition_offset;
116
    int essence_container_count;
117
    uint8_t essence_containers_indices[FF_ARRAY_ELEMS(mxf_essence_container_uls)];
118
    AVRational time_base;
119
    int header_written;
120
    MXFIndexEntry *index_entries;
121
    unsigned edit_units_count;
122
    int edit_unit_start;  ///< index of the stream starting edit unit
123
} MXFContext;
124

    
125
static const uint8_t uuid_base[]            = { 0xAD,0xAB,0x44,0x24,0x2f,0x25,0x4d,0xc7,0x92,0xff,0x29,0xbd };
126
static const uint8_t umid_base[]            = { 0x06,0x0A,0x2B,0x34,0x01,0x01,0x01,0x05,0x01,0x01,0x0D,0x00,0x13,0x00,0x00,0x00 };
127

    
128
/**
129
 * complete key for operation pattern, partitions, and primer pack
130
 */
131
static const uint8_t op1a_ul[]                     = { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x02,0x01,0x01,0x01,0x01,0x00 };
132
static const uint8_t footer_partition_key[]        = { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0D,0x01,0x02,0x01,0x01,0x04,0x04,0x00 }; // ClosedComplete
133
static const uint8_t primer_pack_key[]             = { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0D,0x01,0x02,0x01,0x01,0x05,0x01,0x00 };
134
static const uint8_t index_table_segment_key[]     = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x10,0x01,0x00 };
135
static const uint8_t random_index_pack_key[]       = { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0D,0x01,0x02,0x01,0x01,0x11,0x01,0x00 };
136
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
137
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
138

    
139
/**
140
 * partial key for header metadata
141
 */
142
static const uint8_t header_metadata_key[]  = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0D,0x01,0x01,0x01,0x01 };
143
static const uint8_t multiple_desc_ul[]     = { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x0D,0x01,0x03,0x01,0x02,0x7F,0x01,0x00 };
144

    
145
/**
146
 * SMPTE RP210 http://www.smpte-ra.org/mdd/index.html
147
 */
148
static const MXFLocalTagPair mxf_local_tag_batch[] = {
149
    // preface set
150
    { 0x3C0A, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x01,0x01,0x15,0x02,0x00,0x00,0x00,0x00}}, /* Instance UID */
151
    { 0x3B02, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x10,0x02,0x04,0x00,0x00}}, /* Last Modified Date */
152
    { 0x3B05, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x03,0x01,0x02,0x01,0x05,0x00,0x00,0x00}}, /* Version */
153
    { 0x3B06, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x06,0x04,0x00,0x00}}, /* Identifications reference */
154
    { 0x3B03, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x02,0x01,0x00,0x00}}, /* Content Storage reference */
155
    { 0x3B09, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x01,0x02,0x02,0x03,0x00,0x00,0x00,0x00}}, /* Operational Pattern UL */
156
    { 0x3B0A, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x01,0x02,0x02,0x10,0x02,0x01,0x00,0x00}}, /* Essence Containers UL batch */
157
    { 0x3B0B, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x01,0x02,0x02,0x10,0x02,0x02,0x00,0x00}}, /* DM Schemes UL batch */
158
    // Identification
159
    { 0x3C09, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x20,0x07,0x01,0x01,0x00,0x00,0x00}}, /* This Generation UID */
160
    { 0x3C01, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x20,0x07,0x01,0x02,0x01,0x00,0x00}}, /* Company Name */
161
    { 0x3C02, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x20,0x07,0x01,0x03,0x01,0x00,0x00}}, /* Product Name */
162
    { 0x3C04, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x20,0x07,0x01,0x05,0x01,0x00,0x00}}, /* Version String */
163
    { 0x3C05, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x20,0x07,0x01,0x07,0x00,0x00,0x00}}, /* Product ID */
164
    { 0x3C06, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x10,0x02,0x03,0x00,0x00}}, /* Modification Date */
165
    // Content Storage
166
    { 0x1901, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x05,0x01,0x00,0x00}}, /* Package strong reference batch */
167
    { 0x1902, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x05,0x02,0x00,0x00}}, /* Package strong reference batch */
168
    // Essence Container Data
169
    { 0x2701, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x06,0x01,0x00,0x00,0x00}}, /* Linked Package UID */
170
    { 0x3F07, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x01,0x03,0x04,0x04,0x00,0x00,0x00,0x00}}, /* BodySID */
171
    // Package
172
    { 0x4401, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x01,0x01,0x15,0x10,0x00,0x00,0x00,0x00}}, /* Package UID */
173
    { 0x4405, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x10,0x01,0x03,0x00,0x00}}, /* Package Creation Date */
174
    { 0x4404, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x10,0x02,0x05,0x00,0x00}}, /* Package Modified Date */
175
    { 0x4403, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x06,0x05,0x00,0x00}}, /* Tracks Strong reference array */
176
    { 0x4701, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x02,0x03,0x00,0x00}}, /* Descriptor */
177
    // Track
178
    { 0x4801, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x01,0x07,0x01,0x01,0x00,0x00,0x00,0x00}}, /* Track ID */
179
    { 0x4804, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x01,0x04,0x01,0x03,0x00,0x00,0x00,0x00}}, /* Track Number */
180
    { 0x4B01, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x30,0x04,0x05,0x00,0x00,0x00,0x00}}, /* Edit Rate */
181
    { 0x4B02, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x03,0x01,0x03,0x00,0x00}}, /* Origin */
182
    { 0x4803, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x02,0x04,0x00,0x00}}, /* Sequence reference */
183
    // Sequence
184
    { 0x0201, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x07,0x01,0x00,0x00,0x00,0x00,0x00}}, /* Data Definition UL */
185
    { 0x0202, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x02,0x01,0x01,0x03,0x00,0x00}}, /* Duration */
186
    { 0x1001, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x06,0x09,0x00,0x00}}, /* Structural Components reference array */
187
    // Source Clip
188
    { 0x1201, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x07,0x02,0x01,0x03,0x01,0x04,0x00,0x00}}, /* Start position */
189
    { 0x1101, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x03,0x01,0x00,0x00,0x00}}, /* SourcePackageID */
190
    { 0x1102, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x03,0x02,0x00,0x00,0x00}}, /* SourceTrackID */
191
    // File Descriptor
192
    { 0x3F01, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x06,0x01,0x01,0x04,0x06,0x0B,0x00,0x00}}, /* Sub Descriptors reference array */
193
    { 0x3006, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x06,0x01,0x01,0x03,0x05,0x00,0x00,0x00}}, /* Linked Track ID */
194
    { 0x3001, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x06,0x01,0x01,0x00,0x00,0x00,0x00}}, /* SampleRate */
195
    { 0x3004, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x01,0x02,0x00,0x00}}, /* Essence Container */
196
    // Generic Picture Essence Descriptor
197
    { 0x320C, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x03,0x01,0x04,0x00,0x00,0x00}}, /* Frame Layout */
198
    { 0x320D, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x03,0x02,0x05,0x00,0x00,0x00}}, /* Video Line Map */
199
    { 0x3203, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x02,0x02,0x00,0x00,0x00}}, /* Stored Width */
200
    { 0x3202, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x02,0x01,0x00,0x00,0x00}}, /* Stored Height */
201
    { 0x3209, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x01,0x0C,0x00,0x00,0x00}}, /* Display Width */
202
    { 0x3208, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x01,0x0B,0x00,0x00,0x00}}, /* Display Height */
203
    { 0x320E, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x01,0x01,0x01,0x00,0x00,0x00}}, /* Aspect Ratio */
204
    { 0x3201, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x01,0x06,0x01,0x00,0x00,0x00,0x00}}, /* Picture Essence Coding */
205
    // Generic Sound Essence Descriptor
206
    { 0x3D02, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x02,0x03,0x01,0x04,0x00,0x00,0x00}}, /* Locked/Unlocked */
207
    { 0x3D03, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x02,0x03,0x01,0x01,0x01,0x00,0x00}}, /* Audio sampling rate */
208
    { 0x3D07, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x02,0x01,0x01,0x04,0x00,0x00,0x00}}, /* ChannelCount */
209
    { 0x3D01, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x04,0x02,0x03,0x03,0x04,0x00,0x00,0x00}}, /* Quantization bits */
210
    { 0x3D06, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x02,0x04,0x02,0x00,0x00,0x00,0x00}}, /* Sound Essence Compression */
211
    // Index Table Segment
212
    { 0x3F0B, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x05,0x30,0x04,0x06,0x00,0x00,0x00,0x00}}, /* Index Edit Rate */
213
    { 0x3F0C, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x07,0x02,0x01,0x03,0x01,0x0A,0x00,0x00}}, /* Index Start Position */
214
    { 0x3F0D, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x07,0x02,0x02,0x01,0x01,0x02,0x00,0x00}}, /* Index Duration */
215
    { 0x3F05, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x06,0x02,0x01,0x00,0x00,0x00,0x00,0x00}}, /* Edit Unit Byte Count */
216
    { 0x3F06, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x01,0x03,0x04,0x05,0x00,0x00,0x00,0x00}}, /* IndexSID */
217
    { 0x3F08, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x04,0x04,0x04,0x01,0x01,0x00,0x00,0x00}}, /* Slice Count */
218
    { 0x3F09, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x04,0x04,0x01,0x06,0x00,0x00,0x00}}, /* Delta Entry Array */
219
    { 0x3F0A, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x04,0x04,0x02,0x05,0x00,0x00,0x00}}, /* Index Entry Array */
220
    // MPEG video Descriptor
221
    { 0x8000, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x01,0x06,0x02,0x01,0x0B,0x00,0x00}}, /* BitRate */
222
    // Wave Audio Essence Descriptor
223
    { 0x3D09, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x02,0x03,0x03,0x05,0x00,0x00,0x00}}, /* Average Bytes Per Second */
224
    { 0x3D0A, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x02,0x03,0x02,0x01,0x00,0x00,0x00}}, /* Block Align */
225
};
226

    
227
static void mxf_write_uuid(ByteIOContext *pb, enum MXFMetadataSetType type, int value)
228
{
229
    put_buffer(pb, uuid_base, 12);
230
    put_be16(pb, type);
231
    put_be16(pb, value);
232
}
233

    
234
static void mxf_write_umid(ByteIOContext *pb, enum MXFMetadataSetType type, int value)
235
{
236
    put_buffer(pb, umid_base, 16);
237
    mxf_write_uuid(pb, type, value);
238
}
239

    
240
static void mxf_write_refs_count(ByteIOContext *pb, int ref_count)
241
{
242
    put_be32(pb, ref_count);
243
    put_be32(pb, 16);
244
}
245

    
246
static int klv_encode_ber_length(ByteIOContext *pb, uint64_t len)
247
{
248
    // Determine the best BER size
249
    int size;
250
    if (len < 128) {
251
        //short form
252
        put_byte(pb, len);
253
        return 1;
254
    }
255

    
256
    size = (av_log2(len) >> 3) + 1;
257

    
258
    // long form
259
    put_byte(pb, 0x80 + size);
260
    while(size) {
261
        size--;
262
        put_byte(pb, len >> 8 * size & 0xff);
263
    }
264
    return 0;
265
}
266

    
267
/*
268
 * Get essence container ul index
269
 */
270
static int mxf_get_essence_container_ul_index(enum CodecID id)
271
{
272
    int i;
273
    for (i = 0; mxf_essence_mappings[i].id; i++)
274
        if (mxf_essence_mappings[i].id == id)
275
            return mxf_essence_mappings[i].index;
276
    return -1;
277
}
278

    
279
static void mxf_write_primer_pack(AVFormatContext *s)
280
{
281
    ByteIOContext *pb = s->pb;
282
    int local_tag_number, i = 0;
283

    
284
    local_tag_number = FF_ARRAY_ELEMS(mxf_local_tag_batch);
285

    
286
    put_buffer(pb, primer_pack_key, 16);
287
    klv_encode_ber_length(pb, local_tag_number * 18 + 8);
288

    
289
    put_be32(pb, local_tag_number); // local_tag num
290
    put_be32(pb, 18); // item size, always 18 according to the specs
291

    
292
    for (i = 0; i < local_tag_number; i++) {
293
        put_be16(pb, mxf_local_tag_batch[i].local_tag);
294
        put_buffer(pb, mxf_local_tag_batch[i].uid, 16);
295
    }
296
}
297

    
298
static void mxf_write_local_tag(ByteIOContext *pb, int size, int tag)
299
{
300
    put_be16(pb, tag);
301
    put_be16(pb, size);
302
}
303

    
304
static void mxf_write_metadata_key(ByteIOContext *pb, unsigned int value)
305
{
306
    put_buffer(pb, header_metadata_key, 13);
307
    put_be24(pb, value);
308
}
309

    
310
static void mxf_free(AVFormatContext *s)
311
{
312
    int i;
313

    
314
    for (i = 0; i < s->nb_streams; i++) {
315
        AVStream *st = s->streams[i];
316
        av_freep(&st->priv_data);
317
    }
318
}
319

    
320
static const MXFDataDefinitionUL *mxf_get_data_definition_ul(enum CodecType type)
321
{
322
    const MXFDataDefinitionUL *uls = ff_mxf_data_definition_uls;
323
    while (uls->type != CODEC_TYPE_DATA) {
324
        if (type == uls->type)
325
            break;
326
        uls++;
327
    }
328
    return uls;
329
}
330

    
331
static void mxf_write_essence_container_refs(AVFormatContext *s)
332
{
333
    MXFContext *c = s->priv_data;
334
    ByteIOContext *pb = s->pb;
335
    int i;
336

    
337
    mxf_write_refs_count(pb, c->essence_container_count);
338
    av_log(s,AV_LOG_DEBUG, "essence container count:%d\n", c->essence_container_count);
339
    for (i = 0; i < c->essence_container_count; i++) {
340
        put_buffer(pb, mxf_essence_container_uls[c->essence_containers_indices[i]].container_ul, 16);
341
        PRINT_KEY(s, "essence container ul:\n", mxf_essence_container_uls[c->essence_containers_indices[i]].container_ul);
342
    }
343
}
344

    
345
static void mxf_write_preface(AVFormatContext *s)
346
{
347
    MXFContext *mxf = s->priv_data;
348
    ByteIOContext *pb = s->pb;
349

    
350
    mxf_write_metadata_key(pb, 0x012f00);
351
    PRINT_KEY(s, "preface key", pb->buf_ptr - 16);
352
    klv_encode_ber_length(pb, 130 + 16 * mxf->essence_container_count);
353

    
354
    // write preface set uid
355
    mxf_write_local_tag(pb, 16, 0x3C0A);
356
    mxf_write_uuid(pb, Preface, 0);
357
    PRINT_KEY(s, "preface uid", pb->buf_ptr - 16);
358

    
359
    // write create date as unknown
360
    mxf_write_local_tag(pb, 8, 0x3B02);
361
    put_be64(pb, 0);
362

    
363
    // write version
364
    mxf_write_local_tag(pb, 2, 0x3B05);
365
    put_be16(pb, 1);
366

    
367
    // write identification_refs
368
    mxf_write_local_tag(pb, 16 + 8, 0x3B06);
369
    mxf_write_refs_count(pb, 1);
370
    mxf_write_uuid(pb, Identification, 0);
371

    
372
    // write content_storage_refs
373
    mxf_write_local_tag(pb, 16, 0x3B03);
374
    mxf_write_uuid(pb, ContentStorage, 0);
375

    
376
    mxf_write_local_tag(pb, 16, 0x3B09);
377
    put_buffer(pb, op1a_ul, 16);
378

    
379
    // write essence_container_refs
380
    mxf_write_local_tag(pb, 8 + 16 * mxf->essence_container_count, 0x3B0A);
381
    mxf_write_essence_container_refs(s);
382

    
383
    // write dm_scheme_refs
384
    mxf_write_local_tag(pb, 8, 0x3B0B);
385
    put_be64(pb, 0);
386
}
387

    
388
/*
389
 * Write a local tag containing an ascii string as utf-16
390
 */
391
static void mxf_write_local_tag_utf16(ByteIOContext *pb, int tag, const char *value)
392
{
393
    int i, size = strlen(value);
394
    mxf_write_local_tag(pb, size*2, tag);
395
    for (i = 0; i < size; i++)
396
        put_be16(pb, value[i]);
397
}
398

    
399
static void mxf_write_identification(AVFormatContext *s)
400
{
401
    ByteIOContext *pb = s->pb;
402
    const char *company = "FFmpeg";
403
    const char *product = "OP1a Muxer";
404
    const char *version;
405
    int length;
406

    
407
    mxf_write_metadata_key(pb, 0x013000);
408
    PRINT_KEY(s, "identification key", pb->buf_ptr - 16);
409

    
410
    version = s->streams[0]->codec->flags & CODEC_FLAG_BITEXACT ?
411
        "0.0.0" : AV_STRINGIFY(LIBAVFORMAT_VERSION);
412
    length = 84 + (strlen(company)+strlen(product)+strlen(version))*2; // utf-16
413
    klv_encode_ber_length(pb, length);
414

    
415
    // write uid
416
    mxf_write_local_tag(pb, 16, 0x3C0A);
417
    mxf_write_uuid(pb, Identification, 0);
418
    PRINT_KEY(s, "identification uid", pb->buf_ptr - 16);
419

    
420
    // write generation uid
421
    mxf_write_local_tag(pb, 16, 0x3C09);
422
    mxf_write_uuid(pb, Identification, 1);
423

    
424
    mxf_write_local_tag_utf16(pb, 0x3C01, company); // Company Name
425
    mxf_write_local_tag_utf16(pb, 0x3C02, product); // Product Name
426
    mxf_write_local_tag_utf16(pb, 0x3C04, version); // Version String
427

    
428
    // write product uid
429
    mxf_write_local_tag(pb, 16, 0x3C05);
430
    mxf_write_uuid(pb, Identification, 2);
431

    
432
    // write modified date
433
    mxf_write_local_tag(pb, 8, 0x3C06);
434
    put_be64(pb, 0);
435
}
436

    
437
static void mxf_write_content_storage(AVFormatContext *s)
438
{
439
    ByteIOContext *pb = s->pb;
440

    
441
    mxf_write_metadata_key(pb, 0x011800);
442
    PRINT_KEY(s, "content storage key", pb->buf_ptr - 16);
443
    klv_encode_ber_length(pb, 92);
444

    
445
    // write uid
446
    mxf_write_local_tag(pb, 16, 0x3C0A);
447
    mxf_write_uuid(pb, ContentStorage, 0);
448
    PRINT_KEY(s, "content storage uid", pb->buf_ptr - 16);
449

    
450
    // write package reference
451
    mxf_write_local_tag(pb, 16 * 2 + 8, 0x1901);
452
    mxf_write_refs_count(pb, 2);
453
    mxf_write_uuid(pb, MaterialPackage, 0);
454
    mxf_write_uuid(pb, SourcePackage, 0);
455

    
456
    // write essence container data
457
    mxf_write_local_tag(pb, 8 + 16, 0x1902);
458
    mxf_write_refs_count(pb, 1);
459
    mxf_write_uuid(pb, EssenceContainerData, 0);
460
}
461

    
462
static void mxf_write_track(AVFormatContext *s, AVStream *st, enum MXFMetadataSetType type)
463
{
464
    ByteIOContext *pb = s->pb;
465
    MXFStreamContext *sc = st->priv_data;
466

    
467
    mxf_write_metadata_key(pb, 0x013b00);
468
    PRINT_KEY(s, "track key", pb->buf_ptr - 16);
469
    klv_encode_ber_length(pb, 80);
470

    
471
    // write track uid
472
    mxf_write_local_tag(pb, 16, 0x3C0A);
473
    mxf_write_uuid(pb, type == MaterialPackage ? Track : Track + TypeBottom, st->index);
474
    PRINT_KEY(s, "track uid", pb->buf_ptr - 16);
475

    
476
    // write track id
477
    mxf_write_local_tag(pb, 4, 0x4801);
478
    put_be32(pb, st->index);
479

    
480
    // write track number
481
    mxf_write_local_tag(pb, 4, 0x4804);
482
    if (type == MaterialPackage)
483
        put_be32(pb, 0); // track number of material package is 0
484
    else
485
        put_buffer(pb, sc->track_essence_element_key + 12, 4);
486

    
487
    mxf_write_local_tag(pb, 8, 0x4B01);
488
    put_be32(pb, st->time_base.den);
489
    put_be32(pb, st->time_base.num);
490

    
491
    // write origin
492
    mxf_write_local_tag(pb, 8, 0x4B02);
493
    put_be64(pb, 0);
494

    
495
    // write sequence refs
496
    mxf_write_local_tag(pb, 16, 0x4803);
497
    mxf_write_uuid(pb, type == MaterialPackage ? Sequence: Sequence + TypeBottom, st->index);
498
}
499

    
500
static void mxf_write_common_fields(ByteIOContext *pb, AVStream *st)
501
{
502
    const MXFDataDefinitionUL *data_def_ul = mxf_get_data_definition_ul(st->codec->codec_type);
503
    MXFStreamContext *sc = st->priv_data;
504

    
505
    // find data define uls
506
    mxf_write_local_tag(pb, 16, 0x0201);
507
    put_buffer(pb, data_def_ul->uid, 16);
508

    
509
    // write duration
510
    mxf_write_local_tag(pb, 8, 0x0202);
511
    put_be64(pb, sc->duration);
512
}
513

    
514
static void mxf_write_sequence(AVFormatContext *s, AVStream *st, enum MXFMetadataSetType type)
515
{
516
    ByteIOContext *pb = s->pb;
517

    
518
    mxf_write_metadata_key(pb, 0x010f00);
519
    PRINT_KEY(s, "sequence key", pb->buf_ptr - 16);
520
    klv_encode_ber_length(pb, 80);
521

    
522
    mxf_write_local_tag(pb, 16, 0x3C0A);
523
    mxf_write_uuid(pb, type == MaterialPackage ? Sequence: Sequence + TypeBottom, st->index);
524

    
525
    PRINT_KEY(s, "sequence uid", pb->buf_ptr - 16);
526
    mxf_write_common_fields(pb, st);
527

    
528
    // write structural component
529
    mxf_write_local_tag(pb, 16 + 8, 0x1001);
530
    mxf_write_refs_count(pb, 1);
531
    mxf_write_uuid(pb, type == MaterialPackage ? SourceClip: SourceClip + TypeBottom, st->index);
532
}
533

    
534
static void mxf_write_structural_component(AVFormatContext *s, AVStream *st, enum MXFMetadataSetType type)
535
{
536
    ByteIOContext *pb = s->pb;
537
    int i;
538

    
539
    mxf_write_metadata_key(pb, 0x011100);
540
    PRINT_KEY(s, "sturctural component key", pb->buf_ptr - 16);
541
    klv_encode_ber_length(pb, 108);
542

    
543
    // write uid
544
    mxf_write_local_tag(pb, 16, 0x3C0A);
545
    mxf_write_uuid(pb, type == MaterialPackage ? SourceClip: SourceClip + TypeBottom, st->index);
546

    
547
    PRINT_KEY(s, "structural component uid", pb->buf_ptr - 16);
548
    mxf_write_common_fields(pb, st);
549

    
550
    // write start_position
551
    mxf_write_local_tag(pb, 8, 0x1201);
552
    put_be64(pb, 0);
553

    
554
    // write source package uid, end of the reference
555
    mxf_write_local_tag(pb, 32, 0x1101);
556
    if (type == SourcePackage) {
557
        for (i = 0; i < 4; i++)
558
            put_be64(pb, 0);
559
    } else
560
        mxf_write_umid(pb, SourcePackage, 0);
561

    
562
    // write source track id
563
    mxf_write_local_tag(pb, 4, 0x1102);
564
    if (type == SourcePackage)
565
        put_be32(pb, 0);
566
    else
567
        put_be32(pb, st->index);
568
}
569

    
570
static void mxf_write_multi_descriptor(AVFormatContext *s)
571
{
572
    ByteIOContext *pb = s->pb;
573
    int i;
574

    
575
    mxf_write_metadata_key(pb, 0x014400);
576
    PRINT_KEY(s, "multiple descriptor key", pb->buf_ptr - 16);
577
    klv_encode_ber_length(pb, 64 + 16 * s->nb_streams);
578

    
579
    mxf_write_local_tag(pb, 16, 0x3C0A);
580
    mxf_write_uuid(pb, MultipleDescriptor, 0);
581
    PRINT_KEY(s, "multi_desc uid", pb->buf_ptr - 16);
582

    
583
    // write sample rate
584
    mxf_write_local_tag(pb, 8, 0x3001);
585
    put_be32(pb, s->streams[0]->time_base.den);
586
    put_be32(pb, s->streams[0]->time_base.num);
587

    
588
    // write essence container ul
589
    mxf_write_local_tag(pb, 16, 0x3004);
590
    put_buffer(pb, multiple_desc_ul, 16);
591

    
592
    // write sub descriptor refs
593
    mxf_write_local_tag(pb, s->nb_streams * 16 + 8, 0x3F01);
594
    mxf_write_refs_count(pb, s->nb_streams);
595
    for (i = 0; i < s->nb_streams; i++)
596
        mxf_write_uuid(pb, SubDescriptor, i);
597
}
598

    
599
static void mxf_write_generic_desc(ByteIOContext *pb, AVStream *st, const UID key, unsigned size)
600
{
601
    MXFStreamContext *sc = st->priv_data;
602

    
603
    put_buffer(pb, key, 16);
604
    klv_encode_ber_length(pb, size);
605

    
606
    mxf_write_local_tag(pb, 16, 0x3C0A);
607
    mxf_write_uuid(pb, SubDescriptor, st->index);
608

    
609
    mxf_write_local_tag(pb, 4, 0x3006);
610
    put_be32(pb, st->index);
611

    
612
    mxf_write_local_tag(pb, 8, 0x3001);
613
    put_be32(pb, st->time_base.den);
614
    put_be32(pb, st->time_base.num);
615

    
616
    mxf_write_local_tag(pb, 16, 0x3004);
617
    put_buffer(pb, mxf_essence_container_uls[sc->index].container_ul, 16);
618
}
619

    
620
static const UID mxf_mpegvideo_descriptor_key = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x51,0x00 };
621
static const UID mxf_wav_descriptor_key       = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x48,0x00 };
622
static const UID mxf_aes3_descriptor_key      = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x47,0x00 };
623

    
624
static void mxf_write_mpegvideo_desc(AVFormatContext *s, AVStream *st)
625
{
626
    MXFStreamContext *sc = st->priv_data;
627
    ByteIOContext *pb = s->pb;
628
    int stored_height = (st->codec->height+15)/16*16;
629
    AVRational dar;
630
    int f1, f2;
631

    
632
    mxf_write_generic_desc(pb, st, mxf_mpegvideo_descriptor_key, 153+sc->interlaced*4);
633

    
634
    mxf_write_local_tag(pb, 4, 0x3203);
635
    put_be32(pb, st->codec->width);
636

    
637
    mxf_write_local_tag(pb, 4, 0x3202);
638
    put_be32(pb, stored_height>>sc->interlaced);
639

    
640
    mxf_write_local_tag(pb, 4, 0x3209);
641
    put_be32(pb, st->codec->width);
642

    
643
    mxf_write_local_tag(pb, 4, 0x3208);
644
    put_be32(pb, st->codec->height>>sc->interlaced);
645

    
646
    // bit rate
647
    mxf_write_local_tag(pb, 4, 0x8000);
648
    put_be32(pb, st->codec->bit_rate);
649

    
650
    // frame layout
651
    mxf_write_local_tag(pb, 1, 0x320C);
652
    put_byte(pb, sc->interlaced);
653

    
654
    // video line map
655
    switch (st->codec->height) {
656
    case  576: f1 = 23; f2 = 336; break;
657
    case  608: f1 =  7; f2 = 320; break;
658
    case  480: f1 = 20; f2 = 283; break;
659
    case  512: f1 =  7; f2 = 270; break;
660
    case  720: f1 = 26; f2 =   0; break; // progressive
661
    case 1080: f1 = 21; f2 = 584; break;
662
    default:   f1 =  0; f2 =   0; break;
663
    }
664

    
665
    if (!sc->interlaced) {
666
        f2  = 0;
667
        f1 *= 2;
668
    }
669

    
670
    mxf_write_local_tag(pb, 12+sc->interlaced*4, 0x320D);
671
    put_be32(pb, sc->interlaced ? 2 : 1);
672
    put_be32(pb, 4);
673
    put_be32(pb, f1);
674
    if (sc->interlaced)
675
        put_be32(pb, f2);
676

    
677
    av_reduce(&dar.num, &dar.den,
678
              st->codec->width*st->codec->sample_aspect_ratio.num,
679
              st->codec->height*st->codec->sample_aspect_ratio.den,
680
              1024*1024);
681

    
682
    mxf_write_local_tag(pb, 8, 0x320E);
683
    put_be32(pb, dar.num);
684
    put_be32(pb, dar.den);
685

    
686
    mxf_write_local_tag(pb, 16, 0x3201);
687
    put_buffer(pb, *sc->codec_ul, 16);
688
}
689

    
690
static void mxf_write_generic_sound_desc(AVFormatContext *s, AVStream *st, const UID key, unsigned size)
691
{
692
    ByteIOContext *pb = s->pb;
693

    
694
    mxf_write_generic_desc(pb, st, key, size);
695

    
696
    // audio locked
697
    mxf_write_local_tag(pb, 1, 0x3D02);
698
    put_byte(pb, 1);
699

    
700
    // write audio sampling rate
701
    mxf_write_local_tag(pb, 8, 0x3D03);
702
    put_be32(pb, st->codec->sample_rate);
703
    put_be32(pb, 1);
704

    
705
    mxf_write_local_tag(pb, 4, 0x3D07);
706
    put_be32(pb, st->codec->channels);
707

    
708
    mxf_write_local_tag(pb, 4, 0x3D01);
709
    put_be32(pb, av_get_bits_per_sample(st->codec->codec_id));
710
}
711

    
712
static void mxf_write_wav_common_desc(AVFormatContext *s, AVStream *st, const UID key, unsigned size)
713
{
714
    ByteIOContext *pb = s->pb;
715

    
716
    mxf_write_generic_sound_desc(s, st, key, size);
717

    
718
    mxf_write_local_tag(pb, 2, 0x3D0A);
719
    put_be16(pb, st->codec->block_align);
720

    
721
    // avg bytes per sec
722
    mxf_write_local_tag(pb, 4, 0x3D09);
723
    put_be32(pb, st->codec->block_align*st->codec->sample_rate);
724
}
725

    
726
static void mxf_write_wav_desc(AVFormatContext *s, AVStream *st)
727
{
728
    mxf_write_wav_common_desc(s, st, mxf_wav_descriptor_key, 107);
729
}
730

    
731
static void mxf_write_aes3_desc(AVFormatContext *s, AVStream *st)
732
{
733
    mxf_write_wav_common_desc(s, st, mxf_aes3_descriptor_key, 107);
734
}
735

    
736
static void mxf_write_package(AVFormatContext *s, enum MXFMetadataSetType type)
737
{
738
    ByteIOContext *pb = s->pb;
739
    int i;
740

    
741
    if (type == MaterialPackage) {
742
        mxf_write_metadata_key(pb, 0x013600);
743
        PRINT_KEY(s, "Material Package key", pb->buf_ptr - 16);
744
        klv_encode_ber_length(pb, 92 + 16 * s->nb_streams);
745
    } else {
746
        mxf_write_metadata_key(pb, 0x013700);
747
        PRINT_KEY(s, "Source Package key", pb->buf_ptr - 16);
748
        klv_encode_ber_length(pb, 112 + 16 * s->nb_streams); // 20 bytes length for descriptor reference
749
    }
750

    
751
    // write uid
752
    mxf_write_local_tag(pb, 16, 0x3C0A);
753
    mxf_write_uuid(pb, type, 0);
754
    av_log(s,AV_LOG_DEBUG, "package type:%d\n", type);
755
    PRINT_KEY(s, "package uid", pb->buf_ptr - 16);
756

    
757
    // write package umid
758
    mxf_write_local_tag(pb, 32, 0x4401);
759
    mxf_write_umid(pb, type, 0);
760
    PRINT_KEY(s, "package umid second part", pb->buf_ptr - 16);
761

    
762
    // write create date
763
    mxf_write_local_tag(pb, 8, 0x4405);
764
    put_be64(pb, 0);
765

    
766
    // write modified date
767
    mxf_write_local_tag(pb, 8, 0x4404);
768
    put_be64(pb, 0);
769

    
770
    // write track refs
771
    mxf_write_local_tag(pb, s->nb_streams * 16 + 8, 0x4403);
772
    mxf_write_refs_count(pb, s->nb_streams);
773
    for (i = 0; i < s->nb_streams; i++)
774
        mxf_write_uuid(pb, type == MaterialPackage ? Track : Track + TypeBottom, i);
775

    
776
    // write multiple descriptor reference
777
    if (type == SourcePackage) {
778
        mxf_write_local_tag(pb, 16, 0x4701);
779
        if (s->nb_streams > 1) {
780
            mxf_write_uuid(pb, MultipleDescriptor, 0);
781
            mxf_write_multi_descriptor(s);
782
        } else
783
            mxf_write_uuid(pb, SubDescriptor, 0);
784
    }
785

    
786
    for (i = 0; i < s->nb_streams; i++) {
787
        AVStream *st = s->streams[i];
788
        mxf_write_track(s, st, type);
789
        mxf_write_sequence(s, st, type);
790
        mxf_write_structural_component(s, st, type);
791

    
792
        if (type == SourcePackage) {
793
            MXFStreamContext *sc = st->priv_data;
794
            mxf_essence_container_uls[sc->index].write_desc(s, st);
795
        }
796
    }
797
}
798

    
799
static int mxf_write_essence_container_data(AVFormatContext *s)
800
{
801
    ByteIOContext *pb = s->pb;
802

    
803
    mxf_write_metadata_key(pb, 0x012300);
804
    klv_encode_ber_length(pb, 72);
805

    
806
    mxf_write_local_tag(pb, 16, 0x3C0A); // Instance UID
807
    mxf_write_uuid(pb, EssenceContainerData, 0);
808

    
809
    mxf_write_local_tag(pb, 32, 0x2701); // Linked Package UID
810
    mxf_write_umid(pb, SourcePackage, 0);
811

    
812
    mxf_write_local_tag(pb, 4, 0x3F07); // BodySID
813
    put_be32(pb, 1);
814

    
815
    mxf_write_local_tag(pb, 4, 0x3F06); // IndexSID
816
    put_be32(pb, 2);
817

    
818
    return 0;
819
}
820

    
821
static int mxf_write_header_metadata_sets(AVFormatContext *s)
822
{
823
    mxf_write_preface(s);
824
    mxf_write_identification(s);
825
    mxf_write_content_storage(s);
826
    mxf_write_package(s, MaterialPackage);
827
    mxf_write_package(s, SourcePackage);
828
    mxf_write_essence_container_data(s);
829
    return 0;
830
}
831

    
832
static int mxf_write_index_table_segment(AVFormatContext *s)
833
{
834
    MXFContext *mxf = s->priv_data;
835
    ByteIOContext *pb = s->pb;
836
    int i, j, k, ret, key_offset = 0;
837
    int temporal_reordering = 0;
838

    
839
    av_log(s, AV_LOG_DEBUG, "edit units count %d\n", mxf->edit_units_count);
840

    
841
    put_buffer(pb, index_table_segment_key, 16);
842
    ret = klv_encode_ber_length(pb, 109 + s->nb_streams*6 +
843
                                mxf->edit_units_count*(11+(s->nb_streams-1)*4));
844

    
845
    // instance id
846
    mxf_write_local_tag(pb, 16, 0x3C0A);
847
    mxf_write_uuid(pb, IndexTableSegment, 0);
848

    
849
    // index edit rate
850
    mxf_write_local_tag(pb, 8, 0x3F0B);
851
    put_be32(pb, mxf->time_base.num);
852
    put_be32(pb, mxf->time_base.den);
853

    
854
    // index start position
855
    mxf_write_local_tag(pb, 8, 0x3F0C);
856
    put_be64(pb, 0);
857

    
858
    // index duration
859
    mxf_write_local_tag(pb, 8, 0x3F0D);
860
    put_be64(pb, mxf->edit_units_count);
861

    
862
    // edit unit byte count
863
    mxf_write_local_tag(pb, 4, 0x3F05);
864
    put_be32(pb, 0);
865

    
866
    // index sid
867
    mxf_write_local_tag(pb, 4, 0x3F06);
868
    put_be32(pb, 2);
869

    
870
    // body sid
871
    mxf_write_local_tag(pb, 4, 0x3F07);
872
    put_be32(pb, 1);
873

    
874
    // slice count - 1
875
    mxf_write_local_tag(pb, 1, 0x3F08);
876
    put_byte(pb, s->nb_streams-1);
877

    
878
    // delta entry array
879
    mxf_write_local_tag(pb, 8 + s->nb_streams*6, 0x3F09);
880
    put_be32(pb, s->nb_streams); // num of entries
881
    put_be32(pb, 6);             // size of one entry
882
    for (i = 0; i < s->nb_streams; i++) {
883
        AVStream *st = s->streams[i];
884
        MXFStreamContext *sc = st->priv_data;
885
        put_byte(pb, sc->temporal_reordering);
886
        if (sc->temporal_reordering)
887
            temporal_reordering = 1;
888
        put_byte(pb, i);
889
        put_be32(pb, 0); // element delta
890
    }
891

    
892
    mxf_write_local_tag(pb, 8 + mxf->edit_units_count*(11+(s->nb_streams-1)*4), 0x3F0A);
893
    put_be32(pb, mxf->edit_units_count);  // num of entries
894
    put_be32(pb, 11+(s->nb_streams-1)*4); // size of one entry
895
    for (i = 0; i < mxf->edit_units_count; i++) {
896
        if (mxf->index_entries[i].flags & 0x40)
897
            key_offset = 0;
898
        if (temporal_reordering) {
899
            int temporal_offset = 0;
900
            for (j = i+1; j < mxf->edit_units_count; j++) {
901
                temporal_offset++;
902
                if (mxf->index_entries[j].flags & 0x10) { // backward prediction
903
                    // next is not b, so is reordered
904
                    if (!(mxf->index_entries[i+1].flags & 0x10)) {
905
                        if ((mxf->index_entries[i].flags & 0x11) == 0) // i frame
906
                            temporal_offset = 0;
907
                        else
908
                            temporal_offset = -temporal_offset;
909
                    }
910
                    break;
911
                }
912
            }
913
            put_byte(pb, temporal_offset);
914
        } else
915
            put_byte(pb, 0);
916
        put_byte(pb, key_offset);
917
        put_byte(pb, mxf->index_entries[i].flags);
918
        // stream offset
919
        put_be64(pb, mxf->index_entries[i].offset - mxf->index_entries[0].offset);
920
        for (k = 0; k < s->nb_streams; k++) {
921
            if (mxf->index_entries[i].slice_offset[k])
922
                put_be32(pb, mxf->index_entries[i].slice_offset[k]);
923
        }
924
        key_offset--;
925
    }
926

    
927
    return ret;
928
}
929

    
930
static void mxf_write_partition(AVFormatContext *s, int bodysid, int indexsid,
931
                                const uint8_t *key, int write_metadata)
932
{
933
    MXFContext *mxf = s->priv_data;
934
    ByteIOContext *pb = s->pb;
935
    int64_t header_byte_count_offset;
936

    
937
    // write klv
938
    put_buffer(pb, key, 16);
939

    
940
    klv_encode_ber_length(pb, 88 + 16 * mxf->essence_container_count);
941

    
942
    // write partition value
943
    put_be16(pb, 1); // majorVersion
944
    put_be16(pb, 2); // minorVersion
945
    put_be32(pb, 1); // kagSize
946

    
947
    put_be64(pb, url_ftell(pb) - 25); // thisPartition
948
    put_be64(pb, 0); // previousPartition
949

    
950
    put_be64(pb, mxf->footer_partition_offset); // footerPartition
951

    
952
    // set offset
953
    header_byte_count_offset = url_ftell(pb);
954
    put_be64(pb, 0); // headerByteCount, update later
955

    
956
    // indexTable
957
    put_be64(pb, indexsid ? 19 + 109 + s->nb_streams*6 +
958
             mxf->edit_units_count*(11+(s->nb_streams-1)*4) : 0); // indexByteCount
959
    put_be32(pb, indexsid); // indexSID
960
    put_be64(pb, 0); // bodyOffset
961

    
962
    put_be32(pb, bodysid); // bodySID
963
    put_buffer(pb, op1a_ul, 16); // operational pattern
964

    
965
    // essence container
966
    mxf_write_essence_container_refs(s);
967

    
968
    if (write_metadata) {
969
        // mark the start of the headermetadata and calculate metadata size
970
        int64_t pos, start = url_ftell(s->pb);
971
        mxf_write_primer_pack(s);
972
        mxf_write_header_metadata_sets(s);
973
        pos = url_ftell(s->pb);
974
        // update header_byte_count
975
        url_fseek(pb, header_byte_count_offset, SEEK_SET);
976
        put_be64(pb, pos - start);
977
        url_fseek(pb, pos, SEEK_SET);
978
    }
979

    
980
    put_flush_packet(pb);
981
}
982

    
983
static const UID mxf_mpeg2_codec_uls[] = {
984
    { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x01,0x10,0x00 }, // MP-ML I-Frame
985
    { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x01,0x11,0x00 }, // MP-ML Long GOP
986
    { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x02,0x02,0x00 }, // 422P-ML I-Frame
987
    { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x02,0x03,0x00 }, // 422P-ML Long GOP
988
    { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x03,0x02,0x00 }, // MP-HL I-Frame
989
    { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x03,0x03,0x00 }, // MP-HL Long GOP
990
    { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x04,0x02,0x00 }, // 422P-HL I-Frame
991
    { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x04,0x03,0x00 }, // 422P-HL Long GOP
992
};
993

    
994
static const UID *mxf_get_mpeg2_codec_ul(AVCodecContext *avctx)
995
{
996
    if (avctx->profile == 4) { // Main
997
        if (avctx->level == 8) // Main
998
            return avctx->gop_size ?
999
                &mxf_mpeg2_codec_uls[1] :
1000
                &mxf_mpeg2_codec_uls[0];
1001
        else if (avctx->level == 4) // High
1002
            return avctx->gop_size ?
1003
                &mxf_mpeg2_codec_uls[5] :
1004
                &mxf_mpeg2_codec_uls[4];
1005
    } else if (avctx->profile == 0) { // 422
1006
        if (avctx->level == 5) // Main
1007
            return avctx->gop_size ?
1008
                &mxf_mpeg2_codec_uls[3] :
1009
                &mxf_mpeg2_codec_uls[2];
1010
        else if (avctx->level == 2) // High
1011
            return avctx->gop_size ?
1012
                &mxf_mpeg2_codec_uls[7] :
1013
                &mxf_mpeg2_codec_uls[6];
1014
    }
1015
    return NULL;
1016
}
1017

    
1018
static int mxf_parse_mpeg2_frame(AVFormatContext *s, AVStream *st, AVPacket *pkt)
1019
{
1020
    MXFContext *mxf = s->priv_data;
1021
    MXFStreamContext *sc = st->priv_data;
1022
    uint32_t c = -1;
1023
    int i;
1024

    
1025
    mxf->index_entries[mxf->edit_units_count].flags = 0;
1026

    
1027
    for(i = 0; i < pkt->size - 4; i++) {
1028
        c = (c<<8) + pkt->data[i];
1029
        if (c == 0x1B5) {
1030
            if (i + 2 < pkt->size && (pkt->data[i+1] & 0xf0) == 0x10) { // seq ext
1031
                st->codec->profile = pkt->data[i+1] & 0x07;
1032
                st->codec->level   = pkt->data[i+2] >> 4;
1033
            } else if (i + 5 < pkt->size && (pkt->data[i+1] & 0xf0) == 0x80) { // pict coding ext
1034
                sc->interlaced = !(pkt->data[i+5] & 0x80); // progressive frame
1035
                break;
1036
            }
1037
        } else if (c == 0x1b8) { // gop
1038
            if (i + 4 < pkt->size && pkt->data[i+4]>>6 & 0x01) // closed
1039
                mxf->index_entries[mxf->edit_units_count].flags |= 0x80; // random access
1040
        } else if (c == 0x1b3) { // seq
1041
            mxf->index_entries[mxf->edit_units_count].flags |= 0x40;
1042
        } else if (c == 0x100) { // pic
1043
            int pict_type = (pkt->data[i+2]>>3) & 0x07;
1044
            if (pict_type == 2) { // P frame
1045
                mxf->index_entries[mxf->edit_units_count].flags |= 0x22;
1046
                st->codec->gop_size = 1;
1047
            } else if (pict_type == 3) { // B frame
1048
                mxf->index_entries[mxf->edit_units_count].flags |= 0x33;
1049
                sc->temporal_reordering = -1;
1050
            } else if (!pict_type) {
1051
                av_log(s, AV_LOG_ERROR, "error parsing mpeg2 frame\n");
1052
                return 0;
1053
            }
1054
        }
1055
    }
1056
    sc->codec_ul = mxf_get_mpeg2_codec_ul(st->codec);
1057
    return !!sc->codec_ul;
1058
}
1059

    
1060
static int ff_audio_interleave_init(AVFormatContext *s, const int *samples_per_frame)
1061
{
1062
    int i;
1063

    
1064
    if (!samples_per_frame)
1065
        return -1;
1066

    
1067
    for (i = 0; i < s->nb_streams; i++) {
1068
        AVStream *st = s->streams[i];
1069
        AudioInterleaveContext *aic = st->priv_data;
1070

    
1071
        if (st->codec->codec_type == CODEC_TYPE_AUDIO) {
1072
            aic->sample_size = (st->codec->channels *
1073
                                av_get_bits_per_sample(st->codec->codec_id)) / 8;
1074
            if (!aic->sample_size) {
1075
                av_log(s, AV_LOG_ERROR, "could not compute sample size\n");
1076
                return -1;
1077
            }
1078
            aic->samples_per_frame = samples_per_frame;
1079
            aic->samples = aic->samples_per_frame;
1080

    
1081
            av_fifo_init(&aic->fifo, 100 * *aic->samples);
1082
        }
1083
    }
1084

    
1085
    return 0;
1086
}
1087

    
1088
static void ff_audio_interleave_close(AVFormatContext *s)
1089
{
1090
    int i;
1091
    for (i = 0; i < s->nb_streams; i++) {
1092
        AVStream *st = s->streams[i];
1093
        AudioInterleaveContext *aic = st->priv_data;
1094

    
1095
        if (st->codec->codec_type == CODEC_TYPE_AUDIO)
1096
            av_fifo_free(&aic->fifo);
1097
    }
1098
}
1099

    
1100
static int mxf_write_header(AVFormatContext *s)
1101
{
1102
    MXFContext *mxf = s->priv_data;
1103
    int i;
1104
    uint8_t present[FF_ARRAY_ELEMS(mxf_essence_container_uls)] = {0};
1105
    const int *samples_per_frame = NULL;
1106

    
1107
    if (s->nb_streams > 17) {
1108
        av_log(s, AV_LOG_ERROR, "error, mxf muxer supports 17 tracks maximum\n");
1109
        return -1;
1110
    }
1111

    
1112
    for (i = 0; i < s->nb_streams; i++) {
1113
        AVStream *st = s->streams[i];
1114
        MXFStreamContext *sc = av_mallocz(sizeof(*sc));
1115
        if (!sc)
1116
            return AVERROR(ENOMEM);
1117
        st->priv_data = sc;
1118

    
1119
        if (st->codec->codec_type == CODEC_TYPE_VIDEO) {
1120
            if (!av_cmp_q(st->codec->time_base, (AVRational){ 1, 25 })) {
1121
                samples_per_frame = PAL_samples_per_frame;
1122
                mxf->time_base = (AVRational){ 1, 25 };
1123
            } else if (!av_cmp_q(st->codec->time_base, (AVRational){ 1001, 30000 })) {
1124
                samples_per_frame = NTSC_samples_per_frame;
1125
                mxf->time_base = (AVRational){ 1001, 30000 };
1126
            } else {
1127
                av_log(s, AV_LOG_ERROR, "unsupported video frame rate\n");
1128
                return -1;
1129
            }
1130
            mxf->edit_unit_start = st->index;
1131
        } else if (st->codec->codec_type == CODEC_TYPE_AUDIO) {
1132
            if (st->codec->sample_rate != 48000) {
1133
                av_log(s, AV_LOG_ERROR, "only 48khz is implemented\n");
1134
                return -1;
1135
            }
1136
        }
1137
        sc->duration = -1;
1138

    
1139
        sc->index = mxf_get_essence_container_ul_index(st->codec->codec_id);
1140
        if (sc->index == -1) {
1141
            av_log(s, AV_LOG_ERROR, "track %d: could not find essence container ul, "
1142
                   "codec not currently supported in container\n", i);
1143
            return -1;
1144
        }
1145

    
1146
        sc->codec_ul = &mxf_essence_container_uls[sc->index].codec_ul;
1147

    
1148
        if (!present[sc->index]) {
1149
            mxf->essence_containers_indices[mxf->essence_container_count++] = sc->index;
1150
            present[sc->index] = 1;
1151
        } else
1152
            present[sc->index]++;
1153
        memcpy(sc->track_essence_element_key, mxf_essence_container_uls[sc->index].element_ul, 15);
1154
        sc->track_essence_element_key[15] = present[sc->index];
1155
        PRINT_KEY(s, "track essence element key", sc->track_essence_element_key);
1156
    }
1157

    
1158
    for (i = 0; i < s->nb_streams; i++) {
1159
        MXFStreamContext *sc = s->streams[i]->priv_data;
1160
        av_set_pts_info(s->streams[i], 64, mxf->time_base.num, mxf->time_base.den);
1161
        // update element count
1162
        sc->track_essence_element_key[13] = present[sc->index];
1163
        sc->order = AV_RB32(sc->track_essence_element_key+12);
1164
    }
1165

    
1166
    if (!samples_per_frame)
1167
        samples_per_frame = PAL_samples_per_frame;
1168

    
1169
    if (ff_audio_interleave_init(s, samples_per_frame) < 0)
1170
        return -1;
1171

    
1172
    return 0;
1173
}
1174

    
1175
static int mxf_write_packet(AVFormatContext *s, AVPacket *pkt)
1176
{
1177
    MXFContext *mxf = s->priv_data;
1178
    ByteIOContext *pb = s->pb;
1179
    AVStream *st = s->streams[pkt->stream_index];
1180
    MXFStreamContext *sc = st->priv_data;
1181

    
1182
    if (!(mxf->edit_units_count % MXF_INDEX_CLUSTER_SIZE)) {
1183
        mxf->index_entries = av_realloc(mxf->index_entries,
1184
            (mxf->edit_units_count + MXF_INDEX_CLUSTER_SIZE)*
1185
             sizeof(*mxf->index_entries));
1186
        if (!mxf->index_entries) {
1187
            av_log(s, AV_LOG_ERROR, "could not allocate index entries\n");
1188
            return -1;
1189
        }
1190
    }
1191

    
1192
    if (st->codec->codec_id == CODEC_ID_MPEG2VIDEO) {
1193
        if (!mxf_parse_mpeg2_frame(s, st, pkt)) {
1194
            av_log(s, AV_LOG_ERROR, "could not get mpeg2 profile and level\n");
1195
            return -1;
1196
        }
1197
    }
1198

    
1199
    if (!mxf->header_written) {
1200
        mxf_write_partition(s, 1, 0, header_open_partition_key, 1);
1201
        mxf->header_written = 1;
1202
    }
1203

    
1204
    if (st->index == mxf->edit_unit_start) {
1205
        mxf->index_entries[mxf->edit_units_count].offset = url_ftell(pb);
1206
        mxf->index_entries[mxf->edit_units_count].slice_offset[st->index] = 0;
1207
        mxf->edit_units_count++;
1208
    } else {
1209
        mxf->index_entries[mxf->edit_units_count-1].slice_offset[st->index] =
1210
            url_ftell(pb) - mxf->index_entries[mxf->edit_units_count-1].offset;
1211
    }
1212

    
1213
    put_buffer(pb, sc->track_essence_element_key, 16); // write key
1214
    klv_encode_ber_length(pb, pkt->size); // write length
1215
    put_buffer(pb, pkt->data, pkt->size); // write value
1216

    
1217
    sc->duration = FFMAX(pkt->pts + pkt->duration, sc->duration);
1218

    
1219
    put_flush_packet(pb);
1220
    return 0;
1221
}
1222

    
1223
static void mxf_write_random_index_pack(AVFormatContext *s)
1224
{
1225
    MXFContext *mxf = s->priv_data;
1226
    ByteIOContext *pb = s->pb;
1227
    uint64_t pos = url_ftell(pb);
1228

    
1229
    put_buffer(pb, random_index_pack_key, 16);
1230
    klv_encode_ber_length(pb, 28);
1231

    
1232
    put_be32(pb, 1); // BodySID of header partition
1233
    put_be64(pb, 0); // offset of header partition
1234

    
1235
    put_be32(pb, 0); // BodySID of footer partition
1236
    put_be64(pb, mxf->footer_partition_offset);
1237

    
1238
    put_be32(pb, url_ftell(pb) - pos + 4);
1239
}
1240

    
1241
static int mxf_write_footer(AVFormatContext *s)
1242
{
1243
    MXFContext *mxf = s->priv_data;
1244
    ByteIOContext *pb = s->pb;
1245

    
1246
    mxf->footer_partition_offset = url_ftell(pb);
1247
    mxf_write_partition(s, 0, 2, footer_partition_key, 0);
1248

    
1249
    mxf_write_index_table_segment(s);
1250

    
1251
    mxf_write_random_index_pack(s);
1252

    
1253
    if (!url_is_streamed(s->pb)) {
1254
        url_fseek(pb, 0, SEEK_SET);
1255
        mxf_write_partition(s, 1, 0, header_closed_partition_key, 1);
1256
    }
1257

    
1258
    ff_audio_interleave_close(s);
1259

    
1260
    mxf_free(s);
1261
    return 0;
1262
}
1263

    
1264
static int mxf_interleave_new_audio_packet(AVFormatContext *s, AVPacket *pkt,
1265
                                           int stream_index, int flush)
1266
{
1267
    AVStream *st = s->streams[stream_index];
1268
    AudioInterleaveContext *aic = st->priv_data;
1269

    
1270
    int size = FFMIN(av_fifo_size(&aic->fifo), *aic->samples * aic->sample_size);
1271
    if (!size || (!flush && size == av_fifo_size(&aic->fifo)))
1272
        return 0;
1273

    
1274
    av_new_packet(pkt, size);
1275
    av_fifo_read(&aic->fifo, pkt->data, size);
1276

    
1277
    pkt->dts = pkt->pts = aic->dts;
1278
    pkt->duration = av_rescale_q(*aic->samples,
1279
                                 (AVRational){ 1, st->codec->sample_rate },
1280
                                 st->time_base);
1281
    pkt->stream_index = stream_index;
1282
    aic->dts += pkt->duration;
1283

    
1284
    aic->samples++;
1285
    if (!*aic->samples)
1286
        aic->samples = aic->samples_per_frame;
1287

    
1288
    return size;
1289
}
1290

    
1291
static int mxf_interleave_get_packet(AVFormatContext *s, AVPacket *out, AVPacket *pkt, int flush)
1292
{
1293
    AVPacketList *pktl;
1294
    int stream_count = 0;
1295
    int streams[MAX_STREAMS];
1296

    
1297
    memset(streams, 0, sizeof(streams));
1298
    pktl = s->packet_buffer;
1299
    while (pktl) {
1300
        //av_log(s, AV_LOG_DEBUG, "show st:%d dts:%lld\n", pktl->pkt.stream_index, pktl->pkt.dts);
1301
        if (!streams[pktl->pkt.stream_index])
1302
            stream_count++;
1303
        streams[pktl->pkt.stream_index]++;
1304
        pktl = pktl->next;
1305
    }
1306

    
1307
    if (stream_count && (s->nb_streams == stream_count || flush)) {
1308
        pktl = s->packet_buffer;
1309
        if (s->nb_streams != stream_count) {
1310
            MXFContext *mxf = s->priv_data;
1311
            AVPacketList *first = NULL;
1312
            // find first packet in edit unit
1313
            while (pktl) {
1314
                AVStream *st = s->streams[pktl->pkt.stream_index];
1315
                if (st->index == mxf->edit_unit_start)
1316
                    break;
1317
                else if (!first)
1318
                    first = pktl;
1319
                pktl = pktl->next;
1320
            }
1321
            // purge packet queue
1322
            while (pktl) {
1323
                AVPacketList *next = pktl->next;
1324
                av_free_packet(&pktl->pkt);
1325
                av_freep(&pktl);
1326
                pktl = next;
1327
            }
1328
            if (!first)
1329
                goto out;
1330
            pktl = first;
1331
        }
1332

    
1333
        *out = pktl->pkt;
1334
        //av_log(s, AV_LOG_DEBUG, "out st:%d dts:%lld\n", (*out).stream_index, (*out).dts);
1335
        s->packet_buffer = pktl->next;
1336
        av_freep(&pktl);
1337
        return 1;
1338
    } else {
1339
    out:
1340
        av_init_packet(out);
1341
        return 0;
1342
    }
1343
}
1344

    
1345
static int mxf_compare_timestamps(AVFormatContext *s, AVPacket *next, AVPacket *pkt)
1346
{
1347
    AVStream *st  = s->streams[pkt ->stream_index];
1348
    AVStream *st2 = s->streams[next->stream_index];
1349
    MXFStreamContext *sc  = st ->priv_data;
1350
    MXFStreamContext *sc2 = st2->priv_data;
1351

    
1352
    int64_t left  = st2->time_base.num * (int64_t)st ->time_base.den;
1353
    int64_t right = st ->time_base.num * (int64_t)st2->time_base.den;
1354

    
1355
    return next->dts * left > pkt->dts * right || // FIXME this can overflow
1356
        (next->dts * left == pkt->dts * right && sc->order < sc2->order);
1357
}
1358

    
1359
static int mxf_interleave(AVFormatContext *s, AVPacket *out, AVPacket *pkt, int flush)
1360
{
1361
    int i;
1362

    
1363
    if (pkt) {
1364
        AVStream *st = s->streams[pkt->stream_index];
1365
        AudioInterleaveContext *aic = st->priv_data;
1366
        if (st->codec->codec_type == CODEC_TYPE_AUDIO) {
1367
            av_fifo_generic_write(&aic->fifo, pkt->data, pkt->size, NULL);
1368
        } else {
1369
            // rewrite pts and dts to be decoded time line position
1370
            pkt->pts = pkt->dts = aic->dts;
1371
            aic->dts += pkt->duration;
1372
            ff_interleave_add_packet(s, pkt, mxf_compare_timestamps);
1373
        }
1374
        pkt = NULL;
1375
    }
1376

    
1377
    for (i = 0; i < s->nb_streams; i++) {
1378
        AVStream *st = s->streams[i];
1379
        if (st->codec->codec_type == CODEC_TYPE_AUDIO) {
1380
            AVPacket new_pkt;
1381
            while (mxf_interleave_new_audio_packet(s, &new_pkt, i, flush))
1382
                ff_interleave_add_packet(s, &new_pkt, mxf_compare_timestamps);
1383
        }
1384
    }
1385

    
1386
    return mxf_interleave_get_packet(s, out, pkt, flush);
1387
}
1388

    
1389
AVOutputFormat mxf_muxer = {
1390
    "mxf",
1391
    NULL_IF_CONFIG_SMALL("Material eXchange Format"),
1392
    NULL,
1393
    "mxf",
1394
    sizeof(MXFContext),
1395
    CODEC_ID_PCM_S16LE,
1396
    CODEC_ID_MPEG2VIDEO,
1397
    mxf_write_header,
1398
    mxf_write_packet,
1399
    mxf_write_footer,
1400
    AVFMT_NOTIMESTAMPS,
1401
    NULL,
1402
    mxf_interleave,
1403
};