You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1129 lines
41KB

  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. /**
  22. * @file
  23. * Windows Television (WTV) demuxer
  24. * @author Peter Ross <pross@xvid.org>
  25. */
  26. #include <inttypes.h>
  27. #include "libavutil/channel_layout.h"
  28. #include "libavutil/intreadwrite.h"
  29. #include "libavutil/intfloat.h"
  30. #include "libavutil/dict.h"
  31. #include "libavutil/time_internal.h"
  32. #include "avformat.h"
  33. #include "internal.h"
  34. #include "riff.h"
  35. #include "asf.h"
  36. #include "mpegts.h"
  37. /* Macros for formating GUIDs */
  38. #define PRI_PRETTY_GUID \
  39. "%08"PRIx32"-%04"PRIx16"-%04"PRIx16"-%02x%02x%02x%02x%02x%02x%02x%02x"
  40. #define ARG_PRETTY_GUID(g) \
  41. 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]
  42. #define LEN_PRETTY_GUID 34
  43. /*
  44. *
  45. * File system routines
  46. *
  47. */
  48. #define WTV_SECTOR_BITS 12
  49. #define WTV_SECTOR_SIZE (1 << WTV_SECTOR_BITS)
  50. #define WTV_BIGSECTOR_BITS 18
  51. #define SHIFT_SECTOR_BITS(a) ((int64_t)(a) << WTV_SECTOR_BITS)
  52. typedef struct WtvFile {
  53. AVIOContext *pb_filesystem; /** file system (AVFormatContext->pb) */
  54. int sector_bits; /** sector shift bits; used to convert sector number into pb_filesystem offset */
  55. uint32_t *sectors; /** file allocation table */
  56. int nb_sectors; /** number of sectors */
  57. int error;
  58. int64_t position;
  59. int64_t length;
  60. } WtvFile;
  61. static int64_t seek_by_sector(AVIOContext *pb, int64_t sector, int64_t offset)
  62. {
  63. return avio_seek(pb, SHIFT_SECTOR_BITS(sector) + offset, SEEK_SET);
  64. }
  65. /**
  66. * @return bytes read, 0 on end of file, or <0 on error
  67. */
  68. static int wtvfile_read_packet(void *opaque, uint8_t *buf, int buf_size)
  69. {
  70. WtvFile *wf = opaque;
  71. AVIOContext *pb = wf->pb_filesystem;
  72. int nread = 0;
  73. if (wf->error || pb->error)
  74. return -1;
  75. if (wf->position >= wf->length || pb->eof_reached)
  76. return 0;
  77. buf_size = FFMIN(buf_size, wf->length - wf->position);
  78. while(nread < buf_size) {
  79. int n;
  80. int remaining_in_sector = (1 << wf->sector_bits) - (wf->position & ((1 << wf->sector_bits) - 1));
  81. int read_request = FFMIN(buf_size - nread, remaining_in_sector);
  82. n = avio_read(pb, buf, read_request);
  83. if (n <= 0)
  84. break;
  85. nread += n;
  86. buf += n;
  87. wf->position += n;
  88. if (n == remaining_in_sector) {
  89. int i = wf->position >> wf->sector_bits;
  90. if (i >= wf->nb_sectors ||
  91. (wf->sectors[i] != wf->sectors[i - 1] + (1 << (wf->sector_bits - WTV_SECTOR_BITS)) &&
  92. seek_by_sector(pb, wf->sectors[i], 0) < 0)) {
  93. wf->error = 1;
  94. break;
  95. }
  96. }
  97. }
  98. return nread;
  99. }
  100. /**
  101. * @return position (or file length)
  102. */
  103. static int64_t wtvfile_seek(void *opaque, int64_t offset, int whence)
  104. {
  105. WtvFile *wf = opaque;
  106. AVIOContext *pb = wf->pb_filesystem;
  107. if (whence == AVSEEK_SIZE)
  108. return wf->length;
  109. else if (whence == SEEK_CUR)
  110. offset = wf->position + offset;
  111. else if (whence == SEEK_END)
  112. offset = wf->length;
  113. wf->error = offset < 0 || offset >= wf->length ||
  114. seek_by_sector(pb, wf->sectors[offset >> wf->sector_bits],
  115. offset & ((1 << wf->sector_bits) - 1)) < 0;
  116. wf->position = offset;
  117. return offset;
  118. }
  119. /**
  120. * read non-zero integers (le32) from input stream
  121. * @param pb
  122. * @param[out] data destination
  123. * @param count maximum number of integers to read
  124. * @return total number of integers read
  125. */
  126. static int read_ints(AVIOContext *pb, uint32_t *data, int count)
  127. {
  128. int i, total = 0;
  129. for (i = 0; i < count; i++) {
  130. if ((data[total] = avio_rl32(pb)))
  131. total++;
  132. }
  133. return total;
  134. }
  135. /**
  136. * Open file
  137. * @param first_sector First sector
  138. * @param length Length of file (bytes)
  139. * @param depth File allocation table depth
  140. * @return NULL on error
  141. */
  142. static AVIOContext * wtvfile_open_sector(int first_sector, uint64_t length, int depth, AVFormatContext *s)
  143. {
  144. AVIOContext *pb;
  145. WtvFile *wf;
  146. uint8_t *buffer;
  147. if (seek_by_sector(s->pb, first_sector, 0) < 0)
  148. return NULL;
  149. wf = av_mallocz(sizeof(WtvFile));
  150. if (!wf)
  151. return NULL;
  152. if (depth == 0) {
  153. wf->sectors = av_malloc(sizeof(uint32_t));
  154. if (!wf->sectors) {
  155. av_free(wf);
  156. return NULL;
  157. }
  158. wf->sectors[0] = first_sector;
  159. wf->nb_sectors = 1;
  160. wf->sector_bits = WTV_SECTOR_BITS;
  161. } else if (depth == 1) {
  162. wf->sectors = av_malloc(WTV_SECTOR_SIZE);
  163. if (!wf->sectors) {
  164. av_free(wf);
  165. return NULL;
  166. }
  167. wf->nb_sectors = read_ints(s->pb, wf->sectors, WTV_SECTOR_SIZE / 4);
  168. wf->sector_bits = length & (1ULL<<63) ? WTV_SECTOR_BITS : WTV_BIGSECTOR_BITS;
  169. } else if (depth == 2) {
  170. uint32_t sectors1[WTV_SECTOR_SIZE / 4];
  171. int nb_sectors1 = read_ints(s->pb, sectors1, WTV_SECTOR_SIZE / 4);
  172. int i;
  173. wf->sectors = av_malloc(SHIFT_SECTOR_BITS(nb_sectors1));
  174. if (!wf->sectors) {
  175. av_free(wf);
  176. return NULL;
  177. }
  178. wf->nb_sectors = 0;
  179. for (i = 0; i < nb_sectors1; i++) {
  180. if (seek_by_sector(s->pb, sectors1[i], 0) < 0)
  181. break;
  182. wf->nb_sectors += read_ints(s->pb, wf->sectors + i * WTV_SECTOR_SIZE / 4, WTV_SECTOR_SIZE / 4);
  183. }
  184. wf->sector_bits = length & (1ULL<<63) ? WTV_SECTOR_BITS : WTV_BIGSECTOR_BITS;
  185. } else {
  186. av_log(s, AV_LOG_ERROR, "unsupported file allocation table depth (0x%x)\n", depth);
  187. av_free(wf);
  188. return NULL;
  189. }
  190. if (!wf->nb_sectors) {
  191. av_free(wf->sectors);
  192. av_free(wf);
  193. return NULL;
  194. }
  195. /* check length */
  196. length &= 0xFFFFFFFFFFFF;
  197. if (length > ((int64_t)wf->nb_sectors << wf->sector_bits)) {
  198. 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);
  199. length = (int64_t)wf->nb_sectors << wf->sector_bits;
  200. }
  201. wf->length = length;
  202. /* seek to initial sector */
  203. wf->position = 0;
  204. if (seek_by_sector(s->pb, wf->sectors[0], 0) < 0) {
  205. av_free(wf->sectors);
  206. av_free(wf);
  207. return NULL;
  208. }
  209. wf->pb_filesystem = s->pb;
  210. buffer = av_malloc(1 << wf->sector_bits);
  211. if (!buffer) {
  212. av_free(wf->sectors);
  213. av_free(wf);
  214. return NULL;
  215. }
  216. pb = avio_alloc_context(buffer, 1 << wf->sector_bits, 0, wf,
  217. wtvfile_read_packet, NULL, wtvfile_seek);
  218. if (!pb) {
  219. av_free(buffer);
  220. av_free(wf->sectors);
  221. av_free(wf);
  222. }
  223. return pb;
  224. }
  225. static const ff_asf_guid dir_entry_guid =
  226. {0x92,0xB7,0x74,0x91,0x59,0x70,0x70,0x44,0x88,0xDF,0x06,0x3B,0x82,0xCC,0x21,0x3D};
  227. /**
  228. * Open file using filename
  229. * @param[in] buf directory buffer
  230. * @param buf_size directory buffer size
  231. * @param[in] filename
  232. * @param filename_size size of filename
  233. * @return NULL on error
  234. */
  235. static AVIOContext * wtvfile_open2(AVFormatContext *s, const uint8_t *buf, int buf_size, const uint8_t *filename, int filename_size)
  236. {
  237. const uint8_t *buf_end = buf + buf_size;
  238. while(buf + 48 <= buf_end) {
  239. int dir_length, name_size, first_sector, depth;
  240. uint64_t file_length;
  241. const uint8_t *name;
  242. if (ff_guidcmp(buf, dir_entry_guid)) {
  243. av_log(s, AV_LOG_ERROR, "unknown guid "FF_PRI_GUID", expected dir_entry_guid; "
  244. "remaining directory entries ignored\n", FF_ARG_GUID(buf));
  245. break;
  246. }
  247. dir_length = AV_RL16(buf + 16);
  248. file_length = AV_RL64(buf + 24);
  249. name_size = 2 * AV_RL32(buf + 32);
  250. if (name_size < 0) {
  251. av_log(s, AV_LOG_ERROR,
  252. "bad filename length, remaining directory entries ignored\n");
  253. break;
  254. }
  255. if (48 + name_size > buf_end - buf) {
  256. av_log(s, AV_LOG_ERROR, "filename exceeds buffer size; remaining directory entries ignored\n");
  257. break;
  258. }
  259. first_sector = AV_RL32(buf + 40 + name_size);
  260. depth = AV_RL32(buf + 44 + name_size);
  261. /* compare file name; test optional null terminator */
  262. name = buf + 40;
  263. if (name_size >= filename_size &&
  264. !memcmp(name, filename, filename_size) &&
  265. (name_size < filename_size + 2 || !AV_RN16(name + filename_size)))
  266. return wtvfile_open_sector(first_sector, file_length, depth, s);
  267. buf += dir_length;
  268. }
  269. return 0;
  270. }
  271. #define wtvfile_open(s, buf, buf_size, filename) \
  272. wtvfile_open2(s, buf, buf_size, filename, sizeof(filename))
  273. /**
  274. * Close file opened with wtvfile_open_sector(), or wtv_open()
  275. */
  276. static void wtvfile_close(AVIOContext *pb)
  277. {
  278. WtvFile *wf = pb->opaque;
  279. av_free(wf->sectors);
  280. av_free(wf);
  281. av_free(pb->buffer);
  282. av_free(pb);
  283. }
  284. /*
  285. *
  286. * Main demuxer
  287. *
  288. */
  289. typedef struct WtvStream {
  290. int seen_data;
  291. } WtvStream;
  292. typedef struct WtvContext {
  293. AVIOContext *pb; /** timeline file */
  294. int64_t epoch;
  295. int64_t pts; /** pts for next data chunk */
  296. int64_t last_valid_pts; /** latest valid pts, used for interative seeking */
  297. /* maintain private seek index, as the AVIndexEntry->pos is relative to the
  298. start of the 'timeline' file, not the file system (AVFormatContext->pb) */
  299. AVIndexEntry *index_entries;
  300. int nb_index_entries;
  301. unsigned int index_entries_allocated_size;
  302. } WtvContext;
  303. /* WTV GUIDs */
  304. static const ff_asf_guid wtv_guid =
  305. {0xB7,0xD8,0x00,0x20,0x37,0x49,0xDA,0x11,0xA6,0x4E,0x00,0x07,0xE9,0x5E,0xAD,0x8D};
  306. static const ff_asf_guid metadata_guid =
  307. {0x5A,0xFE,0xD7,0x6D,0xC8,0x1D,0x8F,0x4A,0x99,0x22,0xFA,0xB1,0x1C,0x38,0x14,0x53};
  308. static const ff_asf_guid timestamp_guid =
  309. {0x5B,0x05,0xE6,0x1B,0x97,0xA9,0x49,0x43,0x88,0x17,0x1A,0x65,0x5A,0x29,0x8A,0x97};
  310. static const ff_asf_guid data_guid =
  311. {0x95,0xC3,0xD2,0xC2,0x7E,0x9A,0xDA,0x11,0x8B,0xF7,0x00,0x07,0xE9,0x5E,0xAD,0x8D};
  312. static const ff_asf_guid stream_guid =
  313. {0xED,0xA4,0x13,0x23,0x2D,0xBF,0x4F,0x45,0xAD,0x8A,0xD9,0x5B,0xA7,0xF9,0x1F,0xEE};
  314. static const ff_asf_guid stream2_guid =
  315. {0xA2,0xC3,0xD2,0xC2,0x7E,0x9A,0xDA,0x11,0x8B,0xF7,0x00,0x07,0xE9,0x5E,0xAD,0x8D};
  316. static const ff_asf_guid EVENTID_SubtitleSpanningEvent =
  317. {0x48,0xC0,0xCE,0x5D,0xB9,0xD0,0x63,0x41,0x87,0x2C,0x4F,0x32,0x22,0x3B,0xE8,0x8A};
  318. static const ff_asf_guid EVENTID_LanguageSpanningEvent =
  319. {0x6D,0x66,0x92,0xE2,0x02,0x9C,0x8D,0x44,0xAA,0x8D,0x78,0x1A,0x93,0xFD,0xC3,0x95};
  320. static const ff_asf_guid EVENTID_AudioDescriptorSpanningEvent =
  321. {0x1C,0xD4,0x7B,0x10,0xDA,0xA6,0x91,0x46,0x83,0x69,0x11,0xB2,0xCD,0xAA,0x28,0x8E};
  322. static const ff_asf_guid EVENTID_CtxADescriptorSpanningEvent =
  323. {0xE6,0xA2,0xB4,0x3A,0x47,0x42,0x34,0x4B,0x89,0x6C,0x30,0xAF,0xA5,0xD2,0x1C,0x24};
  324. static const ff_asf_guid EVENTID_CSDescriptorSpanningEvent =
  325. {0xD9,0x79,0xE7,0xEf,0xF0,0x97,0x86,0x47,0x80,0x0D,0x95,0xCF,0x50,0x5D,0xDC,0x66};
  326. static const ff_asf_guid EVENTID_DVBScramblingControlSpanningEvent =
  327. {0xC4,0xE1,0xD4,0x4B,0xA1,0x90,0x09,0x41,0x82,0x36,0x27,0xF0,0x0E,0x7D,0xCC,0x5B};
  328. static const ff_asf_guid EVENTID_StreamIDSpanningEvent =
  329. {0x68,0xAB,0xF1,0xCA,0x53,0xE1,0x41,0x4D,0xA6,0xB3,0xA7,0xC9,0x98,0xDB,0x75,0xEE};
  330. static const ff_asf_guid EVENTID_TeletextSpanningEvent =
  331. {0x50,0xD9,0x99,0x95,0x33,0x5F,0x17,0x46,0xAF,0x7C,0x1E,0x54,0xB5,0x10,0xDA,0xA3};
  332. static const ff_asf_guid EVENTID_AudioTypeSpanningEvent =
  333. {0xBE,0xBF,0x1C,0x50,0x49,0xB8,0xCE,0x42,0x9B,0xE9,0x3D,0xB8,0x69,0xFB,0x82,0xB3};
  334. /* Windows media GUIDs */
  335. /* Media types */
  336. static const ff_asf_guid mediatype_audio =
  337. {'a','u','d','s',FF_MEDIASUBTYPE_BASE_GUID};
  338. static const ff_asf_guid mediatype_video =
  339. {'v','i','d','s',FF_MEDIASUBTYPE_BASE_GUID};
  340. static const ff_asf_guid mediasubtype_mpeg1payload =
  341. {0x81,0xEB,0x36,0xE4,0x4F,0x52,0xCE,0x11,0x9F,0x53,0x00,0x20,0xAF,0x0B,0xA7,0x70};
  342. static const ff_asf_guid mediatype_mpeg2_sections =
  343. {0x6C,0x17,0x5F,0x45,0x06,0x4B,0xCE,0x47,0x9A,0xEF,0x8C,0xAE,0xF7,0x3D,0xF7,0xB5};
  344. static const ff_asf_guid mediatype_mpeg2_pes =
  345. {0x20,0x80,0x6D,0xE0,0x46,0xDB,0xCF,0x11,0xB4,0xD1,0x00,0x80,0x5F,0x6C,0xBB,0xEA};
  346. static const ff_asf_guid mediatype_mstvcaption =
  347. {0x89,0x8A,0x8B,0xB8,0x49,0xB0,0x80,0x4C,0xAD,0xCF,0x58,0x98,0x98,0x5E,0x22,0xC1};
  348. /* Media subtypes */
  349. static const ff_asf_guid mediasubtype_cpfilters_processed =
  350. {0x28,0xBD,0xAD,0x46,0xD0,0x6F,0x96,0x47,0x93,0xB2,0x15,0x5C,0x51,0xDC,0x04,0x8D};
  351. static const ff_asf_guid mediasubtype_dvb_subtitle =
  352. {0xC3,0xCB,0xFF,0x34,0xB3,0xD5,0x71,0x41,0x90,0x02,0xD4,0xC6,0x03,0x01,0x69,0x7F};
  353. static const ff_asf_guid mediasubtype_teletext =
  354. {0xE3,0x76,0x2A,0xF7,0x0A,0xEB,0xD0,0x11,0xAC,0xE4,0x00,0x00,0xC0,0xCC,0x16,0xBA};
  355. static const ff_asf_guid mediasubtype_dtvccdata =
  356. {0xAA,0xDD,0x2A,0xF5,0xF0,0x36,0xF5,0x43,0x95,0xEA,0x6D,0x86,0x64,0x84,0x26,0x2A};
  357. static const ff_asf_guid mediasubtype_mpeg2_sections =
  358. {0x79,0x85,0x9F,0x4A,0xF8,0x6B,0x92,0x43,0x8A,0x6D,0xD2,0xDD,0x09,0xFA,0x78,0x61};
  359. /* Formats */
  360. static const ff_asf_guid format_cpfilters_processed =
  361. {0x6F,0xB3,0x39,0x67,0x5F,0x1D,0xC2,0x4A,0x81,0x92,0x28,0xBB,0x0E,0x73,0xD1,0x6A};
  362. static const ff_asf_guid format_waveformatex =
  363. {0x81,0x9F,0x58,0x05,0x56,0xC3,0xCE,0x11,0xBF,0x01,0x00,0xAA,0x00,0x55,0x59,0x5A};
  364. static const ff_asf_guid format_videoinfo2 =
  365. {0xA0,0x76,0x2A,0xF7,0x0A,0xEB,0xD0,0x11,0xAC,0xE4,0x00,0x00,0xC0,0xCC,0x16,0xBA};
  366. static const ff_asf_guid format_mpeg2_video =
  367. {0xE3,0x80,0x6D,0xE0,0x46,0xDB,0xCF,0x11,0xB4,0xD1,0x00,0x80,0x5F,0x6C,0xBB,0xEA};
  368. static const ff_asf_guid format_none =
  369. {0xD6,0x17,0x64,0x0F,0x18,0xC3,0xD0,0x11,0xA4,0x3F,0x00,0xA0,0xC9,0x22,0x31,0x96};
  370. static const AVCodecGuid video_guids[] = {
  371. {AV_CODEC_ID_MPEG2VIDEO, {0x26,0x80,0x6D,0xE0,0x46,0xDB,0xCF,0x11,0xB4,0xD1,0x00,0x80,0x5F,0x6C,0xBB,0xEA}},
  372. {AV_CODEC_ID_NONE}
  373. };
  374. static int read_probe(AVProbeData *p)
  375. {
  376. return ff_guidcmp(p->buf, wtv_guid) ? 0 : AVPROBE_SCORE_MAX;
  377. }
  378. /**
  379. * Convert win32 FILETIME to ISO-8601 string
  380. */
  381. static void filetime_to_iso8601(char *buf, int buf_size, int64_t value)
  382. {
  383. time_t t = (value / 10000000LL) - 11644473600LL;
  384. struct tm tmbuf;
  385. struct tm *tm = gmtime_r(&t, &tmbuf);
  386. if (tm) {
  387. if (!strftime(buf, buf_size, "%Y-%m-%d %H:%M:%S", tm))
  388. buf[0] = '\0';
  389. } else
  390. buf[0] = '\0';
  391. }
  392. /**
  393. * Convert crazy time (100ns since 1 Jan 0001) to ISO-8601 string
  394. */
  395. static void crazytime_to_iso8601(char *buf, int buf_size, int64_t value)
  396. {
  397. time_t t = (value / 10000000LL) - 719162LL*86400LL;
  398. struct tm tmbuf;
  399. struct tm *tm = gmtime_r(&t, &tmbuf);
  400. if (tm) {
  401. if (!strftime(buf, buf_size, "%Y-%m-%d %H:%M:%S", tm))
  402. buf[0] = '\0';
  403. } else
  404. buf[0] = '\0';
  405. }
  406. /**
  407. * Convert OLE DATE to ISO-8601 string
  408. */
  409. static void oledate_to_iso8601(char *buf, int buf_size, int64_t value)
  410. {
  411. time_t t = 631112400LL + 86400*av_int2double(value);
  412. struct tm tmbuf;
  413. struct tm *tm = gmtime_r(&t, &tmbuf);
  414. if (tm) {
  415. if (!strftime(buf, buf_size, "%Y-%m-%d %H:%M:%S", tm))
  416. buf[0] = '\0';
  417. } else
  418. buf[0] = '\0';
  419. }
  420. static void get_attachment(AVFormatContext *s, AVIOContext *pb, int length)
  421. {
  422. char mime[1024];
  423. char description[1024];
  424. unsigned int filesize;
  425. AVStream *st;
  426. int64_t pos = avio_tell(pb);
  427. avio_get_str16le(pb, INT_MAX, mime, sizeof(mime));
  428. if (strcmp(mime, "image/jpeg"))
  429. goto done;
  430. avio_r8(pb);
  431. avio_get_str16le(pb, INT_MAX, description, sizeof(description));
  432. filesize = avio_rl32(pb);
  433. if (!filesize)
  434. goto done;
  435. st = avformat_new_stream(s, NULL);
  436. if (!st)
  437. goto done;
  438. av_dict_set(&st->metadata, "title", description, 0);
  439. st->codec->codec_id = AV_CODEC_ID_MJPEG;
  440. st->codec->codec_type = AVMEDIA_TYPE_ATTACHMENT;
  441. st->codec->extradata = av_mallocz(filesize);
  442. st->id = -1;
  443. if (!st->codec->extradata)
  444. goto done;
  445. st->codec->extradata_size = filesize;
  446. avio_read(pb, st->codec->extradata, filesize);
  447. done:
  448. avio_seek(pb, pos + length, SEEK_SET);
  449. }
  450. static void get_tag(AVFormatContext *s, AVIOContext *pb, const char *key, int type, int length)
  451. {
  452. int buf_size = FFMAX(2*length, LEN_PRETTY_GUID) + 1;
  453. char *buf = av_malloc(buf_size);
  454. if (!buf)
  455. return;
  456. if (type == 0 && length == 4) {
  457. snprintf(buf, buf_size, "%u", avio_rl32(pb));
  458. } else if (type == 1) {
  459. avio_get_str16le(pb, length, buf, buf_size);
  460. if (!strlen(buf)) {
  461. av_free(buf);
  462. return;
  463. }
  464. } else if (type == 3 && length == 4) {
  465. strcpy(buf, avio_rl32(pb) ? "true" : "false");
  466. } else if (type == 4 && length == 8) {
  467. int64_t num = avio_rl64(pb);
  468. if (!strcmp(key, "WM/EncodingTime") ||
  469. !strcmp(key, "WM/MediaOriginalBroadcastDateTime"))
  470. filetime_to_iso8601(buf, buf_size, num);
  471. else if (!strcmp(key, "WM/WMRVEncodeTime") ||
  472. !strcmp(key, "WM/WMRVEndTime"))
  473. crazytime_to_iso8601(buf, buf_size, num);
  474. else if (!strcmp(key, "WM/WMRVExpirationDate"))
  475. oledate_to_iso8601(buf, buf_size, num);
  476. else if (!strcmp(key, "WM/WMRVBitrate"))
  477. snprintf(buf, buf_size, "%f", av_int2double(num));
  478. else
  479. snprintf(buf, buf_size, "%"PRIi64, num);
  480. } else if (type == 5 && length == 2) {
  481. snprintf(buf, buf_size, "%u", avio_rl16(pb));
  482. } else if (type == 6 && length == 16) {
  483. ff_asf_guid guid;
  484. avio_read(pb, guid, 16);
  485. snprintf(buf, buf_size, PRI_PRETTY_GUID, ARG_PRETTY_GUID(guid));
  486. } else if (type == 2 && !strcmp(key, "WM/Picture")) {
  487. get_attachment(s, pb, length);
  488. av_freep(&buf);
  489. return;
  490. } else {
  491. av_freep(&buf);
  492. av_log(s, AV_LOG_WARNING, "unsupported metadata entry; key:%s, type:%d, length:0x%x\n", key, type, length);
  493. avio_skip(pb, length);
  494. return;
  495. }
  496. av_dict_set(&s->metadata, key, buf, 0);
  497. av_freep(&buf);
  498. }
  499. /**
  500. * Parse metadata entries
  501. */
  502. static void parse_legacy_attrib(AVFormatContext *s, AVIOContext *pb)
  503. {
  504. ff_asf_guid guid;
  505. int length, type;
  506. while(!pb->eof_reached) {
  507. char key[1024];
  508. ff_get_guid(pb, &guid);
  509. type = avio_rl32(pb);
  510. length = avio_rl32(pb);
  511. if (!length)
  512. break;
  513. if (ff_guidcmp(&guid, metadata_guid)) {
  514. av_log(s, AV_LOG_WARNING, "unknown guid "FF_PRI_GUID", expected metadata_guid; "
  515. "remaining metadata entries ignored\n", FF_ARG_GUID(guid));
  516. break;
  517. }
  518. avio_get_str16le(pb, INT_MAX, key, sizeof(key));
  519. get_tag(s, pb, key, type, length);
  520. }
  521. ff_metadata_conv(&s->metadata, NULL, ff_asf_metadata_conv);
  522. }
  523. /**
  524. * parse VIDEOINFOHEADER2 structure
  525. * @return bytes consumed
  526. */
  527. static int parse_videoinfoheader2(AVFormatContext *s, AVStream *st)
  528. {
  529. WtvContext *wtv = s->priv_data;
  530. AVIOContext *pb = wtv->pb;
  531. avio_skip(pb, 72); // picture aspect ratio is unreliable
  532. ff_get_bmp_header(pb, st);
  533. return 72 + 40;
  534. }
  535. /**
  536. * Parse MPEG1WAVEFORMATEX extradata structure
  537. */
  538. static void parse_mpeg1waveformatex(AVStream *st)
  539. {
  540. /* fwHeadLayer */
  541. switch (AV_RL16(st->codec->extradata)) {
  542. case 0x0001 : st->codec->codec_id = AV_CODEC_ID_MP1; break;
  543. case 0x0002 : st->codec->codec_id = AV_CODEC_ID_MP2; break;
  544. case 0x0004 : st->codec->codec_id = AV_CODEC_ID_MP3; break;
  545. }
  546. st->codec->bit_rate = AV_RL32(st->codec->extradata + 2); /* dwHeadBitrate */
  547. /* dwHeadMode */
  548. switch (AV_RL16(st->codec->extradata + 6)) {
  549. case 1 :
  550. case 2 :
  551. case 4 : st->codec->channels = 2;
  552. st->codec->channel_layout = AV_CH_LAYOUT_STEREO;
  553. break;
  554. case 8 : st->codec->channels = 1;
  555. st->codec->channel_layout = AV_CH_LAYOUT_MONO;
  556. break;
  557. }
  558. }
  559. /**
  560. * Initialise stream
  561. * @param st Stream to initialise, or NULL to create and initialise new stream
  562. * @return NULL on error
  563. */
  564. static AVStream * new_stream(AVFormatContext *s, AVStream *st, int sid, int codec_type)
  565. {
  566. if (st) {
  567. if (st->codec->extradata) {
  568. av_freep(&st->codec->extradata);
  569. st->codec->extradata_size = 0;
  570. }
  571. } else {
  572. WtvStream *wst = av_mallocz(sizeof(WtvStream));
  573. if (!wst)
  574. return NULL;
  575. st = avformat_new_stream(s, NULL);
  576. if (!st) {
  577. av_free(wst);
  578. return NULL;
  579. }
  580. st->id = sid;
  581. st->priv_data = wst;
  582. }
  583. st->codec->codec_type = codec_type;
  584. st->need_parsing = AVSTREAM_PARSE_FULL;
  585. avpriv_set_pts_info(st, 64, 1, 10000000);
  586. return st;
  587. }
  588. /**
  589. * parse Media Type structure and populate stream
  590. * @param st Stream, or NULL to create new stream
  591. * @param mediatype Mediatype GUID
  592. * @param subtype Subtype GUID
  593. * @param formattype Format GUID
  594. * @param size Size of format buffer
  595. * @return NULL on error
  596. */
  597. static AVStream * parse_media_type(AVFormatContext *s, AVStream *st, int sid,
  598. ff_asf_guid mediatype, ff_asf_guid subtype,
  599. ff_asf_guid formattype, int size)
  600. {
  601. WtvContext *wtv = s->priv_data;
  602. AVIOContext *pb = wtv->pb;
  603. if (!ff_guidcmp(subtype, mediasubtype_cpfilters_processed) &&
  604. !ff_guidcmp(formattype, format_cpfilters_processed)) {
  605. ff_asf_guid actual_subtype;
  606. ff_asf_guid actual_formattype;
  607. if (size < 32) {
  608. av_log(s, AV_LOG_WARNING, "format buffer size underflow\n");
  609. avio_skip(pb, size);
  610. return NULL;
  611. }
  612. avio_skip(pb, size - 32);
  613. ff_get_guid(pb, &actual_subtype);
  614. ff_get_guid(pb, &actual_formattype);
  615. avio_seek(pb, -size, SEEK_CUR);
  616. st = parse_media_type(s, st, sid, mediatype, actual_subtype, actual_formattype, size - 32);
  617. avio_skip(pb, 32);
  618. return st;
  619. } else if (!ff_guidcmp(mediatype, mediatype_audio)) {
  620. st = new_stream(s, st, sid, AVMEDIA_TYPE_AUDIO);
  621. if (!st)
  622. return NULL;
  623. if (!ff_guidcmp(formattype, format_waveformatex)) {
  624. int ret = ff_get_wav_header(s, pb, st->codec, size);
  625. if (ret < 0)
  626. return NULL;
  627. } else {
  628. if (ff_guidcmp(formattype, format_none))
  629. av_log(s, AV_LOG_WARNING, "unknown formattype:"FF_PRI_GUID"\n", FF_ARG_GUID(formattype));
  630. avio_skip(pb, size);
  631. }
  632. if (!memcmp(subtype + 4, (const uint8_t[]){FF_MEDIASUBTYPE_BASE_GUID}, 12)) {
  633. st->codec->codec_id = ff_wav_codec_get_id(AV_RL32(subtype), st->codec->bits_per_coded_sample);
  634. } else if (!ff_guidcmp(subtype, mediasubtype_mpeg1payload)) {
  635. if (st->codec->extradata && st->codec->extradata_size >= 22)
  636. parse_mpeg1waveformatex(st);
  637. else
  638. av_log(s, AV_LOG_WARNING, "MPEG1WAVEFORMATEX underflow\n");
  639. } else {
  640. st->codec->codec_id = ff_codec_guid_get_id(ff_codec_wav_guids, subtype);
  641. if (st->codec->codec_id == AV_CODEC_ID_NONE)
  642. av_log(s, AV_LOG_WARNING, "unknown subtype:"FF_PRI_GUID"\n", FF_ARG_GUID(subtype));
  643. }
  644. return st;
  645. } else if (!ff_guidcmp(mediatype, mediatype_video)) {
  646. st = new_stream(s, st, sid, AVMEDIA_TYPE_VIDEO);
  647. if (!st)
  648. return NULL;
  649. if (!ff_guidcmp(formattype, format_videoinfo2)) {
  650. int consumed = parse_videoinfoheader2(s, st);
  651. avio_skip(pb, FFMAX(size - consumed, 0));
  652. } else if (!ff_guidcmp(formattype, format_mpeg2_video)) {
  653. int consumed = parse_videoinfoheader2(s, st);
  654. avio_skip(pb, FFMAX(size - consumed, 0));
  655. } else {
  656. if (ff_guidcmp(formattype, format_none))
  657. av_log(s, AV_LOG_WARNING, "unknown formattype:"FF_PRI_GUID"\n", FF_ARG_GUID(formattype));
  658. avio_skip(pb, size);
  659. }
  660. if (!memcmp(subtype + 4, (const uint8_t[]){FF_MEDIASUBTYPE_BASE_GUID}, 12)) {
  661. st->codec->codec_id = ff_codec_get_id(ff_codec_bmp_tags, AV_RL32(subtype));
  662. } else {
  663. st->codec->codec_id = ff_codec_guid_get_id(video_guids, subtype);
  664. }
  665. if (st->codec->codec_id == AV_CODEC_ID_NONE)
  666. av_log(s, AV_LOG_WARNING, "unknown subtype:"FF_PRI_GUID"\n", FF_ARG_GUID(subtype));
  667. return st;
  668. } else if (!ff_guidcmp(mediatype, mediatype_mpeg2_pes) &&
  669. !ff_guidcmp(subtype, mediasubtype_dvb_subtitle)) {
  670. st = new_stream(s, st, sid, AVMEDIA_TYPE_SUBTITLE);
  671. if (!st)
  672. return NULL;
  673. if (ff_guidcmp(formattype, format_none))
  674. av_log(s, AV_LOG_WARNING, "unknown formattype:"FF_PRI_GUID"\n", FF_ARG_GUID(formattype));
  675. avio_skip(pb, size);
  676. st->codec->codec_id = AV_CODEC_ID_DVB_SUBTITLE;
  677. return st;
  678. } else if (!ff_guidcmp(mediatype, mediatype_mstvcaption) &&
  679. (!ff_guidcmp(subtype, mediasubtype_teletext) || !ff_guidcmp(subtype, mediasubtype_dtvccdata))) {
  680. st = new_stream(s, st, sid, AVMEDIA_TYPE_SUBTITLE);
  681. if (!st)
  682. return NULL;
  683. if (ff_guidcmp(formattype, format_none))
  684. av_log(s, AV_LOG_WARNING, "unknown formattype:"FF_PRI_GUID"\n", FF_ARG_GUID(formattype));
  685. avio_skip(pb, size);
  686. st->codec->codec_id = AV_CODEC_ID_DVB_TELETEXT;
  687. return st;
  688. } else if (!ff_guidcmp(mediatype, mediatype_mpeg2_sections) &&
  689. !ff_guidcmp(subtype, mediasubtype_mpeg2_sections)) {
  690. if (ff_guidcmp(formattype, format_none))
  691. av_log(s, AV_LOG_WARNING, "unknown formattype:"FF_PRI_GUID"\n", FF_ARG_GUID(formattype));
  692. avio_skip(pb, size);
  693. return NULL;
  694. }
  695. av_log(s, AV_LOG_WARNING, "unknown media type, mediatype:"FF_PRI_GUID
  696. ", subtype:"FF_PRI_GUID", formattype:"FF_PRI_GUID"\n",
  697. FF_ARG_GUID(mediatype), FF_ARG_GUID(subtype), FF_ARG_GUID(formattype));
  698. avio_skip(pb, size);
  699. return NULL;
  700. }
  701. enum {
  702. SEEK_TO_DATA = 0,
  703. SEEK_TO_PTS,
  704. };
  705. /**
  706. * Parse WTV chunks
  707. * @param mode SEEK_TO_DATA or SEEK_TO_PTS
  708. * @param seekts timestamp
  709. * @param[out] len_ptr Length of data chunk
  710. * @return stream index of data chunk, or <0 on error
  711. */
  712. static int parse_chunks(AVFormatContext *s, int mode, int64_t seekts, int *len_ptr)
  713. {
  714. WtvContext *wtv = s->priv_data;
  715. AVIOContext *pb = wtv->pb;
  716. while (!pb->eof_reached) {
  717. ff_asf_guid g;
  718. int len, sid, consumed;
  719. ff_get_guid(pb, &g);
  720. len = avio_rl32(pb);
  721. if (len < 32)
  722. break;
  723. sid = avio_rl32(pb) & 0x7FFF;
  724. avio_skip(pb, 8);
  725. consumed = 32;
  726. if (!ff_guidcmp(g, stream_guid)) {
  727. if (ff_find_stream_index(s, sid) < 0) {
  728. ff_asf_guid mediatype, subtype, formattype;
  729. int size;
  730. avio_skip(pb, 28);
  731. ff_get_guid(pb, &mediatype);
  732. ff_get_guid(pb, &subtype);
  733. avio_skip(pb, 12);
  734. ff_get_guid(pb, &formattype);
  735. size = avio_rl32(pb);
  736. parse_media_type(s, 0, sid, mediatype, subtype, formattype, size);
  737. consumed += 92 + size;
  738. }
  739. } else if (!ff_guidcmp(g, stream2_guid)) {
  740. int stream_index = ff_find_stream_index(s, sid);
  741. if (stream_index >= 0 && !((WtvStream*)s->streams[stream_index]->priv_data)->seen_data) {
  742. ff_asf_guid mediatype, subtype, formattype;
  743. int size;
  744. avio_skip(pb, 12);
  745. ff_get_guid(pb, &mediatype);
  746. ff_get_guid(pb, &subtype);
  747. avio_skip(pb, 12);
  748. ff_get_guid(pb, &formattype);
  749. size = avio_rl32(pb);
  750. parse_media_type(s, s->streams[stream_index], sid, mediatype, subtype, formattype, size);
  751. consumed += 76 + size;
  752. }
  753. } else if (!ff_guidcmp(g, EVENTID_AudioDescriptorSpanningEvent) ||
  754. !ff_guidcmp(g, EVENTID_CtxADescriptorSpanningEvent) ||
  755. !ff_guidcmp(g, EVENTID_CSDescriptorSpanningEvent) ||
  756. !ff_guidcmp(g, EVENTID_StreamIDSpanningEvent) ||
  757. !ff_guidcmp(g, EVENTID_SubtitleSpanningEvent) ||
  758. !ff_guidcmp(g, EVENTID_TeletextSpanningEvent)) {
  759. int stream_index = ff_find_stream_index(s, sid);
  760. if (stream_index >= 0) {
  761. AVStream *st = s->streams[stream_index];
  762. uint8_t buf[258];
  763. const uint8_t *pbuf = buf;
  764. int buf_size;
  765. avio_skip(pb, 8);
  766. consumed += 8;
  767. if (!ff_guidcmp(g, EVENTID_CtxADescriptorSpanningEvent) ||
  768. !ff_guidcmp(g, EVENTID_CSDescriptorSpanningEvent)) {
  769. avio_skip(pb, 6);
  770. consumed += 6;
  771. }
  772. buf_size = FFMIN(len - consumed, sizeof(buf));
  773. avio_read(pb, buf, buf_size);
  774. consumed += buf_size;
  775. ff_parse_mpeg2_descriptor(s, st, 0, &pbuf, buf + buf_size, NULL, 0, 0, NULL);
  776. }
  777. } else if (!ff_guidcmp(g, EVENTID_AudioTypeSpanningEvent)) {
  778. int stream_index = ff_find_stream_index(s, sid);
  779. if (stream_index >= 0) {
  780. AVStream *st = s->streams[stream_index];
  781. int audio_type;
  782. avio_skip(pb, 8);
  783. audio_type = avio_r8(pb);
  784. if (audio_type == 2)
  785. st->disposition |= AV_DISPOSITION_HEARING_IMPAIRED;
  786. else if (audio_type == 3)
  787. st->disposition |= AV_DISPOSITION_VISUAL_IMPAIRED;
  788. consumed += 9;
  789. }
  790. } else if (!ff_guidcmp(g, EVENTID_DVBScramblingControlSpanningEvent)) {
  791. int stream_index = ff_find_stream_index(s, sid);
  792. if (stream_index >= 0) {
  793. avio_skip(pb, 12);
  794. if (avio_rl32(pb))
  795. av_log(s, AV_LOG_WARNING, "DVB scrambled stream detected (st:%d), decoding will likely fail\n", stream_index);
  796. consumed += 16;
  797. }
  798. } else if (!ff_guidcmp(g, EVENTID_LanguageSpanningEvent)) {
  799. int stream_index = ff_find_stream_index(s, sid);
  800. if (stream_index >= 0) {
  801. AVStream *st = s->streams[stream_index];
  802. uint8_t language[4];
  803. avio_skip(pb, 12);
  804. avio_read(pb, language, 3);
  805. if (language[0]) {
  806. language[3] = 0;
  807. av_dict_set(&st->metadata, "language", language, 0);
  808. if (!strcmp(language, "nar") || !strcmp(language, "NAR"))
  809. st->disposition |= AV_DISPOSITION_VISUAL_IMPAIRED;
  810. }
  811. consumed += 15;
  812. }
  813. } else if (!ff_guidcmp(g, timestamp_guid)) {
  814. int stream_index = ff_find_stream_index(s, sid);
  815. if (stream_index >= 0) {
  816. avio_skip(pb, 8);
  817. wtv->pts = avio_rl64(pb);
  818. consumed += 16;
  819. if (wtv->pts == -1)
  820. wtv->pts = AV_NOPTS_VALUE;
  821. else {
  822. wtv->last_valid_pts = wtv->pts;
  823. if (wtv->epoch == AV_NOPTS_VALUE || wtv->pts < wtv->epoch)
  824. wtv->epoch = wtv->pts;
  825. if (mode == SEEK_TO_PTS && wtv->pts >= seekts) {
  826. #define WTV_PAD8(x) (((x) + 7) & ~7)
  827. avio_skip(pb, WTV_PAD8(len) - consumed);
  828. return 0;
  829. }
  830. }
  831. }
  832. } else if (!ff_guidcmp(g, data_guid)) {
  833. int stream_index = ff_find_stream_index(s, sid);
  834. if (mode == SEEK_TO_DATA && stream_index >= 0 && len > 32) {
  835. WtvStream *wst = s->streams[stream_index]->priv_data;
  836. wst->seen_data = 1;
  837. if (len_ptr) {
  838. *len_ptr = len;
  839. }
  840. return stream_index;
  841. }
  842. } else if (
  843. !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}) ||
  844. !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}) ||
  845. !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}) ||
  846. !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}) ||
  847. !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}) ||
  848. !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}) ||
  849. !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}) ||
  850. !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}) ||
  851. !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}) ||
  852. !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}) ||
  853. !ff_guidcmp(g, (const ff_asf_guid){0x1E,0xBE,0xC3,0xC5,0x43,0x92,0xDC,0x11,0x85,0xE5,0x00,0x12,0x3F,0x6F,0x73,0xB9}) ||
  854. !ff_guidcmp(g, (const ff_asf_guid){0x3B,0x86,0xA2,0xB1,0xEB,0x1E,0xC3,0x44,0x8C,0x88,0x1C,0xA3,0xFF,0xE3,0xE7,0x6A}) ||
  855. !ff_guidcmp(g, (const ff_asf_guid){0x4E,0x7F,0x4C,0x5B,0xC4,0xD0,0x38,0x4B,0xA8,0x3E,0x21,0x7F,0x7B,0xBF,0x52,0xE7}) ||
  856. !ff_guidcmp(g, (const ff_asf_guid){0x63,0x36,0xEB,0xFE,0xA1,0x7E,0xD9,0x11,0x83,0x08,0x00,0x07,0xE9,0x5E,0xAD,0x8D}) ||
  857. !ff_guidcmp(g, (const ff_asf_guid){0x70,0xE9,0xF1,0xF8,0x89,0xA4,0x4C,0x4D,0x83,0x73,0xB8,0x12,0xE0,0xD5,0xF8,0x1E}) ||
  858. !ff_guidcmp(g, (const ff_asf_guid){0x96,0xC3,0xD2,0xC2,0x7E,0x9A,0xDA,0x11,0x8B,0xF7,0x00,0x07,0xE9,0x5E,0xAD,0x8D}) ||
  859. !ff_guidcmp(g, (const ff_asf_guid){0x97,0xC3,0xD2,0xC2,0x7E,0x9A,0xDA,0x11,0x8B,0xF7,0x00,0x07,0xE9,0x5E,0xAD,0x8D}) ||
  860. !ff_guidcmp(g, (const ff_asf_guid){0xA1,0xC3,0xD2,0xC2,0x7E,0x9A,0xDA,0x11,0x8B,0xF7,0x00,0x07,0xE9,0x5E,0xAD,0x8D})) {
  861. //ignore known guids
  862. } else
  863. av_log(s, AV_LOG_WARNING, "unsupported chunk:"FF_PRI_GUID"\n", FF_ARG_GUID(g));
  864. avio_skip(pb, WTV_PAD8(len) - consumed);
  865. }
  866. return AVERROR_EOF;
  867. }
  868. /* declare utf16le strings */
  869. #define _ , 0,
  870. static const uint8_t timeline_le16[] =
  871. {'t'_'i'_'m'_'e'_'l'_'i'_'n'_'e', 0};
  872. static const uint8_t table_0_entries_legacy_attrib_le16[] =
  873. {'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};
  874. static const uint8_t table_0_entries_time_le16[] =
  875. {'t'_'a'_'b'_'l'_'e'_'.'_'0'_'.'_'e'_'n'_'t'_'r'_'i'_'e'_'s'_'.'_'t'_'i'_'m'_'e', 0};
  876. static const uint8_t timeline_table_0_entries_Events_le16[] =
  877. {'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};
  878. #undef _
  879. static int read_header(AVFormatContext *s)
  880. {
  881. WtvContext *wtv = s->priv_data;
  882. int root_sector, root_size;
  883. uint8_t root[WTV_SECTOR_SIZE];
  884. AVIOContext *pb;
  885. int64_t timeline_pos;
  886. int ret;
  887. wtv->epoch =
  888. wtv->pts =
  889. wtv->last_valid_pts = AV_NOPTS_VALUE;
  890. /* read root directory sector */
  891. avio_skip(s->pb, 0x30);
  892. root_size = avio_rl32(s->pb);
  893. if (root_size > sizeof(root)) {
  894. av_log(s, AV_LOG_ERROR, "root directory size exceeds sector size\n");
  895. return AVERROR_INVALIDDATA;
  896. }
  897. avio_skip(s->pb, 4);
  898. root_sector = avio_rl32(s->pb);
  899. ret = seek_by_sector(s->pb, root_sector, 0);
  900. if (ret < 0)
  901. return ret;
  902. root_size = avio_read(s->pb, root, root_size);
  903. if (root_size < 0)
  904. return AVERROR_INVALIDDATA;
  905. /* parse chunks up until first data chunk */
  906. wtv->pb = wtvfile_open(s, root, root_size, timeline_le16);
  907. if (!wtv->pb) {
  908. av_log(s, AV_LOG_ERROR, "timeline data missing\n");
  909. return AVERROR_INVALIDDATA;
  910. }
  911. ret = parse_chunks(s, SEEK_TO_DATA, 0, 0);
  912. if (ret < 0)
  913. return ret;
  914. avio_seek(wtv->pb, -32, SEEK_CUR);
  915. timeline_pos = avio_tell(s->pb); // save before opening another file
  916. /* read metadata */
  917. pb = wtvfile_open(s, root, root_size, table_0_entries_legacy_attrib_le16);
  918. if (pb) {
  919. parse_legacy_attrib(s, pb);
  920. wtvfile_close(pb);
  921. }
  922. /* read seek index */
  923. if (s->nb_streams) {
  924. AVStream *st = s->streams[0];
  925. pb = wtvfile_open(s, root, root_size, table_0_entries_time_le16);
  926. if (pb) {
  927. while(1) {
  928. uint64_t timestamp = avio_rl64(pb);
  929. uint64_t frame_nb = avio_rl64(pb);
  930. if (pb->eof_reached)
  931. break;
  932. ff_add_index_entry(&wtv->index_entries, &wtv->nb_index_entries, &wtv->index_entries_allocated_size,
  933. 0, timestamp, frame_nb, 0, AVINDEX_KEYFRAME);
  934. }
  935. wtvfile_close(pb);
  936. if (wtv->nb_index_entries) {
  937. pb = wtvfile_open(s, root, root_size, timeline_table_0_entries_Events_le16);
  938. if (pb) {
  939. int i;
  940. while (1) {
  941. uint64_t frame_nb = avio_rl64(pb);
  942. uint64_t position = avio_rl64(pb);
  943. if (pb->eof_reached)
  944. break;
  945. for (i = wtv->nb_index_entries - 1; i >= 0; i--) {
  946. AVIndexEntry *e = wtv->index_entries + i;
  947. if (frame_nb > e->size)
  948. break;
  949. if (position > e->pos)
  950. e->pos = position;
  951. }
  952. }
  953. wtvfile_close(pb);
  954. st->duration = wtv->index_entries[wtv->nb_index_entries - 1].timestamp;
  955. }
  956. }
  957. }
  958. }
  959. avio_seek(s->pb, timeline_pos, SEEK_SET);
  960. return 0;
  961. }
  962. static int read_packet(AVFormatContext *s, AVPacket *pkt)
  963. {
  964. WtvContext *wtv = s->priv_data;
  965. AVIOContext *pb = wtv->pb;
  966. int stream_index, len, ret;
  967. stream_index = parse_chunks(s, SEEK_TO_DATA, 0, &len);
  968. if (stream_index < 0)
  969. return stream_index;
  970. ret = av_get_packet(pb, pkt, len - 32);
  971. if (ret < 0)
  972. return ret;
  973. pkt->stream_index = stream_index;
  974. pkt->pts = wtv->pts;
  975. avio_skip(pb, WTV_PAD8(len) - len);
  976. return 0;
  977. }
  978. static int read_seek(AVFormatContext *s, int stream_index,
  979. int64_t ts, int flags)
  980. {
  981. WtvContext *wtv = s->priv_data;
  982. AVIOContext *pb = wtv->pb;
  983. AVStream *st = s->streams[0];
  984. int64_t ts_relative;
  985. int i;
  986. if ((flags & AVSEEK_FLAG_FRAME) || (flags & AVSEEK_FLAG_BYTE))
  987. return AVERROR(ENOSYS);
  988. /* timestamp adjustment is required because wtv->pts values are absolute,
  989. * whereas AVIndexEntry->timestamp values are relative to epoch. */
  990. ts_relative = ts;
  991. if (wtv->epoch != AV_NOPTS_VALUE)
  992. ts_relative -= wtv->epoch;
  993. i = ff_index_search_timestamp(wtv->index_entries, wtv->nb_index_entries, ts_relative, flags);
  994. if (i < 0) {
  995. if (wtv->last_valid_pts == AV_NOPTS_VALUE || ts < wtv->last_valid_pts)
  996. avio_seek(pb, 0, SEEK_SET);
  997. else if (st->duration != AV_NOPTS_VALUE && ts_relative > st->duration && wtv->nb_index_entries)
  998. avio_seek(pb, wtv->index_entries[wtv->nb_index_entries - 1].pos, SEEK_SET);
  999. if (parse_chunks(s, SEEK_TO_PTS, ts, 0) < 0)
  1000. return AVERROR(ERANGE);
  1001. return 0;
  1002. }
  1003. wtv->pts = wtv->index_entries[i].timestamp;
  1004. if (wtv->epoch != AV_NOPTS_VALUE)
  1005. wtv->pts += wtv->epoch;
  1006. wtv->last_valid_pts = wtv->pts;
  1007. avio_seek(pb, wtv->index_entries[i].pos, SEEK_SET);
  1008. return 0;
  1009. }
  1010. static int read_close(AVFormatContext *s)
  1011. {
  1012. WtvContext *wtv = s->priv_data;
  1013. av_free(wtv->index_entries);
  1014. wtvfile_close(wtv->pb);
  1015. return 0;
  1016. }
  1017. AVInputFormat ff_wtv_demuxer = {
  1018. .name = "wtv",
  1019. .long_name = NULL_IF_CONFIG_SMALL("Windows Television (WTV)"),
  1020. .priv_data_size = sizeof(WtvContext),
  1021. .read_probe = read_probe,
  1022. .read_header = read_header,
  1023. .read_packet = read_packet,
  1024. .read_seek = read_seek,
  1025. .read_close = read_close,
  1026. .flags = AVFMT_SHOW_IDS,
  1027. };