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.

842 lines
28KB

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