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.

651 lines
21KB

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