From b04122f3f7884de08eb5d59bb3fd2bff829f9039 Mon Sep 17 00:00:00 2001 From: deva Date: Sun, 22 May 2005 15:49:22 +0000 Subject: Added multithreaded encoding support. --- src/mov_encoder.cc | 108 ++++++++++++++++++++++++++--------------------------- 1 file changed, 52 insertions(+), 56 deletions(-) (limited to 'src/mov_encoder.cc') diff --git a/src/mov_encoder.cc b/src/mov_encoder.cc index 7bffc50..0a478fc 100644 --- a/src/mov_encoder.cc +++ b/src/mov_encoder.cc @@ -39,28 +39,26 @@ /* * $Log$ - * Revision 1.23 2005/05/19 14:10:22 deva + * Revision 1.24 2005/05/22 15:49:22 deva + * Added multithreaded encoding support. * + * Revision 1.23 2005/05/19 14:10:22 deva * Multithreading rulez? * * Revision 1.22 2005/05/17 19:16:26 deva - * * Made new mpeg writer work, with proper file permissions. * * Revision 1.21 2005/05/17 14:30:56 deva * Added code, preparing threaded encoding. * * Revision 1.20 2005/05/16 16:00:57 deva - * * Lots of stuff! * * Revision 1.19 2005/05/16 13:25:52 deva - * * Moved video setting to configuration file. * Fine tuned setting for 2.4ghz server * * Revision 1.18 2005/05/16 11:13:24 deva - * * Optimized some encoding parameters. * * Revision 1.17 2005/05/16 10:45:10 deva @@ -102,10 +100,10 @@ #include "miav_config.h" #include "debug.h" -//av_alloc_format_context -//av_destruct_packet_nofree -MovEncoder::MovEncoder() +MovEncoder::MovEncoder(sem_t *r_sem, + FrameVectorQueue *in, sem_t *in_sem, pthread_mutex_t *in_mutex, + FramePriorityQueue *out, sem_t *out_sem, pthread_mutex_t *out_mutex) { // FIXME: Hmmm... should this be detected somewhere?! int w = 720; @@ -212,12 +210,18 @@ MovEncoder::MovEncoder() fame_init(fame_context, &fame_par, fame_buffer, FAME_BUFFER_SIZE); + running = true; - // Thread stuff - sem_init(&sem, 0, 0); - sem_init(&done, 0, 0); + inputqueue = in; + outputqueue = out; - running = true; + input_sem = in_sem; + output_sem = out_sem; + + read_sem = r_sem; + + input_mutex = in_mutex; + output_mutex = out_mutex; } MovEncoder::~MovEncoder() @@ -265,7 +269,7 @@ Frame *MovEncoder::encode_video(Frame *dvframe) dvdecoder->num_dif_seqs = 12; } - pixels[ 0 ] = rgb; // We use this as the output buffer + pixels[ 0 ] = picture; // We use this as the output buffer pitches[ 0 ] = w * 2; dv_decode_full_frame(dvdecoder, @@ -279,7 +283,7 @@ Frame *MovEncoder::encode_video(Frame *dvframe) uint8_t *y = yuv.y; uint8_t *cb = yuv.u; uint8_t *cr = yuv.v; - uint8_t *p = rgb; + uint8_t *p = picture; for ( int i = 0; i < h; i += 2 ) { // process two scanlines (one from each field, interleaved) @@ -326,59 +330,51 @@ void MovEncoder::encode_audio(Frame *dvframe) // TODO: Do some audio stuff here sometime! } -void MovEncoder::encodeSequence(Queue *queue) -{ - // set input queue - inputqueue = queue; - - // unlock semaphore - sem_post(&sem); -} - - // this runs in a thread void MovEncoder::run() { - fprintf(stderr, "Encoder Ready\n"); fflush(stderr); + FrameVector *item; + Frame *in_frame; + Frame *out_frame; + + fprintf(stderr, "\t\t\t\tEncoder Ready\n"); fflush(stderr); while(running) { - // wait for semaphore - // lock semaphore - sem_wait(&sem); - if(inputqueue == NULL) continue; + sem_wait(input_sem); - fprintf(stderr, "."); fflush(stderr); + fprintf(stderr, "\t\t\t\tReading block\n"); fflush(stderr); - // allocate new output queue - outputqueue = new Queue(); - // while input queue.pop - Frame *fi, *fo; - while(inputqueue->length() > 0) { - fi = inputqueue->pop(); + // Lock inout mutex + pthread_mutex_lock(input_mutex); + item = inputqueue->front(); + inputqueue->pop(); + pthread_mutex_unlock(input_mutex); + // Unlock input mutex - // encode frame from input queue - fo = encode(fi); + if(!item) { + fprintf(stderr, "\t\t\t\tEmpty block detected.\n"); fflush(stderr); + continue; + } - // and push result on output queue - outputqueue->push(fo); + for(int cnt = 0; cnt < item->size(); cnt++) { + in_frame = item->at(cnt); + out_frame = encode(in_frame); + out_frame->number = in_frame->number; - // delete the input frame - //delete fi; - } - // delete input queue - delete inputqueue; - sem_post(&done); - } -} + delete in_frame; -Queue *MovEncoder::getResultSequence() -{ - // wait for sempahore - sem_wait(&done); + // Lock output mutex + pthread_mutex_lock(output_mutex); + outputqueue->push(out_frame); + pthread_mutex_unlock(output_mutex); + // Unlock output mutex + + fprintf(stderr, "\t\t\t\tEncoded [%d] - pushed it for writing\n", in_frame->number); fflush(stderr); + } - // fprintf(stderr, "POP!\n"); fflush(stderr); + delete item; - // return output queue - return outputqueue; + sem_post(read_sem); + sem_post(output_sem); + } } - -- cgit v1.2.3