avienc.c 21 KB

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