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.

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