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