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