changeset 3842:c4e0e02a4832 libavformat

simplify and correctly rewrite metadata in header partition, mark it closed complete
author bcoudurier
date Sun, 31 Aug 2008 03:36:25 +0000
parents cd6c579c5651
children b60ab685d415
files mxfenc.c
diffstat 1 files changed, 26 insertions(+), 44 deletions(-) [+]
line wrap: on
line diff
--- a/mxfenc.c	Sun Aug 31 03:20:41 2008 +0000
+++ b/mxfenc.c	Sun Aug 31 03:36:25 2008 +0000
@@ -65,9 +65,8 @@
 };
 
 typedef struct MXFContext {
-    int64_t header_byte_count;
     int64_t header_byte_count_offset;
-    int64_t header_footer_partition_offset;
+    int64_t footer_partition_offset;
     int essence_container_count;
     uint8_t essence_containers_indices[sizeof(mxf_essence_container_uls)/
                                        sizeof(*mxf_essence_container_uls)];
@@ -678,7 +677,7 @@
     return 0;
 }
 
-static void mxf_write_partition(AVFormatContext *s, int64_t byte_position, int bodysid, const uint8_t *key)
+static void mxf_write_partition(AVFormatContext *s, int bodysid, const uint8_t *key, int write_metadata)
 {
     MXFContext *mxf = s->priv_data;
     ByteIOContext *pb = s->pb;
@@ -696,14 +695,10 @@
     put_be64(pb, url_ftell(pb) - 25); // thisPartition
     put_be64(pb, 0); // previousPartition
 
-    // set offset
-    if (!byte_position)
-        mxf->header_footer_partition_offset = url_ftell(pb);
-    put_be64(pb, byte_position); // footerPartition,update later
+    put_be64(pb, mxf->footer_partition_offset); // footerPartition
 
     // set offset
-    if (!byte_position)
-        mxf->header_byte_count_offset = url_ftell(pb);
+    mxf->header_byte_count_offset = url_ftell(pb);
     put_be64(pb, 0); // headerByteCount, update later
 
     // no indexTable
@@ -716,6 +711,20 @@
 
     // essence container
     mxf_write_essence_container_refs(s);
+
+    if (write_metadata) {
+        // mark the start of the headermetadata and calculate metadata size
+        int64_t pos, start = url_ftell(s->pb);
+        mxf_write_primer_pack(s);
+        mxf_write_header_metadata_sets(s);
+        pos = url_ftell(s->pb);
+        // update header_byte_count
+        url_fseek(pb, mxf->header_byte_count_offset, SEEK_SET);
+        put_be64(pb, pos - start);
+        url_fseek(pb, pos, SEEK_SET);
+    }
+
+    put_flush_packet(pb);
 }
 
 static const UID mxf_mpeg2_codec_uls[] = {
@@ -756,8 +765,6 @@
 static int mxf_write_header(AVFormatContext *s)
 {
     MXFContext *mxf = s->priv_data;
-    ByteIOContext *pb = s->pb;
-    int64_t header_metadata_start, offset_now;
     int i, index;
     uint8_t present[sizeof(mxf_essence_container_uls)/
                     sizeof(*mxf_essence_container_uls)] = {0};
@@ -807,25 +814,9 @@
         PRINT_KEY(s, "track essence element key", sc->track_essence_element_key);
     }
 
-    mxf_write_partition(s, 0, 1, header_open_partition_key);
+    mxf_write_partition(s, 1, header_open_partition_key, 1);
 
-    // mark the start of the headermetadata and calculate metadata size
-    header_metadata_start = url_ftell(s->pb);
-    mxf_write_primer_pack(s);
-    if (mxf_write_header_metadata_sets(s) < 0)
-        goto fail;
-    offset_now = url_ftell(s->pb);
-    mxf->header_byte_count = offset_now - header_metadata_start;
-    // update header_byte_count
-    url_fseek(pb, mxf->header_byte_count_offset, SEEK_SET);
-    put_be64(pb, mxf->header_byte_count);
-    url_fseek(pb, offset_now, SEEK_SET);
-
-    put_flush_packet(pb);
     return 0;
-fail:
-    mxf_free(s);
-    return -1;
 }
 
 static int mxf_write_packet(AVFormatContext *s, AVPacket *pkt)
@@ -844,26 +835,17 @@
     return 0;
 }
 
-static void mxf_update_header_partition(AVFormatContext *s, int64_t footer_partition_offset)
+static int mxf_write_footer(AVFormatContext *s)
 {
     MXFContext *mxf = s->priv_data;
     ByteIOContext *pb = s->pb;
 
-    url_fseek(pb, mxf->header_footer_partition_offset, SEEK_SET);
-    put_be64(pb, footer_partition_offset);
-    put_flush_packet(pb);
-}
-
-
-static int mxf_write_footer(AVFormatContext *s)
-{
-    ByteIOContext *pb = s->pb;
-    int64_t byte_position= url_ftell(pb);
-
-    mxf_write_partition(s, byte_position, 0, footer_partition_key);
-    put_flush_packet(pb);
-    if (!url_is_streamed(s->pb))
-        mxf_update_header_partition(s, byte_position);
+    mxf->footer_partition_offset = url_ftell(pb);
+    mxf_write_partition(s, 0, footer_partition_key, 0);
+    if (!url_is_streamed(s->pb)) {
+        url_fseek(pb, 0, SEEK_SET);
+        mxf_write_partition(s, 1, header_closed_partition_key, 1);
+    }
     mxf_free(s);
     return 0;
 }