Ok-ed parts from patch by zhentan feng.
[ffmpeg.git] / libavformat / mxfenc.c
1 /*
2  * MXF muxer
3  * Copyright (c) 2008 GUCAS, Zhentan Feng <spyfeng at gmail dot com>
4  *
5  * This file is part of FFmpeg.
6  *
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.
11  *
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.
16  *
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
20  */
21
22 /*
23  * References
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
30  */
31
32 //#define DEBUG
33
34 #include "mxf.h"
35
36 typedef struct {
37     int local_tag;
38     UID uid;
39 } MXFLocalTagPair;
40
41 typedef struct {
42     UID track_essence_element_key;
43 } MXFStreamContext;
44
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;
50 } MXFContext;
51
52 typedef struct {
53     const UID key;
54     int (*write)();
55     enum CodecType type;
56 } MXFDescriptorWriteTableEntry;
57
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 };
60
61 /**
62  * complete key for operation pattern, partitions, and primer pack
63  */
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 };
68
69 /**
70  * partial key for header metadata
71  */
72 static const uint8_t header_metadata_key[]  = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0D,0x01,0x01,0x01,0x01 };
73
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},
78 };
79
80 static const uint8_t multiple_desc_ul[] = { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x0D,0x01,0x03,0x01,0x02,0x7F,0x01,0x00 };
81
82 static void mxf_write_uuid(ByteIOContext *pb, enum CodecID type, int value)
83 {
84     put_buffer(pb, uuid_base, 12);
85     put_be16(pb, type);
86     put_be16(pb, value);
87 }
88
89 static void mxf_write_umid(ByteIOContext *pb, enum CodecID type, int value)
90 {
91     put_buffer(pb, umid_base, 16);
92     mxf_write_uuid(pb, type, value);
93 }
94
95 static void mxf_write_refs_count(ByteIOContext *pb, int ref_count)
96 {
97     put_be32(pb, ref_count);
98     put_be32(pb, 16);
99 }
100
101 static int klv_encode_ber_length(ByteIOContext *pb, uint64_t len)
102 {
103     // Determine the best BER size
104     int size;
105     if (len < 128) {
106         //short form
107         put_byte(pb, len);
108         return 1;
109     }
110
111     size = (av_log2(len) >> 3) + 1;
112
113     // long form
114     put_byte(pb, 0x80 + size);
115     while(size) {
116         size --;
117         put_byte(pb, len >> 8 * size & 0xff);
118     }
119     return 0;
120 }
121
122 static const MXFCodecUL *mxf_get_essence_container_ul(enum CodecID type)
123 {
124     const MXFCodecUL *uls = ff_mxf_essence_container_uls;
125     while (uls->id != CODEC_ID_NONE) {
126         if (uls->id == type)
127             break;
128         uls++;
129     }
130     return uls;
131 }
132
133 static int mxf_write_primer_pack(AVFormatContext *s)
134 {
135     ByteIOContext *pb = s->pb;
136     int local_tag_number, i = 0;
137
138     local_tag_number = sizeof(mxf_local_tag_batch) / sizeof(MXFLocalTagPair);
139
140     put_buffer(pb, primer_pack_key, 16);
141     klv_encode_ber_length(pb, local_tag_number * 18 + 8);
142
143     put_be32(pb, local_tag_number); // local_tag num
144     put_be32(pb, 18); // item size, always 18 according to the specs
145
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);
149     }
150     return 0;
151 }
152
153 static void mxf_write_local_tag(ByteIOContext *pb, int value_size, int tag)
154 {
155     put_be16(pb, tag);
156     put_be16(pb, value_size);
157 }
158
159 static void mxf_write_metadata_key(ByteIOContext *pb, unsigned int value)
160 {
161     put_buffer(pb, header_metadata_key, 13);
162     put_be24(pb, value);
163 }
164
165 static void mxf_free(AVFormatContext *s)
166 {
167     AVStream *st;
168     int i;
169
170     for (i = 0; i < s->nb_streams; i++) {
171         st = s->streams[i];
172         av_freep(&st->priv_data);
173     }
174 }
175
176 static const MXFDataDefinitionUL *mxf_get_data_definition_ul(enum CodecType type)
177 {
178     const MXFDataDefinitionUL *uls = ff_mxf_data_definition_uls;
179     while (uls->type != CODEC_TYPE_DATA) {
180         if (type == uls->type)
181             break;
182         uls ++;
183     }
184     return uls;
185 }
186
187 static void mxf_write_common_fields(    ByteIOContext *pb, AVStream *st)
188 {
189     const MXFDataDefinitionUL * data_def_ul;
190
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);
195
196     // write duration
197     mxf_write_local_tag(pb, 8, 0x0202);
198     put_be64(pb, st->duration);
199 }
200
201 static int mux_write_packet(AVFormatContext *s, AVPacket *pkt)
202 {
203     ByteIOContext *pb = s->pb;
204     AVStream *st = s->streams[pkt->stream_index];
205     MXFStreamContext *sc = st->priv_data;
206
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
210
211     put_flush_packet(pb);
212     return 0;
213 }
214
215 static void mxf_write_header_desc(ByteIOContext *pb, const MXFDescriptorWriteTableEntry *desc_tbl, AVStream *st)
216 {
217     const MXFCodecUL *codec_ul;
218
219     put_buffer(pb, desc_tbl->key, 16);
220     klv_encode_ber_length(pb, 96);
221
222     mxf_write_local_tag(pb, 16, 0x3C0A);
223     mxf_write_uuid(pb, SubDescriptor, st->index);
224
225     mxf_write_local_tag(pb, 4, 0x3006);
226     put_be32(pb, st->index);
227
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);
231 }
232
233 static int mxf_write_mpeg_video_desc(AVFormatContext *s, const MXFDescriptorWriteTableEntry *desc_tbl, int stream_index)
234 {
235     ByteIOContext *pb = s->pb;
236     AVStream *st;
237
238     st = s->streams[stream_index];
239     mxf_write_header_desc(pb, desc_tbl, st);
240
241     mxf_write_local_tag(pb, 4, 0x3203);
242     put_be32(pb, st->codec->width);
243
244     mxf_write_local_tag(pb, 4, 0x3202);
245     put_be32(pb, st->codec->height);
246
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);
250
251     // tmp write, will modified later
252     mxf_write_local_tag(pb, 16, 0x3201);
253     put_buffer(pb, ff_mxf_codec_uls->uid, 16);
254     return 0;
255 }
256
257 static int mxf_write_wav_desc(AVFormatContext *s, const MXFDescriptorWriteTableEntry *desc_tbl, int stream_index)
258 {
259     ByteIOContext *pb = s->pb;
260     AVStream *st;
261
262     st = s->streams[stream_index];
263     mxf_write_header_desc(pb, desc_tbl, st);
264
265     // write audio sampling rate
266     mxf_write_local_tag(pb, 8, 0x3D03);
267     put_be32(pb, st->codec->sample_rate);
268     put_be32(pb, 1);
269
270     mxf_write_local_tag(pb, 4, 0x3D07);
271     put_be32(pb, st->codec->channels);
272
273     mxf_write_local_tag(pb, 4, 0x3D01);
274     put_be32(pb, st->codec->bits_per_sample);
275
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);
279     return 0;
280 }
281
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},
286 };
287
288 static int mxf_build_structural_metadata(AVFormatContext *s, enum MXFMetadataSetType type)
289 {
290     int i;
291     const MXFDescriptorWriteTableEntry *desc = NULL;
292     int track_number_sign[sizeof(mxf_essence_element_key)/sizeof(MXFCodecUL)] = { 0 };
293
294     if (mxf_write_package(s, type) < 0)
295         return -1;
296     if (type == SourcePackage) {
297         if (mxf_write_multi_descriptor(s) < 0)
298             return -1;
299     }
300
301     for (i = 0;i < s->nb_streams; i++) {
302         if ( mxf_write_track(s, i, type, track_number_sign) < 0)
303             return -1;
304         if ( mxf_write_sequence(s, i, type) < 0)
305             return -1;
306         if ( mxf_write_structural_component(s, i, type) < 0)
307             return -1;
308
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");
314                         return -1;
315                     }
316                     break;
317                 }
318             }
319         }
320     }
321     return 0;
322 }
323
324 static int mxf_write_header_metadata_sets(AVFormatContext *s)
325 {
326     AVStream *st;
327     MXFStreamContext *sc = NULL;
328     int i;
329     if (mxf_write_preface(s) < 0)
330         return -1;
331
332     if (mxf_write_identification(s) < 0)
333         return -1;
334
335     if (mxf_write_content_storage(s) < 0)
336         return -1;
337
338     for (i = 0; i < s->nb_streams; i++) {
339         st = s->streams[i];
340         sc = av_mallocz(sizeof(MXFStreamContext));
341         if (!sc)
342             return AVERROR(ENOMEM);
343         st->priv_data = sc;
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);
349         }
350     }
351
352     if (mxf_build_structural_metadata(s, MaterialPackage) < 0)
353         return -1;
354
355     if (mxf_build_structural_metadata(s, SourcePackage) < 0)
356         return -1;
357     return 0;
358 }
359
360 static int mxf_update_header_partition(AVFormatContext *s, int64_t footer_partition_offset)
361 {
362     MXFContext *mxf = s->priv_data;
363     ByteIOContext *pb = s->pb;
364
365     url_fseek(pb, mxf->header_byte_count_offset, SEEK_SET);
366     put_be64(pb, mxf->header_byte_count);
367     put_flush_packet(pb);
368
369     url_fseek(pb, mxf->header_footer_partition_offset, SEEK_SET);
370     put_be64(pb, footer_partition_offset);
371     put_flush_packet(pb);
372     return 0;
373 }
374 AVOutputFormat mxf_muxer = {
375     "mxf",
376     NULL_IF_CONFIG_SMALL("Material eXchange Format"),
377     NULL,
378     "mxf",
379     sizeof(MXFContext),
380     CODEC_ID_PCM_S16LE,
381     CODEC_ID_MPEG2VIDEO,
382     mux_write_header,
383     mux_write_packet,
384     mux_write_footer,
385 };