1 
2 /** *************************************************************************
3         \file  aviIndexAvi
4         \brief write type1 avi with only one index
5 
6 		etc...
7 
8 
9     copyright            : (C) 2002-2012 by mean
10                            (C) Feb 2005 by GMV: ODML write support
11     GPL V2.0
12  ***************************************************************************/
13 
14 /***************************************************************************
15  *                                                                         *
16  *   This program is free software; you can redistribute it and/or modify  *
17  *   it under the terms of the GNU General Public License as published by  *
18  *   the Free Software Foundation; either version 2 of the License, or     *
19  *   (at your option) any later version.                                   *
20  *                                                                         *
21  ***************************************************************************/
22 #include "ADM_default.h"
23 #include "vector"
24 #include "aviIndexAvi.h"
25 #include "op_aviwrite.hxx"
26 #include "fourcc.h"
27 /**
28     \fn ctor
29 */
aviIndexAvi(aviWrite * father,AviListAvi * lst,uint64_t odmlChunk)30 aviIndexAvi::aviIndexAvi(aviWrite *father,AviListAvi *lst,uint64_t odmlChunk ): aviIndexBase(father,lst,odmlChunk)
31 {
32 
33     LMovie = new AviListAvi ("LIST", father->_file);
34     LMovie->Begin();
35     LMovie->Write32("movi");
36 }
37 /**
38     \fn dtor
39 */
40 
~aviIndexAvi()41 aviIndexAvi::~aviIndexAvi()
42 {
43     if(LMovie)
44         delete LMovie;
45     LMovie=NULL;
46 }
47 /**
48     \fn addVideoFrame
49 */
addVideoFrame(int len,uint32_t flags,const uint8_t * data)50 bool  aviIndexAvi::addVideoFrame(int len,uint32_t flags,const uint8_t *data)
51 {
52       IdxEntry entry;
53       uint64_t offset=LMovie->Tell ();
54       entry.fcc = fourccs[0];
55       entry.len = len;
56       entry.flags = flags;
57       entry.offset = offset;
58 
59       LMovie->WriteChunk (entry.fcc, len, data);
60       if(!myIndex.size())
61       {
62             // place holder...
63             uint64_t pos;
64             LMovie->writeDummyChunk(AVI_REGULAR_INDEX_CHUNK_SIZE,&pos);
65             placeHolder[0]=pos;
66       }
67       myIndex.push_back(entry);
68       nbVideoFrame++;
69       return true;
70 }
71 /**
72     \fn addAudioFrame
73 */
74 
addAudioFrame(int trackNo,int len,uint32_t flags,const uint8_t * data)75 bool  aviIndexAvi::addAudioFrame(int trackNo,int len,uint32_t flags,const uint8_t *data)
76 {
77     IdxEntry entry;
78     uint64_t offset=LMovie->Tell ();
79       entry.fcc = entry.fcc = fourccs[trackNo+1];
80       entry.len = len;
81       entry.flags = flags;
82       entry.offset = offset;
83       myIndex.push_back(entry);
84       LMovie->WriteChunk (entry.fcc, len, data);
85       audioSizeCount[trackNo]+=len;
86 
87       if(!audioFrameCount[trackNo])
88         {  // place holder...
89             uint64_t pos;
90             LMovie->writeDummyChunk(AVI_REGULAR_INDEX_CHUNK_SIZE,&pos);
91             placeHolder[1+trackNo]=pos;
92         }
93       audioFrameCount[trackNo]++;
94       return true;
95 }
96 /**
97     \fn writeIndex
98     \brief write the final index if needed
99 */
100 
writeIndex()101 bool  aviIndexAvi::writeIndex()
102 {
103   // End the movie atom
104   uint32_t base=LMovie->TellBegin()+8;
105   LMovie->End ();
106   delete LMovie;
107   LMovie = NULL;
108     // and start the idx1  = avi type1 index
109 
110     #define ix32(a)  memIo.write32(myIndex[i].a)
111     ADM_info("Writing type 1 Avi index\n");
112     // Write index
113     int curindex=myIndex.size();
114     AviListAvi *lst=new AviListAvi("idx1",_masterList->getFile());
115     ADMMemio memIo(4*4);
116     lst->Begin();
117     for (uint32_t i = 0; i < curindex; i++)
118     {
119         memIo.reset();
120         ix32(fcc);ix32(flags);memIo.write32(myIndex[i].offset-base);ix32(len);
121         lst->WriteMem (memIo);
122     }
123     lst->End();
124     delete lst;
125     lst=NULL;
126     _masterList->End();
127     delete _masterList;
128     _masterList=NULL;
129     ADM_info("Done writing type 1 Avi index\n");
130     return true;
131 }
132 /**
133     \fn getNbVideoFrameForHeaders
134     \brief for type1 avi, just return the actual # of frames
135 */
getNbVideoFrameForHeaders()136 int   aviIndexAvi::getNbVideoFrameForHeaders()
137 {
138         return nbVideoFrame;
139 }
140 
141 //-------------------------------------------------
142 /**
143     \fn ctor
144     \brief constructor for base class
145 */
aviIndexBase(aviWrite * father,AviListAvi * lst,uint64_t odmlChunk)146 aviIndexBase::aviIndexBase(aviWrite *father,AviListAvi *lst,uint64_t odmlChunk)
147 {
148       odmlChunkPosition=odmlChunk;
149       _masterList=lst;
150       nbVideoFrame=0;
151       memset(audioFrameCount,0,sizeof(audioFrameCount));
152       memset(audioSizeCount,0,sizeof(audioSizeCount));
153       nbAudioTrack=father->nb_audio;
154       currentBaseOffset=0;
155       fourccs[0]=fourCC::get ((uint8_t *)"00dc");
156         for(int i=0;i<ADM_AVI_MAX_AUDIO_TRACK;i++)
157         {
158             char txt[10]="01wb";
159             txt[1]+=i;
160             fourccs[i+1]=fourCC::get (( uint8_t *)txt);
161         }
162         for(int i=0;i<1+ADM_AVI_MAX_AUDIO_TRACK;i++)
163                openDmlHeaderPosition[i]=father->openDmlHeaderPosition[i];
164 
165 }
166 /**
167     \fn
168 */
switchToType2Needed(int len)169 bool         aviIndexAvi::switchToType2Needed(int len)
170 {
171     uint64_t delta=_masterList->Tell()-_masterList->TellBegin();
172     delta+=12*myIndex.size();
173     delta+=len;
174     delta+=2*1024*1024; // margin
175     if(delta>AVI_TYPE1_THRESHOLD) return true;
176     return false;
177 }
handOver()178 bool         aviIndexAvi::handOver()
179 {
180     return false;
181 }
182 
183 // EOF
184