Revision 85d0d114

View differences:

libavformat/mxfenc.c
42 42
static const int PAL_samples_per_frame[]  = { 1920, 0 };
43 43

  
44 44
#define MXF_INDEX_CLUSTER_SIZE 4096
45
#define KAG_SIZE 512
45 46

  
46 47
typedef struct {
47 48
    AVFifoBuffer fifo;
......
140 141
static const uint8_t random_index_pack_key[]       = { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0D,0x01,0x02,0x01,0x01,0x11,0x01,0x00 };
141 142
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
142 143
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
144
static const uint8_t klv_fill_key[]                = { 0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x03,0x01,0x02,0x10,0x01,0x00,0x00,0x00 };
143 145

  
144 146
/**
145 147
 * partial key for header metadata
......
248 250
    put_be32(pb, 16);
249 251
}
250 252

  
253
static int klv_ber_length(uint64_t len)
254
{
255
    if (len < 128)
256
        return 1;
257
    else
258
        return (av_log2(len) >> 3) + 2;
259
}
260

  
251 261
static int klv_encode_ber_length(ByteIOContext *pb, uint64_t len)
252 262
{
253 263
    // Determine the best BER size
......
840 850
    return 0;
841 851
}
842 852

  
853
static unsigned klv_fill_size(AVFormatContext *s)
854
{
855
    unsigned pad = KAG_SIZE - (url_ftell(s->pb) & (KAG_SIZE-1));
856
    if (pad < 17) // smallest fill item possible
857
        return pad + KAG_SIZE;
858
    else
859
        return pad & (KAG_SIZE-1);
860
}
861

  
843 862
static int mxf_write_index_table_segment(AVFormatContext *s)
844 863
{
845 864
    MXFContext *mxf = s->priv_data;
......
963 982
    // write partition value
964 983
    put_be16(pb, 1); // majorVersion
965 984
    put_be16(pb, 2); // minorVersion
966
    put_be32(pb, 1); // kagSize
985
    put_be32(pb, KAG_SIZE); // KAGSize
967 986

  
968 987
    put_be64(pb, url_ftell(pb) - 25); // thisPartition
969 988
    put_be64(pb, 0); // previousPartition
......
988 1007
    if (write_metadata) {
989 1008
        // mark the start of the headermetadata and calculate metadata size
990 1009
        int64_t pos, start = url_ftell(s->pb);
1010
        unsigned header_byte_count;
1011

  
991 1012
        mxf_write_primer_pack(s);
992 1013
        mxf_write_header_metadata_sets(s);
993 1014
        pos = url_ftell(s->pb);
1015
        header_byte_count = pos - start + klv_fill_size(s);
1016

  
994 1017
        // update header_byte_count
995 1018
        url_fseek(pb, header_byte_count_offset, SEEK_SET);
996
        put_be64(pb, pos - start);
1019
        put_be64(pb, header_byte_count);
997 1020
        url_fseek(pb, pos, SEEK_SET);
998 1021
    }
999 1022

  
......
1209 1232
    return 0;
1210 1233
}
1211 1234

  
1235
static void mxf_write_klv_fill(AVFormatContext *s)
1236
{
1237
    unsigned pad = klv_fill_size(s);
1238
    if (pad) {
1239
        put_buffer(s->pb, klv_fill_key, 16);
1240
        pad -= 16;
1241
        pad -= klv_ber_length(pad);
1242
        klv_encode_ber_length(s->pb, pad);
1243
        for (; pad > 7; pad -= 8)
1244
            put_be64(s->pb, 0);
1245
        for (; pad; pad--)
1246
            put_byte(s->pb, 0);
1247
        assert(!(url_ftell(s->pb) & (KAG_SIZE-1)));
1248
    }
1249
}
1250

  
1212 1251
static int mxf_write_packet(AVFormatContext *s, AVPacket *pkt)
1213 1252
{
1214 1253
    MXFContext *mxf = s->priv_data;
......
1238 1277
        mxf->header_written = 1;
1239 1278
    }
1240 1279

  
1280
    mxf_write_klv_fill(s);
1281

  
1241 1282
    if (st->index == mxf->edit_unit_start) {
1242 1283
        mxf->index_entries[mxf->edit_units_count].offset = url_ftell(pb);
1243 1284
        mxf->index_entries[mxf->edit_units_count].slice_offset[st->index] = 0;
......
1284 1325
        mxf->edit_units_count*(11+(s->nb_streams-1)*4);
1285 1326

  
1286 1327
    // add encoded ber length
1287
    index_byte_count += 16 +
1288
        (index_byte_count < 128 ? 1 : (av_log2(index_byte_count) >> 3) + 2);
1328
    index_byte_count += 16 + klv_ber_length(index_byte_count);
1329

  
1330
    mxf_write_klv_fill(s);
1289 1331

  
1290 1332
    mxf->footer_partition_offset = url_ftell(pb);
1291 1333
    mxf_write_partition(s, 0, 2, index_byte_count, footer_partition_key, 0);

Also available in: Unified diff