avienc.c 21 KB

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