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.

729 lines
22KB

  1. /*
  2. * Windows Television (WTV) muxer
  3. * Copyright (c) 2011 Zhentan Feng <spyfeng at gmail dot com>
  4. * Copyright (c) 2011 Peter Ross <pross@xvid.org>
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * Windows Television (WTV) demuxer
  24. * @author Zhentan Feng <spyfeng at gmail dot com>
  25. */
  26. #include "libavutil/intreadwrite.h"
  27. #include "libavutil/avassert.h"
  28. #include "avformat.h"
  29. #include "internal.h"
  30. #include "wtv.h"
  31. #define WTV_BIGSECTOR_SIZE (1 << WTV_BIGSECTOR_BITS)
  32. #define INDEX_BASE 0x2
  33. #define MAX_NB_INDEX 10
  34. /* declare utf16le strings */
  35. #define _ , 0,
  36. static const uint8_t timeline_table_0_header_events[] =
  37. {'t'_'i'_'m'_'e'_'l'_'i'_'n'_'e'_'.'_'t'_'a'_'b'_'l'_'e'_'.'_'0'_'.'_'h'_'e'_'a'_'d'_'e'_'r'_'.'_'E'_'v'_'e'_'n'_'t'_'s', 0};
  38. static const uint8_t table_0_header_legacy_attrib[] =
  39. {'t'_'a'_'b'_'l'_'e'_'.'_'0'_'.'_'h'_'e'_'a'_'d'_'e'_'r'_'.'_'l'_'e'_'g'_'a'_'c'_'y'_'_'_'a'_'t'_'t'_'r'_'i'_'b', 0};
  40. static const uint8_t table_0_redirector_legacy_attrib[] =
  41. {'t'_'a'_'b'_'l'_'e'_'.'_'0'_'.'_'r'_'e'_'d'_'i'_'r'_'e'_'c'_'t'_'o'_'r'_'.'_'l'_'e'_'g'_'a'_'c'_'y'_'_'_'a'_'t'_'t'_'r'_'i'_'b', 0};
  42. static const uint8_t table_0_header_time[] =
  43. {'t'_'a'_'b'_'l'_'e'_'.'_'0'_'.'_'h'_'e'_'a'_'d'_'e'_'r'_'.'_'t'_'i'_'m'_'e', 0};
  44. static const uint8_t legacy_attrib[] =
  45. {'l'_'e'_'g'_'a'_'c'_'y'_'_'_'a'_'t'_'t'_'r'_'i'_'b', 0};
  46. #undef _
  47. static const ff_asf_guid sub_wtv_guid =
  48. {0x8C,0xC3,0xD2,0xC2,0x7E,0x9A,0xDA,0x11,0x8B,0xF7,0x00,0x07,0xE9,0x5E,0xAD,0x8D};
  49. static const ff_asf_guid stream1_guid =
  50. {0xA1,0xC3,0xD2,0xC2,0x7E,0x9A,0xDA,0x11,0x8B,0xF7,0x00,0x07,0xE9,0x5E,0xAD,0x8D};
  51. static const ff_asf_guid sync_guid =
  52. {0x97,0xC3,0xD2,0xC2,0x7E,0x9A,0xDA,0x11,0x8B,0xF7,0x00,0x07,0xE9,0x5E,0xAD,0x8D};
  53. static const ff_asf_guid index_guid =
  54. {0x96,0xc3,0xd2,0xc2,0x7e,0x9a,0xda,0x11,0x8b,0xf7,0x00,0x07,0xe9,0x5e,0xad,0x8d};
  55. enum WtvFileIndex {
  56. WTV_TIMELINE_TABLE_0_HEADER_EVENTS = 0,
  57. WTV_TIMELINE_TABLE_0_ENTRIES_EVENTS,
  58. WTV_TIMELINE,
  59. WTV_TABLE_0_HEADER_LEGACY_ATTRIB,
  60. WTV_TABLE_0_ENTRIES_LEGACY_ATTRIB,
  61. WTV_TABLE_0_REDIRECTOR_LEGACY_ATTRIB,
  62. WTV_TABLE_0_HEADER_TIME,
  63. WTV_TABLE_0_ENTRIES_TIME,
  64. WTV_FILES
  65. };
  66. typedef struct {
  67. int64_t length;
  68. const void *header;
  69. int depth;
  70. int first_sector;
  71. } WtvFile;
  72. typedef struct {
  73. int64_t pos;
  74. int64_t serial;
  75. const ff_asf_guid * guid;
  76. int stream_id;
  77. } WtvChunkEntry;
  78. typedef struct {
  79. int64_t timeline_start_pos;
  80. WtvFile file[WTV_FILES];
  81. int64_t serial; /** chunk serial number */
  82. int64_t last_chunk_pos; /** last chunk position */
  83. int64_t frame_nb;
  84. WtvChunkEntry index[MAX_NB_INDEX];
  85. int nb_index;
  86. int first_video_flag;
  87. int64_t sync_pos;
  88. } WtvContext;
  89. typedef int WTVHeaderWriteFunc(AVIOContext *pb);
  90. typedef struct {
  91. const uint8_t *header;
  92. int header_size;
  93. WTVHeaderWriteFunc *write_header;
  94. } WTVRootEntryTable;
  95. static int write_pad(AVIOContext *pb, int size)
  96. {
  97. for (; size > 0; size--)
  98. avio_w8(pb, 0);
  99. return 0;
  100. }
  101. static void put_guid(AVIOContext *s, const ff_asf_guid *g)
  102. {
  103. assert(sizeof(*g) == 16);
  104. avio_write(s, *g, sizeof(*g));
  105. }
  106. static const ff_asf_guid *get_codec_guid(enum CodecID id, const AVCodecGuid *av_guid)
  107. {
  108. int i;
  109. for (i = 0; av_guid[i].id != CODEC_ID_NONE; i++) {
  110. if (id == av_guid[i].id)
  111. return &(av_guid[i].guid);
  112. }
  113. return NULL;
  114. }
  115. /**
  116. * Write chunk header. If header chunk (0x80000000 set) then add to list of header chunks
  117. */
  118. static void write_chunk_header(AVFormatContext *s, const ff_asf_guid *guid, int length, int stream_id)
  119. {
  120. WtvContext *wctx = s->priv_data;
  121. AVIOContext *pb = s->pb;
  122. wctx->last_chunk_pos = avio_tell(pb) - wctx->timeline_start_pos;
  123. put_guid(pb, guid);
  124. avio_wl32(pb, 32 + length);
  125. avio_wl32(pb, stream_id);
  126. avio_wl64(pb, wctx->serial);
  127. if ((stream_id & 0x80000000) && guid != &index_guid) {
  128. WtvChunkEntry *t = wctx->index + wctx->nb_index;
  129. av_assert0(wctx->nb_index < MAX_NB_INDEX);
  130. t->pos = wctx->last_chunk_pos;
  131. t->serial = wctx->serial;
  132. t->guid = guid;
  133. t->stream_id = stream_id & 0x3FFFFFFF;
  134. wctx->nb_index++;
  135. }
  136. }
  137. static void write_chunk_header2(AVFormatContext *s, const ff_asf_guid *guid, int stream_id)
  138. {
  139. WtvContext *wctx = s->priv_data;
  140. AVIOContext *pb = s->pb;
  141. int64_t last_chunk_pos = wctx->last_chunk_pos;
  142. write_chunk_header(s, guid, 0, stream_id); // length updated later
  143. avio_wl64(pb, last_chunk_pos);
  144. }
  145. static void finish_chunk_noindex(AVFormatContext *s)
  146. {
  147. WtvContext *wctx = s->priv_data;
  148. AVIOContext *pb = s->pb;
  149. // update the chunk_len field and pad.
  150. int64_t chunk_len = avio_tell(pb) - (wctx->last_chunk_pos + wctx->timeline_start_pos);
  151. avio_seek(pb, -(chunk_len - 16), SEEK_CUR);
  152. avio_wl32(pb, chunk_len);
  153. avio_seek(pb, chunk_len - (16 + 4), SEEK_CUR);
  154. write_pad(pb, WTV_PAD8(chunk_len) - chunk_len);
  155. wctx->serial++;
  156. }
  157. static void write_index(AVFormatContext *s)
  158. {
  159. AVIOContext *pb = s->pb;
  160. WtvContext *wctx = s->priv_data;
  161. int i;
  162. write_chunk_header2(s, &index_guid, 0x80000000);
  163. avio_wl32(pb, 0);
  164. avio_wl32(pb, 0);
  165. for (i = 0; i < wctx->nb_index; i++) {
  166. WtvChunkEntry *t = wctx->index + i;
  167. put_guid(pb, t->guid);
  168. avio_wl64(pb, t->pos);
  169. avio_wl32(pb, t->stream_id);
  170. avio_wl32(pb, 0); // checksum?
  171. avio_wl64(pb, t->serial);
  172. }
  173. wctx->nb_index = 0; // reset index
  174. finish_chunk_noindex(s);
  175. }
  176. static void finish_chunk(AVFormatContext *s)
  177. {
  178. WtvContext *wctx = s->priv_data;
  179. finish_chunk_noindex(s);
  180. if (wctx->nb_index == MAX_NB_INDEX)
  181. write_index(s);
  182. }
  183. static int write_stream_codec_info(AVFormatContext *s, AVStream *st)
  184. {
  185. WtvContext *wctx = s->priv_data;
  186. const ff_asf_guid *g, *media_type, *format_type;
  187. AVIOContext *pb = s->pb;
  188. int64_t hdr_pos_start;
  189. int hdr_size = 0;
  190. if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
  191. g = get_codec_guid(st->codec->codec_id, ff_video_guids);
  192. media_type = &ff_mediatype_video;
  193. format_type = &ff_format_mpeg2_video;
  194. } else if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
  195. g = get_codec_guid(st->codec->codec_id, ff_codec_wav_guids);
  196. media_type = &ff_mediatype_audio;
  197. format_type = &ff_format_waveformatex;
  198. } else {
  199. av_log(s, AV_LOG_ERROR, "unknown codec_type (0x%x)\n", st->codec->codec_type);
  200. return -1;
  201. }
  202. if (g == NULL) {
  203. av_log(s, AV_LOG_ERROR, "can't get video codec_id (0x%x) guid.\n", st->codec->codec_id);
  204. return -1;
  205. }
  206. put_guid(pb, media_type); // mediatype
  207. put_guid(pb, &ff_mediasubtype_cpfilters_processed); // subtype
  208. write_pad(pb, 12);
  209. put_guid(pb,&ff_format_cpfilters_processed); // format type
  210. avio_wl32(pb, 0); // size
  211. hdr_pos_start = avio_tell(pb);
  212. if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
  213. if (wctx->first_video_flag) {
  214. write_pad(pb, 216); //The size is sensitive.
  215. wctx->first_video_flag = 0;
  216. } else {
  217. write_pad(pb, 72); // aspect ratio
  218. ff_put_bmp_header(pb, st->codec, ff_codec_bmp_tags, 0);
  219. }
  220. } else {
  221. ff_put_wav_header(pb, st->codec);
  222. }
  223. hdr_size = avio_tell(pb) - hdr_pos_start;
  224. // seek back write hdr_size
  225. avio_seek(pb, -(hdr_size + 4), SEEK_CUR);
  226. avio_wl32(pb, hdr_size + 32);
  227. avio_seek(pb, hdr_size, SEEK_CUR);
  228. put_guid(pb, g); // actual_subtype
  229. put_guid(pb, format_type); // actual_formattype
  230. return 0;
  231. }
  232. static int write_stream_codec(AVFormatContext *s, AVStream * st)
  233. {
  234. AVIOContext *pb = s->pb;
  235. int ret;
  236. write_chunk_header2(s, &stream1_guid, 0x80000000 | 0x01);
  237. avio_wl32(pb, 0x01);
  238. write_pad(pb, 4);
  239. write_pad(pb, 4);
  240. ret = write_stream_codec_info(s, st);
  241. if (ret < 0) {
  242. av_log(s, AV_LOG_ERROR, "write stream codec info failed codec_type(0x%x)\n", st->codec->codec_type);
  243. return -1;
  244. }
  245. finish_chunk(s);
  246. return 0;
  247. }
  248. static void write_sync(AVFormatContext *s)
  249. {
  250. AVIOContext *pb = s->pb;
  251. WtvContext *wctx = s->priv_data;
  252. int64_t last_chunk_pos = wctx->last_chunk_pos;
  253. wctx->sync_pos = avio_tell(pb) - wctx->timeline_start_pos;;
  254. write_chunk_header(s, &sync_guid, 0x18, 0);
  255. write_pad(pb, 24);
  256. finish_chunk(s);
  257. wctx->last_chunk_pos = last_chunk_pos;
  258. }
  259. static void write_DSATTRIB_TRANSPORT_PROPERTIES_init(AVFormatContext *s, int stream_index)
  260. {
  261. AVIOContext *pb = s->pb;
  262. write_chunk_header2(s, &ff_DSATTRIB_TRANSPORT_PROPERTIES, 0x80000000 | stream_index);
  263. avio_wl64(pb, stream_index);
  264. avio_wl64(pb, -1);
  265. avio_wl64(pb, 0);
  266. finish_chunk(s);
  267. }
  268. static int write_stream_data(AVFormatContext *s, AVStream *st, int flag)
  269. {
  270. AVIOContext *pb = s->pb;
  271. int ret;
  272. if (!flag) {
  273. write_chunk_header2(s, &ff_stream_guid, 0x80000000 | (st->index + INDEX_BASE));
  274. avio_wl32(pb, 0x00000001);
  275. avio_wl32(pb, st->index + INDEX_BASE); //stream_id
  276. avio_wl32(pb, 0x00000001);
  277. write_pad(pb, 8);
  278. } else {
  279. write_chunk_header2(s, &ff_stream2_guid, 0x80000000 | (st->index + INDEX_BASE));
  280. write_pad(pb, 4);
  281. }
  282. ret = write_stream_codec_info(s, st);
  283. if (ret < 0) {
  284. av_log(s, AV_LOG_ERROR, "write stream codec info failed codec_type(0x%x)\n", st->codec->codec_type);
  285. return -1;
  286. }
  287. finish_chunk(s);
  288. av_set_pts_info(st, 64, 1, 10000000);
  289. return 0;
  290. }
  291. static int write_header(AVFormatContext *s)
  292. {
  293. AVIOContext *pb = s->pb;
  294. WtvContext *wctx = s->priv_data;
  295. int i, pad, ret;
  296. AVStream *st;
  297. put_guid(pb, &ff_wtv_guid);
  298. put_guid(pb, &sub_wtv_guid);
  299. avio_wl32(pb, 0x01);
  300. avio_wl32(pb, 0x02);
  301. avio_wl32(pb, 1 << WTV_SECTOR_BITS);
  302. avio_wl32(pb, 1 << WTV_BIGSECTOR_BITS);
  303. //write initial root fields
  304. avio_wl32(pb, 0); // root_size, update later
  305. write_pad(pb, 4);
  306. avio_wl32(pb, 0); // root_sector, update it later.
  307. write_pad(pb, 32);
  308. avio_wl32(pb, 0); // file ends pointer, update it later.
  309. pad = (1 << WTV_SECTOR_BITS) - avio_tell(pb);
  310. write_pad(pb, pad);
  311. wctx->timeline_start_pos = avio_tell(pb);
  312. wctx->serial = 1;
  313. wctx->last_chunk_pos = -1;
  314. wctx->first_video_flag = 1;
  315. for (i = 0; i < s->nb_streams; i++) {
  316. st = s->streams[i];
  317. ret = write_stream_codec(s, st);
  318. if (ret < 0) {
  319. av_log(s, AV_LOG_ERROR, "write stream codec failed codec_type(0x%x)\n", st->codec->codec_type);
  320. return -1;
  321. }
  322. if (i + 1 < s->nb_streams) {
  323. write_sync(s);
  324. }
  325. }
  326. for (i = 0; i < s->nb_streams; i++) {
  327. st = s->streams[i];
  328. ret = write_stream_data(s, st, 0);
  329. if (ret < 0) {
  330. av_log(s, AV_LOG_ERROR, "write stream data failed codec_type(0x%x)\n", st->codec->codec_type);
  331. return -1;
  332. }
  333. ret = write_stream_data(s, st, 1);
  334. if (ret < 0) {
  335. av_log(s, AV_LOG_ERROR, "write stream2 data failed codec_type(0x%x)\n", st->codec->codec_type);
  336. return -1;
  337. }
  338. }
  339. for (i = 0; i < s->nb_streams; i++)
  340. write_DSATTRIB_TRANSPORT_PROPERTIES_init(s, INDEX_BASE + i);
  341. if (wctx->nb_index)
  342. write_index(s);
  343. return 0;
  344. }
  345. static void write_timestamp(AVFormatContext *s, AVPacket *pkt)
  346. {
  347. AVIOContext *pb = s->pb;
  348. WtvContext *wctx = s->priv_data;
  349. AVCodecContext *enc = s->streams[pkt->stream_index]->codec;
  350. int flag = 0;
  351. int64_t frame_number = 0;
  352. if (enc->codec_type == AVMEDIA_TYPE_VIDEO) {
  353. wctx->frame_nb++;
  354. frame_number = wctx->frame_nb;
  355. flag = pkt->flags & AV_PKT_FLAG_KEY ? 1 : 0;
  356. }
  357. write_chunk_header(s, &ff_timestamp_guid, 56, 0x40000000 | (INDEX_BASE + pkt->stream_index));
  358. write_pad(pb, 8);
  359. avio_wl64(pb, pkt->pts == AV_NOPTS_VALUE ? -1 : pkt->pts);
  360. avio_wl64(pb, pkt->pts == AV_NOPTS_VALUE ? -1 : pkt->pts);
  361. avio_wl64(pb, frame_number);
  362. avio_wl64(pb, 0);
  363. avio_wl64(pb, flag);
  364. avio_wl64(pb, 0);
  365. }
  366. static int write_packet(AVFormatContext *s, AVPacket *pkt)
  367. {
  368. AVIOContext *pb = s->pb;
  369. WtvContext *wctx = s->priv_data;
  370. // write timestamp chunk
  371. write_timestamp(s, pkt);
  372. write_chunk_header(s, &ff_data_guid, pkt->size, INDEX_BASE + pkt->stream_index);
  373. avio_write(pb, pkt->data, pkt->size);
  374. write_pad(pb, WTV_PAD8(pkt->size) - pkt->size);
  375. wctx->serial++;
  376. avio_flush(pb);
  377. return 0;
  378. }
  379. static int write_table0_header_envents(AVIOContext *pb)
  380. {
  381. avio_wl32(pb, 0x10);
  382. write_pad(pb, 84);
  383. avio_wl64(pb, 0x32);
  384. return 96;
  385. }
  386. static int write_table0_header_legacy_attrib(AVIOContext *pb)
  387. {
  388. int pad = 0;
  389. avio_wl32(pb, 0xFFFFFFFF);
  390. write_pad(pb, 12);
  391. avio_write(pb, legacy_attrib, sizeof(legacy_attrib));
  392. pad = WTV_PAD8(sizeof(legacy_attrib)) - sizeof(legacy_attrib);
  393. write_pad(pb, pad);
  394. write_pad(pb, 32);
  395. return 48 + WTV_PAD8(sizeof(legacy_attrib));
  396. }
  397. static int write_table0_header_time(AVIOContext *pb)
  398. {
  399. avio_wl32(pb, 0x10);
  400. write_pad(pb, 76);
  401. avio_wl64(pb, 0x40);
  402. return 88;
  403. }
  404. static const WTVRootEntryTable wtv_root_entry_table[] = {
  405. { timeline_table_0_header_events, sizeof(timeline_table_0_header_events), write_table0_header_envents},
  406. { ff_timeline_table_0_entries_Events_le16, sizeof(ff_timeline_table_0_entries_Events_le16), NULL},
  407. { ff_timeline_le16, sizeof(ff_timeline_le16), NULL},
  408. { table_0_header_legacy_attrib, sizeof(table_0_header_legacy_attrib), write_table0_header_legacy_attrib},
  409. { ff_table_0_entries_legacy_attrib_le16, sizeof(ff_table_0_entries_legacy_attrib_le16), NULL},
  410. { table_0_redirector_legacy_attrib, sizeof(table_0_redirector_legacy_attrib), NULL},
  411. { table_0_header_time, sizeof(table_0_header_time), write_table0_header_time},
  412. { ff_table_0_entries_time_le16, sizeof(ff_table_0_entries_time_le16), NULL},
  413. };
  414. static int write_root_table(AVFormatContext *s, int64_t sector_pos)
  415. {
  416. AVIOContext *pb = s->pb;
  417. WtvContext *wctx = s->priv_data;
  418. int size, pad;
  419. int i;
  420. const WTVRootEntryTable *h = wtv_root_entry_table;
  421. for (i = 0; i < sizeof(wtv_root_entry_table)/sizeof(WTVRootEntryTable); i++, h++) {
  422. WtvFile *w = &wctx->file[i];
  423. int filename_padding = WTV_PAD8(h->header_size) - h->header_size;
  424. WTVHeaderWriteFunc *write = h->write_header;
  425. int len = 0;
  426. int64_t len_pos;
  427. put_guid(pb, &ff_dir_entry_guid);
  428. len_pos = avio_tell(pb);
  429. avio_wl16(pb, 40 + h->header_size + filename_padding + 8); // maybe updated later
  430. write_pad(pb, 6);
  431. avio_wl64(pb, write ? 0 : w->length);// maybe update later
  432. avio_wl32(pb, (h->header_size + filename_padding) >> 1);
  433. write_pad(pb, 4);
  434. avio_write(pb, h->header, h->header_size);
  435. write_pad(pb, filename_padding);
  436. if (write) {
  437. len = write(pb);
  438. // update length field
  439. avio_seek(pb, len_pos, SEEK_SET);
  440. avio_wl64(pb, 40 + h->header_size + filename_padding + len);
  441. avio_wl64(pb, len |(1ULL<<62) | (1ULL<<60));
  442. avio_seek(pb, 8 + h->header_size + filename_padding + len, SEEK_CUR);
  443. } else {
  444. avio_wl32(pb, w->first_sector);
  445. avio_wl32(pb, w->depth);
  446. }
  447. }
  448. // caculate root table size
  449. size = avio_tell(pb) - sector_pos;
  450. pad = WTV_SECTOR_SIZE- size;
  451. write_pad(pb, pad);
  452. return size;
  453. }
  454. static void write_fat(AVIOContext *pb, int start_sector, int nb_sectors, int shift)
  455. {
  456. int i;
  457. for (i = 0; i < nb_sectors; i++) {
  458. avio_wl32(pb, start_sector + (i << shift));
  459. }
  460. // pad left sector pointer size
  461. write_pad(pb, WTV_SECTOR_SIZE - (nb_sectors << 2));
  462. }
  463. static int write_fat_sector(AVFormatContext *s, int64_t start_pos, int nb_sectors, int sector_bits, int depth)
  464. {
  465. int64_t start_sector = start_pos >> WTV_SECTOR_BITS;
  466. int shift = sector_bits - WTV_SECTOR_BITS;
  467. int64_t fat = avio_tell(s->pb);
  468. write_fat(s->pb, start_sector, nb_sectors, shift);
  469. if (depth == 2) {
  470. int64_t start_sector1 = fat >> WTV_SECTOR_BITS;
  471. int nb_sectors1 = ((nb_sectors << 2) + WTV_SECTOR_SIZE - 1) / WTV_SECTOR_SIZE;
  472. int64_t fat1 = avio_tell(s->pb);
  473. write_fat(s->pb, start_sector1, nb_sectors1, 0);
  474. return fat1;
  475. }
  476. return fat;
  477. }
  478. static void write_table_entries_events(AVFormatContext *s)
  479. {
  480. AVIOContext *pb = s->pb;
  481. WtvContext *wctx = s->priv_data;
  482. //FIXME: output frame_nb, position pairs.
  483. //We only set the first sync_chunk position here.
  484. avio_wl64(pb, 0x2); avio_wl64(pb, wctx->sync_pos);
  485. }
  486. static void write_tag(AVIOContext *pb, const char *key, const char *value)
  487. {
  488. put_guid(pb, &ff_metadata_guid);
  489. avio_wl32(pb, 1);
  490. avio_wl32(pb, strlen(value)*2 + 2);
  491. avio_put_str16le(pb, key);
  492. avio_put_str16le(pb, value);
  493. }
  494. static void write_table_entries_attrib(AVFormatContext *s)
  495. {
  496. AVDictionaryEntry *tag = 0;
  497. //FIXME: translate special tags (e.g. WM/Bitrate) to binary representation
  498. ff_metadata_conv(&s->metadata, ff_asf_metadata_conv, NULL);
  499. while ((tag = av_dict_get(s->metadata, "", tag, AV_DICT_IGNORE_SUFFIX)))
  500. write_tag(s->pb, tag->key, tag->value);
  501. }
  502. static void write_table_redirector_legacy_attrib(AVFormatContext *s)
  503. {
  504. AVIOContext *pb = s->pb;
  505. AVDictionaryEntry *tag = 0;
  506. int64_t pos = 0;
  507. //FIXME: translate special tags to binary representation
  508. while ((tag = av_dict_get(s->metadata, "", tag, AV_DICT_IGNORE_SUFFIX))) {
  509. avio_wl64(pb, pos);
  510. pos += 16 + 4 + 4 + strlen(tag->key)*2 + 2 + strlen(tag->value)*2 + 2;
  511. }
  512. }
  513. /**
  514. * Pad the remainder of a file
  515. * Write out fat table
  516. * @return <0 on error
  517. */
  518. static int finish_file(AVFormatContext *s, enum WtvFileIndex index, int64_t start_pos)
  519. {
  520. WtvContext *wctx = s->priv_data;
  521. AVIOContext *pb = s->pb;
  522. WtvFile *w = &wctx->file[index];
  523. int64_t end_pos = avio_tell(pb);
  524. int sector_bits, nb_sectors, pad;
  525. av_assert0(index < WTV_FILES);
  526. w->length = (end_pos - start_pos);
  527. // determine optimal fat table depth, sector_bits, nb_sectors
  528. if (w->length <= WTV_SECTOR_SIZE) {
  529. w->depth = 0;
  530. sector_bits = WTV_SECTOR_BITS;
  531. } else if (w->length <= (WTV_SECTOR_SIZE / 4) * WTV_SECTOR_SIZE) {
  532. w->depth = 1;
  533. sector_bits = WTV_SECTOR_BITS;
  534. } else if (w->length <= (WTV_SECTOR_SIZE / 4) * WTV_BIGSECTOR_SIZE) {
  535. w->depth = 1;
  536. sector_bits = WTV_BIGSECTOR_BITS;
  537. } else if (w->length <= (int64_t)(WTV_SECTOR_SIZE / 4) * (WTV_SECTOR_SIZE / 4) * WTV_SECTOR_SIZE) {
  538. w->depth = 2;
  539. sector_bits = WTV_SECTOR_BITS;
  540. } else if (w->length <= (int64_t)(WTV_SECTOR_SIZE / 4) * (WTV_SECTOR_SIZE / 4) * WTV_BIGSECTOR_SIZE) {
  541. w->depth = 2;
  542. sector_bits = WTV_BIGSECTOR_BITS;
  543. } else {
  544. av_log(s, AV_LOG_ERROR, "unsupported file allocation table depth (%"PRIi64" bytes)\n", w->length);
  545. return -1;
  546. }
  547. // determine the nb_sectors
  548. nb_sectors = (int)(w->length >> sector_bits);
  549. // pad sector of timeline
  550. pad = (1 << sector_bits) - (w->length % (1 << sector_bits));
  551. if (pad) {
  552. nb_sectors++;
  553. write_pad(pb, pad);
  554. }
  555. //write fat table
  556. if (w->depth > 0) {
  557. w->first_sector = write_fat_sector(s, start_pos, nb_sectors, sector_bits, w->depth);
  558. } else {
  559. w->first_sector = start_pos;
  560. }
  561. w->first_sector >>= WTV_SECTOR_BITS;
  562. w->length |= 1ULL<<60;
  563. if (sector_bits == WTV_SECTOR_BITS)
  564. w->length |= 1ULL<<63;
  565. return 0;
  566. }
  567. static int write_trailer(AVFormatContext *s)
  568. {
  569. WtvContext *wctx = s->priv_data;
  570. AVIOContext *pb = s->pb;
  571. int root_size;
  572. int64_t sector_pos;
  573. int64_t start_pos, file_end_pos;
  574. if (finish_file(s, WTV_TIMELINE, wctx->timeline_start_pos) < 0)
  575. return -1;
  576. start_pos = avio_tell(pb);
  577. write_table_entries_events(s);
  578. if (finish_file(s, WTV_TIMELINE_TABLE_0_ENTRIES_EVENTS, start_pos) < 0)
  579. return -1;
  580. start_pos = avio_tell(pb);
  581. write_table_entries_attrib(s);
  582. if (finish_file(s, WTV_TABLE_0_ENTRIES_LEGACY_ATTRIB, start_pos) < 0)
  583. return -1;
  584. start_pos = avio_tell(pb);
  585. write_table_redirector_legacy_attrib(s);
  586. if (finish_file(s, WTV_TABLE_0_REDIRECTOR_LEGACY_ATTRIB, start_pos) < 0)
  587. return -1;
  588. start_pos = avio_tell(pb);
  589. //FIXME: output timestamp, frame_nb pairs here.
  590. if (finish_file(s, WTV_TABLE_0_ENTRIES_TIME, start_pos) < 0)
  591. return -1;
  592. // write root table
  593. sector_pos = avio_tell(pb);
  594. root_size = write_root_table(s, sector_pos);
  595. file_end_pos = avio_tell(pb);
  596. // update root value
  597. avio_seek(pb, 0x30, SEEK_SET);
  598. avio_wl32(pb, root_size);
  599. avio_seek(pb, 4, SEEK_CUR);
  600. avio_wl32(pb, sector_pos >> WTV_SECTOR_BITS);
  601. avio_seek(pb, 0x5c, SEEK_SET);
  602. avio_wl32(pb, file_end_pos >> WTV_SECTOR_BITS);
  603. avio_flush(pb);
  604. return 0;
  605. }
  606. AVOutputFormat ff_wtv_muxer = {
  607. .name = "wtv",
  608. .long_name = NULL_IF_CONFIG_SMALL("Windows Television (WTV)"),
  609. .extensions = "wtv",
  610. .priv_data_size = sizeof(WtvContext),
  611. .audio_codec = CODEC_ID_MP2,
  612. .video_codec = CODEC_ID_MPEG2VIDEO,
  613. .write_header = write_header,
  614. .write_packet = write_packet,
  615. .write_trailer = write_trailer,
  616. .codec_tag = (const AVCodecTag* const []){ ff_codec_bmp_tags,
  617. ff_codec_wav_tags, 0 },
  618. };