FFmpeg
 All Data Structures Namespaces 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 "avio_internal.h"
32 #include "internal.h"
33 #include "wtv.h"
34 #include "asf.h"
35 
36 #define WTV_BIGSECTOR_SIZE (1 << WTV_BIGSECTOR_BITS)
37 #define INDEX_BASE 0x2
38 #define MAX_NB_INDEX 10
39 
40 /* declare utf16le strings */
41 #define _ , 0,
43  {'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};
45  {'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};
47  {'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};
48 static const uint8_t table_0_header_time[] =
49  {'t'_'a'_'b'_'l'_'e'_'.'_'0'_'.'_'h'_'e'_'a'_'d'_'e'_'r'_'.'_'t'_'i'_'m'_'e', 0};
50 static const uint8_t legacy_attrib[] =
51  {'l'_'e'_'g'_'a'_'c'_'y'_'_'_'a'_'t'_'t'_'r'_'i'_'b', 0};
52 #undef _
53 
54 static const ff_asf_guid sub_wtv_guid =
55  {0x8C,0xC3,0xD2,0xC2,0x7E,0x9A,0xDA,0x11,0x8B,0xF7,0x00,0x07,0xE9,0x5E,0xAD,0x8D};
56 
67 };
68 
69 typedef struct {
70  int64_t length;
71  const void *header;
72  int depth;
74 } WtvFile;
75 
76 typedef struct {
77  int64_t pos;
78  int64_t serial;
79  const ff_asf_guid * guid;
80  int stream_id;
82 
83 typedef struct {
84  int64_t serial;
85  int64_t value;
86 } WtvSyncEntry;
87 
88 typedef struct {
91  int64_t serial; /**< chunk serial number */
92  int64_t last_chunk_pos; /**< last chunk position */
93  int64_t last_timestamp_pos; /**< last timestamp chunk position */
94  int64_t first_index_pos; /**< first index_chunk position */
95 
97  int nb_index;
99 
100  WtvSyncEntry *st_pairs; /* (serial, timestamp) pairs */
102  WtvSyncEntry *sp_pairs; /* (serial, position) pairs */
104 
105  int64_t last_pts;
106  int64_t last_serial;
107 
109 } WtvContext;
110 
111 
112 static void add_serial_pair(WtvSyncEntry ** list, int * count, int64_t serial, int64_t value)
113 {
114  int new_count = *count + 1;
115  WtvSyncEntry *new_list = av_realloc(*list, new_count * sizeof(WtvSyncEntry));
116  if (!new_list)
117  return;
118  new_list[*count] = (WtvSyncEntry){serial, value};
119  *list = new_list;
120  *count = new_count;
121 }
122 
124 
125 typedef struct {
126  const uint8_t *header;
130 
131 #define write_pad(pb, size) ffio_fill(pb, 0, size)
132 
133 static const ff_asf_guid *get_codec_guid(enum AVCodecID id, const AVCodecGuid *av_guid)
134 {
135  int i;
136  for (i = 0; av_guid[i].id != AV_CODEC_ID_NONE; i++) {
137  if (id == av_guid[i].id)
138  return &(av_guid[i].guid);
139  }
140  return NULL;
141 }
142 
143 /**
144  * Write chunk header. If header chunk (0x80000000 set) then add to list of header chunks
145  */
146 static void write_chunk_header(AVFormatContext *s, const ff_asf_guid *guid, int length, int stream_id)
147 {
148  WtvContext *wctx = s->priv_data;
149  AVIOContext *pb = s->pb;
150 
151  wctx->last_chunk_pos = avio_tell(pb) - wctx->timeline_start_pos;
152  ff_put_guid(pb, guid);
153  avio_wl32(pb, 32 + length);
154  avio_wl32(pb, stream_id);
155  avio_wl64(pb, wctx->serial);
156 
157  if ((stream_id & 0x80000000) && guid != &ff_index_guid) {
158  WtvChunkEntry *t = wctx->index + wctx->nb_index;
160  t->pos = wctx->last_chunk_pos;
161  t->serial = wctx->serial;
162  t->guid = guid;
163  t->stream_id = stream_id & 0x3FFFFFFF;
164  wctx->nb_index++;
165  }
166 }
167 
168 static void write_chunk_header2(AVFormatContext *s, const ff_asf_guid *guid, int stream_id)
169 {
170  WtvContext *wctx = s->priv_data;
171  AVIOContext *pb = s->pb;
172 
173  int64_t last_chunk_pos = wctx->last_chunk_pos;
174  write_chunk_header(s, guid, 0, stream_id); // length updated later
175  avio_wl64(pb, last_chunk_pos);
176 }
177 
179 {
180  WtvContext *wctx = s->priv_data;
181  AVIOContext *pb = s->pb;
182 
183  // update the chunk_len field and pad.
184  int64_t chunk_len = avio_tell(pb) - (wctx->last_chunk_pos + wctx->timeline_start_pos);
185  avio_seek(pb, -(chunk_len - 16), SEEK_CUR);
186  avio_wl32(pb, chunk_len);
187  avio_seek(pb, chunk_len - (16 + 4), SEEK_CUR);
188 
189  write_pad(pb, WTV_PAD8(chunk_len) - chunk_len);
190  wctx->serial++;
191 }
192 
194 {
195  AVIOContext *pb = s->pb;
196  WtvContext *wctx = s->priv_data;
197  int i;
198 
199  write_chunk_header2(s, &ff_index_guid, 0x80000000);
200  avio_wl32(pb, 0);
201  avio_wl32(pb, 0);
202 
203  for (i = 0; i < wctx->nb_index; i++) {
204  WtvChunkEntry *t = wctx->index + i;
205  ff_put_guid(pb, t->guid);
206  avio_wl64(pb, t->pos);
207  avio_wl32(pb, t->stream_id);
208  avio_wl32(pb, 0); // checksum?
209  avio_wl64(pb, t->serial);
210  }
211  wctx->nb_index = 0; // reset index
213 
214  if (!wctx->first_index_pos)
215  wctx->first_index_pos = wctx->last_chunk_pos;
216 }
217 
219 {
220  WtvContext *wctx = s->priv_data;
222  if (wctx->nb_index == MAX_NB_INDEX)
223  write_index(s);
224 }
225 
227 {
228  WtvContext *wctx = s->priv_data;
229  const ff_asf_guid *g, *media_type, *format_type;
230  AVIOContext *pb = s->pb;
231  int64_t hdr_pos_start;
232  int hdr_size = 0;
233 
234  if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
236  media_type = &ff_mediatype_video;
237  format_type = &ff_format_mpeg2_video;
238  } else if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
240  media_type = &ff_mediatype_audio;
241  format_type = &ff_format_waveformatex;
242  } else {
243  av_log(s, AV_LOG_ERROR, "unknown codec_type (0x%x)\n", st->codec->codec_type);
244  return -1;
245  }
246 
247  if (g == NULL) {
248  av_log(s, AV_LOG_ERROR, "can't get video codec_id (0x%x) guid.\n", st->codec->codec_id);
249  return -1;
250  }
251 
252  ff_put_guid(pb, media_type); // mediatype
254  write_pad(pb, 12);
255  ff_put_guid(pb,&ff_format_cpfilters_processed); // format type
256  avio_wl32(pb, 0); // size
257 
258  hdr_pos_start = avio_tell(pb);
259  if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
260  if (wctx->first_video_flag) {
261  write_pad(pb, 216); //The size is sensitive.
262  wctx->first_video_flag = 0;
263  } else {
264  write_pad(pb, 72); // aspect ratio
266  }
267  } else {
268  ff_put_wav_header(pb, st->codec);
269  }
270  hdr_size = avio_tell(pb) - hdr_pos_start;
271 
272  // seek back write hdr_size
273  avio_seek(pb, -(hdr_size + 4), SEEK_CUR);
274  avio_wl32(pb, hdr_size + 32);
275  avio_seek(pb, hdr_size, SEEK_CUR);
276  ff_put_guid(pb, g); // actual_subtype
277  ff_put_guid(pb, format_type); // actual_formattype
278 
279  return 0;
280 }
281 
283 {
284  AVIOContext *pb = s->pb;
285  int ret;
286  write_chunk_header2(s, &ff_stream1_guid, 0x80000000 | 0x01);
287 
288  avio_wl32(pb, 0x01);
289  write_pad(pb, 4);
290  write_pad(pb, 4);
291 
292  ret = write_stream_codec_info(s, st);
293  if (ret < 0) {
294  av_log(s, AV_LOG_ERROR, "write stream codec info failed codec_type(0x%x)\n", st->codec->codec_type);
295  return -1;
296  }
297 
298  finish_chunk(s);
299  return 0;
300 }
301 
303 {
304  AVIOContext *pb = s->pb;
305  WtvContext *wctx = s->priv_data;
306  int64_t last_chunk_pos = wctx->last_chunk_pos;
307 
308  write_chunk_header(s, &ff_sync_guid, 0x18, 0);
309  avio_wl64(pb, wctx->first_index_pos);
310  avio_wl64(pb, wctx->last_timestamp_pos);
311  avio_wl64(pb, 0);
312 
313  finish_chunk(s);
314  add_serial_pair(&wctx->sp_pairs, &wctx->nb_sp_pairs, wctx->serial, wctx->last_chunk_pos);
315 
316  wctx->last_chunk_pos = last_chunk_pos;
317 }
318 
320 {
321  AVIOContext *pb = s->pb;
322  int ret;
323 
325  avio_wl32(pb, 0x00000001);
326  avio_wl32(pb, st->index + INDEX_BASE); //stream_id
327  avio_wl32(pb, 0x00000001);
328  write_pad(pb, 8);
329 
330  ret = write_stream_codec_info(s, st);
331  if (ret < 0) {
332  av_log(s, AV_LOG_ERROR, "write stream codec info failed codec_type(0x%x)\n", st->codec->codec_type);
333  return -1;
334  }
335  finish_chunk(s);
336 
337  avpriv_set_pts_info(st, 64, 1, 10000000);
338 
339  return 0;
340 }
341 
343 {
344  AVIOContext *pb = s->pb;
345  WtvContext *wctx = s->priv_data;
346  int i, pad, ret;
347  AVStream *st;
348 
349  wctx->last_chunk_pos = -1;
350  wctx->last_timestamp_pos = -1;
351 
352  ff_put_guid(pb, &ff_wtv_guid);
354 
355  avio_wl32(pb, 0x01);
356  avio_wl32(pb, 0x02);
357  avio_wl32(pb, 1 << WTV_SECTOR_BITS);
358  avio_wl32(pb, 1 << WTV_BIGSECTOR_BITS);
359 
360  //write initial root fields
361  avio_wl32(pb, 0); // root_size, update later
362  write_pad(pb, 4);
363  avio_wl32(pb, 0); // root_sector, update it later.
364 
365  write_pad(pb, 32);
366  avio_wl32(pb, 0); // file ends pointer, update it later.
367 
368  pad = (1 << WTV_SECTOR_BITS) - avio_tell(pb);
369  write_pad(pb, pad);
370 
371  wctx->timeline_start_pos = avio_tell(pb);
372 
373  wctx->serial = 1;
374  wctx->last_chunk_pos = -1;
375  wctx->first_video_flag = 1;
376 
377  for (i = 0; i < s->nb_streams; i++) {
378  st = s->streams[i];
379  if (st->codec->codec_id == AV_CODEC_ID_MJPEG)
380  continue;
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  if (st->codec->codec_id == AV_CODEC_ID_MJPEG)
393  continue;
394  ret = write_stream_data(s, st);
395  if (ret < 0) {
396  av_log(s, AV_LOG_ERROR, "write stream data failed codec_type(0x%x)\n", st->codec->codec_type);
397  return -1;
398  }
399  }
400 
401  if (wctx->nb_index)
402  write_index(s);
403 
404  return 0;
405 }
406 
408 {
409  AVIOContext *pb = s->pb;
410  WtvContext *wctx = s->priv_data;
411  AVCodecContext *enc = s->streams[pkt->stream_index]->codec;
412 
413  write_chunk_header(s, &ff_timestamp_guid, 56, 0x40000000 | (INDEX_BASE + pkt->stream_index));
414  write_pad(pb, 8);
415  avio_wl64(pb, pkt->pts == AV_NOPTS_VALUE ? -1 : pkt->pts);
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  avio_wl64(pb, 0);
419  avio_wl64(pb, enc->codec_type == AVMEDIA_TYPE_VIDEO && (pkt->flags & AV_PKT_FLAG_KEY) ? 1 : 0);
420  avio_wl64(pb, 0);
421 
422  wctx->last_timestamp_pos = wctx->last_chunk_pos;
423 }
424 
426 {
427  AVIOContext *pb = s->pb;
428  WtvContext *wctx = s->priv_data;
429 
430  if (s->streams[pkt->stream_index]->codec->codec_id == AV_CODEC_ID_MJPEG && !wctx->thumbnail.size) {
431  av_copy_packet(&wctx->thumbnail, pkt);
432  return 0;
433  }
434 
435  /* emit sync chunk and 'timeline.table.0.entries.Event' record every 50 frames */
436  if (wctx->serial - (wctx->nb_sp_pairs ? wctx->sp_pairs[wctx->nb_sp_pairs - 1].serial : 0) >= 50)
437  write_sync(s);
438 
439  /* emit 'table.0.entries.time' record every 500ms */
440  if (pkt->pts != AV_NOPTS_VALUE && pkt->pts - (wctx->nb_st_pairs ? wctx->st_pairs[wctx->nb_st_pairs - 1].value : 0) >= 5000000)
441  add_serial_pair(&wctx->st_pairs, &wctx->nb_st_pairs, wctx->serial, pkt->pts);
442 
443  if (pkt->pts != AV_NOPTS_VALUE && pkt->pts > wctx->last_pts) {
444  wctx->last_pts = pkt->pts;
445  wctx->last_serial = wctx->serial;
446  }
447 
448  // write timestamp chunk
449  write_timestamp(s, pkt);
450 
452  avio_write(pb, pkt->data, pkt->size);
453  write_pad(pb, WTV_PAD8(pkt->size) - pkt->size);
454 
455  wctx->serial++;
456  return 0;
457 }
458 
460 {
461  avio_wl32(pb, 0x10);
462  write_pad(pb, 84);
463  avio_wl64(pb, 0x32);
464  return 96;
465 }
466 
468 {
469  int pad = 0;
470  avio_wl32(pb, 0xFFFFFFFF);
471  write_pad(pb, 12);
472  avio_write(pb, legacy_attrib, sizeof(legacy_attrib));
473  pad = WTV_PAD8(sizeof(legacy_attrib)) - sizeof(legacy_attrib);
474  write_pad(pb, pad);
475  write_pad(pb, 32);
476  return 48 + WTV_PAD8(sizeof(legacy_attrib));
477 }
478 
480 {
481  avio_wl32(pb, 0x10);
482  write_pad(pb, 76);
483  avio_wl64(pb, 0x40);
484  return 88;
485 }
486 
490  { ff_timeline_le16, sizeof(ff_timeline_le16), NULL},
496 };
497 
498 static int write_root_table(AVFormatContext *s, int64_t sector_pos)
499 {
500  AVIOContext *pb = s->pb;
501  WtvContext *wctx = s->priv_data;
502  int size, pad;
503  int i;
504 
506  for (i = 0; i < sizeof(wtv_root_entry_table)/sizeof(WTVRootEntryTable); i++, h++) {
507  WtvFile *w = &wctx->file[i];
508  int filename_padding = WTV_PAD8(h->header_size) - h->header_size;
509  WTVHeaderWriteFunc *write = h->write_header;
510  int len = 0;
511  int64_t len_pos;
512 
514  len_pos = avio_tell(pb);
515  avio_wl16(pb, 40 + h->header_size + filename_padding + 8); // maybe updated later
516  write_pad(pb, 6);
517  avio_wl64(pb, write ? 0 : w->length);// maybe update later
518  avio_wl32(pb, (h->header_size + filename_padding) >> 1);
519  write_pad(pb, 4);
520 
521  avio_write(pb, h->header, h->header_size);
522  write_pad(pb, filename_padding);
523 
524  if (write) {
525  len = write(pb);
526  // update length field
527  avio_seek(pb, len_pos, SEEK_SET);
528  avio_wl64(pb, 40 + h->header_size + filename_padding + len);
529  avio_wl64(pb, len |(1ULL<<62) | (1ULL<<60));
530  avio_seek(pb, 8 + h->header_size + filename_padding + len, SEEK_CUR);
531  } else {
532  avio_wl32(pb, w->first_sector);
533  avio_wl32(pb, w->depth);
534  }
535  }
536 
537  // caculate root table size
538  size = avio_tell(pb) - sector_pos;
539  pad = WTV_SECTOR_SIZE- size;
540  write_pad(pb, pad);
541 
542  return size;
543 }
544 
545 static void write_fat(AVIOContext *pb, int start_sector, int nb_sectors, int shift)
546 {
547  int i;
548  for (i = 0; i < nb_sectors; i++) {
549  avio_wl32(pb, start_sector + (i << shift));
550  }
551  // pad left sector pointer size
552  write_pad(pb, WTV_SECTOR_SIZE - ((nb_sectors << 2) % WTV_SECTOR_SIZE));
553 }
554 
555 static int64_t write_fat_sector(AVFormatContext *s, int64_t start_pos, int nb_sectors, int sector_bits, int depth)
556 {
557  int64_t start_sector = start_pos >> WTV_SECTOR_BITS;
558  int shift = sector_bits - WTV_SECTOR_BITS;
559 
560  int64_t fat = avio_tell(s->pb);
561  write_fat(s->pb, start_sector, nb_sectors, shift);
562 
563  if (depth == 2) {
564  int64_t start_sector1 = fat >> WTV_SECTOR_BITS;
565  int nb_sectors1 = ((nb_sectors << 2) + WTV_SECTOR_SIZE - 1) / WTV_SECTOR_SIZE;
566  int64_t fat1 = avio_tell(s->pb);
567 
568  write_fat(s->pb, start_sector1, nb_sectors1, 0);
569  return fat1;
570  }
571 
572  return fat;
573 }
574 
576 {
577  AVIOContext *pb = s->pb;
578  WtvContext *wctx = s->priv_data;
579  int i;
580  for (i = 0; i < wctx->nb_sp_pairs; i++) {
581  avio_wl64(pb, wctx->sp_pairs[i].serial);
582  avio_wl64(pb, wctx->sp_pairs[i].value);
583  }
584 }
585 
587 {
588  AVIOContext *pb = s->pb;
589  WtvContext *wctx = s->priv_data;
590  int i;
591  for (i = 0; i < wctx->nb_st_pairs; i++) {
592  avio_wl64(pb, wctx->st_pairs[i].value);
593  avio_wl64(pb, wctx->st_pairs[i].serial);
594  }
595  avio_wl64(pb, wctx->last_pts);
596  avio_wl64(pb, wctx->last_serial);
597 }
598 
599 static void write_metadata_header(AVIOContext *pb, int type, const char *key, int value_size)
600 {
602  avio_wl32(pb, type);
603  avio_wl32(pb, value_size);
604  avio_put_str16le(pb, key);
605 }
606 
607 static int metadata_header_size(const char *key)
608 {
609  return 16 + 4 + 4 + strlen(key)*2 + 2;
610 }
611 
612 static void write_tag_int32(AVIOContext *pb, const char *key, int value)
613 {
614  write_metadata_header(pb, 0, key, 4);
615  avio_wl32(pb, value);
616 }
617 
618 static void write_tag(AVIOContext *pb, const char *key, const char *value)
619 {
620  write_metadata_header(pb, 1, key, strlen(value)*2 + 2);
621  avio_put_str16le(pb, value);
622 }
623 
625 {
626  return strlen("image/jpeg")*2 + 2 + 1 + (e ? strlen(e->value)*2 : 0) + 2 + 4 + pkt->size;
627 }
628 
630 {
631  WtvContext *wctx = s->priv_data;
632  AVIOContext *pb = s->pb;
633  AVDictionaryEntry *tag = 0;
634 
635  //FIXME: translate special tags (e.g. WM/Bitrate) to binary representation
637  while ((tag = av_dict_get(s->metadata, "", tag, AV_DICT_IGNORE_SUFFIX)))
638  write_tag(pb, tag->key, tag->value);
639 
640  if (wctx->thumbnail.size) {
641  AVStream *st = s->streams[wctx->thumbnail.stream_index];
642  tag = av_dict_get(st->metadata, "title", NULL, 0);
643  write_metadata_header(pb, 2, "WM/Picture", attachment_value_size(&wctx->thumbnail, tag));
644 
645  avio_put_str16le(pb, "image/jpeg");
646  avio_w8(pb, 0x10);
647  avio_put_str16le(pb, tag ? tag->value : "");
648 
649  avio_wl32(pb, wctx->thumbnail.size);
650  avio_write(pb, wctx->thumbnail.data, wctx->thumbnail.size);
651 
652  write_tag_int32(pb, "WM/MediaThumbType", 2);
653  }
654 }
655 
657 {
658  WtvContext *wctx = s->priv_data;
659  AVIOContext *pb = s->pb;
660  AVDictionaryEntry *tag = 0;
661  int64_t pos = 0;
662 
663  //FIXME: translate special tags to binary representation
664  while ((tag = av_dict_get(s->metadata, "", tag, AV_DICT_IGNORE_SUFFIX))) {
665  avio_wl64(pb, pos);
666  pos += metadata_header_size(tag->key) + strlen(tag->value)*2 + 2;
667  }
668 
669  if (wctx->thumbnail.size) {
670  AVStream *st = s->streams[wctx->thumbnail.stream_index];
671  avio_wl64(pb, pos);
672  pos += metadata_header_size("WM/Picture") + attachment_value_size(&wctx->thumbnail, av_dict_get(st->metadata, "title", NULL, 0));
673 
674  avio_wl64(pb, pos);
675  pos += metadata_header_size("WM/MediaThumbType") + 4;
676  }
677 }
678 
679 /**
680  * Pad the remainder of a file
681  * Write out fat table
682  * @return <0 on error
683  */
684 static int finish_file(AVFormatContext *s, enum WtvFileIndex index, int64_t start_pos)
685 {
686  WtvContext *wctx = s->priv_data;
687  AVIOContext *pb = s->pb;
688  WtvFile *w = &wctx->file[index];
689  int64_t end_pos = avio_tell(pb);
690  int sector_bits, nb_sectors, pad;
691 
692  av_assert0(index < WTV_FILES);
693 
694  w->length = (end_pos - start_pos);
695 
696  // determine optimal fat table depth, sector_bits, nb_sectors
697  if (w->length <= WTV_SECTOR_SIZE) {
698  w->depth = 0;
699  sector_bits = WTV_SECTOR_BITS;
700  } else if (w->length <= (WTV_SECTOR_SIZE / 4) * WTV_SECTOR_SIZE) {
701  w->depth = 1;
702  sector_bits = WTV_SECTOR_BITS;
703  } else if (w->length <= (WTV_SECTOR_SIZE / 4) * WTV_BIGSECTOR_SIZE) {
704  w->depth = 1;
705  sector_bits = WTV_BIGSECTOR_BITS;
706  } else if (w->length <= (int64_t)(WTV_SECTOR_SIZE / 4) * (WTV_SECTOR_SIZE / 4) * WTV_SECTOR_SIZE) {
707  w->depth = 2;
708  sector_bits = WTV_SECTOR_BITS;
709  } else if (w->length <= (int64_t)(WTV_SECTOR_SIZE / 4) * (WTV_SECTOR_SIZE / 4) * WTV_BIGSECTOR_SIZE) {
710  w->depth = 2;
711  sector_bits = WTV_BIGSECTOR_BITS;
712  } else {
713  av_log(s, AV_LOG_ERROR, "unsupported file allocation table depth (%"PRIi64" bytes)\n", w->length);
714  return -1;
715  }
716 
717  // determine the nb_sectors
718  nb_sectors = (int)(w->length >> sector_bits);
719 
720  // pad sector of timeline
721  pad = (1 << sector_bits) - (w->length % (1 << sector_bits));
722  if (pad) {
723  nb_sectors++;
724  write_pad(pb, pad);
725  }
726 
727  //write fat table
728  if (w->depth > 0) {
729  w->first_sector = write_fat_sector(s, start_pos, nb_sectors, sector_bits, w->depth) >> WTV_SECTOR_BITS;
730  } else {
731  w->first_sector = start_pos >> WTV_SECTOR_BITS;
732  }
733 
734  w->length |= 1ULL<<60;
735  if (sector_bits == WTV_SECTOR_BITS)
736  w->length |= 1ULL<<63;
737 
738  return 0;
739 }
740 
742 {
743  WtvContext *wctx = s->priv_data;
744  AVIOContext *pb = s->pb;
745  int root_size;
746  int64_t sector_pos;
747  int64_t start_pos, file_end_pos;
748 
749  if (finish_file(s, WTV_TIMELINE, wctx->timeline_start_pos) < 0)
750  return -1;
751 
752  start_pos = avio_tell(pb);
754  if (finish_file(s, WTV_TIMELINE_TABLE_0_ENTRIES_EVENTS, start_pos) < 0)
755  return -1;
756 
757  start_pos = avio_tell(pb);
759  if (finish_file(s, WTV_TABLE_0_ENTRIES_LEGACY_ATTRIB, start_pos) < 0)
760  return -1;
761 
762  start_pos = avio_tell(pb);
764  if (finish_file(s, WTV_TABLE_0_REDIRECTOR_LEGACY_ATTRIB, start_pos) < 0)
765  return -1;
766 
767  start_pos = avio_tell(pb);
769  if (finish_file(s, WTV_TABLE_0_ENTRIES_TIME, start_pos) < 0)
770  return -1;
771 
772  // write root table
773  sector_pos = avio_tell(pb);
774  root_size = write_root_table(s, sector_pos);
775 
776  file_end_pos = avio_tell(pb);
777  // update root value
778  avio_seek(pb, 0x30, SEEK_SET);
779  avio_wl32(pb, root_size);
780  avio_seek(pb, 4, SEEK_CUR);
781  avio_wl32(pb, sector_pos >> WTV_SECTOR_BITS);
782  avio_seek(pb, 0x5c, SEEK_SET);
783  avio_wl32(pb, file_end_pos >> WTV_SECTOR_BITS);
784 
785  avio_flush(pb);
786 
787  av_free(wctx->sp_pairs);
788  av_free(wctx->st_pairs);
789  av_free_packet(&wctx->thumbnail);
790  return 0;
791 }
792 
794  .name = "wtv",
795  .long_name = NULL_IF_CONFIG_SMALL("Windows Television (WTV)"),
796  .extensions = "wtv",
797  .priv_data_size = sizeof(WtvContext),
798  .audio_codec = AV_CODEC_ID_AC3,
799  .video_codec = AV_CODEC_ID_MPEG2VIDEO,
803  .codec_tag = (const AVCodecTag* const []){ ff_codec_bmp_tags,
804  ff_codec_wav_tags, 0 },
805 };