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.

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