diff options
Diffstat (limited to 'source/blender/avi/intern')
-rw-r--r-- | source/blender/avi/intern/avi.c | 1673 | ||||
-rw-r--r-- | source/blender/avi/intern/avi_codecs.c | 156 | ||||
-rw-r--r-- | source/blender/avi/intern/avi_endian.c | 243 | ||||
-rw-r--r-- | source/blender/avi/intern/avi_endian.h | 18 | ||||
-rw-r--r-- | source/blender/avi/intern/avi_intern.h | 38 | ||||
-rw-r--r-- | source/blender/avi/intern/avi_mjpeg.c | 728 | ||||
-rw-r--r-- | source/blender/avi/intern/avi_options.c | 192 | ||||
-rw-r--r-- | source/blender/avi/intern/avi_rgb.c | 161 | ||||
-rw-r--r-- | source/blender/avi/intern/avi_rgb32.c | 74 |
9 files changed, 1693 insertions, 1590 deletions
diff --git a/source/blender/avi/intern/avi.c b/source/blender/avi/intern/avi.c index cc53a7561f4..1f55f6ce557 100644 --- a/source/blender/avi/intern/avi.c +++ b/source/blender/avi/intern/avi.c @@ -30,7 +30,7 @@ #include <ctype.h> #ifdef WIN32 -#include "BLI_winstuff.h" +# include "BLI_winstuff.h" #endif #include "MEM_guardedalloc.h" @@ -47,1005 +47,982 @@ static int AVI_DEBUG = 0; static char DEBUG_FCC[4]; -#define DEBUG_PRINT(x) if (AVI_DEBUG) { printf("AVI DEBUG: " x); } (void)0 +#define DEBUG_PRINT(x) \ + if (AVI_DEBUG) { \ + printf("AVI DEBUG: " x); \ + } \ + (void)0 /* local functions */ char *fcc_to_char(unsigned int fcc); char *tcc_to_char(unsigned int tcc); - - /* implementation */ unsigned int GET_FCC(FILE *fp) { - unsigned char tmp[4]; + unsigned char tmp[4]; - tmp[0] = getc(fp); - tmp[1] = getc(fp); - tmp[2] = getc(fp); - tmp[3] = getc(fp); + tmp[0] = getc(fp); + tmp[1] = getc(fp); + tmp[2] = getc(fp); + tmp[3] = getc(fp); - return FCC(tmp); + return FCC(tmp); } unsigned int GET_TCC(FILE *fp) { - char tmp[5]; + char tmp[5]; - tmp[0] = getc(fp); - tmp[1] = getc(fp); - tmp[2] = 0; - tmp[3] = 0; + tmp[0] = getc(fp); + tmp[1] = getc(fp); + tmp[2] = 0; + tmp[3] = 0; - return FCC(tmp); + return FCC(tmp); } char *fcc_to_char(unsigned int fcc) { - DEBUG_FCC[0] = (fcc) & 127; - DEBUG_FCC[1] = (fcc >> 8) & 127; - DEBUG_FCC[2] = (fcc >> 16) & 127; - DEBUG_FCC[3] = (fcc >> 24) & 127; + DEBUG_FCC[0] = (fcc)&127; + DEBUG_FCC[1] = (fcc >> 8) & 127; + DEBUG_FCC[2] = (fcc >> 16) & 127; + DEBUG_FCC[3] = (fcc >> 24) & 127; - return DEBUG_FCC; + return DEBUG_FCC; } char *tcc_to_char(unsigned int tcc) { - DEBUG_FCC[0] = (tcc) & 127; - DEBUG_FCC[1] = (tcc >> 8) & 127; - DEBUG_FCC[2] = 0; - DEBUG_FCC[3] = 0; + DEBUG_FCC[0] = (tcc)&127; + DEBUG_FCC[1] = (tcc >> 8) & 127; + DEBUG_FCC[2] = 0; + DEBUG_FCC[3] = 0; - return DEBUG_FCC; + return DEBUG_FCC; } int AVI_get_stream(AviMovie *movie, int avist_type, int stream_num) { - int cur_stream; + int cur_stream; - if (movie == NULL) - return -AVI_ERROR_OPTION; + if (movie == NULL) + return -AVI_ERROR_OPTION; - for (cur_stream = 0; cur_stream < movie->header->Streams; cur_stream++) { - if (movie->streams[cur_stream].sh.Type == avist_type) { - if (stream_num == 0) - return cur_stream; - else - stream_num--; - } - } + for (cur_stream = 0; cur_stream < movie->header->Streams; cur_stream++) { + if (movie->streams[cur_stream].sh.Type == avist_type) { + if (stream_num == 0) + return cur_stream; + else + stream_num--; + } + } - return -AVI_ERROR_FOUND; + return -AVI_ERROR_FOUND; } static int fcc_get_stream(int fcc) { - char fccs[4]; + char fccs[4]; - fccs[0] = fcc; - fccs[1] = fcc >> 8; - fccs[2] = fcc >> 16; - fccs[3] = fcc >> 24; + fccs[0] = fcc; + fccs[1] = fcc >> 8; + fccs[2] = fcc >> 16; + fccs[3] = fcc >> 24; - return 10 * (fccs[0] - '0') + (fccs[1] - '0'); + return 10 * (fccs[0] - '0') + (fccs[1] - '0'); } static bool fcc_is_data(int fcc) { - char fccs[4]; + char fccs[4]; - fccs[0] = fcc; - fccs[1] = fcc >> 8; - fccs[2] = fcc >> 16; - fccs[3] = fcc >> 24; + fccs[0] = fcc; + fccs[1] = fcc >> 8; + fccs[2] = fcc >> 16; + fccs[3] = fcc >> 24; - if (!isdigit(fccs[0]) || !isdigit(fccs[1]) || (fccs[2] != 'd' && fccs[2] != 'w')) - return 0; - if (fccs[3] != 'b' && fccs[3] != 'c') - return 0; + if (!isdigit(fccs[0]) || !isdigit(fccs[1]) || (fccs[2] != 'd' && fccs[2] != 'w')) + return 0; + if (fccs[3] != 'b' && fccs[3] != 'c') + return 0; - return 1; + return 1; } AviError AVI_print_error(AviError in_error) { - int error; - - if ((int) in_error < 0) - error = -in_error; - else - error = in_error; - - switch (error) { - case AVI_ERROR_NONE: - break; - case AVI_ERROR_COMPRESSION: - printf("AVI ERROR: compressed in an unsupported format\n"); - break; - case AVI_ERROR_OPEN: - printf("AVI ERROR: could not open file\n"); - break; - case AVI_ERROR_READING: - printf("AVI ERROR: could not read from file\n"); - break; - case AVI_ERROR_WRITING: - printf("AVI ERROR: could not write to file\n"); - break; - case AVI_ERROR_FORMAT: - printf("AVI ERROR: file is in an illegal or unrecognized format\n"); - break; - case AVI_ERROR_ALLOC: - printf("AVI ERROR: error encountered while allocating memory\n"); - break; - case AVI_ERROR_OPTION: - printf("AVI ERROR: program made illegal request\n"); - break; - case AVI_ERROR_FOUND: - printf("AVI ERROR: movie did not contain expected item\n"); - break; - default: - break; - } - - return in_error; + int error; + + if ((int)in_error < 0) + error = -in_error; + else + error = in_error; + + switch (error) { + case AVI_ERROR_NONE: + break; + case AVI_ERROR_COMPRESSION: + printf("AVI ERROR: compressed in an unsupported format\n"); + break; + case AVI_ERROR_OPEN: + printf("AVI ERROR: could not open file\n"); + break; + case AVI_ERROR_READING: + printf("AVI ERROR: could not read from file\n"); + break; + case AVI_ERROR_WRITING: + printf("AVI ERROR: could not write to file\n"); + break; + case AVI_ERROR_FORMAT: + printf("AVI ERROR: file is in an illegal or unrecognized format\n"); + break; + case AVI_ERROR_ALLOC: + printf("AVI ERROR: error encountered while allocating memory\n"); + break; + case AVI_ERROR_OPTION: + printf("AVI ERROR: program made illegal request\n"); + break; + case AVI_ERROR_FOUND: + printf("AVI ERROR: movie did not contain expected item\n"); + break; + default: + break; + } + + return in_error; } bool AVI_is_avi(const char *name) { - int temp, fcca, j; - AviMovie movie = {NULL}; - AviMainHeader header; - AviBitmapInfoHeader bheader; - int movie_tracks = 0; - - DEBUG_PRINT("opening movie\n"); - - movie.type = AVI_MOVIE_READ; - movie.fp = BLI_fopen(name, "rb"); - movie.offset_table = NULL; - - if (movie.fp == NULL) - return 0; - - if (GET_FCC(movie.fp) != FCC("RIFF") || - !(movie.size = GET_FCC(movie.fp))) - { - fclose(movie.fp); - return 0; - } - - movie.header = &header; - - if (GET_FCC(movie.fp) != FCC("AVI ") || - GET_FCC(movie.fp) != FCC("LIST") || - !GET_FCC(movie.fp) || - GET_FCC(movie.fp) != FCC("hdrl") || - (movie.header->fcc = GET_FCC(movie.fp)) != FCC("avih") || - !(movie.header->size = GET_FCC(movie.fp))) - { - DEBUG_PRINT("bad initial header info\n"); - fclose(movie.fp); - return 0; - } - - movie.header->MicroSecPerFrame = GET_FCC(movie.fp); - movie.header->MaxBytesPerSec = GET_FCC(movie.fp); - movie.header->PaddingGranularity = GET_FCC(movie.fp); - movie.header->Flags = GET_FCC(movie.fp); - movie.header->TotalFrames = GET_FCC(movie.fp); - movie.header->InitialFrames = GET_FCC(movie.fp); - movie.header->Streams = GET_FCC(movie.fp); - movie.header->SuggestedBufferSize = GET_FCC(movie.fp); - movie.header->Width = GET_FCC(movie.fp); - movie.header->Height = GET_FCC(movie.fp); - movie.header->Reserved[0] = GET_FCC(movie.fp); - movie.header->Reserved[1] = GET_FCC(movie.fp); - movie.header->Reserved[2] = GET_FCC(movie.fp); - movie.header->Reserved[3] = GET_FCC(movie.fp); - - fseek(movie.fp, movie.header->size - 14 * 4, SEEK_CUR); - - /* Limit number of streams to some reasonable amount to prevent - * buffer overflow vulnerabilities. */ - if (movie.header->Streams < 1 || movie.header->Streams > 65536) { - DEBUG_PRINT("Number of streams should be in range 1-65536\n"); - fclose(movie.fp); - return 0; - } - - movie.streams = (AviStreamRec *) MEM_calloc_arrayN(movie.header->Streams, sizeof(AviStreamRec), "moviestreams"); - - for (temp = 0; temp < movie.header->Streams; temp++) { - - if (GET_FCC(movie.fp) != FCC("LIST") || - !GET_FCC(movie.fp) || - GET_FCC(movie.fp) != FCC("strl") || - (movie.streams[temp].sh.fcc = GET_FCC(movie.fp)) != FCC("strh") || - !(movie.streams[temp].sh.size = GET_FCC(movie.fp))) - { - DEBUG_PRINT("bad stream header information\n"); - - MEM_freeN(movie.streams); - fclose(movie.fp); - return 0; - } - - movie.streams[temp].sh.Type = GET_FCC(movie.fp); - movie.streams[temp].sh.Handler = GET_FCC(movie.fp); - - fcca = movie.streams[temp].sh.Handler; - - if (movie.streams[temp].sh.Type == FCC("vids")) { - if (fcca == FCC("DIB ") || - fcca == FCC("RGB ") || - fcca == FCC("rgb ") || - fcca == FCC("RAW ") || - fcca == 0) - { - movie.streams[temp].format = AVI_FORMAT_AVI_RGB; - } - else if (fcca == FCC("mjpg") || fcca == FCC("MJPG")) { - movie.streams[temp].format = AVI_FORMAT_MJPEG; - } - else { - MEM_freeN(movie.streams); - fclose(movie.fp); - return 0; - } - movie_tracks++; - } - - movie.streams[temp].sh.Flags = GET_FCC(movie.fp); - movie.streams[temp].sh.Priority = GET_TCC(movie.fp); - movie.streams[temp].sh.Language = GET_TCC(movie.fp); - movie.streams[temp].sh.InitialFrames = GET_FCC(movie.fp); - movie.streams[temp].sh.Scale = GET_FCC(movie.fp); - movie.streams[temp].sh.Rate = GET_FCC(movie.fp); - movie.streams[temp].sh.Start = GET_FCC(movie.fp); - movie.streams[temp].sh.Length = GET_FCC(movie.fp); - movie.streams[temp].sh.SuggestedBufferSize = GET_FCC(movie.fp); - movie.streams[temp].sh.Quality = GET_FCC(movie.fp); - movie.streams[temp].sh.SampleSize = GET_FCC(movie.fp); - movie.streams[temp].sh.left = GET_TCC(movie.fp); - movie.streams[temp].sh.top = GET_TCC(movie.fp); - movie.streams[temp].sh.right = GET_TCC(movie.fp); - movie.streams[temp].sh.bottom = GET_TCC(movie.fp); - - fseek(movie.fp, movie.streams[temp].sh.size - 14 * 4, SEEK_CUR); - - if (GET_FCC(movie.fp) != FCC("strf")) { - DEBUG_PRINT("no stream format information\n"); - MEM_freeN(movie.streams); - fclose(movie.fp); - return 0; - } - - movie.streams[temp].sf_size = GET_FCC(movie.fp); - if (movie.streams[temp].sh.Type == FCC("vids")) { - j = movie.streams[temp].sf_size - (sizeof(AviBitmapInfoHeader) - 8); - if (j >= 0) { - AviBitmapInfoHeader *bi; - - movie.streams[temp].sf = &bheader; - bi = (AviBitmapInfoHeader *) movie.streams[temp].sf; - - bi->fcc = FCC("strf"); - bi->size = movie.streams[temp].sf_size; - bi->Size = GET_FCC(movie.fp); - bi->Width = GET_FCC(movie.fp); - bi->Height = GET_FCC(movie.fp); - bi->Planes = GET_TCC(movie.fp); - bi->BitCount = GET_TCC(movie.fp); - bi->Compression = GET_FCC(movie.fp); - bi->SizeImage = GET_FCC(movie.fp); - bi->XPelsPerMeter = GET_FCC(movie.fp); - bi->YPelsPerMeter = GET_FCC(movie.fp); - bi->ClrUsed = GET_FCC(movie.fp); - bi->ClrImportant = GET_FCC(movie.fp); - - fcca = bi->Compression; - - if (movie.streams[temp].format == AVI_FORMAT_AVI_RGB) { - if (fcca == FCC("DIB ") || - fcca == FCC("RGB ") || - fcca == FCC("rgb ") || - fcca == FCC("RAW ") || - fcca == 0) - { - /* pass */ - } - else if (fcca == FCC("mjpg") || - fcca == FCC("MJPG")) - { - movie.streams[temp].format = AVI_FORMAT_MJPEG; - } - else { - MEM_freeN(movie.streams); - fclose(movie.fp); - return 0; - } - } - - } - if (j > 0) fseek(movie.fp, j, SEEK_CUR); - } - else { - fseek(movie.fp, movie.streams[temp].sf_size, SEEK_CUR); - } - - /* Walk to the next LIST */ - while (GET_FCC(movie.fp) != FCC("LIST")) { - temp = GET_FCC(movie.fp); - if (temp < 0 || ftell(movie.fp) > movie.size) { - DEBUG_PRINT("incorrect size in header or error in AVI\n"); - - MEM_freeN(movie.streams); - fclose(movie.fp); - return 0; - } - fseek(movie.fp, temp, SEEK_CUR); - } - - fseek(movie.fp, -4L, SEEK_CUR); - } - - MEM_freeN(movie.streams); - fclose(movie.fp); - - /* at least one video track is needed */ - return (movie_tracks != 0); - + int temp, fcca, j; + AviMovie movie = {NULL}; + AviMainHeader header; + AviBitmapInfoHeader bheader; + int movie_tracks = 0; + + DEBUG_PRINT("opening movie\n"); + + movie.type = AVI_MOVIE_READ; + movie.fp = BLI_fopen(name, "rb"); + movie.offset_table = NULL; + + if (movie.fp == NULL) + return 0; + + if (GET_FCC(movie.fp) != FCC("RIFF") || !(movie.size = GET_FCC(movie.fp))) { + fclose(movie.fp); + return 0; + } + + movie.header = &header; + + if (GET_FCC(movie.fp) != FCC("AVI ") || GET_FCC(movie.fp) != FCC("LIST") || !GET_FCC(movie.fp) || + GET_FCC(movie.fp) != FCC("hdrl") || (movie.header->fcc = GET_FCC(movie.fp)) != FCC("avih") || + !(movie.header->size = GET_FCC(movie.fp))) { + DEBUG_PRINT("bad initial header info\n"); + fclose(movie.fp); + return 0; + } + + movie.header->MicroSecPerFrame = GET_FCC(movie.fp); + movie.header->MaxBytesPerSec = GET_FCC(movie.fp); + movie.header->PaddingGranularity = GET_FCC(movie.fp); + movie.header->Flags = GET_FCC(movie.fp); + movie.header->TotalFrames = GET_FCC(movie.fp); + movie.header->InitialFrames = GET_FCC(movie.fp); + movie.header->Streams = GET_FCC(movie.fp); + movie.header->SuggestedBufferSize = GET_FCC(movie.fp); + movie.header->Width = GET_FCC(movie.fp); + movie.header->Height = GET_FCC(movie.fp); + movie.header->Reserved[0] = GET_FCC(movie.fp); + movie.header->Reserved[1] = GET_FCC(movie.fp); + movie.header->Reserved[2] = GET_FCC(movie.fp); + movie.header->Reserved[3] = GET_FCC(movie.fp); + + fseek(movie.fp, movie.header->size - 14 * 4, SEEK_CUR); + + /* Limit number of streams to some reasonable amount to prevent + * buffer overflow vulnerabilities. */ + if (movie.header->Streams < 1 || movie.header->Streams > 65536) { + DEBUG_PRINT("Number of streams should be in range 1-65536\n"); + fclose(movie.fp); + return 0; + } + + movie.streams = (AviStreamRec *)MEM_calloc_arrayN( + movie.header->Streams, sizeof(AviStreamRec), "moviestreams"); + + for (temp = 0; temp < movie.header->Streams; temp++) { + + if (GET_FCC(movie.fp) != FCC("LIST") || !GET_FCC(movie.fp) || + GET_FCC(movie.fp) != FCC("strl") || + (movie.streams[temp].sh.fcc = GET_FCC(movie.fp)) != FCC("strh") || + !(movie.streams[temp].sh.size = GET_FCC(movie.fp))) { + DEBUG_PRINT("bad stream header information\n"); + + MEM_freeN(movie.streams); + fclose(movie.fp); + return 0; + } + + movie.streams[temp].sh.Type = GET_FCC(movie.fp); + movie.streams[temp].sh.Handler = GET_FCC(movie.fp); + + fcca = movie.streams[temp].sh.Handler; + + if (movie.streams[temp].sh.Type == FCC("vids")) { + if (fcca == FCC("DIB ") || fcca == FCC("RGB ") || fcca == FCC("rgb ") || + fcca == FCC("RAW ") || fcca == 0) { + movie.streams[temp].format = AVI_FORMAT_AVI_RGB; + } + else if (fcca == FCC("mjpg") || fcca == FCC("MJPG")) { + movie.streams[temp].format = AVI_FORMAT_MJPEG; + } + else { + MEM_freeN(movie.streams); + fclose(movie.fp); + return 0; + } + movie_tracks++; + } + + movie.streams[temp].sh.Flags = GET_FCC(movie.fp); + movie.streams[temp].sh.Priority = GET_TCC(movie.fp); + movie.streams[temp].sh.Language = GET_TCC(movie.fp); + movie.streams[temp].sh.InitialFrames = GET_FCC(movie.fp); + movie.streams[temp].sh.Scale = GET_FCC(movie.fp); + movie.streams[temp].sh.Rate = GET_FCC(movie.fp); + movie.streams[temp].sh.Start = GET_FCC(movie.fp); + movie.streams[temp].sh.Length = GET_FCC(movie.fp); + movie.streams[temp].sh.SuggestedBufferSize = GET_FCC(movie.fp); + movie.streams[temp].sh.Quality = GET_FCC(movie.fp); + movie.streams[temp].sh.SampleSize = GET_FCC(movie.fp); + movie.streams[temp].sh.left = GET_TCC(movie.fp); + movie.streams[temp].sh.top = GET_TCC(movie.fp); + movie.streams[temp].sh.right = GET_TCC(movie.fp); + movie.streams[temp].sh.bottom = GET_TCC(movie.fp); + + fseek(movie.fp, movie.streams[temp].sh.size - 14 * 4, SEEK_CUR); + + if (GET_FCC(movie.fp) != FCC("strf")) { + DEBUG_PRINT("no stream format information\n"); + MEM_freeN(movie.streams); + fclose(movie.fp); + return 0; + } + + movie.streams[temp].sf_size = GET_FCC(movie.fp); + if (movie.streams[temp].sh.Type == FCC("vids")) { + j = movie.streams[temp].sf_size - (sizeof(AviBitmapInfoHeader) - 8); + if (j >= 0) { + AviBitmapInfoHeader *bi; + + movie.streams[temp].sf = &bheader; + bi = (AviBitmapInfoHeader *)movie.streams[temp].sf; + + bi->fcc = FCC("strf"); + bi->size = movie.streams[temp].sf_size; + bi->Size = GET_FCC(movie.fp); + bi->Width = GET_FCC(movie.fp); + bi->Height = GET_FCC(movie.fp); + bi->Planes = GET_TCC(movie.fp); + bi->BitCount = GET_TCC(movie.fp); + bi->Compression = GET_FCC(movie.fp); + bi->SizeImage = GET_FCC(movie.fp); + bi->XPelsPerMeter = GET_FCC(movie.fp); + bi->YPelsPerMeter = GET_FCC(movie.fp); + bi->ClrUsed = GET_FCC(movie.fp); + bi->ClrImportant = GET_FCC(movie.fp); + + fcca = bi->Compression; + + if (movie.streams[temp].format == AVI_FORMAT_AVI_RGB) { + if (fcca == FCC("DIB ") || fcca == FCC("RGB ") || fcca == FCC("rgb ") || + fcca == FCC("RAW ") || fcca == 0) { + /* pass */ + } + else if (fcca == FCC("mjpg") || fcca == FCC("MJPG")) { + movie.streams[temp].format = AVI_FORMAT_MJPEG; + } + else { + MEM_freeN(movie.streams); + fclose(movie.fp); + return 0; + } + } + } + if (j > 0) + fseek(movie.fp, j, SEEK_CUR); + } + else { + fseek(movie.fp, movie.streams[temp].sf_size, SEEK_CUR); + } + + /* Walk to the next LIST */ + while (GET_FCC(movie.fp) != FCC("LIST")) { + temp = GET_FCC(movie.fp); + if (temp < 0 || ftell(movie.fp) > movie.size) { + DEBUG_PRINT("incorrect size in header or error in AVI\n"); + + MEM_freeN(movie.streams); + fclose(movie.fp); + return 0; + } + fseek(movie.fp, temp, SEEK_CUR); + } + + fseek(movie.fp, -4L, SEEK_CUR); + } + + MEM_freeN(movie.streams); + fclose(movie.fp); + + /* at least one video track is needed */ + return (movie_tracks != 0); } AviError AVI_open_movie(const char *name, AviMovie *movie) { - int temp, fcca, size, j; - - DEBUG_PRINT("opening movie\n"); - - memset(movie, 0, sizeof(AviMovie)); - - movie->type = AVI_MOVIE_READ; - movie->fp = BLI_fopen(name, "rb"); - movie->offset_table = NULL; - - if (movie->fp == NULL) - return AVI_ERROR_OPEN; - - if (GET_FCC(movie->fp) != FCC("RIFF") || - !(movie->size = GET_FCC(movie->fp))) - { - return AVI_ERROR_FORMAT; - } - - movie->header = (AviMainHeader *) MEM_mallocN(sizeof(AviMainHeader), "movieheader"); - - if (GET_FCC(movie->fp) != FCC("AVI ") || - GET_FCC(movie->fp) != FCC("LIST") || - !GET_FCC(movie->fp) || - GET_FCC(movie->fp) != FCC("hdrl") || - (movie->header->fcc = GET_FCC(movie->fp)) != FCC("avih") || - !(movie->header->size = GET_FCC(movie->fp))) - { - DEBUG_PRINT("bad initial header info\n"); - return AVI_ERROR_FORMAT; - } - - movie->header->MicroSecPerFrame = GET_FCC(movie->fp); - movie->header->MaxBytesPerSec = GET_FCC(movie->fp); - movie->header->PaddingGranularity = GET_FCC(movie->fp); - movie->header->Flags = GET_FCC(movie->fp); - movie->header->TotalFrames = GET_FCC(movie->fp); - movie->header->InitialFrames = GET_FCC(movie->fp); - movie->header->Streams = GET_FCC(movie->fp); - movie->header->SuggestedBufferSize = GET_FCC(movie->fp); - movie->header->Width = GET_FCC(movie->fp); - movie->header->Height = GET_FCC(movie->fp); - movie->header->Reserved[0] = GET_FCC(movie->fp); - movie->header->Reserved[1] = GET_FCC(movie->fp); - movie->header->Reserved[2] = GET_FCC(movie->fp); - movie->header->Reserved[3] = GET_FCC(movie->fp); - - fseek(movie->fp, movie->header->size - 14 * 4, SEEK_CUR); - - /* Limit number of streams to some reasonable amount to prevent - * buffer overflow vulnerabilities. */ - if (movie->header->Streams < 1 || movie->header->Streams > 65536) { - DEBUG_PRINT("Number of streams should be in range 1-65536\n"); - return AVI_ERROR_FORMAT; - } - - movie->streams = (AviStreamRec *) MEM_calloc_arrayN(movie->header->Streams, sizeof(AviStreamRec), "moviestreams"); - - for (temp = 0; temp < movie->header->Streams; temp++) { - - if (GET_FCC(movie->fp) != FCC("LIST") || - !GET_FCC(movie->fp) || - GET_FCC(movie->fp) != FCC("strl") || - (movie->streams[temp].sh.fcc = GET_FCC(movie->fp)) != FCC("strh") || - !(movie->streams[temp].sh.size = GET_FCC(movie->fp))) - { - DEBUG_PRINT("bad stream header information\n"); - return AVI_ERROR_FORMAT; - } - - movie->streams[temp].sh.Type = GET_FCC(movie->fp); - movie->streams[temp].sh.Handler = GET_FCC(movie->fp); - - fcca = movie->streams[temp].sh.Handler; - - if (movie->streams[temp].sh.Type == FCC("vids")) { - if (fcca == FCC("DIB ") || - fcca == FCC("RGB ") || - fcca == FCC("rgb ") || - fcca == FCC("RAW ") || - fcca == 0) - { - movie->streams[temp].format = AVI_FORMAT_AVI_RGB; - } - else if (fcca == FCC("mjpg") || fcca == FCC("MJPG")) { - movie->streams[temp].format = AVI_FORMAT_MJPEG; - } - else { - return AVI_ERROR_COMPRESSION; - } - } - - movie->streams[temp].sh.Flags = GET_FCC(movie->fp); - movie->streams[temp].sh.Priority = GET_TCC(movie->fp); - movie->streams[temp].sh.Language = GET_TCC(movie->fp); - movie->streams[temp].sh.InitialFrames = GET_FCC(movie->fp); - movie->streams[temp].sh.Scale = GET_FCC(movie->fp); - movie->streams[temp].sh.Rate = GET_FCC(movie->fp); - movie->streams[temp].sh.Start = GET_FCC(movie->fp); - movie->streams[temp].sh.Length = GET_FCC(movie->fp); - movie->streams[temp].sh.SuggestedBufferSize = GET_FCC(movie->fp); - movie->streams[temp].sh.Quality = GET_FCC(movie->fp); - movie->streams[temp].sh.SampleSize = GET_FCC(movie->fp); - movie->streams[temp].sh.left = GET_TCC(movie->fp); - movie->streams[temp].sh.top = GET_TCC(movie->fp); - movie->streams[temp].sh.right = GET_TCC(movie->fp); - movie->streams[temp].sh.bottom = GET_TCC(movie->fp); - - fseek(movie->fp, movie->streams[temp].sh.size - 14 * 4, SEEK_CUR); - - if (GET_FCC(movie->fp) != FCC("strf")) { - DEBUG_PRINT("no stream format information\n"); - return AVI_ERROR_FORMAT; - } - - movie->streams[temp].sf_size = GET_FCC(movie->fp); - if (movie->streams[temp].sh.Type == FCC("vids")) { - j = movie->streams[temp].sf_size - (sizeof(AviBitmapInfoHeader) - 8); - if (j >= 0) { - AviBitmapInfoHeader *bi; - - movie->streams[temp].sf = MEM_mallocN(sizeof(AviBitmapInfoHeader), "streamformat"); - - bi = (AviBitmapInfoHeader *) movie->streams[temp].sf; - - bi->fcc = FCC("strf"); - bi->size = movie->streams[temp].sf_size; - bi->Size = GET_FCC(movie->fp); - bi->Width = GET_FCC(movie->fp); - bi->Height = GET_FCC(movie->fp); - bi->Planes = GET_TCC(movie->fp); - bi->BitCount = GET_TCC(movie->fp); - bi->Compression = GET_FCC(movie->fp); - bi->SizeImage = GET_FCC(movie->fp); - bi->XPelsPerMeter = GET_FCC(movie->fp); - bi->YPelsPerMeter = GET_FCC(movie->fp); - bi->ClrUsed = GET_FCC(movie->fp); - bi->ClrImportant = GET_FCC(movie->fp); - - fcca = bi->Compression; - - if (movie->streams[temp].format == - AVI_FORMAT_AVI_RGB) - { - if (fcca == FCC("DIB ") || - fcca == FCC("RGB ") || - fcca == FCC("rgb ") || - fcca == FCC("RAW ") || - fcca == 0) - { - /* pass */ - } - else if (fcca == FCC("mjpg") || - fcca == FCC("MJPG")) - { - movie->streams[temp].format = AVI_FORMAT_MJPEG; - } - else { - return AVI_ERROR_COMPRESSION; - } - } - - } - if (j > 0) fseek(movie->fp, j, SEEK_CUR); - } - else { - fseek(movie->fp, movie->streams[temp].sf_size, SEEK_CUR); - } - - /* Walk to the next LIST */ - while (GET_FCC(movie->fp) != FCC("LIST")) { - temp = GET_FCC(movie->fp); - if (temp < 0 || ftell(movie->fp) > movie->size) { - DEBUG_PRINT("incorrect size in header or error in AVI\n"); - return AVI_ERROR_FORMAT; - } - fseek(movie->fp, temp, SEEK_CUR); - } - - fseek(movie->fp, -4L, SEEK_CUR); - } - - while (1) { - temp = GET_FCC(movie->fp); - size = GET_FCC(movie->fp); - - if (size == 0) - break; - - if (temp == FCC("LIST")) { - if (GET_FCC(movie->fp) == FCC("movi")) - break; - else - fseek(movie->fp, size - 4, SEEK_CUR); - } - else { - fseek(movie->fp, size, SEEK_CUR); - } - if (ftell(movie->fp) > movie->size) { - DEBUG_PRINT("incorrect size in header or error in AVI\n"); - return AVI_ERROR_FORMAT; - } - } - - movie->movi_offset = ftell(movie->fp); - movie->read_offset = movie->movi_offset; - - /* Read in the index if the file has one, otherwise create one */ - if (movie->header->Flags & AVIF_HASINDEX) { - fseek(movie->fp, size - 4, SEEK_CUR); - - if (GET_FCC(movie->fp) != FCC("idx1")) { - DEBUG_PRINT("bad index informatio\n"); - return AVI_ERROR_FORMAT; - } - - movie->index_entries = GET_FCC(movie->fp) / sizeof(AviIndexEntry); - if (movie->index_entries == 0) { - DEBUG_PRINT("no index entries\n"); - return AVI_ERROR_FORMAT; - } - - movie->entries = (AviIndexEntry *) MEM_mallocN(movie->index_entries * sizeof(AviIndexEntry), "movieentries"); - - for (temp = 0; temp < movie->index_entries; temp++) { - movie->entries[temp].ChunkId = GET_FCC(movie->fp); - movie->entries[temp].Flags = GET_FCC(movie->fp); - movie->entries[temp].Offset = GET_FCC(movie->fp); - movie->entries[temp].Size = GET_FCC(movie->fp); - - 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); - } - } - - /* Some AVI's have offset entries in absolute coordinates - * instead of an offset from the movie beginning... this is... - * wacky, but we need to handle it. The wacky offset always - * starts at movi_offset it seems... so we'll check that. - * Note the offset needs an extra 4 bytes for some - * undetermined reason */ - - if (movie->entries[0].Offset == movie->movi_offset) - movie->read_offset = 4; - } - - DEBUG_PRINT("movie successfully opened\n"); - return AVI_ERROR_NONE; + int temp, fcca, size, j; + + DEBUG_PRINT("opening movie\n"); + + memset(movie, 0, sizeof(AviMovie)); + + movie->type = AVI_MOVIE_READ; + movie->fp = BLI_fopen(name, "rb"); + movie->offset_table = NULL; + + if (movie->fp == NULL) + return AVI_ERROR_OPEN; + + if (GET_FCC(movie->fp) != FCC("RIFF") || !(movie->size = GET_FCC(movie->fp))) { + return AVI_ERROR_FORMAT; + } + + movie->header = (AviMainHeader *)MEM_mallocN(sizeof(AviMainHeader), "movieheader"); + + if (GET_FCC(movie->fp) != FCC("AVI ") || GET_FCC(movie->fp) != FCC("LIST") || + !GET_FCC(movie->fp) || GET_FCC(movie->fp) != FCC("hdrl") || + (movie->header->fcc = GET_FCC(movie->fp)) != FCC("avih") || + !(movie->header->size = GET_FCC(movie->fp))) { + DEBUG_PRINT("bad initial header info\n"); + return AVI_ERROR_FORMAT; + } + + movie->header->MicroSecPerFrame = GET_FCC(movie->fp); + movie->header->MaxBytesPerSec = GET_FCC(movie->fp); + movie->header->PaddingGranularity = GET_FCC(movie->fp); + movie->header->Flags = GET_FCC(movie->fp); + movie->header->TotalFrames = GET_FCC(movie->fp); + movie->header->InitialFrames = GET_FCC(movie->fp); + movie->header->Streams = GET_FCC(movie->fp); + movie->header->SuggestedBufferSize = GET_FCC(movie->fp); + movie->header->Width = GET_FCC(movie->fp); + movie->header->Height = GET_FCC(movie->fp); + movie->header->Reserved[0] = GET_FCC(movie->fp); + movie->header->Reserved[1] = GET_FCC(movie->fp); + movie->header->Reserved[2] = GET_FCC(movie->fp); + movie->header->Reserved[3] = GET_FCC(movie->fp); + + fseek(movie->fp, movie->header->size - 14 * 4, SEEK_CUR); + + /* Limit number of streams to some reasonable amount to prevent + * buffer overflow vulnerabilities. */ + if (movie->header->Streams < 1 || movie->header->Streams > 65536) { + DEBUG_PRINT("Number of streams should be in range 1-65536\n"); + return AVI_ERROR_FORMAT; + } + + movie->streams = (AviStreamRec *)MEM_calloc_arrayN( + movie->header->Streams, sizeof(AviStreamRec), "moviestreams"); + + for (temp = 0; temp < movie->header->Streams; temp++) { + + if (GET_FCC(movie->fp) != FCC("LIST") || !GET_FCC(movie->fp) || + GET_FCC(movie->fp) != FCC("strl") || + (movie->streams[temp].sh.fcc = GET_FCC(movie->fp)) != FCC("strh") || + !(movie->streams[temp].sh.size = GET_FCC(movie->fp))) { + DEBUG_PRINT("bad stream header information\n"); + return AVI_ERROR_FORMAT; + } + + movie->streams[temp].sh.Type = GET_FCC(movie->fp); + movie->streams[temp].sh.Handler = GET_FCC(movie->fp); + + fcca = movie->streams[temp].sh.Handler; + + if (movie->streams[temp].sh.Type == FCC("vids")) { + if (fcca == FCC("DIB ") || fcca == FCC("RGB ") || fcca == FCC("rgb ") || + fcca == FCC("RAW ") || fcca == 0) { + movie->streams[temp].format = AVI_FORMAT_AVI_RGB; + } + else if (fcca == FCC("mjpg") || fcca == FCC("MJPG")) { + movie->streams[temp].format = AVI_FORMAT_MJPEG; + } + else { + return AVI_ERROR_COMPRESSION; + } + } + + movie->streams[temp].sh.Flags = GET_FCC(movie->fp); + movie->streams[temp].sh.Priority = GET_TCC(movie->fp); + movie->streams[temp].sh.Language = GET_TCC(movie->fp); + movie->streams[temp].sh.InitialFrames = GET_FCC(movie->fp); + movie->streams[temp].sh.Scale = GET_FCC(movie->fp); + movie->streams[temp].sh.Rate = GET_FCC(movie->fp); + movie->streams[temp].sh.Start = GET_FCC(movie->fp); + movie->streams[temp].sh.Length = GET_FCC(movie->fp); + movie->streams[temp].sh.SuggestedBufferSize = GET_FCC(movie->fp); + movie->streams[temp].sh.Quality = GET_FCC(movie->fp); + movie->streams[temp].sh.SampleSize = GET_FCC(movie->fp); + movie->streams[temp].sh.left = GET_TCC(movie->fp); + movie->streams[temp].sh.top = GET_TCC(movie->fp); + movie->streams[temp].sh.right = GET_TCC(movie->fp); + movie->streams[temp].sh.bottom = GET_TCC(movie->fp); + + fseek(movie->fp, movie->streams[temp].sh.size - 14 * 4, SEEK_CUR); + + if (GET_FCC(movie->fp) != FCC("strf")) { + DEBUG_PRINT("no stream format information\n"); + return AVI_ERROR_FORMAT; + } + + movie->streams[temp].sf_size = GET_FCC(movie->fp); + if (movie->streams[temp].sh.Type == FCC("vids")) { + j = movie->streams[temp].sf_size - (sizeof(AviBitmapInfoHeader) - 8); + if (j >= 0) { + AviBitmapInfoHeader *bi; + + movie->streams[temp].sf = MEM_mallocN(sizeof(AviBitmapInfoHeader), "streamformat"); + + bi = (AviBitmapInfoHeader *)movie->streams[temp].sf; + + bi->fcc = FCC("strf"); + bi->size = movie->streams[temp].sf_size; + bi->Size = GET_FCC(movie->fp); + bi->Width = GET_FCC(movie->fp); + bi->Height = GET_FCC(movie->fp); + bi->Planes = GET_TCC(movie->fp); + bi->BitCount = GET_TCC(movie->fp); + bi->Compression = GET_FCC(movie->fp); + bi->SizeImage = GET_FCC(movie->fp); + bi->XPelsPerMeter = GET_FCC(movie->fp); + bi->YPelsPerMeter = GET_FCC(movie->fp); + bi->ClrUsed = GET_FCC(movie->fp); + bi->ClrImportant = GET_FCC(movie->fp); + + fcca = bi->Compression; + + if (movie->streams[temp].format == AVI_FORMAT_AVI_RGB) { + if (fcca == FCC("DIB ") || fcca == FCC("RGB ") || fcca == FCC("rgb ") || + fcca == FCC("RAW ") || fcca == 0) { + /* pass */ + } + else if (fcca == FCC("mjpg") || fcca == FCC("MJPG")) { + movie->streams[temp].format = AVI_FORMAT_MJPEG; + } + else { + return AVI_ERROR_COMPRESSION; + } + } + } + if (j > 0) + fseek(movie->fp, j, SEEK_CUR); + } + else { + fseek(movie->fp, movie->streams[temp].sf_size, SEEK_CUR); + } + + /* Walk to the next LIST */ + while (GET_FCC(movie->fp) != FCC("LIST")) { + temp = GET_FCC(movie->fp); + if (temp < 0 || ftell(movie->fp) > movie->size) { + DEBUG_PRINT("incorrect size in header or error in AVI\n"); + return AVI_ERROR_FORMAT; + } + fseek(movie->fp, temp, SEEK_CUR); + } + + fseek(movie->fp, -4L, SEEK_CUR); + } + + while (1) { + temp = GET_FCC(movie->fp); + size = GET_FCC(movie->fp); + + if (size == 0) + break; + + if (temp == FCC("LIST")) { + if (GET_FCC(movie->fp) == FCC("movi")) + break; + else + fseek(movie->fp, size - 4, SEEK_CUR); + } + else { + fseek(movie->fp, size, SEEK_CUR); + } + if (ftell(movie->fp) > movie->size) { + DEBUG_PRINT("incorrect size in header or error in AVI\n"); + return AVI_ERROR_FORMAT; + } + } + + movie->movi_offset = ftell(movie->fp); + movie->read_offset = movie->movi_offset; + + /* Read in the index if the file has one, otherwise create one */ + if (movie->header->Flags & AVIF_HASINDEX) { + fseek(movie->fp, size - 4, SEEK_CUR); + + if (GET_FCC(movie->fp) != FCC("idx1")) { + DEBUG_PRINT("bad index informatio\n"); + return AVI_ERROR_FORMAT; + } + + movie->index_entries = GET_FCC(movie->fp) / sizeof(AviIndexEntry); + if (movie->index_entries == 0) { + DEBUG_PRINT("no index entries\n"); + return AVI_ERROR_FORMAT; + } + + movie->entries = (AviIndexEntry *)MEM_mallocN(movie->index_entries * sizeof(AviIndexEntry), + "movieentries"); + + for (temp = 0; temp < movie->index_entries; temp++) { + movie->entries[temp].ChunkId = GET_FCC(movie->fp); + movie->entries[temp].Flags = GET_FCC(movie->fp); + movie->entries[temp].Offset = GET_FCC(movie->fp); + movie->entries[temp].Size = GET_FCC(movie->fp); + + 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); + } + } + + /* Some AVI's have offset entries in absolute coordinates + * instead of an offset from the movie beginning... this is... + * wacky, but we need to handle it. The wacky offset always + * starts at movi_offset it seems... so we'll check that. + * Note the offset needs an extra 4 bytes for some + * undetermined reason */ + + if (movie->entries[0].Offset == movie->movi_offset) + movie->read_offset = 4; + } + + DEBUG_PRINT("movie successfully opened\n"); + return AVI_ERROR_NONE; } void *AVI_read_frame(AviMovie *movie, AviFormat format, int frame, int stream) { - int cur_frame = -1, i = 0, rewind = 1; - void *buffer; - - /* Retrieve the record number of the desired frame in the index - * If a chunk has Size 0 we need to rewind to previous frame */ - while (rewind && frame > -1) { - i = 0; - cur_frame = -1; - rewind = 0; - - while (cur_frame < frame && i < movie->index_entries) { - if (fcc_is_data(movie->entries[i].ChunkId) && - fcc_get_stream(movie->entries[i].ChunkId) == stream) - { - if ((cur_frame == frame - 1) && (movie->entries[i].Size == 0)) { - rewind = 1; - frame = frame - 1; - } - else { - cur_frame++; - } - } - i++; - } - } - - if (cur_frame != frame) return NULL; - - - fseek(movie->fp, movie->read_offset + movie->entries[i - 1].Offset, SEEK_SET); - - size_t size = GET_FCC(movie->fp); - buffer = MEM_mallocN(size, "readbuffer"); - - if (fread(buffer, 1, size, movie->fp) != size) { - MEM_freeN(buffer); - - return NULL; - } - - buffer = avi_format_convert(movie, stream, buffer, movie->streams[stream].format, format, &size); - - return buffer; + int cur_frame = -1, i = 0, rewind = 1; + void *buffer; + + /* Retrieve the record number of the desired frame in the index + * If a chunk has Size 0 we need to rewind to previous frame */ + while (rewind && frame > -1) { + i = 0; + cur_frame = -1; + rewind = 0; + + while (cur_frame < frame && i < movie->index_entries) { + if (fcc_is_data(movie->entries[i].ChunkId) && + fcc_get_stream(movie->entries[i].ChunkId) == stream) { + if ((cur_frame == frame - 1) && (movie->entries[i].Size == 0)) { + rewind = 1; + frame = frame - 1; + } + else { + cur_frame++; + } + } + i++; + } + } + + if (cur_frame != frame) + return NULL; + + fseek(movie->fp, movie->read_offset + movie->entries[i - 1].Offset, SEEK_SET); + + size_t size = GET_FCC(movie->fp); + buffer = MEM_mallocN(size, "readbuffer"); + + if (fread(buffer, 1, size, movie->fp) != size) { + MEM_freeN(buffer); + + return NULL; + } + + buffer = avi_format_convert(movie, stream, buffer, movie->streams[stream].format, format, &size); + + return buffer; } AviError AVI_close(AviMovie *movie) { - int i; + int i; - fclose(movie->fp); + fclose(movie->fp); - for (i = 0; i < movie->header->Streams; i++) { - if (movie->streams[i].sf != NULL) { - MEM_freeN(movie->streams[i].sf); - } - } + for (i = 0; i < movie->header->Streams; i++) { + if (movie->streams[i].sf != NULL) { + MEM_freeN(movie->streams[i].sf); + } + } - MEM_freeN(movie->header); - MEM_freeN(movie->streams); + MEM_freeN(movie->header); + MEM_freeN(movie->streams); - if (movie->entries != NULL) - MEM_freeN(movie->entries); - if (movie->offset_table != NULL) - MEM_freeN(movie->offset_table); + if (movie->entries != NULL) + MEM_freeN(movie->entries); + if (movie->offset_table != NULL) + MEM_freeN(movie->offset_table); - return AVI_ERROR_NONE; + return AVI_ERROR_NONE; } AviError AVI_open_compress(char *name, AviMovie *movie, int streams, ...) { - va_list ap; - AviList list; - AviChunk chunk; - int i; - int64_t header_pos1, header_pos2; - int64_t stream_pos1, stream_pos2; - int64_t junk_pos; - - movie->type = AVI_MOVIE_WRITE; - movie->fp = BLI_fopen(name, "wb"); - - movie->index_entries = 0; - - if (movie->fp == NULL) - return AVI_ERROR_OPEN; - - movie->offset_table = (int64_t *) MEM_mallocN((1 + streams * 2) * sizeof(int64_t), "offsettable"); - - for (i = 0; i < 1 + streams * 2; i++) - movie->offset_table[i] = -1L; - - movie->entries = NULL; - - movie->header = (AviMainHeader *) MEM_mallocN(sizeof(AviMainHeader), "movieheader"); - - movie->header->fcc = FCC("avih"); - movie->header->size = 56; - movie->header->MicroSecPerFrame = 66667; - movie->header->MaxBytesPerSec = 0; - movie->header->PaddingGranularity = 0; - movie->header->Flags = AVIF_HASINDEX | AVIF_MUSTUSEINDEX; - movie->header->TotalFrames = 0; - movie->header->InitialFrames = 0; - movie->header->Streams = streams; - movie->header->SuggestedBufferSize = 0; - movie->header->Width = 0; - movie->header->Height = 0; - movie->header->Reserved[0] = 0; - movie->header->Reserved[1] = 0; - movie->header->Reserved[2] = 0; - movie->header->Reserved[3] = 0; - - /* Limit number of streams to some reasonable amount to prevent - * buffer overflow vulnerabilities. */ - if (movie->header->Streams < 0 || movie->header->Streams > 65536) { - DEBUG_PRINT("Number of streams should be in range 0-65536\n"); - return AVI_ERROR_FORMAT; - } - - movie->streams = (AviStreamRec *) MEM_mallocN(sizeof(AviStreamRec) * movie->header->Streams, "moviestreams"); - - va_start(ap, streams); - - for (i = 0; i < movie->header->Streams; i++) { - movie->streams[i].format = va_arg(ap, AviFormat); - - movie->streams[i].sh.fcc = FCC("strh"); - movie->streams[i].sh.size = 56; - movie->streams[i].sh.Type = avi_get_format_type(movie->streams[i].format); - if (movie->streams[i].sh.Type == 0) { - va_end(ap); - return AVI_ERROR_FORMAT; - } - - movie->streams[i].sh.Handler = avi_get_format_fcc(movie->streams[i].format); - if (movie->streams[i].sh.Handler == 0) { - va_end(ap); - return AVI_ERROR_FORMAT; - } - - movie->streams[i].sh.Flags = 0; - movie->streams[i].sh.Priority = 0; - movie->streams[i].sh.Language = 0; - movie->streams[i].sh.InitialFrames = 0; - movie->streams[i].sh.Scale = 66667; - movie->streams[i].sh.Rate = 1000000; - movie->streams[i].sh.Start = 0; - movie->streams[i].sh.Length = 0; - movie->streams[i].sh.SuggestedBufferSize = 0; - movie->streams[i].sh.Quality = 10000; - movie->streams[i].sh.SampleSize = 0; - movie->streams[i].sh.left = 0; - movie->streams[i].sh.top = 0; - movie->streams[i].sh.right = 0; - movie->streams[i].sh.bottom = 0; - - if (movie->streams[i].sh.Type == FCC("vids")) { - movie->streams[i].sf = MEM_mallocN(sizeof(AviBitmapInfoHeader), "moviestreamformatS"); - movie->streams[i].sf_size = sizeof(AviBitmapInfoHeader); - - ((AviBitmapInfoHeader *) movie->streams[i].sf)->fcc = FCC("strf"); - ((AviBitmapInfoHeader *) movie->streams[i].sf)->size = movie->streams[i].sf_size - 8; - ((AviBitmapInfoHeader *) movie->streams[i].sf)->Size = movie->streams[i].sf_size - 8; - ((AviBitmapInfoHeader *) movie->streams[i].sf)->Width = 0; - ((AviBitmapInfoHeader *) movie->streams[i].sf)->Height = 0; - ((AviBitmapInfoHeader *) movie->streams[i].sf)->Planes = 1; - ((AviBitmapInfoHeader *) movie->streams[i].sf)->BitCount = 24; - ((AviBitmapInfoHeader *) movie->streams[i].sf)->Compression = avi_get_format_compression(movie->streams[i].format); - ((AviBitmapInfoHeader *) movie->streams[i].sf)->SizeImage = 0; - ((AviBitmapInfoHeader *) movie->streams[i].sf)->XPelsPerMeter = 0; - ((AviBitmapInfoHeader *) movie->streams[i].sf)->YPelsPerMeter = 0; - ((AviBitmapInfoHeader *) movie->streams[i].sf)->ClrUsed = 0; - ((AviBitmapInfoHeader *) movie->streams[i].sf)->ClrImportant = 0; - } - } + va_list ap; + AviList list; + AviChunk chunk; + int i; + int64_t header_pos1, header_pos2; + int64_t stream_pos1, stream_pos2; + int64_t junk_pos; + + movie->type = AVI_MOVIE_WRITE; + movie->fp = BLI_fopen(name, "wb"); + + movie->index_entries = 0; + + if (movie->fp == NULL) + return AVI_ERROR_OPEN; + + movie->offset_table = (int64_t *)MEM_mallocN((1 + streams * 2) * sizeof(int64_t), "offsettable"); + + for (i = 0; i < 1 + streams * 2; i++) + movie->offset_table[i] = -1L; + + movie->entries = NULL; + + movie->header = (AviMainHeader *)MEM_mallocN(sizeof(AviMainHeader), "movieheader"); + + movie->header->fcc = FCC("avih"); + movie->header->size = 56; + movie->header->MicroSecPerFrame = 66667; + movie->header->MaxBytesPerSec = 0; + movie->header->PaddingGranularity = 0; + movie->header->Flags = AVIF_HASINDEX | AVIF_MUSTUSEINDEX; + movie->header->TotalFrames = 0; + movie->header->InitialFrames = 0; + movie->header->Streams = streams; + movie->header->SuggestedBufferSize = 0; + movie->header->Width = 0; + movie->header->Height = 0; + movie->header->Reserved[0] = 0; + movie->header->Reserved[1] = 0; + movie->header->Reserved[2] = 0; + movie->header->Reserved[3] = 0; + + /* Limit number of streams to some reasonable amount to prevent + * buffer overflow vulnerabilities. */ + if (movie->header->Streams < 0 || movie->header->Streams > 65536) { + DEBUG_PRINT("Number of streams should be in range 0-65536\n"); + return AVI_ERROR_FORMAT; + } + + movie->streams = (AviStreamRec *)MEM_mallocN(sizeof(AviStreamRec) * movie->header->Streams, + "moviestreams"); + + va_start(ap, streams); + + for (i = 0; i < movie->header->Streams; i++) { + movie->streams[i].format = va_arg(ap, AviFormat); + + movie->streams[i].sh.fcc = FCC("strh"); + movie->streams[i].sh.size = 56; + movie->streams[i].sh.Type = avi_get_format_type(movie->streams[i].format); + if (movie->streams[i].sh.Type == 0) { + va_end(ap); + return AVI_ERROR_FORMAT; + } + + movie->streams[i].sh.Handler = avi_get_format_fcc(movie->streams[i].format); + if (movie->streams[i].sh.Handler == 0) { + va_end(ap); + return AVI_ERROR_FORMAT; + } + + movie->streams[i].sh.Flags = 0; + movie->streams[i].sh.Priority = 0; + movie->streams[i].sh.Language = 0; + movie->streams[i].sh.InitialFrames = 0; + movie->streams[i].sh.Scale = 66667; + movie->streams[i].sh.Rate = 1000000; + movie->streams[i].sh.Start = 0; + movie->streams[i].sh.Length = 0; + movie->streams[i].sh.SuggestedBufferSize = 0; + movie->streams[i].sh.Quality = 10000; + movie->streams[i].sh.SampleSize = 0; + movie->streams[i].sh.left = 0; + movie->streams[i].sh.top = 0; + movie->streams[i].sh.right = 0; + movie->streams[i].sh.bottom = 0; - list.fcc = FCC("RIFF"); - list.size = 0; - list.ids = FCC("AVI "); + if (movie->streams[i].sh.Type == FCC("vids")) { + movie->streams[i].sf = MEM_mallocN(sizeof(AviBitmapInfoHeader), "moviestreamformatS"); + movie->streams[i].sf_size = sizeof(AviBitmapInfoHeader); - awrite(movie, &list, 1, sizeof(AviList), movie->fp, AVI_LIST); + ((AviBitmapInfoHeader *)movie->streams[i].sf)->fcc = FCC("strf"); + ((AviBitmapInfoHeader *)movie->streams[i].sf)->size = movie->streams[i].sf_size - 8; + ((AviBitmapInfoHeader *)movie->streams[i].sf)->Size = movie->streams[i].sf_size - 8; + ((AviBitmapInfoHeader *)movie->streams[i].sf)->Width = 0; + ((AviBitmapInfoHeader *)movie->streams[i].sf)->Height = 0; + ((AviBitmapInfoHeader *)movie->streams[i].sf)->Planes = 1; + ((AviBitmapInfoHeader *)movie->streams[i].sf)->BitCount = 24; + ((AviBitmapInfoHeader *)movie->streams[i].sf)->Compression = avi_get_format_compression( + movie->streams[i].format); + ((AviBitmapInfoHeader *)movie->streams[i].sf)->SizeImage = 0; + ((AviBitmapInfoHeader *)movie->streams[i].sf)->XPelsPerMeter = 0; + ((AviBitmapInfoHeader *)movie->streams[i].sf)->YPelsPerMeter = 0; + ((AviBitmapInfoHeader *)movie->streams[i].sf)->ClrUsed = 0; + ((AviBitmapInfoHeader *)movie->streams[i].sf)->ClrImportant = 0; + } + } - list.fcc = FCC("LIST"); - list.size = 0; - list.ids = FCC("hdrl"); + list.fcc = FCC("RIFF"); + list.size = 0; + list.ids = FCC("AVI "); - awrite(movie, &list, 1, sizeof(AviList), movie->fp, AVI_LIST); + awrite(movie, &list, 1, sizeof(AviList), movie->fp, AVI_LIST); - header_pos1 = ftell(movie->fp); + list.fcc = FCC("LIST"); + list.size = 0; + list.ids = FCC("hdrl"); - movie->offset_table[0] = ftell(movie->fp); + awrite(movie, &list, 1, sizeof(AviList), movie->fp, AVI_LIST); - awrite(movie, movie->header, 1, sizeof(AviMainHeader), movie->fp, AVI_MAINH); + header_pos1 = ftell(movie->fp); - for (i = 0; i < movie->header->Streams; i++) { - list.fcc = FCC("LIST"); - list.size = 0; - list.ids = FCC("strl"); + movie->offset_table[0] = ftell(movie->fp); + + awrite(movie, movie->header, 1, sizeof(AviMainHeader), movie->fp, AVI_MAINH); + + for (i = 0; i < movie->header->Streams; i++) { + list.fcc = FCC("LIST"); + list.size = 0; + list.ids = FCC("strl"); - awrite(movie, &list, 1, sizeof(AviList), movie->fp, AVI_LIST); + awrite(movie, &list, 1, sizeof(AviList), movie->fp, AVI_LIST); - stream_pos1 = ftell(movie->fp); + stream_pos1 = ftell(movie->fp); - movie->offset_table[1 + i * 2] = ftell(movie->fp); - awrite(movie, &movie->streams[i].sh, 1, sizeof(AviStreamHeader), movie->fp, AVI_STREAMH); + movie->offset_table[1 + i * 2] = ftell(movie->fp); + awrite(movie, &movie->streams[i].sh, 1, sizeof(AviStreamHeader), movie->fp, AVI_STREAMH); - movie->offset_table[1 + i * 2 + 1] = ftell(movie->fp); - awrite(movie, movie->streams[i].sf, 1, movie->streams[i].sf_size, movie->fp, AVI_BITMAPH); + movie->offset_table[1 + i * 2 + 1] = ftell(movie->fp); + awrite(movie, movie->streams[i].sf, 1, movie->streams[i].sf_size, movie->fp, AVI_BITMAPH); - stream_pos2 = ftell(movie->fp); + stream_pos2 = ftell(movie->fp); - fseek(movie->fp, stream_pos1 - 8, SEEK_SET); + fseek(movie->fp, stream_pos1 - 8, SEEK_SET); - PUT_FCCN((stream_pos2 - stream_pos1 + 4L), movie->fp); + PUT_FCCN((stream_pos2 - stream_pos1 + 4L), movie->fp); - fseek(movie->fp, stream_pos2, SEEK_SET); - } + fseek(movie->fp, stream_pos2, SEEK_SET); + } - junk_pos = ftell(movie->fp); + junk_pos = ftell(movie->fp); - if (junk_pos < 2024 - 8) { - chunk.fcc = FCC("JUNK"); - chunk.size = 2024 - 8 - (int)junk_pos; + if (junk_pos < 2024 - 8) { + chunk.fcc = FCC("JUNK"); + chunk.size = 2024 - 8 - (int)junk_pos; - awrite(movie, &chunk, 1, sizeof(AviChunk), movie->fp, AVI_CHUNK); + awrite(movie, &chunk, 1, sizeof(AviChunk), movie->fp, AVI_CHUNK); - for (i = 0; i < chunk.size; i++) - putc(0, movie->fp); - } + for (i = 0; i < chunk.size; i++) + putc(0, movie->fp); + } - header_pos2 = ftell(movie->fp); + header_pos2 = ftell(movie->fp); - list.fcc = FCC("LIST"); - list.size = 0; - list.ids = FCC("movi"); + list.fcc = FCC("LIST"); + list.size = 0; + list.ids = FCC("movi"); - awrite(movie, &list, 1, sizeof(AviList), movie->fp, AVI_LIST); + awrite(movie, &list, 1, sizeof(AviList), movie->fp, AVI_LIST); - movie->movi_offset = ftell(movie->fp) - 8L; + movie->movi_offset = ftell(movie->fp) - 8L; - fseek(movie->fp, AVI_HDRL_SOFF, SEEK_SET); + fseek(movie->fp, AVI_HDRL_SOFF, SEEK_SET); - PUT_FCCN((header_pos2 - header_pos1 + 4L), movie->fp); + PUT_FCCN((header_pos2 - header_pos1 + 4L), movie->fp); - va_end(ap); + va_end(ap); - return AVI_ERROR_NONE; + return AVI_ERROR_NONE; } AviError AVI_write_frame(AviMovie *movie, int frame_num, ...) { - AviList list; - AviChunk chunk; - va_list ap; - int stream; - int64_t rec_off; - AviFormat format; - void *buffer; + AviList list; + AviChunk chunk; + va_list ap; + int stream; + int64_t rec_off; + AviFormat format; + void *buffer; - if (frame_num < 0) - return AVI_ERROR_OPTION; + if (frame_num < 0) + return AVI_ERROR_OPTION; - /* Allocate the new memory for the index entry */ + /* Allocate the new memory for the index entry */ - if (frame_num >= movie->index_entries) { - const size_t entry_size = (movie->header->Streams + 1) * sizeof(AviIndexEntry); - movie->entries = (AviIndexEntry *)MEM_recallocN(movie->entries, (frame_num + 1) * entry_size); - movie->index_entries = frame_num + 1; - } + if (frame_num >= movie->index_entries) { + const size_t entry_size = (movie->header->Streams + 1) * sizeof(AviIndexEntry); + movie->entries = (AviIndexEntry *)MEM_recallocN(movie->entries, (frame_num + 1) * entry_size); + movie->index_entries = frame_num + 1; + } - /* Slap a new record entry onto the end of the file */ + /* Slap a new record entry onto the end of the file */ - fseek(movie->fp, 0L, SEEK_END); + fseek(movie->fp, 0L, SEEK_END); - list.fcc = FCC("LIST"); - list.size = 0; - list.ids = FCC("rec "); + list.fcc = FCC("LIST"); + list.size = 0; + list.ids = FCC("rec "); - awrite(movie, &list, 1, sizeof(AviList), movie->fp, AVI_LIST); + awrite(movie, &list, 1, sizeof(AviList), movie->fp, AVI_LIST); - rec_off = ftell(movie->fp) - 8L; + rec_off = ftell(movie->fp) - 8L; - /* Write a frame for every stream */ + /* Write a frame for every stream */ - va_start(ap, frame_num); + va_start(ap, frame_num); - for (stream = 0; stream < movie->header->Streams; stream++) { - unsigned int tbuf = 0; + for (stream = 0; stream < movie->header->Streams; stream++) { + unsigned int tbuf = 0; - format = va_arg(ap, AviFormat); - buffer = va_arg(ap, void *); - size_t size = va_arg(ap, int); + format = va_arg(ap, AviFormat); + buffer = va_arg(ap, void *); + size_t size = va_arg(ap, int); - /* Convert the buffer into the output format */ - buffer = avi_format_convert(movie, stream, buffer, format, movie->streams[stream].format, &size); + /* Convert the buffer into the output format */ + buffer = avi_format_convert( + movie, stream, buffer, format, movie->streams[stream].format, &size); - /* Write the header info for this data chunk */ + /* Write the header info for this data chunk */ - fseek(movie->fp, 0L, SEEK_END); + fseek(movie->fp, 0L, SEEK_END); - chunk.fcc = avi_get_data_id(format, stream); - chunk.size = size; + chunk.fcc = avi_get_data_id(format, stream); + chunk.size = size; - if (size % 4) chunk.size += 4 - size % 4; + if (size % 4) + chunk.size += 4 - size % 4; - awrite(movie, &chunk, 1, sizeof(AviChunk), movie->fp, AVI_CHUNK); + awrite(movie, &chunk, 1, sizeof(AviChunk), movie->fp, AVI_CHUNK); - /* Write the index entry for this data chunk */ + /* Write the index entry for this data chunk */ - movie->entries[frame_num * (movie->header->Streams + 1) + stream + 1].ChunkId = chunk.fcc; - movie->entries[frame_num * (movie->header->Streams + 1) + stream + 1].Flags = AVIIF_KEYFRAME; - movie->entries[frame_num * (movie->header->Streams + 1) + stream + 1].Offset = (int)(ftell(movie->fp) - 12L - movie->movi_offset); - movie->entries[frame_num * (movie->header->Streams + 1) + stream + 1].Size = chunk.size; + movie->entries[frame_num * (movie->header->Streams + 1) + stream + 1].ChunkId = chunk.fcc; + movie->entries[frame_num * (movie->header->Streams + 1) + stream + 1].Flags = AVIIF_KEYFRAME; + movie->entries[frame_num * (movie->header->Streams + 1) + stream + 1].Offset = + (int)(ftell(movie->fp) - 12L - movie->movi_offset); + movie->entries[frame_num * (movie->header->Streams + 1) + stream + 1].Size = chunk.size; - /* Write the chunk */ - awrite(movie, buffer, 1, size, movie->fp, AVI_RAW); - MEM_freeN(buffer); + /* Write the chunk */ + awrite(movie, buffer, 1, size, movie->fp, AVI_RAW); + MEM_freeN(buffer); - if (size % 4) awrite(movie, &tbuf, 1, 4 - size % 4, movie->fp, AVI_RAW); + if (size % 4) + awrite(movie, &tbuf, 1, 4 - size % 4, movie->fp, AVI_RAW); - /* Update the stream headers length field */ - movie->streams[stream].sh.Length++; - fseek(movie->fp, movie->offset_table[1 + stream * 2], SEEK_SET); - awrite(movie, &movie->streams[stream].sh, 1, sizeof(AviStreamHeader), movie->fp, AVI_STREAMH); - } - va_end(ap); + /* Update the stream headers length field */ + movie->streams[stream].sh.Length++; + fseek(movie->fp, movie->offset_table[1 + stream * 2], SEEK_SET); + awrite(movie, &movie->streams[stream].sh, 1, sizeof(AviStreamHeader), movie->fp, AVI_STREAMH); + } + va_end(ap); - /* Record the entry for the new record */ + /* Record the entry for the new record */ - fseek(movie->fp, 0L, SEEK_END); + fseek(movie->fp, 0L, SEEK_END); - movie->entries[frame_num * (movie->header->Streams + 1)].ChunkId = FCC("rec "); - movie->entries[frame_num * (movie->header->Streams + 1)].Flags = AVIIF_LIST; - movie->entries[frame_num * (movie->header->Streams + 1)].Offset = (int)(rec_off - 8L - movie->movi_offset); - movie->entries[frame_num * (movie->header->Streams + 1)].Size = (int)(ftell(movie->fp) - (rec_off + 4L)); + movie->entries[frame_num * (movie->header->Streams + 1)].ChunkId = FCC("rec "); + movie->entries[frame_num * (movie->header->Streams + 1)].Flags = AVIIF_LIST; + movie->entries[frame_num * (movie->header->Streams + 1)].Offset = (int)(rec_off - 8L - + movie->movi_offset); + movie->entries[frame_num * (movie->header->Streams + 1)].Size = (int)(ftell(movie->fp) - + (rec_off + 4L)); - /* Update the record size */ - fseek(movie->fp, rec_off, SEEK_SET); - PUT_FCCN(movie->entries[frame_num * (movie->header->Streams + 1)].Size, movie->fp); + /* Update the record size */ + fseek(movie->fp, rec_off, SEEK_SET); + PUT_FCCN(movie->entries[frame_num * (movie->header->Streams + 1)].Size, movie->fp); - /* Update the main header information in the file */ - movie->header->TotalFrames++; - fseek(movie->fp, movie->offset_table[0], SEEK_SET); - awrite(movie, movie->header, 1, sizeof(AviMainHeader), movie->fp, AVI_MAINH); + /* Update the main header information in the file */ + movie->header->TotalFrames++; + fseek(movie->fp, movie->offset_table[0], SEEK_SET); + awrite(movie, movie->header, 1, sizeof(AviMainHeader), movie->fp, AVI_MAINH); - return AVI_ERROR_NONE; + return AVI_ERROR_NONE; } AviError AVI_close_compress(AviMovie *movie) { - int temp, movi_size, i; + int temp, movi_size, i; - if (movie->fp == NULL) { - /* none of the allocations below were done if the file failed to open */ - return AVI_ERROR_FOUND; - } + if (movie->fp == NULL) { + /* none of the allocations below were done if the file failed to open */ + return AVI_ERROR_FOUND; + } - fseek(movie->fp, 0L, SEEK_END); - movi_size = (int)ftell(movie->fp); + fseek(movie->fp, 0L, SEEK_END); + movi_size = (int)ftell(movie->fp); - PUT_FCC("idx1", movie->fp); - PUT_FCCN((movie->index_entries * (movie->header->Streams + 1) * 16), movie->fp); + PUT_FCC("idx1", movie->fp); + PUT_FCCN((movie->index_entries * (movie->header->Streams + 1) * 16), movie->fp); - for (temp = 0; temp < movie->index_entries * (movie->header->Streams + 1); temp++) - awrite(movie, &movie->entries[temp], 1, sizeof(AviIndexEntry), movie->fp, AVI_INDEXE); + for (temp = 0; temp < movie->index_entries * (movie->header->Streams + 1); temp++) + awrite(movie, &movie->entries[temp], 1, sizeof(AviIndexEntry), movie->fp, AVI_INDEXE); - temp = (int)ftell(movie->fp); + temp = (int)ftell(movie->fp); - fseek(movie->fp, AVI_RIFF_SOFF, SEEK_SET); + fseek(movie->fp, AVI_RIFF_SOFF, SEEK_SET); - PUT_FCCN((temp - 8L), movie->fp); + PUT_FCCN((temp - 8L), movie->fp); - fseek(movie->fp, movie->movi_offset, SEEK_SET); + fseek(movie->fp, movie->movi_offset, SEEK_SET); - PUT_FCCN((movi_size - (movie->movi_offset + 4L)), movie->fp); + PUT_FCCN((movi_size - (movie->movi_offset + 4L)), movie->fp); - fclose(movie->fp); + fclose(movie->fp); - for (i = 0; i < movie->header->Streams; i++) { - if (movie->streams && (movie->streams[i].sf != NULL)) { - MEM_freeN(movie->streams[i].sf); - } - } + for (i = 0; i < movie->header->Streams; i++) { + if (movie->streams && (movie->streams[i].sf != NULL)) { + MEM_freeN(movie->streams[i].sf); + } + } - MEM_freeN(movie->header); + MEM_freeN(movie->header); - if (movie->entries != NULL) - MEM_freeN(movie->entries); - if (movie->streams != NULL) - MEM_freeN(movie->streams); - if (movie->offset_table != NULL) - MEM_freeN(movie->offset_table); - return AVI_ERROR_NONE; + if (movie->entries != NULL) + MEM_freeN(movie->entries); + if (movie->streams != NULL) + MEM_freeN(movie->streams); + if (movie->offset_table != NULL) + MEM_freeN(movie->offset_table); + return AVI_ERROR_NONE; } diff --git a/source/blender/avi/intern/avi_codecs.c b/source/blender/avi/intern/avi_codecs.c index 781519bbe24..705ca1e41e1 100644 --- a/source/blender/avi/intern/avi_codecs.c +++ b/source/blender/avi/intern/avi_codecs.c @@ -23,7 +23,6 @@ * This is external code. Identify and convert different avi-files. */ - #include "AVI_avi.h" #include "avi_intern.h" @@ -31,102 +30,105 @@ #include "avi_mjpeg.h" #include "avi_rgb32.h" -void *avi_format_convert(AviMovie *movie, int stream, void *buffer, AviFormat from, AviFormat to, size_t *size) +void *avi_format_convert( + AviMovie *movie, int stream, void *buffer, AviFormat from, AviFormat to, size_t *size) { - if (from == to) - return buffer; + if (from == to) + return buffer; - if (from != AVI_FORMAT_RGB24 && - to != AVI_FORMAT_RGB24) - { - return avi_format_convert(movie, stream, - avi_format_convert(movie, stream, buffer, from, AVI_FORMAT_RGB24, size), - AVI_FORMAT_RGB24, to, size); - } + if (from != AVI_FORMAT_RGB24 && to != AVI_FORMAT_RGB24) { + return avi_format_convert( + movie, + stream, + avi_format_convert(movie, stream, buffer, from, AVI_FORMAT_RGB24, size), + AVI_FORMAT_RGB24, + to, + size); + } - switch (to) { - case AVI_FORMAT_RGB24: - switch (from) { - case AVI_FORMAT_AVI_RGB: - buffer = avi_converter_from_avi_rgb(movie, stream, buffer, size); - break; - case AVI_FORMAT_MJPEG: - buffer = avi_converter_from_mjpeg(movie, stream, buffer, size); - break; - case AVI_FORMAT_RGB32: - buffer = avi_converter_from_rgb32(movie, stream, buffer, size); - break; - default: - break; - } - break; - case AVI_FORMAT_AVI_RGB: - buffer = avi_converter_to_avi_rgb(movie, stream, buffer, size); - break; - case AVI_FORMAT_MJPEG: - buffer = avi_converter_to_mjpeg(movie, stream, buffer, size); - break; - case AVI_FORMAT_RGB32: - buffer = avi_converter_to_rgb32(movie, stream, buffer, size); - break; - default: - break; - } + switch (to) { + case AVI_FORMAT_RGB24: + switch (from) { + case AVI_FORMAT_AVI_RGB: + buffer = avi_converter_from_avi_rgb(movie, stream, buffer, size); + break; + case AVI_FORMAT_MJPEG: + buffer = avi_converter_from_mjpeg(movie, stream, buffer, size); + break; + case AVI_FORMAT_RGB32: + buffer = avi_converter_from_rgb32(movie, stream, buffer, size); + break; + default: + break; + } + break; + case AVI_FORMAT_AVI_RGB: + buffer = avi_converter_to_avi_rgb(movie, stream, buffer, size); + break; + case AVI_FORMAT_MJPEG: + buffer = avi_converter_to_mjpeg(movie, stream, buffer, size); + break; + case AVI_FORMAT_RGB32: + buffer = avi_converter_to_rgb32(movie, stream, buffer, size); + break; + default: + break; + } - return buffer; + return buffer; } int avi_get_data_id(AviFormat format, int stream) { - char fcc[5]; + char fcc[5]; - if (avi_get_format_type(format) == FCC("vids")) - sprintf(fcc, "%2.2ddc", stream); - else if (avi_get_format_type(format) == FCC("auds")) - sprintf(fcc, "%2.2ddc", stream); - else - return 0; + if (avi_get_format_type(format) == FCC("vids")) + sprintf(fcc, "%2.2ddc", stream); + else if (avi_get_format_type(format) == FCC("auds")) + sprintf(fcc, "%2.2ddc", stream); + else + return 0; - return FCC(fcc); + return FCC(fcc); } int avi_get_format_type(AviFormat format) { - switch (format) { - case AVI_FORMAT_RGB24: - case AVI_FORMAT_RGB32: - case AVI_FORMAT_AVI_RGB: - case AVI_FORMAT_MJPEG: - return FCC("vids"); - default: - return 0; - } + switch (format) { + case AVI_FORMAT_RGB24: + case AVI_FORMAT_RGB32: + case AVI_FORMAT_AVI_RGB: + case AVI_FORMAT_MJPEG: + return FCC("vids"); + default: + return 0; + } } int avi_get_format_fcc(AviFormat format) { - switch (format) { - case AVI_FORMAT_RGB24: - case AVI_FORMAT_RGB32: - case AVI_FORMAT_AVI_RGB: - return FCC("DIB "); - case AVI_FORMAT_MJPEG: - return FCC("MJPG"); - default: - return 0; - } + switch (format) { + case AVI_FORMAT_RGB24: + case AVI_FORMAT_RGB32: + case AVI_FORMAT_AVI_RGB: + return FCC("DIB "); + case AVI_FORMAT_MJPEG: + return FCC("MJPG"); + default: + return 0; + } } int avi_get_format_compression(AviFormat format) { - switch (format) { - case AVI_FORMAT_RGB24: - case AVI_FORMAT_RGB32: - case AVI_FORMAT_AVI_RGB: - return 0; - case AVI_FORMAT_MJPEG: - return FCC("MJPG"); - default: - return 0; - } + switch (format) { + case AVI_FORMAT_RGB24: + case AVI_FORMAT_RGB32: + case AVI_FORMAT_AVI_RGB: + return 0; + case AVI_FORMAT_MJPEG: + return FCC("MJPG"); + default: + return 0; + } } diff --git a/source/blender/avi/intern/avi_endian.c b/source/blender/avi/intern/avi_endian.c index 7556f7f5c66..56474e9e329 100644 --- a/source/blender/avi/intern/avi_endian.c +++ b/source/blender/avi/intern/avi_endian.c @@ -24,7 +24,6 @@ * endianness of the system. */ - #include <stdlib.h> #include <string.h> #include <stdio.h> @@ -34,7 +33,7 @@ #include "avi_intern.h" #ifdef __BIG_ENDIAN__ -#include "MEM_guardedalloc.h" +# include "MEM_guardedalloc.h" #endif #ifdef __BIG_ENDIAN__ @@ -42,167 +41,163 @@ /* copied from BLI_endian_switch_inline.h */ static void invert(int *val) { - int tval = *val; - *val = ((tval >> 24)) | - ((tval << 8) & 0x00ff0000) | - ((tval >> 8) & 0x0000ff00) | - ((tval << 24)); + int tval = *val; + *val = ((tval >> 24)) | ((tval << 8) & 0x00ff0000) | ((tval >> 8) & 0x0000ff00) | ((tval << 24)); } static void sinvert(short int *val) { - short tval = *val; - *val = (tval >> 8) | - (tval << 8); + short tval = *val; + *val = (tval >> 8) | (tval << 8); } static void Ichunk(AviChunk *chunk) { - invert(&chunk->fcc); - invert(&chunk->size); + invert(&chunk->fcc); + invert(&chunk->size); } #endif #ifdef __BIG_ENDIAN__ static void Ilist(AviList *list) { - invert(&list->fcc); - invert(&list->size); - invert(&list->ids); + invert(&list->fcc); + invert(&list->size); + invert(&list->ids); } static void Imainh(AviMainHeader *mainh) { - invert(&mainh->fcc); - invert(&mainh->size); - invert(&mainh->MicroSecPerFrame); - invert(&mainh->MaxBytesPerSec); - invert(&mainh->PaddingGranularity); - invert(&mainh->Flags); - invert(&mainh->TotalFrames); - invert(&mainh->InitialFrames); - invert(&mainh->Streams); - invert(&mainh->SuggestedBufferSize); - invert(&mainh->Width); - invert(&mainh->Height); - invert(&mainh->Reserved[0]); - invert(&mainh->Reserved[1]); - invert(&mainh->Reserved[2]); - invert(&mainh->Reserved[3]); + invert(&mainh->fcc); + invert(&mainh->size); + invert(&mainh->MicroSecPerFrame); + invert(&mainh->MaxBytesPerSec); + invert(&mainh->PaddingGranularity); + invert(&mainh->Flags); + invert(&mainh->TotalFrames); + invert(&mainh->InitialFrames); + invert(&mainh->Streams); + invert(&mainh->SuggestedBufferSize); + invert(&mainh->Width); + invert(&mainh->Height); + invert(&mainh->Reserved[0]); + invert(&mainh->Reserved[1]); + invert(&mainh->Reserved[2]); + invert(&mainh->Reserved[3]); } static void Istreamh(AviStreamHeader *streamh) { - invert(&streamh->fcc); - invert(&streamh->size); - invert(&streamh->Type); - invert(&streamh->Handler); - invert(&streamh->Flags); - sinvert(&streamh->Priority); - sinvert(&streamh->Language); - invert(&streamh->InitialFrames); - invert(&streamh->Scale); - invert(&streamh->Rate); - invert(&streamh->Start); - invert(&streamh->Length); - invert(&streamh->SuggestedBufferSize); - invert(&streamh->Quality); - invert(&streamh->SampleSize); - sinvert(&streamh->left); - sinvert(&streamh->right); - sinvert(&streamh->top); - sinvert(&streamh->bottom); + invert(&streamh->fcc); + invert(&streamh->size); + invert(&streamh->Type); + invert(&streamh->Handler); + invert(&streamh->Flags); + sinvert(&streamh->Priority); + sinvert(&streamh->Language); + invert(&streamh->InitialFrames); + invert(&streamh->Scale); + invert(&streamh->Rate); + invert(&streamh->Start); + invert(&streamh->Length); + invert(&streamh->SuggestedBufferSize); + invert(&streamh->Quality); + invert(&streamh->SampleSize); + sinvert(&streamh->left); + sinvert(&streamh->right); + sinvert(&streamh->top); + sinvert(&streamh->bottom); } static void Ibitmaph(AviBitmapInfoHeader *bitmaph) { - invert(&bitmaph->fcc); - invert(&bitmaph->size); - invert(&bitmaph->Size); - invert(&bitmaph->Width); - invert(&bitmaph->Height); - sinvert(&bitmaph->Planes); - sinvert(&bitmaph->BitCount); - invert(&bitmaph->Compression); - invert(&bitmaph->SizeImage); - invert(&bitmaph->XPelsPerMeter); - invert(&bitmaph->YPelsPerMeter); - invert(&bitmaph->ClrUsed); - invert(&bitmaph->ClrImportant); + invert(&bitmaph->fcc); + invert(&bitmaph->size); + invert(&bitmaph->Size); + invert(&bitmaph->Width); + invert(&bitmaph->Height); + sinvert(&bitmaph->Planes); + sinvert(&bitmaph->BitCount); + invert(&bitmaph->Compression); + invert(&bitmaph->SizeImage); + invert(&bitmaph->XPelsPerMeter); + invert(&bitmaph->YPelsPerMeter); + invert(&bitmaph->ClrUsed); + invert(&bitmaph->ClrImportant); } static void Imjpegu(AviMJPEGUnknown *mjpgu) { - invert(&mjpgu->a); - invert(&mjpgu->b); - invert(&mjpgu->c); - invert(&mjpgu->d); - invert(&mjpgu->e); - invert(&mjpgu->f); - invert(&mjpgu->g); + invert(&mjpgu->a); + invert(&mjpgu->b); + invert(&mjpgu->c); + invert(&mjpgu->d); + invert(&mjpgu->e); + invert(&mjpgu->f); + invert(&mjpgu->g); } static void Iindexe(AviIndexEntry *indexe) { - invert(&indexe->ChunkId); - invert(&indexe->Flags); - invert(&indexe->Offset); - invert(&indexe->Size); + invert(&indexe->ChunkId); + invert(&indexe->Flags); + invert(&indexe->Offset); + invert(&indexe->Size); } #endif /* __BIG_ENDIAN__ */ void awrite(AviMovie *movie, void *datain, int block, int size, FILE *fp, int type) { #ifdef __BIG_ENDIAN__ - void *data; - - data = MEM_mallocN(size, "avi endian"); - - memcpy(data, datain, size); - - switch (type) { - case AVI_RAW: - fwrite(data, block, size, fp); - break; - case AVI_CHUNK: - Ichunk((AviChunk *) data); - fwrite(data, block, size, fp); - break; - case AVI_LIST: - Ilist((AviList *) data); - fwrite(data, block, size, fp); - break; - case AVI_MAINH: - Imainh((AviMainHeader *) data); - fwrite(data, block, size, fp); - break; - case AVI_STREAMH: - Istreamh((AviStreamHeader *) data); - fwrite(data, block, size, fp); - break; - case AVI_BITMAPH: - Ibitmaph((AviBitmapInfoHeader *) data); - if (size == sizeof(AviBitmapInfoHeader) + sizeof(AviMJPEGUnknown)) { - Imjpegu((AviMJPEGUnknown *)((char *)data + sizeof(AviBitmapInfoHeader))); - } - fwrite(data, block, size, fp); - break; - case AVI_MJPEGU: - Imjpegu((AviMJPEGUnknown *) data); - fwrite(data, block, size, fp); - break; - case AVI_INDEXE: - Iindexe((AviIndexEntry *) data); - fwrite(data, block, size, fp); - break; - default: - break; - } - - MEM_freeN(data); -#else /* __BIG_ENDIAN__ */ - (void)movie; /* unused */ - (void)type; /* unused */ - fwrite(datain, block, size, fp); + void *data; + + data = MEM_mallocN(size, "avi endian"); + + memcpy(data, datain, size); + + switch (type) { + case AVI_RAW: + fwrite(data, block, size, fp); + break; + case AVI_CHUNK: + Ichunk((AviChunk *)data); + fwrite(data, block, size, fp); + break; + case AVI_LIST: + Ilist((AviList *)data); + fwrite(data, block, size, fp); + break; + case AVI_MAINH: + Imainh((AviMainHeader *)data); + fwrite(data, block, size, fp); + break; + case AVI_STREAMH: + Istreamh((AviStreamHeader *)data); + fwrite(data, block, size, fp); + break; + case AVI_BITMAPH: + Ibitmaph((AviBitmapInfoHeader *)data); + if (size == sizeof(AviBitmapInfoHeader) + sizeof(AviMJPEGUnknown)) { + Imjpegu((AviMJPEGUnknown *)((char *)data + sizeof(AviBitmapInfoHeader))); + } + fwrite(data, block, size, fp); + break; + case AVI_MJPEGU: + Imjpegu((AviMJPEGUnknown *)data); + fwrite(data, block, size, fp); + break; + case AVI_INDEXE: + Iindexe((AviIndexEntry *)data); + fwrite(data, block, size, fp); + break; + default: + break; + } + + MEM_freeN(data); +#else /* __BIG_ENDIAN__ */ + (void)movie; /* unused */ + (void)type; /* unused */ + fwrite(datain, block, size, fp); #endif /* __BIG_ENDIAN__ */ } diff --git a/source/blender/avi/intern/avi_endian.h b/source/blender/avi/intern/avi_endian.h index 733e6366ae6..d1253f488e7 100644 --- a/source/blender/avi/intern/avi_endian.h +++ b/source/blender/avi/intern/avi_endian.h @@ -26,15 +26,15 @@ #ifndef __AVI_ENDIAN_H__ #define __AVI_ENDIAN_H__ -#define AVI_RAW 0 -#define AVI_CHUNK 1 -#define AVI_LIST 2 -#define AVI_MAINH 3 -#define AVI_STREAMH 4 -#define AVI_BITMAPH 5 -#define AVI_INDEXE 6 -#define AVI_MJPEGU 7 +#define AVI_RAW 0 +#define AVI_CHUNK 1 +#define AVI_LIST 2 +#define AVI_MAINH 3 +#define AVI_STREAMH 4 +#define AVI_BITMAPH 5 +#define AVI_INDEXE 6 +#define AVI_MJPEGU 7 void awrite(AviMovie *movie, void *datain, int block, int size, FILE *fp, int type); -#endif /* __AVI_ENDIAN_H__ */ +#endif /* __AVI_ENDIAN_H__ */ diff --git a/source/blender/avi/intern/avi_intern.h b/source/blender/avi/intern/avi_intern.h index d31e4299ff0..6ce91ce7f70 100644 --- a/source/blender/avi/intern/avi_intern.h +++ b/source/blender/avi/intern/avi_intern.h @@ -30,28 +30,32 @@ unsigned int GET_FCC(FILE *fp); unsigned int GET_TCC(FILE *fp); #define PUT_FCC(ch4, fp) \ -{ \ - putc(ch4[0], fp); \ - putc(ch4[1], fp); \ - putc(ch4[2], fp); \ - putc(ch4[3], fp); \ -} (void)0 + { \ + putc(ch4[0], fp); \ + putc(ch4[1], fp); \ + putc(ch4[2], fp); \ + putc(ch4[3], fp); \ + } \ + (void)0 #define PUT_FCCN(num, fp) \ -{ \ - putc((num >> 0) & 0377, fp); \ - putc((num >> 8) & 0377, fp); \ - putc((num >> 16) & 0377, fp); \ - putc((num >> 24) & 0377, fp); \ -} (void)0 + { \ + putc((num >> 0) & 0377, fp); \ + putc((num >> 8) & 0377, fp); \ + putc((num >> 16) & 0377, fp); \ + putc((num >> 24) & 0377, fp); \ + } \ + (void)0 #define PUT_TCC(ch2, fp) \ -{ \ - putc(ch2[0], fp); \ - putc(ch2[1], fp); \ -} (void)0 + { \ + putc(ch2[0], fp); \ + putc(ch2[1], fp); \ + } \ + (void)0 -void *avi_format_convert(AviMovie *movie, int stream, void *buffer, AviFormat from, AviFormat to, size_t *size); +void *avi_format_convert( + AviMovie *movie, int stream, void *buffer, AviFormat from, AviFormat to, size_t *size); int avi_get_data_id(AviFormat format, int stream); int avi_get_format_type(AviFormat format); diff --git a/source/blender/avi/intern/avi_mjpeg.c b/source/blender/avi/intern/avi_mjpeg.c index 249cb57afab..28743619c04 100644 --- a/source/blender/avi/intern/avi_mjpeg.c +++ b/source/blender/avi/intern/avi_mjpeg.c @@ -42,16 +42,19 @@ static void jpegmemsrcmgr_build(j_decompress_ptr dinfo, unsigned char *buffer, s static size_t numbytes; -static void add_huff_table(j_decompress_ptr dinfo, JHUFF_TBL **htblptr, const UINT8 *bits, const UINT8 *val) +static void add_huff_table(j_decompress_ptr dinfo, + JHUFF_TBL **htblptr, + const UINT8 *bits, + const UINT8 *val) { - if (*htblptr == NULL) - *htblptr = jpeg_alloc_huff_table((j_common_ptr) dinfo); + if (*htblptr == NULL) + *htblptr = jpeg_alloc_huff_table((j_common_ptr)dinfo); - memcpy((*htblptr)->bits, bits, sizeof((*htblptr)->bits)); - memcpy((*htblptr)->huffval, val, sizeof((*htblptr)->huffval)); + memcpy((*htblptr)->bits, bits, sizeof((*htblptr)->bits)); + memcpy((*htblptr)->huffval, val, sizeof((*htblptr)->huffval)); - /* Initialize sent_table false so table will be written to JPEG file. */ - (*htblptr)->sent_table = false; + /* Initialize sent_table false so table will be written to JPEG file. */ + (*htblptr)->sent_table = false; } /* Set up the standard Huffman tables (cf. JPEG standard section K.3) */ @@ -59,392 +62,477 @@ static void add_huff_table(j_decompress_ptr dinfo, JHUFF_TBL **htblptr, const UI static void std_huff_tables(j_decompress_ptr dinfo) { - static const UINT8 bits_dc_luminance[17] = - { /* 0-base */ - 0, 0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, - }; - static const UINT8 val_dc_luminance[] = - { - 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, - }; - - static const UINT8 bits_dc_chrominance[17] = - { /* 0-base */ - 0, 0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, - }; - static const UINT8 val_dc_chrominance[] = - { - 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, - }; - - static const UINT8 bits_ac_luminance[17] = - { /* 0-base */ - 0, 0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 0x7d, - }; - static const UINT8 val_ac_luminance[] = - { - 0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12, - 0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07, - 0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08, - 0x23, 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0, - 0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a, 0x16, - 0x17, 0x18, 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28, - 0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, - 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, - 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, - 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, - 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, - 0x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, - 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, - 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, - 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, - 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5, - 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4, - 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2, - 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, - 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, - 0xf9, 0xfa, - }; - static const UINT8 bits_ac_chrominance[17] = - { /* 0-base */ - 0, 0, 2, 1, 2, 4, 4, 3, 4, 7, 5, 4, 4, 0, 1, 2, 0x77, - }; - static const UINT8 val_ac_chrominance[] = - { - 0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21, - 0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71, - 0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91, - 0xa1, 0xb1, 0xc1, 0x09, 0x23, 0x33, 0x52, 0xf0, - 0x15, 0x62, 0x72, 0xd1, 0x0a, 0x16, 0x24, 0x34, - 0xe1, 0x25, 0xf1, 0x17, 0x18, 0x19, 0x1a, 0x26, - 0x27, 0x28, 0x29, 0x2a, 0x35, 0x36, 0x37, 0x38, - 0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, - 0x49, 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, - 0x59, 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, - 0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, - 0x79, 0x7a, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, - 0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, - 0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, - 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, - 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, - 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, - 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, - 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, - 0xea, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, - 0xf9, 0xfa, - }; - - add_huff_table(dinfo, &dinfo->dc_huff_tbl_ptrs[0], - bits_dc_luminance, val_dc_luminance); - add_huff_table(dinfo, &dinfo->ac_huff_tbl_ptrs[0], - bits_ac_luminance, val_ac_luminance); - add_huff_table(dinfo, &dinfo->dc_huff_tbl_ptrs[1], - bits_dc_chrominance, val_dc_chrominance); - add_huff_table(dinfo, &dinfo->ac_huff_tbl_ptrs[1], - bits_ac_chrominance, val_ac_chrominance); + static const UINT8 bits_dc_luminance[17] = { + /* 0-base */ + 0, + 0, + 1, + 5, + 1, + 1, + 1, + 1, + 1, + 1, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + }; + static const UINT8 val_dc_luminance[] = { + 0, + 1, + 2, + 3, + 4, + 5, + 6, + 7, + 8, + 9, + 10, + 11, + }; + + static const UINT8 bits_dc_chrominance[17] = { + /* 0-base */ + 0, + 0, + 3, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 0, + 0, + 0, + 0, + 0, + }; + static const UINT8 val_dc_chrominance[] = { + 0, + 1, + 2, + 3, + 4, + 5, + 6, + 7, + 8, + 9, + 10, + 11, + }; + + static const UINT8 bits_ac_luminance[17] = { + /* 0-base */ + 0, + 0, + 2, + 1, + 3, + 3, + 2, + 4, + 3, + 5, + 5, + 4, + 4, + 0, + 0, + 1, + 0x7d, + }; + static const UINT8 val_ac_luminance[] = { + 0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12, 0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, + 0x07, 0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08, 0x23, 0x42, 0xb1, 0xc1, 0x15, 0x52, + 0xd1, 0xf0, 0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x25, + 0x26, 0x27, 0x28, 0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x43, 0x44, 0x45, + 0x46, 0x47, 0x48, 0x49, 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x63, 0x64, + 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x83, + 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, + 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, + 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, + 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, + 0xe9, 0xea, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, + }; + static const UINT8 bits_ac_chrominance[17] = { + /* 0-base */ + 0, + 0, + 2, + 1, + 2, + 4, + 4, + 3, + 4, + 7, + 5, + 4, + 4, + 0, + 1, + 2, + 0x77, + }; + static const UINT8 val_ac_chrominance[] = { + 0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21, 0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, + 0x71, 0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91, 0xa1, 0xb1, 0xc1, 0x09, 0x23, 0x33, + 0x52, 0xf0, 0x15, 0x62, 0x72, 0xd1, 0x0a, 0x16, 0x24, 0x34, 0xe1, 0x25, 0xf1, 0x17, 0x18, + 0x19, 0x1a, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x43, 0x44, + 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x63, + 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, + 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, + 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, + 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, + 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, + 0xe8, 0xe9, 0xea, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, + }; + + add_huff_table(dinfo, &dinfo->dc_huff_tbl_ptrs[0], bits_dc_luminance, val_dc_luminance); + add_huff_table(dinfo, &dinfo->ac_huff_tbl_ptrs[0], bits_ac_luminance, val_ac_luminance); + add_huff_table(dinfo, &dinfo->dc_huff_tbl_ptrs[1], bits_dc_chrominance, val_dc_chrominance); + add_huff_table(dinfo, &dinfo->ac_huff_tbl_ptrs[1], bits_ac_chrominance, val_ac_chrominance); } -static int Decode_JPEG(unsigned char *inBuffer, unsigned char *outBuffer, unsigned int width, unsigned int height, size_t bufsize) +static int Decode_JPEG(unsigned char *inBuffer, + unsigned char *outBuffer, + unsigned int width, + unsigned int height, + size_t bufsize) { - struct jpeg_decompress_struct dinfo; - struct jpeg_error_mgr jerr; - - (void)width; /* unused */ - - numbytes = 0; - - dinfo.err = jpeg_std_error(&jerr); - jpeg_create_decompress(&dinfo); - jpegmemsrcmgr_build(&dinfo, inBuffer, bufsize); - jpeg_read_header(&dinfo, true); - if (dinfo.dc_huff_tbl_ptrs[0] == NULL) { - std_huff_tables(&dinfo); - } - dinfo.out_color_space = JCS_RGB; - dinfo.dct_method = JDCT_IFAST; - - jpeg_start_decompress(&dinfo); - - size_t rowstride = dinfo.output_width * dinfo.output_components; - for (size_t y = 0; y < dinfo.output_height; y++) { - jpeg_read_scanlines(&dinfo, (JSAMPARRAY) &outBuffer, 1); - outBuffer += rowstride; - } - jpeg_finish_decompress(&dinfo); - - if (dinfo.output_height >= height) return 0; - - inBuffer += numbytes; - jpegmemsrcmgr_build(&dinfo, inBuffer, bufsize - numbytes); - - numbytes = 0; - jpeg_read_header(&dinfo, true); - if (dinfo.dc_huff_tbl_ptrs[0] == NULL) { - std_huff_tables(&dinfo); - } - - jpeg_start_decompress(&dinfo); - rowstride = dinfo.output_width * dinfo.output_components; - for (size_t y = 0; y < dinfo.output_height; y++) { - jpeg_read_scanlines(&dinfo, (JSAMPARRAY) &outBuffer, 1); - outBuffer += rowstride; - } - jpeg_finish_decompress(&dinfo); - jpeg_destroy_decompress(&dinfo); - - return 1; + struct jpeg_decompress_struct dinfo; + struct jpeg_error_mgr jerr; + + (void)width; /* unused */ + + numbytes = 0; + + dinfo.err = jpeg_std_error(&jerr); + jpeg_create_decompress(&dinfo); + jpegmemsrcmgr_build(&dinfo, inBuffer, bufsize); + jpeg_read_header(&dinfo, true); + if (dinfo.dc_huff_tbl_ptrs[0] == NULL) { + std_huff_tables(&dinfo); + } + dinfo.out_color_space = JCS_RGB; + dinfo.dct_method = JDCT_IFAST; + + jpeg_start_decompress(&dinfo); + + size_t rowstride = dinfo.output_width * dinfo.output_components; + for (size_t y = 0; y < dinfo.output_height; y++) { + jpeg_read_scanlines(&dinfo, (JSAMPARRAY)&outBuffer, 1); + outBuffer += rowstride; + } + jpeg_finish_decompress(&dinfo); + + if (dinfo.output_height >= height) + return 0; + + inBuffer += numbytes; + jpegmemsrcmgr_build(&dinfo, inBuffer, bufsize - numbytes); + + numbytes = 0; + jpeg_read_header(&dinfo, true); + if (dinfo.dc_huff_tbl_ptrs[0] == NULL) { + std_huff_tables(&dinfo); + } + + jpeg_start_decompress(&dinfo); + rowstride = dinfo.output_width * dinfo.output_components; + for (size_t y = 0; y < dinfo.output_height; y++) { + jpeg_read_scanlines(&dinfo, (JSAMPARRAY)&outBuffer, 1); + outBuffer += rowstride; + } + jpeg_finish_decompress(&dinfo); + jpeg_destroy_decompress(&dinfo); + + return 1; } -static void Compress_JPEG(int quality, unsigned char *outbuffer, const unsigned char *inBuffer, int width, int height, size_t bufsize) +static void Compress_JPEG(int quality, + unsigned char *outbuffer, + const unsigned char *inBuffer, + int width, + int height, + size_t bufsize) { - struct jpeg_compress_struct cinfo; - struct jpeg_error_mgr jerr; - unsigned char marker[60]; - - cinfo.err = jpeg_std_error(&jerr); - jpeg_create_compress(&cinfo); - jpegmemdestmgr_build(&cinfo, outbuffer, bufsize); - - cinfo.image_width = width; - cinfo.image_height = height; - cinfo.input_components = 3; - cinfo.in_color_space = JCS_RGB; - - jpeg_set_defaults(&cinfo); - jpeg_set_colorspace(&cinfo, JCS_YCbCr); - - jpeg_set_quality(&cinfo, quality, true); - - cinfo.dc_huff_tbl_ptrs[0]->sent_table = true; - cinfo.dc_huff_tbl_ptrs[1]->sent_table = true; - cinfo.ac_huff_tbl_ptrs[0]->sent_table = true; - cinfo.ac_huff_tbl_ptrs[1]->sent_table = true; - - cinfo.comp_info[0].component_id = 0; - cinfo.comp_info[0].v_samp_factor = 1; - cinfo.comp_info[1].component_id = 1; - cinfo.comp_info[2].component_id = 2; - - cinfo.write_JFIF_header = false; - - jpeg_start_compress(&cinfo, false); - - int i = 0; - marker[i++] = 'A'; - marker[i++] = 'V'; - marker[i++] = 'I'; - marker[i++] = '1'; - marker[i++] = 0; - while (i < 60) - marker[i++] = 32; - - jpeg_write_marker(&cinfo, JPEG_APP0, marker, 60); - - i = 0; - while (i < 60) - marker[i++] = 0; - - jpeg_write_marker(&cinfo, JPEG_COM, marker, 60); - - size_t rowstride = cinfo.image_width * cinfo.input_components; - for (size_t y = 0; y < cinfo.image_height; y++) { - jpeg_write_scanlines(&cinfo, (JSAMPARRAY) &inBuffer, 1); - inBuffer += rowstride; - } - jpeg_finish_compress(&cinfo); - jpeg_destroy_compress(&cinfo); + struct jpeg_compress_struct cinfo; + struct jpeg_error_mgr jerr; + unsigned char marker[60]; + + cinfo.err = jpeg_std_error(&jerr); + jpeg_create_compress(&cinfo); + jpegmemdestmgr_build(&cinfo, outbuffer, bufsize); + + cinfo.image_width = width; + cinfo.image_height = height; + cinfo.input_components = 3; + cinfo.in_color_space = JCS_RGB; + + jpeg_set_defaults(&cinfo); + jpeg_set_colorspace(&cinfo, JCS_YCbCr); + + jpeg_set_quality(&cinfo, quality, true); + + cinfo.dc_huff_tbl_ptrs[0]->sent_table = true; + cinfo.dc_huff_tbl_ptrs[1]->sent_table = true; + cinfo.ac_huff_tbl_ptrs[0]->sent_table = true; + cinfo.ac_huff_tbl_ptrs[1]->sent_table = true; + + cinfo.comp_info[0].component_id = 0; + cinfo.comp_info[0].v_samp_factor = 1; + cinfo.comp_info[1].component_id = 1; + cinfo.comp_info[2].component_id = 2; + + cinfo.write_JFIF_header = false; + + jpeg_start_compress(&cinfo, false); + + int i = 0; + marker[i++] = 'A'; + marker[i++] = 'V'; + marker[i++] = 'I'; + marker[i++] = '1'; + marker[i++] = 0; + while (i < 60) + marker[i++] = 32; + + jpeg_write_marker(&cinfo, JPEG_APP0, marker, 60); + + i = 0; + while (i < 60) + marker[i++] = 0; + + jpeg_write_marker(&cinfo, JPEG_COM, marker, 60); + + size_t rowstride = cinfo.image_width * cinfo.input_components; + for (size_t y = 0; y < cinfo.image_height; y++) { + jpeg_write_scanlines(&cinfo, (JSAMPARRAY)&inBuffer, 1); + inBuffer += rowstride; + } + jpeg_finish_compress(&cinfo); + jpeg_destroy_compress(&cinfo); } static void interlace(unsigned char *to, unsigned char *from, int width, int height) { - size_t i, rowstride = width * 3; - - for (i = 0; i < height; i++) { - if (i & 1) - memcpy(&to[i * rowstride], &from[(i / 2 + height / 2) * rowstride], rowstride); - else - memcpy(&to[i * rowstride], &from[(i / 2) * rowstride], rowstride); - } + size_t i, rowstride = width * 3; + + for (i = 0; i < height; i++) { + if (i & 1) + memcpy(&to[i * rowstride], &from[(i / 2 + height / 2) * rowstride], rowstride); + else + memcpy(&to[i * rowstride], &from[(i / 2) * rowstride], rowstride); + } } static void deinterlace(int odd, unsigned char *to, unsigned char *from, int width, int height) { - size_t i, rowstride = width * 3; - - for (i = 0; i < height; i++) { - if ((i & 1) == odd) - memcpy(&to[(i / 2 + height / 2) * rowstride], &from[i * rowstride], rowstride); - else - memcpy(&to[(i / 2) * rowstride], &from[i * rowstride], rowstride); - } + size_t i, rowstride = width * 3; + + for (i = 0; i < height; i++) { + if ((i & 1) == odd) + memcpy(&to[(i / 2 + height / 2) * rowstride], &from[i * rowstride], rowstride); + else + memcpy(&to[(i / 2) * rowstride], &from[i * rowstride], rowstride); + } } void *avi_converter_from_mjpeg(AviMovie *movie, int stream, unsigned char *buffer, size_t *size) { - int deint; - unsigned char *buf; - - (void)stream; /* unused */ - - buf = imb_alloc_pixels(movie->header->Height, movie->header->Width, 3, sizeof(unsigned char), "avi.avi_converter_from_mjpeg 1"); - if (!buf) { - return NULL; - } - - deint = Decode_JPEG(buffer, buf, movie->header->Width, movie->header->Height, *size); - - MEM_freeN(buffer); - - if (deint) { - buffer = imb_alloc_pixels(movie->header->Height, movie->header->Width, 3, sizeof(unsigned char), "avi.avi_converter_from_mjpeg 2"); - if (buffer) { - interlace(buffer, buf, movie->header->Width, movie->header->Height); - } - MEM_freeN(buf); - - buf = buffer; - } - - return buf; + int deint; + unsigned char *buf; + + (void)stream; /* unused */ + + buf = imb_alloc_pixels(movie->header->Height, + movie->header->Width, + 3, + sizeof(unsigned char), + "avi.avi_converter_from_mjpeg 1"); + if (!buf) { + return NULL; + } + + deint = Decode_JPEG(buffer, buf, movie->header->Width, movie->header->Height, *size); + + MEM_freeN(buffer); + + if (deint) { + buffer = imb_alloc_pixels(movie->header->Height, + movie->header->Width, + 3, + sizeof(unsigned char), + "avi.avi_converter_from_mjpeg 2"); + if (buffer) { + interlace(buffer, buf, movie->header->Width, movie->header->Height); + } + MEM_freeN(buf); + + buf = buffer; + } + + return buf; } void *avi_converter_to_mjpeg(AviMovie *movie, int stream, unsigned char *buffer, size_t *size) { - unsigned char *buf; - size_t bufsize = *size; - - numbytes = 0; - *size = 0; - - buf = imb_alloc_pixels(movie->header->Height, movie->header->Width, 3, sizeof(unsigned char), "avi.avi_converter_to_mjpeg 1"); - if (!buf) { - return NULL; - } - - if (!movie->interlace) { - Compress_JPEG(movie->streams[stream].sh.Quality / 100, - buf, buffer, - movie->header->Width, - movie->header->Height, - bufsize); - *size += numbytes; - } - else { - deinterlace(movie->odd_fields, buf, buffer, movie->header->Width, movie->header->Height); - MEM_freeN(buffer); - - buffer = buf; - buf = imb_alloc_pixels(movie->header->Height, movie->header->Width, 3, sizeof(unsigned char), "avi.avi_converter_to_mjpeg 1"); - - if (buf) { - Compress_JPEG( - movie->streams[stream].sh.Quality / 100, - buf, buffer, - movie->header->Width, - movie->header->Height / 2, - bufsize / 2); - *size += numbytes; - numbytes = 0; - Compress_JPEG( - movie->streams[stream].sh.Quality / 100, - buf + *size, buffer + (size_t)(movie->header->Height / 2) * (size_t)movie->header->Width * 3, - movie->header->Width, - movie->header->Height / 2, - bufsize / 2); - *size += numbytes; - } - } - - MEM_freeN(buffer); - return buf; + unsigned char *buf; + size_t bufsize = *size; + + numbytes = 0; + *size = 0; + + buf = imb_alloc_pixels(movie->header->Height, + movie->header->Width, + 3, + sizeof(unsigned char), + "avi.avi_converter_to_mjpeg 1"); + if (!buf) { + return NULL; + } + + if (!movie->interlace) { + Compress_JPEG(movie->streams[stream].sh.Quality / 100, + buf, + buffer, + movie->header->Width, + movie->header->Height, + bufsize); + *size += numbytes; + } + else { + deinterlace(movie->odd_fields, buf, buffer, movie->header->Width, movie->header->Height); + MEM_freeN(buffer); + + buffer = buf; + buf = imb_alloc_pixels(movie->header->Height, + movie->header->Width, + 3, + sizeof(unsigned char), + "avi.avi_converter_to_mjpeg 1"); + + if (buf) { + Compress_JPEG(movie->streams[stream].sh.Quality / 100, + buf, + buffer, + movie->header->Width, + movie->header->Height / 2, + bufsize / 2); + *size += numbytes; + numbytes = 0; + Compress_JPEG(movie->streams[stream].sh.Quality / 100, + buf + *size, + buffer + + (size_t)(movie->header->Height / 2) * (size_t)movie->header->Width * 3, + movie->header->Width, + movie->header->Height / 2, + bufsize / 2); + *size += numbytes; + } + } + + MEM_freeN(buffer); + return buf; } - /* Compression from memory */ static void jpegmemdestmgr_init_destination(j_compress_ptr cinfo) { - (void)cinfo; /* unused */ + (void)cinfo; /* unused */ } static boolean jpegmemdestmgr_empty_output_buffer(j_compress_ptr cinfo) { - (void)cinfo; /* unused */ - return true; + (void)cinfo; /* unused */ + return true; } static void jpegmemdestmgr_term_destination(j_compress_ptr cinfo) { - numbytes -= cinfo->dest->free_in_buffer; + numbytes -= cinfo->dest->free_in_buffer; - MEM_freeN(cinfo->dest); + MEM_freeN(cinfo->dest); } static void jpegmemdestmgr_build(j_compress_ptr cinfo, unsigned char *buffer, size_t bufsize) { - cinfo->dest = MEM_mallocN(sizeof(*(cinfo->dest)), "avi.jpegmemdestmgr_build"); + cinfo->dest = MEM_mallocN(sizeof(*(cinfo->dest)), "avi.jpegmemdestmgr_build"); - cinfo->dest->init_destination = jpegmemdestmgr_init_destination; - cinfo->dest->empty_output_buffer = jpegmemdestmgr_empty_output_buffer; - cinfo->dest->term_destination = jpegmemdestmgr_term_destination; + cinfo->dest->init_destination = jpegmemdestmgr_init_destination; + cinfo->dest->empty_output_buffer = jpegmemdestmgr_empty_output_buffer; + cinfo->dest->term_destination = jpegmemdestmgr_term_destination; - cinfo->dest->next_output_byte = buffer; - cinfo->dest->free_in_buffer = bufsize; + cinfo->dest->next_output_byte = buffer; + cinfo->dest->free_in_buffer = bufsize; - numbytes = bufsize; + numbytes = bufsize; } /* Decompression from memory */ static void jpegmemsrcmgr_init_source(j_decompress_ptr dinfo) { - (void)dinfo; + (void)dinfo; } static boolean jpegmemsrcmgr_fill_input_buffer(j_decompress_ptr dinfo) { - unsigned char *buf = (unsigned char *) dinfo->src->next_input_byte - 2; + unsigned char *buf = (unsigned char *)dinfo->src->next_input_byte - 2; - /* if we get called, must have run out of data */ - WARNMS(dinfo, JWRN_JPEG_EOF); + /* if we get called, must have run out of data */ + WARNMS(dinfo, JWRN_JPEG_EOF); - buf[0] = (JOCTET) 0xFF; - buf[1] = (JOCTET) JPEG_EOI; + buf[0] = (JOCTET)0xFF; + buf[1] = (JOCTET)JPEG_EOI; - dinfo->src->next_input_byte = buf; - dinfo->src->bytes_in_buffer = 2; + dinfo->src->next_input_byte = buf; + dinfo->src->bytes_in_buffer = 2; - return true; + return true; } static void jpegmemsrcmgr_skip_input_data(j_decompress_ptr dinfo, long skipcnt) { - if (dinfo->src->bytes_in_buffer < skipcnt) - skipcnt = dinfo->src->bytes_in_buffer; + if (dinfo->src->bytes_in_buffer < skipcnt) + skipcnt = dinfo->src->bytes_in_buffer; - dinfo->src->next_input_byte += skipcnt; - dinfo->src->bytes_in_buffer -= skipcnt; + dinfo->src->next_input_byte += skipcnt; + dinfo->src->bytes_in_buffer -= skipcnt; } static void jpegmemsrcmgr_term_source(j_decompress_ptr dinfo) { - numbytes -= dinfo->src->bytes_in_buffer; + numbytes -= dinfo->src->bytes_in_buffer; - MEM_freeN(dinfo->src); + MEM_freeN(dinfo->src); } static void jpegmemsrcmgr_build(j_decompress_ptr dinfo, unsigned char *buffer, size_t bufsize) { - dinfo->src = MEM_mallocN(sizeof(*(dinfo->src)), "avi.jpegmemsrcmgr_build"); + dinfo->src = MEM_mallocN(sizeof(*(dinfo->src)), "avi.jpegmemsrcmgr_build"); - dinfo->src->init_source = jpegmemsrcmgr_init_source; - dinfo->src->fill_input_buffer = jpegmemsrcmgr_fill_input_buffer; - dinfo->src->skip_input_data = jpegmemsrcmgr_skip_input_data; - dinfo->src->resync_to_restart = jpeg_resync_to_restart; - dinfo->src->term_source = jpegmemsrcmgr_term_source; + dinfo->src->init_source = jpegmemsrcmgr_init_source; + dinfo->src->fill_input_buffer = jpegmemsrcmgr_fill_input_buffer; + dinfo->src->skip_input_data = jpegmemsrcmgr_skip_input_data; + dinfo->src->resync_to_restart = jpeg_resync_to_restart; + dinfo->src->term_source = jpegmemsrcmgr_term_source; - dinfo->src->bytes_in_buffer = bufsize; - dinfo->src->next_input_byte = buffer; + dinfo->src->bytes_in_buffer = bufsize; + dinfo->src->next_input_byte = buffer; - numbytes = bufsize; + numbytes = bufsize; } diff --git a/source/blender/avi/intern/avi_options.c b/source/blender/avi/intern/avi_options.c index a8b0bd68fb1..d491f3bb2d0 100644 --- a/source/blender/avi/intern/avi_options.c +++ b/source/blender/avi/intern/avi_options.c @@ -34,89 +34,113 @@ /* avi_set_compress_options gets its own file... now don't WE feel important? */ -AviError AVI_set_compress_option(AviMovie *movie, int option_type, int stream, AviOption option, void *opt_data) +AviError AVI_set_compress_option( + AviMovie *movie, int option_type, int stream, AviOption option, void *opt_data) { - int i; - int useconds; - - (void)stream; /* unused */ - - if (movie->header->TotalFrames != 0) /* Can't change params after we have already started writing frames */ - return AVI_ERROR_OPTION; - - switch (option_type) { - case AVI_OPTION_TYPE_MAIN: - switch (option) { - case AVI_OPTION_WIDTH: - movie->header->Width = *((int *) opt_data); - movie->header->SuggestedBufferSize = movie->header->Width * movie->header->Height * 3; - - for (i = 0; i < movie->header->Streams; i++) { - if (avi_get_format_type(movie->streams[i].format) == FCC("vids")) { - ((AviBitmapInfoHeader *) movie->streams[i].sf)->Width = *((int *) opt_data); - movie->streams[i].sh.SuggestedBufferSize = movie->header->SuggestedBufferSize; - movie->streams[i].sh.right = *((int *) opt_data); - ((AviBitmapInfoHeader *) movie->streams[i].sf)->SizeImage = movie->header->SuggestedBufferSize; - fseek(movie->fp, movie->offset_table[1 + i * 2 + 1], SEEK_SET); - awrite(movie, movie->streams[i].sf, 1, movie->streams[i].sf_size, movie->fp, AVI_BITMAPH); - } - } - - break; - - case AVI_OPTION_HEIGHT: - movie->header->Height = *((int *) opt_data); - movie->header->SuggestedBufferSize = movie->header->Width * movie->header->Height * 3; - - for (i = 0; i < movie->header->Streams; i++) { - if (avi_get_format_type(movie->streams[i].format) == FCC("vids")) { - ((AviBitmapInfoHeader *) movie->streams[i].sf)->Height = *((int *) opt_data); - movie->streams[i].sh.SuggestedBufferSize = movie->header->SuggestedBufferSize; - movie->streams[i].sh.bottom = *((int *) opt_data); - ((AviBitmapInfoHeader *) movie->streams[i].sf)->SizeImage = movie->header->SuggestedBufferSize; - fseek(movie->fp, movie->offset_table[1 + i * 2 + 1], SEEK_SET); - awrite(movie, movie->streams[i].sf, 1, movie->streams[i].sf_size, movie->fp, AVI_BITMAPH); - } - } - - break; - - case AVI_OPTION_QUALITY: - for (i = 0; i < movie->header->Streams; i++) { - if (avi_get_format_type(movie->streams[i].format) == FCC("vids")) { - movie->streams[i].sh.Quality = (*((int *) opt_data)) * 100; - fseek(movie->fp, movie->offset_table[1 + i * 2 + 1], SEEK_SET); - awrite(movie, movie->streams[i].sf, 1, movie->streams[i].sf_size, movie->fp, AVI_BITMAPH); - } - } - break; - - case AVI_OPTION_FRAMERATE: - useconds = (int)(1000000 / (*((double *) opt_data))); - if (useconds) - movie->header->MicroSecPerFrame = useconds; - - for (i = 0; i < movie->header->Streams; i++) { - if (avi_get_format_type(movie->streams[i].format) == FCC("vids")) { - movie->streams[i].sh.Scale = movie->header->MicroSecPerFrame; - fseek(movie->fp, movie->offset_table[1 + i * 2 + 1], SEEK_SET); - awrite(movie, movie->streams[i].sf, 1, movie->streams[i].sf_size, movie->fp, AVI_BITMAPH); - } - } - break; - } - - fseek(movie->fp, movie->offset_table[0], SEEK_SET); - awrite(movie, movie->header, 1, sizeof(AviMainHeader), movie->fp, AVI_MAINH); - - break; - case AVI_OPTION_TYPE_STRH: - break; - case AVI_OPTION_TYPE_STRF: - break; - default: - return AVI_ERROR_OPTION; - } - - return AVI_ERROR_NONE; + int i; + int useconds; + + (void)stream; /* unused */ + + if (movie->header->TotalFrames != + 0) /* Can't change params after we have already started writing frames */ + return AVI_ERROR_OPTION; + + switch (option_type) { + case AVI_OPTION_TYPE_MAIN: + switch (option) { + case AVI_OPTION_WIDTH: + movie->header->Width = *((int *)opt_data); + movie->header->SuggestedBufferSize = movie->header->Width * movie->header->Height * 3; + + for (i = 0; i < movie->header->Streams; i++) { + if (avi_get_format_type(movie->streams[i].format) == FCC("vids")) { + ((AviBitmapInfoHeader *)movie->streams[i].sf)->Width = *((int *)opt_data); + movie->streams[i].sh.SuggestedBufferSize = movie->header->SuggestedBufferSize; + movie->streams[i].sh.right = *((int *)opt_data); + ((AviBitmapInfoHeader *)movie->streams[i].sf)->SizeImage = + movie->header->SuggestedBufferSize; + fseek(movie->fp, movie->offset_table[1 + i * 2 + 1], SEEK_SET); + awrite(movie, + movie->streams[i].sf, + 1, + movie->streams[i].sf_size, + movie->fp, + AVI_BITMAPH); + } + } + + break; + + case AVI_OPTION_HEIGHT: + movie->header->Height = *((int *)opt_data); + movie->header->SuggestedBufferSize = movie->header->Width * movie->header->Height * 3; + + for (i = 0; i < movie->header->Streams; i++) { + if (avi_get_format_type(movie->streams[i].format) == FCC("vids")) { + ((AviBitmapInfoHeader *)movie->streams[i].sf)->Height = *((int *)opt_data); + movie->streams[i].sh.SuggestedBufferSize = movie->header->SuggestedBufferSize; + movie->streams[i].sh.bottom = *((int *)opt_data); + ((AviBitmapInfoHeader *)movie->streams[i].sf)->SizeImage = + movie->header->SuggestedBufferSize; + fseek(movie->fp, movie->offset_table[1 + i * 2 + 1], SEEK_SET); + awrite(movie, + movie->streams[i].sf, + 1, + movie->streams[i].sf_size, + movie->fp, + AVI_BITMAPH); + } + } + + break; + + case AVI_OPTION_QUALITY: + for (i = 0; i < movie->header->Streams; i++) { + if (avi_get_format_type(movie->streams[i].format) == FCC("vids")) { + movie->streams[i].sh.Quality = (*((int *)opt_data)) * 100; + fseek(movie->fp, movie->offset_table[1 + i * 2 + 1], SEEK_SET); + awrite(movie, + movie->streams[i].sf, + 1, + movie->streams[i].sf_size, + movie->fp, + AVI_BITMAPH); + } + } + break; + + case AVI_OPTION_FRAMERATE: + useconds = (int)(1000000 / (*((double *)opt_data))); + if (useconds) + movie->header->MicroSecPerFrame = useconds; + + for (i = 0; i < movie->header->Streams; i++) { + if (avi_get_format_type(movie->streams[i].format) == FCC("vids")) { + movie->streams[i].sh.Scale = movie->header->MicroSecPerFrame; + fseek(movie->fp, movie->offset_table[1 + i * 2 + 1], SEEK_SET); + awrite(movie, + movie->streams[i].sf, + 1, + movie->streams[i].sf_size, + movie->fp, + AVI_BITMAPH); + } + } + break; + } + + fseek(movie->fp, movie->offset_table[0], SEEK_SET); + awrite(movie, movie->header, 1, sizeof(AviMainHeader), movie->fp, AVI_MAINH); + + break; + case AVI_OPTION_TYPE_STRH: + break; + case AVI_OPTION_TYPE_STRF: + break; + default: + return AVI_ERROR_OPTION; + } + + return AVI_ERROR_NONE; } diff --git a/source/blender/avi/intern/avi_rgb.c b/source/blender/avi/intern/avi_rgb.c index 55a79a72bcf..8d81b561403 100644 --- a/source/blender/avi/intern/avi_rgb.c +++ b/source/blender/avi/intern/avi_rgb.c @@ -37,107 +37,116 @@ void *avi_converter_from_avi_rgb(AviMovie *movie, int stream, unsigned char *buffer, size_t *size) { - unsigned char *buf; - AviBitmapInfoHeader *bi; - short bits = 32; + unsigned char *buf; + AviBitmapInfoHeader *bi; + short bits = 32; - (void)size; /* unused */ + (void)size; /* unused */ - bi = (AviBitmapInfoHeader *) movie->streams[stream].sf; - if (bi) bits = bi->BitCount; + bi = (AviBitmapInfoHeader *)movie->streams[stream].sf; + if (bi) + bits = bi->BitCount; - if (bits == 16) { - unsigned short *pxl; - unsigned char *to; + if (bits == 16) { + unsigned short *pxl; + unsigned char *to; #ifdef __BIG_ENDIAN__ - unsigned char *pxla; + unsigned char *pxla; #endif - buf = imb_alloc_pixels(movie->header->Height, movie->header->Width, 3, sizeof(unsigned char), "fromavirgbbuf"); + buf = imb_alloc_pixels( + movie->header->Height, movie->header->Width, 3, sizeof(unsigned char), "fromavirgbbuf"); - if (buf) { - size_t y = movie->header->Height; - to = buf; + if (buf) { + size_t y = movie->header->Height; + to = buf; - while (y--) { - pxl = (unsigned short *) (buffer + y * movie->header->Width * 2); + while (y--) { + pxl = (unsigned short *)(buffer + y * movie->header->Width * 2); #ifdef __BIG_ENDIAN__ - pxla = (unsigned char *)pxl; + pxla = (unsigned char *)pxl; #endif - size_t x = movie->header->Width; - while (x--) { + size_t x = movie->header->Width; + while (x--) { #ifdef __BIG_ENDIAN__ - int i = pxla[0]; - pxla[0] = pxla[1]; - pxla[1] = i; + int i = pxla[0]; + pxla[0] = pxla[1]; + pxla[1] = i; - pxla += 2; + pxla += 2; #endif - *(to++) = ((*pxl >> 10) & 0x1f) * 8; - *(to++) = ((*pxl >> 5) & 0x1f) * 8; - *(to++) = (*pxl & 0x1f) * 8; - pxl++; - } - } - } - - MEM_freeN(buffer); - - return buf; - } - else { - buf = imb_alloc_pixels(movie->header->Height, movie->header->Width, 3, sizeof(unsigned char), "fromavirgbbuf"); - - if (buf) { - size_t rowstride = movie->header->Width * 3; - if ((bits != 16) && (movie->header->Width % 2)) rowstride++; - - for (size_t y = 0; y < movie->header->Height; y++) { - memcpy(&buf[y * movie->header->Width * 3], &buffer[((movie->header->Height - 1) - y) * rowstride], movie->header->Width * 3); - } - - for (size_t y = 0; y < (size_t)movie->header->Height * (size_t)movie->header->Width * 3; y += 3) { - int i = buf[y]; - buf[y] = buf[y + 2]; - buf[y + 2] = i; - } - } - - MEM_freeN(buffer); - - return buf; - } + *(to++) = ((*pxl >> 10) & 0x1f) * 8; + *(to++) = ((*pxl >> 5) & 0x1f) * 8; + *(to++) = (*pxl & 0x1f) * 8; + pxl++; + } + } + } + + MEM_freeN(buffer); + + return buf; + } + else { + buf = imb_alloc_pixels( + movie->header->Height, movie->header->Width, 3, sizeof(unsigned char), "fromavirgbbuf"); + + if (buf) { + size_t rowstride = movie->header->Width * 3; + if ((bits != 16) && (movie->header->Width % 2)) + rowstride++; + + for (size_t y = 0; y < movie->header->Height; y++) { + memcpy(&buf[y * movie->header->Width * 3], + &buffer[((movie->header->Height - 1) - y) * rowstride], + movie->header->Width * 3); + } + + for (size_t y = 0; y < (size_t)movie->header->Height * (size_t)movie->header->Width * 3; + y += 3) { + int i = buf[y]; + buf[y] = buf[y + 2]; + buf[y + 2] = i; + } + } + + MEM_freeN(buffer); + + return buf; + } } void *avi_converter_to_avi_rgb(AviMovie *movie, int stream, unsigned char *buffer, size_t *size) { - unsigned char *buf; + unsigned char *buf; - (void)stream; /* unused */ + (void)stream; /* unused */ - size_t rowstride = movie->header->Width * 3; - /* AVI files has uncompressed lines 4-byte aligned */ - rowstride = (rowstride + 3) & ~3; + size_t rowstride = movie->header->Width * 3; + /* AVI files has uncompressed lines 4-byte aligned */ + rowstride = (rowstride + 3) & ~3; - *size = movie->header->Height * rowstride; - buf = MEM_mallocN(*size, "toavirgbbuf"); + *size = movie->header->Height * rowstride; + buf = MEM_mallocN(*size, "toavirgbbuf"); - for (size_t y = 0; y < movie->header->Height; y++) { - memcpy(&buf[y * rowstride], &buffer[((movie->header->Height - 1) - y) * movie->header->Width * 3], movie->header->Width * 3); - } + for (size_t y = 0; y < movie->header->Height; y++) { + memcpy(&buf[y * rowstride], + &buffer[((movie->header->Height - 1) - y) * movie->header->Width * 3], + movie->header->Width * 3); + } - for (size_t y = 0; y < movie->header->Height; y++) { - for (size_t x = 0; x < movie->header->Width * 3; x += 3) { - int i = buf[y * rowstride + x]; - buf[y * rowstride + x] = buf[y * rowstride + x + 2]; - buf[y * rowstride + x + 2] = i; - } - } + for (size_t y = 0; y < movie->header->Height; y++) { + for (size_t x = 0; x < movie->header->Width * 3; x += 3) { + int i = buf[y * rowstride + x]; + buf[y * rowstride + x] = buf[y * rowstride + x + 2]; + buf[y * rowstride + x + 2] = i; + } + } - MEM_freeN(buffer); + MEM_freeN(buffer); - return buf; + return buf; } diff --git a/source/blender/avi/intern/avi_rgb32.c b/source/blender/avi/intern/avi_rgb32.c index 49270cd3ce0..fae255d65ba 100644 --- a/source/blender/avi/intern/avi_rgb32.c +++ b/source/blender/avi/intern/avi_rgb32.c @@ -35,56 +35,60 @@ void *avi_converter_from_rgb32(AviMovie *movie, int stream, unsigned char *buffer, size_t *size) { - unsigned char *buf; + unsigned char *buf; - (void)stream; /* unused */ + (void)stream; /* unused */ - *size = (size_t)movie->header->Height * (size_t)movie->header->Width * 3; - buf = imb_alloc_pixels(movie->header->Height, movie->header->Width, 3, sizeof(unsigned char), "fromrgb32buf"); - if (!buf) { - return NULL; - } + *size = (size_t)movie->header->Height * (size_t)movie->header->Width * 3; + buf = imb_alloc_pixels( + movie->header->Height, movie->header->Width, 3, sizeof(unsigned char), "fromrgb32buf"); + if (!buf) { + return NULL; + } - size_t rowstridea = movie->header->Width * 3; - size_t rowstrideb = movie->header->Width * 4; + size_t rowstridea = movie->header->Width * 3; + size_t rowstrideb = movie->header->Width * 4; - for (size_t y = 0; y < movie->header->Height; y++) { - for (size_t x = 0; x < movie->header->Width; x++) { - buf[y * rowstridea + x * 3 + 0] = buffer[y * rowstrideb + x * 4 + 3]; - buf[y * rowstridea + x * 3 + 1] = buffer[y * rowstrideb + x * 4 + 2]; - buf[y * rowstridea + x * 3 + 2] = buffer[y * rowstrideb + x * 4 + 1]; - } - } + for (size_t y = 0; y < movie->header->Height; y++) { + for (size_t x = 0; x < movie->header->Width; x++) { + buf[y * rowstridea + x * 3 + 0] = buffer[y * rowstrideb + x * 4 + 3]; + buf[y * rowstridea + x * 3 + 1] = buffer[y * rowstrideb + x * 4 + 2]; + buf[y * rowstridea + x * 3 + 2] = buffer[y * rowstrideb + x * 4 + 1]; + } + } - MEM_freeN(buffer); + MEM_freeN(buffer); - return buf; + return buf; } void *avi_converter_to_rgb32(AviMovie *movie, int stream, unsigned char *buffer, size_t *size) { - unsigned char *buf; - unsigned char *to, *from; + unsigned char *buf; + unsigned char *to, *from; - (void)stream; /* unused */ + (void)stream; /* unused */ - *size = (size_t)movie->header->Height * (size_t)movie->header->Width * 4; - buf = imb_alloc_pixels(movie->header->Height, movie->header->Width, 3, sizeof(unsigned char), "torgb32buf"); - if (!buf) { - return NULL; - } + *size = (size_t)movie->header->Height * (size_t)movie->header->Width * 4; + buf = imb_alloc_pixels( + movie->header->Height, movie->header->Width, 3, sizeof(unsigned char), "torgb32buf"); + if (!buf) { + return NULL; + } - memset(buf, 255, *size); + memset(buf, 255, *size); - to = buf; from = buffer; - size_t i = (size_t)movie->header->Height * (size_t)movie->header->Width; + to = buf; + from = buffer; + size_t i = (size_t)movie->header->Height * (size_t)movie->header->Width; - while (i--) { - memcpy(to, from, 3); - to += 4; from += 3; - } + while (i--) { + memcpy(to, from, 3); + to += 4; + from += 3; + } - MEM_freeN(buffer); + MEM_freeN(buffer); - return buf; + return buf; } |