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.

724 lines
24KB

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