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.

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