Mercurial > libavformat.hg
comparison mxfenc.c @ 4393:87a896580005 libavformat
use 512 kag size needed for d10
author | bcoudurier |
---|---|
date | Sat, 07 Feb 2009 02:13:23 +0000 |
parents | e8e064a00ea5 |
children | 2a72b8b7a69d |
comparison
equal
deleted
inserted
replaced
4392:ef5d6bb2c41b | 4393:87a896580005 |
---|---|
40 | 40 |
41 static const int NTSC_samples_per_frame[] = { 1602, 1601, 1602, 1601, 1602, 0 }; | 41 static const int NTSC_samples_per_frame[] = { 1602, 1601, 1602, 1601, 1602, 0 }; |
42 static const int PAL_samples_per_frame[] = { 1920, 0 }; | 42 static const int PAL_samples_per_frame[] = { 1920, 0 }; |
43 | 43 |
44 #define MXF_INDEX_CLUSTER_SIZE 4096 | 44 #define MXF_INDEX_CLUSTER_SIZE 4096 |
45 #define KAG_SIZE 512 | |
45 | 46 |
46 typedef struct { | 47 typedef struct { |
47 AVFifoBuffer fifo; | 48 AVFifoBuffer fifo; |
48 unsigned fifo_size; ///< current fifo size allocated | 49 unsigned fifo_size; ///< current fifo size allocated |
49 uint64_t dts; ///< current dts | 50 uint64_t dts; ///< current dts |
138 static const uint8_t primer_pack_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0D,0x01,0x02,0x01,0x01,0x05,0x01,0x00 }; | 139 static const uint8_t primer_pack_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0D,0x01,0x02,0x01,0x01,0x05,0x01,0x00 }; |
139 static const uint8_t index_table_segment_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x10,0x01,0x00 }; | 140 static const uint8_t index_table_segment_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x10,0x01,0x00 }; |
140 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 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 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 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 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 | 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 * partial key for header metadata | 147 * partial key for header metadata |
146 */ | 148 */ |
147 static const uint8_t header_metadata_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0D,0x01,0x01,0x01,0x01 }; | 149 static const uint8_t header_metadata_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0D,0x01,0x01,0x01,0x01 }; |
246 { | 248 { |
247 put_be32(pb, ref_count); | 249 put_be32(pb, ref_count); |
248 put_be32(pb, 16); | 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 static int klv_encode_ber_length(ByteIOContext *pb, uint64_t len) | 261 static int klv_encode_ber_length(ByteIOContext *pb, uint64_t len) |
252 { | 262 { |
253 // Determine the best BER size | 263 // Determine the best BER size |
254 int size; | 264 int size; |
255 if (len < 128) { | 265 if (len < 128) { |
836 mxf_write_content_storage(s); | 846 mxf_write_content_storage(s); |
837 mxf_write_package(s, MaterialPackage); | 847 mxf_write_package(s, MaterialPackage); |
838 mxf_write_package(s, SourcePackage); | 848 mxf_write_package(s, SourcePackage); |
839 mxf_write_essence_container_data(s); | 849 mxf_write_essence_container_data(s); |
840 return 0; | 850 return 0; |
851 } | |
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); | |
841 } | 860 } |
842 | 861 |
843 static int mxf_write_index_table_segment(AVFormatContext *s) | 862 static int mxf_write_index_table_segment(AVFormatContext *s) |
844 { | 863 { |
845 MXFContext *mxf = s->priv_data; | 864 MXFContext *mxf = s->priv_data; |
961 klv_encode_ber_length(pb, 88 + 16 * mxf->essence_container_count); | 980 klv_encode_ber_length(pb, 88 + 16 * mxf->essence_container_count); |
962 | 981 |
963 // write partition value | 982 // write partition value |
964 put_be16(pb, 1); // majorVersion | 983 put_be16(pb, 1); // majorVersion |
965 put_be16(pb, 2); // minorVersion | 984 put_be16(pb, 2); // minorVersion |
966 put_be32(pb, 1); // kagSize | 985 put_be32(pb, KAG_SIZE); // KAGSize |
967 | 986 |
968 put_be64(pb, url_ftell(pb) - 25); // thisPartition | 987 put_be64(pb, url_ftell(pb) - 25); // thisPartition |
969 put_be64(pb, 0); // previousPartition | 988 put_be64(pb, 0); // previousPartition |
970 | 989 |
971 put_be64(pb, mxf->footer_partition_offset); // footerPartition | 990 put_be64(pb, mxf->footer_partition_offset); // footerPartition |
986 mxf_write_essence_container_refs(s); | 1005 mxf_write_essence_container_refs(s); |
987 | 1006 |
988 if (write_metadata) { | 1007 if (write_metadata) { |
989 // mark the start of the headermetadata and calculate metadata size | 1008 // mark the start of the headermetadata and calculate metadata size |
990 int64_t pos, start = url_ftell(s->pb); | 1009 int64_t pos, start = url_ftell(s->pb); |
1010 unsigned header_byte_count; | |
1011 | |
991 mxf_write_primer_pack(s); | 1012 mxf_write_primer_pack(s); |
992 mxf_write_header_metadata_sets(s); | 1013 mxf_write_header_metadata_sets(s); |
993 pos = url_ftell(s->pb); | 1014 pos = url_ftell(s->pb); |
1015 header_byte_count = pos - start + klv_fill_size(s); | |
1016 | |
994 // update header_byte_count | 1017 // update header_byte_count |
995 url_fseek(pb, header_byte_count_offset, SEEK_SET); | 1018 url_fseek(pb, header_byte_count_offset, SEEK_SET); |
996 put_be64(pb, pos - start); | 1019 put_be64(pb, header_byte_count); |
997 url_fseek(pb, pos, SEEK_SET); | 1020 url_fseek(pb, pos, SEEK_SET); |
998 } | 1021 } |
999 | 1022 |
1000 put_flush_packet(pb); | 1023 put_flush_packet(pb); |
1001 } | 1024 } |
1207 return -1; | 1230 return -1; |
1208 | 1231 |
1209 return 0; | 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 static int mxf_write_packet(AVFormatContext *s, AVPacket *pkt) | 1251 static int mxf_write_packet(AVFormatContext *s, AVPacket *pkt) |
1213 { | 1252 { |
1214 MXFContext *mxf = s->priv_data; | 1253 MXFContext *mxf = s->priv_data; |
1215 ByteIOContext *pb = s->pb; | 1254 ByteIOContext *pb = s->pb; |
1216 AVStream *st = s->streams[pkt->stream_index]; | 1255 AVStream *st = s->streams[pkt->stream_index]; |
1236 if (!mxf->header_written) { | 1275 if (!mxf->header_written) { |
1237 mxf_write_partition(s, 1, 0, 0, header_open_partition_key, 1); | 1276 mxf_write_partition(s, 1, 0, 0, header_open_partition_key, 1); |
1238 mxf->header_written = 1; | 1277 mxf->header_written = 1; |
1239 } | 1278 } |
1240 | 1279 |
1280 mxf_write_klv_fill(s); | |
1281 | |
1241 if (st->index == mxf->edit_unit_start) { | 1282 if (st->index == mxf->edit_unit_start) { |
1242 mxf->index_entries[mxf->edit_units_count].offset = url_ftell(pb); | 1283 mxf->index_entries[mxf->edit_units_count].offset = url_ftell(pb); |
1243 mxf->index_entries[mxf->edit_units_count].slice_offset[st->index] = 0; | 1284 mxf->index_entries[mxf->edit_units_count].slice_offset[st->index] = 0; |
1244 mxf->edit_units_count++; | 1285 mxf->edit_units_count++; |
1245 } else { | 1286 } else { |
1282 unsigned index_byte_count = | 1323 unsigned index_byte_count = |
1283 109 + s->nb_streams*6 + | 1324 109 + s->nb_streams*6 + |
1284 mxf->edit_units_count*(11+(s->nb_streams-1)*4); | 1325 mxf->edit_units_count*(11+(s->nb_streams-1)*4); |
1285 | 1326 |
1286 // add encoded ber length | 1327 // add encoded ber length |
1287 index_byte_count += 16 + | 1328 index_byte_count += 16 + klv_ber_length(index_byte_count); |
1288 (index_byte_count < 128 ? 1 : (av_log2(index_byte_count) >> 3) + 2); | 1329 |
1330 mxf_write_klv_fill(s); | |
1289 | 1331 |
1290 mxf->footer_partition_offset = url_ftell(pb); | 1332 mxf->footer_partition_offset = url_ftell(pb); |
1291 mxf_write_partition(s, 0, 2, index_byte_count, footer_partition_key, 0); | 1333 mxf_write_partition(s, 0, 2, index_byte_count, footer_partition_key, 0); |
1292 | 1334 |
1293 mxf_write_index_table_segment(s); | 1335 mxf_write_index_table_segment(s); |