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.

670 lines
22KB

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