3 * Copyright (c) 2008 GUCAS, Zhentan Feng <spyfeng at gmail dot com>
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 * SMPTE 336M KLV Data Encoding Protocol Using Key-Length-Value
25 * SMPTE 377M MXF File Format Specifications
26 * SMPTE 379M MXF Generic Container
27 * SMPTE 381M Mapping MPEG Streams into the MXF Generic Container
28 * SMPTE RP210: SMPTE Metadata Dictionary
29 * SMPTE RP224: Registry of SMPTE Universal Labels
42 UID track_essence_element_key;
45 typedef struct MXFContext {
46 int64_t header_byte_count;
47 int64_t header_byte_count_offset;
48 int64_t header_footer_partition_offset;
49 int essence_container_count;
56 } MXFDescriptorWriteTableEntry;
58 static const uint8_t uuid_base[] = { 0xAD,0xAB,0x44,0x24,0x2f,0x25,0x4d,0xc7,0x92,0xff,0x29,0xbd };
59 static const uint8_t umid_base[] = { 0x06,0x0A,0x2B,0x34,0x01,0x01,0x01,0x01,0x01,0x01,0x0F,0x00,0x13,0x00,0x00,0x00 };
62 * complete key for operation pattern, partitions, and primer pack
64 static const uint8_t op1a_ul[] = { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x02,0x01,0x01,0x01,0x01,0x00 };
65 static const uint8_t header_partition_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0D,0x01,0x02,0x01,0x01,0x02,0x04,0x00 }; // ClosedComplete
66 static const uint8_t footer_partition_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0D,0x01,0x02,0x01,0x01,0x04,0x04,0x00 }; // ClosedComplete
67 static const uint8_t primer_pack_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0D,0x01,0x02,0x01,0x01,0x05,0x01,0x00 };
70 * partial key for header metadata
72 static const uint8_t header_metadata_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0D,0x01,0x01,0x01,0x01 };
74 static const MXFCodecUL mxf_essence_element_key[] = {
75 { { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x15,0x01,0x05,0x00 }, 14, CODEC_ID_MPEG2VIDEO},
76 { { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x16,0x01,0x01,0x00 }, 14, CODEC_ID_PCM_S16LE},
77 { { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }, 0, CODEC_ID_NONE},
80 static const uint8_t multiple_desc_ul[] = { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x0D,0x01,0x03,0x01,0x02,0x7F,0x01,0x00 };
82 static void mxf_write_uuid(ByteIOContext *pb, enum CodecID type, int value)
84 put_buffer(pb, uuid_base, 12);
89 static void mxf_write_umid(ByteIOContext *pb, enum CodecID type, int value)
91 put_buffer(pb, umid_base, 16);
92 mxf_write_uuid(pb, type, value);
95 static void mxf_write_refs_count(ByteIOContext *pb, int ref_count)
97 put_be32(pb, ref_count);
101 static int klv_encode_ber_length(ByteIOContext *pb, uint64_t len)
103 // Determine the best BER size
111 size = (av_log2(len) >> 3) + 1;
114 put_byte(pb, 0x80 + size);
117 put_byte(pb, len >> 8 * size & 0xff);
122 static const MXFCodecUL *mxf_get_essence_container_ul(enum CodecID type)
124 const MXFCodecUL *uls = ff_mxf_essence_container_uls;
125 while (uls->id != CODEC_ID_NONE) {
133 static int mxf_write_primer_pack(AVFormatContext *s)
135 ByteIOContext *pb = s->pb;
136 int local_tag_number, i = 0;
138 local_tag_number = sizeof(mxf_local_tag_batch) / sizeof(MXFLocalTagPair);
140 put_buffer(pb, primer_pack_key, 16);
141 klv_encode_ber_length(pb, local_tag_number * 18 + 8);
143 put_be32(pb, local_tag_number); // local_tag num
144 put_be32(pb, 18); // item size, always 18 according to the specs
146 for (i = 0; i < local_tag_number; i++) {
147 put_be16(pb, mxf_local_tag_batch[i].local_tag);
148 put_buffer(pb, mxf_local_tag_batch[i].uid, 16);
153 static void mxf_write_local_tag(ByteIOContext *pb, int value_size, int tag)
156 put_be16(pb, value_size);
159 static void mxf_write_metadata_key(ByteIOContext *pb, unsigned int value)
161 put_buffer(pb, header_metadata_key, 13);
165 static void mxf_free(AVFormatContext *s)
170 for (i = 0; i < s->nb_streams; i++) {
172 av_freep(&st->priv_data);
176 static const MXFDataDefinitionUL *mxf_get_data_definition_ul(enum CodecType type)
178 const MXFDataDefinitionUL *uls = ff_mxf_data_definition_uls;
179 while (uls->type != CODEC_TYPE_DATA) {
180 if (type == uls->type)
187 static void mxf_write_common_fields( ByteIOContext *pb, AVStream *st)
189 const MXFDataDefinitionUL * data_def_ul;
191 // find data define uls
192 data_def_ul = mxf_get_data_definition_ul(st->codec->codec_type);
193 mxf_write_local_tag(pb, 16, 0x0201);
194 put_buffer(pb, data_def_ul->uid, 16);
197 mxf_write_local_tag(pb, 8, 0x0202);
198 put_be64(pb, st->duration);
201 static int mux_write_packet(AVFormatContext *s, AVPacket *pkt)
203 ByteIOContext *pb = s->pb;
204 AVStream *st = s->streams[pkt->stream_index];
205 MXFStreamContext *sc = st->priv_data;
207 put_buffer(pb, sc->track_essence_element_key, 16); // write key
208 klv_encode_ber_length(pb, pkt->size); // write length
209 put_buffer(pb, pkt->data, pkt->size); // write value
211 put_flush_packet(pb);
215 static void mxf_write_header_desc(ByteIOContext *pb, const MXFDescriptorWriteTableEntry *desc_tbl, AVStream *st)
217 const MXFCodecUL *codec_ul;
219 put_buffer(pb, desc_tbl->key, 16);
220 klv_encode_ber_length(pb, 96);
222 mxf_write_local_tag(pb, 16, 0x3C0A);
223 mxf_write_uuid(pb, SubDescriptor, st->index);
225 mxf_write_local_tag(pb, 4, 0x3006);
226 put_be32(pb, st->index);
228 codec_ul = mxf_get_essence_container_ul(st->codec->codec_id);
229 mxf_write_local_tag(pb, 16, 0x3004);
230 put_buffer(pb, codec_ul->uid, 16);
233 static int mxf_write_mpeg_video_desc(AVFormatContext *s, const MXFDescriptorWriteTableEntry *desc_tbl, int stream_index)
235 ByteIOContext *pb = s->pb;
238 st = s->streams[stream_index];
239 mxf_write_header_desc(pb, desc_tbl, st);
241 mxf_write_local_tag(pb, 4, 0x3203);
242 put_be32(pb, st->codec->width);
244 mxf_write_local_tag(pb, 4, 0x3202);
245 put_be32(pb, st->codec->height);
247 mxf_write_local_tag(pb, 8, 0x320E);
248 put_be32(pb, st->codec->height * st->codec->sample_aspect_ratio.den);
249 put_be32(pb, st->codec->width * st->codec->sample_aspect_ratio.num);
251 // tmp write, will modified later
252 mxf_write_local_tag(pb, 16, 0x3201);
253 put_buffer(pb, ff_mxf_codec_uls->uid, 16);
257 static int mxf_write_wav_desc(AVFormatContext *s, const MXFDescriptorWriteTableEntry *desc_tbl, int stream_index)
259 ByteIOContext *pb = s->pb;
262 st = s->streams[stream_index];
263 mxf_write_header_desc(pb, desc_tbl, st);
265 // write audio sampling rate
266 mxf_write_local_tag(pb, 8, 0x3D03);
267 put_be32(pb, st->codec->sample_rate);
270 mxf_write_local_tag(pb, 4, 0x3D07);
271 put_be32(pb, st->codec->channels);
273 mxf_write_local_tag(pb, 4, 0x3D01);
274 put_be32(pb, st->codec->bits_per_sample);
276 // tmp write, will modified later
277 mxf_write_local_tag(pb, 16, 0x3201);
278 put_buffer(pb, (ff_mxf_codec_uls + 8) ->uid, 16);
282 static const MXFDescriptorWriteTableEntry mxf_descriptor_write_table[] = {
283 { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x51,0x00 }, mxf_write_mpeg_video_desc, CODEC_ID_MPEG2VIDEO},
284 { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x48,0x00 }, mxf_write_wav_desc, CODEC_ID_PCM_S16LE},
285 { { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }, NULL, CODEC_ID_NONE},
288 static int mxf_build_structural_metadata(AVFormatContext *s, enum MXFMetadataSetType type)
291 const MXFDescriptorWriteTableEntry *desc = NULL;
292 int track_number_sign[sizeof(mxf_essence_element_key)/sizeof(MXFCodecUL)] = { 0 };
294 if (mxf_write_package(s, type) < 0)
296 if (type == SourcePackage) {
297 if (mxf_write_multi_descriptor(s) < 0)
301 for (i = 0;i < s->nb_streams; i++) {
302 if ( mxf_write_track(s, i, type, track_number_sign) < 0)
304 if ( mxf_write_sequence(s, i, type) < 0)
306 if ( mxf_write_structural_component(s, i, type) < 0)
309 if (type == SourcePackage) {
310 for (desc = mxf_descriptor_write_table; desc->write; desc++) {
311 if (s->streams[i]->codec->codec_id == desc->type) {
312 if ( desc->write(s, desc, i) < 0) {
313 av_log(s, AV_LOG_ERROR, "error writing descriptor\n");
324 static int mxf_write_header_metadata_sets(AVFormatContext *s)
327 MXFStreamContext *sc = NULL;
329 if (mxf_write_preface(s) < 0)
332 if (mxf_write_identification(s) < 0)
335 if (mxf_write_content_storage(s) < 0)
338 for (i = 0; i < s->nb_streams; i++) {
340 sc = av_mallocz(sizeof(MXFStreamContext));
342 return AVERROR(ENOMEM);
344 // set pts information
345 if (st->codec->codec_type == CODEC_TYPE_VIDEO) {
346 av_set_pts_info(st, 64, 1, st->codec->time_base.den);
347 } else if (st->codec->codec_type == CODEC_TYPE_AUDIO) {
348 av_set_pts_info(st, 64, 1, st->codec->sample_rate);
352 if (mxf_build_structural_metadata(s, MaterialPackage) < 0)
355 if (mxf_build_structural_metadata(s, SourcePackage) < 0)
360 static int mxf_update_header_partition(AVFormatContext *s, int64_t footer_partition_offset)
362 MXFContext *mxf = s->priv_data;
363 ByteIOContext *pb = s->pb;
365 url_fseek(pb, mxf->header_byte_count_offset, SEEK_SET);
366 put_be64(pb, mxf->header_byte_count);
367 put_flush_packet(pb);
369 url_fseek(pb, mxf->header_footer_partition_offset, SEEK_SET);
370 put_be64(pb, footer_partition_offset);
371 put_flush_packet(pb);
374 AVOutputFormat mxf_muxer = {
376 NULL_IF_CONFIG_SMALL("Material eXchange Format"),