producer_avformat.c: fix to previous commit to still allow compilation on version...
[melted] / src / modules / avformat / producer_avformat.c
1 /*
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
6 *
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.
11 *
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.
16 *
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
20 */
21
22 // MLT Header files
23 #include <framework/mlt_producer.h>
24 #include <framework/mlt_frame.h>
25
26 // ffmpeg Header files
27 #include <avformat.h>
28 #ifdef SWSCALE
29 # include <swscale.h>
30 #endif
31 #if (LIBAVCODEC_VERSION_INT >= ((51<<16)+(71<<8)+0))
32 # include "audioconvert.h"
33 #endif
34
35 // System header files
36 #include <stdlib.h>
37 #include <string.h>
38 #include <pthread.h>
39 #include <math.h>
40
41 void avformat_lock( );
42 void avformat_unlock( );
43
44 // Forward references.
45 static int producer_open( mlt_producer this, mlt_profile profile, char *file );
46 static int producer_get_frame( mlt_producer this, mlt_frame_ptr frame, int index );
47
48 /** Constructor for libavformat.
49 */
50
51 mlt_producer producer_avformat_init( mlt_profile profile, char *file )
52 {
53 mlt_producer this = NULL;
54
55 // Check that we have a non-NULL argument
56 if ( file != NULL )
57 {
58 // Construct the producer
59 this = calloc( 1, sizeof( struct mlt_producer_s ) );
60
61 // Initialise it
62 if ( mlt_producer_init( this, NULL ) == 0 )
63 {
64 // Get the properties
65 mlt_properties properties = MLT_PRODUCER_PROPERTIES( this );
66
67 // Set the resource property (required for all producers)
68 mlt_properties_set( properties, "resource", file );
69
70 // Register our get_frame implementation
71 this->get_frame = producer_get_frame;
72
73 // Open the file
74 if ( producer_open( this, profile, file ) != 0 )
75 {
76 // Clean up
77 mlt_producer_close( this );
78 this = NULL;
79 }
80 else
81 {
82 // Close the file to release resources for large playlists - reopen later as needed
83 mlt_properties_set_data( properties, "dummy_context", NULL, 0, NULL, NULL );
84 mlt_properties_set_data( properties, "audio_context", NULL, 0, NULL, NULL );
85 mlt_properties_set_data( properties, "video_context", NULL, 0, NULL, NULL );
86
87 // Default the user-selectable indices from the auto-detected indices
88 mlt_properties_set_int( properties, "audio_index", mlt_properties_get_int( properties, "_audio_index" ) );
89 mlt_properties_set_int( properties, "video_index", mlt_properties_get_int( properties, "_video_index" ) );
90 }
91 }
92 }
93
94 return this;
95 }
96
97 /** Find the default streams.
98 */
99
100 static void find_default_streams( AVFormatContext *context, int *audio_index, int *video_index )
101 {
102 int i;
103
104 // Allow for multiple audio and video streams in the file and select first of each (if available)
105 for( i = 0; i < context->nb_streams; i++ )
106 {
107 // Get the codec context
108 AVCodecContext *codec_context = context->streams[ i ]->codec;
109
110 if ( avcodec_find_decoder( codec_context->codec_id ) == NULL )
111 continue;
112
113 // Determine the type and obtain the first index of each type
114 switch( codec_context->codec_type )
115 {
116 case CODEC_TYPE_VIDEO:
117 if ( *video_index < 0 )
118 *video_index = i;
119 break;
120 case CODEC_TYPE_AUDIO:
121 if ( *audio_index < 0 )
122 *audio_index = i;
123 break;
124 default:
125 break;
126 }
127 }
128 }
129
130 /** Producer file destructor.
131 */
132
133 static void producer_file_close( void *context )
134 {
135 if ( context != NULL )
136 {
137 // Lock the mutex now
138 avformat_lock( );
139
140 // Close the file
141 av_close_input_file( context );
142
143 // Unlock the mutex now
144 avformat_unlock( );
145 }
146 }
147
148 /** Producer file destructor.
149 */
150
151 static void producer_codec_close( void *codec )
152 {
153 if ( codec != NULL )
154 {
155 // Lock the mutex now
156 avformat_lock( );
157
158 // Close the file
159 avcodec_close( codec );
160
161 // Unlock the mutex now
162 avformat_unlock( );
163 }
164 }
165
166 static inline int dv_is_pal( AVPacket *pkt )
167 {
168 return pkt->data[3] & 0x80;
169 }
170
171 static int dv_is_wide( AVPacket *pkt )
172 {
173 int i = 80 /* block size */ *3 /* VAUX starts at block 3 */ +3 /* skip block header */;
174
175 for ( ; i < pkt->size; i += 5 /* packet size */ )
176 {
177 if ( pkt->data[ i ] == 0x61 )
178 {
179 uint8_t x = pkt->data[ i + 2 ] & 0x7;
180 return ( x == 2 ) || ( x == 7 );
181 }
182 }
183 return 0;
184 }
185
186 static double get_aspect_ratio( AVStream *stream, AVCodecContext *codec_context, AVPacket *pkt )
187 {
188 double aspect_ratio = 1.0;
189
190 if ( codec_context->codec_id == CODEC_ID_DVVIDEO )
191 {
192 if ( pkt )
193 {
194 if ( dv_is_pal( pkt ) )
195 {
196 aspect_ratio = dv_is_wide( pkt )
197 ? 64.0/45.0 // 16:9 PAL
198 : 16.0/15.0; // 4:3 PAL
199 }
200 else
201 {
202 aspect_ratio = dv_is_wide( pkt )
203 ? 32.0/27.0 // 16:9 NTSC
204 : 8.0/9.0; // 4:3 NTSC
205 }
206 }
207 else
208 {
209 AVRational ar =
210 #if LIBAVFORMAT_VERSION_INT >= ((52<<16)+(21<<8)+0)
211 stream->sample_aspect_ratio;
212 #else
213 codec_context->sample_aspect_ratio;
214 #endif
215 // Override FFmpeg's notion of DV aspect ratios, which are
216 // based upon a width of 704. Since we do not have a normaliser
217 // that crops (nor is cropping 720 wide ITU-R 601 video always desirable)
218 // we just coerce the values to facilitate a passive behaviour through
219 // the rescale normaliser when using equivalent producers and consumers.
220 // = display_aspect / (width * height)
221 if ( ar.num == 10 && ar.den == 11 )
222 aspect_ratio = 8.0/9.0; // 4:3 NTSC
223 else if ( ar.num == 59 && ar.den == 54 )
224 aspect_ratio = 16.0/15.0; // 4:3 PAL
225 else if ( ar.num == 40 && ar.den == 33 )
226 aspect_ratio = 32.0/27.0; // 16:9 NTSC
227 else if ( ar.num == 118 && ar.den == 81 )
228 aspect_ratio = 64.0/45.0; // 16:9 PAL
229 }
230 }
231 else
232 {
233 AVRational codec_sar = codec_context->sample_aspect_ratio;
234 AVRational stream_sar =
235 #if LIBAVFORMAT_VERSION_INT >= ((52<<16)+(21<<8)+0)
236 stream->sample_aspect_ratio;
237 #else
238 { 0, 1 };
239 #endif
240 if ( codec_sar.num > 0 )
241 aspect_ratio = av_q2d( codec_sar );
242 else if ( stream_sar.num > 0 )
243 aspect_ratio = av_q2d( stream_sar );
244 }
245 return aspect_ratio;
246 }
247
248 /** Open the file.
249 */
250
251 static int producer_open( mlt_producer this, mlt_profile profile, char *file )
252 {
253 // Return an error code (0 == no error)
254 int error = 0;
255
256 // Context for avformat
257 AVFormatContext *context = NULL;
258
259 // Get the properties
260 mlt_properties properties = MLT_PRODUCER_PROPERTIES( this );
261
262 // We will treat everything with the producer fps
263 double fps = mlt_profile_fps( profile );
264
265 // Lock the mutex now
266 avformat_lock( );
267
268 // If "MRL", then create AVInputFormat
269 AVInputFormat *format = NULL;
270 AVFormatParameters *params = NULL;
271 char *standard = NULL;
272 char *mrl = strchr( file, ':' );
273
274 // AV option (0 = both, 1 = video, 2 = audio)
275 int av = 0;
276
277 // Setting lowest log level
278 av_log_set_level( -1 );
279
280 // Only if there is not a protocol specification that avformat can handle
281 if ( mrl && !url_exist( file ) )
282 {
283 // 'file' becomes format abbreviation
284 mrl[0] = 0;
285
286 // Lookup the format
287 format = av_find_input_format( file );
288
289 // Eat the format designator
290 file = ++mrl;
291
292 if ( format )
293 {
294 // Allocate params
295 params = calloc( sizeof( AVFormatParameters ), 1 );
296
297 // These are required by video4linux (defaults)
298 params->width = 640;
299 params->height = 480;
300 params->time_base= (AVRational){1,25};
301 // params->device = file;
302 params->channels = 2;
303 params->sample_rate = 48000;
304 }
305
306 // XXX: this does not work anymore since avdevice
307 // TODO: make producer_avddevice?
308 // Parse out params
309 mrl = strchr( file, '?' );
310 while ( mrl )
311 {
312 mrl[0] = 0;
313 char *name = strdup( ++mrl );
314 char *value = strchr( name, ':' );
315 if ( value )
316 {
317 value[0] = 0;
318 value++;
319 char *t = strchr( value, '&' );
320 if ( t )
321 t[0] = 0;
322 if ( !strcmp( name, "frame_rate" ) )
323 params->time_base.den = atoi( value );
324 else if ( !strcmp( name, "frame_rate_base" ) )
325 params->time_base.num = atoi( value );
326 else if ( !strcmp( name, "sample_rate" ) )
327 params->sample_rate = atoi( value );
328 else if ( !strcmp( name, "channels" ) )
329 params->channels = atoi( value );
330 else if ( !strcmp( name, "width" ) )
331 params->width = atoi( value );
332 else if ( !strcmp( name, "height" ) )
333 params->height = atoi( value );
334 else if ( !strcmp( name, "standard" ) )
335 {
336 standard = strdup( value );
337 params->standard = standard;
338 }
339 else if ( !strcmp( name, "av" ) )
340 av = atoi( value );
341 }
342 free( name );
343 mrl = strchr( mrl, '&' );
344 }
345 }
346
347 // Now attempt to open the file
348 error = av_open_input_file( &context, file, format, 0, params ) < 0;
349
350 // Cleanup AVFormatParameters
351 free( standard );
352 free( params );
353
354 // If successful, then try to get additional info
355 if ( error == 0 )
356 {
357 // Get the stream info
358 error = av_find_stream_info( context ) < 0;
359
360 // Continue if no error
361 if ( error == 0 )
362 {
363 // We will default to the first audio and video streams found
364 int audio_index = -1;
365 int video_index = -1;
366 int av_bypass = 0;
367
368 // Now set properties where we can (use default unknowns if required)
369 if ( context->duration != AV_NOPTS_VALUE )
370 {
371 // This isn't going to be accurate for all formats
372 mlt_position frames = ( mlt_position )( ( ( double )context->duration / ( double )AV_TIME_BASE ) * fps + 0.5 );
373 mlt_properties_set_position( properties, "out", frames - 1 );
374 mlt_properties_set_position( properties, "length", frames );
375 }
376
377 // Find default audio and video streams
378 find_default_streams( context, &audio_index, &video_index );
379
380 if ( context->start_time != AV_NOPTS_VALUE )
381 mlt_properties_set_double( properties, "_start_time", context->start_time );
382
383 // Check if we're seekable (something funny about mpeg here :-/)
384 if ( strcmp( file, "pipe:" ) && strncmp( file, "http://", 6 ) && strncmp( file, "udp:", 4 ) && strncmp( file, "tcp:", 4 ) && strncmp( file, "rtsp:", 5 ) && strncmp( file, "rtp:", 4 ) )
385 {
386 mlt_properties_set_int( properties, "seekable", av_seek_frame( context, -1, mlt_properties_get_double( properties, "_start_time" ), AVSEEK_FLAG_BACKWARD ) >= 0 );
387 mlt_properties_set_data( properties, "dummy_context", context, 0, producer_file_close, NULL );
388 av_open_input_file( &context, file, NULL, 0, NULL );
389 av_find_stream_info( context );
390 }
391 else
392 av_bypass = 1;
393
394 // Store selected audio and video indexes on properties
395 mlt_properties_set_int( properties, "_audio_index", audio_index );
396 mlt_properties_set_int( properties, "_video_index", video_index );
397 mlt_properties_set_int( properties, "_last_position", -1 );
398
399 // Fetch the width, height and aspect ratio
400 if ( video_index != -1 )
401 {
402 AVCodecContext *codec_context = context->streams[ video_index ]->codec;
403 mlt_properties_set_int( properties, "width", codec_context->width );
404 mlt_properties_set_int( properties, "height", codec_context->height );
405
406 if ( codec_context->codec_id == CODEC_ID_DVVIDEO )
407 {
408 // Fetch the first frame of DV so we can read it directly
409 AVPacket pkt;
410 int ret = 0;
411 while ( ret >= 0 )
412 {
413 ret = av_read_frame( context, &pkt );
414 if ( ret >= 0 && pkt.stream_index == video_index && pkt.size > 0 )
415 {
416 mlt_properties_set_double( properties, "aspect_ratio",
417 get_aspect_ratio( context->streams[ video_index ], codec_context, &pkt ) );
418 break;
419 }
420 }
421 }
422 else
423 {
424 mlt_properties_set_double( properties, "aspect_ratio",
425 get_aspect_ratio( context->streams[ video_index ], codec_context, NULL ) );
426 }
427 }
428
429 // Read Metadata
430 if (context->title != NULL)
431 mlt_properties_set(properties, "meta.attr.title.markup", context->title );
432 if (context->author != NULL)
433 mlt_properties_set(properties, "meta.attr.author.markup", context->author );
434 if (context->copyright != NULL)
435 mlt_properties_set(properties, "meta.attr.copyright.markup", context->copyright );
436 if (context->comment != NULL)
437 mlt_properties_set(properties, "meta.attr.comment.markup", context->comment );
438 if (context->album != NULL)
439 mlt_properties_set(properties, "meta.attr.album.markup", context->album );
440 if (context->year != 0)
441 mlt_properties_set_int(properties, "meta.attr.year.markup", context->year );
442 if (context->track != 0)
443 mlt_properties_set_int(properties, "meta.attr.track.markup", context->track );
444
445 // We're going to cheat here - for a/v files, we will have two contexts (reasoning will be clear later)
446 if ( av == 0 && audio_index != -1 && video_index != -1 )
447 {
448 // We'll use the open one as our video_context
449 mlt_properties_set_data( properties, "video_context", context, 0, producer_file_close, NULL );
450
451 // And open again for our audio context
452 av_open_input_file( &context, file, NULL, 0, NULL );
453 av_find_stream_info( context );
454
455 // Audio context
456 mlt_properties_set_data( properties, "audio_context", context, 0, producer_file_close, NULL );
457 }
458 else if ( av != 2 && video_index != -1 )
459 {
460 // We only have a video context
461 mlt_properties_set_data( properties, "video_context", context, 0, producer_file_close, NULL );
462 }
463 else if ( audio_index != -1 )
464 {
465 // We only have an audio context
466 mlt_properties_set_data( properties, "audio_context", context, 0, producer_file_close, NULL );
467 }
468 else
469 {
470 // Something has gone wrong
471 error = -1;
472 }
473
474 mlt_properties_set_int( properties, "av_bypass", av_bypass );
475 }
476 }
477
478 // Unlock the mutex now
479 avformat_unlock( );
480
481 return error;
482 }
483
484 /** Convert a frame position to a time code.
485 */
486
487 static double producer_time_of_frame( mlt_producer this, mlt_position position )
488 {
489 return ( double )position / mlt_producer_get_fps( this );
490 }
491
492 static inline void convert_image( AVFrame *frame, uint8_t *buffer, int pix_fmt, mlt_image_format format, int width, int height )
493 {
494 #ifdef SWSCALE
495 if ( format == mlt_image_yuv420p )
496 {
497 struct SwsContext *context = sws_getContext( width, height, pix_fmt,
498 width, height, PIX_FMT_YUV420P, SWS_FAST_BILINEAR, NULL, NULL, NULL);
499 AVPicture output;
500 output.data[0] = buffer;
501 output.data[1] = buffer + width * height;
502 output.data[2] = buffer + ( 3 * width * height ) / 2;
503 output.linesize[0] = width;
504 output.linesize[1] = width >> 1;
505 output.linesize[2] = width >> 1;
506 sws_scale( context, frame->data, frame->linesize, 0, height,
507 output.data, output.linesize);
508 sws_freeContext( context );
509 }
510 else if ( format == mlt_image_rgb24 )
511 {
512 struct SwsContext *context = sws_getContext( width, height, pix_fmt,
513 width, height, PIX_FMT_RGB24, SWS_FAST_BILINEAR, NULL, NULL, NULL);
514 AVPicture output;
515 avpicture_fill( &output, buffer, PIX_FMT_RGB24, width, height );
516 sws_scale( context, frame->data, frame->linesize, 0, height,
517 output.data, output.linesize);
518 sws_freeContext( context );
519 }
520 else
521 {
522 struct SwsContext *context = sws_getContext( width, height, pix_fmt,
523 width, height, PIX_FMT_YUYV422, SWS_FAST_BILINEAR, NULL, NULL, NULL);
524 AVPicture output;
525 avpicture_fill( &output, buffer, PIX_FMT_YUYV422, width, height );
526 sws_scale( context, frame->data, frame->linesize, 0, height,
527 output.data, output.linesize);
528 sws_freeContext( context );
529 }
530 #else
531 if ( format == mlt_image_yuv420p )
532 {
533 AVPicture pict;
534 pict.data[0] = buffer;
535 pict.data[1] = buffer + width * height;
536 pict.data[2] = buffer + ( 3 * width * height ) / 2;
537 pict.linesize[0] = width;
538 pict.linesize[1] = width >> 1;
539 pict.linesize[2] = width >> 1;
540 img_convert( &pict, PIX_FMT_YUV420P, (AVPicture *)frame, pix_fmt, width, height );
541 }
542 else if ( format == mlt_image_rgb24 )
543 {
544 AVPicture output;
545 avpicture_fill( &output, buffer, PIX_FMT_RGB24, width, height );
546 img_convert( &output, PIX_FMT_RGB24, (AVPicture *)frame, pix_fmt, width, height );
547 }
548 else
549 {
550 AVPicture output;
551 avpicture_fill( &output, buffer, PIX_FMT_YUV422, width, height );
552 img_convert( &output, PIX_FMT_YUV422, (AVPicture *)frame, pix_fmt, width, height );
553 }
554 #endif
555 }
556
557 /** Allocate the image buffer and set it on the frame.
558 */
559
560 static int allocate_buffer( mlt_properties frame_properties, AVCodecContext *codec_context, uint8_t **buffer, mlt_image_format *format, int *width, int *height )
561 {
562 int size = 0;
563
564 if ( codec_context->width == 0 || codec_context->height == 0 )
565 return size;
566
567 *width = codec_context->width;
568 *height = codec_context->height;
569 mlt_properties_set_int( frame_properties, "width", *width );
570 mlt_properties_set_int( frame_properties, "height", *height );
571
572 switch ( *format )
573 {
574 case mlt_image_yuv420p:
575 size = *width * 3 * ( *height + 1 ) / 2;
576 break;
577 case mlt_image_rgb24:
578 size = *width * ( *height + 1 ) * 3;
579 break;
580 default:
581 *format = mlt_image_yuv422;
582 size = *width * ( *height + 1 ) * 2;
583 break;
584 }
585
586 // Construct the output image
587 *buffer = mlt_pool_alloc( size );
588 if ( *buffer )
589 mlt_properties_set_data( frame_properties, "image", *buffer, size, (mlt_destructor)mlt_pool_release, NULL );
590 else
591 size = 0;
592
593 return size;
594 }
595
596 /** Get an image from a frame.
597 */
598
599 static int producer_get_image( mlt_frame frame, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable )
600 {
601 // Get the properties from the frame
602 mlt_properties frame_properties = MLT_FRAME_PROPERTIES( frame );
603
604 // Obtain the frame number of this frame
605 mlt_position position = mlt_properties_get_position( frame_properties, "avformat_position" );
606
607 // Get the producer
608 mlt_producer this = mlt_properties_get_data( frame_properties, "avformat_producer", NULL );
609
610 // Get the producer properties
611 mlt_properties properties = MLT_PRODUCER_PROPERTIES( this );
612
613 // Fetch the video_context
614 AVFormatContext *context = mlt_properties_get_data( properties, "video_context", NULL );
615
616 // Get the video_index
617 int index = mlt_properties_get_int( properties, "video_index" );
618
619 // Obtain the expected frame numer
620 mlt_position expected = mlt_properties_get_position( properties, "_video_expected" );
621
622 // Get the video stream
623 AVStream *stream = context->streams[ index ];
624
625 // Get codec context
626 AVCodecContext *codec_context = stream->codec;
627
628 // Packet
629 AVPacket pkt;
630
631 // Get the conversion frame
632 AVFrame *av_frame = mlt_properties_get_data( properties, "av_frame", NULL );
633
634 // Special case pause handling flag
635 int paused = 0;
636
637 // Special case ffwd handling
638 int ignore = 0;
639
640 // We may want to use the source fps if available
641 double source_fps = mlt_properties_get_double( properties, "source_fps" );
642 double fps = mlt_producer_get_fps( this );
643
644 // This is the physical frame position in the source
645 int req_position = ( int )( position / fps * source_fps + 0.5 );
646
647 // Get the seekable status
648 int seekable = mlt_properties_get_int( properties, "seekable" );
649
650 // Hopefully provide better support for streams...
651 int av_bypass = mlt_properties_get_int( properties, "av_bypass" );
652
653 // Determines if we have to decode all frames in a sequence
654 int must_decode = 1;
655
656 // Temporary hack to improve intra frame only
657 must_decode = strcmp( codec_context->codec->name, "mjpeg" ) &&
658 strcmp( codec_context->codec->name, "rawvideo" ) &&
659 strcmp( codec_context->codec->name, "dvvideo" );
660
661 // Seek if necessary
662 if ( position != expected )
663 {
664 if ( av_frame != NULL && position + 1 == expected )
665 {
666 // We're paused - use last image
667 paused = 1;
668 }
669 else if ( !seekable && position > expected && ( position - expected ) < 250 )
670 {
671 // Fast forward - seeking is inefficient for small distances - just ignore following frames
672 ignore = ( int )( ( position - expected ) / fps * source_fps );
673 }
674 else if ( seekable && ( position < expected || position - expected >= 12 ) )
675 {
676 // Calculate the timestamp for the requested frame
677 int64_t timestamp = ( int64_t )( ( double )req_position / source_fps * AV_TIME_BASE + 0.5 );
678 if ( ( uint64_t )context->start_time != AV_NOPTS_VALUE )
679 timestamp += context->start_time;
680 if ( must_decode )
681 timestamp -= AV_TIME_BASE;
682 if ( timestamp < 0 )
683 timestamp = 0;
684
685 // Set to the timestamp
686 av_seek_frame( context, -1, timestamp, AVSEEK_FLAG_BACKWARD );
687
688 // Remove the cached info relating to the previous position
689 mlt_properties_set_int( properties, "_current_position", -1 );
690 mlt_properties_set_int( properties, "_last_position", -1 );
691 mlt_properties_set_data( properties, "av_frame", NULL, 0, NULL, NULL );
692 av_frame = NULL;
693 }
694 }
695
696 // Duplicate the last image if necessary (see comment on rawvideo below)
697 int current_position = mlt_properties_get_int( properties, "_current_position" );
698 int got_picture = mlt_properties_get_int( properties, "_got_picture" );
699 if ( av_frame != NULL && got_picture && ( paused || current_position >= req_position ) && av_bypass == 0 )
700 {
701 // Duplicate it
702 if ( allocate_buffer( frame_properties, codec_context, buffer, format, width, height ) )
703 convert_image( av_frame, *buffer, codec_context->pix_fmt, *format, *width, *height );
704 else
705 mlt_frame_get_image( frame, buffer, format, width, height, writable );
706 }
707 else
708 {
709 int ret = 0;
710 int int_position = 0;
711 got_picture = 0;
712
713 av_init_packet( &pkt );
714
715 // Construct an AVFrame for YUV422 conversion
716 if ( av_frame == NULL )
717 {
718 av_frame = avcodec_alloc_frame( );
719 mlt_properties_set_data( properties, "av_frame", av_frame, 0, av_free, NULL );
720 }
721
722 while( ret >= 0 && !got_picture )
723 {
724 // Read a packet
725 ret = av_read_frame( context, &pkt );
726
727 // We only deal with video from the selected video_index
728 if ( ret >= 0 && pkt.stream_index == index && pkt.size > 0 )
729 {
730 // Determine time code of the packet
731 int_position = ( int )( av_q2d( stream->time_base ) * pkt.dts * source_fps + 0.5 );
732 if ( context->start_time != AV_NOPTS_VALUE )
733 int_position -= ( int )( context->start_time * source_fps / AV_TIME_BASE + 0.5 );
734 int last_position = mlt_properties_get_int( properties, "_last_position" );
735 if ( int_position == last_position )
736 int_position = last_position + 1;
737 mlt_properties_set_int( properties, "_last_position", int_position );
738
739 // Decode the image
740 if ( must_decode || int_position >= req_position )
741 ret = avcodec_decode_video( codec_context, av_frame, &got_picture, pkt.data, pkt.size );
742
743 if ( got_picture )
744 {
745 // Handle ignore
746 if ( int_position < req_position )
747 {
748 ignore = 0;
749 got_picture = 0;
750 }
751 else if ( int_position >= req_position )
752 {
753 ignore = 0;
754 }
755 else if ( ignore -- )
756 {
757 got_picture = 0;
758 }
759 }
760 av_free_packet( &pkt );
761 }
762 else if ( ret >= 0 )
763 {
764 av_free_packet( &pkt );
765 }
766
767 // Now handle the picture if we have one
768 if ( got_picture )
769 {
770 if ( allocate_buffer( frame_properties, codec_context, buffer, format, width, height ) )
771 {
772 convert_image( av_frame, *buffer, codec_context->pix_fmt, *format, *width, *height );
773 mlt_properties_set_int( frame_properties, "progressive", !av_frame->interlaced_frame );
774 mlt_properties_set_int( properties, "top_field_first", av_frame->top_field_first );
775 mlt_properties_set_int( properties, "_current_position", int_position );
776 mlt_properties_set_int( properties, "_got_picture", 1 );
777 }
778 else
779 {
780 got_picture = 0;
781 }
782 }
783 }
784 if ( !got_picture )
785 mlt_frame_get_image( frame, buffer, format, width, height, writable );
786 }
787
788 // Very untidy - for rawvideo, the packet contains the frame, hence the free packet
789 // above will break the pause behaviour - so we wipe the frame now
790 if ( !strcmp( codec_context->codec->name, "rawvideo" ) )
791 mlt_properties_set_data( properties, "av_frame", NULL, 0, NULL, NULL );
792
793 // Set the field order property for this frame
794 mlt_properties_set_int( frame_properties, "top_field_first", mlt_properties_get_int( properties, "top_field_first" ) );
795
796 // Regardless of speed, we expect to get the next frame (cos we ain't too bright)
797 mlt_properties_set_position( properties, "_video_expected", position + 1 );
798
799 return 0;
800 }
801
802 /** Set up video handling.
803 */
804
805 static void producer_set_up_video( mlt_producer this, mlt_frame frame )
806 {
807 // Get the properties
808 mlt_properties properties = MLT_PRODUCER_PROPERTIES( this );
809
810 // Fetch the video_context
811 AVFormatContext *context = mlt_properties_get_data( properties, "video_context", NULL );
812
813 // Get the video_index
814 int index = mlt_properties_get_int( properties, "video_index" );
815
816 // Reopen the file if necessary
817 if ( !context && index > -1 )
818 {
819 mlt_events_block( properties, this );
820 producer_open( this, mlt_service_profile( MLT_PRODUCER_SERVICE(this) ),
821 mlt_properties_get( properties, "resource" ) );
822 context = mlt_properties_get_data( properties, "video_context", NULL );
823 mlt_properties_set_data( properties, "dummy_context", NULL, 0, NULL, NULL );
824 mlt_events_unblock( properties, this );
825 }
826
827 // Exception handling for video_index
828 if ( context && index >= (int) context->nb_streams )
829 {
830 // Get the last video stream
831 for ( index = context->nb_streams - 1; index >= 0 && context->streams[ index ]->codec->codec_type != CODEC_TYPE_VIDEO; --index );
832 mlt_properties_set_int( properties, "video_index", index );
833 }
834 if ( context && index > -1 && context->streams[ index ]->codec->codec_type != CODEC_TYPE_VIDEO )
835 {
836 // Invalidate the video stream
837 index = -1;
838 mlt_properties_set_int( properties, "video_index", index );
839 }
840
841 // Get the frame properties
842 mlt_properties frame_properties = MLT_FRAME_PROPERTIES( frame );
843
844 if ( context && index > -1 )
845 {
846 // Get the video stream
847 AVStream *stream = context->streams[ index ];
848
849 // Get codec context
850 AVCodecContext *codec_context = stream->codec;
851
852 // Get the codec
853 AVCodec *codec = mlt_properties_get_data( properties, "video_codec", NULL );
854
855 // Update the video properties if the index changed
856 if ( index != mlt_properties_get_int( properties, "_video_index" ) )
857 {
858 // Reset the video properties if the index changed
859 mlt_properties_set_int( properties, "_video_index", index );
860 mlt_properties_set_data( properties, "video_codec", NULL, 0, NULL, NULL );
861 mlt_properties_set_int( properties, "width", codec_context->width );
862 mlt_properties_set_int( properties, "height", codec_context->height );
863 // TODO: get the first usable AVPacket and reset the stream position
864 mlt_properties_set_double( properties, "aspect_ratio",
865 get_aspect_ratio( context->streams[ index ], codec_context, NULL ) );
866 codec = NULL;
867 }
868
869 // Initialise the codec if necessary
870 if ( codec == NULL )
871 {
872 // Initialise multi-threading
873 int thread_count = mlt_properties_get_int( properties, "threads" );
874 if ( thread_count == 0 && getenv( "MLT_AVFORMAT_THREADS" ) )
875 thread_count = atoi( getenv( "MLT_AVFORMAT_THREADS" ) );
876 if ( thread_count > 1 )
877 {
878 avcodec_thread_init( codec_context, thread_count );
879 codec_context->thread_count = thread_count;
880 }
881
882 // Find the codec
883 codec = avcodec_find_decoder( codec_context->codec_id );
884
885 // If we don't have a codec and we can't initialise it, we can't do much more...
886 avformat_lock( );
887 if ( codec != NULL && avcodec_open( codec_context, codec ) >= 0 )
888 {
889 // Now store the codec with its destructor
890 mlt_properties_set_data( properties, "video_codec", codec_context, 0, producer_codec_close, NULL );
891 }
892 else
893 {
894 // Remember that we can't use this later
895 mlt_properties_set_int( properties, "video_index", -1 );
896 index = -1;
897 }
898 avformat_unlock( );
899 }
900
901 // No codec, no show...
902 if ( codec && index > -1 )
903 {
904 double source_fps = 0;
905 double force_aspect_ratio = mlt_properties_get_double( properties, "force_aspect_ratio" );
906 double aspect_ratio = ( force_aspect_ratio > 0.0 ) ?
907 force_aspect_ratio : mlt_properties_get_double( properties, "aspect_ratio" );
908
909 // Determine the fps
910 source_fps = ( double )codec_context->time_base.den / ( codec_context->time_base.num == 0 ? 1 : codec_context->time_base.num );
911
912 // We'll use fps if it's available
913 if ( source_fps > 0 )
914 mlt_properties_set_double( properties, "source_fps", source_fps );
915 else
916 mlt_properties_set_double( properties, "source_fps", mlt_producer_get_fps( this ) );
917 mlt_properties_set_double( properties, "aspect_ratio", aspect_ratio );
918
919 // Set the width and height
920 mlt_properties_set_int( frame_properties, "width", codec_context->width );
921 mlt_properties_set_int( frame_properties, "height", codec_context->height );
922 mlt_properties_set_double( frame_properties, "aspect_ratio", aspect_ratio );
923
924 mlt_frame_push_get_image( frame, producer_get_image );
925 mlt_properties_set_data( frame_properties, "avformat_producer", this, 0, NULL, NULL );
926 }
927 else
928 {
929 mlt_properties_set_int( frame_properties, "test_image", 1 );
930 }
931 }
932 else
933 {
934 mlt_properties_set_int( frame_properties, "test_image", 1 );
935 }
936 }
937
938 /** Get the audio from a frame.
939 */
940
941 static int producer_get_audio( mlt_frame frame, int16_t **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
942 {
943 // Get the properties from the frame
944 mlt_properties frame_properties = MLT_FRAME_PROPERTIES( frame );
945
946 // Obtain the frame number of this frame
947 mlt_position position = mlt_properties_get_position( frame_properties, "avformat_position" );
948
949 // Get the producer
950 mlt_producer this = mlt_properties_get_data( frame_properties, "avformat_producer", NULL );
951
952 // Get the producer properties
953 mlt_properties properties = MLT_PRODUCER_PROPERTIES( this );
954
955 // Fetch the audio_context
956 AVFormatContext *context = mlt_properties_get_data( properties, "audio_context", NULL );
957
958 // Get the audio_index
959 int index = mlt_properties_get_int( properties, "audio_index" );
960
961 // Get the seekable status
962 int seekable = mlt_properties_get_int( properties, "seekable" );
963
964 // Obtain the expected frame numer
965 mlt_position expected = mlt_properties_get_position( properties, "_audio_expected" );
966
967 // Obtain the resample context if it exists (not always needed)
968 ReSampleContext *resample = mlt_properties_get_data( properties, "audio_resample", NULL );
969
970 #if (LIBAVCODEC_VERSION_INT >= ((51<<16)+(71<<8)+0))
971 // Get the format converter context if it exists
972 AVAudioConvert *convert = mlt_properties_get_data( properties, "audio_convert", NULL );
973 #endif
974
975 // Obtain the audio buffers
976 int16_t *audio_buffer = mlt_properties_get_data( properties, "audio_buffer", NULL );
977 int16_t *decode_buffer = mlt_properties_get_data( properties, "decode_buffer", NULL );
978 int16_t *convert_buffer = mlt_properties_get_data( properties, "convert_buffer", NULL );
979
980 // Get amount of audio used
981 int audio_used = mlt_properties_get_int( properties, "_audio_used" );
982
983 // Calculate the real time code
984 double real_timecode = producer_time_of_frame( this, position );
985
986 // Get the audio stream
987 AVStream *stream = context->streams[ index ];
988
989 // Get codec context
990 AVCodecContext *codec_context = stream->codec;
991
992 // Packet
993 AVPacket pkt;
994
995 // Number of frames to ignore (for ffwd)
996 int ignore = 0;
997
998 // Flag for paused (silence)
999 int paused = 0;
1000
1001 // Check for resample and create if necessary
1002 if ( resample == NULL && ( *frequency != codec_context->sample_rate || codec_context->channels <= 2 ) )
1003 {
1004 // Create the resampler
1005 resample = audio_resample_init( *channels, codec_context->channels, *frequency, codec_context->sample_rate );
1006
1007 // And store it on properties
1008 mlt_properties_set_data( properties, "audio_resample", resample, 0, ( mlt_destructor )audio_resample_close, NULL );
1009 }
1010 else if ( resample == NULL )
1011 {
1012 *channels = codec_context->channels;
1013 *frequency = codec_context->sample_rate;
1014 }
1015
1016 #if (LIBAVCODEC_VERSION_INT >= ((51<<16)+(71<<8)+0))
1017 // Check for audio format converter and create if necessary
1018 // TODO: support higher resolutions than 16-bit.
1019 if ( convert == NULL && codec_context->sample_fmt != SAMPLE_FMT_S16 )
1020 {
1021 // Create single channel converter for interleaved with no mixing matrix
1022 convert = av_audio_convert_alloc( SAMPLE_FMT_S16, 1, codec_context->sample_fmt, 1, NULL, 0 );
1023 mlt_properties_set_data( properties, "audio_convert", convert, 0, ( mlt_destructor )av_audio_convert_free, NULL );
1024 }
1025 #endif
1026
1027 // Check for audio buffer and create if necessary
1028 if ( audio_buffer == NULL )
1029 {
1030 // Allocate the audio buffer
1031 audio_buffer = mlt_pool_alloc( AVCODEC_MAX_AUDIO_FRAME_SIZE * sizeof( int16_t ) );
1032
1033 // And store it on properties for reuse
1034 mlt_properties_set_data( properties, "audio_buffer", audio_buffer, 0, ( mlt_destructor )mlt_pool_release, NULL );
1035 }
1036
1037 // Check for decoder buffer and create if necessary
1038 if ( decode_buffer == NULL )
1039 {
1040 // Allocate the audio buffer
1041 decode_buffer = mlt_pool_alloc( AVCODEC_MAX_AUDIO_FRAME_SIZE * sizeof( int16_t ) );
1042
1043 // And store it on properties for reuse
1044 mlt_properties_set_data( properties, "decode_buffer", decode_buffer, 0, ( mlt_destructor )mlt_pool_release, NULL );
1045 }
1046
1047 #if (LIBAVCODEC_VERSION_INT >= ((51<<16)+(71<<8)+0))
1048 // Check for format converter buffer and create if necessary
1049 if ( resample && convert && convert_buffer == NULL )
1050 {
1051 // Allocate the audio buffer
1052 convert_buffer = mlt_pool_alloc( AVCODEC_MAX_AUDIO_FRAME_SIZE * sizeof( int16_t ) );
1053
1054 // And store it on properties for reuse
1055 mlt_properties_set_data( properties, "convert_buffer", convert_buffer, 0, ( mlt_destructor )mlt_pool_release, NULL );
1056 }
1057 #endif
1058
1059 // Seek if necessary
1060 if ( position != expected )
1061 {
1062 if ( position + 1 == expected )
1063 {
1064 // We're paused - silence required
1065 paused = 1;
1066 }
1067 else if ( !seekable && position > expected && ( position - expected ) < 250 )
1068 {
1069 // Fast forward - seeking is inefficient for small distances - just ignore following frames
1070 ignore = position - expected;
1071 }
1072 else if ( position < expected || position - expected >= 12 )
1073 {
1074 // Set to the real timecode
1075 if ( av_seek_frame( context, -1, mlt_properties_get_double( properties, "_start_time" ) + real_timecode * 1000000.0, AVSEEK_FLAG_BACKWARD ) != 0 )
1076 paused = 1;
1077
1078 // Clear the usage in the audio buffer
1079 audio_used = 0;
1080 }
1081 }
1082
1083 // Get the audio if required
1084 if ( !paused )
1085 {
1086 int ret = 0;
1087 int got_audio = 0;
1088
1089 av_init_packet( &pkt );
1090
1091 while( ret >= 0 && !got_audio )
1092 {
1093 // Check if the buffer already contains the samples required
1094 if ( audio_used >= *samples && ignore == 0 )
1095 {
1096 got_audio = 1;
1097 break;
1098 }
1099
1100 // Read a packet
1101 ret = av_read_frame( context, &pkt );
1102
1103 int len = pkt.size;
1104 uint8_t *ptr = pkt.data;
1105
1106 // We only deal with audio from the selected audio_index
1107 while ( ptr != NULL && ret >= 0 && pkt.stream_index == index && len > 0 )
1108 {
1109 int data_size = sizeof( int16_t ) * AVCODEC_MAX_AUDIO_FRAME_SIZE;
1110
1111 // Decode the audio
1112 #if (LIBAVCODEC_VERSION_INT >= ((51<<16)+(29<<8)+0))
1113 ret = avcodec_decode_audio2( codec_context, decode_buffer, &data_size, ptr, len );
1114 #else
1115 ret = avcodec_decode_audio( codec_context, decode_buffer, &data_size, ptr, len );
1116 #endif
1117 if ( ret < 0 )
1118 {
1119 ret = 0;
1120 break;
1121 }
1122
1123 len -= ret;
1124 ptr += ret;
1125
1126 if ( data_size > 0 )
1127 {
1128 int src_stride[6]= { av_get_bits_per_sample_format( codec_context->sample_fmt ) / 8 };
1129 int dst_stride[6]= { av_get_bits_per_sample_format( SAMPLE_FMT_S16 ) / 8 };
1130
1131 if ( resample )
1132 {
1133 int16_t *source = decode_buffer;
1134 int16_t *dest = &audio_buffer[ audio_used * *channels ];
1135 int convert_samples = data_size / src_stride[0];
1136
1137 #if (LIBAVCODEC_VERSION_INT >= ((51<<16)+(71<<8)+0))
1138 if ( convert )
1139 {
1140 const void *src_buf[6] = { decode_buffer };
1141 void *dst_buf[6] = { convert_buffer };
1142 av_audio_convert( convert, dst_buf, dst_stride, src_buf, src_stride, convert_samples );
1143 source = convert_buffer;
1144 }
1145 #endif
1146 audio_used += audio_resample( resample, dest, source, convert_samples / codec_context->channels );
1147 }
1148 else
1149 {
1150 #if (LIBAVCODEC_VERSION_INT >= ((51<<16)+(71<<8)+0))
1151 if ( convert )
1152 {
1153 const void *src_buf[6] = { decode_buffer };
1154 void *dst_buf[6] = { &audio_buffer[ audio_used * *channels ] };
1155 av_audio_convert( convert, dst_buf, dst_stride, src_buf, src_stride, data_size / src_stride[0] );
1156 }
1157 else
1158 #endif
1159 {
1160 memcpy( &audio_buffer[ audio_used * *channels ], decode_buffer, data_size );
1161 }
1162 audio_used += data_size / *channels / src_stride[0];
1163 }
1164
1165 // Handle ignore
1166 while ( ignore && audio_used > *samples )
1167 {
1168 ignore --;
1169 audio_used -= *samples;
1170 memmove( audio_buffer, &audio_buffer[ *samples * *channels ], audio_used * sizeof( int16_t ) );
1171 }
1172 }
1173
1174 // If we're behind, ignore this packet
1175 if ( pkt.pts >= 0 )
1176 {
1177 float current_pts = av_q2d( stream->time_base ) * pkt.pts;
1178 if ( seekable && ( !ignore && current_pts <= ( real_timecode - 0.02 ) ) )
1179 ignore = 1;
1180 }
1181 }
1182
1183 // We're finished with this packet regardless
1184 av_free_packet( &pkt );
1185 }
1186
1187 *buffer = mlt_pool_alloc( *samples * *channels * sizeof( int16_t ) );
1188 mlt_properties_set_data( frame_properties, "audio", *buffer, 0, ( mlt_destructor )mlt_pool_release, NULL );
1189
1190 // Now handle the audio if we have enough
1191 if ( audio_used >= *samples )
1192 {
1193 memcpy( *buffer, audio_buffer, *samples * *channels * sizeof( int16_t ) );
1194 audio_used -= *samples;
1195 memmove( audio_buffer, &audio_buffer[ *samples * *channels ], audio_used * *channels * sizeof( int16_t ) );
1196 }
1197 else
1198 {
1199 memset( *buffer, 0, *samples * *channels * sizeof( int16_t ) );
1200 }
1201
1202 // Store the number of audio samples still available
1203 mlt_properties_set_int( properties, "_audio_used", audio_used );
1204 }
1205 else
1206 {
1207 // Get silence and don't touch the context
1208 mlt_frame_get_audio( frame, buffer, format, frequency, channels, samples );
1209 }
1210
1211 // Regardless of speed (other than paused), we expect to get the next frame
1212 if ( !paused )
1213 mlt_properties_set_position( properties, "_audio_expected", position + 1 );
1214
1215 return 0;
1216 }
1217
1218 /** Set up audio handling.
1219 */
1220
1221 static void producer_set_up_audio( mlt_producer this, mlt_frame frame )
1222 {
1223 // Get the properties
1224 mlt_properties properties = MLT_PRODUCER_PROPERTIES( this );
1225
1226 // Fetch the audio_context
1227 AVFormatContext *context = mlt_properties_get_data( properties, "audio_context", NULL );
1228
1229 // Get the audio_index
1230 int index = mlt_properties_get_int( properties, "audio_index" );
1231
1232 // Reopen the file if necessary
1233 if ( !context && index > -1 )
1234 {
1235 mlt_events_block( properties, this );
1236 producer_open( this, mlt_service_profile( MLT_PRODUCER_SERVICE(this) ),
1237 mlt_properties_get( properties, "resource" ) );
1238 context = mlt_properties_get_data( properties, "audio_context", NULL );
1239 mlt_properties_set_data( properties, "dummy_context", NULL, 0, NULL, NULL );
1240 mlt_events_unblock( properties, this );
1241 }
1242
1243 // Exception handling for audio_index
1244 if ( context && index >= (int) context->nb_streams )
1245 {
1246 for ( index = context->nb_streams - 1; index >= 0 && context->streams[ index ]->codec->codec_type != CODEC_TYPE_AUDIO; --index );
1247 mlt_properties_set_int( properties, "audio_index", index );
1248 }
1249 if ( context && index > -1 && context->streams[ index ]->codec->codec_type != CODEC_TYPE_AUDIO )
1250 {
1251 index = -1;
1252 mlt_properties_set_int( properties, "audio_index", index );
1253 }
1254
1255 // Update the audio properties if the index changed
1256 if ( index > -1 && index != mlt_properties_get_int( properties, "_audio_index" ) )
1257 {
1258 mlt_properties_set_int( properties, "_audio_index", index );
1259 mlt_properties_set_data( properties, "audio_codec", NULL, 0, NULL, NULL );
1260 }
1261
1262 // Deal with audio context
1263 if ( context != NULL && index > -1 )
1264 {
1265 // Get the frame properties
1266 mlt_properties frame_properties = MLT_FRAME_PROPERTIES( frame );
1267
1268 // Get the audio stream
1269 AVStream *stream = context->streams[ index ];
1270
1271 // Get codec context
1272 AVCodecContext *codec_context = stream->codec;
1273
1274 // Get the codec
1275 AVCodec *codec = mlt_properties_get_data( properties, "audio_codec", NULL );
1276
1277 // Initialise the codec if necessary
1278 if ( codec == NULL )
1279 {
1280 // Find the codec
1281 codec = avcodec_find_decoder( codec_context->codec_id );
1282
1283 // If we don't have a codec and we can't initialise it, we can't do much more...
1284 avformat_lock( );
1285 if ( codec != NULL && avcodec_open( codec_context, codec ) >= 0 )
1286 {
1287 // Now store the codec with its destructor
1288 mlt_properties_set_data( properties, "audio_codec", codec_context, 0, producer_codec_close, NULL );
1289
1290 }
1291 else
1292 {
1293 // Remember that we can't use this later
1294 mlt_properties_set_int( properties, "audio_index", -1 );
1295 index = -1;
1296 }
1297 avformat_unlock( );
1298 }
1299
1300 // No codec, no show...
1301 if ( codec && index > -1 )
1302 {
1303 mlt_frame_push_audio( frame, producer_get_audio );
1304 mlt_properties_set_data( frame_properties, "avformat_producer", this, 0, NULL, NULL );
1305 mlt_properties_set_int( frame_properties, "frequency", codec_context->sample_rate );
1306 mlt_properties_set_int( frame_properties, "channels", codec_context->channels );
1307 }
1308 }
1309 }
1310
1311 /** Our get frame implementation.
1312 */
1313
1314 static int producer_get_frame( mlt_producer this, mlt_frame_ptr frame, int index )
1315 {
1316 // Create an empty frame
1317 *frame = mlt_frame_init( MLT_PRODUCER_SERVICE( this ) );
1318
1319 // Update timecode on the frame we're creating
1320 mlt_frame_set_position( *frame, mlt_producer_position( this ) );
1321
1322 // Set the position of this producer
1323 mlt_properties_set_position( MLT_FRAME_PROPERTIES( *frame ), "avformat_position", mlt_producer_frame( this ) );
1324
1325 // Set up the video
1326 producer_set_up_video( this, *frame );
1327
1328 // Set up the audio
1329 producer_set_up_audio( this, *frame );
1330
1331 // Set the aspect_ratio
1332 mlt_properties_set_double( MLT_FRAME_PROPERTIES( *frame ), "aspect_ratio", mlt_properties_get_double( MLT_PRODUCER_PROPERTIES( this ), "aspect_ratio" ) );
1333
1334 // Calculate the next timecode
1335 mlt_producer_prepare_next( this );
1336
1337 return 0;
1338 }