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.

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