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