--- parser3/src/classes/image.C 2020/11/30 19:50:47 1.169 +++ parser3/src/classes/image.C 2020/12/01 18:53:01 1.172 @@ -26,7 +26,7 @@ #include "pa_table.h" #include "pa_charsets.h" -volatile const char * IDENT_IMAGE_C="$Id: image.C,v 1.169 2020/11/30 19:50:47 moko Exp $"; +volatile const char * IDENT_IMAGE_C="$Id: image.C,v 1.172 2020/12/01 18:53:01 moko Exp $"; // defines @@ -228,13 +228,14 @@ public: } } exif_gps_tag_value2name; + ///*********************************************** support functions class Measure_reader { public: virtual size_t read(const char* &buf, size_t limit)=0; - virtual void seek(long value, int whence)=0; - virtual long tell()=0; + virtual void seek(off_t value, int whence)=0; + virtual off_t tell()=0; }; class Measure_file_reader: public Measure_reader { @@ -257,12 +258,12 @@ public: return read_size; } - override void seek(long value, int whence) { + override void seek(off_t value, int whence) { if(lseek(f, value, whence)<0) throw Exception(IMAGE_FORMAT, &file_name, "seek(value=%ld, whence=%d) failed: %s (%d)", value, whence, strerror(errno), errno); } - override long tell() { return lseek(f, 0, SEEK_CUR); } + override off_t tell() { return lseek(f, 0, SEEK_CUR); } }; @@ -285,7 +286,7 @@ public: return to_read; } - override void seek(long value, int whence) { + override void seek(off_t value, int whence) { size_t new_offset; switch(whence) { case SEEK_CUR: new_offset=offset+value; break; @@ -302,7 +303,7 @@ public: offset=new_offset; } - override long tell() { return offset; } + override off_t tell() { return offset; } }; @@ -332,7 +333,9 @@ inline uint endian_to_uint(bool is_big, return is_big ? x_endian_to_uint(b[3], b[2], b[1], b[0]) : x_endian_to_uint(b[0], b[1], b[2], b[3]); } + ///*********************************************** JPEG + struct JPG_Segment_head { uchar marker; uchar code; @@ -487,7 +490,7 @@ static Value* parse_IFD_entry_value(bool if(value_size<=4) result=parse_IFD_entry_formatted_value(is_big, format, component_size, components_count, entry.value_or_offset_to_it); else { - long remembered=reader.tell(); + off_t remembered=reader.tell(); { reader.seek(tiff_base+endian_to_uint(is_big, entry.value_or_offset_to_it), SEEK_SET); const char* value; @@ -507,7 +510,7 @@ static void parse_IFD_entry(HashStringVa ushort tag=endian_to_ushort(is_big, entry.tag); if(tag==JPG_IFD_TAG_EXIF_OFFSET || tag==JPG_IFD_TAG_EXIF_GPS_OFFSET){ - long remembered=reader.tell(); + off_t remembered=reader.tell(); { reader.seek(tiff_base+endian_to_uint(is_big, entry.value_or_offset_to_it), SEEK_SET); parse_IFD(hash, is_big, reader, tiff_base, (tag==JPG_IFD_TAG_EXIF_GPS_OFFSET)?true:gps); @@ -864,6 +867,69 @@ static void measure_webp(const String& o } else throw Exception(IMAGE_FORMAT, &origin_string, "broken WEBP file - invalid chunk signature"); } + +///*********************************************** MP4 + +struct MP4_Header { + uchar size[4]; + char signature[4]; // 'ftyp' in first chunk +}; + +static bool measure_mp4(const String& origin_string, Measure_reader& reader, ushort& width, ushort& height, off_t anext, const char* lastTkhd=NULL) { + for(bool first=anext==0;;){ + const char* buf; + const size_t head_size=sizeof(MP4_Header); + if(reader.read(buf, head_size)size); + off_t next=reader.tell() + size - head_size; + +// printf("%d processing chunk size %d signature '%c%c%c%c %p'\n", anext, size, head->signature[0], head->signature[1], head->signature[2], head->signature[3], lastTkhd); + + if(first){ + if(strncmp(head->signature, "ftyp", 4)!=0) + throw Exception(IMAGE_FORMAT, &origin_string, "not MP4 file - wrong signature"); + first=false; + reader.seek(0, SEEK_END); + anext=reader.tell(); // to avoid reading beyond EOF + } else if(strncmp(head->signature, "moov", 4)==0 || strncmp(head->signature, "mdia", 4)==0 || strncmp(head->signature, "trak", 4)==0) { + if(measure_mp4(origin_string, reader, width, height, next, lastTkhd)) + return true; + } else if(strncmp(head->signature, "tkhd", 4)==0) { + if(size>8){ + reader.seek(next-8, SEEK_SET); + if(reader.read(lastTkhd, 8)<8) + throw Exception(IMAGE_FORMAT, &origin_string, "broken MP4 file - bad tkhd chunk"); + } + } else if (strncmp(head->signature, "hdlr", 4)==0) { + if(size>12){ + const char* hdlr; + if(reader.read(hdlr, 12)<12) + throw Exception(IMAGE_FORMAT, &origin_string, "broken MP4 file - bad hdlr chunk"); +// if(strncmp(hdlr+8, "vide", 4)==0) +// printf("vide found\n"); + if( lastTkhd && strncmp(hdlr+8, "vide", 4)==0) { + width=endian_to_ushort(true, (const unsigned char*)(lastTkhd)); + height=endian_to_ushort(true, (const unsigned char*)(lastTkhd+4)); +// printf("wh %d %d\n",width, height); + return true; + } + } + } + if(anext && next>=anext) + break; + reader.seek(next, SEEK_SET); + } + return false; +} + +static void measure_mp4(const String& origin_string, Measure_reader& reader, ushort& width, ushort& height) { + if(!measure_mp4(origin_string, reader, width, height, 0)) + throw Exception(IMAGE_FORMAT, &origin_string, "unsupported MP4 file - size not found"); +} + ///*********************************************** measure center static void measure(const String& file_name, Measure_reader& reader, Measure_info &info) { @@ -882,6 +948,8 @@ static void measure(const String& file_n measure_webp(file_name, reader, info.width, info.height); else if(strcasecmp(cext, "TIF")==0 || strcasecmp(cext, "TIFF")==0) measure_tiff(file_name, reader, info); + else if(strcasecmp(cext, "MP4")==0 || strcasecmp(cext, "MOV")==0) + measure_mp4(file_name, reader, info.width, info.height); else throw Exception(IMAGE_FORMAT, &file_name, "unhandled image file name extension '%s'", cext); } else