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.

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