comparison mov.c @ 395:c1ed10f3b052 libavformat

cleanup frame_rate code, this may also fix some large file bugs
author michael
date Wed, 24 Mar 2004 19:03:20 +0000
parents 1674ed5ca2f0
children 42cc78ece67f
comparison
equal deleted inserted replaced
394:c7a3987b4462 395:c1ed10f3b052
14 * 14 *
15 * You should have received a copy of the GNU Lesser General Public 15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software 16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */ 18 */
19
20 #include <limits.h>
21
19 #include "avformat.h" 22 #include "avformat.h"
20 #include "avi.h" 23 #include "avi.h"
21 24
22 #ifdef CONFIG_ZLIB 25 #ifdef CONFIG_ZLIB
23 #include <zlib.h> 26 #include <zlib.h>
1166 static int mov_read_stts(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom) 1169 static int mov_read_stts(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
1167 { 1170 {
1168 AVStream *st = c->fc->streams[c->fc->nb_streams-1]; 1171 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
1169 //MOVStreamContext *sc = (MOVStreamContext *)st->priv_data; 1172 //MOVStreamContext *sc = (MOVStreamContext *)st->priv_data;
1170 int entries, i; 1173 int entries, i;
1171 int duration=0; 1174 int64_t duration=0;
1172 int total_sample_count=0; 1175 int64_t total_sample_count=0;
1173 1176
1174 print_atom("stts", atom); 1177 print_atom("stts", atom);
1175 1178
1176 get_byte(pb); /* version */ 1179 get_byte(pb); /* version */
1177 get_byte(pb); get_byte(pb); get_byte(pb); /* flags */ 1180 get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
1203 #endif 1206 #endif
1204 } 1207 }
1205 #endif 1208 #endif
1206 } 1209 }
1207 1210
1208 #define MAX(a,b) a>b?a:b
1209 #define MIN(a,b) a>b?b:a
1210 /*The stsd atom which contain codec type sometimes comes after the stts so we cannot check for codec_type*/ 1211 /*The stsd atom which contain codec type sometimes comes after the stts so we cannot check for codec_type*/
1211 if(duration>0) 1212 if(duration>0)
1212 { 1213 {
1213 //Find greatest common divisor to avoid overflow using the Euclidean Algorithm... 1214 av_reduce(
1214 uint32_t max=MAX(duration,total_sample_count); 1215 &st->codec.frame_rate,
1215 uint32_t min=MIN(duration,total_sample_count); 1216 &st->codec.frame_rate_base,
1216 uint32_t spare=max%min; 1217 c->streams[c->total_streams]->time_scale * total_sample_count,
1217 1218 duration,
1218 while(spare!=0) 1219 INT_MAX
1219 { 1220 );
1220 max=min;
1221 min=spare;
1222 spare=max%min;
1223 }
1224
1225 duration/=min;
1226 total_sample_count/=min;
1227
1228 //Only support constant frame rate. But lets calculate the average. Needed by .mp4-files created with nec e606 3g phone.
1229 //To get better precision, we use the duration as frame_rate_base
1230
1231 st->codec.frame_rate_base=duration;
1232 st->codec.frame_rate = c->streams[c->total_streams]->time_scale * total_sample_count;
1233 1221
1234 #ifdef DEBUG 1222 #ifdef DEBUG
1235 printf("FRAME RATE average (video or audio)= %f (tot sample count= %i ,tot dur= %i timescale=%d)\n", (float)st->codec.frame_rate/st->codec.frame_rate_base,total_sample_count,duration,c->streams[c->total_streams]->time_scale); 1223 printf("FRAME RATE average (video or audio)= %f (tot sample count= %i ,tot dur= %i timescale=%d)\n", (float)st->codec.frame_rate/st->codec.frame_rate_base,total_sample_count,duration,c->streams[c->total_streams]->time_scale);
1236 #endif 1224 #endif
1237 } 1225 }
1238 else 1226 else
1239 { 1227 {
1240 st->codec.frame_rate_base = 1; 1228 st->codec.frame_rate_base = 1;
1241 st->codec.frame_rate = c->streams[c->total_streams]->time_scale; 1229 st->codec.frame_rate = c->streams[c->total_streams]->time_scale;
1242 } 1230 }
1243 #undef MAX
1244 #undef MIN
1245 return 0; 1231 return 0;
1246 } 1232 }
1247 1233
1248 static int mov_read_trak(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom) 1234 static int mov_read_trak(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
1249 { 1235 {