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