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.
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.
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.
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
24 * Windows Television (WTV) demuxer
25 * @author Zhentan Feng <spyfeng at gmail dot com>
28 #include "libavutil/intreadwrite.h"
29 #include "libavutil/avassert.h"
35 #define WTV_BIGSECTOR_SIZE (1 << WTV_BIGSECTOR_BITS)
36 #define INDEX_BASE 0x2
37 #define MAX_NB_INDEX 10
39 /* declare utf16le strings */
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};
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};
57 WTV_TIMELINE_TABLE_0_HEADER_EVENTS = 0,
58 WTV_TIMELINE_TABLE_0_ENTRIES_EVENTS,
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,
78 const ff_asf_guid * guid;
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 */
89 WtvChunkEntry index[MAX_NB_INDEX];
95 typedef int WTVHeaderWriteFunc(AVIOContext *pb);
98 const uint8_t *header;
100 WTVHeaderWriteFunc *write_header;
103 static int write_pad(AVIOContext *pb, int size)
105 for (; size > 0; size--)
110 static const ff_asf_guid *get_codec_guid(enum AVCodecID id, const AVCodecGuid *av_guid)
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);
121 * Write chunk header. If header chunk (0x80000000 set) then add to list of header chunks
123 static void write_chunk_header(AVFormatContext *s, const ff_asf_guid *guid, int length, int stream_id)
125 WtvContext *wctx = s->priv_data;
126 AVIOContext *pb = s->pb;
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);
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;
140 t->stream_id = stream_id & 0x3FFFFFFF;
145 static void write_chunk_header2(AVFormatContext *s, const ff_asf_guid *guid, int stream_id)
147 WtvContext *wctx = s->priv_data;
148 AVIOContext *pb = s->pb;
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);
155 static void finish_chunk_noindex(AVFormatContext *s)
157 WtvContext *wctx = s->priv_data;
158 AVIOContext *pb = s->pb;
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);
166 write_pad(pb, WTV_PAD8(chunk_len) - chunk_len);
170 static void write_index(AVFormatContext *s)
172 AVIOContext *pb = s->pb;
173 WtvContext *wctx = s->priv_data;
176 write_chunk_header2(s, &ff_index_guid, 0x80000000);
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);
188 wctx->nb_index = 0; // reset index
189 finish_chunk_noindex(s);
192 static void finish_chunk(AVFormatContext *s)
194 WtvContext *wctx = s->priv_data;
195 finish_chunk_noindex(s);
196 if (wctx->nb_index == MAX_NB_INDEX)
200 static int write_stream_codec_info(AVFormatContext *s, AVStream *st)
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;
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;
217 av_log(s, AV_LOG_ERROR, "unknown codec_type (0x%x)\n", st->codec->codec_type);
222 av_log(s, AV_LOG_ERROR, "can't get video codec_id (0x%x) guid.\n", st->codec->codec_id);
226 ff_put_guid(pb, media_type); // mediatype
227 ff_put_guid(pb, &ff_mediasubtype_cpfilters_processed); // subtype
229 ff_put_guid(pb,&ff_format_cpfilters_processed); // format type
230 avio_wl32(pb, 0); // size
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;
238 write_pad(pb, 72); // aspect ratio
239 ff_put_bmp_header(pb, st->codec, ff_codec_bmp_tags, 0);
242 ff_put_wav_header(pb, st->codec);
244 hdr_size = avio_tell(pb) - hdr_pos_start;
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
256 static int write_stream_codec(AVFormatContext *s, AVStream * st)
258 AVIOContext *pb = s->pb;
260 write_chunk_header2(s, &ff_stream1_guid, 0x80000000 | 0x01);
266 ret = write_stream_codec_info(s, st);
268 av_log(s, AV_LOG_ERROR, "write stream codec info failed codec_type(0x%x)\n", st->codec->codec_type);
276 static void write_sync(AVFormatContext *s)
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;
283 write_chunk_header(s, &ff_sync_guid, 0x18, 0);
288 wctx->last_chunk_pos = last_chunk_pos;
291 static int write_stream_data(AVFormatContext *s, AVStream *st)
293 AVIOContext *pb = s->pb;
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);
302 ret = write_stream_codec_info(s, st);
304 av_log(s, AV_LOG_ERROR, "write stream codec info failed codec_type(0x%x)\n", st->codec->codec_type);
309 avpriv_set_pts_info(st, 64, 1, 10000000);
314 static int write_header(AVFormatContext *s)
316 AVIOContext *pb = s->pb;
317 WtvContext *wctx = s->priv_data;
321 ff_put_guid(pb, &ff_wtv_guid);
322 ff_put_guid(pb, &sub_wtv_guid);
326 avio_wl32(pb, 1 << WTV_SECTOR_BITS);
327 avio_wl32(pb, 1 << WTV_BIGSECTOR_BITS);
329 //write initial root fields
330 avio_wl32(pb, 0); // root_size, update later
332 avio_wl32(pb, 0); // root_sector, update it later.
335 avio_wl32(pb, 0); // file ends pointer, update it later.
337 pad = (1 << WTV_SECTOR_BITS) - avio_tell(pb);
339 wctx->timeline_start_pos = avio_tell(pb);
342 wctx->last_chunk_pos = -1;
343 wctx->first_video_flag = 1;
345 for (i = 0; i < s->nb_streams; i++) {
347 ret = write_stream_codec(s, st);
349 av_log(s, AV_LOG_ERROR, "write stream codec failed codec_type(0x%x)\n", st->codec->codec_type);
356 for (i = 0; i < s->nb_streams; i++) {
358 ret = write_stream_data(s, st);
360 av_log(s, AV_LOG_ERROR, "write stream data failed codec_type(0x%x)\n", st->codec->codec_type);
371 static void write_timestamp(AVFormatContext *s, AVPacket *pkt)
373 AVIOContext *pb = s->pb;
374 WtvContext *wctx = s->priv_data;
375 AVCodecContext *enc = s->streams[pkt->stream_index]->codec;
377 int64_t frame_number = 0;
379 if (enc->codec_type == AVMEDIA_TYPE_VIDEO) {
381 frame_number = wctx->frame_nb;
382 flag = pkt->flags & AV_PKT_FLAG_KEY ? 1 : 0;
384 write_chunk_header(s, &ff_timestamp_guid, 56, 0x40000000 | (INDEX_BASE + pkt->stream_index));
386 avio_wl64(pb, pkt->pts == AV_NOPTS_VALUE ? -1 : pkt->pts);
387 avio_wl64(pb, pkt->pts == AV_NOPTS_VALUE ? -1 : pkt->pts);
389 avio_wl64(pb, frame_number);
395 static int write_packet(AVFormatContext *s, AVPacket *pkt)
397 AVIOContext *pb = s->pb;
398 WtvContext *wctx = s->priv_data;
400 // write timestamp chunk
401 write_timestamp(s, pkt);
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);
412 static int write_table0_header_events(AVIOContext *pb)
420 static int write_table0_header_legacy_attrib(AVIOContext *pb)
423 avio_wl32(pb, 0xFFFFFFFF);
425 avio_write(pb, legacy_attrib, sizeof(legacy_attrib));
426 pad = WTV_PAD8(sizeof(legacy_attrib)) - sizeof(legacy_attrib);
429 return 48 + WTV_PAD8(sizeof(legacy_attrib));
432 static int write_table0_header_time(AVIOContext *pb)
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},
451 static int write_root_table(AVFormatContext *s, int64_t sector_pos)
453 AVIOContext *pb = s->pb;
454 WtvContext *wctx = s->priv_data;
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;
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
470 avio_wl64(pb, write ? 0 : w->length);// maybe update later
471 avio_wl32(pb, (h->header_size + filename_padding) >> 1);
474 avio_write(pb, h->header, h->header_size);
475 write_pad(pb, filename_padding);
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);
485 avio_wl32(pb, w->first_sector);
486 avio_wl32(pb, w->depth);
490 // caculate root table size
491 size = avio_tell(pb) - sector_pos;
492 pad = WTV_SECTOR_SIZE- size;
498 static void write_fat(AVIOContext *pb, int start_sector, int nb_sectors, int shift)
501 for (i = 0; i < nb_sectors; i++) {
502 avio_wl32(pb, start_sector + (i << shift));
504 // pad left sector pointer size
505 write_pad(pb, WTV_SECTOR_SIZE - ((nb_sectors << 2) % WTV_SECTOR_SIZE));
508 static int write_fat_sector(AVFormatContext *s, int64_t start_pos, int nb_sectors, int sector_bits, int depth)
510 int64_t start_sector = start_pos >> WTV_SECTOR_BITS;
511 int shift = sector_bits - WTV_SECTOR_BITS;
513 int64_t fat = avio_tell(s->pb);
514 write_fat(s->pb, start_sector, nb_sectors, shift);
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);
521 write_fat(s->pb, start_sector1, nb_sectors1, 0);
528 static void write_table_entries_events(AVFormatContext *s)
530 AVIOContext *pb = s->pb;
531 WtvContext *wctx = s->priv_data;
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);
538 static void write_tag(AVIOContext *pb, const char *key, const char *value)
540 ff_put_guid(pb, &ff_metadata_guid);
542 avio_wl32(pb, strlen(value)*2 + 2);
543 avio_put_str16le(pb, key);
544 avio_put_str16le(pb, value);
547 static void write_table_entries_attrib(AVFormatContext *s)
549 AVDictionaryEntry *tag = 0;
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);
557 static void write_table_redirector_legacy_attrib(AVFormatContext *s)
559 AVIOContext *pb = s->pb;
560 AVDictionaryEntry *tag = 0;
563 //FIXME: translate special tags to binary representation
564 while ((tag = av_dict_get(s->metadata, "", tag, AV_DICT_IGNORE_SUFFIX))) {
566 pos += 16 + 4 + 4 + strlen(tag->key)*2 + 2 + strlen(tag->value)*2 + 2;
571 * Pad the remainder of a file
572 * Write out fat table
573 * @return <0 on error
575 static int finish_file(AVFormatContext *s, enum WtvFileIndex index, int64_t start_pos)
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;
583 av_assert0(index < WTV_FILES);
585 w->length = (end_pos - start_pos);
587 // determine optimal fat table depth, sector_bits, nb_sectors
588 if (w->length <= WTV_SECTOR_SIZE) {
590 sector_bits = WTV_SECTOR_BITS;
591 } else if (w->length <= (WTV_SECTOR_SIZE / 4) * WTV_SECTOR_SIZE) {
593 sector_bits = WTV_SECTOR_BITS;
594 } else if (w->length <= (WTV_SECTOR_SIZE / 4) * WTV_BIGSECTOR_SIZE) {
596 sector_bits = WTV_BIGSECTOR_BITS;
597 } else if (w->length <= (int64_t)(WTV_SECTOR_SIZE / 4) * (WTV_SECTOR_SIZE / 4) * WTV_SECTOR_SIZE) {
599 sector_bits = WTV_SECTOR_BITS;
600 } else if (w->length <= (int64_t)(WTV_SECTOR_SIZE / 4) * (WTV_SECTOR_SIZE / 4) * WTV_BIGSECTOR_SIZE) {
602 sector_bits = WTV_BIGSECTOR_BITS;
604 av_log(s, AV_LOG_ERROR, "unsupported file allocation table depth (%"PRIi64" bytes)\n", w->length);
608 // determine the nb_sectors
609 nb_sectors = (int)(w->length >> sector_bits);
611 // pad sector of timeline
612 pad = (1 << sector_bits) - (w->length % (1 << sector_bits));
620 w->first_sector = write_fat_sector(s, start_pos, nb_sectors, sector_bits, w->depth);
622 w->first_sector = start_pos;
624 w->first_sector >>= WTV_SECTOR_BITS;
626 w->length |= 1ULL<<60;
627 if (sector_bits == WTV_SECTOR_BITS)
628 w->length |= 1ULL<<63;
633 static int write_trailer(AVFormatContext *s)
635 WtvContext *wctx = s->priv_data;
636 AVIOContext *pb = s->pb;
639 int64_t start_pos, file_end_pos;
641 if (finish_file(s, WTV_TIMELINE, wctx->timeline_start_pos) < 0)
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)
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)
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)
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)
665 sector_pos = avio_tell(pb);
666 root_size = write_root_table(s, sector_pos);
668 file_end_pos = avio_tell(pb);
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);
681 AVOutputFormat ff_wtv_muxer = {
683 .long_name = NULL_IF_CONFIG_SMALL("Windows Television (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 },