Libav 0.7.1
|
00001 /* 00002 * MXF muxer 00003 * Copyright (c) 2008 GUCAS, Zhentan Feng <spyfeng at gmail dot com> 00004 * Copyright (c) 2008 Baptiste Coudurier <baptiste dot coudurier at gmail dot com> 00005 * 00006 * This file is part of Libav. 00007 * 00008 * Libav is free software; you can redistribute it and/or 00009 * modify it under the terms of the GNU Lesser General Public 00010 * License as published by the Free Software Foundation; either 00011 * version 2.1 of the License, or (at your option) any later version. 00012 * 00013 * Libav is distributed in the hope that it will be useful, 00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00016 * Lesser General Public License for more details. 00017 * 00018 * You should have received a copy of the GNU Lesser General Public 00019 * License along with Libav; if not, write to the Free Software 00020 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00021 */ 00022 00023 /* 00024 * References 00025 * SMPTE 336M KLV Data Encoding Protocol Using Key-Length-Value 00026 * SMPTE 377M MXF File Format Specifications 00027 * SMPTE 379M MXF Generic Container 00028 * SMPTE 381M Mapping MPEG Streams into the MXF Generic Container 00029 * SMPTE RP210: SMPTE Metadata Dictionary 00030 * SMPTE RP224: Registry of SMPTE Universal Labels 00031 */ 00032 00033 //#define DEBUG 00034 00035 #include <math.h> 00036 #include <time.h> 00037 00038 #include "libavutil/random_seed.h" 00039 #include "libavcodec/bytestream.h" 00040 #include "audiointerleave.h" 00041 #include "avformat.h" 00042 #include "mxf.h" 00043 00044 static const int NTSC_samples_per_frame[] = { 1602, 1601, 1602, 1601, 1602, 0 }; 00045 static const int PAL_samples_per_frame[] = { 1920, 0 }; 00046 00047 extern AVOutputFormat ff_mxf_d10_muxer; 00048 00049 #define EDIT_UNITS_PER_BODY 250 00050 #define KAG_SIZE 512 00051 00052 typedef struct { 00053 int local_tag; 00054 UID uid; 00055 } MXFLocalTagPair; 00056 00057 typedef struct { 00058 uint8_t flags; 00059 uint64_t offset; 00060 unsigned slice_offset; 00061 uint16_t temporal_ref; 00062 } MXFIndexEntry; 00063 00064 typedef struct { 00065 AudioInterleaveContext aic; 00066 UID track_essence_element_key; 00067 int index; 00068 const UID *codec_ul; 00069 int order; 00070 int interlaced; 00071 int temporal_reordering; 00072 AVRational aspect_ratio; 00073 int closed_gop; 00074 } MXFStreamContext; 00075 00076 typedef struct { 00077 UID container_ul; 00078 UID element_ul; 00079 UID codec_ul; 00080 void (*write_desc)(AVFormatContext *, AVStream *); 00081 } MXFContainerEssenceEntry; 00082 00083 static const struct { 00084 enum CodecID id; 00085 int index; 00086 } mxf_essence_mappings[] = { 00087 { CODEC_ID_MPEG2VIDEO, 0 }, 00088 { CODEC_ID_PCM_S24LE, 1 }, 00089 { CODEC_ID_PCM_S16LE, 1 }, 00090 { CODEC_ID_NONE } 00091 }; 00092 00093 static void mxf_write_wav_desc(AVFormatContext *s, AVStream *st); 00094 static void mxf_write_aes3_desc(AVFormatContext *s, AVStream *st); 00095 static void mxf_write_mpegvideo_desc(AVFormatContext *s, AVStream *st); 00096 static void mxf_write_cdci_desc(AVFormatContext *s, AVStream *st); 00097 static void mxf_write_generic_sound_desc(AVFormatContext *s, AVStream *st); 00098 00099 static const MXFContainerEssenceEntry mxf_essence_container_uls[] = { 00100 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x02,0x0D,0x01,0x03,0x01,0x02,0x04,0x60,0x01 }, 00101 { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x15,0x01,0x05,0x00 }, 00102 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x00,0x00,0x00 }, 00103 mxf_write_mpegvideo_desc }, 00104 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x06,0x03,0x00 }, 00105 { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x16,0x01,0x03,0x00 }, 00106 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 }, 00107 mxf_write_aes3_desc }, 00108 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x06,0x01,0x00 }, 00109 { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x16,0x01,0x01,0x00 }, 00110 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 }, 00111 mxf_write_wav_desc }, 00112 // D-10 625/50 PAL 50mb/s 00113 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x01,0x01 }, 00114 { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x05,0x01,0x01,0x00 }, 00115 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x01,0x02,0x01,0x01 }, 00116 mxf_write_cdci_desc }, 00117 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x01,0x01 }, 00118 { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x06,0x01,0x10,0x00 }, 00119 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 }, 00120 mxf_write_generic_sound_desc }, 00121 // D-10 525/60 NTSC 50mb/s 00122 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x02,0x01 }, 00123 { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x05,0x01,0x01,0x00 }, 00124 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x01,0x02,0x01,0x02 }, 00125 mxf_write_cdci_desc }, 00126 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x02,0x01 }, 00127 { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x06,0x01,0x10,0x00 }, 00128 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 }, 00129 mxf_write_generic_sound_desc }, 00130 // D-10 625/50 PAL 40mb/s 00131 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x03,0x01 }, 00132 { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x05,0x01,0x01,0x00 }, 00133 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x01,0x02,0x01,0x03 }, 00134 mxf_write_cdci_desc }, 00135 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x03,0x01 }, 00136 { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x06,0x01,0x10,0x00 }, 00137 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 }, 00138 mxf_write_generic_sound_desc }, 00139 // D-10 525/60 NTSC 40mb/s 00140 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x04,0x01 }, 00141 { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x05,0x01,0x01,0x00 }, 00142 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x01,0x02,0x01,0x04 }, 00143 mxf_write_cdci_desc }, 00144 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x04,0x01 }, 00145 { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x06,0x01,0x10,0x00 }, 00146 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 }, 00147 mxf_write_generic_sound_desc }, 00148 // D-10 625/50 PAL 30mb/s 00149 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x05,0x01 }, 00150 { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x05,0x01,0x01,0x00 }, 00151 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x01,0x02,0x01,0x05 }, 00152 mxf_write_cdci_desc }, 00153 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x05,0x01 }, 00154 { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x06,0x01,0x10,0x00 }, 00155 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 }, 00156 mxf_write_generic_sound_desc }, 00157 // D-10 525/60 NTSC 30mb/s 00158 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x06,0x01 }, 00159 { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x05,0x01,0x01,0x00 }, 00160 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x01,0x02,0x01,0x06 }, 00161 mxf_write_cdci_desc }, 00162 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x06,0x01 }, 00163 { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x06,0x01,0x10,0x00 }, 00164 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 }, 00165 mxf_write_generic_sound_desc }, 00166 { { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }, 00167 { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }, 00168 { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }, 00169 NULL }, 00170 }; 00171 00172 typedef struct MXFContext { 00173 int64_t footer_partition_offset; 00174 int essence_container_count; 00175 AVRational time_base; 00176 int header_written; 00177 MXFIndexEntry *index_entries; 00178 unsigned edit_units_count; 00179 uint64_t timestamp; 00180 uint8_t slice_count; 00181 int last_indexed_edit_unit; 00182 uint64_t *body_partition_offset; 00183 unsigned body_partitions_count; 00184 int last_key_index; 00185 uint64_t duration; 00186 AVStream *timecode_track; 00187 int timecode_base; 00188 int timecode_start; 00189 int timecode_drop_frame; 00190 int edit_unit_byte_count; 00191 uint64_t body_offset; 00192 uint32_t instance_number; 00193 uint8_t umid[16]; 00194 } MXFContext; 00195 00196 static const uint8_t uuid_base[] = { 0xAD,0xAB,0x44,0x24,0x2f,0x25,0x4d,0xc7,0x92,0xff,0x29,0xbd }; 00197 static const uint8_t umid_ul[] = { 0x06,0x0A,0x2B,0x34,0x01,0x01,0x01,0x05,0x01,0x01,0x0D,0x00,0x13 }; 00198 00202 static const uint8_t op1a_ul[] = { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x02,0x01,0x01,0x01,0x09,0x00 }; 00203 static const uint8_t footer_partition_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0D,0x01,0x02,0x01,0x01,0x04,0x04,0x00 }; // ClosedComplete 00204 static const uint8_t primer_pack_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0D,0x01,0x02,0x01,0x01,0x05,0x01,0x00 }; 00205 static const uint8_t index_table_segment_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x10,0x01,0x00 }; 00206 static const uint8_t random_index_pack_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0D,0x01,0x02,0x01,0x01,0x11,0x01,0x00 }; 00207 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 00208 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 00209 static const uint8_t klv_fill_key[] = { 0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x03,0x01,0x02,0x10,0x01,0x00,0x00,0x00 }; 00210 static const uint8_t body_partition_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0D,0x01,0x02,0x01,0x01,0x03,0x04,0x00 }; // ClosedComplete 00211 00215 static const uint8_t header_metadata_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0D,0x01,0x01,0x01,0x01 }; 00216 static const uint8_t multiple_desc_ul[] = { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x0D,0x01,0x03,0x01,0x02,0x7F,0x01,0x00 }; 00217 00221 static const MXFLocalTagPair mxf_local_tag_batch[] = { 00222 // preface set 00223 { 0x3C0A, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x01,0x01,0x15,0x02,0x00,0x00,0x00,0x00}}, /* Instance UID */ 00224 { 0x3B02, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x10,0x02,0x04,0x00,0x00}}, /* Last Modified Date */ 00225 { 0x3B05, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x03,0x01,0x02,0x01,0x05,0x00,0x00,0x00}}, /* Version */ 00226 { 0x3B06, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x06,0x04,0x00,0x00}}, /* Identifications reference */ 00227 { 0x3B03, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x02,0x01,0x00,0x00}}, /* Content Storage reference */ 00228 { 0x3B09, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x01,0x02,0x02,0x03,0x00,0x00,0x00,0x00}}, /* Operational Pattern UL */ 00229 { 0x3B0A, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x01,0x02,0x02,0x10,0x02,0x01,0x00,0x00}}, /* Essence Containers UL batch */ 00230 { 0x3B0B, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x01,0x02,0x02,0x10,0x02,0x02,0x00,0x00}}, /* DM Schemes UL batch */ 00231 // Identification 00232 { 0x3C09, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x20,0x07,0x01,0x01,0x00,0x00,0x00}}, /* This Generation UID */ 00233 { 0x3C01, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x20,0x07,0x01,0x02,0x01,0x00,0x00}}, /* Company Name */ 00234 { 0x3C02, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x20,0x07,0x01,0x03,0x01,0x00,0x00}}, /* Product Name */ 00235 { 0x3C04, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x20,0x07,0x01,0x05,0x01,0x00,0x00}}, /* Version String */ 00236 { 0x3C05, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x20,0x07,0x01,0x07,0x00,0x00,0x00}}, /* Product ID */ 00237 { 0x3C06, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x10,0x02,0x03,0x00,0x00}}, /* Modification Date */ 00238 // Content Storage 00239 { 0x1901, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x05,0x01,0x00,0x00}}, /* Package strong reference batch */ 00240 { 0x1902, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x05,0x02,0x00,0x00}}, /* Package strong reference batch */ 00241 // Essence Container Data 00242 { 0x2701, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x06,0x01,0x00,0x00,0x00}}, /* Linked Package UID */ 00243 { 0x3F07, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x01,0x03,0x04,0x04,0x00,0x00,0x00,0x00}}, /* BodySID */ 00244 // Package 00245 { 0x4401, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x01,0x01,0x15,0x10,0x00,0x00,0x00,0x00}}, /* Package UID */ 00246 { 0x4405, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x10,0x01,0x03,0x00,0x00}}, /* Package Creation Date */ 00247 { 0x4404, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x10,0x02,0x05,0x00,0x00}}, /* Package Modified Date */ 00248 { 0x4403, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x06,0x05,0x00,0x00}}, /* Tracks Strong reference array */ 00249 { 0x4701, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x02,0x03,0x00,0x00}}, /* Descriptor */ 00250 // Track 00251 { 0x4801, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x01,0x07,0x01,0x01,0x00,0x00,0x00,0x00}}, /* Track ID */ 00252 { 0x4804, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x01,0x04,0x01,0x03,0x00,0x00,0x00,0x00}}, /* Track Number */ 00253 { 0x4B01, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x30,0x04,0x05,0x00,0x00,0x00,0x00}}, /* Edit Rate */ 00254 { 0x4B02, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x03,0x01,0x03,0x00,0x00}}, /* Origin */ 00255 { 0x4803, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x02,0x04,0x00,0x00}}, /* Sequence reference */ 00256 // Sequence 00257 { 0x0201, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x07,0x01,0x00,0x00,0x00,0x00,0x00}}, /* Data Definition UL */ 00258 { 0x0202, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x02,0x01,0x01,0x03,0x00,0x00}}, /* Duration */ 00259 { 0x1001, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x06,0x09,0x00,0x00}}, /* Structural Components reference array */ 00260 // Source Clip 00261 { 0x1201, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x03,0x01,0x04,0x00,0x00}}, /* Start position */ 00262 { 0x1101, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x03,0x01,0x00,0x00,0x00}}, /* SourcePackageID */ 00263 { 0x1102, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x03,0x02,0x00,0x00,0x00}}, /* SourceTrackID */ 00264 // Timecode Component 00265 { 0x1501, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x03,0x01,0x05,0x00,0x00}}, /* Start Time Code */ 00266 { 0x1502, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x04,0x01,0x01,0x02,0x06,0x00,0x00}}, /* Rounded Time Code Base */ 00267 { 0x1503, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x04,0x01,0x01,0x05,0x00,0x00,0x00}}, /* Drop Frame */ 00268 // File Descriptor 00269 { 0x3F01, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x06,0x01,0x01,0x04,0x06,0x0B,0x00,0x00}}, /* Sub Descriptors reference array */ 00270 { 0x3006, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x06,0x01,0x01,0x03,0x05,0x00,0x00,0x00}}, /* Linked Track ID */ 00271 { 0x3001, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x06,0x01,0x01,0x00,0x00,0x00,0x00}}, /* SampleRate */ 00272 { 0x3004, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x01,0x02,0x00,0x00}}, /* Essence Container */ 00273 // Generic Picture Essence Descriptor 00274 { 0x320C, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x03,0x01,0x04,0x00,0x00,0x00}}, /* Frame Layout */ 00275 { 0x320D, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x01,0x03,0x02,0x05,0x00,0x00,0x00}}, /* Video Line Map */ 00276 { 0x3203, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x02,0x02,0x00,0x00,0x00}}, /* Stored Width */ 00277 { 0x3202, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x02,0x01,0x00,0x00,0x00}}, /* Stored Height */ 00278 { 0x3209, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x01,0x0C,0x00,0x00,0x00}}, /* Display Width */ 00279 { 0x3208, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x01,0x0B,0x00,0x00,0x00}}, /* Display Height */ 00280 { 0x320E, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x01,0x01,0x01,0x00,0x00,0x00}}, /* Aspect Ratio */ 00281 { 0x3201, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x01,0x06,0x01,0x00,0x00,0x00,0x00}}, /* Picture Essence Coding */ 00282 // CDCI Picture Essence Descriptor 00283 { 0x3301, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x01,0x05,0x03,0x0A,0x00,0x00,0x00}}, /* Component Depth */ 00284 { 0x3302, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x01,0x05,0x00,0x00,0x00}}, /* Horizontal Subsampling */ 00285 // Generic Sound Essence Descriptor 00286 { 0x3D02, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x04,0x02,0x03,0x01,0x04,0x00,0x00,0x00}}, /* Locked/Unlocked */ 00287 { 0x3D03, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x02,0x03,0x01,0x01,0x01,0x00,0x00}}, /* Audio sampling rate */ 00288 { 0x3D07, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x02,0x01,0x01,0x04,0x00,0x00,0x00}}, /* ChannelCount */ 00289 { 0x3D01, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x04,0x02,0x03,0x03,0x04,0x00,0x00,0x00}}, /* Quantization bits */ 00290 { 0x3D06, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x02,0x04,0x02,0x00,0x00,0x00,0x00}}, /* Sound Essence Compression */ 00291 // Index Table Segment 00292 { 0x3F0B, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x05,0x30,0x04,0x06,0x00,0x00,0x00,0x00}}, /* Index Edit Rate */ 00293 { 0x3F0C, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x07,0x02,0x01,0x03,0x01,0x0A,0x00,0x00}}, /* Index Start Position */ 00294 { 0x3F0D, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x07,0x02,0x02,0x01,0x01,0x02,0x00,0x00}}, /* Index Duration */ 00295 { 0x3F05, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x04,0x06,0x02,0x01,0x00,0x00,0x00,0x00}}, /* Edit Unit Byte Count */ 00296 { 0x3F06, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x01,0x03,0x04,0x05,0x00,0x00,0x00,0x00}}, /* IndexSID */ 00297 { 0x3F08, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x04,0x04,0x04,0x01,0x01,0x00,0x00,0x00}}, /* Slice Count */ 00298 { 0x3F09, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x04,0x04,0x01,0x06,0x00,0x00,0x00}}, /* Delta Entry Array */ 00299 { 0x3F0A, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x04,0x04,0x02,0x05,0x00,0x00,0x00}}, /* Index Entry Array */ 00300 // MPEG video Descriptor 00301 { 0x8000, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x01,0x06,0x02,0x01,0x0B,0x00,0x00}}, /* BitRate */ 00302 { 0x8007, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x01,0x06,0x02,0x01,0x0A,0x00,0x00}}, /* ProfileAndLevel */ 00303 // Wave Audio Essence Descriptor 00304 { 0x3D09, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x02,0x03,0x03,0x05,0x00,0x00,0x00}}, /* Average Bytes Per Second */ 00305 { 0x3D0A, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x02,0x03,0x02,0x01,0x00,0x00,0x00}}, /* Block Align */ 00306 }; 00307 00308 static void mxf_write_uuid(AVIOContext *pb, enum MXFMetadataSetType type, int value) 00309 { 00310 avio_write(pb, uuid_base, 12); 00311 avio_wb16(pb, type); 00312 avio_wb16(pb, value); 00313 } 00314 00315 static void mxf_write_umid(AVFormatContext *s, int type) 00316 { 00317 MXFContext *mxf = s->priv_data; 00318 avio_write(s->pb, umid_ul, 13); 00319 avio_wb24(s->pb, mxf->instance_number); 00320 avio_write(s->pb, mxf->umid, 15); 00321 avio_w8(s->pb, type); 00322 } 00323 00324 static void mxf_write_refs_count(AVIOContext *pb, int ref_count) 00325 { 00326 avio_wb32(pb, ref_count); 00327 avio_wb32(pb, 16); 00328 } 00329 00330 static int klv_ber_length(uint64_t len) 00331 { 00332 if (len < 128) 00333 return 1; 00334 else 00335 return (av_log2(len) >> 3) + 2; 00336 } 00337 00338 static int klv_encode_ber_length(AVIOContext *pb, uint64_t len) 00339 { 00340 // Determine the best BER size 00341 int size; 00342 if (len < 128) { 00343 //short form 00344 avio_w8(pb, len); 00345 return 1; 00346 } 00347 00348 size = (av_log2(len) >> 3) + 1; 00349 00350 // long form 00351 avio_w8(pb, 0x80 + size); 00352 while(size) { 00353 size--; 00354 avio_w8(pb, len >> 8 * size & 0xff); 00355 } 00356 return 0; 00357 } 00358 00359 static void klv_encode_ber4_length(AVIOContext *pb, int len) 00360 { 00361 avio_w8(pb, 0x80 + 3); 00362 avio_wb24(pb, len); 00363 } 00364 00365 /* 00366 * Get essence container ul index 00367 */ 00368 static int mxf_get_essence_container_ul_index(enum CodecID id) 00369 { 00370 int i; 00371 for (i = 0; mxf_essence_mappings[i].id; i++) 00372 if (mxf_essence_mappings[i].id == id) 00373 return mxf_essence_mappings[i].index; 00374 return -1; 00375 } 00376 00377 static void mxf_write_primer_pack(AVFormatContext *s) 00378 { 00379 AVIOContext *pb = s->pb; 00380 int local_tag_number, i = 0; 00381 00382 local_tag_number = FF_ARRAY_ELEMS(mxf_local_tag_batch); 00383 00384 avio_write(pb, primer_pack_key, 16); 00385 klv_encode_ber_length(pb, local_tag_number * 18 + 8); 00386 00387 avio_wb32(pb, local_tag_number); // local_tag num 00388 avio_wb32(pb, 18); // item size, always 18 according to the specs 00389 00390 for (i = 0; i < local_tag_number; i++) { 00391 avio_wb16(pb, mxf_local_tag_batch[i].local_tag); 00392 avio_write(pb, mxf_local_tag_batch[i].uid, 16); 00393 } 00394 } 00395 00396 static void mxf_write_local_tag(AVIOContext *pb, int size, int tag) 00397 { 00398 avio_wb16(pb, tag); 00399 avio_wb16(pb, size); 00400 } 00401 00402 static void mxf_write_metadata_key(AVIOContext *pb, unsigned int value) 00403 { 00404 avio_write(pb, header_metadata_key, 13); 00405 avio_wb24(pb, value); 00406 } 00407 00408 static void mxf_free(AVFormatContext *s) 00409 { 00410 int i; 00411 00412 for (i = 0; i < s->nb_streams; i++) { 00413 AVStream *st = s->streams[i]; 00414 av_freep(&st->priv_data); 00415 } 00416 } 00417 00418 static const MXFCodecUL *mxf_get_data_definition_ul(int type) 00419 { 00420 const MXFCodecUL *uls = ff_mxf_data_definition_uls; 00421 while (uls->uid[0]) { 00422 if (type == uls->id) 00423 break; 00424 uls++; 00425 } 00426 return uls; 00427 } 00428 00429 static void mxf_write_essence_container_refs(AVFormatContext *s) 00430 { 00431 MXFContext *c = s->priv_data; 00432 AVIOContext *pb = s->pb; 00433 int i; 00434 00435 mxf_write_refs_count(pb, c->essence_container_count); 00436 av_log(s,AV_LOG_DEBUG, "essence container count:%d\n", c->essence_container_count); 00437 for (i = 0; i < c->essence_container_count; i++) { 00438 MXFStreamContext *sc = s->streams[i]->priv_data; 00439 avio_write(pb, mxf_essence_container_uls[sc->index].container_ul, 16); 00440 } 00441 } 00442 00443 static void mxf_write_preface(AVFormatContext *s) 00444 { 00445 MXFContext *mxf = s->priv_data; 00446 AVIOContext *pb = s->pb; 00447 00448 mxf_write_metadata_key(pb, 0x012f00); 00449 PRINT_KEY(s, "preface key", pb->buf_ptr - 16); 00450 klv_encode_ber_length(pb, 130 + 16 * mxf->essence_container_count); 00451 00452 // write preface set uid 00453 mxf_write_local_tag(pb, 16, 0x3C0A); 00454 mxf_write_uuid(pb, Preface, 0); 00455 PRINT_KEY(s, "preface uid", pb->buf_ptr - 16); 00456 00457 // last modified date 00458 mxf_write_local_tag(pb, 8, 0x3B02); 00459 avio_wb64(pb, mxf->timestamp); 00460 00461 // write version 00462 mxf_write_local_tag(pb, 2, 0x3B05); 00463 avio_wb16(pb, 258); // v1.2 00464 00465 // write identification_refs 00466 mxf_write_local_tag(pb, 16 + 8, 0x3B06); 00467 mxf_write_refs_count(pb, 1); 00468 mxf_write_uuid(pb, Identification, 0); 00469 00470 // write content_storage_refs 00471 mxf_write_local_tag(pb, 16, 0x3B03); 00472 mxf_write_uuid(pb, ContentStorage, 0); 00473 00474 // operational pattern 00475 mxf_write_local_tag(pb, 16, 0x3B09); 00476 avio_write(pb, op1a_ul, 16); 00477 00478 // write essence_container_refs 00479 mxf_write_local_tag(pb, 8 + 16 * mxf->essence_container_count, 0x3B0A); 00480 mxf_write_essence_container_refs(s); 00481 00482 // write dm_scheme_refs 00483 mxf_write_local_tag(pb, 8, 0x3B0B); 00484 avio_wb64(pb, 0); 00485 } 00486 00487 /* 00488 * Write a local tag containing an ascii string as utf-16 00489 */ 00490 static void mxf_write_local_tag_utf16(AVIOContext *pb, int tag, const char *value) 00491 { 00492 int i, size = strlen(value); 00493 mxf_write_local_tag(pb, size*2, tag); 00494 for (i = 0; i < size; i++) 00495 avio_wb16(pb, value[i]); 00496 } 00497 00498 static void mxf_write_identification(AVFormatContext *s) 00499 { 00500 MXFContext *mxf = s->priv_data; 00501 AVIOContext *pb = s->pb; 00502 const char *company = "Libav"; 00503 const char *product = "OP1a Muxer"; 00504 const char *version; 00505 int length; 00506 00507 mxf_write_metadata_key(pb, 0x013000); 00508 PRINT_KEY(s, "identification key", pb->buf_ptr - 16); 00509 00510 version = s->streams[0]->codec->flags & CODEC_FLAG_BITEXACT ? 00511 "0.0.0" : AV_STRINGIFY(LIBAVFORMAT_VERSION); 00512 length = 84 + (strlen(company)+strlen(product)+strlen(version))*2; // utf-16 00513 klv_encode_ber_length(pb, length); 00514 00515 // write uid 00516 mxf_write_local_tag(pb, 16, 0x3C0A); 00517 mxf_write_uuid(pb, Identification, 0); 00518 PRINT_KEY(s, "identification uid", pb->buf_ptr - 16); 00519 00520 // write generation uid 00521 mxf_write_local_tag(pb, 16, 0x3C09); 00522 mxf_write_uuid(pb, Identification, 1); 00523 00524 mxf_write_local_tag_utf16(pb, 0x3C01, company); // Company Name 00525 mxf_write_local_tag_utf16(pb, 0x3C02, product); // Product Name 00526 mxf_write_local_tag_utf16(pb, 0x3C04, version); // Version String 00527 00528 // write product uid 00529 mxf_write_local_tag(pb, 16, 0x3C05); 00530 mxf_write_uuid(pb, Identification, 2); 00531 00532 // modification date 00533 mxf_write_local_tag(pb, 8, 0x3C06); 00534 avio_wb64(pb, mxf->timestamp); 00535 } 00536 00537 static void mxf_write_content_storage(AVFormatContext *s) 00538 { 00539 AVIOContext *pb = s->pb; 00540 00541 mxf_write_metadata_key(pb, 0x011800); 00542 PRINT_KEY(s, "content storage key", pb->buf_ptr - 16); 00543 klv_encode_ber_length(pb, 92); 00544 00545 // write uid 00546 mxf_write_local_tag(pb, 16, 0x3C0A); 00547 mxf_write_uuid(pb, ContentStorage, 0); 00548 PRINT_KEY(s, "content storage uid", pb->buf_ptr - 16); 00549 00550 // write package reference 00551 mxf_write_local_tag(pb, 16 * 2 + 8, 0x1901); 00552 mxf_write_refs_count(pb, 2); 00553 mxf_write_uuid(pb, MaterialPackage, 0); 00554 mxf_write_uuid(pb, SourcePackage, 0); 00555 00556 // write essence container data 00557 mxf_write_local_tag(pb, 8 + 16, 0x1902); 00558 mxf_write_refs_count(pb, 1); 00559 mxf_write_uuid(pb, EssenceContainerData, 0); 00560 } 00561 00562 static void mxf_write_track(AVFormatContext *s, AVStream *st, enum MXFMetadataSetType type) 00563 { 00564 MXFContext *mxf = s->priv_data; 00565 AVIOContext *pb = s->pb; 00566 MXFStreamContext *sc = st->priv_data; 00567 00568 mxf_write_metadata_key(pb, 0x013b00); 00569 PRINT_KEY(s, "track key", pb->buf_ptr - 16); 00570 klv_encode_ber_length(pb, 80); 00571 00572 // write track uid 00573 mxf_write_local_tag(pb, 16, 0x3C0A); 00574 mxf_write_uuid(pb, type == MaterialPackage ? Track : Track + TypeBottom, st->index); 00575 PRINT_KEY(s, "track uid", pb->buf_ptr - 16); 00576 00577 // write track id 00578 mxf_write_local_tag(pb, 4, 0x4801); 00579 avio_wb32(pb, st->index+2); 00580 00581 // write track number 00582 mxf_write_local_tag(pb, 4, 0x4804); 00583 if (type == MaterialPackage) 00584 avio_wb32(pb, 0); // track number of material package is 0 00585 else 00586 avio_write(pb, sc->track_essence_element_key + 12, 4); 00587 00588 mxf_write_local_tag(pb, 8, 0x4B01); 00589 avio_wb32(pb, mxf->time_base.den); 00590 avio_wb32(pb, mxf->time_base.num); 00591 00592 // write origin 00593 mxf_write_local_tag(pb, 8, 0x4B02); 00594 avio_wb64(pb, 0); 00595 00596 // write sequence refs 00597 mxf_write_local_tag(pb, 16, 0x4803); 00598 mxf_write_uuid(pb, type == MaterialPackage ? Sequence: Sequence + TypeBottom, st->index); 00599 } 00600 00601 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 }; 00602 00603 static void mxf_write_common_fields(AVFormatContext *s, AVStream *st) 00604 { 00605 MXFContext *mxf = s->priv_data; 00606 AVIOContext *pb = s->pb; 00607 00608 // find data define uls 00609 mxf_write_local_tag(pb, 16, 0x0201); 00610 if (st == mxf->timecode_track) 00611 avio_write(pb, smpte_12m_timecode_track_data_ul, 16); 00612 else { 00613 const MXFCodecUL *data_def_ul = mxf_get_data_definition_ul(st->codec->codec_type); 00614 avio_write(pb, data_def_ul->uid, 16); 00615 } 00616 00617 // write duration 00618 mxf_write_local_tag(pb, 8, 0x0202); 00619 avio_wb64(pb, mxf->duration); 00620 } 00621 00622 static void mxf_write_sequence(AVFormatContext *s, AVStream *st, enum MXFMetadataSetType type) 00623 { 00624 MXFContext *mxf = s->priv_data; 00625 AVIOContext *pb = s->pb; 00626 enum MXFMetadataSetType component; 00627 00628 mxf_write_metadata_key(pb, 0x010f00); 00629 PRINT_KEY(s, "sequence key", pb->buf_ptr - 16); 00630 klv_encode_ber_length(pb, 80); 00631 00632 mxf_write_local_tag(pb, 16, 0x3C0A); 00633 mxf_write_uuid(pb, type == MaterialPackage ? Sequence: Sequence + TypeBottom, st->index); 00634 00635 PRINT_KEY(s, "sequence uid", pb->buf_ptr - 16); 00636 mxf_write_common_fields(s, st); 00637 00638 // write structural component 00639 mxf_write_local_tag(pb, 16 + 8, 0x1001); 00640 mxf_write_refs_count(pb, 1); 00641 if (st == mxf->timecode_track) 00642 component = TimecodeComponent; 00643 else 00644 component = SourceClip; 00645 if (type == SourcePackage) 00646 component += TypeBottom; 00647 mxf_write_uuid(pb, component, st->index); 00648 } 00649 00650 static void mxf_write_timecode_component(AVFormatContext *s, AVStream *st, enum MXFMetadataSetType type) 00651 { 00652 MXFContext *mxf = s->priv_data; 00653 AVIOContext *pb = s->pb; 00654 00655 mxf_write_metadata_key(pb, 0x011400); 00656 klv_encode_ber_length(pb, 75); 00657 00658 // UID 00659 mxf_write_local_tag(pb, 16, 0x3C0A); 00660 mxf_write_uuid(pb, type == MaterialPackage ? TimecodeComponent : 00661 TimecodeComponent + TypeBottom, st->index); 00662 00663 mxf_write_common_fields(s, st); 00664 00665 // Start Time Code 00666 mxf_write_local_tag(pb, 8, 0x1501); 00667 avio_wb64(pb, mxf->timecode_start); 00668 00669 // Rounded Time Code Base 00670 mxf_write_local_tag(pb, 2, 0x1502); 00671 avio_wb16(pb, mxf->timecode_base); 00672 00673 // Drop Frame 00674 mxf_write_local_tag(pb, 1, 0x1503); 00675 avio_w8(pb, mxf->timecode_drop_frame); 00676 } 00677 00678 static void mxf_write_structural_component(AVFormatContext *s, AVStream *st, enum MXFMetadataSetType type) 00679 { 00680 AVIOContext *pb = s->pb; 00681 int i; 00682 00683 mxf_write_metadata_key(pb, 0x011100); 00684 PRINT_KEY(s, "sturctural component key", pb->buf_ptr - 16); 00685 klv_encode_ber_length(pb, 108); 00686 00687 // write uid 00688 mxf_write_local_tag(pb, 16, 0x3C0A); 00689 mxf_write_uuid(pb, type == MaterialPackage ? SourceClip: SourceClip + TypeBottom, st->index); 00690 00691 PRINT_KEY(s, "structural component uid", pb->buf_ptr - 16); 00692 mxf_write_common_fields(s, st); 00693 00694 // write start_position 00695 mxf_write_local_tag(pb, 8, 0x1201); 00696 avio_wb64(pb, 0); 00697 00698 // write source package uid, end of the reference 00699 mxf_write_local_tag(pb, 32, 0x1101); 00700 if (type == SourcePackage) { 00701 for (i = 0; i < 4; i++) 00702 avio_wb64(pb, 0); 00703 } else 00704 mxf_write_umid(s, 1); 00705 00706 // write source track id 00707 mxf_write_local_tag(pb, 4, 0x1102); 00708 if (type == SourcePackage) 00709 avio_wb32(pb, 0); 00710 else 00711 avio_wb32(pb, st->index+2); 00712 } 00713 00714 static void mxf_write_multi_descriptor(AVFormatContext *s) 00715 { 00716 MXFContext *mxf = s->priv_data; 00717 AVIOContext *pb = s->pb; 00718 const uint8_t *ul; 00719 int i; 00720 00721 mxf_write_metadata_key(pb, 0x014400); 00722 PRINT_KEY(s, "multiple descriptor key", pb->buf_ptr - 16); 00723 klv_encode_ber_length(pb, 64 + 16 * s->nb_streams); 00724 00725 mxf_write_local_tag(pb, 16, 0x3C0A); 00726 mxf_write_uuid(pb, MultipleDescriptor, 0); 00727 PRINT_KEY(s, "multi_desc uid", pb->buf_ptr - 16); 00728 00729 // write sample rate 00730 mxf_write_local_tag(pb, 8, 0x3001); 00731 avio_wb32(pb, mxf->time_base.den); 00732 avio_wb32(pb, mxf->time_base.num); 00733 00734 // write essence container ul 00735 mxf_write_local_tag(pb, 16, 0x3004); 00736 if (mxf->essence_container_count > 1) 00737 ul = multiple_desc_ul; 00738 else { 00739 MXFStreamContext *sc = s->streams[0]->priv_data; 00740 ul = mxf_essence_container_uls[sc->index].container_ul; 00741 } 00742 avio_write(pb, ul, 16); 00743 00744 // write sub descriptor refs 00745 mxf_write_local_tag(pb, s->nb_streams * 16 + 8, 0x3F01); 00746 mxf_write_refs_count(pb, s->nb_streams); 00747 for (i = 0; i < s->nb_streams; i++) 00748 mxf_write_uuid(pb, SubDescriptor, i); 00749 } 00750 00751 static void mxf_write_generic_desc(AVFormatContext *s, AVStream *st, const UID key, unsigned size) 00752 { 00753 MXFContext *mxf = s->priv_data; 00754 MXFStreamContext *sc = st->priv_data; 00755 AVIOContext *pb = s->pb; 00756 00757 avio_write(pb, key, 16); 00758 klv_encode_ber4_length(pb, size+20+8+12+20); 00759 00760 mxf_write_local_tag(pb, 16, 0x3C0A); 00761 mxf_write_uuid(pb, SubDescriptor, st->index); 00762 00763 mxf_write_local_tag(pb, 4, 0x3006); 00764 avio_wb32(pb, st->index+2); 00765 00766 mxf_write_local_tag(pb, 8, 0x3001); 00767 avio_wb32(pb, mxf->time_base.den); 00768 avio_wb32(pb, mxf->time_base.num); 00769 00770 mxf_write_local_tag(pb, 16, 0x3004); 00771 avio_write(pb, mxf_essence_container_uls[sc->index].container_ul, 16); 00772 } 00773 00774 static const UID mxf_mpegvideo_descriptor_key = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x51,0x00 }; 00775 static const UID mxf_wav_descriptor_key = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x48,0x00 }; 00776 static const UID mxf_aes3_descriptor_key = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x47,0x00 }; 00777 static const UID mxf_cdci_descriptor_key = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0D,0x01,0x01,0x01,0x01,0x01,0x28,0x00 }; 00778 static const UID mxf_generic_sound_descriptor_key = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0D,0x01,0x01,0x01,0x01,0x01,0x42,0x00 }; 00779 00780 static void mxf_write_cdci_common(AVFormatContext *s, AVStream *st, const UID key, unsigned size) 00781 { 00782 MXFStreamContext *sc = st->priv_data; 00783 AVIOContext *pb = s->pb; 00784 int stored_height = (st->codec->height+15)/16*16; 00785 int display_height; 00786 int f1, f2; 00787 00788 mxf_write_generic_desc(s, st, key, size+8+8+8+8+8+8+5+16+sc->interlaced*4+12+20); 00789 00790 mxf_write_local_tag(pb, 4, 0x3203); 00791 avio_wb32(pb, st->codec->width); 00792 00793 mxf_write_local_tag(pb, 4, 0x3202); 00794 avio_wb32(pb, stored_height>>sc->interlaced); 00795 00796 mxf_write_local_tag(pb, 4, 0x3209); 00797 avio_wb32(pb, st->codec->width); 00798 00799 if (st->codec->height == 608) // PAL + VBI 00800 display_height = 576; 00801 else if (st->codec->height == 512) // NTSC + VBI 00802 display_height = 486; 00803 else 00804 display_height = st->codec->height; 00805 00806 mxf_write_local_tag(pb, 4, 0x3208); 00807 avio_wb32(pb, display_height>>sc->interlaced); 00808 00809 // component depth 00810 mxf_write_local_tag(pb, 4, 0x3301); 00811 avio_wb32(pb, 8); 00812 00813 // horizontal subsampling 00814 mxf_write_local_tag(pb, 4, 0x3302); 00815 avio_wb32(pb, 2); 00816 00817 // frame layout 00818 mxf_write_local_tag(pb, 1, 0x320C); 00819 avio_w8(pb, sc->interlaced); 00820 00821 // video line map 00822 switch (st->codec->height) { 00823 case 576: f1 = 23; f2 = 336; break; 00824 case 608: f1 = 7; f2 = 320; break; 00825 case 480: f1 = 20; f2 = 283; break; 00826 case 512: f1 = 7; f2 = 270; break; 00827 case 720: f1 = 26; f2 = 0; break; // progressive 00828 case 1080: f1 = 21; f2 = 584; break; 00829 default: f1 = 0; f2 = 0; break; 00830 } 00831 00832 if (!sc->interlaced) { 00833 f2 = 0; 00834 f1 *= 2; 00835 } 00836 00837 mxf_write_local_tag(pb, 12+sc->interlaced*4, 0x320D); 00838 avio_wb32(pb, sc->interlaced ? 2 : 1); 00839 avio_wb32(pb, 4); 00840 avio_wb32(pb, f1); 00841 if (sc->interlaced) 00842 avio_wb32(pb, f2); 00843 00844 mxf_write_local_tag(pb, 8, 0x320E); 00845 avio_wb32(pb, sc->aspect_ratio.num); 00846 avio_wb32(pb, sc->aspect_ratio.den); 00847 00848 mxf_write_local_tag(pb, 16, 0x3201); 00849 avio_write(pb, *sc->codec_ul, 16); 00850 } 00851 00852 static void mxf_write_cdci_desc(AVFormatContext *s, AVStream *st) 00853 { 00854 mxf_write_cdci_common(s, st, mxf_cdci_descriptor_key, 0); 00855 } 00856 00857 static void mxf_write_mpegvideo_desc(AVFormatContext *s, AVStream *st) 00858 { 00859 AVIOContext *pb = s->pb; 00860 int profile_and_level = (st->codec->profile<<4) | st->codec->level; 00861 00862 mxf_write_cdci_common(s, st, mxf_mpegvideo_descriptor_key, 8+5); 00863 00864 // bit rate 00865 mxf_write_local_tag(pb, 4, 0x8000); 00866 avio_wb32(pb, st->codec->bit_rate); 00867 00868 // profile and level 00869 mxf_write_local_tag(pb, 1, 0x8007); 00870 if (!st->codec->profile) 00871 profile_and_level |= 0x80; // escape bit 00872 avio_w8(pb, profile_and_level); 00873 } 00874 00875 static void mxf_write_generic_sound_common(AVFormatContext *s, AVStream *st, const UID key, unsigned size) 00876 { 00877 AVIOContext *pb = s->pb; 00878 00879 mxf_write_generic_desc(s, st, key, size+5+12+8+8); 00880 00881 // audio locked 00882 mxf_write_local_tag(pb, 1, 0x3D02); 00883 avio_w8(pb, 1); 00884 00885 // write audio sampling rate 00886 mxf_write_local_tag(pb, 8, 0x3D03); 00887 avio_wb32(pb, st->codec->sample_rate); 00888 avio_wb32(pb, 1); 00889 00890 mxf_write_local_tag(pb, 4, 0x3D07); 00891 avio_wb32(pb, st->codec->channels); 00892 00893 mxf_write_local_tag(pb, 4, 0x3D01); 00894 avio_wb32(pb, av_get_bits_per_sample(st->codec->codec_id)); 00895 } 00896 00897 static void mxf_write_wav_common(AVFormatContext *s, AVStream *st, const UID key, unsigned size) 00898 { 00899 AVIOContext *pb = s->pb; 00900 00901 mxf_write_generic_sound_common(s, st, key, size+6+8); 00902 00903 mxf_write_local_tag(pb, 2, 0x3D0A); 00904 avio_wb16(pb, st->codec->block_align); 00905 00906 // avg bytes per sec 00907 mxf_write_local_tag(pb, 4, 0x3D09); 00908 avio_wb32(pb, st->codec->block_align*st->codec->sample_rate); 00909 } 00910 00911 static void mxf_write_wav_desc(AVFormatContext *s, AVStream *st) 00912 { 00913 mxf_write_wav_common(s, st, mxf_wav_descriptor_key, 0); 00914 } 00915 00916 static void mxf_write_aes3_desc(AVFormatContext *s, AVStream *st) 00917 { 00918 mxf_write_wav_common(s, st, mxf_aes3_descriptor_key, 0); 00919 } 00920 00921 static void mxf_write_generic_sound_desc(AVFormatContext *s, AVStream *st) 00922 { 00923 mxf_write_generic_sound_common(s, st, mxf_generic_sound_descriptor_key, 0); 00924 } 00925 00926 static void mxf_write_package(AVFormatContext *s, enum MXFMetadataSetType type) 00927 { 00928 MXFContext *mxf = s->priv_data; 00929 AVIOContext *pb = s->pb; 00930 int i, track_count = s->nb_streams+1; 00931 00932 if (type == MaterialPackage) { 00933 mxf_write_metadata_key(pb, 0x013600); 00934 PRINT_KEY(s, "Material Package key", pb->buf_ptr - 16); 00935 klv_encode_ber_length(pb, 92 + 16*track_count); 00936 } else { 00937 mxf_write_metadata_key(pb, 0x013700); 00938 PRINT_KEY(s, "Source Package key", pb->buf_ptr - 16); 00939 klv_encode_ber_length(pb, 112 + 16*track_count); // 20 bytes length for descriptor reference 00940 } 00941 00942 // write uid 00943 mxf_write_local_tag(pb, 16, 0x3C0A); 00944 mxf_write_uuid(pb, type, 0); 00945 av_log(s,AV_LOG_DEBUG, "package type:%d\n", type); 00946 PRINT_KEY(s, "package uid", pb->buf_ptr - 16); 00947 00948 // write package umid 00949 mxf_write_local_tag(pb, 32, 0x4401); 00950 mxf_write_umid(s, type == SourcePackage); 00951 PRINT_KEY(s, "package umid second part", pb->buf_ptr - 16); 00952 00953 // package creation date 00954 mxf_write_local_tag(pb, 8, 0x4405); 00955 avio_wb64(pb, mxf->timestamp); 00956 00957 // package modified date 00958 mxf_write_local_tag(pb, 8, 0x4404); 00959 avio_wb64(pb, mxf->timestamp); 00960 00961 // write track refs 00962 mxf_write_local_tag(pb, track_count*16 + 8, 0x4403); 00963 mxf_write_refs_count(pb, track_count); 00964 mxf_write_uuid(pb, type == MaterialPackage ? Track : 00965 Track + TypeBottom, -1); // timecode track 00966 for (i = 0; i < s->nb_streams; i++) 00967 mxf_write_uuid(pb, type == MaterialPackage ? Track : Track + TypeBottom, i); 00968 00969 // write multiple descriptor reference 00970 if (type == SourcePackage) { 00971 mxf_write_local_tag(pb, 16, 0x4701); 00972 if (s->nb_streams > 1) { 00973 mxf_write_uuid(pb, MultipleDescriptor, 0); 00974 mxf_write_multi_descriptor(s); 00975 } else 00976 mxf_write_uuid(pb, SubDescriptor, 0); 00977 } 00978 00979 // write timecode track 00980 mxf_write_track(s, mxf->timecode_track, type); 00981 mxf_write_sequence(s, mxf->timecode_track, type); 00982 mxf_write_timecode_component(s, mxf->timecode_track, type); 00983 00984 for (i = 0; i < s->nb_streams; i++) { 00985 AVStream *st = s->streams[i]; 00986 mxf_write_track(s, st, type); 00987 mxf_write_sequence(s, st, type); 00988 mxf_write_structural_component(s, st, type); 00989 00990 if (type == SourcePackage) { 00991 MXFStreamContext *sc = st->priv_data; 00992 mxf_essence_container_uls[sc->index].write_desc(s, st); 00993 } 00994 } 00995 } 00996 00997 static int mxf_write_essence_container_data(AVFormatContext *s) 00998 { 00999 AVIOContext *pb = s->pb; 01000 01001 mxf_write_metadata_key(pb, 0x012300); 01002 klv_encode_ber_length(pb, 72); 01003 01004 mxf_write_local_tag(pb, 16, 0x3C0A); // Instance UID 01005 mxf_write_uuid(pb, EssenceContainerData, 0); 01006 01007 mxf_write_local_tag(pb, 32, 0x2701); // Linked Package UID 01008 mxf_write_umid(s, 1); 01009 01010 mxf_write_local_tag(pb, 4, 0x3F07); // BodySID 01011 avio_wb32(pb, 1); 01012 01013 mxf_write_local_tag(pb, 4, 0x3F06); // IndexSID 01014 avio_wb32(pb, 2); 01015 01016 return 0; 01017 } 01018 01019 static int mxf_write_header_metadata_sets(AVFormatContext *s) 01020 { 01021 mxf_write_preface(s); 01022 mxf_write_identification(s); 01023 mxf_write_content_storage(s); 01024 mxf_write_package(s, MaterialPackage); 01025 mxf_write_package(s, SourcePackage); 01026 mxf_write_essence_container_data(s); 01027 return 0; 01028 } 01029 01030 static unsigned klv_fill_size(uint64_t size) 01031 { 01032 unsigned pad = KAG_SIZE - (size & (KAG_SIZE-1)); 01033 if (pad < 20) // smallest fill item possible 01034 return pad + KAG_SIZE; 01035 else 01036 return pad & (KAG_SIZE-1); 01037 } 01038 01039 static void mxf_write_index_table_segment(AVFormatContext *s) 01040 { 01041 MXFContext *mxf = s->priv_data; 01042 AVIOContext *pb = s->pb; 01043 int i, j, temporal_reordering = 0; 01044 int key_index = mxf->last_key_index; 01045 01046 av_log(s, AV_LOG_DEBUG, "edit units count %d\n", mxf->edit_units_count); 01047 01048 if (!mxf->edit_units_count && !mxf->edit_unit_byte_count) 01049 return; 01050 01051 avio_write(pb, index_table_segment_key, 16); 01052 01053 if (mxf->edit_unit_byte_count) { 01054 klv_encode_ber_length(pb, 80); 01055 } else { 01056 klv_encode_ber_length(pb, 85 + 12+(s->nb_streams+1)*6 + 01057 12+mxf->edit_units_count*(11+mxf->slice_count*4)); 01058 } 01059 01060 // instance id 01061 mxf_write_local_tag(pb, 16, 0x3C0A); 01062 mxf_write_uuid(pb, IndexTableSegment, 0); 01063 01064 // index edit rate 01065 mxf_write_local_tag(pb, 8, 0x3F0B); 01066 avio_wb32(pb, mxf->time_base.den); 01067 avio_wb32(pb, mxf->time_base.num); 01068 01069 // index start position 01070 mxf_write_local_tag(pb, 8, 0x3F0C); 01071 avio_wb64(pb, mxf->last_indexed_edit_unit); 01072 01073 // index duration 01074 mxf_write_local_tag(pb, 8, 0x3F0D); 01075 if (mxf->edit_unit_byte_count) 01076 avio_wb64(pb, 0); // index table covers whole container 01077 else 01078 avio_wb64(pb, mxf->edit_units_count); 01079 01080 // edit unit byte count 01081 mxf_write_local_tag(pb, 4, 0x3F05); 01082 avio_wb32(pb, mxf->edit_unit_byte_count); 01083 01084 // index sid 01085 mxf_write_local_tag(pb, 4, 0x3F06); 01086 avio_wb32(pb, 2); 01087 01088 // body sid 01089 mxf_write_local_tag(pb, 4, 0x3F07); 01090 avio_wb32(pb, 1); 01091 01092 if (!mxf->edit_unit_byte_count) { 01093 // real slice count - 1 01094 mxf_write_local_tag(pb, 1, 0x3F08); 01095 avio_w8(pb, mxf->slice_count); 01096 01097 // delta entry array 01098 mxf_write_local_tag(pb, 8 + (s->nb_streams+1)*6, 0x3F09); 01099 avio_wb32(pb, s->nb_streams+1); // num of entries 01100 avio_wb32(pb, 6); // size of one entry 01101 // write system item delta entry 01102 avio_w8(pb, 0); 01103 avio_w8(pb, 0); // slice entry 01104 avio_wb32(pb, 0); // element delta 01105 for (i = 0; i < s->nb_streams; i++) { 01106 AVStream *st = s->streams[i]; 01107 MXFStreamContext *sc = st->priv_data; 01108 avio_w8(pb, sc->temporal_reordering); 01109 if (sc->temporal_reordering) 01110 temporal_reordering = 1; 01111 if (i == 0) { // video track 01112 avio_w8(pb, 0); // slice number 01113 avio_wb32(pb, KAG_SIZE); // system item size including klv fill 01114 } else { // audio track 01115 unsigned audio_frame_size = sc->aic.samples[0]*sc->aic.sample_size; 01116 audio_frame_size += klv_fill_size(audio_frame_size); 01117 avio_w8(pb, 1); 01118 avio_wb32(pb, (i-1)*audio_frame_size); // element delta 01119 } 01120 } 01121 01122 mxf_write_local_tag(pb, 8 + mxf->edit_units_count*(11+mxf->slice_count*4), 0x3F0A); 01123 avio_wb32(pb, mxf->edit_units_count); // num of entries 01124 avio_wb32(pb, 11+mxf->slice_count*4); // size of one entry 01125 01126 for (i = 0; i < mxf->edit_units_count; i++) { 01127 int temporal_offset = 0; 01128 01129 if (!(mxf->index_entries[i].flags & 0x33)) { // I frame 01130 mxf->last_key_index = key_index; 01131 key_index = i; 01132 } 01133 01134 if (temporal_reordering) { 01135 int pic_num_in_gop = i - key_index; 01136 if (pic_num_in_gop != mxf->index_entries[i].temporal_ref) { 01137 for (j = key_index; j < mxf->edit_units_count; j++) { 01138 if (pic_num_in_gop == mxf->index_entries[j].temporal_ref) 01139 break; 01140 } 01141 if (j == mxf->edit_units_count) 01142 av_log(s, AV_LOG_WARNING, "missing frames\n"); 01143 temporal_offset = j - key_index - pic_num_in_gop; 01144 } 01145 } 01146 avio_w8(pb, temporal_offset); 01147 01148 if ((mxf->index_entries[i].flags & 0x30) == 0x30) { // back and forward prediction 01149 avio_w8(pb, mxf->last_key_index - i); 01150 } else { 01151 avio_w8(pb, key_index - i); // key frame offset 01152 if ((mxf->index_entries[i].flags & 0x20) == 0x20) // only forward 01153 mxf->last_key_index = key_index; 01154 } 01155 01156 if (!(mxf->index_entries[i].flags & 0x33) && // I frame 01157 mxf->index_entries[i].flags & 0x40 && !temporal_offset) 01158 mxf->index_entries[i].flags |= 0x80; // random access 01159 avio_w8(pb, mxf->index_entries[i].flags); 01160 // stream offset 01161 avio_wb64(pb, mxf->index_entries[i].offset); 01162 if (s->nb_streams > 1) 01163 avio_wb32(pb, mxf->index_entries[i].slice_offset); 01164 } 01165 01166 mxf->last_key_index = key_index - mxf->edit_units_count; 01167 mxf->last_indexed_edit_unit += mxf->edit_units_count; 01168 mxf->edit_units_count = 0; 01169 } 01170 } 01171 01172 static void mxf_write_klv_fill(AVFormatContext *s) 01173 { 01174 unsigned pad = klv_fill_size(avio_tell(s->pb)); 01175 if (pad) { 01176 avio_write(s->pb, klv_fill_key, 16); 01177 pad -= 16 + 4; 01178 klv_encode_ber4_length(s->pb, pad); 01179 for (; pad; pad--) 01180 avio_w8(s->pb, 0); 01181 assert(!(avio_tell(s->pb) & (KAG_SIZE-1))); 01182 } 01183 } 01184 01185 static void mxf_write_partition(AVFormatContext *s, int bodysid, 01186 int indexsid, 01187 const uint8_t *key, int write_metadata) 01188 { 01189 MXFContext *mxf = s->priv_data; 01190 AVIOContext *pb = s->pb; 01191 int64_t header_byte_count_offset; 01192 unsigned index_byte_count = 0; 01193 uint64_t partition_offset = avio_tell(pb); 01194 01195 if (!mxf->edit_unit_byte_count && mxf->edit_units_count) 01196 index_byte_count = 85 + 12+(s->nb_streams+1)*6 + 01197 12+mxf->edit_units_count*(11+mxf->slice_count*4); 01198 else if (mxf->edit_unit_byte_count && indexsid) 01199 index_byte_count = 80; 01200 01201 if (index_byte_count) { 01202 // add encoded ber length 01203 index_byte_count += 16 + klv_ber_length(index_byte_count); 01204 index_byte_count += klv_fill_size(index_byte_count); 01205 } 01206 01207 if (!memcmp(key, body_partition_key, 16)) { 01208 mxf->body_partition_offset = 01209 av_realloc(mxf->body_partition_offset, 01210 (mxf->body_partitions_count+1)* 01211 sizeof(*mxf->body_partition_offset)); 01212 mxf->body_partition_offset[mxf->body_partitions_count++] = partition_offset; 01213 } 01214 01215 // write klv 01216 avio_write(pb, key, 16); 01217 klv_encode_ber_length(pb, 88 + 16 * mxf->essence_container_count); 01218 01219 // write partition value 01220 avio_wb16(pb, 1); // majorVersion 01221 avio_wb16(pb, 2); // minorVersion 01222 avio_wb32(pb, KAG_SIZE); // KAGSize 01223 01224 avio_wb64(pb, partition_offset); // ThisPartition 01225 01226 if (!memcmp(key, body_partition_key, 16) && mxf->body_partitions_count > 1) 01227 avio_wb64(pb, mxf->body_partition_offset[mxf->body_partitions_count-2]); // PreviousPartition 01228 else if (!memcmp(key, footer_partition_key, 16) && mxf->body_partitions_count) 01229 avio_wb64(pb, mxf->body_partition_offset[mxf->body_partitions_count-1]); // PreviousPartition 01230 else 01231 avio_wb64(pb, 0); 01232 01233 avio_wb64(pb, mxf->footer_partition_offset); // footerPartition 01234 01235 // set offset 01236 header_byte_count_offset = avio_tell(pb); 01237 avio_wb64(pb, 0); // headerByteCount, update later 01238 01239 // indexTable 01240 avio_wb64(pb, index_byte_count); // indexByteCount 01241 avio_wb32(pb, index_byte_count ? indexsid : 0); // indexSID 01242 01243 // BodyOffset 01244 if (bodysid && mxf->edit_units_count && mxf->body_partitions_count) { 01245 avio_wb64(pb, mxf->body_offset); 01246 } else 01247 avio_wb64(pb, 0); 01248 01249 avio_wb32(pb, bodysid); // bodySID 01250 01251 // operational pattern 01252 avio_write(pb, op1a_ul, 16); 01253 01254 // essence container 01255 mxf_write_essence_container_refs(s); 01256 01257 if (write_metadata) { 01258 // mark the start of the headermetadata and calculate metadata size 01259 int64_t pos, start; 01260 unsigned header_byte_count; 01261 01262 mxf_write_klv_fill(s); 01263 start = avio_tell(s->pb); 01264 mxf_write_primer_pack(s); 01265 mxf_write_header_metadata_sets(s); 01266 pos = avio_tell(s->pb); 01267 header_byte_count = pos - start + klv_fill_size(pos); 01268 01269 // update header_byte_count 01270 avio_seek(pb, header_byte_count_offset, SEEK_SET); 01271 avio_wb64(pb, header_byte_count); 01272 avio_seek(pb, pos, SEEK_SET); 01273 } 01274 01275 avio_flush(pb); 01276 } 01277 01278 static const UID mxf_mpeg2_codec_uls[] = { 01279 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x01,0x10,0x00 }, // MP-ML I-Frame 01280 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x01,0x11,0x00 }, // MP-ML Long GOP 01281 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x02,0x02,0x00 }, // 422P-ML I-Frame 01282 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x02,0x03,0x00 }, // 422P-ML Long GOP 01283 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x03,0x02,0x00 }, // MP-HL I-Frame 01284 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x03,0x03,0x00 }, // MP-HL Long GOP 01285 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x04,0x02,0x00 }, // 422P-HL I-Frame 01286 { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x04,0x03,0x00 }, // 422P-HL Long GOP 01287 }; 01288 01289 static const UID *mxf_get_mpeg2_codec_ul(AVCodecContext *avctx) 01290 { 01291 int long_gop = avctx->gop_size > 1 || avctx->has_b_frames; 01292 01293 if (avctx->profile == 4) { // Main 01294 if (avctx->level == 8) // Main 01295 return &mxf_mpeg2_codec_uls[0+long_gop]; 01296 else if (avctx->level == 4) // High 01297 return &mxf_mpeg2_codec_uls[4+long_gop]; 01298 } else if (avctx->profile == 0) { // 422 01299 if (avctx->level == 5) // Main 01300 return &mxf_mpeg2_codec_uls[2+long_gop]; 01301 else if (avctx->level == 2) // High 01302 return &mxf_mpeg2_codec_uls[6+long_gop]; 01303 } 01304 return NULL; 01305 } 01306 01307 static int mxf_parse_mpeg2_frame(AVFormatContext *s, AVStream *st, 01308 AVPacket *pkt, MXFIndexEntry *e) 01309 { 01310 MXFStreamContext *sc = st->priv_data; 01311 MXFContext *mxf = s->priv_data; 01312 uint32_t c = -1; 01313 int i; 01314 01315 for(i = 0; i < pkt->size - 4; i++) { 01316 c = (c<<8) + pkt->data[i]; 01317 if (c == 0x1b5) { 01318 if ((pkt->data[i+1] & 0xf0) == 0x10) { // seq ext 01319 st->codec->profile = pkt->data[i+1] & 0x07; 01320 st->codec->level = pkt->data[i+2] >> 4; 01321 } else if (i + 5 < pkt->size && (pkt->data[i+1] & 0xf0) == 0x80) { // pict coding ext 01322 sc->interlaced = !(pkt->data[i+5] & 0x80); // progressive frame 01323 break; 01324 } 01325 } else if (c == 0x1b8) { // gop 01326 if (pkt->data[i+4]>>6 & 0x01) { // closed 01327 sc->closed_gop = 1; 01328 if (e->flags & 0x40) // sequence header present 01329 e->flags |= 0x80; // random access 01330 } 01331 if (!mxf->header_written) { 01332 unsigned hours = (pkt->data[i+1]>>2) & 0x1f; 01333 unsigned minutes = ((pkt->data[i+1] & 0x03) << 4) | (pkt->data[i+2]>>4); 01334 unsigned seconds = ((pkt->data[i+2] & 0x07) << 3) | (pkt->data[i+3]>>5); 01335 unsigned frames = ((pkt->data[i+3] & 0x1f) << 1) | (pkt->data[i+4]>>7); 01336 mxf->timecode_drop_frame = !!(pkt->data[i+1] & 0x80); 01337 mxf->timecode_start = (hours*3600 + minutes*60 + seconds) * 01338 mxf->timecode_base + frames; 01339 if (mxf->timecode_drop_frame) { 01340 unsigned tminutes = 60 * hours + minutes; 01341 mxf->timecode_start -= 2 * (tminutes - tminutes / 10); 01342 } 01343 av_log(s, AV_LOG_DEBUG, "frame %d %d:%d:%d%c%d\n", mxf->timecode_start, 01344 hours, minutes, seconds, mxf->timecode_drop_frame ? ';':':', frames); 01345 } 01346 } else if (c == 0x1b3) { // seq 01347 e->flags |= 0x40; 01348 switch ((pkt->data[i+4]>>4) & 0xf) { 01349 case 2: sc->aspect_ratio = (AVRational){ 4, 3}; break; 01350 case 3: sc->aspect_ratio = (AVRational){ 16, 9}; break; 01351 case 4: sc->aspect_ratio = (AVRational){221,100}; break; 01352 default: 01353 av_reduce(&sc->aspect_ratio.num, &sc->aspect_ratio.den, 01354 st->codec->width, st->codec->height, 1024*1024); 01355 } 01356 } else if (c == 0x100) { // pic 01357 int pict_type = (pkt->data[i+2]>>3) & 0x07; 01358 e->temporal_ref = (pkt->data[i+1]<<2) | (pkt->data[i+2]>>6); 01359 if (pict_type == 2) { // P frame 01360 e->flags |= 0x22; 01361 sc->closed_gop = 0; // reset closed gop, don't matter anymore 01362 } else if (pict_type == 3) { // B frame 01363 if (sc->closed_gop) 01364 e->flags |= 0x13; // only backward prediction 01365 else 01366 e->flags |= 0x33; 01367 sc->temporal_reordering = -1; 01368 } else if (!pict_type) { 01369 av_log(s, AV_LOG_ERROR, "error parsing mpeg2 frame\n"); 01370 return 0; 01371 } 01372 } 01373 } 01374 if (s->oformat != &ff_mxf_d10_muxer) 01375 sc->codec_ul = mxf_get_mpeg2_codec_ul(st->codec); 01376 return !!sc->codec_ul; 01377 } 01378 01379 static uint64_t mxf_parse_timestamp(time_t timestamp) 01380 { 01381 struct tm *time = gmtime(×tamp); 01382 if (!time) 01383 return 0; 01384 return (uint64_t)(time->tm_year+1900) << 48 | 01385 (uint64_t)(time->tm_mon+1) << 40 | 01386 (uint64_t) time->tm_mday << 32 | 01387 time->tm_hour << 24 | 01388 time->tm_min << 16 | 01389 time->tm_sec << 8; 01390 } 01391 01392 static void mxf_gen_umid(AVFormatContext *s) 01393 { 01394 MXFContext *mxf = s->priv_data; 01395 uint32_t seed = av_get_random_seed(); 01396 uint64_t umid = seed + 0x5294713400000000LL; 01397 01398 AV_WB64(mxf->umid , umid); 01399 AV_WB64(mxf->umid+8, umid>>8); 01400 01401 mxf->instance_number = seed; 01402 } 01403 01404 static int mxf_write_header(AVFormatContext *s) 01405 { 01406 MXFContext *mxf = s->priv_data; 01407 int i; 01408 uint8_t present[FF_ARRAY_ELEMS(mxf_essence_container_uls)] = {0}; 01409 const int *samples_per_frame = NULL; 01410 01411 if (!s->nb_streams) 01412 return -1; 01413 01414 for (i = 0; i < s->nb_streams; i++) { 01415 AVStream *st = s->streams[i]; 01416 MXFStreamContext *sc = av_mallocz(sizeof(*sc)); 01417 if (!sc) 01418 return AVERROR(ENOMEM); 01419 st->priv_data = sc; 01420 01421 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) { 01422 if (i != 0) { 01423 av_log(s, AV_LOG_ERROR, "video stream must be first track\n"); 01424 return -1; 01425 } 01426 if (fabs(av_q2d(st->codec->time_base) - 1/25.0) < 0.0001) { 01427 samples_per_frame = PAL_samples_per_frame; 01428 mxf->time_base = (AVRational){ 1, 25 }; 01429 mxf->timecode_base = 25; 01430 } else if (fabs(av_q2d(st->codec->time_base) - 1001/30000.0) < 0.0001) { 01431 samples_per_frame = NTSC_samples_per_frame; 01432 mxf->time_base = (AVRational){ 1001, 30000 }; 01433 mxf->timecode_base = 30; 01434 } else { 01435 av_log(s, AV_LOG_ERROR, "unsupported video frame rate\n"); 01436 return -1; 01437 } 01438 av_set_pts_info(st, 64, mxf->time_base.num, mxf->time_base.den); 01439 if (s->oformat == &ff_mxf_d10_muxer) { 01440 if (st->codec->bit_rate == 50000000) 01441 if (mxf->time_base.den == 25) sc->index = 3; 01442 else sc->index = 5; 01443 else if (st->codec->bit_rate == 40000000) 01444 if (mxf->time_base.den == 25) sc->index = 7; 01445 else sc->index = 9; 01446 else if (st->codec->bit_rate == 30000000) 01447 if (mxf->time_base.den == 25) sc->index = 11; 01448 else sc->index = 13; 01449 else { 01450 av_log(s, AV_LOG_ERROR, "error MXF D-10 only support 30/40/50 mbit/s\n"); 01451 return -1; 01452 } 01453 01454 mxf->edit_unit_byte_count = KAG_SIZE; // system element 01455 mxf->edit_unit_byte_count += 16 + 4 + (uint64_t)st->codec->bit_rate * 01456 mxf->time_base.num / (8*mxf->time_base.den); 01457 mxf->edit_unit_byte_count += klv_fill_size(mxf->edit_unit_byte_count); 01458 mxf->edit_unit_byte_count += 16 + 4 + 4 + samples_per_frame[0]*8*4; 01459 mxf->edit_unit_byte_count += klv_fill_size(mxf->edit_unit_byte_count); 01460 } 01461 } else if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) { 01462 if (st->codec->sample_rate != 48000) { 01463 av_log(s, AV_LOG_ERROR, "only 48khz is implemented\n"); 01464 return -1; 01465 } 01466 av_set_pts_info(st, 64, 1, st->codec->sample_rate); 01467 if (s->oformat == &ff_mxf_d10_muxer) { 01468 if (st->index != 1) { 01469 av_log(s, AV_LOG_ERROR, "MXF D-10 only support one audio track\n"); 01470 return -1; 01471 } 01472 if (st->codec->codec_id != CODEC_ID_PCM_S16LE && 01473 st->codec->codec_id != CODEC_ID_PCM_S24LE) { 01474 av_log(s, AV_LOG_ERROR, "MXF D-10 only support 16 or 24 bits le audio\n"); 01475 } 01476 sc->index = ((MXFStreamContext*)s->streams[0]->priv_data)->index + 1; 01477 } else 01478 mxf->slice_count = 1; 01479 } 01480 01481 if (!sc->index) { 01482 sc->index = mxf_get_essence_container_ul_index(st->codec->codec_id); 01483 if (sc->index == -1) { 01484 av_log(s, AV_LOG_ERROR, "track %d: could not find essence container ul, " 01485 "codec not currently supported in container\n", i); 01486 return -1; 01487 } 01488 } 01489 01490 sc->codec_ul = &mxf_essence_container_uls[sc->index].codec_ul; 01491 01492 memcpy(sc->track_essence_element_key, mxf_essence_container_uls[sc->index].element_ul, 15); 01493 sc->track_essence_element_key[15] = present[sc->index]; 01494 PRINT_KEY(s, "track essence element key", sc->track_essence_element_key); 01495 01496 if (!present[sc->index]) 01497 mxf->essence_container_count++; 01498 present[sc->index]++; 01499 } 01500 01501 if (s->oformat == &ff_mxf_d10_muxer) { 01502 mxf->essence_container_count = 1; 01503 } 01504 01505 if (!(s->streams[0]->codec->flags & CODEC_FLAG_BITEXACT)) 01506 mxf_gen_umid(s); 01507 01508 for (i = 0; i < s->nb_streams; i++) { 01509 MXFStreamContext *sc = s->streams[i]->priv_data; 01510 // update element count 01511 sc->track_essence_element_key[13] = present[sc->index]; 01512 sc->order = AV_RB32(sc->track_essence_element_key+12); 01513 } 01514 01515 if (s->timestamp) 01516 mxf->timestamp = mxf_parse_timestamp(s->timestamp); 01517 mxf->duration = -1; 01518 01519 mxf->timecode_track = av_mallocz(sizeof(*mxf->timecode_track)); 01520 if (!mxf->timecode_track) 01521 return AVERROR(ENOMEM); 01522 mxf->timecode_track->priv_data = av_mallocz(sizeof(MXFStreamContext)); 01523 if (!mxf->timecode_track->priv_data) 01524 return AVERROR(ENOMEM); 01525 mxf->timecode_track->index = -1; 01526 01527 if (!samples_per_frame) 01528 samples_per_frame = PAL_samples_per_frame; 01529 01530 if (ff_audio_interleave_init(s, samples_per_frame, mxf->time_base) < 0) 01531 return -1; 01532 01533 return 0; 01534 } 01535 01536 static const uint8_t system_metadata_pack_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0D,0x01,0x03,0x01,0x04,0x01,0x01,0x00 }; 01537 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 }; 01538 01539 static uint32_t ff_framenum_to_12m_time_code(unsigned frame, int drop, int fps) 01540 { 01541 return (0 << 31) | // color frame flag 01542 (drop << 30) | // drop frame flag 01543 ( ((frame % fps) / 10) << 28) | // tens of frames 01544 ( ((frame % fps) % 10) << 24) | // units of frames 01545 (0 << 23) | // field phase (NTSC), b0 (PAL) 01546 ((((frame / fps) % 60) / 10) << 20) | // tens of seconds 01547 ((((frame / fps) % 60) % 10) << 16) | // units of seconds 01548 (0 << 15) | // b0 (NTSC), b2 (PAL) 01549 ((((frame / (fps * 60)) % 60) / 10) << 12) | // tens of minutes 01550 ((((frame / (fps * 60)) % 60) % 10) << 8) | // units of minutes 01551 (0 << 7) | // b1 01552 (0 << 6) | // b2 (NSC), field phase (PAL) 01553 ((((frame / (fps * 3600) % 24)) / 10) << 4) | // tens of hours 01554 ( (frame / (fps * 3600) % 24)) % 10; // units of hours 01555 } 01556 01557 static void mxf_write_system_item(AVFormatContext *s) 01558 { 01559 MXFContext *mxf = s->priv_data; 01560 AVIOContext *pb = s->pb; 01561 unsigned frame; 01562 uint32_t time_code; 01563 01564 frame = mxf->timecode_start + mxf->last_indexed_edit_unit + mxf->edit_units_count; 01565 01566 // write system metadata pack 01567 avio_write(pb, system_metadata_pack_key, 16); 01568 klv_encode_ber4_length(pb, 57); 01569 avio_w8(pb, 0x5c); // UL, user date/time stamp, picture and sound item present 01570 avio_w8(pb, 0x04); // content package rate 01571 avio_w8(pb, 0x00); // content package type 01572 avio_wb16(pb, 0x00); // channel handle 01573 avio_wb16(pb, frame); // continuity count 01574 if (mxf->essence_container_count > 1) 01575 avio_write(pb, multiple_desc_ul, 16); 01576 else { 01577 MXFStreamContext *sc = s->streams[0]->priv_data; 01578 avio_write(pb, mxf_essence_container_uls[sc->index].container_ul, 16); 01579 } 01580 avio_w8(pb, 0); 01581 avio_wb64(pb, 0); 01582 avio_wb64(pb, 0); // creation date/time stamp 01583 01584 avio_w8(pb, 0x81); // SMPTE 12M time code 01585 time_code = ff_framenum_to_12m_time_code(frame, mxf->timecode_drop_frame, mxf->timecode_base); 01586 avio_wb32(pb, time_code); 01587 avio_wb32(pb, 0); // binary group data 01588 avio_wb64(pb, 0); 01589 01590 // write system metadata package set 01591 avio_write(pb, system_metadata_package_set_key, 16); 01592 klv_encode_ber4_length(pb, 35); 01593 avio_w8(pb, 0x83); // UMID 01594 avio_wb16(pb, 0x20); 01595 mxf_write_umid(s, 1); 01596 } 01597 01598 static void mxf_write_d10_video_packet(AVFormatContext *s, AVStream *st, AVPacket *pkt) 01599 { 01600 MXFContext *mxf = s->priv_data; 01601 AVIOContext *pb = s->pb; 01602 int packet_size = (uint64_t)st->codec->bit_rate*mxf->time_base.num / 01603 (8*mxf->time_base.den); // frame size 01604 int pad; 01605 01606 packet_size += 16 + 4; 01607 packet_size += klv_fill_size(packet_size); 01608 01609 klv_encode_ber4_length(pb, pkt->size); 01610 avio_write(pb, pkt->data, pkt->size); 01611 01612 // ensure CBR muxing by padding to correct video frame size 01613 pad = packet_size - pkt->size - 16 - 4; 01614 if (pad > 20) { 01615 avio_write(s->pb, klv_fill_key, 16); 01616 pad -= 16 + 4; 01617 klv_encode_ber4_length(s->pb, pad); 01618 for (; pad; pad--) 01619 avio_w8(s->pb, 0); 01620 assert(!(avio_tell(s->pb) & (KAG_SIZE-1))); 01621 } else { 01622 av_log(s, AV_LOG_WARNING, "cannot fill d-10 video packet\n"); 01623 for (; pad > 0; pad--) 01624 avio_w8(s->pb, 0); 01625 } 01626 } 01627 01628 static void mxf_write_d10_audio_packet(AVFormatContext *s, AVStream *st, AVPacket *pkt) 01629 { 01630 MXFContext *mxf = s->priv_data; 01631 AVIOContext *pb = s->pb; 01632 int frame_size = pkt->size / st->codec->block_align; 01633 uint8_t *samples = pkt->data; 01634 uint8_t *end = pkt->data + pkt->size; 01635 int i; 01636 01637 klv_encode_ber4_length(pb, 4 + frame_size*4*8); 01638 01639 avio_w8(pb, (frame_size == 1920 ? 0 : (mxf->edit_units_count-1) % 5 + 1)); 01640 avio_wl16(pb, frame_size); 01641 avio_w8(pb, (1<<st->codec->channels)-1); 01642 01643 while (samples < end) { 01644 for (i = 0; i < st->codec->channels; i++) { 01645 uint32_t sample; 01646 if (st->codec->codec_id == CODEC_ID_PCM_S24LE) { 01647 sample = AV_RL24(samples)<< 4; 01648 samples += 3; 01649 } else { 01650 sample = AV_RL16(samples)<<12; 01651 samples += 2; 01652 } 01653 avio_wl32(pb, sample | i); 01654 } 01655 for (; i < 8; i++) 01656 avio_wl32(pb, i); 01657 } 01658 } 01659 01660 static int mxf_write_packet(AVFormatContext *s, AVPacket *pkt) 01661 { 01662 MXFContext *mxf = s->priv_data; 01663 AVIOContext *pb = s->pb; 01664 AVStream *st = s->streams[pkt->stream_index]; 01665 MXFStreamContext *sc = st->priv_data; 01666 MXFIndexEntry ie = {0}; 01667 01668 if (!mxf->edit_unit_byte_count && !(mxf->edit_units_count % EDIT_UNITS_PER_BODY)) { 01669 mxf->index_entries = av_realloc(mxf->index_entries, 01670 (mxf->edit_units_count + EDIT_UNITS_PER_BODY)*sizeof(*mxf->index_entries)); 01671 if (!mxf->index_entries) { 01672 av_log(s, AV_LOG_ERROR, "could not allocate index entries\n"); 01673 return -1; 01674 } 01675 } 01676 01677 if (st->codec->codec_id == CODEC_ID_MPEG2VIDEO) { 01678 if (!mxf_parse_mpeg2_frame(s, st, pkt, &ie)) { 01679 av_log(s, AV_LOG_ERROR, "could not get mpeg2 profile and level\n"); 01680 return -1; 01681 } 01682 } 01683 01684 if (!mxf->header_written) { 01685 if (mxf->edit_unit_byte_count) { 01686 mxf_write_partition(s, 1, 2, header_open_partition_key, 1); 01687 mxf_write_klv_fill(s); 01688 mxf_write_index_table_segment(s); 01689 } else { 01690 mxf_write_partition(s, 0, 0, header_open_partition_key, 1); 01691 } 01692 mxf->header_written = 1; 01693 } 01694 01695 if (st->index == 0) { 01696 if (!mxf->edit_unit_byte_count && 01697 (!mxf->edit_units_count || mxf->edit_units_count > EDIT_UNITS_PER_BODY) && 01698 !(ie.flags & 0x33)) { // I frame, Gop start 01699 mxf_write_klv_fill(s); 01700 mxf_write_partition(s, 1, 2, body_partition_key, 0); 01701 01702 mxf_write_klv_fill(s); 01703 mxf_write_index_table_segment(s); 01704 } 01705 01706 mxf_write_klv_fill(s); 01707 mxf_write_system_item(s); 01708 01709 if (!mxf->edit_unit_byte_count) { 01710 mxf->index_entries[mxf->edit_units_count].offset = mxf->body_offset; 01711 mxf->index_entries[mxf->edit_units_count].flags = ie.flags; 01712 mxf->index_entries[mxf->edit_units_count].temporal_ref = ie.temporal_ref; 01713 mxf->body_offset += KAG_SIZE; // size of system element 01714 } 01715 mxf->edit_units_count++; 01716 } else if (!mxf->edit_unit_byte_count && st->index == 1) { 01717 mxf->index_entries[mxf->edit_units_count-1].slice_offset = 01718 mxf->body_offset - mxf->index_entries[mxf->edit_units_count-1].offset; 01719 } 01720 01721 mxf_write_klv_fill(s); 01722 avio_write(pb, sc->track_essence_element_key, 16); // write key 01723 if (s->oformat == &ff_mxf_d10_muxer) { 01724 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) 01725 mxf_write_d10_video_packet(s, st, pkt); 01726 else 01727 mxf_write_d10_audio_packet(s, st, pkt); 01728 } else { 01729 klv_encode_ber4_length(pb, pkt->size); // write length 01730 avio_write(pb, pkt->data, pkt->size); 01731 mxf->body_offset += 16+4+pkt->size + klv_fill_size(16+4+pkt->size); 01732 } 01733 01734 avio_flush(pb); 01735 01736 return 0; 01737 } 01738 01739 static void mxf_write_random_index_pack(AVFormatContext *s) 01740 { 01741 MXFContext *mxf = s->priv_data; 01742 AVIOContext *pb = s->pb; 01743 uint64_t pos = avio_tell(pb); 01744 int i; 01745 01746 avio_write(pb, random_index_pack_key, 16); 01747 klv_encode_ber_length(pb, 28 + 12*mxf->body_partitions_count); 01748 01749 if (mxf->edit_unit_byte_count) 01750 avio_wb32(pb, 1); // BodySID of header partition 01751 else 01752 avio_wb32(pb, 0); 01753 avio_wb64(pb, 0); // offset of header partition 01754 01755 for (i = 0; i < mxf->body_partitions_count; i++) { 01756 avio_wb32(pb, 1); // BodySID 01757 avio_wb64(pb, mxf->body_partition_offset[i]); 01758 } 01759 01760 avio_wb32(pb, 0); // BodySID of footer partition 01761 avio_wb64(pb, mxf->footer_partition_offset); 01762 01763 avio_wb32(pb, avio_tell(pb) - pos + 4); 01764 } 01765 01766 static int mxf_write_footer(AVFormatContext *s) 01767 { 01768 MXFContext *mxf = s->priv_data; 01769 AVIOContext *pb = s->pb; 01770 01771 mxf->duration = mxf->last_indexed_edit_unit + mxf->edit_units_count; 01772 01773 mxf_write_klv_fill(s); 01774 mxf->footer_partition_offset = avio_tell(pb); 01775 if (mxf->edit_unit_byte_count) { // no need to repeat index 01776 mxf_write_partition(s, 0, 0, footer_partition_key, 0); 01777 } else { 01778 mxf_write_partition(s, 0, 2, footer_partition_key, 0); 01779 01780 mxf_write_klv_fill(s); 01781 mxf_write_index_table_segment(s); 01782 } 01783 01784 mxf_write_klv_fill(s); 01785 mxf_write_random_index_pack(s); 01786 01787 if (s->pb->seekable) { 01788 avio_seek(pb, 0, SEEK_SET); 01789 if (mxf->edit_unit_byte_count) { 01790 mxf_write_partition(s, 1, 2, header_closed_partition_key, 1); 01791 mxf_write_klv_fill(s); 01792 mxf_write_index_table_segment(s); 01793 } else { 01794 mxf_write_partition(s, 0, 0, header_closed_partition_key, 1); 01795 } 01796 } 01797 01798 avio_flush(pb); 01799 01800 ff_audio_interleave_close(s); 01801 01802 av_freep(&mxf->index_entries); 01803 av_freep(&mxf->body_partition_offset); 01804 av_freep(&mxf->timecode_track->priv_data); 01805 av_freep(&mxf->timecode_track); 01806 01807 mxf_free(s); 01808 01809 return 0; 01810 } 01811 01812 static int mxf_interleave_get_packet(AVFormatContext *s, AVPacket *out, AVPacket *pkt, int flush) 01813 { 01814 int i, stream_count = 0; 01815 01816 for (i = 0; i < s->nb_streams; i++) 01817 stream_count += !!s->streams[i]->last_in_packet_buffer; 01818 01819 if (stream_count && (s->nb_streams == stream_count || flush)) { 01820 AVPacketList *pktl = s->packet_buffer; 01821 if (s->nb_streams != stream_count) { 01822 AVPacketList *last = NULL; 01823 // find last packet in edit unit 01824 while (pktl) { 01825 if (!stream_count || pktl->pkt.stream_index == 0) 01826 break; 01827 last = pktl; 01828 pktl = pktl->next; 01829 stream_count--; 01830 } 01831 // purge packet queue 01832 while (pktl) { 01833 AVPacketList *next = pktl->next; 01834 01835 if(s->streams[pktl->pkt.stream_index]->last_in_packet_buffer == pktl) 01836 s->streams[pktl->pkt.stream_index]->last_in_packet_buffer= NULL; 01837 av_free_packet(&pktl->pkt); 01838 av_freep(&pktl); 01839 pktl = next; 01840 } 01841 if (last) 01842 last->next = NULL; 01843 else { 01844 s->packet_buffer = NULL; 01845 s->packet_buffer_end= NULL; 01846 goto out; 01847 } 01848 pktl = s->packet_buffer; 01849 } 01850 01851 *out = pktl->pkt; 01852 //av_log(s, AV_LOG_DEBUG, "out st:%d dts:%lld\n", (*out).stream_index, (*out).dts); 01853 s->packet_buffer = pktl->next; 01854 if(s->streams[pktl->pkt.stream_index]->last_in_packet_buffer == pktl) 01855 s->streams[pktl->pkt.stream_index]->last_in_packet_buffer= NULL; 01856 if(!s->packet_buffer) 01857 s->packet_buffer_end= NULL; 01858 av_freep(&pktl); 01859 return 1; 01860 } else { 01861 out: 01862 av_init_packet(out); 01863 return 0; 01864 } 01865 } 01866 01867 static int mxf_compare_timestamps(AVFormatContext *s, AVPacket *next, AVPacket *pkt) 01868 { 01869 MXFStreamContext *sc = s->streams[pkt ->stream_index]->priv_data; 01870 MXFStreamContext *sc2 = s->streams[next->stream_index]->priv_data; 01871 01872 return next->dts > pkt->dts || 01873 (next->dts == pkt->dts && sc->order < sc2->order); 01874 } 01875 01876 static int mxf_interleave(AVFormatContext *s, AVPacket *out, AVPacket *pkt, int flush) 01877 { 01878 return ff_audio_rechunk_interleave(s, out, pkt, flush, 01879 mxf_interleave_get_packet, mxf_compare_timestamps); 01880 } 01881 01882 AVOutputFormat ff_mxf_muxer = { 01883 "mxf", 01884 NULL_IF_CONFIG_SMALL("Material eXchange Format"), 01885 "application/mxf", 01886 "mxf", 01887 sizeof(MXFContext), 01888 CODEC_ID_PCM_S16LE, 01889 CODEC_ID_MPEG2VIDEO, 01890 mxf_write_header, 01891 mxf_write_packet, 01892 mxf_write_footer, 01893 AVFMT_NOTIMESTAMPS, 01894 NULL, 01895 mxf_interleave, 01896 }; 01897 01898 AVOutputFormat ff_mxf_d10_muxer = { 01899 "mxf_d10", 01900 NULL_IF_CONFIG_SMALL("Material eXchange Format, D-10 Mapping"), 01901 "application/mxf", 01902 NULL, 01903 sizeof(MXFContext), 01904 CODEC_ID_PCM_S16LE, 01905 CODEC_ID_MPEG2VIDEO, 01906 mxf_write_header, 01907 mxf_write_packet, 01908 mxf_write_footer, 01909 AVFMT_NOTIMESTAMPS, 01910 NULL, 01911 mxf_interleave, 01912 };