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