avienc.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  1. /*
  2. * AVI muxer
  3. * Copyright (c) 2000 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 "avi.h"
  23. #include "riff.h"
  24. #include "libavutil/intreadwrite.h"
  25. /*
  26. * TODO:
  27. * - fill all fields if non streamed (nb_frames for example)
  28. */
  29. typedef struct AVIIentry {
  30. unsigned int flags, pos, len;
  31. } AVIIentry;
  32. #define AVI_INDEX_CLUSTER_SIZE 16384
  33. typedef struct AVIIndex {
  34. int64_t indx_start;
  35. int entry;
  36. int ents_allocated;
  37. AVIIentry** cluster;
  38. } AVIIndex;
  39. typedef struct {
  40. int64_t riff_start, movi_list, odml_list;
  41. int64_t frames_hdr_all;
  42. int riff_id;
  43. } AVIContext;
  44. typedef struct {
  45. int64_t frames_hdr_strm;
  46. int audio_strm_length;
  47. int packet_count;
  48. int entry;
  49. AVIIndex indexes;
  50. } AVIStream ;
  51. static inline AVIIentry* avi_get_ientry(AVIIndex* idx, int ent_id)
  52. {
  53. int cl = ent_id / AVI_INDEX_CLUSTER_SIZE;
  54. int id = ent_id % AVI_INDEX_CLUSTER_SIZE;
  55. return &idx->cluster[cl][id];
  56. }
  57. static int64_t avi_start_new_riff(AVFormatContext *s, ByteIOContext *pb,
  58. const char* riff_tag, const char* list_tag)
  59. {
  60. AVIContext *avi= s->priv_data;
  61. int64_t loff;
  62. int i;
  63. avi->riff_id++;
  64. for (i=0; i<s->nb_streams; i++){
  65. AVIStream *avist= s->streams[i]->priv_data;
  66. avist->indexes.entry = 0;
  67. }
  68. avi->riff_start = ff_start_tag(pb, "RIFF");
  69. put_tag(pb, riff_tag);
  70. loff = ff_start_tag(pb, "LIST");
  71. put_tag(pb, list_tag);
  72. return loff;
  73. }
  74. static char* avi_stream2fourcc(char* tag, int index, enum AVMediaType type)
  75. {
  76. tag[0] = '0';
  77. tag[1] = '0' + index;
  78. if (type == AVMEDIA_TYPE_VIDEO) {
  79. tag[2] = 'd';
  80. tag[3] = 'c';
  81. } else if (type == AVMEDIA_TYPE_SUBTITLE) {
  82. // note: this is not an official code
  83. tag[2] = 's';
  84. tag[3] = 'b';
  85. } else {
  86. tag[2] = 'w';
  87. tag[3] = 'b';
  88. }
  89. tag[4] = '\0';
  90. return tag;
  91. }
  92. static void avi_write_info_tag(ByteIOContext *pb, const char *tag, const char *str)
  93. {
  94. int len = strlen(str);
  95. if (len > 0) {
  96. len++;
  97. put_tag(pb, tag);
  98. put_le32(pb, len);
  99. avio_put_str(pb, str);
  100. if (len & 1)
  101. put_byte(pb, 0);
  102. }
  103. }
  104. static int avi_write_counters(AVFormatContext* s, int riff_id)
  105. {
  106. ByteIOContext *pb = s->pb;
  107. AVIContext *avi = s->priv_data;
  108. int n, au_byterate, au_ssize, au_scale, nb_frames = 0;
  109. int64_t file_size;
  110. AVCodecContext* stream;
  111. file_size = url_ftell(pb);
  112. for(n = 0; n < s->nb_streams; n++) {
  113. AVIStream *avist= s->streams[n]->priv_data;
  114. assert(avist->frames_hdr_strm);
  115. stream = s->streams[n]->codec;
  116. url_fseek(pb, avist->frames_hdr_strm, SEEK_SET);
  117. ff_parse_specific_params(stream, &au_byterate, &au_ssize, &au_scale);
  118. if(au_ssize == 0) {
  119. put_le32(pb, avist->packet_count);
  120. } else {
  121. put_le32(pb, avist->audio_strm_length / au_ssize);
  122. }
  123. if(stream->codec_type == AVMEDIA_TYPE_VIDEO)
  124. nb_frames = FFMAX(nb_frames, avist->packet_count);
  125. }
  126. if(riff_id == 1) {
  127. assert(avi->frames_hdr_all);
  128. url_fseek(pb, avi->frames_hdr_all, SEEK_SET);
  129. put_le32(pb, nb_frames);
  130. }
  131. url_fseek(pb, file_size, SEEK_SET);
  132. return 0;
  133. }
  134. static int avi_write_header(AVFormatContext *s)
  135. {
  136. AVIContext *avi = s->priv_data;
  137. ByteIOContext *pb = s->pb;
  138. int bitrate, n, i, nb_frames, au_byterate, au_ssize, au_scale;
  139. AVCodecContext *stream, *video_enc;
  140. int64_t list1, list2, strh, strf;
  141. AVMetadataTag *t = NULL;
  142. for(n=0;n<s->nb_streams;n++) {
  143. s->streams[n]->priv_data= av_mallocz(sizeof(AVIStream));
  144. if(!s->streams[n]->priv_data)
  145. return AVERROR(ENOMEM);
  146. }
  147. /* header list */
  148. avi->riff_id = 0;
  149. list1 = avi_start_new_riff(s, pb, "AVI ", "hdrl");
  150. /* avi header */
  151. put_tag(pb, "avih");
  152. put_le32(pb, 14 * 4);
  153. bitrate = 0;
  154. video_enc = NULL;
  155. for(n=0;n<s->nb_streams;n++) {
  156. stream = s->streams[n]->codec;
  157. bitrate += stream->bit_rate;
  158. if (stream->codec_type == AVMEDIA_TYPE_VIDEO)
  159. video_enc = stream;
  160. }
  161. nb_frames = 0;
  162. if(video_enc){
  163. put_le32(pb, (uint32_t)(INT64_C(1000000) * video_enc->time_base.num / video_enc->time_base.den));
  164. } else {
  165. put_le32(pb, 0);
  166. }
  167. put_le32(pb, bitrate / 8); /* XXX: not quite exact */
  168. put_le32(pb, 0); /* padding */
  169. if (url_is_streamed(pb))
  170. put_le32(pb, AVIF_TRUSTCKTYPE | AVIF_ISINTERLEAVED); /* flags */
  171. else
  172. put_le32(pb, AVIF_TRUSTCKTYPE | AVIF_HASINDEX | AVIF_ISINTERLEAVED); /* flags */
  173. avi->frames_hdr_all = url_ftell(pb); /* remember this offset to fill later */
  174. put_le32(pb, nb_frames); /* nb frames, filled later */
  175. put_le32(pb, 0); /* initial frame */
  176. put_le32(pb, s->nb_streams); /* nb streams */
  177. put_le32(pb, 1024 * 1024); /* suggested buffer size */
  178. if(video_enc){
  179. put_le32(pb, video_enc->width);
  180. put_le32(pb, video_enc->height);
  181. } else {
  182. put_le32(pb, 0);
  183. put_le32(pb, 0);
  184. }
  185. put_le32(pb, 0); /* reserved */
  186. put_le32(pb, 0); /* reserved */
  187. put_le32(pb, 0); /* reserved */
  188. put_le32(pb, 0); /* reserved */
  189. /* stream list */
  190. for(i=0;i<n;i++) {
  191. AVIStream *avist= s->streams[i]->priv_data;
  192. list2 = ff_start_tag(pb, "LIST");
  193. put_tag(pb, "strl");
  194. stream = s->streams[i]->codec;
  195. /* stream generic header */
  196. strh = ff_start_tag(pb, "strh");
  197. switch(stream->codec_type) {
  198. case AVMEDIA_TYPE_SUBTITLE:
  199. // XSUB subtitles behave like video tracks, other subtitles
  200. // are not (yet) supported.
  201. if (stream->codec_id != CODEC_ID_XSUB) {
  202. av_log(s, AV_LOG_ERROR, "Subtitle streams other than DivX XSUB are not supported by the AVI muxer.\n");
  203. return AVERROR_PATCHWELCOME;
  204. }
  205. case AVMEDIA_TYPE_VIDEO: put_tag(pb, "vids"); break;
  206. case AVMEDIA_TYPE_AUDIO: put_tag(pb, "auds"); break;
  207. // case AVMEDIA_TYPE_TEXT : put_tag(pb, "txts"); break;
  208. case AVMEDIA_TYPE_DATA : put_tag(pb, "dats"); break;
  209. }
  210. if(stream->codec_type == AVMEDIA_TYPE_VIDEO ||
  211. stream->codec_id == CODEC_ID_XSUB)
  212. put_le32(pb, stream->codec_tag);
  213. else
  214. put_le32(pb, 1);
  215. put_le32(pb, 0); /* flags */
  216. put_le16(pb, 0); /* priority */
  217. put_le16(pb, 0); /* language */
  218. put_le32(pb, 0); /* initial frame */
  219. ff_parse_specific_params(stream, &au_byterate, &au_ssize, &au_scale);
  220. put_le32(pb, au_scale); /* scale */
  221. put_le32(pb, au_byterate); /* rate */
  222. av_set_pts_info(s->streams[i], 64, au_scale, au_byterate);
  223. put_le32(pb, 0); /* start */
  224. avist->frames_hdr_strm = url_ftell(pb); /* remember this offset to fill later */
  225. if (url_is_streamed(pb))
  226. put_le32(pb, AVI_MAX_RIFF_SIZE); /* FIXME: this may be broken, but who cares */
  227. else
  228. put_le32(pb, 0); /* length, XXX: filled later */
  229. /* suggested buffer size */ //FIXME set at the end to largest chunk
  230. if(stream->codec_type == AVMEDIA_TYPE_VIDEO)
  231. put_le32(pb, 1024 * 1024);
  232. else if(stream->codec_type == AVMEDIA_TYPE_AUDIO)
  233. put_le32(pb, 12 * 1024);
  234. else
  235. put_le32(pb, 0);
  236. put_le32(pb, -1); /* quality */
  237. put_le32(pb, au_ssize); /* sample size */
  238. put_le32(pb, 0);
  239. put_le16(pb, stream->width);
  240. put_le16(pb, stream->height);
  241. ff_end_tag(pb, strh);
  242. if(stream->codec_type != AVMEDIA_TYPE_DATA){
  243. strf = ff_start_tag(pb, "strf");
  244. switch(stream->codec_type) {
  245. case AVMEDIA_TYPE_SUBTITLE:
  246. // XSUB subtitles behave like video tracks, other subtitles
  247. // are not (yet) supported.
  248. if (stream->codec_id != CODEC_ID_XSUB) break;
  249. case AVMEDIA_TYPE_VIDEO:
  250. ff_put_bmp_header(pb, stream, ff_codec_bmp_tags, 0);
  251. break;
  252. case AVMEDIA_TYPE_AUDIO:
  253. if (ff_put_wav_header(pb, stream) < 0) {
  254. return -1;
  255. }
  256. break;
  257. default:
  258. return -1;
  259. }
  260. ff_end_tag(pb, strf);
  261. if ((t = av_metadata_get(s->streams[i]->metadata, "title", NULL, 0))) {
  262. avi_write_info_tag(s->pb, "strn", t->value);
  263. t = NULL;
  264. }
  265. }
  266. if (!url_is_streamed(pb)) {
  267. unsigned char tag[5];
  268. int j;
  269. /* Starting to lay out AVI OpenDML master index.
  270. * We want to make it JUNK entry for now, since we'd
  271. * like to get away without making AVI an OpenDML one
  272. * for compatibility reasons.
  273. */
  274. avist->indexes.entry = avist->indexes.ents_allocated = 0;
  275. avist->indexes.indx_start = ff_start_tag(pb, "JUNK");
  276. put_le16(pb, 4); /* wLongsPerEntry */
  277. put_byte(pb, 0); /* bIndexSubType (0 == frame index) */
  278. put_byte(pb, 0); /* bIndexType (0 == AVI_INDEX_OF_INDEXES) */
  279. put_le32(pb, 0); /* nEntriesInUse (will fill out later on) */
  280. put_tag(pb, avi_stream2fourcc(&tag[0], i, stream->codec_type));
  281. /* dwChunkId */
  282. put_le64(pb, 0); /* dwReserved[3]
  283. put_le32(pb, 0); Must be 0. */
  284. for (j=0; j < AVI_MASTER_INDEX_SIZE * 2; j++)
  285. put_le64(pb, 0);
  286. ff_end_tag(pb, avist->indexes.indx_start);
  287. }
  288. if( stream->codec_type == AVMEDIA_TYPE_VIDEO
  289. && s->streams[i]->sample_aspect_ratio.num>0
  290. && s->streams[i]->sample_aspect_ratio.den>0){
  291. int vprp= ff_start_tag(pb, "vprp");
  292. AVRational dar = av_mul_q(s->streams[i]->sample_aspect_ratio,
  293. (AVRational){stream->width, stream->height});
  294. int num, den;
  295. av_reduce(&num, &den, dar.num, dar.den, 0xFFFF);
  296. put_le32(pb, 0); //video format = unknown
  297. put_le32(pb, 0); //video standard= unknown
  298. put_le32(pb, lrintf(1.0/av_q2d(stream->time_base)));
  299. put_le32(pb, stream->width );
  300. put_le32(pb, stream->height);
  301. put_le16(pb, den);
  302. put_le16(pb, num);
  303. put_le32(pb, stream->width );
  304. put_le32(pb, stream->height);
  305. put_le32(pb, 1); //progressive FIXME
  306. put_le32(pb, stream->height);
  307. put_le32(pb, stream->width );
  308. put_le32(pb, stream->height);
  309. put_le32(pb, stream->width );
  310. put_le32(pb, 0);
  311. put_le32(pb, 0);
  312. put_le32(pb, 0);
  313. put_le32(pb, 0);
  314. ff_end_tag(pb, vprp);
  315. }
  316. ff_end_tag(pb, list2);
  317. }
  318. if (!url_is_streamed(pb)) {
  319. /* AVI could become an OpenDML one, if it grows beyond 2Gb range */
  320. avi->odml_list = ff_start_tag(pb, "JUNK");
  321. put_tag(pb, "odml");
  322. put_tag(pb, "dmlh");
  323. put_le32(pb, 248);
  324. for (i = 0; i < 248; i+= 4)
  325. put_le32(pb, 0);
  326. ff_end_tag(pb, avi->odml_list);
  327. }
  328. ff_end_tag(pb, list1);
  329. list2 = ff_start_tag(pb, "LIST");
  330. put_tag(pb, "INFO");
  331. ff_metadata_conv(&s->metadata, ff_avi_metadata_conv, NULL);
  332. for (i = 0; *ff_avi_tags[i]; i++) {
  333. if ((t = av_metadata_get(s->metadata, ff_avi_tags[i], NULL, AV_METADATA_MATCH_CASE)))
  334. avi_write_info_tag(s->pb, t->key, t->value);
  335. }
  336. ff_end_tag(pb, list2);
  337. /* some padding for easier tag editing */
  338. list2 = ff_start_tag(pb, "JUNK");
  339. for (i = 0; i < 1016; i += 4)
  340. put_le32(pb, 0);
  341. ff_end_tag(pb, list2);
  342. avi->movi_list = ff_start_tag(pb, "LIST");
  343. put_tag(pb, "movi");
  344. put_flush_packet(pb);
  345. return 0;
  346. }
  347. static int avi_write_ix(AVFormatContext *s)
  348. {
  349. ByteIOContext *pb = s->pb;
  350. AVIContext *avi = s->priv_data;
  351. char tag[5];
  352. char ix_tag[] = "ix00";
  353. int i, j;
  354. assert(!url_is_streamed(pb));
  355. if (avi->riff_id > AVI_MASTER_INDEX_SIZE)
  356. return -1;
  357. for (i=0;i<s->nb_streams;i++) {
  358. AVIStream *avist= s->streams[i]->priv_data;
  359. int64_t ix, pos;
  360. avi_stream2fourcc(&tag[0], i, s->streams[i]->codec->codec_type);
  361. ix_tag[3] = '0' + i;
  362. /* Writing AVI OpenDML leaf index chunk */
  363. ix = url_ftell(pb);
  364. put_tag(pb, &ix_tag[0]); /* ix?? */
  365. put_le32(pb, avist->indexes.entry * 8 + 24);
  366. /* chunk size */
  367. put_le16(pb, 2); /* wLongsPerEntry */
  368. put_byte(pb, 0); /* bIndexSubType (0 == frame index) */
  369. put_byte(pb, 1); /* bIndexType (1 == AVI_INDEX_OF_CHUNKS) */
  370. put_le32(pb, avist->indexes.entry);
  371. /* nEntriesInUse */
  372. put_tag(pb, &tag[0]); /* dwChunkId */
  373. put_le64(pb, avi->movi_list);/* qwBaseOffset */
  374. put_le32(pb, 0); /* dwReserved_3 (must be 0) */
  375. for (j=0; j<avist->indexes.entry; j++) {
  376. AVIIentry* ie = avi_get_ientry(&avist->indexes, j);
  377. put_le32(pb, ie->pos + 8);
  378. put_le32(pb, ((uint32_t)ie->len & ~0x80000000) |
  379. (ie->flags & 0x10 ? 0 : 0x80000000));
  380. }
  381. put_flush_packet(pb);
  382. pos = url_ftell(pb);
  383. /* Updating one entry in the AVI OpenDML master index */
  384. url_fseek(pb, avist->indexes.indx_start - 8, SEEK_SET);
  385. put_tag(pb, "indx"); /* enabling this entry */
  386. url_fskip(pb, 8);
  387. put_le32(pb, avi->riff_id); /* nEntriesInUse */
  388. url_fskip(pb, 16*avi->riff_id);
  389. put_le64(pb, ix); /* qwOffset */
  390. put_le32(pb, pos - ix); /* dwSize */
  391. put_le32(pb, avist->indexes.entry); /* dwDuration */
  392. url_fseek(pb, pos, SEEK_SET);
  393. }
  394. return 0;
  395. }
  396. static int avi_write_idx1(AVFormatContext *s)
  397. {
  398. ByteIOContext *pb = s->pb;
  399. AVIContext *avi = s->priv_data;
  400. int64_t idx_chunk;
  401. int i;
  402. char tag[5];
  403. if (!url_is_streamed(pb)) {
  404. AVIStream *avist;
  405. AVIIentry* ie = 0, *tie;
  406. int empty, stream_id = -1;
  407. idx_chunk = ff_start_tag(pb, "idx1");
  408. for(i=0; i<s->nb_streams; i++){
  409. avist= s->streams[i]->priv_data;
  410. avist->entry=0;
  411. }
  412. do {
  413. empty = 1;
  414. for (i=0; i<s->nb_streams; i++) {
  415. avist= s->streams[i]->priv_data;
  416. if (avist->indexes.entry <= avist->entry)
  417. continue;
  418. tie = avi_get_ientry(&avist->indexes, avist->entry);
  419. if (empty || tie->pos < ie->pos) {
  420. ie = tie;
  421. stream_id = i;
  422. }
  423. empty = 0;
  424. }
  425. if (!empty) {
  426. avist= s->streams[stream_id]->priv_data;
  427. avi_stream2fourcc(&tag[0], stream_id,
  428. s->streams[stream_id]->codec->codec_type);
  429. put_tag(pb, &tag[0]);
  430. put_le32(pb, ie->flags);
  431. put_le32(pb, ie->pos);
  432. put_le32(pb, ie->len);
  433. avist->entry++;
  434. }
  435. } while (!empty);
  436. ff_end_tag(pb, idx_chunk);
  437. avi_write_counters(s, avi->riff_id);
  438. }
  439. return 0;
  440. }
  441. static int avi_write_packet(AVFormatContext *s, AVPacket *pkt)
  442. {
  443. AVIContext *avi = s->priv_data;
  444. ByteIOContext *pb = s->pb;
  445. unsigned char tag[5];
  446. unsigned int flags=0;
  447. const int stream_index= pkt->stream_index;
  448. AVIStream *avist= s->streams[stream_index]->priv_data;
  449. AVCodecContext *enc= s->streams[stream_index]->codec;
  450. int size= pkt->size;
  451. // av_log(s, AV_LOG_DEBUG, "%"PRId64" %d %d\n", pkt->dts, avi->packet_count[stream_index], stream_index);
  452. while(enc->block_align==0 && pkt->dts != AV_NOPTS_VALUE && pkt->dts > avist->packet_count){
  453. AVPacket empty_packet;
  454. av_init_packet(&empty_packet);
  455. empty_packet.size= 0;
  456. empty_packet.data= NULL;
  457. empty_packet.stream_index= stream_index;
  458. avi_write_packet(s, &empty_packet);
  459. // av_log(s, AV_LOG_DEBUG, "dup %"PRId64" %d\n", pkt->dts, avi->packet_count[stream_index]);
  460. }
  461. avist->packet_count++;
  462. // Make sure to put an OpenDML chunk when the file size exceeds the limits
  463. if (!url_is_streamed(pb) &&
  464. (url_ftell(pb) - avi->riff_start > AVI_MAX_RIFF_SIZE)) {
  465. avi_write_ix(s);
  466. ff_end_tag(pb, avi->movi_list);
  467. if (avi->riff_id == 1)
  468. avi_write_idx1(s);
  469. ff_end_tag(pb, avi->riff_start);
  470. avi->movi_list = avi_start_new_riff(s, pb, "AVIX", "movi");
  471. }
  472. avi_stream2fourcc(&tag[0], stream_index, enc->codec_type);
  473. if(pkt->flags&AV_PKT_FLAG_KEY)
  474. flags = 0x10;
  475. if (enc->codec_type == AVMEDIA_TYPE_AUDIO) {
  476. avist->audio_strm_length += size;
  477. }
  478. if (!url_is_streamed(s->pb)) {
  479. AVIIndex* idx = &avist->indexes;
  480. int cl = idx->entry / AVI_INDEX_CLUSTER_SIZE;
  481. int id = idx->entry % AVI_INDEX_CLUSTER_SIZE;
  482. if (idx->ents_allocated <= idx->entry) {
  483. idx->cluster = av_realloc(idx->cluster, (cl+1)*sizeof(void*));
  484. if (!idx->cluster)
  485. return -1;
  486. idx->cluster[cl] = av_malloc(AVI_INDEX_CLUSTER_SIZE*sizeof(AVIIentry));
  487. if (!idx->cluster[cl])
  488. return -1;
  489. idx->ents_allocated += AVI_INDEX_CLUSTER_SIZE;
  490. }
  491. idx->cluster[cl][id].flags = flags;
  492. idx->cluster[cl][id].pos = url_ftell(pb) - avi->movi_list;
  493. idx->cluster[cl][id].len = size;
  494. idx->entry++;
  495. }
  496. put_buffer(pb, tag, 4);
  497. put_le32(pb, size);
  498. put_buffer(pb, pkt->data, size);
  499. if (size & 1)
  500. put_byte(pb, 0);
  501. put_flush_packet(pb);
  502. return 0;
  503. }
  504. static int avi_write_trailer(AVFormatContext *s)
  505. {
  506. AVIContext *avi = s->priv_data;
  507. ByteIOContext *pb = s->pb;
  508. int res = 0;
  509. int i, j, n, nb_frames;
  510. int64_t file_size;
  511. if (!url_is_streamed(pb)){
  512. if (avi->riff_id == 1) {
  513. ff_end_tag(pb, avi->movi_list);
  514. res = avi_write_idx1(s);
  515. ff_end_tag(pb, avi->riff_start);
  516. } else {
  517. avi_write_ix(s);
  518. ff_end_tag(pb, avi->movi_list);
  519. ff_end_tag(pb, avi->riff_start);
  520. file_size = url_ftell(pb);
  521. url_fseek(pb, avi->odml_list - 8, SEEK_SET);
  522. put_tag(pb, "LIST"); /* Making this AVI OpenDML one */
  523. url_fskip(pb, 16);
  524. for (n=nb_frames=0;n<s->nb_streams;n++) {
  525. AVCodecContext *stream = s->streams[n]->codec;
  526. AVIStream *avist= s->streams[n]->priv_data;
  527. if (stream->codec_type == AVMEDIA_TYPE_VIDEO) {
  528. if (nb_frames < avist->packet_count)
  529. nb_frames = avist->packet_count;
  530. } else {
  531. if (stream->codec_id == CODEC_ID_MP2 || stream->codec_id == CODEC_ID_MP3) {
  532. nb_frames += avist->packet_count;
  533. }
  534. }
  535. }
  536. put_le32(pb, nb_frames);
  537. url_fseek(pb, file_size, SEEK_SET);
  538. avi_write_counters(s, avi->riff_id);
  539. }
  540. }
  541. put_flush_packet(pb);
  542. for (i=0; i<s->nb_streams; i++) {
  543. AVIStream *avist= s->streams[i]->priv_data;
  544. for (j=0; j<avist->indexes.ents_allocated/AVI_INDEX_CLUSTER_SIZE; j++)
  545. av_free(avist->indexes.cluster[j]);
  546. av_freep(&avist->indexes.cluster);
  547. avist->indexes.ents_allocated = avist->indexes.entry = 0;
  548. }
  549. return res;
  550. }
  551. AVOutputFormat ff_avi_muxer = {
  552. "avi",
  553. NULL_IF_CONFIG_SMALL("AVI format"),
  554. "video/x-msvideo",
  555. "avi",
  556. sizeof(AVIContext),
  557. CODEC_ID_MP2,
  558. CODEC_ID_MPEG4,
  559. avi_write_header,
  560. avi_write_packet,
  561. avi_write_trailer,
  562. .codec_tag= (const AVCodecTag* const []){ff_codec_bmp_tags, ff_codec_wav_tags, 0},
  563. .flags= AVFMT_VARIABLE_FPS,
  564. };