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.

712 lines
24KB

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