From c287a51b7a4c2cc437256c178a5eb2e7775d34fa Mon Sep 17 00:00:00 2001 From: lilo_booter Date: Mon, 29 Mar 2004 12:06:13 +0000 Subject: [PATCH] consumer avformat added, various cleanups and consumer realtime switching git-svn-id: https://mlt.svn.sourceforge.net/svnroot/mlt/trunk/mlt@257 d19143bc-622f-0410-bfdd-b5b2a6649095 --- README | 13 +- src/framework/configure | 2 +- src/framework/mlt.h | 1 - src/framework/mlt_consumer.c | 56 +- src/framework/mlt_factory.c | 2 +- src/framework/mlt_pool.c | 4 +- src/modules/avformat/Makefile | 3 +- src/modules/avformat/configure | 4 + src/modules/avformat/consumer_avformat.c | 714 +++++++++++++++++++++++ src/modules/avformat/consumer_avformat.h | 28 + src/modules/avformat/factory.c | 69 +++ src/modules/avformat/producer_avformat.c | 64 +- src/modules/dv/consumer_libdv.c | 8 +- src/modules/dv/producer_libdv.c | 4 +- src/modules/sdl/consumer_sdl.c | 6 +- 15 files changed, 896 insertions(+), 82 deletions(-) create mode 100644 src/modules/avformat/consumer_avformat.c create mode 100644 src/modules/avformat/consumer_avformat.h diff --git a/README b/README index 1b6aa5b6..b22c0a85 100644 --- a/README +++ b/README @@ -7,10 +7,12 @@ MLT/Miracle README MLT is a multimedia framework designed for television broadcasting, and Miracle is a multi-unit video playout server with realtime effects. + This document provides a quick reference for the minimal configuration, build and installation of MLT. See the docs directory for usage and development details. + Configuration ------------- @@ -20,14 +22,15 @@ Configuration Usage is: - ./configure --help - report all configure options - ./configure --prefix=[dir] - target install directory (default: /usr/local) + ./configure --help - report all configure options + ./configure --prefix=[dir] - target install directory (default: /usr/local) ./configure --disable-[mod] - do not compile specified module(s) - ./configure --[other] - pass through to children + ./configure --[other] - pass through to children NB: This script must be run to register new services after a CVS checkout or subsequent update. - + + Compilation ----------- @@ -37,6 +40,7 @@ Compilation to compile the system. + Testing ------- @@ -48,6 +52,7 @@ Testing NB: This applies to your current shell only and it assumes a bash or regular bourne shell is in use. + Installation ------------ diff --git a/src/framework/configure b/src/framework/configure index ba91ce66..bdc61c37 100755 --- a/src/framework/configure +++ b/src/framework/configure @@ -1,2 +1,2 @@ #!/bin/bash -echo "framework -I$prefix/mlt -D_REENTRANT -lmlt" >> ../../packages.dat +echo "framework -I$prefix/include/mlt -D_REENTRANT -lmlt" >> ../../packages.dat diff --git a/src/framework/mlt.h b/src/framework/mlt.h index f7f802e1..9cbfc414 100644 --- a/src/framework/mlt.h +++ b/src/framework/mlt.h @@ -34,7 +34,6 @@ extern "C" #include "mlt_transition.h" #include "mlt_consumer.h" #include "mlt_filter.h" -#include "mlt_manager.h" #include "mlt_playlist.h" #include "mlt_properties.h" #include "mlt_field.h" diff --git a/src/framework/mlt_consumer.c b/src/framework/mlt_consumer.c index 2a05df19..9bccbed1 100644 --- a/src/framework/mlt_consumer.c +++ b/src/framework/mlt_consumer.c @@ -71,6 +71,13 @@ int mlt_consumer_init( mlt_consumer this, void *child ) // Default read ahead buffer size mlt_properties_set_int( properties, "buffer", 25 ); + // Default audio frequency and channels + mlt_properties_set_int( properties, "frequency", 48000 ); + mlt_properties_set_int( properties, "channels", 2 ); + + // Default of all consumers is real time + mlt_properties_set_int( properties, "real_time", 1 ); + // Hmm - default all consumers to yuv422 :-/ this->format = mlt_image_yuv422; } @@ -357,24 +364,40 @@ mlt_frame mlt_consumer_rt_frame( mlt_consumer this ) { // Frame to return mlt_frame frame = NULL; - int size = 1; - // Is the read ahead running? - if ( this->ahead == 0 ) + // Get the properties + mlt_properties properties = mlt_consumer_properties( this ); + + // Check if the user has requested real time or not + if ( mlt_properties_get_int( properties, "real_time" ) ) { - int buffer = mlt_properties_get_int( mlt_consumer_properties( this ), "buffer" ); - consumer_read_ahead_start( this ); - if ( buffer > 1 ) - size = buffer / 2; + int size = 1; + + // Is the read ahead running? + if ( this->ahead == 0 ) + { + int buffer = mlt_properties_get_int( properties, "buffer" ); + consumer_read_ahead_start( this ); + if ( buffer > 1 ) + size = buffer / 2; + } + + // Get frame from queue + pthread_mutex_lock( &this->mutex ); + while( this->ahead && mlt_deque_count( this->queue ) < size ) + pthread_cond_wait( &this->cond, &this->mutex ); + frame = mlt_deque_pop_front( this->queue ); + pthread_cond_broadcast( &this->cond ); + pthread_mutex_unlock( &this->mutex ); } + else + { + // Get the frame in non real time + frame = mlt_consumer_get_frame( this ); - // Get frame from queue - pthread_mutex_lock( &this->mutex ); - while( this->ahead && mlt_deque_count( this->queue ) < size ) - pthread_cond_wait( &this->cond, &this->mutex ); - frame = mlt_deque_pop_front( this->queue ); - pthread_cond_broadcast( &this->cond ); - pthread_mutex_unlock( &this->mutex ); + // This isn't true, but from the consumers perspective it is + mlt_properties_set_int( mlt_frame_properties( frame ), "rendered", 1 ); + } return frame; } @@ -391,8 +414,9 @@ int mlt_consumer_stop( mlt_consumer this ) if ( this->stop != NULL ) this->stop( this ); - // Kill the read ahead - consumer_read_ahead_stop( this ); + // Check if the user has requested real time or not and stop if necessary + if ( mlt_properties_get_int( properties, "real_time" ) ) + consumer_read_ahead_stop( this ); // Kill the test card mlt_properties_set_data( properties, "test_card_producer", NULL, 0, NULL, NULL ); diff --git a/src/framework/mlt_factory.c b/src/framework/mlt_factory.c index 61093a13..4b92fe41 100644 --- a/src/framework/mlt_factory.c +++ b/src/framework/mlt_factory.c @@ -195,8 +195,8 @@ void mlt_factory_close( ) mlt_repository_close( filters ); mlt_repository_close( transitions ); mlt_repository_close( consumers ); - mlt_properties_close( object_list ); mlt_properties_close( global_properties ); + mlt_properties_close( object_list ); free( mlt_prefix ); mlt_prefix = NULL; mlt_pool_close( ); diff --git a/src/framework/mlt_pool.c b/src/framework/mlt_pool.c index e908cbf7..12ad00c3 100644 --- a/src/framework/mlt_pool.c +++ b/src/framework/mlt_pool.c @@ -303,11 +303,13 @@ void mlt_pool_close( ) #ifdef _MLT_POOL_CHECKS_ // Stats dump on close int i = 0; + fprintf( stderr, "Usage:\n\n" ); for ( i = 0; i < mlt_properties_count( pools ); i ++ ) { mlt_pool pool = mlt_properties_get_data_at( pools, i, NULL ); if ( pool->count ) - fprintf( stderr, "%d: allocated %d returned %d\n", pool->size, pool->count, mlt_deque_count( pool->stack ) ); + fprintf( stderr, "%d: allocated %d returned %d %c\n", pool->size, pool->count, mlt_deque_count( pool->stack ), + pool->count != mlt_deque_count( pool->stack ) ? '*' : ' ' ); } #endif diff --git a/src/modules/avformat/Makefile b/src/modules/avformat/Makefile index 66e57f83..3d1e1cc0 100644 --- a/src/modules/avformat/Makefile +++ b/src/modules/avformat/Makefile @@ -3,7 +3,8 @@ include ../../../config.mak TARGET = ../libmltavformat.so OBJS = factory.o \ - producer_avformat.o + producer_avformat.o \ + consumer_avformat.o CFLAGS = -O3 -I../../ -Wall -g -D_FILE_OFFSET_BITS=64 diff --git a/src/modules/avformat/configure b/src/modules/avformat/configure index a3be0c65..f3dd31a2 100755 --- a/src/modules/avformat/configure +++ b/src/modules/avformat/configure @@ -7,5 +7,9 @@ cat << EOF >> ../producers.dat avformat libmltavformat.so EOF +cat << EOF >> ../consumers.dat +avformat libmltavformat.so +EOF + fi diff --git a/src/modules/avformat/consumer_avformat.c b/src/modules/avformat/consumer_avformat.c new file mode 100644 index 00000000..4b64d379 --- /dev/null +++ b/src/modules/avformat/consumer_avformat.c @@ -0,0 +1,714 @@ +/* + * consumer_avformat.c -- an encoder based on avformat + * Copyright (C) 2003-2004 Ushodaya Enterprises Limited + * Author: Charles Yates + * + * 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. + * + * 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. + */ + +// Local header files +#include "consumer_avformat.h" + +// mlt Header files +#include + +// System header files +#include +#include +#include +#include + +#include + +// avformat header files +#include + +typedef struct +{ + int16_t *buffer; + int size; + int used; +} +*sample_fifo, sample_fifo_s; + +sample_fifo sample_fifo_init( ) +{ + return calloc( 1, sizeof( sample_fifo_s ) ); +} + +void sample_fifo_append( sample_fifo this, int16_t *samples, int count ) +{ + if ( ( this->size - this->used ) < count ) + { + this->size += count * 5; + this->buffer = realloc( this->buffer, this->size * sizeof( int16_t ) ); + } + + memcpy( &this->buffer[ this->used ], samples, count * sizeof( int16_t ) ); + this->used += count; +} + +int sample_fifo_used( sample_fifo this ) +{ + return this->used; +} + +int sample_fifo_fetch( sample_fifo this, int16_t *samples, int count ) +{ + if ( count > this->used ) + count = this->used; + + memcpy( samples, this->buffer, count * sizeof( int16_t ) ); + this->used -= count; + memmove( this->buffer, &this->buffer[ count ], this->used * sizeof( int16_t ) ); + + return count; +} + +void sample_fifo_close( sample_fifo this ) +{ + free( this->buffer ); + free( this ); +} + +// Forward references. +static int consumer_start( mlt_consumer this ); +static int consumer_stop( mlt_consumer this ); +static int consumer_is_stopped( mlt_consumer this ); +static void *consumer_thread( void *arg ); +static void consumer_close( mlt_consumer this ); + +/** Initialise the dv consumer. +*/ + +mlt_consumer consumer_avformat_init( char *arg ) +{ + // Allocate the consumer + mlt_consumer this = calloc( 1, sizeof( struct mlt_consumer_s ) ); + + // If memory allocated and initialises without error + if ( this != NULL && mlt_consumer_init( this, NULL ) == 0 ) + { + // Get properties from the consumer + mlt_properties properties = mlt_consumer_properties( this ); + + // Assign close callback + this->close = consumer_close; + + // Interpret the argument + if ( arg != NULL ) + 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 + mlt_properties_set_int( properties, "audio_bit_rate", 128000 ); + mlt_properties_set_int( properties, "video_bit_rate", 400000 ); + mlt_properties_set_int( properties, "gop_size", 12 ); + mlt_properties_set_int( properties, "max_b_frames", 0 ); + mlt_properties_set_int( properties, "mb_decision", 0 ); + + // Ensure termination at end of the stream + mlt_properties_set_int( properties, "terminate_on_pause", 1 ); + + // Set up start/stop/terminated callbacks + this->start = consumer_start; + this->stop = consumer_stop; + this->is_stopped = consumer_is_stopped; + } + else + { + // Clean up in case of init failure + free( this ); + this = NULL; + } + + // Return this + return this; +} + +/** Start the consumer. +*/ + +static int consumer_start( mlt_consumer this ) +{ + // Get the properties + mlt_properties properties = mlt_consumer_properties( this ); + + // Check that we're not already running + if ( !mlt_properties_get_int( properties, "running" ) ) + { + // Allocate a thread + pthread_t *thread = calloc( 1, sizeof( pthread_t ) ); + + // Get the width and height + int width = mlt_properties_get_int( properties, "width" ); + int height = mlt_properties_get_int( properties, "height" ); + + // Obtain the size property + char *size = mlt_properties_get( properties, "size" ); + + // Interpret it + if ( size != NULL ) + { + int tw, th; + if ( sscanf( size, "%dx%d", &tw, &th ) == 2 && tw > 0 && th > 0 ) + { + width = tw; + height = th; + } + else + { + fprintf( stderr, "consumer_avformat: Invalid size property %s - ignoring.\n", size ); + } + } + + // Now ensure we honour the multiple of two requested by libavformat + mlt_properties_set_int( properties, "width", ( width / 2 ) * 2 ); + mlt_properties_set_int( properties, "height", ( height / 2 ) * 2 ); + + // Assign the thread to properties + mlt_properties_set_data( properties, "thread", thread, sizeof( pthread_t ), free, NULL ); + + // Set the running state + mlt_properties_set_int( properties, "running", 1 ); + + // Create the thread + pthread_create( thread, NULL, consumer_thread, this ); + } + return 0; +} + +/** Stop the consumer. +*/ + +static int consumer_stop( mlt_consumer this ) +{ + // Get the properties + mlt_properties properties = mlt_consumer_properties( this ); + + // Check that we're running + if ( mlt_properties_get_int( properties, "running" ) ) + { + // Get the thread + pthread_t *thread = mlt_properties_get_data( properties, "thread", NULL ); + + // Stop the thread + mlt_properties_set_int( properties, "running", 0 ); + + // Wait for termination + pthread_join( *thread, NULL ); + } + + return 0; +} + +/** Determine if the consumer is stopped. +*/ + +static int consumer_is_stopped( mlt_consumer this ) +{ + // Get the properties + mlt_properties properties = mlt_consumer_properties( this ); + return !mlt_properties_get_int( properties, "running" ); +} + +/** Add an audio output stream +*/ + +static AVStream *add_audio_stream( mlt_consumer this, AVFormatContext *oc, int codec_id ) +{ + // Get the properties + mlt_properties properties = mlt_consumer_properties( this ); + + // Create a new stream + AVStream *st = av_new_stream( oc, 1 ); + + // If created, then initialise from properties + if ( st != NULL ) + { + AVCodecContext *c = &st->codec; + c->codec_id = codec_id; + c->codec_type = CODEC_TYPE_AUDIO; + + // Put sample parameters + c->bit_rate = mlt_properties_get_int( properties, "audio_bit_rate" ); + c->sample_rate = mlt_properties_get_int( properties, "frequency" ); + c->channels = mlt_properties_get_int( properties, "channels" ); + } + else + { + fprintf( stderr, "Could not allocate a stream for audio\n" ); + } + + return st; +} + +static int open_audio( AVFormatContext *oc, AVStream *st, int audio_outbuf_size ) +{ + // We will return the audio input size from here + int audio_input_frame_size = 0; + + // Get the context + AVCodecContext *c = &st->codec; + + // Find the encoder + AVCodec *codec = avcodec_find_encoder( c->codec_id ); + + // Continue if codec found and we can open it + if ( codec != NULL && avcodec_open(c, codec) >= 0 ) + { + // ugly hack for PCM codecs (will be removed ASAP with new PCM + // support to compute the input frame size in samples + if ( c->frame_size <= 1 ) + { + audio_input_frame_size = audio_outbuf_size / c->channels; + switch(st->codec.codec_id) + { + case CODEC_ID_PCM_S16LE: + case CODEC_ID_PCM_S16BE: + case CODEC_ID_PCM_U16LE: + case CODEC_ID_PCM_U16BE: + audio_input_frame_size >>= 1; + break; + default: + break; + } + } + else + { + audio_input_frame_size = c->frame_size; + } + } + else + { + fprintf( stderr, "Unable to encode audio - disabling audio output.\n" ); + } + + return audio_input_frame_size; +} + +static void close_audio( AVFormatContext *oc, AVStream *st ) +{ + avcodec_close( &st->codec ); +} + +/** Add a video output stream +*/ + +static AVStream *add_video_stream( mlt_consumer this, AVFormatContext *oc, int codec_id ) +{ + // Get the properties + mlt_properties properties = mlt_consumer_properties( this ); + + // Create a new stream + AVStream *st = av_new_stream( oc, 0 ); + + if ( st != NULL ) + { + AVCodecContext *c = &st->codec; + c->codec_id = codec_id; + c->codec_type = CODEC_TYPE_VIDEO; + + // put sample parameters + c->bit_rate = mlt_properties_get_int( properties, "video_bit_rate" ); + c->width = mlt_properties_get_int( properties, "width" ); + c->height = mlt_properties_get_int( properties, "height" ); + c->frame_rate = mlt_properties_get_double( properties, "fps" ); + c->frame_rate_base = 1; + c->gop_size = mlt_properties_get_int( properties, "gop_size" ); + c->max_b_frames = mlt_properties_get_int( properties, "max_b_frames" ); + c->mb_decision = mlt_properties_get_int( properties, "mb_decision" ); + + // Some formats want stream headers to be seperate (hmm) + if( !strcmp( oc->oformat->name, "mp4" ) || + !strcmp( oc->oformat->name, "mov" ) || + !strcmp( oc->oformat->name, "3gp" ) ) + c->flags |= CODEC_FLAG_GLOBAL_HEADER; + } + else + { + fprintf( stderr, "Could not allocate a stream for video\n" ); + } + + return st; +} + +static AVFrame *alloc_picture( int pix_fmt, int width, int height ) +{ + // Allocate a frame + AVFrame *picture = avcodec_alloc_frame(); + + // Determine size of the + int size = avpicture_get_size(pix_fmt, width, height); + + // Allocate the picture buf + uint8_t *picture_buf = av_malloc(size); + + // If we have both, then fill the image + if ( picture != NULL && picture_buf != NULL ) + { + // Fill the frame with the allocated buffer + avpicture_fill((AVPicture *)picture, picture_buf, pix_fmt, width, height); + } + else + { + // Something failed - clean up what we can + av_free(picture); + av_free(picture_buf); + picture = NULL; + } + + return picture; +} + +static int open_video(AVFormatContext *oc, AVStream *st) +{ + // Get the codec + AVCodecContext *c = &st->codec; + + // find the video encoder + AVCodec *codec = avcodec_find_encoder(c->codec_id); + + // Open the codec safely + return codec != NULL && avcodec_open(c, codec) >= 0; +} + +void close_video(AVFormatContext *oc, AVStream *st) +{ + avcodec_close(&st->codec); +} + +/** The main thread - the argument is simply the consumer. +*/ + +static void *consumer_thread( void *arg ) +{ + // Map the argument to the object + mlt_consumer this = arg; + + // Get the properties + mlt_properties properties = mlt_consumer_properties( this ); + + // Get the terminate on pause property + int terminate_on_pause = mlt_properties_get_int( properties, "terminate_on_pause" ); + + // Get the frame rate + int fps = mlt_properties_get_double( properties, "fps" ); + + // Get width and height + int width = mlt_properties_get_int( properties, "width" ); + int height = mlt_properties_get_int( properties, "height" ); + int img_width = width; + int img_height = height; + + // Get default audio properties + mlt_audio_format aud_fmt = mlt_audio_pcm; + int channels = mlt_properties_get_int( properties, "channels" ); + int frequency = mlt_properties_get_int( properties, "frequency" ); + int16_t *pcm = NULL; + int samples = 0; + + // AVFormat audio buffer and frame size + int audio_outbuf_size = 10000; + uint8_t *audio_outbuf = av_malloc( audio_outbuf_size ); + int audio_input_frame_size = 0; + + // AVFormat video buffer and frame count + int frame_count = 0; + int video_outbuf_size = 200000; + uint8_t *video_outbuf = av_malloc(video_outbuf_size); + + // Used for the frame properties + mlt_frame frame = NULL; + mlt_properties frame_properties = NULL; + + // Get the queues + mlt_deque queue = mlt_properties_get_data( properties, "frame_queue", NULL ); + sample_fifo fifo = mlt_properties_get_data( properties, "sample_fifo", NULL ); + + // Need two av pictures for converting + AVFrame *output = alloc_picture( PIX_FMT_YUV420P, width, height ); + AVFrame *input = alloc_picture( PIX_FMT_YUV422, width, height ); + + // For receiving images from an mlt_frame + uint8_t *image; + mlt_image_format img_fmt = mlt_image_yuv422; + + // Fo receiving audio samples back from the fifo + int16_t buffer[ 48000 * 2 ]; + int count = 0; + + // Allocate the context + AVFormatContext *oc = av_alloc_format_context(); + + // Streams + AVStream *audio_st = NULL; + AVStream *video_st = NULL; + + // Time stamps + double audio_pts, video_pts; + + // Loop variable + int i; + + // Determine the format + AVOutputFormat *fmt = NULL; + char *filename = mlt_properties_get( properties, "target" ); + char *format = mlt_properties_get( properties, "format" ); + //char *vcodec = mlt_properties_get( properties, "vcodec" ); + //char *acodec = mlt_properties_get( properties, "acodec" ); + + // Check for user selected format first + if ( format != NULL ) + fmt = guess_format( format, NULL, NULL ); + + // Otherwise check on the filename + if ( fmt == NULL && filename != NULL ) + fmt = guess_format( NULL, filename, NULL ); + + // Otherwise default to mpeg + if ( fmt == NULL ) + fmt = guess_format( "mpeg", NULL, NULL ); + + // We need a filename - default to stdout? + if ( filename == NULL ) + filename = "pipe:"; + + // Update the output context + oc->oformat = fmt; + snprintf( oc->filename, sizeof(oc->filename), "%s", filename ); + + // Add audio and video streams + if ( fmt->video_codec != CODEC_ID_NONE ) + video_st = add_video_stream( this, oc, fmt->video_codec ); + if ( fmt->audio_codec != CODEC_ID_NONE ) + audio_st = add_audio_stream( this, oc, fmt->audio_codec ); + + // Set the parameters (even though we have none...) + if ( av_set_parameters(oc, NULL) >= 0 ) + { + if ( video_st && !open_video( oc, video_st ) ) + video_st = NULL; + if ( audio_st ) + audio_input_frame_size = open_audio( oc, audio_st, audio_outbuf_size ); + + // Open the output file, if needed + if ( !( fmt->flags & AVFMT_NOFILE ) ) + { + if (url_fopen(&oc->pb, filename, URL_WRONLY) < 0) + { + fprintf(stderr, "Could not open '%s'\n", filename); + mlt_properties_set_int( properties, "running", 0 ); + } + } + + // Write the stream header, if any + if ( mlt_properties_get_int( properties, "running" ) ) + av_write_header( oc ); + } + else + { + fprintf(stderr, "Invalid output format parameters\n"); + mlt_properties_set_int( properties, "running", 0 ); + } + + // Last check - need at least one stream + if ( audio_st == NULL && video_st == NULL ) + mlt_properties_set_int( properties, "running", 0 ); + + // Loop while running + while( mlt_properties_get_int( properties, "running" ) ) + { + // Get the frame + frame = mlt_consumer_rt_frame( this ); + + // Check that we have a frame to work with + if ( frame != NULL ) + { + // Default audio args + frame_properties = mlt_frame_properties( frame ); + + // Get audio and append to the fifo + if ( audio_st ) + { + samples = mlt_sample_calculator( fps, frequency, count ); + mlt_frame_get_audio( frame, &pcm, &aud_fmt, &frequency, &channels, &samples ); + sample_fifo_append( fifo, pcm, samples * channels ); + } + + // Encode the image + if ( video_st ) + mlt_deque_push_back( queue, frame ); + else + mlt_frame_close( frame ); + + // While we have stuff to process, process... + while ( 1 ) + { + // Compute current audio and video time + if (audio_st) + audio_pts = (double)audio_st->pts.val * oc->pts_num / oc->pts_den; + else + audio_pts = 0.0; + + if (video_st) + video_pts = (double)video_st->pts.val * oc->pts_num / oc->pts_den; + else + video_pts = 0.0; + + // Write interleaved audio and video frames + if ( !video_st || ( video_st && audio_st && audio_pts < video_pts ) ) + { + if ( channels * audio_input_frame_size < sample_fifo_used( fifo ) ) + { + int out_size; + AVCodecContext *c; + + 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 ); + + // Write the compressed frame in the media file + if (av_write_frame(oc, audio_st->index, audio_outbuf, out_size) != 0) + fprintf(stderr, "Error while writing audio frame\n"); + } + else + { + break; + } + } + else if ( video_st ) + { + if ( mlt_deque_count( queue ) ) + { + int out_size, ret; + AVCodecContext *c; + + frame = mlt_deque_pop_front( queue ); + frame_properties = mlt_frame_properties( frame ); + + if ( terminate_on_pause && mlt_properties_get_double( frame_properties, "_speed" ) == 0.0 ) + { + mlt_properties_set_int( properties, "running", 0 ); + break; + } + + c = &video_st->codec; + + if ( mlt_properties_get_int( frame_properties, "rendered" ) ) + { + int i = 0; + int j = 0; + uint8_t *p; + uint8_t *q; + + mlt_frame_get_image( frame, &image, &img_fmt, &img_width, &img_height, 0 ); + + q = image; + + for ( i = 0; i < height; i ++ ) + { + p = input->data[ 0 ] + i * input->linesize[ 0 ]; + j = width; + while( j -- ) + { + *p ++ = *q ++; + *p ++ = *q ++; + } + } + + img_convert( ( AVPicture * )output, PIX_FMT_YUV420P, ( AVPicture * )input, PIX_FMT_YUV422, width, height ); + } + + 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)); + } + else + { + // Encode the image + out_size = avcodec_encode_video(c, video_outbuf, video_outbuf_size, output ); + + // 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 ); + } + } + frame_count++; + mlt_frame_close( frame ); + } + else + { + break; + } + } + } + } + } + + // close each codec + if (video_st) + close_video(oc, video_st); + if (audio_st) + close_audio(oc, audio_st); + + // Write the trailer, if any + av_write_trailer(oc); + + // Free the streams + for(i = 0; i < oc->nb_streams; i++) + av_freep(&oc->streams[i]); + + // Close the output file + if (!(fmt->flags & AVFMT_NOFILE)) + url_fclose(&oc->pb); + + // Clean up input and output frames + av_free( output->data[0] ); + av_free( output ); + av_free( input->data[0] ); + av_free( input ); + av_free( video_outbuf ); + + // Free the stream + av_free(oc); + + return NULL; +} + +/** Close the consumer. +*/ + +static void consumer_close( mlt_consumer this ) +{ + // Stop the consumer + mlt_consumer_stop( this ); + + // Close the parent + mlt_consumer_close( this ); + + // Free the memory + free( this ); +} + diff --git a/src/modules/avformat/consumer_avformat.h b/src/modules/avformat/consumer_avformat.h new file mode 100644 index 00000000..ee1b682b --- /dev/null +++ b/src/modules/avformat/consumer_avformat.h @@ -0,0 +1,28 @@ +/* + * consumer_avformat.h -- avformat consumer + * Copyright (C) 2003-2004 Ushodaya Enterprises Limited + * Author: Charles Yates + * + * 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. + * + * 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. + */ + +#ifndef _CONSUMER_AVFORMAT_H_ +#define _CONSUMER_AVFORMAT_H_ + +#include + +extern mlt_consumer consumer_avformat_init( char *file ); + +#endif diff --git a/src/modules/avformat/factory.c b/src/modules/avformat/factory.c index 4d984ba1..bf01e958 100644 --- a/src/modules/avformat/factory.c +++ b/src/modules/avformat/factory.c @@ -19,11 +19,77 @@ */ #include +#include +#include #include "producer_avformat.h" +#include "consumer_avformat.h" + +// ffmpeg Header files +#include + +// A static flag used to determine if avformat has been initialised +static int avformat_initialised = 0; + +// A locking mutex +static pthread_mutex_t avformat_mutex; + +#if 0 +// These 3 functions should override the alloc functions in libavformat +// but some formats or codecs seem to crash when used (wmv in particular) + +void *av_malloc( unsigned int size ) +{ + return mlt_pool_alloc( size ); +} + +void *av_realloc( void *ptr, unsigned int size ) +{ + return mlt_pool_realloc( ptr, size ); +} + +void av_free( void *ptr ) +{ + return mlt_pool_release( ptr ); +} +#endif + +void avformat_destroy( void *ignore ) +{ + // Clean up + av_free_static( ); + + // Destroy the mutex + pthread_mutex_destroy( &avformat_mutex ); +} + +void avformat_lock( ) +{ + // Lock the mutex now + pthread_mutex_lock( &avformat_mutex ); +} + +void avformat_unlock( ) +{ + // Unlock the mutex now + pthread_mutex_unlock( &avformat_mutex ); +} + +static void avformat_init( ) +{ + // Initialise avformat if necessary + if ( avformat_initialised == 0 ) + { + avformat_initialised = 1; + pthread_mutex_init( &avformat_mutex, NULL ); + av_register_all( ); + mlt_factory_register_for_clean_up( NULL, avformat_destroy ); + } +} void *mlt_create_producer( char *id, void *arg ) { + avformat_init( ); if ( !strcmp( id, "avformat" ) ) return producer_avformat_init( arg ); return NULL; @@ -41,6 +107,9 @@ void *mlt_create_transition( char *id, void *arg ) void *mlt_create_consumer( char *id, void *arg ) { + avformat_init( ); + if ( !strcmp( id, "avformat" ) ) + return consumer_avformat_init( arg ); return NULL; } diff --git a/src/modules/avformat/producer_avformat.c b/src/modules/avformat/producer_avformat.c index 0f3a5597..582171ea 100644 --- a/src/modules/avformat/producer_avformat.c +++ b/src/modules/avformat/producer_avformat.c @@ -33,31 +33,13 @@ #include #include +void avformat_lock( ); +void avformat_unlock( ); + // Forward references. static int producer_open( mlt_producer this, char *file ); static int producer_get_frame( mlt_producer this, mlt_frame_ptr frame, int index ); -// A static flag used to determine if avformat has been initialised -static int avformat_initialised = 0; -static pthread_mutex_t avformat_mutex; - -#if 0 -void *av_malloc( unsigned int size ) -{ - return mlt_pool_alloc( size ); -} - -void *av_realloc( void *ptr, unsigned int size ) -{ - return mlt_pool_realloc( ptr, size ); -} - -void av_free( void *ptr ) -{ - return mlt_pool_release( ptr ); -} -#endif - /** Constructor for libavformat. */ @@ -86,14 +68,6 @@ mlt_producer producer_avformat_init( char *file ) // Register our get_frame implementation this->get_frame = producer_get_frame; - // Initialise avformat if necessary - if ( avformat_initialised == 0 ) - { - avformat_initialised = 1; - pthread_mutex_init( &avformat_mutex, NULL ); - av_register_all( ); - } - // Open the file if ( producer_open( this, file ) != 0 ) { @@ -145,13 +119,13 @@ static void producer_file_close( void *context ) if ( context != NULL ) { // Lock the mutex now - pthread_mutex_lock( &avformat_mutex ); + avformat_lock( ); // Close the file av_close_input_file( context ); // Unlock the mutex now - pthread_mutex_unlock( &avformat_mutex ); + avformat_unlock( ); } } @@ -163,13 +137,13 @@ static void producer_codec_close( void *codec ) if ( codec != NULL ) { // Lock the mutex now - pthread_mutex_lock( &avformat_mutex ); + avformat_lock( ); // Close the file avcodec_close( codec ); // Unlock the mutex now - pthread_mutex_unlock( &avformat_mutex ); + avformat_unlock( ); } } @@ -191,7 +165,7 @@ static int producer_open( mlt_producer this, char *file ) double fps = mlt_properties_get_double( properties, "fps" ); // Lock the mutex now - pthread_mutex_lock( &avformat_mutex ); + avformat_lock( ); // If "MRL", then create AVInputFormat AVInputFormat *format = NULL; @@ -332,7 +306,7 @@ static int producer_open( mlt_producer this, char *file ) } // Unlock the mutex now - pthread_mutex_unlock( &avformat_mutex ); + avformat_unlock( ); return error; } @@ -415,16 +389,14 @@ static int producer_get_image( mlt_frame frame, uint8_t **buffer, mlt_image_form mlt_properties_set_int( frame_properties, "height", *height ); // Lock the mutex now - pthread_mutex_lock( &avformat_mutex ); + avformat_lock( ); // Construct an AVFrame for YUV422 conversion if ( output == NULL ) { - int size = avpicture_get_size( PIX_FMT_YUV422, *width, *height ); - size += *width * 2; + int size = avpicture_get_size( PIX_FMT_YUV422, *width, *height + 1 ); uint8_t *buf = mlt_pool_alloc( size ); output = mlt_pool_alloc( sizeof( AVPicture ) ); - //memset( output, 0, sizeof( AVPicture ) ); avpicture_fill( output, buf, PIX_FMT_YUV422, *width, *height ); mlt_properties_set_data( properties, "video_output_frame", output, 0, ( mlt_destructor )mlt_pool_release, NULL ); mlt_properties_set_data( properties, "video_output_buffer", buf, 0, ( mlt_destructor )mlt_pool_release, NULL ); @@ -604,7 +576,7 @@ static int producer_get_image( mlt_frame frame, uint8_t **buffer, mlt_image_form mlt_properties_set_position( properties, "video_expected", position + 1 ); // Unlock the mutex now - pthread_mutex_unlock( &avformat_mutex ); + avformat_unlock( ); return 0; } @@ -627,7 +599,7 @@ static void producer_set_up_video( mlt_producer this, mlt_frame frame ) mlt_properties frame_properties = mlt_frame_properties( frame ); // Lock the mutex now - pthread_mutex_lock( &avformat_mutex ); + avformat_lock( ); if ( context != NULL && index != -1 ) { @@ -697,7 +669,7 @@ static void producer_set_up_video( mlt_producer this, mlt_frame frame ) } // Unlock the mutex now - pthread_mutex_unlock( &avformat_mutex ); + avformat_unlock( ); } /** Get the audio from a frame. @@ -755,7 +727,7 @@ static int producer_get_audio( mlt_frame frame, int16_t **buffer, mlt_audio_form int locked = 0; // Lock the mutex now - pthread_mutex_lock( &avformat_mutex ); + avformat_lock( ); // Check for resample and create if necessary if ( resample == NULL && codec_context->channels <= 2 ) @@ -928,7 +900,7 @@ static int producer_get_audio( mlt_frame frame, int16_t **buffer, mlt_audio_form mlt_properties_set_position( properties, "audio_expected", position + 1 ); // Unlock the mutex now - pthread_mutex_unlock( &avformat_mutex ); + avformat_unlock( ); return 0; } @@ -948,7 +920,7 @@ static void producer_set_up_audio( mlt_producer this, mlt_frame frame ) int index = mlt_properties_get_int( properties, "audio_index" ); // Lock the mutex now - pthread_mutex_lock( &avformat_mutex ); + avformat_lock( ); // Deal with audio context if ( context != NULL && index != -1 ) @@ -994,7 +966,7 @@ static void producer_set_up_audio( mlt_producer this, mlt_frame frame ) } // Unlock the mutex now - pthread_mutex_unlock( &avformat_mutex ); + avformat_unlock( ); } /** Our get frame implementation. diff --git a/src/modules/dv/consumer_libdv.c b/src/modules/dv/consumer_libdv.c index b7714920..af257959 100644 --- a/src/modules/dv/consumer_libdv.c +++ b/src/modules/dv/consumer_libdv.c @@ -285,7 +285,7 @@ static void consumer_encode_audio( mlt_consumer this, uint8_t *dv_frame, mlt_fra int i = 0; int j = 0; for ( i = 0 ; i < 4; i ++ ) - audio_buffers[ i ] = calloc( 1, 2 * DV_AUDIO_MAX_SAMPLES ); + audio_buffers[ i ] = mlt_pool_alloc( 2 * DV_AUDIO_MAX_SAMPLES ); // Get the audio mlt_frame_get_audio( frame, &pcm, &fmt, &frequency, &channels, &samples ); @@ -313,7 +313,7 @@ static void consumer_encode_audio( mlt_consumer this, uint8_t *dv_frame, mlt_fra // Temporary - free audio buffers for ( i = 0 ; i < 4; i ++ ) - free( audio_buffers[ i ] ); + mlt_pool_release( audio_buffers[ i ] ); } } @@ -367,7 +367,7 @@ static void *consumer_thread( void *arg ) int ( *output )( mlt_consumer, uint8_t *, int, mlt_frame ) = mlt_properties_get_data( properties, "output", NULL ); // Allocate a single PAL frame for encoding - uint8_t *dv_frame = malloc( frame_size_625_50 ); + uint8_t *dv_frame = mlt_pool_alloc( frame_size_625_50 ); // Frame and size mlt_frame frame = NULL; @@ -406,7 +406,7 @@ static void *consumer_thread( void *arg ) } // Tidy up - free( dv_frame ); + mlt_pool_release( dv_frame ); return NULL; } diff --git a/src/modules/dv/producer_libdv.c b/src/modules/dv/producer_libdv.c index 9ed89780..f356ffae 100644 --- a/src/modules/dv/producer_libdv.c +++ b/src/modules/dv/producer_libdv.c @@ -81,10 +81,8 @@ dv_decoder_t *dv_decoder_alloc( ) dv_set_audio_correction( this, DV_AUDIO_CORRECT_AVERAGE ); // Register it with the properties to ensure clean up - // BUG: dv_decoder_free core dumps here sprintf( label, "%p", this ); - //mlt_properties_set_data( dv_decoders, label, this, 0, ( mlt_destructor )dv_decoder_free, NULL ); - mlt_properties_set_data( dv_decoders, label, this, 0, NULL, NULL ); + mlt_properties_set_data( dv_decoders, label, this, 0, ( mlt_destructor )dv_decoder_free, NULL ); } } diff --git a/src/modules/sdl/consumer_sdl.c b/src/modules/sdl/consumer_sdl.c index 7d867df9..238bf2a7 100644 --- a/src/modules/sdl/consumer_sdl.c +++ b/src/modules/sdl/consumer_sdl.c @@ -246,11 +246,9 @@ static int consumer_play_audio( consumer_sdl this, mlt_frame frame, int init_aud mlt_audio_format afmt = mlt_audio_pcm; // Set the preferred params of the test card signal - int channels = 2; - int frequency = 48000; + int channels = mlt_properties_get_int( properties, "channels" ); + int frequency = mlt_properties_get_int( properties, "frequency" ); static int counter = 0; - if ( mlt_properties_get_int( properties, "frequency" ) != 0 ) - frequency = mlt_properties_get_int( properties, "frequency" ); int samples = mlt_sample_calculator( mlt_properties_get_double( this->properties, "fps" ), frequency, counter++ ); -- 2.39.2