avienc.c 19 KB

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