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.

772 lines
25KB

  1. /*
  2. * AVI encoder.
  3. * Copyright (c) 2000 Fabrice Bellard.
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include "avformat.h"
  20. #include "avi.h"
  21. /*
  22. * TODO:
  23. * - fill all fields if non streamed (nb_frames for example)
  24. */
  25. #ifdef CONFIG_MUXERS
  26. typedef struct AVIIentry {
  27. unsigned int flags, pos, len;
  28. } AVIIentry;
  29. #define AVI_INDEX_CLUSTER_SIZE 16384
  30. typedef struct AVIIndex {
  31. offset_t indx_start;
  32. int entry;
  33. int ents_allocated;
  34. AVIIentry** cluster;
  35. } AVIIndex;
  36. typedef struct {
  37. offset_t riff_start, movi_list, odml_list;
  38. offset_t frames_hdr_all, frames_hdr_strm[MAX_STREAMS];
  39. int audio_strm_length[MAX_STREAMS];
  40. int riff_id;
  41. int packet_count[MAX_STREAMS];
  42. AVIIndex indexes[MAX_STREAMS];
  43. } AVIContext;
  44. static inline AVIIentry* avi_get_ientry(AVIIndex* idx, int ent_id)
  45. {
  46. int cl = ent_id / AVI_INDEX_CLUSTER_SIZE;
  47. int id = ent_id % AVI_INDEX_CLUSTER_SIZE;
  48. return &idx->cluster[cl][id];
  49. }
  50. offset_t start_tag(ByteIOContext *pb, const char *tag)
  51. {
  52. put_tag(pb, tag);
  53. put_le32(pb, 0);
  54. return url_ftell(pb);
  55. }
  56. void end_tag(ByteIOContext *pb, offset_t start)
  57. {
  58. offset_t pos;
  59. pos = url_ftell(pb);
  60. url_fseek(pb, start - 4, SEEK_SET);
  61. put_le32(pb, (uint32_t)(pos - start));
  62. url_fseek(pb, pos, SEEK_SET);
  63. }
  64. #endif //CONFIG_MUXERS
  65. /* Note: when encoding, the first matching tag is used, so order is
  66. important if multiple tags possible for a given codec. */
  67. const CodecTag codec_bmp_tags[] = {
  68. { CODEC_ID_H264, MKTAG('H', '2', '6', '4') },
  69. { CODEC_ID_H264, MKTAG('V', 'S', 'S', 'H') },
  70. { CODEC_ID_H263, MKTAG('H', '2', '6', '3') },
  71. { CODEC_ID_H263P, MKTAG('H', '2', '6', '3') },
  72. { CODEC_ID_H263I, MKTAG('I', '2', '6', '3') }, /* intel h263 */
  73. { CODEC_ID_H261, MKTAG('H', '2', '6', '1') },
  74. /* added based on MPlayer */
  75. { CODEC_ID_H263P, MKTAG('U', '2', '6', '3') },
  76. { CODEC_ID_H263P, MKTAG('v', 'i', 'v', '1') },
  77. { CODEC_ID_MPEG4, MKTAG('F', 'M', 'P', '4')},
  78. { CODEC_ID_MPEG4, MKTAG('D', 'I', 'V', 'X'), .invalid_asf = 1 },
  79. { CODEC_ID_MPEG4, MKTAG('D', 'X', '5', '0'), .invalid_asf = 1 },
  80. { CODEC_ID_MPEG4, MKTAG('X', 'V', 'I', 'D'), .invalid_asf = 1 },
  81. { CODEC_ID_MPEG4, MKTAG('M', 'P', '4', 'S') },
  82. { CODEC_ID_MPEG4, MKTAG('M', '4', 'S', '2') },
  83. { CODEC_ID_MPEG4, MKTAG(0x04, 0, 0, 0) }, /* some broken avi use this */
  84. /* added based on MPlayer */
  85. { CODEC_ID_MPEG4, MKTAG('D', 'I', 'V', '1') },
  86. { CODEC_ID_MPEG4, MKTAG('B', 'L', 'Z', '0') },
  87. { CODEC_ID_MPEG4, MKTAG('m', 'p', '4', 'v') },
  88. { CODEC_ID_MPEG4, MKTAG('U', 'M', 'P', '4') },
  89. { CODEC_ID_MPEG4, MKTAG('W', 'V', '1', 'F') },
  90. { CODEC_ID_MSMPEG4V3, MKTAG('D', 'I', 'V', '3'), .invalid_asf = 1 }, /* default signature when using MSMPEG4 */
  91. { CODEC_ID_MSMPEG4V3, MKTAG('M', 'P', '4', '3') },
  92. /* added based on MPlayer */
  93. { CODEC_ID_MSMPEG4V3, MKTAG('M', 'P', 'G', '3') },
  94. { CODEC_ID_MSMPEG4V3, MKTAG('D', 'I', 'V', '5') },
  95. { CODEC_ID_MSMPEG4V3, MKTAG('D', 'I', 'V', '6') },
  96. { CODEC_ID_MSMPEG4V3, MKTAG('D', 'I', 'V', '4') },
  97. { CODEC_ID_MSMPEG4V3, MKTAG('A', 'P', '4', '1') },
  98. { CODEC_ID_MSMPEG4V3, MKTAG('C', 'O', 'L', '1') },
  99. { CODEC_ID_MSMPEG4V3, MKTAG('C', 'O', 'L', '0') },
  100. { CODEC_ID_MSMPEG4V2, MKTAG('M', 'P', '4', '2') },
  101. /* added based on MPlayer */
  102. { CODEC_ID_MSMPEG4V2, MKTAG('D', 'I', 'V', '2') },
  103. { CODEC_ID_MSMPEG4V1, MKTAG('M', 'P', 'G', '4') },
  104. { CODEC_ID_WMV1, MKTAG('W', 'M', 'V', '1') },
  105. /* added based on MPlayer */
  106. { CODEC_ID_WMV2, MKTAG('W', 'M', 'V', '2') },
  107. { CODEC_ID_DVVIDEO, MKTAG('d', 'v', 's', 'd') },
  108. { CODEC_ID_DVVIDEO, MKTAG('d', 'v', 'h', 'd') },
  109. { CODEC_ID_DVVIDEO, MKTAG('d', 'v', 's', 'l') },
  110. { CODEC_ID_DVVIDEO, MKTAG('d', 'v', '2', '5') },
  111. { CODEC_ID_MPEG1VIDEO, MKTAG('m', 'p', 'g', '1') },
  112. { CODEC_ID_MPEG1VIDEO, MKTAG('m', 'p', 'g', '2') },
  113. { CODEC_ID_MPEG2VIDEO, MKTAG('m', 'p', 'g', '2') },
  114. { CODEC_ID_MPEG1VIDEO, MKTAG('P', 'I', 'M', '1') },
  115. { CODEC_ID_MPEG1VIDEO, MKTAG('V', 'C', 'R', '2') },
  116. { CODEC_ID_MPEG1VIDEO, 0x10000001 },
  117. { CODEC_ID_MPEG2VIDEO, 0x10000002 },
  118. { CODEC_ID_MPEG2VIDEO, MKTAG('D', 'V', 'R', ' ') },
  119. { CODEC_ID_MJPEG, MKTAG('M', 'J', 'P', 'G') },
  120. { CODEC_ID_MJPEG, MKTAG('L', 'J', 'P', 'G') },
  121. { CODEC_ID_LJPEG, MKTAG('L', 'J', 'P', 'G') },
  122. { CODEC_ID_MJPEG, MKTAG('J', 'P', 'G', 'L') }, /* Pegasus lossless JPEG */
  123. { CODEC_ID_HUFFYUV, MKTAG('H', 'F', 'Y', 'U') },
  124. { CODEC_ID_FFVHUFF, MKTAG('F', 'F', 'V', 'H') },
  125. { CODEC_ID_CYUV, MKTAG('C', 'Y', 'U', 'V') },
  126. { CODEC_ID_RAWVIDEO, MKTAG('I', '4', '2', '0') },
  127. { CODEC_ID_RAWVIDEO, MKTAG('Y', 'U', 'Y', '2') },
  128. { CODEC_ID_RAWVIDEO, MKTAG('Y', '4', '2', '2') },
  129. { CODEC_ID_RAWVIDEO, MKTAG('U', 'Y', 'V', 'Y') },
  130. { CODEC_ID_RAWVIDEO, MKTAG('I', 'Y', 'U', 'V') },
  131. { CODEC_ID_INDEO3, MKTAG('I', 'V', '3', '1') },
  132. { CODEC_ID_INDEO3, MKTAG('I', 'V', '3', '2') },
  133. { CODEC_ID_VP3, MKTAG('V', 'P', '3', '1') },
  134. { CODEC_ID_VP3, MKTAG('V', 'P', '3', '0') },
  135. { CODEC_ID_ASV1, MKTAG('A', 'S', 'V', '1') },
  136. { CODEC_ID_ASV2, MKTAG('A', 'S', 'V', '2') },
  137. { CODEC_ID_VCR1, MKTAG('V', 'C', 'R', '1') },
  138. { CODEC_ID_FFV1, MKTAG('F', 'F', 'V', '1') },
  139. { CODEC_ID_XAN_WC4, MKTAG('X', 'x', 'a', 'n') },
  140. { CODEC_ID_MSRLE, MKTAG('m', 'r', 'l', 'e') },
  141. { CODEC_ID_MSRLE, MKTAG(0x1, 0x0, 0x0, 0x0) },
  142. { CODEC_ID_MSVIDEO1, MKTAG('M', 'S', 'V', 'C') },
  143. { CODEC_ID_MSVIDEO1, MKTAG('m', 's', 'v', 'c') },
  144. { CODEC_ID_MSVIDEO1, MKTAG('C', 'R', 'A', 'M') },
  145. { CODEC_ID_MSVIDEO1, MKTAG('c', 'r', 'a', 'm') },
  146. { CODEC_ID_MSVIDEO1, MKTAG('W', 'H', 'A', 'M') },
  147. { CODEC_ID_MSVIDEO1, MKTAG('w', 'h', 'a', 'm') },
  148. { CODEC_ID_CINEPAK, MKTAG('c', 'v', 'i', 'd') },
  149. { CODEC_ID_TRUEMOTION1, MKTAG('D', 'U', 'C', 'K') },
  150. { CODEC_ID_MSZH, MKTAG('M', 'S', 'Z', 'H') },
  151. { CODEC_ID_ZLIB, MKTAG('Z', 'L', 'I', 'B') },
  152. { CODEC_ID_SNOW, MKTAG('S', 'N', 'O', 'W') },
  153. { CODEC_ID_4XM, MKTAG('4', 'X', 'M', 'V') },
  154. { CODEC_ID_FLV1, MKTAG('F', 'L', 'V', '1') },
  155. { CODEC_ID_SVQ1, MKTAG('s', 'v', 'q', '1') },
  156. { CODEC_ID_TSCC, MKTAG('t', 's', 'c', 'c') },
  157. { CODEC_ID_ULTI, MKTAG('U', 'L', 'T', 'I') },
  158. { CODEC_ID_VIXL, MKTAG('V', 'I', 'X', 'L') },
  159. { CODEC_ID_QPEG, MKTAG('Q', 'P', 'E', 'G') },
  160. { CODEC_ID_QPEG, MKTAG('Q', '1', '.', '0') },
  161. { CODEC_ID_QPEG, MKTAG('Q', '1', '.', '1') },
  162. { CODEC_ID_WMV3, MKTAG('W', 'M', 'V', '3') },
  163. { CODEC_ID_LOCO, MKTAG('L', 'O', 'C', 'O') },
  164. { CODEC_ID_WNV1, MKTAG('W', 'N', 'V', '1') },
  165. { CODEC_ID_AASC, MKTAG('A', 'A', 'S', 'C') },
  166. { CODEC_ID_INDEO2, MKTAG('R', 'T', '2', '1') },
  167. { CODEC_ID_FRAPS, MKTAG('F', 'P', 'S', '1') },
  168. { CODEC_ID_THEORA, MKTAG('t', 'h', 'e', 'o') },
  169. { CODEC_ID_TRUEMOTION2, MKTAG('T', 'M', '2', '0') },
  170. { CODEC_ID_RAWVIDEO, 0 },
  171. { 0, 0 },
  172. };
  173. unsigned int codec_get_tag(const CodecTag *tags, int id)
  174. {
  175. while (tags->id != 0) {
  176. if (tags->id == id)
  177. return tags->tag;
  178. tags++;
  179. }
  180. return 0;
  181. }
  182. static unsigned int codec_get_asf_tag(const CodecTag *tags, unsigned int id)
  183. {
  184. while (tags->id != 0) {
  185. if (!tags->invalid_asf && tags->id == id)
  186. return tags->tag;
  187. tags++;
  188. }
  189. return 0;
  190. }
  191. enum CodecID codec_get_id(const CodecTag *tags, unsigned int tag)
  192. {
  193. while (tags->id != 0) {
  194. if( toupper((tag >> 0)&0xFF) == toupper((tags->tag >> 0)&0xFF)
  195. && toupper((tag >> 8)&0xFF) == toupper((tags->tag >> 8)&0xFF)
  196. && toupper((tag >>16)&0xFF) == toupper((tags->tag >>16)&0xFF)
  197. && toupper((tag >>24)&0xFF) == toupper((tags->tag >>24)&0xFF))
  198. return tags->id;
  199. tags++;
  200. }
  201. return CODEC_ID_NONE;
  202. }
  203. unsigned int codec_get_bmp_tag(int id)
  204. {
  205. return codec_get_tag(codec_bmp_tags, id);
  206. }
  207. unsigned int codec_get_wav_tag(int id)
  208. {
  209. return codec_get_tag(codec_wav_tags, id);
  210. }
  211. enum CodecID codec_get_bmp_id(unsigned int tag)
  212. {
  213. return codec_get_id(codec_bmp_tags, tag);
  214. }
  215. enum CodecID codec_get_wav_id(unsigned int tag)
  216. {
  217. return codec_get_id(codec_wav_tags, tag);
  218. }
  219. #ifdef CONFIG_MUXERS
  220. /* BITMAPINFOHEADER header */
  221. void put_bmp_header(ByteIOContext *pb, AVCodecContext *enc, const CodecTag *tags, int for_asf)
  222. {
  223. put_le32(pb, 40 + enc->extradata_size); /* size */
  224. put_le32(pb, enc->width);
  225. put_le32(pb, enc->height);
  226. put_le16(pb, 1); /* planes */
  227. put_le16(pb, enc->bits_per_sample ? enc->bits_per_sample : 24); /* depth */
  228. /* compression type */
  229. put_le32(pb, for_asf ? (enc->codec_tag ? enc->codec_tag : codec_get_asf_tag(tags, enc->codec_id)) : enc->codec_tag); //
  230. put_le32(pb, enc->width * enc->height * 3);
  231. put_le32(pb, 0);
  232. put_le32(pb, 0);
  233. put_le32(pb, 0);
  234. put_le32(pb, 0);
  235. put_buffer(pb, enc->extradata, enc->extradata_size);
  236. if (enc->extradata_size & 1)
  237. put_byte(pb, 0);
  238. }
  239. void ff_parse_specific_params(AVCodecContext *stream, int *au_rate, int *au_ssize, int *au_scale)
  240. {
  241. int gcd;
  242. *au_ssize= stream->block_align;
  243. if(stream->frame_size && stream->sample_rate){
  244. *au_scale=stream->frame_size;
  245. *au_rate= stream->sample_rate;
  246. }else if(stream->codec_type == CODEC_TYPE_VIDEO){
  247. *au_scale= stream->time_base.num;
  248. *au_rate = stream->time_base.den;
  249. }else{
  250. *au_scale= stream->block_align ? stream->block_align*8 : 8;
  251. *au_rate = stream->bit_rate;
  252. }
  253. gcd= ff_gcd(*au_scale, *au_rate);
  254. *au_scale /= gcd;
  255. *au_rate /= gcd;
  256. }
  257. static offset_t avi_start_new_riff(AVIContext *avi, ByteIOContext *pb,
  258. const char* riff_tag, const char* list_tag)
  259. {
  260. offset_t loff;
  261. int i;
  262. avi->riff_id++;
  263. for (i=0; i<MAX_STREAMS; i++)
  264. avi->indexes[i].entry = 0;
  265. avi->riff_start = start_tag(pb, "RIFF");
  266. put_tag(pb, riff_tag);
  267. loff = start_tag(pb, "LIST");
  268. put_tag(pb, list_tag);
  269. return loff;
  270. }
  271. static unsigned char* avi_stream2fourcc(unsigned char* tag, int index,
  272. enum CodecType type)
  273. {
  274. tag[0] = '0';
  275. tag[1] = '0' + index;
  276. if (type == CODEC_TYPE_VIDEO) {
  277. tag[2] = 'd';
  278. tag[3] = 'c';
  279. } else {
  280. tag[2] = 'w';
  281. tag[3] = 'b';
  282. }
  283. tag[4] = '\0';
  284. return tag;
  285. }
  286. static int avi_write_header(AVFormatContext *s)
  287. {
  288. AVIContext *avi = s->priv_data;
  289. ByteIOContext *pb = &s->pb;
  290. int bitrate, n, i, nb_frames, au_byterate, au_ssize, au_scale;
  291. AVCodecContext *stream, *video_enc;
  292. offset_t list1, list2, strh, strf;
  293. /* header list */
  294. avi->riff_id = 0;
  295. list1 = avi_start_new_riff(avi, pb, "AVI ", "hdrl");
  296. /* avi header */
  297. put_tag(pb, "avih");
  298. put_le32(pb, 14 * 4);
  299. bitrate = 0;
  300. video_enc = NULL;
  301. for(n=0;n<s->nb_streams;n++) {
  302. stream = s->streams[n]->codec;
  303. bitrate += stream->bit_rate;
  304. if (stream->codec_type == CODEC_TYPE_VIDEO)
  305. video_enc = stream;
  306. }
  307. nb_frames = 0;
  308. if(video_enc){
  309. put_le32(pb, (uint32_t)(int64_t_C(1000000) * video_enc->time_base.num / video_enc->time_base.den));
  310. } else {
  311. put_le32(pb, 0);
  312. }
  313. put_le32(pb, bitrate / 8); /* XXX: not quite exact */
  314. put_le32(pb, 0); /* padding */
  315. if (url_is_streamed(pb))
  316. put_le32(pb, AVIF_TRUSTCKTYPE | AVIF_ISINTERLEAVED); /* flags */
  317. else
  318. put_le32(pb, AVIF_TRUSTCKTYPE | AVIF_HASINDEX | AVIF_ISINTERLEAVED); /* flags */
  319. avi->frames_hdr_all = url_ftell(pb); /* remember this offset to fill later */
  320. put_le32(pb, nb_frames); /* nb frames, filled later */
  321. put_le32(pb, 0); /* initial frame */
  322. put_le32(pb, s->nb_streams); /* nb streams */
  323. put_le32(pb, 1024 * 1024); /* suggested buffer size */
  324. if(video_enc){
  325. put_le32(pb, video_enc->width);
  326. put_le32(pb, video_enc->height);
  327. } else {
  328. put_le32(pb, 0);
  329. put_le32(pb, 0);
  330. }
  331. put_le32(pb, 0); /* reserved */
  332. put_le32(pb, 0); /* reserved */
  333. put_le32(pb, 0); /* reserved */
  334. put_le32(pb, 0); /* reserved */
  335. /* stream list */
  336. for(i=0;i<n;i++) {
  337. list2 = start_tag(pb, "LIST");
  338. put_tag(pb, "strl");
  339. stream = s->streams[i]->codec;
  340. /* FourCC should really be set by the codec itself */
  341. if (! stream->codec_tag) {
  342. stream->codec_tag = codec_get_bmp_tag(stream->codec_id);
  343. }
  344. /* stream generic header */
  345. strh = start_tag(pb, "strh");
  346. switch(stream->codec_type) {
  347. case CODEC_TYPE_VIDEO: put_tag(pb, "vids"); break;
  348. case CODEC_TYPE_AUDIO: put_tag(pb, "auds"); break;
  349. // case CODEC_TYPE_TEXT : put_tag(pb, "txts"); break;
  350. case CODEC_TYPE_DATA : put_tag(pb, "dats"); break;
  351. }
  352. if(stream->codec_type == CODEC_TYPE_VIDEO)
  353. put_le32(pb, stream->codec_tag);
  354. else
  355. put_le32(pb, 1);
  356. put_le32(pb, 0); /* flags */
  357. put_le16(pb, 0); /* priority */
  358. put_le16(pb, 0); /* language */
  359. put_le32(pb, 0); /* initial frame */
  360. ff_parse_specific_params(stream, &au_byterate, &au_ssize, &au_scale);
  361. put_le32(pb, au_scale); /* scale */
  362. put_le32(pb, au_byterate); /* rate */
  363. av_set_pts_info(s->streams[i], 64, au_scale, au_byterate);
  364. put_le32(pb, 0); /* start */
  365. avi->frames_hdr_strm[i] = url_ftell(pb); /* remember this offset to fill later */
  366. if (url_is_streamed(pb))
  367. put_le32(pb, AVI_MAX_RIFF_SIZE); /* FIXME: this may be broken, but who cares */
  368. else
  369. put_le32(pb, 0); /* length, XXX: filled later */
  370. /* suggested buffer size */ //FIXME set at the end to largest chunk
  371. if(stream->codec_type == CODEC_TYPE_VIDEO)
  372. put_le32(pb, 1024 * 1024);
  373. else if(stream->codec_type == CODEC_TYPE_AUDIO)
  374. put_le32(pb, 12 * 1024);
  375. else
  376. put_le32(pb, 0);
  377. put_le32(pb, -1); /* quality */
  378. put_le32(pb, au_ssize); /* sample size */
  379. put_le32(pb, 0);
  380. put_le16(pb, stream->width);
  381. put_le16(pb, stream->height);
  382. end_tag(pb, strh);
  383. if(stream->codec_type != CODEC_TYPE_DATA){
  384. strf = start_tag(pb, "strf");
  385. switch(stream->codec_type) {
  386. case CODEC_TYPE_VIDEO:
  387. put_bmp_header(pb, stream, codec_bmp_tags, 0);
  388. break;
  389. case CODEC_TYPE_AUDIO:
  390. if (put_wav_header(pb, stream) < 0) {
  391. av_free(avi);
  392. return -1;
  393. }
  394. break;
  395. default:
  396. return -1;
  397. }
  398. end_tag(pb, strf);
  399. }
  400. if (!url_is_streamed(pb)) {
  401. unsigned char tag[5];
  402. int j;
  403. /* Starting to lay out AVI OpenDML master index.
  404. * We want to make it JUNK entry for now, since we'd
  405. * like to get away without making AVI an OpenDML one
  406. * for compatibility reasons.
  407. */
  408. avi->indexes[i].entry = avi->indexes[i].ents_allocated = 0;
  409. avi->indexes[i].indx_start = start_tag(pb, "JUNK");
  410. put_le16(pb, 4); /* wLongsPerEntry */
  411. put_byte(pb, 0); /* bIndexSubType (0 == frame index) */
  412. put_byte(pb, 0); /* bIndexType (0 == AVI_INDEX_OF_INDEXES) */
  413. put_le32(pb, 0); /* nEntriesInUse (will fill out later on) */
  414. put_tag(pb, avi_stream2fourcc(&tag[0], i, stream->codec_type));
  415. /* dwChunkId */
  416. put_le64(pb, 0); /* dwReserved[3]
  417. put_le32(pb, 0); Must be 0. */
  418. for (j=0; j < AVI_MASTER_INDEX_SIZE * 2; j++)
  419. put_le64(pb, 0);
  420. end_tag(pb, avi->indexes[i].indx_start);
  421. }
  422. end_tag(pb, list2);
  423. }
  424. if (!url_is_streamed(pb)) {
  425. /* AVI could become an OpenDML one, if it grows beyond 2Gb range */
  426. avi->odml_list = start_tag(pb, "JUNK");
  427. put_tag(pb, "odml");
  428. put_tag(pb, "dmlh");
  429. put_le32(pb, 248);
  430. for (i = 0; i < 248; i+= 4)
  431. put_le32(pb, 0);
  432. end_tag(pb, avi->odml_list);
  433. }
  434. end_tag(pb, list1);
  435. avi->movi_list = start_tag(pb, "LIST");
  436. put_tag(pb, "movi");
  437. put_flush_packet(pb);
  438. return 0;
  439. }
  440. static int avi_write_ix(AVFormatContext *s)
  441. {
  442. ByteIOContext *pb = &s->pb;
  443. AVIContext *avi = s->priv_data;
  444. unsigned char tag[5];
  445. unsigned char ix_tag[] = "ix00";
  446. int i, j;
  447. if (url_is_streamed(pb))
  448. return -1;
  449. if (avi->riff_id > AVI_MASTER_INDEX_SIZE)
  450. return -1;
  451. for (i=0;i<s->nb_streams;i++) {
  452. offset_t ix, pos;
  453. avi_stream2fourcc(&tag[0], i, s->streams[i]->codec->codec_type);
  454. ix_tag[3] = '0' + i;
  455. /* Writing AVI OpenDML leaf index chunk */
  456. ix = url_ftell(pb);
  457. put_tag(pb, &ix_tag[0]); /* ix?? */
  458. put_le32(pb, avi->indexes[i].entry * 8 + 24);
  459. /* chunk size */
  460. put_le16(pb, 2); /* wLongsPerEntry */
  461. put_byte(pb, 0); /* bIndexSubType (0 == frame index) */
  462. put_byte(pb, 1); /* bIndexType (1 == AVI_INDEX_OF_CHUNKS) */
  463. put_le32(pb, avi->indexes[i].entry);
  464. /* nEntriesInUse */
  465. put_tag(pb, &tag[0]); /* dwChunkId */
  466. put_le64(pb, avi->movi_list);/* qwBaseOffset */
  467. put_le32(pb, 0); /* dwReserved_3 (must be 0) */
  468. for (j=0; j<avi->indexes[i].entry; j++) {
  469. AVIIentry* ie = avi_get_ientry(&avi->indexes[i], j);
  470. put_le32(pb, ie->pos + 8);
  471. put_le32(pb, ((uint32_t)ie->len & ~0x80000000) |
  472. (ie->flags & 0x10 ? 0 : 0x80000000));
  473. }
  474. put_flush_packet(pb);
  475. pos = url_ftell(pb);
  476. /* Updating one entry in the AVI OpenDML master index */
  477. url_fseek(pb, avi->indexes[i].indx_start - 8, SEEK_SET);
  478. put_tag(pb, "indx"); /* enabling this entry */
  479. url_fskip(pb, 8);
  480. put_le32(pb, avi->riff_id); /* nEntriesInUse */
  481. url_fskip(pb, 16*avi->riff_id);
  482. put_le64(pb, ix); /* qwOffset */
  483. put_le32(pb, pos - ix); /* dwSize */
  484. put_le32(pb, avi->indexes[i].entry); /* dwDuration */
  485. url_fseek(pb, pos, SEEK_SET);
  486. }
  487. return 0;
  488. }
  489. static int avi_write_idx1(AVFormatContext *s)
  490. {
  491. ByteIOContext *pb = &s->pb;
  492. AVIContext *avi = s->priv_data;
  493. offset_t file_size, idx_chunk;
  494. int i, n, nb_frames, au_byterate, au_ssize, au_scale;
  495. AVCodecContext *stream;
  496. unsigned char tag[5];
  497. if (!url_is_streamed(pb)) {
  498. AVIIentry* ie = 0, *tie;
  499. int entry[MAX_STREAMS];
  500. int empty, stream_id = -1;
  501. idx_chunk = start_tag(pb, "idx1");
  502. memset(&entry[0], 0, sizeof(entry));
  503. do {
  504. empty = 1;
  505. for (i=0; i<s->nb_streams; i++) {
  506. if (avi->indexes[i].entry <= entry[i])
  507. continue;
  508. tie = avi_get_ientry(&avi->indexes[i], entry[i]);
  509. if (empty || tie->pos < ie->pos) {
  510. ie = tie;
  511. stream_id = i;
  512. }
  513. empty = 0;
  514. }
  515. if (!empty) {
  516. avi_stream2fourcc(&tag[0], stream_id,
  517. s->streams[stream_id]->codec->codec_type);
  518. put_tag(pb, &tag[0]);
  519. put_le32(pb, ie->flags);
  520. put_le32(pb, ie->pos);
  521. put_le32(pb, ie->len);
  522. entry[stream_id]++;
  523. }
  524. } while (!empty);
  525. end_tag(pb, idx_chunk);
  526. /* Fill in frame/sample counters */
  527. file_size = url_ftell(pb);
  528. nb_frames = 0;
  529. for(n=0;n<s->nb_streams;n++) {
  530. if (avi->frames_hdr_strm[n] != 0) {
  531. stream = s->streams[n]->codec;
  532. url_fseek(pb, avi->frames_hdr_strm[n], SEEK_SET);
  533. ff_parse_specific_params(stream, &au_byterate, &au_ssize, &au_scale);
  534. if (au_ssize == 0) {
  535. put_le32(pb, stream->frame_number);
  536. nb_frames += stream->frame_number;
  537. } else {
  538. put_le32(pb, avi->audio_strm_length[n] / au_ssize);
  539. }
  540. }
  541. }
  542. if (avi->frames_hdr_all != 0) {
  543. url_fseek(pb, avi->frames_hdr_all, SEEK_SET);
  544. put_le32(pb, nb_frames);
  545. }
  546. url_fseek(pb, file_size, SEEK_SET);
  547. }
  548. return 0;
  549. }
  550. static int avi_write_packet(AVFormatContext *s, AVPacket *pkt)
  551. {
  552. AVIContext *avi = s->priv_data;
  553. ByteIOContext *pb = &s->pb;
  554. unsigned char tag[5];
  555. unsigned int flags=0;
  556. const int stream_index= pkt->stream_index;
  557. AVCodecContext *enc= s->streams[stream_index]->codec;
  558. int size= pkt->size;
  559. // av_log(s, AV_LOG_DEBUG, "%lld %d %d\n", pkt->dts, avi->packet_count[stream_index], stream_index);
  560. while(enc->block_align==0 && pkt->dts != AV_NOPTS_VALUE && pkt->dts > avi->packet_count[stream_index]){
  561. AVPacket empty_packet;
  562. av_init_packet(&empty_packet);
  563. empty_packet.size= 0;
  564. empty_packet.data= NULL;
  565. empty_packet.stream_index= stream_index;
  566. avi_write_packet(s, &empty_packet);
  567. // av_log(s, AV_LOG_DEBUG, "dup %lld %d\n", pkt->dts, avi->packet_count[stream_index]);
  568. }
  569. avi->packet_count[stream_index]++;
  570. // Make sure to put an OpenDML chunk when the file size exceeds the limits
  571. if (!url_is_streamed(pb) &&
  572. (url_ftell(pb) - avi->riff_start > AVI_MAX_RIFF_SIZE)) {
  573. avi_write_ix(s);
  574. end_tag(pb, avi->movi_list);
  575. if (avi->riff_id == 1)
  576. avi_write_idx1(s);
  577. end_tag(pb, avi->riff_start);
  578. avi->movi_list = avi_start_new_riff(avi, pb, "AVIX", "movi");
  579. }
  580. avi_stream2fourcc(&tag[0], stream_index, enc->codec_type);
  581. if(pkt->flags&PKT_FLAG_KEY)
  582. flags = 0x10;
  583. if (enc->codec_type == CODEC_TYPE_AUDIO) {
  584. avi->audio_strm_length[stream_index] += size;
  585. }
  586. if (!url_is_streamed(&s->pb)) {
  587. AVIIndex* idx = &avi->indexes[stream_index];
  588. int cl = idx->entry / AVI_INDEX_CLUSTER_SIZE;
  589. int id = idx->entry % AVI_INDEX_CLUSTER_SIZE;
  590. if (idx->ents_allocated <= idx->entry) {
  591. idx->cluster = av_realloc(idx->cluster, (cl+1)*sizeof(void*));
  592. if (!idx->cluster)
  593. return -1;
  594. idx->cluster[cl] = av_malloc(AVI_INDEX_CLUSTER_SIZE*sizeof(AVIIentry));
  595. if (!idx->cluster[cl])
  596. return -1;
  597. idx->ents_allocated += AVI_INDEX_CLUSTER_SIZE;
  598. }
  599. idx->cluster[cl][id].flags = flags;
  600. idx->cluster[cl][id].pos = url_ftell(pb) - avi->movi_list;
  601. idx->cluster[cl][id].len = size;
  602. idx->entry++;
  603. }
  604. put_buffer(pb, tag, 4);
  605. put_le32(pb, size);
  606. put_buffer(pb, pkt->data, size);
  607. if (size & 1)
  608. put_byte(pb, 0);
  609. put_flush_packet(pb);
  610. return 0;
  611. }
  612. static int avi_write_trailer(AVFormatContext *s)
  613. {
  614. AVIContext *avi = s->priv_data;
  615. ByteIOContext *pb = &s->pb;
  616. int res = 0;
  617. int i, j, n, nb_frames;
  618. offset_t file_size;
  619. if (!url_is_streamed(pb))
  620. {
  621. if (avi->riff_id == 1) {
  622. end_tag(pb, avi->movi_list);
  623. res = avi_write_idx1(s);
  624. end_tag(pb, avi->riff_start);
  625. } else {
  626. avi_write_ix(s);
  627. end_tag(pb, avi->movi_list);
  628. end_tag(pb, avi->riff_start);
  629. file_size = url_ftell(pb);
  630. url_fseek(pb, avi->odml_list - 8, SEEK_SET);
  631. put_tag(pb, "LIST"); /* Making this AVI OpenDML one */
  632. url_fskip(pb, 16);
  633. for (n=nb_frames=0;n<s->nb_streams;n++) {
  634. AVCodecContext *stream = s->streams[n]->codec;
  635. if (stream->codec_type == CODEC_TYPE_VIDEO) {
  636. if (nb_frames < stream->frame_number)
  637. nb_frames = stream->frame_number;
  638. } else {
  639. if (stream->codec_id == CODEC_ID_MP2 || stream->codec_id == CODEC_ID_MP3) {
  640. nb_frames += stream->frame_number;
  641. }
  642. }
  643. }
  644. put_le32(pb, nb_frames);
  645. url_fseek(pb, file_size, SEEK_SET);
  646. }
  647. }
  648. put_flush_packet(pb);
  649. for (i=0; i<MAX_STREAMS; i++) {
  650. for (j=0; j<avi->indexes[i].ents_allocated/AVI_INDEX_CLUSTER_SIZE; j++)
  651. av_free(avi->indexes[i].cluster[j]);
  652. av_free(avi->indexes[i].cluster);
  653. avi->indexes[i].cluster = NULL;
  654. avi->indexes[i].ents_allocated = avi->indexes[i].entry = 0;
  655. }
  656. return res;
  657. }
  658. static AVOutputFormat avi_oformat = {
  659. "avi",
  660. "avi format",
  661. "video/x-msvideo",
  662. "avi",
  663. sizeof(AVIContext),
  664. CODEC_ID_MP2,
  665. CODEC_ID_MPEG4,
  666. avi_write_header,
  667. avi_write_packet,
  668. avi_write_trailer,
  669. };
  670. int avienc_init(void)
  671. {
  672. av_register_output_format(&avi_oformat);
  673. return 0;
  674. }
  675. #endif //CONFIG_MUXERS