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.

797 lines
27KB

  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. int master_odml_riff_id_base;
  50. AVIIentry** cluster;
  51. } AVIIndex;
  52. typedef struct AVIContext {
  53. int64_t riff_start, movi_list, odml_list;
  54. int64_t frames_hdr_all;
  55. int riff_id;
  56. } AVIContext;
  57. typedef struct AVIStream {
  58. int64_t frames_hdr_strm;
  59. int64_t audio_strm_length;
  60. int packet_count;
  61. int entry;
  62. int max_size;
  63. int sample_requested;
  64. int64_t last_dts;
  65. AVIIndex indexes;
  66. } AVIStream;
  67. static int avi_write_packet(AVFormatContext *s, AVPacket *pkt);
  68. static inline AVIIentry *avi_get_ientry(const AVIIndex *idx, int ent_id)
  69. {
  70. int cl = ent_id / AVI_INDEX_CLUSTER_SIZE;
  71. int id = ent_id % AVI_INDEX_CLUSTER_SIZE;
  72. return &idx->cluster[cl][id];
  73. }
  74. static int64_t avi_start_new_riff(AVFormatContext *s, AVIOContext *pb,
  75. const char *riff_tag, const char *list_tag)
  76. {
  77. AVIContext *avi = s->priv_data;
  78. int64_t loff;
  79. int i;
  80. avi->riff_id++;
  81. for (i = 0; i < s->nb_streams; i++) {
  82. AVIStream *avist = s->streams[i]->priv_data;
  83. avist->indexes.audio_strm_offset = avist->audio_strm_length;
  84. avist->indexes.entry = 0;
  85. }
  86. avi->riff_start = ff_start_tag(pb, "RIFF");
  87. ffio_wfourcc(pb, riff_tag);
  88. loff = ff_start_tag(pb, "LIST");
  89. ffio_wfourcc(pb, list_tag);
  90. return loff;
  91. }
  92. static char *avi_stream2fourcc(char *tag, int index, enum AVMediaType type)
  93. {
  94. tag[0] = '0' + index / 10;
  95. tag[1] = '0' + index % 10;
  96. if (type == AVMEDIA_TYPE_VIDEO) {
  97. tag[2] = 'd';
  98. tag[3] = 'c';
  99. } else if (type == AVMEDIA_TYPE_SUBTITLE) {
  100. // note: this is not an official code
  101. tag[2] = 's';
  102. tag[3] = 'b';
  103. } else {
  104. tag[2] = 'w';
  105. tag[3] = 'b';
  106. }
  107. tag[4] = '\0';
  108. return tag;
  109. }
  110. static int avi_write_counters(AVFormatContext *s, int riff_id)
  111. {
  112. AVIOContext *pb = s->pb;
  113. AVIContext *avi = s->priv_data;
  114. int n, au_byterate, au_ssize, au_scale, nb_frames = 0;
  115. int64_t file_size;
  116. AVCodecContext *stream;
  117. file_size = avio_tell(pb);
  118. for (n = 0; n < s->nb_streams; n++) {
  119. AVIStream *avist = s->streams[n]->priv_data;
  120. av_assert0(avist->frames_hdr_strm);
  121. stream = s->streams[n]->codec;
  122. avio_seek(pb, avist->frames_hdr_strm, SEEK_SET);
  123. ff_parse_specific_params(s->streams[n], &au_byterate, &au_ssize, &au_scale);
  124. if (au_ssize == 0)
  125. avio_wl32(pb, avist->packet_count);
  126. else
  127. avio_wl32(pb, avist->audio_strm_length / au_ssize);
  128. if (stream->codec_type == AVMEDIA_TYPE_VIDEO)
  129. nb_frames = FFMAX(nb_frames, avist->packet_count);
  130. }
  131. if (riff_id == 1) {
  132. av_assert0(avi->frames_hdr_all);
  133. avio_seek(pb, avi->frames_hdr_all, SEEK_SET);
  134. avio_wl32(pb, nb_frames);
  135. }
  136. avio_seek(pb, file_size, SEEK_SET);
  137. return 0;
  138. }
  139. static void write_odml_master(AVFormatContext *s, int stream_index)
  140. {
  141. AVIOContext *pb = s->pb;
  142. AVStream *st = s->streams[stream_index];
  143. AVCodecContext *enc = st->codec;
  144. AVIStream *avist = st->priv_data;
  145. unsigned char tag[5];
  146. int j;
  147. /* Starting to lay out AVI OpenDML master index.
  148. * We want to make it JUNK entry for now, since we'd
  149. * like to get away without making AVI an OpenDML one
  150. * for compatibility reasons. */
  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, int size)
  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 - avist->indexes.master_odml_riff_id_base); /* nEntriesInUse */
  431. avio_skip(pb, 16 * (avi->riff_id - avist->indexes.master_odml_riff_id_base));
  432. avio_wl64(pb, ix); /* qwOffset */
  433. avio_wl32(pb, size); /* 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. for (i = 0; i < s->nb_streams; i++) {
  455. AVIStream *avist = s->streams[i]->priv_data;
  456. if (avi->riff_id - avist->indexes.master_odml_riff_id_base == AVI_MASTER_INDEX_SIZE) {
  457. int64_t pos;
  458. int size = 8+2+1+1+4+8+4+4+16*AVI_MASTER_INDEX_SIZE;
  459. pos = avio_tell(pb);
  460. update_odml_entry(s, i, pos, size);
  461. write_odml_master(s, i);
  462. av_assert1(avio_tell(pb) - pos == size);
  463. avist->indexes.master_odml_riff_id_base = avi->riff_id - 1;
  464. }
  465. av_assert0(avi->riff_id - avist->indexes.master_odml_riff_id_base < AVI_MASTER_INDEX_SIZE);
  466. }
  467. for (i = 0; i < s->nb_streams; i++) {
  468. AVIStream *avist = s->streams[i]->priv_data;
  469. int64_t ix;
  470. avi_stream2fourcc(tag, i, s->streams[i]->codec->codec_type);
  471. ix_tag[3] = '0' + i;
  472. /* Writing AVI OpenDML leaf index chunk */
  473. ix = avio_tell(pb);
  474. ffio_wfourcc(pb, ix_tag); /* ix?? */
  475. avio_wl32(pb, avist->indexes.entry * 8 + 24);
  476. /* chunk size */
  477. avio_wl16(pb, 2); /* wLongsPerEntry */
  478. avio_w8(pb, 0); /* bIndexSubType (0 == frame index) */
  479. avio_w8(pb, 1); /* bIndexType (1 == AVI_INDEX_OF_CHUNKS) */
  480. avio_wl32(pb, avist->indexes.entry);
  481. /* nEntriesInUse */
  482. ffio_wfourcc(pb, tag); /* dwChunkId */
  483. avio_wl64(pb, avi->movi_list); /* qwBaseOffset */
  484. avio_wl32(pb, 0); /* dwReserved_3 (must be 0) */
  485. for (j = 0; j < avist->indexes.entry; j++) {
  486. AVIIentry *ie = avi_get_ientry(&avist->indexes, j);
  487. avio_wl32(pb, ie->pos + 8);
  488. avio_wl32(pb, ((uint32_t) ie->len & ~0x80000000) |
  489. (ie->flags & 0x10 ? 0 : 0x80000000));
  490. }
  491. update_odml_entry(s, i, ix, avio_tell(pb) - ix);
  492. }
  493. return 0;
  494. }
  495. static int avi_write_idx1(AVFormatContext *s)
  496. {
  497. AVIOContext *pb = s->pb;
  498. AVIContext *avi = s->priv_data;
  499. int64_t idx_chunk;
  500. int i;
  501. char tag[5];
  502. if (pb->seekable) {
  503. AVIStream *avist;
  504. AVIIentry *ie = 0, *tie;
  505. int empty, stream_id = -1;
  506. idx_chunk = ff_start_tag(pb, "idx1");
  507. for (i = 0; i < s->nb_streams; i++) {
  508. avist = s->streams[i]->priv_data;
  509. avist->entry = 0;
  510. }
  511. do {
  512. empty = 1;
  513. for (i = 0; i < s->nb_streams; i++) {
  514. avist = s->streams[i]->priv_data;
  515. if (avist->indexes.entry <= avist->entry)
  516. continue;
  517. tie = avi_get_ientry(&avist->indexes, avist->entry);
  518. if (empty || tie->pos < ie->pos) {
  519. ie = tie;
  520. stream_id = i;
  521. }
  522. empty = 0;
  523. }
  524. if (!empty) {
  525. avist = s->streams[stream_id]->priv_data;
  526. avi_stream2fourcc(tag, stream_id,
  527. s->streams[stream_id]->codec->codec_type);
  528. ffio_wfourcc(pb, tag);
  529. avio_wl32(pb, ie->flags);
  530. avio_wl32(pb, ie->pos);
  531. avio_wl32(pb, ie->len);
  532. avist->entry++;
  533. }
  534. } while (!empty);
  535. ff_end_tag(pb, idx_chunk);
  536. avi_write_counters(s, avi->riff_id);
  537. }
  538. return 0;
  539. }
  540. static int write_skip_frames(AVFormatContext *s, int stream_index, int64_t dts)
  541. {
  542. AVIStream *avist = s->streams[stream_index]->priv_data;
  543. AVCodecContext *enc = s->streams[stream_index]->codec;
  544. av_dlog(s, "dts:%s packet_count:%d stream_index:%d\n", av_ts2str(dts), avist->packet_count, stream_index);
  545. while (enc->block_align == 0 && dts != AV_NOPTS_VALUE &&
  546. dts > avist->packet_count && enc->codec_id != AV_CODEC_ID_XSUB && avist->packet_count) {
  547. AVPacket empty_packet;
  548. if (dts - avist->packet_count > 60000) {
  549. av_log(s, AV_LOG_ERROR, "Too large number of skipped frames %"PRId64" > 60000\n", dts - avist->packet_count);
  550. return AVERROR(EINVAL);
  551. }
  552. av_init_packet(&empty_packet);
  553. empty_packet.size = 0;
  554. empty_packet.data = NULL;
  555. empty_packet.stream_index = stream_index;
  556. avi_write_packet(s, &empty_packet);
  557. av_dlog(s, "dup dts:%s packet_count:%d\n", av_ts2str(dts), avist->packet_count);
  558. }
  559. return 0;
  560. }
  561. static int avi_write_packet(AVFormatContext *s, AVPacket *pkt)
  562. {
  563. unsigned char tag[5];
  564. unsigned int flags = 0;
  565. const int stream_index = pkt->stream_index;
  566. int size = pkt->size;
  567. AVIContext *avi = s->priv_data;
  568. AVIOContext *pb = s->pb;
  569. AVIStream *avist = s->streams[stream_index]->priv_data;
  570. AVCodecContext *enc = s->streams[stream_index]->codec;
  571. int ret;
  572. if (enc->codec_id == AV_CODEC_ID_H264 && enc->codec_tag == MKTAG('H','2','6','4') && pkt->size) {
  573. ret = ff_check_h264_startcode(s, s->streams[stream_index], pkt);
  574. if (ret < 0)
  575. return ret;
  576. }
  577. if ((ret = write_skip_frames(s, stream_index, pkt->dts)) < 0)
  578. return ret;
  579. if (pkt->dts != AV_NOPTS_VALUE)
  580. avist->last_dts = pkt->dts + pkt->duration;
  581. avist->packet_count++;
  582. // Make sure to put an OpenDML chunk when the file size exceeds the limits
  583. if (pb->seekable &&
  584. (avio_tell(pb) - avi->riff_start > AVI_MAX_RIFF_SIZE)) {
  585. avi_write_ix(s);
  586. ff_end_tag(pb, avi->movi_list);
  587. if (avi->riff_id == 1)
  588. avi_write_idx1(s);
  589. ff_end_tag(pb, avi->riff_start);
  590. avi->movi_list = avi_start_new_riff(s, pb, "AVIX", "movi");
  591. }
  592. avi_stream2fourcc(tag, stream_index, enc->codec_type);
  593. if (pkt->flags & AV_PKT_FLAG_KEY)
  594. flags = 0x10;
  595. if (enc->codec_type == AVMEDIA_TYPE_AUDIO)
  596. avist->audio_strm_length += size;
  597. if (s->pb->seekable) {
  598. AVIIndex *idx = &avist->indexes;
  599. int cl = idx->entry / AVI_INDEX_CLUSTER_SIZE;
  600. int id = idx->entry % AVI_INDEX_CLUSTER_SIZE;
  601. if (idx->ents_allocated <= idx->entry) {
  602. idx->cluster = av_realloc_f(idx->cluster, sizeof(void*), cl+1);
  603. if (!idx->cluster) {
  604. idx->ents_allocated = 0;
  605. idx->entry = 0;
  606. return AVERROR(ENOMEM);
  607. }
  608. idx->cluster[cl] =
  609. av_malloc(AVI_INDEX_CLUSTER_SIZE * sizeof(AVIIentry));
  610. if (!idx->cluster[cl])
  611. return AVERROR(ENOMEM);
  612. idx->ents_allocated += AVI_INDEX_CLUSTER_SIZE;
  613. }
  614. idx->cluster[cl][id].flags = flags;
  615. idx->cluster[cl][id].pos = avio_tell(pb) - avi->movi_list;
  616. idx->cluster[cl][id].len = size;
  617. avist->max_size = FFMAX(avist->max_size, size);
  618. idx->entry++;
  619. }
  620. avio_write(pb, tag, 4);
  621. avio_wl32(pb, size);
  622. avio_write(pb, pkt->data, size);
  623. if (size & 1)
  624. avio_w8(pb, 0);
  625. return 0;
  626. }
  627. static int avi_write_trailer(AVFormatContext *s)
  628. {
  629. AVIContext *avi = s->priv_data;
  630. AVIOContext *pb = s->pb;
  631. int res = 0;
  632. int i, j, n, nb_frames;
  633. int64_t file_size;
  634. for (i = 0; i < s->nb_streams; i++) {
  635. AVIStream *avist = s->streams[i]->priv_data;
  636. write_skip_frames(s, i, avist->last_dts);
  637. }
  638. if (pb->seekable) {
  639. if (avi->riff_id == 1) {
  640. ff_end_tag(pb, avi->movi_list);
  641. res = avi_write_idx1(s);
  642. ff_end_tag(pb, avi->riff_start);
  643. } else {
  644. avi_write_ix(s);
  645. ff_end_tag(pb, avi->movi_list);
  646. ff_end_tag(pb, avi->riff_start);
  647. file_size = avio_tell(pb);
  648. avio_seek(pb, avi->odml_list - 8, SEEK_SET);
  649. ffio_wfourcc(pb, "LIST"); /* Making this AVI OpenDML one */
  650. avio_skip(pb, 16);
  651. for (n = nb_frames = 0; n < s->nb_streams; n++) {
  652. AVCodecContext *stream = s->streams[n]->codec;
  653. AVIStream *avist = s->streams[n]->priv_data;
  654. if (stream->codec_type == AVMEDIA_TYPE_VIDEO) {
  655. if (nb_frames < avist->packet_count)
  656. nb_frames = avist->packet_count;
  657. } else {
  658. if (stream->codec_id == AV_CODEC_ID_MP2 ||
  659. stream->codec_id == AV_CODEC_ID_MP3)
  660. nb_frames += avist->packet_count;
  661. }
  662. }
  663. avio_wl32(pb, nb_frames);
  664. avio_seek(pb, file_size, SEEK_SET);
  665. avi_write_counters(s, avi->riff_id);
  666. }
  667. }
  668. for (i = 0; i < s->nb_streams; i++) {
  669. AVIStream *avist = s->streams[i]->priv_data;
  670. for (j = 0; j < avist->indexes.ents_allocated / AVI_INDEX_CLUSTER_SIZE; j++)
  671. av_freep(&avist->indexes.cluster[j]);
  672. av_freep(&avist->indexes.cluster);
  673. avist->indexes.ents_allocated = avist->indexes.entry = 0;
  674. if (pb->seekable) {
  675. avio_seek(pb, avist->frames_hdr_strm + 4, SEEK_SET);
  676. avio_wl32(pb, avist->max_size);
  677. }
  678. }
  679. return res;
  680. }
  681. AVOutputFormat ff_avi_muxer = {
  682. .name = "avi",
  683. .long_name = NULL_IF_CONFIG_SMALL("AVI (Audio Video Interleaved)"),
  684. .mime_type = "video/x-msvideo",
  685. .extensions = "avi",
  686. .priv_data_size = sizeof(AVIContext),
  687. .audio_codec = CONFIG_LIBMP3LAME ? AV_CODEC_ID_MP3 : AV_CODEC_ID_AC3,
  688. .video_codec = AV_CODEC_ID_MPEG4,
  689. .write_header = avi_write_header,
  690. .write_packet = avi_write_packet,
  691. .write_trailer = avi_write_trailer,
  692. .codec_tag = (const AVCodecTag * const []) {
  693. ff_codec_bmp_tags, ff_codec_wav_tags, 0
  694. },
  695. };