]> git.sesse.net Git - ffmpeg/blob - libavformat/wtvenc.c
wtvenc: do not emit stream2 and DSATTRIB_TRANSPORT_PROPERTY chunks; these are not...
[ffmpeg] / libavformat / wtvenc.c
1 /*
2  * Windows Television (WTV) muxer
3  * Copyright (c) 2011 Zhentan Feng <spyfeng at gmail dot com>
4  * Copyright (c) 2011 Peter Ross <pross@xvid.org>
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 /**
23  * @file
24  * Windows Television (WTV) demuxer
25  * @author Zhentan Feng <spyfeng at gmail dot com>
26  */
27
28 #include "libavutil/intreadwrite.h"
29 #include "libavutil/avassert.h"
30 #include "avformat.h"
31 #include "internal.h"
32 #include "wtv.h"
33 #include "asf.h"
34
35 #define WTV_BIGSECTOR_SIZE (1 << WTV_BIGSECTOR_BITS)
36 #define INDEX_BASE 0x2
37 #define MAX_NB_INDEX 10
38
39 /* declare utf16le strings */
40 #define _ , 0,
41 static const uint8_t timeline_table_0_header_events[] =
42     {'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};
43 static const uint8_t table_0_header_legacy_attrib[] =
44     {'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};
45 static const uint8_t table_0_redirector_legacy_attrib[] =
46     {'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};
47 static const uint8_t table_0_header_time[] =
48     {'t'_'a'_'b'_'l'_'e'_'.'_'0'_'.'_'h'_'e'_'a'_'d'_'e'_'r'_'.'_'t'_'i'_'m'_'e', 0};
49 static const uint8_t legacy_attrib[] =
50     {'l'_'e'_'g'_'a'_'c'_'y'_'_'_'a'_'t'_'t'_'r'_'i'_'b', 0};
51 #undef _
52
53 static const ff_asf_guid sub_wtv_guid =
54     {0x8C,0xC3,0xD2,0xC2,0x7E,0x9A,0xDA,0x11,0x8B,0xF7,0x00,0x07,0xE9,0x5E,0xAD,0x8D};
55
56 enum WtvFileIndex {
57     WTV_TIMELINE_TABLE_0_HEADER_EVENTS = 0,
58     WTV_TIMELINE_TABLE_0_ENTRIES_EVENTS,
59     WTV_TIMELINE,
60     WTV_TABLE_0_HEADER_LEGACY_ATTRIB,
61     WTV_TABLE_0_ENTRIES_LEGACY_ATTRIB,
62     WTV_TABLE_0_REDIRECTOR_LEGACY_ATTRIB,
63     WTV_TABLE_0_HEADER_TIME,
64     WTV_TABLE_0_ENTRIES_TIME,
65     WTV_FILES
66 };
67
68 typedef struct {
69     int64_t length;
70     const void *header;
71     int depth;
72     int first_sector;
73 } WtvFile;
74
75 typedef struct {
76     int64_t             pos;
77     int64_t             serial;
78     const ff_asf_guid * guid;
79     int                 stream_id;
80 } WtvChunkEntry;
81
82 typedef struct {
83     int64_t timeline_start_pos;
84     WtvFile file[WTV_FILES];
85     int64_t serial;         /** chunk serial number */
86     int64_t last_chunk_pos; /** last chunk position */
87     int64_t frame_nb;
88
89     WtvChunkEntry index[MAX_NB_INDEX];
90     int nb_index;
91     int first_video_flag;
92     int64_t sync_pos;
93 } WtvContext;
94
95 typedef int WTVHeaderWriteFunc(AVIOContext *pb);
96
97 typedef struct {
98     const uint8_t *header;
99     int header_size;
100     WTVHeaderWriteFunc *write_header;
101 } WTVRootEntryTable;
102
103 static int write_pad(AVIOContext *pb, int size)
104 {
105     for (; size > 0; size--)
106         avio_w8(pb, 0);
107     return 0;
108 }
109
110 static const ff_asf_guid *get_codec_guid(enum AVCodecID id, const AVCodecGuid *av_guid)
111 {
112     int i;
113     for (i = 0; av_guid[i].id != AV_CODEC_ID_NONE; i++) {
114         if (id == av_guid[i].id)
115             return &(av_guid[i].guid);
116     }
117     return NULL;
118 }
119
120 /**
121  * Write chunk header. If header chunk (0x80000000 set) then add to list of header chunks
122  */
123 static void write_chunk_header(AVFormatContext *s, const ff_asf_guid *guid, int length, int stream_id)
124 {
125     WtvContext *wctx = s->priv_data;
126     AVIOContext *pb = s->pb;
127
128     wctx->last_chunk_pos = avio_tell(pb) - wctx->timeline_start_pos;
129     ff_put_guid(pb, guid);
130     avio_wl32(pb, 32 + length);
131     avio_wl32(pb, stream_id);
132     avio_wl64(pb, wctx->serial);
133
134     if ((stream_id & 0x80000000) && guid != &ff_index_guid) {
135         WtvChunkEntry *t = wctx->index + wctx->nb_index;
136         av_assert0(wctx->nb_index < MAX_NB_INDEX);
137         t->pos       = wctx->last_chunk_pos;
138         t->serial    = wctx->serial;
139         t->guid      = guid;
140         t->stream_id = stream_id & 0x3FFFFFFF;
141         wctx->nb_index++;
142     }
143 }
144
145 static void write_chunk_header2(AVFormatContext *s, const ff_asf_guid *guid, int stream_id)
146 {
147     WtvContext *wctx = s->priv_data;
148     AVIOContext *pb = s->pb;
149
150     int64_t last_chunk_pos = wctx->last_chunk_pos;
151     write_chunk_header(s, guid, 0, stream_id); // length updated later
152     avio_wl64(pb, last_chunk_pos);
153 }
154
155 static void finish_chunk_noindex(AVFormatContext *s)
156 {
157     WtvContext *wctx = s->priv_data;
158     AVIOContext *pb = s->pb;
159
160     // update the chunk_len field and pad.
161     int64_t chunk_len = avio_tell(pb) - (wctx->last_chunk_pos + wctx->timeline_start_pos);
162     avio_seek(pb, -(chunk_len - 16), SEEK_CUR);
163     avio_wl32(pb, chunk_len);
164     avio_seek(pb, chunk_len - (16 + 4), SEEK_CUR);
165
166     write_pad(pb, WTV_PAD8(chunk_len) - chunk_len);
167     wctx->serial++;
168 }
169
170 static void write_index(AVFormatContext *s)
171 {
172     AVIOContext *pb = s->pb;
173     WtvContext *wctx = s->priv_data;
174     int i;
175
176     write_chunk_header2(s, &ff_index_guid, 0x80000000);
177     avio_wl32(pb, 0);
178     avio_wl32(pb, 0);
179
180     for (i = 0; i < wctx->nb_index; i++) {
181         WtvChunkEntry *t = wctx->index + i;
182         ff_put_guid(pb,  t->guid);
183         avio_wl64(pb, t->pos);
184         avio_wl32(pb, t->stream_id);
185         avio_wl32(pb, 0); // checksum?
186         avio_wl64(pb, t->serial);
187     }
188     wctx->nb_index = 0;   // reset index
189     finish_chunk_noindex(s);
190 }
191
192 static void finish_chunk(AVFormatContext *s)
193 {
194     WtvContext *wctx = s->priv_data;
195     finish_chunk_noindex(s);
196     if (wctx->nb_index == MAX_NB_INDEX)
197         write_index(s);
198 }
199
200 static int write_stream_codec_info(AVFormatContext *s, AVStream *st)
201 {
202     WtvContext *wctx = s->priv_data;
203     const ff_asf_guid *g, *media_type, *format_type;
204     AVIOContext *pb = s->pb;
205     int64_t  hdr_pos_start;
206     int hdr_size = 0;
207
208     if (st->codec->codec_type  == AVMEDIA_TYPE_VIDEO) {
209         g = get_codec_guid(st->codec->codec_id, ff_video_guids);
210         media_type = &ff_mediatype_video;
211         format_type = &ff_format_mpeg2_video;
212     } else if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
213         g = get_codec_guid(st->codec->codec_id, ff_codec_wav_guids);
214         media_type = &ff_mediatype_audio;
215         format_type = &ff_format_waveformatex;
216     } else {
217         av_log(s, AV_LOG_ERROR, "unknown codec_type (0x%x)\n", st->codec->codec_type);
218         return -1;
219     }
220
221     if (g == NULL) {
222         av_log(s, AV_LOG_ERROR, "can't get video codec_id (0x%x) guid.\n", st->codec->codec_id);
223         return -1;
224     }
225
226     ff_put_guid(pb, media_type); // mediatype
227     ff_put_guid(pb, &ff_mediasubtype_cpfilters_processed); // subtype
228     write_pad(pb, 12);
229     ff_put_guid(pb,&ff_format_cpfilters_processed); // format type
230     avio_wl32(pb, 0); // size
231
232     hdr_pos_start = avio_tell(pb);
233     if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
234         if (wctx->first_video_flag) {
235             write_pad(pb, 216); //The size is sensitive.
236             wctx->first_video_flag = 0;
237         } else {
238             write_pad(pb, 72); // aspect ratio
239             ff_put_bmp_header(pb, st->codec, ff_codec_bmp_tags, 0);
240         }
241     } else {
242         ff_put_wav_header(pb, st->codec);
243     }
244     hdr_size = avio_tell(pb) - hdr_pos_start;
245
246     // seek back write hdr_size
247     avio_seek(pb, -(hdr_size + 4), SEEK_CUR);
248     avio_wl32(pb, hdr_size + 32);
249     avio_seek(pb, hdr_size, SEEK_CUR);
250     ff_put_guid(pb, g);           // actual_subtype
251     ff_put_guid(pb, format_type); // actual_formattype
252
253     return 0;
254 }
255
256 static int write_stream_codec(AVFormatContext *s, AVStream * st)
257 {
258     AVIOContext *pb = s->pb;
259     int ret;
260     write_chunk_header2(s, &ff_stream1_guid, 0x80000000 | 0x01);
261
262     avio_wl32(pb,  0x01);
263     write_pad(pb, 4);
264     write_pad(pb, 4);
265
266     ret = write_stream_codec_info(s, st);
267     if (ret < 0) {
268         av_log(s, AV_LOG_ERROR, "write stream codec info failed codec_type(0x%x)\n", st->codec->codec_type);
269         return -1;
270     }
271
272     finish_chunk(s);
273     return 0;
274 }
275
276 static void write_sync(AVFormatContext *s)
277 {
278     AVIOContext *pb = s->pb;
279     WtvContext *wctx = s->priv_data;
280     int64_t last_chunk_pos = wctx->last_chunk_pos;
281     wctx->sync_pos = avio_tell(pb) - wctx->timeline_start_pos;
282
283     write_chunk_header(s, &ff_sync_guid, 0x18, 0);
284     write_pad(pb, 24);
285
286     finish_chunk(s);
287
288     wctx->last_chunk_pos = last_chunk_pos;
289 }
290
291 static int write_stream_data(AVFormatContext *s, AVStream *st)
292 {
293     AVIOContext *pb = s->pb;
294     int ret;
295
296         write_chunk_header2(s, &ff_SBE2_STREAM_DESC_EVENT, 0x80000000 | (st->index + INDEX_BASE));
297         avio_wl32(pb, 0x00000001);
298         avio_wl32(pb, st->index + INDEX_BASE); //stream_id
299         avio_wl32(pb, 0x00000001);
300         write_pad(pb, 8);
301
302     ret = write_stream_codec_info(s, st);
303     if (ret < 0) {
304         av_log(s, AV_LOG_ERROR, "write stream codec info failed codec_type(0x%x)\n", st->codec->codec_type);
305         return -1;
306     }
307     finish_chunk(s);
308
309     avpriv_set_pts_info(st, 64, 1, 10000000);
310
311     return 0;
312 }
313
314 static int write_header(AVFormatContext *s)
315 {
316     AVIOContext *pb = s->pb;
317     WtvContext *wctx = s->priv_data;
318     int i, pad, ret;
319     AVStream *st;
320
321     ff_put_guid(pb, &ff_wtv_guid);
322     ff_put_guid(pb, &sub_wtv_guid);
323
324     avio_wl32(pb, 0x01);
325     avio_wl32(pb, 0x02);
326     avio_wl32(pb, 1 << WTV_SECTOR_BITS);
327     avio_wl32(pb, 1 << WTV_BIGSECTOR_BITS);
328
329     //write initial root fields
330     avio_wl32(pb, 0); // root_size, update later
331     write_pad(pb, 4);
332     avio_wl32(pb, 0); // root_sector, update it later.
333
334     write_pad(pb, 32);
335     avio_wl32(pb, 0); // file ends pointer, update it later.
336
337     pad = (1 << WTV_SECTOR_BITS) - avio_tell(pb);
338     write_pad(pb, pad);
339     wctx->timeline_start_pos = avio_tell(pb);
340
341     wctx->serial = 1;
342     wctx->last_chunk_pos = -1;
343     wctx->first_video_flag = 1;
344
345     for (i = 0; i < s->nb_streams; i++) {
346         st = s->streams[i];
347         ret = write_stream_codec(s, st);
348         if (ret < 0) {
349             av_log(s, AV_LOG_ERROR, "write stream codec failed codec_type(0x%x)\n", st->codec->codec_type);
350             return -1;
351         }
352         if (!i)
353             write_sync(s);
354     }
355
356     for (i = 0; i < s->nb_streams; i++) {
357         st = s->streams[i];
358         ret  = write_stream_data(s, st);
359         if (ret < 0) {
360             av_log(s, AV_LOG_ERROR, "write stream data failed codec_type(0x%x)\n", st->codec->codec_type);
361             return -1;
362         }
363     }
364
365     if (wctx->nb_index)
366         write_index(s);
367
368     return 0;
369 }
370
371 static void write_timestamp(AVFormatContext *s, AVPacket *pkt)
372 {
373     AVIOContext *pb = s->pb;
374     WtvContext  *wctx = s->priv_data;
375     AVCodecContext *enc = s->streams[pkt->stream_index]->codec;
376     int flag = 0;
377     int64_t frame_number = 0;
378
379     if (enc->codec_type == AVMEDIA_TYPE_VIDEO) {
380         wctx->frame_nb++;
381         frame_number = wctx->frame_nb;
382         flag = pkt->flags & AV_PKT_FLAG_KEY ? 1 : 0;
383     }
384     write_chunk_header(s, &ff_timestamp_guid, 56, 0x40000000 | (INDEX_BASE + pkt->stream_index));
385     write_pad(pb, 8);
386     avio_wl64(pb, pkt->pts == AV_NOPTS_VALUE ? -1 : pkt->pts);
387     avio_wl64(pb, pkt->pts == AV_NOPTS_VALUE ? -1 : pkt->pts);
388
389     avio_wl64(pb, frame_number);
390     avio_wl64(pb, 0);
391     avio_wl64(pb, flag);
392     avio_wl64(pb, 0);
393 }
394
395 static int write_packet(AVFormatContext *s, AVPacket *pkt)
396 {
397     AVIOContext *pb = s->pb;
398     WtvContext  *wctx = s->priv_data;
399
400     // write timestamp chunk
401     write_timestamp(s, pkt);
402
403     write_chunk_header(s, &ff_data_guid, pkt->size, INDEX_BASE + pkt->stream_index);
404     avio_write(pb, pkt->data, pkt->size);
405     write_pad(pb, WTV_PAD8(pkt->size) - pkt->size);
406
407     wctx->serial++;
408     avio_flush(pb);
409     return 0;
410 }
411
412 static int write_table0_header_events(AVIOContext *pb)
413 {
414     avio_wl32(pb, 0x10);
415     write_pad(pb, 84);
416     avio_wl64(pb, 0x32);
417     return 96;
418 }
419
420 static int write_table0_header_legacy_attrib(AVIOContext *pb)
421 {
422     int pad = 0;
423     avio_wl32(pb, 0xFFFFFFFF);
424     write_pad(pb, 12);
425     avio_write(pb, legacy_attrib, sizeof(legacy_attrib));
426     pad = WTV_PAD8(sizeof(legacy_attrib)) - sizeof(legacy_attrib);
427     write_pad(pb, pad);
428     write_pad(pb, 32);
429     return 48 + WTV_PAD8(sizeof(legacy_attrib));
430 }
431
432 static int write_table0_header_time(AVIOContext *pb)
433 {
434     avio_wl32(pb, 0x10);
435     write_pad(pb, 76);
436     avio_wl64(pb, 0x40);
437     return 88;
438 }
439
440 static const WTVRootEntryTable wtv_root_entry_table[] = {
441     { timeline_table_0_header_events,          sizeof(timeline_table_0_header_events),          write_table0_header_events},
442     { ff_timeline_table_0_entries_Events_le16, sizeof(ff_timeline_table_0_entries_Events_le16), NULL},
443     { ff_timeline_le16,                        sizeof(ff_timeline_le16),                        NULL},
444     { table_0_header_legacy_attrib,            sizeof(table_0_header_legacy_attrib),            write_table0_header_legacy_attrib},
445     { ff_table_0_entries_legacy_attrib_le16,   sizeof(ff_table_0_entries_legacy_attrib_le16),   NULL},
446     { table_0_redirector_legacy_attrib,        sizeof(table_0_redirector_legacy_attrib),        NULL},
447     { table_0_header_time,                     sizeof(table_0_header_time),                     write_table0_header_time},
448     { ff_table_0_entries_time_le16,            sizeof(ff_table_0_entries_time_le16),            NULL},
449 };
450
451 static int write_root_table(AVFormatContext *s, int64_t sector_pos)
452 {
453     AVIOContext *pb = s->pb;
454     WtvContext  *wctx = s->priv_data;
455     int size, pad;
456     int i;
457
458     const WTVRootEntryTable *h = wtv_root_entry_table;
459     for (i = 0; i < sizeof(wtv_root_entry_table)/sizeof(WTVRootEntryTable); i++, h++) {
460         WtvFile *w = &wctx->file[i];
461         int filename_padding = WTV_PAD8(h->header_size) - h->header_size;
462         WTVHeaderWriteFunc *write = h->write_header;
463         int len = 0;
464         int64_t len_pos;
465
466         ff_put_guid(pb, &ff_dir_entry_guid);
467         len_pos = avio_tell(pb);
468         avio_wl16(pb, 40 + h->header_size + filename_padding + 8); // maybe updated later
469         write_pad(pb, 6);
470         avio_wl64(pb, write ? 0 : w->length);// maybe update later
471         avio_wl32(pb, (h->header_size + filename_padding) >> 1);
472         write_pad(pb, 4);
473
474         avio_write(pb, h->header, h->header_size);
475         write_pad(pb, filename_padding);
476
477         if (write) {
478             len = write(pb);
479             // update length field
480             avio_seek(pb, len_pos, SEEK_SET);
481             avio_wl64(pb, 40 + h->header_size + filename_padding + len);
482             avio_wl64(pb, len |(1ULL<<62) | (1ULL<<60));
483             avio_seek(pb, 8 + h->header_size + filename_padding + len, SEEK_CUR);
484         } else {
485             avio_wl32(pb, w->first_sector);
486             avio_wl32(pb, w->depth);
487         }
488     }
489
490     // caculate root table size
491     size = avio_tell(pb) - sector_pos;
492     pad = WTV_SECTOR_SIZE- size;
493     write_pad(pb, pad);
494
495     return size;
496 }
497
498 static void write_fat(AVIOContext *pb, int start_sector, int nb_sectors, int shift)
499 {
500     int i;
501     for (i = 0; i < nb_sectors; i++) {
502         avio_wl32(pb, start_sector + (i << shift));
503     }
504     // pad left sector pointer size
505     write_pad(pb, WTV_SECTOR_SIZE - ((nb_sectors << 2) % WTV_SECTOR_SIZE));
506 }
507
508 static int write_fat_sector(AVFormatContext *s, int64_t start_pos, int nb_sectors, int sector_bits, int depth)
509 {
510     int64_t start_sector = start_pos >> WTV_SECTOR_BITS;
511     int shift = sector_bits - WTV_SECTOR_BITS;
512
513     int64_t fat = avio_tell(s->pb);
514     write_fat(s->pb, start_sector, nb_sectors, shift);
515
516     if (depth == 2) {
517         int64_t start_sector1 = fat >> WTV_SECTOR_BITS;
518         int nb_sectors1 = ((nb_sectors << 2) + WTV_SECTOR_SIZE - 1) / WTV_SECTOR_SIZE;
519         int64_t fat1 = avio_tell(s->pb);
520
521        write_fat(s->pb, start_sector1, nb_sectors1, 0);
522        return fat1;
523     }
524
525     return fat;
526 }
527
528 static void write_table_entries_events(AVFormatContext *s)
529 {
530     AVIOContext *pb = s->pb;
531     WtvContext *wctx = s->priv_data;
532
533     //FIXME: output frame_nb, position pairs.
534     //We only set the first sync_chunk position here.
535     avio_wl64(pb, 0x2);   avio_wl64(pb, wctx->sync_pos);
536 }
537
538 static void write_tag(AVIOContext *pb, const char *key, const char *value)
539 {
540     ff_put_guid(pb, &ff_metadata_guid);
541     avio_wl32(pb, 1);
542     avio_wl32(pb, strlen(value)*2 + 2);
543     avio_put_str16le(pb, key);
544     avio_put_str16le(pb, value);
545 }
546
547 static void write_table_entries_attrib(AVFormatContext *s)
548 {
549     AVDictionaryEntry *tag = 0;
550
551     //FIXME: translate special tags (e.g. WM/Bitrate) to binary representation
552     ff_metadata_conv(&s->metadata, ff_asf_metadata_conv, NULL);
553     while ((tag = av_dict_get(s->metadata, "", tag, AV_DICT_IGNORE_SUFFIX)))
554         write_tag(s->pb, tag->key, tag->value);
555 }
556
557 static void write_table_redirector_legacy_attrib(AVFormatContext *s)
558 {
559     AVIOContext *pb = s->pb;
560     AVDictionaryEntry *tag = 0;
561     int64_t pos = 0;
562
563     //FIXME: translate special tags to binary representation
564     while ((tag = av_dict_get(s->metadata, "", tag, AV_DICT_IGNORE_SUFFIX))) {
565         avio_wl64(pb, pos);
566         pos += 16 + 4 + 4 + strlen(tag->key)*2 + 2 + strlen(tag->value)*2 + 2;
567     }
568 }
569
570 /**
571  * Pad the remainder of a file
572  * Write out fat table
573  * @return <0 on error
574  */
575 static int finish_file(AVFormatContext *s, enum WtvFileIndex index, int64_t start_pos)
576 {
577     WtvContext *wctx = s->priv_data;
578     AVIOContext *pb = s->pb;
579     WtvFile *w = &wctx->file[index];
580     int64_t end_pos = avio_tell(pb);
581     int sector_bits, nb_sectors, pad;
582
583     av_assert0(index < WTV_FILES);
584
585     w->length = (end_pos - start_pos);
586
587     // determine optimal fat table depth, sector_bits, nb_sectors
588     if (w->length <= WTV_SECTOR_SIZE) {
589         w->depth = 0;
590         sector_bits = WTV_SECTOR_BITS;
591     } else if (w->length <= (WTV_SECTOR_SIZE / 4) * WTV_SECTOR_SIZE) {
592         w->depth = 1;
593         sector_bits = WTV_SECTOR_BITS;
594     } else if (w->length <= (WTV_SECTOR_SIZE / 4) * WTV_BIGSECTOR_SIZE) {
595         w->depth = 1;
596         sector_bits = WTV_BIGSECTOR_BITS;
597     } else if (w->length <= (int64_t)(WTV_SECTOR_SIZE / 4) * (WTV_SECTOR_SIZE / 4) * WTV_SECTOR_SIZE) {
598         w->depth = 2;
599         sector_bits = WTV_SECTOR_BITS;
600     } else if (w->length <= (int64_t)(WTV_SECTOR_SIZE / 4) * (WTV_SECTOR_SIZE / 4) * WTV_BIGSECTOR_SIZE) {
601         w->depth = 2;
602         sector_bits = WTV_BIGSECTOR_BITS;
603     } else {
604         av_log(s, AV_LOG_ERROR, "unsupported file allocation table depth (%"PRIi64" bytes)\n", w->length);
605         return -1;
606     }
607
608     // determine the nb_sectors
609     nb_sectors = (int)(w->length >> sector_bits);
610
611     // pad sector of timeline
612     pad = (1 << sector_bits) - (w->length % (1 << sector_bits));
613     if (pad) {
614         nb_sectors++;
615         write_pad(pb, pad);
616     }
617
618     //write fat table
619     if (w->depth > 0) {
620         w->first_sector = write_fat_sector(s, start_pos, nb_sectors, sector_bits, w->depth);
621     } else {
622         w->first_sector = start_pos;
623     }
624     w->first_sector >>= WTV_SECTOR_BITS;
625
626     w->length |= 1ULL<<60;
627     if (sector_bits == WTV_SECTOR_BITS)
628         w->length |= 1ULL<<63;
629
630     return 0;
631 }
632
633 static int write_trailer(AVFormatContext *s)
634 {
635     WtvContext *wctx = s->priv_data;
636     AVIOContext *pb = s->pb;
637     int root_size;
638     int64_t sector_pos;
639     int64_t start_pos, file_end_pos;
640
641     if (finish_file(s, WTV_TIMELINE, wctx->timeline_start_pos) < 0)
642         return -1;
643
644     start_pos = avio_tell(pb);
645     write_table_entries_events(s);
646     if (finish_file(s, WTV_TIMELINE_TABLE_0_ENTRIES_EVENTS, start_pos) < 0)
647         return -1;
648
649     start_pos = avio_tell(pb);
650     write_table_entries_attrib(s);
651     if (finish_file(s, WTV_TABLE_0_ENTRIES_LEGACY_ATTRIB, start_pos) < 0)
652         return -1;
653
654     start_pos = avio_tell(pb);
655     write_table_redirector_legacy_attrib(s);
656     if (finish_file(s, WTV_TABLE_0_REDIRECTOR_LEGACY_ATTRIB, start_pos) < 0)
657         return -1;
658
659     start_pos = avio_tell(pb);
660     //FIXME: output timestamp, frame_nb pairs here.
661     if (finish_file(s, WTV_TABLE_0_ENTRIES_TIME, start_pos) < 0)
662         return -1;
663
664     // write root table
665     sector_pos = avio_tell(pb);
666     root_size = write_root_table(s, sector_pos);
667
668     file_end_pos = avio_tell(pb);
669     // update root value
670     avio_seek(pb, 0x30, SEEK_SET);
671     avio_wl32(pb, root_size);
672     avio_seek(pb, 4, SEEK_CUR);
673     avio_wl32(pb, sector_pos >> WTV_SECTOR_BITS);
674     avio_seek(pb, 0x5c, SEEK_SET);
675     avio_wl32(pb, file_end_pos >> WTV_SECTOR_BITS);
676
677     avio_flush(pb);
678     return 0;
679 }
680
681 AVOutputFormat ff_wtv_muxer = {
682     .name           = "wtv",
683     .long_name      = NULL_IF_CONFIG_SMALL("Windows Television (WTV)"),
684     .extensions     = "wtv",
685     .priv_data_size = sizeof(WtvContext),
686     .audio_codec    = AV_CODEC_ID_AC3,
687     .video_codec    = AV_CODEC_ID_MPEG2VIDEO,
688     .write_header   = write_header,
689     .write_packet   = write_packet,
690     .write_trailer  = write_trailer,
691     .codec_tag      = (const AVCodecTag* const []){ ff_codec_bmp_tags,
692                                                     ff_codec_wav_tags, 0 },
693 };