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.

788 lines
26KB

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