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.

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