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.

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