Example #1
0
static int write_stream_data(AVFormatContext *s, AVStream *st, int flag)
{
    AVIOContext *pb = s->pb;
    int ret;

    if (!flag) {
        write_chunk_header2(s, &ff_stream_guid, 0x80000000 | (st->index + INDEX_BASE));
        avio_wl32(pb, 0x00000001);
        avio_wl32(pb, st->index + INDEX_BASE); //stream_id
        avio_wl32(pb, 0x00000001);
        write_pad(pb, 8);
    } else {
        write_chunk_header2(s, &ff_stream2_guid, 0x80000000 | (st->index + INDEX_BASE));
        write_pad(pb, 4);
    }

    ret = write_stream_codec_info(s, st);
    if (ret < 0) {
        av_log(s, AV_LOG_ERROR, "write stream codec info failed codec_type(0x%x)\n", st->codec->codec_type);
        return -1;
    }
    finish_chunk(s);

    avpriv_set_pts_info(st, 64, 1, 10000000);

    return 0;
}
Example #2
0
static int write_header(AVFormatContext *s)
{
    AVIOContext *pb = s->pb;
    WtvContext *wctx = s->priv_data;
    int i, pad, ret;
    AVStream *st;

    wctx->last_chunk_pos     = -1;
    wctx->last_timestamp_pos = -1;

    ff_put_guid(pb, &ff_wtv_guid);
    ff_put_guid(pb, &sub_wtv_guid);

    avio_wl32(pb, 0x01);
    avio_wl32(pb, 0x02);
    avio_wl32(pb, 1 << WTV_SECTOR_BITS);
    avio_wl32(pb, 1 << WTV_BIGSECTOR_BITS);

    //write initial root fields
    avio_wl32(pb, 0); // root_size, update later
    write_pad(pb, 4);
    avio_wl32(pb, 0); // root_sector, update it later.

    write_pad(pb, 32);
    avio_wl32(pb, 0); // file ends pointer, update it later.

    pad = (1 << WTV_SECTOR_BITS) - avio_tell(pb);
    write_pad(pb, pad);

    wctx->timeline_start_pos = avio_tell(pb);

    wctx->serial = 1;
    wctx->last_chunk_pos = -1;
    wctx->first_video_flag = 1;

    for (i = 0; i < s->nb_streams; i++) {
        st = s->streams[i];
        ret = write_stream_codec(s, st);
        if (ret < 0) {
            av_log(s, AV_LOG_ERROR, "write stream codec failed codec_type(0x%x)\n", st->codec->codec_type);
            return -1;
        }
        if (!i)
            write_sync(s);
    }

    for (i = 0; i < s->nb_streams; i++) {
        st = s->streams[i];
        ret  = write_stream_data(s, st);
        if (ret < 0) {
            av_log(s, AV_LOG_ERROR, "write stream data failed codec_type(0x%x)\n", st->codec->codec_type);
            return -1;
        }
    }

    if (wctx->nb_index)
        write_index(s);

    return 0;
}
Example #3
0
static int write_stream_codec_info(AVFormatContext *s, AVStream *st)
{
    WtvContext *wctx = s->priv_data;
    const ff_asf_guid *g, *media_type, *format_type;
    AVIOContext *pb = s->pb;
    int64_t  hdr_pos_start;
    int hdr_size = 0;

    if (st->codec->codec_type  == AVMEDIA_TYPE_VIDEO) {
        g = get_codec_guid(st->codec->codec_id, ff_video_guids);
        media_type = &ff_mediatype_video;
        format_type = &ff_format_mpeg2_video;
    } else if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
        g = get_codec_guid(st->codec->codec_id, ff_codec_wav_guids);
        media_type = &ff_mediatype_audio;
        format_type = &ff_format_waveformatex;
    } else {
        av_log(s, AV_LOG_ERROR, "unknown codec_type (0x%x)\n", st->codec->codec_type);
        return -1;
    }

    if (g == NULL) {
        av_log(s, AV_LOG_ERROR, "can't get video codec_id (0x%x) guid.\n", st->codec->codec_id);
        return -1;
    }

    ff_put_guid(pb, media_type); // mediatype
    ff_put_guid(pb, &ff_mediasubtype_cpfilters_processed); // subtype
    write_pad(pb, 12);
    ff_put_guid(pb,&ff_format_cpfilters_processed); // format type
    avio_wl32(pb, 0); // size

    hdr_pos_start = avio_tell(pb);
    if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
        if (wctx->first_video_flag) {
            write_pad(pb, 216); //The size is sensitive.
            wctx->first_video_flag = 0;
        } else {
            write_pad(pb, 72); // aspect ratio
            ff_put_bmp_header(pb, st->codec, ff_codec_bmp_tags, 0);
        }
    } else {
        ff_put_wav_header(pb, st->codec);
    }
    hdr_size = avio_tell(pb) - hdr_pos_start;

    // seek back write hdr_size
    avio_seek(pb, -(hdr_size + 4), SEEK_CUR);
    avio_wl32(pb, hdr_size + 32);
    avio_seek(pb, hdr_size, SEEK_CUR);
    ff_put_guid(pb, g);           // actual_subtype
    ff_put_guid(pb, format_type); // actual_formattype

    return 0;
}
Example #4
0
static int write_table0_header_legacy_attrib(AVIOContext *pb)
{
    int pad = 0;
    avio_wl32(pb, 0xFFFFFFFF);
    write_pad(pb, 12);
    avio_write(pb, legacy_attrib, sizeof(legacy_attrib));
    pad = WTV_PAD8(sizeof(legacy_attrib)) - sizeof(legacy_attrib);
    write_pad(pb, pad);
    write_pad(pb, 32);
    return 48 + WTV_PAD8(sizeof(legacy_attrib));
}
Example #5
0
static int write_table0_header_events(AVIOContext *pb)
{
    avio_wl32(pb, 0x10);
    write_pad(pb, 84);
    avio_wl64(pb, 0x32);
    return 96;
}
Example #6
0
static int write_packet(AVFormatContext *s, AVPacket *pkt)
{
    AVIOContext *pb = s->pb;
    WtvContext  *wctx = s->priv_data;

    /* emit sync chunk and 'timeline.table.0.entries.Event' record every 50 frames */
    if (wctx->serial - (wctx->nb_sp_pairs ? wctx->sp_pairs[wctx->nb_sp_pairs - 1].serial : 0) >= 50)
        write_sync(s);

    /* emit 'table.0.entries.time' record every 500ms */
    if (pkt->pts != AV_NOPTS_VALUE && pkt->pts - (wctx->nb_st_pairs ? wctx->st_pairs[wctx->nb_st_pairs - 1].value : 0) >= 5000000)
        add_serial_pair(&wctx->st_pairs, &wctx->nb_st_pairs, wctx->serial, pkt->pts);

    if (pkt->pts != AV_NOPTS_VALUE && pkt->pts > wctx->last_pts) {
        wctx->last_pts = pkt->pts;
        wctx->last_serial = wctx->serial;
    }

    // write timestamp chunk
    write_timestamp(s, pkt);

    write_chunk_header(s, &ff_data_guid, pkt->size, INDEX_BASE + pkt->stream_index);
    avio_write(pb, pkt->data, pkt->size);
    write_pad(pb, WTV_PAD8(pkt->size) - pkt->size);

    wctx->serial++;
    avio_flush(pb);
    return 0;
}
Example #7
0
static int write_table0_header_time(AVIOContext *pb)
{
    avio_wl32(pb, 0x10);
    write_pad(pb, 76);
    avio_wl64(pb, 0x40);
    return 88;
}
Example #8
0
void SkPipeCanvas::onDrawTextRSXform(const void* text, size_t byteLength, const SkRSXform xform[],
                                     const SkRect* cull, const SkPaint& paint) {
    SkASSERT(byteLength);
    
    bool compact = fits_in(byteLength, 23);
    unsigned extra = compact ? (byteLength << 1) : 0;
    if (cull) {
        extra |= 1;
    }
    
    SkPipeWriter writer(this);
    writer.write32(pack_verb(SkPipeVerb::kDrawTextRSXform, extra));
    if (!compact) {
        writer.write32(SkToU32(byteLength));
    }
    write_pad(&writer, text, byteLength);

    int count = paint.countText(text, byteLength);
    writer.write32(count);  // maybe we can/should store this in extra as well?
    writer.write(xform, count * sizeof(SkRSXform));
    if (cull) {
        writer.writeRect(*cull);
    }
    write_paint(writer, paint, kText_PaintUsage);
}
Example #9
0
int SkPipeDeduper::findOrDefineImage(SkImage* image) {
    int index = fImages.find(image->uniqueID());
    SkASSERT(index >= 0);
    if (index) {
        if (show_deduper_traffic) {
            SkDebugf("  reuseImage(%d)\n", index - 1);
        }
        return index;
    }

    sk_sp<SkData> data = fIMSerializer ? fIMSerializer->serialize(image)
                                       : default_image_serializer(image);
    if (data) {
        index = fImages.add(image->uniqueID());
        SkASSERT(index > 0);
        SkASSERT(fits_in(index, 24));
        fStream->write32(pack_verb(SkPipeVerb::kDefineImage, index));

        uint32_t len = SkToU32(data->size());
        fStream->write32(SkAlign4(len));
        write_pad(fStream, data->data(), len);

        if (show_deduper_traffic) {
            int size = image->width() * image->height() << 2;
            SkDebugf("  defineImage(%d) %d -> %d\n", index - 1, size, len);
        }
        return index;
    }
    SkDebugf("+++ failed to encode image [%d %d]\n", image->width(), image->height());
    return 0;   // failed to encode
}
Example #10
0
int SkPipeDeduper::findOrDefineTypeface(SkTypeface* typeface) {
    if (!typeface) {
        return 0;   // default
    }

    int index = fTypefaces.find(typeface->uniqueID());
    SkASSERT(index >= 0);
    if (index) {
        if (show_deduper_traffic) {
            SkDebugf("  reuseTypeface(%d)\n", index - 1);
        }
        return index;
    }

    sk_sp<SkData> data = fTFSerializer ? fTFSerializer->serialize(typeface) : encode(typeface);
    if (data) {
        index = fTypefaces.add(typeface->uniqueID());
        SkASSERT(index > 0);
        SkASSERT(fits_in(index, 24));
        fStream->write32(pack_verb(SkPipeVerb::kDefineTypeface, index));

        uint32_t len = SkToU32(data->size());
        fStream->write32(SkAlign4(len));
        write_pad(fStream, data->data(), len);

        if (show_deduper_traffic) {
            SkDebugf("  defineTypeface(%d) %d\n", index - 1, len);
        }
        return index;
    }
    SkDebugf("+++ failed to encode typeface %d\n", typeface->uniqueID());
    return 0;   // failed to encode
}
Example #11
0
int SkPipeDeduper::findOrDefineFactory(SkFlattenable* flattenable) {
    if (!flattenable) {
        return 0;
    }

    int index = fFactories.find(flattenable->getFactory());
    SkASSERT(index >= 0);
    if (index) {
        if (show_deduper_traffic) {
            SkDebugf("  reuseFactory(%d)\n", index - 1);
        }
        return index;
    }

    index = fFactories.add(flattenable->getFactory());
    ASSERT_FITS_IN(index, kIndex_DefineFactoryExtraBits);
    const char* name = flattenable->getTypeName();
    size_t len = strlen(name);
    ASSERT_FITS_IN(len, kNameLength_DefineFactoryExtraBits);
    unsigned extra = (index << kNameLength_DefineFactoryExtraBits) | len;
    size_t prevWritten = fStream->bytesWritten();
    fStream->write32(pack_verb(SkPipeVerb::kDefineFactory, extra));
    write_pad(fStream, name, len + 1);
    if (false) {
        SkDebugf("  defineFactory(%d) %d %s\n",
             index - 1, SkToU32(fStream->bytesWritten() - prevWritten), name);
    }
    return index;
}
Example #12
0
static int write_root_table(AVFormatContext *s, int64_t sector_pos)
{
    AVIOContext *pb = s->pb;
    WtvContext  *wctx = s->priv_data;
    int size, pad;
    int i;

    const WTVRootEntryTable *h = wtv_root_entry_table;
    for (i = 0; i < sizeof(wtv_root_entry_table)/sizeof(WTVRootEntryTable); i++, h++) {
        WtvFile *w = &wctx->file[i];
        int filename_padding = WTV_PAD8(h->header_size) - h->header_size;
        WTVHeaderWriteFunc *write = h->write_header;
        int len = 0;
        int64_t len_pos;

        ff_put_guid(pb, &ff_dir_entry_guid);
        len_pos = avio_tell(pb);
        avio_wl16(pb, 40 + h->header_size + filename_padding + 8); // maybe updated later
        write_pad(pb, 6);
        avio_wl64(pb, write ? 0 : w->length);// maybe update later
        avio_wl32(pb, (h->header_size + filename_padding) >> 1);
        write_pad(pb, 4);

        avio_write(pb, h->header, h->header_size);
        write_pad(pb, filename_padding);

        if (write) {
            len = write(pb);
            // update length field
            avio_seek(pb, len_pos, SEEK_SET);
            avio_wl64(pb, 40 + h->header_size + filename_padding + len);
            avio_wl64(pb, len |(1ULL<<62) | (1ULL<<60));
            avio_seek(pb, 8 + h->header_size + filename_padding + len, SEEK_CUR);
        } else {
            avio_wl32(pb, w->first_sector);
            avio_wl32(pb, w->depth);
        }
    }

    // caculate root table size
    size = avio_tell(pb) - sector_pos;
    pad = WTV_SECTOR_SIZE- size;
    write_pad(pb, pad);

    return size;
}
Example #13
0
static void write_fat(AVIOContext *pb, int start_sector, int nb_sectors, int shift)
{
    int i;
    for (i = 0; i < nb_sectors; i++) {
        avio_wl32(pb, start_sector + (i << shift));
    }
    // pad left sector pointer size
    write_pad(pb, WTV_SECTOR_SIZE - ((nb_sectors << 2) % WTV_SECTOR_SIZE));
}
Example #14
0
static int write_stream_codec(AVFormatContext *s, AVStream * st)
{
    AVIOContext *pb = s->pb;
    int ret;
    write_chunk_header2(s, &ff_stream1_guid, 0x80000000 | 0x01);

    avio_wl32(pb,  0x01);
    write_pad(pb, 4);
    write_pad(pb, 4);

    ret = write_stream_codec_info(s, st);
    if (ret < 0) {
        av_log(s, AV_LOG_ERROR, "write stream codec info failed codec_type(0x%x)\n", st->codec->codec_type);
        return -1;
    }

    finish_chunk(s);
    return 0;
}
Example #15
0
void SkPipeCanvas::onDrawAnnotation(const SkRect& rect, const char key[], SkData* data) {
    const size_t len = strlen(key) + 1; // must write the trailing 0
    bool compact = fits_in(len, 23);
    uint32_t extra = compact ? (unsigned)len : 0;
    extra <<= 1;   // make room for has_data_sentinel
    if (data) {
        extra |= 1;
    }

    fStream->write32(pack_verb(SkPipeVerb::kDrawAnnotation, extra));
    fStream->write(&rect, sizeof(SkRect));
    if (!compact) {
        fStream->write32(SkToU32(len));
    }
    write_pad(fStream, key, len);
    if (data) {
        fStream->write32(SkToU32(data->size()));
        write_pad(fStream, data->data(), data->size());
    }
}
Example #16
0
static int write_stream_codec_info(AVFormatContext *s, AVStream *st)
{
    const ff_asf_guid *g, *media_type, *format_type;
    const AVCodecTag *tags;
    AVIOContext *pb = s->pb;
    int64_t  hdr_pos_start;
    int hdr_size = 0;

    if (st->codec->codec_type  == AVMEDIA_TYPE_VIDEO) {
        g = ff_get_codec_guid(st->codec->codec_id, ff_video_guids);
        media_type = &ff_mediatype_video;
        format_type = st->codec->codec_id == AV_CODEC_ID_MPEG2VIDEO ? &ff_format_mpeg2_video : &ff_format_videoinfo2;
        tags = ff_codec_bmp_tags;
    } else if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
        g = ff_get_codec_guid(st->codec->codec_id, ff_codec_wav_guids);
        media_type = &ff_mediatype_audio;
        format_type = &ff_format_waveformatex;
        tags = ff_codec_wav_tags;
    } else {
        av_log(s, AV_LOG_ERROR, "unknown codec_type (0x%x)\n", st->codec->codec_type);
        return -1;
    }

    ff_put_guid(pb, media_type); // mediatype
    ff_put_guid(pb, &ff_mediasubtype_cpfilters_processed); // subtype
    write_pad(pb, 12);
    ff_put_guid(pb,&ff_format_cpfilters_processed); // format type
    avio_wl32(pb, 0); // size

    hdr_pos_start = avio_tell(pb);
    if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
        put_videoinfoheader2(pb, st);
    } else {
        if (ff_put_wav_header(pb, st->codec, 0) < 0)
            format_type = &ff_format_none;
    }
    hdr_size = avio_tell(pb) - hdr_pos_start;

    // seek back write hdr_size
    avio_seek(pb, -(hdr_size + 4), SEEK_CUR);
    avio_wl32(pb, hdr_size + 32);
    avio_seek(pb, hdr_size, SEEK_CUR);
    if (g) {
        ff_put_guid(pb, g);           // actual_subtype
    } else {
        int tag = ff_codec_get_tag(tags, st->codec->codec_id);
        if (!tag) {
            av_log(s, AV_LOG_ERROR, "unsupported codec_id (0x%x)\n", st->codec->codec_id);
            return -1;
        }
        avio_wl32(pb, tag);
        avio_write(pb, (const uint8_t[]){FF_MEDIASUBTYPE_BASE_GUID}, 12);
    }
