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