/* Copyright (C) 2003 Commonwealth Scientific and Industrial Research Organisation (CSIRO) Australia Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: - Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. - Neither the name of CSIRO Australia nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE ORGANISATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include #include #include #include #include #include #include #include #define anx_malloc malloc #define anx_free free #define PALBPP 144000 /*PAL bytes per packet*/ #define NTSCBPP 120000 /*ntsc bytes per packet /*#define DEBUG*/ #define CHECKPAL /* define if checking for PAL or NTFSC*/ #define MIN(a,b) ((a)<(b)?(a):(b)) #include #include typedef struct _AnxCBRData AnxCBRData; struct _AnxCBRData { FILE * cbrfile; long total_remaining; long bytesperpacket; /*frame size*/ }; static AnxImporter anxcbr_importer; static unsigned int le_int(unsigned int i) { unsigned int ret=i; #ifdef WORDS_BIGENDIAN ret = i>>24; ret += (i>>8)&0x0000ff00; ret += (i<<8)&0x00ff0000; ret += (i<<24); #endif return ret; } static unsigned short le_short(unsigned short s) { unsigned short ret=s; #ifdef WORDS_BIGENDIAN ret = s>>8; ret += s<<8; #endif return ret; } static AnxSource * anxcbr_open (const char * path, const char * id, int ignore_media, double start_time, double end_time, AnxImportCallbacks * unused) { AnxSource * m; AnxSourceTrack * track; AnxCBRData * cbrd; char data[80]; BOOL sof; int num_sofs = 0; int blk_count = 0; int blk_diff=0; int sof_blknum[2]; enum {UNKNOWN=0, PAL, NTSC}; int signal_type = UNKNOWN; if (ignore_media) return NULL; m = (AnxSource *) anx_malloc (sizeof (AnxSource)); if(m==NULL) printf("not enough memory"); track = (AnxSourceTrack *) anx_malloc (sizeof (AnxSourceTrack)); if(track==NULL) printf("not enough memory"); m->importer = &anxcbr_importer; m->current_track = track; m->tracks = anx_list_append (NULL, track); m->eos = 0; m->start_time = start_time; m->end_time = end_time; m->current_time = start_time; cbrd = anx_malloc (sizeof (AnxCBRData)); if(cbrd==NULL) printf("not enough memory"); cbrd->cbrfile=NULL; m->custom_data = cbrd; cbrd->cbrfile = fopen(path,"rb"); if(cbrd->cbrfile == NULL) { printf("/ncbrd file is null - %s/n",strerror(errno)); return NULL; } #ifdef CHECKPAL while(signal_type == UNKNOWN) { //count blocks between 2 successive start-of-frames while( num_sofs <2) { //read(file, data, sizeof(data)); fread(data,sizeof(data),1,cbrd->cbrfile); //printf("\ndata is - %d\n", data); sof = ((data[0] == 0x1f) && (data[1]==0x07)); if(sof) { sof_blknum[num_sofs] = blk_count; num_sofs++; } blk_count++; } blk_diff = sof_blknum[1]- sof_blknum[0]; switch(blk_diff) { case (12*150): signal_type = PAL; cbrd->bytesperpacket = PALBPP; break; case (10*150): signal_type = NTSC; cbrd->bytesperpacket=NTSCBPP; break; default: num_sofs = 0; // try again break; } } fseek(cbrd->cbrfile,0,0); //rewind the file can also use rewind(); #endif #ifndef CHECKPAL cbrd->bytesperpacket = PALBPP; //PAL is 144000 bpp #endif track->id = (char *)id; track->content_type = "video/x-dv"; track->granule_rate_n = 1000000; track->granule_rate_d = 40; track->current_granule = 0; track->eos = 0; track->end_granule = 0; if (start_time != 0.0) { // forward to time offset in input DV file start_timex40 miliseconds track->start_granule = start_time * 0.0004; track->current_granule = fseek(cbrd->cbrfile,track->start_granule,SEEK_SET); //track->current_granule = lseek(cbrd->cbrfile,0,SEEK_SET); //sf_seek(cbrd->cbrfile, track->start_granule, SEEK_SET); // detect end of file and set eos accordingly if (track->current_granule >= track->end_granule) track->eos = 1; else track->eos = 0; } else { // no fast forward necessary track->start_granule = 0; //track->current_granule = _lseek(cbrd->cbrfile,0.0,SEEK_SET); } #ifdef DEBUG fprintf (stderr, "anxcbr_open: %lld - %lld\n", track->start_granule, track->end_granule); #endif // number of head packets in DV = 0 track->nr_header_packets = 0; return m; } static long anxcbr_read (AnxSource * media, char * buf, long n, long bound) { AnxCBRData * cbrd = (AnxCBRData *)media->custom_data; AnxSourceTrack * track = media->current_track; long header_n = 0; if(cbrd->cbrfile==NULL) printf("File is null in read"); fread(buf,n,1,cbrd->cbrfile); /*read the file*/ if(feof(cbrd->cbrfile)) /*check for end of file - use feof() cause its binary mode*/ { n=-1; } return n; } static long anxcbr_sizeof_next_read (AnxSource * media, long bound) { AnxCBRData * cbrd = (AnxCBRData *)media->custom_data; return cbrd->bytesperpacket; } static int anxcbr_close (AnxSource * media) { AnxCBRData * cbrd = (AnxCBRData *)media->custom_data; fclose(cbrd->cbrfile); anx_free (cbrd); // not sure if you need this anx_free (media); return 0; } static struct _AnxImporter anxcbr_importer_dv = { (AnxImporterOpenFunc)anxcbr_open, (AnxImporterOpenFDFunc)NULL, (AnxImporterCloseFunc)anxcbr_close, (AnxImporterReadFunc)anxcbr_read, (AnxImporterSizeofNextReadFunc)anxcbr_sizeof_next_read, "video/x-dv" }; AnxImporter * anx_importer_init (int i) { switch (i) { case 0: return &anxcbr_importer_dv; break; default: return NULL; break; } }