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.

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