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
23KB

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