From 6a708e3b1d4acb456f24bcd1e3d09a83d1070243 Mon Sep 17 00:00:00 2001 From: "N.N." Date: Tue, 18 May 2004 00:50:40 +0000 Subject: PiDiP 0.12.14 svn path=/trunk/externals/pidip/; revision=1734 --- modules/Makefile | 2 +- modules/Makefile.in | 2 +- modules/pdp_ascii.c | 153 ++++++++++++++++++++-- modules/pdp_canvas.c | 8 +- modules/pdp_ffmpeg~.c | 2 +- modules/pdp_i.c | 2 +- modules/pdp_lumafilt.c | 2 +- modules/pdp_mp4audiosource.cpp | 45 +------ modules/pdp_mp4audiosync.cpp | 289 ++++++++++------------------------------- modules/pdp_mp4live~.cpp | 32 ++--- modules/pdp_mp4player~.cpp | 24 +++- modules/pdp_mp4videosync.cpp | 40 +++++- modules/pdp_ocanvas.c | 2 +- modules/pdp_vertigo.c | 2 +- 14 files changed, 298 insertions(+), 307 deletions(-) (limited to 'modules') diff --git a/modules/Makefile b/modules/Makefile index 5d93176..1bca11a 100644 --- a/modules/Makefile +++ b/modules/Makefile @@ -15,7 +15,7 @@ OBJECTS = pdp_intrusion.o pdp_yqt.o pdp_simura.o pdp_underwatch.o \ pdp_capture.o pdp_smuck.o pdp_lumafilt.o \ pdp_transition.o pdp_imgloader.o pdp_imgsaver.o pdp_cache.o \ pdp_canvas.o pdp_pen.o pdp_shape.o pdp_fqt.o pdp_fcqt.o \ - pdp_ocanvas.o pdp_spotlight.o pdp_live~.o pdp_ffmpeg~.o + pdp_ocanvas.o pdp_spotlight.o pdp_colorgrid.o pdp_live~.o pdp_ffmpeg~.o # pdp_xcanvas.o pdp_aa.o all_modules: $(OBJECTS) diff --git a/modules/Makefile.in b/modules/Makefile.in index 0b221e6..7d9daad 100644 --- a/modules/Makefile.in +++ b/modules/Makefile.in @@ -15,7 +15,7 @@ OBJECTS = pdp_intrusion.o pdp_yqt.o pdp_simura.o pdp_underwatch.o \ pdp_capture.o pdp_smuck.o pdp_lumafilt.o \ pdp_transition.o pdp_imgloader.o pdp_imgsaver.o pdp_cache.o \ pdp_canvas.o pdp_pen.o pdp_shape.o pdp_fqt.o pdp_fcqt.o \ - pdp_ocanvas.o pdp_spotlight.o @PDP_STREAMING_OBJECTS@ + pdp_ocanvas.o pdp_spotlight.o pdp_colorgrid.o @PDP_STREAMING_OBJECTS@ # pdp_xcanvas.o pdp_aa.o all_modules: $(OBJECTS) diff --git a/modules/pdp_ascii.c b/modules/pdp_ascii.c index cf7f77a..a43956c 100644 --- a/modules/pdp_ascii.c +++ b/modules/pdp_ascii.c @@ -25,9 +25,11 @@ #include "pdp.h" #include "yuv.h" -#include "charmaps.h" +#include "default.map" #include +#define LINE_MAX_LENGTH 1024 + static char *pdp_ascii_version = "pdp_ascii: version 0.1, ASCII art output written by ydegoyon@free.fr"; typedef struct pdp_ascii_struct @@ -49,6 +51,13 @@ typedef struct pdp_ascii_struct t_int x_brightness; // added value for brightness t_float x_ratio; // character to pixel ratio + t_int x_charwidth; // width of characters + t_int x_charheight; // height of characters + t_int x_nbchars; // number of characters in the map + char* x_charmaps; // the table of characters + + FILE *x_filed; // charmaps file descriptor + } t_pdp_ascii; static void pdp_ascii_color(t_pdp_ascii *x, t_floatarg fcolor) @@ -61,9 +70,12 @@ static void pdp_ascii_color(t_pdp_ascii *x, t_floatarg fcolor) static void pdp_ascii_ratio(t_pdp_ascii *x, t_floatarg fratio) { - if ( ( fratio > 0) && ( x->x_ratio < x->x_vwidth/2 ) ) + if ( ( fratio > 0) + && ( fratio < x->x_vwidth/x->x_charwidth ) + && ( fratio < x->x_vheight/x->x_charheight ) ) { x->x_ratio = fratio; + // post( "psp_ascii : set ratio : %f", x->x_ratio ); } } @@ -84,7 +96,7 @@ static void pdp_ascii_process_yv12(t_pdp_ascii *x) t_int i, pixsum; t_int px, py, ppx, ppy; t_int rank, value; - t_int pwidth, pheight; + t_int pwidth, pheight, offset; x->x_vwidth = header->info.image.width; x->x_vheight = header->info.image.height; @@ -96,14 +108,16 @@ static void pdp_ascii_process_yv12(t_pdp_ascii *x) memset( newdata, 0x00, (x->x_vsize+(x->x_vsize>>1))<<1 ); - pwidth = (int) CHARWIDTH*x->x_ratio; + pwidth = (int) x->x_charwidth*x->x_ratio; if (pwidth==0) pwidth=1; - if (pwidth>x->x_vwidth) return; - pheight = (int) CHARHEIGHT*x->x_ratio; + // if (pwidth>x->x_vwidth) return; + pheight = (int) x->x_charheight*x->x_ratio; if (pheight==0) pheight=1; - if (pheight>x->x_vheight) return; + // if (pheight>x->x_vheight) return; - for(py=1; pyx_vheight; py+=pheight) + // post( "psp_ascii : pwidth=%d, pheight=%d", pwidth, pheight ); + + for(py=0; pyx_vheight; py+=pheight) { for(px=0; pxx_vwidth; px+=pwidth) { @@ -112,6 +126,11 @@ static void pdp_ascii_process_yv12(t_pdp_ascii *x) { for ( ppx=0; ppx= x->x_vwidth ) || + ( py+ppy >= x->x_vheight ) ) + { + break; + } pixsum += (data[(py+ppy)*x->x_vwidth + (px+ppx)]>>7); } } @@ -120,12 +139,14 @@ static void pdp_ascii_process_yv12(t_pdp_ascii *x) { for ( ppx=0; ppx x->x_vwidth ) || - ( py+ppy > x->x_vheight ) ) + if ( ( px+ppx >= x->x_vwidth ) || + ( py+ppy >= x->x_vheight ) ) { break; } - if ( charmaps[rank][((int)(ppy/x->x_ratio))*CHARWIDTH+((int)(ppx/x->x_ratio))] ) + offset = rank*x->x_charwidth*x->x_charheight + +((int)(ppy/x->x_ratio))*x->x_charwidth+((int)(ppx/x->x_ratio)); + if ( *(x->x_charmaps+offset) && ( offset < x->x_nbchars*x->x_charwidth*x->x_charheight ) ) { value = ( (2*rank+x->x_brightness) > 255 ) ? 255 : (2*rank+x->x_brightness); newdata[(py+ppy)*x->x_vwidth+(px+ppx)] = (value)<<7; @@ -209,10 +230,113 @@ static void pdp_ascii_free(t_pdp_ascii *x) { int i; + if ( x->x_charmaps ) free( x->x_charmaps ); pdp_queue_finish(x->x_queue_id); pdp_packet_mark_unused(x->x_packet0); } + /* load a new charmaps file */ +static void pdp_ascii_load(t_pdp_ascii *x, t_symbol *sfile) +{ + char *lineread = (char*) getbytes( LINE_MAX_LENGTH ); + char *word1 = (char*) getbytes( LINE_MAX_LENGTH ); + char *word2 = (char*) getbytes( LINE_MAX_LENGTH ); + char *word3 = (char*) getbytes( LINE_MAX_LENGTH ); + t_int charwidth, charheight, nbchars, nblines; + t_int nbexpdata; + char *pdata; + char charread; + + // opening new charmaps file + if ( ( x->x_filed = fopen( sfile->s_name, "r" ) ) == NULL ) + { + error( "pdp_ascii : cannot open >%s<", sfile->s_name); + return; + } + post( "pdp_ascii : opened >%s<", sfile->s_name); + + // read the new dimensions of the charmaps + charwidth = -1; + charheight = -1; + nbchars = -1; + nblines = 0; + while ( lineread[0] != '{' ) + { + if ( fgets( lineread, LINE_MAX_LENGTH, x->x_filed ) == 0 ) + { + post( "pdp_ascii : abnormal end of file encountered..." ); + goto closeandreturn; + } + + if ( strncmp( lineread, "#define", 7 ) == 0 ) + { + sscanf( lineread, "%s %s %s", word1, word2, word3 ); + // post( "pdp_ascii : definition : %s = %s", word2, word3 ); + if ( !strcmp( word2, "CHARWIDTH" ) ) + { + charwidth = atoi( word3 ); + } + if ( !strcmp( word2, "CHARHEIGHT" ) ) + { + charheight = atoi( word3 ); + } + if ( !strcmp( word2, "NBCHARS" ) ) + { + nbchars = atoi( word3 ); + } + } + + nblines++; + if ( nblines>20 ) break; + } + + if ( ( charwidth > 0 ) && ( charheight > 0 ) && ( nbchars > 0 ) ) + { + post( "pdp_ascii : new dimensions : %d %dx%d characters", nbchars, charwidth, charheight ); + if ( x->x_charmaps ) free( x->x_charmaps ); + x->x_charwidth = charwidth; + x->x_charheight = charheight; + x->x_nbchars = nbchars; + x->x_charmaps = (char*)malloc(x->x_nbchars*x->x_charwidth*x->x_charheight*sizeof(char)); + } + else + { + post( "pdp_ascii : wrong file format : couldn't read new dimensions" ); + goto closeandreturn; + } + + nbexpdata = nbchars*charwidth*charheight; + pdata = x->x_charmaps; + + while ( ( ( charread = fgetc( x->x_filed ) ) != EOF ) && ( nbexpdata > 0 ) ) + { + switch ( charread ) + { + case '0': + case '1': + *pdata++ = ( charread == '0' )?0:1; + nbexpdata--; + break; + + default: + break; + } + } + +closeandreturn : + + // closing opened file + if ( x->x_filed != NULL ) + { + if(fclose(x->x_filed) < 0) + { + perror( "pdp_ascii : closing file" ); + } + x->x_filed = NULL; + return; + } +} + t_class *pdp_ascii_class; void *pdp_ascii_new(void) @@ -230,6 +354,12 @@ void *pdp_ascii_new(void) x->x_ratio = 1.; x->x_brightness = 25; + x->x_charwidth = CHARWIDTH; + x->x_charheight = CHARHEIGHT; + x->x_nbchars = NBCHARS; + x->x_charmaps = (char*)malloc(x->x_nbchars*x->x_charwidth*x->x_charheight*sizeof(char)); + memcpy( (void*)x->x_charmaps, (void*)charmaps, x->x_nbchars*x->x_charwidth*x->x_charheight*sizeof(char) ); + return (void *)x; } @@ -250,6 +380,7 @@ void pdp_ascii_setup(void) class_addmethod(pdp_ascii_class, (t_method)pdp_ascii_color, gensym("color"), A_DEFFLOAT, A_NULL); class_addmethod(pdp_ascii_class, (t_method)pdp_ascii_brightness, gensym("brightness"), A_DEFFLOAT, A_NULL); class_addmethod(pdp_ascii_class, (t_method)pdp_ascii_ratio, gensym("ratio"), A_DEFFLOAT, A_NULL); + class_addmethod( pdp_ascii_class, (t_method)pdp_ascii_load, gensym("load"), A_SYMBOL, 0); class_sethelpsymbol( pdp_ascii_class, gensym("pdp_ascii.pd") ); } diff --git a/modules/pdp_canvas.c b/modules/pdp_canvas.c index a099da1..587811e 100644 --- a/modules/pdp_canvas.c +++ b/modules/pdp_canvas.c @@ -41,6 +41,7 @@ typedef struct pdp_canvas_struct t_int x_queue_id; t_int x_opacket; + t_int x_lastpacket; t_int x_current; t_float x_xmouse; @@ -139,6 +140,7 @@ static void pdp_canvas_process_yv12(t_pdp_canvas *x) memcpy( pV+((py>>1)*(x->x_owidth>>1))+((t_int)(x->x_xoffsets[ii]+mx)>>1), ppV+((py-(t_int)x->x_yoffsets[ii])>>1)*(x->x_widths[ii]>>1)+(mx>>1), dx ); } + } } @@ -241,10 +243,10 @@ static void pdp_canvas_input(t_pdp_canvas *x, t_symbol *s, t_floatarg f, t_int n if (s== gensym("register_rw")) { - /* release the packet */ if ( x->x_packets[ni] != -1 ) - { - pdp_packet_mark_unused(x->x_packets[ni]); + { + // delete the packet + pdp_packet_delete(x->x_packets[ni]); x->x_packets[ni] = -1; } x->x_dropped = pdp_packet_convert_ro_or_drop(&x->x_packets[ni], (int)f, pdp_gensym("image/YCrCb/*") ); diff --git a/modules/pdp_ffmpeg~.c b/modules/pdp_ffmpeg~.c index 5669e96..58cdd61 100644 --- a/modules/pdp_ffmpeg~.c +++ b/modules/pdp_ffmpeg~.c @@ -601,7 +601,7 @@ static t_int *pdp_ffmpeg_perform(t_int *w) x->x_audioin_position=(x->x_audioin_position+1)%(2*MAX_AUDIO_PACKET_SIZE); if ( x->x_audioin_position == 2*MAX_AUDIO_PACKET_SIZE-1 ) { - post( "pdp_ffmpeg~ : reaching end of audio buffer" ); + // post( "pdp_ffmpeg~ : reaching end of audio buffer" ); } } diff --git a/modules/pdp_i.c b/modules/pdp_i.c index d7d4b2f..e18a077 100644 --- a/modules/pdp_i.c +++ b/modules/pdp_i.c @@ -204,7 +204,7 @@ static void pdp_i_recv(t_pdp_i *x) // check we don't overflow input buffer if ( x->x_inwriteposition+ret >= x->x_inbuffersize ) { - // post( "pdp_i : too much input...resetting" ); + post( "pdp_i : too much input...resetting" ); x->x_inwriteposition=0; return; } diff --git a/modules/pdp_lumafilt.c b/modules/pdp_lumafilt.c index 0de1676..17036d4 100644 --- a/modules/pdp_lumafilt.c +++ b/modules/pdp_lumafilt.c @@ -134,7 +134,7 @@ static void pdp_lumafilt_process_yv12(t_pdp_lumafilt *x) } else { - post( "pdp_lumafilt : luminosity value out of bounds : %d", luma ); + // post( "pdp_lumafilt : luminosity value out of bounds : %d", luma ); } pnY++; if ( (px%2==0) && (py%2==0) ) diff --git a/modules/pdp_mp4audiosource.cpp b/modules/pdp_mp4audiosource.cpp index 7ae18f4..40a8c91 100644 --- a/modules/pdp_mp4audiosource.cpp +++ b/modules/pdp_mp4audiosource.cpp @@ -101,8 +101,7 @@ bool CPDPAudioSource::Init(void) // for live capture we can match the source to the destination m_audioSrcSamplesPerFrame = m_audioDstSamplesPerFrame; - m_pcmFrameSize = - m_audioSrcSamplesPerFrame * m_audioSrcChannels * sizeof(u_int16_t); + m_pcmFrameSize = m_audioSrcSamplesPerFrame * m_audioSrcChannels * sizeof(u_int16_t); if (m_audioOssMaxBufferSize > 0) { size_t array_size; @@ -135,7 +134,6 @@ bool CPDPAudioSource::Init(void) void CPDPAudioSource::ProcessAudio(u_int8_t* pcmBuffer, u_int32_t pcmBufferSize) { - audio_buf_info info; Timestamp currentTime = GetTimestamp(); Timestamp timestamp; @@ -156,49 +154,14 @@ void CPDPAudioSource::ProcessAudio(u_int8_t* pcmBuffer, u_int32_t pcmBufferSize) memcpy( m_pcmFrameBuffer, pcmBuffer, pcmBufferSize ); } - if (info.bytes == m_audioOssMaxBufferSize) { - // means the audio buffer is full, and not capturing - // we want to make the timestamp based on the previous one - // When we hit this case, we start using the m_timestampOverflowArray - // This will give us a timestamp for when the array is full. - // - // In other words, if we have a full audio buffer (ie: it's not loading - // any more), we start storing the current timestamp into the array. - // This will let us "catch up", and have a somewhat accurate timestamp - // when we loop around - // - // wmay - I'm not convinced that this actually works - if the buffer - // cleans up, we'll ignore m_timestampOverflowArray - if (m_timestampOverflowArray != NULL && - m_timestampOverflowArray[m_timestampOverflowArrayIndex] != 0) { - timestamp = m_timestampOverflowArray[m_timestampOverflowArrayIndex]; - } else { - timestamp = m_prevTimestamp + SrcSamplesToTicks(m_audioSrcSamplesPerFrame); - } - - if (m_timestampOverflowArray != NULL) - m_timestampOverflowArray[m_timestampOverflowArrayIndex] = currentTime; - - debug_message("pdp_mp4live~ : audio buffer full !"); - - } else { - // buffer is not full - so, we make the timestamp based on the number - // of bytes in the buffer that we read. - timestamp = currentTime - SrcSamplesToTicks(SrcBytesToSamples(info.bytes)); - if (m_timestampOverflowArray != NULL) - m_timestampOverflowArray[m_timestampOverflowArrayIndex] = 0; - } + timestamp = currentTime - SrcSamplesToTicks(SrcBytesToSamples(pcmBufferSize)); #ifdef DEBUG_TIMESTAMPS - debug_message("pdp_mp4live~ : info.bytes=%d t=%llu timestamp=%llu delta=%llu", - info.bytes, currentTime, timestamp, timestamp - m_prevTimestamp); + debug_message("pdp_mp4live~ : bytes=%d t=%llu timestamp=%llu delta=%llu", + pcmBufferSize, currentTime, timestamp, timestamp - m_prevTimestamp); #endif m_prevTimestamp = timestamp; - if (m_timestampOverflowArray != NULL) { - m_timestampOverflowArrayIndex = (m_timestampOverflowArrayIndex + 1) % - m_audioOssMaxBufferFrames; - } ProcessAudioFrame(m_pcmFrameBuffer, m_pcmFrameSize, timestamp, false); } diff --git a/modules/pdp_mp4audiosync.cpp b/modules/pdp_mp4audiosync.cpp index e364c0f..e53a533 100644 --- a/modules/pdp_mp4audiosync.cpp +++ b/modules/pdp_mp4audiosync.cpp @@ -33,54 +33,36 @@ #include "our_config_file.h" #include "m_pd.h" -#define audio_message(loglevel, fmt...) message(loglevel, "audiosync", fmt) - static void pdp_audio_callback (void *userdata, Uint8 *stream, int len) { CPDPAudioSync *a = (CPDPAudioSync *)userdata; + // post( "pdp_mp4audiosync : audio callback" ); a->audio_callback(stream, len); } CPDPAudioSync::CPDPAudioSync (CPlayerSession *psptr, t_pdp_mp4player *pdp_father) : CAudioSync(psptr) { - m_fill_index = m_play_index = 0; - for (int ix = 0; ix < DECODE_BUFFERS_MAX; ix++) { - m_buffer_filled[ix] = 0; - m_sample_buffer[ix] = NULL; - } - m_buffer_size = 0; m_config_set = 0; - m_audio_initialized = 0; - m_audio_paused = 1; + m_audio_initialized = 1; + m_audio_paused = 0; m_resync_required = 0; m_dont_fill = 0; - m_consec_no_buffers = 0; - m_audio_waiting_buffer = 0; - m_skipped_buffers = 0; - m_didnt_fill_buffers = 0; m_play_time = 0 ; m_buffer_latency = 0; m_first_time = 1; - m_first_filled = 1; - m_buffer_offset_on = 0; - m_buffer_ts = 0; - m_load_audio_do_next_resync = 0; m_convert_buffer = NULL; m_father = pdp_father; + post( "pdp_mp4audiosync : created audio sync" ); } CPDPAudioSync::~CPDPAudioSync (void) { - for (int ix = 0; ix < DECODE_BUFFERS_MAX; ix++) { - if (m_sample_buffer[ix] != NULL) - free(m_sample_buffer[ix]); - m_sample_buffer[ix] = NULL; + if (m_sample_buffer[0] != NULL) + { + free(m_sample_buffer[0]); } + m_sample_buffer[0] = NULL; CHECK_AND_FREE(m_convert_buffer); - audio_message(LOG_NOTICE, - "Audio sync skipped %u buffers", - m_skipped_buffers); - audio_message(LOG_NOTICE, "didn't fill %u buffers", m_didnt_fill_buffers); } void CPDPAudioSync::set_config (int freq, @@ -107,10 +89,9 @@ void CPDPAudioSync::set_config (int freq, m_buffer_size = channels * sample_size * m_bytes_per_sample; - for (int ix = 0; ix < DECODE_BUFFERS_MAX; ix++) { - m_buffer_filled[ix] = 0; - m_sample_buffer[ix] = (uint8_t *)malloc(2 * m_buffer_size); - } + m_buffer_filled[0] = 0; + m_sample_buffer[0] = (uint8_t *)malloc(2 * m_buffer_size); + m_freq = freq; m_channels = channels; m_format = format; @@ -121,7 +102,7 @@ void CPDPAudioSync::set_config (int freq, } m_config_set = 1; m_msec_per_frame = (sample_size * 1000) / m_freq; - audio_message(LOG_DEBUG, "buffer size %d msec per frame %d", m_buffer_size, m_msec_per_frame); + post("pdp_mp4audiosync : buffer size %d msec per frame %d", m_buffer_size, m_msec_per_frame); }; uint8_t *CPDPAudioSync::get_audio_buffer (void) @@ -132,28 +113,14 @@ uint8_t *CPDPAudioSync::get_audio_buffer (void) return (NULL); } - if (m_audio_initialized != 0) { - locked = 1; - } - ret = m_buffer_filled[m_fill_index]; - if (ret == 1) { - m_audio_waiting_buffer = 1; - m_audio_waiting_buffer = 0; - if (m_dont_fill != 0) { - return (NULL); - } - locked = 0; - if (m_audio_initialized != 0) { - locked = 1; - } - ret = m_buffer_filled[m_fill_index]; - if (locked) - if (ret == 1) { - post("pdp_mp4audiosync : no buffer"); - return (NULL); - } + ret = m_buffer_filled[0]; + if (ret == 1) + { + post("pdp_mp4audiosync : no buffer"); + return (NULL); } - return (m_sample_buffer[m_fill_index]); + // post("pdp_mp4audiosync : get_audio_buffer : return %x", m_sample_buffer[0]); + return (m_sample_buffer[0]); } void CPDPAudioSync::load_audio_buffer (uint8_t *from, @@ -164,182 +131,71 @@ void CPDPAudioSync::load_audio_buffer (uint8_t *from, uint8_t *to; uint32_t copied; copied = 0; - if (m_buffer_offset_on == 0) { - int64_t diff = ts - m_buffer_ts; - if (m_buffer_ts != 0 && diff > 1) { - m_load_audio_do_next_resync = 1; - audio_message(LOG_DEBUG, "timeslot doesn't match - %llu %llu", - ts, m_buffer_ts); - } - m_buffer_ts = ts; - } else { - int64_t check; - check = ts - m_loaded_next_ts; - if (check > m_msec_per_frame) { - audio_message(LOG_DEBUG, "potential resync at ts "U64" should be ts "U64, - ts, m_loaded_next_ts); - uint32_t left; - left = m_buffer_size - m_buffer_offset_on; - to = get_audio_buffer(); - memset(to + m_buffer_offset_on, 0, left); - filled_audio_buffer(m_buffer_ts, 0); - m_buffer_offset_on = 0; - m_load_audio_do_next_resync = 1; - m_buffer_ts = ts; - } - } - m_loaded_next_ts = bytes * M_64; - m_loaded_next_ts /= m_bytes_per_sample; - m_loaded_next_ts /= m_freq; - m_loaded_next_ts += ts; - - while ( bytes > 0) { - to = get_audio_buffer(); - if (to == NULL) { - return; - } - int copy; - uint32_t left; - - left = m_buffer_size - m_buffer_offset_on; - copy = MIN(left, bytes); - memcpy(to + m_buffer_offset_on, from, copy); - bytes -= copy; - copied += copy; - from += copy; - m_buffer_offset_on += copy; - if (m_buffer_offset_on >= m_buffer_size) { - m_buffer_offset_on = 0; - filled_audio_buffer(m_buffer_ts, resync | m_load_audio_do_next_resync); - m_buffer_ts += m_msec_per_frame; - resync = 0; - m_load_audio_do_next_resync = 0; - } + post( "pdp_mp4audiosync : load audio buffer : length=%d", bytes ); + + to = get_audio_buffer(); + if (to == NULL) + { + return; } + int copy; + uint32_t left; + + bytes = MIN(m_buffer_size, bytes); + memcpy(to, from, bytes); + return; } void CPDPAudioSync::filled_audio_buffer (uint64_t ts, int resync) { - uint32_t fill_index; - int locked; - // m_dont_fill will be set when we have a pause - if (m_dont_fill == 1) { - return; - } - // resync = 0; - fill_index = m_fill_index; - m_fill_index++; - m_fill_index %= DECODE_BUFFERS_MAX; - - locked = 0; - if (m_audio_initialized != 0) { - locked = 1; - } - if (m_first_filled != 0) { - m_first_filled = 0; - resync = 0; - m_resync_required = 0; - } else { - int64_t diff; - diff = ts - m_last_fill_timestamp; - if (diff - m_msec_per_frame > m_msec_per_frame) { - // have a hole here - don't want to resync - if (diff > ((m_msec_per_frame + 1) * 4)) { - resync = 1; - } else { - // try to fill the holes - m_last_fill_timestamp += m_msec_per_frame + 1; // fill plus extra - int64_t ts_diff; - do { - uint8_t *retbuffer; - // Get and swap buffers. - retbuffer = get_audio_buffer(); - if (retbuffer == NULL) { - return; - } - if (retbuffer != m_sample_buffer[m_fill_index]) { - audio_message(LOG_ERR, "retbuffer not fill index in audio sync"); - return; - } - locked = 0; - if (m_audio_initialized != 0) { - locked = 1; - } - m_sample_buffer[m_fill_index] = m_sample_buffer[fill_index]; - m_sample_buffer[fill_index] = retbuffer; - memset(retbuffer, m_silence, m_buffer_size); - m_buffer_time[fill_index] = m_last_fill_timestamp; - m_buffer_filled[fill_index] = 1; - m_samples_loaded += m_buffer_size; - fill_index++; - fill_index %= DECODE_BUFFERS_MAX; - m_fill_index++; - m_fill_index %= DECODE_BUFFERS_MAX; - audio_message(LOG_NOTICE, "Filling timestamp %llu with silence", - m_last_fill_timestamp); - m_last_fill_timestamp += m_msec_per_frame + 1; // fill plus extra - ts_diff = ts - m_last_fill_timestamp; - audio_message(LOG_DEBUG, "diff is %lld", ts_diff); - } while (ts_diff > 0); - locked = 0; - if (m_audio_initialized != 0) { - locked = 1; - } - } - } else { - if (m_last_fill_timestamp == ts) { - audio_message(LOG_NOTICE, "Repeat timestamp with audio %llu", ts); - return; + // post( "pdp_mp4audiosync : filled audio buffer" ); + // if (resync) m_psptr->wake_sync_thread(); + + if ( m_father->x_audio ) + { + // copy the buffer filled by the codec towards pdp + if ( (m_father->x_audioin_position*sizeof(short)+m_buffer_size) < (4*MAX_AUDIO_PACKET_SIZE*sizeof(short)) ) + { + memcpy( m_father->x_audio_in+m_father->x_audioin_position, m_sample_buffer[0], m_buffer_size ); + m_father->x_audioin_position+=(m_buffer_size/sizeof(short)); + // post( "pdp_mp4audiosync : filled_audio_buffer : copied %d PCM samples : audio in : %d : resync : %d", + // m_buffer_size/sizeof(short), m_father->x_audioin_position, resync ); + if ( ( m_father->x_audioin_position > DEFAULT_CHANNELS*m_father->x_blocksize ) + && (!m_father->x_audioon) ) + { + m_father->x_audioon = 1; + // post( "pdp_mp4audiosync : audio on" ); } } + else + { + post( "pdp_mp4audiosync : filled_audio_buffer : skipped buffer : (in : %d)", + m_father->x_audioin_position ); + } } - m_last_fill_timestamp = ts; - m_buffer_filled[fill_index] = 1; - m_samples_loaded += m_buffer_size; - m_buffer_time[fill_index] = ts; - if (resync) { - m_resync_required = 1; - m_resync_buffer = fill_index; -#ifdef DEBUG_AUDIO_FILL - audio_message(LOG_DEBUG, "Resync from filled_audio_buffer"); -#endif - } - - // Check this - we might not want to do this unless we're resyncing - if (resync) m_psptr->wake_sync_thread(); -#ifdef DEBUG_AUDIO_FILL - audio_message(LOG_DEBUG, "Filling " LLU " %u %u", ts, fill_index, m_samples_loaded); -#endif + + return; } void CPDPAudioSync::set_eof(void) { uint8_t *to; - if (m_buffer_offset_on != 0) { - to = get_audio_buffer(); - if (to != NULL) { - uint32_t left; - left = m_buffer_size - m_buffer_offset_on; - memset(to + m_buffer_offset_on, 0, left); - m_buffer_offset_on = 0; - filled_audio_buffer(m_buffer_ts, 0); - m_buffer_ts += m_msec_per_frame; - } - } + + to = get_audio_buffer(); CAudioSync::set_eof(); } -int CPDPAudioSync::initialize_audio (int have_video) +int CPDPAudioSync::initialize_audio (int have_audio) { + m_audio_initialized = 1; return (1); } int CPDPAudioSync::is_audio_ready (uint64_t &disptime) { - disptime = m_buffer_time[m_play_index]; - return (m_dont_fill == 0 && m_buffer_filled[m_play_index] == 1); + return (1); } uint64_t CPDPAudioSync::check_audio_sync (uint64_t current_time, int &have_eof) @@ -355,6 +211,8 @@ void CPDPAudioSync::audio_callback (Uint8 *stream, int ilen) int delay = 0; int playtime; + post( "pdp_mp4audiosync : audio callback" ); + } void CPDPAudioSync::play_audio (void) @@ -366,29 +224,14 @@ void CPDPAudioSync::play_audio (void) void CPDPAudioSync::flush_sync_buffers (void) { + post( "pdp_mp4audiosync : flush sync buffer" ); clear_eof(); - m_dont_fill = 1; - if (m_audio_waiting_buffer) { - m_audio_waiting_buffer = 0; - } } void CPDPAudioSync::flush_decode_buffers (void) { - int locked = 0; - if (m_audio_initialized != 0) { - locked = 1; - } - m_dont_fill = 0; - m_first_filled = 1; - for (int ix = 0; ix < DECODE_BUFFERS_MAX; ix++) { - m_buffer_filled[ix] = 0; - } - m_buffer_offset_on = 0; - m_play_index = m_fill_index = 0; - m_audio_paused = 1; - m_resync_buffer = 0; - m_samples_loaded = 0; + post( "pdp_mp4audiosync : flush decode buffer" ); + m_buffer_filled[0] = 0; } void CPDPAudioSync::set_volume (int volume) @@ -466,7 +309,7 @@ static void pdp_filled_audio_buffer (void *ifptr, int resync_req) { ((CPDPAudioSync *)ifptr)->filled_audio_buffer(ts, - resync_req); + resync_req); } static void pdp_load_audio_buffer (void *ifptr, diff --git a/modules/pdp_mp4live~.cpp b/modules/pdp_mp4live~.cpp index dfa55a5..2c3f52c 100644 --- a/modules/pdp_mp4live~.cpp +++ b/modules/pdp_mp4live~.cpp @@ -60,7 +60,7 @@ #define VIDEO_BUFFER_SIZE (1024*1024) #define MAX_AUDIO_PACKET_SIZE (128 * 1024) -#define AUDIO_PACKET_SIZE (2*1024) /* using aac encoding */ +#define AUDIO_PACKET_SIZE (2*1024*2) /* using aac encoding, 2 channels, 2 bytes per sample */ static char *pdp_mp4live_version = "pdp_mp4live~: version 0.1, an mpeg4ip video streaming object ( ydegoyon@free.fr )"; @@ -90,8 +90,6 @@ typedef struct pdp_mp4live_struct /* audio structures */ short x_audio_buf[2*MAX_AUDIO_PACKET_SIZE]; /* buffer for incoming audio */ - short x_audio_enc_buf[2*MAX_AUDIO_PACKET_SIZE]; /* buffer for audio to be encoded */ - uint8_t x_audio_out[4*MAX_AUDIO_PACKET_SIZE]; /* buffer for encoded audio */ t_int x_audioin_position; // writing position for incoming audio t_int x_audio_per_frame; // number of audio samples to transmit for each frame @@ -425,8 +423,6 @@ static void pdp_mp4live_process_yv12(t_pdp_mp4live *x) pU = data+x->x_vsize; pV = data+x->x_vsize+(x->x_vsize>>2); - x->x_videosource->ProcessVideo( pY, pV, pU ); - /* update frames counter */ if ( gettimeofday(&etime, NULL) == -1) @@ -442,17 +438,8 @@ static void pdp_mp4live_process_yv12(t_pdp_mp4live *x) x->x_nbframes++; x->x_secondcount++; - /* send an audio frame */ - if ( x->x_audioin_position > x->x_audio_per_frame ) - { - x->x_audiosource->ProcessAudio( (u_int8_t*)x->x_audio_buf, - (u_int32_t)x->x_audio_per_frame*sizeof(short) ); + x->x_videosource->ProcessVideo( pY, pV, pU ); - /* output resampled raw samples */ - memcpy( x->x_audio_buf, x->x_audio_buf+x->x_audio_per_frame, - x->x_audioin_position-x->x_audio_per_frame ); - x->x_audioin_position-=x->x_audio_per_frame; - } } return; } @@ -499,6 +486,21 @@ static t_int *pdp_mp4live_perform(t_int *w) } } + if ( x->x_streaming ) + { + /* send an audio frame */ + if ( (t_int)(x->x_audioin_position*sizeof(short)) > (t_int)x->x_audio_per_frame ) + { + x->x_audiosource->ProcessAudio( (u_int8_t*)x->x_audio_buf, + (u_int32_t)x->x_audio_per_frame ); + + /* recopy the buffer and set new pointers */ + memcpy( x->x_audio_buf, x->x_audio_buf+(x->x_audio_per_frame/sizeof(short)), + x->x_audioin_position*sizeof(short)-x->x_audio_per_frame ); + x->x_audioin_position-=(x->x_audio_per_frame/sizeof(short)); + } + } + return (w+5); } diff --git a/modules/pdp_mp4player~.cpp b/modules/pdp_mp4player~.cpp index 613f673..de47614 100644 --- a/modules/pdp_mp4player~.cpp +++ b/modules/pdp_mp4player~.cpp @@ -102,10 +102,13 @@ static void pdp_mp4player_disconnect(t_pdp_mp4player *x) } x->x_streaming = 0; + x->x_newpicture = 0; outlet_float( x->x_outlet_streaming, x->x_streaming ); x->x_nbframes = 0; outlet_float( x->x_outlet_nbframes, x->x_nbframes ); + x->x_framerate = 0; + outlet_float( x->x_outlet_framerate, x->x_framerate ); post( "pdp_mp4player~ : deleting session" ); delete x->x_psession; @@ -227,11 +230,12 @@ static t_int *pdp_mp4player_perform(t_int *w) struct timeval etime; t_int sn; + x->x_blocksize = n; + // just read the buffer if ( x->x_audioon ) { sn=0; - n=n*DEFAULT_CHANNELS; while (n--) { sampleL=x->x_audio_in[ sn++ ]; @@ -249,7 +253,7 @@ static t_int *pdp_mp4player_perform(t_int *w) out2++; } x->x_audioin_position-=sn; - memcpy( &x->x_audio_in[0], &x->x_audio_in[sn], 4*MAX_AUDIO_PACKET_SIZE-sn ); + memcpy( &x->x_audio_in[0], &x->x_audio_in[sn], (4*MAX_AUDIO_PACKET_SIZE-sn-1)*sizeof(short) ); // post( "pdp_mp4player~ : audio in position : %d", x->x_audioin_position ); if ( x->x_audioin_position <= sn ) { @@ -281,13 +285,17 @@ static t_int *pdp_mp4player_perform(t_int *w) if ( x->x_newpicture ) { - pdp_packet_pass_if_valid(x->x_pdp_out, &x->x_packet0); + x->x_packet = pdp_packet_new_image_YCrCb( x->x_vwidth, x->x_vheight ); + x->x_data = (short int *)pdp_packet_data(x->x_packet); + memcpy( x->x_data, x->x_datav, (x->x_vsize + (x->x_vsize>>1))<<1 ); + pdp_packet_pass_if_valid(x->x_pdp_out, &x->x_packet); // update streaming status outlet_float( x->x_outlet_streaming, x->x_streaming ); x->x_nbframes++; x->x_secondcount++; outlet_float( x->x_outlet_nbframes, x->x_nbframes ); + x->x_newpicture = 0; } return (w+5); @@ -307,7 +315,7 @@ static void pdp_mp4player_free(t_pdp_mp4player *x) pdp_mp4player_disconnect(x); } post( "pdp_mp4player~ : freeing object" ); - pdp_packet_mark_unused(x->x_packet0); + pdp_packet_mark_unused(x->x_packet); // remove invalid global ports close_plugins(); @@ -330,16 +338,20 @@ void *pdp_mp4player_new(void) x->x_outlet_nbframes = outlet_new(&x->x_obj, &s_float); x->x_outlet_framerate = outlet_new(&x->x_obj, &s_float); - x->x_packet0 = -1; + x->x_packet = -1; x->x_nbframes = 0; x->x_cursec = 0; x->x_secondcount = 0; x->x_audioin_position = 0; + x->x_blocksize = MIN_AUDIO_SIZE; x->x_priority = DEFAULT_PRIORITY; x->x_decodechild = 0; x->x_newpicture = 0; - memset( &x->x_audio_buf[0], 0x0, 4*MAX_AUDIO_PACKET_SIZE*sizeof(short) ); + x->x_vwidth = -1; + x->x_vheight = -1; + x->x_datav = NULL; + memset( &x->x_audio_in[0], 0x0, 4*MAX_AUDIO_PACKET_SIZE*sizeof(short) ); // initialize mpeg4hippies diff --git a/modules/pdp_mp4videosync.cpp b/modules/pdp_mp4videosync.cpp index b7fca2e..76a874b 100644 --- a/modules/pdp_mp4videosync.cpp +++ b/modules/pdp_mp4videosync.cpp @@ -175,9 +175,47 @@ void CPDPVideoSync::set_video_frame(const uint8_t *y, int pixelw_uv, uint64_t time) { - // post( "pdp_mp4videosync : set video frame : %dx%d", m_width, m_height ); + short int *pY, *pU, *pV; + t_int px, py; + m_psptr->wake_sync_thread(); + if ( !m_father->x_streaming ) + { + return; + } + + // transforming into a image for pdp + if ( ( (t_int)m_father->x_vheight != (t_int)m_height ) || + ( (t_int)m_father->x_vwidth != (t_int)m_width ) ) + { + m_father->x_vheight = m_height; + m_father->x_vwidth = m_width; + m_father->x_vsize = m_father->x_vheight*m_father->x_vwidth; + post( "pdp_mp4videosync : allocating video data : %dx%d", m_width, m_height ); + + // allocate video data + m_father->x_datav = ( short int* ) malloc( (m_father->x_vsize+(m_father->x_vsize>>1))<<1 ); + } + + // post( "pdp_mp4videosync : set video frame : width : y:%d, uv:%d", pixelw_y, pixelw_uv ); + + pY = m_father->x_datav; + pV = m_father->x_datav+m_father->x_vsize; + pU = m_father->x_datav+m_father->x_vsize+(m_father->x_vsize>>2); + for(py=0; pyx_vheight; py++) + { + for(px=0; pxx_vwidth; px++) + { + *(pY+py*m_father->x_vwidth+px) = *(y+py*pixelw_y+px)<<7; + if ( ( px%2 == 0 ) && ( py%2 == 0 ) ) + { + *(pU+(py>>1)*(m_father->x_vwidth>>1)+(px>>1)) = ((*(u+(py>>1)*pixelw_uv+(px>>1)))-128)<<8; + *(pV+(py>>1)*(m_father->x_vwidth>>1)+(px>>1)) = ((*(v+(py>>1)*pixelw_uv+(px>>1)))-128)<<8; + } + } + } + // pass the data to the pdp object m_father->x_newpicture = 1; return; diff --git a/modules/pdp_ocanvas.c b/modules/pdp_ocanvas.c index e06a500..c92253b 100644 --- a/modules/pdp_ocanvas.c +++ b/modules/pdp_ocanvas.c @@ -238,7 +238,7 @@ static void pdp_ocanvas_input(t_pdp_ocanvas *x, t_symbol *s, t_floatarg f, t_int /* release the packet */ if ( x->x_packets[ni] != -1 ) { - pdp_packet_mark_unused(x->x_packets[ni]); + pdp_packet_delete(x->x_packets[ni]); x->x_packets[ni] = -1; } x->x_dropped = pdp_packet_convert_ro_or_drop(&x->x_packets[ni], (int)f, pdp_gensym("image/YCrCb/*") ); diff --git a/modules/pdp_vertigo.c b/modules/pdp_vertigo.c index ce5295a..f9dd553 100644 --- a/modules/pdp_vertigo.c +++ b/modules/pdp_vertigo.c @@ -211,7 +211,7 @@ static void pdp_vertigo_process_yv12(t_pdp_vertigo *x) if ( i >= (x->x_vsize + (x->x_vsize>>1)) ) i = (x->x_vsize + (x->x_vsize>>1))-1; v = pcy[i] & 0xffff; v = (v * 3) + ((*poy++) & 0xffff); - *pny++ = (v>>2); + *pny++ = (v>>2)<<7; if ( (((px+1)%2)==0) && (((py+1)%2)==0) ) { ninc++; -- cgit v1.2.1