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.

695 lines
23KB

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