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.

908 lines
31KB

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