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

Generated on Sat Feb 16 2013 09:23:15 for ffmpeg by  doxygen 1.7.1