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.

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