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.

717 lines
22KB

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