• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

libavformat/avienc.c

Go to the documentation of this file.
00001 /*
00002  * AVI muxer
00003  * Copyright (c) 2000 Fabrice Bellard
00004  *
00005  * This file is part of FFmpeg.
00006  *
00007  * FFmpeg is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * FFmpeg is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with FFmpeg; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00020  */
00021 #include "avformat.h"
00022 #include "avi.h"
00023 #include "riff.h"
00024 #include "libavutil/intreadwrite.h"
00025 
00026 /*
00027  * TODO:
00028  *  - fill all fields if non streamed (nb_frames for example)
00029  */
00030 
00031 typedef struct AVIIentry {
00032     unsigned int flags, pos, len;
00033 } AVIIentry;
00034 
00035 #define AVI_INDEX_CLUSTER_SIZE 16384
00036 
00037 typedef struct AVIIndex {
00038     int64_t     indx_start;
00039     int         entry;
00040     int         ents_allocated;
00041     AVIIentry** cluster;
00042 } AVIIndex;
00043 
00044 typedef struct {
00045     int64_t riff_start, movi_list, odml_list;
00046     int64_t frames_hdr_all;
00047     int riff_id;
00048 } AVIContext;
00049 
00050 typedef struct  {
00051     int64_t frames_hdr_strm;
00052     int audio_strm_length;
00053     int packet_count;
00054     int entry;
00055 
00056     AVIIndex indexes;
00057 } AVIStream ;
00058 
00059 static inline AVIIentry* avi_get_ientry(AVIIndex* idx, int ent_id)
00060 {
00061     int cl = ent_id / AVI_INDEX_CLUSTER_SIZE;
00062     int id = ent_id % AVI_INDEX_CLUSTER_SIZE;
00063     return &idx->cluster[cl][id];
00064 }
00065 
00066 static int64_t avi_start_new_riff(AVFormatContext *s, ByteIOContext *pb,
00067                                   const char* riff_tag, const char* list_tag)
00068 {
00069     AVIContext *avi= s->priv_data;
00070     int64_t loff;
00071     int i;
00072 
00073     avi->riff_id++;
00074     for (i=0; i<s->nb_streams; i++){
00075         AVIStream *avist= s->streams[i]->priv_data;
00076         avist->indexes.entry = 0;
00077     }
00078 
00079     avi->riff_start = ff_start_tag(pb, "RIFF");
00080     put_tag(pb, riff_tag);
00081     loff = ff_start_tag(pb, "LIST");
00082     put_tag(pb, list_tag);
00083     return loff;
00084 }
00085 
00086 static char* avi_stream2fourcc(char* tag, int index, enum AVMediaType type)
00087 {
00088     tag[0] = '0';
00089     tag[1] = '0' + index;
00090     if (type == AVMEDIA_TYPE_VIDEO) {
00091         tag[2] = 'd';
00092         tag[3] = 'c';
00093     } else if (type == AVMEDIA_TYPE_SUBTITLE) {
00094         // note: this is not an official code
00095         tag[2] = 's';
00096         tag[3] = 'b';
00097     } else {
00098         tag[2] = 'w';
00099         tag[3] = 'b';
00100     }
00101     tag[4] = '\0';
00102     return tag;
00103 }
00104 
00105 static void avi_write_info_tag(ByteIOContext *pb, const char *tag, const char *str)
00106 {
00107     int len = strlen(str);
00108     if (len > 0) {
00109         len++;
00110         put_tag(pb, tag);
00111         put_le32(pb, len);
00112         put_strz(pb, str);
00113         if (len & 1)
00114             put_byte(pb, 0);
00115     }
00116 }
00117 
00118 static int avi_write_counters(AVFormatContext* s, int riff_id)
00119 {
00120     ByteIOContext *pb = s->pb;
00121     AVIContext *avi = s->priv_data;
00122     int n, au_byterate, au_ssize, au_scale, nb_frames = 0;
00123     int64_t file_size;
00124     AVCodecContext* stream;
00125 
00126     file_size = url_ftell(pb);
00127     for(n = 0; n < s->nb_streams; n++) {
00128         AVIStream *avist= s->streams[n]->priv_data;
00129 
00130         assert(avist->frames_hdr_strm);
00131         stream = s->streams[n]->codec;
00132         url_fseek(pb, avist->frames_hdr_strm, SEEK_SET);
00133         ff_parse_specific_params(stream, &au_byterate, &au_ssize, &au_scale);
00134         if(au_ssize == 0) {
00135             put_le32(pb, avist->packet_count);
00136         } else {
00137             put_le32(pb, avist->audio_strm_length / au_ssize);
00138         }
00139         if(stream->codec_type == AVMEDIA_TYPE_VIDEO)
00140             nb_frames = FFMAX(nb_frames, avist->packet_count);
00141     }
00142     if(riff_id == 1) {
00143         assert(avi->frames_hdr_all);
00144         url_fseek(pb, avi->frames_hdr_all, SEEK_SET);
00145         put_le32(pb, nb_frames);
00146     }
00147     url_fseek(pb, file_size, SEEK_SET);
00148 
00149     return 0;
00150 }
00151 
00152 static int avi_write_header(AVFormatContext *s)
00153 {
00154     AVIContext *avi = s->priv_data;
00155     ByteIOContext *pb = s->pb;
00156     int bitrate, n, i, nb_frames, au_byterate, au_ssize, au_scale;
00157     AVCodecContext *stream, *video_enc;
00158     int64_t list1, list2, strh, strf;
00159     AVMetadataTag *t = NULL;
00160 
00161     for(n=0;n<s->nb_streams;n++) {
00162         s->streams[n]->priv_data= av_mallocz(sizeof(AVIStream));
00163         if(!s->streams[n]->priv_data)
00164             return AVERROR(ENOMEM);
00165     }
00166 
00167     /* header list */
00168     avi->riff_id = 0;
00169     list1 = avi_start_new_riff(s, pb, "AVI ", "hdrl");
00170 
00171     /* avi header */
00172     put_tag(pb, "avih");
00173     put_le32(pb, 14 * 4);
00174     bitrate = 0;
00175 
00176     video_enc = NULL;
00177     for(n=0;n<s->nb_streams;n++) {
00178         stream = s->streams[n]->codec;
00179         bitrate += stream->bit_rate;
00180         if (stream->codec_type == AVMEDIA_TYPE_VIDEO)
00181             video_enc = stream;
00182     }
00183 
00184     nb_frames = 0;
00185 
00186     if(video_enc){
00187         put_le32(pb, (uint32_t)(INT64_C(1000000) * video_enc->time_base.num / video_enc->time_base.den));
00188     } else {
00189         put_le32(pb, 0);
00190     }
00191     put_le32(pb, bitrate / 8); /* XXX: not quite exact */
00192     put_le32(pb, 0); /* padding */
00193     if (url_is_streamed(pb))
00194         put_le32(pb, AVIF_TRUSTCKTYPE | AVIF_ISINTERLEAVED); /* flags */
00195     else
00196         put_le32(pb, AVIF_TRUSTCKTYPE | AVIF_HASINDEX | AVIF_ISINTERLEAVED); /* flags */
00197     avi->frames_hdr_all = url_ftell(pb); /* remember this offset to fill later */
00198     put_le32(pb, nb_frames); /* nb frames, filled later */
00199     put_le32(pb, 0); /* initial frame */
00200     put_le32(pb, s->nb_streams); /* nb streams */
00201     put_le32(pb, 1024 * 1024); /* suggested buffer size */
00202     if(video_enc){
00203         put_le32(pb, video_enc->width);
00204         put_le32(pb, video_enc->height);
00205     } else {
00206         put_le32(pb, 0);
00207         put_le32(pb, 0);
00208     }
00209     put_le32(pb, 0); /* reserved */
00210     put_le32(pb, 0); /* reserved */
00211     put_le32(pb, 0); /* reserved */
00212     put_le32(pb, 0); /* reserved */
00213 
00214     /* stream list */
00215     for(i=0;i<n;i++) {
00216         AVIStream *avist= s->streams[i]->priv_data;
00217         list2 = ff_start_tag(pb, "LIST");
00218         put_tag(pb, "strl");
00219 
00220         stream = s->streams[i]->codec;
00221 
00222         /* stream generic header */
00223         strh = ff_start_tag(pb, "strh");
00224         switch(stream->codec_type) {
00225         case AVMEDIA_TYPE_SUBTITLE:
00226             // XSUB subtitles behave like video tracks, other subtitles
00227             // are not (yet) supported.
00228             if (stream->codec_id != CODEC_ID_XSUB) break;
00229         case AVMEDIA_TYPE_VIDEO: put_tag(pb, "vids"); break;
00230         case AVMEDIA_TYPE_AUDIO: put_tag(pb, "auds"); break;
00231 //        case AVMEDIA_TYPE_TEXT : put_tag(pb, "txts"); break;
00232         case AVMEDIA_TYPE_DATA : put_tag(pb, "dats"); break;
00233         }
00234         if(stream->codec_type == AVMEDIA_TYPE_VIDEO ||
00235            stream->codec_id == CODEC_ID_XSUB)
00236             put_le32(pb, stream->codec_tag);
00237         else
00238             put_le32(pb, 1);
00239         put_le32(pb, 0); /* flags */
00240         put_le16(pb, 0); /* priority */
00241         put_le16(pb, 0); /* language */
00242         put_le32(pb, 0); /* initial frame */
00243 
00244         ff_parse_specific_params(stream, &au_byterate, &au_ssize, &au_scale);
00245 
00246         put_le32(pb, au_scale); /* scale */
00247         put_le32(pb, au_byterate); /* rate */
00248         av_set_pts_info(s->streams[i], 64, au_scale, au_byterate);
00249 
00250         put_le32(pb, 0); /* start */
00251         avist->frames_hdr_strm = url_ftell(pb); /* remember this offset to fill later */
00252         if (url_is_streamed(pb))
00253             put_le32(pb, AVI_MAX_RIFF_SIZE); /* FIXME: this may be broken, but who cares */
00254         else
00255             put_le32(pb, 0); /* length, XXX: filled later */
00256 
00257         /* suggested buffer size */ //FIXME set at the end to largest chunk
00258         if(stream->codec_type == AVMEDIA_TYPE_VIDEO)
00259             put_le32(pb, 1024 * 1024);
00260         else if(stream->codec_type == AVMEDIA_TYPE_AUDIO)
00261             put_le32(pb, 12 * 1024);
00262         else
00263             put_le32(pb, 0);
00264         put_le32(pb, -1); /* quality */
00265         put_le32(pb, au_ssize); /* sample size */
00266         put_le32(pb, 0);
00267         put_le16(pb, stream->width);
00268         put_le16(pb, stream->height);
00269         ff_end_tag(pb, strh);
00270 
00271       if(stream->codec_type != AVMEDIA_TYPE_DATA){
00272         strf = ff_start_tag(pb, "strf");
00273         switch(stream->codec_type) {
00274         case AVMEDIA_TYPE_SUBTITLE:
00275             // XSUB subtitles behave like video tracks, other subtitles
00276             // are not (yet) supported.
00277             if (stream->codec_id != CODEC_ID_XSUB) break;
00278         case AVMEDIA_TYPE_VIDEO:
00279             ff_put_bmp_header(pb, stream, ff_codec_bmp_tags, 0);
00280             break;
00281         case AVMEDIA_TYPE_AUDIO:
00282             if (ff_put_wav_header(pb, stream) < 0) {
00283                 return -1;
00284             }
00285             break;
00286         default:
00287             return -1;
00288         }
00289         ff_end_tag(pb, strf);
00290         if ((t = av_metadata_get(s->streams[i]->metadata, "strn", NULL, 0))) {
00291             avi_write_info_tag(s->pb, t->key, t->value);
00292             t = NULL;
00293         }
00294         //FIXME a limitation of metadata conversion system
00295         else if ((t = av_metadata_get(s->streams[i]->metadata, "INAM", NULL, 0))) {
00296             avi_write_info_tag(s->pb, "strn", t->value);
00297             t = NULL;
00298         }
00299       }
00300 
00301         if (!url_is_streamed(pb)) {
00302             unsigned char tag[5];
00303             int j;
00304 
00305             /* Starting to lay out AVI OpenDML master index.
00306              * We want to make it JUNK entry for now, since we'd
00307              * like to get away without making AVI an OpenDML one
00308              * for compatibility reasons.
00309              */
00310             avist->indexes.entry = avist->indexes.ents_allocated = 0;
00311             avist->indexes.indx_start = ff_start_tag(pb, "JUNK");
00312             put_le16(pb, 4);        /* wLongsPerEntry */
00313             put_byte(pb, 0);        /* bIndexSubType (0 == frame index) */
00314             put_byte(pb, 0);        /* bIndexType (0 == AVI_INDEX_OF_INDEXES) */
00315             put_le32(pb, 0);        /* nEntriesInUse (will fill out later on) */
00316             put_tag(pb, avi_stream2fourcc(&tag[0], i, stream->codec_type));
00317                                     /* dwChunkId */
00318             put_le64(pb, 0);        /* dwReserved[3]
00319             put_le32(pb, 0);           Must be 0.    */
00320             for (j=0; j < AVI_MASTER_INDEX_SIZE * 2; j++)
00321                  put_le64(pb, 0);
00322             ff_end_tag(pb, avist->indexes.indx_start);
00323         }
00324 
00325         if(   stream->codec_type == AVMEDIA_TYPE_VIDEO
00326            && s->streams[i]->sample_aspect_ratio.num>0
00327            && s->streams[i]->sample_aspect_ratio.den>0){
00328             int vprp= ff_start_tag(pb, "vprp");
00329             AVRational dar = av_mul_q(s->streams[i]->sample_aspect_ratio,
00330                                       (AVRational){stream->width, stream->height});
00331             int num, den;
00332             av_reduce(&num, &den, dar.num, dar.den, 0xFFFF);
00333 
00334             put_le32(pb, 0); //video format  = unknown
00335             put_le32(pb, 0); //video standard= unknown
00336             put_le32(pb, lrintf(1.0/av_q2d(stream->time_base)));
00337             put_le32(pb, stream->width );
00338             put_le32(pb, stream->height);
00339             put_le16(pb, den);
00340             put_le16(pb, num);
00341             put_le32(pb, stream->width );
00342             put_le32(pb, stream->height);
00343             put_le32(pb, 1); //progressive FIXME
00344 
00345             put_le32(pb, stream->height);
00346             put_le32(pb, stream->width );
00347             put_le32(pb, stream->height);
00348             put_le32(pb, stream->width );
00349             put_le32(pb, 0);
00350             put_le32(pb, 0);
00351 
00352             put_le32(pb, 0);
00353             put_le32(pb, 0);
00354             ff_end_tag(pb, vprp);
00355         }
00356 
00357         ff_end_tag(pb, list2);
00358     }
00359 
00360     if (!url_is_streamed(pb)) {
00361         /* AVI could become an OpenDML one, if it grows beyond 2Gb range */
00362         avi->odml_list = ff_start_tag(pb, "JUNK");
00363         put_tag(pb, "odml");
00364         put_tag(pb, "dmlh");
00365         put_le32(pb, 248);
00366         for (i = 0; i < 248; i+= 4)
00367              put_le32(pb, 0);
00368         ff_end_tag(pb, avi->odml_list);
00369     }
00370 
00371     ff_end_tag(pb, list1);
00372 
00373     list2 = ff_start_tag(pb, "LIST");
00374     put_tag(pb, "INFO");
00375     for (i = 0; *ff_avi_tags[i]; i++) {
00376         if ((t = av_metadata_get(s->metadata, ff_avi_tags[i], NULL, AV_METADATA_MATCH_CASE)))
00377             avi_write_info_tag(s->pb, t->key, t->value);
00378     }
00379     ff_end_tag(pb, list2);
00380 
00381     /* some padding for easier tag editing */
00382     list2 = ff_start_tag(pb, "JUNK");
00383     for (i = 0; i < 1016; i += 4)
00384         put_le32(pb, 0);
00385     ff_end_tag(pb, list2);
00386 
00387     avi->movi_list = ff_start_tag(pb, "LIST");
00388     put_tag(pb, "movi");
00389 
00390     put_flush_packet(pb);
00391 
00392     return 0;
00393 }
00394 
00395 static int avi_write_ix(AVFormatContext *s)
00396 {
00397     ByteIOContext *pb = s->pb;
00398     AVIContext *avi = s->priv_data;
00399     char tag[5];
00400     char ix_tag[] = "ix00";
00401     int i, j;
00402 
00403     assert(!url_is_streamed(pb));
00404 
00405     if (avi->riff_id > AVI_MASTER_INDEX_SIZE)
00406         return -1;
00407 
00408     for (i=0;i<s->nb_streams;i++) {
00409         AVIStream *avist= s->streams[i]->priv_data;
00410          int64_t ix, pos;
00411 
00412          avi_stream2fourcc(&tag[0], i, s->streams[i]->codec->codec_type);
00413          ix_tag[3] = '0' + i;
00414 
00415          /* Writing AVI OpenDML leaf index chunk */
00416          ix = url_ftell(pb);
00417          put_tag(pb, &ix_tag[0]);     /* ix?? */
00418          put_le32(pb, avist->indexes.entry * 8 + 24);
00419                                       /* chunk size */
00420          put_le16(pb, 2);             /* wLongsPerEntry */
00421          put_byte(pb, 0);             /* bIndexSubType (0 == frame index) */
00422          put_byte(pb, 1);             /* bIndexType (1 == AVI_INDEX_OF_CHUNKS) */
00423          put_le32(pb, avist->indexes.entry);
00424                                       /* nEntriesInUse */
00425          put_tag(pb, &tag[0]);        /* dwChunkId */
00426          put_le64(pb, avi->movi_list);/* qwBaseOffset */
00427          put_le32(pb, 0);             /* dwReserved_3 (must be 0) */
00428 
00429          for (j=0; j<avist->indexes.entry; j++) {
00430              AVIIentry* ie = avi_get_ientry(&avist->indexes, j);
00431              put_le32(pb, ie->pos + 8);
00432              put_le32(pb, ((uint32_t)ie->len & ~0x80000000) |
00433                           (ie->flags & 0x10 ? 0 : 0x80000000));
00434          }
00435          put_flush_packet(pb);
00436          pos = url_ftell(pb);
00437 
00438          /* Updating one entry in the AVI OpenDML master index */
00439          url_fseek(pb, avist->indexes.indx_start - 8, SEEK_SET);
00440          put_tag(pb, "indx");                 /* enabling this entry */
00441          url_fskip(pb, 8);
00442          put_le32(pb, avi->riff_id);          /* nEntriesInUse */
00443          url_fskip(pb, 16*avi->riff_id);
00444          put_le64(pb, ix);                    /* qwOffset */
00445          put_le32(pb, pos - ix);              /* dwSize */
00446          put_le32(pb, avist->indexes.entry); /* dwDuration */
00447 
00448          url_fseek(pb, pos, SEEK_SET);
00449     }
00450     return 0;
00451 }
00452 
00453 static int avi_write_idx1(AVFormatContext *s)
00454 {
00455     ByteIOContext *pb = s->pb;
00456     AVIContext *avi = s->priv_data;
00457     int64_t idx_chunk;
00458     int i;
00459     char tag[5];
00460 
00461     if (!url_is_streamed(pb)) {
00462         AVIStream *avist;
00463         AVIIentry* ie = 0, *tie;
00464         int empty, stream_id = -1;
00465 
00466         idx_chunk = ff_start_tag(pb, "idx1");
00467         for(i=0; i<s->nb_streams; i++){
00468             avist= s->streams[i]->priv_data;
00469             avist->entry=0;
00470         }
00471 
00472         do {
00473             empty = 1;
00474             for (i=0; i<s->nb_streams; i++) {
00475                 avist= s->streams[i]->priv_data;
00476                  if (avist->indexes.entry <= avist->entry)
00477                      continue;
00478 
00479                  tie = avi_get_ientry(&avist->indexes, avist->entry);
00480                  if (empty || tie->pos < ie->pos) {
00481                      ie = tie;
00482                      stream_id = i;
00483                  }
00484                  empty = 0;
00485             }
00486             if (!empty) {
00487                 avist= s->streams[stream_id]->priv_data;
00488                 avi_stream2fourcc(&tag[0], stream_id,
00489                                   s->streams[stream_id]->codec->codec_type);
00490                 put_tag(pb, &tag[0]);
00491                 put_le32(pb, ie->flags);
00492                 put_le32(pb, ie->pos);
00493                 put_le32(pb, ie->len);
00494                 avist->entry++;
00495             }
00496         } while (!empty);
00497         ff_end_tag(pb, idx_chunk);
00498 
00499         avi_write_counters(s, avi->riff_id);
00500     }
00501     return 0;
00502 }
00503 
00504 static int avi_write_packet(AVFormatContext *s, AVPacket *pkt)
00505 {
00506     AVIContext *avi = s->priv_data;
00507     ByteIOContext *pb = s->pb;
00508     unsigned char tag[5];
00509     unsigned int flags=0;
00510     const int stream_index= pkt->stream_index;
00511     AVIStream *avist= s->streams[stream_index]->priv_data;
00512     AVCodecContext *enc= s->streams[stream_index]->codec;
00513     int size= pkt->size;
00514 
00515 //    av_log(s, AV_LOG_DEBUG, "%"PRId64" %d %d\n", pkt->dts, avi->packet_count[stream_index], stream_index);
00516     while(enc->block_align==0 && pkt->dts != AV_NOPTS_VALUE && pkt->dts > avist->packet_count){
00517         AVPacket empty_packet;
00518 
00519         av_init_packet(&empty_packet);
00520         empty_packet.size= 0;
00521         empty_packet.data= NULL;
00522         empty_packet.stream_index= stream_index;
00523         avi_write_packet(s, &empty_packet);
00524 //        av_log(s, AV_LOG_DEBUG, "dup %"PRId64" %d\n", pkt->dts, avi->packet_count[stream_index]);
00525     }
00526     avist->packet_count++;
00527 
00528     // Make sure to put an OpenDML chunk when the file size exceeds the limits
00529     if (!url_is_streamed(pb) &&
00530         (url_ftell(pb) - avi->riff_start > AVI_MAX_RIFF_SIZE)) {
00531 
00532         avi_write_ix(s);
00533         ff_end_tag(pb, avi->movi_list);
00534 
00535         if (avi->riff_id == 1)
00536             avi_write_idx1(s);
00537 
00538         ff_end_tag(pb, avi->riff_start);
00539         avi->movi_list = avi_start_new_riff(s, pb, "AVIX", "movi");
00540     }
00541 
00542     avi_stream2fourcc(&tag[0], stream_index, enc->codec_type);
00543     if(pkt->flags&AV_PKT_FLAG_KEY)
00544         flags = 0x10;
00545     if (enc->codec_type == AVMEDIA_TYPE_AUDIO) {
00546        avist->audio_strm_length += size;
00547     }
00548 
00549     if (!url_is_streamed(s->pb)) {
00550         AVIIndex* idx = &avist->indexes;
00551         int cl = idx->entry / AVI_INDEX_CLUSTER_SIZE;
00552         int id = idx->entry % AVI_INDEX_CLUSTER_SIZE;
00553         if (idx->ents_allocated <= idx->entry) {
00554             idx->cluster = av_realloc(idx->cluster, (cl+1)*sizeof(void*));
00555             if (!idx->cluster)
00556                 return -1;
00557             idx->cluster[cl] = av_malloc(AVI_INDEX_CLUSTER_SIZE*sizeof(AVIIentry));
00558             if (!idx->cluster[cl])
00559                 return -1;
00560             idx->ents_allocated += AVI_INDEX_CLUSTER_SIZE;
00561         }
00562 
00563         idx->cluster[cl][id].flags = flags;
00564         idx->cluster[cl][id].pos = url_ftell(pb) - avi->movi_list;
00565         idx->cluster[cl][id].len = size;
00566         idx->entry++;
00567     }
00568 
00569     put_buffer(pb, tag, 4);
00570     put_le32(pb, size);
00571     put_buffer(pb, pkt->data, size);
00572     if (size & 1)
00573         put_byte(pb, 0);
00574 
00575     put_flush_packet(pb);
00576     return 0;
00577 }
00578 
00579 static int avi_write_trailer(AVFormatContext *s)
00580 {
00581     AVIContext *avi = s->priv_data;
00582     ByteIOContext *pb = s->pb;
00583     int res = 0;
00584     int i, j, n, nb_frames;
00585     int64_t file_size;
00586 
00587     if (!url_is_streamed(pb)){
00588         if (avi->riff_id == 1) {
00589             ff_end_tag(pb, avi->movi_list);
00590             res = avi_write_idx1(s);
00591             ff_end_tag(pb, avi->riff_start);
00592         } else {
00593             avi_write_ix(s);
00594             ff_end_tag(pb, avi->movi_list);
00595             ff_end_tag(pb, avi->riff_start);
00596 
00597             file_size = url_ftell(pb);
00598             url_fseek(pb, avi->odml_list - 8, SEEK_SET);
00599             put_tag(pb, "LIST"); /* Making this AVI OpenDML one */
00600             url_fskip(pb, 16);
00601 
00602             for (n=nb_frames=0;n<s->nb_streams;n++) {
00603                 AVCodecContext *stream = s->streams[n]->codec;
00604                 AVIStream *avist= s->streams[n]->priv_data;
00605 
00606                 if (stream->codec_type == AVMEDIA_TYPE_VIDEO) {
00607                     if (nb_frames < avist->packet_count)
00608                         nb_frames = avist->packet_count;
00609                 } else {
00610                     if (stream->codec_id == CODEC_ID_MP2 || stream->codec_id == CODEC_ID_MP3) {
00611                         nb_frames += avist->packet_count;
00612                     }
00613                 }
00614             }
00615             put_le32(pb, nb_frames);
00616             url_fseek(pb, file_size, SEEK_SET);
00617 
00618             avi_write_counters(s, avi->riff_id);
00619         }
00620     }
00621     put_flush_packet(pb);
00622 
00623     for (i=0; i<s->nb_streams; i++) {
00624          AVIStream *avist= s->streams[i]->priv_data;
00625          for (j=0; j<avist->indexes.ents_allocated/AVI_INDEX_CLUSTER_SIZE; j++)
00626               av_free(avist->indexes.cluster[j]);
00627          av_freep(&avist->indexes.cluster);
00628          avist->indexes.ents_allocated = avist->indexes.entry = 0;
00629     }
00630 
00631     return res;
00632 }
00633 
00634 AVOutputFormat avi_muxer = {
00635     "avi",
00636     NULL_IF_CONFIG_SMALL("AVI format"),
00637     "video/x-msvideo",
00638     "avi",
00639     sizeof(AVIContext),
00640     CODEC_ID_MP2,
00641     CODEC_ID_MPEG4,
00642     avi_write_header,
00643     avi_write_packet,
00644     avi_write_trailer,
00645     .codec_tag= (const AVCodecTag* const []){ff_codec_bmp_tags, ff_codec_wav_tags, 0},
00646     .flags= AVFMT_VARIABLE_FPS,
00647     .metadata_conv = ff_avi_metadata_conv,
00648 };

Generated on Fri Sep 16 2011 17:17:48 for FFmpeg by  doxygen 1.7.1