• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

libavformat/mxfenc.c

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