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.

685 lines
22KB

  1. /*
  2. * AVI muxer
  3. * Copyright (c) 2000 Fabrice Bellard
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg 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. * FFmpeg 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 FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. //#define DEBUG
  22. #include "avformat.h"
  23. #include "internal.h"
  24. #include "avi.h"
  25. #include "avio_internal.h"
  26. #include "riff.h"
  27. #include "libavformat/avlanguage.h"
  28. #include "libavutil/avstring.h"
  29. #include "libavutil/intreadwrite.h"
  30. #include "libavutil/dict.h"
  31. #include "libavutil/avassert.h"
  32. #include "libavutil/timestamp.h"
  33. /*
  34. * TODO:
  35. * - fill all fields if non streamed (nb_frames for example)
  36. */
  37. typedef struct AVIIentry {
  38. unsigned int flags, pos, len;
  39. } AVIIentry;
  40. #define AVI_INDEX_CLUSTER_SIZE 16384
  41. typedef struct AVIIndex {
  42. int64_t indx_start;
  43. int entry;
  44. int ents_allocated;
  45. AVIIentry** cluster;
  46. } AVIIndex;
  47. typedef struct {
  48. int64_t riff_start, movi_list, odml_list;
  49. int64_t frames_hdr_all;
  50. int riff_id;
  51. } AVIContext;
  52. typedef struct {
  53. int64_t frames_hdr_strm;
  54. int64_t audio_strm_length;
  55. int packet_count;
  56. int entry;
  57. AVIIndex indexes;
  58. } AVIStream ;
  59. static inline AVIIentry* avi_get_ientry(AVIIndex* idx, int ent_id)
  60. {
  61. int cl = ent_id / AVI_INDEX_CLUSTER_SIZE;
  62. int id = ent_id % AVI_INDEX_CLUSTER_SIZE;
  63. return &idx->cluster[cl][id];
  64. }
  65. static int64_t avi_start_new_riff(AVFormatContext *s, AVIOContext *pb,
  66. const char* riff_tag, const char* list_tag)
  67. {
  68. AVIContext *avi= s->priv_data;
  69. int64_t loff;
  70. int i;
  71. avi->riff_id++;
  72. for (i=0; i<s->nb_streams; i++){
  73. AVIStream *avist= s->streams[i]->priv_data;
  74. avist->indexes.entry = 0;
  75. }
  76. avi->riff_start = ff_start_tag(pb, "RIFF");
  77. ffio_wfourcc(pb, riff_tag);
  78. loff = ff_start_tag(pb, "LIST");
  79. ffio_wfourcc(pb, list_tag);
  80. return loff;
  81. }
  82. static char* avi_stream2fourcc(char* tag, int index, enum AVMediaType type)
  83. {
  84. tag[0] = '0' + index/10;
  85. tag[1] = '0' + index%10;
  86. if (type == AVMEDIA_TYPE_VIDEO) {
  87. tag[2] = 'd';
  88. tag[3] = 'c';
  89. } else if (type == AVMEDIA_TYPE_SUBTITLE) {
  90. // note: this is not an official code
  91. tag[2] = 's';
  92. tag[3] = 'b';
  93. } else {
  94. tag[2] = 'w';
  95. tag[3] = 'b';
  96. }
  97. tag[4] = '\0';
  98. return tag;
  99. }
  100. static int avi_write_counters(AVFormatContext* s, int riff_id)
  101. {
  102. AVIOContext *pb = s->pb;
  103. AVIContext *avi = s->priv_data;
  104. int n, au_byterate, au_ssize, au_scale, nb_frames = 0;
  105. int64_t file_size;
  106. AVCodecContext* stream;
  107. file_size = avio_tell(pb);
  108. for(n = 0; n < s->nb_streams; n++) {
  109. AVIStream *avist= s->streams[n]->priv_data;
  110. av_assert0(avist->frames_hdr_strm);
  111. stream = s->streams[n]->codec;
  112. avio_seek(pb, avist->frames_hdr_strm, SEEK_SET);
  113. ff_parse_specific_params(stream, &au_byterate, &au_ssize, &au_scale);
  114. if(au_ssize == 0) {
  115. avio_wl32(pb, avist->packet_count);
  116. } else {
  117. avio_wl32(pb, avist->audio_strm_length / au_ssize);
  118. }
  119. if(stream->codec_type == AVMEDIA_TYPE_VIDEO)
  120. nb_frames = FFMAX(nb_frames, avist->packet_count);
  121. }
  122. if(riff_id == 1) {
  123. av_assert0(avi->frames_hdr_all);
  124. avio_seek(pb, avi->frames_hdr_all, SEEK_SET);
  125. avio_wl32(pb, nb_frames);
  126. }
  127. avio_seek(pb, file_size, SEEK_SET);
  128. return 0;
  129. }
  130. static int avi_write_header(AVFormatContext *s)
  131. {
  132. AVIContext *avi = s->priv_data;
  133. AVIOContext *pb = s->pb;
  134. int bitrate, n, i, nb_frames, au_byterate, au_ssize, au_scale;
  135. AVCodecContext *stream, *video_enc;
  136. int64_t list1, list2, strh, strf;
  137. AVDictionaryEntry *t = NULL;
  138. int padding;
  139. if (s->nb_streams > AVI_MAX_STREAM_COUNT) {
  140. av_log(s, AV_LOG_ERROR, "AVI does not support >%d streams\n",
  141. AVI_MAX_STREAM_COUNT);
  142. return AVERROR(EINVAL);
  143. }
  144. for(n=0;n<s->nb_streams;n++) {
  145. s->streams[n]->priv_data= av_mallocz(sizeof(AVIStream));
  146. if(!s->streams[n]->priv_data)
  147. return AVERROR(ENOMEM);
  148. }
  149. /* header list */
  150. avi->riff_id = 0;
  151. list1 = avi_start_new_riff(s, pb, "AVI ", "hdrl");
  152. /* avi header */
  153. ffio_wfourcc(pb, "avih");
  154. avio_wl32(pb, 14 * 4);
  155. bitrate = 0;
  156. video_enc = NULL;
  157. for(n=0;n<s->nb_streams;n++) {
  158. stream = s->streams[n]->codec;
  159. bitrate += stream->bit_rate;
  160. if (stream->codec_type == AVMEDIA_TYPE_VIDEO)
  161. video_enc = stream;
  162. }
  163. nb_frames = 0;
  164. if(video_enc){
  165. avio_wl32(pb, (uint32_t)(INT64_C(1000000) * video_enc->time_base.num / video_enc->time_base.den));
  166. } else {
  167. avio_wl32(pb, 0);
  168. }
  169. avio_wl32(pb, bitrate / 8); /* XXX: not quite exact */
  170. avio_wl32(pb, 0); /* padding */
  171. if (!pb->seekable)
  172. avio_wl32(pb, AVIF_TRUSTCKTYPE | AVIF_ISINTERLEAVED); /* flags */
  173. else
  174. avio_wl32(pb, AVIF_TRUSTCKTYPE | AVIF_HASINDEX | AVIF_ISINTERLEAVED); /* flags */
  175. avi->frames_hdr_all = avio_tell(pb); /* remember this offset to fill later */
  176. avio_wl32(pb, nb_frames); /* nb frames, filled later */
  177. avio_wl32(pb, 0); /* initial frame */
  178. avio_wl32(pb, s->nb_streams); /* nb streams */
  179. avio_wl32(pb, 1024 * 1024); /* suggested buffer size */
  180. if(video_enc){
  181. avio_wl32(pb, video_enc->width);
  182. avio_wl32(pb, video_enc->height);
  183. } else {
  184. avio_wl32(pb, 0);
  185. avio_wl32(pb, 0);
  186. }
  187. avio_wl32(pb, 0); /* reserved */
  188. avio_wl32(pb, 0); /* reserved */
  189. avio_wl32(pb, 0); /* reserved */
  190. avio_wl32(pb, 0); /* reserved */
  191. /* stream list */
  192. for(i=0;i<n;i++) {
  193. AVIStream *avist= s->streams[i]->priv_data;
  194. list2 = ff_start_tag(pb, "LIST");
  195. ffio_wfourcc(pb, "strl");
  196. stream = s->streams[i]->codec;
  197. /* stream generic header */
  198. strh = ff_start_tag(pb, "strh");
  199. switch(stream->codec_type) {
  200. case AVMEDIA_TYPE_SUBTITLE:
  201. // XSUB subtitles behave like video tracks, other subtitles
  202. // are not (yet) supported.
  203. if (stream->codec_id != AV_CODEC_ID_XSUB) {
  204. av_log(s, AV_LOG_ERROR, "Subtitle streams other than DivX XSUB are not supported by the AVI muxer.\n");
  205. return AVERROR_PATCHWELCOME;
  206. }
  207. case AVMEDIA_TYPE_VIDEO: ffio_wfourcc(pb, "vids"); break;
  208. case AVMEDIA_TYPE_AUDIO: ffio_wfourcc(pb, "auds"); break;
  209. // case AVMEDIA_TYPE_TEXT : ffio_wfourcc(pb, "txts"); break;
  210. case AVMEDIA_TYPE_DATA : ffio_wfourcc(pb, "dats"); break;
  211. }
  212. if(stream->codec_type == AVMEDIA_TYPE_VIDEO ||
  213. stream->codec_id == AV_CODEC_ID_XSUB)
  214. avio_wl32(pb, stream->codec_tag);
  215. else
  216. avio_wl32(pb, 1);
  217. avio_wl32(pb, 0); /* flags */
  218. avio_wl16(pb, 0); /* priority */
  219. avio_wl16(pb, 0); /* language */
  220. avio_wl32(pb, 0); /* initial frame */
  221. ff_parse_specific_params(stream, &au_byterate, &au_ssize, &au_scale);
  222. if ( stream->codec_type == AVMEDIA_TYPE_VIDEO
  223. && stream->codec_id != AV_CODEC_ID_XSUB
  224. && au_byterate > 1000LL*au_scale) {
  225. au_byterate = 600;
  226. au_scale = 1;
  227. }
  228. avpriv_set_pts_info(s->streams[i], 64, au_scale, au_byterate);
  229. if(stream->codec_id == AV_CODEC_ID_XSUB)
  230. au_scale = au_byterate = 0;
  231. avio_wl32(pb, au_scale); /* scale */
  232. avio_wl32(pb, au_byterate); /* rate */
  233. avio_wl32(pb, 0); /* start */
  234. avist->frames_hdr_strm = avio_tell(pb); /* remember this offset to fill later */
  235. if (!pb->seekable)
  236. avio_wl32(pb, AVI_MAX_RIFF_SIZE); /* FIXME: this may be broken, but who cares */
  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(stream->codec_type == AVMEDIA_TYPE_VIDEO)
  241. avio_wl32(pb, 1024 * 1024);
  242. else if(stream->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, stream->width);
  250. avio_wl16(pb, stream->height);
  251. ff_end_tag(pb, strh);
  252. if(stream->codec_type != AVMEDIA_TYPE_DATA){
  253. int ret;
  254. strf = ff_start_tag(pb, "strf");
  255. switch(stream->codec_type) {
  256. case AVMEDIA_TYPE_SUBTITLE:
  257. // XSUB subtitles behave like video tracks, other subtitles
  258. // are not (yet) supported.
  259. if (stream->codec_id != AV_CODEC_ID_XSUB) break;
  260. case AVMEDIA_TYPE_VIDEO:
  261. ff_put_bmp_header(pb, stream, ff_codec_bmp_tags, 0, 0);
  262. break;
  263. case AVMEDIA_TYPE_AUDIO:
  264. if ((ret = ff_put_wav_header(pb, stream)) < 0) {
  265. return ret;
  266. }
  267. break;
  268. default:
  269. av_log(s, AV_LOG_ERROR,
  270. "Invalid or not supported codec type '%s' found in the input\n",
  271. (char *)av_x_if_null(av_get_media_type_string(stream->codec_type), "?"));
  272. return AVERROR(EINVAL);
  273. }
  274. ff_end_tag(pb, strf);
  275. if ((t = av_dict_get(s->streams[i]->metadata, "title", NULL, 0))) {
  276. ff_riff_write_info_tag(s->pb, "strn", t->value);
  277. t = NULL;
  278. }
  279. if(stream->codec_id == AV_CODEC_ID_XSUB
  280. && (t = av_dict_get(s->streams[i]->metadata, "language", NULL, 0))) {
  281. const char* langstr = av_convert_lang_to(t->value, AV_LANG_ISO639_1);
  282. t = NULL;
  283. if (langstr) {
  284. char* str = av_asprintf("Subtitle - %s-xx;02", langstr);
  285. ff_riff_write_info_tag(s->pb, "strn", str);
  286. av_free(str);
  287. }
  288. }
  289. }
  290. if (pb->seekable) {
  291. unsigned char tag[5];
  292. int j;
  293. /* Starting to lay out AVI OpenDML master index.
  294. * We want to make it JUNK entry for now, since we'd
  295. * like to get away without making AVI an OpenDML one
  296. * for compatibility reasons.
  297. */
  298. avist->indexes.entry = avist->indexes.ents_allocated = 0;
  299. avist->indexes.indx_start = ff_start_tag(pb, "JUNK");
  300. avio_wl16(pb, 4); /* wLongsPerEntry */
  301. avio_w8(pb, 0); /* bIndexSubType (0 == frame index) */
  302. avio_w8(pb, 0); /* bIndexType (0 == AVI_INDEX_OF_INDEXES) */
  303. avio_wl32(pb, 0); /* nEntriesInUse (will fill out later on) */
  304. ffio_wfourcc(pb, avi_stream2fourcc(tag, i, stream->codec_type));
  305. /* dwChunkId */
  306. avio_wl64(pb, 0); /* dwReserved[3]
  307. avio_wl32(pb, 0); Must be 0. */
  308. for (j=0; j < AVI_MASTER_INDEX_SIZE * 2; j++)
  309. avio_wl64(pb, 0);
  310. ff_end_tag(pb, avist->indexes.indx_start);
  311. }
  312. if( stream->codec_type == AVMEDIA_TYPE_VIDEO
  313. && s->streams[i]->sample_aspect_ratio.num>0
  314. && s->streams[i]->sample_aspect_ratio.den>0){
  315. int vprp= ff_start_tag(pb, "vprp");
  316. AVRational dar = av_mul_q(s->streams[i]->sample_aspect_ratio,
  317. (AVRational){stream->width, stream->height});
  318. int num, den;
  319. av_reduce(&num, &den, dar.num, dar.den, 0xFFFF);
  320. avio_wl32(pb, 0); //video format = unknown
  321. avio_wl32(pb, 0); //video standard= unknown
  322. avio_wl32(pb, lrintf(1.0/av_q2d(stream->time_base)));
  323. avio_wl32(pb, stream->width );
  324. avio_wl32(pb, stream->height);
  325. avio_wl16(pb, den);
  326. avio_wl16(pb, num);
  327. avio_wl32(pb, stream->width );
  328. avio_wl32(pb, stream->height);
  329. avio_wl32(pb, 1); //progressive FIXME
  330. avio_wl32(pb, stream->height);
  331. avio_wl32(pb, stream->width );
  332. avio_wl32(pb, stream->height);
  333. avio_wl32(pb, stream->width );
  334. avio_wl32(pb, 0);
  335. avio_wl32(pb, 0);
  336. avio_wl32(pb, 0);
  337. avio_wl32(pb, 0);
  338. ff_end_tag(pb, vprp);
  339. }
  340. ff_end_tag(pb, list2);
  341. }
  342. if (pb->seekable) {
  343. /* AVI could become an OpenDML one, if it grows beyond 2Gb range */
  344. avi->odml_list = ff_start_tag(pb, "JUNK");
  345. ffio_wfourcc(pb, "odml");
  346. ffio_wfourcc(pb, "dmlh");
  347. avio_wl32(pb, 248);
  348. for (i = 0; i < 248; i+= 4)
  349. avio_wl32(pb, 0);
  350. ff_end_tag(pb, avi->odml_list);
  351. }
  352. ff_end_tag(pb, list1);
  353. ff_riff_write_info(s);
  354. padding = s->metadata_header_padding;
  355. if (padding < 0)
  356. padding = 1016;
  357. /* some padding for easier tag editing */
  358. if (padding) {
  359. list2 = ff_start_tag(pb, "JUNK");
  360. for (i = padding; i > 0; i -= 4)
  361. avio_wl32(pb, 0);
  362. ff_end_tag(pb, list2);
  363. }
  364. avi->movi_list = ff_start_tag(pb, "LIST");
  365. ffio_wfourcc(pb, "movi");
  366. avio_flush(pb);
  367. return 0;
  368. }
  369. static int avi_write_ix(AVFormatContext *s)
  370. {
  371. AVIOContext *pb = s->pb;
  372. AVIContext *avi = s->priv_data;
  373. char tag[5];
  374. char ix_tag[] = "ix00";
  375. int i, j;
  376. av_assert0(pb->seekable);
  377. if (avi->riff_id > AVI_MASTER_INDEX_SIZE) {
  378. av_log(s, AV_LOG_ERROR, "Invalid riff index %d > %d\n",
  379. avi->riff_id, AVI_MASTER_INDEX_SIZE);
  380. return AVERROR(EINVAL);
  381. }
  382. for (i=0;i<s->nb_streams;i++) {
  383. AVIStream *avist= s->streams[i]->priv_data;
  384. int64_t ix, pos;
  385. avi_stream2fourcc(tag, i, s->streams[i]->codec->codec_type);
  386. ix_tag[3] = '0' + i;
  387. /* Writing AVI OpenDML leaf index chunk */
  388. ix = avio_tell(pb);
  389. ffio_wfourcc(pb, ix_tag); /* ix?? */
  390. avio_wl32(pb, avist->indexes.entry * 8 + 24);
  391. /* chunk size */
  392. avio_wl16(pb, 2); /* wLongsPerEntry */
  393. avio_w8(pb, 0); /* bIndexSubType (0 == frame index) */
  394. avio_w8(pb, 1); /* bIndexType (1 == AVI_INDEX_OF_CHUNKS) */
  395. avio_wl32(pb, avist->indexes.entry);
  396. /* nEntriesInUse */
  397. ffio_wfourcc(pb, tag); /* dwChunkId */
  398. avio_wl64(pb, avi->movi_list);/* qwBaseOffset */
  399. avio_wl32(pb, 0); /* dwReserved_3 (must be 0) */
  400. for (j=0; j<avist->indexes.entry; j++) {
  401. AVIIentry* ie = avi_get_ientry(&avist->indexes, j);
  402. avio_wl32(pb, ie->pos + 8);
  403. avio_wl32(pb, ((uint32_t)ie->len & ~0x80000000) |
  404. (ie->flags & 0x10 ? 0 : 0x80000000));
  405. }
  406. avio_flush(pb);
  407. pos = avio_tell(pb);
  408. /* Updating one entry in the AVI OpenDML master index */
  409. avio_seek(pb, avist->indexes.indx_start - 8, SEEK_SET);
  410. ffio_wfourcc(pb, "indx"); /* enabling this entry */
  411. avio_skip(pb, 8);
  412. avio_wl32(pb, avi->riff_id); /* nEntriesInUse */
  413. avio_skip(pb, 16*avi->riff_id);
  414. avio_wl64(pb, ix); /* qwOffset */
  415. avio_wl32(pb, pos - ix); /* dwSize */
  416. avio_wl32(pb, avist->indexes.entry); /* dwDuration */
  417. avio_seek(pb, pos, SEEK_SET);
  418. }
  419. return 0;
  420. }
  421. static int avi_write_idx1(AVFormatContext *s)
  422. {
  423. AVIOContext *pb = s->pb;
  424. AVIContext *avi = s->priv_data;
  425. int64_t idx_chunk;
  426. int i;
  427. char tag[5];
  428. if (pb->seekable) {
  429. AVIStream *avist;
  430. AVIIentry* ie = 0, *tie;
  431. int empty, stream_id = -1;
  432. idx_chunk = ff_start_tag(pb, "idx1");
  433. for(i=0; i<s->nb_streams; i++){
  434. avist= s->streams[i]->priv_data;
  435. avist->entry=0;
  436. }
  437. do {
  438. empty = 1;
  439. for (i=0; i<s->nb_streams; i++) {
  440. avist= s->streams[i]->priv_data;
  441. if (avist->indexes.entry <= avist->entry)
  442. continue;
  443. tie = avi_get_ientry(&avist->indexes, avist->entry);
  444. if (empty || tie->pos < ie->pos) {
  445. ie = tie;
  446. stream_id = i;
  447. }
  448. empty = 0;
  449. }
  450. if (!empty) {
  451. avist= s->streams[stream_id]->priv_data;
  452. avi_stream2fourcc(tag, stream_id,
  453. s->streams[stream_id]->codec->codec_type);
  454. ffio_wfourcc(pb, tag);
  455. avio_wl32(pb, ie->flags);
  456. avio_wl32(pb, ie->pos);
  457. avio_wl32(pb, ie->len);
  458. avist->entry++;
  459. }
  460. } while (!empty);
  461. ff_end_tag(pb, idx_chunk);
  462. avi_write_counters(s, avi->riff_id);
  463. }
  464. return 0;
  465. }
  466. static int avi_write_packet(AVFormatContext *s, AVPacket *pkt)
  467. {
  468. AVIContext *avi = s->priv_data;
  469. AVIOContext *pb = s->pb;
  470. unsigned char tag[5];
  471. unsigned int flags=0;
  472. const int stream_index= pkt->stream_index;
  473. AVIStream *avist= s->streams[stream_index]->priv_data;
  474. AVCodecContext *enc= s->streams[stream_index]->codec;
  475. int size= pkt->size;
  476. av_dlog(s, "dts:%s packet_count:%d stream_index:%d\n", av_ts2str(pkt->dts), avist->packet_count, stream_index);
  477. while(enc->block_align==0 && pkt->dts != AV_NOPTS_VALUE && pkt->dts > avist->packet_count && enc->codec_id != AV_CODEC_ID_XSUB && avist->packet_count){
  478. AVPacket empty_packet;
  479. if(pkt->dts - avist->packet_count > 60000){
  480. av_log(s, AV_LOG_ERROR, "Too large number of skipped frames %"PRId64" > 60000\n", pkt->dts - avist->packet_count);
  481. return AVERROR(EINVAL);
  482. }
  483. av_init_packet(&empty_packet);
  484. empty_packet.size= 0;
  485. empty_packet.data= NULL;
  486. empty_packet.stream_index= stream_index;
  487. avi_write_packet(s, &empty_packet);
  488. av_dlog(s, "dup dts:%s packet_count:%d\n", av_ts2str(pkt->dts), avist->packet_count);
  489. }
  490. avist->packet_count++;
  491. // Make sure to put an OpenDML chunk when the file size exceeds the limits
  492. if (pb->seekable &&
  493. (avio_tell(pb) - avi->riff_start > AVI_MAX_RIFF_SIZE)) {
  494. avi_write_ix(s);
  495. ff_end_tag(pb, avi->movi_list);
  496. if (avi->riff_id == 1)
  497. avi_write_idx1(s);
  498. ff_end_tag(pb, avi->riff_start);
  499. avi->movi_list = avi_start_new_riff(s, pb, "AVIX", "movi");
  500. }
  501. avi_stream2fourcc(tag, stream_index, enc->codec_type);
  502. if(pkt->flags&AV_PKT_FLAG_KEY)
  503. flags = 0x10;
  504. if (enc->codec_type == AVMEDIA_TYPE_AUDIO) {
  505. avist->audio_strm_length += size;
  506. }
  507. if (s->pb->seekable) {
  508. AVIIndex* idx = &avist->indexes;
  509. int cl = idx->entry / AVI_INDEX_CLUSTER_SIZE;
  510. int id = idx->entry % AVI_INDEX_CLUSTER_SIZE;
  511. if (idx->ents_allocated <= idx->entry) {
  512. idx->cluster = av_realloc_f(idx->cluster, sizeof(void*), cl+1);
  513. if (!idx->cluster) {
  514. idx->ents_allocated = 0;
  515. idx->entry = 0;
  516. return AVERROR(ENOMEM);
  517. }
  518. idx->cluster[cl] = av_malloc(AVI_INDEX_CLUSTER_SIZE*sizeof(AVIIentry));
  519. if (!idx->cluster[cl])
  520. return AVERROR(ENOMEM);
  521. idx->ents_allocated += AVI_INDEX_CLUSTER_SIZE;
  522. }
  523. idx->cluster[cl][id].flags = flags;
  524. idx->cluster[cl][id].pos = avio_tell(pb) - avi->movi_list;
  525. idx->cluster[cl][id].len = size;
  526. idx->entry++;
  527. }
  528. avio_write(pb, tag, 4);
  529. avio_wl32(pb, size);
  530. avio_write(pb, pkt->data, size);
  531. if (size & 1)
  532. avio_w8(pb, 0);
  533. return 0;
  534. }
  535. static int avi_write_trailer(AVFormatContext *s)
  536. {
  537. AVIContext *avi = s->priv_data;
  538. AVIOContext *pb = s->pb;
  539. int res = 0;
  540. int i, j, n, nb_frames;
  541. int64_t file_size;
  542. if (pb->seekable){
  543. if (avi->riff_id == 1) {
  544. ff_end_tag(pb, avi->movi_list);
  545. res = avi_write_idx1(s);
  546. ff_end_tag(pb, avi->riff_start);
  547. } else {
  548. avi_write_ix(s);
  549. ff_end_tag(pb, avi->movi_list);
  550. ff_end_tag(pb, avi->riff_start);
  551. file_size = avio_tell(pb);
  552. avio_seek(pb, avi->odml_list - 8, SEEK_SET);
  553. ffio_wfourcc(pb, "LIST"); /* Making this AVI OpenDML one */
  554. avio_skip(pb, 16);
  555. for (n=nb_frames=0;n<s->nb_streams;n++) {
  556. AVCodecContext *stream = s->streams[n]->codec;
  557. AVIStream *avist= s->streams[n]->priv_data;
  558. if (stream->codec_type == AVMEDIA_TYPE_VIDEO) {
  559. if (nb_frames < avist->packet_count)
  560. nb_frames = avist->packet_count;
  561. } else {
  562. if (stream->codec_id == AV_CODEC_ID_MP2 || stream->codec_id == AV_CODEC_ID_MP3) {
  563. nb_frames += avist->packet_count;
  564. }
  565. }
  566. }
  567. avio_wl32(pb, nb_frames);
  568. avio_seek(pb, file_size, SEEK_SET);
  569. avi_write_counters(s, avi->riff_id);
  570. }
  571. }
  572. for (i=0; i<s->nb_streams; i++) {
  573. AVIStream *avist= s->streams[i]->priv_data;
  574. for (j=0; j<avist->indexes.ents_allocated/AVI_INDEX_CLUSTER_SIZE; j++)
  575. av_freep(&avist->indexes.cluster[j]);
  576. av_freep(&avist->indexes.cluster);
  577. avist->indexes.ents_allocated = avist->indexes.entry = 0;
  578. }
  579. return res;
  580. }
  581. AVOutputFormat ff_avi_muxer = {
  582. .name = "avi",
  583. .long_name = NULL_IF_CONFIG_SMALL("AVI (Audio Video Interleaved)"),
  584. .mime_type = "video/x-msvideo",
  585. .extensions = "avi",
  586. .priv_data_size = sizeof(AVIContext),
  587. .audio_codec = CONFIG_LIBMP3LAME ? AV_CODEC_ID_MP3 : AV_CODEC_ID_AC3,
  588. .video_codec = AV_CODEC_ID_MPEG4,
  589. .write_header = avi_write_header,
  590. .write_packet = avi_write_packet,
  591. .write_trailer = avi_write_trailer,
  592. .codec_tag = (const AVCodecTag* const []){
  593. ff_codec_bmp_tags, ff_codec_wav_tags, 0
  594. },
  595. };