Statistics
| Branch: | Revision:

ffmpeg / libavformat / mxfenc.c @ 9dd94f83

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