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.

635 lines
20KB

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