wtvenc.c 22 KB

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