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.

799 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. if (!str)
  351. return AVERROR(ENOMEM);
  352. ff_riff_write_info_tag(s->pb, "strn", str);
  353. av_free(str);
  354. }
  355. }
  356. }
  357. if (pb->seekable) {
  358. write_odml_master(s, i);
  359. }
  360. if (enc->codec_type == AVMEDIA_TYPE_VIDEO &&
  361. st->sample_aspect_ratio.num > 0 &&
  362. st->sample_aspect_ratio.den > 0) {
  363. int vprp = ff_start_tag(pb, "vprp");
  364. AVRational dar = av_mul_q(st->sample_aspect_ratio,
  365. (AVRational) { enc->width,
  366. enc->height });
  367. int num, den;
  368. av_reduce(&num, &den, dar.num, dar.den, 0xFFFF);
  369. avio_wl32(pb, 0); // video format = unknown
  370. avio_wl32(pb, 0); // video standard = unknown
  371. // TODO: should be avg_frame_rate
  372. avio_wl32(pb, (2LL*st->time_base.den + st->time_base.num - 1) / (2LL * st->time_base.num));
  373. avio_wl32(pb, enc->width);
  374. avio_wl32(pb, enc->height);
  375. avio_wl16(pb, den);
  376. avio_wl16(pb, num);
  377. avio_wl32(pb, enc->width);
  378. avio_wl32(pb, enc->height);
  379. avio_wl32(pb, 1); // progressive FIXME
  380. avio_wl32(pb, enc->height);
  381. avio_wl32(pb, enc->width);
  382. avio_wl32(pb, enc->height);
  383. avio_wl32(pb, enc->width);
  384. avio_wl32(pb, 0);
  385. avio_wl32(pb, 0);
  386. avio_wl32(pb, 0);
  387. avio_wl32(pb, 0);
  388. ff_end_tag(pb, vprp);
  389. }
  390. ff_end_tag(pb, list2);
  391. }
  392. if (pb->seekable) {
  393. /* AVI could become an OpenDML one, if it grows beyond 2Gb range */
  394. avi->odml_list = ff_start_tag(pb, "JUNK");
  395. ffio_wfourcc(pb, "odml");
  396. ffio_wfourcc(pb, "dmlh");
  397. avio_wl32(pb, 248);
  398. for (i = 0; i < 248; i += 4)
  399. avio_wl32(pb, 0);
  400. ff_end_tag(pb, avi->odml_list);
  401. }
  402. ff_end_tag(pb, list1);
  403. ff_riff_write_info(s);
  404. padding = s->metadata_header_padding;
  405. if (padding < 0)
  406. padding = 1016;
  407. /* some padding for easier tag editing */
  408. if (padding) {
  409. list2 = ff_start_tag(pb, "JUNK");
  410. for (i = padding; i > 0; i -= 4)
  411. avio_wl32(pb, 0);
  412. ff_end_tag(pb, list2);
  413. }
  414. avi->movi_list = ff_start_tag(pb, "LIST");
  415. ffio_wfourcc(pb, "movi");
  416. avio_flush(pb);
  417. return 0;
  418. }
  419. static void update_odml_entry(AVFormatContext *s, int stream_index, int64_t ix, int size)
  420. {
  421. AVIOContext *pb = s->pb;
  422. AVIContext *avi = s->priv_data;
  423. AVIStream *avist = s->streams[stream_index]->priv_data;
  424. int64_t pos;
  425. int au_byterate, au_ssize, au_scale;
  426. avio_flush(pb);
  427. pos = avio_tell(pb);
  428. /* Updating one entry in the AVI OpenDML master index */
  429. avio_seek(pb, avist->indexes.indx_start - 8, SEEK_SET);
  430. ffio_wfourcc(pb, "indx"); /* enabling this entry */
  431. avio_skip(pb, 8);
  432. avio_wl32(pb, avi->riff_id - avist->indexes.master_odml_riff_id_base); /* nEntriesInUse */
  433. avio_skip(pb, 16 * (avi->riff_id - avist->indexes.master_odml_riff_id_base));
  434. avio_wl64(pb, ix); /* qwOffset */
  435. avio_wl32(pb, size); /* dwSize */
  436. ff_parse_specific_params(s->streams[stream_index], &au_byterate, &au_ssize, &au_scale);
  437. if (s->streams[stream_index]->codec->codec_type == AVMEDIA_TYPE_AUDIO && au_ssize > 0) {
  438. uint32_t audio_segm_size = (avist->audio_strm_length - avist->indexes.audio_strm_offset);
  439. if ((audio_segm_size % au_ssize > 0) && !avist->sample_requested) {
  440. avpriv_request_sample(s, "OpenDML index duration for audio packets with partial frames");
  441. avist->sample_requested = 1;
  442. }
  443. avio_wl32(pb, audio_segm_size / au_ssize); /* dwDuration (sample count) */
  444. } else
  445. avio_wl32(pb, avist->indexes.entry); /* dwDuration (packet count) */
  446. avio_seek(pb, pos, SEEK_SET);
  447. }
  448. static int avi_write_ix(AVFormatContext *s)
  449. {
  450. AVIOContext *pb = s->pb;
  451. AVIContext *avi = s->priv_data;
  452. char tag[5];
  453. char ix_tag[] = "ix00";
  454. int i, j;
  455. av_assert0(pb->seekable);
  456. for (i = 0; i < s->nb_streams; i++) {
  457. AVIStream *avist = s->streams[i]->priv_data;
  458. if (avi->riff_id - avist->indexes.master_odml_riff_id_base == AVI_MASTER_INDEX_SIZE) {
  459. int64_t pos;
  460. int size = 8+2+1+1+4+8+4+4+16*AVI_MASTER_INDEX_SIZE;
  461. pos = avio_tell(pb);
  462. update_odml_entry(s, i, pos, size);
  463. write_odml_master(s, i);
  464. av_assert1(avio_tell(pb) - pos == size);
  465. avist->indexes.master_odml_riff_id_base = avi->riff_id - 1;
  466. }
  467. av_assert0(avi->riff_id - avist->indexes.master_odml_riff_id_base < AVI_MASTER_INDEX_SIZE);
  468. }
  469. for (i = 0; i < s->nb_streams; i++) {
  470. AVIStream *avist = s->streams[i]->priv_data;
  471. int64_t ix;
  472. avi_stream2fourcc(tag, i, s->streams[i]->codec->codec_type);
  473. ix_tag[3] = '0' + i;
  474. /* Writing AVI OpenDML leaf index chunk */
  475. ix = avio_tell(pb);
  476. ffio_wfourcc(pb, ix_tag); /* ix?? */
  477. avio_wl32(pb, avist->indexes.entry * 8 + 24);
  478. /* chunk size */
  479. avio_wl16(pb, 2); /* wLongsPerEntry */
  480. avio_w8(pb, 0); /* bIndexSubType (0 == frame index) */
  481. avio_w8(pb, 1); /* bIndexType (1 == AVI_INDEX_OF_CHUNKS) */
  482. avio_wl32(pb, avist->indexes.entry);
  483. /* nEntriesInUse */
  484. ffio_wfourcc(pb, tag); /* dwChunkId */
  485. avio_wl64(pb, avi->movi_list); /* qwBaseOffset */
  486. avio_wl32(pb, 0); /* dwReserved_3 (must be 0) */
  487. for (j = 0; j < avist->indexes.entry; j++) {
  488. AVIIentry *ie = avi_get_ientry(&avist->indexes, j);
  489. avio_wl32(pb, ie->pos + 8);
  490. avio_wl32(pb, ((uint32_t) ie->len & ~0x80000000) |
  491. (ie->flags & 0x10 ? 0 : 0x80000000));
  492. }
  493. update_odml_entry(s, i, ix, avio_tell(pb) - ix);
  494. }
  495. return 0;
  496. }
  497. static int avi_write_idx1(AVFormatContext *s)
  498. {
  499. AVIOContext *pb = s->pb;
  500. AVIContext *avi = s->priv_data;
  501. int64_t idx_chunk;
  502. int i;
  503. char tag[5];
  504. if (pb->seekable) {
  505. AVIStream *avist;
  506. AVIIentry *ie = 0, *tie;
  507. int empty, stream_id = -1;
  508. idx_chunk = ff_start_tag(pb, "idx1");
  509. for (i = 0; i < s->nb_streams; i++) {
  510. avist = s->streams[i]->priv_data;
  511. avist->entry = 0;
  512. }
  513. do {
  514. empty = 1;
  515. for (i = 0; i < s->nb_streams; i++) {
  516. avist = s->streams[i]->priv_data;
  517. if (avist->indexes.entry <= avist->entry)
  518. continue;
  519. tie = avi_get_ientry(&avist->indexes, avist->entry);
  520. if (empty || tie->pos < ie->pos) {
  521. ie = tie;
  522. stream_id = i;
  523. }
  524. empty = 0;
  525. }
  526. if (!empty) {
  527. avist = s->streams[stream_id]->priv_data;
  528. avi_stream2fourcc(tag, stream_id,
  529. s->streams[stream_id]->codec->codec_type);
  530. ffio_wfourcc(pb, tag);
  531. avio_wl32(pb, ie->flags);
  532. avio_wl32(pb, ie->pos);
  533. avio_wl32(pb, ie->len);
  534. avist->entry++;
  535. }
  536. } while (!empty);
  537. ff_end_tag(pb, idx_chunk);
  538. avi_write_counters(s, avi->riff_id);
  539. }
  540. return 0;
  541. }
  542. static int write_skip_frames(AVFormatContext *s, int stream_index, int64_t dts)
  543. {
  544. AVIStream *avist = s->streams[stream_index]->priv_data;
  545. AVCodecContext *enc = s->streams[stream_index]->codec;
  546. av_dlog(s, "dts:%s packet_count:%d stream_index:%d\n", av_ts2str(dts), avist->packet_count, stream_index);
  547. while (enc->block_align == 0 && dts != AV_NOPTS_VALUE &&
  548. dts > avist->packet_count && enc->codec_id != AV_CODEC_ID_XSUB && avist->packet_count) {
  549. AVPacket empty_packet;
  550. if (dts - avist->packet_count > 60000) {
  551. av_log(s, AV_LOG_ERROR, "Too large number of skipped frames %"PRId64" > 60000\n", dts - avist->packet_count);
  552. return AVERROR(EINVAL);
  553. }
  554. av_init_packet(&empty_packet);
  555. empty_packet.size = 0;
  556. empty_packet.data = NULL;
  557. empty_packet.stream_index = stream_index;
  558. avi_write_packet(s, &empty_packet);
  559. av_dlog(s, "dup dts:%s packet_count:%d\n", av_ts2str(dts), avist->packet_count);
  560. }
  561. return 0;
  562. }
  563. static int avi_write_packet(AVFormatContext *s, AVPacket *pkt)
  564. {
  565. unsigned char tag[5];
  566. unsigned int flags = 0;
  567. const int stream_index = pkt->stream_index;
  568. int size = pkt->size;
  569. AVIContext *avi = s->priv_data;
  570. AVIOContext *pb = s->pb;
  571. AVIStream *avist = s->streams[stream_index]->priv_data;
  572. AVCodecContext *enc = s->streams[stream_index]->codec;
  573. int ret;
  574. if (enc->codec_id == AV_CODEC_ID_H264 && enc->codec_tag == MKTAG('H','2','6','4') && pkt->size) {
  575. ret = ff_check_h264_startcode(s, s->streams[stream_index], pkt);
  576. if (ret < 0)
  577. return ret;
  578. }
  579. if ((ret = write_skip_frames(s, stream_index, pkt->dts)) < 0)
  580. return ret;
  581. if (pkt->dts != AV_NOPTS_VALUE)
  582. avist->last_dts = pkt->dts + pkt->duration;
  583. avist->packet_count++;
  584. // Make sure to put an OpenDML chunk when the file size exceeds the limits
  585. if (pb->seekable &&
  586. (avio_tell(pb) - avi->riff_start > AVI_MAX_RIFF_SIZE)) {
  587. avi_write_ix(s);
  588. ff_end_tag(pb, avi->movi_list);
  589. if (avi->riff_id == 1)
  590. avi_write_idx1(s);
  591. ff_end_tag(pb, avi->riff_start);
  592. avi->movi_list = avi_start_new_riff(s, pb, "AVIX", "movi");
  593. }
  594. avi_stream2fourcc(tag, stream_index, enc->codec_type);
  595. if (pkt->flags & AV_PKT_FLAG_KEY)
  596. flags = 0x10;
  597. if (enc->codec_type == AVMEDIA_TYPE_AUDIO)
  598. avist->audio_strm_length += size;
  599. if (s->pb->seekable) {
  600. AVIIndex *idx = &avist->indexes;
  601. int cl = idx->entry / AVI_INDEX_CLUSTER_SIZE;
  602. int id = idx->entry % AVI_INDEX_CLUSTER_SIZE;
  603. if (idx->ents_allocated <= idx->entry) {
  604. idx->cluster = av_realloc_f(idx->cluster, sizeof(void*), cl+1);
  605. if (!idx->cluster) {
  606. idx->ents_allocated = 0;
  607. idx->entry = 0;
  608. return AVERROR(ENOMEM);
  609. }
  610. idx->cluster[cl] =
  611. av_malloc(AVI_INDEX_CLUSTER_SIZE * sizeof(AVIIentry));
  612. if (!idx->cluster[cl])
  613. return AVERROR(ENOMEM);
  614. idx->ents_allocated += AVI_INDEX_CLUSTER_SIZE;
  615. }
  616. idx->cluster[cl][id].flags = flags;
  617. idx->cluster[cl][id].pos = avio_tell(pb) - avi->movi_list;
  618. idx->cluster[cl][id].len = size;
  619. avist->max_size = FFMAX(avist->max_size, size);
  620. idx->entry++;
  621. }
  622. avio_write(pb, tag, 4);
  623. avio_wl32(pb, size);
  624. avio_write(pb, pkt->data, size);
  625. if (size & 1)
  626. avio_w8(pb, 0);
  627. return 0;
  628. }
  629. static int avi_write_trailer(AVFormatContext *s)
  630. {
  631. AVIContext *avi = s->priv_data;
  632. AVIOContext *pb = s->pb;
  633. int res = 0;
  634. int i, j, n, nb_frames;
  635. int64_t file_size;
  636. for (i = 0; i < s->nb_streams; i++) {
  637. AVIStream *avist = s->streams[i]->priv_data;
  638. write_skip_frames(s, i, avist->last_dts);
  639. }
  640. if (pb->seekable) {
  641. if (avi->riff_id == 1) {
  642. ff_end_tag(pb, avi->movi_list);
  643. res = avi_write_idx1(s);
  644. ff_end_tag(pb, avi->riff_start);
  645. } else {
  646. avi_write_ix(s);
  647. ff_end_tag(pb, avi->movi_list);
  648. ff_end_tag(pb, avi->riff_start);
  649. file_size = avio_tell(pb);
  650. avio_seek(pb, avi->odml_list - 8, SEEK_SET);
  651. ffio_wfourcc(pb, "LIST"); /* Making this AVI OpenDML one */
  652. avio_skip(pb, 16);
  653. for (n = nb_frames = 0; n < s->nb_streams; n++) {
  654. AVCodecContext *stream = s->streams[n]->codec;
  655. AVIStream *avist = s->streams[n]->priv_data;
  656. if (stream->codec_type == AVMEDIA_TYPE_VIDEO) {
  657. if (nb_frames < avist->packet_count)
  658. nb_frames = avist->packet_count;
  659. } else {
  660. if (stream->codec_id == AV_CODEC_ID_MP2 ||
  661. stream->codec_id == AV_CODEC_ID_MP3)
  662. nb_frames += avist->packet_count;
  663. }
  664. }
  665. avio_wl32(pb, nb_frames);
  666. avio_seek(pb, file_size, SEEK_SET);
  667. avi_write_counters(s, avi->riff_id);
  668. }
  669. }
  670. for (i = 0; i < s->nb_streams; i++) {
  671. AVIStream *avist = s->streams[i]->priv_data;
  672. for (j = 0; j < avist->indexes.ents_allocated / AVI_INDEX_CLUSTER_SIZE; j++)
  673. av_freep(&avist->indexes.cluster[j]);
  674. av_freep(&avist->indexes.cluster);
  675. avist->indexes.ents_allocated = avist->indexes.entry = 0;
  676. if (pb->seekable) {
  677. avio_seek(pb, avist->frames_hdr_strm + 4, SEEK_SET);
  678. avio_wl32(pb, avist->max_size);
  679. }
  680. }
  681. return res;
  682. }
  683. AVOutputFormat ff_avi_muxer = {
  684. .name = "avi",
  685. .long_name = NULL_IF_CONFIG_SMALL("AVI (Audio Video Interleaved)"),
  686. .mime_type = "video/x-msvideo",
  687. .extensions = "avi",
  688. .priv_data_size = sizeof(AVIContext),
  689. .audio_codec = CONFIG_LIBMP3LAME ? AV_CODEC_ID_MP3 : AV_CODEC_ID_AC3,
  690. .video_codec = AV_CODEC_ID_MPEG4,
  691. .write_header = avi_write_header,
  692. .write_packet = avi_write_packet,
  693. .write_trailer = avi_write_trailer,
  694. .codec_tag = (const AVCodecTag * const []) {
  695. ff_codec_bmp_tags, ff_codec_wav_tags, 0
  696. },
  697. };