Statistics
| Branch: | Revision:

ffmpeg / libavformat / mxfenc.c @ 8d1a5af7

History | View | Annotate | Download (71.8 KB)

1 b5f46e93 Baptiste Coudurier
/*
2
 * MXF muxer
3
 * Copyright (c) 2008 GUCAS, Zhentan Feng <spyfeng at gmail dot com>
4 a1a37f13 Baptiste Coudurier
 * Copyright (c) 2008 Baptiste Coudurier <baptiste dot coudurier at gmail dot com>
5 b5f46e93 Baptiste Coudurier
 *
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 33abc1a7 Baptiste Coudurier
#include <math.h>
36 f5b28890 Baptiste Coudurier
#include <time.h>
37 33abc1a7 Baptiste Coudurier
38 cc812c8c Baptiste Coudurier
#include "libavutil/fifo.h"
39 251bf875 Baptiste Coudurier
#include "libavcodec/bytestream.h"
40 f1544e79 Baptiste Coudurier
#include "audiointerleave.h"
41 251bf875 Baptiste Coudurier
#include "avformat.h"
42 e48891bd Zhentan Feng
#include "mxf.h"
43
44 cc812c8c Baptiste Coudurier
static const int NTSC_samples_per_frame[] = { 1602, 1601, 1602, 1601, 1602, 0 };
45
static const int PAL_samples_per_frame[]  = { 1920, 0 };
46
47 bd3f81f3 Baptiste Coudurier
AVOutputFormat mxf_d10_muxer;
48
49 99b170f8 Baptiste Coudurier
#define EDIT_UNITS_PER_BODY 250
50 85d0d114 Baptiste Coudurier
#define KAG_SIZE 512
51 0e9dbd96 Baptiste Coudurier
52 cc812c8c Baptiste Coudurier
typedef struct {
53 e48891bd Zhentan Feng
    int local_tag;
54
    UID uid;
55
} MXFLocalTagPair;
56
57 2c8d4388 Zhentan Feng
typedef struct {
58 0e9dbd96 Baptiste Coudurier
    uint8_t flags;
59
    uint64_t offset;
60 425c9962 Baptiste Coudurier
    unsigned slice_offset; ///< offset of audio slice
61 0e9dbd96 Baptiste Coudurier
} MXFIndexEntry;
62
63
typedef struct {
64 cc812c8c Baptiste Coudurier
    AudioInterleaveContext aic;
65 2c8d4388 Zhentan Feng
    UID track_essence_element_key;
66 aecee3b2 Baptiste Coudurier
    int index;            ///< index in mxf_essence_container_uls table
67 4d418883 Baptiste Coudurier
    const UID *codec_ul;
68 8090b222 Baptiste Coudurier
    int order;            ///< interleaving order if dts are equal
69
    int interlaced;       ///< wether picture is interlaced
70 0e9dbd96 Baptiste Coudurier
    int temporal_reordering;
71 2c8d4388 Zhentan Feng
} MXFStreamContext;
72
73 b8ad3f78 Baptiste Coudurier
typedef struct {
74
    UID container_ul;
75
    UID element_ul;
76 4d418883 Baptiste Coudurier
    UID codec_ul;
77 f2ebd159 Baptiste Coudurier
    void (*write_desc)();
78 a4d7a678 Baptiste Coudurier
} MXFContainerEssenceEntry;
79 b8ad3f78 Baptiste Coudurier
80 2253d6d7 Baptiste Coudurier
static const struct {
81
    enum CodecID id;
82
    int index;
83
} mxf_essence_mappings[] = {
84
    { CODEC_ID_MPEG2VIDEO, 0 },
85 822617cd Baptiste Coudurier
    { CODEC_ID_PCM_S24LE,  1 },
86 2253d6d7 Baptiste Coudurier
    { CODEC_ID_PCM_S16LE,  1 },
87
    { 0 }
88
};
89
90 f2ebd159 Baptiste Coudurier
static void mxf_write_wav_desc(AVFormatContext *s, AVStream *st);
91 baed2ec4 Baptiste Coudurier
static void mxf_write_aes3_desc(AVFormatContext *s, AVStream *st);
92 f2ebd159 Baptiste Coudurier
static void mxf_write_mpegvideo_desc(AVFormatContext *s, AVStream *st);
93 bd3f81f3 Baptiste Coudurier
static void mxf_write_cdci_desc(AVFormatContext *s, AVStream *st);
94
static void mxf_write_generic_sound_desc(AVFormatContext *s, AVStream *st);
95 f2ebd159 Baptiste Coudurier
96 a4d7a678 Baptiste Coudurier
static const MXFContainerEssenceEntry mxf_essence_container_uls[] = {
97 b8ad3f78 Baptiste Coudurier
    { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x02,0x0D,0x01,0x03,0x01,0x02,0x04,0x60,0x01 },
98 4d418883 Baptiste Coudurier
      { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x15,0x01,0x05,0x00 },
99 f2ebd159 Baptiste Coudurier
      { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x00,0x00,0x00 },
100 2253d6d7 Baptiste Coudurier
      mxf_write_mpegvideo_desc },
101 baed2ec4 Baptiste Coudurier
    { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x06,0x03,0x00 },
102
      { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x16,0x01,0x03,0x00 },
103
      { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 },
104 2253d6d7 Baptiste Coudurier
      mxf_write_aes3_desc },
105 b8ad3f78 Baptiste Coudurier
    { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x06,0x01,0x00 },
106 4d418883 Baptiste Coudurier
      { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x16,0x01,0x01,0x00 },
107 f2ebd159 Baptiste Coudurier
      { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 },
108 2253d6d7 Baptiste Coudurier
      mxf_write_wav_desc },
109 bd3f81f3 Baptiste Coudurier
    // D-10 625/50 PAL 50mb/s
110
    { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x01,0x01 },
111
      { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x05,0x01,0x01,0x00 },
112
      { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x01,0x02,0x01,0x01 },
113
      mxf_write_cdci_desc },
114
    { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x01,0x01 },
115
      { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x06,0x01,0x10,0x00 },
116
      { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 },
117
      mxf_write_generic_sound_desc },
118
    // D-10 525/60 NTSC 50mb/s
119
    { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x02,0x01 },
120
      { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x05,0x01,0x01,0x00 },
121
      { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x01,0x02,0x01,0x02 },
122
      mxf_write_cdci_desc },
123
    { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x02,0x01 },
124
      { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x06,0x01,0x10,0x00 },
125
      { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 },
126
      mxf_write_generic_sound_desc },
127
    // D-10 625/50 PAL 40mb/s
128
    { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x03,0x01 },
129
      { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x05,0x01,0x01,0x00 },
130
      { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x01,0x02,0x01,0x03 },
131
      mxf_write_cdci_desc },
132
    { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x03,0x01 },
133
      { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x06,0x01,0x10,0x00 },
134
      { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 },
135
      mxf_write_generic_sound_desc },
136
    // D-10 525/60 NTSC 40mb/s
137
    { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x04,0x01 },
138
      { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x05,0x01,0x01,0x00 },
139
      { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x01,0x02,0x01,0x04 },
140
      mxf_write_cdci_desc },
141
    { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x04,0x01 },
142
      { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x06,0x01,0x10,0x00 },
143
      { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 },
144
      mxf_write_generic_sound_desc },
145
    // D-10 625/50 PAL 30mb/s
146
    { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x05,0x01 },
147
      { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x05,0x01,0x01,0x00 },
148
      { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x01,0x02,0x01,0x05 },
149
      mxf_write_cdci_desc },
150
    { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x05,0x01 },
151
      { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x06,0x01,0x10,0x00 },
152
      { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 },
153
      mxf_write_generic_sound_desc },
154
    // D-10 525/60 NTSC 30mb/s
155
    { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x06,0x01 },
156
      { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x05,0x01,0x01,0x00 },
157
      { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x01,0x02,0x01,0x06 },
158
      mxf_write_cdci_desc },
159
    { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x06,0x01 },
160
      { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x06,0x01,0x10,0x00 },
161
      { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 },
162
      mxf_write_generic_sound_desc },
163 b8ad3f78 Baptiste Coudurier
    { { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 },
164 4d418883 Baptiste Coudurier
      { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 },
165 f2ebd159 Baptiste Coudurier
      { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 },
166 2253d6d7 Baptiste Coudurier
      NULL },
167 b8ad3f78 Baptiste Coudurier
};
168
169 2c8d4388 Zhentan Feng
typedef struct MXFContext {
170 cc984060 Baptiste Coudurier
    int64_t footer_partition_offset;
171 2c8d4388 Zhentan Feng
    int essence_container_count;
172 37d3e066 Aurelien Jacobs
    uint8_t essence_containers_indices[FF_ARRAY_ELEMS(mxf_essence_container_uls)];
173 cc812c8c Baptiste Coudurier
    AVRational time_base;
174 7c7c1a76 Baptiste Coudurier
    int header_written;
175 0e9dbd96 Baptiste Coudurier
    MXFIndexEntry *index_entries;
176
    unsigned edit_units_count;
177 f5b28890 Baptiste Coudurier
    uint64_t timestamp;   ///< timestamp, as year(16),month(8),day(8),hour(8),minutes(8),msec/4(8)
178 425c9962 Baptiste Coudurier
    uint8_t slice_count;  ///< index slice count minus 1 (1 if no audio, 0 otherwise)
179 99b170f8 Baptiste Coudurier
    int last_indexed_edit_unit;
180
    uint64_t first_edit_unit_offset;
181
    uint64_t *body_partition_offset;
182
    unsigned body_partitions_count;
183
    int last_key_index;  ///< index of last key frame
184 7c9668cf Baptiste Coudurier
    uint64_t duration;
185
    AVStream *timecode_track;
186
    int timecode_base;       ///< rounded time code base (25 or 30)
187 7a101190 Baptiste Coudurier
    int timecode_start;      ///< frame number computed from mpeg-2 gop header timecode
188 7c9668cf Baptiste Coudurier
    int timecode_drop_frame; ///< time code use drop frame method frop mpeg-2 essence gop header
189 bd3f81f3 Baptiste Coudurier
    int edit_unit_byte_count; ///< fixed edit unit byte count
190 2c8d4388 Zhentan Feng
} MXFContext;
191 fda0d4af Zhentan Feng
192 e48891bd Zhentan Feng
static const uint8_t uuid_base[]            = { 0xAD,0xAB,0x44,0x24,0x2f,0x25,0x4d,0xc7,0x92,0xff,0x29,0xbd };
193 785b0f3c Baptiste Coudurier
static const uint8_t umid_base[]            = { 0x06,0x0A,0x2B,0x34,0x01,0x01,0x01,0x05,0x01,0x01,0x0D,0x00,0x13,0x00,0x00,0x00 };
194 e48891bd Zhentan Feng
195
/**
196
 * complete key for operation pattern, partitions, and primer pack
197
 */
198 6fb625c4 Baptiste Coudurier
static const uint8_t op1a_ul[]                     = { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x02,0x01,0x01,0x01,0x01,0x00 };
199
static const uint8_t footer_partition_key[]        = { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0D,0x01,0x02,0x01,0x01,0x04,0x04,0x00 }; // ClosedComplete
200
static const uint8_t primer_pack_key[]             = { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0D,0x01,0x02,0x01,0x01,0x05,0x01,0x00 };
201
static const uint8_t index_table_segment_key[]     = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x10,0x01,0x00 };
202
static const uint8_t random_index_pack_key[]       = { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0D,0x01,0x02,0x01,0x01,0x11,0x01,0x00 };
203 6685f639 Baptiste Coudurier
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
204
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
205 85d0d114 Baptiste Coudurier
static const uint8_t klv_fill_key[]                = { 0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x03,0x01,0x02,0x10,0x01,0x00,0x00,0x00 };
206 99b170f8 Baptiste Coudurier
static const uint8_t body_partition_key[]          = { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0D,0x01,0x02,0x01,0x01,0x03,0x04,0x00 }; // ClosedComplete
207 6685f639 Baptiste Coudurier
208 fda0d4af Zhentan Feng
/**
209
 * partial key for header metadata
210
 */
211
static const uint8_t header_metadata_key[]  = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0D,0x01,0x01,0x01,0x01 };
212 6fb625c4 Baptiste Coudurier
static const uint8_t multiple_desc_ul[]     = { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x0D,0x01,0x03,0x01,0x02,0x7F,0x01,0x00 };
213 fda0d4af Zhentan Feng
214 9b9466b0 Vitor Sessak
/**
215
 * SMPTE RP210 http://www.smpte-ra.org/mdd/index.html
216
 */
217
static const MXFLocalTagPair mxf_local_tag_batch[] = {
218
    // preface set
219
    { 0x3C0A, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x01,0x01,0x15,0x02,0x00,0x00,0x00,0x00}}, /* Instance UID */
220
    { 0x3B02, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x10,0x02,0x04,0x00,0x00}}, /* Last Modified Date */
221
    { 0x3B05, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x03,0x01,0x02,0x01,0x05,0x00,0x00,0x00}}, /* Version */
222
    { 0x3B06, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x06,0x04,0x00,0x00}}, /* Identifications reference */
223
    { 0x3B03, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x02,0x01,0x00,0x00}}, /* Content Storage reference */
224
    { 0x3B09, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x01,0x02,0x02,0x03,0x00,0x00,0x00,0x00}}, /* Operational Pattern UL */
225
    { 0x3B0A, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x01,0x02,0x02,0x10,0x02,0x01,0x00,0x00}}, /* Essence Containers UL batch */
226
    { 0x3B0B, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x01,0x02,0x02,0x10,0x02,0x02,0x00,0x00}}, /* DM Schemes UL batch */
227
    // Identification
228
    { 0x3C09, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x20,0x07,0x01,0x01,0x00,0x00,0x00}}, /* This Generation UID */
229
    { 0x3C01, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x20,0x07,0x01,0x02,0x01,0x00,0x00}}, /* Company Name */
230
    { 0x3C02, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x20,0x07,0x01,0x03,0x01,0x00,0x00}}, /* Product Name */
231 faac3f0f Baptiste Coudurier
    { 0x3C04, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x20,0x07,0x01,0x05,0x01,0x00,0x00}}, /* Version String */
232 9b9466b0 Vitor Sessak
    { 0x3C05, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x20,0x07,0x01,0x07,0x00,0x00,0x00}}, /* Product ID */
233
    { 0x3C06, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x10,0x02,0x03,0x00,0x00}}, /* Modification Date */
234
    // Content Storage
235
    { 0x1901, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x05,0x01,0x00,0x00}}, /* Package strong reference batch */