Example #17
0
static void finish_chunk_noindex(AVFormatContext *s)
{
    WtvContext *wctx = s->priv_data;
    AVIOContext *pb = s->pb;

    // update the chunk_len field and pad.
    int64_t chunk_len = avio_tell(pb) - (wctx->last_chunk_pos + wctx->timeline_start_pos);
    avio_seek(pb, -(chunk_len - 16), SEEK_CUR);
    avio_wl32(pb, chunk_len);
    avio_seek(pb, chunk_len - (16 + 4), SEEK_CUR);

    write_pad(pb, WTV_PAD8(chunk_len) - chunk_len);
    wctx->serial++;
}
Example #18
0
static void write_sync(AVFormatContext *s)
{
    AVIOContext *pb = s->pb;
    WtvContext *wctx = s->priv_data;
    int64_t last_chunk_pos = wctx->last_chunk_pos;
    wctx->sync_pos = avio_tell(pb) - wctx->timeline_start_pos;

    write_chunk_header(s, &sync_guid, 0x18, 0);
    write_pad(pb, 24);

    finish_chunk(s);

    wctx->last_chunk_pos = last_chunk_pos;
}
Example #19
0
void SkPipeCanvas::onDrawPosText(const void* text, size_t byteLength, const SkPoint pos[],
                                 const SkPaint& paint) {
    SkASSERT(byteLength);
    
    bool compact = fits_in(byteLength, 24);
    
    SkPipeWriter writer(this);
    writer.write32(pack_verb(SkPipeVerb::kDrawPosText, compact ? (unsigned)byteLength : 0));
    if (!compact) {
        writer.write32(SkToU32(byteLength));
    }
    write_pad(&writer, text, byteLength);
    writer.writePointArray(pos, paint.countText(text, byteLength));
    write_paint(writer, paint, kText_PaintUsage);
}
Example #20
0
void SkPipeCanvas::onDrawText(const void* text, size_t byteLength, SkScalar x, SkScalar y,
                              const SkPaint& paint) {
    SkASSERT(byteLength);

    bool compact = fits_in(byteLength, 24);

    SkPipeWriter writer(this);
    writer.write32(pack_verb(SkPipeVerb::kDrawText, compact ? (unsigned)byteLength : 0));
    if (!compact) {
        writer.write32(SkToU32(byteLength));
    }
    write_pad(&writer, text, byteLength);
    writer.writeScalar(x);
    writer.writeScalar(y);
    write_paint(writer, paint, kText_PaintUsage);
}
Example #21
0
static int write_packet(AVFormatContext *s, AVPacket *pkt)
{
    AVIOContext *pb = s->pb;
    WtvContext  *wctx = s->priv_data;

    // write timestamp chunk
    write_timestamp(s, pkt);

    write_chunk_header(s, &ff_data_guid, pkt->size, INDEX_BASE + pkt->stream_index);
    avio_write(pb, pkt->data, pkt->size);
    write_pad(pb, WTV_PAD8(pkt->size) - pkt->size);

    wctx->serial++;
    avio_flush(pb);
    return 0;
}
Example #22
0
static void write_timestamp(AVFormatContext *s, AVPacket *pkt)
{
    AVIOContext *pb = s->pb;
    WtvContext  *wctx = s->priv_data;
    AVCodecContext *enc = s->streams[pkt->stream_index]->codec;

    write_chunk_header(s, &ff_timestamp_guid, 56, 0x40000000 | (INDEX_BASE + pkt->stream_index));
    write_pad(pb, 8);
    avio_wl64(pb, pkt->pts == AV_NOPTS_VALUE ? -1 : pkt->pts);
    avio_wl64(pb, pkt->pts == AV_NOPTS_VALUE ? -1 : pkt->pts);
    avio_wl64(pb, pkt->pts == AV_NOPTS_VALUE ? -1 : pkt->pts);
    avio_wl64(pb, 0);
    avio_wl64(pb, enc->codec_type == AVMEDIA_TYPE_VIDEO && (pkt->flags & AV_PKT_FLAG_KEY) ? 1 : 0);
    avio_wl64(pb, 0);

    wctx->last_timestamp_pos = wctx->last_chunk_pos;
}
Example #23
0
void SkPipeCanvas::onDrawRegion(const SkRegion& region, const SkPaint& paint) {
    size_t size = region.writeToMemory(nullptr);
    unsigned extra = 0;
    if (fits_in(size, 24)) {
        extra = SkToUInt(size);
    }

    SkPipeWriter writer(this);
    writer.write32(pack_verb(SkPipeVerb::kDrawRegion, extra));
    if (0 == extra) {
        writer.write32(size);
    }
    SkAutoSMalloc<2048> storage(size);
    region.writeToMemory(storage.get());
    write_pad(&writer, storage.get(), size);
    write_paint(writer, paint, kGeometry_PaintUsage);
}
Example #24
0
void SkPipeCanvas::onDrawImageLattice(const SkImage* image, const Lattice& lattice,
                                      const SkRect& dst, const SkPaint* paint) {
    unsigned extra = 0;
    if (paint) {
        extra |= kHasPaint_DrawImageLatticeMask;
    }
    if (lattice.fFlags) {
        extra |= kHasFlags_DrawImageLatticeMask;
    }
    if (lattice.fXCount >= kCount_DrawImageLatticeMask) {
        extra |= kCount_DrawImageLatticeMask << kXCount_DrawImageLatticeShift;
    } else {
        extra |= lattice.fXCount << kXCount_DrawImageLatticeShift;
    }
    if (lattice.fYCount >= kCount_DrawImageLatticeMask) {
        extra |= kCount_DrawImageLatticeMask << kYCount_DrawImageLatticeShift;
    } else {
        extra |= lattice.fYCount << kYCount_DrawImageLatticeShift;
    }

    SkPipeWriter writer(this);
    writer.write32(pack_verb(SkPipeVerb::kDrawImageLattice, extra));
    writer.writeImage(image);
    if (lattice.fXCount >= kCount_DrawImageLatticeMask) {
        writer.write32(lattice.fXCount);
    }
    if (lattice.fYCount >= kCount_DrawImageLatticeMask) {
        writer.write32(lattice.fYCount);
    }
    // Often these divs will be small (8 or 16 bits). Consider sniffing that and writing a flag
    // so we can store them smaller.
    writer.write(lattice.fXDivs, lattice.fXCount * sizeof(int32_t));
    writer.write(lattice.fYDivs, lattice.fYCount * sizeof(int32_t));
    if (lattice.fFlags) {
        int32_t count = (lattice.fXCount + 1) * (lattice.fYCount + 1);
        SkASSERT(count > 0);
        write_pad(&writer, lattice.fFlags, count);
    }
    SkASSERT(lattice.fBounds);
    writer.write(&lattice.fBounds, sizeof(*lattice.fBounds));
    writer.write(&dst, sizeof(dst));
    if (paint) {
        write_paint(writer, *paint, kImage_PaintUsage);
    }
}
Example #25
0
static void write_timestamp(AVFormatContext *s, AVPacket *pkt)
{
    AVIOContext *pb = s->pb;
    WtvContext  *wctx = s->priv_data;
    AVCodecContext *enc = s->streams[pkt->stream_index]->codec;
    int flag = 0;
    int64_t frame_number = 0;

    if (enc->codec_type == AVMEDIA_TYPE_VIDEO) {
        wctx->frame_nb++;
        frame_number = wctx->frame_nb;
        flag = pkt->flags & AV_PKT_FLAG_KEY ? 1 : 0;
    }
    write_chunk_header(s, &ff_timestamp_guid, 56, 0x40000000 | (INDEX_BASE + pkt->stream_index));
    write_pad(pb, 8);
    avio_wl64(pb, pkt->pts == AV_NOPTS_VALUE ? -1 : pkt->pts);
    avio_wl64(pb, pkt->pts == AV_NOPTS_VALUE ? -1 : pkt->pts);

    avio_wl64(pb, frame_number);
    avio_wl64(pb, 0);
    avio_wl64(pb, flag);
    avio_wl64(pb, 0);
}
Example #26
0
void SkPipeCanvas::onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& path,
                                    const SkMatrix* matrix, const SkPaint& paint) {
    SkASSERT(byteLength > 0);

    unsigned extra = 0;
    if (byteLength <= kTextLength_DrawTextOnPathMask) {
        extra |= byteLength;
    } // else we will write the length after the packedverb
    SkMatrix::TypeMask tm = matrix ? matrix->getType() : SkMatrix::kIdentity_Mask;
    extra |= (unsigned)tm << kMatrixType_DrawTextOnPathShift;

    SkPipeWriter writer(this);
    writer.write32(pack_verb(SkPipeVerb::kDrawTextOnPath, extra));
    if (byteLength > kTextLength_DrawTextOnPathMask) {
        writer.write32(byteLength);
    }
    write_pad(&writer, text, byteLength);
    writer.writePath(path);
    if (matrix) {
        write_sparse_matrix(&writer, *matrix);
    }
    write_paint(writer, paint, kText_PaintUsage);
}
Example #27
0
/*-------------------------------------------------------------------------
 * Function:    main
 *
 * Purpose:     HDF5 user block jammer
 *
 * Return:      Success:    0
 *              Failure:    1
 *
 * Programmer:
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
int
main (int argc, const char *argv[])
{
  int ufid;
  int h5fid;
  int ofid;
  void *edata;
  H5E_auto_t func; 
  hid_t ifile;
  hid_t plist;
  herr_t status;
  htri_t testval;
  hsize_t usize;
  hsize_t h5fsize;
  hsize_t startub;
  hsize_t where;
  hsize_t newubsize;
  off_t fsize;
  struct stat sbuf;
  struct stat sbuf2;
  int res;

  /* Disable error reporting */
  H5Eget_auto (&func, &edata);
  H5Eset_auto (NULL, NULL);

  parse_command_line (argc, argv);

  if (ub_file == NULL)
    {
      /* no user block */
      error_msg (progname, "no user block file name\n");
      usage (progname);
      exit (EXIT_FAILURE);
    }

  if (input_file == NULL)
    {
      /* no user block */
      error_msg (progname, "no HDF5 file\n");
      usage (progname);
      exit (EXIT_FAILURE);
    }

  testval = H5Fis_hdf5 (input_file);

  if (testval <= 0)
    {
      error_msg (progname, "Input HDF5 file is not HDF \"%s\"\n", input_file);
      exit (EXIT_FAILURE);
    }

  ifile = H5Fopen (input_file, H5F_ACC_RDONLY, H5P_DEFAULT);

  if (ifile < 0)
    {
      error_msg (progname, "Can't open input HDF5 file \"%s\"\n", input_file);
      exit (EXIT_FAILURE);
    }

  plist = H5Fget_create_plist (ifile);
  if (plist < 0)
    {
      error_msg (progname, "Can't get file creation plist for file \"%s\"\n",
		 input_file);
      exit (EXIT_FAILURE);
    }

  status = H5Pget_userblock (plist, &usize);
  if (status < 0)
    {
      error_msg (progname, "Can't get user block for file \"%s\"\n",
		 input_file);
      exit (EXIT_FAILURE);
    }

  H5Pclose (plist);
  H5Fclose (ifile);

  ufid = HDopen (ub_file, O_RDONLY, 0);

  if (ufid < 0)
    {
      error_msg (progname, "unable to open user block file \"%s\"\n",
		 ub_file);
      exit (EXIT_FAILURE);
    }

  res = stat (ub_file, &sbuf);

  if (res < 0)
    {
      error_msg (progname, "Can't stat file \"%s\"\n", ub_file);
      exit (EXIT_FAILURE);
    }

  fsize = sbuf.st_size;

  h5fid = HDopen (input_file, O_RDONLY, 0);

  if (h5fid < 0)
    {
      error_msg (progname, "unable to open HDF5 file for read \"%s\"\n",
		 input_file);
      exit (EXIT_FAILURE);
    }

  res = stat (input_file, &sbuf2);

  if (res < 0)
    {
      error_msg (progname, "Can't stat file \"%s\"\n", input_file);
      exit (EXIT_FAILURE);
    }

  h5fsize = sbuf2.st_size;

  if (output_file == NULL)
    {
      ofid = HDopen (input_file, O_WRONLY, 0);

      if (ofid < 0)
	{
	  error_msg (progname, "unable to open output file \"%s\"\n",
		     output_file);
	  exit (EXIT_FAILURE);
	}
    }
  else
    {
      ofid = HDopen (output_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);

      if (ofid < 0)
	{
	  error_msg (progname, "unable to create output file \"%s\"\n",
		     output_file);
	  exit (EXIT_FAILURE);
	}
    }

  newubsize = compute_user_block_size ((hsize_t) fsize);

  startub = usize;

  if (usize > 0)
    {
      if (do_clobber == TRUE)
	{
	  /* where is max of the current size or the new UB */
	  if (usize > newubsize)
	    {
	      newubsize = usize;
	    }
	  startub = 0;		/*blast the old */
	}
      else
	{
	  /* add new ub to current ublock, pad to new offset */
	  newubsize += usize;
	  newubsize = compute_user_block_size ((hsize_t) newubsize);
	}
    }

  /* copy the HDF5 from starting at usize to starting at newubsize:
   *  makes room at 'from' for new ub */
  /* if no current ub, usize is 0 */
  copy_some_to_file (h5fid, ofid, usize, newubsize,
		     (ssize_t) (h5fsize - usize));

  /* copy the old ub to the beginning of the new file */
  if (!do_clobber)
    {
      where =
	copy_some_to_file (h5fid, ofid, (hsize_t) 0, (hsize_t) 0,
			   (ssize_t) usize);
    }

  /* copy the new ub to the end of the ub */
  where = copy_some_to_file (ufid, ofid, (hsize_t) 0, startub, (ssize_t) - 1);

  /* pad the ub */
  where = write_pad (ofid, where);


  HDclose (ufid);
  HDclose (h5fid);
  HDclose (ofid);

  return d_status;
}
Example #28
0
File: sip.c Project: bert/pcb-fpw
/*!
 * \brief Write a SIP pin through hole footprint.
 *
 * The pin/pad numbering scheme of the SIP package is: \n
 * 1 \n
 * 2 \n
 * 3 \n
 * 4 \n
 *
 * \return \c EXIT_FAILURE when errors were encountered,
 * \c EXIT_SUCCESS when OK.
 */
