producer_avformat.c: bugfix (kdenlive-432) segfault when reusing previous AVFrame...
[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 av_frame = avcodec_alloc_frame( );
718
719 while( ret >= 0 && !got_picture )
720 {
721 // Read a packet
722 ret = av_read_frame( context, &pkt );
723
724 // We only deal with video from the selected video_index
725 if ( ret >= 0 && pkt.stream_index == index && pkt.size > 0 )
726 {
727 // Determine time code of the packet
728 int_position = ( int )( av_q2d( stream->time_base ) * pkt.dts * source_fps + 0.5 );
729 if ( context->start_time != AV_NOPTS_VALUE )
730 int_position -= ( int )( context->start_time * source_fps / AV_TIME_BASE + 0.5 );
731 int last_position = mlt_properties_get_int( properties, "_last_position" );
732 if ( int_position == last_position )
733 int_position = last_position + 1;
734 mlt_properties_set_int( properties, "_last_position", int_position );
735
736 // Decode the image
737 if ( must_decode || int_position >= req_position )
738 ret = avcodec_decode_video( codec_context, av_frame, &got_picture, pkt.data, pkt.size );
739
740 if ( got_picture )
741 {
742 // Handle ignore
743 if ( int_position < req_position )
744 {
745 ignore = 0;
746 got_picture = 0;
747 }
748 else if ( int_position >= req_position )
749 {
750 ignore = 0;
751 }
752 else if ( ignore -- )
753 {
754 got_picture = 0;
755 }
756 }
757 av_free_packet( &pkt );
758 }
759 else if ( ret >= 0 )
760 {
761 av_free_packet( &pkt );
762 }
763
764 // Now handle the picture if we have one
765 if ( got_picture )
766 {
767 if ( allocate_buffer( frame_properties, codec_context, buffer, format, width, height ) )
768 {
769 convert_image( av_frame, *buffer, codec_context->pix_fmt, *format, *width, *height );
770 mlt_properties_set_int( frame_properties, "progressive", !av_frame->interlaced_frame );
771 mlt_properties_set_int( properties, "top_field_first", av_frame->top_field_first );
772 mlt_properties_set_int( properties, "_current_position", int_position );
773 mlt_properties_set_int( properties, "_got_picture", 1 );
774 mlt_properties_set_data( properties, "av_frame", av_frame, 0, av_free, NULL );
775 }
776 else
777 {
778 got_picture = 0;
779 }
780 }
781 }
782 if ( !got_picture )
783 mlt_frame_get_image( frame, buffer, format, width, height, writable );
784 }
785
786 // Very untidy - for rawvideo, the packet contains the frame, hence the free packet
787 // above will break the pause behaviour - so we wipe the frame now
788 if ( !strcmp( codec_context->codec->name, "rawvideo" ) )
789 mlt_properties_set_data( properties, "av_frame", NULL, 0, NULL, NULL );
790
791 // Set the field order property for this frame
792 mlt_properties_set_int( frame_properties, "top_field_first", mlt_properties_get_int( properties, "top_field_first" ) );
793
794 // Regardless of speed, we expect to get the next frame (cos we ain't too bright)
795 mlt_properties_set_position( properties, "_video_expected", position + 1 );
796
797 return 0;
798 }
799
800 /** Set up video handling.
801 */
802
803 static void producer_set_up_video( mlt_producer this, mlt_frame frame )
804 {
805 // Get the properties
806 mlt_properties properties = MLT_PRODUCER_PROPERTIES( this );
807
808 // Fetch the video_context
809 AVFormatContext *context = mlt_properties_get_data( properties, "video_context", NULL );
810
811 // Get the video_index
812 int index = mlt_properties_get_int( properties, "video_index" );
813
814 // Reopen the file if necessary
815 if ( !context && index > -1 )
816 {
817 mlt_events_block( properties, this );
818 producer_open( this, mlt_service_profile( MLT_PRODUCER_SERVICE(this) ),
819 mlt_properties_get( properties, "resource" ) );
820 context = mlt_properties_get_data( properties, "video_context", NULL );
821 mlt_properties_set_data( properties, "dummy_context", NULL, 0, NULL, NULL );
822 mlt_events_unblock( properties, this );
823 }
824
825 // Exception handling for video_index
826 if ( context && index >= (int) context->nb_streams )
827 {
828 // Get the last video stream
829 for ( index = context->nb_streams - 1; index >= 0 && context->streams[ index ]->codec->codec_type != CODEC_TYPE_VIDEO; --index );
830 mlt_properties_set_int( properties, "video_index", index );
831 }
832 if ( context && index > -1 && context->streams[ index ]->codec->codec_type != CODEC_TYPE_VIDEO )
833 {
834 // Invalidate the video stream
835 index = -1;
836 mlt_properties_set_int( properties, "video_index", index );
837 }
838
839 // Get the frame properties
840 mlt_properties frame_properties = MLT_FRAME_PROPERTIES( frame );
841
842 if ( context && index > -1 )
843 {
844 // Get the video stream
845 AVStream *stream = context->streams[ index ];
846
847 // Get codec context
848 AVCodecContext *codec_context = stream->codec;
849
850 // Get the codec
851 AVCodec *codec = mlt_properties_get_data( properties, "video_codec", NULL );
852
853 // Update the video properties if the index changed
854 if ( index != mlt_properties_get_int( properties, "_video_index" ) )
855 {
856 // Reset the video properties if the index changed
857 mlt_properties_set_int( properties, "_video_index", index );
858 mlt_properties_set_data( properties, "video_codec", NULL, 0, NULL, NULL );
859 mlt_properties_set_int( properties, "width", codec_context->width );
860 mlt_properties_set_int( properties, "height", codec_context->height );
861 // TODO: get the first usable AVPacket and reset the stream position
862 mlt_properties_set_double( properties, "aspect_ratio",
863 get_aspect_ratio( context->streams[ index ], codec_context, NULL ) );
864 codec = NULL;
865 }
866
867 // Initialise the codec if necessary
868 if ( codec == NULL )
869 {
870 // Initialise multi-threading
871 int thread_count = mlt_properties_get_int( properties, "threads" );
872 if ( thread_count == 0 && getenv( "MLT_AVFORMAT_THREADS" ) )
873 thread_count = atoi( getenv( "MLT_AVFORMAT_THREADS" ) );
874 if ( thread_count > 1 )
875 {
876 avcodec_thread_init( codec_context, thread_count );
877 codec_context->thread_count = thread_count;
878 }
879
880 // Find the codec
881 codec = avcodec_find_decoder( codec_context->codec_id );
882
883 // If we don't have a codec and we can't initialise it, we can't do much more...
884 avformat_lock( );
885 if ( codec != NULL && avcodec_open( codec_context, codec ) >= 0 )
886 {
887 // Now store the codec with its destructor
888 mlt_properties_set_data( properties, "video_codec", codec_context, 0, producer_codec_close, NULL );
889 }
890 else
891 {
892 // Remember that we can't use this later
893 mlt_properties_set_int( properties, "video_index", -1 );
894 index = -1;
895 }
896 avformat_unlock( );
897 }
898
899 // No codec, no show...
900 if ( codec && index > -1 )
901 {
902 double source_fps = 0;
903 double force_aspect_ratio = mlt_properties_get_double( properties, "force_aspect_ratio" );
904 double aspect_ratio = ( force_aspect_ratio > 0.0 ) ?
905 force_aspect_ratio : mlt_properties_get_double( properties, "aspect_ratio" );
906
907 // Determine the fps
908 source_fps = ( double )codec_context->time_base.den / ( codec_context->time_base.num == 0 ? 1 : codec_context->time_base.num );
909
910 // We'll use fps if it's available
911 if ( source_fps > 0 )
912 mlt_properties_set_double( properties, "source_fps", source_fps );
913 else
914 mlt_properties_set_double( properties, "source_fps", mlt_producer_get_fps( this ) );
915 mlt_properties_set_double( properties, "aspect_ratio", aspect_ratio );
916
917 // Set the width and height
918 mlt_properties_set_int( frame_properties, "width", codec_context->width );
919 mlt_properties_set_int( frame_properties, "height", codec_context->height );
920 mlt_properties_set_double( frame_properties, "aspect_ratio", aspect_ratio );
921
922 mlt_frame_push_get_image( frame, producer_get_image );
923 mlt_properties_set_data( frame_properties, "avformat_producer", this, 0, NULL, NULL );
924 }
925 else
926 {
927 mlt_properties_set_int( frame_properties, "test_image", 1 );
928 }
929 }
930 else
931 {
932 mlt_properties_set_int( frame_properties, "test_image", 1 );
933 }
934 }
935
936 /** Get the audio from a frame.
937 */
938
939 static int producer_get_audio( mlt_frame frame, int16_t **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
940 {
941 // Get the properties from the frame
942 mlt_properties frame_properties = MLT_FRAME_PROPERTIES( frame );
943
944 // Obtain the frame number of this frame
945 mlt_position position = mlt_properties_get_position( frame_properties, "avformat_position" );
946
947 // Get the producer
948 mlt_producer this = mlt_properties_get_data( frame_properties, "avformat_producer", NULL );
949
950 // Get the producer properties
951 mlt_properties properties = MLT_PRODUCER_PROPERTIES( this );
952
953 // Fetch the audio_context
954 AVFormatContext *context = mlt_properties_get_data( properties, "audio_context", NULL );
955
956 // Get the audio_index
957 int index = mlt_properties_get_int( properties, "audio_index" );
958
959 // Get the seekable status
960 int seekable = mlt_properties_get_int( properties, "seekable" );
961
962 // Obtain the expected frame numer
963 mlt_position expected = mlt_properties_get_position( properties, "_audio_expected" );
964
965 // Obtain the resample context if it exists (not always needed)
966 ReSampleContext *resample = mlt_properties_get_data( properties, "audio_resample", NULL );
967
968 #if (LIBAVCODEC_VERSION_INT >= ((51<<16)+(71<<8)+0))
969 // Get the format converter context if it exists
970 AVAudioConvert *convert = mlt_properties_get_data( properties, "audio_convert", NULL );
971 #endif
972
973 // Obtain the audio buffers
974 int16_t *audio_buffer = mlt_properties_get_data( properties, "audio_buffer", NULL );
975 int16_t *decode_buffer = mlt_properties_get_data( properties, "decode_buffer", NULL );
976 int16_t *convert_buffer = mlt_properties_get_data( properties, "convert_buffer", NULL );
977
978 // Get amount of audio used
979 int audio_used = mlt_properties_get_int( properties, "_audio_used" );
980
981 // Calculate the real time code
982 double real_timecode = producer_time_of_frame( this, position );
983
984 // Get the audio stream
985 AVStream *stream = context->streams[ index ];
986
987 // Get codec context
988 AVCodecContext *codec_context = stream->codec;
989
990 // Packet
991 AVPacket pkt;
992
993 // Number of frames to ignore (for ffwd)
994 int ignore = 0;
995
996 // Flag for paused (silence)
997 int paused = 0;
998
999 // Check for resample and create if necessary
1000 if ( resample == NULL && codec_context->channels <= 2 )
1001 {
1002 // Create the resampler
1003 resample = audio_resample_init( *channels, codec_context->channels, *frequency, codec_context->sample_rate );
1004
1005 // And store it on properties
1006 mlt_properties_set_data( properties, "audio_resample", resample, 0, ( mlt_destructor )audio_resample_close, NULL );
1007 }
1008 else if ( resample == NULL )
1009 {
1010 *channels = codec_context->channels;
1011 *frequency = codec_context->sample_rate;
1012 }
1013
1014 #if (LIBAVCODEC_VERSION_INT >= ((51<<16)+(71<<8)+0))
1015 // Check for audio format converter and create if necessary
1016 // TODO: support higher resolutions than 16-bit.
1017 if ( convert == NULL && codec_context->sample_fmt != SAMPLE_FMT_S16 )
1018 {
1019 // Create single channel converter for interleaved with no mixing matrix
1020 convert = av_audio_convert_alloc( SAMPLE_FMT_S16, 1, codec_context->sample_fmt, 1, NULL, 0 );
1021 mlt_properties_set_data( properties, "audio_convert", convert, 0, ( mlt_destructor )av_audio_convert_free, NULL );
1022 }
1023 #endif
1024
1025 // Check for audio buffer and create if necessary
1026 if ( audio_buffer == NULL )
1027 {
1028 // Allocate the audio buffer
1029 audio_buffer = mlt_pool_alloc( AVCODEC_MAX_AUDIO_FRAME_SIZE * sizeof( int16_t ) );
1030
1031 // And store it on properties for reuse
1032 mlt_properties_set_data( properties, "audio_buffer", audio_buffer, 0, ( mlt_destructor )mlt_pool_release, NULL );
1033 }
1034
1035 // Check for decoder buffer and create if necessary
1036 if ( decode_buffer == NULL )
1037 {
1038 // Allocate the audio buffer
1039 decode_buffer = av_malloc( AVCODEC_MAX_AUDIO_FRAME_SIZE * sizeof( int16_t ) );
1040
1041 // And store it on properties for reuse
1042 mlt_properties_set_data( properties, "decode_buffer", decode_buffer, 0, ( mlt_destructor )av_free, NULL );
1043 }
1044
1045 #if (LIBAVCODEC_VERSION_INT >= ((51<<16)+(71<<8)+0))
1046 // Check for format converter buffer and create if necessary
1047 if ( resample && convert && convert_buffer == NULL )
1048 {
1049 // Allocate the audio buffer
1050 convert_buffer = mlt_pool_alloc( AVCODEC_MAX_AUDIO_FRAME_SIZE * sizeof( int16_t ) );
1051
1052 // And store it on properties for reuse
1053 mlt_properties_set_data( properties, "convert_buffer", convert_buffer, 0, ( mlt_destructor )mlt_pool_release, NULL );
1054 }
1055 #endif
1056
1057 // Seek if necessary
1058 if ( position != expected )
1059 {
1060 if ( position + 1 == expected )
1061 {
1062 // We're paused - silence required
1063 paused = 1;
1064 }
1065 else if ( !seekable && position > expected && ( position - expected ) < 250 )
1066 {
1067 // Fast forward - seeking is inefficient for small distances - just ignore following frames
1068 ignore = position - expected;
1069 }
1070 else if ( position < expected || position - expected >= 12 )
1071 {
1072 // Set to the real timecode
1073 if ( av_seek_frame( context, -1, mlt_properties_get_double( properties, "_start_time" ) + real_timecode * 1000000.0, AVSEEK_FLAG_BACKWARD ) != 0 )
1074 paused = 1;
1075
1076 // Clear the usage in the audio buffer
1077 audio_used = 0;
1078 }
1079 }
1080
1081 // Get the audio if required
1082 if ( !paused )
1083 {
1084 int ret = 0;
1085 int got_audio = 0;
1086
1087 av_init_packet( &pkt );
1088
1089 while( ret >= 0 && !got_audio )
1090 {
1091 // Check if the buffer already contains the samples required
1092 if ( audio_used >= *samples && ignore == 0 )
1093 {
1094 got_audio = 1;
1095 break;
1096 }
1097
1098 // Read a packet
1099 ret = av_read_frame( context, &pkt );
1100
1101 int len = pkt.size;
1102 uint8_t *ptr = pkt.data;
1103
1104 // We only deal with audio from the selected audio_index
1105 while ( ptr != NULL && ret >= 0 && pkt.stream_index == index && len > 0 )
1106 {
1107 int data_size = sizeof( int16_t ) * AVCODEC_MAX_AUDIO_FRAME_SIZE;
1108
1109 // Decode the audio
1110 #if (LIBAVCODEC_VERSION_INT >= ((51<<16)+(29<<8)+0))
1111 ret = avcodec_decode_audio2( codec_context, decode_buffer, &data_size, ptr, len );
1112 #else
1113 ret = avcodec_decode_audio( codec_context, decode_buffer, &data_size, ptr, len );
1114 #endif
1115 if ( ret < 0 )
1116 {
1117 ret = 0;
1118 break;
1119 }
1120
1121 len -= ret;
1122 ptr += ret;
1123
1124 if ( data_size > 0 )
1125 {
1126 int src_stride[6]= { av_get_bits_per_sample_format( codec_context->sample_fmt ) / 8 };
1127 int dst_stride[6]= { av_get_bits_per_sample_format( SAMPLE_FMT_S16 ) / 8 };
1128
1129 if ( resample )
1130 {
1131 int16_t *source = decode_buffer;
1132 int16_t *dest = &audio_buffer[ audio_used * *channels ];
1133 int convert_samples = data_size / src_stride[0];
1134
1135 #if (LIBAVCODEC_VERSION_INT >= ((51<<16)+(71<<8)+0))
1136 if ( convert )
1137 {
1138 const void *src_buf[6] = { decode_buffer };
1139 void *dst_buf[6] = { convert_buffer };
1140 av_audio_convert( convert, dst_buf, dst_stride, src_buf, src_stride, convert_samples );
1141 source = convert_buffer;
1142 }
1143 #endif
1144 audio_used += audio_resample( resample, dest, source, convert_samples / codec_context->channels );
1145 }
1146 else
1147 {
1148 #if (LIBAVCODEC_VERSION_INT >= ((51<<16)+(71<<8)+0))
1149 if ( convert )
1150 {
1151 const void *src_buf[6] = { decode_buffer };
1152 void *dst_buf[6] = { &audio_buffer[ audio_used * *channels ] };
1153 av_audio_convert( convert, dst_buf, dst_stride, src_buf, src_stride, data_size / src_stride[0] );
1154 }
1155 else
1156 #endif
1157 {
1158 memcpy( &audio_buffer[ audio_used * *channels ], decode_buffer, data_size );
1159 }
1160 audio_used += data_size / *channels / src_stride[0];
1161 }
1162
1163 // Handle ignore
1164 while ( ignore && audio_used > *samples )
1165 {
1166 ignore --;
1167 audio_used -= *samples;
1168 memmove( audio_buffer, &audio_buffer[ *samples * *channels ], audio_used * sizeof( int16_t ) );
1169 }
1170 }
1171
1172 // If we're behind, ignore this packet
1173 if ( pkt.pts >= 0 )
1174 {
1175 float current_pts = av_q2d( stream->time_base ) * pkt.pts;
1176 if ( seekable && ( !ignore && current_pts <= ( real_timecode - 0.02 ) ) )
1177 ignore = 1;
1178 }
1179 }
1180
1181 // We're finished with this packet regardless
1182 av_free_packet( &pkt );
1183 }
1184
1185 *buffer = mlt_pool_alloc( *samples * *channels * sizeof( int16_t ) );
1186 mlt_properties_set_data( frame_properties, "audio", *buffer, 0, ( mlt_destructor )mlt_pool_release, NULL );
1187
1188 // Now handle the audio if we have enough
1189 if ( audio_used >= *samples )
1190 {
1191 memcpy( *buffer, audio_buffer, *samples * *channels * sizeof( int16_t ) );
1192 audio_used -= *samples;
1193 memmove( audio_buffer, &audio_buffer[ *samples * *channels ], audio_used * *channels * sizeof( int16_t ) );
1194 }
1195 else
1196 {
1197 memset( *buffer, 0, *samples * *channels * sizeof( int16_t ) );
1198 }
1199
1200 // Store the number of audio samples still available
1201 mlt_properties_set_int( properties, "_audio_used", audio_used );
1202 }
1203 else
1204 {
1205 // Get silence and don't touch the context
1206 mlt_frame_get_audio( frame, buffer, format, frequency, channels, samples );
1207 }
1208
1209 // Regardless of speed (other than paused), we expect to get the next frame
1210 if ( !paused )
1211 mlt_properties_set_position( properties, "_audio_expected", position + 1 );
1212
1213 return 0;
1214 }
1215
1216 /** Set up audio handling.
1217 */
1218
1219 static void producer_set_up_audio( mlt_producer this, mlt_frame frame )
1220 {
1221 // Get the properties
1222 mlt_properties properties = MLT_PRODUCER_PROPERTIES( this );
1223
1224 // Fetch the audio_context
1225 AVFormatContext *context = mlt_properties_get_data( properties, "audio_context", NULL );
1226
1227 // Get the audio_index
1228 int index = mlt_properties_get_int( properties, "audio_index" );
1229
1230 // Reopen the file if necessary
1231 if ( !context && index > -1 )
1232 {
1233 mlt_events_block( properties, this );
1234 producer_open( this, mlt_service_profile( MLT_PRODUCER_SERVICE(this) ),
1235 mlt_properties_get( properties, "resource" ) );
1236 context = mlt_properties_get_data( properties, "audio_context", NULL );
1237 mlt_properties_set_data( properties, "dummy_context", NULL, 0, NULL, NULL );
1238 mlt_events_unblock( properties, this );
1239 }
1240
1241 // Exception handling for audio_index
1242 if ( context && index >= (int) context->nb_streams )
1243 {
1244 for ( index = context->nb_streams - 1; index >= 0 && context->streams[ index ]->codec->codec_type != CODEC_TYPE_AUDIO; --index );
1245 mlt_properties_set_int( properties, "audio_index", index );
1246 }
1247 if ( context && index > -1 && context->streams[ index ]->codec->codec_type != CODEC_TYPE_AUDIO )
1248 {
1249 index = -1;
1250 mlt_properties_set_int( properties, "audio_index", index );
1251 }
1252
1253 // Update the audio properties if the index changed
1254 if ( index > -1 && index != mlt_properties_get_int( properties, "_audio_index" ) )
1255 {
1256 mlt_properties_set_int( properties, "_audio_index", index );
1257 mlt_properties_set_data( properties, "audio_codec", NULL, 0, NULL, NULL );
1258 }
1259
1260 // Deal with audio context
1261 if ( context != NULL && index > -1 )
1262 {
1263 // Get the frame properties
1264 mlt_properties frame_properties = MLT_FRAME_PROPERTIES( frame );
1265
1266 // Get the audio stream
1267 AVStream *stream = context->streams[ index ];
1268
1269 // Get codec context
1270 AVCodecContext *codec_context = stream->codec;
1271
1272 // Get the codec
1273 AVCodec *codec = mlt_properties_get_data( properties, "audio_codec", NULL );
1274
1275 // Initialise the codec if necessary
1276 if ( codec == NULL )
1277 {
1278 // Find the codec
1279 codec = avcodec_find_decoder( codec_context->codec_id );
1280
1281 // If we don't have a codec and we can't initialise it, we can't do much more...
1282 avformat_lock( );
1283 if ( codec != NULL && avcodec_open( codec_context, codec ) >= 0 )
1284 {
1285 // Now store the codec with its destructor
1286 mlt_properties_set_data( properties, "audio_codec", codec_context, 0, producer_codec_close, NULL );
1287
1288 }
1289 else
1290 {
1291 // Remember that we can't use this later
1292 mlt_properties_set_int( properties, "audio_index", -1 );
1293 index = -1;
1294 }
1295 avformat_unlock( );
1296 }
1297
1298 // No codec, no show...
1299 if ( codec && index > -1 )
1300 {
1301 mlt_frame_push_audio( frame, producer_get_audio );
1302 mlt_properties_set_data( frame_properties, "avformat_producer", this, 0, NULL, NULL );
1303 mlt_properties_set_int( frame_properties, "frequency", codec_context->sample_rate );
1304 mlt_properties_set_int( frame_properties, "channels", codec_context->channels );
1305 }
1306 }
1307 }
1308
1309 /** Our get frame implementation.
1310 */
1311
1312 static int producer_get_frame( mlt_producer this, mlt_frame_ptr frame, int index )
1313 {
1314 // Create an empty frame
1315 *frame = mlt_frame_init( MLT_PRODUCER_SERVICE( this ) );
1316
1317 // Update timecode on the frame we're creating
1318 mlt_frame_set_position( *frame, mlt_producer_position( this ) );
1319
1320 // Set the position of this producer
1321 mlt_properties_set_position( MLT_FRAME_PROPERTIES( *frame ), "avformat_position", mlt_producer_frame( this ) );
1322
1323 // Set up the video
1324 producer_set_up_video( this, *frame );
1325
1326 // Set up the audio
1327 producer_set_up_audio( this, *frame );
1328
1329 // Set the aspect_ratio
1330 mlt_properties_set_double( MLT_FRAME_PROPERTIES( *frame ), "aspect_ratio", mlt_properties_get_double( MLT_PRODUCER_PROPERTIES( this ), "aspect_ratio" ) );
1331
1332 // Calculate the next timecode
1333 mlt_producer_prepare_next( this );
1334
1335 return 0;
1336 }