avienc.c 21 KB

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