/* $Id: quicktimehw.c 3878 2008-06-15 04:37:33Z matju $ GridFlow Copyright (c) 2001-2008 by Mathieu Bouchard This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. See file ../COPYING for further informations on licensing terms. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #define QUICKTIMEHW_INCLUDE_HERE #include "../gridflow.h.fcs" #include #include #include #include #include static std::map *> codecs; static std::map fourccs; \class FormatQuickTimeHW : Format { quicktime_t *anim; int track; P dim; char *codec; int colorspace; int channels; bool started; P force; int length; // in frames float64 framerate; P bit_packing; int jpeg_quality; // in theory we shouldn't need this, but... ~FormatQuickTimeHW() {if (anim) quicktime_close(anim);} \constructor (t_symbol *mode, string filename) { track=0; dim=0; codec=QUICKTIME_RAW; started=false; force=0; framerate=29.97; bit_packing=0; jpeg_quality=75; // libquicktime may be nice, but it won't take a filehandle, only filename filename = gf_find_file(filename); anim = quicktime_open((char *)filename.data(),mode==gensym("in"),mode==gensym("out")); if (!anim) RAISE("can't open file `%s': %s (or some other reason that libquicktime won't tell us)", filename.data(), strerror(errno)); if (mode==gensym("in")) { length = quicktime_video_length(anim,track); post("quicktime: codec=%s height=%d width=%d depth=%d framerate=%f", quicktime_video_compressor(anim,track), quicktime_video_height(anim,track), quicktime_video_width(anim,track), quicktime_video_depth(anim,track), quicktime_frame_rate(anim,track)); /* This doesn't really work: (is it just for encoding?) if (!quicktime_supported_video(anim,track)) RAISE("quicktime: unsupported codec: %s", quicktime_video_compressor(anim,track)); */ } _0_colorspace(0,0,string("rgb")); quicktime_set_cpus(anim,1); uint32 mask[3] = {0x0000ff,0x00ff00,0xff0000}; bit_packing = new BitPacking(is_le(),3,3,mask); } \decl 0 bang (); \decl 0 seek (long frame); \decl 0 rewind (); \decl 0 force_size (int32 height, int32 width); \decl 0 codec (string c); \decl 0 colorspace (string c); \decl 0 parameter (string name, int32 value); \decl 0 framerate (float64 f); \decl 0 size (int32 height, int32 width); \decl 0 get (); \grin 0 int }; \def 0 force_size (int32 height, int32 width) { force = new Dim(height, width); } \def 0 seek (long frame) { quicktime_set_video_position(anim,clip(frame,0L,quicktime_video_length(anim,track)-1),track); } \def 0 rewind () {_0_seek(0,0,0);} \def 0 bang () { long length = quicktime_video_length(anim,track); long nframe = quicktime_video_position(anim,track); if (nframe >= length) {outlet_bang(bself->te_outlet); return;} /* if it works, only do it once, to avoid silly stderr messages forgotten in LQT */ if (!quicktime_reads_cmodel(anim,colorspace,0) && !started) { RAISE("LQT says this video cannot be decoded into the chosen colorspace"); } int sx = quicktime_video_width(anim,track); int sy = quicktime_video_height(anim,track); int sz = quicktime_video_depth(anim,track); channels = sz/8; // hack. how do i get the video's native colormodel ? switch (sz) { case 24: colorspace=BC_RGB888; break; case 32: colorspace=BC_RGBA8888; break; default: post("strange quicktime. ask matju."); break; } if (force) { sy = force->get(0); sx = force->get(1); } uint8 *buf = new uint8[sy*sx*channels]; uint8 *rows[sy]; for (int i=0; i(name.data()), &val); if (name=="jpeg_quality") jpeg_quality=value; } \def 0 framerate (float64 f) { framerate=f; quicktime_set_framerate(anim, f); } \def 0 size (int32 height, int32 width) { if (dim) RAISE("video size already set!"); // first frame: have to do setup dim = new Dim(height, width, 3); quicktime_set_video(anim,1,dim->get(1),dim->get(0),framerate,codec); quicktime_set_cmodel(anim,colorspace); } GRID_INLET(FormatQuickTimeHW,0) { if (in->dim->n != 3) RAISE("expecting 3 dimensions: rows,columns,channels"); if (in->dim->get(2)!=channels) RAISE("expecting %d channels (got %d)",channels,in->dim->get(2)); in->set_chunk(0); if (dim) { if (!dim->equal(in->dim)) RAISE("all frames should be same size"); } else { // first frame: have to do setup dim = in->dim; quicktime_set_video(anim,1,dim->get(1),dim->get(0),framerate,codec); quicktime_set_cmodel(anim,colorspace); quicktime_set_depth(anim,8*channels,track); } //post("quicktime jpeg_quality %d", jpeg_quality); quicktime_set_parameter(anim, (char*)"jpeg_quality", &jpeg_quality); } GRID_FLOW { int sx = quicktime_video_width(anim,track); int sy = quicktime_video_height(anim,track); uint8 *rows[sy]; if (sizeof(T)>1) { uint8 data2[n]; bit_packing->pack(sx*sy,data,(uint8 *)data2); for (int i=0; ite_outlet,gensym("frames"),1,a); */ t_atom a[1]; SETFLOAT(a,length); outlet_anything(bself->outlets[0],gensym("frames"),1,a); //SUPER; } \classinfo {install_format("#io.quicktime",6,"mov avi"); // def self.info; %[codecs: #{@codecs.keys.join' '}] end //#define L fprintf(stderr,"%s:%d in %s\n",__FILE__,__LINE__,__PRETTY_FUNCTION__); #ifdef LQT_VERSION lqt_registry_init(); int n = lqt_get_num_video_codecs(); for (int i=0; iname) { fprintf(stderr,"[#in quicktime]: skipping codec with null name!\n"); continue; } string name = string(s->name); std::vector *f = new std::vector(s->num_fourccs); if (!s->fourccs) { post("WARNING: no fourccs (quicktime library is broken?)"); goto hell; } //fprintf(stderr,"num_fourccs=%d fourccs=%p\n",s->num_fourccs,s->fourccs); for (int j=0; jnum_fourccs; j++) { string fn = string(s->fourccs[j]); f->push_back(fn); fourccs[fn]=name; } codecs[name]=f; hell:; } #endif } \end class FormatQuickTimeHW void startup_quicktimehw () { \startall }