2 * producer_avformat.c -- avformat producer
3 * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4 * Author: Charles Yates <charles.yates@pandora.be>
5 * Much code borrowed from ffmpeg.c: Copyright (c) 2000-2003 Fabrice Bellard
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <framework/mlt_producer.h>
24 #include <framework/mlt_frame.h>
26 // ffmpeg Header files
32 #if (LIBAVCODEC_VERSION_INT >= ((51<<16)+(71<<8)+0))
33 # include "audioconvert.h"
36 // System header files
42 void avformat_lock( );
43 void avformat_unlock( );
45 // Forward references.
46 static int producer_open( mlt_producer
this, mlt_profile profile
, char *file
);
47 static int producer_get_frame( mlt_producer
this, mlt_frame_ptr frame
, int index
);
49 /** Constructor for libavformat.
52 mlt_producer
producer_avformat_init( mlt_profile profile
, char *file
)
54 mlt_producer
this = NULL
;
56 // Check that we have a non-NULL argument
59 // Construct the producer
60 this = calloc( 1, sizeof( struct mlt_producer_s
) );
63 if ( mlt_producer_init( this, NULL
) == 0 )
66 mlt_properties properties
= MLT_PRODUCER_PROPERTIES( this );
68 // Set the resource property (required for all producers)
69 mlt_properties_set( properties
, "resource", file
);
71 // Register our get_frame implementation
72 this->get_frame
= producer_get_frame
;
75 if ( producer_open( this, profile
, file
) != 0 )
78 mlt_producer_close( this );
83 // Close the file to release resources for large playlists - reopen later as needed
84 mlt_properties_set_data( properties
, "dummy_context", NULL
, 0, NULL
, NULL
);
85 mlt_properties_set_data( properties
, "audio_context", NULL
, 0, NULL
, NULL
);
86 mlt_properties_set_data( properties
, "video_context", NULL
, 0, NULL
, NULL
);
88 // Default the user-selectable indices from the auto-detected indices
89 mlt_properties_set_int( properties
, "audio_index", mlt_properties_get_int( properties
, "_audio_index" ) );
90 mlt_properties_set_int( properties
, "video_index", mlt_properties_get_int( properties
, "_video_index" ) );
98 /** Find the default streams.
101 static mlt_properties
find_default_streams( mlt_properties meta_media
, AVFormatContext
*context
, int *audio_index
, int *video_index
)
106 mlt_properties_set_int( meta_media
, "meta.media.nb_streams", context
->nb_streams
);
108 // Allow for multiple audio and video streams in the file and select first of each (if available)
109 for( i
= 0; i
< context
->nb_streams
; i
++ )
111 // Get the codec context
112 AVStream
*stream
= context
->streams
[ i
];
113 if ( ! stream
) continue;
114 AVCodecContext
*codec_context
= stream
->codec
;
115 if ( ! codec_context
) continue;
116 AVCodec
*codec
= avcodec_find_decoder( codec_context
->codec_id
);
117 if ( ! codec
) continue;
119 snprintf( key
, sizeof(key
), "meta.media.%d.stream.type", i
);
121 // Determine the type and obtain the first index of each type
122 switch( codec_context
->codec_type
)
124 case CODEC_TYPE_VIDEO
:
125 if ( *video_index
< 0 )
127 mlt_properties_set( meta_media
, key
, "video" );
128 snprintf( key
, sizeof(key
), "meta.media.%d.stream.frame_rate", i
);
129 mlt_properties_set_double( meta_media
, key
, av_q2d( context
->streams
[ i
]->r_frame_rate
) );
130 snprintf( key
, sizeof(key
), "meta.media.%d.stream.sample_aspect_ratio", i
);
131 mlt_properties_set_double( meta_media
, key
, av_q2d( context
->streams
[ i
]->sample_aspect_ratio
) );
132 snprintf( key
, sizeof(key
), "meta.media.%d.codec.pix_fmt", i
);
133 mlt_properties_set( meta_media
, key
, avcodec_get_pix_fmt_name( codec_context
->pix_fmt
) );
134 snprintf( key
, sizeof(key
), "meta.media.%d.codec.sample_aspect_ratio", i
);
135 mlt_properties_set_double( meta_media
, key
, av_q2d( codec_context
->sample_aspect_ratio
) );
137 case CODEC_TYPE_AUDIO
:
138 if ( *audio_index
< 0 )
140 mlt_properties_set( meta_media
, key
, "audio" );
141 snprintf( key
, sizeof(key
), "meta.media.%d.codec.sample_fmt", i
);
142 mlt_properties_set( meta_media
, key
, avcodec_get_sample_fmt_name( codec_context
->sample_fmt
) );
143 snprintf( key
, sizeof(key
), "meta.media.%d.codec.sample_rate", i
);
144 mlt_properties_set_int( meta_media
, key
, codec_context
->sample_rate
);
145 snprintf( key
, sizeof(key
), "meta.media.%d.codec.channels", i
);
146 mlt_properties_set_int( meta_media
, key
, codec_context
->channels
);
151 // snprintf( key, sizeof(key), "meta.media.%d.stream.time_base", i );
152 // mlt_properties_set_double( meta_media, key, av_q2d( context->streams[ i ]->time_base ) );
153 snprintf( key
, sizeof(key
), "meta.media.%d.codec.name", i
);
154 mlt_properties_set( meta_media
, key
, codec
->name
);
155 snprintf( key
, sizeof(key
), "meta.media.%d.codec.long_name", i
);
156 mlt_properties_set( meta_media
, key
, codec
->long_name
);
157 snprintf( key
, sizeof(key
), "meta.media.%d.codec.bit_rate", i
);
158 mlt_properties_set_int( meta_media
, key
, codec_context
->bit_rate
);
159 // snprintf( key, sizeof(key), "meta.media.%d.codec.time_base", i );
160 // mlt_properties_set_double( meta_media, key, av_q2d( codec_context->time_base ) );
161 snprintf( key
, sizeof(key
), "meta.media.%d.codec.profile", i
);
162 mlt_properties_set_int( meta_media
, key
, codec_context
->profile
);
163 snprintf( key
, sizeof(key
), "meta.media.%d.codec.level", i
);
164 mlt_properties_set_int( meta_media
, key
, codec_context
->level
);
170 /** Producer file destructor.
173 static void producer_file_close( void *context
)
175 if ( context
!= NULL
)
177 // Lock the mutex now
181 av_close_input_file( context
);
183 // Unlock the mutex now
188 /** Producer file destructor.
191 static void producer_codec_close( void *codec
)
195 // Lock the mutex now
199 avcodec_close( codec
);
201 // Unlock the mutex now
206 static inline int dv_is_pal( AVPacket
*pkt
)
208 return pkt
->data
[3] & 0x80;
211 static int dv_is_wide( AVPacket
*pkt
)
213 int i
= 80 /* block size */ *3 /* VAUX starts at block 3 */ +3 /* skip block header */;
215 for ( ; i
< pkt
->size
; i
+= 5 /* packet size */ )
217 if ( pkt
->data
[ i
] == 0x61 )
219 uint8_t x
= pkt
->data
[ i
+ 2 ] & 0x7;
220 return ( x
== 2 ) || ( x
== 7 );
226 static double get_aspect_ratio( AVStream
*stream
, AVCodecContext
*codec_context
, AVPacket
*pkt
)
228 double aspect_ratio
= 1.0;
230 if ( codec_context
->codec_id
== CODEC_ID_DVVIDEO
)
234 if ( dv_is_pal( pkt
) )
236 aspect_ratio
= dv_is_wide( pkt
)
237 ?
64.0/45.0 // 16:9 PAL
238 : 16.0/15.0; // 4:3 PAL
242 aspect_ratio
= dv_is_wide( pkt
)
243 ?
32.0/27.0 // 16:9 NTSC
244 : 8.0/9.0; // 4:3 NTSC
250 #if LIBAVFORMAT_VERSION_INT >= ((52<<16)+(21<<8)+0)
251 stream
->sample_aspect_ratio
;
253 codec_context
->sample_aspect_ratio
;
255 // Override FFmpeg's notion of DV aspect ratios, which are
256 // based upon a width of 704. Since we do not have a normaliser
257 // that crops (nor is cropping 720 wide ITU-R 601 video always desirable)
258 // we just coerce the values to facilitate a passive behaviour through
259 // the rescale normaliser when using equivalent producers and consumers.
260 // = display_aspect / (width * height)
261 if ( ar
.num
== 10 && ar
.den
== 11 )
262 aspect_ratio
= 8.0/9.0; // 4:3 NTSC
263 else if ( ar
.num
== 59 && ar
.den
== 54 )
264 aspect_ratio
= 16.0/15.0; // 4:3 PAL
265 else if ( ar
.num
== 40 && ar
.den
== 33 )
266 aspect_ratio
= 32.0/27.0; // 16:9 NTSC
267 else if ( ar
.num
== 118 && ar
.den
== 81 )
268 aspect_ratio
= 64.0/45.0; // 16:9 PAL
273 AVRational codec_sar
= codec_context
->sample_aspect_ratio
;
274 AVRational stream_sar
=
275 #if LIBAVFORMAT_VERSION_INT >= ((52<<16)+(21<<8)+0)
276 stream
->sample_aspect_ratio
;
280 if ( codec_sar
.num
> 0 )
281 aspect_ratio
= av_q2d( codec_sar
);
282 else if ( stream_sar
.num
> 0 )
283 aspect_ratio
= av_q2d( stream_sar
);
291 static int producer_open( mlt_producer
this, mlt_profile profile
, char *file
)
293 // Return an error code (0 == no error)
296 // Context for avformat
297 AVFormatContext
*context
= NULL
;
299 // Get the properties
300 mlt_properties properties
= MLT_PRODUCER_PROPERTIES( this );
302 // We will treat everything with the producer fps
303 double fps
= mlt_profile_fps( profile
);
305 // Lock the mutex now
308 // If "MRL", then create AVInputFormat
309 AVInputFormat
*format
= NULL
;
310 AVFormatParameters
*params
= NULL
;
311 char *standard
= NULL
;
312 char *mrl
= strchr( file
, ':' );
314 // AV option (0 = both, 1 = video, 2 = audio)
317 // Setting lowest log level
318 av_log_set_level( -1 );
320 // Only if there is not a protocol specification that avformat can handle
321 if ( mrl
&& !url_exist( file
) )
323 // 'file' becomes format abbreviation
327 format
= av_find_input_format( file
);
329 // Eat the format designator
335 params
= calloc( sizeof( AVFormatParameters
), 1 );
337 // These are required by video4linux (defaults)
339 params
->height
= 480;
340 params
->time_base
= (AVRational
){1,25};
341 // params->device = file;
342 params
->channels
= 2;
343 params
->sample_rate
= 48000;
346 // XXX: this does not work anymore since avdevice
347 // TODO: make producer_avddevice?
349 mrl
= strchr( file
, '?' );
353 char *name
= strdup( ++mrl
);
354 char *value
= strchr( name
, ':' );
359 char *t
= strchr( value
, '&' );
362 if ( !strcmp( name
, "frame_rate" ) )
363 params
->time_base
.den
= atoi( value
);
364 else if ( !strcmp( name
, "frame_rate_base" ) )
365 params
->time_base
.num
= atoi( value
);
366 else if ( !strcmp( name
, "sample_rate" ) )
367 params
->sample_rate
= atoi( value
);
368 else if ( !strcmp( name
, "channels" ) )
369 params
->channels
= atoi( value
);
370 else if ( !strcmp( name
, "width" ) )
371 params
->width
= atoi( value
);
372 else if ( !strcmp( name
, "height" ) )
373 params
->height
= atoi( value
);
374 else if ( !strcmp( name
, "standard" ) )
376 standard
= strdup( value
);
377 params
->standard
= standard
;
379 else if ( !strcmp( name
, "av" ) )
383 mrl
= strchr( mrl
, '&' );
387 // Now attempt to open the file
388 error
= av_open_input_file( &context
, file
, format
, 0, params
) < 0;
390 // Cleanup AVFormatParameters
394 // If successful, then try to get additional info
397 // Get the stream info
398 error
= av_find_stream_info( context
) < 0;
400 // Continue if no error
403 // We will default to the first audio and video streams found
404 int audio_index
= -1;
405 int video_index
= -1;
408 // Now set properties where we can (use default unknowns if required)
409 if ( context
->duration
!= AV_NOPTS_VALUE
)
411 // This isn't going to be accurate for all formats
412 mlt_position frames
= ( mlt_position
)( ( ( double )context
->duration
/ ( double )AV_TIME_BASE
) * fps
+ 0.5 );
413 mlt_properties_set_position( properties
, "out", frames
- 1 );
414 mlt_properties_set_position( properties
, "length", frames
);
417 // Find default audio and video streams
418 find_default_streams( properties
, context
, &audio_index
, &video_index
);
420 if ( context
->start_time
!= AV_NOPTS_VALUE
)
421 mlt_properties_set_double( properties
, "_start_time", context
->start_time
);
423 // Check if we're seekable (something funny about mpeg here :-/)
424 if ( strcmp( file
, "pipe:" ) && strncmp( file
, "http://", 6 ) && strncmp( file
, "udp:", 4 ) && strncmp( file
, "tcp:", 4 ) && strncmp( file
, "rtsp:", 5 ) && strncmp( file
, "rtp:", 4 ) )
426 mlt_properties_set_int( properties
, "seekable", av_seek_frame( context
, -1, mlt_properties_get_double( properties
, "_start_time" ), AVSEEK_FLAG_BACKWARD
) >= 0 );
427 mlt_properties_set_data( properties
, "dummy_context", context
, 0, producer_file_close
, NULL
);
428 av_open_input_file( &context
, file
, NULL
, 0, NULL
);
429 av_find_stream_info( context
);
434 // Store selected audio and video indexes on properties
435 mlt_properties_set_int( properties
, "_audio_index", audio_index
);
436 mlt_properties_set_int( properties
, "_video_index", video_index
);
437 mlt_properties_set_int( properties
, "_last_position", -1 );
439 // Fetch the width, height and aspect ratio
440 if ( video_index
!= -1 )
442 AVCodecContext
*codec_context
= context
->streams
[ video_index
]->codec
;
443 mlt_properties_set_int( properties
, "width", codec_context
->width
);
444 mlt_properties_set_int( properties
, "height", codec_context
->height
);
446 if ( codec_context
->codec_id
== CODEC_ID_DVVIDEO
)
448 // Fetch the first frame of DV so we can read it directly
453 ret
= av_read_frame( context
, &pkt
);
454 if ( ret
>= 0 && pkt
.stream_index
== video_index
&& pkt
.size
> 0 )
456 mlt_properties_set_double( properties
, "aspect_ratio",
457 get_aspect_ratio( context
->streams
[ video_index
], codec_context
, &pkt
) );
464 mlt_properties_set_double( properties
, "aspect_ratio",
465 get_aspect_ratio( context
->streams
[ video_index
], codec_context
, NULL
) );
470 if (context
->title
!= NULL
)
471 mlt_properties_set(properties
, "meta.attr.title.markup", context
->title
);
472 if (context
->author
!= NULL
)
473 mlt_properties_set(properties
, "meta.attr.author.markup", context
->author
);
474 if (context
->copyright
!= NULL
)
475 mlt_properties_set(properties
, "meta.attr.copyright.markup", context
->copyright
);
476 if (context
->comment
!= NULL
)
477 mlt_properties_set(properties
, "meta.attr.comment.markup", context
->comment
);
478 if (context
->album
!= NULL
)
479 mlt_properties_set(properties
, "meta.attr.album.markup", context
->album
);
480 if (context
->year
!= 0)
481 mlt_properties_set_int(properties
, "meta.attr.year.markup", context
->year
);
482 if (context
->track
!= 0)
483 mlt_properties_set_int(properties
, "meta.attr.track.markup", context
->track
);
485 // We're going to cheat here - for a/v files, we will have two contexts (reasoning will be clear later)
486 if ( av
== 0 && audio_index
!= -1 && video_index
!= -1 )
488 // We'll use the open one as our video_context
489 mlt_properties_set_data( properties
, "video_context", context
, 0, producer_file_close
, NULL
);
491 // And open again for our audio context
492 av_open_input_file( &context
, file
, NULL
, 0, NULL
);
493 av_find_stream_info( context
);
496 mlt_properties_set_data( properties
, "audio_context", context
, 0, producer_file_close
, NULL
);
498 else if ( av
!= 2 && video_index
!= -1 )
500 // We only have a video context
501 mlt_properties_set_data( properties
, "video_context", context
, 0, producer_file_close
, NULL
);
503 else if ( audio_index
!= -1 )
505 // We only have an audio context
506 mlt_properties_set_data( properties
, "audio_context", context
, 0, producer_file_close
, NULL
);
510 // Something has gone wrong
514 mlt_properties_set_int( properties
, "av_bypass", av_bypass
);
518 // Unlock the mutex now
524 /** Convert a frame position to a time code.
527 static double producer_time_of_frame( mlt_producer
this, mlt_position position
)
529 return ( double )position
/ mlt_producer_get_fps( this );
532 static inline void convert_image( AVFrame
*frame
, uint8_t *buffer
, int pix_fmt
, mlt_image_format format
, int width
, int height
)
535 if ( format
== mlt_image_yuv420p
)
537 struct SwsContext
*context
= sws_getContext( width
, height
, pix_fmt
,
538 width
, height
, PIX_FMT_YUV420P
, SWS_FAST_BILINEAR
, NULL
, NULL
, NULL
);
540 output
.data
[0] = buffer
;
541 output
.data
[1] = buffer
+ width
* height
;
542 output
.data
[2] = buffer
+ ( 3 * width
* height
) / 2;
543 output
.linesize
[0] = width
;
544 output
.linesize
[1] = width
>> 1;
545 output
.linesize
[2] = width
>> 1;
546 sws_scale( context
, frame
->data
, frame
->linesize
, 0, height
,
547 output
.data
, output
.linesize
);
548 sws_freeContext( context
);
550 else if ( format
== mlt_image_rgb24
)
552 struct SwsContext
*context
= sws_getContext( width
, height
, pix_fmt
,
553 width
, height
, PIX_FMT_RGB24
, SWS_FAST_BILINEAR
, NULL
, NULL
, NULL
);
555 avpicture_fill( &output
, buffer
, PIX_FMT_RGB24
, width
, height
);
556 sws_scale( context
, frame
->data
, frame
->linesize
, 0, height
,
557 output
.data
, output
.linesize
);
558 sws_freeContext( context
);
562 struct SwsContext
*context
= sws_getContext( width
, height
, pix_fmt
,
563 width
, height
, PIX_FMT_YUYV422
, SWS_FAST_BILINEAR
, NULL
, NULL
, NULL
);
565 avpicture_fill( &output
, buffer
, PIX_FMT_YUYV422
, width
, height
);
566 sws_scale( context
, frame
->data
, frame
->linesize
, 0, height
,
567 output
.data
, output
.linesize
);
568 sws_freeContext( context
);
571 if ( format
== mlt_image_yuv420p
)
574 pict
.data
[0] = buffer
;
575 pict
.data
[1] = buffer
+ width
* height
;
576 pict
.data
[2] = buffer
+ ( 3 * width
* height
) / 2;
577 pict
.linesize
[0] = width
;
578 pict
.linesize
[1] = width
>> 1;
579 pict
.linesize
[2] = width
>> 1;
580 img_convert( &pict
, PIX_FMT_YUV420P
, (AVPicture
*)frame
, pix_fmt
, width
, height
);
582 else if ( format
== mlt_image_rgb24
)
585 avpicture_fill( &output
, buffer
, PIX_FMT_RGB24
, width
, height
);
586 img_convert( &output
, PIX_FMT_RGB24
, (AVPicture
*)frame
, pix_fmt
, width
, height
);
591 avpicture_fill( &output
, buffer
, PIX_FMT_YUV422
, width
, height
);
592 img_convert( &output
, PIX_FMT_YUV422
, (AVPicture
*)frame
, pix_fmt
, width
, height
);
597 /** Allocate the image buffer and set it on the frame.
600 static int allocate_buffer( mlt_properties frame_properties
, AVCodecContext
*codec_context
, uint8_t **buffer
, mlt_image_format
*format
, int *width
, int *height
)
604 if ( codec_context
->width
== 0 || codec_context
->height
== 0 )
607 *width
= codec_context
->width
;
608 *height
= codec_context
->height
;
609 mlt_properties_set_int( frame_properties
, "width", *width
);
610 mlt_properties_set_int( frame_properties
, "height", *height
);
614 case mlt_image_yuv420p
:
615 size
= *width
* 3 * ( *height
+ 1 ) / 2;
617 case mlt_image_rgb24
:
618 size
= *width
* ( *height
+ 1 ) * 3;
621 *format
= mlt_image_yuv422
;
622 size
= *width
* ( *height
+ 1 ) * 2;
626 // Construct the output image
627 *buffer
= mlt_pool_alloc( size
);
629 mlt_properties_set_data( frame_properties
, "image", *buffer
, size
, (mlt_destructor
)mlt_pool_release
, NULL
);
636 /** Get an image from a frame.
639 static int producer_get_image( mlt_frame frame
, uint8_t **buffer
, mlt_image_format
*format
, int *width
, int *height
, int writable
)
641 // Get the properties from the frame
642 mlt_properties frame_properties
= MLT_FRAME_PROPERTIES( frame
);
644 // Obtain the frame number of this frame
645 mlt_position position
= mlt_properties_get_position( frame_properties
, "avformat_position" );
648 mlt_producer
this = mlt_properties_get_data( frame_properties
, "avformat_producer", NULL
);
650 // Get the producer properties
651 mlt_properties properties
= MLT_PRODUCER_PROPERTIES( this );
653 // Fetch the video_context
654 AVFormatContext
*context
= mlt_properties_get_data( properties
, "video_context", NULL
);
656 // Get the video_index
657 int index
= mlt_properties_get_int( properties
, "video_index" );
659 // Obtain the expected frame numer
660 mlt_position expected
= mlt_properties_get_position( properties
, "_video_expected" );
662 // Get the video stream
663 AVStream
*stream
= context
->streams
[ index
];
666 AVCodecContext
*codec_context
= stream
->codec
;
671 // Get the conversion frame
672 AVFrame
*av_frame
= mlt_properties_get_data( properties
, "av_frame", NULL
);
674 // Special case pause handling flag
677 // Special case ffwd handling
680 // We may want to use the source fps if available
681 double source_fps
= mlt_properties_get_double( properties
, "source_fps" );
682 double fps
= mlt_producer_get_fps( this );
684 // This is the physical frame position in the source
685 int req_position
= ( int )( position
/ fps
* source_fps
+ 0.5 );
687 // Get the seekable status
688 int seekable
= mlt_properties_get_int( properties
, "seekable" );
690 // Hopefully provide better support for streams...
691 int av_bypass
= mlt_properties_get_int( properties
, "av_bypass" );
693 // Determines if we have to decode all frames in a sequence
696 // Temporary hack to improve intra frame only
697 must_decode
= strcmp( codec_context
->codec
->name
, "mjpeg" ) &&
698 strcmp( codec_context
->codec
->name
, "rawvideo" ) &&
699 strcmp( codec_context
->codec
->name
, "dvvideo" );
702 if ( position
!= expected
)
704 if ( av_frame
!= NULL
&& position
+ 1 == expected
)
706 // We're paused - use last image
709 else if ( !seekable
&& position
> expected
&& ( position
- expected
) < 250 )
711 // Fast forward - seeking is inefficient for small distances - just ignore following frames
712 ignore
= ( int )( ( position
- expected
) / fps
* source_fps
);
714 else if ( seekable
&& ( position
< expected
|| position
- expected
>= 12 ) )
716 // Calculate the timestamp for the requested frame
717 int64_t timestamp
= ( int64_t )( ( double )req_position
/ source_fps
* AV_TIME_BASE
+ 0.5 );
718 if ( ( uint64_t )context
->start_time
!= AV_NOPTS_VALUE
)
719 timestamp
+= context
->start_time
;
721 timestamp
-= AV_TIME_BASE
;
725 // Set to the timestamp
726 av_seek_frame( context
, -1, timestamp
, AVSEEK_FLAG_BACKWARD
);
728 // Remove the cached info relating to the previous position
729 mlt_properties_set_int( properties
, "_current_position", -1 );
730 mlt_properties_set_int( properties
, "_last_position", -1 );
731 mlt_properties_set_data( properties
, "av_frame", NULL
, 0, NULL
, NULL
);
736 // Duplicate the last image if necessary (see comment on rawvideo below)
737 int current_position
= mlt_properties_get_int( properties
, "_current_position" );
738 int got_picture
= mlt_properties_get_int( properties
, "_got_picture" );
739 if ( av_frame
!= NULL
&& got_picture
&& ( paused
|| current_position
>= req_position
) && av_bypass
== 0 )
742 if ( allocate_buffer( frame_properties
, codec_context
, buffer
, format
, width
, height
) )
743 convert_image( av_frame
, *buffer
, codec_context
->pix_fmt
, *format
, *width
, *height
);
745 mlt_frame_get_image( frame
, buffer
, format
, width
, height
, writable
);
750 int int_position
= 0;
753 av_init_packet( &pkt
);
755 // Construct an AVFrame for YUV422 conversion
756 if ( av_frame
== NULL
)
757 av_frame
= avcodec_alloc_frame( );
759 while( ret
>= 0 && !got_picture
)
762 ret
= av_read_frame( context
, &pkt
);
764 // We only deal with video from the selected video_index
765 if ( ret
>= 0 && pkt
.stream_index
== index
&& pkt
.size
> 0 )
767 // Determine time code of the packet
768 int_position
= ( int )( av_q2d( stream
->time_base
) * pkt
.dts
* source_fps
+ 0.5 );
769 if ( context
->start_time
!= AV_NOPTS_VALUE
)
770 int_position
-= ( int )( context
->start_time
* source_fps
/ AV_TIME_BASE
+ 0.5 );
771 int last_position
= mlt_properties_get_int( properties
, "_last_position" );
772 if ( int_position
== last_position
)
773 int_position
= last_position
+ 1;
774 mlt_properties_set_int( properties
, "_last_position", int_position
);
777 if ( must_decode
|| int_position
>= req_position
)
778 ret
= avcodec_decode_video( codec_context
, av_frame
, &got_picture
, pkt
.data
, pkt
.size
);
783 if ( int_position
< req_position
)
788 else if ( int_position
>= req_position
)
792 else if ( ignore
-- )
797 av_free_packet( &pkt
);
801 av_free_packet( &pkt
);
804 // Now handle the picture if we have one
807 if ( allocate_buffer( frame_properties
, codec_context
, buffer
, format
, width
, height
) )
809 convert_image( av_frame
, *buffer
, codec_context
->pix_fmt
, *format
, *width
, *height
);
810 mlt_properties_set_int( frame_properties
, "progressive", !av_frame
->interlaced_frame
);
811 mlt_properties_set_int( properties
, "top_field_first", av_frame
->top_field_first
);
812 mlt_properties_set_int( properties
, "_current_position", int_position
);
813 mlt_properties_set_int( properties
, "_got_picture", 1 );
814 mlt_properties_set_data( properties
, "av_frame", av_frame
, 0, av_free
, NULL
);
823 mlt_frame_get_image( frame
, buffer
, format
, width
, height
, writable
);
826 // Very untidy - for rawvideo, the packet contains the frame, hence the free packet
827 // above will break the pause behaviour - so we wipe the frame now
828 if ( !strcmp( codec_context
->codec
->name
, "rawvideo" ) )
829 mlt_properties_set_data( properties
, "av_frame", NULL
, 0, NULL
, NULL
);
831 // Set the field order property for this frame
832 mlt_properties_set_int( frame_properties
, "top_field_first", mlt_properties_get_int( properties
, "top_field_first" ) );
834 // Regardless of speed, we expect to get the next frame (cos we ain't too bright)
835 mlt_properties_set_position( properties
, "_video_expected", position
+ 1 );
840 /** Process properties as AVOptions and apply to AV context obj
843 static void apply_properties( void *obj
, mlt_properties properties
, int flags
)
846 int count
= mlt_properties_count( properties
);
847 for ( i
= 0; i
< count
; i
++ )
849 const char *opt_name
= mlt_properties_get_name( properties
, i
);
850 const AVOption
*opt
= av_find_opt( obj
, opt_name
, NULL
, flags
, flags
);
852 #if LIBAVCODEC_VERSION_INT >= ((52<<16)+(7<<8)+0)
853 av_set_string3( obj
, opt_name
, mlt_properties_get( properties
, opt_name
), 0, NULL
);
854 #elif LIBAVCODEC_VERSION_INT >= ((51<<16)+(59<<8)+0)
855 av_set_string2( obj
, opt_name
, mlt_properties_get( properties
, opt_name
), 0 );
857 av_set_string( obj
, opt_name
, mlt_properties_get( properties
, opt_name
) );
862 /** Set up video handling.
865 static void producer_set_up_video( mlt_producer
this, mlt_frame frame
)
867 // Get the properties
868 mlt_properties properties
= MLT_PRODUCER_PROPERTIES( this );
870 // Fetch the video_context
871 AVFormatContext
*context
= mlt_properties_get_data( properties
, "video_context", NULL
);
873 // Get the video_index
874 int index
= mlt_properties_get_int( properties
, "video_index" );
876 // Reopen the file if necessary
877 if ( !context
&& index
> -1 )
879 mlt_events_block( properties
, this );
880 producer_open( this, mlt_service_profile( MLT_PRODUCER_SERVICE(this) ),
881 mlt_properties_get( properties
, "resource" ) );
882 context
= mlt_properties_get_data( properties
, "video_context", NULL
);
883 mlt_properties_set_data( properties
, "dummy_context", NULL
, 0, NULL
, NULL
);
884 mlt_events_unblock( properties
, this );
886 // Process properties as AVOptions
887 apply_properties( context
, properties
, AV_OPT_FLAG_DECODING_PARAM
);
890 // Exception handling for video_index
891 if ( context
&& index
>= (int) context
->nb_streams
)
893 // Get the last video stream
894 for ( index
= context
->nb_streams
- 1; index
>= 0 && context
->streams
[ index
]->codec
->codec_type
!= CODEC_TYPE_VIDEO
; --index
);
895 mlt_properties_set_int( properties
, "video_index", index
);
897 if ( context
&& index
> -1 && context
->streams
[ index
]->codec
->codec_type
!= CODEC_TYPE_VIDEO
)
899 // Invalidate the video stream
901 mlt_properties_set_int( properties
, "video_index", index
);
904 // Get the frame properties
905 mlt_properties frame_properties
= MLT_FRAME_PROPERTIES( frame
);
907 if ( context
&& index
> -1 )
909 // Get the video stream
910 AVStream
*stream
= context
->streams
[ index
];
913 AVCodecContext
*codec_context
= stream
->codec
;
916 AVCodec
*codec
= mlt_properties_get_data( properties
, "video_codec", NULL
);
918 // Update the video properties if the index changed
919 if ( index
!= mlt_properties_get_int( properties
, "_video_index" ) )
921 // Reset the video properties if the index changed
922 mlt_properties_set_int( properties
, "_video_index", index
);
923 mlt_properties_set_data( properties
, "video_codec", NULL
, 0, NULL
, NULL
);
924 mlt_properties_set_int( properties
, "width", codec_context
->width
);
925 mlt_properties_set_int( properties
, "height", codec_context
->height
);
926 // TODO: get the first usable AVPacket and reset the stream position
927 mlt_properties_set_double( properties
, "aspect_ratio",
928 get_aspect_ratio( context
->streams
[ index
], codec_context
, NULL
) );
932 // Initialise the codec if necessary
935 // Initialise multi-threading
936 int thread_count
= mlt_properties_get_int( properties
, "threads" );
937 if ( thread_count
== 0 && getenv( "MLT_AVFORMAT_THREADS" ) )
938 thread_count
= atoi( getenv( "MLT_AVFORMAT_THREADS" ) );
939 if ( thread_count
> 1 )
941 avcodec_thread_init( codec_context
, thread_count
);
942 codec_context
->thread_count
= thread_count
;
946 codec
= avcodec_find_decoder( codec_context
->codec_id
);
948 // If we don't have a codec and we can't initialise it, we can't do much more...
950 if ( codec
!= NULL
&& avcodec_open( codec_context
, codec
) >= 0 )
952 // Now store the codec with its destructor
953 mlt_properties_set_data( properties
, "video_codec", codec_context
, 0, producer_codec_close
, NULL
);
957 // Remember that we can't use this later
958 mlt_properties_set_int( properties
, "video_index", -1 );
963 // Process properties as AVOptions
964 apply_properties( codec_context
, properties
, AV_OPT_FLAG_VIDEO_PARAM
| AV_OPT_FLAG_DECODING_PARAM
);
967 // No codec, no show...
968 if ( codec
&& index
> -1 )
970 double source_fps
= 0;
971 double force_aspect_ratio
= mlt_properties_get_double( properties
, "force_aspect_ratio" );
972 double aspect_ratio
= ( force_aspect_ratio
> 0.0 ) ?
973 force_aspect_ratio
: mlt_properties_get_double( properties
, "aspect_ratio" );
976 source_fps
= ( double )codec_context
->time_base
.den
/ ( codec_context
->time_base
.num
== 0 ?
1 : codec_context
->time_base
.num
);
978 // We'll use fps if it's available
979 if ( source_fps
> 0 )
980 mlt_properties_set_double( properties
, "source_fps", source_fps
);
982 mlt_properties_set_double( properties
, "source_fps", mlt_producer_get_fps( this ) );
983 mlt_properties_set_double( properties
, "aspect_ratio", aspect_ratio
);
985 // Set the width and height
986 mlt_properties_set_int( frame_properties
, "width", codec_context
->width
);
987 mlt_properties_set_int( frame_properties
, "height", codec_context
->height
);
988 mlt_properties_set_int( frame_properties
, "real_width", codec_context
->width
);
989 mlt_properties_set_int( frame_properties
, "real_height", codec_context
->height
);
990 mlt_properties_set_double( frame_properties
, "aspect_ratio", aspect_ratio
);
992 mlt_frame_push_get_image( frame
, producer_get_image
);
993 mlt_properties_set_data( frame_properties
, "avformat_producer", this, 0, NULL
, NULL
);
997 mlt_properties_set_int( frame_properties
, "test_image", 1 );
1002 mlt_properties_set_int( frame_properties
, "test_image", 1 );
1006 /** Get the audio from a frame.
1009 static int producer_get_audio( mlt_frame frame
, int16_t **buffer
, mlt_audio_format
*format
, int *frequency
, int *channels
, int *samples
)
1011 // Get the properties from the frame
1012 mlt_properties frame_properties
= MLT_FRAME_PROPERTIES( frame
);
1014 // Obtain the frame number of this frame
1015 mlt_position position
= mlt_properties_get_position( frame_properties
, "avformat_position" );
1018 mlt_producer
this = mlt_properties_get_data( frame_properties
, "avformat_producer", NULL
);
1020 // Get the producer properties
1021 mlt_properties properties
= MLT_PRODUCER_PROPERTIES( this );
1023 // Fetch the audio_context
1024 AVFormatContext
*context
= mlt_properties_get_data( properties
, "audio_context", NULL
);
1026 // Get the audio_index
1027 int index
= mlt_properties_get_int( properties
, "audio_index" );
1029 // Get the seekable status
1030 int seekable
= mlt_properties_get_int( properties
, "seekable" );
1032 // Obtain the expected frame numer
1033 mlt_position expected
= mlt_properties_get_position( properties
, "_audio_expected" );
1035 // Obtain the resample context if it exists (not always needed)
1036 ReSampleContext
*resample
= mlt_properties_get_data( properties
, "audio_resample", NULL
);
1038 #if (LIBAVCODEC_VERSION_INT >= ((51<<16)+(71<<8)+0))
1039 // Get the format converter context if it exists
1040 AVAudioConvert
*convert
= mlt_properties_get_data( properties
, "audio_convert", NULL
);
1043 // Obtain the audio buffers
1044 int16_t *audio_buffer
= mlt_properties_get_data( properties
, "audio_buffer", NULL
);
1045 int16_t *decode_buffer
= mlt_properties_get_data( properties
, "decode_buffer", NULL
);
1046 int16_t *convert_buffer
= mlt_properties_get_data( properties
, "convert_buffer", NULL
);
1048 // Get amount of audio used
1049 int audio_used
= mlt_properties_get_int( properties
, "_audio_used" );
1051 // Calculate the real time code
1052 double real_timecode
= producer_time_of_frame( this, position
);
1054 // Get the audio stream
1055 AVStream
*stream
= context
->streams
[ index
];
1057 // Get codec context
1058 AVCodecContext
*codec_context
= stream
->codec
;
1063 // Number of frames to ignore (for ffwd)
1066 // Flag for paused (silence)
1069 // Check for resample and create if necessary
1070 if ( resample
== NULL
&& codec_context
->channels
<= 2 )
1072 // Create the resampler
1073 resample
= audio_resample_init( *channels
, codec_context
->channels
, *frequency
, codec_context
->sample_rate
);
1075 // And store it on properties
1076 mlt_properties_set_data( properties
, "audio_resample", resample
, 0, ( mlt_destructor
)audio_resample_close
, NULL
);
1078 else if ( resample
== NULL
)
1080 *channels
= codec_context
->channels
;
1081 *frequency
= codec_context
->sample_rate
;
1084 #if (LIBAVCODEC_VERSION_INT >= ((51<<16)+(71<<8)+0))
1085 // Check for audio format converter and create if necessary
1086 // TODO: support higher resolutions than 16-bit.
1087 if ( convert
== NULL
&& codec_context
->sample_fmt
!= SAMPLE_FMT_S16
)
1089 // Create single channel converter for interleaved with no mixing matrix
1090 convert
= av_audio_convert_alloc( SAMPLE_FMT_S16
, 1, codec_context
->sample_fmt
, 1, NULL
, 0 );
1091 mlt_properties_set_data( properties
, "audio_convert", convert
, 0, ( mlt_destructor
)av_audio_convert_free
, NULL
);
1095 // Check for audio buffer and create if necessary
1096 if ( audio_buffer
== NULL
)
1098 // Allocate the audio buffer
1099 audio_buffer
= mlt_pool_alloc( AVCODEC_MAX_AUDIO_FRAME_SIZE
* sizeof( int16_t ) );
1101 // And store it on properties for reuse
1102 mlt_properties_set_data( properties
, "audio_buffer", audio_buffer
, 0, ( mlt_destructor
)mlt_pool_release
, NULL
);
1105 // Check for decoder buffer and create if necessary
1106 if ( decode_buffer
== NULL
)
1108 // Allocate the audio buffer
1109 decode_buffer
= av_malloc( AVCODEC_MAX_AUDIO_FRAME_SIZE
* sizeof( int16_t ) );
1111 // And store it on properties for reuse
1112 mlt_properties_set_data( properties
, "decode_buffer", decode_buffer
, 0, ( mlt_destructor
)av_free
, NULL
);
1115 #if (LIBAVCODEC_VERSION_INT >= ((51<<16)+(71<<8)+0))
1116 // Check for format converter buffer and create if necessary
1117 if ( resample
&& convert
&& convert_buffer
== NULL
)
1119 // Allocate the audio buffer
1120 convert_buffer
= mlt_pool_alloc( AVCODEC_MAX_AUDIO_FRAME_SIZE
* sizeof( int16_t ) );
1122 // And store it on properties for reuse
1123 mlt_properties_set_data( properties
, "convert_buffer", convert_buffer
, 0, ( mlt_destructor
)mlt_pool_release
, NULL
);
1127 // Seek if necessary
1128 if ( position
!= expected
)
1130 if ( position
+ 1 == expected
)
1132 // We're paused - silence required
1135 else if ( !seekable
&& position
> expected
&& ( position
- expected
) < 250 )
1137 // Fast forward - seeking is inefficient for small distances - just ignore following frames
1138 ignore
= position
- expected
;
1140 else if ( position
< expected
|| position
- expected
>= 12 )
1142 // Set to the real timecode
1143 if ( av_seek_frame( context
, -1, mlt_properties_get_double( properties
, "_start_time" ) + real_timecode
* 1000000.0, AVSEEK_FLAG_BACKWARD
) != 0 )
1146 // Clear the usage in the audio buffer
1151 // Get the audio if required
1157 av_init_packet( &pkt
);
1159 while( ret
>= 0 && !got_audio
)
1161 // Check if the buffer already contains the samples required
1162 if ( audio_used
>= *samples
&& ignore
== 0 )
1169 ret
= av_read_frame( context
, &pkt
);
1172 uint8_t *ptr
= pkt
.data
;
1174 // We only deal with audio from the selected audio_index
1175 while ( ptr
!= NULL
&& ret
>= 0 && pkt
.stream_index
== index
&& len
> 0 )
1177 int data_size
= sizeof( int16_t ) * AVCODEC_MAX_AUDIO_FRAME_SIZE
;
1180 #if (LIBAVCODEC_VERSION_INT >= ((51<<16)+(29<<8)+0))
1181 ret
= avcodec_decode_audio2( codec_context
, decode_buffer
, &data_size
, ptr
, len
);
1183 ret
= avcodec_decode_audio( codec_context
, decode_buffer
, &data_size
, ptr
, len
);
1194 if ( data_size
> 0 )
1196 int src_stride
[6]= { av_get_bits_per_sample_format( codec_context
->sample_fmt
) / 8 };
1197 int dst_stride
[6]= { av_get_bits_per_sample_format( SAMPLE_FMT_S16
) / 8 };
1201 int16_t *source
= decode_buffer
;
1202 int16_t *dest
= &audio_buffer
[ audio_used
* *channels
];
1203 int convert_samples
= data_size
/ src_stride
[0];
1205 #if (LIBAVCODEC_VERSION_INT >= ((51<<16)+(71<<8)+0))
1208 const void *src_buf
[6] = { decode_buffer
};
1209 void *dst_buf
[6] = { convert_buffer
};
1210 av_audio_convert( convert
, dst_buf
, dst_stride
, src_buf
, src_stride
, convert_samples
);
1211 source
= convert_buffer
;
1214 audio_used
+= audio_resample( resample
, dest
, source
, convert_samples
/ codec_context
->channels
);
1218 #if (LIBAVCODEC_VERSION_INT >= ((51<<16)+(71<<8)+0))
1221 const void *src_buf
[6] = { decode_buffer
};
1222 void *dst_buf
[6] = { &audio_buffer
[ audio_used
* *channels
] };
1223 av_audio_convert( convert
, dst_buf
, dst_stride
, src_buf
, src_stride
, data_size
/ src_stride
[0] );
1228 memcpy( &audio_buffer
[ audio_used
* *channels
], decode_buffer
, data_size
);
1230 audio_used
+= data_size
/ *channels
/ src_stride
[0];
1234 while ( ignore
&& audio_used
> *samples
)
1237 audio_used
-= *samples
;
1238 memmove( audio_buffer
, &audio_buffer
[ *samples
* *channels
], audio_used
* sizeof( int16_t ) );
1242 // If we're behind, ignore this packet
1245 float current_pts
= av_q2d( stream
->time_base
) * pkt
.pts
;
1246 if ( seekable
&& ( !ignore
&& current_pts
<= ( real_timecode
- 0.02 ) ) )
1251 // We're finished with this packet regardless
1252 av_free_packet( &pkt
);
1255 *buffer
= mlt_pool_alloc( *samples
* *channels
* sizeof( int16_t ) );
1256 mlt_properties_set_data( frame_properties
, "audio", *buffer
, 0, ( mlt_destructor
)mlt_pool_release
, NULL
);
1258 // Now handle the audio if we have enough
1259 if ( audio_used
>= *samples
)
1261 memcpy( *buffer
, audio_buffer
, *samples
* *channels
* sizeof( int16_t ) );
1262 audio_used
-= *samples
;
1263 memmove( audio_buffer
, &audio_buffer
[ *samples
* *channels
], audio_used
* *channels
* sizeof( int16_t ) );
1267 memset( *buffer
, 0, *samples
* *channels
* sizeof( int16_t ) );
1270 // Store the number of audio samples still available
1271 mlt_properties_set_int( properties
, "_audio_used", audio_used
);
1275 // Get silence and don't touch the context
1276 mlt_frame_get_audio( frame
, buffer
, format
, frequency
, channels
, samples
);
1279 // Regardless of speed (other than paused), we expect to get the next frame
1281 mlt_properties_set_position( properties
, "_audio_expected", position
+ 1 );
1286 /** Set up audio handling.
1289 static void producer_set_up_audio( mlt_producer
this, mlt_frame frame
)
1291 // Get the properties
1292 mlt_properties properties
= MLT_PRODUCER_PROPERTIES( this );
1294 // Fetch the audio_context
1295 AVFormatContext
*context
= mlt_properties_get_data( properties
, "audio_context", NULL
);
1297 // Get the audio_index
1298 int index
= mlt_properties_get_int( properties
, "audio_index" );
1300 // Reopen the file if necessary
1301 if ( !context
&& index
> -1 )
1303 mlt_events_block( properties
, this );
1304 producer_open( this, mlt_service_profile( MLT_PRODUCER_SERVICE(this) ),
1305 mlt_properties_get( properties
, "resource" ) );
1306 context
= mlt_properties_get_data( properties
, "audio_context", NULL
);
1307 mlt_properties_set_data( properties
, "dummy_context", NULL
, 0, NULL
, NULL
);
1308 mlt_events_unblock( properties
, this );
1311 // Exception handling for audio_index
1312 if ( context
&& index
>= (int) context
->nb_streams
)
1314 for ( index
= context
->nb_streams
- 1; index
>= 0 && context
->streams
[ index
]->codec
->codec_type
!= CODEC_TYPE_AUDIO
; --index
);
1315 mlt_properties_set_int( properties
, "audio_index", index
);
1317 if ( context
&& index
> -1 && context
->streams
[ index
]->codec
->codec_type
!= CODEC_TYPE_AUDIO
)
1320 mlt_properties_set_int( properties
, "audio_index", index
);
1323 // Update the audio properties if the index changed
1324 if ( index
> -1 && index
!= mlt_properties_get_int( properties
, "_audio_index" ) )
1326 mlt_properties_set_int( properties
, "_audio_index", index
);
1327 mlt_properties_set_data( properties
, "audio_codec", NULL
, 0, NULL
, NULL
);
1330 // Deal with audio context
1331 if ( context
!= NULL
&& index
> -1 )
1333 // Get the frame properties
1334 mlt_properties frame_properties
= MLT_FRAME_PROPERTIES( frame
);
1336 // Get the audio stream
1337 AVStream
*stream
= context
->streams
[ index
];
1339 // Get codec context
1340 AVCodecContext
*codec_context
= stream
->codec
;
1343 AVCodec
*codec
= mlt_properties_get_data( properties
, "audio_codec", NULL
);
1345 // Initialise the codec if necessary
1346 if ( codec
== NULL
)
1349 codec
= avcodec_find_decoder( codec_context
->codec_id
);
1351 // If we don't have a codec and we can't initialise it, we can't do much more...
1353 if ( codec
!= NULL
&& avcodec_open( codec_context
, codec
) >= 0 )
1355 // Now store the codec with its destructor
1356 mlt_properties_set_data( properties
, "audio_codec", codec_context
, 0, producer_codec_close
, NULL
);
1361 // Remember that we can't use this later
1362 mlt_properties_set_int( properties
, "audio_index", -1 );
1367 // Process properties as AVOptions
1368 apply_properties( codec_context
, properties
, AV_OPT_FLAG_AUDIO_PARAM
| AV_OPT_FLAG_DECODING_PARAM
);
1371 // No codec, no show...
1372 if ( codec
&& index
> -1 )
1374 mlt_frame_push_audio( frame
, producer_get_audio
);
1375 mlt_properties_set_data( frame_properties
, "avformat_producer", this, 0, NULL
, NULL
);
1376 mlt_properties_set_int( frame_properties
, "frequency", codec_context
->sample_rate
);
1377 mlt_properties_set_int( frame_properties
, "channels", codec_context
->channels
);
1382 /** Our get frame implementation.
1385 static int producer_get_frame( mlt_producer
this, mlt_frame_ptr frame
, int index
)
1387 // Create an empty frame
1388 *frame
= mlt_frame_init( MLT_PRODUCER_SERVICE( this ) );
1390 // Update timecode on the frame we're creating
1391 mlt_frame_set_position( *frame
, mlt_producer_position( this ) );
1393 // Set the position of this producer
1394 mlt_properties_set_position( MLT_FRAME_PROPERTIES( *frame
), "avformat_position", mlt_producer_frame( this ) );
1397 producer_set_up_video( this, *frame
);
1400 producer_set_up_audio( this, *frame
);
1402 // Set the aspect_ratio
1403 mlt_properties_set_double( MLT_FRAME_PROPERTIES( *frame
), "aspect_ratio", mlt_properties_get_double( MLT_PRODUCER_PROPERTIES( this ), "aspect_ratio" ) );
1405 // Calculate the next timecode
1406 mlt_producer_prepare_next( this );