int mxf_mem_file_flush_to_file(MXFMemoryFile *mxfMemFile, MXFFile *mxfFile) { MXFFileSysData *sysData = mxfMemFile->mxfFile->sysData; size_t i; for (i = 0; i < sysData->numChunks; i++) { const unsigned char *data = sysData->chunks[i].data; int64_t remainder = sysData->chunks[i].size; uint32_t writeSize; while (remainder > 0) { if (remainder > UINT32_MAX) writeSize = UINT32_MAX; else writeSize = (uint32_t)remainder; if (!mxf_file_write(mxfFile, data, writeSize)) return 0; data += writeSize; remainder -= writeSize; } } return 1; }
int mxf_write_item(MXFFile* mxfFile, MXFMetadataItem* item) { CHK_ORET(mxf_write_local_tag(mxfFile, item->tag)); CHK_ORET(mxf_write_uint16(mxfFile, item->length)); CHK_ORET(mxf_file_write(mxfFile, item->value, item->length) == item->length); item->isPersistent = 1; return 1; }
int do_write(MXFFile *mxfFile) { memset(data, 0xaa, 256); CHK_ORET(mxf_file_write(mxfFile, NULL, 0) == 0); CHK_ORET(mxf_file_write(mxfFile, data, 0) == 0); CHK_ORET(mxf_file_write(mxfFile, data, 100) == 100); CHK_ORET(mxf_file_putc(mxfFile, 0xff)); CHK_ORET(mxf_file_putc(mxfFile, 0xff)); CHK_ORET(mxf_write_uint8(mxfFile, 0x0f)); CHK_ORET(mxf_write_uint16(mxfFile, 0x0f00)); CHK_ORET(mxf_write_uint32(mxfFile, 0x0f000000)); CHK_ORET(mxf_write_uint64(mxfFile, 0x0f00000000000000LL)); CHK_ORET(mxf_write_int8(mxfFile, -0x0f)); CHK_ORET(mxf_write_int16(mxfFile, -0x0f00)); CHK_ORET(mxf_write_int32(mxfFile, -0x0f000000)); CHK_ORET(mxf_write_int64(mxfFile, -0x0f00000000000000LL)); CHK_ORET(mxf_write_local_tag(mxfFile, 0xffaa)); CHK_ORET(mxf_write_k(mxfFile, &someKey)); CHK_ORET(mxf_write_l(mxfFile, 0x01) == 1); CHK_ORET(mxf_write_l(mxfFile, 0x80) == 2); CHK_ORET(mxf_write_l(mxfFile, 0x8000) == 3); CHK_ORET(mxf_write_l(mxfFile, 0x800000) == 4); CHK_ORET(mxf_write_l(mxfFile, 0x80000000) == 5); CHK_ORET(mxf_write_l(mxfFile, 0x8000000000LL) == 6); CHK_ORET(mxf_write_l(mxfFile, 0x800000000000LL) == 7); CHK_ORET(mxf_write_l(mxfFile, 0x80000000000000LL) == 8); CHK_ORET(mxf_write_l(mxfFile, 0x8000000000000000LL) == 9); CHK_ORET(mxf_write_kl(mxfFile, &someKey, 0xf100)); CHK_ORET(mxf_write_fixed_l(mxfFile, 8, 0x10)); CHK_ORET(mxf_write_fixed_l(mxfFile, 4, 0x10)); CHK_ORET(mxf_write_fixed_kl(mxfFile, &someKey, 8, 0x1000)); CHK_ORET(mxf_write_ul(mxfFile, &someUL)); CHK_ORET(mxf_write_uid(mxfFile, &someUID)); CHK_ORET(mxf_write_uuid(mxfFile, &someUUID)); CHK_ORET(mxf_write_batch_header(mxfFile, 2, 16)); CHK_ORET(mxf_write_array_header(mxfFile, 4, 32)); return 1; }
int mxf_write_essence_element_data(MXFFile *mxfFile, MXFEssenceElement *essenceElement, const uint8_t *data, uint32_t len) { uint64_t numWritten = mxf_file_write(mxfFile, data, len); essenceElement->totalLen += numWritten; essenceElement->currentFilePos += numWritten; if (numWritten != len) { return 0; } return 1; }
int test_create_and_write(const char *filename) { MXFFile *mxfFile = NULL; uint8_t runin[RUNIN_LEN]; MXFFilePartitions partitions; MXFPartition *headerPartition = NULL; MXFPartition *bodyPartition1 = NULL; MXFPartition *bodyPartition2 = NULL; MXFPartition *footerPartition = NULL; if (!mxf_disk_file_open_new(filename, &mxfFile)) { mxf_log_error("Failed to create '%s'" LOG_LOC_FORMAT, filename, LOG_LOC_PARAMS); return 0; } mxf_initialise_file_partitions(&partitions); /* TEST */ /* write 8 bytes of runin */ memset(runin, 0, RUNIN_LEN); CHK_OFAIL(mxf_file_write(mxfFile, runin, RUNIN_LEN) == RUNIN_LEN); /* write the header pp */ CHK_OFAIL(mxf_append_new_partition(&partitions, &headerPartition)); headerPartition->key = MXF_PP_K(ClosedComplete, Header); headerPartition->indexSID = 1; CHK_OFAIL(mxf_append_partition_esscont_label(headerPartition, &MXF_EC_L(SD_Unc_625_50i_422_135_FrameWrapped))); CHK_OFAIL(mxf_append_partition_esscont_label(headerPartition, &MXF_EC_L(BWFFrameWrapped))); CHK_OFAIL(mxf_write_partition(mxfFile, headerPartition)); /* write empty header metadata */ CHK_OFAIL(mxf_mark_header_start(mxfFile, headerPartition)); CHK_OFAIL(mxf_allocate_space(mxfFile, 1024)); CHK_OFAIL(mxf_mark_header_end(mxfFile, headerPartition)); /* write the body pp 1 */ CHK_OFAIL(mxf_append_new_from_partition(&partitions, headerPartition, &bodyPartition1)); bodyPartition1->key = MXF_PP_K(ClosedComplete, Body); bodyPartition1->bodySID = 2; CHK_OFAIL(mxf_write_partition(mxfFile, bodyPartition1)); /* write the body pp 2, with KAG 256 */ CHK_OFAIL(mxf_append_new_from_partition(&partitions, headerPartition, &bodyPartition2)); bodyPartition2->key = MXF_PP_K(ClosedComplete, Body); bodyPartition2->bodySID = 3; bodyPartition2->kagSize = 256; CHK_OFAIL(mxf_write_partition(mxfFile, bodyPartition2)); CHK_OFAIL(mxf_fill_to_kag(mxfFile, bodyPartition2)); /* write the footer pp */ CHK_OFAIL(mxf_append_new_from_partition(&partitions, headerPartition, &footerPartition)); footerPartition->key = MXF_PP_K(ClosedComplete, Footer); footerPartition->indexSID = 1; CHK_OFAIL(mxf_write_partition(mxfFile, footerPartition)); /* write RIP */ CHK_OFAIL(mxf_write_rip(mxfFile, &partitions)); /* update the partitions */ CHK_OFAIL(mxf_update_partitions(mxfFile, &partitions)); mxf_file_close(&mxfFile); mxf_clear_file_partitions(&partitions); return 1; fail: mxf_file_close(&mxfFile); mxf_clear_file_partitions(&partitions); return 0; }
int test_create_and_write(const char *filename) { MXFFile *mxfFile = NULL; MXFFilePartitions partitions; MXFPartition *headerPartition = NULL; MXFPartition *bodyPartition1 = NULL; MXFPartition *bodyPartition2 = NULL; MXFPartition *footerPartition = NULL; MXFEssenceElement *essenceElement = NULL; uint8_t essenceData[1024]; memset(essenceData, 0, 1024); if (!mxf_disk_file_open_new(filename, &mxfFile)) { mxf_log_error("Failed to create '%s'" LOG_LOC_FORMAT, filename, LOG_LOC_PARAMS); return 0; } mxf_initialise_file_partitions(&partitions); /* TEST */ /* write the header pp */ CHK_OFAIL(mxf_append_new_partition(&partitions, &headerPartition)); headerPartition->key = MXF_PP_K(ClosedComplete, Header); headerPartition->bodySID = 1; CHK_OFAIL(mxf_append_partition_esscont_label(headerPartition, &MXF_EC_L(BWFFrameWrapped))); CHK_OFAIL(mxf_append_partition_esscont_label(headerPartition, &MXF_EC_L(BWFClipWrapped))); CHK_OFAIL(mxf_write_partition(mxfFile, headerPartition)); /* write essence element directly */ CHK_OFAIL(mxf_write_fixed_kl(mxfFile, &g_bwfFrameWrappedEEKey, 4, 256)); CHK_OFAIL(mxf_file_write(mxfFile, essenceData, 256)); /* write the body pp 1 */ CHK_OFAIL(mxf_append_new_from_partition(&partitions, headerPartition, &bodyPartition1)); bodyPartition1->key = MXF_PP_K(ClosedComplete, Body); bodyPartition1->bodySID = 2; CHK_OFAIL(mxf_write_partition(mxfFile, bodyPartition1)); /* write using MXFEssenceElement with known length */ CHK_OFAIL(mxf_open_essence_element_write(mxfFile, &g_bwfClipWrappedEEKey, 8, 1024, &essenceElement)); CHK_OFAIL(mxf_write_essence_element_data(mxfFile, essenceElement, essenceData, 1024)); mxf_close_essence_element(&essenceElement); /* write the body pp 2 */ CHK_OFAIL(mxf_append_new_from_partition(&partitions, headerPartition, &bodyPartition2)); bodyPartition2->key = MXF_PP_K(ClosedComplete, Body); bodyPartition2->bodySID = 3; CHK_OFAIL(mxf_write_partition(mxfFile, bodyPartition2)); /* write using MXFEssenceElement with uknown length */ CHK_OFAIL(mxf_open_essence_element_write(mxfFile, &g_bwfClipWrappedEEKey, 8, 0, &essenceElement)); CHK_OFAIL(mxf_write_essence_element_data(mxfFile, essenceElement, essenceData, 256)); CHK_OFAIL(mxf_write_essence_element_data(mxfFile, essenceElement, essenceData, 512)); CHK_OFAIL(mxf_write_essence_element_data(mxfFile, essenceElement, essenceData, 256)); CHK_ORET(mxf_finalize_essence_element_write(mxfFile, essenceElement)); mxf_close_essence_element(&essenceElement); /* write the footer pp */ CHK_OFAIL(mxf_append_new_from_partition(&partitions, headerPartition, &footerPartition)); footerPartition->key = MXF_PP_K(ClosedComplete, Footer); CHK_OFAIL(mxf_write_partition(mxfFile, footerPartition)); /* update the partitions */ CHK_OFAIL(mxf_update_partitions(mxfFile, &partitions)); mxf_file_close(&mxfFile); mxf_clear_file_partitions(&partitions); mxf_close_essence_element(&essenceElement); return 1; fail: mxf_file_close(&mxfFile); mxf_clear_file_partitions(&partitions); mxf_close_essence_element(&essenceElement); return 0; }
int main() { MXFPageFile *mxfPageFile; MXFFile *mxfFile; uint8_t *data; int i; data = malloc(DATA_SIZE); remove_test_files(); CHECK(mxf_page_file_open_new(g_testFile, PAGE_SIZE, &mxfPageFile)); mxfFile = mxf_page_file_get_file(mxfPageFile); memset(data, 0, DATA_SIZE); CHECK(mxf_file_write(mxfFile, data, DATA_SIZE) == DATA_SIZE); mxf_file_close(&mxfFile); CHECK(mxf_page_file_open_modify(g_testFile, PAGE_SIZE, &mxfPageFile)); mxfFile = mxf_page_file_get_file(mxfPageFile); memset(data, 1, DATA_SIZE); CHECK(mxf_file_size(mxfFile) == DATA_SIZE); CHECK(mxf_file_read(mxfFile, data, DATA_SIZE) == DATA_SIZE); CHECK(mxf_file_eof(mxfFile)); CHECK(mxf_file_tell(mxfFile) == DATA_SIZE); CHECK(mxf_file_write(mxfFile, data, DATA_SIZE) == DATA_SIZE); CHECK(mxf_file_eof(mxfFile)); CHECK(mxf_file_tell(mxfFile) == DATA_SIZE * 2); CHECK(mxf_file_seek(mxfFile, 0, SEEK_SET)); CHECK(mxf_file_tell(mxfFile) == 0); CHECK(mxf_file_write(mxfFile, data, DATA_SIZE) == DATA_SIZE); mxf_file_close(&mxfFile); CHECK(mxf_page_file_open_read(g_testFile, &mxfPageFile)); mxfFile = mxf_page_file_get_file(mxfPageFile); memset(data, 1, DATA_SIZE); CHECK(mxf_file_size(mxfFile) == DATA_SIZE * 2); CHECK(mxf_file_read(mxfFile, data, DATA_SIZE) == DATA_SIZE); CHECK(mxf_file_tell(mxfFile) == DATA_SIZE); CHECK(mxf_file_read(mxfFile, data, DATA_SIZE) == DATA_SIZE); CHECK(mxf_file_eof(mxfFile)); CHECK(mxf_file_tell(mxfFile) == DATA_SIZE * 2); CHECK(mxf_file_seek(mxfFile, 0, SEEK_SET)); CHECK(mxf_file_tell(mxfFile) == 0); CHECK(mxf_file_read(mxfFile, data, DATA_SIZE) == DATA_SIZE); CHECK(mxf_file_tell(mxfFile) == DATA_SIZE); CHECK(mxf_file_seek(mxfFile, DATA_SIZE + 1, SEEK_SET)); CHECK(mxf_file_read(mxfFile, data, DATA_SIZE - 1) == DATA_SIZE - 1); CHECK(mxf_file_tell(mxfFile) == DATA_SIZE * 2); CHECK(mxf_file_seek(mxfFile, DATA_SIZE - 1, SEEK_SET)); CHECK(mxf_file_tell(mxfFile) == DATA_SIZE - 1); CHECK(mxf_file_seek(mxfFile, DATA_SIZE * 2, SEEK_SET)); CHECK(mxf_file_tell(mxfFile) == DATA_SIZE * 2); CHECK(mxf_file_seek(mxfFile, 0, SEEK_END)); CHECK(mxf_file_tell(mxfFile) == DATA_SIZE * 2); CHECK(mxf_file_seek(mxfFile, -DATA_SIZE * 2, SEEK_END)); CHECK(mxf_file_tell(mxfFile) == 0); CHECK(mxf_file_seek(mxfFile, DATA_SIZE - 5, SEEK_CUR)); CHECK(mxf_file_tell(mxfFile) == DATA_SIZE - 5); CHECK(mxf_file_read(mxfFile, data, DATA_SIZE) == DATA_SIZE); mxf_file_close(&mxfFile); CHECK(mxf_page_file_open_new("pagetest_out__%d.mxf", PAGE_SIZE, &mxfPageFile)); mxfFile = mxf_page_file_get_file(mxfPageFile); CHECK(mxf_file_write(mxfFile, data, DATA_SIZE) == DATA_SIZE); mxf_file_close(&mxfFile); CHECK(mxf_page_file_remove("pagetest_out__%d.mxf")); /* test forward truncate */ CHECK(mxf_page_file_open_new(g_testFile, PAGE_SIZE, &mxfPageFile)); mxfFile = mxf_page_file_get_file(mxfPageFile); memset(data, 0, DATA_SIZE); for (i = 0; i < 10; i++) { CHECK(mxf_file_write(mxfFile, data, DATA_SIZE) == DATA_SIZE); } mxf_file_close(&mxfFile); CHECK(mxf_page_file_open_modify(g_testFile, PAGE_SIZE, &mxfPageFile)); mxfFile = mxf_page_file_get_file(mxfPageFile); memset(data, 1, DATA_SIZE); CHECK(mxf_page_file_forward_truncate(mxfPageFile)); for (i = 0; i < 5; i++) { CHECK(mxf_file_read(mxfFile, data, DATA_SIZE) == DATA_SIZE); } CHECK(mxf_page_file_forward_truncate(mxfPageFile)); for (i = 0; i < 4; i++) { CHECK(mxf_file_read(mxfFile, data, DATA_SIZE) == DATA_SIZE); } CHECK(mxf_page_file_forward_truncate(mxfPageFile)); CHECK(mxf_file_read(mxfFile, data, DATA_SIZE) == DATA_SIZE); CHECK(mxf_page_file_forward_truncate(mxfPageFile)); mxf_file_close(&mxfFile); CHECK(mxf_page_file_remove(g_testFile)); free(data); return 0; }