FFmpeg
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
wtvenc.c
Go to the documentation of this file.
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,
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};
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};
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 
66 };
67 
68 typedef struct {
69  int64_t length;
70  const void *header;
71  int depth;
73 } WtvFile;
74 
75 typedef struct {
76  int64_t pos;
77  int64_t serial;
78  const ff_asf_guid * guid;
79  int stream_id;
81 
82 typedef struct {
83  int64_t serial;
84  int64_t value;
85 } WtvSyncEntry;
86 
87 typedef struct {
90  int64_t serial; /** chunk serial number */
91  int64_t last_chunk_pos; /** last chunk position */
92  int64_t last_timestamp_pos; /** last timestamp chunk position */
93  int64_t first_index_pos; /** first index_chunk position */
94 
96  int nb_index;
98 
99  WtvSyncEntry *st_pairs; /* (serial, timestamp) pairs */
101  WtvSyncEntry *sp_pairs; /* (serial, position) pairs */
103 
104  int64_t last_pts;
105  int64_t last_serial;
106 } WtvContext;
107 
108 
109 static void add_serial_pair(WtvSyncEntry ** list, int * count, int64_t serial, int64_t value)
110 {
111  int new_count = *count + 1;
112  WtvSyncEntry *new_list = av_realloc(*list, new_count * sizeof(WtvSyncEntry));
113  if (!new_list)
114  return;
115  new_list[*count] = (WtvSyncEntry){serial, value};
116  *list = new_list;
117  *count = new_count;
118 }
119 
121 
122 typedef struct {
123  const uint8_t *header;
127 
128 static int write_pad(AVIOContext *pb, int size)
129 {
130  for (; size > 0; size--)
131  avio_w8(pb, 0);
132  return 0;
133 }
134 
135 static const ff_asf_guid *get_codec_guid(enum AVCodecID id, const AVCodecGuid *av_guid)
136 {
137  int i;
138  for (i = 0; av_guid[i].id != AV_CODEC_ID_NONE; i++) {
139  if (id == av_guid[i].id)
140  return &(av_guid[i].guid);
141  }
142  return NULL;
143 }
144 
145 /**
146  * Write chunk header. If header chunk (0x80000000 set) then add to list of header chunks
147  */
148 static void write_chunk_header(AVFormatContext *s, const ff_asf_guid *guid, int length, int stream_id)
149 {
150  WtvContext *wctx = s->priv_data;
151  AVIOContext *pb = s->pb;
152 
153  wctx->last_chunk_pos = avio_tell(pb) - wctx->timeline_start_pos;
154  ff_put_guid(pb, guid);
155  avio_wl32(pb, 32 + length);
156  avio_wl32(pb, stream_id);
157  avio_wl64(pb, wctx->serial);
158 
159  if ((stream_id & 0x80000000) && guid != &ff_index_guid) {
160  WtvChunkEntry *t = wctx->index + wctx->nb_index;
162  t->pos = wctx->last_chunk_pos;
163  t->serial = wctx->serial;
164  t->guid = guid;
165  t->stream_id = stream_id & 0x3FFFFFFF;
166  wctx->nb_index++;
167  }
168 }
169 
170 static void write_chunk_header2(AVFormatContext *s, const ff_asf_guid *guid, int stream_id)
171 {
172  WtvContext *wctx = s->priv_data;
173  AVIOContext *pb = s->pb;
174 
175  int64_t last_chunk_pos = wctx->last_chunk_pos;
176  write_chunk_header(s, guid, 0, stream_id); // length updated later
177  avio_wl64(pb, last_chunk_pos);
178 }
179 
181 {
182  WtvContext *wctx = s->priv_data;
183  AVIOContext *pb = s->pb;
184 
185  // update the chunk_len field and pad.
186  int64_t chunk_len = avio_tell(pb) - (wctx->last_chunk_pos + wctx->timeline_start_pos);
187  avio_seek(pb, -(chunk_len - 16), SEEK_CUR);
188  avio_wl32(pb, chunk_len);
189  avio_seek(pb, chunk_len - (16 + 4), SEEK_CUR);
190 
191  write_pad(pb, WTV_PAD8(chunk_len) - chunk_len);
192  wctx->serial++;
193 }
194 
196 {
197  AVIOContext *pb = s->pb;
198  WtvContext *wctx = s->priv_data;
199  int i;
200 
201  write_chunk_header2(s, &ff_index_guid, 0x80000000);
202  avio_wl32(pb, 0);
203  avio_wl32(pb, 0);
204 
205  for (i = 0; i < wctx->nb_index; i++) {
206  WtvChunkEntry *t = wctx->index + i;
207  ff_put_guid(pb, t->guid);
208  avio_wl64(pb, t->pos);
209  avio_wl32(pb, t->stream_id);
210  avio_wl32(pb, 0); // checksum?
211  avio_wl64(pb, t->serial);
212  }
213  wctx->nb_index = 0; // reset index
215 
216  if (!wctx->first_index_pos)
217  wctx->first_index_pos = wctx->last_chunk_pos;
218 }
219 
221 {
222  WtvContext *wctx = s->priv_data;
224  if (wctx->nb_index == MAX_NB_INDEX)
225  write_index(s);
226 }
227 
229 {
230  WtvContext *wctx = s->priv_data;
231  const ff_asf_guid *g, *media_type, *format_type;
232  AVIOContext *pb = s->pb;
233  int64_t hdr_pos_start;
234  int hdr_size = 0;
235 
236  if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
238  media_type = &ff_mediatype_video;
239  format_type = &ff_format_mpeg2_video;
240  } else if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
242  media_type = &ff_mediatype_audio;
243  format_type = &ff_format_waveformatex;
244  } else {
245  av_log(s, AV_LOG_ERROR, "unknown codec_type (0x%x)\n", st->codec->codec_type);
246  return -1;
247  }
248 
249  if (g == NULL) {
250  av_log(s, AV_LOG_ERROR, "can't get video codec_id (0x%x) guid.\n", st->codec->codec_id);
251  return -1;
252  }
253 
254  ff_put_guid(pb, media_type); // mediatype
256  write_pad(pb, 12);
257  ff_put_guid(pb,&ff_format_cpfilters_processed); // format type
258  avio_wl32(pb, 0); // size
259 
260  hdr_pos_start = avio_tell(pb);
261  if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
262  if (wctx->first_video_flag) {
263  write_pad(pb, 216); //The size is sensitive.
264  wctx->first_video_flag = 0;
265  } else {
266  write_pad(pb, 72); // aspect ratio
268  }
269  } else {
270  ff_put_wav_header(pb, st->codec);
271  }
272  hdr_size = avio_tell(pb) - hdr_pos_start;
273 
274  // seek back write hdr_size
275  avio_seek(pb, -(hdr_size + 4), SEEK_CUR);
276  avio_wl32(pb, hdr_size + 32);
277  avio_seek(pb, hdr_size, SEEK_CUR);
278  ff_put_guid(pb, g); // actual_subtype
279  ff_put_guid(pb, format_type); // actual_formattype
280 
281  return 0;
282 }
283 
285 {
286  AVIOContext *pb = s->pb;
287  int ret;
288  write_chunk_header2(s, &ff_stream1_guid, 0x80000000 | 0x01);
289 
290  avio_wl32(pb, 0x01);
291  write_pad(pb, 4);
292  write_pad(pb, 4);
293 
294  ret = write_stream_codec_info(s, st);
295  if (ret < 0) {
296  av_log(s, AV_LOG_ERROR, "write stream codec info failed codec_type(0x%x)\n", st->codec->codec_type);
297  return -1;
298  }
299 
300  finish_chunk(s);
301  return 0;
302 }
303 
305 {
306  AVIOContext *pb = s->pb;
307  WtvContext *wctx = s->priv_data;
308  int64_t last_chunk_pos = wctx->last_chunk_pos;
309 
310  write_chunk_header(s, &ff_sync_guid, 0x18, 0);
311  avio_wl64(pb, wctx->first_index_pos);
312  avio_wl64(pb, wctx->last_timestamp_pos);
313  avio_wl64(pb, 0);
314 
315  finish_chunk(s);
316  add_serial_pair(&wctx->sp_pairs, &wctx->nb_sp_pairs, wctx->serial, wctx->last_chunk_pos);
317 
318  wctx->last_chunk_pos = last_chunk_pos;
319 }
320 
322 {
323  AVIOContext *pb = s->pb;
324  int ret;
325 
327  avio_wl32(pb, 0x00000001);
328  avio_wl32(pb, st->index + INDEX_BASE); //stream_id
329  avio_wl32(pb, 0x00000001);
330  write_pad(pb, 8);
331 
332  ret = write_stream_codec_info(s, st);
333  if (ret < 0) {
334  av_log(s, AV_LOG_ERROR, "write stream codec info failed codec_type(0x%x)\n", st->codec->codec_type);
335  return -1;
336  }
337  finish_chunk(s);
338 
339  avpriv_set_pts_info(st, 64, 1, 10000000);
340 
341  return 0;
342 }
343 
345 {
346  AVIOContext *pb = s->pb;
347  WtvContext *wctx = s->priv_data;
348  int i, pad, ret;
349  AVStream *st;
350 
351  wctx->last_chunk_pos = -1;
352  wctx->last_timestamp_pos = -1;
353 
354  ff_put_guid(pb, &ff_wtv_guid);
356 
357  avio_wl32(pb, 0x01);
358  avio_wl32(pb, 0x02);
359  avio_wl32(pb, 1 << WTV_SECTOR_BITS);
360  avio_wl32(pb, 1 << WTV_BIGSECTOR_BITS);
361 
362  //write initial root fields
363  avio_wl32(pb, 0); // root_size, update later
364  write_pad(pb, 4);
365  avio_wl32(pb, 0); // root_sector, update it later.
366 
367  write_pad(pb, 32);
368  avio_wl32(pb, 0); // file ends pointer, update it later.
369 
370  pad = (1 << WTV_SECTOR_BITS) - avio_tell(pb);
371  write_pad(pb, pad);
372 
373  wctx->timeline_start_pos = avio_tell(pb);
374 
375  wctx->serial = 1;
376  wctx->last_chunk_pos = -1;
377  wctx->first_video_flag = 1;
378 
379  for (i = 0; i < s->nb_streams; i++) {
380  st = s->streams[i];
381  ret = write_stream_codec(s, st);
382  if (ret < 0) {
383  av_log(s, AV_LOG_ERROR, "write stream codec failed codec_type(0x%x)\n", st->codec->codec_type);
384  return -1;
385  }
386  if (!i)
387  write_sync(s);
388  }
389 
390  for (i = 0; i < s->nb_streams; i++) {
391  st = s->streams[i];
392  ret = write_stream_data(s, st);
393  if (ret < 0) {
394  av_log(s, AV_LOG_ERROR, "write stream data failed codec_type(0x%x)\n", st->codec->codec_type);
395  return -1;
396  }
397  }
398 
399  if (wctx->nb_index)
400  write_index(s);
401 
402  return 0;
403 }
404 
406 {
407  AVIOContext *pb = s->pb;
408  WtvContext *wctx = s->priv_data;
409  AVCodecContext *enc = s->streams[pkt->stream_index]->codec;
410 
411  write_chunk_header(s, &ff_timestamp_guid, 56, 0x40000000 | (INDEX_BASE + pkt->stream_index));
412  write_pad(pb, 8);
413  avio_wl64(pb, pkt->pts == AV_NOPTS_VALUE ? -1 : pkt->pts);
414  avio_wl64(pb, pkt->pts == AV_NOPTS_VALUE ? -1 : pkt->pts);
415  avio_wl64(pb, pkt->pts == AV_NOPTS_VALUE ? -1 : pkt->pts);
416  avio_wl64(pb, 0);
417  avio_wl64(pb, enc->codec_type == AVMEDIA_TYPE_VIDEO && (pkt->flags & AV_PKT_FLAG_KEY) ? 1 : 0);
418  avio_wl64(pb, 0);
419 
420  wctx->last_timestamp_pos = wctx->last_chunk_pos;
421 }
422 
424 {
425  AVIOContext *pb = s->pb;
426  WtvContext *wctx = s->priv_data;
427 
428  /* emit sync chunk and 'timeline.table.0.entries.Event' record every 50 frames */
429  if (wctx->serial - (wctx->nb_sp_pairs ? wctx->sp_pairs[wctx->nb_sp_pairs - 1].serial : 0) >= 50)
430  write_sync(s);
431 
432  /* emit 'table.0.entries.time' record every 500ms */
433  if (pkt->pts != AV_NOPTS_VALUE && pkt->pts - (wctx->nb_st_pairs ? wctx->st_pairs[wctx->nb_st_pairs - 1].value : 0) >= 5000000)
434  add_serial_pair(&wctx->st_pairs, &wctx->nb_st_pairs, wctx->serial, pkt->pts);
435 
436  if (pkt->pts != AV_NOPTS_VALUE && pkt->pts > wctx->last_pts) {
437  wctx->last_pts = pkt->pts;
438  wctx->last_serial = wctx->serial;
439  }
440 
441  // write timestamp chunk
442  write_timestamp(s, pkt);
443 
445  avio_write(pb, pkt->data, pkt->size);
446  write_pad(pb, WTV_PAD8(pkt->size) - pkt->size);
447 
448  wctx->serial++;
449  avio_flush(pb);
450  return 0;
451 }
452 
454 {
455  avio_wl32(pb, 0x10);
456  write_pad(pb, 84);
457  avio_wl64(pb, 0x32);
458  return 96;
459 }
460 
462 {
463  int pad = 0;
464  avio_wl32(pb, 0xFFFFFFFF);
465  write_pad(pb, 12);
466  avio_write(pb, legacy_attrib, sizeof(legacy_attrib));
467  pad = WTV_PAD8(sizeof(legacy_attrib)) - sizeof(legacy_attrib);
468  write_pad(pb, pad);
469  write_pad(pb, 32);
470  return 48 + WTV_PAD8(sizeof(legacy_attrib));
471 }
472 
474 {
475  avio_wl32(pb, 0x10);
476  write_pad(pb, 76);
477  avio_wl64(pb, 0x40);
478  return 88;
479 }
480 
490 };
491 
492 static int write_root_table(AVFormatContext *s, int64_t sector_pos)
493 {
494  AVIOContext *pb = s->pb;
495  WtvContext *wctx = s->priv_data;
496  int size, pad;
497  int i;
498 
500  for (i = 0; i < sizeof(wtv_root_entry_table)/sizeof(WTVRootEntryTable); i++, h++) {
501  WtvFile *w = &wctx->file[i];
502  int filename_padding = WTV_PAD8(h->header_size) - h->header_size;
503  WTVHeaderWriteFunc *write = h->write_header;
504  int len = 0;
505  int64_t len_pos;
506 
508  len_pos = avio_tell(pb);
509  avio_wl16(pb, 40 + h->header_size + filename_padding + 8); // maybe updated later
510  write_pad(pb, 6);
511  avio_wl64(pb, write ? 0 : w->length);// maybe update later
512  avio_wl32(pb, (h->header_size + filename_padding) >> 1);
513  write_pad(pb, 4);
514 
515  avio_write(pb, h->header, h->header_size);
516  write_pad(pb, filename_padding);
517 
518  if (write) {
519  len = write(pb);
520  // update length field
521  avio_seek(pb, len_pos, SEEK_SET);
522  avio_wl64(pb, 40 + h->header_size + filename_padding + len);
523  avio_wl64(pb, len |(1ULL<<62) | (1ULL<<60));
524  avio_seek(pb, 8 + h->header_size + filename_padding + len, SEEK_CUR);
525  } else {
526  avio_wl32(pb, w->first_sector);
527  avio_wl32(pb, w->depth);
528  }
529  }
530 
531  // caculate root table size
532  size = avio_tell(pb) - sector_pos;
533  pad = WTV_SECTOR_SIZE- size;
534  write_pad(pb, pad);
535 
536  return size;
537 }
538 
539 static void write_fat(AVIOContext *pb, int start_sector, int nb_sectors, int shift)
540 {
541  int i;
542  for (i = 0; i < nb_sectors; i++) {
543  avio_wl32(pb, start_sector + (i << shift));
544  }
545  // pad left sector pointer size
546  write_pad(pb, WTV_SECTOR_SIZE - ((nb_sectors << 2) % WTV_SECTOR_SIZE));
547 }
548 
549 static int64_t write_fat_sector(AVFormatContext *s, int64_t start_pos, int nb_sectors, int sector_bits, int depth)
550 {
551  int64_t start_sector = start_pos >> WTV_SECTOR_BITS;
552  int shift = sector_bits - WTV_SECTOR_BITS;
553 
554  int64_t fat = avio_tell(s->pb);
555  write_fat(s->pb, start_sector, nb_sectors, shift);
556 
557  if (depth == 2) {
558  int64_t start_sector1 = fat >> WTV_SECTOR_BITS;
559  int nb_sectors1 = ((nb_sectors << 2) + WTV_SECTOR_SIZE - 1) / WTV_SECTOR_SIZE;
560  int64_t fat1 = avio_tell(s->pb);
561 
562  write_fat(s->pb, start_sector1, nb_sectors1, 0);
563  return fat1;
564  }
565 
566  return fat;
567 }
568 
570 {
571  AVIOContext *pb = s->pb;
572  WtvContext *wctx = s->priv_data;
573  int i;
574  for (i = 0; i < wctx->nb_sp_pairs; i++) {
575  avio_wl64(pb, wctx->sp_pairs[i].serial);
576  avio_wl64(pb, wctx->sp_pairs[i].value);
577  }
578 }
579 
581 {
582  AVIOContext *pb = s->pb;
583  WtvContext *wctx = s->priv_data;
584  int i;
585  for (i = 0; i < wctx->nb_st_pairs; i++) {
586  avio_wl64(pb, wctx->st_pairs[i].value);
587  avio_wl64(pb, wctx->st_pairs[i].serial);
588  }
589  avio_wl64(pb, wctx->last_pts);
590  avio_wl64(pb, wctx->last_serial);
591 }
592 
593 static void write_tag(AVIOContext *pb, const char *key, const char *value)
594 {
596  avio_wl32(pb, 1);
597  avio_wl32(pb, strlen(value)*2 + 2);
598  avio_put_str16le(pb, key);
599  avio_put_str16le(pb, value);
600 }
601 
603 {
604  AVDictionaryEntry *tag = 0;
605 
606  //FIXME: translate special tags (e.g. WM/Bitrate) to binary representation
608  while ((tag = av_dict_get(s->metadata, "", tag, AV_DICT_IGNORE_SUFFIX)))
609  write_tag(s->pb, tag->key, tag->value);
610 }
611 
613 {
614  AVIOContext *pb = s->pb;
615  AVDictionaryEntry *tag = 0;
616  int64_t pos = 0;
617 
618  //FIXME: translate special tags to binary representation
619  while ((tag = av_dict_get(s->metadata, "", tag, AV_DICT_IGNORE_SUFFIX))) {
620  avio_wl64(pb, pos);
621  pos += 16 + 4 + 4 + strlen(tag->key)*2 + 2 + strlen(tag->value)*2 + 2;
622  }
623 }
624 
625 /**
626  * Pad the remainder of a file
627  * Write out fat table
628  * @return <0 on error
629  */
630 static int finish_file(AVFormatContext *s, enum WtvFileIndex index, int64_t start_pos)
631 {
632  WtvContext *wctx = s->priv_data;
633  AVIOContext *pb = s->pb;
634  WtvFile *w = &wctx->file[index];
635  int64_t end_pos = avio_tell(pb);
636  int sector_bits, nb_sectors, pad;
637 
638  av_assert0(index < WTV_FILES);
639 
640  w->length = (end_pos - start_pos);
641 
642  // determine optimal fat table depth, sector_bits, nb_sectors
643  if (w->length <= WTV_SECTOR_SIZE) {
644  w->depth = 0;
645  sector_bits = WTV_SECTOR_BITS;
646  } else if (w->length <= (WTV_SECTOR_SIZE / 4) * WTV_SECTOR_SIZE) {
647  w->depth = 1;
648  sector_bits = WTV_SECTOR_BITS;
649  } else if (w->length <= (WTV_SECTOR_SIZE / 4) * WTV_BIGSECTOR_SIZE) {
650  w->depth = 1;
651  sector_bits = WTV_BIGSECTOR_BITS;
652  } else if (w->length <= (int64_t)(WTV_SECTOR_SIZE / 4) * (WTV_SECTOR_SIZE / 4) * WTV_SECTOR_SIZE) {
653  w->depth = 2;
654  sector_bits = WTV_SECTOR_BITS;
655  } else if (w->length <= (int64_t)(WTV_SECTOR_SIZE / 4) * (WTV_SECTOR_SIZE / 4) * WTV_BIGSECTOR_SIZE) {
656  w->depth = 2;
657  sector_bits = WTV_BIGSECTOR_BITS;
658  } else {
659  av_log(s, AV_LOG_ERROR, "unsupported file allocation table depth (%"PRIi64" bytes)\n", w->length);
660  return -1;
661  }
662 
663  // determine the nb_sectors
664  nb_sectors = (int)(w->length >> sector_bits);
665 
666  // pad sector of timeline
667  pad = (1 << sector_bits) - (w->length % (1 << sector_bits));
668  if (pad) {
669  nb_sectors++;
670  write_pad(pb, pad);
671  }
672 
673  //write fat table
674  if (w->depth > 0) {
675  w->first_sector = write_fat_sector(s, start_pos, nb_sectors, sector_bits, w->depth) >> WTV_SECTOR_BITS;
676  } else {
677  w->first_sector = start_pos >> WTV_SECTOR_BITS;
678  }
679 
680  w->length |= 1ULL<<60;
681  if (sector_bits == WTV_SECTOR_BITS)
682  w->length |= 1ULL<<63;
683 
684  return 0;
685 }
686 
688 {
689  WtvContext *wctx = s->priv_data;
690  AVIOContext *pb = s->pb;
691  int root_size;
692  int64_t sector_pos;
693  int64_t start_pos, file_end_pos;
694 
695  if (finish_file(s, WTV_TIMELINE, wctx->timeline_start_pos) < 0)
696  return -1;
697 
698  start_pos = avio_tell(pb);
700  if (finish_file(s, WTV_TIMELINE_TABLE_0_ENTRIES_EVENTS, start_pos) < 0)
701  return -1;
702 
703  start_pos = avio_tell(pb);
705  if (finish_file(s, WTV_TABLE_0_ENTRIES_LEGACY_ATTRIB, start_pos) < 0)
706  return -1;
707 
708  start_pos = avio_tell(pb);
710  if (finish_file(s, WTV_TABLE_0_REDIRECTOR_LEGACY_ATTRIB, start_pos) < 0)
711  return -1;
712 
713  start_pos = avio_tell(pb);
715  if (finish_file(s, WTV_TABLE_0_ENTRIES_TIME, start_pos) < 0)
716  return -1;
717 
718  // write root table
719  sector_pos = avio_tell(pb);
720  root_size = write_root_table(s, sector_pos);
721 
722  file_end_pos = avio_tell(pb);
723  // update root value
724  avio_seek(pb, 0x30, SEEK_SET);
725  avio_wl32(pb, root_size);
726  avio_seek(pb, 4, SEEK_CUR);
727  avio_wl32(pb, sector_pos >> WTV_SECTOR_BITS);
728  avio_seek(pb, 0x5c, SEEK_SET);
729  avio_wl32(pb, file_end_pos >> WTV_SECTOR_BITS);
730 
731  avio_flush(pb);
732 
733  av_free(wctx->sp_pairs);
734  av_free(wctx->st_pairs);
735  return 0;
736 }
737 
739  .name = "wtv",
740  .long_name = NULL_IF_CONFIG_SMALL("Windows Television (WTV)"),
741  .extensions = "wtv",
742  .priv_data_size = sizeof(WtvContext),
743  .audio_codec = AV_CODEC_ID_AC3,
744  .video_codec = AV_CODEC_ID_MPEG2VIDEO,
748  .codec_tag = (const AVCodecTag* const []){ ff_codec_bmp_tags,
749  ff_codec_wav_tags, 0 },
750 };