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 *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. AVCodecContext *codec = s->streams[n]->codec;
  152. bitrate += codec->bit_rate;
  153. if (codec->codec_type == AVMEDIA_TYPE_VIDEO)
  154. video_enc = codec;
  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. AVStream *st = s->streams[i];
  187. AVCodecContext *enc = st->codec;
  188. AVIStream *avist = st->priv_data;
  189. list2 = ff_start_tag(pb, "LIST");
  190. ffio_wfourcc(pb, "strl");
  191. /* stream generic header */
  192. strh = ff_start_tag(pb, "strh");
  193. switch (enc->codec_type) {
  194. case AVMEDIA_TYPE_SUBTITLE:
  195. // XSUB subtitles behave like video tracks, other subtitles
  196. // are not (yet) supported.
  197. if (enc->codec_id != AV_CODEC_ID_XSUB) {
  198. av_log(s, AV_LOG_ERROR,
  199. "Subtitle streams other than DivX XSUB are not supported by the AVI muxer.\n");
  200. return AVERROR_PATCHWELCOME;
  201. }
  202. case AVMEDIA_TYPE_VIDEO:
  203. ffio_wfourcc(pb, "vids");
  204. break;
  205. case AVMEDIA_TYPE_AUDIO:
  206. ffio_wfourcc(pb, "auds");
  207. break;
  208. // case AVMEDIA_TYPE_TEXT:
  209. // ffio_wfourcc(pb, "txts");
  210. // break;
  211. case AVMEDIA_TYPE_DATA:
  212. ffio_wfourcc(pb, "dats");
  213. break;
  214. }
  215. if (enc->codec_type == AVMEDIA_TYPE_VIDEO ||
  216. enc->codec_id == AV_CODEC_ID_XSUB)
  217. avio_wl32(pb, enc->codec_tag);
  218. else
  219. avio_wl32(pb, 1);
  220. avio_wl32(pb, 0); /* flags */
  221. avio_wl16(pb, 0); /* priority */
  222. avio_wl16(pb, 0); /* language */
  223. avio_wl32(pb, 0); /* initial frame */
  224. ff_parse_specific_params(enc, &au_byterate, &au_ssize, &au_scale);
  225. avio_wl32(pb, au_scale); /* scale */
  226. avio_wl32(pb, au_byterate); /* rate */
  227. avpriv_set_pts_info(st, 64, au_scale, au_byterate);
  228. avio_wl32(pb, 0); /* start */
  229. /* remember this offset to fill later */
  230. avist->frames_hdr_strm = avio_tell(pb);
  231. if (!pb->seekable)
  232. /* FIXME: this may be broken, but who cares */
  233. avio_wl32(pb, AVI_MAX_RIFF_SIZE);
  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 (enc->codec_type == AVMEDIA_TYPE_VIDEO)
  238. avio_wl32(pb, 1024 * 1024);
  239. else if (enc->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, enc->width);
  247. avio_wl16(pb, enc->height);
  248. ff_end_tag(pb, strh);
  249. if (enc->codec_type != AVMEDIA_TYPE_DATA) {
  250. strf = ff_start_tag(pb, "strf");
  251. switch (enc->codec_type) {
  252. case AVMEDIA_TYPE_SUBTITLE:
  253. /* XSUB subtitles behave like video tracks, other subtitles
  254. * are not (yet) supported. */
  255. if (enc->codec_id != AV_CODEC_ID_XSUB)
  256. break;
  257. case AVMEDIA_TYPE_VIDEO:
  258. ff_put_bmp_header(pb, enc, ff_codec_bmp_tags, 0);
  259. break;
  260. case AVMEDIA_TYPE_AUDIO:
  261. if (ff_put_wav_header(pb, enc) < 0)
  262. return -1;
  263. break;
  264. default:
  265. return -1;
  266. }
  267. ff_end_tag(pb, strf);
  268. if ((t = av_dict_get(st->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. avist->indexes.entry = avist->indexes.ents_allocated = 0;
  281. avist->indexes.indx_start = ff_start_tag(pb, "JUNK");
  282. avio_wl16(pb, 4); /* wLongsPerEntry */
  283. avio_w8(pb, 0); /* bIndexSubType (0 == frame index) */
  284. avio_w8(pb, 0); /* bIndexType (0 == AVI_INDEX_OF_INDEXES) */
  285. avio_wl32(pb, 0); /* nEntriesInUse (will fill out later on) */
  286. ffio_wfourcc(pb, avi_stream2fourcc(tag, i, enc->codec_type));
  287. /* dwChunkId */
  288. avio_wl64(pb, 0); /* dwReserved[3] */
  289. // avio_wl32(pb, 0); /* Must be 0. */
  290. for (j = 0; j < AVI_MASTER_INDEX_SIZE * 2; j++)
  291. avio_wl64(pb, 0);
  292. ff_end_tag(pb, avist->indexes.indx_start);
  293. }
  294. if (enc->codec_type == AVMEDIA_TYPE_VIDEO &&
  295. st->sample_aspect_ratio.num > 0 &&
  296. st->sample_aspect_ratio.den > 0) {
  297. int vprp = ff_start_tag(pb, "vprp");
  298. AVRational dar = av_mul_q(st->sample_aspect_ratio,
  299. (AVRational) { enc->width,
  300. enc->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(enc->time_base)));
  306. avio_wl32(pb, enc->width);
  307. avio_wl32(pb, enc->height);
  308. avio_wl16(pb, den);
  309. avio_wl16(pb, num);
  310. avio_wl32(pb, enc->width);
  311. avio_wl32(pb, enc->height);
  312. avio_wl32(pb, 1); // progressive FIXME
  313. avio_wl32(pb, enc->height);
  314. avio_wl32(pb, enc->width);
  315. avio_wl32(pb, enc->height);
  316. avio_wl32(pb, enc->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. assert(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. unsigned char tag[5];
  444. unsigned int flags = 0;
  445. const int stream_index = pkt->stream_index;
  446. int size = pkt->size;
  447. AVIContext *avi = s->priv_data;
  448. AVIOContext *pb = s->pb;
  449. AVIStream *avist = s->streams[stream_index]->priv_data;
  450. AVCodecContext *enc = s->streams[stream_index]->codec;
  451. while (enc->block_align == 0 && pkt->dts != AV_NOPTS_VALUE &&
  452. pkt->dts > avist->packet_count) {
  453. AVPacket empty_packet;
  454. av_init_packet(&empty_packet);
  455. empty_packet.size = 0;
  456. empty_packet.data = NULL;
  457. empty_packet.stream_index = stream_index;
  458. avi_write_packet(s, &empty_packet);
  459. }
  460. avist->packet_count++;
  461. // Make sure to put an OpenDML chunk when the file size exceeds the limits
  462. if (pb->seekable &&
  463. (avio_tell(pb) - avi->riff_start > AVI_MAX_RIFF_SIZE)) {
  464. avi_write_ix(s);
  465. ff_end_tag(pb, avi->movi_list);
  466. if (avi->riff_id == 1)
  467. avi_write_idx1(s);
  468. ff_end_tag(pb, avi->riff_start);
  469. avi->movi_list = avi_start_new_riff(s, pb, "AVIX", "movi");
  470. }
  471. avi_stream2fourcc(tag, stream_index, enc->codec_type);
  472. if (pkt->flags & AV_PKT_FLAG_KEY)
  473. flags = 0x10;
  474. if (enc->codec_type == AVMEDIA_TYPE_AUDIO)
  475. avist->audio_strm_length += size;
  476. if (s->pb->seekable) {
  477. int err;
  478. AVIIndex *idx = &avist->indexes;
  479. int cl = idx->entry / AVI_INDEX_CLUSTER_SIZE;
  480. int id = idx->entry % AVI_INDEX_CLUSTER_SIZE;
  481. if (idx->ents_allocated <= idx->entry) {
  482. if ((err = av_reallocp(&idx->cluster,
  483. (cl + 1) * sizeof(*idx->cluster))) < 0) {
  484. idx->ents_allocated = 0;
  485. idx->entry = 0;
  486. return err;
  487. }
  488. idx->cluster[cl] =
  489. av_malloc(AVI_INDEX_CLUSTER_SIZE * sizeof(AVIIentry));
  490. if (!idx->cluster[cl])
  491. return -1;
  492. idx->ents_allocated += AVI_INDEX_CLUSTER_SIZE;
  493. }
  494. idx->cluster[cl][id].flags = flags;
  495. idx->cluster[cl][id].pos = avio_tell(pb) - avi->movi_list;
  496. idx->cluster[cl][id].len = size;
  497. idx->entry++;
  498. }
  499. avio_write(pb, tag, 4);
  500. avio_wl32(pb, size);
  501. avio_write(pb, pkt->data, size);
  502. if (size & 1)
  503. avio_w8(pb, 0);
  504. return 0;
  505. }
  506. static int avi_write_trailer(AVFormatContext *s)
  507. {
  508. AVIContext *avi = s->priv_data;
  509. AVIOContext *pb = s->pb;
  510. int res = 0;
  511. int i, j, n, nb_frames;
  512. int64_t file_size;
  513. if (pb->seekable) {
  514. if (avi->riff_id == 1) {
  515. ff_end_tag(pb, avi->movi_list);
  516. res = avi_write_idx1(s);
  517. ff_end_tag(pb, avi->riff_start);
  518. } else {
  519. avi_write_ix(s);
  520. ff_end_tag(pb, avi->movi_list);
  521. ff_end_tag(pb, avi->riff_start);
  522. file_size = avio_tell(pb);
  523. avio_seek(pb, avi->odml_list - 8, SEEK_SET);
  524. ffio_wfourcc(pb, "LIST"); /* Making this AVI OpenDML one */
  525. avio_skip(pb, 16);
  526. for (n = nb_frames = 0; n < s->nb_streams; n++) {
  527. AVCodecContext *stream = s->streams[n]->codec;
  528. AVIStream *avist = s->streams[n]->priv_data;
  529. if (stream->codec_type == AVMEDIA_TYPE_VIDEO) {
  530. if (nb_frames < avist->packet_count)
  531. nb_frames = avist->packet_count;
  532. } else {
  533. if (stream->codec_id == AV_CODEC_ID_MP2 ||
  534. stream->codec_id == AV_CODEC_ID_MP3)
  535. nb_frames += avist->packet_count;
  536. }
  537. }
  538. avio_wl32(pb, nb_frames);
  539. avio_seek(pb, file_size, SEEK_SET);
  540. avi_write_counters(s, avi->riff_id);
  541. }
  542. }
  543. for (i = 0; i < s->nb_streams; i++) {
  544. AVIStream *avist = s->streams[i]->priv_data;
  545. for (j = 0; j < avist->indexes.ents_allocated / AVI_INDEX_CLUSTER_SIZE; j++)
  546. av_free(avist->indexes.cluster[j]);
  547. av_freep(&avist->indexes.cluster);
  548. avist->indexes.ents_allocated = avist->indexes.entry = 0;
  549. }
  550. return res;
  551. }
  552. AVOutputFormat ff_avi_muxer = {
  553. .name = "avi",
  554. .long_name = NULL_IF_CONFIG_SMALL("AVI (Audio Video Interleaved)"),
  555. .mime_type = "video/x-msvideo",
  556. .extensions = "avi",
  557. .priv_data_size = sizeof(AVIContext),
  558. .audio_codec = CONFIG_LIBMP3LAME ? AV_CODEC_ID_MP3 : AV_CODEC_ID_AC3,
  559. .video_codec = AV_CODEC_ID_MPEG4,
  560. .write_header = avi_write_header,
  561. .write_packet = avi_write_packet,
  562. .write_trailer = avi_write_trailer,
  563. .codec_tag = (const AVCodecTag * const []) {
  564. ff_codec_bmp_tags, ff_codec_wav_tags, 0
  565. },
  566. };