wtv.c
Go to the documentation of this file.
1 /*
2  * Windows Television (WTV) demuxer
3  * Copyright (c) 2010-2011 Peter Ross <pross@xvid.org>
4  *
5  * This file is part of Libav.
6  *
7  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
28 #include "libavutil/intreadwrite.h"
29 #include "libavutil/intfloat.h"
30 #include "libavutil/dict.h"
31 #include "avformat.h"
32 #include "internal.h"
33 #include "riff.h"
34 #include "asf.h"
35 #include "mpegts.h"
36 
37 /* Macros for formating GUIDs */
38 #define PRI_GUID \
39  "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x"
40 #define ARG_GUID(g) \
41  g[0],g[1],g[2],g[3],g[4],g[5],g[6],g[7],g[8],g[9],g[10],g[11],g[12],g[13],g[14],g[15]
42 
43 #define PRI_PRETTY_GUID \
44  "%08x-%04x-%04x-%02x%02x%02x%02x%02x%02x%02x%02x"
45 #define ARG_PRETTY_GUID(g) \
46  AV_RL32(g),AV_RL16(g+4),AV_RL16(g+6),g[8],g[9],g[10],g[11],g[12],g[13],g[14],g[15]
47 #define LEN_PRETTY_GUID 34
48 
49 /*
50  *
51  * File system routines
52  *
53  */
54 
55 #define WTV_SECTOR_BITS 12
56 #define WTV_SECTOR_SIZE (1 << WTV_SECTOR_BITS)
57 #define WTV_BIGSECTOR_BITS 18
58 
59 typedef struct {
63  uint32_t *sectors;
64  int nb_sectors;
66  int error;
67  int64_t position;
68  int64_t length;
69 } WtvFile;
70 
74 static int wtvfile_read_packet(void *opaque, uint8_t *buf, int buf_size)
75 {
76  WtvFile *wf = opaque;
77  AVIOContext *pb = wf->pb_filesystem;
78  int nread = 0;
79 
80  if (wf->error || pb->error)
81  return -1;
82  if (wf->position >= wf->length || pb->eof_reached)
83  return 0;
84 
85  buf_size = FFMIN(buf_size, wf->length - wf->position);
86  while(nread < buf_size) {
87  int n;
88  int remaining_in_sector = (1 << wf->sector_bits) - (wf->position & ((1 << wf->sector_bits) - 1));
89  int read_request = FFMIN(buf_size - nread, remaining_in_sector);
90 
91  n = avio_read(pb, buf, read_request);
92  if (n <= 0)
93  break;
94  nread += n;
95  buf += n;
96  wf->position += n;
97  if (n == remaining_in_sector) {
98  int i = wf->position >> wf->sector_bits;
99  if (i >= wf->nb_sectors ||
100  (wf->sectors[i] != wf->sectors[i - 1] + (1 << (wf->sector_bits - WTV_SECTOR_BITS)) &&
101  avio_seek(pb, (int64_t)wf->sectors[i] << WTV_SECTOR_BITS, SEEK_SET) < 0)) {
102  wf->error = 1;
103  break;
104  }
105  }
106  }
107  return nread;
108 }
109 
113 static int64_t wtvfile_seek(void *opaque, int64_t offset, int whence)
114 {
115  WtvFile *wf = opaque;
116  AVIOContext *pb = wf->pb_filesystem;
117 
118  if (whence == AVSEEK_SIZE)
119  return wf->length;
120  else if (whence == SEEK_CUR)
121  offset = wf->position + offset;
122  else if (whence == SEEK_END)
123  offset = wf->length;
124 
125  wf->error = offset < 0 || offset >= wf->length ||
126  avio_seek(pb, ((int64_t)wf->sectors[offset >> wf->sector_bits] << WTV_SECTOR_BITS)
127  + (offset & ((1 << wf->sector_bits) - 1)), SEEK_SET) < 0;
128  wf->position = offset;
129  return offset;
130 }
131 
139 static int read_ints(AVIOContext *pb, uint32_t *data, int count)
140 {
141  int i, total = 0;
142  for (i = 0; i < count; i++) {
143  if ((data[total] = avio_rl32(pb)))
144  total++;
145  }
146  return total;
147 }
148 
156 static AVIOContext * wtvfile_open_sector(int first_sector, uint64_t length, int depth, AVFormatContext *s)
157 {
158  AVIOContext *pb;
159  WtvFile *wf;
160  uint8_t *buffer;
161 
162  if (avio_seek(s->pb, first_sector << WTV_SECTOR_BITS, SEEK_SET) < 0)
163  return NULL;
164 
165  wf = av_mallocz(sizeof(WtvFile));
166  if (!wf)
167  return NULL;
168 
169  if (depth == 0) {
170  wf->sectors = av_malloc(sizeof(uint32_t));
171  if (!wf->sectors) {
172  av_free(wf);
173  return NULL;
174  }
175  wf->sectors[0] = first_sector;
176  wf->nb_sectors = 1;
178  } else if (depth == 1) {
180  if (!wf->sectors) {
181  av_free(wf);
182  return NULL;
183  }
184  wf->nb_sectors = read_ints(s->pb, wf->sectors, WTV_SECTOR_SIZE / 4);
185  wf->sector_bits = length & (1ULL<<63) ? WTV_SECTOR_BITS : WTV_BIGSECTOR_BITS;
186  } else if (depth == 2) {
187  uint32_t sectors1[WTV_SECTOR_SIZE / 4];
188  int nb_sectors1 = read_ints(s->pb, sectors1, WTV_SECTOR_SIZE / 4);
189  int i;
190 
191  wf->sectors = av_malloc(nb_sectors1 << WTV_SECTOR_BITS);
192  if (!wf->sectors) {
193  av_free(wf);
194  return NULL;
195  }
196  wf->nb_sectors = 0;
197  for (i = 0; i < nb_sectors1; i++) {
198  if (avio_seek(s->pb, (int64_t)sectors1[i] << WTV_SECTOR_BITS, SEEK_SET) < 0)
199  break;
200  wf->nb_sectors += read_ints(s->pb, wf->sectors + i * WTV_SECTOR_SIZE / 4, WTV_SECTOR_SIZE / 4);
201  }
202  wf->sector_bits = length & (1ULL<<63) ? WTV_SECTOR_BITS : WTV_BIGSECTOR_BITS;
203  } else {
204  av_log(s, AV_LOG_ERROR, "unsupported file allocation table depth (0x%x)\n", depth);
205  av_free(wf);
206  return NULL;
207  }
208 
209  if (!wf->nb_sectors) {
210  av_free(wf->sectors);
211  av_free(wf);
212  return NULL;
213  }
214 
215  /* check length */
216  length &= 0xFFFFFFFFFFFF;
217  if (length > ((int64_t)wf->nb_sectors << wf->sector_bits)) {
218  av_log(s, AV_LOG_WARNING, "reported file length (0x%"PRIx64") exceeds number of available sectors (0x%"PRIx64")\n", length, (int64_t)wf->nb_sectors << wf->sector_bits);
219  length = (int64_t)wf->nb_sectors << wf->sector_bits;
220  }
221  wf->length = length;
222 
223  /* seek to intial sector */
224  wf->position = 0;
225  if (avio_seek(s->pb, (int64_t)wf->sectors[0] << WTV_SECTOR_BITS, SEEK_SET) < 0) {
226  av_free(wf->sectors);
227  av_free(wf);
228  return NULL;
229  }
230 
231  wf->pb_filesystem = s->pb;
232  buffer = av_malloc(1 << wf->sector_bits);
233  if (!buffer) {
234  av_free(wf->sectors);
235  av_free(wf);
236  return NULL;
237  }
238 
239  pb = avio_alloc_context(buffer, 1 << wf->sector_bits, 0, wf,
241  if (!pb) {
242  av_free(buffer);
243  av_free(wf->sectors);
244  av_free(wf);
245  }
246  return pb;
247 }
248 
250  {0x92,0xB7,0x74,0x91,0x59,0x70,0x70,0x44,0x88,0xDF,0x06,0x3B,0x82,0xCC,0x21,0x3D};
251 
260 static AVIOContext * wtvfile_open2(AVFormatContext *s, const uint8_t *buf, int buf_size, const uint8_t *filename, int filename_size)
261 {
262  const uint8_t *buf_end = buf + buf_size;
263 
264  while(buf + 48 <= buf_end) {
265  int dir_length, name_size, first_sector, depth;
266  uint64_t file_length;
267  const uint8_t *name;
268  if (ff_guidcmp(buf, dir_entry_guid)) {
269  av_log(s, AV_LOG_ERROR, "unknown guid "PRI_GUID", expected dir_entry_guid; "
270  "remaining directory entries ignored\n", ARG_GUID(buf));
271  break;
272  }
273  dir_length = AV_RL16(buf + 16);
274  file_length = AV_RL64(buf + 24);
275  name_size = 2 * AV_RL32(buf + 32);
276  if (name_size < 0) {
277  av_log(s, AV_LOG_ERROR,
278  "bad filename length, remaining directory entries ignored\n");
279  break;
280  }
281  if (48 + name_size > buf_end - buf) {
282  av_log(s, AV_LOG_ERROR, "filename exceeds buffer size; remaining directory entries ignored\n");
283  break;
284  }
285  first_sector = AV_RL32(buf + 40 + name_size);
286  depth = AV_RL32(buf + 44 + name_size);
287 
288  /* compare file name; test optional null terminator */
289  name = buf + 40;
290  if (name_size >= filename_size &&
291  !memcmp(name, filename, filename_size) &&
292  (name_size < filename_size + 2 || !AV_RN16(name + filename_size)))
293  return wtvfile_open_sector(first_sector, file_length, depth, s);
294 
295  buf += dir_length;
296  }
297  return 0;
298 }
299 
300 #define wtvfile_open(s, buf, buf_size, filename) \
301  wtvfile_open2(s, buf, buf_size, filename, sizeof(filename))
302 
306 static void wtvfile_close(AVIOContext *pb)
307 {
308  WtvFile *wf = pb->opaque;
309  av_free(wf->sectors);
310  av_free(wf);
311  av_free(pb->buffer);
312  av_free(pb);
313 }
314 
315 /*
316  *
317  * Main demuxer
318  *
319  */
320 
321 typedef struct {
323 } WtvStream;
324 
325 typedef struct {
327  int64_t epoch;
328  int64_t pts;
329  int64_t last_valid_pts;
331  /* maintain private seek index, as the AVIndexEntry->pos is relative to the
332  start of the 'timeline' file, not the file system (AVFormatContext->pb) */
336 } WtvContext;
337 
338 typedef struct {
339  enum CodecID id;
341 } AVCodecGuid;
342 
343 static enum CodecID ff_codec_guid_get_id(const AVCodecGuid *guids, ff_asf_guid guid)
344 {
345  int i;
346  for (i = 0; guids[i].id != CODEC_ID_NONE; i++) {
347  if (!ff_guidcmp(guids[i].guid, guid))
348  return guids[i].id;
349  }
350  return CODEC_ID_NONE;
351 }
352 
353 /* WTV GUIDs */
354 static const ff_asf_guid wtv_guid =
355  {0xB7,0xD8,0x00,0x20,0x37,0x49,0xDA,0x11,0xA6,0x4E,0x00,0x07,0xE9,0x5E,0xAD,0x8D};
357  {0x5A,0xFE,0xD7,0x6D,0xC8,0x1D,0x8F,0x4A,0x99,0x22,0xFA,0xB1,0x1C,0x38,0x14,0x53};
359  {0x5B,0x05,0xE6,0x1B,0x97,0xA9,0x49,0x43,0x88,0x17,0x1A,0x65,0x5A,0x29,0x8A,0x97};
360 static const ff_asf_guid data_guid =
361  {0x95,0xC3,0xD2,0xC2,0x7E,0x9A,0xDA,0x11,0x8B,0xF7,0x00,0x07,0xE9,0x5E,0xAD,0x8D};
362 static const ff_asf_guid stream_guid =
363  {0xED,0xA4,0x13,0x23,0x2D,0xBF,0x4F,0x45,0xAD,0x8A,0xD9,0x5B,0xA7,0xF9,0x1F,0xEE};
364 static const ff_asf_guid stream2_guid =
365  {0xA2,0xC3,0xD2,0xC2,0x7E,0x9A,0xDA,0x11,0x8B,0xF7,0x00,0x07,0xE9,0x5E,0xAD,0x8D};
367  {0x48,0xC0,0xCE,0x5D,0xB9,0xD0,0x63,0x41,0x87,0x2C,0x4F,0x32,0x22,0x3B,0xE8,0x8A};
369  {0x6D,0x66,0x92,0xE2,0x02,0x9C,0x8D,0x44,0xAA,0x8D,0x78,0x1A,0x93,0xFD,0xC3,0x95};
371  {0x1C,0xD4,0x7B,0x10,0xDA,0xA6,0x91,0x46,0x83,0x69,0x11,0xB2,0xCD,0xAA,0x28,0x8E};
373  {0xE6,0xA2,0xB4,0x3A,0x47,0x42,0x34,0x4B,0x89,0x6C,0x30,0xAF,0xA5,0xD2,0x1C,0x24};
375  {0xD9,0x79,0xE7,0xEf,0xF0,0x97,0x86,0x47,0x80,0x0D,0x95,0xCF,0x50,0x5D,0xDC,0x66};
377  {0xC4,0xE1,0xD4,0x4B,0xA1,0x90,0x09,0x41,0x82,0x36,0x27,0xF0,0x0E,0x7D,0xCC,0x5B};
379  {0x68,0xAB,0xF1,0xCA,0x53,0xE1,0x41,0x4D,0xA6,0xB3,0xA7,0xC9,0x98,0xDB,0x75,0xEE};
381  {0x50,0xD9,0x99,0x95,0x33,0x5F,0x17,0x46,0xAF,0x7C,0x1E,0x54,0xB5,0x10,0xDA,0xA3};
383  {0xBE,0xBF,0x1C,0x50,0x49,0xB8,0xCE,0x42,0x9B,0xE9,0x3D,0xB8,0x69,0xFB,0x82,0xB3};
384 
385 /* Windows media GUIDs */
386 
387 #define MEDIASUBTYPE_BASE_GUID \
388  0x00,0x00,0x10,0x00,0x80,0x00,0x00,0xAA,0x00,0x38,0x9B,0x71
389 
390 /* Media types */
392  {'a','u','d','s',MEDIASUBTYPE_BASE_GUID};
394  {'v','i','d','s',MEDIASUBTYPE_BASE_GUID};
396  {0x81,0xEB,0x36,0xE4,0x4F,0x52,0xCE,0x11,0x9F,0x53,0x00,0x20,0xAF,0x0B,0xA7,0x70};
398  {0x6C,0x17,0x5F,0x45,0x06,0x4B,0xCE,0x47,0x9A,0xEF,0x8C,0xAE,0xF7,0x3D,0xF7,0xB5};
400  {0x20,0x80,0x6D,0xE0,0x46,0xDB,0xCF,0x11,0xB4,0xD1,0x00,0x80,0x5F,0x6C,0xBB,0xEA};
402  {0x89,0x8A,0x8B,0xB8,0x49,0xB0,0x80,0x4C,0xAD,0xCF,0x58,0x98,0x98,0x5E,0x22,0xC1};
403 
404 /* Media subtypes */
406  {0x28,0xBD,0xAD,0x46,0xD0,0x6F,0x96,0x47,0x93,0xB2,0x15,0x5C,0x51,0xDC,0x04,0x8D};
408  {0xC3,0xCB,0xFF,0x34,0xB3,0xD5,0x71,0x41,0x90,0x02,0xD4,0xC6,0x03,0x01,0x69,0x7F};
410  {0xE3,0x76,0x2A,0xF7,0x0A,0xEB,0xD0,0x11,0xAC,0xE4,0x00,0x00,0xC0,0xCC,0x16,0xBA};
412  {0xAA,0xDD,0x2A,0xF5,0xF0,0x36,0xF5,0x43,0x95,0xEA,0x6D,0x86,0x64,0x84,0x26,0x2A};
414  {0x79,0x85,0x9F,0x4A,0xF8,0x6B,0x92,0x43,0x8A,0x6D,0xD2,0xDD,0x09,0xFA,0x78,0x61};
415 
416 /* Formats */
418  {0x6F,0xB3,0x39,0x67,0x5F,0x1D,0xC2,0x4A,0x81,0x92,0x28,0xBB,0x0E,0x73,0xD1,0x6A};
420  {0x81,0x9F,0x58,0x05,0x56,0xC3,0xCE,0x11,0xBF,0x01,0x00,0xAA,0x00,0x55,0x59,0x5A};
422  {0xA0,0x76,0x2A,0xF7,0x0A,0xEB,0xD0,0x11,0xAC,0xE4,0x00,0x00,0xC0,0xCC,0x16,0xBA};
424  {0xE3,0x80,0x6D,0xE0,0x46,0xDB,0xCF,0x11,0xB4,0xD1,0x00,0x80,0x5F,0x6C,0xBB,0xEA};
425 static const ff_asf_guid format_none =
426  {0xD6,0x17,0x64,0x0F,0x18,0xC3,0xD0,0x11,0xA4,0x3F,0x00,0xA0,0xC9,0x22,0x31,0x96};
427 
428 static const AVCodecGuid video_guids[] = {
429  {CODEC_ID_MPEG2VIDEO, {0x26,0x80,0x6D,0xE0,0x46,0xDB,0xCF,0x11,0xB4,0xD1,0x00,0x80,0x5F,0x6C,0xBB,0xEA}},
430  {CODEC_ID_NONE}
431 };
432 
433 static const AVCodecGuid audio_guids[] = {
434  {CODEC_ID_AC3, {0x2C,0x80,0x6D,0xE0,0x46,0xDB,0xCF,0x11,0xB4,0xD1,0x00,0x80,0x5F,0x6C,0xBB,0xEA}},
435  {CODEC_ID_EAC3, {0xAF,0x87,0xFB,0xA7,0x02,0x2D,0xFB,0x42,0xA4,0xD4,0x05,0xCD,0x93,0x84,0x3B,0xDD}},
436  {CODEC_ID_MP2, {0x2B,0x80,0x6D,0xE0,0x46,0xDB,0xCF,0x11,0xB4,0xD1,0x00,0x80,0x5F,0x6C,0xBB,0xEA}},
437  {CODEC_ID_NONE}
438 };
439 
440 static int read_probe(AVProbeData *p)
441 {
442  return ff_guidcmp(p->buf, wtv_guid) ? 0 : AVPROBE_SCORE_MAX;
443 }
444 
448 static void filetime_to_iso8601(char *buf, int buf_size, int64_t value)
449 {
450  time_t t = (value / 10000000LL) - 11644473600LL;
451  strftime(buf, buf_size, "%Y-%m-%d %H:%M:%S", gmtime(&t));
452 }
453 
457 static void crazytime_to_iso8601(char *buf, int buf_size, int64_t value)
458 {
459  time_t t = (value / 10000000LL) - 719162LL*86400LL;
460  strftime(buf, buf_size, "%Y-%m-%d %H:%M:%S", gmtime(&t));
461 }
462 
466 static void oledate_to_iso8601(char *buf, int buf_size, int64_t value)
467 {
468  time_t t = 631112400LL + 86400*av_int2double(value);
469  strftime(buf, buf_size, "%Y-%m-%d %H:%M:%S", gmtime(&t));
470 }
471 
472 static void get_attachment(AVFormatContext *s, AVIOContext *pb, int length)
473 {
474  char mime[1024];
475  char description[1024];
476  unsigned int filesize;
477  AVStream *st;
478  int64_t pos = avio_tell(pb);
479 
480  avio_get_str16le(pb, INT_MAX, mime, sizeof(mime));
481  if (strcmp(mime, "image/jpeg"))
482  goto done;
483 
484  avio_r8(pb);
485  avio_get_str16le(pb, INT_MAX, description, sizeof(description));
486  filesize = avio_rl32(pb);
487  if (!filesize)
488  goto done;
489 
490  st = avformat_new_stream(s, NULL);
491  if (!st)
492  goto done;
493  av_dict_set(&st->metadata, "title", description, 0);
496  st->codec->extradata = av_mallocz(filesize);
497  st->id = -1;
498  if (!st->codec->extradata)
499  goto done;
500  st->codec->extradata_size = filesize;
501  avio_read(pb, st->codec->extradata, filesize);
502 done:
503  avio_seek(pb, pos + length, SEEK_SET);
504 }
505 
506 static void get_tag(AVFormatContext *s, AVIOContext *pb, const char *key, int type, int length)
507 {
508  int buf_size = FFMAX(2*length, LEN_PRETTY_GUID) + 1;
509  char *buf = av_malloc(buf_size);
510  if (!buf)
511  return;
512 
513  if (type == 0 && length == 4) {
514  snprintf(buf, buf_size, "%"PRIi32, avio_rl32(pb));
515  } else if (type == 1) {
516  avio_get_str16le(pb, length, buf, buf_size);
517  if (!strlen(buf)) {
518  av_free(buf);
519  return;
520  }
521  } else if (type == 3 && length == 4) {
522  strcpy(buf, avio_rl32(pb) ? "true" : "false");
523  } else if (type == 4 && length == 8) {
524  int64_t num = avio_rl64(pb);
525  if (!strcmp(key, "WM/EncodingTime") ||
526  !strcmp(key, "WM/MediaOriginalBroadcastDateTime"))
527  filetime_to_iso8601(buf, buf_size, num);
528  else if (!strcmp(key, "WM/WMRVEncodeTime") ||
529  !strcmp(key, "WM/WMRVEndTime"))
530  crazytime_to_iso8601(buf, buf_size, num);
531  else if (!strcmp(key, "WM/WMRVExpirationDate"))
532  oledate_to_iso8601(buf, buf_size, num);
533  else if (!strcmp(key, "WM/WMRVBitrate"))
534  snprintf(buf, buf_size, "%f", av_int2double(num));
535  else
536  snprintf(buf, buf_size, "%"PRIi64, num);
537  } else if (type == 5 && length == 2) {
538  snprintf(buf, buf_size, "%"PRIi16, avio_rl16(pb));
539  } else if (type == 6 && length == 16) {
540  ff_asf_guid guid;
541  avio_read(pb, guid, 16);
542  snprintf(buf, buf_size, PRI_PRETTY_GUID, ARG_PRETTY_GUID(guid));
543  } else if (type == 2 && !strcmp(key, "WM/Picture")) {
544  get_attachment(s, pb, length);
545  av_freep(&buf);
546  return;
547  } else {
548  av_freep(&buf);
549  av_log(s, AV_LOG_WARNING, "unsupported metadata entry; key:%s, type:%d, length:0x%x\n", key, type, length);
550  avio_skip(pb, length);
551  return;
552  }
553 
554  av_dict_set(&s->metadata, key, buf, 0);
555  av_freep(&buf);
556 }
557 
562 {
563  ff_asf_guid guid;
564  int length, type;
565  while(!pb->eof_reached) {
566  char key[1024];
567  ff_get_guid(pb, &guid);
568  type = avio_rl32(pb);
569  length = avio_rl32(pb);
570  if (!length)
571  break;
572  if (ff_guidcmp(&guid, metadata_guid)) {
573  av_log(s, AV_LOG_WARNING, "unknown guid "PRI_GUID", expected metadata_guid; "
574  "remaining metadata entries ignored\n", ARG_GUID(guid));
575  break;
576  }
577  avio_get_str16le(pb, INT_MAX, key, sizeof(key));
578  get_tag(s, pb, key, type, length);
579  }
580 
582 }
583 
589 {
590  WtvContext *wtv = s->priv_data;
591  AVIOContext *pb = wtv->pb;
592 
593  avio_skip(pb, 72); // picture aspect ratio is unreliable
594  ff_get_bmp_header(pb, st);
595 
596  return 72 + 40;
597 }
598 
603 {
604  /* fwHeadLayer */
605  switch (AV_RL16(st->codec->extradata)) {
606  case 0x0001 : st->codec->codec_id = CODEC_ID_MP1; break;
607  case 0x0002 : st->codec->codec_id = CODEC_ID_MP2; break;
608  case 0x0004 : st->codec->codec_id = CODEC_ID_MP3; break;
609  }
610 
611  st->codec->bit_rate = AV_RL32(st->codec->extradata + 2); /* dwHeadBitrate */
612 
613  /* dwHeadMode */
614  switch (AV_RL16(st->codec->extradata + 6)) {
615  case 1 : case 2 : case 4 : st->codec->channels = 2; break;
616  case 8 : st->codec->channels = 1; break;
617  }
618 }
619 
625 static AVStream * new_stream(AVFormatContext *s, AVStream *st, int sid, int codec_type)
626 {
627  if (st) {
628  if (st->codec->extradata) {
629  av_freep(&st->codec->extradata);
630  st->codec->extradata_size = 0;
631  }
632  } else {
633  WtvStream *wst = av_mallocz(sizeof(WtvStream));
634  if (!wst)
635  return NULL;
636  st = avformat_new_stream(s, NULL);
637  if (!st)
638  return NULL;
639  st->id = sid;
640  st->priv_data = wst;
641  }
642  st->codec->codec_type = codec_type;
644  avpriv_set_pts_info(st, 64, 1, 10000000);
645  return st;
646 }
647 
658  ff_asf_guid mediatype, ff_asf_guid subtype,
659  ff_asf_guid formattype, int size)
660 {
661  WtvContext *wtv = s->priv_data;
662  AVIOContext *pb = wtv->pb;
664  !ff_guidcmp(formattype, format_cpfilters_processed)) {
665  ff_asf_guid actual_subtype;
666  ff_asf_guid actual_formattype;
667 
668  if (size < 32) {
669  av_log(s, AV_LOG_WARNING, "format buffer size underflow\n");
670  avio_skip(pb, size);
671  return NULL;
672  }
673 
674  avio_skip(pb, size - 32);
675  ff_get_guid(pb, &actual_subtype);
676  ff_get_guid(pb, &actual_formattype);
677  avio_seek(pb, -size, SEEK_CUR);
678 
679  st = parse_media_type(s, st, sid, mediatype, actual_subtype, actual_formattype, size - 32);
680  avio_skip(pb, 32);
681  return st;
682  } else if (!ff_guidcmp(mediatype, mediatype_audio)) {
683  st = new_stream(s, st, sid, AVMEDIA_TYPE_AUDIO);
684  if (!st)
685  return NULL;
686  if (!ff_guidcmp(formattype, format_waveformatex)) {
687  int ret = ff_get_wav_header(pb, st->codec, size);
688  if (ret < 0)
689  return NULL;
690  } else {
691  if (ff_guidcmp(formattype, format_none))
692  av_log(s, AV_LOG_WARNING, "unknown formattype:"PRI_GUID"\n", ARG_GUID(formattype));
693  avio_skip(pb, size);
694  }
695 
696  if (!memcmp(subtype + 4, (const uint8_t[]){MEDIASUBTYPE_BASE_GUID}, 12)) {
698  } else if (!ff_guidcmp(subtype, mediasubtype_mpeg1payload)) {
699  if (st->codec->extradata && st->codec->extradata_size >= 22)
701  else
702  av_log(s, AV_LOG_WARNING, "MPEG1WAVEFORMATEX underflow\n");
703  } else {
704  st->codec->codec_id = ff_codec_guid_get_id(audio_guids, subtype);
705  if (st->codec->codec_id == CODEC_ID_NONE)
706  av_log(s, AV_LOG_WARNING, "unknown subtype:"PRI_GUID"\n", ARG_GUID(subtype));
707  }
708  return st;
709  } else if (!ff_guidcmp(mediatype, mediatype_video)) {
710  st = new_stream(s, st, sid, AVMEDIA_TYPE_VIDEO);
711  if (!st)
712  return NULL;
713  if (!ff_guidcmp(formattype, format_videoinfo2)) {
714  int consumed = parse_videoinfoheader2(s, st);
715  avio_skip(pb, FFMAX(size - consumed, 0));
716  } else if (!ff_guidcmp(formattype, format_mpeg2_video)) {
717  int consumed = parse_videoinfoheader2(s, st);
718  avio_skip(pb, FFMAX(size - consumed, 0));
719  } else {
720  if (ff_guidcmp(formattype, format_none))
721  av_log(s, AV_LOG_WARNING, "unknown formattype:"PRI_GUID"\n", ARG_GUID(formattype));
722  avio_skip(pb, size);
723  }
724 
725  if (!memcmp(subtype + 4, (const uint8_t[]){MEDIASUBTYPE_BASE_GUID}, 12)) {
727  } else {
728  st->codec->codec_id = ff_codec_guid_get_id(video_guids, subtype);
729  }
730  if (st->codec->codec_id == CODEC_ID_NONE)
731  av_log(s, AV_LOG_WARNING, "unknown subtype:"PRI_GUID"\n", ARG_GUID(subtype));
732  return st;
733  } else if (!ff_guidcmp(mediatype, mediatype_mpeg2_pes) &&
735  st = new_stream(s, st, sid, AVMEDIA_TYPE_SUBTITLE);
736  if (!st)
737  return NULL;
738  if (ff_guidcmp(formattype, format_none))
739  av_log(s, AV_LOG_WARNING, "unknown formattype:"PRI_GUID"\n", ARG_GUID(formattype));
740  avio_skip(pb, size);
742  return st;
743  } else if (!ff_guidcmp(mediatype, mediatype_mstvcaption) &&
745  st = new_stream(s, st, sid, AVMEDIA_TYPE_SUBTITLE);
746  if (!st)
747  return NULL;
748  if (ff_guidcmp(formattype, format_none))
749  av_log(s, AV_LOG_WARNING, "unknown formattype:"PRI_GUID"\n", ARG_GUID(formattype));
750  avio_skip(pb, size);
752  return st;
753  } else if (!ff_guidcmp(mediatype, mediatype_mpeg2_sections) &&
755  if (ff_guidcmp(formattype, format_none))
756  av_log(s, AV_LOG_WARNING, "unknown formattype:"PRI_GUID"\n", ARG_GUID(formattype));
757  avio_skip(pb, size);
758  return NULL;
759  }
760 
761  av_log(s, AV_LOG_WARNING, "unknown media type, mediatype:"PRI_GUID
762  ", subtype:"PRI_GUID", formattype:"PRI_GUID"\n",
763  ARG_GUID(mediatype), ARG_GUID(subtype), ARG_GUID(formattype));
764  avio_skip(pb, size);
765  return NULL;
766 }
767 
768 enum {
771 };
772 
780 static int parse_chunks(AVFormatContext *s, int mode, int64_t seekts, int *len_ptr)
781 {
782  WtvContext *wtv = s->priv_data;
783  AVIOContext *pb = wtv->pb;
784  while (!pb->eof_reached) {
785  ff_asf_guid g;
786  int len, sid, consumed;
787 
788  ff_get_guid(pb, &g);
789  len = avio_rl32(pb);
790  if (len < 32)
791  break;
792  sid = avio_rl32(pb) & 0x7FFF;
793  avio_skip(pb, 8);
794  consumed = 32;
795 
796  if (!ff_guidcmp(g, stream_guid)) {
797  if (ff_find_stream_index(s, sid) < 0) {
798  ff_asf_guid mediatype, subtype, formattype;
799  int size;
800  avio_skip(pb, 28);
801  ff_get_guid(pb, &mediatype);
802  ff_get_guid(pb, &subtype);
803  avio_skip(pb, 12);
804  ff_get_guid(pb, &formattype);
805  size = avio_rl32(pb);
806  parse_media_type(s, 0, sid, mediatype, subtype, formattype, size);
807  consumed += 92 + size;
808  }
809  } else if (!ff_guidcmp(g, stream2_guid)) {
810  int stream_index = ff_find_stream_index(s, sid);
811  if (stream_index >= 0 && !((WtvStream*)s->streams[stream_index]->priv_data)->seen_data) {
812  ff_asf_guid mediatype, subtype, formattype;
813  int size;
814  avio_skip(pb, 12);
815  ff_get_guid(pb, &mediatype);
816  ff_get_guid(pb, &subtype);
817  avio_skip(pb, 12);
818  ff_get_guid(pb, &formattype);
819  size = avio_rl32(pb);
820  parse_media_type(s, s->streams[stream_index], sid, mediatype, subtype, formattype, size);
821  consumed += 76 + size;
822  }
829  int stream_index = ff_find_stream_index(s, sid);
830  if (stream_index >= 0) {
831  AVStream *st = s->streams[stream_index];
832  uint8_t buf[258];
833  const uint8_t *pbuf = buf;
834  int buf_size;
835 
836  avio_skip(pb, 8);
837  consumed += 8;
840  avio_skip(pb, 6);
841  consumed += 6;
842  }
843 
844  buf_size = FFMIN(len - consumed, sizeof(buf));
845  avio_read(pb, buf, buf_size);
846  consumed += buf_size;
847  ff_parse_mpeg2_descriptor(s, st, 0, &pbuf, buf + buf_size, NULL, 0, 0, NULL);
848  }
849  } else if (!ff_guidcmp(g, EVENTID_AudioTypeSpanningEvent)) {
850  int stream_index = ff_find_stream_index(s, sid);
851  if (stream_index >= 0) {
852  AVStream *st = s->streams[stream_index];
853  int audio_type;
854  avio_skip(pb, 8);
855  audio_type = avio_r8(pb);
856  if (audio_type == 2)
858  else if (audio_type == 3)
860  consumed += 9;
861  }
863  int stream_index = ff_find_stream_index(s, sid);
864  if (stream_index >= 0) {
865  avio_skip(pb, 12);
866  if (avio_rl32(pb))
867  av_log(s, AV_LOG_WARNING, "DVB scrambled stream detected (st:%d), decoding will likely fail\n", stream_index);
868  consumed += 16;
869  }
870  } else if (!ff_guidcmp(g, EVENTID_LanguageSpanningEvent)) {
871  int stream_index = ff_find_stream_index(s, sid);
872  if (stream_index >= 0) {
873  AVStream *st = s->streams[stream_index];
874  uint8_t language[4];
875  avio_skip(pb, 12);
876  avio_read(pb, language, 3);
877  if (language[0]) {
878  language[3] = 0;
879  av_dict_set(&st->metadata, "language", language, 0);
880  if (!strcmp(language, "nar") || !strcmp(language, "NAR"))
882  }
883  consumed += 15;
884  }
885  } else if (!ff_guidcmp(g, timestamp_guid)) {
886  int stream_index = ff_find_stream_index(s, sid);
887  if (stream_index >= 0) {
888  avio_skip(pb, 8);
889  wtv->pts = avio_rl64(pb);
890  consumed += 16;
891  if (wtv->pts == -1)
892  wtv->pts = AV_NOPTS_VALUE;
893  else {
894  wtv->last_valid_pts = wtv->pts;
895  if (wtv->epoch == AV_NOPTS_VALUE || wtv->pts < wtv->epoch)
896  wtv->epoch = wtv->pts;
897  if (mode == SEEK_TO_PTS && wtv->pts >= seekts) {
898 #define WTV_PAD8(x) (((x) + 7) & ~7)
899  avio_skip(pb, WTV_PAD8(len) - consumed);
900  return 0;
901  }
902  }
903  }
904  } else if (!ff_guidcmp(g, data_guid)) {
905  int stream_index = ff_find_stream_index(s, sid);
906  if (mode == SEEK_TO_DATA && stream_index >= 0 && len > 32) {
907  WtvStream *wst = s->streams[stream_index]->priv_data;
908  wst->seen_data = 1;
909  if (len_ptr) {
910  *len_ptr = len;
911  }
912  return stream_index;
913  }
914  } else if (
915  !ff_guidcmp(g, /* DSATTRIB_CAPTURE_STREAMTIME */ (const ff_asf_guid){0x14,0x56,0x1A,0x0C,0xCD,0x30,0x40,0x4F,0xBC,0xBF,0xD0,0x3E,0x52,0x30,0x62,0x07}) ||
916  !ff_guidcmp(g, /* DSATTRIB_PicSampleSeq */ (const ff_asf_guid){0x02,0xAE,0x5B,0x2F,0x8F,0x7B,0x60,0x4F,0x82,0xD6,0xE4,0xEA,0x2F,0x1F,0x4C,0x99}) ||
917  !ff_guidcmp(g, /* DSATTRIB_TRANSPORT_PROPERTIES */ (const ff_asf_guid){0x12,0xF6,0x22,0xB6,0xAD,0x47,0x71,0x46,0xAD,0x6C,0x05,0xA9,0x8E,0x65,0xDE,0x3A}) ||
918  !ff_guidcmp(g, /* dvr_ms_vid_frame_rep_data */ (const ff_asf_guid){0xCC,0x32,0x64,0xDD,0x29,0xE2,0xDB,0x40,0x80,0xF6,0xD2,0x63,0x28,0xD2,0x76,0x1F}) ||
919  !ff_guidcmp(g, /* EVENTID_ChannelChangeSpanningEvent */ (const ff_asf_guid){0xE5,0xC5,0x67,0x90,0x5C,0x4C,0x05,0x42,0x86,0xC8,0x7A,0xFE,0x20,0xFE,0x1E,0xFA}) ||
920  !ff_guidcmp(g, /* EVENTID_ChannelInfoSpanningEvent */ (const ff_asf_guid){0x80,0x6D,0xF3,0x41,0x32,0x41,0xC2,0x4C,0xB1,0x21,0x01,0xA4,0x32,0x19,0xD8,0x1B}) ||
921  !ff_guidcmp(g, /* EVENTID_ChannelTypeSpanningEvent */ (const ff_asf_guid){0x51,0x1D,0xAB,0x72,0xD2,0x87,0x9B,0x48,0xBA,0x11,0x0E,0x08,0xDC,0x21,0x02,0x43}) ||
922  !ff_guidcmp(g, /* EVENTID_PIDListSpanningEvent */ (const ff_asf_guid){0x65,0x8F,0xFC,0x47,0xBB,0xE2,0x34,0x46,0x9C,0xEF,0xFD,0xBF,0xE6,0x26,0x1D,0x5C}) ||
923  !ff_guidcmp(g, /* EVENTID_SignalAndServiceStatusSpanningEvent */ (const ff_asf_guid){0xCB,0xC5,0x68,0x80,0x04,0x3C,0x2B,0x49,0xB4,0x7D,0x03,0x08,0x82,0x0D,0xCE,0x51}) ||
924  !ff_guidcmp(g, /* EVENTID_StreamTypeSpanningEvent */ (const ff_asf_guid){0xBC,0x2E,0xAF,0x82,0xA6,0x30,0x64,0x42,0xA8,0x0B,0xAD,0x2E,0x13,0x72,0xAC,0x60}) ||
925  !ff_guidcmp(g, (const ff_asf_guid){0x1E,0xBE,0xC3,0xC5,0x43,0x92,0xDC,0x11,0x85,0xE5,0x00,0x12,0x3F,0x6F,0x73,0xB9}) ||
926  !ff_guidcmp(g, (const ff_asf_guid){0x3B,0x86,0xA2,0xB1,0xEB,0x1E,0xC3,0x44,0x8C,0x88,0x1C,0xA3,0xFF,0xE3,0xE7,0x6A}) ||
927  !ff_guidcmp(g, (const ff_asf_guid){0x4E,0x7F,0x4C,0x5B,0xC4,0xD0,0x38,0x4B,0xA8,0x3E,0x21,0x7F,0x7B,0xBF,0x52,0xE7}) ||
928  !ff_guidcmp(g, (const ff_asf_guid){0x63,0x36,0xEB,0xFE,0xA1,0x7E,0xD9,0x11,0x83,0x08,0x00,0x07,0xE9,0x5E,0xAD,0x8D}) ||
929  !ff_guidcmp(g, (const ff_asf_guid){0x70,0xE9,0xF1,0xF8,0x89,0xA4,0x4C,0x4D,0x83,0x73,0xB8,0x12,0xE0,0xD5,0xF8,0x1E}) ||
930  !ff_guidcmp(g, (const ff_asf_guid){0x96,0xC3,0xD2,0xC2,0x7E,0x9A,0xDA,0x11,0x8B,0xF7,0x00,0x07,0xE9,0x5E,0xAD,0x8D}) ||
931  !ff_guidcmp(g, (const ff_asf_guid){0x97,0xC3,0xD2,0xC2,0x7E,0x9A,0xDA,0x11,0x8B,0xF7,0x00,0x07,0xE9,0x5E,0xAD,0x8D}) ||
932  !ff_guidcmp(g, (const ff_asf_guid){0xA1,0xC3,0xD2,0xC2,0x7E,0x9A,0xDA,0x11,0x8B,0xF7,0x00,0x07,0xE9,0x5E,0xAD,0x8D})) {
933  //ignore known guids
934  } else
935  av_log(s, AV_LOG_WARNING, "unsupported chunk:"PRI_GUID"\n", ARG_GUID(g));
936 
937  avio_skip(pb, WTV_PAD8(len) - consumed);
938  }
939  return AVERROR_EOF;
940 }
941 
942 /* declare utf16le strings */
943 #define _ , 0,
944 static const uint8_t timeline_le16[] =
945  {'t'_'i'_'m'_'e'_'l'_'i'_'n'_'e', 0};
946 static const uint8_t table_0_entries_legacy_attrib_le16[] =
947  {'t'_'a'_'b'_'l'_'e'_'.'_'0'_'.'_'e'_'n'_'t'_'r'_'i'_'e'_'s'_'.'_'l'_'e'_'g'_'a'_'c'_'y'_'_'_'a'_'t'_'t'_'r'_'i'_'b', 0};
948 static const uint8_t table_0_entries_time_le16[] =
949  {'t'_'a'_'b'_'l'_'e'_'.'_'0'_'.'_'e'_'n'_'t'_'r'_'i'_'e'_'s'_'.'_'t'_'i'_'m'_'e', 0};
950 static const uint8_t timeline_table_0_entries_Events_le16[] =
951  {'t'_'i'_'m'_'e'_'l'_'i'_'n'_'e'_'.'_'t'_'a'_'b'_'l'_'e'_'.'_'0'_'.'_'e'_'n'_'t'_'r'_'i'_'e'_'s'_'.'_'E'_'v'_'e'_'n'_'t'_'s', 0};
952 #undef _
953 
955 {
956  WtvContext *wtv = s->priv_data;
957  int root_sector, root_size;
958  uint8_t root[WTV_SECTOR_SIZE];
959  AVIOContext *pb;
960  int64_t timeline_pos;
961  int ret;
962 
963  wtv->epoch =
964  wtv->pts =
966 
967  /* read root directory sector */
968  avio_skip(s->pb, 0x30);
969  root_size = avio_rl32(s->pb);
970  if (root_size > sizeof(root)) {
971  av_log(s, AV_LOG_ERROR, "root directory size exceeds sector size\n");
972  return AVERROR_INVALIDDATA;
973  }
974  avio_skip(s->pb, 4);
975  root_sector = avio_rl32(s->pb);
976 
977  avio_seek(s->pb, root_sector << WTV_SECTOR_BITS, SEEK_SET);
978  root_size = avio_read(s->pb, root, root_size);
979  if (root_size < 0)
980  return AVERROR_INVALIDDATA;
981 
982  /* parse chunks up until first data chunk */
983  wtv->pb = wtvfile_open(s, root, root_size, timeline_le16);
984  if (!wtv->pb) {
985  av_log(s, AV_LOG_ERROR, "timeline data missing\n");
986  return AVERROR_INVALIDDATA;
987  }
988 
989  ret = parse_chunks(s, SEEK_TO_DATA, 0, 0);
990  if (ret < 0)
991  return ret;
992  avio_seek(wtv->pb, -32, SEEK_CUR);
993 
994  timeline_pos = avio_tell(s->pb); // save before opening another file
995 
996  /* read metadata */
997  pb = wtvfile_open(s, root, root_size, table_0_entries_legacy_attrib_le16);
998  if (pb) {
999  parse_legacy_attrib(s, pb);
1000  wtvfile_close(pb);
1001  }
1002 
1003  /* read seek index */
1004  if (s->nb_streams) {
1005  AVStream *st = s->streams[0];
1006  pb = wtvfile_open(s, root, root_size, table_0_entries_time_le16);
1007  if (pb) {
1008  while(1) {
1009  uint64_t timestamp = avio_rl64(pb);
1010  uint64_t frame_nb = avio_rl64(pb);
1011  if (pb->eof_reached)
1012  break;
1014  0, timestamp, frame_nb, 0, AVINDEX_KEYFRAME);
1015  }
1016  wtvfile_close(pb);
1017 
1018  if (wtv->nb_index_entries) {
1019  pb = wtvfile_open(s, root, root_size, timeline_table_0_entries_Events_le16);
1020  if (pb) {
1021  int i;
1022  while (1) {
1023  uint64_t frame_nb = avio_rl64(pb);
1024  uint64_t position = avio_rl64(pb);
1025  if (pb->eof_reached)
1026  break;
1027  for (i = wtv->nb_index_entries - 1; i >= 0; i--) {
1028  AVIndexEntry *e = wtv->index_entries + i;
1029  if (frame_nb > e->size)
1030  break;
1031  if (position > e->pos)
1032  e->pos = position;
1033  }
1034  }
1035  wtvfile_close(pb);
1036  st->duration = wtv->index_entries[wtv->nb_index_entries - 1].timestamp;
1037  }
1038  }
1039  }
1040  }
1041 
1042  avio_seek(s->pb, timeline_pos, SEEK_SET);
1043  return 0;
1044 }
1045 
1047 {
1048  WtvContext *wtv = s->priv_data;
1049  AVIOContext *pb = wtv->pb;
1050  int stream_index, len, ret;
1051 
1052  stream_index = parse_chunks(s, SEEK_TO_DATA, 0, &len);
1053  if (stream_index < 0)
1054  return stream_index;
1055 
1056  ret = av_get_packet(pb, pkt, len - 32);
1057  if (ret < 0)
1058  return ret;
1059  pkt->stream_index = stream_index;
1060  pkt->pts = wtv->pts;
1061  avio_skip(pb, WTV_PAD8(len) - len);
1062  return 0;
1063 }
1064 
1065 static int read_seek(AVFormatContext *s, int stream_index,
1066  int64_t ts, int flags)
1067 {
1068  WtvContext *wtv = s->priv_data;
1069  AVIOContext *pb = wtv->pb;
1070  AVStream *st = s->streams[0];
1071  int64_t ts_relative;
1072  int i;
1073 
1074  if ((flags & AVSEEK_FLAG_FRAME) || (flags & AVSEEK_FLAG_BYTE))
1075  return AVERROR(ENOSYS);
1076 
1077  /* timestamp adjustment is required because wtv->pts values are absolute,
1078  * whereas AVIndexEntry->timestamp values are relative to epoch. */
1079  ts_relative = ts;
1080  if (wtv->epoch != AV_NOPTS_VALUE)
1081  ts_relative -= wtv->epoch;
1082 
1083  i = ff_index_search_timestamp(wtv->index_entries, wtv->nb_index_entries, ts_relative, flags);
1084  if (i < 0) {
1085  if (wtv->last_valid_pts == AV_NOPTS_VALUE || ts < wtv->last_valid_pts)
1086  avio_seek(pb, 0, SEEK_SET);
1087  else if (st->duration != AV_NOPTS_VALUE && ts_relative > st->duration && wtv->nb_index_entries)
1088  avio_seek(pb, wtv->index_entries[wtv->nb_index_entries - 1].pos, SEEK_SET);
1089  if (parse_chunks(s, SEEK_TO_PTS, ts, 0) < 0)
1090  return AVERROR(ERANGE);
1091  return 0;
1092  }
1093  wtv->pts = wtv->index_entries[i].timestamp;
1094  if (wtv->epoch != AV_NOPTS_VALUE)
1095  wtv->pts += wtv->epoch;
1096  wtv->last_valid_pts = wtv->pts;
1097  avio_seek(pb, wtv->index_entries[i].pos, SEEK_SET);
1098  return 0;
1099 }
1100 
1102 {
1103  WtvContext *wtv = s->priv_data;
1104  av_free(wtv->index_entries);
1105  wtvfile_close(wtv->pb);
1106  return 0;
1107 }
1108 
1110  .name = "wtv",
1111  .long_name = NULL_IF_CONFIG_SMALL("Windows Television (WTV)"),
1112  .priv_data_size = sizeof(WtvContext),
1116  .read_seek = read_seek,
1118  .flags = AVFMT_SHOW_IDS,
1119 };