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 Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; 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 AVIContext {
  43. int64_t riff_start, movi_list, odml_list;
  44. int64_t frames_hdr_all;
  45. int riff_id;
  46. } AVIContext;
  47. typedef struct AVIStream {
  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 int avi_write_counters(AVFormatContext *s, int riff_id)
  96. {
  97. AVIOContext *pb = s->pb;
  98. AVIContext *avi = s->priv_data;
  99. int n, au_byterate, au_ssize, au_scale, nb_frames = 0;
  100. int64_t file_size;
  101. AVCodecParameters *par;
  102. file_size = avio_tell(pb);
  103. for (n = 0; n < s->nb_streams; n++) {
  104. AVIStream *avist = s->streams[n]->priv_data;
  105. assert(avist->frames_hdr_strm);
  106. par = s->streams[n]->codecpar;
  107. avio_seek(pb, avist->frames_hdr_strm, SEEK_SET);
  108. ff_parse_specific_params(s->streams[n], &au_byterate, &au_ssize, &au_scale);
  109. if (au_ssize == 0)
  110. avio_wl32(pb, avist->packet_count);
  111. else
  112. avio_wl32(pb, avist->audio_strm_length / au_ssize);
  113. if (par->codec_type == AVMEDIA_TYPE_VIDEO)
  114. nb_frames = FFMAX(nb_frames, avist->packet_count);
  115. }
  116. if (riff_id == 1) {
  117. assert(avi->frames_hdr_all);
  118. avio_seek(pb, avi->frames_hdr_all, SEEK_SET);
  119. avio_wl32(pb, nb_frames);
  120. }
  121. avio_seek(pb, file_size, SEEK_SET);
  122. return 0;
  123. }
  124. static int avi_write_header(AVFormatContext *s)
  125. {
  126. AVIContext *avi = s->priv_data;
  127. AVIOContext *pb = s->pb;
  128. int bitrate, n, i, nb_frames, au_byterate, au_ssize, au_scale;
  129. AVCodecParameters *video_par;
  130. AVStream *video_st = NULL;
  131. int64_t list1, list2, strh, strf;
  132. AVDictionaryEntry *t = NULL;
  133. if (s->nb_streams > AVI_MAX_STREAM_COUNT) {
  134. av_log(s, AV_LOG_ERROR, "AVI does not support >%d streams\n",
  135. AVI_MAX_STREAM_COUNT);
  136. return -1;
  137. }
  138. for (n = 0; n < s->nb_streams; n++) {
  139. s->streams[n]->priv_data = av_mallocz(sizeof(AVIStream));
  140. if (!s->streams[n]->priv_data)
  141. return AVERROR(ENOMEM);
  142. }
  143. /* header list */
  144. avi->riff_id = 0;
  145. list1 = avi_start_new_riff(s, pb, "AVI ", "hdrl");
  146. /* avi header */
  147. ffio_wfourcc(pb, "avih");
  148. avio_wl32(pb, 14 * 4);
  149. bitrate = 0;
  150. video_par = NULL;
  151. for (n = 0; n < s->nb_streams; n++) {
  152. AVCodecParameters *par = s->streams[n]->codecpar;
  153. bitrate += par->bit_rate;
  154. if (par->codec_type == AVMEDIA_TYPE_VIDEO) {
  155. video_par = par;
  156. video_st = s->streams[n];
  157. }
  158. }
  159. nb_frames = 0;
  160. // TODO: should be avg_frame_rate
  161. if (video_st)
  162. avio_wl32(pb, (uint32_t) (INT64_C(1000000) * video_st->time_base.num /
  163. video_st->time_base.den));
  164. else
  165. avio_wl32(pb, 0);
  166. avio_wl32(pb, bitrate / 8); /* XXX: not quite exact */
  167. avio_wl32(pb, 0); /* padding */
  168. if (!(pb->seekable & AVIO_SEEKABLE_NORMAL))
  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_par) {
  178. avio_wl32(pb, video_par->width);
  179. avio_wl32(pb, video_par->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. AVStream *st = s->streams[i];
  191. AVCodecParameters *par = st->codecpar;
  192. AVIStream *avist = st->priv_data;
  193. list2 = ff_start_tag(pb, "LIST");
  194. ffio_wfourcc(pb, "strl");
  195. /* stream generic header */
  196. strh = ff_start_tag(pb, "strh");
  197. switch (par->codec_type) {
  198. case AVMEDIA_TYPE_SUBTITLE:
  199. // XSUB subtitles behave like video tracks, other subtitles
  200. // are not (yet) supported.
  201. if (par->codec_id != AV_CODEC_ID_XSUB) {
  202. avpriv_report_missing_feature(s, "Subtitle streams other than DivX XSUB");
  203. return AVERROR_PATCHWELCOME;
  204. }
  205. case AVMEDIA_TYPE_VIDEO:
  206. ffio_wfourcc(pb, "vids");
  207. break;
  208. case AVMEDIA_TYPE_AUDIO:
  209. ffio_wfourcc(pb, "auds");
  210. break;
  211. // case AVMEDIA_TYPE_TEXT:
  212. // ffio_wfourcc(pb, "txts");
  213. // break;
  214. case AVMEDIA_TYPE_DATA:
  215. ffio_wfourcc(pb, "dats");
  216. break;
  217. }
  218. if (par->codec_type == AVMEDIA_TYPE_VIDEO ||
  219. par->codec_id == AV_CODEC_ID_XSUB)
  220. avio_wl32(pb, par->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(st, &au_byterate, &au_ssize, &au_scale);
  228. avio_wl32(pb, au_scale); /* scale */
  229. avio_wl32(pb, au_byterate); /* rate */
  230. avpriv_set_pts_info(st, 64, au_scale, au_byterate);
  231. avio_wl32(pb, 0); /* start */
  232. /* remember this offset to fill later */
  233. avist->frames_hdr_strm = avio_tell(pb);
  234. if (!(pb->seekable & AVIO_SEEKABLE_NORMAL))
  235. /* FIXME: this may be broken, but who cares */
  236. avio_wl32(pb, AVI_MAX_RIFF_SIZE);
  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 (par->codec_type == AVMEDIA_TYPE_VIDEO)
  241. avio_wl32(pb, 1024 * 1024);
  242. else if (par->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, par->width);
  250. avio_wl16(pb, par->height);
  251. ff_end_tag(pb, strh);
  252. if (par->codec_type != AVMEDIA_TYPE_DATA) {
  253. strf = ff_start_tag(pb, "strf");
  254. switch (par->codec_type) {
  255. case AVMEDIA_TYPE_SUBTITLE:
  256. /* XSUB subtitles behave like video tracks, other subtitles
  257. * are not (yet) supported. */
  258. if (par->codec_id != AV_CODEC_ID_XSUB)
  259. break;
  260. case AVMEDIA_TYPE_VIDEO:
  261. ff_put_bmp_header(pb, par, ff_codec_bmp_tags, 0);
  262. break;
  263. case AVMEDIA_TYPE_AUDIO:
  264. if (ff_put_wav_header(s, pb, par) < 0)
  265. return -1;
  266. break;
  267. default:
  268. return -1;
  269. }
  270. ff_end_tag(pb, strf);
  271. if ((t = av_dict_get(st->metadata, "title", NULL, 0))) {
  272. ff_riff_write_info_tag(s->pb, "strn", t->value);
  273. t = NULL;
  274. }
  275. }
  276. if (pb->seekable & AVIO_SEEKABLE_NORMAL) {
  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. avist->indexes.entry = avist->indexes.ents_allocated = 0;
  284. avist->indexes.indx_start = ff_start_tag(pb, "JUNK");
  285. avio_wl16(pb, 4); /* wLongsPerEntry */
  286. avio_w8(pb, 0); /* bIndexSubType (0 == frame index) */
  287. avio_w8(pb, 0); /* bIndexType (0 == AVI_INDEX_OF_INDEXES) */
  288. avio_wl32(pb, 0); /* nEntriesInUse (will fill out later on) */
  289. ffio_wfourcc(pb, avi_stream2fourcc(tag, i, par->codec_type));
  290. /* dwChunkId */
  291. avio_wl64(pb, 0); /* dwReserved[3] */
  292. // avio_wl32(pb, 0); /* Must be 0. */
  293. for (j = 0; j < AVI_MASTER_INDEX_SIZE * 2; j++)
  294. avio_wl64(pb, 0);
  295. ff_end_tag(pb, avist->indexes.indx_start);
  296. }
  297. if (par->codec_type == AVMEDIA_TYPE_VIDEO &&
  298. st->sample_aspect_ratio.num > 0 &&
  299. st->sample_aspect_ratio.den > 0) {
  300. int vprp = ff_start_tag(pb, "vprp");
  301. AVRational dar = av_mul_q(st->sample_aspect_ratio,
  302. (AVRational) { par->width,
  303. par->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. // TODO: should be avg_frame_rate
  309. avio_wl32(pb, lrintf(1.0 / av_q2d(st->time_base)));
  310. avio_wl32(pb, par->width);
  311. avio_wl32(pb, par->height);
  312. avio_wl16(pb, den);
  313. avio_wl16(pb, num);
  314. avio_wl32(pb, par->width);
  315. avio_wl32(pb, par->height);
  316. avio_wl32(pb, 1); // progressive FIXME
  317. avio_wl32(pb, par->height);
  318. avio_wl32(pb, par->width);
  319. avio_wl32(pb, par->height);
  320. avio_wl32(pb, par->width);
  321. avio_wl32(pb, 0);
  322. avio_wl32(pb, 0);
  323. avio_wl32(pb, 0);
  324. avio_wl32(pb, 0);
  325. ff_end_tag(pb, vprp);
  326. }
  327. ff_end_tag(pb, list2);
  328. }
  329. if (pb->seekable & AVIO_SEEKABLE_NORMAL) {
  330. /* AVI could become an OpenDML one, if it grows beyond 2Gb range */
  331. avi->odml_list = ff_start_tag(pb, "JUNK");
  332. ffio_wfourcc(pb, "odml");
  333. ffio_wfourcc(pb, "dmlh");
  334. avio_wl32(pb, 248);
  335. for (i = 0; i < 248; i += 4)
  336. avio_wl32(pb, 0);
  337. ff_end_tag(pb, avi->odml_list);
  338. }
  339. ff_end_tag(pb, list1);
  340. ff_riff_write_info(s);
  341. /* some padding for easier tag editing */
  342. list2 = ff_start_tag(pb, "JUNK");
  343. for (i = 0; i < 1016; i += 4)
  344. avio_wl32(pb, 0);
  345. ff_end_tag(pb, list2);
  346. avi->movi_list = ff_start_tag(pb, "LIST");
  347. ffio_wfourcc(pb, "movi");
  348. avio_flush(pb);
  349. return 0;
  350. }
  351. static int avi_write_ix(AVFormatContext *s)
  352. {
  353. AVIOContext *pb = s->pb;
  354. AVIContext *avi = s->priv_data;
  355. char tag[5];
  356. char ix_tag[] = "ix00";
  357. int i, j;
  358. assert(pb->seekable & AVIO_SEEKABLE_NORMAL);
  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, i, s->streams[i]->codecpar->codec_type);
  365. ix_tag[3] = '0' + i;
  366. /* Writing AVI OpenDML leaf index chunk */
  367. ix = avio_tell(pb);
  368. ffio_wfourcc(pb, ix_tag); /* ix?? */
  369. avio_wl32(pb, avist->indexes.entry * 8 + 24);
  370. /* chunk size */
  371. avio_wl16(pb, 2); /* wLongsPerEntry */
  372. avio_w8(pb, 0); /* bIndexSubType (0 == frame index) */
  373. avio_w8(pb, 1); /* bIndexType (1 == AVI_INDEX_OF_CHUNKS) */
  374. avio_wl32(pb, avist->indexes.entry);
  375. /* nEntriesInUse */
  376. ffio_wfourcc(pb, tag); /* dwChunkId */
  377. avio_wl64(pb, avi->movi_list); /* qwBaseOffset */
  378. avio_wl32(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. avio_wl32(pb, ie->pos + 8);
  382. avio_wl32(pb, ((uint32_t) ie->len & ~0x80000000) |
  383. (ie->flags & 0x10 ? 0 : 0x80000000));
  384. }
  385. avio_flush(pb);
  386. pos = avio_tell(pb);
  387. /* Updating one entry in the AVI OpenDML master index */
  388. avio_seek(pb, avist->indexes.indx_start - 8, SEEK_SET);
  389. ffio_wfourcc(pb, "indx"); /* enabling this entry */
  390. avio_skip(pb, 8);
  391. avio_wl32(pb, avi->riff_id); /* nEntriesInUse */
  392. avio_skip(pb, 16 * avi->riff_id);
  393. avio_wl64(pb, ix); /* qwOffset */
  394. avio_wl32(pb, pos - ix); /* dwSize */
  395. avio_wl32(pb, avist->indexes.entry); /* dwDuration */
  396. avio_seek(pb, pos, SEEK_SET);
  397. }
  398. return 0;
  399. }
  400. static int avi_write_idx1(AVFormatContext *s)
  401. {
  402. AVIOContext *pb = s->pb;
  403. AVIContext *avi = s->priv_data;
  404. int64_t idx_chunk;
  405. int i;
  406. char tag[5];
  407. if (pb->seekable & AVIO_SEEKABLE_NORMAL) {
  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, stream_id,
  432. s->streams[stream_id]->codecpar->codec_type);
  433. ffio_wfourcc(pb, tag);
  434. avio_wl32(pb, ie->flags);
  435. avio_wl32(pb, ie->pos);
  436. avio_wl32(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. unsigned char tag[5];
  448. unsigned int flags = 0;
  449. const int stream_index = pkt->stream_index;
  450. int size = pkt->size;
  451. AVIContext *avi = s->priv_data;
  452. AVIOContext *pb = s->pb;
  453. AVIStream *avist = s->streams[stream_index]->priv_data;
  454. AVCodecParameters *par = s->streams[stream_index]->codecpar;
  455. while (par->block_align == 0 && pkt->dts != AV_NOPTS_VALUE &&
  456. 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. }
  464. avist->packet_count++;
  465. // Make sure to put an OpenDML chunk when the file size exceeds the limits
  466. if ((pb->seekable & AVIO_SEEKABLE_NORMAL) &&
  467. (avio_tell(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, stream_index, par->codec_type);
  476. if (pkt->flags & AV_PKT_FLAG_KEY)
  477. flags = 0x10;
  478. if (par->codec_type == AVMEDIA_TYPE_AUDIO)
  479. avist->audio_strm_length += size;
  480. if (s->pb->seekable & AVIO_SEEKABLE_NORMAL) {
  481. int err;
  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. if ((err = av_reallocp(&idx->cluster,
  487. (cl + 1) * sizeof(*idx->cluster))) < 0) {
  488. idx->ents_allocated = 0;
  489. idx->entry = 0;
  490. return err;
  491. }
  492. idx->cluster[cl] =
  493. av_malloc(AVI_INDEX_CLUSTER_SIZE * sizeof(AVIIentry));
  494. if (!idx->cluster[cl])
  495. return -1;
  496. idx->ents_allocated += AVI_INDEX_CLUSTER_SIZE;
  497. }
  498. idx->cluster[cl][id].flags = flags;
  499. idx->cluster[cl][id].pos = avio_tell(pb) - avi->movi_list;
  500. idx->cluster[cl][id].len = size;
  501. idx->entry++;
  502. }
  503. avio_write(pb, tag, 4);
  504. avio_wl32(pb, size);
  505. avio_write(pb, pkt->data, size);
  506. if (size & 1)
  507. avio_w8(pb, 0);
  508. return 0;
  509. }
  510. static int avi_write_trailer(AVFormatContext *s)
  511. {
  512. AVIContext *avi = s->priv_data;
  513. AVIOContext *pb = s->pb;
  514. int res = 0;
  515. int i, j, n, nb_frames;
  516. int64_t file_size;
  517. if (pb->seekable & AVIO_SEEKABLE_NORMAL) {
  518. if (avi->riff_id == 1) {
  519. ff_end_tag(pb, avi->movi_list);
  520. res = avi_write_idx1(s);
  521. ff_end_tag(pb, avi->riff_start);
  522. } else {
  523. avi_write_ix(s);
  524. ff_end_tag(pb, avi->movi_list);
  525. ff_end_tag(pb, avi->riff_start);
  526. file_size = avio_tell(pb);
  527. avio_seek(pb, avi->odml_list - 8, SEEK_SET);
  528. ffio_wfourcc(pb, "LIST"); /* Making this AVI OpenDML one */
  529. avio_skip(pb, 16);
  530. for (n = nb_frames = 0; n < s->nb_streams; n++) {
  531. AVCodecParameters *par = s->streams[n]->codecpar;
  532. AVIStream *avist = s->streams[n]->priv_data;
  533. if (par->codec_type == AVMEDIA_TYPE_VIDEO) {
  534. if (nb_frames < avist->packet_count)
  535. nb_frames = avist->packet_count;
  536. } else {
  537. if (par->codec_id == AV_CODEC_ID_MP2 ||
  538. par->codec_id == AV_CODEC_ID_MP3)
  539. nb_frames += avist->packet_count;
  540. }
  541. }
  542. avio_wl32(pb, nb_frames);
  543. avio_seek(pb, file_size, SEEK_SET);
  544. avi_write_counters(s, avi->riff_id);
  545. }
  546. }
  547. for (i = 0; i < s->nb_streams; i++) {
  548. AVIStream *avist = s->streams[i]->priv_data;
  549. for (j = 0; j < avist->indexes.ents_allocated / AVI_INDEX_CLUSTER_SIZE; j++)
  550. av_free(avist->indexes.cluster[j]);
  551. av_freep(&avist->indexes.cluster);
  552. avist->indexes.ents_allocated = avist->indexes.entry = 0;
  553. }
  554. return res;
  555. }
  556. AVOutputFormat ff_avi_muxer = {
  557. .name = "avi",
  558. .long_name = NULL_IF_CONFIG_SMALL("AVI (Audio Video Interleaved)"),
  559. .mime_type = "video/x-msvideo",
  560. .extensions = "avi",
  561. .priv_data_size = sizeof(AVIContext),
  562. .audio_codec = CONFIG_LIBMP3LAME ? AV_CODEC_ID_MP3 : AV_CODEC_ID_AC3,
  563. .video_codec = AV_CODEC_ID_MPEG4,
  564. .write_header = avi_write_header,
  565. .write_packet = avi_write_packet,
  566. .write_trailer = avi_write_trailer,
  567. .codec_tag = (const AVCodecTag * const []) {
  568. ff_codec_bmp_tags, ff_codec_wav_tags, 0
  569. },
  570. };