236 6e4668b8 Baptiste Coudurier
    { 0x1902, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x05,0x02,0x00,0x00}}, /* Package strong reference batch */
237 9b9466b0 Vitor Sessak
    // Essence Container Data
238
    { 0x2701, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x06,0x01,0x00,0x00,0x00}}, /* Linked Package UID */
239
    { 0x3F07, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x01,0x03,0x04,0x04,0x00,0x00,0x00,0x00}}, /* BodySID */
240
    // Package
241
    { 0x4401, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x01,0x01,0x15,0x10,0x00,0x00,0x00,0x00}}, /* Package UID */
242
    { 0x4405, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x10,0x01,0x03,0x00,0x00}}, /* Package Creation Date */
243
    { 0x4404, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x10,0x02,0x05,0x00,0x00}}, /* Package Modified Date */
244
    { 0x4403, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x06,0x05,0x00,0x00}}, /* Tracks Strong reference array */
245
    { 0x4701, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x02,0x03,0x00,0x00}}, /* Descriptor */
246
    // Track
247
    { 0x4801, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x01,0x07,0x01,0x01,0x00,0x00,0x00,0x00}}, /* Track ID */
248 316b438f Baptiste Coudurier
    { 0x4804, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x01,0x04,0x01,0x03,0x00,0x00,0x00,0x00}}, /* Track Number */
249 9b9466b0 Vitor Sessak
    { 0x4B01, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x30,0x04,0x05,0x00,0x00,0x00,0x00}}, /* Edit Rate */
250
    { 0x4B02, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x03,0x01,0x03,0x00,0x00}}, /* Origin */
251
    { 0x4803, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x02,0x04,0x00,0x00}}, /* Sequence reference */
252
    // Sequence
253
    { 0x0201, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x07,0x01,0x00,0x00,0x00,0x00,0x00}}, /* Data Definition UL */
254
    { 0x0202, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x02,0x01,0x01,0x03,0x00,0x00}}, /* Duration */
255
    { 0x1001, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x06,0x09,0x00,0x00}}, /* Structural Components reference array */
256
    // Source Clip
257 bd5c94b5 Baptiste Coudurier
    { 0x1201, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x03,0x01,0x04,0x00,0x00}}, /* Start position */
258 9b9466b0 Vitor Sessak
    { 0x1101, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x03,0x01,0x00,0x00,0x00}}, /* SourcePackageID */
259
    { 0x1102, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x03,0x02,0x00,0x00,0x00}}, /* SourceTrackID */
260 7c9668cf Baptiste Coudurier
    // Timecode Component
261
    { 0x1501, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x03,0x01,0x05,0x00,0x00}}, /* Start Time Code */
262
    { 0x1502, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x04,0x01,0x01,0x02,0x06,0x00,0x00}}, /* Rounded Time Code Base */
263
    { 0x1503, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x04,0x01,0x01,0x05,0x00,0x00,0x00}}, /* Drop Frame */
264 5b9bb66d Baptiste Coudurier
    // File Descriptor
265
    { 0x3F01, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x06,0x01,0x01,0x04,0x06,0x0B,0x00,0x00}}, /* Sub Descriptors reference array */
266 9b9466b0 Vitor Sessak
    { 0x3006, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x06,0x01,0x01,0x03,0x05,0x00,0x00,0x00}}, /* Linked Track ID */
267
    { 0x3001, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x06,0x01,0x01,0x00,0x00,0x00,0x00}}, /* SampleRate */
268 5b9bb66d Baptiste Coudurier
    { 0x3004, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x01,0x02,0x00,0x00}}, /* Essence Container */
269
    // Generic Picture Essence Descriptor
270 4118d68f Baptiste Coudurier
    { 0x320C, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x03,0x01,0x04,0x00,0x00,0x00}}, /* Frame Layout */
271 bd5c94b5 Baptiste Coudurier
    { 0x320D, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x01,0x03,0x02,0x05,0x00,0x00,0x00}}, /* Video Line Map */
272 5b9bb66d Baptiste Coudurier
    { 0x3203, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x02,0x02,0x00,0x00,0x00}}, /* Stored Width */
273
    { 0x3202, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x02,0x01,0x00,0x00,0x00}}, /* Stored Height */
274 724e7d8d Baptiste Coudurier
    { 0x3209, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x01,0x0C,0x00,0x00,0x00}}, /* Display Width */
275
    { 0x3208, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x01,0x0B,0x00,0x00,0x00}}, /* Display Height */
276 5b9bb66d Baptiste Coudurier
    { 0x320E, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x01,0x01,0x01,0x00,0x00,0x00}}, /* Aspect Ratio */
277
    { 0x3201, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x01,0x06,0x01,0x00,0x00,0x00,0x00}}, /* Picture Essence Coding */
278 ee1f7f4b Baptiste Coudurier
    // CDCI Picture Essence Descriptor
279
    { 0x3301, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x01,0x05,0x03,0x0A,0x00,0x00,0x00}}, /* Component Depth */
280
    { 0x3302, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x01,0x05,0x00,0x00,0x00}}, /* Horizontal Subsampling */
281 5b9bb66d Baptiste Coudurier
    // Generic Sound Essence Descriptor
282 bd5c94b5 Baptiste Coudurier
    { 0x3D02, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x04,0x02,0x03,0x01,0x04,0x00,0x00,0x00}}, /* Locked/Unlocked */
283 5b9bb66d Baptiste Coudurier
    { 0x3D03, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x02,0x03,0x01,0x01,0x01,0x00,0x00}}, /* Audio sampling rate */
284
    { 0x3D07, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x02,0x01,0x01,0x04,0x00,0x00,0x00}}, /* ChannelCount */
285
    { 0x3D01, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x04,0x02,0x03,0x03,0x04,0x00,0x00,0x00}}, /* Quantization bits */
286 0df4903a Baptiste Coudurier
    { 0x3D06, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x02,0x04,0x02,0x00,0x00,0x00,0x00}}, /* Sound Essence Compression */
287 0e9dbd96 Baptiste Coudurier
    // Index Table Segment
288
    { 0x3F0B, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x05,0x30,0x04,0x06,0x00,0x00,0x00,0x00}}, /* Index Edit Rate */
289
    { 0x3F0C, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x07,0x02,0x01,0x03,0x01,0x0A,0x00,0x00}}, /* Index Start Position */
290
    { 0x3F0D, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x07,0x02,0x02,0x01,0x01,0x02,0x00,0x00}}, /* Index Duration */
291 bd5c94b5 Baptiste Coudurier
    { 0x3F05, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x04,0x06,0x02,0x01,0x00,0x00,0x00,0x00}}, /* Edit Unit Byte Count */
292 0e9dbd96 Baptiste Coudurier
    { 0x3F06, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x01,0x03,0x04,0x05,0x00,0x00,0x00,0x00}}, /* IndexSID */
293
    { 0x3F08, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x04,0x04,0x04,0x01,0x01,0x00,0x00,0x00}}, /* Slice Count */
294
    { 0x3F09, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x04,0x04,0x01,0x06,0x00,0x00,0x00}}, /* Delta Entry Array */
295
    { 0x3F0A, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x04,0x04,0x02,0x05,0x00,0x00,0x00}}, /* Index Entry Array */
296 cbd70c41 Baptiste Coudurier
    // MPEG video Descriptor
297
    { 0x8000, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x01,0x06,0x02,0x01,0x0B,0x00,0x00}}, /* BitRate */
298 f9303ffd Baptiste Coudurier
    // Wave Audio Essence Descriptor
299
    { 0x3D09, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x02,0x03,0x03,0x05,0x00,0x00,0x00}}, /* Average Bytes Per Second */
300
    { 0x3D0A, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x02,0x03,0x02,0x01,0x00,0x00,0x00}}, /* Block Align */
301 9b9466b0 Vitor Sessak
};
302
303 b6fa8a8a Baptiste Coudurier
static void mxf_write_uuid(ByteIOContext *pb, enum MXFMetadataSetType type, int value)
304 e48891bd Zhentan Feng
{
305
    put_buffer(pb, uuid_base, 12);
306
    put_be16(pb, type);
307
    put_be16(pb, value);
308
}
309
310 b6fa8a8a Baptiste Coudurier
static void mxf_write_umid(ByteIOContext *pb, enum MXFMetadataSetType type, int value)
311 2c8d4388 Zhentan Feng
{
312
    put_buffer(pb, umid_base, 16);
313
    mxf_write_uuid(pb, type, value);
314
}
315 fda0d4af Zhentan Feng
316
static void mxf_write_refs_count(ByteIOContext *pb, int ref_count)
317
{
318
    put_be32(pb, ref_count);
319
    put_be32(pb, 16);
320
}
321
322 85d0d114 Baptiste Coudurier
static int klv_ber_length(uint64_t len)
323
{
324
    if (len < 128)
325
        return 1;
326
    else
327
        return (av_log2(len) >> 3) + 2;
328
}
329
330 e48891bd Zhentan Feng
static int klv_encode_ber_length(ByteIOContext *pb, uint64_t len)
331
{
332
    // Determine the best BER size
333
    int size;
334
    if (len < 128) {
335
        //short form
336
        put_byte(pb, len);
337
        return 1;
338
    }
339
340
    size = (av_log2(len) >> 3) + 1;
341
342
    // long form
343
    put_byte(pb, 0x80 + size);
344
    while(size) {
345 6fb625c4 Baptiste Coudurier
        size--;
346 e48891bd Zhentan Feng
        put_byte(pb, len >> 8 * size & 0xff);
347
    }
348
    return 0;
349
}
350
351 baaf7b1b Baptiste Coudurier
static void klv_encode_ber4_length(ByteIOContext *pb, int len)
352
{
353
    put_byte(pb, 0x80 + 3);
354
    put_be24(pb, len);
355
}
356
357 c18d208a Baptiste Coudurier
/*
358 d4ffc57d Baptiste Coudurier
 * Get essence container ul index
359 c18d208a Baptiste Coudurier
 */
360 d4ffc57d Baptiste Coudurier
static int mxf_get_essence_container_ul_index(enum CodecID id)
361 b5f46e93 Baptiste Coudurier
{
362 d4ffc57d Baptiste Coudurier
    int i;
363 2253d6d7 Baptiste Coudurier
    for (i = 0; mxf_essence_mappings[i].id; i++)
364
        if (mxf_essence_mappings[i].id == id)
365
            return mxf_essence_mappings[i].index;
366 d4ffc57d Baptiste Coudurier
    return -1;
367 b5f46e93 Baptiste Coudurier
}
368
369 9b9466b0 Vitor Sessak
static void mxf_write_primer_pack(AVFormatContext *s)
370 e48891bd Zhentan Feng
{
371
    ByteIOContext *pb = s->pb;
372
    int local_tag_number, i = 0;
373
374 37d3e066 Aurelien Jacobs
    local_tag_number = FF_ARRAY_ELEMS(mxf_local_tag_batch);
375 e48891bd Zhentan Feng
376
    put_buffer(pb, primer_pack_key, 16);
377
    klv_encode_ber_length(pb, local_tag_number * 18 + 8);
378
379
    put_be32(pb, local_tag_number); // local_tag num
380
    put_be32(pb, 18); // item size, always 18 according to the specs
381
382
    for (i = 0; i < local_tag_number; i++) {
383
        put_be16(pb, mxf_local_tag_batch[i].local_tag);
384
        put_buffer(pb, mxf_local_tag_batch[i].uid, 16);
385
    }
386
}
387
388 851a8297 Baptiste Coudurier
static void mxf_write_local_tag(ByteIOContext *pb, int size, int tag)
389 e48891bd Zhentan Feng
{
390
    put_be16(pb, tag);
391 851a8297 Baptiste Coudurier
    put_be16(pb, size);
392 e48891bd Zhentan Feng
}
393
394 2c8d4388 Zhentan Feng
static void mxf_write_metadata_key(ByteIOContext *pb, unsigned int value)
395
{
396
    put_buffer(pb, header_metadata_key, 13);
397
    put_be24(pb, value);
398
}
399
400 b5f46e93 Baptiste Coudurier
static void mxf_free(AVFormatContext *s)
401
{
402
    int i;
403
404
    for (i = 0; i < s->nb_streams; i++) {
405 ac0ab12f Baptiste Coudurier
        AVStream *st = s->streams[i];
406 b5f46e93 Baptiste Coudurier
        av_freep(&st->priv_data);
407
    }
408
}
409
410 5ac4d768 Baptiste Coudurier
static const MXFCodecUL *mxf_get_data_definition_ul(int type)
411 b5f46e93 Baptiste Coudurier
{
412 5ac4d768 Baptiste Coudurier
    const MXFCodecUL *uls = ff_mxf_data_definition_uls;
413
    while (uls->uid[0]) {
414
        if (type == uls->id)
415 b5f46e93 Baptiste Coudurier
            break;
416 a8f6e362 Baptiste Coudurier
        uls++;
417 b5f46e93 Baptiste Coudurier
    }
418
    return uls;
419
}
420
421 3eaa53b3 Baptiste Coudurier
static void mxf_write_essence_container_refs(AVFormatContext *s)
422 cedebde1 Carl Eugen Hoyos
{
423 c18d208a Baptiste Coudurier
    MXFContext *c = s->priv_data;
424 cedebde1 Carl Eugen Hoyos
    ByteIOContext *pb = s->pb;
425 c18d208a Baptiste Coudurier
    int i;
426 cedebde1 Carl Eugen Hoyos
427 e7daab1e Baptiste Coudurier
    mxf_write_refs_count(pb, c->essence_container_count);
428
    av_log(s,AV_LOG_DEBUG, "essence container count:%d\n", c->essence_container_count);
429
    for (i = 0; i < c->essence_container_count; i++) {
430 b8ad3f78 Baptiste Coudurier
        put_buffer(pb, mxf_essence_container_uls[c->essence_containers_indices[i]].container_ul, 16);
431
        PRINT_KEY(s, "essence container ul:\n", mxf_essence_container_uls[c->essence_containers_indices[i]].container_ul);
432 e7daab1e Baptiste Coudurier
    }
433 cedebde1 Carl Eugen Hoyos
}
434
435 9b9466b0 Vitor Sessak
static void mxf_write_preface(AVFormatContext *s)
436
{
437
    MXFContext *mxf = s->priv_data;
438
    ByteIOContext *pb = s->pb;
439
440
    mxf_write_metadata_key(pb, 0x012f00);
441
    PRINT_KEY(s, "preface key", pb->buf_ptr - 16);
442
    klv_encode_ber_length(pb, 130 + 16 * mxf->essence_container_count);
443
444
    // write preface set uid
445
    mxf_write_local_tag(pb, 16, 0x3C0A);
446
    mxf_write_uuid(pb, Preface, 0);
447
    PRINT_KEY(s, "preface uid", pb->buf_ptr - 16);
448
449 f5b28890 Baptiste Coudurier
    // last modified date
450 9b9466b0 Vitor Sessak
    mxf_write_local_tag(pb, 8, 0x3B02);
451 f5b28890 Baptiste Coudurier
    put_be64(pb, mxf->timestamp);
452 9b9466b0 Vitor Sessak
453
    // write version
454
    mxf_write_local_tag(pb, 2, 0x3B05);
455
    put_be16(pb, 1);
456
457
    // write identification_refs
458
    mxf_write_local_tag(pb, 16 + 8, 0x3B06);
459
    mxf_write_refs_count(pb, 1);
460
    mxf_write_uuid(pb, Identification, 0);
461
462
    // write content_storage_refs
463
    mxf_write_local_tag(pb, 16, 0x3B03);
464
    mxf_write_uuid(pb, ContentStorage, 0);
465
466 216b6631 Baptiste Coudurier
    // operational pattern
467 9b9466b0 Vitor Sessak
    mxf_write_local_tag(pb, 16, 0x3B09);
468 216b6631 Baptiste Coudurier
    if (s->nb_streams > 1) {
469
        put_buffer(pb, op1a_ul, 14);
470
        put_be16(pb, 0x0900); // multi track
471
    } else {
472
        put_buffer(pb, op1a_ul, 16);
473
    }
474 9b9466b0 Vitor Sessak
475
    // write essence_container_refs
476
    mxf_write_local_tag(pb, 8 + 16 * mxf->essence_container_count, 0x3B0A);
477 3eaa53b3 Baptiste Coudurier
    mxf_write_essence_container_refs(s);
478 9b9466b0 Vitor Sessak
479
    // write dm_scheme_refs
480
    mxf_write_local_tag(pb, 8, 0x3B0B);
481
    put_be64(pb, 0);
482
}
483
484 5f94b920 Baptiste Coudurier
/*
485 9fedbc89 Baptiste Coudurier
 * Write a local tag containing an ascii string as utf-16
486 5f94b920 Baptiste Coudurier
 */
