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.

1094 lines
39KB

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