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
22 #include "libavutil/intreadwrite.h"
23 #include "libavutil/avassert.h"
28 #define WTV_BIGSECTOR_SIZE (1 << WTV_BIGSECTOR_BITS)
29 #define INDEX_BASE 0x2
30 #define MAX_NB_INDEX 10
32 /* declare utf16le strings */
34 static const uint8_t timeline_table_0_header_events[] =
35 {'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};
36 static const uint8_t table_0_header_legacy_attrib[] =
37 {'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};
38 static const uint8_t table_0_redirector_legacy_attrib[] =
39 {'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};
40 static const uint8_t table_0_header_time[] =
41 {'t'_'a'_'b'_'l'_'e'_'.'_'0'_'.'_'h'_'e'_'a'_'d'_'e'_'r'_'.'_'t'_'i'_'m'_'e', 0};
42 static const uint8_t legacy_attrib[] =
43 {'l'_'e'_'g'_'a'_'c'_'y'_'_'_'a'_'t'_'t'_'r'_'i'_'b', 0};
46 static const ff_asf_guid sub_wtv_guid =
47 {0x8C,0xC3,0xD2,0xC2,0x7E,0x9A,0xDA,0x11,0x8B,0xF7,0x00,0x07,0xE9,0x5E,0xAD,0x8D};
48 static const ff_asf_guid stream1_guid =
49 {0xA1,0xC3,0xD2,0xC2,0x7E,0x9A,0xDA,0x11,0x8B,0xF7,0x00,0x07,0xE9,0x5E,0xAD,0x8D};
50 static const ff_asf_guid sync_guid =
51 {0x97,0xC3,0xD2,0xC2,0x7E,0x9A,0xDA,0x11,0x8B,0xF7,0x00,0x07,0xE9,0x5E,0xAD,0x8D};
52 static const ff_asf_guid index_guid =
53 {0x96,0xc3,0xd2,0xc2,0x7e,0x9a,0xda,0x11,0x8b,0xf7,0x00,0x07,0xe9,0x5e,0xad,0x8d};
56 WTV_TIMELINE_TABLE_0_HEADER_EVENTS = 0,
57 WTV_TIMELINE_TABLE_0_ENTRIES_EVENTS,
59 WTV_TABLE_0_HEADER_LEGACY_ATTRIB,
60 WTV_TABLE_0_ENTRIES_LEGACY_ATTRIB,
61 WTV_TABLE_0_REDIRECTOR_LEGACY_ATTRIB,
62 WTV_TABLE_0_HEADER_TIME,
63 WTV_TABLE_0_ENTRIES_TIME,
77 const ff_asf_guid * guid;
82 int64_t timeline_start_pos;
83 WtvFile file[WTV_FILES];
84 int64_t serial; /** chunk serial number */
85 int64_t last_chunk_pos; /** last chunk position */
88 WtvChunkEntry index[MAX_NB_INDEX];
94 typedef int WTVHeaderWriteFunc(AVIOContext *pb);
97 const uint8_t *header;
99 WTVHeaderWriteFunc *write_header;
102 static int write_pad(AVIOContext *pb, int size)
104 for (; size > 0; size--)
109 static void put_guid(AVIOContext *s, const ff_asf_guid *g)
111 assert(sizeof(*g) == 16);
112 avio_write(s, *g, sizeof(*g));
115 static const ff_asf_guid *get_codec_guid(enum CodecID id, const AVCodecGuid *av_guid)
118 for (i = 0; av_guid[i].id != CODEC_ID_NONE; i++) {
119 if (id == av_guid[i].id)
120 return &(av_guid[i].guid);
126 * Write chunk header. If header chunk (0x80000000 set) then add to list of header chunks
128 static void write_chunk_header(AVFormatContext *s, const ff_asf_guid *guid, int length, int stream_id)
130 WtvContext *wctx = s->priv_data;
131 AVIOContext *pb = s->pb;
133 wctx->last_chunk_pos = avio_tell(pb) - wctx->timeline_start_pos;
135 avio_wl32(pb, 32 + length);
136 avio_wl32(pb, stream_id);
137 avio_wl64(pb, wctx->serial);
139 if ((stream_id & 0x80000000) && guid != &index_guid) {
140 WtvChunkEntry *t = wctx->index + wctx->nb_index;
141 av_assert0(wctx->nb_index < MAX_NB_INDEX);
142 t->pos = wctx->last_chunk_pos;
143 t->serial = wctx->serial;
145 t->stream_id = stream_id & 0x3FFFFFFF;
150 static void write_chunk_header2(AVFormatContext *s, const ff_asf_guid *guid, int stream_id)
152 WtvContext *wctx = s->priv_data;
153 AVIOContext *pb = s->pb;
155 int64_t last_chunk_pos = wctx->last_chunk_pos;
156 write_chunk_header(s, guid, 0, stream_id); // length updated later
157 avio_wl64(pb, last_chunk_pos);
160 static void finish_chunk_noindex(AVFormatContext *s)
162 WtvContext *wctx = s->priv_data;
163 AVIOContext *pb = s->pb;
165 // update the chunk_len field and pad.
166 int64_t chunk_len = avio_tell(pb) - (wctx->last_chunk_pos + wctx->timeline_start_pos);
167 avio_seek(pb, -(chunk_len - 16), SEEK_CUR);
168 avio_wl32(pb, chunk_len);
169 avio_seek(pb, chunk_len - (16 + 4), SEEK_CUR);
171 write_pad(pb, WTV_PAD8(chunk_len) - chunk_len);
175 static void write_index(AVFormatContext *s)
177 AVIOContext *pb = s->pb;
178 WtvContext *wctx = s->priv_data;
181 write_chunk_header2(s, &index_guid, 0x80000000);
185 for (i = 0; i < wctx->nb_index; i++) {
186 WtvChunkEntry *t = wctx->index + i;
187 put_guid(pb, t->guid);
188 avio_wl64(pb, t->pos);
189 avio_wl32(pb, t->stream_id);
190 avio_wl32(pb, 0); // checksum?
191 avio_wl64(pb, t->serial);
193 wctx->nb_index = 0; // reset index
194 finish_chunk_noindex(s);
197 static void finish_chunk(AVFormatContext *s)
199 WtvContext *wctx = s->priv_data;
200 finish_chunk_noindex(s);
201 if (wctx->nb_index == MAX_NB_INDEX)
205 static int write_stream_codec_info(AVFormatContext *s, AVStream *st)
207 WtvContext *wctx = s->priv_data;
208 const ff_asf_guid *g, *media_type, *format_type;
209 AVIOContext *pb = s->pb;
210 int64_t hdr_pos_start;
213 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
214 g = get_codec_guid(st->codec->codec_id, ff_video_guids);
215 media_type = &ff_mediatype_video;
216 format_type = &ff_format_mpeg2_video;
217 } else if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
218 g = get_codec_guid(st->codec->codec_id, ff_codec_wav_guids);
219 media_type = &ff_mediatype_audio;
220 format_type = &ff_format_waveformatex;
222 av_log(s, AV_LOG_ERROR, "unknown codec_type (0x%x)\n", st->codec->codec_type);
227 av_log(s, AV_LOG_ERROR, "can't get video codec_id (0x%x) guid.\n", st->codec->codec_id);
231 put_guid(pb, media_type); // mediatype
232 put_guid(pb, &ff_mediasubtype_cpfilters_processed); // subtype
234 put_guid(pb,&ff_format_cpfilters_processed); // format type
235 avio_wl32(pb, 0); // size
237 hdr_pos_start = avio_tell(pb);
238 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
239 if (wctx->first_video_flag) {
240 write_pad(pb, 216); //The size is sensitive.
241 wctx->first_video_flag = 0;
243 write_pad(pb, 72); // aspect ratio
244 ff_put_bmp_header(pb, st->codec, ff_codec_bmp_tags, 0);
247 ff_put_wav_header(pb, st->codec);
249 hdr_size = avio_tell(pb) - hdr_pos_start;
251 // seek back write hdr_size
252 avio_seek(pb, -(hdr_size + 4), SEEK_CUR);
253 avio_wl32(pb, hdr_size + 32);
254 avio_seek(pb, hdr_size, SEEK_CUR);
255 put_guid(pb, g); // actual_subtype
256 put_guid(pb, format_type); // actual_formattype
261 static int write_stream_codec(AVFormatContext *s, AVStream * st)
263 AVIOContext *pb = s->pb;
265 write_chunk_header2(s, &stream1_guid, 0x80000000 | 0x01);
271 ret = write_stream_codec_info(s, st);
273 av_log(s, AV_LOG_ERROR, "write stream codec info failed codec_type(0x%x)\n", st->codec->codec_type);
281 static void write_sync(AVFormatContext *s)
283 AVIOContext *pb = s->pb;
284 WtvContext *wctx = s->priv_data;
285 int64_t last_chunk_pos = wctx->last_chunk_pos;
286 wctx->sync_pos = avio_tell(pb) - wctx->timeline_start_pos;;
288 write_chunk_header(s, &sync_guid, 0x18, 0);
293 wctx->last_chunk_pos = last_chunk_pos;
296 static void write_DSATTRIB_TRANSPORT_PROPERTIES_init(AVFormatContext *s, int stream_index)
298 AVIOContext *pb = s->pb;
299 write_chunk_header2(s, &ff_DSATTRIB_TRANSPORT_PROPERTIES, 0x80000000 | stream_index);
300 avio_wl64(pb, stream_index);
306 static int write_stream_data(AVFormatContext *s, AVStream *st, int flag)
308 AVIOContext *pb = s->pb;
312 write_chunk_header2(s, &ff_stream_guid, 0x80000000 | (st->index + INDEX_BASE));
313 avio_wl32(pb, 0x00000001);
314 avio_wl32(pb, st->index + INDEX_BASE); //stream_id
315 avio_wl32(pb, 0x00000001);
318 write_chunk_header2(s, &ff_stream2_guid, 0x80000000 | (st->index + INDEX_BASE));
322 ret = write_stream_codec_info(s, st);
324 av_log(s, AV_LOG_ERROR, "write stream codec info failed codec_type(0x%x)\n", st->codec->codec_type);
329 av_set_pts_info(st, 64, 1, 10000000);
334 static int write_header(AVFormatContext *s)
336 AVIOContext *pb = s->pb;
337 WtvContext *wctx = s->priv_data;
341 put_guid(pb, &ff_wtv_guid);
342 put_guid(pb, &sub_wtv_guid);
346 avio_wl32(pb, 1 << WTV_SECTOR_BITS);
347 avio_wl32(pb, 1 << WTV_BIGSECTOR_BITS);
349 //write initial root fields
350 avio_wl32(pb, 0); // root_size, update later
352 avio_wl32(pb, 0); // root_sector, update it later.
355 avio_wl32(pb, 0); // file ends pointer, update it later.
357 pad = (1 << WTV_SECTOR_BITS) - avio_tell(pb);
359 wctx->timeline_start_pos = avio_tell(pb);
362 wctx->last_chunk_pos = -1;
363 wctx->first_video_flag = 1;
365 for (i = 0; i < s->nb_streams; i++) {
367 ret = write_stream_codec(s, st);
369 av_log(s, AV_LOG_ERROR, "write stream codec failed codec_type(0x%x)\n", st->codec->codec_type);
372 if (i + 1 < s->nb_streams) {
377 for (i = 0; i < s->nb_streams; i++) {
379 ret = write_stream_data(s, st, 0);
381 av_log(s, AV_LOG_ERROR, "write stream data failed codec_type(0x%x)\n", st->codec->codec_type);
384 ret = write_stream_data(s, st, 1);
386 av_log(s, AV_LOG_ERROR, "write stream2 data failed codec_type(0x%x)\n", st->codec->codec_type);
391 for (i = 0; i < s->nb_streams; i++)
392 write_DSATTRIB_TRANSPORT_PROPERTIES_init(s, INDEX_BASE + i);
400 static void write_timestamp(AVFormatContext *s, AVPacket *pkt)
402 AVIOContext *pb = s->pb;
403 WtvContext *wctx = s->priv_data;
404 AVCodecContext *enc = s->streams[pkt->stream_index]->codec;
406 int64_t frame_number = 0;
408 if (enc->codec_type == AVMEDIA_TYPE_VIDEO) {
410 frame_number = wctx->frame_nb;
411 flag = pkt->flags & AV_PKT_FLAG_KEY ? 1 : 0;
413 write_chunk_header(s, &ff_timestamp_guid, 56, 0x40000000 | (INDEX_BASE + pkt->stream_index));
415 avio_wl64(pb, pkt->pts == AV_NOPTS_VALUE ? -1 : pkt->pts);
416 avio_wl64(pb, pkt->pts == AV_NOPTS_VALUE ? -1 : pkt->pts);
418 avio_wl64(pb, frame_number);
424 static int write_packet(AVFormatContext *s, AVPacket *pkt)
426 AVIOContext *pb = s->pb;
427 WtvContext *wctx = s->priv_data;
429 // write timestamp chunk
430 write_timestamp(s, pkt);
432 write_chunk_header(s, &ff_data_guid, pkt->size, INDEX_BASE + pkt->stream_index);
433 avio_write(pb, pkt->data, pkt->size);
434 write_pad(pb, WTV_PAD8(pkt->size) - pkt->size);
441 static int write_table0_header_envents(AVIOContext *pb)
449 static int write_table0_header_legacy_attrib(AVIOContext *pb)
452 avio_wl32(pb, 0xFFFFFFFF);
454 avio_write(pb, legacy_attrib, sizeof(legacy_attrib));
455 pad = WTV_PAD8(sizeof(legacy_attrib)) - sizeof(legacy_attrib);
458 return 48 + WTV_PAD8(sizeof(legacy_attrib));
461 static int write_table0_header_time(AVIOContext *pb)
469 static const WTVRootEntryTable wtv_root_entry_table[] = {
470 { timeline_table_0_header_events, sizeof(timeline_table_0_header_events), write_table0_header_envents},
471 { ff_timeline_table_0_entries_Events_le16, sizeof(ff_timeline_table_0_entries_Events_le16), NULL},
472 { ff_timeline_le16, sizeof(ff_timeline_le16), NULL},
473 { table_0_header_legacy_attrib, sizeof(table_0_header_legacy_attrib), write_table0_header_legacy_attrib},
474 { ff_table_0_entries_legacy_attrib_le16, sizeof(ff_table_0_entries_legacy_attrib_le16), NULL},
475 { table_0_redirector_legacy_attrib, sizeof(table_0_redirector_legacy_attrib), NULL},
476 { table_0_header_time, sizeof(table_0_header_time), write_table0_header_time},
477 { ff_table_0_entries_time_le16, sizeof(ff_table_0_entries_time_le16), NULL},
480 static int write_root_table(AVFormatContext *s, int64_t sector_pos)
482 AVIOContext *pb = s->pb;
483 WtvContext *wctx = s->priv_data;
487 const WTVRootEntryTable *h = wtv_root_entry_table;
488 for (i = 0; i < sizeof(wtv_root_entry_table)/sizeof(WTVRootEntryTable); i++, h++) {
489 WtvFile *w = &wctx->file[i];
490 int filename_padding = WTV_PAD8(h->header_size) - h->header_size;
491 WTVHeaderWriteFunc *write = h->write_header;
495 put_guid(pb, &ff_dir_entry_guid);
496 len_pos = avio_tell(pb);
497 avio_wl16(pb, 40 + h->header_size + filename_padding + 8); // maybe updated later
499 avio_wl64(pb, write ? 0 : w->length);// maybe update later
500 avio_wl32(pb, (h->header_size + filename_padding) >> 1);
503 avio_write(pb, h->header, h->header_size);
504 write_pad(pb, filename_padding);
508 // update length field
509 avio_seek(pb, len_pos, SEEK_SET);
510 avio_wl64(pb, 40 + h->header_size + filename_padding + len);
511 avio_wl64(pb, len |(1ULL<<62) | (1ULL<<60));
512 avio_seek(pb, 8 + h->header_size + filename_padding + len, SEEK_CUR);
514 avio_wl32(pb, w->first_sector);
515 avio_wl32(pb, w->depth);
519 // caculate root table size
520 size = avio_tell(pb) - sector_pos;
521 pad = WTV_SECTOR_SIZE- size;
527 static void write_fat(AVIOContext *pb, int start_sector, int nb_sectors, int shift)
530 for (i = 0; i < nb_sectors; i++) {
531 avio_wl32(pb, start_sector + (i << shift));
533 // pad left sector pointer size
534 write_pad(pb, WTV_SECTOR_SIZE - (nb_sectors << 2));
537 static int write_fat_sector(AVFormatContext *s, int64_t start_pos, int nb_sectors, int sector_bits, int depth)
539 int64_t start_sector = start_pos >> WTV_SECTOR_BITS;
540 int shift = sector_bits - WTV_SECTOR_BITS;
542 int64_t fat = avio_tell(s->pb);
543 write_fat(s->pb, start_sector, nb_sectors, shift);
546 int64_t start_sector1 = fat >> WTV_SECTOR_BITS;
547 int nb_sectors1 = ((nb_sectors << 2) + WTV_SECTOR_SIZE - 1) / WTV_SECTOR_SIZE;
548 int64_t fat1 = avio_tell(s->pb);
550 write_fat(s->pb, start_sector1, nb_sectors1, 0);
557 static void write_table_entries_events(AVFormatContext *s)
559 AVIOContext *pb = s->pb;
560 WtvContext *wctx = s->priv_data;
562 //FIXME: output frame_nb, position pairs.
563 //We only set the first sync_chunk position here.
564 avio_wl64(pb, 0x2); avio_wl64(pb, wctx->sync_pos);
567 static void write_tag(AVIOContext *pb, const char *key, const char *value)
569 put_guid(pb, &ff_metadata_guid);
571 avio_wl32(pb, strlen(value)*2 + 2);
572 avio_put_str16le(pb, key);
573 avio_put_str16le(pb, value);
576 static void write_table_entries_attrib(AVFormatContext *s)
578 AVDictionaryEntry *tag = 0;
580 //FIXME: translate special tags (e.g. WM/Bitrate) to binary representation
581 ff_metadata_conv(&s->metadata, ff_asf_metadata_conv, NULL);
582 while((tag = av_dict_get(s->metadata, "", tag, AV_DICT_IGNORE_SUFFIX)))
583 write_tag(s->pb, tag->key, tag->value);
586 static void write_table_redirector_legacy_attrib(AVFormatContext *s)
588 AVIOContext *pb = s->pb;
589 AVDictionaryEntry *tag = 0;
592 //FIXME: translate special tags to binary representation
593 while((tag = av_dict_get(s->metadata, "", tag, AV_DICT_IGNORE_SUFFIX))) {
595 pos += 16 + 4 + 4 + strlen(tag->key)*2 + 2 + strlen(tag->value)*2 + 2;
600 * Pad the remainder of a file
601 * Write out fat table
602 * @return <0 on error
604 static int finish_file(AVFormatContext *s, enum WtvFileIndex index, int64_t start_pos)
606 WtvContext *wctx = s->priv_data;
607 AVIOContext *pb = s->pb;
608 WtvFile *w = &wctx->file[index];
609 int64_t end_pos = avio_tell(pb);
610 int sector_bits, nb_sectors, pad;
612 av_assert0(index < WTV_FILES);
614 w->length = (end_pos - start_pos);
616 // determine optimal fat table depth, sector_bits, nb_sectors
617 if (w->length <= WTV_SECTOR_SIZE) {
619 sector_bits = WTV_SECTOR_BITS;
620 } else if (w->length <= (WTV_SECTOR_SIZE / 4) * WTV_SECTOR_SIZE) {
622 sector_bits = WTV_SECTOR_BITS;
623 } else if (w->length <= (WTV_SECTOR_SIZE / 4) * WTV_BIGSECTOR_SIZE) {
625 sector_bits = WTV_BIGSECTOR_BITS;
626 } else if (w->length <= (int64_t)(WTV_SECTOR_SIZE / 4) * (WTV_SECTOR_SIZE / 4) * WTV_SECTOR_SIZE) {
628 sector_bits = WTV_SECTOR_BITS;
629 } else if (w->length <= (int64_t)(WTV_SECTOR_SIZE / 4) * (WTV_SECTOR_SIZE / 4) * WTV_BIGSECTOR_SIZE) {
631 sector_bits = WTV_BIGSECTOR_BITS;
633 av_log(s, AV_LOG_ERROR, "unsupported file allocation table depth (%"PRIi64" bytes)\n", w->length);
637 // determine the nb_sectors
638 nb_sectors = (int)(w->length >> sector_bits);
640 // pad sector of timeline
641 pad = (1 << sector_bits) - (w->length % (1 << sector_bits));
649 w->first_sector = write_fat_sector(s, start_pos, nb_sectors, sector_bits, w->depth);
651 w->first_sector = start_pos;
653 w->first_sector >>= WTV_SECTOR_BITS;
655 w->length |= 1ULL<<60;
656 if (sector_bits == WTV_SECTOR_BITS)
657 w->length |= 1ULL<<63;
662 static int write_trailer(AVFormatContext *s)
664 WtvContext *wctx = s->priv_data;
665 AVIOContext *pb = s->pb;
668 int64_t start_pos, file_end_pos;
670 if (finish_file(s, WTV_TIMELINE, wctx->timeline_start_pos) < 0)
673 start_pos = avio_tell(pb);
674 write_table_entries_events(s);
675 if (finish_file(s, WTV_TIMELINE_TABLE_0_ENTRIES_EVENTS, start_pos) < 0)
678 start_pos = avio_tell(pb);
679 write_table_entries_attrib(s);
680 if (finish_file(s, WTV_TABLE_0_ENTRIES_LEGACY_ATTRIB, start_pos) < 0)
683 start_pos = avio_tell(pb);
684 write_table_redirector_legacy_attrib(s);
685 if (finish_file(s, WTV_TABLE_0_REDIRECTOR_LEGACY_ATTRIB, start_pos) < 0)
688 start_pos = avio_tell(pb);
689 //FIXME: output timestamp, frame_nb pairs here.
690 if (finish_file(s, WTV_TABLE_0_ENTRIES_TIME, start_pos) < 0)
694 sector_pos = avio_tell(pb);
695 root_size = write_root_table(s, sector_pos);
697 file_end_pos = avio_tell(pb);
699 avio_seek(pb, 0x30, SEEK_SET);
700 avio_wl32(pb, root_size);
701 avio_seek(pb, 4, SEEK_CUR);
702 avio_wl32(pb, sector_pos >> WTV_SECTOR_BITS);
703 avio_seek(pb, 0x5c, SEEK_SET);
704 avio_wl32(pb, file_end_pos >> WTV_SECTOR_BITS);
710 AVOutputFormat ff_wtv_muxer = {
712 NULL_IF_CONFIG_SMALL("Windows Television (WTV)"),
721 .codec_tag= (const AVCodecTag* const []){ff_codec_bmp_tags, ff_codec_wav_tags, 0},