00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00028 #include "libavutil/intreadwrite.h"
00029 #include "libavutil/avassert.h"
00030 #include "avformat.h"
00031 #include "internal.h"
00032 #include "wtv.h"
00033 #include "asf.h"
00034
00035 #define WTV_BIGSECTOR_SIZE (1 << WTV_BIGSECTOR_BITS)
00036 #define INDEX_BASE 0x2
00037 #define MAX_NB_INDEX 10
00038
00039
00040 #define _ , 0,
00041 static const uint8_t timeline_table_0_header_events[] =
00042 {'t'_'i'_'m'_'e'_'l'_'i'_'n'_'e'_'.'_'t'_'a'_'b'_'l'_'e'_'.'_'0'_'.'_'h'_'e'_'a'_'d'_'e'_'r'_'.'_'E'_'v'_'e'_'n'_'t'_'s', 0};
00043 static const uint8_t table_0_header_legacy_attrib[] =
00044 {'t'_'a'_'b'_'l'_'e'_'.'_'0'_'.'_'h'_'e'_'a'_'d'_'e'_'r'_'.'_'l'_'e'_'g'_'a'_'c'_'y'_'_'_'a'_'t'_'t'_'r'_'i'_'b', 0};
00045 static const uint8_t table_0_redirector_legacy_attrib[] =
00046 {'t'_'a'_'b'_'l'_'e'_'.'_'0'_'.'_'r'_'e'_'d'_'i'_'r'_'e'_'c'_'t'_'o'_'r'_'.'_'l'_'e'_'g'_'a'_'c'_'y'_'_'_'a'_'t'_'t'_'r'_'i'_'b', 0};
00047 static const uint8_t table_0_header_time[] =
00048 {'t'_'a'_'b'_'l'_'e'_'.'_'0'_'.'_'h'_'e'_'a'_'d'_'e'_'r'_'.'_'t'_'i'_'m'_'e', 0};
00049 static const uint8_t legacy_attrib[] =
00050 {'l'_'e'_'g'_'a'_'c'_'y'_'_'_'a'_'t'_'t'_'r'_'i'_'b', 0};
00051 #undef _
00052
00053 static const ff_asf_guid sub_wtv_guid =
00054 {0x8C,0xC3,0xD2,0xC2,0x7E,0x9A,0xDA,0x11,0x8B,0xF7,0x00,0x07,0xE9,0x5E,0xAD,0x8D};
00055 static const ff_asf_guid stream1_guid =
00056 {0xA1,0xC3,0xD2,0xC2,0x7E,0x9A,0xDA,0x11,0x8B,0xF7,0x00,0x07,0xE9,0x5E,0xAD,0x8D};
00057 static const ff_asf_guid sync_guid =
00058 {0x97,0xC3,0xD2,0xC2,0x7E,0x9A,0xDA,0x11,0x8B,0xF7,0x00,0x07,0xE9,0x5E,0xAD,0x8D};
00059 static const ff_asf_guid index_guid =
00060 {0x96,0xc3,0xd2,0xc2,0x7e,0x9a,0xda,0x11,0x8b,0xf7,0x00,0x07,0xe9,0x5e,0xad,0x8d};
00061
00062 enum WtvFileIndex {
00063 WTV_TIMELINE_TABLE_0_HEADER_EVENTS = 0,
00064 WTV_TIMELINE_TABLE_0_ENTRIES_EVENTS,
00065 WTV_TIMELINE,
00066 WTV_TABLE_0_HEADER_LEGACY_ATTRIB,
00067 WTV_TABLE_0_ENTRIES_LEGACY_ATTRIB,
00068 WTV_TABLE_0_REDIRECTOR_LEGACY_ATTRIB,
00069 WTV_TABLE_0_HEADER_TIME,
00070 WTV_TABLE_0_ENTRIES_TIME,
00071 WTV_FILES
00072 };
00073
00074 typedef struct {
00075 int64_t length;
00076 const void *header;
00077 int depth;
00078 int first_sector;
00079 } WtvFile;
00080
00081 typedef struct {
00082 int64_t pos;
00083 int64_t serial;
00084 const ff_asf_guid * guid;
00085 int stream_id;
00086 } WtvChunkEntry;
00087
00088 typedef struct {
00089 int64_t timeline_start_pos;
00090 WtvFile file[WTV_FILES];
00091 int64_t serial;
00092 int64_t last_chunk_pos;
00093 int64_t frame_nb;
00094
00095 WtvChunkEntry index[MAX_NB_INDEX];
00096 int nb_index;
00097 int first_video_flag;
00098 int64_t sync_pos;
00099 } WtvContext;
00100
00101 typedef int WTVHeaderWriteFunc(AVIOContext *pb);
00102
00103 typedef struct {
00104 const uint8_t *header;
00105 int header_size;
00106 WTVHeaderWriteFunc *write_header;
00107 } WTVRootEntryTable;
00108
00109 static int write_pad(AVIOContext *pb, int size)
00110 {
00111 for (; size > 0; size--)
00112 avio_w8(pb, 0);
00113 return 0;
00114 }
00115
00116 static const ff_asf_guid *get_codec_guid(enum AVCodecID id, const AVCodecGuid *av_guid)
00117 {
00118 int i;
00119 for (i = 0; av_guid[i].id != AV_CODEC_ID_NONE; i++) {
00120 if (id == av_guid[i].id)
00121 return &(av_guid[i].guid);
00122 }
00123 return NULL;
00124 }
00125
00129 static void write_chunk_header(AVFormatContext *s, const ff_asf_guid *guid, int length, int stream_id)
00130 {
00131 WtvContext *wctx = s->priv_data;
00132 AVIOContext *pb = s->pb;
00133
00134 wctx->last_chunk_pos = avio_tell(pb) - wctx->timeline_start_pos;
00135 ff_put_guid(pb, guid);
00136 avio_wl32(pb, 32 + length);
00137 avio_wl32(pb, stream_id);
00138 avio_wl64(pb, wctx->serial);
00139
00140 if ((stream_id & 0x80000000) && guid != &index_guid) {
00141 WtvChunkEntry *t = wctx->index + wctx->nb_index;
00142 av_assert0(wctx->nb_index < MAX_NB_INDEX);
00143 t->pos = wctx->last_chunk_pos;
00144 t->serial = wctx->serial;
00145 t->guid = guid;
00146 t->stream_id = stream_id & 0x3FFFFFFF;
00147 wctx->nb_index++;
00148 }
00149 }
00150
00151 static void write_chunk_header2(AVFormatContext *s, const ff_asf_guid *guid, int stream_id)
00152 {
00153 WtvContext *wctx = s->priv_data;
00154 AVIOContext *pb = s->pb;
00155
00156 int64_t last_chunk_pos = wctx->last_chunk_pos;
00157 write_chunk_header(s, guid, 0, stream_id);
00158 avio_wl64(pb, last_chunk_pos);
00159 }
00160
00161 static void finish_chunk_noindex(AVFormatContext *s)
00162 {
00163 WtvContext *wctx = s->priv_data;
00164 AVIOContext *pb = s->pb;
00165
00166
00167 int64_t chunk_len = avio_tell(pb) - (wctx->last_chunk_pos + wctx->timeline_start_pos);
00168 avio_seek(pb, -(chunk_len - 16), SEEK_CUR);
00169 avio_wl32(pb, chunk_len);
00170 avio_seek(pb, chunk_len - (16 + 4), SEEK_CUR);
00171
00172 write_pad(pb, WTV_PAD8(chunk_len) - chunk_len);
00173 wctx->serial++;
00174 }
00175
00176 static void write_index(AVFormatContext *s)
00177 {
00178 AVIOContext *pb = s->pb;
00179 WtvContext *wctx = s->priv_data;
00180 int i;
00181
00182 write_chunk_header2(s, &index_guid, 0x80000000);
00183 avio_wl32(pb, 0);
00184 avio_wl32(pb, 0);
00185
00186 for (i = 0; i < wctx->nb_index; i++) {
00187 WtvChunkEntry *t = wctx->index + i;
00188 ff_put_guid(pb, t->guid);
00189 avio_wl64(pb, t->pos);
00190 avio_wl32(pb, t->stream_id);
00191 avio_wl32(pb, 0);
00192 avio_wl64(pb, t->serial);
00193 }
00194 wctx->nb_index = 0;
00195 finish_chunk_noindex(s);
00196 }
00197
00198 static void finish_chunk(AVFormatContext *s)
00199 {
00200 WtvContext *wctx = s->priv_data;
00201 finish_chunk_noindex(s);
00202 if (wctx->nb_index == MAX_NB_INDEX)
00203 write_index(s);
00204 }
00205
00206 static int write_stream_codec_info(AVFormatContext *s, AVStream *st)
00207 {
00208 WtvContext *wctx = s->priv_data;
00209 const ff_asf_guid *g, *media_type, *format_type;
00210 AVIOContext *pb = s->pb;
00211 int64_t hdr_pos_start;
00212 int hdr_size = 0;
00213
00214 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
00215 g = get_codec_guid(st->codec->codec_id, ff_video_guids);
00216 media_type = &ff_mediatype_video;
00217 format_type = &ff_format_mpeg2_video;
00218 } else if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
00219 g = get_codec_guid(st->codec->codec_id, ff_codec_wav_guids);
00220 media_type = &ff_mediatype_audio;
00221 format_type = &ff_format_waveformatex;
00222 } else {
00223 av_log(s, AV_LOG_ERROR, "unknown codec_type (0x%x)\n", st->codec->codec_type);
00224 return -1;
00225 }
00226
00227 if (g == NULL) {
00228 av_log(s, AV_LOG_ERROR, "can't get video codec_id (0x%x) guid.\n", st->codec->codec_id);
00229 return -1;
00230 }
00231
00232 ff_put_guid(pb, media_type);
00233 ff_put_guid(pb, &ff_mediasubtype_cpfilters_processed);
00234 write_pad(pb, 12);
00235 ff_put_guid(pb,&ff_format_cpfilters_processed);
00236 avio_wl32(pb, 0);
00237
00238 hdr_pos_start = avio_tell(pb);
00239 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
00240 if (wctx->first_video_flag) {
00241 write_pad(pb, 216);
00242 wctx->first_video_flag = 0;
00243 } else {
00244 write_pad(pb, 72);
00245 ff_put_bmp_header(pb, st->codec, ff_codec_bmp_tags, 0);
00246 }
00247 } else {
00248 ff_put_wav_header(pb, st->codec);
00249 }
00250 hdr_size = avio_tell(pb) - hdr_pos_start;
00251
00252
00253 avio_seek(pb, -(hdr_size + 4), SEEK_CUR);
00254 avio_wl32(pb, hdr_size + 32);
00255 avio_seek(pb, hdr_size, SEEK_CUR);
00256 ff_put_guid(pb, g);
00257 ff_put_guid(pb, format_type);
00258
00259 return 0;
00260 }
00261
00262 static int write_stream_codec(AVFormatContext *s, AVStream * st)
00263 {
00264 AVIOContext *pb = s->pb;
00265 int ret;
00266 write_chunk_header2(s, &stream1_guid, 0x80000000 | 0x01);
00267
00268 avio_wl32(pb, 0x01);
00269 write_pad(pb, 4);
00270 write_pad(pb, 4);
00271
00272 ret = write_stream_codec_info(s, st);
00273 if (ret < 0) {
00274 av_log(s, AV_LOG_ERROR, "write stream codec info failed codec_type(0x%x)\n", st->codec->codec_type);
00275 return -1;
00276 }
00277
00278 finish_chunk(s);
00279 return 0;
00280 }
00281
00282 static void write_sync(AVFormatContext *s)
00283 {
00284 AVIOContext *pb = s->pb;
00285 WtvContext *wctx = s->priv_data;
00286 int64_t last_chunk_pos = wctx->last_chunk_pos;
00287 wctx->sync_pos = avio_tell(pb) - wctx->timeline_start_pos;
00288
00289 write_chunk_header(s, &sync_guid, 0x18, 0);
00290 write_pad(pb, 24);
00291
00292 finish_chunk(s);
00293
00294 wctx->last_chunk_pos = last_chunk_pos;
00295 }
00296
00297 static void write_DSATTRIB_TRANSPORT_PROPERTIES_init(AVFormatContext *s, int stream_index)
00298 {
00299 AVIOContext *pb = s->pb;
00300 write_chunk_header2(s, &ff_DSATTRIB_TRANSPORT_PROPERTIES, 0x80000000 | stream_index);
00301 avio_wl64(pb, stream_index);
00302 avio_wl64(pb, -1);
00303 avio_wl64(pb, 0);
00304 finish_chunk(s);
00305 }
00306
00307 static int write_stream_data(AVFormatContext *s, AVStream *st, int flag)
00308 {
00309 AVIOContext *pb = s->pb;
00310 int ret;
00311
00312 if (!flag) {
00313 write_chunk_header2(s, &ff_stream_guid, 0x80000000 | (st->index + INDEX_BASE));
00314 avio_wl32(pb, 0x00000001);
00315 avio_wl32(pb, st->index + INDEX_BASE);
00316 avio_wl32(pb, 0x00000001);
00317 write_pad(pb, 8);
00318 } else {
00319 write_chunk_header2(s, &ff_stream2_guid, 0x80000000 | (st->index + INDEX_BASE));
00320 write_pad(pb, 4);
00321 }
00322
00323 ret = write_stream_codec_info(s, st);
00324 if (ret < 0) {
00325 av_log(s, AV_LOG_ERROR, "write stream codec info failed codec_type(0x%x)\n", st->codec->codec_type);
00326 return -1;
00327 }
00328 finish_chunk(s);
00329
00330 avpriv_set_pts_info(st, 64, 1, 10000000);
00331
00332 return 0;
00333 }
00334
00335 static int write_header(AVFormatContext *s)
00336 {
00337 AVIOContext *pb = s->pb;
00338 WtvContext *wctx = s->priv_data;
00339 int i, pad, ret;
00340 AVStream *st;
00341
00342 ff_put_guid(pb, &ff_wtv_guid);
00343 ff_put_guid(pb, &sub_wtv_guid);
00344
00345 avio_wl32(pb, 0x01);
00346 avio_wl32(pb, 0x02);
00347 avio_wl32(pb, 1 << WTV_SECTOR_BITS);
00348 avio_wl32(pb, 1 << WTV_BIGSECTOR_BITS);
00349
00350
00351 avio_wl32(pb, 0);
00352 write_pad(pb, 4);
00353 avio_wl32(pb, 0);
00354
00355 write_pad(pb, 32);
00356 avio_wl32(pb, 0);
00357
00358 pad = (1 << WTV_SECTOR_BITS) - avio_tell(pb);
00359 write_pad(pb, pad);
00360 wctx->timeline_start_pos = avio_tell(pb);
00361
00362 wctx->serial = 1;
00363 wctx->last_chunk_pos = -1;
00364 wctx->first_video_flag = 1;
00365
00366 for (i = 0; i < s->nb_streams; i++) {
00367 st = s->streams[i];
00368 ret = write_stream_codec(s, st);
00369 if (ret < 0) {
00370 av_log(s, AV_LOG_ERROR, "write stream codec failed codec_type(0x%x)\n", st->codec->codec_type);
00371 return -1;
00372 }
00373 if (i + 1 < s->nb_streams) {
00374 write_sync(s);
00375 }
00376 }
00377
00378 for (i = 0; i < s->nb_streams; i++) {
00379 st = s->streams[i];
00380 ret = write_stream_data(s, st, 0);
00381 if (ret < 0) {
00382 av_log(s, AV_LOG_ERROR, "write stream data failed codec_type(0x%x)\n", st->codec->codec_type);
00383 return -1;
00384 }
00385 ret = write_stream_data(s, st, 1);
00386 if (ret < 0) {
00387 av_log(s, AV_LOG_ERROR, "write stream2 data failed codec_type(0x%x)\n", st->codec->codec_type);
00388 return -1;
00389 }
00390 }
00391
00392 for (i = 0; i < s->nb_streams; i++)
00393 write_DSATTRIB_TRANSPORT_PROPERTIES_init(s, INDEX_BASE + i);
00394
00395 if (wctx->nb_index)
00396 write_index(s);
00397
00398 return 0;
00399 }
00400
00401 static void write_timestamp(AVFormatContext *s, AVPacket *pkt)
00402 {
00403 AVIOContext *pb = s->pb;
00404 WtvContext *wctx = s->priv_data;
00405 AVCodecContext *enc = s->streams[pkt->stream_index]->codec;
00406 int flag = 0;
00407 int64_t frame_number = 0;
00408
00409 if (enc->codec_type == AVMEDIA_TYPE_VIDEO) {
00410 wctx->frame_nb++;
00411 frame_number = wctx->frame_nb;
00412 flag = pkt->flags & AV_PKT_FLAG_KEY ? 1 : 0;
00413 }
00414 write_chunk_header(s, &ff_timestamp_guid, 56, 0x40000000 | (INDEX_BASE + pkt->stream_index));
00415 write_pad(pb, 8);
00416 avio_wl64(pb, pkt->pts == AV_NOPTS_VALUE ? -1 : pkt->pts);
00417 avio_wl64(pb, pkt->pts == AV_NOPTS_VALUE ? -1 : pkt->pts);
00418
00419 avio_wl64(pb, frame_number);
00420 avio_wl64(pb, 0);
00421 avio_wl64(pb, flag);
00422 avio_wl64(pb, 0);
00423 }
00424
00425 static int write_packet(AVFormatContext *s, AVPacket *pkt)
00426 {
00427 AVIOContext *pb = s->pb;
00428 WtvContext *wctx = s->priv_data;
00429
00430
00431 write_timestamp(s, pkt);
00432
00433 write_chunk_header(s, &ff_data_guid, pkt->size, INDEX_BASE + pkt->stream_index);
00434 avio_write(pb, pkt->data, pkt->size);
00435 write_pad(pb, WTV_PAD8(pkt->size) - pkt->size);
00436
00437 wctx->serial++;
00438 avio_flush(pb);
00439 return 0;
00440 }
00441
00442 static int write_table0_header_envents(AVIOContext *pb)
00443 {
00444 avio_wl32(pb, 0x10);
00445 write_pad(pb, 84);
00446 avio_wl64(pb, 0x32);
00447 return 96;
00448 }
00449
00450 static int write_table0_header_legacy_attrib(AVIOContext *pb)
00451 {
00452 int pad = 0;
00453 avio_wl32(pb, 0xFFFFFFFF);
00454 write_pad(pb, 12);
00455 avio_write(pb, legacy_attrib, sizeof(legacy_attrib));
00456 pad = WTV_PAD8(sizeof(legacy_attrib)) - sizeof(legacy_attrib);
00457 write_pad(pb, pad);
00458 write_pad(pb, 32);
00459 return 48 + WTV_PAD8(sizeof(legacy_attrib));
00460 }
00461
00462 static int write_table0_header_time(AVIOContext *pb)
00463 {
00464 avio_wl32(pb, 0x10);
00465 write_pad(pb, 76);
00466 avio_wl64(pb, 0x40);
00467 return 88;
00468 }
00469
00470 static const WTVRootEntryTable wtv_root_entry_table[] = {
00471 { timeline_table_0_header_events, sizeof(timeline_table_0_header_events), write_table0_header_envents},
00472 { ff_timeline_table_0_entries_Events_le16, sizeof(ff_timeline_table_0_entries_Events_le16), NULL},
00473 { ff_timeline_le16, sizeof(ff_timeline_le16), NULL},
00474 { table_0_header_legacy_attrib, sizeof(table_0_header_legacy_attrib), write_table0_header_legacy_attrib},
00475 { ff_table_0_entries_legacy_attrib_le16, sizeof(ff_table_0_entries_legacy_attrib_le16), NULL},
00476 { table_0_redirector_legacy_attrib, sizeof(table_0_redirector_legacy_attrib), NULL},
00477 { table_0_header_time, sizeof(table_0_header_time), write_table0_header_time},
00478 { ff_table_0_entries_time_le16, sizeof(ff_table_0_entries_time_le16), NULL},
00479 };
00480
00481 static int write_root_table(AVFormatContext *s, int64_t sector_pos)
00482 {
00483 AVIOContext *pb = s->pb;
00484 WtvContext *wctx = s->priv_data;
00485 int size, pad;
00486 int i;
00487
00488 const WTVRootEntryTable *h = wtv_root_entry_table;
00489 for (i = 0; i < sizeof(wtv_root_entry_table)/sizeof(WTVRootEntryTable); i++, h++) {
00490 WtvFile *w = &wctx->file[i];
00491 int filename_padding = WTV_PAD8(h->header_size) - h->header_size;
00492 WTVHeaderWriteFunc *write = h->write_header;
00493 int len = 0;
00494 int64_t len_pos;
00495
00496 ff_put_guid(pb, &ff_dir_entry_guid);
00497 len_pos = avio_tell(pb);
00498 avio_wl16(pb, 40 + h->header_size + filename_padding + 8);
00499 write_pad(pb, 6);
00500 avio_wl64(pb, write ? 0 : w->length);
00501 avio_wl32(pb, (h->header_size + filename_padding) >> 1);
00502 write_pad(pb, 4);
00503
00504 avio_write(pb, h->header, h->header_size);
00505 write_pad(pb, filename_padding);
00506
00507 if (write) {
00508 len = write(pb);
00509
00510 avio_seek(pb, len_pos, SEEK_SET);
00511 avio_wl64(pb, 40 + h->header_size + filename_padding + len);
00512 avio_wl64(pb, len |(1ULL<<62) | (1ULL<<60));
00513 avio_seek(pb, 8 + h->header_size + filename_padding + len, SEEK_CUR);
00514 } else {
00515 avio_wl32(pb, w->first_sector);
00516 avio_wl32(pb, w->depth);
00517 }
00518 }
00519
00520
00521 size = avio_tell(pb) - sector_pos;
00522 pad = WTV_SECTOR_SIZE- size;
00523 write_pad(pb, pad);
00524
00525 return size;
00526 }
00527
00528 static void write_fat(AVIOContext *pb, int start_sector, int nb_sectors, int shift)
00529 {
00530 int i;
00531 for (i = 0; i < nb_sectors; i++) {
00532 avio_wl32(pb, start_sector + (i << shift));
00533 }
00534
00535 write_pad(pb, WTV_SECTOR_SIZE - ((nb_sectors << 2) % WTV_SECTOR_SIZE));
00536 }
00537
00538 static int write_fat_sector(AVFormatContext *s, int64_t start_pos, int nb_sectors, int sector_bits, int depth)
00539 {
00540 int64_t start_sector = start_pos >> WTV_SECTOR_BITS;
00541 int shift = sector_bits - WTV_SECTOR_BITS;
00542
00543 int64_t fat = avio_tell(s->pb);
00544 write_fat(s->pb, start_sector, nb_sectors, shift);
00545
00546 if (depth == 2) {
00547 int64_t start_sector1 = fat >> WTV_SECTOR_BITS;
00548 int nb_sectors1 = ((nb_sectors << 2) + WTV_SECTOR_SIZE - 1) / WTV_SECTOR_SIZE;
00549 int64_t fat1 = avio_tell(s->pb);
00550
00551 write_fat(s->pb, start_sector1, nb_sectors1, 0);
00552 return fat1;
00553 }
00554
00555 return fat;
00556 }
00557
00558 static void write_table_entries_events(AVFormatContext *s)
00559 {
00560 AVIOContext *pb = s->pb;
00561 WtvContext *wctx = s->priv_data;
00562
00563
00564
00565 avio_wl64(pb, 0x2); avio_wl64(pb, wctx->sync_pos);
00566 }
00567
00568 static void write_tag(AVIOContext *pb, const char *key, const char *value)
00569 {
00570 ff_put_guid(pb, &ff_metadata_guid);
00571 avio_wl32(pb, 1);
00572 avio_wl32(pb, strlen(value)*2 + 2);
00573 avio_put_str16le(pb, key);
00574 avio_put_str16le(pb, value);
00575 }
00576
00577 static void write_table_entries_attrib(AVFormatContext *s)
00578 {
00579 AVDictionaryEntry *tag = 0;
00580
00581
00582 ff_metadata_conv(&s->metadata, ff_asf_metadata_conv, NULL);
00583 while ((tag = av_dict_get(s->metadata, "", tag, AV_DICT_IGNORE_SUFFIX)))
00584 write_tag(s->pb, tag->key, tag->value);
00585 }
00586
00587 static void write_table_redirector_legacy_attrib(AVFormatContext *s)
00588 {
00589 AVIOContext *pb = s->pb;
00590 AVDictionaryEntry *tag = 0;
00591 int64_t pos = 0;
00592
00593
00594 while ((tag = av_dict_get(s->metadata, "", tag, AV_DICT_IGNORE_SUFFIX))) {
00595 avio_wl64(pb, pos);
00596 pos += 16 + 4 + 4 + strlen(tag->key)*2 + 2 + strlen(tag->value)*2 + 2;
00597 }
00598 }
00599
00605 static int finish_file(AVFormatContext *s, enum WtvFileIndex index, int64_t start_pos)
00606 {
00607 WtvContext *wctx = s->priv_data;
00608 AVIOContext *pb = s->pb;
00609 WtvFile *w = &wctx->file[index];
00610 int64_t end_pos = avio_tell(pb);
00611 int sector_bits, nb_sectors, pad;
00612
00613 av_assert0(index < WTV_FILES);
00614
00615 w->length = (end_pos - start_pos);
00616
00617
00618 if (w->length <= WTV_SECTOR_SIZE) {
00619 w->depth = 0;
00620 sector_bits = WTV_SECTOR_BITS;
00621 } else if (w->length <= (WTV_SECTOR_SIZE / 4) * WTV_SECTOR_SIZE) {
00622 w->depth = 1;
00623 sector_bits = WTV_SECTOR_BITS;
00624 } else if (w->length <= (WTV_SECTOR_SIZE / 4) * WTV_BIGSECTOR_SIZE) {
00625 w->depth = 1;
00626 sector_bits = WTV_BIGSECTOR_BITS;
00627 } else if (w->length <= (int64_t)(WTV_SECTOR_SIZE / 4) * (WTV_SECTOR_SIZE / 4) * WTV_SECTOR_SIZE) {
00628 w->depth = 2;
00629 sector_bits = WTV_SECTOR_BITS;
00630 } else if (w->length <= (int64_t)(WTV_SECTOR_SIZE / 4) * (WTV_SECTOR_SIZE / 4) * WTV_BIGSECTOR_SIZE) {
00631 w->depth = 2;
00632 sector_bits = WTV_BIGSECTOR_BITS;
00633 } else {
00634 av_log(s, AV_LOG_ERROR, "unsupported file allocation table depth (%"PRIi64" bytes)\n", w->length);
00635 return -1;
00636 }
00637
00638
00639 nb_sectors = (int)(w->length >> sector_bits);
00640
00641
00642 pad = (1 << sector_bits) - (w->length % (1 << sector_bits));
00643 if (pad) {
00644 nb_sectors++;
00645 write_pad(pb, pad);
00646 }
00647
00648
00649 if (w->depth > 0) {
00650 w->first_sector = write_fat_sector(s, start_pos, nb_sectors, sector_bits, w->depth);
00651 } else {
00652 w->first_sector = start_pos;
00653 }
00654 w->first_sector >>= WTV_SECTOR_BITS;
00655
00656 w->length |= 1ULL<<60;
00657 if (sector_bits == WTV_SECTOR_BITS)
00658 w->length |= 1ULL<<63;
00659
00660 return 0;
00661 }
00662
00663 static int write_trailer(AVFormatContext *s)
00664 {
00665 WtvContext *wctx = s->priv_data;
00666 AVIOContext *pb = s->pb;
00667 int root_size;
00668 int64_t sector_pos;
00669 int64_t start_pos, file_end_pos;
00670
00671 if (finish_file(s, WTV_TIMELINE, wctx->timeline_start_pos) < 0)
00672 return -1;
00673
00674 start_pos = avio_tell(pb);
00675 write_table_entries_events(s);
00676 if (finish_file(s, WTV_TIMELINE_TABLE_0_ENTRIES_EVENTS, start_pos) < 0)
00677 return -1;
00678
00679 start_pos = avio_tell(pb);
00680 write_table_entries_attrib(s);
00681 if (finish_file(s, WTV_TABLE_0_ENTRIES_LEGACY_ATTRIB, start_pos) < 0)
00682 return -1;
00683
00684 start_pos = avio_tell(pb);
00685 write_table_redirector_legacy_attrib(s);
00686 if (finish_file(s, WTV_TABLE_0_REDIRECTOR_LEGACY_ATTRIB, start_pos) < 0)
00687 return -1;
00688
00689 start_pos = avio_tell(pb);
00690
00691 if (finish_file(s, WTV_TABLE_0_ENTRIES_TIME, start_pos) < 0)
00692 return -1;
00693
00694
00695 sector_pos = avio_tell(pb);
00696 root_size = write_root_table(s, sector_pos);
00697
00698 file_end_pos = avio_tell(pb);
00699
00700 avio_seek(pb, 0x30, SEEK_SET);
00701 avio_wl32(pb, root_size);
00702 avio_seek(pb, 4, SEEK_CUR);
00703 avio_wl32(pb, sector_pos >> WTV_SECTOR_BITS);
00704 avio_seek(pb, 0x5c, SEEK_SET);
00705 avio_wl32(pb, file_end_pos >> WTV_SECTOR_BITS);
00706
00707 avio_flush(pb);
00708 return 0;
00709 }
00710
00711 AVOutputFormat ff_wtv_muxer = {
00712 .name = "wtv",
00713 .long_name = NULL_IF_CONFIG_SMALL("Windows Television (WTV)"),
00714 .extensions = "wtv",
00715 .priv_data_size = sizeof(WtvContext),
00716 .audio_codec = AV_CODEC_ID_AC3,
00717 .video_codec = AV_CODEC_ID_MPEG2VIDEO,
00718 .write_header = write_header,
00719 .write_packet = write_packet,
00720 .write_trailer = write_trailer,
00721 .codec_tag = (const AVCodecTag* const []){ ff_codec_bmp_tags,
00722 ff_codec_wav_tags, 0 },
00723 };