From f742dd679138b1e3428b72e8f934fee15ade9ecb Mon Sep 17 00:00:00 2001 From: deva Date: Wed, 13 Apr 2005 15:23:19 +0000 Subject: cleaned up... more needed! --- src/camera.cc | 5 -- src/decoder.cc | 212 +++++++++++++++++++-------------------------------------- src/decoder.h | 12 ++-- src/dvframe.h | 2 +- src/ffframe.cc | 1 + 5 files changed, 78 insertions(+), 154 deletions(-) (limited to 'src') diff --git a/src/camera.cc b/src/camera.cc index 772d48a..a73c063 100644 --- a/src/camera.cc +++ b/src/camera.cc @@ -44,9 +44,6 @@ void Camera::connect(const char *ip, const int port) */ running = 1; - int channel = 0; - char *device = "/dev/dv1394"; - av_register_all(); encode_queue = new Queue(); // infinite size @@ -56,8 +53,6 @@ void Camera::connect(const char *ip, const int port) sem_init(&player_sem, 0, 0); decoder = new Decoder(errorstatus, - device, - channel, &encode_sem, &player_sem, encode_queue, diff --git a/src/decoder.cc b/src/decoder.cc index fcbf136..10ef245 100644 --- a/src/decoder.cc +++ b/src/decoder.cc @@ -30,11 +30,7 @@ #include "decoder.h" #include "debug.h" -static AVCodecContext mycodec; - Decoder::Decoder(Error* err, - char *device, - int channel, sem_t *gencode_sem, sem_t *gplayer_sem, Queue *gencode_queue, @@ -43,7 +39,6 @@ Decoder::Decoder(Error* err, volatile int *grunning) { errobj = err; -device = ""; encode_sem = gencode_sem; player_sem = gplayer_sem; encode_queue = gencode_queue; @@ -51,78 +46,38 @@ device = ""; mutex = gmutex; running = grunning; - AVFormatContext *ifc; - AVFormatParameters dvpars; - AVInputFormat *iformat; AVCodec *dec_codec; - -/* - memset(&dvpars, 0, sizeof(dvpars)); - - if(!(iformat = av_find_input_format("dv1394"))) { - errobj->pushError("Failed to get input format dv1394."); - fc = NULL; - return; - } - - dvpars.device = device; // "/dev/dv1394"; - dvpars.channel = channel; - dvpars.standard = "pal"; - memset(&dvpars, 0, sizeof(dvpars)); - dvpars.device = ""; - int err1; - if((err1 = av_open_input_file(&ifc, "file:///root/dvgrab-001.dv", iformat, 0, &dvpars)) < 0) { -fprintf(stderr, "Error in open: %d\n", err1); - errobj->pushError("Device is in use."); - fc = NULL; -// return; - } - if(av_find_stream_info(ifc) < 0) { - errobj->pushError("Could not find enough parameters."); - fc = NULL; -// return; - } - dump_format(ifc, 1, "", 0); -*/ - if(!(dec_codec = avcodec_find_decoder(CODEC_ID_DVVIDEO/*ifc->streams[0]->codec.codec_id*/))) { - errobj->pushError("Unsupported codec for input stream."); - fc = NULL; + // Find ffmpeg-dv-codec + if(!(dec_codec = avcodec_find_decoder(CODEC_ID_DVVIDEO))) { + errobj->pushError("Unable to find codec."); return; } - if(avcodec_open(&mycodec/*&ifc->streams[0]->codec*/, dec_codec) < 0) { + // Initialize ffmpeg-dv-codec + if(avcodec_open(&dvcodec, dec_codec) < 0) { errobj->pushError("Error while opening codec for input stream."); - fc = NULL; return; } - - fc = ifc; } Decoder::~Decoder() { - avcodec_close(&fc->streams[0]->codec); - av_close_input_file(fc); + avcodec_close(&dvcodec); } static int raw_reader( raw1394handle_t handle, int channel, size_t length, quadlet_t *data ) { - /* skip empty packets */ static char *framedata = NULL; -//printf("raw_reader\n"); fflush(stdout); + + // Only process packets with reasonable length. if ( length > 16 ) { unsigned char * p = ( unsigned char* ) & data[ 3 ]; - int section_type = p[ 0 ] >> 5; /* section type is in bits 5 - 7 */ - int dif_sequence = p[ 1 ] >> 4; /* dif sequence number is in bits 4 - 7 */ + int section_type = p[ 0 ] >> 5; // section type is in bits 5 - 7 + int dif_sequence = p[ 1 ] >> 4; // dif sequence number is in bits 4 - 7 int dif_block = p[ 2 ]; - /* if we are at the beginning of a frame, we put the previous - frame in our output_queue. Then we try to get an unused - frame_buffer from the buffer_queue for the current frame. - We must lock the queues because they are shared between - this thread and the main thread. */ if ( section_type == 0 && dif_sequence == 0 ) { if ( framedata != NULL ) @@ -137,35 +92,35 @@ static int raw_reader( raw1394handle_t handle, int channel, size_t length, quadl framedata = (char *)malloc(DVPACKAGE_SIZE); // dvframe.h if(!framedata) { - /* We're fucked */ + // We're fucked exit(1); } } switch ( section_type ) { - case 0: /* 1 Header block */ - /* p[3] |= 0x80; // hack to force PAL data */ + case 0: // 1 Header block + // p[3] |= 0x80; // hack to force PAL data memcpy( framedata + dif_sequence * 150 * 80, p, 480 ); break; - case 1: /* 2 Subcode blocks */ + case 1: // 2 Subcode blocks memcpy( framedata + dif_sequence * 150 * 80 + ( 1 + dif_block ) * 80, p, 480 ); break; - case 2: /* 3 VAUX blocks */ + case 2: // 3 VAUX blocks memcpy( framedata + dif_sequence * 150 * 80 + ( 3 + dif_block ) * 80, p, 480 ); break; - case 3: /* 9 Audio blocks interleaved with video */ + case 3: // 9 Audio blocks interleaved with video memcpy( framedata + dif_sequence * 150 * 80 + ( 6 + dif_block * 16 ) * 80, p, 480 ); break; - case 4: /* 135 Video blocks interleaved with audio */ + case 4: // 135 Video blocks interleaved with audio memcpy( framedata + dif_sequence * 150 * 80 + ( 7 + ( dif_block / 15 ) + dif_block ) * 80, p, 480 ); break; - default: /* we can´t handle any other data */ + default: // we can´t handle any other data break; } } @@ -175,62 +130,54 @@ static int raw_reader( raw1394handle_t handle, int channel, size_t length, quadl void Decoder::decode() { + // FIXME: Read port and channel data from config. int port = 0; int channel = 63; + int n_ports; struct raw1394_portinfo pinf[ 16 ]; raw1394handle_t handle; + + // Get handle to firewire channels handle = raw1394_new_handle(); - if ( !handle ) - { + if(!handle) { fprintf( stderr, "raw1394 - failed to get handle: %s.\n", strerror( errno ) ); exit( EXIT_FAILURE ); } - if ( ( n_ports = raw1394_get_port_info( handle, pinf, 16 ) ) < 0 ) - { + // how many adapters are hook in? + if((n_ports = raw1394_get_port_info(handle, pinf, 16)) < 0 ) { fprintf( stderr, "raw1394 - failed to get port info: %s.\n", strerror( errno ) ); raw1394_destroy_handle( handle ); exit( EXIT_FAILURE ); } -printf("n_ports: %d\n", n_ports); - /* tell raw1394 which host adapter to use */ - if ( raw1394_set_port( handle, port ) < 0 ) - { + + // Tell raw1394 which host adapter to use + if(raw1394_set_port(handle, port) < 0 ) { + errobj->pushError("Error while opening codec for input stream."); fprintf( stderr, "raw1394 - failed to set set port: %s.\n", strerror( errno ) ); exit( EXIT_FAILURE ); } raw1394_set_iso_handler( handle, channel, raw_reader); // 63 is default channel... sucks. raw1394_set_userdata( handle, ( void* ) NULL); - printf("fisk: %d\n", raw1394_start_iso_rcv( handle, channel)); fflush(stdout); + raw1394_start_iso_rcv( handle, channel); -/* - if(fc == NULL) { - errobj->pushError("Decoder not initialized."); - return; - } -*/ while(*running) { AVPacket pkt; uint8_t *ptr; int len; SDL_Event user_event; -// av_read_packet(fc, &pkt); - - while(1) - { + while(1) { raw1394_loop_iterate(handle); -//printf("-"); fflush(stdout); pkt.data = (uint8_t *)raw1394_get_userdata(handle); - if(pkt.data) - { + if(pkt.data) { raw1394_set_userdata(handle, NULL); break; } } -printf("."); fflush(stdout); +printf("1"); fflush(stdout); len = pkt.size = DVPACKAGE_SIZE; ptr = pkt.data; pkt.stream_index = 0; @@ -238,63 +185,46 @@ printf("."); fflush(stdout); // NOTE: we only decode video, we only need the data from stream_index 0 // (stream 0: video, stream 1: audio) // while(pkt.stream_index == 0 && len > 0) { - while(pkt.stream_index == 0 && len > 0) { - int ret; - int got_picture; - // buf_t *buf = buf_alloc(); - FFFrame *fff = new FFFrame(); ALLOC(fff, "FFFrame in decode"); -// DVFrame *dvf = new DVFrame(); ALLOC(dvf, "DVFrame in decode"); -// dvf->type = DVF_VIDEO; - -// memcpy(dvf->frame, ptr, len); - - //printf("DVBufferSize: [%d]bytes\n", len); - - ret = avcodec_decode_video(&mycodec/*&fc->streams[0]->codec*/, - fff->frame, &got_picture, ptr, len); - - if(ret < 0) { - errobj->pushError("Error while decoding stream."); - return; - } - - len -= ret; - ptr += ret; - - pthread_mutex_lock(mutex); -// encode_queue->push(dvf); - player_queue->push(fff); - pthread_mutex_unlock(mutex); - - sem_post(encode_sem); - - user_event.type = SDL_USEREVENT; - user_event.user.code = 0; - user_event.user.data1 = NULL; - user_event.user.data2 = NULL; -// SDL_PushEvent(&user_event); - } -free(ptr); - /* - // For later use, when audio must be implemented - while(pkt.stream_index == 1 && len > 0) { - DVFrame *dvf = new DVFrame(); - dvf->type = DVF_AUDIO; - - memcpy(dvf->frame, ptr, sizeof(dvf->frame)); - - ptr += sizeof(dvf->frame); - len -= sizeof(dvf->frame); - - pthread_mutex_lock(mutex); - encode_queue->push(dvf); - pthread_mutex_unlock(mutex); - - sem_post(encode_sem); + int ret; + int got_picture; + // buf_t *buf = buf_alloc(); + FFFrame *fff = new FFFrame(); ALLOC(fff, "FFFrame in decode"); + memset(fff->frame, 0 , sizeof(AVFrame)); + //DVFrame *dvf = new DVFrame(); ALLOC(dvf, "DVFrame in decode"); + // dvf->type = DVF_VIDEO; + + // memcpy(dvf->frame, ptr, len); + +printf("2"); fflush(stdout); + ret = avcodec_decode_video(&dvcodec, fff->frame, &got_picture, ptr, len); +printf("3"); fflush(stdout); + + if(ret < 0) { + errobj->pushError("Error while decoding stream."); + return; } - */ -// av_free_packet(&pkt); FREE(&pkt); + +printf("4"); fflush(stdout); + pthread_mutex_lock(mutex); + // encode_queue->push(dvf); + player_queue->push(fff); + pthread_mutex_unlock(mutex); +printf("5"); fflush(stdout); + + sem_post(encode_sem); + + user_event.type = SDL_USEREVENT; + user_event.user.code = 0; + user_event.user.data1 = NULL; + user_event.user.data2 = NULL; +printf("6"); fflush(stdout); + SDL_PushEvent(&user_event); +printf("7"); fflush(stdout); + + free(ptr); +printf("8"); fflush(stdout); } +printf("9"); fflush(stdout); /* Kick the others so they wake up with empty queues */ sem_post(encode_sem); diff --git a/src/decoder.h b/src/decoder.h index fcf61cf..6d50e5b 100644 --- a/src/decoder.h +++ b/src/decoder.h @@ -44,8 +44,6 @@ class Decoder : public Thread { public: Decoder(Error* err, - char *device, - int channel, sem_t *gencode_sem, sem_t *gplayer_sem, Queue *gencode_queue, @@ -55,7 +53,10 @@ public: ~Decoder(); void run(); - AVFormatContext *fc; +private: + Error *errobj; + AVCodecContext dvcodec; + sem_t *encode_sem; sem_t *player_sem; Queue *encode_queue; @@ -63,12 +64,9 @@ public: pthread_mutex_t *mutex; volatile int *running; - private: - Error *errobj; - void decode(); }; -#endif +#endif/* __RTVIDEOREC_DECODER_H*/ #endif/*USE_GUI*/ diff --git a/src/dvframe.h b/src/dvframe.h index a9e948c..3bf61fe 100644 --- a/src/dvframe.h +++ b/src/dvframe.h @@ -26,7 +26,7 @@ #ifndef __DVFRAME_H__ #define __DVFRAME_H__ -//#include +#include #define DVPACKAGE_SIZE 144000 diff --git a/src/ffframe.cc b/src/ffframe.cc index b6d8e14..d2bd5f9 100644 --- a/src/ffframe.cc +++ b/src/ffframe.cc @@ -31,6 +31,7 @@ FFFrame::FFFrame() { frame = avcodec_alloc_frame(); ALLOC(frame, "FFFrame (inside obj)"); + fprintf(stderr, "Frame: %d, \n", frame); fflush(stderr); } FFFrame::~FFFrame() -- cgit v1.2.3