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;
53  int entry;
57 } AVIIndex;
58 
59 typedef struct AVIContext {
61  int64_t frames_hdr_all;
62  int riff_id;
63 } AVIContext;
64 
65 typedef struct AVIStream {
66  int64_t frames_hdr_strm;
69  int entry;
70  int max_size;
72 
73  int64_t last_dts;
74 
76 } AVIStream;
77 
79 
80 static inline AVIIentry *avi_get_ientry(const AVIIndex *idx, int ent_id)
81 {
82  int cl = ent_id / AVI_INDEX_CLUSTER_SIZE;
83  int id = ent_id % AVI_INDEX_CLUSTER_SIZE;
84  return &idx->cluster[cl][id];
85 }
86 
88  const char *riff_tag, const char *list_tag)
89 {
90  AVIContext *avi = s->priv_data;
91  int64_t loff;
92  int i;
93 
94  avi->riff_id++;
95  for (i = 0; i < s->nb_streams; i++) {
96  AVIStream *avist = s->streams[i]->priv_data;
98  avist->indexes.entry = 0;
99  }
100 
101  avi->riff_start = ff_start_tag(pb, "RIFF");
102  ffio_wfourcc(pb, riff_tag);
103  loff = ff_start_tag(pb, "LIST");
104  ffio_wfourcc(pb, list_tag);
105  return loff;
106 }
107 
108 static char *avi_stream2fourcc(char *tag, int index, enum AVMediaType type)
109 {
110  tag[0] = '0' + index / 10;
111  tag[1] = '0' + index % 10;
112  if (type == AVMEDIA_TYPE_VIDEO) {
113  tag[2] = 'd';
114  tag[3] = 'c';
115  } else if (type == AVMEDIA_TYPE_SUBTITLE) {
116  // note: this is not an official code
117  tag[2] = 's';
118  tag[3] = 'b';
119  } else {
120  tag[2] = 'w';
121  tag[3] = 'b';
122  }
123  tag[4] = '\0';
124  return tag;
125 }
126 
127 static int avi_write_counters(AVFormatContext *s, int riff_id)
128 {
129  AVIOContext *pb = s->pb;
130  AVIContext *avi = s->priv_data;
131  int n, au_byterate, au_ssize, au_scale, nb_frames = 0;
132  int64_t file_size;
133  AVCodecContext *stream;
134 
135  file_size = avio_tell(pb);
136  for (n = 0; n < s->nb_streams; n++) {
137  AVIStream *avist = s->streams[n]->priv_data;
138 
139  av_assert0(avist->frames_hdr_strm);
140  stream = s->streams[n]->codec;
141  avio_seek(pb, avist->frames_hdr_strm, SEEK_SET);
142  ff_parse_specific_params(s->streams[n], &au_byterate, &au_ssize, &au_scale);
143  if (au_ssize == 0)
144  avio_wl32(pb, avist->packet_count);
145  else
146  avio_wl32(pb, avist->audio_strm_length / au_ssize);
147  if (stream->codec_type == AVMEDIA_TYPE_VIDEO)
148  nb_frames = FFMAX(nb_frames, avist->packet_count);
149  }
150  if (riff_id == 1) {
152  avio_seek(pb, avi->frames_hdr_all, SEEK_SET);
153  avio_wl32(pb, nb_frames);
154  }
155  avio_seek(pb, file_size, SEEK_SET);
156 
157  return 0;
158 }
159 
160 static void write_odml_master(AVFormatContext *s, int stream_index)
161 {
162  AVIOContext *pb = s->pb;
163  AVStream *st = s->streams[stream_index];
164  AVCodecContext *enc = st->codec;
165  AVIStream *avist = st->priv_data;
166  unsigned char tag[5];
167  int j;
168 
169  /* Starting to lay out AVI OpenDML master index.
170  * We want to make it JUNK entry for now, since we'd
171  * like to get away without making AVI an OpenDML one
172  * for compatibility reasons. */
173  avist->indexes.indx_start = ff_start_tag(pb, "JUNK");
174  avio_wl16(pb, 4); /* wLongsPerEntry */
175  avio_w8(pb, 0); /* bIndexSubType (0 == frame index) */
176  avio_w8(pb, 0); /* bIndexType (0 == AVI_INDEX_OF_INDEXES) */
177  avio_wl32(pb, 0); /* nEntriesInUse (will fill out later on) */
178  ffio_wfourcc(pb, avi_stream2fourcc(tag, stream_index, enc->codec_type));
179  /* dwChunkId */
180  avio_wl64(pb, 0); /* dwReserved[3] */
181  avio_wl32(pb, 0); /* Must be 0. */
182  for (j = 0; j < AVI_MASTER_INDEX_SIZE * 2; j++)
183  avio_wl64(pb, 0);
184  ff_end_tag(pb, avist->indexes.indx_start);
185 }
186 
188 {
189  AVIContext *avi = s->priv_data;
190  AVIOContext *pb = s->pb;
191  int bitrate, n, i, nb_frames, au_byterate, au_ssize, au_scale;
192  AVCodecContext *video_enc;
193  AVStream *video_st = NULL;
194  int64_t list1, list2, strh, strf;
195  AVDictionaryEntry *t = NULL;
196  int padding;
197 
198  if (s->nb_streams > AVI_MAX_STREAM_COUNT) {
199  av_log(s, AV_LOG_ERROR, "AVI does not support >%d streams\n",
201  return AVERROR(EINVAL);
202  }
203 
204  for (n = 0; n < s->nb_streams; n++) {
205  s->streams[n]->priv_data = av_mallocz(sizeof(AVIStream));
206  if (!s->streams[n]->priv_data)
207  return AVERROR(ENOMEM);
208  }
209 
210  /* header list */
211  avi->riff_id = 0;
212  list1 = avi_start_new_riff(s, pb, "AVI ", "hdrl");
213 
214  /* avi header */
215  ffio_wfourcc(pb, "avih");
216  avio_wl32(pb, 14 * 4);
217  bitrate = 0;
218 
219  video_enc = NULL;
220  for (n = 0; n < s->nb_streams; n++) {
221  AVCodecContext *codec = s->streams[n]->codec;
222  bitrate += codec->bit_rate;
223  if (codec->codec_type == AVMEDIA_TYPE_VIDEO) {
224  video_enc = codec;
225  video_st = s->streams[n];
226  }
227  }
228 
229  nb_frames = 0;
230 
231  // TODO: should be avg_frame_rate
232  if (video_st)
233  avio_wl32(pb, (uint32_t) (INT64_C(1000000) * video_st->time_base.num /
234  video_st->time_base.den));
235  else
236  avio_wl32(pb, 0);
237  avio_wl32(pb, bitrate / 8); /* XXX: not quite exact */
238  avio_wl32(pb, 0); /* padding */
239  if (!pb->seekable)
240  avio_wl32(pb, AVIF_TRUSTCKTYPE | AVIF_ISINTERLEAVED); /* flags */
241  else
243  avi->frames_hdr_all = avio_tell(pb); /* remember this offset to fill later */
244  avio_wl32(pb, nb_frames); /* nb frames, filled later */
245  avio_wl32(pb, 0); /* initial frame */
246  avio_wl32(pb, s->nb_streams); /* nb streams */
247  avio_wl32(pb, 1024 * 1024); /* suggested buffer size */
248  if (video_enc) {
249  avio_wl32(pb, video_enc->width);
250  avio_wl32(pb, video_enc->height);
251  } else {
252  avio_wl32(pb, 0);
253  avio_wl32(pb, 0);
254  }
255  avio_wl32(pb, 0); /* reserved */
256  avio_wl32(pb, 0); /* reserved */
257  avio_wl32(pb, 0); /* reserved */
258  avio_wl32(pb, 0); /* reserved */
259 
260  /* stream list */
261  for (i = 0; i < n; i++) {
262  AVStream *st = s->streams[i];
263  AVCodecContext *enc = st->codec;
264  AVIStream *avist = st->priv_data;
265  list2 = ff_start_tag(pb, "LIST");
266  ffio_wfourcc(pb, "strl");
267 
268  /* stream generic header */
269  strh = ff_start_tag(pb, "strh");
270  switch (enc->codec_type) {
272  // XSUB subtitles behave like video tracks, other subtitles
273  // are not (yet) supported.
274  if (enc->codec_id != AV_CODEC_ID_XSUB) {
275  av_log(s, AV_LOG_ERROR,
276  "Subtitle streams other than DivX XSUB are not supported by the AVI muxer.\n");
277  return AVERROR_PATCHWELCOME;
278  }
279  case AVMEDIA_TYPE_VIDEO:
280  ffio_wfourcc(pb, "vids");
281  break;
282  case AVMEDIA_TYPE_AUDIO:
283  ffio_wfourcc(pb, "auds");
284  break;
285 // case AVMEDIA_TYPE_TEXT:
286 // ffio_wfourcc(pb, "txts");
287 // break;
288  case AVMEDIA_TYPE_DATA:
289  ffio_wfourcc(pb, "dats");
290  break;
291  }
292  if (enc->codec_type == AVMEDIA_TYPE_VIDEO ||
293  enc->codec_id == AV_CODEC_ID_XSUB)
294  avio_wl32(pb, enc->codec_tag);
295  else
296  avio_wl32(pb, 1);
297  avio_wl32(pb, 0); /* flags */
298  avio_wl16(pb, 0); /* priority */
299  avio_wl16(pb, 0); /* language */
300  avio_wl32(pb, 0); /* initial frame */
301 
302  ff_parse_specific_params(st, &au_byterate, &au_ssize, &au_scale);
303 
304  if ( enc->codec_type == AVMEDIA_TYPE_VIDEO
305  && enc->codec_id != AV_CODEC_ID_XSUB
306  && au_byterate > 1000LL*au_scale) {
307  au_byterate = 600;
308  au_scale = 1;
309  }
310  avpriv_set_pts_info(st, 64, au_scale, au_byterate);
311  if (enc->codec_id == AV_CODEC_ID_XSUB)
312  au_scale = au_byterate = 0;
313 
314  avio_wl32(pb, au_scale); /* scale */
315  avio_wl32(pb, au_byterate); /* rate */
316 
317  avio_wl32(pb, 0); /* start */
318  /* remember this offset to fill later */
319  avist->frames_hdr_strm = avio_tell(pb);
320  if (!pb->seekable)
321  /* FIXME: this may be broken, but who cares */
323  else
324  avio_wl32(pb, 0); /* length, XXX: filled later */
325 
326  /* suggested buffer size, is set to largest chunk size in avi_write_trailer */
327  if (enc->codec_type == AVMEDIA_TYPE_VIDEO)
328  avio_wl32(pb, 1024 * 1024);
329  else if (enc->codec_type == AVMEDIA_TYPE_AUDIO)
330  avio_wl32(pb, 12 * 1024);
331  else
332  avio_wl32(pb, 0);
333  avio_wl32(pb, -1); /* quality */
334  avio_wl32(pb, au_ssize); /* sample size */
335  avio_wl32(pb, 0);
336  avio_wl16(pb, enc->width);
337  avio_wl16(pb, enc->height);
338  ff_end_tag(pb, strh);
339 
340  if (enc->codec_type != AVMEDIA_TYPE_DATA) {
341  int ret;
342  enum AVPixelFormat pix_fmt;
343 
344  strf = ff_start_tag(pb, "strf");
345  switch (enc->codec_type) {
347  /* XSUB subtitles behave like video tracks, other subtitles
348  * are not (yet) supported. */
349  if (enc->codec_id != AV_CODEC_ID_XSUB)
350  break;
351  case AVMEDIA_TYPE_VIDEO:
352  /* WMP expects RGB 5:5:5 rawvideo in avi to have bpp set to 16. */
353  if ( !enc->codec_tag
354  && enc->codec_id == AV_CODEC_ID_RAWVIDEO
355  && enc->pix_fmt == AV_PIX_FMT_RGB555LE
356  && enc->bits_per_coded_sample == 15)
357  enc->bits_per_coded_sample = 16;
358  ff_put_bmp_header(pb, enc, ff_codec_bmp_tags, 0, 0);
360  enc->bits_per_coded_sample);
361  if ( !enc->codec_tag
362  && enc->codec_id == AV_CODEC_ID_RAWVIDEO
363  && enc->pix_fmt != pix_fmt
364  && enc->pix_fmt != AV_PIX_FMT_NONE)
365  av_log(s, AV_LOG_ERROR, "%s rawvideo cannot be written to avi, output file will be unreadable\n",
367  break;
368  case AVMEDIA_TYPE_AUDIO:
369  if ((ret = ff_put_wav_header(pb, enc, 0)) < 0)
370  return ret;
371  break;
372  default:
373  av_log(s, AV_LOG_ERROR,
374  "Invalid or not supported codec type '%s' found in the input\n",
375  (char *)av_x_if_null(av_get_media_type_string(enc->codec_type), "?"));
376  return AVERROR(EINVAL);
377  }
378  ff_end_tag(pb, strf);
379  if ((t = av_dict_get(st->metadata, "title", NULL, 0))) {
380  ff_riff_write_info_tag(s->pb, "strn", t->value);
381  t = NULL;
382  }
383  if (enc->codec_id == AV_CODEC_ID_XSUB
384  && (t = av_dict_get(s->streams[i]->metadata, "language", NULL, 0))) {
385  const char* langstr = av_convert_lang_to(t->value, AV_LANG_ISO639_1);
386  t = NULL;
387  if (langstr) {
388  char* str = av_asprintf("Subtitle - %s-xx;02", langstr);
389  if (!str)
390  return AVERROR(ENOMEM);
391  ff_riff_write_info_tag(s->pb, "strn", str);
392  av_free(str);
393  }
394  }
395  }
396 
397  if (pb->seekable) {
398  write_odml_master(s, i);
399  }
400 
401  if (enc->codec_type == AVMEDIA_TYPE_VIDEO &&
402  st->sample_aspect_ratio.num > 0 &&
403  st->sample_aspect_ratio.den > 0) {
404  int vprp = ff_start_tag(pb, "vprp");
406  (AVRational) { enc->width,
407  enc->height });
408  int num, den;
409  av_reduce(&num, &den, dar.num, dar.den, 0xFFFF);
410 
411  avio_wl32(pb, 0); // video format = unknown
412  avio_wl32(pb, 0); // video standard = unknown
413  // TODO: should be avg_frame_rate
414  avio_wl32(pb, (2LL*st->time_base.den + st->time_base.num - 1) / (2LL * st->time_base.num));
415  avio_wl32(pb, enc->width);
416  avio_wl32(pb, enc->height);
417  avio_wl16(pb, den);
418  avio_wl16(pb, num);
419  avio_wl32(pb, enc->width);
420  avio_wl32(pb, enc->height);
421  avio_wl32(pb, 1); // progressive FIXME
422 
423  avio_wl32(pb, enc->height);
424  avio_wl32(pb, enc->width);
425  avio_wl32(pb, enc->height);
426  avio_wl32(pb, enc->width);
427  avio_wl32(pb, 0);
428  avio_wl32(pb, 0);
429 
430  avio_wl32(pb, 0);
431  avio_wl32(pb, 0);
432  ff_end_tag(pb, vprp);
433  }
434 
435  ff_end_tag(pb, list2);
436  }
437 
438  if (pb->seekable) {
439  /* AVI could become an OpenDML one, if it grows beyond 2Gb range */
440  avi->odml_list = ff_start_tag(pb, "JUNK");
441  ffio_wfourcc(pb, "odml");
442  ffio_wfourcc(pb, "dmlh");
443  avio_wl32(pb, 248);
444  for (i = 0; i < 248; i += 4)
445  avio_wl32(pb, 0);
446  ff_end_tag(pb, avi->odml_list);
447  }
448 
449  ff_end_tag(pb, list1);
450 
452 
453 
454  padding = s->metadata_header_padding;
455  if (padding < 0)
456  padding = 1016;
457 
458  /* some padding for easier tag editing */
459  if (padding) {
460  list2 = ff_start_tag(pb, "JUNK");
461  for (i = padding; i > 0; i -= 4)
462  avio_wl32(pb, 0);
463  ff_end_tag(pb, list2);
464  }
465 
466  avi->movi_list = ff_start_tag(pb, "LIST");
467  ffio_wfourcc(pb, "movi");
468 
469  avio_flush(pb);
470 
471  return 0;
472 }
473 
474 static void update_odml_entry(AVFormatContext *s, int stream_index, int64_t ix, int size)
475 {
476  AVIOContext *pb = s->pb;
477  AVIContext *avi = s->priv_data;
478  AVIStream *avist = s->streams[stream_index]->priv_data;
479  int64_t pos;
480  int au_byterate, au_ssize, au_scale;
481 
482  avio_flush(pb);
483  pos = avio_tell(pb);
484 
485  /* Updating one entry in the AVI OpenDML master index */
486  avio_seek(pb, avist->indexes.indx_start - 8, SEEK_SET);
487  ffio_wfourcc(pb, "indx"); /* enabling this entry */
488  avio_skip(pb, 8);
489  avio_wl32(pb, avi->riff_id - avist->indexes.master_odml_riff_id_base); /* nEntriesInUse */
490  avio_skip(pb, 16 * (avi->riff_id - avist->indexes.master_odml_riff_id_base));
491  avio_wl64(pb, ix); /* qwOffset */
492  avio_wl32(pb, size); /* dwSize */
493  ff_parse_specific_params(s->streams[stream_index], &au_byterate, &au_ssize, &au_scale);
494  if (s->streams[stream_index]->codec->codec_type == AVMEDIA_TYPE_AUDIO && au_ssize > 0) {
495  uint32_t audio_segm_size = (avist->audio_strm_length - avist->indexes.audio_strm_offset);
496  if ((audio_segm_size % au_ssize > 0) && !avist->sample_requested) {
497  avpriv_request_sample(s, "OpenDML index duration for audio packets with partial frames");
498  avist->sample_requested = 1;
499  }
500  avio_wl32(pb, audio_segm_size / au_ssize); /* dwDuration (sample count) */
501  } else
502  avio_wl32(pb, avist->indexes.entry); /* dwDuration (packet count) */
503 
504  avio_seek(pb, pos, SEEK_SET);
505 }
506 
508 {
509  AVIOContext *pb = s->pb;
510  AVIContext *avi = s->priv_data;
511  char tag[5];
512  char ix_tag[] = "ix00";
513  int i, j;
514 
515  av_assert0(pb->seekable);
516 
517  for (i = 0; i < s->nb_streams; i++) {
518  AVIStream *avist = s->streams[i]->priv_data;
520  int64_t pos;
521  int size = 8+2+1+1+4+8+4+4+16*AVI_MASTER_INDEX_SIZE;
522 
523  pos = avio_tell(pb);
524  update_odml_entry(s, i, pos, size);
525  write_odml_master(s, i);
526  av_assert1(avio_tell(pb) - pos == size);
527  avist->indexes.master_odml_riff_id_base = avi->riff_id - 1;
528  }
530  }
531 
532  for (i = 0; i < s->nb_streams; i++) {
533  AVIStream *avist = s->streams[i]->priv_data;
534  int64_t ix;
535 
536  avi_stream2fourcc(tag, i, s->streams[i]->codec->codec_type);
537  ix_tag[3] = '0' + i;
538 
539  /* Writing AVI OpenDML leaf index chunk */
540  ix = avio_tell(pb);
541  ffio_wfourcc(pb, ix_tag); /* ix?? */
542  avio_wl32(pb, avist->indexes.entry * 8 + 24);
543  /* chunk size */
544  avio_wl16(pb, 2); /* wLongsPerEntry */
545  avio_w8(pb, 0); /* bIndexSubType (0 == frame index) */
546  avio_w8(pb, 1); /* bIndexType (1 == AVI_INDEX_OF_CHUNKS) */
547  avio_wl32(pb, avist->indexes.entry);
548  /* nEntriesInUse */
549  ffio_wfourcc(pb, tag); /* dwChunkId */
550  avio_wl64(pb, avi->movi_list); /* qwBaseOffset */
551  avio_wl32(pb, 0); /* dwReserved_3 (must be 0) */
552 
553  for (j = 0; j < avist->indexes.entry; j++) {
554  AVIIentry *ie = avi_get_ientry(&avist->indexes, j);
555  avio_wl32(pb, ie->pos + 8);
556  avio_wl32(pb, ((uint32_t) ie->len & ~0x80000000) |
557  (ie->flags & 0x10 ? 0 : 0x80000000));
558  }
559 
560  update_odml_entry(s, i, ix, avio_tell(pb) - ix);
561  }
562  return 0;
563 }
564 
566 {
567  AVIOContext *pb = s->pb;
568  AVIContext *avi = s->priv_data;
569  int64_t idx_chunk;
570  int i;
571  char tag[5];
572 
573  if (pb->seekable) {
574  AVIStream *avist;
575  AVIIentry *ie = 0, *tie;
576  int empty, stream_id = -1;
577 
578  idx_chunk = ff_start_tag(pb, "idx1");
579  for (i = 0; i < s->nb_streams; i++) {
580  avist = s->streams[i]->priv_data;
581  avist->entry = 0;
582  }
583 
584  do {
585  empty = 1;
586  for (i = 0; i < s->nb_streams; i++) {
587  avist = s->streams[i]->priv_data;
588  if (avist->indexes.entry <= avist->entry)
589  continue;
590 
591  tie = avi_get_ientry(&avist->indexes, avist->entry);
592  if (empty || tie->pos < ie->pos) {
593  ie = tie;
594  stream_id = i;
595  }
596  empty = 0;
597  }
598  if (!empty) {
599  avist = s->streams[stream_id]->priv_data;
600  avi_stream2fourcc(tag, stream_id,
601  s->streams[stream_id]->codec->codec_type);
602  ffio_wfourcc(pb, tag);
603  avio_wl32(pb, ie->flags);
604  avio_wl32(pb, ie->pos);
605  avio_wl32(pb, ie->len);
606  avist->entry++;
607  }
608  } while (!empty);
609  ff_end_tag(pb, idx_chunk);
610 
611  avi_write_counters(s, avi->riff_id);
612  }
613  return 0;
614 }
615 
616 static int write_skip_frames(AVFormatContext *s, int stream_index, int64_t dts)
617 {
618  AVIStream *avist = s->streams[stream_index]->priv_data;
619  AVCodecContext *enc = s->streams[stream_index]->codec;
620 
621  av_dlog(s, "dts:%s packet_count:%d stream_index:%d\n", av_ts2str(dts), avist->packet_count, stream_index);
622  while (enc->block_align == 0 && dts != AV_NOPTS_VALUE &&
623  dts > avist->packet_count && enc->codec_id != AV_CODEC_ID_XSUB && avist->packet_count) {
624  AVPacket empty_packet;
625 
626  if (dts - avist->packet_count > 60000) {
627  av_log(s, AV_LOG_ERROR, "Too large number of skipped frames %"PRId64" > 60000\n", dts - avist->packet_count);
628  return AVERROR(EINVAL);
629  }
630 
631  av_init_packet(&empty_packet);
632  empty_packet.size = 0;
633  empty_packet.data = NULL;
634  empty_packet.stream_index = stream_index;
635  avi_write_packet(s, &empty_packet);
636  av_dlog(s, "dup dts:%s packet_count:%d\n", av_ts2str(dts), avist->packet_count);
637  }
638 
639  return 0;
640 }
641 
643 {
644  unsigned char tag[5];
645  unsigned int flags = 0;
646  const int stream_index = pkt->stream_index;
647  int size = pkt->size;
648  AVIContext *avi = s->priv_data;
649  AVIOContext *pb = s->pb;
650  AVIStream *avist = s->streams[stream_index]->priv_data;
651  AVCodecContext *enc = s->streams[stream_index]->codec;
652  int ret;
653 
654  if (enc->codec_id == AV_CODEC_ID_H264 && enc->codec_tag == MKTAG('H','2','6','4') && pkt->size) {
655  ret = ff_check_h264_startcode(s, s->streams[stream_index], pkt);
656  if (ret < 0)
657  return ret;
658  }
659 
660  if ((ret = write_skip_frames(s, stream_index, pkt->dts)) < 0)
661  return ret;
662 
663  if (pkt->dts != AV_NOPTS_VALUE)
664  avist->last_dts = pkt->dts + pkt->duration;
665 
666  avist->packet_count++;
667 
668  // Make sure to put an OpenDML chunk when the file size exceeds the limits
669  if (pb->seekable &&
670  (avio_tell(pb) - avi->riff_start > AVI_MAX_RIFF_SIZE)) {
671  avi_write_ix(s);
672  ff_end_tag(pb, avi->movi_list);
673 
674  if (avi->riff_id == 1)
675  avi_write_idx1(s);
676 
677  ff_end_tag(pb, avi->riff_start);
678  avi->movi_list = avi_start_new_riff(s, pb, "AVIX", "movi");
679  }
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 
687  if (s->pb->seekable) {
688  AVIIndex *idx = &avist->indexes;
689  int cl = idx->entry / AVI_INDEX_CLUSTER_SIZE;
690  int id = idx->entry % AVI_INDEX_CLUSTER_SIZE;
691  if (idx->ents_allocated <= idx->entry) {
692  idx->cluster = av_realloc_f(idx->cluster, sizeof(void*), cl+1);
693  if (!idx->cluster) {
694  idx->ents_allocated = 0;
695  idx->entry = 0;
696  return AVERROR(ENOMEM);
697  }
698  idx->cluster[cl] =
700  if (!idx->cluster[cl])
701  return AVERROR(ENOMEM);
703  }
704 
705  idx->cluster[cl][id].flags = flags;
706  idx->cluster[cl][id].pos = avio_tell(pb) - avi->movi_list;
707  idx->cluster[cl][id].len = size;
708  avist->max_size = FFMAX(avist->max_size, size);
709  idx->entry++;
710  }
711 
712  avio_write(pb, tag, 4);
713  avio_wl32(pb, size);
714  avio_write(pb, pkt->data, size);
715  if (size & 1)
716  avio_w8(pb, 0);
717 
718  return 0;
719 }
720 
722 {
723  AVIContext *avi = s->priv_data;
724  AVIOContext *pb = s->pb;
725  int res = 0;
726  int i, j, n, nb_frames;
727  int64_t file_size;
728 
729  for (i = 0; i < s->nb_streams; i++) {
730  AVIStream *avist = s->streams[i]->priv_data;
731  write_skip_frames(s, i, avist->last_dts);
732  }
733 
734  if (pb->seekable) {
735  if (avi->riff_id == 1) {
736  ff_end_tag(pb, avi->movi_list);
737  res = avi_write_idx1(s);
738  ff_end_tag(pb, avi->riff_start);
739  } else {
740  avi_write_ix(s);
741  ff_end_tag(pb, avi->movi_list);
742  ff_end_tag(pb, avi->riff_start);
743 
744  file_size = avio_tell(pb);
745  avio_seek(pb, avi->odml_list - 8, SEEK_SET);
746  ffio_wfourcc(pb, "LIST"); /* Making this AVI OpenDML one */
747  avio_skip(pb, 16);
748 
749  for (n = nb_frames = 0; n < s->nb_streams; n++) {
750  AVCodecContext *stream = s->streams[n]->codec;
751  AVIStream *avist = s->streams[n]->priv_data;
752 
753  if (stream->codec_type == AVMEDIA_TYPE_VIDEO) {
754  if (nb_frames < avist->packet_count)
755  nb_frames = avist->packet_count;
756  } else {
757  if (stream->codec_id == AV_CODEC_ID_MP2 ||
758  stream->codec_id == AV_CODEC_ID_MP3)
759  nb_frames += avist->packet_count;
760  }
761  }
762  avio_wl32(pb, nb_frames);
763  avio_seek(pb, file_size, SEEK_SET);
764 
765  avi_write_counters(s, avi->riff_id);
766  }
767  }
768 
769  for (i = 0; i < s->nb_streams; i++) {
770  AVIStream *avist = s->streams[i]->priv_data;
771  for (j = 0; j < avist->indexes.ents_allocated / AVI_INDEX_CLUSTER_SIZE; j++)
772  av_freep(&avist->indexes.cluster[j]);
773  av_freep(&avist->indexes.cluster);
774  avist->indexes.ents_allocated = avist->indexes.entry = 0;
775  if (pb->seekable) {
776  avio_seek(pb, avist->frames_hdr_strm + 4, SEEK_SET);
777  avio_wl32(pb, avist->max_size);
778  }
779  }
780 
781  return res;
782 }
783 
785  .name = "avi",
786  .long_name = NULL_IF_CONFIG_SMALL("AVI (Audio Video Interleaved)"),
787  .mime_type = "video/x-msvideo",
788  .extensions = "avi",
789  .priv_data_size = sizeof(AVIContext),
790  .audio_codec = CONFIG_LIBMP3LAME ? AV_CODEC_ID_MP3 : AV_CODEC_ID_AC3,
791  .video_codec = AV_CODEC_ID_MPEG4,
795  .codec_tag = (const AVCodecTag * const []) {
797  },
798 };
static int write_skip_frames(AVFormatContext *s, int stream_index, int64_t dts)
Definition: avienc.c:616
#define NULL
Definition: coverity.c:32
void avio_wl16(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:416
const char * s
Definition: avisynth_c.h:631
Bytestream IO Context.
Definition: avio.h:111
static enum AVPixelFormat pix_fmt
#define av_realloc_f(p, o, n)
void ff_end_tag(AVIOContext *pb, int64_t start)
Definition: riffenc.c:38
int64_t ff_start_tag(AVIOContext *pb, const char *tag)
Definition: riffenc.c:31
enum AVCodecID id
Definition: mxfenc.c:99
int master_odml_riff_id_base
Definition: avienc.c:55
unsigned int pos
Definition: avienc.c:45
int ents_allocated
Definition: avienc.c:54
void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition: utils.c:4006
#define AVI_MAX_RIFF_SIZE
Definition: avi.h:31
int64_t audio_strm_offset
Definition: avienc.c:52
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown)
Definition: avformat.h:914
int num
numerator
Definition: rational.h:44
packed RGB 5:5:5, 16bpp, (msb)1X 5R 5G 5B(lsb), little-endian, X=unused/undefined ...
Definition: pixfmt.h:117
int size
Definition: avcodec.h:1163
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:203
#define AVI_MASTER_INDEX_SIZE
Definition: avi.h:32
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1444
static char * avi_stream2fourcc(char *tag, int index, enum AVMediaType type)
Definition: avienc.c:108
void * priv_data
Definition: avformat.h:862
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:276
static AVPacket pkt
AVOutputFormat ff_avi_muxer
Definition: avienc.c:784
int64_t frames_hdr_strm
Definition: avienc.c:66
int block_align
number of bytes per packet if constant and known or 0 Used by some WAV based audio codecs...
Definition: avcodec.h:2022
Format I/O context.
Definition: avformat.h:1272
int64_t odml_list
Definition: avienc.c:60
#define AVIF_ISINTERLEAVED
Definition: avi.h:26
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
Public dictionary API.
void void avpriv_request_sample(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
void avio_wl32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:318
#define av_malloc(s)
Opaque data information usually continuous.
Definition: avutil.h:196
timestamp utils, mostly useful for debugging/logging purposes
int64_t movi_list
Definition: avidec.c:75
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1340
AVDictionaryEntry * av_dict_get(const AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags)
Get a dictionary entry with matching key.
Definition: dict.c:39
uint8_t * data
Definition: avcodec.h:1162
AVRational av_mul_q(AVRational b, AVRational c)
Multiply two rationals.
Definition: rational.c:80
int64_t riff_start
Definition: avienc.c:60
uint32_t tag
Definition: movenc.c:1333
static int64_t avi_start_new_riff(AVFormatContext *s, AVIOContext *pb, const char *riff_tag, const char *list_tag)
Definition: avienc.c:87
ptrdiff_t size
Definition: opengl_enc.c:101
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:365
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition: avcodec.h:2720
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:177
static av_always_inline void ffio_wfourcc(AVIOContext *pb, const uint8_t *s)
Definition: avio_internal.h:67
int duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: avcodec.h:1180
#define av_log(a,...)
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1208
void avio_wl64(AVIOContext *s, uint64_t val)
Definition: aviobuf.c:404
static void * av_x_if_null(const void *p, const void *x)
Return x default pointer in case p is NULL.
Definition: avutil.h:301
#define AVI_MAX_STREAM_COUNT
Definition: avi.h:33
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
static int avi_write_idx1(AVFormatContext *s)
Definition: avienc.c:565
void ff_parse_specific_params(AVStream *st, int *au_rate, int *au_ssize, int *au_scale)
Definition: riffenc.c:237
int ff_check_h264_startcode(AVFormatContext *s, const AVStream *st, const AVPacket *pkt)
Check presence of H264 startcode.
Definition: mpegtsenc.c:1204
#define AVERROR(e)
Definition: error.h:43
const char * av_convert_lang_to(const char *lang, enum AVLangCodespace target_codespace)
Convert a language code to a target codespace.
Definition: avlanguage.c:736
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:175
void ff_riff_write_info_tag(AVIOContext *pb, const char *tag, const char *str)
Write a single RIFF info tag.
Definition: riffenc.c:270
preferred ID for decoding MPEG audio layer 1, 2 or 3
Definition: avcodec.h:421
simple assert() macros that are a bit more flexible than ISO C assert().
unsigned int flags
Definition: avienc.c:45
const AVCodecTag ff_codec_wav_tags[]
Definition: riff.c:376
#define FFMAX(a, b)
Definition: common.h:64
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1168
int packet_count
Definition: avienc.c:68
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:861
int av_reduce(int *dst_num, int *dst_den, int64_t num, int64_t den, int64_t max)
Reduce a fraction.
Definition: rational.c:35
char * av_asprintf(const char *fmt,...)
Definition: avstring.c:113
unsigned int len
Definition: avienc.c:45
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:1328
static void update_odml_entry(AVFormatContext *s, int stream_index, int64_t ix, int size)
Definition: avienc.c:474
int seekable
A combination of AVIO_SEEKABLE_ flags or 0 when the stream is not seekable.
Definition: avio.h:160
int bit_rate
the average bitrate
Definition: avcodec.h:1305
int void avio_flush(AVIOContext *s)
Force flushing of buffered data.
Definition: aviobuf.c:197
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition: avassert.h:53
Raw Video Codec.
const AVCodecTag ff_codec_bmp_tags[]
Definition: riff.c:32
ret
Definition: avfilter.c:974
int width
picture width / height.
Definition: avcodec.h:1414
static int write_trailer(AVFormatContext *s1)
Definition: v4l2enc.c:94
const char * name
Definition: avformat.h:513
internal header for RIFF based (de)muxers do NOT include this in end user applications ...
static int avi_write_trailer(AVFormatContext *s)
Definition: avienc.c:721
int n
Definition: avisynth_c.h:547
AVDictionary * metadata
Definition: avformat.h:916
int64_t indx_start
Definition: avienc.c:51
int64_t audio_strm_length
Definition: avienc.c:67
static int avi_write_header(AVFormatContext *s)
Definition: avienc.c:187
#define AVIF_HASINDEX
Definition: avi.h:24
Stream structure.
Definition: avformat.h:842
#define av_dlog(pctx,...)
av_dlog macros
Definition: log.h:330
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
static int avi_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: avienc.c:642
enum AVPixelFormat avpriv_find_pix_fmt(const PixelFormatTag *tags, unsigned int fourcc)
Definition: utils.c:1129
enum AVMediaType codec_type
Definition: avcodec.h:1249
enum AVCodecID codec_id
Definition: avcodec.h:1258
AVIOContext * pb
I/O context.
Definition: avformat.h:1314
void avio_w8(AVIOContext *s, int b)
Definition: aviobuf.c:155
main external API structure.
Definition: avcodec.h:1241
int sample_requested
Definition: avienc.c:71
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
Definition: avcodec.h:1273
GLint GLenum type
Definition: opengl_enc.c:105
void ff_riff_write_info(AVFormatContext *s)
Write all recognized RIFF tags from s->metadata.
Definition: riffenc.c:301
#define AVI_INDEX_CLUSTER_SIZE
Definition: avienc.c:48
int index
Definition: gxfenc.c:89
rational number numerator/denominator
Definition: rational.h:43
void ff_put_bmp_header(AVIOContext *pb, AVCodecContext *enc, const AVCodecTag *tags, int for_asf, int ignore_extradata)
Definition: riffenc.c:205
AVMediaType
Definition: avutil.h:192
static AVIIentry * avi_get_ientry(const AVIIndex *idx, int ent_id)
Definition: avienc.c:80
static void write_odml_master(AVFormatContext *s, int stream_index)
Definition: avienc.c:160
AVIIentry ** cluster
Definition: avienc.c:56
const char * av_get_media_type_string(enum AVMediaType media_type)
Return a string describing the media_type enum, NULL if media_type is unknown.
Definition: utils.c:72
static int flags
Definition: cpu.c:47
Main libavformat public API header.
void av_init_packet(AVPacket *pkt)
Initialize optional fields of a packet with default values.
Definition: avpacket.c:49
int den
denominator
Definition: rational.h:45
#define av_free(p)
char * value
Definition: dict.h:88
#define av_ts2str(ts)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition: timestamp.h:54
int64_t frames_hdr_all
Definition: avienc.c:61
const PixelFormatTag avpriv_pix_fmt_bps_avi[]
Definition: raw.c:243
int ff_put_wav_header(AVIOContext *pb, AVCodecContext *enc, int flags)
Write WAVEFORMAT header structure.
Definition: riffenc.c:54
void * priv_data
Format private data.
Definition: avformat.h:1300
static void write_header(FFV1Context *f)
Definition: ffv1enc.c:493
int entry
Definition: avienc.c:69
AVIIndex indexes
Definition: avienc.c:75
static int avi_write_counters(AVFormatContext *s, int riff_id)
Definition: avienc.c:127
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
Definition: avcodec.h:1161
#define AVIF_TRUSTCKTYPE
Definition: avi.h:27
#define av_freep(p)
int entry
Definition: avienc.c:53
const char * av_get_pix_fmt_name(enum AVPixelFormat pix_fmt)
Return the short name for a pixel format, NULL in case pix_fmt is unknown.
Definition: pixdesc.c:2011
int stream_index
Definition: avcodec.h:1164
int max_size
Definition: avienc.c:70
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avformat.h:884
int64_t last_dts
Definition: avienc.c:73
#define MKTAG(a, b, c, d)
Definition: common.h:315
AVPixelFormat
Pixel format.
Definition: pixfmt.h:61
This structure stores compressed data.
Definition: avcodec.h:1139
static int write_packet(AVFormatContext *s1, AVPacket *pkt)
Definition: v4l2enc.c:86
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:250
static int avi_write_ix(AVFormatContext *s)
Definition: avienc.c:507
3-char terminologic language codes as per ISO-IEC 639-2
Definition: avlanguage.h:30
int riff_id
Definition: avienc.c:62
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:241