487 9fedbc89 Baptiste Coudurier
static void mxf_write_local_tag_utf16(ByteIOContext *pb, int tag, const char *value)
488 5f94b920 Baptiste Coudurier
{
489 6b05863a Baptiste Coudurier
    int i, size = strlen(value);
490 9fedbc89 Baptiste Coudurier
    mxf_write_local_tag(pb, size*2, tag);
491 5f94b920 Baptiste Coudurier
    for (i = 0; i < size; i++)
492
        put_be16(pb, value[i]);
493
}
494
495 9b9466b0 Vitor Sessak
static void mxf_write_identification(AVFormatContext *s)
496
{
497 f5b28890 Baptiste Coudurier
    MXFContext *mxf = s->priv_data;
498 9b9466b0 Vitor Sessak
    ByteIOContext *pb = s->pb;
499 6fdb4fb0 Baptiste Coudurier
    const char *company = "FFmpeg";
500
    const char *product = "OP1a Muxer";
501 283a02c5 Baptiste Coudurier
    const char *version;
502 6fdb4fb0 Baptiste Coudurier
    int length;
503 9b9466b0 Vitor Sessak
504
    mxf_write_metadata_key(pb, 0x013000);
505
    PRINT_KEY(s, "identification key", pb->buf_ptr - 16);
506
507 b4395253 Baptiste Coudurier
    version = s->streams[0]->codec->flags & CODEC_FLAG_BITEXACT ?
508 0c0c4d64 Baptiste Coudurier
        "0.0.0" : AV_STRINGIFY(LIBAVFORMAT_VERSION);
509 6fdb4fb0 Baptiste Coudurier
    length = 84 + (strlen(company)+strlen(product)+strlen(version))*2; // utf-16
510 9b9466b0 Vitor Sessak
    klv_encode_ber_length(pb, length);
511
512
    // write uid
513
    mxf_write_local_tag(pb, 16, 0x3C0A);
514
    mxf_write_uuid(pb, Identification, 0);
515
    PRINT_KEY(s, "identification uid", pb->buf_ptr - 16);
516 0b62964f Baptiste Coudurier
517 9b9466b0 Vitor Sessak
    // write generation uid
518
    mxf_write_local_tag(pb, 16, 0x3C09);
519
    mxf_write_uuid(pb, Identification, 1);
520
521 6fdb4fb0 Baptiste Coudurier
    mxf_write_local_tag_utf16(pb, 0x3C01, company); // Company Name
522
    mxf_write_local_tag_utf16(pb, 0x3C02, product); // Product Name
523 283a02c5 Baptiste Coudurier
    mxf_write_local_tag_utf16(pb, 0x3C04, version); // Version String
524 9b9466b0 Vitor Sessak
525
    // write product uid
526
    mxf_write_local_tag(pb, 16, 0x3C05);
527
    mxf_write_uuid(pb, Identification, 2);
528
529 5ccad11d Baptiste Coudurier
    // modification date
530 9b9466b0 Vitor Sessak
    mxf_write_local_tag(pb, 8, 0x3C06);
531 f5b28890 Baptiste Coudurier
    put_be64(pb, mxf->timestamp);
532 9b9466b0 Vitor Sessak
}
533
534
static void mxf_write_content_storage(AVFormatContext *s)
535
{
536
    ByteIOContext *pb = s->pb;
537
538
    mxf_write_metadata_key(pb, 0x011800);
539
    PRINT_KEY(s, "content storage key", pb->buf_ptr - 16);
540 6e4668b8 Baptiste Coudurier
    klv_encode_ber_length(pb, 92);
541 9b9466b0 Vitor Sessak
542
    // write uid
543
    mxf_write_local_tag(pb, 16, 0x3C0A);
544
    mxf_write_uuid(pb, ContentStorage, 0);
545
    PRINT_KEY(s, "content storage uid", pb->buf_ptr - 16);
546 a8f6e362 Baptiste Coudurier
547 9b9466b0 Vitor Sessak
    // write package reference
548
    mxf_write_local_tag(pb, 16 * 2 + 8, 0x1901);
549
    mxf_write_refs_count(pb, 2);
550
    mxf_write_uuid(pb, MaterialPackage, 0);
551
    mxf_write_uuid(pb, SourcePackage, 0);
552 6e4668b8 Baptiste Coudurier
553
    // write essence container data
554
    mxf_write_local_tag(pb, 8 + 16, 0x1902);
555
    mxf_write_refs_count(pb, 1);
556
    mxf_write_uuid(pb, EssenceContainerData, 0);
557 9b9466b0 Vitor Sessak
}
558
559 064abb8a Baptiste Coudurier
static void mxf_write_track(AVFormatContext *s, AVStream *st, enum MXFMetadataSetType type)
560 6986662b Vitor Sessak
{
561 5a897cfa Baptiste Coudurier
    MXFContext *mxf = s->priv_data;
562 6986662b Vitor Sessak
    ByteIOContext *pb = s->pb;
563 ac0ab12f Baptiste Coudurier
    MXFStreamContext *sc = st->priv_data;
564 6986662b Vitor Sessak
565
    mxf_write_metadata_key(pb, 0x013b00);
566
    PRINT_KEY(s, "track key", pb->buf_ptr - 16);
567
    klv_encode_ber_length(pb, 80);
568
569
    // write track uid
570
    mxf_write_local_tag(pb, 16, 0x3C0A);
571 064abb8a Baptiste Coudurier
    mxf_write_uuid(pb, type == MaterialPackage ? Track : Track + TypeBottom, st->index);
572 6986662b Vitor Sessak
    PRINT_KEY(s, "track uid", pb->buf_ptr - 16);
573 a8f6e362 Baptiste Coudurier
574 6986662b Vitor Sessak
    // write track id
575
    mxf_write_local_tag(pb, 4, 0x4801);
576 064abb8a Baptiste Coudurier
    put_be32(pb, st->index);
577 6986662b Vitor Sessak
578 b8ad3f78 Baptiste Coudurier
    // write track number
579 6986662b Vitor Sessak
    mxf_write_local_tag(pb, 4, 0x4804);
580 b8ad3f78 Baptiste Coudurier
    if (type == MaterialPackage)
581 6986662b Vitor Sessak
        put_be32(pb, 0); // track number of material package is 0
582 b8ad3f78 Baptiste Coudurier
    else
583
        put_buffer(pb, sc->track_essence_element_key + 12, 4);
584 6986662b Vitor Sessak
585
    mxf_write_local_tag(pb, 8, 0x4B01);
586 5a897cfa Baptiste Coudurier
    put_be32(pb, mxf->time_base.den);
587
    put_be32(pb, mxf->time_base.num);
588 6986662b Vitor Sessak
589
    // write origin
590
    mxf_write_local_tag(pb, 8, 0x4B02);
591
    put_be64(pb, 0);
592
593
    // write sequence refs
594
    mxf_write_local_tag(pb, 16, 0x4803);
595 064abb8a Baptiste Coudurier
    mxf_write_uuid(pb, type == MaterialPackage ? Sequence: Sequence + TypeBottom, st->index);
596 6986662b Vitor Sessak
}
597
598 7c9668cf Baptiste Coudurier
static const uint8_t smpte_12m_timecode_track_data_ul[] = { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x01,0x03,0x02,0x01,0x01,0x00,0x00,0x00 };
599
600
static void mxf_write_common_fields(AVFormatContext *s, AVStream *st)
601 fda0d4af Zhentan Feng
{
602 7c9668cf Baptiste Coudurier
    MXFContext *mxf = s->priv_data;
603
    ByteIOContext *pb = s->pb;
604 cdedd878 Baptiste Coudurier
605 fda0d4af Zhentan Feng
    // find data define uls
606
    mxf_write_local_tag(pb, 16, 0x0201);
607 7c9668cf Baptiste Coudurier
    if (st == mxf->timecode_track)
608
        put_buffer(pb, smpte_12m_timecode_track_data_ul, 16);
609
    else {
610
        const MXFCodecUL *data_def_ul = mxf_get_data_definition_ul(st->codec->codec_type);
611
        put_buffer(pb, data_def_ul->uid, 16);
612
    }
613 fda0d4af Zhentan Feng
614
    // write duration
615
    mxf_write_local_tag(pb, 8, 0x0202);
616 7c9668cf Baptiste Coudurier
    put_be64(pb, mxf->duration);
617 fda0d4af Zhentan Feng
}
618
619 064abb8a Baptiste Coudurier
static void mxf_write_sequence(AVFormatContext *s, AVStream *st, enum MXFMetadataSetType type)
620 6986662b Vitor Sessak
{
621 7c9668cf Baptiste Coudurier
    MXFContext *mxf = s->priv_data;
622 6986662b Vitor Sessak
    ByteIOContext *pb = s->pb;
623 7c9668cf Baptiste Coudurier
    enum MXFMetadataSetType component;
624 6986662b Vitor Sessak
625
    mxf_write_metadata_key(pb, 0x010f00);
626
    PRINT_KEY(s, "sequence key", pb->buf_ptr - 16);
627
    klv_encode_ber_length(pb, 80);
628
629
    mxf_write_local_tag(pb, 16, 0x3C0A);
630 064abb8a Baptiste Coudurier
    mxf_write_uuid(pb, type == MaterialPackage ? Sequence: Sequence + TypeBottom, st->index);
631 6986662b Vitor Sessak
632
    PRINT_KEY(s, "sequence uid", pb->buf_ptr - 16);
633 7c9668cf Baptiste Coudurier
    mxf_write_common_fields(s, st);
634 6986662b Vitor Sessak
635
    // write structural component
636
    mxf_write_local_tag(pb, 16 + 8, 0x1001);
637
    mxf_write_refs_count(pb, 1);
638 7c9668cf Baptiste Coudurier
    if (st == mxf->timecode_track)
639
        component = TimecodeComponent;
640
    else if (type == MaterialPackage)
641
        component = SourceClip;
642
    else
643
        component = SourceClip+TypeBottom;
644
    mxf_write_uuid(pb, component, st->index);
645
}
646
647
static void mxf_write_timecode_component(AVFormatContext *s, AVStream *st)
648
{
649
    MXFContext *mxf = s->priv_data;
650
    ByteIOContext *pb = s->pb;
651
652
    mxf_write_metadata_key(pb, 0x011400);
653
    klv_encode_ber_length(pb, 75);
654
655
    // UID
656
    mxf_write_local_tag(pb, 16, 0x3C0A);
657
    mxf_write_uuid(pb, TimecodeComponent, st->index);
658
659
    mxf_write_common_fields(s, st);
660
661
    // Start Time Code
662
    mxf_write_local_tag(pb, 8, 0x1501);
663
    put_be64(pb, mxf->timecode_start);
664
665
    // Rounded Time Code Base
666
    mxf_write_local_tag(pb, 2, 0x1502);
667
    put_be16(pb, mxf->timecode_base);
668
669
    // Drop Frame
670
    mxf_write_local_tag(pb, 1, 0x1503);
671
    put_byte(pb, mxf->timecode_drop_frame);
672 6986662b Vitor Sessak
}
673
674 064abb8a Baptiste Coudurier
static void mxf_write_structural_component(AVFormatContext *s, AVStream *st, enum MXFMetadataSetType type)
675 6986662b Vitor Sessak
{
676
    ByteIOContext *pb = s->pb;
677
    int i;
678
679
    mxf_write_metadata_key(pb, 0x011100);
680
    PRINT_KEY(s, "sturctural component key", pb->buf_ptr - 16);
681
    klv_encode_ber_length(pb, 108);
682
683
    // write uid
684
    mxf_write_local_tag(pb, 16, 0x3C0A);
685 064abb8a Baptiste Coudurier
    mxf_write_uuid(pb, type == MaterialPackage ? SourceClip: SourceClip + TypeBottom, st->index);
686 6986662b Vitor Sessak
687
    PRINT_KEY(s, "structural component uid", pb->buf_ptr - 16);
688 7c9668cf Baptiste Coudurier
    mxf_write_common_fields(s, st);
689 6986662b Vitor Sessak
690
    // write start_position
691
    mxf_write_local_tag(pb, 8, 0x1201);
692
    put_be64(pb, 0);
693
694 a8f6e362 Baptiste Coudurier
    // write source package uid, end of the reference
695 6986662b Vitor Sessak
    mxf_write_local_tag(pb, 32, 0x1101);
696
    if (type == SourcePackage) {
697 a8f6e362 Baptiste Coudurier
        for (i = 0; i < 4; i++)
698 6986662b Vitor Sessak
            put_be64(pb, 0);
699
    } else
700
        mxf_write_umid(pb, SourcePackage, 0);
701
702 a8f6e362 Baptiste Coudurier
    // write source track id
703 6986662b Vitor Sessak
    mxf_write_local_tag(pb, 4, 0x1102);
704
    if (type == SourcePackage)
705
        put_be32(pb, 0);
706
    else
707 064abb8a Baptiste Coudurier
        put_be32(pb, st->index);
708 6986662b Vitor Sessak
}
709
710 9b9466b0 Vitor Sessak
static void mxf_write_multi_descriptor(AVFormatContext *s)
711
{
712 034fcddf Baptiste Coudurier
    MXFContext *mxf = s->priv_data;
713 9b9466b0 Vitor Sessak
    ByteIOContext *pb = s->pb;
714 bd3f81f3 Baptiste Coudurier
    const uint8_t *ul;
715 9b9466b0 Vitor Sessak
    int i;
716
717
    mxf_write_metadata_key(pb, 0x014400);
718
    PRINT_KEY(s, "multiple descriptor key", pb->buf_ptr - 16);
719
    klv_encode_ber_length(pb, 64 + 16 * s->nb_streams);
720
721
    mxf_write_local_tag(pb, 16, 0x3C0A);
722
    mxf_write_uuid(pb, MultipleDescriptor, 0);
723
    PRINT_KEY(s, "multi_desc uid", pb->buf_ptr - 16);
724
725
    // write sample rate
726
    mxf_write_local_tag(pb, 8, 0x3001);
727 034fcddf Baptiste Coudurier
    put_be32(pb, mxf->time_base.den);
728
    put_be32(pb, mxf->time_base.num);
729 9b9466b0 Vitor Sessak
730
    // write essence container ul
731
    mxf_write_local_tag(pb, 16, 0x3004);
732 bd3f81f3 Baptiste Coudurier
    if (mxf->essence_container_count > 1)
733
        ul = multiple_desc_ul;
734
    else
735
        ul = mxf_essence_container_uls[mxf->essence_containers_indices[0]].container_ul;
736
    put_buffer(pb, ul, 16);
737 9b9466b0 Vitor Sessak
738
    // write sub descriptor refs
739
    mxf_write_local_tag(pb, s->nb_streams * 16 + 8, 0x3F01);
740
    mxf_write_refs_count(pb, s->nb_streams);
741 a8f6e362 Baptiste Coudurier
    for (i = 0; i < s->nb_streams; i++)
742 9b9466b0 Vitor Sessak
        mxf_write_uuid(pb, SubDescriptor, i);
743
}
744
745 034fcddf Baptiste Coudurier
static void mxf_write_generic_desc(AVFormatContext *s, AVStream *st, const UID key, unsigned size)
746 fda0d4af Zhentan Feng
{
747 034fcddf Baptiste Coudurier
    MXFContext *mxf = s->priv_data;
748 a2f55f22 Baptiste Coudurier
    MXFStreamContext *sc = st->priv_data;
749 034fcddf Baptiste Coudurier
    ByteIOContext *pb = s->pb;
750 fda0d4af Zhentan Feng
751 f2ebd159 Baptiste Coudurier
    put_buffer(pb, key, 16);
752 95900588 Baptiste Coudurier
    klv_encode_ber_length(pb, size);
753 fda0d4af Zhentan Feng
754
    mxf_write_local_tag(pb, 16, 0x3C0A);
755
    mxf_write_uuid(pb, SubDescriptor, st->index);
756
757
    mxf_write_local_tag(pb, 4, 0x3006);
758
    put_be32(pb, st->index);
759
760 9b9466b0 Vitor Sessak
    mxf_write_local_tag(pb, 8, 0x3001);
761 034fcddf Baptiste Coudurier
    put_be32(pb, mxf->time_base.den);
762
    put_be32(pb, mxf->time_base.num);
763 9b9466b0 Vitor Sessak
764 fda0d4af Zhentan Feng
    mxf_write_local_tag(pb, 16, 0x3004);
765 d4ffc57d Baptiste Coudurier
    put_buffer(pb, mxf_essence_container_uls[sc->index].container_ul, 16);
766 fda0d4af Zhentan Feng
}
767
768 f2ebd159 Baptiste Coudurier
static const UID mxf_mpegvideo_descriptor_key = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x51,0x00 };
769
static const UID mxf_wav_descriptor_key       = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x48,0x00 };
770 baed2ec4 Baptiste Coudurier
static const UID mxf_aes3_descriptor_key      = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x47,0x00 };
771 bd3f81f3 Baptiste Coudurier
static const UID mxf_cdci_descriptor_key      = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0D,0x01,0x01,0x01,0x01,0x01,0x28,0x00 };
772
static const UID mxf_generic_sound_descriptor_key = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0D,0x01,0x01,0x01,0x01,0x01,0x42,0x00 };
773 f2ebd159 Baptiste Coudurier
774 db6db7e5 Baptiste Coudurier
static void mxf_write_cdci_common(AVFormatContext *s, AVStream *st, const UID key, unsigned size)
775 fda0d4af Zhentan Feng
{
776 383dde71 Baptiste Coudurier
    MXFStreamContext *sc = st->priv_data;
777 fda0d4af Zhentan Feng
    ByteIOContext *pb = s->pb;
778 24d6a613 Baptiste Coudurier
    int stored_height = (st->codec->height+15)/16*16;
779 6249143d Baptiste Coudurier
    int display_height;
780 5e4aa542 Baptiste Coudurier
    AVRational dar;
781 74334ae1 Baptiste Coudurier
    int f1, f2;
782 fda0d4af Zhentan Feng
783 db6db7e5 Baptiste Coudurier
    mxf_write_generic_desc(s, st, key, size);
784 fda0d4af Zhentan Feng
785
    mxf_write_local_tag(pb, 4, 0x3203);
786
    put_be32(pb, st->codec->width);
787
788
    mxf_write_local_tag(pb, 4, 0x3202);
789 383dde71 Baptiste Coudurier
    put_be32(pb, stored_height>>sc->interlaced);
790 fda0d4af Zhentan Feng
791 724e7d8d Baptiste Coudurier
    mxf_write_local_tag(pb, 4, 0x3209);
792
    put_be32(pb, st->codec->width);
793
794 6249143d Baptiste Coudurier
    if (st->codec->height == 608) // PAL + VBI
795
        display_height = 576;
796
    else if (st->codec->height == 512)  // NTSC + VBI
797
        display_height = 486;
798
    else
799
        display_height = st->codec->height;
800
801 724e7d8d Baptiste Coudurier
    mxf_write_local_tag(pb, 4, 0x3208);
802 6249143d Baptiste Coudurier
    put_be32(pb, display_height>>sc->interlaced);
803 724e7d8d Baptiste Coudurier
804 ee1f7f4b Baptiste Coudurier
    // component depth
805
    mxf_write_local_tag(pb, 4, 0x3301);
806
    put_be32(pb, 8);
807
808
    // horizontal subsampling
809
    mxf_write_local_tag(pb, 4, 0x3302);
810
    put_be32(pb, 2);
811
812 4118d68f Baptiste Coudurier
    // frame layout
813
    mxf_write_local_tag(pb, 1, 0x320C);
814
    put_byte(pb, sc->interlaced);
815
816 74334ae1 Baptiste Coudurier
    // video line map
817
    switch (st->codec->height) {
818
    case  576: f1 = 23; f2 = 336; break;
819
    case  608: f1 =  7; f2 = 320; break;
820
    case  480: f1 = 20; f2 = 283; break;
821
    case  512: f1 =  7; f2 = 270; break;
822
    case  720: f1 = 26; f2 =   0; break; // progressive
823
    case 1080: f1 = 21; f2 = 584; break;
824
    default:   f1 =  0; f2 =   0; break;
825
    }
826
827
    if (!sc->interlaced) {
828
        f2  = 0;
829
        f1 *= 2;
830
    }
831
832 ed00d0c1 Baptiste Coudurier
    mxf_write_local_tag(pb, 12+sc->interlaced*4, 0x320D);
833 8ed82d81 Baptiste Coudurier
    put_be32(pb, sc->interlaced ? 2 : 1);
834
    put_be32(pb, 4);
835 74334ae1 Baptiste Coudurier
    put_be32(pb, f1);
836 8ed82d81 Baptiste Coudurier
    if (sc->interlaced)
837
        put_be32(pb, f2);
838 74334ae1 Baptiste Coudurier
839 5e4aa542 Baptiste Coudurier
    av_reduce(&dar.num, &dar.den,
840
              st->codec->width*st->codec->sample_aspect_ratio.num,
841
              st->codec->height*st->codec->sample_aspect_ratio.den,
842
              1024*1024);
843
844 fda0d4af Zhentan Feng
    mxf_write_local_tag(pb, 8, 0x320E);
845 5e4aa542 Baptiste Coudurier
    put_be32(pb, dar.num);
846
    put_be32(pb, dar.den);
847 221ef8ca Baptiste Coudurier
848
    mxf_write_local_tag(pb, 16, 0x3201);
849
    put_buffer(pb, *sc->codec_ul, 16);
850 fda0d4af Zhentan Feng
}
851
852 bd3f81f3 Baptiste Coudurier
static void mxf_write_cdci_desc(AVFormatContext *s, AVStream *st)
853
{
854
    MXFStreamContext *sc = st->priv_data;
855
    mxf_write_cdci_common(s, st, mxf_cdci_descriptor_key, 161+sc->interlaced*4);
856
}
857
858 db6db7e5 Baptiste Coudurier
static void mxf_write_mpegvideo_desc(AVFormatContext *s, AVStream *st)
859
{
860
    MXFStreamContext *sc = st->priv_data;
861
    ByteIOContext *pb = s->pb;
862
863
    mxf_write_cdci_common(s, st, mxf_mpegvideo_descriptor_key, 169+sc->interlaced*4);
864
865
    // bit rate
866
    mxf_write_local_tag(pb, 4, 0x8000);
867
    put_be32(pb, st->codec->bit_rate);
868
}
869
870
static void mxf_write_generic_sound_common(AVFormatContext *s, AVStream *st, const UID key, unsigned size)
871 fda0d4af Zhentan Feng
{
872
    ByteIOContext *pb = s->pb;
873
874 034fcddf Baptiste Coudurier
    mxf_write_generic_desc(s, st, key, size);
875 3f2795ed Baptiste Coudurier
876 c6b579df Baptiste Coudurier
    // audio locked
877 3f2795ed Baptiste Coudurier
    mxf_write_local_tag(pb, 1, 0x3D02);
878
    put_byte(pb, 1);
879 fda0d4af Zhentan Feng
880
    // write audio sampling rate
881
    mxf_write_local_tag(pb, 8, 0x3D03);
882
    put_be32(pb, st->codec->sample_rate);
883
    put_be32(pb, 1);
884
885
    mxf_write_local_tag(pb, 4, 0x3D07);
886
    put_be32(pb, st->codec->channels);
887
888
    mxf_write_local_tag(pb, 4, 0x3D01);
889 822617cd Baptiste Coudurier
    put_be32(pb, av_get_bits_per_sample(st->codec->codec_id));
890 fda0d4af Zhentan Feng
}
891
892 db6db7e5 Baptiste Coudurier
static void mxf_write_wav_common(AVFormatContext *s, AVStream *st, const UID key, unsigned size)
893 c6b579df Baptiste Coudurier
{
894 f9303ffd Baptiste Coudurier
    ByteIOContext *pb = s->pb;
895
896 db6db7e5 Baptiste Coudurier
    mxf_write_generic_sound_common(s, st, key, size);
897 f9303ffd Baptiste Coudurier
898
    mxf_write_local_tag(pb, 2, 0x3D0A);
899
    put_be16(pb, st->codec->block_align);
900
901
    // avg bytes per sec
902
    mxf_write_local_tag(pb, 4, 0x3D09);
903
    put_be32(pb, st->codec->block_align*st->codec->sample_rate);
904 c6b579df Baptiste Coudurier
}
905
906 0290074c Baptiste Coudurier
static void mxf_write_wav_desc(AVFormatContext *s, AVStream *st)
907 baed2ec4 Baptiste Coudurier
{
908 db6db7e5 Baptiste Coudurier
    mxf_write_wav_common(s, st, mxf_wav_descriptor_key, 107);
909 0290074c Baptiste Coudurier
}
910 f9303ffd Baptiste Coudurier
911 0290074c Baptiste Coudurier
static void mxf_write_aes3_desc(AVFormatContext *s, AVStream *st)
912
{
913 db6db7e5 Baptiste Coudurier
    mxf_write_wav_common(s, st, mxf_aes3_descriptor_key, 107);
914 baed2ec4 Baptiste Coudurier
}
915
916 bd3f81f3 Baptiste Coudurier
static void mxf_write_generic_sound_desc(AVFormatContext *s, AVStream *st)
917
{
918
    mxf_write_generic_sound_common(s, st, mxf_generic_sound_descriptor_key, 93);
919
}
920
921 98f7c5e0 Baptiste Coudurier
static void mxf_write_package(AVFormatContext *s, enum MXFMetadataSetType type)
922
{
923 f5b28890 Baptiste Coudurier
    MXFContext *mxf = s->priv_data;
924 98f7c5e0 Baptiste Coudurier
    ByteIOContext *pb = s->pb;
925 7c9668cf Baptiste Coudurier
    int i, track_count;
926 98f7c5e0 Baptiste Coudurier
927
    if (type == MaterialPackage) {
928 7c9668cf Baptiste Coudurier
        track_count = s->nb_streams + 1; // add timecode track
929 98f7c5e0 Baptiste Coudurier
        mxf_write_metadata_key(pb, 0x013600);
930
        PRINT_KEY(s, "Material Package key", pb->buf_ptr - 16);
931 7c9668cf Baptiste Coudurier
        klv_encode_ber_length(pb, 92 + 16*track_count);
932 98f7c5e0 Baptiste Coudurier
    } else {
933 7c9668cf Baptiste Coudurier
        track_count = s->nb_streams;
934 98f7c5e0 Baptiste Coudurier
        mxf_write_metadata_key(pb, 0x013700);
935
        PRINT_KEY(s, "Source Package key", pb->buf_ptr - 16);
936 7c9668cf Baptiste Coudurier
        klv_encode_ber_length(pb, 112 + 16*track_count); // 20 bytes length for descriptor reference
937 98f7c5e0 Baptiste Coudurier
    }
938
939
    // write uid
940
    mxf_write_local_tag(pb, 16, 0x3C0A);
941
    mxf_write_uuid(pb, type, 0);
942
    av_log(s,AV_LOG_DEBUG, "package type:%d\n", type);
943
    PRINT_KEY(s, "package uid", pb->buf_ptr - 16);
944
945
    // write package umid
946
    mxf_write_local_tag(pb, 32, 0x4401);
947
    mxf_write_umid(pb, type, 0);
948
    PRINT_KEY(s, "package umid second part", pb->buf_ptr - 16);
949
950 f5b28890 Baptiste Coudurier
    // package creation date
951 98f7c5e0 Baptiste Coudurier
    mxf_write_local_tag(pb, 8, 0x4405);
952 f5b28890 Baptiste Coudurier
    put_be64(pb, mxf->timestamp);
953 98f7c5e0 Baptiste Coudurier
954 f5b28890 Baptiste Coudurier
    // package modified date
955 98f7c5e0 Baptiste Coudurier
    mxf_write_local_tag(pb, 8, 0x4404);
956 f5b28890 Baptiste Coudurier
    put_be64(pb, mxf->timestamp);
957 98f7c5e0 Baptiste Coudurier
958
    // write track refs
959 7c9668cf Baptiste Coudurier
    mxf_write_local_tag(pb, track_count*16 + 8, 0x4403);
960
    mxf_write_refs_count(pb, track_count);
961 98f7c5e0 Baptiste Coudurier
    for (i = 0; i < s->nb_streams; i++)
962
        mxf_write_uuid(pb, type == MaterialPackage ? Track : Track + TypeBottom, i);
963 7c9668cf Baptiste Coudurier
    if (type == MaterialPackage)
964
        mxf_write_uuid(pb, Track, s->nb_streams); // timecode track
965 98f7c5e0 Baptiste Coudurier
966
    // write multiple descriptor reference
967
    if (type == SourcePackage) {
968
        mxf_write_local_tag(pb, 16, 0x4701);
969 b1878364 Baptiste Coudurier
        if (s->nb_streams > 1) {
970
            mxf_write_uuid(pb, MultipleDescriptor, 0);
971
            mxf_write_multi_descriptor(s);
972
        } else
973
            mxf_write_uuid(pb, SubDescriptor, 0);
974 7c9668cf Baptiste Coudurier
    } else {
975
        // write timecode track
976
        mxf_write_track(s, mxf->timecode_track, type);
977
        mxf_write_sequence(s, mxf->timecode_track, type);
978
        mxf_write_timecode_component(s, mxf->timecode_track);
979 6ae9d3fa Baptiste Coudurier
    }
980 fda0d4af Zhentan Feng
981 38573e8f Baptiste Coudurier
    for (i = 0; i < s->nb_streams; i++) {
982 064abb8a Baptiste Coudurier
        AVStream *st = s->streams[i];
983
        mxf_write_track(s, st, type);
984
        mxf_write_sequence(s, st, type);
985
        mxf_write_structural_component(s, st, type);
986 fda0d4af Zhentan Feng
987
        if (type == SourcePackage) {
988 f2ebd159 Baptiste Coudurier
            MXFStreamContext *sc = st->priv_data;
989 d4ffc57d Baptiste Coudurier
            mxf_essence_container_uls[sc->index].write_desc(s, st);
990 fda0d4af Zhentan Feng
        }
991
    }
992
}
993
994 6e4668b8 Baptiste Coudurier
static int mxf_write_essence_container_data(AVFormatContext *s)
995
{
996
    ByteIOContext *pb = s->pb;
997
998
    mxf_write_metadata_key(pb, 0x012300);
999 0e9dbd96 Baptiste Coudurier
    klv_encode_ber_length(pb, 72);
1000 6e4668b8 Baptiste Coudurier
1001
    mxf_write_local_tag(pb, 16, 0x3C0A); // Instance UID
1002
    mxf_write_uuid(pb, EssenceContainerData, 0);
1003
1004
    mxf_write_local_tag(pb, 32, 0x2701); // Linked Package UID
1005
    mxf_write_umid(pb, SourcePackage, 0);
1006
1007
    mxf_write_local_tag(pb, 4, 0x3F07); // BodySID
1008
    put_be32(pb, 1);
1009
1010 0e9dbd96 Baptiste Coudurier
    mxf_write_local_tag(pb, 4, 0x3F06); // IndexSID
1011
    put_be32(pb, 2);
1012
1013 6e4668b8 Baptiste Coudurier
    return 0;
1014
}
1015
1016 2c8d4388 Zhentan Feng
static int mxf_write_header_metadata_sets(AVFormatContext *s)
1017
{
1018 a8f6e362 Baptiste Coudurier
    mxf_write_preface(s);
1019 9b9466b0 Vitor Sessak
    mxf_write_identification(s);
1020
    mxf_write_content_storage(s);
1021 f05773ea Baptiste Coudurier
    mxf_write_package(s, MaterialPackage);
1022
    mxf_write_package(s, SourcePackage);
1023 6e4668b8 Baptiste Coudurier
    mxf_write_essence_container_data(s);
1024 2c8d4388 Zhentan Feng
    return 0;
1025
}
1026
1027 d526105f Baptiste Coudurier
static unsigned klv_fill_size(uint64_t size)
1028 85d0d114 Baptiste Coudurier
{
1029 d526105f Baptiste Coudurier
    unsigned pad = KAG_SIZE - (size & (KAG_SIZE-1));
1030 baaf7b1b Baptiste Coudurier
    if (pad < 20) // smallest fill item possible
1031 85d0d114 Baptiste Coudurier
        return pad + KAG_SIZE;
1032
    else
1033
        return pad & (KAG_SIZE-1);
1034
}
1035
1036 59453725 Baptiste Coudurier
static void mxf_write_index_table_segment(AVFormatContext *s)
1037 0e9dbd96 Baptiste Coudurier
{
1038
    MXFContext *mxf = s->priv_data;
1039
    ByteIOContext *pb = s->pb;
1040 59453725 Baptiste Coudurier
    int i, j;
1041 0e9dbd96 Baptiste Coudurier
    int temporal_reordering = 0;
1042 0cfa3596 Baptiste Coudurier
    int key_index = mxf->last_key_index;
1043 0e9dbd96 Baptiste Coudurier
1044
    av_log(s, AV_LOG_DEBUG, "edit units count %d\n", mxf->edit_units_count);
1045
1046 bd3f81f3 Baptiste Coudurier
    if (!mxf->edit_units_count && !mxf->edit_unit_byte_count)
1047 99b170f8 Baptiste Coudurier
        return;
1048
1049 0e9dbd96 Baptiste Coudurier
    put_buffer(pb, index_table_segment_key, 16);
1050 bd3f81f3 Baptiste Coudurier
1051
    if (mxf->edit_unit_byte_count) {
1052
        klv_encode_ber_length(pb, 85);
1053
    } else {
1054
        klv_encode_ber_length(pb, 85 + 12+(s->nb_streams+1)*6 +
1055
                              12+mxf->edit_units_count*(11+mxf->slice_count*4));
1056
    }
1057 0e9dbd96 Baptiste Coudurier
1058
    // instance id
1059
    mxf_write_local_tag(pb, 16, 0x3C0A);
1060
    mxf_write_uuid(pb, IndexTableSegment, 0);
1061
1062
    // index edit rate
1063
    mxf_write_local_tag(pb, 8, 0x3F0B);
1064
    put_be32(pb, mxf->time_base.num);
1065
    put_be32(pb, mxf->time_base.den);
1066
1067
    // index start position
1068
    mxf_write_local_tag(pb, 8, 0x3F0C);
1069 99b170f8 Baptiste Coudurier
    put_be64(pb, mxf->last_indexed_edit_unit);
1070 0e9dbd96 Baptiste Coudurier
1071
    // index duration
1072
    mxf_write_local_tag(pb, 8, 0x3F0D);
1073
    put_be64(pb, mxf->edit_units_count);
1074
1075
    // edit unit byte count
1076
    mxf_write_local_tag(pb, 4, 0x3F05);
1077 bd3f81f3 Baptiste Coudurier
    put_be32(pb, mxf->edit_unit_byte_count);
1078 0e9dbd96 Baptiste Coudurier
1079
    // index sid
1080
    mxf_write_local_tag(pb, 4, 0x3F06);
1081
    put_be32(pb, 2);
1082
1083
    // body sid
1084
    mxf_write_local_tag(pb, 4, 0x3F07);
1085
    put_be32(pb, 1);
1086
1087 d526105f Baptiste Coudurier
    // real slice count - 1
1088 0e9dbd96 Baptiste Coudurier
    mxf_write_local_tag(pb, 1, 0x3F08);
1089 d526105f Baptiste Coudurier
    put_byte(pb, mxf->slice_count);
1090 0e9dbd96 Baptiste Coudurier
1091 bd3f81f3 Baptiste Coudurier
    if (!mxf->edit_unit_byte_count) {
1092 8d1a5af7 Baptiste Coudurier
        // delta entry array
1093
        mxf_write_local_tag(pb, 8 + (s->nb_streams+1)*6, 0x3F09);
1094
        put_be32(pb, s->nb_streams+1); // num of entries
1095
        put_be32(pb, 6);               // size of one entry
1096
        // write system item delta entry
1097
        put_byte(pb, 0);
1098
        put_byte(pb, 0); // slice entry
1099
        put_be32(pb, 0); // element delta
1100
        for (i = 0; i < s->nb_streams; i++) {
1101
            AVStream *st = s->streams[i];
1102
            MXFStreamContext *sc = st->priv_data;
1103
            put_byte(pb, sc->temporal_reordering);
1104
            if (sc->temporal_reordering)
1105
                temporal_reordering = 1;
1106
            if (i == 0) { // video track
1107
                put_byte(pb, 0); // slice number
1108
                put_be32(pb, KAG_SIZE); // system item size including klv fill
1109
            } else { // audio track
1110
                unsigned audio_frame_size = sc->aic.samples[0]*sc->aic.sample_size;
1111
                audio_frame_size += klv_fill_size(audio_frame_size);
1112
                put_byte(pb, 1);
1113
                put_be32(pb, (i-1)*audio_frame_size); // element delta
1114
            }
1115 d526105f Baptiste Coudurier
        }
1116 0e9dbd96 Baptiste Coudurier
1117 8d1a5af7 Baptiste Coudurier
        mxf_write_local_tag(pb, 8 + mxf->edit_units_count*(11+mxf->slice_count*4), 0x3F0A);
1118
        put_be32(pb, mxf->edit_units_count);  // num of entries
1119
        put_be32(pb, 11+mxf->slice_count*4);  // size of one entry
1120
        for (i = 0; i < mxf->edit_units_count; i++) {
1121
            if (temporal_reordering) {
1122
                int temporal_offset = 0;
1123
                for (j = i+1; j < mxf->edit_units_count; j++) {
1124
                    temporal_offset++;
1125
                    if (mxf->index_entries[j].flags & 0x10) { // backward prediction
1126
                        // next is not b, so is reordered
1127
                        if (!(mxf->index_entries[i+1].flags & 0x10)) {
1128
                            if ((mxf->index_entries[i].flags & 0x11) == 0) // i frame
1129
                                temporal_offset = 0;
1130
                            else
1131
                                temporal_offset = -temporal_offset;
1132
                        }
1133
                        break;
1134 a4397fa9 Baptiste Coudurier
                    }
1135 0e9dbd96 Baptiste Coudurier
                }
1136 8d1a5af7 Baptiste Coudurier
                put_byte(pb, temporal_offset);
1137
            } else
1138
                put_byte(pb, 0);
1139
            if (!(mxf->index_entries[i].flags & 0x33)) { // I frame
1140 99b170f8 Baptiste Coudurier
                mxf->last_key_index = key_index;
1141 8d1a5af7 Baptiste Coudurier
                key_index = i;
1142
            }
1143
            if (mxf->index_entries[i].flags & 0x10 && // backward prediction
1144
                !(mxf->index_entries[key_index].flags & 0x80)) { // open gop
1145
                put_byte(pb, mxf->last_key_index - i);
1146
            } else {
1147
                put_byte(pb, key_index - i); // key frame offset
1148
                if ((mxf->index_entries[i].flags & 0x20) == 0x20) // only forward
1149
                    mxf->last_key_index = key_index;
1150
            }
1151
            put_byte(pb, mxf->index_entries[i].flags);
1152
            // stream offset
1153
            put_be64(pb, mxf->index_entries[i].offset - mxf->first_edit_unit_offset);
1154
            if (s->nb_streams > 1)
1155
                put_be32(pb, mxf->index_entries[i].slice_offset);
1156 1c7c6669 Baptiste Coudurier
        }
1157 99b170f8 Baptiste Coudurier
1158 8d1a5af7 Baptiste Coudurier
        mxf->last_key_index = key_index - mxf->edit_units_count;
1159
        mxf->last_indexed_edit_unit += mxf->edit_units_count;
1160
        mxf->edit_units_count = 0;
1161 bd3f81f3 Baptiste Coudurier
    }
1162 0e9dbd96 Baptiste Coudurier
}
1163
1164 b25ee3fb Baptiste Coudurier
static void mxf_write_klv_fill(AVFormatContext *s)
1165
{
1166
    unsigned pad = klv_fill_size(url_ftell(s->pb));
1167
    if (pad) {
1168
        put_buffer(s->pb, klv_fill_key, 16);
1169 baaf7b1b Baptiste Coudurier
        pad -= 16 + 4;
1170
        klv_encode_ber4_length(s->pb, pad);
1171 b25ee3fb Baptiste Coudurier
        for (; pad; pad--)
1172
            put_byte(s->pb, 0);
1173
        assert(!(url_ftell(s->pb) & (KAG_SIZE-1)));
1174
    }
1175
}
1176
1177 832404ce Baptiste Coudurier
static void mxf_write_partition(AVFormatContext *s, int bodysid,
1178 7524f46e Baptiste Coudurier
                                int indexsid,
1179 0e9dbd96 Baptiste Coudurier
                                const uint8_t *key, int write_metadata)
