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.

755 lines
25KB

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