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.

647 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 {
  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 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. AVCodecContext *stream;
  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. stream = s->streams[n]->codec;
  107. avio_seek(pb, avist->frames_hdr_strm, SEEK_SET);
  108. ff_parse_specific_params(stream, &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 (stream->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. AVCodecContext *stream, *video_enc;
  130. int64_t list1, list2, strh, strf;
  131. AVDictionaryEntry *t = NULL;
  132. if (s->nb_streams > AVI_MAX_STREAM_COUNT) {
  133. av_log(s, AV_LOG_ERROR, "AVI does not support >%d streams\n",
  134. AVI_MAX_STREAM_COUNT);
  135. return -1;
  136. }
  137. for (n = 0; n < s->nb_streams; n++) {
  138. s->streams[n]->priv_data = av_mallocz(sizeof(AVIStream));
  139. if (!s->streams[n]->priv_data)
  140. return AVERROR(ENOMEM);
  141. }
  142. /* header list */
  143. avi->riff_id = 0;
  144. list1 = avi_start_new_riff(s, pb, "AVI ", "hdrl");
  145. /* avi header */
  146. ffio_wfourcc(pb, "avih");
  147. avio_wl32(pb, 14 * 4);
  148. bitrate = 0;
  149. video_enc = NULL;
  150. for (n = 0; n < s->nb_streams; n++) {
  151. stream = s->streams[n]->codec;
  152. bitrate += stream->bit_rate;
  153. if (stream->codec_type == AVMEDIA_TYPE_VIDEO)
  154. video_enc = stream;
  155. }
  156. nb_frames = 0;
  157. if (video_enc)
  158. avio_wl32(pb, (uint32_t) (INT64_C(1000000) * video_enc->time_base.num /
  159. video_enc->time_base.den));
  160. else
  161. avio_wl32(pb, 0);
  162. avio_wl32(pb, bitrate / 8); /* XXX: not quite exact */
  163. avio_wl32(pb, 0); /* padding */
  164. if (!pb->seekable)
  165. avio_wl32(pb, AVIF_TRUSTCKTYPE | AVIF_ISINTERLEAVED); /* flags */
  166. else
  167. avio_wl32(pb, AVIF_TRUSTCKTYPE | AVIF_HASINDEX | AVIF_ISINTERLEAVED); /* flags */
  168. avi->frames_hdr_all = avio_tell(pb); /* remember this offset to fill later */
  169. avio_wl32(pb, nb_frames); /* nb frames, filled later */
  170. avio_wl32(pb, 0); /* initial frame */
  171. avio_wl32(pb, s->nb_streams); /* nb streams */
  172. avio_wl32(pb, 1024 * 1024); /* suggested buffer size */
  173. if (video_enc) {
  174. avio_wl32(pb, video_enc->width);
  175. avio_wl32(pb, video_enc->height);
  176. } else {
  177. avio_wl32(pb, 0);
  178. avio_wl32(pb, 0);
  179. }
  180. avio_wl32(pb, 0); /* reserved */
  181. avio_wl32(pb, 0); /* reserved */
  182. avio_wl32(pb, 0); /* reserved */
  183. avio_wl32(pb, 0); /* reserved */
  184. /* stream list */
  185. for (i = 0; i < n; i++) {
  186. AVIStream *avist = s->streams[i]->priv_data;
  187. list2 = ff_start_tag(pb, "LIST");
  188. ffio_wfourcc(pb, "strl");
  189. stream = s->streams[i]->codec;
  190. /* stream generic header */
  191. strh = ff_start_tag(pb, "strh");
  192. switch (stream->codec_type) {
  193. case AVMEDIA_TYPE_SUBTITLE:
  194. // XSUB subtitles behave like video tracks, other subtitles
  195. // are not (yet) supported.
  196. if (stream->codec_id != AV_CODEC_ID_XSUB) {
  197. av_log(s, AV_LOG_ERROR,
  198. "Subtitle streams other than DivX XSUB are not supported by the AVI muxer.\n");
  199. return AVERROR_PATCHWELCOME;
  200. }
  201. case AVMEDIA_TYPE_VIDEO:
  202. ffio_wfourcc(pb, "vids");
  203. break;
  204. case AVMEDIA_TYPE_AUDIO:
  205. ffio_wfourcc(pb, "auds");
  206. break;
  207. // case AVMEDIA_TYPE_TEXT:
  208. // ffio_wfourcc(pb, "txts");
  209. // break;
  210. case AVMEDIA_TYPE_DATA:
  211. ffio_wfourcc(pb, "dats");
  212. break;
  213. }
  214. if (stream->codec_type == AVMEDIA_TYPE_VIDEO ||
  215. stream->codec_id == AV_CODEC_ID_XSUB)
  216. avio_wl32(pb, stream->codec_tag);
  217. else
  218. avio_wl32(pb, 1);
  219. avio_wl32(pb, 0); /* flags */
  220. avio_wl16(pb, 0); /* priority */
  221. avio_wl16(pb, 0); /* language */
  222. avio_wl32(pb, 0); /* initial frame */
  223. ff_parse_specific_params(stream, &au_byterate, &au_ssize, &au_scale);
  224. avio_wl32(pb, au_scale); /* scale */
  225. avio_wl32(pb, au_byterate); /* rate */
  226. avpriv_set_pts_info(s->streams[i], 64, au_scale, au_byterate);
  227. avio_wl32(pb, 0); /* start */
  228. /* remember this offset to fill later */
  229. avist->frames_hdr_strm = avio_tell(pb);
  230. if (!pb->seekable)
  231. /* FIXME: this may be broken, but who cares */
  232. avio_wl32(pb, AVI_MAX_RIFF_SIZE);
  233. else
  234. avio_wl32(pb, 0); /* length, XXX: filled later */
  235. /* suggested buffer size */ //FIXME set at the end to largest chunk
  236. if (stream->codec_type == AVMEDIA_TYPE_VIDEO)
  237. avio_wl32(pb, 1024 * 1024);
  238. else if (stream->codec_type == AVMEDIA_TYPE_AUDIO)
  239. avio_wl32(pb, 12 * 1024);
  240. else
  241. avio_wl32(pb, 0);
  242. avio_wl32(pb, -1); /* quality */
  243. avio_wl32(pb, au_ssize); /* sample size */
  244. avio_wl32(pb, 0);
  245. avio_wl16(pb, stream->width);
  246. avio_wl16(pb, stream->height);
  247. ff_end_tag(pb, strh);
  248. if (stream->codec_type != AVMEDIA_TYPE_DATA) {
  249. strf = ff_start_tag(pb, "strf");
  250. switch (stream->codec_type) {
  251. case AVMEDIA_TYPE_SUBTITLE:
  252. /* XSUB subtitles behave like video tracks, other subtitles
  253. * are not (yet) supported. */
  254. if (stream->codec_id != AV_CODEC_ID_XSUB)
  255. 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. break;
  263. default:
  264. return -1;
  265. }
  266. ff_end_tag(pb, strf);
  267. if ((t = av_dict_get(s->streams[i]->metadata, "title", NULL, 0))) {
  268. ff_riff_write_info_tag(s->pb, "strn", t->value);
  269. t = NULL;
  270. }
  271. }
  272. if (pb->seekable) {
  273. unsigned char tag[5];
  274. int j;
  275. /* Starting to lay out AVI OpenDML master index.
  276. * We want to make it JUNK entry for now, since we'd
  277. * like to get away without making AVI an OpenDML one
  278. * for compatibility reasons. */
  279. avist->indexes.entry = avist->indexes.ents_allocated = 0;
  280. avist->indexes.indx_start = ff_start_tag(pb, "JUNK");
  281. avio_wl16(pb, 4); /* wLongsPerEntry */
  282. avio_w8(pb, 0); /* bIndexSubType (0 == frame index) */
  283. avio_w8(pb, 0); /* bIndexType (0 == AVI_INDEX_OF_INDEXES) */
  284. avio_wl32(pb, 0); /* nEntriesInUse (will fill out later on) */
  285. ffio_wfourcc(pb, avi_stream2fourcc(tag, i, stream->codec_type));
  286. /* dwChunkId */
  287. avio_wl64(pb, 0); /* dwReserved[3] */
  288. // avio_wl32(pb, 0); /* Must be 0. */
  289. for (j = 0; j < AVI_MASTER_INDEX_SIZE * 2; j++)
  290. avio_wl64(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,
  299. stream->height });
  300. int num, den;
  301. av_reduce(&num, &den, dar.num, dar.den, 0xFFFF);
  302. avio_wl32(pb, 0); // video format = unknown
  303. avio_wl32(pb, 0); // video standard = unknown
  304. avio_wl32(pb, lrintf(1.0 / av_q2d(stream->time_base)));
  305. avio_wl32(pb, stream->width);
  306. avio_wl32(pb, stream->height);
  307. avio_wl16(pb, den);
  308. avio_wl16(pb, num);
  309. avio_wl32(pb, stream->width);
  310. avio_wl32(pb, stream->height);
  311. avio_wl32(pb, 1); // progressive FIXME
  312. avio_wl32(pb, stream->height);
  313. avio_wl32(pb, stream->width);
  314. avio_wl32(pb, stream->height);
  315. avio_wl32(pb, stream->width);
  316. avio_wl32(pb, 0);
  317. avio_wl32(pb, 0);
  318. avio_wl32(pb, 0);
  319. avio_wl32(pb, 0);
  320. ff_end_tag(pb, vprp);
  321. }
  322. ff_end_tag(pb, list2);
  323. }
  324. if (pb->seekable) {
  325. /* AVI could become an OpenDML one, if it grows beyond 2Gb range */
  326. avi->odml_list = ff_start_tag(pb, "JUNK");
  327. ffio_wfourcc(pb, "odml");
  328. ffio_wfourcc(pb, "dmlh");
  329. avio_wl32(pb, 248);
  330. for (i = 0; i < 248; i += 4)
  331. avio_wl32(pb, 0);
  332. ff_end_tag(pb, avi->odml_list);
  333. }
  334. ff_end_tag(pb, list1);
  335. ff_riff_write_info(s);
  336. /* some padding for easier tag editing */
  337. list2 = ff_start_tag(pb, "JUNK");
  338. for (i = 0; i < 1016; i += 4)
  339. avio_wl32(pb, 0);
  340. ff_end_tag(pb, list2);
  341. avi->movi_list = ff_start_tag(pb, "LIST");
  342. ffio_wfourcc(pb, "movi");
  343. avio_flush(pb);
  344. return 0;
  345. }
  346. static int avi_write_ix(AVFormatContext *s)
  347. {
  348. AVIOContext *pb = s->pb;
  349. AVIContext *avi = s->priv_data;
  350. char tag[5];
  351. char ix_tag[] = "ix00";
  352. int i, j;
  353. assert(pb->seekable);
  354. if (avi->riff_id > AVI_MASTER_INDEX_SIZE)
  355. return -1;
  356. for (i = 0; i < s->nb_streams; i++) {
  357. AVIStream *avist = s->streams[i]->priv_data;
  358. int64_t ix, pos;
  359. avi_stream2fourcc(tag, i, s->streams[i]->codec->codec_type);
  360. ix_tag[3] = '0' + i;
  361. /* Writing AVI OpenDML leaf index chunk */
  362. ix = avio_tell(pb);
  363. ffio_wfourcc(pb, ix_tag); /* ix?? */
  364. avio_wl32(pb, avist->indexes.entry * 8 + 24);
  365. /* chunk size */
  366. avio_wl16(pb, 2); /* wLongsPerEntry */
  367. avio_w8(pb, 0); /* bIndexSubType (0 == frame index) */
  368. avio_w8(pb, 1); /* bIndexType (1 == AVI_INDEX_OF_CHUNKS) */
  369. avio_wl32(pb, avist->indexes.entry);
  370. /* nEntriesInUse */
  371. ffio_wfourcc(pb, tag); /* dwChunkId */
  372. avio_wl64(pb, avi->movi_list); /* qwBaseOffset */
  373. avio_wl32(pb, 0); /* dwReserved_3 (must be 0) */
  374. for (j = 0; j < avist->indexes.entry; j++) {
  375. AVIIentry *ie = avi_get_ientry(&avist->indexes, j);
  376. avio_wl32(pb, ie->pos + 8);
  377. avio_wl32(pb, ((uint32_t) ie->len & ~0x80000000) |
  378. (ie->flags & 0x10 ? 0 : 0x80000000));
  379. }
  380. avio_flush(pb);
  381. pos = avio_tell(pb);
  382. /* Updating one entry in the AVI OpenDML master index */
  383. avio_seek(pb, avist->indexes.indx_start - 8, SEEK_SET);
  384. ffio_wfourcc(pb, "indx"); /* enabling this entry */
  385. avio_skip(pb, 8);
  386. avio_wl32(pb, avi->riff_id); /* nEntriesInUse */
  387. avio_skip(pb, 16 * avi->riff_id);
  388. avio_wl64(pb, ix); /* qwOffset */
  389. avio_wl32(pb, pos - ix); /* dwSize */
  390. avio_wl32(pb, avist->indexes.entry); /* dwDuration */
  391. avio_seek(pb, pos, SEEK_SET);
  392. }
  393. return 0;
  394. }
  395. static int avi_write_idx1(AVFormatContext *s)
  396. {
  397. AVIOContext *pb = s->pb;
  398. AVIContext *avi = s->priv_data;
  399. int64_t idx_chunk;
  400. int i;
  401. char tag[5];
  402. if (pb->seekable) {
  403. AVIStream *avist;
  404. AVIIentry *ie = 0, *tie;
  405. int empty, stream_id = -1;
  406. idx_chunk = ff_start_tag(pb, "idx1");
  407. for (i = 0; i < s->nb_streams; i++) {
  408. avist = s->streams[i]->priv_data;
  409. avist->entry = 0;
  410. }
  411. do {
  412. empty = 1;
  413. for (i = 0; i < s->nb_streams; i++) {
  414. avist = s->streams[i]->priv_data;
  415. if (avist->indexes.entry <= avist->entry)
  416. continue;
  417. tie = avi_get_ientry(&avist->indexes, avist->entry);
  418. if (empty || tie->pos < ie->pos) {
  419. ie = tie;
  420. stream_id = i;
  421. }
  422. empty = 0;
  423. }
  424. if (!empty) {
  425. avist = s->streams[stream_id]->priv_data;
  426. avi_stream2fourcc(tag, stream_id,
  427. s->streams[stream_id]->codec->codec_type);
  428. ffio_wfourcc(pb, tag);
  429. avio_wl32(pb, ie->flags);
  430. avio_wl32(pb, ie->pos);
  431. avio_wl32(pb, ie->len);
  432. avist->entry++;
  433. }
  434. } while (!empty);
  435. ff_end_tag(pb, idx_chunk);
  436. avi_write_counters(s, avi->riff_id);
  437. }
  438. return 0;
  439. }
  440. static int avi_write_packet(AVFormatContext *s, AVPacket *pkt)
  441. {
  442. unsigned char tag[5];
  443. unsigned int flags = 0;
  444. const int stream_index = pkt->stream_index;
  445. int size = pkt->size;
  446. AVIContext *avi = s->priv_data;
  447. AVIOContext *pb = s->pb;
  448. AVIStream *avist = s->streams[stream_index]->priv_data;
  449. AVCodecContext *enc = s->streams[stream_index]->codec;
  450. while (enc->block_align == 0 && pkt->dts != AV_NOPTS_VALUE &&
  451. pkt->dts > avist->packet_count) {
  452. AVPacket empty_packet;
  453. av_init_packet(&empty_packet);
  454. empty_packet.size = 0;
  455. empty_packet.data = NULL;
  456. empty_packet.stream_index = stream_index;
  457. avi_write_packet(s, &empty_packet);
  458. }
  459. avist->packet_count++;
  460. // Make sure to put an OpenDML chunk when the file size exceeds the limits
  461. if (pb->seekable &&
  462. (avio_tell(pb) - avi->riff_start > AVI_MAX_RIFF_SIZE)) {
  463. avi_write_ix(s);
  464. ff_end_tag(pb, avi->movi_list);
  465. if (avi->riff_id == 1)
  466. avi_write_idx1(s);
  467. ff_end_tag(pb, avi->riff_start);
  468. avi->movi_list = avi_start_new_riff(s, pb, "AVIX", "movi");
  469. }
  470. avi_stream2fourcc(tag, stream_index, enc->codec_type);
  471. if (pkt->flags & AV_PKT_FLAG_KEY)
  472. flags = 0x10;
  473. if (enc->codec_type == AVMEDIA_TYPE_AUDIO)
  474. avist->audio_strm_length += size;
  475. if (s->pb->seekable) {
  476. int err;
  477. AVIIndex *idx = &avist->indexes;
  478. int cl = idx->entry / AVI_INDEX_CLUSTER_SIZE;
  479. int id = idx->entry % AVI_INDEX_CLUSTER_SIZE;
  480. if (idx->ents_allocated <= idx->entry) {
  481. if ((err = av_reallocp(&idx->cluster,
  482. (cl + 1) * sizeof(*idx->cluster))) < 0) {
  483. idx->ents_allocated = 0;
  484. idx->entry = 0;
  485. return err;
  486. }
  487. idx->cluster[cl] =
  488. av_malloc(AVI_INDEX_CLUSTER_SIZE * sizeof(AVIIentry));
  489. if (!idx->cluster[cl])
  490. return -1;
  491. idx->ents_allocated += AVI_INDEX_CLUSTER_SIZE;
  492. }
  493. idx->cluster[cl][id].flags = flags;
  494. idx->cluster[cl][id].pos = avio_tell(pb) - avi->movi_list;
  495. idx->cluster[cl][id].len = size;
  496. idx->entry++;
  497. }
  498. avio_write(pb, tag, 4);
  499. avio_wl32(pb, size);
  500. avio_write(pb, pkt->data, size);
  501. if (size & 1)
  502. avio_w8(pb, 0);
  503. return 0;
  504. }
  505. static int avi_write_trailer(AVFormatContext *s)
  506. {
  507. AVIContext *avi = s->priv_data;
  508. AVIOContext *pb = s->pb;
  509. int res = 0;
  510. int i, j, n, nb_frames;
  511. int64_t file_size;
  512. if (pb->seekable) {
  513. if (avi->riff_id == 1) {
  514. ff_end_tag(pb, avi->movi_list);
  515. res = avi_write_idx1(s);
  516. ff_end_tag(pb, avi->riff_start);
  517. } else {
  518. avi_write_ix(s);
  519. ff_end_tag(pb, avi->movi_list);
  520. ff_end_tag(pb, avi->riff_start);
  521. file_size = avio_tell(pb);
  522. avio_seek(pb, avi->odml_list - 8, SEEK_SET);
  523. ffio_wfourcc(pb, "LIST"); /* Making this AVI OpenDML one */
  524. avio_skip(pb, 16);
  525. for (n = nb_frames = 0; n < s->nb_streams; n++) {
  526. AVCodecContext *stream = s->streams[n]->codec;
  527. AVIStream *avist = s->streams[n]->priv_data;
  528. if (stream->codec_type == AVMEDIA_TYPE_VIDEO) {
  529. if (nb_frames < avist->packet_count)
  530. nb_frames = avist->packet_count;
  531. } else {
  532. if (stream->codec_id == AV_CODEC_ID_MP2 ||
  533. stream->codec_id == AV_CODEC_ID_MP3)
  534. nb_frames += avist->packet_count;
  535. }
  536. }
  537. avio_wl32(pb, nb_frames);
  538. avio_seek(pb, file_size, SEEK_SET);
  539. avi_write_counters(s, avi->riff_id);
  540. }
  541. }
  542. for (i = 0; i < s->nb_streams; i++) {
  543. AVIStream *avist = s->streams[i]->priv_data;
  544. for (j = 0; j < avist->indexes.ents_allocated / AVI_INDEX_CLUSTER_SIZE; j++)
  545. av_free(avist->indexes.cluster[j]);
  546. av_freep(&avist->indexes.cluster);
  547. avist->indexes.ents_allocated = avist->indexes.entry = 0;
  548. }
  549. return res;
  550. }
  551. AVOutputFormat ff_avi_muxer = {
  552. .name = "avi",
  553. .long_name = NULL_IF_CONFIG_SMALL("AVI (Audio Video Interleaved)"),
  554. .mime_type = "video/x-msvideo",
  555. .extensions = "avi",
  556. .priv_data_size = sizeof(AVIContext),
  557. .audio_codec = CONFIG_LIBMP3LAME ? AV_CODEC_ID_MP3 : AV_CODEC_ID_AC3,
  558. .video_codec = AV_CODEC_ID_MPEG4,
  559. .write_header = avi_write_header,
  560. .write_packet = avi_write_packet,
  561. .write_trailer = avi_write_trailer,
  562. .codec_tag = (const AVCodecTag * const []) {
  563. ff_codec_bmp_tags, ff_codec_wav_tags, 0
  564. },
  565. };