int
sip_write_footprint ()
{
        gdouble xmax;
        gdouble xmin;
        gdouble ymax;
        gdouble ymin;
        gdouble x_text;
        gdouble y_text;
        gint pin_number;
        gchar *pin_pad_name = g_strdup ("");
        gchar *pin_pad_flags = g_strdup ("");
        gint i;

        number_of_columns = 1;
        /* Attempt to open a file with write permission. */
        fp = fopen (footprint_filename, "w");
        if (!fp)
        {
                g_log ("", G_LOG_LEVEL_WARNING,
                        _("could not open file for %s footprint: %s."),
                        footprint_type, footprint_filename);
                fclose (fp);
                return (EXIT_FAILURE);
        }
        /* Print a license if requested. */
        if (license_in_footprint)
        {
                write_license ();
        }
        /* Determine (extreme) courtyard dimensions based on pin/pad
         * properties */
        xmin = multiplier *
        (
                (((pad_diameter > pad_length) ? pad_diameter : pad_length) / -2.0) - pad_solder_mask_clearance
        );
        xmax = multiplier *
        (
                (((pad_diameter > pad_length) ? pad_diameter : pad_length) / 2.0) + pad_solder_mask_clearance
        );
        ymin = multiplier *
        (
                (((-number_of_rows + 1) / 2.0) * pitch_y) -
                (((pad_diameter > pad_width) ? pad_diameter : pad_width) / 2.0) -
                pad_solder_mask_clearance
        );
        ymax = multiplier *
        (
                (((number_of_rows - 1 ) / 2.0) * pitch_y) +
                (((pad_diameter > pad_width) ? pad_diameter : pad_width) / 2.0) +
                pad_solder_mask_clearance
        );
        /* Determine (extreme) courtyard dimensions based on package
         * properties */
        if ((multiplier * ((-package_body_length / 2.0) - courtyard_clearance_with_package)) < xmin)
        {
                xmin = (multiplier * ((-package_body_length / 2.0) - courtyard_clearance_with_package));
        }
        if ((multiplier * ((package_body_length / 2.0) + courtyard_clearance_with_package)) > xmax)
        {
                xmax = (multiplier * ((package_body_length / 2.0) + courtyard_clearance_with_package));
        }
        if ((multiplier * ((-package_body_width / 2.0) - courtyard_clearance_with_package)) < ymin)
        {
                ymin = (multiplier * ((-package_body_width / 2.0) - courtyard_clearance_with_package));
        }
        if ((multiplier * ((package_body_width / 2.0) + courtyard_clearance_with_package)) > ymax)
        {
                ymax = (multiplier * ((package_body_width / 2.0) + courtyard_clearance_with_package));
        }
        /* If the user input is using even more real-estate then use it */
        if (multiplier * (-courtyard_length / 2.0) < xmin)
        {
                xmin = multiplier * (-courtyard_length / 2.0);
        }
        if (multiplier * (courtyard_length / 2.0) > xmax)
        {
                xmax = multiplier * (courtyard_length / 2.0);
        }
        if (multiplier * (-courtyard_width / 2.0) < ymin)
        {
                ymin = multiplier * (-courtyard_width / 2.0);
        }
        if (multiplier * (courtyard_width / 2.0) > ymax)
        {
                ymax = multiplier * (courtyard_width / 2.0);
        }
        /* Write element header
         * Guess for a place where to put the refdes text */
        x_text = 0.0 ; /* already in mil/100 */
        y_text = (ymin - 10000.0); /* already in mil/100 */
        write_element_header (x_text, y_text);
        /* Write pin and/or pad entities */
        for (i = 0; (i < number_of_rows); i++)
        {
                pin_number = 1 + i;
                write_pin
                (
                        pin_number, /* pin number */
                        pin_pad_name, /* pin name */
                        0, /* x0 coordinate */
                        multiplier * ((((-number_of_rows - 1) / 2.0) +1 + i) * pitch_y), /* y0-coordinate */
                        multiplier * pad_diameter, /* width of the annulus ring (pad) */
                        multiplier * 2 * pad_clearance, /* clearance */
                        multiplier * (pad_diameter + (2 * pad_solder_mask_clearance)), /* solder mask clearance */
                        multiplier * pin_drill_diameter, /* pin drill diameter */
                        (pin1_square && (pin_number == 1)) ? "square" : pin_pad_flags /* flags */
                );
                if (!strcmp (pad_shape, "rounded pad, elongated"))
                {
                        if (!strcmp (pin_pad_flags, ""))
                                pin_pad_flags = g_strconcat (pin_pad_flags, "onsolder", NULL);
                        else
                                pin_pad_flags = g_strconcat (pin_pad_flags, ",onsolder", NULL);
                        write_pad
                        (
                                pin_number, /* pad number = pin_number */
                                pin_pad_name, /* pad name */
                                multiplier * (-pad_width + pad_length) / 2.0, /* x0 coordinate */
                                multiplier * ((((-number_of_rows - 1) / 2.0) + 1 + i) * pitch_y), /* y0-coordinate */
                                multiplier * (pad_width - pad_length) / 2.0, /* x1 coordinate */
                                multiplier * ((((-number_of_rows - 1) / 2.0) + 1 + i) * pitch_y), /* y1-coordinate */
                                multiplier * pad_width, /* width of the pad */
                                multiplier * 2 * pad_clearance, /* clearance */
                                multiplier * (pad_width + (2 * pad_solder_mask_clearance)), /* solder mask clearance */
                                pin_pad_flags /* flags */
                        );
                }
        }
        /* Write a package body on the silkscreen */
        if (silkscreen_package_outline)
        {
                fprintf (fp, "# Write a package body on the silkscreen\n");
                write_rectangle
                (
                        multiplier * ((-package_body_length) / 2.0), /* xmin-coordinate */
                        multiplier * ((-package_body_width) / 2.0), /* ymin-coordinate */
                        multiplier * ((package_body_length) / 2.0), /* xmax-coordinate */
                        multiplier * ((package_body_width) / 2.0), /* ymax-coordiante */
                        multiplier * silkscreen_line_width
                );
        }
        /* Write a pin #1 marker on the silkscreen */
        if (silkscreen_indicate_1)
        {
                fprintf (fp, "# Write a pin 1 marker on the silkscreen\n");
                /* Write a marker around pin #1 inside the package outline */
                write_element_line
                (
                        multiplier * (-package_body_length / 2.0), /* x0-coordinate */
                        multiplier * ((((-number_of_rows - 1) / 2.0) + 1.5) * pitch_y), /* y0-coordinate */
                        multiplier * (package_body_length / 2.0), /* x1-coordinate */
                        multiplier * ((((-number_of_rows - 1) / 2.0) + 1.5) * pitch_y), /* y1-coordinate */
                        multiplier * (silkscreen_line_width)
                );
                /* Write a triangle shaped marker between package outline and maximum used real estate */
                if (xmax > ((multiplier * package_body_length) / 2))
                {
                        write_element_line
                        (
                                multiplier * (-package_body_length / 2.0), /* x0-coordinate */
                                multiplier * (((-number_of_rows + 1) / 2.0) * pitch_y), /* y0-coordinate */
                                (multiplier * (-package_body_length / 2.0)) - 2500 , /* x1-coordinate */
                                (multiplier * (((-number_of_rows + 1) / 2.0) * pitch_y)) - 1250, /* y1-coordinate */
                                multiplier * (silkscreen_line_width)
                        );
                        write_element_line
                        (
                                multiplier * (-package_body_length / 2.0), /* x0-coordinate */
                                multiplier * (((-number_of_rows + 1) / 2.0) * pitch_y), /* y0-coordinate */
                                (multiplier * (-package_body_length / 2.0)) - 2500 , /* x1-coordinate */
                                (multiplier * (((-number_of_rows + 1) / 2.0) * pitch_y)) + 1250, /* y1-coordinate */
                                multiplier * (silkscreen_line_width)
                        );
                        write_element_line
                        (
                                (multiplier * (-package_body_length / 2.0)) - 2500 , /* x0-coordinate */
                                (multiplier * (((-number_of_rows + 1) / 2.0) * pitch_y)) - 1250, /* y0-coordinate */
                                (multiplier * (-package_body_length / 2.0)) - 2500 , /* x1-coordinate */
                                (multiplier * (((-number_of_rows + 1) / 2.0) * pitch_y)) + 1250, /* y1-coordinate */
                                multiplier * (silkscreen_line_width)
                        );
                }
        }
        /* Write a courtyard on the silkscreen */
        if (courtyard)
        {
                fprintf (fp, "# Write a courtyard on the silkscreen\n");
                write_rectangle
                (
                        xmin, /* already in mil/100 */
                        ymin, /* already in mil/100 */
                        xmax, /* already in mil/100 */
                        ymax, /* already in mil/100 */
                        multiplier * courtyard_line_width
                );
        }
        /* Write attributes to the footprint file. */
        if (attributes_in_footprint)
        {
                write_attributes ();
        }
        /* Finishing touch. */
        fprintf (fp, "\n");
        fprintf (fp, ")\n");
        fclose (fp);
        /* We are ready creating a footprint. */
        if (verbose)
        {
                g_log ("", G_LOG_LEVEL_INFO,
                        _("wrote a footprint for a %s package: %s."),
                        footprint_type,
                        footprint_filename);
        }
        return (EXIT_SUCCESS);
}
Example #29
0
int __v_printf(struct arg_printf* fn, const unsigned char *format, va_list arg_ptr)
{
  int len=0;
#ifdef WANT_ERROR_PRINTF
  int _my_errno = errno;
#endif

  while (*format) {
    unsigned int sz = skip_to(format);
    if (sz) {
      A_WRITE(fn,format,sz); len+=sz;
      format+=sz;
    }
    if (*format=='%') {
      char buf[2000];
      union { char*s; } u_str;
#define s u_str.s

      int retval;
      unsigned char ch, padwith=' ';

      char flag_in_sign=0;
      char flag_upcase=0;
      char flag_hash=0;
      char flag_left=0;
      char flag_space=0;
      char flag_sign=0;
      char flag_dot=0;
      signed char flag_long=0;

      unsigned int base;
      unsigned int width=0, preci=0;

      long number=0;
#ifdef WANT_LONGLONG_PRINTF
      long long llnumber=0;
#endif

      ++format;
inn_printf:
      switch(ch=*format++) {
      case 0:
	return -1;
	break;

      /* FLAGS */
      case '#':
	flag_hash=-1;
      case 'z':
	goto inn_printf;

      case 'h':
	--flag_long;
	goto inn_printf;
      case 'q':		/* BSD ... */
      case 'L':
	++flag_long; /* fall through */
      case 'l':
	++flag_long;
	goto inn_printf;

      case '-':
	flag_left=1;
	goto inn_printf;

      case ' ':
	flag_space=1;
	goto inn_printf;

      case '+':
	flag_sign=1;
	goto inn_printf;

      case '0':
      case '1':
      case '2':
      case '3':
      case '4':
      case '5':
      case '6':
      case '7':
      case '8':
      case '9':
	if(flag_dot) return -1;
	width=strtoul(format-1,(char**)&s,10);
	if (ch=='0' && !flag_left) padwith='0';
	format=s;
	goto inn_printf;

      case '*':
	width=va_arg(arg_ptr,int);
	goto inn_printf;

      case '.':
	flag_dot=1;
	if (*format=='*') {
	  int tmp=va_arg(arg_ptr,int);
	  preci=tmp<0?0:tmp;
	  ++format;
	} else {
	  long int tmp=strtol(format,(char**)&s,10);
	  preci=tmp<0?0:tmp;
	  format=s;
	}
	goto inn_printf;

      /* print a char or % */
      case 'c':
	ch=(char)va_arg(arg_ptr,int);
      case '%':
	A_WRITE(fn,&ch,1); ++len;
	break;

#ifdef WANT_ERROR_PRINTF
      /* print an error message */
      case 'm':
	s=strerror(_my_errno);
	sz=strlen(s);
	A_WRITE(fn,s,sz); len+=sz;
	break;
#endif
      /* print a string */
      case 's':
	s=va_arg(arg_ptr,char *);
#ifdef WANT_NULL_PRINTF
	if (!s) s="(null)";
#endif
	sz = strlen(s);
	if (flag_dot && sz>preci) sz=preci;
	preci=0;
	flag_dot^=flag_dot;
	padwith=' ';

print_out:
      {
	char *sign=s;
	int todo=0;
	int vs;
	
	if (! (width||preci) ) {
	  A_WRITE(fn,s,sz); len+=sz;
	  break;
	}
	
	if (flag_in_sign) todo=1;
	if (flag_hash>0)  todo=flag_hash;
	if (todo) {
	  s+=todo;
	  sz-=todo;
	  width-=todo;
	}
	
	if (!flag_left) {
	  if (flag_dot) {
	    vs=preci>sz?preci:sz;
	    len+=write_pad(fn,(signed int)width-(signed int)vs,' ');
	    if (todo) {
	      A_WRITE(fn,sign,todo);
	      len+=todo;
	    }
	    len+=write_pad(fn,(signed int)preci-(signed int)sz,'0');
	  } else {
	    if (todo && padwith=='0') {
	      A_WRITE(fn,sign,todo);
	      len+=todo; todo=0;
	    }
	    len+=write_pad(fn,(signed int)width-(signed int)sz, padwith);
	    if (todo) {
	      A_WRITE(fn,sign,todo);
	      len+=todo;
	    }
	  }
	  A_WRITE(fn,s,sz); len+=sz;
	} else if (flag_left) {
	  if (todo) {
	    A_WRITE(fn,sign,todo);
	    len+=todo;
	  }
	  len+=write_pad(fn,(signed int)preci-(signed int)sz, '0');
	  A_WRITE(fn,s,sz); len+=sz;
	  vs=preci>sz?preci:sz;
	  len+=write_pad(fn,(signed int)width-(signed int)vs, ' ');
	} else {
	  A_WRITE(fn,s,sz); len+=sz;
	}
	break;
      }

      /* print an integer value */
      case 'b':
	base=2;
	sz=0;
	goto num_printf;
      case 'p':
	flag_hash=2;
	flag_long=1;
	ch='x';
      case 'X':
	flag_upcase=(ch=='X');
      case 'x':
	base=16;
	sz=0;
	if (flag_hash) {
	  buf[1]='0';
	  buf[2]=ch;
	  flag_hash=2;
	  sz=2;
	}
	if (preci>width) width=preci;
	goto num_printf;
      case 'd':
      case 'i':
	flag_in_sign=1;
      case 'u':
	base=10;
	sz=0;
	goto num_printf;
      case 'o':
	base=8;
	sz=0;
	if (flag_hash) {
	  buf[1]='0';
	  flag_hash=1;
	  ++sz;
	}

num_printf:
	s=buf+1;

	if (flag_long>0) {
#ifdef WANT_LONGLONG_PRINTF
	  if (flag_long>1)
	    llnumber=va_arg(arg_ptr,long long);
	  else
#endif
	    number=va_arg(arg_ptr,long);
	}
Example #30
0
File: h5jam.c Project: ElaraFX/hdf5
/*-------------------------------------------------------------------------
 * Function:    main
 *
 * Purpose:     HDF5 user block jammer
 *
 * Return:      Success:    0
 *              Failure:    1
 *
 * Programmer:
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
int
main (int argc, const char *argv[])
{
    int         ufid = -1;
    int         h5fid = -1;
    int         ofid = -1;
    void       *edata;
    H5E_auto2_t func;
    hid_t       ifile = -1;
    hid_t       plist = -1;
    herr_t      status;
    htri_t      testval;
    hsize_t     usize;
    hsize_t     h5fsize;
    hsize_t     startub;
    hsize_t     where;
    hsize_t     newubsize;
    off_t       fsize;
    h5_stat_t   sbuf;
    h5_stat_t   sbuf2;
    int         res;

    h5tools_setprogname(PROGRAMNAME);
    h5tools_setstatus(EXIT_SUCCESS);

    /* Disable error reporting */
    H5Eget_auto2(H5E_DEFAULT, &func, &edata);
    H5Eset_auto2(H5E_DEFAULT, NULL, NULL);

    /* Initialize h5tools lib */
    h5tools_init();

    parse_command_line (argc, argv);

    if (ub_file == NULL) {
        /* no user block */
        error_msg("missing arguemnt for -u <user_file>.\n");
        help_ref_msg(stderr);
        leave (EXIT_FAILURE);
    }

    testval = H5Fis_hdf5 (ub_file);

    if (testval > 0) {
        error_msg("-u <user_file> cannot be HDF5 file, but it appears to be an HDF5 file.\n");
        help_ref_msg(stderr);
        leave (EXIT_FAILURE);
    }

    if (input_file == NULL) {
        error_msg("missing arguemnt for -i <HDF5 file>.\n");
        help_ref_msg(stderr);
        leave (EXIT_FAILURE);
    }

    testval = H5Fis_hdf5 (input_file);

    if (testval <= 0) {
        error_msg("Input HDF5 file \"%s\" is not HDF5 format.\n", input_file);
        help_ref_msg(stderr);
        leave (EXIT_FAILURE);
    }

    ifile = H5Fopen (input_file, H5F_ACC_RDONLY, H5P_DEFAULT);

    if (ifile < 0) {
        error_msg("Can't open input HDF5 file \"%s\"\n", input_file);
        leave (EXIT_FAILURE);
    }

    plist = H5Fget_create_plist (ifile);
    if (plist < 0) {
        error_msg("Can't get file creation plist for file \"%s\"\n", input_file);
        H5Fclose(ifile);
        leave (EXIT_FAILURE);
    }

    status = H5Pget_userblock (plist, &usize);
    if (status < 0) {
        error_msg("Can't get user block for file \"%s\"\n", input_file);
        H5Pclose(plist);
        H5Fclose(ifile);
        leave (EXIT_FAILURE);
    }

    H5Pclose(plist);
    H5Fclose(ifile);

    ufid = HDopen(ub_file, O_RDONLY, 0);
    if(ufid < 0) {
        error_msg("unable to open user block file \"%s\"\n", ub_file);
        leave (EXIT_FAILURE);
    }

    res = HDfstat(ufid, &sbuf);
    if(res < 0) {
        error_msg("Can't stat file \"%s\"\n", ub_file);
        HDclose (ufid);
        leave (EXIT_FAILURE);
    }

    fsize = (off_t)sbuf.st_size;

    h5fid = HDopen(input_file, O_RDONLY, 0);
    if(h5fid < 0) {
        error_msg("unable to open HDF5 file for read \"%s\"\n", input_file);
        HDclose (ufid);
        leave (EXIT_FAILURE);
    }

    res = HDfstat(h5fid, &sbuf2);
    if(res < 0) {
        error_msg("Can't stat file \"%s\"\n", input_file);
        HDclose (h5fid);
        HDclose (ufid);
        leave (EXIT_FAILURE);
    }

    h5fsize = (hsize_t)sbuf2.st_size;

    if (output_file == NULL) {
        ofid = HDopen (input_file, O_WRONLY, 0);

        if (ofid < 0) {
            error_msg("unable to open output file \"%s\"\n", output_file);
            HDclose (h5fid);
            HDclose (ufid);
            leave (EXIT_FAILURE);
        }
    }
    else {
        ofid = HDopen (output_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);

        if (ofid < 0) {
            error_msg("unable to create output file \"%s\"\n", output_file);
            HDclose (h5fid);
            HDclose (ufid);
            leave (EXIT_FAILURE);
        }
    }

    newubsize = compute_user_block_size ((hsize_t) fsize);

    startub = usize;

    if (usize > 0) {
        if (do_clobber == TRUE) {
            /* where is max of the current size or the new UB */
            if (usize > newubsize) {
                newubsize = usize;
            }
            startub = 0;    /*blast the old */
        }
        else {
            /* add new ub to current ublock, pad to new offset */
            newubsize += usize;
            newubsize = compute_user_block_size ((hsize_t) newubsize);
        }
    }

    /* copy the HDF5 from starting at usize to starting at newubsize:
     *  makes room at 'from' for new ub */
    /* if no current ub, usize is 0 */
    copy_some_to_file (h5fid, ofid, usize, newubsize, (ssize_t) (h5fsize - usize));

    /* copy the old ub to the beginning of the new file */
    if (!do_clobber) {
        where = copy_some_to_file (h5fid, ofid, (hsize_t) 0, (hsize_t) 0, (ssize_t) usize);
    }

    /* copy the new ub to the end of the ub */
    where = copy_some_to_file (ufid, ofid, (hsize_t) 0, startub, (ssize_t) - 1);

    /* pad the ub */
    where = write_pad (ofid, where);

    if(ub_file)
        HDfree (ub_file);
    if(input_file)
        HDfree (input_file);
    if(output_file)
        HDfree (output_file);
    
    if(ufid >= 0)
        HDclose (ufid);
    if(h5fid >= 0)
        HDclose (h5fid);
    if(ofid >= 0)
        HDclose (ofid);

    return h5tools_getstatus();
}