3721
|
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
|
3735
|
34 #include "mxf.h"
|
|
35
|
|
36 typedef struct {
|
|
37 int local_tag;
|
|
38 UID uid;
|
|
39 } MXFLocalTagPair;
|
|
40
|
|
41 static const uint8_t uuid_base[] = { 0xAD,0xAB,0x44,0x24,0x2f,0x25,0x4d,0xc7,0x92,0xff,0x29,0xbd };
|
|
42 static const uint8_t umid_base[] = { 0x06,0x0A,0x2B,0x34,0x01,0x01,0x01,0x01,0x01,0x01,0x0F,0x00,0x13,0x00,0x00,0x00 };
|
|
43
|
|
44 /**
|
|
45 * complete key for operation pattern, partitions, and primer pack
|
|
46 */
|
|
47 static const uint8_t op1a_ul[] = { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x02,0x01,0x01,0x01,0x01,0x00 };
|
|
48 static const uint8_t header_partition_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0D,0x01,0x02,0x01,0x01,0x02,0x04,0x00 }; // ClosedComplete
|
|
49 static const uint8_t footer_partition_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0D,0x01,0x02,0x01,0x01,0x04,0x04,0x00 }; // ClosedComplete
|
|
50 static const uint8_t primer_pack_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0D,0x01,0x02,0x01,0x01,0x05,0x01,0x00 };
|
|
51
|
|
52 static void mxf_write_uuid(ByteIOContext *pb, enum CodecID type, int value)
|
|
53 {
|
|
54 put_buffer(pb, uuid_base, 12);
|
|
55 put_be16(pb, type);
|
|
56 put_be16(pb, value);
|
|
57 }
|
|
58
|
|
59 static int klv_encode_ber_length(ByteIOContext *pb, uint64_t len)
|
|
60 {
|
|
61 // Determine the best BER size
|
|
62 int size;
|
|
63 if (len < 128) {
|
|
64 //short form
|
|
65 put_byte(pb, len);
|
|
66 return 1;
|
|
67 }
|
|
68
|
|
69 size = (av_log2(len) >> 3) + 1;
|
|
70
|
|
71 // long form
|
|
72 put_byte(pb, 0x80 + size);
|
|
73 while(size) {
|
|
74 size --;
|
|
75 put_byte(pb, len >> 8 * size & 0xff);
|
|
76 }
|
|
77 return 0;
|
|
78 }
|
|
79
|
3721
|
80 static const MXFCodecUL *mxf_get_essence_container_ul(enum CodecID type)
|
|
81 {
|
3735
|
82 const MXFCodecUL *uls = ff_mxf_essence_container_uls;
|
3721
|
83 while (uls->id != CODEC_ID_NONE) {
|
|
84 if (uls->id == type)
|
|
85 break;
|
|
86 uls++;
|
|
87 }
|
|
88 return uls;
|
|
89 }
|
|
90
|
3735
|
91 static int mxf_write_primer_pack(AVFormatContext *s)
|
|
92 {
|
|
93 ByteIOContext *pb = s->pb;
|
|
94 int local_tag_number, i = 0;
|
|
95
|
|
96 local_tag_number = sizeof(mxf_local_tag_batch) / sizeof(MXFLocalTagPair);
|
|
97
|
|
98 put_buffer(pb, primer_pack_key, 16);
|
|
99 klv_encode_ber_length(pb, local_tag_number * 18 + 8);
|
|
100
|
|
101 put_be32(pb, local_tag_number); // local_tag num
|
|
102 put_be32(pb, 18); // item size, always 18 according to the specs
|
|
103
|
|
104 for (i = 0; i < local_tag_number; i++) {
|
|
105 put_be16(pb, mxf_local_tag_batch[i].local_tag);
|
|
106 put_buffer(pb, mxf_local_tag_batch[i].uid, 16);
|
|
107 }
|
|
108 return 0;
|
|
109 }
|
|
110
|
|
111 static void mxf_write_local_tag(ByteIOContext *pb, int value_size, int tag)
|
|
112 {
|
|
113 put_be16(pb, tag);
|
|
114 put_be16(pb, value_size);
|
|
115 }
|
|
116
|
3721
|
117 static void mxf_free(AVFormatContext *s)
|
|
118 {
|
|
119 MXFContext *mxf = s->priv_data;
|
|
120 AVStream *st;
|
|
121 int i;
|
|
122
|
|
123 for (i = 0; i < s->nb_streams; i++) {
|
|
124 st = s->streams[i];
|
|
125 av_freep(&st->priv_data);
|
|
126 }
|
|
127 av_freep(&mxf->essence_container_uls);
|
|
128 }
|
|
129
|
|
130 static const MXFDataDefinitionUL *mxf_get_data_definition_ul(enum CodecType type)
|
|
131 {
|
3735
|
132 const MXFDataDefinitionUL *uls = ff_mxf_data_definition_uls;
|
3721
|
133 while (uls->type != CODEC_TYPE_DATA) {
|
|
134 if (type == uls->type)
|
|
135 break;
|
|
136 uls ++;
|
|
137 }
|
|
138 return uls;
|
|
139 }
|
|
140
|
|
141 static int mux_write_packet(AVFormatContext *s, AVPacket *pkt)
|
|
142 {
|
|
143 ByteIOContext *pb = s->pb;
|
|
144 AVStream *st = s->streams[pkt->stream_index];
|
|
145 MXFStreamContext *sc = st->priv_data;
|
|
146
|
|
147 put_buffer(pb, sc->track_essence_element_key, 16); // write key
|
|
148 klv_encode_ber_length(pb, pkt->size); // write length
|
|
149 put_buffer(pb, pkt->data, pkt->size); // write value
|
|
150
|
|
151 put_flush_packet(pb);
|
|
152 return 0;
|
|
153 }
|
|
154
|
|
155 AVOutputFormat mxf_muxer = {
|
|
156 "mxf",
|
|
157 NULL_IF_CONFIG_SMALL("Material eXchange Format"),
|
|
158 NULL,
|
|
159 "mxf",
|
|
160 sizeof(MXFContext),
|
|
161 CODEC_ID_PCM_S16LE,
|
|
162 CODEC_ID_MPEG2VIDEO,
|
|
163 mux_write_header,
|
|
164 mux_write_packet,
|
|
165 mux_write_footer,
|
|
166 };
|