X-Git-Url: http://research.m1stereo.tv/gitweb?a=blobdiff_plain;f=src%2Fmodules%2Favformat%2Fconsumer_avformat.c;h=1e801ce7b4a9f94c42f136e956ace4f208048cfd;hb=3833ec4ee7ffd18732242b30b602740d1ac7acad;hp=41febfccf547815f6837d1771913a727753d4e43;hpb=9b304e5ac1fbab3425088a96c72a652e39a0a260;p=melted diff --git a/src/modules/avformat/consumer_avformat.c b/src/modules/avformat/consumer_avformat.c index 41febfc..1e801ce 100644 --- a/src/modules/avformat/consumer_avformat.c +++ b/src/modules/avformat/consumer_avformat.c @@ -35,17 +35,54 @@ // avformat header files #include +// +// This structure should be extended and made globally available in mlt +// + typedef struct { int16_t *buffer; int size; int used; + double time; + int frequency; + int channels; } *sample_fifo, sample_fifo_s; -sample_fifo sample_fifo_init( ) +sample_fifo sample_fifo_init( int frequency, int channels ) +{ + sample_fifo this = calloc( 1, sizeof( sample_fifo_s ) ); + this->frequency = frequency; + this->channels = channels; + return this; +} + +// sample_fifo_clear and check are temporarily aborted (not working as intended) + +void sample_fifo_clear( sample_fifo this, double time ) +{ + int words = ( float )( time - this->time ) * this->frequency * this->channels; + if ( ( int )( ( float )time * 100 ) < ( int )( ( float )this->time * 100 ) && this->used > words && words > 0 ) + { + memmove( this->buffer, &this->buffer[ words ], ( this->used - words ) * sizeof( int16_t ) ); + this->used -= words; + this->time = time; + } + else if ( ( int )( ( float )time * 100 ) != ( int )( ( float )this->time * 100 ) ) + { + this->used = 0; + this->time = time; + } +} + +void sample_fifo_check( sample_fifo this, double time ) { - return calloc( 1, sizeof( sample_fifo_s ) ); + if ( this->used == 0 ) + { + if ( ( int )( ( float )time * 100 ) < ( int )( ( float )this->time * 100 ) ) + this->time = time; + } } void sample_fifo_append( sample_fifo this, int16_t *samples, int count ) @@ -74,6 +111,8 @@ int sample_fifo_fetch( sample_fifo this, int16_t *samples, int count ) this->used -= count; memmove( this->buffer, &this->buffer[ count ], this->used * sizeof( int16_t ) ); + this->time += ( double )count / this->channels / this->frequency; + return count; } @@ -112,7 +151,6 @@ mlt_consumer consumer_avformat_init( char *arg ) mlt_properties_set( properties, "target", arg ); // sample and frame queue - mlt_properties_set_data( properties, "sample_fifo", sample_fifo_init( ), 0, ( mlt_destructor )sample_fifo_close, NULL ); mlt_properties_set_data( properties, "frame_queue", mlt_deque_init( ), 0, ( mlt_destructor )mlt_deque_close, NULL ); // Set avformat defaults (all lifted from ffmpeg.c) @@ -555,7 +593,7 @@ static void *consumer_thread( void *arg ) int samples = 0; // AVFormat audio buffer and frame size - int audio_outbuf_size = 2 * 128 * 1024; + int audio_outbuf_size = 10000; uint8_t *audio_outbuf = av_malloc( audio_outbuf_size ); int audio_input_frame_size = 0; @@ -625,7 +663,7 @@ static void *consumer_thread( void *arg ) fmt = guess_format( "mpeg", NULL, NULL ); // We need a filename - default to stdout? - if ( filename == NULL ) + if ( filename == NULL || !strcmp( filename, "" ) ) filename = "pipe:"; // Get the codec ids selected @@ -685,7 +723,7 @@ static void *consumer_thread( void *arg ) // Open the output file, if needed if ( !( fmt->flags & AVFMT_NOFILE ) ) { - if (url_fopen(&oc->pb, filename, URL_RDWR) < 0) + if (url_fopen(&oc->pb, filename, URL_WRONLY) < 0) { fprintf(stderr, "Could not open '%s'\n", filename); mlt_properties_set_int( properties, "running", 0 ); @@ -732,6 +770,15 @@ static void *consumer_thread( void *arg ) { samples = mlt_sample_calculator( fps, frequency, count ); mlt_frame_get_audio( frame, &pcm, &aud_fmt, &frequency, &channels, &samples ); + + // Create the fifo if we don't have one + if ( fifo == NULL ) + { + fifo = sample_fifo_init( frequency, channels ); + mlt_properties_set_data( properties, "sample_fifo", fifo, 0, ( mlt_destructor )sample_fifo_close, NULL ); + } + + // Append the samples sample_fifo_append( fifo, pcm, samples * channels ); total_time += ( samples * 1000000 ) / frequency; } @@ -748,12 +795,12 @@ static void *consumer_thread( void *arg ) { // Compute current audio and video time if (audio_st) - audio_pts = (double)audio_st->pts.val * oc->pts_num / oc->pts_den; + audio_pts = (double)audio_st->pts.val * audio_st->time_base.num / audio_st->time_base.den; else audio_pts = 0.0; if (video_st) - video_pts = (double)video_st->pts.val * oc->pts_num / oc->pts_den; + video_pts = (double)video_st->pts.val * video_st->time_base.num / video_st->time_base.den; else video_pts = 0.0; @@ -762,17 +809,22 @@ static void *consumer_thread( void *arg ) { if ( channels * audio_input_frame_size < sample_fifo_used( fifo ) ) { - int out_size; AVCodecContext *c; + AVPacket pkt; + av_init_packet( &pkt ); c = &audio_st->codec; sample_fifo_fetch( fifo, buffer, channels * audio_input_frame_size ); - out_size = avcodec_encode_audio( c, audio_outbuf, audio_outbuf_size, buffer ); - + pkt.size = avcodec_encode_audio( c, audio_outbuf, audio_outbuf_size, buffer ); // Write the compressed frame in the media file - if (av_write_frame(oc, audio_st->index, audio_outbuf, out_size) != 0) + pkt.pts= c->coded_frame->pts; + pkt.flags |= PKT_FLAG_KEY; + pkt.stream_index= audio_st->index; + pkt.data= audio_outbuf; + + if ( av_write_frame( oc, &pkt ) != 0) fprintf(stderr, "Error while writing audio frame\n"); } else @@ -820,7 +872,15 @@ static void *consumer_thread( void *arg ) if (oc->oformat->flags & AVFMT_RAWPICTURE) { // raw video case. The API will change slightly in the near future for that - ret = av_write_frame(oc, video_st->index, (uint8_t *)output, sizeof(AVPicture)); + AVPacket pkt; + av_init_packet(&pkt); + + pkt.flags |= PKT_FLAG_KEY; + pkt.stream_index= video_st->index; + pkt.data= (uint8_t *)output; + pkt.size= sizeof(AVPicture); + + ret = av_write_frame(oc, &pkt); } else { @@ -833,9 +893,18 @@ static void *consumer_thread( void *arg ) // If zero size, it means the image was buffered if (out_size != 0) { - // write the compressed frame in the media file - // XXX: in case of B frames, the pts is not yet valid - ret = av_write_frame( oc, video_st->index, video_outbuf, out_size ); + AVPacket pkt; + av_init_packet( &pkt ); + + pkt.pts= c->coded_frame->pts; + if(c->coded_frame->key_frame) + pkt.flags |= PKT_FLAG_KEY; + pkt.stream_index= video_st->index; + pkt.data= video_outbuf; + pkt.size= out_size; + + // write the compressed frame in the media file + ret = av_write_frame(oc, &pkt); } } frame_count++; @@ -851,8 +920,11 @@ static void *consumer_thread( void *arg ) if ( real_time_output && frames % 25 == 0 ) { long passed = time_difference( &ante ); - long pending = ( ( ( long )sample_fifo_used( fifo ) * 1000 ) / frequency ) * 1000; - passed -= pending; + if ( fifo != NULL ) + { + long pending = ( ( ( long )sample_fifo_used( fifo ) * 1000 ) / frequency ) * 1000; + passed -= pending; + } if ( passed < total_time ) { long total = ( total_time - passed );