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 {
229  unsigned int num, den;
230  av_reduce(&num, &den, dar.num, dar.den, 0xFFFFFFFF);
231 
232  /* VIDEOINFOHEADER2 */
233  avio_wl32(pb, 0);
234  avio_wl32(pb, 0);
235  avio_wl32(pb, st->codec->width);
236  avio_wl32(pb, st->codec->height);
237 
238  avio_wl32(pb, 0);
239  avio_wl32(pb, 0);
240  avio_wl32(pb, 0);
241  avio_wl32(pb, 0);
242 
243  avio_wl32(pb, st->codec->bit_rate);
244  avio_wl32(pb, 0);
245  avio_wl64(pb, st->avg_frame_rate.num && st->avg_frame_rate.den ? INT64_C(10000000) / av_q2d(st->avg_frame_rate) : 0);
246  avio_wl32(pb, 0);
247  avio_wl32(pb, 0);
248 
249  avio_wl32(pb, num);
250  avio_wl32(pb, den);
251  avio_wl32(pb, 0);
252  avio_wl32(pb, 0);
253 
254  ff_put_bmp_header(pb, st->codec, ff_codec_bmp_tags, 0, 1);
255 
256  if (st->codec->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
257  /* MPEG2VIDEOINFO */
258  avio_wl32(pb, 0);
259  avio_wl32(pb, st->codec->extradata_size);
260  avio_wl32(pb, -1);
261  avio_wl32(pb, -1);
262  avio_wl32(pb, 0);
263  avio_write(pb, st->codec->extradata, st->codec->extradata_size);
264  avio_wl64(pb, 0);
265  }
266 }
267 
269 {
270  const ff_asf_guid *g, *media_type, *format_type;
271  AVIOContext *pb = s->pb;
272  int64_t hdr_pos_start;
273  int hdr_size = 0;
274 
275  if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
277  media_type = &ff_mediatype_video;
278  format_type = &ff_format_mpeg2_video;
279  } else if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
281  media_type = &ff_mediatype_audio;
282  format_type = &ff_format_waveformatex;
283  } else {
284  av_log(s, AV_LOG_ERROR, "unknown codec_type (0x%x)\n", st->codec->codec_type);
285  return -1;
286  }
287 
288  if (g == NULL) {
289  av_log(s, AV_LOG_ERROR, "can't get video codec_id (0x%x) guid.\n", st->codec->codec_id);
290  return -1;
291  }
292 
293  ff_put_guid(pb, media_type); // mediatype
295  write_pad(pb, 12);
296  ff_put_guid(pb,&ff_format_cpfilters_processed); // format type
297  avio_wl32(pb, 0); // size
298 
299  hdr_pos_start = avio_tell(pb);
300  if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
301  put_videoinfoheader2(pb, st);
302  } else {
303  ff_put_wav_header(pb, st->codec);
304  }
305  hdr_size = avio_tell(pb) - hdr_pos_start;
306 
307  // seek back write hdr_size
308  avio_seek(pb, -(hdr_size + 4), SEEK_CUR);
309  avio_wl32(pb, hdr_size + 32);
310  avio_seek(pb, hdr_size, SEEK_CUR);
311  ff_put_guid(pb, g); // actual_subtype
312  ff_put_guid(pb, format_type); // actual_formattype
313 
314  return 0;
315 }
316 
318 {
319  AVIOContext *pb = s->pb;
320  int ret;
321  write_chunk_header2(s, &ff_stream1_guid, 0x80000000 | 0x01);
322 
323  avio_wl32(pb, 0x01);
324  write_pad(pb, 4);
325  write_pad(pb, 4);
326 
327  ret = write_stream_codec_info(s, st);
328  if (ret < 0) {
329  av_log(s, AV_LOG_ERROR, "write stream codec info failed codec_type(0x%x)\n", st->codec->codec_type);
330  return -1;
331  }
332 
333  finish_chunk(s);
334  return 0;
335 }
336 
338 {
339  AVIOContext *pb = s->pb;
340  WtvContext *wctx = s->priv_data;
341  int64_t last_chunk_pos = wctx->last_chunk_pos;
342 
343  write_chunk_header(s, &ff_sync_guid, 0x18, 0);
344  avio_wl64(pb, wctx->first_index_pos);
345  avio_wl64(pb, wctx->last_timestamp_pos);
346  avio_wl64(pb, 0);
347 
348  finish_chunk(s);
349  add_serial_pair(&wctx->sp_pairs, &wctx->nb_sp_pairs, wctx->serial, wctx->last_chunk_pos);
350 
351  wctx->last_chunk_pos = last_chunk_pos;
352 }
353 
355 {
356  AVIOContext *pb = s->pb;
357  int ret;
358 
360  avio_wl32(pb, 0x00000001);
361  avio_wl32(pb, st->index + INDEX_BASE); //stream_id
362  avio_wl32(pb, 0x00000001);
363  write_pad(pb, 8);
364 
365  ret = write_stream_codec_info(s, st);
366  if (ret < 0) {
367  av_log(s, AV_LOG_ERROR, "write stream codec info failed codec_type(0x%x)\n", st->codec->codec_type);
368  return -1;
369  }
370  finish_chunk(s);
371 
372  avpriv_set_pts_info(st, 64, 1, 10000000);
373 
374  return 0;
375 }
376 
378 {
379  AVIOContext *pb = s->pb;
380  WtvContext *wctx = s->priv_data;
381  int i, pad, ret;
382  AVStream *st;
383 
384  wctx->last_chunk_pos = -1;
385  wctx->last_timestamp_pos = -1;
386 
387  ff_put_guid(pb, &ff_wtv_guid);
389 
390  avio_wl32(pb, 0x01);
391  avio_wl32(pb, 0x02);
392  avio_wl32(pb, 1 << WTV_SECTOR_BITS);
393  avio_wl32(pb, 1 << WTV_BIGSECTOR_BITS);
394 
395  //write initial root fields
396  avio_wl32(pb, 0); // root_size, update later
397  write_pad(pb, 4);
398  avio_wl32(pb, 0); // root_sector, update it later.
399 
400  write_pad(pb, 32);
401  avio_wl32(pb, 0); // file ends pointer, update it later.
402 
403  pad = (1 << WTV_SECTOR_BITS) - avio_tell(pb);
404  write_pad(pb, pad);
405 
406  wctx->timeline_start_pos = avio_tell(pb);
407 
408  wctx->serial = 1;
409  wctx->last_chunk_pos = -1;
410  wctx->first_video_flag = 1;
411 
412  for (i = 0; i < s->nb_streams; i++) {
413  st = s->streams[i];
414  if (st->codec->codec_id == AV_CODEC_ID_MJPEG)
415  continue;
416  ret = write_stream_codec(s, st);
417  if (ret < 0) {
418  av_log(s, AV_LOG_ERROR, "write stream codec failed codec_type(0x%x)\n", st->codec->codec_type);
419  return -1;
420  }
421  if (!i)
422  write_sync(s);
423  }
424 
425  for (i = 0; i < s->nb_streams; i++) {
426  st = s->streams[i];
427  if (st->codec->codec_id == AV_CODEC_ID_MJPEG)
428  continue;
429  ret = write_stream_data(s, st);
430  if (ret < 0) {
431  av_log(s, AV_LOG_ERROR, "write stream data failed codec_type(0x%x)\n", st->codec->codec_type);
432  return -1;
433  }
434  }
435 
436  if (wctx->nb_index)
437  write_index(s);
438 
439  return 0;
440 }
441 
443 {
444  AVIOContext *pb = s->pb;
445  WtvContext *wctx = s->priv_data;
446  AVCodecContext *enc = s->streams[pkt->stream_index]->codec;
447 
448  write_chunk_header(s, &ff_timestamp_guid, 56, 0x40000000 | (INDEX_BASE + pkt->stream_index));
449  write_pad(pb, 8);
450  avio_wl64(pb, pkt->pts == AV_NOPTS_VALUE ? -1 : pkt->pts);
451  avio_wl64(pb, pkt->pts == AV_NOPTS_VALUE ? -1 : pkt->pts);
452  avio_wl64(pb, pkt->pts == AV_NOPTS_VALUE ? -1 : pkt->pts);
453  avio_wl64(pb, 0);
454  avio_wl64(pb, enc->codec_type == AVMEDIA_TYPE_VIDEO && (pkt->flags & AV_PKT_FLAG_KEY) ? 1 : 0);
455  avio_wl64(pb, 0);
456 
457  wctx->last_timestamp_pos = wctx->last_chunk_pos;
458 }
459 
461 {
462  AVIOContext *pb = s->pb;
463  WtvContext *wctx = s->priv_data;
464 
465  if (s->streams[pkt->stream_index]->codec->codec_id == AV_CODEC_ID_MJPEG && !wctx->thumbnail.size) {
466  av_copy_packet(&wctx->thumbnail, pkt);
467  return 0;
468  }
469 
470  /* emit sync chunk and 'timeline.table.0.entries.Event' record every 50 frames */
471  if (wctx->serial - (wctx->nb_sp_pairs ? wctx->sp_pairs[wctx->nb_sp_pairs - 1].serial : 0) >= 50)
472  write_sync(s);
473 
474  /* emit 'table.0.entries.time' record every 500ms */
475  if (pkt->pts != AV_NOPTS_VALUE && pkt->pts - (wctx->nb_st_pairs ? wctx->st_pairs[wctx->nb_st_pairs - 1].value : 0) >= 5000000)
476  add_serial_pair(&wctx->st_pairs, &wctx->nb_st_pairs, wctx->serial, pkt->pts);
477 
478  if (pkt->pts != AV_NOPTS_VALUE && pkt->pts > wctx->last_pts) {
479  wctx->last_pts = pkt->pts;
480  wctx->last_serial = wctx->serial;
481  }
482 
483  // write timestamp chunk
484  write_timestamp(s, pkt);
485 
487  avio_write(pb, pkt->data, pkt->size);
488  write_pad(pb, WTV_PAD8(pkt->size) - pkt->size);
489 
490  wctx->serial++;
491  return 0;
492 }
493 
495 {
496  avio_wl32(pb, 0x10);
497  write_pad(pb, 84);
498  avio_wl64(pb, 0x32);
499  return 96;
500 }
501 
503 {
504  int pad = 0;
505  avio_wl32(pb, 0xFFFFFFFF);
506  write_pad(pb, 12);
507  avio_write(pb, legacy_attrib, sizeof(legacy_attrib));
508  pad = WTV_PAD8(sizeof(legacy_attrib)) - sizeof(legacy_attrib);
509  write_pad(pb, pad);
510  write_pad(pb, 32);
511  return 48 + WTV_PAD8(sizeof(legacy_attrib));
512 }
513 
515 {
516  avio_wl32(pb, 0x10);
517  write_pad(pb, 76);
518  avio_wl64(pb, 0x40);
519  return 88;
520 }
521 
525  { ff_timeline_le16, sizeof(ff_timeline_le16), NULL},
531 };
532 
533 static int write_root_table(AVFormatContext *s, int64_t sector_pos)
534 {
535  AVIOContext *pb = s->pb;
536  WtvContext *wctx = s->priv_data;
537  int size, pad;
538  int i;
539 
541  for (i = 0; i < sizeof(wtv_root_entry_table)/sizeof(WTVRootEntryTable); i++, h++) {
542  WtvFile *w = &wctx->file[i];
543  int filename_padding = WTV_PAD8(h->header_size) - h->header_size;
544  WTVHeaderWriteFunc *write = h->write_header;
545  int len = 0;
546  int64_t len_pos;
547 
549  len_pos = avio_tell(pb);
550  avio_wl16(pb, 40 + h->header_size + filename_padding + 8); // maybe updated later
551  write_pad(pb, 6);
552  avio_wl64(pb, write ? 0 : w->length);// maybe update later
553  avio_wl32(pb, (h->header_size + filename_padding) >> 1);
554  write_pad(pb, 4);
555 
556  avio_write(pb, h->header, h->header_size);
557  write_pad(pb, filename_padding);
558 
559  if (write) {
560  len = write(pb);
561  // update length field
562  avio_seek(pb, len_pos, SEEK_SET);
563  avio_wl64(pb, 40 + h->header_size + filename_padding + len);
564  avio_wl64(pb, len |(1ULL<<62) | (1ULL<<60));
565  avio_seek(pb, 8 + h->header_size + filename_padding + len, SEEK_CUR);
566  } else {
567  avio_wl32(pb, w->first_sector);
568  avio_wl32(pb, w->depth);
569  }
570  }
571 
572  // caculate root table size
573  size = avio_tell(pb) - sector_pos;
574  pad = WTV_SECTOR_SIZE- size;
575  write_pad(pb, pad);
576 
577  return size;
578 }
579 
580 static void write_fat(AVIOContext *pb, int start_sector, int nb_sectors, int shift)
581 {
582  int i;
583  for (i = 0; i < nb_sectors; i++) {
584  avio_wl32(pb, start_sector + (i << shift));
585  }
586  // pad left sector pointer size
587  write_pad(pb, WTV_SECTOR_SIZE - ((nb_sectors << 2) % WTV_SECTOR_SIZE));
588 }
589 
590 static int64_t write_fat_sector(AVFormatContext *s, int64_t start_pos, int nb_sectors, int sector_bits, int depth)
591 {
592  int64_t start_sector = start_pos >> WTV_SECTOR_BITS;
593  int shift = sector_bits - WTV_SECTOR_BITS;
594 
595  int64_t fat = avio_tell(s->pb);
596  write_fat(s->pb, start_sector, nb_sectors, shift);
597 
598  if (depth == 2) {
599  int64_t start_sector1 = fat >> WTV_SECTOR_BITS;
600  int nb_sectors1 = ((nb_sectors << 2) + WTV_SECTOR_SIZE - 1) / WTV_SECTOR_SIZE;
601  int64_t fat1 = avio_tell(s->pb);
602 
603  write_fat(s->pb, start_sector1, nb_sectors1, 0);
604  return fat1;
605  }
606 
607  return fat;
608 }
609 
611 {
612  AVIOContext *pb = s->pb;
613  WtvContext *wctx = s->priv_data;
614  int i;
615  for (i = 0; i < wctx->nb_sp_pairs; i++) {
616  avio_wl64(pb, wctx->sp_pairs[i].serial);
617  avio_wl64(pb, wctx->sp_pairs[i].value);
618  }
619 }
620 
622 {
623  AVIOContext *pb = s->pb;
624  WtvContext *wctx = s->priv_data;
625  int i;
626  for (i = 0; i < wctx->nb_st_pairs; i++) {
627  avio_wl64(pb, wctx->st_pairs[i].value);
628  avio_wl64(pb, wctx->st_pairs[i].serial);
629  }
630  avio_wl64(pb, wctx->last_pts);
631  avio_wl64(pb, wctx->last_serial);
632 }
633 
634 static void write_metadata_header(AVIOContext *pb, int type, const char *key, int value_size)
635 {
637  avio_wl32(pb, type);
638  avio_wl32(pb, value_size);
639  avio_put_str16le(pb, key);
640 }
641 
642 static int metadata_header_size(const char *key)
643 {
644  return 16 + 4 + 4 + strlen(key)*2 + 2;
645 }
646 
647 static void write_tag_int32(AVIOContext *pb, const char *key, int value)
648 {
649  write_metadata_header(pb, 0, key, 4);
650  avio_wl32(pb, value);
651 }
652 
653 static void write_tag(AVIOContext *pb, const char *key, const char *value)
654 {
655  write_metadata_header(pb, 1, key, strlen(value)*2 + 2);
656  avio_put_str16le(pb, value);
657 }
658 
660 {
661  return strlen("image/jpeg")*2 + 2 + 1 + (e ? strlen(e->value)*2 : 0) + 2 + 4 + pkt->size;
662 }
663 
665 {
666  WtvContext *wctx = s->priv_data;
667  AVIOContext *pb = s->pb;
668  AVDictionaryEntry *tag = 0;
669 
670  //FIXME: translate special tags (e.g. WM/Bitrate) to binary representation
672  while ((tag = av_dict_get(s->metadata, "", tag, AV_DICT_IGNORE_SUFFIX)))
673  write_tag(pb, tag->key, tag->value);
674 
675  if (wctx->thumbnail.size) {
676  AVStream *st = s->streams[wctx->thumbnail.stream_index];
677  tag = av_dict_get(st->metadata, "title", NULL, 0);
678  write_metadata_header(pb, 2, "WM/Picture", attachment_value_size(&wctx->thumbnail, tag));
679 
680  avio_put_str16le(pb, "image/jpeg");
681  avio_w8(pb, 0x10);
682  avio_put_str16le(pb, tag ? tag->value : "");
683 
684  avio_wl32(pb, wctx->thumbnail.size);
685  avio_write(pb, wctx->thumbnail.data, wctx->thumbnail.size);
686 
687  write_tag_int32(pb, "WM/MediaThumbType", 2);
688  }
689 }
690 
692 {
693  WtvContext *wctx = s->priv_data;
694  AVIOContext *pb = s->pb;
695  AVDictionaryEntry *tag = 0;
696  int64_t pos = 0;
697 
698  //FIXME: translate special tags to binary representation
699  while ((tag = av_dict_get(s->metadata, "", tag, AV_DICT_IGNORE_SUFFIX))) {
700  avio_wl64(pb, pos);
701  pos += metadata_header_size(tag->key) + strlen(tag->value)*2 + 2;
702  }
703 
704  if (wctx->thumbnail.size) {
705  AVStream *st = s->streams[wctx->thumbnail.stream_index];
706  avio_wl64(pb, pos);
707  pos += metadata_header_size("WM/Picture") + attachment_value_size(&wctx->thumbnail, av_dict_get(st->metadata, "title", NULL, 0));
708 
709  avio_wl64(pb, pos);
710  pos += metadata_header_size("WM/MediaThumbType") + 4;
711  }
712 }
713 
714 /**
715  * Pad the remainder of a file
716  * Write out fat table
717  * @return <0 on error
718  */
719 static int finish_file(AVFormatContext *s, enum WtvFileIndex index, int64_t start_pos)
720 {
721  WtvContext *wctx = s->priv_data;
722  AVIOContext *pb = s->pb;
723  WtvFile *w = &wctx->file[index];
724  int64_t end_pos = avio_tell(pb);
725  int sector_bits, nb_sectors, pad;
726 
727  av_assert0(index < WTV_FILES);
728 
729  w->length = (end_pos - start_pos);
730 
731  // determine optimal fat table depth, sector_bits, nb_sectors
732  if (w->length <= WTV_SECTOR_SIZE) {
733  w->depth = 0;
734  sector_bits = WTV_SECTOR_BITS;
735  } else if (w->length <= (WTV_SECTOR_SIZE / 4) * WTV_SECTOR_SIZE) {
736  w->depth = 1;
737  sector_bits = WTV_SECTOR_BITS;
738  } else if (w->length <= (WTV_SECTOR_SIZE / 4) * WTV_BIGSECTOR_SIZE) {
739  w->depth = 1;
740  sector_bits = WTV_BIGSECTOR_BITS;
741  } else if (w->length <= (int64_t)(WTV_SECTOR_SIZE / 4) * (WTV_SECTOR_SIZE / 4) * WTV_SECTOR_SIZE) {
742  w->depth = 2;
743  sector_bits = WTV_SECTOR_BITS;
744  } else if (w->length <= (int64_t)(WTV_SECTOR_SIZE / 4) * (WTV_SECTOR_SIZE / 4) * WTV_BIGSECTOR_SIZE) {
745  w->depth = 2;
746  sector_bits = WTV_BIGSECTOR_BITS;
747  } else {
748  av_log(s, AV_LOG_ERROR, "unsupported file allocation table depth (%"PRIi64" bytes)\n", w->length);
749  return -1;
750  }
751 
752  // determine the nb_sectors
753  nb_sectors = (int)(w->length >> sector_bits);
754 
755  // pad sector of timeline
756  pad = (1 << sector_bits) - (w->length % (1 << sector_bits));
757  if (pad) {
758  nb_sectors++;
759  write_pad(pb, pad);
760  }
761 
762  //write fat table
763  if (w->depth > 0) {
764  w->first_sector = write_fat_sector(s, start_pos, nb_sectors, sector_bits, w->depth) >> WTV_SECTOR_BITS;
765  } else {
766  w->first_sector = start_pos >> WTV_SECTOR_BITS;
767  }
768 
769  w->length |= 1ULL<<60;
770  if (sector_bits == WTV_SECTOR_BITS)
771  w->length |= 1ULL<<63;
772 
773  return 0;
774 }
775 
777 {
778  WtvContext *wctx = s->priv_data;
779  AVIOContext *pb = s->pb;
780  int root_size;
781  int64_t sector_pos;
782  int64_t start_pos, file_end_pos;
783 
784  if (finish_file(s, WTV_TIMELINE, wctx->timeline_start_pos) < 0)
785  return -1;
786 
787  start_pos = avio_tell(pb);
789  if (finish_file(s, WTV_TIMELINE_TABLE_0_ENTRIES_EVENTS, start_pos) < 0)
790  return -1;
791 
792  start_pos = avio_tell(pb);
794  if (finish_file(s, WTV_TABLE_0_ENTRIES_LEGACY_ATTRIB, start_pos) < 0)
795  return -1;
796 
797  start_pos = avio_tell(pb);
799  if (finish_file(s, WTV_TABLE_0_REDIRECTOR_LEGACY_ATTRIB, start_pos) < 0)
800  return -1;
801 
802  start_pos = avio_tell(pb);
804  if (finish_file(s, WTV_TABLE_0_ENTRIES_TIME, start_pos) < 0)
805  return -1;
806 
807  // write root table
808  sector_pos = avio_tell(pb);
809  root_size = write_root_table(s, sector_pos);
810 
811  file_end_pos = avio_tell(pb);
812  // update root value
813  avio_seek(pb, 0x30, SEEK_SET);
814  avio_wl32(pb, root_size);
815  avio_seek(pb, 4, SEEK_CUR);
816  avio_wl32(pb, sector_pos >> WTV_SECTOR_BITS);
817  avio_seek(pb, 0x5c, SEEK_SET);
818  avio_wl32(pb, file_end_pos >> WTV_SECTOR_BITS);
819 
820  avio_flush(pb);
821 
822  av_free(wctx->sp_pairs);
823  av_free(wctx->st_pairs);
824  av_free_packet(&wctx->thumbnail);
825  return 0;
826 }
827 
829  .name = "wtv",
830  .long_name = NULL_IF_CONFIG_SMALL("Windows Television (WTV)"),
831  .extensions = "wtv",
832  .priv_data_size = sizeof(WtvContext),
833  .audio_codec = AV_CODEC_ID_AC3,
834  .video_codec = AV_CODEC_ID_MPEG2VIDEO,
838  .codec_tag = (const AVCodecTag* const []){ ff_codec_bmp_tags,
839  ff_codec_wav_tags, 0 },
840 };