|
Blender
V2.59
|
00001 /* 00002 * $Id: avi.c 38142 2011-07-06 10:19:04Z blendix $ 00003 * 00004 * This is external code. 00005 * 00006 * ***** BEGIN GPL LICENSE BLOCK ***** 00007 * 00008 * This program is free software; you can redistribute it and/or 00009 * modify it under the terms of the GNU General Public License 00010 * as published by the Free Software Foundation; either version 2 00011 * of the License, or (at your option) any later version. 00012 * 00013 * This program is distributed in the hope that it will be useful, 00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00016 * GNU General Public License for more details. 00017 * 00018 * You should have received a copy of the GNU General Public License 00019 * along with this program; if not, write to the Free Software Foundation, 00020 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 00021 * 00022 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. 00023 * All rights reserved. 00024 * 00025 * The Original Code is: all of this file. 00026 * 00027 * Contributor(s): none yet. 00028 * 00029 * ***** END GPL LICENSE BLOCK ***** 00030 * 00031 */ 00032 00038 #include <stdlib.h> 00039 #include <string.h> 00040 #include <stdarg.h> 00041 #include <stdio.h> 00042 #include <ctype.h> 00043 00044 #include "MEM_guardedalloc.h" 00045 #include "MEM_sys_types.h" 00046 00047 #include "BLI_winstuff.h" 00048 00049 #include "AVI_avi.h" 00050 #include "avi_intern.h" 00051 00052 #include "endian.h" 00053 00054 static int AVI_DEBUG=0; 00055 static char DEBUG_FCC[4]; 00056 00057 #define DEBUG_PRINT(x) if(AVI_DEBUG) printf("AVI DEBUG: " x); 00058 00059 /* local functions */ 00060 char *fcc_to_char (unsigned int fcc); 00061 char *tcc_to_char (unsigned int tcc); 00062 00063 00064 00065 /* implemetation */ 00066 00067 unsigned int GET_FCC (FILE *fp) { 00068 unsigned char tmp[4]; 00069 00070 tmp[0] = getc(fp); 00071 tmp[1] = getc(fp); 00072 tmp[2] = getc(fp); 00073 tmp[3] = getc(fp); 00074 00075 return FCC (tmp); 00076 } 00077 00078 unsigned int GET_TCC (FILE *fp) { 00079 char tmp[5]; 00080 00081 tmp[0] = getc(fp); 00082 tmp[1] = getc(fp); 00083 tmp[2] = 0; 00084 tmp[3] = 0; 00085 00086 return FCC (tmp); 00087 } 00088 00089 char *fcc_to_char (unsigned int fcc) { 00090 DEBUG_FCC[0]= (fcc)&0177; 00091 DEBUG_FCC[1]= (fcc>>8)&0177; 00092 DEBUG_FCC[2]= (fcc>>16)&0177; 00093 DEBUG_FCC[3]= (fcc>>24)&0177; 00094 00095 return DEBUG_FCC; 00096 } 00097 00098 char *tcc_to_char (unsigned int tcc) { 00099 DEBUG_FCC[0]= (tcc)&0177; 00100 DEBUG_FCC[1]= (tcc>>8)&0177; 00101 DEBUG_FCC[2]= 0; 00102 DEBUG_FCC[3]= 0; 00103 00104 return DEBUG_FCC; 00105 } 00106 00107 int AVI_get_stream (AviMovie *movie, int avist_type, int stream_num) { 00108 int cur_stream; 00109 00110 if (movie == NULL) 00111 return -AVI_ERROR_OPTION; 00112 00113 for (cur_stream=0; cur_stream < movie->header->Streams; cur_stream++) { 00114 if (movie->streams[cur_stream].sh.Type == avist_type) { 00115 if (stream_num == 0) 00116 return cur_stream; 00117 else 00118 stream_num--; 00119 } 00120 } 00121 00122 return -AVI_ERROR_FOUND; 00123 } 00124 00125 static int fcc_get_stream (int fcc) { 00126 char fccs[4]; 00127 00128 fccs[0] = fcc; 00129 fccs[1] = fcc>>8; 00130 fccs[2] = fcc>>16; 00131 fccs[3] = fcc>>24; 00132 00133 return 10*(fccs[0]-'0') + (fccs[1]-'0'); 00134 } 00135 00136 static int fcc_is_data (int fcc) { 00137 char fccs[4]; 00138 00139 fccs[0] = fcc; 00140 fccs[1] = fcc>>8; 00141 fccs[2] = fcc>>16; 00142 fccs[3] = fcc>>24; 00143 00144 if (!isdigit (fccs[0]) || !isdigit (fccs[1]) || (fccs[2] != 'd' && fccs[2] != 'w')) 00145 return 0; 00146 if (fccs[3] != 'b' && fccs[3] != 'c') 00147 return 0; 00148 00149 return 1; 00150 } 00151 00152 AviError AVI_print_error (AviError in_error) { 00153 int error; 00154 00155 if ((int) in_error < 0) 00156 error = -in_error; 00157 else 00158 error = in_error; 00159 00160 switch (error) { 00161 case AVI_ERROR_NONE: 00162 break; 00163 case AVI_ERROR_COMPRESSION: 00164 printf ("AVI ERROR: compressed in an unsupported format\n"); 00165 break; 00166 case AVI_ERROR_OPEN: 00167 printf ("AVI ERROR: could not open file\n"); 00168 break; 00169 case AVI_ERROR_READING: 00170 printf ("AVI ERROR: could not read from file\n"); 00171 break; 00172 case AVI_ERROR_WRITING: 00173 printf ("AVI ERROR: could not write to file\n"); 00174 break; 00175 case AVI_ERROR_FORMAT: 00176 printf ("AVI ERROR: file is in an illegal or unrecognized format\n"); 00177 break; 00178 case AVI_ERROR_ALLOC: 00179 printf ("AVI ERROR: error encountered while allocating memory\n"); 00180 break; 00181 case AVI_ERROR_OPTION: 00182 printf ("AVI ERROR: program made illegal request\n"); 00183 break; 00184 case AVI_ERROR_FOUND: 00185 printf ("AVI ERROR: movie did not contain expected item\n"); 00186 break; 00187 default: 00188 break; 00189 } 00190 00191 return in_error; 00192 } 00193 /* 00194 void AVI_set_debug (int mode) { 00195 AVI_DEBUG= mode; 00196 } 00197 */ 00198 /* 00199 int AVI_is_avi (char *name) { 00200 FILE *fp; 00201 int ret; 00202 00203 fp = fopen (name, "rb"); 00204 if (fp == NULL) 00205 return 0; 00206 00207 if (GET_FCC (fp) != FCC("RIFF") || 00208 !GET_FCC (fp) || 00209 GET_FCC (fp) != FCC("AVI ")) { 00210 ret = 0; 00211 } else { 00212 ret = 1; 00213 } 00214 00215 fclose(fp); 00216 return ret; 00217 } 00218 */ 00219 00220 int AVI_is_avi (const char *name) { 00221 int temp, fcca, j; 00222 AviMovie movie= {NULL}; 00223 AviMainHeader header; 00224 AviBitmapInfoHeader bheader; 00225 int movie_tracks = 0; 00226 00227 DEBUG_PRINT("opening movie\n"); 00228 00229 movie.type = AVI_MOVIE_READ; 00230 movie.fp = fopen (name, "rb"); 00231 movie.offset_table = NULL; 00232 00233 if (movie.fp == NULL) 00234 return 0; 00235 00236 if (GET_FCC (movie.fp) != FCC("RIFF") || 00237 !(movie.size = GET_FCC (movie.fp))) { 00238 fclose(movie.fp); 00239 return 0; 00240 } 00241 00242 movie.header = &header; 00243 00244 if (GET_FCC (movie.fp) != FCC("AVI ") || 00245 GET_FCC (movie.fp) != FCC("LIST") || 00246 !GET_FCC (movie.fp) || 00247 GET_FCC (movie.fp) != FCC("hdrl") || 00248 (movie.header->fcc = GET_FCC (movie.fp)) != FCC("avih") || 00249 !(movie.header->size = GET_FCC (movie.fp))) { 00250 DEBUG_PRINT("bad initial header info\n"); 00251 fclose(movie.fp); 00252 return 0; 00253 } 00254 00255 movie.header->MicroSecPerFrame = GET_FCC(movie.fp); 00256 movie.header->MaxBytesPerSec = GET_FCC(movie.fp); 00257 movie.header->PaddingGranularity = GET_FCC(movie.fp); 00258 movie.header->Flags = GET_FCC(movie.fp); 00259 movie.header->TotalFrames = GET_FCC(movie.fp); 00260 movie.header->InitialFrames = GET_FCC(movie.fp); 00261 movie.header->Streams = GET_FCC(movie.fp); 00262 movie.header->SuggestedBufferSize = GET_FCC(movie.fp); 00263 movie.header->Width = GET_FCC(movie.fp); 00264 movie.header->Height = GET_FCC(movie.fp); 00265 movie.header->Reserved[0] = GET_FCC(movie.fp); 00266 movie.header->Reserved[1] = GET_FCC(movie.fp); 00267 movie.header->Reserved[2] = GET_FCC(movie.fp); 00268 movie.header->Reserved[3] = GET_FCC(movie.fp); 00269 00270 fseek (movie.fp, movie.header->size-14*4, SEEK_CUR); 00271 00272 if (movie.header->Streams < 1) { 00273 DEBUG_PRINT("streams less than 1\n"); 00274 fclose(movie.fp); 00275 return 0; 00276 } 00277 00278 movie.streams = (AviStreamRec *) MEM_callocN (sizeof(AviStreamRec) * movie.header->Streams, "moviestreams"); 00279 00280 for (temp=0; temp < movie.header->Streams; temp++) { 00281 00282 if (GET_FCC(movie.fp) != FCC("LIST") || 00283 !GET_FCC (movie.fp) || 00284 GET_FCC (movie.fp) != FCC ("strl") || 00285 (movie.streams[temp].sh.fcc = GET_FCC (movie.fp)) != FCC ("strh") || 00286 !(movie.streams[temp].sh.size = GET_FCC (movie.fp))) { 00287 DEBUG_PRINT("bad stream header information\n"); 00288 00289 MEM_freeN(movie.streams); 00290 fclose(movie.fp); 00291 return 0; 00292 } 00293 00294 movie.streams[temp].sh.Type = GET_FCC (movie.fp); 00295 movie.streams[temp].sh.Handler = GET_FCC (movie.fp); 00296 00297 fcca = movie.streams[temp].sh.Handler; 00298 00299 if (movie.streams[temp].sh.Type == FCC("vids")) { 00300 if (fcca == FCC ("DIB ") || 00301 fcca == FCC ("RGB ") || 00302 fcca == FCC ("rgb ") || 00303 fcca == FCC ("RAW ") || 00304 fcca == 0) { 00305 movie.streams[temp].format = AVI_FORMAT_AVI_RGB; 00306 } else if (fcca == FCC ("mjpg")||fcca == FCC ("MJPG")) { 00307 movie.streams[temp].format = AVI_FORMAT_MJPEG; 00308 } else { 00309 MEM_freeN(movie.streams); 00310 fclose(movie.fp); 00311 return 0; 00312 } 00313 movie_tracks++; 00314 } 00315 00316 movie.streams[temp].sh.Flags = GET_FCC (movie.fp); 00317 movie.streams[temp].sh.Priority = GET_TCC (movie.fp); 00318 movie.streams[temp].sh.Language = GET_TCC (movie.fp); 00319 movie.streams[temp].sh.InitialFrames = GET_FCC (movie.fp); 00320 movie.streams[temp].sh.Scale = GET_FCC (movie.fp); 00321 movie.streams[temp].sh.Rate = GET_FCC (movie.fp); 00322 movie.streams[temp].sh.Start = GET_FCC (movie.fp); 00323 movie.streams[temp].sh.Length = GET_FCC (movie.fp); 00324 movie.streams[temp].sh.SuggestedBufferSize = GET_FCC (movie.fp); 00325 movie.streams[temp].sh.Quality = GET_FCC (movie.fp); 00326 movie.streams[temp].sh.SampleSize = GET_FCC (movie.fp); 00327 movie.streams[temp].sh.left = GET_TCC (movie.fp); 00328 movie.streams[temp].sh.top = GET_TCC (movie.fp); 00329 movie.streams[temp].sh.right = GET_TCC (movie.fp); 00330 movie.streams[temp].sh.bottom = GET_TCC (movie.fp); 00331 00332 fseek (movie.fp, movie.streams[temp].sh.size-14*4, SEEK_CUR); 00333 00334 if (GET_FCC (movie.fp) != FCC("strf")) { 00335 DEBUG_PRINT("no stream format information\n"); 00336 MEM_freeN(movie.streams); 00337 fclose(movie.fp); 00338 return 0; 00339 } 00340 00341 movie.streams[temp].sf_size= GET_FCC(movie.fp); 00342 if (movie.streams[temp].sh.Type == FCC("vids")) { 00343 j = movie.streams[temp].sf_size - (sizeof(AviBitmapInfoHeader) - 8); 00344 if (j >= 0) { 00345 AviBitmapInfoHeader *bi; 00346 00347 movie.streams[temp].sf= &bheader; 00348 bi= (AviBitmapInfoHeader *) movie.streams[temp].sf; 00349 00350 bi->fcc= FCC("strf"); 00351 bi->size= movie.streams[temp].sf_size; 00352 bi->Size= GET_FCC(movie.fp); 00353 bi->Width= GET_FCC(movie.fp); 00354 bi->Height= GET_FCC(movie.fp); 00355 bi->Planes= GET_TCC(movie.fp); 00356 bi->BitCount= GET_TCC(movie.fp); 00357 bi->Compression= GET_FCC(movie.fp); 00358 bi->SizeImage= GET_FCC(movie.fp); 00359 bi->XPelsPerMeter= GET_FCC(movie.fp); 00360 bi->YPelsPerMeter= GET_FCC(movie.fp); 00361 bi->ClrUsed= GET_FCC(movie.fp); 00362 bi->ClrImportant= GET_FCC(movie.fp); 00363 00364 fcca = bi->Compression; 00365 00366 if ( movie.streams[temp].format == 00367 AVI_FORMAT_AVI_RGB) { 00368 if (fcca == FCC ("DIB ") || 00369 fcca == FCC ("RGB ") || 00370 fcca == FCC ("rgb ") || 00371 fcca == FCC ("RAW ") || 00372 fcca == 0 ) { 00373 } else if ( fcca == FCC ("mjpg") || 00374 fcca == FCC ("MJPG")) { 00375 movie.streams[temp].format = AVI_FORMAT_MJPEG; 00376 } else { 00377 MEM_freeN(movie.streams); 00378 fclose(movie.fp); 00379 return 0; 00380 } 00381 } 00382 00383 } 00384 if (j > 0) fseek (movie.fp, j, SEEK_CUR); 00385 } else fseek (movie.fp, movie.streams[temp].sf_size, SEEK_CUR); 00386 00387 /* Walk to the next LIST */ 00388 while (GET_FCC (movie.fp) != FCC("LIST")) { 00389 temp= GET_FCC (movie.fp); 00390 if (temp<0 || ftell(movie.fp) > movie.size) { 00391 DEBUG_PRINT("incorrect size in header or error in AVI\n"); 00392 00393 MEM_freeN(movie.streams); 00394 fclose(movie.fp); 00395 return 0; 00396 } 00397 fseek(movie.fp, temp, SEEK_CUR); 00398 } 00399 00400 fseek(movie.fp, -4L, SEEK_CUR); 00401 } 00402 00403 MEM_freeN(movie.streams); 00404 fclose(movie.fp); 00405 00406 /* at least one video track is needed */ 00407 return (movie_tracks != 0); 00408 00409 } 00410 00411 AviError AVI_open_movie (const char *name, AviMovie *movie) { 00412 int temp, fcca, size, j; 00413 00414 DEBUG_PRINT("opening movie\n"); 00415 00416 memset(movie, 0, sizeof(AviMovie)); 00417 00418 movie->type = AVI_MOVIE_READ; 00419 movie->fp = fopen (name, "rb"); 00420 movie->offset_table = NULL; 00421 00422 if (movie->fp == NULL) 00423 return AVI_ERROR_OPEN; 00424 00425 if (GET_FCC (movie->fp) != FCC("RIFF") || 00426 !(movie->size = GET_FCC (movie->fp))) 00427 return AVI_ERROR_FORMAT; 00428 00429 movie->header = (AviMainHeader *) MEM_mallocN (sizeof (AviMainHeader), "movieheader"); 00430 00431 if (GET_FCC (movie->fp) != FCC("AVI ") || 00432 GET_FCC (movie->fp) != FCC("LIST") || 00433 !GET_FCC (movie->fp) || 00434 GET_FCC (movie->fp) != FCC("hdrl") || 00435 (movie->header->fcc = GET_FCC (movie->fp)) != FCC("avih") || 00436 !(movie->header->size = GET_FCC (movie->fp))) { 00437 DEBUG_PRINT("bad initial header info\n"); 00438 return AVI_ERROR_FORMAT; 00439 } 00440 00441 movie->header->MicroSecPerFrame = GET_FCC(movie->fp); 00442 movie->header->MaxBytesPerSec = GET_FCC(movie->fp); 00443 movie->header->PaddingGranularity = GET_FCC(movie->fp); 00444 movie->header->Flags = GET_FCC(movie->fp); 00445 movie->header->TotalFrames = GET_FCC(movie->fp); 00446 movie->header->InitialFrames = GET_FCC(movie->fp); 00447 movie->header->Streams = GET_FCC(movie->fp); 00448 movie->header->SuggestedBufferSize = GET_FCC(movie->fp); 00449 movie->header->Width = GET_FCC(movie->fp); 00450 movie->header->Height = GET_FCC(movie->fp); 00451 movie->header->Reserved[0] = GET_FCC(movie->fp); 00452 movie->header->Reserved[1] = GET_FCC(movie->fp); 00453 movie->header->Reserved[2] = GET_FCC(movie->fp); 00454 movie->header->Reserved[3] = GET_FCC(movie->fp); 00455 00456 fseek (movie->fp, movie->header->size-14*4, SEEK_CUR); 00457 00458 if (movie->header->Streams < 1) { 00459 DEBUG_PRINT("streams less than 1\n"); 00460 return AVI_ERROR_FORMAT; 00461 } 00462 00463 movie->streams = (AviStreamRec *) MEM_callocN (sizeof(AviStreamRec) * movie->header->Streams, "moviestreams"); 00464 00465 for (temp=0; temp < movie->header->Streams; temp++) { 00466 00467 if (GET_FCC(movie->fp) != FCC("LIST") || 00468 !GET_FCC (movie->fp) || 00469 GET_FCC (movie->fp) != FCC ("strl") || 00470 (movie->streams[temp].sh.fcc = GET_FCC (movie->fp)) != FCC ("strh") || 00471 !(movie->streams[temp].sh.size = GET_FCC (movie->fp))) { 00472 DEBUG_PRINT("bad stream header information\n"); 00473 return AVI_ERROR_FORMAT; 00474 } 00475 00476 movie->streams[temp].sh.Type = GET_FCC (movie->fp); 00477 movie->streams[temp].sh.Handler = GET_FCC (movie->fp); 00478 00479 fcca = movie->streams[temp].sh.Handler; 00480 00481 if (movie->streams[temp].sh.Type == FCC("vids")) { 00482 if (fcca == FCC ("DIB ") || 00483 fcca == FCC ("RGB ") || 00484 fcca == FCC ("rgb ") || 00485 fcca == FCC ("RAW ") || 00486 fcca == 0) { 00487 movie->streams[temp].format = AVI_FORMAT_AVI_RGB; 00488 } else if (fcca == FCC ("mjpg")||fcca == FCC ("MJPG")) { 00489 movie->streams[temp].format = AVI_FORMAT_MJPEG; 00490 } else { 00491 return AVI_ERROR_COMPRESSION; 00492 } 00493 } 00494 00495 movie->streams[temp].sh.Flags = GET_FCC (movie->fp); 00496 movie->streams[temp].sh.Priority = GET_TCC (movie->fp); 00497 movie->streams[temp].sh.Language = GET_TCC (movie->fp); 00498 movie->streams[temp].sh.InitialFrames = GET_FCC (movie->fp); 00499 movie->streams[temp].sh.Scale = GET_FCC (movie->fp); 00500 movie->streams[temp].sh.Rate = GET_FCC (movie->fp); 00501 movie->streams[temp].sh.Start = GET_FCC (movie->fp); 00502 movie->streams[temp].sh.Length = GET_FCC (movie->fp); 00503 movie->streams[temp].sh.SuggestedBufferSize = GET_FCC (movie->fp); 00504 movie->streams[temp].sh.Quality = GET_FCC (movie->fp); 00505 movie->streams[temp].sh.SampleSize = GET_FCC (movie->fp); 00506 movie->streams[temp].sh.left = GET_TCC (movie->fp); 00507 movie->streams[temp].sh.top = GET_TCC (movie->fp); 00508 movie->streams[temp].sh.right = GET_TCC (movie->fp); 00509 movie->streams[temp].sh.bottom = GET_TCC (movie->fp); 00510 00511 fseek (movie->fp, movie->streams[temp].sh.size-14*4, SEEK_CUR); 00512 00513 if (GET_FCC (movie->fp) != FCC("strf")) { 00514 DEBUG_PRINT("no stream format information\n"); 00515 return AVI_ERROR_FORMAT; 00516 } 00517 00518 movie->streams[temp].sf_size= GET_FCC(movie->fp); 00519 if (movie->streams[temp].sh.Type == FCC("vids")) { 00520 j = movie->streams[temp].sf_size - (sizeof(AviBitmapInfoHeader) - 8); 00521 if (j >= 0) { 00522 AviBitmapInfoHeader *bi; 00523 00524 movie->streams[temp].sf= MEM_mallocN(sizeof(AviBitmapInfoHeader), "streamformat"); 00525 00526 bi= (AviBitmapInfoHeader *) movie->streams[temp].sf; 00527 00528 bi->fcc= FCC("strf"); 00529 bi->size= movie->streams[temp].sf_size; 00530 bi->Size= GET_FCC(movie->fp); 00531 bi->Width= GET_FCC(movie->fp); 00532 bi->Height= GET_FCC(movie->fp); 00533 bi->Planes= GET_TCC(movie->fp); 00534 bi->BitCount= GET_TCC(movie->fp); 00535 bi->Compression= GET_FCC(movie->fp); 00536 bi->SizeImage= GET_FCC(movie->fp); 00537 bi->XPelsPerMeter= GET_FCC(movie->fp); 00538 bi->YPelsPerMeter= GET_FCC(movie->fp); 00539 bi->ClrUsed= GET_FCC(movie->fp); 00540 bi->ClrImportant= GET_FCC(movie->fp); 00541 00542 fcca = bi->Compression; 00543 00544 if ( movie->streams[temp].format == 00545 AVI_FORMAT_AVI_RGB) { 00546 if (fcca == FCC ("DIB ") || 00547 fcca == FCC ("RGB ") || 00548 fcca == FCC ("rgb ") || 00549 fcca == FCC ("RAW ") || 00550 fcca == 0 ) { 00551 } else if ( fcca == FCC ("mjpg") || 00552 fcca == FCC ("MJPG")) { 00553 movie->streams[temp].format = AVI_FORMAT_MJPEG; 00554 } else { 00555 return AVI_ERROR_COMPRESSION; 00556 } 00557 } 00558 00559 } 00560 if (j > 0) fseek (movie->fp, j, SEEK_CUR); 00561 } else fseek (movie->fp, movie->streams[temp].sf_size, SEEK_CUR); 00562 00563 /* Walk to the next LIST */ 00564 while (GET_FCC (movie->fp) != FCC("LIST")) { 00565 temp= GET_FCC (movie->fp); 00566 if (temp<0 || ftell(movie->fp) > movie->size) { 00567 DEBUG_PRINT("incorrect size in header or error in AVI\n"); 00568 return AVI_ERROR_FORMAT; 00569 } 00570 fseek(movie->fp, temp, SEEK_CUR); 00571 } 00572 00573 fseek(movie->fp, -4L, SEEK_CUR); 00574 } 00575 00576 while (1) { 00577 temp = GET_FCC (movie->fp); 00578 size = GET_FCC (movie->fp); 00579 00580 if (size == 0) 00581 break; 00582 00583 if (temp == FCC("LIST")) { 00584 if (GET_FCC(movie->fp) == FCC ("movi")) 00585 break; 00586 else 00587 fseek (movie->fp, size-4, SEEK_CUR); 00588 } else { 00589 fseek (movie->fp, size, SEEK_CUR); 00590 } 00591 if (ftell(movie->fp) > movie->size) { 00592 DEBUG_PRINT("incorrect size in header or error in AVI\n"); 00593 return AVI_ERROR_FORMAT; 00594 } 00595 } 00596 00597 movie->movi_offset = ftell (movie->fp); 00598 movie->read_offset = movie->movi_offset; 00599 00600 /* Read in the index if the file has one, otherwise create one */ 00601 if (movie->header->Flags & AVIF_HASINDEX) { 00602 fseek(movie->fp, size-4, SEEK_CUR); 00603 00604 if (GET_FCC(movie->fp) != FCC("idx1")) { 00605 DEBUG_PRINT("bad index informatio\n"); 00606 return AVI_ERROR_FORMAT; 00607 } 00608 00609 movie->index_entries = GET_FCC (movie->fp)/sizeof(AviIndexEntry); 00610 if (movie->index_entries == 0) { 00611 DEBUG_PRINT("no index entries\n"); 00612 return AVI_ERROR_FORMAT; 00613 } 00614 00615 movie->entries = (AviIndexEntry *) MEM_mallocN (movie->index_entries * sizeof(AviIndexEntry),"movieentries"); 00616 00617 for (temp=0; temp < movie->index_entries; temp++) { 00618 movie->entries[temp].ChunkId = GET_FCC (movie->fp); 00619 movie->entries[temp].Flags = GET_FCC (movie->fp); 00620 movie->entries[temp].Offset = GET_FCC (movie->fp); 00621 movie->entries[temp].Size = GET_FCC (movie->fp); 00622 00623 if (AVI_DEBUG) printf ("Index entry %04d: ChunkId:%s Flags:%d Offset:%d Size:%d\n", temp, fcc_to_char(movie->entries[temp].ChunkId), movie->entries[temp].Flags, movie->entries[temp].Offset, movie->entries[temp].Size); 00624 } 00625 00626 /* Some AVI's have offset entries in absolute coordinates 00627 * instead of an offset from the movie beginning... this is... 00628 * wacky, but we need to handle it. The wacky offset always 00629 * starts at movi_offset it seems... so we'll check that. 00630 * Note the the offset needs an extra 4 bytes for some 00631 * undetermined reason */ 00632 00633 if (movie->entries[0].Offset == movie->movi_offset) 00634 movie->read_offset= 4; 00635 } 00636 00637 DEBUG_PRINT("movie succesfully opened\n"); 00638 return AVI_ERROR_NONE; 00639 } 00640 00641 void *AVI_read_frame (AviMovie *movie, AviFormat format, int frame, int stream) { 00642 int cur_frame=-1, temp, i=0, rewind=1; 00643 void *buffer; 00644 00645 /* Retrieve the record number of the desired frame in the index 00646 If a chunk has Size 0 we need to rewind to previous frame */ 00647 while(rewind && frame > -1) { 00648 i=0; 00649 cur_frame=-1; 00650 rewind = 0; 00651 00652 while (cur_frame < frame && i < movie->index_entries) { 00653 if (fcc_is_data (movie->entries[i].ChunkId) && 00654 fcc_get_stream (movie->entries[i].ChunkId) == stream) { 00655 if ((cur_frame == frame -1) && (movie->entries[i].Size == 0)) { 00656 rewind = 1; 00657 frame = frame -1; 00658 } else { 00659 cur_frame++; 00660 } 00661 } 00662 i++; 00663 } 00664 } 00665 00666 if (cur_frame != frame) return NULL; 00667 00668 00669 fseek (movie->fp, movie->read_offset + movie->entries[i-1].Offset, SEEK_SET); 00670 00671 temp = GET_FCC(movie->fp); 00672 buffer = MEM_mallocN (temp,"readbuffer"); 00673 00674 if (fread (buffer, 1, temp, movie->fp) != temp) { 00675 MEM_freeN(buffer); 00676 00677 return NULL; 00678 } 00679 00680 buffer = avi_format_convert (movie, stream, buffer, movie->streams[stream].format, format, &temp); 00681 00682 return buffer; 00683 } 00684 00685 AviError AVI_close (AviMovie *movie) { 00686 int i; 00687 00688 fclose (movie->fp); 00689 00690 for (i=0; i < movie->header->Streams; i++) { 00691 if (movie->streams[i].sf != NULL) 00692 MEM_freeN (movie->streams[i].sf); 00693 } 00694 00695 if (movie->header != NULL) 00696 MEM_freeN (movie->header); 00697 if (movie->streams!= NULL) 00698 MEM_freeN (movie->streams); 00699 if (movie->entries != NULL) 00700 MEM_freeN (movie->entries); 00701 if (movie->offset_table != NULL) 00702 MEM_freeN (movie->offset_table); 00703 00704 return AVI_ERROR_NONE; 00705 } 00706 00707 AviError AVI_open_compress (char *name, AviMovie *movie, int streams, ...) { 00708 va_list ap; 00709 AviList list; 00710 AviChunk chunk; 00711 int i; 00712 int64_t header_pos1, header_pos2; 00713 int64_t stream_pos1, stream_pos2; 00714 00715 movie->type = AVI_MOVIE_WRITE; 00716 movie->fp = fopen (name, "wb"); 00717 00718 movie->index_entries = 0; 00719 00720 if (movie->fp == NULL) 00721 return AVI_ERROR_OPEN; 00722 00723 movie->offset_table = (int64_t *) MEM_mallocN ((1+streams*2) * sizeof (int64_t),"offsettable"); 00724 00725 for (i=0; i < 1 + streams*2; i++) 00726 movie->offset_table[i] = -1L; 00727 00728 movie->entries = NULL; 00729 00730 movie->header = (AviMainHeader *) MEM_mallocN (sizeof(AviMainHeader),"movieheader"); 00731 00732 movie->header->fcc = FCC("avih"); 00733 movie->header->size = 56; 00734 movie->header->MicroSecPerFrame = 66667; 00735 movie->header->MaxBytesPerSec = 0; 00736 movie->header->PaddingGranularity = 0; 00737 movie->header->Flags = AVIF_HASINDEX | AVIF_MUSTUSEINDEX; 00738 movie->header->TotalFrames = 0; 00739 movie->header->InitialFrames = 0; 00740 movie->header->Streams = streams; 00741 movie->header->SuggestedBufferSize = 0; 00742 movie->header->Width = 0; 00743 movie->header->Height = 0; 00744 movie->header->Reserved[0] = 0; 00745 movie->header->Reserved[1] = 0; 00746 movie->header->Reserved[2] = 0; 00747 movie->header->Reserved[3] = 0; 00748 00749 movie->streams = (AviStreamRec *) MEM_mallocN (sizeof(AviStreamRec) * movie->header->Streams,"moviestreams"); 00750 00751 va_start (ap, streams); 00752 00753 for (i=0; i < movie->header->Streams; i++) { 00754 movie->streams[i].format = va_arg(ap, AviFormat); 00755 00756 movie->streams[i].sh.fcc = FCC ("strh"); 00757 movie->streams[i].sh.size = 56; 00758 movie->streams[i].sh.Type = avi_get_format_type (movie->streams[i].format); 00759 if (movie->streams[i].sh.Type == 0) 00760 return AVI_ERROR_FORMAT; 00761 00762 movie->streams[i].sh.Handler = avi_get_format_fcc (movie->streams[i].format); 00763 if (movie->streams[i].sh.Handler == 0) 00764 return AVI_ERROR_FORMAT; 00765 00766 movie->streams[i].sh.Flags = 0; 00767 movie->streams[i].sh.Priority = 0; 00768 movie->streams[i].sh.Language = 0; 00769 movie->streams[i].sh.InitialFrames = 0; 00770 movie->streams[i].sh.Scale = 66667; 00771 movie->streams[i].sh.Rate = 1000000; 00772 movie->streams[i].sh.Start = 0; 00773 movie->streams[i].sh.Length = 0; 00774 movie->streams[i].sh.SuggestedBufferSize = 0; 00775 movie->streams[i].sh.Quality = 10000; 00776 movie->streams[i].sh.SampleSize = 0; 00777 movie->streams[i].sh.left = 0; 00778 movie->streams[i].sh.top = 0; 00779 movie->streams[i].sh.right = 0; 00780 movie->streams[i].sh.bottom = 0; 00781 00782 if (movie->streams[i].sh.Type == FCC("vids")) { 00783 /* 00784 if (movie->streams[i].format == AVI_FORMAT_MJPEG) { 00785 movie->streams[i].sf = MEM_mallocN (sizeof(AviBitmapInfoHeader) 00786 + sizeof(AviMJPEGUnknown),"moviestreamformatL"); 00787 movie->streams[i].sf_size = sizeof(AviBitmapInfoHeader) + sizeof(AviMJPEGUnknown); 00788 } else { 00789 */ 00790 movie->streams[i].sf = MEM_mallocN (sizeof(AviBitmapInfoHeader), "moviestreamformatS"); 00791 movie->streams[i].sf_size = sizeof(AviBitmapInfoHeader); 00792 00793 ((AviBitmapInfoHeader *) movie->streams[i].sf)->fcc = FCC ("strf"); 00794 ((AviBitmapInfoHeader *) movie->streams[i].sf)->size = movie->streams[i].sf_size - 8; 00795 ((AviBitmapInfoHeader *) movie->streams[i].sf)->Size = movie->streams[i].sf_size - 8; 00796 ((AviBitmapInfoHeader *) movie->streams[i].sf)->Width = 0; 00797 ((AviBitmapInfoHeader *) movie->streams[i].sf)->Height = 0; 00798 ((AviBitmapInfoHeader *) movie->streams[i].sf)->Planes = 1; 00799 ((AviBitmapInfoHeader *) movie->streams[i].sf)->BitCount = 24; 00800 ((AviBitmapInfoHeader *) movie->streams[i].sf)->Compression = avi_get_format_compression (movie->streams[i].format); 00801 ((AviBitmapInfoHeader *) movie->streams[i].sf)->SizeImage = 0; 00802 ((AviBitmapInfoHeader *) movie->streams[i].sf)->XPelsPerMeter = 0; 00803 ((AviBitmapInfoHeader *) movie->streams[i].sf)->YPelsPerMeter = 0; 00804 ((AviBitmapInfoHeader *) movie->streams[i].sf)->ClrUsed = 0; 00805 ((AviBitmapInfoHeader *) movie->streams[i].sf)->ClrImportant = 0; 00806 00807 /* 00808 if (movie->streams[i].format == AVI_FORMAT_MJPEG) { 00809 AviMJPEGUnknown *tmp; 00810 00811 tmp = (AviMJPEGUnknown *) ((char*) movie->streams[i].sf +sizeof(AviBitmapInfoHeader)); 00812 00813 tmp->a = 44; 00814 tmp->b = 24; 00815 tmp->c = 0; 00816 tmp->d = 2; 00817 tmp->e = 8; 00818 tmp->f = 2; 00819 tmp->g = 1; 00820 } 00821 } else if (movie->streams[i].sh.Type == FCC("auds")) { 00822 ; 00823 */ 00824 } 00825 } 00826 00827 list.fcc = FCC("RIFF"); 00828 list.size = 0; 00829 list.ids = FCC("AVI "); 00830 00831 awrite (movie, &list, 1, sizeof(AviList), movie->fp, AVI_LIST); 00832 00833 list.fcc = FCC("LIST"); 00834 list.size = 0; 00835 list.ids = FCC("hdrl"); 00836 00837 awrite (movie, &list, 1, sizeof(AviList), movie->fp, AVI_LIST); 00838 00839 header_pos1 = ftell(movie->fp); 00840 00841 movie->offset_table[0] = ftell(movie->fp); 00842 00843 awrite (movie, movie->header, 1, sizeof(AviMainHeader), movie->fp, AVI_MAINH); 00844 00845 for (i=0; i < movie->header->Streams; i++) { 00846 list.fcc = FCC("LIST"); 00847 list.size = 0; 00848 list.ids = FCC("strl"); 00849 00850 awrite (movie, &list, 1, sizeof(AviList), movie->fp, AVI_LIST); 00851 00852 stream_pos1 = ftell(movie->fp); 00853 00854 movie->offset_table[1+i*2] = ftell(movie->fp); 00855 awrite (movie, &movie->streams[i].sh, 1, sizeof(AviStreamHeader), movie->fp, AVI_STREAMH); 00856 00857 movie->offset_table[1+i*2+1] = ftell(movie->fp); 00858 awrite (movie, movie->streams[i].sf, 1, movie->streams[i].sf_size, movie->fp, AVI_BITMAPH); 00859 00860 stream_pos2 = ftell(movie->fp); 00861 00862 fseek (movie->fp, stream_pos1-8, SEEK_SET); 00863 00864 PUT_FCCN((stream_pos2-stream_pos1+4L), movie->fp); 00865 00866 fseek (movie->fp, stream_pos2, SEEK_SET); 00867 } 00868 00869 if (ftell(movie->fp) < 2024 - 8) { 00870 chunk.fcc = FCC("JUNK"); 00871 chunk.size = 2024-8-ftell(movie->fp); 00872 00873 awrite (movie, &chunk, 1, sizeof(AviChunk), movie->fp, AVI_CHUNK); 00874 00875 for (i=0; i < chunk.size; i++) 00876 putc(0, movie->fp); 00877 } 00878 00879 header_pos2 = ftell(movie->fp); 00880 00881 list.fcc = FCC("LIST"); 00882 list.size = 0; 00883 list.ids = FCC("movi"); 00884 00885 awrite (movie, &list, 1, sizeof(AviList), movie->fp, AVI_LIST); 00886 00887 movie->movi_offset = ftell(movie->fp)-8L; 00888 00889 fseek (movie->fp, AVI_HDRL_SOFF, SEEK_SET); 00890 00891 PUT_FCCN((header_pos2-header_pos1+4L), movie->fp); 00892 00893 return AVI_ERROR_NONE; 00894 } 00895 00896 AviError AVI_write_frame (AviMovie *movie, int frame_num, ...) { 00897 AviList list; 00898 AviChunk chunk; 00899 AviIndexEntry *temp; 00900 va_list ap; 00901 int stream; 00902 int64_t rec_off; 00903 AviFormat format; 00904 void *buffer; 00905 int size; 00906 00907 if (frame_num < 0) 00908 return AVI_ERROR_OPTION; 00909 00910 /* Allocate the new memory for the index entry */ 00911 00912 if (frame_num+1 > movie->index_entries) { 00913 temp = (AviIndexEntry *) MEM_mallocN ((frame_num+1) * 00914 (movie->header->Streams+1) * sizeof(AviIndexEntry),"newidxentry"); 00915 if (movie->entries != NULL) { 00916 memcpy (temp, movie->entries, movie->index_entries * (movie->header->Streams+1) 00917 * sizeof(AviIndexEntry)); 00918 MEM_freeN (movie->entries); 00919 } 00920 00921 movie->entries = temp; 00922 movie->index_entries = frame_num+1; 00923 } 00924 00925 /* Slap a new record entry onto the end of the file */ 00926 00927 fseek (movie->fp, 0L, SEEK_END); 00928 00929 list.fcc = FCC("LIST"); 00930 list.size = 0; 00931 list.ids = FCC("rec "); 00932 00933 awrite (movie, &list, 1, sizeof(AviList), movie->fp, AVI_LIST); 00934 00935 rec_off = ftell (movie->fp)-8L; 00936 00937 /* Write a frame for every stream */ 00938 00939 va_start (ap, frame_num); 00940 00941 for (stream=0; stream < movie->header->Streams; stream++) { 00942 unsigned int tbuf=0; 00943 00944 format = va_arg (ap, AviFormat); 00945 buffer = va_arg (ap, void*); 00946 size = va_arg (ap, int); 00947 00948 /* Convert the buffer into the output format */ 00949 buffer = avi_format_convert (movie, stream, buffer, format, movie->streams[stream].format, &size); 00950 00951 /* Write the header info for this data chunk */ 00952 00953 fseek (movie->fp, 0L, SEEK_END); 00954 00955 chunk.fcc = avi_get_data_id (format, stream); 00956 chunk.size = size; 00957 00958 if (size%4) chunk.size += 4 - size%4; 00959 00960 awrite (movie, &chunk, 1, sizeof(AviChunk), movie->fp, AVI_CHUNK); 00961 00962 /* Write the index entry for this data chunk */ 00963 00964 movie->entries[frame_num * (movie->header->Streams+1) + stream + 1].ChunkId = chunk.fcc; 00965 movie->entries[frame_num * (movie->header->Streams+1) + stream + 1].Flags = AVIIF_KEYFRAME; 00966 movie->entries[frame_num * (movie->header->Streams+1) + stream + 1].Offset = ftell(movie->fp)-12L-movie->movi_offset; 00967 movie->entries[frame_num * (movie->header->Streams+1) + stream + 1].Size = chunk.size; 00968 00969 /* Write the chunk */ 00970 awrite (movie, buffer, 1, size, movie->fp, AVI_RAW); 00971 MEM_freeN (buffer); 00972 00973 if (size%4) awrite (movie, &tbuf, 1, 4-size%4, movie->fp, AVI_RAW); 00974 00975 /* Update the stream headers length field */ 00976 movie->streams[stream].sh.Length++; 00977 fseek (movie->fp, movie->offset_table[1+stream*2], SEEK_SET); 00978 awrite (movie, &movie->streams[stream].sh, 1, sizeof(AviStreamHeader), movie->fp, AVI_STREAMH); 00979 } 00980 va_end (ap); 00981 00982 /* Record the entry for the new record */ 00983 00984 fseek (movie->fp, 0L, SEEK_END); 00985 00986 movie->entries[frame_num * (movie->header->Streams+1)].ChunkId = FCC("rec "); 00987 movie->entries[frame_num * (movie->header->Streams+1)].Flags = AVIIF_LIST; 00988 movie->entries[frame_num * (movie->header->Streams+1)].Offset = rec_off-8L-movie->movi_offset; 00989 movie->entries[frame_num * (movie->header->Streams+1)].Size = ftell(movie->fp)-(rec_off+4L); 00990 00991 /* Update the record size */ 00992 fseek (movie->fp, rec_off, SEEK_SET); 00993 PUT_FCCN (movie->entries[frame_num * (movie->header->Streams+1)].Size, movie->fp); 00994 00995 /* Update the main header information in the file */ 00996 movie->header->TotalFrames++; 00997 fseek (movie->fp, movie->offset_table[0], SEEK_SET); 00998 awrite (movie, movie->header, 1, sizeof(AviMainHeader), movie->fp, AVI_MAINH); 00999 01000 return AVI_ERROR_NONE; 01001 } 01002 01003 AviError AVI_close_compress (AviMovie *movie) { 01004 int temp, movi_size, i; 01005 01006 fseek (movie->fp, 0L, SEEK_END); 01007 movi_size = ftell (movie->fp); 01008 01009 PUT_FCC ("idx1", movie->fp); 01010 PUT_FCCN ((movie->index_entries*(movie->header->Streams+1)*16), movie->fp); 01011 01012 for (temp=0; temp < movie->index_entries*(movie->header->Streams+1); temp++) 01013 awrite (movie, &movie->entries[temp], 1, sizeof(AviIndexEntry), movie->fp, AVI_INDEXE); 01014 01015 temp = ftell (movie->fp); 01016 01017 fseek (movie->fp, AVI_RIFF_SOFF, SEEK_SET); 01018 01019 PUT_FCCN((temp-8L), movie->fp); 01020 01021 fseek (movie->fp, movie->movi_offset, SEEK_SET); 01022 01023 PUT_FCCN((movi_size-(movie->movi_offset+4L)),movie->fp); 01024 01025 fclose (movie->fp); 01026 01027 for (i=0; i < movie->header->Streams; i++) { 01028 if (movie->streams[i].sf != NULL) 01029 MEM_freeN (movie->streams[i].sf); 01030 } 01031 if (movie->header != NULL) 01032 MEM_freeN (movie->header); 01033 if (movie->entries != NULL) 01034 MEM_freeN (movie->entries); 01035 if (movie->streams != NULL) 01036 MEM_freeN (movie->streams); 01037 if (movie->offset_table != NULL) 01038 MEM_freeN (movie->offset_table); 01039 return AVI_ERROR_NONE; 01040 }