1180 6986662b Vitor Sessak
{
1181
    MXFContext *mxf = s->priv_data;
1182
    ByteIOContext *pb = s->pb;
1183 85b5ee2b Baptiste Coudurier
    int64_t header_byte_count_offset;
1184 7524f46e Baptiste Coudurier
    unsigned index_byte_count = 0;
1185 9c91a0b0 Baptiste Coudurier
    uint64_t partition_offset = url_ftell(pb);
1186 7524f46e Baptiste Coudurier
1187 bd3f81f3 Baptiste Coudurier
    if (!mxf->edit_unit_byte_count && mxf->edit_units_count)
1188
        index_byte_count = 85 + 12+(s->nb_streams+1)*6 +
1189
            12+mxf->edit_units_count*(11+mxf->slice_count*4);
1190
    else if (mxf->edit_unit_byte_count && indexsid)
1191
        index_byte_count = 85;
1192
1193
    if (index_byte_count) {
1194 7524f46e Baptiste Coudurier
        // add encoded ber length
1195
        index_byte_count += 16 + klv_ber_length(index_byte_count);
1196
        index_byte_count += klv_fill_size(index_byte_count);
1197 99b170f8 Baptiste Coudurier
    }
1198
1199
    if (!memcmp(key, body_partition_key, 16)) {
1200
        mxf->body_partition_offset =
1201
            av_realloc(mxf->body_partition_offset,
1202
                       (mxf->body_partitions_count+1)*
1203
                       sizeof(*mxf->body_partition_offset));
1204 9c91a0b0 Baptiste Coudurier
        mxf->body_partition_offset[mxf->body_partitions_count++] = partition_offset;
1205 7524f46e Baptiste Coudurier
    }
1206 a8f6e362 Baptiste Coudurier
1207 6986662b Vitor Sessak
    // write klv
1208
    put_buffer(pb, key, 16);
1209
    klv_encode_ber_length(pb, 88 + 16 * mxf->essence_container_count);
1210
1211
    // write partition value
1212
    put_be16(pb, 1); // majorVersion
1213
    put_be16(pb, 2); // minorVersion
1214 85d0d114 Baptiste Coudurier
    put_be32(pb, KAG_SIZE); // KAGSize
1215 6986662b Vitor Sessak
1216 9c91a0b0 Baptiste Coudurier
    put_be64(pb, partition_offset); // ThisPartition
1217 99b170f8 Baptiste Coudurier
1218
    if (!memcmp(key, body_partition_key, 16) && mxf->body_partitions_count > 1)
1219
        put_be64(pb, mxf->body_partition_offset[mxf->body_partitions_count-2]); // PreviousPartition
1220 cfa3caf8 Baptiste Coudurier
    else if (!memcmp(key, footer_partition_key, 16) && mxf->body_partitions_count)
1221 99b170f8 Baptiste Coudurier
        put_be64(pb, mxf->body_partition_offset[mxf->body_partitions_count-1]); // PreviousPartition
1222
    else
1223
        put_be64(pb, 0);
1224 6986662b Vitor Sessak
1225 cc984060 Baptiste Coudurier
    put_be64(pb, mxf->footer_partition_offset); // footerPartition
1226 6986662b Vitor Sessak
1227
    // set offset
1228 85b5ee2b Baptiste Coudurier
    header_byte_count_offset = url_ftell(pb);
1229 6986662b Vitor Sessak
    put_be64(pb, 0); // headerByteCount, update later
1230
1231 0e9dbd96 Baptiste Coudurier
    // indexTable
1232 832404ce Baptiste Coudurier
    put_be64(pb, index_byte_count); // indexByteCount
1233 7524f46e Baptiste Coudurier
    put_be32(pb, index_byte_count ? indexsid : 0); // indexSID
1234 99b170f8 Baptiste Coudurier
1235
    // BodyOffset
1236 bd3f81f3 Baptiste Coudurier
    if (bodysid && mxf->edit_units_count && mxf->body_partitions_count) {
1237 1585b93a Baptiste Coudurier
        uint64_t partition_end = url_ftell(pb) + 8 + 4 + 16 + 8 +
1238
            16*mxf->essence_container_count;
1239
        put_be64(pb, partition_end + klv_fill_size(partition_end) +
1240
                 index_byte_count - mxf->first_edit_unit_offset);
1241
    } else
1242
        put_be64(pb, 0);
1243 6986662b Vitor Sessak
1244
    put_be32(pb, bodysid); // bodySID
1245 216b6631 Baptiste Coudurier
1246
    // operational pattern
1247
    if (s->nb_streams > 1) {
1248
        put_buffer(pb, op1a_ul, 14);
1249
        put_be16(pb, 0x0900); // multi track
1250
    } else {
1251
        put_buffer(pb, op1a_ul, 16);
1252
    }
1253 6986662b Vitor Sessak
1254
    // essence container
1255 3eaa53b3 Baptiste Coudurier
    mxf_write_essence_container_refs(s);
1256 cc984060 Baptiste Coudurier
1257
    if (write_metadata) {
1258
        // mark the start of the headermetadata and calculate metadata size
1259 7be34052 Baptiste Coudurier
        int64_t pos, start;
1260 85d0d114 Baptiste Coudurier
        unsigned header_byte_count;
1261
1262 b25ee3fb Baptiste Coudurier
        mxf_write_klv_fill(s);
1263 7be34052 Baptiste Coudurier
        start = url_ftell(s->pb);
1264 cc984060 Baptiste Coudurier
        mxf_write_primer_pack(s);
1265
        mxf_write_header_metadata_sets(s);
1266
        pos = url_ftell(s->pb);
1267 d526105f Baptiste Coudurier
        header_byte_count = pos - start + klv_fill_size(pos);
1268 85d0d114 Baptiste Coudurier
1269 cc984060 Baptiste Coudurier
        // update header_byte_count
1270 85b5ee2b Baptiste Coudurier
        url_fseek(pb, header_byte_count_offset, SEEK_SET);
1271 85d0d114 Baptiste Coudurier
        put_be64(pb, header_byte_count);
1272 cc984060 Baptiste Coudurier
        url_fseek(pb, pos, SEEK_SET);
1273
    }
1274
1275
    put_flush_packet(pb);
1276 6986662b Vitor Sessak
}
1277
1278 4d418883 Baptiste Coudurier
static const UID mxf_mpeg2_codec_uls[] = {
1279
    { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x01,0x10,0x00 }, // MP-ML I-Frame
1280
    { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x01,0x11,0x00 }, // MP-ML Long GOP
1281
    { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x02,0x02,0x00 }, // 422P-ML I-Frame
1282
    { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x02,0x03,0x00 }, // 422P-ML Long GOP
1283
    { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x03,0x02,0x00 }, // MP-HL I-Frame
1284
    { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x03,0x03,0x00 }, // MP-HL Long GOP
1285
    { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x04,0x02,0x00 }, // 422P-HL I-Frame
1286
    { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x04,0x03,0x00 }, // 422P-HL Long GOP
1287
};
1288
1289
static const UID *mxf_get_mpeg2_codec_ul(AVCodecContext *avctx)
1290
{
1291
    if (avctx->profile == 4) { // Main
1292
        if (avctx->level == 8) // Main
1293
            return avctx->gop_size ?
1294
                &mxf_mpeg2_codec_uls[1] :
1295
                &mxf_mpeg2_codec_uls[0];
1296
        else if (avctx->level == 4) // High
1297
            return avctx->gop_size ?
1298
                &mxf_mpeg2_codec_uls[5] :
1299
                &mxf_mpeg2_codec_uls[4];
1300
    } else if (avctx->profile == 0) { // 422
1301
        if (avctx->level == 5) // Main
1302
            return avctx->gop_size ?
1303
                &mxf_mpeg2_codec_uls[3] :
1304
                &mxf_mpeg2_codec_uls[2];
1305
        else if (avctx->level == 2) // High
1306
            return avctx->gop_size ?
1307
                &mxf_mpeg2_codec_uls[7] :
1308
                &mxf_mpeg2_codec_uls[6];
1309
    }
1310
    return NULL;
1311
}
1312
1313 4a15f3ee Baptiste Coudurier
static int mxf_parse_mpeg2_frame(AVFormatContext *s, AVStream *st, AVPacket *pkt, int *flags)
1314 7c7c1a76 Baptiste Coudurier
{
1315
    MXFStreamContext *sc = st->priv_data;
1316 7a101190 Baptiste Coudurier
    MXFContext *mxf = s->priv_data;
1317 7c7c1a76 Baptiste Coudurier
    uint32_t c = -1;
1318
    int i;
1319
1320 4a15f3ee Baptiste Coudurier
    *flags = 0;
1321 0e9dbd96 Baptiste Coudurier
1322 7c7c1a76 Baptiste Coudurier
    for(i = 0; i < pkt->size - 4; i++) {
1323
        c = (c<<8) + pkt->data[i];
1324
        if (c == 0x1B5) {
1325
            if (i + 2 < pkt->size && (pkt->data[i+1] & 0xf0) == 0x10) { // seq ext
1326
                st->codec->profile = pkt->data[i+1] & 0x07;
1327
                st->codec->level   = pkt->data[i+2] >> 4;
1328 52e5cd2b Baptiste Coudurier
            } else if (i + 5 < pkt->size && (pkt->data[i+1] & 0xf0) == 0x80) { // pict coding ext
1329
                sc->interlaced = !(pkt->data[i+5] & 0x80); // progressive frame
1330 7c7c1a76 Baptiste Coudurier
                break;
1331
            }
1332 0e9dbd96 Baptiste Coudurier
        } else if (c == 0x1b8) { // gop
1333 7a101190 Baptiste Coudurier
            if (i + 4 < pkt->size) {
1334
                if (pkt->data[i+4]>>6 & 0x01) // closed
1335
                    *flags |= 0x80; // random access
1336
                if (!mxf->header_written) {
1337
                    unsigned hours   =  (pkt->data[i+1]>>2) & 0x1f;
1338
                    unsigned minutes = ((pkt->data[i+1] & 0x03) << 4) | (pkt->data[i+2]>>4);
1339
                    unsigned seconds = ((pkt->data[i+2] & 0x07) << 3) | (pkt->data[i+3]>>5);
1340
                    unsigned frames  = ((pkt->data[i+3] & 0x1f) << 1) | (pkt->data[i+4]>>7);
1341
                    mxf->timecode_drop_frame = !!(pkt->data[i+1] & 0x80);
1342
                    mxf->timecode_start = (hours*3600 + minutes*60 + seconds) *
1343
                        mxf->timecode_base + frames;
1344
                    if (mxf->timecode_drop_frame) {
1345
                        unsigned tminutes = 60 * hours + minutes;
1346
                        mxf->timecode_start -= 2 * (tminutes - tminutes / 10);
1347
                    }
1348
                    av_log(s, AV_LOG_DEBUG, "frame %d %d:%d:%d%c%d\n", mxf->timecode_start,
1349
                           hours, minutes, seconds, mxf->timecode_drop_frame ? ';':':', frames);
1350
                }
1351
            }
1352 0e9dbd96 Baptiste Coudurier
        } else if (c == 0x1b3) { // seq
1353 4a15f3ee Baptiste Coudurier
            *flags |= 0x40;
1354 0e9dbd96 Baptiste Coudurier
        } else if (c == 0x100) { // pic
1355
            int pict_type = (pkt->data[i+2]>>3) & 0x07;
1356
            if (pict_type == 2) { // P frame
1357 4a15f3ee Baptiste Coudurier
                *flags |= 0x22;
1358 0e9dbd96 Baptiste Coudurier
                st->codec->gop_size = 1;
1359
            } else if (pict_type == 3) { // B frame
1360 4a15f3ee Baptiste Coudurier
                *flags |= 0x33;
1361 0e9dbd96 Baptiste Coudurier
                sc->temporal_reordering = -1;
1362
            } else if (!pict_type) {
1363
                av_log(s, AV_LOG_ERROR, "error parsing mpeg2 frame\n");
1364
                return 0;
1365
            }
1366 7c7c1a76 Baptiste Coudurier
        }
1367
    }
1368 bd3f81f3 Baptiste Coudurier
    if (s->oformat != &mxf_d10_muxer)
1369
        sc->codec_ul = mxf_get_mpeg2_codec_ul(st->codec);
1370 7c7c1a76 Baptiste Coudurier
    return !!sc->codec_ul;
1371
}
1372
1373 f5b28890 Baptiste Coudurier
static uint64_t mxf_parse_timestamp(time_t timestamp)
1374
{
1375
    struct tm *time = localtime(&timestamp);
1376
    return (uint64_t)(time->tm_year+1900) << 48 |
1377
           (uint64_t)(time->tm_mon+1)     << 40 |
1378
           (uint64_t) time->tm_mday       << 32 |
1379
                      time->tm_hour       << 24 |
1380
                      time->tm_min        << 16 |
1381
                      time->tm_sec        << 8;
1382
}
1383
1384 4e2f346b Baptiste Coudurier
static int mxf_write_header(AVFormatContext *s)
1385 2c8d4388 Zhentan Feng
{
1386
    MXFContext *mxf = s->priv_data;
1387 d4ffc57d Baptiste Coudurier
    int i;
1388 37d3e066 Aurelien Jacobs
    uint8_t present[FF_ARRAY_ELEMS(mxf_essence_container_uls)] = {0};
1389 cc812c8c Baptiste Coudurier
    const int *samples_per_frame = NULL;
1390 17930876 Baptiste Coudurier
1391
    for (i = 0; i < s->nb_streams; i++) {
1392
        AVStream *st = s->streams[i];
1393 e14d374e Baptiste Coudurier
        MXFStreamContext *sc = av_mallocz(sizeof(*sc));
1394 17930876 Baptiste Coudurier
        if (!sc)
1395
            return AVERROR(ENOMEM);
1396
        st->priv_data = sc;
1397 cc812c8c Baptiste Coudurier
1398
        if (st->codec->codec_type == CODEC_TYPE_VIDEO) {
1399 5ccc2316 Baptiste Coudurier
            if (i != 0) {
1400
                av_log(s, AV_LOG_ERROR, "video stream must be first track\n");
1401
                return -1;
1402
            }
1403 33abc1a7 Baptiste Coudurier
            if (fabs(av_q2d(st->codec->time_base) - 1/25.0) < 0.0001) {
1404 cc812c8c Baptiste Coudurier
                samples_per_frame = PAL_samples_per_frame;
1405
                mxf->time_base = (AVRational){ 1, 25 };
1406 7c9668cf Baptiste Coudurier
                mxf->timecode_base = 25;
1407 33abc1a7 Baptiste Coudurier
            } else if (fabs(av_q2d(st->codec->time_base) - 1001/30000.0) < 0.0001) {
1408 cc812c8c Baptiste Coudurier
                samples_per_frame = NTSC_samples_per_frame;
1409
                mxf->time_base = (AVRational){ 1001, 30000 };
1410 7c9668cf Baptiste Coudurier
                mxf->timecode_base = 30;
1411 cc812c8c Baptiste Coudurier
            } else {
1412
                av_log(s, AV_LOG_ERROR, "unsupported video frame rate\n");
1413
                return -1;
1414
            }
1415 5a897cfa Baptiste Coudurier
            av_set_pts_info(st, 64, mxf->time_base.num, mxf->time_base.den);
1416 bd3f81f3 Baptiste Coudurier
            if (s->oformat == &mxf_d10_muxer) {
1417
                if (st->codec->bit_rate == 50000000)
1418
                    if (mxf->time_base.den == 25) sc->index = 3;
1419
                    else                          sc->index = 5;
1420
                else if (st->codec->bit_rate == 40000000)
1421
                    if (mxf->time_base.den == 25) sc->index = 7;
1422
                    else                          sc->index = 9;
1423
                else if (st->codec->bit_rate == 30000000)
1424
                    if (mxf->time_base.den == 25) sc->index = 11;
1425
                    else                          sc->index = 13;
1426
                else {
1427
                    av_log(s, AV_LOG_ERROR, "error MXF D-10 only support 30/40/50 mbit/s\n");
1428
                    return -1;
1429
                }
1430
1431
                mxf->edit_unit_byte_count = KAG_SIZE; // system element
1432
                mxf->edit_unit_byte_count += 16 + 4 + (uint64_t)st->codec->bit_rate *
1433
                    mxf->time_base.num / (8*mxf->time_base.den);
1434
                mxf->edit_unit_byte_count += klv_fill_size(mxf->edit_unit_byte_count);
1435
                mxf->edit_unit_byte_count += 16 + 4 + 4 + samples_per_frame[0]*8*4;
1436
                mxf->edit_unit_byte_count += klv_fill_size(mxf->edit_unit_byte_count);
1437
            }
1438 cc812c8c Baptiste Coudurier
        } else if (st->codec->codec_type == CODEC_TYPE_AUDIO) {
1439
            if (st->codec->sample_rate != 48000) {
1440
                av_log(s, AV_LOG_ERROR, "only 48khz is implemented\n");
1441
                return -1;
1442
            }
1443 5a897cfa Baptiste Coudurier
            av_set_pts_info(st, 64, 1, st->codec->sample_rate);
1444 bd3f81f3 Baptiste Coudurier
            if (s->oformat == &mxf_d10_muxer) {
1445
                if (st->index != 1) {
1446
                    av_log(s, AV_LOG_ERROR, "MXF D-10 only support one audio track\n");
1447
                    return -1;
1448
                }
1449
                if (st->codec->codec_id != CODEC_ID_PCM_S16LE &&
1450
                    st->codec->codec_id != CODEC_ID_PCM_S24LE) {
1451
                    av_log(s, AV_LOG_ERROR, "MXF D-10 only support 16 or 24 bits le audio\n");
1452
                }
1453
                sc->index = ((MXFStreamContext*)s->streams[0]->priv_data)->index + 1;
1454
            } else
1455 d526105f Baptiste Coudurier
            mxf->slice_count = 1;
1456 cc812c8c Baptiste Coudurier
        }
1457 cdedd878 Baptiste Coudurier
1458 bd3f81f3 Baptiste Coudurier
        if (!sc->index) {
1459 8d1a5af7 Baptiste Coudurier
            sc->index = mxf_get_essence_container_ul_index(st->codec->codec_id);
1460
            if (sc->index == -1) {
1461
                av_log(s, AV_LOG_ERROR, "track %d: could not find essence container ul, "
1462
                       "codec not currently supported in container\n", i);
1463
                return -1;
1464
            }
1465 bd3f81f3 Baptiste Coudurier
        }
1466 4d418883 Baptiste Coudurier
1467 7c7c1a76 Baptiste Coudurier
        sc->codec_ul = &mxf_essence_container_uls[sc->index].codec_ul;
1468 4d418883 Baptiste Coudurier
1469 d4ffc57d Baptiste Coudurier
        if (!present[sc->index]) {
1470
            mxf->essence_containers_indices[mxf->essence_container_count++] = sc->index;
1471
            present[sc->index] = 1;
1472 b8ad3f78 Baptiste Coudurier
        } else
1473 d4ffc57d Baptiste Coudurier
            present[sc->index]++;
1474 8d1a5af7 Baptiste Coudurier
1475 d4ffc57d Baptiste Coudurier
        memcpy(sc->track_essence_element_key, mxf_essence_container_uls[sc->index].element_ul, 15);
1476
        sc->track_essence_element_key[15] = present[sc->index];
1477 b8ad3f78 Baptiste Coudurier
        PRINT_KEY(s, "track essence element key", sc->track_essence_element_key);
1478 17930876 Baptiste Coudurier
    }
1479 9b9466b0 Vitor Sessak
1480 bd3f81f3 Baptiste Coudurier
    if (s->oformat == &mxf_d10_muxer) {
1481
        mxf->essence_container_count = 1;
1482
    }
1483
1484 cc812c8c Baptiste Coudurier
    for (i = 0; i < s->nb_streams; i++) {
1485
        MXFStreamContext *sc = s->streams[i]->priv_data;
1486
        // update element count
1487
        sc->track_essence_element_key[13] = present[sc->index];
1488
        sc->order = AV_RB32(sc->track_essence_element_key+12);
1489
    }
1490
1491 f5b28890 Baptiste Coudurier
    mxf->timestamp = mxf_parse_timestamp(s->timestamp);
1492 7c9668cf Baptiste Coudurier
    mxf->duration = -1;
1493
1494
    mxf->timecode_track = av_mallocz(sizeof(*mxf->timecode_track));
1495
    if (!mxf->timecode_track)
1496
        return AVERROR(ENOMEM);
1497
    mxf->timecode_track->priv_data = av_mallocz(sizeof(MXFStreamContext));
1498
    if (!mxf->timecode_track->priv_data)
1499
        return AVERROR(ENOMEM);
1500
    mxf->timecode_track->index = s->nb_streams;
1501 f5b28890 Baptiste Coudurier
1502 59bcc93b Baptiste Coudurier
    if (!samples_per_frame)
1503
        samples_per_frame = PAL_samples_per_frame;
1504
1505 5a897cfa Baptiste Coudurier
    if (ff_audio_interleave_init(s, samples_per_frame, mxf->time_base) < 0)
1506 cc812c8c Baptiste Coudurier
        return -1;
1507
1508 9b9466b0 Vitor Sessak
    return 0;
1509
}
1510
1511 67d8ac4a Baptiste Coudurier
static const uint8_t system_metadata_pack_key[]        = { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0D,0x01,0x03,0x01,0x04,0x01,0x01,0x00 };
1512
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 };
1513
1514
static uint32_t ff_framenum_to_12m_time_code(unsigned frame, int drop, int fps)
1515
{
1516
    return (0                                    << 31) | // color frame flag
1517
           (0                                    << 30) | // drop  frame flag
1518
           ( ((frame % fps) / 10)                << 28) | // tens  of frames
1519
           ( ((frame % fps) % 10)                << 24) | // units of frames
1520
           (0                                    << 23) | // field phase (NTSC), b0 (PAL)
1521
           ((((frame / fps) % 60) / 10)          << 20) | // tens  of seconds
1522
           ((((frame / fps) % 60) % 10)          << 16) | // units of seconds
1523
           (0                                    << 15) | // b0 (NTSC), b2 (PAL)
1524
           ((((frame / (fps * 60)) % 60) / 10)   << 12) | // tens  of minutes
1525
           ((((frame / (fps * 60)) % 60) % 10)   <<  8) | // units of minutes
1526
           (0                                    <<  7) | // b1
1527
           (0                                    <<  6) | // b2 (NSC), field phase (PAL)
1528
           ((((frame / (fps * 3600) % 24)) / 10) <<  4) | // tens  of hours
1529
           (  (frame / (fps * 3600) % 24)) % 10;          // units of hours
1530
}
1531
1532
static void mxf_write_system_item(AVFormatContext *s)
1533
{
1534
    MXFContext *mxf = s->priv_data;
1535
    ByteIOContext *pb = s->pb;
1536 7c9668cf Baptiste Coudurier
    unsigned frame;
1537 67d8ac4a Baptiste Coudurier
    uint32_t time_code;
1538
1539 7a101190 Baptiste Coudurier
    frame = mxf->timecode_start + mxf->last_indexed_edit_unit + mxf->edit_units_count;
1540 67d8ac4a Baptiste Coudurier
1541
    // write system metadata pack
1542
    put_buffer(pb, system_metadata_pack_key, 16);
1543 e15421fe Baptiste Coudurier
    klv_encode_ber4_length(pb, 57);
1544 67d8ac4a Baptiste Coudurier
    put_byte(pb, 0x5c); // UL, user date/time stamp, picture and sound item present
1545
    put_byte(pb, 0x04); // content package rate
1546
    put_byte(pb, 0x00); // content package type
1547
    put_be16(pb, 0x00); // channel handle
1548
    put_be16(pb, frame); // continuity count
1549
    if (mxf->essence_container_count > 1)
1550
        put_buffer(pb, multiple_desc_ul, 16);
1551
    else
1552
        put_buffer(pb, mxf_essence_container_uls[mxf->essence_containers_indices[0]].container_ul, 16);
1553
    put_byte(pb, 0);
1554
    put_be64(pb, 0);
1555
    put_be64(pb, 0); // creation date/time stamp
1556
1557
    put_byte(pb, 0x81); // SMPTE 12M time code
1558 7c9668cf Baptiste Coudurier
    time_code = ff_framenum_to_12m_time_code(frame, mxf->timecode_drop_frame, mxf->timecode_base);
1559 67d8ac4a Baptiste Coudurier
    put_be32(pb, time_code);
1560
    put_be32(pb, 0); // binary group data
1561
    put_be64(pb, 0);
1562
1563
    // write system metadata package set
1564
    put_buffer(pb, system_metadata_package_set_key, 16);
1565 e15421fe Baptiste Coudurier
    klv_encode_ber4_length(pb, 35);
1566 67d8ac4a Baptiste Coudurier
    put_byte(pb, 0x83); // UMID
1567
    put_be16(pb, 0x20);
1568
    mxf_write_umid(pb, SourcePackage, 0);
1569
}
1570
1571 bd3f81f3 Baptiste Coudurier
static void mxf_write_d10_video_packet(AVFormatContext *s, AVStream *st, AVPacket *pkt)
1572
{
1573
    MXFContext *mxf = s->priv_data;
1574
    ByteIOContext *pb = s->pb;
1575
    int packet_size = (uint64_t)st->codec->bit_rate*mxf->time_base.num /
1576
        (8*mxf->time_base.den); // frame size
1577
    int pad;
1578
1579
    packet_size += 16 + 4;
1580
    packet_size += klv_fill_size(packet_size);
1581
1582
    klv_encode_ber4_length(pb, pkt->size);
1583
    put_buffer(pb, pkt->data, pkt->size);
1584
1585
    // ensure CBR muxing by padding to correct video frame size
1586
    pad = packet_size - pkt->size - 16 - 4;
1587
    if (pad > 20) {
1588
        put_buffer(s->pb, klv_fill_key, 16);
1589
        pad -= 16 + 4;
1590
        klv_encode_ber4_length(s->pb, pad);
1591
        for (; pad; pad--)
1592
            put_byte(s->pb, 0);
1593
        assert(!(url_ftell(s->pb) & (KAG_SIZE-1)));
1594
    } else {
1595
        av_log(s, AV_LOG_WARNING, "cannot fill d-10 video packet\n");
1596
        for (; pad > 0; pad--)
1597
            put_byte(s->pb, 0);
1598
    }
1599
}
1600
1601
static void mxf_write_d10_audio_packet(AVFormatContext *s, AVStream *st, AVPacket *pkt)
1602
{
1603
    MXFContext *mxf = s->priv_data;
1604
    ByteIOContext *pb = s->pb;
1605
    int frame_size = (pkt->size<<3) /
1606
        (st->codec->channels*av_get_bits_per_sample(st->codec->codec_id));
1607
    uint8_t *samples = pkt->data;
1608
    uint8_t *end = pkt->data + pkt->size;
1609
    int i;
1610
1611
    klv_encode_ber4_length(pb, 4 + frame_size*4*8);
1612
1613
    put_byte(pb, (frame_size == 1920 ? 0 : (mxf->edit_units_count-1) % 5 + 1));
1614
    put_le16(pb, frame_size);
1615
    put_byte(pb, (1<<st->codec->channels)-1);
1616
1617
    while (samples < end) {
1618
        for (i = 0; i < st->codec->channels; i++) {
1619
            uint32_t sample;
1620
            if (st->codec->codec_id == CODEC_ID_PCM_S24LE) {
1621
                sample = (AV_RL24(samples)<< 4)|((samples==pkt->data)<<3) | i;
1622
                samples += 3;
1623
            } else {
1624
                sample = (AV_RL16(samples)<<12)|((samples==pkt->data)<<3) | i;
1625
                samples += 2;
1626
            }
1627
            put_le32(pb, sample);
1628
        }
1629
        for (; i < 8; i++)
1630
            put_le32(pb, 0);
1631
    }
1632
}
1633
1634
static int mxf_write_d10_packet(AVFormatContext *s, AVPacket *pkt)
1635
{
1636
    MXFContext *mxf = s->priv_data;
1637
    ByteIOContext *pb = s->pb;
1638
    AVStream *st = s->streams[pkt->stream_index];
1639
    MXFStreamContext *sc = st->priv_data;
1640
    int flags = 0;
1641
1642
    if (st->codec->codec_id == CODEC_ID_MPEG2VIDEO) {
1643
        if (!mxf_parse_mpeg2_frame(s, st, pkt, &flags)) {
1644
            av_log(s, AV_LOG_ERROR, "could not get mpeg2 profile and level\n");
1645
            return -1;
1646
        }
1647
    }
1648
1649
    if (!mxf->header_written) {
1650
        mxf_write_partition(s, 1, 2, header_open_partition_key, 1);
1651
        mxf->header_written = 1;
1652
        mxf_write_klv_fill(s);
1653
        mxf_write_index_table_segment(s);
1654
    }
1655
1656
    if (st->index == 0) {
1657
        mxf_write_klv_fill(s);
1658
        mxf_write_system_item(s);
1659
1660
        mxf->edit_units_count++;
1661
    }
1662
1663
    mxf_write_klv_fill(s);
1664
    put_buffer(pb, sc->track_essence_element_key, 16); // write key
1665
    if (st->codec->codec_type == CODEC_TYPE_VIDEO)
1666
        mxf_write_d10_video_packet(s, st, pkt);
1667
    else
1668
        mxf_write_d10_audio_packet(s, st, pkt);
1669
1670
    put_flush_packet(pb);
1671
1672
    return 0;
1673
}
1674
1675 4e2f346b Baptiste Coudurier
static int mxf_write_packet(AVFormatContext *s, AVPacket *pkt)
1676 9b0d3e0e Vitor Sessak
{
1677 7c7c1a76 Baptiste Coudurier
    MXFContext *mxf = s->priv_data;
1678 9b0d3e0e Vitor Sessak
    ByteIOContext *pb = s->pb;
1679
    AVStream *st = s->streams[pkt->stream_index];
1680
    MXFStreamContext *sc = st->priv_data;
1681 4a15f3ee Baptiste Coudurier
    int flags = 0;
1682 9b0d3e0e Vitor Sessak
1683 99b170f8 Baptiste Coudurier
    if (!(mxf->edit_units_count % EDIT_UNITS_PER_BODY)) {
1684 0e9dbd96 Baptiste Coudurier
        mxf->index_entries = av_realloc(mxf->index_entries,
1685 99b170f8 Baptiste Coudurier
            (mxf->edit_units_count + EDIT_UNITS_PER_BODY)*sizeof(*mxf->index_entries));
1686 0e9dbd96 Baptiste Coudurier
        if (!mxf->index_entries) {
1687
            av_log(s, AV_LOG_ERROR, "could not allocate index entries\n");
1688
            return -1;
1689
        }
1690
    }
1691
1692
    if (st->codec->codec_id == CODEC_ID_MPEG2VIDEO) {
1693 4a15f3ee Baptiste Coudurier
        if (!mxf_parse_mpeg2_frame(s, st, pkt, &flags)) {
1694 0e9dbd96 Baptiste Coudurier
            av_log(s, AV_LOG_ERROR, "could not get mpeg2 profile and level\n");
1695
            return -1;
1696 7c7c1a76 Baptiste Coudurier
        }
1697 0e9dbd96 Baptiste Coudurier
    }
1698
1699
    if (!mxf->header_written) {
1700 99b170f8 Baptiste Coudurier
        mxf_write_partition(s, 0, 0, header_open_partition_key, 1);
1701 7c7c1a76 Baptiste Coudurier
        mxf->header_written = 1;
1702
    }
1703
1704 5ccc2316 Baptiste Coudurier
    if (st->index == 0) {
1705 99b170f8 Baptiste Coudurier
        if ((!mxf->edit_units_count || mxf->edit_units_count > EDIT_UNITS_PER_BODY) &&
1706
            !(flags & 0x33)) { // I frame, Gop start
1707
            mxf_write_klv_fill(s);
1708
            mxf_write_partition(s, 1, 2, body_partition_key, 0);
1709
1710
            mxf_write_klv_fill(s);
1711
            mxf_write_index_table_segment(s);
1712
        }
1713
1714 67d8ac4a Baptiste Coudurier
        mxf_write_klv_fill(s);
1715 a784cc67 Baptiste Coudurier
        mxf->index_entries[mxf->edit_units_count].offset = url_ftell(pb);
1716 4a15f3ee Baptiste Coudurier
        mxf->index_entries[mxf->edit_units_count].flags = flags;
1717 99b170f8 Baptiste Coudurier
        if (!mxf->first_edit_unit_offset)
1718
            mxf->first_edit_unit_offset = mxf->index_entries[0].offset;
1719 bf31d93a Baptiste Coudurier
        mxf_write_system_item(s);
1720 67d8ac4a Baptiste Coudurier
1721 0e9dbd96 Baptiste Coudurier
        mxf->edit_units_count++;
1722 d526105f Baptiste Coudurier
    } else if (st->index == 1) {
1723
        mxf->index_entries[mxf->edit_units_count-1].slice_offset =
1724 0e9dbd96 Baptiste Coudurier
            url_ftell(pb) - mxf->index_entries[mxf->edit_units_count-1].offset;
1725
    }
1726
1727 bf31d93a Baptiste Coudurier
    mxf_write_klv_fill(s);
1728 9b0d3e0e Vitor Sessak
    put_buffer(pb, sc->track_essence_element_key, 16); // write key
1729 e15421fe Baptiste Coudurier
    klv_encode_ber4_length(pb, pkt->size); // write length
1730 9b0d3e0e Vitor Sessak
    put_buffer(pb, pkt->data, pkt->size); // write value
1731
1732
    put_flush_packet(pb);
1733
    return 0;
1734
}
1735
1736 0e9dbd96 Baptiste Coudurier
static void mxf_write_random_index_pack(AVFormatContext *s)
1737
{
1738
    MXFContext *mxf = s->priv_data;
1739
    ByteIOContext *pb = s->pb;
1740
    uint64_t pos = url_ftell(pb);
1741 99b170f8 Baptiste Coudurier
    int i;
1742 0e9dbd96 Baptiste Coudurier
1743
    put_buffer(pb, random_index_pack_key, 16);
1744 99b170f8 Baptiste Coudurier
    klv_encode_ber_length(pb, 28 + 12*mxf->body_partitions_count);
1745 0e9dbd96 Baptiste Coudurier
1746 99b170f8 Baptiste Coudurier
    put_be32(pb, 0); // BodySID of header partition
1747 0e9dbd96 Baptiste Coudurier
    put_be64(pb, 0); // offset of header partition
1748
1749 99b170f8 Baptiste Coudurier
    for (i = 0; i < mxf->body_partitions_count; i++) {
1750
        put_be32(pb, 1); // BodySID
1751
        put_be64(pb, mxf->body_partition_offset[i]);
1752
    }
1753
1754 0e9dbd96 Baptiste Coudurier
    put_be32(pb, 0); // BodySID of footer partition
1755
    put_be64(pb, mxf->footer_partition_offset);
1756
1757
    put_be32(pb, url_ftell(pb) - pos + 4);
1758
}
1759
1760 4e2f346b Baptiste Coudurier
static int mxf_write_footer(AVFormatContext *s)
1761 9b9466b0 Vitor Sessak
{
1762 cc984060 Baptiste Coudurier
    MXFContext *mxf = s->priv_data;
1763 9b9466b0 Vitor Sessak
    ByteIOContext *pb = s->pb;
1764 85d0d114 Baptiste Coudurier
1765 7c9668cf Baptiste Coudurier
    mxf->duration = mxf->last_indexed_edit_unit + mxf->edit_units_count;
1766
1767 85d0d114 Baptiste Coudurier
    mxf_write_klv_fill(s);
1768 cc984060 Baptiste Coudurier
    mxf->footer_partition_offset = url_ftell(pb);
1769 bd3f81f3 Baptiste Coudurier
    if (mxf->edit_unit_byte_count) { // no need to repeat index
1770
        mxf_write_partition(s, 0, 0, footer_partition_key, 0);
1771
    } else {
1772 8d1a5af7 Baptiste Coudurier
        mxf_write_partition(s, 0, 2, footer_partition_key, 0);
1773 0e9dbd96 Baptiste Coudurier
1774 8d1a5af7 Baptiste Coudurier
        mxf_write_klv_fill(s);
1775
        mxf_write_index_table_segment(s);
1776 bd3f81f3 Baptiste Coudurier
    }
1777 0e9dbd96 Baptiste Coudurier
1778 b25ee3fb Baptiste Coudurier
    mxf_write_klv_fill(s);
1779 0e9dbd96 Baptiste Coudurier
    mxf_write_random_index_pack(s);
1780
1781 cc984060 Baptiste Coudurier
    if (!url_is_streamed(s->pb)) {
1782 bd3f81f3 Baptiste Coudurier
        int index;
1783 cc984060 Baptiste Coudurier
        url_fseek(pb, 0, SEEK_SET);
1784 bd3f81f3 Baptiste Coudurier
        if (s->oformat == &mxf_d10_muxer) {
1785
            mxf_write_partition(s, 1, 2, header_closed_partition_key, 1);
1786
            index = 1;
1787
        } else if (mxf->edit_unit_byte_count) {
1788
            mxf_write_partition(s, 0, 2, header_closed_partition_key, 1);
1789
            index = 1;
1790
        } else {
1791
            mxf_write_partition(s, 0, 0, header_closed_partition_key, 1);
1792
            index = 0;
1793
        }
1794
        if (index) {
1795
            mxf_write_klv_fill(s);
1796
            mxf_write_index_table_segment(s);
1797
        }
1798 cc984060 Baptiste Coudurier
    }
1799 06466638 Baptiste Coudurier
1800
    ff_audio_interleave_close(s);
1801
1802 84a2d304 Baptiste Coudurier
    av_freep(&mxf->index_entries);
1803 99b170f8 Baptiste Coudurier
    av_freep(&mxf->body_partition_offset);
1804 7c9668cf Baptiste Coudurier
    av_freep(&mxf->timecode_track->priv_data);
1805
    av_freep(&mxf->timecode_track);
1806 84a2d304 Baptiste Coudurier
1807 9b9466b0 Vitor Sessak
    mxf_free(s);
1808 8d1a5af7 Baptiste Coudurier
1809 2c8d4388 Zhentan Feng
    return 0;
1810
}
1811 9b9466b0 Vitor Sessak
1812 8f819636 Baptiste Coudurier
static int mxf_interleave_get_packet(AVFormatContext *s, AVPacket *out, AVPacket *pkt, int flush)
1813 cc812c8c Baptiste Coudurier
{
1814
    AVPacketList *pktl;
1815
    int stream_count = 0;
1816
    int streams[MAX_STREAMS];
1817
1818
    memset(streams, 0, sizeof(streams));
1819
    pktl = s->packet_buffer;
1820
    while (pktl) {
1821
        //av_log(s, AV_LOG_DEBUG, "show st:%d dts:%lld\n", pktl->pkt.stream_index, pktl->pkt.dts);
1822
        if (!streams[pktl->pkt.stream_index])
1823
            stream_count++;
1824
        streams[pktl->pkt.stream_index]++;
1825
        pktl = pktl->next;
1826
    }
1827
1828
    if (stream_count && (s->nb_streams == stream_count || flush)) {
1829
        pktl = s->packet_buffer;
1830 bc185fc1 Baptiste Coudurier
        if (s->nb_streams != stream_count) {
1831
            AVPacketList *first = NULL;
1832 6f0b1866 Baptiste Coudurier
            AVPacketList *last = NULL;
1833 bc185fc1 Baptiste Coudurier
            // find first packet in edit unit
1834
            while (pktl) {
1835
                AVStream *st = s->streams[pktl->pkt.stream_index];
1836 5ccc2316 Baptiste Coudurier
                if (st->index == 0)
1837 bc185fc1 Baptiste Coudurier
                    break;
1838
                else if (!first)
1839
                    first = pktl;
1840 6f0b1866 Baptiste Coudurier
                last = pktl;
1841 bc185fc1 Baptiste Coudurier
                pktl = pktl->next;
1842
            }
1843 6f0b1866 Baptiste Coudurier
            if (last)
1844
                last->next = NULL;
1845 cc812c8c Baptiste Coudurier
            // purge packet queue
1846
            while (pktl) {
1847
                AVPacketList *next = pktl->next;
1848
                av_free_packet(&pktl->pkt);
1849
                av_freep(&pktl);
1850
                pktl = next;
1851
            }
1852 bc185fc1 Baptiste Coudurier
            if (!first)
1853
                goto out;
1854
            pktl = first;
1855 cc812c8c Baptiste Coudurier
        }
1856
1857 bc185fc1 Baptiste Coudurier
        *out = pktl->pkt;
1858
        //av_log(s, AV_LOG_DEBUG, "out st:%d dts:%lld\n", (*out).stream_index, (*out).dts);
1859
        s->packet_buffer = pktl->next;
1860
        av_freep(&pktl);
1861 cc812c8c Baptiste Coudurier
        return 1;
1862
    } else {
1863 bc185fc1 Baptiste Coudurier
    out:
1864 cc812c8c Baptiste Coudurier
        av_init_packet(out);
1865
        return 0;
1866
    }
1867
}
1868
1869
static int mxf_compare_timestamps(AVFormatContext *s, AVPacket *next, AVPacket *pkt)
1870
{
1871 5a897cfa Baptiste Coudurier
    MXFStreamContext *sc  = s->streams[pkt ->stream_index]->priv_data;
1872
    MXFStreamContext *sc2 = s->streams[next->stream_index]->priv_data;
1873 cc812c8c Baptiste Coudurier
1874 5a897cfa Baptiste Coudurier
    return next->dts > pkt->dts ||
1875
        (next->dts == pkt->dts && sc->order < sc2->order);
1876 cc812c8c Baptiste Coudurier
}
1877
1878
static int mxf_interleave(AVFormatContext *s, AVPacket *out, AVPacket *pkt, int flush)
1879
{
1880 16e3b0b3 Baptiste Coudurier
    return ff_audio_rechunk_interleave(s, out, pkt, flush,
1881 f1544e79 Baptiste Coudurier
                               mxf_interleave_get_packet, mxf_compare_timestamps);
1882 cc812c8c Baptiste Coudurier
}
1883
1884 b5f46e93 Baptiste Coudurier
AVOutputFormat mxf_muxer = {
1885
    "mxf",
1886
    NULL_IF_CONFIG_SMALL("Material eXchange Format"),
1887
    NULL,
1888
    "mxf",
1889
    sizeof(MXFContext),
1890
    CODEC_ID_PCM_S16LE,
1891
    CODEC_ID_MPEG2VIDEO,
1892 4e2f346b Baptiste Coudurier
    mxf_write_header,
1893
    mxf_write_packet,
1894
    mxf_write_footer,
1895 f35254d1 Baptiste Coudurier
    AVFMT_NOTIMESTAMPS,
1896 cc812c8c Baptiste Coudurier
    NULL,
1897
    mxf_interleave,
1898 b5f46e93 Baptiste Coudurier
};
1899 bd3f81f3 Baptiste Coudurier
1900
AVOutputFormat mxf_d10_muxer = {
1901
    "mxf_d10",
1902
    NULL_IF_CONFIG_SMALL("Material eXchange Format, D-10 Mapping"),
1903
    "application/mxf",
1904
    NULL,
1905
    sizeof(MXFContext),
1906
    CODEC_ID_PCM_S16LE,
1907
    CODEC_ID_MPEG2VIDEO,
1908
    mxf_write_header,
1909
    mxf_write_d10_packet,
1910
    mxf_write_footer,
1911
    AVFMT_NOTIMESTAMPS,
1912
    NULL,
1913
    mxf_interleave,
1914
};