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.

652 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. #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. put_strz(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, "strn", NULL, 0))) {
  262. avi_write_info_tag(s->pb, t->key, t->value);
  263. t = NULL;
  264. }
  265. //FIXME a limitation of metadata conversion system
  266. else if ((t = av_metadata_get(s->streams[i]->metadata, "INAM", NULL, 0))) {
  267. avi_write_info_tag(s->pb, "strn", t->value);
  268. t = NULL;
  269. }
  270. }
  271. if (!url_is_streamed(pb)) {
  272. unsigned char tag[5];
  273. int j;
  274. /* Starting to lay out AVI OpenDML master index.
  275. * We want to make it JUNK entry for now, since we'd
  276. * like to get away without making AVI an OpenDML one
  277. * for compatibility reasons.
  278. */
  279. avist->indexes.entry = avist->indexes.ents_allocated = 0;
  280. avist->indexes.indx_start = ff_start_tag(pb, "JUNK");
  281. put_le16(pb, 4); /* wLongsPerEntry */
  282. put_byte(pb, 0); /* bIndexSubType (0 == frame index) */
  283. put_byte(pb, 0); /* bIndexType (0 == AVI_INDEX_OF_INDEXES) */
  284. put_le32(pb, 0); /* nEntriesInUse (will fill out later on) */
  285. put_tag(pb, avi_stream2fourcc(&tag[0], i, stream->codec_type));
  286. /* dwChunkId */
  287. put_le64(pb, 0); /* dwReserved[3]
  288. put_le32(pb, 0); Must be 0. */
  289. for (j=0; j < AVI_MASTER_INDEX_SIZE * 2; j++)
  290. put_le64(pb, 0);
  291. ff_end_tag(pb, avist->indexes.indx_start);
  292. }
  293. if( stream->codec_type == AVMEDIA_TYPE_VIDEO
  294. && s->streams[i]->sample_aspect_ratio.num>0
  295. && s->streams[i]->sample_aspect_ratio.den>0){
  296. int vprp= ff_start_tag(pb, "vprp");
  297. AVRational dar = av_mul_q(s->streams[i]->sample_aspect_ratio,
  298. (AVRational){stream->width, stream->height});
  299. int num, den;
  300. av_reduce(&num, &den, dar.num, dar.den, 0xFFFF);
  301. put_le32(pb, 0); //video format = unknown
  302. put_le32(pb, 0); //video standard= unknown
  303. put_le32(pb, lrintf(1.0/av_q2d(stream->time_base)));
  304. put_le32(pb, stream->width );
  305. put_le32(pb, stream->height);
  306. put_le16(pb, den);
  307. put_le16(pb, num);
  308. put_le32(pb, stream->width );
  309. put_le32(pb, stream->height);
  310. put_le32(pb, 1); //progressive FIXME
  311. put_le32(pb, stream->height);
  312. put_le32(pb, stream->width );
  313. put_le32(pb, stream->height);
  314. put_le32(pb, stream->width );
  315. put_le32(pb, 0);
  316. put_le32(pb, 0);
  317. put_le32(pb, 0);
  318. put_le32(pb, 0);
  319. ff_end_tag(pb, vprp);
  320. }
  321. ff_end_tag(pb, list2);
  322. }
  323. if (!url_is_streamed(pb)) {
  324. /* AVI could become an OpenDML one, if it grows beyond 2Gb range */
  325. avi->odml_list = ff_start_tag(pb, "JUNK");
  326. put_tag(pb, "odml");
  327. put_tag(pb, "dmlh");
  328. put_le32(pb, 248);
  329. for (i = 0; i < 248; i+= 4)
  330. put_le32(pb, 0);
  331. ff_end_tag(pb, avi->odml_list);
  332. }
  333. ff_end_tag(pb, list1);
  334. list2 = ff_start_tag(pb, "LIST");
  335. put_tag(pb, "INFO");
  336. for (i = 0; *ff_avi_tags[i]; i++) {
  337. if ((t = av_metadata_get(s->metadata, ff_avi_tags[i], NULL, AV_METADATA_MATCH_CASE)))
  338. avi_write_info_tag(s->pb, t->key, t->value);
  339. }
  340. ff_end_tag(pb, list2);
  341. /* some padding for easier tag editing */
  342. list2 = ff_start_tag(pb, "JUNK");
  343. for (i = 0; i < 1016; i += 4)
  344. put_le32(pb, 0);
  345. ff_end_tag(pb, list2);
  346. avi->movi_list = ff_start_tag(pb, "LIST");
  347. put_tag(pb, "movi");
  348. put_flush_packet(pb);
  349. return 0;
  350. }
  351. static int avi_write_ix(AVFormatContext *s)
  352. {
  353. ByteIOContext *pb = s->pb;
  354. AVIContext *avi = s->priv_data;
  355. char tag[5];
  356. char ix_tag[] = "ix00";
  357. int i, j;
  358. assert(!url_is_streamed(pb));
  359. if (avi->riff_id > AVI_MASTER_INDEX_SIZE)
  360. return -1;
  361. for (i=0;i<s->nb_streams;i++) {
  362. AVIStream *avist= s->streams[i]->priv_data;
  363. int64_t ix, pos;
  364. avi_stream2fourcc(&tag[0], i, s->streams[i]->codec->codec_type);
  365. ix_tag[3] = '0' + i;
  366. /* Writing AVI OpenDML leaf index chunk */
  367. ix = url_ftell(pb);
  368. put_tag(pb, &ix_tag[0]); /* ix?? */
  369. put_le32(pb, avist->indexes.entry * 8 + 24);
  370. /* chunk size */
  371. put_le16(pb, 2); /* wLongsPerEntry */
  372. put_byte(pb, 0); /* bIndexSubType (0 == frame index) */
  373. put_byte(pb, 1); /* bIndexType (1 == AVI_INDEX_OF_CHUNKS) */
  374. put_le32(pb, avist->indexes.entry);
  375. /* nEntriesInUse */
  376. put_tag(pb, &tag[0]); /* dwChunkId */
  377. put_le64(pb, avi->movi_list);/* qwBaseOffset */
  378. put_le32(pb, 0); /* dwReserved_3 (must be 0) */
  379. for (j=0; j<avist->indexes.entry; j++) {
  380. AVIIentry* ie = avi_get_ientry(&avist->indexes, j);
  381. put_le32(pb, ie->pos + 8);
  382. put_le32(pb, ((uint32_t)ie->len & ~0x80000000) |
  383. (ie->flags & 0x10 ? 0 : 0x80000000));
  384. }
  385. put_flush_packet(pb);
  386. pos = url_ftell(pb);
  387. /* Updating one entry in the AVI OpenDML master index */
  388. url_fseek(pb, avist->indexes.indx_start - 8, SEEK_SET);
  389. put_tag(pb, "indx"); /* enabling this entry */
  390. url_fskip(pb, 8);
  391. put_le32(pb, avi->riff_id); /* nEntriesInUse */
  392. url_fskip(pb, 16*avi->riff_id);
  393. put_le64(pb, ix); /* qwOffset */
  394. put_le32(pb, pos - ix); /* dwSize */
  395. put_le32(pb, avist->indexes.entry); /* dwDuration */
  396. url_fseek(pb, pos, SEEK_SET);
  397. }
  398. return 0;
  399. }
  400. static int avi_write_idx1(AVFormatContext *s)
  401. {
  402. ByteIOContext *pb = s->pb;
  403. AVIContext *avi = s->priv_data;
  404. int64_t idx_chunk;
  405. int i;
  406. char tag[5];
  407. if (!url_is_streamed(pb)) {
  408. AVIStream *avist;
  409. AVIIentry* ie = 0, *tie;
  410. int empty, stream_id = -1;
  411. idx_chunk = ff_start_tag(pb, "idx1");
  412. for(i=0; i<s->nb_streams; i++){
  413. avist= s->streams[i]->priv_data;
  414. avist->entry=0;
  415. }
  416. do {
  417. empty = 1;
  418. for (i=0; i<s->nb_streams; i++) {
  419. avist= s->streams[i]->priv_data;
  420. if (avist->indexes.entry <= avist->entry)
  421. continue;
  422. tie = avi_get_ientry(&avist->indexes, avist->entry);
  423. if (empty || tie->pos < ie->pos) {
  424. ie = tie;
  425. stream_id = i;
  426. }
  427. empty = 0;
  428. }
  429. if (!empty) {
  430. avist= s->streams[stream_id]->priv_data;
  431. avi_stream2fourcc(&tag[0], stream_id,
  432. s->streams[stream_id]->codec->codec_type);
  433. put_tag(pb, &tag[0]);
  434. put_le32(pb, ie->flags);
  435. put_le32(pb, ie->pos);
  436. put_le32(pb, ie->len);
  437. avist->entry++;
  438. }
  439. } while (!empty);
  440. ff_end_tag(pb, idx_chunk);
  441. avi_write_counters(s, avi->riff_id);
  442. }
  443. return 0;
  444. }
  445. static int avi_write_packet(AVFormatContext *s, AVPacket *pkt)
  446. {
  447. AVIContext *avi = s->priv_data;
  448. ByteIOContext *pb = s->pb;
  449. unsigned char tag[5];
  450. unsigned int flags=0;
  451. const int stream_index= pkt->stream_index;
  452. AVIStream *avist= s->streams[stream_index]->priv_data;
  453. AVCodecContext *enc= s->streams[stream_index]->codec;
  454. int size= pkt->size;
  455. // av_log(s, AV_LOG_DEBUG, "%"PRId64" %d %d\n", pkt->dts, avi->packet_count[stream_index], stream_index);
  456. while(enc->block_align==0 && pkt->dts != AV_NOPTS_VALUE && pkt->dts > avist->packet_count){
  457. AVPacket empty_packet;
  458. av_init_packet(&empty_packet);
  459. empty_packet.size= 0;
  460. empty_packet.data= NULL;
  461. empty_packet.stream_index= stream_index;
  462. avi_write_packet(s, &empty_packet);
  463. // av_log(s, AV_LOG_DEBUG, "dup %"PRId64" %d\n", pkt->dts, avi->packet_count[stream_index]);
  464. }
  465. avist->packet_count++;
  466. // Make sure to put an OpenDML chunk when the file size exceeds the limits
  467. if (!url_is_streamed(pb) &&
  468. (url_ftell(pb) - avi->riff_start > AVI_MAX_RIFF_SIZE)) {
  469. avi_write_ix(s);
  470. ff_end_tag(pb, avi->movi_list);
  471. if (avi->riff_id == 1)
  472. avi_write_idx1(s);
  473. ff_end_tag(pb, avi->riff_start);
  474. avi->movi_list = avi_start_new_riff(s, pb, "AVIX", "movi");
  475. }
  476. avi_stream2fourcc(&tag[0], stream_index, enc->codec_type);
  477. if(pkt->flags&AV_PKT_FLAG_KEY)
  478. flags = 0x10;
  479. if (enc->codec_type == AVMEDIA_TYPE_AUDIO) {
  480. avist->audio_strm_length += size;
  481. }
  482. if (!url_is_streamed(s->pb)) {
  483. AVIIndex* idx = &avist->indexes;
  484. int cl = idx->entry / AVI_INDEX_CLUSTER_SIZE;
  485. int id = idx->entry % AVI_INDEX_CLUSTER_SIZE;
  486. if (idx->ents_allocated <= idx->entry) {
  487. idx->cluster = av_realloc(idx->cluster, (cl+1)*sizeof(void*));
  488. if (!idx->cluster)
  489. return -1;
  490. idx->cluster[cl] = av_malloc(AVI_INDEX_CLUSTER_SIZE*sizeof(AVIIentry));
  491. if (!idx->cluster[cl])
  492. return -1;
  493. idx->ents_allocated += AVI_INDEX_CLUSTER_SIZE;
  494. }
  495. idx->cluster[cl][id].flags = flags;
  496. idx->cluster[cl][id].pos = url_ftell(pb) - avi->movi_list;
  497. idx->cluster[cl][id].len = size;
  498. idx->entry++;
  499. }
  500. put_buffer(pb, tag, 4);
  501. put_le32(pb, size);
  502. put_buffer(pb, pkt->data, size);
  503. if (size & 1)
  504. put_byte(pb, 0);
  505. put_flush_packet(pb);
  506. return 0;
  507. }
  508. static int avi_write_trailer(AVFormatContext *s)
  509. {
  510. AVIContext *avi = s->priv_data;
  511. ByteIOContext *pb = s->pb;
  512. int res = 0;
  513. int i, j, n, nb_frames;
  514. int64_t file_size;
  515. if (!url_is_streamed(pb)){
  516. if (avi->riff_id == 1) {
  517. ff_end_tag(pb, avi->movi_list);
  518. res = avi_write_idx1(s);
  519. ff_end_tag(pb, avi->riff_start);
  520. } else {
  521. avi_write_ix(s);
  522. ff_end_tag(pb, avi->movi_list);
  523. ff_end_tag(pb, avi->riff_start);
  524. file_size = url_ftell(pb);
  525. url_fseek(pb, avi->odml_list - 8, SEEK_SET);
  526. put_tag(pb, "LIST"); /* Making this AVI OpenDML one */
  527. url_fskip(pb, 16);
  528. for (n=nb_frames=0;n<s->nb_streams;n++) {
  529. AVCodecContext *stream = s->streams[n]->codec;
  530. AVIStream *avist= s->streams[n]->priv_data;
  531. if (stream->codec_type == AVMEDIA_TYPE_VIDEO) {
  532. if (nb_frames < avist->packet_count)
  533. nb_frames = avist->packet_count;
  534. } else {
  535. if (stream->codec_id == CODEC_ID_MP2 || stream->codec_id == CODEC_ID_MP3) {
  536. nb_frames += avist->packet_count;
  537. }
  538. }
  539. }
  540. put_le32(pb, nb_frames);
  541. url_fseek(pb, file_size, SEEK_SET);
  542. avi_write_counters(s, avi->riff_id);
  543. }
  544. }
  545. put_flush_packet(pb);
  546. for (i=0; i<s->nb_streams; i++) {
  547. AVIStream *avist= s->streams[i]->priv_data;
  548. for (j=0; j<avist->indexes.ents_allocated/AVI_INDEX_CLUSTER_SIZE; j++)
  549. av_free(avist->indexes.cluster[j]);
  550. av_freep(&avist->indexes.cluster);
  551. avist->indexes.ents_allocated = avist->indexes.entry = 0;
  552. }
  553. return res;
  554. }
  555. AVOutputFormat avi_muxer = {
  556. "avi",
  557. NULL_IF_CONFIG_SMALL("AVI format"),
  558. "video/x-msvideo",
  559. "avi",
  560. sizeof(AVIContext),
  561. CODEC_ID_MP2,
  562. CODEC_ID_MPEG4,
  563. avi_write_header,
  564. avi_write_packet,
  565. avi_write_trailer,
  566. .codec_tag= (const AVCodecTag* const []){ff_codec_bmp_tags, ff_codec_wav_tags, 0},
  567. .flags= AVFMT_VARIABLE_FPS,
  568. .metadata_conv = ff_avi_metadata_conv,
  569. };