asfenc.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882
  1. /*
  2. * ASF muxer
  3. * Copyright (c) 2000, 2001 Fabrice Bellard
  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. #include "avformat.h"
  22. #include "metadata.h"
  23. #include "riff.h"
  24. #include "asf.h"
  25. #undef NDEBUG
  26. #include <assert.h>
  27. #define ASF_INDEXED_INTERVAL 10000000
  28. #define ASF_INDEX_BLOCK 600
  29. #define ASF_PACKET_ERROR_CORRECTION_DATA_SIZE 0x2
  30. #define ASF_PACKET_ERROR_CORRECTION_FLAGS (\
  31. ASF_PACKET_FLAG_ERROR_CORRECTION_PRESENT | \
  32. ASF_PACKET_ERROR_CORRECTION_DATA_SIZE\
  33. )
  34. #if (ASF_PACKET_ERROR_CORRECTION_FLAGS != 0)
  35. # define ASF_PACKET_ERROR_CORRECTION_FLAGS_FIELD_SIZE 1
  36. #else
  37. # define ASF_PACKET_ERROR_CORRECTION_FLAGS_FIELD_SIZE 0
  38. #endif
  39. #define ASF_PPI_PROPERTY_FLAGS (\
  40. ASF_PL_FLAG_REPLICATED_DATA_LENGTH_FIELD_IS_BYTE | \
  41. ASF_PL_FLAG_OFFSET_INTO_MEDIA_OBJECT_LENGTH_FIELD_IS_DWORD | \
  42. ASF_PL_FLAG_MEDIA_OBJECT_NUMBER_LENGTH_FIELD_IS_BYTE | \
  43. ASF_PL_FLAG_STREAM_NUMBER_LENGTH_FIELD_IS_BYTE \
  44. )
  45. #define ASF_PPI_LENGTH_TYPE_FLAGS 0
  46. #define ASF_PAYLOAD_FLAGS ASF_PL_FLAG_PAYLOAD_LENGTH_FIELD_IS_WORD
  47. #if (ASF_PPI_FLAG_SEQUENCE_FIELD_IS_BYTE == (ASF_PPI_LENGTH_TYPE_FLAGS & ASF_PPI_MASK_SEQUENCE_FIELD_SIZE))
  48. # define ASF_PPI_SEQUENCE_FIELD_SIZE 1
  49. #endif
  50. #if (ASF_PPI_FLAG_SEQUENCE_FIELD_IS_WORD == (ASF_PPI_LENGTH_TYPE_FLAGS & ASF_PPI_MASK_SEQUENCE_FIELD_SIZE))
  51. # define ASF_PPI_SEQUENCE_FIELD_SIZE 2
  52. #endif
  53. #if (ASF_PPI_FLAG_SEQUENCE_FIELD_IS_DWORD == (ASF_PPI_LENGTH_TYPE_FLAGS & ASF_PPI_MASK_SEQUENCE_FIELD_SIZE))
  54. # define ASF_PPI_SEQUENCE_FIELD_SIZE 4
  55. #endif
  56. #ifndef ASF_PPI_SEQUENCE_FIELD_SIZE
  57. # define ASF_PPI_SEQUENCE_FIELD_SIZE 0
  58. #endif
  59. #if (ASF_PPI_FLAG_PACKET_LENGTH_FIELD_IS_BYTE == (ASF_PPI_LENGTH_TYPE_FLAGS & ASF_PPI_MASK_PACKET_LENGTH_FIELD_SIZE))
  60. # define ASF_PPI_PACKET_LENGTH_FIELD_SIZE 1
  61. #endif
  62. #if (ASF_PPI_FLAG_PACKET_LENGTH_FIELD_IS_WORD == (ASF_PPI_LENGTH_TYPE_FLAGS & ASF_PPI_MASK_PACKET_LENGTH_FIELD_SIZE))
  63. # define ASF_PPI_PACKET_LENGTH_FIELD_SIZE 2
  64. #endif
  65. #if (ASF_PPI_FLAG_PACKET_LENGTH_FIELD_IS_DWORD == (ASF_PPI_LENGTH_TYPE_FLAGS & ASF_PPI_MASK_PACKET_LENGTH_FIELD_SIZE))
  66. # define ASF_PPI_PACKET_LENGTH_FIELD_SIZE 4
  67. #endif
  68. #ifndef ASF_PPI_PACKET_LENGTH_FIELD_SIZE
  69. # define ASF_PPI_PACKET_LENGTH_FIELD_SIZE 0
  70. #endif
  71. #if (ASF_PPI_FLAG_PADDING_LENGTH_FIELD_IS_BYTE == (ASF_PPI_LENGTH_TYPE_FLAGS & ASF_PPI_MASK_PADDING_LENGTH_FIELD_SIZE))
  72. # define ASF_PPI_PADDING_LENGTH_FIELD_SIZE 1
  73. #endif
  74. #if (ASF_PPI_FLAG_PADDING_LENGTH_FIELD_IS_WORD == (ASF_PPI_LENGTH_TYPE_FLAGS & ASF_PPI_MASK_PADDING_LENGTH_FIELD_SIZE))
  75. # define ASF_PPI_PADDING_LENGTH_FIELD_SIZE 2
  76. #endif
  77. #if (ASF_PPI_FLAG_PADDING_LENGTH_FIELD_IS_DWORD == (ASF_PPI_LENGTH_TYPE_FLAGS & ASF_PPI_MASK_PADDING_LENGTH_FIELD_SIZE))
  78. # define ASF_PPI_PADDING_LENGTH_FIELD_SIZE 4
  79. #endif
  80. #ifndef ASF_PPI_PADDING_LENGTH_FIELD_SIZE
  81. # define ASF_PPI_PADDING_LENGTH_FIELD_SIZE 0
  82. #endif
  83. #if (ASF_PL_FLAG_REPLICATED_DATA_LENGTH_FIELD_IS_BYTE == (ASF_PPI_PROPERTY_FLAGS & ASF_PL_MASK_REPLICATED_DATA_LENGTH_FIELD_SIZE))
  84. # define ASF_PAYLOAD_REPLICATED_DATA_LENGTH_FIELD_SIZE 1
  85. #endif
  86. #if (ASF_PL_FLAG_REPLICATED_DATA_LENGTH_FIELD_IS_WORD == (ASF_PPI_PROPERTY_FLAGS & ASF_PL_MASK_REPLICATED_DATA_LENGTH_FIELD_SIZE))
  87. # define ASF_PAYLOAD_REPLICATED_DATA_LENGTH_FIELD_SIZE 2
  88. #endif
  89. #if (ASF_PL_FLAG_REPLICATED_DATA_LENGTH_FIELD_IS_DWORD == (ASF_PPI_PROPERTY_FLAGS & ASF_PL_MASK_REPLICATED_DATA_LENGTH_FIELD_SIZE))
  90. # define ASF_PAYLOAD_REPLICATED_DATA_LENGTH_FIELD_SIZE 4
  91. #endif
  92. #ifndef ASF_PAYLOAD_REPLICATED_DATA_LENGTH_FIELD_SIZE
  93. # define ASF_PAYLOAD_REPLICATED_DATA_LENGTH_FIELD_SIZE 0
  94. #endif
  95. #if (ASF_PL_FLAG_OFFSET_INTO_MEDIA_OBJECT_LENGTH_FIELD_IS_BYTE == (ASF_PPI_PROPERTY_FLAGS & ASF_PL_MASK_OFFSET_INTO_MEDIA_OBJECT_LENGTH_FIELD_SIZE))
  96. # define ASF_PAYLOAD_OFFSET_INTO_MEDIA_OBJECT_FIELD_SIZE 1
  97. #endif
  98. #if (ASF_PL_FLAG_OFFSET_INTO_MEDIA_OBJECT_LENGTH_FIELD_IS_WORD == (ASF_PPI_PROPERTY_FLAGS & ASF_PL_MASK_OFFSET_INTO_MEDIA_OBJECT_LENGTH_FIELD_SIZE))
  99. # define ASF_PAYLOAD_OFFSET_INTO_MEDIA_OBJECT_FIELD_SIZE 2
  100. #endif
  101. #if (ASF_PL_FLAG_OFFSET_INTO_MEDIA_OBJECT_LENGTH_FIELD_IS_DWORD == (ASF_PPI_PROPERTY_FLAGS & ASF_PL_MASK_OFFSET_INTO_MEDIA_OBJECT_LENGTH_FIELD_SIZE))
  102. # define ASF_PAYLOAD_OFFSET_INTO_MEDIA_OBJECT_FIELD_SIZE 4
  103. #endif
  104. #ifndef ASF_PAYLOAD_OFFSET_INTO_MEDIA_OBJECT_FIELD_SIZE
  105. # define ASF_PAYLOAD_OFFSET_INTO_MEDIA_OBJECT_FIELD_SIZE 0
  106. #endif
  107. #if (ASF_PL_FLAG_MEDIA_OBJECT_NUMBER_LENGTH_FIELD_IS_BYTE == (ASF_PPI_PROPERTY_FLAGS & ASF_PL_MASK_MEDIA_OBJECT_NUMBER_LENGTH_FIELD_SIZE))
  108. # define ASF_PAYLOAD_MEDIA_OBJECT_NUMBER_FIELD_SIZE 1
  109. #endif
  110. #if (ASF_PL_FLAG_MEDIA_OBJECT_NUMBER_LENGTH_FIELD_IS_WORD == (ASF_PPI_PROPERTY_FLAGS & ASF_PL_MASK_MEDIA_OBJECT_NUMBER_LENGTH_FIELD_SIZE))
  111. # define ASF_PAYLOAD_MEDIA_OBJECT_NUMBER_FIELD_SIZE 2
  112. #endif
  113. #if (ASF_PL_FLAG_MEDIA_OBJECT_NUMBER_LENGTH_FIELD_IS_DWORD == (ASF_PPI_PROPERTY_FLAGS & ASF_PL_MASK_MEDIA_OBJECT_NUMBER_LENGTH_FIELD_SIZE))
  114. # define ASF_PAYLOAD_MEDIA_OBJECT_NUMBER_FIELD_SIZE 4
  115. #endif
  116. #ifndef ASF_PAYLOAD_MEDIA_OBJECT_NUMBER_FIELD_SIZE
  117. # define ASF_PAYLOAD_MEDIA_OBJECT_NUMBER_FIELD_SIZE 0
  118. #endif
  119. #if (ASF_PL_FLAG_PAYLOAD_LENGTH_FIELD_IS_BYTE == (ASF_PAYLOAD_FLAGS & ASF_PL_MASK_PAYLOAD_LENGTH_FIELD_SIZE))
  120. # define ASF_PAYLOAD_LENGTH_FIELD_SIZE 1
  121. #endif
  122. #if (ASF_PL_FLAG_PAYLOAD_LENGTH_FIELD_IS_WORD == (ASF_PAYLOAD_FLAGS & ASF_PL_MASK_PAYLOAD_LENGTH_FIELD_SIZE))
  123. # define ASF_PAYLOAD_LENGTH_FIELD_SIZE 2
  124. #endif
  125. #ifndef ASF_PAYLOAD_LENGTH_FIELD_SIZE
  126. # define ASF_PAYLOAD_LENGTH_FIELD_SIZE 0
  127. #endif
  128. #define PACKET_HEADER_MIN_SIZE (\
  129. ASF_PACKET_ERROR_CORRECTION_FLAGS_FIELD_SIZE + \
  130. ASF_PACKET_ERROR_CORRECTION_DATA_SIZE + \
  131. 1 + /*Length Type Flags*/ \
  132. 1 + /*Property Flags*/ \
  133. ASF_PPI_PACKET_LENGTH_FIELD_SIZE + \
  134. ASF_PPI_SEQUENCE_FIELD_SIZE + \
  135. ASF_PPI_PADDING_LENGTH_FIELD_SIZE + \
  136. 4 + /*Send Time Field*/ \
  137. 2 /*Duration Field*/ \
  138. )
  139. // Replicated Data shall be at least 8 bytes long.
  140. #define ASF_PAYLOAD_REPLICATED_DATA_LENGTH 0x08
  141. #define PAYLOAD_HEADER_SIZE_SINGLE_PAYLOAD (\
  142. 1 + /*Stream Number*/ \
  143. ASF_PAYLOAD_MEDIA_OBJECT_NUMBER_FIELD_SIZE + \
  144. ASF_PAYLOAD_OFFSET_INTO_MEDIA_OBJECT_FIELD_SIZE + \
  145. ASF_PAYLOAD_REPLICATED_DATA_LENGTH_FIELD_SIZE + \
  146. ASF_PAYLOAD_REPLICATED_DATA_LENGTH \
  147. )
  148. #define PAYLOAD_HEADER_SIZE_MULTIPLE_PAYLOADS (\
  149. 1 + /*Stream Number*/ \
  150. ASF_PAYLOAD_MEDIA_OBJECT_NUMBER_FIELD_SIZE + \
  151. ASF_PAYLOAD_OFFSET_INTO_MEDIA_OBJECT_FIELD_SIZE + \
  152. ASF_PAYLOAD_REPLICATED_DATA_LENGTH_FIELD_SIZE + \
  153. ASF_PAYLOAD_REPLICATED_DATA_LENGTH + \
  154. ASF_PAYLOAD_LENGTH_FIELD_SIZE \
  155. )
  156. #define SINGLE_PAYLOAD_DATA_LENGTH (\
  157. PACKET_SIZE - \
  158. PACKET_HEADER_MIN_SIZE - \
  159. PAYLOAD_HEADER_SIZE_SINGLE_PAYLOAD \
  160. )
  161. #define MULTI_PAYLOAD_CONSTANT (\
  162. PACKET_SIZE - \
  163. PACKET_HEADER_MIN_SIZE - \
  164. 1 - /*Payload Flags*/ \
  165. 2*PAYLOAD_HEADER_SIZE_MULTIPLE_PAYLOADS \
  166. )
  167. static const AVCodecTag codec_asf_bmp_tags[] = {
  168. { CODEC_ID_MPEG4, MKTAG('M', 'P', '4', 'S') },
  169. { CODEC_ID_MPEG4, MKTAG('M', '4', 'S', '2') },
  170. { CODEC_ID_MSMPEG4V3, MKTAG('M', 'P', '4', '3') },
  171. { CODEC_ID_NONE, 0 },
  172. };
  173. #define PREROLL_TIME 3100
  174. static void put_guid(ByteIOContext *s, const GUID *g)
  175. {
  176. assert(sizeof(*g) == 16);
  177. put_buffer(s, *g, sizeof(*g));
  178. }
  179. static void put_str16_nolen(ByteIOContext *s, const char *tag);
  180. static void put_str16(ByteIOContext *s, const char *tag)
  181. {
  182. put_le16(s,strlen(tag) + 1);
  183. put_str16_nolen(s, tag);
  184. }
  185. static void put_str16_nolen(ByteIOContext *s, const char *tag)
  186. {
  187. int c;
  188. do{
  189. c = (uint8_t)*tag++;
  190. put_le16(s, c);
  191. }while(c);
  192. }
  193. static int64_t put_header(ByteIOContext *pb, const GUID *g)
  194. {
  195. int64_t pos;
  196. pos = url_ftell(pb);
  197. put_guid(pb, g);
  198. put_le64(pb, 24);
  199. return pos;
  200. }
  201. /* update header size */
  202. static void end_header(ByteIOContext *pb, int64_t pos)
  203. {
  204. int64_t pos1;
  205. pos1 = url_ftell(pb);
  206. url_fseek(pb, pos + 16, SEEK_SET);
  207. put_le64(pb, pos1 - pos);
  208. url_fseek(pb, pos1, SEEK_SET);
  209. }
  210. /* write an asf chunk (only used in streaming case) */
  211. static void put_chunk(AVFormatContext *s, int type, int payload_length, int flags)
  212. {
  213. ASFContext *asf = s->priv_data;
  214. ByteIOContext *pb = s->pb;
  215. int length;
  216. length = payload_length + 8;
  217. put_le16(pb, type);
  218. put_le16(pb, length); //size
  219. put_le32(pb, asf->seqno);//sequence number
  220. put_le16(pb, flags); /* unknown bytes */
  221. put_le16(pb, length); //size_confirm
  222. asf->seqno++;
  223. }
  224. /* convert from unix to windows time */
  225. static int64_t unix_to_file_time(int ti)
  226. {
  227. int64_t t;
  228. t = ti * INT64_C(10000000);
  229. t += INT64_C(116444736000000000);
  230. return t;
  231. }
  232. /* write the header (used two times if non streamed) */
  233. static int asf_write_header1(AVFormatContext *s, int64_t file_size, int64_t data_chunk_size)
  234. {
  235. ASFContext *asf = s->priv_data;
  236. ByteIOContext *pb = s->pb;
  237. AVMetadataTag *title, *author, *copyright, *comment;
  238. int header_size, n, extra_size, extra_size2, wav_extra_size, file_time;
  239. int has_title;
  240. int metadata_count;
  241. AVCodecContext *enc;
  242. int64_t header_offset, cur_pos, hpos;
  243. int bit_rate;
  244. int64_t duration;
  245. title = av_metadata_get(s->metadata, "title" , NULL, 0);
  246. author = av_metadata_get(s->metadata, "author" , NULL, 0);
  247. copyright = av_metadata_get(s->metadata, "copyright", NULL, 0);
  248. comment = av_metadata_get(s->metadata, "comment" , NULL, 0);
  249. duration = asf->duration + PREROLL_TIME * 10000;
  250. has_title = title || author || copyright || comment;
  251. metadata_count = s->metadata ? s->metadata->count : 0;
  252. bit_rate = 0;
  253. for(n=0;n<s->nb_streams;n++) {
  254. enc = s->streams[n]->codec;
  255. av_set_pts_info(s->streams[n], 32, 1, 1000); /* 32 bit pts in ms */
  256. bit_rate += enc->bit_rate;
  257. }
  258. if (asf->is_streamed) {
  259. put_chunk(s, 0x4824, 0, 0xc00); /* start of stream (length will be patched later) */
  260. }
  261. put_guid(pb, &ff_asf_header);
  262. put_le64(pb, -1); /* header length, will be patched after */
  263. put_le32(pb, 3 + has_title + !!metadata_count + s->nb_streams); /* number of chunks in header */
  264. put_byte(pb, 1); /* ??? */
  265. put_byte(pb, 2); /* ??? */
  266. /* file header */
  267. header_offset = url_ftell(pb);
  268. hpos = put_header(pb, &ff_asf_file_header);
  269. put_guid(pb, &ff_asf_my_guid);
  270. put_le64(pb, file_size);
  271. file_time = 0;
  272. put_le64(pb, unix_to_file_time(file_time));
  273. put_le64(pb, asf->nb_packets); /* number of packets */
  274. put_le64(pb, duration); /* end time stamp (in 100ns units) */
  275. put_le64(pb, asf->duration); /* duration (in 100ns units) */
  276. put_le64(pb, PREROLL_TIME); /* start time stamp */
  277. put_le32(pb, (asf->is_streamed || url_is_streamed(pb)) ? 3 : 2); /* ??? */
  278. put_le32(pb, s->packet_size); /* packet size */
  279. put_le32(pb, s->packet_size); /* packet size */
  280. put_le32(pb, bit_rate); /* Nominal data rate in bps */
  281. end_header(pb, hpos);
  282. /* unknown headers */
  283. hpos = put_header(pb, &ff_asf_head1_guid);
  284. put_guid(pb, &ff_asf_head2_guid);
  285. put_le32(pb, 6);
  286. put_le16(pb, 0);
  287. end_header(pb, hpos);
  288. /* title and other infos */
  289. if (has_title) {
  290. hpos = put_header(pb, &ff_asf_comment_header);
  291. put_le16(pb, title ? 2 * (strlen(title->value ) + 1) : 0);
  292. put_le16(pb, author ? 2 * (strlen(author->value ) + 1) : 0);
  293. put_le16(pb, copyright ? 2 * (strlen(copyright->value) + 1) : 0);
  294. put_le16(pb, comment ? 2 * (strlen(comment->value ) + 1) : 0);
  295. put_le16(pb, 0);
  296. if (title ) put_str16_nolen(pb, title->value );
  297. if (author ) put_str16_nolen(pb, author->value );
  298. if (copyright) put_str16_nolen(pb, copyright->value);
  299. if (comment ) put_str16_nolen(pb, comment->value );
  300. end_header(pb, hpos);
  301. }
  302. if (metadata_count) {
  303. AVMetadataTag *tag = NULL;
  304. hpos = put_header(pb, &ff_asf_extended_content_header);
  305. put_le16(pb, metadata_count);
  306. while ((tag = av_metadata_get(s->metadata, "", tag, AV_METADATA_IGNORE_SUFFIX))) {
  307. put_le16(pb, 2*(strlen(tag->key) + 3) + 1);
  308. put_le16(pb, 'W');
  309. put_le16(pb, 'M');
  310. put_le16(pb, '/');
  311. put_str16_nolen(pb, tag->key);
  312. put_le16(pb, 0);
  313. put_le16(pb, 2*strlen(tag->value) + 1);
  314. put_str16_nolen(pb, tag->value);
  315. }
  316. end_header(pb, hpos);
  317. }
  318. /* stream headers */
  319. for(n=0;n<s->nb_streams;n++) {
  320. int64_t es_pos;
  321. // ASFStream *stream = &asf->streams[n];
  322. enc = s->streams[n]->codec;
  323. asf->streams[n].num = n + 1;
  324. asf->streams[n].seq = 0;
  325. switch(enc->codec_type) {
  326. case CODEC_TYPE_AUDIO:
  327. wav_extra_size = 0;
  328. extra_size = 18 + wav_extra_size;
  329. extra_size2 = 8;
  330. break;
  331. default:
  332. case CODEC_TYPE_VIDEO:
  333. wav_extra_size = enc->extradata_size;
  334. extra_size = 0x33 + wav_extra_size;
  335. extra_size2 = 0;
  336. break;
  337. }
  338. hpos = put_header(pb, &ff_asf_stream_header);
  339. if (enc->codec_type == CODEC_TYPE_AUDIO) {
  340. put_guid(pb, &ff_asf_audio_stream);
  341. put_guid(pb, &ff_asf_audio_conceal_spread);
  342. } else {
  343. put_guid(pb, &ff_asf_video_stream);
  344. put_guid(pb, &ff_asf_video_conceal_none);
  345. }
  346. put_le64(pb, 0); /* ??? */
  347. es_pos = url_ftell(pb);
  348. put_le32(pb, extra_size); /* wav header len */
  349. put_le32(pb, extra_size2); /* additional data len */
  350. put_le16(pb, n + 1); /* stream number */
  351. put_le32(pb, 0); /* ??? */
  352. if (enc->codec_type == CODEC_TYPE_AUDIO) {
  353. /* WAVEFORMATEX header */
  354. int wavsize = put_wav_header(pb, enc);
  355. if ((enc->codec_id != CODEC_ID_MP3) && (enc->codec_id != CODEC_ID_MP2) && (enc->codec_id != CODEC_ID_ADPCM_IMA_WAV) && (enc->extradata_size==0)) {
  356. wavsize += 2;
  357. put_le16(pb, 0);
  358. }
  359. if (wavsize < 0)
  360. return -1;
  361. if (wavsize != extra_size) {
  362. cur_pos = url_ftell(pb);
  363. url_fseek(pb, es_pos, SEEK_SET);
  364. put_le32(pb, wavsize); /* wav header len */
  365. url_fseek(pb, cur_pos, SEEK_SET);
  366. }
  367. /* ERROR Correction */
  368. put_byte(pb, 0x01);
  369. if(enc->codec_id == CODEC_ID_ADPCM_G726 || !enc->block_align){
  370. put_le16(pb, 0x0190);
  371. put_le16(pb, 0x0190);
  372. }else{
  373. put_le16(pb, enc->block_align);
  374. put_le16(pb, enc->block_align);
  375. }
  376. put_le16(pb, 0x01);
  377. put_byte(pb, 0x00);
  378. } else {
  379. put_le32(pb, enc->width);
  380. put_le32(pb, enc->height);
  381. put_byte(pb, 2); /* ??? */
  382. put_le16(pb, 40 + enc->extradata_size); /* size */
  383. /* BITMAPINFOHEADER header */
  384. put_bmp_header(pb, enc, codec_bmp_tags, 1);
  385. }
  386. end_header(pb, hpos);
  387. }
  388. /* media comments */
  389. hpos = put_header(pb, &ff_asf_codec_comment_header);
  390. put_guid(pb, &ff_asf_codec_comment1_header);
  391. put_le32(pb, s->nb_streams);
  392. for(n=0;n<s->nb_streams;n++) {
  393. AVCodec *p;
  394. enc = s->streams[n]->codec;
  395. p = avcodec_find_encoder(enc->codec_id);
  396. if(enc->codec_type == CODEC_TYPE_AUDIO)
  397. put_le16(pb, 2);
  398. else if(enc->codec_type == CODEC_TYPE_VIDEO)
  399. put_le16(pb, 1);
  400. else
  401. put_le16(pb, -1);
  402. if(enc->codec_id == CODEC_ID_WMAV2)
  403. put_str16(pb, "Windows Media Audio V8");
  404. else
  405. put_str16(pb, p ? p->name : enc->codec_name);
  406. put_le16(pb, 0); /* no parameters */
  407. /* id */
  408. if (enc->codec_type == CODEC_TYPE_AUDIO) {
  409. put_le16(pb, 2);
  410. put_le16(pb, enc->codec_tag);
  411. } else {
  412. put_le16(pb, 4);
  413. put_le32(pb, enc->codec_tag);
  414. }
  415. if(!enc->codec_tag)
  416. return -1;
  417. }
  418. end_header(pb, hpos);
  419. /* patch the header size fields */
  420. cur_pos = url_ftell(pb);
  421. header_size = cur_pos - header_offset;
  422. if (asf->is_streamed) {
  423. header_size += 8 + 30 + 50;
  424. url_fseek(pb, header_offset - 10 - 30, SEEK_SET);
  425. put_le16(pb, header_size);
  426. url_fseek(pb, header_offset - 2 - 30, SEEK_SET);
  427. put_le16(pb, header_size);
  428. header_size -= 8 + 30 + 50;
  429. }
  430. header_size += 24 + 6;
  431. url_fseek(pb, header_offset - 14, SEEK_SET);
  432. put_le64(pb, header_size);
  433. url_fseek(pb, cur_pos, SEEK_SET);
  434. /* movie chunk, followed by packets of packet_size */
  435. asf->data_offset = cur_pos;
  436. put_guid(pb, &ff_asf_data_header);
  437. put_le64(pb, data_chunk_size);
  438. put_guid(pb, &ff_asf_my_guid);
  439. put_le64(pb, asf->nb_packets); /* nb packets */
  440. put_byte(pb, 1); /* ??? */
  441. put_byte(pb, 1); /* ??? */
  442. return 0;
  443. }
  444. static int asf_write_header(AVFormatContext *s)
  445. {
  446. ASFContext *asf = s->priv_data;
  447. s->packet_size = PACKET_SIZE;
  448. asf->nb_packets = 0;
  449. asf->last_indexed_pts = 0;
  450. asf->index_ptr = av_malloc( sizeof(ASFIndex) * ASF_INDEX_BLOCK );
  451. asf->nb_index_memory_alloc = ASF_INDEX_BLOCK;
  452. asf->nb_index_count = 0;
  453. asf->maximum_packet = 0;
  454. /* the data-chunk-size has to be 50, which is data_size - asf->data_offset
  455. * at the moment this function is done. It is needed to use asf as
  456. * streamable format. */
  457. if (asf_write_header1(s, 0, 50) < 0) {
  458. //av_free(asf);
  459. return -1;
  460. }
  461. put_flush_packet(s->pb);
  462. asf->packet_nb_payloads = 0;
  463. asf->packet_timestamp_start = -1;
  464. asf->packet_timestamp_end = -1;
  465. init_put_byte(&asf->pb, asf->packet_buf, s->packet_size, 1,
  466. NULL, NULL, NULL, NULL);
  467. return 0;
  468. }
  469. static int asf_write_stream_header(AVFormatContext *s)
  470. {
  471. ASFContext *asf = s->priv_data;
  472. asf->is_streamed = 1;
  473. return asf_write_header(s);
  474. }
  475. static int put_payload_parsing_info(
  476. AVFormatContext *s,
  477. unsigned int sendtime,
  478. unsigned int duration,
  479. int nb_payloads,
  480. int padsize
  481. )
  482. {
  483. ASFContext *asf = s->priv_data;
  484. ByteIOContext *pb = s->pb;
  485. int ppi_size, i;
  486. int64_t start= url_ftell(pb);
  487. int iLengthTypeFlags = ASF_PPI_LENGTH_TYPE_FLAGS;
  488. padsize -= PACKET_HEADER_MIN_SIZE;
  489. if(asf->multi_payloads_present)
  490. padsize--;
  491. assert(padsize>=0);
  492. put_byte(pb, ASF_PACKET_ERROR_CORRECTION_FLAGS);
  493. for (i = 0; i < ASF_PACKET_ERROR_CORRECTION_DATA_SIZE; i++){
  494. put_byte(pb, 0x0);
  495. }
  496. if (asf->multi_payloads_present)
  497. iLengthTypeFlags |= ASF_PPI_FLAG_MULTIPLE_PAYLOADS_PRESENT;
  498. if (padsize > 0) {
  499. if (padsize < 256)
  500. iLengthTypeFlags |= ASF_PPI_FLAG_PADDING_LENGTH_FIELD_IS_BYTE;
  501. else
  502. iLengthTypeFlags |= ASF_PPI_FLAG_PADDING_LENGTH_FIELD_IS_WORD;
  503. }
  504. put_byte(pb, iLengthTypeFlags);
  505. put_byte(pb, ASF_PPI_PROPERTY_FLAGS);
  506. if (iLengthTypeFlags & ASF_PPI_FLAG_PADDING_LENGTH_FIELD_IS_WORD)
  507. put_le16(pb, padsize - 2);
  508. if (iLengthTypeFlags & ASF_PPI_FLAG_PADDING_LENGTH_FIELD_IS_BYTE)
  509. put_byte(pb, padsize - 1);
  510. put_le32(pb, sendtime);
  511. put_le16(pb, duration);
  512. if (asf->multi_payloads_present)
  513. put_byte(pb, nb_payloads | ASF_PAYLOAD_FLAGS);
  514. ppi_size = url_ftell(pb) - start;
  515. return ppi_size;
  516. }
  517. static void flush_packet(AVFormatContext *s)
  518. {
  519. ASFContext *asf = s->priv_data;
  520. int packet_hdr_size, packet_filled_size;
  521. assert(asf->packet_timestamp_end >= asf->packet_timestamp_start);
  522. if (asf->is_streamed) {
  523. put_chunk(s, 0x4424, s->packet_size, 0);
  524. }
  525. packet_hdr_size = put_payload_parsing_info(
  526. s,
  527. asf->packet_timestamp_start,
  528. asf->packet_timestamp_end - asf->packet_timestamp_start,
  529. asf->packet_nb_payloads,
  530. asf->packet_size_left
  531. );
  532. packet_filled_size = PACKET_SIZE - asf->packet_size_left;
  533. assert(packet_hdr_size <= asf->packet_size_left);
  534. memset(asf->packet_buf + packet_filled_size, 0, asf->packet_size_left);
  535. put_buffer(s->pb, asf->packet_buf, s->packet_size - packet_hdr_size);
  536. put_flush_packet(s->pb);
  537. asf->nb_packets++;
  538. asf->packet_nb_payloads = 0;
  539. asf->packet_timestamp_start = -1;
  540. asf->packet_timestamp_end = -1;
  541. init_put_byte(&asf->pb, asf->packet_buf, s->packet_size, 1,
  542. NULL, NULL, NULL, NULL);
  543. }
  544. static void put_payload_header(
  545. AVFormatContext *s,
  546. ASFStream *stream,
  547. int presentation_time,
  548. int m_obj_size,
  549. int m_obj_offset,
  550. int payload_len,
  551. int flags
  552. )
  553. {
  554. ASFContext *asf = s->priv_data;
  555. ByteIOContext *pb = &asf->pb;
  556. int val;
  557. val = stream->num;
  558. if (flags & PKT_FLAG_KEY)
  559. val |= ASF_PL_FLAG_KEY_FRAME;
  560. put_byte(pb, val);
  561. put_byte(pb, stream->seq); //Media object number
  562. put_le32(pb, m_obj_offset); //Offset Into Media Object
  563. // Replicated Data shall be at least 8 bytes long.
  564. // The first 4 bytes of data shall contain the
  565. // Size of the Media Object that the payload belongs to.
  566. // The next 4 bytes of data shall contain the
  567. // Presentation Time for the media object that the payload belongs to.
  568. put_byte(pb, ASF_PAYLOAD_REPLICATED_DATA_LENGTH);
  569. put_le32(pb, m_obj_size); //Replicated Data - Media Object Size
  570. put_le32(pb, presentation_time);//Replicated Data - Presentation Time
  571. if (asf->multi_payloads_present){
  572. put_le16(pb, payload_len); //payload length
  573. }
  574. }
  575. static void put_frame(
  576. AVFormatContext *s,
  577. ASFStream *stream,
  578. AVStream *avst,
  579. int timestamp,
  580. const uint8_t *buf,
  581. int m_obj_size,
  582. int flags
  583. )
  584. {
  585. ASFContext *asf = s->priv_data;
  586. int m_obj_offset, payload_len, frag_len1;
  587. m_obj_offset = 0;
  588. while (m_obj_offset < m_obj_size) {
  589. payload_len = m_obj_size - m_obj_offset;
  590. if (asf->packet_timestamp_start == -1) {
  591. asf->multi_payloads_present = (payload_len < MULTI_PAYLOAD_CONSTANT);
  592. asf->packet_size_left = PACKET_SIZE;
  593. if (asf->multi_payloads_present){
  594. frag_len1 = MULTI_PAYLOAD_CONSTANT - 1;
  595. }
  596. else {
  597. frag_len1 = SINGLE_PAYLOAD_DATA_LENGTH;
  598. }
  599. asf->packet_timestamp_start = timestamp;
  600. }
  601. else {
  602. // multi payloads
  603. frag_len1 = asf->packet_size_left - PAYLOAD_HEADER_SIZE_MULTIPLE_PAYLOADS - PACKET_HEADER_MIN_SIZE - 1;
  604. if(frag_len1 < payload_len && avst->codec->codec_type == CODEC_TYPE_AUDIO){
  605. flush_packet(s);
  606. continue;
  607. }
  608. }
  609. if (frag_len1 > 0) {
  610. if (payload_len > frag_len1)
  611. payload_len = frag_len1;
  612. else if (payload_len == (frag_len1 - 1))
  613. payload_len = frag_len1 - 2; //additional byte need to put padding length
  614. put_payload_header(s, stream, timestamp+PREROLL_TIME, m_obj_size, m_obj_offset, payload_len, flags);
  615. put_buffer(&asf->pb, buf, payload_len);
  616. if (asf->multi_payloads_present)
  617. asf->packet_size_left -= (payload_len + PAYLOAD_HEADER_SIZE_MULTIPLE_PAYLOADS);
  618. else
  619. asf->packet_size_left -= (payload_len + PAYLOAD_HEADER_SIZE_SINGLE_PAYLOAD);
  620. asf->packet_timestamp_end = timestamp;
  621. asf->packet_nb_payloads++;
  622. } else {
  623. payload_len = 0;
  624. }
  625. m_obj_offset += payload_len;
  626. buf += payload_len;
  627. if (!asf->multi_payloads_present)
  628. flush_packet(s);
  629. else if (asf->packet_size_left <= (PAYLOAD_HEADER_SIZE_MULTIPLE_PAYLOADS + PACKET_HEADER_MIN_SIZE + 1))
  630. flush_packet(s);
  631. }
  632. stream->seq++;
  633. }
  634. static int asf_write_packet(AVFormatContext *s, AVPacket *pkt)
  635. {
  636. ASFContext *asf = s->priv_data;
  637. ASFStream *stream;
  638. int64_t duration;
  639. AVCodecContext *codec;
  640. int64_t packet_st,pts;
  641. int start_sec,i;
  642. int flags= pkt->flags;
  643. codec = s->streams[pkt->stream_index]->codec;
  644. stream = &asf->streams[pkt->stream_index];
  645. if(codec->codec_type == CODEC_TYPE_AUDIO)
  646. flags &= ~PKT_FLAG_KEY;
  647. pts = (pkt->pts != AV_NOPTS_VALUE) ? pkt->pts : pkt->dts;
  648. assert(pts != AV_NOPTS_VALUE);
  649. duration = pts * 10000;
  650. asf->duration= FFMAX(asf->duration, duration + pkt->duration * 10000);
  651. packet_st = asf->nb_packets;
  652. put_frame(s, stream, s->streams[pkt->stream_index], pkt->dts, pkt->data, pkt->size, flags);
  653. /* check index */
  654. if ((!asf->is_streamed) && (flags & PKT_FLAG_KEY)) {
  655. start_sec = (int)(duration / INT64_C(10000000));
  656. if (start_sec != (int)(asf->last_indexed_pts / INT64_C(10000000))) {
  657. for(i=asf->nb_index_count;i<start_sec;i++) {
  658. if (i>=asf->nb_index_memory_alloc) {
  659. asf->nb_index_memory_alloc += ASF_INDEX_BLOCK;
  660. asf->index_ptr = (ASFIndex*)av_realloc( asf->index_ptr, sizeof(ASFIndex) * asf->nb_index_memory_alloc );
  661. }
  662. // store
  663. asf->index_ptr[i].packet_number = (uint32_t)packet_st;
  664. asf->index_ptr[i].packet_count = (uint16_t)(asf->nb_packets-packet_st);
  665. asf->maximum_packet = FFMAX(asf->maximum_packet, (uint16_t)(asf->nb_packets-packet_st));
  666. }
  667. asf->nb_index_count = start_sec;
  668. asf->last_indexed_pts = duration;
  669. }
  670. }
  671. return 0;
  672. }
  673. //
  674. static int asf_write_index(AVFormatContext *s, ASFIndex *index, uint16_t max, uint32_t count)
  675. {
  676. ByteIOContext *pb = s->pb;
  677. int i;
  678. put_guid(pb, &ff_asf_simple_index_header);
  679. put_le64(pb, 24 + 16 + 8 + 4 + 4 + (4 + 2)*count);
  680. put_guid(pb, &ff_asf_my_guid);
  681. put_le64(pb, ASF_INDEXED_INTERVAL);
  682. put_le32(pb, max);
  683. put_le32(pb, count);
  684. for(i=0; i<count; i++) {
  685. put_le32(pb, index[i].packet_number);
  686. put_le16(pb, index[i].packet_count);
  687. }
  688. return 0;
  689. }
  690. static int asf_write_trailer(AVFormatContext *s)
  691. {
  692. ASFContext *asf = s->priv_data;
  693. int64_t file_size,data_size;
  694. /* flush the current packet */
  695. if (asf->pb.buf_ptr > asf->pb.buffer)
  696. flush_packet(s);
  697. /* write index */
  698. data_size = url_ftell(s->pb);
  699. if ((!asf->is_streamed) && (asf->nb_index_count != 0)) {
  700. asf_write_index(s, asf->index_ptr, asf->maximum_packet, asf->nb_index_count);
  701. }
  702. put_flush_packet(s->pb);
  703. if (asf->is_streamed || url_is_streamed(s->pb)) {
  704. put_chunk(s, 0x4524, 0, 0); /* end of stream */
  705. } else {
  706. /* rewrite an updated header */
  707. file_size = url_ftell(s->pb);
  708. url_fseek(s->pb, 0, SEEK_SET);
  709. asf_write_header1(s, file_size, data_size - asf->data_offset);
  710. }
  711. put_flush_packet(s->pb);
  712. av_free(asf->index_ptr);
  713. return 0;
  714. }
  715. #if CONFIG_ASF_MUXER
  716. AVOutputFormat asf_muxer = {
  717. "asf",
  718. NULL_IF_CONFIG_SMALL("ASF format"),
  719. "video/x-ms-asf",
  720. "asf,wmv,wma",
  721. sizeof(ASFContext),
  722. #if CONFIG_LIBMP3LAME
  723. CODEC_ID_MP3,
  724. #else
  725. CODEC_ID_MP2,
  726. #endif
  727. CODEC_ID_MSMPEG4V3,
  728. asf_write_header,
  729. asf_write_packet,
  730. asf_write_trailer,
  731. .flags = AVFMT_GLOBALHEADER,
  732. .codec_tag= (const AVCodecTag* const []){codec_asf_bmp_tags, codec_bmp_tags, codec_wav_tags, 0},
  733. .metadata_conv = ff_asf_metadata_conv,
  734. };
  735. #endif
  736. #if CONFIG_ASF_STREAM_MUXER
  737. AVOutputFormat asf_stream_muxer = {
  738. "asf_stream",
  739. NULL_IF_CONFIG_SMALL("ASF format"),
  740. "video/x-ms-asf",
  741. "asf,wmv,wma",
  742. sizeof(ASFContext),
  743. #if CONFIG_LIBMP3LAME
  744. CODEC_ID_MP3,
  745. #else
  746. CODEC_ID_MP2,
  747. #endif
  748. CODEC_ID_MSMPEG4V3,
  749. asf_write_stream_header,
  750. asf_write_packet,
  751. asf_write_trailer,
  752. .flags = AVFMT_GLOBALHEADER,
  753. .codec_tag= (const AVCodecTag* const []){codec_asf_bmp_tags, codec_bmp_tags, codec_wav_tags, 0},
  754. .metadata_conv = ff_asf_metadata_conv,
  755. };
  756. #endif //CONFIG_ASF_STREAM_MUXER